Skip to contentSkip to conversation

fuse: add no forget support

PATCH New yangyun 1 participant 3 messages Aug 27, 2024

Subsystems (2)
lore
Share & tools
lore

yangyun

author
#0 lore raw

v3 cover fuse: add no forget support

FUSE_FORGET requests are not used in some cases (e.g., juicefs) but have an impact on the system. So add no forget support. The details about no forget support can be seen in the following patches.

Patch 1 cleans up the allocation and the processing of forget requests. Patch 2 adds the no forget support based on patch 1.

The link of libfuse with no forget support:
https://github.com/yangyun50/libfuse/commit/ef7061fef6f8c7154c975e1b0348dc9f12f945fe
It enables the no forget by default if the filesystem doesn't implement the forget operation (i.e., se->op.forget == NULL).

---

v1:
https://lore.kernel.org/lkml/[email protected]/
v2:
https://lore.kernel.org/lkml/[email protected]/

Changes from v1->v2:
- Still use fuse_queue_forget in patch 1 (Miklos)
- Simplify function name in patch 2 (Josef)

Changes from v2->v3:
- Still preallocate fuse_forget_link on the inode creation in patch1 (Miklos)

yangyun (2):
fuse: move fuse_forget_link allocation inside fuse_queue_forget()
fuse: add support for no forget requests

 fs/fuse/dev.c             | 16 ++++++++--
 fs/fuse/dir.c             | 63 +++++++++------------------------------
 fs/fuse/fuse_i.h          | 33 +++++++++++++++++---
 fs/fuse/inode.c           | 53 ++++++++++++++++----------------
 fs/fuse/readdir.c         | 38 ++++++-----------------
 include/uapi/linux/fuse.h |  3 ++
 6 files changed, 96 insertions(+), 110 deletions(-)
Signature · 1 line
2.33.0
author
#1 lore raw

PATCH v3 1/2 fuse: move fuse_forget_link allocation inside fuse_queue_forget()

The struct fuse_forget_link is allocated outside fuse_queue_forget() before this patch. This requires the allocation in advance. In some cases (e.g., directory operations without errors), this struct is not needed but allocated, which contributes to memory usage and performance degradation. Besides, this messes up the code to some extent. So move the fuse_forget_link allocation inside fuse_queue_forget() with __GFP_NOFAIL in these cases. Since the inode eviction path can be triggered from memory reclaim and may block the system, it is better to preallocate it on inode creation.

fuse_force_forget() is used by readdirplus before this patch for the reason that we do not know how many 'fuse_forget_link' structures will be allocated in advance when error happens. After this patch, this function is not needed any more and can be removed (The noreply fuse_args can also be removed). By this way, all FUSE_FORGET requests are sent by using fuse_queue_forget() function as e.g. virtiofs handles them differently from regular requests.

---
 fs/fuse/dev.c     | 13 ++++++++---
 fs/fuse/dir.c     | 59 +++++++++++------------------------------------
 fs/fuse/fuse_i.h  | 10 ++++----
 fs/fuse/inode.c   |  9 ++------
 fs/fuse/readdir.c | 30 ++++++------------------
 5 files changed, 38 insertions(+), 83 deletions(-)
fs/fuse/dev.c +10 -3
index 9eb191b5c4de..31ca97b1b32c 100644
@@ -234,10 +234,18 @@ __releases(fiq->lock)
 }
 
 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
-		       u64 nodeid, u64 nlookup)
+			u64 nodeid, u64 nlookup)
 {
 	struct fuse_iqueue *fiq = &fc->iq;
 
+	/*
+	 * The nullptr means that fuse_queue_forget() is used in error cases.
+	 * Avoid preallocating this structure because it is unlikely used.
+	 * Use __GFP_NOFAIL to make memory allocation always succeed.
+	 */
+	if (forget == NULL)
+		forget = fuse_alloc_forget(GFP_KERNEL_ACCOUNT | __GFP_NOFAIL);
+
 	forget->forget_one.nodeid = nodeid;
 	forget->forget_one.nlookup = nlookup;
 
@@ -508,8 +516,7 @@ ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args)
 	fuse_adjust_compat(fc, args);
 	fuse_args_to_req(req, args);
 
