Skip to content

Commit 67abbd0

Browse files
zx2c4syphyr
authored andcommitted
wireguard: queueing: use saner cpu selection wrapping
commit 7387943fa35516f6f8017a3b0e9ce48a3bef9faa upstream. Using `% nr_cpumask_bits` is slow and complicated, and not totally robust toward dynamic changes to CPU topologies. Rather than storing the next CPU in the round-robin, just store the last one, and also return that value. This simplifies the loop drastically into a much more common pattern. Fixes: e7096c131e51 ("net: WireGuard secure network tunnel") Cc: stable@vger.kernel.org Reported-by: Linus Torvalds <torvalds@linux-foundation.org> Tested-by: Manuel Leiner <manuel.leiner@gmx.de> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4981b71 commit 67abbd0

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

src/queueing.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int wg_packet_queue_init(struct crypt_queue *queue, work_func_t function,
2828
int ret;
2929

3030
memset(queue, 0, sizeof(*queue));
31+
queue->last_cpu = -1;
3132
ret = ptr_ring_init(&queue->ring, len, GFP_KERNEL);
3233
if (ret)
3334
return ret;

src/queueing.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,17 @@ static inline int wg_cpumask_choose_online(int *stored_cpu, unsigned int id)
122122
return cpu;
123123
}
124124

125-
/* This function is racy, in the sense that next is unlocked, so it could return
126-
* the same CPU twice. A race-free version of this would be to instead store an
127-
* atomic sequence number, do an increment-and-return, and then iterate through
128-
* every possible CPU until we get to that index -- choose_cpu. However that's
129-
* a bit slower, and it doesn't seem like this potential race actually
130-
* introduces any performance loss, so we live with it.
125+
/* This function is racy, in the sense that it's called while last_cpu is
126+
* unlocked, so it could return the same CPU twice. Adding locking or using
127+
* atomic sequence numbers is slower though, and the consequences of racing are
128+
* harmless, so live with it.
131129
*/
132-
static inline int wg_cpumask_next_online(int *next)
130+
static inline int wg_cpumask_next_online(int *last_cpu)
133131
{
134-
int cpu = *next;
135-
136-
while (unlikely(!cpumask_test_cpu(cpu, cpu_online_mask)))
137-
cpu = cpumask_next(cpu, cpu_online_mask) % nr_cpumask_bits;
138-
*next = cpumask_next(cpu, cpu_online_mask) % nr_cpumask_bits;
132+
int cpu = cpumask_next(*last_cpu, cpu_online_mask);
133+
if (cpu >= nr_cpu_ids)
134+
cpu = cpumask_first(cpu_online_mask);
135+
*last_cpu = cpu;
139136
return cpu;
140137
}
141138

@@ -164,7 +161,7 @@ static inline void wg_prev_queue_drop_peeked(struct prev_queue *queue)
164161

165162
static inline int wg_queue_enqueue_per_device_and_peer(
166163
struct crypt_queue *device_queue, struct prev_queue *peer_queue,
167-
struct sk_buff *skb, struct workqueue_struct *wq, int *next_cpu)
164+
struct sk_buff *skb, struct workqueue_struct *wq)
168165
{
169166
int cpu;
170167

@@ -178,7 +175,7 @@ static inline int wg_queue_enqueue_per_device_and_peer(
178175
/* Then we queue it up in the device queue, which consumes the
179176
* packet as soon as it can.
180177
*/
181-
cpu = wg_cpumask_next_online(next_cpu);
178+
cpu = wg_cpumask_next_online(&device_queue->last_cpu);
182179
if (unlikely(ptr_ring_produce_bh(&device_queue->ring, skb)))
183180
return -EPIPE;
184181
queue_work_on(cpu, wq, &per_cpu_ptr(device_queue->worker, cpu)->work);

src/receive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ static void wg_packet_consume_data(struct wg_device *wg, struct sk_buff *skb)
540540
goto err;
541541

542542
ret = wg_queue_enqueue_per_device_and_peer(&wg->decrypt_queue, &peer->rx_queue, skb,
543-
wg->packet_crypt_wq, &wg->decrypt_queue.last_cpu);
543+
wg->packet_crypt_wq);
544544
if (unlikely(ret == -EPIPE))
545545
wg_queue_enqueue_per_peer_rx(skb, PACKET_STATE_DEAD);
546546
if (likely(!ret || ret == -EPIPE)) {

src/send.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static void wg_packet_create_data(struct wg_peer *peer, struct sk_buff *first)
325325
goto err;
326326

327327
ret = wg_queue_enqueue_per_device_and_peer(&wg->encrypt_queue, &peer->tx_queue, first,
328-
wg->packet_crypt_wq, &wg->encrypt_queue.last_cpu);
328+
wg->packet_crypt_wq);
329329
if (unlikely(ret == -EPIPE))
330330
wg_queue_enqueue_per_peer_tx(first, PACKET_STATE_DEAD);
331331
err:

0 commit comments

Comments
 (0)