Skip to contentSkip to conversation

xen/privcmd.c: improve performance of MMAPBATCH_V2.

PATCH In review Mats Petersson 3 participants 8 messages Dec 19, 2012 – Jan 2, 2013

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

PATCH v5 1/1 xen/privcmd.c: improve performance of MMAPBATCH_V2.

This patch makes the IOCTL_PRIVCMD_MMAPBATCH_V2 (and older V1 version) map multiple (typically 4KB worth of mfn's) 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.

Using a small test program to map Guest memory into Dom0 (repeatedly for "Iterations" mapping the same first "Num Pages")

Iterations    Num Pages    Time 3.7rc4  Time With this patch
5000          4096         76.107       37.027
10000         2048         75.703       37.177
20000         1024         75.893       37.247

Performance of mapping guest memory into Dom0 is about half of the original time.

When migrating a guest, this gives around 15-40% improvement on overall live-migration time - bigger guests show bigger improvement, and around 30% improvment on "guest downtime".

---

V5 of the patch contains fixes to ARM architecture, to add the same API on that side (which would otherwise have caused a build break). The ARM variant can do with a little bit of performance improvement.

Some minor formatting, and merging to latest 3.8 branch. No functionality changes for x86.

---
 arch/arm/xen/enlighten.c |  113 +++++++++++++++++++++++++++++++-------
 arch/x86/xen/mmu.c       |  137 ++++++++++++++++++++++++++++++++++++++++------
 drivers/xen/privcmd.c    |   54 ++++++++++++++----
 include/xen/xen-ops.h    |    6 ++
 4 files changed, 264 insertions(+), 46 deletions(-)
arch/arm/xen/enlighten.c +94 -19
index 7a32976..967dea9 100644
@@ -67,19 +67,19 @@ static int map_foreign_page(unsigned long lpfn, unsigned long fgmfn,
 	if (rc) {
 		pr_warn("Failed to map pfn to mfn rc:%d pfn:%lx mfn:%lx\n",
 			rc, lpfn, fgmfn);
-		return 1;
+		return rc;
 	}
 	return 0;
 }
 
 struct remap_data {
-	xen_pfn_t fgmfn; /* foreign domain's gmfn */
+	xen_pfn_t *fgmfn; /* foreign domain's gmfn */
 	pgprot_t prot;
 	domid_t  domid;
 	struct vm_area_struct *vma;
 	int index;
 	struct page **pages;
-	struct xen_remap_mfn_info *info;
+	int *err_ptr;
 };
 
 static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
@@ -89,37 +89,112 @@ static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
 	struct page *page = info->pages[info->index++];
 	unsigned long pfn = page_to_pfn(page);
 	pte_t pte = pfn_pte(pfn, info->prot);
-
-	if (map_foreign_page(pfn, info->fgmfn, info->domid))
-		return -EFAULT;
+	int err;
+	// TODO: We should really batch these updates.
+	err = map_foreign_page(pfn, *info->fgmfn, info->domid);
+	*info->err_ptr++ = err;
 	set_pte_at(info->vma->vm_mm, addr, ptep, pte);
+	info->fgmfn++;
 
-	return 0;
+	return err;
 }
 
-int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
-			       unsigned long addr,
-			       xen_pfn_t mfn, int nr,
-			       pgprot_t prot, unsigned domid,
-			       struct page **pages)
+/* do_remap_mfn() - helper function to map foreign pages
+ * @vma:     the vma for the pages to be mapped into
+ * @addr:    the address at which to map the pages
+ * @mfn:     pointer to array of MFNs to map
+ * @nr:      the number entries in the MFN array
+ * @prot:    page protection mask
+ * @domid:   id of the domain that we are mapping from
+ * @pages:   page information.
+ *
+ * This function takes an array of mfns and maps nr pages from that into
+ * this kernel's memory. The owner of the pages is defined by domid. Where the
+ * pages are mapped is determined by addr, and vma is used for "accounting" of
+ * the pages.
+ *
+ * Return value is zero for success, negative for failure.
+ */
+static int do_remap_mfn(struct vm_area_struct *vma,
+			unsigned long addr,
+			xen_pfn_t *mfn, int nr, int *err_ptr,
+			pgprot_t prot, unsigned domid,
+			struct page **pages)
 {
 	int err;
 	struct remap_data data;
+	unsigned long range = nr << PAGE_SHIFT;
 
-	/* TBD: Batching, current sole caller only does page at a time */
-	if (nr > 1)
-		return -EINVAL;
+	/* Kept here for the purpose of making sure code doesn't break
+	   x86 PVOPS */
+	BUG_ON(!((vma->vm_flags & (VM_PFNMAP | VM_IO)) == (VM_PFNMAP | VM_IO)));
 
 	data.fgmfn = mfn;
-	data.prot = prot;
+	data.prot  = prot;
 	data.domid = domid;
-	data.vma = vma;
-	data.index = 0;
+	data.vma   = vma;
 	data.pages = pages;
-	err = apply_to_page_range(vma->vm_mm, addr, nr << PAGE_SHIFT,
+	data.index = 0;
+	data.err_ptr = err_ptr;
+
+	err = apply_to_page_range(vma->vm_mm, addr, range,
 				  remap_pte_fn, &data);
+	/* Warning: We do probably need to care about what error we
+	   get here. However, currently, the remap_pte_fn is only
+	   likely to return EFAULT or some other "things are very
+	   bad" error code, which the rest of the calling code won't
+	   be able to fix up. So we just exit with the error we got.
+	*/
 	return err;
 }
+
+/* xen_remap_domain_mfn_array() - Used to map an array of foreign pages
+ * @vma:     the vma for the pages to be mapped into
+ * @addr:    the address at which to map the pages
+ * @mfn:     pointer to array of MFNs to map
+ * @nr:      the number entries in the MFN array
+ * @err_ptr: pointer to array of integers, one per MFN, for an error
+ *           value for each page. The err_ptr must not be NULL.
+ * @prot:    page protection mask
+ * @domid:   id of the domain that we are mapping from
+ * @pages:   page information
+ *
+ * This function takes an array of mfns and maps nr pages from that into this
+ * kernel's memory. The owner of the pages is defined by domid. Where the pages
+ * are mapped is determined by addr, and vma is used for "accounting" of the
+ * pages. The err_ptr array is filled in on any page that is not successfully
+ * mapped in.
+ *
+ * Return value is zero for success, negative ERRNO value for failure.
+ * Note that the error value -ENOENT is considered a "retry", so when this
+ * error code is seen, another call should be made with the list of pages that
+ * are marked as -ENOENT in the err_ptr array.
+ */
+int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
+			       unsigned long addr,
+			       xen_pfn_t *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned domid,
+			       struct page **pages)
+{
+	/* We BUG_ON because it's a programmer error to pass a NULL err_ptr,
+	 * and the consequences later is quite hard to detect what the actual
+	 * cause of "wrong memory was mapped in".
+	 */
+	BUG_ON(err_ptr == NULL);
+	return do_remap_mfn(vma, addr, mfn, nr, err_ptr, prot, domid, pages);
+}
+EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_array);
+
+/* Not used in ARM. Use xen_remap_domain_mfn_array(). */
+int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
+			       unsigned long addr,
+			       xen_pfn_t mfn, int nr,
+			       pgprot_t prot, unsigned domid,
+			       struct page **pages)
+{
+	return -ENOSYS;
+}
 EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_range);
 
 int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
arch/x86/xen/mmu.c +121 -16

188 lines not on this page · raw message

drivers/xen/privcmd.c +43 -11

90 lines not on this page · raw message

include/xen/xen-ops.h +6 -0
index d6fe062..d8c57cc 100644
@@ -30,6 +30,12 @@ int xen_remap_domain_mfn_range(struct vm_area_struct *vma,
 			       xen_pfn_t mfn, int nr,
 			       pgprot_t prot, unsigned domid,
 			       struct page **pages);
+int xen_remap_domain_mfn_array(struct vm_area_struct *vma,
+			       unsigned long addr,
+			       xen_pfn_t *mfn, int nr,
+			       int *err_ptr, pgprot_t prot,
+			       unsigned domid, 
+			       struct page **pages);
 int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
 			       int numpgs, struct page **pages);
 
Signature · 1 line
1.7.9.5
#1 lore raw

I've added Andres since I think this accumulation of ENOENT in err_ptr is a paging related thing, or at least I think he understands this code ;-)

Quoted 6 lines
arch/arm/xen/enlighten.c +0 -2
@@ -89,37 +89,112 @@ static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
        struct page *page = info->pages[info->index++];
        unsigned long pfn = page_to_pfn(page);
        pte_t pte = pfn_pte(pfn, info->prot);
-
-       if (map_foreign_page(pfn, info->fgmfn, info->domid))

arch/arm/xen/enlighten.c static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr, · line 99

Quoted 10 lines
arch/arm/xen/enlighten.c +6 -2
-               return -EFAULT;
+       int err;
+       // TODO: We should really batch these updates.
+       err = map_foreign_page(pfn, *info->fgmfn, info->domid);
+       *info->err_ptr++ = err;
        set_pte_at(info->vma->vm_mm, addr, ptep, pte);
+       info->fgmfn++;
 
-       return 0;
+       return err;

This will cause apply_to_page_range to stop walking and return an error. AIUI the intention of the err_ptr array is to accumulate the individual success/error for the entire range. The caller can then take care of the successes/failures and ENOENTs as appropriate (in particular it doesn't want to abort a batch because of an ENOENT, it wants to do as much as possible)

On x86 (when err_ptr != NULL) you accumulate all of the errors from HYPERVISOR_mmu_update rather than aborting on the first one and this seems correct to me.

Quoted 28 lines
(quoted diff) +0 -0
 int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
arch/x86/xen/mmu.c +14 -3
index 01de35c..323a2ab 100644
[...]
@@ -2528,23 +2557,98 @@ 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)
+                                       goto out;
+                               /* increment done so we skip the error item */
+                               done++;
+                               last_err = err_ptr[index] = err;
+                       }
+                       batch_left -= done;
+                       index += done;

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

Quoted 10 lines
arch/x86/xen/mmu.c +4 -1
+               } while (batch_left);
 
                nr -= batch;
                addr += range;
+               if (err_ptr)
+                       err_ptr += batch;
        }
 
-       err = 0;
+       err = last_err;

This means that if you have 100 failures followed by one success you return success overall. Is that intentional? That doesn't seem right.

Quoted 6 lines
arch/x86/xen/mmu.c +0 -0
 out:
 
        xen_flush_tlb_all();
 
        return err;
 }

[...]

Quoted 4 lines
drivers/xen/privcmd.c +0 -0
index 0bbbccb..8f86a44 100644

[...]

Quoted 6 lines
drivers/xen/privcmd.c +0 -2
@@ -283,12 +317,10 @@ static int mmap_batch_fn(void *data, void *state)
        if (xen_feature(XENFEAT_auto_translated_physmap))
                cur_page = pages[st->index++];
 
