diff mbox series

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

Message ID 20250113183933.1268282-2-jaegeuk@kernel.org (mailing list archive)
State New
Headers show
Series [f2fs-dev,1/2] f2fs: register inodes which is able to donate pages | expand

Commit Message

Jaegeuk Kim Jan. 13, 2025, 6:39 p.m. UTC
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 <jaegeuk@kernel.org>
---
 Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
 fs/f2fs/f2fs.h                          |  4 ++++
 fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
 fs/f2fs/sysfs.c                         |  8 ++++++++
 4 files changed, 46 insertions(+)

Comments

Chao Yu Jan. 14, 2025, 7:34 a.m. UTC | #1
On 1/14/25 02:39, 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.
> 
> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> ---
>   Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
>   fs/f2fs/f2fs.h                          |  4 ++++
>   fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
>   fs/f2fs/sysfs.c                         |  8 ++++++++
>   4 files changed, 46 insertions(+)
> 
> diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
> index 3e1630c70d8a..6f9d8b8889fd 100644
> --- a/Documentation/ABI/testing/sysfs-fs-f2fs
> +++ b/Documentation/ABI/testing/sysfs-fs-f2fs
> @@ -828,3 +828,10 @@ Date:		November 2024
>   Contact:	"Chao Yu" <chao@kernel.org>
>   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" <jaegeuk@kernel.org>
> +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.
> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> index 7ce3e3eab17a..6c434ae94cb1 100644
> --- a/fs/f2fs/f2fs.h
> +++ b/fs/f2fs/f2fs.h
> @@ -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 */
> @@ -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);
>   
> diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
> index 83d6fb97dcae..a3e2063392a7 100644
> --- a/fs/f2fs/shrinker.c
> +++ b/fs/f2fs/shrinker.c
> @@ -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]);

Not needed to drop it from the global list, right?

Thanks,

> +	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);
> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
> index 6b99dc49f776..7570580ec3c0 100644
> --- a/fs/f2fs/sysfs.c
> +++ b/fs/f2fs/sysfs.c
> @@ -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;
> @@ -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),
Jaegeuk Kim Jan. 14, 2025, 5:18 p.m. UTC | #2
On 01/14, Chao Yu wrote:
> On 1/14/25 02:39, 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.
> > 
> > Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
> > ---
> >   Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
> >   fs/f2fs/f2fs.h                          |  4 ++++
> >   fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
> >   fs/f2fs/sysfs.c                         |  8 ++++++++
> >   4 files changed, 46 insertions(+)
> > 
> > diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
> > index 3e1630c70d8a..6f9d8b8889fd 100644
> > --- a/Documentation/ABI/testing/sysfs-fs-f2fs
> > +++ b/Documentation/ABI/testing/sysfs-fs-f2fs
> > @@ -828,3 +828,10 @@ Date:		November 2024
> >   Contact:	"Chao Yu" <chao@kernel.org>
> >   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" <jaegeuk@kernel.org>
> > +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.
> > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
> > index 7ce3e3eab17a..6c434ae94cb1 100644
> > --- a/fs/f2fs/f2fs.h
> > +++ b/fs/f2fs/f2fs.h
> > @@ -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 */
> > @@ -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);
> > diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
> > index 83d6fb97dcae..a3e2063392a7 100644
> > --- a/fs/f2fs/shrinker.c
> > +++ b/fs/f2fs/shrinker.c
> > @@ -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]);
> 
> Not needed to drop it from the global list, right?

Yea, there're two paths to drop: 1) waiting for evict_inode, 2) setting a new
range having len=0.

> 
> Thanks,
> 
> > +	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);
> > diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
> > index 6b99dc49f776..7570580ec3c0 100644
> > --- a/fs/f2fs/sysfs.c
> > +++ b/fs/f2fs/sysfs.c
> > @@ -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;
> > @@ -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),
Jaegeuk Kim Jan. 14, 2025, 8:50 p.m. UTC | #3
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 <jaegeuk@kernel.org>
---

 Change log from v1:
  - don't use sbi->ndirty_inode which is not defined by default

 Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
 fs/f2fs/f2fs.h                          |  4 ++++
 fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
 fs/f2fs/sysfs.c                         |  8 ++++++++
 4 files changed, 46 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
