Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp842761rdh; Thu, 23 Nov 2023 22:05:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IG0c6hXd5KzP5BQB66bB2zwtuBHOZXY5+5C0Mc6ctG+Ff1Q7wPwcWiR36FJyjBn3G00ZX1s X-Received: by 2002:a05:6602:3356:b0:7a5:a391:73ae with SMTP id c22-20020a056602335600b007a5a39173aemr2111244ioz.17.1700805924276; Thu, 23 Nov 2023 22:05:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700805924; cv=none; d=google.com; s=arc-20160816; b=dPHgtn47KQ/ZuzIHVbeI1ncsmzR58HBizUOk9UkuzISWl/CoohPvUUapP60xgXX9HO DCdqbY+9gJ50zOqWsq2PTs0WClTiadPDEdvmKPU4yzitZDmaQyo+ZMbe5ngalHsOfdmV gl5NV4BAjmbjo5KbMATrrQmA1RtFpgu/5R4ClCS2/xxixAp92ah55ZmeG7KoLXhTo5A8 eTgyYqVl23fRk7g66alq/p0H2afhwBsOoRsv2ONmkVicFgO0fgfvddhMpl2qoymstLKW JtHTbexcFEPhJMRnjMV9xwgnGYf2BW8rp24ZUiY3A/EKaz/oCCIQdfXLqGKWJD2cPPS1 qozw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=RiZ+r8FbLExsvm//mT+YJVahGYKvclc7LYL6er4dEWE=; fh=vDsv/0Rb6aybIi/Xas2DR+uQpFXMLtn4no3J1t38V0s=; b=xFWMQXR8Ojps2ZWoAianQyT39LG9yp7moBpdgzCOZmj2vEPbhTWCCPzhO8ZKU+23MI 1tfVNoqT2sWXoVnh/4d+CuKptCEhsXmcDdHwo+GD3xsZFlqfa8qkRM9w39ryWR/MpaSd sWQyAl+E50viCjHUyJqa+nL0VqswCwTpkkReAnAIfBFA9svkfYy2f0sCyzdQSRotbp5x HlxwvFkUjqRTC1uXvUuEBt4sssGvEZd2O6Yj2pvMTmb8M0HS4LiXjTCYSKseS5y582ea xmkAUOf/N5eWTnpzhjVcUIVtlTjz8Mxz1pomLmDePqomeg3f0wMHsq51TLnwiDhAZu7B ydcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=Y4JKQzTk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id h1-20020a63c001000000b00565db2812a0si2958177pgg.60.2023.11.23.22.05.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 22:05:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b=Y4JKQzTk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id A6F74805DC5D; Thu, 23 Nov 2023 22:05:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231157AbjKXGEu (ORCPT + 99 others); Fri, 24 Nov 2023 01:04:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231255AbjKXGES (ORCPT ); Fri, 24 Nov 2023 01:04:18 -0500 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9C0810C7; Thu, 23 Nov 2023 22:04:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=RiZ+r8FbLExsvm//mT+YJVahGYKvclc7LYL6er4dEWE=; b=Y4JKQzTktLvOt/kY8qX7d2wYpF aPCfYY2W8xTpb7JRHL6ntOWeKF/D/j0gSif4/M30412UYwjBIg1RPtrvHTYF85pqQcRdbgvEskJuV R9t1vO0kt5Z8rrRQzMWi5SK/2EijTjDh0jAZOO3tVe7wL/v7JfrDgKnnQfsRQmqjsrZDaq+U49o6Q uHbh/VbtfF8Wi/e9pZ2WN06uytmlmMSY6WCagghs5mlZaTkJxa01na7UuzwkfEHZ3bruZF+U6Vb7f jgOhrZqdE/JlnTF+ZWuhB/ACyv1eURGEz/KzoNWpDUaPj6KsgC0MysZHtulE0+AWYsOadTAFdBsGW xAJihuKA==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1r6PId-002Pu3-1F; Fri, 24 Nov 2023 06:04:23 +0000 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , Christian Brauner , linux-kernel@vger.kernel.org Subject: [PATCH v3 07/21] fast_dput(): handle underflows gracefully Date: Fri, 24 Nov 2023 06:04:08 +0000 Message-Id: <20231124060422.576198-7-viro@zeniv.linux.org.uk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231124060422.576198-1-viro@zeniv.linux.org.uk> References: <20231124060200.GR38156@ZenIV> <20231124060422.576198-1-viro@zeniv.linux.org.uk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: Al Viro X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Thu, 23 Nov 2023 22:05:18 -0800 (PST) If refcount is less than 1, we should just warn, unlock dentry and return true, so that the caller doesn't try to do anything else. Taking care of that leaves the rest of "lockref_put_return() has failed" case equivalent to "decrement refcount and rejoin the normal slow path after the point where we grab ->d_lock". NOTE: lockref_put_return() is strictly a fastpath thing - unlike the rest of lockref primitives, it does not contain a fallback. Caller (and it looks like fast_dput() is the only legitimate one in the entire kernel) has to do that itself. Reasons for lockref_put_return() failures: * ->d_lock held by somebody * refcount <= 0 * ... or an architecture not supporting lockref use of cmpxchg - sparc, anything non-SMP, config with spinlock debugging... We could add a fallback, but it would be a clumsy API - we'd have to distinguish between: (1) refcount > 1 - decremented, lock not held on return (2) refcount < 1 - left alone, probably no sense to hold the lock (3) refcount is 1, no cmphxcg - decremented, lock held on return (4) refcount is 1, cmphxcg supported - decremented, lock *NOT* held on return. We want to return with no lock held in case (4); that's the whole point of that thing. We very much do not want to have the fallback in case (3) return without a lock, since the caller might have to retake it in that case. So it wouldn't be more convenient than doing the fallback in the caller and it would be very easy to screw up, especially since the test coverage would suck - no way to test (3) and (4) on the same kernel build. Reviewed-by: Christian Brauner Signed-off-by: Al Viro --- fs/dcache.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index 00c19041adf3..9edabc7e2e64 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -779,12 +779,12 @@ static inline bool fast_dput(struct dentry *dentry) */ if (unlikely(ret < 0)) { spin_lock(&dentry->d_lock); - if (dentry->d_lockref.count > 1) { - dentry->d_lockref.count--; + if (WARN_ON_ONCE(dentry->d_lockref.count <= 0)) { spin_unlock(&dentry->d_lock); return true; } - return false; + dentry->d_lockref.count--; + goto locked; } /* @@ -842,6 +842,7 @@ static inline bool fast_dput(struct dentry *dentry) * else could have killed it and marked it dead. Either way, we * don't need to do anything else. */ +locked: if (dentry->d_lockref.count) { spin_unlock(&dentry->d_lock); return true; -- 2.39.2