-       ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-                                        st->vma->vm_page_prot, st->domain,

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

Quoted 10 lines
drivers/xen/privcmd.c +4 -4
-                                        &cur_page);
-
-       /* Store error code for second pass. */
-       *(st->err++) = ret;
+       BUG_ON(nr < 0);
+       ret = xen_remap_domain_mfn_array(st->vma, st->va & PAGE_MASK, mfnp, nr,
+                                        st->err, st->vma->vm_page_prot,
+                                        st->domain, &cur_page);
 
        /* And see if it affects the global_error. */

The code which follows needs adjustment to cope with the fact that we now batch. I think it needs to walk st->err and set global_error as appropriate. This is related to my comment about the return value of xen_remap_domain_mfn_range too.

I think rather than trying to fabricate some sort of meaningful error code for an entire batch xen_remap_domain_mfn_range should just return an indication about whether there were any errors or not and leave it to the caller to figure out the overall result by looking at the err array.

Perhaps return either the number of errors or the number of successes (which turns the following if into either (ret) or (ret < nr) respectively).

Quoted 11 lines
drivers/xen/privcmd.c +1 -1
        if (ret < 0) {
@@ -300,7 +332,7 @@ static int mmap_batch_fn(void *data, void *state)
                                st->global_error = 1;
                }
        }
-       st->va += PAGE_SIZE;
+       st->va += PAGE_SIZE * nr;
 
        return 0;
 }

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, int version) · line 466

Quoted 7 lines
drivers/xen/privcmd.c +2 -2
@@ -430,8 +462,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
        state.err           = err_array;
 
        /* mmap_batch_fn guarantees ret == 0 */
-       BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
-                            &pagelist, mmap_batch_fn, &state));
+       BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
+                                   &pagelist, mmap_batch_fn, &state));

Can we make traverse_pages and _block common by passing a block size parameter?

Ian.

#2 lore raw
Quoted 27 lines · Ian Campbell

I've added Andres since I think this accumulation of ENOENT in err_ptr is a paging related thing, or at least I think he understands this code ;-)

Quoted 6 lines
arch/arm/xen/enlighten.c +0 -2
@@ -89,37 +89,112 @@ static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
         struct page *page = info->pages[info->index++];
         unsigned long pfn = page_to_pfn(page);
         pte_t pte = pfn_pte(pfn, info->prot);
-
-       if (map_foreign_page(pfn, info->fgmfn, info->domid))

arch/arm/xen/enlighten.c static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr, · line 99

Quoted 10 lines
arch/arm/xen/enlighten.c +6 -2
-               return -EFAULT;
+       int err;
+       // TODO: We should really batch these updates.
+       err = map_foreign_page(pfn, *info->fgmfn, info->domid);
+       *info->err_ptr++ = err;
         set_pte_at(info->vma->vm_mm, addr, ptep, pte);
+       info->fgmfn++;
 
-       return 0;
+       return err;

This will cause apply_to_page_range to stop walking and return an error. AIUI the intention of the err_ptr array is to accumulate the individual success/error for the entire range. The caller can then take care of the successes/failures and ENOENTs as appropriate (in particular it doesn't want to abort a batch because of an ENOENT, it wants to do as much as possible)

Quoted 3 lines

On x86 (when err_ptr != NULL) you accumulate all of the errors from HYPERVISOR_mmu_update rather than aborting on the first one and this seems correct to me.

Ok, will rework that bit.

Quoted 36 lines
Quoted 36 lines
(quoted diff) +0 -0
  int xen_unmap_domain_mfn_range(struct vm_area_struct *vma,
arch/x86/xen/mmu.c +17 -3
index 01de35c..323a2ab 100644
[...]
@@ -2528,23 +2557,98 @@ 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)
+                                       goto out;
+                               /* increment done so we skip the error item */
+                               done++;
+                               last_err = err_ptr[index] = err;
+                       }
+                       batch_left -= done;
+                       index += done;
+               } while (batch_left);
 
                 nr -= batch;
                 addr += range;
+               if (err_ptr)
+                       err_ptr += batch;
         }
 
Quoted 4 lines

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

Quoted 2 lines
arch/x86/xen/mmu.c +1 -1
-       err = 0;
+       err = last_err;

This means that if you have 100 failures followed by one success you return success overall. Is that intentional? That doesn't seem right.

As far as I see, it doesn't mean that. last_err is only set at the beginning of the call (to zero) and if there is an error.

Quoted 28 lines
Quoted 7 lines
arch/x86/xen/mmu.c +0 -0
  out:
 
         xen_flush_tlb_all();
 
         return err;
  }

[...]

Quoted 4 lines
drivers/xen/privcmd.c +0 -0
index 0bbbccb..8f86a44 100644

[...]

Quoted 15 lines
drivers/xen/privcmd.c +4 -6
@@ -283,12 +317,10 @@ static int mmap_batch_fn(void *data, void *state)
         if (xen_feature(XENFEAT_auto_translated_physmap))
                 cur_page = pages[st->index++];
 
-       ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-                                        st->vma->vm_page_prot, st->domain,
-                                        &cur_page);
-
-       /* Store error code for second pass. */
-       *(st->err++) = ret;
+       BUG_ON(nr < 0);
+       ret = xen_remap_domain_mfn_array(st->vma, st->va & PAGE_MASK, mfnp, nr,
+                                        st->err, st->vma->vm_page_prot,
+                                        st->domain, &cur_page);
 
