From nobody Thu May 2 10:06:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1611835356; cv=none; d=zohomail.com; s=zohoarc; b=VWHtL5hctBkA+XUly/TQuNSs6iY6UA3bxhh9VbIcKokEAMS5SnCq6ZM0/dVCcacrQkRPkfa3BvBM63Ci1Aqs0g7fCry6v0QoJQDFxG2il1+lXeKKLxMLy3vqCdH5fyJiI6Ci3ZVCERPD+Q2NOLtd74iYLOX9qcQ3nE0sPEUD7QA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611835356; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=+F/ywAIc1YxapwKY9Q3tfJ5i7bnybzAcud868fhMMvI=; b=XU7oxe3AGI1v+qfTIm3WZEf/tjukf03NSCXveV5MhQ5L/UD/RfwnX1AaEUxXmCSxq4mTV94ykSvkWiuvXVLYnF9U4NpxtINWBLL7a7MqWI99x1OyTv/mPo3muW4C2KAhUf1KRNTMf47mG/uLJc3iLxIKvnthW8YDW+yQxLlTE4Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1611835356163214.39672680693354; Thu, 28 Jan 2021 04:02:36 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.76970.139157 (Exim 4.92) (envelope-from ) id 1l5605-0006iQ-U4; Thu, 28 Jan 2021 12:02:13 +0000 Received: by outflank-mailman (output) from mailman id 76970.139157; Thu, 28 Jan 2021 12:02:13 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l5605-0006iJ-Qj; Thu, 28 Jan 2021 12:02:13 +0000 Received: by outflank-mailman (input) for mailman id 76970; Thu, 28 Jan 2021 12:02:12 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1l5604-0006iE-9Q for xen-devel@lists.xenproject.org; Thu, 28 Jan 2021 12:02:12 +0000 Received: from esa6.hc3370-68.iphmx.com (unknown [216.71.155.175]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8bb4e74d-00ad-4160-b7d6-4bf30690cc29; Thu, 28 Jan 2021 12:02:10 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 8bb4e74d-00ad-4160-b7d6-4bf30690cc29 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1611835330; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=YNbZ86esMvcGT7tjDvXP+K2Apsd+TVJMsW1oTRfUtJU=; b=DzTnkb+0dUjHJjDbt1+Rx64BZ/w5Ko3OWYbdwCK7Yd2EIV4iTzZGXlGF xsWFNWGBLh2Ax1YS0Knq7uNWQJGQ32WriUkFg7QvbnPPOWUMmGtH1OaUE 3JYYSUWWb023Rs240xnXfGXsLz+HGPMpQIjVaIo+9VW1KEj0+vqHnAh6P U=; Authentication-Results: esa6.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: DbzTGMTtxSheKBtHJvcDXpDeDvNOtA1NzEtz37Kek/jGDo30h+TtuYhWyIK4x5+qcFtxhuVQSr SkiyZDKfPxKwvS8wZxNaJaXe0SKogMDXkZvT4X7yBlyJqJxa2/gBXvKWiPk5aCFOmzpjfjpJfH xp5bBiqRaaq5ALRUFZVa7shD0vfShkozANAPMovfgvEcH8F5tAoSd88Bgf4QczxA4gS91qQjoM YVl9VNDH7TYypORCBzwvx3zWdb89nRDPN1q+4QD875lZfZcuKLMf950pRWzAPTKwKIHe/1c/W3 CC4= X-SBRS: 5.1 X-MesageID: 36255448 X-Ironport-Server: esa6.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.79,382,1602561600"; d="scan'208";a="36255448" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Wei Liu , "Paul Durrant" , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Juergen Gross , Ian Jackson , =?UTF-8?q?Micha=C5=82=20Leszczy=C5=84ski?= , Hubert Jasudowicz , Tamas K Lengyel , Manuel Bouyer Subject: [PATCH v4] tools/foreignmem: Support querying the size of a resource Date: Thu, 28 Jan 2021 12:01:52 +0000 Message-ID: <20210128120152.9908-1-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) With the Xen side of this interface (soon to be) fixed to return real sizes, userspace needs to be able to make the query. Introduce xenforeignmemory_resource_size() for the purpose, bumping the library minor version. Update both all osdep_xenforeignmemory_map_resource() implementations to understand size requests, skip the mmap() operation, and copy back the nr_frames field. For NetBSD, also fix up the ioctl() error path to issue an unmap(), which w= as overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD". Signed-off-by: Andrew Cooper Reviewed-by: Roger Pau Monn=C3=A9 Acked-by: Wei Liu Reviewed-by: Paul Durrant --- CC: Wei Liu CC: Paul Durrant CC: Roger Pau Monn=C3=A9 CC: Juergen Gross CC: Ian Jackson CC: Micha=C5=82 Leszczy=C5=84ski CC: Hubert Jasudowicz CC: Tamas K Lengyel CC: Manuel Bouyer This depends on a bugfix to the Linux IOCTL to understand size requests and pass them on to Xen. https://lore.kernel.org/xen-devel/20210112115358.23346-1-roger.pau@citrix= .com/T/#u v4: * Rebase over NetBSD support being added. v3: * Rewrite from scratch, to avoid breaking restricted domid situations. In particular, we cannot open a xencall interface and issue blind hypercall= s. --- tools/include/xenforeignmemory.h | 15 +++++++++++++++ tools/libs/foreignmemory/Makefile | 2 +- tools/libs/foreignmemory/core.c | 18 ++++++++++++++++++ tools/libs/foreignmemory/freebsd.c | 18 +++++++++++++++--- tools/libs/foreignmemory/libxenforeignmemory.map | 4 ++++ tools/libs/foreignmemory/linux.c | 18 +++++++++++++++--- tools/libs/foreignmemory/netbsd.c | 21 ++++++++++++++++++++- 7 files changed, 88 insertions(+), 8 deletions(-) diff --git a/tools/include/xenforeignmemory.h b/tools/include/xenforeignmem= ory.h index d594be8df0..0ab1dd19d3 100644 --- a/tools/include/xenforeignmemory.h +++ b/tools/include/xenforeignmemory.h @@ -179,6 +179,21 @@ xenforeignmemory_resource_handle *xenforeignmemory_map= _resource( int xenforeignmemory_unmap_resource( xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres); =20 +/** + * Determine the maximum size of a specific resource. + * + * @parm fmem handle to the open foreignmemory interface + * @parm domid the domain id + * @parm type the resource type + * @parm id the type-specific resource identifier + * + * Return 0 on success and fills in *size, with a value in bytes. Sets er= rno + * and return -1 on error. + */ +int xenforeignmemory_resource_size( + xenforeignmemory_handle *fmem, domid_t domid, unsigned int type, + unsigned int id, size_t *size); + #endif =20 /* diff --git a/tools/libs/foreignmemory/Makefile b/tools/libs/foreignmemory/M= akefile index f191cdbed0..0eb9a3a712 100644 --- a/tools/libs/foreignmemory/Makefile +++ b/tools/libs/foreignmemory/Makefile @@ -2,7 +2,7 @@ XEN_ROOT =3D $(CURDIR)/../../.. include $(XEN_ROOT)/tools/Rules.mk =20 MAJOR =3D 1 -MINOR =3D 3 +MINOR =3D 4 =20 SRCS-y +=3D core.c SRCS-$(CONFIG_Linux) +=3D linux.c diff --git a/tools/libs/foreignmemory/core.c b/tools/libs/foreignmemory/cor= e.c index 63f12e2450..28ec311af1 100644 --- a/tools/libs/foreignmemory/core.c +++ b/tools/libs/foreignmemory/core.c @@ -188,6 +188,24 @@ int xenforeignmemory_unmap_resource( return rc; } =20 +int xenforeignmemory_resource_size( + xenforeignmemory_handle *fmem, domid_t domid, unsigned int type, + unsigned int id, size_t *size) +{ + xenforeignmemory_resource_handle fres =3D { + .domid =3D domid, + .type =3D type, + .id =3D id, + }; + int rc =3D osdep_xenforeignmemory_map_resource(fmem, &fres); + + if ( rc ) + return rc; + + *size =3D fres.nr_frames << PAGE_SHIFT; + return 0; +} + /* * Local variables: * mode: C diff --git a/tools/libs/foreignmemory/freebsd.c b/tools/libs/foreignmemory/= freebsd.c index 3d403a7cd0..9a2796f0b7 100644 --- a/tools/libs/foreignmemory/freebsd.c +++ b/tools/libs/foreignmemory/freebsd.c @@ -119,6 +119,10 @@ int osdep_xenforeignmemory_map_resource(xenforeignmemo= ry_handle *fmem, }; int rc; =20 + if ( !fres->addr && !fres->nr_frames ) + /* Request for resource size. Skip mmap(). */ + goto skip_mmap; + fres->addr =3D mmap(fres->addr, fres->nr_frames << PAGE_SHIFT, fres->prot, fres->flags | MAP_SHARED, fmem->fd, 0); if ( fres->addr =3D=3D MAP_FAILED ) @@ -126,6 +130,7 @@ int osdep_xenforeignmemory_map_resource(xenforeignmemor= y_handle *fmem, =20 mr.addr =3D (uintptr_t)fres->addr; =20 + skip_mmap: rc =3D ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr); if ( rc ) { @@ -136,13 +141,20 @@ int osdep_xenforeignmemory_map_resource(xenforeignmem= ory_handle *fmem, else errno =3D EOPNOTSUPP; =20 - saved_errno =3D errno; - osdep_xenforeignmemory_unmap_resource(fmem, fres); - errno =3D saved_errno; + if ( fres->addr ) + { + saved_errno =3D errno; + osdep_xenforeignmemory_unmap_resource(fmem, fres); + errno =3D saved_errno; + } =20 return -1; } =20 + /* If requesting size, copy back. */ + if ( !fres->addr ) + fres->nr_frames =3D mr.num; + return 0; } =20 diff --git a/tools/libs/foreignmemory/libxenforeignmemory.map b/tools/libs/= foreignmemory/libxenforeignmemory.map index d5323c87d9..8aca341b99 100644 --- a/tools/libs/foreignmemory/libxenforeignmemory.map +++ b/tools/libs/foreignmemory/libxenforeignmemory.map @@ -19,3 +19,7 @@ VERS_1.3 { xenforeignmemory_map_resource; xenforeignmemory_unmap_resource; } VERS_1.2; +VERS_1.4 { + global: + xenforeignmemory_resource_size; +} VERS_1.3; diff --git a/tools/libs/foreignmemory/linux.c b/tools/libs/foreignmemory/li= nux.c index fe73d5ab72..d0eead1196 100644 --- a/tools/libs/foreignmemory/linux.c +++ b/tools/libs/foreignmemory/linux.c @@ -312,6 +312,10 @@ int osdep_xenforeignmemory_map_resource( }; int rc; =20 + if ( !fres->addr && !fres->nr_frames ) + /* Request for resource size. Skip mmap(). */ + goto skip_mmap; + fres->addr =3D mmap(fres->addr, fres->nr_frames << PAGE_SHIFT, fres->prot, fres->flags | MAP_SHARED, fmem->fd, 0); if ( fres->addr =3D=3D MAP_FAILED ) @@ -319,6 +323,7 @@ int osdep_xenforeignmemory_map_resource( =20 mr.addr =3D (uintptr_t)fres->addr; =20 + skip_mmap: rc =3D ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr); if ( rc ) { @@ -329,13 +334,20 @@ int osdep_xenforeignmemory_map_resource( else errno =3D EOPNOTSUPP; =20 - saved_errno =3D errno; - (void)osdep_xenforeignmemory_unmap_resource(fmem, fres); - errno =3D saved_errno; + if ( fres->addr ) + { + saved_errno =3D errno; + osdep_xenforeignmemory_unmap_resource(fmem, fres); + errno =3D saved_errno; + } =20 return -1; } =20 + /* If requesting size, copy back. */ + if ( !fres->addr ) + fres->nr_frames =3D mr.num; + return 0; } =20 diff --git a/tools/libs/foreignmemory/netbsd.c b/tools/libs/foreignmemory/n= etbsd.c index d26566f601..4ae60aafdd 100644 --- a/tools/libs/foreignmemory/netbsd.c +++ b/tools/libs/foreignmemory/netbsd.c @@ -132,6 +132,10 @@ int osdep_xenforeignmemory_map_resource( }; int rc; =20 + if ( !fres->addr && !fres->nr_frames ) + /* Request for resource size. Skip mmap(). */ + goto skip_mmap; + fres->addr =3D mmap(fres->addr, fres->nr_frames << PAGE_SHIFT, fres->prot, fres->flags | MAP_ANON | MAP_SHARED, -1,= 0); if ( fres->addr =3D=3D MAP_FAILED ) @@ -139,13 +143,28 @@ int osdep_xenforeignmemory_map_resource( =20 mr.addr =3D (uintptr_t)fres->addr; =20 + skip_mmap: rc =3D ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr); if ( rc ) { PERROR("ioctl failed"); + + if ( fres->addr ) + { + int saved_errno =3D errno; + + osdep_xenforeignmemory_unmap_resource(fmem, fres); + errno =3D saved_errno; + } + + return -1; } =20 - return rc; + /* If requesting size, copy back. */ + if ( !fres->addr ) + fres->nr_frames =3D mr.num; + + return 0; } =20 /* --=20 2.11.0