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 ptr_ring_cleanup(&queue->ring, NULL); 40 return -ENOMEM; 41 } 42 } else { 43 INIT_WORK(&queue->work, function); 44 } 45 } 46 return 0; 47 } 48 49 void wg_packet_queue_free(struct crypt_queue *queue, bool multicore) 50 { 51 if (multicore) 52 free_percpu(queue->worker); 53 WARN_ON(!__ptr_ring_empty(&queue->ring)); 54 ptr_ring_cleanup(&queue->ring, NULL); 55 } 56