Skip to content

Commit 5e6300e

Browse files
edumazetdavem330
authored andcommitted
net: annotate data-races around sk->sk_forward_alloc
Every time sk->sk_forward_alloc is read locklessly, add a READ_ONCE(). Add sk_forward_alloc_add() helper to centralize updates, to reduce number of WRITE_ONCE(). Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 66d58f0 commit 5e6300e

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

include/net/sock.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@ static inline void sk_wmem_queued_add(struct sock *sk, int val)
10531053
WRITE_ONCE(sk->sk_wmem_queued, sk->sk_wmem_queued + val);
10541054
}
10551055

1056+
static inline void sk_forward_alloc_add(struct sock *sk, int val)
1057+
{
1058+
/* Paired with lockless reads of sk->sk_forward_alloc */
1059+
WRITE_ONCE(sk->sk_forward_alloc, sk->sk_forward_alloc + val);
1060+
}
1061+
10561062
void sk_stream_write_space(struct sock *sk);
10571063

10581064
/* OOB backlog add */
@@ -1377,7 +1383,7 @@ static inline int sk_forward_alloc_get(const struct sock *sk)
13771383
if (sk->sk_prot->forward_alloc_get)
13781384
return sk->sk_prot->forward_alloc_get(sk);
13791385
#endif
1380-
return sk->sk_forward_alloc;
1386+
return READ_ONCE(sk->sk_forward_alloc);
13811387
}
13821388

13831389
static inline bool __sk_stream_memory_free(const struct sock *sk, int wake)
@@ -1673,14 +1679,14 @@ static inline void sk_mem_charge(struct sock *sk, int size)
16731679
{
16741680
if (!sk_has_account(sk))
16751681
return;
1676-
sk->sk_forward_alloc -= size;
1682+
sk_forward_alloc_add(sk, -size);
16771683
}
16781684

16791685
static inline void sk_mem_uncharge(struct sock *sk, int size)
16801686
{
16811687
if (!sk_has_account(sk))
16821688
return;
1683-
sk->sk_forward_alloc += size;
1689+
sk_forward_alloc_add(sk, size);
16841690
sk_mem_reclaim(sk);
16851691
}
16861692

net/core/sock.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ static int sock_reserve_memory(struct sock *sk, int bytes)
10451045
mem_cgroup_uncharge_skmem(sk->sk_memcg, pages);
10461046
return -ENOMEM;
10471047
}
1048-
sk->sk_forward_alloc += pages << PAGE_SHIFT;
1048+
sk_forward_alloc_add(sk, pages << PAGE_SHIFT);
10491049

10501050
WRITE_ONCE(sk->sk_reserved_mem,
10511051
sk->sk_reserved_mem + (pages << PAGE_SHIFT));
@@ -3139,10 +3139,10 @@ int __sk_mem_schedule(struct sock *sk, int size, int kind)
31393139
{
31403140
int ret, amt = sk_mem_pages(size);
31413141

3142-
sk->sk_forward_alloc += amt << PAGE_SHIFT;
3142+
sk_forward_alloc_add(sk, amt << PAGE_SHIFT);
31433143
ret = __sk_mem_raise_allocated(sk, size, amt, kind);
31443144
if (!ret)
3145-
sk->sk_forward_alloc -= amt << PAGE_SHIFT;
3145+
sk_forward_alloc_add(sk, -(amt << PAGE_SHIFT));
31463146
return ret;
31473147
}
31483148
EXPORT_SYMBOL(__sk_mem_schedule);
@@ -3174,7 +3174,7 @@ void __sk_mem_reduce_allocated(struct sock *sk, int amount)
31743174
void __sk_mem_reclaim(struct sock *sk, int amount)
31753175
{
31763176
amount >>= PAGE_SHIFT;
3177-
sk->sk_forward_alloc -= amount << PAGE_SHIFT;
3177+
sk_forward_alloc_add(sk, -(amount << PAGE_SHIFT));
31783178
__sk_mem_reduce_allocated(sk, amount);
31793179
}
31803180
EXPORT_SYMBOL(__sk_mem_reclaim);

net/ipv4/tcp_output.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3474,7 +3474,7 @@ void sk_forced_mem_schedule(struct sock *sk, int size)
34743474
if (delta <= 0)
34753475
return;
34763476
amt = sk_mem_pages(delta);
3477-
sk->sk_forward_alloc += amt << PAGE_SHIFT;
3477+
sk_forward_alloc_add(sk, amt << PAGE_SHIFT);
34783478
sk_memory_allocated_add(sk, amt);
34793479

34803480
if (mem_cgroup_sockets_enabled && sk->sk_memcg)

net/ipv4/udp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,9 +1414,9 @@ static void udp_rmem_release(struct sock *sk, int size, int partial,
14141414
spin_lock(&sk_queue->lock);
14151415

14161416

1417-
sk->sk_forward_alloc += size;
1417+
sk_forward_alloc_add(sk, size);
14181418
amt = (sk->sk_forward_alloc - partial) & ~(PAGE_SIZE - 1);
1419-
sk->sk_forward_alloc -= amt;
1419+
sk_forward_alloc_add(sk, -amt);
14201420

14211421
if (amt)
14221422
__sk_mem_reduce_allocated(sk, amt >> PAGE_SHIFT);
@@ -1527,7 +1527,7 @@ int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
15271527
goto uncharge_drop;
15281528
}
15291529

1530-
sk->sk_forward_alloc -= size;
1530+
sk_forward_alloc_add(sk, -size);
15311531

15321532
/* no need to setup a destructor, we will explicitly release the
15331533
* forward allocated memory on dequeue

net/mptcp/protocol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,7 @@ static int mptcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
18001800
}
18011801

18021802
/* data successfully copied into the write queue */
1803-
sk->sk_forward_alloc -= total_ts;
1803+
sk_forward_alloc_add(sk, -total_ts);
18041804
copied += psize;
18051805
dfrag->data_len += psize;
18061806
frag_truesize += psize;
@@ -3257,7 +3257,7 @@ void mptcp_destroy_common(struct mptcp_sock *msk, unsigned int flags)
32573257
/* move all the rx fwd alloc into the sk_mem_reclaim_final in
32583258
* inet_sock_destruct() will dispose it
32593259
*/
3260-
sk->sk_forward_alloc += msk->rmem_fwd_alloc;
3260+
sk_forward_alloc_add(sk, msk->rmem_fwd_alloc);
32613261
msk->rmem_fwd_alloc = 0;
32623262
mptcp_token_destroy(msk);
32633263
mptcp_pm_free_anno_list(msk);
@@ -3522,7 +3522,7 @@ static void mptcp_shutdown(struct sock *sk, int how)
35223522

35233523
static int mptcp_forward_alloc_get(const struct sock *sk)
35243524
{
3525-
return sk->sk_forward_alloc + mptcp_sk(sk)->rmem_fwd_alloc;
3525+
return READ_ONCE(sk->sk_forward_alloc) + mptcp_sk(sk)->rmem_fwd_alloc;
35263526
}
35273527

35283528
static int mptcp_ioctl_outq(const struct mptcp_sock *msk, u64 v)

0 commit comments

Comments
 (0)