diff mbox series

[wireless-next] wifi: ath12k: allocate dummy net_device dynamically

Message ID 20240503100440.6066-1-leitao@debian.org (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [wireless-next] wifi: ath12k: allocate dummy net_device dynamically | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Breno Leitao May 3, 2024, 10:04 a.m. UTC
Embedding net_device into structures prohibits the usage of flexible
arrays in the net_device structure. For more details, see the discussion
at [1].

Un-embed the net_device from struct ath12k_ext_irq_grp by converting it
into a pointer. Then use the leverage alloc_netdev_dummy() to allocate
the net_device object at ath12k_pci_ext_irq_config().

The free of the device occurs at ath12k_pci_free_ext_irq().

[1] https://lore.kernel.org/all/20240229225910.79e224cf@kernel.org/

This is *very* similar to the same changes in ath11k commit
bca592ead82528b ("wifi: ath11k: allocate dummy net_device dynamically")

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 drivers/net/wireless/ath/ath12k/core.h |  2 +-
 drivers/net/wireless/ath/ath12k/pci.c  | 14 +++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

Comments

Simon Horman May 4, 2024, 2:55 p.m. UTC | #1
On Fri, May 03, 2024 at 03:04:39AM -0700, Breno Leitao wrote:
> Embedding net_device into structures prohibits the usage of flexible
> arrays in the net_device structure. For more details, see the discussion
> at [1].
> 
> Un-embed the net_device from struct ath12k_ext_irq_grp by converting it
> into a pointer. Then use the leverage alloc_netdev_dummy() to allocate
> the net_device object at ath12k_pci_ext_irq_config().
> 
> The free of the device occurs at ath12k_pci_free_ext_irq().
> 
> [1] https://lore.kernel.org/all/20240229225910.79e224cf@kernel.org/
> 
> This is *very* similar to the same changes in ath11k commit
> bca592ead82528b ("wifi: ath11k: allocate dummy net_device dynamically")
> 
> Signed-off-by: Breno Leitao <leitao@debian.org>

...

> diff --git a/drivers/net/wireless/ath/ath12k/pci.c b/drivers/net/wireless/ath/ath12k/pci.c

...

> @@ -577,8 +578,11 @@ static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
>  
>  		irq_grp->ab = ab;
>  		irq_grp->grp_id = i;
> -		init_dummy_netdev(&irq_grp->napi_ndev);
> -		netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
> +		irq_grp->napi_ndev = alloc_netdev_dummy(0);
> +		if (!irq_grp->napi_ndev)
> +			return -ENOMEM;

Hi Breno,

Will returning on error here leak resources allocated by
alloc_netdev_dummy() in previous iterations of this loop?

If so, I suggest jumping to unwind handling which
can be shared with the error path in the hunk below.

> +
> +		netif_napi_add(irq_grp->napi_ndev, &irq_grp->napi,
>  			       ath12k_pci_ext_grp_napi_poll);
>  
>  		if (ab->hw_params->ring_mask->tx[i] ||
> @@ -611,6 +615,10 @@ static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
>  			if (ret) {
>  				ath12k_err(ab, "failed request irq %d: %d\n",
>  					   vector, ret);
> +				for (n = 0; n <= i; n++) {
> +					irq_grp = &ab->ext_irq_grp[n];
> +					free_netdev(irq_grp->napi_ndev);
> +				}
>  				return ret;
>  			}
>  		}
Breno Leitao May 7, 2024, 11:47 a.m. UTC | #2
On Sat, May 04, 2024 at 03:55:23PM +0100, Simon Horman wrote:
> On Fri, May 03, 2024 at 03:04:39AM -0700, Breno Leitao wrote:
> > Embedding net_device into structures prohibits the usage of flexible
> > arrays in the net_device structure. For more details, see the discussion
> > at [1].
> > 
> > Un-embed the net_device from struct ath12k_ext_irq_grp by converting it
> > into a pointer. Then use the leverage alloc_netdev_dummy() to allocate
> > the net_device object at ath12k_pci_ext_irq_config().
> > 
> > The free of the device occurs at ath12k_pci_free_ext_irq().
> > 
> > [1] https://lore.kernel.org/all/20240229225910.79e224cf@kernel.org/
> > 
> > This is *very* similar to the same changes in ath11k commit
> > bca592ead82528b ("wifi: ath11k: allocate dummy net_device dynamically")
> > 
> > Signed-off-by: Breno Leitao <leitao@debian.org>
> 
> ...
> 
> > diff --git a/drivers/net/wireless/ath/ath12k/pci.c b/drivers/net/wireless/ath/ath12k/pci.c
> 
> ...
> 
> > @@ -577,8 +578,11 @@ static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
> >  
> >  		irq_grp->ab = ab;
> >  		irq_grp->grp_id = i;
> > -		init_dummy_netdev(&irq_grp->napi_ndev);
> > -		netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
> > +		irq_grp->napi_ndev = alloc_netdev_dummy(0);
> > +		if (!irq_grp->napi_ndev)
> > +			return -ENOMEM;
> 
> Hi Breno,
> 
> Will returning on error here leak resources allocated by
> alloc_netdev_dummy() in previous iterations of this loop?

very good catch. Thanks!

> If so, I suggest jumping to unwind handling which
> can be shared with the error path in the hunk below.

Agree. let me spend some time and propose a V2.

Thanks
diff mbox series

Patch

diff --git a/drivers/net/wireless/ath/ath12k/core.h b/drivers/net/wireless/ath/ath12k/core.h
index 97e5a0ccd233..71c67f47d0ad 100644
--- a/drivers/net/wireless/ath/ath12k/core.h
+++ b/drivers/net/wireless/ath/ath12k/core.h
@@ -144,7 +144,7 @@  struct ath12k_ext_irq_grp {
 	u32 grp_id;
 	u64 timestamp;
 	struct napi_struct napi;
-	struct net_device napi_ndev;
+	struct net_device *napi_ndev;
 };
 
 struct ath12k_smbios_bdf {
diff --git a/drivers/net/wireless/ath/ath12k/pci.c b/drivers/net/wireless/ath/ath12k/pci.c
index 14954bc05144..322f0ad1b12d 100644
--- a/drivers/net/wireless/ath/ath12k/pci.c
+++ b/drivers/net/wireless/ath/ath12k/pci.c
@@ -350,6 +350,7 @@  static void ath12k_pci_free_ext_irq(struct ath12k_base *ab)
 			free_irq(ab->irq_num[irq_grp->irqs[j]], irq_grp);
 
 		netif_napi_del(&irq_grp->napi);
+		free_netdev(irq_grp->napi_ndev);
 	}
 }
 
@@ -560,7 +561,7 @@  static irqreturn_t ath12k_pci_ext_interrupt_handler(int irq, void *arg)
 static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
 {
 	struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
-	int i, j, ret, num_vectors = 0;
+	int i, j, n, ret, num_vectors = 0;
 	u32 user_base_data = 0, base_vector = 0, base_idx;
 
 	base_idx = ATH12K_PCI_IRQ_CE0_OFFSET + CE_COUNT_MAX;
@@ -577,8 +578,11 @@  static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
 
 		irq_grp->ab = ab;
 		irq_grp->grp_id = i;
-		init_dummy_netdev(&irq_grp->napi_ndev);
-		netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
+		irq_grp->napi_ndev = alloc_netdev_dummy(0);
+		if (!irq_grp->napi_ndev)
+			return -ENOMEM;
+
+		netif_napi_add(irq_grp->napi_ndev, &irq_grp->napi,
 			       ath12k_pci_ext_grp_napi_poll);
 
 		if (ab->hw_params->ring_mask->tx[i] ||
@@ -611,6 +615,10 @@  static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
 			if (ret) {
 				ath12k_err(ab, "failed request irq %d: %d\n",
 					   vector, ret);
+				for (n = 0; n <= i; n++) {
+					irq_grp = &ab->ext_irq_grp[n];
+					free_netdev(irq_grp->napi_ndev);
+				}
 				return ret;
 			}
 		}