Skip to content

Commit 3271d7e

Browse files
Fengnan ChangJaegeuk Kim
authored andcommitted
f2fs: compress: reduce one page array alloc and free when write compressed page
Don't alloc new page pointers array to replace old, just use old, introduce valid_nr_cpages to indicate valid number of page pointers in array, try to reduce one page array alloc and free when write compress page. Signed-off-by: Fengnan Chang <[email protected]> Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent 8ab7745 commit 3271d7e

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

fs/f2fs/compress.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
154154
cc->rpages = NULL;
155155
cc->nr_rpages = 0;
156156
cc->nr_cpages = 0;
157+
cc->valid_nr_cpages = 0;
157158
if (!reuse)
158159
cc->cluster_idx = NULL_CLUSTER;
159160
}
@@ -620,7 +621,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
620621
const struct f2fs_compress_ops *cops =
621622
f2fs_cops[fi->i_compress_algorithm];
622623
unsigned int max_len, new_nr_cpages;
623-
struct page **new_cpages;
624624
u32 chksum = 0;
625625
int i, ret;
626626

@@ -635,6 +635,7 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
635635

636636
max_len = COMPRESS_HEADER_SIZE + cc->clen;
637637
cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
638+
cc->valid_nr_cpages = cc->nr_cpages;
638639

639640
cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
640641
if (!cc->cpages) {
@@ -685,13 +686,6 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
685686

686687
new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
687688

688-
/* Now we're going to cut unnecessary tail pages */
689-
new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
690-
if (!new_cpages) {
691-
ret = -ENOMEM;
692-
goto out_vunmap_cbuf;
693-
}
694-
695689
/* zero out any unused part of the last page */
696690
memset(&cc->cbuf->cdata[cc->clen], 0,
697691
(new_nr_cpages * PAGE_SIZE) -
@@ -701,20 +695,16 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
701695
vm_unmap_ram(cc->rbuf, cc->cluster_size);
702696

703697
for (i = 0; i < cc->nr_cpages; i++) {
704-
if (i < new_nr_cpages) {
705-
new_cpages[i] = cc->cpages[i];
698+
if (i < new_nr_cpages)
706699
continue;
707-
}
708700
f2fs_compress_free_page(cc->cpages[i]);
709701
cc->cpages[i] = NULL;
710702
}
711703

712704
if (cops->destroy_compress_ctx)
713705
cops->destroy_compress_ctx(cc);
714706

715-
page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
716-
cc->cpages = new_cpages;
717-
cc->nr_cpages = new_nr_cpages;
707+
cc->valid_nr_cpages = new_nr_cpages;
718708

719709
trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
720710
cc->clen, ret);
@@ -1308,14 +1298,14 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
13081298

13091299
cic->magic = F2FS_COMPRESSED_PAGE_MAGIC;
13101300
cic->inode = inode;
1311-
atomic_set(&cic->pending_pages, cc->nr_cpages);
1301+
atomic_set(&cic->pending_pages, cc->valid_nr_cpages);
13121302
cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
13131303
if (!cic->rpages)
13141304
goto out_put_cic;
13151305

13161306
cic->nr_rpages = cc->cluster_size;
13171307

1318-
for (i = 0; i < cc->nr_cpages; i++) {
1308+
for (i = 0; i < cc->valid_nr_cpages; i++) {
13191309
f2fs_set_compressed_page(cc->cpages[i], inode,
13201310
cc->rpages[i + 1]->index, cic);
13211311
fio.compressed_page = cc->cpages[i];
@@ -1360,7 +1350,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
13601350
if (fio.compr_blocks && __is_valid_data_blkaddr(blkaddr))
13611351
fio.compr_blocks++;
13621352

1363-
if (i > cc->nr_cpages) {
1353+
if (i > cc->valid_nr_cpages) {
13641354
if (__is_valid_data_blkaddr(blkaddr)) {
13651355
f2fs_invalidate_blocks(sbi, blkaddr);
13661356
f2fs_update_data_blkaddr(&dn, NEW_ADDR);
@@ -1385,8 +1375,8 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
13851375

13861376
if (fio.compr_blocks)
13871377
f2fs_i_compr_blocks_update(inode, fio.compr_blocks - 1, false);
1388-
f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
1389-
add_compr_block_stat(inode, cc->nr_cpages);
1378+
f2fs_i_compr_blocks_update(inode, cc->valid_nr_cpages, true);
1379+
add_compr_block_stat(inode, cc->valid_nr_cpages);
13901380

13911381
set_inode_flag(cc->inode, FI_APPEND_WRITE);
13921382
if (cc->cluster_idx == 0)
@@ -1424,9 +1414,7 @@ static int f2fs_write_compressed_pages(struct compress_ctx *cc,
14241414
else
14251415
f2fs_unlock_op(sbi);
14261416
out_free:
1427-
for (i = 0; i < cc->nr_cpages; i++) {
1428-
if (!cc->cpages[i])
1429-
continue;
1417+
for (i = 0; i < cc->valid_nr_cpages; i++) {
14301418
f2fs_compress_free_page(cc->cpages[i]);
14311419
cc->cpages[i] = NULL;
14321420
}

fs/f2fs/data.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,6 +2987,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,
29872987
.rpages = NULL,
29882988
.nr_rpages = 0,
29892989
.cpages = NULL,
2990+
.valid_nr_cpages = 0,
29902991
.rbuf = NULL,
29912992
.cbuf = NULL,
29922993
.rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,

fs/f2fs/f2fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,7 @@ struct compress_ctx {
14861486
unsigned int nr_rpages; /* total page number in rpages */
14871487
struct page **cpages; /* pages store compressed data in cluster */
14881488
unsigned int nr_cpages; /* total page number in cpages */
1489+
unsigned int valid_nr_cpages; /* valid page number in cpages */
14891490
void *rbuf; /* virtual mapped address on rpages */
14901491
struct compress_data *cbuf; /* virtual mapped address on cpages */
14911492
size_t rlen; /* valid data length in rbuf */

0 commit comments

Comments
 (0)