blob: 614d9d47de36b0873596ba14876328a17387f613 [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 Hellwigd152c682021-08-16 15:46:24 +020093 if (!q->disk)
Christoph Hellwigedb08722021-08-09 16:17:43 +020094 return -EINVAL;
Christoph Hellwigd152c682021-08-16 15:46:24 +020095 ra_kb = q->disk->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 Hellwigd152c682021-08-16 15:46:24 +0200105 if (!q->disk)
Christoph Hellwigedb08722021-08-09 16:17:43 +0200106 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 Hellwigd152c682021-08-16 15:46:24 +0200110 q->disk->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 Hellwigd152c682021-08-16 15:46:24 +0200257 if (q->disk)
258 q->disk->bdi->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700259 spin_unlock_irq(&q->queue_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100260
261 return ret;
262}
263
264static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
265{
Martin K. Petersenae03bf62009-05-22 17:17:50 -0400266 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
Jens Axboe8324aa92008-01-29 14:51:59 +0100267
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000268 return queue_var_show(max_hw_sectors_kb, page);
Jens Axboe8324aa92008-01-29 14:51:59 +0100269}
270
Max Gurtovoy28af7422021-04-05 13:20:12 +0000271static ssize_t queue_virt_boundary_mask_show(struct request_queue *q, char *page)
272{
Max Gurtovoy8c390ff2021-05-11 15:53:19 +0000273 return queue_var_show(q->limits.virt_boundary_mask, page);
Max Gurtovoy28af7422021-04-05 13:20:12 +0000274}
275
Jens Axboe956bcb72010-08-07 18:13:50 +0200276#define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
277static ssize_t \
Christoph Hellwigfc93fe12020-09-03 08:07:01 +0200278queue_##name##_show(struct request_queue *q, char *page) \
Jens Axboe956bcb72010-08-07 18:13:50 +0200279{ \
280 int bit; \
281 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
282 return queue_var_show(neg ? !bit : bit, page); \
283} \
284static ssize_t \
Christoph Hellwigfc93fe12020-09-03 08:07:01 +0200285queue_##name##_store(struct request_queue *q, const char *page, size_t count) \
Jens Axboe956bcb72010-08-07 18:13:50 +0200286{ \
287 unsigned long val; \
288 ssize_t ret; \
289 ret = queue_var_store(&val, page, count); \
Arnd Bergmannc678ef52013-04-03 21:53:57 +0200290 if (ret < 0) \
291 return ret; \
Jens Axboe956bcb72010-08-07 18:13:50 +0200292 if (neg) \
293 val = !val; \
294 \
Jens Axboe956bcb72010-08-07 18:13:50 +0200295 if (val) \
Bart Van Assche8814ce82018-03-07 17:10:04 -0800296 blk_queue_flag_set(QUEUE_FLAG_##flag, q); \
Jens Axboe956bcb72010-08-07 18:13:50 +0200297 else \
Bart Van Assche8814ce82018-03-07 17:10:04 -0800298 blk_queue_flag_clear(QUEUE_FLAG_##flag, q); \
Jens Axboe956bcb72010-08-07 18:13:50 +0200299 return ret; \
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100300}
301
Jens Axboe956bcb72010-08-07 18:13:50 +0200302QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
303QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
304QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200305QUEUE_SYSFS_BIT_FNS(stable_writes, STABLE_WRITES, 0);
Jens Axboe956bcb72010-08-07 18:13:50 +0200306#undef QUEUE_SYSFS_BIT_FNS
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100307
Damien Le Moal797476b2016-10-18 15:40:29 +0900308static ssize_t queue_zoned_show(struct request_queue *q, char *page)
309{
310 switch (blk_queue_zoned_model(q)) {
311 case BLK_ZONED_HA:
312 return sprintf(page, "host-aware\n");
313 case BLK_ZONED_HM:
314 return sprintf(page, "host-managed\n");
315 default:
316 return sprintf(page, "none\n");
317 }
318}
319
Damien Le Moal965b6522018-10-12 19:08:48 +0900320static ssize_t queue_nr_zones_show(struct request_queue *q, char *page)
321{
322 return queue_var_show(blk_queue_nr_zones(q), page);
323}
324
Niklas Cassele15864f2020-07-14 23:18:23 +0200325static ssize_t queue_max_open_zones_show(struct request_queue *q, char *page)
326{
327 return queue_var_show(queue_max_open_zones(q), page);
328}
329
Niklas Cassel659bf822020-07-14 23:18:24 +0200330static ssize_t queue_max_active_zones_show(struct request_queue *q, char *page)
331{
332 return queue_var_show(queue_max_active_zones(q), page);
333}
334
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200335static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
336{
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100337 return queue_var_show((blk_queue_nomerges(q) << 1) |
338 blk_queue_noxmerges(q), page);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200339}
340
341static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
342 size_t count)
343{
344 unsigned long nm;
345 ssize_t ret = queue_var_store(&nm, page, count);
346
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400347 if (ret < 0)
348 return ret;
349
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100350 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
351 blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100352 if (nm == 2)
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100353 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelle488991e2010-01-29 09:04:08 +0100354 else if (nm)
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100355 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100356
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200357 return ret;
358}
359
Jens Axboec7c22e42008-09-13 20:26:01 +0200360static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
361{
Xiaotian Feng9cb308c2009-07-17 15:26:26 +0800362 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
Dan Williams5757a6d2011-07-23 20:44:25 +0200363 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
Jens Axboec7c22e42008-09-13 20:26:01 +0200364
Dan Williams5757a6d2011-07-23 20:44:25 +0200365 return queue_var_show(set << force, page);
Jens Axboec7c22e42008-09-13 20:26:01 +0200366}
367
368static ssize_t
369queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
370{
371 ssize_t ret = -EINVAL;
Christoph Hellwig0a06ff02013-11-14 14:32:07 -0800372#ifdef CONFIG_SMP
Jens Axboec7c22e42008-09-13 20:26:01 +0200373 unsigned long val;
374
375 ret = queue_var_store(&val, page, count);
Dave Reisnerb1f3b642012-09-08 11:55:45 -0400376 if (ret < 0)
377 return ret;
378
Eric Seppanene8037d42011-08-23 21:25:12 +0200379 if (val == 2) {
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100380 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
381 blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
Eric Seppanene8037d42011-08-23 21:25:12 +0200382 } else if (val == 1) {
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100383 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
384 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
Eric Seppanene8037d42011-08-23 21:25:12 +0200385 } else if (val == 0) {
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100386 blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
387 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
Dan Williams5757a6d2011-07-23 20:44:25 +0200388 }
Jens Axboec7c22e42008-09-13 20:26:01 +0200389#endif
390 return ret;
391}
Jens Axboe8324aa92008-01-29 14:51:59 +0100392
Jens Axboe06426ad2016-11-14 13:01:59 -0700393static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
394{
Jens Axboe64f1c212016-11-14 13:03:03 -0700395 int val;
396
Yufen Yu29ece8b2019-03-18 22:44:41 +0800397 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
398 val = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -0700399 else
400 val = q->poll_nsec / 1000;
401
402 return sprintf(page, "%d\n", val);
Jens Axboe06426ad2016-11-14 13:01:59 -0700403}
404
405static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
406 size_t count)
407{
Jens Axboe64f1c212016-11-14 13:03:03 -0700408 int err, val;
Jens Axboe06426ad2016-11-14 13:01:59 -0700409
410 if (!q->mq_ops || !q->mq_ops->poll)
411 return -EINVAL;
412
Jens Axboe64f1c212016-11-14 13:03:03 -0700413 err = kstrtoint(page, 10, &val);
414 if (err < 0)
415 return err;
Jens Axboe06426ad2016-11-14 13:01:59 -0700416
Yufen Yu29ece8b2019-03-18 22:44:41 +0800417 if (val == BLK_MQ_POLL_CLASSIC)
418 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
419 else if (val >= 0)
Jens Axboe64f1c212016-11-14 13:03:03 -0700420 q->poll_nsec = val * 1000;
Yufen Yu29ece8b2019-03-18 22:44:41 +0800421 else
422 return -EINVAL;
Jens Axboe64f1c212016-11-14 13:03:03 -0700423
424 return count;
Jens Axboe06426ad2016-11-14 13:01:59 -0700425}
426
Jens Axboe05229beed2015-11-05 10:44:55 -0700427static ssize_t queue_poll_show(struct request_queue *q, char *page)
428{
429 return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
430}
431
432static ssize_t queue_poll_store(struct request_queue *q, const char *page,
433 size_t count)
434{
435 unsigned long poll_on;
436 ssize_t ret;
437
Ming Leicd191812018-12-18 12:15:29 +0800438 if (!q->tag_set || q->tag_set->nr_maps <= HCTX_TYPE_POLL ||
439 !q->tag_set->map[HCTX_TYPE_POLL].nr_queues)
Jens Axboe05229beed2015-11-05 10:44:55 -0700440 return -EINVAL;
441
442 ret = queue_var_store(&poll_on, page, count);
443 if (ret < 0)
444 return ret;
445
Jeffle Xu6b09b4d2021-02-22 14:54:52 +0800446 if (poll_on) {
Bart Van Assche8814ce82018-03-07 17:10:04 -0800447 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jeffle Xu6b09b4d2021-02-22 14:54:52 +0800448 } else {
449 blk_mq_freeze_queue(q);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800450 blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
Jeffle Xu6b09b4d2021-02-22 14:54:52 +0800451 blk_mq_unfreeze_queue(q);
452 }
Jens Axboe05229beed2015-11-05 10:44:55 -0700453
454 return ret;
455}
456
Weiping Zhang65cd1d12018-11-29 00:04:39 +0800457static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
458{
459 return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
460}
461
462static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
463 size_t count)
464{
465 unsigned int val;
466 int err;
467
468 err = kstrtou32(page, 10, &val);
469 if (err || val == 0)
470 return -EINVAL;
471
472 blk_queue_rq_timeout(q, msecs_to_jiffies(val));
473
474 return count;
475}
476
Jens Axboe87760e52016-11-09 12:38:14 -0700477static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
478{
Josef Bacika7905042018-07-03 09:32:35 -0600479 if (!wbt_rq_qos(q))
Jens Axboe87760e52016-11-09 12:38:14 -0700480 return -EINVAL;
481
Josef Bacika7905042018-07-03 09:32:35 -0600482 return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
Jens Axboe87760e52016-11-09 12:38:14 -0700483}
484
485static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
486 size_t count)
487{
Josef Bacika7905042018-07-03 09:32:35 -0600488 struct rq_qos *rqos;
Jens Axboe87760e52016-11-09 12:38:14 -0700489 ssize_t ret;
Jens Axboe80e091d2016-11-28 09:22:47 -0700490 s64 val;
Jens Axboe87760e52016-11-09 12:38:14 -0700491
Jens Axboe87760e52016-11-09 12:38:14 -0700492 ret = queue_var_store64(&val, page);
493 if (ret < 0)
494 return ret;
Jens Axboed62118b2016-11-28 09:40:34 -0700495 if (val < -1)
496 return -EINVAL;
497
Josef Bacika7905042018-07-03 09:32:35 -0600498 rqos = wbt_rq_qos(q);
499 if (!rqos) {
Jens Axboed62118b2016-11-28 09:40:34 -0700500 ret = wbt_init(q);
501 if (ret)
502 return ret;
Jens Axboed62118b2016-11-28 09:40:34 -0700503 }
Jens Axboe87760e52016-11-09 12:38:14 -0700504
Jens Axboe80e091d2016-11-28 09:22:47 -0700505 if (val == -1)
Josef Bacika7905042018-07-03 09:32:35 -0600506 val = wbt_default_latency_nsec(q);
Jens Axboe80e091d2016-11-28 09:22:47 -0700507 else if (val >= 0)
Josef Bacika7905042018-07-03 09:32:35 -0600508 val *= 1000ULL;
Jens Axboed62118b2016-11-28 09:40:34 -0700509
Aleksei Zakharovb7143fe2019-02-11 13:10:34 +0300510 if (wbt_get_min_lat(q) == val)
511 return count;
512
Jens Axboec1253112018-08-23 09:34:46 -0600513 /*
514 * Ensure that the queue is idled, in case the latency update
515 * ends up either enabling or disabling wbt completely. We can't
516 * have IO inflight if that happens.
517 */
Jens Axboea1ce35f2018-10-29 10:23:51 -0600518 blk_mq_freeze_queue(q);
519 blk_mq_quiesce_queue(q);
Jens Axboe80e091d2016-11-28 09:22:47 -0700520
Jens Axboec1253112018-08-23 09:34:46 -0600521 wbt_set_min_lat(q, val);
Jens Axboec1253112018-08-23 09:34:46 -0600522
Jens Axboea1ce35f2018-10-29 10:23:51 -0600523 blk_mq_unquiesce_queue(q);
524 blk_mq_unfreeze_queue(q);
Jens Axboec1253112018-08-23 09:34:46 -0600525
Jens Axboe87760e52016-11-09 12:38:14 -0700526 return count;
527}
528
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600529static ssize_t queue_wc_show(struct request_queue *q, char *page)
530{
531 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
532 return sprintf(page, "write back\n");
533
534 return sprintf(page, "write through\n");
535}
536
537static ssize_t queue_wc_store(struct request_queue *q, const char *page,
538 size_t count)
539{
540 int set = -1;
541
542 if (!strncmp(page, "write back", 10))
543 set = 1;
544 else if (!strncmp(page, "write through", 13) ||
545 !strncmp(page, "none", 4))
546 set = 0;
547
548 if (set == -1)
549 return -EINVAL;
550
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600551 if (set)
Bart Van Assche8814ce82018-03-07 17:10:04 -0800552 blk_queue_flag_set(QUEUE_FLAG_WC, q);
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600553 else
Bart Van Assche8814ce82018-03-07 17:10:04 -0800554 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600555
556 return count;
557}
558
Kent Overstreet6fcefbe2018-05-08 21:33:58 -0400559static ssize_t queue_fua_show(struct request_queue *q, char *page)
560{
561 return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
562}
563
Yigal Kormanea6ca602016-06-23 17:05:51 -0400564static ssize_t queue_dax_show(struct request_queue *q, char *page)
565{
566 return queue_var_show(blk_queue_dax(q), page);
567}
568
Christoph Hellwig35626142020-09-03 08:07:00 +0200569#define QUEUE_RO_ENTRY(_prefix, _name) \
570static struct queue_sysfs_entry _prefix##_entry = { \
571 .attr = { .name = _name, .mode = 0444 }, \
572 .show = _prefix##_show, \
Jens Axboe8324aa92008-01-29 14:51:59 +0100573};
574
Christoph Hellwig35626142020-09-03 08:07:00 +0200575#define QUEUE_RW_ENTRY(_prefix, _name) \
576static struct queue_sysfs_entry _prefix##_entry = { \
577 .attr = { .name = _name, .mode = 0644 }, \
578 .show = _prefix##_show, \
579 .store = _prefix##_store, \
Jens Axboe8324aa92008-01-29 14:51:59 +0100580};
581
Christoph Hellwig35626142020-09-03 08:07:00 +0200582QUEUE_RW_ENTRY(queue_requests, "nr_requests");
583QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
584QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
585QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
586QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
587QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
588QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
589QUEUE_RW_ENTRY(elv_iosched, "scheduler");
Jens Axboe8324aa92008-01-29 14:51:59 +0100590
Christoph Hellwig35626142020-09-03 08:07:00 +0200591QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
592QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
593QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
594QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size");
595QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size");
Jens Axboe8324aa92008-01-29 14:51:59 +0100596
Christoph Hellwig35626142020-09-03 08:07:00 +0200597QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
598QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity");
599QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes");
600QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes");
601QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500602
Christoph Hellwig35626142020-09-03 08:07:00 +0200603QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
604QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes");
605QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes");
Damien Le Moala805a4f2021-01-28 13:47:30 +0900606QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
Christoph Hellwig1e739732017-02-08 14:46:49 +0100607
Christoph Hellwig35626142020-09-03 08:07:00 +0200608QUEUE_RO_ENTRY(queue_zoned, "zoned");
609QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
610QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones");
611QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones");
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200612
Christoph Hellwig35626142020-09-03 08:07:00 +0200613QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
614QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
615QUEUE_RW_ENTRY(queue_poll, "io_poll");
616QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
617QUEUE_RW_ENTRY(queue_wc, "write_cache");
618QUEUE_RO_ENTRY(queue_fua, "fua");
619QUEUE_RO_ENTRY(queue_dax, "dax");
620QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
621QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
Max Gurtovoy28af7422021-04-05 13:20:12 +0000622QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500623
Christoph Hellwig35626142020-09-03 08:07:00 +0200624#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
625QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time");
626#endif
Jens Axboe8324aa92008-01-29 14:51:59 +0100627
Christoph Hellwig35626142020-09-03 08:07:00 +0200628/* legacy alias for logical_block_size: */
Martin K. Petersene68b9032008-01-29 19:14:08 +0100629static struct queue_sysfs_entry queue_hw_sector_size_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600630 .attr = {.name = "hw_sector_size", .mode = 0444 },
Martin K. Petersene1defc42009-05-22 17:17:49 -0400631 .show = queue_logical_block_size_show,
632};
633
Christoph Hellwigfc93fe12020-09-03 08:07:01 +0200634QUEUE_RW_ENTRY(queue_nonrot, "rotational");
635QUEUE_RW_ENTRY(queue_iostats, "iostats");
636QUEUE_RW_ENTRY(queue_random, "add_random");
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200637QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes");
Jens Axboee2e1a142010-06-09 10:42:09 +0200638
Weiping Zhang4d253392019-04-02 21:14:30 +0800639static struct attribute *queue_attrs[] = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100640 &queue_requests_entry.attr,
641 &queue_ra_entry.attr,
642 &queue_max_hw_sectors_entry.attr,
643 &queue_max_sectors_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500644 &queue_max_segments_entry.attr,
Christoph Hellwig1e739732017-02-08 14:46:49 +0100645 &queue_max_discard_segments_entry.attr,
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200646 &queue_max_integrity_segments_entry.attr,
Martin K. Petersenc77a5712010-03-10 00:48:33 -0500647 &queue_max_segment_size_entry.attr,
Christoph Hellwig35626142020-09-03 08:07:00 +0200648 &elv_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100649 &queue_hw_sector_size_entry.attr,
Martin K. Petersene1defc42009-05-22 17:17:49 -0400650 &queue_logical_block_size_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400651 &queue_physical_block_size_entry.attr,
Hannes Reinecke87caf972016-10-18 15:40:30 +0900652 &queue_chunk_sectors_entry.attr,
Martin K. Petersenc72758f2009-05-22 17:17:53 -0400653 &queue_io_min_entry.attr,
654 &queue_io_opt_entry.attr,
Martin K. Petersen86b37282009-11-10 11:50:21 +0100655 &queue_discard_granularity_entry.attr,
656 &queue_discard_max_entry.attr,
Jens Axboe0034af02015-07-16 09:14:26 -0600657 &queue_discard_max_hw_entry.attr,
Martin K. Petersen98262f22009-12-03 09:24:48 +0100658 &queue_discard_zeroes_data_entry.attr,
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400659 &queue_write_same_max_entry.attr,
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800660 &queue_write_zeroes_max_entry.attr,
Keith Busch0512a752020-05-12 17:55:47 +0900661 &queue_zone_append_max_entry.attr,
Damien Le Moala805a4f2021-01-28 13:47:30 +0900662 &queue_zone_write_granularity_entry.attr,
Bartlomiej Zolnierkiewicz1308835f2009-01-07 12:22:39 +0100663 &queue_nonrot_entry.attr,
Damien Le Moal797476b2016-10-18 15:40:29 +0900664 &queue_zoned_entry.attr,
Damien Le Moal965b6522018-10-12 19:08:48 +0900665 &queue_nr_zones_entry.attr,
Niklas Cassele15864f2020-07-14 23:18:23 +0200666 &queue_max_open_zones_entry.attr,
Niklas Cassel659bf822020-07-14 23:18:24 +0200667 &queue_max_active_zones_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200668 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200669 &queue_rq_affinity_entry.attr,
Jens Axboebc58ba92009-01-23 10:54:44 +0100670 &queue_iostats_entry.attr,
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200671 &queue_stable_writes_entry.attr,
Jens Axboee2e1a142010-06-09 10:42:09 +0200672 &queue_random_entry.attr,
Jens Axboe05229beed2015-11-05 10:44:55 -0700673 &queue_poll_entry.attr,
Jens Axboe93e9d8e2016-04-12 12:32:46 -0600674 &queue_wc_entry.attr,
Kent Overstreet6fcefbe2018-05-08 21:33:58 -0400675 &queue_fua_entry.attr,
Yigal Kormanea6ca602016-06-23 17:05:51 -0400676 &queue_dax_entry.attr,
Jens Axboe87760e52016-11-09 12:38:14 -0700677 &queue_wb_lat_entry.attr,
Jens Axboe06426ad2016-11-14 13:01:59 -0700678 &queue_poll_delay_entry.attr,
Weiping Zhang65cd1d12018-11-29 00:04:39 +0800679 &queue_io_timeout_entry.attr,
Shaohua Li297e3d82017-03-27 10:51:37 -0700680#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
Christoph Hellwig35626142020-09-03 08:07:00 +0200681 &blk_throtl_sample_time_entry.attr,
Shaohua Li297e3d82017-03-27 10:51:37 -0700682#endif
Max Gurtovoy28af7422021-04-05 13:20:12 +0000683 &queue_virt_boundary_mask_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100684 NULL,
685};
686
Weiping Zhang4d253392019-04-02 21:14:30 +0800687static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
688 int n)
689{
690 struct request_queue *q =
691 container_of(kobj, struct request_queue, kobj);
692
693 if (attr == &queue_io_timeout_entry.attr &&
694 (!q->mq_ops || !q->mq_ops->timeout))
695 return 0;
696
Niklas Cassel659bf822020-07-14 23:18:24 +0200697 if ((attr == &queue_max_open_zones_entry.attr ||
698 attr == &queue_max_active_zones_entry.attr) &&
Niklas Cassele15864f2020-07-14 23:18:23 +0200699 !blk_queue_is_zoned(q))
700 return 0;
701
Weiping Zhang4d253392019-04-02 21:14:30 +0800702 return attr->mode;
703}
704
705static struct attribute_group queue_attr_group = {
706 .attrs = queue_attrs,
707 .is_visible = queue_attr_visible,
708};
709
710
Jens Axboe8324aa92008-01-29 14:51:59 +0100711#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
712
713static ssize_t
714queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
715{
716 struct queue_sysfs_entry *entry = to_queue(attr);
717 struct request_queue *q =
718 container_of(kobj, struct request_queue, kobj);
719 ssize_t res;
720
721 if (!entry->show)
722 return -EIO;
723 mutex_lock(&q->sysfs_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100724 res = entry->show(q, page);
725 mutex_unlock(&q->sysfs_lock);
726 return res;
727}
728
729static ssize_t
730queue_attr_store(struct kobject *kobj, struct attribute *attr,
731 const char *page, size_t length)
732{
733 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100734 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100735 ssize_t res;
736
737 if (!entry->store)
738 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100739
740 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100741 mutex_lock(&q->sysfs_lock);
Jens Axboe8324aa92008-01-29 14:51:59 +0100742 res = entry->store(q, page, length);
743 mutex_unlock(&q->sysfs_lock);
744 return res;
745}
746
Tejun Heo548bc8e2013-01-09 08:05:13 -0800747static void blk_free_queue_rcu(struct rcu_head *rcu_head)
748{
749 struct request_queue *q = container_of(rcu_head, struct request_queue,
750 rcu_head);
751 kmem_cache_free(blk_requestq_cachep, q);
752}
753
Ming Lei47cdee22019-05-15 11:03:08 +0800754/* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
755static void blk_exit_queue(struct request_queue *q)
756{
757 /*
758 * Since the I/O scheduler exit code may access cgroup information,
759 * perform I/O scheduler exit before disassociating from the block
760 * cgroup controller.
761 */
762 if (q->elevator) {
763 ioc_clear_queue(q);
Ming Leic3e22192019-06-04 21:08:02 +0800764 __elevator_exit(q, q->elevator);
Ming Lei47cdee22019-05-15 11:03:08 +0800765 }
766
767 /*
768 * Remove all references to @q from the block cgroup controller before
769 * restoring @q->queue_lock to avoid that restoring this pointer causes
770 * e.g. blkcg_print_blkgs() to crash.
771 */
772 blkcg_exit_queue(q);
Ming Lei47cdee22019-05-15 11:03:08 +0800773}
774
Jens Axboe8324aa92008-01-29 14:51:59 +0100775/**
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000776 * blk_release_queue - releases all allocated resources of the request_queue
777 * @kobj: pointer to a kobject, whose container is a request_queue
Jens Axboe8324aa92008-01-29 14:51:59 +0100778 *
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000779 * This function releases all allocated resources of the request queue.
780 *
781 * The struct request_queue refcount is incremented with blk_get_queue() and
782 * decremented with blk_put_queue(). Once the refcount reaches 0 this function
783 * is called.
784 *
785 * For drivers that have a request_queue on a gendisk and added with
786 * __device_add_disk() the refcount to request_queue will reach 0 with
787 * the last put_disk() called by the driver. For drivers which don't use
788 * __device_add_disk() this happens with blk_cleanup_queue().
789 *
790 * Drivers exist which depend on the release of the request_queue to be
791 * synchronous, it should not be deferred.
792 *
793 * Context: can sleep
Bart Van Asschedc9edc42017-06-14 13:27:50 -0600794 */
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000795static void blk_release_queue(struct kobject *kobj)
Jens Axboe8324aa92008-01-29 14:51:59 +0100796{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000797 struct request_queue *q =
798 container_of(kobj, struct request_queue, kobj);
799
800 might_sleep();
Jens Axboe8324aa92008-01-29 14:51:59 +0100801
Omar Sandoval34dbad52017-03-21 08:56:08 -0700802 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
803 blk_stat_remove_callback(q, q->poll_cb);
804 blk_stat_free_callback(q->poll_cb);
Hannes Reinecke777eb1b2011-09-28 08:07:01 -0600805
Omar Sandoval34dbad52017-03-21 08:56:08 -0700806 blk_free_queue_stats(q->stats);
807
Yang Yang47ce0302020-10-09 01:00:14 -0700808 if (queue_is_mq(q)) {
809 struct blk_mq_hw_ctx *hctx;
810 int i;
811
zhengbine26cc082019-08-12 20:36:55 +0800812 cancel_delayed_work_sync(&q->requeue_work);
813
Yang Yang47ce0302020-10-09 01:00:14 -0700814 queue_for_each_hw_ctx(q, hctx, i)
815 cancel_delayed_work_sync(&hctx->run_work);
816 }
817
Ming Lei47cdee22019-05-15 11:03:08 +0800818 blk_exit_queue(q);
819
Damien Le Moalbf505452018-10-12 19:08:50 +0900820 blk_queue_free_zone_bitmaps(q);
821
Jens Axboe344e9ff2018-11-15 12:22:51 -0700822 if (queue_is_mq(q))
Ming Leie09aae72015-01-29 20:17:27 +0800823 blk_mq_release(q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700824
Jens Axboe8324aa92008-01-29 14:51:59 +0100825 blk_trace_shutdown(q);
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000826 mutex_lock(&q->debugfs_mutex);
827 debugfs_remove_recursive(q->debugfs_dir);
828 mutex_unlock(&q->debugfs_mutex);
Jens Axboe8324aa92008-01-29 14:51:59 +0100829
Jens Axboe344e9ff2018-11-15 12:22:51 -0700830 if (queue_is_mq(q))
Omar Sandoval62ebce162017-01-31 14:53:21 -0800831 blk_mq_debugfs_unregister(q);
832
Kent Overstreet338aa962018-05-20 18:25:47 -0400833 bioset_exit(&q->bio_split);
Kent Overstreet54efd502015-04-23 22:37:18 -0700834
Tejun Heoa73f7302011-12-14 00:33:37 +0100835 ida_simple_remove(&blk_queue_ida, q->id);
Tejun Heo548bc8e2013-01-09 08:05:13 -0800836 call_rcu(&q->rcu_head, blk_free_queue_rcu);
Jens Axboe8324aa92008-01-29 14:51:59 +0100837}
838
Emese Revfy52cf25d2010-01-19 02:58:23 +0100839static const struct sysfs_ops queue_sysfs_ops = {
Jens Axboe8324aa92008-01-29 14:51:59 +0100840 .show = queue_attr_show,
841 .store = queue_attr_store,
842};
843
844struct kobj_type blk_queue_ktype = {
845 .sysfs_ops = &queue_sysfs_ops,
Jens Axboe8324aa92008-01-29 14:51:59 +0100846 .release = blk_release_queue,
847};
848
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800849/**
850 * blk_register_queue - register a block layer queue with sysfs
851 * @disk: Disk of which the request queue should be registered with sysfs.
852 */
Jens Axboe8324aa92008-01-29 14:51:59 +0100853int blk_register_queue(struct gendisk *disk)
854{
855 int ret;
Li Zefan1d54ad62009-04-14 14:00:05 +0800856 struct device *dev = disk_to_dev(disk);
Jens Axboe8324aa92008-01-29 14:51:59 +0100857 struct request_queue *q = disk->queue;
858
Li Zefan1d54ad62009-04-14 14:00:05 +0800859 ret = blk_trace_init_sysfs(dev);
860 if (ret)
861 return ret;
862
Ming Leicecf5d82019-08-27 19:01:48 +0800863 mutex_lock(&q->sysfs_dir_lock);
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800864
Linus Torvaldsc9059592009-06-11 10:52:27 -0700865 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
Liu Yuaned5302d2011-04-19 13:47:58 +0200866 if (ret < 0) {
867 blk_trace_remove_sysfs(dev);
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800868 goto unlock;
Liu Yuaned5302d2011-04-19 13:47:58 +0200869 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100870
Weiping Zhang4d253392019-04-02 21:14:30 +0800871 ret = sysfs_create_group(&q->kobj, &queue_attr_group);
872 if (ret) {
873 blk_trace_remove_sysfs(dev);
874 kobject_del(&q->kobj);
875 kobject_put(&dev->kobj);
876 goto unlock;
877 }
878
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000879 mutex_lock(&q->debugfs_mutex);
880 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
881 blk_debugfs_root);
882 mutex_unlock(&q->debugfs_mutex);
883
Jens Axboe344e9ff2018-11-15 12:22:51 -0700884 if (queue_is_mq(q)) {
Bart Van Assche2d0364c2017-04-26 13:47:48 -0700885 __blk_mq_register_dev(dev, q);
Bart Van Asschea8ecdd72017-05-25 16:38:06 -0700886 blk_mq_debugfs_register(q);
887 }
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600888
Ming Leib89f6252019-09-23 23:12:09 +0800889 mutex_lock(&q->sysfs_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700890 if (q->elevator) {
Ming Leicecf5d82019-08-27 19:01:48 +0800891 ret = elv_register_queue(q, false);
Omar Sandoval80c6b152017-02-06 12:52:24 -0800892 if (ret) {
Ming Leib89f6252019-09-23 23:12:09 +0800893 mutex_unlock(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800894 mutex_unlock(&q->sysfs_dir_lock);
Omar Sandoval80c6b152017-02-06 12:52:24 -0800895 kobject_del(&q->kobj);
896 blk_trace_remove_sysfs(dev);
897 kobject_put(&dev->kobj);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800898 return ret;
Omar Sandoval80c6b152017-02-06 12:52:24 -0800899 }
Jens Axboe8324aa92008-01-29 14:51:59 +0100900 }
Ming Leicecf5d82019-08-27 19:01:48 +0800901
Ming Leicecf5d82019-08-27 19:01:48 +0800902 blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
903 wbt_enable_default(q);
904 blk_throtl_register_queue(q);
905
906 /* Now everything is ready and send out KOBJ_ADD uevent */
907 kobject_uevent(&q->kobj, KOBJ_ADD);
Yufen Yu05468582020-10-08 23:26:32 -0400908 if (q->elevator)
Ming Leicecf5d82019-08-27 19:01:48 +0800909 kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
910 mutex_unlock(&q->sysfs_lock);
911
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800912 ret = 0;
913unlock:
Ming Leicecf5d82019-08-27 19:01:48 +0800914 mutex_unlock(&q->sysfs_dir_lock);
Ming Leia72c3742021-06-09 09:58:22 +0800915
916 /*
917 * SCSI probing may synchronously create and destroy a lot of
918 * request_queues for non-existent devices. Shutting down a fully
919 * functional queue takes measureable wallclock time as RCU grace
920 * periods are involved. To avoid excessive latency in these
921 * cases, a request_queue starts out in a degraded mode which is
922 * faster to shut down and is made fully functional here as
923 * request_queues for non-existent devices never get registered.
924 */
925 if (!blk_queue_init_done(q)) {
926 blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
927 percpu_ref_switch_to_percpu(&q->q_usage_counter);
928 }
929
Tahsin Erdoganb410aff2017-02-14 19:27:38 -0800930 return ret;
Jens Axboe8324aa92008-01-29 14:51:59 +0100931}
932
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800933/**
934 * blk_unregister_queue - counterpart of blk_register_queue()
935 * @disk: Disk of which the request queue should be unregistered from sysfs.
936 *
937 * Note: the caller is responsible for guaranteeing that this function is called
938 * after blk_register_queue() has finished.
939 */
Jens Axboe8324aa92008-01-29 14:51:59 +0100940void blk_unregister_queue(struct gendisk *disk)
941{
942 struct request_queue *q = disk->queue;
943
Akinobu Mitafb199742008-04-21 09:51:06 +0200944 if (WARN_ON(!q))
945 return;
946
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500947 /* Return early if disk->queue was never registered. */
Ming Lei58c898b2019-08-27 19:01:47 +0800948 if (!blk_queue_registered(q))
Mike Snitzerfa70d2e2018-01-08 22:01:13 -0500949 return;
950
Mike Snitzer667257e2018-01-11 14:11:01 -0500951 /*
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800952 * Since sysfs_remove_dir() prevents adding new directory entries
953 * before removal of existing entries starts, protect against
954 * concurrent elv_iosched_store() calls.
Mike Snitzer667257e2018-01-11 14:11:01 -0500955 */
David Jefferye9a823f2017-08-28 10:52:44 -0600956 mutex_lock(&q->sysfs_lock);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800957 blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
Ming Leicecf5d82019-08-27 19:01:48 +0800958 mutex_unlock(&q->sysfs_lock);
Omar Sandoval334335d2017-03-28 16:12:15 -0700959
Ming Leicecf5d82019-08-27 19:01:48 +0800960 mutex_lock(&q->sysfs_dir_lock);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800961 /*
962 * Remove the sysfs attributes before unregistering the queue data
963 * structures that can be modified through sysfs.
964 */
Jens Axboe344e9ff2018-11-15 12:22:51 -0700965 if (queue_is_mq(q))
Matias Bjørlingb21d5b32016-09-16 14:25:06 +0200966 blk_mq_unregister_dev(disk_to_dev(disk), q);
Jens Axboe8324aa92008-01-29 14:51:59 +0100967
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +0200968 kobject_uevent(&q->kobj, KOBJ_REMOVE);
969 kobject_del(&q->kobj);
970 blk_trace_remove_sysfs(disk_to_dev(disk));
Mike Snitzer667257e2018-01-11 14:11:01 -0500971
Ming Leib89f6252019-09-23 23:12:09 +0800972 mutex_lock(&q->sysfs_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700973 if (q->elevator)
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800974 elv_unregister_queue(q);
Ming Leib89f6252019-09-23 23:12:09 +0800975 mutex_unlock(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800976 mutex_unlock(&q->sysfs_dir_lock);
Bart Van Assche2c2086a2018-01-17 11:48:10 -0800977
978 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100979}