xen: arm: correct return value of raw_copy_{to/from}_guest_*, raw_clear_guest
PATCH v2 1/1 xen: arm: correct return value of raw_copy_{to/from}_guest_*, raw_clear_guest
This is a generic interface which is supposed to return the number of bytes which were not copied. Make it so.
Update the incorrect callers prepare_dtb, decode_thumb{2} and xenmem_add_to_physmap_range.
In the xenmem_add_to_physmap_range case, observe that we are not propagating errors from xenmem_add_to_physmap_one and do so.
In the decode_thumb case and an emacs magic block to decode.c
Make the flush_dcache parameter to the helper an int while at it.
---
v2: Fix xenmem_add_to_physmap_range, which was assuming -errno return codes.
Fix raw_copy_from_guest and raw_clear_guest too
--- xen/arch/arm/decode.c | 22 ++++++++++++++-------- xen/arch/arm/domain_build.c | 8 ++++---- xen/arch/arm/guestcopy.c | 20 +++++++------------- xen/arch/arm/mm.c | 23 +++++++++++++++++------ 4 files changed, 42 insertions(+), 31 deletions(-)
xen/arch/arm/decode.c +14 -8
@@ -36,12 +36,10 @@ static void update_dabt(struct hsr_dabt *dabt, int reg,static int decode_thumb2 (register_t pc,struct hsr_dabt *dabt,uint16_t hw1) {uint16_t hw2; -int rc;uint16_t rt; - rc =raw_copy_from_guest (&hw2, (void *__user )(pc +2 ),sizeof (hw2)); -if ( rc ) -return rc; +if (raw_copy_from_guest (&hw2, (void *__user )(pc +2 ),sizeof (hw2)) ) +return -EFAULT ; rt = (hw2 >>12 ) &0x7 ;
@@ -88,11 +86,9 @@ bad_thumb2:static int decode_thumb (register_t pc,struct hsr_dabt *dabt) {uint16_t instr; -int rc; - rc =raw_copy_from_guest (&instr, (void *__user )pc,sizeof (instr)); -if ( rc ) -return rc; +if (raw_copy_from_guest (&instr, (void *__user )pc,sizeof (instr)) ) +return -EFAULT ;switch ( instr >>12 ) {
@@ -163,3 +159,13 @@ int decode_instruction(const struct cpu_user_regs *regs, struct hsr_dabt *dabt)return 1 ; } + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/
xen/arch/arm/domain_build.c +4 -4
@@ -907,15 +907,15 @@ static int prepare_dtb(struct domain *d, struct kernel_info *kinfo)static void dtb_load (struct kernel_info *kinfo) {void *__user dtb_virt = (void *__user )(register_t )kinfo->dtb_paddr; -unsigned long rc; +unsigned long left;printk ("Loading dom0 DTB to 0x%" PRIpaddr"-0x%" PRIpaddr"\n" , kinfo->dtb_paddr, kinfo->dtb_paddr +fdt_totalsize (kinfo->fdt)); - rc =raw_copy_to_guest_flush_dcache (dtb_virt, kinfo->fdt, + left =raw_copy_to_guest_flush_dcache (dtb_virt, kinfo->fdt,fdt_totalsize (kinfo->fdt)); -if ( rc !=0 ) -panic ("Unable to copy the DTB to dom0 memory (rc = %lu)" , rc); +if ( left !=0 ) +panic ("Unable to copy the DTB to dom0 memory (left = %lu bytes)" , left);xfree (kinfo->fdt); }
xen/arch/arm/guestcopy.c +7 -13
@@ -6,21 +6,19 @@#include <asm/guest_access.h> static unsigned long raw_copy_to_guest_helper (void *to,const void *from, -unsigned len,unsigned flush_dcache) +unsigned len,int flush_dcache) {/* XXX needs to handle faults */ unsigned offset = (vaddr_t )to & ~PAGE_MASK ;while ( len ) { -int rc;paddr_t g;void *p;unsigned size =min (len, (unsigned )PAGE_SIZE - offset); - rc =gvirt_to_maddr ((vaddr_t ) to, &g); -if ( rc ) -return rc; +if (gvirt_to_maddr ((vaddr_t ) to, &g) ) +return len; p =map_domain_page (g>>PAGE_SHIFT ); p += offset;
@@ -56,14 +54,12 @@ unsigned long raw_clear_guest(void *to, unsigned len)while ( len ) { -int rc;paddr_t g;void *p;unsigned size =min (len, (unsigned )PAGE_SIZE - offset); - rc =gvirt_to_maddr ((vaddr_t ) to, &g); -if ( rc ) -return rc; +if (gvirt_to_maddr ((vaddr_t ) to, &g) ) +return len; p =map_domain_page (g>>PAGE_SHIFT ); p += offset;
@@ -84,14 +80,12 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned lewhile ( len ) { -int rc;paddr_t g;void *p;unsigned size =min (len, (unsigned )(PAGE_SIZE - offset)); - rc =gvirt_to_maddr ((vaddr_t ) from &PAGE_MASK , &g); -if ( rc ) -return rc; +if (gvirt_to_maddr ((vaddr_t ) from &PAGE_MASK , &g) ) +return len; p =map_domain_page (g>>PAGE_SHIFT ); p += ((vaddr_t )from & (~PAGE_MASK ));
xen/arch/arm/mm.c +17 -6
@@ -1067,22 +1067,33 @@ static int xenmem_add_to_physmap_range(struct domain *d,xen_ulong_t idx;xen_pfn_t gpfn; - rc =copy_from_guest_offset (&idx, xatpr->idxs, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&idx, xatpr->idxs, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ; goto out; + } - rc =copy_from_guest_offset (&gpfn, xatpr->gpfns, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&gpfn, xatpr->gpfns, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ; goto out; + } rc =xenmem_add_to_physmap_one (d, xatpr->space, xatpr->foreign_domid, idx, gpfn); - - rc =copy_to_guest_offset (xatpr->errs, xatpr->size-1 , &rc,1 );if ( rc <0 ) goto out; +if (unlikely (copy_to_guest_offset (xatpr->errs, + xatpr->size-1 , &rc,1 )) ); + { + rc = -EFAULT ; + goto out; + } + xatpr->size--;/* Check for continuation if it's not the last interation */
Signature · 1 line
1.7.10.4
Quoted 6 lines · Ian Campbell
This is a generic interface which is supposed to return the number of bytes which were not copied. Make it so.
Update the incorrect callers
prepare_dtb, decode_thumb{2} andxenmem_add_to_physmap_range.
Quoted 8 lines
In the
xenmem_add_to_physmap_rangecase, observe that we are not propagating errors fromxenmem_add_to_physmap_oneand do so.In the
decode_thumbcase and an emacs magic block todecode.cMake the
flush_dcacheparameter to the helper an int while at it.Signed-off-by Ian Campbell <[email protected]>
Quoted 189 lines
---
v2: Fixxenmem_add_to_physmap_range, which was assuming -errno return codes.
Fixraw_copy_from_guestandraw_clear_guesttoo--- xen/arch/arm/decode.c | 22 ++++++++++++++-------- xen/arch/arm/domain_build.c | 8 ++++---- xen/arch/arm/guestcopy.c | 20 +++++++------------- xen/arch/arm/mm.c | 23 +++++++++++++++++------ 4 files changed, 42 insertions(+), 31 deletions(-)xen/arch/arm/decode.c +14
-8@@ -36,12 +36,10 @@ static void update_dabt(struct hsr_dabt *dabt, int reg,static int decode_thumb2 (register_t pc,struct hsr_dabt *dabt,uint16_t hw1) {uint16_t hw2; -int rc;uint16_t rt; - rc =raw_copy_from_guest (&hw2, (void *__user )(pc +2 ),sizeof (hw2)); -if ( rc ) -return rc; +if (raw_copy_from_guest (&hw2, (void *__user )(pc +2 ),sizeof (hw2)) ) +return -EFAULT ; rt = (hw2 >>12 ) &0x7 ;@@ -88,11 +86,9 @@ bad_thumb2:static int decode_thumb (register_t pc,struct hsr_dabt *dabt) {uint16_t instr; -int rc; - rc =raw_copy_from_guest (&instr, (void *__user )pc,sizeof (instr)); -if ( rc ) -return rc; +if (raw_copy_from_guest (&instr, (void *__user )pc,sizeof (instr)) ) +return -EFAULT ;switch ( instr >>12 ) {@@ -163,3 +159,13 @@ int decode_instruction(const struct cpu_user_regs *regs, struct hsr_dabt *dabt)return 1 ; } + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ xen/arch/arm/domain_build.c +4
-4@@ -907,15 +907,15 @@ static int prepare_dtb(struct domain *d, struct kernel_info *kinfo)static void dtb_load (struct kernel_info *kinfo) {void *__user dtb_virt = (void *__user )(register_t )kinfo->dtb_paddr; -unsigned long rc; +unsigned long left;printk ("Loading dom0 DTB to 0x%" PRIpaddr"-0x%" PRIpaddr"\n" , kinfo->dtb_paddr, kinfo->dtb_paddr +fdt_totalsize (kinfo->fdt)); - rc =raw_copy_to_guest_flush_dcache (dtb_virt, kinfo->fdt, + left =raw_copy_to_guest_flush_dcache (dtb_virt, kinfo->fdt,fdt_totalsize (kinfo->fdt)); -if ( rc !=0 ) -panic ("Unable to copy the DTB to dom0 memory (rc = %lu)" , rc); +if ( left !=0 ) +panic ("Unable to copy the DTB to dom0 memory (left = %lu bytes)" , left);xfree (kinfo->fdt); }xen/arch/arm/guestcopy.c +7
-13@@ -6,21 +6,19 @@#include <asm/guest_access.h> static unsigned long raw_copy_to_guest_helper (void *to,const void *from, -unsigned len,unsigned flush_dcache) +unsigned len,int flush_dcache) {/* XXX needs to handle faults */ unsigned offset = (vaddr_t )to & ~PAGE_MASK ;while ( len ) { -int rc;paddr_t g;void *p;unsigned size =min (len, (unsigned )PAGE_SIZE - offset); - rc =gvirt_to_maddr ((vaddr_t ) to, &g); -if ( rc ) -return rc; +if (gvirt_to_maddr ((vaddr_t ) to, &g) ) +return len; p =map_domain_page (g>>PAGE_SHIFT ); p += offset;@@ -56,14 +54,12 @@ unsigned long raw_clear_guest(void *to, unsigned len)while ( len ) { -int rc;paddr_t g;void *p;unsigned size =min (len, (unsigned )PAGE_SIZE - offset); - rc =gvirt_to_maddr ((vaddr_t ) to, &g); -if ( rc ) -return rc; +if (gvirt_to_maddr ((vaddr_t ) to, &g) ) +return len; p =map_domain_page (g>>PAGE_SHIFT ); p += offset;@@ -84,14 +80,12 @@ unsigned long raw_copy_from_guest(void *to, const void __user *from, unsigned lewhile ( len ) { -int rc;paddr_t g;void *p;unsigned size =min (len, (unsigned )(PAGE_SIZE - offset)); - rc =gvirt_to_maddr ((vaddr_t ) from &PAGE_MASK , &g); -if ( rc ) -return rc; +if (gvirt_to_maddr ((vaddr_t ) from &PAGE_MASK , &g) ) +return len; p =map_domain_page (g>>PAGE_SHIFT ); p += ((vaddr_t )from & (~PAGE_MASK ));xen/arch/arm/mm.c +17
-6@@ -1067,22 +1067,33 @@ static int xenmem_add_to_physmap_range(struct domain *d,xen_ulong_t idx;xen_pfn_t gpfn; - rc =copy_from_guest_offset (&idx, xatpr->idxs, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&idx, xatpr->idxs, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ; goto out; + } - rc =copy_from_guest_offset (&gpfn, xatpr->gpfns, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&gpfn, xatpr->gpfns, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ; goto out; + } rc =xenmem_add_to_physmap_one (d, xatpr->space, xatpr->foreign_domid, idx, gpfn); - - rc =copy_to_guest_offset (xatpr->errs, xatpr->size-1 , &rc,1 );if ( rc <0 ) goto out; +if (unlikely (copy_to_guest_offset (xatpr->errs, + xatpr->size-1 , &rc,1 )) ); + { + rc = -EFAULT ; + goto out; + } + xatpr->size--;/* Check for continuation if it's not the last interation */
Signature · 1 line
Julien Grall
Ian Campbell
AppliedQuoted 10 lines · Julien Grall
Quoted 9 lines · Ian Campbell
This is a generic interface which is supposed to return the number of bytes which were not copied. Make it so.
Update the incorrect callers
prepare_dtb, decode_thumb{2} andxenmem_add_to_physmap_range.In the
xenmem_add_to_physmap_rangecase, observe that we are not propagating errors fromxenmem_add_to_physmap_oneand do so.
Quoted 6 lines
Quoted 5 lines
In the
decode_thumbcase and an emacs magic block todecode.cMake the
flush_dcacheparameter to the helper an int while at it.Signed-off-by Ian Campbell <[email protected]>Acked-by Julien Grall <[email protected]>
Applied, thanks.
Quoted 6 lines · Ian Campbell
This is a generic interface which is supposed to return the number of bytes which were not copied. Make it so.
Update the incorrect callers
prepare_dtb, decode_thumb{2} andxenmem_add_to_physmap_range.
Quoted 6 lines
In the
xenmem_add_to_physmap_rangecase, observe that we are not propagating errors fromxenmem_add_to_physmap_oneand do so.In the
decode_thumbcase and an emacs magic block todecode.cMake the
flush_dcacheparameter to the helper an int while at it.
Actually this patch is buggy, I can't anymore create a guest on midway. I get lots of:
Failed to map pfn to mfn rc:-14:0 pfn:3efd8 mfn:802a3
Failed to map pfn to mfn rc:-14:0 pfn:3e019 mfn:802a4
Failed to map pfn to mfn rc:-14:0 pfn:3ed92 mfn:802a5
...
Quoted 5 lines
Signed-off-by Ian Campbell <[email protected]>---
v2: Fixxenmem_add_to_physmap_range, which was assuming -errno return codes.
Fixraw_copy_from_guestandraw_clear_guesttoo
---
[..]
Quoted 23 lines
xen/arch/arm/mm.c +9
-4@@ -1067,22 +1067,33 @@ static int xenmem_add_to_physmap_range(struct domain *d,xen_ulong_t idx;xen_pfn_t gpfn; - rc =copy_from_guest_offset (&idx, xatpr->idxs, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&idx, xatpr->idxs, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ; goto out; + } - rc =copy_from_guest_offset (&gpfn, xatpr->gpfns, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&gpfn, xatpr->gpfns, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ;
xen/arch/arm/mm.c static int xenmem_add_to_physmap_range(struct domain *d, · line 1088
Quoted 10 lines
xen/arch/arm/mm.c +1
-2goto out; + } rc =xenmem_add_to_physmap_one (d, xatpr->space, xatpr->foreign_domid, idx, gpfn); - - rc =copy_to_guest_offset (xatpr->errs, xatpr->size-1 , &rc,1 );if ( rc <0 ) goto out;
copy_to_guest_offset was fine before the "if ( rc < 0 )" because we want to copy the error in the xatpr->errs.
xen/arch/arm/mm.c static int xenmem_add_to_physmap_range(struct domain *d, · line 1090
Quoted 2 lines
xen/arch/arm/mm.c +2
-0+if (unlikely (copy_to_guest_offset (xatpr->errs, + xatpr->size-1 , &rc,1 )) );
You forgot to remote the ';' at the end.
Here a patch to fix this commit
======================================================================================
commit ce9b426051d742e116d43cbd0e15dc47f5fab88b
Author: Julien Grall <[email protected]> Date: Mon Dec 9 18:29:50 2013 +0000
xen/arm: Fix regression after commit d963923
The commit d963923 "xen: arm: correct return value of raw_copy_{to/from}_guest_*, raw_clear_guest" doesn't permit to boot guest on Xen ARM.
Also we want to get the right rc in the error arrays, so move back copy_to_guest function to the previous place.
xen/arch/arm/mm.c +4 -3
@@ -1108,16 +1108,17 @@ static int xenmem_add_to_physmap_range(struct domain *d, rc =xenmem_add_to_physmap_one (d, xatpr->space, xatpr->foreign_domid, idx, gpfn); -if ( rc <0 ) - goto out;if (unlikely (copy_to_guest_offset (xatpr->errs, - xatpr->size-1 , &rc,1 )) ); + xatpr->size-1 , &rc,1 )) ) { rc = -EFAULT ; goto out; } +if ( rc <0 ) + goto out; + xatpr->size--;/* Check for continuation if it's not the last interation */
Signature · 1 line
Julien Grall
Quoted 64 lines · Julien Grall
Quoted 6 lines · Ian Campbell
This is a generic interface which is supposed to return the number of bytes which were not copied. Make it so.
Update the incorrect callers
prepare_dtb, decode_thumb{2} andxenmem_add_to_physmap_range.Quoted 6 lines
In the
xenmem_add_to_physmap_rangecase, observe that we are not propagating errors fromxenmem_add_to_physmap_oneand do so.In the
decode_thumbcase and an emacs magic block todecode.cMake the
flush_dcacheparameter to the helper an int while at it.Actually this patch is buggy, I can't anymore create a guest on midway. I get lots of:
Failed to map pfn to mfn rc:-14:0 pfn:3efd8 mfn:802a3
Failed to map pfn to mfn rc:-14:0 pfn:3e019 mfn:802a4
Failed to map pfn to mfn rc:-14:0 pfn:3ed92 mfn:802a5
...Quoted 5 lines
Signed-off-by Ian Campbell <[email protected]>---
v2: Fixxenmem_add_to_physmap_range, which was assuming -errno return codes.
Fixraw_copy_from_guestandraw_clear_guesttoo
---[..]
Quoted 23 lines
xen/arch/arm/mm.c +9
-4@@ -1067,22 +1067,33 @@ static int xenmem_add_to_physmap_range(struct domain *d,xen_ulong_t idx;xen_pfn_t gpfn; - rc =copy_from_guest_offset (&idx, xatpr->idxs, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&idx, xatpr->idxs, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ; goto out; + } - rc =copy_from_guest_offset (&gpfn, xatpr->gpfns, xatpr->size-1 ,1 ); -if ( rc <0 ) +if (unlikely (copy_from_guest_offset (&gpfn, xatpr->gpfns, + xatpr->size-1 ,1 )) ) + { + rc = -EFAULT ;xen/arch/arm/mm.c static int xenmem_add_to_physmap_range(struct domain *d, · line 1088
Quoted 10 lines
xen/arch/arm/mm.c +1
-2goto out; + } rc =xenmem_add_to_physmap_one (d, xatpr->space, xatpr->foreign_domid, idx, gpfn); - - rc =copy_to_guest_offset (xatpr->errs, xatpr->size-1 , &rc,1 );if ( rc <0 ) goto out;
copy_to_guest_offsetwas fine before the "if ( rc < 0 )" because we want to copy the error in thexatpr->errs.
Quoted 4 lines
xen/arch/arm/mm.c static int xenmem_add_to_physmap_range(struct domain *d, · line 1090
Quoted 2 lines
xen/arch/arm/mm.c +2
-0+if (unlikely (copy_to_guest_offset (xatpr->errs, + xatpr->size-1 , &rc,1 )) );You forgot to remote the ';' at the end.
Damn, that's what I get for making a last minute change to the patch!
Quoted 9 lines
Here a patch to fix this commit
======================================================================================
commit
ce9b426051d742e116d43cbd0e15dc47f5fab88bAuthor: Julien Grall <[email protected]> Date: Mon Dec 9 18:29:50 2013 +0000
Quoted 8 lines
xen/arm: Fix regression after commit d963923
The commit d963923 "xen: arm: correct return value of raw_copy_{to/from}_guest_*,
raw_clear_guest" doesn't permit to boot guest on Xen ARM.Also we want to get the right rc in the error arrays, so move back
copy_to_guestfunction to the previous place.
Ah, I missed that the error check was in the copy_to_guest_offset and thought it wasn't being tested at all, which is why I changed it.
I tried to clarify what was going on by expanding the commit log to:
Remove the stray semicolon from the end of the if statement.
Also we want to get the right rc in the error arrays, so we need to do the copy_to_guest_offset before checking the rc returned by xenmem_add_to_physmap_one.
Quoted 1 line
Signed-off-by Julien Grall <[email protected]>
Acked + applied, thanks for catching this.
Ian.