Skip to content

Commit 20fd4d1

Browse files
Pravin B Shelardavem330
authored andcommitted
gre: Simplify gre protocol registration locking.
Use cmpxchg() for atomic protocol registration which saves code and data space. Signed-off-by: Pravin B Shelar <pshelar@nicira.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ac8025a commit 20fd4d1

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

net/ipv4/gre.c

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,32 @@
2626

2727

2828
static const struct gre_protocol __rcu *gre_proto[GREPROTO_MAX] __read_mostly;
29-
static DEFINE_SPINLOCK(gre_proto_lock);
3029

3130
int gre_add_protocol(const struct gre_protocol *proto, u8 version)
3231
{
3332
if (version >= GREPROTO_MAX)
34-
goto err_out;
35-
36-
spin_lock(&gre_proto_lock);
37-
if (gre_proto[version])
38-
goto err_out_unlock;
39-
40-
RCU_INIT_POINTER(gre_proto[version], proto);
41-
spin_unlock(&gre_proto_lock);
42-
return 0;
33+
return -EINVAL;
4334

44-
err_out_unlock:
45-
spin_unlock(&gre_proto_lock);
46-
err_out:
47-
return -1;
35+
return (cmpxchg((const struct gre_protocol **)&gre_proto[version], NULL, proto) == NULL) ?
36+
0 : -EBUSY;
4837
}
4938
EXPORT_SYMBOL_GPL(gre_add_protocol);
5039

5140
int gre_del_protocol(const struct gre_protocol *proto, u8 version)
5241
{
42+
int ret;
43+
5344
if (version >= GREPROTO_MAX)
54-
goto err_out;
55-
56-
spin_lock(&gre_proto_lock);
57-
if (rcu_dereference_protected(gre_proto[version],
58-
lockdep_is_held(&gre_proto_lock)) != proto)
59-
goto err_out_unlock;
60-
RCU_INIT_POINTER(gre_proto[version], NULL);
61-
spin_unlock(&gre_proto_lock);
45+
return -EINVAL;
46+
47+
ret = (cmpxchg((const struct gre_protocol **)&gre_proto[version], proto, NULL) == proto) ?
48+
0 : -EBUSY;
49+
50+
if (ret)
51+
return ret;
52+
6253
synchronize_rcu();
6354
return 0;
64-
65-
err_out_unlock:
66-
spin_unlock(&gre_proto_lock);
67-
err_out:
68-
return -1;
6955
}
7056
EXPORT_SYMBOL_GPL(gre_del_protocol);
7157

0 commit comments

Comments
 (0)