Begin to disentangle libxenctrl and provide some stable libraries
Subsystems (2)
Re: [PATCH QEMU-XEN v6 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
In reply to Andrew CooperQuoted 35 lines · Andrew Cooper
Quoted 28 lines · Ian Campbell
Quoted 10 lines · Ian Campbell
hw/display/xenfb.c +0
-0@@ -104,9 +104,8 @@ static int common_bind(struct common *c)if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 1 line
xendev.remote_port) == -1)
return -1;
- c->page = xc_map_foreign_range(xen_xc,c->xendev.dom,hw/display/xenfb.c
Quoted 4 lines
hw/display/xenfb.c +2
-2- XC_PAGE_SIZE, - PROT_READ | PROT_WRITE, mfn); + c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, + PROT_READ | PROT_WRITE, &mfn, 1);This doesn't build for i386 userspace, since mfn is a
uint64_tbutxc_map_foreign_pages()wants axen_pfn_t* (wherexen_pfn_t== unsigned long on x86).Until now that was just a truncation which was already checked for with:
uint64_t mfn;
if (
xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) return -1;
assert(mfn == (xen_pfn_t)mfn);Quoted 5 lines
I think in principal passing "(
xen_pfn_t*)&mfn" would ok (since it is a
singleton array in this case), but I was thinking of going a bit further
and:
Quoted 3 lines
It is never ok to convert a pointer like this. In 32bit (little endian) userspace, it will leave the upper half of mfn uninitialised on the stack.
mfn is a 32-bit value on such systems, so there is no upper half any way.
NB I was talking about passing to xc_map_..., not the call to xenstore_read_fe...
In any case my preference is the more long winded way I had further down.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [Qemu-devel] [Xen-devel] [PATCH QEMU-XEN v6 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
In reply to Andrew CooperQuoted 35 lines · Andrew Cooper
Quoted 28 lines · Ian Campbell
Quoted 10 lines · Ian Campbell
hw/display/xenfb.c +0
-0@@ -104,9 +104,8 @@ static int common_bind(struct common *c)if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 1 line
xendev.remote_port) == -1)
return -1;
- c->page = xc_map_foreign_range(xen_xc,c->xendev.dom,hw/display/xenfb.c
Quoted 4 lines
hw/display/xenfb.c +2
-2- XC_PAGE_SIZE, - PROT_READ | PROT_WRITE, mfn); + c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, + PROT_READ | PROT_WRITE, &mfn, 1);This doesn't build for i386 userspace, since mfn is a
uint64_tbutxc_map_foreign_pages()wants axen_pfn_t* (wherexen_pfn_t== unsigned long on x86).Until now that was just a truncation which was already checked for with:
uint64_t mfn;
if (
xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) return -1;
assert(mfn == (xen_pfn_t)mfn);Quoted 5 lines
I think in principal passing "(
xen_pfn_t*)&mfn" would ok (since it is a
singleton array in this case), but I was thinking of going a bit further
and:
Quoted 3 lines
It is never ok to convert a pointer like this. In 32bit (little endian) userspace, it will leave the upper half of mfn uninitialised on the stack.
mfn is a 32-bit value on such systems, so there is no upper half any way.
NB I was talking about passing to xc_map_..., not the call to xenstore_read_fe...
In any case my preference is the more long winded way I had further down.
Ian.
Re: [PATCH XEN v6 16/32] tools: Refactor foreign memory mapping into libxenforeignmemory
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 16/32] tools: Refactor foreign memory mapping into libxenforeignmemory"):
Quoted 2 lines
libxenforeignmemory will provide a stable API and ABI for mapping foreign domain memory (subject to appropriate privileges).
Re: [PATCH XEN v6 17/32] tools/libs/foreignmemory: provide xenforeignmemory_unmap.
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 17/32] tools/libs/foreignmemory: provide xenforeignmemory_unmap."):
Quoted 1 line
And require it be used instead of direct munmap.
Re: [PATCH XEN v6 20/32] tools/libs/foreignmemory: Support err == NULL to map.
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 20/32] tools/libs/foreignmemory: Support err == NULL to map."):
Quoted 3 lines
The existing xc_map_foreign_bulk-like interface encourages callers to miss error checking for partial failure (by forgetting to scan the err array).
I have not reviewed the implemenation but I very much like the new API spec.
Ian.
Re: [PATCH XEN v6 21/32] tools/libs/foreignmemory: pull array length argument to map forward
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 21/32] tools/libs/foreignmemory: pull array length argument to map forward"):
Quoted 7 lines
By having the "num" argument before the page and error arrays we can potentially use a variable-length-array argument ("int pages[num]") in the function prototype.
However VLAs are a C99 feature and we are currently targetting C89 and later, so we don't actually make use of this here, merely arrange that we can switch to VLAs in the future without changing the function ABI.
Re: [PATCH XEN v6 22/32] tools/libs/foreignmemory: optimise map(num==1, err==NULL) case
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 22/32] tools/libs/foreignmemory: optimise map(num==1, err==NULL) case"):
Quoted 3 lines
This looks to be a reasonably common case (many of the previous callers to
xc_map_foreign_pagesuse it) and it is easy enough to avoid a malloc for it.
...
Quoted 2 lines
I'm not 100% sure this is worth the complexity, hence not folding into the base patch yet, it could easily be dropped.
I would suggest that we should park this until we have a benchmark showing it's helpful.
Ian.
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation."):
Quoted 52 lines
In particular around error handling, behaviour on fork and the unmap notification mechanism.
Behaviour of xengnttab_map_*grant_refs and
xengntshr_share_pageson partial failure has been confirmed/inferred (by inspection) on Linux and Mini-os (the only two known implementations. Likewise the behaviour of the notification mechanism has been confirmed/inferred (by inspection) of the Linux implementation (currently the only implementation) and libvchan (primary known user).These updates are not folded into "tools: Refactor
/dev/xen/gnt{dev,shr}wrappers into libxengnttab." to try and reduce the amount of non-movement changes in that patch.While I'm not convinced by javadoc/doxygen cause the existing comments which appear to use that syntax to have the appropriate /** marker.
Also fix a typo in a code comment.
Signed-off-by Ian Campbell <[email protected]>Cc Daniel De Graaf <[email protected]>---
Daniel, you input on the description of the unmap notification stuff would be much appreciated.v6: Rewrapped a comment.
Incorported much review from Ian on the API, retitled (was:
"tools/libs/gnttab: Review and update doc comments.") and dropped acks--- tools/libs/gnttab/include/xengnttab.h | 166 ++++++++++++++++++++++++++++------ tools/libs/gnttab/linux.c | 12 ++- 2 files changed, 147 insertions(+), 31 deletions(-)tools/libs/gnttab/include/xengnttab.h +10
-0@@ -31,28 +31,111 @@typedef struct xentoollog_logger xentoollog_logger ;/* +* PRODUCING AND CONSUMING GRANT REFERENCES +* ======================================== +* +* The xengnttab library contains two distinct interfaces, each with +* their own distinct handle type and entry points. The represent the +* two sides of the grant table interface, producer (gntshr) and +* consumer (gnttab). +* +* The xengnttab_* interfaces take a xengnttab_handle and provide +* mechanisms for consuming (i.e. mapping or copying to/from) grant
tools/libs/gnttab/include/xengnttab.h line 53
Quoted 10 lines
tools/libs/gnttab/include/xengnttab.h +10
-0+* references provided by a peer. +* +* The xengntshr_* interfaces take a xengntshr_handle and provide a +* mechanism to produce grantable memory and grant references to that +* memory, which can be handed to some peer. +* +* UNMAP NOTIFICATION +* ================== +* +* The xengnt{tab,shr}_*_nofity interfaces implement a best effort
^^^^^^
I would say cooperative' rather than best effort' (the latter implying it may fail due to kernel or hypervisor trouble, rather than malice).
tools/libs/gnttab/include/xengnttab.h line 56
Quoted 3 lines
tools/libs/gnttab/include/xengnttab.h +3
-0+* interface which is intended to allow the underlying kernel +* interfaces to attempt a graceful teardown of the peer upon failure +* (i.e. crash or exit) of the process on their end.
I would write ... to attempt to notify the peer to perform graceful teardown ...' rather than to attempt graceful teardown of the peer'.
tools/libs/gnttab/include/xengnttab.h line 85
Quoted 2 lines
tools/libs/gnttab/include/xengnttab.h +2
-0+* Depending on the mechanisms which have been registered an +* application may receive a shutdown notification as:
I would continue to use the peer' to avoid swapping the point of view. So Depending ... the peer may receive ...'
tools/libs/gnttab/include/xengnttab.h line 97
Quoted 3 lines
tools/libs/gnttab/include/xengnttab.h +3
-0+* Upon receiving notification of their peer's shutdown an application +* is expected to teardown any resources (and in particular any grant +* mappings) in a timely manner.
`Upon receiving notification the peer is expected ...'
tools/libs/gnttab/include/xengnttab.h line 114
Quoted 9 lines
tools/libs/gnttab/include/xengnttab.h +4
-4/* -* Note: -* After fork a child process must not use any opened xc gnttab -* handle inherited from their parent. They must open a new handle if -* they want to interact with xc. +* Returns a handle onto the grant table driver. Logs errors. +* +* Note: After fork a child process must not use any opened gnttab +* handle inherited from their parent, more access any grant mapped
^^^^
tools/libs/gnttab/include/xengnttab.h line 115
Quoted 1 line
tools/libs/gnttab/include/xengnttab.h +1
-0+* areas associated with that handle.
You mean `nor'.
tools/libs/gnttab/include/xengnttab.h line 122
Quoted 7 lines
tools/libs/gnttab/include/xengnttab.h +5
-1+* The child must open a new handle if they want to interact with +* gnttab. * -* Return an fd onto the grant table driver. Logs errors. +* A child may safely call xengnttab_close() on a xengnttab_handle +* inherited from a parent and this will safely reclaim any virtual +* address space used by mappings associated with that handle.
Is this actually true ? Normally close() on an fd does not munmap anything mmapped from the fd.
tools/libs/gnttab/include/xengnttab.h void *xengnttab_map_domain_grant_refs(xengnttab_handle *xgt, · line 204
Quoted 3 lines
tools/libs/gnttab/include/xengnttab.h +3
-0+* On failure sets errno and returns NULL. +* +* If notify_offset or notify_port a requested and cannot be set up an
^
`are'
tools/libs/gnttab/include/xengnttab.h int xengnttab_munmap(xengnttab_handle *xgt, · line 259
Quoted 6 lines
tools/libs/gnttab/include/xengnttab.h +4
-1/* -* Return an fd onto the grant sharing driver. Logs errors. +* Returns a handle onto the grant sharing driver. Logs errors. +* +* The child must open a new handle if they want to interact with +* gntshr.
You probably want to mention `fork' in this sentence, or move it after the Note:.
tools/libs/gnttab/include/xengnttab.h xengntshr_handle *xengntshr_open(xentoollog_logger *logger, · line 283
Quoted 3 lines
tools/libs/gnttab/include/xengnttab.h +1
-0+/** * Creates and shares pages with another domain. *
I think you mean Allocates' not Creates'. (Throughout.) Userland people are used to thinking of memory as being allocated.
It might be worth mentioning that the API does not permit the sharing of memory which is allocated in some other way (eg, malloc, mmap).
Quoted 11 lines
tools/libs/gnttab/linux.c +0
-3@@ -132,12 +132,14 @@ void *osdep_gnttab_grant_map(xengnttab_handle *xgt,if (addr ==MAP_FAILED && errno ==EAGAIN ) {/* -* The grant hypercall can return EAGAIN if the granted page is -* swapped out. Since the paging daemon may be in the same domain, the -* hypercall cannot block without causing a deadlock.
tools/libs/gnttab/linux.c void *osdep_gnttab_grant_map(xengnttab_handle *xgt, · line 142
Quoted 10 lines
tools/libs/gnttab/linux.c +7
-2+* The grant hypercall can return EAGAIN if the granted page +* is swapped out. Since the paging daemon may be in the same +* domain, the hypercall cannot block without causing a +* deadlock. * -* Because there are no notificaitons when the page is swapped in, wait -* a bit before retrying, and hope that the page will arrive eventually. +* Because there are no notifications when the page is swapped +* in, wait a bit before retrying, and hope that the page will +* arrive eventually.
WTF. But not a blocker.
Ian.
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
In reply to Ian CampbellIan Campbell writes ("Re: [Xen-devel] [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation."):
Quoted 8 lines · Andrew Cooper
Quoted 1 line
An
_open()call must be matched with a_close()call.This is not safe after an exec though, since the fd will be closed, or perhaps even reopened already (although the requirement could be to call
_closebefore doing such things as opening any fds, at which point close could silently handle EBADF).
Quoted 6 lines
Quoted 2 lines
In the case of fork but no exec, there should be a
_close()call in both the parent and child, to free other resources.In the parent I assume you mean "at some point (or call
exit())" rather than in some way associated with the forking, because the parent is entitled to keep using the handle.
There seems to be some confusion in this subthread between fork and fork+exec (and, for that matter, exec).
I think the grant fds can sensibly be marked CLOEXEC but that it doesn't matter, because I think the openness of such an fd after exec can never cause anyone a problem. (CLOEXEC is needed for sockets, pipes, etc., where the very openness of the openfile has semantics which are detectable by the peer.)
There is no problem with CLOEXEC causing any bad EBADF or fd reuse, since the post-exec process has a completely fresh address space so cannot have any xengnttab_handle* or whatever. It has nothing on which to call xengnttab_close (or any other call in this file).
(I'm assuming throughout that no-one has conspired to cause the gnttab or gntshr library to get fd 0, 1 or 2. If some did so conspire then they deserve to keep all of the resulting pieces.)
But conversely CLOEXEC doesn't help with fork at all.
Ian.
Re: [PATCH XEN v6 27/32] tools/libs/call: Describe return values and error semantics for xencall*
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 27/32] tools/libs/call: Describe return values and error semantics for xencall*"):
Quoted 1 line
This behaviour has been confirmed by inspection on:
Wei Liu
Acked-byRe: [PATCH XEN v6 20/32] tools/libs/foreignmemory: Support err == NULL to map.
In reply to Ian CampbellQuoted 50 lines · Ian Campbell
Quoted 41 lines · Wei Liu
Quoted 40 lines · Ian Campbell
Quoted 25 lines · Wei Liu
Quoted 25 lines
tools/libs/foreignmemory/core.c +0
-0@@ -14,6 +14,8 @@*/
#include <stdlib.h>tools/libs/foreignmemory/core.c +2
-0+#include <assert.h> +#include <errno.h>
#include "private.h"tools/libs/foreignmemory/core.c +0
-0@@ -64,7 +66,33 @@ void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom, int prot,const
xen_pfn_t*arr, int *err,size_tnum)
{
- returnosdep_xenforeignmemory_map(fmem, dom, prot, arr, err, num);
+ void *ret;
+ int*err_to_free= NULL;Quoted 8 lines
tools/libs/foreignmemory/core.c void *xenforeignmemory_map(xenforeignmemory_handle *fmem, · line 74
Quoted 4 lines
tools/libs/foreignmemory/core.c +4
-0+ + if ( err == NULL ) + err = err_to_free = malloc(num * sizeof(int)); +malloc can fail, which means when user passes in err==NULL there is two
possible behaviours of this function?Partially/Full success and "fail entirely reporting an error" you mean? There are always those two possibilities, yes, regardless of err==NULL. I
think that's inline with the doc comment too.Quoted 7 lines
Quoted 4 lines
If that's not what you were asking then I'm afraid I don't see what you are
getting at.What I meant was we should check err != NULL after malloc because
osdep_xenforeignmeory_mapdereferences it unconditionally.
Quoted 7 lines
Ah, I appear to have managed to put that check in "
tools/libs/foreignmemory: optimisemap(num==1, err==NULL)case" by mistake, so I was confused because I was looking at the final version of the file.I shall move it, thanks.
With that fixed:
Thanks
Quoted 1 line
Ian.
Re: [PATCH XEN v6 29/32] tools/libs/call: Use O_CLOEXEC when opening /dev/xen/privcmd on Linux
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 29/32] tools/libs/call: Use O_CLOEXEC when opening /dev/xen/privcmd on Linux"):
Quoted 3 lines
We stick with using
FD_CLOEXECon the legacy/proc/xen/privcmdfallback device since it was present in older kernel whereO_CLOEXECmay not be assured.
This is a lot of effort and may not be needed. I don't object, but some of the statements are (I think) rather too fierce:
tools/libs/call/linux.c line 59
Quoted 5 lines
tools/libs/call/linux.c +5
-0+/* +* This file descriptor is opaque to the caller, thus we must take +* responsibility to ensure it doesn't propagate (ie leak) outside +* the process, by using CLOEXEC. +*/
For example, I don't think this is a `must' at all, although not propagating irrelevant fds is (nowadays) seen as polite.
Ian.
Re: [PATCH XEN v6 30/32] tools/libs/call: linux: avoid forking between mmap and madvise
In reply to Ian CampbellIan Campbell writes ("[PATCH XEN v6 30/32] tools/libs/call: linux: avoid forking between mmap and madvise"):
Quoted 3 lines
Use
pthread_atforkto prevent the application from forking before themadvisoe(), which would result in CoW mappings getting passed to hypercalls.
OMG.
Is it not possible (not effective) to use mlock/madvise in the parent ?
pthread_atfork is a very big hammer to be using in libxc.
Ian.
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
In reply to Ian JacksonQuoted 10 lines · Ian Jackson
Quoted 7 lines
+ * The child must open a new handle if they want to interact with
+ * gnttab.
*
- * Return an fd onto the grant table driver. Logs errors.
+ * A child may safely callxengnttab_close()on axengnttab_handle
+ * inherited from a parent and this will safely reclaim any virtual
+ * address space used by mappings associated with that handle.Is this actually true ? Normally
close()on an fd does not munmap anything mmapped from the fd.
Uh yes, I was talking rubbish here, unless the drivers are more magic than I think they are.
So what is the answer? That such memory is wasted and irrecovable until exec?
See also the discussion beneath Wei's reply.
Ack to all other feedback.
Quoted 5 lines
Quoted 3 lines
+ * Because there are no notifications when the page is swapped + * in, wait a bit before retrying, and hope that the page will + * arrive eventually.
WTF. But not a blocker.
Quite. A feature of much of the paging code AFAICT...
Quoted 3 lines
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
Ian Campbell writes ("Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation."):
Quoted 9 lines · Ian Jackson
Quoted 2 lines
Is this actually true ? Normally
close()on an fd does not munmap anything mmapped from the fd.Uh yes, I was talking rubbish here, unless the drivers are more magic than I think they are.
So what is the answer? That such memory is wasted and irrecovable until exec?
I don't know. What would happen if you munmapped it ?
Ian.
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
Quoted 6 lines · Ian Jackson
Ian Campbell writes ("Re: [PATCH XEN v6 25/32]
tools/libs/gnttab:
Extensive updates to API documentation."):Quoted 4 lines · Ian Jackson
Quoted 2 lines
Is this actually true ? Normally
close()on an fd does not munmap anything mmapped from the fd.
Quoted 8 lines
Quoted 6 lines
Uh yes, I was talking rubbish here, unless the drivers are more magic than
I think they are.So what is the answer? That such memory is wasted and irrecovable until exec?
I don't know. What would happen if you munmapped it ?
We discussed all this IRL and concluded after much back and forth that there is nothing we can do here other than require a call to each relevant library's _close() method by callers of fork() who do not exec but who care about the (usually modest) resources consumed by these sorts of mappings.
The _close call after fork should be documented as potentially not actually achieving anything and we decided to treat this as a future bug if/when it has a real world impact by fixing.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH XEN v6 14/32] tools: Remove xc_map_foreign_batch
In reply to Ian CampbellQuoted 13 lines · Ian Campbell
It can trivially be replaced by
xc_map_foreign_pageswhich is the interface I want to move to going forward (by standardising on_bulkbut handling err=NULL as_pagesdoes).The callers of
_batchare checking a mixture of a NULL return or looking to see if the top nibble of the (usually sole) mfn they pass has been modified to be non-zero to detect errors._pagesnever modifies the mfn it was given (it's const) and returns NULL on failure, so adjust the error handling where necessary. Some callers use a copy of the mfn array, for reuse on failure with_batch, which is no longer necessary as_pagesdoesn't modify the array, however I haven't cleaned that up here.
Quoted 7 lines
This reduces the twist maze of xc_map_foreign_* by one, which will be useful when trying to come up with an underlying stable interface.
NetBSD and Solaris implemented
xc_map_foreign_bulkin terms ofxc_map_foreign_batchvia a compat layer, soxc_map_foreign_batchbecomes an internal osdep for them. New OS ports should always implementxc_map_foreign_bulkinstead.
"New OS ports should always implement xc_map_foreign_pages instead"?
Other than that:
Quoted 357 lines
Signed-off-by Ian Campbell <[email protected]>Cc George Dunlap <[email protected]>---
v6: Switch toxc_map_foreign_pagesnotxc_map_foreign_bulk, since the
former has more similar error handling semantics to the current usage.Dropped acks.
--- tools/libxc/include/xenctrl.h | 10 ------- tools/libxc/xc_foreign_memory.c | 4 ++- tools/libxc/xc_linux_osdep.c | 59 +++-------------------------------------- tools/libxc/xc_minios.c | 22 --------------- tools/libxc/xc_netbsd.c | 10 +++---- tools/libxc/xc_solaris.c | 6 ++--- tools/libxc/xc_vm_event.c | 18 ++++++++++--- tools/xenmon/xenbaked.c | 12 ++++++++- tools/xenpaging/xenpaging.c | 14 +++++----- tools/xentrace/xentrace.c | 3 ++- 10 files changed, 48 insertions(+), 110 deletions(-)tools/libxc/include/xenctrl.h +0
-10@@ -1387,16 +1387,6 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot, const xen_pfn_t *arr, int num );/** -* DEPRECATED - use xc_map_foreign_bulk() instead. -* -* Like xc_map_foreign_pages(), except it can succeeed partially. -* When a page cannot be mapped, its PFN in @arr is or'ed with -* 0xF0000000 to indicate the error. -*/ -void *xc_map_foreign_batch (xc_interface *xch,uint32_t dom,int prot, -xen_pfn_t *arr,int num ); - -/** * Like xc_map_foreign_pages(), except it can succeed partially. * When a page cannot be mapped, its respective field in @err is * set to the corresponding errno value. tools/libxc/xc_foreign_memory.c +3
-1@@ -55,6 +55,8 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,* just implement xc_map_foreign_bulk. */ #if defined(__NetBSD__) || defined(__sun__) +void *osdep_map_foreign_batch (xc_interface *xch,uint32_t dom,int prot, +xen_pfn_t *arr,int num );void *xc_map_foreign_bulk (xc_interface *xch,uint32_t dom,int prot,const xen_pfn_t *arr,int *err,unsigned int num)@@ -75,7 +77,7 @@ void *xc_map_foreign_bulk(xc_interface *xch, }memcpy (pfn, arr, num *sizeof (*arr)); - ret =xc_map_foreign_batch (xch, dom, prot, pfn, num); + ret =osdep_map_foreign_batch (xch, dom, prot, pfn, num);if (ret) {for (i =0 ; i < num; ++i)tools/libxc/xc_linux_osdep.c +3
-56@@ -91,8 +91,8 @@ int osdep_privcmd_close(xc_interface *xch)return close (fd); } -static int xc_map_foreign_batch_single (int fd,uint32_t dom, -xen_pfn_t *mfn,unsigned long addr) +static int map_foreign_batch_single (int fd,uint32_t dom, +xen_pfn_t *mfn,unsigned long addr) {privcmd_mmapbatch_t ioctlx;int rc;@@ -113,59 +113,6 @@ static int xc_map_foreign_batch_single(int fd, uint32_t dom,return rc; } -void *xc_map_foreign_batch (xc_interface *xch, -uint32_t dom,int prot, -xen_pfn_t *arr,int num) -{ -int fd = xch->privcmdfd; -privcmd_mmapbatch_t ioctlx; -void *addr; -int rc; - - addr =mmap (NULL , num <<XC_PAGE_SHIFT , prot,MAP_SHARED , fd,0 ); -if ( addr ==MAP_FAILED ) - { -PERROR ("xc_map_foreign_batch: mmap failed" ); -return NULL ; - } - - ioctlx.num = num; - ioctlx.dom = dom; - ioctlx.addr = (unsigned long )addr; - ioctlx.arr = arr; - - rc =ioctl (fd,IOCTL_PRIVCMD_MMAPBATCH , &ioctlx); -if ( (rc <0 ) && (errno ==ENOENT ) ) - { -int i; - -for ( i =0 ; i < num; i++ ) - { -if ( (arr[i] &PRIVCMD_MMAPBATCH_MFN_ERROR ) == -PRIVCMD_MMAPBATCH_PAGED_ERROR ) - { -unsigned long paged_addr = (unsigned long )addr + (i <<XC_PAGE_SHIFT ); - rc =xc_map_foreign_batch_single (fd, dom, &arr[i], - paged_addr); -if ( rc <0 ) - goto out; - } - } - } - - out: -if ( rc <0 ) - { -int saved_errno = errno; -PERROR ("xc_map_foreign_batch: ioctl failed" ); - (void )munmap (addr, num <<XC_PAGE_SHIFT ); - errno = saved_errno; -return NULL ; - } - -return addr; -} -/* * Retry mmap of all paged gfns in batches * retuns < 0 on fatal error @@ -305,7 +252,7 @@ void *xc_map_foreign_bulk(xc_interface *xch, err[i] = rc ?: -EINVAL ;continue ; } - rc =xc_map_foreign_batch_single (fd, dom, pfn + i, + rc =map_foreign_batch_single (fd, dom, pfn + i, (unsigned long )addr + ((unsigned long )i<<XC_PAGE_SHIFT ));if ( rc <0 ) {tools/libxc/xc_minios.c +0
-22@@ -73,28 +73,6 @@ void *xc_map_foreign_bulk(xc_interface *xch,return map_frames_ex (arr, num,1 ,0 ,1 , dom, err, pt_prot); } -void *xc_map_foreign_batch (xc_interface *xch, -uint32_t dom,int prot, -xen_pfn_t *arr,int num) -{ -unsigned long pt_prot =0 ; -int err[num]; -int i; -unsigned long addr; - -if (prot &PROT_READ ) - pt_prot =L1_PROT_RO ; -if (prot &PROT_WRITE ) - pt_prot =L1_PROT ; - - addr = (unsigned long )map_frames_ex (arr, num,1 ,0 ,1 , dom, err, pt_prot); -for (i =0 ; i < num; i++) { -if (err[i]) - arr[i] |=0xF0000000 ; - } -return (void *) addr; -} - void *xc_map_foreign_range(xc_interface *xch, uint32_t dom, int size, int prot,tools/libxc/xc_netbsd.c +5
-5@@ -67,16 +67,16 @@ int osdep_privcmd_close(xc_interface *xch)return close (fd); } -void *xc_map_foreign_batch (xc_interface *xch, -uint32_t dom,int prot, -xen_pfn_t *arr,int num) +void *osdep_map_foreign_batch (xc_interface *xch, +uint32_t dom,int prot, +xen_pfn_t *arr,int num) {int fd = xch->privcmdfd;privcmd_mmapbatch_t ioctlx;void *addr; addr =mmap (NULL , num*XC_PAGE_SIZE , prot,MAP_ANON |MAP_SHARED ,-1 ,0 );if ( addr ==MAP_FAILED ) { -PERROR ("xc_map_foreign_batch: mmap failed" ); +PERROR ("osdep_map_foreign_batch: mmap failed" );return NULL ; }@@ -87,7 +87,7 @@ void *xc_map_foreign_batch(xc_interface *xch,if (ioctl (fd,IOCTL_PRIVCMD_MMAPBATCH , &ioctlx) <0 ) {int saved_errno = errno; -PERROR ("xc_map_foreign_batch: ioctl failed" ); +PERROR ("osdep_map_foreign_batch: ioctl failed" ); (void )munmap (addr, num*XC_PAGE_SIZE ); errno = saved_errno;return NULL ;tools/libxc/xc_solaris.c +3
-3@@ -67,9 +67,9 @@ int osdep_privcmd_close(xc_interface *xch)return close (fd); } -void *xc_map_foreign_batch (xc_interface *xch, -uint32_t dom,int prot, -xen_pfn_t *arr,int num) +void *osdep_map_foreign_batch (xc_interface *xch, +uint32_t dom,int prot, +xen_pfn_t *arr,int num) {int fd = xch->privcmdfd;privcmd_mmapbatch_t ioctlx;tools/libxc/xc_vm_event.c +14
-4@@ -72,9 +72,9 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param, ring_pfn = pfn; mmap_pfn = pfn; - ring_page =xc_map_foreign_batch (xch, domain_id,PROT_READ |PROT_WRITE , + ring_page =xc_map_foreign_pages (xch, domain_id,PROT_READ |PROT_WRITE , &mmap_pfn,1 ); -if ( mmap_pfn &XEN_DOMCTL_PFINFO_XTAB ) +if ( !ring_page ) {/* Map failed, populate ring page */ rc1 = xc_domain_populate_physmap_exact(xch, domain_id, 1, 0, 0,@@ -86,9 +86,9 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param, } mmap_pfn = ring_pfn; - ring_page =xc_map_foreign_batch (xch, domain_id,PROT_READ |PROT_WRITE , + ring_page =xc_map_foreign_pages (xch, domain_id,PROT_READ |PROT_WRITE , &mmap_pfn,1 ); -if ( mmap_pfn &XEN_DOMCTL_PFINFO_XTAB ) +if ( !ring_page ) {PERROR ("Could not map the ring page\n" ); goto out;@@ -156,3 +156,13 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param,return ring_page; } + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ tools/xenmon/xenbaked.c +11
-1@@ -411,7 +411,7 @@ static struct t_struct *map_tbufs(unsigned long tbufs_mfn, unsigned int num,for ( j=0 ; j<tbufs.t_info->tbuf_size; j++) pfn_list[j] = (xen_pfn_t )mfn_list[j]; - tbufs.meta[i] =xc_map_foreign_batch (xc_handle,DOMID_XEN , + tbufs.meta[i] =xc_map_foreign_pages (xc_handle,DOMID_XEN ,PROT_READ |PROT_WRITE , pfn_list, tbufs.t_info->tbuf_size);@@ -1175,3 +1175,13 @@ static int process_record(int cpu, struct t_rec *r)return 4 + (r->cycles_included ?8 :0 ) + (r->extra_u32 *4 ); } + +/* +* Local variables: +* mode: C +* c-file-style: "BSD" +* c-basic-offset: 4 +* tab-width: 4 +* indent-tabs-mode: nil +* End: +*/ tools/xenpaging/xenpaging.c +7
-7@@ -342,9 +342,9 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[]) HVM_PARAM_PAGING_RING_PFN, &ring_pfn); mmap_pfn = ring_pfn; paging->vm_event.ring_page = -xc_map_foreign_batch (xch, paging->vm_event.domain_id, -PROT_READ |PROT_WRITE , &mmap_pfn,1 ); -if ( mmap_pfn &XEN_DOMCTL_PFINFO_XTAB ) +xc_map_foreign_pages (xch, paging->vm_event.domain_id, +PROT_READ |PROT_WRITE , &mmap_pfn,1 ); +if ( !paging->vm_event.ring_page ) {/* Map failed, populate ring page */ rc = xc_domain_populate_physmap_exact(paging->xc_handle,@@ -356,11 +356,11 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[]) goto err; } - mmap_pfn = ring_pfn; paging->vm_event.ring_page = -xc_map_foreign_batch (xch, paging->vm_event.domain_id, -PROT_READ |PROT_WRITE , &mmap_pfn,1 ); -if ( mmap_pfn &XEN_DOMCTL_PFINFO_XTAB ) +xc_map_foreign_pages (xch, paging->vm_event.domain_id, +PROT_READ |PROT_WRITE , + &mmap_pfn,1 ); +if ( !paging->vm_event.ring_page ) {PERROR ("Could not map the ring page\n" ); goto err;tools/xentrace/xentrace.c +2
-1@@ -508,7 +508,7 @@ static struct t_struct *map_tbufs(unsigned long tbufs_mfn, unsigned int num,for ( j=0 ; j<tbufs.t_info->tbuf_size; j++) pfn_list[j] = (xen_pfn_t )mfn_list[j]; - tbufs.meta[i] =xc_map_foreign_batch (xc_handle,DOMID_XEN , + tbufs.meta[i] =xc_map_foreign_pages (xc_handle,DOMID_XEN ,PROT_READ |PROT_WRITE , pfn_list, tbufs.t_info->tbuf_size);@@ -1221,6 +1221,7 @@ int main(int argc, char **argv)return ret; } +/* * Local variables: * mode: C
Re: [PATCH QEMU-XEN v6 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
In reply to Ian CampbellQuoted 53 lines · Ian Campbell
hw/display/xenfb.c
Quoted 13 lines · Ian Campbell
hw/display/xenfb.c +0
-0@@ -104,9 +104,8 @@ static int common_bind(struct common *c) if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1) return -1;
- c->page = xc_map_foreign_range(xen_xc,c->xendev.dom,hw/display/xenfb.c +2
-2- XC_PAGE_SIZE, - PROT_READ | PROT_WRITE, mfn); + c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, + PROT_READ | PROT_WRITE, &mfn, 1);This doesn't build for i386 userspace, since mfn is a
uint64_tbutxc_map_foreign_pages()wants axen_pfn_t* (wherexen_pfn_t== unsigned long on x86).Until now that was just a truncation which was already checked for with:
uint64_tmfn;if (
xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) return -1;
assert(mfn == (xen_pfn_t)mfn);I think in principal passing "(
xen_pfn_t*)&mfn" would ok (since it is a singleton array in this case), but I was thinking of going a bit further and:hw/display/xenfb.c +0
-0@@ -95,11 +95,13 @@ struct XenFB {
static intcommon_bind(struct common *c)
{
- uint64_t mfn;
+ uint64_t val;
+ xen_pfn_t mfn;
- if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1)
+ if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val)== -1) return -1;
- assert(mfn == (xen_pfn_t)mfn);
+ mfn = (xen_pfn_t)val;
+ assert(val == mfn);
if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port)== -1) return -1;
Quoted 1 line
Stefano, what do you think/prefer? An alternative to the above
I like this change because it makes the code more obvious
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [Qemu-devel] [PATCH QEMU-XEN v6 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
In reply to Ian CampbellQuoted 53 lines · Ian Campbell
hw/display/xenfb.c
Quoted 13 lines · Ian Campbell
hw/display/xenfb.c +0
-0@@ -104,9 +104,8 @@ static int common_bind(struct common *c) if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1) return -1;
- c->page = xc_map_foreign_range(xen_xc,c->xendev.dom,hw/display/xenfb.c +2
-2- XC_PAGE_SIZE, - PROT_READ | PROT_WRITE, mfn); + c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, + PROT_READ | PROT_WRITE, &mfn, 1);This doesn't build for i386 userspace, since mfn is a
uint64_tbutxc_map_foreign_pages()wants axen_pfn_t* (wherexen_pfn_t== unsigned long on x86).Until now that was just a truncation which was already checked for with:
uint64_tmfn;if (
xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) return -1;
assert(mfn == (xen_pfn_t)mfn);I think in principal passing "(
xen_pfn_t*)&mfn" would ok (since it is a singleton array in this case), but I was thinking of going a bit further and:hw/display/xenfb.c +0
-0@@ -95,11 +95,13 @@ struct XenFB {
static intcommon_bind(struct common *c)
{
- uint64_t mfn;
+ uint64_t val;
+ xen_pfn_t mfn;
- if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1)
+ if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val)== -1) return -1;
- assert(mfn == (xen_pfn_t)mfn);
+ mfn = (xen_pfn_t)val;
+ assert(val == mfn);
if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port)== -1) return -1;
Quoted 1 line
Stefano, what do you think/prefer? An alternative to the above
I like this change because it makes the code more obvious
Re: [Qemu-devel] [PATCH QEMU-XEN v6 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
Quoted 56 lines · Stefano Stabellini
Quoted 55 lines · Ian Campbell
Quoted 10 lines · Ian Campbell
hw/display/xenfb.c +0
-0@@ -104,9 +104,8 @@ static int common_bind(struct common *c)if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 1 line
xendev.remote_port) == -1)
return -1;
- c->page = xc_map_foreign_range(xen_xc,c->xendev.dom,hw/display/xenfb.c
Quoted 4 lines
hw/display/xenfb.c +2
-2- XC_PAGE_SIZE, - PROT_READ | PROT_WRITE, mfn); + c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, + PROT_READ | PROT_WRITE, &mfn, 1);This doesn't build for i386 userspace, since mfn is a
uint64_tbutxc_map_foreign_pages()wants axen_pfn_t* (wherexen_pfn_t== unsigned long on x86).Until now that was just a truncation which was already checked for with:
uint64_tmfn;if (
xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) return -1;
assert(mfn == (xen_pfn_t)mfn);I think in principal passing "(
xen_pfn_t*)&mfn" would ok (since it is a
singleton array in this case), but I was thinking of going a bit further
and:hw/display/xenfb.c +0
-0@@ -95,11 +95,13 @@ struct XenFB {
static intcommon_bind(struct common *c)
{
- uint64_t mfn;
+ uint64_t val;
+ xen_pfn_t mfn;
- if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) + if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val)== -1) return -1;
- assert(mfn == (xen_pfn_t)mfn);
+ mfn = (xen_pfn_t)val;
+ assert(val == mfn);
if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
xendev.remote_port) == -1)
return -1;
Stefano, what do you think/prefer? An alternative to the above
I like this change because it makes the code more obvious
Thanks, with that change may I keep your Reviewed-by?
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH XEN v6 14/32] tools: Remove xc_map_foreign_batch
In reply to George DunlapQuoted 17 lines · George Dunlap
Quoted 16 lines · Ian Campbell
It can trivially be replaced by
xc_map_foreign_pageswhich is the interface I want to move to going forward (by standardising on_bulkbut handling err=NULL as_pagesdoes).The callers of
_batchare checking a mixture of a NULL return or looking to see if the top nibble of the (usually sole) mfn they pass has been modified to be non-zero to detect errors._pagesnever modifies the mfn it was given (it's const) and returns NULL on failure, so adjust the error handling where necessary. Some callers use a copy of the mfn array, for reuse on failure with_batch, which is no longer necessary as_pagesdoesn't modify the array, however I haven't cleaned that up here.This reduces the twist maze of xc_map_foreign_* by one, which will be useful when trying to come up with an underlying stable interface.
Quoted 6 lines
Quoted 4 lines
NetBSD and Solaris implemented
xc_map_foreign_bulkin terms ofxc_map_foreign_batchvia a compat layer, soxc_map_foreign_batchbecomes an internal osdep for them. New OS ports should always implementxc_map_foreign_bulkinstead."New OS ports should always implement
xc_map_foreign_pagesinstead"?
"It's complicated" ;-).
New ports should indeed implement the bulk interface, which will in a later patch become the API which libxenforeignmemory provides. Then the _pages style interface comes via a that API providing a mode which is compatible with that way of use (tolerating passing a NULL err array).
Given that the advice here is going to be superceded pretty soon in this series, I think I will therefore just drop that final sentence.
Quoted 3 lines
Other than that:
Acked-by George Dunlap <[email protected]>
I'll take the liberty of assuming this still applies with the change discussed above, please let me know if not.
Cheers,
Ian.
Re: [PATCH XEN v6 22/32] tools/libs/foreignmemory: optimise map(num==1, err==NULL) case
In reply to Ian JacksonQuoted 11 lines · Ian Jackson
Ian Campbell writes ("[PATCH XEN v6 22/32]
tools/libs/foreignmemory:
optimisemap(num==1, err==NULL)case"):Quoted 3 lines
This looks to be a reasonably common case (many of the previous callers to
xc_map_foreign_pagesuse it) and it is easy enough to avoid a malloc for it....
Quoted 2 lines
I'm not 100% sure this is worth the complexity, hence not folding into the base patch yet, it could easily be dropped.
I would suggest that we should park this until we have a benchmark showing it's helpful.
Agreed, dropped for v7.
Ian.
Re: [PATCH XEN v6 14/32] tools: Remove xc_map_foreign_batch
In reply to Ian CampbellQuoted 35 lines · Ian Campbell
Quoted 17 lines · George Dunlap
Quoted 16 lines · Ian Campbell
It can trivially be replaced by
xc_map_foreign_pageswhich is the interface I want to move to going forward (by standardising on_bulkbut handling err=NULL as_pagesdoes).The callers of
_batchare checking a mixture of a NULL return or looking to see if the top nibble of the (usually sole) mfn they pass has been modified to be non-zero to detect errors._pagesnever modifies the mfn it was given (it's const) and returns NULL on failure, so adjust the error handling where necessary. Some callers use a copy of the mfn array, for reuse on failure with_batch, which is no longer necessary as_pagesdoesn't modify the array, however I haven't cleaned that up here.This reduces the twist maze of xc_map_foreign_* by one, which will be useful when trying to come up with an underlying stable interface.
Quoted 6 lines
Quoted 4 lines
NetBSD and Solaris implemented
xc_map_foreign_bulkin terms ofxc_map_foreign_batchvia a compat layer, soxc_map_foreign_batchbecomes an internal osdep for them. New OS ports should always implementxc_map_foreign_bulkinstead."New OS ports should always implement
xc_map_foreign_pagesinstead"?"It's complicated" ;-).
New ports should indeed implement the bulk interface, which will in a later patch become the API which libxenforeignmemory provides. Then the
_pagesstyle interface comes via a that API providing a mode which is compatible with that way of use (tolerating passing a NULL err array).Given that the advice here is going to be superceded pretty soon in this series, I think I will therefore just drop that final sentence.
Quoted 6 lines
Quoted 3 lines
Other than that:
Acked-by George Dunlap <[email protected]>I'll take the liberty of assuming this still applies with the change discussed above, please let me know if not.
Oh, right -- I assumed that you'd just missed a string replace somewhere. Yes, the Ack still stands.
-George
Re: [PATCH QEMU-XEN v6 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
In reply to Ian CampbellQuoted 57 lines · Ian Campbell
Quoted 56 lines · Stefano Stabellini
Quoted 55 lines · Ian Campbell
Quoted 10 lines · Ian Campbell
hw/display/xenfb.c +0
-0@@ -104,9 +104,8 @@ static int common_bind(struct common *c)if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 1 line
xendev.remote_port) == -1)
return -1;
- c->page = xc_map_foreign_range(xen_xc,c->xendev.dom,hw/display/xenfb.c
Quoted 4 lines
hw/display/xenfb.c +2
-2- XC_PAGE_SIZE, - PROT_READ | PROT_WRITE, mfn); + c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, + PROT_READ | PROT_WRITE, &mfn, 1);This doesn't build for i386 userspace, since mfn is a
uint64_tbutxc_map_foreign_pages()wants axen_pfn_t* (wherexen_pfn_t== unsigned long on x86).Until now that was just a truncation which was already checked for with:
uint64_tmfn;if (
xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) return -1;
assert(mfn == (xen_pfn_t)mfn);I think in principal passing "(
xen_pfn_t*)&mfn" would ok (since it is a
singleton array in this case), but I was thinking of going a bit further
and:hw/display/xenfb.c +0
-0@@ -95,11 +95,13 @@ struct XenFB {
static intcommon_bind(struct common *c)
{
- uint64_t mfn;
+ uint64_t val;
+ xen_pfn_t mfn;
- if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) + if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val)== -1) return -1;
- assert(mfn == (xen_pfn_t)mfn);
+ mfn = (xen_pfn_t)val;
+ assert(val == mfn);
if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
xendev.remote_port) == -1)
return -1;
Stefano, what do you think/prefer? An alternative to the above
I like this change because it makes the code more obvious
Thanks, with that change may I keep your Reviewed-by?
Sure
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [Qemu-devel] [PATCH QEMU-XEN v6 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
In reply to Ian CampbellQuoted 57 lines · Ian Campbell
Quoted 56 lines · Stefano Stabellini
Quoted 55 lines · Ian Campbell
Quoted 10 lines · Ian Campbell
hw/display/xenfb.c +0
-0@@ -104,9 +104,8 @@ static int common_bind(struct common *c)if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 1 line
xendev.remote_port) == -1)
return -1;
- c->page = xc_map_foreign_range(xen_xc,c->xendev.dom,hw/display/xenfb.c
Quoted 4 lines
hw/display/xenfb.c +2
-2- XC_PAGE_SIZE, - PROT_READ | PROT_WRITE, mfn); + c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom, + PROT_READ | PROT_WRITE, &mfn, 1);This doesn't build for i386 userspace, since mfn is a
uint64_tbutxc_map_foreign_pages()wants axen_pfn_t* (wherexen_pfn_t== unsigned long on x86).Until now that was just a truncation which was already checked for with:
uint64_tmfn;if (
xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) return -1;
assert(mfn == (xen_pfn_t)mfn);I think in principal passing "(
xen_pfn_t*)&mfn" would ok (since it is a
singleton array in this case), but I was thinking of going a bit further
and:hw/display/xenfb.c +0
-0@@ -95,11 +95,13 @@ struct XenFB {
static intcommon_bind(struct common *c)
{
- uint64_t mfn;
+ uint64_t val;
+ xen_pfn_t mfn;
- if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn)== -1) + if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val)== -1) return -1;
- assert(mfn == (xen_pfn_t)mfn);
+ mfn = (xen_pfn_t)val;
+ assert(val == mfn);
if (xenstore_read_fe_int(&c->xendev, "event-channel", &c-
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
xendev.remote_port) == -1)
return -1;
Stefano, what do you think/prefer? An alternative to the above
I like this change because it makes the code more obvious
Thanks, with that change may I keep your Reviewed-by?
Sure
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
In reply to Ian CampbellQuoted 17 lines · Ian Campbell
Quoted 10 lines · Ian Jackson
Ian Campbell writes ("Re: [PATCH XEN v6 25/32]
tools/libs/gnttab:
Extensive updates to API documentation."):Quoted 8 lines · Ian Jackson
Quoted 2 lines
Is this actually true ? Normally
close()on an fd does not munmap anything mmapped from the fd.Uh yes, I was talking rubbish here, unless the drivers are more magic than
I think they are.Quoted 5 lines
Quoted 3 lines
So what is the answer? That such memory is wasted and irrecovable until
exec?I don't know. What would happen if you munmapped it ?
Quoted 8 lines
We discussed all this IRL and concluded after much back and forth that there is nothing we can do here other than require a call to each relevant library's
_close()method by callers offork()who do not exec but who care about the (usually modest) resources consumed by these sorts of mappings.The
_closecall after fork should be documented as potentially not actually achieving anything and we decided to treat this as a future bug if/when it has a real world impact by fixing.
Here is what I cam up with.
/*
* Returns a handle onto the grant table driver. Logs errors. *
* Note: After fork(2) a child process must not use any opened gnttab
* handle inherited from their parent, nor access any grant mapped
* areas associated with that handle.
*
* The child must open a new handle if they want to interact with
* gnttab.
*
* Calling exec(2) in a child will safely (and reliably) reclaim any
* allocated resources via a xengnttab_handle in the parent. *
* A child which does not call exec(2) may safely call
* xengnttab_close() on a xengnttab_handle inherited from their
* parent. This will attempt to reclaim any resources associated with
* that handle. Note that in some implementations this reclamation may
* not be completely effective, in this case any affected resources
* remain allocated.
*
* Calling xengnttab_close() is the only safe operation on a
* xengnttab_handle which has been inherited.
*/
I'm currently intending to write something very similar for each of the xen*_open (gntshr, gnttab, foreignmemory, evtchn and probably call too) functions. I don't really like repeating the same thing like this, but I also don't really like API documentation which makes you play follow the piece of string to the docs of other libraries to find out what is going on.
I'm also considering whether on the _close function I should write something to the effect that in non-forked contexts the application should call the associated _unmap() on any active mappings before calling _close() in order to ensure all resources have been freed. And reiterate that calling _unmap after fork() is not safe.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH XEN v6 30/32] tools/libs/call: linux: avoid forking between mmap and madvise
In reply to Ian JacksonQuoted 6 lines · Ian Jackson
Ian Campbell writes ("[PATCH XEN v6 30/32]
tools/libs/call: linux: avoid forking between mmap and madvise"):Quoted 3 lines
Use
pthread_atforkto prevent the application from forking before themadvisoe(), which would result in CoW mappings getting passed to hypercalls.
Quoted 6 lines
OMG.
Is it not possible (not effective) to use mlock/madvise in the parent ?
pthread_atforkis a very big hammer to be using in libxc.
I think we decided in our RL conversation the other day that this was indeed unnecessary and the docs updates (just sent) together with a bug fix (touch the memory after madvise to un-cow it) would be sufficient for now.
Ian.
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
In reply to Ian CampbellIan Campbell writes ("Re: [Xen-devel] [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation."):
Quoted 6 lines
I'm currently intending to write something very similar for each of the xen*_open (gntshr, gnttab, foreignmemory, evtchn and probably call too) functions. I don't really like repeating the same thing like this, but I also don't really like API documentation which makes you play follow the piece of string to the docs of other libraries to find out what is going on.
That would be fine by me. But do these other functions suffer from the same problem ?
Ie, can we for gntshr et al, tell the user that they should call blah_unmap ? This is not practical in a multithreaded program for hypercall memory but it might well be practical for other kinds of borrowed memory.
Quoted 5 lines
I'm also considering whether on the
_closefunction I should write something to the effect that in non-forked contexts the application should call the associated_unmap()on any active mappings before calling_close()in order to ensure all resources have been freed. And reiterate that calling_unmapafterfork()is not safe.
Yes.
Ian
Re: [PATCH XEN v6 29/32] tools/libs/call: Use O_CLOEXEC when opening /dev/xen/privcmd on Linux
In reply to Ian JacksonQuoted 9 lines · Ian Jackson
Ian Campbell writes ("[PATCH XEN v6 29/32]
tools/libs/call: UseO_CLOEXECwhen opening/dev/xen/privcmdon Linux"):Quoted 3 lines
We stick with using
FD_CLOEXECon the legacy/proc/xen/privcmdfallback device since it was present in older kernel whereO_CLOEXECmay not be assured.This is a lot of effort and may not be needed. I don't object, but some of the statements are (I think) rather too fierce:
Quoted 8 lines
Quoted 5 lines
+ /*
+ * This file descriptor is opaque to the caller, thus we must take + * responsibility to ensure it doesn't propagate (ie leak) outside + * the process, by using CLOEXEC.
+ */For example, I don't think this is a `must' at all, although not propagating irrelevant fds is (nowadays) seen as polite.
Right, this bit was actually (mostly) code^Wcomment motion.
I'll happily update it to say polite rather than required.
I did a bit more investigation of O_CLOEXEC and found that Linux introduced it in 2.6.23 (October 2007) which seems to be old enough that we should just use it anywhere we feel it necessary.
Jan did mention (on IRC) that while he doesn't use any kernels so old, he still occasionally builds on userspace which is old enough to lack the definition, hence I would do #ifndef+#define.
Roger, I see that open(2) on FreeBSD mentions O_CLOEXEC too. Do you know when that arrived and whether it is something we can assume these days? I found a posting of the patch in https://lists.freebsd.org/pipermail/freeb
sd-fs/2011-March/011021.html, but I don't know when it landed or when it became something code could assume.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
In reply to Ian JacksonQuoted 12 lines · Ian Jackson
Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v6 25/32]
tools/libs/gnttab: Extensive updates to API documentation."):Quoted 9 lines
I'm currently intending to write something very similar for each of the xen*_open (gntshr, gnttab, foreignmemory, evtchn and probably call too) functions. I don't really like repeating the same thing like this, but I
also don't really like API documentation which makes you play follow the
piece of string to the docs of other libraries to find out what is going
on.
Quoted 7 lines
That would be fine by me. But do these other functions suffer from the same problem ?
Ie, can we for gntshr et al, tell the user that they should call
blah_unmap? This is not practical in a multithreaded program for hypercall memory but it might well be practical for other kinds of borrowed memory.
Good point, I think most of the rest of them probably can support unmap after fork, but I've not checked (I will before I write anything).
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH XEN v6 29/32] tools/libs/call: Use O_CLOEXEC when opening /dev/xen/privcmd on Linux
In reply to Ian CampbellEl 11/12/15 a les 18.22, Ian Campbell ha escrit:
Quoted 31 lines · Ian Jackson
Quoted 9 lines
Ian Campbell writes ("[PATCH XEN v6 29/32]
tools/libs/call: UseO_CLOEXECwhen opening/dev/xen/privcmdon Linux"):Quoted 3 lines
We stick with using
FD_CLOEXECon the legacy/proc/xen/privcmdfallback device since it was present in older kernel whereO_CLOEXECmay not be assured.This is a lot of effort and may not be needed. I don't object, but some of the statements are (I think) rather too fierce:
Quoted 8 lines
tools/libs/call/linux.c line 59
Quoted 5 lines
tools/libs/call/linux.c +5
-0+/* +* This file descriptor is opaque to the caller, thus we must take +* responsibility to ensure it doesn't propagate (ie leak) outside +* the process, by using CLOEXEC. +*/ For example, I don't think this is a `must' at all, although not propagating irrelevant fds is (nowadays) seen as polite.
Right, this bit was actually (mostly) code^Wcomment motion.
I'll happily update it to say polite rather than required.
I did a bit more investigation of
O_CLOEXECand found that Linux introduced it in 2.6.23 (October 2007) which seems to be old enough that we should just use it anywhere we feel it necessary.Jan did mention (on IRC) that while he doesn't use any kernels so old, he still occasionally builds on userspace which is old enough to lack the definition, hence I would do #ifndef+#define.
Quoted 5 lines
Roger, I see that open(2) on FreeBSD mentions
O_CLOEXECtoo. Do you know when that arrived and whether it is something we can assume these days? I found a posting of the patch in https://lists.freebsd.org/pipermail/freeb
sd-fs/2011-March/011021.html, but I don't know when it landed or when it became something code could assume.
privcmd is only available on FreeBSD HEAD anyway, and AFAICT (by looking at the man pages) all stable FreeBSD versions support O_CLOEXEC (10.2, 9.3 and 8.4).
I think we can safely assume O_CLOEXEC is available in all FreeBSD version we build on.
Roger.
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
In reply to Ian CampbellQuoted 19 lines · Ian Campbell
Quoted 18 lines · Ian Jackson
Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v6 25/32]
tools/libs/gnttab: Extensive updates to API documentation."):Quoted 4 lines
I'm currently intending to write something very similar for each of the
xen*_open (gntshr, gnttab, foreignmemory, evtchn and probably call too)Quoted 8 lines
functions. I don't really like repeating the same thing like this, but
I
also don't really like API documentation which makes you play follow the
piece of string to the docs of other libraries to find out what is going
on.That would be fine by me. But do these other functions suffer from the same problem ?
Quoted 7 lines
Quoted 4 lines
Ie, can we for gntshr et al, tell the user that they should call
blah_unmap? This is not practical in a multithreaded program for hypercall memory but it might well be practical for other kinds of borrowed memory.Good point, I think most of the rest of them probably can support unmap after fork, but I've not checked (I will before I write anything).
Good thing I did.
On Linux both the gnttab and privcmd foreign mappings set VMA_DONTCOPY on the mappings from the driver, which is the same effect as the madvise(DONTFORK) in the hypercall buffer allocation has.
For gnttab this is obvious now I think about it, since you cannot unmap or otherwise operate on a gntmap with normal PTE updates, you have to use gntmap hypercalls, which core OS code like CoW handling obviously won't.
The same could be true for privcmd foreign mappings for PV guests, I think, since the PTE update hypercall needs the foreign domid in it, which common code wouldn't have.
So those memory regions simply won't exist in the child, and munmap would therefore either fail or unmap whatever new thing has shown up at an address.
So I think at least call, foreignmemory and gnttab need a restriction/caveat like the above, which is that on fork but not exec you can call close() and hope it does something, but unmap is off the table.
gntshr might be ok, although TBH I'm not really sure what the effect of cow-wing memory which has been granted, nor how one ensures the desired child/parent ends up with the actually granted memory and not a copy (IOW it's not clear why VMA_DONTCOPY isn't applied here too). On that basis I'd be inclined to give gntshr the same semantics as the others.
evtchn doesn't actually involve any memory mappings, and in that case closing the fd is always fine and sufficient AFAICT, no need to disable individual irqs first. I'm not entirely sure what the semantics of a poll or read from both parent and child would be. I'm slightly inclined to outlaw use in the child at the library API level. A slightly less draconian rule would be one or the other may continue to use it, but never both.
That's all based on the Linux implementation, but the libraries need to be LCD and so far that's pretty low... I looked at the FreeBSD privcmd, and it didn't seem to have any obvious equivalent to VMA_DONTCOPY, but I might be missing something (and it doesn't really matter for the library API given Linux).
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation.
Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v6 25/32] tools/libs/gnttab: Extensive updates to API documentation."):
Quoted 3 lines
So I think at least call, foreignmemory and gnttab need a restriction/caveat like the above, which is that on fork but not exec you can call
close()and hope it does something, but unmap is off the table.
Hrm, right.
(This all seems rather unsatisfactory but I guess it's where we are. ISTM that part of what's needed is a version of MADV_DONTFORK which causes children to get a mapping of nothing, with PROT_NONE, for the relevant ranges. Then you could safely munmap.)
Ian.
Re: [PATCH XEN v6 10/32] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab.
In reply to Ian Campbelltools/libs/gnttab/include/xengnttab.h line 133
Quoted 9 lines · Ian Campbell
[...]
tools/libs/gnttab/include/xengnttab.h new file +7
-0+void *xengnttab_map_grant_ref (xengnttab_handle *xgt, + uint32_t domid, + uint32_t ref, + int prot); +int xengnttab_munmap (xengnttab_handle *xgt, + void *start_address, + uint32_t count);
The use of munmap here is a bit inconsistent, it's not xengnttab_mmap and xenforeignmemory is just unmap, so I think I'll change this s/munmap/unmap/.
tools/libs/gnttab/include/xengnttab.h line 206
Quoted 6 lines
[...]
tools/libs/gnttab/include/xengnttab.h new file +4
-0+void *xengntshr_share_pages (xengntshr_handle *xgs,uint32_t domid, + int count, uint32_t *refs, int writable); + +int xengntshr_munmap (xengntshr_handle *xgs,void *start_address,uint32_t count);
For this one I think unshare would be a better name as a counterpart to xengntshr_share_*.
I don't think these changes should invalidate any existing review/ack, but I thought I would mention it up front.
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel