Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1076494ybj; Thu, 7 May 2020 14:20:24 -0700 (PDT) X-Google-Smtp-Source: APiQypLuNuQFpyii93Yq1s2wmWytiW+j8wl24Kohf7fk5epeDuS7U3avEdkZQtKlflIU1R9hXmaJ X-Received: by 2002:a50:b285:: with SMTP id p5mr14344351edd.150.1588886423907; Thu, 07 May 2020 14:20:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588886423; cv=none; d=google.com; s=arc-20160816; b=oRdJoO/EDU+2tFbfvdn1Zu5ITh4XK4PliKAau+PAc092yzBjsN0oJ8tV/xRPxt18Ik XA93Evz8pnTPwGrP92BxYd+nwqsxI9QDI+Kt+nftSBSPjEtfpn5Dh1FzFGgBwVyoOxKE pA307pqrBGk0rTQ/MPI1jdgK/BQuhBjJ9hFf/L1obn4q6LnRKK0+LtP1Nv6xKYB66V57 K1f9QT1fJHo/BcgZTKBeZisygK+jDd1LnzhbIGGH6CAXqDZySczAYvUhExHGzTeOUtUM R6HRmxh+HFJpxsV/A2bMx9Bfm7PvSHhB26yhfODMvFnXN1Zyhs2YHEydhH995E18MBX0 2rgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:references :in-reply-to:subject:cc:date:to:from; bh=0HJjrQ91nFDW/dxO4TCMD+B5+aBrkipVS5+dV9aBKyc=; b=R2M+MjlMjzj/6npFu/Vd6kxU4nx0ucLTx59FZKHmLdIcTye8vx5DrqPBTNeJdo1kmd pe3nmPArITV3bvKOQqHyUw5meGuAOxNMNAnMowBKCeN4+8fC8g14YocXpBI0nW5IMdBy B1gkc0V9aTA1YcHSvo/BRf81rXGfxSwevEjzd03LGNA/T+KKyuFoT2a/Whc67qptNjib BJndQZ3rlnmyqLjQQ9UGWtv7NX1UOOw8M+iWFcOA4rCv+yAUlVojX+bES13qLkVyZjNL 1nshUJUd1x3EmlpikFuM+RM4U/sHJKAvlogne5Q1FVlr6JzG+tB5s2OEnobRvVV7Kqti 2ZSQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id eb5si4800865edb.145.2020.05.07.14.19.41; Thu, 07 May 2020 14:20:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726470AbgEGVTD (ORCPT + 99 others); Thu, 7 May 2020 17:19:03 -0400 Received: from mx2.suse.de ([195.135.220.15]:50014 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726218AbgEGVTD (ORCPT ); Thu, 7 May 2020 17:19:03 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 4D2F5ACFE; Thu, 7 May 2020 21:19:03 +0000 (UTC) From: NeilBrown To: Sasha Levin , linux-kernel@vger.kernel.org, stable@vger.kernel.org Date: Fri, 08 May 2020 07:18:53 +1000 Cc: Trond Myklebust , Sasha Levin , linux-nfs@vger.kernel.org, netdev@vger.kernel.org Subject: Re: [PATCH AUTOSEL 5.4 26/35] SUNRPC: defer slow parts of rpc_free_client() to a workqueue. In-Reply-To: <20200507142830.26239-26-sashal@kernel.org> References: <20200507142830.26239-1-sashal@kernel.org> <20200507142830.26239-26-sashal@kernel.org> Message-ID: <878si3cuki.fsf@notabene.neil.brown.name> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Thu, May 07 2020, Sasha Levin wrote: > From: NeilBrown > > [ Upstream commit 7c4310ff56422ea43418305d22bbc5fe19150ec4 ] This one is buggy - it introduces a use-after-free. Best delay it for now. NeilBrown > > The rpciod workqueue is on the write-out path for freeing dirty memory, > so it is important that it never block waiting for memory to be > allocated - this can lead to a deadlock. > > rpc_execute() - which is often called by an rpciod work item - calls > rcp_task_release_client() which can lead to rpc_free_client(). > > rpc_free_client() makes two calls which could potentially block wating > for memory allocation. > > rpc_clnt_debugfs_unregister() calls into debugfs and will block while > any of the debugfs files are being accessed. In particular it can block > while any of the 'open' methods are being called and all of these use > malloc for one thing or another. So this can deadlock if the memory > allocation waits for NFS to complete some writes via rpciod. > > rpc_clnt_remove_pipedir() can take the inode_lock() and while it isn't > obvious that memory allocations can happen while the lock it held, it is > safer to assume they might and to not let rpciod call > rpc_clnt_remove_pipedir(). > > So this patch moves these two calls (together with the final kfree() and > rpciod_down()) into a work-item to be run from the system work-queue. > rpciod can continue its important work, and the final stages of the free > can happen whenever they happen. > > I have seen this deadlock on a 4.12 based kernel where debugfs used > synchronize_srcu() when removing objects. synchronize_srcu() requires a > workqueue and there were no free workther threads and none could be > allocated. While debugsfs no longer uses SRCU, I believe the deadlock > is still possible. > > Signed-off-by: NeilBrown > Signed-off-by: Trond Myklebust > Signed-off-by: Sasha Levin > --- > include/linux/sunrpc/clnt.h | 8 +++++++- > net/sunrpc/clnt.c | 21 +++++++++++++++++---- > 2 files changed, 24 insertions(+), 5 deletions(-) > > diff --git a/include/linux/sunrpc/clnt.h b/include/linux/sunrpc/clnt.h > index abc63bd1be2b5..d99d39d45a494 100644 > --- a/include/linux/sunrpc/clnt.h > +++ b/include/linux/sunrpc/clnt.h > @@ -71,7 +71,13 @@ struct rpc_clnt { > #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) > struct dentry *cl_debugfs; /* debugfs directory */ > #endif > - struct rpc_xprt_iter cl_xpi; > + /* cl_work is only needed after cl_xpi is no longer used, > + * and that are of similar size > + */ > + union { > + struct rpc_xprt_iter cl_xpi; > + struct work_struct cl_work; > + }; > const struct cred *cl_cred; > }; >=20=20 > diff --git a/net/sunrpc/clnt.c b/net/sunrpc/clnt.c > index f7f78566be463..a7430b66c7389 100644 > --- a/net/sunrpc/clnt.c > +++ b/net/sunrpc/clnt.c > @@ -877,6 +877,20 @@ EXPORT_SYMBOL_GPL(rpc_shutdown_client); > /* > * Free an RPC client > */ > +static void rpc_free_client_work(struct work_struct *work) > +{ > + struct rpc_clnt *clnt =3D container_of(work, struct rpc_clnt, cl_work); > + > + /* These might block on processes that might allocate memory, > + * so they cannot be called in rpciod, so they are handled separately > + * here. > + */ > + rpc_clnt_debugfs_unregister(clnt); > + rpc_clnt_remove_pipedir(clnt); > + > + kfree(clnt); > + rpciod_down(); > +} > static struct rpc_clnt * > rpc_free_client(struct rpc_clnt *clnt) > { > @@ -887,17 +901,16 @@ rpc_free_client(struct rpc_clnt *clnt) > rcu_dereference(clnt->cl_xprt)->servername); > if (clnt->cl_parent !=3D clnt) > parent =3D clnt->cl_parent; > - rpc_clnt_debugfs_unregister(clnt); > - rpc_clnt_remove_pipedir(clnt); > rpc_unregister_client(clnt); > rpc_free_iostats(clnt->cl_metrics); > clnt->cl_metrics =3D NULL; > xprt_put(rcu_dereference_raw(clnt->cl_xprt)); > xprt_iter_destroy(&clnt->cl_xpi); > - rpciod_down(); > put_cred(clnt->cl_cred); > rpc_free_clid(clnt); > - kfree(clnt); > + > + INIT_WORK(&clnt->cl_work, rpc_free_client_work); > + schedule_work(&clnt->cl_work); > return parent; > } >=20=20 > --=20 > 2.20.1 --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEG8Yp69OQ2HB7X0l6Oeye3VZigbkFAl60ez0ACgkQOeye3VZi gbkTlxAAhHKw2SYW5PnmU2uD/cyIddjxyuhSkumoo6COwdVY0602KufdxCiP5mfe vSOjHjYICTnZMcbaSYEd3PHZEzezAh3DZggn7sXc+8I3cJb1JU2EcZeVArClZv0n b/5jvqW525cmCpg1XIUGwbOZcgMsSVE4N6FmfMD/JMNrp3pA/k4NLvyS//Qd+/8P OG+TPIljq8lermJmhVyckBVSBojXtUuEkR9kG0+o87JJwR8JaeIKeR+CFTKuIMub E9ql7aDYQrPQzfKiiE4jPNSl1cqO5qQUkuEIsc/ziKHdRJR1E8txD2IvKsL6zL2y yV9ptqTmDT+o7M2qf7irjxamriaPM5xUMPibHUSOhdVILPY6strqeWSRqblf8C+D 9DLpk3KM0t9oeQwiG2ODLImkzZJ3SdXXKH2oL0HwFdl/GYqb6pY5xbSF2QbX6uda +52AZka4B4TxIwe+SBi5W0jh6wrrJqWL02djWQWLFT2WXwVgh2gSgstUwD1+y0hg BvAcs8Zj+RXFq1/yUz5JSQ6EbjQaMSXD7hZ6ponFXLlODy8YkesWvsHLJVDoFu+v 3okAx1WOUgSqujM/qeWzrYKYEYopi14fhlJeN7qY2vaSKCAJUGxChMC314nGLHrH unLW37ThQF4XTp9LhwgaIj9LVNhzkWHPlN5L88ff5Ai/q5XhkIk= =cDQC -----END PGP SIGNATURE----- --=-=-=--