Skip to contentSkip to conversation

a few devtmpfs patches

PATCH New Rasmus Villemoes 1 participant 6 messages Jan 15, 2020 Driver core

lore
Share & tools
lore
#0 lore raw

cover a few devtmpfs patches

Patch 1 fixes a mostly theoretical problem, but as this is core code, the pattern might be copy-pasted, so it might as well be fixed. The remaining ones reduce memory footprint a bit by initifying a function and factoring out common code.

Rasmus Villemoes (5):
devtmpfs: fix theoretical stale pointer deref in devtmpfsd()
devtmpfs: factor out setup part of devtmpfsd()
devtmpfs: simplify initialization of mount_dev
devtmpfs: initify a bit
devtmpfs: factor out common tail of devtmpfs_{create,delete}_node

 drivers/base/devtmpfs.c | 79 ++++++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 40 deletions(-)
Signature · 1 line
2.23.0
#1 lore raw

PATCH 1/5 devtmpfs: fix theoretical stale pointer deref in devtmpfsd()

After complete(&setup_done), devtmpfs_init proceeds and may actually return, invalidating the *err pointer, before devtmpfsd() proceeds to reading back *err.

This is of course completely theoretical since the error conditions never trigger in practice, and even if they did, nobody cares about the exit value from a kernel thread, so it doesn't matter if we happen to read back some garbage from some other stack frame. Still, this isn't a pattern that should be copy-pasted, so fix it.

---
 drivers/base/devtmpfs.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/base/devtmpfs.c +8 -6
index 6cdbf1531238..ccb046fe12b7 100644
@@ -390,12 +390,13 @@ static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
 
 static int devtmpfsd(void *p)
 {
-	int *err = p;
-	*err = ksys_unshare(CLONE_NEWNS);
-	if (*err)
+	int err;
+
+	err = ksys_unshare(CLONE_NEWNS);
+	if (err)
 		goto out;
-	*err = do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
-	if (*err)
+	err = do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
+	if (err)
 		goto out;
 	ksys_chdir("/.."); /* will traverse into overmounted root */
 	ksys_chroot(".");
@@ -421,8 +422,9 @@ static int devtmpfsd(void *p)
 	}
 	return 0;
 out:
+	*(int *)p = err;
 	complete(&setup_done);
-	return *err;
+	return err;
 }
 
 /*
Signature · 1 line
2.23.0
#2 lore raw

PATCH 2/5 devtmpfs: factor out setup part of devtmpfsd()

In reply to Rasmus Villemoes

Factor out the setup part of devtmpfsd() to make it a bit easier to see that we always call setup_done() exactly once (provided of course the kthread is succesfully created).

---
 drivers/base/devtmpfs.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/base/devtmpfs.c +12 -5
index ccb046fe12b7..963889331bb4 100644
@@ -388,7 +388,7 @@ static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
 		return handle_remove(name, dev);
 }
 
-static int devtmpfsd(void *p)
+static int devtmpfs_setup(void *p)
 {
 	int err;
 
@@ -400,7 +400,18 @@ static int devtmpfsd(void *p)
 		goto out;
 	ksys_chdir("/.."); /* will traverse into overmounted root */
 	ksys_chroot(".");
+out:
+	*(int *)p = err;
 	complete(&setup_done);
+	return err;
+}
+
+static int devtmpfsd(void *p)
+{
+	int err = devtmpfs_setup(p);
+
+	if (err)
+		return err;
 	while (1) {
 		spin_lock(&req_lock);
 		while (requests) {
@@ -421,10 +432,6 @@ static int devtmpfsd(void *p)
 		schedule();
 	}
 	return 0;
-out:
-	*(int *)p = err;
-	complete(&setup_done);
-	return err;
 }
 
 /*
Signature · 1 line
2.23.0
#3 lore raw

PATCH 3/5 devtmpfs: simplify initialization of mount_dev

In reply to Rasmus Villemoes

Avoid a bit of ifdeffery by using the IS_ENABLED() helper.

---
 drivers/base/devtmpfs.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/base/devtmpfs.c +1 -5
index 963889331bb4..693390d9c545 100644
@@ -30,11 +30,7 @@
 
 static struct task_struct *thread;
 
-#if defined CONFIG_DEVTMPFS_MOUNT
-static int mount_dev = 1;
-#else
-static int mount_dev;
-#endif
+static int mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
 
 static DEFINE_SPINLOCK(req_lock);
 
Signature · 1 line
2.23.0
#4 lore raw

PATCH 4/5 devtmpfs: initify a bit

In reply to Rasmus Villemoes

devtmpfs_mount() is only called from prepare_namespace() in init/do_mounts.c, which is an __init function, so devtmpfs_mount() can also be moved to .init.text.

Then the mount_dev static variable is only referenced from __init functions (devtmpfs_mount and its initializer function mount_param).

---
 drivers/base/devtmpfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/base/devtmpfs.c +2 -2
index 693390d9c545..56632fb22fc0 100644
@@ -30,7 +30,7 @@
 
 static struct task_struct *thread;
 
-static int mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
+static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
 
 static DEFINE_SPINLOCK(req_lock);
 
@@ -355,7 +355,7 @@ static int handle_remove(const char *nodename, struct device *dev)
  * If configured, or requested by the commandline, devtmpfs will be
  * auto-mounted after the kernel mounted the root filesystem.
  */
-int devtmpfs_mount(void)
+int __init devtmpfs_mount(void)
 {
 	int err;
 
Signature · 1 line
2.23.0
#5 lore raw

PATCH 5/5 devtmpfs: factor out common tail of devtmpfs_{create,delete}_node

In reply to Rasmus Villemoes

There's some common boilerplate in devtmpfs_{create,delete}_node, put that in a little helper.

---
 drivers/base/devtmpfs.c | 44 ++++++++++++++++++-----------------------
 1 file changed, 19 insertions(+), 25 deletions(-)
drivers/base/devtmpfs.c +19 -25
index 56632fb22fc0..5995c437cbdf 100644
@@ -89,6 +89,23 @@ static inline int is_blockdev(struct device *dev)
 static inline int is_blockdev(struct device *dev) { return 0; }
 #endif
 
+static int devtmpfs_submit_req(struct req *req, const char *tmp)
+{
+	init_completion(&req->done);
+
+	spin_lock(&req_lock);
+	req->next = requests;
+	requests = req;
+	spin_unlock(&req_lock);
+
+	wake_up_process(thread);
+	wait_for_completion(&req->done);
+
+	kfree(tmp);
+
+	return req->err;
+}
+
 int devtmpfs_create_node(struct device *dev)
 {
 	const char *tmp = NULL;
@@ -113,19 +130,7 @@ int devtmpfs_create_node(struct device *dev)
 
 	req.dev = dev;
 
-	init_completion(&req.done);
-
-	spin_lock(&req_lock);
-	req.next = requests;
-	requests = &req;
-	spin_unlock(&req_lock);
-
-	wake_up_process(thread);
-	wait_for_completion(&req.done);
-
-	kfree(tmp);
-
-	return req.err;
+	return devtmpfs_submit_req(&req, tmp);
 }
 
 int devtmpfs_delete_node(struct device *dev)
@@ -143,18 +148,7 @@ int devtmpfs_delete_node(struct device *dev)
 	req.mode = 0;
 	req.dev = dev;
 
-	init_completion(&req.done);
-
-	spin_lock(&req_lock);
-	req.next = requests;
-	requests = &req;
-	spin_unlock(&req_lock);
-
-	wake_up_process(thread);
-	wait_for_completion(&req.done);
-
-	kfree(tmp);
-	return req.err;
+	return devtmpfs_submit_req(&req, tmp);
 }
 
 static int dev_mkdir(const char *name, umode_t mode)
Signature · 1 line
2.23.0