Skip to content

Commit 3c12466

Browse files
committed
erofs: fix lz4 inplace decompression
Currently EROFS can map another compressed buffer for inplace decompression, that was used to handle the cases that some pages of compressed data are actually not in-place I/O. However, like most simple LZ77 algorithms, LZ4 expects the compressed data is arranged at the end of the decompressed buffer and it explicitly uses memmove() to handle overlapping: __________________________________________________________ |_ direction of decompression --> ____ |_ compressed data _| Although EROFS arranges compressed data like this, it typically maps two individual virtual buffers so the relative order is uncertain. Previously, it was hardly observed since LZ4 only uses memmove() for short overlapped literals and x86/arm64 memmove implementations seem to completely cover it up and they don't have this issue. Juhyung reported that EROFS data corruption can be found on a new Intel x86 processor. After some analysis, it seems that recent x86 processors with the new FSRM feature expose this issue with "rep movsb". Let's strictly use the decompressed buffer for lz4 inplace decompression for now. Later, as an useful improvement, we could try to tie up these two buffers together in the correct order. Reported-and-tested-by: Juhyung Park <qkrwngud825@gmail.com> Closes: https://lore.kernel.org/r/CAD14+f2AVKf8Fa2OO1aAUdDNTDsVzzR6ctU_oJSmTyd6zSYR2Q@mail.gmail.com Fixes: 0ffd71b ("staging: erofs: introduce LZ4 decompression inplace") Fixes: 598162d ("erofs: support decompress big pcluster for lz4 backend") Cc: stable <stable@vger.kernel.org> # 5.4+ Tested-by: Yifan Zhao <zhaoyifan@sjtu.edu.cn> Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com> Link: https://lore.kernel.org/r/20231206045534.3920847-1-hsiangkao@linux.alibaba.com
1 parent 93d6fda commit 3c12466

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

fs/erofs/decompressor.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ static int z_erofs_lz4_prepare_dstpages(struct z_erofs_lz4_decompress_ctx *ctx,
121121
}
122122

123123
static void *z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx *ctx,
124-
void *inpage, unsigned int *inputmargin, int *maptype,
125-
bool may_inplace)
124+
void *inpage, void *out, unsigned int *inputmargin,
125+
int *maptype, bool may_inplace)
126126
{
127127
struct z_erofs_decompress_req *rq = ctx->rq;
128-
unsigned int omargin, total, i, j;
128+
unsigned int omargin, total, i;
129129
struct page **in;
130130
void *src, *tmp;
131131

@@ -135,20 +135,20 @@ static void *z_erofs_lz4_handle_overlap(struct z_erofs_lz4_decompress_ctx *ctx,
135135
omargin < LZ4_DECOMPRESS_INPLACE_MARGIN(rq->inputsize))
136136
goto docopy;
137137

138-
for (i = 0; i < ctx->inpages; ++i) {
139-
DBG_BUGON(rq->in[i] == NULL);
140-
for (j = 0; j < ctx->outpages - ctx->inpages + i; ++j)
141-
if (rq->out[j] == rq->in[i])
142-
goto docopy;
143-
}
138+
for (i = 0; i < ctx->inpages; ++i)
139+
if (rq->out[ctx->outpages - ctx->inpages + i] !=
140+
rq->in[i])
141+
goto docopy;
142+
kunmap_local(inpage);
143+
*maptype = 3;
144+
return out + ((ctx->outpages - ctx->inpages) << PAGE_SHIFT);
144145
}
145146

146147
if (ctx->inpages <= 1) {
147148
*maptype = 0;
148149
return inpage;
149150
}
150151
kunmap_local(inpage);
151-
might_sleep();
152152
src = erofs_vm_map_ram(rq->in, ctx->inpages);
153153
if (!src)
154154
return ERR_PTR(-ENOMEM);
@@ -204,12 +204,12 @@ int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
204204
}
205205

206206
static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx,
207-
u8 *out)
207+
u8 *dst)
208208
{
209209
struct z_erofs_decompress_req *rq = ctx->rq;
210210
bool support_0padding = false, may_inplace = false;
211211
unsigned int inputmargin;
212-
u8 *headpage, *src;
212+
u8 *out, *headpage, *src;
213213
int ret, maptype;
214214

215215
DBG_BUGON(*rq->in == NULL);
@@ -230,11 +230,12 @@ static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx,
230230
}
231231

232232
inputmargin = rq->pageofs_in;
233-
src = z_erofs_lz4_handle_overlap(ctx, headpage, &inputmargin,
233+
src = z_erofs_lz4_handle_overlap(ctx, headpage, dst, &inputmargin,
234234
&maptype, may_inplace);
235235
if (IS_ERR(src))
236236
return PTR_ERR(src);
237237

238+
out = dst + rq->pageofs_out;
238239
/* legacy format could compress extra data in a pcluster. */
239240
if (rq->partial_decoding || !support_0padding)
240241
ret = LZ4_decompress_safe_partial(src + inputmargin, out,
@@ -265,7 +266,7 @@ static int z_erofs_lz4_decompress_mem(struct z_erofs_lz4_decompress_ctx *ctx,
265266
vm_unmap_ram(src, ctx->inpages);
266267
} else if (maptype == 2) {
267268
erofs_put_pcpubuf(src);
268-
} else {
269+
} else if (maptype != 3) {
269270
DBG_BUGON(1);
270271
return -EFAULT;
271272
}
@@ -308,7 +309,7 @@ static int z_erofs_lz4_decompress(struct z_erofs_decompress_req *rq,
308309
}
309310

310311
dstmap_out:
311-
ret = z_erofs_lz4_decompress_mem(&ctx, dst + rq->pageofs_out);
312+
ret = z_erofs_lz4_decompress_mem(&ctx, dst);
312313
if (!dst_maptype)
313314
kunmap_local(dst);
314315
else if (dst_maptype == 2)

0 commit comments

Comments
 (0)