From patchwork Mon Jun 30 15:49:13 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 4451511 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 863D8BEEAA for ; Mon, 30 Jun 2014 15:51:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 8DA57201C0 for ; Mon, 30 Jun 2014 15:51:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 99C812021A for ; Mon, 30 Jun 2014 15:51:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753226AbaF3Pvf (ORCPT ); Mon, 30 Jun 2014 11:51:35 -0400 Received: from mail-qc0-f175.google.com ([209.85.216.175]:37414 "EHLO mail-qc0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750897AbaF3Pvf (ORCPT ); Mon, 30 Jun 2014 11:51:35 -0400 Received: by mail-qc0-f175.google.com with SMTP id i8so7121679qcq.20 for ; Mon, 30 Jun 2014 08:51:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=Oeql1beSMf8XWsvjLextobnO49x9yaThMyTVO2jFsQo=; b=iCfG5mHDaO1eDkFKBZoPgOFzkj/7C93hvymsVSvChuqeSmudg7JGPLeT8G/mqnfF8+ IXBB4p0mak47Ho6PspgbIM6m2pxb+ii1TYuBQtPSK/BgXbV5ZyIs7ZKbfGBY4up/xuPO YHdqWwRq1w2CfUj0vio8kR/LQ85te3uLNt2aQCnOOM5c1nQDN7/LDP1CjIowZp+MYLFM v6Syc/BMwmzv7flIX6viAAwNbGDeHj4VxKMS6orh4MM2MPoIM/fNHH2Jygydu8+vXwS/ rTAWvKpwwXP+GVGAAOKKa7LZR/KMe/rVai2/U3MBo8gP3gvExMLXE4Sce9Z31WWI+tWD ZTjA== X-Gm-Message-State: ALoCoQkscMtnRiPeHxUtyLdio2ruQcqOpqoJUQa/bsNNSf8XIXjK5uLzjm9m5Clg+lY3iAaS1yQ1 X-Received: by 10.224.130.136 with SMTP id t8mr65278209qas.49.1404143494567; Mon, 30 Jun 2014 08:51:34 -0700 (PDT) Received: from tlielax.poochiereds.net ([2001:470:8:d63:3a60:77ff:fe93:a95d]) by mx.google.com with ESMTPSA id m1sm32584105qaz.27.2014.06.30.08.51.33 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Jun 2014 08:51:33 -0700 (PDT) From: Jeff Layton To: bfields@fieldses.org Cc: linux-nfs@vger.kernel.org Subject: [PATCH v3 044/114] nfsd: clean up races in lock stateid searching and creation Date: Mon, 30 Jun 2014 11:49:13 -0400 Message-Id: <1404143423-24381-45-git-send-email-jlayton@primarydata.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1404143423-24381-1-git-send-email-jlayton@primarydata.com> References: <1404143423-24381-1-git-send-email-jlayton@primarydata.com> Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Preparation for removal of the client_mutex. Currently, no lock aside from the client_mutex is held when calling find_lock_state. Ensure that the cl_lock is held by adding a lockdep assertion. Once we remove the client_mutex, it'll be possible for another thread to race in and insert a lock state for the same file after we search but before we insert a new one. Ensure that doesn't happen by redoing the search after allocating a new stid that we plan to insert. If one is found just put the one we just allocated. Signed-off-by: Jeff Layton --- fs/nfsd/nfs4state.c | 58 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 05d0fac2e50a..60cc162cc8a5 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -4652,16 +4652,14 @@ alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, str return lo; } -static struct nfs4_ol_stateid * -alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp) +static void +init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, + struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp) { - struct nfs4_openowner *oo = openowner(open_stp->st_stateowner); - struct nfs4_ol_stateid *stp; struct nfs4_client *clp = lo->lo_owner.so_client; - stp = nfs4_alloc_stateid(clp); - if (stp == NULL) - return NULL; + lockdep_assert_held(&clp->cl_lock); + stp->st_stid.sc_type = NFS4_LOCK_STID; stp->st_stateowner = &lo->lo_owner; get_nfs4_file(fp); @@ -4670,20 +4668,20 @@ alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct stp->st_access_bmap = 0; stp->st_deny_bmap = open_stp->st_deny_bmap; stp->st_openstp = open_stp; - spin_lock(&oo->oo_owner.so_client->cl_lock); list_add(&stp->st_locks, &open_stp->st_locks); list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); spin_lock(&fp->fi_lock); list_add(&stp->st_perfile, &fp->fi_stateids); spin_unlock(&fp->fi_lock); - spin_unlock(&oo->oo_owner.so_client->cl_lock); - return stp; } static struct nfs4_ol_stateid * find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) { struct nfs4_ol_stateid *lst; + struct nfs4_client *clp = lo->lo_owner.so_client; + + lockdep_assert_held(&clp->cl_lock); list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { if (lst->st_stid.sc_file == fp) @@ -4692,6 +4690,36 @@ find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) return NULL; } +static struct nfs4_ol_stateid * +find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, + struct nfs4_ol_stateid *ost, bool *new) +{ + struct nfs4_ol_stateid *lst, *nst = NULL; + struct nfs4_openowner *oo = openowner(ost->st_stateowner); + struct nfs4_client *clp = oo->oo_owner.so_client; + + spin_lock(&clp->cl_lock); + lst = find_lock_stateid(lo, fi); + if (lst == NULL) { + spin_unlock(&clp->cl_lock); + nst = nfs4_alloc_stateid(clp); + if (nst == NULL) + return NULL; + + spin_lock(&clp->cl_lock); + lst = find_lock_stateid(lo, fi); + if (likely(!lst)) { + init_lock_stateid(nst, lo, fi, ost); + lst = nst; + nst = NULL; + *new = true; + } + } + spin_unlock(&clp->cl_lock); + if (nst) + put_generic_stateid(nst); + return lst; +} static int check_lock_length(u64 offset, u64 length) @@ -4739,14 +4767,10 @@ static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, s return nfserr_bad_seqid; } - *lst = find_lock_stateid(lo, fi); + *lst = find_or_create_lock_stateid(lo, fi, ost, new); if (*lst == NULL) { - *lst = alloc_init_lock_stateid(lo, fi, ost); - if (*lst == NULL) { - release_lockowner_if_empty(lo); - return nfserr_jukebox; - } - *new = true; + release_lockowner_if_empty(lo); + return nfserr_jukebox; } return nfs_ok; }