Skip to content

Commit

Permalink
f2fs: add a sysfs entry to request donate file-backed pages
Browse files Browse the repository at this point in the history
1. ioctl(fd1, F2FS_IOC_DONATE_RANGE, {0,3});
2. ioctl(fd2, F2FS_IOC_DONATE_RANGE, {1,2});
3. ioctl(fd3, F2FS_IOC_DONATE_RANGE, {3,1});
4. echo 3 > /sys/fs/f2fs/blk/donate_caches

will reclaim 3 page cache ranges, registered by #1, #2, and #3.

Signed-off-by: Jaegeuk Kim <[email protected]>
  • Loading branch information
Jaegeuk Kim committed Jan 8, 2025
1 parent d2e7bc7 commit 9ea74b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Documentation/ABI/testing/sysfs-fs-f2fs
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,10 @@ Date: November 2024
Contact: "Chao Yu" <[email protected]>
Description: It controls max read extent count for per-inode, the value of threshold
is 10240 by default.

What: /sys/fs/f2fs/<disk>/donate_caches
Date: December 2024
Contact: "Jaegeuk Kim" <[email protected]>
Description: It reclaims the certian file-backed pages registered by
ioctl(F2FS_IOC_DONATE_RANGE).
For example, writing N tries to drop N address spaces in LRU.
4 changes: 4 additions & 0 deletions fs/f2fs/f2fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,9 @@ struct f2fs_sb_info {
unsigned int warm_data_age_threshold;
unsigned int last_age_weight;

/* control donate caches */
unsigned int donate_caches;

/* basic filesystem units */
unsigned int log_sectors_per_block; /* log2 sectors per block */
unsigned int log_blocksize; /* log2 block size */
Expand Down Expand Up @@ -4256,6 +4259,7 @@ unsigned long f2fs_shrink_count(struct shrinker *shrink,
struct shrink_control *sc);
unsigned long f2fs_shrink_scan(struct shrinker *shrink,
struct shrink_control *sc);
void f2fs_donate_caches(struct f2fs_sb_info *sbi);
void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);

Expand Down
27 changes: 27 additions & 0 deletions fs/f2fs/shrinker.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@ unsigned long f2fs_shrink_scan(struct shrinker *shrink,
return freed;
}

void f2fs_donate_caches(struct f2fs_sb_info *sbi)
{
struct inode *inode = NULL;
struct f2fs_inode_info *fi;
int nfiles = sbi->donate_caches;
next:
spin_lock(&sbi->inode_lock[DONATE_INODE]);
if (list_empty(&sbi->inode_list[DONATE_INODE]) || !nfiles) {
spin_unlock(&sbi->inode_lock[DONATE_INODE]);
return;
}

fi = list_first_entry(&sbi->inode_list[DONATE_INODE],
struct f2fs_inode_info, gdonate_list);
list_move_tail(&fi->gdonate_list, &sbi->inode_list[DONATE_INODE]);
inode = igrab(&fi->vfs_inode);
spin_unlock(&sbi->inode_lock[DONATE_INODE]);

if (inode) {
invalidate_inode_pages2_range(inode->i_mapping,
fi->donate_start, fi->donate_end);
iput(inode);
}
if (nfiles--)
goto next;
}

void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
{
spin_lock(&f2fs_list_lock);
Expand Down
8 changes: 8 additions & 0 deletions fs/f2fs/sysfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@ static ssize_t __sbi_store(struct f2fs_attr *a,
return count;
}

if (!strcmp(a->attr.name, "donate_caches")) {
sbi->donate_caches = min(t, sbi->ndirty_inode[DONATE_INODE]);
f2fs_donate_caches(sbi);
return count;
}

*ui = (unsigned int)t;

return count;
Expand Down Expand Up @@ -1030,6 +1036,7 @@ F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
F2FS_SBI_GENERAL_RW_ATTR(migration_window_granularity);
F2FS_SBI_GENERAL_RW_ATTR(dir_level);
F2FS_SBI_GENERAL_RW_ATTR(donate_caches);
#ifdef CONFIG_F2FS_IOSTAT
F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
Expand Down Expand Up @@ -1178,6 +1185,7 @@ static struct attribute *f2fs_attrs[] = {
ATTR_LIST(migration_granularity),
ATTR_LIST(migration_window_granularity),
ATTR_LIST(dir_level),
ATTR_LIST(donate_caches),
ATTR_LIST(ram_thresh),
ATTR_LIST(ra_nid_pages),
ATTR_LIST(dirty_nats_ratio),
Expand Down

0 comments on commit 9ea74b4

Please sign in to comment.