Linux 9p file system development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Howells" <dhowells@redhat.com>
Cc: v9fs@lists.linux.dev, "Chuck Lever" <cel@kernel.org>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Anna Schumaker" <anna@kernel.org>
Subject: Re: [PATCH v18 04/17] netfs: Add some tools for managing bvecq chains
Date: Tue, 17 Nov 2026 17:51:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-219277@kernel.org> (raw)
In-Reply-To: <20261117171958.2982801-5-dhowells@redhat.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] netfs: dangling prev pointer in bvecq_put() when next node
  survives
- [Medium] netfs: bvecq_alloc_buffer2() returns empty bvecq on page
  alloc failure
- [Medium] netfs: bvecq_alloc_buffer2() stops page fill early on
  BVECQ_HIT_OOM
- [Medium] netfs: false OOM in bvecq_alloc_buffer2() when pre_slots
  fills first bvecq
- [Low] netfs: direct access to private struct semaphore count in
  netfs_stats_show()
--

commit 98d0ef34f6cf6f54725f27653fd3ae0e910be6c9
Author: David Howells <dhowells@redhat.com>

netfs: Add some tools for managing bvecq chains

This commit adds helpers for allocating, populating, reference-counting, and
freeing bvecq chains, along with request and subrequest emergency mempools.

> diff --git a/fs/netfs/bvecq.c b/fs/netfs/bvecq.c
> new file mode 100644
> index 0000000000000..8ac2dc2352585
> --- /dev/null
> +++ b/fs/netfs/bvecq.c
[ ... ]
> +struct bvecq *bvecq_alloc_buffer2(size_t size, unsigned int pre_slots, gfp_t gfp,
> +				  unsigned int *flags)
> +{
> +	struct bvecq *head = NULL, *tail = NULL;
> +	size_t count = pre_slots + DIV_ROUND_UP(size, PAGE_SIZE);
> +
> +	_enter("%zx,%zx,%u", size, count, pre_slots);
> +
> +	*flags &= ~BVECQ_HIT_OOM;
> +	if (WARN_ON_ONCE(pre_slots > BVECQ_POOL_SLOTS))
> +		return NULL;
> +
> +	do {
> +		struct page **pages;
> +		size_t unused, want, got, slot;
> +
> +		if (!tail || bvecq_is_full(tail)) {
> +			struct bvecq *bq;
> +
> +			bq = bvecq_alloc_one(min(count, BVECQ_4KB_SLOTS), gfp, flags);
> +			if (!bq)
> +				goto oom;
> +
> +			if (tail)
> +				bvecq_append(tail, bq);
> +			else
> +				head = bq;
> +			tail = bq;
> +
> +			/* Need to clear pre slots and pages[], so just clear all. */
> +			memset(tail->bv, 0, tail->max_slots * sizeof(tail->bv[0]));
> +			tail->mem_type = BVECQ_MEM_ALLOCED;
> +			tail->nr_slots = pre_slots;
> +			count -= pre_slots;
> +			pre_slots = 0;
> +		}
> +
> +		if (!count)
> +			break;
> +
> +		unused = tail->max_slots - tail->nr_slots;
> +
> +		/* We can put pages[] at the tail of bv[] as the elements are
> +		 * smaller and we won't overrun if the slots are filled in
> +		 * ascending order.
> +		 */
> +		pages = (struct page **)&tail->bv[tail->max_slots];
> +		pages -= unused;
> +
> +		want = min(count, unused);
> +		got = alloc_pages_bulk(gfp, want, pages);
> +		if (!got) {
> +			*flags |= BVECQ_HIT_OOM;
> +			goto oom;
> +		}

[Severity: Medium]
Does bvecq_alloc_buffer2() spuriously fail with BVECQ_HIT_OOM when pre_slots
fills the entire first bvecq node and size > 0?

pre_slots is permitted up to BVECQ_POOL_SLOTS.  If pre_slots ==
BVECQ_POOL_SLOTS and the first bvecq comes from the emergency pool,
tail->nr_slots equals tail->max_slots, so unused and want are both 0 while
count > 0.  alloc_pages_bulk(gfp, 0, pages) then returns 0 and jumps to oom
instead of looping to allocate a second bvecq for the buffer pages.

> +
> +		slot = tail->nr_slots;
> +		for (int i = 0; i < got; i++)
> +			bvec_set_page(&tail->bv[slot++], pages[i], PAGE_SIZE, 0);
> +
> +		bvecq_filled_to(tail, slot);
> +		count -= got;
> +
> +		if (unlikely(*flags & BVECQ_HIT_OOM) && (*flags & BVECQ_SHORT_OKAY))
> +			break;

[Severity: Medium]
Does checking BVECQ_HIT_OOM here stop filling tail after a single page when
bvecq_alloc_one() used the emergency pool?

When bvecq_alloc_one() falls back to the mempool, it sets BVECQ_HIT_OOM and
returns a node with BVECQ_POOL_SLOTS (29) slots.  Under low memory (or when
CONFIG_PAGE_OWNER or __GFP_ACCOUNT is active), alloc_pages_bulk() falls back
to allocating only 1 page per call, expecting the caller's loop to iterate
until the array is filled.  Because BVECQ_HIT_OOM is already set in *flags,
this check breaks out of the loop after populating just 1 slot of tail and
leaves the remaining 28 slots unused.

> +	} while (count > 0);
> +
> +out:
> +	return head;
> +oom:
> +	if (*flags & BVECQ_SHORT_OKAY)
> +		goto out;
> +	bvecq_put(head);
> +	return NULL;
> +}

