[PATCH] debugfs: warn if file creation failed due to uninitialized debugfs
From: Yohei Kojima
Date: Thu Jun 11 2026 - 09:58:39 EST
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@xxxxxxxxx>
---
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