xref: /openbmc/linux/block/blk-sysfs.c (revision 2455f0e1)
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/debugfs.h>
14 
15 #include "blk.h"
16 #include "blk-mq.h"
17 #include "blk-mq-debugfs.h"
18 #include "blk-mq-sched.h"
19 #include "blk-wbt.h"
20 #include "blk-cgroup.h"
21 #include "blk-throttle.h"
22 
23 struct queue_sysfs_entry {
24 	struct attribute attr;
25 	ssize_t (*show)(struct request_queue *, char *);
26 	ssize_t (*store)(struct request_queue *, const char *, size_t);
27 };
28 
29 static ssize_t
30 queue_var_show(unsigned long var, char *page)
31 {
32 	return sprintf(page, "%lu\n", var);
33 }
34 
35 static ssize_t
36 queue_var_store(unsigned long *var, const char *page, size_t count)
37 {
38 	int err;
39 	unsigned long v;
40 
41 	err = kstrtoul(page, 10, &v);
42 	if (err || v > UINT_MAX)
43 		return -EINVAL;
44 
45 	*var = v;
46 
47 	return count;
48 }
49 
50 static ssize_t queue_var_store64(s64 *var, const char *page)
51 {
52 	int err;
53 	s64 v;
54 
55 	err = kstrtos64(page, 10, &v);
56 	if (err < 0)
57 		return err;
58 
59 	*var = v;
60 	return 0;
61 }
62 
63 static ssize_t queue_requests_show(struct request_queue *q, char *page)
64 {
65 	return queue_var_show(q->nr_requests, page);
66 }
67 
68 static ssize_t
69 queue_requests_store(struct request_queue *q, const char *page, size_t count)
70 {
71 	unsigned long nr;
72 	int ret, err;
73 
74 	if (!queue_is_mq(q))
75 		return -EINVAL;
76 
77 	ret = queue_var_store(&nr, page, count);
78 	if (ret < 0)
79 		return ret;
80 
81 	if (nr < BLKDEV_MIN_RQ)
82 		nr = BLKDEV_MIN_RQ;
83 
84 	err = blk_mq_update_nr_requests(q, nr);
85 	if (err)
86 		return err;
87 
88 	return ret;
89 }
90 
91 static ssize_t queue_ra_show(struct request_queue *q, char *page)
92 {
93 	unsigned long ra_kb;
94 
95 	if (!q->disk)
96 		return -EINVAL;
97 	ra_kb = q->disk->bdi->ra_pages << (PAGE_SHIFT - 10);
98 	return queue_var_show(ra_kb, page);
99 }
100 
101 static ssize_t
102 queue_ra_store(struct request_queue *q, const char *page, size_t count)
103 {
104 	unsigned long ra_kb;
105 	ssize_t ret;
106 
107 	if (!q->disk)
108 		return -EINVAL;
109 	ret = queue_var_store(&ra_kb, page, count);
110 	if (ret < 0)
111 		return ret;
112 	q->disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
113 	return ret;
114 }
115 
116 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
117 {
118 	int max_sectors_kb = queue_max_sectors(q) >> 1;
119 
120 	return queue_var_show(max_sectors_kb, page);
121 }
122 
123 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
124 {
125 	return queue_var_show(queue_max_segments(q), page);
126 }
127 
128 static ssize_t queue_max_discard_segments_show(struct request_queue *q,
129 		char *page)
130 {
131 	return queue_var_show(queue_max_discard_segments(q), page);
132 }
133 
134 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
135 {
136 	return queue_var_show(q->limits.max_integrity_segments, page);
137 }
138 
139 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
140 {
141 	return queue_var_show(queue_max_segment_size(q), page);
142 }
143 
144 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
145 {
146 	return queue_var_show(queue_logical_block_size(q), page);
147 }
148 
149 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
150 {
151 	return queue_var_show(queue_physical_block_size(q), page);
152 }
153 
154 static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
155 {
156 	return queue_var_show(q->limits.chunk_sectors, page);
157 }
158 
159 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
160 {
161 	return queue_var_show(queue_io_min(q), page);
162 }
163 
164 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
165 {
166 	return queue_var_show(queue_io_opt(q), page);
167 }
168 
169 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
170 {
171 	return queue_var_show(q->limits.discard_granularity, page);
172 }
173 
174 static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
175 {
176 
177 	return sprintf(page, "%llu\n",
178 		(unsigned long long)q->limits.max_hw_discard_sectors << 9);
179 }
180 
181 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
182 {
183 	return sprintf(page, "%llu\n",
184 		       (unsigned long long)q->limits.max_discard_sectors << 9);
185 }
186 
187 static ssize_t queue_discard_max_store(struct request_queue *q,
188 				       const char *page, size_t count)
189 {
190 	unsigned long max_discard;
191 	ssize_t ret = queue_var_store(&max_discard, page, count);
192 
193 	if (ret < 0)
194 		return ret;
195 
196 	if (max_discard & (q->limits.discard_granularity - 1))
197 		return -EINVAL;
198 
199 	max_discard >>= 9;
200 	if (max_discard > UINT_MAX)
201 		return -EINVAL;
202 
203 	if (max_discard > q->limits.max_hw_discard_sectors)
204 		max_discard = q->limits.max_hw_discard_sectors;
205 
206 	q->limits.max_discard_sectors = max_discard;
207 	return ret;
208 }
209 
210 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
211 {
212 	return queue_var_show(0, page);
213 }
214 
215 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
216 {
217 	return queue_var_show(0, page);
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 static ssize_t queue_dma_alignment_show(struct request_queue *q, char *page)
278 {
279 	return queue_var_show(queue_dma_alignment(q), page);
280 }
281 
282 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)				\
283 static ssize_t								\
284 queue_##name##_show(struct request_queue *q, char *page)		\
285 {									\
286 	int bit;							\
287 	bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);		\
288 	return queue_var_show(neg ? !bit : bit, page);			\
289 }									\
290 static ssize_t								\
291 queue_##name##_store(struct request_queue *q, const char *page, size_t count) \
292 {									\
293 	unsigned long val;						\
294 	ssize_t ret;							\
295 	ret = queue_var_store(&val, page, count);			\
296 	if (ret < 0)							\
297 		 return ret;						\
298 	if (neg)							\
299 		val = !val;						\
300 									\
301 	if (val)							\
302 		blk_queue_flag_set(QUEUE_FLAG_##flag, q);		\
303 	else								\
304 		blk_queue_flag_clear(QUEUE_FLAG_##flag, q);		\
305 	return ret;							\
306 }
307 
308 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
309 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
310 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
311 QUEUE_SYSFS_BIT_FNS(stable_writes, STABLE_WRITES, 0);
312 #undef QUEUE_SYSFS_BIT_FNS
313 
314 static ssize_t queue_zoned_show(struct request_queue *q, char *page)
315 {
316 	switch (blk_queue_zoned_model(q)) {
317 	case BLK_ZONED_HA:
318 		return sprintf(page, "host-aware\n");
319 	case BLK_ZONED_HM:
320 		return sprintf(page, "host-managed\n");
321 	default:
322 		return sprintf(page, "none\n");
323 	}
324 }
325 
326 static ssize_t queue_nr_zones_show(struct request_queue *q, char *page)
327 {
328 	return queue_var_show(disk_nr_zones(q->disk), page);
329 }
330 
331 static ssize_t queue_max_open_zones_show(struct request_queue *q, char *page)
332 {
333 	return queue_var_show(bdev_max_open_zones(q->disk->part0), page);
334 }
335 
336 static ssize_t queue_max_active_zones_show(struct request_queue *q, char *page)
337 {
338 	return queue_var_show(bdev_max_active_zones(q->disk->part0), page);
339 }
340 
341 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
342 {
343 	return queue_var_show((blk_queue_nomerges(q) << 1) |
344 			       blk_queue_noxmerges(q), page);
345 }
346 
347 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
348 				    size_t count)
349 {
350 	unsigned long nm;
351 	ssize_t ret = queue_var_store(&nm, page, count);
352 
353 	if (ret < 0)
354 		return ret;
355 
356 	blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
357 	blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
358 	if (nm == 2)
359 		blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
360 	else if (nm)
361 		blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
362 
363 	return ret;
364 }
365 
366 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
367 {
368 	bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
369 	bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
370 
371 	return queue_var_show(set << force, page);
372 }
373 
374 static ssize_t
375 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
376 {
377 	ssize_t ret = -EINVAL;
378 #ifdef CONFIG_SMP
379 	unsigned long val;
380 
381 	ret = queue_var_store(&val, page, count);
382 	if (ret < 0)
383 		return ret;
384 
385 	if (val == 2) {
386 		blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
387 		blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
388 	} else if (val == 1) {
389 		blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
390 		blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
391 	} else if (val == 0) {
392 		blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
393 		blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
394 	}
395 #endif
396 	return ret;
397 }
398 
399 static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
400 {
401 	int val;
402 
403 	if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
404 		val = BLK_MQ_POLL_CLASSIC;
405 	else
406 		val = q->poll_nsec / 1000;
407 
408 	return sprintf(page, "%d\n", val);
409 }
410 
411 static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
412 				size_t count)
413 {
414 	int err, val;
415 
416 	if (!q->mq_ops || !q->mq_ops->poll)
417 		return -EINVAL;
418 
419 	err = kstrtoint(page, 10, &val);
420 	if (err < 0)
421 		return err;
422 
423 	if (val == BLK_MQ_POLL_CLASSIC)
424 		q->poll_nsec = BLK_MQ_POLL_CLASSIC;
425 	else if (val >= 0)
426 		q->poll_nsec = val * 1000;
427 	else
428 		return -EINVAL;
429 
430 	return count;
431 }
432 
433 static ssize_t queue_poll_show(struct request_queue *q, char *page)
434 {
435 	return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
436 }
437 
438 static ssize_t queue_poll_store(struct request_queue *q, const char *page,
439 				size_t count)
440 {
441 	if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
442 		return -EINVAL;
443 	pr_info_ratelimited("writes to the poll attribute are ignored.\n");
444 	pr_info_ratelimited("please use driver specific parameters instead.\n");
445 	return count;
446 }
447 
448 static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
449 {
450 	return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
451 }
452 
453 static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
454 				  size_t count)
455 {
456 	unsigned int val;
457 	int err;
458 
459 	err = kstrtou32(page, 10, &val);
460 	if (err || val == 0)
461 		return -EINVAL;
462 
463 	blk_queue_rq_timeout(q, msecs_to_jiffies(val));
464 
465 	return count;
466 }
467 
468 static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
469 {
470 	if (!wbt_rq_qos(q))
471 		return -EINVAL;
472 
473 	if (wbt_disabled(q))
474 		return sprintf(page, "0\n");
475 
476 	return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
477 }
478 
479 static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
480 				  size_t count)
481 {
482 	struct rq_qos *rqos;
483 	ssize_t ret;
484 	s64 val;
485 
486 	ret = queue_var_store64(&val, page);
487 	if (ret < 0)
488 		return ret;
489 	if (val < -1)
490 		return -EINVAL;
491 
492 	rqos = wbt_rq_qos(q);
493 	if (!rqos) {
494 		ret = wbt_init(q);
495 		if (ret)
496 			return ret;
497 	}
498 
499 	if (val == -1)
500 		val = wbt_default_latency_nsec(q);
501 	else if (val >= 0)
502 		val *= 1000ULL;
503 
504 	if (wbt_get_min_lat(q) == val)
505 		return count;
506 
507 	/*
508 	 * Ensure that the queue is idled, in case the latency update
509 	 * ends up either enabling or disabling wbt completely. We can't
510 	 * have IO inflight if that happens.
511 	 */
512 	blk_mq_freeze_queue(q);
513 	blk_mq_quiesce_queue(q);
514 
515 	wbt_set_min_lat(q, val);
516 
517 	blk_mq_unquiesce_queue(q);
518 	blk_mq_unfreeze_queue(q);
519 
520 	return count;
521 }
522 
523 static ssize_t queue_wc_show(struct request_queue *q, char *page)
524 {
525 	if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
526 		return sprintf(page, "write back\n");
527 
528 	return sprintf(page, "write through\n");
529 }
530 
531 static ssize_t queue_wc_store(struct request_queue *q, const char *page,
532 			      size_t count)
533 {
534 	int set = -1;
535 
536 	if (!strncmp(page, "write back", 10))
537 		set = 1;
538 	else if (!strncmp(page, "write through", 13) ||
539 		 !strncmp(page, "none", 4))
540 		set = 0;
541 
542 	if (set == -1)
543 		return -EINVAL;
544 
545 	if (set)
546 		blk_queue_flag_set(QUEUE_FLAG_WC, q);
547 	else
548 		blk_queue_flag_clear(QUEUE_FLAG_WC, q);
549 
550 	return count;
551 }
552 
553 static ssize_t queue_fua_show(struct request_queue *q, char *page)
554 {
555 	return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
556 }
557 
558 static ssize_t queue_dax_show(struct request_queue *q, char *page)
559 {
560 	return queue_var_show(blk_queue_dax(q), page);
561 }
562 
563 #define QUEUE_RO_ENTRY(_prefix, _name)			\
564 static struct queue_sysfs_entry _prefix##_entry = {	\
565 	.attr	= { .name = _name, .mode = 0444 },	\
566 	.show	= _prefix##_show,			\
567 };
568 
569 #define QUEUE_RW_ENTRY(_prefix, _name)			\
570 static struct queue_sysfs_entry _prefix##_entry = {	\
571 	.attr	= { .name = _name, .mode = 0644 },	\
572 	.show	= _prefix##_show,			\
573 	.store	= _prefix##_store,			\
574 };
575 
576 QUEUE_RW_ENTRY(queue_requests, "nr_requests");
577 QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
578 QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
579 QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
580 QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
581 QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
582 QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
583 QUEUE_RW_ENTRY(elv_iosched, "scheduler");
584 
585 QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
586 QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
587 QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
588 QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size");
589 QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size");
590 
591 QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
592 QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity");
593 QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes");
594 QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes");
595 QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
596 
597 QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
598 QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes");
599 QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes");
600 QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
601 
602 QUEUE_RO_ENTRY(queue_zoned, "zoned");
603 QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
604 QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones");
605 QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones");
606 
607 QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
608 QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
609 QUEUE_RW_ENTRY(queue_poll, "io_poll");
610 QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
611 QUEUE_RW_ENTRY(queue_wc, "write_cache");
612 QUEUE_RO_ENTRY(queue_fua, "fua");
613 QUEUE_RO_ENTRY(queue_dax, "dax");
614 QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
615 QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
616 QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
617 QUEUE_RO_ENTRY(queue_dma_alignment, "dma_alignment");
618 
619 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
620 QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time");
621 #endif
622 
623 /* legacy alias for logical_block_size: */
624 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
625 	.attr = {.name = "hw_sector_size", .mode = 0444 },
626 	.show = queue_logical_block_size_show,
627 };
628 
629 QUEUE_RW_ENTRY(queue_nonrot, "rotational");
630 QUEUE_RW_ENTRY(queue_iostats, "iostats");
631 QUEUE_RW_ENTRY(queue_random, "add_random");
632 QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes");
633 
634 static struct attribute *queue_attrs[] = {
635 	&queue_requests_entry.attr,
636 	&queue_ra_entry.attr,
637 	&queue_max_hw_sectors_entry.attr,
638 	&queue_max_sectors_entry.attr,
639 	&queue_max_segments_entry.attr,
640 	&queue_max_discard_segments_entry.attr,
641 	&queue_max_integrity_segments_entry.attr,
642 	&queue_max_segment_size_entry.attr,
643 	&elv_iosched_entry.attr,
644 	&queue_hw_sector_size_entry.attr,
645 	&queue_logical_block_size_entry.attr,
646 	&queue_physical_block_size_entry.attr,
647 	&queue_chunk_sectors_entry.attr,
648 	&queue_io_min_entry.attr,
649 	&queue_io_opt_entry.attr,
650 	&queue_discard_granularity_entry.attr,
651 	&queue_discard_max_entry.attr,
652 	&queue_discard_max_hw_entry.attr,
653 	&queue_discard_zeroes_data_entry.attr,
654 	&queue_write_same_max_entry.attr,
655 	&queue_write_zeroes_max_entry.attr,
656 	&queue_zone_append_max_entry.attr,
657 	&queue_zone_write_granularity_entry.attr,
658 	&queue_nonrot_entry.attr,
659 	&queue_zoned_entry.attr,
660 	&queue_nr_zones_entry.attr,
661 	&queue_max_open_zones_entry.attr,
662 	&queue_max_active_zones_entry.attr,
663 	&queue_nomerges_entry.attr,
664 	&queue_rq_affinity_entry.attr,
665 	&queue_iostats_entry.attr,
666 	&queue_stable_writes_entry.attr,
667 	&queue_random_entry.attr,
668 	&queue_poll_entry.attr,
669 	&queue_wc_entry.attr,
670 	&queue_fua_entry.attr,
671 	&queue_dax_entry.attr,
672 	&queue_wb_lat_entry.attr,
673 	&queue_poll_delay_entry.attr,
674 	&queue_io_timeout_entry.attr,
675 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
676 	&blk_throtl_sample_time_entry.attr,
677 #endif
678 	&queue_virt_boundary_mask_entry.attr,
679 	&queue_dma_alignment_entry.attr,
680 	NULL,
681 };
682 
683 static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
684 				int n)
685 {
686 	struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
687 	struct request_queue *q = disk->queue;
688 
689 	if (attr == &queue_io_timeout_entry.attr &&
690 		(!q->mq_ops || !q->mq_ops->timeout))
691 			return 0;
692 
693 	if ((attr == &queue_max_open_zones_entry.attr ||
694 	     attr == &queue_max_active_zones_entry.attr) &&
695 	    !blk_queue_is_zoned(q))
696 		return 0;
697 
698 	return attr->mode;
699 }
700 
701 static struct attribute_group queue_attr_group = {
702 	.attrs = queue_attrs,
703 	.is_visible = queue_attr_visible,
704 };
705 
706 
707 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
708 
709 static ssize_t
710 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
711 {
712 	struct queue_sysfs_entry *entry = to_queue(attr);
713 	struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
714 	struct request_queue *q = disk->queue;
715 	ssize_t res;
716 
717 	if (!entry->show)
718 		return -EIO;
719 	mutex_lock(&q->sysfs_lock);
720 	res = entry->show(q, page);
721 	mutex_unlock(&q->sysfs_lock);
722 	return res;
723 }
724 
725 static ssize_t
726 queue_attr_store(struct kobject *kobj, struct attribute *attr,
727 		    const char *page, size_t length)
728 {
729 	struct queue_sysfs_entry *entry = to_queue(attr);
730 	struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
731 	struct request_queue *q = disk->queue;
732 	ssize_t res;
733 
734 	if (!entry->store)
735 		return -EIO;
736 
737 	mutex_lock(&q->sysfs_lock);
738 	res = entry->store(q, page, length);
739 	mutex_unlock(&q->sysfs_lock);
740 	return res;
741 }
742 
743 static const struct sysfs_ops queue_sysfs_ops = {
744 	.show	= queue_attr_show,
745 	.store	= queue_attr_store,
746 };
747 
748 static const struct attribute_group *blk_queue_attr_groups[] = {
749 	&queue_attr_group,
750 	NULL
751 };
752 
753 static void blk_queue_release(struct kobject *kobj)
754 {
755 	/* nothing to do here, all data is associated with the parent gendisk */
756 }
757 
758 static struct kobj_type blk_queue_ktype = {
759 	.default_groups = blk_queue_attr_groups,
760 	.sysfs_ops	= &queue_sysfs_ops,
761 	.release	= blk_queue_release,
762 };
763 
764 static void blk_debugfs_remove(struct gendisk *disk)
765 {
766 	struct request_queue *q = disk->queue;
767 
768 	mutex_lock(&q->debugfs_mutex);
769 	blk_trace_shutdown(q);
770 	debugfs_remove_recursive(q->debugfs_dir);
771 	q->debugfs_dir = NULL;
772 	q->sched_debugfs_dir = NULL;
773 	q->rqos_debugfs_dir = NULL;
774 	mutex_unlock(&q->debugfs_mutex);
775 }
776 
777 /**
778  * blk_register_queue - register a block layer queue with sysfs
779  * @disk: Disk of which the request queue should be registered with sysfs.
780  */
781 int blk_register_queue(struct gendisk *disk)
782 {
783 	struct request_queue *q = disk->queue;
784 	int ret;
785 
786 	mutex_lock(&q->sysfs_dir_lock);
787 	kobject_init(&disk->queue_kobj, &blk_queue_ktype);
788 	ret = kobject_add(&disk->queue_kobj, &disk_to_dev(disk)->kobj, "queue");
789 	if (ret < 0)
790 		goto out_put_queue_kobj;
791 
792 	if (queue_is_mq(q)) {
793 		ret = blk_mq_sysfs_register(disk);
794 		if (ret)
795 			goto out_put_queue_kobj;
796 	}
797 	mutex_lock(&q->sysfs_lock);
798 
799 	mutex_lock(&q->debugfs_mutex);
800 	q->debugfs_dir = debugfs_create_dir(disk->disk_name, blk_debugfs_root);
801 	if (queue_is_mq(q))
802 		blk_mq_debugfs_register(q);
803 	mutex_unlock(&q->debugfs_mutex);
804 
805 	ret = disk_register_independent_access_ranges(disk);
806 	if (ret)
807 		goto out_debugfs_remove;
808 
809 	if (q->elevator) {
810 		ret = elv_register_queue(q, false);
811 		if (ret)
812 			goto out_unregister_ia_ranges;
813 	}
814 
815 	ret = blk_crypto_sysfs_register(disk);
816 	if (ret)
817 		goto out_elv_unregister;
818 
819 	blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
820 	wbt_enable_default(q);
821 	blk_throtl_register(disk);
822 
823 	/* Now everything is ready and send out KOBJ_ADD uevent */
824 	kobject_uevent(&disk->queue_kobj, KOBJ_ADD);
825 	if (q->elevator)
826 		kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
827 	mutex_unlock(&q->sysfs_lock);
828 	mutex_unlock(&q->sysfs_dir_lock);
829 
830 	/*
831 	 * SCSI probing may synchronously create and destroy a lot of
832 	 * request_queues for non-existent devices.  Shutting down a fully
833 	 * functional queue takes measureable wallclock time as RCU grace
834 	 * periods are involved.  To avoid excessive latency in these
835 	 * cases, a request_queue starts out in a degraded mode which is
836 	 * faster to shut down and is made fully functional here as
837 	 * request_queues for non-existent devices never get registered.
838 	 */
839 	if (!blk_queue_init_done(q)) {
840 		blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
841 		percpu_ref_switch_to_percpu(&q->q_usage_counter);
842 	}
843 
844 	return ret;
845 
846 out_elv_unregister:
847 	elv_unregister_queue(q);
848 out_unregister_ia_ranges:
849 	disk_unregister_independent_access_ranges(disk);
850 out_debugfs_remove:
851 	blk_debugfs_remove(disk);
852 	mutex_unlock(&q->sysfs_lock);
853 out_put_queue_kobj:
854 	kobject_put(&disk->queue_kobj);
855 	mutex_unlock(&q->sysfs_dir_lock);
856 	return ret;
857 }
858 
859 /**
860  * blk_unregister_queue - counterpart of blk_register_queue()
861  * @disk: Disk of which the request queue should be unregistered from sysfs.
862  *
863  * Note: the caller is responsible for guaranteeing that this function is called
864  * after blk_register_queue() has finished.
865  */
866 void blk_unregister_queue(struct gendisk *disk)
867 {
868 	struct request_queue *q = disk->queue;
869 
870 	if (WARN_ON(!q))
871 		return;
872 
873 	/* Return early if disk->queue was never registered. */
874 	if (!blk_queue_registered(q))
875 		return;
876 
877 	/*
878 	 * Since sysfs_remove_dir() prevents adding new directory entries
879 	 * before removal of existing entries starts, protect against
880 	 * concurrent elv_iosched_store() calls.
881 	 */
882 	mutex_lock(&q->sysfs_lock);
883 	blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
884 	mutex_unlock(&q->sysfs_lock);
885 
886 	mutex_lock(&q->sysfs_dir_lock);
887 	/*
888 	 * Remove the sysfs attributes before unregistering the queue data
889 	 * structures that can be modified through sysfs.
890 	 */
891 	if (queue_is_mq(q))
892 		blk_mq_sysfs_unregister(disk);
893 	blk_crypto_sysfs_unregister(disk);
894 
895 	mutex_lock(&q->sysfs_lock);
896 	elv_unregister_queue(q);
897 	disk_unregister_independent_access_ranges(disk);
898 	mutex_unlock(&q->sysfs_lock);
899 
900 	/* Now that we've deleted all child objects, we can delete the queue. */
901 	kobject_uevent(&disk->queue_kobj, KOBJ_REMOVE);
902 	kobject_del(&disk->queue_kobj);
903 	mutex_unlock(&q->sysfs_dir_lock);
904 
905 	blk_debugfs_remove(disk);
906 }
907