Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp5605822rwb; Wed, 7 Sep 2022 05:33:31 -0700 (PDT) X-Google-Smtp-Source: AA6agR4ktSniOhWhOTJZZiMRMFkOqHJKIW+tOrnKRsKUhu67PwCBGwJkVFTTp6H+6ogeE9TFwDSC X-Received: by 2002:a17:907:6e90:b0:770:8130:b51b with SMTP id sh16-20020a1709076e9000b007708130b51bmr1868052ejc.234.1662554011028; Wed, 07 Sep 2022 05:33:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662554011; cv=none; d=google.com; s=arc-20160816; b=v+/o1HKqRxzQVw40anw1C16v3Fk0S5/agNPo/piQOzVLuKnt8g9+xDlhVeyNCXYmoG ypTDXrmrosfY4UZ4cIr+cJmqfZM/vVCjZHeDPanAz1DlEBaIin8vcwH+/95W2Fk5O3mD h1Rif0gApmUVwOVyCX/axCi3909rqxUwCAQQH3E1aEJ34zeUxthWG6ETblpycNYD2hEQ ncb9jxi1UG5QisFOrX113aMIUbhylzHzkXnCo7QtSxnAnKggSUC+6UH83YW1lwataKuE oW/ZxYvoxBh3LUzaueDDfKKemTReZmAn6/W7apAu/aZWerjodYY5ll71iMqDyXbnGdup V0GA== 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=WIB8a4RMKIbsQgzbAFRigZUlzKpMzaK/mPjLu18jivg=; b=nbgDR7jPORI+hYPwd8Iy/NdpN8rIydXU2if4ULWCtE/TILHvPB34Zqz12jXxywYagJ FGih1kbXKYa3CWoF16HlGik/miK/Ahwb4b0TehiYYUjYovY5V/VgBgUTfkEnYhyVAfTY 9nxKH+OdcRRgoweb3s3/3ifXhfqpMk6qTwY7yLhIcxe8Du40MFXz13JAtBLuVHlqJSFL b7tafr9XCubRW3qBMBuxT8lKNAiW4R78oWbnpO8AkTMISl8ry2GCFVp0i7mnarM8mxiU pY94bo6YuvUBTHmSbqKrTTs1fMZ8Cdxznkfivgrwpgs3ot1DjX/k8xNXjTMWyWQBmwLR gayA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=HP3rDaJj; 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 d13-20020a056402400d00b0043c25d25046si11019075eda.249.2022.09.07.05.33.06; Wed, 07 Sep 2022 05:33:31 -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=HP3rDaJj; 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 S229920AbiIGMLn (ORCPT + 99 others); Wed, 7 Sep 2022 08:11:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229850AbiIGMLi (ORCPT ); Wed, 7 Sep 2022 08:11:38 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E98186048 for ; Wed, 7 Sep 2022 05:11:36 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id a15so10262765qko.4 for ; Wed, 07 Sep 2022 05:11:36 -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:subject:date; bh=WIB8a4RMKIbsQgzbAFRigZUlzKpMzaK/mPjLu18jivg=; b=HP3rDaJjD8dZmYIHrjdNkoDnmUw7g9uCc4vHBTmXsOrDzTaDzvVkGROQU4JiMIG6SF qudhCqJZhmdnGxt9Qchu2u/Kb1AzirulrNQKlif2VVN775xzycYMvQlIejXeuRl9FUCV B3sY2DzX6HPglZ4Tt6xcS2UP6H2y+QTtsv8hPxcu7qACinqcAbBEosQlGwToao3GbIXm 9n3zqjh8c5lL3+Pt3dwF6hwztXdonsbyY8QaXeKX/rwCr1nuqyp6n1gg8cgYPm+00Q2X HA4OSsdsbupGMsvkpLpJhuXakDbiiPMBOk6rzxqSUV5A1n0+hK7OQ2N2NVdOzCqW+cgi GP2A== 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:subject:date; bh=WIB8a4RMKIbsQgzbAFRigZUlzKpMzaK/mPjLu18jivg=; b=dJz+k3UbmVgAL28n2F7kSoaSjiDn/W26i64fxdYh30DWhkoMcMtoxezb20oJOPXIWs zXbZYCy+IXJ3wKTcEfaBftZqEcjzHcAfEbnDFDMBnt5T6AoRA1vfqiMSQjYw/CHYCS/o a+g5GEiI+2P4yCC1cb88I3hY45xfNxKEheHnAiHrJ5gn9VfVgH7LvXTNWEqglcK+ReXt lxyCNxuA/dIVMkvp13t4KkykbVGOB1Sh852z9lbXtRovpgkmoJWGvkBw8SuceLUWwj+A FpoHJiHpRHX/TchP+7s9ndlyJ37LYnsNiXwegsp64NooEzk+X7han+sEamRH+/2WVefG TnAg== X-Gm-Message-State: ACgBeo1E9/mKQS5k36BdTuHY58fQi4PDWf+JK/p/fwIRrKI1yK6RN/vR 2/3tA8fVLRZtMriuyRyEr0SU8A== X-Received: by 2002:a05:620a:1402:b0:6bc:2055:6da0 with SMTP id d2-20020a05620a140200b006bc20556da0mr2302837qkj.534.1662552695680; Wed, 07 Sep 2022 05:11:35 -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 n3-20020a05620a294300b006b953a7929csm14765165qkp.73.2022.09.07.05.11.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Sep 2022 05:11:34 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.95) (envelope-from ) id 1oVtu2-008YHj-0u; Wed, 07 Sep 2022 09:11:34 -0300 Date: Wed, 7 Sep 2022 09:11:34 -0300 From: Jason Gunthorpe To: Christoph Hellwig Cc: "Tian, Kevin" , Matthew Rosato , David Airlie , Joonas Lahtinen , "dri-devel@lists.freedesktop.org" , "linux-kernel@vger.kernel.org" , Kirti Wankhede , Vineeth Vijayan , Diana Craciun , Alexander Gordeev , Longfang Liu , "linux-s390@vger.kernel.org" , "Liu, Yi L" , "kvm@vger.kernel.org" , Leon Romanovsky , Halil Pasic , Christian Borntraeger , "intel-gfx@lists.freedesktop.org" , "Wang, Zhi A" , Tony Krowiak , Eric Farman , Vasily Gorbik , Heiko Carstens , Jani Nikula , Eric Auger , Alex Williamson , Harald Freudenberger , Zhenyu Wang , "Vivi, Rodrigo" , "intel-gvt-dev@lists.freedesktop.org" , Jason Herne , Tvrtko Ursulin , Yishai Hadas , Cornelia Huck , Peter Oberparleiter , Shameer Kolothum , Sven Schnelle , Daniel Vetter , Abhishek Sahu Subject: Re: [PATCH v2 01/15] vfio: Add helpers for unifying vfio_device life cycle Message-ID: References: <20220901143747.32858-1-kevin.tian@intel.com> <20220901143747.32858-2-kevin.tian@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Wed, Sep 07, 2022 at 04:55:18AM -0700, Christoph Hellwig wrote: > On Wed, Sep 07, 2022 at 12:43:30AM +0000, Tian, Kevin wrote: > > > From: Christoph Hellwig > > > Sent: Tuesday, September 6, 2022 5:42 PM > > > > > > What is the point? This adds indirect calls, and actually creates > > > more boilerplate code in the drivers. i.g. when using this code there > > > is more, and harder to read code. > > > > The point is to align with struct device life cycle when it's introduced > > to vfio_device. The object is released via put_device() then what would > > be the alternative if the driver doesn't provide a @release callback? > > > > and with @release then naturally @init is also expected. > > No, with a release no @init is expected. The init method is one > of the major obsfucations here, only topped by the weird > vfio_alloc_device macro. Yes, that saves about 4 lines of code > in every driver, but places a burden on the struct layout and > very much obsfucated things. Without vfio_alloc_device and > the init method I think much of this would make a lot more sense. > > See the patch below that goes on top of this series to show how > undoing these two would look on mbochs. It it a slight reduction > lines of code, but more readable and much less churn compared > to the status before this series. I've seen alot of error handling bugs caused by open-coding patterns like this. People get confused about what the lifecycle is and botch the error unwinds, almost 100% of the time :\ They call kfree when they should call put_device, they call put_device before initing enough stuff that the release callback doesn't crash, double free stuff by calling put_device at the wrong point, and so on. The advantage of init/release is the strict pairing and the core code helping get the error unwind right, by not calling release until init succeeds. The advantage of the vfio_alloc_device() is not saving 4 lines, it is giving the drivers a simple/sane error handling strategy. Goto unwind inside init, release undoes everything init does and the probe path only calls put_device(). It is simple and logical to implement and hard to make subtle bugs. Specifically it eliminates the open coded transition of kfree to put_device that seems so difficult for people to get right. netdev has done a version of this, so has rdma, and it works well. Jason