Skip to content

Commit 60f05dd

Browse files
deepa-hubgregkh
authored andcommitted
sock: Make sock->sk_stamp thread-safe
[ Upstream commit 3a0ed3e ] Al Viro mentioned (Message-ID <20170626041334.GZ10672@ZenIV.linux.org.uk>) that there is probably a race condition lurking in accesses of sk_stamp on 32-bit machines. sock->sk_stamp is of type ktime_t which is always an s64. On a 32 bit architecture, we might run into situations of unsafe access as the access to the field becomes non atomic. Use seqlocks for synchronization. This allows us to avoid using spinlocks for readers as readers do not need mutual exclusion. Another approach to solve this is to require sk_lock for all modifications of the timestamps. The current approach allows for timestamps to have their own lock: sk_stamp_lock. This allows for the patch to not compete with already existing critical sections, and side effects are limited to the paths in the patch. The addition of the new field maintains the data locality optimizations from commit 9115e8c ("net: reorganize struct sock for better data locality") Note that all the instances of the sk_stamp accesses are either through the ioctl or the syscall recvmsg. Signed-off-by: Deepa Dinamani <deepa.kernel@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent fff7f71 commit 60f05dd

4 files changed

Lines changed: 55 additions & 15 deletions

File tree

‎include/net/sock.h‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ struct sock_common {
298298
* @sk_filter: socket filtering instructions
299299
* @sk_timer: sock cleanup timer
300300
* @sk_stamp: time stamp of last packet received
301+
* @sk_stamp_seq: lock for accessing sk_stamp on 32 bit architectures only
301302
* @sk_tsflags: SO_TIMESTAMPING socket options
302303
* @sk_tskey: counter to disambiguate concurrent tstamp requests
303304
* @sk_zckey: counter to order MSG_ZEROCOPY notifications
@@ -474,6 +475,9 @@ struct sock {
474475
const struct cred *sk_peer_cred;
475476
long sk_rcvtimeo;
476477
ktime_t sk_stamp;
478+
#if BITS_PER_LONG==32
479+
seqlock_t sk_stamp_seq;
480+
#endif
477481
u16 sk_tsflags;
478482
u8 sk_shutdown;
479483
u32 sk_tskey;
@@ -2290,6 +2294,34 @@ static inline void sk_drops_add(struct sock *sk, const struct sk_buff *skb)
22902294
atomic_add(segs, &sk->sk_drops);
22912295
}
22922296

2297+
static inline ktime_t sock_read_timestamp(struct sock *sk)
2298+
{
2299+
#if BITS_PER_LONG==32
2300+
unsigned int seq;
2301+
ktime_t kt;
2302+
2303+
do {
2304+
seq = read_seqbegin(&sk->sk_stamp_seq);
2305+
kt = sk->sk_stamp;
2306+
} while (read_seqretry(&sk->sk_stamp_seq, seq));
2307+
2308+
return kt;
2309+
#else
2310+
return sk->sk_stamp;
2311+
#endif
2312+
}
2313+
2314+
static inline void sock_write_timestamp(struct sock *sk, ktime_t kt)
2315+
{
2316+
#if BITS_PER_LONG==32
2317+
write_seqlock(&sk->sk_stamp_seq);
2318+
sk->sk_stamp = kt;
2319+
write_sequnlock(&sk->sk_stamp_seq);
2320+
#else
2321+
sk->sk_stamp = kt;
2322+
#endif
2323+
}
2324+
22932325
void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
22942326
struct sk_buff *skb);
22952327
void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
@@ -2314,7 +2346,7 @@ sock_recv_timestamp(struct msghdr *msg, struct sock *sk, struct sk_buff *skb)
23142346
(sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)))
23152347
__sock_recv_timestamp(msg, sk, skb);
23162348
else
2317-
sk->sk_stamp = kt;
2349+
sock_write_timestamp(sk, kt);
23182350

23192351
if (sock_flag(sk, SOCK_WIFI_STATUS) && skb->wifi_acked_valid)
23202352
__sock_recv_wifi_status(msg, sk, skb);
@@ -2335,9 +2367,9 @@ static inline void sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
23352367
if (sk->sk_flags & FLAGS_TS_OR_DROPS || sk->sk_tsflags & TSFLAGS_ANY)
23362368
__sock_recv_ts_and_drops(msg, sk, skb);
23372369
else if (unlikely(sock_flag(sk, SOCK_TIMESTAMP)))
2338-
sk->sk_stamp = skb->tstamp;
2370+
sock_write_timestamp(sk, skb->tstamp);
23392371
else if (unlikely(sk->sk_stamp == SK_DEFAULT_STAMP))
2340-
sk->sk_stamp = 0;
2372+
sock_write_timestamp(sk, 0);
23412373
}
23422374

