From: "Jan Beulich" Subject: [PATCH 1/4] linux-2.6.18: fix VM_FOREIGN users after c/s 878:eba6fe6d8d53 Date: Thu, 10 May 2012 16:17:06 +0100 Message-ID: <4FABF8120200007800082C87@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartB59B5CE2.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. --=__PartB59B5CE2.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The level of indirection got increased by the blktap2 changes, yet existing users were not properly updated. While c/s 901:9242c5b965c1 did so for a specific case in blktap, this was incomplete and left out gntdev altogether. Signed-off-by: Jan Beulich --- a/drivers/xen/blktap/blktap.c +++ b/drivers/xen/blktap/blktap.c @@ -120,7 +120,7 @@ typedef struct tap_blkif { [req id, idx] tuple = */ blkif_t *blkif; /*Associate blkif with tapdev = */ struct domid_translate_ext trans; /*Translation from domid to bus. = */ - struct vm_foreign_map foreign_map; /*Mapping page */ + struct vm_foreign_map *foreign_maps; /*Mapping pages = */ } tap_blkif_t; =20 static struct tap_blkif *tapfds[MAX_TAP_DEV]; @@ -329,7 +329,7 @@ static pte_t blktap_clear_pte(struct vm_ =20 pg =3D idx_to_page(mmap_idx, pending_idx, seg); ClearPageReserved(pg); - info->foreign_map.map[offset + RING_PAGES] =3D NULL; + info->foreign_maps->map[offset + RING_PAGES] =3D NULL; =20 khandle =3D &pending_handle(mmap_idx, pending_idx, seg); =20 @@ -377,12 +377,17 @@ static pte_t blktap_clear_pte(struct vm_ static void blktap_vma_open(struct vm_area_struct *vma) { tap_blkif_t *info; + unsigned long idx; + struct vm_foreign_map *foreign_map; + if (vma->vm_file =3D=3D NULL) return; =20 info =3D vma->vm_file->private_data; - vma->vm_private_data =3D - &info->foreign_map.map[(vma->vm_start - info->rings_vstart)= >> PAGE_SHIFT]; + idx =3D (vma->vm_start - info->rings_vstart) >> PAGE_SHIFT; + foreign_map =3D &info->foreign_maps[idx]; + foreign_map->map =3D &info->foreign_maps->map[idx]; + vma->vm_private_data =3D foreign_map; } =20 /* tricky part @@ -392,7 +397,6 @@ static void blktap_vma_open(struct vm_ar */ static void blktap_vma_close(struct vm_area_struct *vma) { - tap_blkif_t *info; struct vm_area_struct *next =3D vma->vm_next; =20 if (next =3D=3D NULL || @@ -402,9 +406,7 @@ static void blktap_vma_close(struct vm_a vma->vm_file !=3D next->vm_file) return; =20 - info =3D vma->vm_file->private_data; - next->vm_private_data =3D - &info->foreign_map.map[(next->vm_start - info->rings_vstart= ) >> PAGE_SHIFT]; + blktap_vma_open(next); } =20 static struct vm_operations_struct blktap_vm_ops =3D { @@ -640,8 +642,9 @@ static int blktap_release(struct inode * mm =3D xchg(&info->mm, NULL); if (mm) mmput(mm); - kfree(info->foreign_map.map); - info->foreign_map.map =3D NULL; + kfree(info->foreign_maps->map); + kfree(info->foreign_maps); + info->foreign_maps =3D NULL; =20 /* Free the ring page. */ ClearPageReserved(virt_to_page(info->ufe_ring.sring)); @@ -730,14 +733,19 @@ static int blktap_mmap(struct file *filp } =20 /* Mark this VM as containing foreign pages, and set up mappings. = */ - info->foreign_map.map =3D kzalloc(((vma->vm_end - vma->vm_start) = >> PAGE_SHIFT) * - sizeof(*info->foreign_map.map), GFP_KERNEL); - if (info->foreign_map.map =3D=3D NULL) { + info->foreign_maps =3D kcalloc(size, sizeof(*info->foreign_maps), + GFP_KERNEL); + if (info->foreign_maps) + info->foreign_maps->map =3D + kcalloc(size, sizeof(*info->foreign_maps->map), + GFP_KERNEL); + if (!info->foreign_maps || !info->foreign_maps->map) { + kfree(info->foreign_maps); WPRINTK("Couldn't alloc VM_FOREIGN map.\n"); goto fail; } =20 - vma->vm_private_data =3D &info->foreign_map; + vma->vm_private_data =3D info->foreign_maps; vma->vm_flags |=3D VM_FOREIGN; vma->vm_flags |=3D VM_DONTCOPY; =20 @@ -1242,7 +1250,7 @@ static int blktap_read_ufe_ring(tap_blki pg =3D idx_to_page(mmap_idx, pending_idx, j); ClearPageReserved(pg); offset =3D (uvaddr - info->rings_vstart) >> = PAGE_SHIFT; - info->foreign_map.map[offset] =3D NULL; + info->foreign_maps->map[offset] =3D NULL; } fast_flush_area(pending_req, pending_idx, usr_idx, info); make_response(blkif, pending_req->id, res.operation, @@ -1530,7 +1538,7 @@ static void dispatch_rw_block_io(blkif_t FOREIGN_FRAME(map[i].dev_bus_ad= dr >> PAGE_SHIFT)); offset =3D (uvaddr - info->rings_vstart) >> = PAGE_SHIFT; - info->foreign_map.map[offset] =3D pg; + info->foreign_maps->map[offset] =3D pg; } } else { for (i =3D 0; i < nseg; i++) { @@ -1556,7 +1564,7 @@ static void dispatch_rw_block_io(blkif_t =20 offset =3D (uvaddr - info->rings_vstart) >> = PAGE_SHIFT; pg =3D idx_to_page(mmap_idx, pending_idx, i); - info->foreign_map.map[offset] =3D pg; + info->foreign_maps->map[offset] =3D pg; } } =20 --- a/drivers/xen/gntdev/gntdev.c +++ b/drivers/xen/gntdev/gntdev.c @@ -459,6 +459,7 @@ static int gntdev_mmap (struct file *fli int i; struct page *page; gntdev_file_private_data_t *private_data =3D flip->private_data; + struct vm_foreign_map *foreign_map; =20 if (unlikely(!private_data)) { printk(KERN_ERR "File's private data is NULL.\n"); @@ -487,6 +488,14 @@ static int gntdev_mmap (struct file *fli return -EINVAL; } =20 + foreign_map =3D kmalloc(sizeof(*foreign_map), GFP_KERNEL); + if (!foreign_map) { + printk(KERN_ERR "Couldn't allocate mapping structure for = VM " + "area.\n"); + return -ENOMEM; + } + foreign_map->map =3D &private_data->foreign_pages[slot_index]; + /* Slots must be in the NOT_YET_MAPPED state. */ down_write(&private_data->grants_sem); for (i =3D 0; i < size; ++i) { @@ -496,6 +505,7 @@ static int gntdev_mmap (struct file *fli "state (%d).\n", slot_index + i,=20 private_data->grants[slot_index + i].state);= up_write(&private_data->grants_sem); + kfree(foreign_map); return -EINVAL; } } @@ -504,14 +514,8 @@ static int gntdev_mmap (struct file *fli vma->vm_ops =3D &gntdev_vmops; =20 /* The VM area contains pages from another VM. */ + vma->vm_private_data =3D foreign_map; vma->vm_flags |=3D VM_FOREIGN; - vma->vm_private_data =3D kzalloc(size * sizeof(struct page *), - GFP_KERNEL); - if (vma->vm_private_data =3D=3D NULL) { - printk(KERN_ERR "Couldn't allocate mapping structure for = VM " - "area.\n"); - return -ENOMEM; - } =20 /* This flag prevents Bad PTE errors when the memory is unmapped. = */ vma->vm_flags |=3D VM_RESERVED; @@ -567,11 +571,6 @@ static int gntdev_mmap (struct file *fli goto undo_map_out; } =20 - /* Store a reference to the page that will be mapped into = user - * space. - */ - ((struct page **) vma->vm_private_data)[i] =3D page; - /* Mark mapped page as reserved. */ SetPageReserved(page); =20 @@ -676,7 +675,8 @@ undo_map_out: * by do_mmap_pgoff(), which will eventually call gntdev_clear_pte(= ). * All we need to do here is free the vma_private_data. */ - kfree(vma->vm_private_data); + vma->vm_flags &=3D ~VM_FOREIGN; + kfree(foreign_map); =20 /* THIS IS VERY UNPLEASANT: do_mmap_pgoff() will set the vma->vm_fi= le * to NULL on failure. However, we need this in gntdev_clear_pte() = to @@ -780,9 +780,8 @@ static pte_t gntdev_clear_pte(struct vm_ /* "Destructor" for a VM area. */ static void gntdev_vma_close(struct vm_area_struct *vma) { - if (vma->vm_private_data) { + if (vma->vm_flags & VM_FOREIGN) kfree(vma->vm_private_data); - } } =20 /* Called when an ioctl is made on the device. --=__PartB59B5CE2.0__= Content-Type: text/plain; name="xen-foreign-map.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-foreign-map.patch" fix VM_FOREIGN users after c/s 878:eba6fe6d8d53=0A=0AThe level of = indirection got increased by the blktap2 changes, yet=0Aexisting users = were not properly updated. While c/s 901:9242c5b965c1=0Adid so for a = specific case in blktap, this was incomplete and left out=0Agntdev = altogether.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/drivers/xen/blktap/blktap.c=0A+++ b/drivers/xen/blktap/blktap.c=0A@@ = -120,7 +120,7 @@ typedef struct tap_blkif {=0A = [req id, idx] tuple */=0A blkif_t *blkif; = /*Associate blkif with tapdev */=0A struct domid_transl= ate_ext trans; /*Translation from domid to bus. */=0A- struct = vm_foreign_map foreign_map; /*Mapping page */=0A+ struct vm_foreign_m= ap *foreign_maps; /*Mapping pages */=0A } tap_blkif_t;=0A = =0A static struct tap_blkif *tapfds[MAX_TAP_DEV];=0A@@ -329,7 +329,7 @@ = static pte_t blktap_clear_pte(struct vm_=0A =0A pg =3D idx_to_page(= mmap_idx, pending_idx, seg);=0A ClearPageReserved(pg);=0A- = info->foreign_map.map[offset + RING_PAGES] =3D NULL;=0A+ info->forei= gn_maps->map[offset + RING_PAGES] =3D NULL;=0A =0A khandle =3D = &pending_handle(mmap_idx, pending_idx, seg);=0A =0A@@ -377,12 +377,17 @@ = static pte_t blktap_clear_pte(struct vm_=0A static void blktap_vma_open(str= uct vm_area_struct *vma)=0A {=0A tap_blkif_t *info;=0A+ unsigned = long idx;=0A+ struct vm_foreign_map *foreign_map;=0A+=0A if = (vma->vm_file =3D=3D NULL)=0A return;=0A =0A info =3D vma->vm_fi= le->private_data;=0A- vma->vm_private_data =3D=0A- &info->fore= ign_map.map[(vma->vm_start - info->rings_vstart) >> PAGE_SHIFT];=0A+ = idx =3D (vma->vm_start - info->rings_vstart) >> PAGE_SHIFT;=0A+ foreign_map= =3D &info->foreign_maps[idx];=0A+ foreign_map->map =3D &info->foreign= _maps->map[idx];=0A+ vma->vm_private_data =3D foreign_map;=0A }=0A =0A = /* tricky part=0A@@ -392,7 +397,6 @@ static void blktap_vma_open(struct = vm_ar=0A */=0A static void blktap_vma_close(struct vm_area_struct = *vma)=0A {=0A- tap_blkif_t *info;=0A struct vm_area_struct *next =3D = vma->vm_next;=0A =0A if (next =3D=3D NULL ||=0A@@ -402,9 +406,7 @@ = static void blktap_vma_close(struct vm_a=0A vma->vm_file !=3D = next->vm_file)=0A return;=0A =0A- info =3D vma->vm_file->priv= ate_data;=0A- next->vm_private_data =3D=0A- &info->foreign_map.= map[(next->vm_start - info->rings_vstart) >> PAGE_SHIFT];=0A+ blktap_vma_= open(next);=0A }=0A =0A static struct vm_operations_struct blktap_vm_ops = =3D {=0A@@ -640,8 +642,9 @@ static int blktap_release(struct inode *=0A = mm =3D xchg(&info->mm, NULL);=0A if (mm)=0A mmput(mm);= =0A- kfree(info->foreign_map.map);=0A- info->foreign_map.map =3D = NULL;=0A+ kfree(info->foreign_maps->map);=0A+ kfree(info->foreign= _maps);=0A+ info->foreign_maps =3D NULL;=0A =0A /* Free the ring = page. */=0A ClearPageReserved(virt_to_page(info->ufe_ring.sring));=0A@@= -730,14 +733,19 @@ static int blktap_mmap(struct file *filp=0A = }=0A =0A /* Mark this VM as containing foreign pages, and set up = mappings. */=0A- info->foreign_map.map =3D kzalloc(((vma->vm_end - = vma->vm_start) >> PAGE_SHIFT) *=0A- sizeof(*info->f= oreign_map.map), GFP_KERNEL);=0A- if (info->foreign_map.map =3D=3D = NULL) {=0A+ info->foreign_maps =3D kcalloc(size, sizeof(*info->foreign_= maps),=0A+ GFP_KERNEL);=0A+ if = (info->foreign_maps)=0A+ info->foreign_maps->map =3D=0A+ = kcalloc(size, sizeof(*info->foreign_maps->map),=0A+ = GFP_KERNEL);=0A+ if (!info->foreign_maps || !info->foreign_m= aps->map) {=0A+ kfree(info->foreign_maps);=0A WPRINTK("Co= uldn't alloc VM_FOREIGN map.\n");=0A goto fail;=0A }=0A =0A- = vma->vm_private_data =3D &info->foreign_map;=0A+ vma->vm_private_dat= a =3D info->foreign_maps;=0A vma->vm_flags |=3D VM_FOREIGN;=0A = vma->vm_flags |=3D VM_DONTCOPY;=0A =0A@@ -1242,7 +1250,7 @@ static int = blktap_read_ufe_ring(tap_blki=0A pg =3D idx_to_page(= mmap_idx, pending_idx, j);=0A ClearPageReserved(pg);=0A = offset =3D (uvaddr - info->rings_vstart) >> PAGE_SHIFT;=0A-= info->foreign_map.map[offset] =3D NULL;=0A+ = info->foreign_maps->map[offset] =3D NULL;=0A }=0A = fast_flush_area(pending_req, pending_idx, usr_idx, info);=0A = make_response(blkif, pending_req->id, res.operation,=0A@@ -1530,7 +1538,7 = @@ static void dispatch_rw_block_io(blkif_t=0A = FOREIGN_FRAME(map[i].dev_bus_addr=0A = >> PAGE_SHIFT));=0A offset =3D (uvaddr = - info->rings_vstart) >> PAGE_SHIFT;=0A- info->forei= gn_map.map[offset] =3D pg;=0A+ info->foreign_maps->map[off= set] =3D pg;=0A }=0A } else {=0A for (i =3D = 0; i < nseg; i++) {=0A@@ -1556,7 +1564,7 @@ static void dispatch_rw_block_i= o(blkif_t=0A =0A offset =3D (uvaddr - info->rings_vs= tart) >> PAGE_SHIFT;=0A pg =3D idx_to_page(mmap_idx= , pending_idx, i);=0A- info->foreign_map.map[offset] =3D = pg;=0A+ info->foreign_maps->map[offset] =3D pg;=0A = }=0A }=0A =0A--- a/drivers/xen/gntdev/gntdev.c=0A+++ b/drivers/xen/gntde= v/gntdev.c=0A@@ -459,6 +459,7 @@ static int gntdev_mmap (struct file = *fli=0A int i;=0A struct page *page;=0A gntdev_file_private= _data_t *private_data =3D flip->private_data;=0A+ struct vm_foreign_m= ap *foreign_map;=0A =0A if (unlikely(!private_data)) {=0A = printk(KERN_ERR "File's private data is NULL.\n");=0A@@ -487,6 +488,14 @@ = static int gntdev_mmap (struct file *fli=0A return -EINVAL;=0A = }=0A =0A+ foreign_map =3D kmalloc(sizeof(*foreign_map), = GFP_KERNEL);=0A+ if (!foreign_map) {=0A+ printk(KERN_ERR = "Couldn't allocate mapping structure for VM "=0A+ = "area.\n");=0A+ return -ENOMEM;=0A+ }=0A+ foreign_map->map = =3D &private_data->foreign_pages[slot_index];=0A+=0A /* Slots must be = in the NOT_YET_MAPPED state. */=0A down_write(&private_data->grants_se= m);=0A for (i =3D 0; i < size; ++i) {=0A@@ -496,6 +505,7 @@ static int = gntdev_mmap (struct file *fli=0A "state = (%d).\n", slot_index + i, =0A private_data->grants= [slot_index + i].state);=0A up_write(&private_data->gra= nts_sem);=0A+ kfree(foreign_map);=0A = return -EINVAL;=0A }=0A }=0A@@ -504,14 +514,8 @@ static = int gntdev_mmap (struct file *fli=0A vma->vm_ops =3D &gntdev_vmops;=0A = =0A /* The VM area contains pages from another VM. */=0A+ vma->vm_pri= vate_data =3D foreign_map;=0A vma->vm_flags |=3D VM_FOREIGN;=0A- = vma->vm_private_data =3D kzalloc(size * sizeof(struct page *),=0A- = GFP_KERNEL);=0A- if (vma->vm_private_data =3D=3D = NULL) {=0A- printk(KERN_ERR "Couldn't allocate mapping = structure for VM "=0A- "area.\n");=0A- return = -ENOMEM;=0A- }=0A =0A /* This flag prevents Bad PTE errors when = the memory is unmapped. */=0A vma->vm_flags |=3D VM_RESERVED;=0A@@ = -567,11 +571,6 @@ static int gntdev_mmap (struct file *fli=0A = goto undo_map_out;=0A }=0A =0A- /* Store a = reference to the page that will be mapped into user=0A- * = space.=0A- */=0A- ((struct page **) vma->vm_private_d= ata)[i] =3D page;=0A-=0A /* Mark mapped page as reserved. = */=0A SetPageReserved(page);=0A =0A@@ -676,7 +675,8 @@ undo_map_o= ut:=0A * by do_mmap_pgoff(), which will eventually call gntdev_clear_pte(= ).=0A * All we need to do here is free the vma_private_data.=0A = */=0A- kfree(vma->vm_private_data);=0A+ vma->vm_flags &=3D = ~VM_FOREIGN;=0A+ kfree(foreign_map);=0A =0A /* THIS IS VERY = UNPLEASANT: do_mmap_pgoff() will set the vma->vm_file=0A * to NULL = on failure. However, we need this in gntdev_clear_pte() to=0A@@ -780,9 = +780,8 @@ static pte_t gntdev_clear_pte(struct vm_=0A /* "Destructor" for = a VM area.=0A */=0A static void gntdev_vma_close(struct vm_area_struct = *vma) {=0A- if (vma->vm_private_data) {=0A+ if (vma->vm_flags & = VM_FOREIGN)=0A kfree(vma->vm_private_data);=0A- }=0A }=0A = =0A /* Called when an ioctl is made on the device.=0A --=__PartB59B5CE2.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 --=__PartB59B5CE2.0__=--