Skip to content

Commit 6a8487a

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: scompress - defer allocation of scratch buffer to first use
The scompress code allocates 2 x 128 KB of scratch buffers for each CPU, so that clients of the async API can use synchronous implementations even from atomic context. However, on systems such as Cavium Thunderx (which has 96 cores), this adds up to a non-negligible 24 MB. Also, 32-bit systems may prefer to use their precious vmalloc space for other things,especially since there don't appear to be any clients for the async compression API yet. So let's defer allocation of the scratch buffers until the first time we allocate an acompress cipher based on an scompress implementation. Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent cc4d110 commit 6a8487a

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

crypto/scompress.c

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ static void crypto_scomp_show(struct seq_file *m, struct crypto_alg *alg)
6565
seq_puts(m, "type : scomp\n");
6666
}
6767

68-
static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
69-
{
70-
return 0;
71-
}
72-
7368
static void crypto_scomp_free_scratches(void * __percpu *scratches)
7469
{
7570
int i;
@@ -134,6 +129,17 @@ static int crypto_scomp_alloc_all_scratches(void)
134129
return 0;
135130
}
136131

132+
static int crypto_scomp_init_tfm(struct crypto_tfm *tfm)
133+
{
134+
int ret;
135+
136+
mutex_lock(&scomp_lock);
137+
ret = crypto_scomp_alloc_all_scratches();
138+
mutex_unlock(&scomp_lock);
139+
140+
return ret;
141+
}
142+
137143
static void crypto_scomp_sg_free(struct scatterlist *sgl)
138144
{
139145
int i, n;
@@ -241,6 +247,10 @@ static void crypto_exit_scomp_ops_async(struct crypto_tfm *tfm)
241247
struct crypto_scomp **ctx = crypto_tfm_ctx(tfm);
242248

243249
crypto_free_scomp(*ctx);
250+
251+
mutex_lock(&scomp_lock);
252+
crypto_scomp_free_all_scratches();
253+
mutex_unlock(&scomp_lock);
244254
}
245255

246256
int crypto_init_scomp_ops_async(struct crypto_tfm *tfm)
@@ -317,40 +327,18 @@ static const struct crypto_type crypto_scomp_type = {
317327
int crypto_register_scomp(struct scomp_alg *alg)
318328
{
319329
struct crypto_alg *base = &alg->base;
320-
int ret = -ENOMEM;
321-
322-
mutex_lock(&scomp_lock);
323-
if (crypto_scomp_alloc_all_scratches())
324-
goto error;
325330

326331
base->cra_type = &crypto_scomp_type;
327332
base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
328333
base->cra_flags |= CRYPTO_ALG_TYPE_SCOMPRESS;
329334

330-
ret = crypto_register_alg(base);
331-
if (ret)
332-
goto error;
333-
334-
mutex_unlock(&scomp_lock);
335-
return ret;
336-
337-
error:
338-
crypto_scomp_free_all_scratches();
339-
mutex_unlock(&scomp_lock);
340-
return ret;
335+
return crypto_register_alg(base);
341336
}
342337
EXPORT_SYMBOL_GPL(crypto_register_scomp);
343338

344339
int crypto_unregister_scomp(struct scomp_alg *alg)
345340
{
346-
int ret;
347-
348-
mutex_lock(&scomp_lock);
349-
ret = crypto_unregister_alg(&alg->base);
350-
crypto_scomp_free_all_scratches();
351-
mutex_unlock(&scomp_lock);
352-
353-
return ret;
341+
return crypto_unregister_alg(&alg->base);
354342
}
355343
EXPORT_SYMBOL_GPL(crypto_unregister_scomp);
356344

0 commit comments

Comments
 (0)