Skip to contentSkip to conversation

tools/foreignmem: Support querying the size of a resource

PATCH In review Andrew Cooper 5 participants 7 messages Jan 28, 2021 – Jan 29, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 tools/foreignmem: Support querying the size of a resource

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 was overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD".

---
CC: Wei Liu <[email protected]>
CC: Paul Durrant <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Juergen Gross <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Michał Leszczyński <[email protected]>
CC: Hubert Jasudowicz <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Manuel Bouyer <[email protected]>

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/[email protected]/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 hypercalls.

---
 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(-)
tools/include/xenforeignmemory.h +15 -0
index d594be8df0..0ab1dd19d3 100644
@@ -179,6 +179,21 @@ xenforeignmemory_resource_handle *xenforeignmemory_map_resource(
 int xenforeignmemory_unmap_resource(
     xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres);
 
+/**
+ * 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 errno
+ * 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
 
 /*
tools/libs/foreignmemory/Makefile +1 -1
index f191cdbed0..0eb9a3a712 100644
@@ -2,7 +2,7 @@ XEN_ROOT = $(CURDIR)/../../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 MAJOR    = 1
-MINOR    = 3
+MINOR    = 4
 
 SRCS-y                 += core.c
 SRCS-$(CONFIG_Linux)   += linux.c
tools/libs/foreignmemory/core.c +18 -0
index 63f12e2450..28ec311af1 100644
@@ -188,6 +188,24 @@ int xenforeignmemory_unmap_resource(
     return rc;
 }
 
+int xenforeignmemory_resource_size(
+    xenforeignmemory_handle *fmem, domid_t domid, unsigned int type,
+    unsigned int id, size_t *size)
+{
+    xenforeignmemory_resource_handle fres = {
+        .domid = domid,
+        .type  = type,
+        .id    = id,
+    };
+    int rc = osdep_xenforeignmemory_map_resource(fmem, &fres);
+
+    if ( rc )
+        return rc;
+
+    *size = fres.nr_frames << PAGE_SHIFT;
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libs/foreignmemory/freebsd.c +15 -3
index 3d403a7cd0..9a2796f0b7 100644
@@ -119,6 +119,10 @@ int osdep_xenforeignmemory_map_resource(xenforeignmemory_handle *fmem,
     };
     int rc;
 
+    if ( !fres->addr && !fres->nr_frames )
+        /* Request for resource size.  Skip mmap(). */
+        goto skip_mmap;
+
     fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
                       fres->prot, fres->flags | MAP_SHARED, fmem->fd, 0);
     if ( fres->addr == MAP_FAILED )
@@ -126,6 +130,7 @@ int osdep_xenforeignmemory_map_resource(xenforeignmemory_handle *fmem,
 
     mr.addr = (uintptr_t)fres->addr;
 
+ skip_mmap:
     rc = ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr);
     if ( rc )
     {
@@ -136,13 +141,20 @@ int osdep_xenforeignmemory_map_resource(xenforeignmemory_handle *fmem,
         else
             errno = EOPNOTSUPP;
 
-        saved_errno = errno;
-        osdep_xenforeignmemory_unmap_resource(fmem, fres);
-        errno = saved_errno;
+        if ( fres->addr )
+        {
+            saved_errno = errno;
+            osdep_xenforeignmemory_unmap_resource(fmem, fres);
+            errno = saved_errno;
+        }
 
         return -1;
     }
 
+    /* If requesting size, copy back. */
+    if ( !fres->addr )
+        fres->nr_frames = mr.num;
+
     return 0;
 }
 
tools/libs/foreignmemory/libxenforeignmemory.map +4 -0
index d5323c87d9..8aca341b99 100644
@@ -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;
tools/libs/foreignmemory/linux.c +15 -3
index fe73d5ab72..d0eead1196 100644
@@ -312,6 +312,10 @@ int osdep_xenforeignmemory_map_resource(
     };
     int rc;
 
+    if ( !fres->addr && !fres->nr_frames )
+        /* Request for resource size.  Skip mmap(). */
+        goto skip_mmap;
+
     fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
                       fres->prot, fres->flags | MAP_SHARED, fmem->fd, 0);
     if ( fres->addr == MAP_FAILED )
@@ -319,6 +323,7 @@ int osdep_xenforeignmemory_map_resource(
 
     mr.addr = (uintptr_t)fres->addr;
 
+ skip_mmap:
     rc = ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr);
     if ( rc )
     {
@@ -329,13 +334,20 @@ int osdep_xenforeignmemory_map_resource(
         else
             errno = EOPNOTSUPP;
 
-        saved_errno = errno;
-        (void)osdep_xenforeignmemory_unmap_resource(fmem, fres);
-        errno = saved_errno;
+        if ( fres->addr )
+        {
+            saved_errno = errno;
+            osdep_xenforeignmemory_unmap_resource(fmem, fres);
+            errno = saved_errno;
+        }
 
         return -1;
     }
 
+    /* If requesting size, copy back. */
+    if ( !fres->addr )
+        fres->nr_frames = mr.num;
+
     return 0;
 }
 
