Commit a7d6e36b authored by Duoming Zhou's avatar Duoming Zhou Committed by Jakub Kicinski

ax25: Use kernel universal linked list to implement ax25_dev_list

The origin ax25_dev_list implements its own single linked list,
which is complicated and error-prone. For example, when deleting
the node of ax25_dev_list in ax25_dev_device_down(), we have to
operate on the head node and other nodes separately.

This patch uses kernel universal linked list to replace original
ax25_dev_list, which make the operation of ax25_dev_list easier.

We should do "dev->ax25_ptr = ax25_dev;" and "dev->ax25_ptr = NULL;"
while holding the spinlock, otherwise the ax25_dev_device_up() and
ax25_dev_device_down() could race.
Suggested-by: default avatarDan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: default avatarDuoming Zhou <duoming@zju.edu.cn>
Reviewed-by: default avatarDan Carpenter <dan.carpenter@linaro.org>
Link: https://lore.kernel.org/r/85bba3af651ca0e1a519da8d0d715b949891171c.1715247018.git.duoming@zju.edu.cnSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent ecf848eb
...@@ -216,7 +216,7 @@ typedef struct { ...@@ -216,7 +216,7 @@ typedef struct {
struct ctl_table; struct ctl_table;
typedef struct ax25_dev { typedef struct ax25_dev {
struct ax25_dev *next; struct list_head list;
struct net_device *dev; struct net_device *dev;
netdevice_tracker dev_tracker; netdevice_tracker dev_tracker;
...@@ -330,7 +330,6 @@ int ax25_addr_size(const ax25_digi *); ...@@ -330,7 +330,6 @@ int ax25_addr_size(const ax25_digi *);
void ax25_digi_invert(const ax25_digi *, ax25_digi *); void ax25_digi_invert(const ax25_digi *, ax25_digi *);
/* ax25_dev.c */ /* ax25_dev.c */
extern ax25_dev *ax25_dev_list;
extern spinlock_t ax25_dev_lock; extern spinlock_t ax25_dev_lock;
#if IS_ENABLED(CONFIG_AX25) #if IS_ENABLED(CONFIG_AX25)
......
...@@ -22,11 +22,12 @@ ...@@ -22,11 +22,12 @@
#include <net/sock.h> #include <net/sock.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/fcntl.h> #include <linux/fcntl.h>
#include <linux/list.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/interrupt.h> #include <linux/interrupt.h>
#include <linux/init.h> #include <linux/init.h>
ax25_dev *ax25_dev_list; static LIST_HEAD(ax25_dev_list);
DEFINE_SPINLOCK(ax25_dev_lock); DEFINE_SPINLOCK(ax25_dev_lock);
ax25_dev *ax25_addr_ax25dev(ax25_address *addr) ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
...@@ -34,7 +35,7 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr) ...@@ -34,7 +35,7 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
ax25_dev *ax25_dev, *res = NULL; ax25_dev *ax25_dev, *res = NULL;
spin_lock_bh(&ax25_dev_lock); 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) { if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
res = ax25_dev; res = ax25_dev;
ax25_dev_hold(ax25_dev); ax25_dev_hold(ax25_dev);
...@@ -59,7 +60,6 @@ void ax25_dev_device_up(struct net_device *dev) ...@@ -59,7 +60,6 @@ void ax25_dev_device_up(struct net_device *dev)
} }
refcount_set(&ax25_dev->refcount, 1); refcount_set(&ax25_dev->refcount, 1);
dev->ax25_ptr = ax25_dev;
ax25_dev->dev = dev; ax25_dev->dev = dev;
netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL); netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL);
ax25_dev->forward = NULL; ax25_dev->forward = NULL;
...@@ -85,8 +85,8 @@ void ax25_dev_device_up(struct net_device *dev) ...@@ -85,8 +85,8 @@ void ax25_dev_device_up(struct net_device *dev)
#endif #endif
spin_lock_bh(&ax25_dev_lock); spin_lock_bh(&ax25_dev_lock);
ax25_dev->next = ax25_dev_list; list_add(&ax25_dev->list, &ax25_dev_list);
ax25_dev_list = ax25_dev; dev->ax25_ptr = ax25_dev;
spin_unlock_bh(&ax25_dev_lock); spin_unlock_bh(&ax25_dev_lock);
ax25_dev_hold(ax25_dev); ax25_dev_hold(ax25_dev);
...@@ -111,32 +111,25 @@ void ax25_dev_device_down(struct net_device *dev) ...@@ -111,32 +111,25 @@ void ax25_dev_device_down(struct net_device *dev)
/* /*
* Remove any packet forwarding that points to this device. * 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) if (s->forward == dev)
s->forward = NULL; s->forward = NULL;
if ((s = ax25_dev_list) == ax25_dev) { list_for_each_entry(s, &ax25_dev_list, list) {
ax25_dev_list = s->next; if (s == ax25_dev) {
goto unlock_put; list_del(&s->list);
}
while (s != NULL && s->next != NULL) {
if (s->next == ax25_dev) {
s->next = ax25_dev->next;
goto unlock_put; goto unlock_put;
} }
s = s->next;
} }
spin_unlock_bh(&ax25_dev_lock);
dev->ax25_ptr = NULL; dev->ax25_ptr = NULL;
spin_unlock_bh(&ax25_dev_lock);
ax25_dev_put(ax25_dev); ax25_dev_put(ax25_dev);
return; return;
unlock_put: unlock_put:
dev->ax25_ptr = NULL;
spin_unlock_bh(&ax25_dev_lock); spin_unlock_bh(&ax25_dev_lock);
ax25_dev_put(ax25_dev); ax25_dev_put(ax25_dev);
dev->ax25_ptr = NULL;
netdev_put(dev, &ax25_dev->dev_tracker); netdev_put(dev, &ax25_dev->dev_tracker);
ax25_dev_put(ax25_dev); ax25_dev_put(ax25_dev);
} }
...@@ -200,16 +193,13 @@ struct net_device *ax25_fwd_dev(struct net_device *dev) ...@@ -200,16 +193,13 @@ struct net_device *ax25_fwd_dev(struct net_device *dev)
*/ */
void __exit ax25_dev_free(void) void __exit ax25_dev_free(void)
{ {
ax25_dev *s, *ax25_dev; ax25_dev *s, *n;
spin_lock_bh(&ax25_dev_lock); spin_lock_bh(&ax25_dev_lock);
ax25_dev = ax25_dev_list; list_for_each_entry_safe(s, n, &ax25_dev_list, list) {
while (ax25_dev != NULL) { netdev_put(s->dev, &s->dev_tracker);
s = ax25_dev; list_del(&s->list);
netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker);
ax25_dev = ax25_dev->next;
kfree(s); kfree(s);
} }
ax25_dev_list = NULL;
spin_unlock_bh(&ax25_dev_lock); spin_unlock_bh(&ax25_dev_lock);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment