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@xxxxxxxxxxxxxxx Fixes: e21b7a0b9887 ("block, bfq: add full hierarchical scheduling and cgroups support") Signed-off-by: Jan Kara <jack@xxxxxxx> --- 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