From mboxrd@z Thu Jan 1 00:00:00 1970 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=-18.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable 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 C27A2C4338F for ; Tue, 3 Aug 2021 07:22:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A57F160ED6 for ; Tue, 3 Aug 2021 07:22:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234163AbhHCHW7 (ORCPT ); Tue, 3 Aug 2021 03:22:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:22753 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233966AbhHCHW6 (ORCPT ); Tue, 3 Aug 2021 03:22:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1627975367; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vvnHHx5SLZ5txIch7uBR/ScqaaZaH/q4GjsYjYEelMc=; b=EH1MvqpjUFKZkzwpqLPWipLAAX2JvNP/40XWQSmQeo6LWBsP4Kmh+UPJ3X23XvDSaVJBY9 9/9NbFla9laCQVN+x+IyMXKKU5Gv0i9h1drgQx+mS9zoh2289+wgl/0+QR+Lmc05vMX2oh t5fRLMftLLLf3xPe0pULoCx0JNLyC1c= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-571-Sv0By2ONMYmp-Uu-b2RHLQ-1; Tue, 03 Aug 2021 03:22:44 -0400 X-MC-Unique: Sv0By2ONMYmp-Uu-b2RHLQ-1 Received: by mail-wr1-f72.google.com with SMTP id p12-20020a5d68cc0000b02901426384855aso7223155wrw.11 for ; Tue, 03 Aug 2021 00:22:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=vvnHHx5SLZ5txIch7uBR/ScqaaZaH/q4GjsYjYEelMc=; b=WK4PNvceiUehsroIbTHzKkIEOJircXu+mcg9Vz7I6aBXrEtkrnyVmSYZcnxt88T/AP gAu5CfrSPBSv1R0yuYA0w53hTan5WI2Im9EstU42yexszWMjpH7zdetfGGgduZA+0VlR OMUTlNqveF8PFckeQ1avxa5raAqUR2yGbhwU0uxsiLK/msEUqO6L8pVDCu2sTcRcBOEw mvdNxxQAhEQNu1eMDiTZmVDbZulYjxkqHw8uhwskCYJqMD9X1Z4DDDMyFZ7CdsqkfGsG rLaJ5eMAk0O+Z/GNkl3glKerT2FdKQ9vx9A9fF7d+rZIr6umFDHGzWQmKzBICQz5kvkf O/mw== X-Gm-Message-State: AOAM530bVfiZLhHKfnCwd5G+383ehQmojzcpoESp2dOeY6y888y8ME81 6SmmaV2InpuNjX4VpbXfgV2IfNIezMBM7XgtsbPeuXd1rwsFBEhwC0u9Ecd0cCP85qcnJwbvrC4 i8YqwnLHTeU288S5BKKPaaBCg X-Received: by 2002:a1c:7f50:: with SMTP id a77mr20328710wmd.163.1627975363072; Tue, 03 Aug 2021 00:22:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+tgS1Mk8R2aPdLRZYExFRWj4Yx3qDfpTy+W7fwabL4DjFpr/pDuM96T7v5V0HkrgnPGd44w== X-Received: by 2002:a1c:7f50:: with SMTP id a77mr20328690wmd.163.1627975362815; Tue, 03 Aug 2021 00:22:42 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e? ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id w5sm15777146wro.45.2021.08.03.00.22.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 03 Aug 2021 00:22:42 -0700 (PDT) Subject: Re: [PATCH v2] KVM: SVM: improve the code readability for ASID management To: Mingwei Zhang Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tom Lendacky , Marc Orr , David Rientjes , Alper Gun , Dionna Glaze , Vipin Sharma , Peter Gonda References: <20210802180903.159381-1-mizhang@google.com> From: Paolo Bonzini Message-ID: Date: Tue, 3 Aug 2021 09:22:39 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210802180903.159381-1-mizhang@google.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/08/21 20:09, Mingwei Zhang wrote: > KVM SEV code uses bitmaps to manage ASID states. ASID 0 was always skipped > because it is never used by VM. Thus, in existing code, ASID value and its > bitmap postion always has an 'offset-by-1' relationship. > > Both SEV and SEV-ES shares the ASID space, thus KVM uses a dynamic range > [min_asid, max_asid] to handle SEV and SEV-ES ASIDs separately. > > Existing code mixes the usage of ASID value and its bitmap position by > using the same variable called 'min_asid'. > > Fix the min_asid usage: ensure that its usage is consistent with its name; > allocate extra size for ASID 0 to ensure that each ASID has the same value > with its bitmap position. Add comments on ASID bitmap allocation to clarify > the size change. > > v1 -> v2: > - change ASID bitmap size to incorporate ASID 0 [sean] > - remove the 'fixes' line in commit message. [sean/joerg] > > Signed-off-by: Mingwei Zhang > Cc: Tom Lendacky > Cc: Marc Orr > Cc: David Rientjes > Cc: Alper Gun > Cc: Dionna Glaze > Cc: Sean Christopherson > Cc: Vipin Sharma > Cc: Peter Gonda > Cc: Joerg Roedel > --- > arch/x86/kvm/svm/sev.c | 36 +++++++++++++++++++++--------------- > 1 file changed, 21 insertions(+), 15 deletions(-) > > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index 8d36f0c73071..42d46c30f313 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c > @@ -63,6 +63,7 @@ static DEFINE_MUTEX(sev_bitmap_lock); > unsigned int max_sev_asid; > static unsigned int min_sev_asid; > static unsigned long sev_me_mask; > +static unsigned int nr_asids; > static unsigned long *sev_asid_bitmap; > static unsigned long *sev_reclaim_asid_bitmap; > > @@ -77,11 +78,11 @@ struct enc_region { > /* Called with the sev_bitmap_lock held, or on shutdown */ > static int sev_flush_asids(int min_asid, int max_asid) > { > - int ret, pos, error = 0; > + int ret, asid, error = 0; > > /* Check if there are any ASIDs to reclaim before performing a flush */ > - pos = find_next_bit(sev_reclaim_asid_bitmap, max_asid, min_asid); > - if (pos >= max_asid) > + asid = find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid); > + if (asid > max_asid) > return -EBUSY; > > /* > @@ -114,15 +115,15 @@ static bool __sev_recycle_asids(int min_asid, int max_asid) > > /* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */ > bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap, > - max_sev_asid); > - bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid); > + nr_asids); > + bitmap_zero(sev_reclaim_asid_bitmap, nr_asids); > > return true; > } > > static int sev_asid_new(struct kvm_sev_info *sev) > { > - int pos, min_asid, max_asid, ret; > + int asid, min_asid, max_asid, ret; > bool retry = true; > enum misc_res_type type; > > @@ -142,11 +143,11 @@ static int sev_asid_new(struct kvm_sev_info *sev) > * SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. > * SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. > */ > - min_asid = sev->es_active ? 0 : min_sev_asid - 1; > + min_asid = sev->es_active ? 1 : min_sev_asid; > max_asid = sev->es_active ? min_sev_asid - 1 : max_sev_asid; > again: > - pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_asid); > - if (pos >= max_asid) { > + asid = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_asid); > + if (asid > max_asid) { > if (retry && __sev_recycle_asids(min_asid, max_asid)) { > retry = false; > goto again; > @@ -156,11 +157,11 @@ static int sev_asid_new(struct kvm_sev_info *sev) > goto e_uncharge; > } > > - __set_bit(pos, sev_asid_bitmap); > + __set_bit(asid, sev_asid_bitmap); > > mutex_unlock(&sev_bitmap_lock); > > - return pos + 1; > + return asid; > e_uncharge: > misc_cg_uncharge(type, sev->misc_cg, 1); > put_misc_cg(sev->misc_cg); > @@ -1854,12 +1855,17 @@ void __init sev_hardware_setup(void) > min_sev_asid = edx; > sev_me_mask = 1UL << (ebx & 0x3f); > > - /* Initialize SEV ASID bitmaps */ > - sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); > + /* > + * Initialize SEV ASID bitmaps. Allocate space for ASID 0 in the bitmap, > + * even though it's never used, so that the bitmap is indexed by the > + * actual ASID. > + */ > + nr_asids = max_sev_asid + 1; > + sev_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL); > if (!sev_asid_bitmap) > goto out; > > - sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL); > + sev_reclaim_asid_bitmap = bitmap_zalloc(nr_asids, GFP_KERNEL); > if (!sev_reclaim_asid_bitmap) { > bitmap_free(sev_asid_bitmap); > sev_asid_bitmap = NULL; > @@ -1904,7 +1910,7 @@ void sev_hardware_teardown(void) > return; > > /* No need to take sev_bitmap_lock, all VMs have been destroyed. */ > - sev_flush_asids(0, max_sev_asid); > + sev_flush_asids(1, max_sev_asid); > > bitmap_free(sev_asid_bitmap); > bitmap_free(sev_reclaim_asid_bitmap); > Queued, thanks. Paolo