Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6751967rwb; Wed, 18 Jan 2023 08:58:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXtU2Rr+i0LdxFV9C5V0FNXAXExkwa8aJWlw/n8X2BuW75YGKT62s9DJgfEtxgG+6LQIk3IB X-Received: by 2002:a05:6a20:54a4:b0:a4:3fc2:eb10 with SMTP id i36-20020a056a2054a400b000a43fc2eb10mr9230811pzk.37.1674061115963; Wed, 18 Jan 2023 08:58:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674061115; cv=none; d=google.com; s=arc-20160816; b=IvZeLsPraE6TXL1R3Fh7mAV99+S4+Z3doUCS8YytAbiLoGcmML55cufSGe0uY7OZ6d 2Y8mwYYGH4yojItky1U3/NM8OzoO1B+JB2m2Bh0dpeVobO8tXlmOBH0j6LL4p9fAj+qP Ocz57zIESvh+fokQiaimHCDhVcUZ1e/xPHSPpH1SMpdSI8MO2MyxDH4ohvVdmB5avYjM We94vsGpBkLyDH/kf+rAPZKNPkf3KQIirwJ9y3Qt42Hx6eryYhxUs638MNDvAmKYiMKu vt44hfXklPedmL8iB/TLF33UZ1obIWNoSltBDQHotKBXa61Cx6G/OZozdkipWgKfJfFV qPhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:references:in-reply-to:date:cc:to:from :subject:message-id:dkim-signature; bh=EqcgCFb5aDpiD81Sm0Yo3VU6Iio2d+gMqBWCudJsfgw=; b=fnnwbbyxgd2SN9oUoL/CcQ069Q0pwA4DdN7xX6KNDhn7tumpdaOrciAXELWVRKlarc CyGU5Od+nm1UoCZrMdnC6SnWEuS/+X/JptTxnP5KjXtIlMb+Nlox2ex1YT00sbnYikx5 Y3BB/ZWYtxmLkriFaG79Rt06XG3TLMOI6QEs9QWh9PS8CNIGgOfg8UPdODqG4OO99r1E f/QpLgt3jB8ZnunYwe4zyMBY/CLLwhdoJeQydFKDVGoLRFdjRjdZ5jtXES53sl5r06AY H3BDTb3G7elzybcw2FfxACzo+J0XEWje13W8+n1druP8fQ74VflFseNd9RH/hTGzEtmv /08g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RxgEvsmr; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l65-20020a639144000000b004cfc5850edasi4239597pge.290.2023.01.18.08.58.21; Wed, 18 Jan 2023 08:58:35 -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=@kernel.org header.s=k20201202 header.b=RxgEvsmr; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230184AbjARQ5j (ORCPT + 99 others); Wed, 18 Jan 2023 11:57:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230329AbjARQ5i (ORCPT ); Wed, 18 Jan 2023 11:57:38 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B012458A6 for ; Wed, 18 Jan 2023 08:57:37 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id D8AC3B81D81 for ; Wed, 18 Jan 2023 16:57:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2664FC433EF; Wed, 18 Jan 2023 16:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674061054; bh=40EVwOpf1lPtZgppdLEBRIiDeq/rMc8QUBBph29nVeU=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=RxgEvsmrVMqhNZCRAQ5dA4c+rxeEzw3slL8hiOjO2tvGzLPvboNZeXqUACOwTN7PI 4HGjO6ahxbn5Zy9eKDrdghIj1hUKpnYedhF4YkHBQR5lhCanWRatS7Sb8R7lHX/3yX h7uOrBD590BZy0Mz73e0kew8xveKuFHmfw1JG9T/jAeqTMRIMtnXiy+cNLVSPp558J s4laZSUL1nlGPHqM3UBhPKYeDfApDVowRZ8rHpgF1emAWIglQvchpVlhOE7dvB+8Mm R2Ss4aNfVz1wANjXhaZfZl6oX+MIO40UGrkEaT4TUjqZQjmY2HWBgOdqaFA1fFI73h gaXvK85YOrCyg== Message-ID: Subject: Re: [PATCH 2/2] nfsd: clean up potential nfsd_file refcount leaks in COPY codepath From: Jeff Layton To: Olga Kornievskaia Cc: chuck.lever@oracle.com, linux-nfs@vger.kernel.org, dai.ngo@oracle.com Date: Wed, 18 Jan 2023 11:57:32 -0500 In-Reply-To: References: <20230117193831.75201-1-jlayton@kernel.org> <20230117193831.75201-3-jlayton@kernel.org> <1fc9af5a2c2a79c5befa4510c714f97e26b13ed5.camel@kernel.org> Content-Type: text/plain; charset="ISO-8859-15" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.3 (3.46.3-1.fc37) MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Wed, 2023-01-18 at 11:29 -0500, Olga Kornievskaia wrote: > On Wed, Jan 18, 2023 at 10:27 AM Jeff Layton wrote: > >=20 > > On Wed, 2023-01-18 at 09:42 -0500, Olga Kornievskaia wrote: > > > On Tue, Jan 17, 2023 at 2:38 PM Jeff Layton wrot= e: > > > >=20 > > > > There are two different flavors of the nfsd4_copy struct. One is > > > > embedded in the compound and is used directly in synchronous copies= . The > > > > other is dynamically allocated, refcounted and tracked in the clien= t > > > > struture. For the embedded one, the cleanup just involves releasing= any > > > > nfsd_files held on its behalf. For the async one, the cleanup is a = bit > > > > more involved, and we need to dequeue it from lists, unhash it, etc= . > > > >=20 > > > > There is at least one potential refcount leak in this code now. If = the > > > > kthread_create call fails, then both the src and dst nfsd_files in = the > > > > original nfsd4_copy object are leaked. > > >=20 > > > I don't believe that's true. If kthread_create thread fails we call > > > cleanup_async_copy() that does a put on the file descriptors. > > >=20 > >=20 > > You mean this? > >=20 > > out_err: > > if (async_copy) > > cleanup_async_copy(async_copy); > >=20 > > That puts the references that were taken in dup_copy_fields, but the > > original (embedded) nfsd4_copy also holds references and those are not > > being put in this codepath. >=20 > Can you please point out where do we take a reference on the original cop= y? >=20 In the case of an inter-server copy, nf_dst is set in nfsd4_setup_inter_ssc. For intraserver copy, both pointers are set via the call to nfsd4_verify_copy. Both functions call nfs4_preprocess_stateid_op, which returns a reference to the nfsd_file in the second to last arg. > > > > The cleanup in this codepath is also sort of weird. In the async co= py > > > > case, we'll have up to four nfsd_file references (src and dst for b= oth > > > > flavors of copy structure). > > >=20 > > > That's not true. There is a careful distinction between intra -- whic= h > > > had 2 valid file pointers and does a get on both as they both point t= o > > > something that's opened on this server--- but inter -- only does a ge= t > > > on the dst file descriptor, the src doesn't exit. And yes I realize > > > the code checks for nfs_src being null which it should be but it make= s > > > the code less clear and at some point somebody might want to decide t= o > > > really do a put on it. > > >=20 > >=20 > > This is part of the problem here. We have a nfsd4_copy structure, and > > depending on what has been done to it, you need to call different > > methods to clean it up. That seems like a real antipattern to me. >=20 > But they call different methods because different things need to be > done there and it makes it clear what needs to be for what type of > copy. >=20 I sure as hell had a hard time dissecting how all of that was supposed to work. There is clear bug here, and I think this patch makes the result clearer and more robust in the face of changes. There are actually 4 different cases here: sync vs. async, alongside intra vs. interserver copy. These are all overloaded onto a nfsd4_copy structure, seemingly for no good reason. The cleanup, in particular seems quite fragile to me, and there is a dearth of defensive coding measures. If you subtly call the "wrong" cleanup function at the wrong point in time, then things may go awry. I'll leave it up to Chuck to make the final determination, but I see this patch as an improvement. > > > > They are both put at the end of > > > > nfsd4_do_async_copy, even though the ones held on behalf of the emb= edded > > > > one outlive that structure. > > > >=20 > > > > Change it so that we always clean up the nfsd_file refs held by the > > > > embedded copy structure before nfsd4_copy returns. Rework > > > > cleanup_async_copy to handle both inter and intra copies. Eliminate > > > > nfsd4_cleanup_intra_ssc since it now becomes a no-op. > > >=20 > > > I feel by combining the cleanup for both it obscures a very important > > > destication that src filehandle doesn't exist for inter. > >=20 > > If the src filehandle doesn't exist, then the pointer to it will be > > NULL. I don't see what we gain by keeping these two distinct, other tha= n > > avoiding a NULL pointer check. >=20 > My reason would be for code clarity because different things are > supposed to happen for intra and inter. Difference of opinion it > seems. >=20 > >=20 > > >=20 > > > > Signed-off-by: Jeff Layton > > > > --- > > > > fs/nfsd/nfs4proc.c | 23 ++++++++++------------- > > > > 1 file changed, 10 insertions(+), 13 deletions(-) > > > >=20 > > > > diff --git a/fs/nfsd/nfs4proc.c b/fs/nfsd/nfs4proc.c > > > > index 37a9cc8ae7ae..62b9d6c1b18b 100644 > > > > --- a/fs/nfsd/nfs4proc.c > > > > +++ b/fs/nfsd/nfs4proc.c > > > > @@ -1512,7 +1512,6 @@ nfsd4_cleanup_inter_ssc(struct nfsd4_ssc_umou= nt_item *nsui, struct file *filp, > > > > long timeout =3D msecs_to_jiffies(nfsd4_ssc_umount_timeout)= ; > > > >=20 > > > > nfs42_ssc_close(filp); > > > > - nfsd_file_put(dst); > > > > fput(filp); > > > >=20 > > > > spin_lock(&nn->nfsd_ssc_lock); > > > > @@ -1562,13 +1561,6 @@ nfsd4_setup_intra_ssc(struct svc_rqst *rqstp= , > > > > ©->nf_dst); > > > > } > > > >=20 > > > > -static void > > > > -nfsd4_cleanup_intra_ssc(struct nfsd_file *src, struct nfsd_file *d= st) > > > > -{ > > > > - nfsd_file_put(src); > > > > - nfsd_file_put(dst); > > > > -} > > > > - > > > > static void nfsd4_cb_offload_release(struct nfsd4_callback *cb) > > > > { > > > > struct nfsd4_cb_offload *cbo =3D > > > > @@ -1683,12 +1675,18 @@ static void dup_copy_fields(struct nfsd4_co= py *src, struct nfsd4_copy *dst) > > > > dst->ss_nsui =3D src->ss_nsui; > > > > } > > > >=20 > > > > +static void release_copy_files(struct nfsd4_copy *copy) > > > > +{ > > > > + if (copy->nf_src) > > > > + nfsd_file_put(copy->nf_src); > > > > + if (copy->nf_dst) > > > > + nfsd_file_put(copy->nf_dst); > > > > +} > > > > + > > > > static void cleanup_async_copy(struct nfsd4_copy *copy) > > > > { > > > > nfs4_free_copy_state(copy); > > > > - nfsd_file_put(copy->nf_dst); > > > > - if (!nfsd4_ssc_is_inter(copy)) > > > > - nfsd_file_put(copy->nf_src); > > > > + release_copy_files(copy); > > > > spin_lock(©->cp_clp->async_lock); > > > > list_del(©->copies); > > > > spin_unlock(©->cp_clp->async_lock); > > > > @@ -1748,7 +1746,6 @@ static int nfsd4_do_async_copy(void *data) > > > > } else { > > > > nfserr =3D nfsd4_do_copy(copy, copy->nf_src->nf_fil= e, > > > > copy->nf_dst->nf_file, false= ); > > > > - nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst)= ; > > > > } > > > >=20 > > > > do_callback: > > > > @@ -1811,9 +1808,9 @@ nfsd4_copy(struct svc_rqst *rqstp, struct nfs= d4_compound_state *cstate, > > > > } else { > > > > status =3D nfsd4_do_copy(copy, copy->nf_src->nf_fil= e, > > > > copy->nf_dst->nf_file, true)= ; > > > > - nfsd4_cleanup_intra_ssc(copy->nf_src, copy->nf_dst)= ; > > > > } > > > > out: > > > > + release_copy_files(copy); > > > > return status; > > > > out_err: > > > > if (async_copy) > > > > -- > > > > 2.39.0 > > > >=20 > >=20 > > -- > > Jeff Layton --=20 Jeff Layton