erofs: support external crypto for decompression
PATCH 1/1 erofs: support external crypto for decompression
Some SoCs have hardware decompressors which using private algorithms, which can be used for accelerating decompression for EROFS, but current EROFS decompressor architecture do not support external decompressors, this change adds a crypto layer interface for decompression and can be used to hook SoC vendor's decompressor by crypto name. Soc vendors can develop their own code which can be added to crypto layer.
--- fs/erofs/Kconfig | 19 +++ fs/erofs/Makefile | 1 + fs/erofs/compress.h | 28 ++++ fs/erofs/decompressor.c | 7 + fs/erofs/decompressor_cryptor.c | 306 ++++++++++++++++++++++++++++++++++++++++ fs/erofs/erofs_fs.h | 14 ++ fs/erofs/internal.h | 4 + fs/erofs/super.c | 8 +- 8 files changed, 386 insertions(+), 1 deletion(-)
fs/erofs/Kconfig +19 -0
@@ -127,6 +127,25 @@ config EROFS_FS_ZIP_ZSTD If unsure, say N. +config EROFS_FS_ZIP_CRYPTO + bool "EROFS external crypto decompress support" + depends on EROFS_FS_ZIP + help + Saying Y here to support external crypto for decompressing EROFS + file systems. Some SoCs have hardware decompressor with private + algorithm, which can be used for accelarating decompression of + EROFS. This config enables external cryptos. + + If unsure, say N. + +config EROFS_CRYPTO_MAX_DISTANCE_PAGES + int "EROFS max distance pages for crypto usage" + default 32 + help + This config defines max distance pages for external crypto. Crypto + layer will use this value to grow up PCP buffers. + The default value is 128KB(32 pages). + config EROFS_FS_ONDEMAND bool "EROFS fscache-based on-demand read support" depends on EROFS_FS
fs/erofs/Makefile +1 -0
@@ -7,4 +7,5 @@ erofs-$(CONFIG_EROFS_FS_ZIP) += decompressor.o zmap.o zdata.o zutil.o erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.o erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o +erofs-$(CONFIG_EROFS_FS_ZIP_CRYPTO) += decompressor_cryptor.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o
fs/erofs/compress.h +28 -0
@@ -98,4 +98,32 @@ int z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq, struct page **pagepool);int z_erofs_zstd_decompress (struct z_erofs_decompress_req *rq,struct page **pgpl); + +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +/* for external cryto decompress */ +int z_erofs_crypto_decompress (struct z_erofs_decompress_req *rq, +struct page **pagepool); +int z_erofs_load_crypto_config (struct super_block *sb, +struct erofs_super_block *dsb, +void *data,int size); +#else +static inline int z_erofs_crypto_decompress (struct z_erofs_decompress_req *rq, +struct page **pagepool) +{ +return -EINVAL ; +} + +static inline int z_erofs_load_crypto_config (struct super_block *sb, +struct erofs_super_block *dsb, +void *data, +int size); +{ +if (crypto) { +erofs_err (sb,"crypto algorithm isn't enabled" ); +return -EINVAL ; + } +return 0 ; +} +#endif +#endif
fs/erofs/decompressor.c +7 -0
@@ -406,6 +406,13 @@ const struct z_erofs_decompressor erofs_decompressors[] = { .name = "zstd" },#endif +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO + [Z_EROFS_COMPRESSION_CRYPTO] = { + .config = z_erofs_load_crypto_config, + .decompress = z_erofs_crypto_decompress, + .name ="crypto" + }, +#endif };int z_erofs_parse_cfgs (struct super_block *sb,struct erofs_super_block *dsb)
fs/erofs/decompressor_cryptor.c new file +306 -0
306 lines not on this page · raw message
fs/erofs/erofs_fs.h +14 -0
@@ -297,6 +297,10 @@ enum {Z_EROFS_COMPRESSION_LZMA =1 ,Z_EROFS_COMPRESSION_DEFLATE =2 ,Z_EROFS_COMPRESSION_ZSTD =3 , +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +/* for generic crypto framework */ +Z_EROFS_COMPRESSION_CRYPTO =4 , +#endif Z_EROFS_COMPRESSION_MAX };#define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)
@@ -330,6 +334,16 @@ struct z_erofs_zstd_cfgs {u8 reserved[4 ]; } __packed; +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +/* 16 bytes */ +struct z_erofs_crypto_cfgs { +char crypto_name[8 ]; +__le16 max_distance; +__le16 max_pclusterblks; +u8 reserved[4 ]; +} __packed; +#endif +#define Z_EROFS_ZSTD_MAX_DICT_SIZE Z_EROFS_PCLUSTER_MAX_SIZE/*
fs/erofs/internal.h +4 -0
@@ -129,6 +129,10 @@ struct erofs_sb_info {struct inode *managed_cache;struct erofs_sb_lz4_info lz4; +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +/* decompress crypto for other algorithms except lz4/lzma */ +struct crypto_comp *crypto; +#endif #endif /* CONFIG_EROFS_FS_ZIP */ struct inode *packed_inode;struct erofs_dev_context *devs;
fs/erofs/super.c +7 -1
@@ -10,6 +10,9 @@#include <linux/fs_context.h> #include <linux/fs_parser.h> #include <linux/exportfs.h> +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +#include <linux/crypto.h> +#endif #include "xattr.h" #define CREATE_TRACE_POINTS
@@ -793,7 +796,10 @@ static void erofs_kill_sb(struct super_block *sb)kill_anon_super (sb); elsekill_block_super (sb ); - +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +if (sbi->crypto) +crypto_free_comp (sbi->crypto); +#endif erofs_free_dev_context (sbi->devs);fs_put_dax (sbi->dax_dev,NULL );erofs_fscache_unregister_fs (sb);
---
base-commit: ee78a17615ad0cfdbbc27182b1047cd36c9d4d5f
change-id: 20240606-erofs-decompression-fd5e2f271980
Best regards,
Signature · 1 line
Tao Zeng <[email protected]>
Hi,
Quoted 8 lines · On 2024/6/6 15:53, Tao Zeng via B4 Relay wrote:
From: Tao Zeng <[email protected]>
Some SoCs have hardware decompressors which using private algorithms, which can be used for accelerating decompression for EROFS, but current EROFS decompressor architecture do not support external decompressors, this change adds a crypto layer interface for decompression and can be used to hook SoC vendor's decompressor by crypto name. Soc vendors can develop their own code which can be added to crypto layer.
There are several fundamental points with this stuff.
First, I think using crypto APIs to support hardware accelerators _may_ be fine (I also plan to use crypto APIs for Intel IAA accelerator), as long as you contribute your SoC accelerator implementation upstream.
Using crypto APIs for out-of-tree external drivers as workaround doesn't work on my side (or I guess the whole Linux project).
Quoted 18 lines
Signed-off-by Tao Zeng <[email protected]>--- fs/erofs/Kconfig | 19 +++ fs/erofs/Makefile | 1 + fs/erofs/compress.h | 28 ++++ fs/erofs/decompressor.c | 7 + fs/erofs/decompressor_cryptor.c | 306 ++++++++++++++++++++++++++++++++++++++++ fs/erofs/erofs_fs.h | 14 ++ fs/erofs/internal.h | 4 + fs/erofs/super.c | 8 +- 8 files changed, 386 insertions(+), 1 deletion(-)fs/erofs/Kconfig +0
-0
fs/erofs/Kconfig config EROFS_FS_ZIP_ZSTD · line 131
Quoted 5 lines
fs/erofs/Kconfig +2
-0@@ -127,6 +127,25 @@ config EROFS_FS_ZIP_ZSTD If unsure, say N. +config EROFS_FS_ZIP_CRYPTO + bool "EROFS external crypto decompress support"
bool "EROFS crypto decompressor support"
fs/erofs/Kconfig config EROFS_FS_ZIP_ZSTD · line 136
Quoted 5 lines
fs/erofs/Kconfig +5
-0+ depends on EROFS_FS_ZIP + help + Saying Y here to support external crypto for decompressing EROFS + file systems. Some SoCs have hardware decompressor with private + algorithm, which can be used for accelarating decompression of
Sorry upstream Linux doesn't support private out-of-tree algorithms.
fs/erofs/Kconfig config EROFS_FS_ZIP_ZSTD · line 137
Quoted 1 line
fs/erofs/Kconfig +1
-0+ EROFS. This config enables external cryptos.
help
Saying Y here to use specific crypto decompressors to decompress EROFS file systems instead of software implementation, which can be used for offloading asynchronous requests to accelerators.
If unsure, say N.
fs/erofs/Kconfig config EROFS_FS_ZIP_ZSTD · line 147
Quoted 10 lines
fs/erofs/Kconfig +10
-0+ + If unsure, say N. + +config EROFS_CRYPTO_MAX_DISTANCE_PAGES + int "EROFS max distance pages for crypto usage" + default 32 + help + This config defines max distance pages for external crypto. Crypto + layer will use this value to grow up PCP buffers. + The default value is 128KB(32 pages).
No, I don't think it's of any use for accelerators.
Quoted 118 lines
fs/erofs/Kconfig +1
-0+ config EROFS_FS_ONDEMAND bool "EROFS fscache-based on-demand read support" depends on EROFS_FSfs/erofs/Makefile +1
-0@@ -7,4 +7,5 @@ erofs-$(CONFIG_EROFS_FS_ZIP) += decompressor.o zmap.o zdata.o zutil.o erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.o erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o +erofs-$(CONFIG_EROFS_FS_ZIP_CRYPTO) += decompressor_cryptor.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.ofs/erofs/compress.h +28
-0@@ -98,4 +98,32 @@ int z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq, struct page **pagepool);int z_erofs_zstd_decompress (struct z_erofs_decompress_req *rq,struct page **pgpl); + +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +/* for external cryto decompress */ +int z_erofs_crypto_decompress (struct z_erofs_decompress_req *rq, +struct page **pagepool); +int z_erofs_load_crypto_config (struct super_block *sb, +struct erofs_super_block *dsb, +void *data,int size); +#else +static inline int z_erofs_crypto_decompress (struct z_erofs_decompress_req *rq, +struct page **pagepool) +{ +return -EINVAL ; +} + +static inline int z_erofs_load_crypto_config (struct super_block *sb, +struct erofs_super_block *dsb, +void *data, +int size); +{ +if (crypto) { +erofs_err (sb,"crypto algorithm isn't enabled" ); +return -EINVAL ; + } +return 0 ; +} +#endif +#endif fs/erofs/decompressor.c +7
-0@@ -406,6 +406,13 @@ const struct z_erofs_decompressor erofs_decompressors[] = { .name = "zstd" },#endif +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO + [Z_EROFS_COMPRESSION_CRYPTO] = { + .config = z_erofs_load_crypto_config, + .decompress = z_erofs_crypto_decompress, + .name ="crypto" + }, +#endif };int z_erofs_parse_cfgs (struct super_block *sb,struct erofs_super_block *dsb)fs/erofs/decompressor_cryptor.c new file +43
-0@@ -0,0 +1,306 @@ +// SPDX-License-Identifier: (GPL-2.0+ OR MIT) +/* +* Copyright (c) 2024 Amlogic, Inc. All rights reserved. +*/ +#include <linux/types.h> +#include <linux/vmalloc.h> +#include <linux/mm.h> +#include <linux/slab.h> +#include <linux/highmem.h> +#include <linux/xz.h> +#include <linux/module.h> +#include <linux/crypto.h> +#include "compress.h" +#include "internal.h" + +static int crypto_max_distance_pages; + +int z_erofs_load_crypto_config (struct super_block *sb, +struct erofs_super_block *dsb, +void *data,int size) +{ +struct erofs_sb_info *sbi; +struct z_erofs_crypto_cfgs *crypto = (struct z_erofs_crypto_cfgs *)data; +int max_pages; + + sbi =EROFS_SB (sb); +if (!sbi) +return -EINVAL ; + +if (sbi->crypto) { +erofs_err (sb,"already have crypto\n" ); +return -EINVAL ; + } +if (crypto) { + max_pages =BIT (crypto->max_distance) /PAGE_SIZE ; +if (max_pages >CONFIG_EROFS_CRYPTO_MAX_DISTANCE_PAGES ) { +erofs_err (sb,"bad max distance:%d\n" , max_pages); +return -EINVAL ; + } + +if (max_pages > crypto_max_distance_pages) + crypto_max_distance_pages = max_pages; + sbi->crypto =crypto_alloc_comp (crypto->crypto_name,0 ,0 );
fs/erofs/decompressor_cryptor.c line 53
Quoted 10 lines
fs/erofs/decompressor_cryptor.c new file +10
-0+if (IS_ERR (sbi->crypto)) { +erofs_err (sb,"failed to alloc cryto %s\n" , + crypto->crypto_name); +return PTR_ERR (sbi->crypto); + } +erofs_info (sb,"max pcluster:%d, distance:%d, %d, crypto:%s\n" , + crypto->max_pclusterblks, + crypto->max_distance, crypto_max_distance_pages, + crypto->crypto_name); +return z_erofs_gbuf_growsize (crypto_max_distance_pages);
That is optimized for LZ4, I don't think it's useful for hardware accelerators.
fs/erofs/decompressor_cryptor.c line 65
Quoted 12 lines
fs/erofs/decompressor_cryptor.c new file +12
-0+ }else { +return -EINVAL ; + } +} + +static void *z_erofs_crypto_handle_inplace_io (struct z_erofs_decompress_req *rq, +void *inpage, +unsigned int *inputmargin, +int *maptype, +bool support_0padding) +{ +unsigned int nrpages_in, nrpages_out;
...
Same here.
fs/erofs/decompressor_cryptor.c line 132
Quoted 5 lines
fs/erofs/decompressor_cryptor.c new file +5
-0+return src; +} + +static int z_erofs_crypto_decompress_mem (struct z_erofs_decompress_req *rq,u8 *out) +{
..
Same here.
fs/erofs/decompressor_cryptor.c line 211
Quoted 10 lines
fs/erofs/decompressor_cryptor.c new file +10
-0+return ret; +} + +/* +* Fill all gaps with bounce pages if it's a sparse page list. Also check if +* all physical pages are consecutive, which can be seen for moderate CR. +*/ +static int z_erofs_crypto_prepare_dstpages (struct z_erofs_decompress_req *rq, +struct page **pagepool) +{
..
Same here.
Quoted 4 lines
fs/erofs/decompressor_cryptor.c new file +4
-0+ } +return kaddr ?1 :0 ; +} +
...
fs/erofs/erofs_fs.h enum { · line 303
Quoted 12 lines
fs/erofs/erofs_fs.h +4
-0@@ -297,6 +297,10 @@ enum {Z_EROFS_COMPRESSION_LZMA =1 ,Z_EROFS_COMPRESSION_DEFLATE =2 ,Z_EROFS_COMPRESSION_ZSTD =3 , +#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +/* for generic crypto framework */ + Z_EROFS_COMPRESSION_CRYPTO = 4, +#endif
crypto is NOT a new algorithm, please maintain a mapping (crypto decompressor <-> algorithm) with existing algrithms.
Quoted 6 lines
fs/erofs/erofs_fs.h +0
-0Z_EROFS_COMPRESSION_MAX };#define Z_EROFS_ALL_COMPR_ALGS ((1 << Z_EROFS_COMPRESSION_MAX) - 1)@@ -330,6 +334,16 @@ struct z_erofs_zstd_cfgs {u8 reserved[4 ]; } __packed;
fs/erofs/erofs_fs.h struct z_erofs_zstd_cfgs { · line 345
Quoted 10 lines
fs/erofs/erofs_fs.h +9
-0+#ifdef CONFIG_EROFS_FS_ZIP_CRYPTO +/* 16 bytes */ +struct z_erofs_crypto_cfgs { +char crypto_name[8 ]; +__le16 max_distance; +__le16 max_pclusterblks; +u8 reserved[4 ]; +} __packed; +#endif
Same here, no needed.
Thanks,
Gao Xiang
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Tao,
kernel test robot noticed the following build errors:
[auto build test ERROR on ee78a17615ad0cfdbbc27182b1047cd36c9d4d5f]
url: https://github.com/intel-lab-lkp/linux/commits/Tao-Zeng-via-B4-Relay/erofs-support-external-crypto-for-decompression/20240606-155702 base: ee78a17615ad0cfdbbc27182b1047cd36c9d4d5f patch link: https://lore.kernel.org/r/20240606-erofs-decompression-v1-1-ec5f31396e04%40amlogic.com patch subject: [PATCH] erofs: support external crypto for decompression config: m68k-defconfig (https://download.01.org/0day-ci/archive/20240607/[email protected]/config) compiler: m68k-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240607/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All error/warnings (new ones prefixed by >>):
In file included from fs/erofs/decompressor.c:6:
>> fs/erofs/compress.h:120:1: error: expected identifier or '(' before '{' token
120 | {
| ^
>> fs/erofs/compress.h:116:19: warning: 'z_erofs_load_crypto_config' declared 'static' but never defined [-Wunused-function] 116 | static inline int z_erofs_load_crypto_config(struct super_block *sb,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
vim +120 fs/erofs/compress.h
115
> 116 static inline int z_erofs_load_crypto_config(struct super_block *sb,
117 struct erofs_super_block *dsb,
118 void *data,
119 int size);
> 120 { 121 if (crypto) {
122 erofs_err(sb, "crypto algorithm isn't enabled");
123 return -EINVAL;
124 }
125 return 0;
126 }
127 #endif
128
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Tao,
kernel test robot noticed the following build errors:
[auto build test ERROR on ee78a17615ad0cfdbbc27182b1047cd36c9d4d5f]
url: https://github.com/intel-lab-lkp/linux/commits/Tao-Zeng-via-B4-Relay/erofs-support-external-crypto-for-decompression/20240606-155702 base: ee78a17615ad0cfdbbc27182b1047cd36c9d4d5f patch link: https://lore.kernel.org/r/20240606-erofs-decompression-v1-1-ec5f31396e04%40amlogic.com patch subject: [PATCH] erofs: support external crypto for decompression config: arm64-randconfig-004-20240607 (https://download.01.org/0day-ci/archive/20240607/[email protected]/config) compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project d7d2d4f53fc79b4b58e8d8d08151b577c3699d4a) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240607/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from fs/erofs/zdata.c:7:
In file included from fs/erofs/compress.h:9:
In file included from fs/erofs/internal.h:11:
In file included from include/linux/dax.h:6:
In file included from include/linux/mm.h:2241:
include/linux/vmstat.h:484:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
484 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
485 | item];
| ~~~~
include/linux/vmstat.h:491:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
491 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
492 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:498:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
498 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
include/linux/vmstat.h:503:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
503 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
504 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
include/linux/vmstat.h:512:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
512 | return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~ ^
513 | NR_VM_NUMA_EVENT_ITEMS +
| ~~~~~~~~~~~~~~~~~~~~~~
In file included from fs/erofs/zdata.c:7:
>> fs/erofs/compress.h:120:1: error: expected identifier or '('
120 | {
| ^5 warnings and 1 error generated.
vim +120 fs/erofs/compress.h
115
116 static inline int z_erofs_load_crypto_config(struct super_block *sb,
117 struct erofs_super_block *dsb,
118 void *data,
119 int size);
> 120 { 121 if (crypto) {
122 erofs_err(sb, "crypto algorithm isn't enabled");
123 return -EINVAL;
124 }
125 return 0;
126 }
127 #endif
128
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki