Skip to content

Commit 6601850

Browse files
Eric Dumazetdavem330
authored andcommitted
ip: Router Alert RCU conversion
Straightforward conversion to RCU. One rwlock becomes a spinlock, and is static. Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8b37ef0 commit 6601850

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

‎include/net/ip.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ struct ip_ra_chain {
6262
struct ip_ra_chain *next;
6363
struct sock *sk;
6464
void (*destructor)(struct sock *);
65+
struct rcu_head rcu;
6566
};
6667

6768
extern struct ip_ra_chain *ip_ra_chain;
68-
extern rwlock_t ip_ra_lock;
6969

7070
/* IP flags. */
7171
#define IP_CE 0x8000 /* Flag: "Congestion" */

‎net/ipv4/ip_input.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
#include <linux/netlink.h>
147147

148148
/*
149-
* Process Router Attention IP option
149+
* Process Router Attention IP option (RFC 2113)
150150
*/
151151
int ip_call_ra_chain(struct sk_buff *skb)
152152
{
@@ -155,8 +155,7 @@ int ip_call_ra_chain(struct sk_buff *skb)
155155
struct sock *last = NULL;
156156
struct net_device *dev = skb->dev;
157157

158-
read_lock(&ip_ra_lock);
159-
for (ra = ip_ra_chain; ra; ra = ra->next) {
158+
for (ra = rcu_dereference(ip_ra_chain); ra; ra = rcu_dereference(ra->next)) {
160159
struct sock *sk = ra->sk;
161160

162161
/* If socket is bound to an interface, only report
@@ -167,10 +166,8 @@ int ip_call_ra_chain(struct sk_buff *skb)
167166
sk->sk_bound_dev_if == dev->ifindex) &&
168167
net_eq(sock_net(sk), dev_net(dev))) {
169168
if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
170-
if (ip_defrag(skb, IP_DEFRAG_CALL_RA_CHAIN)) {
171-
read_unlock(&ip_ra_lock);
169+
if (ip_defrag(skb, IP_DEFRAG_CALL_RA_CHAIN))
172170
return 1;
173-
}
174171
}
175172
if (last) {
176173
struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
@@ -183,10 +180,8 @@ int ip_call_ra_chain(struct sk_buff *skb)
183180

184181
if (last) {
185182
raw_rcv(last, skb);
186-
read_unlock(&ip_ra_lock);
187183
return 1;
188184
}
189-
read_unlock(&ip_ra_lock);
190185
return 0;
191186
}
192187

‎net/ipv4/ip_sockglue.c‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,12 @@ int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
239239
sent to multicast group to reach destination designated router.
240240
*/
241241
struct ip_ra_chain *ip_ra_chain;
242-
DEFINE_RWLOCK(ip_ra_lock);
242+
static DEFINE_SPINLOCK(ip_ra_lock);
243+
244+
static void ip_ra_free_rcu(struct rcu_head *head)
245+
{
246+
kfree(container_of(head, struct ip_ra_chain, rcu));
247+
}
243248

244249
int ip_ra_control(struct sock *sk, unsigned char on,
245250
void (*destructor)(struct sock *))
@@ -251,35 +256,35 @@ int ip_ra_control(struct sock *sk, unsigned char on,
251256

252257
new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
253258

254-
write_lock_bh(&ip_ra_lock);
259+
spin_lock_bh(&ip_ra_lock);
255260
for (rap = &ip_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
256261
if (ra->sk == sk) {
257262
if (on) {
258-
write_unlock_bh(&ip_ra_lock);
263+
spin_unlock_bh(&ip_ra_lock);
259264
kfree(new_ra);
260265
return -EADDRINUSE;
261266
}
262-
*rap = ra->next;
263-
write_unlock_bh(&ip_ra_lock);
267+
rcu_assign_pointer(*rap, ra->next);
268+
spin_unlock_bh(&ip_ra_lock);
264269

265270
if (ra->destructor)
266271
ra->destructor(sk);
267272
sock_put(sk);
268-
kfree(ra);
273+
call_rcu(&ra->rcu, ip_ra_free_rcu);
269274
return 0;
270275
}
271276
}
272277
if (new_ra == NULL) {
273-
write_unlock_bh(&ip_ra_lock);
278+
spin_unlock_bh(&ip_ra_lock);
274279
return -ENOBUFS;
275280
}
276281
new_ra->sk = sk;
277282
new_ra->destructor = destructor;
278283

279284
new_ra->next = ra;
280-
*rap = new_ra;
285+
rcu_assign_pointer(*rap, new_ra);
281286
sock_hold(sk);
282-
write_unlock_bh(&ip_ra_lock);
287+
spin_unlock_bh(&ip_ra_lock);
283288

284289
return 0;
285290
}

0 commit comments

Comments
 (0)