Blame view

block/blk-timeout.c 4.12 KB
3dcf60bcb   Christoph Hellwig   block: add SPDX t...
1
  // SPDX-License-Identifier: GPL-2.0
242f9dcb8   Jens Axboe   block: unify requ...
2
3
4
5
6
7
  /*
   * Functions related to generic timeout handling of requests.
   */
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/blkdev.h>
581d4e28d   Jens Axboe   block: add fault ...
8
  #include <linux/fault-inject.h>
242f9dcb8   Jens Axboe   block: unify requ...
9
10
  
  #include "blk.h"
904158376   Christoph Hellwig   block: fix blk_ab...
11
  #include "blk-mq.h"
242f9dcb8   Jens Axboe   block: unify requ...
12

581d4e28d   Jens Axboe   block: add fault ...
13
14
15
16
17
18
19
20
21
  #ifdef CONFIG_FAIL_IO_TIMEOUT
  
  static DECLARE_FAULT_ATTR(fail_io_timeout);
  
  static int __init setup_fail_io_timeout(char *str)
  {
  	return setup_fault_attr(&fail_io_timeout, str);
  }
  __setup("fail_io_timeout=", setup_fail_io_timeout);
15f73f5b3   Christoph Hellwig   blk-mq: move fail...
22
  bool __blk_should_fake_timeout(struct request_queue *q)
581d4e28d   Jens Axboe   block: add fault ...
23
  {
581d4e28d   Jens Axboe   block: add fault ...
24
25
  	return should_fail(&fail_io_timeout, 1);
  }
15f73f5b3   Christoph Hellwig   blk-mq: move fail...
26
  EXPORT_SYMBOL_GPL(__blk_should_fake_timeout);
581d4e28d   Jens Axboe   block: add fault ...
27
28
29
  
  static int __init fail_io_timeout_debugfs(void)
  {
dd48c085c   Akinobu Mita   fault-injection: ...
30
31
  	struct dentry *dir = fault_create_debugfs_attr("fail_io_timeout",
  						NULL, &fail_io_timeout);
8616ebb16   Duan Jiong   block: replace IS...
32
  	return PTR_ERR_OR_ZERO(dir);
581d4e28d   Jens Axboe   block: add fault ...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  }
  
  late_initcall(fail_io_timeout_debugfs);
  
  ssize_t part_timeout_show(struct device *dev, struct device_attribute *attr,
  			  char *buf)
  {
  	struct gendisk *disk = dev_to_disk(dev);
  	int set = test_bit(QUEUE_FLAG_FAIL_IO, &disk->queue->queue_flags);
  
  	return sprintf(buf, "%d
  ", set != 0);
  }
  
  ssize_t part_timeout_store(struct device *dev, struct device_attribute *attr,
  			   const char *buf, size_t count)
  {
  	struct gendisk *disk = dev_to_disk(dev);
  	int val;
  
  	if (count) {
  		struct request_queue *q = disk->queue;
  		char *p = (char *) buf;
  
  		val = simple_strtoul(p, &p, 10);
581d4e28d   Jens Axboe   block: add fault ...
58
  		if (val)
8814ce8a0   Bart Van Assche   block: Introduce ...
59
  			blk_queue_flag_set(QUEUE_FLAG_FAIL_IO, q);
581d4e28d   Jens Axboe   block: add fault ...
60
  		else
8814ce8a0   Bart Van Assche   block: Introduce ...
61
  			blk_queue_flag_clear(QUEUE_FLAG_FAIL_IO, q);
581d4e28d   Jens Axboe   block: add fault ...
62
63
64
65
66
67
  	}
  
  	return count;
  }
  
  #endif /* CONFIG_FAIL_IO_TIMEOUT */
242f9dcb8   Jens Axboe   block: unify requ...
68
  /**
d958e343b   Randy Dunlap   block: blk-timeou...
69
   * blk_abort_request - Request recovery for the specified command
242f9dcb8   Jens Axboe   block: unify requ...
70
71
72
73
74
   * @req:	pointer to the request of interest
   *
   * This function requests that the block layer start recovery for the
   * request by deleting the timer and calling the q's timeout function.
   * LLDDs who implement their own error recovery MAY ignore the timeout
39795d653   Christoph Hellwig   block: don't hold...
75
   * event if they generated blk_abort_request.
242f9dcb8   Jens Axboe   block: unify requ...
76
77
78
   */
  void blk_abort_request(struct request *req)
  {
4316b79e4   Jens Axboe   block: kill legac...
79
80
81
82
83
  	/*
  	 * All we need to ensure is that timeout scan takes place
  	 * immediately and that scan sees the new timeout value.
  	 * No need for fancy synchronizations.
  	 */
079076b34   Christoph Hellwig   block: remove dea...
84
  	WRITE_ONCE(req->deadline, jiffies);
4316b79e4   Jens Axboe   block: kill legac...
85
  	kblockd_schedule_work(&req->q->timeout_work);
242f9dcb8   Jens Axboe   block: unify requ...
86
87
  }
  EXPORT_SYMBOL_GPL(blk_abort_request);
9054650fa   Jens Axboe   block: relax jiff...
88
  static unsigned long blk_timeout_mask __read_mostly;
943c4d907   Wei Yongjun   block: make blk_t...
89
  static int __init blk_timeout_init(void)
9054650fa   Jens Axboe   block: relax jiff...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  {
  	blk_timeout_mask = roundup_pow_of_two(HZ) - 1;
  	return 0;
  }
  
  late_initcall(blk_timeout_init);
  
  /*
   * Just a rough estimate, we don't care about specific values for timeouts.
   */
  static inline unsigned long blk_round_jiffies(unsigned long j)
  {
  	return (j + blk_timeout_mask) + 1;
  }
0d2602ca3   Jens Axboe   blk-mq: improve s...
104
105
106
  unsigned long blk_rq_timeout(unsigned long timeout)
  {
  	unsigned long maxt;
9054650fa   Jens Axboe   block: relax jiff...
107
  	maxt = blk_round_jiffies(jiffies + BLK_MAX_TIMEOUT);
0d2602ca3   Jens Axboe   blk-mq: improve s...
108
109
110
111
112
  	if (time_after(timeout, maxt))
  		timeout = maxt;
  
  	return timeout;
  }
c4a634f43   Christoph Hellwig   block: fold __blk...
113
114
115
116
117
118
119
120
121
  /**
   * blk_add_timer - Start timeout timer for a single request
   * @req:	request that is about to start running.
   *
   * Notes:
   *    Each request has its own timer, and as it is added to the queue, we
   *    set up the timer. When the request completes, we cancel the timer.
   */
  void blk_add_timer(struct request *req)
242f9dcb8   Jens Axboe   block: unify requ...
122
123
124
  {
  	struct request_queue *q = req->q;
  	unsigned long expiry;
2eef33e43   Tejun Heo   block: clean up m...
125
126
127
128
129
  	/*
  	 * Some LLDs, like scsi, peek at the timeout to prevent a
  	 * command from being retried forever.
  	 */
  	if (!req->timeout)
242f9dcb8   Jens Axboe   block: unify requ...
130
  		req->timeout = q->rq_timeout;
2eef33e43   Tejun Heo   block: clean up m...
131

f5e350f02   Bart Van Assche   blk-mq: Fix timeo...
132
  	req->rq_flags &= ~RQF_TIMED_OUT;
079076b34   Christoph Hellwig   block: remove dea...
133
134
135
  
  	expiry = jiffies + req->timeout;
  	WRITE_ONCE(req->deadline, expiry);
3b627a3f9   Jens Axboe   block: clarify bl...
136
137
  
  	/*
242f9dcb8   Jens Axboe   block: unify requ...
138
  	 * If the timer isn't already pending or this timeout is earlier
7838c15b8   Alan Stern   Block: use round_...
139
  	 * than an existing one, modify the timer. Round up to next nearest
242f9dcb8   Jens Axboe   block: unify requ...
140
141
  	 * second.
  	 */
9054650fa   Jens Axboe   block: relax jiff...
142
  	expiry = blk_rq_timeout(blk_round_jiffies(expiry));
242f9dcb8   Jens Axboe   block: unify requ...
143
144
  
  	if (!timer_pending(&q->timeout) ||
f793aa537   Jens Axboe   block: relax when...
145
146
147
148
149
150
151
152
153
154
  	    time_before(expiry, q->timeout.expires)) {
  		unsigned long diff = q->timeout.expires - expiry;
  
  		/*
  		 * Due to added timer slack to group timers, the timer
  		 * will often be a little in front of what we asked for.
  		 * So apply some tolerance here too, otherwise we keep
  		 * modifying the timer because expires for value X
  		 * will be X + something.
  		 */
c7bca4183   Jens Axboe   block: ensure tha...
155
  		if (!timer_pending(&q->timeout) || (diff >= HZ / 2))
f793aa537   Jens Axboe   block: relax when...
156
157
  			mod_timer(&q->timeout, expiry);
  	}
320ae51fe   Jens Axboe   blk-mq: new multi...
158
159
  
  }