From nobody Wed Nov 27 13:00:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1699531423; cv=none; d=zohomail.com; s=zohoarc; b=kYcxj/+eULgKUJc/vUpZJ3LvKdbKYeDDw7LxtR6zdwU1Kd4kYyvN0fkrGwV989wtM6MDzFJbmamc0NEsdEVmkLnkOc5IepfZym+NQRYzJr6BjIeCPS/PPIMTmzZ5RbwJ3F500xf6sGxOaZgSyardPrPdonZeMu6FM8M0A7Vywss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699531423; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ybDgJnXbrSeWOXbNCQ0zk6cEiYO/D+i/WTN5Y8FQ0CU=; b=gz2CTi5L4iS4wshLOLbBZytYs0ZBrd0KQnqhS9sSvth8Hv2kJ3nqP1eX/uDx3vX8JjMvHHhek4hjK/MSXNeTr8X1JhHDRfSh6GyR2gmEgiu13r50r8AfR3kke3q9megjxegxHPEarJVqwZ1BVCJYOlS6WPt4KyaEzMQwQPReM4s= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1699531423683248.527613306253; Thu, 9 Nov 2023 04:03:43 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r13jR-000269-Ac; Thu, 09 Nov 2023 07:01:57 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r13jG-0001uS-Es for qemu-devel@nongnu.org; Thu, 09 Nov 2023 07:01:48 -0500 Received: from mgamail.intel.com ([192.55.52.136]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r13jD-0001gn-L6 for qemu-devel@nongnu.org; Thu, 09 Nov 2023 07:01:45 -0500 Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Nov 2023 04:01:42 -0800 Received: from duan-server-s2600bt.bj.intel.com ([10.240.192.147]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Nov 2023 04:01:38 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699531303; x=1731067303; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/FVO90dmacO3PUCbL9JOBbZNyydyIi7kvmZeqZfjEvk=; b=ERmoBheHbqEIoxo1ntI3jZHfo7tOcPqcDunlSNzB4Pgux8TmxvbR01hw b5pYoj6r/b/saIZAroTAgDzZ5vC6BqVCmaiVYlJGYBCq0wuALCwC73xmx YyTt22TAfJqZ26WhYvpmbfQPH7slanW7mUTD3ie6obKVfW46y0o5BzwPy EeWUc9UX0bIuENarbEtqe0SO4jCQspOZuLO5m1lU0oFJ7eZ/7zFdXfwc1 JjflWL+eVaxpktSYl45/yg3l7WRcy6/NGnDrmWnbK0dxbvdy+ki9msIZr 712rwMAc4RdhU1JkA5Voj9X7iHE5A1nPOz3wZ5Ts7U3DEYFh+j6A3YZ8G g==; X-IronPort-AV: E=McAfee;i="6600,9927,10888"; a="369305859" X-IronPort-AV: E=Sophos;i="6.03,289,1694761200"; d="scan'208";a="369305859" X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.03,289,1694761200"; d="scan'208";a="11516105" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, kevin.tian@intel.com, yi.l.liu@intel.com, yi.y.sun@intel.com, chao.p.peng@intel.com, Zhenzhong Duan Subject: [PATCH v5 12/20] vfio/platform: Make vfio cdev pre-openable by passing a file handle Date: Thu, 9 Nov 2023 19:45:21 +0800 Message-Id: <20231109114529.1904193-13-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231109114529.1904193-1-zhenzhong.duan@intel.com> References: <20231109114529.1904193-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=192.55.52.136; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1699531424803100002 Content-Type: text/plain; charset="utf-8" This gives management tools like libvirt a chance to open the vfio cdev with privilege and pass FD to qemu. This way qemu never needs to have privilege to open a VFIO or iommu cdev node. Signed-off-by: Zhenzhong Duan --- hw/vfio/platform.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c index 86e176ee97..59a50b1598 100644 --- a/hw/vfio/platform.c +++ b/hw/vfio/platform.c @@ -36,6 +36,7 @@ #include "hw/platform-bus.h" #include "hw/qdev-properties.h" #include "sysemu/kvm.h" +#include "monitor/monitor.h" =20 /* * Functions used whatever the injection method @@ -530,14 +531,13 @@ static VFIODeviceOps vfio_platform_ops =3D { */ static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp) { - struct stat st; int ret; =20 - /* @sysfsdev takes precedence over @host */ - if (vbasedev->sysfsdev) { + /* @fd takes precedence over @sysfsdev which takes precedence over @ho= st */ + if (vbasedev->fd < 0 && vbasedev->sysfsdev) { g_free(vbasedev->name); vbasedev->name =3D g_path_get_basename(vbasedev->sysfsdev); - } else { + } else if (vbasedev->fd < 0) { if (!vbasedev->name || strchr(vbasedev->name, '/')) { error_setg(errp, "wrong host device name"); return -EINVAL; @@ -547,10 +547,9 @@ static int vfio_base_device_init(VFIODevice *vbasedev,= Error **errp) vbasedev->name); } =20 - if (stat(vbasedev->sysfsdev, &st) < 0) { - error_setg_errno(errp, errno, - "failed to get the sysfs host device file status"= ); - return -errno; + ret =3D vfio_device_get_name(vbasedev, errp); + if (ret) { + return ret; } =20 ret =3D vfio_attach_device(vbasedev->name, vbasedev, @@ -657,6 +656,28 @@ static Property vfio_platform_dev_properties[] =3D { DEFINE_PROP_END_OF_LIST(), }; =20 +static void vfio_platform_instance_init(Object *obj) +{ + VFIOPlatformDevice *vdev =3D VFIO_PLATFORM_DEVICE(obj); + + vdev->vbasedev.fd =3D -1; +} + +#ifdef CONFIG_IOMMUFD +static void vfio_platform_set_fd(Object *obj, const char *str, Error **err= p) +{ + VFIOPlatformDevice *vdev =3D VFIO_PLATFORM_DEVICE(obj); + int fd =3D -1; + + fd =3D monitor_fd_param(monitor_cur(), str, errp); + if (fd =3D=3D -1) { + error_prepend(errp, "Could not parse remote object fd %s:", str); + return; + } + vdev->vbasedev.fd =3D fd; +} +#endif + static void vfio_platform_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); @@ -664,6 +685,9 @@ static void vfio_platform_class_init(ObjectClass *klass= , void *data) =20 dc->realize =3D vfio_platform_realize; device_class_set_props(dc, vfio_platform_dev_properties); +#ifdef CONFIG_IOMMUFD + object_class_property_add_str(klass, "fd", NULL, vfio_platform_set_fd); +#endif dc->vmsd =3D &vfio_platform_vmstate; dc->desc =3D "VFIO-based platform device assignment"; sbc->connect_irq_notifier =3D vfio_start_irqfd_injection; @@ -676,6 +700,7 @@ static const TypeInfo vfio_platform_dev_info =3D { .name =3D TYPE_VFIO_PLATFORM, .parent =3D TYPE_SYS_BUS_DEVICE, .instance_size =3D sizeof(VFIOPlatformDevice), + .instance_init =3D vfio_platform_instance_init, .class_init =3D vfio_platform_class_init, .class_size =3D sizeof(VFIOPlatformDeviceClass), }; --=20 2.34.1