d8ea8908b9e36f59239ca14447a08288242a23e6
[openwrt/staging/mans0n.git] /
1 From 8df862b663b026d61b4c463caece77f1f127771f Mon Sep 17 00:00:00 2001
2 From: "Jason A. Donenfeld" <Jason@zx2c4.com>
3 Date: Wed, 6 May 2020 15:33:06 -0600
4 Subject: [PATCH 102/124] wireguard: send/receive: use explicit unlikely branch
5 instead of implicit coalescing
6
7 commit 243f2148937adc72bcaaa590d482d599c936efde upstream.
8
9 It's very unlikely that send will become true. It's nearly always false
10 between 0 and 120 seconds of a session, and in most cases becomes true
11 only between 120 and 121 seconds before becoming false again. So,
12 unlikely(send) is clearly the right option here.
13
14 What happened before was that we had this complex boolean expression
15 with multiple likely and unlikely clauses nested. Since this is
16 evaluated left-to-right anyway, the whole thing got converted to
17 unlikely. So, we can clean this up to better represent what's going on.
18
19 The generated code is the same.
20
21 Suggested-by: Sultan Alsawaf <sultan@kerneltoast.com>
22 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
23 Signed-off-by: David S. Miller <davem@davemloft.net>
24 Signed-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