From patchwork Mon Nov 20 21:41:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Scott Mayhew X-Patchwork-Id: 10067365 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8E504602B7 for ; Mon, 20 Nov 2017 21:41:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F4A529356 for ; Mon, 20 Nov 2017 21:41:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7155F293C0; Mon, 20 Nov 2017 21:41:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0957F29356 for ; Mon, 20 Nov 2017 21:41:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752904AbdKTVlT (ORCPT ); Mon, 20 Nov 2017 16:41:19 -0500 Received: from mx1.redhat.com ([209.132.183.28]:58564 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752816AbdKTVlS (ORCPT ); Mon, 20 Nov 2017 16:41:18 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C8106C0587F3; Mon, 20 Nov 2017 21:41:18 +0000 (UTC) Received: from coeurl.usersys.redhat.com (ovpn-123-196.rdu2.redhat.com [10.10.123.196]) by smtp.corp.redhat.com (Postfix) with ESMTP id 982B3620A2; Mon, 20 Nov 2017 21:41:18 +0000 (UTC) Received: by coeurl.usersys.redhat.com (Postfix, from userid 1000) id 3D49020422; Mon, 20 Nov 2017 16:41:18 -0500 (EST) From: Scott Mayhew To: trond.myklebust@primarydata.com, anna.schumaker@netapp.com Cc: linux-nfs@vger.kernel.org Subject: [PATCH] nfs: fix a deadlock in nfs client initialization Date: Mon, 20 Nov 2017 16:41:18 -0500 Message-Id: <20171120214118.4240-1-smayhew@redhat.com> In-Reply-To: <20171120212819.3yxutvgmigxc7at5@tonberry.usersys.redhat.com> References: <20171120212819.3yxutvgmigxc7at5@tonberry.usersys.redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 20 Nov 2017 21:41:18 +0000 (UTC) Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The following deadlock can occur between a process waiting for a client to initialize in while walking the client list and another process waiting for the nfs_clid_init_mutex so it can initialize that client: Process 1 Process 2 --------- --------- spin_lock(&nn->nfs_client_lock); list_add_tail(&CLIENTA->cl_share_link, &nn->nfs_client_list); spin_unlock(&nn->nfs_client_lock); spin_lock(&nn->nfs_client_lock); list_add_tail(&CLIENTB->cl_share_link, &nn->nfs_client_list); spin_unlock(&nn->nfs_client_lock); mutex_lock(&nfs_clid_init_mutex); nfs41_walk_client_list(clp, result, cred); nfs_wait_client_init_complete(CLIENTA); (waiting for nfs_clid_init_mutex) Add and initilize the client with the nfs_clid_init_mutex held in order to prevent that deadlock. Signed-off-by: Scott Mayhew --- fs/nfs/client.c | 21 +++++++++++++++++++-- fs/nfs/nfs4state.c | 4 ---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/fs/nfs/client.c b/fs/nfs/client.c index 0ac2fb1..db38c47 100644 --- a/fs/nfs/client.c +++ b/fs/nfs/client.c @@ -60,6 +60,7 @@ static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq); static DEFINE_SPINLOCK(nfs_version_lock); static DEFINE_MUTEX(nfs_version_mutex); static LIST_HEAD(nfs_versions); +static DEFINE_MUTEX(nfs_clid_init_mutex); /* * RPC cruft for NFS @@ -386,7 +387,7 @@ nfs_found_client(const struct nfs_client_initdata *cl_init, */ struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) { - struct nfs_client *clp, *new = NULL; + struct nfs_client *clp, *new = NULL, *result = NULL; struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id); const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops; @@ -407,11 +408,27 @@ struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) return nfs_found_client(cl_init, clp); } if (new) { + /* add and initialize the client with the + * nfs_clid_init_mutex held to prevent a deadlock + * with the server trunking detection + */ + spin_unlock(&nn->nfs_client_lock); + mutex_lock(&nfs_clid_init_mutex); + spin_lock(&nn->nfs_client_lock); + clp = nfs_match_client(cl_init); + if (clp) { + spin_unlock(&nn->nfs_client_lock); + mutex_unlock(&nfs_clid_init_mutex); + new->rpc_ops->free_client(new); + return nfs_found_client(cl_init, clp); + } list_add_tail(&new->cl_share_link, &nn->nfs_client_list); spin_unlock(&nn->nfs_client_lock); new->cl_flags = cl_init->init_flags; - return rpc_ops->init_client(new, cl_init); + result = rpc_ops->init_client(new, cl_init); + mutex_unlock(&nfs_clid_init_mutex); + return result; } spin_unlock(&nn->nfs_client_lock); diff --git a/fs/nfs/nfs4state.c b/fs/nfs/nfs4state.c index 54fd56d..668164e 100644 --- a/fs/nfs/nfs4state.c +++ b/fs/nfs/nfs4state.c @@ -77,8 +77,6 @@ const nfs4_stateid invalid_stateid = { .type = NFS4_INVALID_STATEID_TYPE, }; -static DEFINE_MUTEX(nfs_clid_init_mutex); - int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred) { struct nfs4_setclientid_res clid = { @@ -2164,7 +2162,6 @@ int nfs4_discover_server_trunking(struct nfs_client *clp, clnt = clp->cl_rpcclient; i = 0; - mutex_lock(&nfs_clid_init_mutex); again: status = -ENOENT; cred = nfs4_get_clid_cred(clp); @@ -2232,7 +2229,6 @@ int nfs4_discover_server_trunking(struct nfs_client *clp, } out_unlock: - mutex_unlock(&nfs_clid_init_mutex); dprintk("NFS: %s: status = %d\n", __func__, status); return status; }