Skip to content

Commit 5e6bbde

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: introduce mempool for {,de}compress intermediate page allocation
If compression feature is on, in scenario of no enough free memory, page refault ratio is higher than before, the root cause is: - {,de}compression flow needs to allocate intermediate pages to store compressed data in cluster, so during their allocation, vm may reclaim mmaped pages. - if above reclaimed pages belong to compressed cluster, during its refault, it may cause more intermediate pages allocation, result in reclaiming more mmaped pages. So this patch introduces a mempool for intermediate page allocation, in order to avoid high refault ratio, by default, number of preallocated page in pool is 512, user can change the number by assigning 'num_compress_pages' parameter during module initialization. Ma Feng found warnings in the original patch and fixed like below. Fix the following sparse warning: fs/f2fs/compress.c:501:5: warning: symbol 'num_compress_pages' was not declared. Should it be static? fs/f2fs/compress.c:530:6: warning: symbol 'f2fs_compress_free_page' was not declared. Should it be static? Reported-by: Hulk Robot <hulkci@huawei.com> Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Ma Feng <mafeng.ma@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent c1c6338 commit 5e6bbde

3 files changed

Lines changed: 52 additions & 22 deletions

File tree

‎fs/f2fs/compress.c‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ static void f2fs_set_compressed_page(struct page *page,
6565
page->mapping = inode->i_mapping;
6666
}
6767

68-
static void f2fs_put_compressed_page(struct page *page)
69-
{
70-
set_page_private(page, (unsigned long)NULL);
71-
ClearPagePrivate(page);
72-
page->mapping = NULL;
73-
unlock_page(page);
74-
put_page(page);
75-
}
76-
7768
static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
7869
{
7970
int i;
@@ -476,17 +467,47 @@ bool f2fs_is_compress_backend_ready(struct inode *inode)
476467
return f2fs_cops[F2FS_I(inode)->i_compress_algorithm];
477468
}
478469

479-
static struct page *f2fs_grab_page(void)
470+
static mempool_t *compress_page_pool = NULL;
471+
static int num_compress_pages = 512;
472+
module_param(num_compress_pages, uint, 0444);
473+
MODULE_PARM_DESC(num_compress_pages,
474+
"Number of intermediate compress pages to preallocate");
475+
476+
int f2fs_init_compress_mempool(void)
477+
{
478+
compress_page_pool = mempool_create_page_pool(num_compress_pages, 0);
479+
if (!compress_page_pool)
480+
return -ENOMEM;
481+
482+
return 0;
483+
}
484+
485+
void f2fs_destroy_compress_mempool(void)
486+
{
487+
mempool_destroy(compress_page_pool);
488+
}
489+
490+
static struct page *f2fs_compress_alloc_page(void)
480491
{
481492
struct page *page;
482493

483-
page = alloc_page(GFP_NOFS);
484-
if (!page)
485-
return NULL;
494+
page = mempool_alloc(compress_page_pool, GFP_NOFS);
486495
lock_page(page);
496+
487497
return page;
488498
}
489499

500+
static void f2fs_compress_free_page(struct page *page)
501+
{
502+
if (!page)
503+
return;
504+
set_page_private(page, (unsigned long)NULL);
505+
ClearPagePrivate(page);
506+
page->mapping = NULL;
507+
unlock_page(page);
508+
mempool_free(page, compress_page_pool);
509+
}
510+
490511
static int f2fs_compress_pages(struct compress_ctx *cc)
491512
{
492513
struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
@@ -516,7 +537,7 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
516537
}
517538

