Skip to contentSkip to conversation

xen/privcmd: support for paged-out frames

RFC PATCH Superseded David Vrabel 5 participants 17 messages Aug 23, 2012 – Aug 24, 2012

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover xen/privcmd: support for paged-out frames

This series is a straight forward-port of some functionality from classic kernels to support Xen hosts that do paging of guests.

This isn't functionality the XenServer makes use of so I've not tested these with paging in use (GridCentric requested that our older kernels supported this and I'm just doing the forward port).

I'm not entirely happy about the approach used here because:

1. It relies on the meaning of the return code of the update_mmu hypercall and it assumes the value Xen used for -ENOENT is the same the kernel uses. This does not appear to be a formal part of the hypercall ABI.

Keir, can you comment on this?

2. It seems more sensible to have the kernel do the retries instead of libxc doing them. The kernel has to have a mechanism for this any way (for mapping back/front rings).

3. The current way of handling paged-out frames by repeatedly retrying is a bit lame. Shouldn't there be some event that the guest waiting for the frame can wait on instead? By moving the retry mechanism into the kernel we can change this without impacting the ABI to userspace.

David

#1 lore raw

PATCH 1/3 xen/mm: return more precise error from xen_remap_domain_range()

From: David Vrabel <[email protected]>

Callers of xen_remap_domain_range() need to know if the remap failed because frame is currently paged out. So they can retry the remap later on. Return -ENOENT in this case.

This assumes that the error codes returned by Xen are a subset of those used by the kernel. It is unclear if this is defined as part of the hypercall ABI.

---
 arch/x86/xen/mmu.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/xen/mmu.c +2 -2
index b65a761..fb187ea 100644
@@ -2355,8 +2355,8 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 		if (err)
 			goto out;
 
-		err = -EFAULT;
-		if (HYPERVISOR_mmu_update(mmu_update, batch, NULL, domid) < 0)
+		err = HYPERVISOR_mmu_update(mmu_update, batch, NULL, domid);
+		if (err < 0)
 			goto out;
 
 		nr -= batch;
Signature · 1 line
1.7.2.5
#2 lore raw

PATCH 2/3 xen/privcmd: report paged-out frames in PRIVCMD_MMAPBATCH ioctl

In reply to David Vrabel

From: David Vrabel <[email protected]>

libxc handles paged-out frames in xc_map_foreign_bulk() and friends by retrying the map operation. libxc expects the PRIVCMD_MMAPBATCH ioctl to report paged out frames by setting bit 31 in the mfn.

Do this for the PRIVCMD_MMAPBATCH ioctl if
xen_remap_domain_mfn_range() returned -ENOENT.

---
 drivers/xen/privcmd.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
drivers/xen/privcmd.c +8 -3
index ccee0f1..f8c1b6d 100644
@@ -255,10 +255,15 @@ static int mmap_batch_fn(void *data, void *state)
 {
 	xen_pfn_t *mfnp = data;
 	struct mmap_batch_state *st = state;
+	int ret;
 
-	if (xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-				       st->vma->vm_page_prot, st->domain) < 0) {
-		*mfnp |= 0xf0000000U;
+	ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
+					 st->vma->vm_page_prot, st->domain);
+	if (ret < 0) {
+		if (ret == -ENOENT)
+			*mfnp |= 0x80000000U;
+		else
+			*mfnp |= 0xf0000000U;
 		st->err++;
 	}
 	st->va += PAGE_SIZE;
Signature · 1 line
1.7.2.5
#5 lore raw

PATCH 3/3 xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

In reply to David Vrabel

From: David Vrabel <[email protected]>

PRIVCMD_MMAPBATCH_V2 extends PRIVCMD_MMAPBATCH with an additional field for reporting the error code for every frame that could not be mapped. libxc prefers PRIVCMD_MMAPBATCH_V2 over PRIVCMD_MMAPBATCH.

---
 drivers/xen/privcmd.c |   54 +++++++++++++++++++++++++++++++++++++++----------
 include/xen/privcmd.h |   10 +++++++++
 2 files changed, 53 insertions(+), 11 deletions(-)
drivers/xen/privcmd.c +43 -11
index f8c1b6d..4f97160 100644
@@ -248,7 +248,8 @@ struct mmap_batch_state {
 	struct vm_area_struct *vma;
 	int err;
 
-	xen_pfn_t __user *user;
+	xen_pfn_t __user *user_mfn;
+	int __user *user_err;
 };
 
 static int mmap_batch_fn(void *data, void *state)
@@ -275,34 +276,58 @@ static int mmap_return_errors(void *data, void *state)
 {
 	xen_pfn_t *mfnp = data;
 	struct mmap_batch_state *st = state;
+	int ret = 0;
 
-	return put_user(*mfnp, st->user++);
+	if (st->user_err) {
+		if ((*mfnp & 0xf0000000U) == 0xf0000000U)
+			ret = -ENOENT;
+		else if ((*mfnp & 0xf0000000U) == 0x80000000U)
+			ret = -EINVAL;
+		else
+			ret = 0;
+		return __put_user(ret, st->user_err);
+	} else
+		return __put_user(*mfnp, st->user_mfn++);
 }
 
 static struct vm_operations_struct privcmd_vm_ops;
 
-static long privcmd_ioctl_mmap_batch(void __user *udata)
+static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
 {
 	int ret;
-	struct privcmd_mmapbatch m;
+	struct privcmd_mmapbatch_v2 m;
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long nr_pages;
 	LIST_HEAD(pagelist);
 	struct mmap_batch_state state;
 
+	printk("%s(%d)\n", __func__, version);
+
 	if (!xen_initial_domain())
 		return -EPERM;
 
-	if (copy_from_user(&m, udata, sizeof(m)))
-		return -EFAULT;
+	if (version == 1) {
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;
+		m.err = NULL;
+	} else {
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_READ, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
+			return -EFAULT;
+	}
 
 	nr_pages = m.num;
 	if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
 		return -EINVAL;
 
 	ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t),
-			   m.arr);
+			   (xen_pfn_t *)m.arr);
 
 	if (ret || list_empty(&pagelist))
 		goto out;
@@ -331,10 +356,11 @@ static long privcmd_ioctl_mmap_batch(void __user *udata)
 	up_write(&mm->mmap_sem);
 
 	if (state.err > 0) {
-		state.user = m.arr;
+		state.user_mfn = (xen_pfn_t *)m.arr;
+		state.user_err = m.err;
 		ret = traverse_pages(m.num, sizeof(xen_pfn_t),
-			       &pagelist,
-			       mmap_return_errors, &state);
+				     &pagelist,
+				     mmap_return_errors, &state);
 	}
 
 out:
@@ -359,7 +385,13 @@ static long privcmd_ioctl(struct file *file,
 		break;
 
 	case IOCTL_PRIVCMD_MMAPBATCH:
-		ret = privcmd_ioctl_mmap_batch(udata);
+		ret = privcmd_ioctl_mmap_batch(udata, 1);
+		printk("%s() batch ret = %d\n", __func__, ret);
+		break;
+
+	case IOCTL_PRIVCMD_MMAPBATCH_V2:
+		ret = privcmd_ioctl_mmap_batch(udata, 2);
+		printk("%s() batch ret = %d\n", __func__, ret);
 		break;
 
 	default:
include/xen/privcmd.h +10 -0
index 17857fb..9fa27c4 100644
@@ -62,6 +62,14 @@ struct privcmd_mmapbatch {
 	xen_pfn_t __user *arr; /* array of mfns - top nibble set on err */
 };
 
+struct privcmd_mmapbatch_v2 {
+	unsigned int num; /* number of pages to populate */
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */
+};
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -73,5 +81,7 @@ struct privcmd_mmapbatch {
 	_IOC(_IOC_NONE, 'P', 2, sizeof(struct privcmd_mmap))
 #define IOCTL_PRIVCMD_MMAPBATCH					\
 	_IOC(_IOC_NONE, 'P', 3, sizeof(struct privcmd_mmapbatch))
+#define IOCTL_PRIVCMD_MMAPBATCH_V2				\
+	_IOC(_IOC_NONE, 'P', 4, sizeof(struct privcmd_mmapbatch_v2))
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
Signature · 1 line
1.7.2.5
#6 lore raw

Re: [PATCH 3/3] xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

Quoted 28 lines · David Vrabel

From: David Vrabel <[email protected]>

PRIVCMD_MMAPBATCH_V2 extends PRIVCMD_MMAPBATCH with an additional field for reporting the error code for every frame that could not be mapped. libxc prefers PRIVCMD_MMAPBATCH_V2 over PRIVCMD_MMAPBATCH.

---
 drivers/xen/privcmd.c |   54 +++++++++++++++++++++++++++++++++++++++----------
 include/xen/privcmd.h |   10 +++++++++
 2 files changed, 53 insertions(+), 11 deletions(-)
drivers/xen/privcmd.c +2 -1
index f8c1b6d..4f97160 100644
@@ -248,7 +248,8 @@ struct mmap_batch_state {
 	struct vm_area_struct *vma;
 	int err;
 
-	xen_pfn_t __user *user;
+	xen_pfn_t __user *user_mfn;
+	int __user *user_err;
 };
 
 static int mmap_batch_fn(void *data, void *state)
@@ -275,34 +276,58 @@ static int mmap_return_errors(void *data, void *state)
 {

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 285

Quoted 10 lines
drivers/xen/privcmd.c +6 -1
 	xen_pfn_t *mfnp = data;
 	struct mmap_batch_state *st = state;
+	int ret = 0;
 
-	return put_user(*mfnp, st->user++);
+	if (st->user_err) {
+		if ((*mfnp & 0xf0000000U) == 0xf0000000U)
+			ret = -ENOENT;
+		else if ((*mfnp & 0xf0000000U) == 0x80000000U)
+			ret = -EINVAL;

Yikes. Any way those 0xf000.. and 0x8000 can at least be #defined

Quoted 13 lines
drivers/xen/privcmd.c +6 -1
+		else
+			ret = 0;
+		return __put_user(ret, st->user_err);
+	} else
+		return __put_user(*mfnp, st->user_mfn++);
 }
 
 static struct vm_operations_struct privcmd_vm_ops;
 
-static long privcmd_ioctl_mmap_batch(void __user *udata)
+static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
 {
 	int ret;

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 306

Quoted 10 lines
drivers/xen/privcmd.c +3 -1
-	struct privcmd_mmapbatch m;
+	struct privcmd_mmapbatch_v2 m;
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long nr_pages;
 	LIST_HEAD(pagelist);
 	struct mmap_batch_state state;
 
+	printk("%s(%d)\n", __func__, version);
+

Hehe.

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 314

Quoted 10 lines
drivers/xen/privcmd.c +5 -2
 	if (!xen_initial_domain())
 		return -EPERM;
 
-	if (copy_from_user(&m, udata, sizeof(m)))
-		return -EFAULT;
+	if (version == 1) {
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;

That is new..

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 316

Quoted 2 lines
drivers/xen/privcmd.c +2 -0
+		m.err = NULL;
+	} else {

Not elseif (version == 2) ?
.. what if version 3 comes around?

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 322

Quoted 6 lines
drivers/xen/privcmd.c +6 -0
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_READ, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
+			return -EFAULT;

I think the VERIFY_WRITE can cover both versions?

Quoted 28 lines
drivers/xen/privcmd.c +6 -4
+	}
 
 	nr_pages = m.num;
 	if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
 		return -EINVAL;
 
 	ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t),
-			   m.arr);
+			   (xen_pfn_t *)m.arr);
 
 	if (ret || list_empty(&pagelist))
 		goto out;
@@ -331,10 +356,11 @@ static long privcmd_ioctl_mmap_batch(void __user *udata)
 	up_write(&mm->mmap_sem);
 
 	if (state.err > 0) {
-		state.user = m.arr;
+		state.user_mfn = (xen_pfn_t *)m.arr;
+		state.user_err = m.err;
 		ret = traverse_pages(m.num, sizeof(xen_pfn_t),
-			       &pagelist,
-			       mmap_return_errors, &state);
+				     &pagelist,
+				     mmap_return_errors, &state);
 	}
 
 out:

drivers/xen/privcmd.c static long privcmd_ioctl(struct file *file, · line 389

Quoted 6 lines
drivers/xen/privcmd.c +2 -1
@@ -359,7 +385,13 @@ static long privcmd_ioctl(struct file *file,
 		break;
 
 	case IOCTL_PRIVCMD_MMAPBATCH:
-		ret = privcmd_ioctl_mmap_batch(udata);
+		ret = privcmd_ioctl_mmap_batch(udata, 1);
+		printk("%s() batch ret = %d\n", __func__, ret);

Pfff...

Quoted 13 lines
drivers/xen/privcmd.c +5 -0
+		break;
+
+	case IOCTL_PRIVCMD_MMAPBATCH_V2:
+		ret = privcmd_ioctl_mmap_batch(udata, 2);
+		printk("%s() batch ret = %d\n", __func__, ret);
 		break;
 
 	default:
include/xen/privcmd.h +0 -0
index 17857fb..9fa27c4 100644

include/xen/privcmd.h struct privcmd_mmapbatch { · line 66

Quoted 5 lines
include/xen/privcmd.h +2 -0
@@ -62,6 +62,14 @@ struct privcmd_mmapbatch {
 	xen_pfn_t __user *arr; /* array of mfns - top nibble set on err */
 };
 
+struct privcmd_mmapbatch_v2 {
+	unsigned int num; /* number of pages to populate */

unsigend int? Not 'u32'?

include/xen/privcmd.h struct privcmd_mmapbatch { · line 70

Quoted 4 lines
include/xen/privcmd.h +4 -0
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */

int? Not a specific type?

Quoted 15 lines
include/xen/privcmd.h +4 -0
+};
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -73,5 +81,7 @@ struct privcmd_mmapbatch {
 	_IOC(_IOC_NONE, 'P', 2, sizeof(struct privcmd_mmap))
 #define IOCTL_PRIVCMD_MMAPBATCH					\
 	_IOC(_IOC_NONE, 'P', 3, sizeof(struct privcmd_mmapbatch))
+#define IOCTL_PRIVCMD_MMAPBATCH_V2				\
+	_IOC(_IOC_NONE, 'P', 4, sizeof(struct privcmd_mmapbatch_v2))
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
Signature · 1 line
1.7.2.5
#13 lore raw
In reply to David Vrabel
Quoted 6 lines · David Vrabel

This series is a straight forward-port of some functionality from classic kernels to support Xen hosts that do paging of guests.

This isn't functionality the XenServer makes use of so I've not tested these with paging in use (GridCentric requested that our older kernels supported this and I'm just doing the forward port).

Thanks for this series. Very timely. I may add that we are not the only consumers of paging. This functionality was first added into classic kernels by Olaf Hering from Suse (added to cc).

Quoted 9 lines

I'm not entirely happy about the approach used here because:

1. It relies on the meaning of the return code of the update_mmu hypercall and it assumes the value Xen used for -ENOENT is the same the kernel uses. This does not appear to be a formal part of the hypercall ABI.

Keir, can you comment on this?

I see your point. I may add that it's likely to be more pervasive than just relying on ENOENT being 12. Which is a fairly safe bet.

Quoted 9 lines

2. It seems more sensible to have the kernel do the retries instead of libxc doing them. The kernel has to have a mechanism for this any way (for mapping back/front rings).

3. The current way of handling paged-out frames by repeatedly retrying is a bit lame. Shouldn't there be some event that the guest waiting for the frame can wait on instead? By moving the retry mechanism into the kernel we can change this without impacting the ABI to userspace.

Lame is an interesting choice of language :)

I am not a huge fan of libxc retry, but we've been pounding it quite hard for a while and it works -- and, importantly, it yields the scheduler :)

While kernel retry may benefit from hypothetical code reuse, "Shouldn't there be some event that the guest waiting for the frame can wait on instead?" will need to become concrete to start a real discussion.

For better or worse, since xen-4.1 (!) libxc will do the right thing if fed the appropriate errno.

Let me re-iterate that this is great work. Thanks
Andres

Quoted 3 lines

David

#3 lore raw

Re: [PATCH 2/3] xen/privcmd: report paged-out frames in PRIVCMD_MMAPBATCH ioctl

In reply to David Vrabel
Quoted 22 lines · David Vrabel

From: David Vrabel <[email protected]>

libxc handles paged-out frames in xc_map_foreign_bulk() and friends by retrying the map operation. libxc expects the PRIVCMD_MMAPBATCH ioctl to report paged out frames by setting bit 31 in the mfn.

Do this for the PRIVCMD_MMAPBATCH ioctl if
xen_remap_domain_mfn_range() returned -ENOENT.

---
drivers/xen/privcmd.c | 11 ++++++++---

1 files changed, 8 insertions(+), 3 deletions(-)
drivers/xen/privcmd.c +0 -0
index ccee0f1..f8c1b6d 100644
@@ -255,10 +255,15 @@ static int mmap_batch_fn(void *data, void *state)
 {
 	xen_pfn_t *mfnp = data;
 	struct mmap_batch_state *st = state;

drivers/xen/privcmd.c static int mmap_batch_fn(void *data, void *state) · line 264

Quoted 10 lines
drivers/xen/privcmd.c +6 -3
+	int ret;
 
-	if (xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-				       st->vma->vm_page_prot, st->domain) < 0) {
-		*mfnp |= 0xf0000000U;
+	ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
+					 st->vma->vm_page_prot, st->domain);
+	if (ret < 0) {
+		if (ret == -ENOENT)
+			*mfnp |= 0x80000000U;

As Konrad pointed out separately, constants here would be great.

These two constants are defined in <xen>/include/public/domctl.h. In particular, the 0x80..0 constant is defined there but *not* used by the hypervisor. I don't see a problem in (re)defining it in the linux interface headers shared between libxc and the kernel -- this is where it really belongs. It could be even phased out of domctl.h in 4.2 (unlikely) or 4.3.

As for the 0xf0..0 constant I have no strong opinion -- pre-existing problem.

drivers/xen/privcmd.c static int mmap_batch_fn(void *data, void *state) · line 269

Quoted 5 lines
drivers/xen/privcmd.c +2 -0
+		else
+			*mfnp |= 0xf0000000U;
 		st->err++;
 	}
 	st->va += PAGE_SIZE;

Libxc expects errno to be ENOENT if at least one map hypercall returned ENOENT. So you need to keep an extra latch in the state, e.g. st->enoents. Or recycle st->err to be a tristate (ok, error, enoent), since I don't see any actual use of err count. Whichever way, privcmd_ioctl_mmap_batch needs to find out if it needs to return ENOENT. Note this requirement also extends to PRIVCMD_MMAPBATCH_V2.

Andres

Quoted 3 lines
Signature · 1 line
1.7.2.5
#12 lore raw

Re: [PATCH 3/3] xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

In reply to David Vrabel
Quoted 28 lines · David Vrabel

From: David Vrabel <[email protected]>

PRIVCMD_MMAPBATCH_V2 extends PRIVCMD_MMAPBATCH with an additional field for reporting the error code for every frame that could not be mapped. libxc prefers PRIVCMD_MMAPBATCH_V2 over PRIVCMD_MMAPBATCH.

---

drivers/xen/privcmd.c |   54 +++++++++++++++++++++++++++++++++++++++----------
include/xen/privcmd.h |   10 +++++++++
2 files changed, 53 insertions(+), 11 deletions(-)
drivers/xen/privcmd.c +2 -1
index f8c1b6d..4f97160 100644
@@ -248,7 +248,8 @@ struct mmap_batch_state {
 	struct vm_area_struct *vma;
 	int err;
 
-	xen_pfn_t __user *user;
+	xen_pfn_t __user *user_mfn;
+	int __user *user_err;
 };
 
 static int mmap_batch_fn(void *data, void *state)
@@ -275,34 +276,58 @@ static int mmap_return_errors(void *data, void *state)
 {

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 285

Quoted 10 lines
drivers/xen/privcmd.c +6 -1
 	xen_pfn_t *mfnp = data;
 	struct mmap_batch_state *st = state;
+	int ret = 0;
 
-	return put_user(*mfnp, st->user++);
+	if (st->user_err) {
+		if ((*mfnp & 0xf0000000U) == 0xf0000000U)
+			ret = -ENOENT;
+		else if ((*mfnp & 0xf0000000U) == 0x80000000U)
+			ret = -EINVAL;

Wires crossed. 0x80..0 is ENOENT, 0xf0..0 is EINVAL. Really in need of constants.

Quoted 12 lines
drivers/xen/privcmd.c +6 -1
+		else
+			ret = 0;
+		return __put_user(ret, st->user_err);
+	} else
+		return __put_user(*mfnp, st->user_mfn++);
 }
 
 static struct vm_operations_struct privcmd_vm_ops;
 
-static long privcmd_ioctl_mmap_batch(void __user *udata)
+static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
 {

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 305

Quoted 10 lines
drivers/xen/privcmd.c +2 -1
 	int ret;
-	struct privcmd_mmapbatch m;
+	struct privcmd_mmapbatch_v2 m;
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long nr_pages;
 	LIST_HEAD(pagelist);
 	struct mmap_batch_state state;
 
+	printk("%s(%d)\n", __func__, version);

Surely this and other unconditional printk's to go away in next round…

Thanks
Andres

Quoted 91 lines
drivers/xen/privcmd.c +20 -6
+
 	if (!xen_initial_domain())
 		return -EPERM;
 
-	if (copy_from_user(&m, udata, sizeof(m)))
-		return -EFAULT;
+	if (version == 1) {
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;
+		m.err = NULL;
+	} else {
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_READ, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
+			return -EFAULT;
+	}
 
 	nr_pages = m.num;
 	if ((m.num <= 0) || (nr_pages > (LONG_MAX >> PAGE_SHIFT)))
 		return -EINVAL;
 
 	ret = gather_array(&pagelist, m.num, sizeof(xen_pfn_t),
-			   m.arr);
+			   (xen_pfn_t *)m.arr);
 
 	if (ret || list_empty(&pagelist))
 		goto out;
@@ -331,10 +356,11 @@ static long privcmd_ioctl_mmap_batch(void __user *udata)
 	up_write(&mm->mmap_sem);
 
 	if (state.err > 0) {
-		state.user = m.arr;
+		state.user_mfn = (xen_pfn_t *)m.arr;
+		state.user_err = m.err;
 		ret = traverse_pages(m.num, sizeof(xen_pfn_t),
-			       &pagelist,
-			       mmap_return_errors, &state);
+				     &pagelist,
+				     mmap_return_errors, &state);
 	}
 

out:

drivers/xen/privcmd.c +7 -1
@@ -359,7 +385,13 @@ static long privcmd_ioctl(struct file *file,
 		break;
 
 	case IOCTL_PRIVCMD_MMAPBATCH:
-		ret = privcmd_ioctl_mmap_batch(udata);
+		ret = privcmd_ioctl_mmap_batch(udata, 1);
+		printk("%s() batch ret = %d\n", __func__, ret);
+		break;
+
+	case IOCTL_PRIVCMD_MMAPBATCH_V2:
+		ret = privcmd_ioctl_mmap_batch(udata, 2);
+		printk("%s() batch ret = %d\n", __func__, ret);
 		break;
 
 	default:
include/xen/privcmd.h +10 -0
index 17857fb..9fa27c4 100644
@@ -62,6 +62,14 @@ struct privcmd_mmapbatch {
 	xen_pfn_t __user *arr; /* array of mfns - top nibble set on err */
 };
 
+struct privcmd_mmapbatch_v2 {
+	unsigned int num; /* number of pages to populate */
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */
+};
+
 /*
 * @cmd: IOCTL_PRIVCMD_HYPERCALL
 * @arg: &privcmd_hypercall_t
@@ -73,5 +81,7 @@ struct privcmd_mmapbatch {
 	_IOC(_IOC_NONE, 'P', 2, sizeof(struct privcmd_mmap))
 #define IOCTL_PRIVCMD_MMAPBATCH					\
 	_IOC(_IOC_NONE, 'P', 3, sizeof(struct privcmd_mmapbatch))
+#define IOCTL_PRIVCMD_MMAPBATCH_V2				\
+	_IOC(_IOC_NONE, 'P', 4, sizeof(struct privcmd_mmapbatch_v2))
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
Signature · 1 line
1.7.2.5
#7 lore raw

Re: [PATCH 3/3] xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

In reply to Konrad Rzeszutek Wilk
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · David Vrabel

From: David Vrabel <[email protected]>

PRIVCMD_MMAPBATCH_V2 extends PRIVCMD_MMAPBATCH with an additional field for reporting the error code for every frame that could not be mapped. libxc prefers PRIVCMD_MMAPBATCH_V2 over PRIVCMD_MMAPBATCH.

[...]

Quoted 10 lines
Quoted 10 lines
drivers/xen/privcmd.c +1 -0
index f8c1b6d..4f97160 100644
@@ -275,34 +276,58 @@ static int mmap_return_errors(void *data, void *state)
 {
 	xen_pfn_t *mfnp = data;
 	struct mmap_batch_state *st = state;
+	int ret = 0;
 
Quoted 8 lines

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 285

Quoted 6 lines
drivers/xen/privcmd.c +5 -1
-	return put_user(*mfnp, st->user++);
+	if (st->user_err) {
+		if ((*mfnp & 0xf0000000U) == 0xf0000000U)
+			ret = -ENOENT;
+		else if ((*mfnp & 0xf0000000U) == 0x80000000U)
+			ret = -EINVAL;

Yikes. Any way those 0xf000.. and 0x8000 can at least be #defined

Agreed.

Quoted 21 lines
Quoted 21 lines
drivers/xen/privcmd.c +7 -2
+		else
+			ret = 0;
+		return __put_user(ret, st->user_err);
+	} else
+		return __put_user(*mfnp, st->user_mfn++);
 }
 
 static struct vm_operations_struct privcmd_vm_ops;
 
-static long privcmd_ioctl_mmap_batch(void __user *udata)
+static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
 {
 	int ret;
-	struct privcmd_mmapbatch m;
+	struct privcmd_mmapbatch_v2 m;
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long nr_pages;
 	LIST_HEAD(pagelist);
 	struct mmap_batch_state state;
 
Quoted 4 lines

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 306

Quoted 2 lines
drivers/xen/privcmd.c +2 -0
+	printk("%s(%d)\n", __func__, version);
+

Hehe.

Heh. I didn't polish up these patches as I wasn't sure this new interface was useful.

Quoted 12 lines

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 314

Quoted 10 lines
drivers/xen/privcmd.c +5 -2
 	if (!xen_initial_domain())
 		return -EPERM;
 
-	if (copy_from_user(&m, udata, sizeof(m)))
-		return -EFAULT;
+	if (version == 1) {
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;

That is new..

We use access_ok() here so we can use the less expensive __get_user() and __put_user() later on.

Quoted 8 lines

drivers/xen/privcmd.c static int mmap_return_errors(void *data, void *state) · line 322

Quoted 6 lines
drivers/xen/privcmd.c +6 -0
+		if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch_v2)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_READ, m.arr, m.num * sizeof(*m.arr)))
+			return -EFAULT;
+		if (!access_ok(VERIFY_WRITE, m.err, m.num * (sizeof(*m.err))))
+			return -EFAULT;

I think the VERIFY_WRITE can cover both versions?

Yes, VERIFY_WRITE is a superset of VERIFY_READ. In v1, m.arr is read/write but in v2, m.arr is const so we use VERIFY_READ so the userspace buffer can reside in a read-only section.

Quoted 10 lines

include/xen/privcmd.h struct privcmd_mmapbatch { · line 66

Quoted 8 lines
include/xen/privcmd.h +2 -0
@@ -62,6 +62,14 @@ struct privcmd_mmapbatch {
 	xen_pfn_t __user *arr; /* array of mfns - top nibble set on err */
 };
 
+struct privcmd_mmapbatch_v2 {
+	unsigned int num; /* number of pages to populate */

unsigend int? Not 'u32'?

Quoted 6 lines

include/xen/privcmd.h struct privcmd_mmapbatch { · line 70

Quoted 4 lines
include/xen/privcmd.h +4 -0
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */

int? Not a specific type?

It's an existing interface supported by classic Xen kernels and currently being used by libxc. So while I agree that it's not the best interface, I don't think it can be changed.

David

#8 lore raw

Re: [PATCH 3/3] xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

Quoted 11 lines
Quoted 10 lines

include/xen/privcmd.h struct privcmd_mmapbatch { · line 66

Quoted 2 lines
include/xen/privcmd.h +2 -0
+struct privcmd_mmapbatch_v2 {
+	unsigned int num; /* number of pages to populate */

unsigend int? Not 'u32'?

include/xen/privcmd.h struct privcmd_mmapbatch { · line 70

Quoted 4 lines
include/xen/privcmd.h +4 -0
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */

int? Not a specific type?

Quoted 3 lines

It's an existing interface supported by classic Xen kernels and currently being used by libxc. So while I agree that it's not the best interface, I don't think it can be changed.

How does it work with a 64-bit dom0 and 32-bit userspace? Is the libxc smart enough to figure out the size of the structure?

Quoted 2 lines

David

#9 lore raw

Re: [PATCH 3/3] xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 11 lines
Quoted 10 lines

include/xen/privcmd.h struct privcmd_mmapbatch { · line 66

Quoted 2 lines
include/xen/privcmd.h +2 -0
+struct privcmd_mmapbatch_v2 {
+	unsigned int num; /* number of pages to populate */

unsigend int? Not 'u32'?

include/xen/privcmd.h struct privcmd_mmapbatch { · line 70

Quoted 4 lines
include/xen/privcmd.h +4 -0
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */

int? Not a specific type?

Quoted 6 lines
Quoted 3 lines

It's an existing interface supported by classic Xen kernels and currently being used by libxc. So while I agree that it's not the best interface, I don't think it can be changed.

How does it work with a 64-bit dom0 and 32-bit userspace? Is the libxc smart enough to figure out the size of the structure?

This already doesn't work for numerous reasons.

The man one being that libxc will make 32 bit hypercalls, but when the kernel forwards them on they look like 64 bit ones.

Ian.

#14 lore raw
In reply to Andres Lagar-Cavilla
Quoted 5 lines · Andres Lagar-Cavilla
Quoted 3 lines · David Vrabel

This series is a straight forward-port of some functionality from classic kernels to support Xen hosts that do paging of guests.

Quoted 7 lines
Quoted 3 lines

This isn't functionality the XenServer makes use of so I've not tested these with paging in use (GridCentric requested that our older kernels supported this and I'm just doing the forward port).

Thanks for this series. Very timely. I may add that we are not the only consumers of paging. This functionality was first added into classic kernels by Olaf Hering from Suse (added to cc).

Sure.

Quoted 17 lines
Quoted 8 lines

I'm not entirely happy about the approach used here because:

1. It relies on the meaning of the return code of the update_mmu hypercall and it assumes the value Xen used for -ENOENT is the same the kernel uses. This does not appear to be a formal part of the hypercall ABI.

Keir, can you comment on this?

I see your point. I may add that it's likely to be more pervasive than just relying on ENOENT being 12. Which is a fairly safe bet.

Quoted 5 lines

2. It seems more sensible to have the kernel do the retries instead of libxc doing them. The kernel has to have a mechanism for this any way (for mapping back/front rings).

Quoted 6 lines
Quoted 4 lines

3. The current way of handling paged-out frames by repeatedly retrying is a bit lame. Shouldn't there be some event that the guest waiting for the frame can wait on instead? By moving the retry mechanism into the kernel we can change this without impacting the ABI to userspace.

Lame is an interesting choice of language :)

My embedded background makes me frown at anything that polls -- it's generally bad for power consumption.

Quoted 7 lines

I am not a huge fan of libxc retry, but we've been pounding it quite hard for a while and it works -- and, importantly, it yields the scheduler :)

While kernel retry may benefit from hypothetical code reuse, "Shouldn't there be some event that the guest waiting for the frame can wait on instead?" will need to become concrete to start a real discussion.

In kernel retries don't require the event. Using the event is something to consider in the longer term.

Quoted 2 lines

For better or worse, since xen-4.1 (!) libxc will do the right thing if fed the appropriate errno.

At a minimum I think we need to fix the existing interface to have the behavior libxc expects.

Is PRIVCMD_MMAPBATCH_V2 actually required? It doesn't seem to do much more than the V1 interface. Perhaps the fix in patches #1 and #2 sufficient to make libxc work?

David

#10 lore raw

Re: [PATCH 3/3] xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

In reply to Konrad Rzeszutek Wilk
Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 11 lines
Quoted 10 lines

include/xen/privcmd.h struct privcmd_mmapbatch { · line 66

Quoted 2 lines
include/xen/privcmd.h +2 -0
+struct privcmd_mmapbatch_v2 {
+	unsigned int num; /* number of pages to populate */

unsigend int? Not 'u32'?

include/xen/privcmd.h struct privcmd_mmapbatch { · line 70

Quoted 4 lines
include/xen/privcmd.h +4 -0
+	domid_t dom;      /* target domain */
+	__u64 addr;       /* virtual address */
+	const xen_pfn_t __user *arr; /* array of mfns */
+	int __user *err;  /* array of error codes */

int? Not a specific type?

Quoted 3 lines
Quoted 3 lines

It's an existing interface supported by classic Xen kernels and currently being used by libxc. So while I agree that it's not the best interface, I don't think it can be changed.

It's also the same as struct privcmd_mmapbatch except for the extra 'err' field and 'arr' being const.

Quoted 2 lines

How does it work with a 64-bit dom0 and 32-bit userspace? Is the libxc smart enough to figure out the size of the structure?

privcmd doesn't support compat ioctls because there there is nothing doing the translation of the hypercalls from the 32-bit to 64-bit ABI -- the hypervisor won't do it as the hypercalls are called from the 64-bit kernel.

64 bit Xen, 64 bit dom0, 32 bit tools has never worked for this reason.
I think Ian Campbell had some hacky patches for this but he may not
want to admit to them. ;)

David

#11 lore raw

Re: [PATCH 3/3] xen/privcmd: add PRIVCMD_MMAPBATCH_V2 ioctl

Quoted 3 lines · David Vrabel

64 bit Xen, 64 bit dom0, 32 bit tools has never worked for this reason.
I think Ian Campbell had some hacky patches for this but he may not
want to admit to them. ;)

They were pretty nasty.

I think I made a 32-bit hypercall entry path available to 64 bit kernels (via int 0x8?) so privcmd used that for 32 bit tasks. Then there was some hacking to make the hypercall compat arg translation area work for 64 bit guests (I don't remember what I did here, I seem to recall it wasn't pretty)

I eventually fell at the hurdle of getting 64 bit blktap kernel module to work with 32 bit tapdisk process (since the ring layouts are different). Not an impossible problem but at this poiint I couldn't be bothered any more.

Quoted 2 lines

David

#15 lore raw
In reply to David Vrabel
Quoted 3 lines · David Vrabel

Is PRIVCMD_MMAPBATCH_V2 actually required? It doesn't seem to do much more than the V1 interface. Perhaps the fix in patches #1 and #2 sufficient to make libxc work?

The V1 interface has some hideous misfeature wrt error reporting iirc.

It doesn't report the actual per-frame error code, just the "an error" signal through setting the top nibble (already a nasty interface!), IIRC

Aha: http://xenbits.xen.org/hg/linux-2.6.18-xen.hg/rev/6d6c3dd995c0

It was knackered on 64 bit (actually set bits 28-31...)...

Ian.

#16 lore raw
Quoted 11 lines · Ian Campbell
Quoted 3 lines · David Vrabel

Is PRIVCMD_MMAPBATCH_V2 actually required? It doesn't seem to do much more than the V1 interface. Perhaps the fix in patches #1 and #2 sufficient to make libxc work?

The V1 interface has some hideous misfeature wrt error reporting iirc.

It doesn't report the actual per-frame error code, just the "an error" signal through setting the top nibble (already a nasty interface!), IIRC

Aha: http://xenbits.xen.org/hg/linux-2.6.18-xen.hg/rev/6d6c3dd995c0

IIuc, David's approach in these patches was
hypervisor rc -> encode into mfn field -> decode into error rval if available.

The changeset Ian references shows a different approach is needed. Perhaps:
1. Provide local err array in all cases
2. Always store hypervisor rc into err array
3. if version 2, copy into user-space. If version 1, encode into user-space mfn

Andres

Quoted 5 lines

It was knackered on 64 bit (actually set bits 28-31...)...

Ian.

#4 lore raw

Re: [PATCH 2/3] xen/privcmd: report paged-out frames in PRIVCMD_MMAPBATCH ioctl

In reply to David Vrabel

drivers/xen/privcmd.c static int mmap_batch_fn(void *data, void *state) · line 266

Quoted 5 lines · David Vrabel
drivers/xen/privcmd.c +5 -0
+	if (ret < 0) {
+		if (ret == -ENOENT)
+			*mfnp |= 0x80000000U;
+		else
+			*mfnp |= 0xf0000000U;

Please don't use magic constants.

Bastian

Signature · 2 lines
Respect is a rational process
		-- McCoy, "The Galileo Seven", stardate 2822.3