blk-timeout.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifdef CONFIG_FAIL_IO_TIMEOUT
  10. static DECLARE_FAULT_ATTR(fail_io_timeout);
  11. static int __init setup_fail_io_timeout(char *str)
  12. {
  13. return setup_fault_attr(&fail_io_timeout, str);
  14. }
  15. __setup("fail_io_timeout=", setup_fail_io_timeout);
  16. int blk_should_fake_timeout(struct request_queue *q)
  17. {
  18. if (!test_bit(QUEUE_FLAG_FAIL_IO, &q->queue_flags))
  19. return 0;
  20. return should_fail(&fail_io_timeout, 1);
  21. }
  22. static int __init fail_io_timeout_debugfs(void)
  23. {
  24. return init_fault_attr_dentries(&fail_io_timeout, "fail_io_timeout");
  25. }
  26. late_initcall(fail_io_timeout_debugfs);
  27. ssize_t part_timeout_show(struct device *dev, struct device_attribute *attr,
  28. char *buf)
  29. {
  30. struct gendisk *disk = dev_to_disk(dev);
  31. int set = test_bit(QUEUE_FLAG_FAIL_IO, &disk->queue->queue_flags);
  32. return sprintf(buf, "%d\n", set != 0);
  33. }
  34. ssize_t part_timeout_store(struct device *dev, struct device_attribute *attr,
  35. const char *buf, size_t count)
  36. {
  37. struct gendisk *disk = dev_to_disk(dev);
  38. int val;
  39. if (count) {
  40. struct request_queue *q = disk->queue;
  41. char *p = (char *) buf;
  42. val = simple_strtoul(p, &p, 10);
  43. spin_lock_irq(q->queue_lock);
  44. if (val)
  45. queue_flag_set(QUEUE_FLAG_FAIL_IO, q);
  46. else
  47. queue_flag_clear(QUEUE_FLAG_FAIL_IO, q);
  48. spin_unlock_irq(q->queue_lock);
  49. }
  50. return count;
  51. }
  52. #endif /* CONFIG_FAIL_IO_TIMEOUT */
  53. /*
  54. * blk_delete_timer - Delete/cancel timer for a given function.
  55. * @req: request that we are canceling timer for
  56. *
  57. */
  58. void blk_delete_timer(struct request *req)
  59. {
  60. list_del_init(&req->timeout_list);
  61. }
  62. static void blk_rq_timed_out(struct request *req)
  63. {
  64. struct request_queue *q = req->q;
  65. enum blk_eh_timer_return ret;
  66. ret = q->rq_timed_out_fn(req);
  67. switch (ret) {
  68. case BLK_EH_HANDLED:
  69. __blk_complete_request(req);
  70. break;
  71. case BLK_EH_RESET_TIMER:
  72. blk_clear_rq_complete(req);
  73. blk_add_timer(req);
  74. break;
  75. case BLK_EH_NOT_HANDLED:
  76. /*
  77. * LLD handles this for now but in the future
  78. * we can send a request msg to abort the command
  79. * and we can move more of the generic scsi eh code to
  80. * the blk layer.
  81. */
  82. break;
  83. default:
  84. printk(KERN_ERR "block: bad eh return: %d\n", ret);
  85. break;
  86. }
  87. }
  88. void blk_rq_timed_out_timer(unsigned long data)
  89. {
  90. struct request_queue *q = (struct request_queue *) data;
  91. unsigned long flags, next = 0;
  92. struct request *rq, *tmp;
  93. spin_lock_irqsave(q->queue_lock, flags);
  94. list_for_each_entry_safe(rq, tmp, &q->timeout_list, timeout_list) {
  95. if (time_after_eq(jiffies, rq->deadline)) {
  96. list_del_init(&rq->timeout_list);
  97. /*
  98. * Check if we raced with end io completion
  99. */
  100. if (blk_mark_rq_complete(rq))
  101. continue;
  102. blk_rq_timed_out(rq);
  103. } else {
  104. if (!next || time_after(next, rq->deadline))
  105. next = rq->deadline;
  106. }
  107. }
  108. /*
  109. * next can never be 0 here with the list non-empty, since we always
  110. * bump ->deadline to 1 so we can detect if the timer was ever added
  111. * or not. See comment in blk_add_timer()
  112. */
  113. if (next)
  114. mod_timer(&q->timeout, round_jiffies_up(next));
  115. spin_unlock_irqrestore(q->queue_lock, flags);
  116. }
  117. /**
  118. * blk_abort_request -- Request request recovery for the specified command
  119. * @req: pointer to the request of interest
  120. *
  121. * This function requests that the block layer start recovery for the
  122. * request by deleting the timer and calling the q's timeout function.
  123. * LLDDs who implement their own error recovery MAY ignore the timeout
  124. * event if they generated blk_abort_req. Must hold queue lock.
  125. */
  126. void blk_abort_request(struct request *req)
  127. {
  128. if (blk_mark_rq_complete(req))
  129. return;
  130. blk_delete_timer(req);
  131. blk_rq_timed_out(req);
  132. }
  133. EXPORT_SYMBOL_GPL(blk_abort_request);
  134. /**
  135. * blk_add_timer - Start timeout timer for a single request
  136. * @req: request that is about to start running.
  137. *
  138. * Notes:
  139. * Each request has its own timer, and as it is added to the queue, we
  140. * set up the timer. When the request completes, we cancel the timer.
  141. */
  142. void blk_add_timer(struct request *req)
  143. {
  144. struct request_queue *q = req->q;
  145. unsigned long expiry;
  146. if (!q->rq_timed_out_fn)
  147. return;
  148. BUG_ON(!list_empty(&req->timeout_list));
  149. BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
  150. if (req->timeout)
  151. req->deadline = jiffies + req->timeout;
  152. else {
  153. req->deadline = jiffies + q->rq_timeout;
  154. /*
  155. * Some LLDs, like scsi, peek at the timeout to prevent
  156. * a command from being retried forever.
  157. */
  158. req->timeout = q->rq_timeout;
  159. }
  160. list_add_tail(&req->timeout_list, &q->timeout_list);
  161. /*
  162. * If the timer isn't already pending or this timeout is earlier
  163. * than an existing one, modify the timer. Round up to next nearest
  164. * second.
  165. */
  166. expiry = round_jiffies_up(req->deadline);
  167. if (!timer_pending(&q->timeout) ||
  168. time_before(expiry, q->timeout.expires))
  169. mod_timer(&q->timeout, expiry);
  170. }
  171. /**
  172. * blk_abort_queue -- Abort all request on given queue
  173. * @queue: pointer to queue
  174. *
  175. */
  176. void blk_abort_queue(struct request_queue *q)
  177. {
  178. unsigned long flags;
  179. struct request *rq, *tmp;
  180. spin_lock_irqsave(q->queue_lock, flags);
  181. elv_abort_queue(q);
  182. list_for_each_entry_safe(rq, tmp, &q->timeout_list, timeout_list)
  183. blk_abort_request(rq);
  184. spin_unlock_irqrestore(q->queue_lock, flags);
  185. }
  186. EXPORT_SYMBOL_GPL(blk_abort_queue);