Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2113415pxp; Mon, 21 Mar 2022 11:35:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzTU01UW+zEGg1P4ws43NqVyTgRkAn6t0PbDE+GTuxgXnUlJWxOq5dd+RtVTzc7krI3Dyy X-Received: by 2002:a63:c24:0:b0:382:2a04:3dbe with SMTP id b36-20020a630c24000000b003822a043dbemr13915634pgl.158.1647887732871; Mon, 21 Mar 2022 11:35:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647887732; cv=none; d=google.com; s=arc-20160816; b=B2psSWgV5I7k7Xv+k965trlZMdpT8wBveq719yIj90QH1Me6B970DWB51Enj3ARM9g nO6M0bd6io9HBZWiMx1pSqwWejCx7mo9jGWAJAknJOjTiWcYEeqtJZGgc6eHp/x6kMl+ nhNpeJkuZTL9dPOkNOBVVx91KdISvBa8i/e+IqPlWqqiDuOZF225LpX/5G7rhE32WMX5 vpF94zwWbhGmQUZI+JeNT4FeF49sY7D+3TBUx6ryTwVIfolcZ2wNP6RBqcYsvxY5Go9p tT0EGXoVr5iPJtWEMarJb/Hg23RNHelAVhAOu1zzcuwxaTHAPiIUF7xNIjADC9AXC/4z bSuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature :dkim-signature; bh=S7YH2NCMXs0j2a1y2EKU6RGg6TUUSRPTbk3irwt96Do=; b=DKg2f/9FrP9A13U5e4L2mgLDprTf5DC6MQhNQ57ezfRhkcwL2WbhfVJQhJt+VSzuu/ txoDRkc0t6sKgl0DOES6erQW2YUgxsO1Aps1qQ2CZZcJ24l2BNXWZ04GdanulQXaKiAp hydAo02tZrwK49aGaRIotNtN98Y2iHUtyc9i9GmRYe8coCiUG+khlXr6yZKQEPU3WxbD sBqrlPeFGPXMUGMNhHNOMHJikfM9UFBtx9r+ejeu8b5ncczE8AS+10+aZf2W9JC5foiK LYwKaqHLX9V01uVBfZwh6jHyUCssLs96OQIQfHopReseDrdo15fbkD9MFEmjGHCW61Cf FtsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=aalP1obo; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q13-20020a170902b10d00b00153b691f326si10945995plr.364.2022.03.21.11.35.06; Mon, 21 Mar 2022 11:35:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=aalP1obo; dkim=neutral (no key) header.i=@suse.cz; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233349AbiCULYi (ORCPT + 99 others); Mon, 21 Mar 2022 07:24:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346756AbiCULYh (ORCPT ); Mon, 21 Mar 2022 07:24:37 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 398B217E38; Mon, 21 Mar 2022 04:23:12 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id CC01E210E6; Mon, 21 Mar 2022 11:23:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1647861790; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=S7YH2NCMXs0j2a1y2EKU6RGg6TUUSRPTbk3irwt96Do=; b=aalP1obog8/eREHh9ClZN4HsFStjor4Z0JkslGHycaxdP1O8udwwt4Rhw9a2GNxZmDQG2l LEurYMCxd3JPPFQ1M0y1h63ZviD4/vZt016a33NTlo1n9UeUqmJhaPYpiTq5RqKn7T+IUK gX9WbiMMNQyqzCRV7vxWE7qGMhPnzj4= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1647861790; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=S7YH2NCMXs0j2a1y2EKU6RGg6TUUSRPTbk3irwt96Do=; b=bLjmaa7wPygu6nvPdXNzh0NDKAiTnKhNsbLO5uX5C9DszxOw/eoB9xF0ddAQRYon5oiUIc z+WdaCgdrFPKrWAw== 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 6A476A3B83; Mon, 21 Mar 2022 11:23:10 +0000 (UTC) Received: by quack3.suse.cz (Postfix, from userid 1000) id 14B4DA0610; Mon, 21 Mar 2022 12:23:10 +0100 (CET) Date: Mon, 21 Mar 2022 12:23:10 +0100 From: Jan Kara To: Amir Goldstein Cc: Trond Myklebust , Jan Kara , "bfields@fieldses.org" , "khazhy@google.com" , "chuck.lever@oracle.com" , "linux-mm@kvack.org" , "linux-nfs@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Jeff Layton Subject: Re: [PATCH RFC] nfsd: avoid recursive locking through fsnotify Message-ID: <20220321112310.vpr7oxro2xkz5llh@quack3.lan> References: <20220319001635.4097742-1-khazhy@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org On Sat 19-03-22 11:36:13, Amir Goldstein wrote: > On Sat, Mar 19, 2022 at 9:02 AM Trond Myklebust wrote: > > > > 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. > > > > > > nfsd D 0 1591536 2 0x80004080 > > > Call Trace: > > > __schedule+0x497/0x630 > > > schedule+0x67/0x90 > > > schedule_preempt_disabled+0xe/0x10 > > > __mutex_lock+0x347/0x4b0 > > > fsnotify_destroy_mark+0x22/0xa0 > > > nfsd_file_free+0x79/0xd0 [nfsd] > > > nfsd_file_put_noref+0x7c/0x90 [nfsd] > > > nfsd_file_lru_dispose+0x6d/0xa0 [nfsd] > > > nfsd_file_lru_scan+0x57/0x80 [nfsd] > > > do_shrink_slab+0x1f2/0x330 > > > shrink_slab+0x244/0x2f0 > > > shrink_node+0xd7/0x490 > > > do_try_to_free_pages+0x12f/0x3b0 > > > try_to_free_pages+0x43f/0x540 > > > __alloc_pages_slowpath+0x6ab/0x11c0 > > > __alloc_pages_nodemask+0x274/0x2c0 > > > alloc_slab_page+0x32/0x2e0 > > > new_slab+0xa6/0x8b0 > > > ___slab_alloc+0x34b/0x520 > > > kmem_cache_alloc+0x1c4/0x250 > > > fsnotify_add_mark_locked+0x18d/0x4c0 > > > fsnotify_add_mark+0x48/0x70 > > > nfsd_file_acquire+0x570/0x6f0 [nfsd] > > > nfsd_read+0xa7/0x1c0 [nfsd] > > > nfsd3_proc_read+0xc1/0x110 [nfsd] > > > nfsd_dispatch+0xf7/0x240 [nfsd] > > > svc_process_common+0x2f4/0x610 [sunrpc] > > > svc_process+0xf9/0x110 [sunrpc] > > > nfsd+0x10e/0x180 [nfsd] > > > kthread+0x130/0x140 > > > ret_from_fork+0x35/0x40 > > > > > > Signed-off-by: Khazhismel Kumykov > > > --- > > > fs/nfsd/filecache.c | 4 ++++ > > > 1 file changed, 4 insertions(+) > > > > > > Marking this RFC since I haven't actually had a chance to test this, > > > we > > > we're seeing this deadlock for some customers. > > > > > > 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) > > > struct fsnotify_mark *mark; > > > struct nfsd_file_mark *nfm = NULL, *new; > > > struct inode *inode = nf->nf_inode; > > > + unsigned int pflags; > > > > > > do { > > > mutex_lock(&nfsd_file_fsnotify_group->mark_mutex); > > > @@ -149,7 +150,10 @@ nfsd_file_mark_find_or_create(struct nfsd_file > > > *nf) > > > new->nfm_mark.mask = FS_ATTRIB|FS_DELETE_SELF; > > > refcount_set(&new->nfm_ref, 1); > > > > > > + /* fsnotify allocates, avoid recursion back into nfsd > > > */ > > > + pflags = memalloc_nofs_save(); > > > err = fsnotify_add_inode_mark(&new->nfm_mark, inode, > > > 0); > > > + memalloc_nofs_restore(pflags); > > > > > > /* > > > * If the add was successful, then return the object. > > > > Isn't that stack trace showing a slab direct reclaim, and not a > > filesystem writeback situation? > > > > 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. > > > > Maybe NOFS throttles direct reclaims to the point that the problem is > harder to hit? > > This report came in at good timing for me. > > 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. > > 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()? > > Jan, > > 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? 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. Honza -- Jan Kara SUSE Labs, CR