Skip to contentSkip to conversation

linux_gntshr_munmap: munmap takes a length, not a page count

PATCH Superseded David Scott 5 participants 12 messages Sep 1, 2014 – Sep 10, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux_gntshr_munmap: munmap takes a length, not a page count

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

---
 tools/libxc/xc_linux_osdep.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +1 -1
index 86bff3e..a19e4b6 100644
@@ -847,7 +847,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 static int linux_gntshr_munmap(xc_gntshr *xcg, xc_osdep_handle h,
                                void *start_address, uint32_t count)
 {
-    return munmap(start_address, count);
+    return munmap(start_address, count * XC_PAGE_SIZE);
 }
 
 static struct xc_osdep_ops linux_gntshr_ops = {
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

Great catch!

Quoted 19 lines
 tools/libxc/xc_linux_osdep.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +1 -1
index 86bff3e..a19e4b6 100644
@@ -847,7 +847,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 static int linux_gntshr_munmap(xc_gntshr *xcg, xc_osdep_handle h,
                                void *start_address, uint32_t count)
 {
-    return munmap(start_address, count);
+    return munmap(start_address, count * XC_PAGE_SIZE);
 }
 
 static struct xc_osdep_ops linux_gntshr_ops = {
Signature · 1 line
1.7.10.4
#2 lore raw
In reply to David Scott
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

So I don't think this change is complete without also updating those.

Ian.

Quoted 19 lines
---
 tools/libxc/xc_linux_osdep.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +1 -1
index 86bff3e..a19e4b6 100644
@@ -847,7 +847,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 static int linux_gntshr_munmap(xc_gntshr *xcg, xc_osdep_handle h,
                                void *start_address, uint32_t count)
 {
-    return munmap(start_address, count);
+    return munmap(start_address, count * XC_PAGE_SIZE);
 }
 
 static struct xc_osdep_ops linux_gntshr_ops = {
#3 lore raw
Quoted 11 lines · Ian Campbell
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

So I don't think this change is complete without also updating those.

Would the corrent non-ABI/API-breaking fix be to just update the documentation?

David

#4 lore raw
Quoted 11 lines · David Vrabel
Quoted 10 lines · Ian Campbell
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

Quoted 4 lines
Quoted 1 line

So I don't think this change is complete without also updating those.

Would the corrent non-ABI/API-breaking fix be to just update the documentation?

libxc doesn't have a stable API or ABI.

My thinking in this case was that @count being pages was consistent with other related functions in the API and also avoids issues of what to do if count%4096 != 0.

Ian.

#5 lore raw
Quoted 12 lines · Ian Campbell
Quoted 11 lines · David Vrabel
Quoted 10 lines · Ian Campbell
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

So I don't think this change is complete without also updating those.

Would the corrent non-ABI/API-breaking fix be to just update the documentation?

libxc doesn't have a stable API or ABI.

xc_gntshr_*() really should be part of a separate libxc-for-domu that does have a stable ABI. But since it isn't yet...

Quoted 3 lines

My thinking in this case was that @count being pages was consistent with other related functions in the API and also avoids issues of what to do if count%4096 != 0.

...this sounds reasonable.

David

#6 lore raw
Quoted 15 lines · David Vrabel
Quoted 14 lines · Ian Campbell
Quoted 13 lines · David Vrabel
Quoted 11 lines · Ian Campbell
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

So I don't think this change is complete without also updating those.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Would the corrent non-ABI/API-breaking fix be to just update the documentation?

libxc doesn't have a stable API or ABI.

xc_gntshr_*() really should be part of a separate libxc-for-domu that does have a stable ABI.

This hadn't occurred to me, but yes, this does sound sensible.

Quoted 7 lines

But since it isn't yet...

Quoted 3 lines

My thinking in this case was that @count being pages was consistent with other related functions in the API and also avoids issues of what to do if count%4096 != 0.

...this sounds reasonable.

Thanks.

Ian

#7 lore raw
Quoted 13 lines · Ian Campbell
Quoted 12 lines · David Vrabel
Quoted 11 lines · Ian Campbell
Quoted 10 lines · David Vrabel
Quoted 9 lines · Ian Campbell
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

So I don't think this change is complete without also updating those.

Would the corrent non-ABI/API-breaking fix be to just update the documentation?

libxc doesn't have a stable API or ABI.

xc_gntshr_*() really should be part of a separate libxc-for-domu that does have a stable ABI.

This hadn't occurred to me, but yes, this does sound sensible.

Currently, all domU event and grant operations require libxc, including the use of libxenstore.

I have raised this before, and believe there is a line item defered until 4.6 to cleave libxc into several pieces, to avoid needing to put libxc into all domUs to use the xenstore-* binaries. libxenevent and libxengrant seem like plausible library names.

~Andrew

author
#8 lore raw
In reply to Ian Campbell
Quoted 9 lines · Ian Campbell
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

Good point.

Quoted 1 line

So I don't think this change is complete without also updating those.

I’ll resubmit with the in-tree callers fixed.

Thanks,
Dave

Quoted 24 lines

Ian.

Quoted 19 lines

---
tools/libxc/xc_linux_osdep.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +1 -1
index 86bff3e..a19e4b6 100644
@@ -847,7 +847,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 static int linux_gntshr_munmap(xc_gntshr *xcg, xc_osdep_handle h,
                               void *start_address, uint32_t count)
 {
-    return munmap(start_address, count);
+    return munmap(start_address, count * XC_PAGE_SIZE);
 }
 
 static struct xc_osdep_ops linux_gntshr_ops = {
author
#9 lore raw
Quoted 13 lines · David Scott
Quoted 9 lines · Ian Campbell
Quoted 3 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

The doc comment on xc_gntshr_munmap does say count is in pages, so your change is correct but all of the in tree callers seem to pass a number of bytes not a number of pages. i.e. everything in tools/libvchan passes n*PAGE_SIZE.

Quoted 5 lines

Good point.

Quoted 1 line

So I don't think this change is complete without also updating those.

I’ll resubmit with the in-tree callers fixed.

Also it looks like xc_gnttab_munmap (gnttab not gntshr) uses pages in both the API and implementation, but libvchan is supplying bytes. I’ll fix vchan here too.

Dave

Quoted 29 lines

Thanks,
Dave

Quoted 24 lines

Ian.

Quoted 19 lines

---
tools/libxc/xc_linux_osdep.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxc/xc_linux_osdep.c +1 -1
index 86bff3e..a19e4b6 100644
@@ -847,7 +847,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 static int linux_gntshr_munmap(xc_gntshr *xcg, xc_osdep_handle h,
                              void *start_address, uint32_t count)
 {
-    return munmap(start_address, count);
+    return munmap(start_address, count * XC_PAGE_SIZE);
 }
 
 static struct xc_osdep_ops linux_gntshr_ops = {
#10 lore raw

PATCH v2 1/1 linux_gntshr_munmap: munmap takes a length, not a page count

In reply to David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

This patch also fixes in-tree callers of
- xc_gntshr_munmap
- xc_gnttab_munmap
to supply page counts rather than lengths.

---
 tools/libvchan/init.c        |    8 ++++----
 tools/libvchan/io.c          |    4 ++--
 tools/libxc/xc_linux_osdep.c |    2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
tools/libvchan/init.c +4 -4
index c080a1c..de10817 100644
@@ -129,9 +129,9 @@ out:
 	return ring_ref;
 out_unmap_left:
 	if (pages_left)
-		xc_gntshr_munmap(ctrl->gntshr, ctrl->read.buffer, pages_left * PAGE_SIZE);
+		xc_gntshr_munmap(ctrl->gntshr, ctrl->read.buffer, pages_left);
 out_ring:
-	xc_gntshr_munmap(ctrl->gntshr, ring, PAGE_SIZE);
+	xc_gntshr_munmap(ctrl->gntshr, ring, 1);
 	ring_ref = -1;
 	ctrl->ring = NULL;
 	ctrl->write.order = ctrl->read.order = 0;
@@ -204,9 +204,9 @@ static int init_gnt_cli(struct libxenvchan *ctrl, int domain, uint32_t ring_ref)
  out_unmap_left:
 	if (ctrl->write.order >= PAGE_SHIFT)
 		xc_gnttab_munmap(ctrl->gnttab, ctrl->write.buffer,
-		                 1 << ctrl->write.order);
+		                 1 << (ctrl->write.order - PAGE_SHIFT));
  out_unmap_ring:
-	xc_gnttab_munmap(ctrl->gnttab, ctrl->ring, PAGE_SIZE);
+	xc_gnttab_munmap(ctrl->gnttab, ctrl->ring, 1);
 	ctrl->ring = 0;
 	ctrl->write.order = ctrl->read.order = 0;
 	rv = -1;
tools/libvchan/io.c +2 -2
index 6e6e239..e66bc4e 100644
@@ -365,10 +365,10 @@ void libxenvchan_close(struct libxenvchan *ctrl)
 	if (ctrl->ring) {
 		if (ctrl->is_server) {
 			ctrl->ring->srv_live = 0;
-			xc_gntshr_munmap(ctrl->gntshr, ctrl->ring, PAGE_SIZE);
+			xc_gntshr_munmap(ctrl->gntshr, ctrl->ring, 1);
 		} else {
 			ctrl->ring->cli_live = 0;
-			xc_gnttab_munmap(ctrl->gnttab, ctrl->ring, PAGE_SIZE);
+			xc_gnttab_munmap(ctrl->gnttab, ctrl->ring, 1);
 		}
 	}
 	if (ctrl->event) {
tools/libxc/xc_linux_osdep.c +1 -1
index 86bff3e..a19e4b6 100644
@@ -847,7 +847,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 static int linux_gntshr_munmap(xc_gntshr *xcg, xc_osdep_handle h,
                                void *start_address, uint32_t count)
 {
-    return munmap(start_address, count);
+    return munmap(start_address, count * XC_PAGE_SIZE);
 }
 
 static struct xc_osdep_ops linux_gntshr_ops = {
Signature · 1 line
1.7.10.4
#11 lore raw
Quoted 10 lines · David Scott

This fixes a bug where if a client shares more than 1 page, the munmap call fails to clean up everything. A process which does a lot of sharing and unsharing can run out of resources.

This patch also fixes in-tree callers of
- xc_gntshr_munmap
- xc_gnttab_munmap
to supply page counts rather than lengths.

Acked + applied, thanks.