From patchwork Tue Nov 26 15:54:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13886133 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 962A2D3B9AC for ; Tue, 26 Nov 2024 15:54:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4642E6B0083; Tue, 26 Nov 2024 10:54:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4131C6B0085; Tue, 26 Nov 2024 10:54:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DAAC6B0088; Tue, 26 Nov 2024 10:54:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 11B076B0083 for ; Tue, 26 Nov 2024 10:54:57 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B60F21A1362 for ; Tue, 26 Nov 2024 15:54:56 +0000 (UTC) X-FDA: 82828694466.22.1BBA970 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf03.hostedemail.com (Postfix) with ESMTP id DA8BA20011 for ; Tue, 26 Nov 2024 15:54:52 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fIAh4dxF; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732636492; a=rsa-sha256; cv=none; b=17UPU6sVvAXS731Sgc66ejiPdY62thas89wzvtxnnJrmQOl2aiNrdR9Vx4Js+/1o0K7qC7 6G0PTeLgnRN1uFadPZ9YBd0gjz1w8NQPsWgvH41Mlqay3XIin2UnH4kiLVCw9Bn53BrZv6 ZMiu8Zyv1j9hxczp4Rk7uFNo5I/Qk9s= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=fIAh4dxF; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732636492; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9bhJfmmX8eWLULpBVOq7lXLXxEaVv114s3PiBizSr28=; b=h1ZUxfhV6ohA5teLL4W2hqohgUqkYnq539dNmX8sWKLowJQ1MbMFCI3KOZljXv6yheZpow 2DxjgRAcvZB+DRY0OtCUo2YXLrmdRfW8f10kqGhZ/61XTJ1wjThvL9caQEkvOE5QATJd8+ A2LcVSTdlnGJVLziiiqhXpqXjEPselM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 4556EA407F8; Tue, 26 Nov 2024 15:53:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 345EFC4CED0; Tue, 26 Nov 2024 15:54:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732636494; bh=uhgh93plhiFV1pHFjFAu6nt5SYYT8N+YCq9mpcqsLXk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fIAh4dxF+U4xexzovtB/7kPFGF35L5IKVEm3YrWxmpi09Xhg+s3Q6xBHA+BEOKxM6 01c8Ahzm2uaKRidP1kBlNetkAHbGcVjfgSZQcMQsSVcnkzT0DMkz1+VBNapdeMsbVW ggHJdbDQPhH5XARuL9TLITKoR450QibdFfJ4W/Xn+OhcLvlvPGY8eNqgc66lmwXcem RlTQItF+ZF8mUqqa7lJxN0dnHSjampVwpgtBVUukrEZkV10plMr9XHU9RyWafi/DGu Mmqs1R8Ie5cshvo6L+ztqqUnOPCJWlkOhaXYEp1Aaci3D1uCUKchh4g1N9J1OOlxuO KmFZcPfEyEVEg== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever , stable@vger.kernel.org Subject: [RFC PATCH v2 1/5] libfs: Return ENOSPC when the directory offset range is exhausted Date: Tue, 26 Nov 2024 10:54:40 -0500 Message-ID: <20241126155444.2556-2-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241126155444.2556-1-cel@kernel.org> References: <20241126155444.2556-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: DA8BA20011 X-Rspamd-Server: rspam01 X-Stat-Signature: 31f8o3scqa7wdj5pq67m1if7o9w939a5 X-HE-Tag: 1732636492-818874 X-HE-Meta: U2FsdGVkX1+PP1hfCvJrGz799MuRu52zGq/k9wFZ6AzLbOoOMuD8Q2hTSn5Lf8hiOeC523GkeHMIN0XsiP+VIpnoV9N2JXZXyUm9mCHdffb+Ud23aWZYhKA+MxV8lVhbA98lah+oVHey6ToxdVhYYpLfNrbtYvrEwVYCUooA05mrKvYWbwEFZMCXojLC0xuLQba5nrVX3PrsIE77rxkHVWq+GPHyX5nL6zvQxTVtuP2M23TAG1v13Z7AC4WU1xs3ShNiXKpk/erdH+Ce307QIeQUBX8Nn4Rb4jFFDE6apyxstpedmmAcP/IcDO1AV1Tm+Woxw4/FXX7NU+2YyCm2UdBuhjd6sbzmfONwZC0XN5/E10jGiMmUp5/WGibp7qXkOf1d1A6aBnAdFHcdqI0S/AnVwLTvR8MHEWeOtRWvN/yvFbaBkH5sjx8uebk5Hb5+sHeM2qPeXoRvyw29dyNt954jKJTcICkxCd3D+qewR/rUR3Q0BU4kvkmZC/oxruB1TCdt7MtsnOcr5kSZxQEy35UDBXgdd6EYwA4MiljcH3m9auHkT4ddSAVKOldb7/dpe+TA8r5kQjZznHEaVD7JhY3T/ACl75IoZlmWFnSzdB7cR8vdEAiYRSQFCzMyZ3HHuRw/pu7jwu6Gq8qEJE4in6uBh4AJFQBlDC047e/HXsVuDXl9alUY7iL70wvtkD+TwkhA46hfycGiqX0kpmxkirAN5VWewI1hwv0+k3f/1/XZlYqyX8Qs98PQJL/qZ8Ll6OmYGn4nREBLhhzTSVrsZfb8Mv8PPOqsY1m9RKVkieRB8gqoI5vb0e2EiJwqh4HcIy+znQXSCQyprnjgeJsQYJ3uAJ9yfbbODJtah22GYh+KDqHlGhdtxhd9FdumauD5a5iNmA75B1ws7f+OD2RVSgHtiXh4QMwAQsHj1zV9LH42qvNRnWToCYu5iJCVreR+lg41ptCCx4u5IUVs9XJ Hwr9PFhA KLxa6Z/r2X3MCn3kGaM1mVQzWscjMRD881ISsIsoSA7wf3+c2GkSXgVU0p0XeJRhzOAGax+dSOVAgl6Lz7puVnxNirJjr3eHDpFezicq/De5YT7wSoc3zsbi+Fbu9Bo6RaFi5srz2h/Ds1okIEZhHgbTwkBoe3Xu9LLs75h9aJmHfOttrB2vmhE7HEW1pD+UnaBAzGEdsPLGtbGlIqaeCv61bb0YVpVv0BcE/OpC9PmFhRNutPe5dyRizDHotAJxKgIAR X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever Testing shows that the EBUSY error return from mtree_alloc_cyclic() leaks into user space. The ERRORS section of "man creat(2)" says: > EBUSY O_EXCL was specified in flags and pathname refers > to a block device that is in use by the system > (e.g., it is mounted). ENOSPC is closer to what applications expect in this situation. Note that the normal range of simple directory offset values is 2..2^63, so hitting this error is going to be rare to impossible. Fixes: 6faddda69f62 ("libfs: Add directory operations for stable offsets") Cc: # v6.9+ Signed-off-by: Chuck Lever --- fs/libfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/libfs.c b/fs/libfs.c index 46966fd8bcf9..bf67954b525b 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -288,7 +288,9 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN, LONG_MAX, &octx->next_offset, GFP_KERNEL); - if (ret < 0) + if (unlikely(ret == -EBUSY)) + return -ENOSPC; + if (unlikely(ret < 0)) return ret; offset_set(dentry, offset); From patchwork Tue Nov 26 15:54:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13886134 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02577D3B9AC for ; Tue, 26 Nov 2024 15:55:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 093F26B0085; Tue, 26 Nov 2024 10:54:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F37C66B0088; Tue, 26 Nov 2024 10:54:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D88116B008C; Tue, 26 Nov 2024 10:54:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BD48D6B0085 for ; Tue, 26 Nov 2024 10:54:57 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 743F840C55 for ; Tue, 26 Nov 2024 15:54:57 +0000 (UTC) X-FDA: 82828694592.14.197469C Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf30.hostedemail.com (Postfix) with ESMTP id 3770680011 for ; Tue, 26 Nov 2024 15:54:47 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=oOKjw2+F; spf=pass (imf30.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732636492; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HywAkSPUKDEyheF2c93nt5psYFw121GWnXMNeLgqIC4=; b=GmpIkOKRjb/6gP2Kbi5QID6qBH5zocH5U+708lSaxFxrwNsSygqmheiacHlId2AbebULeL w/XZVl6f4UC8lwQ1AkQsNJv2IsazBv09962JEub/rLEe8rJH1WQnCbWLYIJjNnZmy74ABK Tw4k3GsFEbzaPT6KHPf6oUz+9PTH4Ag= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732636492; a=rsa-sha256; cv=none; b=v16MtnQTFoveuSWJ6RF8dQc9o6j5o/5LLuXUpLJhSoLplw1eZUkI8a1rBeuCx/5bzQlUQI L+10UFdn4i6cyF6RMmJTT/NZbvYI4HaFkeE4vA2ZAsSvHXq1o70fKkbUxEvC/EAwxnpBED RHyq0zXn+zvbaYswkvbN4krjQ/KoKDs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=oOKjw2+F; spf=pass (imf30.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 49C66A40811; Tue, 26 Nov 2024 15:53:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 436F8C4CECF; Tue, 26 Nov 2024 15:54:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732636495; bh=LzHZSIB3uuUqqSEGYbIshH/HG/EMysiZv3qwp3h8skw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oOKjw2+FzS1xUbmtZewdYyxDYIfYCcte0hAkepPYuNfbsifvo+xMGVOkNw7OhlSQ9 OJYlnQN3efHG+cxx1dSKQYCKF6kjnNGnR537DsU3brjQo+rJYRAjHQLSdAcUnAOzGS bnkOLvMiElXJSCCz26M2/WPO25rUPQgH/ZJkUvb97X2pKwG/IdIfcnIYdmfFZiPoR4 b9dLVWzC8LUIkMRsQrBOEsBw4MO1u7TQQ/AEOL8M8GVoxPKQYM+tLCM1/WvHxEQMIH ZW85T3EPgmXUfDGOkoa8P7VGRY9J2eqiHsFTKcAhFs4jnu8rgzFmu2l4C/r6GmRQ4a SBBYdKTfKqUKQ== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v2 2/5] libfs: Check dentry before locking in simple_offset_empty() Date: Tue, 26 Nov 2024 10:54:41 -0500 Message-ID: <20241126155444.2556-3-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241126155444.2556-1-cel@kernel.org> References: <20241126155444.2556-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: tx41716o8iz36thcai8inu9s1on4qthi X-Rspam-User: X-Rspamd-Queue-Id: 3770680011 X-Rspamd-Server: rspam02 X-HE-Tag: 1732636487-243441 X-HE-Meta: U2FsdGVkX1+qYGIs2ye01tt3iMjnWJ34xKS771K3lczNtr0BSoBJkkMaK8FfFlOIPO9KtmnKJdv0MXYJKullxq2/V83VuEEpVjq625My4EIt5vP06UKWGLEDKsL0SvX5BCiD29Q2iwCsE0AKn1+ZxF4t12bflnX2MAn3a8G/Qli/+M0TTAArsG+61I0p5cCQyIkDyGuOpmauK41R/oSyhuCjfS/DpOHmF95Tpd3v0fjRbareZApOzoojH0CcGZfebNbvG+6PtfdBgcAmNnmW9pDYViOl0moYFI7T9zjXfJTh1teMC0ZBusWjn7vh/jImuKf2Yw7w9jjsgp9v8m2O0nL8req3zyw1n9QsaPey6GSxRlu7km8BV34R7ZEyPgQonb1doidwFi4sMnD8d5QuXzBzuwsRIKLaOaLQy+QwP6riinS2ThqCK5GYd03alI5vMFHqqnfyk+cy4z7Znp+glJWm/hMCcPXytoBJTePOk2Z2oxntCP+7N7X+zO6f+rS22f+6WmVwK2uoPHw0be0R3ULd12h9TZHU9h/CGzwoclhmzNFJvqugS2RtO+r22OdNZfv9CKHprH55d7FvVHodVd544R9L8FUD2dcJ59o6/CuMKFZSrrhiyBnuWGDNU05FcNFVIrdqk57BcHRMP3c0/kDl7tuAt+vXf3gkBcDi04E0w1nDBieD7uGjZgqLvdnGXu8xEogT2BskaycmupeleQkhyXbbC/Awv0gSJ4eH249N5VMY2bqbQOsGRdIQ1nzEIdYU9IohwS+D0CLW+OywdTh4tZTH4aP61fEES4C+5jYhqok+gCAgmlJMoC2WUTFrRdcyPH/cMgdt+l/8khvQ9dFpT2ZpeqpKcrfNmQWcnsdb5YoUscuNB1l8JVgVBVshCsMxV3kbMQT348bnPZ2VteUVZ/MUFhCBqEJr6ZifxTxQhh7U5I91ej1Zgu/hbQpBJ+YbnckUnJn77SHk3YU 13FBvgzh PNHOMZ70+j+J+h9pingXkSTNXAoQlt1zkpFWfRkVzAyP+i6dJkR42meyrWNB4ck2o5yU6POh7t0O3+fKLRFP6OQo0b+NNhfeIX6yCcG5TXBALfQ/gObvSZMfFd9S2ZB+8q/6qlDai+kqhA1FxfBg/XLDfzLBqHk9pcGM68CxXqOUAbHoIMZ3AwDff7ZC8f8Wuv4CxXPCxkW+qo9uv8dIlnY70Pbbb+BrNAYUHBF8kLXxPgYL9rmBHtX/tzoa3AGsKhnmYiXwCScl40hj/CFkXuhra7Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever Defensive change: Don't try to lock a dentry unless it is positive. Trying to lock a negative entry will generate a refcount underflow. The underflow has been seen only while testing. Fixes: ecba88a3b32d ("libfs: Add simple_offset_empty()") Signed-off-by: Chuck Lever --- fs/libfs.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index bf67954b525b..c88ed15437c7 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -347,13 +347,14 @@ int simple_offset_empty(struct dentry *dentry) index = DIR_OFFSET_MIN; octx = inode->i_op->get_offset_ctx(inode); mt_for_each(&octx->mt, child, index, LONG_MAX) { - spin_lock(&child->d_lock); if (simple_positive(child)) { + spin_lock(&child->d_lock); + if (simple_positive(child)) + ret = 0; spin_unlock(&child->d_lock); - ret = 0; - break; + if (!ret) + break; } - spin_unlock(&child->d_lock); } return ret; From patchwork Tue Nov 26 15:54:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13886135 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4EDCD3B9AC for ; Tue, 26 Nov 2024 15:55:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 578166B0088; Tue, 26 Nov 2024 10:54:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 524E06B008C; Tue, 26 Nov 2024 10:54:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C62D6B0092; Tue, 26 Nov 2024 10:54:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1C5966B0088 for ; Tue, 26 Nov 2024 10:54:59 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C3706AEBA2 for ; Tue, 26 Nov 2024 15:54:58 +0000 (UTC) X-FDA: 82828694340.17.D802B6B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf01.hostedemail.com (Postfix) with ESMTP id 79AC640003 for ; Tue, 26 Nov 2024 15:54:53 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=By05q8Qa; spf=pass (imf01.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732636494; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+wG1gqRKTxVBichEZ6K667VeySnEED3AOvysGA+zHNY=; b=k5rBQ0e9OvqTTOosQitN01OroQ9oRlpw42Li4tyCOeVCyU8xo+tRxKusb7mP5HgAvLLDFp cLAXmHnX/8mWb1RMIw9wPl5sw3bu3MSjgEPFXrlmENjdnP/KmnqOSWLW2dSFlHQQcfPh8i lvpKmbeTDgeA2pKPbnHfVrrnpAOJMy4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732636494; a=rsa-sha256; cv=none; b=nKlo2UISzMwo3rgpO0ET/FdkAwMMoTqowUgjqgRYoHbpn7UIRKn1nfX+svNM6lZeqKJdAk UC9rmyRWogTR/E06k+45TFGf5M5hg3SOkQ0643OUuHhXOLgB51+iXxLM5xqd6mtC7xfMBi 89HLlLrAiaTBmUKZle9yFr8+8gDoHS0= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=By05q8Qa; spf=pass (imf01.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D69E05C5931; Tue, 26 Nov 2024 15:54:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B2F4C4CED2; Tue, 26 Nov 2024 15:54:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732636496; bh=G5Isx5LWQiXPXi2iyTyoRug884HtfgCAxYAVKEyJLvo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=By05q8QaTXtyBsRr33oV31LREOAX2SwvLMusYu3wtZsr58fcPnb2O6q6OVj5NXrg5 K1HCHoPJ9iV1kyOupcUv9D6Ev255H5pDTnKE365/Boa7xD8q261E8HpblyqRdUFGXN 6S1Eq4+P6nDCAoOIubvwlhe7PAn+gCCxgqdSx96oVZgn4WfXaNavV4Y5a6wZ+Ef2mg vQCS0Sqr0O1LnmdolPkaIKmcFv9f/BtAUfjFLLQkVEtni3j0/8iy2pkb12EV9Fdy9h 2y+NhHSiEobBm0DiCwuxEhfElV5uaHugC2fdXausDp9XXrpmj9Ob5/PLxCqr1E2ofB FlWAyDTdmQJtA== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v2 3/5] Revert "libfs: fix infinite directory reads for offset dir" Date: Tue, 26 Nov 2024 10:54:42 -0500 Message-ID: <20241126155444.2556-4-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241126155444.2556-1-cel@kernel.org> References: <20241126155444.2556-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: e7y4ym5ysc77ewo68fpa9byeskm8xhg1 X-Rspam-User: X-Rspamd-Queue-Id: 79AC640003 X-Rspamd-Server: rspam02 X-HE-Tag: 1732636493-794237 X-HE-Meta: U2FsdGVkX1974lBPSZ67AccDR2/Gi9bIR9p6haUwa+gQnPMPADEsDmSvZN4QpGdmkHgkjyRcCXOmxpwes6RNAlMTohouiZCP4oIZB+/ZfCeGkb2KWpmN6U95w7+QGMPkSFZRWw9bJnMzrMymymrH5RyWhvExdf6DobiFb9BfPxYwx2Zw8sburO2DXo0m0Ca03+6Yb5mGSm8Jz7B2vK13lTwr6ixeHJsQnqBZsVeY7NCyTVBe4S6o6GEI1QoK9GBwHGWiqzVpD4M01Jgf56NuqhM/PvuLPJUSFk//YkKSKrqlDfnnqfblCCDmUF1o6PGV096lnQWeR/Pa5iEw2m9cJWX44CUvJ8aNUibfkYrcGIrTXCGvvc4kvr5aa8TtqZ0weluhPQAl873LaxciN9VqxeC6R0xE/xG28HHBjVFU0ZoFjHOWX5zbXBfYUFJiTS1lmatOl9lpXWIrJ1g2M7FQExfXrjNnp19H5nKY73TH423SWL8TbQ3Qw3uM0LyZEIGzvSsiD1bygMmuHw3ebTe+15IUWoT2C+PGeeoxUpWUD0V9buf2HgnUpGAfwxNoNx8Qqpz8lOAK5v0mbq17vMKnnyzV+lzCppJjuoITrtv5IsFK++zjfpD3qJ5hStUtpl/IOG6ARLjbw7/NX1U5G4sNFthE8ZavWID2tw4q2BfujjIaMatT27sVkDfxE0Yhw+yz9xN4tXCK/gZw0Baby8H3QNczC3XpPLdaDAyxnsM0oJXKswWmZvfWvZDnubHaCfZCyrhBhyMCEcQk1PVkEZWPXAqPtPv+bQ4QpQgI475tpkfjSkAH3tlyAW+Sg5yWD9t7SFHkErqivrFS79sKlKjsgF34oOxpjYzPOekqg3owEr4kpvIom3kfReM7MIC56xTkaJVvUKf/XO0x+VOuAQmHzjMbgUf5NrbRY2jUyV7VgpYEwY1WkcYt3rMW162QNdXifGiWDPNPr/sZpEBVrhb un+byfkk +SPpL/nRHC2sFBTLUKjUdg4Ak41ZJgdGOjYswaFSsyK8SqVFAappb/sFJYrSdZkSnJKoM9eqakaAX/vQUvXJ8mCK6GV2J1gL/ZRi54GhVFuEuS1Lj2uDTkJAuVp1mLanykj3hWS8rM7raZtsDXgbUcFx7gICikJBpQCVwr0iBDjHMTJaZwexbwXXOOLophuGafJ3QP64UsFneh791fJP7KhMpN5GHA2TGeGIEggyBgW/2FgU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever Using octx->next_offset to determine the newest entries works only because the offset value range is 63-bits. If an offset were to wrap, existing entries are no longer visible to readdir because offset_readdir() stops listing entries once an entry's offset is larger than octx->next_offset. Revert this fix for the infinite readdir loop bug to make room for a better fix. Signed-off-by: Chuck Lever --- fs/libfs.c | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index c88ed15437c7..e6c46b13fc71 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -453,14 +453,6 @@ void simple_offset_destroy(struct offset_ctx *octx) mtree_destroy(&octx->mt); } -static int offset_dir_open(struct inode *inode, struct file *file) -{ - struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); - - file->private_data = (void *)ctx->next_offset; - return 0; -} - /** * offset_dir_llseek - Advance the read position of a directory descriptor * @file: an open directory whose position is to be updated @@ -474,9 +466,6 @@ static int offset_dir_open(struct inode *inode, struct file *file) */ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) { - struct inode *inode = file->f_inode; - struct offset_ctx *ctx = inode->i_op->get_offset_ctx(inode); - switch (whence) { case SEEK_CUR: offset += file->f_pos; @@ -490,8 +479,7 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) } /* In this case, ->private_data is protected by f_pos_lock */ - if (!offset) - file->private_data = (void *)ctx->next_offset; + file->private_data = NULL; return vfs_setpos(file, offset, LONG_MAX); } @@ -522,7 +510,7 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } -static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, long last_index) +static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) { struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; @@ -530,21 +518,17 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lon while (true) { dentry = offset_find_next(octx, ctx->pos); if (!dentry) - return; - - if (dentry2offset(dentry) >= last_index) { - dput(dentry); - return; - } + return ERR_PTR(-ENOENT); if (!offset_dir_emit(ctx, dentry)) { dput(dentry); - return; + break; } ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } + return NULL; } /** @@ -571,19 +555,22 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lon static int offset_readdir(struct file *file, struct dir_context *ctx) { struct dentry *dir = file->f_path.dentry; - long last_index = (long)file->private_data; lockdep_assert_held(&d_inode(dir)->i_rwsem); if (!dir_emit_dots(file, ctx)) return 0; - offset_iterate_dir(d_inode(dir), ctx, last_index); + /* In this case, ->private_data is protected by f_pos_lock */ + if (ctx->pos == DIR_OFFSET_MIN) + file->private_data = NULL; + else if (file->private_data == ERR_PTR(-ENOENT)) + return 0; + file->private_data = offset_iterate_dir(d_inode(dir), ctx); return 0; } const struct file_operations simple_offset_dir_operations = { - .open = offset_dir_open, .llseek = offset_dir_llseek, .iterate_shared = offset_readdir, .read = generic_read_dir, From patchwork Tue Nov 26 15:54:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13886136 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5AC55D3B9AC for ; Tue, 26 Nov 2024 15:55:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01FB46B008C; Tue, 26 Nov 2024 10:55:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9B646B0092; Tue, 26 Nov 2024 10:54:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9E086B0093; Tue, 26 Nov 2024 10:54:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id ADF776B008C for ; Tue, 26 Nov 2024 10:54:59 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 550F1A0754 for ; Tue, 26 Nov 2024 15:54:59 +0000 (UTC) X-FDA: 82828694592.05.D14D434 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf03.hostedemail.com (Postfix) with ESMTP id 9526420006 for ; Tue, 26 Nov 2024 15:54:55 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SsK6NV8j; spf=pass (imf03.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732636494; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4owM0JWNafDjFnRPWWpt2dnmgvCfRcrIWe73NDBkHyw=; b=pxaWPIwfVW2CBwN2NIhIQSvIwrdNlzlVleRgbwv6NagFcidlVL0HdV2qUrJeFVv1TyRWo6 Tx8dm7Mzc+hJ33Y2lvErkgMI5Vxh/kzhGxl5DduheJbky/vxvjs5WjSKYoHbfXb99w6G+f taCDDqzGcYaJp1eldsZo5gxSttZmEOc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732636494; a=rsa-sha256; cv=none; b=rMhyM07zpENn+t8v4lFIUgkUiusxJJc4epsF2zFj6jTRa3Z0+6lBxRpXqUNLSAZaHIrAba 2aee6lGwQqybRZc6C68kpWehkAu4GPZEUBhQ0lx1BvWSDZpj3V+6CgVxzxrG3selAYOLBb jAPg2B4XtQr6jfOSohIInO59X62U74A= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=SsK6NV8j; spf=pass (imf03.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 379C6A4080B; Tue, 26 Nov 2024 15:53:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AA74C4CED3; Tue, 26 Nov 2024 15:54:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732636497; bh=tMz/47mxpsuDcyrl6vBlEBAasdrsXvapQPHriWB0vcU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SsK6NV8jA66kTudkpuY9LMmdM8pQatvG2B2mhSyAac4M/AEOF25/vatqmM9Falwci yj7mYfpwIf2l+hqSnEoF+aP5ks6Sa9qzjw4ccEmVHYRy4GL0VFDkDcsJNEqWotFeud presvLu/aV0ui65cWMC7eg09NrL7cNa2Nq2ua9M6mxyMZYUPfoiJYim0q/GAjg3Md5 VO5R3CDpPk9qijalnRWIXExd8XetVGsblXaetFQ8LqpSTHEuyY/f27mhUzno/q2yQp FjMxtZUy+R4esvZFh/CugOAt1+qwdk6nNVWCiKHQe0RHfsWh95kIV/HLyZBzAJto/H HCyI48l739NTg== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v2 4/5] libfs: Refactor end-of-directory detection for simple_offset directories Date: Tue, 26 Nov 2024 10:54:43 -0500 Message-ID: <20241126155444.2556-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241126155444.2556-1-cel@kernel.org> References: <20241126155444.2556-1-cel@kernel.org> MIME-Version: 1.0 X-Stat-Signature: mhw7d77gwsncjyj66jtxc9eh1mf5cez3 X-Rspam-User: X-Rspamd-Queue-Id: 9526420006 X-Rspamd-Server: rspam02 X-HE-Tag: 1732636495-303477 X-HE-Meta: U2FsdGVkX19Yzj2v7dq6gWbmSU6wuV/7uHz2pEQwb3+VYcfNTGjcNR5cZDq/V2xa7PxlPHyyjuHg8LQukO3xVA3Rf0RO6xht6Ts44cm9j4kpQ1Vjb9G48r+bYZBIYrr2XVSwfMyt9tyiS2yFnrS93oq39YtAY5BbQlML9oCAywkojCByzAebHkD03/x8ZxN5D7Iu9WBWc+DBXwZf+otDUwGQ3mAgpQgNbX7XXjQ1DP4ZxeEpH/lWH8aZ6h+WaPhOYURv4y1B/x81XyoaMR21l6vRSIDV8aDdMIhmy0+hZy6CH3HAxtTZ6zuX3vgda/oEVF2m6HqRKBgpEiGe78bKB0v3epoITtuo/jfGXn4wT4ey+aMPuIhpkXE/c8irppBGsKH1XJ/PNeGMoB4QKIwnoesp41Fp6GeaiadLM+ymVHs21sbUAPx83fBE2IowdTPlbI98wnOY43IFzDomni3ofI6fvpCGYWsvGOjOYovkKZ40jbANDmwSOi4fIRk0RBIyO7gqGAGk3TxRUSv/XQqWeiifcuOq6avKPZsOAPTLwVrS/l3GrUoVfHQ2/4uShWzVg2Pzsc0t2er4n19mmMbZFeMp7xjiDOX4sg33Z5GOOEKRd8gLpb9yu5nIG5BQzLTokksmRdnVvv2VomiuC1LdB2BUJFe/Eey2QtmXJID0L1U13S61k33oexvHHuV8XYg3aSE+/dAe+sGxKn+hk2EHrdkUaJ+OQ+YNqW/LpO99x6W5naXN9LO+Y3UasEVw5coiK34CngfQKLxbJosprcVsHrN6J+TcMeu9V/FZI7qjvRwBT16KpK3YNvjF74mT8O7GHdbxT4TJMEoG6rb6MbuadKmAKQHDf+PCjXQpjGyWmdJ3D1MVnY2DyWANuL9z4DyJ0JZ5BxWGBWlpQvJ55u05Idme2vMwVbXlrVIY8jp7dIv2g9kW3l2fV4P30zHSiFoBnQf/VEgOjwbGbJeuoz0 Zo1ETAFG rCxufXDbck3+RFheL5m7xhj9NxO3R/9SkSEKfQae1q6Vki5t4QyXnUHnxCa+W+Vff7UeuB7uZD5KVz5TnbJpc0Gr5DwwKqxBDdBhhxa5qeD4GC80YMz2vVJeINLn/Y216ediGy3BwR69mcuVspQSE3LVz4WDJcdg/QGjxUxA3raTZGPPm90X6lvtHJQ9ZJBq2gIWGZkRtJWRK/2hR+1ClTFXxatq+v3vz/sqRT7LlfeMj6hozwgU7KzJ0vA+KEfnti4R5byCbjSxSAfGEAvvVkpZ0TA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever This mechanism seems have been misunderstood more than once. Make the code more self-documentary. Signed-off-by: Chuck Lever --- fs/libfs.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index e6c46b13fc71..be641a84047a 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -453,6 +453,34 @@ void simple_offset_destroy(struct offset_ctx *octx) mtree_destroy(&octx->mt); } +static void offset_set_eod(struct file *file) +{ + file->private_data = ERR_PTR(-ENOENT); +} + +static void offset_clear_eod(struct file *file) +{ + file->private_data = NULL; +} + +static bool offset_at_eod(struct file *file) +{ + return file->private_data == ERR_PTR(-ENOENT); +} + +/** + * offset_dir_open - Open a directory descriptor + * @inode: directory to be opened + * @file: struct file to instantiate + * + * Returns zero on success, or a negative errno value. + */ +static int offset_dir_open(struct inode *inode, struct file *file) +{ + offset_clear_eod(file); + return 0; +} + /** * offset_dir_llseek - Advance the read position of a directory descriptor * @file: an open directory whose position is to be updated @@ -478,8 +506,8 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return -EINVAL; } - /* In this case, ->private_data is protected by f_pos_lock */ - file->private_data = NULL; + /* ->private_data is protected by f_pos_lock */ + offset_clear_eod(file); return vfs_setpos(file, offset, LONG_MAX); } @@ -510,15 +538,20 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } -static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) +static void offset_iterate_dir(struct file *file, struct dir_context *ctx) { + struct dentry *dir = file->f_path.dentry; + struct inode *inode = d_inode(dir); struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; while (true) { dentry = offset_find_next(octx, ctx->pos); - if (!dentry) - return ERR_PTR(-ENOENT); + if (!dentry) { + /* ->private_data is protected by f_pos_lock */ + offset_set_eod(file); + return; + } if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -528,7 +561,6 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } - return NULL; } /** @@ -561,16 +593,14 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; - /* In this case, ->private_data is protected by f_pos_lock */ - if (ctx->pos == DIR_OFFSET_MIN) - file->private_data = NULL; - else if (file->private_data == ERR_PTR(-ENOENT)) - return 0; - file->private_data = offset_iterate_dir(d_inode(dir), ctx); + /* ->private_data is protected by f_pos_lock */ + if (!offset_at_eod(file)) + offset_iterate_dir(file, ctx); return 0; } const struct file_operations simple_offset_dir_operations = { + .open = offset_dir_open, .llseek = offset_dir_llseek, .iterate_shared = offset_readdir, .read = generic_read_dir, From patchwork Tue Nov 26 15:54:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13886137 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40108D3B9B5 for ; Tue, 26 Nov 2024 15:55:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EEA4E6B0096; Tue, 26 Nov 2024 10:55:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E9BF76B0095; Tue, 26 Nov 2024 10:55:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CC76D6B0096; Tue, 26 Nov 2024 10:55:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A8D016B0092 for ; Tue, 26 Nov 2024 10:55:00 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6FDD3C1228 for ; Tue, 26 Nov 2024 15:55:00 +0000 (UTC) X-FDA: 82828694508.01.4A3CB5B Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf01.hostedemail.com (Postfix) with ESMTP id 5864140003 for ; Tue, 26 Nov 2024 15:54:55 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aukl0fCD; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732636494; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0eiKIPAFAINpQvIQNm5swN/POrKtOBR4g3W7io/hVGA=; b=MbiDpmt+HI+e4o1FuBhVeCtfRXlrCxxDd+5bhQfJVQJSShZ53KHAA89TKV8br1uzivlvoi XcIJjIRwd7ZY9X7FA5jPmIxL0tKcuN58J+xGCvv25gbBx5z1wxk599jmZGONNGmrof9JQZ XqmK3LP2bDmu8ODTjwQ5rkTtBZxQh6Y= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aukl0fCD; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf01.hostedemail.com: domain of cel@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732636494; a=rsa-sha256; cv=none; b=Rddb/rasY68rPjN0ODRfHXMBiKNiz79YWYRyj1bPGukVs9AMMrdNoIwOiGRhzaJzXGkNWW rtp9hBzWjHp1i2Io9i+CdzBR4M+JoK9FwLhLrbFErhZtgtnhO+dSa0VqS2Nqv/CQsekWD9 /vmt4n7Igh7HrYyRsmyklIsfKGz8ziw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 40998A40808; Tue, 26 Nov 2024 15:53:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3870BC4CED7; Tue, 26 Nov 2024 15:54:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732636498; bh=l4XCZVlwO/32TPnzhT69mYkOXGa3rUk3rjWN4U/BZdE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aukl0fCDiCZH1mltSMfE078b+QiSx1oYrncyG7BPLLeBrJpI/mYBPvRwqP1VOvqOE 4aTKsTb5LJePJEXAnF7uz+znj+PvK2gAXJF6PcrL4fsAqfdVwHAR3Ps38LP1lCeO/v AaMJZKZaQJoXpdnpHVK0hvWY0RNqxnymlHPEqP8d9JLHUsZvdUWdBioiU/veJtU/hw kc7H44lmJRBVOsJm3BZWhBscorvatt1RzkelvzMeMSfZteV/eE8AphXPhC/j8ShCkn wonsfhF5alWIOvuXOnRfGI0/r5d7g/iEDUc2lstZe411KI0GGiaX1bfHbZTcQYo5jQ uun6vWQx5mkwg== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [RFC PATCH v2 5/5] libfs: Refactor offset_iterate_dir() Date: Tue, 26 Nov 2024 10:54:44 -0500 Message-ID: <20241126155444.2556-6-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241126155444.2556-1-cel@kernel.org> References: <20241126155444.2556-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5864140003 X-Stat-Signature: oyi47ai9k43nygd3spt85qn95uc678jt X-Rspam-User: X-HE-Tag: 1732636495-896198 X-HE-Meta: U2FsdGVkX1/ZvTEwhJVLeHI+LXnISIDv8tOeCeQKddGYxep4nl0qKXhHw+ejCW9276WzJQGUxDJ3bC/TonNWgYkb77+a7z0mnJaKawqEH0rw/5w9osyGDxPzRvgN7kt1EPyBhMbFDWU/EO00J3O/pnrNe8bZ+hqhyFGzhAg2FaefLptCIGjNwH9meaFUOMnQM7qf5f9QXYy5op8A8WwCwpc4WXp8u30EXWtvjfhpBq6MSTKBkh0GSCTog9lWcR4eQ7UuUxuNL3O0wwTCy+Su7dKWCiNz7rvTygXSC+ZiYgGnB6Iynjspg8Q7SxcQv5NC/h2ProcjMAKptSw30PQjYExuVwz7U7FGGzndLh4bMxU62uNs6HzvLdBm9kdj4C/Tx8/2LzyixGz9EYxN4XM9FKq59aIXp+daPMEWeWXjmQsrFR3j8AnVd3KHQzd2DDYu7s525XTZQC5sXb+k8Doq1eHBABRCGj4wekmXQDEi4H4Y6/mT3PQuTPTKXm0XuldloBxmeICJAAze3E57KsYaZv+t+JcdlzsGIKsBTfdkuLQ+cY5xpPwYhHgRnWgjJ6Az+LE/WXEjfwBc2Jl5vWAWL4djX97gl6g8llQWaPZ1Xd1Jvv3jtvxfpQhRDr7WI5pqrUeKicbW+BJt9dRQob213Y4SCwkI15PfoiK3yCSYG7gG8x0jALi8Sjx9M+UFZUREYmz1mg/FhAFdB78ATCY2y3IENNYABMzmup7M6Qw2OTy5D1kCbjTt4IFLnuTchzeTzrEP8vZInGlX5c+LIprPg/S9HRU2xHy/c8YWubxRS0/g60KygNTDcAQpXaxH5fjPdYps1rHbUNDcsC58jEzony/27x0bPPPFkXXuNgWAimXBcQ0Ya0aSEMJSYkEhIpLTTw6jfp0vEj3aIJyk6Z3Wvd0V9FUvJ4tPtmcsrmVu92Owpj3S5nDHvt7+/UezbFQtE7QuyAggDpcvTEm5+BE ma74DQ0i HEmbMSFROx1hWUH3dKqna7yKSg3DscoreH9E51xutx3QbuH5JMqsiS+VcroqZxmlcX6p5LOmwoEJd9MkFbUjbYwf3zLwEFuhRDy9UgGvxLH9MBXFBEM8FTsOgyL/D94W8I6OmJdDxFjWuTfNKpLpnCKKUbwfcmAJluHJDYKJ4pPLVwmAQ+zZVJg5SvFQfurRQu/ABmETM22XQX8U82qyNXBgIBCVYp8ukYd5bSQhkolLZ9Agfa2dba0A1GqiEiMYooTZwNAjlnkY991JTskyXL+hrgQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever This line in offset_iterate_dir(): ctx->pos = dentry2offset(dentry) + 1; assumes that the next child entry has an offset value that is greater than the current child entry. Since directory offsets are actually cookies, this heuristic is not always correct. We have tested the current code with a limited offset range to see if this is an operational problem. It doesn't seem to be, but doing a "+ 1" on what is supposed to be an opaque cookie is very likely wrong and brittle. Instead of using the mtree to emit entries in the order of their offset values, use it only to map the initial ctx->pos to a starting entry. Then use the directory's d_children list, which is already maintained by the dcache, to find the next child to emit, as the simple cursor-based implementation still does. Signed-off-by: Chuck Lever --- fs/libfs.c | 89 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 18 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index be641a84047a..862b4203d389 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -241,9 +241,9 @@ const struct inode_operations simple_dir_inode_operations = { }; EXPORT_SYMBOL(simple_dir_inode_operations); -/* 0 is '.', 1 is '..', so always start with offset 2 or more */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_FIRST = 2, /* seek to the first real entry */ + DIR_OFFSET_MIN = 3, /* lowest real offset value */ }; static void offset_set(struct dentry *dentry, long offset) @@ -267,7 +267,7 @@ void simple_offset_init(struct offset_ctx *octx) { mt_init_flags(&octx->mt, MT_FLAGS_ALLOC_RANGE); lockdep_set_class(&octx->mt.ma_lock, &simple_offset_lock_class); - octx->next_offset = DIR_OFFSET_MIN; + octx->next_offset = 0; } /** @@ -511,10 +511,30 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, LONG_MAX); } -static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) +static noinline_for_stack struct dentry *offset_dir_first(struct file *file) { + struct dentry *child, *found = NULL, *dir = file->f_path.dentry; + + spin_lock(&dir->d_lock); + child = d_first_child(dir); + if (child && simple_positive(child)) { + spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); + if (simple_positive(child)) + found = dget_dlock(child); + spin_unlock(&child->d_lock); + } + spin_unlock(&dir->d_lock); + return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup(struct file *file, loff_t offset) +{ + struct dentry *child, *found = NULL, *dir = file->f_path.dentry; + struct inode *inode = d_inode(dir); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + MA_STATE(mas, &octx->mt, offset, offset); - struct dentry *child, *found = NULL; rcu_read_lock(); child = mas_find(&mas, LONG_MAX); @@ -538,29 +558,62 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } +/* + * This is find_next_child() without the dput() tail. We might + * combine offset_dir_next() and find_next_child(). + */ +static struct dentry *offset_dir_next(struct dentry *dentry) +{ + struct dentry *parent = dentry->d_parent; + struct dentry *d, *found = NULL; + + spin_lock(&parent->d_lock); + d = d_next_sibling(dentry); + hlist_for_each_entry_from(d, d_sib) { + if (simple_positive(d)) { + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); + if (simple_positive(d)) + found = dget_dlock(d); + spin_unlock(&d->d_lock); + if (likely(found)) + break; + } + } + spin_unlock(&parent->d_lock); + return found; +} + static void offset_iterate_dir(struct file *file, struct dir_context *ctx) { - struct dentry *dir = file->f_path.dentry; - struct inode *inode = d_inode(dir); - struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); - struct dentry *dentry; + struct dentry *dentry, *next = NULL; + + if (ctx->pos == DIR_OFFSET_FIRST) + dentry = offset_dir_first(file); + else + dentry = offset_dir_lookup(file, ctx->pos); + if (!dentry) { + /* ->private_data is protected by f_pos_lock */ + offset_set_eod(file); + return; + } while (true) { - dentry = offset_find_next(octx, ctx->pos); - if (!dentry) { - /* ->private_data is protected by f_pos_lock */ - offset_set_eod(file); - return; - } - if (!offset_dir_emit(ctx, dentry)) { - dput(dentry); + ctx->pos = dentry2offset(dentry); + break; + } + + next = offset_dir_next(dentry); + if (!next) { + offset_set_eod(file); break; } - ctx->pos = dentry2offset(dentry) + 1; dput(dentry); + dentry = next; } + + dput(dentry); } /**