Skip to contentSkip to conversation

Improve performance of IOCTL_PRIVCMD_MMAPBATCH_V2

PATCH In review Mats Petersson 3 participants 6 messages Nov 16, 2012 – Nov 21, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Improve performance of IOCTL_PRIVCMD_MMAPBATCH_V2

This patch makes the IOCTL_PRIVCMD_MMAPBATCH_V2 (and older V1 version) map multiple (typically 1024) pages at a time rather than one page at a time, despite the pages being non-consecutive MFNs. The main change is to pass a pointer to an array of mfns, rather than one mfn. To support error reporting, we also pass an err_ptr. If err_ptr is NULL, it indicates we want the contiguous pages behaviour, so the mfn value is incremented rather than the pointer itself. Performance of mapping guest memory into Dom0 is improved by a factor of around 6 with this change.

---
 arch/x86/xen/mmu.c    |   47 ++++++++++++++++++++++++++-------
 drivers/xen/privcmd.c |   70 ++++++++++++++++++++++++++++++++++++++++++-------
 include/xen/xen-ops.h |    5 ++--
 3 files changed, 100 insertions(+), 22 deletions(-)
arch/x86/xen/mmu.c +37 -10
index dcf5f2d..c5e23ba 100644
@@ -2477,7 +2477,8 @@ void __init xen_hvm_init_mmu_ops(void)
 #define REMAP_BATCH_SIZE 16
 
 struct remap_data {
-	unsigned long mfn;
+	unsigned long *mfn;
+	int contiguous;
 	pgprot_t prot;
 	struct mmu_update *mmu_update;
 };
@@ -2486,7 +2487,13 @@ static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
 				 unsigned long addr, void *data)
 {
 	struct remap_data *rmd = data;
-	pte_t pte = pte_mkspecial(pfn_pte(rmd->mfn++, rmd->prot));
+	pte_t pte = pte_mkspecial(pfn_pte(*rmd->mfn, rmd->prot));
+	/* If we have a contigious range, just update the mfn itself,
+	   else update pointer to be "next mfn". */
+	if (rmd->contiguous)
+		(*rmd->mfn)++;
+	else
+		rmd->mfn++;
 
 	rmd->mmu_update->ptr = virt_to_machine(ptep).maddr;
 	rmd->mmu_update->val = pte_val_ma(pte);
@@ -2495,16 +2502,17 @@ static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
 	return 0;
 }
 
+
 int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 			       unsigned long addr,
-			       unsigned long mfn, int nr,
-			       pgprot_t prot, unsigned domid)
+			       unsigned long *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned domid)
 {
+	int err;
 	struct remap_data rmd;
 	struct mmu_update mmu_update[REMAP_BATCH_SIZE];
-	int batch;
 	unsigned long range;
-	int err = 0;
 
 	if (xen_feature(XENFEAT_auto_translated_physmap))
 		return -EINVAL;
@@ -2515,9 +2523,15 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 
 	rmd.mfn = mfn;
 	rmd.prot = prot;
+	/* We use the err_ptr to indicate if there we are doing a contigious
+	 * mapping or a discontigious mapping. */
+	rmd.contiguous = !err_ptr;
 
 	while (nr) {
-		batch = min(REMAP_BATCH_SIZE, nr);
+		int index = 0;
+		int done = 0;
+		int batch = min(REMAP_BATCH_SIZE, nr);
+		int batch_left = batch;
 		range = (unsigned long)batch << PAGE_SHIFT;
 
 		rmd.mmu_update = mmu_update;
@@ -2526,12 +2540,25 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 		if (err)
 			goto out;
 
-		err = HYPERVISOR_mmu_update(mmu_update, batch, NULL, domid);
-		if (err < 0)
-			goto out;
+		/* We record the error for each page that gives an error, but
+		 * continue mapping until the whole set is done */
+		do {
+			err = HYPERVISOR_mmu_update(&mmu_update[index],
+						    batch_left, &done, domid);
+			if (err < 0) {
+				if (err_ptr)
+					err_ptr[index] = err;
+				else /* exit if error and no err_ptr */
+					goto out;
+			}
+			batch_left -= done;
+			index += done;
+		} while (batch_left);
 
 		nr -= batch;
 		addr += range;
+		if (err_ptr)
+			err_ptr += batch;
 	}
 
 	err = 0;
drivers/xen/privcmd.c +60 -10

132 lines not on this page · raw message

include/xen/xen-ops.h +3 -2
index 6a198e4..1bcda39 100644
@@ -26,7 +26,8 @@ void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order);
 struct vm_area_struct;
 int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 			       unsigned long addr,
-			       unsigned long mfn, int nr,
-			       pgprot_t prot, unsigned domid);
+			       unsigned long *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned domid);
 
 #endif /* INCLUDE_XEN_OPS_H */
Signature · 1 line
1.7.9.5
#1 lore raw

On 16/11/12 14:45, Mats Petersson wrote:

Add "xen/privcmd:" prefix to subject.

Quoted 8 lines

This patch makes the IOCTL_PRIVCMD_MMAPBATCH_V2 (and older V1 version) map multiple (typically 1024) pages at a time rather than one page at a time, despite the pages being non-consecutive MFNs. The main change is to pass a pointer to an array of mfns, rather than one mfn. To support error reporting, we also pass an err_ptr. If err_ptr is NULL, it indicates we want the contiguous pages behaviour, so the mfn value is incremented rather than the pointer itself. Performance of mapping guest memory into Dom0 is improved by a factor of around 6 with this change.

Can you include details on the test and the raw figures as well?

Quoted 12 lines
---
 arch/x86/xen/mmu.c    |   47 ++++++++++++++++++++++++++-------
 drivers/xen/privcmd.c |   70 ++++++++++++++++++++++++++++++++++++++++++-------
 include/xen/xen-ops.h |    5 ++--
 3 files changed, 100 insertions(+), 22 deletions(-)
arch/x86/xen/mmu.c +0 -0
index dcf5f2d..c5e23ba 100644

arch/x86/xen/mmu.c void __init xen_hvm_init_mmu_ops(void) · line 2481

Quoted 6 lines
arch/x86/xen/mmu.c +2 -1
@@ -2477,7 +2477,8 @@ void __init xen_hvm_init_mmu_ops(void)
 #define REMAP_BATCH_SIZE 16
 
 struct remap_data {
-	unsigned long mfn;
+	unsigned long *mfn;
+	int contiguous;

bool.

Quoted 41 lines
arch/x86/xen/mmu.c +12 -5
 	pgprot_t prot;
 	struct mmu_update *mmu_update;
 };
@@ -2486,7 +2487,13 @@ static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
 				 unsigned long addr, void *data)
 {
 	struct remap_data *rmd = data;
-	pte_t pte = pte_mkspecial(pfn_pte(rmd->mfn++, rmd->prot));
+	pte_t pte = pte_mkspecial(pfn_pte(*rmd->mfn, rmd->prot));
+	/* If we have a contigious range, just update the mfn itself,
+	   else update pointer to be "next mfn". */
+	if (rmd->contiguous)
+		(*rmd->mfn)++;
+	else
+		rmd->mfn++;
 
 	rmd->mmu_update->ptr = virt_to_machine(ptep).maddr;
 	rmd->mmu_update->val = pte_val_ma(pte);
@@ -2495,16 +2502,17 @@ static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
 	return 0;
 }
 
+
 int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 			       unsigned long addr,
-			       unsigned long mfn, int nr,
-			       pgprot_t prot, unsigned domid)
+			       unsigned long *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned domid)
 {
+	int err;
 	struct remap_data rmd;
 	struct mmu_update mmu_update[REMAP_BATCH_SIZE];
-	int batch;
 	unsigned long range;
-	int err = 0;
 
 	if (xen_feature(XENFEAT_auto_translated_physmap))
 		return -EINVAL;

arch/x86/xen/mmu.c int xen_remap_domain_mfn_range(struct vm_area_struct *vma, · line 2528

Quoted 6 lines
arch/x86/xen/mmu.c +3 -0
@@ -2515,9 +2523,15 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 
 	rmd.mfn = mfn;
 	rmd.prot = prot;
+	/* We use the err_ptr to indicate if there we are doing a contigious
+	 * mapping or a discontigious mapping. */
+	rmd.contiguous = !err_ptr;

This is non-obvious for an API call. Suggest having two wrapper functions for the two different use cases that share a common internal implementation.

e.g.,

int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
unsigned long addr, unsigned long *mfns, int nr, int *err_ptr,
pgprot_t prot, unsigned domid)

int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
unsigned long addr,
unsigned long start_mfn, unsigned long end_mfn, pgprot_t prot, unsigned domid)

It would be nice if the API calls had some docs as well (e.g., in kernel-doc style).

David

#2 lore raw
Quoted 76 lines · David Vrabel

On 16/11/12 14:45, Mats Petersson wrote:

Add "xen/privcmd:" prefix to subject.

Quoted 8 lines

This patch makes the IOCTL_PRIVCMD_MMAPBATCH_V2 (and older V1 version) map multiple (typically 1024) pages at a time rather than one page at a time, despite the pages being non-consecutive MFNs. The main change is to pass a pointer to an array of mfns, rather than one mfn. To support error reporting, we also pass an err_ptr. If err_ptr is NULL, it indicates we want the contiguous pages behaviour, so the mfn value is incremented rather than the pointer itself. Performance of mapping guest memory into Dom0 is improved by a factor of around 6 with this change.

Can you include details on the test and the raw figures as well?

Quoted 12 lines
---
  arch/x86/xen/mmu.c    |   47 ++++++++++++++++++++++++++-------
  drivers/xen/privcmd.c |   70 ++++++++++++++++++++++++++++++++++++++++++-------
  include/xen/xen-ops.h |    5 ++--
  3 files changed, 100 insertions(+), 22 deletions(-)
arch/x86/xen/mmu.c +0 -0
index dcf5f2d..c5e23ba 100644

arch/x86/xen/mmu.c void __init xen_hvm_init_mmu_ops(void) · line 2481

Quoted 6 lines
arch/x86/xen/mmu.c +2 -1
@@ -2477,7 +2477,8 @@ void __init xen_hvm_init_mmu_ops(void)
  #define REMAP_BATCH_SIZE 16
 
  struct remap_data {
-	unsigned long mfn;
+	unsigned long *mfn;
+	int contiguous;

bool.

Quoted 42 lines
arch/x86/xen/mmu.c +12 -5
  	pgprot_t prot;
  	struct mmu_update *mmu_update;
  };
@@ -2486,7 +2487,13 @@ static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
  				 unsigned long addr, void *data)
  {
  	struct remap_data *rmd = data;
-	pte_t pte = pte_mkspecial(pfn_pte(rmd->mfn++, rmd->prot));
+	pte_t pte = pte_mkspecial(pfn_pte(*rmd->mfn, rmd->prot));
+	/* If we have a contigious range, just update the mfn itself,
+	   else update pointer to be "next mfn". */
+	if (rmd->contiguous)
+		(*rmd->mfn)++;
+	else
+		rmd->mfn++;
 
  	rmd->mmu_update->ptr = virt_to_machine(ptep).maddr;
  	rmd->mmu_update->val = pte_val_ma(pte);
@@ -2495,16 +2502,17 @@ static int remap_area_mfn_pte_fn(pte_t *ptep, pgtable_t token,
  	return 0;
  }
 
+
  int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
  			       unsigned long addr,
-			       unsigned long mfn, int nr,
-			       pgprot_t prot, unsigned domid)
+			       unsigned long *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned domid)
  {
+	int err;
  	struct remap_data rmd;
  	struct mmu_update mmu_update[REMAP_BATCH_SIZE];
-	int batch;
  	unsigned long range;
-	int err = 0;
 
  	if (xen_feature(XENFEAT_auto_translated_physmap))
  		return -EINVAL;
@@ -2515,9 +2523,15 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 
Quoted 8 lines

arch/x86/xen/mmu.c int xen_remap_domain_mfn_range(struct vm_area_struct *vma, · line 2528

Quoted 5 lines
arch/x86/xen/mmu.c +3 -0
  	rmd.mfn = mfn;
  	rmd.prot = prot;
+	/* We use the err_ptr to indicate if there we are doing a contigious
+	 * mapping or a discontigious mapping. */
+	rmd.contiguous = !err_ptr;

This is non-obvious for an API call. Suggest having two wrapper functions for the two different use cases that share a common internal implementation.

I originally had another argument to the function of "contiguous" - but found that in both places this function is called, it had both contiguous and err_ptr either "1, NULL" or "0, not-NULL". So I thought "why not save one argument".

Do you foresee these functions being called from many other places?

Quoted 9 lines

e.g.,

int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
unsigned long addr, unsigned long *mfns, int nr, int *err_ptr,
pgprot_t prot, unsigned domid)

Quoted 4 lines

int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
unsigned long addr,
unsigned long start_mfn, unsigned long end_mfn, pgprot_t prot, unsigned domid)

The original xen_remap_mfn_range() takes mfn and nr, so I think if we have two functions, the original function should remain as before (to reduce the amount of changes). It also makes the common function more symmetrical between the two cases, as we'd have to back-calculate the "nr" argument - we clearly can't know "end_mfn" in the array case.

Quoted 3 lines

It would be nice if the API calls had some docs as well (e.g., in kernel-doc style).

Yes, that would be nice, wouldn't it. I'll add something.

--
Mats

Quoted 4 lines

David

#3 lore raw
In reply to Mats Petersson
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Mats Petersson
arch/x86/xen/mmu.c +0 -1
@@ -2526,12 +2540,25 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 		if (err)
 			goto out;
 
-		err = HYPERVISOR_mmu_update(mmu_update, batch, NULL, domid);

arch/x86/xen/mmu.c int xen_remap_domain_mfn_range(struct vm_area_struct *vma, · line 2550

Quoted 10 lines
arch/x86/xen/mmu.c +8 -2
-		if (err < 0)
-			goto out;
+		/* We record the error for each page that gives an error, but
+		 * continue mapping until the whole set is done */
+		do {
+			err = HYPERVISOR_mmu_update(&mmu_update[index],
+						    batch_left, &done, domid);
+			if (err < 0) {
+				if (err_ptr)
+					err_ptr[index] = err;

Shouldn't you increment "done" here, in order to not retry the failed slot immediately?

Quoted 15 lines
arch/x86/xen/mmu.c +8 -0
+				else /* exit if error and no err_ptr */
+					goto out;
+			}
+			batch_left -= done;
+			index += done;
+		} while (batch_left);
 
 		nr -= batch;
 		addr += range;
+		if (err_ptr)
+			err_ptr += batch;
 	}
 
 	err = 0;

arch/x86/xen/mmu.c static int mmap_return_errors_v1(void *data, void *state) · line 353

Quoted 5 lines
arch/x86/xen/mmu.c +2 -0
@@ -303,6 +349,8 @@ static int mmap_return_errors_v1(void *data, void *state)
 	return __put_user(*mfnp, st->user_mfn++);
 }
 
+
+

???

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, · line 372

Quoted 10 lines
drivers/xen/privcmd.c +2 -0
 static struct vm_operations_struct privcmd_vm_ops;
 
 static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
@@ -319,6 +367,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, 
 int version)
 	if (!xen_initial_domain())
 		return -EPERM;
 
+
+

???

Jan

Quoted 3 lines
drivers/xen/privcmd.c +0 -0
 	switch (version) {
 	case 1:

if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))

#4 lore raw
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Mats Petersson
arch/x86/xen/mmu.c +0 -0
@@ -2526,12 +2540,25 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
  		if (err)
  			goto out;
 
Quoted 13 lines

arch/x86/xen/mmu.c int xen_remap_domain_mfn_range(struct vm_area_struct *vma, · line 2550

Quoted 11 lines
arch/x86/xen/mmu.c +8 -3
-		err = HYPERVISOR_mmu_update(mmu_update, batch, NULL, domid);
-		if (err < 0)
-			goto out;
+		/* We record the error for each page that gives an error, but
+		 * continue mapping until the whole set is done */
+		do {
+			err = HYPERVISOR_mmu_update(&mmu_update[index],
+						    batch_left, &done, domid);
+			if (err < 0) {
+				if (err_ptr)
+					err_ptr[index] = err;

Shouldn't you increment "done" here, in order to not retry the failed slot immediately?

Yes, good spot - for some reason, I have double checked the behaviour of "done", and it returns the index of the item which gave the error, not actually "how many were processed".

I have rewritten this part of code for V3 of this patch, but I think it still requires an increment of "done" to make it work correctly.

Quoted 14 lines
Quoted 14 lines
arch/x86/xen/mmu.c +8 -0
+				else /* exit if error and no err_ptr */
+					goto out;
+			}
+			batch_left -= done;
+			index += done;
+		} while (batch_left);
 
  		nr -= batch;
  		addr += range;
+		if (err_ptr)
+			err_ptr += batch;
  	}
 
Quoted 8 lines

arch/x86/xen/mmu.c static int mmap_return_errors_v1(void *data, void *state) · line 353

Quoted 7 lines
arch/x86/xen/mmu.c +2 -0
  	err = 0;
@@ -303,6 +349,8 @@ static int mmap_return_errors_v1(void *data, void *state)
  	return __put_user(*mfnp, st->user_mfn++);
  }
 
+
+

???

This is cleaned up in the V3 patch.

Quoted 11 lines

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, · line 372

Quoted 10 lines
drivers/xen/privcmd.c +2 -0
  static struct vm_operations_struct privcmd_vm_ops;
 
  static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
@@ -319,6 +367,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata,
 int version)
  	if (!xen_initial_domain())
  		return -EPERM;
 
+
+

???

As above.

--
Mats

Quoted 8 lines

Jan

Quoted 3 lines
drivers/xen/privcmd.c +0 -0
  	switch (version) {
  	case 1:

if (copy_from_user(&m, udata, sizeof(struct privcmd_mmapbatch)))

#5 lore raw
Quoted 22 lines
Quoted 1 line
Quoted 1 line · Mats Petersson
Quoted 2 lines · Jan Beulich

Review int xen_remap_domain_mfn_range(struct vm_area_struct

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Mats Petersson
(quoted diff) +0 -0
@@ -2526,12 +2540,25 @@ int xen_remap_domain_mfn_range(struct vm_area_struct 

*vma,

Quoted 16 lines
Quoted 4 lines
arch/x86/xen/mmu.c +0 -1
  		if (err)
  			goto out;
 
-		err = HYPERVISOR_mmu_update(mmu_update, batch, NULL, domid);

arch/x86/xen/mmu.c int xen_remap_domain_mfn_range(struct vm_area_struct *vma, · line 2550

Quoted 10 lines
arch/x86/xen/mmu.c +8 -2
-		if (err < 0)
-			goto out;
+		/* We record the error for each page that gives an error, but
+		 * continue mapping until the whole set is done */
+		do {
+			err = HYPERVISOR_mmu_update(&mmu_update[index],
+						    batch_left, &done, domid);
+			if (err < 0) {
+				if (err_ptr)
+					err_ptr[index] = err;

Shouldn't you increment "done" here, in order to not retry the failed slot immediately?

Quoted 3 lines

Yes, good spot - for some reason, I have double checked the behaviour of "done", and it returns the index of the item which gave the error, not actually "how many were processed".

Isn't the index of failure equal to the number of successfully processed ones?

Quoted 21 lines

I have rewritten this part of code for V3 of this patch, but I think it still requires an increment of "done" to make it work correctly.

Quoted 19 lines
Quoted 19 lines
arch/x86/xen/mmu.c +8 -0
+				else /* exit if error and no err_ptr */
+					goto out;
+			}
+			batch_left -= done;
+			index += done;
+		} while (batch_left);
 
  		nr -= batch;
  		addr += range;
+		if (err_ptr)
+			err_ptr += batch;
  	}
 
  	err = 0;
@@ -303,6 +349,8 @@ static int mmap_return_errors_v1(void *data, void *state)
  	return __put_user(*mfnp, st->user_mfn++);
  }
 
Quoted 4 lines
Quoted 3 lines

Review static int mmap_return_errors_v1(void *data, void *state) · line 353

Quoted 2 lines
(quoted diff) +2 -0
+
+

???

This is cleaned up in the V3 patch.

Did I miss that (and v2 too)?

Jan