Skip to content

Commit a7d6e36

Browse files
stonezdmkuba-moo
authored andcommitted
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: Dan Carpenter <[email protected]> Signed-off-by: Duoming Zhou <[email protected]> Reviewed-by: Dan Carpenter <[email protected]> Link: https://lore.kernel.org/r/85bba3af651ca0e1a519da8d0d715b949891171c.1715247018.git.duoming@zju.edu.cn Signed-off-by: Jakub Kicinski <[email protected]>
1 parent ecf848e commit a7d6e36

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

include/net/ax25.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ typedef struct {
216216
struct ctl_table;
217217

218218
typedef struct ax25_dev {
219-
struct ax25_dev *next;
219+
struct list_head list;
220220

221221
struct net_device *dev;
222222
netdevice_tracker dev_tracker;
@@ -330,7 +330,6 @@ int ax25_addr_size(const ax25_digi *);
330330
void ax25_digi_invert(const ax25_digi *, ax25_digi *);
331331

332332
/* ax25_dev.c */
333-
extern ax25_dev *ax25_dev_list;
334333
extern spinlock_t ax25_dev_lock;
335334

336335
#if IS_ENABLED(CONFIG_AX25)

net/ax25/ax25_dev.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@
2222
#include <net/sock.h>
2323
#include <linux/uaccess.h>
2424
#include <linux/fcntl.h>
25+
#include <linux/list.h>
2526
#include <linux/mm.h>
2627
#include <linux/interrupt.h>
2728
#include <linux/init.h>
2829

29-
ax25_dev *ax25_dev_list;
30+
static LIST_HEAD(ax25_dev_list);
3031
DEFINE_SPINLOCK(ax25_dev_lock);
3132

3233
ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
3334
{
3435
ax25_dev *ax25_dev, *res = NULL;
3536

3637
spin_lock_bh(&ax25_dev_lock);
37-
for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
38+
list_for_each_entry(ax25_dev, &ax25_dev_list, list)
3839
if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
3940
res = ax25_dev;
4041
ax25_dev_hold(ax25_dev);
@@ -59,7 +60,6 @@ void ax25_dev_device_up(struct net_device *dev)
5960
}
6061

6162
refcount_set(&ax25_dev->refcount, 1);
62-
dev->ax25_ptr = ax25_dev;
6363
ax25_dev->dev = dev;
6464
netdev_hold(dev, &ax25_dev->dev_tracker, GFP_KERNEL);
6565
ax25_dev->forward = NULL;
@@ -85,8 +85,8 @@ void ax25_dev_device_up(struct net_device *dev)
8585
#endif
8686

8787
spin_lock_bh(&ax25_dev_lock);
88-
ax25_dev->next = ax25_dev_list;
89-
ax25_dev_list = ax25_dev;
88+
list_add(&ax25_dev->list, &ax25_dev_list);
89+
dev->ax25_ptr = ax25_dev;
9090
spin_unlock_bh(&ax25_dev_lock);
9191
ax25_dev_hold(ax25_dev);
9292

@@ -111,32 +111,25 @@ void ax25_dev_device_down(struct net_device *dev)
111111
/*
112112
* Remove any packet forwarding that points to this device.
113113
*/
114-
for (s = ax25_dev_list; s != NULL; s = s->next)
114+
list_for_each_entry(s, &ax25_dev_list, list)
115115
if (s->forward == dev)
116116
s->forward = NULL;
117117

118-
if ((s = ax25_dev_list) == ax25_dev) {
119-
ax25_dev_list = s->next;
120-
goto unlock_put;
121-
}
122-
123-
while (s != NULL && s->next != NULL) {
124-
if (s->next == ax25_dev) {
125-
s->next = ax25_dev->next;
118+
list_for_each_entry(s, &ax25_dev_list, list) {
119+
if (s == ax25_dev) {
120+
list_del(&s->list);
126121
goto unlock_put;
127122
}
128-
129-
s = s->next;
130123
}
131-
spin_unlock_bh(&ax25_dev_lock);
132124
dev->ax25_ptr = NULL;
125+
spin_unlock_bh(&ax25_dev_lock);
133126
ax25_dev_put(ax25_dev);
134127
return;
135128

136129
unlock_put:
130+
dev->ax25_ptr = NULL;
137131
spin_unlock_bh(&ax25_dev_lock);
138132
ax25_dev_put(ax25_dev);
139-
dev->ax25_ptr = NULL;
140133
netdev_put(dev, &ax25_dev->dev_tracker);
141134
ax25_dev_put(ax25_dev);
142135
}
@@ -200,16 +193,13 @@ struct net_device *ax25_fwd_dev(struct net_device *dev)
200193
*/
201194
void __exit ax25_dev_free(void)
202195
{
203-
ax25_dev *s, *ax25_dev;
196+
ax25_dev *s, *n;
204197

205198
spin_lock_bh(&ax25_dev_lock);
206-
ax25_dev = ax25_dev_list;
207-
while (ax25_dev != NULL) {
208-
s = ax25_dev;
209-
netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker);
210-
ax25_dev = ax25_dev->next;
199+
list_for_each_entry_safe(s, n, &ax25_dev_list, list) {
200+
netdev_put(s->dev, &s->dev_tracker);
201+
list_del(&s->list);
211202
kfree(s);
212203
}
213-
ax25_dev_list = NULL;
214204
spin_unlock_bh(&ax25_dev_lock);
215205
}

0 commit comments

Comments
 (0)