ubifs: Use ACOMP_REQUEST_CLONE

Switch to the new acomp API where stacks requests are used by
default and a dynamic request is only allocted when necessary.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
pull/1250/head
Herbert Xu 2025-04-07 18:03:02 +08:00
parent 097c432caa
commit e87e95d8dd
1 changed files with 116 additions and 135 deletions

View File

@ -19,6 +19,11 @@
#include <linux/highmem.h> #include <linux/highmem.h>
#include "ubifs.h" #include "ubifs.h"
union ubifs_in_ptr {
const void *buf;
struct folio *folio;
};
/* Fake description object for the "none" compressor */ /* Fake description object for the "none" compressor */
static struct ubifs_compressor none_compr = { static struct ubifs_compressor none_compr = {
.compr_type = UBIFS_COMPR_NONE, .compr_type = UBIFS_COMPR_NONE,
@ -68,28 +73,61 @@ static struct ubifs_compressor zstd_compr = {
/* All UBIFS compressors */ /* All UBIFS compressors */
struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT]; struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
static int ubifs_compress_req(const struct ubifs_info *c, static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,
struct acomp_req *req, size_t in_offset, int in_len, bool in_folio,
void *out_buf, int *out_len, void *out_buf, int *out_len)
const char *compr_name)
{ {
struct crypto_wait wait; struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
int in_len = req->slen;
int dlen = *out_len; int dlen = *out_len;
int err; int err;
if (*compr_type == UBIFS_COMPR_NONE)
goto no_compr;
/* If the input data is small, do not even try to compress it */
if (in_len < UBIFS_MIN_COMPR_LEN)
goto no_compr;
dlen = min(dlen, in_len - UBIFS_MIN_COMPRESS_DIFF); dlen = min(dlen, in_len - UBIFS_MIN_COMPRESS_DIFF);
crypto_init_wait(&wait); do {
acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, ACOMP_REQUEST_ON_STACK(req, compr->cc);
crypto_req_done, &wait); DECLARE_CRYPTO_WAIT(wait);
acomp_request_set_callback(req, 0, NULL, NULL);
if (in_folio)
acomp_request_set_src_folio(req, in_ptr.folio,
in_offset, in_len);
else
acomp_request_set_src_dma(req, in_ptr.buf, in_len);
acomp_request_set_dst_dma(req, out_buf, dlen); acomp_request_set_dst_dma(req, out_buf, dlen);
err = crypto_acomp_compress(req); err = crypto_acomp_compress(req);
err = crypto_wait_req(err, &wait); dlen = req->dlen;
*out_len = req->dlen; if (err != -EAGAIN)
acomp_request_free(req); break;
return err; req = ACOMP_REQUEST_CLONE(req, GFP_NOFS | __GFP_NOWARN);
acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
crypto_req_done, &wait);
err = crypto_acomp_compress(req);
err = crypto_wait_req(err, &wait);
dlen = req->dlen;
acomp_request_free(req);
} while (0);
*out_len = dlen;
if (err)
goto no_compr;
return;
no_compr:
if (in_folio)
memcpy_from_folio(out_buf, in_ptr.folio, in_offset, in_len);
else
memcpy(out_buf, in_ptr.buf, in_len);
*out_len = in_len;
*compr_type = UBIFS_COMPR_NONE;
} }
/** /**
@ -114,32 +152,10 @@ static int ubifs_compress_req(const struct ubifs_info *c,
void ubifs_compress(const struct ubifs_info *c, const void *in_buf, void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
int in_len, void *out_buf, int *out_len, int *compr_type) int in_len, void *out_buf, int *out_len, int *compr_type)
{ {
int err; union ubifs_in_ptr in_ptr = { .buf = in_buf };
struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
if (*compr_type == UBIFS_COMPR_NONE) ubifs_compress_common(compr_type, in_ptr, 0, in_len, false,
goto no_compr; out_buf, out_len);
/* If the input data is small, do not even try to compress it */
if (in_len < UBIFS_MIN_COMPR_LEN)
goto no_compr;
{
ACOMP_REQUEST_ALLOC(req, compr->cc, GFP_NOFS | __GFP_NOWARN);
acomp_request_set_src_dma(req, in_buf, in_len);
err = ubifs_compress_req(c, req, out_buf, out_len, compr->name);
}
if (err)
goto no_compr;
return;
no_compr:
memcpy(out_buf, in_buf, in_len);
*out_len = in_len;
*compr_type = UBIFS_COMPR_NONE;
} }
/** /**
@ -166,55 +182,71 @@ void ubifs_compress_folio(const struct ubifs_info *c, struct folio *in_folio,
size_t in_offset, int in_len, void *out_buf, size_t in_offset, int in_len, void *out_buf,
int *out_len, int *compr_type) int *out_len, int *compr_type)
{ {
int err; union ubifs_in_ptr in_ptr = { .folio = in_folio };
struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
if (*compr_type == UBIFS_COMPR_NONE) ubifs_compress_common(compr_type, in_ptr, in_offset, in_len, true,
goto no_compr; out_buf, out_len);
/* If the input data is small, do not even try to compress it */
if (in_len < UBIFS_MIN_COMPR_LEN)
goto no_compr;
{
ACOMP_REQUEST_ALLOC(req, compr->cc, GFP_NOFS | __GFP_NOWARN);
acomp_request_set_src_folio(req, in_folio, in_offset, in_len);
err = ubifs_compress_req(c, req, out_buf, out_len, compr->name);
}
if (err)
goto no_compr;
return;
no_compr:
memcpy_from_folio(out_buf, in_folio, in_offset, in_len);
*out_len = in_len;
*compr_type = UBIFS_COMPR_NONE;
} }
static int ubifs_decompress_req(const struct ubifs_info *c, static int ubifs_decompress_common(const struct ubifs_info *c,
struct acomp_req *req, const void *in_buf, int in_len,
const void *in_buf, int in_len, int *out_len, void *out_ptr, size_t out_offset,
const char *compr_name) int *out_len, bool out_folio,
int compr_type)
{ {
struct crypto_wait wait; struct ubifs_compressor *compr;
int dlen = *out_len;
int err; int err;
crypto_init_wait(&wait); if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
ubifs_err(c, "invalid compression type %d", compr_type);
return -EINVAL;
}
compr = ubifs_compressors[compr_type];
if (unlikely(!compr->capi_name)) {
ubifs_err(c, "%s compression is not compiled in", compr->name);
return -EINVAL;
}
if (compr_type == UBIFS_COMPR_NONE) {
if (out_folio)
memcpy_to_folio(out_ptr, out_offset, in_buf, in_len);
else
memcpy(out_ptr, in_buf, in_len);
*out_len = in_len;
return 0;
}
do {
ACOMP_REQUEST_ON_STACK(req, compr->cc);
DECLARE_CRYPTO_WAIT(wait);
acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
crypto_req_done, &wait); crypto_req_done, &wait);
acomp_request_set_src_dma(req, in_buf, in_len); acomp_request_set_src_dma(req, in_buf, in_len);
if (out_folio)
acomp_request_set_dst_folio(req, out_ptr, out_offset,
dlen);
else
acomp_request_set_dst_dma(req, out_ptr, dlen);
err = crypto_acomp_decompress(req);
dlen = req->dlen;
if (err != -EAGAIN)
break;
req = ACOMP_REQUEST_CLONE(req, GFP_NOFS | __GFP_NOWARN);
err = crypto_acomp_decompress(req); err = crypto_acomp_decompress(req);
err = crypto_wait_req(err, &wait); err = crypto_wait_req(err, &wait);
*out_len = req->dlen; dlen = req->dlen;
acomp_request_free(req);
} while (0);
*out_len = dlen;
if (err) if (err)
ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d", ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
in_len, compr_name, err); in_len, compr->name, err);
acomp_request_free(req);
return err; return err;
} }
@ -235,33 +267,8 @@ static int ubifs_decompress_req(const struct ubifs_info *c,
int ubifs_decompress(const struct ubifs_info *c, const void *in_buf, int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
int in_len, void *out_buf, int *out_len, int compr_type) int in_len, void *out_buf, int *out_len, int compr_type)
{ {
struct ubifs_compressor *compr; return ubifs_decompress_common(c, in_buf, in_len, out_buf, 0, out_len,
false, compr_type);
if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
ubifs_err(c, "invalid compression type %d", compr_type);
return -EINVAL;
}
compr = ubifs_compressors[compr_type];
if (unlikely(!compr->capi_name)) {
ubifs_err(c, "%s compression is not compiled in", compr->name);
return -EINVAL;
}
if (compr_type == UBIFS_COMPR_NONE) {
memcpy(out_buf, in_buf, in_len);
*out_len = in_len;
return 0;
}
{
ACOMP_REQUEST_ALLOC(req, compr->cc, GFP_NOFS | __GFP_NOWARN);
acomp_request_set_dst_dma(req, out_buf, *out_len);
return ubifs_decompress_req(c, req, in_buf, in_len, out_len,
compr->name);
}
} }
/** /**
@ -283,34 +290,8 @@ int ubifs_decompress_folio(const struct ubifs_info *c, const void *in_buf,
int in_len, struct folio *out_folio, int in_len, struct folio *out_folio,
size_t out_offset, int *out_len, int compr_type) size_t out_offset, int *out_len, int compr_type)
{ {
struct ubifs_compressor *compr; return ubifs_decompress_common(c, in_buf, in_len, out_folio,
out_offset, out_len, true, compr_type);
if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
ubifs_err(c, "invalid compression type %d", compr_type);
return -EINVAL;
}
compr = ubifs_compressors[compr_type];
if (unlikely(!compr->capi_name)) {
ubifs_err(c, "%s compression is not compiled in", compr->name);
return -EINVAL;
}
if (compr_type == UBIFS_COMPR_NONE) {
memcpy_to_folio(out_folio, out_offset, in_buf, in_len);
*out_len = in_len;
return 0;
}
{
ACOMP_REQUEST_ALLOC(req, compr->cc, GFP_NOFS | __GFP_NOWARN);
acomp_request_set_dst_folio(req, out_folio, out_offset,
*out_len);
return ubifs_decompress_req(c, req, in_buf, in_len, out_len,
compr->name);
}
} }
/** /**