A tag set's debugfs directory identifies its attached request queues, but does not identify the controller that owns it. Drivers with an existing controller debugfs directory can expose this relationship without duplicating the controller's debug information. Add blk_mq_tagset_debugfs_link_controller() and blk_mq_tagset_debugfs_unlink_controller() for drivers to manage a controller symlink in the tag set directory. Build a relative target from the controller dentry so the link stays within debugfs and does not depend on the mount point. Serialize link updates with tag set debugfs registration and removal, and use the existing NOIO scope for allocations. Callers must keep both objects alive and remove the link before deleting the controller directory. Provide stubs when CONFIG_BLK_DEBUG_FS is disabled. Signed-off-by: Lei Chen --- block/blk-mq-debugfs.c | 54 ++++++++++++++++++++++++++++++++++++++++++ include/linux/blk-mq.h | 16 +++++++++++++ 2 files changed, 70 insertions(+) diff --git a/block/blk-mq-debugfs.c b/block/blk-mq-debugfs.c index 72634277e088..1a1364a34c0a 100644 --- a/block/blk-mq-debugfs.c +++ b/block/blk-mq-debugfs.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "blk.h" @@ -860,6 +861,59 @@ static void blk_mq_tagset_debugfs_unlock(unsigned int memflags) memalloc_noio_restore(memflags); } +/** + * blk_mq_tagset_debugfs_link_controller - link a tag set to its controller + * @set: allocated tag set + * @controller: controller debugfs directory + * + * The caller must keep both objects alive during this call and remove the + * link before deleting the controller directory. No dentry reference is kept. + */ +void blk_mq_tagset_debugfs_link_controller(struct blk_mq_tag_set *set, + struct dentry *controller) +{ + unsigned int memflags; + char *buf, *path, *target; + + if (IS_ERR(controller)) + return; + + memflags = blk_mq_tagset_debugfs_lock(); + if (IS_ERR_OR_NULL(set->debugfs_dir)) + goto out_unlock; + + buf = kmalloc(PATH_MAX, GFP_KERNEL); + if (!buf) + goto out_unlock; + path = dentry_path_raw(controller, buf, PATH_MAX); + if (IS_ERR(path)) + goto out_free; + + /* The tag set lives two levels below the debugfs root. */ + target = kasprintf(GFP_KERNEL, "../..%s", path); + if (!target) + goto out_free; + debugfs_lookup_and_remove("controller", set->debugfs_dir); + debugfs_create_symlink("controller", set->debugfs_dir, target); + kfree(target); +out_free: + kfree(buf); +out_unlock: + blk_mq_tagset_debugfs_unlock(memflags); +} +EXPORT_SYMBOL_GPL(blk_mq_tagset_debugfs_link_controller); + +void blk_mq_tagset_debugfs_unlink_controller(struct blk_mq_tag_set *set) +{ + unsigned int memflags; + + memflags = blk_mq_tagset_debugfs_lock(); + if (!IS_ERR_OR_NULL(set->debugfs_dir)) + debugfs_lookup_and_remove("controller", set->debugfs_dir); + blk_mq_tagset_debugfs_unlock(memflags); +} +EXPORT_SYMBOL_GPL(blk_mq_tagset_debugfs_unlink_controller); + #define BLK_MQ_F_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name static const char *const tagset_flag_name[] = { BLK_MQ_F_NAME(TAG_QUEUE_SHARED), diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h index 882ed11e7c2b..d98ec1dc1130 100644 --- a/include/linux/blk-mq.h +++ b/include/linux/blk-mq.h @@ -564,6 +564,22 @@ struct blk_mq_tag_set { #endif }; +#ifdef CONFIG_BLK_DEBUG_FS +void blk_mq_tagset_debugfs_link_controller(struct blk_mq_tag_set *set, + struct dentry *controller); +void blk_mq_tagset_debugfs_unlink_controller(struct blk_mq_tag_set *set); +#else +static inline void blk_mq_tagset_debugfs_link_controller( + struct blk_mq_tag_set *set, struct dentry *controller) +{ +} + +static inline void +blk_mq_tagset_debugfs_unlink_controller(struct blk_mq_tag_set *set) +{ +} +#endif + /** * struct blk_mq_queue_data - Data about a request inserted in a queue * -- 2.43.0