net: annotate data-races around sk->sk_tsflags

sk->sk_tsflags can be read locklessly, add corresponding annotations.

Fixes: b9f40e21ef ("net-timestamp: move timestamp flags out of sk_flags")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Willem de Bruijn <willemb@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Eric Dumazet 2023-08-31 13:52:11 +00:00 коммит произвёл David S. Miller
Родитель 9531e4a83f
Коммит e3390b30a5
13 изменённых файлов: 40 добавлений и 32 удалений

Просмотреть файл

@ -94,7 +94,7 @@ static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
ipcm_init(ipcm); ipcm_init(ipcm);
ipcm->sockc.mark = READ_ONCE(inet->sk.sk_mark); ipcm->sockc.mark = READ_ONCE(inet->sk.sk_mark);
ipcm->sockc.tsflags = inet->sk.sk_tsflags; ipcm->sockc.tsflags = READ_ONCE(inet->sk.sk_tsflags);
ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if); ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if);
ipcm->addr = inet->inet_saddr; ipcm->addr = inet->inet_saddr;
ipcm->protocol = inet->inet_num; ipcm->protocol = inet->inet_num;

Просмотреть файл

@ -1906,7 +1906,9 @@ struct sockcm_cookie {
static inline void sockcm_init(struct sockcm_cookie *sockc, static inline void sockcm_init(struct sockcm_cookie *sockc,
const struct sock *sk) const struct sock *sk)
{ {
*sockc = (struct sockcm_cookie) { .tsflags = sk->sk_tsflags }; *sockc = (struct sockcm_cookie) {
.tsflags = READ_ONCE(sk->sk_tsflags)
};
} }
int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg, int __sock_cmsg_send(struct sock *sk, struct cmsghdr *cmsg,
@ -2701,9 +2703,9 @@ void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
static inline void static inline void
sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb) sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
{ {
ktime_t kt = skb->tstamp;
struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb); struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
u32 tsflags = READ_ONCE(sk->sk_tsflags);
ktime_t kt = skb->tstamp;
/* /*
* generate control messages if * generate control messages if
* - receive time stamping in software requested * - receive time stamping in software requested
@ -2711,10 +2713,10 @@ sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
* - hardware time stamps available and wanted * - hardware time stamps available and wanted
*/ */
if (sock_flag(sk, SOCK_RCVTSTAMP) || if (sock_flag(sk, SOCK_RCVTSTAMP) ||
(sk->sk_tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) || (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE) ||
(kt && sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) || (kt && tsflags & SOF_TIMESTAMPING_SOFTWARE) ||
(hwtstamps->hwtstamp && (hwtstamps->hwtstamp &&
(sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE))) (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)))
__sock_recv_timestamp(msg, sk, skb); __sock_recv_timestamp(msg, sk, skb);
else else
sock_write_timestamp(sk, kt); sock_write_timestamp(sk, kt);
@ -2736,7 +2738,8 @@ static inline void sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
#define TSFLAGS_ANY (SOF_TIMESTAMPING_SOFTWARE | \ #define TSFLAGS_ANY (SOF_TIMESTAMPING_SOFTWARE | \
SOF_TIMESTAMPING_RAW_HARDWARE) SOF_TIMESTAMPING_RAW_HARDWARE)
if (sk->sk_flags & FLAGS_RECV_CMSGS || sk->sk_tsflags & TSFLAGS_ANY) if (sk->sk_flags & FLAGS_RECV_CMSGS ||
READ_ONCE(sk->sk_tsflags) & TSFLAGS_ANY)
__sock_recv_cmsgs(msg, sk, skb); __sock_recv_cmsgs(msg, sk, skb);
else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP))) else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP)))
sock_write_timestamp(sk, skb->tstamp); sock_write_timestamp(sk, skb->tstamp);

Просмотреть файл

