From nobody Sun Feb 8 22:49:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1536238877751787.8287460283669; Thu, 6 Sep 2018 06:01:17 -0700 (PDT) Received: from localhost ([::1]:33168 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fxtuJ-0005tV-9b for importer@patchew.org; Thu, 06 Sep 2018 09:01:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52926) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fxttA-0005SR-46 for qemu-devel@nongnu.org; Thu, 06 Sep 2018 09:00:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fxtt6-0006ya-2z for qemu-devel@nongnu.org; Thu, 06 Sep 2018 09:00:00 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:52496 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fxtt5-0006xw-Tb for qemu-devel@nongnu.org; Thu, 06 Sep 2018 08:59:56 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3EDF14021700; Thu, 6 Sep 2018 12:59:55 +0000 (UTC) Received: from dell-r430-03.lab.eng.brq.redhat.com (dell-r430-03.lab.eng.brq.redhat.com [10.37.153.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8EB2F2027EA0; Thu, 6 Sep 2018 12:59:54 +0000 (UTC) From: Igor Mammedov To: qemu-devel@nongnu.org Date: Thu, 6 Sep 2018 14:51:54 +0200 Message-Id: <1536238314-108139-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 06 Sep 2018 12:59:55 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Thu, 06 Sep 2018 12:59:55 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'imammedo@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2] qga: ignore non present cpus when handling qmp_guest_get_vcpus() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lersek@redhat.com, mdroth@linux.vnet.ibm.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If VM has VCPUs plugged sparselly (for example a VM started with 3 VCPUs (cpu0, cpu1 and cpu2) and then cpu1 was hotunplugged so only cpu0 and cpu2 are present), QGA will rise a error error: internal error: unable to execute QEMU agent command 'guest-get-vc= pus': open("/sys/devices/system/cpu/cpu1/"): No such file or directory when virsh vcpucount FOO --guest is executed. Fix it by ignoring non present CPUs when fetching CPUs status from sysfs. Signed-off-by: Igor Mammedov Reviewed-by: Laszlo Ersek Reviewed-by: Marc-Andr=C3=A9 Lureau --- v2: do not create CPU entry if cpu isn't present (Laszlo Ersek ) --- qga/commands-posix.c | 115 ++++++++++++++++++++++++++---------------------= ---- 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 37e8a2d..42d30f0 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -2035,61 +2035,56 @@ static long sysconf_exact(int name, const char *nam= e_str, Error **errp) * Written members remain unmodified on error. */ static void transfer_vcpu(GuestLogicalProcessor *vcpu, bool sys2vcpu, - Error **errp) + char *dirpath, Error **errp) { - char *dirpath; + int fd; + int res; int dirfd; + static const char fn[] =3D "online"; =20 - dirpath =3D g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId64 "/", - vcpu->logical_id); dirfd =3D open(dirpath, O_RDONLY | O_DIRECTORY); if (dirfd =3D=3D -1) { error_setg_errno(errp, errno, "open(\"%s\")", dirpath); - } else { - static const char fn[] =3D "online"; - int fd; - int res; - - fd =3D openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR); - if (fd =3D=3D -1) { - if (errno !=3D ENOENT) { - error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, = fn); - } else if (sys2vcpu) { - vcpu->online =3D true; - vcpu->can_offline =3D false; - } else if (!vcpu->online) { - error_setg(errp, "logical processor #%" PRId64 " can't be " - "offlined", vcpu->logical_id); - } /* otherwise pretend successful re-onlining */ - } else { - unsigned char status; - - res =3D pread(fd, &status, 1, 0); - if (res =3D=3D -1) { - error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath,= fn); - } else if (res =3D=3D 0) { - error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpa= th, - fn); - } else if (sys2vcpu) { - vcpu->online =3D (status !=3D '0'); - vcpu->can_offline =3D true; - } else if (vcpu->online !=3D (status !=3D '0')) { - status =3D '0' + vcpu->online; - if (pwrite(fd, &status, 1, 0) =3D=3D -1) { - error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dir= path, - fn); - } - } /* otherwise pretend successful re-(on|off)-lining */ + return; + } =20 - res =3D close(fd); - g_assert(res =3D=3D 0); - } + fd =3D openat(dirfd, fn, sys2vcpu ? O_RDONLY : O_RDWR); + if (fd =3D=3D -1) { + if (errno !=3D ENOENT) { + error_setg_errno(errp, errno, "open(\"%s/%s\")", dirpath, fn); + } else if (sys2vcpu) { + vcpu->online =3D true; + vcpu->can_offline =3D false; + } else if (!vcpu->online) { + error_setg(errp, "logical processor #%" PRId64 " can't be " + "offlined", vcpu->logical_id); + } /* otherwise pretend successful re-onlining */ + } else { + unsigned char status; + + res =3D pread(fd, &status, 1, 0); + if (res =3D=3D -1) { + error_setg_errno(errp, errno, "pread(\"%s/%s\")", dirpath, fn); + } else if (res =3D=3D 0) { + error_setg(errp, "pread(\"%s/%s\"): unexpected EOF", dirpath, + fn); + } else if (sys2vcpu) { + vcpu->online =3D (status !=3D '0'); + vcpu->can_offline =3D true; + } else if (vcpu->online !=3D (status !=3D '0')) { + status =3D '0' + vcpu->online; + if (pwrite(fd, &status, 1, 0) =3D=3D -1) { + error_setg_errno(errp, errno, "pwrite(\"%s/%s\")", dirpath, + fn); + } + } /* otherwise pretend successful re-(on|off)-lining */ =20 - res =3D close(dirfd); + res =3D close(fd); g_assert(res =3D=3D 0); } =20 - g_free(dirpath); + res =3D close(dirfd); + g_assert(res =3D=3D 0); } =20 GuestLogicalProcessorList *qmp_guest_get_vcpus(Error **errp) @@ -2107,17 +2102,21 @@ GuestLogicalProcessorList *qmp_guest_get_vcpus(Erro= r **errp) while (local_err =3D=3D NULL && current < sc_max) { GuestLogicalProcessor *vcpu; GuestLogicalProcessorList *entry; - - vcpu =3D g_malloc0(sizeof *vcpu); - vcpu->logical_id =3D current++; - vcpu->has_can_offline =3D true; /* lolspeak ftw */ - transfer_vcpu(vcpu, true, &local_err); - - entry =3D g_malloc0(sizeof *entry); - entry->value =3D vcpu; - - *link =3D entry; - link =3D &entry->next; + int64_t id =3D current++; + char *path =3D g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId= 64 "/", + id); + + if (g_file_test(path, G_FILE_TEST_EXISTS)) { + vcpu =3D g_malloc0(sizeof *vcpu); + vcpu->logical_id =3D id; + vcpu->has_can_offline =3D true; /* lolspeak ftw */ + transfer_vcpu(vcpu, true, path, &local_err); + entry =3D g_malloc0(sizeof *entry); + entry->value =3D vcpu; + *link =3D entry; + link =3D &entry->next; + } + g_free(path); } =20 if (local_err =3D=3D NULL) { @@ -2138,7 +2137,11 @@ int64_t qmp_guest_set_vcpus(GuestLogicalProcessorLis= t *vcpus, Error **errp) =20 processed =3D 0; while (vcpus !=3D NULL) { - transfer_vcpu(vcpus->value, false, &local_err); + char *path =3D g_strdup_printf("/sys/devices/system/cpu/cpu%" PRId= 64 "/", + vcpus->value->logical_id); + + transfer_vcpu(vcpus->value, false, path, &local_err); + g_free(path); if (local_err !=3D NULL) { break; } --=20 2.7.4