Skip to contentSkip to conversation

net: Split ndo_set_rx_mode into snapshot and deferred write

RFC PATCH Superseded I Viswanath 3 participants 7 messages Oct 20, 2025 – Oct 24, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover net: Split ndo_set_rx_mode into snapshot and deferred write

This is an implementation of the idea provided by Jakub here

https://lore.kernel.org/netdev/[email protected]/

ndo_set_rx_mode is problematic because it cannot sleep.

To address this, this series proposes dividing existing set_rx_mode implementations into set_rx_mode and write_rx_config

The new set_rx_mode will be responsible for updating the rx_config snapshot which will be used by ndo_write_rx_config to update the hardware

In brief, The callback implementations should look something like:

set_rx_mode():

    prepare_rx_config();
    update_snapshot();

write_rx_mode():

    read_snapshot();
    do_io();

write_rx_mode() is called from a work item making it sleepable during the do_io() section.

This model should work correctly if the following conditions hold:

1. write_rx_config should use the rx_config set by the most recent call to set_rx_mode before its execution.

2. If a set_rx_mode call happens during execution of write_rx_config, write_rx_config should be rescheduled.

3. All calls to modify rx_mode should pass through the set_rx_mode + schedule write_rx_config execution flow.

1 and 2 are guaranteed because of the properties of work queues

Drivers need to ensure 3

ndo_write_rx_config has been implemented for 8139cp driver as proof of concept

To use this model, a driver needs to implement the ndo_write_rx_config callback, have a member rx_config in the priv struct and replace all calls to set rx mode with schedule_and_set_rx_mode();

I Viswanath (2):
net: Add ndo_write_rx_config and helper structs and functions:
net: ethernet: Implement ndo_write_rx_config callback for the 8139cp
driver

 drivers/net/ethernet/realtek/8139cp.c | 67 ++++++++++++++++++---------
 include/linux/netdevice.h             | 38 ++++++++++++++-
 net/core/dev.c                        | 48 +++++++++++++++++--
 3 files changed, 127 insertions(+), 26 deletions(-)

---
I tested the correctness of this by comparing rx_config request in set_rx_config and written values in write_rx_config.

The prints I used:

After new_config is set in set_rx_mode:

printk("Requested Values: rx_config: %8x, mc_filter[0]: %8x, mc_filter[1]: %8x", new_config.rx_mode, new_config.mc_filter[0], new_config.mc_filter[1]);

After the writes in write_rx_config:

printk("RxConfig: %8x", cpr32(RxConfig));
printk("MC Filter[0]: %8x, MC Filter[1]: %8x", cpr32(MAR0 + 0), cpr32(MAR0 + 4));

Is this sufficient testing for a proof of concept?

I picked 8139cp because I could emulate it on QEMU and it was somewhat easy to refactor.

Signature · 1 line
2.47.3
#1 lore raw

RFC PATCH 1/2 net: Add ndo_write_rx_config and helper structs and functions.

Add ndo_write_rx_config callback and following helper structs/functions:

rx_config_work - To schedule the callback and handle synchronization

read_snapshot/update_snapshot - Helper functions to read/update the
rx_config snapshot

set_and_schedule_rx_config - Helper function to call ndo_set_rx_mode
and schedule ndo_write_rx_config

execute_write_rx_config - Helper function that will be scheduled
by rx_work->config_write

---
I expect that shallow copy should be good enough as rx_config should consist exclusively of integer types (primitives and arrays)

Would flushing the work queue be necessary for functions like *_init_hw()?

 include/linux/netdevice.h | 38 ++++++++++++++++++++++++++++++-
 net/core/dev.c            | 48 +++++++++++++++++++++++++++++++++++----
 2 files changed, 81 insertions(+), 5 deletions(-)
