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 v17 04/17] netfs: Add some tools for managing bvecq chains
Date: Wed, 11 Nov 2026 22:17:46 +0000	[thread overview]
Message-ID: <sashiko-outbox-209778@kernel.org> (raw)
In-Reply-To: <20261111203034.2126314-5-dhowells@redhat.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] netfs: mempool deadlock in bvecq_alloc_chain() when
  for_writeback is true
--

commit e5b9a085f1bcfd322d1044543745cbe0c1ae3076
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 nodes and chains, along with an emergency mempool for
writeback allocations.

> diff --git a/fs/netfs/bvecq.c b/fs/netfs/bvecq.c
> new file mode 100644
> index 0000000000000..1d4da3de9b9d2
> --- /dev/null
> +++ b/fs/netfs/bvecq.c

[ ... ]

> +struct bvecq *bvecq_alloc_one(size_t nr_slots, gfp_t gfp, bool for_writeback)
> +{
> +	struct bvecq *bq;
> +	size_t size;
> +	bool from_pool = false;
> +
> +	/* Don't overrun ->max_slots or ->nr_slots. */
> +	if (nr_slots > BVECQ_MAX_INLINE_SLOTS)
> +		nr_slots = BVECQ_MAX_INLINE_SLOTS;
> +
> +	size = kmalloc_size_roundup(struct_size(bq, __bv, nr_slots));
> +	gfp &= ~(GFP_ZONEMASK | __GFP_THISNODE);
> +
> +	if (for_writeback) {
> +		if (size != BVECQ_POOL_SIZE) {
> +			gfp_t gfp_temp = gfp;
> +
> +			gfp_temp |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
> +			gfp_temp &= ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
> +			bq = kmalloc(size, gfp_temp);
> +			if (bq)
> +				goto success;
> +		}
> +
> +		bq = mempool_alloc(&netfs_bvecq_pool, gfp);
> +		if (!bq)
> +			return bq;
> +		from_pool = true;
> +		size = BVECQ_POOL_SIZE;
> +	} else {
> +		bq = kmalloc(size, gfp);
> +		if (!bq)
> +			return bq;
> +	}
> +
> +success:
> +	*bq = (struct bvecq) {
> +		.ref		= REFCOUNT_INIT(1),
> +		.bv		= bq->__bv,
> +		.inline_bv	= true,
> +		.max_slots	= min((size - sizeof(*bq)) / sizeof(bq->__bv[0]),
> +				      BVECQ_MAX_INLINE_SLOTS),
> +		.from_pool	= from_pool,
> +	};
> +	netfs_stat(&netfs_n_bvecq);
> +	return bq;
> +}
> +EXPORT_SYMBOL(bvecq_alloc_one);
> +
> +/**
> + * bvecq_alloc_chain - Allocate an unpopulated bvecq chain
> + * @nr_slots: Number of slots to allocate
> + * @gfp: The allocation constraints.
> + * @for_writeback: True if allocating for writeback
> + *
> + * Allocate a chain of bvecq nodes providing at least the requested cumulative
> + * number of slots.  Each node is a maximum of 4KiB in size.
> + *
> + * Return: The first node pointer or NULL on allocation failure.
> + */
> +struct bvecq *bvecq_alloc_chain(size_t nr_slots, gfp_t gfp, bool for_writeback)
> +{
> +	struct bvecq *head = NULL, *tail = NULL;
> +
> +	_enter("%zu", nr_slots);
> +
> +	for (;;) {
> +		struct bvecq *bq;
> +
> +		bq = bvecq_alloc_one(min(nr_slots, BVECQ_4KB_SLOTS), gfp, for_writeback);
> +		if (!bq)
> +			goto oom;
> +
> +		if (tail)
> +			bvecq_append(tail, bq);
> +		else
> +			head = bq;
> +		tail = bq;
> +		if (tail->max_slots >= nr_slots)
> +			break;
> +		nr_slots -= tail->max_slots;
> +	}
> +
> +	return head;
> +oom:
> +	bvecq_put(head);
> +	return NULL;
> +}

[Severity: High]
Can bvecq_alloc_chain() deadlock on netfs_bvecq_pool when for_writeback is
true and gfp permits direct reclaim?

Under memory pressure, bvecq_alloc_one() falls back to
mempool_alloc(&netfs_bvecq_pool, gfp), which yields a 512-byte node holding
BVECQ_POOL_SLOTS (29 slots on 64-bit).  bvecq_alloc_chain() loops calling
bvecq_alloc_one() while holding previously allocated pool elements in head.

Because netfs_bvecq_pool has 100 elements, a single chain requesting more
than 2900 slots (~11.3 MiB of 4 KiB pages) or concurrent writeback threads
building chains at the same time can exhaust the pool midway through the
loop.  Each thread then sleeps in mempool_alloc() waiting for elements to
be freed while pinning the elements already attached to head, before any
I/O can be submitted to release them.

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

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

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 20:30 [PATCH v17 00/17] netfs, iov_iter: Use a chain of bio_vec arrays instead of folio_queue David Howells
2026-11-11 20:30 ` [PATCH v17 01/17] iov_iter: Fix kunit test to remove __init and __refdata marks David Howells
2026-11-11 21:23   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 02/17] Add a function to kmap one page of a multipage bio_vec David Howells
2026-11-11 21:26   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 03/17] iov_iter: Add a segmented queue of bio_vec[] David Howells
2026-11-11 21:46   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 04/17] netfs: Add some tools for managing bvecq chains David Howells
2026-11-11 22:17   ` sashiko-bot [this message]
2026-11-11 20:30 ` [PATCH v17 05/17] iov_iter: Add an API document for the bvecq structure David Howells
2026-11-11 22:22   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 06/17] iov_iter: Add a bvecq kunit test David Howells
2026-11-11 22:31   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 07/17] afs: Fix an inconsistency in the symlink code David Howells
2026-11-11 22:45   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 08/17] netfs: Explicitly pass tail padding to netfs_writeback_single() David Howells
2026-11-11 23:17   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 09/17] netfs: Change netfs_writeback_single() to not look inside the iterator David Howells
2026-11-11 23:36   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 10/17] netfs: Pass the write length to request creation where appropriate David Howells
2026-11-11 23:39   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 11/17] afs: Use a bvecq to hold dir content rather than folioq David Howells
2026-11-12  0:06   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 12/17] cifs: Use a bvecq for buffering instead of a folioq David Howells
2026-11-12  0:20   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 13/17] smbdirect: Support ITER_BVECQ in smbdirect_map_sges_from_iter() David Howells
2026-11-12  0:27   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 14/17] netfs: Switch folioq to bvecq David Howells
2026-11-12  1:03   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 15/17] smbdirect: Remove support for ITER_FOLIOQ from smbdirect_map_sges_from_iter() David Howells
2026-11-12  1:06   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 16/17] iov_iter: Remove ITER_FOLIOQ David Howells
2026-11-12  1:11   ` sashiko-bot
2026-11-11 20:30 ` [PATCH v17 17/17] netfs: Remove folio_queue David Howells
2026-11-12  1:14   ` 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-209778@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