blob: 772347adc56b38290001fbce9a88d38cbf11ceeb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jens Axboe8324aa92008-01-29 14:51:59 +01002/*
3 * Functions related to sysfs handling
4 */
5#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Jens Axboe8324aa92008-01-29 14:51:59 +01007#include <linux/module.h>
8#include <linux/bio.h>
9#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040010#include <linux/backing-dev.h>
Jens Axboe8324aa92008-01-29 14:51:59 +010011#include <linux/blktrace_api.h>
Jens Axboe320ae512013-10-24 09:20:05 +010012#include <linux/blk-mq.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040013#include <linux/blk-cgroup.h>
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +000014#include <linux/debugfs.h>
Jens Axboe8324aa92008-01-29 14:51:59 +010015
16#include "blk.h"
Ming Lei3edcc0c2013-12-26 21:31:38 +080017#include "blk-mq.h"
Omar Sandovald173a252017-05-04 00:31:30 -070018#include "blk-mq-debugfs.h"
Jens Axboe87760e52016-11-09 12:38:14 -070019#include "blk-wbt.h"
Jens Axboea7b36ee2021-10-05 09:11:56 -060020#include "blk-throttle.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010021
22struct queue_sysfs_entry {
23 struct attribute attr;
24 ssize_t (*show)(struct request_queue *, char *);
25 ssize_t (*store)(struct request_queue *, const char *, size_t);
26};
27
28static ssize_t
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080029queue_var_show(unsigned long var, char *page)
Jens Axboe8324aa92008-01-29 14:51:59 +010030{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080031 return sprintf(page, "%lu\n", var);
Jens Axboe8324aa92008-01-29 14:51:59 +010032}
33
34static ssize_t
35queue_var_store(unsigned long *var, const char *page, size_t count)
36{
Dave Reisnerb1f3b642012-09-08 11:55:45 -040037 int err;
38 unsigned long v;
Jens Axboe8324aa92008-01-29 14:51:59 +010039
Jingoo Haned751e62013-09-11 14:20:08 -070040 err = kstrtoul(page, 10, &v);
Dave Reisnerb1f3b642012-09-08 11:55:45 -040041 if (err || v > UINT_MAX)
42 return -EINVAL;
43
44 *var = v;
45
Jens Axboe8324aa92008-01-29 14:51:59 +010046 return count;
47}
48
Jens Axboe80e091d2016-11-28 09:22:47 -070049static ssize_t queue_var_store64(s64 *var, const char *page)
Jens Axboe87760e52016-11-09 12:38:14 -070050{
51 int err;
Jens Axboe80e091d2016-11-28 09:22:47 -070052 s64 v;
Jens Axboe87760e52016-11-09 12:38:14 -070053
Jens Axboe80e091d2016-11-28 09:22:47 -070054 err = kstrtos64(page, 10, &v);
Jens Axboe87760e52016-11-09 12:38:14 -070055 if (err < 0)
56 return err;
57
58 *var = v;
59 return 0;
60}
61
Jens Axboe8324aa92008-01-29 14:51:59 +010062static ssize_t queue_requests_show(struct request_queue *q, char *page)
63{
Max Gurtovoy28af7422021-04-05 13:20:12 +000064 return queue_var_show(q->nr_requests, page);
Jens Axboe8324aa92008-01-29 14:51:59 +010065}
66
67static ssize_t
68queue_requests_store(struct request_queue *q, const char *page, size_t count)
69{
Jens Axboe8324aa92008-01-29 14:51:59 +010070 unsigned long nr;
Jens Axboee3a2b3f2014-05-20 11:49:02 -060071 int ret, err;
Jens Axboeb8a9ae72009-09-11 22:44:29 +020072
Jens Axboe344e9ff2018-11-15 12:22:51 -070073 if (!queue_is_mq(q))
Jens Axboeb8a9ae72009-09-11 22:44:29 +020074 return -EINVAL;
75
76 ret = queue_var_store(&nr, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -040077 if (ret < 0)
78 return ret;
79
Jens Axboe8324aa92008-01-29 14:51:59 +010080 if (nr < BLKDEV_MIN_RQ)
81 nr = BLKDEV_MIN_RQ;
82
Jens Axboea1ce35f2018-10-29 10:23:51 -060083 err = blk_mq_update_nr_requests(q, nr);
Jens Axboee3a2b3f2014-05-20 11:49:02 -060084 if (err)
85 return err;
Tejun Heoa0516612012-06-26 15:05:44 -070086
Jens Axboe8324aa92008-01-29 14:51:59 +010087 return ret;
88}
89
90static ssize_t queue_ra_show(struct request_queue *q, char *page)
91{
Christoph Hellwigedb08722021-08-09 16:17:43 +020092 unsigned long ra_kb;
Jens Axboe8324aa92008-01-29 14:51:59 +010093
Christoph Hellwigd152c682021-08-16 15:46:24 +020094 if (!q->disk)
Christoph Hellwigedb08722021-08-09 16:17:43 +020095 return -EINVAL;
Christoph Hellwigd152c682021-08-16 15:46:24 +020096 ra_kb = q->disk->bdi->ra_pages << (PAGE_SHIFT - 10);
Max Gurtovoy8c390ff2021-05-11 15:53:19 +000097 return queue_var_show(ra_kb, page);
Jens Axboe8324aa92008-01-29 14:51:59 +010098}
99
100static ssize_t
101queue_ra_store(struct request_queue *q, const char *page, size_t count)
102{
103 unsigned long ra_kb;
Christoph Hellwigedb08722021-08-09 16:17:43 +0200104 ssize_t ret;
Jens Axboe8324aa92008-01-29 14:51:59 +0100105
Christoph Hellwigd152c682021-08-16 15:46:24 +0200106 if (!q->disk)
Christoph Hellwigedb08722021-08-09 16:17:43 +0200107 return -EINVAL;
108 ret = queue_var_store(&ra_kb, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400109 if (ret < 0)
110 return ret;
Christoph Hellwigd152c682021-08-16 15:46:24 +0200111 q->disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +0100112 return ret;
113}
114
115static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
116{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400117 int max_sectors_kb = queue_max_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100118
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000119 return queue_var_show(max_sectors_kb, page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100120}
121
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500122static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
123{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000124 return queue_var_show(queue_max_segments(q), page);
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500125}
126
Christoph Hellwig1e739732017-02-08 14:46:49 +0100127static ssize_t queue_max_discard_segments_show(struct request_queue *q,
128 char *page)
129{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000130 return queue_var_show(queue_max_discard_segments(q), page);
Christoph Hellwig1e739732017-02-08 14:46:49 +0100131}
132
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200133static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
134{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000135 return queue_var_show(q->limits.max_integrity_segments, page);
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200136}
137
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500138static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
139{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000140 return queue_var_show(queue_max_segment_size(q), page);
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500141}
142
Martin K. Petersene1defc42009-05-22 17:17:49 -0400143static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
Martin K. Petersene68b9032008-01-29 19:14:08 +0100144{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400145 return queue_var_show(queue_logical_block_size(q), page);
Martin K. Petersene68b9032008-01-29 19:14:08 +0100146}
147
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400148static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
149{
150 return queue_var_show(queue_physical_block_size(q), page);
151}
152
Hannes Reinecke87caf972016-10-18 15:40:30 +0900153static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
154{
155 return queue_var_show(q->limits.chunk_sectors, page);
156}
157
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400158static ssize_t queue_io_min_show(struct request_queue *q, char *page)
159{
160 return queue_var_show(queue_io_min(q), page);
161}
162
163static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
164{
165 return queue_var_show(queue_io_opt(q), page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100166}
167
Martin K. Petersen86b37282009-11-10 11:50:21 +0100168static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
169{
170 return queue_var_show(q->limits.discard_granularity, page);
171}
172
Jens Axboe0034af02015-07-16 09:14:26 -0600173static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
174{
Jens Axboe0034af02015-07-16 09:14:26 -0600175
Alan18f922d02016-02-17 14:15:30 +0000176 return sprintf(page, "%llu\n",
177 (unsigned long long)q->limits.max_hw_discard_sectors << 9);
Jens Axboe0034af02015-07-16 09:14:26 -0600178}
179
Martin K. Petersen86b37282009-11-10 11:50:21 +0100180static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
181{
Martin K. Petersena934a002011-05-18 10:37:35 +0200182 return sprintf(page, "%llu\n",
183 (unsigned long long)q->limits.max_discard_sectors << 9);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100184}
185
Jens Axboe0034af02015-07-16 09:14:26 -0600186static ssize_t queue_discard_max_store(struct request_queue *q,
187 const char *page, size_t count)
188{
189 unsigned long max_discard;
190 ssize_t ret = queue_var_store(&max_discard, page, count);
191
192 if (ret < 0)
193 return ret;
194
195 if (max_discard & (q->limits.discard_granularity - 1))
196 return -EINVAL;
197
198 max_discard >>= 9;
199 if (max_discard > UINT_MAX)
200 return -EINVAL;
201
202 if (max_discard > q->limits.max_hw_discard_sectors)
203 max_discard = q->limits.max_hw_discard_sectors;
204
205 q->limits.max_discard_sectors = max_discard;
206 return ret;
207}
208
Martin K. Petersen98262f22009-12-03 09:24:48 +0100209static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
210{
Christoph Hellwig48920ff2017-04-05 19:21:23 +0200211 return queue_var_show(0, page);
Martin K. Petersen98262f22009-12-03 09:24:48 +0100212}
213
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400214static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
215{
216 return sprintf(page, "%llu\n",
217 (unsigned long long)q->limits.max_write_same_sectors << 9);
218}
219
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800220static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
221{
222 return sprintf(page, "%llu\n",
223 (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
224}
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400225
Damien Le Moala805a4f2021-01-28 13:47:30 +0900226static ssize_t queue_zone_write_granularity_show(struct request_queue *q,
227 char *page)
228{
229 return queue_var_show(queue_zone_write_granularity(q), page);
230}
231
Keith Busch0512a752020-05-12 17:55:47 +0900232static ssize_t queue_zone_append_max_show(struct request_queue *q, char *page)
233{
234 unsigned long long max_sectors = q->limits.max_zone_append_sectors;
235
236 return sprintf(page, "%llu\n", max_sectors << SECTOR_SHIFT);
237}
238
Jens Axboe8324aa92008-01-29 14:51:59 +0100239static ssize_t
240queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
241{
242 unsigned long max_sectors_kb,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400243 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 page_kb = 1 << (PAGE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +0100245 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
246
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400247 if (ret < 0)
248 return ret;
249
Martin K. Petersenca369d52015-11-13 16:46:48 -0500250 max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
251 q->limits.max_dev_sectors >> 1);
252
Jens Axboe8324aa92008-01-29 14:51:59 +0100253 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
254 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100255
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700256 spin_lock_irq(&q->queue_lock);
Nikanth Karthikesanc295fc02009-09-01 22:40:15 +0200257 q->limits.max_sectors = max_sectors_kb << 1;
Christoph Hellwigd152c682021-08-16 15:46:24 +0200258 if (q->disk)
259 q->disk->bdi->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700260 spin_unlock_irq(&q->queue_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100261
262 return ret;
263}
264
265static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
266{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400267 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100268
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000269 return queue_var_show(max_hw_sectors_kb, page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100270}
271
Max Gurtovoy28af7422021-04-05 13:20:12 +0000272static ssize_t queue_virt_boundary_mask_show(struct request_queue *q, char *page)
273{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000274 return queue_var_show(q->limits.virt_boundary_mask, page);
Max Gurtovoy28af7422021-04-05 13:20:12 +0000275}
276
Jens Axboe956bcb72010-08-07 18:13:50 +0200277#define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
278static ssize_t \
Christoph Hellwigfc93fe12020-09-03 08:07:01 +0200279queue_##name##_show(struct request_queue *q, char *page) \
Jens Axboe956bcb72010-08-07 18:13:50 +0200280{ \
281 int bit; \
282 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
283 return queue_var_show(neg ? !bit : bit, page); \
284} \
285static ssize_t \
Christoph Hellwigfc93fe12020-09-03 08:07:01 +0200286queue_##name##_store(struct request_queue *q, const char *page, size_t count) \
Jens Axboe956bcb72010-08-07 18:13:50 +0200287{ \
288 unsigned long val; \
289 ssize_t ret; \
290 ret = queue_var_store(&val, page, count); \
Arnd Bergmannc678ef52013-04-03 21:53:57 +0200291 if (ret < 0) \
292 return ret; \
Jens Axboe956bcb72010-08-07 18:13:50 +0200293 if (neg) \
294 val = !val; \
295 \
Jens Axboe956bcb72010-08-07 18:13:50 +0200296 if (val) \
Bart Van Assche8814ce82018-03-07 17:10:04 -0800297 blk_queue_flag_set(QUEUE_FLAG_##flag, q); \
Jens Axboe956bcb72010-08-07 18:13:50 +0200298 else \
Bart Van Assche8814ce82018-03-07 17:10:04 -0800299 blk_queue_flag_clear(QUEUE_FLAG_##flag, q); \
Jens Axboe956bcb72010-08-07 18:13:50 +0200300 return ret; \
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100301}
302
Jens Axboe956bcb72010-08-07 18:13:50 +0200303QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
304QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
305QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200306QUEUE_SYSFS_BIT_FNS(stable_writes, STABLE_WRITES, 0);
Jens Axboe956bcb72010-08-07 18:13:50 +0200307#undef QUEUE_SYSFS_BIT_FNS
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100308
Damien Le Moal797476b2016-10-18 15:40:29 +0900309static ssize_t queue_zoned_show(struct request_queue *q, char *page)
310{
311 switch (blk_queue_zoned_model(q)) {
312 case BLK_ZONED_HA:
313 return sprintf(page, "host-aware\n");
314 case BLK_ZONED_HM:
315 return sprintf(page, "host-managed\n");
316 default:
317 return sprintf(page, "none\n");
318 }
319}
320
Damien Le Moal965b6522018-10-12 19:08:48 +0900321static ssize_t queue_nr_zones_show(struct request_queue *q, char *page)
322{
323 return queue_var_show(blk_queue_nr_zones(q), page);
324}
325
Niklas Cassele15864f2020-07-14 23:18:23 +0200326static ssize_t queue_max_open_zones_show(struct request_queue *q, char *page)
327{
328 return queue_var_show(queue_max_open_zones(q), page);
329}
330
Niklas Cassel659bf822020-07-14 23:18:24 +0200331static ssize_t queue_max_active_zones_show(struct request_queue *q, char *page)
332{
333 return queue_var_show(queue_max_active_zones(q), page);
334}
335
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200336static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
337{
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100338 return queue_var_show((blk_queue_nomerges(q) << 1) |
339 blk_queue_noxmerges(q), page);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200340}
341
342static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
343 size_t count)
344{
345 unsigned long nm;
346 ssize_t ret = queue_var_store(&nm, page, count);
347
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400348 if (ret < 0)
349 return ret;
350
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100351 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
352 blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100353 if (nm == 2)
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100354 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100355 else if (nm)
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100356 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100357
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200358 return ret;
359}
360
Jens Axboec7c22e42008-09-13 20:26:01 +0200361static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
362{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +0800363 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
Dan Williams5757a6d2011-07-23 20:44:25 +0200364 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
Jens Axboec7c22e42008-09-13 20:26:01 +0200365
Dan Williams5757a6d2011-07-23 20:44:25 +0200366 return queue_var_show(set << force, page);
Jens Axboec7c22e42008-09-13 20:26:01 +0200367}
368
369static ssize_t
370queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
371{
372 ssize_t ret = -EINVAL;
Christoph Hellwig0a06ff02013-11-14 14:32:07 -0800373#ifdef CONFIG_SMP
Jens Axboec7c22e42008-09-13 20:26:01 +0200374 unsigned long val;
375
376 ret = queue_var_store(&val, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400377 if (ret < 0)
378 return ret;
379
Eric Seppanene8037d42011-08-23 21:25:12 +0200380 if (val == 2) {
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100381 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
382 blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
Eric Seppanene8037d42011-08-23 21:25:12 +0200383 } else if (val == 1) {
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100384 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
385 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
Eric Seppanene8037d42011-08-23 21:25:12 +0200386 } else if (val == 0) {
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100387 blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
388 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
Dan Williams5757a6d2011-07-23 20:44:25 +0200389 }
Jens Axboec7c22e42008-09-13 20:26:01 +0200390#endif
391 return ret;
392}
Jens Axboe8324aa92008-01-29 14:51:59 +0100393
Jens Axboe06426ad2016-11-14 13:01:59 -0700394static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
395{
Jens Axboe64f1c212016-11-14 13:03:03 -0700396 int val;
397
Yufen Yu29ece8b2019-03-18 22:44:41 +0800398 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
399 val = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -0700400 else
401 val = q->poll_nsec / 1000;
402
403 return sprintf(page, "%d\n", val);
Jens Axboe06426ad2016-11-14 13:01:59 -0700404}
405
406static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
407 size_t count)
408{
Jens Axboe64f1c212016-11-14 13:03:03 -0700409 int err, val;
Jens Axboe06426ad2016-11-14 13:01:59 -0700410
411 if (!q->mq_ops || !q->mq_ops->poll)
412 return -EINVAL;
413
Jens Axboe64f1c212016-11-14 13:03:03 -0700414 err = kstrtoint(page, 10, &val);
415 if (err < 0)
416 return err;
Jens Axboe06426ad2016-11-14 13:01:59 -0700417
Yufen Yu29ece8b2019-03-18 22:44:41 +0800418 if (val == BLK_MQ_POLL_CLASSIC)
419 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
420 else if (val >= 0)
Jens Axboe64f1c212016-11-14 13:03:03 -0700421 q->poll_nsec = val * 1000;
Yufen Yu29ece8b2019-03-18 22:44:41 +0800422 else
423 return -EINVAL;
Jens Axboe64f1c212016-11-14 13:03:03 -0700424
425 return count;
Jens Axboe06426ad2016-11-14 13:01:59 -0700426}
427
Jens Axboe05229beed2015-11-05 10:44:55 -0700428static ssize_t queue_poll_show(struct request_queue *q, char *page)
429{
430 return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
431}
432
433static ssize_t queue_poll_store(struct request_queue *q, const char *page,
434 size_t count)
435{
Christoph Hellwiga614dd22021-10-12 13:12:25 +0200436 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe05229beed2015-11-05 10:44:55 -0700437 return -EINVAL;
Christoph Hellwiga614dd22021-10-12 13:12:25 +0200438 pr_info_ratelimited("writes to the poll attribute are ignored.\n");
439 pr_info_ratelimited("please use driver specific parameters instead.\n");
440 return count;
Jens Axboe05229beed2015-11-05 10:44:55 -0700441}
442
Weiping Zhang65cd1d12018-11-29 00:04:39 +0800443static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
444{
445 return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
446}
447
448static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
449 size_t count)
450{
451 unsigned int val;
452 int err;
453
454 err = kstrtou32(page, 10, &val);
455 if (err || val == 0)
456 return -EINVAL;
457
458 blk_queue_rq_timeout(q, msecs_to_jiffies(val));
459
460 return count;
461}
462
Jens Axboe87760e52016-11-09 12:38:14 -0700463static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
464{
Josef Bacika7905042018-07-03 09:32:35 -0600465 if (!wbt_rq_qos(q))
Jens Axboe87760e52016-11-09 12:38:14 -0700466 return -EINVAL;
467
Josef Bacika7905042018-07-03 09:32:35 -0600468 return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
Jens Axboe87760e52016-11-09 12:38:14 -0700469}
470
471static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
472 size_t count)
473{
Josef Bacika7905042018-07-03 09:32:35 -0600474 struct rq_qos *rqos;
Jens Axboe87760e52016-11-09 12:38:14 -0700475 ssize_t ret;
Jens Axboe80e091d2016-11-28 09:22:47 -0700476 s64 val;
Jens Axboe87760e52016-11-09 12:38:14 -0700477
Jens Axboe87760e52016-11-09 12:38:14 -0700478 ret = queue_var_store64(&val, page);
479 if (ret < 0)
480 return ret;
Jens Axboed62118b2016-11-28 09:40:34 -0700481 if (val < -1)
482 return -EINVAL;
483
Josef Bacika7905042018-07-03 09:32:35 -0600484 rqos = wbt_rq_qos(q);
485 if (!rqos) {
Jens Axboed62118b2016-11-28 09:40:34 -0700486 ret = wbt_init(q);
487 if (ret)
488 return ret;
Jens Axboed62118b2016-11-28 09:40:34 -0700489 }
Jens Axboe87760e52016-11-09 12:38:14 -0700490
Jens Axboe80e091d2016-11-28 09:22:47 -0700491 if (val == -1)
Josef Bacika7905042018-07-03 09:32:35 -0600492 val = wbt_default_latency_nsec(q);
Jens Axboe80e091d2016-11-28 09:22:47 -0700493 else if (val >= 0)
Josef Bacika7905042018-07-03 09:32:35 -0600494 val *= 1000ULL;
Jens Axboed62118b2016-11-28 09:40:34 -0700495
Aleksei Zakharovb7143fe2019-02-11 13:10:34 +0300496 if (wbt_get_min_lat(q) == val)
497 return count;
498
Jens Axboec1253112018-08-23 09:34:46 -0600499 /*
500 * Ensure that the queue is idled, in case the latency update
501 * ends up either enabling or disabling wbt completely. We can't
502 * have IO inflight if that happens.
503 */
Jens Axboea1ce35f2018-10-29 10:23:51 -0600504 blk_mq_freeze_queue(q);
505 blk_mq_quiesce_queue(q);
Jens Axboe80e091d2016-11-28 09:22:47 -0700506
Jens Axboec1253112018-08-23 09:34:46 -0600507 wbt_set_min_lat(q, val);
Jens Axboec1253112018-08-23 09:34:46 -0600508
Jens Axboea1ce35f2018-10-29 10:23:51 -0600509 blk_mq_unquiesce_queue(q);
510 blk_mq_unfreeze_queue(q);
Jens Axboec1253112018-08-23 09:34:46 -0600511
Jens Axboe87760e52016-11-09 12:38:14 -0700512 return count;
513}
514
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600515static ssize_t queue_wc_show(struct request_queue *q, char *page)
516{
517 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
518 return sprintf(page, "write back\n");
519
520 return sprintf(page, "write through\n");
521}
522
523static ssize_t queue_wc_store(struct request_queue *q, const char *page,
524 size_t count)
525{
526 int set = -1;
527
528 if (!strncmp(page, "write back", 10))
529 set = 1;
530 else if (!strncmp(page, "write through", 13) ||
531 !strncmp(page, "none", 4))
532 set = 0;
533
534 if (set == -1)
535 return -EINVAL;
536
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600537 if (set)
Bart Van Assche8814ce82018-03-07 17:10:04 -0800538 blk_queue_flag_set(QUEUE_FLAG_WC, q);
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600539 else
Bart Van Assche8814ce82018-03-07 17:10:04 -0800540 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600541
542 return count;
543}
544
Kent Overstreet6fcefbe2018-05-08 21:33:58 -0400545static ssize_t queue_fua_show(struct request_queue *q, char *page)
546{
547 return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
548}
549
Yigal Kormanea6ca602016-06-23 17:05:51 -0400550static ssize_t queue_dax_show(struct request_queue *q, char *page)
551{
552 return queue_var_show(blk_queue_dax(q), page);
553}
554
Christoph Hellwig35626142020-09-03 08:07:00 +0200555#define QUEUE_RO_ENTRY(_prefix, _name) \
556static struct queue_sysfs_entry _prefix##_entry = { \
557 .attr = { .name = _name, .mode = 0444 }, \
558 .show = _prefix##_show, \
Jens Axboe8324aa92008-01-29 14:51:59 +0100559};
560
Christoph Hellwig35626142020-09-03 08:07:00 +0200561#define QUEUE_RW_ENTRY(_prefix, _name) \
562static struct queue_sysfs_entry _prefix##_entry = { \
563 .attr = { .name = _name, .mode = 0644 }, \
564 .show = _prefix##_show, \
565 .store = _prefix##_store, \
Jens Axboe8324aa92008-01-29 14:51:59 +0100566};
567
Christoph Hellwig35626142020-09-03 08:07:00 +0200568QUEUE_RW_ENTRY(queue_requests, "nr_requests");
569QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
570QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
571QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
572QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
573QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
574QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
575QUEUE_RW_ENTRY(elv_iosched, "scheduler");
Jens Axboe8324aa92008-01-29 14:51:59 +0100576
Christoph Hellwig35626142020-09-03 08:07:00 +0200577QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
578QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
579QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
580QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size");
581QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size");
Jens Axboe8324aa92008-01-29 14:51:59 +0100582
Christoph Hellwig35626142020-09-03 08:07:00 +0200583QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
584QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity");
585QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes");
586QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes");
587QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500588
Christoph Hellwig35626142020-09-03 08:07:00 +0200589QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
590QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes");
591QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes");
Damien Le Moala805a4f2021-01-28 13:47:30 +0900592QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
Christoph Hellwig1e739732017-02-08 14:46:49 +0100593
Christoph Hellwig35626142020-09-03 08:07:00 +0200594QUEUE_RO_ENTRY(queue_zoned, "zoned");
595QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
596QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones");
597QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones");
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200598
Christoph Hellwig35626142020-09-03 08:07:00 +0200599QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
600QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
601QUEUE_RW_ENTRY(queue_poll, "io_poll");
602QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
603QUEUE_RW_ENTRY(queue_wc, "write_cache");
604QUEUE_RO_ENTRY(queue_fua, "fua");
605QUEUE_RO_ENTRY(queue_dax, "dax");
606QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
607QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
Max Gurtovoy28af7422021-04-05 13:20:12 +0000608QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500609
Christoph Hellwig35626142020-09-03 08:07:00 +0200610#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
611QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time");
612#endif
Jens Axboe8324aa92008-01-29 14:51:59 +0100613
Christoph Hellwig35626142020-09-03 08:07:00 +0200614/* legacy alias for logical_block_size: */
Martin K. Petersene68b9032008-01-29 19:14:08 +0100615static struct queue_sysfs_entry queue_hw_sector_size_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600616 .attr = {.name = "hw_sector_size", .mode = 0444 },
Martin K. Petersene1defc42009-05-22 17:17:49 -0400617 .show = queue_logical_block_size_show,
618};
619
Christoph Hellwigfc93fe12020-09-03 08:07:01 +0200620QUEUE_RW_ENTRY(queue_nonrot, "rotational");
621QUEUE_RW_ENTRY(queue_iostats, "iostats");
622QUEUE_RW_ENTRY(queue_random, "add_random");
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200623QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes");
Jens Axboee2e1a142010-06-09 10:42:09 +0200624
Weiping Zhang4d253392019-04-02 21:14:30 +0800625static struct attribute *queue_attrs[] = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100626 &queue_requests_entry.attr,
627 &queue_ra_entry.attr,
628 &queue_max_hw_sectors_entry.attr,
629 &queue_max_sectors_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500630 &queue_max_segments_entry.attr,
Christoph Hellwig1e739732017-02-08 14:46:49 +0100631 &queue_max_discard_segments_entry.attr,
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200632 &queue_max_integrity_segments_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500633 &queue_max_segment_size_entry.attr,
Christoph Hellwig35626142020-09-03 08:07:00 +0200634 &elv_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100635 &queue_hw_sector_size_entry.attr,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400636 &queue_logical_block_size_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400637 &queue_physical_block_size_entry.attr,
Hannes Reinecke87caf972016-10-18 15:40:30 +0900638 &queue_chunk_sectors_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400639 &queue_io_min_entry.attr,
640 &queue_io_opt_entry.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +0100641 &queue_discard_granularity_entry.attr,
642 &queue_discard_max_entry.attr,
Jens Axboe0034af02015-07-16 09:14:26 -0600643 &queue_discard_max_hw_entry.attr,
Martin K. Petersen98262f22009-12-03 09:24:48 +0100644 &queue_discard_zeroes_data_entry.attr,
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400645 &queue_write_same_max_entry.attr,
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800646 &queue_write_zeroes_max_entry.attr,
Keith Busch0512a752020-05-12 17:55:47 +0900647 &queue_zone_append_max_entry.attr,
Damien Le Moala805a4f2021-01-28 13:47:30 +0900648 &queue_zone_write_granularity_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100649 &queue_nonrot_entry.attr,
Damien Le Moal797476b2016-10-18 15:40:29 +0900650 &queue_zoned_entry.attr,
Damien Le Moal965b6522018-10-12 19:08:48 +0900651 &queue_nr_zones_entry.attr,
Niklas Cassele15864f2020-07-14 23:18:23 +0200652 &queue_max_open_zones_entry.attr,
Niklas Cassel659bf822020-07-14 23:18:24 +0200653 &queue_max_active_zones_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200654 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200655 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100656 &queue_iostats_entry.attr,
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200657 &queue_stable_writes_entry.attr,
Jens Axboee2e1a142010-06-09 10:42:09 +0200658 &queue_random_entry.attr,
Jens Axboe05229beed2015-11-05 10:44:55 -0700659 &queue_poll_entry.attr,
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600660 &queue_wc_entry.attr,
Kent Overstreet6fcefbe2018-05-08 21:33:58 -0400661 &queue_fua_entry.attr,
Yigal Kormanea6ca602016-06-23 17:05:51 -0400662 &queue_dax_entry.attr,
Jens Axboe87760e52016-11-09 12:38:14 -0700663 &queue_wb_lat_entry.attr,
Jens Axboe06426ad2016-11-14 13:01:59 -0700664 &queue_poll_delay_entry.attr,
Weiping Zhang65cd1d12018-11-29 00:04:39 +0800665 &queue_io_timeout_entry.attr,
Shaohua Li297e3d82017-03-27 10:51:37 -0700666#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
Christoph Hellwig35626142020-09-03 08:07:00 +0200667 &blk_throtl_sample_time_entry.attr,
Shaohua Li297e3d82017-03-27 10:51:37 -0700668#endif
Max Gurtovoy28af7422021-04-05 13:20:12 +0000669 &queue_virt_boundary_mask_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100670 NULL,
671};
672
Weiping Zhang4d253392019-04-02 21:14:30 +0800673static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
674 int n)
675{
676 struct request_queue *q =
677 container_of(kobj, struct request_queue, kobj);
678
679 if (attr == &queue_io_timeout_entry.attr &&
680 (!q->mq_ops || !q->mq_ops->timeout))
681 return 0;
682
Niklas Cassel659bf822020-07-14 23:18:24 +0200683 if ((attr == &queue_max_open_zones_entry.attr ||
684 attr == &queue_max_active_zones_entry.attr) &&
Niklas Cassele15864f2020-07-14 23:18:23 +0200685 !blk_queue_is_zoned(q))
686 return 0;
687
Weiping Zhang4d253392019-04-02 21:14:30 +0800688 return attr->mode;
689}
690
691static struct attribute_group queue_attr_group = {
692 .attrs = queue_attrs,
693 .is_visible = queue_attr_visible,
694};
695
696
Jens Axboe8324aa92008-01-29 14:51:59 +0100697#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
698
699static ssize_t
700queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
701{
702 struct queue_sysfs_entry *entry = to_queue(attr);
703 struct request_queue *q =
704 container_of(kobj, struct request_queue, kobj);
705 ssize_t res;
706
707 if (!entry->show)
708 return -EIO;
709 mutex_lock(&q->sysfs_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100710 res = entry->show(q, page);
711 mutex_unlock(&q->sysfs_lock);
712 return res;
713}
714
715static ssize_t
716queue_attr_store(struct kobject *kobj, struct attribute *attr,
717 const char *page, size_t length)
718{
719 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100720 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100721 ssize_t res;
722
723 if (!entry->store)
724 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100725
726 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100727 mutex_lock(&q->sysfs_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100728 res = entry->store(q, page, length);
729 mutex_unlock(&q->sysfs_lock);
730 return res;
731}
732
Tejun Heo548bc8e2013-01-09 08:05:13 -0800733static void blk_free_queue_rcu(struct rcu_head *rcu_head)
734{
735 struct request_queue *q = container_of(rcu_head, struct request_queue,
736 rcu_head);
737 kmem_cache_free(blk_requestq_cachep, q);
738}
739
Ming Lei47cdee22019-05-15 11:03:08 +0800740/* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
741static void blk_exit_queue(struct request_queue *q)
742{
743 /*
744 * Since the I/O scheduler exit code may access cgroup information,
745 * perform I/O scheduler exit before disassociating from the block
746 * cgroup controller.
747 */
748 if (q->elevator) {
749 ioc_clear_queue(q);
Christoph Hellwig0c6cb3a2021-11-23 19:53:07 +0100750 elevator_exit(q);
Ming Lei47cdee22019-05-15 11:03:08 +0800751 }
752
753 /*
754 * Remove all references to @q from the block cgroup controller before
755 * restoring @q->queue_lock to avoid that restoring this pointer causes
756 * e.g. blkcg_print_blkgs() to crash.
757 */
758 blkcg_exit_queue(q);
Ming Lei47cdee22019-05-15 11:03:08 +0800759}
760
Jens Axboe8324aa92008-01-29 14:51:59 +0100761/**
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000762 * blk_release_queue - releases all allocated resources of the request_queue
763 * @kobj: pointer to a kobject, whose container is a request_queue
Jens Axboe8324aa92008-01-29 14:51:59 +0100764 *
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000765 * This function releases all allocated resources of the request queue.
766 *
767 * The struct request_queue refcount is incremented with blk_get_queue() and
768 * decremented with blk_put_queue(). Once the refcount reaches 0 this function
769 * is called.
770 *
771 * For drivers that have a request_queue on a gendisk and added with
772 * __device_add_disk() the refcount to request_queue will reach 0 with
773 * the last put_disk() called by the driver. For drivers which don't use
774 * __device_add_disk() this happens with blk_cleanup_queue().
775 *
776 * Drivers exist which depend on the release of the request_queue to be
777 * synchronous, it should not be deferred.
778 *
779 * Context: can sleep
Bart Van Asschedc9edc42017-06-14 13:27:50 -0600780 */
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000781static void blk_release_queue(struct kobject *kobj)
Jens Axboe8324aa92008-01-29 14:51:59 +0100782{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000783 struct request_queue *q =
784 container_of(kobj, struct request_queue, kobj);
785
786 might_sleep();
Jens Axboe8324aa92008-01-29 14:51:59 +0100787
Jens Axboe48b5c1f2021-11-13 14:03:26 -0700788 if (q->poll_stat)
Omar Sandoval34dbad52017-03-21 08:56:08 -0700789 blk_stat_remove_callback(q, q->poll_cb);
790 blk_stat_free_callback(q->poll_cb);
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600791
Omar Sandoval34dbad52017-03-21 08:56:08 -0700792 blk_free_queue_stats(q->stats);
Jens Axboe48b5c1f2021-11-13 14:03:26 -0700793 kfree(q->poll_stat);
Omar Sandoval34dbad52017-03-21 08:56:08 -0700794
Ming Lei47cdee22019-05-15 11:03:08 +0800795 blk_exit_queue(q);
796
Damien Le Moalbf505452018-10-12 19:08:50 +0900797 blk_queue_free_zone_bitmaps(q);
798
Jens Axboe344e9ff2018-11-15 12:22:51 -0700799 if (queue_is_mq(q))
Ming Leie09aae72015-01-29 20:17:27 +0800800 blk_mq_release(q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700801
Jens Axboe8324aa92008-01-29 14:51:59 +0100802 blk_trace_shutdown(q);
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000803 mutex_lock(&q->debugfs_mutex);
804 debugfs_remove_recursive(q->debugfs_dir);
805 mutex_unlock(&q->debugfs_mutex);
Jens Axboe8324aa92008-01-29 14:51:59 +0100806
Jens Axboe344e9ff2018-11-15 12:22:51 -0700807 if (queue_is_mq(q))
Omar Sandoval62ebce162017-01-31 14:53:21 -0800808 blk_mq_debugfs_unregister(q);
809
Kent Overstreet338aa962018-05-20 18:25:47 -0400810 bioset_exit(&q->bio_split);
Kent Overstreet54efd502015-04-23 22:37:18 -0700811
Tejun Heoa73f7302011-12-14 00:33:37 +0100812 ida_simple_remove(&blk_queue_ida, q->id);
Tejun Heo548bc8e2013-01-09 08:05:13 -0800813 call_rcu(&q->rcu_head, blk_free_queue_rcu);
Jens Axboe8324aa92008-01-29 14:51:59 +0100814}
815
Emese Revfy52cf25d2010-01-19 02:58:23 +0100816static const struct sysfs_ops queue_sysfs_ops = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100817 .show = queue_attr_show,
818 .store = queue_attr_store,
819};
820
821struct kobj_type blk_queue_ktype = {
822 .sysfs_ops = &queue_sysfs_ops,
Jens Axboe8324aa92008-01-29 14:51:59 +0100823 .release = blk_release_queue,
824};
825
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800826/**
827 * blk_register_queue - register a block layer queue with sysfs
828 * @disk: Disk of which the request queue should be registered with sysfs.
829 */
Jens Axboe8324aa92008-01-29 14:51:59 +0100830int blk_register_queue(struct gendisk *disk)
831{
832 int ret;
Li Zefan1d54ad62009-04-14 14:00:05 +0800833 struct device *dev = disk_to_dev(disk);
Jens Axboe8324aa92008-01-29 14:51:59 +0100834 struct request_queue *q = disk->queue;
835
Li Zefan1d54ad62009-04-14 14:00:05 +0800836 ret = blk_trace_init_sysfs(dev);
837 if (ret)
838 return ret;
839
Ming Leicecf5d82019-08-27 19:01:48 +0800840 mutex_lock(&q->sysfs_dir_lock);
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800841
Linus Torvaldsc9059592009-06-11 10:52:27 -0700842 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
Liu Yuaned5302d2011-04-19 13:47:58 +0200843 if (ret < 0) {
844 blk_trace_remove_sysfs(dev);
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800845 goto unlock;
Liu Yuaned5302d2011-04-19 13:47:58 +0200846 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100847
Weiping Zhang4d253392019-04-02 21:14:30 +0800848 ret = sysfs_create_group(&q->kobj, &queue_attr_group);
849 if (ret) {
850 blk_trace_remove_sysfs(dev);
851 kobject_del(&q->kobj);
852 kobject_put(&dev->kobj);
853 goto unlock;
854 }
855
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000856 mutex_lock(&q->debugfs_mutex);
857 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
858 blk_debugfs_root);
859 mutex_unlock(&q->debugfs_mutex);
860
Jens Axboe344e9ff2018-11-15 12:22:51 -0700861 if (queue_is_mq(q)) {
Bart Van Assche2d0364c2017-04-26 13:47:48 -0700862 __blk_mq_register_dev(dev, q);
Bart Van Asschea8ecdd72017-05-25 16:38:06 -0700863 blk_mq_debugfs_register(q);
864 }
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600865
Ming Leib89f6252019-09-23 23:12:09 +0800866 mutex_lock(&q->sysfs_lock);
Damien Le Moala2247f12021-10-27 11:22:19 +0900867
868 ret = disk_register_independent_access_ranges(disk, NULL);
869 if (ret)
870 goto put_dev;
871
Jens Axboe344e9ff2018-11-15 12:22:51 -0700872 if (q->elevator) {
Ming Leicecf5d82019-08-27 19:01:48 +0800873 ret = elv_register_queue(q, false);
Damien Le Moala2247f12021-10-27 11:22:19 +0900874 if (ret)
875 goto put_dev;
Jens Axboe8324aa92008-01-29 14:51:59 +0100876 }
Ming Leicecf5d82019-08-27 19:01:48 +0800877
Ming Leicecf5d82019-08-27 19:01:48 +0800878 blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
879 wbt_enable_default(q);
880 blk_throtl_register_queue(q);
881
882 /* Now everything is ready and send out KOBJ_ADD uevent */
883 kobject_uevent(&q->kobj, KOBJ_ADD);
Yufen Yu05468582020-10-08 23:26:32 -0400884 if (q->elevator)
Ming Leicecf5d82019-08-27 19:01:48 +0800885 kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
886 mutex_unlock(&q->sysfs_lock);
887
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800888 ret = 0;
889unlock:
Ming Leicecf5d82019-08-27 19:01:48 +0800890 mutex_unlock(&q->sysfs_dir_lock);
Ming Leia72c3742021-06-09 09:58:22 +0800891
892 /*
893 * SCSI probing may synchronously create and destroy a lot of
894 * request_queues for non-existent devices. Shutting down a fully
895 * functional queue takes measureable wallclock time as RCU grace
896 * periods are involved. To avoid excessive latency in these
897 * cases, a request_queue starts out in a degraded mode which is
898 * faster to shut down and is made fully functional here as
899 * request_queues for non-existent devices never get registered.
900 */
901 if (!blk_queue_init_done(q)) {
902 blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
903 percpu_ref_switch_to_percpu(&q->q_usage_counter);
904 }
905
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800906 return ret;
Damien Le Moala2247f12021-10-27 11:22:19 +0900907
908put_dev:
909 disk_unregister_independent_access_ranges(disk);
910 mutex_unlock(&q->sysfs_lock);
911 mutex_unlock(&q->sysfs_dir_lock);
912 kobject_del(&q->kobj);
913 blk_trace_remove_sysfs(dev);
914 kobject_put(&dev->kobj);
915
916 return ret;
Jens Axboe8324aa92008-01-29 14:51:59 +0100917}
918
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800919/**
920 * blk_unregister_queue - counterpart of blk_register_queue()
921 * @disk: Disk of which the request queue should be unregistered from sysfs.
922 *
923 * Note: the caller is responsible for guaranteeing that this function is called
924 * after blk_register_queue() has finished.
925 */
Jens Axboe8324aa92008-01-29 14:51:59 +0100926void blk_unregister_queue(struct gendisk *disk)
927{
928 struct request_queue *q = disk->queue;
929
Akinobu Mitafb199742008-04-21 09:51:06 +0200930 if (WARN_ON(!q))
931 return;
932
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500933 /* Return early if disk->queue was never registered. */
Ming Lei58c898b2019-08-27 19:01:47 +0800934 if (!blk_queue_registered(q))
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500935 return;
936
Mike Snitzer667257e2018-01-11 14:11:01 -0500937 /*
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800938 * Since sysfs_remove_dir() prevents adding new directory entries
939 * before removal of existing entries starts, protect against
940 * concurrent elv_iosched_store() calls.
Mike Snitzer667257e2018-01-11 14:11:01 -0500941 */
David Jefferye9a823f2017-08-28 10:52:44 -0600942 mutex_lock(&q->sysfs_lock);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800943 blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
Ming Leicecf5d82019-08-27 19:01:48 +0800944 mutex_unlock(&q->sysfs_lock);
Omar Sandoval334335d2017-03-28 16:12:15 -0700945
Ming Leicecf5d82019-08-27 19:01:48 +0800946 mutex_lock(&q->sysfs_dir_lock);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800947 /*
948 * Remove the sysfs attributes before unregistering the queue data
949 * structures that can be modified through sysfs.
950 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700951 if (queue_is_mq(q))
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200952 blk_mq_unregister_dev(disk_to_dev(disk), q);
Jens Axboe8324aa92008-01-29 14:51:59 +0100953
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +0200954 kobject_uevent(&q->kobj, KOBJ_REMOVE);
955 kobject_del(&q->kobj);
956 blk_trace_remove_sysfs(disk_to_dev(disk));
Mike Snitzer667257e2018-01-11 14:11:01 -0500957
Ming Leib89f6252019-09-23 23:12:09 +0800958 mutex_lock(&q->sysfs_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700959 if (q->elevator)
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800960 elv_unregister_queue(q);
Damien Le Moala2247f12021-10-27 11:22:19 +0900961 disk_unregister_independent_access_ranges(disk);
Ming Leib89f6252019-09-23 23:12:09 +0800962 mutex_unlock(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800963 mutex_unlock(&q->sysfs_dir_lock);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800964
965 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100966}