From: Andrew Cooper Subject: [PATCH for-4.5 2/2] tools/[lib]xl: Fix `xl vcpu-list` when domains without vcpus exist Date: Mon, 20 Oct 2014 17:21:33 +0100 Message-ID: <1413822093-1621-3-git-send-email-andrew.cooper3@citrix.com> References: <1413822093-1621-1-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1413822093-1621-1-git-send-email-andrew.cooper3@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Xen-devel Cc: Andrew Cooper , Ian Jackson , Ian Campbell , Wei Liu List-Id: xen-devel@lists.xenproject.org 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.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. Reported-by: Euan Harris Signed-off-by: Andrew Cooper CC: Ian Campbell CC: Ian Jackson CC: Wei Liu --- tools/libxl/libxl.c | 8 +++++++- tools/libxl/xl_cmdimpl.c | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index f27b581..d7e2442 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -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)); diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index ea43761..0882644 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -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); -- 1.7.10.4