Skip to content

Commit 85e0cbb

Browse files
mcgrofaxboe
authored andcommitted
block: create the request_queue debugfs_dir on registration
We were only creating the request_queue debugfs_dir only for make_request block drivers (multiqueue), but never for request-based block drivers. We did this as we were only creating non-blktrace additional debugfs files on that directory for make_request drivers. However, since blktrace *always* creates that directory anyway, we special-case the use of that directory on blktrace. Other than this being an eye-sore, this exposes request-based block drivers to the same debugfs fragile race that used to exist with make_request block drivers where if we start adding files onto that directory we can later run a race with a double removal of dentries on the directory if we don't deal with this carefully on blktrace. Instead, just simplify things by always creating the request_queue debugfs_dir on request_queue registration. Rename the mutex also to reflect the fact that this is used outside of the blktrace context. Signed-off-by: Luis Chamberlain <mcgrof@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent b431ef8 commit 85e0cbb

6 files changed

Lines changed: 39 additions & 48 deletions

File tree

‎block/blk-core.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@
5151
#include "blk-pm.h"
5252
#include "blk-rq-qos.h"
5353

54-
#ifdef CONFIG_DEBUG_FS
5554
struct dentry *blk_debugfs_root;
56-
#endif
5755

5856
EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
5957
EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
@@ -555,9 +553,7 @@ struct request_queue *__blk_alloc_queue(int node_id)
555553

556554
kobject_init(&q->kobj, &blk_queue_ktype);
557555

558-
#ifdef CONFIG_BLK_DEV_IO_TRACE
559-
mutex_init(&q->blk_trace_mutex);
560-
#endif
556+
mutex_init(&q->debugfs_mutex);
561557
mutex_init(&q->sysfs_lock);
562558
mutex_init(&q->sysfs_dir_lock);
563559
spin_lock_init(&q->queue_lock);
@@ -1931,9 +1927,7 @@ int __init blk_dev_init(void)
19311927
blk_requestq_cachep = kmem_cache_create("request_queue",
19321928
sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
19331929

1934-
#ifdef CONFIG_DEBUG_FS
19351930
blk_debugfs_root = debugfs_create_dir("block", NULL);
1936-
#endif
19371931

19381932
return 0;
19391933
}

‎block/blk-mq-debugfs.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,6 @@ void blk_mq_debugfs_register(struct request_queue *q)
824824
struct blk_mq_hw_ctx *hctx;
825825
int i;
826826

827-
q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
828-
blk_debugfs_root);
829-
830827
debugfs_create_files(q->debugfs_dir, q, blk_mq_debugfs_queue_attrs);
831828

832829
/*
@@ -857,9 +854,7 @@ void blk_mq_debugfs_register(struct request_queue *q)
857854

858855
void blk_mq_debugfs_unregister(struct request_queue *q)
859856
{
860-
debugfs_remove_recursive(q->debugfs_dir);
861857
q->sched_debugfs_dir = NULL;
862-
q->debugfs_dir = NULL;
863858
}
864859

865860
static void blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,

‎block/blk-sysfs.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/blktrace_api.h>
1212
#include <linux/blk-mq.h>
1313
#include <linux/blk-cgroup.h>
14+
#include <linux/debugfs.h>
1415

1516
#include "blk.h"
1617
#include "blk-mq.h"
@@ -917,6 +918,9 @@ static void blk_release_queue(struct kobject *kobj)
917918
blk_mq_release(q);
918919

919920
blk_trace_shutdown(q);
921+
mutex_lock(&q->debugfs_mutex);
922+
debugfs_remove_recursive(q->debugfs_dir);
923+
mutex_unlock(&q->debugfs_mutex);
920924

921925
if (queue_is_mq(q))
922926
blk_mq_debugfs_unregister(q);
@@ -989,6 +993,11 @@ int blk_register_queue(struct gendisk *disk)
989993
goto unlock;
990994
}
991995

996+
mutex_lock(&q->debugfs_mutex);
997+
q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
998+
blk_debugfs_root);
999+
mutex_unlock(&q->debugfs_mutex);
1000+
9921001
if (queue_is_mq(q)) {
9931002
__blk_mq_register_dev(dev, q);
9941003
blk_mq_debugfs_register(q);

‎block/blk.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
/* Max future timer expiry for timeouts */
1515
#define BLK_MAX_TIMEOUT (5 * HZ)
1616

17-
#ifdef CONFIG_DEBUG_FS
1817
extern struct dentry *blk_debugfs_root;
19-
#endif
2018

