Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1194236rwe; Sat, 27 Aug 2022 02:52:43 -0700 (PDT) X-Google-Smtp-Source: AA6agR6Whbey6ulMxPVigPVIhpQfVS5o0ZoR+dlTiI8d73aXykFG3B4o5FiWslplHc9NexIjV25Z X-Received: by 2002:aa7:cdc8:0:b0:447:f0cf:8de with SMTP id h8-20020aa7cdc8000000b00447f0cf08demr4794331edw.131.1661593963078; Sat, 27 Aug 2022 02:52:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661593963; cv=none; d=google.com; s=arc-20160816; b=UL+2EbWnvGvTllJDr+30yVA0GRb78Hq0uP9m2VcH9YeUuPyuVYarVeu8oncsatEh21 LvnhJ2yC1IT+0B8qt+BXgYo2WEjTanTGNhdZcEUZ1cqh9aDBxhXQWnezbieDrE2fFseF iv+nBFCLETwZu/cN+LhsPwxuDExf1wmZo/7K0KjiF0aYPm0nK7zB7kD/mA/rHAW1PP4g EiuKZ+xlmS3R/IdjpGfZSnV/rkJggXc6vvZpNoGmN8t9zaeIdThdKXx/zxT2NhiKNXIg iGq0S5NNqoMpx238reYVGGj01Qm1vynPUmX4bzbYlbDgvktXYnwf6IfoqAkFYPLMo00n HzTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7vOaRULcJ7QECIa0awF153KXqTFZAbQMLIUwE03xFBw=; b=unDEYkYh23wZg3Q61edEyvyO/0S+G4uBNxcGkL6Vug4NnqfcHSeZd4UUFcNFmScAc2 hrSfAatpT1546fM8BMYpzvJba9CAxVrKZH372CIPXAOZefSl6YFh8WZXlzQJdfSF8/t7 BgHzbaLuMIPfFkT2H2H1wrM3WvSNU+JeWXLBw2hsZ4/tutS7zUo+YV0EbY2YIQyNbvbr wyRJQqefQFan7zN+DZjtQbf6ClCeGABWe6Z+bdQ+cMHSSm79vqMSafqh9xNPSD/VhPNX vRQzPmAtdoiuSTpFZaJnMl0345suA/6+eiuRt6p+26k2hmoVyzAmY6+vOB0MCx7exv/p vnxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BJcXwUap; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f16-20020a0564021e9000b0044630d162d3si3258968edf.584.2022.08.27.02.52.17; Sat, 27 Aug 2022 02:52:43 -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=@intel.com header.s=Intel header.b=BJcXwUap; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234350AbiH0JuP (ORCPT + 99 others); Sat, 27 Aug 2022 05:50:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231391AbiH0JuL (ORCPT ); Sat, 27 Aug 2022 05:50:11 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 915FC792D7; Sat, 27 Aug 2022 02:50:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1661593809; x=1693129809; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pZ2n2dr9aQVhIM3k3w4/+FkGW9/EiD1RXEbKITXctZU=; b=BJcXwUap081J+k8XJjy5prxno4BW4YmJjpLZ4bIHvY5QIGE6aGez8G7n y6oVnKTB7gcgP1Dho78ZlOEySd1YqHFd9RDgKJcG/XA1YnBXZ/akW7wcb YiBJ5m4yDKBxwuPiGxkzWQ69DjIWGFs9urywGyvBEiLYvsJQ3lg40XODF ZCuxdNpIYxI9mnpkktBtoJUEj/eHZW/sYMpDkAolb3EoO64bN7LLBflXQ 0eEDeS13zz1qVBndGvD8Zot+YbHoKh5B/Kjc3HKLN+mut+LFfu4/iSshu X5nLltvZU7vs8Af/DAC/OkvwQZudEYW3u9alu3icKO21CWwiI8XzIvXR6 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10451"; a="293395993" X-IronPort-AV: E=Sophos;i="5.93,267,1654585200"; d="scan'208";a="293395993" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Aug 2022 02:50:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,267,1654585200"; d="scan'208";a="640353801" Received: from sqa-gate.sh.intel.com (HELO michael.clx.dev.tsp.org) ([10.239.48.212]) by orsmga008.jf.intel.com with ESMTP; 27 Aug 2022 02:49:55 -0700 From: Kevin Tian To: 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 , Kevin Tian , 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 Cc: Yi Liu Subject: [PATCH 02/15] vfio/pci: Use the new device life cycle helpers Date: Sun, 28 Aug 2022 01:10:24 +0800 Message-Id: <20220827171037.30297-3-kevin.tian@intel.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20220827171037.30297-1-kevin.tian@intel.com> References: <20220827171037.30297-1-kevin.tian@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_06_12, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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-kernel@vger.kernel.org From: Yi Liu Also introduce two pci core helpers as @init/@release for pci drivers: - vfio_pci_core_init_dev() - vfio_pci_core_release_dev() Signed-off-by: Yi Liu Signed-off-by: Kevin Tian --- drivers/vfio/pci/vfio_pci.c | 20 +++++++++--------- drivers/vfio/pci/vfio_pci_core.c | 35 ++++++++++++++++++++++++++++++++ include/linux/vfio_pci_core.h | 2 ++ 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 4d1a97415a27..c1223c458615 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -127,6 +127,8 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev) static const struct vfio_device_ops vfio_pci_ops = { .name = "vfio-pci", + .init = vfio_pci_core_init_dev, + .release = vfio_pci_core_release_dev, .open_device = vfio_pci_open_device, .close_device = vfio_pci_core_close_device, .ioctl = vfio_pci_core_ioctl, @@ -146,20 +148,19 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (vfio_pci_is_denylisted(pdev)) return -EINVAL; - vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); - if (!vdev) - return -ENOMEM; - vfio_pci_core_init_device(vdev, pdev, &vfio_pci_ops); + vdev = vfio_alloc_device(vfio_pci_core_device, vdev, &pdev->dev, + &vfio_pci_ops); + if (IS_ERR(vdev)) + return PTR_ERR(vdev); dev_set_drvdata(&pdev->dev, vdev); ret = vfio_pci_core_register_device(vdev); if (ret) - goto out_free; + goto out_put_vdev; return 0; -out_free: - vfio_pci_core_uninit_device(vdev); - kfree(vdev); +out_put_vdev: + vfio_put_device(&vdev->vdev); return ret; } @@ -168,8 +169,7 @@ static void vfio_pci_remove(struct pci_dev *pdev) struct vfio_pci_core_device *vdev = dev_get_drvdata(&pdev->dev); vfio_pci_core_unregister_device(vdev); - vfio_pci_core_uninit_device(vdev); - kfree(vdev); + vfio_put_device(&vdev->vdev); } static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index c8d3b0450fb3..708b61d1b364 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1825,6 +1825,41 @@ static void vfio_pci_vga_uninit(struct vfio_pci_core_device *vdev) VGA_RSRC_LEGACY_MEM); } +int vfio_pci_core_init_dev(struct vfio_device *core_vdev) +{ + struct vfio_pci_core_device *vdev = + container_of(core_vdev, struct vfio_pci_core_device, vdev); + + vdev->pdev = to_pci_dev(core_vdev->dev); + vdev->irq_type = VFIO_PCI_NUM_IRQS; + mutex_init(&vdev->igate); + spin_lock_init(&vdev->irqlock); + mutex_init(&vdev->ioeventfds_lock); + INIT_LIST_HEAD(&vdev->dummy_resources_list); + INIT_LIST_HEAD(&vdev->ioeventfds_list); + mutex_init(&vdev->vma_lock); + INIT_LIST_HEAD(&vdev->vma_list); + INIT_LIST_HEAD(&vdev->sriov_pfs_item); + init_rwsem(&vdev->memory_lock); + + return 0; +} +EXPORT_SYMBOL_GPL(vfio_pci_core_init_dev); + +void vfio_pci_core_release_dev(struct vfio_device *core_vdev) +{ + struct vfio_pci_core_device *vdev = + container_of(core_vdev, struct vfio_pci_core_device, vdev); + + mutex_destroy(&vdev->igate); + mutex_destroy(&vdev->ioeventfds_lock); + mutex_destroy(&vdev->vma_lock); + kfree(vdev->region); + kfree(vdev->pm_save); + vfio_free_device(core_vdev); +} +EXPORT_SYMBOL_GPL(vfio_pci_core_release_dev); + void vfio_pci_core_init_device(struct vfio_pci_core_device *vdev, struct pci_dev *pdev, const struct vfio_device_ops *vfio_pci_ops) diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 5579ece4347b..98c8c66e2400 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -233,6 +233,8 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev); void vfio_pci_core_init_device(struct vfio_pci_core_device *vdev, struct pci_dev *pdev, const struct vfio_device_ops *vfio_pci_ops); +int vfio_pci_core_init_dev(struct vfio_device *core_vdev); +void vfio_pci_core_release_dev(struct vfio_device *core_vdev); int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev); void vfio_pci_core_uninit_device(struct vfio_pci_core_device *vdev); void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev); -- 2.21.3