From nobody Mon Feb 9 08:53:51 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DE3E1C7EE2F for ; Fri, 26 May 2023 23:51:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243202AbjEZXvG (ORCPT ); Fri, 26 May 2023 19:51:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237211AbjEZXu7 (ORCPT ); Fri, 26 May 2023 19:50:59 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51D679E for ; Fri, 26 May 2023 16:50:55 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-521262a6680so1328766a12.1 for ; Fri, 26 May 2023 16:50:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685145055; x=1687737055; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=LQ1i2DguubB/+TBn/4idJEX6+htUYXJLnvWqgTtQ+sE=; b=oHPPJs2TrWWxfVTfh0L6PYe6vc/Ms3IcW3ZenHdvhI3xb0N3mUWHwvhAXg2lKBbnSA mGVb1+PpZ7crIlrD6Gi52A7680Hg0E9PY+orIMxLbSDHx8zW9k3j5kx0LAdcsNRTx2Mu 1cFTeCdL1HR9ofonVDVrdvKpRD+5jjtuyDlZ6S2hswUNAhTQhKIAFoYB6cLZpxt5t9lm NRaANo4FDjvt0UczMWprEBMSVeDVmXFbuzF6wASwyQRf6tl3a/7Y9HhhzhVknO2Pzw7y WNXNjV6Ae/vC7RVVO7IjPopqgPsw7T7vxeBf7XRY9VDCzXYVTXEWrxs//V2fYQ64wfrK z5JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685145055; x=1687737055; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=LQ1i2DguubB/+TBn/4idJEX6+htUYXJLnvWqgTtQ+sE=; b=M6DbV6PFi0QzBzKp/GlMHIBOwe3lCztaRqBpsNJiTd4uvg3PsZY1xYjIDr5uQ2ANvx Gk/CZVr9YhQ2ChAMl4Y7LnoOe1h3Vm6Ot8ynGDIPo8D/Kfueoyi2UCT4bwhCLwGyznG9 JfNycopUymzxIwQ4+QWsKUDrvcEp+4jL+V/YCz5x8+tdESBGO+Enz0rpl/r+RDCPgdFM z4q+s/IWX4V2UqmA0B+MCqsiDPXFL3xcka16/BfG/z5RubUsx1/dHdewvFEt5kRCBfD4 pBHtrJaVbiUY73EZS2uN4sY3HpGyQUMhnulR2/7F1KgeTQtW/mwtWLM2LQ+Pa/0I1dei Lguw== X-Gm-Message-State: AC+VfDzlT0pSII3hED82zQO6llmrFTB9KN7evtEp37E+RyOKYsPH09oD epXCti3wyITHbx+k+p0fEXqVhpvwSas= X-Google-Smtp-Source: ACHHUZ63QxfDBJN5gcO9zbQioZ92vLq9NWqlULPbXcgIfsDEZwl5bOocAFcUbzwZq1IWilptEAhIrasqwCw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:6c2:0:b0:53f:5c6c:3d18 with SMTP id 185-20020a6306c2000000b0053f5c6c3d18mr205156pgg.10.1685145054665; Fri, 26 May 2023 16:50:54 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 26 May 2023 16:50:47 -0700 In-Reply-To: <20230526235048.2842761-1-seanjc@google.com> Mime-Version: 1.0 References: <20230526235048.2842761-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230526235048.2842761-3-seanjc@google.com> Subject: [PATCH v2 2/3] KVM: x86: Retry APIC optimized map recalc if vCPU is added/enabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Michal Luczaj Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Retry the optimized APIC map recalculation if an APIC-enabled vCPU shows up between allocating the map and filling in the map data. Conditionally reschedule before retrying even though the number of vCPUs that can be created is bounded by KVM. Retrying a few thousand times isn't so slow as to be hugely problematic, but it's not blazing fast either. Reset xapic_id_mistach on each retry as a vCPU could change its xAPIC ID between loops, but do NOT reset max_id. The map size also factors in whether or not a vCPU's local APIC is hardware-enabled, i.e. userspace and/or the guest can theoretically keep KVM retrying indefinitely. The only downside is that KVM will allocate more memory than is strictly necessary if the vCPU with the highest x2APIC ID disabled its APIC while the recalculation was in-progress. Refresh kvm->arch.apic_map_dirty to opportunistically change it from DIRTY =3D> UPDATE_IN_PROGRESS to avoid an unnecessary recalc from a different task, i.e. if another task is waiting to attempt an update (which is likely since a retry happens if and only if an update is required). Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 3c300a196bdf..cadeaba25e65 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -381,7 +381,8 @@ void kvm_recalculate_apic_map(struct kvm *kvm) struct kvm_vcpu *vcpu; unsigned long i; u32 max_id =3D 255; /* enough space for any xAPIC ID */ - bool xapic_id_mismatch =3D false; + bool xapic_id_mismatch; + int r; =20 /* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */ if (atomic_read_acquire(&kvm->arch.apic_map_dirty) =3D=3D CLEAN) @@ -391,9 +392,14 @@ void kvm_recalculate_apic_map(struct kvm *kvm) "Dirty APIC map without an in-kernel local APIC"); =20 mutex_lock(&kvm->arch.apic_map_lock); + +retry: /* - * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map - * (if clean) or the APIC registers (if dirty). + * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map (if clean) + * or the APIC registers (if dirty). Note, on retry the map may have + * not yet been marked dirty by whatever task changed a vCPU's x2APIC + * ID, i.e. the map may still show up as in-progress. In that case + * this task still needs to retry and copmlete its calculation. */ if (atomic_cmpxchg_acquire(&kvm->arch.apic_map_dirty, DIRTY, UPDATE_IN_PROGRESS) =3D=3D CLEAN) { @@ -402,6 +408,15 @@ void kvm_recalculate_apic_map(struct kvm *kvm) return; } =20 + /* + * Reset the mismatch flag between attempts so that KVM does the right + * thing if a vCPU changes its xAPIC ID, but do NOT reset max_id, i.e. + * keep max_id strictly increasing. Disallowing max_id from shrinking + * ensures KVM won't get stuck in an infinite loop, e.g. if the vCPU + * with the highest x2APIC ID is toggling its APIC on and off. + */ + xapic_id_mismatch =3D false; + kvm_for_each_vcpu(i, vcpu, kvm) if (kvm_apic_present(vcpu)) max_id =3D max(max_id, kvm_x2apic_id(vcpu->arch.apic)); @@ -420,9 +435,15 @@ void kvm_recalculate_apic_map(struct kvm *kvm) if (!kvm_apic_present(vcpu)) continue; =20 - if (kvm_recalculate_phys_map(new, vcpu, &xapic_id_mismatch)) { + r =3D kvm_recalculate_phys_map(new, vcpu, &xapic_id_mismatch); + if (r) { kvfree(new); new =3D NULL; + if (r =3D=3D -E2BIG) { + cond_resched(); + goto retry; + } + goto out; } =20 --=20 2.41.0.rc0.172.g3f132b7071-goog