]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
zram: move immutable comp params away from per-CPU context
authorSergey Senozhatsky <senozhatsky@chromium.org>
Mon, 2 Sep 2024 10:56:07 +0000 (19:56 +0900)
committerAndrew Morton <akpm@linux-foundation.org>
Mon, 9 Sep 2024 23:39:10 +0000 (16:39 -0700)
Immutable params never change once comp has been allocated and setup, so
we don't need to store multiple copies of them in each per-CPU backend
context.  Move those to per-comp zcomp_params and pass it to backends
callbacks for requests execution.  Basically, this means parameters
sharing between different contexts.

Also introduce two new backends callbacks: setup_params() and
release_params().  First, we need to validate params in a driver-specific
way; second, driver may want to allocate its specific representation of
the params which is needed to execute requests.

Link: https://lkml.kernel.org/r/20240902105656.1383858-20-senozhatsky@chromium.org
Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Nick Terrell <terrelln@fb.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
drivers/block/zram/backend_842.c
drivers/block/zram/backend_deflate.c
drivers/block/zram/backend_lz4.c
drivers/block/zram/backend_lz4hc.c
drivers/block/zram/backend_lzo.c
drivers/block/zram/backend_lzorle.c
drivers/block/zram/backend_zstd.c
drivers/block/zram/zcomp.c
drivers/block/zram/zcomp.h

index 2f12023222642a2b775ba5775416533e5f6df2b2..10d9d5c60f53136d8eba257188ab27747ab855f2 100644 (file)
@@ -7,6 +7,15 @@
 
 #include "backend_842.h"
 
+static void release_params_842(struct zcomp_params *params)
+{
+}
+
+static int setup_params_842(struct zcomp_params *params)
+{
+       return 0;
+}
+
 static void destroy_842(struct zcomp_ctx *ctx)
 {
        kfree(ctx->context);
@@ -20,7 +29,8 @@ static int create_842(struct zcomp_params *params, struct zcomp_ctx *ctx)
        return 0;
 }
 
