From: "Jan Beulich" Subject: [PATCH 4/4] linux-2.6.18/gntdev: misc cleanup Date: Thu, 10 May 2012 16:19:06 +0100 Message-ID: <4FABF88A0200007800082C93@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part2A04C37A.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part2A04C37A.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline - eliminate struct gntdev_grant_info's dev_bus_addr member (which was used to communicate a value inside the main loop of gntdev_mmap()) - correct types - use a local variable 'grants' in gntdev_mmap() to improve readability - avoid re-calculating already calculated values - properly check for out of range values - combine both GNTTABOP_unmap_grant_ref calls in gntdev_clear_pte() into a single hypercall - adjust error diagnostic in unmap ioctl to be more useful and better legible Signed-off-by: Jan Beulich --- a/drivers/xen/gntdev/gntdev.c +++ b/drivers/xen/gntdev/gntdev.c @@ -80,7 +80,6 @@ typedef struct gntdev_grant_info { grant_ref_t ref; grant_handle_t kernel_handle; grant_handle_t user_handle; - uint64_t dev_bus_addr; } valid; } u; } gntdev_grant_info_t; @@ -473,14 +472,14 @@ static int gntdev_mmap (struct file *fli { struct gnttab_map_grant_ref op; unsigned long slot_index =3D vma->vm_pgoff; - unsigned long kernel_vaddr, user_vaddr; - uint32_t size =3D (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; + unsigned long kernel_vaddr, user_vaddr, mfn; + unsigned long size =3D (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;= uint64_t ptep; int ret, exit_ret; - int flags; - int i; + unsigned int i, flags; struct page *page; gntdev_file_private_data_t *private_data =3D flip->private_data; + gntdev_grant_info_t *grants; struct vm_foreign_map *foreign_map; =20 if (unlikely(!private_data)) { @@ -490,17 +489,19 @@ static int gntdev_mmap (struct file *fli =20 /* Test to make sure that the grants array has been initialised. = */ down_read(&private_data->grants_sem); - if (unlikely(!private_data->grants)) { - up_read(&private_data->grants_sem); + grants =3D private_data->grants; + up_read(&private_data->grants_sem); + + if (unlikely(!grants)) { printk(KERN_ERR "Attempted to mmap before ioctl.\n"); return -EINVAL; } - up_read(&private_data->grants_sem); + grants +=3D slot_index; =20 - if (unlikely((size <=3D 0) ||=20 - (size + slot_index) > private_data->grants_size)) { + if (unlikely(size + slot_index <=3D slot_index || + size + slot_index > private_data->grants_size)) { printk(KERN_ERR "Invalid number of pages or offset" - "(num_pages =3D %d, first_slot =3D %ld).\n", + "(num_pages =3D %lu, first_slot =3D %lu)\n", size, slot_index); return -ENXIO; } @@ -521,11 +522,10 @@ static int gntdev_mmap (struct file *fli /* Slots must be in the NOT_YET_MAPPED state. */ down_write(&private_data->grants_sem); for (i =3D 0; i < size; ++i) { - if (private_data->grants[slot_index + i].state !=3D=20 - GNTDEV_SLOT_NOT_YET_MAPPED) { - printk(KERN_ERR "Slot (index =3D %ld) is in the = wrong " + if (grants[i].state !=3D GNTDEV_SLOT_NOT_YET_MAPPED) { + printk(KERN_ERR "Slot (index =3D %lu) is in the = wrong " "state (%d).\n", slot_index + i,=20 - private_data->grants[slot_index + i].state);= + grants[i].state); up_write(&private_data->grants_sem); kfree(foreign_map); return -EINVAL; @@ -565,14 +565,11 @@ static int gntdev_mmap (struct file *fli flags |=3D GNTMAP_readonly; =20 kernel_vaddr =3D get_kernel_vaddr(private_data, slot_index = + i); - user_vaddr =3D get_user_vaddr(vma, i); page =3D private_data->foreign_pages[slot_index + i]; =20 gnttab_set_map_op(&op, kernel_vaddr, flags, =20 - private_data->grants[slot_index+i] - .u.valid.ref,=20 - private_data->grants[slot_index+i] - .u.valid.domid); + grants[i].u.valid.ref, + grants[i].u.valid.domid); =20 /* Carry out the mapping of the grant reference. */ ret =3D HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref,= =20 @@ -583,10 +580,8 @@ static int gntdev_mmap (struct file *fli printk(KERN_ERR "Error mapping the grant = reference " "into the kernel (%d). domid =3D = %d; ref =3D %d\n", op.status, - private_data->grants[slot_index+i] - .u.valid.domid, - private_data->grants[slot_index+i] - .u.valid.ref); + grants[i].u.valid.domid, + grants[i].u.valid.ref); else /* Propagate eagain instead of trying to = fix it up */ exit_ret =3D -EAGAIN; @@ -597,16 +592,14 @@ static int gntdev_mmap (struct file *fli SetPageReserved(page); =20 /* Record the grant handle, for use in the unmap operation.= */ - private_data->grants[slot_index+i].u.valid.kernel_handle = =3D=20 - op.handle; - private_data->grants[slot_index+i].u.valid.dev_bus_addr = =3D=20 - op.dev_bus_addr; + grants[i].u.valid.kernel_handle =3D op.handle; =09 - private_data->grants[slot_index+i].state =3D GNTDEV_SLOT_MA= PPED; - private_data->grants[slot_index+i].u.valid.user_handle =3D - GNTDEV_INVALID_HANDLE; + grants[i].state =3D GNTDEV_SLOT_MAPPED; + grants[i].u.valid.user_handle =3D GNTDEV_INVALID_HANDLE; =20 /* Now perform the mapping to user space. */ + user_vaddr =3D get_user_vaddr(vma, i); + if (xen_feature(XENFEAT_auto_translated_physmap)) { /* USING SHADOW PAGE TABLES. */ /* In this case, we simply insert the page into = the VM @@ -622,11 +615,11 @@ static int gntdev_mmap (struct file *fli /* In this case, we map the grant(s) straight into user * space. */ + mfn =3D op.dev_bus_addr >> PAGE_SHIFT; =20 /* Get the machine address of the PTE for the user page. = */ if ((ret =3D create_lookup_pte_addr(vma->vm_mm, - vma->vm_start - + (i << PAGE_SHIFT), + user_vaddr, &ptep))) { printk(KERN_ERR "Error obtaining PTE pointer = (%d)\n", @@ -636,9 +629,6 @@ static int gntdev_mmap (struct file *fli =20 /* Configure the map operation. */ =20 - /* The reference is to be used by host CPUs. */ - flags =3D GNTMAP_host_map; - /* Specifies a user space mapping. */ flags |=3D GNTMAP_application_map; =20 @@ -647,14 +637,9 @@ static int gntdev_mmap (struct file *fli */ flags |=3D GNTMAP_contains_pte; =20 - if (!(vma->vm_flags & VM_WRITE)) - flags |=3D GNTMAP_readonly; - gnttab_set_map_op(&op, ptep, flags, - private_data->grants[slot_index+i] - .u.valid.ref, - private_data->grants[slot_index+i] - .u.valid.domid); + grants[i].u.valid.ref, + grants[i].u.valid.domid); =20 /* Carry out the mapping of the grant reference. */ ret =3D HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, @@ -664,10 +649,8 @@ static int gntdev_mmap (struct file *fli printk(KERN_ERR "Error mapping the grant reference = " "into user space (%d). domid =3D %d; ref = =3D %d\n", op.status, - private_data->grants[slot_index+i].u - .valid.domid, - private_data->grants[slot_index+i].u - .valid.ref); + grants[i].u.valid.domid, + grants[i].u.valid.ref); /* This should never happen after we've mapped = into * the kernel space. */ BUG_ON(op.status =3D=3D GNTST_eagain); @@ -677,15 +660,11 @@ static int gntdev_mmap (struct file *fli /* Record the grant handle, for use in the unmap * operation. */ - private_data->grants[slot_index+i].u.valid.user_handle - =3D op.handle; + grants[i].u.valid.user_handle =3D op.handle; =20 /* Update p2m structure with the new mapping. */ set_phys_to_machine(__pa(kernel_vaddr) >> PAGE_SHIFT, - FOREIGN_FRAME(private_data-> - grants[slot_index+i] - .u.valid.dev_bus_addr - >> PAGE_SHIFT)); + FOREIGN_FRAME(mfn)); } exit_ret =3D 0; =20 @@ -715,9 +694,11 @@ undo_map_out: static pte_t gntdev_clear_pte(struct vm_area_struct *vma, unsigned long = addr, pte_t *ptep, int is_fullmm) { - int slot_index, ret; + int ret; + unsigned int nr; + unsigned long slot_index; pte_t copy; - struct gnttab_unmap_grant_ref op; + struct gnttab_unmap_grant_ref op[2]; gntdev_file_private_data_t *private_data; =20 /* THIS IS VERY UNPLEASANT: do_mmap_pgoff() will set the vma->vm_fi= le @@ -750,36 +731,35 @@ static pte_t gntdev_clear_pte(struct vm_ return copy; } =20 - /* First, we clear the user space mapping, if it has been made. - */ + /* Clear the user space mapping, if it has been made. */ if (private_data->grants[slot_index].u.valid.user_handle !=3D - GNTDEV_INVALID_HANDLE && - !xen_feature(XENFEAT_auto_translated_physmap)) { - /* NOT USING SHADOW PAGE TABLES. */ - gnttab_set_unmap_op(&op, ptep_to_machine(ptep), + GNTDEV_INVALID_HANDLE) { + /* NOT USING SHADOW PAGE TABLES (and user handle valid). = */ + gnttab_set_unmap_op(&op[0], ptep_to_machine(ptep), GNTMAP_contains_pte, private_data->grants[slot_index].u.vali= d .user_handle); - ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,= - &op, 1); - BUG_ON(ret); - if (op.status !=3D GNTST_okay) - printk("User unmap grant status =3D %d\n", = op.status); + nr =3D 1; } else { - /* USING SHADOW PAGE TABLES. */ + /* USING SHADOW PAGE TABLES (or user handle invalid). */ pte_clear_full(vma->vm_mm, addr, ptep, is_fullmm); + nr =3D 0; } =20 - /* Finally, we unmap the grant from kernel space. */ - gnttab_set_unmap_op(&op, + /* We always unmap the grant from kernel space. */ + gnttab_set_unmap_op(&op[nr], get_kernel_vaddr(private_data, slot_index), GNTMAP_host_map, private_data->grants[slot_index].u.valid .kernel_handle); - ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, = 1); + + ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, op, nr = + 1); BUG_ON(ret); - if (op.status !=3D GNTST_okay) - printk("Kernel unmap grant status =3D %d\n", op.status); + + if (nr && op[0].status !=3D GNTST_okay) + printk("User unmap grant status =3D %d\n", op[0].status); + if (op[nr].status !=3D GNTST_okay) + printk("Kernel unmap grant status =3D %d\n", op[nr].status)= ; =20 /* Return slot to the not-yet-mapped state, so that it may be * mapped again, or removed by a subsequent ioctl. @@ -915,7 +895,8 @@ private_data_initialised: return -EFAULT; =20 start_index =3D op.index >> PAGE_SHIFT; - if (start_index + op.count > private_data->grants_size) + if (start_index + op.count < start_index || + start_index + op.count > private_data->grants_size) return -EINVAL; =20 down_write(&private_data->grants_sem); @@ -924,28 +905,29 @@ private_data_initialised: * state. */ for (i =3D 0; i < op.count; ++i) { - if (unlikely - (private_data->grants[start_index + i].state - !=3D GNTDEV_SLOT_NOT_YET_MAPPED)) { - if (private_data->grants[start_index + = i].state - =3D=3D GNTDEV_SLOT_INVALID) { - printk(KERN_ERR - "Tried to remove an invalid = " - "grant at offset 0x%x.", - (start_index + i)=20 - << PAGE_SHIFT); - rc =3D -EINVAL; - } else { - printk(KERN_ERR - "Tried to remove a grant = which " - "is currently mmap()-ed at = " - "offset 0x%x.", - (start_index + i)=20 - << PAGE_SHIFT); - rc =3D -EBUSY; - } - goto unmap_out; + const char *what; + + switch (private_data->grants[start_index + = i].state) { + case GNTDEV_SLOT_NOT_YET_MAPPED: + continue; + case GNTDEV_SLOT_INVALID: + what =3D "invalid"; + rc =3D -EINVAL; + break; + case GNTDEV_SLOT_MAPPED: + what =3D "currently mmap()-ed"; + rc =3D -EBUSY; + break; + default: + what =3D "in an invalid state"; + rc =3D -ENXIO; + break; } + printk(KERN_ERR "%s[%d] tried to remove a grant" + " which is %s at %#x+%#x\n", + current->comm, current->pid, + what, start_index, i); + goto unmap_out; } =20 down_write(&private_data->free_list_sem); --=__Part2A04C37A.0__= Content-Type: text/plain; name="xen-gntdev-cleanup.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-gntdev-cleanup.patch" gntdev: misc cleanup=0A=0A- eliminate struct gntdev_grant_info's dev_bus_ad= dr member (which was=0A used to communicate a value inside the main loop = of gntdev_mmap())=0A- correct types=0A- use a local variable 'grants' in = gntdev_mmap() to improve readability=0A- avoid re-calculating already = calculated values=0A- properly check for out of range values=0A- combine = both GNTTABOP_unmap_grant_ref calls in gntdev_clear_pte()=0A into a = single hypercall=0A- adjust error diagnostic in unmap ioctl to be more = useful and better=0A legible=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/drivers/xen/gntdev/gntdev.c=0A+++ b/drivers/xen/gntdev/gn= tdev.c=0A@@ -80,7 +80,6 @@ typedef struct gntdev_grant_info {=0A = grant_ref_t ref;=0A grant_handle_t kernel_handl= e;=0A grant_handle_t user_handle;=0A- = uint64_t dev_bus_addr;=0A } valid;=0A } u;=0A } = gntdev_grant_info_t;=0A@@ -473,14 +472,14 @@ static int gntdev_mmap = (struct file *fli=0A {=0A struct gnttab_map_grant_ref op;=0A = unsigned long slot_index =3D vma->vm_pgoff;=0A- unsigned long kernel_vaddr,= user_vaddr;=0A- uint32_t size =3D (vma->vm_end - vma->vm_start) >> = PAGE_SHIFT;=0A+ unsigned long kernel_vaddr, user_vaddr, mfn;=0A+ = unsigned long size =3D (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;=0A = uint64_t ptep;=0A int ret, exit_ret;=0A- int flags;=0A- int i;=0A+ = unsigned int i, flags;=0A struct page *page;=0A gntdev_file_private= _data_t *private_data =3D flip->private_data;=0A+ gntdev_grant_info_t= *grants;=0A struct vm_foreign_map *foreign_map;=0A =0A if = (unlikely(!private_data)) {=0A@@ -490,17 +489,19 @@ static int gntdev_mmap = (struct file *fli=0A =0A /* Test to make sure that the grants array = has been initialised. */=0A down_read(&private_data->grants_sem);=0A- = if (unlikely(!private_data->grants)) {=0A- up_read(&private_da= ta->grants_sem);=0A+ grants =3D private_data->grants;=0A+ up_read(&pr= ivate_data->grants_sem);=0A+=0A+ if (unlikely(!grants)) {=0A = printk(KERN_ERR "Attempted to mmap before ioctl.\n");=0A = return -EINVAL;=0A }=0A- up_read(&private_data->grants_sem);=0A+ = grants +=3D slot_index;=0A =0A- if (unlikely((size <=3D 0) || =0A- = (size + slot_index) > private_data->grants_size)) {=0A+ if = (unlikely(size + slot_index <=3D slot_index ||=0A+ size = + slot_index > private_data->grants_size)) {=0A printk(KERN= _ERR "Invalid number of pages or offset"=0A- "(num_pages = =3D %d, first_slot =3D %ld).\n",=0A+ "(num_pages =3D = %lu, first_slot =3D %lu)\n",=0A size, slot_index);= =0A return -ENXIO;=0A }=0A@@ -521,11 +522,10 @@ static = int gntdev_mmap (struct file *fli=0A /* Slots must be in the NOT_YET_MAP= PED state. */=0A down_write(&private_data->grants_sem);=0A = for (i =3D 0; i < size; ++i) {=0A- if (private_data->grants[sl= ot_index + i].state !=3D =0A- GNTDEV_SLOT_NOT_YET_MAPPED) = {=0A- printk(KERN_ERR "Slot (index =3D %ld) is in the = wrong "=0A+ if (grants[i].state !=3D GNTDEV_SLOT_NOT_YET_MAPPED= ) {=0A+ printk(KERN_ERR "Slot (index =3D %lu) is in the = wrong "=0A "state (%d).\n", slot_index + i, = =0A- private_data->grants[slot_index + i].state);= =0A+ grants[i].state);=0A = up_write(&private_data->grants_sem);=0A kfree(forei= gn_map);=0A return -EINVAL;=0A@@ -565,14 +565,11 @@ = static int gntdev_mmap (struct file *fli=0A flags |=3D = GNTMAP_readonly;=0A =0A kernel_vaddr =3D get_kernel_vaddr(p= rivate_data, slot_index + i);=0A- user_vaddr =3D get_user_vad= dr(vma, i);=0A page =3D private_data->foreign_pages[slot_index + = i];=0A =0A gnttab_set_map_op(&op, kernel_vaddr, flags, =0A- = private_data->grants[slot_index+i]=0A- = .u.valid.ref, =0A- = private_data->grants[slot_index+i]=0A- = .u.valid.domid);=0A+ grants[i].u.valid.ref,=0A= + grants[i].u.valid.domid);=0A =0A = /* Carry out the mapping of the grant reference. */=0A ret =3D = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, =0A@@ -583,10 +580,8 @@ = static int gntdev_mmap (struct file *fli=0A = printk(KERN_ERR "Error mapping the grant reference "=0A = "into the kernel (%d). domid =3D %d; ref =3D %d\n",=0A = op.status,=0A- = private_data->grants[slot_index+i]=0A- = .u.valid.domid,=0A- private_data->grants= [slot_index+i]=0A- .u.valid.ref);=0A+ = grants[i].u.valid.domid,=0A+ = grants[i].u.valid.ref);=0A else=0A = /* Propagate eagain instead of trying to fix it up = */=0A exit_ret =3D -EAGAIN;=0A@@ -597,16 +592,14 = @@ static int gntdev_mmap (struct file *fli=0A SetPageReserved(pag= e);=0A =0A /* Record the grant handle, for use in the unmap = operation. */=0A- private_data->grants[slot_index+i].u.valid.= kernel_handle =3D =0A- op.handle;=0A- private_dat= a->grants[slot_index+i].u.valid.dev_bus_addr =3D =0A- = op.dev_bus_addr;=0A+ grants[i].u.valid.kernel_handle =3D = op.handle;=0A =0A- private_data->grants[slot_index+i].= state =3D GNTDEV_SLOT_MAPPED;=0A- private_data->grants[slot_i= ndex+i].u.valid.user_handle =3D=0A- GNTDEV_INVALID_HAND= LE;=0A+ grants[i].state =3D GNTDEV_SLOT_MAPPED;=0A+ = grants[i].u.valid.user_handle =3D GNTDEV_INVALID_HANDLE;=0A =0A = /* Now perform the mapping to user space. */=0A+ user_vaddr = =3D get_user_vaddr(vma, i);=0A+=0A if (xen_feature(XENFEAT_aut= o_translated_physmap)) {=0A /* USING SHADOW PAGE = TABLES. */=0A /* In this case, we simply insert the page = into the VM=0A@@ -622,11 +615,11 @@ static int gntdev_mmap (struct file = *fli=0A /* In this case, we map the grant(s) straight into = user=0A * space.=0A */=0A+ mfn =3D = op.dev_bus_addr >> PAGE_SHIFT;=0A =0A /* Get the machine address = of the PTE for the user page. */=0A if ((ret =3D create_lookup_= pte_addr(vma->vm_mm,=0A- = vma->vm_start=0A- + (i << = PAGE_SHIFT),=0A+ = user_vaddr,=0A &ptep)))=0A = {=0A printk(KERN_ERR "Error obtaining PTE pointer = (%d)\n",=0A@@ -636,9 +629,6 @@ static int gntdev_mmap (struct file *fli=0A = =0A /* Configure the map operation. */=0A =0A- /* = The reference is to be used by host CPUs. */=0A- flags =3D = GNTMAP_host_map;=0A-=0A /* Specifies a user space mapping. = */=0A flags |=3D GNTMAP_application_map;=0A =0A@@ -647,14 +637,9 = @@ static int gntdev_mmap (struct file *fli=0A */=0A = flags |=3D GNTMAP_contains_pte;=0A =0A- if (!(vma->vm_flags & = VM_WRITE))=0A- flags |=3D GNTMAP_readonly;=0A-=0A = gnttab_set_map_op(&op, ptep, flags,=0A- = private_data->grants[slot_index+i]=0A- = .u.valid.ref,=0A- private_data->grants[slot= _index+i]=0A- .u.valid.domid);=0A+ = grants[i].u.valid.ref,=0A+ = grants[i].u.valid.domid);=0A =0A /* Carry out the mapping = of the grant reference. */=0A ret =3D HYPERVISOR_grant_table_op(G= NTTABOP_map_grant_ref,=0A@@ -664,10 +649,8 @@ static int gntdev_mmap = (struct file *fli=0A printk(KERN_ERR "Error mapping the = grant reference "=0A "into user space (%d). = domid =3D %d; ref =3D %d\n",=0A op.status,= =0A- private_data->grants[slot_index+i].u=0A- = .valid.domid,=0A- private_data= ->grants[slot_index+i].u=0A- .valid.ref);=0A+ = grants[i].u.valid.domid,=0A+ = grants[i].u.valid.ref);=0A /* This should never = happen after we've mapped into=0A * the kernel = space. */=0A BUG_ON(op.status =3D=3D GNTST_eagain);=0A@@= -677,15 +660,11 @@ static int gntdev_mmap (struct file *fli=0A = /* Record the grant handle, for use in the unmap=0A * = operation.=0A */=0A- private_data->grants[slot_index+i].= u.valid.user_handle=0A- =3D op.handle;=0A+ = grants[i].u.valid.user_handle =3D op.handle;=0A =0A /* Update = p2m structure with the new mapping. */=0A set_phys_to_machine= (__pa(kernel_vaddr) >> PAGE_SHIFT,=0A- = FOREIGN_FRAME(private_data->=0A- = grants[slot_index+i]=0A- = .u.valid.dev_bus_addr=0A- = >> PAGE_SHIFT));=0A+ FOREIGN_FRAME(mfn));=0A= }=0A exit_ret =3D 0;=0A =0A@@ -715,9 +694,11 @@ undo_map_out:=0A= static pte_t gntdev_clear_pte(struct vm_area_struct *vma, unsigned long = addr,=0A pte_t *ptep, int is_fullmm)=0A {=0A- = int slot_index, ret;=0A+ int ret;=0A+ unsigned int nr;=0A+ = unsigned long slot_index;=0A pte_t copy;=0A- struct gnttab_unmap_grant_r= ef op;=0A+ struct gnttab_unmap_grant_ref op[2];=0A gntdev_file= _private_data_t *private_data;=0A =0A /* THIS IS VERY UNPLEASANT: = do_mmap_pgoff() will set the vma->vm_file=0A@@ -750,36 +731,35 @@ static = pte_t gntdev_clear_pte(struct vm_=0A return copy;=0A = }=0A =0A- /* First, we clear the user space mapping, if it has been = made.=0A- */=0A+ /* Clear the user space mapping, if it has been = made. */=0A if (private_data->grants[slot_index].u.valid.user_handle = !=3D=0A- GNTDEV_INVALID_HANDLE &&=0A- !xen_feature(XE= NFEAT_auto_translated_physmap)) {=0A- /* NOT USING SHADOW PAGE = TABLES. */=0A- gnttab_set_unmap_op(&op, ptep_to_machine(ptep),=0A+= GNTDEV_INVALID_HANDLE) {=0A+ /* NOT USING = SHADOW PAGE TABLES (and user handle valid). */=0A+ gnttab_set_= unmap_op(&op[0], ptep_to_machine(ptep),=0A = GNTMAP_contains_pte,=0A private_data->g= rants[slot_index].u.valid=0A .user_handle);= =0A- ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,= =0A- &op, 1);=0A- = BUG_ON(ret);=0A- if (op.status !=3D GNTST_okay)=0A- = printk("User unmap grant status =3D %d\n", op.status);=0A+ = nr =3D 1;=0A } else {=0A- /* USING SHADOW PAGE TABLES. = */=0A+ /* USING SHADOW PAGE TABLES (or user handle invalid). = */=0A pte_clear_full(vma->vm_mm, addr, ptep, is_fullmm);=0A+ = nr =3D 0;=0A }=0A =0A- /* Finally, we unmap the grant from kernel = space. */=0A- gnttab_set_unmap_op(&op,=0A+ /* We always unmap the = grant from kernel space. */=0A+ gnttab_set_unmap_op(&op[nr],=0A = get_kernel_vaddr(private_data, slot_index),=0A = GNTMAP_host_map,=0A private_data->grants[sl= ot_index].u.valid=0A .kernel_handle);=0A- = ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1);=0A+=0A= + ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, op, nr = + 1);=0A BUG_ON(ret);=0A- if (op.status !=3D GNTST_okay)=0A- = printk("Kernel unmap grant status =3D %d\n", op.status);=0A+=0A+ = if (nr && op[0].status !=3D GNTST_okay)=0A+ printk("User unmap = grant status =3D %d\n", op[0].status);=0A+ if (op[nr].status !=3D = GNTST_okay)=0A+ printk("Kernel unmap grant status =3D %d\n", = op[nr].status);=0A =0A /* Return slot to the not-yet-mapped state, so = that it may be=0A * mapped again, or removed by a subsequent = ioctl.=0A@@ -915,7 +895,8 @@ private_data_initialised:=0A = return -EFAULT;=0A =0A start_index =3D op.index >> PAGE_SHIFT;=0A-= if (start_index + op.count > private_data->grants_size)=0A+= if (start_index + op.count < start_index ||=0A+ = start_index + op.count > private_data->grants_size)=0A = return -EINVAL;=0A =0A down_write(&private_data->grants_sem);=0A@@= -924,28 +905,29 @@ private_data_initialised:=0A * = state.=0A */=0A for (i =3D 0; i < op.count; ++i) = {=0A- if (unlikely=0A- = (private_data->grants[start_index + i].state=0A- = !=3D GNTDEV_SLOT_NOT_YET_MAPPED)) {=0A- if = (private_data->grants[start_index + i].state=0A- = =3D=3D GNTDEV_SLOT_INVALID) {=0A- = printk(KERN_ERR=0A- "Tried to = remove an invalid "=0A- "grant at = offset 0x%x.",=0A- (start_index= + i) =0A- << PAGE_SHIFT);=0A- = rc =3D -EINVAL;=0A- = } else {=0A- printk(KERN_ERR=0A- = "Tried to remove a grant which "=0A- = "is currently mmap()-ed at "=0A- = "offset 0x%x.",=0A- = (start_index + i) =0A- << = PAGE_SHIFT);=0A- rc =3D -EBUSY;=0A- = }=0A- goto unmap_out;=0A+= const char *what;=0A+=0A+ = switch (private_data->grants[start_index + i].state) {=0A+ = case GNTDEV_SLOT_NOT_YET_MAPPED:=0A+ = continue;=0A+ case GNTDEV_SLOT_INVALID:=0A+ = what =3D "invalid";=0A+ rc =3D -EINVAL;=0A+= break;=0A+ case = GNTDEV_SLOT_MAPPED:=0A+ what =3D "currently = mmap()-ed";=0A+ rc =3D -EBUSY;=0A+ = break;=0A+ default:=0A+ = what =3D "in an invalid state";=0A+ rc =3D = -ENXIO;=0A+ break;=0A = }=0A+ printk(KERN_ERR "%s[%d] tried to remove a = grant"=0A+ " which is %s at %#x+%#x\n",=0A+ = current->comm, current->pid,=0A+ = what, start_index, i);=0A+ goto unmap_out;=0A = }=0A =0A down_write(&private_data->free_list_sem);=0A --=__Part2A04C37A.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part2A04C37A.0__=--