Skip to contentSkip to conversation

add ioctl/sysfs to donate file-backed pages

PATCH Superseded Jaegeuk Kim 3 participants 9 messages Jan 15, 2025 – Jan 17, 2025

Subsystems (5)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Jaegeuk Kim

author maintainer
#0 lore raw

v3 cover add ioctl/sysfs to donate file-backed pages

If users clearly know which file-backed pages to reclaim in system view, they can use this ioctl() to register in advance and reclaim all at once later.

Change log from v2:
- add more boundary checks
- de-register the range, if len is zero

Jaegeuk Kim (2):
f2fs: register inodes which is able to donate pages
f2fs: add a sysfs entry to request donate file-backed pages

 Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++
 fs/f2fs/debug.c                         |  3 ++
 fs/f2fs/f2fs.h                          | 14 +++++-
 fs/f2fs/file.c                          | 64 +++++++++++++++++++++++++
 fs/f2fs/inode.c                         | 14 ++++++
 fs/f2fs/shrinker.c                      | 27 +++++++++++
 fs/f2fs/super.c                         |  1 +
 fs/f2fs/sysfs.c                         |  8 ++++
 include/uapi/linux/f2fs.h               |  7 +++
 9 files changed, 144 insertions(+), 1 deletion(-)
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author maintainer
#1 lore raw

PATCH v3 1/2 f2fs: register inodes which is able to donate pages

This patch introduces an inode list to keep the page cache ranges that users can donate pages together.

#define F2FS_IOC_DONATE_RANGE _IOW(F2FS_IOCTL_MAGIC, 27, \
struct f2fs_donate_range)

 struct f2fs_donate_range {
	__u64 start;
	__u64 len;

};

e.g., ioctl(F2FS_IOC_DONATE_RANGE, &range);

---
 fs/f2fs/debug.c           |  3 ++
 fs/f2fs/f2fs.h            | 12 +++++++-
 fs/f2fs/file.c            | 64 +++++++++++++++++++++++++++++++++++++++
 fs/f2fs/inode.c           | 14 +++++++++
 fs/f2fs/super.c           |  1 +
 include/uapi/linux/f2fs.h |  7 +++++
 6 files changed, 100 insertions(+), 1 deletion(-)
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 4bfe162eefd3..951fbc3f94c7 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;
+	loff_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 */
@@ -3984,7 +3993,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 +64 -0

82 lines not on this page · raw message

fs/f2fs/inode.c +14 -0
index 7de33da8b3ea..f9fc58f313f2 100644
@@ -804,6 +804,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
  */
@@ -838,6 +851,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 fc7d463dee15..ef639a6d82e5 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);
include/uapi/linux/f2fs.h +7 -0
index f7aaf8d23e20..cd38a7c166e6 100644
@@ -44,6 +44,8 @@
 #define F2FS_IOC_COMPRESS_FILE		_IO(F2FS_IOCTL_MAGIC, 24)
 #define F2FS_IOC_START_ATOMIC_REPLACE	_IO(F2FS_IOCTL_MAGIC, 25)
 #define F2FS_IOC_GET_DEV_ALIAS_FILE	_IOR(F2FS_IOCTL_MAGIC, 26, __u32)
+#define F2FS_IOC_DONATE_RANGE		_IOW(F2FS_IOCTL_MAGIC, 27,	\
+						struct f2fs_donate_range)
 
 /*
  * should be same as XFS_IOC_GOINGDOWN.
@@ -97,4 +99,9 @@ struct f2fs_comp_option {
 	__u8 log_cluster_size;
 };
 
+struct f2fs_donate_range {
+	__u64 start;
+	__u64 len;
+};
+
 #endif /* _UAPI_LINUX_F2FS_H */
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
maintainer
#2 lore raw

Re: [f2fs-dev] [PATCH 1/2] f2fs: register inodes which is able to donate pages

Quoted 106 lines · On 1/16/25 06:16, Jaegeuk Kim via Linux-f2fs-devel wrote:

This patch introduces an inode list to keep the page cache ranges that users can donate pages together.

