RE: [PATCH 5/7] misc: reject duplicate names in misc_register()
From: Justin He
Date: Thu May 14 2026 - 03:27:47 EST
> -----Original Message-----
> From: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> Sent: Thursday, May 14, 2026 3:00 PM
> To: Justin He <Justin.He@xxxxxxx>
> Cc: linux-kernel@xxxxxxxxxxxxxxx; linux-fsdevel@xxxxxxxxxxxxxxx; linux-perf-
> users@xxxxxxxxxxxxxxx; linux-kselftest@xxxxxxxxxxxxxxx; kunit-
> dev@xxxxxxxxxxxxxxxx; kasan-dev@xxxxxxxxxxxxxxxx; linux-
> mm@xxxxxxxxx; Arnd Bergmann <arnd@xxxxxxxx>; Alexander Viro
> <viro@xxxxxxxxxxxxxxxxxx>; Christian Brauner <brauner@xxxxxxxxxx>; Jan Kara
> <jack@xxxxxxx>; Peter Zijlstra <peterz@xxxxxxxxxxxxx>; Ingo Molnar
> <mingo@xxxxxxxxxx>; Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>;
> Namhyung Kim <namhyung@xxxxxxxxxx>; Mark Rutland
> <Mark.Rutland@xxxxxxx>; Alexander Shishkin
> <alexander.shishkin@xxxxxxxxxxxxxxx>; Jiri Olsa <jolsa@xxxxxxxxxx>; Ian
> Rogers <irogers@xxxxxxxxxx>; Adrian Hunter <adrian.hunter@xxxxxxxxx>;
> James Clark <james.clark@xxxxxxxxxx>; Brendan Higgins
> <brendan.higgins@xxxxxxxxx>; David Gow <david@xxxxxxxxxxxx>; Rae Moar
> <raemoar63@xxxxxxxxx>; Alexander Potapenko <glider@xxxxxxxxxx>;
> Marco Elver <elver@xxxxxxxxxx>; Dmitry Vyukov <dvyukov@xxxxxxxxxx>;
> Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>; Paul E. McKenney
> <paulmck@xxxxxxxxxx>; Petr Mladek <pmladek@xxxxxxxx>; Kees Cook
> <kees@xxxxxxxxxx>; David Disseldorp <ddiss@xxxxxxx>
> Subject: Re: [PATCH 5/7] misc: reject duplicate names in misc_register()
>
> On Thu, May 14, 2026 at 05:04:53AM +0000, Jia He wrote:
> > 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 <arnd@xxxxxxxx>
> > To: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
>
> This should be Cc: right?
>
> >
> > Signed-off-by: Jia He <justin.he@xxxxxxx>
> > ---
> > 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;
> > + }
> > + }
> > + }
>
> Don't do additional {} where not needed.
>
> And as the current code works properly, we are relying on sysfs for the
> rejection, why do this now here? The stack dump is good, it shows the
> offending caller, so they can fix it up better. So why change this?
>
Thanks for the feedback.
I’ll drop the two patches that silence the sysfs warning for duplicate filenames.
---
Cheers,
Justin He(Jia He)