From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796010503; b=byWsXhqbfJCKHfrMmIIB+W22VWTJhAmzT7h9hbFNA8C0x1/RCTDPqghbPCkmwfOJIVis P42Fj08B1dSRNJTpn0FYVFYQ44DfsoWLWNSGe3RXUxGBBb8qLpsXT54vSOFhhptKL8qSN PkuZg7haP0IvmeiMcuC39R62Bb8g0hZE7MDSc/vO7uGtoy/TruLJP1gyxrIPBSG87Am4N fIX0ahHXa4yhM9tDw+UUxCM/Q+aPFQ9tHSvaBoF5pw29KbF24mpkGzPNVKOZ9iTDJ4a0b Eqic40irNBINUMtcrKqiDeZAXbkSOdxnCHyynYeyAuYWYGD77AsqCkvWXTJsEMVHl7Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796010503; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:Message-ID:In-Reply-To:References:From:Subject:To:Cc: MIME-Version:Content-Type:Content-Disposition:User-Agent; bh=rGWYmtEOJLBOKBaMakl1GXbCXowIH47ZSRKQ9Z/J7RE=; b=x6WpK/K7nzku7SN9T1Mz6cgyjN9jw429IPfhe0ULiJjRUjOXvhqCUT5rpFVdUWR4BS10 05iZCHytLpd7PMhyOObFoqnWclpl22WfvfMy9UCzE66SQrxdNHhziCU9X9mEi/3uRMJgu /V9DpaMUQ/z8LdOnhsPVNWOFWyKvmduGL8NzqMPnUld8oLtoKhcDzUpcYCoue2/5EPd4e +EZb/qysuhdoInK9mh389TAK07JQIZ34stAVHvMGs9bm5iIxXTnw98jOWp8tZ+ZCLn7RI 6hww9nDo7uS5nvIN4jGKDH0oa3wplnJkSiVMUYJJPaiMPuM29hYyjh7Q3tx0KGsdswA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=chrisdown.name; spf=pass smtp.mailfrom=chrisdown.name; dkim=pass header.d=chrisdown.name header.i=@chrisdown.name header.a=rsa-sha256 header.s=google header.b=kR9LcDDn; arc=none smtp.remote-ip=74.125.224.41 Received: from mail-yx1-f41.google.com (mail-yx1-f41.google.com [74.125.224.41]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 860F9606A680 for ; Mon, 30 Nov 2026 03:48:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 860F9606A680 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chrisdown.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chrisdown.name header.i=@chrisdown.name header.a=rsa-sha256 header.s=google header.b=kR9LcDDn Received: by mail-yx1-f41.google.com with SMTP id 956f58d0204a3-675617e81f9so11424095d50.3 for ; Sun, 29 Nov 2026 19:48:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chrisdown.name; s=google; t=1796010501; x=1796615301; darn=vger.kernel.org; h=user-agent:content-disposition:content-type:mime-version:cc:to :subject:from:references:in-reply-to:message-id:date:from:to:cc :subject:date:message-id:reply-to:content-type; bh=rGWYmtEOJLBOKBaMakl1GXbCXowIH47ZSRKQ9Z/J7RE=; b=kR9LcDDnWvmv20JHMfsw+UQMXvwfFrk905wZpsPpJmA5CeuOWdmiL97b9z2Q/lvCtC 6AlvKNZoOG/duHKK3+57VT4Mk3L2G0waFF/lTOrv6EnhDi5RrF14jy60brIiF1EnF0kZ +21qPQZ7uz0FERUTfPjednsIn+ir2t6hkqeFg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796010501; x=1796615301; h=user-agent:content-disposition:content-type:mime-version:cc:to :subject:from:references:in-reply-to:message-id:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=rGWYmtEOJLBOKBaMakl1GXbCXowIH47ZSRKQ9Z/J7RE=; b=hEFgyaUzIuFspsELmUJ0oVZZNjFU7HdQRfocvg7kX86CdGeouoFwm3YeVvlvQUBYh7 +/Ih+W8Ke4DdHgUZmy8O4oe3YS+utcSf7MNPFZLO6PP+CIoCjAA4F9bFXblV0TIvCsIO TUThjgiSJCWc9GYmjSnA5b5tDRDXNzrg2waW4zkfn2ZP4+CzcHK1tDQnIkNOPzFmih7P WL+Z7PXGuapZ5rlZM9ZNOVRbKA8SdLNqnxKHVCgpPfYJWmayNMysWi0jmDCwoHFZ9xb0 7DzXtpNsTAi4rP5RGwb6WR6Beusdg0TChqCKDiXWLySEQ7rxurxg4tHf6O+LhTeRQyrj YJHA== X-Forwarded-Encrypted: i=1; AL+hErGWGFZl0vaFqzN4WnieFUzitZvS45ueUgAdxJ4kLLfiZoPbeWycf8q2smUX/rv7qOebvpWoo191ljGgkM0=@vger.kernel.org X-Gm-Message-State: APuU4Z/GZWnA0ghqKVANSUCObCQZ5oA52wCc+ThCw7AlUGdU3kYXvYoS LIGSt1Y4W8W8mGMV0SBVUW+Ygyo4k9SujaRc5VVyFK3ELbOi/1fHvOxU+7HgJZK6j7k= X-Gm-Gg: AZFdTZdzaDTHhdnwF2E02v0hvjJIAdMgl+x/NWhLjSnAbI+/uwcuYLc3wQ/vMjSBqLR hB9BrZ13xWwmQPBxMPTBFmVCbuKCSeujqBJq5GUdQqX7k5FU9M9GgPXSid4Hz3tbME1j84zn7WX DqTB2wa840FyyVC8nOeTbnCJ9xcKTA6OpWF6spdjIIcJkohaGNsvvk2pjFuoI32rym7XIgJgAjq PoiTjSuu2EqNQF+zlwkvex3wxAoivT0uO1dbozkCijOgQRCbCO+nNzJHkQT5kz4QEmIPovR/QLd GEi6Z5CdMH4WVeGP0gSC6qpgvZ3TqlLyEC4LLPOMMC8bERZOrOZwF83pvAFguha4tYMSH5YI2s0 pGWvoseh76c7z7KEgEcXD/Hm1MBI6mSftn3wPYcbKx67lfTIJFGYvL/ZPue+rwoyONYtpdON7Jb UrrTlbyeukH+sGsMzqUhcn4jlmsWBfqzer0WB1VVc33/GHsYvMqHNTW1R/eL94a6myl5YTTuSYN /7iNdAw/c0XXzHilGcy32q/5V/69GF0/PJn0r1BIHLEToB8zl26Y73Gp3dC3MSnbNyyMlD5ErfD QDFeKlI7iV+U1SHQzXlG4iSzJarRV5MTI0c3JXTE7nB4Ye+TSL9EUdbkPJsMlHQDJ3gkKDE2tCd gRIZ+0jbaKfLhI/W1veiRE17G5bw3h3wlBJBTE0g9K8Jxw+KUiIkL/m2OM+LjXgy2beF0AZUtdJ zQ2GgMjsre9qnqF+fMC5NqnE1hRUZ9bPEhZTNyoO24m4yCclUfZkyUAuoeyZ6bwjf5Mdm5IURRq O1qTezaDail50oVic1v1rc= X-Received: by 2002:a05:690e:479f:20b0:67c:1383:1d68 with SMTP id 956f58d0204a3-67df4441ebfmr8953142d50.76.1796010501540; Sun, 29 Nov 2026 19:48:21 -0800 (PST) Received: from [172.20.0.3] (c-73-207-185-9.hsd1.ga.comcast.net. [73.207.185.9]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67df33d64a0sm15218348d50.22.2026.11.29.19.48.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 19:48:20 -0800 (PST) Date: Sun, 29 Nov 2026 19:48:18 -0800 Message-ID: In-Reply-To: References: From: Chris Down Subject: [PATCH v3 1/3] kernfs: Build paths in linear time with respect to depth To: Greg Kroah-Hartman , Tejun Heo Cc: Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= , Serge Hallyn , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , Yonghong Song , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , driver-core@lists.linux.dev, cgroups@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, sashiko-bot@kernel.org X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/2.4.3 (cc9e3943) (2026-10-09) kernfs nodes only point to their parent. To build a path top-down, kernfs_path_from_node_locked() walks back up from the leaf for each name, so the work grows with the square of the depth. Reading /proc//cgroup does this with IRQs off and css_set_lock held, so a read from a deep delegated subtree can hold up fork and exit elsewhere on the machine, which is certainly suboptimal. Nobody needs a hierarchy that deep, and it's up to whoever delegates a subtree to set cgroup.max.depth on it. But cgroup.max.depth defaults to max, so where that hasn't been done, anyone with a delegated subtree can make one. As an example, take a 4 CPU VM where one task reads /proc/self/cgroup in a loop from the bottom of a chain of cgroups. The median time for a fork() and exit on another CPU is: depth of reader before after (no reader) 51us 52us 64 56us 53us 256 363us 60us 1024 5.0ms 93us 2046 21.9ms 117us The names are a single character, so at 2046 the path is just short of PATH_MAX. Let's fill the buffer from the end as we walk up instead, and move the path to the front once it's complete. This also changes what happens when the path doesn't fit: we now return -E2BIG and leave "..." followed by the end of the path in the buffer. Reads of /proc//cgroup then fail with ENAMETOOLONG instead of getting the first 4095 bytes, and reads of /proc//mountinfo fail with ERANGE once they reach a cgroup mount whose root is such a path. That's how both behaved until commit ff6d413b0b59 ("kernfs: Convert kernfs_path_from_node_locked() from strlcpy() to strscpy()"), which changed these callers to check for -E2BIG but copied the names with scnprintf(), so it was never returned. Callers which ignore the return value, like the tracepoints, print the end of the path, which is the more useful part. pr_cont_kernfs_path() now does the same, rather than print "(name too long)". We also now return -E2BIG for a zero length buffer instead of writing to it, although no caller passes one. In the same VM, the median time for a read of /proc/self/cgroup from the bottom of the chain is: depth before after 4 304ns 258ns 16 549ns 396ns 64 2.8us 1.0us 256 69us 4.2us 1024 2.5ms 20us 2046 12.0ms 43us 15000 3.3s 643us At 15000 the path doesn't fit. Before this change it is truncated, and after it the read fails. Signed-off-by: Chris Down --- fs/kernfs/dir.c | 83 +++++++++++++++++++++++++++--------------- include/linux/kernfs.h | 7 ++-- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 800af10d9511..c305e7b00039 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -94,6 +94,21 @@ static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a, return a; } +/* + * Put @str in front of the path which starts at @buf + *@pos. If it doesn't + * all fit, keep the end of it and return false. + */ +static bool kernfs_path_prepend(char *buf, size_t *pos, const char *str, + size_t len) +{ + size_t copy = min(len, *pos); + + *pos -= copy; + memcpy(buf + *pos, str + len - copy, copy); + + return copy == len; +} + /** * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to, * where kn_from is treated as root of the path. @@ -120,9 +135,10 @@ static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a, * * [3] when @kn_to is %NULL result will be "(null)" * - * Return: the length of the constructed path. If the path would have been - * greater than @buflen, @buf contains the truncated path with the trailing - * '\0'. On error, -errno is returned. + * Return: the length of the constructed path. If the path doesn't fit in + * @buf, -E2BIG is returned and @buf contains "..." followed by as much of + * the end of the path as fits, with the trailing '\0'. On other errors, + * -errno is returned. */ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to, struct kernfs_node *kn_from, @@ -130,9 +146,10 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to, { struct kernfs_node *kn, *common; const char parent_str[] = "/.."; - size_t depth_from, depth_to, len = 0; - ssize_t copied; - int i, j; + size_t depth_from, pos, len, i; + + if (!buflen) + return -E2BIG; if (!kn_to) return strscpy(buf, "(null)", buflen); @@ -147,30 +164,38 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to, if (WARN_ON(!common)) return -EINVAL; - depth_to = kernfs_depth(common, kn_to); depth_from = kernfs_depth(common, kn_from); - buf[0] = '\0'; - - for (i = 0; i < depth_from; i++) { - copied = strscpy(buf + len, parent_str, buflen - len); - if (copied < 0) - return copied; - len += copied; - } + /* + * We can only walk towards the root, so fill @buf from the end and + * move the path to the front once it's complete. If the path is too + * long, we stop, keep the end of it and put "..." at the start. + */ + pos = buflen - 1; + buf[pos] = '\0'; - /* Calculate how many bytes we need for the rest */ - for (i = depth_to - 1; i >= 0; i--) { - const char *name; + for (kn = kn_to; kn != common; kn = rcu_dereference(kn->__parent)) { + const char *name = rcu_dereference(kn->name); - for (kn = kn_to, j = 0; j < i; j++) - kn = rcu_dereference(kn->__parent); + if (!kernfs_path_prepend(buf, &pos, name, strlen(name)) || + !kernfs_path_prepend(buf, &pos, "/", 1)) + goto too_long; + } - name = rcu_dereference(kn->name); - len += scnprintf(buf + len, buflen - len, "/%s", name); + for (i = 0; i < depth_from; i++) { + if (!kernfs_path_prepend(buf, &pos, parent_str, + sizeof(parent_str) - 1)) + goto too_long; } + len = buflen - 1 - pos; + memmove(buf, buf + pos, len + 1); + return len; + +too_long: + memcpy(buf, "...", min(buflen - 1, 3)); + return -E2BIG; } /** @@ -217,9 +242,10 @@ int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen) * path (which includes '..'s) as needed to reach from @from to @to is * returned. * - * Return: the length of the constructed path. If the path would have been - * greater than @buflen, @buf contains the truncated path with the trailing - * '\0'. On error, -errno is returned. + * Return: the length of the constructed path. If the path doesn't fit in + * @buf, -E2BIG is returned and @buf contains "..." followed by as much of + * the end of the path as fits, with the trailing '\0'. On other errors, + * -errno is returned. */ int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from, char *buf, size_t buflen) @@ -271,11 +297,8 @@ void pr_cont_kernfs_path(struct kernfs_node *kn) sz = kernfs_path_from_node(kn, NULL, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf)); - if (sz < 0) { - if (sz == -E2BIG) - pr_cont("(name too long)"); - else - pr_cont("(error)"); + if (sz < 0 && sz != -E2BIG) { + pr_cont("(error)"); goto out; } diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 73786b567ccd..023474e5247a 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -608,9 +608,10 @@ static inline void kernfs_init(void) { } * * If @kn is NULL result will be "(null)". * - * Returns the length of the full path. If the full length is equal to or - * greater than @buflen, @buf contains the truncated path with the trailing - * '\0'. On error, -errno is returned. + * Return: the length of the constructed path. If the path doesn't fit in + * @buf, -E2BIG is returned and @buf contains "..." followed by as much of + * the end of the path as fits, with the trailing '\0'. On other errors, + * -errno is returned. */ static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen) { -- 2.56.1