tracefs: Simplify get_dname() with kmemdup_nul()
PATCH 1/1 tracefs: Simplify get_dname() with kmemdup_nul()
In fs/tracefs/inode.c, get_dname() allocates a buffer with kmalloc() to hold a dentry name, followed by a memcpy() and manual null-termination.
Replace this open-coded pattern with the standard kmemdup_nul() helper. Additionally, remove the now single-use local variables dname and len. This simplifies the function to a single line, reducing visual clutter and making the memory-safety intent immediately obvious without changing any functional behavior.
Testing:
Booted a custom kernel natively in virtme-ng (ARM64). Triggered tracefs inode and dentry allocation by creating and removing a custom directory under a temporary tracefs mount. Verified that the instance is created successfully and that no memory errors or warnings are emitted in dmesg.
--- fs/tracefs/inode.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
fs/tracefs/inode.c +1 -11
@@ -96,17 +96,7 @@ static struct tracefs_dir_ops {static char *get_dname (struct dentry *dentry) { -const char *dname; -char *name; -int len = dentry->d_name.len; - - dname = dentry->d_name.name; - name =kmalloc (len +1 ,GFP_KERNEL ); -if (!name) -return NULL ; -memcpy (name, dname, len); - name[len] =0 ; -return name; +return kmemdup_nul (dentry->d_name.name, dentry->d_name.len,GFP_KERNEL ); } static struct dentry *tracefs_syscall_mkdir(struct mnt_idmap *idmap,
Signature · 1 line
2.51.0
Quoted 10 lines · AnishMulay
index
d9d8932a7b9c9..86ba8dc25aaef100644fs/tracefs/inode.c +0
-3@@ -96,17 +96,7 @@ static struct tracefs_dir_ops {static char *get_dname (struct dentry *dentry) { -const char *dname; -char *name; -int len = dentry->d_name.len;
fs/tracefs/inode.c static struct tracefs_dir_ops { · line 100
Quoted 10 lines
fs/tracefs/inode.c +1
-8- - dname = dentry->d_name.name; - name =kmalloc (len +1 ,GFP_KERNEL ); -if (!name) -return NULL ; -memcpy (name, dname, len); - name[len] =0 ; -return name; +return kmemdup_nul (dentry->d_name.name, dentry->d_name.len,GFP_KERNEL ); }
Why not have the callers use {take,release}_dentry_name_snapshot() instead of doing any allocations at all?
I mean,
static struct dentry *tracefs_syscall_mkdir(struct mnt_idmap *idmap,
struct inode *inode, struct dentry *dentry, umode_t mode)
{
struct tracefs_inode *ti;
struct name_snapshot s;
int ret;
take_dentry_name_snapshot(&s, dentry);
...
ret = tracefs_ops.mkdir(s.name.name);
release_dentry_name_snapshot(&s);
...
}and similar on the rmdir side. Then remove get_dname()...
Steven Rostedt
maintainerQuoted 41 lines · On Fri, 27 Feb 2026 20:18:24 +0000 Al Viro <[email protected]> wrote:
Quoted 10 lines · AnishMulay
index
d9d8932a7b9c9..86ba8dc25aaef100644fs/tracefs/inode.c +0
-3@@ -96,17 +96,7 @@ static struct tracefs_dir_ops {static char *get_dname (struct dentry *dentry) { -const char *dname; -char *name; -int len = dentry->d_name.len;fs/tracefs/inode.c static struct tracefs_dir_ops { · line 100
Quoted 10 lines
fs/tracefs/inode.c +1
-8- - dname = dentry->d_name.name; - name =kmalloc (len +1 ,GFP_KERNEL ); -if (!name) -return NULL ; -memcpy (name, dname, len); - name[len] =0 ; -return name; +return kmemdup_nul (dentry->d_name.name, dentry->d_name.len,GFP_KERNEL ); }Why not have the callers use {take,release}_dentry_name_snapshot() instead of doing any allocations at all?
I mean,
static struct dentry *tracefs_syscall_mkdir(structmnt_idmap*idmap,
struct inode *inode, struct dentry *dentry,umode_tmode){ struct tracefs_inode *ti; struct name_snapshot s; int ret; take_dentry_name_snapshot(&s, dentry); ... ret = tracefs_ops.mkdir(s.name.name); release_dentry_name_snapshot(&s); ... }
Quoted 1 line
and similar on the rmdir side. Then remove
get_dname()...
That sounds good to me.
-- Steve
PATCH v2 1/1 tracefs: Use dentry name snapshots instead of heap allocation
In fs/tracefs/inode.c, tracefs_syscall_mkdir() and tracefs_syscall_rmdir() previously used a local helper, get_dname(), which allocated a temporary buffer on the heap via kmalloc() to hold the dentry name. This introduced unnecessary overhead, an ENOMEM failure path, and required manual memory cleanup via kfree().
As suggested by Al Viro, replace this heap allocation with the VFS dentry name snapshot API. By stack-allocating a struct name_snapshot and using take_dentry_name_snapshot() and release_dentry_name_snapshot(), we safely capture the dentry name locklessly, eliminate the heap allocation entirely, and remove the now-obsolete error handling paths. The get_dname() helper is completely removed.
Testing:
Booted a custom kernel natively in virtme-ng (ARM64). Triggered tracefs inode and dentry allocation by creating and removing a custom directory under a temporary tracefs mount. Verified that the instance is created successfully and that no memory errors or warnings are emitted in dmesg.
--- fs/tracefs/inode.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-)
fs/tracefs/inode.c +8 -21
@@ -94,23 +94,14 @@ static struct tracefs_dir_ops {int (*rmdir)(const char *name); } tracefs_ops__ro_after_init ; -static char *get_dname (struct dentry *dentry) -{ -return kmemdup_nul (dentry->d_name.name, dentry->d_name.len,GFP_KERNEL ); -} -static struct dentry *tracefs_syscall_mkdir (struct mnt_idmap *idmap,struct inode *inode,struct dentry *dentry,umode_t mode) {struct tracefs_inode *ti; -char *name; +struct name_snapshot name;int ret; - name =get_dname (dentry); -if (!name) -return ERR_PTR (-ENOMEM ); -/* * This is a new directory that does not take the default of * the rootfs. It becomes the default permissions for all the
@@ -125,24 +116,20 @@ static struct dentry *tracefs_syscall_mkdir(struct mnt_idmap *idmap,* the files within the tracefs system. It is up to the individual * mkdir routine to handle races. */ +take_dentry_name_snapshot (&name, dentry);inode_unlock (inode); - ret = tracefs_ops.mkdir (name); + ret = tracefs_ops.mkdir (name.name.name);inode_lock (inode); - -kfree (name); +release_dentry_name_snapshot (&name);return ERR_PTR (ret); }static int tracefs_syscall_rmdir (struct inode *inode,struct dentry *dentry) { -char *name; +struct name_snapshot name;int ret; - name =get_dname (dentry); -if (!name) -return -ENOMEM ; -/* * The rmdir call can call the generic functions that create * the files within the tracefs system. It is up to the individual
@@ -150,15 +137,15 @@ static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)* This time we need to unlock not only the parent (inode) but * also the directory that is being deleted. */ +take_dentry_name_snapshot (&name, dentry);inode_unlock (inode);inode_unlock (d_inode (dentry)); - ret = tracefs_ops.rmdir (name); + ret = tracefs_ops.rmdir (name.name.name);inode_lock_nested (inode,I_MUTEX_PARENT );inode_lock (d_inode (dentry)); - -kfree (name); +release_dentry_name_snapshot (&name);return ret; }
Signature · 1 line
2.51.0
Steven Rostedt
maintainerRe: [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation
Quoted 13 lines · On Fri, 27 Feb 2026 16:15:05 -0500 AnishMulay <[email protected]> wrote:
In
fs/tracefs/inode.c,tracefs_syscall_mkdir()andtracefs_syscall_rmdir()previously used a local helper,get_dname(), which allocated a temporary buffer on the heap viakmalloc()to hold the dentry name. This introduced unnecessary overhead, an ENOMEM failure path, and required manual memory cleanup viakfree().As suggested by Al Viro, replace this heap allocation with the VFS dentry name snapshot API. By stack-allocating a
struct name_snapshotand usingtake_dentry_name_snapshot()andrelease_dentry_name_snapshot(), we safely capture the dentry name locklessly, eliminate the heap allocation entirely, and remove the now-obsolete error handling paths. Theget_dname()helper is completely removed.
Quoted 8 lines
Testing:
Booted a custom kernel natively in virtme-ng (ARM64). Triggered tracefs inode and dentry allocation by creating and removing a custom directory under a temporary tracefs mount. Verified that the instance is created successfully and that no memory errors or warnings are emitted in dmesg.Signed-off-by AnishMulay <[email protected]>---
It's nice to add version change history below the "---" with a link to the previous patch:
Changes since v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/
- Use the helper functions take/release_dentry_name_snapshot() instead of allocating the name. (Al Viro)
As when I pull in a patch, my scripts add a link to the patch itself, and having that patch have a link to the previous version is always helpful.
(This email serves that purpose for this patch)
-- Steve
Steven Rostedt
maintainerRe: [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation
In reply to AnishMulayQuoted 6 lines · On Fri, 27 Feb 2026 16:15:05 -0500 AnishMulay <[email protected]> wrote:
fs/tracefs/inode.c +0
-0@@ -94,23 +94,14 @@ static struct tracefs_dir_ops {int (*rmdir)(const char *name);
fs/tracefs/inode.c static struct tracefs_dir_ops { · line 99
Quoted 10 lines
fs/tracefs/inode.c +0
-5} tracefs_ops__ro_after_init ; -static char *get_dname (struct dentry *dentry) -{ -return kmemdup_nul (dentry->d_name.name, dentry->d_name.len,GFP_KERNEL ); -} -static struct dentry *tracefs_syscall_mkdir (struct mnt_idmap *idmap,struct inode *inode,struct dentry *dentry,umode_t mode)
I can't even apply your patch as it appears you based it off of your local branch that applied your previous version of the patch.
Please rebase it off of 7.0-rc2 and resend.
Thanks!
-- Steve
PATCH v3 1/1 tracefs: Use dentry name snapshots instead of heap allocation
In fs/tracefs/inode.c, tracefs_syscall_mkdir() and tracefs_syscall_rmdir() previously used a local helper, get_dname(), which allocated a temporary buffer on the heap via kmalloc() to hold the dentry name. This introduced unnecessary overhead, an ENOMEM failure path, and required manual memory cleanup via kfree().
As suggested by Al Viro, replace this heap allocation with the VFS dentry name snapshot API. By stack-allocating a struct name_snapshot and using take_dentry_name_snapshot() and release_dentry_name_snapshot(), we safely capture the dentry name locklessly, eliminate the heap allocation entirely, and remove the now-obsolete error handling paths. The get_dname() helper is completely removed.
Testing:
Booted a custom kernel natively in virtme-ng (ARM64). Triggered tracefs inode and dentry allocation by creating and removing a custom directory under a temporary tracefs mount. Verified that the instance is created successfully and that no memory errors or warnings are emitted in dmesg.
---
Changes in v3:
- Rebased into a single clean commit against upstream.
Changes in v2:
- Link to v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/
- Use the helper functions take/release_dentry_name_snapshot() instead of allocating the name. (Al Viro)
fs/tracefs/inode.c | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-)
fs/tracefs/inode.c +8 -31
@@ -94,33 +94,14 @@ static struct tracefs_dir_ops {int (*rmdir)(const char *name); } tracefs_ops__ro_after_init ; -static char *get_dname (struct dentry *dentry) -{ -const char *dname; -char *name; -int len = dentry->d_name.len; - - dname = dentry->d_name.name; - name =kmalloc (len +1 ,GFP_KERNEL ); -if (!name) -return NULL ; -memcpy (name, dname, len); - name[len] =0 ; -return name; -} -static struct dentry *tracefs_syscall_mkdir (struct mnt_idmap *idmap,struct inode *inode,struct dentry *dentry,umode_t mode) {struct tracefs_inode *ti; -char *name; +struct name_snapshot name;int ret; - name =get_dname (dentry); -if (!name) -return ERR_PTR (-ENOMEM ); -/* * This is a new directory that does not take the default of * the rootfs. It becomes the default permissions for all the
@@ -135,24 +116,20 @@ static struct dentry *tracefs_syscall_mkdir(struct mnt_idmap *idmap,* the files within the tracefs system. It is up to the individual * mkdir routine to handle races. */ +take_dentry_name_snapshot (&name, dentry);inode_unlock (inode); - ret = tracefs_ops.mkdir (name); + ret = tracefs_ops.mkdir (name.name.name);inode_lock (inode); - -kfree (name); +release_dentry_name_snapshot (&name);return ERR_PTR (ret); }static int tracefs_syscall_rmdir (struct inode *inode,struct dentry *dentry) { -char *name; +struct name_snapshot name;int ret; - name =get_dname (dentry); -if (!name) -return -ENOMEM ; -/* * The rmdir call can call the generic functions that create * the files within the tracefs system. It is up to the individual
@@ -160,15 +137,15 @@ static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)* This time we need to unlock not only the parent (inode) but * also the directory that is being deleted. */ +take_dentry_name_snapshot (&name, dentry);inode_unlock (inode);inode_unlock (d_inode (dentry)); - ret = tracefs_ops.rmdir (name); + ret = tracefs_ops.rmdir (name.name.name);inode_lock_nested (inode,I_MUTEX_PARENT );inode_lock (d_inode (dentry)); - -kfree (name); +release_dentry_name_snapshot (&name);return ret; }
Signature · 1 line
2.51.0
Steven Rostedt
maintainerRe: [PATCH v3] tracefs: Use dentry name snapshots instead of heap allocation
On Fri, 6 Mar 2026 15:04:58 -0500
AnishMulay <[email protected]> wrote:
Thanks for the update, but please do not have a new patch as a reply to the old one. It makes it harder to find. As I'll only look at top level emails for patches in my inbox which means all new versions should be their own thread.
Quoted 19 lines
In
fs/tracefs/inode.c,tracefs_syscall_mkdir()andtracefs_syscall_rmdir()previously used a local helper,get_dname(), which allocated a temporary buffer on the heap viakmalloc()to hold the dentry name. This introduced unnecessary overhead, an ENOMEM failure path, and required manual memory cleanup viakfree().As suggested by Al Viro, replace this heap allocation with the VFS dentry name snapshot API. By stack-allocating a
struct name_snapshotand usingtake_dentry_name_snapshot()andrelease_dentry_name_snapshot(), we safely capture the dentry name locklessly, eliminate the heap allocation entirely, and remove the now-obsolete error handling paths. Theget_dname()helper is completely removed.Testing:
Booted a custom kernel natively in virtme-ng (ARM64). Triggered tracefs inode and dentry allocation by creating and removing a custom directory under a temporary tracefs mount. Verified that the instance is created successfully and that no memory errors or warnings are emitted in dmesg.
Quoted 3 lines
Signed-off-by AnishMulay <[email protected]>---
Changes in v3:
The above should have been:
Changes since v2: https://lore.kernel.org/all/[email protected]/
Quoted 7 lines
- Rebased into a single clean commit against upstream.
Changes in v2:
- Link to v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/
- Use the helper functions take/release_dentry_name_snapshot() instead of allocating the name. (Al Viro)
The above isn't needed as the above link will give it to you.
No need to resend (unless I find something wrong with the code of the patch).
-- Steve