include/linux/netdevice.h +37 -1
index d1a687444b27..37a48e41a004 100644
@@ -1100,7 +1100,12 @@ struct netdev_net_notifier {
  * void (*ndo_set_rx_mode)(struct net_device *dev);
  *	This function is called device changes address list filtering.
  *	If driver handles unicast address filtering, it should set
- *	IFF_UNICAST_FLT in its priv_flags.
+ *	IFF_UNICAST_FLT in its priv_flags. This sets up the snapshot of
+ *	rx_config that will be written to the device.
+ *
+ * void (*ndo_write_rx_config)(struct net_device *dev);
+ *	This function is scheduled immediately after ndo_set_rx_mode to
+ *	write rx_config to the device.
  *
  * int (*ndo_set_mac_address)(struct net_device *dev, void *addr);
  *	This function  is called when the Media Access Control address
@@ -1421,6 +1426,7 @@ struct net_device_ops {
 	void			(*ndo_change_rx_flags)(struct net_device *dev,
 						       int flags);
 	void			(*ndo_set_rx_mode)(struct net_device *dev);
+	void			(*ndo_write_rx_config)(struct net_device *dev);
 	int			(*ndo_set_mac_address)(struct net_device *dev,
 						       void *addr);
 	int			(*ndo_validate_addr)(struct net_device *dev);
@@ -1767,6 +1773,12 @@ enum netdev_reg_state {
 	NETREG_DUMMY,		/* dummy device for NAPI poll */
 };
 
+struct rx_config_work {
+	struct work_struct config_write;
+	struct net_device *dev;
+	spinlock_t config_lock;
+};
+
 /**
  *	struct net_device - The DEVICE structure.
  *
@@ -2082,6 +2094,8 @@ enum netdev_reg_state {
  *			dev_list, one per address-family.
  *	@hwprov: Tracks which PTP performs hardware packet time stamping.
  *
+ *	@rx_work: helper struct to schedule rx config write to the hardware.
+ *
  *	FIXME: cleanup struct net_device such that network protocol info
  *	moves out.
  */
@@ -2559,6 +2573,8 @@ struct net_device {
 
 	struct hwtstamp_provider __rcu	*hwprov;
 
+	struct rx_config_work *rx_work;
+
 	u8			priv[] ____cacheline_aligned
 				       __counted_by(priv_len);
 } ____cacheline_aligned;
@@ -2734,6 +2750,26 @@ void dev_net_set(struct net_device *dev, struct net *net)
 	write_pnet(&dev->nd_net, net);
 }
 
+#define update_snapshot(config_ptr, type)						\
+	do {										\
+		typeof((config_ptr)) rx_config = ((type *)(dev->priv))->rx_config;	\
+		unsigned long flags;							\
+		spin_lock_irqsave(&((dev)->rx_work->config_lock), flags);		\
+		*rx_config = *(config_ptr);						\
+		spin_unlock_irqrestore(&((dev)->rx_work->config_lock), flags);		\
+	} while (0)
+
+#define read_snapshot(config_ptr, type)							\
+	do {										\
+		typeof((config_ptr)) rx_config = ((type *)(dev->priv))->rx_config;	\
+		unsigned long flags;							\
+		spin_lock_irqsave(&((dev)->rx_work->config_lock), flags);		\
+		*(config_ptr) = *rx_config;						\
+		spin_unlock_irqrestore(&((dev)->rx_work->config_lock), flags);		\
+	} while (0)
+
+void set_and_schedule_rx_config(struct net_device *dev, bool flush);
+
 /**
  *	netdev_priv - access network device private data
  *	@dev: network device
net/core/dev.c +44 -4

78 lines not on this page · raw message

Signature · 1 line
2.47.3
#2 lore raw

RFC PATCH 2/2 net: ethernet: Implement ndo_write_rx_config callback for the 8139cp driver

In reply to I Viswanath

Implement ndo_write_rx_config for the 8139cp driver

---
I modified and set ndo_set_rx_mode to be __cp_set_rx_mode as using cp_set_rx_mode as ndo_set_rx_mode callback would have led to a deadlock in cp_init_hw()

 drivers/net/ethernet/realtek/8139cp.c | 67 ++++++++++++++++++---------
 1 file changed, 46 insertions(+), 21 deletions(-)
drivers/net/ethernet/realtek/8139cp.c +46 -21
index 5652da8a178c..ff6195c7f3b6 100644
@@ -319,6 +319,11 @@ struct cp_extra_stats {
 	unsigned long		rx_frags;
 };
 
+struct cp_rx_config {
+	int rx_mode;
+	u32 mc_filter[2];	/* Multicast hash filter */
+};
+
 struct cp_private {
 	void			__iomem *regs;
 	struct net_device	*dev;
@@ -328,7 +333,7 @@ struct cp_private {
 	struct napi_struct	napi;
 
 	struct pci_dev		*pdev;
-	u32			rx_config;
+	struct cp_rx_config	*rx_config;
 	u16			cpcmd;
 
 	struct cp_extra_stats	cp_stats;
@@ -882,45 +887,53 @@ static netdev_tx_t cp_start_xmit (struct sk_buff *skb,
 	goto out_unlock;
 }
 
+static void cp_write_rx_config(struct net_device *dev)
+{
+	struct cp_private *cp = netdev_priv(dev);
+	struct cp_rx_config snapshot;
+
+	read_snapshot((&snapshot), struct cp_private);
+
+	/* We can safely update without stopping the chip. */
+	cpw32_f(RxConfig, snapshot.rx_mode);
+
+	cpw32_f(MAR0 + 0, snapshot.mc_filter[0]);
+	cpw32_f(MAR0 + 4, snapshot.mc_filter[1]);
+}
+
 /* Set or clear the multicast filter for this adaptor.
    This routine is not state sensitive and need not be SMP locked. */
 
 static void __cp_set_rx_mode (struct net_device *dev)
 {
-	struct cp_private *cp = netdev_priv(dev);
-	u32 mc_filter[2];	/* Multicast hash filter */
-	int rx_mode;
+	struct cp_rx_config new_config;
 
 	/* Note: do not reorder, GCC is clever about common statements. */
 	if (dev->flags & IFF_PROMISC) {
 		/* Unconditionally log net taps. */
-		rx_mode =
+		new_config.rx_mode =
 		    AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
 		    AcceptAllPhys;
-		mc_filter[1] = mc_filter[0] = 0xffffffff;
+		new_config.mc_filter[1] = new_config.mc_filter[0] = 0xffffffff;
 	} else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
 		   (dev->flags & IFF_ALLMULTI)) {
 		/* Too many to filter perfectly -- accept all multicasts. */
-		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
-		mc_filter[1] = mc_filter[0] = 0xffffffff;
+		new_config.rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
+		new_config.mc_filter[1] = new_config.mc_filter[0] = 0xffffffff;
 	} else {
 		struct netdev_hw_addr *ha;
-		rx_mode = AcceptBroadcast | AcceptMyPhys;
-		mc_filter[1] = mc_filter[0] = 0;
+		new_config.rx_mode = AcceptBroadcast | AcceptMyPhys;
+		new_config.mc_filter[1] = new_config.mc_filter[0] = 0;
 		netdev_for_each_mc_addr(ha, dev) {
 			int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
 
-			mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
-			rx_mode |= AcceptMulticast;
+			new_config.mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
+			new_config.rx_mode |= AcceptMulticast;
 		}
 	}
 
-	/* We can safely update without stopping the chip. */
-	cp->rx_config = cp_rx_config | rx_mode;
-	cpw32_f(RxConfig, cp->rx_config);
-
-	cpw32_f (MAR0 + 0, mc_filter[0]);
-	cpw32_f (MAR0 + 4, mc_filter[1]);
+	new_config.rx_mode = cp_rx_config | new_config.rx_mode;
+	update_snapshot(&new_config, struct cp_private);
 }
 
 static void cp_set_rx_mode (struct net_device *dev)
@@ -1040,7 +1053,7 @@ static void cp_init_hw (struct cp_private *cp)
 	cp_start_hw(cp);
 	cpw8(TxThresh, 0x06); /* XXX convert magic num to a constant */
 
-	__cp_set_rx_mode(dev);
+	set_and_schedule_rx_config(dev, true);
 	cpw32_f (TxConfig, IFG | (TX_DMA_BURST << TxDMAShift));
 
 	cpw8(Config1, cpr8(Config1) | DriverLoaded | PMEnable);
@@ -1188,6 +1201,12 @@ static int cp_open (struct net_device *dev)
 	if (rc)
 		return rc;
 
+	cp->rx_config = kmalloc(sizeof(*cp->rx_config), GFP_KERNEL);
+	if (!cp->rx_config) {
+		rc = -ENOMEM;
+		goto err_out_rx_config;
+	}
+
 	napi_enable(&cp->napi);
 
 	cp_init_hw(cp);
@@ -1207,6 +1226,9 @@ static int cp_open (struct net_device *dev)
 err_out_hw:
 	napi_disable(&cp->napi);
 	cp_stop_hw(cp);
+	kfree(cp->rx_config);
+
+err_out_rx_config:
 	cp_free_rings(cp);
 	return rc;
 }
