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