diff mbox series

[net-next,v2,1/2] net: mana: introduce helper to get a master netdev

Message ID 1719838736-20338-2-git-send-email-kotaranov@linux.microsoft.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series Provide master netdev to mana_ib | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for net-next
netdev/apply fail Patch does not apply to net-next-1

Commit Message

Konstantin Taranov July 1, 2024, 12:58 p.m. UTC
From: Konstantin Taranov <kotaranov@microsoft.com>

Add helper to get a master netdevice for a given port.
When mana is used with netvsc, the VF netdev is controlled
by an upper netvsc device. In a baremetal case, the VF
netdev is the master device.

Signed-off-by: Konstantin Taranov <kotaranov@microsoft.com>
---
 drivers/net/ethernet/microsoft/mana/mana_en.c | 18 ++++++++++++++++++
 include/net/mana/mana.h                       |  2 ++
 2 files changed, 20 insertions(+)

Comments

Zhu Yanjun July 1, 2024, 8:14 p.m. UTC | #1
在 2024/7/1 20:58, Konstantin Taranov 写道:
> From: Konstantin Taranov <kotaranov@microsoft.com>
> 
> Add helper to get a master netdevice for a given port.
> When mana is used with netvsc, the VF netdev is controlled
> by an upper netvsc device. In a baremetal case, the VF
> netdev is the master device.
> 
> Signed-off-by: Konstantin Taranov <kotaranov@microsoft.com>
> ---
>   drivers/net/ethernet/microsoft/mana/mana_en.c | 18 ++++++++++++++++++
>   include/net/mana/mana.h                       |  2 ++
>   2 files changed, 20 insertions(+)
> 
> diff --git a/drivers/net/ethernet/microsoft/mana/mana_en.c b/drivers/net/ethernet/microsoft/mana/mana_en.c
> index d087cf9..b893339 100644
> --- a/drivers/net/ethernet/microsoft/mana/mana_en.c
> +++ b/drivers/net/ethernet/microsoft/mana/mana_en.c
> @@ -2948,3 +2948,21 @@ out:
>   	gd->gdma_context = NULL;
>   	kfree(ac);
>   }
> +
> +/* the caller should hold rcu_read_lock */
> +struct net_device *mana_get_master_netdev_rcu(struct mana_context *ac, u32 port_index)
> +{
> +	struct net_device *ndev;

 From the comments, this function requires rcu_read_lock. Can the 
following be added in this function?

RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu read lock held");

If rcu_read_lock is not held, this function will notify the caller.
This can explicitly ensure that rcu_read_lock should be held before this 
fuction is called.

Best Regards,

Zhu Yanjun

> +
> +	if (port_index >= ac->num_ports)
> +		return NULL;
> +
> +	/* When mana is used in netvsc, the upper netdevice should be returned. */
> +	if (ac->ports[port_index]->flags & IFF_SLAVE)
> +		ndev = netdev_master_upper_dev_get_rcu(ac->ports[port_index]);
> +	else
> +		ndev = ac->ports[port_index];
> +
> +	return ndev;
> +}
> +EXPORT_SYMBOL_NS(mana_get_master_netdev_rcu, NET_MANA);
> diff --git a/include/net/mana/mana.h b/include/net/mana/mana.h
> index 46f741e..2d3625c 100644
> --- a/include/net/mana/mana.h
> +++ b/include/net/mana/mana.h
> @@ -797,4 +797,6 @@ void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
>   int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
>   		   u32 doorbell_pg_id);
>   void mana_uncfg_vport(struct mana_port_context *apc);
> +
> +struct net_device *mana_get_master_netdev_rcu(struct mana_context *ac, u32 port_index);
>   #endif /* _MANA_H */
Konstantin Taranov July 3, 2024, 9:20 a.m. UTC | #2
> > +/* the caller should hold rcu_read_lock */ struct net_device
> > +*mana_get_master_netdev_rcu(struct mana_context *ac, u32 port_index)
> > +{
> > +	struct net_device *ndev;
> 
>  From the comments, this function requires rcu_read_lock. Can the following
> be added in this function?
> 
> RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu read lock held");
> 
> If rcu_read_lock is not held, this function will notify the caller.
> This can explicitly ensure that rcu_read_lock should be held before this
> fuction is called.

Thanks! Great idea!

> 
> Best Regards,
> 
> Zhu Yanjun
>
diff mbox series

Patch

diff --git a/drivers/net/ethernet/microsoft/mana/mana_en.c b/drivers/net/ethernet/microsoft/mana/mana_en.c
index d087cf9..b893339 100644
--- a/drivers/net/ethernet/microsoft/mana/mana_en.c
+++ b/drivers/net/ethernet/microsoft/mana/mana_en.c
@@ -2948,3 +2948,21 @@  out:
 	gd->gdma_context = NULL;
 	kfree(ac);
 }
+
+/* the caller should hold rcu_read_lock */
+struct net_device *mana_get_master_netdev_rcu(struct mana_context *ac, u32 port_index)
+{
+	struct net_device *ndev;
+
+	if (port_index >= ac->num_ports)
+		return NULL;
+
+	/* When mana is used in netvsc, the upper netdevice should be returned. */
+	if (ac->ports[port_index]->flags & IFF_SLAVE)
+		ndev = netdev_master_upper_dev_get_rcu(ac->ports[port_index]);
+	else
+		ndev = ac->ports[port_index];
+
+	return ndev;
+}
+EXPORT_SYMBOL_NS(mana_get_master_netdev_rcu, NET_MANA);
diff --git a/include/net/mana/mana.h b/include/net/mana/mana.h
index 46f741e..2d3625c 100644
--- a/include/net/mana/mana.h
+++ b/include/net/mana/mana.h
@@ -797,4 +797,6 @@  void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
 		   u32 doorbell_pg_id);
 void mana_uncfg_vport(struct mana_port_context *apc);
+
+struct net_device *mana_get_master_netdev_rcu(struct mana_context *ac, u32 port_index);
 #endif /* _MANA_H */