Skip to content

Commit 67aec14

Browse files
committed
blk-mq: make the sysfs mq/ layout reflect current mappings
Currently blk-mq registers all the hardware queues in sysfs, regardless of whether it uses them (e.g. they have CPU mappings) or not. The unused hardware queues lack the cpux/ directories, and the other sysfs entries (like active, pending, etc) are all zeroes. Change this so that sysfs correctly reflects the current mappings of the hardware queues. Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 2230237 commit 67aec14

4 files changed

Lines changed: 94 additions & 19 deletions

File tree

‎block/blk-mq-sysfs.c‎

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,42 @@ static struct kobj_type blk_mq_hw_ktype = {
327327
.release = blk_mq_sysfs_release,
328328
};
329329

330+
void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
331+
{
332+
struct blk_mq_ctx *ctx;
333+
int i;
334+
335+
if (!hctx->nr_ctx || !(hctx->flags & BLK_MQ_F_SYSFS_UP))
336+
return;
337+
338+
hctx_for_each_ctx(hctx, ctx, i)
339+
kobject_del(&ctx->kobj);
340+
341+
kobject_del(&hctx->kobj);
342+
}
343+
344+
int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
345+
{
346+
struct request_queue *q = hctx->queue;
347+
struct blk_mq_ctx *ctx;
348+
int i, ret;
349+
350+
if (!hctx->nr_ctx || !(hctx->flags & BLK_MQ_F_SYSFS_UP))
351+
return 0;
352+
353+
ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", hctx->queue_num);
354+
if (ret)
355+
return ret;
356+
357+
hctx_for_each_ctx(hctx, ctx, i) {
358+
ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
359+
if (ret)
360+
break;
361+
}
362+
363+
return ret;
364+
}
365+
330366
void blk_mq_unregister_disk(struct gendisk *disk)
331367
{
332368
struct request_queue *q = disk->queue;
@@ -335,11 +371,11 @@ void blk_mq_unregister_disk(struct gendisk *disk)
335371
int i, j;
336372

337373
queue_for_each_hw_ctx(q, hctx, i) {
338-
hctx_for_each_ctx(hctx, ctx, j) {
339-
kobject_del(&ctx->kobj);
374+
blk_mq_unregister_hctx(hctx);
375+
376+
hctx_for_each_ctx(hctx, ctx, j)
340377
kobject_put(&ctx->kobj);
341-
}
342-
kobject_del(&hctx->kobj);
378+
343379
kobject_put(&hctx->kobj);
344380
}
345381

@@ -350,15 +386,30 @@ void blk_mq_unregister_disk(struct gendisk *disk)
350386
kobject_put(&disk_to_dev(disk)->kobj);
351387
}
352388

389+
static void blk_mq_sysfs_init(struct request_queue *q)
390+
{
391+
struct blk_mq_hw_ctx *hctx;
392+
struct blk_mq_ctx *ctx;
393+
int i, j;
394+
395+
kobject_init(&q->mq_kobj, &blk_mq_ktype);
396+
397+
queue_for_each_hw_ctx(q, hctx, i) {
398+
kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
399+
400+
hctx_for_each_ctx(hctx, ctx, j)
401+
kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
402+
}
403+
}
404+
353405
int blk_mq_register_disk(struct gendisk *disk)
354406
{
355407
struct device *dev = disk_to_dev(disk);
356408
struct request_queue *q = disk->queue;
357409
struct blk_mq_hw_ctx *hctx;
358-
struct blk_mq_ctx *ctx;
359-
int ret, i, j;
410+
int ret, i;
360411

361-
kobject_init(&q->mq_kobj, &blk_mq_ktype);
412+
blk_mq_sysfs_init(q);
362413

363414
ret = kobject_add(&q->mq_kobj, kobject_get(&dev->kobj), "%s", "mq");
364415
if (ret < 0)
@@ -367,20 +418,10 @@ int blk_mq_register_disk(struct gendisk *disk)
367418
kobject_uevent(&q->mq_kobj, KOBJ_ADD);
368419

369420
queue_for_each_hw_ctx(q, hctx, i) {
370-
kobject_init(&hctx->kobj, &blk_mq_hw_ktype);
371-
ret = kobject_add(&hctx->kobj, &q->mq_kobj, "%u", i);
421+
hctx->flags |= BLK_MQ_F_SYSFS_UP;
422+
ret = blk_mq_register_hctx(hctx);
372423
if (ret)
373424
break;
374-
375-
if (!hctx->nr_ctx)
376-
continue;
377-
378-
hctx_for_each_ctx(hctx, ctx, j) {
379-
kobject_init(&ctx->kobj, &blk_mq_ctx_ktype);
380-
ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
381-
if (ret)
382-
break;
383-
}
384425
}
385426

386427
if (ret) {
@@ -390,3 +431,26 @@ int blk_mq_register_disk(struct gendisk *disk)
390431

391432
return 0;
392433
}
434+
435+
void blk_mq_sysfs_unregister(struct request_queue *q)
436+
{
437+
struct blk_mq_hw_ctx *hctx;
438+
int i;
439+
440+
queue_for_each_hw_ctx(q, hctx, i)
441+
blk_mq_unregister_hctx(hctx);
442+
}
443+
444+
int blk_mq_sysfs_register(struct request_queue *q)
445+
{
446+
struct blk_mq_hw_ctx *hctx;
447+
int i, ret = 0;
448+
449+
queue_for_each_hw_ctx(q, hctx, i) {
450+
ret = blk_mq_register_hctx(hctx);
451+
if (ret)
452+
break;
453+
}
454+
455+
return ret;
456+
}

‎block/blk-mq.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,8 @@ static void blk_mq_queue_reinit(struct request_queue *q)
19241924
{
19251925
blk_mq_freeze_queue(q);
19261926

1927+
blk_mq_sysfs_unregister(q);
1928+
19271929
blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
19281930

19291931
/*
@@ -1934,6 +1936,8 @@ static void blk_mq_queue_reinit(struct request_queue *q)
19341936

19351937
blk_mq_map_swqueue(q);
19361938

1939+
blk_mq_sysfs_register(q);
1940+
19371941
blk_mq_unfreeze_queue(q);
19381942
}
19391943

‎block/blk-mq.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ extern unsigned int *blk_mq_make_queue_map(struct blk_mq_tag_set *set);
5454
extern int blk_mq_update_queue_map(unsigned int *map, unsigned int nr_queues);
5555
extern int blk_mq_hw_queue_to_node(unsigned int *map, unsigned int);
5656

57+
/*
58+
* sysfs helpers
59+
*/
60+
extern int blk_mq_sysfs_register(struct request_queue *q);
61+
extern void blk_mq_sysfs_unregister(struct request_queue *q);
62+
5763
/*
5864
* Basic implementation of sparser bitmap, allowing the user to spread
5965
* the bits over more cachelines.

‎include/linux/blk-mq.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ enum {
130130
BLK_MQ_F_SHOULD_SORT = 1 << 1,
131131
BLK_MQ_F_TAG_SHARED = 1 << 2,
132132
BLK_MQ_F_SG_MERGE = 1 << 3,
133+
BLK_MQ_F_SYSFS_UP = 1 << 4,
133134

134135
BLK_MQ_S_STOPPED = 0,
135136
BLK_MQ_S_TAG_ACTIVE = 1,

0 commit comments

Comments
 (0)