2119
struct blk_flush_queue {
2220
unsigned int flush_pending_idx:1;

‎include/linux/blkdev.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,9 @@ struct request_queue {
528528
unsigned int sg_timeout;
529529
unsigned int sg_reserved_size;
530530
int node;
531+
struct mutex debugfs_mutex;
531532
#ifdef CONFIG_BLK_DEV_IO_TRACE
532533
struct blk_trace __rcu *blk_trace;
533-
struct mutex blk_trace_mutex;
534534
#endif
535535
/*
536536
* for flush operations
@@ -574,8 +574,9 @@ struct request_queue {
574574
struct list_head tag_set_list;
575575
struct bio_set bio_split;
576576

577-
#ifdef CONFIG_BLK_DEBUG_FS
578577
struct dentry *debugfs_dir;
578+
579+
#ifdef CONFIG_BLK_DEBUG_FS
579580
struct dentry *sched_debugfs_dir;
580581
struct dentry *rqos_debugfs_dir;
581582
#endif

‎kernel/trace/blktrace.c‎

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ static int __blk_trace_remove(struct request_queue *q)
348348
struct blk_trace *bt;
349349

350350
bt = rcu_replace_pointer(q->blk_trace, NULL,
351-
lockdep_is_held(&q->blk_trace_mutex));
351+
lockdep_is_held(&q->debugfs_mutex));
352352
if (!bt)
353353
return -EINVAL;
354354

@@ -362,9 +362,9 @@ int blk_trace_remove(struct request_queue *q)
362362
{
363363
int ret;
364364

365-
mutex_lock(&q->blk_trace_mutex);
365+
mutex_lock(&q->debugfs_mutex);
366366
ret = __blk_trace_remove(q);
367-
mutex_unlock(&q->blk_trace_mutex);
367+
mutex_unlock(&q->debugfs_mutex);
368368

369369
return ret;
370370
}
@@ -483,14 +483,11 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
483483
struct dentry *dir = NULL;
484484
int ret;
485485

486-
lockdep_assert_held(&q->blk_trace_mutex);
486+
lockdep_assert_held(&q->debugfs_mutex);
487487

488488
if (!buts->buf_size || !buts->buf_nr)
489489
return -EINVAL;
490490

491-
if (!blk_debugfs_root)
492-
return -ENOENT;
493-
494491
strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
495492
buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
496493

@@ -505,7 +502,7 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
505502
* we can be.
506503
*/
507504
if (rcu_dereference_protected(q->blk_trace,
508-
lockdep_is_held(&q->blk_trace_mutex))) {
505+
lockdep_is_held(&q->debugfs_mutex))) {
509506
pr_warn("Concurrent blktraces are not allowed on %s\n",
510507
buts->name);
511508
return -EBUSY;
@@ -524,18 +521,15 @@ static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
524521
if (!bt->msg_data)
525522
goto err;
526523

527-
#ifdef CONFIG_BLK_DEBUG_FS
528524
/*
529-
* When tracing whole make_request drivers (multiqueue) block devices,
530-
* reuse the existing debugfs directory created by the block layer on
531-
* init. For request-based block devices, all partitions block devices,
525+
* When tracing the whole disk reuse the existing debugfs directory
526+
* created by the block layer on init. For partitions block devices,
532527
* and scsi-generic block devices we create a temporary new debugfs
533528
* directory that will be removed once the trace ends.
534529
*/
535-
if (queue_is_mq(q) && bdev && bdev == bdev->bd_contains)
530+
if (bdev && bdev == bdev->bd_contains)
536531
dir = q->debugfs_dir;
537532
else
538-
#endif
539533
bt->dir = dir = debugfs_create_dir(buts->name, blk_debugfs_root);
540534

541535
/*
@@ -617,9 +611,9 @@ int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
617611
{
618612
int ret;
619613

620-
mutex_lock(&q->blk_trace_mutex);
614+
mutex_lock(&q->debugfs_mutex);
621615
ret = __blk_trace_setup(q, name, dev, bdev, arg);
622-
mutex_unlock(&q->blk_trace_mutex);
616+
mutex_unlock(&q->debugfs_mutex);
623617

624618
return ret;
625619
}
@@ -665,7 +659,7 @@ static int __blk_trace_startstop(struct request_queue *q, int start)
665659
struct blk_trace *bt;
666660

667661
bt = rcu_dereference_protected(q->blk_trace,
668-
lockdep_is_held(&q->blk_trace_mutex));
662+
lockdep_is_held(&q->debugfs_mutex));
669663
if (bt == NULL)
670664
return -EINVAL;
671665

@@ -705,9 +699,9 @@ int blk_trace_startstop(struct request_queue *q, int start)
705699
{
706700
int ret;
707701

708-
mutex_lock(&q->blk_trace_mutex);
702+
mutex_lock(&q->debugfs_mutex);
709703
ret = __blk_trace_startstop(q, start);
710-
mutex_unlock(&q->blk_trace_mutex);
704+
mutex_unlock(&q->debugfs_mutex);
711705

712706
return ret;
713707
}
@@ -736,7 +730,7 @@ int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
736730
if (!q)
737731
return -ENXIO;
738732

739-
mutex_lock(&q->blk_trace_mutex);
733+
mutex_lock(&q->debugfs_mutex);
740734

741735
switch (cmd) {
742736
case BLKTRACESETUP:
@@ -763,7 +757,7 @@ int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
763757
break;
764758
}
765759

766-
mutex_unlock(&q->blk_trace_mutex);
760+
mutex_unlock(&q->debugfs_mutex);
767761
return ret;
768762
}
769763

@@ -774,14 +768,14 @@ int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
774768
**/
775769
void blk_trace_shutdown(struct request_queue *q)
776770
{
777-
mutex_lock(&q->blk_trace_mutex);
771+
mutex_lock(&q->debugfs_mutex);
778772
if (rcu_dereference_protected(q->blk_trace,
779-
lockdep_is_held(&q->blk_trace_mutex))) {
773+
lockdep_is_held(&q->debugfs_mutex))) {
780774
__blk_trace_startstop(q, 0);
781775
__blk_trace_remove(q);
782776
}
783777

784-
mutex_unlock(&q->blk_trace_mutex);
778+
mutex_unlock(&q->debugfs_mutex);
785779
}
786780

787781
#ifdef CONFIG_BLK_CGROUP
@@ -1662,7 +1656,7 @@ static int blk_trace_remove_queue(struct request_queue *q)
16621656
struct blk_trace *bt;
16631657

16641658
bt = rcu_replace_pointer(q->blk_trace, NULL,
1665-
lockdep_is_held(&q->blk_trace_mutex));
1659+
lockdep_is_held(&q->debugfs_mutex));
16661660
if (bt == NULL)
16671661
return -EINVAL;
16681662