index 3e1630c70d8a..6f9d8b8889fd 100644
--- a/Documentation/ABI/testing/sysfs-fs-f2fs
+++ b/Documentation/ABI/testing/sysfs-fs-f2fs
@@ -828,3 +828,10 @@ Date:		November 2024
 Contact:	"Chao Yu" <chao@kernel.org>
 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" <jaegeuk@kernel.org>
+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.
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 7ce3e3eab17a..6c434ae94cb1 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -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 */
@@ -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);
 
diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
index 83d6fb97dcae..a3e2063392a7 100644
--- a/fs/f2fs/shrinker.c
+++ b/fs/f2fs/shrinker.c
@@ -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);
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index 6b99dc49f776..2a6b01257ad8 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -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 = t;
+		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),
Chao Yu Jan. 15, 2025, 2:17 a.m. UTC | #4
On 1/15/25 01:18, Jaegeuk Kim wrote:
> On 01/14, Chao Yu wrote:
>> On 1/14/25 02:39, 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.
>>>
>>> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
>>> ---
>>>    Documentation/ABI/testing/sysfs-fs-f2fs |  7 +++++++
>>>    fs/f2fs/f2fs.h                          |  4 ++++
>>>    fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++++
>>>    fs/f2fs/sysfs.c                         |  8 ++++++++
>>>    4 files changed, 46 insertions(+)
>>>
>>> diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
>>> index 3e1630c70d8a..6f9d8b8889fd 100644
>>> --- a/Documentation/ABI/testing/sysfs-fs-f2fs
>>> +++ b/Documentation/ABI/testing/sysfs-fs-f2fs
>>> @@ -828,3 +828,10 @@ Date:		November 2024
>>>    Contact:	"Chao Yu" <chao@kernel.org>
>>>    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" <jaegeuk@kernel.org>
>>> +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.
>>> diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
>>> index 7ce3e3eab17a..6c434ae94cb1 100644
>>> --- a/fs/f2fs/f2fs.h
>>> +++ b/fs/f2fs/f2fs.h
>>> @@ -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 */
>>> @@ -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);
>>> diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
>>> index 83d6fb97dcae..a3e2063392a7 100644
>>> --- a/fs/f2fs/shrinker.c
>>> +++ b/fs/f2fs/shrinker.c
>>> @@ -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]);
>>
>> Not needed to drop it from the global list, right?
> 
> Yea, there're two paths to drop: 1) waiting for evict_inode, 2) setting a new
> range having len=0.

Second way just relocate entry to list tail, not drop it from list?

Thanks,

> 
>>
>> Thanks,
>>
>>> +	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);
>>> diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
>>> index 6b99dc49f776..7570580ec3c0 100644
>>> --- a/fs/f2fs/sysfs.c
>>> +++ b/fs/f2fs/sysfs.c
>>> @@ -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;
>>> @@ -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),
diff mbox series

Patch

diff --git a/Documentation/ABI/testing/sysfs-fs-f2fs b/Documentation/ABI/testing/sysfs-fs-f2fs
index 3e1630c70d8a..6f9d8b8889fd 100644
--- a/Documentation/ABI/testing/sysfs-fs-f2fs
+++ b/Documentation/ABI/testing/sysfs-fs-f2fs
@@ -828,3 +828,10 @@  Date:		November 2024
 Contact:	"Chao Yu" <chao@kernel.org>
 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" <jaegeuk@kernel.org>
+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.
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 7ce3e3eab17a..6c434ae94cb1 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -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 */
@@ -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);
 
diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c
index 83d6fb97dcae..a3e2063392a7 100644
--- a/fs/f2fs/shrinker.c
+++ b/fs/f2fs/shrinker.c
@@ -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);
diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
index 6b99dc49f776..7570580ec3c0 100644
--- a/fs/f2fs/sysfs.c
+++ b/fs/f2fs/sysfs.c
@@ -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;
@@ -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),