Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp251730rwb; Fri, 30 Sep 2022 22:04:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7XyuGdxXxp1NO4zk3aNR5sirpjjfvKX6EhNQ8+QGJmcnQ9YybNPTdYSEV6miuerq9V473X X-Received: by 2002:aa7:9107:0:b0:553:92a4:d930 with SMTP id 7-20020aa79107000000b0055392a4d930mr12274607pfh.72.1664600690322; Fri, 30 Sep 2022 22:04:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664600690; cv=none; d=google.com; s=arc-20160816; b=FCtUXwSk35Rx8dbVH/WAc+/hxLolyhFWdTbVcVKJ5dIQItR5azahjoHy67DQrrQrUx sJCXYXzrMJUU1LqYrpYZ2QJUMjKI+2AQXebmVAx5MSIyMPWh/gEqppu7KvMHtrUm4h7D TtsoBHUjdtFVusHSmozmx7uvszCXsDFcD0JYjbrNOphZPmo1GUgTb0QWAfDgxcRhIJry 3SDTsxK1PuZ7HV2KUlmQ1/Dw3BmASb/GgO2CN6i/p/JugdR5qUthn3uukwTSicQSK4uq CMS/3xZ5/RbddE70w41BH0tfLdOjNDjPBwatapeHMTtuOpS/Fu6HPFV+7t8OOrrB57gz NpPg== 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=sOAhG365r1YKWPtMAZ+7EwTHakaspF+5YlSwaoTLlAM=; b=gObyezEY1RWNHELqvqLuDhPPCIq9nOltGqwbo8PWR+zG7y+KQWvfZRfK5VBIOuxTsI l/BBAoFGRqueL8p636tBtk4avzfzQch4ENV9ZoaGkSxhafgrhD7bNXijD52oRIONRCOy uboRjmbd9VwqNPdMWMGH+cRI3IHxKYdsDCzEbL+Pl2I2wJLtbEVCvT5I5OMjyv4aHxQP 56ef9lsCAhoTSfaRX5X1yp3j0PB9/r29tKDeZBsJwJdr/LHunwRtAhY1mueeVbUIlNnA 9lbCd9SQasxjrOywvY0pFxAZ4OsIrYiKk4oYt80McWEFV45pWPZWiBxWgH+uDnrgusw6 TZlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=RbP5b1uV; 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 e11-20020a170903240b00b00179e70b9e6csi4557199plo.519.2022.09.30.22.04.34; Fri, 30 Sep 2022 22:04:50 -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.de header.s=susede2_rsa header.b=RbP5b1uV; 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 S229447AbiJAFDp (ORCPT + 99 others); Sat, 1 Oct 2022 01:03:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbiJAFDp (ORCPT ); Sat, 1 Oct 2022 01:03:45 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3ED0324BE2 for ; Fri, 30 Sep 2022 22:03:44 -0700 (PDT) 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 DF595218E1; Sat, 1 Oct 2022 05:03:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1664600622; 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=sOAhG365r1YKWPtMAZ+7EwTHakaspF+5YlSwaoTLlAM=; b=RbP5b1uV6pxK/JGCrIhqFNL0UF6r+ISJScehGkRuSSamEp0/bkUJlx2HxZm69diOIC6uUn To3AKD4yTOuEUd74yCUKL48k7/pc5dxyj99u6tlTXqk7CQb1jPmrR/80ccM12IIojPiBsg owCoDBJiL59mIjurqkFrIcvmtH27n1M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1664600622; 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=sOAhG365r1YKWPtMAZ+7EwTHakaspF+5YlSwaoTLlAM=; b=6CkPjoMzmfzQ59b4PL9EDeSnU7aOar+bBSk4CpiHSfUcjnR6uhDncnURVEw87shsAXxEm0 Tvu3fgKQ3RXJihDw== 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 9588D1348E; Sat, 1 Oct 2022 05:03:41 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 6IhUEi3KN2POcwAAMHmgww (envelope-from ); Sat, 01 Oct 2022 05:03:41 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit MIME-Version: 1.0 From: "NeilBrown" To: "Jeff Layton" Cc: chuck.lever@oracle.com, linux-nfs@vger.kernel.org Subject: Re: [PATCH 2/3] nfsd: fix potential race in nfsd_file_close In-reply-to: <20220930191550.172087-3-jlayton@kernel.org> References: <20220930191550.172087-1-jlayton@kernel.org>, <20220930191550.172087-3-jlayton@kernel.org> Date: Sat, 01 Oct 2022 15:03:38 +1000 Message-id: <166460061835.17572.12490851025838613566@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 Sat, 01 Oct 2022, Jeff Layton wrote: > Once we call nfsd_file_put, there is no guarantee that "nf" can still be > safely accessed. That may have been the last reference. > > Change the code to instead check for whether nf_ref is 2 and then unhash > it and put the reference if we're successful. > > We might occasionally race with another lookup and end up unhashing it > when it probably shouldn't have been, but that should hopefully be rare > and will just result in the competing lookup having to create a new > nfsd_file. > > Signed-off-by: Jeff Layton > --- > fs/nfsd/filecache.c | 12 +++++++----- > 1 file changed, 7 insertions(+), 5 deletions(-) > > diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c > index 6237715bd23e..58f4d9267f4a 100644 > --- a/fs/nfsd/filecache.c > +++ b/fs/nfsd/filecache.c > @@ -461,12 +461,14 @@ nfsd_file_put(struct nfsd_file *nf) > */ > void nfsd_file_close(struct nfsd_file *nf) > { > - nfsd_file_put(nf); > - if (refcount_dec_if_one(&nf->nf_ref)) { > - nfsd_file_unhash(nf); > - nfsd_file_lru_remove(nf); > - nfsd_file_free(nf); > + /* One for the reference being put, and one for the hash */ > + if (refcount_read(&nf->nf_ref) == 2) { > + if (nfsd_file_unhash(nf)) > + nfsd_file_put_noref(nf); > } > + /* put the ref for the stateid */ > + nfsd_file_put(nf); > + This looks racy. What if a get happens after the read and before the unhash? If we unhash the nfsd_file at last close, why does the hash table hold a counted reference at all? When it is hashed, set the NFSD_FILE_HASHED flag. On last-put, if that flag is set, unhash it. If you want to unhash it earlier, test/clear the flag and delete from rhashtable. NeilBrown > } > > struct nfsd_file * > -- > 2.37.3 > >