tools/libs/foreignmemory/netbsd.c +20 -1
index d26566f601..4ae60aafdd 100644
@@ -132,6 +132,10 @@ int osdep_xenforeignmemory_map_resource(
     };
     int rc;
 
+    if ( !fres->addr && !fres->nr_frames )
+        /* Request for resource size.  Skip mmap(). */
+        goto skip_mmap;
+
     fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
                       fres->prot, fres->flags | MAP_ANON | MAP_SHARED, -1, 0);
     if ( fres->addr == MAP_FAILED )
@@ -139,13 +143,28 @@ int osdep_xenforeignmemory_map_resource(
 
     mr.addr = (uintptr_t)fres->addr;
 
+ skip_mmap:
     rc = ioctl(fmem->fd, IOCTL_PRIVCMD_MMAP_RESOURCE, &mr);
     if ( rc )
     {
         PERROR("ioctl failed");
+
+        if ( fres->addr )
+        {
+            int saved_errno = errno;
+
+            osdep_xenforeignmemory_unmap_resource(fmem, fres);
+            errno = saved_errno;
+        }
+
+        return -1;
     }
 
-    return rc;
+    /* If requesting size, copy back. */
+    if ( !fres->addr )
+        fres->nr_frames = mr.num;
+
+    return 0;
 }
 
 /*
Signature · 1 line
2.11.0
Reviewed-by on v4 1/1
#1 lore raw
Quoted 20 lines

-----Original Message-----
From: Andrew Cooper <[email protected]>
Sent: 28 January 2021 12:02
To: Xen-devel <[email protected]>
Cc: Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>; Roger Pau Monné <[email protected]>; Juergen Gross <[email protected]>; Ian Jackson <[email protected]>; Michał Leszczyński <[email protected]>; Hubert Jasudowicz <[email protected]>; Tamas K Lengyel <[email protected]>; Manuel Bouyer <[email protected]>
Subject: [PATCH v4] tools/foreignmem: Support querying the size of a resource

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.

Quoted 5 lines

For NetBSD, also fix up the ioctl() error path to issue an unmap(), which was overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD".

Acked-by on v4 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

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.

Quoted 5 lines

For NetBSD, also fix up the ioctl() error path to issue an unmap(), which was overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD".

#3 lore raw
In reply to Andrew Cooper
Quoted 18 lines · Andrew Cooper

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 was overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD". [....]

tools/libs/foreignmemory/netbsd.c +0 -0
index d26566f601..4ae60aafdd 100644

tools/libs/foreignmemory/netbsd.c int osdep_xenforeignmemory_map_resource( · line 140

Quoted 9 lines
tools/libs/foreignmemory/netbsd.c +4 -0
@@ -132,6 +132,10 @@ int osdep_xenforeignmemory_map_resource(
     };
     int rc;
 
+    if ( !fres->addr && !fres->nr_frames )
+        /* Request for resource size.  Skip mmap(). */
+        goto skip_mmap;
+
     fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
                       fres->prot, fres->flags | MAP_ANON | MAP_SHARED, -1, 0);

What happens if fres->addr is not NULL and nr_frames is 0 ? Is it supposed to happen ? Should we assert that fres->addr is NULL when nr_frames is 0 ? Or force fres->addr to NULL when nr_frames is 0 ?

Signature · 3 lines
Manuel Bouyer <[email protected]>
     NetBSD: 26 ans d'experience feront toujours la difference
--
#4 lore raw
Quoted 22 lines · Manuel Bouyer
Quoted 21 lines · Andrew Cooper

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 was overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD". [....]

tools/libs/foreignmemory/netbsd.c +0 -0
index d26566f601..4ae60aafdd 100644
@@ -132,6 +132,10 @@ int osdep_xenforeignmemory_map_resource(
     };
     int rc;
 
Quoted 8 lines

tools/libs/foreignmemory/netbsd.c int osdep_xenforeignmemory_map_resource( · line 140

Quoted 6 lines
tools/libs/foreignmemory/netbsd.c +4 -0
+    if ( !fres->addr && !fres->nr_frames )
+        /* Request for resource size.  Skip mmap(). */
+        goto skip_mmap;
+
     fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
                       fres->prot, fres->flags | MAP_ANON | MAP_SHARED, -1, 0);

What happens if fres->addr is not NULL and nr_frames is 0 ?

mmap would return MAP_FAILED and errno == EINVAL in that case AFAICT on Linux and FreeBSD. NetBSD mmap man page doesn't seem to mention what happens in that case, so the comments below apply to Linux and FreeBSD. Maybe we need to handle this differently for NetBSD?

Quoted 1 line

Is it supposed to happen ?

