From patchwork Mon Jul 23 04:59:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiwei Bie X-Patchwork-Id: 10539631 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 902E1112B for ; Mon, 23 Jul 2018 05:01:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7B97328515 for ; Mon, 23 Jul 2018 05:01:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6F77328518; Mon, 23 Jul 2018 05:01:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1848928515 for ; Mon, 23 Jul 2018 05:01:51 +0000 (UTC) Received: from localhost ([::1]:32785 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhSyk-0004Ws-Bt for patchwork-qemu-devel@patchwork.kernel.org; Mon, 23 Jul 2018 01:01:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43748) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhSxa-0003lL-Hv for qemu-devel@nongnu.org; Mon, 23 Jul 2018 01:00:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fhSxX-00050j-8q for qemu-devel@nongnu.org; Mon, 23 Jul 2018 01:00:38 -0400 Received: from mga07.intel.com ([134.134.136.100]:45043) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fhSxW-0004yY-V6 for qemu-devel@nongnu.org; Mon, 23 Jul 2018 01:00:35 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Jul 2018 22:00:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,392,1526367600"; d="scan'208";a="59837285" Received: from debian.sh.intel.com ([10.67.104.228]) by orsmga006.jf.intel.com with ESMTP; 22 Jul 2018 22:00:19 -0700 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:55 +0800 Message-Id: <20180723045956.27521-3-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.100 Subject: [Qemu-devel] [RFC 2/3] vfio: support getting VFIOGroup from groupfd 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch introduces an API to support getting VFIOGroup from groupfd. This is useful when the groupfd is opened and shared by another process via UNIX socket. Signed-off-by: Tiwei Bie --- hw/vfio/common.c | 44 +++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-common.h | 1 + 2 files changed, 45 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 52a05532cd..4c19a33dd5 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1279,6 +1279,30 @@ static void vfio_disconnect_container(VFIOGroup *group) } } +static int vfio_groupfd_to_groupid(int groupfd) +{ + char *tmp, group_path[PATH_MAX], *group_name; + int groupid, len; + + tmp = g_strdup_printf("/proc/self/fd/%d", groupfd); + len = readlink(tmp, group_path, sizeof(group_path)); + g_free(tmp); + + if (len <= 0 || len >= sizeof(group_path)) { + return -1; + } + + group_path[len] = '\0'; + + group_name = g_path_get_basename(group_path); + if (sscanf(group_name, "%d", &groupid) != 1) { + groupid = -1; + } + g_free(group_name); + + return groupid; +} + static VFIOGroup *vfio_init_group(int groupfd, int groupid, AddressSpace *as, Error **errp) { @@ -1373,6 +1397,26 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp) return group; } +VFIOGroup *vfio_get_group_from_fd(int groupfd, AddressSpace *as, Error **errp) +{ + VFIOGroup *group; + int groupid; + + groupid = vfio_groupfd_to_groupid(groupfd); + if (groupid < 0) { + error_setg(errp, "failed to get group id from group fd %d", + groupfd); + return NULL; + } + + group = vfio_find_group(groupid, as, errp); + if (group) { + return group; + } + + return vfio_init_group(groupfd, groupid, as, errp); +} + void vfio_put_group(VFIOGroup *group) { if (!group || !QLIST_EMPTY(&group->device_list)) { diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index a9036929b2..9bb1068a36 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -170,6 +170,7 @@ void vfio_region_exit(VFIORegion *region); void vfio_region_finalize(VFIORegion *region); void vfio_reset_handler(void *opaque); VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp); +VFIOGroup *vfio_get_group_from_fd(int groupfd, AddressSpace *as, Error **errp); void vfio_put_group(VFIOGroup *group); int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vbasedev, Error **errp);