blk-timeout.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Functions related to generic timeout handling of requests.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/blkdev.h>
  7. #include <linux/fault-inject.h>
  8. #include "blk.h"
  9. #include "blk-mq.h"
  10. #ifdef CONFIG_FAIL_IO_TIMEOUT
  11. static DECLARE_FAULT_ATTR(fail_io_timeout);
  12. static int __init setup_fail_io_timeout(char *str)
  13. {
  14. return setup_fault_attr(&fail_io_timeout, str);
  15. }
  16. __setup("fail_io_timeout=", setup_fail_io_timeout);
  17. int blk_should_fake_timeout(struct request_queue *q)
  18. {
  19. if (!test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
  20. return 0;
  21. return should_fail(&fail_io_timeout, 1);
  22. }
  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_delete_timer - Delete/cancel timer for a given function.
  56. * @req: request that we are canceling timer for
  57. *
  58. */
  59. void blk_delete_timer(struct request *req)
  60. {
  61. list_del_init(&req->timeout_list);
  62. }
  63. static void blk_rq_timed_out(struct request *req)
  64. {
  65. struct request_queue *q = req->q;
  66. enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
  67. if (q->rq_timed_out_fn)
  68. ret = q->rq_timed_out_fn(req);
  69. switch (ret) {
  70. case BLK_EH_HANDLED:
  71. __blk_complete_request(req);
  72. break;
  73. case BLK_EH_RESET_TIMER:
  74. blk_add_timer(req);
  75. blk_clear_rq_complete(req);
  76. break;
  77. case BLK_EH_NOT_HANDLED:
  78. /*
  79. * LLD handles this for now but in the future
  80. * we can send a request msg to abort the command
  81. * and we can move more of the generic scsi eh code to
  82. * the blk layer.
  83. */
  84. break;
  85. default:
  86. printk(KERN_ERR "block: bad eh return: %d\n", ret);
  87. break;
  88. }
  89. }
  90. static void blk_rq_check_expired(struct request *rq, unsigned long *next_timeout,
  91. unsigned int *next_set)
  92. {
  93. const unsigned long deadline = blk_rq_deadline(rq);
  94. if (time_after_eq(jiffies, deadline)) {
  95. list_del_init(&rq->timeout_list);
  96. /*
  97. * Check if we raced with end io completion
  98. */
  99. if (!blk_mark_rq_complete(rq))
  100. blk_rq_timed_out(rq);
  101. } else if (!*next_set || time_after(*next_timeout, deadline)) {
  102. *next_timeout = deadline;
  103. *next_set = 1;
  104. }
  105. }
  106. void blk_timeout_work(struct work_struct *work)
  107. {
  108. struct request_queue *q =
  109. container_of(work, struct request_queue, timeout_work);
  110. unsigned long flags, next = 0;
  111. struct request *rq, *tmp;
  112. int next_set = 0;
  113. spin_lock_irqsave(q->queue_lock, flags);
  114. list_for_each_entry_safe(rq, tmp, &q->timeout_list, timeout_list)
  115. blk_rq_check_expired(rq, &next, &next_set);
  116. if (next_set)
  117. mod_timer(&q->timeout, round_jiffies_up(next));
  118. spin_unlock_irqrestore(q->queue_lock, flags);
  119. }
  120. /**
  121. * blk_abort_request -- Request request recovery for the specified command
  122. * @req: pointer to the request of interest
  123. *
  124. * This function requests that the block layer start recovery for the
  125. * request by deleting the timer and calling the q's timeout function.
  126. * LLDDs who implement their own error recovery MAY ignore the timeout
  127. * event if they generated blk_abort_req. Must hold queue lock.
  128. */
  129. void blk_abort_request(struct request *req)
  130. {
  131. if (req->q->mq_ops) {
  132. /*
  133. * All we need to ensure is that timeout scan takes place
  134. * immediately and that scan sees the new timeout value.
  135. * No need for fancy synchronizations.
  136. */
  137. blk_rq_set_deadline(req, jiffies);
  138. kblockd_schedule_work(&req->q->timeout_work);
  139. } else {
  140. if (blk_mark_rq_complete(req))
  141. return;
  142. blk_delete_timer(req);
  143. blk_rq_timed_out(req);
  144. }
  145. }
  146. EXPORT_SYMBOL_GPL(blk_abort_request);
  147. unsigned long blk_rq_timeout(unsigned long timeout)
  148. {
  149. unsigned long maxt;
  150. maxt = round_jiffies_up(jiffies + BLK_MAX_TIMEOUT);
  151. if (time_after(timeout, maxt))
  152. timeout = maxt;
  153. return timeout;
  154. }
  155. /**
  156. * blk_add_timer - Start timeout timer for a single request
  157. * @req: request that is about to start running.
  158. *
  159. * Notes:
  160. * Each request has its own timer, and as it is added to the queue, we
  161. * set up the timer. When the request completes, we cancel the timer.
  162. */
  163. void blk_add_timer(struct request *req)
  164. {
  165. struct request_queue *q = req->q;
  166. unsigned long expiry;
  167. if (!q->mq_ops)
  168. lockdep_assert_held(q->queue_lock);
  169. /* blk-mq has its own handler, so we don't need ->rq_timed_out_fn */
  170. if (!q->mq_ops && !q->rq_timed_out_fn)
  171. return;
  172. BUG_ON(!list_empty(&req->timeout_list));
  173. /*
  174. * Some LLDs, like scsi, peek at the timeout to prevent a
  175. * command from being retried forever.
  176. */
  177. if (!req->timeout)
  178. req->timeout = q->rq_timeout;
  179. blk_rq_set_deadline(req, jiffies + req->timeout);
  180. req->rq_flags &= ~RQF_MQ_TIMEOUT_EXPIRED;
  181. /*
  182. * Only the non-mq case needs to add the request to a protected list.
  183. * For the mq case we simply scan the tag map.
  184. */
  185. if (!q->mq_ops)
  186. list_add_tail(&req->timeout_list, &req->q->timeout_list);
  187. /*
  188. * If the timer isn't already pending or this timeout is earlier
  189. * than an existing one, modify the timer. Round up to next nearest
  190. * second.
  191. */
  192. expiry = blk_rq_timeout(round_jiffies_up(blk_rq_deadline(req)));
  193. if (!timer_pending(&q->timeout) ||
  194. time_before(expiry, q->timeout.expires)) {
  195. unsigned long diff = q->timeout.expires - expiry;
  196. /*
  197. * Due to added timer slack to group timers, the timer
  198. * will often be a little in front of what we asked for.
  199. * So apply some tolerance here too, otherwise we keep
  200. * modifying the timer because expires for value X
  201. * will be X + something.
  202. */
  203. if (!timer_pending(&q->timeout) || (diff >= HZ / 2))
  204. mod_timer(&q->timeout, expiry);
  205. }
  206. }