ubifs: Use ACOMP_REQUEST_CLONE
authorHerbert Xu <herbert@gondor.apana.org.au>
Mon, 7 Apr 2025 10:03:02 +0000 (18:03 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Wed, 16 Apr 2025 07:16:20 +0000 (15:16 +0800)
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>
fs/ubifs/compress.c

index ea6f06adcd43ac1f2e16d2d6ba2677e4a2b2cdad..059a02691edd230e0c27554802fd746457ecbfb1 100644 (file)
 #include <linux/highmem.h>
 #include "ubifs.h"
 
+union ubifs_in_ptr {
+       const void *buf;
+       struct folio *folio;
+};
+
 /* Fake description object for the "none" compressor */
 static struct ubifs_compressor none_compr = {
        .compr_type = UBIFS_COMPR_NONE,
@@ -68,28 +73,61 @@ static struct ubifs_compressor zstd_compr = {
 /* All UBIFS compressors */
 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
 
-static int ubifs_compress_req(const struct ubifs_info *c,
-                             struct acomp_req *req,
-                             void *out_buf, int *out_len,
-                             const char *compr_name)
+static void ubifs_compress_common(int *compr_type, union ubifs_in_ptr in_ptr,
+                                 size_t in_offset, int in_len, bool in_folio,
+                                 void *out_buf, int *out_len)
 {
-       struct crypto_wait wait;
-       int in_len = req->slen;
+       struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
        int dlen = *out_len;
        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);
 
-       crypto_init_wait(&wait);
-       acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
-                                  crypto_req_done, &wait);
-       acomp_request_set_dst_dma(req, out_buf, dlen);
-       err = crypto_acomp_compress(req);
-       err = crypto_wait_req(err, &wait);
-       *out_len = req->dlen;
-       acomp_request_free(req);
+       do {
+               ACOMP_REQUEST_ON_STACK(req, compr->cc);
+               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);
+               err = crypto_acomp_compress(req);
+               dlen = req->dlen;
+               if (err != -EAGAIN)
+                       break;
+
+               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 err;
+       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,
                    int in_len, void *out_buf, int *out_len, int *compr_type)
 {
-       int err;
-       struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
-
-       if (*compr_type == UBIFS_COMPR_NONE)
-               goto no_compr;
+       union ubifs_in_ptr in_ptr = { .buf = in_buf };
 
-       /* 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;
+       ubifs_compress_common(compr_type, in_ptr, 0, in_len, false,
+                             out_buf, out_len);
 }
 
 /**
@@ -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,
                          int *out_len, int *compr_type)
 {
-       int err;
-       struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
+       union ubifs_in_ptr in_ptr = { .folio = in_folio };
 
-       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;
+       ubifs_compress_common(compr_type, in_ptr, in_offset, in_len, true,
+                             out_buf, out_len);
+}
 
-       {
-               ACOMP_REQUEST_ALLOC(req, compr->cc, GFP_NOFS | __GFP_NOWARN);
+static int ubifs_decompress_common(const struct ubifs_info *c,
+                                  const void *in_buf, int in_len,
+                                  void *out_ptr, size_t out_offset,
+                                  int *out_len, bool out_folio,
+                                  int compr_type)
+{
+       struct ubifs_compressor *compr;
+       int dlen = *out_len;
+       int err;
 
-               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 (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
+               ubifs_err(c, "invalid compression type %d", compr_type);
+               return -EINVAL;
        }
 
-       if (err)
-               goto no_compr;
-
-       return;
+       compr = ubifs_compressors[compr_type];
 
-no_compr:
-       memcpy_from_folio(out_buf, in_folio, in_offset, in_len);
-       *out_len = in_len;
-       *compr_type = UBIFS_COMPR_NONE;
-}
+       if (unlikely(!compr->capi_name)) {
+               ubifs_err(c, "%s compression is not compiled in", compr->name);
+               return -EINVAL;
+       }
 
-static int ubifs_decompress_req(const struct ubifs_info *c,
-                               struct acomp_req *req,
-                               const void *in_buf, int in_len, int *out_len,
-                               const char *compr_name)
-{
-       struct crypto_wait wait;
-       int err;
+       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;
+       }
 
-       crypto_init_wait(&wait);
-       acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
-                                  crypto_req_done, &wait);
-       acomp_request_set_src_dma(req, in_buf, in_len);
-       err = crypto_acomp_decompress(req);
-       err = crypto_wait_req(err, &wait);
-       *out_len = req->dlen;
+       do {
+               ACOMP_REQUEST_ON_STACK(req, compr->cc);
+               DECLARE_CRYPTO_WAIT(wait);
 
+               acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
+                                          crypto_req_done, &wait);
+               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_wait_req(err, &wait);
+               dlen = req->dlen;
+               acomp_request_free(req);
+       } while (0);
+
+       *out_len = dlen;
        if (err)
                ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
-                         in_len, compr_name, err);
-
-       acomp_request_free(req);
+                         in_len, compr->name, 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 in_len, void *out_buf, int *out_len, int compr_type)
 {
-       struct ubifs_compressor *compr;
-
-       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);
-       }
+       return ubifs_decompress_common(c, in_buf, in_len, out_buf, 0, out_len,
+                                      false, compr_type);
 }
 
 /**
@@ -283,34 +290,8 @@ int ubifs_decompress_folio(const struct ubifs_info *c, const void *in_buf,
                           int in_len, struct folio *out_folio,
                           size_t out_offset, int *out_len, int compr_type)
 {
-       struct ubifs_compressor *compr;
-
-       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);
-       }
+       return ubifs_decompress_common(c, in_buf, in_len, out_folio,
+                                      out_offset, out_len, true, compr_type);
 }
 
 /**