* [PATCH] tracefs: Simplify get_dname() with kmemdup_nul() @ 2026-02-27 19:44 AnishMulay 2026-02-27 20:18 ` Al Viro 0 siblings, 1 reply; 8+ messages in thread From: AnishMulay @ 2026-02-27 19:44 UTC (permalink / raw) To: rostedt, mhiramat Cc: mathieu.desnoyers, linux-trace-kernel, linux-kernel, AnishMulay 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. Signed-off-by: AnishMulay <anishm7030@gmail.com> --- fs/tracefs/inode.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index d9d8932a7b9c9..86ba8dc25aaef 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -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, -- 2.51.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] tracefs: Simplify get_dname() with kmemdup_nul() 2026-02-27 19:44 [PATCH] tracefs: Simplify get_dname() with kmemdup_nul() AnishMulay @ 2026-02-27 20:18 ` Al Viro 2026-02-27 20:42 ` Steven Rostedt 0 siblings, 1 reply; 8+ messages in thread From: Al Viro @ 2026-02-27 20:18 UTC (permalink / raw) To: AnishMulay Cc: rostedt, mhiramat, mathieu.desnoyers, linux-trace-kernel, linux-kernel On Fri, Feb 27, 2026 at 02:44:53PM -0500, AnishMulay wrote: > index d9d8932a7b9c9..86ba8dc25aaef 100644 > --- a/fs/tracefs/inode.c > +++ b/fs/tracefs/inode.c > @@ -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); > } 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()... ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] tracefs: Simplify get_dname() with kmemdup_nul() 2026-02-27 20:18 ` Al Viro @ 2026-02-27 20:42 ` Steven Rostedt 2026-02-27 21:15 ` [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation AnishMulay 0 siblings, 1 reply; 8+ messages in thread From: Steven Rostedt @ 2026-02-27 20:42 UTC (permalink / raw) To: Al Viro Cc: AnishMulay, mhiramat, mathieu.desnoyers, linux-trace-kernel, linux-kernel On Fri, 27 Feb 2026 20:18:24 +0000 Al Viro <viro@zeniv.linux.org.uk> wrote: > On Fri, Feb 27, 2026 at 02:44:53PM -0500, AnishMulay wrote: > > index d9d8932a7b9c9..86ba8dc25aaef 100644 > > --- a/fs/tracefs/inode.c > > +++ b/fs/tracefs/inode.c > > @@ -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); > > } > > 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()... That sounds good to me. -- Steve ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation 2026-02-27 20:42 ` Steven Rostedt @ 2026-02-27 21:15 ` AnishMulay 2026-03-05 18:12 ` Steven Rostedt 2026-03-05 21:52 ` Steven Rostedt 0 siblings, 2 replies; 8+ messages in thread From: AnishMulay @ 2026-02-27 21:15 UTC (permalink / raw) To: rostedt, viro Cc: mhiramat, mathieu.desnoyers, linux-trace-kernel, linux-kernel, AnishMulay 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. Signed-off-by: AnishMulay <anishm7030@gmail.com> --- fs/tracefs/inode.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index 86ba8dc25aaef..ad322e8f9e2ad 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -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; } -- 2.51.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation 2026-02-27 21:15 ` [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation AnishMulay @ 2026-03-05 18:12 ` Steven Rostedt 2026-03-05 21:52 ` Steven Rostedt 1 sibling, 0 replies; 8+ messages in thread From: Steven Rostedt @ 2026-03-05 18:12 UTC (permalink / raw) To: AnishMulay Cc: viro, mhiramat, mathieu.desnoyers, linux-trace-kernel, linux-kernel On Fri, 27 Feb 2026 16:15:05 -0500 AnishMulay <anishm7030@gmail.com> wrote: > 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. > > Signed-off-by: AnishMulay <anishm7030@gmail.com> > --- 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/20260227194453.213095-1-anishm7030@gmail.com/ - 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation 2026-02-27 21:15 ` [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation AnishMulay 2026-03-05 18:12 ` Steven Rostedt @ 2026-03-05 21:52 ` Steven Rostedt 2026-03-06 20:04 ` [PATCH v3] " AnishMulay 1 sibling, 1 reply; 8+ messages in thread From: Steven Rostedt @ 2026-03-05 21:52 UTC (permalink / raw) To: AnishMulay Cc: viro, mhiramat, mathieu.desnoyers, linux-trace-kernel, linux-kernel On Fri, 27 Feb 2026 16:15:05 -0500 AnishMulay <anishm7030@gmail.com> wrote: > diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c > index 86ba8dc25aaef..ad322e8f9e2ad 100644 > --- a/fs/tracefs/inode.c > +++ b/fs/tracefs/inode.c > @@ -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) 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3] tracefs: Use dentry name snapshots instead of heap allocation 2026-03-05 21:52 ` Steven Rostedt @ 2026-03-06 20:04 ` AnishMulay 2026-03-06 21:41 ` Steven Rostedt 0 siblings, 1 reply; 8+ messages in thread From: AnishMulay @ 2026-03-06 20:04 UTC (permalink / raw) To: rostedt, viro Cc: mhiramat, mathieu.desnoyers, linux-trace-kernel, linux-kernel, AnishMulay 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. Signed-off-by: AnishMulay <anishm7030@gmail.com> --- Changes in v3: - Rebased into a single clean commit against upstream. Changes in v2: - Link to v1: https://lore.kernel.org/linux-trace-kernel/20260227194453.213095-1-anishm7030@gmail.com/ - 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(-) diff --git a/fs/tracefs/inode.c b/fs/tracefs/inode.c index d9d8932a7b9c9..ad322e8f9e2ad 100644 --- a/fs/tracefs/inode.c +++ b/fs/tracefs/inode.c @@ -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; } -- 2.51.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3] tracefs: Use dentry name snapshots instead of heap allocation 2026-03-06 20:04 ` [PATCH v3] " AnishMulay @ 2026-03-06 21:41 ` Steven Rostedt 0 siblings, 0 replies; 8+ messages in thread From: Steven Rostedt @ 2026-03-06 21:41 UTC (permalink / raw) To: AnishMulay Cc: viro, mhiramat, mathieu.desnoyers, linux-trace-kernel, linux-kernel On Fri, 6 Mar 2026 15:04:58 -0500 AnishMulay <anishm7030@gmail.com> 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. > 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. > > Signed-off-by: AnishMulay <anishm7030@gmail.com> > --- > Changes in v3: The above should have been: Changes since v2: https://lore.kernel.org/all/20260227211505.226643-1-anishm7030@gmail.com/ > - Rebased into a single clean commit against upstream. > > Changes in v2: > - Link to v1: https://lore.kernel.org/linux-trace-kernel/20260227194453.213095-1-anishm7030@gmail.com/ > - 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-03-06 21:41 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-02-27 19:44 [PATCH] tracefs: Simplify get_dname() with kmemdup_nul() AnishMulay 2026-02-27 20:18 ` Al Viro 2026-02-27 20:42 ` Steven Rostedt 2026-02-27 21:15 ` [PATCH v2] tracefs: Use dentry name snapshots instead of heap allocation AnishMulay 2026-03-05 18:12 ` Steven Rostedt 2026-03-05 21:52 ` Steven Rostedt 2026-03-06 20:04 ` [PATCH v3] " AnishMulay 2026-03-06 21:41 ` Steven Rostedt
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox