Skip to contentSkip to conversation

Userspace grant communication

PATCH Superseded Daniel De Graaf 5 participants 38 messages Dec 17, 2010 – Jan 17, 2011 Xen hypervisor interface

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Userspace grant communication

Rebased on top of Stefano's 2.6.37 gntdev patches; included suggestions from the v2 patch queue. HVM mapping is now much cleaner, since pages with real GFNs are available. Multicall API didn't make it in, because the actual hypercall is within a function that I'm not sure should be changed to multicall in all instances, and I haven't had a chance to figure out how to use it despite this.

[PATCH 1/7] xen-gntdev: Fix circular locking dependency
[PATCH 2/7] xen-gntdev: Change page limit to be global instead of per-open [PATCH 3/7] xen-gntdev: Remove unneeded structures from grant_map tracking data [PATCH 4/7] xen-gntdev: Use find_vma rather than iterating our vma list manually [PATCH 5/7] xen-gntdev: Add reference counting to maps
[PATCH 6/7] xen-gntdev: Support mapping in HVM domains
[PATCH 7/7] xen-gntalloc: Userspace grant allocation driver

#1 lore raw

PATCH 1/7 xen-gntdev: Fix circular locking dependency

apply_to_page_range will acquire PTE lock while priv->lock is held, and mn_invl_range_start tries to acquire priv->lock with PTE already held. Fix by not holding priv->lock during the entire map operation. This is safe because map->vma is set nonzero while the lock is held, which will cause subsequent maps to fail and will cause the unmap ioctl (and other users of gntdev_del_map) to return -EBUSY until the area is unmapped. It is similarly impossible for gntdev_vma_close to be called while the vma is still being created.

---
 drivers/xen/gntdev.c |   19 +++++++++----------
 1 files changed, 9 insertions(+), 10 deletions(-)
drivers/xen/gntdev.c +9 -10
index 35de6bb..387c5f1 100644
@@ -608,23 +608,22 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	if (!(vma->vm_flags & VM_WRITE))
 		map->flags |= GNTMAP_readonly;
 
+	spin_unlock(&priv->lock);
+
 	err = apply_to_page_range(vma->vm_mm, vma->vm_start,
 				  vma->vm_end - vma->vm_start,
 				  find_grant_ptes, map);
-	if (err) {
-		goto unlock_out;
-		if (debug)
-			printk("%s: find_grant_ptes() failure.\n", __FUNCTION__);
-	}
+	if (err)
+		return err;
 
 	err = map_grant_pages(map);
-	if (err) {
-		goto unlock_out;
-		if (debug)
-			printk("%s: map_grant_pages() failure.\n", __FUNCTION__);
-	}
+	if (err)
+		return err;
+
 	map->is_mapped = 1;
 
+	return 0;
+
 unlock_out:
 	spin_unlock(&priv->lock);
 	return err;
Signature · 1 line
1.7.2.3
#2 lore raw

PATCH 2/7 xen-gntdev: Change page limit to be global instead of per-open

In reply to Daniel De Graaf

Because there is no limitation on how many times a user can open a given device file, an per-file-description limit on the number of pages granted offers little to no benefit. Change to a global limit and remove the ioctl() as the parameter can now be changed via sysfs.

---
 drivers/xen/gntdev.c |   48 ++++++++++++++----------------------------------
 1 files changed, 14 insertions(+), 34 deletions(-)
drivers/xen/gntdev.c +14 -34
index 387c5f1..7e21592 100644
@@ -45,13 +45,12 @@ MODULE_DESCRIPTION("User-space granted page access driver");
 
 static int debug = 0;
 module_param(debug, int, 0644);
-static int limit = 1024;
+static int limit = 1024*1024;
 module_param(limit, int, 0644);
+static atomic_t pages_mapped = ATOMIC_INIT(0);
 
 struct gntdev_priv {
 	struct list_head maps;
-	uint32_t used;
-	uint32_t limit;
 	spinlock_t lock;
 	struct mm_struct *mm;
 	struct mmu_notifier mn;
@@ -78,8 +77,8 @@ static void gntdev_print_maps(struct gntdev_priv *priv,
 {
 	struct grant_map *map;
 
-	printk("%s: maps list (priv %p, usage %d/%d)\n",
-	       __FUNCTION__, priv, priv->used, priv->limit);
+	printk("%s: maps list (priv %p)\n",
+	       __FUNCTION__, priv);
 	list_for_each_entry(map, &priv->maps, next)
 		printk("  index %2d, count %2d %s\n",
 		       map->index, map->count,
@@ -115,9 +114,6 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
 	add->count = count;
 	add->priv  = priv;
 
-	if (add->count + priv->used > priv->limit)
-		goto err;
-
 	return add;
 
 err:
@@ -148,7 +144,6 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 	list_add_tail(&add->next, &priv->maps);
 
 done:
-	priv->used += add->count;
 	if (debug)
 		gntdev_print_maps(priv, "[new]", add->index);
 }
@@ -195,7 +190,7 @@ static int gntdev_del_map(struct grant_map *map)
 		if (map->unmap_ops[i].handle)
 			return -EBUSY;
 
-	map->priv->used -= map->count;
+	atomic_sub(map->count, &pages_mapped);
 	list_del(&map->next);
 	return 0;
 }
@@ -386,7 +381,6 @@ static int gntdev_open(struct inode *inode, struct file *flip)
 
 	INIT_LIST_HEAD(&priv->maps);
 	spin_lock_init(&priv->lock);
-	priv->limit = limit;
 
 	priv->mm = get_task_mm(current);
 	if (!priv->mm) {
@@ -442,19 +436,26 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 		       op.count);
 	if (unlikely(op.count <= 0))
 		return -EINVAL;
-	if (unlikely(op.count > priv->limit))
-		return -EINVAL;
 
 	err = -ENOMEM;
 	map = gntdev_alloc_map(priv, op.count);
 	if (!map)
 		return err;
+
 	if (copy_from_user(map->grants, &u->refs,
 			   sizeof(map->grants[0]) * op.count) != 0) {
 		gntdev_free_map(map);
 		return err;
 	}
 
+	if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit))
+	{
+		if (debug)
+			printk("%s: can't map: over limit\n", __FUNCTION__);
+		gntdev_free_map(map);
+		return err;
+	}
+
 	spin_lock(&priv->lock);
 	gntdev_add_map(priv, map);
 	op.index = map->index << PAGE_SHIFT;
@@ -521,24 +522,6 @@ static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
 	return 0;
 }
 
-static long gntdev_ioctl_set_max_grants(struct gntdev_priv *priv,
-					struct ioctl_gntdev_set_max_grants __user *u)
-{
-	struct ioctl_gntdev_set_max_grants op;
-
-	if (copy_from_user(&op, u, sizeof(op)) != 0)
-		return -EFAULT;
-	if (debug)
-		printk("%s: priv %p, limit %d\n", __FUNCTION__, priv, op.count);
-	if (op.count > limit)
-		return -EINVAL;
-
-	spin_lock(&priv->lock);
-	priv->limit = op.count;
-	spin_unlock(&priv->lock);
-	return 0;
-}
-
 static long gntdev_ioctl(struct file *flip,
 			 unsigned int cmd, unsigned long arg)
 {
@@ -555,9 +538,6 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 
-	case IOCTL_GNTDEV_SET_MAX_GRANTS:
-		return gntdev_ioctl_set_max_grants(priv, ptr);
-
 	default:
 		if (debug)
 			printk("%s: priv %p, unknown cmd %x\n",
Signature · 1 line
1.7.2.3
#3 lore raw

Re: [PATCH 2/7] xen-gntdev: Change page limit to be global instead of per-open

drivers/xen/gntdev.c static long gntdev_ioctl(struct file *flip, · line 559

Quoted 9 lines

static long gntdev_ioctl(struct file *flip,

drivers/xen/gntdev.c +0 -2
 			 unsigned int cmd, unsigned long arg)
 {
@@ -555,9 +538,6 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 
-	case IOCTL_GNTDEV_SET_MAX_GRANTS:
-		return gntdev_ioctl_set_max_grants(priv, ptr);

Would it make sense to return -EPNOTSUPPORTED? Or does it not really matter as nobody has been using this ioctl call?

Quoted 12 lines
drivers/xen/gntdev.c +0 -1
-
 	default:
 		if (debug)
 			printk("%s: priv %p, unknown cmd %x\n",
Signature · 7 lines
1.7.2.3


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
#4 lore raw

Re: [PATCH 2/7] xen-gntdev: Change page limit to be global instead of per-open

Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 7 lines

static long gntdev_ioctl(struct file *flip,

drivers/xen/gntdev.c +0 -0
 			 unsigned int cmd, unsigned long arg)
 {
@@ -555,9 +538,6 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 
Quoted 5 lines

drivers/xen/gntdev.c static long gntdev_ioctl(struct file *flip, · line 559

Quoted 2 lines
drivers/xen/gntdev.c +0 -2
-	case IOCTL_GNTDEV_SET_MAX_GRANTS:
-		return gntdev_ioctl_set_max_grants(priv, ptr);

Would it make sense to return -EPNOTSUPPORTED? Or does it not really matter as nobody has been using this ioctl call?

Does this produce a clearer error message than the default -ENOIOCTLCMD? It's possible that some people use it, since it was exposed as an API.

Doesn't really matter to me; we could also have it return 0 and be a noop in case someone interprets its failure as inability to support a sufficient number of grants.

Quoted 13 lines
Quoted 12 lines
drivers/xen/gntdev.c +0 -1
-
 	default:
 		if (debug)
 			printk("%s: priv %p, unknown cmd %x\n",
Signature · 7 lines
1.7.2.3


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
#5 lore raw

Re: [PATCH 2/7] xen-gntdev: Change page limit to be global instead of per-open

Quoted 8 lines · Daniel De Graaf
Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 7 lines

static long gntdev_ioctl(struct file *flip,

drivers/xen/gntdev.c +0 -0
 			 unsigned int cmd, unsigned long arg)
 {
@@ -555,9 +538,6 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 
Quoted 8 lines
Quoted 5 lines

drivers/xen/gntdev.c static long gntdev_ioctl(struct file *flip, · line 559

Quoted 2 lines
drivers/xen/gntdev.c +0 -2
-	case IOCTL_GNTDEV_SET_MAX_GRANTS:
-		return gntdev_ioctl_set_max_grants(priv, ptr);

Would it make sense to return -EPNOTSUPPORTED? Or does it not really matter as nobody has been using this ioctl call?

Does this produce a clearer error message than the default -ENOIOCTLCMD? It's possible that some people use it, since it was exposed as an API.

Looking at the Xen tools the user of this is:
xc_gnttab_set_max_grants which would end up returning whatever the error is. I don't see any users of this in the Xen tools, thought there might be some in the XCP code. Lets stay with your ENOIOCTLCMD.

However, I was wondering if you are going to submit a patch to the Xen tool stack so that it can utlize the SysFS interface to set the limits for that API call?

#6 lore raw

Re: [PATCH 2/7] xen-gntdev: Change page limit to be global instead of per-open

Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 8 lines · Daniel De Graaf
Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 7 lines

static long gntdev_ioctl(struct file *flip,

drivers/xen/gntdev.c +0 -0
 			 unsigned int cmd, unsigned long arg)
 {
@@ -555,9 +538,6 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 
Quoted 8 lines
Quoted 5 lines

drivers/xen/gntdev.c static long gntdev_ioctl(struct file *flip, · line 559

Quoted 2 lines
drivers/xen/gntdev.c +0 -2
-	case IOCTL_GNTDEV_SET_MAX_GRANTS:
-		return gntdev_ioctl_set_max_grants(priv, ptr);

Would it make sense to return -EPNOTSUPPORTED? Or does it not really matter as nobody has been using this ioctl call?

Does this produce a clearer error message than the default -ENOIOCTLCMD? It's possible that some people use it, since it was exposed as an API.

Quoted 9 lines

Looking at the Xen tools the user of this is:
xc_gnttab_set_max_grants which would end up returning whatever the error is. I don't see any users of this in the Xen tools, thought there might be some in the XCP code. Lets stay with your ENOIOCTLCMD.

However, I was wondering if you are going to submit a patch to the Xen tool stack so that it can utlize the SysFS interface to set the limits for that API call?

No, because the semantics of what the limit is covering have changed. The new limit is per-domain, and if there was any existing code that set the limit, it would have been a per-open value and probably too low. I think it was suggested that the call be removed from the Xen API; I can submit a patch to do that, if you want.

The new value is probably best set in modprobe.conf if gntdev is a module; the sysfs interface is useful for runtime adjustment or if it is builtin.

Signature · 2 lines
Daniel De Graaf
National Security Agency
#7 lore raw

Re: [PATCH 2/7] xen-gntdev: Change page limit to be global instead of per-open

Quoted 29 lines · Daniel De Graaf
Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 8 lines · Daniel De Graaf
Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 7 lines

static long gntdev_ioctl(struct file *flip,

drivers/xen/gntdev.c +0 -0
 			 unsigned int cmd, unsigned long arg)
 {
@@ -555,9 +538,6 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 
Quoted 8 lines
Quoted 5 lines

drivers/xen/gntdev.c static long gntdev_ioctl(struct file *flip, · line 559

Quoted 2 lines
drivers/xen/gntdev.c +0 -2
-	case IOCTL_GNTDEV_SET_MAX_GRANTS:
-		return gntdev_ioctl_set_max_grants(priv, ptr);

Would it make sense to return -EPNOTSUPPORTED? Or does it not really matter as nobody has been using this ioctl call?

Does this produce a clearer error message than the default -ENOIOCTLCMD? It's possible that some people use it, since it was exposed as an API.

Quoted 9 lines

Looking at the Xen tools the user of this is:
xc_gnttab_set_max_grants which would end up returning whatever the error is. I don't see any users of this in the Xen tools, thought there might be some in the XCP code. Lets stay with your ENOIOCTLCMD.

However, I was wondering if you are going to submit a patch to the Xen tool stack so that it can utlize the SysFS interface to set the limits for that API call?

Quoted 5 lines

No, because the semantics of what the limit is covering have changed. The new limit is per-domain, and if there was any existing code that set the limit, it would have been a per-open value and probably too low. I think it was suggested that the call be removed from the Xen API; I can submit a patch to do that, if you want.

Please do. Thank you.

Quoted 7 lines

The new value is probably best set in modprobe.conf if gntdev is a module; the sysfs interface is useful for runtime adjustment or if it is builtin.

Signature · 2 lines
Daniel De Graaf
National Security Agency
#8 lore raw

PATCH 1/1 Remove set_max_grants in linux

Quoted 30 lines · Konrad Rzeszutek Wilk
Quoted 29 lines · Daniel De Graaf
Quoted 27 lines · Konrad Rzeszutek Wilk
Quoted 8 lines · Daniel De Graaf
Quoted 7 lines · Konrad Rzeszutek Wilk
Quoted 7 lines

static long gntdev_ioctl(struct file *flip,

(quoted diff) +0 -0
 			 unsigned int cmd, unsigned long arg)
 {
@@ -555,9 +538,6 @@ static long gntdev_ioctl(struct file *flip,
 	case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR:
 		return gntdev_ioctl_get_offset_for_vaddr(priv, ptr);
 
Quoted 8 lines
Quoted 5 lines

Review static long gntdev_ioctl(struct file *flip, · line 559

Quoted 2 lines
(quoted diff) +0 -2
-	case IOCTL_GNTDEV_SET_MAX_GRANTS:
-		return gntdev_ioctl_set_max_grants(priv, ptr);

Would it make sense to return -EPNOTSUPPORTED? Or does it not really matter as nobody has been using this ioctl call?

Does this produce a clearer error message than the default -ENOIOCTLCMD? It's possible that some people use it, since it was exposed as an API.

Looking at the Xen tools the user of this is:
xc_gnttab_set_max_grants which would end up returning whatever the error is. I don't see any users of this in the Xen tools, thought there might be some in the XCP code. Lets stay with your ENOIOCTLCMD.

However, I was wondering if you are going to submit a patch to the Xen tool stack so that it can utlize the SysFS interface to set the limits for that API call?

Quoted 8 lines
Quoted 5 lines

No, because the semantics of what the limit is covering have changed. The new limit is per-domain, and if there was any existing code that set the limit, it would have been a per-open value and probably too low. I think it was suggested that the call be removed from the Xen API; I can submit a patch to do that, if you want.

Please do. Thank you.

I just removed the linux version, because I didn't want to prevent its use in minios where it is needed to resize the array of grants.

------------------------------------------------------------------->8

The maximum number of grants is now constrained domain-wide in linux, so set_max_grants should be a noop there. Previously, this constraint was per-file-description.

tools/libxc/xc_gnttab.c +2 -0
diff -r 7b4c82f07281 tools/libxc/xc_gnttab.c
@@ -184,6 +184,8 @@ int xc_gnttab_munmap(xc_gnttab *xcg,
 
 int xc_gnttab_set_max_grants(xc_gnttab *xcg, uint32_t count)
 {
+	if (!xcg->ops->u.gnttab.set_max_grants)
+		return 0;
 	return xcg->ops->u.gnttab.set_max_grants(xcg, xcg->ops_handle, count);
 }
 
tools/libxc/xc_linux_osdep.c +0 -14
diff -r 7b4c82f07281 tools/libxc/xc_linux_osdep.c
@@ -627,19 +627,6 @@ static int linux_gnttab_munmap(xc_gnttab
     return 0;
 }
 
-static int linux_gnttab_set_max_grants(xc_gnttab *xcg, xc_osdep_handle h, uint32_t count)
-{
-    int fd = (int)h;
-    struct ioctl_gntdev_set_max_grants set_max;
-    int rc;
-
-    set_max.count = count;
-    if ( (rc = ioctl(fd, IOCTL_GNTDEV_SET_MAX_GRANTS, &set_max)) )
-        return rc;
-
-    return 0;
-}
-
 static struct xc_osdep_ops linux_gnttab_ops = {
     .open = &linux_gnttab_open,
     .close = &linux_gnttab_close,
@@ -649,7 +636,6 @@ static struct xc_osdep_ops linux_gnttab_
         .map_grant_refs = &linux_gnttab_map_grant_refs,
         .map_domain_grant_refs = &linux_gnttab_map_domain_grant_refs,
         .munmap = &linux_gnttab_munmap,
-        .set_max_grants = &linux_gnttab_set_max_grants,
     },
 };
#9 lore raw

Re: [PATCH libxc] Remove set_max_grants in linux

Daniel De Graaf writes ("[Xen-devel] [PATCH libxc] Remove set_max_grants in linux"):

Quoted 8 lines

I just removed the linux version, because I didn't want to prevent its use in minios where it is needed to resize the array of grants.

------------------------------------------------------------------->8

The maximum number of grants is now constrained domain-wide in linux, so set_max_grants should be a noop there. Previously, this constraint was per-file-description.

Is this unconditional change suitable for putting into xen-unstable.hg for the 4.1 release ? In particular, will it break compatibility with "traditional Xen" kernels ?

Ian.

#10 lore raw

Re: [PATCH libxc] Remove set_max_grants in linux

Quoted 10 lines · Ian Jackson

Daniel De Graaf writes ("[Xen-devel] [PATCH libxc] Remove set_max_grants in linux"):

Quoted 8 lines

I just removed the linux version, because I didn't want to prevent its use in minios where it is needed to resize the array of grants.

------------------------------------------------------------------->8

The maximum number of grants is now constrained domain-wide in linux, so set_max_grants should be a noop there. Previously, this constraint was per-file-description.

Quoted 6 lines

Is this unconditional change suitable for putting into xen-unstable.hg for the 4.1 release ? In particular, will it break compatibility with "traditional Xen" kernels ?

Ian.

It won't break compatibility; Linux has never required that you set the limit in order to use grants. The default limit is set to the maximum possible limit, so existing users cannot have been increasing it; changing a possible decrease to a noop shouldn't break anything sane.

Signature · 2 lines
Daniel De Graaf
National Security Agency
#11 lore raw

Re: [PATCH libxc] Remove set_max_grants in linux

In reply to Daniel De Graaf

Daniel De Graaf writes ("[Xen-devel] [PATCH libxc] Remove set_max_grants in linux"):

Quoted 2 lines

I just removed the linux version, because I didn't want to prevent its use in minios where it is needed to resize the array of grants.

...

Quoted 3 lines

The maximum number of grants is now constrained domain-wide in linux, so set_max_grants should be a noop there. Previously, this constraint was per-file-description.

Can you provide a Signed-off-By please, as your confirmation of the statements in the Developer's Certificate of Origin ?

Then I'll apply your patch after rc1.

Thanks,
Ian.

Quoted 1 line

From Documentation/SubmittingPatches in the Linux kernel tree:

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or

(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

(d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

#12 lore raw

Re: [PATCH libxc] Remove set_max_grants in linux

Quoted 11 lines · Ian Jackson

Daniel De Graaf writes ("[Xen-devel] [PATCH libxc] Remove set_max_grants in linux"):

Quoted 2 lines

I just removed the linux version, because I didn't want to prevent its use in minios where it is needed to resize the array of grants.

...

Quoted 3 lines

The maximum number of grants is now constrained domain-wide in linux, so set_max_grants should be a noop there. Previously, this constraint was per-file-description.

Can you provide a Signed-off-By please, as your confirmation of the statements in the Developer's Certificate of Origin ?

Whoops, I forgot that mercurial likes to hide those lines.

Quoted 33 lines

Then I'll apply your patch after rc1.

Thanks,
Ian.

Quoted 1 line

From Documentation/SubmittingPatches in the Linux kernel tree:

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or

(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

(d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

Signature · 2 lines
Daniel De Graaf
National Security Agency
Applied
#13 lore raw

Re: [PATCH libxc] Remove set_max_grants in linux

In reply to Daniel De Graaf

Daniel De Graaf writes ("[Xen-devel] [PATCH libxc] Remove set_max_grants in linux"):

Quoted 3 lines

The maximum number of grants is now constrained domain-wide in linux, so set_max_grants should be a noop there. Previously, this constraint was per-file-description.

I have applied this patch, thanks.

Ian.

#14 lore raw

PATCH 3/7 xen-gntdev: Remove unneeded structures from grant_map tracking data

In reply to Daniel De Graaf

The entire hypercall argument list isn't required; only selected fields from the hypercall need to be tracked between the ioctl, map, and unmap operations.

---
 drivers/xen/gntdev.c |  222 ++++++++++++++++++++++++++++++--------------------
 1 files changed, 134 insertions(+), 88 deletions(-)
drivers/xen/gntdev.c +134 -88
index 7e21592..3778b85 100644
@@ -56,18 +56,24 @@ struct gntdev_priv {
 	struct mmu_notifier mn;
 };
 
+struct granted_page {
+	u64 pte_maddr;
+	union {
+		struct ioctl_gntdev_grant_ref target;
+		grant_handle_t handle;
+	};
+};
+
 struct grant_map {
 	struct list_head next;
 	struct gntdev_priv *priv;
 	struct vm_area_struct *vma;
 	int index;
 	int count;
-	int flags;
-	int is_mapped;
-	struct ioctl_gntdev_grant_ref *grants;
-	struct gnttab_map_grant_ref   *map_ops;
-	struct gnttab_unmap_grant_ref *unmap_ops;
+	int is_mapped:1;
+	int is_ro:1;
 	struct page **pages;
+	struct granted_page pginfo[0];
 };
 
 /* ------------------------------------------------------------------ */
@@ -85,24 +91,19 @@ static void gntdev_print_maps(struct gntdev_priv *priv,
 		       map->index == text_index && text ? text : "");
 }
 
-static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
+static struct grant_map *gntdev_alloc_map(int count,
+		struct ioctl_gntdev_grant_ref* grants)
 {
 	struct grant_map *add;
 	int i;
 
-	add = kzalloc(sizeof(struct grant_map), GFP_KERNEL);
-	if (NULL == add)
+	add = kzalloc(sizeof(*add) + sizeof(add->pginfo[0])*count, GFP_KERNEL);
+	if (!add)
 		return NULL;
 
-	add->grants    = kzalloc(sizeof(add->grants[0])    * count, GFP_KERNEL);
-	add->map_ops   = kzalloc(sizeof(add->map_ops[0])   * count, GFP_KERNEL);
-	add->unmap_ops = kzalloc(sizeof(add->unmap_ops[0]) * count, GFP_KERNEL);
-	add->pages     = kzalloc(sizeof(add->pages[0])     * count, GFP_KERNEL);
-	if (NULL == add->grants    ||
-	    NULL == add->map_ops   ||
-	    NULL == add->unmap_ops ||
-	    NULL == add->pages)
-		goto err;
+	add->pages = kzalloc(sizeof(add->pages[0])*count, GFP_KERNEL);
+	if (!add->pages)
+		goto err_nopages;
 
 	for (i = 0; i < count; i++) {
 		add->pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
@@ -112,20 +113,18 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
 
 	add->index = 0;
 	add->count = count;
-	add->priv  = priv;
+	for(i = 0; i < count; i++)
+		add->pginfo[i].target = grants[i];
 
 	return add;
 
 err:
-	if (add->pages)
-		for (i = 0; i < count; i++) {
-			if (add->pages[i])
-				__free_page(add->pages[i]);
-		}
+	for (i = 0; i < count; i++) {
+		if (add->pages[i])
+			__free_page(add->pages[i]);
+	}
 	kfree(add->pages);
-	kfree(add->grants);
-	kfree(add->map_ops);
-	kfree(add->unmap_ops);
+err_nopages:
 	kfree(add);
 	return NULL;
 }
@@ -134,6 +133,7 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 {
 	struct grant_map *map;
 
+	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
 		if (add->index + add->count < map->index) {
 			list_add_tail(&add->next, &map->next);
@@ -144,8 +144,10 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 	list_add_tail(&add->next, &priv->maps);
 
 done:
+	add->priv = priv;
 	if (debug)
 		gntdev_print_maps(priv, "[new]", add->index);
+	spin_unlock(&priv->lock);
 }
 
 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int index,
@@ -186,9 +188,10 @@ static int gntdev_del_map(struct grant_map *map)
 
 	if (map->vma)
 		return -EBUSY;
-	for (i = 0; i < map->count; i++)
-		if (map->unmap_ops[i].handle)
-			return -EBUSY;
+	if (map->is_mapped)
+		for (i = 0; i < map->count; i++)
+			if (map->pginfo[i].handle)
+				return -EBUSY;
 
 	atomic_sub(map->count, &pages_mapped);
 	list_del(&map->next);
@@ -202,15 +205,10 @@ static void gntdev_free_map(struct grant_map *map)
 	if (!map)
 		return;
 
-	if (map->pages)
-		for (i = 0; i < map->count; i++) {
-			if (map->pages[i])
-				__free_page(map->pages[i]);
-		}
-	kfree(map->pages);
-	kfree(map->grants);
-	kfree(map->map_ops);
-	kfree(map->unmap_ops);
+	for (i = 0; i < map->count; i++) {
+		if (map->pages[i])
+			__free_page(map->pages[i]);
+	}
 	kfree(map);
 }
 
@@ -223,53 +221,99 @@ static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void
 	u64 pte_maddr;
 
 	BUG_ON(pgnr >= map->count);
+
 	pte_maddr = arbitrary_virt_to_machine(pte).maddr;
+	map->pginfo[pgnr].pte_maddr = pte_maddr;
 
-	gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
-			  GNTMAP_contains_pte | map->flags,
-			  map->grants[pgnr].ref,
-			  map->grants[pgnr].domid);
-	gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
-			    GNTMAP_contains_pte | map->flags,
-			    0 /* handle */);
 	return 0;
 }
 
 static int map_grant_pages(struct grant_map *map)
 {
-	int i, err = 0;
+	int i, flags, err = 0;
+	struct gnttab_map_grant_ref* map_ops = NULL;
 
+	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	if (map->is_ro)
+		flags |= GNTMAP_readonly;
+
+	err = -ENOMEM;
+	map_ops = kzalloc(sizeof(map_ops[0]) * map->count, GFP_TEMPORARY);
+	if (!map_ops)
+		goto out;
+
+	for(i=0; i < map->count; i++) {
+		gnttab_set_map_op(&map_ops[i], map->pginfo[i].pte_maddr, flags,
+				  map->pginfo[i].target.ref,
+				  map->pginfo[i].target.domid);
+	}
 	if (debug)
 		printk("%s: map %d+%d\n", __FUNCTION__, map->index, map->count);
-	err = gnttab_map_refs(map->map_ops, map->pages, map->count);
+
+	err = gnttab_map_refs(map_ops, map->pages, map->count);
+
 	if (WARN_ON(err))
-		return err;
+		goto out;
 
 	for (i = 0; i < map->count; i++) {
-		if (map->map_ops[i].status)
+		if (map_ops[i].status) {
+			__free_page(map->pages[i]);
+			map->pages[i] = NULL;
 			err = -EINVAL;
-		map->unmap_ops[i].handle = map->map_ops[i].handle;
+		} else {
+			map->pginfo[i].handle = map_ops[i].handle;
+		}
 	}
+
+out:
+	kfree(map_ops);
 	return err;
 }
 
-static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
+static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 {
-	int i, err = 0;
+	int i, flags, err = 0;
+	struct gnttab_unmap_grant_ref *unmap_ops;
+	struct gnttab_unmap_grant_ref unmap_single;
+
+	if (pages > 1) {
+		unmap_ops = kzalloc(sizeof(unmap_ops[0]) * pages,
+		                    GFP_TEMPORARY);
+		if (unlikely(!unmap_ops)) {
+			for(i=0; i < pages; i++)
+				unmap_grant_pages(map, offset + i, 1);
+			return;
+		}
+	} else {
+		unmap_ops = &unmap_single;
+	}
+
+	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	if (map->is_ro)
+		flags |= GNTMAP_readonly;
 
+	for(i=0; i < pages; i++)
+		gnttab_set_unmap_op(&unmap_ops[i],
+		                    map->pginfo[offset+i].pte_maddr, flags,
+		                    map->pginfo[offset+i].handle);
 	if (debug)
 		printk("%s: map %d+%d [%d+%d]\n", __FUNCTION__,
 		       map->index, map->count, offset, pages);
-	err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
+
+	err = gnttab_unmap_refs(unmap_ops, map->pages + offset, pages);
+
 	if (WARN_ON(err))
-		return err;
+		goto out;
 
 	for (i = 0; i < pages; i++) {
-		if (map->unmap_ops[offset+i].status)
-			err = -EINVAL;
-		map->unmap_ops[offset+i].handle = 0;
+		WARN_ON(unmap_ops[i].status);
+		__free_page(map->pages[offset+i]);
+		map->pages[offset+i] = NULL;
+		map->pginfo[offset+i].handle = 0;
 	}
-	return err;
+out:
+	if (unmap_ops != &unmap_single)
+		kfree(unmap_ops);
 }
 
 /* ------------------------------------------------------------------ */
@@ -308,7 +352,6 @@ static void mn_invl_range_start(struct mmu_notifier *mn,
 	struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 	struct grant_map *map;
 	unsigned long mstart, mend;
-	int err;
 
 	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
@@ -327,10 +370,9 @@ static void mn_invl_range_start(struct mmu_notifier *mn,
 			       __FUNCTION__, map->index, map->count,
 			       map->vma->vm_start, map->vma->vm_end,
 			       start, end, mstart, mend);
-		err = unmap_grant_pages(map,
-					(mstart - map->vma->vm_start) >> PAGE_SHIFT,
-					(mend - mstart) >> PAGE_SHIFT);
-		WARN_ON(err);
+		unmap_grant_pages(map,
+				  (mstart - map->vma->vm_start) >> PAGE_SHIFT,
+				  (mend - mstart) >> PAGE_SHIFT);
 	}
 	spin_unlock(&priv->lock);
 }
@@ -347,7 +389,6 @@ static void mn_release(struct mmu_notifier *mn,
 {
 	struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 	struct grant_map *map;
-	int err;
 
 	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
@@ -357,8 +398,7 @@ static void mn_release(struct mmu_notifier *mn,
 			printk("%s: map %d+%d (%lx %lx)\n",
 			       __FUNCTION__, map->index, map->count,
 			       map->vma->vm_start, map->vma->vm_end);
-		err = unmap_grant_pages(map, 0, map->count);
-		WARN_ON(err);
+		unmap_grant_pages(map, 0, map->count);
 	}
 	spin_unlock(&priv->lock);
 }
@@ -427,6 +467,7 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 {
 	struct ioctl_gntdev_map_grant_ref op;
 	struct grant_map *map;
+	struct ioctl_gntdev_grant_ref* grants;
 	int err;
 
 	if (copy_from_user(&op, u, sizeof(op)) != 0)
@@ -437,38 +478,45 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 	if (unlikely(op.count <= 0))
 		return -EINVAL;
 
-	err = -ENOMEM;
-	map = gntdev_alloc_map(priv, op.count);
-	if (!map)
-		return err;
+	grants = kmalloc(sizeof(grants[0]) * op.count, GFP_TEMPORARY);
+	if (!grants)
+		return -ENOMEM;
 
-	if (copy_from_user(map->grants, &u->refs,
-			   sizeof(map->grants[0]) * op.count) != 0) {
-		gntdev_free_map(map);
-		return err;
+	if (copy_from_user(grants, u->refs, sizeof(grants[0]) * op.count)) {
+		err = -EFAULT;
+		goto out_free;
 	}
 
+	err = -ENOMEM;
+	map = gntdev_alloc_map(op.count, grants);
+	if (!map)
+		goto out_free;
+
 	if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit))
 	{
 		if (debug)
 			printk("%s: can't map: over limit\n", __FUNCTION__);
-		gntdev_free_map(map);
-		return err;
+		goto out_free_map;
 	}
 
-	spin_lock(&priv->lock);
 	gntdev_add_map(priv, map);
 	op.index = map->index << PAGE_SHIFT;
-	spin_unlock(&priv->lock);
 
-	if (copy_to_user(u, &op, sizeof(op)) != 0) {
-		spin_lock(&priv->lock);
-		gntdev_del_map(map);
-		spin_unlock(&priv->lock);
-		gntdev_free_map(map);
-		return err;
+	if (copy_to_user(u, &op, sizeof(op))) {
+		err = -EFAULT;
+		goto out_remove;
 	}
-	return 0;
+	err = 0;
+out_free:
+	kfree(grants);
+	return err;
+out_remove:
+	spin_lock(&priv->lock);
+	gntdev_del_map(map);
+	spin_unlock(&priv->lock);
+out_free_map:
+	gntdev_free_map(map);
+	goto out_free;
 }
 
 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
@@ -584,9 +632,7 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	vma->vm_private_data = map;
 	map->vma = vma;
 
-	map->flags = GNTMAP_host_map | GNTMAP_application_map;
-	if (!(vma->vm_flags & VM_WRITE))
-		map->flags |= GNTMAP_readonly;
+	map->is_ro = !(vma->vm_flags & VM_WRITE);
 
 	spin_unlock(&priv->lock);
 
Signature · 1 line
1.7.2.3
#15 lore raw

Re: [PATCH 3/7] xen-gntdev: Remove unneeded structures from grant_map tracking data

Quoted 9 lines
drivers/xen/gntdev.c +1 -0
@@ -134,6 +133,7 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 {
 	struct grant_map *map;
 
+	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
 		if (add->index + add->count < map->index) {
 			list_add_tail(&add->next, &map->next);

drivers/xen/gntdev.c static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) · line 150

Quoted 7 lines
drivers/xen/gntdev.c +2 -0
@@ -144,8 +144,10 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 	list_add_tail(&add->next, &priv->maps);
 
 done:
+	add->priv = priv;
 	if (debug)
 		gntdev_print_maps(priv, "[new]", add->index);
+	spin_unlock(&priv->lock);

That looks like you are also fixing a bug?

Quoted 52 lines
drivers/xen/gntdev.c +10 -19
 }
 
 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int index,
@@ -186,9 +188,10 @@ static int gntdev_del_map(struct grant_map *map)
 
 	if (map->vma)
 		return -EBUSY;
-	for (i = 0; i < map->count; i++)
-		if (map->unmap_ops[i].handle)
-			return -EBUSY;
+	if (map->is_mapped)
+		for (i = 0; i < map->count; i++)
+			if (map->pginfo[i].handle)
+				return -EBUSY;
 
 	atomic_sub(map->count, &pages_mapped);
 	list_del(&map->next);
@@ -202,15 +205,10 @@ static void gntdev_free_map(struct grant_map *map)
 	if (!map)
 		return;
 
-	if (map->pages)
-		for (i = 0; i < map->count; i++) {
-			if (map->pages[i])
-				__free_page(map->pages[i]);
-		}
-	kfree(map->pages);
-	kfree(map->grants);
-	kfree(map->map_ops);
-	kfree(map->unmap_ops);
+	for (i = 0; i < map->count; i++) {
+		if (map->pages[i])
+			__free_page(map->pages[i]);
+	}
 	kfree(map);
 }
 
@@ -223,53 +221,99 @@ static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void
 	u64 pte_maddr;
 
 	BUG_ON(pgnr >= map->count);
+
 	pte_maddr = arbitrary_virt_to_machine(pte).maddr;
+	map->pginfo[pgnr].pte_maddr = pte_maddr;
 
-	gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
-			  GNTMAP_contains_pte | map->flags,
-			  map->grants[pgnr].ref,
-			  map->grants[pgnr].domid);
-	gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
-			    GNTMAP_contains_pte | map->flags,
-			    0 /* handle */);

drivers/xen/gntdev.c static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void · line 236

Quoted 10 lines
drivers/xen/gntdev.c +3 -1
 	return 0;
 }
 
 static int map_grant_pages(struct grant_map *map)
 {
-	int i, err = 0;
+	int i, flags, err = 0;
+	struct gnttab_map_grant_ref* map_ops = NULL;
 
+	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;

I am not sure if the GNTMAP_contains_pte is correct here. Stefano mentioned that is used to determine how many arguments to put in the hypercall. Looking at the previous usage - it was only done on the unmap_op, while you enforce it on map_op too?

Quoted 74 lines
drivers/xen/gntdev.c +52 -7
+	if (map->is_ro)
+		flags |= GNTMAP_readonly;
+
+	err = -ENOMEM;
+	map_ops = kzalloc(sizeof(map_ops[0]) * map->count, GFP_TEMPORARY);
+	if (!map_ops)
+		goto out;
+
+	for(i=0; i < map->count; i++) {
+		gnttab_set_map_op(&map_ops[i], map->pginfo[i].pte_maddr, flags,
+				  map->pginfo[i].target.ref,
+				  map->pginfo[i].target.domid);
+	}
 	if (debug)
 		printk("%s: map %d+%d\n", __FUNCTION__, map->index, map->count);
-	err = gnttab_map_refs(map->map_ops, map->pages, map->count);
+
+	err = gnttab_map_refs(map_ops, map->pages, map->count);
+
 	if (WARN_ON(err))
-		return err;
+		goto out;
 
 	for (i = 0; i < map->count; i++) {
-		if (map->map_ops[i].status)
+		if (map_ops[i].status) {
+			__free_page(map->pages[i]);
+			map->pages[i] = NULL;
 			err = -EINVAL;
-		map->unmap_ops[i].handle = map->map_ops[i].handle;
+		} else {
+			map->pginfo[i].handle = map_ops[i].handle;
+		}
 	}
+
+out:
+	kfree(map_ops);
 	return err;
 }
 
-static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
+static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 {
-	int i, err = 0;
+	int i, flags, err = 0;
+	struct gnttab_unmap_grant_ref *unmap_ops;
+	struct gnttab_unmap_grant_ref unmap_single;
+
+	if (pages > 1) {
+		unmap_ops = kzalloc(sizeof(unmap_ops[0]) * pages,
+		                    GFP_TEMPORARY);
+		if (unlikely(!unmap_ops)) {
+			for(i=0; i < pages; i++)
+				unmap_grant_pages(map, offset + i, 1);
+			return;
+		}
+	} else {
+		unmap_ops = &unmap_single;
+	}
+
+	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	if (map->is_ro)
+		flags |= GNTMAP_readonly;
 
+	for(i=0; i < pages; i++)
+		gnttab_set_unmap_op(&unmap_ops[i],
+		                    map->pginfo[offset+i].pte_maddr, flags,
+		                    map->pginfo[offset+i].handle);
 	if (debug)
 		printk("%s: map %d+%d [%d+%d]\n", __FUNCTION__,
 		       map->index, map->count, offset, pages);
-	err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
+
+	err = gnttab_unmap_refs(unmap_ops, map->pages + offset, pages);

drivers/xen/gntdev.c static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void · line 309

Quoted 10 lines
drivers/xen/gntdev.c +3 -4
+
 	if (WARN_ON(err))
-		return err;
+		goto out;
 
 	for (i = 0; i < pages; i++) {
-		if (map->unmap_ops[offset+i].status)
-			err = -EINVAL;
-		map->unmap_ops[offset+i].handle = 0;
+		WARN_ON(unmap_ops[i].status);

Why change it from err to WARN_ON? I think the caller of this function checks for this too so you would end up with two WARN_ON?

Also, you don't add the offset value to i here..

Quoted 20 lines
drivers/xen/gntdev.c +6 -2
+		__free_page(map->pages[offset+i]);
+		map->pages[offset+i] = NULL;
+		map->pginfo[offset+i].handle = 0;
 	}
-	return err;
+out:
+	if (unmap_ops != &unmap_single)
+		kfree(unmap_ops);
 }
 
 /* ------------------------------------------------------------------ */
@@ -308,7 +352,6 @@ static void mn_invl_range_start(struct mmu_notifier *mn,
 	struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 	struct grant_map *map;
 	unsigned long mstart, mend;
-	int err;
 
 	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {

drivers/xen/gntdev.c static void mn_invl_range_start(struct mmu_notifier *mn, · line 375

Quoted 10 lines
drivers/xen/gntdev.c +3 -4
@@ -327,10 +370,9 @@ static void mn_invl_range_start(struct mmu_notifier *mn,
 			       __FUNCTION__, map->index, map->count,
 			       map->vma->vm_start, map->vma->vm_end,
 			       start, end, mstart, mend);
-		err = unmap_grant_pages(map,
-					(mstart - map->vma->vm_start) >> PAGE_SHIFT,
-					(mend - mstart) >> PAGE_SHIFT);
-		WARN_ON(err);
+		unmap_grant_pages(map,
+				  (mstart - map->vma->vm_start) >> PAGE_SHIFT,
+				  (mend - mstart) >> PAGE_SHIFT);

Ah, so you rememoved the WARN_ON here. What is the reason for doing so?

Quoted 52 lines
drivers/xen/gntdev.c +10 -11
 	}
 	spin_unlock(&priv->lock);
 }
@@ -347,7 +389,6 @@ static void mn_release(struct mmu_notifier *mn,
 {
 	struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 	struct grant_map *map;
-	int err;
 
 	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
@@ -357,8 +398,7 @@ static void mn_release(struct mmu_notifier *mn,
 			printk("%s: map %d+%d (%lx %lx)\n",
 			       __FUNCTION__, map->index, map->count,
 			       map->vma->vm_start, map->vma->vm_end);
-		err = unmap_grant_pages(map, 0, map->count);
-		WARN_ON(err);
+		unmap_grant_pages(map, 0, map->count);
 	}
 	spin_unlock(&priv->lock);
 }
@@ -427,6 +467,7 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 {
 	struct ioctl_gntdev_map_grant_ref op;
 	struct grant_map *map;
+	struct ioctl_gntdev_grant_ref* grants;
 	int err;
 
 	if (copy_from_user(&op, u, sizeof(op)) != 0)
@@ -437,38 +478,45 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 	if (unlikely(op.count <= 0))
 		return -EINVAL;
 
-	err = -ENOMEM;
-	map = gntdev_alloc_map(priv, op.count);
-	if (!map)
-		return err;
+	grants = kmalloc(sizeof(grants[0]) * op.count, GFP_TEMPORARY);
+	if (!grants)
+		return -ENOMEM;
 
-	if (copy_from_user(map->grants, &u->refs,
-			   sizeof(map->grants[0]) * op.count) != 0) {
-		gntdev_free_map(map);
-		return err;
+	if (copy_from_user(grants, u->refs, sizeof(grants[0]) * op.count)) {
+		err = -EFAULT;
+		goto out_free;
 	}
 
+	err = -ENOMEM;
+	map = gntdev_alloc_map(op.count, grants);

drivers/xen/gntdev.c static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, · line 499

Quoted 10 lines
drivers/xen/gntdev.c +4 -2
+	if (!map)
+		goto out_free;
+
 	if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit))
 	{
 		if (debug)
 			printk("%s: can't map: over limit\n", __FUNCTION__);
-		gntdev_free_map(map);
-		return err;
+		goto out_free_map;

I just noticed it now, but shouldn't we also free grants? That looks like it needs a seperate bug patch thought.

drivers/xen/gntdev.c static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, · line 462

Quoted 6 lines
drivers/xen/gntdev.c +0 -2
 	}
 
-	spin_lock(&priv->lock);
 	gntdev_add_map(priv, map);
 	op.index = map->index << PAGE_SHIFT;
-	spin_unlock(&priv->lock);

Ah, so you moved the spinlock down. I presume it is OK to have op.index be unprotected.

drivers/xen/gntdev.c static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, · line 507

Quoted 10 lines
drivers/xen/gntdev.c +3 -6
-	if (copy_to_user(u, &op, sizeof(op)) != 0) {
-		spin_lock(&priv->lock);
-		gntdev_del_map(map);
-		spin_unlock(&priv->lock);
-		gntdev_free_map(map);
-		return err;
+	if (copy_to_user(u, &op, sizeof(op))) {
+		err = -EFAULT;
+		goto out_remove;

Hmm, should we free the grants on this exit path?

Quoted 14 lines
drivers/xen/gntdev.c +11 -1
 	}
-	return 0;
+	err = 0;
+out_free:
+	kfree(grants);
+	return err;
+out_remove:
+	spin_lock(&priv->lock);
+	gntdev_del_map(map);
+	spin_unlock(&priv->lock);
+out_free_map:
+	gntdev_free_map(map);
+	goto out_free;
 }
#16 lore raw

Re: [PATCH 3/7] xen-gntdev: Remove unneeded structures from grant_map tracking data

Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 11 lines
drivers/xen/gntdev.c +1 -0
@@ -134,6 +133,7 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 {
 	struct grant_map *map;
 
+	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
 		if (add->index + add->count < map->index) {
 			list_add_tail(&add->next, &map->next);
@@ -144,8 +144,10 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 	list_add_tail(&add->next, &priv->maps);
 
Quoted 7 lines

drivers/xen/gntdev.c static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add) · line 150

Quoted 5 lines
drivers/xen/gntdev.c +2 -0
 done:
+	add->priv = priv;
 	if (debug)
 		gntdev_print_maps(priv, "[new]", add->index);
+	spin_unlock(&priv->lock);

That looks like you are also fixing a bug?

The lock is just being pushed inside this function from its caller, to make it easier to see where the lock was being held.

Quoted 61 lines
Quoted 61 lines
drivers/xen/gntdev.c +12 -20
 }
 
 static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int index,
@@ -186,9 +188,10 @@ static int gntdev_del_map(struct grant_map *map)
 
 	if (map->vma)
 		return -EBUSY;
-	for (i = 0; i < map->count; i++)
-		if (map->unmap_ops[i].handle)
-			return -EBUSY;
+	if (map->is_mapped)
+		for (i = 0; i < map->count; i++)
+			if (map->pginfo[i].handle)
+				return -EBUSY;
 
 	atomic_sub(map->count, &pages_mapped);
 	list_del(&map->next);
@@ -202,15 +205,10 @@ static void gntdev_free_map(struct grant_map *map)
 	if (!map)
 		return;
 
-	if (map->pages)
-		for (i = 0; i < map->count; i++) {
-			if (map->pages[i])
-				__free_page(map->pages[i]);
-		}
-	kfree(map->pages);
-	kfree(map->grants);
-	kfree(map->map_ops);
-	kfree(map->unmap_ops);
+	for (i = 0; i < map->count; i++) {
+		if (map->pages[i])
+			__free_page(map->pages[i]);
+	}
 	kfree(map);
 }
 
@@ -223,53 +221,99 @@ static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void
 	u64 pte_maddr;
 
 	BUG_ON(pgnr >= map->count);
+
 	pte_maddr = arbitrary_virt_to_machine(pte).maddr;
+	map->pginfo[pgnr].pte_maddr = pte_maddr;
 
-	gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr,
-			  GNTMAP_contains_pte | map->flags,
-			  map->grants[pgnr].ref,
-			  map->grants[pgnr].domid);
-	gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr,
-			    GNTMAP_contains_pte | map->flags,
-			    0 /* handle */);
 	return 0;
 }
 
 static int map_grant_pages(struct grant_map *map)
 {
-	int i, err = 0;
+	int i, flags, err = 0;
+	struct gnttab_map_grant_ref* map_ops = NULL;
 
Quoted 6 lines

Review static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void · line 236

Quoted 1 line
(quoted diff) +1 -0
+	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;

I am not sure if the GNTMAP_contains_pte is correct here. Stefano mentioned that is used to determine how many arguments to put in the hypercall. Looking at the previous usage - it was only done on the unmap_op, while you enforce it on map_op too?

GNTMAP_contains_pte is used on PV guests (only) to indicate that the argument contains PTEs that need to be modified, rather than PFNs to remap. See patch 6 for the HVM case where this flag is not used.

Previous use had it on both map and unmap. This was clearer prior to a reshuffling patch that moved it to the gnttab_set_map_op call.

Quoted 79 lines
Quoted 79 lines
drivers/xen/gntdev.c +54 -8
+	if (map->is_ro)
+		flags |= GNTMAP_readonly;
+
+	err = -ENOMEM;
+	map_ops = kzalloc(sizeof(map_ops[0]) * map->count, GFP_TEMPORARY);
+	if (!map_ops)
+		goto out;
+
+	for(i=0; i < map->count; i++) {
+		gnttab_set_map_op(&map_ops[i], map->pginfo[i].pte_maddr, flags,
+				  map->pginfo[i].target.ref,
+				  map->pginfo[i].target.domid);
+	}
 	if (debug)
 		printk("%s: map %d+%d\n", __FUNCTION__, map->index, map->count);
-	err = gnttab_map_refs(map->map_ops, map->pages, map->count);
+
+	err = gnttab_map_refs(map_ops, map->pages, map->count);
+
 	if (WARN_ON(err))
-		return err;
+		goto out;
 
 	for (i = 0; i < map->count; i++) {
-		if (map->map_ops[i].status)
+		if (map_ops[i].status) {
+			__free_page(map->pages[i]);
+			map->pages[i] = NULL;
 			err = -EINVAL;
-		map->unmap_ops[i].handle = map->map_ops[i].handle;
+		} else {
+			map->pginfo[i].handle = map_ops[i].handle;
+		}
 	}
+
+out:
+	kfree(map_ops);
 	return err;
 }
 
-static int unmap_grant_pages(struct grant_map *map, int offset, int pages)
+static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 {
-	int i, err = 0;
+	int i, flags, err = 0;
+	struct gnttab_unmap_grant_ref *unmap_ops;
+	struct gnttab_unmap_grant_ref unmap_single;
+
+	if (pages > 1) {
+		unmap_ops = kzalloc(sizeof(unmap_ops[0]) * pages,
+		                    GFP_TEMPORARY);
+		if (unlikely(!unmap_ops)) {
+			for(i=0; i < pages; i++)
+				unmap_grant_pages(map, offset + i, 1);
+			return;
+		}
+	} else {
+		unmap_ops = &unmap_single;
+	}
+
+	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	if (map->is_ro)
+		flags |= GNTMAP_readonly;
 
+	for(i=0; i < pages; i++)
+		gnttab_set_unmap_op(&unmap_ops[i],
+		                    map->pginfo[offset+i].pte_maddr, flags,
+		                    map->pginfo[offset+i].handle);
 	if (debug)
 		printk("%s: map %d+%d [%d+%d]\n", __FUNCTION__,
 		       map->index, map->count, offset, pages);
-	err = gnttab_unmap_refs(map->unmap_ops + offset, map->pages, pages);
+
+	err = gnttab_unmap_refs(unmap_ops, map->pages + offset, pages);
+
 	if (WARN_ON(err))
-		return err;
+		goto out;
 
Quoted 8 lines

drivers/xen/gntdev.c static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void · line 309

Quoted 5 lines
drivers/xen/gntdev.c +1 -3
 	for (i = 0; i < pages; i++) {
-		if (map->unmap_ops[offset+i].status)
-			err = -EINVAL;
-		map->unmap_ops[offset+i].handle = 0;
+		WARN_ON(unmap_ops[i].status);

Why change it from err to WARN_ON? I think the caller of this function checks for this too so you would end up with two WARN_ON?

No, unmap_ops[i].status is not ever seen by the caller. This function returns void, so the caller doesn't have anything to warn about.

Quoted 1 line

Also, you don't add the offset value to i here..

unmap_ops (the local variable) is indexed without using offset.

Quoted 17 lines
Quoted 17 lines
drivers/xen/gntdev.c +6 -2
+		__free_page(map->pages[offset+i]);
+		map->pages[offset+i] = NULL;
+		map->pginfo[offset+i].handle = 0;
 	}
-	return err;
+out:
+	if (unmap_ops != &unmap_single)
+		kfree(unmap_ops);
 }
 
 /* ------------------------------------------------------------------ */
@@ -308,7 +352,6 @@ static void mn_invl_range_start(struct mmu_notifier *mn,
 	struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 	struct grant_map *map;
 	unsigned long mstart, mend;
-	int err;
 
Quoted 15 lines

drivers/xen/gntdev.c static void mn_invl_range_start(struct mmu_notifier *mn, · line 375

Quoted 13 lines
drivers/xen/gntdev.c +3 -4
 	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
@@ -327,10 +370,9 @@ static void mn_invl_range_start(struct mmu_notifier *mn,
 			       __FUNCTION__, map->index, map->count,
 			       map->vma->vm_start, map->vma->vm_end,
 			       start, end, mstart, mend);
-		err = unmap_grant_pages(map,
-					(mstart - map->vma->vm_start) >> PAGE_SHIFT,
-					(mend - mstart) >> PAGE_SHIFT);
-		WARN_ON(err);
+		unmap_grant_pages(map,
+				  (mstart - map->vma->vm_start) >> PAGE_SHIFT,
+				  (mend - mstart) >> PAGE_SHIFT);

Ah, so you rememoved the WARN_ON here. What is the reason for doing so?

This makes it clearer where the error was located (in the hypercall return, or in the status of some page). Since the return value of unmap_grant_pages was never used except to pass to a WARN_ON, it seemed redundant.

Quoted 50 lines
Quoted 50 lines
drivers/xen/gntdev.c +8 -11
 	}
 	spin_unlock(&priv->lock);
 }
@@ -347,7 +389,6 @@ static void mn_release(struct mmu_notifier *mn,
 {
 	struct gntdev_priv *priv = container_of(mn, struct gntdev_priv, mn);
 	struct grant_map *map;
-	int err;
 
 	spin_lock(&priv->lock);
 	list_for_each_entry(map, &priv->maps, next) {
@@ -357,8 +398,7 @@ static void mn_release(struct mmu_notifier *mn,
 			printk("%s: map %d+%d (%lx %lx)\n",
 			       __FUNCTION__, map->index, map->count,
 			       map->vma->vm_start, map->vma->vm_end);
-		err = unmap_grant_pages(map, 0, map->count);
-		WARN_ON(err);
+		unmap_grant_pages(map, 0, map->count);
 	}
 	spin_unlock(&priv->lock);
 }
@@ -427,6 +467,7 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 {
 	struct ioctl_gntdev_map_grant_ref op;
 	struct grant_map *map;
+	struct ioctl_gntdev_grant_ref* grants;
 	int err;
 
 	if (copy_from_user(&op, u, sizeof(op)) != 0)
@@ -437,38 +478,45 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 	if (unlikely(op.count <= 0))
 		return -EINVAL;
 
-	err = -ENOMEM;
-	map = gntdev_alloc_map(priv, op.count);
-	if (!map)
-		return err;
+	grants = kmalloc(sizeof(grants[0]) * op.count, GFP_TEMPORARY);
+	if (!grants)
+		return -ENOMEM;
 
-	if (copy_from_user(map->grants, &u->refs,
-			   sizeof(map->grants[0]) * op.count) != 0) {
-		gntdev_free_map(map);
-		return err;
+	if (copy_from_user(grants, u->refs, sizeof(grants[0]) * op.count)) {
+		err = -EFAULT;
+		goto out_free;
 	}
 
Quoted 15 lines

drivers/xen/gntdev.c static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, · line 499

Quoted 12 lines
drivers/xen/gntdev.c +6 -2
+	err = -ENOMEM;
+	map = gntdev_alloc_map(op.count, grants);
+	if (!map)
+		goto out_free;
+
 	if (unlikely(atomic_add_return(op.count, &pages_mapped) > limit))
 	{
 		if (debug)
 			printk("%s: can't map: over limit\n", __FUNCTION__);
-		gntdev_free_map(map);
-		return err;
+		goto out_free_map;

I just noticed it now, but shouldn't we also free grants? That looks like it needs a seperate bug patch thought.

It is getting freed: out_free_map goes to out_free after freeing the map.

Quoted 9 lines

drivers/xen/gntdev.c static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, · line 462

Quoted 6 lines
drivers/xen/gntdev.c +0 -2
 	}
 
-	spin_lock(&priv->lock);
 	gntdev_add_map(priv, map);
 	op.index = map->index << PAGE_SHIFT;
-	spin_unlock(&priv->lock);

Ah, so you moved the spinlock down. I presume it is OK to have op.index be unprotected.

Yep, op is on the stack.

Quoted 12 lines

drivers/xen/gntdev.c static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, · line 507

Quoted 10 lines
drivers/xen/gntdev.c +3 -6
-	if (copy_to_user(u, &op, sizeof(op)) != 0) {
-		spin_lock(&priv->lock);
-		gntdev_del_map(map);
-		spin_unlock(&priv->lock);
-		gntdev_free_map(map);
-		return err;
+	if (copy_to_user(u, &op, sizeof(op))) {
+		err = -EFAULT;
+		goto out_remove;

Hmm, should we free the grants on this exit path?

We are; out_remove falls to out_free_map falls to out_free.

Quoted 15 lines
Quoted 14 lines
drivers/xen/gntdev.c +11 -1
 	}
-	return 0;
+	err = 0;
+out_free:
+	kfree(grants);
+	return err;
+out_remove:
+	spin_lock(&priv->lock);
+	gntdev_del_map(map);
+	spin_unlock(&priv->lock);
+out_free_map:
+	gntdev_free_map(map);
+	goto out_free;
 }
#17 lore raw

PATCH 4/7 xen-gntdev: Use find_vma rather than iterating our vma list manually

In reply to Daniel De Graaf

This should be faster if many mappings exist, and also removes the only user of map->vma not related to PTE modification.

---
 drivers/xen/gntdev.c |   32 ++++++++------------------------
 1 files changed, 8 insertions(+), 24 deletions(-)
drivers/xen/gntdev.c +8 -24
index 3778b85..6a3c9e4 100644
@@ -165,23 +165,6 @@ static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind
 	return NULL;
 }
 
-static struct grant_map *gntdev_find_map_vaddr(struct gntdev_priv *priv,
-					       unsigned long vaddr)
-{
-	struct grant_map *map;
-
-	list_for_each_entry(map, &priv->maps, next) {
-		if (!map->vma)
-			continue;
-		if (vaddr < map->vma->vm_start)
-			continue;
-		if (vaddr >= map->vma->vm_end)
-			continue;
-		return map;
-	}
-	return NULL;
-}
-
 static int gntdev_del_map(struct grant_map *map)
 {
 	int i;
@@ -546,6 +529,7 @@ static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
 					      struct ioctl_gntdev_get_offset_for_vaddr __user *u)
 {
 	struct ioctl_gntdev_get_offset_for_vaddr op;
+	struct vm_area_struct *vma;
 	struct grant_map *map;
 
 	if (copy_from_user(&op, u, sizeof(op)) != 0)
@@ -554,16 +538,16 @@ static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv,
 		printk("%s: priv %p, offset for vaddr %lx\n", __FUNCTION__, priv,
 		       (unsigned long)op.vaddr);
 
-	spin_lock(&priv->lock);
-	map = gntdev_find_map_vaddr(priv, op.vaddr);
-	if (map == NULL ||
-	    map->vma->vm_start != op.vaddr) {
-		spin_unlock(&priv->lock);
+	vma = find_vma(current->mm, op.vaddr);
+	if (!vma || vma->vm_ops != &gntdev_vmops)
 		return -EINVAL;
-	}
+
+	map = vma->vm_private_data;
+	if (!map)
+		return -EINVAL;
+
 	op.offset = map->index << PAGE_SHIFT;
 	op.count = map->count;
-	spin_unlock(&priv->lock);
 
 	if (copy_to_user(u, &op, sizeof(op)) != 0)
 		return -EFAULT;
Signature · 1 line
1.7.2.3
#18 lore raw

PATCH 5/7 xen-gntdev: Add reference counting to maps

In reply to Daniel De Graaf

This allows userspace to perform mmap() on the gntdev device and then immediately close the filehandle or remove the mapping using the remove ioctl, with the mapped area remaining valid until unmapped. This also fixes an infinite loop when a gntdev device is closed without first unmapping all areas.

---
 drivers/xen/gntdev.c |   67 +++++++++++++++++++++++--------------------------
 1 files changed, 31 insertions(+), 36 deletions(-)
drivers/xen/gntdev.c +31 -36
index 6a3c9e4..f1fc8fa 100644
@@ -66,16 +66,18 @@ struct granted_page {
 
 struct grant_map {
 	struct list_head next;
-	struct gntdev_priv *priv;
 	struct vm_area_struct *vma;
 	int index;
 	int count;
+	atomic_t users;
 	int is_mapped:1;
 	int is_ro:1;
 	struct page **pages;
 	struct granted_page pginfo[0];
 };
 
+static void unmap_grant_pages(struct grant_map *map, int offset, int pages);
+
 /* ------------------------------------------------------------------ */
 
 static void gntdev_print_maps(struct gntdev_priv *priv,
@@ -112,6 +114,7 @@ static struct grant_map *gntdev_alloc_map(int count,
 	}
 
 	add->index = 0;
+	atomic_set(&add->users, 1);
 	add->count = count;
 	for(i = 0; i < count; i++)
 		add->pginfo[i].target = grants[i];
@@ -144,7 +147,6 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 	list_add_tail(&add->next, &priv->maps);
 
 done:
-	add->priv = priv;
 	if (debug)
 		gntdev_print_maps(priv, "[new]", add->index);
 	spin_unlock(&priv->lock);
@@ -165,33 +167,26 @@ static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind
 	return NULL;
 }
 
-static int gntdev_del_map(struct grant_map *map)
+static void gntdev_put_map(struct grant_map *map)
 {
 	int i;
 
-	if (map->vma)
-		return -EBUSY;
-	if (map->is_mapped)
-		for (i = 0; i < map->count; i++)
-			if (map->pginfo[i].handle)
-				return -EBUSY;
+	if (!map)
+		return;
 
-	atomic_sub(map->count, &pages_mapped);
-	list_del(&map->next);
-	return 0;
-}
+	if (!atomic_dec_and_test(&map->users))
+		return;
 
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
+	atomic_sub(map->count, &pages_mapped);
 
-	if (!map)
-		return;
+	if (!use_ptemod)
+		unmap_grant_pages(map, 0, map->count);
 
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
 	}
+	kfree(map->pages);
 	kfree(map);
 }
 
@@ -310,6 +305,7 @@ static void gntdev_vma_close(struct vm_area_struct *vma)
 	map->is_mapped = 0;
 	map->vma = NULL;
 	vma->vm_private_data = NULL;
+	gntdev_put_map(map);
 }
 
 static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
@@ -425,7 +421,6 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 {
 	struct gntdev_priv *priv = flip->private_data;
 	struct grant_map *map;
-	int err;
 
 	if (debug)
 		printk("%s: priv %p\n", __FUNCTION__, priv);
@@ -433,10 +428,8 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 	spin_lock(&priv->lock);
 	while (!list_empty(&priv->maps)) {
 		map = list_entry(priv->maps.next, struct grant_map, next);
-		err = gntdev_del_map(map);
-		if (WARN_ON(err))
-			gntdev_free_map(map);
-
+		list_del(&map->next);
+		gntdev_put_map(map);
 	}
 	spin_unlock(&priv->lock);
 
@@ -487,18 +480,14 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 
 	if (copy_to_user(u, &op, sizeof(op))) {
 		err = -EFAULT;
-		goto out_remove;
+		goto out_free;
 	}
 	err = 0;
 out_free:
 	kfree(grants);
 	return err;
-out_remove:
-	spin_lock(&priv->lock);
-	gntdev_del_map(map);
-	spin_unlock(&priv->lock);
 out_free_map:
-	gntdev_free_map(map);
+	gntdev_put_map(map);
 	goto out_free;
 }
 
@@ -507,7 +496,7 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 {
 	struct ioctl_gntdev_unmap_grant_ref op;
 	struct grant_map *map;
-	int err = -EINVAL;
+	int err;
 
 	if (copy_from_user(&op, u, sizeof(op)) != 0)
 		return -EFAULT;
@@ -517,11 +506,13 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 
 	spin_lock(&priv->lock);
 	map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
-	if (map)
-		err = gntdev_del_map(map);
+	if (map) {
+		list_del(&map->next);
+		gntdev_put_map(map);
+		err = 0;
+	} else
+		err = -EINVAL;
 	spin_unlock(&priv->lock);
-	if (!err)
-		gntdev_free_map(map);
 	return err;
 }
 
@@ -599,13 +590,15 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	map = gntdev_find_map_index(priv, index, count);
 	if (!map)
 		goto unlock_out;
-	if (map->vma)
+	if (use_ptemod && map->vma)
 		goto unlock_out;
 	if (priv->mm != vma->vm_mm) {
 		printk("%s: Huh? Other mm?\n", __FUNCTION__);
 		goto unlock_out;
 	}
 
+	atomic_inc(&map->users);
+
 	vma->vm_ops = &gntdev_vmops;
 
 	vma->vm_flags |= VM_RESERVED;
@@ -614,7 +607,9 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	vma->vm_flags |= VM_PFNMAP;
 
 	vma->vm_private_data = map;
-	map->vma = vma;
+
+	if (use_ptemod)
+		map->vma = vma;
 
 	map->is_ro = !(vma->vm_flags & VM_WRITE);
 
Signature · 1 line
1.7.2.3
#19 lore raw

Re: [PATCH 5/7] xen-gntdev: Add reference counting to maps

Quoted 16 lines · Daniel De Graaf

This allows userspace to perform mmap() on the gntdev device and then immediately close the filehandle or remove the mapping using the remove ioctl, with the mapped area remaining valid until unmapped. This also fixes an infinite loop when a gntdev device is closed without first unmapping all areas.

---
 drivers/xen/gntdev.c |   67 +++++++++++++++++++++++--------------------------
 1 files changed, 31 insertions(+), 36 deletions(-)
drivers/xen/gntdev.c +0 -0
index 6a3c9e4..f1fc8fa 100644

drivers/xen/gntdev.c struct granted_page { · line 72

Quoted 8 lines
drivers/xen/gntdev.c +1 -1
@@ -66,16 +66,18 @@ struct granted_page {
 
 struct grant_map {
 	struct list_head next;
-	struct gntdev_priv *priv;
 	struct vm_area_struct *vma;
 	int index;
 	int count;
+	atomic_t users;

Does this need to be atomic? Won't it be happening under spinlock anyway?

Quoted 131 lines
drivers/xen/gntdev.c +18 -29
 	int is_mapped:1;
 	int is_ro:1;
 	struct page **pages;
 	struct granted_page pginfo[0];
 };
 
+static void unmap_grant_pages(struct grant_map *map, int offset, int pages);
+
 /* ------------------------------------------------------------------ */
 
 static void gntdev_print_maps(struct gntdev_priv *priv,
@@ -112,6 +114,7 @@ static struct grant_map *gntdev_alloc_map(int count,
 	}
 
 	add->index = 0;
+	atomic_set(&add->users, 1);
 	add->count = count;
 	for(i = 0; i < count; i++)
 		add->pginfo[i].target = grants[i];
@@ -144,7 +147,6 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 	list_add_tail(&add->next, &priv->maps);
 
 done:
-	add->priv = priv;
 	if (debug)
 		gntdev_print_maps(priv, "[new]", add->index);
 	spin_unlock(&priv->lock);
@@ -165,33 +167,26 @@ static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind
 	return NULL;
 }
 
-static int gntdev_del_map(struct grant_map *map)
+static void gntdev_put_map(struct grant_map *map)
 {
 	int i;
 
-	if (map->vma)
-		return -EBUSY;
-	if (map->is_mapped)
-		for (i = 0; i < map->count; i++)
-			if (map->pginfo[i].handle)
-				return -EBUSY;
+	if (!map)
+		return;
 
-	atomic_sub(map->count, &pages_mapped);
-	list_del(&map->next);
-	return 0;
-}
+	if (!atomic_dec_and_test(&map->users))
+		return;
 
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
+	atomic_sub(map->count, &pages_mapped);
 
-	if (!map)
-		return;
+	if (!use_ptemod)
+		unmap_grant_pages(map, 0, map->count);
 
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
 	}
+	kfree(map->pages);
 	kfree(map);
 }
 
@@ -310,6 +305,7 @@ static void gntdev_vma_close(struct vm_area_struct *vma)
 	map->is_mapped = 0;
 	map->vma = NULL;
 	vma->vm_private_data = NULL;
+	gntdev_put_map(map);
 }
 
 static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
@@ -425,7 +421,6 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 {
 	struct gntdev_priv *priv = flip->private_data;
 	struct grant_map *map;
-	int err;
 
 	if (debug)
 		printk("%s: priv %p\n", __FUNCTION__, priv);
@@ -433,10 +428,8 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 	spin_lock(&priv->lock);
 	while (!list_empty(&priv->maps)) {
 		map = list_entry(priv->maps.next, struct grant_map, next);
-		err = gntdev_del_map(map);
-		if (WARN_ON(err))
-			gntdev_free_map(map);
-
+		list_del(&map->next);
+		gntdev_put_map(map);
 	}
 	spin_unlock(&priv->lock);
 
@@ -487,18 +480,14 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 
 	if (copy_to_user(u, &op, sizeof(op))) {
 		err = -EFAULT;
-		goto out_remove;
+		goto out_free;
 	}
 	err = 0;
 out_free:
 	kfree(grants);
 	return err;
-out_remove:
-	spin_lock(&priv->lock);
-	gntdev_del_map(map);
-	spin_unlock(&priv->lock);
 out_free_map:
-	gntdev_free_map(map);
+	gntdev_put_map(map);
 	goto out_free;
 }
 
@@ -507,7 +496,7 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 {
 	struct ioctl_gntdev_unmap_grant_ref op;
 	struct grant_map *map;
-	int err = -EINVAL;
+	int err;
 
 	if (copy_from_user(&op, u, sizeof(op)) != 0)
 		return -EFAULT;
@@ -517,11 +506,13 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 

drivers/xen/gntdev.c static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, · line 514

Quoted 10 lines
drivers/xen/gntdev.c +6 -2
 	spin_lock(&priv->lock);
 	map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
-	if (map)
-		err = gntdev_del_map(map);
+	if (map) {
+		list_del(&map->next);
+		gntdev_put_map(map);
+		err = 0;
+	} else
+		err = -EINVAL;

What prevents unmap_grant_ref being called multiple times?

Quoted 24 lines
drivers/xen/gntdev.c +3 -3
 	spin_unlock(&priv->lock);
-	if (!err)
-		gntdev_free_map(map);
 	return err;
 }
 
@@ -599,13 +590,15 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	map = gntdev_find_map_index(priv, index, count);
 	if (!map)
 		goto unlock_out;
-	if (map->vma)
+	if (use_ptemod && map->vma)
 		goto unlock_out;
 	if (priv->mm != vma->vm_mm) {
 		printk("%s: Huh? Other mm?\n", __FUNCTION__);
 		goto unlock_out;
 	}
 
+	atomic_inc(&map->users);
+
 	vma->vm_ops = &gntdev_vmops;
 
 	vma->vm_flags |= VM_RESERVED;

drivers/xen/gntdev.c static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) · line 615

Quoted 10 lines
drivers/xen/gntdev.c +3 -1
@@ -614,7 +607,9 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	vma->vm_flags |= VM_PFNMAP;
 
 	vma->vm_private_data = map;
-	map->vma = vma;
+
+	if (use_ptemod)
+		map->vma = vma;
 
 	map->is_ro = !(vma->vm_flags & VM_WRITE);
 

J

#20 lore raw

Re: [PATCH 5/7] xen-gntdev: Add reference counting to maps

Quoted 18 lines · Jeremy Fitzhardinge
Quoted 17 lines · Daniel De Graaf

This allows userspace to perform mmap() on the gntdev device and then immediately close the filehandle or remove the mapping using the remove ioctl, with the mapped area remaining valid until unmapped. This also fixes an infinite loop when a gntdev device is closed without first unmapping all areas.

---
 drivers/xen/gntdev.c |   67 +++++++++++++++++++++++--------------------------
 1 files changed, 31 insertions(+), 36 deletions(-)
drivers/xen/gntdev.c +0 -0
index 6a3c9e4..f1fc8fa 100644
@@ -66,16 +66,18 @@ struct granted_page {
 
Quoted 9 lines

drivers/xen/gntdev.c struct granted_page { · line 72

Quoted 7 lines
drivers/xen/gntdev.c +1 -1
 struct grant_map {
 	struct list_head next;
-	struct gntdev_priv *priv;
 	struct vm_area_struct *vma;
 	int index;
 	int count;
+	atomic_t users;

Does this need to be atomic? Won't it be happening under spinlock anyway?

gntdev_put_map will not be called under spinlock if it is called from an munmap(), especially one that happens after the file is closed.

Quoted 14 lines

drivers/xen/gntdev.c static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, · line 514

Quoted 12 lines
drivers/xen/gntdev.c +6 -2
@@ -517,11 +506,13 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 
 	spin_lock(&priv->lock);
 	map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
-	if (map)
-		err = gntdev_del_map(map);
+	if (map) {
+		list_del(&map->next);
+		gntdev_put_map(map);
+		err = 0;
+	} else
+		err = -EINVAL;

What prevents unmap_grant_ref being called multiple times?

gntdev_find_map_index searches in priv->list for the mapping; if found, it removes it from that list. A second search will just return -EINVAL, even if the pages are still mapped.

Quoted 6 lines
Quoted 6 lines
drivers/xen/gntdev.c +0 -2
 	spin_unlock(&priv->lock);
-	if (!err)
-		gntdev_free_map(map);
 	return err;
 }
 
Quoted 9 lines

drivers/xen/gntdev.c static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) · line 594

Quoted 7 lines
drivers/xen/gntdev.c +1 -1
@@ -599,13 +590,15 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	map = gntdev_find_map_index(priv, index, count);
 	if (!map)
 		goto unlock_out;
-	if (map->vma)
+	if (use_ptemod && map->vma)
 		goto unlock_out;

Does this depend on the later hvm patch?

Whoops, that ended up in the wrong patch. I'll correct the pair.

Quoted 24 lines
Quoted 11 lines
drivers/xen/gntdev.c +2 -0
 	if (priv->mm != vma->vm_mm) {
 		printk("%s: Huh? Other mm?\n", __FUNCTION__);
 		goto unlock_out;
 	}
 
+	atomic_inc(&map->users);
+
 	vma->vm_ops = &gntdev_vmops;
 
 	vma->vm_flags |= VM_RESERVED;

drivers/xen/gntdev.c static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) · line 615

Quoted 10 lines
drivers/xen/gntdev.c +3 -1
@@ -614,7 +607,9 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	vma->vm_flags |= VM_PFNMAP;
 
 	vma->vm_private_data = map;
-	map->vma = vma;
+
+	if (use_ptemod)
+		map->vma = vma;
 
 	map->is_ro = !(vma->vm_flags & VM_WRITE);
 

J

Signature · 2 lines
Daniel De Graaf
National Security Agency
#21 lore raw

Re: [PATCH 5/7] xen-gntdev: Add reference counting to maps

In reply to Daniel De Graaf

drivers/xen/gntdev.c static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind · line 182

Quoted 8 lines · Daniel De Graaf
drivers/xen/gntdev.c +2 -5
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
+	atomic_sub(map->count, &pages_mapped);
 
-	if (!map)
-		return;
+	if (!use_ptemod)

Does this depend on the later hvm patch?

J

#22 lore raw

Re: [PATCH 5/7 v2] xen-gntdev: Add reference counting to maps

In reply to Daniel De Graaf

This allows userspace to perform mmap() on the gntdev device and then immediately close the filehandle or remove the mapping using the remove ioctl, with the mapped area remaining valid until unmapped. This also fixes an infinite loop when a gntdev device is closed without first unmapping all areas.

---
 drivers/xen/gntdev.c |   61 +++++++++++++++++++++----------------------------
 1 files changed, 26 insertions(+), 35 deletions(-)
drivers/xen/gntdev.c +26 -35
index 6a3c9e4..0c8e8a2 100644
@@ -66,16 +66,18 @@ struct granted_page {
 
 struct grant_map {
 	struct list_head next;
-	struct gntdev_priv *priv;
 	struct vm_area_struct *vma;
 	int index;
 	int count;
+	atomic_t users;
 	int is_mapped:1;
 	int is_ro:1;
 	struct page **pages;
 	struct granted_page pginfo[0];
 };
 
+static void unmap_grant_pages(struct grant_map *map, int offset, int pages);
+
 /* ------------------------------------------------------------------ */
 
 static void gntdev_print_maps(struct gntdev_priv *priv,
@@ -112,6 +114,7 @@ static struct grant_map *gntdev_alloc_map(int count,
 	}
 
 	add->index = 0;
+	atomic_set(&add->users, 1);
 	add->count = count;
 	for(i = 0; i < count; i++)
 		add->pginfo[i].target = grants[i];
@@ -144,7 +147,6 @@ static void gntdev_add_map(struct gntdev_priv *priv, struct grant_map *add)
 	list_add_tail(&add->next, &priv->maps);
 
 done:
-	add->priv = priv;
 	if (debug)
 		gntdev_print_maps(priv, "[new]", add->index);
 	spin_unlock(&priv->lock);
@@ -165,33 +167,23 @@ static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind
 	return NULL;
 }
 
-static int gntdev_del_map(struct grant_map *map)
+static void gntdev_put_map(struct grant_map *map)
 {
 	int i;
 
-	if (map->vma)
-		return -EBUSY;
-	if (map->is_mapped)
-		for (i = 0; i < map->count; i++)
-			if (map->pginfo[i].handle)
-				return -EBUSY;
-
-	atomic_sub(map->count, &pages_mapped);
-	list_del(&map->next);
-	return 0;
-}
-
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
-
 	if (!map)
 		return;
 
+	if (!atomic_dec_and_test(&map->users))
+		return;
+
+	atomic_sub(map->count, &pages_mapped);
+
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
 	}
+	kfree(map->pages);
 	kfree(map);
 }
 
@@ -310,6 +302,7 @@ static void gntdev_vma_close(struct vm_area_struct *vma)
 	map->is_mapped = 0;
 	map->vma = NULL;
 	vma->vm_private_data = NULL;
+	gntdev_put_map(map);
 }
 
 static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
@@ -425,7 +418,6 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 {
 	struct gntdev_priv *priv = flip->private_data;
 	struct grant_map *map;
-	int err;
 
 	if (debug)
 		printk("%s: priv %p\n", __FUNCTION__, priv);
@@ -433,10 +425,8 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 	spin_lock(&priv->lock);
 	while (!list_empty(&priv->maps)) {
 		map = list_entry(priv->maps.next, struct grant_map, next);
-		err = gntdev_del_map(map);
-		if (WARN_ON(err))
-			gntdev_free_map(map);
-
+		list_del(&map->next);
+		gntdev_put_map(map);
 	}
 	spin_unlock(&priv->lock);
 
@@ -487,18 +477,14 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
 
 	if (copy_to_user(u, &op, sizeof(op))) {
 		err = -EFAULT;
-		goto out_remove;
+		goto out_free;
 	}
 	err = 0;
 out_free:
 	kfree(grants);
 	return err;
-out_remove:
-	spin_lock(&priv->lock);
-	gntdev_del_map(map);
-	spin_unlock(&priv->lock);
 out_free_map:
-	gntdev_free_map(map);
+	gntdev_put_map(map);
 	goto out_free;
 }
 
@@ -507,7 +493,7 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 {
 	struct ioctl_gntdev_unmap_grant_ref op;
 	struct grant_map *map;
-	int err = -EINVAL;
+	int err;
 
 	if (copy_from_user(&op, u, sizeof(op)) != 0)
 		return -EFAULT;
@@ -517,11 +503,13 @@ static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv,
 
 	spin_lock(&priv->lock);
 	map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count);
-	if (map)
-		err = gntdev_del_map(map);
+	if (map) {
+		list_del(&map->next);
+		gntdev_put_map(map);
+		err = 0;
+	} else
+		err = -EINVAL;
 	spin_unlock(&priv->lock);
-	if (!err)
-		gntdev_free_map(map);
 	return err;
 }
 
@@ -606,6 +594,8 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 		goto unlock_out;
 	}
 
+	atomic_inc(&map->users);
+
 	vma->vm_ops = &gntdev_vmops;
 
 	vma->vm_flags |= VM_RESERVED;
@@ -614,6 +604,7 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	vma->vm_flags |= VM_PFNMAP;
 
 	vma->vm_private_data = map;
+
 	map->vma = vma;
 
 	map->is_ro = !(vma->vm_flags & VM_WRITE);
#23 lore raw

Re: Re: [PATCH 5/7 v2] xen-gntdev: Add reference counting to maps

drivers/xen/gntdev.c static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind · line 170

Quoted 2 lines
drivers/xen/gntdev.c +1 -1
-static int gntdev_del_map(struct grant_map *map)
+static void gntdev_put_map(struct grant_map *map)

Aha.. here it is.

.. snip ..

Quoted 10 lines
drivers/xen/gntdev.c +2 -0
@@ -606,6 +594,8 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 		goto unlock_out;
 	}
 
+	atomic_inc(&map->users);
+
 	vma->vm_ops = &gntdev_vmops;
 
 	vma->vm_flags |= VM_RESERVED;

drivers/xen/gntdev.c static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) · line 607

Quoted 4 lines
drivers/xen/gntdev.c +1 -0
@@ -614,6 +604,7 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	vma->vm_flags |= VM_PFNMAP;
 
 	vma->vm_private_data = map;
+

No need for that.

#24 lore raw

Re: [PATCH 5/7] xen-gntdev: Add reference counting to maps

In reply to Daniel De Graaf
Quoted 5 lines
drivers/xen/gntdev.c +1 -3
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
+	atomic_sub(map->count, &pages_mapped);
 

drivers/xen/gntdev.c static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind · line 189

Quoted 10 lines
drivers/xen/gntdev.c +3 -2
-	if (!map)
-		return;
+	if (!use_ptemod)
+		unmap_grant_pages(map, 0, map->count);
 
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
 	}
+	kfree(map->pages);

Can you roll that in the previous patch that introduced the map->pages code?

drivers/xen/gntdev.c static void gntdev_vma_close(struct vm_area_struct *vma) · line 308

Quoted 8 lines
drivers/xen/gntdev.c +1 -0
 	kfree(map);
 }
@@ -310,6 +305,7 @@ static void gntdev_vma_close(struct vm_area_struct *vma)
 	map->is_mapped = 0;
 	map->vma = NULL;
 	vma->vm_private_data = NULL;
+	gntdev_put_map(map);

I am somehow not seeing this function, nor the use_ptemod defined. Ah, you answered that later on..

#25 lore raw

Re: [PATCH 5/7] xen-gntdev: Add reference counting to maps

Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 10 lines
drivers/xen/gntdev.c +3 -5
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
+	atomic_sub(map->count, &pages_mapped);
 
-	if (!map)
-		return;
+	if (!use_ptemod)
+		unmap_grant_pages(map, 0, map->count);
Quoted 8 lines

drivers/xen/gntdev.c static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind · line 189

Quoted 5 lines
drivers/xen/gntdev.c +1 -0
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
 	}
+	kfree(map->pages);

Can you roll that in the previous patch that introduced the map->pages code?

map->pages is actually introduced by "xen gntdev: use gnttab_map_refs and gnttab_unmap_refs" in my patch series and it already has a kfree(map->pages) in gntdev_free_map.
In fact I think reading this chuck of the patch on its own is misleading because as you can see the whole gntdev_free_map function has been removed...

#26 lore raw

Re: [PATCH 5/7] xen-gntdev: Add reference counting to maps

Quoted 20 lines · Stefano Stabellini
Quoted 10 lines · Konrad Rzeszutek Wilk
Quoted 10 lines
drivers/xen/gntdev.c +3 -5
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
+	atomic_sub(map->count, &pages_mapped);
 
-	if (!map)
-		return;
+	if (!use_ptemod)
+		unmap_grant_pages(map, 0, map->count);
Quoted 8 lines

drivers/xen/gntdev.c static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind · line 189

Quoted 5 lines
drivers/xen/gntdev.c +1 -0
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
 	}
+	kfree(map->pages);

Can you roll that in the previous patch that introduced the map->pages code?

Quoted 2 lines

map->pages is actually introduced by "xen gntdev: use gnttab_map_refs and gnttab_unmap_refs" in my patch series and it already has a

Right. But I meant his patch that collapsed all of the different kzalloc's in just one.

Quoted 4 lines

kfree(map->pages) in gntdev_free_map.
In fact I think reading this chuck of the patch on its own is misleading because as you can see the whole gntdev_free_map function has been removed...

#27 lore raw

Re: [PATCH 5/7] xen-gntdev: Add reference counting to maps

Quoted 21 lines · Konrad Rzeszutek Wilk
Quoted 20 lines · Stefano Stabellini
Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 5 lines
drivers/xen/gntdev.c +1 -3
-static void gntdev_free_map(struct grant_map *map)
-{
-	unsigned i;
+	atomic_sub(map->count, &pages_mapped);
 

drivers/xen/gntdev.c static struct grant_map *gntdev_find_map_index(struct gntdev_priv *priv, int ind · line 189

Quoted 10 lines
drivers/xen/gntdev.c +3 -2
-	if (!map)
-		return;
+	if (!use_ptemod)
+		unmap_grant_pages(map, 0, map->count);
 
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
 	}
+	kfree(map->pages);

Can you roll that in the previous patch that introduced the map->pages code?

Quoted 5 lines
Quoted 2 lines

map->pages is actually introduced by "xen gntdev: use gnttab_map_refs and gnttab_unmap_refs" in my patch series and it already has a

Right. But I meant his patch that collapsed all of the different kzalloc's in just one.

"xen-gntdev: Remove unneeded structures from grant_map tracking data" is a nice cleanup but I'd rather keep it separate from "xen gntdev: use gnttab_map_refs and gnttab_unmap_refs" that introduces a new functionality (the usage of gnttab_map_refs and gnttab_unmap_refs).

But I could import just the bit that collapses all the kzalloc's in one.

#28 lore raw

PATCH 6/7 xen-gntdev: Support mapping in HVM domains

In reply to Daniel De Graaf

HVM does not allow direct PTE modification, so instead we request that Xen change its internal p2m mappings on the allocated pages and map the memory into userspace normally.

---
 drivers/xen/gntdev.c      |  109 +++++++++++++++++++++++++++++++--------------
 drivers/xen/grant-table.c |    7 +++
 2 files changed, 83 insertions(+), 33 deletions(-)
drivers/xen/gntdev.c +76 -33
index f1fc8fa..0985577 100644
@@ -30,6 +30,7 @@
 #include <linux/sched.h>
 #include <linux/spinlock.h>
 #include <linux/slab.h>
+#include <linux/highmem.h>
 
 #include <xen/xen.h>
 #include <xen/grant_table.h>
@@ -49,6 +50,8 @@ static int limit = 1024*1024;
 module_param(limit, int, 0644);
 static atomic_t pages_mapped = ATOMIC_INIT(0);
 
+static int use_ptemod = 0;
+
 struct gntdev_priv {
 	struct list_head maps;
 	spinlock_t lock;
@@ -209,9 +212,12 @@ static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void
 static int map_grant_pages(struct grant_map *map)
 {
 	int i, flags, err = 0;
+	phys_addr_t addr;
 	struct gnttab_map_grant_ref* map_ops = NULL;
 
-	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	flags = GNTMAP_host_map;
+	if (use_ptemod)
+		flags |= GNTMAP_application_map | GNTMAP_contains_pte;
 	if (map->is_ro)
 		flags |= GNTMAP_readonly;
 
@@ -221,7 +227,11 @@ static int map_grant_pages(struct grant_map *map)
 		goto out;
 
 	for(i=0; i < map->count; i++) {
-		gnttab_set_map_op(&map_ops[i], map->pginfo[i].pte_maddr, flags,
+		if (use_ptemod)
+			addr = map->pginfo[i].pte_maddr;
+		else
+			addr = (phys_addr_t)pfn_to_kaddr(page_to_pfn(map->pages[i]));
+		gnttab_set_map_op(&map_ops[i], addr, flags,
 				  map->pginfo[i].target.ref,
 				  map->pginfo[i].target.domid);
 	}
@@ -253,6 +263,7 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 	int i, flags, err = 0;
 	struct gnttab_unmap_grant_ref *unmap_ops;
 	struct gnttab_unmap_grant_ref unmap_single;
+	phys_addr_t addr;
 
 	if (pages > 1) {
 		unmap_ops = kzalloc(sizeof(unmap_ops[0]) * pages,
@@ -266,14 +277,23 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		unmap_ops = &unmap_single;
 	}
 
-	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	flags = GNTMAP_host_map;
+	if (use_ptemod)
+		flags |= GNTMAP_application_map | GNTMAP_contains_pte;
 	if (map->is_ro)
 		flags |= GNTMAP_readonly;
 
-	for(i=0; i < pages; i++)
-		gnttab_set_unmap_op(&unmap_ops[i],
-		                    map->pginfo[offset+i].pte_maddr, flags,
+	for(i=0; i < pages; i++) {
+		if (WARN_ON(!map->pages[i]))
+			continue;
+		if (use_ptemod)
+			addr = map->pginfo[i].pte_maddr;
+		else
+			addr = (phys_addr_t)pfn_to_kaddr(page_to_pfn(map->pages[i]));
+		gnttab_set_unmap_op(&unmap_ops[i], addr, flags,
 		                    map->pginfo[offset+i].handle);
+	}
+
 	if (debug)
 		printk("%s: map %d+%d [%d+%d]\n", __FUNCTION__,
 		       map->index, map->count, offset, pages);
@@ -284,8 +304,25 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		goto out;
 
 	for (i = 0; i < pages; i++) {
+		uint32_t check, *tmp;
 		WARN_ON(unmap_ops[i].status);
-		__free_page(map->pages[offset+i]);
+		if (!map->pages[i])
+			continue;
+		/* XXX When unmapping, Xen will sometimes end up mapping the GFN
+		 * to an invalid MFN. In this case, writes will be discarded and
+		 * reads will return all 0xFF bytes. Leak these unusable GFNs
+		 * until a way to restore them is found.
+		 */
+		tmp = kmap(map->pages[i]);
+		tmp[0] = 0xdeaddead;
+		mb();
+		check = tmp[0];
+		kunmap(map->pages[i]);
+		if (check == 0xdeaddead)
+			__free_page(map->pages[i]);
+		else if (debug)
+			printk("%s: Discard page %d=%ld\n", __func__,
+				i, page_to_pfn(map->pages[i]));
 		map->pages[offset+i] = NULL;
 		map->pginfo[offset+i].handle = 0;
 	}
@@ -308,18 +345,8 @@ static void gntdev_vma_close(struct vm_area_struct *vma)
 	gntdev_put_map(map);
 }
 
-static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
-{
-	if (debug)
-		printk("%s: vaddr %p, pgoff %ld (shouldn't happen)\n",
-		       __FUNCTION__, vmf->virtual_address, vmf->pgoff);
-	vmf->flags = VM_FAULT_ERROR;
-	return 0;
-}
-
 static struct vm_operations_struct gntdev_vmops = {
 	.close = gntdev_vma_close,
-	.fault = gntdev_vma_fault,
 };
 
 /* ------------------------------------------------------------------ */
@@ -401,14 +428,16 @@ static int gntdev_open(struct inode *inode, struct file *flip)
 	INIT_LIST_HEAD(&priv->maps);
 	spin_lock_init(&priv->lock);
 
-	priv->mm = get_task_mm(current);
-	if (!priv->mm) {
-		kfree(priv);
-		return -ENOMEM;
+	if (use_ptemod) {
+		priv->mm = get_task_mm(current);
+		if (!priv->mm) {
+			kfree(priv);
+			return -ENOMEM;
+		}
+		priv->mn.ops = &gntdev_mmu_ops;
+		mmu_notifier_register(&priv->mn, priv->mm);
+		mmput(priv->mm);
 	}
-	priv->mn.ops = &gntdev_mmu_ops;
-	mmu_notifier_register(&priv->mn, priv->mm);
-	mmput(priv->mm);
 
 	flip->private_data = priv;
 	if (debug)
@@ -433,7 +462,8 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 	}
 	spin_unlock(&priv->lock);
 
-	mmu_notifier_unregister(&priv->mn, priv->mm);
+	if (use_ptemod)
+		mmu_notifier_unregister(&priv->mn, priv->mm);
 	kfree(priv);
 	return 0;
 }
@@ -577,7 +607,7 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	int index = vma->vm_pgoff;
 	int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
 	struct grant_map *map;
-	int err = -EINVAL;
+	int i, err = -EINVAL;
 
 	if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
 		return -EINVAL;
@@ -592,7 +622,7 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 		goto unlock_out;
 	if (use_ptemod && map->vma)
 		goto unlock_out;
-	if (priv->mm != vma->vm_mm) {
+	if (use_ptemod && priv->mm != vma->vm_mm) {
 		printk("%s: Huh? Other mm?\n", __FUNCTION__);
 		goto unlock_out;
 	}
@@ -615,18 +645,29 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 
 	spin_unlock(&priv->lock);
 
-	err = apply_to_page_range(vma->vm_mm, vma->vm_start,
-				  vma->vm_end - vma->vm_start,
-				  find_grant_ptes, map);
-	if (err)
-		return err;
+	if (use_ptemod) {
+		err = apply_to_page_range(vma->vm_mm, vma->vm_start,
+					  vma->vm_end - vma->vm_start,
+					  find_grant_ptes, map);
+		if (err)
+			return err;
+	}
 
 	err = map_grant_pages(map);
 	if (err)
 		return err;
-
+	
 	map->is_mapped = 1;
 
+	if (!use_ptemod) {
+		for(i = 0; i < count; i++) {
+			err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
+			                     map->pages[i]);
+			if (err)
+				return err;
+		}
+	}
+
 	return 0;
 
 unlock_out:
@@ -657,6 +698,8 @@ static int __init gntdev_init(void)
 	if (!xen_domain())
 		return -ENODEV;
 
+	use_ptemod = xen_pv_domain();
+
 	err = misc_register(&gntdev_miscdev);
 	if (err != 0) {
 		printk(KERN_ERR "Could not register gntdev device\n");
drivers/xen/grant-table.c +7 -0
index a5cf820..c8ab76e 100644
@@ -457,6 +457,9 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 
 	ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
 
+	if (xen_feature(XENFEAT_auto_translated_physmap))
+		return ret;
+
 	for (i = 0; i < count; i++) {
 		pfn = mfn_to_pfn(map_ops[i].host_addr >> PAGE_SHIFT);
 		pte = (pte_t *) __va((pfn << PAGE_SHIFT) +
@@ -476,6 +479,10 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 	int i, ret;
 
 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
+
+	if (xen_feature(XENFEAT_auto_translated_physmap))
+		return ret;
+
 	for (i = 0; i < count; i++)
 		m2p_remove_override(pages[i]);
 
Signature · 1 line
1.7.2.3
#29 lore raw

Re: [PATCH 6/7 v2] xen-gntdev: Support mapping in HVM domains

HVM does not allow direct PTE modification, so instead we request that Xen change its internal p2m mappings on the allocated pages and map the memory into userspace normally.

---
 drivers/xen/gntdev.c      |  117 +++++++++++++++++++++++++++++++-------------
 drivers/xen/grant-table.c |    7 +++
 2 files changed, 89 insertions(+), 35 deletions(-)
drivers/xen/gntdev.c +82 -35
index 0c8e8a2..0985577 100644
@@ -30,6 +30,7 @@
 #include <linux/sched.h>
 #include <linux/spinlock.h>
 #include <linux/slab.h>
+#include <linux/highmem.h>
 
 #include <xen/xen.h>
 #include <xen/grant_table.h>
@@ -49,6 +50,8 @@ static int limit = 1024*1024;
 module_param(limit, int, 0644);
 static atomic_t pages_mapped = ATOMIC_INIT(0);
 
+static int use_ptemod = 0;
+
 struct gntdev_priv {
 	struct list_head maps;
 	spinlock_t lock;
@@ -179,6 +182,9 @@ static void gntdev_put_map(struct grant_map *map)
 
 	atomic_sub(map->count, &pages_mapped);
 
+	if (!use_ptemod)
+		unmap_grant_pages(map, 0, map->count);
+
 	for (i = 0; i < map->count; i++) {
 		if (map->pages[i])
 			__free_page(map->pages[i]);
@@ -206,9 +212,12 @@ static int find_grant_ptes(pte_t *pte, pgtable_t token, unsigned long addr, void
 static int map_grant_pages(struct grant_map *map)
 {
 	int i, flags, err = 0;
+	phys_addr_t addr;
 	struct gnttab_map_grant_ref* map_ops = NULL;
 
-	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	flags = GNTMAP_host_map;
+	if (use_ptemod)
+		flags |= GNTMAP_application_map | GNTMAP_contains_pte;
 	if (map->is_ro)
 		flags |= GNTMAP_readonly;
 
@@ -218,7 +227,11 @@ static int map_grant_pages(struct grant_map *map)
 		goto out;
 
 	for(i=0; i < map->count; i++) {
-		gnttab_set_map_op(&map_ops[i], map->pginfo[i].pte_maddr, flags,
+		if (use_ptemod)
+			addr = map->pginfo[i].pte_maddr;
+		else
+			addr = (phys_addr_t)pfn_to_kaddr(page_to_pfn(map->pages[i]));
+		gnttab_set_map_op(&map_ops[i], addr, flags,
 				  map->pginfo[i].target.ref,
 				  map->pginfo[i].target.domid);
 	}
@@ -250,6 +263,7 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 	int i, flags, err = 0;
 	struct gnttab_unmap_grant_ref *unmap_ops;
 	struct gnttab_unmap_grant_ref unmap_single;
+	phys_addr_t addr;
 
 	if (pages > 1) {
 		unmap_ops = kzalloc(sizeof(unmap_ops[0]) * pages,
@@ -263,14 +277,23 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		unmap_ops = &unmap_single;
 	}
 
-	flags = GNTMAP_host_map | GNTMAP_application_map | GNTMAP_contains_pte;
+	flags = GNTMAP_host_map;
+	if (use_ptemod)
+		flags |= GNTMAP_application_map | GNTMAP_contains_pte;
 	if (map->is_ro)
 		flags |= GNTMAP_readonly;
 
-	for(i=0; i < pages; i++)
-		gnttab_set_unmap_op(&unmap_ops[i],
-		                    map->pginfo[offset+i].pte_maddr, flags,
+	for(i=0; i < pages; i++) {
+		if (WARN_ON(!map->pages[i]))
+			continue;
+		if (use_ptemod)
+			addr = map->pginfo[i].pte_maddr;
+		else
+			addr = (phys_addr_t)pfn_to_kaddr(page_to_pfn(map->pages[i]));
+		gnttab_set_unmap_op(&unmap_ops[i], addr, flags,
 		                    map->pginfo[offset+i].handle);
+	}
+
 	if (debug)
 		printk("%s: map %d+%d [%d+%d]\n", __FUNCTION__,
 		       map->index, map->count, offset, pages);
@@ -281,8 +304,25 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		goto out;
 
 	for (i = 0; i < pages; i++) {
+		uint32_t check, *tmp;
 		WARN_ON(unmap_ops[i].status);
-		__free_page(map->pages[offset+i]);
+		if (!map->pages[i])
+			continue;
+		/* XXX When unmapping, Xen will sometimes end up mapping the GFN
+		 * to an invalid MFN. In this case, writes will be discarded and
+		 * reads will return all 0xFF bytes. Leak these unusable GFNs
+		 * until a way to restore them is found.
+		 */
+		tmp = kmap(map->pages[i]);
+		tmp[0] = 0xdeaddead;
+		mb();
+		check = tmp[0];
+		kunmap(map->pages[i]);
+		if (check == 0xdeaddead)
+			__free_page(map->pages[i]);
+		else if (debug)
+			printk("%s: Discard page %d=%ld\n", __func__,
+				i, page_to_pfn(map->pages[i]));
 		map->pages[offset+i] = NULL;
 		map->pginfo[offset+i].handle = 0;
 	}
@@ -305,18 +345,8 @@ static void gntdev_vma_close(struct vm_area_struct *vma)
 	gntdev_put_map(map);
 }
 
-static int gntdev_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
-{
-	if (debug)
-		printk("%s: vaddr %p, pgoff %ld (shouldn't happen)\n",
-		       __FUNCTION__, vmf->virtual_address, vmf->pgoff);
-	vmf->flags = VM_FAULT_ERROR;
-	return 0;
-}
-
 static struct vm_operations_struct gntdev_vmops = {
 	.close = gntdev_vma_close,
-	.fault = gntdev_vma_fault,
 };
 
 /* ------------------------------------------------------------------ */
@@ -398,14 +428,16 @@ static int gntdev_open(struct inode *inode, struct file *flip)
 	INIT_LIST_HEAD(&priv->maps);
 	spin_lock_init(&priv->lock);
 
-	priv->mm = get_task_mm(current);
-	if (!priv->mm) {
-		kfree(priv);
-		return -ENOMEM;
+	if (use_ptemod) {
+		priv->mm = get_task_mm(current);
+		if (!priv->mm) {
+			kfree(priv);
+			return -ENOMEM;
+		}
+		priv->mn.ops = &gntdev_mmu_ops;
+		mmu_notifier_register(&priv->mn, priv->mm);
+		mmput(priv->mm);
 	}
-	priv->mn.ops = &gntdev_mmu_ops;
-	mmu_notifier_register(&priv->mn, priv->mm);
-	mmput(priv->mm);
 
 	flip->private_data = priv;
 	if (debug)
@@ -430,7 +462,8 @@ static int gntdev_release(struct inode *inode, struct file *flip)
 	}
 	spin_unlock(&priv->lock);
 
-	mmu_notifier_unregister(&priv->mn, priv->mm);
+	if (use_ptemod)
+		mmu_notifier_unregister(&priv->mn, priv->mm);
 	kfree(priv);
 	return 0;
 }
@@ -574,7 +607,7 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	int index = vma->vm_pgoff;
 	int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
 	struct grant_map *map;
-	int err = -EINVAL;
+	int i, err = -EINVAL;
 
 	if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED))
 		return -EINVAL;
@@ -587,9 +620,9 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 	map = gntdev_find_map_index(priv, index, count);
 	if (!map)
 		goto unlock_out;
-	if (map->vma)
+	if (use_ptemod && map->vma)
 		goto unlock_out;
-	if (priv->mm != vma->vm_mm) {
+	if (use_ptemod && priv->mm != vma->vm_mm) {
 		printk("%s: Huh? Other mm?\n", __FUNCTION__);
 		goto unlock_out;
 	}
@@ -605,24 +638,36 @@ static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma)
 
 	vma->vm_private_data = map;
 
-	map->vma = vma;
+	if (use_ptemod)
+		map->vma = vma;
 
 	map->is_ro = !(vma->vm_flags & VM_WRITE);
 
 	spin_unlock(&priv->lock);
 
-	err = apply_to_page_range(vma->vm_mm, vma->vm_start,
-				  vma->vm_end - vma->vm_start,
-				  find_grant_ptes, map);
-	if (err)
-		return err;
+	if (use_ptemod) {
+		err = apply_to_page_range(vma->vm_mm, vma->vm_start,
+					  vma->vm_end - vma->vm_start,
+					  find_grant_ptes, map);
+		if (err)
+			return err;
+	}
 
 	err = map_grant_pages(map);
 	if (err)
 		return err;
-
+	
 	map->is_mapped = 1;
 
+	if (!use_ptemod) {
+		for(i = 0; i < count; i++) {
+			err = vm_insert_page(vma, vma->vm_start + i*PAGE_SIZE,
+			                     map->pages[i]);
+			if (err)
+				return err;
+		}
+	}
+
 	return 0;
 
 unlock_out:
@@ -653,6 +698,8 @@ static int __init gntdev_init(void)
 	if (!xen_domain())
 		return -ENODEV;
 
+	use_ptemod = xen_pv_domain();
+
 	err = misc_register(&gntdev_miscdev);
 	if (err != 0) {
 		printk(KERN_ERR "Could not register gntdev device\n");
drivers/xen/grant-table.c +7 -0
index a5cf820..c8ab76e 100644
@@ -457,6 +457,9 @@ int gnttab_map_refs(struct gnttab_map_grant_ref *map_ops,
 
 	ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map_ops, count);
 
+	if (xen_feature(XENFEAT_auto_translated_physmap))
+		return ret;
+
 	for (i = 0; i < count; i++) {
 		pfn = mfn_to_pfn(map_ops[i].host_addr >> PAGE_SHIFT);
 		pte = (pte_t *) __va((pfn << PAGE_SHIFT) +
@@ -476,6 +479,10 @@ int gnttab_unmap_refs(struct gnttab_unmap_grant_ref *unmap_ops,
 	int i, ret;
 
 	ret = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap_ops, count);
+
+	if (xen_feature(XENFEAT_auto_translated_physmap))
+		return ret;
+
 	for (i = 0; i < count; i++)
 		m2p_remove_override(pages[i]);
#30 lore raw

Re: [PATCH 6/7] xen-gntdev: Support mapping in HVM domains

In reply to Daniel De Graaf
Quoted 14 lines
drivers/xen/gntdev.c +8 -1
@@ -284,8 +304,25 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		goto out;
 
 	for (i = 0; i < pages; i++) {
+		uint32_t check, *tmp;
 		WARN_ON(unmap_ops[i].status);
-		__free_page(map->pages[offset+i]);
+		if (!map->pages[i])
+			continue;
+		/* XXX When unmapping, Xen will sometimes end up mapping the GFN
+		 * to an invalid MFN. In this case, writes will be discarded and
+		 * reads will return all 0xFF bytes. Leak these unusable GFNs
+		 * until a way to restore them is found.
+		 */

drivers/xen/gntdev.c static void unmap_grant_pages(struct grant_map *map, int offset, int pages) · line 325

Quoted 10 lines
drivers/xen/gntdev.c +10 -0
+		tmp = kmap(map->pages[i]);
+		tmp[0] = 0xdeaddead;
+		mb();
+		check = tmp[0];
+		kunmap(map->pages[i]);
+		if (check == 0xdeaddead)
+			__free_page(map->pages[i]);
+		else if (debug)
+			printk("%s: Discard page %d=%ld\n", __func__,
+				i, page_to_pfn(map->pages[i]));

Whoa. Any leads to when the "sometimes" happens? Does the status report an error or is it silent?

Quoted 3 lines
drivers/xen/gntdev.c +0 -0
 		map->pages[offset+i] = NULL;
 		map->pginfo[offset+i].handle = 0;
 	}
#31 lore raw

Re: [PATCH 6/7] xen-gntdev: Support mapping in HVM domains

Quoted 19 lines · Konrad Rzeszutek Wilk
Quoted 19 lines
drivers/xen/gntdev.c +13 -1
@@ -284,8 +304,25 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		goto out;
 
 	for (i = 0; i < pages; i++) {
+		uint32_t check, *tmp;
 		WARN_ON(unmap_ops[i].status);
-		__free_page(map->pages[offset+i]);
+		if (!map->pages[i])
+			continue;
+		/* XXX When unmapping, Xen will sometimes end up mapping the GFN
+		 * to an invalid MFN. In this case, writes will be discarded and
+		 * reads will return all 0xFF bytes. Leak these unusable GFNs
+		 * until a way to restore them is found.
+		 */
+		tmp = kmap(map->pages[i]);
+		tmp[0] = 0xdeaddead;
+		mb();
+		check = tmp[0];
+		kunmap(map->pages[i]);
Quoted 8 lines

drivers/xen/gntdev.c static void unmap_grant_pages(struct grant_map *map, int offset, int pages) · line 325

Quoted 5 lines
drivers/xen/gntdev.c +5 -0
+		if (check == 0xdeaddead)
+			__free_page(map->pages[i]);
+		else if (debug)
+			printk("%s: Discard page %d=%ld\n", __func__,
+				i, page_to_pfn(map->pages[i]));

Whoa. Any leads to when the "sometimes" happens? Does the status report an error or is it silent?

Status is silent in this case. I can produce it quite reliably on my test system where I am mapping a framebuffer (1280 pages) between two HVM guests - in this case, about 2/3 of the released pages will end up being invalid. It doesn't seem to be size-related as I have also seen it on the small 3-page page index mapping. There is a message on xm dmesg that may be related:

(XEN) sh error: sh_remove_all_mappings(): can't find all mappings of mfn 7cbc6: c=8000000000000004 t=7400000000000002

This appears about once per page, with different MFNs but the same c/t. One of the two HVM guests (the one doing the mapping) has the PCI graphics card forwarded to it.

Quoted 4 lines
Quoted 3 lines
drivers/xen/gntdev.c +0 -0
 		map->pages[offset+i] = NULL;
 		map->pginfo[offset+i].handle = 0;
 	}
#32 lore raw

Re: [PATCH 6/7] xen-gntdev: Support mapping in HVM domains

Quoted 36 lines · Daniel De Graaf
Quoted 19 lines · Konrad Rzeszutek Wilk
Quoted 19 lines
drivers/xen/gntdev.c +13 -1
@@ -284,8 +304,25 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		goto out;
 
 	for (i = 0; i < pages; i++) {
+		uint32_t check, *tmp;
 		WARN_ON(unmap_ops[i].status);
-		__free_page(map->pages[offset+i]);
+		if (!map->pages[i])
+			continue;
+		/* XXX When unmapping, Xen will sometimes end up mapping the GFN
+		 * to an invalid MFN. In this case, writes will be discarded and
+		 * reads will return all 0xFF bytes. Leak these unusable GFNs
+		 * until a way to restore them is found.
+		 */
+		tmp = kmap(map->pages[i]);
+		tmp[0] = 0xdeaddead;
+		mb();
+		check = tmp[0];
+		kunmap(map->pages[i]);
Quoted 8 lines

drivers/xen/gntdev.c static void unmap_grant_pages(struct grant_map *map, int offset, int pages) · line 325

Quoted 5 lines
drivers/xen/gntdev.c +5 -0
+		if (check == 0xdeaddead)
+			__free_page(map->pages[i]);
+		else if (debug)
+			printk("%s: Discard page %d=%ld\n", __func__,
+				i, page_to_pfn(map->pages[i]));

Whoa. Any leads to when the "sometimes" happens? Does the status report an error or is it silent?

Status is silent in this case. I can produce it quite reliably on my test system where I am mapping a framebuffer (1280 pages) between two HVM guests - in this case, about 2/3 of the released pages will end up being invalid. It doesn't seem to be size-related as I have also seen it on the small 3-page page index mapping. There is a message on xm dmesg that may be related:

Quoted 6 lines

(XEN) sh error: sh_remove_all_mappings(): can't find all mappings of mfn 7cbc6: c=8000000000000004 t=7400000000000002

This appears about once per page, with different MFNs but the same c/t. One of the two HVM guests (the one doing the mapping) has the PCI graphics card forwarded to it.

Just tested on the latest xen 4.1 (with 22402:7d2fdc083c9c reverted as that breaks HVM grants), which produces different output:

...
(XEN) mm.c:889:d1 Error getting mfn b803e (pfn 25a3e) from L1 entry 00000000b803e021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn b8038 (pfn 25a38) from L1 entry 00000000b8038021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn b803d (pfn 25a3d) from L1 entry 00000000b803d021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 10829 (pfn 25a29) from L1 entry 0000000010829021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 1081c (pfn 25a1c) from L1 entry 000000001081c021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 10816 (pfn 25a16) from L1 entry 0000000010816021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 1081a (pfn 25a1a) from L1 entry 000000001081a021 for l1e_owner=1, pg_owner=1
...

This appears on the map; nothing is printed on the unmap. If the unmap happens while the domain is up, it seems to be invalid more often; most (perhaps all) of the destination-valid unmaps happen when the domain is being destroyed. Exactly which pages are valid or invalid seems to be mostly random, although nearby GFNs tend to have the same validity.

If you have any thoughts as to the cause, I can test patches or provide output as needed; it would be better if this workaround weren't needed.

Signature · 2 lines
Daniel De Graaf
National Security Agency
#33 lore raw

c/s 22402 ("86 hvm: Refuse to perform __hvm_copy() work in atomic context.") breaks HVM, race possible in other code - any ideas?

Quoted 39 lines · Daniel De Graaf
Quoted 38 lines · Daniel De Graaf
Quoted 19 lines · Konrad Rzeszutek Wilk
Quoted 19 lines
drivers/xen/gntdev.c +13 -1
@@ -284,8 +304,25 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		goto out;
 
 	for (i = 0; i < pages; i++) {
+		uint32_t check, *tmp;
 		WARN_ON(unmap_ops[i].status);
-		__free_page(map->pages[offset+i]);
+		if (!map->pages[i])
+			continue;
+		/* XXX When unmapping, Xen will sometimes end up mapping the GFN
+		 * to an invalid MFN. In this case, writes will be discarded and
+		 * reads will return all 0xFF bytes. Leak these unusable GFNs
+		 * until a way to restore them is found.
+		 */
+		tmp = kmap(map->pages[i]);
+		tmp[0] = 0xdeaddead;
+		mb();
+		check = tmp[0];
+		kunmap(map->pages[i]);
Quoted 8 lines

drivers/xen/gntdev.c static void unmap_grant_pages(struct grant_map *map, int offset, int pages) · line 325

Quoted 5 lines
drivers/xen/gntdev.c +5 -0
+		if (check == 0xdeaddead)
+			__free_page(map->pages[i]);
+		else if (debug)
+			printk("%s: Discard page %d=%ld\n", __func__,
+				i, page_to_pfn(map->pages[i]));

Whoa. Any leads to when the "sometimes" happens? Does the status report an error or is it silent?

Status is silent in this case. I can produce it quite reliably on my test system where I am mapping a framebuffer (1280 pages) between two HVM guests - in this case, about 2/3 of the released pages will end up being invalid. It doesn't seem to be size-related as I have also seen it on the small 3-page page index mapping. There is a message on xm dmesg that may be related:

(XEN) sh error: sh_remove_all_mappings(): can't find all mappings of mfn 7cbc6: c=8000000000000004 t=7400000000000002

Quoted 7 lines
Quoted 4 lines

This appears about once per page, with different MFNs but the same c/t. One of the two HVM guests (the one doing the mapping) has the PCI graphics card forwarded to it.

Just tested on the latest xen 4.1 (with 22402:7d2fdc083c9c reverted as that breaks HVM grants), which produces different output:

Keir, the c/s 22402 has your name on it.

Any ideas on the problem that Daniel is hitting with unmapping grants?

Quoted 23 lines

...
(XEN) mm.c:889:d1 Error getting mfn b803e (pfn 25a3e) from L1 entry 00000000b803e021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn b8038 (pfn 25a38) from L1 entry 00000000b8038021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn b803d (pfn 25a3d) from L1 entry 00000000b803d021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 10829 (pfn 25a29) from L1 entry 0000000010829021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 1081c (pfn 25a1c) from L1 entry 000000001081c021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 10816 (pfn 25a16) from L1 entry 0000000010816021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 1081a (pfn 25a1a) from L1 entry 000000001081a021 for l1e_owner=1, pg_owner=1
...

This appears on the map; nothing is printed on the unmap. If the unmap happens while the domain is up, it seems to be invalid more often; most (perhaps all) of the destination-valid unmaps happen when the domain is being destroyed. Exactly which pages are valid or invalid seems to be mostly random, although nearby GFNs tend to have the same validity.

If you have any thoughts as to the cause, I can test patches or provide output as needed; it would be better if this workaround weren't needed.

Signature · 2 lines
Daniel De Graaf
National Security Agency
#34 lore raw

Re: c/s 22402 ("86 hvm: Refuse to perform __hvm_copy() work in atomic context.") breaks HVM, race possible in other code - any ideas?

Quoted 45 lines · Konrad Rzeszutek Wilk
Quoted 44 lines · Daniel De Graaf
Quoted 42 lines · Daniel De Graaf
Quoted 19 lines · Konrad Rzeszutek Wilk
Quoted 19 lines
drivers/xen/gntdev.c +13 -1
@@ -284,8 +304,25 @@ static void unmap_grant_pages(struct grant_map *map, int offset, int pages)
 		goto out;
 
 	for (i = 0; i < pages; i++) {
+		uint32_t check, *tmp;
 		WARN_ON(unmap_ops[i].status);
-		__free_page(map->pages[offset+i]);
+		if (!map->pages[i])
+			continue;
+		/* XXX When unmapping, Xen will sometimes end up mapping the GFN
+		 * to an invalid MFN. In this case, writes will be discarded and
+		 * reads will return all 0xFF bytes. Leak these unusable GFNs
+		 * until a way to restore them is found.
+		 */
+		tmp = kmap(map->pages[i]);
+		tmp[0] = 0xdeaddead;
+		mb();
+		check = tmp[0];
+		kunmap(map->pages[i]);
Quoted 8 lines

drivers/xen/gntdev.c static void unmap_grant_pages(struct grant_map *map, int offset, int pages) · line 325

Quoted 5 lines
drivers/xen/gntdev.c +5 -0
+		if (check == 0xdeaddead)
+			__free_page(map->pages[i]);
+		else if (debug)
+			printk("%s: Discard page %d=%ld\n", __func__,
+				i, page_to_pfn(map->pages[i]));

Whoa. Any leads to when the "sometimes" happens? Does the status report an error or is it silent?

Status is silent in this case. I can produce it quite reliably on my test system where I am mapping a framebuffer (1280 pages) between two HVM guests - in this case, about 2/3 of the released pages will end up being invalid. It doesn't seem to be size-related as I have also seen it on the small 3-page page index mapping. There is a message on xm dmesg that may be related:

(XEN) sh error: sh_remove_all_mappings(): can't find all mappings of mfn 7cbc6: c=8000000000000004 t=7400000000000002

This appears about once per page, with different MFNs but the same c/t. One of the two HVM guests (the one doing the mapping) has the PCI graphics card forwarded to it.

Quoted 6 lines
Quoted 2 lines

Just tested on the latest xen 4.1 (with 22402:7d2fdc083c9c reverted as that breaks HVM grants), which produces different output:

Keir, the c/s 22402 has your name on it.

Any ideas on the problem that Daniel is hitting with unmapping grants?

c/s 22402 was discussed at the beginning of December:
http://lists.xensource.com/archives/html/xen-devel/2010-12/msg00063.html

Since I don't use xenpaging (which is the reason for the change), this revert shouldn't be relevant to the problems I am seeing.

Quoted 22 lines
Quoted 22 lines

...
(XEN) mm.c:889:d1 Error getting mfn b803e (pfn 25a3e) from L1 entry 00000000b803e021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn b8038 (pfn 25a38) from L1 entry 00000000b8038021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn b803d (pfn 25a3d) from L1 entry 00000000b803d021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 10829 (pfn 25a29) from L1 entry 0000000010829021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 1081c (pfn 25a1c) from L1 entry 000000001081c021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 10816 (pfn 25a16) from L1 entry 0000000010816021 for l1e_owner=1, pg_owner=1 (XEN) mm.c:889:d1 Error getting mfn 1081a (pfn 25a1a) from L1 entry 000000001081a021 for l1e_owner=1, pg_owner=1
...

This appears on the map; nothing is printed on the unmap. If the unmap happens while the domain is up, it seems to be invalid more often; most (perhaps all) of the destination-valid unmaps happen when the domain is being destroyed. Exactly which pages are valid or invalid seems to be mostly random, although nearby GFNs tend to have the same validity.

If you have any thoughts as to the cause, I can test patches or provide output as needed; it would be better if this workaround weren't needed.

Signature · 2 lines
Daniel De Graaf
National Security Agency
#35 lore raw

PATCH 7/7 xen-gntalloc: Userspace grant allocation driver

In reply to Daniel De Graaf

This allows a userspace application to allocate a shared page for implementing inter-domain communication or device drivers. These shared pages can be mapped using the gntdev device or by the kernel in another domain.

---
 drivers/xen/Kconfig    |    7 +
 drivers/xen/Makefile   |    2 +
 drivers/xen/gntalloc.c |  488 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/xen/gntalloc.h |   70 +++++++
 4 files changed, 567 insertions(+), 0 deletions(-)
 create mode 100644 drivers/xen/gntalloc.c
 create mode 100644 include/xen/gntalloc.h
drivers/xen/Kconfig +7 -0
index 0c6d2a1..677ccad 100644
@@ -69,6 +69,13 @@ config XEN_GNTDEV
 	help
 	  Allows userspace processes use grants.
 	  
+config XEN_GRANT_DEV_ALLOC
+	tristate "User-space grant reference allocator driver"
+	depends on XEN
+	help
+	  Allows userspace processes to create pages with access granted
+	  to other domains.
+
 config XEN_PLATFORM_PCI
 	tristate "xen platform pci device driver"
 	depends on XEN_PVHVM
drivers/xen/Makefile +2 -0
index 674fdb5..ec38a72 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_XEN_XENCOMM)	+= xencomm.o
 obj-$(CONFIG_XEN_BALLOON)	+= balloon.o
 obj-$(CONFIG_XEN_DEV_EVTCHN)	+= xen-evtchn.o
 obj-$(CONFIG_XEN_GNTDEV)	+= xen-gntdev.o
+obj-$(CONFIG_XEN_GRANT_DEV_ALLOC)	+= xen-gntalloc.o
 obj-$(CONFIG_XENFS)		+= xenfs/
 obj-$(CONFIG_XEN_SYS_HYPERVISOR)	+= sys-hypervisor.o
 obj-$(CONFIG_XEN_PLATFORM_PCI)	+= platform-pci.o
@@ -18,4 +19,5 @@ obj-$(CONFIG_XEN_DOM0)		+= pci.o
 
 xen-evtchn-y			:= evtchn.o
 xen-gntdev-y				:= gntdev.o
+xen-gntalloc-y				:= gntalloc.o
 
drivers/xen/gntalloc.c new file +488 -0

488 lines not on this page · raw message

include/xen/gntalloc.h new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#36 lore raw
In reply to Daniel De Graaf
Quoted 8 lines · Daniel De Graaf

Rebased on top of Stefano's 2.6.37 gntdev patches; included suggestions from the v2 patch queue. HVM mapping is now much cleaner, since pages with real GFNs are available. Multicall API didn't make it in, because the actual hypercall is within a function that I'm not sure should be changed to multicall in all instances, and I haven't had a chance to figure out how to use it despite this.

[PATCH 1/7] xen-gntdev: Fix circular locking dependency

Thanks for the fix and for doing the rebase; I have imported this patch in my series.

#37 lore raw
In reply to Daniel De Graaf
Quoted 1 line · Daniel De Graaf

Rebased on top of Stefano's 2.6.37 gntdev patches; included suggestions

I was wondering whether you would be OK rebasing this on top of Linus latest tree? (or if you prefer you could use mine:
devel/next-2.6.38) and re-posting them?

2.6.38 has now the gntdev driver along with one of your patches.

Quoted 18 lines

from the v2 patch queue. HVM mapping is now much cleaner, since pages with real GFNs are available. Multicall API didn't make it in, because the actual hypercall is within a function that I'm not sure should be changed to multicall in all instances, and I haven't had a chance to figure out how to use it despite this.

[PATCH 1/7] xen-gntdev: Fix circular locking dependency
[PATCH 2/7] xen-gntdev: Change page limit to be global instead of per-open [PATCH 3/7] xen-gntdev: Remove unneeded structures from grant_map tracking data [PATCH 4/7] xen-gntdev: Use find_vma rather than iterating our vma list manually [PATCH 5/7] xen-gntdev: Add reference counting to maps
[PATCH 6/7] xen-gntdev: Support mapping in HVM domains
[PATCH 7/7] xen-gntalloc: Userspace grant allocation driver

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel