mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] debugfs: warn if file creation failed due to uninitialized debugfs
@ 2026-06-11 13:50 Yohei Kojima
  2026-06-12  8:14 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 4+ messages in thread
From: Yohei Kojima @ 2026-06-11 13:50 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich
  Cc: Yohei Kojima, driver-core, linux-kernel

Improve debugfs_start_creating() to warn if it was used before debugfs
initialization. It silently returned ERR_PTR(-ENOENT) before, but it is
hard to find the cause of failure especially if it was called by
debugfs_create_dir(), because the document of the function says:

> NOTE: it's expected that most callers should _ignore_ the errors returned
> by this function. Other debugfs functions handle the fact that the "dentry"
> passed to them could be an error and they don't crash in that case.
> Drivers should generally work fine even if debugfs fails to init anyway.

Signed-off-by: Yohei Kojima <yk@y-koj.net>
---
I tested my patch on my x86_64 qemu/kvm environment with the following
code (I appended this to the end of fs/debugfs/inode.c, but it should
run anywhere in the kernel):

static int __init try_making_debugfs_early(void)
{
       struct dentry *dentry;
       dentry = debugfs_create_dir("early_debugfs", NULL);
       WARN_ON(!IS_ERR(dentry));
       WARN_ON(PTR_ERR(dentry) != -ENOENT);

       return 0;
}
early_initcall(try_making_debugfs_early);

static int __init try_making_debugfs_pure(void)
{
       struct dentry *dentry;
       dentry = debugfs_create_dir("pure_debugfs", NULL);
       WARN_ON(!IS_ERR(dentry));
       WARN_ON(PTR_ERR(dentry) != -ENOENT);

       return 0;
}
pure_initcall(try_making_debugfs_pure);

static u64 postcore_u64 = 0x0123456789abcdef;

static int __init try_making_debugfs_postcore(void)
{
       struct dentry *dentry;
       dentry = debugfs_create_dir("postcore_debugfs", NULL);
       WARN_ON(IS_ERR(dentry));
       debugfs_create_u64("u64_value", 0400, dentry, &postcore_u64);

       pr_info("Successfully created a directory and a file in debugfs!");

       return 0;
}
postcore_initcall(try_making_debugfs_postcore);


And it warned as expected if it was too early, and succeeded if it was
called after debugfs initialization:

[    0.114434] Timer migration: 1 hierarchy levels; 8 children per group; 1 crossnode level
[    0.114434] debugfs: Unable to create file 'early_debugfs', debugfs is not initialized yet
[    0.114434] NMI watchdog: Perf NMI watchdog permanently disabled
[    0.114434] smp: Bringing up secondary CPUs ...
[    0.114636] smpboot: x86: Booting SMP configuration:
[    0.114910] .... node  #0, CPUs:      #1 #2 #3
[    0.115139] smp: Brought up 1 node, 4 CPUs
[    0.115139] smpboot: Total of 4 processors activated (27351.00 BogoMIPS)
[    0.115466] node 0 deferred pages initialised in 4ms
[    0.117793] Memory: 3939444K/4193784K available (19486K kernel code, 3500K rwdata, 8176K rodata, 4664K init, 4548K bss, 248632K reserved, 0K cma-reserved)
[    0.118647] devtmpfs: initialized
[    0.118647] x86/mm: Memory block size: 128MB
[    0.118647] debugfs: Unable to create file 'pure_debugfs', debugfs is not initialized yet
[    0.118837] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 6370867519511994 ns
[    0.121105] posixtimers hash table entries: 2048 (order: 3, 32768 bytes, linear)
[    0.121508] futex hash table entries: 1024 (65536 bytes on 1 NUMA nodes, total 64 KiB, linear).
[    0.122058] PM: RTC time: 13:28:19, date: 2026-06-11
[    0.122613] NET: Registered PF_NETLINK/PF_ROUTE protocol family
[    0.122989] DMA: preallocated 512 KiB GFP_KERNEL pool for atomic allocations
[    0.123373] DMA: preallocated 512 KiB GFP_KERNEL|GFP_DMA pool for atomic allocations
[    0.123793] DMA: preallocated 512 KiB GFP_KERNEL|GFP_DMA32 pool for atomic allocations
[    0.124222] audit: initializing netlink subsys (disabled)
[    0.124461] audit: type=2000 audit(1781184499.707:1): state=initialized audit_enabled=0 res=1
[    0.124461] debugfs: Successfully created a directory and a file in debugfs!
[    0.124926] thermal_sys: Registered thermal governor 'fair_share'

Thanks,
Yohei
---
 fs/debugfs/inode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
index 4598142355b9..e054e62919ec 100644
--- a/fs/debugfs/inode.c
+++ b/fs/debugfs/inode.c
@@ -368,8 +368,11 @@ static struct dentry *debugfs_start_creating(const char *name,
 	if (!debugfs_enabled)
 		return ERR_PTR(-EPERM);
 
-	if (!debugfs_initialized())
+	if (!debugfs_initialized()) {
+		pr_err("Unable to create file '%s', debugfs is not initialized yet\n",
+		       name);
 		return ERR_PTR(-ENOENT);
+	}
 
 	pr_debug("creating file '%s'\n", name);
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] debugfs: warn if file creation failed due to uninitialized debugfs
  2026-06-11 13:50 [PATCH] debugfs: warn if file creation failed due to uninitialized debugfs Yohei Kojima
@ 2026-06-12  8:14 ` Greg Kroah-Hartman
  2026-06-13  9:45   ` Yohei Kojima
  0 siblings, 1 reply; 4+ messages in thread
From: Greg Kroah-Hartman @ 2026-06-12  8:14 UTC (permalink / raw)
  To: Yohei Kojima
  Cc: Rafael J. Wysocki, Danilo Krummrich, driver-core, linux-kernel

On Thu, Jun 11, 2026 at 10:50:16PM +0900, Yohei Kojima wrote:
> Improve debugfs_start_creating() to warn if it was used before debugfs
> initialization. It silently returned ERR_PTR(-ENOENT) before, but it is
> hard to find the cause of failure especially if it was called by
> debugfs_create_dir(), because the document of the function says:
> 
> > NOTE: it's expected that most callers should _ignore_ the errors returned
> > by this function. Other debugfs functions handle the fact that the "dentry"
> > passed to them could be an error and they don't crash in that case.
> > Drivers should generally work fine even if debugfs fails to init anyway.
> 
> Signed-off-by: Yohei Kojima <yk@y-koj.net>
> ---
> I tested my patch on my x86_64 qemu/kvm environment with the following
> code (I appended this to the end of fs/debugfs/inode.c, but it should
> run anywhere in the kernel):
> 
> static int __init try_making_debugfs_early(void)
> {
>        struct dentry *dentry;
>        dentry = debugfs_create_dir("early_debugfs", NULL);
>        WARN_ON(!IS_ERR(dentry));
>        WARN_ON(PTR_ERR(dentry) != -ENOENT);
> 
>        return 0;
> }
> early_initcall(try_making_debugfs_early);
> 
> static int __init try_making_debugfs_pure(void)
> {
>        struct dentry *dentry;
>        dentry = debugfs_create_dir("pure_debugfs", NULL);
>        WARN_ON(!IS_ERR(dentry));
>        WARN_ON(PTR_ERR(dentry) != -ENOENT);
> 
>        return 0;
> }
> pure_initcall(try_making_debugfs_pure);
> 
> static u64 postcore_u64 = 0x0123456789abcdef;
> 
> static int __init try_making_debugfs_postcore(void)
> {
>        struct dentry *dentry;
>        dentry = debugfs_create_dir("postcore_debugfs", NULL);
>        WARN_ON(IS_ERR(dentry));
>        debugfs_create_u64("u64_value", 0400, dentry, &postcore_u64);
> 
>        pr_info("Successfully created a directory and a file in debugfs!");
> 
>        return 0;
> }
> postcore_initcall(try_making_debugfs_postcore);
> 
> 
> And it warned as expected if it was too early, and succeeded if it was
> called after debugfs initialization:
> 
> [    0.114434] Timer migration: 1 hierarchy levels; 8 children per group; 1 crossnode level
> [    0.114434] debugfs: Unable to create file 'early_debugfs', debugfs is not initialized yet
> [    0.114434] NMI watchdog: Perf NMI watchdog permanently disabled
> [    0.114434] smp: Bringing up secondary CPUs ...
> [    0.114636] smpboot: x86: Booting SMP configuration:
> [    0.114910] .... node  #0, CPUs:      #1 #2 #3
> [    0.115139] smp: Brought up 1 node, 4 CPUs
> [    0.115139] smpboot: Total of 4 processors activated (27351.00 BogoMIPS)
> [    0.115466] node 0 deferred pages initialised in 4ms
> [    0.117793] Memory: 3939444K/4193784K available (19486K kernel code, 3500K rwdata, 8176K rodata, 4664K init, 4548K bss, 248632K reserved, 0K cma-reserved)
> [    0.118647] devtmpfs: initialized
> [    0.118647] x86/mm: Memory block size: 128MB
> [    0.118647] debugfs: Unable to create file 'pure_debugfs', debugfs is not initialized yet
> [    0.118837] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 6370867519511994 ns
> [    0.121105] posixtimers hash table entries: 2048 (order: 3, 32768 bytes, linear)
> [    0.121508] futex hash table entries: 1024 (65536 bytes on 1 NUMA nodes, total 64 KiB, linear).
> [    0.122058] PM: RTC time: 13:28:19, date: 2026-06-11
> [    0.122613] NET: Registered PF_NETLINK/PF_ROUTE protocol family
> [    0.122989] DMA: preallocated 512 KiB GFP_KERNEL pool for atomic allocations
> [    0.123373] DMA: preallocated 512 KiB GFP_KERNEL|GFP_DMA pool for atomic allocations
> [    0.123793] DMA: preallocated 512 KiB GFP_KERNEL|GFP_DMA32 pool for atomic allocations
> [    0.124222] audit: initializing netlink subsys (disabled)
> [    0.124461] audit: type=2000 audit(1781184499.707:1): state=initialized audit_enabled=0 res=1
> [    0.124461] debugfs: Successfully created a directory and a file in debugfs!
> [    0.124926] thermal_sys: Registered thermal governor 'fair_share'
> 
> Thanks,
> Yohei
> ---
>  fs/debugfs/inode.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
> index 4598142355b9..e054e62919ec 100644
> --- a/fs/debugfs/inode.c
> +++ b/fs/debugfs/inode.c
> @@ -368,8 +368,11 @@ static struct dentry *debugfs_start_creating(const char *name,
>  	if (!debugfs_enabled)
>  		return ERR_PTR(-EPERM);
>  
> -	if (!debugfs_initialized())
> +	if (!debugfs_initialized()) {
> +		pr_err("Unable to create file '%s', debugfs is not initialized yet\n",
> +		       name);
>  		return ERR_PTR(-ENOENT);
> +	}

While I understand your frustration when adding new code to the kernel
that would trigger this, does it actually fail on a normal boot today?
If not, is this really needed?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] debugfs: warn if file creation failed due to uninitialized debugfs
  2026-06-12  8:14 ` Greg Kroah-Hartman
@ 2026-06-13  9:45   ` Yohei Kojima
  2026-09-03 12:41     ` Mikhail Gavrilov
  0 siblings, 1 reply; 4+ messages in thread
From: Yohei Kojima @ 2026-06-13  9:45 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Rafael J. Wysocki, Danilo Krummrich, driver-core, linux-kernel

Hi Greg,

Thank you for the response.

On Fri, Jun 12, 2026 at 10:14:34AM +0200, Greg Kroah-Hartman wrote:
> On Thu, Jun 11, 2026 at 10:50:16PM +0900, Yohei Kojima wrote:
> > Improve debugfs_start_creating() to warn if it was used before debugfs
> > initialization. It silently returned ERR_PTR(-ENOENT) before, but it is
> > hard to find the cause of failure especially if it was called by
> > debugfs_create_dir(), because the document of the function says:
> > 
> > > NOTE: it's expected that most callers should _ignore_ the errors returned
> > > by this function. Other debugfs functions handle the fact that the "dentry"
> > > passed to them could be an error and they don't crash in that case.
> > > Drivers should generally work fine even if debugfs fails to init anyway.
> > 
> > Signed-off-by: Yohei Kojima <yk@y-koj.net>
> > ---
> >  fs/debugfs/inode.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/fs/debugfs/inode.c b/fs/debugfs/inode.c
> > index 4598142355b9..e054e62919ec 100644
> > --- a/fs/debugfs/inode.c
> > +++ b/fs/debugfs/inode.c
> > @@ -368,8 +368,11 @@ static struct dentry *debugfs_start_creating(const char *name,
> >  	if (!debugfs_enabled)
> >  		return ERR_PTR(-EPERM);
> >  
> > -	if (!debugfs_initialized())
> > +	if (!debugfs_initialized()) {
> > +		pr_err("Unable to create file '%s', debugfs is not initialized yet\n",
> > +		       name);
> >  		return ERR_PTR(-ENOENT);
> > +	}
> 
> While I understand your frustration when adding new code to the kernel
> that would trigger this, does it actually fail on a normal boot today?

No. As far as I tested, no existing code faied there.

> If not, is this really needed?

At least I want. Also, debugfs_start_creating() already warns if the
caller is trying to create a dentry with the same name of an existing
dentry, which also doesn't happen on a normal boot. The warning for
uninitialized debugfs helps developers avoid pitfalls in the same way.

Thanks,
Yohei

> 
> thanks,
> 
> greg k-h

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] debugfs: warn if file creation failed due to uninitialized debugfs
  2026-06-13  9:45   ` Yohei Kojima
