Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / wireguard / queueing.c
blob71b8e80b58e121d2cb46882bac905522c205146f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 */
6 #include "queueing.h"
8 struct multicore_worker __percpu *
9 wg_packet_percpu_multicore_worker_alloc(work_func_t function, void *ptr)
11 int cpu;
12 struct multicore_worker __percpu *worker =
13 alloc_percpu(struct multicore_worker);
15 if (!worker)
16 return NULL;
18 for_each_possible_cpu(cpu) {
19 per_cpu_ptr(worker, cpu)->ptr = ptr;
20 INIT_WORK(&per_cpu_ptr(worker, cpu)->work, function);
22 return worker;
25 int wg_packet_queue_init(struct crypt_queue *queue, work_func_t function,
26 bool multicore, unsigned int len)
28 int ret;
30 memset(queue, 0, sizeof(*queue));
31 ret = ptr_ring_init(&queue->ring, len, GFP_KERNEL);
32 if (ret)
33 return ret;
34 if (function) {
35 if (multicore) {
36 queue->worker = wg_packet_percpu_multicore_worker_alloc(
37 function, queue);
38 if (!queue->worker) {
39 ptr_ring_cleanup(&queue->ring, NULL);
40 return -ENOMEM;
42 } else {
43 INIT_WORK(&queue->work, function);
46 return 0;
49 void wg_packet_queue_free(struct crypt_queue *queue, bool multicore)
51 if (multicore)
52 free_percpu(queue->worker);
53 WARN_ON(!__ptr_ring_empty(&queue->ring));
54 ptr_ring_cleanup(&queue->ring, NULL);