Re: [PATCH v19 04/17] netfs: Add some tools for managing bvecq chains

From: David Howells

Date: Thu Nov 19 2026 - 06:49:42 EST


Hi Christian,

If you pull this patch, can you apply the attached diff to it to fix some
issues that Sashiko brought up[1]?

- In bvecq_alloc_buffer2(), fix OOM discard where the requested number of
pre-slots fills up a entire bvecq.

- In bvecq_alloc_buffer2(), add comment on why we want to exit the buffer
alloc loop immediately.

- In bvecq_put(), clear the prev pointer of the next bvecq if it points
back to the one being released.

- Fix bvecq_append() kdoc.

- Change %flags to @flags in function kdoc when referring to func arg.

Thanks,
David

[1] https://sashiko.dev/#/patchset/20261119083851.3306418-1-dhowells%40redhat.com
---
diff --git a/fs/netfs/bvecq.c b/fs/netfs/bvecq.c
index b140cd47a1fa..1b2ad42e6d50 100644
--- a/fs/netfs/bvecq.c
+++ b/fs/netfs/bvecq.c
@@ -61,7 +61,7 @@ EXPORT_SYMBOL(bvecq_dump);
* that at least some progress can be made.
*
* Return: The node pointer or NULL on allocation failure. BVECQ_HIT_OOM is
- * set in *flags if an allocation failure occurs.
+ * set in *@flags if an allocation failure occurs.
*/
struct bvecq *bvecq_alloc_one(size_t nr_slots, gfp_t gfp, unsigned int *flags)
{
@@ -136,10 +136,10 @@ EXPORT_SYMBOL(bvecq_alloc_one);
*
* Allocate a chain of bvecq nodes providing at least the requested cumulative
* number of slots. Each node is a maximum of 4KiB in size. If
- * %BVECQ_SHORT_OKAY is set in *%flags, then the chain may be shortened on OOM.
+ * %BVECQ_SHORT_OKAY is set in *@flags, then the chain may be shortened on OOM.
*
* Return: The first node pointer or NULL on allocation failure. BVECQ_HIT_OOM
- * is set in *flags if an allocation failure occurs.
+ * is set in *@flags if an allocation failure occurs.
*/
struct bvecq *bvecq_alloc_chain(size_t nr_slots, gfp_t gfp, unsigned int *flags)
{
@@ -191,7 +191,7 @@ EXPORT_SYMBOL(bvecq_alloc_chain);
* refcounts set to 1 and can be passed to MSG_SPLICE_PAGES.
*
* Return: The first node pointer or NULL on allocation failure. BVECQ_HIT_OOM
- * is set in *flags if a bvecq chain allocation failure occurs (though if
+ * is set in *@flags if a bvecq chain allocation failure occurs (though if
* %BVECQ_SHORT_OKAY is set, the func may return early but will have at least
* one bvecq in the allocation).
*/
@@ -200,6 +200,7 @@ struct bvecq *bvecq_alloc_buffer2(size_t size, unsigned int pre_slots, gfp_t gfp
{
struct bvecq *head = NULL, *tail = NULL;
size_t count = pre_slots + DIV_ROUND_UP(size, PAGE_SIZE);
+ bool got_page = false;

_enter("%zx,%zx,%u", size, count, pre_slots);

@@ -258,7 +259,12 @@ struct bvecq *bvecq_alloc_buffer2(size_t size, unsigned int pre_slots, gfp_t gfp

bvecq_filled_to(tail, slot);
count -= got;
+ got_page = true;

+ /* TODO: If we got an OOM trying to allocate a bvecq, we should
+ * probably stop allocating pages unless/until we have an
+ * emergency pool of those also.
+ */
if (unlikely(*flags & BVECQ_HIT_OOM) && (*flags & BVECQ_SHORT_OKAY))
break;
} while (count > 0);
@@ -273,7 +279,9 @@ struct bvecq *bvecq_alloc_buffer2(size_t size, unsigned int pre_slots, gfp_t gfp
bvecq_put(tail);
}
oom:
- if (!head || (tail == head && head->nr_slots == pre_slots))
+ if (!head)
+ goto out;
+ if (!got_page && size > 0)
goto discard; /* Allocated no pages */
if (*flags & BVECQ_SHORT_OKAY)
goto out;
@@ -328,6 +336,8 @@ void bvecq_put(struct bvecq *bq)
for (int slot = 0; slot < bq->nr_slots; slot++)
bvecq_free_slot(bq, slot);
next = bq->next;
+ if (next && next->prev == bq)
+ next->prev = NULL;
netfs_stat_d(&netfs_n_bvecq);

if (bq->from_pool != BVECQ_USE_NO_POOL)
diff --git a/include/linux/bvecq.h b/include/linux/bvecq.h
index f670a031ac44..da2385c91e9a 100644
--- a/include/linux/bvecq.h
+++ b/include/linux/bvecq.h
@@ -162,7 +162,7 @@ static inline bool bvecq_acquire_slot(const struct bvecq *bvecq, unsigned int sl
}

/**
- * bvecq_append - Get the next bvecq with appropriate barrier
+ * bvecq_append - Attach new bvecq to chain with appropriate barrier
* @to: The bvecq to append to
* @add: The bvecq to append
*