From nobody Wed Nov 5 02:08:14 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 153232213167448.51746359695528; Sun, 22 Jul 2018 22:02:11 -0700 (PDT) Received: from localhost ([::1]:32784 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhSyv-0004OG-MA for importer@patchew.org; Mon, 23 Jul 2018 01:02:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43704) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhSxM-0003fj-4q for qemu-devel@nongnu.org; Mon, 23 Jul 2018 01:00:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fhSxJ-0004mu-3i for qemu-devel@nongnu.org; Mon, 23 Jul 2018 01:00:24 -0400 Received: from mga03.intel.com ([134.134.136.65]:61814) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fhSxI-0004mc-Qv for qemu-devel@nongnu.org; Mon, 23 Jul 2018 01:00:21 -0400 Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Jul 2018 22:00:19 -0700 Received: from debian.sh.intel.com ([10.67.104.228]) by orsmga006.jf.intel.com with ESMTP; 22 Jul 2018 22:00:17 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,392,1526367600"; d="scan'208";a="59837271" From: Tiwei Bie To: mst@redhat.com, alex.williamson@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org Date: Mon, 23 Jul 2018 12:59:54 +0800 Message-Id: <20180723045956.27521-2-tiwei.bie@intel.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180723045956.27521-1-tiwei.bie@intel.com> References: <20180723045956.27521-1-tiwei.bie@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.65 Subject: [Qemu-devel] [RFC 1/3] vfio: split vfio_get_group() into small functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch splits vfio_get_group() into small functions. It makes it easier to implement other vfio_get_group*() functions in the future. Signed-off-by: Tiwei Bie --- hw/vfio/common.c | 83 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index fb396cf00a..52a05532cd 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1279,37 +1279,20 @@ static void vfio_disconnect_container(VFIOGroup *gr= oup) } } =20 -VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp) +static VFIOGroup *vfio_init_group(int groupfd, int groupid, AddressSpace *= as, + Error **errp) { VFIOGroup *group; - char path[32]; struct vfio_group_status status =3D { .argsz =3D sizeof(status) }; =20 - QLIST_FOREACH(group, &vfio_group_list, next) { - if (group->groupid =3D=3D groupid) { - /* Found it. Now is it already in the right context? */ - if (group->container->space->as =3D=3D as) { - return group; - } else { - error_setg(errp, "group %d used in multiple address spaces= ", - group->groupid); - return NULL; - } - } - } - group =3D g_malloc0(sizeof(*group)); =20 - snprintf(path, sizeof(path), "/dev/vfio/%d", groupid); - group->fd =3D qemu_open(path, O_RDWR); - if (group->fd < 0) { - error_setg_errno(errp, errno, "failed to open %s", path); - goto free_group_exit; - } + group->fd =3D groupfd; + group->groupid =3D groupid; =20 if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) { error_setg_errno(errp, errno, "failed to get group %d status", gro= upid); - goto close_fd_exit; + goto free_group_exit; } =20 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) { @@ -1317,16 +1300,15 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace= *as, Error **errp) error_append_hint(errp, "Please ensure all devices within the iommu_grou= p " "are bound to their vfio bus driver.\n"); - goto close_fd_exit; + goto free_group_exit; } =20 - group->groupid =3D groupid; QLIST_INIT(&group->device_list); =20 if (vfio_connect_container(group, as, errp)) { error_prepend(errp, "failed to setup container for group %d: ", groupid); - goto close_fd_exit; + goto free_group_exit; } =20 if (QLIST_EMPTY(&vfio_group_list)) { @@ -1337,15 +1319,60 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace= *as, Error **errp) =20 return group; =20 -close_fd_exit: - close(group->fd); - free_group_exit: g_free(group); =20 return NULL; } =20 +static VFIOGroup *vfio_find_group(int groupid, AddressSpace *as, + Error **errp) +{ + VFIOGroup *group; + + QLIST_FOREACH(group, &vfio_group_list, next) { + if (group->groupid =3D=3D groupid) { + /* Found it. Now is it already in the right context? */ + if (group->container->space->as =3D=3D as) { + return group; + } else { + error_setg(errp, "group %d used in multiple address spaces= ", + group->groupid); + return NULL; + } + } + } + + return NULL; +} + +VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp) +{ + VFIOGroup *group; + char path[32]; + int groupfd; + + group =3D vfio_find_group(groupid, as, errp); + if (group) { + return group; + } + + snprintf(path, sizeof(path), "/dev/vfio/%d", groupid); + groupfd =3D qemu_open(path, O_RDWR); + if (groupfd < 0) { + error_setg_errno(errp, errno, "failed to open %s", path); + return NULL; + } + + group =3D vfio_init_group(groupfd, groupid, as, errp); + if (!group) { + close(groupfd); + return NULL; + } + + return group; +} + void vfio_put_group(VFIOGroup *group) { if (!group || !QLIST_EMPTY(&group->device_list)) { --=20 2.18.0