xref: /openbmc/linux/block/blk-sysfs.c (revision 901181b7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions related to sysfs handling
4  */
5 #include <linux/kernel.h>
6 #include <linux/slab.h>
7 #include <linux/module.h>
8 #include <linux/bio.h>
9 #include <linux/blkdev.h>
10 #include <linux/backing-dev.h>
11 #include <linux/blktrace_api.h>
12 #include <linux/blk-mq.h>
13 #include <linux/blk-cgroup.h>
14 #include <linux/debugfs.h>
15 
16 #include "blk.h"
17 #include "blk-mq.h"
18 #include "blk-mq-debugfs.h"
19 #include "blk-wbt.h"
20 #include "blk-throttle.h"
21 
22 struct queue_sysfs_entry {
23 	struct attribute attr;
24 	ssize_t (*show)(struct request_queue *, char *);
25 	ssize_t (*store)(struct request_queue *, const char *, size_t);
26 };
27 
28 static ssize_t
29 queue_var_show(unsigned long var, char *page)
30 {
31 	return sprintf(page, "%lu\n", var);
32 }
33 
34 static ssize_t
35 queue_var_store(unsigned long *var, const char *page, size_t count)
36 {
37 	int err;
38 	unsigned long v;
39 
40 	err = kstrtoul(page, 10, &v);
41 	if (err || v > UINT_MAX)
42 		return -EINVAL;
43 
44 	*var = v;
45 
46 	return count;
47 }
48 
49 static ssize_t queue_var_store64(s64 *var, const char *page)
50 {
51 	int err;
52 	s64 v;
53 
54 	err = kstrtos64(page, 10, &v);
55 	if (err < 0)
56 		return err;
57 
58 	*var = v;
59 	return 0;
60 }
61 
62 static ssize_t queue_requests_show(struct request_queue *q, char *page)
63 {
64 	return queue_var_show(q->nr_requests, page);
65 }
66 
67 static ssize_t
68 queue_requests_store(struct request_queue *q, const char *page, size_t count)
69 {
70 	unsigned long nr;
71 	int ret, err;
72 
73 	if (!queue_is_mq(q))
74 		return -EINVAL;
75 
76 	ret = queue_var_store(&nr, page, count);
77 	if (ret < 0)
78 		return ret;
79 
80 	if (nr < BLKDEV_MIN_RQ)
81 		nr = BLKDEV_MIN_RQ;
82 
83 	err = blk_mq_update_nr_requests(q, nr);
84 	if (err)
85 		return err;
86 
87 	return ret;
88 }
89 
90 static ssize_t queue_ra_show(struct request_queue *q, char *page)
91 {
92 	unsigned long ra_kb;
93 
94 	if (!q->disk)
95 		return -EINVAL;
96 	ra_kb = q->disk->bdi->ra_pages << (PAGE_SHIFT - 10);
97 	return queue_var_show(ra_kb, page);
98 }
99 
100 static ssize_t
101 queue_ra_store(struct request_queue *q, const char *page, size_t count)
102 {
103 	unsigned long ra_kb;
104 	ssize_t ret;
105 
106 	if (!q->disk)
107 		return -EINVAL;
108 	ret = queue_var_store(&ra_kb, page, count);
109 	if (ret < 0)
110 		return ret;
111 	q->disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
112 	return ret;
113 }
114 
115 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
116 {
117 	int max_sectors_kb = queue_max_sectors(q) >> 1;
118 
119 	return queue_var_show(max_sectors_kb, page);
120 }
121 
122 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
123 {
124 	return queue_var_show(queue_max_segments(q), page);
125 }
126 
127 static ssize_t queue_max_discard_segments_show(struct request_queue *q,
128 		char *page)
129 {
130 	return queue_var_show(queue_max_discard_segments(q), page);
131 }
132 
133 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
134 {
135 	return queue_var_show(q->limits.max_integrity_segments, page);
136 }
137 
138 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
139 {
140 	return queue_var_show(queue_max_segment_size(q), page);
141 }
142 
143 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
144 {
145 	return queue_var_show(queue_logical_block_size(q), page);
146 }
147 
148 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
149 {
150 	return queue_var_show(queue_physical_block_size(q), page);
151 }
152 
153 static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
154 {
155 	return queue_var_show(q->limits.chunk_sectors, page);
156 }
157 
158 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
159 {
160 	return queue_var_show(queue_io_min(q), page);
161 }
162 
163 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
164 {
165 	return queue_var_show(queue_io_opt(q), page);
166 }
167 
168 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
169 {
170 	return queue_var_show(q->limits.discard_granularity, page);
171 }
172 
173 static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
174 {
175 
176 	return sprintf(page, "%llu\n",
177 		(unsigned long long)q->limits.max_hw_discard_sectors << 9);
178 }
179 
180 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
181 {
182 	return sprintf(page, "%llu\n",
183 		       (unsigned long long)q->limits.max_discard_sectors << 9);
184 }
185 
186 static ssize_t queue_discard_max_store(struct request_queue *q,
187 				       const char *page, size_t count)
188 {
189 	unsigned long max_discard;
190 	ssize_t ret = queue_var_store(&max_discard, page, count);
191 
192 	if (ret < 0)
193 		return ret;
194 
195 	if (max_discard & (q->limits.discard_granularity - 1))
196 		return -EINVAL;
197 
198 	max_discard >>= 9;
199 	if (max_discard > UINT_MAX)
200 		return -EINVAL;
201 
202 	if (max_discard > q->limits.max_hw_discard_sectors)
203 		max_discard = q->limits.max_hw_discard_sectors;
204 
205 	q->limits.max_discard_sectors = max_discard;
206 	return ret;
207 }
208 
209 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
210 {
211 	return queue_var_show(0, page);
212 }
213 
214 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
215 {
216 	return sprintf(page, "%llu\n",
217 		(unsigned long long)q->limits.max_write_same_sectors << 9);
218 }
219 
220 static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
221 {
222 	return sprintf(page, "%llu\n",
223 		(unsigned long long)q->limits.max_write_zeroes_sectors << 9);
224 }
225 
226 static ssize_t queue_zone_write_granularity_show(struct request_queue *q,
227 						 char *page)
228 {
229 	return queue_var_show(queue_zone_write_granularity(q), page);
230 }
231 
232 static ssize_t queue_zone_append_max_show(struct request_queue *q, char *page)
233 {
234 	unsigned long long max_sectors = q->limits.max_zone_append_sectors;
235 
236 	return sprintf(page, "%llu\n", max_sectors << SECTOR_SHIFT);
237 }
238 
239 static ssize_t
240 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
241 {
242 	unsigned long max_sectors_kb,
243 		max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
244 			page_kb = 1 << (PAGE_SHIFT - 10);
245 	ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
246 
247 	if (ret < 0)
248 		return ret;
249 
250 	max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
251 					 q->limits.max_dev_sectors >> 1);
252 
253 	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
254 		return -EINVAL;
255 
256 	spin_lock_irq(&q->queue_lock);
257 	q->limits.max_sectors = max_sectors_kb << 1;
258 	if (q->disk)
259 		q->disk->bdi->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
260 	spin_unlock_irq(&q->queue_lock);
261 
262 	return ret;
263 }
264 
265 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
266 {
267 	int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
268 
269 	return queue_var_show(max_hw_sectors_kb, page);
270 }
271 
272 static ssize_t queue_virt_boundary_mask_show(struct request_queue *q, char *page)
273 {
274 	return queue_var_show(q->limits.virt_boundary_mask, page);
275 }
276 
277 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)				\
278 static ssize_t								\
279 queue_##name##_show(struct request_queue *q, char *page)		\
280 {									\
281 	int bit;							\
282 	bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);		\
283 	return queue_var_show(neg ? !bit : bit, page);			\
284 }									\
285 static ssize_t								\
286 queue_##name##_store(struct request_queue *q, const char *page, size_t count) \
287 {									\
288 	unsigned long val;						\
289 	ssize_t ret;							\
290 	ret = queue_var_store(&val, page, count);			\
291 	if (ret < 0)							\
292 		 return ret;						\
293 	if (neg)							\
294 		val = !val;						\
295 									\
296 	if (val)							\
297 		blk_queue_flag_set(QUEUE_FLAG_##flag, q);		\
298 	else								\
299 		blk_queue_flag_clear(QUEUE_FLAG_##flag, q);		\
300 	return ret;							\
301 }
302 
303 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
304 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
305 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
306 QUEUE_SYSFS_BIT_FNS(stable_writes, STABLE_WRITES, 0);
307 #undef QUEUE_SYSFS_BIT_FNS
308 
309 static 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 
321 static 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 
326 static 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 
331 static 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 
336 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
337 {
338 	return queue_var_show((blk_queue_nomerges(q) << 1) |
339 			       blk_queue_noxmerges(q), page);
340 }
341 
342 static 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 
348 	if (ret < 0)
349 		return ret;
350 
351 	blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
352 	blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
353 	if (nm == 2)
354 		blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
355 	else if (nm)
356 		blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
357 
358 	return ret;
359 }
360 
361 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
362 {
363 	bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
364 	bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
365 
366 	return queue_var_show(set << force, page);
367 }
368 
369 static ssize_t
370 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
371 {
372 	ssize_t ret = -EINVAL;
373 #ifdef CONFIG_SMP
374 	unsigned long val;
375 
376 	ret = queue_var_store(&val, page, count);
377 	if (ret < 0)
378 		return ret;
379 
380 	if (val == 2) {
381 		blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
382 		blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
383 	} else if (val == 1) {
384 		blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
385 		blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
386 	} else if (val == 0) {
387 		blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
388 		blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
389 	}
390 #endif
391 	return ret;
392 }
393 
394 static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
395 {
396 	int val;
397 
398 	if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
399 		val = BLK_MQ_POLL_CLASSIC;
400 	else
401 		val = q->poll_nsec / 1000;
402 
403 	return sprintf(page, "%d\n", val);
404 }
405 
406 static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
407 				size_t count)
408 {
409 	int err, val;
410 
411 	if (!q->mq_ops || !q->mq_ops->poll)
412 		return -EINVAL;
413 
414 	err = kstrtoint(page, 10, &val);
415 	if (err < 0)
416 		return err;
417 
418 	if (val == BLK_MQ_POLL_CLASSIC)
419 		q->poll_nsec = BLK_MQ_POLL_CLASSIC;
420 	else if (val >= 0)
421 		q->poll_nsec = val * 1000;
422 	else
423 		return -EINVAL;
424 
425 	return count;
426 }
427 
428 static 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 
433 static ssize_t queue_poll_store(struct request_queue *q, const char *page,
434 				size_t count)
435 {
436 	if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
437 		return -EINVAL;
438 	pr_info_ratelimited("writes to the poll attribute are ignored.\n");
439 	pr_info_ratelimited("please use driver specific parameters instead.\n");
440 	return count;
441 }
442 
443 static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
444 {
445 	return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
446 }
447 
448 static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
449 				  size_t count)
450 {
451 	unsigned int val;
452 	int err;
453 
454 	err = kstrtou32(page, 10, &val);
455 	if (err || val == 0)
456 		return -EINVAL;
457 
458 	blk_queue_rq_timeout(q, msecs_to_jiffies(val));
459 
460 	return count;
461 }
462 
463 static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
464 {
465 	if (!wbt_rq_qos(q))
466 		return -EINVAL;
467 
468 	return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
469 }
470 
471 static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
472 				  size_t count)
473 {
474 	struct rq_qos *rqos;
475 	ssize_t ret;
476 	s64 val;
477 
478 	ret = queue_var_store64(&val, page);
479 	if (ret < 0)
480 		return ret;
481 	if (val < -1)
482 		return -EINVAL;
483 
484 	rqos = wbt_rq_qos(q);
485 	if (!rqos) {
486 		ret = wbt_init(q);
487 		if (ret)
488 			return ret;
489 	}
490 
491 	if (val == -1)
492 		val = wbt_default_latency_nsec(q);
493 	else if (val >= 0)
494 		val *= 1000ULL;
495 
496 	if (wbt_get_min_lat(q) == val)
497 		return count;
498 
499 	/*
500 	 * Ensure that the queue is idled, in case the latency update
501 	 * ends up either enabling or disabling wbt completely. We can't
502 	 * have IO inflight if that happens.
503 	 */
504 	blk_mq_freeze_queue(q);
505 	blk_mq_quiesce_queue(q);
506 
507 	wbt_set_min_lat(q, val);
508 
509 	blk_mq_unquiesce_queue(q);
510 	blk_mq_unfreeze_queue(q);
511 
512 	return count;
513 }
514 
515 static ssize_t queue_wc_show(struct request_queue *q, char *page)
516 {
517 	if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
518 		return sprintf(page, "write back\n");
519 
520 	return sprintf(page, "write through\n");
521 }
522 
523 static ssize_t queue_wc_store(struct request_queue *q, const char *page,
524 			      size_t count)
525 {
526 	int set = -1;
527 
528 	if (!strncmp(page, "write back", 10))
529 		set = 1;
530 	else if (!strncmp(page, "write through", 13) ||
531 		 !strncmp(page, "none", 4))
532 		set = 0;
533 
534 	if (set == -1)
535 		return -EINVAL;
536 
537 	if (set)
538 		blk_queue_flag_set(QUEUE_FLAG_WC, q);
539 	else
540 		blk_queue_flag_clear(QUEUE_FLAG_WC, q);
541 
542 	return count;
543 }
544 
545 static ssize_t queue_fua_show(struct request_queue *q, char *page)
546 {
547 	return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
548 }
549 
550 static ssize_t queue_dax_show(struct request_queue *q, char *page)
551 {
552 	return queue_var_show(blk_queue_dax(q), page);
553 }
554 
555 #define QUEUE_RO_ENTRY(_prefix, _name)			\
556 static struct queue_sysfs_entry _prefix##_entry = {	\
557 	.attr	= { .name = _name, .mode = 0444 },	\
558 	.show	= _prefix##_show,			\
559 };
560 
561 #define QUEUE_RW_ENTRY(_prefix, _name)			\
562 static struct queue_sysfs_entry _prefix##_entry = {	\
563 	.attr	= { .name = _name, .mode = 0644 },	\
564 	.show	= _prefix##_show,			\
565 	.store	= _prefix##_store,			\
566 };
567 
568 QUEUE_RW_ENTRY(queue_requests, "nr_requests");
569 QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
570 QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
571 QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
572 QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
573 QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
574 QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
575 QUEUE_RW_ENTRY(elv_iosched, "scheduler");
576 
577 QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
578 QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
579 QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
580 QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size");
581 QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size");
582 
583 QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
584 QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity");
585 QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes");
586 QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes");
587 QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
588 
589 QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
590 QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes");
591 QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes");
592 QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
593 
594 QUEUE_RO_ENTRY(queue_zoned, "zoned");
595 QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
596 QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones");
597 QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones");
598 
599 QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
600 QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
601 QUEUE_RW_ENTRY(queue_poll, "io_poll");
602 QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
603 QUEUE_RW_ENTRY(queue_wc, "write_cache");
604 QUEUE_RO_ENTRY(queue_fua, "fua");
605 QUEUE_RO_ENTRY(queue_dax, "dax");
606 QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
607 QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
608 QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
609 
610 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
611 QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time");
612 #endif
613 
614 /* legacy alias for logical_block_size: */
615 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
616 	.attr = {.name = "hw_sector_size", .mode = 0444 },
617 	.show = queue_logical_block_size_show,
618 };
619 
620 QUEUE_RW_ENTRY(queue_nonrot, "rotational");
621 QUEUE_RW_ENTRY(queue_iostats, "iostats");
622 QUEUE_RW_ENTRY(queue_random, "add_random");
623 QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes");
624 
625 static struct attribute *queue_attrs[] = {
626 	&queue_requests_entry.attr,
627 	&queue_ra_entry.attr,
628 	&queue_max_hw_sectors_entry.attr,
629 	&queue_max_sectors_entry.attr,
630 	&queue_max_segments_entry.attr,
631 	&queue_max_discard_segments_entry.attr,
632 	&queue_max_integrity_segments_entry.attr,
633 	&queue_max_segment_size_entry.attr,
634 	&elv_iosched_entry.attr,
635 	&queue_hw_sector_size_entry.attr,
636 	&queue_logical_block_size_entry.attr,
637 	&queue_physical_block_size_entry.attr,
638 	&queue_chunk_sectors_entry.attr,
639 	&queue_io_min_entry.attr,
640 	&queue_io_opt_entry.attr,
641 	&queue_discard_granularity_entry.attr,
642 	&queue_discard_max_entry.attr,
643 	&queue_discard_max_hw_entry.attr,
644 	&queue_discard_zeroes_data_entry.attr,
645 	&queue_write_same_max_entry.attr,
646 	&queue_write_zeroes_max_entry.attr,
647 	&queue_zone_append_max_entry.attr,
648 	&queue_zone_write_granularity_entry.attr,
649 	&queue_nonrot_entry.attr,
650 	&queue_zoned_entry.attr,
651 	&queue_nr_zones_entry.attr,
652 	&queue_max_open_zones_entry.attr,
653 	&queue_max_active_zones_entry.attr,
654 	&queue_nomerges_entry.attr,
655 	&queue_rq_affinity_entry.attr,
656 	&queue_iostats_entry.attr,
657 	&queue_stable_writes_entry.attr,
658 	&queue_random_entry.attr,
659 	&queue_poll_entry.attr,
660 	&queue_wc_entry.attr,
661 	&queue_fua_entry.attr,
662 	&queue_dax_entry.attr,
663 	&queue_wb_lat_entry.attr,
664 	&queue_poll_delay_entry.attr,
665 	&queue_io_timeout_entry.attr,
666 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
667 	&blk_throtl_sample_time_entry.attr,
668 #endif
669 	&queue_virt_boundary_mask_entry.attr,
670 	NULL,
671 };
672 
673 static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
674 				int n)
675 {
676 	struct request_queue *q =
677 		container_of(kobj, struct request_queue, kobj);
678 
679 	if (attr == &queue_io_timeout_entry.attr &&
680 		(!q->mq_ops || !q->mq_ops->timeout))
681 			return 0;
682 
683 	if ((attr == &queue_max_open_zones_entry.attr ||
684 	     attr == &queue_max_active_zones_entry.attr) &&
685 	    !blk_queue_is_zoned(q))
686 		return 0;
687 
688 	return attr->mode;
689 }
690 
691 static struct attribute_group queue_attr_group = {
692 	.attrs = queue_attrs,
693 	.is_visible = queue_attr_visible,
694 };
695 
696 
697 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
698 
699 static ssize_t
700 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
701 {
702 	struct queue_sysfs_entry *entry = to_queue(attr);
703 	struct request_queue *q =
704 		container_of(kobj, struct request_queue, kobj);
705 	ssize_t res;
706 
707 	if (!entry->show)
708 		return -EIO;
709 	mutex_lock(&q->sysfs_lock);
710 	res = entry->show(q, page);
711 	mutex_unlock(&q->sysfs_lock);
712 	return res;
713 }
714 
715 static ssize_t
716 queue_attr_store(struct kobject *kobj, struct attribute *attr,
717 		    const char *page, size_t length)
718 {
719 	struct queue_sysfs_entry *entry = to_queue(attr);
720 	struct request_queue *q;
721 	ssize_t res;
722 
723 	if (!entry->store)
724 		return -EIO;
725 
726 	q = container_of(kobj, struct request_queue, kobj);
727 	mutex_lock(&q->sysfs_lock);
728 	res = entry->store(q, page, length);
729 	mutex_unlock(&q->sysfs_lock);
730 	return res;
731 }
732 
733 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
734 {
735 	struct request_queue *q = container_of(rcu_head, struct request_queue,
736 					       rcu_head);
737 	kmem_cache_free(blk_requestq_cachep, q);
738 }
739 
740 /* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
741 static void blk_exit_queue(struct request_queue *q)
742 {
743 	/*
744 	 * Since the I/O scheduler exit code may access cgroup information,
745 	 * perform I/O scheduler exit before disassociating from the block
746 	 * cgroup controller.
747 	 */
748 	if (q->elevator) {
749 		ioc_clear_queue(q);
750 		__elevator_exit(q, q->elevator);
751 	}
752 
753 	/*
754 	 * Remove all references to @q from the block cgroup controller before
755 	 * restoring @q->queue_lock to avoid that restoring this pointer causes
756 	 * e.g. blkcg_print_blkgs() to crash.
757 	 */
758 	blkcg_exit_queue(q);
759 }
760 
761 /**
762  * blk_release_queue - releases all allocated resources of the request_queue
763  * @kobj: pointer to a kobject, whose container is a request_queue
764  *
765  * This function releases all allocated resources of the request queue.
766  *
767  * The struct request_queue refcount is incremented with blk_get_queue() and
768  * decremented with blk_put_queue(). Once the refcount reaches 0 this function
769  * is called.
770  *
771  * For drivers that have a request_queue on a gendisk and added with
772  * __device_add_disk() the refcount to request_queue will reach 0 with
773  * the last put_disk() called by the driver. For drivers which don't use
774  * __device_add_disk() this happens with blk_cleanup_queue().
775  *
776  * Drivers exist which depend on the release of the request_queue to be
777  * synchronous, it should not be deferred.
778  *
779  * Context: can sleep
780  */
781 static void blk_release_queue(struct kobject *kobj)
782 {
783 	struct request_queue *q =
784 		container_of(kobj, struct request_queue, kobj);
785 
786 	might_sleep();
787 
788 	if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
789 		blk_stat_remove_callback(q, q->poll_cb);
790 	blk_stat_free_callback(q->poll_cb);
791 
792 	blk_free_queue_stats(q->stats);
793 
794 	if (queue_is_mq(q)) {
795 		struct blk_mq_hw_ctx *hctx;
796 		int i;
797 
798 		cancel_delayed_work_sync(&q->requeue_work);
799 
800 		queue_for_each_hw_ctx(q, hctx, i)
801 			cancel_delayed_work_sync(&hctx->run_work);
802 	}
803 
804 	blk_exit_queue(q);
805 
806 	blk_queue_free_zone_bitmaps(q);
807 
808 	if (queue_is_mq(q))
809 		blk_mq_release(q);
810 
811 	blk_trace_shutdown(q);
812 	mutex_lock(&q->debugfs_mutex);
813 	debugfs_remove_recursive(q->debugfs_dir);
814 	mutex_unlock(&q->debugfs_mutex);
815 
816 	if (queue_is_mq(q))
817 		blk_mq_debugfs_unregister(q);
818 
819 	bioset_exit(&q->bio_split);
820 
821 	ida_simple_remove(&blk_queue_ida, q->id);
822 	call_rcu(&q->rcu_head, blk_free_queue_rcu);
823 }
824 
825 static const struct sysfs_ops queue_sysfs_ops = {
826 	.show	= queue_attr_show,
827 	.store	= queue_attr_store,
828 };
829 
830 struct kobj_type blk_queue_ktype = {
831 	.sysfs_ops	= &queue_sysfs_ops,
832 	.release	= blk_release_queue,
833 };
834 
835 /**
836  * blk_register_queue - register a block layer queue with sysfs
837  * @disk: Disk of which the request queue should be registered with sysfs.
838  */
839 int blk_register_queue(struct gendisk *disk)
840 {
841 	int ret;
842 	struct device *dev = disk_to_dev(disk);
843 	struct request_queue *q = disk->queue;
844 
845 	ret = blk_trace_init_sysfs(dev);
846 	if (ret)
847 		return ret;
848 
849 	mutex_lock(&q->sysfs_dir_lock);
850 
851 	ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
852 	if (ret < 0) {
853 		blk_trace_remove_sysfs(dev);
854 		goto unlock;
855 	}
856 
857 	ret = sysfs_create_group(&q->kobj, &queue_attr_group);
858 	if (ret) {
859 		blk_trace_remove_sysfs(dev);
860 		kobject_del(&q->kobj);
861 		kobject_put(&dev->kobj);
862 		goto unlock;
863 	}
864 
865 	mutex_lock(&q->debugfs_mutex);
866 	q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
867 					    blk_debugfs_root);
868 	mutex_unlock(&q->debugfs_mutex);
869 
870 	if (queue_is_mq(q)) {
871 		__blk_mq_register_dev(dev, q);
872 		blk_mq_debugfs_register(q);
873 	}
874 
875 	mutex_lock(&q->sysfs_lock);
876 
877 	ret = disk_register_independent_access_ranges(disk, NULL);
878 	if (ret)
879 		goto put_dev;
880 
881 	if (q->elevator) {
882 		ret = elv_register_queue(q, false);
883 		if (ret)
884 			goto put_dev;
885 	}
886 
887 	blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
888 	wbt_enable_default(q);
889 	blk_throtl_register_queue(q);
890 
891 	/* Now everything is ready and send out KOBJ_ADD uevent */
892 	kobject_uevent(&q->kobj, KOBJ_ADD);
893 	if (q->elevator)
894 		kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
895 	mutex_unlock(&q->sysfs_lock);
896 
897 	ret = 0;
898 unlock:
899 	mutex_unlock(&q->sysfs_dir_lock);
900 
901 	/*
902 	 * SCSI probing may synchronously create and destroy a lot of
903 	 * request_queues for non-existent devices.  Shutting down a fully
904 	 * functional queue takes measureable wallclock time as RCU grace
905 	 * periods are involved.  To avoid excessive latency in these
906 	 * cases, a request_queue starts out in a degraded mode which is
907 	 * faster to shut down and is made fully functional here as
908 	 * request_queues for non-existent devices never get registered.
909 	 */
910 	if (!blk_queue_init_done(q)) {
911 		blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
912 		percpu_ref_switch_to_percpu(&q->q_usage_counter);
913 	}
914 
915 	return ret;
916 
917 put_dev:
918 	disk_unregister_independent_access_ranges(disk);
919 	mutex_unlock(&q->sysfs_lock);
920 	mutex_unlock(&q->sysfs_dir_lock);
921 	kobject_del(&q->kobj);
922 	blk_trace_remove_sysfs(dev);
923 	kobject_put(&dev->kobj);
924 
925 	return ret;
926 }
927 
928 /**
929  * blk_unregister_queue - counterpart of blk_register_queue()
930  * @disk: Disk of which the request queue should be unregistered from sysfs.
931  *
932  * Note: the caller is responsible for guaranteeing that this function is called
933  * after blk_register_queue() has finished.
934  */
935 void blk_unregister_queue(struct gendisk *disk)
936 {
937 	struct request_queue *q = disk->queue;
938 
939 	if (WARN_ON(!q))
940 		return;
941 
942 	/* Return early if disk->queue was never registered. */
943 	if (!blk_queue_registered(q))
944 		return;
945 
946 	/*
947 	 * Since sysfs_remove_dir() prevents adding new directory entries
948 	 * before removal of existing entries starts, protect against
949 	 * concurrent elv_iosched_store() calls.
950 	 */
951 	mutex_lock(&q->sysfs_lock);
952 	blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
953 	mutex_unlock(&q->sysfs_lock);
954 
955 	mutex_lock(&q->sysfs_dir_lock);
956 	/*
957 	 * Remove the sysfs attributes before unregistering the queue data
958 	 * structures that can be modified through sysfs.
959 	 */
960 	if (queue_is_mq(q))
961 		blk_mq_unregister_dev(disk_to_dev(disk), q);
962 
963 	kobject_uevent(&q->kobj, KOBJ_REMOVE);
964 	kobject_del(&q->kobj);
965 	blk_trace_remove_sysfs(disk_to_dev(disk));
966 
967 	mutex_lock(&q->sysfs_lock);
968 	if (q->elevator)
969 		elv_unregister_queue(q);
970 	disk_unregister_independent_access_ranges(disk);
971 	mutex_unlock(&q->sysfs_lock);
972 	mutex_unlock(&q->sysfs_dir_lock);
973 
974 	kobject_put(&disk_to_dev(disk)->kobj);
975 }
976