Netdev List
 help / color / mirror / Atom feed
From: Vikas Gupta <vikas.gupta@broadcom.com>
To: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, andrew+netdev@lunn.ch, horms@kernel.org
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	bhargava.marreddy@broadcom.com, rahul-rg.gupta@broadcom.com,
	vsrama-krishna.nemani@broadcom.com,
	rajashekar.hudumula@broadcom.com, dharmender.garg@broadcom.com,
	ajit.khaparde@broadcom.com,
	Vikas Gupta <vikas.gupta@broadcom.com>
Subject: [PATCH net-next v12 10/10] bnge: add cpu_rmap support for IRQ affinity
Date: Tue,  1 Dec 2026 18:50:11 +0530	[thread overview]
Message-ID: <20261201132011.2705569-11-vikas.gupta@broadcom.com> (raw)
In-Reply-To: <20261201132011.2705569-1-vikas.gupta@broadcom.com>

When CONFIG_RFS_ACCEL is enabled, allocate and populate a cpu_rmap
for the RX IRQ vectors so that the kernel's aRFS infrastructure can
correctly map flows to the RX queues serviced by the nearest CPU.

This ensures that NTUPLE rules created by the kernel steer packets
to the optimal queue based on CPU/IRQ affinity.

Also, This is the patch that turns aRFS on. Before it dev->rx_cpu_rmap
was never set, so set_rps_cpu() took its early exit and
bnge_rx_flow_steer() from the previous patch was never called.
The rmap is allocated at open when NTUPLE is enabled. A failed rmap
allocation does not fail the open; aRFS stays off.

Do not fill the rmap with irq_cpu_rmap_add(). It installs a notifier
but does not read the current affinity of the IRQ, so an entry gets a
CPU only when the affinity changes after the notifier exists. NQ0 is
requested once at probe, before the rmap exists, and is not requested
again at open, so its entry would stay empty and aRFS would steer the
flows of its CPU to a neighbouring ring.

Use a driver affinity notifier instead. It is installed in
bnge_nq_request_irq() for every NQ that has an RX ring and for NQ0,
independent of the allocation of the affinity hint mask, and removed in
bnge_nq_free_irq(). It updates the rmap entry of its ring whenever the
affinity of the IRQ changes. The entries are indexed by the RX ring
number, so the rmap does not depend on the order in which the vectors
are added.

The notifier of NQ0 lives from probe to remove, so it also runs while
the interface is down, when there is no rmap. It keeps the affinity it
is given in bn->nq0_aff. At open, bnge_nq0_seed_cpu_rmap() writes that
affinity into entry 0 of the new rmap. The IRQ core can defer an
affinity move to the next interrupt. Then the mask that
irq_get_affinity_mask() returns is still the old one while the notifier
already reports the new one, so the seed does not read that mask. If no
affinity was reported yet, the seed reads it. Embed bn->nq0_aff in
struct bnge_net so saving the affinity needs no separate allocation.
The affinity of NQ0 is not applied again at open, so a CPU chosen by
the administrator or irqbalance is kept.

The notifier does not write to the cpumask that is registered as the
affinity hint, so the hint keeps showing the value chosen by the
driver. For this the calls to irq_set_affinity_hint() are replaced. The
one that sets the CPU becomes irq_set_affinity_and_hint(), which is
what irq_set_affinity_hint() calls, and the one in bnge_nq_free_irq()
becomes irq_update_affinity_hint(), which only clears the hint.

An error from cpu_rmap_update(), from the allocation of the hint mask
or from the installation of the notifier is reported with a warning
and does not turn aRFS off. A ring whose hint mask could not be
allocated has no affinity set by the driver, so its entry keeps the
placeholder value until its affinity changes.

bn->rmap_lock orders the update of the rmap by the notifier and by the
seeding of NQ0 against the creation and teardown of the rmap. It is
needed because the notifier of NQ0 can run while the interface is down
and the rmap is being freed.

Signed-off-by: Vikas Gupta <vikas.gupta@broadcom.com>
Reviewed-by: Dharmender Garg <dharmender.garg@broadcom.com>
---
 .../net/ethernet/broadcom/bnge/bnge_netdev.c  | 201 +++++++++++++++++-
 .../net/ethernet/broadcom/bnge/bnge_netdev.h  |  19 ++
 .../net/ethernet/broadcom/bnge/bnge_resc.h    |   8 +
 3 files changed, 225 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/bnge/bnge_netdev.c b/drivers/net/ethernet/broadcom/bnge/bnge_netdev.c
