Skip to contentSkip to conversation

add ioctl/sysfs to donate file-backed pages

PATCH Superseded Jaegeuk Kim 2 participants 5 messages Jan 16, 2025 – Jan 17, 2025

Subsystems (2)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Jaegeuk Kim

author maintainer
#0 lore raw

v5 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 v4:
- fix range handling

Change log from v3:
- cover partial range

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

Jaegeuk Kim (1):
f2fs: add a sysfs entry to request donate file-backed pages

Yi Sun (1):
f2fs: Optimize f2fs_truncate_data_blocks_range()

 Documentation/ABI/testing/sysfs-fs-f2fs |  7 ++++++
 fs/f2fs/f2fs.h                          |  2 ++
 fs/f2fs/file.c                          | 29 +++++++++++++++++++++----
 fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++
 fs/f2fs/sysfs.c                         |  8 +++++++
 5 files changed, 69 insertions(+), 4 deletions(-)
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
author maintainer
#1 lore raw

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

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
author maintainer
#2 lore raw

PATCH v5 2/2 f2fs: Optimize f2fs_truncate_data_blocks_range()

In reply to Jaegeuk Kim

From: Yi Sun <[email protected]>

Function f2fs_invalidate_blocks() can process consecutive blocks at a time, so f2fs_truncate_data_blocks_range() is optimized to use the new functionality of f2fs_invalidate_blocks().

Add two variables @blkstart and @blklen, @blkstart records the first address of the consecutive blocks, and @blkstart records the number of consecutive blocks.

---
 fs/f2fs/file.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
fs/f2fs/file.c +25 -4
index 902f8f50cc27..6572970a988a 100644
@@ -621,8 +621,11 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
 	int cluster_index = 0, valid_blocks = 0;
 	int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
 	bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
+	block_t blkstart;
+	int blklen = 0;
 
 	addr = get_dnode_addr(dn->inode, dn->node_page) + ofs;
+	blkstart = le32_to_cpu(*addr);
 
 	/* Assumption: truncation starts with cluster */
 	for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
@@ -638,26 +641,44 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
 		}
 
 		if (blkaddr == NULL_ADDR)
-			continue;
+			goto next;
 
 		f2fs_set_data_blkaddr(dn, NULL_ADDR);
 
 		if (__is_valid_data_blkaddr(blkaddr)) {
 			if (time_to_inject(sbi, FAULT_BLKADDR_CONSISTENCE))
-				continue;
+				goto next;
 			if (!f2fs_is_valid_blkaddr_raw(sbi, blkaddr,
 						DATA_GENERIC_ENHANCE))
-				continue;
+				goto next;
 			if (compressed_cluster)
 				valid_blocks++;
 		}
 
-		f2fs_invalidate_blocks(sbi, blkaddr, 1);
+		if (blkstart + blklen == blkaddr) {
+			blklen++;
+		} else {
+			f2fs_invalidate_blocks(sbi, blkstart, blklen);
+			blkstart = blkaddr;
+			blklen = 1;
+		}
 
 		if (!released || blkaddr != COMPRESS_ADDR)
 			nr_free++;
+
+		continue;
+
+next:
+		if (blklen)
+			f2fs_invalidate_blocks(sbi, blkstart, blklen);
+
+		blkstart = le32_to_cpu(*(addr + 1));
+		blklen = 0;
 	}
 
+	if (blklen)
+		f2fs_invalidate_blocks(sbi, blkstart, blklen);
+
 	if (compressed_cluster)
 		f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
 
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog
#3 lore raw
In reply to Jaegeuk Kim

Still NAC for sneaking in an almost undocumented MM feature into a file system ioctl. Especially while a discussion on that is still ongoin.

And it's still bad that you don't even bother to Cc fsdevel on this, nor linux-api or in this case the mm list.

Quoted 19 lines · Jaegeuk Kim

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 v4:
- fix range handling

Change log from v3:
- cover partial range

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

Jaegeuk Kim (1):
f2fs: add a sysfs entry to request donate file-backed pages

Yi Sun (1):
f2fs: Optimize f2fs_truncate_data_blocks_range()

Quoted 11 lines
 Documentation/ABI/testing/sysfs-fs-f2fs |  7 ++++++
 fs/f2fs/f2fs.h                          |  2 ++
 fs/f2fs/file.c                          | 29 +++++++++++++++++++++----
 fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++
 fs/f2fs/sysfs.c                         |  8 +++++++
 5 files changed, 69 insertions(+), 4 deletions(-)
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog

---end quoted text---

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

Still NAC for sneaking in an almost undocumented MM feature into a file system ioctl. Especially while a discussion on that is still ongoin.

And it's still bad that you don't even bother to Cc fsdevel on this, nor linux-api or in this case the mm list.

Well, I don't want to bother other groups for random APIs, unless I, myself, am super confident this is feasible for generic API.
But, let me try to listen to other opinions.

Quoted 33 lines · Jaegeuk Kim
Quoted 19 lines

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 v4:
- fix range handling

Change log from v3:
- cover partial range

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

Jaegeuk Kim (1):
f2fs: add a sysfs entry to request donate file-backed pages

Yi Sun (1):
f2fs: Optimize f2fs_truncate_data_blocks_range()

Quoted 11 lines
 Documentation/ABI/testing/sysfs-fs-f2fs |  7 ++++++
 fs/f2fs/f2fs.h                          |  2 ++
 fs/f2fs/file.c                          | 29 +++++++++++++++++++++----
 fs/f2fs/shrinker.c                      | 27 +++++++++++++++++++++++
 fs/f2fs/sysfs.c                         |  8 +++++++
 5 files changed, 69 insertions(+), 4 deletions(-)
Signature · 1 line
2.48.0.rc2.279.g1de40edade-goog

---end quoted text---