Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: use unrcu_pointer() helper

Toke mentioned unrcu_pointer() existence, allowing
to remove some of the ugly casts we have when using
xchg() for rcu protected pointers.

Also make inet_rcv_compat const.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Toke Høiland-Jørgensen <toke@redhat.com>
Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/r/20240604111603.45871-1-edumazet@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Eric Dumazet and committed by
Paolo Abeni
b4cb4a13 59d0f481

+19 -21
+1 -1
include/net/sock.h
··· 2095 2095 2096 2096 sk_tx_queue_clear(sk); 2097 2097 WRITE_ONCE(sk->sk_dst_pending_confirm, 0); 2098 - old_dst = xchg((__force struct dst_entry **)&sk->sk_dst_cache, dst); 2098 + old_dst = unrcu_pointer(xchg(&sk->sk_dst_cache, RCU_INITIALIZER(dst))); 2099 2099 dst_release(old_dst); 2100 2100 } 2101 2101
+1 -1
net/core/gen_estimator.c
··· 206 206 { 207 207 struct net_rate_estimator *est; 208 208 209 - est = xchg((__force struct net_rate_estimator **)rate_est, NULL); 209 + est = unrcu_pointer(xchg(rate_est, NULL)); 210 210 if (est) { 211 211 timer_shutdown_sync(&est->timer); 212 212 kfree_rcu(est, rcu);
+3 -5
net/core/sock_diag.c
··· 18 18 19 19 static const struct sock_diag_handler __rcu *sock_diag_handlers[AF_MAX]; 20 20 21 - static struct sock_diag_inet_compat __rcu *inet_rcv_compat; 21 + static const struct sock_diag_inet_compat __rcu *inet_rcv_compat; 22 22 23 23 static struct workqueue_struct *broadcast_wq; 24 24 ··· 187 187 188 188 void sock_diag_register_inet_compat(const struct sock_diag_inet_compat *ptr) 189 189 { 190 - xchg((__force const struct sock_diag_inet_compat **)&inet_rcv_compat, 191 - ptr); 190 + xchg(&inet_rcv_compat, RCU_INITIALIZER(ptr)); 192 191 } 193 192 EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); 194 193 ··· 195 196 { 196 197 const struct sock_diag_inet_compat *old; 197 198 198 - old = xchg((__force const struct sock_diag_inet_compat **)&inet_rcv_compat, 199 - NULL); 199 + old = unrcu_pointer(xchg(&inet_rcv_compat, NULL)); 200 200 WARN_ON_ONCE(old != ptr); 201 201 } 202 202 EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
+1 -1
net/ipv4/cipso_ipv4.c
··· 1953 1953 buf = NULL; 1954 1954 1955 1955 req_inet = inet_rsk(req); 1956 - opt = xchg((__force struct ip_options_rcu **)&req_inet->ireq_opt, opt); 1956 + opt = unrcu_pointer(xchg(&req_inet->ireq_opt, RCU_INITIALIZER(opt))); 1957 1957 if (opt) 1958 1958 kfree_rcu(opt, rcu); 1959 1959
+1 -1
net/ipv4/tcp.c
··· 3081 3081 icsk->icsk_ack.rcv_mss = TCP_MIN_MSS; 3082 3082 memset(&tp->rx_opt, 0, sizeof(tp->rx_opt)); 3083 3083 __sk_dst_reset(sk); 3084 - dst_release(xchg((__force struct dst_entry **)&sk->sk_rx_dst, NULL)); 3084 + dst_release(unrcu_pointer(xchg(&sk->sk_rx_dst, NULL))); 3085 3085 tcp_saved_syn_free(tp); 3086 3086 tp->compressed_ack = 0; 3087 3087 tp->segs_in = 0;
+4 -3
net/ipv4/tcp_fastopen.c
··· 49 49 { 50 50 struct tcp_fastopen_context *ctxt; 51 51 52 - ctxt = xchg((__force struct tcp_fastopen_context **)&net->ipv4.tcp_fastopen_ctx, NULL); 52 + ctxt = unrcu_pointer(xchg(&net->ipv4.tcp_fastopen_ctx, NULL)); 53 53 54 54 if (ctxt) 55 55 call_rcu(&ctxt->rcu, tcp_fastopen_ctx_free); ··· 80 80 81 81 if (sk) { 82 82 q = &inet_csk(sk)->icsk_accept_queue.fastopenq; 83 - octx = xchg((__force struct tcp_fastopen_context **)&q->ctx, ctx); 83 + octx = unrcu_pointer(xchg(&q->ctx, RCU_INITIALIZER(ctx))); 84 84 } else { 85 - octx = xchg((__force struct tcp_fastopen_context **)&net->ipv4.tcp_fastopen_ctx, ctx); 85 + octx = unrcu_pointer(xchg(&net->ipv4.tcp_fastopen_ctx, 86 + RCU_INITIALIZER(ctx))); 86 87 } 87 88 88 89 if (octx)
+1 -1
net/ipv4/udp.c
··· 2230 2230 struct dst_entry *old; 2231 2231 2232 2232 if (dst_hold_safe(dst)) { 2233 - old = xchg((__force struct dst_entry **)&sk->sk_rx_dst, dst); 2233 + old = unrcu_pointer(xchg(&sk->sk_rx_dst, RCU_INITIALIZER(dst))); 2234 2234 dst_release(old); 2235 2235 return old != dst; 2236 2236 }
+1 -1
net/ipv6/af_inet6.c
··· 509 509 510 510 /* Free tx options */ 511 511 512 - opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL); 512 + opt = unrcu_pointer(xchg(&np->opt, NULL)); 513 513 if (opt) { 514 514 atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 515 515 txopt_put(opt);
+1 -1
net/ipv6/ip6_fib.c
··· 984 984 if (pcpu_rt && rcu_access_pointer(pcpu_rt->from) == match) { 985 985 struct fib6_info *from; 986 986 987 - from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 987 + from = unrcu_pointer(xchg(&pcpu_rt->from, NULL)); 988 988 fib6_info_release(from); 989 989 } 990 990 }
+1 -2
net/ipv6/ipv6_sockglue.c
··· 111 111 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); 112 112 } 113 113 } 114 - opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt, 115 - opt); 114 + opt = unrcu_pointer(xchg(&inet6_sk(sk)->opt, RCU_INITIALIZER(opt))); 116 115 sk_dst_reset(sk); 117 116 118 117 return opt;
+3 -3
net/ipv6/route.c
··· 368 368 in6_dev_put(idev); 369 369 } 370 370 371 - from = xchg((__force struct fib6_info **)&rt->from, NULL); 371 + from = unrcu_pointer(xchg(&rt->from, NULL)); 372 372 fib6_info_release(from); 373 373 } 374 374 ··· 1437 1437 if (res->f6i->fib6_destroying) { 1438 1438 struct fib6_info *from; 1439 1439 1440 - from = xchg((__force struct fib6_info **)&pcpu_rt->from, NULL); 1440 + from = unrcu_pointer(xchg(&pcpu_rt->from, NULL)); 1441 1441 fib6_info_release(from); 1442 1442 } 1443 1443 ··· 1466 1466 /* purge completely the exception to allow releasing the held resources: 1467 1467 * some [sk] cache may keep the dst around for unlimited time 1468 1468 */ 1469 - from = xchg((__force struct fib6_info **)&rt6_ex->rt6i->from, NULL); 1469 + from = unrcu_pointer(xchg(&rt6_ex->rt6i->from, NULL)); 1470 1470 fib6_info_release(from); 1471 1471 dst_dev_put(&rt6_ex->rt6i->dst); 1472 1472
+1 -1
net/sched/act_api.c
··· 62 62 { 63 63 struct tc_cookie *old; 64 64 65 - old = xchg((__force struct tc_cookie **)old_cookie, new_cookie); 65 + old = unrcu_pointer(xchg(old_cookie, RCU_INITIALIZER(new_cookie))); 66 66 if (old) 67 67 call_rcu(&old->rcu, tcf_free_cookie_rcu); 68 68 }