Quoted 5 lines

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

Quoted 1 line
drivers/xen/privcmd.c +0 -0
         /* And see if it affects the global_error. */

The code which follows needs adjustment to cope with the fact that we now batch. I think it needs to walk st->err and set global_error as appropriate. This is related to my comment about the return value of xen_remap_domain_mfn_range too.

The return value, as commented above, is "either 0 or the last error we saw". There should be no need to walk the st->err, as we know if there was some error or not.

Quoted 9 lines

I think rather than trying to fabricate some sort of meaningful error code for an entire batch xen_remap_domain_mfn_range should just return an indication about whether there were any errors or not and leave it to the caller to figure out the overall result by looking at the err array.

Perhaps return either the number of errors or the number of successes (which turns the following if into either (ret) or (ret < nr) respectively).

I'm trying to not change how the code above expects things to work. Whilst it would be lovely to rewrite the entire code dealing with mapping memory, I don't think that's within the scope of my current project. And if I don't wish to rewrite all of libxc's memory management code, I don't want to alter what values are returned or when. The current code follows what WAS happening before my changes - which isn't exactly the most fantastic thing, and I think there may actually be bugs in there, such as:

    if (ret < 0) {
        if (ret == -ENOENT)
            st->global_error = -ENOENT;
        else {
            /* Record that at least one error has happened. */
            if (st->global_error == 0)
                st->global_error = 1;
        }
    }

if we enter this once with -EFAULT, and then after that with -ENOENT, global_error will say -ENOENT. I think knowing that we got an EFAULT is "higher importance" than ENOENT, but that's how the old code was working, and I'm not sure I should change it at this point.

Quoted 13 lines
Quoted 13 lines
drivers/xen/privcmd.c +1 -1
         if (ret < 0) {
@@ -300,7 +332,7 @@ static int mmap_batch_fn(void *data, void *state)
                                 st->global_error = 1;
                 }
         }
-       st->va += PAGE_SIZE;
+       st->va += PAGE_SIZE * nr;
 
         return 0;
  }
@@ -430,8 +462,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
         state.err           = err_array;
 
Quoted 7 lines

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, int version) · line 466

Quoted 5 lines
drivers/xen/privcmd.c +2 -2
         /* mmap_batch_fn guarantees ret == 0 */
-       BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
-                            &pagelist, mmap_batch_fn, &state));
+       BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
+                                   &pagelist, mmap_batch_fn, &state));

Can we make traverse_pages and _block common by passing a block size parameter?

Yes of course. Is there much benefit from that? I understand that it's less code, but it also makes the original traverse_pages more complex. Not convinced it helps much - it's quite a small function, so not much extra code. Additionally, all of the callback functions will have to deal with an extra parameter (that is probably ignored in all but one place).

--
Mats

Quoted 3 lines

Ian.

#3 lore raw
Quoted 6 lines · Mats Petersson
Quoted 4 lines

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

Quoted 2 lines
arch/x86/xen/mmu.c +1 -1
-       err = 0;
+       err = last_err;

This means that if you have 100 failures followed by one success you return success overall. Is that intentional? That doesn't seem right.

As far as I see, it doesn't mean that. last_err is only set at the beginning of the call (to zero) and if there is an error.

Ah, yes I missed that (but it still isn't right, see below).

Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
drivers/xen/privcmd.c +0 -0
index 0bbbccb..8f86a44 100644

[...]

Quoted 15 lines
drivers/xen/privcmd.c +4 -6
@@ -283,12 +317,10 @@ static int mmap_batch_fn(void *data, void *state)
         if (xen_feature(XENFEAT_auto_translated_physmap))
                 cur_page = pages[st->index++];
 
-       ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-                                        st->vma->vm_page_prot, st->domain,
-                                        &cur_page);
-
-       /* Store error code for second pass. */
-       *(st->err++) = ret;
+       BUG_ON(nr < 0);
+       ret = xen_remap_domain_mfn_array(st->vma, st->va & PAGE_MASK, mfnp, nr,
+                                        st->err, st->vma->vm_page_prot,
+                                        st->domain, &cur_page);
 
Quoted 8 lines
Quoted 5 lines

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

Quoted 1 line
drivers/xen/privcmd.c +0 -0
         /* And see if it affects the global_error. */

The code which follows needs adjustment to cope with the fact that we now batch. I think it needs to walk st->err and set global_error as appropriate. This is related to my comment about the return value of xen_remap_domain_mfn_range too.

The return value, as commented above, is "either 0 or the last error we saw". There should be no need to walk the st->err, as we know if there was some error or not.

The code which follows tries to latch having seen ENOENT in preference to other errors, so you don't need 0 or the last error, you need either 0, ENOENT if one occurred somewhere in the batch or an error!=ENOENT. (or maybe its vice versa, either way the last error isn't necessarily what you need).

Quoted 5 lines
Quoted 5 lines

I think rather than trying to fabricate some sort of meaningful error code for an entire batch xen_remap_domain_mfn_range should just return an indication about whether there were any errors or not and leave it to the caller to figure out the overall result by looking at the err array.

Quoted 7 lines
Quoted 3 lines

Perhaps return either the number of errors or the number of successes (which turns the following if into either (ret) or (ret < nr) respectively).

I'm trying to not change how the code above expects things to work. Whilst it would be lovely to rewrite the entire code dealing with mapping memory, I don't think that's within the scope of my current project. And if I don't wish to rewrite all of libxc's memory management