-static int compress_842(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int compress_842(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                       struct zcomp_req *req)
 {
        unsigned int dlen = req->dst_len;
        int ret;
@@ -32,7 +42,8 @@ static int compress_842(struct zcomp_ctx *ctx, struct zcomp_req *req)
        return ret;
 }
 
-static int decompress_842(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int decompress_842(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                         struct zcomp_req *req)
 {
        unsigned int dlen = req->dst_len;
 
@@ -44,5 +55,7 @@ const struct zcomp_ops backend_842 = {
        .decompress     = decompress_842,
        .create_ctx     = create_842,
        .destroy_ctx    = destroy_842,
+       .setup_params   = setup_params_842,
+       .release_params = release_params_842,
        .name           = "842",
 };
index eae4ee35c1dfb7d0b197d840f5c904a6fbb29b83..0f7f252c12f44806db9c556bae71e030d6b6d4b5 100644 (file)
 struct deflate_ctx {
        struct z_stream_s cctx;
        struct z_stream_s dctx;
-       s32 level;
 };
 
+static void deflate_release_params(struct zcomp_params *params)
+{
+}
+
+static int deflate_setup_params(struct zcomp_params *params)
+{
+       if (params->level == ZCOMP_PARAM_NO_LEVEL)
+               params->level = Z_DEFAULT_COMPRESSION;
+
+       return 0;
+}
+
 static void deflate_destroy(struct zcomp_ctx *ctx)
 {
        struct deflate_ctx *zctx = ctx->context;
@@ -46,17 +57,12 @@ static int deflate_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
                return -ENOMEM;
 
        ctx->context = zctx;
-       if (params->level != ZCOMP_PARAM_NO_LEVEL)
-               zctx->level = params->level;
-       else
-               zctx->level = Z_DEFAULT_COMPRESSION;
-
        sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
        zctx->cctx.workspace = vzalloc(sz);
        if (!zctx->cctx.workspace)
                goto error;
 
-       ret = zlib_deflateInit2(&zctx->cctx, zctx->level, Z_DEFLATED,
+       ret = zlib_deflateInit2(&zctx->cctx, params->level, Z_DEFLATED,
                                -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
                                Z_DEFAULT_STRATEGY);
        if (ret != Z_OK)
@@ -78,7 +84,8 @@ error:
        return -EINVAL;
 }
 
-static int deflate_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int deflate_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                           struct zcomp_req *req)
 {
        struct deflate_ctx *zctx = ctx->context;
        struct z_stream_s *deflate;
@@ -102,7 +109,9 @@ static int deflate_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
        return 0;
 }
 
-static int deflate_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int deflate_decompress(struct zcomp_params *params,
+                             struct zcomp_ctx *ctx,
+                             struct zcomp_req *req)
 {
        struct deflate_ctx *zctx = ctx->context;
        struct z_stream_s *inflate;
@@ -131,5 +140,7 @@ const struct zcomp_ops backend_deflate = {
        .decompress     = deflate_decompress,
        .create_ctx     = deflate_create,
        .destroy_ctx    = deflate_destroy,
+       .setup_params   = deflate_setup_params,
+       .release_params = deflate_release_params,
        .name           = "deflate",
 };
index e2d951e627462559ae9218c515b6fe050af54b8a..cf3c029bd5ad27d3ea59218d7ed53fffdc10c988 100644 (file)
@@ -5,60 +5,47 @@
 
 #include "backend_lz4.h"
 
-struct lz4_ctx {
-       void *mem;
-       s32 level;
-};
+static void lz4_release_params(struct zcomp_params *params)
+{
+}
 
-static void lz4_destroy(struct zcomp_ctx *ctx)
+static int lz4_setup_params(struct zcomp_params *params)
 {
-       struct lz4_ctx *zctx = ctx->context;
+       if (params->level == ZCOMP_PARAM_NO_LEVEL)
+               params->level = LZ4_ACCELERATION_DEFAULT;
 
-       if (!zctx)
-               return;
+       return 0;
+}
 
-       vfree(zctx->mem);
-       kfree(zctx);
+static void lz4_destroy(struct zcomp_ctx *ctx)
+{
+       vfree(ctx->context);
 }
 
 static int lz4_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
 {
-       struct lz4_ctx *zctx;
-
-       zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
-       if (!zctx)
+       ctx->context = vmalloc(LZ4_MEM_COMPRESS);
+       if (!ctx->context)
                return -ENOMEM;
 
-       ctx->context = zctx;
-       if (params->level != ZCOMP_PARAM_NO_LEVEL)
-               zctx->level = params->level;
-       else
-               zctx->level = LZ4_ACCELERATION_DEFAULT;
-
-       zctx->mem = vmalloc(LZ4_MEM_COMPRESS);
-       if (!zctx->mem)
-               goto error;
-
        return 0;
-error:
-       lz4_destroy(ctx);
-       return -EINVAL;
 }
 
-static int lz4_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lz4_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                       struct zcomp_req *req)
 {
-       struct lz4_ctx *zctx = ctx->context;
        int ret;
 
        ret = LZ4_compress_fast(req->src, req->dst, req->src_len,
-                               req->dst_len, zctx->level, zctx->mem);
+                               req->dst_len, params->level, ctx->context);
        if (!ret)
                return -EINVAL;
        req->dst_len = ret;
        return 0;
 }
 
-static int lz4_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lz4_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                         struct zcomp_req *req)
 {
        int ret;
 
@@ -74,5 +61,7 @@ const struct zcomp_ops backend_lz4 = {
        .decompress     = lz4_decompress,
        .create_ctx     = lz4_create,
        .destroy_ctx    = lz4_destroy,
+       .setup_params   = lz4_setup_params,
+       .release_params = lz4_release_params,
        .name           = "lz4",
 };
index 6da71ec5fc05ee222de42dbd441bc111808cf4ba..928a6ea78668db384e59fd9b20633d24d2902d3a 100644 (file)
@@ -5,60 +5,47 @@
 
 #include "backend_lz4hc.h"
 
-struct lz4hc_ctx {
-       void *mem;
-       s32 level;
-};
+static void lz4hc_release_params(struct zcomp_params *params)
+{
+}
 
-static void lz4hc_destroy(struct zcomp_ctx *ctx)
+static int lz4hc_setup_params(struct zcomp_params *params)
 {
-       struct lz4hc_ctx *zctx = ctx->context;
+       if (params->level == ZCOMP_PARAM_NO_LEVEL)
+               params->level = LZ4HC_DEFAULT_CLEVEL;
 
-       if (!zctx)
-               return;
+       return 0;
+}
 
-       vfree(zctx->mem);
-       kfree(zctx);
+static void lz4hc_destroy(struct zcomp_ctx *ctx)
+{
+       vfree(ctx->context);
 }
 
 static int lz4hc_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
 {
-       struct lz4hc_ctx *zctx;
-
-       zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
-       if (!zctx)
+       ctx->context = vmalloc(LZ4HC_MEM_COMPRESS);
+       if (!ctx->context)
                return -ENOMEM;
 
-       ctx->context = zctx;
-       if (params->level != ZCOMP_PARAM_NO_LEVEL)
-               zctx->level = params->level;
-       else
-               zctx->level = LZ4HC_DEFAULT_CLEVEL;
-
-       zctx->mem = vmalloc(LZ4HC_MEM_COMPRESS);
-       if (!zctx->mem)
-               goto error;
-
        return 0;
-error:
-       lz4hc_destroy(ctx);
-       return -EINVAL;
 }
 
-static int lz4hc_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lz4hc_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                         struct zcomp_req *req)
 {
-       struct lz4hc_ctx *zctx = ctx->context;
        int ret;
 
        ret = LZ4_compress_HC(req->src, req->dst, req->src_len, req->dst_len,
-                             zctx->level, zctx->mem);
+                             params->level, ctx->context);
        if (!ret)
                return -EINVAL;
        req->dst_len = ret;
        return 0;
 }
 