@@ -1837,10 +1831,10 @@ static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
18371831
if (q == NULL)
18381832
goto out_bdput;
18391833

1840-
mutex_lock(&q->blk_trace_mutex);
1834+
mutex_lock(&q->debugfs_mutex);
18411835

18421836
bt = rcu_dereference_protected(q->blk_trace,
1843-
lockdep_is_held(&q->blk_trace_mutex));
1837+
lockdep_is_held(&q->debugfs_mutex));
18441838
if (attr == &dev_attr_enable) {
18451839
ret = sprintf(buf, "%u\n", !!bt);
18461840
goto out_unlock_bdev;
@@ -1858,7 +1852,7 @@ static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
18581852
ret = sprintf(buf, "%llu\n", bt->end_lba);
18591853

18601854
out_unlock_bdev:
1861-
mutex_unlock(&q->blk_trace_mutex);
1855+
mutex_unlock(&q->debugfs_mutex);
18621856
out_bdput:
18631857
bdput(bdev);
18641858
out:
@@ -1901,10 +1895,10 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
19011895
if (q == NULL)
19021896
goto out_bdput;
19031897

1904-
mutex_lock(&q->blk_trace_mutex);
1898+
mutex_lock(&q->debugfs_mutex);
19051899

19061900
bt = rcu_dereference_protected(q->blk_trace,
1907-
lockdep_is_held(&q->blk_trace_mutex));
1901+
lockdep_is_held(&q->debugfs_mutex));
19081902
if (attr == &dev_attr_enable) {
19091903
if (!!value == !!bt) {
19101904
ret = 0;
@@ -1921,7 +1915,7 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
19211915
if (bt == NULL) {
19221916
ret = blk_trace_setup_queue(q, bdev);
19231917
bt = rcu_dereference_protected(q->blk_trace,
1924-
lockdep_is_held(&q->blk_trace_mutex));
1918+
lockdep_is_held(&q->debugfs_mutex));
19251919
}
19261920

19271921
if (ret == 0) {
@@ -1936,7 +1930,7 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
19361930
}
19371931

19381932
out_unlock_bdev:
1939-
mutex_unlock(&q->blk_trace_mutex);
1933+
mutex_unlock(&q->debugfs_mutex);
19401934
out_bdput:
19411935
bdput(bdev);
19421936
out:

0 commit comments

Comments
 (0)