Re: [PATCH net] ax25: Fix refcount leak issues of ax25_dev
From: Dan Carpenter <hidden>
Date: 2024-05-02 07:56:49
Also in:
linux-hams, lkml
Subsystem:
networking [general], the rest · Maintainers:
"David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds
On Thu, May 02, 2024 at 12:35:44PM +0800, duoming@zju.edu.cn wrote:
On Wed, 1 May 2024 20:43:37 +0300 Dan Carpenter wrote:quoted
I'm always happy to take credit for stuff but the Reported by should go to Lars and Miroslav. Reported-by: Lars Kellogg-Stedman <redacted> Reported-by: Miroslav Skoric <redacted>This patch is not related with the problem raised by Lars Kellogg-Stedman and Miroslav Skoric, it only solves the reference counting leak issues of ax25_dev in ax25_addr_ax25dev() and ax25_dev_device_down(). So I think there is no need to change the "Reported by" label.
Ah... I was really hoping it was related to the other bugs.
Okay, what about we separate this into different patches one for each
bug? The changes to ax25_addr_ax25dev() and ax25_dev_free() are
obvious and could go in as-is but as two separate patches.
The changes to ax25_dev_device_up/down() are more subtle.
The ax25_dev_list stuff is frustrating. It would be so much easier if
it were a normal list and you could just do:
/*
* Remove any packet forwarding that points to this device.
*/
list_for_each_entry(s, ax25_dev_list, list) {
if (s->forward == dev)
s->forward = NULL;
}
list_for_each_entry(s, ax25_dev_list, list) {
if (s == ax25_dev) {
list_del(s);
free_net = true;
break;
}
}
spin_unlock_bh(&ax25_dev_lock);
dev->ax25_ptr = NULL;
if (free_net)
netdev_put(dev, &ax25_dev->dev_tracker);
ax25_dev_put(ax25_dev);
}
Why do we call netdev_put() on that one path? Btw, here is an untested
conversion to lists...
regards,
dan carpenter
diff --git a/include/net/ax25.h b/include/net/ax25.h
index 0d939e5aee4e..c2a85fd3f5ea 100644
--- a/include/net/ax25.h
+++ b/include/net/ax25.h@@ -216,7 +216,7 @@ typedef struct { struct ctl_table; typedef struct ax25_dev { - struct ax25_dev *next; + struct list_head list; struct net_device *dev; netdevice_tracker dev_tracker;
@@ -330,7 +330,6 @@ int ax25_addr_size(const ax25_digi *); void ax25_digi_invert(const ax25_digi *, ax25_digi *); /* ax25_dev.c */ -extern ax25_dev *ax25_dev_list; extern spinlock_t ax25_dev_lock; #if IS_ENABLED(CONFIG_AX25)
diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c
index 282ec581c072..b632af38f1e1 100644
--- a/net/ax25/ax25_dev.c
+++ b/net/ax25/ax25_dev.c@@ -22,11 +22,12 @@ #include <net/sock.h> #include <linux/uaccess.h> #include <linux/fcntl.h> +#include <linux/list.h> #include <linux/mm.h> #include <linux/interrupt.h> #include <linux/init.h> -ax25_dev *ax25_dev_list; +static struct list_head ax25_dev_list; DEFINE_SPINLOCK(ax25_dev_lock); ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
@@ -34,11 +35,12 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr) ax25_dev *ax25_dev, *res = NULL; spin_lock_bh(&ax25_dev_lock); - for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) + list_for_each_entry(ax25_dev, &ax25_dev_list, list) { if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) { res = ax25_dev; ax25_dev_hold(ax25_dev); } + } spin_unlock_bh(&ax25_dev_lock); return res;
@@ -52,6 +54,10 @@ void ax25_dev_device_up(struct net_device *dev) { ax25_dev *ax25_dev; + // FIXME: do call this in probe or something + if (!ax25_dev_list.next) + INIT_LIST_HEAD(&ax25_dev_list); + ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_KERNEL); if (!ax25_dev) { printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n");
@@ -85,8 +91,7 @@ void ax25_dev_device_up(struct net_device *dev) #endif spin_lock_bh(&ax25_dev_lock); - ax25_dev->next = ax25_dev_list; - ax25_dev_list = ax25_dev; + list_add(&ax25_dev->list, &ax25_dev_list); spin_unlock_bh(&ax25_dev_lock); ax25_dev_hold(ax25_dev);
@@ -111,23 +116,18 @@ void ax25_dev_device_down(struct net_device *dev) /* * Remove any packet forwarding that points to this device. */ - for (s = ax25_dev_list; s != NULL; s = s->next) + list_for_each_entry(s, &ax25_dev_list, list) { if (s->forward == dev) s->forward = NULL; - - if ((s = ax25_dev_list) == ax25_dev) { - ax25_dev_list = s->next; - goto unlock_put; } - while (s != NULL && s->next != NULL) { - if (s->next == ax25_dev) { - s->next = ax25_dev->next; + list_for_each_entry(s, &ax25_dev_list, list) { + if (s == ax25_dev) { + list_del(&s->list); goto unlock_put; } - - s = s->next; } + spin_unlock_bh(&ax25_dev_lock); dev->ax25_ptr = NULL; ax25_dev_put(ax25_dev);
@@ -200,16 +200,13 @@ struct net_device *ax25_fwd_dev(struct net_device *dev) */ void __exit ax25_dev_free(void) { - ax25_dev *s, *ax25_dev; + ax25_dev *s, *n; spin_lock_bh(&ax25_dev_lock); - ax25_dev = ax25_dev_list; - while (ax25_dev != NULL) { - s = ax25_dev; - netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker); - ax25_dev = ax25_dev->next; + list_for_each_entry_safe(s, n, &ax25_dev_list, list) { + netdev_put(s->dev, &s->dev_tracker); + list_del(&s->list); kfree(s); } - ax25_dev_list = NULL; spin_unlock_bh(&ax25_dev_lock); }