-static int lz4hc_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lz4hc_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                           struct zcomp_req *req)
 {
        int ret;
 
@@ -74,5 +61,7 @@ const struct zcomp_ops backend_lz4hc = {
        .decompress     = lz4hc_decompress,
        .create_ctx     = lz4hc_create,
        .destroy_ctx    = lz4hc_destroy,
+       .setup_params   = lz4hc_setup_params,
+       .release_params = lz4hc_release_params,
        .name           = "lz4hc",
 };
index 81fbad286092110dadeba6cb328a57f8ccef9ff8..4c906beaae6b33fcde1af419596317f7d4196c09 100644 (file)
@@ -6,6 +6,15 @@
 
 #include "backend_lzo.h"
 
+static void lzo_release_params(struct zcomp_params *params)
+{
+}
+
+static int lzo_setup_params(struct zcomp_params *params)
+{
+       return 0;
+}
+
 static int lzo_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
 {
        ctx->context = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
@@ -19,7 +28,8 @@ static void lzo_destroy(struct zcomp_ctx *ctx)
        kfree(ctx->context);
 }
 
-static int lzo_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lzo_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                       struct zcomp_req *req)
 {
        int ret;
 
@@ -28,7 +38,8 @@ static int lzo_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
        return ret == LZO_E_OK ? 0 : ret;
 }
 
-static int lzo_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lzo_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                         struct zcomp_req *req)
 {
        int ret;
 
@@ -42,5 +53,7 @@ const struct zcomp_ops backend_lzo = {
        .decompress     = lzo_decompress,
        .create_ctx     = lzo_create,
        .destroy_ctx    = lzo_destroy,
+       .setup_params   = lzo_setup_params,
+       .release_params = lzo_release_params,
        .name           = "lzo",
 };
index 99c9da8f116e4e7ebe6f5bd4f056637e8b0c318c..10640c96cbfcaae862d4f3e2f6797864d475581f 100644 (file)
@@ -6,6 +6,15 @@
 
 #include "backend_lzorle.h"
 
+static void lzorle_release_params(struct zcomp_params *params)
+{
+}
+
+static int lzorle_setup_params(struct zcomp_params *params)
+{
+       return 0;
+}
+
 static int lzorle_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
 {
        ctx->context = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
@@ -19,7 +28,8 @@ static void lzorle_destroy(struct zcomp_ctx *ctx)
        kfree(ctx->context);
 }
 
-static int lzorle_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lzorle_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                          struct zcomp_req *req)
 {
        int ret;
 
@@ -28,7 +38,8 @@ static int lzorle_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
        return ret == LZO_E_OK ? 0 : ret;
 }
 
