Fix users of xc_domain_getinfo()
xc_domain_getinfo() has an insane interface, so much so that most in-tree callers get it wrong. This series implements some fixes.
It is RFC particularly to do with the issues of deprecating an API which is currently being used. Thoughts/suggestions appreciated.
RFC PATCH 1/2 tools/xc_domain_getinfo: Fix callsites looking for more than 1 domain.
xc_domain_getinfo has an absolutely insane interface, leading to it being misused by accident at almost all callsites.
This patch renames the current xc_domain_getinfo() sideways to xc_domain_getinfo_many(), and provides a #define for compilation compatibility.
The callsites which are actually using the ability to find more than a specific domain are updated to to the _many() variant, while all other callsites use the compatibility #define for now.
--- tools/console/daemon/io.c | 2 +- tools/libxc/xc_domain.c | 8 ++++---- tools/libxc/xenctrl.h | 13 ++++++++----- tools/python/xen/lowlevel/xc/xc.c | 2 +- tools/xenmon/xenbaked.c | 2 +- 5 files changed, 15 insertions(+), 12 deletions(-)
tools/console/daemon/io.c +1 -1
@@ -758,7 +758,7 @@ static void enum_domains(void) enum_pass++; -while (xc_domain_getinfo (xc, domid,1 , &dominfo) ==1 ) { +while (xc_domain_getinfo_many (xc, domid,1 , &dominfo) ==1 ) { dom =lookup_domain (dominfo.domid);if (dominfo.dying) {if (dom)
tools/libxc/xc_domain.c +4 -4
@@ -271,10 +271,10 @@ out: } -int xc_domain_getinfo (xc_interface *xch, -uint32_t first_domid, -unsigned int max_doms, -xc_dominfo_t *info) +int xc_domain_getinfo_many (xc_interface *xch, +uint32_t first_domid, +unsigned int max_doms, +xc_dominfo_t *info) {unsigned int nr_doms;uint32_t next_domid = first_domid;
tools/libxc/xenctrl.h +8 -5
@@ -576,11 +576,14 @@ int xc_vcpu_getaffinity(xc_interface *xch,* the enumerated domains. * @return the number of domains enumerated or -1 on error */ -int xc_domain_getinfo (xc_interface *xch, -uint32_t first_domid, -unsigned int max_doms, -xc_dominfo_t *info); - +int xc_domain_getinfo_many (xc_interface *xch, +uint32_t first_domid, +unsigned int max_doms, +xc_dominfo_t *info); + +/* Compatability for the moment for out of tree users. External callers +* should move over to using an explicit _single() or _many() */ +#define xc_domain_getinfo (x, f, m, i) xc_domain_getinfo_many((x), (f), (m), (i))/** * This function will set the execution context for the specified vcpu.
tools/python/xen/lowlevel/xc/xc.c +1 -1
@@ -326,7 +326,7 @@ static PyObject *pyxc_domain_getinfo(XcObject *self,if (info ==NULL )return PyErr_NoMemory (); - nr_doms =xc_domain_getinfo (self->xc_handle, first_dom, max_doms, info); + nr_doms =xc_domain_getinfo_many (self->xc_handle, first_dom, max_doms, info);if (nr_doms <0 ) {
tools/xenmon/xenbaked.c +1 -1
@@ -794,7 +794,7 @@ static int indexof(int domid)// call domaininfo hypercall to try and garbage collect unused entries xc_handle =xc_interface_open (0 ,0 ,0 ); - ndomains =xc_domain_getinfo (xc_handle,0 ,NDOMAINS , dominfo); + ndomains =xc_domain_getinfo_many (xc_handle,0 ,NDOMAINS , dominfo);xc_interface_close (xc_handle);// for each domain in our data, look for it in the system dominfo structure
Signature · 1 line
1.7.10.4
Re: [RFC Patch 1/2] tools/xc_domain_getinfo: Fix callsites looking for more than 1 domain.
Quoted 6 lines · Andrew Cooper
xc_domain_getinfohas an absolutely insane interface, leading to it being misused by accident at almost all callsites.This patch renames the current
xc_domain_getinfo()sideways toxc_domain_getinfo_many(), and provides a #define for compilation compatibility.
libxc doesn't have any API guarantees so we don't need to do this for that reason. Of course renaming makes it easier to validate that you have caught all the callsites.
FWIW libxc has a few existing functions which are called foo_exact rather than foo_single.
Quoted 98 lines
The callsites which are actually using the ability to find more than a specific domain are updated to to the
_many()variant, while all other callsites use the compatibility #define for now.Signed-off-by Andrew Cooper <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>--- tools/console/daemon/io.c | 2 +- tools/libxc/xc_domain.c | 8 ++++---- tools/libxc/xenctrl.h | 13 ++++++++----- tools/python/xen/lowlevel/xc/xc.c | 2 +- tools/xenmon/xenbaked.c | 2 +- 5 files changed, 15 insertions(+), 12 deletions(-)tools/console/daemon/io.c +1
-1@@ -758,7 +758,7 @@ static void enum_domains(void) enum_pass++; -while (xc_domain_getinfo (xc, domid,1 , &dominfo) ==1 ) { +while (xc_domain_getinfo_many (xc, domid,1 , &dominfo) ==1 ) { dom =lookup_domain (dominfo.domid);if (dominfo.dying) {if (dom)tools/libxc/xc_domain.c +4
-4@@ -271,10 +271,10 @@ out: } -int xc_domain_getinfo (xc_interface *xch, -uint32_t first_domid, -unsigned int max_doms, -xc_dominfo_t *info) +int xc_domain_getinfo_many (xc_interface *xch, +uint32_t first_domid, +unsigned int max_doms, +xc_dominfo_t *info) {unsigned int nr_doms;uint32_t next_domid = first_domid;tools/libxc/xenctrl.h +8
-5@@ -576,11 +576,14 @@ int xc_vcpu_getaffinity(xc_interface *xch,* the enumerated domains. * @return the number of domains enumerated or -1 on error */ -int xc_domain_getinfo (xc_interface *xch, -uint32_t first_domid, -unsigned int max_doms, -xc_dominfo_t *info); - +int xc_domain_getinfo_many (xc_interface *xch, +uint32_t first_domid, +unsigned int max_doms, +xc_dominfo_t *info); + +/* Compatability for the moment for out of tree users. External callers +* should move over to using an explicit _single() or _many() */ +#define xc_domain_getinfo (x, f, m, i) xc_domain_getinfo_many((x), (f), (m), (i))/** * This function will set the execution context for the specified vcpu. tools/python/xen/lowlevel/xc/xc.c +1
-1@@ -326,7 +326,7 @@ static PyObject *pyxc_domain_getinfo(XcObject *self,if (info ==NULL )return PyErr_NoMemory (); - nr_doms =xc_domain_getinfo (self->xc_handle, first_dom, max_doms, info); + nr_doms =xc_domain_getinfo_many (self->xc_handle, first_dom, max_doms, info);if (nr_doms <0 ) {tools/xenmon/xenbaked.c +1
-1@@ -794,7 +794,7 @@ static int indexof(int domid)// call domaininfo hypercall to try and garbage collect unused entries xc_handle =xc_interface_open (0 ,0 ,0 ); - ndomains =xc_domain_getinfo (xc_handle,0 ,NDOMAINS , dominfo); + ndomains =xc_domain_getinfo_many (xc_handle,0 ,NDOMAINS , dominfo);xc_interface_close (xc_handle);// for each domain in our data, look for it in the system dominfo structure
Re: [RFC Patch 1/2] tools/xc_domain_getinfo: Fix callsites looking for more than 1 domain.
In reply to Andrew CooperAndrew Cooper writes ("[Xen-devel] [RFC Patch 1/2] tools/xc_domain_getinfo: Fix callsites looking for more than 1 domain."):
Quoted 2 lines
xc_domain_getinfohas an absolutely insane interface, leading to it being misused by accident at almost all callsites.
I looked, in these two patches, for what you did to libxl. But I didn't find the relevant code. Have I missed your intent ? You seem to me to be claiming to have adjusted every call site...
Ian.
RFC PATCH 2/2 tools/xc_domain_getinfo: Implement sane interface for single domain information.
In reply to Andrew CooperNow that xc_domain_getinfo_many() exists, implement a rather more sane interface for xc_domain_getinfo(), under the name xc_domain_getinfo_single(), which will only return success if the information is for the requested domid.
As can be seen from the patch, only 3 of the in-tree callers correctly check whether the information they have refers to the correct domain.
There are out-of-tree callers of xc_domain_getinfo, so removing the API is a little antisocial at this point.
--- tools/console/client/main.c | 3 +- tools/console/daemon/io.c | 6 +- tools/debugger/kdd/kdd-xen.c | 2 +- tools/libxc/xc_core.c | 2 +- tools/libxc/xc_cpuid_x86.c | 2 +- tools/libxc/xc_domain.c | 96 +++++++++++++------- tools/libxc/xc_domain_save.c | 4 +- tools/libxc/xc_offline_page.c | 2 +- tools/libxc/xc_pagetab.c | 3 +- tools/libxc/xc_resume.c | 4 +- tools/libxc/xenctrl.h | 12 +++ tools/misc/xen-hvmcrash.c | 4 +- tools/misc/xen-lowmemd.c | 2 +- .../python/xen/lowlevel/checkpoint/libcheckpoint.c | 5 +- tools/xenstore/xenstored_domain.c | 5 +- tools/xentrace/xenctx.c | 4 +- 16 files changed, 97 insertions(+), 59 deletions(-)
tools/console/client/main.c +2 -1
@@ -339,7 +339,8 @@ int main(int argc, char **argv)xc_interface *xc_handle =xc_interface_open (0 ,0 ,0 );if (xc_handle ==NULL )err (errno,"Could not open xc interface" ); -xc_domain_getinfo (xc_handle, domid,1 , &xcinfo); +if (xc_domain_getinfo_single (xc_handle, domid, &xcinfo) !=0 ) +err (errno,"Unable to get domain information" );/* default to pv console for pv guests and serial for hvm guests */ if (xcinfo.hvm) type =CONSOLE_SERIAL ;
tools/console/daemon/io.c +1 -5
@@ -251,13 +251,9 @@ static void buffer_advance(struct buffer *buffer, size_t len)static bool domain_is_valid (int domid) { -bool ret;xc_dominfo_t info; - ret = (xc_domain_getinfo (xc, domid,1 , &info) ==1 && - info.domid == domid); - -return ret; +return xc_domain_getinfo_single (xc, domid, &info) ==0 ; }static int create_hv_log (void )
tools/debugger/kdd/kdd-xen.c +1 -1
@@ -593,7 +593,7 @@ kdd_guest *kdd_guest_init(char *arg, FILE *log, int verbosity) g->domid = domid;/* Check that the domain exists and is HVM */ -if (xc_domain_getinfo (xch, domid,1 , &info) !=1 || !info.hvm) +if (xc_domain_getinfo_single (xch, domid, &info) !=0 || !info.hvm) goto err; snprintf(g->id, (sizeof g->id) - 1,
tools/libxc/xc_core.c +1 -1
@@ -491,7 +491,7 @@ xc_domain_dumpcore_via_callback(xc_interface *xch, goto out; } -if (xc_domain_getinfo (xch, domid,1 , &info) !=1 ) +if (xc_domain_getinfo_single (xch, domid, &info) !=0 ) {PERROR ("Could not get info for domain" ); goto out;
tools/libxc/xc_cpuid_x86.c +1 -1
@@ -557,7 +557,7 @@ static int xc_cpuid_policy( {xc_dominfo_t info; -if (xc_domain_getinfo (xch, domid,1 , &info) ==0 ) +if (xc_domain_getinfo_single (xch, domid, &info) !=0 )return -EINVAL ;if ( info.hvm )
tools/libxc/xc_domain.c +64 -32
108 lines not on this page · raw message
tools/libxc/xc_domain_save.c +2 -2
@@ -365,7 +365,7 @@ static int suspend_and_state(int (*suspend)(void*), void* data,return -1 ; } -if ( (xc_domain_getinfo (xch, dom,1 , info) !=1 ) || +if ( (xc_domain_getinfo_single (xch, dom, info) !=0 ) || !info->shutdown || (info->shutdown_reason != SHUTDOWN_suspend) ) {ERROR ("Domain not in suspended state" );
@@ -919,7 +919,7 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iterreturn 1 ; } -if (xc_domain_getinfo (xch, dom,1 , &info) !=1 ) +if (xc_domain_getinfo_single (xch, dom, &info) !=0 ) {PERROR ("Could not get domain info" );return 1 ;
tools/libxc/xc_offline_page.c +1 -1
@@ -557,7 +557,7 @@ int xc_exchange_page(xc_interface *xch, int domid, xen_pfn_t mfn)uint32_t status;xen_pfn_t new_mfn, gpfn; -if (xc_domain_getinfo (xch, domid,1 , &info) !=1 ) +if (xc_domain_getinfo_single (xch, domid, &info) !=0 ) {ERROR ("Could not get domain info" );return -EFAULT ;
tools/libxc/xc_pagetab.c +1 -2
@@ -35,8 +35,7 @@ unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom,int size, level, pt_levels =2 ;void *map; -if (xc_domain_getinfo (xch, dom,1 , &dominfo) !=1 - || dominfo.domid != dom) +if (xc_domain_getinfo_single (xch, dom, &dominfo) !=0 )return 0 ;/* What kind of paging are we dealing with? */
tools/libxc/xc_resume.c +2 -2
@@ -46,7 +46,7 @@ static int modify_returncode(xc_interface *xch, uint32_t domid)struct domain_info_context *dinfo = &_dinfo;int rc; -if (xc_domain_getinfo (xch, domid,1 , &info) !=1 ) +if (xc_domain_getinfo_single (xch, domid, &info) !=0 ) {PERROR ("Could not get domain info" );return -1 ;
@@ -131,7 +131,7 @@ static int xc_domain_resume_any(xc_interface *xch, uint32_t domid)xen_pfn_t *p2m =NULL ;#endif -if (xc_domain_getinfo (xch, domid,1 , &info) !=1 ) +if (xc_domain_getinfo_single (xch, domid, &info) !=0 ) {PERROR ("Could not get domain info" );return rc;
tools/libxc/xenctrl.h +12 -0
@@ -562,6 +562,18 @@ int xc_vcpu_getaffinity(xc_interface *xch, xc_cpumap_t cpumap);/** +* This function will return information about a single domain. +* +* @parm xch a handle to an open hypervisor interface +* @parm domid the domain to get information information from. +* @parm info an xc_dominfo_t structure to hold the information. +* @return 0 on success or -1 on error +*/ +int xc_domain_getinfo_single (xc_interface *xch, +uint32_t domid, +xc_dominfo_t *info); + +/** * This function will return information about one or more domains. It is * designed to iterate over the list of domains. If a single domain is * requested, this function will return the next domain in the list - if
tools/misc/xen-hvmcrash.c +2 -2
@@ -66,8 +66,8 @@ main(int argc, char **argv)exit (1 ); } - ret =xc_domain_getinfo (xch, domid,1 , &dominfo); -if (ret <0 ) { + ret =xc_domain_getinfo_single (xch, domid, &dominfo); +if (ret !=0 ) {perror ("xc_domain_getinfo" );exit (1 ); }
tools/misc/xen-lowmemd.c +1 -1
@@ -57,7 +57,7 @@ void handle_low_mem(void)return ; diff =THRESHOLD_PG - free_pages; -if (xc_domain_getinfo (xch,0 ,1 , &dom0_info) <1 ) +if (xc_domain_getinfo_single (xch,0 , &dom0_info) !=0 ) {perror ("Failed to get dom0 info" );return ;
tools/python/xen/lowlevel/checkpoint/libcheckpoint.c +2 -3
@@ -97,7 +97,7 @@ int checkpoint_open(checkpoint_state* s, unsigned int domid)return -1 ; } -if (xc_domain_getinfo (s->xch, s->domid,1 , &dominfo) <0 ) { +if (xc_domain_getinfo_single (s->xch, s->domid, &dominfo) !=0 ) {checkpoint_close (s); s->errstr ="could not get domain info" ;
@@ -428,8 +428,7 @@ static int check_shutdown(checkpoint_state* s) {break ; } -if (xc_domain_getinfo (s->xch, s->domid,1 , &info) !=1 - || info.domid != s->domid) { +if (xc_domain_getinfo_single (s->xch, s->domid, &info) !=0 ) {snprintf (errbuf,sizeof (errbuf),"error getting info for domain %u" , s->domid); s->errstr = errbuf;
tools/xenstore/xenstored_domain.c +2 -3
@@ -217,9 +217,8 @@ static void domain_cleanup(void)int notify =0 ;list_for_each_entry_safe (domain, tmp, &domains, list) { -if (xc_domain_getinfo (*xc_handle, domain->domid,1 , - &dominfo) ==1 && - dominfo.domid == domain->domid) { +if (xc_domain_getinfo_single (*xc_handle, domain->domid, + &dominfo) ==0 ) {if ((dominfo.crashed || dominfo.shutdown) && !domain->shutdown) { domain->shutdown =1 ;
tools/xentrace/xenctx.c +2 -2
@@ -896,8 +896,8 @@ int main(int argc, char **argv)exit (-1 ); } - ret =xc_domain_getinfo (xenctx.xc_handle, xenctx.domid,1 , &xenctx.dominfo); -if (ret <0 ) { + ret =xc_domain_getinfo_single (xenctx.xc_handle, xenctx.domid, &xenctx.dominfo); +if (ret !=0 ) {perror ("xc_domain_getinfo" );exit (-1 ); }
Signature · 1 line
1.7.10.4
Re: [RFC Patch 2/2] tools/xc_domain_getinfo: Implement sane interface for single domain information.
Andrew Cooper writes ("[Xen-devel] [RFC Patch 2/2] tools/xc_domain_getinfo: Implement sane interface for single domain information."):
...
Quoted 10 lines
tools/libxc/xc_domain.c +10
-0+static void xc_domaininfo_to_dominfo (const xc_domaininfo_t *src, +xc_dominfo_t *dst) +{ + dst->dying = !!(src->flags & XEN_DOMINF_dying); + dst->shutdown = !!(src->flags & XEN_DOMINF_shutdown); + dst->paused = !!(src->flags & XEN_DOMINF_paused); + dst->blocked = !!(src->flags & XEN_DOMINF_blocked); + dst->running = !!(src->flags & XEN_DOMINF_running); + dst->hvm = !!(src->flags & XEN_DOMINF_hvm_guest); + dst->debugged = !!(src->flags & XEN_DOMINF_debugged);
...
tools/libxc/xc_domain.c int xc_domain_getinfo_many(xc_interface *xch, · line 295
Quoted 2 lines
tools/libxc/xc_domain.c +0
-2- info->dying = !!(domctl.u.getdomaininfo.flags&XEN_DOMINF_dying); - info->shutdown = !!(domctl.u.getdomaininfo.flags&XEN_DOMINF_shutdown);
This code motion makes the patch a bit harder to review than necessary.
Would it be possible to split it into a separate patch ("introduce xc_domaininfo_to_dominfo" I guess), or provide a "git diff -b" ?
I see you are using git. If you had provided a public git url for your branch then I would have just looked t6here and asked git for diff -b ...
Ian.