Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp1526217rwl; Thu, 5 Jan 2023 15:10:29 -0800 (PST) X-Google-Smtp-Source: AMrXdXudK3Faq3Ec4z4Ty29cp7yDYcl3EoAQfqqWH3/xBXJaEldV+2skpyWDvg/wn3sDO94RAi/L X-Received: by 2002:a17:903:240b:b0:192:991f:d8e8 with SMTP id e11-20020a170903240b00b00192991fd8e8mr32890141plo.53.1672960229197; Thu, 05 Jan 2023 15:10:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672960229; cv=none; d=google.com; s=arc-20160816; b=lELxdJR/FGYbjtV24nQ0UGEzX77YICBahg6J9GaRwxdNKMvi8q/W1tYSql/y8IUUQC fM64+jpTFHAs8QCrPGqsEX9bM2VgHqsYbGihy0v0odO4Z/8Ewg0XspObGgTNHed6V8ap hDYuS+fJNlN10D3V6uL3V8YtHTlTenpnhfcpKKIH4u41QuH18F8aNK70Sj/ZKfVhp3yP iZTLQ0FtuQCo5wqaGVrpBqVwIVmJbvR4dFrk/ujq1o3VfBiYwmx3MIvk9vmcM8T+FNav cyeYfqnIqGVcrC522rSNJp0mn3aHG9CAtCTdqOCEnRGNCJVDGJqQRI/DQdH3KLOU/Gmq 6puw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:references:in-reply-to:subject :cc:to:from:mime-version:content-transfer-encoding:dkim-signature :dkim-signature; bh=wE2E5RH/T3EMWD0ZHpEYlSYcSLqg6q6BDhyxhuyWCwU=; b=fXjFtzmWqtXvGCB74sE4S6dStadTq0XxF4UZAnBkmewpxuCnGunZjyOGt6zrUPmbWP 6++kxH5UBeixBsW+rUM03YERodQFtVb4mxJvqVv28CyjtHTZGEhqfrfNl55BTu7QerES +tnKg45L3Khe/7gD8VAVnAAEaJ758LnGRtmQWlDXDKtV8Q0EX5Vt3zSfMBolM3n4065o +/jd5Fx4ss9P5ZbUTLXu2p2MbZo0imaC2uGe9KDNw/P6jZ5mUxdx0vhABgMON11D+oM/ 6ZucWlLmSQqk19ervCs2F6HSShV950jgTHfBogkSS3nPN3DwUEkSbSxWyCPBkCSFFjjG IjdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=KMXzg0FP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m17-20020a170902f65100b00189697e2433si8702876plg.537.2023.01.05.15.10.13; Thu, 05 Jan 2023 15:10:29 -0800 (PST) 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.de header.s=susede2_rsa header.b=KMXzg0FP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235947AbjAEXFm (ORCPT + 99 others); Thu, 5 Jan 2023 18:05:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232518AbjAEXFl (ORCPT ); Thu, 5 Jan 2023 18:05:41 -0500 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18878671A9 for ; Thu, 5 Jan 2023 15:05:41 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 0F7F83401E; Thu, 5 Jan 2023 23:05:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1672959939; 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=wE2E5RH/T3EMWD0ZHpEYlSYcSLqg6q6BDhyxhuyWCwU=; b=KMXzg0FPsbRIfmxsY88ePCGB0TTp0xbUzR820aZBSKCJbq4vgeKBKYnxL4c1tpphJhQZBY 9v6cW9T7+58tXaOv63gnfPCnmuPZzU0QYByBSHRLWeEt99Wpd7pPPJw1cOzXwwuPueJxXN LbxhT1Iyju5MvK+IBMzSBPRteNxXn4k= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1672959939; 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=wE2E5RH/T3EMWD0ZHpEYlSYcSLqg6q6BDhyxhuyWCwU=; b=kEQCIBldJIVIpOR3BDZs2DIiExRaCh2ykVut63q4UgVEdGWAV2vM3H199yYMIthJrmyHGZ mMWsEuNYNRO6lYBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C61C2138DF; Thu, 5 Jan 2023 23:05:37 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id N5cnHsFXt2OYawAAMHmgww (envelope-from ); Thu, 05 Jan 2023 23:05:37 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 From: "NeilBrown" To: "Jeff Layton" Cc: chuck.lever@oracle.com, linux-nfs@vger.kernel.org Subject: Re: [PATCH] nfsd: fix potential race in nfs4_find_file In-reply-to: <20230105121823.21935-1-jlayton@kernel.org> References: <20230105121823.21935-1-jlayton@kernel.org> Date: Fri, 06 Jan 2023 10:05:31 +1100 Message-id: <167295993121.13974.8791979932693514625@noble.neil.brown.name> 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 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 Thu, 05 Jan 2023, Jeff Layton wrote: > Even though there is a WARN_ON_ONCE check, it seems possible for > nfs4_find_file to race with the destruction of an fi_deleg_file while > trying to take a reference to it. >=20 > put_deleg_file is done while holding the fi_lock. Take and hold it > when dealing with the fi_deleg_file in nfs4_find_file. >=20 > Signed-off-by: Jeff Layton > --- > fs/nfsd/nfs4state.c | 16 ++++++++++------ > 1 file changed, 10 insertions(+), 6 deletions(-) >=20 > diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c > index b68238024e49..3df3ae84bd07 100644 > --- a/fs/nfsd/nfs4state.c > +++ b/fs/nfsd/nfs4state.c > @@ -6417,23 +6417,27 @@ nfsd4_lookup_stateid(struct nfsd4_compound_state *c= state, > static struct nfsd_file * > nfs4_find_file(struct nfs4_stid *s, int flags) > { > + struct nfsd_file *ret =3D NULL; > + > if (!s) > return NULL; > =20 > switch (s->sc_type) { > case NFS4_DELEG_STID: > - if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) > - return NULL; > - return nfsd_file_get(s->sc_file->fi_deleg_file); > + spin_lock(&s->sc_file->fi_lock); > + if (!WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) > + ret =3D nfsd_file_get(s->sc_file->fi_deleg_file); > + spin_unlock(&s->sc_file->fi_lock); > + break; As an nfsd_file is freed with rcu, we don't need the spinlock. rcu_read_lock() ret =3D rcu_dereference(s->sc_file->fi_deleg_file); if (ret) ret =3D nfsd_file_get(ret); rcu_read_unlock(); You could even put the NULL test in nfsd_file_get() and have: rcu_read_lock()l; ret =3D nfsd_file_get(rcu_dereference(s->sc_file->fi_deleg_file)); rcu_read_unlock(); but that might not be a win. I agree with Chuck that the WARNing isn't helpful. NeilBrown > case NFS4_OPEN_STID: > case NFS4_LOCK_STID: > if (flags & RD_STATE) > - return find_readable_file(s->sc_file); > + ret =3D find_readable_file(s->sc_file); > else > - return find_writeable_file(s->sc_file); > + ret =3D find_writeable_file(s->sc_file); > } > =20 > - return NULL; > + return ret; > } > =20 > static __be32 > --=20 > 2.39.0 >=20 >=20