-static int lzorle_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int lzorle_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                            struct zcomp_req *req)
 {
        int ret;
 
@@ -42,5 +53,7 @@ const struct zcomp_ops backend_lzorle = {
        .decompress     = lzorle_decompress,
        .create_ctx     = lzorle_create,
        .destroy_ctx    = lzorle_destroy,
+       .setup_params   = lzorle_setup_params,
+       .release_params = lzorle_release_params,
        .name           = "lzo-rle",
 };
index 9f000dedf1adebdb2d9c617e45ac014b9bc10a5a..5b33daf4f6454857a19e29bd02c0fe1321ba0af3 100644 (file)
 struct zstd_ctx {
        zstd_cctx *cctx;
        zstd_dctx *dctx;
-       zstd_parameters cprm;
        void *cctx_mem;
        void *dctx_mem;
-       s32 level;
 };
 
+struct zstd_params {
+       zstd_parameters cprm;
+};
+
+static void zstd_release_params(struct zcomp_params *params)
+{
+       kfree(params->drv_data);
+}
+
+static int zstd_setup_params(struct zcomp_params *params)
+{
+       struct zstd_params *zp;
+
+       zp = kzalloc(sizeof(*zp), GFP_KERNEL);
+       if (!zp)
+               return -ENOMEM;
+
+       if (params->level == ZCOMP_PARAM_NO_LEVEL)
+               params->level = zstd_default_clevel();
+
+       zp->cprm = zstd_get_params(params->level, PAGE_SIZE);
+       params->drv_data = zp;
+
+       return 0;
+}
+
 static void zstd_destroy(struct zcomp_ctx *ctx)
 {
        struct zstd_ctx *zctx = ctx->context;
@@ -39,13 +63,7 @@ static int zstd_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
                return -ENOMEM;
 
        ctx->context = zctx;
-       if (params->level != ZCOMP_PARAM_NO_LEVEL)
-               zctx->level = params->level;
-       else
-               zctx->level = zstd_default_clevel();
-
-       prm = zstd_get_params(zctx->level, PAGE_SIZE);
-       zctx->cprm = zstd_get_params(zctx->level, PAGE_SIZE);
+       prm = zstd_get_params(params->level, PAGE_SIZE);
        sz = zstd_cctx_workspace_bound(&prm.cParams);
        zctx->cctx_mem = vzalloc(sz);
        if (!zctx->cctx_mem)
@@ -71,20 +89,23 @@ error:
        return -EINVAL;
 }
 
-static int zstd_compress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int zstd_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                        struct zcomp_req *req)
 {
+       struct zstd_params *zp = params->drv_data;
        struct zstd_ctx *zctx = ctx->context;
        size_t ret;
 
        ret = zstd_compress_cctx(zctx->cctx, req->dst, req->dst_len,
-                                req->src, req->src_len, &zctx->cprm);
+                                req->src, req->src_len, &zp->cprm);
        if (zstd_is_error(ret))
                return -EINVAL;
        req->dst_len = ret;
        return 0;
 }
 
-static int zstd_decompress(struct zcomp_ctx *ctx, struct zcomp_req *req)
+static int zstd_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                          struct zcomp_req *req)
 {
        struct zstd_ctx *zctx = ctx->context;
        size_t ret;
@@ -101,5 +122,7 @@ const struct zcomp_ops backend_zstd = {
        .decompress     = zstd_decompress,
        .create_ctx     = zstd_create,
        .destroy_ctx    = zstd_destroy,
+       .setup_params   = zstd_setup_params,
+       .release_params = zstd_release_params,
        .name           = "zstd",
 };
index 96f07287e5712628ce0e7d4bffc1e366e76498e6..bb514403e305274dbc594dc952817728353b2186 100644 (file)
@@ -129,7 +129,7 @@ int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
        };
        int ret;
 
