blk-timeout.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Functions related to generic timeout handling of requests.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/fault-inject.h>
  9. #include "blk.h"
  10. #include "blk-mq.h"
  11. #ifdef CONFIG_FAIL_IO_TIMEOUT
  12. static DECLARE_FAULT_ATTR(fail_io_timeout);
  13. static int __init setup_fail_io_timeout(char *str)
  14. {
  15. return setup_fault_attr(&fail_io_timeout, str);
  16. }
  17. __setup("fail_io_timeout=", setup_fail_io_timeout);
  18. bool __blk_should_fake_timeout(struct request_queue *q)
  19. {
  20. return should_fail(&fail_io_timeout, 1);
  21. }
  22. EXPORT_SYMBOL_GPL(__blk_should_fake_timeout);
  23. static int __init fail_io_timeout_debugfs(void)
  24. {
  25. struct dentry *dir = fault_create_debugfs_attr("fail_io_timeout",
  26. NULL, &fail_io_timeout);
  27. return PTR_ERR_OR_ZERO(dir);
  28. }
  29. late_initcall(fail_io_timeout_debugfs);
  30. ssize_t part_timeout_show(struct device *dev, struct device_attribute *attr,
  31. char *buf)
  32. {
  33. struct gendisk *disk = dev_to_disk(dev);
  34. int set = test_bit(QUEUE_FLAG_FAIL_IO, &disk->queue->queue_flags);
  35. return sprintf(buf, "%d\n", set != 0);
  36. }
  37. ssize_t part_timeout_store(struct device *dev, struct device_attribute *attr,
  38. const char *buf, size_t count)
  39. {
  40. struct gendisk *disk = dev_to_disk(dev);
  41. int val;
  42. if (count) {
  43. struct request_queue *q = disk->queue;
  44. char *p = (char *) buf;
  45. val = simple_strtoul(p, &p, 10);
  46. if (val)
  47. blk_queue_flag_set(QUEUE_FLAG_FAIL_IO, q);
  48. else
  49. blk_queue_flag_clear(QUEUE_FLAG_FAIL_IO, q);
  50. }
  51. return count;
  52. }
  53. #endif /* CONFIG_FAIL_IO_TIMEOUT */
  54. /**
  55. * blk_abort_request - Request recovery for the specified command
  56. * @req: pointer to the request of interest
  57. *
  58. * This function requests that the block layer start recovery for the
  59. * request by deleting the timer and calling the q's timeout function.
  60. * LLDDs who implement their own error recovery MAY ignore the timeout
  61. * event if they generated blk_abort_request.
  62. */
  63. void blk_abort_request(struct request *req)
  64. {
  65. /*
  66. * All we need to ensure is that timeout scan takes place
  67. * immediately and that scan sees the new timeout value.
  68. * No need for fancy synchronizations.
  69. */
  70. WRITE_ONCE(req->deadline, jiffies);
  71. kblockd_schedule_work(&req->q->timeout_work);
  72. }
  73. EXPORT_SYMBOL_GPL(blk_abort_request);
  74. static unsigned long blk_timeout_mask __read_mostly;
  75. static int __init blk_timeout_init(void)
  76. {
  77. blk_timeout_mask = roundup_pow_of_two(HZ) - 1;
  78. return 0;
  79. }
  80. late_initcall(blk_timeout_init);
  81. /*
  82. * Just a rough estimate, we don't care about specific values for timeouts.
  83. */
  84. static inline unsigned long blk_round_jiffies(unsigned long j)
  85. {
  86. return (j + blk_timeout_mask) + 1;
  87. }
  88. unsigned long blk_rq_timeout(unsigned long timeout)
  89. {
  90. unsigned long maxt;
  91. maxt = blk_round_jiffies(jiffies + BLK_MAX_TIMEOUT);
  92. if (time_after(timeout, maxt))
  93. timeout = maxt;
  94. return timeout;
  95. }
  96. /**
  97. * blk_add_timer - Start timeout timer for a single request
  98. * @req: request that is about to start running.
  99. *
  100. * Notes:
  101. * Each request has its own timer, and as it is added to the queue, we
  102. * set up the timer. When the request completes, we cancel the timer.
  103. */
  104. void blk_add_timer(struct request *req)
  105. {
  106. struct request_queue *q = req->q;
  107. unsigned long expiry;
  108. /*
  109. * Some LLDs, like scsi, peek at the timeout to prevent a
  110. * command from being retried forever.
  111. */
  112. if (!req->timeout)
  113. req->timeout = q->rq_timeout;
  114. req->rq_flags &= ~RQF_TIMED_OUT;
  115. expiry = jiffies + req->timeout;
  116. WRITE_ONCE(req->deadline, expiry);
  117. /*
  118. * If the timer isn't already pending or this timeout is earlier
  119. * than an existing one, modify the timer. Round up to next nearest
  120. * second.
  121. */
  122. expiry = blk_rq_timeout(blk_round_jiffies(expiry));
  123. if (!timer_pending(&q->timeout) ||
  124. time_before(expiry, q->timeout.expires)) {
  125. unsigned long diff = q->timeout.expires - expiry;
  126. /*
  127. * Due to added timer slack to group timers, the timer
  128. * will often be a little in front of what we asked for.
  129. * So apply some tolerance here too, otherwise we keep
  130. * modifying the timer because expires for value X
  131. * will be X + something.
  132. */
  133. if (!timer_pending(&q->timeout) || (diff >= HZ / 2))
  134. mod_timer(&q->timeout, expiry);
  135. }
  136. }