Skip to content

Commit 243f214

Browse files
zx2c4davem330
authored andcommitted
wireguard: send/receive: use explicit unlikely branch instead of implicit coalescing
It's very unlikely that send will become true. It's nearly always false between 0 and 120 seconds of a session, and in most cases becomes true only between 120 and 121 seconds before becoming false again. So, unlikely(send) is clearly the right option here. What happened before was that we had this complex boolean expression with multiple likely and unlikely clauses nested. Since this is evaluated left-to-right anyway, the whole thing got converted to unlikely. So, we can clean this up to better represent what's going on. The generated code is the same. Suggested-by: Sultan Alsawaf <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4fed818 commit 243f214

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

drivers/net/wireguard/receive.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,20 @@ void wg_packet_handshake_receive_worker(struct work_struct *work)
226226
static void keep_key_fresh(struct wg_peer *peer)
227227
{
228228
struct noise_keypair *keypair;
229-
bool send = false;
229+
bool send;
230230

231231
if (peer->sent_lastminute_handshake)
232232
return;
233233

234234
rcu_read_lock_bh();
235235
keypair = rcu_dereference_bh(peer->keypairs.current_keypair);
236-
if (likely(keypair && READ_ONCE(keypair->sending.is_valid)) &&
237-
keypair->i_am_the_initiator &&
238-
unlikely(wg_birthdate_has_expired(keypair->sending.birthdate,
239-
REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT)))
240-
send = true;
236+
send = keypair && READ_ONCE(keypair->sending.is_valid) &&
237+
keypair->i_am_the_initiator &&
238+
wg_birthdate_has_expired(keypair->sending.birthdate,
239+
REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT);
241240
rcu_read_unlock_bh();
242241

243-
if (send) {
242+
if (unlikely(send)) {
244243
peer->sent_lastminute_handshake = true;
245244
wg_packet_send_queued_handshake_initiation(peer, false);
246245
}

drivers/net/wireguard/send.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,17 @@ void wg_packet_send_handshake_cookie(struct wg_device *wg,
124124
static void keep_key_fresh(struct wg_peer *peer)
125125
{
126126
struct noise_keypair *keypair;
127-
bool send = false;
127+
bool send;
128128

129129
rcu_read_lock_bh();
130130
keypair = rcu_dereference_bh(peer->keypairs.current_keypair);
131-
if (likely(keypair && READ_ONCE(keypair->sending.is_valid)) &&
132-
(unlikely(atomic64_read(&keypair->sending.counter.counter) >
133-
REKEY_AFTER_MESSAGES) ||
134-
(keypair->i_am_the_initiator &&
135-
unlikely(wg_birthdate_has_expired(keypair->sending.birthdate,
136-
REKEY_AFTER_TIME)))))
137-
send = true;
131+
send = keypair && READ_ONCE(keypair->sending.is_valid) &&
132+
(atomic64_read(&keypair->sending.counter.counter) > REKEY_AFTER_MESSAGES ||
133+
(keypair->i_am_the_initiator &&
134+
wg_birthdate_has_expired(keypair->sending.birthdate, REKEY_AFTER_TIME)));
138135
rcu_read_unlock_bh();
139136

140-
if (send)
137+
if (unlikely(send))
141138
wg_packet_send_queued_handshake_initiation(peer, false);
142139
}
143140

0 commit comments

Comments
 (0)