Skip to content

Commit 6a2968e

Browse files
edumazetdavem330
authored andcommitted
net: add netdev_set_operstate() helper
dev_base_lock is going away, add netdev_set_operstate() helper so that hsr does not have to know core internals. Remove dev_base_lock acquisition from rfc2863_policy() v3: use an "unsigned int" for dev->operstate, so that try_cmpxchg() can work on all arches. ( https://lore.kernel.org/oe-kbuild-all/[email protected]/ ) Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 328771d commit 6a2968e

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

include/linux/netdevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2258,7 +2258,7 @@ struct net_device {
22582258
const struct tlsdev_ops *tlsdev_ops;
22592259
#endif
22602260

2261-
unsigned char operstate;
2261+
unsigned int operstate;
22622262
unsigned char link_mode;
22632263

22642264
unsigned char if_port;

include/linux/rtnetlink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,6 @@ rtnl_notify_needed(const struct net *net, u16 nlflags, u32 group)
172172
return (nlflags & NLM_F_ECHO) || rtnl_has_listeners(net, group);
173173
}
174174

175+
void netdev_set_operstate(struct net_device *dev, int newstate);
176+
175177
#endif /* __LINUX_RTNETLINK_H */

net/core/link_watch.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
3333
static LIST_HEAD(lweventlist);
3434
static DEFINE_SPINLOCK(lweventlist_lock);
3535

36-
static unsigned char default_operstate(const struct net_device *dev)
36+
static unsigned int default_operstate(const struct net_device *dev)
3737
{
3838
if (netif_testing(dev))
3939
return IF_OPER_TESTING;
@@ -62,16 +62,13 @@ static unsigned char default_operstate(const struct net_device *dev)
6262
return IF_OPER_UP;
6363
}
6464

65-
6665
static void rfc2863_policy(struct net_device *dev)
6766
{
68-
unsigned char operstate = default_operstate(dev);
67+
unsigned int operstate = default_operstate(dev);
6968

7069
if (operstate == READ_ONCE(dev->operstate))
7170
return;
7271

73-
write_lock(&dev_base_lock);
74-
7572
switch(dev->link_mode) {
7673
case IF_LINK_MODE_TESTING:
7774
if (operstate == IF_OPER_UP)
@@ -88,8 +85,6 @@ static void rfc2863_policy(struct net_device *dev)
8885
}
8986

9087
WRITE_ONCE(dev->operstate, operstate);
91-
92-
write_unlock(&dev_base_lock);
9388
}
9489

9590

net/core/rtnetlink.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,22 @@ int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
842842
}
843843
EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
844844

845+
void netdev_set_operstate(struct net_device *dev, int newstate)
846+
{
847+
unsigned int old = READ_ONCE(dev->operstate);
848+
849+
do {
850+
if (old == newstate)
851+
return;
852+
} while (!try_cmpxchg(&dev->operstate, &old, newstate));
853+
854+
netdev_state_change(dev);
855+
}
856+
EXPORT_SYMBOL(netdev_set_operstate);
857+
845858
static void set_operstate(struct net_device *dev, unsigned char transition)
846859
{
847-
unsigned char operstate = dev->operstate;
860+
unsigned char operstate = READ_ONCE(dev->operstate);
848861

849862
switch (transition) {
850863
case IF_OPER_UP:
@@ -866,12 +879,7 @@ static void set_operstate(struct net_device *dev, unsigned char transition)
866879
break;
867880
}
868881

869-
if (READ_ONCE(dev->operstate) != operstate) {
870-
write_lock(&dev_base_lock);
871-
WRITE_ONCE(dev->operstate, operstate);
872-
write_unlock(&dev_base_lock);
873-
netdev_state_change(dev);
874-
}
882+
netdev_set_operstate(dev, operstate);
875883
}
876884

877885
static unsigned int rtnl_dev_get_flags(const struct net_device *dev)

net/hsr/hsr_device.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,19 @@ static bool is_slave_up(struct net_device *dev)
2828
return dev && is_admin_up(dev) && netif_oper_up(dev);
2929
}
3030

31-
static void __hsr_set_operstate(struct net_device *dev, int transition)
32-
{
33-
write_lock(&dev_base_lock);
34-
if (READ_ONCE(dev->operstate) != transition) {
35-
WRITE_ONCE(dev->operstate, transition);
36-
write_unlock(&dev_base_lock);
37-
netdev_state_change(dev);
38-
} else {
39-
write_unlock(&dev_base_lock);
40-
}
41-
}
42-
4331
static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
4432
{
45-
if (!is_admin_up(master->dev)) {
46-
__hsr_set_operstate(master->dev, IF_OPER_DOWN);
33+
struct net_device *dev = master->dev;
34+
35+
if (!is_admin_up(dev)) {
36+
netdev_set_operstate(dev, IF_OPER_DOWN);
4737
return;
4838
}
4939

5040
if (has_carrier)
51-
__hsr_set_operstate(master->dev, IF_OPER_UP);
41+
netdev_set_operstate(dev, IF_OPER_UP);
5242
else
53-
__hsr_set_operstate(master->dev, IF_OPER_LOWERLAYERDOWN);
43+
netdev_set_operstate(dev, IF_OPER_LOWERLAYERDOWN);
5444
}
5545

5646
static bool hsr_check_carrier(struct hsr_port *master)

0 commit comments

Comments
 (0)