Skip to content

Commit 37b605f

Browse files
committed
ubifs: Use crypto_acomp interface
Replace the legacy crypto compression interface with the new acomp interface. Remove the compression mutexes and the overallocation for memory (the offender LZO has been fixed). Cap the output buffer length for compression to eliminate the post-compression check for UBIFS_MIN_COMPRESS_DIFF. Signed-off-by: Herbert Xu <[email protected]> Tested-by: Zhihao Cheng <[email protected]> # For xfstests Reviewed-by: Zhihao Cheng <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent b03d542 commit 37b605f

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

fs/ubifs/compress.c

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* decompression.
1616
*/
1717

18-
#include <linux/crypto.h>
18+
#include <crypto/acompress.h>
1919
#include "ubifs.h"
2020

2121
/* Fake description object for the "none" compressor */
@@ -26,11 +26,8 @@ static struct ubifs_compressor none_compr = {
2626
};
2727

2828
#ifdef CONFIG_UBIFS_FS_LZO
29-
static DEFINE_MUTEX(lzo_mutex);
30-
3129
static struct ubifs_compressor lzo_compr = {
3230
.compr_type = UBIFS_COMPR_LZO,
33-
.comp_mutex = &lzo_mutex,
3431
.name = "lzo",
3532
.capi_name = "lzo",
3633
};
@@ -42,13 +39,8 @@ static struct ubifs_compressor lzo_compr = {
4239
#endif
4340

4441
#ifdef CONFIG_UBIFS_FS_ZLIB
45-
static DEFINE_MUTEX(deflate_mutex);
46-
static DEFINE_MUTEX(inflate_mutex);
47-
4842
static struct ubifs_compressor zlib_compr = {
4943
.compr_type = UBIFS_COMPR_ZLIB,
50-
.comp_mutex = &deflate_mutex,
51-
.decomp_mutex = &inflate_mutex,
5244
.name = "zlib",
5345
.capi_name = "deflate",
5446
};
@@ -60,13 +52,8 @@ static struct ubifs_compressor zlib_compr = {
6052
#endif
6153

6254
#ifdef CONFIG_UBIFS_FS_ZSTD
63-
static DEFINE_MUTEX(zstd_enc_mutex);
64-
static DEFINE_MUTEX(zstd_dec_mutex);
65-
6655
static struct ubifs_compressor zstd_compr = {
6756
.compr_type = UBIFS_COMPR_ZSTD,
68-
.comp_mutex = &zstd_enc_mutex,
69-
.decomp_mutex = &zstd_dec_mutex,
7057
.name = "zstd",
7158
.capi_name = "zstd",
7259
};
@@ -80,6 +67,30 @@ static struct ubifs_compressor zstd_compr = {
8067
/* All UBIFS compressors */
8168
struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
8269

70+
static int ubifs_compress_req(const struct ubifs_info *c,
71+
struct acomp_req *req,
72+
void *out_buf, int *out_len,
73+
const char *compr_name)
74+
{
75+
struct crypto_wait wait;
76+
int in_len = req->slen;
77+
int dlen = *out_len;
78+
int err;
79+
80+
dlen = min(dlen, in_len - UBIFS_MIN_COMPRESS_DIFF);
81+
82+
crypto_init_wait(&wait);
83+
acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
84+
crypto_req_done, &wait);
85+
acomp_request_set_dst_dma(req, out_buf, dlen);
86+
err = crypto_acomp_compress(req);
87+
err = crypto_wait_req(err, &wait);
88+
*out_len = req->dlen;
89+
acomp_request_free(req);
90+
91+
return err;
92+
}
93+
8394
/**
8495
* ubifs_compress - compress data.
8596
* @c: UBIFS file-system description object
@@ -112,23 +123,14 @@ void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
112123
if (in_len < UBIFS_MIN_COMPR_LEN)
113124
goto no_compr;
114125

115-
if (compr->comp_mutex)
116-
mutex_lock(compr->comp_mutex);
117-
err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
118-
(unsigned int *)out_len);
119-
if (compr->comp_mutex)
120-
mutex_unlock(compr->comp_mutex);
121-
if (unlikely(err)) {
122-
ubifs_warn(c, "cannot compress %d bytes, compressor %s, error %d, leave data uncompressed",
123-
in_len, compr->name, err);
124-
goto no_compr;
126+
{
127+
ACOMP_REQUEST_ALLOC(req, compr->cc, GFP_NOFS | __GFP_NOWARN);
128+
129+
acomp_request_set_src_nondma(req, in_buf, in_len);
130+
err = ubifs_compress_req(c, req, out_buf, out_len, compr->name);
125131
}
126132

127-
/*
128-
* If the data compressed only slightly, it is better to leave it
129-
* uncompressed to improve read speed.
130-
*/
131-
if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF)
133+
if (err)
132134
goto no_compr;
133135

134136
return;
@@ -139,6 +141,31 @@ void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
139141
*compr_type = UBIFS_COMPR_NONE;
140142
}
141143

