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

soreuseport: Fix socket selection for SO_INCOMING_CPU.

Kazuho Oku reported that setsockopt(SO_INCOMING_CPU) does not work
with setsockopt(SO_REUSEPORT) since v4.6.

With the combination of SO_REUSEPORT and SO_INCOMING_CPU, we could
build a highly efficient server application.

setsockopt(SO_INCOMING_CPU) associates a CPU with a TCP listener
or UDP socket, and then incoming packets processed on the CPU will
likely be distributed to the socket. Technically, a socket could
even receive packets handled on another CPU if no sockets in the
reuseport group have the same CPU receiving the flow.

The logic exists in compute_score() so that a socket will get a higher
score if it has the same CPU with the flow. However, the score gets
ignored after the blamed two commits, which introduced a faster socket
selection algorithm for SO_REUSEPORT.

This patch introduces a counter of sockets with SO_INCOMING_CPU in
a reuseport group to check if we should iterate all sockets to find
a proper one. We increment the counter when

* calling listen() if the socket has SO_INCOMING_CPU and SO_REUSEPORT

* enabling SO_INCOMING_CPU if the socket is in a reuseport group

Also, we decrement it when

* detaching a socket out of the group to apply SO_INCOMING_CPU to
migrated TCP requests

* disabling SO_INCOMING_CPU if the socket is in a reuseport group

When the counter reaches 0, we can get back to the O(1) selection
algorithm.

The overall changes are negligible for the non-SO_INCOMING_CPU case,
and the only notable thing is that we have to update sk_incomnig_cpu
under reuseport_lock. Otherwise, the race prevents transitioning to
the O(n) algorithm and results in the wrong socket selection.

cpu1 (setsockopt) cpu2 (listen)
+-----------------+ +-------------+

lock_sock(sk1) lock_sock(sk2)

reuseport_update_incoming_cpu(sk1, val)
.
| /* set CPU as 0 */
|- WRITE_ONCE(sk1->incoming_cpu, val)
|
| spin_lock_bh(&reuseport_lock)
| reuseport_grow(sk2, reuse)
| .
| |- more_socks_size = reuse->max_socks * 2U;
| |- if (more_socks_size > U16_MAX &&
| | reuse->num_closed_socks)
| | .
| | |- RCU_INIT_POINTER(sk1->sk_reuseport_cb, NULL);
| | `- __reuseport_detach_closed_sock(sk1, reuse)
| | .
| | `- reuseport_put_incoming_cpu(sk1, reuse)
| | .
| | | /* Read shutdown()ed sk1's sk_incoming_cpu
| | | * without lock_sock().
| | | */
| | `- if (sk1->sk_incoming_cpu >= 0)
| | .
| | | /* decrement not-yet-incremented
| | | * count, which is never incremented.
| | | */
| | `- __reuseport_put_incoming_cpu(reuse);
| |
| `- spin_lock_bh(&reuseport_lock)
|
|- spin_lock_bh(&reuseport_lock)
|
|- reuse = rcu_dereference_protected(sk1->sk_reuseport_cb, ...)
|- if (!reuse)
| .
| | /* Cannot increment reuse->incoming_cpu. */
| `- goto out;
|
`- spin_unlock_bh(&reuseport_lock)

Fixes: e32ea7e74727 ("soreuseport: fast reuseport UDP socket selection")
Fixes: c125e80b8868 ("soreuseport: fast reuseport TCP socket selection")
Reported-by: Kazuho Oku <kazuhooku@gmail.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Kuniyuki Iwashima and committed by
Paolo Abeni
b261eda8 71920a77

+92 -6
+2
include/net/sock_reuseport.h
··· 16 16 u16 max_socks; /* length of socks */ 17 17 u16 num_socks; /* elements in socks */ 18 18 u16 num_closed_socks; /* closed elements in socks */ 19 + u16 incoming_cpu; 19 20 /* The last synq overflow event timestamp of this 20 21 * reuse->socks[] group. 21 22 */ ··· 59 58 } 60 59 61 60 void reuseport_has_conns_set(struct sock *sk); 61 + void reuseport_update_incoming_cpu(struct sock *sk, int val); 62 62 63 63 #endif /* _SOCK_REUSEPORT_H */
+1 -1
net/core/sock.c
··· 1436 1436 break; 1437 1437 } 1438 1438 case SO_INCOMING_CPU: 1439 - WRITE_ONCE(sk->sk_incoming_cpu, val); 1439 + reuseport_update_incoming_cpu(sk, val); 1440 1440 break; 1441 1441 1442 1442 case SO_CNX_ADVICE:
+89 -5
net/core/sock_reuseport.c
··· 37 37 } 38 38 EXPORT_SYMBOL(reuseport_has_conns_set); 39 39 40 + static void __reuseport_get_incoming_cpu(struct sock_reuseport *reuse) 41 + { 42 + /* Paired with READ_ONCE() in reuseport_select_sock_by_hash(). */ 43 + WRITE_ONCE(reuse->incoming_cpu, reuse->incoming_cpu + 1); 44 + } 45 + 46 + static void __reuseport_put_incoming_cpu(struct sock_reuseport *reuse) 47 + { 48 + /* Paired with READ_ONCE() in reuseport_select_sock_by_hash(). */ 49 + WRITE_ONCE(reuse->incoming_cpu, reuse->incoming_cpu - 1); 50 + } 51 + 52 + static void reuseport_get_incoming_cpu(struct sock *sk, struct sock_reuseport *reuse) 53 + { 54 + if (sk->sk_incoming_cpu >= 0) 55 + __reuseport_get_incoming_cpu(reuse); 56 + } 57 + 58 + static void reuseport_put_incoming_cpu(struct sock *sk, struct sock_reuseport *reuse) 59 + { 60 + if (sk->sk_incoming_cpu >= 0) 61 + __reuseport_put_incoming_cpu(reuse); 62 + } 63 + 64 + void reuseport_update_incoming_cpu(struct sock *sk, int val) 65 + { 66 + struct sock_reuseport *reuse; 67 + int old_sk_incoming_cpu; 68 + 69 + if (unlikely(!rcu_access_pointer(sk->sk_reuseport_cb))) { 70 + /* Paired with REAE_ONCE() in sk_incoming_cpu_update() 71 + * and compute_score(). 72 + */ 73 + WRITE_ONCE(sk->sk_incoming_cpu, val); 74 + return; 75 + } 76 + 77 + spin_lock_bh(&reuseport_lock); 78 + 79 + /* This must be done under reuseport_lock to avoid a race with 80 + * reuseport_grow(), which accesses sk->sk_incoming_cpu without 81 + * lock_sock() when detaching a shutdown()ed sk. 82 + * 83 + * Paired with READ_ONCE() in reuseport_select_sock_by_hash(). 84 + */ 85 + old_sk_incoming_cpu = sk->sk_incoming_cpu; 86 + WRITE_ONCE(sk->sk_incoming_cpu, val); 87 + 88 + reuse = rcu_dereference_protected(sk->sk_reuseport_cb, 89 + lockdep_is_held(&reuseport_lock)); 90 + 91 + /* reuseport_grow() has detached a closed sk. */ 92 + if (!reuse) 93 + goto out; 94 + 95 + if (old_sk_incoming_cpu < 0 && val >= 0) 96 + __reuseport_get_incoming_cpu(reuse); 97 + else if (old_sk_incoming_cpu >= 0 && val < 0) 98 + __reuseport_put_incoming_cpu(reuse); 99 + 100 + out: 101 + spin_unlock_bh(&reuseport_lock); 102 + } 103 + 40 104 static int reuseport_sock_index(struct sock *sk, 41 105 const struct sock_reuseport *reuse, 42 106 bool closed) ··· 128 64 /* paired with smp_rmb() in reuseport_(select|migrate)_sock() */ 129 65 smp_wmb(); 130 66 reuse->num_socks++; 67 + reuseport_get_incoming_cpu(sk, reuse); 131 68 } 132 69 133 70 static bool __reuseport_detach_sock(struct sock *sk, ··· 141 76 142 77 reuse->socks[i] = reuse->socks[reuse->num_socks - 1]; 143 78 reuse->num_socks--; 79 + reuseport_put_incoming_cpu(sk, reuse); 144 80 145 81 return true; 146 82 } ··· 152 86 reuse->socks[reuse->max_socks - reuse->num_closed_socks - 1] = sk; 153 87 /* paired with READ_ONCE() in inet_csk_bind_conflict() */ 154 88 WRITE_ONCE(reuse->num_closed_socks, reuse->num_closed_socks + 1); 89 + reuseport_get_incoming_cpu(sk, reuse); 155 90 } 156 91 157 92 static bool __reuseport_detach_closed_sock(struct sock *sk, ··· 166 99 reuse->socks[i] = reuse->socks[reuse->max_socks - reuse->num_closed_socks]; 167 100 /* paired with READ_ONCE() in inet_csk_bind_conflict() */ 168 101 WRITE_ONCE(reuse->num_closed_socks, reuse->num_closed_socks - 1); 102 + reuseport_put_incoming_cpu(sk, reuse); 169 103 170 104 return true; 171 105 } ··· 234 166 reuse->bind_inany = bind_inany; 235 167 reuse->socks[0] = sk; 236 168 reuse->num_socks = 1; 169 + reuseport_get_incoming_cpu(sk, reuse); 237 170 rcu_assign_pointer(sk->sk_reuseport_cb, reuse); 238 171 239 172 out: ··· 278 209 more_reuse->reuseport_id = reuse->reuseport_id; 279 210 more_reuse->bind_inany = reuse->bind_inany; 280 211 more_reuse->has_conns = reuse->has_conns; 212 + more_reuse->incoming_cpu = reuse->incoming_cpu; 281 213 282 214 memcpy(more_reuse->socks, reuse->socks, 283 215 reuse->num_socks * sizeof(struct sock *)); ··· 528 458 static struct sock *reuseport_select_sock_by_hash(struct sock_reuseport *reuse, 529 459 u32 hash, u16 num_socks) 530 460 { 461 + struct sock *first_valid_sk = NULL; 531 462 int i, j; 532 463 533 464 i = j = reciprocal_scale(hash, num_socks); 534 - while (reuse->socks[i]->sk_state == TCP_ESTABLISHED) { 465 + do { 466 + struct sock *sk = reuse->socks[i]; 467 + 468 + if (sk->sk_state != TCP_ESTABLISHED) { 469 + /* Paired with WRITE_ONCE() in __reuseport_(get|put)_incoming_cpu(). */ 470 + if (!READ_ONCE(reuse->incoming_cpu)) 471 + return sk; 472 + 473 + /* Paired with WRITE_ONCE() in reuseport_update_incoming_cpu(). */ 474 + if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id()) 475 + return sk; 476 + 477 + if (!first_valid_sk) 478 + first_valid_sk = sk; 479 + } 480 + 535 481 i++; 536 482 if (i >= num_socks) 537 483 i = 0; 538 - if (i == j) 539 - return NULL; 540 - } 484 + } while (i != j); 541 485 542 - return reuse->socks[i]; 486 + return first_valid_sk; 543 487 } 544 488 545 489 /**