index de7320521a3e..54ea9aa5b0d3 100644
--- a/drivers/net/ethernet/broadcom/bnge/bnge_netdev.c
+++ b/drivers/net/ethernet/broadcom/bnge/bnge_netdev.c
@@ -6,6 +6,7 @@
 #include <linux/dmapool.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
+#include <linux/irq.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/pci.h>
@@ -18,6 +19,7 @@
 #include <net/ip.h>
 #include <linux/skbuff.h>
 #include <net/page_pool/helpers.h>
+#include <linux/cpu_rmap.h>
 
 #include "bnge.h"
 #include "bnge_hwrm.h"
@@ -2539,10 +2541,28 @@ static int bnge_setup_interrupts(struct bnge_net *bn)
 {
 	struct net_device *dev = bn->netdev;
 	struct bnge_dev *bd = bn->bd;
+	int rc;
 
 	bnge_setup_msix(bn);
 
-	return netif_set_real_num_queues(dev, bd->tx_nr_rings, bd->rx_nr_rings);
+	rc = netif_set_real_num_queues(dev, bd->tx_nr_rings, bd->rx_nr_rings);
+	if (rc)
+		return rc;
+
+#ifdef CONFIG_RFS_ACCEL
+	if (bn->priv_flags & BNGE_NET_EN_NTUPLE) {
+		struct cpu_rmap *rmap = alloc_irq_cpu_rmap(bd->rx_nr_rings);
+
+		if (!rmap)
+			netdev_warn(dev, "Unable to allocate the CPU rmap, aRFS disabled\n");
+		/* Publish under rmap_lock: the NQ0 notifier reads it. */
+		mutex_lock(&bn->rmap_lock);
+		dev->rx_cpu_rmap = rmap;
+		mutex_unlock(&bn->rmap_lock);
+	}
+#endif
+
+	return rc;
 }
 
 static void bnge_hwrm_resource_free(struct bnge_net *bn, bool close_path)
@@ -2552,14 +2572,155 @@ static void bnge_hwrm_resource_free(struct bnge_net *bn, bool close_path)
 	bnge_hwrm_stat_ctx_free(bn);
 }
 
+#ifdef CONFIG_RFS_ACCEL
+/* The aRFS cpu_rmap is indexed by RX ring number. The affinity notifier updates
+ * the rmap entry of a ring whenever the affinity of its IRQ changes, and
+ * bnge_nq0_seed_cpu_rmap() writes the first entry of ring 0 at open, from the
+ * affinity of NQ0 that the notifier kept in bn->nq0_aff. The notifier does not
+ * touch @cpu_mask, which only holds the affinity hint of the IRQ.
+ * bn->rmap_lock orders both writers against each other and against
+ * bnge_free_cpu_rmap() tearing the rmap down, which matters because NQ0's
+ * notifier is persistent (probe to remove) and can fire while the interface is
+ * down.
+ */
+static void bnge_irq_affinity_notify(struct irq_affinity_notify *notify,
+				     const cpumask_t *mask)
+{
+	struct bnge_irq *irq = container_of(notify, struct bnge_irq,
+					    affinity_notify);
+	struct bnge_net *bn = irq->bn;
+	struct cpu_rmap *rmap;
+	int rc;
+
+	mutex_lock(&bn->rmap_lock);
+	/* Keep the mask of NQ0 even if there is no rmap, so that the seed at
+	 * the next open does not depend on a move being already applied.
+	 */
+	if (!irq->ring_nr) {
+		cpumask_copy(&bn->nq0_aff, mask);
+		bn->nq0_aff_valid = true;
+	}
+	rmap = bn->netdev->rx_cpu_rmap;
+	if (rmap && irq->ring_nr < bn->bd->rx_nr_rings) {
+		rc = cpu_rmap_update(rmap, irq->ring_nr, mask);
+		if (rc)
+			netdev_warn(bn->netdev,
+				    "aRFS rmap update failed: %d\n", rc);
+	}
+	mutex_unlock(&bn->rmap_lock);
+}
+
+static void bnge_irq_affinity_release(struct kref *ref)
+{
+	/* The bnge_irq and its notifier are owned by irq_tbl, nothing to do. */
+}
+
+static void bnge_register_irq_notifier(struct bnge_net *bn,
+				       struct bnge_irq *irq)
+{
+	struct irq_affinity_notify *notify = &irq->affinity_notify;
+	int rc;
+
+	irq->bn = bn;
+	notify->irq = irq->vector;
+	notify->notify = bnge_irq_affinity_notify;
+	notify->release = bnge_irq_affinity_release;
+	rc = irq_set_affinity_notifier(irq->vector, notify);
+	if (rc)
+		netdev_warn(bn->netdev,
+			    "Set affinity notifier failed, IRQ = %d: %d\n",
+			    irq->vector, rc);
+}
+
+static void bnge_release_irq_notifier(struct bnge_irq *irq)
+{
+	/* Cancels any pending notify work, so it cannot touch the rmap after
+	 * this returns.
+	 */
+	irq_set_affinity_notifier(irq->vector, NULL);
+}
+
+/* set_rps_cpu() reads rx_cpu_rmap under rcu_read_lock(). Hide the pointer so
+ * that new readers do not see it, wait for the readers that already loaded it,
+ * and serialise against the affinity notifier via rmap_lock before the rmap is
+ * freed. set_rps_cpu() loads the pointer twice with plain loads, so a reader
+ * that is between the two loads is not covered; that has to be fixed in the
+ * core.
+ */
+static void bnge_free_cpu_rmap(struct bnge_net *bn)
+{
+	struct cpu_rmap *rmap;
+
+	mutex_lock(&bn->rmap_lock);
+	rmap = bn->netdev->rx_cpu_rmap;
+	WRITE_ONCE(bn->netdev->rx_cpu_rmap, NULL);
+	mutex_unlock(&bn->rmap_lock);
+
+	if (!rmap)
+		return;
+
+	synchronize_net();
+	free_irq_cpu_rmap(rmap);
+}
+
+static void bnge_nq0_seed_cpu_rmap(struct bnge_net *bn)
+{
+	const struct cpumask *mask;
+	unsigned int vector;
+	u16 map_idx;
+	int rc;
+
+	map_idx = bn->nq0->nq_ring.ring_struct.map_idx;
+	vector = bn->bd->irq_tbl[map_idx].vector;
+
+	mutex_lock(&bn->rmap_lock);
+	/* Prefer the mask reported by the notifier. The IRQ core may still
+	 * hold a newer affinity of NQ0 as a pending move that the applied
+	 * mask does not show yet.
+	 */
+	if (bn->nq0_aff_valid)
+		mask = &bn->nq0_aff;
+	else
+		mask = irq_get_affinity_mask(vector);
+	if (bn->netdev->rx_cpu_rmap && mask) {
+		rc = cpu_rmap_update(bn->netdev->rx_cpu_rmap, 0, mask);
+		if (rc)
+			netdev_warn(bn->netdev,
+				    "aRFS rmap seed failed: %d\n", rc);
+	}
+	mutex_unlock(&bn->rmap_lock);
+}
+#else
+static void bnge_register_irq_notifier(struct bnge_net *bn,
+				       struct bnge_irq *irq)
+{
+}
+
+static void bnge_release_irq_notifier(struct bnge_irq *irq)
+{
+}
+
+static void bnge_free_cpu_rmap(struct bnge_net *bn)
+{
+}
+
+static void bnge_nq0_seed_cpu_rmap(struct bnge_net *bn)
+{
+}
+#endif
+
 static void bnge_nq_free_irq(struct bnge_net *bn, struct bnge_napi *bnapi)
 {
 	int map_idx = bnapi->nq_ring.ring_struct.map_idx;
 	struct bnge_irq *irq = &bn->bd->irq_tbl[map_idx];
 
 	if (irq->requested) {
+		/* Drop the affinity notifier before freeing the IRQ and the
+		 * rmap.
+		 */
+		bnge_release_irq_notifier(irq);
 		if (irq->have_cpumask) {
-			irq_set_affinity_hint(irq->vector, NULL);
+			irq_update_affinity_hint(irq->vector, NULL);
 			free_cpumask_var(irq->cpu_mask);
 			irq->have_cpumask = 0;
 		}
@@ -2583,6 +2744,16 @@ static int bnge_nq_request_irq(struct bnge_net *bn, struct bnge_napi *bnapi)
 		return rc;
 
 	irq->requested = 1;
+	irq->ring_nr = bnapi->index;
+
+	/* The affinity notifier keeps the aRFS rmap entry of this ring
+	 * current. It does not use cpu_mask, so install it independent of
+	 * the allocation of the hint mask below. NQ0 is requested at probe,
+	 * when it has no rx_ring yet, and its notifier must exist from then
+	 * on.
+	 */
+	if (bnapi->rx_ring || bnapi == bn->nq0)
+		bnge_register_irq_notifier(bn, irq);
 
 	if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
 		int numa_node = dev_to_node(&bd->pdev->dev);
@@ -2590,9 +2761,12 @@ static int bnge_nq_request_irq(struct bnge_net *bn, struct bnge_napi *bnapi)
 		irq->have_cpumask = 1;
 		cpumask_set_cpu(cpumask_local_spread(bnapi->index, numa_node),
 				irq->cpu_mask);
-		if (irq_set_affinity_hint(irq->vector, irq->cpu_mask))
+		if (irq_set_affinity_and_hint(irq->vector, irq->cpu_mask))
 			bnge_warn(bd, "Set affinity failed, IRQ = %d\n",
 				  irq->vector);
+	} else {
+		bnge_warn(bd, "No affinity hint mask, IRQ = %d\n",
+			  irq->vector);
 	}
 	return 0;
 }
