Fix `xl vcpu-list` with partially created domains
cover Fix `xl vcpu-list` with partially created domains
This series fixes a bug to do with listing the vcpus of a domain lacking any. This can occur if there was an error constructing a domain, and it wasn't successfully cleaned up.
This series fixes two problems. First that getdomaininfo() in Xen previously had no way of expressing "this domain has no vcpus", and second that libxl would blindly allocate memory with an uninitialised value in the case that it encountered a domain in this state.
This bug was discovered against Xen 4.4, but the bug is present in -unstable as well. As such, I flag it for backport ASAP.
Andrew Cooper (2):
xen/domctl: Fix getdomaininfo() of a domain without vcpus tools/[lib]xl: Fix xl vcpu-list when domains without vcpus exist
tools/libxl/libxl.c | 8 +++++++- tools/libxl/xl_cmdimpl.c | 4 +--- xen/common/domctl.c | 1 + xen/include/public/domctl.h | 1 + 4 files changed, 10 insertions(+), 4 deletions(-)
Signature · 1 line
1.7.10.4
PATCH 1/2 xen/domctl: Fix getdomaininfo() of a domain without vcpus
In the case that a domain has not got any vcpus, getdomaininfo() leaves info->max_vcpu_id uninitialised, which causes a problem for toolstacks.
Unfortunately, setting it to 0 as a default is not appropriate. A max_vcpu_id of 0 and nr_online_cpus of 0 is the valid state for a single vcpu domain which is in the process of being destroyed.
As all components are required to add 1 to max_vcpu_id to get the number of vcpus, an id of ~0U is not valid to be used. Explicitly define this as an invalid max vcpu value, and use it to express "no vcpus" in getdomaininfo()
--- xen/common/domctl.c | 1 + xen/include/public/domctl.h | 1 + 2 files changed, 2 insertions(+)
xen/common/domctl.c +1 -0
@@ -156,6 +156,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)struct vcpu_runstate_info runstate; info->domain = d->domain_id; + info->max_vcpu_id =XEN_INVALID_MAX_VCPU_ID ; info->nr_online_vcpus =0 ; info->ssidref =0 ;
xen/include/public/domctl.h +1 -0
@@ -108,6 +108,7 @@ struct xen_domctl_getdomaininfo {uint64_aligned_t shared_info_frame;/* GMFN of shared_info struct */ uint64_aligned_t cpu_time;uint32_t nr_online_vcpus;/* Number of VCPUs currently online. */ +#define XEN_INVALID_MAX_VCPU_ID (~0U)/* Domain has no vcpus? */ uint32_t max_vcpu_id;/* Maximum VCPUID in use by this domain. */ uint32_t ssidref;xen_domain_handle_t handle;
Signature · 1 line
1.7.10.4
Re: [PATCH for-4.5 1/2] xen/domctl: Fix getdomaininfo() of a domain without vcpus
xen/common/domctl.c void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) · line 161
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 20.10.14 at 18:21, <[email protected]> wrote:
xen/common/domctl.c +1
-0@@ -156,6 +156,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)struct vcpu_runstate_info runstate; info->domain = d->domain_id; + info->max_vcpu_id =XEN_INVALID_MAX_VCPU_ID ; info->nr_online_vcpus =0 ; info->ssidref =0 ;
Afaict this will convert the so far random allocation failure in the tool stack to a guaranteed one. Hence I think that despite them touching hypervisor and tools, the patches should be folded to not cause an apparent regression between applying the first and second to people who are lucky in not hitting the allocation failure right now.
In any event the hypervisor side is
Jan
Re: [PATCH for-4.5 1/2] xen/domctl: Fix getdomaininfo() of a domain without vcpus
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 20.10.14 at 18:21, <[email protected]> wrote:
xen/common/domctl.c +0
-0@@ -156,6 +156,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info)struct vcpu_runstate_info runstate;
Quoted 11 lines
xen/common/domctl.c void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) · line 161
Quoted 4 lines
xen/common/domctl.c +1
-0info->domain = d->domain_id; + info->max_vcpu_id =XEN_INVALID_MAX_VCPU_ID ; info->nr_online_vcpus =0 ; info->ssidref =0 ;Afaict this will convert the so far random allocation failure in the tool stack to a guaranteed one. Hence I think that despite them touching hypervisor and tools, the patches should be folded to not cause an apparent regression between applying the first and second to people who are lucky in not hitting the allocation failure right now.
+1
-George
PATCH 2/2 tools/[lib]xl: Fix `xl vcpu-list` when domains without vcpus exist
In reply to Andrew CooperOn a system which looks like this:
[root@st04 ~]# xl list
Name ID Mem VCPUs State Time(s) Domain-0 0 752 4 r----- 46699.3 (null) 1 0 0 --p--- 0.0 (null) 2 0 0 --p--- 0.0 (null) 3 0 0 --p--- 0.0 badger 25 0 1 --p--- 0.0
xl vcpu-list failes as so:
[root@st04 ~]# xl vcpu-list
Name ID VCPU CPU State Time(s) CPU Affinity
Domain-0 0 0 0 -b- 12171.0 all Domain-0 0 1 1 -b- 11779.6 all Domain-0 0 2 2 -b- 11599.0 all Domain-0 0 3 3 r-- 11007.0 all
libxl: critical: libxl__calloc: libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40) : Cannot allocate memory
libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40)
The root cause of this is that, in the case that a domain has no vcpus, libxl_list_vcpu() attempts to calloc() with domaininfo.nr_online_vcpus, which is uninitialised on the stack.
Check domaininfo.nr_online_vcpus against the new sentinel value XEN_INVALID_MAX_VCPU_ID (introduced in the previous patch), and return early. This means that it is now valid for libxl_list_vcpu() to return NULL for a domain which lacks any vcpus.
As part of this change, remove the pointless call to libxl_get_max_cpus(), whose returned value is unconditionally clobbered in the for() loop.
--- tools/libxl/libxl.c | 8 +++++++- tools/libxl/xl_cmdimpl.c | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-)
tools/libxl/libxl.c +7 -1
@@ -5246,7 +5246,13 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,GC_FREE ;return NULL ; } - *nr_cpus_out =libxl_get_max_cpus (ctx); + +if (domaininfo.nr_online_vcpus ==XEN_INVALID_MAX_VCPU_ID ) { + *nr_cpus_out =0 ; +GC_FREE ; +return NULL ; + } + ret = ptr =libxl__calloc (NOGC , domaininfo.max_vcpu_id +1 ,sizeof (libxl_vcpuinfo));
tools/libxl/xl_cmdimpl.c +1 -3
@@ -4813,10 +4813,8 @@ static void print_domain_vcpuinfo(uint32_t domid, uint32_t nr_cpus) vcpuinfo =libxl_list_vcpu (ctx, domid, &nb_vcpu, &nrcpus); -if (!vcpuinfo) { -fprintf (stderr,"libxl_list_vcpu failed.\n" ); +if (!vcpuinfo)return ; - }for (i =0 ; i < nb_vcpu; i++) {print_vcpuinfo (domid, &vcpuinfo[i], nr_cpus);
Signature · 1 line
1.7.10.4
PATCH v2 2/2 tools/[lib]xl: Fix `xl vcpu-list` when domains without vcpus exist
On a system which looks like this:
[root@st04 ~]# xl list
Name ID Mem VCPUs State Time(s) Domain-0 0 752 4 r----- 46699.3 (null) 1 0 0 --p--- 0.0 (null) 2 0 0 --p--- 0.0 (null) 3 0 0 --p--- 0.0 badger 25 0 1 --p--- 0.0
xl vcpu-list failes as so:
[root@st04 ~]# xl vcpu-list
Name ID VCPU CPU State Time(s) CPU Affinity
Domain-0 0 0 0 -b- 12171.0 all Domain-0 0 1 1 -b- 11779.6 all Domain-0 0 2 2 -b- 11599.0 all Domain-0 0 3 3 r-- 11007.0 all
libxl: critical: libxl__calloc: libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40) : Cannot allocate memory
libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40)
The root cause of this is that, in the case that a domain has no vcpus, libxl_list_vcpu() attempts to calloc() with domaininfo.max_vcpu_id, which is uninitialised on the stack.
Check domaininfo.max_vcpu_id against the new sentinel value XEN_INVALID_MAX_VCPU_ID (introduced in the previous patch), and return early. This means that it is now valid for libxl_list_vcpu() to return NULL for a domain which lacks any vcpus.
As part of this change, remove the pointless call to libxl_get_max_cpus(), whose returned value is unconditionally clobbered in the for() loop.
---
v2: s/nr_online_cpus/max_vcpu_id/g - I accidentally sent an unrefreshed patch
--- tools/libxl/libxl.c | 8 +++++++- tools/libxl/xl_cmdimpl.c | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-)
tools/libxl/libxl.c +7 -1
@@ -5246,7 +5246,13 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,GC_FREE ;return NULL ; } - *nr_cpus_out =libxl_get_max_cpus (ctx); + +if (domaininfo.max_vcpu_id ==XEN_INVALID_MAX_VCPU_ID ) { + *nr_cpus_out =0 ; +GC_FREE ; +return NULL ; + } + ret = ptr =libxl__calloc (NOGC , domaininfo.max_vcpu_id +1 ,sizeof (libxl_vcpuinfo));
tools/libxl/xl_cmdimpl.c +1 -3
@@ -4813,10 +4813,8 @@ static void print_domain_vcpuinfo(uint32_t domid, uint32_t nr_cpus) vcpuinfo =libxl_list_vcpu (ctx, domid, &nb_vcpu, &nrcpus); -if (!vcpuinfo) { -fprintf (stderr,"libxl_list_vcpu failed.\n" ); +if (!vcpuinfo)return ; - }for (i =0 ; i < nb_vcpu; i++) {print_vcpuinfo (domid, &vcpuinfo[i], nr_cpus);
Signature · 1 line
1.7.10.4
Re: [PATCH for-4.5 v2 2/2] tools/[lib]xl: Fix `xl vcpu-list` when domains without vcpus exist
Quoted 52 lines · Andrew Cooper
On a system which looks like this:
[root@st04 ~]# xl list
Name ID Mem VCPUs State Time(s) Domain-0 0 752 4 r----- 46699.3 (null) 1 0 0 --p--- 0.0 (null) 2 0 0 --p--- 0.0 (null) 3 0 0 --p--- 0.0 badger 25 0 1 --p--- 0.0
xl vcpu-listfailes as so:[root@st04 ~]# xl vcpu-list
Name ID VCPU CPU State Time(s) CPU AffinityDomain-0 0 0 0 -b- 12171.0 all Domain-0 0 1 1 -b- 11779.6 all Domain-0 0 2 2 -b- 11599.0 all Domain-0 0 3 3 r-- 11007.0 alllibxl: critical:
libxl__calloc: libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40) : Cannot allocate memory
libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40)The root cause of this is that, in the case that a domain has no vcpus,
libxl_list_vcpu()attempts tocalloc()with domaininfo.max_vcpu_id, which is uninitialised on the stack.Check domaininfo.max_vcpu_id against the new sentinel value
XEN_INVALID_MAX_VCPU_ID(introduced in the previous patch), and return early. This means that it is now valid forlibxl_list_vcpu()to return NULL for a domain which lacks any vcpus.As part of this change, remove the pointless call to
libxl_get_max_cpus(), whose returned value is unconditionally clobbered in thefor()loop.Reported-by Euan Harris <[email protected]>Signed-off-by Andrew Cooper <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>CC Wei Liu <[email protected]>---
v2: s/nr_online_cpus/max_vcpu_id/g - I accidentally sent an unrefreshed patch--- tools/libxl/libxl.c | 8 +++++++- tools/libxl/xl_cmdimpl.c | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-)tools/libxl/libxl.c +0
-0
tools/libxl/libxl.c libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid, · line 5251
Quoted 7 lines
tools/libxl/libxl.c +3
-1@@ -5246,7 +5246,13 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,GC_FREE ;return NULL ; } - *nr_cpus_out =libxl_get_max_cpus (ctx); + +if (domaininfo.max_vcpu_id ==XEN_INVALID_MAX_VCPU_ID ) { + *nr_cpus_out =0 ;
Do you not need to set nr_vcpus_out to 0 as well?
Wei.
Re: [PATCH for-4.5 v2 2/2] tools/[lib]xl: Fix `xl vcpu-list` when domains without vcpus exist
Quoted 48 lines · Wei Liu
Quoted 47 lines · Andrew Cooper
On a system which looks like this:
[root@st04 ~]# xl list
Name ID Mem VCPUs State Time(s) Domain-0 0 752 4 r----- 46699.3 (null) 1 0 0 --p--- 0.0 (null) 2 0 0 --p--- 0.0 (null) 3 0 0 --p--- 0.0 badger 25 0 1 --p--- 0.0
xl vcpu-listfailes as so:[root@st04 ~]# xl vcpu-list
Name ID VCPU CPU State Time(s) CPU AffinityDomain-0 0 0 0 -b- 12171.0 all Domain-0 0 1 1 -b- 11779.6 all Domain-0 0 2 2 -b- 11599.0 all Domain-0 0 3 3 r-- 11007.0 alllibxl: critical:
libxl__calloc: libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40) : Cannot allocate memory
libxl: FATAL ERROR: memory allocation failure (libxl__calloc, 4294935299 x 40)The root cause of this is that, in the case that a domain has no vcpus,
libxl_list_vcpu()attempts tocalloc()with domaininfo.max_vcpu_id, which is uninitialised on the stack.Check domaininfo.max_vcpu_id against the new sentinel value
XEN_INVALID_MAX_VCPU_ID(introduced in the previous patch), and return early. This means that it is now valid forlibxl_list_vcpu()to return NULL for a domain which lacks any vcpus.As part of this change, remove the pointless call to
libxl_get_max_cpus(), whose returned value is unconditionally clobbered in thefor()loop.Reported-by Euan Harris <[email protected]>Signed-off-by Andrew Cooper <[email protected]>CC Ian Campbell <[email protected]>CC Ian Jackson <[email protected]>CC Wei Liu <[email protected]>---
v2: s/nr_online_cpus/max_vcpu_id/g - I accidentally sent an unrefreshed patch--- tools/libxl/libxl.c | 8 +++++++- tools/libxl/xl_cmdimpl.c | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-)
Quoted 13 lines
tools/libxl/libxl.c libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid, · line 5251
Quoted 12 lines
tools/libxl/libxl.c +3
-1@@ -5246,7 +5246,13 @@ libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,GC_FREE ;return NULL ; } - *nr_cpus_out =libxl_get_max_cpus (ctx); + +if (domaininfo.max_vcpu_id ==XEN_INVALID_MAX_VCPU_ID ) { + *nr_cpus_out =0 ;Do you not need to set
nr_vcpus_outto 0 as well?
Hmm - probably not. NULL should do.
I will respin a v3 with the hypervisor and tool sides merged
~Andrew
Re: [PATCH for-4.5 v2 2/2] tools/[lib]xl: Fix `xl vcpu-list` when domains without vcpus exist
On Tue, Oct 21, 2014 at 02:08:37PM +0100, Andrew Cooper wrote:
[...]
Quoted 11 lines
Quoted 8 lines
tools/libxl/libxl.c libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid, · line 5251
Quoted 7 lines
GC_FREE; return NULL; }tools/libxl/libxl.c +3
-1- *nr_cpus_out =libxl_get_max_cpus (ctx); + +if (domaininfo.max_vcpu_id ==XEN_INVALID_MAX_VCPU_ID ) { + *nr_cpus_out =0 ;Do you not need to set
nr_vcpus_outto 0 as well?Hmm - probably not. NULL should do.
Just setting one out parameter not the other looks a bit odd to me. Nothing fundamental wrong with that though, as you said, it's mostly the NULL return value indicates failure.
Wei.