23432375
void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags);

‎net/compat.c‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,14 @@ int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
467467
ctv = (struct compat_timeval __user *) userstamp;
468468
err = -ENOENT;
469469
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
470-
tv = ktime_to_timeval(sk->sk_stamp);
470+
tv = ktime_to_timeval(sock_read_timestamp(sk));
471+
471472
if (tv.tv_sec == -1)
472473
return err;
473474
if (tv.tv_sec == 0) {
474-
sk->sk_stamp = ktime_get_real();
475-
tv = ktime_to_timeval(sk->sk_stamp);
475+
ktime_t kt = ktime_get_real();
476+
sock_write_timestamp(sk, kt);
477+
tv = ktime_to_timeval(kt);
476478
}
477479
err = 0;
478480
if (put_user(tv.tv_sec, &ctv->tv_sec) ||
@@ -494,12 +496,13 @@ int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *usersta
494496
ctv = (struct compat_timespec __user *) userstamp;
495497
err = -ENOENT;
496498
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
497-
ts = ktime_to_timespec(sk->sk_stamp);
499+
ts = ktime_to_timespec(sock_read_timestamp(sk));
498500
if (ts.tv_sec == -1)
499501
return err;
500502
if (ts.tv_sec == 0) {
501-
sk->sk_stamp = ktime_get_real();
502-
ts = ktime_to_timespec(sk->sk_stamp);
503+
ktime_t kt = ktime_get_real();
504+
sock_write_timestamp(sk, kt);
505+
ts = ktime_to_timespec(kt);
503506
}
504507
err = 0;
505508
if (put_user(ts.tv_sec, &ctv->tv_sec) ||

‎net/core/sock.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,6 +2803,9 @@ void sock_init_data(struct socket *sock, struct sock *sk)
28032803
sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
28042804

28052805
sk->sk_stamp = SK_DEFAULT_STAMP;
2806+
#if BITS_PER_LONG==32
2807+
seqlock_init(&sk->sk_stamp_seq);
2808+
#endif
28062809
atomic_set(&sk->sk_zckey, 0);
28072810

28082811
#ifdef CONFIG_NET_RX_BUSY_POLL
@@ -2902,12 +2905,13 @@ int sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
29022905
struct timeval tv;
29032906

29042907
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
2905-
tv = ktime_to_timeval(sk->sk_stamp);
2908+
tv = ktime_to_timeval(sock_read_timestamp(sk));
29062909
if (tv.tv_sec == -1)
29072910
return -ENOENT;
29082911
if (tv.tv_sec == 0) {
2909-
sk->sk_stamp = ktime_get_real();
2910-
tv = ktime_to_timeval(sk->sk_stamp);
2912+
ktime_t kt = ktime_get_real();
2913+
sock_write_timestamp(sk, kt);
2914+
tv = ktime_to_timeval(kt);
29112915
}
29122916
return copy_to_user(userstamp, &tv, sizeof(tv)) ? -EFAULT : 0;
29132917
}
@@ -2918,11 +2922,12 @@ int sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
29182922
struct timespec ts;
29192923

29202924
sock_enable_timestamp(sk, SOCK_TIMESTAMP);
2921-
ts = ktime_to_timespec(sk->sk_stamp);
2925+
ts = ktime_to_timespec(sock_read_timestamp(sk));
29222926
if (ts.tv_sec == -1)
29232927
return -ENOENT;
29242928
if (ts.tv_sec == 0) {
2925-
sk->sk_stamp = ktime_get_real();
2929+
ktime_t kt = ktime_get_real();
2930+
sock_write_timestamp(sk, kt);
29262931
ts = ktime_to_timespec(sk->sk_stamp);
29272932
}
29282933
return copy_to_user(userstamp, &ts, sizeof(ts)) ? -EFAULT : 0;

‎net/sunrpc/svcsock.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ static int svc_udp_recvfrom(struct svc_rqst *rqstp)
574574
/* Don't enable netstamp, sunrpc doesn't
575575
need that much accuracy */
576576
}
577-
svsk->sk_sk->sk_stamp = skb->tstamp;
577+
sock_write_timestamp(svsk->sk_sk, skb->tstamp);
578578
set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */
579579

580580
len = skb->len;

0 commit comments

Comments
 (0)