@@ -2602,8 +2776,14 @@ static void bnge_free_irq(struct bnge_net *bn)
 	struct bnge_dev *bd = bn->bd;
 	int i;
 
+	/* Release the open NQ notifiers (and their IRQs) before the rmap, so no
+	 * notify work can reference it while it is freed. NQ0's notifier is
+	 * persistent and is serialised against the free via rmap_lock instead.
+	 */
 	bnge_for_each_open_nq(i, bd)
 		bnge_nq_free_irq(bn, bn->bnapi[i]);
+
+	bnge_free_cpu_rmap(bn);
 }
 
 static int bnge_request_irq(struct bnge_net *bn)
@@ -2616,6 +2796,14 @@ static int bnge_request_irq(struct bnge_net *bn)
 		netdev_err(bn->netdev, "bnge_setup_interrupts err: %d\n", rc);
 		return rc;
 	}
+
+	/* NQ0 is persistent (requested at probe) and is not one of the open NQs
+	 * below, so it is not re-requested here and its affinity is left
+	 * untouched. Seed the freshly allocated rmap entry of RX ring 0 from
+	 * the current affinity of NQ0.
+	 */
+	bnge_nq0_seed_cpu_rmap(bn);
+
 	bnge_for_each_open_nq(i, bd) {
 		rc = bnge_nq_request_irq(bn, bn->bnapi[i]);
 		if (rc)
@@ -3749,6 +3937,11 @@ int bnge_netdev_alloc(struct bnge_dev *bd, int max_irqs)
 	bn->netdev = netdev;
 	bn->bd = bd;
 
+	/* Must be ready before bnge_nq0_init() requests NQ0's IRQ, whose
+	 * affinity notifier takes this lock.
+	 */
+	mutex_init(&bn->rmap_lock);
+
 	netdev->min_mtu = ETH_ZLEN;
 	netdev->max_mtu = bd->max_mtu;
 
@@ -3885,6 +4078,7 @@ int bnge_netdev_alloc(struct bnge_dev *bd, int max_irqs)
 err_free_workq:
 	destroy_workqueue(bn->bnge_pf_wq);
 err_netdev:
+	mutex_destroy(&bn->rmap_lock);
 	free_netdev(netdev);
 	bd->netdev = NULL;
 	return rc;
@@ -3913,6 +4107,7 @@ void bnge_netdev_free(struct bnge_dev *bd)
 
 	bnge_free_port_stats(bn);
 
+	mutex_destroy(&bn->rmap_lock);
 	free_netdev(netdev);
 	bd->netdev = NULL;
 }
diff --git a/drivers/net/ethernet/broadcom/bnge/bnge_netdev.h b/drivers/net/ethernet/broadcom/bnge/bnge_netdev.h
index 73879ba7f129..acd9c5045106 100644
--- a/drivers/net/ethernet/broadcom/bnge/bnge_netdev.h
+++ b/drivers/net/ethernet/broadcom/bnge/bnge_netdev.h
@@ -5,7 +5,9 @@
 #define _BNGE_NETDEV_H_
 
 #include <linux/bnge/hsi.h>