@ 2026-09-03 12:41     ` Mikhail Gavrilov
  0 siblings, 0 replies; 4+ messages in thread
From: Mikhail Gavrilov @ 2026-09-03 12:41 UTC (permalink / raw)
  To: Yohei Kojima, Greg Kroah-Hartman
  Cc: Rafael J. Wysocki, Danilo Krummrich, driver-core, linux-kernel

On Sat, Jun 13, 2026 at 06:45:53PM +0900, Yohei Kojima wrote:
> > While I understand your frustration when adding new code to the kernel
> > that would trigger this, does it actually fail on a normal boot today?
>
> No. As far as I tested, no existing code faied there.

It does, with CONFIG_REF_TRACKER=y and CONFIG_DEBUG_FS=y.  Since this
commit every boot here prints:

  debugfs: Unable to create file 'net_refcnt@(____ptrval____)',
    debugfs is not initialized yet
  debugfs: Unable to create file 'net_notrefcnt@(____ptrval____)',
    debugfs is not initialized yet

This is not a race.  net_ns_init() is called directly from start_kernel(),
before any initcall runs, and debugfs_init() is a core_initcall, so
init_net's two ref_tracker directories always miss.  Nothing is actually
lost - net/core/net_namespace.c re-registers them from a late_initcall(),
and both files are present under /sys/kernel/debug/ref_tracker/ once the
system is up - but the error is printed before that happens.

I bisected it to this commit and sent a fix that keeps the warning for the
case you were after:

  https://lore.kernel.org/all/20260903115135.63210-1-mikhail.v.gavrilov@gmail.com/

-- 
Thanks,
Mikhail

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-03 12:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-11 13:50 [PATCH] debugfs: warn if file creation failed due to uninitialized debugfs Yohei Kojima
2026-06-12  8:14 ` Greg Kroah-Hartman
2026-06-13  9:45   ` Yohei Kojima
2026-09-03 12:41     ` Mikhail Gavrilov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®