Skip to content

Commit 8a3854c

Browse files
Paolo Abenidavem330
authored andcommitted
udp: track the forward memory release threshold in an hot cacheline
When the receiver process and the BH runs on different cores, udp_rmem_release() experience a cache miss while accessing sk_rcvbuf, as the latter shares the same cacheline with sk_forward_alloc, written by the BH. With this patch, UDP tracks the rcvbuf value and its update via custom SOL_SOCKET socket options, and copies the forward memory threshold value used by udp_rmem_release() in a different cacheline, already accessed by the above function and uncontended. Since the UDP socket init operation grown a bit, factor out the common code between v4 and v6 in a shared helper. Overall the above give a 10% peek throughput increase under UDP flood. Signed-off-by: Paolo Abeni <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Acked-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent a5ef058 commit 8a3854c

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

include/linux/udp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ struct udp_sock {
8787

8888
/* This field is dirtied by udp_recvmsg() */
8989
int forward_deficit;
90+
91+
/* This fields follows rcvbuf value, and is touched by udp_recvmsg */
92+
int forward_threshold;
9093
};
9194

9295
#define UDP_MAX_SEGMENTS (1 << 6UL)

include/net/udp.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));
174174
struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
175175
netdev_features_t features, bool is_ipv6);
176176

177+
static inline void udp_lib_init_sock(struct sock *sk)
178+
{
179+
struct udp_sock *up = udp_sk(sk);
180+
181+
skb_queue_head_init(&up->reader_queue);
182+
up->forward_threshold = sk->sk_rcvbuf >> 2;
183+
set_bit(SOCK_CUSTOM_SOCKOPT, &sk->sk_socket->flags);
184+
}
185+
177186
/* hash routines shared between UDPv4/6 and UDP-Litev4/6 */
178187
static inline int udp_lib_hash(struct sock *sk)
179188
{

net/ipv4/udp.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ static void udp_rmem_release(struct sock *sk, int size, int partial,
14481448
if (likely(partial)) {
14491449
up->forward_deficit += size;
14501450
size = up->forward_deficit;
1451-
if (size < (sk->sk_rcvbuf >> 2) &&
1451+
if (size < READ_ONCE(up->forward_threshold) &&
14521452
!skb_queue_empty(&up->reader_queue))
14531453
return;
14541454
} else {
@@ -1622,7 +1622,7 @@ static void udp_destruct_sock(struct sock *sk)
16221622

16231623
int udp_init_sock(struct sock *sk)
16241624
{
1625-
skb_queue_head_init(&udp_sk(sk)->reader_queue);
1625+
udp_lib_init_sock(sk);
16261626
sk->sk_destruct = udp_destruct_sock;
16271627
return 0;
16281628
}
@@ -2671,6 +2671,18 @@ int udp_lib_setsockopt(struct sock *sk, int level, int optname,
26712671
int err = 0;
26722672
int is_udplite = IS_UDPLITE(sk);
26732673

2674+
if (level == SOL_SOCKET) {
2675+
err = sk_setsockopt(sk, level, optname, optval, optlen);
2676+
2677+
if (optname == SO_RCVBUF || optname == SO_RCVBUFFORCE) {
2678+
sockopt_lock_sock(sk);
2679+
/* paired with READ_ONCE in udp_rmem_release() */
2680+
WRITE_ONCE(up->forward_threshold, sk->sk_rcvbuf >> 2);
2681+
sockopt_release_sock(sk);
2682+
}
2683+
return err;
2684+
}
2685+
26742686
if (optlen < sizeof(int))
26752687
return -EINVAL;
26762688

@@ -2784,7 +2796,7 @@ EXPORT_SYMBOL(udp_lib_setsockopt);
27842796
int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
27852797
unsigned int optlen)
27862798
{
2787-
if (level == SOL_UDP || level == SOL_UDPLITE)
2799+
if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET)
27882800
return udp_lib_setsockopt(sk, level, optname,
27892801
optval, optlen,
27902802
udp_push_pending_frames);

net/ipv6/udp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void udpv6_destruct_sock(struct sock *sk)
6464

6565
int udpv6_init_sock(struct sock *sk)
6666
{
67-
skb_queue_head_init(&udp_sk(sk)->reader_queue);
67+
udp_lib_init_sock(sk);
6868
sk->sk_destruct = udpv6_destruct_sock;
6969
return 0;
7070
}
@@ -1669,7 +1669,7 @@ void udpv6_destroy_sock(struct sock *sk)
16691669
int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
16701670
unsigned int optlen)
16711671
{
1672-
if (level == SOL_UDP || level == SOL_UDPLITE)
1672+
if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET)
16731673
return udp_lib_setsockopt(sk, level, optname,
16741674
optval, optlen,
16751675
udp_v6_push_pending_frames);

0 commit comments

Comments
 (0)