+#include <linux/cpumask.h>
 #include <linux/io-64-nonatomic-lo-hi.h>
+#include <linux/mutex.h>
 #include <linux/refcount.h>
 #include <linux/u64_stats_sync.h>
 #include <net/netdev_queues.h>
@@ -395,6 +397,23 @@ struct bnge_net {
 	 * lock instead.
 	 */
 	spinlock_t		ntp_fltr_lock;
+
+	/* Serialises aRFS cpu_rmap updates (IRQ affinity notifier, NQ0 seed)
+	 * against the creation and teardown of the rmap. Needed because NQ0's
+	 * notifier is persistent (probe to remove) and can fire while the
+	 * interface is down and the rmap is being freed.
+	 */
+	struct mutex		rmap_lock;
+
+	/* The affinity of NQ0 as last reported by its notifier, kept under
+	 * rmap_lock. NQ0's notifier runs from probe on, when there is no rmap
+	 * yet. A move that the IRQ core defers is reported by the notifier but
+	 * is not returned by irq_get_affinity_mask() until the next interrupt,
+	 * so the seed of RX ring 0 at open reads this copy. @nq0_aff_valid
+	 * tells that @nq0_aff holds a reported mask.
+	 */
+	cpumask_t		nq0_aff;
+	bool			nq0_aff_valid;
 };
 
 #define BNGE_DEFAULT_RX_RING_SIZE	511
diff --git a/drivers/net/ethernet/broadcom/bnge/bnge_resc.h b/drivers/net/ethernet/broadcom/bnge/bnge_resc.h
index 99354ebbe911..be9e753babc8 100644
--- a/drivers/net/ethernet/broadcom/bnge/bnge_resc.h
+++ b/drivers/net/ethernet/broadcom/bnge/bnge_resc.h
@@ -4,6 +4,7 @@
 #ifndef _BNGE_RESC_H_
 #define _BNGE_RESC_H_
 
+#include <linux/interrupt.h>
 #include "bnge_netdev.h"
 #include "bnge_rmem.h"
 
@@ -61,6 +62,13 @@ struct bnge_irq {
 	u8		have_cpumask:1;
 	char		name[IFNAMSIZ + BNGE_IRQ_NAME_EXTRA];
 	cpumask_var_t	cpu_mask;
+	/* aRFS IRQ affinity notifier state. @cpu_mask above only holds the
+	 * affinity hint of the IRQ. @bn and @ring_nr are used by the notifier:
+	 * @ring_nr is the RX ring (and rmap) index this vector serves.
+	 */
+	struct bnge_net	*bn;
+	int		ring_nr;
+	struct irq_affinity_notify affinity_notify;
 };
 
 int bnge_reserve_rings(struct bnge_dev *bd);
-- 
2.52.0


      parent reply	other threads:[~2026-12-01 13:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 13:20 [PATCH net-next v12 00/10] add features to bnge Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 01/10] bnge: restructure VNIC and filter code Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 02/10] bnge: add NTUPLE/ARFS VNIC Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 03/10] bnge: add helper functions for multi RSS contexts Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 04/10] bnge: add RXFH ethtool support Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 05/10] bnge: add ethtool support to manage RSS contexts Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 06/10] bnge: remove refcount from L2 filter Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 07/10] bnge: add NTUPLE filter infrastructure Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 08/10] bnge: add NTUPLE filter support in ethtool Vikas Gupta
2026-12-01 13:20 ` [PATCH net-next v12 09/10] bnge: add aRFS flow steering ndo support Vikas Gupta
2026-12-01 13:20 ` Vikas Gupta [this message]

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=20261201132011.2705569-11-vikas.gupta@broadcom.com \
    --to=vikas.gupta@broadcom.com \
    --cc=ajit.khaparde@broadcom.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=bhargava.marreddy@broadcom.com \
    --cc=davem@davemloft.net \
    --cc=dharmender.garg@broadcom.com \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rahul-rg.gupta@broadcom.com \
    --cc=rajashekar.hudumula@broadcom.com \
    --cc=vsrama-krishna.nemani@broadcom.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