Skip to content

Commit 0f98c38

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.dk/linux-block
Pull final block layer fixes from Jens Axboe: "Unfortunately the hctx/ctx lifetime fix from last pull had some issues. This pull request contains a revert of the problematic commit, and a proper rewrite of it. The rewrite has been tested by the users complaining about the regression, and it works fine now. Additionally, I've run testing on all the blk-mq use cases for it and it passes. So we should definitely get this into 3.19, to avoid regression for some cases" * 'for-linus' of git://git.kernel.dk/linux-block: blk-mq: release mq's kobjects in blk_release_queue() Revert "blk-mq: fix hctx/ctx kobject use-after-free"
2 parents 0dc17d1 + e09aae7 commit 0f98c38

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

block/blk-mq-sysfs.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@
1515

1616
static void blk_mq_sysfs_release(struct kobject *kobj)
1717
{
18-
struct request_queue *q;
19-
20-
q = container_of(kobj, struct request_queue, mq_kobj);
21-
free_percpu(q->queue_ctx);
22-
}
23-
24-
static void blk_mq_ctx_release(struct kobject *kobj)
25-
{
26-
struct blk_mq_ctx *ctx;
27-
28-
ctx = container_of(kobj, struct blk_mq_ctx, kobj);
29-
kobject_put(&ctx->queue->mq_kobj);
30-
}
31-
32-
static void blk_mq_hctx_release(struct kobject *kobj)
33-
{
34-
struct blk_mq_hw_ctx *hctx;
35-
36-
hctx = container_of(kobj, struct blk_mq_hw_ctx, kobj);
37-
kfree(hctx);
3818
}
3919

4020
struct blk_mq_ctx_sysfs_entry {
@@ -338,13 +318,13 @@ static struct kobj_type blk_mq_ktype = {
338318
static struct kobj_type blk_mq_ctx_ktype = {
339319
.sysfs_ops = &blk_mq_sysfs_ops,
340320
.default_attrs = default_ctx_attrs,
341-
.release = blk_mq_ctx_release,
321+
.release = blk_mq_sysfs_release,
342322
};
343323

344324
static struct kobj_type blk_mq_hw_ktype = {
345325
.sysfs_ops = &blk_mq_hw_sysfs_ops,
346326
.default_attrs = default_hw_ctx_attrs,
347-
.release = blk_mq_hctx_release,
327+
.release = blk_mq_sysfs_release,
348328
};
349329

350330
static void blk_mq_unregister_hctx(struct blk_mq_hw_ctx *hctx)
@@ -375,7 +355,6 @@ static int blk_mq_register_hctx(struct blk_mq_hw_ctx *hctx)
375355
return ret;
376356

377357
hctx_for_each_ctx(hctx, ctx, i) {
378-
kobject_get(&q->mq_kobj);
379358
ret = kobject_add(&ctx->kobj, &hctx->kobj, "cpu%u", ctx->cpu);
380359
if (ret)
381360
break;

block/blk-mq.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,27 @@ static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
18671867
mutex_unlock(&set->tag_list_lock);
18681868
}
18691869

1870+
/*
1871+
* It is the actual release handler for mq, but we do it from
1872+
* request queue's release handler for avoiding use-after-free
1873+
* and headache because q->mq_kobj shouldn't have been introduced,
1874+
* but we can't group ctx/kctx kobj without it.
1875+
*/
1876+
void blk_mq_release(struct request_queue *q)
1877+
{
1878+
struct blk_mq_hw_ctx *hctx;
1879+
unsigned int i;
1880+
1881+
/* hctx kobj stays in hctx */
1882+
queue_for_each_hw_ctx(q, hctx, i)
1883+
kfree(hctx);
1884+
1885+
kfree(q->queue_hw_ctx);
1886+
1887+
/* ctx kobj stays in queue_ctx */
1888+
free_percpu(q->queue_ctx);
1889+
}
1890+
18701891
struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
18711892
{
18721893
struct blk_mq_hw_ctx **hctxs;
@@ -2000,10 +2021,8 @@ void blk_mq_free_queue(struct request_queue *q)
20002021

20012022
percpu_ref_exit(&q->mq_usage_counter);
20022023

2003-
kfree(q->queue_hw_ctx);
20042024
kfree(q->mq_map);
20052025

2006-
q->queue_hw_ctx = NULL;
20072026
q->mq_map = NULL;
20082027

20092028
mutex_lock(&all_q_mutex);

block/blk-mq.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ extern void blk_mq_sysfs_unregister(struct request_queue *q);
6262

6363
extern void blk_mq_rq_timed_out(struct request *req, bool reserved);
6464

65+
void blk_mq_release(struct request_queue *q);
66+
6567
/*
6668
* Basic implementation of sparser bitmap, allowing the user to spread
6769
* the bits over more cachelines.

block/blk-sysfs.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ static void blk_release_queue(struct kobject *kobj)
517517

518518
if (!q->mq_ops)
519519
blk_free_flush_queue(q->fq);
520+
else
521+
blk_mq_release(q);
520522

521523
blk_trace_shutdown(q);
522524

0 commit comments

Comments
 (0)