From: Jan Kara <jack@suse.cz>
To: <linux-block@vger.kernel.org>
Cc: Paolo Valente <paolo.valente@linaro.org>,
Jens Axboe <axboe@kernel.dk>, "yukuai (C)" <yukuai3@huawei.com>,
Jan Kara <jack@suse.cz>,
stable@vger.kernel.org
Subject: [PATCH 4/4] bfq: Update cgroup information before merging bio
Date: Fri, 14 Jan 2022 18:01:56 +0100 [thread overview]
Message-ID: <20220114170209.8606-4-jack@suse.cz> (raw)
In-Reply-To: <20220114164215.28972-1-jack@suse.cz>
When the process is migrated to a different cgroup (or in case of
writeback just starts submitting bios associated with a different
cgroup) bfq_merge_bio() can operate with stale cgroup information in
bic. Thus the bio can be merged to a request from a different cgroup or
it can result in merging of bfqqs for different cgroups or bfqqs of
already dead cgroups and causing possible use-after-free issues. Fix the
problem by updating cgroup information in bfq_merge_bio().
CC: stable@vger.kernel.org
Fixes: e21b7a0b9887 ("block, bfq: add full hierarchical scheduling and cgroups support")
Signed-off-by: Jan Kara <jack@suse.cz>
---
block/bfq-cgroup.c | 40 ++++++++++++++++++++++------------------
block/bfq-iosched.c | 11 +++++++++--
2 files changed, 31 insertions(+), 20 deletions(-)
diff --git a/block/bfq-cgroup.c b/block/bfq-cgroup.c
index dbc117e00783..f6f5f156b9f2 100644
--- a/block/bfq-cgroup.c
+++ b/block/bfq-cgroup.c
@@ -729,30 +729,34 @@ static struct bfq_group *__bfq_bic_change_cgroup(struct bfq_data *bfqd,
}
if (sync_bfqq) {
- entity = &sync_bfqq->entity;
- if (entity->sched_data != &bfqg->sched_data) {
+ struct bfq_queue *orig_bfqq = sync_bfqq;
+
+ /* Traverse the merge chain to bfqq we will be using */
+ while (sync_bfqq->new_bfqq)
+ sync_bfqq = sync_bfqq->new_bfqq;
+ /*
+ * Target bfqq got moved to a different cgroup or this process
+ * started submitting bios for different cgroup?
+ */
+ if (sync_bfqq->entity.sched_data != &bfqg->sched_data) {
/*
* Was the queue we use merged to a different queue?
- * Detach process from the queue as merge need not be
- * valid anymore. We cannot easily cancel the merge as
- * there may be other processes scheduled to this
- * queue.
+ * Detach process from the queue as the merge is not
+ * valid anymore. We cannot easily just cancel the
+ * merge (by clearing new_bfqq) as there may be other
+ * processes using this queue and holding refs to all
+ * queues below sync_bfqq->new_bfqq. Similarly if the
+ * merge already happened, we need to detach from bfqq
+ * now so that we cannot merge bio to a request from
+ * the old cgroup.
*/
- if (sync_bfqq->new_bfqq) {
- bfq_put_cooperator(sync_bfqq);
- bfq_release_process_ref(bfqd, sync_bfqq);
+ if (orig_bfqq != sync_bfqq || bfq_bfqq_coop(sync_bfqq)) {
+ bfq_put_cooperator(orig_bfqq);
+ bfq_release_process_ref(bfqd, orig_bfqq);
bic_set_bfqq(bic, NULL, 1);
return bfqg;
}
- /*
- * Moving bfqq that is shared with another process?
- * Split the queues at the nearest occasion as the
- * processes can be in different cgroups now.
- */
- if (bfq_bfqq_coop(sync_bfqq)) {
- bic->stably_merged = false;
- bfq_mark_bfqq_split_coop(sync_bfqq);
- }
+ /* We are the only user of this bfqq, just move it */
bfq_bfqq_move(bfqd, sync_bfqq, bfqg);
}
}
diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c
index 361d321b012a..8a088d77a0b6 100644
--- a/block/bfq-iosched.c
+++ b/block/bfq-iosched.c
@@ -2337,10 +2337,17 @@ static bool bfq_bio_merge(struct request_queue *q, struct bio *bio,
spin_lock_irq(&bfqd->lock);
- if (bic)
+ if (bic) {
+ /*
+ * Make sure cgroup info is uptodate for current process before
+ * considering the merge.
+ */
+ bfq_bic_update_cgroup(bic, bio);
+
bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf));
- else
+ } else {
bfqd->bio_bfqq = NULL;
+ }
bfqd->bio_bic = bic;
ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
--
2.31.1
next prev parent reply other threads:[~2022-01-14 17:02 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-14 17:01 [PATCH 0/4 v4] bfq: Avoid use-after-free when moving processes between cgroups Jan Kara
2022-01-14 17:01 ` [PATCH 1/4] bfq: Avoid false marking of bic as stably merged Jan Kara
2022-01-14 17:01 ` [PATCH 2/4] bfq: Avoid merging queues with different parents Jan Kara
2022-01-14 17:01 ` [PATCH 3/4] bfq: Split shared queues on move between cgroups Jan Kara
2022-01-14 17:01 ` Jan Kara [this message]
2022-01-17 8:13 ` [PATCH 0/4 v4] bfq: Avoid use-after-free when moving processes " yukuai (C)
2022-01-17 11:45 ` Jan Kara
[not found] ` <f6baffae-7105-556c-3785-7b4fed2bf39c@huawei.com>
2022-01-17 15:46 ` Jan Kara
[not found] ` <2d9fbf51-a208-4d18-b8cb-1524ca4b80ba@huawei.com>
2022-01-21 10:58 ` Jan Kara
-- strict thread matches above, loose matches on Subject: below --
2022-01-21 10:56 [PATCH 0/4 v5] " Jan Kara
2022-01-21 10:56 ` [PATCH 4/4] bfq: Update cgroup information before merging bio Jan Kara
2022-01-12 11:39 [PATCH 0/4 v3] bfq: Avoid use-after-free when moving processes between cgroups Jan Kara
2022-01-12 11:39 ` [PATCH 4/4] bfq: Update cgroup information before merging bio Jan Kara
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220114170209.8606-4-jack@suse.cz \
--to=jack@suse.cz \
--cc=axboe@kernel.dk \
--cc=linux-block@vger.kernel.org \
--cc=paolo.valente@linaro.org \
--cc=stable@vger.kernel.org \
--cc=yukuai3@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).