1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to sysfs handling
5 #include <linux/kernel.h>
6 #include <linux/slab.h>
7 #include <linux/module.h>
9 #include <linux/blkdev.h>
10 #include <linux/backing-dev.h>
11 #include <linux/blktrace_api.h>
12 #include <linux/debugfs.h>
16 #include "blk-mq-debugfs.h"
17 #include "blk-mq-sched.h"
18 #include "blk-rq-qos.h"
20 #include "blk-cgroup.h"
21 #include "blk-throttle.h"
23 struct queue_sysfs_entry {
24 struct attribute attr;
25 ssize_t (*show)(struct gendisk *disk, char *page);
26 ssize_t (*show_limit)(struct gendisk *disk, char *page);
28 ssize_t (*store)(struct gendisk *disk, const char *page, size_t count);
29 int (*store_limit)(struct gendisk *disk, const char *page,
30 size_t count, struct queue_limits *lim);
34 queue_var_show(unsigned long var, char *page)
36 return sysfs_emit(page, "%lu\n", var);
40 queue_var_store(unsigned long *var, const char *page, size_t count)
45 err = kstrtoul(page, 10, &v);
46 if (err || v > UINT_MAX)
54 static ssize_t queue_requests_show(struct gendisk *disk, char *page)
58 mutex_lock(&disk->queue->elevator_lock);
59 ret = queue_var_show(disk->queue->nr_requests, page);
60 mutex_unlock(&disk->queue->elevator_lock);
65 queue_requests_store(struct gendisk *disk, const char *page, size_t count)
69 unsigned int memflags;
70 struct request_queue *q = disk->queue;
75 ret = queue_var_store(&nr, page, count);
79 memflags = blk_mq_freeze_queue(q);
80 mutex_lock(&q->elevator_lock);
81 if (nr < BLKDEV_MIN_RQ)
84 err = blk_mq_update_nr_requests(disk->queue, nr);
87 mutex_unlock(&q->elevator_lock);
88 blk_mq_unfreeze_queue(q, memflags);
92 static ssize_t queue_ra_show(struct gendisk *disk, char *page)
96 mutex_lock(&disk->queue->limits_lock);
97 ret = queue_var_show(disk->bdi->ra_pages << (PAGE_SHIFT - 10), page);
98 mutex_unlock(&disk->queue->limits_lock);
104 queue_ra_store(struct gendisk *disk, const char *page, size_t count)
108 unsigned int memflags;
109 struct request_queue *q = disk->queue;
111 ret = queue_var_store(&ra_kb, page, count);
115 * ->ra_pages is protected by ->limits_lock because it is usually
116 * calculated from the queue limits by queue_limits_commit_update.
118 mutex_lock(&q->limits_lock);
119 memflags = blk_mq_freeze_queue(q);
120 disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
121 mutex_unlock(&q->limits_lock);
122 blk_mq_unfreeze_queue(q, memflags);
127 #define QUEUE_SYSFS_LIMIT_SHOW(_field) \
128 static ssize_t queue_##_field##_show(struct gendisk *disk, char *page) \
130 return queue_var_show(disk->queue->limits._field, page); \
133 QUEUE_SYSFS_LIMIT_SHOW(max_segments)
134 QUEUE_SYSFS_LIMIT_SHOW(max_discard_segments)
135 QUEUE_SYSFS_LIMIT_SHOW(max_integrity_segments)
136 QUEUE_SYSFS_LIMIT_SHOW(max_segment_size)
137 QUEUE_SYSFS_LIMIT_SHOW(max_write_streams)
138 QUEUE_SYSFS_LIMIT_SHOW(write_stream_granularity)
139 QUEUE_SYSFS_LIMIT_SHOW(logical_block_size)
140 QUEUE_SYSFS_LIMIT_SHOW(physical_block_size)
141 QUEUE_SYSFS_LIMIT_SHOW(chunk_sectors)
142 QUEUE_SYSFS_LIMIT_SHOW(io_min)
143 QUEUE_SYSFS_LIMIT_SHOW(io_opt)
144 QUEUE_SYSFS_LIMIT_SHOW(discard_granularity)
145 QUEUE_SYSFS_LIMIT_SHOW(zone_write_granularity)
146 QUEUE_SYSFS_LIMIT_SHOW(virt_boundary_mask)
147 QUEUE_SYSFS_LIMIT_SHOW(dma_alignment)
148 QUEUE_SYSFS_LIMIT_SHOW(max_open_zones)
149 QUEUE_SYSFS_LIMIT_SHOW(max_active_zones)
150 QUEUE_SYSFS_LIMIT_SHOW(atomic_write_unit_min)
151 QUEUE_SYSFS_LIMIT_SHOW(atomic_write_unit_max)
153 #define QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(_field) \
154 static ssize_t queue_##_field##_show(struct gendisk *disk, char *page) \
156 return sysfs_emit(page, "%llu\n", \
157 (unsigned long long)disk->queue->limits._field << \
161 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(max_discard_sectors)
162 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(max_hw_discard_sectors)
163 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(max_write_zeroes_sectors)
164 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(atomic_write_max_sectors)
165 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(atomic_write_boundary_sectors)
166 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_BYTES(max_zone_append_sectors)
168 #define QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_KB(_field) \
169 static ssize_t queue_##_field##_show(struct gendisk *disk, char *page) \
171 return queue_var_show(disk->queue->limits._field >> 1, page); \
174 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_KB(max_sectors)
175 QUEUE_SYSFS_LIMIT_SHOW_SECTORS_TO_KB(max_hw_sectors)
177 #define QUEUE_SYSFS_SHOW_CONST(_name, _val) \
178 static ssize_t queue_##_name##_show(struct gendisk *disk, char *page) \
180 return sysfs_emit(page, "%d\n", _val); \
183 /* deprecated fields */
184 QUEUE_SYSFS_SHOW_CONST(discard_zeroes_data, 0)
185 QUEUE_SYSFS_SHOW_CONST(write_same_max, 0)
186 QUEUE_SYSFS_SHOW_CONST(poll_delay, -1)
188 static int queue_max_discard_sectors_store(struct gendisk *disk,
189 const char *page, size_t count, struct queue_limits *lim)
191 unsigned long max_discard_bytes;
194 ret = queue_var_store(&max_discard_bytes, page, count);
198 if (max_discard_bytes & (disk->queue->limits.discard_granularity - 1))
201 if ((max_discard_bytes >> SECTOR_SHIFT) > UINT_MAX)
204 lim->max_user_discard_sectors = max_discard_bytes >> SECTOR_SHIFT;
209 queue_max_sectors_store(struct gendisk *disk, const char *page, size_t count,
210 struct queue_limits *lim)
212 unsigned long max_sectors_kb;
215 ret = queue_var_store(&max_sectors_kb, page, count);
219 lim->max_user_sectors = max_sectors_kb << 1;
223 static ssize_t queue_feature_store(struct gendisk *disk, const char *page,
224 size_t count, struct queue_limits *lim, blk_features_t feature)
229 ret = queue_var_store(&val, page, count);
234 lim->features |= feature;
236 lim->features &= ~feature;
240 #define QUEUE_SYSFS_FEATURE(_name, _feature) \
241 static ssize_t queue_##_name##_show(struct gendisk *disk, char *page) \
243 return sysfs_emit(page, "%u\n", \
244 !!(disk->queue->limits.features & _feature)); \
246 static int queue_##_name##_store(struct gendisk *disk, \
247 const char *page, size_t count, struct queue_limits *lim) \
249 return queue_feature_store(disk, page, count, lim, _feature); \
252 QUEUE_SYSFS_FEATURE(rotational, BLK_FEAT_ROTATIONAL)
253 QUEUE_SYSFS_FEATURE(add_random, BLK_FEAT_ADD_RANDOM)
254 QUEUE_SYSFS_FEATURE(iostats, BLK_FEAT_IO_STAT)
255 QUEUE_SYSFS_FEATURE(stable_writes, BLK_FEAT_STABLE_WRITES);
257 #define QUEUE_SYSFS_FEATURE_SHOW(_name, _feature) \
258 static ssize_t queue_##_name##_show(struct gendisk *disk, char *page) \
260 return sysfs_emit(page, "%u\n", \
261 !!(disk->queue->limits.features & _feature)); \
264 QUEUE_SYSFS_FEATURE_SHOW(fua, BLK_FEAT_FUA);
265 QUEUE_SYSFS_FEATURE_SHOW(dax, BLK_FEAT_DAX);
267 static ssize_t queue_poll_show(struct gendisk *disk, char *page)
269 if (queue_is_mq(disk->queue))
270 return sysfs_emit(page, "%u\n", blk_mq_can_poll(disk->queue));
272 return sysfs_emit(page, "%u\n",
273 !!(disk->queue->limits.features & BLK_FEAT_POLL));
276 static ssize_t queue_zoned_show(struct gendisk *disk, char *page)
278 if (blk_queue_is_zoned(disk->queue))
279 return sysfs_emit(page, "host-managed\n");
280 return sysfs_emit(page, "none\n");
283 static ssize_t queue_nr_zones_show(struct gendisk *disk, char *page)
285 return queue_var_show(disk_nr_zones(disk), page);
288 static ssize_t queue_iostats_passthrough_show(struct gendisk *disk, char *page)
290 return queue_var_show(!!blk_queue_passthrough_stat(disk->queue), page);
293 static int queue_iostats_passthrough_store(struct gendisk *disk,
294 const char *page, size_t count, struct queue_limits *lim)
299 ret = queue_var_store(&ios, page, count);
304 lim->flags |= BLK_FLAG_IOSTATS_PASSTHROUGH;
306 lim->flags &= ~BLK_FLAG_IOSTATS_PASSTHROUGH;
310 static ssize_t queue_nomerges_show(struct gendisk *disk, char *page)
312 return queue_var_show((blk_queue_nomerges(disk->queue) << 1) |
313 blk_queue_noxmerges(disk->queue), page);
316 static ssize_t queue_nomerges_store(struct gendisk *disk, const char *page,
320 unsigned int memflags;
321 struct request_queue *q = disk->queue;
322 ssize_t ret = queue_var_store(&nm, page, count);
327 memflags = blk_mq_freeze_queue(q);
328 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
329 blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
331 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
333 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
334 blk_mq_unfreeze_queue(q, memflags);
339 static ssize_t queue_rq_affinity_show(struct gendisk *disk, char *page)
341 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &disk->queue->queue_flags);
342 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &disk->queue->queue_flags);
344 return queue_var_show(set << force, page);
348 queue_rq_affinity_store(struct gendisk *disk, const char *page, size_t count)
350 ssize_t ret = -EINVAL;
352 struct request_queue *q = disk->queue;
354 unsigned int memflags;
356 ret = queue_var_store(&val, page, count);
361 * Here we update two queue flags each using atomic bitops, although
362 * updating two flags isn't atomic it should be harmless as those flags
363 * are accessed individually using atomic test_bit operation. So we
364 * don't grab any lock while updating these flags.
366 memflags = blk_mq_freeze_queue(q);
368 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
369 blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
370 } else if (val == 1) {
371 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
372 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
373 } else if (val == 0) {
374 blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
375 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
377 blk_mq_unfreeze_queue(q, memflags);
382 static ssize_t queue_poll_delay_store(struct gendisk *disk, const char *page,
388 static ssize_t queue_poll_store(struct gendisk *disk, const char *page,
391 unsigned int memflags;
393 struct request_queue *q = disk->queue;
395 memflags = blk_mq_freeze_queue(q);
396 if (!(q->limits.features & BLK_FEAT_POLL)) {
401 pr_info_ratelimited("writes to the poll attribute are ignored.\n");
402 pr_info_ratelimited("please use driver specific parameters instead.\n");
404 blk_mq_unfreeze_queue(q, memflags);
408 static ssize_t queue_io_timeout_show(struct gendisk *disk, char *page)
410 return sysfs_emit(page, "%u\n",
411 jiffies_to_msecs(READ_ONCE(disk->queue->rq_timeout)));
414 static ssize_t queue_io_timeout_store(struct gendisk *disk, const char *page,
417 unsigned int val, memflags;
419 struct request_queue *q = disk->queue;
421 err = kstrtou32(page, 10, &val);
425 memflags = blk_mq_freeze_queue(q);
426 blk_queue_rq_timeout(q, msecs_to_jiffies(val));
427 blk_mq_unfreeze_queue(q, memflags);
432 static ssize_t queue_wc_show(struct gendisk *disk, char *page)
434 if (blk_queue_write_cache(disk->queue))
435 return sysfs_emit(page, "write back\n");
436 return sysfs_emit(page, "write through\n");
439 static int queue_wc_store(struct gendisk *disk, const char *page,
440 size_t count, struct queue_limits *lim)
444 if (!strncmp(page, "write back", 10)) {
446 } else if (!strncmp(page, "write through", 13) ||
447 !strncmp(page, "none", 4)) {
454 lim->flags |= BLK_FLAG_WRITE_CACHE_DISABLED;
456 lim->flags &= ~BLK_FLAG_WRITE_CACHE_DISABLED;
460 #define QUEUE_RO_ENTRY(_prefix, _name) \
461 static struct queue_sysfs_entry _prefix##_entry = { \
462 .attr = { .name = _name, .mode = 0444 }, \
463 .show = _prefix##_show, \
466 #define QUEUE_RW_ENTRY(_prefix, _name) \
467 static struct queue_sysfs_entry _prefix##_entry = { \
468 .attr = { .name = _name, .mode = 0644 }, \
469 .show = _prefix##_show, \
470 .store = _prefix##_store, \
473 #define QUEUE_LIM_RO_ENTRY(_prefix, _name) \
474 static struct queue_sysfs_entry _prefix##_entry = { \
475 .attr = { .name = _name, .mode = 0444 }, \
476 .show_limit = _prefix##_show, \
479 #define QUEUE_LIM_RW_ENTRY(_prefix, _name) \
480 static struct queue_sysfs_entry _prefix##_entry = { \
481 .attr = { .name = _name, .mode = 0644 }, \
482 .show_limit = _prefix##_show, \
483 .store_limit = _prefix##_store, \
486 QUEUE_RW_ENTRY(queue_requests, "nr_requests");
487 QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
488 QUEUE_LIM_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
489 QUEUE_LIM_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
490 QUEUE_LIM_RO_ENTRY(queue_max_segments, "max_segments");
491 QUEUE_LIM_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
492 QUEUE_LIM_RO_ENTRY(queue_max_segment_size, "max_segment_size");
493 QUEUE_LIM_RO_ENTRY(queue_max_write_streams, "max_write_streams");
494 QUEUE_LIM_RO_ENTRY(queue_write_stream_granularity, "write_stream_granularity");
495 QUEUE_RW_ENTRY(elv_iosched, "scheduler");
497 QUEUE_LIM_RO_ENTRY(queue_logical_block_size, "logical_block_size");
498 QUEUE_LIM_RO_ENTRY(queue_physical_block_size, "physical_block_size");
499 QUEUE_LIM_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
500 QUEUE_LIM_RO_ENTRY(queue_io_min, "minimum_io_size");
501 QUEUE_LIM_RO_ENTRY(queue_io_opt, "optimal_io_size");
503 QUEUE_LIM_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
504 QUEUE_LIM_RO_ENTRY(queue_discard_granularity, "discard_granularity");
505 QUEUE_LIM_RO_ENTRY(queue_max_hw_discard_sectors, "discard_max_hw_bytes");
506 QUEUE_LIM_RW_ENTRY(queue_max_discard_sectors, "discard_max_bytes");
507 QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
509 QUEUE_LIM_RO_ENTRY(queue_atomic_write_max_sectors, "atomic_write_max_bytes");
510 QUEUE_LIM_RO_ENTRY(queue_atomic_write_boundary_sectors,
511 "atomic_write_boundary_bytes");
512 QUEUE_LIM_RO_ENTRY(queue_atomic_write_unit_max, "atomic_write_unit_max_bytes");
513 QUEUE_LIM_RO_ENTRY(queue_atomic_write_unit_min, "atomic_write_unit_min_bytes");
515 QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
516 QUEUE_LIM_RO_ENTRY(queue_max_write_zeroes_sectors, "write_zeroes_max_bytes");
517 QUEUE_LIM_RO_ENTRY(queue_max_zone_append_sectors, "zone_append_max_bytes");
518 QUEUE_LIM_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
520 QUEUE_LIM_RO_ENTRY(queue_zoned, "zoned");
521 QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
522 QUEUE_LIM_RO_ENTRY(queue_max_open_zones, "max_open_zones");
523 QUEUE_LIM_RO_ENTRY(queue_max_active_zones, "max_active_zones");
525 QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
526 QUEUE_LIM_RW_ENTRY(queue_iostats_passthrough, "iostats_passthrough");
527 QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
528 QUEUE_RW_ENTRY(queue_poll, "io_poll");
529 QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
530 QUEUE_LIM_RW_ENTRY(queue_wc, "write_cache");
531 QUEUE_LIM_RO_ENTRY(queue_fua, "fua");
532 QUEUE_LIM_RO_ENTRY(queue_dax, "dax");
533 QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
534 QUEUE_LIM_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
535 QUEUE_LIM_RO_ENTRY(queue_dma_alignment, "dma_alignment");
537 /* legacy alias for logical_block_size: */
538 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
539 .attr = {.name = "hw_sector_size", .mode = 0444 },
540 .show_limit = queue_logical_block_size_show,
543 QUEUE_LIM_RW_ENTRY(queue_rotational, "rotational");
544 QUEUE_LIM_RW_ENTRY(queue_iostats, "iostats");
545 QUEUE_LIM_RW_ENTRY(queue_add_random, "add_random");
546 QUEUE_LIM_RW_ENTRY(queue_stable_writes, "stable_writes");
548 #ifdef CONFIG_BLK_WBT
549 static ssize_t queue_var_store64(s64 *var, const char *page)
554 err = kstrtos64(page, 10, &v);
562 static ssize_t queue_wb_lat_show(struct gendisk *disk, char *page)
565 struct request_queue *q = disk->queue;
567 mutex_lock(&disk->rqos_state_mutex);
568 if (!wbt_rq_qos(q)) {
573 if (wbt_disabled(q)) {
574 ret = sysfs_emit(page, "0\n");
578 ret = sysfs_emit(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
580 mutex_unlock(&disk->rqos_state_mutex);
584 static ssize_t queue_wb_lat_store(struct gendisk *disk, const char *page,
587 struct request_queue *q = disk->queue;
591 unsigned int memflags;
593 ret = queue_var_store64(&val, page);
599 memflags = blk_mq_freeze_queue(q);
601 rqos = wbt_rq_qos(q);
603 ret = wbt_init(disk);
610 val = wbt_default_latency_nsec(q);
614 if (wbt_get_min_lat(q) == val)
618 * Ensure that the queue is idled, in case the latency update
619 * ends up either enabling or disabling wbt completely. We can't
620 * have IO inflight if that happens.
622 blk_mq_quiesce_queue(q);
624 mutex_lock(&disk->rqos_state_mutex);
625 wbt_set_min_lat(q, val);
626 mutex_unlock(&disk->rqos_state_mutex);
628 blk_mq_unquiesce_queue(q);
630 blk_mq_unfreeze_queue(q, memflags);
635 QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
638 /* Common attributes for bio-based and request-based queues. */
639 static struct attribute *queue_attrs[] = {
641 * Attributes which are protected with q->limits_lock.
643 &queue_max_hw_sectors_entry.attr,
644 &queue_max_sectors_entry.attr,
645 &queue_max_segments_entry.attr,
646 &queue_max_discard_segments_entry.attr,
647 &queue_max_integrity_segments_entry.attr,
648 &queue_max_segment_size_entry.attr,
649 &queue_max_write_streams_entry.attr,
650 &queue_write_stream_granularity_entry.attr,
651 &queue_hw_sector_size_entry.attr,
652 &queue_logical_block_size_entry.attr,
653 &queue_physical_block_size_entry.attr,
654 &queue_chunk_sectors_entry.attr,
655 &queue_io_min_entry.attr,
656 &queue_io_opt_entry.attr,
657 &queue_discard_granularity_entry.attr,
658 &queue_max_discard_sectors_entry.attr,
659 &queue_max_hw_discard_sectors_entry.attr,
660 &queue_atomic_write_max_sectors_entry.attr,
661 &queue_atomic_write_boundary_sectors_entry.attr,
662 &queue_atomic_write_unit_min_entry.attr,
663 &queue_atomic_write_unit_max_entry.attr,
664 &queue_max_write_zeroes_sectors_entry.attr,
665 &queue_max_zone_append_sectors_entry.attr,
666 &queue_zone_write_granularity_entry.attr,
667 &queue_rotational_entry.attr,
668 &queue_zoned_entry.attr,
669 &queue_max_open_zones_entry.attr,
670 &queue_max_active_zones_entry.attr,
671 &queue_iostats_passthrough_entry.attr,
672 &queue_iostats_entry.attr,
673 &queue_stable_writes_entry.attr,
674 &queue_add_random_entry.attr,
675 &queue_wc_entry.attr,
676 &queue_fua_entry.attr,
677 &queue_dax_entry.attr,
678 &queue_virt_boundary_mask_entry.attr,
679 &queue_dma_alignment_entry.attr,
680 &queue_ra_entry.attr,
683 * Attributes which don't require locking.
685 &queue_discard_zeroes_data_entry.attr,
686 &queue_write_same_max_entry.attr,
687 &queue_nr_zones_entry.attr,
688 &queue_nomerges_entry.attr,
689 &queue_poll_entry.attr,
690 &queue_poll_delay_entry.attr,
695 /* Request-based queue attributes that are not relevant for bio-based queues. */
696 static struct attribute *blk_mq_queue_attrs[] = {
698 * Attributes which require some form of locking other than
701 &elv_iosched_entry.attr,
702 &queue_requests_entry.attr,
703 #ifdef CONFIG_BLK_WBT
704 &queue_wb_lat_entry.attr,
707 * Attributes which don't require locking.
709 &queue_rq_affinity_entry.attr,
710 &queue_io_timeout_entry.attr,
715 static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
718 struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
719 struct request_queue *q = disk->queue;
721 if ((attr == &queue_max_open_zones_entry.attr ||
722 attr == &queue_max_active_zones_entry.attr) &&
723 !blk_queue_is_zoned(q))
729 static umode_t blk_mq_queue_attr_visible(struct kobject *kobj,
730 struct attribute *attr, int n)
732 struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
733 struct request_queue *q = disk->queue;
738 if (attr == &queue_io_timeout_entry.attr && !q->mq_ops->timeout)
744 static struct attribute_group queue_attr_group = {
745 .attrs = queue_attrs,
746 .is_visible = queue_attr_visible,
749 static struct attribute_group blk_mq_queue_attr_group = {
750 .attrs = blk_mq_queue_attrs,
751 .is_visible = blk_mq_queue_attr_visible,
754 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
757 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
759 struct queue_sysfs_entry *entry = to_queue(attr);
760 struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
762 if (!entry->show && !entry->show_limit)
765 if (entry->show_limit) {
768 mutex_lock(&disk->queue->limits_lock);
769 res = entry->show_limit(disk, page);
770 mutex_unlock(&disk->queue->limits_lock);
774 return entry->show(disk, page);
778 queue_attr_store(struct kobject *kobj, struct attribute *attr,
779 const char *page, size_t length)
781 struct queue_sysfs_entry *entry = to_queue(attr);
782 struct gendisk *disk = container_of(kobj, struct gendisk, queue_kobj);
783 struct request_queue *q = disk->queue;
785 if (!entry->store_limit && !entry->store)
788 if (entry->store_limit) {
791 struct queue_limits lim = queue_limits_start_update(q);
793 res = entry->store_limit(disk, page, length, &lim);
795 queue_limits_cancel_update(q);
799 res = queue_limits_commit_update_frozen(q, &lim);
805 return entry->store(disk, page, length);
808 static const struct sysfs_ops queue_sysfs_ops = {
809 .show = queue_attr_show,
810 .store = queue_attr_store,
813 static const struct attribute_group *blk_queue_attr_groups[] = {
815 &blk_mq_queue_attr_group,
819 static void blk_queue_release(struct kobject *kobj)
821 /* nothing to do here, all data is associated with the parent gendisk */
824 static const struct kobj_type blk_queue_ktype = {
825 .default_groups = blk_queue_attr_groups,
826 .sysfs_ops = &queue_sysfs_ops,
827 .release = blk_queue_release,
830 static void blk_debugfs_remove(struct gendisk *disk)
832 struct request_queue *q = disk->queue;
834 mutex_lock(&q->debugfs_mutex);
835 blk_trace_shutdown(q);
836 debugfs_remove_recursive(q->debugfs_dir);
837 q->debugfs_dir = NULL;
838 q->sched_debugfs_dir = NULL;
839 q->rqos_debugfs_dir = NULL;
840 mutex_unlock(&q->debugfs_mutex);
844 * blk_register_queue - register a block layer queue with sysfs
845 * @disk: Disk of which the request queue should be registered with sysfs.
847 int blk_register_queue(struct gendisk *disk)
849 struct request_queue *q = disk->queue;
852 kobject_init(&disk->queue_kobj, &blk_queue_ktype);
853 ret = kobject_add(&disk->queue_kobj, &disk_to_dev(disk)->kobj, "queue");
855 goto out_put_queue_kobj;
857 if (queue_is_mq(q)) {
858 ret = blk_mq_sysfs_register(disk);
860 goto out_put_queue_kobj;
862 mutex_lock(&q->sysfs_lock);
864 mutex_lock(&q->debugfs_mutex);
865 q->debugfs_dir = debugfs_create_dir(disk->disk_name, blk_debugfs_root);
867 blk_mq_debugfs_register(q);
868 mutex_unlock(&q->debugfs_mutex);
870 ret = disk_register_independent_access_ranges(disk);
872 goto out_debugfs_remove;
874 ret = blk_crypto_sysfs_register(disk);
876 goto out_unregister_ia_ranges;
879 elevator_set_default(q);
880 wbt_enable_default(disk);
882 blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
884 /* Now everything is ready and send out KOBJ_ADD uevent */
885 kobject_uevent(&disk->queue_kobj, KOBJ_ADD);
887 kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
888 mutex_unlock(&q->sysfs_lock);
891 * SCSI probing may synchronously create and destroy a lot of
892 * request_queues for non-existent devices. Shutting down a fully
893 * functional queue takes measureable wallclock time as RCU grace
894 * periods are involved. To avoid excessive latency in these
895 * cases, a request_queue starts out in a degraded mode which is
896 * faster to shut down and is made fully functional here as
897 * request_queues for non-existent devices never get registered.
899 blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
900 percpu_ref_switch_to_percpu(&q->q_usage_counter);
904 out_unregister_ia_ranges:
905 disk_unregister_independent_access_ranges(disk);
907 blk_debugfs_remove(disk);
908 mutex_unlock(&q->sysfs_lock);
910 blk_mq_sysfs_unregister(disk);
912 kobject_put(&disk->queue_kobj);
917 * blk_unregister_queue - counterpart of blk_register_queue()
918 * @disk: Disk of which the request queue should be unregistered from sysfs.
920 * Note: the caller is responsible for guaranteeing that this function is called
921 * after blk_register_queue() has finished.
923 void blk_unregister_queue(struct gendisk *disk)
925 struct request_queue *q = disk->queue;
930 /* Return early if disk->queue was never registered. */
931 if (!blk_queue_registered(q))
935 * Since sysfs_remove_dir() prevents adding new directory entries
936 * before removal of existing entries starts, protect against
937 * concurrent elv_iosched_store() calls.
939 mutex_lock(&q->sysfs_lock);
940 blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
941 mutex_unlock(&q->sysfs_lock);
944 * Remove the sysfs attributes before unregistering the queue data
945 * structures that can be modified through sysfs.
948 blk_mq_sysfs_unregister(disk);
949 blk_crypto_sysfs_unregister(disk);
951 mutex_lock(&q->sysfs_lock);
952 disk_unregister_independent_access_ranges(disk);
953 mutex_unlock(&q->sysfs_lock);
955 /* Now that we've deleted all child objects, we can delete the queue. */
956 kobject_uevent(&disk->queue_kobj, KOBJ_REMOVE);
957 kobject_del(&disk->queue_kobj);
960 elevator_set_none(q);
962 blk_debugfs_remove(disk);