Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp365889rwn; Thu, 8 Sep 2022 02:53:56 -0700 (PDT) X-Google-Smtp-Source: AA6agR5JFdwNDrYZV7UV3W0RjNsu6ho5OaFfvohDuIaeu0dz4Sr5AQRif7kSP2FQ6OGTmD/oCkQ+ X-Received: by 2002:a17:907:70a:b0:741:78ab:dce5 with SMTP id xb10-20020a170907070a00b0074178abdce5mr5682577ejb.527.1662630836050; Thu, 08 Sep 2022 02:53:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662630836; cv=none; d=google.com; s=arc-20160816; b=F5ruzr5h3PdLxym5plLqKBHJjiWqPMZipM0Fa2nV1leiAz9e2b7bun6F6HstlbELVY d4GmmtLd2/goK3kK7zW1J0taZyVYOrhYr5fZEqPIEeMSa9cX7/G5ZhAwBSIw0+9bCrbo BxH12J9zaJpspOQ19QAqffhg2PLVy1pmvQ+a4gA1lJmWDuxfha5wz25M6NGUhOj0fMrH BRQIAhegfxcAcnHOIiTfB4vSia8CmZz9FmTNNydyYiAwTkrdxEN8tqyrJGu2dai4+XHZ dxs3vAlU/Tq+clC3GTkLS6IwVivPB/MBiPEWQZkI4nIgroQneMPfnZQYkJVrisn2jbj6 rIiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:to:content-language:subject:reply-to:user-agent :mime-version:date:message-id:dkim-signature; bh=juN60mjz881n6CsiBEcgXlSOSOlvhxsLXH5xY7MSR4s=; b=oW5plsVheuiqClYCOHFkdG3JhQiX2ve6f8miTshr5t0W0P8KV3azFWSZo5Iyzn2axT XhIynviOsq1IwBA0J0dCUK/br3GIdVI2k5/Lj6j3e/oBCoIX/o7KZr4qwuF51ws83T7S OpfYKso7wJfFlrAhZhUfwUZFg5hjkmZUQmm6DWNFCUOXGlmuBrJ8sZtBQg2meuO9Voj7 jh774/ErwSpy37nQNuqaUkiYbW/eiFyfoDWPF53EIRmjqoAEzcQviAK4g8WjmB+06HlQ Sxw38ovflB310UiG2EiBNvGzELanxTv9+zW4uFaRYKWPCEbc2dwvxqiOdCp6TXNZFeD0 TqJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jFqqwr+3; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pk27-20020a170906d7bb00b007414d1ba434si1434621ejb.537.2022.09.08.02.53.30; Thu, 08 Sep 2022 02:53:56 -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=@redhat.com header.s=mimecast20190719 header.b=jFqqwr+3; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230241AbiIHJjb (ORCPT + 99 others); Thu, 8 Sep 2022 05:39:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229663AbiIHJj1 (ORCPT ); Thu, 8 Sep 2022 05:39:27 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E4B5BBD for ; Thu, 8 Sep 2022 02:39:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662629961; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=juN60mjz881n6CsiBEcgXlSOSOlvhxsLXH5xY7MSR4s=; b=jFqqwr+3UiffNUiezWLfJk39zyNzWGxJevxbArw+CaQ7OXBPGx+YGnQV++GNJFpScr+rUV n+jgZv5UXuo8O48pr6ELS2vhsL3HcLTLE3SeuhScdv8zpvnPFUZmCJX8oTnE34TDf3nTWV podJd5ZTnNg6jhSrPOigpQofftEiuOY= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-347-wfwlIqxyOI6LnGvosDG8tA-1; Thu, 08 Sep 2022 05:39:12 -0400 X-MC-Unique: wfwlIqxyOI6LnGvosDG8tA-1 Received: by mail-wm1-f69.google.com with SMTP id j3-20020a05600c1c0300b003a5e72421c2so1046153wms.1 for ; Thu, 08 Sep 2022 02:39:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:reply-to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date; bh=juN60mjz881n6CsiBEcgXlSOSOlvhxsLXH5xY7MSR4s=; b=P+KJ9lWnHzEn7i3Jv7DmQDIbbRyc9drh2qhAyzl0jk2emzX1IXpnz/Pk6I5G/gax+o 5kC7dYAcccygxMLtqmV24cxq5XGO6fgQuWYvS5FtoD+6U7OnsbZp3n4ZdZXzfIz3OoF4 Myzc0urkolpgRj/4wqunOnbr7YFl1h2EOKLpPSVwXAUMX6mEOzoi0jy7DGua28GcZyZA axYXKh33cItHILVaN1L5UnV89PZd08H5yXtStkjwcBDUGwdwFdL2cFEMS8jlEcS8mIj7 F072ivhVC2NNvhDRAVCYGSUp40Q0NkUEt3MAHaC3MBi/w/26cnAQZdaMZcRo4PbQHcuA dHhQ== X-Gm-Message-State: ACgBeo1cGFZAGiFyIFNVlmMSebMndYmbLeuWhUTbjcjAvpJi7KHhE6KK d+OPhnBfhiAaw1LdE+5k3f3rta9Jdz4Ie8IMIAvCcOn0UhoXaThQjFhlpTExIOc05crvz4i1Cih 6rbobNmk0cFZjK1xLtvGo1925 X-Received: by 2002:a05:6000:178e:b0:220:635f:eb13 with SMTP id e14-20020a056000178e00b00220635feb13mr4489783wrg.634.1662629951466; Thu, 08 Sep 2022 02:39:11 -0700 (PDT) X-Received: by 2002:a05:6000:178e:b0:220:635f:eb13 with SMTP id e14-20020a056000178e00b00220635feb13mr4489754wrg.634.1662629951128; Thu, 08 Sep 2022 02:39:11 -0700 (PDT) Received: from ?IPV6:2a01:e0a:59e:9d80:527b:9dff:feef:3874? ([2a01:e0a:59e:9d80:527b:9dff:feef:3874]) by smtp.gmail.com with ESMTPSA id p14-20020a1c544e000000b003a502c23f2asm2231024wmi.16.2022.09.08.02.39.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 08 Sep 2022 02:39:09 -0700 (PDT) Message-ID: <4c9350cd-c2ce-dc84-9a29-210907d2a2a2@redhat.com> Date: Thu, 8 Sep 2022 11:39:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Reply-To: eric.auger@redhat.com Subject: Re: [PATCH v2 15/15] vfio: Add struct device to vfio_device Content-Language: en-US To: Yi Liu , 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 , Tony Krowiak , Jason Herne , Harald Freudenberger , Diana Craciun , Alex Williamson , Cornelia Huck , Longfang Liu , Shameer Kolothum , Jason Gunthorpe , Yishai Hadas , 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 References: <20220901143747.32858-1-kevin.tian@intel.com> <20220901143747.32858-16-kevin.tian@intel.com> <50d82b01-86a3-e6a3-06f7-7f98e60131eb@redhat.com> <546463b8-54fa-2071-6a9a-e4087eb8bb2c@intel.com> From: Eric Auger In-Reply-To: <546463b8-54fa-2071-6a9a-e4087eb8bb2c@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,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 9/8/22 11:17, Yi Liu wrote: > On 2022/9/8 17:06, Eric Auger wrote: >> Hi Kevin, >> >> On 9/1/22 16:37, Kevin Tian wrote: >>> From: Yi Liu >>> >>> and replace kref. With it a 'vfio-dev/vfioX' node is created under the >>> sysfs path of the parent, indicating the device is bound to a vfio >>> driver, e.g.: >>> >>> /sys/devices/pci0000\:6f/0000\:6f\:01.0/vfio-dev/vfio0 >>> >>> It is also a preparatory step toward adding cdev for supporting future >>> device-oriented uAPI. >>> >>> Add Documentation/ABI/testing/sysfs-devices-vfio-dev. >>> >>> Also take this chance to rename chardev 'vfio' to 'vfio-group' in >>> /proc/devices. >>> >>> Suggested-by: Jason Gunthorpe >>> Signed-off-by: Yi Liu >>> Signed-off-by: Kevin Tian >>> Reviewed-by: Jason Gunthorpe >>> --- >>>   .../ABI/testing/sysfs-devices-vfio-dev        |  8 +++ >>>   drivers/vfio/vfio_main.c                      | 67 >>> +++++++++++++++---- >>>   include/linux/vfio.h                          |  6 +- >>>   3 files changed, 66 insertions(+), 15 deletions(-) >>>   create mode 100644 Documentation/ABI/testing/sysfs-devices-vfio-dev >>> >>> diff --git a/Documentation/ABI/testing/sysfs-devices-vfio-dev >>> b/Documentation/ABI/testing/sysfs-devices-vfio-dev >>> new file mode 100644 >>> index 000000000000..e21424fd9666 >>> --- /dev/null >>> +++ b/Documentation/ABI/testing/sysfs-devices-vfio-dev >>> @@ -0,0 +1,8 @@ >>> +What:         /sys/...//vfio-dev/vfioX/ >>> +Date:         September 2022 >>> +Contact:     Yi Liu >>> +Description: >>> +         This directory is created when the device is bound to a >>> +         vfio driver. The layout under this directory matches what >>> +         exists for a standard 'struct device'. 'X' is a unique >>> +         index marking this device in vfio. >>> diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c >>> index bfa675d314ab..141f55c3faf5 100644 >>> --- a/drivers/vfio/vfio_main.c >>> +++ b/drivers/vfio/vfio_main.c >>> @@ -46,6 +46,8 @@ static struct vfio { >>>       struct mutex            group_lock; /* locks group_list */ >>>       struct ida            group_ida; >>>       dev_t                group_devt; >>> +    struct class            *device_class; >>> +    struct ida            device_ida; >>>   } vfio; >>>     struct vfio_iommu_driver { >>> @@ -483,12 +485,13 @@ static struct vfio_device >>> *vfio_group_get_device(struct vfio_group *group, >>>    * VFIO driver API >>>    */ >>>   /* Release helper called by vfio_put_device() */ >>> -void vfio_device_release(struct kref *kref) >>> +static void vfio_device_release(struct device *dev) >>>   { >>>       struct vfio_device *device = >>> -            container_of(kref, struct vfio_device, kref); >>> +            container_of(dev, struct vfio_device, device); >>>         vfio_release_device_set(device); >>> +    ida_free(&vfio.device_ida, device->index); >>>         /* >>>        * kvfree() cannot be done here due to a life cycle mess in >>> @@ -498,7 +501,6 @@ void vfio_device_release(struct kref *kref) >>>        */ >>>       device->ops->release(device); >>>   } >>> -EXPORT_SYMBOL_GPL(vfio_device_release); >>>     /* >>>    * Alloc and initialize vfio_device so it can be registered to vfio >>> @@ -546,6 +548,13 @@ int vfio_init_device(struct vfio_device >>> *device, struct device *dev, >>>   { >>>       int ret; >>>   +    ret = ida_alloc_max(&vfio.device_ida, MINORMASK, GFP_KERNEL); >>> +    if (ret < 0) { >>> +        dev_dbg(dev, "Error to alloc index\n"); >>> +        return ret; >>> +    } >>> + >>> +    device->index = ret; >>>       init_completion(&device->comp); >>>       device->dev = dev; >>>       device->ops = ops; >>> @@ -556,11 +565,15 @@ int vfio_init_device(struct vfio_device >>> *device, struct device *dev, >>>               goto out_uninit; >>>       } >>>   -    kref_init(&device->kref); >>> +    device_initialize(&device->device); >>> +    device->device.release = vfio_device_release; >>> +    device->device.class = vfio.device_class; >>> +    device->device.parent = device->dev; >>>       return 0; >>>     out_uninit: >>>       vfio_release_device_set(device); >>> +    ida_free(&vfio.device_ida, device->index); >>>       return ret; >>>   } >>>   EXPORT_SYMBOL_GPL(vfio_init_device); >>> @@ -657,6 +670,7 @@ static int __vfio_register_dev(struct >>> vfio_device *device, >>>           struct vfio_group *group) >>>   { >>>       struct vfio_device *existing_device; >>> +    int ret; >>>         if (IS_ERR(group)) >>>           return PTR_ERR(group); >>> @@ -673,16 +687,21 @@ static int __vfio_register_dev(struct >>> vfio_device *device, >>>           dev_WARN(device->dev, "Device already exists on group %d\n", >>>                iommu_group_id(group->iommu_group)); >>>           vfio_device_put_registration(existing_device); >>> -        if (group->type == VFIO_NO_IOMMU || >>> -            group->type == VFIO_EMULATED_IOMMU) >>> -            iommu_group_remove_device(device->dev); >>> -        vfio_group_put(group); >>> -        return -EBUSY; >>> +        ret = -EBUSY; >>> +        goto err_out; >>>       } >>>         /* Our reference on group is moved to the device */ >>>       device->group = group; >>>   +    ret = dev_set_name(&device->device, "vfio%d", device->index); >>> +    if (ret) >>> +        goto err_out; >>> + >>> +    ret = device_add(&device->device); >>> +    if (ret) >>> +        goto err_out; >>> + >>>       /* Refcounting can't start until the driver calls register */ >>>       refcount_set(&device->refcount, 1); >>>   @@ -692,6 +711,12 @@ static int __vfio_register_dev(struct >>> vfio_device *device, >>>       mutex_unlock(&group->device_lock); >>>         return 0; >>> +err_out: >>> +    if (group->type == VFIO_NO_IOMMU || >>> +        group->type == VFIO_EMULATED_IOMMU) >>> +        iommu_group_remove_device(device->dev); >>> +    vfio_group_put(group); >>> +    return ret; >>>   } >>>     int vfio_register_group_dev(struct vfio_device *device) >>> @@ -779,6 +804,9 @@ void vfio_unregister_group_dev(struct >>> vfio_device *device) >>>       group->dev_counter--; >>>       mutex_unlock(&group->device_lock); >>>   +    /* Balances device_add in register path */ >>> +    device_del(&device->device); >>> + >>>       if (group->type == VFIO_NO_IOMMU || group->type == >>> VFIO_EMULATED_IOMMU) >>>           iommu_group_remove_device(device->dev); >>>   @@ -2145,6 +2173,7 @@ static int __init vfio_init(void) >>>       int ret; >>>         ida_init(&vfio.group_ida); >>> +    ida_init(&vfio.device_ida); >>>       mutex_init(&vfio.group_lock); >>>       mutex_init(&vfio.iommu_drivers_lock); >>>       INIT_LIST_HEAD(&vfio.group_list); >>> @@ -2160,12 +2189,20 @@ static int __init vfio_init(void) >>>       vfio.class = class_create(THIS_MODULE, "vfio"); >>>       if (IS_ERR(vfio.class)) { >>>           ret = PTR_ERR(vfio.class); >>> -        goto err_class; >>> +        goto err_group_class; >>>       } >>>         vfio.class->devnode = vfio_devnode; >>>   -    ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, >>> "vfio"); >>> +    /* /sys/class/vfio-dev/vfioX */ >>> +    vfio.device_class = class_create(THIS_MODULE, "vfio-dev"); >>> +    if (IS_ERR(vfio.device_class)) { >>> +        ret = PTR_ERR(vfio.device_class); >>> +        goto err_dev_class; >>> +    } >>> + >>> +    ret = alloc_chrdev_region(&vfio.group_devt, 0, MINORMASK + 1, >>> +                  "vfio-group"); >>>       if (ret) >>>           goto err_alloc_chrdev; >>>   @@ -2181,9 +2218,12 @@ static int __init vfio_init(void) >>>   err_driver_register: >>>       unregister_chrdev_region(vfio.group_devt, MINORMASK + 1); >>>   err_alloc_chrdev: >>> +    class_destroy(vfio.device_class); >>> +    vfio.device_class = NULL; >>> +err_dev_class: >>>       class_destroy(vfio.class); >>>       vfio.class = NULL; >>> -err_class: >>> +err_group_class: >>>       misc_deregister(&vfio_dev); >>>       return ret; >>>   } >>> @@ -2195,8 +2235,11 @@ static void __exit vfio_cleanup(void) >>>   #ifdef CONFIG_VFIO_NOIOMMU >>>       vfio_unregister_iommu_driver(&vfio_noiommu_ops); >>>   #endif >>> +    ida_destroy(&vfio.device_ida); >>>       ida_destroy(&vfio.group_ida); >>>       unregister_chrdev_region(vfio.group_devt, MINORMASK + 1); >>> +    class_destroy(vfio.device_class); >>> +    vfio.device_class = NULL; >>>       class_destroy(vfio.class); >>>       vfio.class = NULL; >>>       misc_deregister(&vfio_dev); >>> diff --git a/include/linux/vfio.h b/include/linux/vfio.h >>> index f03447c8774d..5c13f74da1bb 100644 >>> --- a/include/linux/vfio.h >>> +++ b/include/linux/vfio.h >>> @@ -45,7 +45,8 @@ struct vfio_device { >>>       struct kvm *kvm; >>>         /* Members below here are private, not for driver use */ >>> -    struct kref kref;    /* object life cycle */ >>> +    unsigned int index; >>> +    struct device device;    /* device.kref covers object life >>> circle */ >>>       refcount_t refcount;    /* user count on registered device*/ >>>       unsigned int open_count; >>>       struct completion comp; >> I am not totally clear about remaining 'struct device *dev;' in >> vfio_device struct. I see it used in some places. Is it supposed to >> disappear at some point? > > no, Eric. *dev will not disappear, it stores the dev pointet passed in by > caller of vfio_init_device(). yeah I see but you have device->device.parent = device->dev; Eric > >>> @@ -154,10 +155,9 @@ struct vfio_device *_vfio_alloc_device(size_t >>> size, struct device *dev, >>>   int vfio_init_device(struct vfio_device *device, struct device *dev, >>>                const struct vfio_device_ops *ops); >>>   void vfio_free_device(struct vfio_device *device); >>> -void vfio_device_release(struct kref *kref); >>>   static inline void vfio_put_device(struct vfio_device *device) >>>   { >>> -    kref_put(&device->kref, vfio_device_release); >>> +    put_device(&device->device); >>>   } >>>     int vfio_register_group_dev(struct vfio_device *device); >> >> Thanks >> >> Eric >> >