Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp205235rdb; Thu, 1 Feb 2024 06:25:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IH7c2cGb54opIgwIR8LNhCilYecmKsNU3u7KO5gT3MbDeWVzppxQNbmucn7NVY9avqEnIxj X-Received: by 2002:a0c:f0d4:0:b0:68c:81bd:74ca with SMTP id d20-20020a0cf0d4000000b0068c81bd74camr598492qvl.54.1706797500694; Thu, 01 Feb 2024 06:25:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706797500; cv=pass; d=google.com; s=arc-20160816; b=aCYZMlDU6/bMCTz7ZJxyNHbSH3LSjUAABPOw3gXKyJ7XXFzs76fBvkYuawY1U20KbM E1US+gbQEYeBaQJUhfACqvQ3kf45JMiOJOT6b7ZyrjGVqaXbpq5IVrGgYTnwIhp+ZoGx 3H/U/yuacfEKeJmnPywa2mDSnFFvDa1V2EsLjSQN/UWwjV8DDqqE4FcWGMUTVpJ/4tVY RPPUExEzw8r6gerEzHpyrb/UOzraSRXCyWkPNGg4lrbmRI2aQSHbZKW56021N1vKAs9q XCjm8kLQUb6YmF++5OIfkjrYubiZFzHPBDKjlN3wabPC+oxr+HyxkqT/MBAufKP7uIQd 7Skw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:user-agent:references:in-reply-to :message-id:date:cc:to:from:subject:dkim-signature; bh=NNLxgogOY4YPOGzraDQ63YBjQ+Ae6glHU1+2JovozQo=; fh=EcbNUcHSVAc9I4ZHR+9jpVr3jrjM1E7bX6hqZbF5aTA=; b=gUhL8BNyFXhkERcnVm5mRzw94NgxQifPW9v6avmnsDpQYIhJmekdOVfDT/6lNM7hLm XTEbiMgiewBpLzyduiCvVndXRK4W7JjVirpWSPJD8XZICEoVfXnE2K+W5KDp7k1pqrW0 w7dpK6LWqyzhL0vwpYPZqdF5KeadUg6lCExC1g2k7F4OqqumIn1Mgh3zBsKWj5xpaSdZ VW2vuzDPXbZ91/YqNh6SoAbvWMuCUMfkN8owiplsYqy04ZBOh7Bfe7HIi3/7kpQe7eGz baFFncywAmMQUdwXwWGVJDIJ92QYb9TFA8a9LOax0IjWs/tsX6dP7dxPjEXfjLdae9gr nHkg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=meGgpYrW; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-nfs+bounces-1697-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-nfs+bounces-1697-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=1; AJvYcCX6gv+pGTlDrvArGufleCwqF7kIn1XVN6msby6FolyT8r2oxhSnontCM5B/nb5uXRboqLKGxsqItcMlx5K8FqzpL7BpQeXoCMZCg8JR9Q== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p13-20020a05620a112d00b00783e2b9ab7csi12118308qkk.33.2024.02.01.06.25.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 06:25:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-nfs+bounces-1697-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=meGgpYrW; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-nfs+bounces-1697-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-nfs+bounces-1697-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 659C61C271B7 for ; Thu, 1 Feb 2024 14:25:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E3D075D49B; Thu, 1 Feb 2024 14:23:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="meGgpYrW" X-Original-To: linux-nfs@vger.kernel.org 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 BC9185D498; Thu, 1 Feb 2024 14:23:43 +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=1706797423; cv=none; b=Y3a6T8Np/KAjOEK6T7Gto3jUNuxn7iGEWgF2iiN+Qzpphg0VvsjaC/LeP/AVdpP+WOEcsYFUZS2XcrA0Dl6PlLXsLOKSPkdWzV9ywN2by0hs+ZZ3jW6o0BgJOKVionIdwA0uvbYsPCyI9wk5iUuj6kDohMOXwsNkrTWZgISOKrw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706797423; c=relaxed/simple; bh=pNUs+vJ9FD1IvCIwoyFyFu+KRz/FNWknrmHlaT5e3uA=; h=Subject:From:To:Cc:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Hbn6CeiIhffSKqN4gptqm6BjYIdqRn+OQ2siCeiZJfnOwLciIcPJulXIjeaKcJbVu5pKxGkKPYZjrF9XC0i/KIXIndpmePNFZlelv4RrtnPmoLY2g0o6T1Qse/XYalGPlekMvyp+b1duiEy/ebAV3WFmyuhQ4exMUuNtNyTQ0p8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=meGgpYrW; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 132E7C433F1; Thu, 1 Feb 2024 14:23:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706797423; bh=pNUs+vJ9FD1IvCIwoyFyFu+KRz/FNWknrmHlaT5e3uA=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=meGgpYrWx7o1I9nDq3y9YJmnCymkbm8kMpyA9jWcoZ0xa3NcaPrO0aPGFgUW7RIdp PKnF0BLNeHQRRbLO2/qjSWkARoF+4qTG4+phjMbqgwwASPE/TgctXRp9WyHcbgYdgc hHActmvEbcuk/BwN+4NWg+TKaIpYSByWvFUYJ3p8xBTCIAnGWB5x67QDDGAfa8j1o+ vRe5fVjs9euckI5G+ti2rcX7+Nx37HDOIT3ZeCnHb/3G2cNczD2AYvE8YwTiu6zkxU wQooLzSpMp8c8QmmTuy0F8CrsYC0wGqiftwqy0/5pb0kCeEGVOhU5kPGzxvkJGZs5Z LlHTei305KdpA== Subject: [PATCH 3/3] nfsd: fix RELEASE_LOCKOWNER From: Chuck Lever To: stable@vger.kernel.org Cc: linux-nfs@vger.kernel.org Date: Thu, 01 Feb 2024 09:23:42 -0500 Message-ID: <170679742213.14195.4225736612593673287.stgit@klimt.1015granger.net> In-Reply-To: <170679738225.14195.77163641928598673.stgit@klimt.1015granger.net> References: <170679738225.14195.77163641928598673.stgit@klimt.1015granger.net> User-Agent: StGit/1.5 Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit From: NeilBrown [ Upstream commit edcf9725150e42beeca42d085149f4c88fa97afd ] The test on so_count in nfsd4_release_lockowner() is nonsense and harmful. Revert to using check_for_locks(), changing that to not sleep. First: harmful. As is documented in the kdoc comment for nfsd4_release_lockowner(), the test on so_count can transiently return a false positive resulting in a return of NFS4ERR_LOCKS_HELD when in fact no locks are held. This is clearly a protocol violation and with the Linux NFS client it can cause incorrect behaviour. If RELEASE_LOCKOWNER is sent while some other thread is still processing a LOCK request which failed because, at the time that request was received, the given owner held a conflicting lock, then the nfsd thread processing that LOCK request can hold a reference (conflock) to the lock owner that causes nfsd4_release_lockowner() to return an incorrect error. The Linux NFS client ignores that NFS4ERR_LOCKS_HELD error because it never sends NFS4_RELEASE_LOCKOWNER without first releasing any locks, so it knows that the error is impossible. It assumes the lock owner was in fact released so it feels free to use the same lock owner identifier in some later locking request. When it does reuse a lock owner identifier for which a previous RELEASE failed, it will naturally use a lock_seqid of zero. However the server, which didn't release the lock owner, will expect a larger lock_seqid and so will respond with NFS4ERR_BAD_SEQID. So clearly it is harmful to allow a false positive, which testing so_count allows. The test is nonsense because ... well... it doesn't mean anything. so_count is the sum of three different counts. 1/ the set of states listed on so_stateids 2/ the set of active vfs locks owned by any of those states 3/ various transient counts such as for conflicting locks. When it is tested against '2' it is clear that one of these is the transient reference obtained by find_lockowner_str_locked(). It is not clear what the other one is expected to be. In practice, the count is often 2 because there is precisely one state on so_stateids. If there were more, this would fail. In my testing I see two circumstances when RELEASE_LOCKOWNER is called. In one case, CLOSE is called before RELEASE_LOCKOWNER. That results in all the lock states being removed, and so the lockowner being discarded (it is removed when there are no more references which usually happens when the lock state is discarded). When nfsd4_release_lockowner() finds that the lock owner doesn't exist, it returns success. The other case shows an so_count of '2' and precisely one state listed in so_stateid. It appears that the Linux client uses a separate lock owner for each file resulting in one lock state per lock owner, so this test on '2' is safe. For another client it might not be safe. So this patch changes check_for_locks() to use the (newish) find_any_file_locked() so that it doesn't take a reference on the nfs4_file and so never calls nfsd_file_put(), and so never sleeps. With this check is it safe to restore the use of check_for_locks() rather than testing so_count against the mysterious '2'. Fixes: ce3c4ad7f4ce ("NFSD: Fix possible sleep during nfsd4_release_lockowner()") Signed-off-by: NeilBrown Reviewed-by: Jeff Layton Cc: stable@vger.kernel.org # v6.2+ Signed-off-by: Chuck Lever --- fs/nfsd/nfs4state.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index b6480be7b5e6..16b073c63798 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -7080,14 +7080,16 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) { struct file_lock *fl; int status = false; - struct nfsd_file *nf = find_any_file(fp); + struct nfsd_file *nf; struct inode *inode; struct file_lock_context *flctx; + spin_lock(&fp->fi_lock); + nf = find_any_file_locked(fp); if (!nf) { /* Any valid lock stateid should have some sort of access */ WARN_ON_ONCE(1); - return status; + goto out; } inode = locks_inode(nf->nf_file); @@ -7103,7 +7105,8 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) } spin_unlock(&flctx->flc_lock); } - nfsd_file_put(nf); +out: + spin_unlock(&fp->fi_lock); return status; } @@ -7113,10 +7116,8 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) * @cstate: NFSv4 COMPOUND state * @u: RELEASE_LOCKOWNER arguments * - * The lockowner's so_count is bumped when a lock record is added - * or when copying a conflicting lock. The latter case is brief, - * but can lead to fleeting false positives when looking for - * locks-in-use. + * Check if theree are any locks still held and if not - free the lockowner + * and any lock state that is owned. * * Return values: * %nfs_ok: lockowner released or not found @@ -7152,10 +7153,13 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp, spin_unlock(&clp->cl_lock); return nfs_ok; } - if (atomic_read(&lo->lo_owner.so_count) != 2) { - spin_unlock(&clp->cl_lock); - nfs4_put_stateowner(&lo->lo_owner); - return nfserr_locks_held; + + list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) { + if (check_for_locks(stp->st_stid.sc_file, lo)) { + spin_unlock(&clp->cl_lock); + nfs4_put_stateowner(&lo->lo_owner); + return nfserr_locks_held; + } } unhash_lockowner_locked(lo); while (!list_empty(&lo->lo_owner.so_stateids)) {