From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7342BC433EF for ; Tue, 22 Mar 2022 10:37:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E1636B0072; Tue, 22 Mar 2022 06:37:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 890EC6B0073; Tue, 22 Mar 2022 06:37:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 731446B0074; Tue, 22 Mar 2022 06:37:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0025.hostedemail.com [216.40.44.25]) by kanga.kvack.org (Postfix) with ESMTP id 613B16B0072 for ; Tue, 22 Mar 2022 06:37:48 -0400 (EDT) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 08A631828AE21 for ; Tue, 22 Mar 2022 10:37:48 +0000 (UTC) X-FDA: 79271671416.17.E7EE6F9 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by imf09.hostedemail.com (Postfix) with ESMTP id 60D75140022 for ; Tue, 22 Mar 2022 10:37:47 +0000 (UTC) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id EA2ED1F37C; Tue, 22 Mar 2022 10:37:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1647945465; 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: in-reply-to:in-reply-to:references:references; bh=gmbUswLJoIAaiGeCon8zxgA+Ily6/JQQW3FtwFLIyVQ=; b=EGp9LJf8pvt0tM2iT2vBf2GUcktImAOaYYMJUqQbOHTO2xyfWiL2prt8XyKhUqxj1IVMA3 atpJ3+sDilSPm2Iv6wBQgLsTmIL/9WrHCOgFRVVdo4eWmdx00dx/mNHAjLAOrPIN3UCCEd jb1VJIhhYVH15JsKUO+kzs9yrh23s8M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1647945465; 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: in-reply-to:in-reply-to:references:references; bh=gmbUswLJoIAaiGeCon8zxgA+Ily6/JQQW3FtwFLIyVQ=; b=3JVJ1Yl6N+w3j/zqml7u8u8upXTbA9FT34zEjq/Hbk9pt/pQgnT8THFshn3Kl0VPkOF9N7 X08AOQiR+as64HDA== Received: from quack3.suse.cz (unknown [10.100.224.230]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 6A07FA3B81; Tue, 22 Mar 2022 10:37:45 +0000 (UTC) Received: by quack3.suse.cz (Postfix, from userid 1000) id 1829EA0610; Tue, 22 Mar 2022 11:37:44 +0100 (CET) Date: Tue, 22 Mar 2022 11:37:44 +0100 From: Jan Kara To: Trond Myklebust Cc: "jack@suse.cz" , "amir73il@gmail.com" , "bfields@fieldses.org" , "khazhy@google.com" , "linux-mm@kvack.org" , "linux-nfs@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "jlayton@kernel.org" , "chuck.lever@oracle.com" Subject: Re: [PATCH RFC] nfsd: avoid recursive locking through fsnotify Message-ID: <20220322103744.niejj6ovzxyfej74@quack3.lan> References: <20220319001635.4097742-1-khazhy@google.com> <20220321112310.vpr7oxro2xkz5llh@quack3.lan> <31f7822e84583235d84b8c7be24360c46c7450f7.camel@hammerspace.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <31f7822e84583235d84b8c7be24360c46c7450f7.camel@hammerspace.com> Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=EGp9LJf8; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=3JVJ1Yl6; spf=pass (imf09.hostedemail.com: domain of jack@suse.cz designates 195.135.220.29 as permitted sender) smtp.mailfrom=jack@suse.cz; dmarc=none X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 60D75140022 X-Stat-Signature: 11dnjzmb3fzqyb9y15h3sdcw1c4thq9u X-HE-Tag: 1647945467-350628 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon 21-03-22 22:50:11, Trond Myklebust wrote: > On Mon, 2022-03-21 at 12:23 +0100, Jan Kara wrote: > > On Sat 19-03-22 11:36:13, Amir Goldstein wrote: > > > On Sat, Mar 19, 2022 at 9:02 AM Trond Myklebust > > > wrote: > > > >=20 > > > > On Fri, 2022-03-18 at 17:16 -0700, Khazhismel Kumykov wrote: > > > > > fsnotify_add_inode_mark may allocate with GFP_KERNEL, which may > > > > > result > > > > > in recursing back into nfsd, resulting in deadlock. See below > > > > > stack. > > > > >=20 > > > > > nfsd=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 D=A0=A0=A0 0 1591536=A0=A0= =A0=A0=A0 2 0x80004080 > > > > > Call Trace: > > > > > =A0__schedule+0x497/0x630 > > > > > =A0schedule+0x67/0x90 > > > > > =A0schedule_preempt_disabled+0xe/0x10 > > > > > =A0__mutex_lock+0x347/0x4b0 > > > > > =A0fsnotify_destroy_mark+0x22/0xa0 > > > > > =A0nfsd_file_free+0x79/0xd0 [nfsd] > > > > > =A0nfsd_file_put_noref+0x7c/0x90 [nfsd] > > > > > =A0nfsd_file_lru_dispose+0x6d/0xa0 [nfsd] > > > > > =A0nfsd_file_lru_scan+0x57/0x80 [nfsd] > > > > > =A0do_shrink_slab+0x1f2/0x330 > > > > > =A0shrink_slab+0x244/0x2f0 > > > > > =A0shrink_node+0xd7/0x490 > > > > > =A0do_try_to_free_pages+0x12f/0x3b0 > > > > > =A0try_to_free_pages+0x43f/0x540 > > > > > =A0__alloc_pages_slowpath+0x6ab/0x11c0 > > > > > =A0__alloc_pages_nodemask+0x274/0x2c0 > > > > > =A0alloc_slab_page+0x32/0x2e0 > > > > > =A0new_slab+0xa6/0x8b0 > > > > > =A0___slab_alloc+0x34b/0x520 > > > > > =A0kmem_cache_alloc+0x1c4/0x250 > > > > > =A0fsnotify_add_mark_locked+0x18d/0x4c0 > > > > > =A0fsnotify_add_mark+0x48/0x70 > > > > > =A0nfsd_file_acquire+0x570/0x6f0 [nfsd] > > > > > =A0nfsd_read+0xa7/0x1c0 [nfsd] > > > > > =A0nfsd3_proc_read+0xc1/0x110 [nfsd] > > > > > =A0nfsd_dispatch+0xf7/0x240 [nfsd] > > > > > =A0svc_process_common+0x2f4/0x610 [sunrpc] > > > > > =A0svc_process+0xf9/0x110 [sunrpc] > > > > > =A0nfsd+0x10e/0x180 [nfsd] > > > > > =A0kthread+0x130/0x140 > > > > > =A0ret_from_fork+0x35/0x40 > > > > >=20 > > > > > Signed-off-by: Khazhismel Kumykov > > > > > --- > > > > > =A0fs/nfsd/filecache.c | 4 ++++ > > > > > =A01 file changed, 4 insertions(+) > > > > >=20 > > > > > Marking this RFC since I haven't actually had a chance to test > > > > > this, > > > > > we > > > > > we're seeing this deadlock for some customers. > > > > >=20 > > > > > diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c > > > > > index fdf89fcf1a0c..a14760f9b486 100644 > > > > > --- a/fs/nfsd/filecache.c > > > > > +++ b/fs/nfsd/filecache.c > > > > > @@ -121,6 +121,7 @@ nfsd_file_mark_find_or_create(struct > > > > > nfsd_file > > > > > *nf) > > > > > =A0=A0=A0=A0=A0=A0=A0 struct fsnotify_mark=A0=A0=A0 *mark; > > > > > =A0=A0=A0=A0=A0=A0=A0 struct nfsd_file_mark=A0=A0 *nfm =3D NULL= , *new; > > > > > =A0=A0=A0=A0=A0=A0=A0 struct inode *inode =3D nf->nf_inode; > > > > > +=A0=A0=A0=A0=A0=A0 unsigned int pflags; > > > > >=20 > > > > > =A0=A0=A0=A0=A0=A0=A0 do { > > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 mutex_lock(&nfsd_= file_fsnotify_group- > > > > > >mark_mutex); > > > > > @@ -149,7 +150,10 @@ nfsd_file_mark_find_or_create(struct > > > > > nfsd_file > > > > > *nf) > > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 new->nfm_mark.mas= k =3D FS_ATTRIB|FS_DELETE_SELF; > > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 refcount_set(&new= ->nfm_ref, 1); > > > > >=20 > > > > > +=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 /* fsnotify allocat= es, avoid recursion back > > > > > into nfsd > > > > > */ > > > > > +=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 pflags =3D memalloc= _nofs_save(); > > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 err =3D fsnotify_= add_inode_mark(&new->nfm_mark, > > > > > inode, > > > > > 0); > > > > > +=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 memalloc_nofs_resto= re(pflags); > > > > >=20 > > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 /* > > > > > =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 * If the add w= as successful, then return the > > > > > object. > > > >=20 > > > > Isn't that stack trace showing a slab direct reclaim, and not a > > > > filesystem writeback situation? > > > >=20 > > > > Does memalloc_nofs_save()/restore() really fix this problem? It > > > > seems > > > > to me that it cannot, particularly since knfsd is not a > > > > filesystem, and > > > > so does not ever handle writeback of dirty pages. > > > >=20 > > >=20 > > > Maybe NOFS throttles direct reclaims to the point that the problem > > > is > > > harder to hit? > > >=20 > > > This report came in at good timing for me. > > >=20 > > > It demonstrates an issue I did not predict for "volatile"' fanotify > > > marks [1]. > > > As far as I can tell, nfsd filecache is currently the only fsnotify > > > backend that > > > frees fsnotify marks in memory shrinker. "volatile" fanotify marks > > > would also > > > be evictable in that way, so they would expose fanotify to this > > > deadlock. > > >=20 > > > For the short term, maybe nfsd filecache can avoid the problem by > > > checking > > > mutex_is_locked(&nfsd_file_fsnotify_group->mark_mutex) and abort > > > the > > > shrinker. I wonder if there is a place for a helper > > > mutex_is_locked_by_me()? > > >=20 > > > Jan, > > >=20 > > > A relatively simple fix would be to allocate > > > fsnotify_mark_connector in > > > fsnotify_add_mark() and free it, if a connector already exists for > > > the object. > > > I don't think there is a good reason to optimize away this > > > allocation > > > for the case of a non-first group to set a mark on an object? > >=20 > > Indeed, nasty. Volatile marks will add group->mark_mutex into a set > > of > > locks grabbed during inode slab reclaim. So any allocation under > > group->mark_mutex has to be GFP_NOFS now. This is not just about > > connector > > allocations but also mark allocations for fanotify. Moving > > allocations from > > under mark_mutex is also possible solution but passing preallocated > > memory > > around is kind of ugly as well. So the cleanest solution I currently > > see is > > to come up with helpers like "fsnotify_lock_group() & > > fsnotify_unlock_group()" which will lock/unlock mark_mutex and also > > do > > memalloc_nofs_save / restore magic.=20 >=20 > As has already been reported, the problem was fixed in Linux 5.5 by the > garbage collector rewrite, and so this is no longer an issue. Sorry, I was not clear enough I guess. NFS is not a problem since 5.5 as you say. But Amir has changes in the works after which any filesystem ino= de reclaim could end up in exactly the same path (calling fsnotify_destroy_mark() from clear_inode()). So these changes would introduce the same deadlock NFS was prone to before 5.5. > In addition, please note that memalloc_nofs_save/restore and the use of > GFP_NOFS was never a solution, because it does not prevent the kind of > direct reclaim that was happening here. You'd have to enforce > GFP_NOWAIT allocations, afaics. GFP_NOFS should solve the above problem because with GFP_NOFS we cannot enter inode reclaim from the memory allocation and thus end up freeing marks. Honza --=20 Jan Kara SUSE Labs, CR