* [PATCH v2 0/2] null_blk: stop using page->private
@ 2026-11-26 21:23 Nilay Vaish
2026-11-26 21:23 ` [PATCH v2 1/2] null_blk: convert the page stores to XArray Nilay Vaish
2026-11-26 21:23 ` [PATCH v2 2/2] null_blk: stop storing the page index in page->private Nilay Vaish
0 siblings, 2 replies; 3+ messages in thread
From: Nilay Vaish @ 2026-11-26 21:23 UTC (permalink / raw)
To: Jens Axboe; +Cc: Christoph Hellwig, Bart Van Assche, linux-block, Nilay Vaish
null_blk records the index of each backing page in page->private. v1
only switched those accesses to the page_private() accessors. Christoph
pointed out that the index does not need to be stored at all: it is there
to work around radix_tree_gang_lookup(), which returns entries without
their indices.
So this version follows his suggestion instead. Patch 1 converts the two
page stores from radix trees to XArrays. Patch 2 takes the index from the
XArray iteration and stops storing it in the page. After the series
null_blk does not touch page->private.
Testing: a memory-backed device created through configfs, without and
with a 16 MiB cache, on next-20261125 with lockdep and atomic-sleep
debugging enabled. Write and read back 300 MiB, four parallel rewriters
followed by a flush, discard of a range (zeros there, neighbours intact),
an ext4 file copy with remount and fsck, then device removal. Same
results before the series, after patch 1 and after patch 2, with no
kernel warnings. The out-of-memory retry in null_insert_page() and zoned
devices were not exercised.
Changes since v1:
- Replace the accessor conversion with the XArray conversion and the
removal of the stored index (Christoph).
- Bart's Reviewed-by is not carried over, since the patches are new.
v1: https://lore.kernel.org/r/20261124033630.832566-1-nilayvaish@google.com/
Nilay Vaish (2):
null_blk: convert the page stores to XArray
null_blk: stop storing the page index in page->private
drivers/block/null_blk/main.c | 146 +++++++++++++++---------------
drivers/block/null_blk/null_blk.h | 4 +-
2 files changed, 75 insertions(+), 75 deletions(-)
base-commit: 9437046953b2fb3bf55b36582450ea0267df82a7
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] null_blk: convert the page stores to XArray
2026-11-26 21:23 [PATCH v2 0/2] null_blk: stop using page->private Nilay Vaish
@ 2026-11-26 21:23 ` Nilay Vaish
2026-11-26 21:23 ` [PATCH v2 2/2] null_blk: stop storing the page index in page->private Nilay Vaish
1 sibling, 0 replies; 3+ messages in thread
From: Nilay Vaish @ 2026-11-26 21:23 UTC (permalink / raw)
To: Jens Axboe; +Cc: Christoph Hellwig, Bart Van Assche, linux-block, Nilay Vaish
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] null_blk: stop storing the page index in page->private
2026-11-26 21:23 [PATCH v2 0/2] null_blk: stop using page->private Nilay Vaish
2026-11-26 21:23 ` [PATCH v2 1/2] null_blk: convert the page stores to XArray Nilay Vaish
@ 2026-11-26 21:23 ` Nilay Vaish
1 sibling, 0 replies; 3+ messages in thread
From: Nilay Vaish @ 2026-11-26 21:23 UTC (permalink / raw)
To: Jens Axboe; +Cc: Christoph Hellwig, Bart Van Assche, linux-block, Nilay Vaish
Each backing page records its own index in page->private. The index was
only needed because radix_tree_gang_lookup() returns entries without
their indices. The XArray iteration provides the index, and every other
user already has it at hand.
Take the index from the iteration when flushing the cache, pass it to
null_flush_cache_page(), and drop the copy kept in the page together
with the sanity checks that compared it with the lookup index.
null_blk no longer uses page->private.
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 | 24 ++++++++++--------------
1 file changed, 10 insertions(+), 14 deletions(-)
diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index e073eca48655..299cf57b1c12 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -961,8 +961,6 @@ static struct nullb_page *__null_lookup_page(struct nullb *nullb,
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)))
return t_page;
@@ -1000,7 +998,6 @@ 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.
@@ -1026,7 +1023,6 @@ static struct nullb_page *null_insert_page(struct nullb *nullb,
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)
@@ -1040,17 +1036,15 @@ static struct nullb_page *null_insert_page(struct nullb *nullb,
return null_lookup_page(nullb, sector, true, ignore_cache);
}
-static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
+static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page,
+ unsigned long idx)
{
int i;
unsigned int offset;
- u64 idx;
struct nullb_page *t_page;
void *dst, *src;
- idx = c_page->page->private;
-
- t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
+ t_page = null_insert_page(nullb, (sector_t)idx << PAGE_SECTORS_SHIFT, true);
__clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
@@ -1092,7 +1086,8 @@ static int null_make_cache_space(struct nullb *nullb, unsigned long n)
{
int i, err, nr_pages;
struct nullb_page *c_pages[FREE_BATCH], *c_page;
- unsigned long flushed = 0, one_round, idx;
+ unsigned long c_idx[FREE_BATCH], idx;
+ unsigned long flushed = 0, one_round;
again:
if ((nullb->dev->cache_size * 1024 * 1024) >
@@ -1102,8 +1097,9 @@ static int null_make_cache_space(struct nullb *nullb, unsigned long n)
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)
+ c_pages[nr_pages] = c_page;
+ c_idx[nr_pages] = idx;
+ if (++nr_pages == FREE_BATCH)
break;
}
/*
@@ -1111,7 +1107,7 @@ static int null_make_cache_space(struct nullb *nullb, unsigned long n)
* avoid race, we don't allow page free
*/
for (i = 0; i < nr_pages; i++) {
- nullb->cache_flush_pos = c_pages[i]->page->private;
+ nullb->cache_flush_pos = c_idx[i];
/*
* We found the page which is being flushed to disk by other
* threads
@@ -1126,7 +1122,7 @@ static int null_make_cache_space(struct nullb *nullb, unsigned long n)
for (i = 0; i < nr_pages; i++) {
if (c_pages[i] == NULL)
continue;
- err = null_flush_cache_page(nullb, c_pages[i]);
+ err = null_flush_cache_page(nullb, c_pages[i], c_idx[i]);
if (err)
return err;
one_round++;
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-26 21:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 21:23 [PATCH v2 0/2] null_blk: stop using page->private Nilay Vaish
2026-11-26 21:23 ` [PATCH v2 1/2] null_blk: convert the page stores to XArray Nilay Vaish
2026-11-26 21:23 ` [PATCH v2 2/2] null_blk: stop storing the page index in page->private Nilay Vaish
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox