Skip to contentSkip to conversation

Invalid P2M entries after gnttab unmap

PATCH In review Daniel De Graaf 3 participants 7 messages Mar 4, 2011 – Mar 5, 2011 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

When an HVM guest uses gnttab_map_grant_ref to map granted on top of valid GFNs, it appears that the original MFNs referred to by these GFNs are lost. The unmap operation sets the p2m mapping of the GFN to INVALID_MFN (and it appears that replace_grant_p2m_mapping will not accept valid MFNs).

Most of the time, this appears to be true in testing. However, I have noticed a few cases where the GFNs are valid following the unmap operation. This has happened when a large number of grants (1284) are being unmapped due to a domain shutdown; in this case, perhaps half of the unmapped GFNs will point to valid memory, and half will point to invalid memory. In this case, "invalid memory" discards writes and returns 0xFF on all reads; valid memory appears to be normal RAM.

There doesn't appear to be any documentation on the intended behavior here.

Quoted 1 line

From the guest kernel's perspective, it makes the most sense for GFNs that

pointed to RAM prior to the map operation to continue to point to RAM after the unmap operation - that is, the unmap fully undoes what the map did. The contents of the pages (and which exact MFN they point to) aren't important.

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

Hi,

Quoted 2 lines · Daniel De Graaf

When an HVM guest uses gnttab_map_grant_ref to map granted on top of valid GFNs, it appears that the original MFNs referred to by these GFNs are lost.

Yes. The p2m table only holds one MFN for each PFN (and vice versa). If you want to keep that memory you could move it somewhere else using XENMAPSPACE_gmfn, or just map your grant refs into an MMIO hole.

Quoted 1 line

The unmap operation sets the p2m mapping of the GFN to INVALID_MFN.

There's nothing else it can set it to, really, if you take away the thing that was there.

Quoted 4 lines

(and it appears that replace_grant_p2m_mapping will not accept valid MFNs).

Most of the time, this appears to be true in testing. However, I have noticed a few cases where the GFNs are valid following the unmap operation.

That seems like a bug.

Quoted 2 lines

This has happened when a large number of grants (1284) are being unmapped due to a domain shutdown;

Can you be more specific? Do you mean that a domain that has mapped grants into its p2m is shutting down in a controlled way, and is pulling out all the grant mappings as it does so? What hypercall does it use to unmap the grants?

Cheers,

Tim.

Quoted 20 lines

in this case, perhaps half of the unmapped GFNs
will point to valid memory, and half will point to invalid memory. In this case, "invalid memory" discards writes and returns 0xFF on all reads; valid memory appears to be normal RAM.

There doesn't appear to be any documentation on the intended behavior here.

Quoted 1 line

From the guest kernel's perspective, it makes the most sense for GFNs that

pointed to RAM prior to the map operation to continue to point to RAM after the unmap operation - that is, the unmap fully undoes what the map did. The contents of the pages (and which exact MFN they point to) aren't important.

Signature · 8 lines
Daniel De Graaf
National Security Agency


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#2 lore raw
Quoted 9 lines · Tim Deegan

Hi,

Quoted 2 lines · Daniel De Graaf

When an HVM guest uses gnttab_map_grant_ref to map granted on top of valid GFNs, it appears that the original MFNs referred to by these GFNs are lost.

Yes. The p2m table only holds one MFN for each PFN (and vice versa). If you want to keep that memory you could move it somewhere else using XENMAPSPACE_gmfn,

In which case you might as well do the grant map to "somewhere else" I guess?

Quoted 1 line

or just map your grant refs into an MMIO hole.

The platform-pci device has a BAR for this sort of purpose, doesn't it? Mostly it just gets used for the grant table itself and perhaps it isn't large enough to be a suitable source of mapping space.

Is there some reason the gntdev driver can't just balloon down (XENMEM_decrease_reservation) to make itself a space to map and then balloon back up (XENMEM_increase_reservation) after unmap when running HVM?

Quoted 4 lines
Quoted 4 lines

in this case, perhaps half of the unmapped GFNs
will point to valid memory, and half will point to invalid memory. In this case, "invalid memory" discards writes and returns 0xFF on all reads; valid memory appears to be normal RAM.

The workaround relies entirely on this discard and read 0xff behaviour, which I'm not sure is wise.

I'm not especially happy about the idea of 2.6.39 getting released into the wild with this hack in it. Luckily there is plenty of time to fix the issue properly before then.

Ian.

#6 lore raw
In reply to Tim Deegan
Quoted 10 lines · Tim Deegan

Hi,

Quoted 2 lines · Daniel De Graaf

When an HVM guest uses gnttab_map_grant_ref to map granted on top of valid GFNs, it appears that the original MFNs referred to by these GFNs are lost.

Yes. The p2m table only holds one MFN for each PFN (and vice versa). If you want to keep that memory you could move it somewhere else using XENMAPSPACE_gmfn, or just map your grant refs into an MMIO hole.

[addressed in reply to Ian's mail]

Quoted 12 lines
Quoted 1 line

The unmap operation sets the p2m mapping of the GFN to INVALID_MFN.

There's nothing else it can set it to, really, if you take away the thing that was there.

Quoted 4 lines

(and it appears that replace_grant_p2m_mapping will not accept valid MFNs).

Most of the time, this appears to be true in testing. However, I have noticed a few cases where the GFNs are valid following the unmap operation.

That seems like a bug.

Quoted 7 lines
Quoted 2 lines

This has happened when a large number of grants (1284) are being unmapped due to a domain shutdown;

Can you be more specific? Do you mean that a domain that has mapped grants into its p2m is shutting down in a controlled way, and is pulling out all the grant mappings as it does so? What hypercall does it use to unmap the grants?

I produced this in the following test scenario:

Domain 1 (HVM) is a display server that uses gntdev to map the vfb device from domain 2 (also HVM, using fbfront). Domain 2 was shut down using xm destroy, which triggered (via xenstore) domain 1 to unmap all of the grants. I think I also observed this with a normal shutdown of domain 2.

Quoted 19 lines

Cheers,

Tim.

Quoted 15 lines

in this case, perhaps half of the unmapped GFNs
will point to valid memory, and half will point to invalid memory. In this case, "invalid memory" discards writes and returns 0xFF on all reads; valid memory appears to be normal RAM.

There doesn't appear to be any documentation on the intended behavior here.

Quoted 1 line

From the guest kernel's perspective, it makes the most sense for GFNs that

pointed to RAM prior to the map operation to continue to point to RAM after the unmap operation - that is, the unmap fully undoes what the map did. The contents of the pages (and which exact MFN they point to) aren't important.

Signature · 2 lines
Daniel De Graaf
National Security Agency
#3 lore raw
In reply to Ian Campbell
Quoted 7 lines · Ian Campbell
Quoted 6 lines · Tim Deegan

Hi,

Quoted 2 lines · Daniel De Graaf

When an HVM guest uses gnttab_map_grant_ref to map granted on top of valid GFNs, it appears that the original MFNs referred to by these GFNs are lost.

Quoted 6 lines
Quoted 3 lines

Yes. The p2m table only holds one MFN for each PFN (and vice versa). If you want to keep that memory you could move it somewhere else using XENMAPSPACE_gmfn,

In which case you might as well do the grant map to "somewhere else" I guess?

Yes, remapping seems to be useless unless there's a "somewhere else" that isn't usable for normal memory access.

Quoted 10 lines
Quoted 1 line

or just map your grant refs into an MMIO hole.

The platform-pci device has a BAR for this sort of purpose, doesn't it? Mostly it just gets used for the grant table itself and perhaps it isn't large enough to be a suitable source of mapping space.

Is there some reason the gntdev driver can't just balloon down (XENMEM_decrease_reservation) to make itself a space to map and then balloon back up (XENMEM_increase_reservation) after unmap when running HVM?

I recall having problems with doing this last time, but I think other changes to make the balloon driver work in HVM may have fixed the issue. I'll try it again; I think this would be the best solution.

It may be useful to integrate with the balloon driver and use some of the pages that have already been ballooned down, rather than forcing the map to be a two- step process. This would also make handling a failure to balloon back up far easier. This would add a dependency on the balloon module and a new interface that allows requesting/returning ballooned pages.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

in this case, perhaps half of the unmapped GFNs
will point to valid memory, and half will point to invalid memory. In this case, "invalid memory" discards writes and returns 0xFF on all reads; valid memory appears to be normal RAM.

Quoted 9 lines

The workaround relies entirely on this discard and read 0xff behaviour, which I'm not sure is wise.

I'm not especially happy about the idea of 2.6.39 getting released into the wild with this hack in it. Luckily there is plenty of time to fix the issue properly before then.

Ian.

Agreed, it would be best to avoid this workaround since the intended behavior of unmapping is to produce invalid PFNs (I had written it assuming they were intended to be valid). Using the ballooning hypercalls should fix this.

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

PATCH 1/1 xen-gntdev: Use XENMEM_populate_physmap on unmapped pages in HVM

Quoted 8 lines
Quoted 4 lines

Is there some reason the gntdev driver can't just balloon down (XENMEM_decrease_reservation) to make itself a space to map and then balloon back up (XENMEM_increase_reservation) after unmap when running HVM?

I recall having problems with doing this last time, but I think other changes to make the balloon driver work in HVM may have fixed the issue. I'll try it again; I think this would be the best solution.

Using the balloon hypercalls does work in my test. Below is a patch that uses them directly in gntdev, which is simpler but doesn't handle errors in re-ballooning and doesn't take advantage of already-ballooned pages from the balloon device. Taking advantage of the balloon device is also possible and would be more efficient, but adds another dependency to gntdev. Any opinions on which method is preferred? If not, I'll try making a patch to implement the balloon-assisted version.

8<-------------------------------------------------------------------------

Grant mappings cause the PFN<->MFN mapping to be lost on the pages used for the mapping. Instead of leaking memory, explicitly free it to the hypervisor and request the memory back after unmapping. This removes the need for the bad-page leak workaround.

---
 drivers/xen/gntdev.c |   58 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 39 insertions(+), 19 deletions(-)
drivers/xen/gntdev.c +39 -19
index d43ff30..104098a 100644
@@ -41,6 +41,7 @@
 #include <asm/xen/hypervisor.h>
 #include <asm/xen/hypercall.h>
 #include <asm/xen/page.h>
+#include <xen/interface/memory.h>
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Derek G. Murray <[email protected]>, "
@@ -203,27 +204,9 @@ static void gntdev_put_map(struct grant_map *map)
 			unmap_grant_pages(map, 0, map->count);
 
 		for (i = 0; i < map->count; i++) {
-			uint32_t check, *tmp;
 			if (!map->pages[i])
 				continue;
-			/* XXX When unmapping in an HVM domain, 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 Xen supports fixing their p2m mapping.
-			 *
-			 * Confirmed present in Xen 4.1-RC3 with HVM source
-			 */
-			tmp = kmap(map->pages[i]);
-			*tmp = 0xdeaddead;
-			mb();
-			check = *tmp;
-			kunmap(map->pages[i]);
-			if (check == 0xdeaddead)
-				__free_page(map->pages[i]);
-			else
-				pr_debug("Discard page %d=%ld\n", i,
-					page_to_pfn(map->pages[i]));
+			__free_page(map->pages[i]);
 		}
 	}
 	kfree(map->pages);
@@ -258,11 +241,19 @@ static int map_grant_pages(struct grant_map *map)
 {
 	int i, err = 0;
 	phys_addr_t addr;
+	unsigned long frame;
 
 	if (!use_ptemod) {
+		struct xen_memory_reservation reservation = {
+			.address_bits = 0,
+			.extent_order = 0,
+			.nr_extents   = 1,
+			.domid        = DOMID_SELF
+		};
 		/* Note: it could already be mapped */
 		if (map->map_ops[0].handle != -1)
 			return 0;
+		set_xen_guest_handle(reservation.extent_start, &frame);
 		for (i = 0; i < map->count; i++) {
 			addr = (phys_addr_t)
 				pfn_to_kaddr(page_to_pfn(map->pages[i]));
@@ -271,6 +262,12 @@ static int map_grant_pages(struct grant_map *map)
 				map->grants[i].domid);
 			gnttab_set_unmap_op(&map->unmap_ops[i], addr,
 				map->flags, -1 /* handle */);
+			/* TODO batch hypercall, needs buffer */
+			frame = page_to_pfn(map->pages[i]);
+			err = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
+				&reservation);
+			if (WARN_ON(err <= 0))
+				printk(KERN_INFO "memop returned %d\n", err);
 		}
 	}
 
@@ -324,6 +321,29 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 			map->unmap_ops[offset+i].status);
 		map->unmap_ops[offset+i].handle = -1;
 	}
+
+	if (!use_ptemod) {
+		struct xen_memory_reservation reservation = {
+			.address_bits = 0,
+			.extent_order = 0,
+			.nr_extents   = 1,
+			.domid        = DOMID_SELF
+		};
+		int rc;
+		unsigned long frame;
+		set_xen_guest_handle(reservation.extent_start, &frame);
+		/* TODO batch hypercall, needs buffer */
+		for (i = offset; i < pages + offset; i++) {
+			frame = page_to_pfn(map->pages[i]);
+			rc = HYPERVISOR_memory_op(XENMEM_populate_physmap,
+				&reservation);
+			if (WARN_ON(rc <= 0)) {
+				map->pages[i] = NULL;
+				continue;
+			}
+		}
+	}
+
 	return err;
 }
 
Signature · 1 line
1.7.3.4
#5 lore raw

Re: [RFC/PATCH v1] xen-gntdev: Use XENMEM_populate_physmap on unmapped pages in HVM

Quoted 9 lines · Daniel De Graaf
Quoted 8 lines
Quoted 4 lines

Is there some reason the gntdev driver can't just balloon down (XENMEM_decrease_reservation) to make itself a space to map and then balloon back up (XENMEM_increase_reservation) after unmap when running HVM?

I recall having problems with doing this last time, but I think other changes to make the balloon driver work in HVM may have fixed the issue. I'll try it again; I think this would be the best solution.

Quoted 7 lines

Using the balloon hypercalls does work in my test. Below is a patch that uses them directly in gntdev, which is simpler but doesn't handle errors in re-ballooning and doesn't take advantage of already-ballooned pages from the balloon device. Taking advantage of the balloon device is also possible and would be more efficient, but adds another dependency to gntdev. Any opinions on which method is preferred? If not, I'll try making a patch to implement the balloon-assisted version.

The balloon driver in the 2.6.32 pvops (and all old-style Xen kernels too) has interfaces which are used by the backend drivers for exactly this purpose.

It was always a bit of a wart that the backends depended on the balloon driver in this way. IMHO the core ballooning functionality and kernel interfaces should be moved into the core kernel under CONFIG_XEN. All that should remain under CONFIG_XEN_BALLOON is the sysfs and xenbus integration e.g. the user/admin visible interfaces.

This is somewhat analogous to drivers/xen/{events,grant-table}.c being core kernel and drivers/xen/{gnt,evt}dev.c being configurable optional modules.

It's possible that what remains under CONFIG_XEN_BALLOON is such a tiny amount of code that it's not worth keeping it as a separate option, although the "depends SYSFS" aspect may make it more worthwhile.

Ian.

Quoted 117 lines

8<-------------------------------------------------------------------------

Grant mappings cause the PFN<->MFN mapping to be lost on the pages used for the mapping. Instead of leaking memory, explicitly free it to the hypervisor and request the memory back after unmapping. This removes the need for the bad-page leak workaround.

---
 drivers/xen/gntdev.c |   58 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 39 insertions(+), 19 deletions(-)
drivers/xen/gntdev.c +39 -19
index d43ff30..104098a 100644
@@ -41,6 +41,7 @@
 #include <asm/xen/hypervisor.h>
 #include <asm/xen/hypercall.h>
 #include <asm/xen/page.h>
+#include <xen/interface/memory.h>
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Derek G. Murray <[email protected]>, "
@@ -203,27 +204,9 @@ static void gntdev_put_map(struct grant_map *map)
 			unmap_grant_pages(map, 0, map->count);
 
 		for (i = 0; i < map->count; i++) {
-			uint32_t check, *tmp;
 			if (!map->pages[i])
 				continue;
-			/* XXX When unmapping in an HVM domain, 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 Xen supports fixing their p2m mapping.
-			 *
-			 * Confirmed present in Xen 4.1-RC3 with HVM source
-			 */
-			tmp = kmap(map->pages[i]);
-			*tmp = 0xdeaddead;
-			mb();
-			check = *tmp;
-			kunmap(map->pages[i]);
-			if (check == 0xdeaddead)
-				__free_page(map->pages[i]);
-			else
-				pr_debug("Discard page %d=%ld\n", i,
-					page_to_pfn(map->pages[i]));
+			__free_page(map->pages[i]);
 		}
 	}
 	kfree(map->pages);
@@ -258,11 +241,19 @@ static int map_grant_pages(struct grant_map *map)
 {
 	int i, err = 0;
 	phys_addr_t addr;
+	unsigned long frame;
 
 	if (!use_ptemod) {
+		struct xen_memory_reservation reservation = {
+			.address_bits = 0,
+			.extent_order = 0,
+			.nr_extents   = 1,
+			.domid        = DOMID_SELF
+		};
 		/* Note: it could already be mapped */
 		if (map->map_ops[0].handle != -1)
 			return 0;
+		set_xen_guest_handle(reservation.extent_start, &frame);
 		for (i = 0; i < map->count; i++) {
 			addr = (phys_addr_t)
 				pfn_to_kaddr(page_to_pfn(map->pages[i]));
@@ -271,6 +262,12 @@ static int map_grant_pages(struct grant_map *map)
 				map->grants[i].domid);
 			gnttab_set_unmap_op(&map->unmap_ops[i], addr,
 				map->flags, -1 /* handle */);
+			/* TODO batch hypercall, needs buffer */
+			frame = page_to_pfn(map->pages[i]);
+			err = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
+				&reservation);
+			if (WARN_ON(err <= 0))
+				printk(KERN_INFO "memop returned %d\n", err);
 		}
 	}
 
@@ -324,6 +321,29 @@ static int __unmap_grant_pages(struct grant_map *map, int offset, int pages)
 			map->unmap_ops[offset+i].status);
 		map->unmap_ops[offset+i].handle = -1;
 	}
+
+	if (!use_ptemod) {
+		struct xen_memory_reservation reservation = {
+			.address_bits = 0,
+			.extent_order = 0,
+			.nr_extents   = 1,
+			.domid        = DOMID_SELF
+		};
+		int rc;
+		unsigned long frame;
+		set_xen_guest_handle(reservation.extent_start, &frame);
+		/* TODO batch hypercall, needs buffer */
+		for (i = offset; i < pages + offset; i++) {
+			frame = page_to_pfn(map->pages[i]);
+			rc = HYPERVISOR_memory_op(XENMEM_populate_physmap,
+				&reservation);
+			if (WARN_ON(rc <= 0)) {
+				map->pages[i] = NULL;
+				continue;
+			}
+		}
+	}
+
 	return err;
 }