#define F2FS_IOC_DONATE_RANGE _IOW(F2FS_IOCTL_MAGIC, 27, \
struct f2fs_donate_range)

  struct f2fs_donate_range {
	__u64 start;
	__u64 len;

};

e.g., ioctl(F2FS_IOC_DONATE_RANGE, &range);

---
  fs/f2fs/debug.c           |  3 ++
  fs/f2fs/f2fs.h            | 12 +++++++-
  fs/f2fs/file.c            | 64 +++++++++++++++++++++++++++++++++++++++
  fs/f2fs/inode.c           | 14 +++++++++
  fs/f2fs/super.c           |  1 +
  include/uapi/linux/f2fs.h |  7 +++++
  6 files changed, 100 insertions(+), 1 deletion(-)
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 4bfe162eefd3..951fbc3f94c7 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;
+	loff_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 */
@@ -3984,7 +3993,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 +11 -0
index 81764b10840b..6d071605b0cd 100644
@@ -2429,6 +2429,67 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
  	return ret;
  }
 
+static int f2fs_ioc_donate_range(struct file *filp, unsigned long arg)
+{
+	struct inode *inode = file_inode(filp);
+	struct mnt_idmap *idmap = file_mnt_idmap(filp);
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+	struct f2fs_donate_range range;
+	u64 max_pages = F2FS_BLK_TO_BYTES(max_file_blocks(inode)) >> PAGE_SHIFT;
+	int ret;
+
+	if (copy_from_user(&range, (struct f2fs_donate_range __user *)arg,
+							sizeof(range)))

fs/f2fs/file.c static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) · line 2452

Quoted 10 lines
fs/f2fs/file.c +10 -0
+		return -EFAULT;
+
+	if (!inode_owner_or_capable(idmap, inode))
+		return -EACCES;
+
+	if (!S_ISREG(inode->i_mode))
+		return -EINVAL;
+
+	range.start >>= PAGE_SHIFT;
+	range.len = DIV_ROUND_UP(range.len, PAGE_SIZE);

e.g.

range.start = 2048
range.len = 4096

The range is page #[0, 1]

after calculation,

range.start = 0
range.len = 1

The range is shrunk to page #[0, 0]? IIUC.

Thanks,

Quoted 127 lines
fs/f2fs/file.c +43 -0
+
+	if (range.start >= max_pages || range.len > max_pages ||
+	    (range.start + range.len) > max_pages)
+		return -EINVAL;
+
+	ret = mnt_want_write_file(filp);
+	if (ret)
+		return ret;
+
+	inode_lock(inode);
+
+	if (f2fs_is_atomic_file(inode))
+		goto out;
+
+	spin_lock(&sbi->inode_lock[DONATE_INODE]);
+	/* let's remove the range, if len = 0 */
+	if (!range.len) {
+		if (!list_empty(&F2FS_I(inode)->gdonate_list)) {
+			list_del_init(&F2FS_I(inode)->gdonate_list);
+			sbi->donate_files--;
+		}
+	} else {
+		if (list_empty(&F2FS_I(inode)->gdonate_list)) {
+			list_add_tail(&F2FS_I(inode)->gdonate_list,
+					&sbi->inode_list[DONATE_INODE]);
+			sbi->donate_files++;
+		} else {
+			list_move_tail(&F2FS_I(inode)->gdonate_list,
+					&sbi->inode_list[DONATE_INODE]);
+		}
+		F2FS_I(inode)->donate_start = range.start;
+		F2FS_I(inode)->donate_end = range.start + range.len - 1;
+	}
+	spin_unlock(&sbi->inode_lock[DONATE_INODE]);
+out:
+	inode_unlock(inode);
+	mnt_drop_write_file(filp);
+	return ret;
+}
+
  static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
  {
  	struct inode *inode = file_inode(filp);
@@ -4458,6 +4519,8 @@ static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  		return -EOPNOTSUPP;
  	case F2FS_IOC_SHUTDOWN:
  		return f2fs_ioc_shutdown(filp, arg);
+	case F2FS_IOC_DONATE_RANGE:
+		return f2fs_ioc_donate_range(filp, arg);
  	case FITRIM:
  		return f2fs_ioc_fitrim(filp, arg);
  	case FS_IOC_SET_ENCRYPTION_POLICY:
@@ -5209,6 +5272,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  	case F2FS_IOC_RELEASE_VOLATILE_WRITE:
  	case F2FS_IOC_ABORT_ATOMIC_WRITE:
  	case F2FS_IOC_SHUTDOWN:
+	case F2FS_IOC_DONATE_RANGE:
  	case FITRIM:
  	case FS_IOC_SET_ENCRYPTION_POLICY:
  	case FS_IOC_GET_ENCRYPTION_PWSALT:
fs/f2fs/inode.c +14 -0
index 7de33da8b3ea..f9fc58f313f2 100644
@@ -804,6 +804,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
   */
@@ -838,6 +851,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 fc7d463dee15..ef639a6d82e5 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);
include/uapi/linux/f2fs.h +7 -0
index f7aaf8d23e20..cd38a7c166e6 100644
@@ -44,6 +44,8 @@
  #define F2FS_IOC_COMPRESS_FILE		_IO(F2FS_IOCTL_MAGIC, 24)
  #define F2FS_IOC_START_ATOMIC_REPLACE	_IO(F2FS_IOCTL_MAGIC, 25)
  #define F2FS_IOC_GET_DEV_ALIAS_FILE	_IOR(F2FS_IOCTL_MAGIC, 26, __u32)
