From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F28F2F999F for ; Thu, 9 Apr 2026 18:34:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775759642; cv=none; b=ancAKz07h4tKusA4Ed8Nr2FN7sJOJezkZYWoPD3nqR8/46havu+wGv9Dx+4FD1/5HhSryzxhpiJqyqKfINYG6GiH0YXzJn6mAiTM5eOL25m9w3CoCrCvXgfQisccegcB1+m+C9MvHxjF8c4LfBiYY7BhaYqJ+DRMrhe/HVGyOxs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775759642; c=relaxed/simple; bh=zSj2vgNH5m1UoAUeVeVywV7f/QsqgNtXrWvn/JRmzdA=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Yf8ySKsdeiZgn2kSgOrcGEVF7VKXNI2FxNHyipFPQ8jJUKvKI/du6Xf9cagEtwi/nHWWwRlLzlZ3bTZVqHu8RTXrf9rATV4KuuFpFKf+0YKzOBms9K3gShukFs/5PGWi1Id3zQeEwZgMLEjU+s7n9Psx9MGxm76cWWqjZXplVW8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LjVn8PzQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LjVn8PzQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ED193C2BCAF; Thu, 9 Apr 2026 18:34:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775759642; bh=zSj2vgNH5m1UoAUeVeVywV7f/QsqgNtXrWvn/JRmzdA=; h=Date:From:To:Cc:Subject:References:From; b=LjVn8PzQjQkJdvRX6FEqbnDH+A8dF3FGPw9jSx3vzgUDoor+JrxgeeFXEbR+ZuWBl hT6iqRA+pqRi6u87CqcpVewrAocErbJf8z+soqVL9S5lhfMEbpeziCYYlBwdmRJ/NB cIwbqmSQDo+3LBj3F0UdxBTKJ46tZHvwDquy73QMzLFrq1l3nL71jWwAU4rO4h4VgN KLW3S9+fJ8ATqg8252WPsM89eu6jRmnKS/UmXqJ4x7UA+ZeaMreFdiS4em77PKFaZG XMOQJVUOJVCKyY5blTELYYhjkaPm/ZqlivBC2As4m3hH1mVDQ3Npu2kNJ0grG5xYbr smfsHOIBHSFHw== Received: from rostedt by gandalf with local (Exim 4.99.1) (envelope-from ) id 1wAuDp-00000003pGk-1R7J; Thu, 09 Apr 2026 14:35:21 -0400 Message-ID: <20260409183521.199439366@kernel.org> User-Agent: quilt/0.69 Date: Thu, 09 Apr 2026 14:34:59 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , Al Viro , AnishMulay Subject: [for-next][PATCH 2/3] tracefs: Use dentry name snapshots instead of heap allocation References: <20260409183457.935983082@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 From: 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. Link: https://patch.msgid.link/20260306200458.2264-1-anishm7030@gmail.com Suggested-by: Al Viro Signed-off-by: AnishMulay Signed-off-by: Steven Rostedt (Google) --- 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 51c00c8fa175..fa4c7e6aa5ff 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