Skip to contentSkip to conversation

reclaim file-backed pages given POSIX_FADV_NOREUSE

PATCH Applied Jaegeuk Kim 4 participants 8 messages Feb 12, 2025 – Feb 21, 2025

Subsystems (4)
lore
Share & tools
lore

Jaegeuk Kim

author maintainer
#0 lore raw

v9 cover reclaim file-backed pages given POSIX_FADV_NOREUSE

This patch series does not add new API, but implements POSIX_FADV_NOREUSE where it keeps the page ranges in the f2fs superblock and add a way for users to reclaim the pages manually.

Change log from v8:
- remove new APIs, but use fadvise(POSIX_FADV_NOREUSE)

Jaegeuk Kim (2):
f2fs: keep POSIX_FADV_NOREUSE ranges
f2fs: add a sysfs entry to reclaim POSIX_FADV_NOREUSE pages

 Documentation/ABI/testing/sysfs-fs-f2fs |  7 ++
 fs/f2fs/debug.c                         |  3 +
 fs/f2fs/f2fs.h                          | 14 +++-
 fs/f2fs/file.c                          | 60 +++++++++++++++--
 fs/f2fs/inode.c                         | 14 ++++
 fs/f2fs/shrinker.c                      | 90 +++++++++++++++++++++++++
 fs/f2fs/super.c                         |  1 +
 fs/f2fs/sysfs.c                         | 63 +++++++++++++++++
 8 files changed, 246 insertions(+), 6 deletions(-)
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
author maintainer
#1 lore raw

PATCH v9 1/2 f2fs: keep POSIX_FADV_NOREUSE ranges

This patch records POSIX_FADV_NOREUSE ranges for users to reclaim the caches instantly off from LRU.

---
 fs/f2fs/debug.c |  3 +++
 fs/f2fs/f2fs.h  | 12 +++++++++-
 fs/f2fs/file.c  | 60 ++++++++++++++++++++++++++++++++++++++++++++-----
 fs/f2fs/inode.c | 14 ++++++++++++
 fs/f2fs/super.c |  1 +
 5 files changed, 84 insertions(+), 6 deletions(-)
fs/f2fs/debug.c +3 -0
index 468828288a4a..16c2dfb4f595 100644
@@ -164,6 +164,7 @@ static void update_general_status(struct f2fs_sb_info *sbi)
 	si->ndirty_imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
 	si->ndirty_dirs = sbi->ndirty_inode[DIR_INODE];
 	si->ndirty_files = sbi->ndirty_inode[FILE_INODE];
+	si->ndonate_files = sbi->donate_files;
 	si->nquota_files = sbi->nquota_files;
 	si->ndirty_all = sbi->ndirty_inode[DIRTY_META];
 	si->aw_cnt = atomic_read(&sbi->atomic_files);
@@ -501,6 +502,8 @@ static int stat_show(struct seq_file *s, void *v)
 			   si->compr_inode, si->compr_blocks);
 		seq_printf(s, "  - Swapfile Inode: %u\n",
 			   si->swapfile_inode);
+		seq_printf(s, "  - Donate Inode: %u\n",
+			   si->ndonate_files);
 		seq_printf(s, "  - Orphan/Append/Update Inode: %u, %u, %u\n",
 			   si->orphans, si->append, si->update);
 		seq_printf(s, "\nMain area: %d segs, %d secs %d zones\n",
fs/f2fs/f2fs.h +11 -1
index 395f9d37449c..3abcb84a0d47 100644
@@ -850,6 +850,11 @@ struct f2fs_inode_info {
 #endif
 	struct list_head dirty_list;	/* dirty list for dirs and files */
 	struct list_head gdirty_list;	/* linked in global dirty list */
+
+	/* linked in global inode list for cache donation */
+	struct list_head gdonate_list;
+	pgoff_t donate_start, donate_end; /* inclusive */
+
 	struct task_struct *atomic_write_task;	/* store atomic write task */
 	struct extent_tree *extent_tree[NR_EXTENT_CACHES];
 					/* cached extent_tree entry */
@@ -1274,6 +1279,7 @@ enum inode_type {
 	DIR_INODE,			/* for dirty dir inode */
 	FILE_INODE,			/* for dirty regular/symlink inode */
 	DIRTY_META,			/* for all dirtied inode metadata */
+	DONATE_INODE,			/* for all inode to donate pages */
 	NR_INODE_TYPE,
 };
 
@@ -1629,6 +1635,9 @@ struct f2fs_sb_info {
 	unsigned int warm_data_age_threshold;
 	unsigned int last_age_weight;
 
+	/* control donate caches */
+	unsigned int donate_files;
+
 	/* basic filesystem units */
 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
 	unsigned int log_blocksize;		/* log2 block size */
@@ -3968,7 +3977,8 @@ struct f2fs_stat_info {
 	unsigned long long allocated_data_blocks;
 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
 	int ndirty_data, ndirty_qdata;
-	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
+	unsigned int ndirty_dirs, ndirty_files, ndirty_all;
+	unsigned int nquota_files, ndonate_files;
 	int nats, dirty_nats, sits, dirty_sits;
 	int free_nids, avail_nids, alloc_nids;
 	int total_count, utilization;
fs/f2fs/file.c +55 -5

73 lines not on this page · raw message

fs/f2fs/inode.c +14 -0
index 3f03b1c9b7f1..543fb942bec1 100644
@@ -814,6 +814,19 @@ int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
 	return 0;
 }
 
+static void f2fs_remove_donate_inode(struct inode *inode)
+{
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+
+	if (list_empty(&F2FS_I(inode)->gdonate_list))
+		return;
+
+	spin_lock(&sbi->inode_lock[DONATE_INODE]);
+	list_del_init(&F2FS_I(inode)->gdonate_list);
+	sbi->donate_files--;
+	spin_unlock(&sbi->inode_lock[DONATE_INODE]);
+}
+
 /*
  * Called at the last iput() if i_nlink is zero
  */
@@ -848,6 +861,7 @@ void f2fs_evict_inode(struct inode *inode)
 
 	f2fs_bug_on(sbi, get_dirty_pages(inode));
 	f2fs_remove_dirty_inode(inode);
+	f2fs_remove_donate_inode(inode);
 
 	if (!IS_DEVICE_ALIASING(inode))
 		f2fs_destroy_extent_tree(inode);
fs/f2fs/super.c +1 -0
index 759b642991d4..f5c69cc2de72 100644
@@ -1441,6 +1441,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb)
 	spin_lock_init(&fi->i_size_lock);
 	INIT_LIST_HEAD(&fi->dirty_list);
 	INIT_LIST_HEAD(&fi->gdirty_list);
+	INIT_LIST_HEAD(&fi->gdonate_list);
 	init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
 	init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
 	init_f2fs_rwsem(&fi->i_xattr_sem);
Signature · 1 line
2.48.1.601.g30ceb7b040-goog
maintainer Reviewed-by on v9 1/2
#2 lore raw

Re: [f2fs-dev] [PATCH 1/2] f2fs: keep POSIX_FADV_NOREUSE ranges

Quoted 4 lines · On 2/12/25 10:31, Jaegeuk Kim via Linux-f2fs-devel wrote:

This patch records POSIX_FADV_NOREUSE ranges for users to reclaim the caches instantly off from LRU.

Thanks,

author maintainer
#3 lore raw

PATCH v9 2/2 f2fs: add a sysfs entry to reclaim POSIX_FADV_NOREUSE pages

In reply to Jaegeuk Kim

1. fadvise(fd1, POSIX_FADV_NOREUSE, {0,3});
2. fadvise(fd2, POSIX_FADV_NOREUSE, {1,2});
3. fadvise(fd3, POSIX_FADV_NOREUSE, {3,1});
4. echo 1024 > /sys/fs/f2fs/tuning/reclaim_caches_kb

This gives a way to reclaim file-backed pages by iterating all f2fs mounts until reclaiming 1MB page cache ranges, registered by #1, #2, and #3.

5. cat /sys/fs/f2fs/tuning/reclaim_caches_kb
-> gives total number of registered file ranges.

---
 Documentation/ABI/testing/sysfs-fs-f2fs |  7 ++
 fs/f2fs/f2fs.h                          |  2 +
 fs/f2fs/shrinker.c                      | 90 +++++++++++++++++++++++++
 fs/f2fs/sysfs.c                         | 63 +++++++++++++++++
 4 files changed, 162 insertions(+)
Documentation/ABI/testing/sysfs-fs-f2fs +7 -0
index 3e1630c70d8a..81deae2af84d 100644
@@ -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/tuning/reclaim_caches_kb
+Date:		February 2025
+Contact:	"Jaegeuk Kim" <[email protected]>
+Description:	It reclaims the given KBs of file-backed pages registered by
+		ioctl(F2FS_IOC_DONATE_RANGE).
+		For example, writing N tries to drop N KBs spaces in LRU.
fs/f2fs/f2fs.h +2 -0
index 3abcb84a0d47..05879c6dc4d6 100644
@@ -4243,6 +4243,8 @@ unsigned long f2fs_shrink_count(struct shrinker *shrink,
 			struct shrink_control *sc);
 unsigned long f2fs_shrink_scan(struct shrinker *shrink,
 			struct shrink_control *sc);
+unsigned int f2fs_donate_files(void);
+void f2fs_reclaim_caches(unsigned int reclaim_caches_kb);
 void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
 void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
 
fs/f2fs/shrinker.c +90 -0

96 lines not on this page · raw message

fs/f2fs/sysfs.c +63 -0

100 lines not on this page · raw message

Signature · 1 line
2.48.1.601.g30ceb7b040-goog
maintainer Reviewed-by on v9 2/2
#4 lore raw

Re: [f2fs-dev] [PATCH 2/2] f2fs: add a sysfs entry to reclaim POSIX_FADV_NOREUSE pages

Quoted 5 lines · On 2/12/25 10:31, Jaegeuk Kim via Linux-f2fs-devel wrote:

1. fadvise(fd1, POSIX_FADV_NOREUSE, {0,3});
2. fadvise(fd2, POSIX_FADV_NOREUSE, {1,2});
3. fadvise(fd3, POSIX_FADV_NOREUSE, {3,1});
4. echo 1024 > /sys/fs/f2fs/tuning/reclaim_caches_kb

Quoted 7 lines

This gives a way to reclaim file-backed pages by iterating all f2fs mounts until reclaiming 1MB page cache ranges, registered by #1, #2, and #3.

5. cat /sys/fs/f2fs/tuning/reclaim_caches_kb
-> gives total number of registered file ranges.

Thanks,

#5 lore raw
In reply to Jaegeuk Kim

This still has a file system sysfs HACK, you're still not Ccing the right list, etc.

Can you pleae at least try to get it right?

Quoted 11 lines · Jaegeuk Kim

This patch series does not add new API, but implements POSIX_FADV_NOREUSE where it keeps the page ranges in the f2fs superblock and add a way for users to reclaim the pages manually.

Change log from v8:
- remove new APIs, but use fadvise(POSIX_FADV_NOREUSE)

Jaegeuk Kim (2):
f2fs: keep POSIX_FADV_NOREUSE ranges
f2fs: add a sysfs entry to reclaim POSIX_FADV_NOREUSE pages

Quoted 14 lines
 Documentation/ABI/testing/sysfs-fs-f2fs |  7 ++
 fs/f2fs/debug.c                         |  3 +
 fs/f2fs/f2fs.h                          | 14 +++-
 fs/f2fs/file.c                          | 60 +++++++++++++++--
 fs/f2fs/inode.c                         | 14 ++++
 fs/f2fs/shrinker.c                      | 90 +++++++++++++++++++++++++
 fs/f2fs/super.c                         |  1 +
 fs/f2fs/sysfs.c                         | 63 +++++++++++++++++
 8 files changed, 246 insertions(+), 6 deletions(-)
Signature · 1 line
2.48.1.601.g30ceb7b040-goog

---end quoted text---

author maintainer
#6 lore raw
Quoted 4 lines · Christoph Hellwig

This still has a file system sysfs HACK, you're still not Ccing the right list, etc.

Can you pleae at least try to get it right?

I was modifying the patch having 1) declaring a static global list, 2) adding some fields to superblock and inode structures to keep the given range in the inode through fadvise, 3) adding hooks in evict_inode to handle the list, 4) exploring which sysfs entry in MM to reclaim them explicitly.

But, I stopped at some point, as it looks not good at all. Moreover, I started to be questioning why not just doing in F2FS back, given sementically I didn't change anything on general behavior of fadvise(POSIX_FADV_NOREUSE), IIUC, which moves pages back to LRU. In addiiton to that, I'd like to keep the range hint in a filesystem and provide a sysfs entry to manage the hints additionally. In addition, I don't think there's rule that filesystem cannot reclaim file-back pages, as it just uses the exported symbol that all filesystems are using in various different purpose. Hence, I don't get the point which is wrong.

Thanks,

Quoted 28 lines · Jaegeuk Kim
Quoted 11 lines

This patch series does not add new API, but implements POSIX_FADV_NOREUSE where it keeps the page ranges in the f2fs superblock and add a way for users to reclaim the pages manually.

Change log from v8:
- remove new APIs, but use fadvise(POSIX_FADV_NOREUSE)

Jaegeuk Kim (2):
f2fs: keep POSIX_FADV_NOREUSE ranges
f2fs: add a sysfs entry to reclaim POSIX_FADV_NOREUSE pages

Quoted 14 lines
 Documentation/ABI/testing/sysfs-fs-f2fs |  7 ++
 fs/f2fs/debug.c                         |  3 +
 fs/f2fs/f2fs.h                          | 14 +++-
 fs/f2fs/file.c                          | 60 +++++++++++++++--
 fs/f2fs/inode.c                         | 14 ++++
 fs/f2fs/shrinker.c                      | 90 +++++++++++++++++++++++++
 fs/f2fs/super.c                         |  1 +
 fs/f2fs/sysfs.c                         | 63 +++++++++++++++++
 8 files changed, 246 insertions(+), 6 deletions(-)
Signature · 1 line
2.48.1.601.g30ceb7b040-goog

---end quoted text---

#7 lore raw
In reply to Jaegeuk Kim
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:

Hello:

This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:

Quoted 8 lines · On Wed, 12 Feb 2025 02:31:55 +0000 you wrote:

This patch series does not add new API, but implements POSIX_FADV_NOREUSE where it keeps the page ranges in the f2fs superblock and add a way for users to reclaim the pages manually.

Change log from v8:
- remove new APIs, but use fadvise(POSIX_FADV_NOREUSE)

[...]

Here is the summary with links:
- [f2fs-dev,1/2] f2fs: keep POSIX_FADV_NOREUSE ranges

    https://git.kernel.org/jaegeuk/f2fs/c/ef0c333cad8d
  - [f2fs-dev,2/2] f2fs: add a sysfs entry to reclaim POSIX_FADV_NOREUSE pages
    https://git.kernel.org/jaegeuk/f2fs/c/a907f3a68ee2

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html