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/debugfs.h>
17 #include "blk-mq-debugfs.h"
18 #include "blk-mq-sched.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 request_queue *, char *);
26 ssize_t (*store)(struct request_queue *, const char *, size_t);
30 queue_var_show(unsigned long var, char *page)
32 return sprintf(page, "%lu\n", var);
36 queue_var_store(unsigned long *var, const char *page, size_t count)
41 err = kstrtoul(page, 10, &v);
42 if (err || v > UINT_MAX)
50 static ssize_t queue_var_store64(s64 *var, const char *page)
55 err = kstrtos64(page, 10, &v);
63 static ssize_t queue_requests_show(struct request_queue *q, char *page)
65 return queue_var_show(q->nr_requests, page);
69 queue_requests_store(struct request_queue *q, const char *page, size_t count)
77 ret = queue_var_store(&nr, page, count);
81 if (nr < BLKDEV_MIN_RQ)
84 err = blk_mq_update_nr_requests(q, nr);
91 static ssize_t queue_ra_show(struct request_queue *q, char *page)
97 ra_kb = q->disk->bdi->ra_pages << (PAGE_SHIFT - 10);
98 return queue_var_show(ra_kb, page);
102 queue_ra_store(struct request_queue *q, const char *page, size_t count)
109 ret = queue_var_store(&ra_kb, page, count);
112 q->disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
116 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
118 int max_sectors_kb = queue_max_sectors(q) >> 1;
120 return queue_var_show(max_sectors_kb, page);
123 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
125 return queue_var_show(queue_max_segments(q), page);
128 static ssize_t queue_max_discard_segments_show(struct request_queue *q,
131 return queue_var_show(queue_max_discard_segments(q), page);
134 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
136 return queue_var_show(q->limits.max_integrity_segments, page);
139 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
141 return queue_var_show(queue_max_segment_size(q), page);
144 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
146 return queue_var_show(queue_logical_block_size(q), page);
149 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
151 return queue_var_show(queue_physical_block_size(q), page);
154 static ssize_t queue_chunk_sectors_show(struct request_queue *q, char *page)
156 return queue_var_show(q->limits.chunk_sectors, page);
159 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
161 return queue_var_show(queue_io_min(q), page);
164 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
166 return queue_var_show(queue_io_opt(q), page);
169 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
171 return queue_var_show(q->limits.discard_granularity, page);
174 static ssize_t queue_discard_max_hw_show(struct request_queue *q, char *page)
177 return sprintf(page, "%llu\n",
178 (unsigned long long)q->limits.max_hw_discard_sectors << 9);
181 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
183 return sprintf(page, "%llu\n",
184 (unsigned long long)q->limits.max_discard_sectors << 9);
187 static ssize_t queue_discard_max_store(struct request_queue *q,
188 const char *page, size_t count)
190 unsigned long max_discard;
191 ssize_t ret = queue_var_store(&max_discard, page, count);
196 if (max_discard & (q->limits.discard_granularity - 1))
200 if (max_discard > UINT_MAX)
203 if (max_discard > q->limits.max_hw_discard_sectors)
204 max_discard = q->limits.max_hw_discard_sectors;
206 q->limits.max_discard_sectors = max_discard;
210 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
212 return queue_var_show(0, page);
215 static ssize_t queue_write_same_max_show(struct request_queue *q, char *page)
217 return queue_var_show(0, page);
220 static ssize_t queue_write_zeroes_max_show(struct request_queue *q, char *page)
222 return sprintf(page, "%llu\n",
223 (unsigned long long)q->limits.max_write_zeroes_sectors << 9);
226 static ssize_t queue_zone_write_granularity_show(struct request_queue *q,
229 return queue_var_show(queue_zone_write_granularity(q), page);
232 static ssize_t queue_zone_append_max_show(struct request_queue *q, char *page)
234 unsigned long long max_sectors = q->limits.max_zone_append_sectors;
236 return sprintf(page, "%llu\n", max_sectors << SECTOR_SHIFT);
240 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
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);
250 max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
251 q->limits.max_dev_sectors >> 1);
253 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
256 spin_lock_irq(&q->queue_lock);
257 q->limits.max_sectors = max_sectors_kb << 1;
259 q->disk->bdi->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
260 spin_unlock_irq(&q->queue_lock);
265 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
267 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
269 return queue_var_show(max_hw_sectors_kb, page);
272 static ssize_t queue_virt_boundary_mask_show(struct request_queue *q, char *page)
274 return queue_var_show(q->limits.virt_boundary_mask, page);
277 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
279 queue_##name##_show(struct request_queue *q, char *page) \
282 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
283 return queue_var_show(neg ? !bit : bit, page); \
286 queue_##name##_store(struct request_queue *q, const char *page, size_t count) \
290 ret = queue_var_store(&val, page, count); \
297 blk_queue_flag_set(QUEUE_FLAG_##flag, q); \
299 blk_queue_flag_clear(QUEUE_FLAG_##flag, q); \
303 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
304 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
305 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
306 QUEUE_SYSFS_BIT_FNS(stable_writes, STABLE_WRITES, 0);
307 #undef QUEUE_SYSFS_BIT_FNS
309 static ssize_t queue_zoned_show(struct request_queue *q, char *page)
311 switch (blk_queue_zoned_model(q)) {
313 return sprintf(page, "host-aware\n");
315 return sprintf(page, "host-managed\n");
317 return sprintf(page, "none\n");
321 static ssize_t queue_nr_zones_show(struct request_queue *q, char *page)
323 return queue_var_show(blk_queue_nr_zones(q), page);
326 static ssize_t queue_max_open_zones_show(struct request_queue *q, char *page)
328 return queue_var_show(queue_max_open_zones(q), page);
331 static ssize_t queue_max_active_zones_show(struct request_queue *q, char *page)
333 return queue_var_show(queue_max_active_zones(q), page);
336 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
338 return queue_var_show((blk_queue_nomerges(q) << 1) |
339 blk_queue_noxmerges(q), page);
342 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
346 ssize_t ret = queue_var_store(&nm, page, count);
351 blk_queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
352 blk_queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
354 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
356 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
361 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
363 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
364 bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
366 return queue_var_show(set << force, page);
370 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
372 ssize_t ret = -EINVAL;
376 ret = queue_var_store(&val, page, count);
381 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
382 blk_queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
383 } else if (val == 1) {
384 blk_queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
385 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
386 } else if (val == 0) {
387 blk_queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
388 blk_queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
394 static ssize_t queue_poll_delay_show(struct request_queue *q, char *page)
398 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
399 val = BLK_MQ_POLL_CLASSIC;
401 val = q->poll_nsec / 1000;
403 return sprintf(page, "%d\n", val);
406 static ssize_t queue_poll_delay_store(struct request_queue *q, const char *page,
411 if (!q->mq_ops || !q->mq_ops->poll)
414 err = kstrtoint(page, 10, &val);
418 if (val == BLK_MQ_POLL_CLASSIC)
419 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
421 q->poll_nsec = val * 1000;
428 static ssize_t queue_poll_show(struct request_queue *q, char *page)
430 return queue_var_show(test_bit(QUEUE_FLAG_POLL, &q->queue_flags), page);
433 static ssize_t queue_poll_store(struct request_queue *q, const char *page,
436 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
438 pr_info_ratelimited("writes to the poll attribute are ignored.\n");
439 pr_info_ratelimited("please use driver specific parameters instead.\n");
443 static ssize_t queue_io_timeout_show(struct request_queue *q, char *page)
445 return sprintf(page, "%u\n", jiffies_to_msecs(q->rq_timeout));
448 static ssize_t queue_io_timeout_store(struct request_queue *q, const char *page,
454 err = kstrtou32(page, 10, &val);
458 blk_queue_rq_timeout(q, msecs_to_jiffies(val));
463 static ssize_t queue_wb_lat_show(struct request_queue *q, char *page)
468 return sprintf(page, "%llu\n", div_u64(wbt_get_min_lat(q), 1000));
471 static ssize_t queue_wb_lat_store(struct request_queue *q, const char *page,
478 ret = queue_var_store64(&val, page);
484 rqos = wbt_rq_qos(q);
492 val = wbt_default_latency_nsec(q);
496 if (wbt_get_min_lat(q) == val)
500 * Ensure that the queue is idled, in case the latency update
501 * ends up either enabling or disabling wbt completely. We can't
502 * have IO inflight if that happens.
504 blk_mq_freeze_queue(q);
505 blk_mq_quiesce_queue(q);
507 wbt_set_min_lat(q, val);
509 blk_mq_unquiesce_queue(q);
510 blk_mq_unfreeze_queue(q);
515 static ssize_t queue_wc_show(struct request_queue *q, char *page)
517 if (test_bit(QUEUE_FLAG_WC, &q->queue_flags))
518 return sprintf(page, "write back\n");
520 return sprintf(page, "write through\n");
523 static ssize_t queue_wc_store(struct request_queue *q, const char *page,
528 if (!strncmp(page, "write back", 10))
530 else if (!strncmp(page, "write through", 13) ||
531 !strncmp(page, "none", 4))
538 blk_queue_flag_set(QUEUE_FLAG_WC, q);
540 blk_queue_flag_clear(QUEUE_FLAG_WC, q);
545 static ssize_t queue_fua_show(struct request_queue *q, char *page)
547 return sprintf(page, "%u\n", test_bit(QUEUE_FLAG_FUA, &q->queue_flags));
550 static ssize_t queue_dax_show(struct request_queue *q, char *page)
552 return queue_var_show(blk_queue_dax(q), page);
555 #define QUEUE_RO_ENTRY(_prefix, _name) \
556 static struct queue_sysfs_entry _prefix##_entry = { \
557 .attr = { .name = _name, .mode = 0444 }, \
558 .show = _prefix##_show, \
561 #define QUEUE_RW_ENTRY(_prefix, _name) \
562 static struct queue_sysfs_entry _prefix##_entry = { \
563 .attr = { .name = _name, .mode = 0644 }, \
564 .show = _prefix##_show, \
565 .store = _prefix##_store, \
568 QUEUE_RW_ENTRY(queue_requests, "nr_requests");
569 QUEUE_RW_ENTRY(queue_ra, "read_ahead_kb");
570 QUEUE_RW_ENTRY(queue_max_sectors, "max_sectors_kb");
571 QUEUE_RO_ENTRY(queue_max_hw_sectors, "max_hw_sectors_kb");
572 QUEUE_RO_ENTRY(queue_max_segments, "max_segments");
573 QUEUE_RO_ENTRY(queue_max_integrity_segments, "max_integrity_segments");
574 QUEUE_RO_ENTRY(queue_max_segment_size, "max_segment_size");
575 QUEUE_RW_ENTRY(elv_iosched, "scheduler");
577 QUEUE_RO_ENTRY(queue_logical_block_size, "logical_block_size");
578 QUEUE_RO_ENTRY(queue_physical_block_size, "physical_block_size");
579 QUEUE_RO_ENTRY(queue_chunk_sectors, "chunk_sectors");
580 QUEUE_RO_ENTRY(queue_io_min, "minimum_io_size");
581 QUEUE_RO_ENTRY(queue_io_opt, "optimal_io_size");
583 QUEUE_RO_ENTRY(queue_max_discard_segments, "max_discard_segments");
584 QUEUE_RO_ENTRY(queue_discard_granularity, "discard_granularity");
585 QUEUE_RO_ENTRY(queue_discard_max_hw, "discard_max_hw_bytes");
586 QUEUE_RW_ENTRY(queue_discard_max, "discard_max_bytes");
587 QUEUE_RO_ENTRY(queue_discard_zeroes_data, "discard_zeroes_data");
589 QUEUE_RO_ENTRY(queue_write_same_max, "write_same_max_bytes");
590 QUEUE_RO_ENTRY(queue_write_zeroes_max, "write_zeroes_max_bytes");
591 QUEUE_RO_ENTRY(queue_zone_append_max, "zone_append_max_bytes");
592 QUEUE_RO_ENTRY(queue_zone_write_granularity, "zone_write_granularity");
594 QUEUE_RO_ENTRY(queue_zoned, "zoned");
595 QUEUE_RO_ENTRY(queue_nr_zones, "nr_zones");
596 QUEUE_RO_ENTRY(queue_max_open_zones, "max_open_zones");
597 QUEUE_RO_ENTRY(queue_max_active_zones, "max_active_zones");
599 QUEUE_RW_ENTRY(queue_nomerges, "nomerges");
600 QUEUE_RW_ENTRY(queue_rq_affinity, "rq_affinity");
601 QUEUE_RW_ENTRY(queue_poll, "io_poll");
602 QUEUE_RW_ENTRY(queue_poll_delay, "io_poll_delay");
603 QUEUE_RW_ENTRY(queue_wc, "write_cache");
604 QUEUE_RO_ENTRY(queue_fua, "fua");
605 QUEUE_RO_ENTRY(queue_dax, "dax");
606 QUEUE_RW_ENTRY(queue_io_timeout, "io_timeout");
607 QUEUE_RW_ENTRY(queue_wb_lat, "wbt_lat_usec");
608 QUEUE_RO_ENTRY(queue_virt_boundary_mask, "virt_boundary_mask");
610 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
611 QUEUE_RW_ENTRY(blk_throtl_sample_time, "throttle_sample_time");
614 /* legacy alias for logical_block_size: */
615 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
616 .attr = {.name = "hw_sector_size", .mode = 0444 },
617 .show = queue_logical_block_size_show,
620 QUEUE_RW_ENTRY(queue_nonrot, "rotational");
621 QUEUE_RW_ENTRY(queue_iostats, "iostats");
622 QUEUE_RW_ENTRY(queue_random, "add_random");
623 QUEUE_RW_ENTRY(queue_stable_writes, "stable_writes");
625 static struct attribute *queue_attrs[] = {
626 &queue_requests_entry.attr,
627 &queue_ra_entry.attr,
628 &queue_max_hw_sectors_entry.attr,
629 &queue_max_sectors_entry.attr,
630 &queue_max_segments_entry.attr,
631 &queue_max_discard_segments_entry.attr,
632 &queue_max_integrity_segments_entry.attr,
633 &queue_max_segment_size_entry.attr,
634 &elv_iosched_entry.attr,
635 &queue_hw_sector_size_entry.attr,
636 &queue_logical_block_size_entry.attr,
637 &queue_physical_block_size_entry.attr,
638 &queue_chunk_sectors_entry.attr,
639 &queue_io_min_entry.attr,
640 &queue_io_opt_entry.attr,
641 &queue_discard_granularity_entry.attr,
642 &queue_discard_max_entry.attr,
643 &queue_discard_max_hw_entry.attr,
644 &queue_discard_zeroes_data_entry.attr,
645 &queue_write_same_max_entry.attr,
646 &queue_write_zeroes_max_entry.attr,
647 &queue_zone_append_max_entry.attr,
648 &queue_zone_write_granularity_entry.attr,
649 &queue_nonrot_entry.attr,
650 &queue_zoned_entry.attr,
651 &queue_nr_zones_entry.attr,
652 &queue_max_open_zones_entry.attr,
653 &queue_max_active_zones_entry.attr,
654 &queue_nomerges_entry.attr,
655 &queue_rq_affinity_entry.attr,
656 &queue_iostats_entry.attr,
657 &queue_stable_writes_entry.attr,
658 &queue_random_entry.attr,
659 &queue_poll_entry.attr,
660 &queue_wc_entry.attr,
661 &queue_fua_entry.attr,
662 &queue_dax_entry.attr,
663 &queue_wb_lat_entry.attr,
664 &queue_poll_delay_entry.attr,
665 &queue_io_timeout_entry.attr,
666 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
667 &blk_throtl_sample_time_entry.attr,
669 &queue_virt_boundary_mask_entry.attr,
673 static umode_t queue_attr_visible(struct kobject *kobj, struct attribute *attr,
676 struct request_queue *q =
677 container_of(kobj, struct request_queue, kobj);
679 if (attr == &queue_io_timeout_entry.attr &&
680 (!q->mq_ops || !q->mq_ops->timeout))
683 if ((attr == &queue_max_open_zones_entry.attr ||
684 attr == &queue_max_active_zones_entry.attr) &&
685 !blk_queue_is_zoned(q))
691 static struct attribute_group queue_attr_group = {
692 .attrs = queue_attrs,
693 .is_visible = queue_attr_visible,
697 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
700 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
702 struct queue_sysfs_entry *entry = to_queue(attr);
703 struct request_queue *q =
704 container_of(kobj, struct request_queue, kobj);
709 mutex_lock(&q->sysfs_lock);
710 res = entry->show(q, page);
711 mutex_unlock(&q->sysfs_lock);
716 queue_attr_store(struct kobject *kobj, struct attribute *attr,
717 const char *page, size_t length)
719 struct queue_sysfs_entry *entry = to_queue(attr);
720 struct request_queue *q;
726 q = container_of(kobj, struct request_queue, kobj);
727 mutex_lock(&q->sysfs_lock);
728 res = entry->store(q, page, length);
729 mutex_unlock(&q->sysfs_lock);
733 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
735 struct request_queue *q = container_of(rcu_head, struct request_queue,
738 kmem_cache_free(blk_get_queue_kmem_cache(blk_queue_has_srcu(q)), q);
742 * blk_release_queue - releases all allocated resources of the request_queue
743 * @kobj: pointer to a kobject, whose container is a request_queue
745 * This function releases all allocated resources of the request queue.
747 * The struct request_queue refcount is incremented with blk_get_queue() and
748 * decremented with blk_put_queue(). Once the refcount reaches 0 this function
751 * For drivers that have a request_queue on a gendisk and added with
752 * __device_add_disk() the refcount to request_queue will reach 0 with
753 * the last put_disk() called by the driver. For drivers which don't use
754 * __device_add_disk() this happens with blk_cleanup_queue().
756 * Drivers exist which depend on the release of the request_queue to be
757 * synchronous, it should not be deferred.
761 static void blk_release_queue(struct kobject *kobj)
763 struct request_queue *q =
764 container_of(kobj, struct request_queue, kobj);
768 percpu_ref_exit(&q->q_usage_counter);
771 blk_stat_remove_callback(q, q->poll_cb);
772 blk_stat_free_callback(q->poll_cb);
774 blk_free_queue_stats(q->stats);
777 blk_queue_free_zone_bitmaps(q);
782 blk_trace_shutdown(q);
783 mutex_lock(&q->debugfs_mutex);
784 debugfs_remove_recursive(q->debugfs_dir);
785 mutex_unlock(&q->debugfs_mutex);
788 blk_mq_debugfs_unregister(q);
790 bioset_exit(&q->bio_split);
792 if (blk_queue_has_srcu(q))
793 cleanup_srcu_struct(q->srcu);
795 ida_simple_remove(&blk_queue_ida, q->id);
796 call_rcu(&q->rcu_head, blk_free_queue_rcu);
799 static const struct sysfs_ops queue_sysfs_ops = {
800 .show = queue_attr_show,
801 .store = queue_attr_store,
804 struct kobj_type blk_queue_ktype = {
805 .sysfs_ops = &queue_sysfs_ops,
806 .release = blk_release_queue,
810 * blk_register_queue - register a block layer queue with sysfs
811 * @disk: Disk of which the request queue should be registered with sysfs.
813 int blk_register_queue(struct gendisk *disk)
816 struct device *dev = disk_to_dev(disk);
817 struct request_queue *q = disk->queue;
819 ret = blk_trace_init_sysfs(dev);
823 mutex_lock(&q->sysfs_dir_lock);
825 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
827 blk_trace_remove_sysfs(dev);
831 ret = sysfs_create_group(&q->kobj, &queue_attr_group);
833 blk_trace_remove_sysfs(dev);
834 kobject_del(&q->kobj);
835 kobject_put(&dev->kobj);
839 mutex_lock(&q->debugfs_mutex);
840 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
842 mutex_unlock(&q->debugfs_mutex);
844 if (queue_is_mq(q)) {
845 __blk_mq_register_dev(dev, q);
846 blk_mq_debugfs_register(q);
849 mutex_lock(&q->sysfs_lock);
851 ret = disk_register_independent_access_ranges(disk, NULL);
856 ret = elv_register_queue(q, false);
861 ret = blk_crypto_sysfs_register(q);
865 blk_queue_flag_set(QUEUE_FLAG_REGISTERED, q);
866 wbt_enable_default(q);
867 blk_throtl_register_queue(q);
869 /* Now everything is ready and send out KOBJ_ADD uevent */
870 kobject_uevent(&q->kobj, KOBJ_ADD);
872 kobject_uevent(&q->elevator->kobj, KOBJ_ADD);
873 mutex_unlock(&q->sysfs_lock);
876 mutex_unlock(&q->sysfs_dir_lock);
879 * SCSI probing may synchronously create and destroy a lot of
880 * request_queues for non-existent devices. Shutting down a fully
881 * functional queue takes measureable wallclock time as RCU grace
882 * periods are involved. To avoid excessive latency in these
883 * cases, a request_queue starts out in a degraded mode which is
884 * faster to shut down and is made fully functional here as
885 * request_queues for non-existent devices never get registered.
887 if (!blk_queue_init_done(q)) {
888 blk_queue_flag_set(QUEUE_FLAG_INIT_DONE, q);
889 percpu_ref_switch_to_percpu(&q->q_usage_counter);
895 elv_unregister_queue(q);
896 disk_unregister_independent_access_ranges(disk);
897 mutex_unlock(&q->sysfs_lock);
898 mutex_unlock(&q->sysfs_dir_lock);
899 kobject_del(&q->kobj);
900 blk_trace_remove_sysfs(dev);
901 kobject_put(&dev->kobj);
907 * blk_unregister_queue - counterpart of blk_register_queue()
908 * @disk: Disk of which the request queue should be unregistered from sysfs.
910 * Note: the caller is responsible for guaranteeing that this function is called
911 * after blk_register_queue() has finished.
913 void blk_unregister_queue(struct gendisk *disk)
915 struct request_queue *q = disk->queue;
920 /* Return early if disk->queue was never registered. */
921 if (!blk_queue_registered(q))
925 * Since sysfs_remove_dir() prevents adding new directory entries
926 * before removal of existing entries starts, protect against
927 * concurrent elv_iosched_store() calls.
929 mutex_lock(&q->sysfs_lock);
930 blk_queue_flag_clear(QUEUE_FLAG_REGISTERED, q);
931 mutex_unlock(&q->sysfs_lock);
933 mutex_lock(&q->sysfs_dir_lock);
935 * Remove the sysfs attributes before unregistering the queue data
936 * structures that can be modified through sysfs.
939 blk_mq_unregister_dev(disk_to_dev(disk), q);
940 blk_crypto_sysfs_unregister(q);
941 blk_trace_remove_sysfs(disk_to_dev(disk));
943 mutex_lock(&q->sysfs_lock);
944 elv_unregister_queue(q);
945 disk_unregister_independent_access_ranges(disk);
946 mutex_unlock(&q->sysfs_lock);
948 /* Now that we've deleted all child objects, we can delete the queue. */
949 kobject_uevent(&q->kobj, KOBJ_REMOVE);
950 kobject_del(&q->kobj);
952 mutex_unlock(&q->sysfs_dir_lock);
954 kobject_put(&disk_to_dev(disk)->kobj);