Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp80932pxy; Wed, 21 Apr 2021 19:13:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSD/twHfGVlTjlcMf5JTUwu5UaEoKeOvPN2Sw59m+e+X4igaz0v0SJZzkwhaK7eo569CZ4 X-Received: by 2002:a63:4848:: with SMTP id x8mr1095298pgk.362.1619057629796; Wed, 21 Apr 2021 19:13:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619057629; cv=none; d=google.com; s=arc-20160816; b=YnZEAUegz0SMQLe2n0LFglrNUlxiOHYzPwS3yuSM8f13ZgQ3qpjWoXTzyX7sj1DEkr JrepTi0aDJgVfKyeAl/QCwKQ+uSyEDFNrjOkx8sfTSYqiJYOjvS+nhjJoZ7gpfmI3w/O cj9mHeGiYHCD7iZLTpr6+mKaelI2U6SQ7GtqGftq4Kn1p6Xx1/SfW/OpbMfGZrjx9NI3 F2UQojRt3/OmaIqjeiGYz/r+dsAhF8EqpFC1FMAQXrRrfnAVmilGoWMzI/P9nJKRQ+6C 6pj1XqeQ2Cba/s9/DeMmbtV2PgLQoOjYjjXCPwksCRkhxoBKHtC2+aBrnWK1z8xc+wBI AhHg== 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:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=CxobN34GNkA19G2Vph12+PTqHZch+dEzVbCgj+ROQB8=; b=Vq6cAxnZB09aFf7rtgVwQI+bC7+Z01u/xuuZy/gsSeKPmfza/QTsrfSEgw+jBby+Pt 04KauLfZbrGooSknZ+Ux5UA3aIxKHJXKR68uXL2VCfPNZPOBeJHkxLNxucBt2NSxpZiL Yz/a8yWr8Og2p+NPG5nLrfA9MkCyAuy6Fthw6MDbC7Ihg13zD94mDDjWD8k87MAAhh90 fsRYTiVklM0tOqBGHqRaRwwVigz07uH0kldv+6aLOdKywolXncjwXpoEV1RyRWrEJU0a Ts9+9ww6kRF21BaYIqe2N9x4e4tWckSC2owwLBLam/o4F8ZbOtblvWb7nWZDkTueWunG 4++A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="lAk04Jm/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g6si1437056pll.196.2021.04.21.19.13.37; Wed, 21 Apr 2021 19:13:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="lAk04Jm/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S234744AbhDVCNJ (ORCPT + 99 others); Wed, 21 Apr 2021 22:13:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234548AbhDVCMf (ORCPT ); Wed, 21 Apr 2021 22:12:35 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3B171C06135C for ; Wed, 21 Apr 2021 19:12:01 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id z17-20020a0cf0110000b029019a84330150so14881030qvk.13 for ; Wed, 21 Apr 2021 19:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=CxobN34GNkA19G2Vph12+PTqHZch+dEzVbCgj+ROQB8=; b=lAk04Jm/r7CGDxFvlXBFLbnmWqTdmHmbZY0IPLuHUWrY3GV+Lx9j5bfKpOYmLZD0DG pHm7NOZxo7ybbsRm9on6Gt7VeXTKBWagFj2ASP4SNoIcwvON82Szdx/El/bF4oI+DTlD CZpGxtyQLWOiZiMr6cbusboV02VQIFtsJ4GJRfe15UGx7/I0n3Cbzr3zauNvWXYkEj+Q MmTkLzlyPS+A9OknS29sfT+7jChOCPec+gVZwNH7wQpqq07NUVqkb2C4ZGOXW5Ut1mYi r13W8zAXvlOSue9UviJbdE/mztePapkakprwNeS2NdgkaYBTofxLbCUfL6AsTmDzmDX1 GzIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=CxobN34GNkA19G2Vph12+PTqHZch+dEzVbCgj+ROQB8=; b=cOqSEgsjNeoVLFUqp/rrwHzHQ/jjhBZlVsCmXUWI4nDvi1ba1V8779xCf+g9Qv+jJS qz69pSgsWwvs9+IJY62B7FTACaVLL9qQKjpoN+XigTFvL6zPdt16QzOYkutOjimgr8z2 frBHPHmIrnbPgqdbA5etrqjrOxcEwjBBfluIKiIhkdJs2UFean8RAfVI6/nhLoVDJgb4 YYedw4T93vuyYkqQUISpOBsigLnI+9viC8jh9T+Olj8HudHMfpggSphxlAraGzxL+Fli Nu/jvnTSxbphfrKur2hAUZq7XNMdGhoKEQTjSGmeAceDtQCQZi6VARyk+tvjFsSM2gIf JXVg== X-Gm-Message-State: AOAM530lMEhvk6gPF9c6DuaZpxd1ncAf08j4RNvB/TnVA4PBPAzhQUry LcTv0cMUVOG6FOSfhb3bYSasZm8onJw= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:f:10:e012:374c:592:6194]) (user=seanjc job=sendgmr) by 2002:a05:6214:12d3:: with SMTP id s19mr1171434qvv.26.1619057520374; Wed, 21 Apr 2021 19:12:00 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 21 Apr 2021 19:11:25 -0700 In-Reply-To: <20210422021125.3417167-1-seanjc@google.com> Message-Id: <20210422021125.3417167-16-seanjc@google.com> Mime-Version: 1.0 References: <20210422021125.3417167-1-seanjc@google.com> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH v5 15/15] KVM: SVM: Skip SEV cache flush if no ASIDs have been used From: Sean Christopherson To: Paolo Bonzini , Dave Hansen , Andy Lutomirski , Peter Zijlstra Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Borislav Petkov , Tom Lendacky , Brijesh Singh Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Skip SEV's expensive WBINVD and DF_FLUSH if there are no SEV ASIDs waiting to be reclaimed, e.g. if SEV was never used. This "fixes" an issue where the DF_FLUSH fails during hardware teardown if the original SEV_INIT failed. Ideally, SEV wouldn't be marked as enabled in KVM if SEV_INIT fails, but that's a problem for another day. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/sev.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5cdfea8b1c47..d65193a4ea17 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -58,9 +58,14 @@ struct enc_region { unsigned long size; }; -static int sev_flush_asids(void) +static int sev_flush_asids(int min_asid, int max_asid) { - int ret, error = 0; + int ret, pos, error = 0; + + /* Check if there are any ASIDs to reclaim before performing a flush */ + pos = find_next_bit(sev_reclaim_asid_bitmap, max_sev_asid, min_asid); + if (pos >= max_asid) + return -EBUSY; /* * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail, @@ -87,14 +92,7 @@ static inline bool is_mirroring_enc_context(struct kvm *kvm) /* Must be called with the sev_bitmap_lock held */ static bool __sev_recycle_asids(int min_asid, int max_asid) { - int pos; - - /* Check if there are any ASIDs to reclaim before performing a flush */ - pos = find_next_bit(sev_reclaim_asid_bitmap, max_sev_asid, min_asid); - if (pos >= max_asid) - return false; - - if (sev_flush_asids()) + if (sev_flush_asids(min_asid, max_asid)) return false; /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */ @@ -1846,10 +1844,11 @@ void sev_hardware_teardown(void) if (!sev_enabled) return; + /* No need to take sev_bitmap_lock, all VMs have been destroyed. */ + sev_flush_asids(0, max_sev_asid); + bitmap_free(sev_asid_bitmap); bitmap_free(sev_reclaim_asid_bitmap); - - sev_flush_asids(); } int sev_cpu_init(struct svm_cpu_data *sd) -- 2.31.1.498.g6c1eba8ee3d-goog