144+
static int ubifs_decompress_req(const struct ubifs_info *c,
145+
struct acomp_req *req,
146+
const void *in_buf, int in_len, int *out_len,
147+
const char *compr_name)
148+
{
149+
struct crypto_wait wait;
150+
int err;
151+
152+
crypto_init_wait(&wait);
153+
acomp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
154+
crypto_req_done, &wait);
155+
acomp_request_set_src_dma(req, in_buf, in_len);
156+
err = crypto_acomp_decompress(req);
157+
err = crypto_wait_req(err, &wait);
158+
*out_len = req->dlen;
159+
160+
if (err)
161+
ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
162+
in_len, compr_name, err);
163+
164+
acomp_request_free(req);
165+
166+
return err;
167+
}
168+
142169
/**
143170
* ubifs_decompress - decompress data.
144171
* @c: UBIFS file-system description object
@@ -155,7 +182,6 @@ void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
155182
int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
156183
int in_len, void *out_buf, int *out_len, int compr_type)
157184
{
158-
int err;
159185
struct ubifs_compressor *compr;
160186

161187
if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
@@ -176,17 +202,13 @@ int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
176202
return 0;
177203
}
178204

179-
if (compr->decomp_mutex)
180-
mutex_lock(compr->decomp_mutex);
181-
err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
182-
(unsigned int *)out_len);
183-
if (compr->decomp_mutex)
184-
mutex_unlock(compr->decomp_mutex);
185-
if (err)
186-
ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
187-
in_len, compr->name, err);
205+
{
206+
ACOMP_REQUEST_ALLOC(req, compr->cc, GFP_NOFS | __GFP_NOWARN);
188207

189-
return err;
208+
acomp_request_set_dst_nondma(req, out_buf, *out_len);
209+
return ubifs_decompress_req(c, req, in_buf, in_len, out_len,
210+
compr->name);
211+
}
190212
}
191213

192214
/**
@@ -199,7 +221,7 @@ int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
199221
static int __init compr_init(struct ubifs_compressor *compr)
200222
{
201223
if (compr->capi_name) {
202-
compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
224+
compr->cc = crypto_alloc_acomp(compr->capi_name, 0, 0);
203225
if (IS_ERR(compr->cc)) {
204226
pr_err("UBIFS error (pid %d): cannot initialize compressor %s, error %ld",
205227
current->pid, compr->name, PTR_ERR(compr->cc));
@@ -218,7 +240,7 @@ static int __init compr_init(struct ubifs_compressor *compr)
218240
static void compr_exit(struct ubifs_compressor *compr)
219241
{
220242
if (compr->capi_name)
221-
crypto_free_comp(compr->cc);
243+
crypto_free_acomp(compr->cc);
222244
}
223245

224246
/**

fs/ubifs/journal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ static int truncate_data_node(const struct ubifs_info *c, const struct inode *in
16251625
int err, dlen, compr_type, out_len, data_size;
16261626

16271627
out_len = le32_to_cpu(dn->size);
1628-
buf = kmalloc_array(out_len, WORST_COMPR_FACTOR, GFP_NOFS);
1628+
buf = kmalloc(out_len, GFP_NOFS);
16291629
if (!buf)
16301630
return -ENOMEM;
16311631

fs/ubifs/ubifs.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@
124124
#define OLD_ZNODE_AGE 20
125125
#define YOUNG_ZNODE_AGE 5
126126

127-
/*
128-
* Some compressors, like LZO, may end up with more data then the input buffer.
129-
* So UBIFS always allocates larger output buffer, to be sure the compressor
130-
* will not corrupt memory in case of worst case compression.
131-
*/
132-
#define WORST_COMPR_FACTOR 2
133-
134127
#ifdef CONFIG_FS_ENCRYPTION
135128
#define UBIFS_CIPHER_BLOCK_SIZE FSCRYPT_CONTENTS_ALIGNMENT
136129
#else
@@ -141,7 +134,7 @@
141134
* How much memory is needed for a buffer where we compress a data node.
142135
*/
143136
#define COMPRESSED_DATA_NODE_BUF_SZ \
144-
(UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR)
137+
(UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE)
145138

146139
/* Maximum expected tree height for use by bottom_up_buf */
147140
#define BOTTOM_UP_HEIGHT 64
@@ -835,16 +828,12 @@ struct ubifs_node_range {
835828
* struct ubifs_compressor - UBIFS compressor description structure.
836829
* @compr_type: compressor type (%UBIFS_COMPR_LZO, etc)
837830
* @cc: cryptoapi compressor handle
838-
* @comp_mutex: mutex used during compression
839-
* @decomp_mutex: mutex used during decompression
840831
* @name: compressor name
841832
* @capi_name: cryptoapi compressor name
842833
*/
843834
struct ubifs_compressor {
844835
int compr_type;
845-
struct crypto_comp *cc;
846-
struct mutex *comp_mutex;
847-
struct mutex *decomp_mutex;
836+
struct crypto_acomp *cc;
848837
const char *name;
849838
const char *capi_name;
850839
};

0 commit comments

Comments
 (0)