blob: 900e2f2350e33d6430a5d5dea37060eef90e24ea [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: "Jason A. Donenfeld" <Jason@zx2c4.com>
3Date: Wed, 6 May 2020 15:33:06 -0600
4Subject: [PATCH] wireguard: send/receive: use explicit unlikely branch instead
5 of implicit coalescing
6
7commit 243f2148937adc72bcaaa590d482d599c936efde upstream.
8
9It's very unlikely that send will become true. It's nearly always false
10between 0 and 120 seconds of a session, and in most cases becomes true
11only between 120 and 121 seconds before becoming false again. So,
12unlikely(send) is clearly the right option here.
13
14What happened before was that we had this complex boolean expression
15with multiple likely and unlikely clauses nested. Since this is
16evaluated left-to-right anyway, the whole thing got converted to
17unlikely. So, we can clean this up to better represent what's going on.
18
19The generated code is the same.
20
21Suggested-by: Sultan Alsawaf <sultan@kerneltoast.com>
22Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
23Signed-off-by: David S. Miller <davem@davemloft.net>
24Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
25---
26 drivers/net/wireguard/receive.c | 13 ++++++-------
27 drivers/net/wireguard/send.c | 15 ++++++---------
28 2 files changed, 12 insertions(+), 16 deletions(-)
29
30--- a/drivers/net/wireguard/receive.c
31+++ b/drivers/net/wireguard/receive.c
32@@ -226,21 +226,20 @@ void wg_packet_handshake_receive_worker(
33 static void keep_key_fresh(struct wg_peer *peer)
34 {
35 struct noise_keypair *keypair;
36- bool send = false;
37+ bool send;
38
39 if (peer->sent_lastminute_handshake)
40 return;
41
42 rcu_read_lock_bh();
43 keypair = rcu_dereference_bh(peer->keypairs.current_keypair);
44- if (likely(keypair && READ_ONCE(keypair->sending.is_valid)) &&
45- keypair->i_am_the_initiator &&
46- unlikely(wg_birthdate_has_expired(keypair->sending.birthdate,
47- REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT)))
48- send = true;
49+ send = keypair && READ_ONCE(keypair->sending.is_valid) &&
50+ keypair->i_am_the_initiator &&
51+ wg_birthdate_has_expired(keypair->sending.birthdate,
52+ REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT);
53 rcu_read_unlock_bh();
54
55- if (send) {
56+ if (unlikely(send)) {
57 peer->sent_lastminute_handshake = true;
58 wg_packet_send_queued_handshake_initiation(peer, false);
59 }
60--- a/drivers/net/wireguard/send.c
61+++ b/drivers/net/wireguard/send.c
62@@ -124,20 +124,17 @@ void wg_packet_send_handshake_cookie(str
63 static void keep_key_fresh(struct wg_peer *peer)
64 {
65 struct noise_keypair *keypair;
66- bool send = false;
67+ bool send;
68
69 rcu_read_lock_bh();
70 keypair = rcu_dereference_bh(peer->keypairs.current_keypair);
71- if (likely(keypair && READ_ONCE(keypair->sending.is_valid)) &&
72- (unlikely(atomic64_read(&keypair->sending.counter.counter) >
73- REKEY_AFTER_MESSAGES) ||
74- (keypair->i_am_the_initiator &&
75- unlikely(wg_birthdate_has_expired(keypair->sending.birthdate,
76- REKEY_AFTER_TIME)))))
77- send = true;
78+ send = keypair && READ_ONCE(keypair->sending.is_valid) &&
79+ (atomic64_read(&keypair->sending.counter.counter) > REKEY_AFTER_MESSAGES ||
80+ (keypair->i_am_the_initiator &&
81+ wg_birthdate_has_expired(keypair->sending.birthdate, REKEY_AFTER_TIME)));
82 rcu_read_unlock_bh();
83
84- if (send)
85+ if (unlikely(send))
86 wg_packet_send_queued_handshake_initiation(peer, false);
87 }
88