Re: [PATCH] net: convert netdev_nl_sock bindings list to xarray

Nikolay Aleksandrov <[email protected]>
Newsgroups org.kernel.vger.netdev,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On 10/08/2026 09:57, Shivaji Kant wrote:
> netdev_nl_sock previously used a struct list_head bindings to keep
> track of active netdev netlink bindings, protected by a per-socket
> struct mutex lock (priv->lock).
> 
> Since list modifications and iterations are not concurrency-safe,
> priv->lock was introduced to serialize operations on priv->bindings.
> However, xarray manages its own internal locking (xa_lock) for store,
> erase, and iteration operations.
> 
> Convert bindings in struct netdev_nl_sock from struct list_head to
> struct xarray and remove priv->lock. This simplifies the code by
> removing explicit mutex locking in netdev_nl_bind_rx_doit(),
> netdev_nl_bind_tx_doit(), and socket initialization/teardown functions.
> 
> Reviewed-by: Mina Almasry <[email protected]>
> Signed-off-by: Shivaji Kant <[email protected]>
> ---
>   include/net/netdev_netlink.h |  5 ++---
>   net/core/devmem.c            | 13 ++++++++++---
>   net/core/devmem.h            |  4 ++--
>   net/core/netdev-genl.c       | 25 ++++++-------------------
>   4 files changed, 20 insertions(+), 27 deletions(-)
> 

This patch should target net-next.

