Received: by 2002:a05:7412:7c14:b0:fa:6e18:a558 with SMTP id ii20csp102620rdb; Sun, 21 Jan 2024 19:58:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDGqqt42QeY0VwXzyvFi3Mp9XEhuaDXWIi6QWLBnNQKfmUKuiQpDVCkEIn0V4GabZWMrdt X-Received: by 2002:a05:6512:3f0f:b0:50e:7124:8953 with SMTP id y15-20020a0565123f0f00b0050e71248953mr1651468lfa.26.1705895914353; Sun, 21 Jan 2024 19:58:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705895914; cv=pass; d=google.com; s=arc-20160816; b=NBISHC5xy1mzJ3lPxcqSVPEMmpfGxhnTx4E7LXk5KVk2JXkKBARyTrze0mFLNFYJAa sW7MFDQ/NSLcFToA3zN1NvZwuDOSxmk/3dWyraY9aQr1HIN98NFI8PzKqs+gREktemk1 J6jCrjb4t+sQR/8CZkt8gT92zKyYRe1X34XY8KXU9AcRAMjEyhosBqMD+cKysXDSV7PS SCkZK/abGDp2HRVNip910rb+GU5gRMVkM65TZgwEs0jv4y7WcmiwicmAqNejf0nviAGc /9EhCnIBgP1GH3tJRDi8LIIPIPjxQzfZqrC76PYAolniv3BbaZ4EUwch4Y/zlS8BwNln S8Pw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:content-transfer-encoding :dkim-signature:dkim-signature:dkim-signature:dkim-signature; bh=LJjgbA7NEQUmc62GSVGQ4XFXaBItMBcdzRj4fr3mKXk=; fh=ShCLilEAaluQixciVEZPFps9z1VsJyAgaj5yGa5i8wk=; b=h7ZepaG/V98/H4iLYRlhN7FUtnZsoOvs7zZju3xWIrG2ky7QVxn3ZNuWy8ZVYD3AkG PW77MOCpknmx1TIA8V0h+o6hmkoDVYED1pFoGZ/qaPh5fBf3tkZ1fRD6k2JVVklMygY+ Pd+aBnx+8U/s2ga89NFGbEsnoPqhSpy9OasP9FOAnsFrYDrvQgHjF0C/ur15la8NbLGg WimZ/kSY9u3eZLY5ek0WuRCLmK23W3zjtT6siLkwwodLTJbueXnjlR2t063HilKPCG7X VM/a0GjdzDPu6hqFyXvWUlz32xZXZ3DpYBrTyyWDKwil2eUdXhFv6lv9YuKSMqwfNdch Tgdw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=LvGLRSAw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=LvGLRSAw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-nfs+bounces-1215-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-nfs+bounces-1215-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id k13-20020a170906128d00b00a234b6bc08csi10159482ejb.262.2024.01.21.19.58.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 19:58:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-nfs+bounces-1215-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=LvGLRSAw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=LvGLRSAw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-nfs+bounces-1215-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-nfs+bounces-1215-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de 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 am.mirrors.kernel.org (Postfix) with ESMTPS id E417C1F22EBB for ; Mon, 22 Jan 2024 03:58:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D03E1865; Mon, 22 Jan 2024 03:58:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="LvGLRSAw"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Ebh8Vqoj"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="LvGLRSAw"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Ebh8Vqoj" X-Original-To: linux-nfs@vger.kernel.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFAB91849 for ; Mon, 22 Jan 2024 03:58:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705895910; cv=none; b=Yzl17sSE9S5GSsZq8OUG2VRljrPfYbMTfDbIclq2hU6/i5DC5IhnnmEfWU1jXBrIw5untcrD9fnM5PD0o7gb1jAI36KzB4WXbJK1MysL1RYMSWBqZ9kQeJKw0P7Dt21Au1QmdtvEsrWCXNwXLfrIenEDdu3KGrGaStvfUG7nf60= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705895910; c=relaxed/simple; bh=Q1iH+/WY1WrUjBV7PWDX0CwRfzkVyhfmLAP1MgP2+xw=; h=Content-Type:MIME-Version:From:To:Cc:Subject:Date:Message-id; b=V2en3SiSBRAe0HP9LuOzDLBpjnzkNqREs5Hcx3f+VefRF5Qa0XdE6Rp0vYJ5nDfd9caDCtoD7HYjZYzQkwzN3JTZTWk+m/F+9fFBJv3aYeKSwplYr16B7j62Ch14Vd3su1LRu9+Ty8lcX5aCG8gR6anqxXWTob1BdwT8AfxM3Kk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=LvGLRSAw; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=Ebh8Vqoj; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=LvGLRSAw; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=Ebh8Vqoj; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 5D34822130; Mon, 22 Jan 2024 03:58:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1705895901; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LJjgbA7NEQUmc62GSVGQ4XFXaBItMBcdzRj4fr3mKXk=; b=LvGLRSAwL5FVg0kVN+YJckjORv7OG1vj4tmJ3zNrb6MPPWrlmCY+Uqov413MGdnpkx+6kj 9tZkrdBpevOPyvnBO1AKalhWWE2ukmputxKMSw0agNoOSVFTCf1WH7FKdgZItX70fBZkN6 ivdk4J5xpmQZhP/sFjtsOisnPOFuwiE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1705895901; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LJjgbA7NEQUmc62GSVGQ4XFXaBItMBcdzRj4fr3mKXk=; b=Ebh8VqojWl6C26eBug4O//95VA9oGUHWIoKjbmtoetUcMnLmCGLKOUG+FfB5Wrzu6t2lPa e04ajKXx6qDynmBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1705895901; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LJjgbA7NEQUmc62GSVGQ4XFXaBItMBcdzRj4fr3mKXk=; b=LvGLRSAwL5FVg0kVN+YJckjORv7OG1vj4tmJ3zNrb6MPPWrlmCY+Uqov413MGdnpkx+6kj 9tZkrdBpevOPyvnBO1AKalhWWE2ukmputxKMSw0agNoOSVFTCf1WH7FKdgZItX70fBZkN6 ivdk4J5xpmQZhP/sFjtsOisnPOFuwiE= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1705895901; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LJjgbA7NEQUmc62GSVGQ4XFXaBItMBcdzRj4fr3mKXk=; b=Ebh8VqojWl6C26eBug4O//95VA9oGUHWIoKjbmtoetUcMnLmCGLKOUG+FfB5Wrzu6t2lPa e04ajKXx6qDynmBg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 543FB137FD; Mon, 22 Jan 2024 03:58:19 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap1.dmz-prg2.suse.org with ESMTPSA id BVnPAtvnrWVXGwAAD6G6ig (envelope-from ); Mon, 22 Jan 2024 03:58:19 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: "NeilBrown" To: Chuck Lever , Jeff Layton , Dai Ngo , Olga Kornievskaia , Tom Talpey Cc: linux-nfs@vger.kernel.org Subject: [PATCH] nfsd: fix RELEASE_LOCKOWNER Date: Mon, 22 Jan 2024 14:58:16 +1100 Message-id: <170589589641.23031.16356786177193106749@noble.neil.brown.name> Authentication-Results: smtp-out1.suse.de; none X-Spamd-Result: default: False [-3.10 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; RCPT_COUNT_FIVE(0.00)[6]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: X-Spam-Flag: NO X-Spam-Score: -3.10 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 NFS4_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. It 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 --- 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 2fa54cfd4882..6dc6340e2852 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -7911,14 +7911,16 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_loc= kowner *lowner) { struct file_lock *fl; int status =3D false; - struct nfsd_file *nf =3D find_any_file(fp); + struct nfsd_file *nf; struct inode *inode; struct file_lock_context *flctx; =20 + spin_lock(&fp->fi_lock); + nf =3D 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; } =20 inode =3D file_inode(nf->nf_file); @@ -7934,7 +7936,8 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_locko= wner *lowner) } spin_unlock(&flctx->flc_lock); } - nfsd_file_put(nf); +out: + spin_unlock(&fp->fi_lock); return status; } =20 @@ -7944,10 +7947,8 @@ check_for_locks(struct nfs4_file *fp, struct nfs4_lock= owner *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 @@ -7983,10 +7984,13 @@ nfsd4_release_lockowner(struct svc_rqst *rqstp, spin_unlock(&clp->cl_lock); return nfs_ok; } - if (atomic_read(&lo->lo_owner.so_count) !=3D 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)) { --=20 2.43.0