Skip to contentSkip to conversation

Begin to disentangle libxenctrl and provide some stable libraries

PATCH Superseded Ian Campbell 8 participants 104 messages Nov 9, 2015 – Dec 2, 2015 2 messages not in the archive Page 2 of 2

Subsystems (2)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#16 lore raw

Re: [PATCH XEN v5 07/23] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab.

In reply to Daniel De Graaf
Quoted 21 lines · Daniel De Graaf
Quoted 20 lines · Ian Campbell
Quoted 5 lines · Ian Jackson

Ian Campbell writes ("[PATCH XEN v5 07/23] tools: Refactor /dev/xen/gnt{dev,shr} wrappers into libxengnttab."):

Quoted 3 lines

libxengnttab will provide a stable API and ABI for accessing the grant table devices.

Quoted 7 lines
Quoted 5 lines

The functions are moved into the xengnt{tab,shr} namespace to make a
clean break from libxc and avoid ambiguity regarding which interfaces
are stable.

I have reviewed the API.  Mostly this produced questions...

Yes, thanks this is the sort of feedback I was looking for before we call
this a stable interface.

Some of my answers are of the "yes, we should decide that" variety.

Quoted 7 lines
Quoted 5 lines

Daniel, I've added you because some of the questions below relate to the
notification mechanism and to gntshr which I don't really understand (not
that I really understand gnttab either).

Responses below.

Thanks.

Quoted 27 lines

  I also agree that unifying the interfaces would be better.

[...]

Quoted 23 lines
Quoted 23 lines
Quoted 8 lines

