Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Synchronous Compression operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2015 LG Electronics Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/cryptouser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <crypto/internal/acompress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <crypto/internal/scompress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct scomp_scratch {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	spinlock_t	lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	void		*src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	void		*dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	.lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static const struct crypto_type crypto_scomp_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static int scomp_scratch_users;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static DEFINE_MUTEX(scomp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct crypto_report_comp rscomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	memset(&rscomp, 0, sizeof(rscomp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		       sizeof(rscomp), &rscomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int crypto_scomp_report(struct sk_buff *skb, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	__maybe_unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	seq_puts(m, "type         : scomp\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static void crypto_scomp_free_scratches(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct scomp_scratch *scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		scratch = per_cpu_ptr(&scomp_scratch, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		vfree(scratch->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		vfree(scratch->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		scratch->src = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		scratch->dst = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static int crypto_scomp_alloc_scratches(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct scomp_scratch *scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		void *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		scratch = per_cpu_ptr(&scomp_scratch, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		if (!mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		scratch->src = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		if (!mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		scratch->dst = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	crypto_scomp_free_scratches();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	mutex_lock(&scomp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (!scomp_scratch_users++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		ret = crypto_scomp_alloc_scratches();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	mutex_unlock(&scomp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	struct crypto_acomp *tfm = crypto_acomp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	void **tfm_ctx = acomp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	struct crypto_scomp *scomp = *tfm_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	void **ctx = acomp_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct scomp_scratch *scratch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (req->dst && !req->dlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		req->dlen = SCOMP_SCRATCH_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	scratch = raw_cpu_ptr(&scomp_scratch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	spin_lock(&scratch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	scatterwalk_map_and_copy(scratch->src, req->src, 0, req->slen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		ret = crypto_scomp_compress(scomp, scratch->src, req->slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 					    scratch->dst, &req->dlen, *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		ret = crypto_scomp_decompress(scomp, scratch->src, req->slen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 					      scratch->dst, &req->dlen, *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (!req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			if (!req->dst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		scatterwalk_map_and_copy(scratch->dst, req->dst, 0, req->dlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 					 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	spin_unlock(&scratch->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int scomp_acomp_compress(struct acomp_req *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return scomp_acomp_comp_decomp(req, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static int scomp_acomp_decompress(struct acomp_req *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return scomp_acomp_comp_decomp(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	crypto_free_scomp(*ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	mutex_lock(&scomp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (!--scomp_scratch_users)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		crypto_scomp_free_scratches();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	mutex_unlock(&scomp_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct crypto_alg *calg = tfm->__crt_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct crypto_acomp *crt = __crypto_acomp_tfm(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct crypto_scomp *scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (!crypto_mod_get(calg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	scomp = crypto_create_tfm(calg, &crypto_scomp_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (IS_ERR(scomp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		crypto_mod_put(calg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return PTR_ERR(scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	*ctx = scomp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	tfm->exit = crypto_exit_scomp_ops_async;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	crt->compress = scomp_acomp_compress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	crt->decompress = scomp_acomp_decompress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	crt->dst_free = sgl_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	crt->reqsize = sizeof(void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct crypto_scomp *scomp = *tfm_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	void *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	ctx = crypto_scomp_alloc_ctx(scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (IS_ERR(ctx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	*req->__ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) void crypto_acomp_scomp_free_ctx(struct acomp_req *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct crypto_acomp *acomp = crypto_acomp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	struct crypto_tfm *tfm = crypto_acomp_tfm(acomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct crypto_scomp **tfm_ctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct crypto_scomp *scomp = *tfm_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	void *ctx = *req->__ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		crypto_scomp_free_ctx(scomp, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static const struct crypto_type crypto_scomp_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	.extsize = crypto_alg_extsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	.init_tfm = crypto_scomp_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	.show = crypto_scomp_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	.report = crypto_scomp_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	.maskset = CRYPTO_ALG_TYPE_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	.type = CRYPTO_ALG_TYPE_SCOMPRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	.tfmsize = offsetof(struct crypto_scomp, base),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int crypto_register_scomp(struct scomp_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	struct crypto_alg *base = &alg->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	base->cra_type = &crypto_scomp_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	return crypto_register_alg(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) EXPORT_SYMBOL_GPL(crypto_register_scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) void crypto_unregister_scomp(struct scomp_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	crypto_unregister_alg(&alg->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int crypto_register_scomps(struct scomp_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		ret = crypto_register_scomp(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	for (--i; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		crypto_unregister_scomp(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) EXPORT_SYMBOL_GPL(crypto_register_scomps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) void crypto_unregister_scomps(struct scomp_alg *algs, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	for (i = count - 1; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		crypto_unregister_scomp(&algs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) EXPORT_SYMBOL_GPL(crypto_unregister_scomps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) MODULE_DESCRIPTION("Synchronous compression type");