@ -974,6 +974,7 @@ static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk,
struct sock_exterr_skb *serr; struct sock_exterr_skb *serr;
struct sk_buff *skb; struct sk_buff *skb;
char *state = "UNK"; char *state = "UNK";
u32 tsflags;
int err; int err;
jsk = j1939_sk(sk); jsk = j1939_sk(sk);
@ -981,13 +982,14 @@ static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk,
if (!(jsk->state & J1939_SOCK_ERRQUEUE)) if (!(jsk->state & J1939_SOCK_ERRQUEUE))
return; return;
tsflags = READ_ONCE(sk->sk_tsflags);
switch (type) { switch (type) {
case J1939_ERRQUEUE_TX_ACK: case J1939_ERRQUEUE_TX_ACK:
if (!(sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK)) if (!(tsflags & SOF_TIMESTAMPING_TX_ACK))
return; return;
break; break;
case J1939_ERRQUEUE_TX_SCHED: case J1939_ERRQUEUE_TX_SCHED:
if (!(sk->sk_tsflags & SOF_TIMESTAMPING_TX_SCHED)) if (!(tsflags & SOF_TIMESTAMPING_TX_SCHED))
return; return;
break; break;
case J1939_ERRQUEUE_TX_ABORT: case J1939_ERRQUEUE_TX_ABORT:
@ -997,7 +999,7 @@ static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk,
case J1939_ERRQUEUE_RX_DPO: case J1939_ERRQUEUE_RX_DPO:
fallthrough; fallthrough;
case J1939_ERRQUEUE_RX_ABORT: case J1939_ERRQUEUE_RX_ABORT:
if (!(sk->sk_tsflags & SOF_TIMESTAMPING_RX_SOFTWARE)) if (!(tsflags & SOF_TIMESTAMPING_RX_SOFTWARE))
return; return;
break; break;
default: default:
@ -1054,7 +1056,7 @@ static void __j1939_sk_errqueue(struct j1939_session *session, struct sock *sk,
} }
serr->opt_stats = true; serr->opt_stats = true;
if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) if (tsflags & SOF_TIMESTAMPING_OPT_ID)
serr->ee.ee_data = session->tskey; serr->ee.ee_data = session->tskey;
netdev_dbg(session->priv->ndev, "%s: 0x%p tskey: %i, state: %s\n", netdev_dbg(session->priv->ndev, "%s: 0x%p tskey: %i, state: %s\n",

Просмотреть файл

@ -5207,7 +5207,7 @@ static void __skb_complete_tx_timestamp(struct sk_buff *skb,
serr->ee.ee_info = tstype; serr->ee.ee_info = tstype;
serr->opt_stats = opt_stats; serr->opt_stats = opt_stats;
serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) {
serr->ee.ee_data = skb_shinfo(skb)->tskey; serr->ee.ee_data = skb_shinfo(skb)->tskey;
if (sk_is_tcp(sk)) if (sk_is_tcp(sk))
serr->ee.ee_data -= atomic_read(&sk->sk_tskey); serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
@ -5263,21 +5263,23 @@ void __skb_tstamp_tx(struct sk_buff *orig_skb,
{ {
struct sk_buff *skb; struct sk_buff *skb;
bool tsonly, opt_stats = false; bool tsonly, opt_stats = false;
u32 tsflags;
if (!sk) if (!sk)
return; return;
if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && tsflags = READ_ONCE(sk->sk_tsflags);
if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
return; return;
tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; tsonly = tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
if (!skb_may_tx_timestamp(sk, tsonly)) if (!skb_may_tx_timestamp(sk, tsonly))
return; return;
if (tsonly) { if (tsonly) {
#ifdef CONFIG_INET #ifdef CONFIG_INET
if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && if ((tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
sk_is_tcp(sk)) { sk_is_tcp(sk)) {
skb = tcp_get_timestamping_opt_stats(sk, orig_skb, skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
ack_skb); ack_skb);

Просмотреть файл

@ -937,7 +937,7 @@ int sock_set_timestamping(struct sock *sk, int optname,
return ret; return ret;
} }
sk->sk_tsflags = val; WRITE_ONCE(sk->sk_tsflags, val);
sock_valbool_flag(sk, SOCK_TSTAMP_NEW, optname == SO_TIMESTAMPING_NEW); sock_valbool_flag(sk, SOCK_TSTAMP_NEW, optname == SO_TIMESTAMPING_NEW);
if (val & SOF_TIMESTAMPING_RX_SOFTWARE) if (val & SOF_TIMESTAMPING_RX_SOFTWARE)
@ -1719,7 +1719,7 @@ int sk_getsockopt(struct sock *sk, int level, int optname,
case SO_TIMESTAMPING_OLD: case SO_TIMESTAMPING_OLD:
lv = sizeof(v.timestamping); lv = sizeof(v.timestamping);
v.timestamping.flags = sk->sk_tsflags; v.timestamping.flags = READ_ONCE(sk->sk_tsflags);
v.timestamping.bind_phc = sk->sk_bind_phc; v.timestamping.bind_phc = sk->sk_bind_phc;
break; break;

Просмотреть файл

@ -981,7 +981,7 @@ static int __ip_append_data(struct sock *sk,
paged = !!cork->gso_size; paged = !!cork->gso_size;
if (cork->tx_flags & SKBTX_ANY_TSTAMP && if (cork->tx_flags & SKBTX_ANY_TSTAMP &&
sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID)
tskey = atomic_inc_return(&sk->sk_tskey) - 1; tskey = atomic_inc_return(&sk->sk_tskey) - 1;
hh_len = LL_RESERVED_SPACE(rt->dst.dev); hh_len = LL_RESERVED_SPACE(rt->dst.dev);

Просмотреть файл

@ -511,7 +511,7 @@ static bool ipv4_datagram_support_cmsg(const struct sock *sk,
* or without payload (SOF_TIMESTAMPING_OPT_TSONLY). * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
*/ */
info = PKTINFO_SKB_CB(skb); info = PKTINFO_SKB_CB(skb);
if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) || if (!(READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_CMSG) ||
!info->ipi_ifindex) !info->ipi_ifindex)
return false; return false;

Просмотреть файл

@ -2259,14 +2259,14 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
} }
} }
if (sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_SOFTWARE)
has_timestamping = true; has_timestamping = true;
else else
tss->ts[0] = (struct timespec64) {0}; tss->ts[0] = (struct timespec64) {0};
} }
if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec) { if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec) {
if (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_RAW_HARDWARE)
has_timestamping = true; has_timestamping = true;
else else
tss->ts[2] = (struct timespec64) {0}; tss->ts[2] = (struct timespec64) {0};

Просмотреть файл

@ -1501,7 +1501,7 @@ static int __ip6_append_data(struct sock *sk,
orig_mtu = mtu; orig_mtu = mtu;
if (cork->tx_flags & SKBTX_ANY_TSTAMP && if (cork->tx_flags & SKBTX_ANY_TSTAMP &&
sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID)
tskey = atomic_inc_return(&sk->sk_tskey) - 1; tskey = atomic_inc_return(&sk->sk_tskey) - 1;
hh_len = LL_RESERVED_SPACE(rt->dst.dev); hh_len = LL_RESERVED_SPACE(rt->dst.dev);

Просмотреть файл

@ -119,7 +119,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
return -EINVAL; return -EINVAL;
ipcm6_init_sk(&ipc6, np); ipcm6_init_sk(&ipc6, np);
ipc6.sockc.tsflags = sk->sk_tsflags; ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags);
ipc6.sockc.mark = READ_ONCE(sk->sk_mark); ipc6.sockc.mark = READ_ONCE(sk->sk_mark);
fl6.flowi6_oif = oif; fl6.flowi6_oif = oif;

Просмотреть файл

@ -772,7 +772,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
fl6.flowi6_uid = sk->sk_uid; fl6.flowi6_uid = sk->sk_uid;
ipcm6_init(&ipc6); ipcm6_init(&ipc6);
ipc6.sockc.tsflags = sk->sk_tsflags; ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags);
ipc6.sockc.mark = fl6.flowi6_mark; ipc6.sockc.mark = fl6.flowi6_mark;
if (sin6) { if (sin6) {

Просмотреть файл

@ -1339,7 +1339,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
ipcm6_init(&ipc6); ipcm6_init(&ipc6);
ipc6.gso_size = READ_ONCE(up->gso_size); ipc6.gso_size = READ_ONCE(up->gso_size);
ipc6.sockc.tsflags = sk->sk_tsflags; ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags);
ipc6.sockc.mark = READ_ONCE(sk->sk_mark); ipc6.sockc.mark = READ_ONCE(sk->sk_mark);
/* destination address check */ /* destination address check */

Просмотреть файл

@ -825,7 +825,7 @@ static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index) static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
{ {
bool cycles = sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC; bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
struct net_device *orig_dev; struct net_device *orig_dev;
ktime_t hwtstamp; ktime_t hwtstamp;
@ -877,12 +877,12 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP); int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW); int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
struct scm_timestamping_internal tss; struct scm_timestamping_internal tss;
int empty = 1, false_tstamp = 0; int empty = 1, false_tstamp = 0;
struct skb_shared_hwtstamps *shhwtstamps = struct skb_shared_hwtstamps *shhwtstamps =
skb_hwtstamps(skb); skb_hwtstamps(skb);
int if_index; int if_index;
ktime_t hwtstamp; ktime_t hwtstamp;
u32 tsflags;
/* Race occurred between timestamp enabling and packet /* Race occurred between timestamp enabling and packet
receiving. Fill in the current time for now. */ receiving. Fill in the current time for now. */
@ -924,11 +924,12 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
} }
memset(&tss, 0, sizeof(tss)); memset(&tss, 0, sizeof(tss));
if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) && tsflags = READ_ONCE(sk->sk_tsflags);
if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0)) ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
empty = 0; empty = 0;
if (shhwtstamps && if (shhwtstamps &&
(sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) && (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
!skb_is_swtx_tstamp(skb, false_tstamp)) { !skb_is_swtx_tstamp(skb, false_tstamp)) {
if_index = 0; if_index = 0;
if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV) if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
@ -936,14 +937,14 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
else else
hwtstamp = shhwtstamps->hwtstamp; hwtstamp = shhwtstamps->hwtstamp;
if (sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC) if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
hwtstamp = ptp_convert_timestamp(&hwtstamp, hwtstamp = ptp_convert_timestamp(&hwtstamp,
sk->sk_bind_phc); sk->sk_bind_phc);
if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) { if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) {
empty = 0; empty = 0;
if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) && if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
!skb_is_err_queue(skb)) !skb_is_err_queue(skb))
put_ts_pktinfo(msg, skb, if_index); put_ts_pktinfo(msg, skb, if_index);
} }