Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6792571rwb; Wed, 18 Jan 2023 09:23:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXvgMPUAbwHyHA4sbLDRR5yCXWxtMIp2QxMxr4zi6pnRyp+44Qsblyd+EsWsNMX5dbQp9ZHs X-Received: by 2002:a17:907:d10:b0:86e:df17:df94 with SMTP id gn16-20020a1709070d1000b0086edf17df94mr10809890ejc.14.1674062610170; Wed, 18 Jan 2023 09:23:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674062610; cv=none; d=google.com; s=arc-20160816; b=xs1XZkn5NeIJXCf3b7KU5Unf5+7UpbRseCF5gATvA+5r1v+DIs9mli3KxYRYgzSIBc KzGGYTfI9FdLJJ8WkxcYbpXXuzTpYXtJjhxp91vhCuMv/9qztRYDChsww0Aqiq/tU+DO ruuZ5GTAlzqdogxLQMSXqu+reOTjdFz3xSB8jdZ1d0d7OOt+/Yi8+LvOD+7zjjDK47d4 yhVML41tEkHBf0ZaUX/KdWb4i3lNwBI/4LhS/0bPRRnzFUnVZZPviCouz+hirGN8IATR t1386miGOzAbdrS03gZ6vNJwDAGwF7ey81g2V0tl9AaErSqR1ryuGlzUKUWmFYgNU70G zXUw== 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=QOf3/hpcRWpbxtTLMwFOMfYtTv4HZRFXC8ac78frR4k=; b=rbN4FuvHyWuo2fTfpkT3o+3/Axr/T+vaMRMbu3QpSe1E46niXyN/opCt0FeFxdNcY0 IT+ST8qnIP1VnAq0VOKfE5t/6w7SAjdSAVyPc39gnlGN1J2L0jmJv4insOnEbrVstSWl mNvjwl/rsn6UVr7/nxbWIHlYLCsNYZBPksQQp8cMOLM3fnwRclvbomRswE+xMyXpbUY7 iuNhJ1AhrR8OJKNr3FhgiLPcjsu65SpxVpRl2YzTIXMNqTRtQUpMt5ZDFOMkurEEAEIY Kujd7l71Kt1DSgBkr+xO/hVj+eX/HQHsQj3M9olmQBVBibNJqfXtW7Tgf7eFh0WsIL78 46uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Zq4BRMKS; 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 sc16-20020a1709078a1000b008708a80a16dsi7386837ejc.235.2023.01.18.09.23.06; Wed, 18 Jan 2023 09:23:30 -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=Zq4BRMKS; 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 S229567AbjARRGa (ORCPT + 99 others); Wed, 18 Jan 2023 12:06:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230110AbjARRG3 (ORCPT ); Wed, 18 Jan 2023 12:06:29 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 772701716E for ; Wed, 18 Jan 2023 09:06:27 -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 dfw.source.kernel.org (Postfix) with ESMTPS id F2E14618FF for ; Wed, 18 Jan 2023 17:06:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E66A5C433F1; Wed, 18 Jan 2023 17:06:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674061586; bh=zQm+vgsOJ+AXgcyLum4Egz1zuumWDJSdIGJAqvkYP6s=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=Zq4BRMKSViHJZOrtGSDsfjTWOYAas4RrApGlKf6Bvo97JdkrDWgVXfycwbsHn+OaY ZXs9FyFyj84q59Q1z1crGTp2uJO2oXaKIm7VGz2LgyrEt1tDumpn+nzO+uI0XIWpJk GsKHSebPX3xP1fDjEe2uqiV2mpiQubim5IBdi9ZJCH1CO8aXuzzaAe6yJ7d2QiRwZ6 rvunIEVu1ZUac7zzMiEyUeQdXikworb7TqY4Z/mlmuyDeOQc1U5Bv0Ttw43rSlolU8 GOHNE+QAy5/Dem9y3kjb6/U+rTAv4ho2bakYjojLOzjwaLDqcMzW/36Mu/Q3X2Ojjm LxOPZfbncsg2Q== Message-ID: <0fbcbdc37e7e3f070b491848a74be348843074c2.camel@kernel.org> Subject: Re: [PATCH 2/2] nfsd: clean up potential nfsd_file refcount leaks in COPY codepath From: Jeff Layton To: Chuck Lever III , Olga Kornievskaia Cc: Linux NFS Mailing List , Dai Ngo Date: Wed, 18 Jan 2023 12:06:24 -0500 In-Reply-To: <12C5F3B3-6DB1-4483-8160-A691EB464464@oracle.com> References: <20230117193831.75201-1-jlayton@kernel.org> <20230117193831.75201-3-jlayton@kernel.org> <1fc9af5a2c2a79c5befa4510c714f97e26b13ed5.camel@kernel.org> <12C5F3B3-6DB1-4483-8160-A691EB464464@oracle.com> 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 16:39 +0000, Chuck Lever III wrote: >=20 > > On Jan 18, 2023, at 11:29 AM, Olga Kornievskaia wrote: > >=20 > > 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 wr= ote: > > > > >=20 > > > > > There are two different flavors of the nfsd4_copy struct. One is > > > > > embedded in the compound and is used directly in synchronous copi= es. The > > > > > other is dynamically allocated, refcounted and tracked in the cli= ent > > > > > struture. For the embedded one, the cleanup just involves releasi= ng 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, e= tc. > > > > >=20 > > > > > There is at least one potential refcount leak in this code now. I= f the > > > > > kthread_create call fails, then both the src and dst nfsd_files i= n 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 no= t > > > being put in this codepath. > >=20 > > Can you please point out where do we take a reference on the original c= opy? > >=20 > > > > > The cleanup in this codepath is also sort of weird. In the async = copy > > > > > case, we'll have up to four nfsd_file references (src and dst for= both > > > > > flavors of copy structure). > > > >=20 > > > > That's not true. There is a careful distinction between intra -- wh= ich > > > > had 2 valid file pointers and does a get on both as they both point= to > > > > something that's opened on this server--- but inter -- only does a = get > > > > 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 ma= kes > > > > the code less clear and at some point somebody might want to decide= to > > > > 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 > In cases like this, it makes sense to consider using types to > ensure the code can't do the wrong thing. So you might want to > have a struct nfs4_copy_A for the inter code to use, and a struct > nfs4_copy_B for the intra code to use. Sharing the same struct > for both use cases is probably what's confusing to human readers. >=20 > I've never been a stickler for removing every last ounce of code > duplication. Here, it might help to have a little duplication > just to make it easier to reason about the reference counting in > the two use cases. >=20 > That's my view from the mountain top, worth every penny you paid > for it. >=20 +1 The nfsd4_copy structure has a lot of fields in it that only matter for the async copy case. ISTM that nfsd4_copy (the function) should dynamically allocate a struct nfsd4_async_copy that contains a nfsd4_copy and whatever other fields are needed. Then, we could trim down struct nfsd4_copy to just the info needed. For instance, the nf_src and nf_dst fields really don't need to be in nfsd4_copy. For the synchronous copy case, we can just keep those pointers on the stack, and for the async case they would be inside the larger structure. That would allow us to trim down the footprint of the compound union too. --=20 Jeff Layton