From: "Jan Beulich" Subject: [PATCH 3/4] linux-2.6.18/gntdev: adjust indentation (and fix bugs noticed by doing so) Date: Thu, 10 May 2012 16:18:31 +0100 Message-ID: <4FABF8670200007800082C8F@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part0729EE57.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. --=__Part0729EE57.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline By inverting two conditions, indentation can be decreased by one level for large code portions, thus improving legibility. In gntdev_mmap(), this made obvious that failure of vm_insert_page() was silently ignored rather than being propagated to the caller. In gntdev_clear_pte(), the final set_phys_to_machine() must not be called for the auto-translated case. Signed-off-by: Jan Beulich --- a/drivers/xen/gntdev/gntdev.c +++ b/drivers/xen/gntdev/gntdev.c @@ -607,85 +607,85 @@ static int gntdev_mmap (struct file *fli GNTDEV_INVALID_HANDLE; =20 /* Now perform the mapping to user space. */ - if (!xen_feature(XENFEAT_auto_translated_physmap)) { - - /* NOT USING SHADOW PAGE TABLES. */ - /* In this case, we map the grant(s) straight into = user - * space. - */ - - /* Get the machine address of the PTE for the = user=20 - * page. - */ - if ((ret =3D create_lookup_pte_addr(vma->vm_mm,=20 - vma->vm_start=20 - + (i << = PAGE_SHIFT),=20 - &ptep))) - { - printk(KERN_ERR "Error obtaining PTE = pointer " - "(%d).\n", ret); - goto undo_map_out; - } - =09 - /* Configure the map operation. */ - =09 - /* The reference is to be used by host CPUs. */ - flags =3D GNTMAP_host_map; - =09 - /* Specifies a user space mapping. */ - flags |=3D GNTMAP_application_map; - =09 - /* The map request contains the machine address of = the - * PTE to update. - */ - flags |=3D GNTMAP_contains_pte; - =09 - if (!(vma->vm_flags & VM_WRITE)) - flags |=3D GNTMAP_readonly; - - gnttab_set_map_op(&op, ptep, flags,=20 - private_data->grants[slot_index+i= ] - .u.valid.ref,=20 - private_data->grants[slot_index+i= ] - .u.valid.domid); - - /* Carry out the mapping of the grant reference. = */ - ret =3D HYPERVISOR_grant_table_op(GNTTABOP_map_gran= t_ref, - &op, 1); - BUG_ON(ret); - if (op.status !=3D GNTST_okay) { - 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); - /* This should never happen after we've = mapped into - * the kernel space. */ - BUG_ON(op.status =3D=3D GNTST_eagain); - goto undo_map_out; - } - =09 - /* Record the grant handle, for use in the = unmap=20 - * operation.=20 - */ - private_data->grants[slot_index+i].u. - valid.user_handle =3D op.handle; - - /* 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)); - } else { + if (xen_feature(XENFEAT_auto_translated_physmap)) { /* USING SHADOW PAGE TABLES. */ /* In this case, we simply insert the page into = the VM * area. */ ret =3D vm_insert_page(vma, user_vaddr, page); + if (!ret) + continue; + exit_ret =3D ret; + goto undo_map_out; + } + + /* NOT USING SHADOW PAGE TABLES. */ + /* In this case, we map the grant(s) straight into user + * space. + */ + + /* 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), + &ptep))) + { + printk(KERN_ERR "Error obtaining PTE pointer = (%d)\n", + ret); + goto undo_map_out; + } + + /* Configure the map operation. */ + + /* The reference is to be used by host CPUs. */ + flags =3D GNTMAP_host_map; + + /* Specifies a user space mapping. */ + flags |=3D GNTMAP_application_map; + + /* The map request contains the machine address of the + * PTE to update. + */ + flags |=3D GNTMAP_contains_pte; + + 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); + + /* Carry out the mapping of the grant reference. */ + ret =3D HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, + &op, 1); + BUG_ON(ret); + if (op.status !=3D GNTST_okay) { + 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); + /* This should never happen after we've mapped = into + * the kernel space. */ + BUG_ON(op.status =3D=3D GNTST_eagain); + goto undo_map_out; } =20 + /* Record the grant handle, for use in the unmap + * operation. + */ + private_data->grants[slot_index+i].u.valid.user_handle + =3D op.handle; + + /* 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)); } exit_ret =3D 0; =20 @@ -745,55 +745,52 @@ static pte_t gntdev_clear_pte(struct vm_ /* Only unmap grants if the slot has been mapped. This could be = being * called from a failing mmap(). */ - if (private_data->grants[slot_index].state =3D=3D GNTDEV_SLOT_MAPPE= D) { - - /* First, we clear the user space mapping, if it has been = made. - */ - if (private_data->grants[slot_index].u.valid.user_handle = !=3D - GNTDEV_INVALID_HANDLE &&=20 - !xen_feature(XENFEAT_auto_translated_physmap)) { - /* NOT USING SHADOW PAGE TABLES. */ - gnttab_set_unmap_op(&op, ptep_to_machine(ptep),=20 - GNTMAP_contains_pte, - private_data->grants[slot_index= ] - .u.valid.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",=20 - op.status); - } else { - /* USING SHADOW PAGE TABLES. */ - pte_clear_full(vma->vm_mm, addr, ptep, is_fullmm); - } + if (private_data->grants[slot_index].state !=3D GNTDEV_SLOT_MAPPED)= { + pte_clear_full(vma->vm_mm, addr, ptep, is_fullmm); + return copy; + } =20 - /* Finally, we unmap the grant from kernel space. */ - gnttab_set_unmap_op(&op,=20 - get_kernel_vaddr(private_data, = slot_index), - GNTMAP_host_map,=20 + /* First, we 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), + GNTMAP_contains_pte, private_data->grants[slot_index].u.vali= d - .kernel_handle); - ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,= =20 + .user_handle); + ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref,= &op, 1); BUG_ON(ret); if (op.status !=3D GNTST_okay) - printk("Kernel unmap grant status =3D %d\n", = op.status); + printk("User unmap grant status =3D %d\n", = op.status); + } else { + /* USING SHADOW PAGE TABLES. */ + pte_clear_full(vma->vm_mm, addr, ptep, is_fullmm); + } =20 + /* Finally, we unmap the grant from kernel space. */ + gnttab_set_unmap_op(&op, + 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); + BUG_ON(ret); + if (op.status !=3D GNTST_okay) + printk("Kernel unmap grant status =3D %d\n", op.status); =20 - /* Return slot to the not-yet-mapped state, so that it may = be - * mapped again, or removed by a subsequent ioctl. - */ - private_data->grants[slot_index].state =3D=20 - GNTDEV_SLOT_NOT_YET_MAPPED; + /* Return slot to the not-yet-mapped state, so that it may be + * mapped again, or removed by a subsequent ioctl. + */ + private_data->grants[slot_index].state =3D GNTDEV_SLOT_NOT_YET_MAPP= ED; =20 + if (!xen_feature(XENFEAT_auto_translated_physmap)) { /* Invalidate the physical to machine mapping for this = page. */ set_phys_to_machine( page_to_pfn(private_data->foreign_pages[slot_index]= ), INVALID_P2M_ENTRY); - - } else { - pte_clear_full(vma->vm_mm, addr, ptep, is_fullmm); } =20 return copy; --=__Part0729EE57.0__= Content-Type: text/plain; name="xen-gntdev-indent.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-gntdev-indent.patch" gntdev: adjust indentation (and fix bugs noticed by doing so)=0A=0ABy = inverting two conditions, indentation can be decreased by one level=0Afor = large code portions, thus improving legibility.=0A=0AIn gntdev_mmap(), = this made obvious that failure of vm_insert_page()=0Awas silently ignored = rather than being propagated to the caller.=0A=0AIn gntdev_clear_pte(), = the final set_phys_to_machine() must not be=0Acalled for the auto-translate= d case.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/drivers/xen/gntdev/gntdev.c=0A+++ b/drivers/xen/gntdev/gntdev.c=0A@@ = -607,85 +607,85 @@ static int gntdev_mmap (struct file *fli=0A = GNTDEV_INVALID_HANDLE;=0A =0A /* Now perform the mapping to user = space. */=0A- if (!xen_feature(XENFEAT_auto_translated_physmap)) = {=0A-=0A- /* NOT USING SHADOW PAGE TABLES. */=0A- = /* In this case, we map the grant(s) straight into user=0A- = * space.=0A- */=0A-=0A- /* = Get the machine address of the PTE for the user =0A- * = page.=0A- */=0A- if ((ret =3D = create_lookup_pte_addr(vma->vm_mm, =0A- = vma->vm_start =0A- = + (i << PAGE_SHIFT), =0A- = &ptep)))=0A- {=0A- printk(KERN= _ERR "Error obtaining PTE pointer "=0A- = "(%d).\n", ret);=0A- goto undo_map_out;=0A- = }=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- /* = The map request contains the machine address of the=0A- * = PTE to update.=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->gra= nts[slot_index+i]=0A- .u.valid.ref, = =0A- private_data->grants[slot_index+i= ]=0A- .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- = &op, 1);=0A- BUG_ON(ret)= ;=0A- if (op.status !=3D GNTST_okay) {=0A- = printk(KERN_ERR "Error mapping the grant "=0A- = "reference into user space (%d). domid "=0A- = "=3D %d; ref =3D %d\n", 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- = /* This should never happen after we've mapped into=0A- = * the kernel space. */=0A- = BUG_ON(op.status =3D=3D GNTST_eagain);=0A- = goto undo_map_out;=0A- }=0A- =0A- = /* Record the grant handle, for use in the unmap =0A- = * operation. =0A- */=0A- private_dat= a->grants[slot_index+i].u.=0A- valid.user_handle = =3D op.handle;=0A-=0A- /* Update p2m structure with the = new mapping. */=0A- set_phys_to_machine(__pa(kernel_vad= dr) >> PAGE_SHIFT,=0A- FOREIGN_FRAME(p= rivate_data->=0A- = grants[slot_index+i]=0A- = .u.valid.dev_bus_addr=0A- = >> PAGE_SHIFT));=0A- } else {=0A+ if (xen_feature(XEN= FEAT_auto_translated_physmap)) {=0A /* USING SHADOW = PAGE TABLES. */=0A /* In this case, we simply insert = the page into the VM=0A * area. */=0A = ret =3D vm_insert_page(vma, user_vaddr, page);=0A+ if = (!ret)=0A+ continue;=0A+ = exit_ret =3D ret;=0A+ goto undo_map_out;=0A+ = }=0A+=0A+ /* NOT USING SHADOW PAGE TABLES. */=0A+ /* = In this case, we map the grant(s) straight into user=0A+ * = space.=0A+ */=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+ = &ptep)))=0A+ {=0A+ printk(KERN_ERR "Error = obtaining PTE pointer (%d)\n",=0A+ ret);=0A+ = goto undo_map_out;=0A+ }=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+ /* The map request contains the = machine address of the=0A+ * PTE to update.=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+=0A+ /* Carry out the mapping of the = grant reference. */=0A+ ret =3D HYPERVISOR_grant_table_op(GNTTABOP_= map_grant_ref,=0A+ &op, = 1);=0A+ BUG_ON(ret);=0A+ if (op.status !=3D = GNTST_okay) {=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+ = /* This should never happen after we've mapped into=0A+ = * the kernel space. */=0A+ BUG_ON(op.status =3D=3D = GNTST_eagain);=0A+ goto undo_map_out;=0A = }=0A =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+=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 }=0A exit_ret = =3D 0;=0A =0A@@ -745,55 +745,52 @@ static pte_t gntdev_clear_pte(struct = vm_=0A /* Only unmap grants if the slot has been mapped. This could be = being=0A * called from a failing mmap().=0A */=0A- if = (private_data->grants[slot_index].state =3D=3D GNTDEV_SLOT_MAPPED) = {=0A-=0A- /* First, we clear the user space mapping, if it = has been made.=0A- */=0A- if (private_data->grants[sl= ot_index].u.valid.user_handle !=3D=0A- GNTDEV_INVALID_HANDLE = && =0A- !xen_feature(XENFEAT_auto_translated_physmap)) {=0A- = /* NOT USING SHADOW PAGE TABLES. */=0A- = gnttab_set_unmap_op(&op, ptep_to_machine(ptep), =0A- = GNTMAP_contains_pte,=0A- = private_data->grants[slot_index]=0A- = .u.valid.user_handle);=0A- ret =3D HYPERVISOR_grant_ta= ble_op(=0A- GNTTABOP_unmap_grant_ref, &op, = 1);=0A- BUG_ON(ret);=0A- if = (op.status !=3D GNTST_okay)=0A- printk("User unmap = grant status =3D %d\n", =0A- op.status);= =0A- } else {=0A- /* USING SHADOW PAGE = TABLES. */=0A- pte_clear_full(vma->vm_mm, addr, ptep, = is_fullmm);=0A- }=0A+ if (private_data->grants[slot_index].state = !=3D GNTDEV_SLOT_MAPPED) {=0A+ pte_clear_full(vma->vm_mm, addr, = ptep, is_fullmm);=0A+ return copy;=0A+ }=0A =0A- = /* Finally, we unmap the grant from kernel space. */=0A- = gnttab_set_unmap_op(&op, =0A- get_kernel_vadd= r(private_data, slot_index),=0A- = GNTMAP_host_map, =0A+ /* First, we clear the user space mapping, if it = has been made.=0A+ */=0A+ if (private_data->grants[slot_index].u.vali= d.user_handle !=3D=0A+ GNTDEV_INVALID_HANDLE &&=0A+ = !xen_feature(XENFEAT_auto_translated_physmap)) {=0A+ /* NOT = USING SHADOW PAGE TABLES. */=0A+ gnttab_set_unmap_op(&op, = ptep_to_machine(ptep),=0A+ GNTMAP_contains= _pte,=0A private_data->grants[slot_index= ].u.valid=0A- .kernel_handle);=0A- = ret =3D HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, =0A+ = .user_handle);=0A+ ret =3D HYPERVISOR_grant_ta= ble_op(GNTTABOP_unmap_grant_ref,=0A = &op, 1);=0A BUG_ON(ret);=0A if (op.status !=3D = GNTST_okay)=0A- printk("Kernel unmap grant status =3D = %d\n", op.status);=0A+ printk("User unmap grant status = =3D %d\n", op.status);=0A+ } else {=0A+ /* USING SHADOW = PAGE TABLES. */=0A+ pte_clear_full(vma->vm_mm, addr, ptep, = is_fullmm);=0A+ }=0A =0A+ /* Finally, we unmap the grant from kernel = space. */=0A+ gnttab_set_unmap_op(&op,=0A+ = get_kernel_vaddr(private_data, slot_index),=0A+ = GNTMAP_host_map,=0A+ private_data->grants[slot_index= ].u.valid=0A+ .kernel_handle);=0A+ ret =3D = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 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- = /* Return slot to the not-yet-mapped state, so that it may be=0A- = * mapped again, or removed by a subsequent ioctl.=0A- */=0A- = private_data->grants[slot_index].state =3D =0A- GNTDEV_SLOT= _NOT_YET_MAPPED;=0A+ /* Return slot to the not-yet-mapped state, so = that it may be=0A+ * mapped again, or removed by a subsequent = ioctl.=0A+ */=0A+ private_data->grants[slot_index].state =3D = GNTDEV_SLOT_NOT_YET_MAPPED;=0A =0A+ if (!xen_feature(XENFEAT_auto_trans= lated_physmap)) {=0A /* Invalidate the physical to machine = mapping for this page. */=0A set_phys_to_machine(=0A = page_to_pfn(private_data->foreign_pages[slot_index]),=0A = INVALID_P2M_ENTRY);=0A-=0A- } else {=0A- pte_clear_f= ull(vma->vm_mm, addr, ptep, is_fullmm);=0A }=0A =0A return = copy;=0A --=__Part0729EE57.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 --=__Part0729EE57.0__=--