Each loop_worker is responsible for running requests originated from same blkcg, so: 1) associate with kthread in the entry of loop_process_work(), and disassociate in the end of this function, then we can avoid to do both for each request. 2) remove ->blkcg_css and ->memcg_css from 'loop_cmd' since both are per loop_worker. Cc: Michal Koutný <mkoutny@xxxxxxxx> Cc: Dan Schatzberg <schatzberg.dan@xxxxxxxxx> Signed-off-by: Ming Lei <ming.lei@xxxxxxxxxx> --- drivers/block/loop.c | 67 ++++++++++++++++++++------------------------ drivers/block/loop.h | 2 -- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 02509bc54242..8378b8455f7c 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -949,10 +949,17 @@ static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd) struct loop_worker *cur_worker, *worker = NULL; struct work_struct *work; struct list_head *cmd_list; + struct cgroup_subsys_state *blkcg_css = NULL; +#ifdef CONFIG_BLK_CGROUP + struct request *rq = blk_mq_rq_from_pdu(cmd); + + if (rq->bio && rq->bio->bi_blkg) + blkcg_css = &bio_blkcg(rq->bio)->css; +#endif spin_lock_irq(&lo->lo_work_lock); - if (queue_on_root_worker(cmd->blkcg_css)) + if (queue_on_root_worker(blkcg_css)) goto queue_work; node = &lo->worker_tree.rb_node; @@ -960,10 +967,10 @@ static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd) while (*node) { parent = *node; cur_worker = container_of(*node, struct loop_worker, rb_node); - if (cur_worker->blkcg_css == cmd->blkcg_css) { + if (cur_worker->blkcg_css == blkcg_css) { worker = cur_worker; break; - } else if ((long)cur_worker->blkcg_css < (long)cmd->blkcg_css) { + } else if ((long)cur_worker->blkcg_css < (long)blkcg_css) { node = &(*node)->rb_left; } else { node = &(*node)->rb_right; @@ -977,15 +984,10 @@ static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd) * In the event we cannot allocate a worker, just queue on the * rootcg worker and issue the I/O as the rootcg */ - if (!worker) { - cmd->blkcg_css = NULL; - if (cmd->memcg_css) - css_put(cmd->memcg_css); - cmd->memcg_css = NULL; + if (!worker) goto queue_work; - } - worker->blkcg_css = cmd->blkcg_css; + worker->blkcg_css = blkcg_css; css_get(worker->blkcg_css); INIT_WORK(&worker->work, loop_workfn); INIT_LIST_HEAD(&worker->cmd_list); @@ -2100,19 +2102,6 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx, break; } - /* always use the first bio's css */ - cmd->blkcg_css = NULL; - cmd->memcg_css = NULL; -#ifdef CONFIG_BLK_CGROUP - if (rq->bio && rq->bio->bi_blkg) { - cmd->blkcg_css = &bio_blkcg(rq->bio)->css; -#ifdef CONFIG_MEMCG - cmd->memcg_css = - cgroup_get_e_css(cmd->blkcg_css->cgroup, - &memory_cgrp_subsys); -#endif - } -#endif loop_queue_work(lo, cmd); return BLK_STS_OK; @@ -2124,28 +2113,14 @@ static void loop_handle_cmd(struct loop_cmd *cmd) const bool write = op_is_write(req_op(rq)); struct loop_device *lo = rq->q->queuedata; int ret = 0; - struct mem_cgroup *old_memcg = NULL; if (write && (lo->lo_flags & LO_FLAGS_READ_ONLY)) { ret = -EIO; goto failed; } - if (cmd->blkcg_css) - kthread_associate_blkcg(cmd->blkcg_css); - if (cmd->memcg_css) - old_memcg = set_active_memcg( - mem_cgroup_from_css(cmd->memcg_css)); - ret = do_req_filebacked(lo, rq); - if (cmd->blkcg_css) - kthread_associate_blkcg(NULL); - - if (cmd->memcg_css) { - set_active_memcg(old_memcg); - css_put(cmd->memcg_css); - } failed: /* complete non-aio request */ if (!cmd->use_aio || ret) { @@ -2201,7 +2176,25 @@ static void loop_workfn(struct work_struct *work) { struct loop_worker *worker = container_of(work, struct loop_worker, work); + struct mem_cgroup *old_memcg = NULL; + struct cgroup_subsys_state *memcg_css = NULL; + + kthread_associate_blkcg(worker->blkcg_css); +#ifdef CONFIG_MEMCG + memcg_css = cgroup_get_e_css(worker->blkcg_css->cgroup, + &memory_cgrp_subsys); +#endif + if (memcg_css) + old_memcg = set_active_memcg( + mem_cgroup_from_css(memcg_css)); + loop_process_work(worker, &worker->cmd_list, worker->lo); + + kthread_associate_blkcg(NULL); + if (memcg_css) { + set_active_memcg(old_memcg); + css_put(memcg_css); + } } static void loop_rootcg_workfn(struct work_struct *work) diff --git a/drivers/block/loop.h b/drivers/block/loop.h index 1988899db63a..a52a3fd89457 100644 --- a/drivers/block/loop.h +++ b/drivers/block/loop.h @@ -77,8 +77,6 @@ struct loop_cmd { long ret; struct kiocb iocb; struct bio_vec *bvec; - struct cgroup_subsys_state *blkcg_css; - struct cgroup_subsys_state *memcg_css; }; /* Support for loadable transfer modules */ -- 2.31.1