From patchwork Wed Sep 18 14:10:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao Wang via B4 Relay X-Patchwork-Id: 13806928 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 C2A4518B480; Wed, 18 Sep 2024 14:10:51 +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=1726668651; cv=none; b=dPio8DTvi+zpsDFndn1TGMfHksq2nRHska4K4RvVP4RFy35yf2+Z7EtVaeDE/esMdceQ+NX3bPss33odZHVbvRTgO3BLszH+LN7Jjs6Uf6szaWeVnnmZCmArDP5Ez9c8nCukb1EF4PDiESXv8gK54v/Ae6a2KKW6jNpkb9fmlfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668651; c=relaxed/simple; bh=viNC/wQCouVDr26WK+RdCH9qzhVRbfqkoP/FkrpBbLE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Yaf30L/llWwiftDz+yYcH8NOna8jVsKVIQSXkAgEH5uMdAccL6MQgHPuDCJqPGZmft6kr1a2h9tVkNrn3SIfcQPker9Lt8VJqSAhCN3uSDHv2SSRGN14LREBqQDjdnAhO25LME/9NJL4FUIDuq2iICdvUGZGmO9a/dOAO5oyZGI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sr0HIoFs; 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="sr0HIoFs" Received: by smtp.kernel.org (Postfix) with ESMTPS id 68E3FC4CECE; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726668651; bh=viNC/wQCouVDr26WK+RdCH9qzhVRbfqkoP/FkrpBbLE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=sr0HIoFsyptK3l4S0MX+9D61RyI0M5DDmCDZqwKIFhhcdV7ME4kUVMkH92S68lgeD Du67BqrRsPGnSSFikiYOhGhONr9yZYvT+rdR9CpZyN16QgtBNg6+kRd999M3icW8dG rrz6tNaXBtc6D4qFxnabPfbMIfPET/sYzd1vwM4x7K8/wJbJf6MyEBJQS03GZgwV75 sCfZxeZiUIyu+yQMWMcg/9pPmwKleGY1f0bGsh5oml1KuLACFR1U6l9HaP5kj3Gu/i VK0G2R9+xPIqT2CzHFqqUQUsjZElBx74mdaG3DgrpU+o2A2UFVgAikkUKnhvlvNcT/ IpmbWXknl+4Kw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56100CCD1AF; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) From: Miao Wang via B4 Relay Date: Wed, 18 Sep 2024 22:10:37 +0800 Subject: [PATCH v5.15-v5.10 1/4] vfs: mostly undo glibc turning 'fstat()' into 'fstatat(AT_EMPTY_PATH)' Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240918-statx-stable-linux-5-15-y-v1-1-5afb4401ddbe@gmail.com> References: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> In-Reply-To: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> To: Greg Kroah-Hartman , stable@vger.kernel.org Cc: Xi Ruoyao , Mateusz Guzik , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, Miao Wang , Linus Torvalds X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=2834; i=shankerwangmiao@gmail.com; h=from:subject:message-id; bh=shkXBk+gnzTIPbC7R2tboulSE67SGBTGhMLzTghvzw8=; b=owEBbQKS/ZANAwAKAbAx48p7/tluAcsmYgBm6t9oZdUD+fMtZyQknzM2S2xZgTNBVpkCHtZ+Q fSp0U4g5NOJAjMEAAEKAB0WIQREqPWPgPJBxluezBOwMePKe/7ZbgUCZurfaAAKCRCwMePKe/7Z bhyZD/9ArmkkoE+BhMIr5PdZk1Or/Jt3JiCI4dy/eyIMnFtlx1f2V7HTvzahJ8iQ/Lg/Twc8Mdp 80zqxSJgmUgfStce539KCLS7CTNKfdSfZ/eJCxwbA1l+Pe04MXt/LdhDxciqRp+yksv/PSMsgoP lf2VF2XOHjSinhIdyx5vd7FyLsHYSufgxV2S5+g3WLScx1mGWiqzR+gfzFAA+dDrkw/DtEYYzGQ PwkyStOG+S3GLbHxOxiumeAYyM05jwLnA7EFTInEPlfUTrjjL3aA2ehbBE/yeqtRaiuh8X86vCT zbOSJ745nS4LAiDF68uMu2VU1K/gn0scxmV7ON7qnzAPmomhsEaPictBlCyHpfjFILbITHlZTvb Rxvgqys781+l2J8T+7e6P1CineXHfYQAkIJq0atq+MVlk7qvA5edeyoucyc0y4cRIQBBVrLOtH6 sJZq0A+k4jwHRy0+H889eDbDiUT5bm35W2jdoJ2PVF/z6gPEVngnUfx7avzPXJEXR0aZL8KQ5Qd PSx0PpinuE99O4QAkeb10TMXNFz5mkexEiRgiR0r5eZ5+ChlBB5fdZZ9muxq4iT2vojUXr2IGn+ QJeNclB7Jcxh5BBxyXcRHG/0mRHl93Rdt68p1yZmSi9x3LskINSOSYSGEPhAEU2rPKH5UEbmBv/ GDlMb2VRDdh01ag== X-Developer-Key: i=shankerwangmiao@gmail.com; a=openpgp; fpr=6FAEFF06B7D212A774C60BFDFA0D166D6632EF4A X-Endpoint-Received: by B4 Relay for shankerwangmiao@gmail.com/default with auth_id=189 X-Original-From: Miao Wang Reply-To: shankerwangmiao@gmail.com From: Linus Torvalds commit 9013c51 upstream. Mateusz reports that glibc turns 'fstat()' calls into 'fstatat()', and that seems to have been going on for quite a long time due to glibc having tried to simplify its stat logic into just one point. This turns out to cause completely unnecessary overhead, where we then go off and allocate the kernel side pathname, and actually look up the empty path. Sure, our path lookup is quite optimized, but it still causes a fair bit of allocation overhead and a couple of completely unnecessary rounds of lockref accesses etc. This is all hopefully getting fixed in user space, and there is a patch floating around for just having glibc use the native fstat() system call. But even with the current situation we can at least improve on things by catching the situation and short-circuiting it. Note that this is still measurably slower than just a plain 'fstat()', since just checking that the filename is actually empty is somewhat expensive due to inevitable user space access overhead from the kernel (ie verifying pointers, and SMAP on x86). But it's still quite a bit faster than actually looking up the path for real. To quote numers from Mateusz: "Sapphire Rapids, will-it-scale, ops/s stock fstat 5088199 patched fstat 7625244 (+49%) real fstat 8540383 (+67% / +12%)" where that 'stock fstat' is the glibc translation of fstat into fstatat() with an empty path, the 'patched fstat' is with this short circuiting of the path lookup, and the 'real fstat' is the actual native fstat() system call with none of this overhead. Link: https://lore.kernel.org/lkml/20230903204858.lv7i3kqvw6eamhgz@f/ Reported-by: Mateusz Guzik Signed-off-by: Linus Torvalds Cc: # 5.10.x-5.15.x Signed-off-by: Miao Wang Tested-by: Xi Ruoyao --- fs/stat.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fs/stat.c b/fs/stat.c index 246d138ec066..9669f3268286 100644 --- a/fs/stat.c +++ b/fs/stat.c @@ -240,6 +240,22 @@ static int vfs_statx(int dfd, const char __user *filename, int flags, int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat, int flags) { + /* + * Work around glibc turning fstat() into fstatat(AT_EMPTY_PATH) + * + * If AT_EMPTY_PATH is set, we expect the common case to be that + * empty path, and avoid doing all the extra pathname work. + */ + if (dfd >= 0 && flags == AT_EMPTY_PATH) { + char c; + + ret = get_user(c, filename); + if (unlikely(ret)) + return ret; + + if (likely(!c)) + return vfs_fstat(dfd, stat); + } return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT, stat, STATX_BASIC_STATS); } From patchwork Wed Sep 18 14:10:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao Wang via B4 Relay X-Patchwork-Id: 13806926 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 C2AFF18B48D; Wed, 18 Sep 2024 14:10:51 +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=1726668651; cv=none; b=eRs/N1VZ6fYKFgSlKXCA4z1KeEpkqQ+ZaP+8UKLe/1Ci3AHQkSJfyFkacDdiFqi2MGWiX7uj7RU2BVJFjQKN0s5wJL73NJwB7uSFxjPt/rFW7RqmvogxdOrBU7OTs+L6awlMiZ4W+L3k7EmaEEeW7zMvZCumCprrJ5Ht1nGSDVA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668651; c=relaxed/simple; bh=COwTeI27/J7n7O3Msn2gtQ1KtMylFJM8w6EzpT5RgFo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BFfe8qP99TrP6GzLMeEJoaCqy3+KnEir7IhqK/SSONjkXgxUZ3u2cqvFafgdVwyAdXb1QVenicygrybIDwi2Q5q4YoW6rooP/rgpaBLQhAfQNmtpZOw3OVuAiYu05fJEASGtBxw0mwUOgprUB/kzVcuy0HxPLBhwiUG4CqUyjp0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=havtT3UF; 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="havtT3UF" Received: by smtp.kernel.org (Postfix) with ESMTPS id 783E5C4CECF; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726668651; bh=COwTeI27/J7n7O3Msn2gtQ1KtMylFJM8w6EzpT5RgFo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=havtT3UFG+wAAKgIDkXb+uGiFjzj71HKtr5QnWusRx9LYu0B8mBAutfPGM29ic0OA GTrXRXVmqeOsierKvdpq/uiKiMkOyCKsxIJ48Mf7qreyEZMD9VBFuuY1F5ecGsdzR6 e1qDUBwIpnU8Vu3jwL3A8iw0+tAb3IGyydNjmYTqZIW/wH0xn+vq+BfPNaaMxTnmNB 1rAKiFzM3rke0ZCrsxzJHcczjH9zKQkokjvELLv0YcRFijkwiSKvaWCoPxPxdmpxzw qhOVDiQOE3Q/bOvg4OtzgTHptlIfeXjRQAgRSTEb60OnkRJ4qIkhRhp/GS0W4FMkjW THhIfMeQyapIQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65F85CCD1B1; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) From: Miao Wang via B4 Relay Date: Wed, 18 Sep 2024 22:10:38 +0800 Subject: [PATCH v5.15-v5.10 2/4] fs: new helper vfs_empty_path() Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240918-statx-stable-linux-5-15-y-v1-2-5afb4401ddbe@gmail.com> References: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> In-Reply-To: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> To: Greg Kroah-Hartman , stable@vger.kernel.org Cc: Xi Ruoyao , Mateusz Guzik , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, Miao Wang X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1116; i=shankerwangmiao@gmail.com; h=from:subject:message-id; bh=NNdeRdWR8oQ0NFsZXKcLndO0clVTPZKHtPgFAjQVi/A=; b=owEBbQKS/ZANAwAKAbAx48p7/tluAcsmYgBm6t9opLBl8Es3PPOAB3sh2DYnhurwkAcr8xbwd t0+ZmqsmmmJAjMEAAEKAB0WIQREqPWPgPJBxluezBOwMePKe/7ZbgUCZurfaAAKCRCwMePKe/7Z bvZ/EACAsFifV4qsXKY44jmfyBeldyhGFRY5pwuMVHhdpCD6pudl286QKaFGcDxkhzx7XiDAF4m CxbYmME9OJ1WDbCcOWD4lIb3I/CIs7TO5MqfeChlVHE8Kusfegcj3TCJSVwOF3OWzNU7k7XhmpL NGNomlTBfjZfPFugUK88w66K6p64sHHs5XseQuLiIBcMg1X9NRbZ2ptYtNE/PSMflPZxOthJCua eRAr5bjrauavi7XFby692T4B3w7IhViG1fQLGU9lZBWLdVe47A/bH+gnc5eMVAofuDfsBS5OESD 4VVRMQgt34Ws7IpRzrLc4aIHjetYl3348QjFD701IAUXLo9CDB4J4Ft0XKSMmjCIG1CZWWjEOKI b0IUt3V3JYykbK6po+qieno6bGCKAYtuhvKPyi+EW8zlm6sw9rQJmVrvhBrvfnv6yPAfG7faXsv Wwmriw34HtjMNaH4PONAzS6DBEi5gHI43nzsMOWcA1dolPn0/8VmHB3ch70aQro0VQQypIL5wa/ ToLyiobwgGQEkDZdw1017gnyp77Do0zPnNu3AXw+9LUfz+Md53cc4MrBhkwn9cTrFR2TqEoWcKV p7eZ/cykC79iVs0B/1EACEpAFiGyZgR51cbNPMSWdy0TYWM99M7SrNjfWlhP8/aHf6v7bTGhXay KnBkJZWU31EDtRg== X-Developer-Key: i=shankerwangmiao@gmail.com; a=openpgp; fpr=6FAEFF06B7D212A774C60BFDFA0D166D6632EF4A X-Endpoint-Received: by B4 Relay for shankerwangmiao@gmail.com/default with auth_id=189 X-Original-From: Miao Wang Reply-To: shankerwangmiao@gmail.com From: Christian Brauner commit 1bc6d44 upstream. Make it possible to quickly check whether AT_EMPTY_PATH is valid. Note, after some discussion we decided to also allow NULL to be passed instead of requiring the empty string. Signed-off-by: Christian Brauner Cc: # 5.10.x-5.15.x Signed-off-by: Miao Wang Tested-by: Xi Ruoyao --- include/linux/fs.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index 27da89d0ed5a..6fe2ae89bba6 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3736,4 +3736,21 @@ static inline int inode_drain_writes(struct inode *inode) return filemap_write_and_wait(inode->i_mapping); } +static inline bool vfs_empty_path(int dfd, const char __user *path) +{ + char c; + + if (dfd < 0) + return false; + + /* We now allow NULL to be used for empty path. */ + if (!path) + return true; + + if (unlikely(get_user(c, path))) + return false; + + return !c; +} + #endif /* _LINUX_FS_H */ From patchwork Wed Sep 18 14:10:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao Wang via B4 Relay X-Patchwork-Id: 13806930 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 D04E318B494; Wed, 18 Sep 2024 14:10:51 +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=1726668651; cv=none; b=dvu5uHn/yw0cHBkSHmTZqySPqMRbO3kprAlzY/m58rcyOtzF75ol62lUSU17JrWNLlku50h8kJ76u+r8n6yhAy8PVxZJeGE4TZuIaTYyhzK8FiO+HQ3f7x7GNqgVZoWkaVZgTIl2IF1MTlt0VoMNVR+pNrDdaVKx658N0UrGPt4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668651; c=relaxed/simple; bh=9eGxhGLklk3aCZs4scbt0MeHs5Y4XdfBzockE4wWNYE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nLhclxb7Qd1Zke2uaybCYs3HGDbC5z4g1Zv2TFj4CA5f/A8xj9heQoMGZlRtHaisaxeMdLaA4lMcsXNAjpKY27Mwy7c0uPEMqgQj8rN3uUdGqMatRjbE42V705vOEmBjkAUHpxJ43AJwQQdmPcXkm6KdNv59CtHNIrswNEcknRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YMl5jAx7; 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="YMl5jAx7" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7F2CAC4CEC3; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726668651; bh=9eGxhGLklk3aCZs4scbt0MeHs5Y4XdfBzockE4wWNYE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=YMl5jAx73wRFU75A1VohTY7vT+NE2yKXmDs3PcbY1pR+JgXy1O5UXgBapmyVBw+u3 /HS5MFx+jZDPykTZANilX665+gjP7n15pgSwAk3gTbi/+afSXJC+Q59GH+rJ5q2Cnt UkMHzJQ4O9iSh3w6xlVM2eOmVeittJQwj9db7SDaQTaHx/mTp3nRmU1499Kmx55cD4 wa3SN3GS0asTon54cXlvlTXj4HOzGDh59IpScvrz4r8EeKBDGOy5BRMixsQSeUvk6o QiWqAn2ZFSpbX8XlTlCP5xP6YdbVqSgnpRPq4bg0Kix6YRbGWn5O+rlwfceWvH3Jr8 379URkLzSW2Vw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71860CCD1B3; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) From: Miao Wang via B4 Relay Date: Wed, 18 Sep 2024 22:10:39 +0800 Subject: [PATCH v5.15-v5.10 3/4] stat: use vfs_empty_path() helper Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240918-statx-stable-linux-5-15-y-v1-3-5afb4401ddbe@gmail.com> References: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> In-Reply-To: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> To: Greg Kroah-Hartman , stable@vger.kernel.org Cc: Xi Ruoyao , Mateusz Guzik , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, Miao Wang X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=1082; i=shankerwangmiao@gmail.com; h=from:subject:message-id; bh=bCFrxJOZ1JlvAjj2Q1DjyhrDQOgyZ9D8tBLlcgyrtKc=; b=owEBbQKS/ZANAwAKAbAx48p7/tluAcsmYgBm6t9oR0f0W5q/TKDHi8vrrYol5brh4IWMqGm/o CrztW+Vov+JAjMEAAEKAB0WIQREqPWPgPJBxluezBOwMePKe/7ZbgUCZurfaAAKCRCwMePKe/7Z bjMoD/43wQkEksXRWesOQYsMnncO3tApr7WqjPjqHbkVdYflvX4RqGPyyLaxhy2WyD0s0y/2G/O /1P6hAlr6ZK5u/g0aGB6iMmVAI6zxJS+nR+6GP2rd16VpVB1Te+0r++QPtMUg85OgNgYx+HUI5M xshcVyMPmb+UXgbZ3FjkvZQ5q99aK7C2fEAHZ9XWvc+VIsO02yYVFDC7XShkwPea96oe9zzBTlJ FRifgVefZ0ehZEI0nKzXost8c7xCs3wEHOFs5Kod+9OCdtrqnc9WkItPMME1pzXKWeF0t3cxx+/ qkS6k1j9+kEuzYt44X2wsvpwkb1CYoyjulmGKoqBEd8LsDrR/o85syu9fRvlD8YhbAqFIift/S5 BjOM88WQpdkrwyH5TTek7l9rb/AoqTH329CgkF+SLw4YFsvCOsoByzpQEQ1oGY9quajjiYk54Au fNlTV1Kwn+aghE0qz96FLBDenXAUE8bhSfrJi7NAfki/DTsHjtNjc0iGv+HcyWQi88F9or5Y9GZ Cmdi2pDZf4gAgE5/YpQIYLu7iyS1yuDlaaUkXMwPgVd+EPXynexZYkx90fmBkDkrWc4HNLRrfpc sxMKvd70OZ0ZA/YX3KmNhC2GN0zce8TQemlDGW8EdA6/9xVD614cJcrqZIvqrv7CAn/1vOU2JvS objHa336Dvmj1pA== X-Developer-Key: i=shankerwangmiao@gmail.com; a=openpgp; fpr=6FAEFF06B7D212A774C60BFDFA0D166D6632EF4A X-Endpoint-Received: by B4 Relay for shankerwangmiao@gmail.com/default with auth_id=189 X-Original-From: Miao Wang Reply-To: shankerwangmiao@gmail.com From: Christian Brauner commit 27a2d0c upstream. Use the newly added helper for this. Signed-off-by: Christian Brauner Cc: # 5.10.x-5.15.x Signed-off-by: Miao Wang Tested-by: Xi Ruoyao --- fs/stat.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/fs/stat.c b/fs/stat.c index 9669f3268286..b8faa3f4b046 100644 --- a/fs/stat.c +++ b/fs/stat.c @@ -246,16 +246,9 @@ int vfs_fstatat(int dfd, const char __user *filename, * If AT_EMPTY_PATH is set, we expect the common case to be that * empty path, and avoid doing all the extra pathname work. */ - if (dfd >= 0 && flags == AT_EMPTY_PATH) { - char c; + if (flags == AT_EMPTY_PATH && vfs_empty_path(dfd, filename)) + return vfs_fstat(dfd, stat); - ret = get_user(c, filename); - if (unlikely(ret)) - return ret; - - if (likely(!c)) - return vfs_fstat(dfd, stat); - } return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT, stat, STATX_BASIC_STATS); } From patchwork Wed Sep 18 14:10:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miao Wang via B4 Relay X-Patchwork-Id: 13806929 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 CEBE018B492; Wed, 18 Sep 2024 14:10:51 +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=1726668651; cv=none; b=LOKHUvfSgoD/mfbm6IoAtM1RiajWJZ9DBNSAkxBr6q4irOQZn2pyyiN9dOgolRmMKCNlrNyjsTDXJChscSK7HOP43+CbPTR0LipT3UiaGR0KOAkSmmdSnwIwDzsOWxEI2+ZDdC8oWpWais1ryl+ioWzVedT285HVA8ry/1muUws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726668651; c=relaxed/simple; bh=Kt6CpsC66jTqGsclfSFK2W0/KTCLGfSKeTpOqZDcGpM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=E02hQujui6VIKqepe0Q0FFxQry7CwcrPVqV7E6lhcqHQsKDdoNFaw5ooALMaE705z/MTZLL84j3MNZqNF7WIm6KHDsjNgxAAjBWA9hn8r4pfOvGfVo99ZX2enfu5VGpk4l/QHTWFXsViP7Yh5++0aS/CHFjRhmfLvNxu/R2xwb0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fN+Z7FQM; 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="fN+Z7FQM" Received: by smtp.kernel.org (Postfix) with ESMTPS id 8921BC4CED2; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726668651; bh=Kt6CpsC66jTqGsclfSFK2W0/KTCLGfSKeTpOqZDcGpM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=fN+Z7FQMb1lr6UVOUgXzvBqNQYocsYMinA6diNAYrsZmTUZ29+vrCrMca74IrtrmY RZkwFfOE1uSpvV0rKxmPwQLuOekgcmWiVpcnlnbBcond4IV8LAgNylr1ZK3V2ELe9U tTiC87FNgg5VEQSp2Cum+RhWVxKBdYsn7hw6aG0TwAtvbqgC8FpO+PzHDew+zixJLZ EmViWT2zMuX9FCHDygwtk+m9CnmcK5FeLyJbYq4ezKwGBr6hcZlbYdfcldMW8rd8RT tzm2dEWYBoDkb9Ddgy3arCm/r+JNa3h1T+P5tjZkkcwE6ggJ66z11JgvNs5gpWNE7u +RxckmNNKvl/g== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D4E0CCD1B0; Wed, 18 Sep 2024 14:10:51 +0000 (UTC) From: Miao Wang via B4 Relay Date: Wed, 18 Sep 2024 22:10:40 +0800 Subject: [PATCH v5.15-v5.10 4/4] vfs: support statx(..., NULL, AT_EMPTY_PATH, ...) Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240918-statx-stable-linux-5-15-y-v1-4-5afb4401ddbe@gmail.com> References: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> In-Reply-To: <20240918-statx-stable-linux-5-15-y-v1-0-5afb4401ddbe@gmail.com> To: Greg Kroah-Hartman , stable@vger.kernel.org Cc: Xi Ruoyao , Mateusz Guzik , Christian Brauner , Alexander Viro , linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, Miao Wang X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=openpgp-sha256; l=4898; i=shankerwangmiao@gmail.com; h=from:subject:message-id; bh=OE5eWgqDbbcGmkrUd4eBvT4vTym9+pB223rJsVqpcEE=; b=owEBbQKS/ZANAwAKAbAx48p7/tluAcsmYgBm6t9oSJR2HVH8I9ryMHBIVdRQMzP0udOSKMrz3 yNnic5aTiuJAjMEAAEKAB0WIQREqPWPgPJBxluezBOwMePKe/7ZbgUCZurfaAAKCRCwMePKe/7Z brEGD/9+itzEZuJDXbNurtRve2swtSSlksFJktuKRGLiewwS70XydOAeWzsyDqPNQTaolDcp75S /WJ5u3elMF34kxCZcermuD95hhQuEiIDm9HtsyY3c2a+UZE9Az2rtF3MS/eFA4F0zfxlAKBstgb Yg6OrDSj7fFF1TgHi4nlBrOzY0JmRmjKyKeQ9Ge9ZB2VtgbkvSt1fc1+JIQDL6mCjenjKLZ6el7 iqtVcoeLAofNekO5QIJO5x5DSl2hK2oPYygsqSG4mSKPKNth0gJXyowFya+7Z31/txUP/7GooD6 ucQd4MeKxgNc1jSA2sjnKlV7UZqJQcGxQCUpnh3u0GUnnaLPajOWf2NFm2xn1PC0O3XXcRVKNIb oiLQzAqrUGnu9c/Nw9lz68qo2X6ix34iVsXiBfpFx8980BEVIstliUfHKy8vSrBV5KjLaQRSoVh geFAyY30jeOq5Jx8iHWhscQ4q5i78+C+iikgwTcPZwOjOsdpzL/WK+3FqG3U7Uxxcn77HKfnrjD XF522XTkpAKDWhLKUjxsDrLMtqyIA99iMave6AJPEe3m0rlcnrU8BxgN9ketV1AMWe6A90TXOuS LbPwl4a0MUCYym7jVMskKQE+WkoF3c/mjWvUxudrZouAD2qFrXg7DWKxzltoGa1cTDo8QKRZ+Py +7K985VA+e27zOg== X-Developer-Key: i=shankerwangmiao@gmail.com; a=openpgp; fpr=6FAEFF06B7D212A774C60BFDFA0D166D6632EF4A X-Endpoint-Received: by B4 Relay for shankerwangmiao@gmail.com/default with auth_id=189 X-Original-From: Miao Wang Reply-To: shankerwangmiao@gmail.com From: Mateusz Guzik commit 0ef625b upstream. The newly used helper also checks for empty ("") paths. NULL paths with any flag value other than AT_EMPTY_PATH go the usual route and end up with -EFAULT to retain compatibility (Rust is abusing calls of the sort to detect availability of statx). This avoids path lookup code, lockref management, memory allocation and in case of NULL path userspace memory access (which can be quite expensive with SMAP on x86_64). Benchmarked with statx(..., AT_EMPTY_PATH, ...) running on Sapphire Rapids, with the "" path for the first two cases and NULL for the last one. Results in ops/s: stock: 4231237 pre-check: 5944063 (+40%) NULL path: 6601619 (+11%/+56%) Signed-off-by: Mateusz Guzik Link: https://lore.kernel.org/r/20240625151807.620812-1-mjguzik@gmail.com Tested-by: Xi Ruoyao [brauner: use path_mounted() and other tweaks] Signed-off-by: Christian Brauner Cc: # 5.10.x-5.15.x Signed-off-by: Miao Wang Tested-by: Xi Ruoyao --- fs/stat.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/fs/stat.c b/fs/stat.c index b8faa3f4b046..f02361a2ae54 100644 --- a/fs/stat.c +++ b/fs/stat.c @@ -184,6 +184,37 @@ int vfs_fstat(int fd, struct kstat *stat) return error; } +static int vfs_statx_path(struct path *path, int flags, struct kstat *stat, + u32 request_mask) +{ + int error = vfs_getattr(path, stat, request_mask, flags); + + stat->mnt_id = real_mount(path->mnt)->mnt_id; + stat->result_mask |= STATX_MNT_ID; + if (path->mnt->mnt_root == path->dentry) + stat->attributes |= STATX_ATTR_MOUNT_ROOT; + stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT; + + return error; +} + +static int vfs_statx_fd(int fd, int flags, struct kstat *stat, + u32 request_mask) +{ + int rc; + struct fd f = fdget_raw(fd); + + if (!f.file) { + rc = -EBADF; + goto err; + } + rc = vfs_statx_path(&f.file->f_path, flags, stat, request_mask); + +err: + fdput(f); + return rc; +} + /** * vfs_statx - Get basic and extra attributes by filename * @dfd: A file descriptor representing the base dir for a relative filename @@ -220,20 +251,13 @@ static int vfs_statx(int dfd, const char __user *filename, int flags, retry: error = user_path_at(dfd, filename, lookup_flags, &path); if (error) - goto out; - - error = vfs_getattr(&path, stat, request_mask, flags); - stat->mnt_id = real_mount(path.mnt)->mnt_id; - stat->result_mask |= STATX_MNT_ID; - if (path.mnt->mnt_root == path.dentry) - stat->attributes |= STATX_ATTR_MOUNT_ROOT; - stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT; + return error; + error = vfs_statx_path(&path, flags, stat, request_mask); path_put(&path); if (retry_estale(error, lookup_flags)) { lookup_flags |= LOOKUP_REVAL; goto retry; } -out: return error; } @@ -615,13 +639,28 @@ int do_statx(int dfd, const char __user *filename, unsigned flags, { struct kstat stat; int error; + unsigned lflags; if (mask & STATX__RESERVED) return -EINVAL; if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE) return -EINVAL; - error = vfs_statx(dfd, filename, flags, &stat, mask); + /* + * Short-circuit handling of NULL and "" paths. + * + * For a NULL path we require and accept only the AT_EMPTY_PATH flag + * (possibly |'d with AT_STATX flags). + * + * However, glibc on 32-bit architectures implements fstatat as statx + * with the "" pathname and AT_NO_AUTOMOUNT | AT_EMPTY_PATH flags. + * Supporting this results in the uglification below. + */ + lflags = flags & ~(AT_NO_AUTOMOUNT | AT_STATX_SYNC_TYPE); + if (lflags == AT_EMPTY_PATH && vfs_empty_path(dfd, filename)) + error = vfs_statx_fd(dfd, flags & ~AT_NO_AUTOMOUNT, &stat, mask); + else + error = vfs_statx(dfd, filename, flags, &stat, mask); if (error) return error; @@ -631,13 +670,14 @@ int do_statx(int dfd, const char __user *filename, unsigned flags, /** * sys_statx - System call to get enhanced stats * @dfd: Base directory to pathwalk from *or* fd to stat. - * @filename: File to stat or "" with AT_EMPTY_PATH + * @filename: File to stat or either NULL or "" with AT_EMPTY_PATH * @flags: AT_* flags to control pathwalk. * @mask: Parts of statx struct actually required. * @buffer: Result buffer. * * Note that fstat() can be emulated by setting dfd to the fd of interest, - * supplying "" as the filename and setting AT_EMPTY_PATH in the flags. + * supplying "" (or preferably NULL) as the filename and setting AT_EMPTY_PATH + * in the flags. */ SYSCALL_DEFINE5(statx, int, dfd, const char __user *, filename, unsigned, flags,