Skip to contentSkip to conversation

fuse: schedule dentry_tree_work only between dentry tree init and cleanup

PATCH New Sang-Heon Jeon 1 participant 3 messages Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover fuse: schedule dentry_tree_work only between dentry tree init and cleanup

Hello,

inval_wq_set() schedules dentry_tree_work when inval_wq is enabled, but it can be called before fuse_dentry_tree_init() and after fuse_dentry_tree_cleanup(). This series makes inval_wq_set() schedule the work only between the two.

Patch 1 defers scheduling the work until fuse_dentry_tree_init() sets it up.

Patch 2 does not schedule the work after fuse_dentry_tree_cleanup() cancels it.

Both patches hold kernel_param_lock(), because a sysfs write can call inval_wq_set() at the same time.

---
Changes from v1 [1]
- Hold kernel_param_lock() in fuse_dentry_tree_init()
- Add patch 2 to not schedule the work after fuse_dentry_tree_cleanup()

[1] https://lore.kernel.org/all/[email protected]/
---

Sang-Heon Jeon (2):
fuse: defer dentry_tree_work until fuse_dentry_tree_init()
fuse: do not schedule dentry_tree_work after
fuse_dentry_tree_cleanup()

 fs/fuse/dir.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

base-commit: ab9787d7e66ac7e6e94fc975b37c5b56fff98ca9

Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/2 fuse: defer dentry_tree_work until fuse_dentry_tree_init()

inval_wq_set() schedules dentry_tree_work when inval_wq is enabled.

But inval_wq_set() can be called at boot or module load before fuse_dentry_tree_init() initializes the work, so __queue_delayed_work() warns and the work never starts.

So defer scheduling the work until fuse_dentry_tree_init() sets it up. Also hold kernel_param_lock() in fuse_dentry_tree_init(), because a sysfs write can call inval_wq_set() at the same time.

Fixes ab84ad597386 ("fuse: new work queue to periodically invalidate expired dentries")
---
 fs/fuse/dir.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
fs/fuse/dir.c +13 -0
index da23ccd8685c..0d5d04df477b 100644
@@ -39,6 +39,7 @@ static struct delayed_work dentry_tree_work;
 /* Minimum invalidation work queue frequency */
 #define FUSE_DENTRY_INVAL_FREQ_MIN 5
 
+static bool inval_wq_ready;
 unsigned __read_mostly inval_wq;
 static int inval_wq_set(const char *val, const struct kernel_param *kp)
 {
@@ -62,6 +63,10 @@ static int inval_wq_set(const char *val, const struct kernel_param *kp)
 
 	*((unsigned int *)kp->arg) = num;
 
+	/* Defer the work until fuse_dentry_tree_init() sets it up. */
+	if (!inval_wq_ready)
+		return 0;
+
 	if (num && !old)
 		schedule_delayed_work(&dentry_tree_work,
 				      secs_to_jiffies(num));
@@ -224,6 +229,14 @@ void fuse_dentry_tree_init(void)
 		dentry_hash[i].tree = RB_ROOT;
 	}
 	INIT_DELAYED_WORK(&dentry_tree_work, fuse_dentry_tree_work);
+
+	kernel_param_lock(THIS_MODULE);
+	inval_wq_ready = true;
+
+	if (inval_wq)
+		schedule_delayed_work(&dentry_tree_work,
+				      secs_to_jiffies(inval_wq));
+	kernel_param_unlock(THIS_MODULE);
 }
 
 void fuse_dentry_tree_cleanup(void)
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/2 fuse: do not schedule dentry_tree_work after fuse_dentry_tree_cleanup()

In reply to Sang-Heon Jeon

fuse_dentry_tree_cleanup() cancels dentry_tree_work at module unload.

But a sysfs write can call inval_wq_set() after that and schedule the work again, leading to use-after-free.

So do not schedule the work in inval_wq_set() after fuse_dentry_tree_cleanup() cancels it. Also hold kernel_param_lock() in fuse_dentry_tree_cleanup(), because a sysfs write can call inval_wq_set() at the same time.

Fixes ab84ad597386 ("fuse: new work queue to periodically invalidate expired dentries")
---
 fs/fuse/dir.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/fuse/dir.c +7 -1
index 0d5d04df477b..04ca6c9be2bf 100644
@@ -63,7 +63,10 @@ static int inval_wq_set(const char *val, const struct kernel_param *kp)
 
 	*((unsigned int *)kp->arg) = num;
 
-	/* Defer the work until fuse_dentry_tree_init() sets it up. */
+	/*
+	 * Do not touch the work before fuse_dentry_tree_init() or after
+	 * fuse_dentry_tree_cleanup().
+	 */
 	if (!inval_wq_ready)
 		return 0;
 
@@ -243,8 +246,11 @@ void fuse_dentry_tree_cleanup(void)
 {
 	int i;
 
+	kernel_param_lock(THIS_MODULE);
+	inval_wq_ready = false;
 	inval_wq = 0;
 	cancel_delayed_work_sync(&dentry_tree_work);
+	kernel_param_unlock(THIS_MODULE);
 
 	for (i = 0; i < FUSE_HASH_SIZE; i++)
 		WARN_ON_ONCE(!RB_EMPTY_ROOT(&dentry_hash[i].tree));
Signature · 1 line
2.53.0