Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6BD58C63777 for ; Fri, 20 Nov 2020 09:30:43 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9AEB322226 for ; Fri, 20 Nov 2020 09:30:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9AEB322226 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:38236 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kg2kb-0007Yc-7W for qemu-devel@archiver.kernel.org; Fri, 20 Nov 2020 04:30:41 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:37992) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kg2if-0005nW-DS for qemu-devel@nongnu.org; Fri, 20 Nov 2020 04:28:41 -0500 Received: from mx2.suse.de ([195.135.220.15]:53872) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kg2id-0006Kk-Fm for qemu-devel@nongnu.org; Fri, 20 Nov 2020 04:28:41 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id E30EBAC60; Fri, 20 Nov 2020 09:28:36 +0000 (UTC) MIME-Version: 1.0 Date: Fri, 20 Nov 2020 09:28:36 +0000 From: Lin Ma To: =?UTF-8?Q?Marc-Andr=C3=A9_Lureau?= Subject: Re: [PATCH] qga: Correct loop count in qmp_guest_get_vcpus() In-Reply-To: References: <20201119084557.27870-1-lma@suse.com> User-Agent: Roundcube Webmail Message-ID: <7594e6cebc51b395c98dbc8714beb7ff@suse.de> X-Sender: lma@suse.de Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=195.135.220.15; envelope-from=lma@suse.de; helo=mx2.suse.de X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , QEMU , Lin Ma Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" On 2020-11-19 14:46, Marc-André Lureau wrote: > Hi > > On Thu, Nov 19, 2020 at 12:48 PM Lin Ma wrote: > >> The guest-get-vcpus returns incorrect vcpu info in case we hotunplug >> vcpus(not >> the last one). >> e.g.: >> A VM has 4 VCPUs: cpu0 + 3 hotunpluggable online vcpus(cpu1, cpu2 and >> cpu3). >> Hotunplug cpu2, Now only cpu0, cpu1 and cpu3 are present & online. >> >> ./qmp-shell /tmp/qmp-monitor.sock >> (QEMU) query-hotpluggable-cpus >> {"return": [ >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 3}, >> "vcpus-count": 1, >> "qom-path": "/machine/peripheral/cpu3", "type": "host-x86_64-cpu"}, >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 2}, >> "vcpus-count": 1, >> "qom-path": "/machine/peripheral/cpu2", "type": "host-x86_64-cpu"}, >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 1}, >> "vcpus-count": 1, >> "qom-path": "/machine/peripheral/cpu1", "type": "host-x86_64-cpu"}, >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 0}, >> "vcpus-count": 1, >> "qom-path": "/machine/unattached/device[0]", "type": >> "host-x86_64-cpu"} >> ]} >> >> (QEMU) device_del id=cpu2 >> {"return": {}} >> >> (QEMU) query-hotpluggable-cpus >> {"return": [ >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 3}, >> "vcpus-count": 1, >> "qom-path": "/machine/peripheral/cpu3", "type": "host-x86_64-cpu"}, >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 2}, >> "vcpus-count": 1, >> "type": "host-x86_64-cpu"}, >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 1}, >> "vcpus-count": 1, >> "qom-path": "/machine/peripheral/cpu1", "type": "host-x86_64-cpu"}, >> {"props": {"core-id": 0, "thread-id": 0, "socket-id": 0}, >> "vcpus-count": 1, >> "qom-path": "/machine/unattached/device[0]", "type": >> "host-x86_64-cpu"} >> ]} >> >> Before: >> ./qmp-shell -N /tmp/qmp-ga.sock >> Welcome to the QMP low-level shell! >> Connected >> (QEMU) guest-get-vcpus >> {"return": [ >> {"online": true, "can-offline": false, "logical-id": 0}, >> {"online": true, "can-offline": true, "logical-id": 1}]} >> >> After: >> ./qmp-shell -N /tmp/qmp-ga.sock >> Welcome to the QMP low-level shell! >> Connected >> (QEMU) guest-get-vcpus >> {"execute":"guest-get-vcpus"} >> {"return": [ >> {"online": true, "can-offline": false, "logical-id": 0}, >> {"online": true, "can-offline": true, "logical-id": 1}, >> {"online": true, "can-offline": true, "logical-id": 3}]} >> >> Signed-off-by: Lin Ma >> --- >> qga/commands-posix.c | 8 +++++--- >> 1 file changed, 5 insertions(+), 3 deletions(-) >> >> diff --git a/qga/commands-posix.c b/qga/commands-posix.c >> index 3bffee99d4..accc893373 100644 >> --- a/qga/commands-posix.c >> +++ b/qga/commands-posix.c >> @@ -2182,15 +2182,15 @@ GuestLogicalProcessorList >> *qmp_guest_get_vcpus(Error **errp) >> { >> int64_t current; >> GuestLogicalProcessorList *head, **link; >> - long sc_max; >> + long max_loop_count; >> Error *local_err = NULL; >> >> current = 0; >> head = NULL; >> link = &head; >> - sc_max = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err); >> + max_loop_count = SYSCONF_EXACT(_SC_NPROCESSORS_CONF, &local_err); >> >> - while (local_err == NULL && current < sc_max) { >> + while (local_err == NULL && current < max_loop_count) { >> GuestLogicalProcessor *vcpu; >> GuestLogicalProcessorList *entry; >> int64_t id = current++; >> @@ -2206,6 +2206,8 @@ GuestLogicalProcessorList >> *qmp_guest_get_vcpus(Error >> **errp) >> entry->value = vcpu; >> *link = entry; >> link = &entry->next; >> + } else { >> + max_loop_count += 1; >> > > This looks like a recipe for infinite loop on error. Emm...It is possible. > > Shouldn't we loop over all the /sys/devices/system/cpu/cpu#/ instead? Originally I'd like to use the function fnmatch to handle pattern cpu# to loop over all of the /sys/devices/system/cpu/cpu#/, But it introduces the header file fnmatch.h and make things complicated a little. > > (possibly parse /sys/devices/system/cpu/present, but I doubt it's > necessary) IMO the 'present' won't help. I'm about to post the V2, I made tiny change in the V2, Please help to review. BTW, The local_err will be set in case of error, right? It could avoid infinite loop. Thanks a lot, Lin