1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <crypto/hash.h>
5 #include <linux/kref.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
8 #include <linux/percpu.h>
9 #include <linux/workqueue.h>
12 static size_t __scratch_size;
13 struct sigpool_scratch {
18 static DEFINE_PER_CPU(struct sigpool_scratch, sigpool_scratch) = {
19 .bh_lock = INIT_LOCAL_LOCK(bh_lock),
22 struct sigpool_entry {
23 struct crypto_ahash *hash;
30 #define CPOOL_SIZE (PAGE_SIZE / sizeof(struct sigpool_entry))
31 static struct sigpool_entry cpool[CPOOL_SIZE];
32 static unsigned int cpool_populated;
33 static DEFINE_MUTEX(cpool_mutex);
36 struct scratches_to_free {
42 static void free_old_scratches(struct rcu_head *head)
44 struct scratches_to_free *stf;
46 stf = container_of(head, struct scratches_to_free, rcu);
48 kfree(stf->scratches[stf->cnt]);
53 * sigpool_reserve_scratch - re-allocates scratch buffer, slow-path
54 * @size: request size for the scratch/temp buffer
56 static int sigpool_reserve_scratch(size_t size)
58 struct scratches_to_free *stf;
59 size_t stf_sz = struct_size(stf, scratches, num_possible_cpus());
62 lockdep_assert_held(&cpool_mutex);
63 if (__scratch_size >= size)
66 stf = kmalloc(stf_sz, GFP_KERNEL);
71 size = max(size, __scratch_size);
73 for_each_possible_cpu(cpu) {
74 void *scratch, *old_scratch;
76 scratch = kmalloc_node(size, GFP_KERNEL, cpu_to_node(cpu));
82 old_scratch = rcu_replace_pointer(per_cpu(sigpool_scratch.pad, cpu),
83 scratch, lockdep_is_held(&cpool_mutex));
84 if (!cpu_online(cpu) || !old_scratch) {
88 stf->scratches[stf->cnt++] = old_scratch;
92 __scratch_size = size;
94 call_rcu(&stf->rcu, free_old_scratches);
98 static void sigpool_scratch_free(void)
102 for_each_possible_cpu(cpu)
103 kfree(rcu_replace_pointer(per_cpu(sigpool_scratch.pad, cpu),
104 NULL, lockdep_is_held(&cpool_mutex)));
108 static int __cpool_try_clone(struct crypto_ahash *hash)
110 struct crypto_ahash *tmp;
112 tmp = crypto_clone_ahash(hash);
116 crypto_free_ahash(tmp);
120 static int __cpool_alloc_ahash(struct sigpool_entry *e, const char *alg)
122 struct crypto_ahash *cpu0_hash;
125 e->alg = kstrdup(alg, GFP_KERNEL);
129 cpu0_hash = crypto_alloc_ahash(alg, 0, CRYPTO_ALG_ASYNC);
130 if (IS_ERR(cpu0_hash)) {
131 ret = PTR_ERR(cpu0_hash);
135 e->needs_key = crypto_ahash_get_flags(cpu0_hash) & CRYPTO_TFM_NEED_KEY;
137 ret = __cpool_try_clone(cpu0_hash);
139 goto out_free_cpu0_hash;
145 crypto_free_ahash(cpu0_hash);
153 * tcp_sigpool_alloc_ahash - allocates pool for ahash requests
154 * @alg: name of async hash algorithm
155 * @scratch_size: reserve a tcp_sigpool::scratch buffer of this size
157 int tcp_sigpool_alloc_ahash(const char *alg, size_t scratch_size)
162 mutex_lock(&cpool_mutex);
163 ret = sigpool_reserve_scratch(scratch_size);
166 for (i = 0; i < cpool_populated; i++) {
169 if (strcmp(cpool[i].alg, alg))
172 /* pairs with tcp_sigpool_release() */
173 if (!kref_get_unless_zero(&cpool[i].kref))
174 kref_init(&cpool[i].kref);
179 for (i = 0; i < cpool_populated; i++) {
183 if (i >= CPOOL_SIZE) {
188 ret = __cpool_alloc_ahash(&cpool[i], alg);
191 if (i == cpool_populated)
195 mutex_unlock(&cpool_mutex);
198 EXPORT_SYMBOL_GPL(tcp_sigpool_alloc_ahash);
200 static void __cpool_free_entry(struct sigpool_entry *e)
202 crypto_free_ahash(e->hash);
204 memset(e, 0, sizeof(*e));
207 static void cpool_cleanup_work_cb(struct work_struct *work)
209 bool free_scratch = true;
212 mutex_lock(&cpool_mutex);
213 for (i = 0; i < cpool_populated; i++) {
214 if (kref_read(&cpool[i].kref) > 0) {
215 free_scratch = false;
220 __cpool_free_entry(&cpool[i]);
223 sigpool_scratch_free();
224 mutex_unlock(&cpool_mutex);
227 static DECLARE_WORK(cpool_cleanup_work, cpool_cleanup_work_cb);
228 static void cpool_schedule_cleanup(struct kref *kref)
230 schedule_work(&cpool_cleanup_work);
234 * tcp_sigpool_release - decreases number of users for a pool. If it was
235 * the last user of the pool, releases any memory that was consumed.
236 * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
238 void tcp_sigpool_release(unsigned int id)
240 if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
244 kref_put(&cpool[id].kref, cpool_schedule_cleanup);
246 EXPORT_SYMBOL_GPL(tcp_sigpool_release);
249 * tcp_sigpool_get - increases number of users (refcounter) for a pool
250 * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
252 void tcp_sigpool_get(unsigned int id)
254 if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
256 kref_get(&cpool[id].kref);
258 EXPORT_SYMBOL_GPL(tcp_sigpool_get);
260 int tcp_sigpool_start(unsigned int id, struct tcp_sigpool *c) __cond_acquires(RCU_BH)
262 struct crypto_ahash *hash;
265 if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg)) {
266 rcu_read_unlock_bh();
270 hash = crypto_clone_ahash(cpool[id].hash);
272 rcu_read_unlock_bh();
273 return PTR_ERR(hash);
276 c->req = ahash_request_alloc(hash, GFP_ATOMIC);
278 crypto_free_ahash(hash);
279 rcu_read_unlock_bh();
282 ahash_request_set_callback(c->req, 0, NULL, NULL);
284 /* Pairs with tcp_sigpool_reserve_scratch(), scratch area is
285 * valid (allocated) until tcp_sigpool_end().
287 local_lock_nested_bh(&sigpool_scratch.bh_lock);
288 c->scratch = rcu_dereference_bh(*this_cpu_ptr(&sigpool_scratch.pad));
291 EXPORT_SYMBOL_GPL(tcp_sigpool_start);
293 void tcp_sigpool_end(struct tcp_sigpool *c) __releases(RCU_BH)
295 struct crypto_ahash *hash = crypto_ahash_reqtfm(c->req);
297 local_unlock_nested_bh(&sigpool_scratch.bh_lock);
298 rcu_read_unlock_bh();
299 ahash_request_free(c->req);
300 crypto_free_ahash(hash);
302 EXPORT_SYMBOL_GPL(tcp_sigpool_end);
305 * tcp_sigpool_algo - return algorithm of tcp_sigpool
306 * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
307 * @buf: buffer to return name of algorithm
308 * @buf_len: size of @buf
310 size_t tcp_sigpool_algo(unsigned int id, char *buf, size_t buf_len)
312 if (WARN_ON_ONCE(id >= cpool_populated || !cpool[id].alg))
315 return strscpy(buf, cpool[id].alg, buf_len);
317 EXPORT_SYMBOL_GPL(tcp_sigpool_algo);
320 * tcp_sigpool_hash_skb_data - hash data in skb with initialized tcp_sigpool
321 * @hp: tcp_sigpool pointer
322 * @skb: buffer to add sign for
323 * @header_len: TCP header length for this segment
325 int tcp_sigpool_hash_skb_data(struct tcp_sigpool *hp,
326 const struct sk_buff *skb,
327 unsigned int header_len)
329 const unsigned int head_data_len = skb_headlen(skb) > header_len ?
330 skb_headlen(skb) - header_len : 0;
331 const struct skb_shared_info *shi = skb_shinfo(skb);
332 const struct tcphdr *tp = tcp_hdr(skb);
333 struct ahash_request *req = hp->req;
334 struct sk_buff *frag_iter;
335 struct scatterlist sg;
338 sg_init_table(&sg, 1);
340 sg_set_buf(&sg, ((u8 *)tp) + header_len, head_data_len);
341 ahash_request_set_crypt(req, &sg, NULL, head_data_len);
342 if (crypto_ahash_update(req))
345 for (i = 0; i < shi->nr_frags; ++i) {
346 const skb_frag_t *f = &shi->frags[i];
347 unsigned int offset = skb_frag_off(f);
350 page = skb_frag_page(f) + (offset >> PAGE_SHIFT);
351 sg_set_page(&sg, page, skb_frag_size(f), offset_in_page(offset));
352 ahash_request_set_crypt(req, &sg, NULL, skb_frag_size(f));
353 if (crypto_ahash_update(req))
357 skb_walk_frags(skb, frag_iter)
358 if (tcp_sigpool_hash_skb_data(hp, frag_iter, 0))
363 EXPORT_SYMBOL(tcp_sigpool_hash_skb_data);
365 MODULE_LICENSE("GPL");
366 MODULE_DESCRIPTION("Per-CPU pool of crypto requests");