Skip to contentSkip to conversation

libxencall/linux: prevent page migration for hypercall buffers

PATCH New David Vrabel 1 participant 4 messages Aug 3, 2016 – Oct 24, 2016

lore
Share & tools
lore
#0 lore raw

This is the libxencall side of the "xen/privcmd: prevent page migration for hypercall buffers​" series for Linux. It calls the necessary LOCK/UNLOCK ioctls.

This should not be applied until after the Linux series is applied.

David

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw

PATCH 2/2 libxencall: release the buffer cache before closing the fd

Freeing a buffer may now call the IOCTL_PRIVCMD_HCALL_BUF_UNLOCK ioctl on the fd, so the fd should only be closed after the buffer cache is released.

---
 tools/libs/call/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libs/call/core.c +1 -1
index 5ca0372..578815a 100644
@@ -65,8 +65,8 @@ int xencall_close(xencall_handle *xcall)
     if ( !xcall )
         return 0;
 
-    rc = osdep_xencall_close(xcall);
     buffer_release_cache(xcall);
+    rc = osdep_xencall_close(xcall);
     xtl_logger_destroy(xcall->logger_tofree);
     free(xcall);
     return rc;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH 1/2 libxencall/linux: use LOCK/UNLOCK ioctls for hypercall buffers

In reply to David Vrabel

Using just mlock'd buffers for hypercalls is not sufficient as these are still subject to compaction and page migration. Use the new IOCTL_PRIVCMD_HCALL_BUF_LOCK and IOCTL_PRIVCMD_HCALL_BUF_UNLOCK ioctls provided by the privcmd driver to prevent this.

Since not all kernels support these ioctls, don't repeatedly try these ioctls if they are unsupported.

MAP_LOCKED is still used as this places the pages on the unevictable list avoiding the need for the VM subsystem to scan them.

madvise(.., MADV_DONTFORK) is still required since we still need to prevent children getting CoW mappings of the hypercall buffers.

---
 tools/include/xen-sys/Linux/privcmd.h | 37 +++++++++++++++++++++++++++
 tools/libs/call/linux.c               | 47 ++++++++++++++++++++++++++++++++---
 2 files changed, 81 insertions(+), 3 deletions(-)
tools/include/xen-sys/Linux/privcmd.h +37 -0
index e4e666a..4afb399 100644
@@ -75,6 +75,11 @@ typedef struct privcmd_mmapbatch_v2 {
 	int __user *err;  /* array of error codes */
 } privcmd_mmapbatch_v2_t;
 
+struct privcmd_hcall_buf {
+	void *start;
+	size_t len;
+};
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -89,4 +94,36 @@ typedef struct privcmd_mmapbatch_v2 {
 #define IOCTL_PRIVCMD_MMAPBATCH_V2				\
 	_IOC(_IOC_NONE, 'P', 4, sizeof(privcmd_mmapbatch_v2_t))
 
+/*
+ * @cmd: IOCTL_PRIVCMD_HCALL_BUF_LOCK
+ * @arg: struct privcmd hcall_buf *
+ * Return: 0 on success. On an error, -1 is returned and errno is set
+ * to EINVAL, ENOMEM, or EFAULT.
+ *
+ * Locks a memory buffer so it may be used in a hypercall.  This is
+ * similar to mlock(2) but also prevents compaction/page migration.
+ *
+ * The buffers may have any alignment and size and may overlap other
+ * buffers.
+ *
+ * Locked buffers are unlocked with IOCTL_PRIVCMD_HCALL_BUF_UNLOCK or
+ * by closing the file handle.
+ */
+#define IOCTL_PRIVCMD_HCALL_BUF_LOCK				\
+	_IOC(_IOC_NONE, 'P', 5, sizeof(struct privcmd_hcall_buf))
+
+/*
+ * @cmd: IOCTL_PRIVCMD_HCALL_BUF_UNLOCK
+ * @arg: struct privcmd hcall_buf *
+ * Return: Always 0.
+ *
+ * Unlocks a memory buffer previously locked with
+ * IOCTL_PRIVCMD_HCALL_BUF_LOCK.
+ *
+ * It is not possible to partially unlock a buffer.  i.e., the
+ * LOCK/UNLOCK must be exactly paired.
+ */
+#define IOCTL_PRIVCMD_HCALL_BUF_UNLOCK				\
+	_IOC(_IOC_NONE, 'P', 6, sizeof(struct privcmd_hcall_buf))
+
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
tools/libs/call/linux.c +44 -3

74 lines not on this page · raw message

Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#3 lore raw
In reply to David Vrabel
Quoted 5 lines · David Vrabel

This is the libxencall side of the "xen/privcmd: prevent page migration for hypercall buffers​" series for Linux. It calls the necessary LOCK/UNLOCK ioctls.

This should not be applied until after the Linux series is applied.

This solution wasn't good enough (see the Linux thread for more details).

David

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel