xref: /openbmc/linux/drivers/net/wireguard/queueing.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5 
6 #include "queueing.h"
7 
8 struct multicore_worker __percpu *
9 wg_packet_percpu_multicore_worker_alloc(work_func_t function, void *ptr)
10 {
11 	int cpu;
12 	struct multicore_worker __percpu *worker =
13 		alloc_percpu(struct multicore_worker);
14 
15 	if (!worker)
16 		return NULL;
17 
18 	for_each_possible_cpu(cpu) {
19 		per_cpu_ptr(worker, cpu)->ptr = ptr;
20 		INIT_WORK(&per_cpu_ptr(worker, cpu)->work, function);
21 	}
22 	return worker;
23 }
24 
25 int wg_packet_queue_init(struct crypt_queue *queue, work_func_t function,
26 			 bool multicore, unsigned int len)
27 {
28 	int ret;
29 
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 				return -ENOMEM;
40 		} else {
41 			INIT_WORK(&queue->work, function);
42 		}
43 	}
44 	return 0;
45 }
46 
47 void wg_packet_queue_free(struct crypt_queue *queue, bool multicore)
48 {
49 	if (multicore)
50 		free_percpu(queue->worker);
51 	WARN_ON(!__ptr_ring_empty(&queue->ring));
52 	ptr_ring_cleanup(&queue->ring, NULL);
53 }
54