518539
for (i = 0; i < cc->nr_cpages; i++) {
519-
cc->cpages[i] = f2fs_grab_page();
540+
cc->cpages[i] = f2fs_compress_alloc_page();
520541
if (!cc->cpages[i]) {
521542
ret = -ENOMEM;
522543
goto out_free_cpages;
@@ -561,7 +582,7 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
561582
vunmap(cc->rbuf);
562583

563584
for (i = nr_cpages; i < cc->nr_cpages; i++) {
564-
f2fs_put_compressed_page(cc->cpages[i]);
585+
f2fs_compress_free_page(cc->cpages[i]);
565586
cc->cpages[i] = NULL;
566587
}
567588

@@ -581,7 +602,7 @@ static int f2fs_compress_pages(struct compress_ctx *cc)
581602
out_free_cpages:
582603
for (i = 0; i < cc->nr_cpages; i++) {
583604
if (cc->cpages[i])
584-
f2fs_put_compressed_page(cc->cpages[i]);
605+
f2fs_compress_free_page(cc->cpages[i]);
585606
}
586607
kfree(cc->cpages);
587608
cc->cpages = NULL;
@@ -1183,7 +1204,7 @@ void f2fs_compress_write_end_io(struct bio *bio, struct page *page)
11831204
if (unlikely(bio->bi_status))
11841205
mapping_set_error(cic->inode->i_mapping, -EIO);
11851206

1186-
f2fs_put_compressed_page(page);
1207+
f2fs_compress_free_page(page);
11871208

11881209
dec_page_count(sbi, F2FS_WB_DATA);
11891210

@@ -1344,7 +1365,7 @@ struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
13441365
for (i = 0; i < dic->nr_cpages; i++) {
13451366
struct page *page;
13461367

1347-
page = f2fs_grab_page();
1368+
page = f2fs_compress_alloc_page();
13481369
if (!page)
13491370
goto out_free;
13501371

@@ -1364,7 +1385,7 @@ struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
13641385
continue;
13651386
}
13661387

1367-
dic->tpages[i] = f2fs_grab_page();
1388+
dic->tpages[i] = f2fs_compress_alloc_page();
13681389
if (!dic->tpages[i])
13691390
goto out_free;
13701391
}
@@ -1386,8 +1407,7 @@ void f2fs_free_dic(struct decompress_io_ctx *dic)
13861407
continue;
13871408
if (!dic->tpages[i])
13881409
continue;
1389-
unlock_page(dic->tpages[i]);
1390-
put_page(dic->tpages[i]);
1410+
f2fs_compress_free_page(dic->tpages[i]);
13911411
}
13921412
kfree(dic->tpages);
13931413
}
@@ -1396,7 +1416,7 @@ void f2fs_free_dic(struct decompress_io_ctx *dic)
13961416
for (i = 0; i < dic->nr_cpages; i++) {
13971417
if (!dic->cpages[i])
13981418
continue;
1399-
f2fs_put_compressed_page(dic->cpages[i]);
1419+
f2fs_compress_free_page(dic->cpages[i]);
14001420
}
14011421
kfree(dic->cpages);
14021422
}

‎fs/f2fs/f2fs.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3829,6 +3829,8 @@ bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
38293829
int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
38303830
void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
38313831
bool f2fs_is_compress_backend_ready(struct inode *inode);
3832+
int f2fs_init_compress_mempool(void);
3833+
void f2fs_destroy_compress_mempool(void);
38323834
void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity);
38333835
bool f2fs_cluster_is_empty(struct compress_ctx *cc);
38343836
bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
@@ -3862,6 +3864,8 @@ static inline struct page *f2fs_compress_control_page(struct page *page)
38623864
WARN_ON_ONCE(1);
38633865
return ERR_PTR(-EINVAL);
38643866
}
3867+
static inline int f2fs_init_compress_mempool(void) { return 0; }
3868+
static inline void f2fs_destroy_compress_mempool(void) { }
38653869
#endif
38663870

38673871
static inline void set_compress_context(struct inode *inode)

‎fs/f2fs/super.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3879,7 +3879,12 @@ static int __init init_f2fs_fs(void)
38793879
err = f2fs_init_bioset();
38803880
if (err)
38813881
goto free_bio_enrty_cache;
3882+
err = f2fs_init_compress_mempool();
3883+
if (err)
3884+
goto free_bioset;
38823885
return 0;
3886+
free_bioset:
3887+
f2fs_destroy_bioset();
38833888
free_bio_enrty_cache:
38843889
f2fs_destroy_bio_entry_cache();
38853890
free_post_read:
@@ -3907,6 +3912,7 @@ static int __init init_f2fs_fs(void)
39073912

39083913
static void __exit exit_f2fs_fs(void)
39093914
{
3915+
f2fs_destroy_compress_mempool();
39103916
f2fs_destroy_bioset();
39113917
f2fs_destroy_bio_entry_cache();
39123918
f2fs_destroy_post_read_processing();

0 commit comments

Comments
 (0)