-       ret = comp->ops->compress(&zstrm->ctx, &req);
+       ret = comp->ops->compress(comp->params, &zstrm->ctx, &req);
        if (!ret)
                *dst_len = req.dst_len;
        return ret;
@@ -145,7 +145,7 @@ int zcomp_decompress(struct zcomp *comp, struct zcomp_strm *zstrm,
                .dst_len = PAGE_SIZE,
        };
 
-       return comp->ops->decompress(&zstrm->ctx, &req);
+       return comp->ops->decompress(comp->params, &zstrm->ctx, &req);
 }
 
 int zcomp_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
@@ -173,7 +173,7 @@ int zcomp_cpu_dead(unsigned int cpu, struct hlist_node *node)
        return 0;
 }
 
-static int zcomp_init(struct zcomp *comp)
+static int zcomp_init(struct zcomp *comp, struct zcomp_params *params)
 {
        int ret;
 
@@ -181,12 +181,19 @@ static int zcomp_init(struct zcomp *comp)
        if (!comp->stream)
                return -ENOMEM;
 
+       comp->params = params;
+       ret = comp->ops->setup_params(comp->params);
+       if (ret)
+               goto cleanup;
+
        ret = cpuhp_state_add_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
        if (ret < 0)
                goto cleanup;
+
        return 0;
 
 cleanup:
+       comp->ops->release_params(comp->params);
        free_percpu(comp->stream);
        return ret;
 }
@@ -194,6 +201,7 @@ cleanup:
 void zcomp_destroy(struct zcomp *comp)
 {
        cpuhp_state_remove_instance(CPUHP_ZCOMP_PREPARE, &comp->node);
+       comp->ops->release_params(comp->params);
        free_percpu(comp->stream);
        kfree(comp);
 }
@@ -215,14 +223,13 @@ struct zcomp *zcomp_create(const char *alg, struct zcomp_params *params)
        if (!comp)
                return ERR_PTR(-ENOMEM);
 
-       comp->params = params;
        comp->ops = lookup_backend_ops(alg);
        if (!comp->ops) {
                kfree(comp);
                return ERR_PTR(-EINVAL);
        }
 
-       error = zcomp_init(comp);
+       error = zcomp_init(comp, params);
        if (error) {
                kfree(comp);
                return ERR_PTR(error);
index 1d8920c2d449ea6c0b74944d50cd785b0250f38e..ad576281384248daf4adbb12dcf1ae8c05fb84ce 100644 (file)
@@ -7,10 +7,18 @@
 
 #define ZCOMP_PARAM_NO_LEVEL   INT_MIN
 
+/*
+ * Immutable driver (backend) parameters. The driver may attach private
+ * data to it (e.g. driver representation of the dictionary, etc.).
+ *
+ * This data is kept per-comp and is shared among execution contexts.
+ */
 struct zcomp_params {
        void *dict;
        size_t dict_sz;
        s32 level;
+
+       void *drv_data;
 };
 
 /*
@@ -38,13 +46,17 @@ struct zcomp_req {
 };
 
 struct zcomp_ops {
-       int (*compress)(struct zcomp_ctx *ctx, struct zcomp_req *req);
-       int (*decompress)(struct zcomp_ctx *ctx, struct zcomp_req *req);
+       int (*compress)(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                       struct zcomp_req *req);
+       int (*decompress)(struct zcomp_params *params, struct zcomp_ctx *ctx,
+                         struct zcomp_req *req);
 
-       int (*create_ctx)(struct zcomp_params *params,
-                         struct zcomp_ctx *ctx);
+       int (*create_ctx)(struct zcomp_params *params, struct zcomp_ctx *ctx);
        void (*destroy_ctx)(struct zcomp_ctx *ctx);
 
+       int (*setup_params)(struct zcomp_params *params);
+       void (*release_params)(struct zcomp_params *params);
+
        const char *name;
 };