I think that's fine. Calling osdep_xenforeignmemory_map_resource with nr_frames == 0 is pointing to a bug in the caller, so returning error should be fine.

Quoted 2 lines

Should we assert that fres->addr is NULL when
nr_frames is 0 ? Or force fres->addr to NULL when nr_frames is 0 ?

Doesn't really matter, mmap will return EINVAL if nr_frames == 0 regardless of the value of addr.

Roger.

#5 lore raw
Quoted 36 lines · Roger Pau Monné
Quoted 22 lines · Manuel Bouyer
Quoted 21 lines · Andrew Cooper

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 was overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD". [....]

tools/libs/foreignmemory/netbsd.c +0 -0
index d26566f601..4ae60aafdd 100644
@@ -132,6 +132,10 @@ int osdep_xenforeignmemory_map_resource(
     };
     int rc;
 
Quoted 7 lines

tools/libs/foreignmemory/netbsd.c int osdep_xenforeignmemory_map_resource( · line 140

Quoted 6 lines
tools/libs/foreignmemory/netbsd.c +4 -0
+    if ( !fres->addr && !fres->nr_frames )
+        /* Request for resource size.  Skip mmap(). */
+        goto skip_mmap;
+
     fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
                       fres->prot, fres->flags | MAP_ANON | MAP_SHARED, -1, 0);

What happens if fres->addr is not NULL and nr_frames is 0 ?

mmap would return MAP_FAILED and errno == EINVAL in that case AFAICT on Linux and FreeBSD. NetBSD mmap man page doesn't seem to mention what happens in that case, so the comments below apply to Linux and FreeBSD. Maybe we need to handle this differently for NetBSD?

Quoted 1 line

Is it supposed to happen ?

Quoted 8 lines

I think that's fine. Calling osdep_xenforeignmemory_map_resource with nr_frames == 0 is pointing to a bug in the caller, so returning error should be fine.

Quoted 2 lines

Should we assert that fres->addr is NULL when
nr_frames is 0 ? Or force fres->addr to NULL when nr_frames is 0 ?

Doesn't really matter, mmap will return EINVAL if nr_frames == 0 regardless of the value of addr.

mmap() of 0 is an unconditional failure.  So sayeth POSIX.

For the size request, we don't mmap(), and a pointer of 0 is the signal to Xen.

For the regular mapping, we support both NULL (let the kernel choose), and non-NULL (I want my mapping here please), just like regular mmap().

~Andrew

#6 lore raw
Quoted 46 lines · Andrew Cooper
Quoted 36 lines · Roger Pau Monné
Quoted 22 lines · Manuel Bouyer
Quoted 21 lines · Andrew Cooper

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 was overlooked by c/s 4a64e2bb39 "libs/foreignmemory: Implement on NetBSD". [....]

tools/libs/foreignmemory/netbsd.c +0 -0
index d26566f601..4ae60aafdd 100644
@@ -132,6 +132,10 @@ int osdep_xenforeignmemory_map_resource(
     };
     int rc;
 
Quoted 7 lines

tools/libs/foreignmemory/netbsd.c int osdep_xenforeignmemory_map_resource( · line 140

Quoted 6 lines
tools/libs/foreignmemory/netbsd.c +4 -0
+    if ( !fres->addr && !fres->nr_frames )
+        /* Request for resource size.  Skip mmap(). */
+        goto skip_mmap;
+
     fres->addr = mmap(fres->addr, fres->nr_frames << PAGE_SHIFT,
                       fres->prot, fres->flags | MAP_ANON | MAP_SHARED, -1, 0);

What happens if fres->addr is not NULL and nr_frames is 0 ?

mmap would return MAP_FAILED and errno == EINVAL in that case AFAICT on Linux and FreeBSD. NetBSD mmap man page doesn't seem to mention what happens in that case, so the comments below apply to Linux and FreeBSD. Maybe we need to handle this differently for NetBSD?

Quoted 1 line

Is it supposed to happen ?

Quoted 8 lines

I think that's fine. Calling osdep_xenforeignmemory_map_resource with nr_frames == 0 is pointing to a bug in the caller, so returning error should be fine.

Quoted 2 lines

Should we assert that fres->addr is NULL when
nr_frames is 0 ? Or force fres->addr to NULL when nr_frames is 0 ?

Doesn't really matter, mmap will return EINVAL if nr_frames == 0 regardless of the value of addr.

Quoted 7 lines

mmap() of 0 is an unconditional failure.  So sayeth POSIX.

For the size request, we don't mmap(), and a pointer of 0 is the signal to Xen.

For the regular mapping, we support both NULL (let the kernel choose), and non-NULL (I want my mapping here please), just like regular mmap().

OK, so that's no a bug. Thanks

Signature · 3 lines
Manuel Bouyer <[email protected]>
     NetBSD: 26 ans d'experience feront toujours la difference
--