Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2895683rwd; Fri, 2 Jun 2023 16:58:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4hX+R4m14fiYuzlT1n35n+vdt7sNpgVFJNTZMLMv4r/3i9Tb2p4IgflTGai8xdGySCR2eJ X-Received: by 2002:a05:6808:48b:b0:396:cd:829c with SMTP id z11-20020a056808048b00b0039600cd829cmr1574743oid.3.1685750332663; Fri, 02 Jun 2023 16:58:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685750332; cv=none; d=google.com; s=arc-20160816; b=ojUNJ4IqUMajLszwhphf4829o3qQEPWNOpeTZg2rgpUZDFz1A2JJ+jOzuSlFtKiPpc Rl7B4SND4qqgIWMAl3VO2nbF/xFKF++KyJmHzgk9+BolKfF6WQwslB0WjqyUIn+CdBXu nM+y/M6h6O4lUQQhXh7GxqY5ti+uN4rR3xxyxK86Dxj9x9Bt6si2hZQD0XrEy0w80o+1 EY9J/F9iDg4K9fpZDuYKPs13Q5gKmZ6C8zORvNcU8g/i820qZoKKz7x4mendZTkcTqXf n+5H+wsMKFLuvvssafpuElrPatNwkfHurZXObiQSrDUb3aSTe+xxeLxP3Sfz2MU73ODw +SZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=5mmxUWbNXCWcSKNxzb01KGCvCtOMb6rFR6D6MsiL8rA=; b=na6JwW1dGItLmbARVSg+0eEpd2OyAeUcqnvic3etmKsTA7lgmq1jlffsfzOz5H5e/B ZSrYgf/u2czkDc08HjSFaiCHpc4bySQ6MLp3iLgadojsZconZkQUqYw3lA03y/OoRawG 3rqBq4uilmtC8NlLV/lH+x7HQZdSpC5ACCENno+qNzRsjye1CsMPVN7NSTtZnAqoEjDn 7oEQaqXZ9jmohMSkZNqkK7kL+w5SKbnM58+qFA1e6cDdaOTcI6PQcEzH4O8olPyTBz5t AlhELo5CrXyo+PT/8KMy/Zvnv8j4KP/9xJDBKsC6fNyL5Np4PcXNbstDMnNCE12JxiAe Up3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=WoUEDQ5d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f2-20020a170902ce8200b001afe7ef68b6si1659687plg.452.2023.06.02.16.58.36; Fri, 02 Jun 2023 16:58:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=WoUEDQ5d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236887AbjFBXdL (ORCPT + 99 others); Fri, 2 Jun 2023 19:33:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236840AbjFBXdI (ORCPT ); Fri, 2 Jun 2023 19:33:08 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E1F219A for ; Fri, 2 Jun 2023 16:33:06 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-568a85f180dso36615147b3.2 for ; Fri, 02 Jun 2023 16:33:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685748786; x=1688340786; 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=5mmxUWbNXCWcSKNxzb01KGCvCtOMb6rFR6D6MsiL8rA=; b=WoUEDQ5ddpi/oFfFOoBl18AoWb3GZ6rToUETa/0Sz/BJfgDYlktlML5eoKlq8u2Q+b cJBMCzxzbFBhmCaWyqTLD5b0MQi2Jlt1+Kpxpv361mlWS0fDkbi1Rtp2WT2kll/jgEpj SM+NEGAGmxKHxnlV9fh5xrJrX3GDHlJMqu5i9oE/iA0gyEOavlMdewoDkXKnwtI7A6/R OxdeBopFScxg8WK+70Qfe2uf1umiJQnX9VSojaJyXdwFYW/J0IH/OCOoMUiXgqnjZHQ2 gkU79mN3ZyosGJCBIAKfydsc54p2hbMO9A4vGUy1JsMKsCSeU+Bn4Utgm3+4y1q1438/ dj1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685748786; x=1688340786; 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=5mmxUWbNXCWcSKNxzb01KGCvCtOMb6rFR6D6MsiL8rA=; b=gBVj+iBj28kzFvpQfHLq2uckiNEzUwmxnp/+/O5mxYxIpVmofAn0i+QLZA/lhJbR+L jTuqB+cmwgCjRdKlBJJ3vAi3Zia6IiVXEBL/j8rn7OciY4TwTSYmbESC7hnva3pE3d0F BSbXuipWVZhOftO66yGNO78gxyd0oOfWY6OTfmmsjbg3fqGdJXK6KGHvuBS0pugUwdQd 8rEvk0ULUOxTak9hTbwNiv6TSxIQBqP9eg6j4l/Q3hnwvzqY7e4NI+wsP6OBReKK7Bm9 JAPNOHSBUdPq55Q1inrmwH4FUWrn0EXVvYHYnYsnX783CxZoCkNJ9bXnDK7IiNKzPeFP VTnw== X-Gm-Message-State: AC+VfDymgEwpbPF2BvqJmD5U6kqAt3/RRKsclfKyx5v+1pC+O2rEi8fM JjI5abadh26JlFvO0aeu6r+6sOIUGQc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:ac06:0:b0:561:8e86:9818 with SMTP id k6-20020a81ac06000000b005618e869818mr629972ywh.7.1685748785936; Fri, 02 Jun 2023 16:33:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Jun 2023 16:32:49 -0700 In-Reply-To: <20230602233250.1014316-1-seanjc@google.com> Mime-Version: 1.0 References: <20230602233250.1014316-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog Message-ID: <20230602233250.1014316-3-seanjc@google.com> Subject: [PATCH v3 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 => 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 = 255; /* enough space for any xAPIC ID */ - bool xapic_id_mismatch = false; + bool xapic_id_mismatch; + int r; /* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */ if (atomic_read_acquire(&kvm->arch.apic_map_dirty) == CLEAN) @@ -391,9 +392,14 @@ void kvm_recalculate_apic_map(struct kvm *kvm) "Dirty APIC map without an in-kernel local APIC"); 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) == CLEAN) { @@ -402,6 +408,15 @@ void kvm_recalculate_apic_map(struct kvm *kvm) return; } + /* + * 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 = false; + kvm_for_each_vcpu(i, vcpu, kvm) if (kvm_apic_present(vcpu)) max_id = 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; - if (kvm_recalculate_phys_map(new, vcpu, &xapic_id_mismatch)) { + r = kvm_recalculate_phys_map(new, vcpu, &xapic_id_mismatch); + if (r) { kvfree(new); new = NULL; + if (r == -E2BIG) { + cond_resched(); + goto retry; + } + goto out; } -- 2.41.0.rc2.161.g9c6817b8e7-goog