Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp4394821rwe; Tue, 30 Aug 2022 09:19:12 -0700 (PDT) X-Google-Smtp-Source: AA6agR5TZLo7+vt+pyF2XREL5O2/mrKE1vMLS7BJixT0y0NA4O0kqBp3vUjztqBPyZaT2BdUiiA8 X-Received: by 2002:a17:907:3f98:b0:730:cfce:9c0f with SMTP id hr24-20020a1709073f9800b00730cfce9c0fmr17852961ejc.475.1661876352335; Tue, 30 Aug 2022 09:19:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661876352; cv=none; d=google.com; s=arc-20160816; b=wD7VWcogAKNJiGBX0OoPFIU3OJD35SB0y2dQ00GckXn1ZjQRBTR1dRFxlZWQYPawz4 ZEqEUBAUQcFeDq4nhlTsBqLEVSh3fzBekY3QmZR3jt3kydnCdyh52GUoWfWhrVA8g/f9 90rLKu+mMxlvAbUxGoffkP1R1RRTJCM1s/dFdWu7seEfjb4baVzsz9a2knROXkRmGX3w BpGEuobagZkkYdQyASC/aPoxJ/+ps2fZzaOD9msT8iD66Mg905BFQpJYusOpFBJ7cKxZ 4q3ymQOi7QGGCIklBxsXA6dBY9tNbltkeiisfsOOgZfG1a+KN1MLJmMnItSfOs2GQ8ZZ k7bQ== 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; bh=CBamQzSNNxE6SjxTKzzEO+qM/bRDvj9E4TwCaLJXBMk=; b=vUshyAfDFyfyyI+zCf2PPAW9KRqY9yYDSLhL3lpZwKhnPPEorz+zQOXTyM69UiNfuu O33mRTSpFM+ZeqbVLxQ+fNnYrjSrc5t5J+bNG6nmSMbvXektQ7rJj/Xevuk3o1QyCQ2+ 8G+/6ucazoqMxSh6T7puvsTFCTIbs8MLJB7iPX6EjTjIx1N13xmMLgUdil3VO7jo99pe KcIIM5EbacpcWpqcBFGs8vZ1+Y/rSFGRzDrgdN4n4oUy6mdPK2d/NGiJsdoEqv9jb9kp slIkntPppeUm30P1phrRVjr0BJbFH8XB7CoYUpQhF4a6XQrz9a9+ilFsNITGl+IA8sVn Mqpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=fXNdwdpK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-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 my7-20020a1709065a4700b0074153b85d8esi6063540ejc.411.2022.08.30.09.18.34; Tue, 30 Aug 2022 09:19:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@ziepe.ca header.s=google header.b=fXNdwdpK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230274AbiH3PKV (ORCPT + 99 others); Tue, 30 Aug 2022 11:10:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230286AbiH3PKR (ORCPT ); Tue, 30 Aug 2022 11:10:17 -0400 Received: from mail-qv1-xf32.google.com (mail-qv1-xf32.google.com [IPv6:2607:f8b0:4864:20::f32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05CBE4F182 for ; Tue, 30 Aug 2022 08:10:11 -0700 (PDT) Received: by mail-qv1-xf32.google.com with SMTP id jy14so3123932qvb.12 for ; Tue, 30 Aug 2022 08:10:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc; bh=CBamQzSNNxE6SjxTKzzEO+qM/bRDvj9E4TwCaLJXBMk=; b=fXNdwdpK40/W9ZV/jtDtFanBo50pdD9Zqq2j76SqDodQNMcH/S/9ZORqwpujvM/1o7 Y6ijjlraOllJTnfE8hkllQZkWTkVYG+l9AgDUUsf4ThP+1u3Lezl0Ah/HcTMhGdPhepr 6oaOHPhbuuQXVL7zA8adj2Q6F6wDptOrEHfHvWpbBkt1JOkwh5bMK0Ps71apRT8m0wKL AOMSw1qxFu5ZtF1ie2DWVMI+XNlOgLhGsqhKFseVmGJOBV0nuysZvPAUoIJ8ecRUEdf7 8NQ34drZxPG72BJMShFI1bT2VX5a+waMVxy4LYhuSx5L0ejfGHWsTQ/GqJcHZt7AT8AA 7NCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=CBamQzSNNxE6SjxTKzzEO+qM/bRDvj9E4TwCaLJXBMk=; b=keL26Vg16UtFgJqEpa2PQBjfas11sxtgAqqMXtwJH9SGcDLVFnkoA7KtX0uf9jh52s F9WXRqQGp+bWB2Ljl65l7vYHWpSk/LXAG4qplTpwReSmpjjWlc9JZ72+lstdgsPaa4Pf mTo6dngpH0YMl+/3+iuUPTtSc0SVxUM0aNS8O8PRzkYkwtZMei/B8KUinn1ver9d69UN nJI3NOB8prGcGDODfrJhfMnXFQc0PYo8yJvTz5v2DUK/bvPWvURXevL8H5+5V4CjFZno 5fiuPBgUqNLvfWbAYuvjX9k5Iq7BLfRJzso0Ojm0kUoA/yli42dSHCEB60m+NH0bSLkJ Tpyw== X-Gm-Message-State: ACgBeo3qO6bqQ3rA+bNeexRBiGnoejpdmnTPJ58cS/Qj8vJzad3MdDSN nDFZmU48ywTAIe/TPwVw1kfBnqRBwwBAAg== X-Received: by 2002:a0c:f1c7:0:b0:474:725e:753e with SMTP id u7-20020a0cf1c7000000b00474725e753emr15301629qvl.49.1661872210775; Tue, 30 Aug 2022 08:10:10 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-142-162-113-129.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.162.113.129]) by smtp.gmail.com with ESMTPSA id bl8-20020a05620a1a8800b006bbdcb3fff7sm7634727qkb.69.2022.08.30.08.10.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Aug 2022 08:10:09 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.95) (envelope-from ) id 1oT2sS-003kUN-QX; Tue, 30 Aug 2022 12:10:08 -0300 Date: Tue, 30 Aug 2022 12:10:08 -0300 From: Jason Gunthorpe To: Anthony Krowiak Cc: Kevin Tian , Zhenyu Wang , Zhi Wang , Jani Nikula , Joonas Lahtinen , Rodrigo Vivi , Tvrtko Ursulin , David Airlie , Daniel Vetter , Eric Farman , Matthew Rosato , Halil Pasic , Vineeth Vijayan , Peter Oberparleiter , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Jason Herne , Harald Freudenberger , Diana Craciun , Alex Williamson , Cornelia Huck , Longfang Liu , Shameer Kolothum , Yishai Hadas , Eric Auger , Kirti Wankhede , Leon Romanovsky , Abhishek Sahu , intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, kvm@vger.kernel.org, Yi Liu Subject: Re: [PATCH 01/15] vfio: Add helpers for unifying vfio_device life cycle Message-ID: References: <20220827171037.30297-1-kevin.tian@intel.com> <20220827171037.30297-2-kevin.tian@intel.com> <907c54c6-7f5b-77f3-c284-45604c60c12e@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <907c54c6-7f5b-77f3-c284-45604c60c12e@linux.ibm.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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-kernel@vger.kernel.org On Tue, Aug 30, 2022 at 09:42:42AM -0400, Anthony Krowiak wrote: > > +/* > > + * Alloc and initialize vfio_device so it can be registered to vfio > > + * core. > > + * > > + * Drivers should use the wrapper vfio_alloc_device() for allocation. > > + * @size is the size of the structure to be allocated, including any > > + * private data used by the driver. > > > It seems the purpose of the wrapper is to ensure that the object being > allocated has as its first field a struct vfio_device object and to return > its container. Why not just make that a requirement for this function - > which I would rename vfio_alloc_device - and document it in the prologue? > The caller can then cast the return pointer or use container_of. There are three fairly common patterns for this kind of thing 1) The caller open codes everything: driver_struct = kzalloc() core_init(&driver_struct->core) 2) Some 'get priv' / 'get data' is used instead of container_of(): core_struct = core_alloc(sizeof(*driver_struct)) driver_struct = core_get_priv(core_struct) 3) The allocations and initialization are consolidated in the core, but we continue to use container_of() driver_struct = core_alloc(typeof(*driver_struct)) #1 has a general drawback that people routinely mess up the lifecycle model and get really confused about when to do kfree() vs put(), creating bugs. #2 has a general drawback of not using container_of() at all, and being a bit confusing in some cases #3 has the general drawback of being a bit magical, but solves 1 and 2's problems. I would not fix the struct layout without the BUILD_BUG_ON because someone will accidently change the order and that becomes a subtle runtime error - so at a minimum the wrapper macro has to exist to check that. If you want to allow a dynamic struct layout and avoid the pitfall of exposing the user to kalloc/kfree, then you still need the macro, and it does some more complicated offset stuff. Having the wrapper macro be entirely type safe is appealing and reduces code in the drivers, IMHO. Tell it what type you are initing and get back init'd memory for that type that you always, always free with a put operation. Jason