+/**
+ * Memory maps a grant reference from one domain to a local address
range.
+ * Mappings should be unmapped with xengnttab_munmap. If notify_offset
or
+ * notify_port are not -1, this version will attempt to set up an

Quoted 8 lines

unmap
+ * notification at the given offset and event channel. When the page
is
+ * unmapped, the byte at the given offset will be zeroed and a wakeup
will be
+ * sent to the given event channel.  Logs errors.

What happens if the unmap notification cannot be set up ?

Also "when the page is unmapped" makes it sound like you mean xengnttab_munmap but actually I think this is when the grant is withdrawn by the grantor ?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

If the grant is withdrawn by the grantor, does the page become unuseable immediately ?  If so, how can anyone ever use this safely ?

Daniel, could you answer these ones please.

This is intended to allow the kernel to send a close-request notification when the application that allocated the grant page exits without calling a proper shutdown (i.e. it crashes, calls _exit, calls execve, etc).

That is the kernel of the grantor or grantee process? It sounds like grantor tells grantee (who would then be expected to unmap?)

Who actually does the unmap, the grantee process or their kernel? I suppose the process, which is expected to be watching for the notification and is required to do the unmap itself. IOW the "munmap notification" is a request to please munmap, not a notification that something has been unmapped out from beneath the calling process.

What happens if the unmap notification cannot be set up? Does the call fail (and unmap what it has done) or does it succeed?

I think the answer to the other two questions depend on the clarifications above, but I think it is the case that nothing is unmapped automatically, all that this does is give you a result from evtchn_poll etc with the expectation that the caller will call xengnttab_munmap in a controlled way by themselves.

Quoted 16 lines

Without this signal, the kernel has no way to request that the mapper of the page release it, and since Xen has no grant revocation mechanism, the page will likely be tied up until the process on the other side is told to release the page through some other method.

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines

+/*
+ * Creates and shares pages with another domain.
+ *

...

Quoted 3 lines

+void *xengntshr_share_pages(xengntshr_handle *xgs, uint32_t domid, +                            int count, uint32_t *refs, int writable);

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Can this fail ?  Can it partially succeed ?

Daniel?

It can fail if you are out of pages to grant (there is a module parameter that can be adjusted via sysfs for the maximum), or in the unlikely case that the kernel itself is out of room in its grant mapping table (or if the syscall itself encounters -ENOMEM).

Does it either completely succeed or undo partial work, or does it return partial success somehow?

Quoted 24 lines
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines

+/*
+ * Creates and shares a page with another domain, with unmap notification.
+ *
+ * @parm xgs a handle to an open grant sharing instance
+ * @parm domid the domain to share memory with
+ * @parm refs the grant reference of the pages (output)
+ * @parm writable true if the other domain can write to the page
+ * @parm notify_offset The byte offset in the page to use for unmap
+ *                     notification; -1 for none.
+ * @parm notify_port The event channel port to use for unmap

Quoted 8 lines
Quoted 6 lines

notify,
or -1
+ * @return local mapping of the page
+ */
+void *xengntshr_share_page_notify(xengntshr_handle *xgs, uint32_t domid,

What is this `unmap notification' ?

Quoted 1 line

Daniel?

Quoted 7 lines

As mentioned above, it is a way for the kernel to request for the other side to unmap a page.  It is probably easiest to understand if looking at the libvchan driver: one byte of the page is a "status" byte, and when the side using xengntshr exits or dies, this byte is set to zero and the event channel is notified.  When the peer is woken by the notify and sees the state byte set to zero, it removes its own mappings so that the shared pages can be freed.

This is the sending end of the notification requested by the caller of xengnttab_map_grant_ref_notify I think?

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 6 lines

+/*
+ * Unmaps the @count pages starting at @start_address, which were mapped by a
+ * call to xengntshr_share_*. Never logs.

Linewrap in the comment.

Quoted 3 lines

+ */
+int xengntshr_munmap(xengntshr_handle *xgs, void *start_address, uint32_t count);

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What effect does this have on the peer ?

Daniel?

If this removes the (final copy of the) mapping and a notify offset/port is set, that processing happens.  Otherwise, the peer cannot tell when this is called.

So this will always success (I think?) but the underlying page cannot be freed until the other end unmaps it (whether because of the notification of some other reason).

What is the status of the memory at start_address between the call to xengntshr_munmap and the otherend doing xengnttab_munmap?

What is the status of the mapping made by the peer via xengnttab_map_grant_ref_notify in that same interval?

Who is responsible for reclaiming the underlying memory, the kernel or the process?

Thanks,
Ian.

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

#63 lore raw

Re: [PATCH XEN v5 19/23] tools/libs/call: Update some log messages to not refer to xc.

In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper
Quoted 2 lines · Ian Campbell

I agree with the sentiment, but the error message refer to the libxencall entry API, so a developer can match it back to the call in their code.

The PERROR will prepend "xencall" onto the message.

This osdep_alloc_pages can be called from either xencall_alloc_buffer_pages or xencall_alloc_buffer, so I don't think it is possible to make them more specific than that "xencall: alloc_pages". (The old message was similarly misleading in that regard)

Ian.

Quoted 24 lines

~Andrew

Quoted 21 lines

---
 tools/libs/call/linux.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libs/call/linux.c +1 -1
index 906ca7e..80b505c 100644
@@ -88,7 +88,7 @@ void *osdep_alloc_pages(xencall_handle *xcall,
 unsigned int npages)
      p = mmap(NULL, size, PROT_READ|PROT_WRITE,
 MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
      if ( p == MAP_FAILED )
      {
-        PERROR("xc_alloc_hypercall_buffer: mmap failed");
+        PERROR("alloc_pages: mmap failed");
          return NULL;

     }

tools/libs/call/linux.c +0 -0
@@ -97,7 +97,7 @@ void *osdep_alloc_pages(xencall_handle *xcall,
 unsigned int npages)
Quoted 9 lines
Quoted 8 lines
tools/libs/call/linux.c +1 -1
      rc = madvise(p, npages * PAGE_SIZE, MADV_DONTFORK);
      if ( rc < 0 )
      {
-        PERROR("xc_alloc_hypercall_buffer: madvise failed");
+        PERROR("alloc_pages: madvise failed");
          goto out;
      }

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

#18 lore raw

Re: [PATCH XEN v5 07/23] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab.

In reply to Ian Jackson
Quoted 3 lines · Ian Jackson
Quoted 2 lines

XXX consider combining into a single namespace (i.e. with xengnttab_handle having two open fd's in it on Linux)

[...]

Quoted 1 line

I conclude it would be better to unify them.

Starting to look into this I have some queries on the direction I ought to take.

Currently the real world implementations are such that a given installation either provides:
* Neither gnttab nor gntshr (*BSD)
* Only gnttab, not gntshr (mini-os, older Linux versions)
* Both gnttab and ghtshr (newer Linux versions)

In particular gntshr but not gnttab is not found in any real world system. Should I plan for such systems existing in the API? I think I probably should.

A second question is then what functionality should be provided for callers who only want either gnttab or gntshr, but not both. (The canonical example would be the xc_gntshr_* compat layer in libxenctrl, there may be others).

Previously code could rely on either xc_gnt{tab,shr}_open failing if the underlying interface was not present, and react accordingly, which might involve disabling functionality entirely (in such a way as the other calls to xc_gnt{tab,shr}_* do not need to handle ENOSYS, including by calling exit(1)).

Now xengnttab_open could succeed because the other interface is available, which may cause these applications to think they have an interface which they in reality do not => confusion.

Options I can think of:

* A set of open flags, either FOO_MANDATORY or FOO_ONLY (the latter allowing us to skip opening the relevant interface).
* Provide an interface to query the actual functionality which a given handle supports, so apps can react accordingly (e.g. by closing it again and erroring)
* Force all callers to adjust to handling ENOSYS as part of this transition

Opinions?

Ian.

#19 lore raw

Re: [PATCH XEN v5 07/23] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab.

On Mon, 2015-11-23 at 17:05 +0000, Ian Campbell wrote:

Adding Daniel who I should have included the first time, plus a few more thoughts from me at the end.

Quoted 44 lines · Ian Jackson
Quoted 3 lines
Quoted 2 lines

XXX consider combining into a single namespace (i.e. with xengnttab_handle having two open fd's in it on Linux)

[...]

Quoted 1 line

I conclude it would be better to unify them.

Starting to look into this I have some queries on the direction I ought to take.

Currently the real world implementations are such that a given installation either provides:
* Neither gnttab nor gntshr (*BSD)
* Only gnttab, not gntshr (mini-os, older Linux versions)
* Both gnttab and ghtshr (newer Linux versions)

In particular gntshr but not gnttab is not found in any real world system. Should I plan for such systems existing in the API? I think I probably should.

A second question is then what functionality should be provided for callers who only want either gnttab or gntshr, but not both. (The canonical example would be the xc_gntshr_* compat layer in libxenctrl, there may be others).

Previously code could rely on either xc_gnt{tab,shr}_open failing if the underlying interface was not present, and react accordingly, which might involve disabling functionality entirely (in such a way as the other calls to xc_gnt{tab,shr}_* do not need to handle ENOSYS, including by calling exit(1)).

Now xengnttab_open could succeed because the other interface is available, which may cause these applications to think they have an interface which they in reality do not => confusion.

Options I can think of:

* A set of open flags, either FOO_MANDATORY or FOO_ONLY (the latter allowing us to skip opening the relevant interface).
* Provide an interface to query the actual functionality which a given handle supports, so apps can react accordingly (e.g. by closing it again and erroring)
* Force all callers to adjust to handling ENOSYS as part of this transition

Quoted 1 line

Opinions?

Thinking about this some more overnight, it occurred to me that the real issue is that gnttab and gntshr are actually two quite difference APIs. gnttab is all about consuming grant references which are given to you from elsewhere while gntshr is all about creating grant references to give to others. They have relatively little in common wrt the underlying infrastructure, e.g. gnttab is mostly about making GNTTABOP hypercalls while gntshr mostly interacts with the kernel's grant ref allocator with no interaction with the hypervisor.

Maybe that and the API issues above constitute an argument for not combining, I'm really not sure.

Ian.

#17 lore raw

Re: [PATCH XEN v5 07/23] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab.

In reply to Ian Campbell
Quoted 7 lines · Ian Campbell
Quoted 6 lines · Daniel De Graaf
Quoted 5 lines · Ian Campbell
Quoted 4 lines · Ian Jackson

Ian Campbell writes ("[PATCH XEN v5 07/23] tools: Refactor /dev/xen/gnt{dev,shr} wrappers into libxengnttab."):

Quoted 2 lines

libxengnttab will provide a stable API and ABI for accessing the grant table devices.

[...]

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 8 lines

+/**
+ * Memory maps a grant reference from one domain to a local address
range.
+ * Mappings should be unmapped with xengnttab_munmap. If notify_offset
or
+ * notify_port are not -1, this version will attempt to set up an

Quoted 8 lines

unmap
+ * notification at the given offset and event channel. When the page
is
+ * unmapped, the byte at the given offset will be zeroed and a wakeup
will be
+ * sent to the given event channel. Logs errors.

What happens if the unmap notification cannot be set up ?

Also "when the page is unmapped" makes it sound like you mean xengnttab_munmap but actually I think this is when the grant is withdrawn by the grantor ?

If the grant is withdrawn by the grantor, does the page become unuseable immediately ? If so, how can anyone ever use this safely ?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Daniel, could you answer these ones please.

This is intended to allow the kernel to send a close-request notification when the application that allocated the grant page exits without calling a proper shutdown (i.e. it crashes, calls _exit, calls execve, etc).

That is the kernel of the grantor or grantee process? It sounds like grantor tells grantee (who would then be expected to unmap?)

The kernel providing gntalloc is the one that sends the notification, since the other domain (gntdev) must take action to unmap the page.

Quoted 5 lines

Who actually does the unmap, the grantee process or their kernel? I suppose the process, which is expected to be watching for the notification and is required to do the unmap itself. IOW the "munmap notification" is a request to please munmap, not a notification that something has been unmapped out from beneath the calling process.

Correct. The remote process actually does the unmap; there is no back-channel available to notify the kernel of the other side directly (and setting one up would usually be a waste of resources).

Quoted 2 lines

What happens if the unmap notification cannot be set up? Does the call fail (and unmap what it has done) or does it succeed?

The only reason for the setup to fail is if an invalid event channel or offset is specified. It looks like this causes a new mapping to fail (it is freed).

Quoted 23 lines

I think the answer to the other two questions depend on the clarifications above, but I think it is the case that nothing is unmapped automatically, all that this does is give you a result from evtchn_poll etc with the expectation that the caller will call xengnttab_munmap in a controlled way by themselves.

Quoted 17 lines

Without this signal, the kernel has no way to request that the mapper of the page release it, and since Xen has no grant revocation mechanism, the page will likely be tied up until the process on the other side is told to release the page through some other method.

Quoted 11 lines
Quoted 9 lines
Quoted 3 lines

+/*
+ * Creates and shares pages with another domain.
+ *

...

Quoted 3 lines
(quoted diff) +2 -0
+void *xengntshr_share_pages(xengntshr_handle *xgs, uint32_t domid,
+                            int count, uint32_t *refs, int

writable);

Can this fail ? Can it partially succeed ?

Daniel?

Quoted 7 lines
Quoted 4 lines

It can fail if you are out of pages to grant (there is a module parameter that can be adjusted via sysfs for the maximum), or in the unlikely case that the kernel itself is out of room in its grant mapping table (or if the syscall itself encounters -ENOMEM).

Does it either completely succeed or undo partial work, or does it return partial success somehow?

The limit is checked (and the current count updated) prior to making any changes.

Quoted 32 lines
Quoted 24 lines
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines

+/*
+ * Creates and shares a page with another domain, with unmap notification.
+ *
+ * @parm xgs a handle to an open grant sharing instance
+ * @parm domid the domain to share memory with
+ * @parm refs the grant reference of the pages (output)
+ * @parm writable true if the other domain can write to the page
+ * @parm notify_offset The byte offset in the page to use for unmap
+ * notification; -1 for none.
+ * @parm notify_port The event channel port to use for unmap

Quoted 8 lines
Quoted 6 lines

notify,
or -1
+ * @return local mapping of the page
+ */
+void *xengntshr_share_page_notify(xengntshr_handle *xgs, uint32_t domid,

What is this `unmap notification' ?

Quoted 1 line

Daniel?

Quoted 7 lines

As mentioned above, it is a way for the kernel to request for the other side to unmap a page. It is probably easiest to understand if looking at the libvchan driver: one byte of the page is a "status" byte, and when the side using xengntshr exits or dies, this byte is set to zero and the event channel is notified. When the peer is woken by the notify and sees the state byte set to zero, it removes its own mappings so that the shared pages can be freed.

Quoted 2 lines

This is the sending end of the notification requested by the caller of xengnttab_map_grant_ref_notify I think?

Yes.

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

+/*
+ * Unmaps the @count pages starting at @start_address, which were mapped by a
+ * call to xengntshr_share_*. Never logs.

Quoted 7 lines

Linewrap in the comment.

Quoted 3 lines

+ */
+int xengntshr_munmap(xengntshr_handle *xgs, void *start_address, uint32_t count);

What effect does this have on the peer ?

Daniel?

Quoted 7 lines
Quoted 3 lines

If this removes the (final copy of the) mapping and a notify offset/port is set, that processing happens. Otherwise, the peer cannot tell when this is called.

So this will always success (I think?) but the underlying page cannot be freed until the other end unmaps it (whether because of the notification of some other reason).

Yes.

Quoted 2 lines

What is the status of the memory at start_address between the call to xengntshr_munmap and the otherend doing xengnttab_munmap?

A reference to the page is held in gntalloc's gref_list; the page itself is not usable by that domain until it can verify (gnttab_query_foreign_access) that the page is not mapped elsewhere. At that point, the page is released back to the kernel MM.

Quoted 2 lines

What is the status of the mapping made by the peer via xengnttab_map_grant_ref_notify in that same interval?

The gnttab_map_grant_ref_notify is an analogous notification for the gnttab device, allowing the application using gntalloc to close the channel when the application that is using it has gone away. The notification in this case is not needed to clean up the grant mapping (that has already been done by the gntdev kernel code), but is useful if the sharing application wishes to clean up (remove the shared pages or perhaps exit) when its peer exits.

Quoted 2 lines

Who is responsible for reclaiming the underlying memory, the kernel or the process?

The kernel: gntalloc.c do_cleanup.

Quoted 3 lines

Thanks,
Ian.

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

Re: [PATCH XEN v5 07/23] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab.

In reply to Ian Campbell
Quoted 11 lines · Ian Campbell

Thinking about this some more overnight, it occurred to me that the real issue is that gnttab and gntshr are actually two quite difference APIs. gnttab is all about consuming grant references which are given to you from elsewhere while gntshr is all about creating grant references to give to others. They have relatively little in common wrt the underlying infrastructure, e.g. gnttab is mostly about making GNTTABOP hypercalls while gntshr mostly interacts with the kernel's grant ref allocator with no interaction with the hypervisor.

Maybe that and the API issues above constitute an argument for not combining, I'm really not sure.

Ian and I discussed this (briefly) in real life and based on the above argumentation we decided that keeping the two APIs separate (but in the same library) was justified, since there is a reasonable enough air gap between their functionality (i.e. consuming vs producing grants).

We did wonder a bit about changing the names to make that divide clearer, but couldn't think of anything especially compelling and decided to stick with the current shade of yellow.

Ian.

#48 lore raw

Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory

In reply to Ian Jackson
Quoted 18 lines · Ian Jackson
Quoted 12 lines

+/*
+ * Maps a range within one domain to a local address range.  Mappings
+ * should be unmapped with munmap and should follow the same rules as mmap
+ * regarding page alignment.
+ *
+ * prot is as for mmap(2).
+ *
+ * Can partially succeed. When a page cannot be mapped, its respective
+ * field in @err is set to the corresponding errno value.
+ *
+ * Returns NULL if no pages can be mapped.

...

Quoted 4 lines

+void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom,
+                           int prot, const xen_pfn_t *arr, int *err, +                           unsigned int num);

Quoted 4 lines

If it returns NULL, will all of *err be filled with errno values ? Ie is all of err[] always written ?

Does this function ever set errno ?

From my reading of the Linux code it appears to be thus:

If it returns NULL it will have set errno. In that case it appears that err is invalid.

If it returns non-NULL then all of err has been written to either 0 or a negative errno val, indicating the state of that particular frame.

errno vals are in the OS space, not the Xen one, for both the global errno and the err[] array vals.

I think the same is true of FreeBSD from my reading of that code.

In both cases the behaviour is a combined property of the behaviours of the libxc functions, the privcmd driver and the hypercall on xen side, where the first two differ a fair bit on different platforms (and call different hypercalls under the hood, with different behaviour there too)

But the sum _seems_ to add up the same in both cases. Mini-OS is simpler but looks to do about the same.

IOW I'm not 100% sure of the above but it seems a) like a sensible/desirable enough behaviour and b) it does look, as far as I can tell, like the code does this.

Quoted 4 lines

This API invites callers to fail to notice partial failures.

How about permitting err==NULL to mean `on partial failure, unmap everything and return NULL setting errno' ?

AFAICT both the FreeBSD and Linux privcmd driver do not handle this case, so we would be looking at adding functionality to the library to paper over it.

Looking at the users it seems that those which map batches of pages get things right, since they naturally tend to be looping over the arrays anyway.

It's the callers who are mapping a single page which tend to get this wrong, many of them are pre-existing bugs but a fair few are as a result of conversions made in this set of series.

In particular xc_map_foreign_pages used to unmap and return NULL with errno set to the value first non-zero errno from the err array, which I suppose is what you were thinking of for setting errno in this case?

I'm considering whether to add a second function in the API to map single pages (since that seems to be the most common usage, and commonly exhibits this error) vs doing as you suggest, which has the downside of throwing away all the other errors (which may be more critical than the first).

I did briefly consider allow err == NULL iff nr == 1, but that seems worse than both the other options.

Ian.

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

#51 lore raw

Re: [PATCH XEN v5 14/23] tools: foreignmemory: provide xenforeignmemory_unmap.

In reply to Ian Jackson
Quoted 11 lines · Ian Jackson

Ian Campbell writes ("[PATCH XEN v5 14/23] tools: foreignmemory: provide xenforeignmemory_unmap."):

Quoted 9 lines

And require it be used instead of direct munmap.

This will allow e.g. Valgrind hooks to help track incorrect use of foreign mappings.

Switch all uses of xenforeignmemory_map to use xenforeignmemory_unmap, not that foreign mappings via the libxc compat xc_map_foreign_* interface will not take advantage of this and will need converting.

Quoted 6 lines

...

tools/libs/foreignmemory/include/xenforeignmemory.h void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom, · line 63

Quoted 3 lines
tools/libs/foreignmemory/include/xenforeignmemory.h +3 -0
+int xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                           void *addr, unsigned int num);
+

What kind of errors might this return ?

Same as munmap(2), i.e. only EINVAL according to http://pubs.opengroup.org/
onlinepubs/9699919799/functions/munmap.html

Ian,

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

#41 lore raw

Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory

In reply to Ian Jackson
Quoted 5 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory"):

Quoted 3 lines · Ian Jackson

Are you suggesting:

Quoted 8 lines
Quoted 6 lines

void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom, int prot, const xen_pfn_t *arr, int err_out[num],
                           unsigned int num);

Is that (a var sized array based on another param) really allowed?

Yes, if the array size precedes the array.

Sadly:

    for i in include/xenforeignmemory.h; do \
        gcc -x c -ansi -Wall -Werror -pedantic -I/local/scratch/ianc/devel/libxenctrl-split/xen.git/tools/libs/foreignmemory/../../../tools/include \
              -S -o /dev/null $i || exit 1; \
        echo $i; \
    done >headers.chk.new
    include/xenforeignmemory.h:142:28: error: ISO C90 forbids variable length array ‘arr’ [-Werror=vla]
                                const xen_pfn_t *arr[pages], int err[pages]);
                                ^
    include/xenforeignmemory.h:142:28: error: ISO C90 forbids variable length array ‘err’ [-Werror=vla]
    cc1: all warnings being treated as errors
    /local/scratch/ianc/devel/libxenctrl-split/xen.git/tools/libs/foreignmemory/../../../tools/Rules.mk:200: recipe for target 'headers.chk' failed

It's -ansi (AKA -std=c90) together with -pedantic which does it. -std=c99 is happy with it (including with -pedantic).

The original motivation for this check was [0]:
As part of the tidyup, we should choose a particular C standard (89, probably) and ensure that the API/ABI complies with gcc -std=c$VER -pedantic.  This will help to provide a consistent API on other platforms (I seem to recall an effort to port libvchan to windows.)

It's not clear to me how much the "89 probably" was influenced by the "on other platforms ... windows" bit (maybe Andy or Paul has an opinion).

C99 was 16 years ago now, I'm struggling to think of a reason not to move the baseline for tools stuff at least to that.

https://en.wikipedia.org/wiki/Visual_C%2B%2B might be one such reason I suppose, although I'm not convinced a libvchan port to Windows, even if not entirely hypothetical, would be using any of xen.git/tools/libs/* rather than the equivalent frameworks provided by the Windows PV drivers.

Ian.

[0] http://article.gmane.org/gmane.comp.emulators.xen.devel/247265

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

#42 lore raw

Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory

Quoted 30 lines · Ian Campbell
Quoted 5 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory"):

Quoted 3 lines · Ian Jackson

Are you suggesting:

Quoted 7 lines
Quoted 6 lines

void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom,
int prot, const xen_pfn_t *arr, int err_out[num],
unsigned int num);

Is that (a var sized array based on another param) really allowed?

Yes, if the array size precedes the array.

Sadly:

    for i in include/xenforeignmemory.h; do \
        gcc -x c -ansi -Wall -Werror -pedantic -I/local/scratch/ianc/devel/libxenctrl-split/xen.git/tools/libs/foreignmemory/../../../tools/include \
              -S -o /dev/null $i || exit 1; \
        echo $i; \
    done >headers.chk.new
    include/xenforeignmemory.h:142:28: error: ISO C90 forbids variable length array ‘arr’ [-Werror=vla]
                                const xen_pfn_t *arr[pages], int err[pages]);
                                ^
    include/xenforeignmemory.h:142:28: error: ISO C90 forbids variable length array ‘err’ [-Werror=vla]
    cc1: all warnings being treated as errors
    /local/scratch/ianc/devel/libxenctrl-split/xen.git/tools/libs/foreignmemory/../../../tools/Rules.mk:200: recipe for target 'headers.chk' failed

It's -ansi (AKA -std=c90) together with -pedantic which does it. -std=c99 is happy with it (including with -pedantic).

Quoted 8 lines

The original motivation for this check was [0]:
As part of the tidyup, we should choose a particular C standard (89, probably) and ensure that the API/ABI complies with gcc -std=c$VER -pedantic. This will help to provide a consistent API on other platforms (I seem to recall an effort to port libvchan to windows.)

It's not clear to me how much the "89 probably" was influenced by the "on other platforms ... windows" bit (maybe Andy or Paul has an opinion).

I was under the impression that MSVC still doesn't have C99 support.

According to wikipedia, VS2013 gained some C99, and 2015 "achieved a level of the C99 standard compliance in the library that is similar to other C compilers".

According to MSDN about Visual C++:

C99 Conformance: Visual Studio 2015 fully implements the C99 Standard Library, with the exception of any library features that depend on compiler features not yet supported by the Visual C++ compiler (for example, <tgmath.h> is not implemented).

which is wonderfully circular.

According to
https://msdn.microsoft.com/en-us/library/zb1574zs%28v=vs.140%29.aspx
which appears to be the documentation, VLAs are still not supported, even in VS2015.

Quoted 8 lines

C99 was 16 years ago now, I'm struggling to think of a reason not to move the baseline for tools stuff at least to that.

https://en.wikipedia.org/wiki/Visual_C%2B%2B might be one such reason I suppose, although I'm not convinced a libvchan port to Windows, even if not entirely hypothetical, would be using any of xen.git/tools/libs/* rather than the equivalent frameworks provided by the Windows PV drivers.

It would be nice to at least be able to use the same header files, for ease of porting userspace software.

In this case, VLAs are just being used as an aid for the compiler to spot errors. It doesn't change the API/ABI, and could be #ifdef'd around, if we care both for using C99 in general, and Windows support.

~Andrew

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

#43 lore raw

Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory

Quoted 1 line

-----Original Message-----

[snip]

Quoted 12 lines
Quoted 5 lines

C99 was 16 years ago now, I'm struggling to think of a reason not to move the baseline for tools stuff at least to that.

https://en.wikipedia.org/wiki/Visual_C%2B%2B might be one such reason I suppose, although I'm not convinced a libvchan port to Windows, even if

not

Quoted 2 lines

entirely hypothetical, would be using any of xen.git/tools/libs/* rather than the equivalent frameworks provided by the Windows PV drivers.

It would be nice to at least be able to use the same header files, for ease of porting userspace software.

It's possible that libvchan on Windows will make use of the tools/libs headers. As Andy says it would ease porting client software.

Quoted 4 lines

In this case, VLAs are just being used as an aid for the compiler to spot errors. It doesn't change the API/ABI, and could be #ifdef'd around, if we care both for using C99 in general, and Windows support.

We still compile with VS2012 in Citrix and Xen Project uses VS2013 so we can't rely on C99. A #ifdef here does seem like the best solution.

Paul

#45 lore raw

Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory

In reply to a message not in the archive
Quoted 22 lines · Paul Durrant
Quoted 1 line

-----Original Message-----

[snip]

Quoted 8 lines
Quoted 8 lines

C99 was 16 years ago now, I'm struggling to think of a reason not to move
the baseline for tools stuff at least to that.

https://en.wikipedia.org/wiki/Visual_C%2B%2B might be one such reason I
suppose, although I'm not convinced a libvchan port to Windows, even if

Quoted 8 lines

not

Quoted 3 lines

entirely hypothetical, would be using any of xen.git/tools/libs/* rather
than the equivalent frameworks provided by the Windows PV drivers.

It would be nice to at least be able to use the same header files, for ease of porting userspace software.

It's possible that libvchan on Windows will make use of the tools/libs headers. As Andy says it would ease porting client software.

Quoted 7 lines
Quoted 4 lines

In this case, VLAs are just being used as an aid for the compiler to spot errors.  It doesn't change the API/ABI, and could be #ifdef'd around, if we care both for using C99 in general, and Windows support.

We still compile with VS2012 in Citrix and Xen Project uses VS2013 so we can't rely on C99. A #ifdef here does seem like the best solution.

Would you expect new projects (i.e. stuff based on tools/libs) to continue to have requirements to build with those older versions? (Although with Andy's link saying even VS2015 doesn't do VLAs maybe it is a bit moot).

I don't think using VLA here is worth an ifdef, but I think it is worth reordering the arguments such that once we end up with a new enough compiler baseline (in $donkeys years) we can switch without changing the ABI (I think that's the case).

Ian.

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

#47 lore raw

Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory

In reply to a message not in the archive
Quoted 37 lines

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: 30 November 2015 09:52
To: Paul Durrant; Andrew Cooper; Ian Jackson
Cc: Wei Liu; [email protected]; [email protected]
Subject: Re: [PATCH XEN v5 13/23] tools: Refactor foreign memory mapping into libxenforeignmemory

Quoted 7 lines · Paul Durrant
Quoted 1 line

-----Original Message-----

[snip]

Quoted 5 lines
Quoted 5 lines

C99 was 16 years ago now, I'm struggling to think of a reason not to move
the baseline for tools stuff at least to that.

https://en.wikipedia.org/wiki/Visual_C%2B%2B might be one such

reason

Quoted 20 lines
Quoted 11 lines
Quoted 3 lines

I
suppose, although I'm not convinced a libvchan port to Windows, even if

not

Quoted 3 lines

entirely hypothetical, would be using any of xen.git/tools/libs/* rather
than the equivalent frameworks provided by the Windows PV drivers.

It would be nice to at least be able to use the same header files, for ease of porting userspace software.

It's possible that libvchan on Windows will make use of the tools/libs headers. As Andy says it would ease porting client software.

Quoted 4 lines

In this case, VLAs are just being used as an aid for the compiler to spot errors.  It doesn't change the API/ABI, and could be #ifdef'd around, if we care both for using C99 in general, and Windows support.

Quoted 7 lines
Quoted 2 lines

We still compile with VS2012 in Citrix and Xen Project uses VS2013 so we can't rely on C99. A #ifdef here does seem like the best solution.

Would you expect new projects (i.e. stuff based on tools/libs) to continue to have requirements to build with those older versions? (Although with Andy's link saying even VS2015 doesn't do VLAs maybe it is a bit moot).

It would certainly be preferable to have something that did build under a sufficiently old C standard to cover as many environments as is reasonable, and I think any supported MSVC build environment should be included in that set (although I have no problem using #ifdefs to achieve that).

Quoted 5 lines

I don't think using VLA here is worth an ifdef, but I think it is worth reordering the arguments such that once we end up with a new enough compiler baseline (in $donkeys years) we can switch without changing the ABI (I think that's the case).

IIUC correctly the use of VLAs does not change the calling convention in any way, right? It would still mean a pointer to err_out is passed and the use of a VLA is so that bounds checking can be performed? I certainly don't think we should get ourselves into a situation where additions to compilers or C standards do cause changes in the ABI (and we already have to be careful with Windows 32-bit since it defaults to pascal calling convention).

Paul

Quoted 1 line

Ian.

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

#33 lore raw

Re: [PATCH XEN v5 09/23] tools: Refactor hypercall calling wrappers into libxencall.

In reply to Andrew Cooper
Quoted 36 lines · Andrew Cooper
Quoted 35 lines · Ian Campbell
tools/libs/call/linux.c new file +13 -0
new file mode 100644
index 0000000..906ca7e
@@ -0,0 +1,132 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; If not, see <http://www.gnu.org/li

censes/>.
+ *
+ * Split out from xc_linus_osdep.c:
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ */

tools/libs/call/linux.c new file +10 -0
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+
+#include "private.h"
+
Quoted 8 lines

tools/libs/call/linux.c line 32

Quoted 4 lines
tools/libs/call/linux.c new file +4 -0
+int osdep_xencall_open(xencall_handle *xcall)
+{
+    int flags, saved_errno;
+    int fd = open("/proc/xen/privcmd", O_RDWR);

I know this is a straight copy, but these new libraries really should start with the PVOps interface of /dev/xen/privcmd before falling back to the older classic-linux interfaces under /proc

Right, I'll pick this up for (almost, modulo resolving a conflict or two) free when I rebase over Doug's patch to do this, which I'll be doing before reposting.

Quoted 19 lines

Ideally, once all these library improvements are done, there should be no need to mount xenfs in a Linux PVops domain.

Quoted 4 lines

+
+    if ( fd == -1 )
+    {
+        PERROR("Could not obtain handle on privileged command

Quoted 8 lines

interface");
+        return -1;
+    }
+
+    /* Although we return the file handle as the 'xc handle' the API +       does not specify / guarentee that this integer is in fact +       a file handle. Thus we must take responsiblity to ensure +       it doesn't propagate (ie leak) outside the process */

Again, I know this is a straight copy, but it is racy.  The open() call needs O_CLOEXEC.

Quoted 1 line

There might be some issues here to do with older versions of libc.

Or indeed super old versions of Linux (O_CLOEXEC came with 2.6.23 according to man).

I'm tempted to hedge and use O_CLOEXEC with /dev/xen/privcmd, and leave the legacy /proc/xen/privcmd path doing it this way, after all we've managed to get this far...

Quoted 7 lines
Quoted 4 lines

+    /* Do not copy the VMA to child process on fork. Avoid the page being COW
+        on hypercall. */
+    rc = madvise(p, npages * PAGE_SIZE, MADV_DONTFORK);

This also seems racy, although it is unclear from the manpages how to avoid the race.

pthread_atfork() might be one approach, and libxencall already uses pthreads (for locking the cache), so it wouldn't be a problem to add more uses, I don't think.

Ian.

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