From nobody Tue Dec 2 02:19:53 2025 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (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 6E0C7329E7A for ; Wed, 19 Nov 2025 14:30:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763562605; cv=none; b=SxqShkHmClaYIIo2F5OW/CBWMYW7vCtx63BrlxXRC28sGFqmVavoHmW6wZKrcE40lmLbHdkZT9k+kR71jjPJoHqH+orxOjsiOeIZTlmns3QMWGwthOoylHBJLTruY6ULI9JoKz9PYJCetiUxwzaZPv7mIQtwYvPwc5MTbfb5RU4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763562605; c=relaxed/simple; bh=lcjr10cevC/wlOidXMnjjJxq6BqTEweLwuEWS26Ef4s=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=iOqNo/R9tk9AzKD/T2Rcm6RWeQXd6G8HVNgGcdWN9i1qKMELW7DXxqMXGbIZzMUjTF1laDuYddUXAzbI15NQq2lrtb258A3biAA1rpRnSj4oBl4uB9RLMcdNgCaiFdVtGcZy+jHPxQ/tHM8drvWb8Y/PTpvyi9VvzVjB9ugTuWo= 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=Exg3Aib4; arc=none smtp.client-ip=209.85.128.52 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="Exg3Aib4" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-4779cb0a33fso48792955e9.0 for ; Wed, 19 Nov 2025 06:30:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763562600; x=1764167400; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=q4YCOv2WplfV/3rMOIyhYeErJ5mK15tvKllwtsGjP3s=; b=Exg3Aib4ZUvF1V4M7Qj3z8cygA//xVVOAoC7Uxffu4glFALdzIAy9wD/DSgxLQbtyh /Vo8gMoXq1Lf916Wmpj4MG8T7WBLvc7I3KyqujdHzIGgcxynj9LnLT0R1QymGVlgB6nu N1iKARa5z0Q+tZgOPZWnVrd/9fB+csyDdrsPqXslowbWAZKAP3uAXxWpG88gpStjartp tW4nna6qrVjydupK+bGUL8xmiJ54NoS5/+gfqjuAiR2OhKfZig2XLIFWPvw2+Cyclk7x gU+Us6xIOaqKsf+kEvlznxDKw58tRK8BZZunIPy/HilzskLDS//1G+kpcprPOlH5f2yM awoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763562600; x=1764167400; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=q4YCOv2WplfV/3rMOIyhYeErJ5mK15tvKllwtsGjP3s=; b=eNDpXu+6Thilts2343kJ2sX4s0YfJfeLlLox6ItkfJkltSvrlQ7sZ0L5Mz+CXsuxQL JZ7prRqWqFMCiQ2w3ATs3nuz/reKmIAKMR866z/vpKMWPmt5qxjXtJ3BLghg0ze9uEaf fkpi3FFK6zUTir1gYCgXtS5jQTxf/HlslevF9Tx8E45heLl5OTAPmzxnxTYPetz/mAOL BWxzJ+qEGeQ4amZ5VXyxX5BOpLKjxLcPuHKbxvgYt/UbK9Zo4vNms2RuxCnW+PvOtfJa 8EGVjow6wNcr6QJ4+0unycag84N31+rQSdNrLerQJC2piC2eLtj8FAzAhaCmWrt+uHMN Uh+g== X-Forwarded-Encrypted: i=1; AJvYcCVNfoeG1GX8j0QI0RxRlBnLEB6bCSgNA/vJ/EafdtUicOIlEOvFUjwfBIIgctpl9vJ1o1vdVAqSINVv1KU=@vger.kernel.org X-Gm-Message-State: AOJu0Yzw/WRYtR5SDWofkWtH8geLlWWknvvk/S3g4mQxndkh+ORM9WfC hrfw+W/IT6irC7cq2yyQpcwIZe19ERTcKCxj5GhuYZffnHvx5ZABkcLs X-Gm-Gg: ASbGncsCuSoLpRsJw+SLeEnW0kfcDN0aWX6Es/PaUk3359nB71pbX0ev0PGawfiwnNX Q1wNO2/ZqpEbaUj0Ejzj3Kw69DUfZ1Sq2J7Qteh+KcbxjuW7qz/EVZvgTK1km5Xd8TOLrCfxqav kT38FJmSZ0JZvgi7Kf1mKBKByBinu0TEvkDIQHS6InS+HUKqmK4qATOhc8+mGaKfXJH+mqtTCEd 1IWs9V5y8iR4Gap0LvD3hwLfTfm2nzjdulSxOW8qZ11H4AO8c/tncwXjW2gUbu59Oz3d6K6cBAx Tc+I3+bo9mmKkVzIF0eIj2oVRLRi+iVRAa3je6oQsiURd5UW8nnLM+LfpwXbvIs5QfetAXQCk4n RUN9zau+k7g4jx1Vp6+nRdxLv9UWADOGjleDUh8sElRttCG0fujMJknGsvTzer7eKt0cURSUiMp xy+kp8eMDL9ZZRgzkKFyk0ZCtqOs7sGrJvGs4+rhhZ6TBot40Z9zyIna+MXOiFW8qMGelMrA== X-Google-Smtp-Source: AGHT+IG9f9oJHqipvunqw5h0QfH78OEGhSCMNSVGzC7u18CGVC61h+PcHcWih8kPaYXJtyzXwJwV1Q== X-Received: by 2002:a05:600c:4fcf:b0:471:1717:411 with SMTP id 5b1f17b1804b1-4778fe9b299mr208879825e9.24.1763562599680; Wed, 19 Nov 2025 06:29:59 -0800 (PST) Received: from f.. (cst-prg-14-82.cust.vodafone.cz. [46.135.14.82]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-42b53e91f2dsm39449846f8f.19.2025.11.19.06.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Nov 2025 06:29:58 -0800 (PST) From: Mateusz Guzik To: brauner@kernel.org, viro@zeniv.linux.org.uk Cc: jack@suse.cz, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Mateusz Guzik Subject: [PATCH v5] fs: add predicts based on nd->depth Date: Wed, 19 Nov 2025 15:29:54 +0100 Message-ID: <20251119142954.2909394-1-mjguzik@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Stats from nd->depth usage during the venerable kernel build collected like= so: bpftrace -e 'kprobe:terminate_walk,kprobe:walk_component,kprobe:legitimize_= links { @[probe] =3D lhist(((struct nameidata *)arg0)->depth, 0, 8, 1); }' @[kprobe:legitimize_links]: [0, 1) 6554906 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@= @@@| [1, 2) 3534 | = | @[kprobe:terminate_walk]: [0, 1) 12153664 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@= @@@| @[kprobe:walk_component]: [0, 1) 53075749 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@= @@@| [1, 2) 971421 | = | [2, 3) 84946 | = | Additionally a custom probe was added for depth within link_path_walk(): bpftrace -e 'kprobe:link_path_walk_probe { @[probe] =3D lhist(arg0, 0, 8, 1= ); }' @[kprobe:link_path_walk_probe]: [0, 1) 7528231 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@= @@@| [1, 2) 407905 |@@ = | Given these results: 1. terminate_walk() is called towards the end of the lookup and in this test it never had any links to clean up. 2. legitimize_links() is also called towards the end of lookup and most of the time there s 0 depth. Patch consumers to avoid calling into it in that case. 3. walk_component() is typically called with WALK_MORE and zero depth, checked in that order. Check depth first and predict it is 0. 4. link_path_walk() also does not deal with a symlink most of the time when !*name Signed-off-by: Mateusz Guzik --- v5: - tweak the commit message + add link_path_walk probe stats, no code changes fs/namei.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 2a112b2c0951..11295fcf877c 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -785,7 +785,8 @@ static void leave_rcu(struct nameidata *nd) =20 static void terminate_walk(struct nameidata *nd) { - drop_links(nd); + if (unlikely(nd->depth)) + drop_links(nd); if (!(nd->flags & LOOKUP_RCU)) { int i; path_put(&nd->path); @@ -882,7 +883,7 @@ static bool try_to_unlazy(struct nameidata *nd) =20 BUG_ON(!(nd->flags & LOOKUP_RCU)); =20 - if (unlikely(!legitimize_links(nd))) + if (unlikely(nd->depth && !legitimize_links(nd))) goto out1; if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) goto out; @@ -917,7 +918,7 @@ static bool try_to_unlazy_next(struct nameidata *nd, st= ruct dentry *dentry) int res; BUG_ON(!(nd->flags & LOOKUP_RCU)); =20 - if (unlikely(!legitimize_links(nd))) + if (unlikely(nd->depth && !legitimize_links(nd))) goto out2; res =3D __legitimize_mnt(nd->path.mnt, nd->m_seq); if (unlikely(res)) { @@ -2179,7 +2180,7 @@ static const char *walk_component(struct nameidata *n= d, int flags) * parent relationships. */ if (unlikely(nd->last_type !=3D LAST_NORM)) { - if (!(flags & WALK_MORE) && nd->depth) + if (unlikely(nd->depth) && !(flags & WALK_MORE)) put_link(nd); return handle_dots(nd, nd->last_type); } @@ -2191,7 +2192,7 @@ static const char *walk_component(struct nameidata *n= d, int flags) if (IS_ERR(dentry)) return ERR_CAST(dentry); } - if (!(flags & WALK_MORE) && nd->depth) + if (unlikely(nd->depth) && !(flags & WALK_MORE)) put_link(nd); return step_into(nd, flags, dentry); } @@ -2544,7 +2545,7 @@ static int link_path_walk(const char *name, struct na= meidata *nd) if (unlikely(!*name)) { OK: /* pathname or trailing symlink, done */ - if (!depth) { + if (likely(!depth)) { nd->dir_vfsuid =3D i_uid_into_vfsuid(idmap, nd->inode); nd->dir_mode =3D nd->inode->i_mode; nd->flags &=3D ~LOOKUP_PARENT; --=20 2.48.1