Skip to contentSkip to conversation

Update network drivers to use devres

RFC PATCH In review [email protected] 3 participants 9 messages Jul 24, 2007 – Jul 25, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Update network drivers to use devres

These patches update the network driver core and the e100 driver to use devres. Devres is a simple resource manager for device drivers, see Documentation/driver-model/devres.txt for more information.

If the RFC goes well I will create patches to update all applicable network drivers.

Applies against 2.6.22

#1 lore raw

RFC PATCH 1/2 Update net core to use devres.

* netdev_pci_remove_one() can replace simple pci device remove functions

* devm_alloc_netdev() is like alloc_netdev but allocates memory using devres. alloc_netdev() can be removed once all drivers use devres.

Applies against 2.6.22

---
 include/linux/etherdevice.h |    2 +
 include/linux/netdevice.h   |    5 ++++
 net/core/dev.c              |   48 ++++++++++++++++++++++++++++++++++++++++----
 net/ethernet/eth.c          |    6 +++++
 4 files changed, 57 insertions(+), 4 deletions(-)
include/linux/netdevice.h +5 -0
Index: linux-2.6/include/linux/netdevice.h
===================================================================
@@ -622,6 +622,7 @@ extern int		dev_queue_xmit(struct sk_buf
 extern int		register_netdevice(struct net_device *dev);
 extern void		unregister_netdevice(struct net_device *dev);
 extern void		free_netdev(struct net_device *dev);
+extern void		netdev_pci_remove_one(struct pci_dev *pdev);
 extern void		synchronize_net(void);
 extern int 		register_netdevice_notifier(struct notifier_block *nb);
 extern int		unregister_netdevice_notifier(struct notifier_block *nb);
@@ -992,6 +993,10 @@ static inline void netif_tx_disable(stru
 extern void		ether_setup(struct net_device *dev);
 
 /* Support for loadable net-drivers */
+
+extern struct net_device *devm_alloc_netdev(struct device *dev,
+					    int sizeof_priv, const char *name,
+					    void (*setup)(struct net_device *));
 extern struct net_device *alloc_netdev(int sizeof_priv, const char *name,
 				       void (*setup)(struct net_device *));
 extern int		register_netdev(struct net_device *dev);
net/core/dev.c +44 -4

86 lines not on this page · raw message

include/linux/etherdevice.h +2 -0
Index: linux-2.6/include/linux/etherdevice.h
===================================================================
@@ -40,6 +40,8 @@ extern int		eth_header_cache(struct neig
 					 struct hh_cache *hh);
 
 extern struct net_device *alloc_etherdev(int sizeof_priv);
+extern struct net_device *devm_alloc_etherdev(struct device *dev,
+					      int sizeof_priv);
 static inline void eth_copy_and_sum (struct sk_buff *dest, 
 				     const unsigned char *src, 
 				     int len, int base)
net/ethernet/eth.c +6 -0
Index: linux-2.6/net/ethernet/eth.c
===================================================================
@@ -333,3 +333,9 @@ struct net_device *alloc_etherdev(int si
 	return alloc_netdev(sizeof_priv, "eth%d", ether_setup);
 }
 EXPORT_SYMBOL(alloc_etherdev);
+
+struct net_device *devm_alloc_etherdev(struct device *dev, int sizeof_priv)
+{
+	return devm_alloc_netdev(dev, sizeof_priv, "eth%d", ether_setup);
+}
+EXPORT_SYMBOL(devm_alloc_etherdev);
#2 lore raw

RFC PATCH 2/2 Update e100 driver to use devres.

devres manages device resources and is currently used by all libata low level
drivers. It can greatly reduce the complexity of the error handling on probe
and the device removal functions.

For example the e100_free() function and all of the gotos in e100_probe have been removed. Also, e100_remove() has been deleted and replaced with a much simpler netdev_pci_remove_one() function that should be able to handle any PCI net device that doesn't require any teardown besides resource deallocation.

Applies against 2.6.22.

---
 drivers/net/e100.c |   73 +++++++++++++----------------------------------------
 1 file changed, 19 insertions(+), 54 deletions(-)
drivers/net/e100.c +19 -54
Index: linux-2.6/drivers/net/e100.c
===================================================================
@@ -2514,18 +2514,11 @@ static int e100_do_ioctl(struct net_devi
 
 static int e100_alloc(struct nic *nic)
 {
-	nic->mem = pci_alloc_consistent(nic->pdev, sizeof(struct mem),
-		&nic->dma_addr);
-	return nic->mem ? 0 : -ENOMEM;
-}
+	struct device *dev = &nic->pdev->dev;
 
-static void e100_free(struct nic *nic)
-{
-	if(nic->mem) {
-		pci_free_consistent(nic->pdev, sizeof(struct mem),
-			nic->mem, nic->dma_addr);
-		nic->mem = NULL;
-	}
+	nic->mem = dmam_alloc_coherent(dev, sizeof(struct mem),
+		&nic->dma_addr, GFP_ATOMIC);
+	return nic->mem ? 0 : -ENOMEM;
 }
 
 static int e100_open(struct net_device *netdev)
@@ -2552,7 +2545,7 @@ static int __devinit e100_probe(struct p
 	struct nic *nic;
 	int err;
 
-	if(!(netdev = alloc_etherdev(sizeof(struct nic)))) {
+	if (!(netdev = devm_alloc_etherdev(&pdev->dev, sizeof(struct nic)))) {
 		if(((1 << debug) - 1) & NETIF_MSG_PROBE)
 			printk(KERN_ERR PFX "Etherdev alloc failed, abort.\n");
 		return -ENOMEM;
@@ -2582,26 +2575,26 @@ static int __devinit e100_probe(struct p
 	nic->msg_enable = (1 << debug) - 1;
 	pci_set_drvdata(pdev, netdev);
 
-	if((err = pci_enable_device(pdev))) {
+	if ((err = pcim_enable_device(pdev))) {
 		DPRINTK(PROBE, ERR, "Cannot enable PCI device, aborting.\n");
-		goto err_out_free_dev;
+		return err;
 	}
 
 	if(!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
 		DPRINTK(PROBE, ERR, "Cannot find proper PCI device "
 			"base address, aborting.\n");
 		err = -ENODEV;
-		goto err_out_disable_pdev;
+		return err;
 	}
 
 	if((err = pci_request_regions(pdev, DRV_NAME))) {
 		DPRINTK(PROBE, ERR, "Cannot obtain PCI resources, aborting.\n");
-		goto err_out_disable_pdev;
+		return err;
 	}
 
 	if((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
 		DPRINTK(PROBE, ERR, "No usable DMA configuration, aborting.\n");
-		goto err_out_free_res;
+		return err;
 	}
 
 	SET_MODULE_OWNER(netdev);
@@ -2610,11 +2603,11 @@ static int __devinit e100_probe(struct p
 	if (use_io)
 		DPRINTK(PROBE, INFO, "using i/o access mode\n");
 
-	nic->csr = pci_iomap(pdev, (use_io ? 1 : 0), sizeof(struct csr));
+	nic->csr = pcim_iomap(pdev, (use_io ? 1 : 0), sizeof(struct csr));
 	if(!nic->csr) {
 		DPRINTK(PROBE, ERR, "Cannot map device registers, aborting.\n");
 		err = -ENOMEM;
-		goto err_out_free_res;
+		return err;
 	}
 
 	if(ent->driver_data)
@@ -2647,11 +2640,11 @@ static int __devinit e100_probe(struct p
 
 	if((err = e100_alloc(nic))) {
 		DPRINTK(PROBE, ERR, "Cannot alloc driver memory, aborting.\n");
-		goto err_out_iounmap;
+		return err;
 	}
 
 	if((err = e100_eeprom_load(nic)))
-		goto err_out_free;
+		return err;
 
 	e100_phy_init(nic);
 
@@ -2661,8 +2654,8 @@ static int __devinit e100_probe(struct p
 		if (!eeprom_bad_csum_allow) {
 			DPRINTK(PROBE, ERR, "Invalid MAC address from "
 			        "EEPROM, aborting.\n");
-			err = -EAGAIN;
-			goto err_out_free;
+
+			return -EAGAIN;
 		} else {
 			DPRINTK(PROBE, ERR, "Invalid MAC address from EEPROM, "
 			        "you MUST configure one.\n");
@@ -2682,7 +2675,7 @@ static int __devinit e100_probe(struct p
 	strcpy(netdev->name, "eth%d");
 	if((err = register_netdev(netdev))) {
 		DPRINTK(PROBE, ERR, "Cannot register net device, aborting.\n");
-		goto err_out_free;
+		return err;
 	}
 
 	DPRINTK(PROBE, INFO, "addr 0x%llx, irq %d, "
@@ -2692,36 +2685,8 @@ static int __devinit e100_probe(struct p
 		netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
 
 	return 0;
-
-err_out_free:
-	e100_free(nic);
-err_out_iounmap:
-	pci_iounmap(pdev, nic->csr);
-err_out_free_res:
-	pci_release_regions(pdev);
-err_out_disable_pdev:
-	pci_disable_device(pdev);
-err_out_free_dev:
-	pci_set_drvdata(pdev, NULL);
-	free_netdev(netdev);
-	return err;
 }
 
-static void __devexit e100_remove(struct pci_dev *pdev)
-{
-	struct net_device *netdev = pci_get_drvdata(pdev);
-
-	if(netdev) {
-		struct nic *nic = netdev_priv(netdev);
-		unregister_netdev(netdev);
-		e100_free(nic);
-		iounmap(nic->csr);
-		free_netdev(netdev);
-		pci_release_regions(pdev);
-		pci_disable_device(pdev);
-		pci_set_drvdata(pdev, NULL);
-	}
-}
 
 #ifdef CONFIG_PM
 static int e100_suspend(struct pci_dev *pdev, pm_message_t state)
@@ -2825,7 +2790,7 @@ static pci_ers_result_t e100_io_slot_res
 	struct net_device *netdev = pci_get_drvdata(pdev);
 	struct nic *nic = netdev_priv(netdev);
 
-	if (pci_enable_device(pdev)) {
+	if (pcim_enable_device(pdev)) {
 		printk(KERN_ERR "e100: Cannot re-enable PCI device after reset.\n");
 		return PCI_ERS_RESULT_DISCONNECT;
 	}
@@ -2872,7 +2837,7 @@ static struct pci_driver e100_driver = {
 	.name =         DRV_NAME,
 	.id_table =     e100_id_table,
 	.probe =        e100_probe,
-	.remove =       __devexit_p(e100_remove),
+	.remove =       __devexit_p(netdev_pci_remove_one),
 #ifdef CONFIG_PM
 	/* Power Management hooks */
 	.suspend =      e100_suspend,
#3 lore raw

Re: [PATCH 1/2][RFC] Update net core to use devres.

In reply to [email protected]
Quoted 5 lines · On Tue, Jul 24, 2007 at 03:39:52PM -0700, [email protected] wrote:

* netdev_pci_remove_one() can replace simple pci device remove functions

* devm_alloc_netdev() is like alloc_netdev but allocates memory using devres. alloc_netdev() can be removed once all drivers use devres.

Ewwww... What the hell for? To make sure that we have struct device for everything, whether we need it or not? Have you actually read through drivers/net? I mean, _really_ read through it, looking for ugly cases...

I've done just that several times and I'm sorry, but I would classify that project as hopeless. It's way, _way_ more diverse than SATA...

#4 lore raw

Re: [PATCH 1/2][RFC] Update net core to use devres.

Quoted 7 lines · Al Viro
Quoted 5 lines · On Tue, Jul 24, 2007 at 03:39:52PM -0700, [email protected] wrote:

* netdev_pci_remove_one() can replace simple pci device remove functions

* devm_alloc_netdev() is like alloc_netdev but allocates memory using devres. alloc_netdev() can be removed once all drivers use devres.

Quoted 7 lines

Ewwww... What the hell for? To make sure that we have struct device for everything, whether we need it or not? Have you actually read through drivers/net? I mean, _really_ read through it, looking for ugly cases...

I've done just that several times and I'm sorry, but I would classify that project as hopeless. It's way, _way_ more diverse than SATA...

Actually, it's even worse - net_device itself simply *cannot* be dealt with that way. Its lifetime can indefinitely exceed that of underlying e.g. PCI device.

#5 lore raw

Re: [PATCH 1/2][RFC] Update net core to use devres.

Quoted 13 lines · Al Viro
Quoted 12 lines · Al Viro
Quoted 5 lines · On Tue, Jul 24, 2007 at 03:39:52PM -0700, [email protected] wrote:

* netdev_pci_remove_one() can replace simple pci device remove functions

* devm_alloc_netdev() is like alloc_netdev but allocates memory using devres. alloc_netdev() can be removed once all drivers use devres.

Ewwww... What the hell for? To make sure that we have struct device for everything, whether we need it or not? Have you actually read through drivers/net? I mean, _really_ read through it, looking for ugly cases...

Quoted 6 lines
Quoted 2 lines

I've done just that several times and I'm sorry, but I would classify that project as hopeless. It's way, _way_ more diverse than SATA...

Actually, it's even worse - net_device itself simply *cannot* be dealt with that way. Its lifetime can indefinitely exceed that of underlying e.g. PCI device.

Could you point me to an example you have in mind?

I quickly searched through a handful of the PCI device drivers and couldn't find an example where the .remove function didn't do something to the tune of:

	unregister_netdev(dev);
	... various un-allocs and cleanup ...
	free_netdev(dev)
	...
	return;

Thank You,

Brandon

#6 lore raw

Re: [PATCH 1/2][RFC] Update net core to use devres.

Quoted 9 lines · Brandon Philips

Could you point me to an example you have in mind?

I quickly searched through a handful of the PCI device drivers and couldn't find an example where the .remove function didn't do something to the tune of:

	unregister_netdev(dev);
	... various un-allocs and cleanup ...
	free_netdev(dev)

Now find the definition of free_netdev() and read it through, please.

#7 lore raw

Re: [PATCH 1/2][RFC] Update net core to use devres.

Quoted 7 lines · Al Viro
Quoted 6 lines · Brandon Philips

Could you point me to an example you have in mind?

I quickly searched through a handful of the PCI device drivers and couldn't find an example where the .remove function didn't do something to the tune of:

Quoted 5 lines
Quoted 3 lines
	unregister_netdev(dev);
	... various un-allocs and cleanup ...
	free_netdev(dev)

Now find the definition of free_netdev() and read it through, please.

PS: free_netdev() is certainly a bad name for that; the reasons are mostly historical, since it used to free the damn thing. When that became impossible (read: previous time net_device has meat device model; consequences had not been pretty), the patch series from hell had been big enough without renaming that one on top of everything else.

#8 lore raw

RE: [PATCH 1/2][RFC] Update net core to use devres.

In reply to [email protected]
Quoted 6 lines

* netdev_pci_remove_one() can replace simple pci device remove functions

* devm_alloc_netdev() is like alloc_netdev but allocates memory using devres.
alloc_netdev() can be removed once all drivers use devres.

Please look at the multiqueue network code that is in 2.6.23. It creates alloc_netdev_mq() for multiqueue network device drivers. If you want to add this devres feature (which based on the threads, might be difficult), please make sure you comprehend the multiqueue features.

Thanks,

-PJ Waskiewicz