I'm not talking about changing the libxc or ioctl interface, only about the internal-to-Linux interface between privcmd and mmu.c. In fact I'm only actually asking you to change the return value of the new function you are adding to the API.

Quoted 13 lines

code, I don't want to alter what values are returned or when. The current code follows what WAS happening before my changes - which isn't exactly the most fantastic thing, and I think there may actually be bugs in there, such as:

     if (ret < 0) {
         if (ret == -ENOENT)
             st->global_error = -ENOENT;
         else {
             /* Record that at least one error has happened. */
             if (st->global_error == 0)
                 st->global_error = 1;
         }
     }
Quoted 4 lines

if we enter this once with -EFAULT, and then after that with -ENOENT, global_error will say -ENOENT. I think knowing that we got an EFAULT is "higher importance" than ENOENT, but that's how the old code was working, and I'm not sure I should change it at this point.

But I think you are changing it, by this behaviour or returning the last error in the batch which causes this code to behave differently. That's what I'm asking you to avoid!

Quoted 21 lines
Quoted 14 lines
Quoted 14 lines
drivers/xen/privcmd.c +1 -1
         if (ret < 0) {
@@ -300,7 +332,7 @@ static int mmap_batch_fn(void *data, void *state)
                                 st->global_error = 1;
                 }
         }
-       st->va += PAGE_SIZE;
+       st->va += PAGE_SIZE * nr;
 
         return 0;
  }
@@ -430,8 +462,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
         state.err           = err_array;
 
Quoted 7 lines

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, int version) · line 466

Quoted 5 lines
drivers/xen/privcmd.c +2 -2
         /* mmap_batch_fn guarantees ret == 0 */
-       BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
-                            &pagelist, mmap_batch_fn, &state));
+       BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
+                                   &pagelist, mmap_batch_fn, &state));

Can we make traverse_pages and _block common by passing a block size parameter?

Quoted 6 lines

Yes of course. Is there much benefit from that? I understand that it's less code, but it also makes the original traverse_pages more complex. Not convinced it helps much - it's quite a small function, so not much extra code. Additionally, all of the callback functions will have to deal with an extra parameter (that is probably ignored in all but one place).

It depends on what the combined version ends up looking like but in general I'm in favour of one more generic function over several cloned and hacked versions of the same thing.

Ian.

#4 lore raw

Hi,

Quoted 40 lines · Ian Campbell
Quoted 6 lines · Mats Petersson
Quoted 4 lines

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

Quoted 2 lines
arch/x86/xen/mmu.c +1 -1
-       err = 0;
+       err = last_err;

This means that if you have 100 failures followed by one success you return success overall. Is that intentional? That doesn't seem right.

As far as I see, it doesn't mean that. last_err is only set at the beginning of the call (to zero) and if there is an error.

Ah, yes I missed that (but it still isn't right, see below).

Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
drivers/xen/privcmd.c +0 -0
index 0bbbccb..8f86a44 100644

[...]

Quoted 15 lines
drivers/xen/privcmd.c +4 -6
@@ -283,12 +317,10 @@ static int mmap_batch_fn(void *data, void *state)
        if (xen_feature(XENFEAT_auto_translated_physmap))
                cur_page = pages[st->index++];
 
-       ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-                                        st->vma->vm_page_prot, st->domain,
-                                        &cur_page);
-
-       /* Store error code for second pass. */
-       *(st->err++) = ret;
+       BUG_ON(nr < 0);
+       ret = xen_remap_domain_mfn_array(st->vma, st->va & PAGE_MASK, mfnp, nr,
+                                        st->err, st->vma->vm_page_prot,
+                                        st->domain, &cur_page);
 
Quoted 8 lines
Quoted 5 lines

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

Quoted 1 line
drivers/xen/privcmd.c +0 -0
        /* And see if it affects the global_error. */

The code which follows needs adjustment to cope with the fact that we now batch. I think it needs to walk st->err and set global_error as appropriate. This is related to my comment about the return value of xen_remap_domain_mfn_range too.

The return value, as commented above, is "either 0 or the last error we saw". There should be no need to walk the st->err, as we know if there was some error or not.

Quoted 5 lines

The code which follows tries to latch having seen ENOENT in preference to other errors, so you don't need 0 or the last error, you need either 0, ENOENT if one occurred somewhere in the batch or an error!=ENOENT. (or maybe its vice versa, either way the last error isn't necessarily what you need).

Yeah the error reporting interface sucks. All I can say in my defense is that it was that way when I got here.

FWIW EFAULT *takes* precedence over ENOENT.
/* If we have not had any EFAULT-like global errors then set the global
* error to -ENOENT if necessary. */
if ((ret == 0) && (state.global_error == -ENOENT))

         ret = -ENOENT;

Otherwise, any individual mapping that fails differently from ENOENT does not affect the global error. That is, in absence of global errors like EFAULT, and in absence of per-mapping ENOENT return codes, per mapping errors like EINVAL do not affect the ioctl return code which remains zero.

IIUC Ian has been pointing that your code breaks this last statement.

Thanks
Andres

Quoted 75 lines
Quoted 6 lines
Quoted 6 lines

I think rather than trying to fabricate some sort of meaningful error code for an entire batch xen_remap_domain_mfn_range should just return an indication about whether there were any errors or not and leave it to the caller to figure out the overall result by looking at the err array.

Quoted 7 lines
Quoted 3 lines

Perhaps return either the number of errors or the number of successes (which turns the following if into either (ret) or (ret < nr) respectively).

I'm trying to not change how the code above expects things to work. Whilst it would be lovely to rewrite the entire code dealing with mapping memory, I don't think that's within the scope of my current project. And if I don't wish to rewrite all of libxc's memory management

I'm not talking about changing the libxc or ioctl interface, only about the internal-to-Linux interface between privcmd and mmu.c. In fact I'm only actually asking you to change the return value of the new function you are adding to the API.

Quoted 13 lines

code, I don't want to alter what values are returned or when. The current code follows what WAS happening before my changes - which isn't exactly the most fantastic thing, and I think there may actually be bugs in there, such as:

    if (ret < 0) {
        if (ret == -ENOENT)
            st->global_error = -ENOENT;
        else {
            /* Record that at least one error has happened. */
            if (st->global_error == 0)
                st->global_error = 1;
        }
    }
Quoted 4 lines

if we enter this once with -EFAULT, and then after that with -ENOENT, global_error will say -ENOENT. I think knowing that we got an EFAULT is "higher importance" than ENOENT, but that's how the old code was working, and I'm not sure I should change it at this point.

But I think you are changing it, by this behaviour or returning the last error in the batch which causes this code to behave differently. That's what I'm asking you to avoid!

Quoted 21 lines
Quoted 14 lines
Quoted 14 lines
drivers/xen/privcmd.c +1 -1
        if (ret < 0) {
@@ -300,7 +332,7 @@ static int mmap_batch_fn(void *data, void *state)
                                st->global_error = 1;
                }
        }
-       st->va += PAGE_SIZE;
+       st->va += PAGE_SIZE * nr;
 
        return 0;
 }
@@ -430,8 +462,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
        state.err           = err_array;
 
Quoted 7 lines

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, int version) · line 466

Quoted 5 lines
drivers/xen/privcmd.c +2 -2
        /* mmap_batch_fn guarantees ret == 0 */
-       BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
-                            &pagelist, mmap_batch_fn, &state));
+       BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
+                                   &pagelist, mmap_batch_fn, &state));

Can we make traverse_pages and _block common by passing a block size parameter?

Quoted 6 lines

Yes of course. Is there much benefit from that? I understand that it's less code, but it also makes the original traverse_pages more complex. Not convinced it helps much - it's quite a small function, so not much extra code. Additionally, all of the callback functions will have to deal with an extra parameter (that is probably ignored in all but one place).

It depends on what the combined version ends up looking like but in general I'm in favour of one more generic function over several cloned and hacked versions of the same thing.

Ian.

#5 lore raw
Quoted 51 lines · Andres Lagar-Cavilla

Hi,

Quoted 35 lines · Ian Campbell
Quoted 6 lines · Mats Petersson
Quoted 4 lines

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

Quoted 2 lines
arch/x86/xen/mmu.c +1 -1
-       err = 0;
+       err = last_err;

This means that if you have 100 failures followed by one success you return success overall. Is that intentional? That doesn't seem right.

As far as I see, it doesn't mean that. last_err is only set at the beginning of the call (to zero) and if there is an error.

Ah, yes I missed that (but it still isn't right, see below).

Quoted 25 lines
Quoted 25 lines
Quoted 4 lines
drivers/xen/privcmd.c +0 -0
index 0bbbccb..8f86a44 100644

[...]

Quoted 6 lines
drivers/xen/privcmd.c +0 -2
@@ -283,12 +317,10 @@ static int mmap_batch_fn(void *data, void *state)
         if (xen_feature(XENFEAT_auto_translated_physmap))
                 cur_page = pages[st->index++];
 
-       ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-                                        st->vma->vm_page_prot, st->domain,

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

Quoted 10 lines
drivers/xen/privcmd.c +4 -4
-                                        &cur_page);
-
-       /* Store error code for second pass. */
-       *(st->err++) = ret;
+       BUG_ON(nr < 0);
+       ret = xen_remap_domain_mfn_array(st->vma, st->va & PAGE_MASK, mfnp, nr,
+                                        st->err, st->vma->vm_page_prot,
+                                        st->domain, &cur_page);
 
         /* And see if it affects the global_error. */

The code which follows needs adjustment to cope with the fact that we now batch. I think it needs to walk st->err and set global_error as appropriate. This is related to my comment about the return value of xen_remap_domain_mfn_range too.

Quoted 8 lines
Quoted 3 lines

The return value, as commented above, is "either 0 or the last error we saw". There should be no need to walk the st->err, as we know if there was some error or not.

The code which follows tries to latch having seen ENOENT in preference to other errors, so you don't need 0 or the last error, you need either 0, ENOENT if one occurred somewhere in the batch or an error!=ENOENT. (or maybe its vice versa, either way the last error isn't necessarily what you need).

Yeah the error reporting interface sucks. All I can say in my defense is that it was that way when I got here.

FWIW EFAULT *takes* precedence over ENOENT.
/* If we have not had any EFAULT-like global errors then set the global
* error to -ENOENT if necessary. */

Quoted 6 lines
    if ((ret == 0) && (state.global_error == -ENOENT))
          ret = -ENOENT;

Otherwise, any individual mapping that fails differently from ENOENT does not affect the global error. That is, in absence of global errors like EFAULT, and in absence of per-mapping ENOENT return codes, per mapping errors like EINVAL do not affect the ioctl return code which remains zero.

IIUC Ian has been pointing that your code breaks this last statement.

Sorry, are you saying the correct behaviour is to return 0 if we have EINVAL or some other error [aside from EFAULT and ENOENT]? Or are you saying that my code is broken such that it does this?

I did some fixes, that I believe "does the right thing", before Christmas, but since I knew most people wouldn't be available, I didn't post it. I will try to get round to it before the end of the week.

--
Mats

Quoted 73 lines

Thanks
Andres

Quoted 70 lines
Quoted 5 lines
Quoted 5 lines

I think rather than trying to fabricate some sort of meaningful error code for an entire batch xen_remap_domain_mfn_range should just return an indication about whether there were any errors or not and leave it to the caller to figure out the overall result by looking at the err array.

Quoted 7 lines
Quoted 3 lines

Perhaps return either the number of errors or the number of successes (which turns the following if into either (ret) or (ret < nr) respectively).

I'm trying to not change how the code above expects things to work. Whilst it would be lovely to rewrite the entire code dealing with mapping memory, I don't think that's within the scope of my current project. And if I don't wish to rewrite all of libxc's memory management

I'm not talking about changing the libxc or ioctl interface, only about the internal-to-Linux interface between privcmd and mmu.c. In fact I'm only actually asking you to change the return value of the new function you are adding to the API.

Quoted 13 lines

code, I don't want to alter what values are returned or when. The current code follows what WAS happening before my changes - which isn't exactly the most fantastic thing, and I think there may actually be bugs in there, such as:

     if (ret < 0) {
         if (ret == -ENOENT)
             st->global_error = -ENOENT;
         else {
             /* Record that at least one error has happened. */
             if (st->global_error == 0)
                 st->global_error = 1;
         }
     }
Quoted 4 lines

if we enter this once with -EFAULT, and then after that with -ENOENT, global_error will say -ENOENT. I think knowing that we got an EFAULT is "higher importance" than ENOENT, but that's how the old code was working, and I'm not sure I should change it at this point.

But I think you are changing it, by this behaviour or returning the last error in the batch which causes this code to behave differently. That's what I'm asking you to avoid!

Quoted 20 lines
Quoted 13 lines
Quoted 13 lines
drivers/xen/privcmd.c +1 -1
         if (ret < 0) {
@@ -300,7 +332,7 @@ static int mmap_batch_fn(void *data, void *state)
                                 st->global_error = 1;
                 }
         }
-       st->va += PAGE_SIZE;
+       st->va += PAGE_SIZE * nr;
 
         return 0;
  }
@@ -430,8 +462,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
         state.err           = err_array;
 
Quoted 7 lines

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, int version) · line 466

Quoted 5 lines
drivers/xen/privcmd.c +2 -2
         /* mmap_batch_fn guarantees ret == 0 */
-       BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
-                            &pagelist, mmap_batch_fn, &state));
+       BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
+                                   &pagelist, mmap_batch_fn, &state));

Can we make traverse_pages and _block common by passing a block size parameter?

Quoted 6 lines

Yes of course. Is there much benefit from that? I understand that it's less code, but it also makes the original traverse_pages more complex. Not convinced it helps much - it's quite a small function, so not much extra code. Additionally, all of the callback functions will have to deal with an extra parameter (that is probably ignored in all but one place).

It depends on what the combined version ends up looking like but in general I'm in favour of one more generic function over several cloned and hacked versions of the same thing.

Ian.

#6 lore raw
Quoted 55 lines · Mats Petersson
Quoted 54 lines · Andres Lagar-Cavilla

Hi,

Quoted 35 lines · Ian Campbell
Quoted 6 lines · Mats Petersson
Quoted 4 lines

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

Quoted 2 lines
arch/x86/xen/mmu.c +1 -1
-       err = 0;
+       err = last_err;

This means that if you have 100 failures followed by one success you return success overall. Is that intentional? That doesn't seem right.

As far as I see, it doesn't mean that. last_err is only set at the beginning of the call (to zero) and if there is an error.

Ah, yes I missed that (but it still isn't right, see below).

Quoted 25 lines
Quoted 25 lines
Quoted 4 lines
drivers/xen/privcmd.c +0 -0
index 0bbbccb..8f86a44 100644

[...]

Quoted 6 lines
drivers/xen/privcmd.c +0 -2
@@ -283,12 +317,10 @@ static int mmap_batch_fn(void *data, void *state)
        if (xen_feature(XENFEAT_auto_translated_physmap))
                cur_page = pages[st->index++];
 
-       ret = xen_remap_domain_mfn_range(st->vma, st->va & PAGE_MASK, *mfnp, 1,
-                                        st->vma->vm_page_prot, st->domain,

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

Quoted 10 lines
drivers/xen/privcmd.c +4 -4
-                                        &cur_page);
-
-       /* Store error code for second pass. */
-       *(st->err++) = ret;
+       BUG_ON(nr < 0);
+       ret = xen_remap_domain_mfn_array(st->vma, st->va & PAGE_MASK, mfnp, nr,
+                                        st->err, st->vma->vm_page_prot,
+                                        st->domain, &cur_page);
 
        /* And see if it affects the global_error. */

The code which follows needs adjustment to cope with the fact that we now batch. I think it needs to walk st->err and set global_error as appropriate. This is related to my comment about the return value of xen_remap_domain_mfn_range too.

Quoted 8 lines
Quoted 3 lines

The return value, as commented above, is "either 0 or the last error we saw". There should be no need to walk the st->err, as we know if there was some error or not.

The code which follows tries to latch having seen ENOENT in preference to other errors, so you don't need 0 or the last error, you need either 0, ENOENT if one occurred somewhere in the batch or an error!=ENOENT. (or maybe its vice versa, either way the last error isn't necessarily what you need).

Yeah the error reporting interface sucks. All I can say in my defense is that it was that way when I got here.