+#define F2FS_IOC_DONATE_RANGE		_IOW(F2FS_IOCTL_MAGIC, 27,	\
+						struct f2fs_donate_range)
 
  /*
   * should be same as XFS_IOC_GOINGDOWN.
@@ -97,4 +99,9 @@ struct f2fs_comp_option {
  	__u8 log_cluster_size;
  };
 
+struct f2fs_donate_range {
+	__u64 start;
+	__u64 len;
+};
+
  #endif /* _UAPI_LINUX_F2FS_H */
#3 lore raw

Re: [PATCH 1/2] f2fs: register inodes which is able to donate pages

In reply to Jaegeuk Kim
Quoted 11 lines · Jaegeuk Kim

This patch introduces an inode list to keep the page cache ranges that users can donate pages together.

#define F2FS_IOC_DONATE_RANGE _IOW(F2FS_IOCTL_MAGIC, 27, \
struct f2fs_donate_range)

 struct f2fs_donate_range {
	__u64 start;
	__u64 len;

};

e.g., ioctl(F2FS_IOC_DONATE_RANGE, &range);

This is not a very good description. "donate" here seems to basically mean a invalidate_inode_pages2_range. Which is a strange use of the word. what are the use cases? Why is this queued up to a thread and not done inline? Why is this in f2fs and not in common code.

I also which file systems wouldn't just add random fs specific ioctls all the time without any kinds of discussion of the API. f2fs is by far the worst offender there, but not the only one.

author maintainer
#4 lore raw

Re: [PATCH 1/2] f2fs: register inodes which is able to donate pages

Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Jaegeuk Kim

This patch introduces an inode list to keep the page cache ranges that users can donate pages together.

#define F2FS_IOC_DONATE_RANGE _IOW(F2FS_IOCTL_MAGIC, 27, \
struct f2fs_donate_range)

 struct f2fs_donate_range {
	__u64 start;
	__u64 len;

};

Quoted 6 lines
Quoted 1 line

e.g., ioctl(F2FS_IOC_DONATE_RANGE, &range);

This is not a very good description. "donate" here seems to basically mean a invalidate_inode_pages2_range. Which is a strange use of the word. what are the use cases? Why is this queued up to a thread and not done inline? Why is this in f2fs and not in common code.

The idea is let apps register some file ranges for page donation and admin recliam such pages all togehter if they expect to see memory pressure soon. We can rely on LRU, but this is more user-given trigger. I'm not sure whether there's a need in general, hence, wanted to put it in f2fs first to get more concrete use-cases beyond this Android case.

Quoted 4 lines

I also which file systems wouldn't just add random fs specific ioctls all the time without any kinds of discussion of the API. f2fs is by far the worst offender there, but not the only one.

#5 lore raw

Re: [PATCH 1/2] f2fs: register inodes which is able to donate pages

Quoted 9 lines · Jaegeuk Kim
Quoted 3 lines

mean a invalidate_inode_pages2_range. Which is a strange use of the word. what are the use cases? Why is this queued up to a thread and not done inline? Why is this in f2fs and not in common code.

The idea is let apps register some file ranges for page donation and admin recliam such pages all togehter if they expect to see memory pressure soon. We can rely on LRU, but this is more user-given trigger. I'm not sure whether there's a need in general, hence, wanted to put it in f2fs first to get more concrete use-cases beyond this Android case.

Well, that's certainly not a file system feature. Please build this as generic infrastucture and send it to the linux-mm list.

author maintainer
#6 lore raw

PATCH v3 2/2 f2fs: add a sysfs entry to request donate file-backed pages

In reply to Jaegeuk Kim

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.

---
 Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
 fs/f2fs/f2fs.h                          |  2 ++
 fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
 fs/f2fs/sysfs.c                         |  8 ++++++++
 4 files changed, 44 insertions(+)
Documentation/ABI/testing/sysfs-fs-f2fs +7 -0
index 3e1630c70d8a..6f9d8b8889fd 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/<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.
fs/f2fs/f2fs.h +2 -0
index 951fbc3f94c7..399ddd10a94f 100644
@@ -1637,6 +1637,7 @@ struct f2fs_sb_info {
 
 	/* control donate caches */
 	unsigned int donate_files;
+	unsigned int donate_caches;
 
 	/* basic filesystem units */
 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
@@ -4259,6 +4260,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);
 
fs/f2fs/shrinker.c +27 -0
index 83d6fb97dcae..22f62813910b 100644
@@ -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;
+	struct f2fs_inode_info *fi;
+	int nfiles = sbi->donate_caches;
+
+	while (nfiles--) {
+		spin_lock(&sbi->inode_lock[DONATE_INODE]);
+		if (list_empty(&sbi->inode_list[DONATE_INODE])) {
+			spin_unlock(&sbi->inode_lock[DONATE_INODE]);
+			break;
+		}
+		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)
+			continue;
+
+		invalidate_inode_pages2_range(inode->i_mapping,
+					fi->donate_start, fi->donate_end);
+		iput(inode);
+	}
+}
+
 void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
 {
 	spin_lock(&f2fs_list_lock);
fs/f2fs/sysfs.c +8 -0
index 6b99dc49f776..f81190fabdd3 100644
@@ -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->donate_files);
+		f2fs_donate_caches(sbi);
+		return count;
+	}
+
 	*ui = (unsigned int)t;
 
 	return count;
@@ -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);
@@ -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),
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
maintainer
#7 lore raw

Re: [f2fs-dev] [PATCH 2/2] f2fs: add a sysfs entry to request donate file-backed pages

Quoted 71 lines · On 1/16/25 06:16, Jaegeuk Kim via Linux-f2fs-devel wrote:

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.

---
  Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
  fs/f2fs/f2fs.h                          |  2 ++
  fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
  fs/f2fs/sysfs.c                         |  8 ++++++++
  4 files changed, 44 insertions(+)
Documentation/ABI/testing/sysfs-fs-f2fs +7 -0
index 3e1630c70d8a..6f9d8b8889fd 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/<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.
fs/f2fs/f2fs.h +2 -0
index 951fbc3f94c7..399ddd10a94f 100644
@@ -1637,6 +1637,7 @@ struct f2fs_sb_info {
 
  	/* control donate caches */
  	unsigned int donate_files;
+	unsigned int donate_caches;
 
  	/* basic filesystem units */
  	unsigned int log_sectors_per_block;	/* log2 sectors per block */
@@ -4259,6 +4260,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);
 
fs/f2fs/shrinker.c +13 -0
index 83d6fb97dcae..22f62813910b 100644
@@ -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;
+	struct f2fs_inode_info *fi;
+	int nfiles = sbi->donate_caches;
+
+	while (nfiles--) {
+		spin_lock(&sbi->inode_lock[DONATE_INODE]);
+		if (list_empty(&sbi->inode_list[DONATE_INODE])) {
+			spin_unlock(&sbi->inode_lock[DONATE_INODE]);
+			break;
+		}
+		fi = list_first_entry(&sbi->inode_list[DONATE_INODE],

fs/f2fs/shrinker.c unsigned long f2fs_shrink_scan(struct shrinker *shrink, · line 155

Quoted 10 lines
fs/f2fs/shrinker.c +10 -0
+					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)
+			continue;
+
+		invalidate_inode_pages2_range(inode->i_mapping,
+					fi->donate_start, fi->donate_end);

fi->donate_start and fi->donate_end can be updated in ioctl concurrently and become inconsistent here, is it fine?

Thanks,

Quoted 40 lines
fs/f2fs/shrinker.c +4 -0
+		iput(inode);
+	}
+}
+
  void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
  {
  	spin_lock(&f2fs_list_lock);
fs/f2fs/sysfs.c +8 -0
index 6b99dc49f776..f81190fabdd3 100644
@@ -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->donate_files);
+		f2fs_donate_caches(sbi);
+		return count;
+	}
+
  	*ui = (unsigned int)t;
 
  	return count;
@@ -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);
@@ -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),
author maintainer
#8 lore raw

Re: [f2fs-dev] [PATCH 2/2] f2fs: add a sysfs entry to request donate file-backed pages

Quoted 73 lines · Chao Yu
Quoted 72 lines · On 1/16/25 06:16, Jaegeuk Kim via Linux-f2fs-devel wrote:

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.

---
  Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
  fs/f2fs/f2fs.h                          |  2 ++
  fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
  fs/f2fs/sysfs.c                         |  8 ++++++++
  4 files changed, 44 insertions(+)
Documentation/ABI/testing/sysfs-fs-f2fs +7 -0
index 3e1630c70d8a..6f9d8b8889fd 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/<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.
fs/f2fs/f2fs.h +2 -0
index 951fbc3f94c7..399ddd10a94f 100644
@@ -1637,6 +1637,7 @@ struct f2fs_sb_info {
  	/* control donate caches */
  	unsigned int donate_files;
+	unsigned int donate_caches;
  	/* basic filesystem units */
  	unsigned int log_sectors_per_block;	/* log2 sectors per block */
@@ -4259,6 +4260,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);
fs/f2fs/shrinker.c +18 -0
index 83d6fb97dcae..22f62813910b 100644
@@ -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;
+	struct f2fs_inode_info *fi;
+	int nfiles = sbi->donate_caches;
+
+	while (nfiles--) {
+		spin_lock(&sbi->inode_lock[DONATE_INODE]);
+		if (list_empty(&sbi->inode_list[DONATE_INODE])) {
+			spin_unlock(&sbi->inode_lock[DONATE_INODE]);
+			break;
+		}
+		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]);
+
Quoted 8 lines

fs/f2fs/shrinker.c unsigned long f2fs_shrink_scan(struct shrinker *shrink, · line 155

Quoted 5 lines
fs/f2fs/shrinker.c +5 -0
+		if (!inode)
+			continue;
+
+		invalidate_inode_pages2_range(inode->i_mapping,
+					fi->donate_start, fi->donate_end);

fi->donate_start and fi->donate_end can be updated in ioctl concurrently and become inconsistent here, is it fine?

I think that needs to be managed by user, as there'll be no frequent updates to the donate ranges.

Quoted 41 lines

Thanks,

Quoted 38 lines
fs/f2fs/shrinker.c +4 -0
+		iput(inode);
+	}
+}
+
  void f2fs_join_shrinker(struct f2fs_sb_info *sbi)
  {
  	spin_lock(&f2fs_list_lock);
fs/f2fs/sysfs.c +8 -0
index 6b99dc49f776..f81190fabdd3 100644
@@ -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->donate_files);
+		f2fs_donate_caches(sbi);
+		return count;
+	}
+
  	*ui = (unsigned int)t;
  	return count;
@@ -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);
@@ -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),