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 v19 04/17] netfs: Add some tools for managing bvecq chains
Date: Thu, 19 Nov 2026 09:07:22 +0000	[thread overview]
Message-ID: <sashiko-outbox-222785@kernel.org> (raw)
In-Reply-To: <20261119083851.3306418-5-dhowells@redhat.com>

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

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

netfs: Add some tools for managing bvecq chains

This commit adds helpers for allocating, populating, refcounting, and
freeing bvecq chains along with emergency mempools for request and
subrequest allocations.

> diff --git a/fs/netfs/bvecq.c b/fs/netfs/bvecq.c
> new file mode 100644
> index 0000000000000..b140cd47a1faf
> --- /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;
> +			if (tail == head) {
> +				tail->nr_slots = pre_slots;
> +				count -= pre_slots;
> +			}
> +		}
> +
> +		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 pages_oom;
> +		}
> +
> +		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 after every alloc_pages_bulk() call stop
populating tail prematurely?

When bvecq_alloc_one() falls back to an emergency mempool, it sets
BVECQ_HIT_OOM in *flags.  Under low-memory conditions where zone
watermarks are not met, alloc_pages_bulk() falls back to allocating a
single page per call and relies on the caller's loop to fill the remaining
slots, so breaking here before tail is full leaves a 29-slot pool bvecq
with only one page populated.

> +	} while (count > 0);
> +
> +out:
> +	return head;
> +
> +pages_oom:
> +	/* If tail is empty, discard it. */
> +	if (tail != head && tail->nr_slots == 0) {
> +		tail->prev->next = NULL;
> +		bvecq_put(tail);
> +	}
> +oom:
> +	if (!head || (tail == head && head->nr_slots == pre_slots))
> +		goto discard; /* Allocated no pages */

[Severity: Medium]
Can bvecq_alloc_buffer2() fail with a false OOM when pre_slots fills the
first bvecq node?

pre_slots is allowed to be up to BVECQ_POOL_SLOTS.  If the first bvecq is
allocated from an emergency mempool (max_slots == BVECQ_POOL_SLOTS) with
pre_slots == BVECQ_POOL_SLOTS and size > 0, setting tail->nr_slots =
pre_slots leaves unused == 0 and want == 0, so alloc_pages_bulk(gfp, 0,
pages) returns 0 and triggers pages_oom instead of allocating a second
bvecq node.

Also, when pages_oom discards an empty non-head tail node via
bvecq_put(tail), tail is not reset to tail->prev before the oom check
tests tail == head && head->nr_slots == pre_slots.  If the second node
fails its first page allocation while head only contains pre_slots,
bvecq_alloc_buffer2() with BVECQ_SHORT_OKAY will return a buffer with zero
allocated pages.

[ ... ]

> +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 prev pointer on a surviving successor
node?

When an interior node in a chain has an extra reference held (for example,
via subrequest or retry pinning) and bvecq_put() frees the preceding node,
the loop breaks when refcount_dec_and_test(&bq->ref) returns false on the
surviving node without clearing its prev pointer.  A subsequent backward
walk such as iov_iter_revert() -> iov_iter_bvecq_revert() will then
dereference the freed predecessor node:

lib/iov_iter.c:iov_iter_bvecq_revert() {
    ...
		if (slot == 0) {
			bq = bq->prev;
			slot = bq->nr_slots;
			continue;
		}
    ...
}

> 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]
struct semaphore in include/linux/semaphore.h is marked "Please don't
access any members of this structure directly", and its count field is an
unsigned int protected by sem->lock (formatted here with %d).

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

  reply	other threads:[~2026-11-19  9:07 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  8:38 [PATCH v19 00/17] netfs, iov_iter: Use a chain of bio_vec arrays instead of folio_queue David Howells
2026-11-19  8:38 ` [PATCH v19 01/17] iov_iter: Fix kunit test to remove __init and __refdata marks David Howells
2026-11-19  8:47   ` sashiko-bot
2026-11-19  9:21   ` Wang Yibo
2026-11-19  8:38 ` [PATCH v19 02/17] block: Add a function to kmap one page of a multipage bio_vec David Howells
2026-11-19  8:44   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 03/17] iov_iter: Add a segmented queue of bio_vec[] David Howells
2026-11-19  8:52   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 04/17] netfs: Add some tools for managing bvecq chains David Howells
2026-11-19  9:07   ` sashiko-bot [this message]
2026-11-19 11:49   ` David Howells
2026-11-19  8:38 ` [PATCH v19 05/17] iov_iter: Add an API document for the bvecq structure David Howells
2026-11-19  8:53   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 06/17] iov_iter: Add a bvecq kunit test David Howells
2026-11-19  8:50   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 07/17] afs: Fix an inconsistency in the symlink code David Howells
2026-11-19  8:57   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 08/17] netfs: Explicitly pass tail padding to netfs_writeback_single() David Howells
2026-11-19  9:15   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 09/17] netfs: Change netfs_writeback_single() to not look inside the iterator David Howells
2026-11-19  9:09   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 10/17] netfs: Pass the write length to request creation where appropriate David Howells
2026-11-19  8:56   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 11/17] afs: Use a bvecq to hold dir content rather than folioq David Howells
2026-11-19  9:29   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 12/17] cifs: Use a bvecq for buffering instead of a folioq David Howells
2026-11-19  9:10   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 13/17] smbdirect: Support ITER_BVECQ in smbdirect_map_sges_from_iter() David Howells
2026-11-19  9:15   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 14/17] netfs: Switch folioq to bvecq David Howells
2026-11-19  9:38   ` sashiko-bot
2026-11-19 11:49   ` David Howells
2026-11-19  8:38 ` [PATCH v19 15/17] smbdirect: Remove support for ITER_FOLIOQ from smbdirect_map_sges_from_iter() David Howells
2026-11-19  9:13   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 16/17] iov_iter: Remove ITER_FOLIOQ David Howells
2026-11-19  9:20   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 17/17] netfs: Remove folio_queue David Howells
2026-11-19  9:18   ` 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-222785@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