From: Ming Lei <ming.lei@redhat.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-kernel@vger.kernel.org, linux-block@vger.kernel.org,
Christoph Hellwig <hch@lst.de>,
Dan Schatzberg <schatzberg.dan@gmail.com>,
Ming Lei <ming.lei@redhat.com>
Subject: [PATCH V3 6/7] loop: use xarray to store workers
Date: Fri, 6 Aug 2021 10:34:22 +0800 [thread overview]
Message-ID: <20210806023423.131060-7-ming.lei@redhat.com> (raw)
In-Reply-To: <20210806023423.131060-1-ming.lei@redhat.com>
css->id is unique in io controller wide, so replace rbtree with xarray
for querying/storing 'blkcg_css' by using css->id as key, then code is
simplified a lot.
Acked-by: Dan Schatzberg <schatzberg.dan@gmail.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
drivers/block/loop.c | 36 ++++++++++++++----------------------
drivers/block/loop.h | 3 ++-
2 files changed, 16 insertions(+), 23 deletions(-)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index c9a28483433b..fd07481058e5 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -971,7 +971,6 @@ static void loop_config_discard(struct loop_device *lo)
}
struct loop_worker {
- struct rb_node rb_node;
struct work_struct work;
struct list_head cmd_list;
struct list_head idle_list;
@@ -1019,35 +1018,23 @@ static struct cgroup_subsys_state *loop_rq_get_memcg_css(
static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
{
- struct rb_node **node = &(lo->worker_tree.rb_node), *parent = NULL;
- struct loop_worker *cur_worker, *worker = NULL;
+ struct loop_worker *worker = NULL;
struct work_struct *work;
struct list_head *cmd_list;
struct cgroup_subsys_state *blkcg_css = loop_rq_blkcg_css(cmd);
+ gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN;
spin_lock(&lo->lo_work_lock);
if (queue_on_root_worker(blkcg_css))
goto queue_work;
- node = &lo->worker_tree.rb_node;
-
- while (*node) {
- parent = *node;
- cur_worker = container_of(*node, struct loop_worker, rb_node);
- if (cur_worker->blkcg_css == blkcg_css) {
- worker = cur_worker;
- break;
- } else if ((long)cur_worker->blkcg_css < (long)blkcg_css) {
- node = &(*node)->rb_left;
- } else {
- node = &(*node)->rb_right;
- }
- }
+ /* css->id is unique in each cgroup subsystem */
+ worker = xa_load(&lo->workers, blkcg_css->id);
if (worker)
goto queue_work;
- worker = kzalloc(sizeof(struct loop_worker), GFP_NOWAIT | __GFP_NOWARN);
+ worker = kzalloc(sizeof(*worker), gfp);
/*
* In the event we cannot allocate a worker, just queue on the
* rootcg worker and issue the I/O as the rootcg
@@ -1061,8 +1048,12 @@ static void loop_queue_work(struct loop_device *lo, struct loop_cmd *cmd)
INIT_LIST_HEAD(&worker->cmd_list);
INIT_LIST_HEAD(&worker->idle_list);
worker->lo = lo;
- rb_link_node(&worker->rb_node, parent, node);
- rb_insert_color(&worker->rb_node, &lo->worker_tree);
+
+ if (xa_err(xa_store(&lo->workers, blkcg_css->id, worker, gfp))) {
+ kfree(worker);
+ worker = NULL;
+ }
+
queue_work:
if (worker) {
/*
@@ -1218,7 +1209,7 @@ static void __loop_free_idle_workers(struct loop_device *lo, bool force)
LOOP_IDLE_WORKER_TIMEOUT))
break;
list_del(&worker->idle_list);
- rb_erase(&worker->rb_node, &lo->worker_tree);
+ xa_erase(&lo->workers, worker->blkcg_css->id);
css_put(worker->blkcg_css);
kfree(worker);
}
@@ -1314,7 +1305,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
INIT_LIST_HEAD(&lo->rootcg_cmd_list);
INIT_LIST_HEAD(&lo->idle_worker_list);
- lo->worker_tree = RB_ROOT;
+ xa_init(&lo->workers);
INIT_DELAYED_WORK(&lo->idle_work, loop_free_idle_workers);
lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
lo->lo_device = bdev;
@@ -1416,6 +1407,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
destroy_workqueue(lo->workqueue);
__loop_free_idle_workers(lo, true);
cancel_delayed_work_sync(&lo->idle_work);
+ xa_destroy(&lo->workers);
spin_lock_irq(&lo->lo_lock);
lo->lo_backing_file = NULL;
diff --git a/drivers/block/loop.h b/drivers/block/loop.h
index 9df889af1bcf..cab34da1e1bb 100644
--- a/drivers/block/loop.h
+++ b/drivers/block/loop.h
@@ -14,6 +14,7 @@
#include <linux/blk-mq.h>
#include <linux/spinlock.h>
#include <linux/mutex.h>
+#include <linux/xarray.h>
#include <uapi/linux/loop.h>
/* Possible states of device */
@@ -59,7 +60,7 @@ struct loop_device {
struct work_struct rootcg_work;
struct list_head rootcg_cmd_list;
struct list_head idle_worker_list;
- struct rb_root worker_tree;
+ struct xarray workers;
struct delayed_work idle_work;
bool use_dio;
bool sysfs_inited;
--
2.31.1
next prev parent reply other threads:[~2021-08-06 2:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-06 2:34 [PATCH V3 0/7] loop: cleanup charging io to mem/blkcg Ming Lei
2021-08-06 2:34 ` [PATCH V3 1/7] mm: memcontrol: add helper of memcg_get_e_css Ming Lei
2021-08-06 2:34 ` [PATCH V3 2/7] loop: clean up blkcg association Ming Lei
2021-08-06 5:50 ` kernel test robot
2021-08-06 2:34 ` [PATCH V3 3/7] loop: conver timer for monitoring idle worker into dwork Ming Lei
2021-08-06 2:34 ` [PATCH V3 4/7] loop: add __loop_free_idle_workers() for covering freeing workers in clearing FD Ming Lei
2021-08-06 2:34 ` [PATCH V3 5/7] loop: improve loop_process_work Ming Lei
2021-08-06 2:34 ` Ming Lei [this message]
2021-08-06 2:34 ` [PATCH V3 7/7] loop: don't add worker into idle list Ming Lei
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=20210806023423.131060-7-ming.lei@redhat.com \
--to=ming.lei@redhat.com \
--cc=axboe@kernel.dk \
--cc=hch@lst.de \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=schatzberg.dan@gmail.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).