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/blk-mq.h>
13 #include <linux/blk-cgroup.h>
14 #include <linux/debugfs.h>
18 #include "blk-mq-debugfs.h"
21 struct queue_sysfs_entry {
22 struct attribute attr;
23 ssize_t (*show)(struct request_queue *, char *);
24 ssize_t (*store)(struct request_queue *, const char *, size_t);
28 queue_var_show(unsigned long var, char *page)
30 return sprintf(page, "%lu\n", var);
34 queue_var_store(unsigned long *var, const char *page, size_t count)
39 err = kstrtoul(page, 10, &v);
40 if (err || v > UINT_MAX)
48 static ssize_t queue_var_store64(s64 *var, const char *page)
53 err = kstrtos64(page, 10, &v);
61 static ssize_t queue_requests_show(struct request_queue *q, char *page)
63 return queue_var_show(q->nr_requests, (page));
67 queue_requests_store(struct request_queue *q, const char *page, size_t count)
75 ret = queue_var_store(&nr, page, count);
79 if (nr < BLKDEV_MIN_RQ)
82 err = blk_mq_update_nr_requests(q, nr);
89 static ssize_t queue_ra_show(struct request_queue *q, char *page)
91 unsigned long ra_kb = q->backing_dev_info->ra_pages <<
94 return queue_var_show(ra_kb, (page));
98 queue_ra_store(struct request_queue *q, const char *page, size_t count)
101 ssize_t ret = queue_var_store(&ra_kb, page, count);
106 q->backing_dev_info->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
111 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
113 int max_sectors_kb = queue_max_sectors(q) >> 1;
115 return queue_var_show(max_sectors_kb, (page));
118 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
120 return queue_var_show(queue_max_segments(q), (page));
123 static ssize_t queue_max_discard_segments_show(struct request_queue *q,
126 return queue_var_show(queue_max_discard_segments(q), (page));
129 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
131 return queue_var_show(q->limits.max_integrity_segments, (page));
134 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
136 return queue_var_show(queue_max_segment_size(q), (page));
139 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
141 return queue_var_show(queue_logical_block_size(q), page);
144 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
146 return queue_var_show(queue_physical_block_size(q), page);
149 static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
151 return queue_var_show(q->limits.chunk_sectors, page);
154 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
156 return queue_var_show(queue_io_min(q), page);
159 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
161 return queue_var_show(queue_io_opt(q), page);
164 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
166 return queue_var_show(q->limits.discard_granularity, page);
169 static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
172 return sprintf(page, "%llu\n",
173 (unsigned long long)q->limits.max_hw_discard_sectors << 9);
176 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
178 return sprintf(page, "%llu\n",
179 (unsigned long long)q->limits.max_discard_sectors << 9);
182 static ssize_t queue_discard_max_store(struct request_queue *q,
183 const char *page, size_t count)
185 unsigned long max_discard;
186 ssize_t ret = queue_var_store(&max_discard, page, count);
191 if (max_discard & (q->limits.discard_granularity - 1))
195 if (max_discard > UINT_MAX)
198 if (max_discard > q->limits.max_hw_discard_sectors)
199 max_discard = q->limits.max_hw_discard_sectors;
201 q->limits.max_discard_sectors = max_discard;
205 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
207 return queue_var_show(0, page);
210 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
212 return sprintf(page, "%llu\n",
213 (unsigned long long)q->limits.max_write_same_sectors << 9);
216 static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
218 return sprintf(page, "%llu\n",
219 (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
222 static ssize_t queue_zone_append_max_show(struct request_queue *q, char *page)
224 unsigned long long max_sectors = q->limits.max_zone_append_sectors;
226 return sprintf(page, "%llu\n", max_sectors << SECTOR_SHIFT);
230 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
232 unsigned long max_sectors_kb,
233 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
234 page_kb = 1 << (PAGE_SHIFT - 10);
235 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
240 max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
241 q->limits.max_dev_sectors >> 1);
243 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
246 spin_lock_irq(&q->queue_lock);
247 q->limits.max_sectors = max_sectors_kb << 1;
248 q->backing_dev_info->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
249 spin_unlock_irq(&q->queue_lock);
254 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
256 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
258 return queue_var_show(max_hw_sectors_kb, (page));
261 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
263 queue_show_##name(struct request_queue *q, char *page) \
266 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
267 return queue_var_show(neg ? !bit : bit, page); \
270 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
274 ret = queue_var_store(&val, page, count); \
281 blk_queue_flag_set(QUEUE_FLAG_##flag, q); \
283 blk_queue_flag_clear(QUEUE_FLAG_##flag, q); \
287 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
288 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
289 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
290 #undef QUEUE_SYSFS_BIT_FNS
292 static ssize_t queue_zoned_show(struct request_queue *q, char *page)
294 switch (blk_queue_zoned_model(q)) {
296 return sprintf(page, "host-aware\n");
298 return sprintf(page, "host-managed\n");
300 return sprintf(page, "none\n");
304 static ssize_t queue_nr_zones_show(struct request_queue *q, char *page)
306 return queue_var_show(blk_queue_nr_zones(q), page);
309 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
311 return queue_var_show((blk_queue_nomerges(q) << 1) |
312 blk_queue_noxmerges(q), page);
315 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
319 ssize_t ret = queue_var_store(&nm, page, count);
324 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
325 blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
327 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
329 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
334 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
336 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
337 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
339 return queue_var_show(set << force, page);
343 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
345 ssize_t ret = -EINVAL;
349 ret = queue_var_store(&val, page, count);
354 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
355 blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
356 } else if (val == 1) {
357 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
358 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
359 } else if (val == 0) {
360 blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
361 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
367 static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
371 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
372 val = BLK_MQ_POLL_CLASSIC;
374 val = q->poll_nsec / 1000;
376 return sprintf(page, "%d\n", val);
379 static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
384 if (!q->mq_ops || !q->mq_ops->poll)
387 err = kstrtoint(page, 10, &val);
391 if (val == BLK_MQ_POLL_CLASSIC)
392 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
394 q->poll_nsec = val * 1000;
401 static ssize_t queue_poll_show(struct request_queue *q, char *page)
403 return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
406 static ssize_t queue_poll_store(struct request_queue *q, const char *page,
409 unsigned long poll_on;
412 if (!q->tag_set || q->tag_set->nr_maps <= HCTX_TYPE_POLL ||
413 !q->tag_set->map[HCTX_TYPE_POLL].nr_queues)
416 ret = queue_var_store(&poll_on, page, count);
421 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
423 blk_queue_flag_clear(QUEUE_FLAG_POLL, q);
428 static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
430 return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
433 static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
439 err = kstrtou32(page, 10, &val);
443 blk_queue_rq_timeout(q, msecs_to_jiffies(val));
448 static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
453 return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
456 static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
463 ret = queue_var_store64(&val, page);
469 rqos = wbt_rq_qos(q);
477 val = wbt_default_latency_nsec(q);
481 if (wbt_get_min_lat(q) == val)
485 * Ensure that the queue is idled, in case the latency update
486 * ends up either enabling or disabling wbt completely. We can't
487 * have IO inflight if that happens.
489 blk_mq_freeze_queue(q);
490 blk_mq_quiesce_queue(q);
492 wbt_set_min_lat(q, val);
494 blk_mq_unquiesce_queue(q);
495 blk_mq_unfreeze_queue(q);
500 static ssize_t queue_wc_show(struct request_queue *q, char *page)
502 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
503 return sprintf(page, "write back\n");
505 return sprintf(page, "write through\n");
508 static ssize_t queue_wc_store(struct request_queue *q, const char *page,
513 if (!strncmp(page, "write back", 10))
515 else if (!strncmp(page, "write through", 13) ||
516 !strncmp(page, "none", 4))
523 blk_queue_flag_set(QUEUE_FLAG_WC, q);
525 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
530 static ssize_t queue_fua_show(struct request_queue *q, char *page)
532 return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
535 static ssize_t queue_dax_show(struct request_queue *q, char *page)
537 return queue_var_show(blk_queue_dax(q), page);
540 static struct queue_sysfs_entry queue_requests_entry = {
541 .attr = {.name = "nr_requests", .mode = 0644 },
542 .show = queue_requests_show,
543 .store = queue_requests_store,
546 static struct queue_sysfs_entry queue_ra_entry = {
547 .attr = {.name = "read_ahead_kb", .mode = 0644 },
548 .show = queue_ra_show,
549 .store = queue_ra_store,
552 static struct queue_sysfs_entry queue_max_sectors_entry = {
553 .attr = {.name = "max_sectors_kb", .mode = 0644 },
554 .show = queue_max_sectors_show,
555 .store = queue_max_sectors_store,
558 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
559 .attr = {.name = "max_hw_sectors_kb", .mode = 0444 },
560 .show = queue_max_hw_sectors_show,
563 static struct queue_sysfs_entry queue_max_segments_entry = {
564 .attr = {.name = "max_segments", .mode = 0444 },
565 .show = queue_max_segments_show,
568 static struct queue_sysfs_entry queue_max_discard_segments_entry = {
569 .attr = {.name = "max_discard_segments", .mode = 0444 },
570 .show = queue_max_discard_segments_show,
573 static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
574 .attr = {.name = "max_integrity_segments", .mode = 0444 },
575 .show = queue_max_integrity_segments_show,
578 static struct queue_sysfs_entry queue_max_segment_size_entry = {
579 .attr = {.name = "max_segment_size", .mode = 0444 },
580 .show = queue_max_segment_size_show,
583 static struct queue_sysfs_entry queue_iosched_entry = {
584 .attr = {.name = "scheduler", .mode = 0644 },
585 .show = elv_iosched_show,
586 .store = elv_iosched_store,
589 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
590 .attr = {.name = "hw_sector_size", .mode = 0444 },
591 .show = queue_logical_block_size_show,
594 static struct queue_sysfs_entry queue_logical_block_size_entry = {
595 .attr = {.name = "logical_block_size", .mode = 0444 },
596 .show = queue_logical_block_size_show,
599 static struct queue_sysfs_entry queue_physical_block_size_entry = {
600 .attr = {.name = "physical_block_size", .mode = 0444 },
601 .show = queue_physical_block_size_show,
604 static struct queue_sysfs_entry queue_chunk_sectors_entry = {
605 .attr = {.name = "chunk_sectors", .mode = 0444 },
606 .show = queue_chunk_sectors_show,
609 static struct queue_sysfs_entry queue_io_min_entry = {
610 .attr = {.name = "minimum_io_size", .mode = 0444 },
611 .show = queue_io_min_show,
614 static struct queue_sysfs_entry queue_io_opt_entry = {
615 .attr = {.name = "optimal_io_size", .mode = 0444 },
616 .show = queue_io_opt_show,
619 static struct queue_sysfs_entry queue_discard_granularity_entry = {
620 .attr = {.name = "discard_granularity", .mode = 0444 },
621 .show = queue_discard_granularity_show,
624 static struct queue_sysfs_entry queue_discard_max_hw_entry = {
625 .attr = {.name = "discard_max_hw_bytes", .mode = 0444 },
626 .show = queue_discard_max_hw_show,
629 static struct queue_sysfs_entry queue_discard_max_entry = {
630 .attr = {.name = "discard_max_bytes", .mode = 0644 },
631 .show = queue_discard_max_show,
632 .store = queue_discard_max_store,
635 static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
636 .attr = {.name = "discard_zeroes_data", .mode = 0444 },
637 .show = queue_discard_zeroes_data_show,
640 static struct queue_sysfs_entry queue_write_same_max_entry = {
641 .attr = {.name = "write_same_max_bytes", .mode = 0444 },
642 .show = queue_write_same_max_show,
645 static struct queue_sysfs_entry queue_write_zeroes_max_entry = {
646 .attr = {.name = "write_zeroes_max_bytes", .mode = 0444 },
647 .show = queue_write_zeroes_max_show,
650 static struct queue_sysfs_entry queue_zone_append_max_entry = {
651 .attr = {.name = "zone_append_max_bytes", .mode = 0444 },
652 .show = queue_zone_append_max_show,
655 static struct queue_sysfs_entry queue_nonrot_entry = {
656 .attr = {.name = "rotational", .mode = 0644 },
657 .show = queue_show_nonrot,
658 .store = queue_store_nonrot,
661 static struct queue_sysfs_entry queue_zoned_entry = {
662 .attr = {.name = "zoned", .mode = 0444 },
663 .show = queue_zoned_show,
666 static struct queue_sysfs_entry queue_nr_zones_entry = {
667 .attr = {.name = "nr_zones", .mode = 0444 },
668 .show = queue_nr_zones_show,
671 static struct queue_sysfs_entry queue_nomerges_entry = {
672 .attr = {.name = "nomerges", .mode = 0644 },
673 .show = queue_nomerges_show,
674 .store = queue_nomerges_store,
677 static struct queue_sysfs_entry queue_rq_affinity_entry = {
678 .attr = {.name = "rq_affinity", .mode = 0644 },
679 .show = queue_rq_affinity_show,
680 .store = queue_rq_affinity_store,
683 static struct queue_sysfs_entry queue_iostats_entry = {
684 .attr = {.name = "iostats", .mode = 0644 },
685 .show = queue_show_iostats,
686 .store = queue_store_iostats,
689 static struct queue_sysfs_entry queue_random_entry = {
690 .attr = {.name = "add_random", .mode = 0644 },
691 .show = queue_show_random,
692 .store = queue_store_random,
695 static struct queue_sysfs_entry queue_poll_entry = {
696 .attr = {.name = "io_poll", .mode = 0644 },
697 .show = queue_poll_show,
698 .store = queue_poll_store,
701 static struct queue_sysfs_entry queue_poll_delay_entry = {
702 .attr = {.name = "io_poll_delay", .mode = 0644 },
703 .show = queue_poll_delay_show,
704 .store = queue_poll_delay_store,
707 static struct queue_sysfs_entry queue_wc_entry = {
708 .attr = {.name = "write_cache", .mode = 0644 },
709 .show = queue_wc_show,
710 .store = queue_wc_store,
713 static struct queue_sysfs_entry queue_fua_entry = {
714 .attr = {.name = "fua", .mode = 0444 },
715 .show = queue_fua_show,
718 static struct queue_sysfs_entry queue_dax_entry = {
719 .attr = {.name = "dax", .mode = 0444 },
720 .show = queue_dax_show,
723 static struct queue_sysfs_entry queue_io_timeout_entry = {
724 .attr = {.name = "io_timeout", .mode = 0644 },
725 .show = queue_io_timeout_show,
726 .store = queue_io_timeout_store,
729 static struct queue_sysfs_entry queue_wb_lat_entry = {
730 .attr = {.name = "wbt_lat_usec", .mode = 0644 },
731 .show = queue_wb_lat_show,
732 .store = queue_wb_lat_store,
735 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
736 static struct queue_sysfs_entry throtl_sample_time_entry = {
737 .attr = {.name = "throttle_sample_time", .mode = 0644 },
738 .show = blk_throtl_sample_time_show,
739 .store = blk_throtl_sample_time_store,
743 static struct attribute *queue_attrs[] = {
744 &queue_requests_entry.attr,
745 &queue_ra_entry.attr,
746 &queue_max_hw_sectors_entry.attr,
747 &queue_max_sectors_entry.attr,
748 &queue_max_segments_entry.attr,
749 &queue_max_discard_segments_entry.attr,
750 &queue_max_integrity_segments_entry.attr,
751 &queue_max_segment_size_entry.attr,
752 &queue_iosched_entry.attr,
753 &queue_hw_sector_size_entry.attr,
754 &queue_logical_block_size_entry.attr,
755 &queue_physical_block_size_entry.attr,
756 &queue_chunk_sectors_entry.attr,
757 &queue_io_min_entry.attr,
758 &queue_io_opt_entry.attr,
759 &queue_discard_granularity_entry.attr,
760 &queue_discard_max_entry.attr,
761 &queue_discard_max_hw_entry.attr,
762 &queue_discard_zeroes_data_entry.attr,
763 &queue_write_same_max_entry.attr,
764 &queue_write_zeroes_max_entry.attr,
765 &queue_zone_append_max_entry.attr,
766 &queue_nonrot_entry.attr,
767 &queue_zoned_entry.attr,
768 &queue_nr_zones_entry.attr,
769 &queue_nomerges_entry.attr,
770 &queue_rq_affinity_entry.attr,
771 &queue_iostats_entry.attr,
772 &queue_random_entry.attr,
773 &queue_poll_entry.attr,
774 &queue_wc_entry.attr,
775 &queue_fua_entry.attr,
776 &queue_dax_entry.attr,
777 &queue_wb_lat_entry.attr,
778 &queue_poll_delay_entry.attr,
779 &queue_io_timeout_entry.attr,
780 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
781 &throtl_sample_time_entry.attr,
786 static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
789 struct request_queue *q =
790 container_of(kobj, struct request_queue, kobj);
792 if (attr == &queue_io_timeout_entry.attr &&
793 (!q->mq_ops || !q->mq_ops->timeout))
799 static struct attribute_group queue_attr_group = {
800 .attrs = queue_attrs,
801 .is_visible = queue_attr_visible,
805 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
808 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
810 struct queue_sysfs_entry *entry = to_queue(attr);
811 struct request_queue *q =
812 container_of(kobj, struct request_queue, kobj);
817 mutex_lock(&q->sysfs_lock);
818 res = entry->show(q, page);
819 mutex_unlock(&q->sysfs_lock);
824 queue_attr_store(struct kobject *kobj, struct attribute *attr,
825 const char *page, size_t length)
827 struct queue_sysfs_entry *entry = to_queue(attr);
828 struct request_queue *q;
834 q = container_of(kobj, struct request_queue, kobj);
835 mutex_lock(&q->sysfs_lock);
836 res = entry->store(q, page, length);
837 mutex_unlock(&q->sysfs_lock);
841 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
843 struct request_queue *q = container_of(rcu_head, struct request_queue,
845 kmem_cache_free(blk_requestq_cachep, q);
848 /* Unconfigure the I/O scheduler and dissociate from the cgroup controller. */
849 static void blk_exit_queue(struct request_queue *q)
852 * Since the I/O scheduler exit code may access cgroup information,
853 * perform I/O scheduler exit before disassociating from the block
858 __elevator_exit(q, q->elevator);
863 * Remove all references to @q from the block cgroup controller before
864 * restoring @q->queue_lock to avoid that restoring this pointer causes
865 * e.g. blkcg_print_blkgs() to crash.
870 * Since the cgroup code may dereference the @q->backing_dev_info
871 * pointer, only decrease its reference count after having removed the
872 * association with the block cgroup controller.
874 bdi_put(q->backing_dev_info);
878 * blk_release_queue - releases all allocated resources of the request_queue
879 * @kobj: pointer to a kobject, whose container is a request_queue
881 * This function releases all allocated resources of the request queue.
883 * The struct request_queue refcount is incremented with blk_get_queue() and
884 * decremented with blk_put_queue(). Once the refcount reaches 0 this function
887 * For drivers that have a request_queue on a gendisk and added with
888 * __device_add_disk() the refcount to request_queue will reach 0 with
889 * the last put_disk() called by the driver. For drivers which don't use
890 * __device_add_disk() this happens with blk_cleanup_queue().
892 * Drivers exist which depend on the release of the request_queue to be
893 * synchronous, it should not be deferred.
897 static void blk_release_queue(struct kobject *kobj)
899 struct request_queue *q =
900 container_of(kobj, struct request_queue, kobj);
904 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags))
905 blk_stat_remove_callback(q, q->poll_cb);
906 blk_stat_free_callback(q->poll_cb);
908 blk_free_queue_stats(q->stats);
911 cancel_delayed_work_sync(&q->requeue_work);
915 blk_queue_free_zone_bitmaps(q);
920 blk_trace_shutdown(q);
921 mutex_lock(&q->debugfs_mutex);
922 debugfs_remove_recursive(q->debugfs_dir);
923 mutex_unlock(&q->debugfs_mutex);
926 blk_mq_debugfs_unregister(q);
928 bioset_exit(&q->bio_split);
930 ida_simple_remove(&blk_queue_ida, q->id);
931 call_rcu(&q->rcu_head, blk_free_queue_rcu);
934 static const struct sysfs_ops queue_sysfs_ops = {
935 .show = queue_attr_show,
936 .store = queue_attr_store,
939 struct kobj_type blk_queue_ktype = {
940 .sysfs_ops = &queue_sysfs_ops,
941 .release = blk_release_queue,
945 * blk_register_queue - register a block layer queue with sysfs
946 * @disk: Disk of which the request queue should be registered with sysfs.
948 int blk_register_queue(struct gendisk *disk)
951 struct device *dev = disk_to_dev(disk);
952 struct request_queue *q = disk->queue;
953 bool has_elevator = false;
958 WARN_ONCE(blk_queue_registered(q),
959 "%s is registering an already registered queue\n",
960 kobject_name(&dev->kobj));
963 * SCSI probing may synchronously create and destroy a lot of
964 * request_queues for non-existent devices. Shutting down a fully
965 * functional queue takes measureable wallclock time as RCU grace
966 * periods are involved. To avoid excessive latency in these
967 * cases, a request_queue starts out in a degraded mode which is
968 * faster to shut down and is made fully functional here as
969 * request_queues for non-existent devices never get registered.
971 if (!blk_queue_init_done(q)) {
972 blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
973 percpu_ref_switch_to_percpu(&q->q_usage_counter);
976 ret = blk_trace_init_sysfs(dev);
980 mutex_lock(&q->sysfs_dir_lock);
982 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
984 blk_trace_remove_sysfs(dev);
988 ret = sysfs_create_group(&q->kobj, &queue_attr_group);
990 blk_trace_remove_sysfs(dev);
991 kobject_del(&q->kobj);
992 kobject_put(&dev->kobj);
996 mutex_lock(&q->debugfs_mutex);
997 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
999 mutex_unlock(&q->debugfs_mutex);
1001 if (queue_is_mq(q)) {
1002 __blk_mq_register_dev(dev, q);
1003 blk_mq_debugfs_register(q);
1006 mutex_lock(&q->sysfs_lock);
1008 ret = elv_register_queue(q, false);
1010 mutex_unlock(&q->sysfs_lock);
1011 mutex_unlock(&q->sysfs_dir_lock);
1012 kobject_del(&q->kobj);
1013 blk_trace_remove_sysfs(dev);
1014 kobject_put(&dev->kobj);
1017 has_elevator = true;
1020 blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
1021 wbt_enable_default(q);
1022 blk_throtl_register_queue(q);
1024 /* Now everything is ready and send out KOBJ_ADD uevent */
1025 kobject_uevent(&q->kobj, KOBJ_ADD);
1027 kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
1028 mutex_unlock(&q->sysfs_lock);
1032 mutex_unlock(&q->sysfs_dir_lock);
1035 EXPORT_SYMBOL_GPL(blk_register_queue);
1038 * blk_unregister_queue - counterpart of blk_register_queue()
1039 * @disk: Disk of which the request queue should be unregistered from sysfs.
1041 * Note: the caller is responsible for guaranteeing that this function is called
1042 * after blk_register_queue() has finished.
1044 void blk_unregister_queue(struct gendisk *disk)
1046 struct request_queue *q = disk->queue;
1051 /* Return early if disk->queue was never registered. */
1052 if (!blk_queue_registered(q))
1056 * Since sysfs_remove_dir() prevents adding new directory entries
1057 * before removal of existing entries starts, protect against
1058 * concurrent elv_iosched_store() calls.
1060 mutex_lock(&q->sysfs_lock);
1061 blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
1062 mutex_unlock(&q->sysfs_lock);
1064 mutex_lock(&q->sysfs_dir_lock);
1066 * Remove the sysfs attributes before unregistering the queue data
1067 * structures that can be modified through sysfs.
1070 blk_mq_unregister_dev(disk_to_dev(disk), q);
1072 kobject_uevent(&q->kobj, KOBJ_REMOVE);
1073 kobject_del(&q->kobj);
1074 blk_trace_remove_sysfs(disk_to_dev(disk));
1076 mutex_lock(&q->sysfs_lock);
1078 elv_unregister_queue(q);
1079 mutex_unlock(&q->sysfs_lock);
1080 mutex_unlock(&q->sysfs_dir_lock);
1082 kobject_put(&disk_to_dev(disk)->kobj);