Skip to content

Commit 9c607d4

Browse files
Sebastian Andrzej SiewiorPaolo Abeni
authored andcommitted
xfrm: Use nested-BH locking for nat_keepalive_sk_ipv[46]
nat_keepalive_sk_ipv[46] is a per-CPU variable and relies on disabled BH for its locking. Without per-CPU locking in local_bh_disable() on PREEMPT_RT this data structure requires explicit locking. Use sock_bh_locked which has a sock pointer and a local_lock_t. Use local_lock_nested_bh() for locking. This change adds only lockdep coverage and does not alter the functional behaviour for !PREEMPT_RT. Cc: Steffen Klassert <[email protected]> Cc: Herbert Xu <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent b9eef33 commit 9c607d4

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

net/xfrm/xfrm_nat_keepalive.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#include <net/ip6_checksum.h>
1010
#include <net/xfrm.h>
1111

12-
static DEFINE_PER_CPU(struct sock *, nat_keepalive_sk_ipv4);
12+
static DEFINE_PER_CPU(struct sock_bh_locked, nat_keepalive_sk_ipv4) = {
13+
.bh_lock = INIT_LOCAL_LOCK(bh_lock),
14+
};
1315
#if IS_ENABLED(CONFIG_IPV6)
14-
static DEFINE_PER_CPU(struct sock *, nat_keepalive_sk_ipv6);
16+
static DEFINE_PER_CPU(struct sock_bh_locked, nat_keepalive_sk_ipv6) = {
17+
.bh_lock = INIT_LOCAL_LOCK(bh_lock),
18+
};
1519
#endif
1620

1721
struct nat_keepalive {
@@ -56,10 +60,12 @@ static int nat_keepalive_send_ipv4(struct sk_buff *skb,
5660

5761
skb_dst_set(skb, &rt->dst);
5862

59-
sk = *this_cpu_ptr(&nat_keepalive_sk_ipv4);
63+
local_lock_nested_bh(&nat_keepalive_sk_ipv4.bh_lock);
64+
sk = this_cpu_read(nat_keepalive_sk_ipv4.sock);
6065
sock_net_set(sk, net);
6166
err = ip_build_and_send_pkt(skb, sk, fl4.saddr, fl4.daddr, NULL, tos);
6267
sock_net_set(sk, &init_net);
68+
local_unlock_nested_bh(&nat_keepalive_sk_ipv4.bh_lock);
6369
return err;
6470
}
6571

@@ -89,15 +95,19 @@ static int nat_keepalive_send_ipv6(struct sk_buff *skb,
8995
fl6.fl6_sport = ka->encap_sport;
9096
fl6.fl6_dport = ka->encap_dport;
9197

92-
sk = *this_cpu_ptr(&nat_keepalive_sk_ipv6);
98+
local_lock_nested_bh(&nat_keepalive_sk_ipv6.bh_lock);
99+
sk = this_cpu_read(nat_keepalive_sk_ipv6.sock);
93100
sock_net_set(sk, net);
94101
dst = ipv6_stub->ipv6_dst_lookup_flow(net, sk, &fl6, NULL);
95-
if (IS_ERR(dst))
102+
if (IS_ERR(dst)) {
103+
local_unlock_nested_bh(&nat_keepalive_sk_ipv6.bh_lock);
96104
return PTR_ERR(dst);
105+
}
97106

98107
skb_dst_set(skb, dst);
99108
err = ipv6_stub->ip6_xmit(sk, skb, &fl6, skb->mark, NULL, 0, 0);
100109
sock_net_set(sk, &init_net);
110+
local_unlock_nested_bh(&nat_keepalive_sk_ipv6.bh_lock);
101111
return err;
102112
}
103113
#endif
@@ -202,7 +212,7 @@ static void nat_keepalive_work(struct work_struct *work)
202212
(ctx.next_run - ctx.now) * HZ);
203213
}
204214

205-
static int nat_keepalive_sk_init(struct sock * __percpu *socks,
215+
static int nat_keepalive_sk_init(struct sock_bh_locked __percpu *socks,
206216
unsigned short family)
207217
{
208218
struct sock *sk;
@@ -214,22 +224,22 @@ static int nat_keepalive_sk_init(struct sock * __percpu *socks,
214224
if (err < 0)
215225
goto err;
216226

217-
*per_cpu_ptr(socks, i) = sk;
227+
per_cpu_ptr(socks, i)->sock = sk;
218228
}
219229

220230
return 0;
221231
err:
222232
for_each_possible_cpu(i)
223-
inet_ctl_sock_destroy(*per_cpu_ptr(socks, i));
233+
inet_ctl_sock_destroy(per_cpu_ptr(socks, i)->sock);
224234
return err;
225235
}
226236

227-
static void nat_keepalive_sk_fini(struct sock * __percpu *socks)
237+
static void nat_keepalive_sk_fini(struct sock_bh_locked __percpu *socks)
228238
{
229239
int i;
230240

231241
for_each_possible_cpu(i)
232-
inet_ctl_sock_destroy(*per_cpu_ptr(socks, i));
242+
inet_ctl_sock_destroy(per_cpu_ptr(socks, i)->sock);
233243
}
234244

235245
void xfrm_nat_keepalive_state_updated(struct xfrm_state *x)

0 commit comments

Comments
 (0)