Re: [PATCH net-next v7 00/11] net: Improve netns handling in rtnetlink

Xiao Liang
2025-01-07T12:53:58Z

On Tue, Jan 7, 2025 at 4:57 PM Kuniyuki Iwashima <kuniyu@amazon.com> wrote:
[...]
>
> We can fix this by linking the dev to the socket's netns and
> clean them up in __net_exit hook as done in bareudp and geneve.
>
> ---8<---
> diff --git a/drivers/net/gtp.c b/drivers/net/gtp.c
> index 89a996ad8cd0..77638a815873 100644
> --- a/drivers/net/gtp.c
> +++ b/drivers/net/gtp.c
> @@ -70,6 +70,7 @@ struct pdp_ctx {
> /* One instance of the GTP device. */
> struct gtp_dev {
> struct list_head list;
> + struct list_head sock_list;
>
> struct sock *sk0;
> struct sock *sk1u;
> @@ -102,6 +103,7 @@ static unsigned int gtp_net_id __read_mostly;
>
> struct gtp_net {
> struct list_head gtp_dev_list;
> + struct list_head gtp_sock_list;

After a closer look at the GTP driver, I'm confused about
the gtp_dev_list here. GTP device is linked to this list at
creation time, but netns can be changed afterwards.
The list is used in gtp_net_exit_batch_rtnl(), but to my
understanding net devices can already be deleted in
default_device_exit_batch() by default.
And I wonder if the use in gtp_genl_dump_pdp() can be
replaced by something like for_each_netdev_rcu().

> };
>
> static u32 gtp_h_initval;
> @@ -1526,6 +1528,10 @@ static int gtp_newlink(struct net *src_net, struct net_device *dev,
>
> gn = net_generic(dev_net(dev), gtp_net_id);
> list_add_rcu(&gtp->list, &gn->gtp_dev_list);
> +
> + gn = net_generic(src_net, gtp_net_id);
> + list_add(&gtp->sock_list, &gn->gtp_sock_list);
> +
> dev->priv_destructor = gtp_destructor;
>
> netdev_dbg(dev, "registered new GTP interface\n");
> @@ -1552,6 +1558,7 @@ static void gtp_dellink(struct net_device *dev, struct list_head *head)
> pdp_context_delete(pctx);
>
> list_del_rcu(&gtp->list);
> + list_del(&gtp->sock_list);
> unregister_netdevice_queue(dev, head);
> }
>
> @@ -2465,6 +2472,8 @@ static int __net_init gtp_net_init(struct net *net)
> struct gtp_net *gn = net_generic(net, gtp_net_id);
>
> INIT_LIST_HEAD(&gn->gtp_dev_list);
> + INIT_LIST_HEAD(&gn->gtp_sock_list);
> +
> return 0;
> }
>
> @@ -2475,9 +2484,12 @@ static void __net_exit gtp_net_exit_batch_rtnl(struct list_head *net_list,
>
> list_for_each_entry(net, net_list, exit_list) {
> struct gtp_net *gn = net_generic(net, gtp_net_id);
> - struct gtp_dev *gtp;
> + struct gtp_dev *gtp, *next;
> +
> + list_for_each_entry_safe(gtp, next, &gn->gtp_dev_list, list)
> + gtp_dellink(gtp->dev, dev_to_kill);
>
> - list_for_each_entry(gtp, &gn->gtp_dev_list, list)
> + list_for_each_entry_safe(gtp, next, &gn->gtp_sock_list, sock_list)
> gtp_dellink(gtp->dev, dev_to_kill);
> }
> }
> ---8<---