From: Nilay Vaish <nilayvaish@google.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: Christoph Hellwig <hch@infradead.org>,
Bart Van Assche <bvanassche@acm.org>,
linux-block@vger.kernel.org, Nilay Vaish <nilayvaish@google.com>
Subject: [PATCH v2 1/2] null_blk: convert the page stores to XArray
Date: Thu, 26 Nov 2026 13:23:40 -0800 [thread overview]
Message-ID: <20261126212341.916839-2-nilayvaish@google.com> (raw)
In-Reply-To: <20261126212341.916839-1-nilayvaish@google.com>
The memory-backed store and its cache keep their pages in two radix
trees. Convert both to XArray, the interface that replaced the radix
tree API.
A page is inserted under nullb->lock, where the store cannot sleep. The
radix tree code preloaded nodes before taking the lock. With the XArray,
drop the lock when the store runs out of memory, let xas_nomem()
allocate, and retry.
XArray iteration reports the index of each entry, so freeing the device
storage no longer needs to look it up through the page.
A store that runs out of memory partway can leave empty nodes in the
array until the retry completes, or for good if the retry cannot
allocate either. So check that the cache holds no page after a flush
with xa_find() instead of xa_empty(), and call xa_destroy() when the
storage is freed.
Suggested-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Nilay Vaish <nilayvaish@google.com>
---
Notes:
These patches were written with the help of an AI coding assistant (Gemini).
drivers/block/null_blk/main.c | 134 +++++++++++++++---------------
drivers/block/null_blk/null_blk.h | 4 +-
2 files changed, 71 insertions(+), 67 deletions(-)
diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index ec0d7ba4d938..e073eca48655 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -797,8 +797,8 @@ static struct nullb_device *null_alloc_dev(void)
dev->init_hctx_fault_config.attr = null_init_hctx_attr;
#endif
- INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
- INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
+ xa_init(&dev->data);
+ xa_init(&dev->cache);
if (badblocks_init(&dev->badblocks, 0)) {
kfree(dev);
return NULL;
@@ -911,18 +911,18 @@ static void null_free_sector(struct nullb *nullb, sector_t sector,
unsigned int sector_bit;
u64 idx;
struct nullb_page *t_page, *ret;
- struct radix_tree_root *root;
+ struct xarray *xa;
- root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
+ xa = is_cache ? &nullb->dev->cache : &nullb->dev->data;
idx = sector >> PAGE_SECTORS_SHIFT;
sector_bit = (sector & SECTOR_MASK);
- t_page = radix_tree_lookup(root, idx);
+ t_page = xa_load(xa, idx);
if (t_page) {
__clear_bit(sector_bit, t_page->bitmap);
if (null_page_empty(t_page)) {
- ret = radix_tree_delete_item(root, idx, t_page);
+ ret = xa_erase(xa, idx);
WARN_ON(ret != t_page);
null_free_page(ret);
if (is_cache)
@@ -931,47 +931,18 @@ static void null_free_sector(struct nullb *nullb, sector_t sector,
}
}
-static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
- struct nullb_page *t_page, bool is_cache)
-{
- struct radix_tree_root *root;
-
- root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
-
- if (radix_tree_insert(root, idx, t_page)) {
- null_free_page(t_page);
- t_page = radix_tree_lookup(root, idx);
- WARN_ON(!t_page || t_page->page->private != idx);
- } else if (is_cache)
- nullb->dev->curr_cache += PAGE_SIZE;
-
- return t_page;
-}
-
static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
{
- unsigned long pos = 0;
- int nr_pages;
- struct nullb_page *ret, *t_pages[FREE_BATCH];
- struct radix_tree_root *root;
-
- root = is_cache ? &dev->cache : &dev->data;
-
- do {
- int i;
-
- nr_pages = radix_tree_gang_lookup(root,
- (void **)t_pages, pos, FREE_BATCH);
-
- for (i = 0; i < nr_pages; i++) {
- pos = t_pages[i]->page->private;
- ret = radix_tree_delete_item(root, pos, t_pages[i]);
- WARN_ON(ret != t_pages[i]);
- null_free_page(ret);
- }
+ struct xarray *xa = is_cache ? &dev->cache : &dev->data;
+ struct nullb_page *t_page;
+ unsigned long idx;
- pos++;
- } while (nr_pages == FREE_BATCH);
+ xa_for_each(xa, idx, t_page) {
+ xa_erase(xa, idx);
+ null_free_page(t_page);
+ }
+ /* A failed insert can leave empty nodes behind. */
+ xa_destroy(xa);
if (is_cache)
dev->curr_cache = 0;
@@ -983,13 +954,13 @@ static struct nullb_page *__null_lookup_page(struct nullb *nullb,
unsigned int sector_bit;
u64 idx;
struct nullb_page *t_page;
- struct radix_tree_root *root;
+ struct xarray *xa;
idx = sector >> PAGE_SECTORS_SHIFT;
sector_bit = (sector & SECTOR_MASK);
- root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
- t_page = radix_tree_lookup(root, idx);
+ xa = is_cache ? &nullb->dev->cache : &nullb->dev->data;
+ t_page = xa_load(xa, idx);
WARN_ON(t_page && t_page->page->private != idx);
if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
@@ -1015,8 +986,10 @@ static struct nullb_page *null_insert_page(struct nullb *nullb,
__releases(&nullb->lock)
__acquires(&nullb->lock)
{
- u64 idx;
- struct nullb_page *t_page;
+ struct xarray *xa = ignore_cache ? &nullb->dev->data : &nullb->dev->cache;
+ u64 idx = sector >> PAGE_SECTORS_SHIFT;
+ XA_STATE(xas, xa, idx);
+ struct nullb_page *t_page, *cur;
t_page = null_lookup_page(nullb, sector, true, ignore_cache);
if (t_page)
@@ -1027,15 +1000,37 @@ static struct nullb_page *null_insert_page(struct nullb *nullb,
t_page = null_alloc_page();
if (!t_page)
goto out_lock;
+ t_page->page->private = idx;
+
+ /*
+ * The store has to happen under nullb->lock, where it cannot sleep.
+ * If it runs out of memory, drop the lock, let xas_nomem() allocate
+ * and try again.
+ */
+ do {
+ spin_lock_irq(&nullb->lock);
+ xas_lock(&xas);
+ cur = xas_load(&xas);
+ if (!cur)
+ xas_store(&xas, t_page);
+ xas_unlock(&xas);
+ if (!xas_error(&xas))
+ break;
+ spin_unlock_irq(&nullb->lock);
+ } while (xas_nomem(&xas, GFP_NOIO));
- if (radix_tree_preload(GFP_NOIO))
+ if (xas_error(&xas))
goto out_freepage;
+ xas_destroy(&xas);
- spin_lock_irq(&nullb->lock);
- idx = sector >> PAGE_SECTORS_SHIFT;
- t_page->page->private = idx;
- t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
- radix_tree_preload_end();
+ if (cur) {
+ /* Somebody else inserted a page while the lock was dropped. */
+ null_free_page(t_page);
+ WARN_ON(cur->page->private != idx);
+ return cur;
+ }
+ if (!ignore_cache)
+ nullb->dev->curr_cache += PAGE_SIZE;
return t_page;
out_freepage:
@@ -1050,7 +1045,7 @@ static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
int i;
unsigned int offset;
u64 idx;
- struct nullb_page *t_page, *ret;
+ struct nullb_page *t_page;
void *dst, *src;
idx = c_page->page->private;
@@ -1061,8 +1056,7 @@ static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
null_free_page(c_page);
if (t_page && null_page_empty(t_page)) {
- ret = radix_tree_delete_item(&nullb->dev->data,
- idx, t_page);
+ xa_erase(&nullb->dev->data, idx);
null_free_page(t_page);
}
return 0;
@@ -1087,8 +1081,8 @@ static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
kunmap_local(dst);
kunmap_local(src);
- ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
- null_free_page(ret);
+ xa_erase(&nullb->dev->cache, idx);
+ null_free_page(c_page);
nullb->dev->curr_cache -= PAGE_SIZE;
return 0;
@@ -1097,16 +1091,21 @@ static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
static int null_make_cache_space(struct nullb *nullb, unsigned long n)
{
int i, err, nr_pages;
- struct nullb_page *c_pages[FREE_BATCH];
- unsigned long flushed = 0, one_round;
+ struct nullb_page *c_pages[FREE_BATCH], *c_page;
+ unsigned long flushed = 0, one_round, idx;
again:
if ((nullb->dev->cache_size * 1024 * 1024) >
nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
return 0;
- nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
- (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
+ nr_pages = 0;
+ xa_for_each_start(&nullb->dev->cache, idx, c_page,
+ nullb->cache_flush_pos) {
+ c_pages[nr_pages++] = c_page;
+ if (nr_pages == FREE_BATCH)
+ break;
+ }
/*
* nullb_flush_cache_page could unlock before using the c_pages. To
* avoid race, we don't allow page free
@@ -1229,6 +1228,7 @@ blk_status_t null_handle_discard(struct nullb_device *dev,
static blk_status_t null_handle_flush(struct nullb *nullb)
{
+ unsigned long idx = 0;
int err;
if (!null_cache_active(nullb))
@@ -1242,7 +1242,11 @@ static blk_status_t null_handle_flush(struct nullb *nullb)
break;
}
- WARN_ON(!radix_tree_empty(&nullb->dev->cache));
+ /*
+ * No page may be left. Do not use xa_empty(): an insert that ran out
+ * of memory can leave empty nodes in the array.
+ */
+ WARN_ON(xa_find(&nullb->dev->cache, &idx, ULONG_MAX, XA_PRESENT));
spin_unlock_irq(&nullb->lock);
return errno_to_blk_status(err);
}
diff --git a/drivers/block/null_blk/null_blk.h b/drivers/block/null_blk/null_blk.h
index 6c4c4bbe7dad..8ae2b9128e64 100644
--- a/drivers/block/null_blk/null_blk.h
+++ b/drivers/block/null_blk/null_blk.h
@@ -58,8 +58,8 @@ struct nullb_device {
struct fault_config requeue_config;
struct fault_config init_hctx_fault_config;
#endif
- struct radix_tree_root data; /* data stored in the disk */
- struct radix_tree_root cache; /* disk cache data */
+ struct xarray data; /* data stored in the disk */
+ struct xarray cache; /* disk cache data */
unsigned long flags; /* device flags */
unsigned int curr_cache;
struct badblocks badblocks;
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-26 21:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 21:23 [PATCH v2 0/2] null_blk: stop using page->private Nilay Vaish
2026-11-26 21:23 ` Nilay Vaish [this message]
2026-11-26 21:23 ` [PATCH v2 2/2] null_blk: stop storing the page index in page->private Nilay Vaish
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=20261126212341.916839-2-nilayvaish@google.com \
--to=nilayvaish@google.com \
--cc=axboe@kernel.dk \
--cc=bvanassche@acm.org \
--cc=hch@infradead.org \
--cc=linux-block@vger.kernel.org \
/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