From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51B1F35E936 for ; Fri, 6 Mar 2026 20:05:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772827505; cv=none; b=r6pJvXdJO71o/pJ+H2es2o/up+8+TgjnC7b2K+NP9NakRWnyGo1efiHCE5mXq01/p0mKwNZMdBOI4ymp5fJBmlR140zIJNYLhRDekjaGvWbV6lFsJqGoEXrvq/p02SD8Orn8W6rtPkuZ9A3oDC1hWDNZzJCtVAX0+xwvCMBwnqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772827505; c=relaxed/simple; bh=NvN/xy+/VeQeq+W1h8Raav62De9gFC7UBlGgdAxUzwc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LLobEAY6I3Gik6oSh4xcNBv/jI4iVi2N2r4/2VtvkccRCbWQTGuqJppmK2q7fXclQH85K5RWIFzTKjMULbbvDPTk/anDAgwTpnHIy90mH25wMv7/v2MpWHUWpV2n9vyOvkxNY5X6peVTxw3M1kzVteOYgpd60zQpcpXOb/ju400= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Uk8OxN7G; arc=none smtp.client-ip=209.85.160.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Uk8OxN7G" Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-50698970941so116295101cf.0 for ; Fri, 06 Mar 2026 12:05:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772827502; x=1773432302; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zLRL/21CZaQDWxEDsVS15El8cmGm+Da0IafrOAmYaGA=; b=Uk8OxN7GHiB5czu4U4Zb8zIVfGSF3KA7e0ei8RRn6Bf19Sst5HvmBfoQQ1/N8hi8EK uiZ8PfnC5Qy01wgntYveas3zR2Pb1SU2J9u30akc0eWWbWhL4cCNW6TjKRlv6E9KBhCM ZhGgCEBhGt09n0l2LcGzDGh5xLh64PByAm38Nwmv4KA4kMRPbnYEWLJ9Scz8AHQ+uQa7 oNgFSwfvIC6HA1mx2pQz0zsaC98JzWavYwoCJPePoTPdy1ydYFHQ5Gwa7pqlSjNfLZoG jVjoWRYg9nX5jlJ4TZD/6WZ0JvpKGUvU3wyMLX8UK25cdDnCuegnaIj243Y/6ARD0nOi g69A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772827502; x=1773432302; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=zLRL/21CZaQDWxEDsVS15El8cmGm+Da0IafrOAmYaGA=; b=ZCcypzRtQJGzD1Aj87R1mLL+3ErKviDOinFZIkGXGeEzVJnP9YMd5xIzG3w/VV+/f6 W18jaRXD5H157SUWsRin0HGH+uHDgzUwub6dkiMviFlzdvddcE+esddp5q6fpWlhvYfq PO05Sb76e5SKSAdJV1uiCRXWthfYpGMMomwR7yAJQEu7CFd6KD8Tl3PqekOnQf+Y8k21 bBMNM3hctTvCyrfXxIude3qmpRa16TGba9gJui4kw7wD32sIYnXwDbtycUrSI0MfvOJh sT3/Gy3yXTg0zL6wgh1FQvK4rjwIyKD/DkQs1aJ8vaIGK2WcH7vbl5tTCSr2aiCKSwIj rJ6Q== X-Forwarded-Encrypted: i=1; AJvYcCUq/17wJvCUuIpk0VMrrbFQ3+o01q8AWSLKHYtLQhEl3hcXXfAISglDI4hhc8q6cB9+eI41lK2xcMKGj7Y=@vger.kernel.org X-Gm-Message-State: AOJu0YzabsgKspxTXt7gmGuLxrgguUF3Y0cTyilDyP2AtIzqjIrQBPhG YcK4FOdDIWDvV7WY9yP709UsE+TvV301HtVEBOed+qT0zpXzmoFzryZG X-Gm-Gg: ATEYQzxyo8HiwqTUbPrZEGkwwyPQu/6oVgaE0WVxkdL6+gpUBkdEnWyDxGpw+mG2hNs PEfgmq9sL+gyvAhkDvrg1a2It83viufT6A9+NqTndmOtwp4aiHP6HZc/NbxCPLa/+coVonZ2Ewb yfD5DhNC9M5t4PqI+D5v3lum30kGZvShYe8/58A4PUX3dXvrk6+S0I0cKXcLKYaPHlJFVAgoYx1 twDJ/Mx6G5PAT6NV6wCAO9Y2BCEDwGMR9Hsi6GP+fteGQl9DTjlGv+XyMQWQ/Hj+A4PCX5gDNAU /dnKj67cPnItEWXpbif8ZcxkU9bUy49xDrFriD6oZoR/dEgmnDPOL+vzm194FlVz89AHcxnHRHD dDg3Y453yNqjblerMcO/Z53+M9er24hVEXl42wW74d7+3V0o2DkFPubuUBvJPpjkACiVkxDIxBt dnV2xQXaeBDDepO3Ulzu+HHmvE X-Received: by 2002:a05:622a:1791:b0:4e8:b446:c01b with SMTP id d75a77b69052e-508f499e32emr45992311cf.61.1772827502002; Fri, 06 Mar 2026 12:05:02 -0800 (PST) Received: from ubuntu ([76.38.17.53]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-89a316ea3fesm20945806d6.37.2026.03.06.12.05.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Mar 2026 12:05:01 -0800 (PST) From: AnishMulay To: rostedt@goodmis.org, viro@zeniv.linux.org.uk Cc: mhiramat@kernel.org, mathieu.desnoyers@efficios.com, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, AnishMulay Subject: [PATCH v3] tracefs: Use dentry name snapshots instead of heap allocation Date: Fri, 6 Mar 2026 15:04:58 -0500 Message-ID: <20260306200458.2264-1-anishm7030@gmail.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260305165231.67c2491d@gandalf.local.home> References: <20260305165231.67c2491d@gandalf.local.home> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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