Skip to content

Commit 71052dc

Browse files
Sebastian Andrzej Siewiorherbertx
authored andcommitted
crypto: scompress - Use per-CPU struct instead multiple variables
Two per-CPU variables are allocated as pointer to per-CPU memory which then are used as scratch buffers. We could be smart about this and use instead a per-CPU struct which contains the pointers already and then we need to allocate just the scratch buffers. Add a lock to the struct. By doing so we can avoid the get_cpu() statement and gain lockdep coverage (if enabled) to ensure that the lock is always acquired in the right context. On non-preemptible kernels the lock vanishes. It is okay to use raw_cpu_ptr() in order to get a pointer to the struct since it is protected by the spinlock. The diffstat of this is negative and according to size scompress.o: text data bss dec hex filename 1847 160 24 2031 7ef dbg_before.o 1754 232 4 1990 7c6 dbg_after.o 1799 64 24 1887 75f no_dbg-before.o 1703 88 4 1795 703 no_dbg-after.o The overall size increase difference is also negative. The increase in the data section is only four bytes without lockdep. Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 6a4d1b1 commit 71052dc

1 file changed

Lines changed: 54 additions & 71 deletions

File tree

‎crypto/scompress.c‎

Lines changed: 54 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@
2929
#include <crypto/internal/scompress.h>
3030
#include "internal.h"
3131

32+
struct scomp_scratch {
33+
spinlock_t lock;
34+
void *src;
35+
void *dst;
36+
};
37+
38+
static DEFINE_PER_CPU(struct scomp_scratch, scomp_scratch) = {
39+
.lock = __SPIN_LOCK_UNLOCKED(scomp_scratch.lock),
40+
};
41+
3242
static const struct crypto_type crypto_scomp_type;
33-
static void * __percpu *scomp_src_scratches;
34-
static void * __percpu *scomp_dst_scratches;
3543
static int scomp_scratch_users;
3644
static DEFINE_MUTEX(scomp_lock);
3745

@@ -62,76 +70,53 @@ static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
6270
seq_puts(m, "type : scomp\n");
6371
}
6472

65-
static void crypto_scomp_free_scratches(void * __percpu *scratches)
73+
static void crypto_scomp_free_scratches(void)
6674
{
75+
struct scomp_scratch *scratch;
6776
int i;
6877

69-
if (!scratches)
70-
return;
71-
72-
for_each_possible_cpu(i)
73-
vfree(*per_cpu_ptr(scratches, i));
78+
for_each_possible_cpu(i) {
79+
scratch = raw_cpu_ptr(&scomp_scratch);
7480

75-
free_percpu(scratches);
81+
vfree(scratch->src);
82+
vfree(scratch->dst);
83+
scratch->src = NULL;
84+
scratch->dst = NULL;
85+
}
7686
}
7787

78-
static void * __percpu *crypto_scomp_alloc_scratches(void)
88+
static int crypto_scomp_alloc_scratches(void)
7989
{
80-
void * __percpu *scratches;
90+
struct scomp_scratch *scratch;
8191
int i;
8292

83-
scratches = alloc_percpu(void *);
84-
if (!scratches)
85-
return NULL;
86-
8793
for_each_possible_cpu(i) {
88-
void *scratch;
89-
90-
scratch = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
91-
if (!scratch)
92-
goto error;
93-
*per_cpu_ptr(scratches, i) = scratch;
94-
}
95-
96-
return scratches;
97-
98-
error:
99-
crypto_scomp_free_scratches(scratches);
100-
return NULL;
101-
}
94+
void *mem;
10295

103-
static void crypto_scomp_free_all_scratches(void)
104-
{
105-
if (!--scomp_scratch_users) {
106-
crypto_scomp_free_scratches(scomp_src_scratches);
107-
crypto_scomp_free_scratches(scomp_dst_scratches);
108-
scomp_src_scratches = NULL;
109-
scomp_dst_scratches = NULL;
110-
}
111-
}
96+
scratch = raw_cpu_ptr(&scomp_scratch);
11297

113-
static int crypto_scomp_alloc_all_scratches(void)
114-
{
115-
if (!scomp_scratch_users++) {
116-
scomp_src_scratches = crypto_scomp_alloc_scratches();
117-
if (!scomp_src_scratches)
118-
return -ENOMEM;
119-
scomp_dst_scratches = crypto_scomp_alloc_scratches();
120-
if (!scomp_dst_scratches) {
121-
crypto_scomp_free_scratches(scomp_src_scratches);
122-
scomp_src_scratches = NULL;
123-
return -ENOMEM;
124-
}
98+
mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
99+
if (!mem)
100+
goto error;
101+
scratch->src = mem;
102+
mem = vmalloc_node(SCOMP_SCRATCH_SIZE, cpu_to_node(i));
103+
if (!mem)
104+
goto error;
105+
scratch->dst = mem;
125106
}
126107
return 0;
108+
error:
109+
crypto_scomp_free_scratches();
110+
return -ENOMEM;
127111
}
128112

129113
static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
130114
{
131-
int ret;
115+
int ret = 0;
132116

133117
mutex_lock(&scomp_lock);
134-
ret = crypto_scomp_alloc_all_scratches();
118+
if (!scomp_scratch_users++)
119+
ret = crypto_scomp_alloc_scratches();
135120
mutex_unlock(&scomp_lock);
136121

137122
return ret;
@@ -143,31 +128,28 @@ static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
143128
void **tfm_ctx = acomp_tfm_ctx(tfm);
144129
struct crypto_scomp *scomp = *tfm_ctx;
145130
void **ctx = acomp_request_ctx(req);
146-
const int cpu = get_cpu();
147-
u8 *scratch_src = *per_cpu_ptr(scomp_src_scratches, cpu);
148-
u8 *scratch_dst = *per_cpu_ptr(scomp_dst_scratches, cpu);
131+
struct scomp_scratch *scratch;
149132
int ret;
150133

151-
if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE) {
152-
ret = -EINVAL;
153-
goto out;
154-
}
134+
if (!req->src || !req->slen || req->slen > SCOMP_SCRATCH_SIZE)
135+
return -EINVAL;
155136

156-
if (req->dst && !req->dlen) {
157-
ret = -EINVAL;
158-
goto out;
159-
}
137+
if (req->dst && !req->dlen)
138+
return -EINVAL;
160139

161140
if (!req->dlen || req->dlen > SCOMP_SCRATCH_SIZE)
162141
req->dlen = SCOMP_SCRATCH_SIZE;
163142

164-
scatterwalk_map_and_copy(scratch_src, req->src, 0, req->slen, 0);
143+
scratch = raw_cpu_ptr(&scomp_scratch);
144+
spin_lock(&scratch->lock);
145+
146+
scatterwalk_map_and_copy(scratch->src, req->src, 0, req->slen, 0);
165147
if (dir)
166-
ret = crypto_scomp_compress(scomp, scratch_src, req->slen,
167-
scratch_dst, &req->dlen, *ctx);
148+
ret = crypto_scomp_compress(scomp, scratch->src, req->slen,
149+
scratch->dst, &req->dlen, *ctx);
168150
else
169-
ret = crypto_scomp_decompress(scomp, scratch_src, req->slen,
170-
scratch_dst, &req->dlen, *ctx);
151+
ret = crypto_scomp_decompress(scomp, scratch->src, req->slen,
152+
scratch->dst, &req->dlen, *ctx);
171153
if (!ret) {
172154
if (!req->dst) {
173155
req->dst = sgl_alloc(req->dlen, GFP_ATOMIC, NULL);
@@ -176,11 +158,11 @@ static int scomp_acomp_comp_decomp(struct acomp_req *req, int dir)
176158
goto out;
177159
}
178160
}
179-
scatterwalk_map_and_copy(scratch_dst, req->dst, 0, req->dlen,
161+
scatterwalk_map_and_copy(scratch->dst, req->dst, 0, req->dlen,
180162
1);
181163
}
182164
out:
183-
put_cpu();
165+
spin_unlock(&scratch->lock);
184166
return ret;
185167
}
186168

@@ -201,7 +183,8 @@ static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
201183
crypto_free_scomp(*ctx);
202184

203185
mutex_lock(&scomp_lock);
204-
crypto_scomp_free_all_scratches();
186+
if (!--scomp_scratch_users)
187+
crypto_scomp_free_scratches();
205188
mutex_unlock(&scomp_lock);
206189
}
207190

0 commit comments

Comments
 (0)