[Severity: Medium]
If BVECQ_SHORT_OKAY is set and alloc_pages_bulk() returns 0 on the first
bvecq node, does this return a non-NULL head with zero buffer pages
allocated?  On later iterations where bvecq_alloc_one() just appended a new
tail node before alloc_pages_bulk() failed, jumping to out also leaves an
empty trailing bvecq (nr_slots == 0) attached to the chain.

[ ... ]

> +void bvecq_put(struct bvecq *bq)
> +{
> +	struct bvecq *next;
> +
> +	for (; bq; bq = next) {
> +		if (!refcount_dec_and_test(&bq->ref))
> +			break;
> +		for (int slot = 0; slot < bq->nr_slots; slot++)
> +			bvecq_free_slot(bq, slot);
> +		next = bq->next;
> +		netfs_stat_d(&netfs_n_bvecq);
> +
> +		if (bq->from_pool != BVECQ_USE_NO_POOL)
> +			mempool_free(bq, &netfs_bvecq_pool[bq->from_pool]);
> +		else
> +			kfree(bq);
> +	}
> +}

[Severity: High]
Does bvecq_put() leave a dangling next->prev pointer when bq is freed but
next survives because it holds an additional reference?

When refcount_dec_and_test(&bq->ref) succeeds on bq and then returns false on
next, bq is freed while next->prev still points to bq.  Any later backward
traversal from next, such as iov_iter_revert(), will dereference freed
memory.

> diff --git a/fs/netfs/stats.c b/fs/netfs/stats.c
> index 9a607c4e62dda..07c5f91657683 100644
> --- a/fs/netfs/stats.c
> +++ b/fs/netfs/stats.c
[ ... ]
> @@ -88,10 +89,13 @@ int netfs_stats_show(struct seq_file *m, void *v)
>  		   atomic_read(&netfs_n_rh_retry_read_subreq),
>  		   atomic_read(&netfs_n_wh_retry_write_req),
>  		   atomic_read(&netfs_n_wh_retry_write_subreq));
> -	seq_printf(m, "Objs   : rr=%u sr=%u foq=%u wsc=%u\n",
> +	seq_printf(m, "Objs   : rr=%u sr=%u bq=%u foq=%u bqs=%d,%d wsc=%u\n",
>  		   atomic_read(&netfs_n_rh_rreq),
>  		   atomic_read(&netfs_n_rh_sreq),
> +		   atomic_read(&netfs_n_bvecq),
>  		   atomic_read(&netfs_n_folioq),
> +		   READ_ONCE(netfs_bvecq_pool_sem[1].count),
> +		   READ_ONCE(netfs_bvecq_pool_sem[2].count),
>  		   atomic_read(&netfs_n_wh_wstream_conflict));

[Severity: Low]
include/linux/semaphore.h marks struct semaphore's members as private
("Please don't access any members of this structure directly"), and count is
an unsigned int updated with plain accesses under sem->lock.  Should
netfs_stats_show() avoid reaching into sem.count directly (or format it with
%u if exposed via a helper)?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117171958.2982801-1-dhowells@redhat.com?part=4

  reply	other threads:[~2026-11-17 17:51 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 17:18 [PATCH v18 00/17] netfs, iov_iter: Use a chain of bio_vec arrays instead of folio_queue David Howells
2026-11-17 17:18 ` [PATCH v18 01/17] iov_iter: Fix kunit test to remove __init and __refdata marks David Howells
2026-11-17 17:34   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 02/17] Add a function to kmap one page of a multipage bio_vec David Howells
2026-11-17 17:28   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 03/17] iov_iter: Add a segmented queue of bio_vec[] David Howells
2026-11-17 17:56   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 04/17] netfs: Add some tools for managing bvecq chains David Howells
2026-11-17 17:51   ` sashiko-bot [this message]
2026-11-17 17:19 ` [PATCH v18 05/17] iov_iter: Add an API document for the bvecq structure David Howells
2026-11-17 17:32   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 06/17] iov_iter: Add a bvecq kunit test David Howells
2026-11-17 17:30   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 07/17] afs: Fix an inconsistency in the symlink code David Howells
2026-11-17 17:38   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 08/17] netfs: Explicitly pass tail padding to netfs_writeback_single() David Howells
2026-11-17 17:58   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 09/17] netfs: Change netfs_writeback_single() to not look inside the iterator David Howells
2026-11-17 17:53   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 10/17] netfs: Pass the write length to request creation where appropriate David Howells
2026-11-17 17:28   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 11/17] afs: Use a bvecq to hold dir content rather than folioq David Howells
2026-11-17 18:05   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 12/17] cifs: Use a bvecq for buffering instead of a folioq David Howells
2026-11-17 17:45   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 13/17] smbdirect: Support ITER_BVECQ in smbdirect_map_sges_from_iter() David Howells
2026-11-17 17:36   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 14/17] netfs: Switch folioq to bvecq David Howells
2026-11-17 18:19   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 15/17] smbdirect: Remove support for ITER_FOLIOQ from smbdirect_map_sges_from_iter() David Howells
2026-11-17 17:35   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 16/17] iov_iter: Remove ITER_FOLIOQ David Howells
2026-11-17 17:41   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 17/17] netfs: Remove folio_queue David Howells
2026-11-17 17:37   ` sashiko-bot

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=sashiko-outbox-219277@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anna@kernel.org \
    --cc=cel@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=jlayton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=v9fs@lists.linux.dev \
    /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