Return-Path: Received: from mail-dm3nam03on0104.outbound.protection.outlook.com ([104.47.41.104]:64192 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933206AbeAXET2 (ORCPT ); Tue, 23 Jan 2018 23:19:28 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Trond Myklebust , "J . Bruce Fields" , Sasha Levin Subject: [PATCH AUTOSEL for 4.4 19/35] nfsd: Ensure we don't recognise lock stateids after freeing them Date: Wed, 24 Jan 2018 04:17:02 +0000 Message-ID: <20180124041645.32420-19-alexander.levin@microsoft.com> References: <20180124041645.32420-1-alexander.levin@microsoft.com> In-Reply-To: <20180124041645.32420-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Trond Myklebust [ Upstream commit 659aefb68eca28ba9aa482a9fc64de107332e256 ] In order to deal with lookup races, nfsd4_free_lock_stateid() needs to be able to signal to other stateful functions that the lock stateid is no longer valid. Right now, nfsd_lock() will check whether or not an existing stateid is still hashed, but only in the "new lock" path. To ensure the stateid invalidation is also recognised by the "existing lock= " path, and also by a second call to nfsd4_free_lock_stateid() itself, we can change the type to NFS4_CLOSED_STID under the stp->st_mutex. Signed-off-by: Trond Myklebust Signed-off-by: J. Bruce Fields Signed-off-by: Sasha Levin --- fs/nfsd/nfs4state.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index d31b6cdefb9d..439f01a0bc49 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -4934,7 +4934,9 @@ nfsd4_free_lock_stateid(stateid_t *stateid, struct nf= s4_stid *s) struct nfs4_ol_stateid *stp =3D openlockstateid(s); __be32 ret; =20 - mutex_lock(&stp->st_mutex); + ret =3D nfsd4_lock_ol_stateid(stp); + if (ret) + goto out_put_stid; =20 ret =3D check_stateid_generation(stateid, &s->sc_stateid, 1); if (ret) @@ -4945,11 +4947,13 @@ nfsd4_free_lock_stateid(stateid_t *stateid, struct = nfs4_stid *s) lockowner(stp->st_stateowner))) goto out; =20 + stp->st_stid.sc_type =3D NFS4_CLOSED_STID; release_lock_stateid(stp); ret =3D nfs_ok; =20 out: mutex_unlock(&stp->st_mutex); +out_put_stid: nfs4_put_stid(s); return ret; } @@ -5489,6 +5493,8 @@ find_lock_stateid(struct nfs4_lockowner *lo, struct n= fs4_file *fp) lockdep_assert_held(&clp->cl_lock); =20 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { + if (lst->st_stid.sc_type !=3D NFS4_LOCK_STID) + continue; if (lst->st_stid.sc_file =3D=3D fp) { atomic_inc(&lst->st_stid.sc_count); return lst; @@ -5563,7 +5569,6 @@ lookup_or_create_lock_state(struct nfsd4_compound_sta= te *cstate, struct nfs4_lockowner *lo; struct nfs4_ol_stateid *lst; unsigned int strhashval; - bool hashed; =20 lo =3D find_lockowner_str(cl, &lock->lk_new_owner); if (!lo) { @@ -5586,15 +5591,7 @@ retry: goto out; } =20 - mutex_lock(&lst->st_mutex); - - /* See if it's still hashed to avoid race with FREE_STATEID */ - spin_lock(&cl->cl_lock); - hashed =3D !list_empty(&lst->st_perfile); - spin_unlock(&cl->cl_lock); - - if (!hashed) { - mutex_unlock(&lst->st_mutex); + if (nfsd4_lock_ol_stateid(lst) !=3D nfs_ok) { nfs4_put_stid(&lst->st_stid); goto retry; } --=20 2.11.0