mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: SeungJong Ha <engineer.jjhama@gmail.com>
To: "David S. Miller" <davem@davemloft.net>,
	 Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>,
	 Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	 Simon Horman <horms@kernel.org>,
	Jamal Hadi Salim <jhs@mojatatu.com>,
	 Jiri Pirko <jiri@resnulli.us>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	 Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	 Jason Wang <jasowangio@gmail.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	 John Fastabend <john.fastabend@gmail.com>,
	linux-kernel@vger.kernel.org,  netdev@vger.kernel.org,
	SeungJong Ha <engineer.jjhama@gmail.com>
Subject: [PATCH RFC net-next 2/5] ptr_ring: split ptr_ring_resize_multiple_bh() into alloc and swap
Date: Sun, 06 Dec 2026 23:44:43 +0900	[thread overview]
Message-ID: <20261206-tc1-txqlen-commit-v1-2-ec9f34dce973@gmail.com> (raw)
In-Reply-To: <20261206-tc1-txqlen-commit-v1-0-ec9f34dce973@gmail.com>

ptr_ring_resize_multiple_bh() allocates all the new queues before it
swaps any of them in, so it either resizes every ring or none. A
caller that has to do other work that can fail between allocating
and swapping cannot use it. pfifo_fast needs this to change the
rings of all tx queues of a device together.

Split it into ptr_ring_alloc_queues(), which can fail, and
ptr_ring_swap_queues_bh(), which cannot. ptr_ring_free_queues() frees
an allocation that is not swapped in. ptr_ring_resize_multiple_bh()
now calls the first two in turn; no functional change.

The patch was written with Claude Code (Claude Opus 5.5).

Signed-off-by: SeungJong Ha <engineer.jjhama@gmail.com>
---
 include/linux/ptr_ring.h | 79 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 59 insertions(+), 20 deletions(-)

diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h
index 4e710397903b..539063e29a55 100644
--- a/include/linux/ptr_ring.h
+++ b/include/linux/ptr_ring.h
@@ -645,22 +645,18 @@ static inline int ptr_ring_resize_noprof(struct ptr_ring *r, int size, gfp_t gfp
 #define ptr_ring_resize(...)	alloc_hooks(ptr_ring_resize_noprof(__VA_ARGS__))
 
 /*
- * Note: producer lock is nested within consumer lock, so if you
- * resize you must make sure all uses nest correctly.
- * In particular if you consume ring in BH context, you must
- * disable BH when doing so.
+ * Allocate @nrings queues of @size entries for ptr_ring_swap_queues_bh().
+ * On failure, return NULL with nothing allocated.
  */
-static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
-						     unsigned int nrings,
-						     int size, gfp_t gfp,
-						     void (*destroy)(void *))
+static inline void ***ptr_ring_alloc_queues_noprof(unsigned int nrings,
+						   int size, gfp_t gfp)
 {
 	void ***queues;
 	int i;
 
 	queues = kmalloc_array_noprof(nrings, sizeof(*queues), gfp);
 	if (!queues)
-		goto noqueues;
+		return NULL;
 
 	for (i = 0; i < nrings; ++i) {
 		queues[i] = __ptr_ring_init_queue_alloc_noprof(size, gfp);
@@ -668,6 +664,41 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
 			goto nomem;
 	}
 
+	return queues;
+
+nomem:
+	while (--i >= 0)
+		kvfree(queues[i]);
+
+	kfree(queues);
+
+	return NULL;
+}
+#define ptr_ring_alloc_queues(...) \
+		alloc_hooks(ptr_ring_alloc_queues_noprof(__VA_ARGS__))
+
+/* Free @queues from ptr_ring_alloc_queues() without swapping them in. */
+static inline void ptr_ring_free_queues(void ***queues, unsigned int nrings)
+{
+	int i;
+
+	for (i = 0; i < nrings; ++i)
+		kvfree(queues[i]);
+
+	kfree(queues);
+}
+
+/*
+ * Swap in @queues from ptr_ring_alloc_queues() with the same @nrings and
+ * @size, then free the old queues and @queues. Locking is as for
+ * ptr_ring_resize_multiple_bh().
+ */
+static inline void ptr_ring_swap_queues_bh(struct ptr_ring **rings,
+					   void ***queues, unsigned int nrings,
+					   int size, void (*destroy)(void *))
+{
+	int i;
+
 	for (i = 0; i < nrings; ++i) {
 		spin_lock_bh(&(rings[i])->consumer_lock);
 		spin_lock(&(rings[i])->producer_lock);
@@ -677,21 +708,29 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
 		spin_unlock_bh(&(rings[i])->consumer_lock);
 	}
 
-	for (i = 0; i < nrings; ++i)
-		kvfree(queues[i]);
-
-	kfree(queues);
+	ptr_ring_free_queues(queues, nrings);
+}
 
-	return 0;
+/*
+ * Note: producer lock is nested within consumer lock, so if you
+ * resize you must make sure all uses nest correctly.
+ * In particular if you consume ring in BH context, you must
+ * disable BH when doing so.
+ */
+static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings,
+						     unsigned int nrings,
+						     int size, gfp_t gfp,
+						     void (*destroy)(void *))
+{
+	void ***queues;
 
-nomem:
-	while (--i >= 0)
-		kvfree(queues[i]);
+	queues = ptr_ring_alloc_queues_noprof(nrings, size, gfp);
+	if (!queues)
+		return -ENOMEM;
 
-	kfree(queues);
+	ptr_ring_swap_queues_bh(rings, queues, nrings, size, destroy);
 
-noqueues:
-	return -ENOMEM;
+	return 0;
 }
 #define ptr_ring_resize_multiple_bh(...) \
 		alloc_hooks(ptr_ring_resize_multiple_bh_noprof(__VA_ARGS__))

-- 
2.54.0


  parent reply	other threads:[~2026-12-06 14:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 14:44 [PATCH RFC net-next 0/5] net: make tx_queue_len changes all-or-nothing SeungJong Ha
2026-12-06 14:44 ` [PATCH RFC net-next 1/5] ptr_ring: drop the unused gfp argument of __ptr_ring_swap_queue() SeungJong Ha
2026-12-06 14:44 ` SeungJong Ha [this message]
2026-12-06 14:44 ` [PATCH RFC net-next 3/5] net_sched: change tx_queue_len on all tx queues or on none SeungJong Ha
2026-12-06 14:44 ` [PATCH RFC net-next 4/5] net: resize tun and tap rings from rtnl_link_ops SeungJong Ha
2026-12-06 14:44 ` [PATCH RFC net-next 5/5] net: change tx_queue_len only after all allocations succeeded SeungJong Ha

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=20261206-tc1-txqlen-commit-v1-2-ec9f34dce973@gmail.com \
    --to=engineer.jjhama@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=jasowangio@gmail.com \
    --cc=jhs@mojatatu.com \
    --cc=jiri@resnulli.us \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    --cc=willemdebruijn.kernel@gmail.com \
    /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

all inboxes | Powered by JetHome®