@@ -1227,6 +1249,8 @@ static int cp_close (struct net_device *dev)
 
 	cp_stop_hw(cp);
 
+	kfree(cp->rx_config);
+
 	spin_unlock_irqrestore(&cp->lock, flags);
 
 	free_irq(cp->pdev->irq, dev);
@@ -1262,7 +1286,7 @@ static void cp_tx_timeout(struct net_device *dev, unsigned int txqueue)
 	cp_clean_rings(cp);
 	cp_init_rings(cp);
 	cp_start_hw(cp);
-	__cp_set_rx_mode(dev);
+	set_and_schedule_rx_config(dev, false);
 	cpw16_f(IntrMask, cp_norx_intr_mask);
 
 	netif_wake_queue(dev);
@@ -1869,7 +1893,8 @@ static const struct net_device_ops cp_netdev_ops = {
 	.ndo_stop		= cp_close,
 	.ndo_validate_addr	= eth_validate_addr,
 	.ndo_set_mac_address 	= cp_set_mac_address,
-	.ndo_set_rx_mode	= cp_set_rx_mode,
+	.ndo_set_rx_mode	= __cp_set_rx_mode,
+	.ndo_write_rx_config    = cp_write_rx_config,
 	.ndo_get_stats		= cp_get_stats,
 	.ndo_eth_ioctl		= cp_ioctl,
 	.ndo_start_xmit		= cp_start_xmit,
Signature · 1 line
2.47.3
#3 lore raw

Re: [RFC net-next PATCH 2/2] net: ethernet: Implement ndo_write_rx_config callback for the 8139cp driver

kernel test robot noticed the following build warnings:

Hi Viswanath,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/I-Viswanath/net-Add-ndo_write_rx_config-and-helper-structs-and-functions/20251020-215339
base:   net-next/main
patch link:    https://lore.kernel.org/r/20251020134857.5820-3-viswanathiyyappan%40gmail.com
patch subject: [RFC net-next PATCH 2/2] net: ethernet: Implement ndo_write_rx_config callback for the 8139cp driver
config: alpha-allmodconfig (https://download.01.org/0day-ci/archive/20251023/[email protected]/config)
compiler: alpha-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251023/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/net/ethernet/realtek/8139cp.c:939:13: warning: 'cp_set_rx_mode' defined but not used [-Wunused-function]

939 | static void cp_set_rx_mode (struct net_device *dev)
| ^~~~~~~~~~~~~~

vim +/cp_set_rx_mode +939 drivers/net/ethernet/realtek/8139cp.c

^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16 938

^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16 @939  static void cp_set_rx_mode (struct net_device *dev)
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  940  {
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  941  	unsigned long flags;
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  942  	struct cp_private *cp = netdev_priv(dev);
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  943  
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  944  	spin_lock_irqsave (&cp->lock, flags);
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  945  	__cp_set_rx_mode(dev);
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  946  	spin_unlock_irqrestore (&cp->lock, flags);
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  947  }
^1da177e4c3f415 drivers/net/8139cp.c Linus Torvalds 2005-04-16  948  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
In reply to I Viswanath
Quoted 7 lines · I Viswanath

3. All calls to modify rx_mode should pass through the set_rx_mode + schedule write_rx_config execution flow.

1 and 2 are guaranteed because of the properties of work queues

Drivers need to ensure 3

Is there any mechanism to make this guarantee either implemented or at least verified by the core? If not that, what about some sort of way to lint driver code and make sure its correct?

In my experience, requiring drivers to do something right typically results in a long tail of correcting drivers into the future..

#5 lore raw
Quoted 4 lines · Jacob Keller

Is there any mechanism to make this guarantee either implemented or at least verified by the core? If not that, what about some sort of way to lint driver code and make sure its correct?

From my observations, The sane drivers modify rx_config related registers either through the set_rx_mode function or the unlocked version (prefixed with __)
I am not sure how to convert this to a validation of that kind.

Basically the end result should be that warnings are generated when those functions are called
normally but not when they are called through ops->set_rx_mode. Coccinelle might be able to do
something like this.

Related to this, I don't think a sed would be sufficient as there might be (in theory) cases where
the function has to do a "synchronous" rx write (flush the work queue) for correctness
but it should be good enough for most cases.

I am also not sure what is to be done if the scheduled function just never executes.

#6 lore raw
Quoted 11 lines · I Viswanath
Quoted 4 lines · Jacob Keller

Is there any mechanism to make this guarantee either implemented or at least verified by the core? If not that, what about some sort of way to lint driver code and make sure its correct?

From my observations, The sane drivers modify rx_config related registers either through the set_rx_mode function or the unlocked version (prefixed with __)
I am not sure how to convert this to a validation of that kind.

Right.

Quoted 6 lines

Basically the end result should be that warnings are generated when those functions are called
normally but not when they are called through ops->set_rx_mode. Coccinelle might be able to do
something like this.

Quoted 8 lines

Related to this, I don't think a sed would be sufficient as there might be (in theory) cases where
the function has to do a "synchronous" rx write (flush the work queue) for correctness
but it should be good enough for most cases.

I am also not sure what is to be done if the scheduled function just never executes.

I'm not sure what the best mechanism is for helping make sure drivers get it right. I just know that past experience shows that without some sort of check, we end up burdening reviewers with another thing they have to double check, and inevitably some drivers will screw it up and we'll end up with a long tail of fixes.

If we can't come up with something, I don't think it would prevent moving this forward. I just want to spend a little thought to make sure nothing obvious was missed.

Thanks,
Jake