Skip to content

Commit 55276e1

Browse files
committed
Revert "btrfs: compression: drop kmap/kunmap from zlib"
This reverts commit 696ab56. The kmaps in compression code are still needed and cause crashes on 32bit machines (ARM, x86). Reproducible eg. by running fstest btrfs/004 with enabled LZO or ZSTD compression. Link: https://lore.kernel.org/all/CAJCQCtT+OuemovPO7GZk8Y8=qtOObr0XTDp8jh4OHD6y84AFxw@mail.gmail.com/ Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=214839 Signed-off-by: David Sterba <[email protected]>
1 parent 56ee254 commit 55276e1

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

fs/btrfs/zlib.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
126126
ret = -ENOMEM;
127127
goto out;
128128
}
129-
cpage_out = page_address(out_page);
129+
cpage_out = kmap(out_page);
130130
pages[0] = out_page;
131131
nr_pages = 1;
132132

@@ -148,22 +148,26 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
148148
int i;
149149

150150
for (i = 0; i < in_buf_pages; i++) {
151-
if (in_page)
151+
if (in_page) {
152+
kunmap(in_page);
152153
put_page(in_page);
154+
}
153155
in_page = find_get_page(mapping,
154156
start >> PAGE_SHIFT);
155-
data_in = page_address(in_page);
157+
data_in = kmap(in_page);
156158
memcpy(workspace->buf + i * PAGE_SIZE,
157159
data_in, PAGE_SIZE);
158160
start += PAGE_SIZE;
159161
}
160162
workspace->strm.next_in = workspace->buf;
161163
} else {
162-
if (in_page)
164+
if (in_page) {
165+
kunmap(in_page);
163166
put_page(in_page);
167+
}
164168
in_page = find_get_page(mapping,
165169
start >> PAGE_SHIFT);
166-
data_in = page_address(in_page);
170+
data_in = kmap(in_page);
167171
start += PAGE_SIZE;
168172
workspace->strm.next_in = data_in;
169173
}
@@ -192,6 +196,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
192196
* the stream end if required
193197
*/
194198
if (workspace->strm.avail_out == 0) {
199+
kunmap(out_page);
195200
if (nr_pages == nr_dest_pages) {
196201
out_page = NULL;
197202
ret = -E2BIG;
@@ -202,7 +207,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
202207
ret = -ENOMEM;
203208
goto out;
204209
}
205-
cpage_out = page_address(out_page);
210+
cpage_out = kmap(out_page);
206211
pages[nr_pages] = out_page;
207212
nr_pages++;
208213
workspace->strm.avail_out = PAGE_SIZE;
@@ -229,6 +234,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
229234
goto out;
230235
} else if (workspace->strm.avail_out == 0) {
231236
/* get another page for the stream end */
237+
kunmap(out_page);
232238
if (nr_pages == nr_dest_pages) {
233239
out_page = NULL;
234240
ret = -E2BIG;
@@ -239,7 +245,7 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
239245
ret = -ENOMEM;
240246
goto out;
241247
}
242-
cpage_out = page_address(out_page);
248+
cpage_out = kmap(out_page);
243249
pages[nr_pages] = out_page;
244250
nr_pages++;
245251
workspace->strm.avail_out = PAGE_SIZE;
@@ -258,8 +264,13 @@ int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
258264
*total_in = workspace->strm.total_in;
259265
out:
260266
*out_pages = nr_pages;
261-
if (in_page)
267+
if (out_page)
268+
kunmap(out_page);
269+
270+
if (in_page) {
271+
kunmap(in_page);
262272
put_page(in_page);
273+
}
263274
return ret;
264275
}
265276

@@ -276,7 +287,7 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
276287
unsigned long buf_start;
277288
struct page **pages_in = cb->compressed_pages;
278289

279-
data_in = page_address(pages_in[page_in_index]);
290+
data_in = kmap(pages_in[page_in_index]);
280291
workspace->strm.next_in = data_in;
281292
workspace->strm.avail_in = min_t(size_t, srclen, PAGE_SIZE);
282293
workspace->strm.total_in = 0;
@@ -298,6 +309,7 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
298309

299310
if (Z_OK != zlib_inflateInit2(&workspace->strm, wbits)) {
300311
pr_warn("BTRFS: inflateInit failed\n");
312+
kunmap(pages_in[page_in_index]);
301313
return -EIO;
302314
}
303315
while (workspace->strm.total_in < srclen) {
@@ -324,13 +336,13 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
324336

325337
if (workspace->strm.avail_in == 0) {
326338
unsigned long tmp;
327-
339+
kunmap(pages_in[page_in_index]);
328340
page_in_index++;
329341
if (page_in_index >= total_pages_in) {
330342
data_in = NULL;
331343
break;
332344
}
333-
data_in = page_address(pages_in[page_in_index]);
345+
data_in = kmap(pages_in[page_in_index]);
334346
workspace->strm.next_in = data_in;
335347
tmp = srclen - workspace->strm.total_in;
336348
workspace->strm.avail_in = min(tmp, PAGE_SIZE);
@@ -342,6 +354,8 @@ int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
342354
ret = 0;
343355
done:
344356
zlib_inflateEnd(&workspace->strm);
357+
if (data_in)
358+
kunmap(pages_in[page_in_index]);
345359
if (!ret)
346360
zero_fill_bio(cb->orig_bio);
347361
return ret;

0 commit comments

Comments
 (0)