> diff --git a/include/net/netdev_netlink.h b/include/net/netdev_netlink.h
> index 075962dbe743..45a4a482d42e 100644
> --- a/include/net/netdev_netlink.h
> +++ b/include/net/netdev_netlink.h
> @@ -2,11 +2,10 @@
>   #ifndef __NET_NETDEV_NETLINK_H
>   #define __NET_NETDEV_NETLINK_H
>   
> -#include <linux/list.h>
> +#include <linux/xarray.h>
>   
>   struct netdev_nl_sock {
> -	struct mutex lock;
> -	struct list_head bindings;
> +	struct xarray bindings;
>   };
>   
>   #endif	/* __NET_NETDEV_NETLINK_H */
> diff --git a/net/core/devmem.c b/net/core/devmem.c
> index 957d6b96216b..52fda18b80ef 100644
> --- a/net/core/devmem.c
> +++ b/net/core/devmem.c
> @@ -134,8 +134,8 @@ void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
>   	 */
>   	synchronize_net();
>   
> -	if (binding->list.next)
> -		list_del(&binding->list);
> +	if (binding->sock_priv)
> +		xa_erase(&binding->sock_priv->bindings, binding->id);
>   
>   	xa_for_each(&binding->bound_rxqs, xa_idx, rxq) {
>   		const struct pp_memory_provider_params mp_params = {
> @@ -193,6 +193,7 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
>   	struct dma_buf *dmabuf;
>   	unsigned int sg_idx, i;
>   	unsigned long virtual;
> +	void *res;
>   	int err;
>   
>   	if (!dma_dev) {
> @@ -325,7 +326,13 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
>   	if (err < 0)
>   		goto err_free_chunks;
>   
> -	list_add(&binding->list, &priv->bindings);
> +	binding->sock_priv = priv;
> +	res = xa_store(&priv->bindings, binding->id, binding, GFP_KERNEL);
> +	if (xa_is_err(res)) {
> +		err = xa_err(res);
> +		xa_erase(&net_devmem_dmabuf_bindings, binding->id);
> +		goto err_free_chunks;

This error cleans up after the previous xa_alloc_cyclic call itself instead
of following the style of the rest of the error cleanups. I'd move the xa_erase()
into its own err_ label and goto there.

> +	}
>   
>   	return binding;
>   
> diff --git a/net/core/devmem.h b/net/core/devmem.h
> index 3852a56036cb..bf77e5a9a8fe 100644
> --- a/net/core/devmem.h
> +++ b/net/core/devmem.h
> @@ -49,10 +49,10 @@ struct net_devmem_dmabuf_binding {
>   	 */
>   	struct percpu_ref ref;
>   
> -	/* The list of bindings currently active. Used for netlink to notify us
> +	/* The socket priv this binding belongs to. Used for netlink to notify us
>   	 * of the user dropping the bind.
>   	 */
> -	struct list_head list;
> +	struct netdev_nl_sock *sock_priv;
>   
>   	/* rxq's this binding is active on. */
>   	struct xarray bound_rxqs;
> diff --git a/net/core/netdev-genl.c b/net/core/netdev-genl.c
> index c15d8d4ca1f8..d3f3a91a6d93 100644
> --- a/net/core/netdev-genl.c
> +++ b/net/core/netdev-genl.c
> @@ -1044,13 +1044,11 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
>   		goto err_genlmsg_free;
>   	}
>   
> -	mutex_lock(&priv->lock);
> -
>   	err = 0;
>   	netdev = netdev_get_by_index_lock(genl_info_net(info), ifindex);
>   	if (!netdev) {
>   		err = -ENODEV;
> -		goto err_unlock_sock;
> +		goto err_genlmsg_free;
>   	}
>   	if (!netif_device_present(netdev))
>   		err = -ENODEV;
> @@ -1102,8 +1100,6 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
>   
>   	netdev_unlock(netdev);
>   
> -	mutex_unlock(&priv->lock);
> -
>   	return err < 0 ? err : 0;
>   
>   err_unbind:
> @@ -1112,8 +1108,6 @@ int netdev_nl_bind_rx_doit(struct sk_buff *skb, struct genl_info *info)
>   	bitmap_free(rxq_bitmap);
>   err_unlock:
>   	netdev_unlock(netdev);
> -err_unlock_sock:
> -	mutex_unlock(&priv->lock);
>   err_genlmsg_free:
>   	nlmsg_free(rsp);
>   	return err;
> @@ -1185,12 +1179,10 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
>   		goto err_genlmsg_free;
>   	}
>   
> -	mutex_lock(&priv->lock);
> -
>   	netdev = netdev_get_by_index_lock(genl_info_net(info), ifindex);
>   	if (!netdev) {
>   		err = -ENODEV;
> -		goto err_unlock_sock;
> +		goto err_genlmsg_free;
>   	}
>   
>   	if (!netif_device_present(netdev)) {
> @@ -1233,7 +1225,6 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
>   	if (bind_dev != netdev)
>   		netdev_unlock(bind_dev);
>   	netdev_unlock(netdev);
> -	mutex_unlock(&priv->lock);
>   
>   	return genlmsg_reply(rsp, info);
>   
> @@ -1242,8 +1233,6 @@ int netdev_nl_bind_tx_doit(struct sk_buff *skb, struct genl_info *info)
>   		netdev_unlock(bind_dev);
>   err_unlock_netdev:
>   	netdev_unlock(netdev);
> -err_unlock_sock:
> -	mutex_unlock(&priv->lock);
>   err_genlmsg_free:
>   	nlmsg_free(rsp);
>   	return err;
> @@ -1418,19 +1407,17 @@ int netdev_nl_queue_create_doit(struct sk_buff *skb, struct genl_info *info)
>   
>   void netdev_nl_sock_priv_init(struct netdev_nl_sock *priv)
>   {
> -	INIT_LIST_HEAD(&priv->bindings);
> -	mutex_init(&priv->lock);
> +	xa_init(&priv->bindings);
>   }
>   
>   void netdev_nl_sock_priv_destroy(struct netdev_nl_sock *priv)
>   {
>   	struct net_devmem_dmabuf_binding *binding;
> -	struct net_devmem_dmabuf_binding *temp;
>   	netdevice_tracker dev_tracker;
>   	struct net_device *dev;
> +	unsigned long xa_idx;
>   
> -	mutex_lock(&priv->lock);
> -	list_for_each_entry_safe(binding, temp, &priv->bindings, list) {
> +	xa_for_each(&priv->bindings, xa_idx, binding) {
>   		mutex_lock(&binding->lock);
>   		dev = binding->dev;
>   		if (!dev) {
> @@ -1446,7 +1433,7 @@ void netdev_nl_sock_priv_destroy(struct netdev_nl_sock *priv)
>   		netdev_unlock(dev);
>   		netdev_put(dev, &dev_tracker);
>   	}
> -	mutex_unlock(&priv->lock);
> +	xa_destroy(&priv->bindings);
>   }
>   
>   static int netdev_genl_netdevice_event(struct notifier_block *nb,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.