-	if (!args->noreply)
-		__set_bit(FR_ISREPLY, &req->flags);
+	__set_bit(FR_ISREPLY, &req->flags);
 	__fuse_request_send(req);
 	ret = req->out.h.error;
 	if (!ret && args->out_argvar) {
fs/fuse/dir.c +13 -46

190 lines not on this page · raw message

fs/fuse/fuse_i.h +6 -4
index f23919610313..0b9a02b3155f 100644
@@ -298,7 +298,6 @@ struct fuse_args {
 	uint8_t out_numargs;
 	uint8_t ext_idx;
 	bool force:1;
-	bool noreply:1;
 	bool nocreds:1;
 	bool in_pages:1;
 	bool out_pages:1;
@@ -1048,10 +1047,13 @@ int fuse_lookup_name(struct super_block *sb, u64 nodeid, const struct qstr *name
 /**
  * Send FORGET command
  */
-void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
-		       u64 nodeid, u64 nlookup);
+static inline struct fuse_forget_link *fuse_alloc_forget(gfp_t flags)
+{
+	return kzalloc(sizeof(struct fuse_forget_link), flags);
+}
 
-struct fuse_forget_link *fuse_alloc_forget(void);
+void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link* forget,
+			u64 nodeid, u64 nlookup);
 
 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
 					     unsigned int max,
fs/fuse/inode.c +2 -7
index 99e44ea7d875..ecc867e21acd 100644
@@ -63,11 +63,6 @@ MODULE_PARM_DESC(max_user_congthresh,
 static struct file_system_type fuseblk_fs_type;
 #endif
 
-struct fuse_forget_link *fuse_alloc_forget(void)
-{
-	return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT);
-}
-
 static struct fuse_submount_lookup *fuse_alloc_submount_lookup(void)
 {
 	struct fuse_submount_lookup *sl;
@@ -75,7 +70,7 @@ static struct fuse_submount_lookup *fuse_alloc_submount_lookup(void)
 	sl = kzalloc(sizeof(struct fuse_submount_lookup), GFP_KERNEL_ACCOUNT);
 	if (!sl)
 		return NULL;
-	sl->forget = fuse_alloc_forget();
+	sl->forget = fuse_alloc_forget(GFP_KERNEL_ACCOUNT);
 	if (!sl->forget)
 		goto out_free;
 
@@ -104,7 +99,7 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
 	fi->submount_lookup = NULL;
 	mutex_init(&fi->mutex);
 	spin_lock_init(&fi->lock);
-	fi->forget = fuse_alloc_forget();
+	fi->forget = fuse_alloc_forget(GFP_KERNEL_ACCOUNT);
 	if (!fi->forget)
 		goto out_free;
 
fs/fuse/readdir.c +7 -23
index 0377b6dc24c8..70d161f420c6 100644
@@ -262,27 +262,6 @@ static int fuse_direntplus_link(struct file *file,
 	return 0;
 }
 
-static void fuse_force_forget(struct file *file, u64 nodeid)
-{
-	struct inode *inode = file_inode(file);
-	struct fuse_mount *fm = get_fuse_mount(inode);
-	struct fuse_forget_in inarg;
-	FUSE_ARGS(args);
-
-	memset(&inarg, 0, sizeof(inarg));
-	inarg.nlookup = 1;
-	args.opcode = FUSE_FORGET;
-	args.nodeid = nodeid;
-	args.in_numargs = 1;
-	args.in_args[0].size = sizeof(inarg);
-	args.in_args[0].value = &inarg;
-	args.force = true;
-	args.noreply = true;
-
-	fuse_simple_request(fm, &args);
-	/* ignore errors */
-}
-
 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
 			     struct dir_context *ctx, u64 attr_version)
 {
@@ -320,8 +299,13 @@ static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
 		nbytes -= reclen;
 
 		ret = fuse_direntplus_link(file, direntplus, attr_version);
-		if (ret)
-			fuse_force_forget(file, direntplus->entry_out.nodeid);
+		if (ret) {
+			struct inode *inode = file_inode(file);
+			struct fuse_mount *fm = get_fuse_mount(inode);
+
+			fuse_queue_forget(fm->fc, NULL,
+					direntplus->entry_out.nodeid, 1);
+		}
 	}
 
 	return 0;
Signature · 1 line
2.33.0
author
#2 lore raw

PATCH v3 2/2 fuse: add support for no forget requests

In reply to yangyun

FUSE_FORGET requests are not used if the fuse file system does not implement the forget operation in userspace (e.g., fuse file system does not cache any inodes).

However, the kernel is invisible to the userspace implementation and always sends FUSE_FORGET requests, which can lead to performance degradation because of useless contex switch and memory copy in some cases (e.g., many inodes are evicted from icache which was described in commit 07e77dca8a1f ("fuse: separate queue for FORGET requests")).

Just like 'no_interrupt' in 'struct fuse_conn', we add 'no_forget'. But since FUSE_FORGET request does not have a reply from userspce, we can not use ENOSYS to reflect the 'no_forget' assignment. So add the FUSE_NO_FORGET_SUPPORT init flag.

Besides, if no_forget is enabled, 'nlookup' in 'struct fuse_inode' does not used and its value change can be disabled which are protected by spin_lock to reduce lock contention.

---
 fs/fuse/dev.c             |  3 +++
 fs/fuse/dir.c             |  4 +---
 fs/fuse/fuse_i.h          | 23 ++++++++++++++++++++
 fs/fuse/inode.c           | 46 +++++++++++++++++++++++----------------
 fs/fuse/readdir.c         |  8 ++-----
 include/uapi/linux/fuse.h |  3 +++
 6 files changed, 59 insertions(+), 28 deletions(-)
fs/fuse/dev.c +3 -0
index 31ca97b1b32c..7832f70baf65 100644
@@ -238,6 +238,9 @@ void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
 {
 	struct fuse_iqueue *fiq = &fc->iq;
 
+	if (fc->no_forget)
+		return;
+
 	/*
 	 * The nullptr means that fuse_queue_forget() is used in error cases.
 	 * Avoid preallocating this structure because it is unlikely used.
fs/fuse/dir.c +1 -3
index 583362492ce0..afc56c8ed7d6 100644
@@ -236,9 +236,7 @@ static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
 				fuse_queue_forget(fm->fc, NULL, outarg.nodeid, 1);
 				goto invalid;
 			}
-			spin_lock(&fi->lock);
-			fi->nlookup++;
-			spin_unlock(&fi->lock);
+			fuse_inc_nlookup(fm->fc, fi);
 		}
 		if (ret == -ENOMEM || ret == -EINTR)
 			goto out;
fs/fuse/fuse_i.h +23 -0
index 0b9a02b3155f..857357be5c8a 100644
@@ -859,6 +859,9 @@ struct fuse_conn {
 	/** Passthrough support for read/write IO */
 	unsigned int passthrough:1;
 
+	/** Is forget not implemented by fs? */
+	unsigned int no_forget:1;
+
 	/** Maximum stack depth for passthrough backing files */
 	int max_stack_depth;
 
@@ -1028,6 +1031,26 @@ static inline void fuse_sync_bucket_dec(struct fuse_sync_bucket *bucket)
 	rcu_read_unlock();
 }
 
+static inline void fuse_inc_nlookup(struct fuse_conn *fc, struct fuse_inode *fi)
+{
+	if (fc->no_forget)
+		return;
+
+	spin_lock(&fi->lock);
+	fi->nlookup++;
+	spin_lock(&fi->lock);
+}
+
+static inline void fuse_dec_nlookup(struct fuse_conn *fc, struct fuse_inode *fi)
+{
+	if (fc->no_forget)
+		return;
+
+	spin_lock(&fi->lock);
+	fi->nlookup--;
+	spin_lock(&fi->lock);
+}
+
 /** Device operations */
 extern const struct file_operations fuse_dev_operations;
 
fs/fuse/inode.c +27 -19

97 lines not on this page · raw message

fs/fuse/readdir.c +2 -6
index 70d161f420c6..4c8164d46409 100644
@@ -218,9 +218,7 @@ static int fuse_direntplus_link(struct file *file,
 		}
 
 		fi = get_fuse_inode(inode);
-		spin_lock(&fi->lock);
-		fi->nlookup++;
-		spin_unlock(&fi->lock);
+		fuse_inc_nlookup(fc, fi);
 
 		forget_all_cached_acls(inode);
 		fuse_change_attributes(inode, &o->attr, NULL,
@@ -247,9 +245,7 @@ static int fuse_direntplus_link(struct file *file,
 			if (!IS_ERR(inode)) {
 				struct fuse_inode *fi = get_fuse_inode(inode);
 
-				spin_lock(&fi->lock);
-				fi->nlookup--;
-				spin_unlock(&fi->lock);
+				fuse_dec_nlookup(fc, fi);
 			}
 			return PTR_ERR(dentry);
 		}
include/uapi/linux/fuse.h +3 -0
index d08b99d60f6f..bf660880bc7a 100644
@@ -217,6 +217,7 @@
  *  - add backing_id to fuse_open_out, add FOPEN_PASSTHROUGH open flag
  *  - add FUSE_NO_EXPORT_SUPPORT init flag
  *  - add FUSE_NOTIFY_RESEND, add FUSE_HAS_RESEND init flag
+ *  - add FUSE_NO_FORGET_SUPPORT init flag
  */
 
 #ifndef _LINUX_FUSE_H
@@ -421,6 +422,7 @@ struct fuse_file_lock {
  * FUSE_NO_EXPORT_SUPPORT: explicitly disable export support
  * FUSE_HAS_RESEND: kernel supports resending pending requests, and the high bit
  *		    of the request ID indicates resend requests
+ * FUSE_NO_FORGET_SUPPORT: disable forget requests
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -463,6 +465,7 @@ struct fuse_file_lock {
 #define FUSE_PASSTHROUGH	(1ULL << 37)
 #define FUSE_NO_EXPORT_SUPPORT	(1ULL << 38)
 #define FUSE_HAS_RESEND		(1ULL << 39)
+#define FUSE_NO_FORGET_SUPPORT  (1ULL << 40)
 
 /* Obsolete alias for FUSE_DIRECT_IO_ALLOW_MMAP */
 #define FUSE_DIRECT_IO_RELAX	FUSE_DIRECT_IO_ALLOW_MMAP
Signature · 1 line
2.33.0