Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/zlib: Split deflate and inflate states for DFLTCC

Currently deflate and inflate both use a common state struct. There are
several variables in this struct that we don't need for inflate, and
more may be coming in the future. Therefore split them in two separate
structs.
Apart from that, introduce separate headers for dfltcc_deflate and
dfltcc_inflate.

This commit is based on:
https://github.com/zlib-ng/zlib-ng/commit/c592b1b

Link: https://lkml.kernel.org/r/20230126131428.1222214-7-zaslonko@linux.ibm.com
Signed-off-by: Mikhail Zaslonko <zaslonko@linux.ibm.com>
Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Mikhail Zaslonko and committed by
Andrew Morton
9fec9f8e cbf12540

+110 -76
+2 -2
lib/zlib_deflate/deflate.c
··· 54 54 55 55 /* architecture-specific bits */ 56 56 #ifdef CONFIG_ZLIB_DFLTCC 57 - # include "../zlib_dfltcc/dfltcc.h" 57 + # include "../zlib_dfltcc/dfltcc_deflate.h" 58 58 #else 59 59 #define DEFLATE_RESET_HOOK(strm) do {} while (0) 60 60 #define DEFLATE_HOOK(strm, flush, bstate) 0 ··· 106 106 deflate_state deflate_memory; 107 107 #ifdef CONFIG_ZLIB_DFLTCC 108 108 /* State memory for s390 hardware deflate */ 109 - struct dfltcc_state dfltcc_memory; 109 + struct dfltcc_deflate_state dfltcc_memory; 110 110 #endif 111 111 Byte *window_memory; 112 112 Pos *prev_memory;
+3 -22
lib/zlib_dfltcc/dfltcc.c
··· 23 23 } 24 24 } 25 25 26 - void dfltcc_reset( 27 - z_streamp strm, 28 - uInt size 29 - ) 30 - { 31 - struct dfltcc_state *dfltcc_state = 32 - (struct dfltcc_state *)((char *)strm->state + size); 33 - struct dfltcc_qaf_param *param = 34 - (struct dfltcc_qaf_param *)&dfltcc_state->param; 35 - 26 + void dfltcc_reset_state(struct dfltcc_state *dfltcc_state) { 36 27 /* Initialize available functions */ 37 28 if (is_dfltcc_enabled()) { 38 - dfltcc(DFLTCC_QAF, param, NULL, NULL, NULL, NULL, NULL); 39 - memmove(&dfltcc_state->af, param, sizeof(dfltcc_state->af)); 29 + dfltcc(DFLTCC_QAF, &dfltcc_state->param, NULL, NULL, NULL, NULL, NULL); 30 + memmove(&dfltcc_state->af, &dfltcc_state->param, sizeof(dfltcc_state->af)); 40 31 } else 41 32 memset(&dfltcc_state->af, 0, sizeof(dfltcc_state->af)); 42 33 43 34 /* Initialize parameter block */ 44 35 memset(&dfltcc_state->param, 0, sizeof(dfltcc_state->param)); 45 36 dfltcc_state->param.nt = 1; 46 - 47 - /* Initialize tuning parameters */ 48 - if (zlib_dfltcc_support == ZLIB_DFLTCC_FULL_DEBUG) 49 - dfltcc_state->level_mask = DFLTCC_LEVEL_MASK_DEBUG; 50 - else 51 - dfltcc_state->level_mask = DFLTCC_LEVEL_MASK; 52 - dfltcc_state->block_size = DFLTCC_BLOCK_SIZE; 53 - dfltcc_state->block_threshold = DFLTCC_FIRST_FHT_BLOCK_SIZE; 54 - dfltcc_state->dht_threshold = DFLTCC_DHT_MIN_SAMPLE_SIZE; 55 37 dfltcc_state->param.ribm = DFLTCC_RIBM; 56 38 } 57 - EXPORT_SYMBOL(dfltcc_reset); 58 39 59 40 MODULE_LICENSE("GPL");
+10 -42
lib/zlib_dfltcc/dfltcc.h
··· 93 93 struct dfltcc_state { 94 94 struct dfltcc_param_v0 param; /* Parameter block */ 95 95 struct dfltcc_qaf_param af; /* Available functions */ 96 + char msg[64]; /* Buffer for strm->msg */ 97 + }; 98 + 99 + /* 100 + * Extension of inflate_state and deflate_state for DFLTCC. 101 + */ 102 + struct dfltcc_deflate_state { 103 + struct dfltcc_state common; /* Parameter block */ 96 104 uLong level_mask; /* Levels on which to use DFLTCC */ 97 105 uLong block_size; /* New block each X bytes */ 98 106 uLong block_threshold; /* New block after total_in > X */ 99 107 uLong dht_threshold; /* New block only if avail_in >= X */ 100 - char msg[64]; /* Buffer for strm->msg */ 101 108 }; 102 109 103 110 #define ALIGN_UP(p, size) (__typeof__(p))(((uintptr_t)(p) + ((size) - 1)) & ~((size) - 1)) 104 111 /* Resides right after inflate_state or deflate_state */ 105 112 #define GET_DFLTCC_STATE(state) ((struct dfltcc_state *)((char *)(state) + ALIGN_UP(sizeof(*state), 8))) 106 113 107 - /* External functions */ 108 - int dfltcc_can_deflate(z_streamp strm); 109 - int dfltcc_deflate(z_streamp strm, 110 - int flush, 111 - block_state *result); 112 - void dfltcc_reset(z_streamp strm, uInt size); 113 - int dfltcc_can_inflate(z_streamp strm); 114 - typedef enum { 115 - DFLTCC_INFLATE_CONTINUE, 116 - DFLTCC_INFLATE_BREAK, 117 - DFLTCC_INFLATE_SOFTWARE, 118 - } dfltcc_inflate_action; 119 - dfltcc_inflate_action dfltcc_inflate(z_streamp strm, 120 - int flush, int *ret); 114 + void dfltcc_reset_state(struct dfltcc_state *dfltcc_state); 115 + 121 116 static inline int is_dfltcc_enabled(void) 122 117 { 123 118 return (zlib_dfltcc_support != ZLIB_DFLTCC_DISABLED && 124 119 test_facility(DFLTCC_FACILITY)); 125 120 } 126 121 127 - #define DEFLATE_RESET_HOOK(strm) \ 128 - dfltcc_reset((strm), sizeof(deflate_state)) 129 - 130 - #define DEFLATE_HOOK dfltcc_deflate 131 - 132 - #define DEFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_deflate((strm))) 133 - 134 122 #define DEFLATE_DFLTCC_ENABLED() is_dfltcc_enabled() 135 - 136 - #define INFLATE_RESET_HOOK(strm) \ 137 - dfltcc_reset((strm), sizeof(struct inflate_state)) 138 - 139 - #define INFLATE_TYPEDO_HOOK(strm, flush) \ 140 - if (dfltcc_can_inflate((strm))) { \ 141 - dfltcc_inflate_action action; \ 142 - \ 143 - RESTORE(); \ 144 - action = dfltcc_inflate((strm), (flush), &ret); \ 145 - LOAD(); \ 146 - if (action == DFLTCC_INFLATE_CONTINUE) \ 147 - break; \ 148 - else if (action == DFLTCC_INFLATE_BREAK) \ 149 - goto inf_leave; \ 150 - } 151 - 152 - #define INFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_inflate((strm))) 153 - 154 - #define INFLATE_NEED_UPDATEWINDOW(strm) (!dfltcc_can_inflate((strm))) 155 123 156 124 #endif /* DFLTCC_H */
+27 -8
lib/zlib_dfltcc/dfltcc_deflate.c
··· 2 2 3 3 #include "../zlib_deflate/defutil.h" 4 4 #include "dfltcc_util.h" 5 - #include "dfltcc.h" 5 + #include "dfltcc_deflate.h" 6 6 #include <asm/setup.h> 7 7 #include <linux/export.h> 8 8 #include <linux/zutil.h> 9 + 10 + #define GET_DFLTCC_DEFLATE_STATE(state) ((struct dfltcc_deflate_state *)GET_DFLTCC_STATE(state)) 9 11 10 12 /* 11 13 * Compress. ··· 17 15 ) 18 16 { 19 17 deflate_state *state = (deflate_state *)strm->state; 20 - struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); 18 + struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state); 21 19 22 20 /* Check for kernel dfltcc command line parameter */ 23 21 if (zlib_dfltcc_support == ZLIB_DFLTCC_DISABLED || ··· 30 28 return 0; 31 29 32 30 /* Unsupported hardware */ 33 - if (!is_bit_set(dfltcc_state->af.fns, DFLTCC_GDHT) || 34 - !is_bit_set(dfltcc_state->af.fns, DFLTCC_CMPR) || 35 - !is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0)) 31 + if (!is_bit_set(dfltcc_state->common.af.fns, DFLTCC_GDHT) || 32 + !is_bit_set(dfltcc_state->common.af.fns, DFLTCC_CMPR) || 33 + !is_bit_set(dfltcc_state->common.af.fmts, DFLTCC_FMT0)) 36 34 return 0; 37 35 38 36 return 1; 39 37 } 40 38 EXPORT_SYMBOL(dfltcc_can_deflate); 39 + 40 + void dfltcc_reset_deflate_state(z_streamp strm) { 41 + deflate_state *state = (deflate_state *)strm->state; 42 + struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state); 43 + 44 + dfltcc_reset_state(&dfltcc_state->common); 45 + 46 + /* Initialize tuning parameters */ 47 + if (zlib_dfltcc_support == ZLIB_DFLTCC_FULL_DEBUG) 48 + dfltcc_state->level_mask = DFLTCC_LEVEL_MASK_DEBUG; 49 + else 50 + dfltcc_state->level_mask = DFLTCC_LEVEL_MASK; 51 + dfltcc_state->block_size = DFLTCC_BLOCK_SIZE; 52 + dfltcc_state->block_threshold = DFLTCC_FIRST_FHT_BLOCK_SIZE; 53 + dfltcc_state->dht_threshold = DFLTCC_DHT_MIN_SAMPLE_SIZE; 54 + } 55 + EXPORT_SYMBOL(dfltcc_reset_deflate_state); 41 56 42 57 static void dfltcc_gdht( 43 58 z_streamp strm ··· 123 104 ) 124 105 { 125 106 deflate_state *state = (deflate_state *)strm->state; 126 - struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); 127 - struct dfltcc_param_v0 *param = &dfltcc_state->param; 107 + struct dfltcc_deflate_state *dfltcc_state = GET_DFLTCC_DEFLATE_STATE(state); 108 + struct dfltcc_param_v0 *param = &dfltcc_state->common.param; 128 109 uInt masked_avail_in; 129 110 dfltcc_cc cc; 130 111 int need_empty_block; ··· 263 244 } while (cc == DFLTCC_CC_AGAIN); 264 245 265 246 /* Translate parameter block to stream */ 266 - strm->msg = oesc_msg(dfltcc_state->msg, param->oesc); 247 + strm->msg = oesc_msg(dfltcc_state->common.msg, param->oesc); 267 248 state->bi_valid = param->sbb; 268 249 if (state->bi_valid == 0) 269 250 state->bi_buf = 0; /* Avoid accessing next_out */
+21
lib/zlib_dfltcc/dfltcc_deflate.h
··· 1 + // SPDX-License-Identifier: Zlib 2 + #ifndef DFLTCC_DEFLATE_H 3 + #define DFLTCC_DEFLATE_H 4 + 5 + #include "dfltcc.h" 6 + 7 + /* External functions */ 8 + int dfltcc_can_deflate(z_streamp strm); 9 + int dfltcc_deflate(z_streamp strm, 10 + int flush, 11 + block_state *result); 12 + void dfltcc_reset_deflate_state(z_streamp strm); 13 + 14 + #define DEFLATE_RESET_HOOK(strm) \ 15 + dfltcc_reset_deflate_state((strm)) 16 + 17 + #define DEFLATE_HOOK dfltcc_deflate 18 + 19 + #define DEFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_deflate((strm))) 20 + 21 + #endif /* DFLTCC_DEFLATE_H */
+9 -1
lib/zlib_dfltcc/dfltcc_inflate.c
··· 2 2 3 3 #include "../zlib_inflate/inflate.h" 4 4 #include "dfltcc_util.h" 5 - #include "dfltcc.h" 5 + #include "dfltcc_inflate.h" 6 6 #include <asm/setup.h> 7 7 #include <linux/export.h> 8 8 #include <linux/zutil.h> ··· 31 31 is_bit_set(dfltcc_state->af.fmts, DFLTCC_FMT0); 32 32 } 33 33 EXPORT_SYMBOL(dfltcc_can_inflate); 34 + 35 + void dfltcc_reset_inflate_state(z_streamp strm) { 36 + struct inflate_state *state = (struct inflate_state *)strm->state; 37 + struct dfltcc_state *dfltcc_state = GET_DFLTCC_STATE(state); 38 + 39 + dfltcc_reset_state(dfltcc_state); 40 + } 41 + EXPORT_SYMBOL(dfltcc_reset_inflate_state); 34 42 35 43 static int dfltcc_was_inflate_used( 36 44 z_streamp strm
+37
lib/zlib_dfltcc/dfltcc_inflate.h
··· 1 + // SPDX-License-Identifier: Zlib 2 + #ifndef DFLTCC_INFLATE_H 3 + #define DFLTCC_INFLATE_H 4 + 5 + #include "dfltcc.h" 6 + 7 + /* External functions */ 8 + void dfltcc_reset_inflate_state(z_streamp strm); 9 + int dfltcc_can_inflate(z_streamp strm); 10 + typedef enum { 11 + DFLTCC_INFLATE_CONTINUE, 12 + DFLTCC_INFLATE_BREAK, 13 + DFLTCC_INFLATE_SOFTWARE, 14 + } dfltcc_inflate_action; 15 + dfltcc_inflate_action dfltcc_inflate(z_streamp strm, 16 + int flush, int *ret); 17 + #define INFLATE_RESET_HOOK(strm) \ 18 + dfltcc_reset_inflate_state((strm)) 19 + 20 + #define INFLATE_TYPEDO_HOOK(strm, flush) \ 21 + if (dfltcc_can_inflate((strm))) { \ 22 + dfltcc_inflate_action action; \ 23 + \ 24 + RESTORE(); \ 25 + action = dfltcc_inflate((strm), (flush), &ret); \ 26 + LOAD(); \ 27 + if (action == DFLTCC_INFLATE_CONTINUE) \ 28 + break; \ 29 + else if (action == DFLTCC_INFLATE_BREAK) \ 30 + goto inf_leave; \ 31 + } 32 + 33 + #define INFLATE_NEED_CHECKSUM(strm) (!dfltcc_can_inflate((strm))) 34 + 35 + #define INFLATE_NEED_UPDATEWINDOW(strm) (!dfltcc_can_inflate((strm))) 36 + 37 + #endif /* DFLTCC_DEFLATE_H */
+1 -1
lib/zlib_inflate/inflate.c
··· 17 17 18 18 /* architecture-specific bits */ 19 19 #ifdef CONFIG_ZLIB_DFLTCC 20 - # include "../zlib_dfltcc/dfltcc.h" 20 + # include "../zlib_dfltcc/dfltcc_inflate.h" 21 21 #else 22 22 #define INFLATE_RESET_HOOK(strm) do {} while (0) 23 23 #define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)