The miscdev kunit suite registers two miscdevices with the same name
and expects -EEXIST. The second call currently goes all the way to
sysfs_create_dir_ns(), which prints "cannot create duplicate filename"
with a backtrace on every run.

Walk misc_list under misc_mtx, return -EEXIST on a name collision and
free the just-allocated minor before returning.

To: Arnd Bergmann <[email protected]>
To: Greg Kroah-Hartman <[email protected]>

Signed-off-by: Jia He <[email protected]>
---
 drivers/char/misc.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index 726516fb0a3b..d6ffa21ac495 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -248,6 +248,28 @@ int misc_register(struct miscdevice *misc)
                }
        }
 
+       /*
+        * Detect duplicate names up-front so the subsequent
+        * device_create_with_groups() does not trip
+        * sysfs_create_dir_ns()->sysfs_warn_dup(), which unconditionally
+        * dumps a stack trace. Both the existing miscdev kunit suite
+        * (miscdev_test_duplicate_name) and any caller racing on the same
+        * name would otherwise pollute dmesg on every -EEXIST.
+        */
+       {
+               struct miscdevice *c;
+
+               list_for_each_entry(c, &misc_list, list) {
+                       if (strcmp(c->name, misc->name) == 0) {
+                               misc_minor_free(misc->minor);
+                               if (is_dynamic)
+                                       misc->minor = MISC_DYNAMIC_MINOR;
+                               err = -EEXIST;
+                               goto out;
+                       }
+               }
+       }
+
        dev = MKDEV(MISC_MAJOR, misc->minor);
 
        misc->this_device =
-- 
2.34.1


Reply via email to