From nobody Tue Sep 9 09:54:11 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1745923697; cv=none; d=zohomail.com; s=zohoarc; b=ef3KXftZBQb59kYQO/MbzwzbveHAs4bZ84OXeiJytuHLAYbAnWFjjj6rdg4seqA36PJuuU4L3Yc88rh1hQX+WVakZNoUKt61Ipmt7WTcbC/xMXh/ssByGiWWxmhr3+FTInEAwSxw2vwTIbuPhcFNLMNGa5d4tgW+8rG5z7e6U+k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745923697; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=xwA8gLNj4EgQWJRL6lQlb1J91w1AxqlfLmVJC6eFWkA=; b=IWL+MKSk3pf36mBLKtECCsnhCD+eejySTn74ZFej1a1vrgr1vyDYd7iA9FJJlNUgn35jiQjoeApB5Bie3gEKOGUR6znZy6h8gZ5ntKXpPyBSCkvHve3vUG+DsVlu++L+guDQFvxBh+ZKxkK5jmbxCEUGocDUc9B0LtrcCdh9DoI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1745923697572220.1235285355133; Tue, 29 Apr 2025 03:48:17 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 00B939AC; Tue, 29 Apr 2025 06:48:16 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 5965712CB; Tue, 29 Apr 2025 06:21:35 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 7034613E2; Tue, 29 Apr 2025 06:21:25 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 3FFEC1287 for ; Tue, 29 Apr 2025 06:20:27 -0400 (EDT) Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-648--7J0v-rSMgSWKEuchr4NhQ-1; Tue, 29 Apr 2025 06:20:25 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E40F51800370 for ; Tue, 29 Apr 2025 10:20:24 +0000 (UTC) Received: from orkuz (unknown [10.43.3.115]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7D6B730001A2 for ; Tue, 29 Apr 2025 10:20:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745922026; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CsulxKyWLNXa44NGllEqu+oonqDkNY2+etE88c4KbcU=; b=G3OR5GO17Oc9rIcm9bQ9RrQkhvIdxWd7l19FwXjV1vyM21vsEmoowciyDUpidelvfobTPc BClWDZ5XhfAHxex6exQwqDe++6kXU+QrSzVvYCCLsz5Q9bSJNpWIvWgXtYZZ54Mc2fz7HC j6PbwjpaE/joLsVME2UbIN2YX8/8jlk= X-MC-Unique: -7J0v-rSMgSWKEuchr4NhQ-1 X-Mimecast-MFC-AGG-ID: -7J0v-rSMgSWKEuchr4NhQ_1745922025 To: devel@lists.libvirt.org Subject: [PATCH 15/15] cpu_x86: Fix algorithm for computing CPU model weight Date: Tue, 29 Apr 2025 12:19:50 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: hbyNRfFEcEn-tYXLnb5TSvaY0oKz4D8RS5si4KsETmg_1745922025 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: YVFA6TAPV32UZLEF5JI7PU6OEC574CFZ X-Message-ID-Hash: YVFA6TAPV32UZLEF5JI7PU6OEC574CFZ X-MailFrom: jdenemar@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: From: Jiri Denemark via Devel Reply-To: Jiri Denemark X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1745923698449019000 Content-Type: text/plain; charset="utf-8" From: Jiri Denemark This patch is effectively a NOP, but it fixes a logic bug and makes the heuristics more visible and easier to change should there be a need to do so in the future. We decide which CPU model is the best match for given CPU data by comparing lists of features that need to be enabled/disabled on top of the selected CPU model. Since the original approach of using just the total number of features was not working well enough, commit v8.3.0-42-g48341b025a implemented a penalty for disabled features which would increase for each additional disabled features. Apparently the intention was weighting disabled features as disabled * (disabled + 3) weightDisabled =3D ------------------------- 2 and complete CPU model as weight =3D enabled + weightDisabled But there was a bug in the code which caused it to ignore some of the features and counted as enabled regardless on their policy. Instead of going through all features the code used the number of "enabled" features (the variable was not really counting number of enabled features though) which was initialized to the total number of features and decremented each time a disabled features was found. Thus depending on the number of disabled features, some features at the end of the list were ignored. Luckily we know all the ignored features had to be disabled because the CPU definitions were created by x86DataToCPU which constructs a list of enabled features followed by disabled features. So to fix the bug while providing the same results we can come up with an equivalent formula using properly counted features in the CPU definition. The number of disabled features counted by the buggy code is half =3D (disabled + 1) div 2 and the weight of all disabled features is half * (half + 3) weightDisabled =3D ----------------- 2 When computing the total weight, we can't no longer use number of enabled features because the original code counted some of the disabled features as enabled. So to match the old behavior, we count the total weight as weight =3D features - half + weightDisabled The weight of enabled features now differs from the value computed by the old code, but we don't need to worry about it as it's not really used anywhere except for logging. Fixes: https://gitlab.com/libvirt/libvirt/-/issues/759 Signed-off-by: Jiri Denemark Reviewed-by: Peter Krempa --- src/cpu/cpu_x86.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index b0fe2bed4c..213af67ea4 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -2087,9 +2087,6 @@ virCPUx86Compare(virCPUDef *host, } =20 =20 -/* Base penalty for disabled features. */ -#define BASE_PENALTY 2 - struct virCPUx86Weight { size_t total; size_t enabled; @@ -2100,26 +2097,25 @@ static void virCPUx86WeightFeatures(const virCPUDef *cpu, struct virCPUx86Weight *weight) { - int penalty =3D BASE_PENALTY; size_t i; + size_t half; /* half of disabled features rounded up */ =20 weight->enabled =3D cpu->nfeatures; - weight->disabled =3D 0; =20 if (cpu->type =3D=3D VIR_CPU_TYPE_HOST) { + weight->disabled =3D 0; weight->total =3D cpu->nfeatures; return; } =20 - for (i =3D 0; i < weight->enabled; i++) { - if (cpu->features[i].policy =3D=3D VIR_CPU_FEATURE_DISABLE) { + for (i =3D 0; i < cpu->nfeatures; i++) { + if (cpu->features[i].policy =3D=3D VIR_CPU_FEATURE_DISABLE) weight->enabled--; - weight->disabled +=3D penalty; - penalty++; - } } =20 - weight->total =3D weight->enabled + weight->disabled; + half =3D (cpu->nfeatures - weight->enabled + 1) / 2; + weight->disabled =3D half * (half + 3) / 2; + weight->total =3D cpu->nfeatures - half + weight->disabled; } =20 =20 --=20 2.49.0