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;