FWIW EFAULT *takes* precedence over ENOENT.
/* If we have not had any EFAULT-like global errors then set the global
* error to -ENOENT if necessary. */
if ((ret == 0) && (state.global_error == -ENOENT))

         ret = -ENOENT;
Quoted 4 lines
Quoted 3 lines

Otherwise, any individual mapping that fails differently from ENOENT does not affect the global error. That is, in absence of global errors like EFAULT, and in absence of per-mapping ENOENT return codes, per mapping errors like EINVAL do not affect the ioctl return code which remains zero.

IIUC Ian has been pointing that your code breaks this last statement.

Sorry, are you saying the correct behaviour is to return 0 if we have EINVAL or some other error [aside from EFAULT and ENOENT]?

Yes. Note that before we get to actual mapping work we may fail with ENOMEM, EINVAL, etc (but your patch does not affect that).

Quoted 1 line

Or are you saying that my code is broken such that it does this?

I didn't grok the patch as far as being sure whether it does it or not. I'll look at your next version.

Thanks
Andres

Quoted 79 lines

I did some fixes, that I believe "does the right thing", before Christmas, but since I knew most people wouldn't be available, I didn't post it. I will try to get round to it before the end of the week.

--
Mats

Quoted 73 lines

Thanks
Andres

Quoted 70 lines
Quoted 5 lines
Quoted 5 lines

I think rather than trying to fabricate some sort of meaningful error code for an entire batch xen_remap_domain_mfn_range should just return an indication about whether there were any errors or not and leave it to the caller to figure out the overall result by looking at the err array.

Quoted 7 lines
Quoted 3 lines

Perhaps return either the number of errors or the number of successes (which turns the following if into either (ret) or (ret < nr) respectively).

I'm trying to not change how the code above expects things to work. Whilst it would be lovely to rewrite the entire code dealing with mapping memory, I don't think that's within the scope of my current project. And if I don't wish to rewrite all of libxc's memory management

I'm not talking about changing the libxc or ioctl interface, only about the internal-to-Linux interface between privcmd and mmu.c. In fact I'm only actually asking you to change the return value of the new function you are adding to the API.

Quoted 13 lines

code, I don't want to alter what values are returned or when. The current code follows what WAS happening before my changes - which isn't exactly the most fantastic thing, and I think there may actually be bugs in there, such as:

    if (ret < 0) {
        if (ret == -ENOENT)
            st->global_error = -ENOENT;
        else {
            /* Record that at least one error has happened. */
            if (st->global_error == 0)
                st->global_error = 1;
        }
    }
Quoted 4 lines

if we enter this once with -EFAULT, and then after that with -ENOENT, global_error will say -ENOENT. I think knowing that we got an EFAULT is "higher importance" than ENOENT, but that's how the old code was working, and I'm not sure I should change it at this point.

But I think you are changing it, by this behaviour or returning the last error in the batch which causes this code to behave differently. That's what I'm asking you to avoid!

Quoted 20 lines
Quoted 13 lines
Quoted 13 lines
drivers/xen/privcmd.c +1 -1
        if (ret < 0) {
@@ -300,7 +332,7 @@ static int mmap_batch_fn(void *data, void *state)
                                st->global_error = 1;
                }
        }
-       st->va += PAGE_SIZE;
+       st->va += PAGE_SIZE * nr;
 
        return 0;
 }
@@ -430,8 +462,8 @@ static long privcmd_ioctl_mmap_batch(void __user *udata, int version)
        state.err           = err_array;
 
Quoted 7 lines

drivers/xen/privcmd.c static long privcmd_ioctl_mmap_batch(void __user *udata, int version) · line 466

Quoted 5 lines
drivers/xen/privcmd.c +2 -2
        /* mmap_batch_fn guarantees ret == 0 */
-       BUG_ON(traverse_pages(m.num, sizeof(xen_pfn_t),
-                            &pagelist, mmap_batch_fn, &state));
+       BUG_ON(traverse_pages_block(m.num, sizeof(xen_pfn_t),
+                                   &pagelist, mmap_batch_fn, &state));

Can we make traverse_pages and _block common by passing a block size parameter?

Quoted 6 lines

Yes of course. Is there much benefit from that? I understand that it's less code, but it also makes the original traverse_pages more complex. Not convinced it helps much - it's quite a small function, so not much extra code. Additionally, all of the callback functions will have to deal with an extra parameter (that is probably ignored in all but one place).

It depends on what the combined version ends up looking like but in general I'm in favour of one more generic function over several cloned and hacked versions of the same thing.

Ian.

#7 lore raw
In reply to Ian Campbell
Quoted 4 lines · Ian Campbell

I've added Andres since I think this accumulation of ENOENT in err_ptr is a paging related thing, or at least I think he understands this code ;-)

Quoted 9 lines

arch/arm/xen/enlighten.c static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr, · line 93

Quoted 9 lines
arch/arm/xen/enlighten.c +2 -3
@@ -89,37 +89,112 @@ static int remap_pte_fn(pte_t *ptep, pgtable_t token, unsigned long addr,
        struct page *page = info->pages[info->index++];
        unsigned long pfn = page_to_pfn(page);
        pte_t pte = pfn_pte(pfn, info->prot);
-
-       if (map_foreign_page(pfn, info->fgmfn, info->domid))
-               return -EFAULT;
+       int err;
+       // TODO: We should really batch these updates.

This just made me realise that we need a way for XENMEM_add_to_physmap_range to return errors for each slot. I'll look into that in the new year.

Ian.