blob: 1832587dce3aaafda65e24046946e4cc9bdb29cc [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 Axboe8324aa92008-01-29 14:51:59 +010020
21struct queue_sysfs_entry {
22 struct attribute attr;
23 ssize_t (*show)(struct request_queue *, char *);
24 ssize_t (*store)(struct request_queue *, const char *, size_t);
25};
26
27static ssize_t
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080028queue_var_show(unsigned long var, char *page)
Jens Axboe8324aa92008-01-29 14:51:59 +010029{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +080030 return sprintf(page, "%lu\n", var);
Jens Axboe8324aa92008-01-29 14:51:59 +010031}
32
33static ssize_t
34queue_var_store(unsigned long *var, const char *page, size_t count)
35{
Dave Reisnerb1f3b642012-09-08 11:55:45 -040036 int err;
37 unsigned long v;
Jens Axboe8324aa92008-01-29 14:51:59 +010038
Jingoo Haned751e62013-09-11 14:20:08 -070039 err = kstrtoul(page, 10, &v);
Dave Reisnerb1f3b642012-09-08 11:55:45 -040040 if (err || v > UINT_MAX)
41 return -EINVAL;
42
43 *var = v;
44
Jens Axboe8324aa92008-01-29 14:51:59 +010045 return count;
46}
47
Jens Axboe80e091d2016-11-28 09:22:47 -070048static ssize_t queue_var_store64(s64 *var, const char *page)
Jens Axboe87760e52016-11-09 12:38:14 -070049{
50 int err;
Jens Axboe80e091d2016-11-28 09:22:47 -070051 s64 v;
Jens Axboe87760e52016-11-09 12:38:14 -070052
Jens Axboe80e091d2016-11-28 09:22:47 -070053 err = kstrtos64(page, 10, &v);
Jens Axboe87760e52016-11-09 12:38:14 -070054 if (err < 0)
55 return err;
56
57 *var = v;
58 return 0;
59}
60
Jens Axboe8324aa92008-01-29 14:51:59 +010061static ssize_t queue_requests_show(struct request_queue *q, char *page)
62{
Max Gurtovoy28af7422021-04-05 13:20:12 +000063 return queue_var_show(q->nr_requests, page);
Jens Axboe8324aa92008-01-29 14:51:59 +010064}
65
66static ssize_t
67queue_requests_store(struct request_queue *q, const char *page, size_t count)
68{
Jens Axboe8324aa92008-01-29 14:51:59 +010069 unsigned long nr;
Jens Axboee3a2b3f2014-05-20 11:49:02 -060070 int ret, err;
Jens Axboeb8a9ae72009-09-11 22:44:29 +020071
Jens Axboe344e9ff2018-11-15 12:22:51 -070072 if (!queue_is_mq(q))
Jens Axboeb8a9ae72009-09-11 22:44:29 +020073 return -EINVAL;
74
75 ret = queue_var_store(&nr, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -040076 if (ret < 0)
77 return ret;
78
Jens Axboe8324aa92008-01-29 14:51:59 +010079 if (nr < BLKDEV_MIN_RQ)
80 nr = BLKDEV_MIN_RQ;
81
Jens Axboea1ce35f2018-10-29 10:23:51 -060082 err = blk_mq_update_nr_requests(q, nr);
Jens Axboee3a2b3f2014-05-20 11:49:02 -060083 if (err)
84 return err;
Tejun Heoa0516612012-06-26 15:05:44 -070085
Jens Axboe8324aa92008-01-29 14:51:59 +010086 return ret;
87}
88
89static ssize_t queue_ra_show(struct request_queue *q, char *page)
90{
Christoph Hellwigedb08722021-08-09 16:17:43 +020091 unsigned long ra_kb;
Jens Axboe8324aa92008-01-29 14:51:59 +010092
Christoph Hellwigedb08722021-08-09 16:17:43 +020093 if (!queue_has_disk(q))
94 return -EINVAL;
95 ra_kb = queue_to_disk(q)->bdi->ra_pages << (PAGE_SHIFT - 10);
Max Gurtovoy8c390ff2021-05-11 15:53:19 +000096 return queue_var_show(ra_kb, page);
Jens Axboe8324aa92008-01-29 14:51:59 +010097}
98
99static ssize_t
100queue_ra_store(struct request_queue *q, const char *page, size_t count)
101{
102 unsigned long ra_kb;
Christoph Hellwigedb08722021-08-09 16:17:43 +0200103 ssize_t ret;
Jens Axboe8324aa92008-01-29 14:51:59 +0100104
Christoph Hellwigedb08722021-08-09 16:17:43 +0200105 if (!queue_has_disk(q))
106 return -EINVAL;
107 ret = queue_var_store(&ra_kb, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400108 if (ret < 0)
109 return ret;
Christoph Hellwigedb08722021-08-09 16:17:43 +0200110 queue_to_disk(q)->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +0100111 return ret;
112}
113
114static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
115{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400116 int max_sectors_kb = queue_max_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100117
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000118 return queue_var_show(max_sectors_kb, page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100119}
120
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500121static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
122{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000123 return queue_var_show(queue_max_segments(q), page);
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500124}
125
Christoph Hellwig1e739732017-02-08 14:46:49 +0100126static ssize_t queue_max_discard_segments_show(struct request_queue *q,
127 char *page)
128{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000129 return queue_var_show(queue_max_discard_segments(q), page);
Christoph Hellwig1e739732017-02-08 14:46:49 +0100130}
131
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200132static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
133{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000134 return queue_var_show(q->limits.max_integrity_segments, page);
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200135}
136
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500137static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
138{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000139 return queue_var_show(queue_max_segment_size(q), page);
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500140}
141
Martin K. Petersene1defc42009-05-22 17:17:49 -0400142static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
Martin K. Petersene68b9032008-01-29 19:14:08 +0100143{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400144 return queue_var_show(queue_logical_block_size(q), page);
Martin K. Petersene68b9032008-01-29 19:14:08 +0100145}
146
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400147static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
148{
149 return queue_var_show(queue_physical_block_size(q), page);
150}
151
Hannes Reinecke87caf972016-10-18 15:40:30 +0900152static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
153{
154 return queue_var_show(q->limits.chunk_sectors, page);
155}
156
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400157static ssize_t queue_io_min_show(struct request_queue *q, char *page)
158{
159 return queue_var_show(queue_io_min(q), page);
160}
161
162static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
163{
164 return queue_var_show(queue_io_opt(q), page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100165}
166
Martin K. Petersen86b37282009-11-10 11:50:21 +0100167static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
168{
169 return queue_var_show(q->limits.discard_granularity, page);
170}
171
Jens Axboe0034af02015-07-16 09:14:26 -0600172static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
173{
Jens Axboe0034af02015-07-16 09:14:26 -0600174
Alan18f922d02016-02-17 14:15:30 +0000175 return sprintf(page, "%llu\n",
176 (unsigned long long)q->limits.max_hw_discard_sectors << 9);
Jens Axboe0034af02015-07-16 09:14:26 -0600177}
178
Martin K. Petersen86b37282009-11-10 11:50:21 +0100179static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
180{
Martin K. Petersena934a002011-05-18 10:37:35 +0200181 return sprintf(page, "%llu\n",
182 (unsigned long long)q->limits.max_discard_sectors << 9);
Martin K. Petersen86b37282009-11-10 11:50:21 +0100183}
184
Jens Axboe0034af02015-07-16 09:14:26 -0600185static ssize_t queue_discard_max_store(struct request_queue *q,
186 const char *page, size_t count)
187{
188 unsigned long max_discard;
189 ssize_t ret = queue_var_store(&max_discard, page, count);
190
191 if (ret < 0)
192 return ret;
193
194 if (max_discard & (q->limits.discard_granularity - 1))
195 return -EINVAL;
196
197 max_discard >>= 9;
198 if (max_discard > UINT_MAX)
199 return -EINVAL;
200
201 if (max_discard > q->limits.max_hw_discard_sectors)
202 max_discard = q->limits.max_hw_discard_sectors;
203
204 q->limits.max_discard_sectors = max_discard;
205 return ret;
206}
207
Martin K. Petersen98262f22009-12-03 09:24:48 +0100208static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
209{
Christoph Hellwig48920ff2017-04-05 19:21:23 +0200210 return queue_var_show(0, page);
Martin K. Petersen98262f22009-12-03 09:24:48 +0100211}
212
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400213static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
214{
215 return sprintf(page, "%llu\n",
216 (unsigned long long)q->limits.max_write_same_sectors << 9);
217}
218
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800219static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
220{
221 return sprintf(page, "%llu\n",
222 (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
223}
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400224
Damien Le Moala805a4f2021-01-28 13:47:30 +0900225static ssize_t queue_zone_write_granularity_show(struct request_queue *q,
226 char *page)
227{
228 return queue_var_show(queue_zone_write_granularity(q), page);
229}
230
Keith Busch0512a752020-05-12 17:55:47 +0900231static ssize_t queue_zone_append_max_show(struct request_queue *q, char *page)
232{
233 unsigned long long max_sectors = q->limits.max_zone_append_sectors;
234
235 return sprintf(page, "%llu\n", max_sectors << SECTOR_SHIFT);
236}
237
Jens Axboe8324aa92008-01-29 14:51:59 +0100238static ssize_t
239queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
240{
241 unsigned long max_sectors_kb,
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400242 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300243 page_kb = 1 << (PAGE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +0100244 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
245
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400246 if (ret < 0)
247 return ret;
248
Martin K. Petersenca369d52015-11-13 16:46:48 -0500249 max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
250 q->limits.max_dev_sectors >> 1);
251
Jens Axboe8324aa92008-01-29 14:51:59 +0100252 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
253 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100254
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700255 spin_lock_irq(&q->queue_lock);
Nikanth Karthikesanc295fc02009-09-01 22:40:15 +0200256 q->limits.max_sectors = max_sectors_kb << 1;
Christoph Hellwigedb08722021-08-09 16:17:43 +0200257 if (queue_has_disk(q))
258 queue_to_disk(q)->bdi->io_pages =
259 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{
436 unsigned long poll_on;
437 ssize_t ret;
438
Ming Leicd191812018-12-18 12:15:29 +0800439 if (!q->tag_set || q->tag_set->nr_maps <= HCTX_TYPE_POLL ||
440 !q->tag_set->map[HCTX_TYPE_POLL].nr_queues)
Jens Axboe05229beed2015-11-05 10:44:55 -0700441 return -EINVAL;
442
443 ret = queue_var_store(&poll_on, page, count);
444 if (ret < 0)
445 return ret;
446
Jeffle Xu6b09b4d2021-02-22 14:54:52 +0800447 if (poll_on) {
Bart Van Assche8814ce82018-03-07 17:10:04 -0800448 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jeffle Xu6b09b4d2021-02-22 14:54:52 +0800449 } else {
450 blk_mq_freeze_queue(q);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800451 blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
Jeffle Xu6b09b4d2021-02-22 14:54:52 +0800452 blk_mq_unfreeze_queue(q);
453 }
Jens Axboe05229beed2015-11-05 10:44:55 -0700454
455 return ret;
456}
457
Weiping Zhang65cd1d12018-11-29 00:04:39 +0800458static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
459{
460 return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
461}
462
463static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
464 size_t count)
465{
466 unsigned int val;
467 int err;
468
469 err = kstrtou32(page, 10, &val);
470 if (err || val == 0)
471 return -EINVAL;
472
473 blk_queue_rq_timeout(q, msecs_to_jiffies(val));
474
475 return count;
476}
477
Jens Axboe87760e52016-11-09 12:38:14 -0700478static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
479{
Josef Bacika7905042018-07-03 09:32:35 -0600480 if (!wbt_rq_qos(q))
Jens Axboe87760e52016-11-09 12:38:14 -0700481 return -EINVAL;
482
Josef Bacika7905042018-07-03 09:32:35 -0600483 return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
Jens Axboe87760e52016-11-09 12:38:14 -0700484}
485
486static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
487 size_t count)
488{
Josef Bacika7905042018-07-03 09:32:35 -0600489 struct rq_qos *rqos;
Jens Axboe87760e52016-11-09 12:38:14 -0700490 ssize_t ret;
Jens Axboe80e091d2016-11-28 09:22:47 -0700491 s64 val;
Jens Axboe87760e52016-11-09 12:38:14 -0700492
Jens Axboe87760e52016-11-09 12:38:14 -0700493 ret = queue_var_store64(&val, page);
494 if (ret < 0)
495 return ret;
Jens Axboed62118b2016-11-28 09:40:34 -0700496 if (val < -1)
497 return -EINVAL;
498
Josef Bacika7905042018-07-03 09:32:35 -0600499 rqos = wbt_rq_qos(q);
500 if (!rqos) {
Jens Axboed62118b2016-11-28 09:40:34 -0700501 ret = wbt_init(q);
502 if (ret)
503 return ret;
Jens Axboed62118b2016-11-28 09:40:34 -0700504 }
Jens Axboe87760e52016-11-09 12:38:14 -0700505
Jens Axboe80e091d2016-11-28 09:22:47 -0700506 if (val == -1)
Josef Bacika7905042018-07-03 09:32:35 -0600507 val = wbt_default_latency_nsec(q);
Jens Axboe80e091d2016-11-28 09:22:47 -0700508 else if (val >= 0)
Josef Bacika7905042018-07-03 09:32:35 -0600509 val *= 1000ULL;
Jens Axboed62118b2016-11-28 09:40:34 -0700510
Aleksei Zakharovb7143fe2019-02-11 13:10:34 +0300511 if (wbt_get_min_lat(q) == val)
512 return count;
513
Jens Axboec1253112018-08-23 09:34:46 -0600514 /*
515 * Ensure that the queue is idled, in case the latency update
516 * ends up either enabling or disabling wbt completely. We can't
517 * have IO inflight if that happens.
518 */
Jens Axboea1ce35f2018-10-29 10:23:51 -0600519 blk_mq_freeze_queue(q);
520 blk_mq_quiesce_queue(q);
Jens Axboe80e091d2016-11-28 09:22:47 -0700521
Jens Axboec1253112018-08-23 09:34:46 -0600522 wbt_set_min_lat(q, val);
Jens Axboec1253112018-08-23 09:34:46 -0600523
Jens Axboea1ce35f2018-10-29 10:23:51 -0600524 blk_mq_unquiesce_queue(q);
525 blk_mq_unfreeze_queue(q);
Jens Axboec1253112018-08-23 09:34:46 -0600526
Jens Axboe87760e52016-11-09 12:38:14 -0700527 return count;
528}
529
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600530static ssize_t queue_wc_show(struct request_queue *q, char *page)
531{
532 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
533 return sprintf(page, "write back\n");
534
535 return sprintf(page, "write through\n");
536}
537
538static ssize_t queue_wc_store(struct request_queue *q, const char *page,
539 size_t count)
540{
541 int set = -1;
542
543 if (!strncmp(page, "write back", 10))
544 set = 1;
545 else if (!strncmp(page, "write through", 13) ||
546 !strncmp(page, "none", 4))
547 set = 0;
548
549 if (set == -1)
550 return -EINVAL;
551
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600552 if (set)
Bart Van Assche8814ce82018-03-07 17:10:04 -0800553 blk_queue_flag_set(QUEUE_FLAG_WC, q);
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600554 else
Bart Van Assche8814ce82018-03-07 17:10:04 -0800555 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600556
557 return count;
558}
559
Kent Overstreet6fcefbe2018-05-08 21:33:58 -0400560static ssize_t queue_fua_show(struct request_queue *q, char *page)
561{
562 return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
563}
564
Yigal Kormanea6ca602016-06-23 17:05:51 -0400565static ssize_t queue_dax_show(struct request_queue *q, char *page)
566{
567 return queue_var_show(blk_queue_dax(q), page);
568}
569
Christoph Hellwig35626142020-09-03 08:07:00 +0200570#define QUEUE_RO_ENTRY(_prefix, _name) \
571static struct queue_sysfs_entry _prefix##_entry = { \
572 .attr = { .name = _name, .mode = 0444 }, \
573 .show = _prefix##_show, \
Jens Axboe8324aa92008-01-29 14:51:59 +0100574};
575
Christoph Hellwig35626142020-09-03 08:07:00 +0200576#define QUEUE_RW_ENTRY(_prefix, _name) \
577static struct queue_sysfs_entry _prefix##_entry = { \
578 .attr = { .name = _name, .mode = 0644 }, \
579 .show = _prefix##_show, \
580 .store = _prefix##_store, \
Jens Axboe8324aa92008-01-29 14:51:59 +0100581};
582
Christoph Hellwig35626142020-09-03 08:07:00 +0200583QUEUE_RW_ENTRY(queue_requests, "nr_requests");
584QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
585QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
586QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
587QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
588QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
589QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
590QUEUE_RW_ENTRY(elv_iosched, "scheduler");
Jens Axboe8324aa92008-01-29 14:51:59 +0100591
Christoph Hellwig35626142020-09-03 08:07:00 +0200592QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
593QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
594QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
595QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size");
596QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size");
Jens Axboe8324aa92008-01-29 14:51:59 +0100597
Christoph Hellwig35626142020-09-03 08:07:00 +0200598QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
599QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity");
600QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes");
601QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes");
602QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500603
Christoph Hellwig35626142020-09-03 08:07:00 +0200604QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
605QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes");
606QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes");
Damien Le Moala805a4f2021-01-28 13:47:30 +0900607QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
Christoph Hellwig1e739732017-02-08 14:46:49 +0100608
Christoph Hellwig35626142020-09-03 08:07:00 +0200609QUEUE_RO_ENTRY(queue_zoned, "zoned");
610QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
611QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones");
612QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones");
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200613
Christoph Hellwig35626142020-09-03 08:07:00 +0200614QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
615QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
616QUEUE_RW_ENTRY(queue_poll, "io_poll");
617QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
618QUEUE_RW_ENTRY(queue_wc, "write_cache");
619QUEUE_RO_ENTRY(queue_fua, "fua");
620QUEUE_RO_ENTRY(queue_dax, "dax");
621QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
622QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
Max Gurtovoy28af7422021-04-05 13:20:12 +0000623QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500624
Christoph Hellwig35626142020-09-03 08:07:00 +0200625#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
626QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time");
627#endif
Jens Axboe8324aa92008-01-29 14:51:59 +0100628
Christoph Hellwig35626142020-09-03 08:07:00 +0200629/* legacy alias for logical_block_size: */
Martin K. Petersene68b9032008-01-29 19:14:08 +0100630static struct queue_sysfs_entry queue_hw_sector_size_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600631 .attr = {.name = "hw_sector_size", .mode = 0444 },
Martin K. Petersene1defc42009-05-22 17:17:49 -0400632 .show = queue_logical_block_size_show,
633};
634
Christoph Hellwigfc93fe12020-09-03 08:07:01 +0200635QUEUE_RW_ENTRY(queue_nonrot, "rotational");
636QUEUE_RW_ENTRY(queue_iostats, "iostats");
637QUEUE_RW_ENTRY(queue_random, "add_random");
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200638QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes");
Jens Axboee2e1a142010-06-09 10:42:09 +0200639
Weiping Zhang4d253392019-04-02 21:14:30 +0800640static struct attribute *queue_attrs[] = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100641 &queue_requests_entry.attr,
642 &queue_ra_entry.attr,
643 &queue_max_hw_sectors_entry.attr,
644 &queue_max_sectors_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500645 &queue_max_segments_entry.attr,
Christoph Hellwig1e739732017-02-08 14:46:49 +0100646 &queue_max_discard_segments_entry.attr,
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200647 &queue_max_integrity_segments_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500648 &queue_max_segment_size_entry.attr,
Christoph Hellwig35626142020-09-03 08:07:00 +0200649 &elv_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100650 &queue_hw_sector_size_entry.attr,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400651 &queue_logical_block_size_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400652 &queue_physical_block_size_entry.attr,
Hannes Reinecke87caf972016-10-18 15:40:30 +0900653 &queue_chunk_sectors_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400654 &queue_io_min_entry.attr,
655 &queue_io_opt_entry.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +0100656 &queue_discard_granularity_entry.attr,
657 &queue_discard_max_entry.attr,
Jens Axboe0034af02015-07-16 09:14:26 -0600658 &queue_discard_max_hw_entry.attr,
Martin K. Petersen98262f22009-12-03 09:24:48 +0100659 &queue_discard_zeroes_data_entry.attr,
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400660 &queue_write_same_max_entry.attr,
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800661 &queue_write_zeroes_max_entry.attr,
Keith Busch0512a752020-05-12 17:55:47 +0900662 &queue_zone_append_max_entry.attr,
Damien Le Moala805a4f2021-01-28 13:47:30 +0900663 &queue_zone_write_granularity_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100664 &queue_nonrot_entry.attr,
Damien Le Moal797476b2016-10-18 15:40:29 +0900665 &queue_zoned_entry.attr,
Damien Le Moal965b6522018-10-12 19:08:48 +0900666 &queue_nr_zones_entry.attr,
Niklas Cassele15864f2020-07-14 23:18:23 +0200667 &queue_max_open_zones_entry.attr,
Niklas Cassel659bf822020-07-14 23:18:24 +0200668 &queue_max_active_zones_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200669 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200670 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100671 &queue_iostats_entry.attr,
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200672 &queue_stable_writes_entry.attr,
Jens Axboee2e1a142010-06-09 10:42:09 +0200673 &queue_random_entry.attr,
Jens Axboe05229beed2015-11-05 10:44:55 -0700674 &queue_poll_entry.attr,
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600675 &queue_wc_entry.attr,
Kent Overstreet6fcefbe2018-05-08 21:33:58 -0400676 &queue_fua_entry.attr,
Yigal Kormanea6ca602016-06-23 17:05:51 -0400677 &queue_dax_entry.attr,
Jens Axboe87760e52016-11-09 12:38:14 -0700678 &queue_wb_lat_entry.attr,
Jens Axboe06426ad2016-11-14 13:01:59 -0700679 &queue_poll_delay_entry.attr,
Weiping Zhang65cd1d12018-11-29 00:04:39 +0800680 &queue_io_timeout_entry.attr,
Shaohua Li297e3d82017-03-27 10:51:37 -0700681#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
Christoph Hellwig35626142020-09-03 08:07:00 +0200682 &blk_throtl_sample_time_entry.attr,
Shaohua Li297e3d82017-03-27 10:51:37 -0700683#endif
Max Gurtovoy28af7422021-04-05 13:20:12 +0000684 &queue_virt_boundary_mask_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100685 NULL,
686};
687
Weiping Zhang4d253392019-04-02 21:14:30 +0800688static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
689 int n)
690{
691 struct request_queue *q =
692 container_of(kobj, struct request_queue, kobj);
693
694 if (attr == &queue_io_timeout_entry.attr &&
695 (!q->mq_ops || !q->mq_ops->timeout))
696 return 0;
697
Niklas Cassel659bf822020-07-14 23:18:24 +0200698 if ((attr == &queue_max_open_zones_entry.attr ||
699 attr == &queue_max_active_zones_entry.attr) &&
Niklas Cassele15864f2020-07-14 23:18:23 +0200700 !blk_queue_is_zoned(q))
701 return 0;
702
Weiping Zhang4d253392019-04-02 21:14:30 +0800703 return attr->mode;
704}
705
706static struct attribute_group queue_attr_group = {
707 .attrs = queue_attrs,
708 .is_visible = queue_attr_visible,
709};
710
711
Jens Axboe8324aa92008-01-29 14:51:59 +0100712#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
713
714static ssize_t
715queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
716{
717 struct queue_sysfs_entry *entry = to_queue(attr);
718 struct request_queue *q =
719 container_of(kobj, struct request_queue, kobj);
720 ssize_t res;
721
722 if (!entry->show)
723 return -EIO;
724 mutex_lock(&q->sysfs_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100725 res = entry->show(q, page);
726 mutex_unlock(&q->sysfs_lock);
727 return res;
728}
729
730static ssize_t
731queue_attr_store(struct kobject *kobj, struct attribute *attr,
732 const char *page, size_t length)
733{
734 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100735 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100736 ssize_t res;
737
738 if (!entry->store)
739 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100740
741 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100742 mutex_lock(&q->sysfs_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100743 res = entry->store(q, page, length);
744 mutex_unlock(&q->sysfs_lock);
745 return res;
746}
747
Tejun Heo548bc8e2013-01-09 08:05:13 -0800748static void blk_free_queue_rcu(struct rcu_head *rcu_head)
749{
750 struct request_queue *q = container_of(rcu_head, struct request_queue,
751 rcu_head);
752 kmem_cache_free(blk_requestq_cachep, q);
753}
754
Ming Lei47cdee22019-05-15 11:03:08 +0800755/* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
756static void blk_exit_queue(struct request_queue *q)
757{
758 /*
759 * Since the I/O scheduler exit code may access cgroup information,
760 * perform I/O scheduler exit before disassociating from the block
761 * cgroup controller.
762 */
763 if (q->elevator) {
764 ioc_clear_queue(q);
Ming Leic3e22192019-06-04 21:08:02 +0800765 __elevator_exit(q, q->elevator);
Ming Lei47cdee22019-05-15 11:03:08 +0800766 }
767
768 /*
769 * Remove all references to @q from the block cgroup controller before
770 * restoring @q->queue_lock to avoid that restoring this pointer causes
771 * e.g. blkcg_print_blkgs() to crash.
772 */
773 blkcg_exit_queue(q);
Ming Lei47cdee22019-05-15 11:03:08 +0800774}
775
Jens Axboe8324aa92008-01-29 14:51:59 +0100776/**
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000777 * blk_release_queue - releases all allocated resources of the request_queue
778 * @kobj: pointer to a kobject, whose container is a request_queue
Jens Axboe8324aa92008-01-29 14:51:59 +0100779 *
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000780 * This function releases all allocated resources of the request queue.
781 *
782 * The struct request_queue refcount is incremented with blk_get_queue() and
783 * decremented with blk_put_queue(). Once the refcount reaches 0 this function
784 * is called.
785 *
786 * For drivers that have a request_queue on a gendisk and added with
787 * __device_add_disk() the refcount to request_queue will reach 0 with
788 * the last put_disk() called by the driver. For drivers which don't use
789 * __device_add_disk() this happens with blk_cleanup_queue().
790 *
791 * Drivers exist which depend on the release of the request_queue to be
792 * synchronous, it should not be deferred.
793 *
794 * Context: can sleep
Bart Van Asschedc9edc42017-06-14 13:27:50 -0600795 */
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000796static void blk_release_queue(struct kobject *kobj)
Jens Axboe8324aa92008-01-29 14:51:59 +0100797{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000798 struct request_queue *q =
799 container_of(kobj, struct request_queue, kobj);
800
801 might_sleep();
Jens Axboe8324aa92008-01-29 14:51:59 +0100802
Omar Sandoval34dbad52017-03-21 08:56:08 -0700803 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
804 blk_stat_remove_callback(q, q->poll_cb);
805 blk_stat_free_callback(q->poll_cb);
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600806
Omar Sandoval34dbad52017-03-21 08:56:08 -0700807 blk_free_queue_stats(q->stats);
808
Yang Yang47ce0302020-10-09 01:00:14 -0700809 if (queue_is_mq(q)) {
810 struct blk_mq_hw_ctx *hctx;
811 int i;
812
zhengbine26cc082019-08-12 20:36:55 +0800813 cancel_delayed_work_sync(&q->requeue_work);
814
Yang Yang47ce0302020-10-09 01:00:14 -0700815 queue_for_each_hw_ctx(q, hctx, i)
816 cancel_delayed_work_sync(&hctx->run_work);
817 }
818
Ming Lei47cdee22019-05-15 11:03:08 +0800819 blk_exit_queue(q);
820
Damien Le Moalbf505452018-10-12 19:08:50 +0900821 blk_queue_free_zone_bitmaps(q);
822
Jens Axboe344e9ff2018-11-15 12:22:51 -0700823 if (queue_is_mq(q))
Ming Leie09aae72015-01-29 20:17:27 +0800824 blk_mq_release(q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700825
Jens Axboe8324aa92008-01-29 14:51:59 +0100826 blk_trace_shutdown(q);
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000827 mutex_lock(&q->debugfs_mutex);
828 debugfs_remove_recursive(q->debugfs_dir);
829 mutex_unlock(&q->debugfs_mutex);
Jens Axboe8324aa92008-01-29 14:51:59 +0100830
Jens Axboe344e9ff2018-11-15 12:22:51 -0700831 if (queue_is_mq(q))
Omar Sandoval62ebce162017-01-31 14:53:21 -0800832 blk_mq_debugfs_unregister(q);
833
Kent Overstreet338aa962018-05-20 18:25:47 -0400834 bioset_exit(&q->bio_split);
Kent Overstreet54efd502015-04-23 22:37:18 -0700835
Tejun Heoa73f7302011-12-14 00:33:37 +0100836 ida_simple_remove(&blk_queue_ida, q->id);
Tejun Heo548bc8e2013-01-09 08:05:13 -0800837 call_rcu(&q->rcu_head, blk_free_queue_rcu);
Jens Axboe8324aa92008-01-29 14:51:59 +0100838}
839
Emese Revfy52cf25d2010-01-19 02:58:23 +0100840static const struct sysfs_ops queue_sysfs_ops = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100841 .show = queue_attr_show,
842 .store = queue_attr_store,
843};
844
845struct kobj_type blk_queue_ktype = {
846 .sysfs_ops = &queue_sysfs_ops,
Jens Axboe8324aa92008-01-29 14:51:59 +0100847 .release = blk_release_queue,
848};
849
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800850/**
851 * blk_register_queue - register a block layer queue with sysfs
852 * @disk: Disk of which the request queue should be registered with sysfs.
853 */
Jens Axboe8324aa92008-01-29 14:51:59 +0100854int blk_register_queue(struct gendisk *disk)
855{
856 int ret;
Li Zefan1d54ad62009-04-14 14:00:05 +0800857 struct device *dev = disk_to_dev(disk);
Jens Axboe8324aa92008-01-29 14:51:59 +0100858 struct request_queue *q = disk->queue;
859
Akinobu Mitafb199742008-04-21 09:51:06 +0200860 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100861 return -ENXIO;
862
Ming Lei58c898b2019-08-27 19:01:47 +0800863 WARN_ONCE(blk_queue_registered(q),
Omar Sandoval334335d2017-03-28 16:12:15 -0700864 "%s is registering an already registered queue\n",
865 kobject_name(&dev->kobj));
Omar Sandoval334335d2017-03-28 16:12:15 -0700866
Christoph Hellwig471aa702021-08-09 16:17:41 +0200867 disk_update_readahead(disk);
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +0200868
Li Zefan1d54ad62009-04-14 14:00:05 +0800869 ret = blk_trace_init_sysfs(dev);
870 if (ret)
871 return ret;
872
Ming Leicecf5d82019-08-27 19:01:48 +0800873 mutex_lock(&q->sysfs_dir_lock);
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800874
Linus Torvaldsc9059592009-06-11 10:52:27 -0700875 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
Liu Yuaned5302d2011-04-19 13:47:58 +0200876 if (ret < 0) {
877 blk_trace_remove_sysfs(dev);
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800878 goto unlock;
Liu Yuaned5302d2011-04-19 13:47:58 +0200879 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100880
Weiping Zhang4d253392019-04-02 21:14:30 +0800881 ret = sysfs_create_group(&q->kobj, &queue_attr_group);
882 if (ret) {
883 blk_trace_remove_sysfs(dev);
884 kobject_del(&q->kobj);
885 kobject_put(&dev->kobj);
886 goto unlock;
887 }
888
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000889 mutex_lock(&q->debugfs_mutex);
890 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
891 blk_debugfs_root);
892 mutex_unlock(&q->debugfs_mutex);
893
Jens Axboe344e9ff2018-11-15 12:22:51 -0700894 if (queue_is_mq(q)) {
Bart Van Assche2d0364c2017-04-26 13:47:48 -0700895 __blk_mq_register_dev(dev, q);
Bart Van Asschea8ecdd72017-05-25 16:38:06 -0700896 blk_mq_debugfs_register(q);
897 }
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600898
Ming Leib89f6252019-09-23 23:12:09 +0800899 mutex_lock(&q->sysfs_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700900 if (q->elevator) {
Ming Leicecf5d82019-08-27 19:01:48 +0800901 ret = elv_register_queue(q, false);
Omar Sandoval80c6b152017-02-06 12:52:24 -0800902 if (ret) {
Ming Leib89f6252019-09-23 23:12:09 +0800903 mutex_unlock(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800904 mutex_unlock(&q->sysfs_dir_lock);
Omar Sandoval80c6b152017-02-06 12:52:24 -0800905 kobject_del(&q->kobj);
906 blk_trace_remove_sysfs(dev);
907 kobject_put(&dev->kobj);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800908 return ret;
Omar Sandoval80c6b152017-02-06 12:52:24 -0800909 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100910 }
Ming Leicecf5d82019-08-27 19:01:48 +0800911
Ming Leicecf5d82019-08-27 19:01:48 +0800912 blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
913 wbt_enable_default(q);
914 blk_throtl_register_queue(q);
915
916 /* Now everything is ready and send out KOBJ_ADD uevent */
917 kobject_uevent(&q->kobj, KOBJ_ADD);
Yufen Yu05468582020-10-08 23:26:32 -0400918 if (q->elevator)
Ming Leicecf5d82019-08-27 19:01:48 +0800919 kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
920 mutex_unlock(&q->sysfs_lock);
921
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800922 ret = 0;
923unlock:
Ming Leicecf5d82019-08-27 19:01:48 +0800924 mutex_unlock(&q->sysfs_dir_lock);
Ming Leia72c3742021-06-09 09:58:22 +0800925
926 /*
927 * SCSI probing may synchronously create and destroy a lot of
928 * request_queues for non-existent devices. Shutting down a fully
929 * functional queue takes measureable wallclock time as RCU grace
930 * periods are involved. To avoid excessive latency in these
931 * cases, a request_queue starts out in a degraded mode which is
932 * faster to shut down and is made fully functional here as
933 * request_queues for non-existent devices never get registered.
934 */
935 if (!blk_queue_init_done(q)) {
936 blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
937 percpu_ref_switch_to_percpu(&q->q_usage_counter);
938 }
939
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800940 return ret;
Jens Axboe8324aa92008-01-29 14:51:59 +0100941}
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500942EXPORT_SYMBOL_GPL(blk_register_queue);
Jens Axboe8324aa92008-01-29 14:51:59 +0100943
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800944/**
945 * blk_unregister_queue - counterpart of blk_register_queue()
946 * @disk: Disk of which the request queue should be unregistered from sysfs.
947 *
948 * Note: the caller is responsible for guaranteeing that this function is called
949 * after blk_register_queue() has finished.
950 */
Jens Axboe8324aa92008-01-29 14:51:59 +0100951void blk_unregister_queue(struct gendisk *disk)
952{
953 struct request_queue *q = disk->queue;
954
Akinobu Mitafb199742008-04-21 09:51:06 +0200955 if (WARN_ON(!q))
956 return;
957
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500958 /* Return early if disk->queue was never registered. */
Ming Lei58c898b2019-08-27 19:01:47 +0800959 if (!blk_queue_registered(q))
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500960 return;
961
Mike Snitzer667257e2018-01-11 14:11:01 -0500962 /*
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800963 * Since sysfs_remove_dir() prevents adding new directory entries
964 * before removal of existing entries starts, protect against
965 * concurrent elv_iosched_store() calls.
Mike Snitzer667257e2018-01-11 14:11:01 -0500966 */
David Jefferye9a823f2017-08-28 10:52:44 -0600967 mutex_lock(&q->sysfs_lock);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800968 blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
Ming Leicecf5d82019-08-27 19:01:48 +0800969 mutex_unlock(&q->sysfs_lock);
Omar Sandoval334335d2017-03-28 16:12:15 -0700970
Ming Leicecf5d82019-08-27 19:01:48 +0800971 mutex_lock(&q->sysfs_dir_lock);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800972 /*
973 * Remove the sysfs attributes before unregistering the queue data
974 * structures that can be modified through sysfs.
975 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700976 if (queue_is_mq(q))
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200977 blk_mq_unregister_dev(disk_to_dev(disk), q);
Jens Axboe8324aa92008-01-29 14:51:59 +0100978
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +0200979 kobject_uevent(&q->kobj, KOBJ_REMOVE);
980 kobject_del(&q->kobj);
981 blk_trace_remove_sysfs(disk_to_dev(disk));
Mike Snitzer667257e2018-01-11 14:11:01 -0500982
Ming Leib89f6252019-09-23 23:12:09 +0800983 mutex_lock(&q->sysfs_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700984 if (q->elevator)
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800985 elv_unregister_queue(q);
Ming Leib89f6252019-09-23 23:12:09 +0800986 mutex_unlock(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800987 mutex_unlock(&q->sysfs_dir_lock);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800988
989 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100990}