From nobody Mon Feb 9 15:09:04 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 231F45A0FB for ; Mon, 18 Mar 2024 22:10:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710799810; cv=none; b=YxhvBM+iwVfiuRmzAxRMUGpeMzD5lDJW7BDnQ6LXBcxz/BL5Zy7MbQzvXZwNlDzrLZXNxZSzBfa+40ZspXGoSHLNibGu6DOxjjCs/55IKC5n//hJHNFwTZHlOHbhzc74StrXXixTdsMRdYXty2ZcVweBdWqloRUojYT06Anjg8k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710799810; c=relaxed/simple; bh=8OmIJ7TG5EgMLMe89eEfIqHHCE4J+CiAn1kyL9d3cRo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uCm7WssNPqshc8iONwCeHfKH6tIot64CGI7+pUPiMUAvDv1d+qKR4HbupLc8BQxAe+ZcrM06TMbKtn+JvszWfCjbSkwa9Ovl2He0fH11ly48+bGPDA5gupvLmZF15SxTT9RyKBttxnua+LY8CpOn6Sef7d4470KCdd2tKioN5LM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fuyHW1Cd; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fuyHW1Cd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710799808; 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=9aIqm5wZzKo1WTdEYKyjDcm8eFDA+KlO2RXyMnu6dq4=; b=fuyHW1CdV6lAXx7Ke/EcGZGT5nfenhSYrJm/1MGMDTo3ywzLQSwIovgzAs+mxdeQ6NF/zE dUdFneM1werEHwiV/0kruuvbuMO80csiSIm08WaYn8k3gE9KIBSwbtZq7B89UW4VtyxABR xn1J6LC8N+q+byFwQhI+J+nSmGHrges= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-pHmGvFWAOfOLRzoj8U5bjA-1; Mon, 18 Mar 2024 18:10:04 -0400 X-MC-Unique: pHmGvFWAOfOLRzoj8U5bjA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id EEBB58007A2; Mon, 18 Mar 2024 22:10:03 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id CDCFC492BC8; Mon, 18 Mar 2024 22:10:03 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, Tom Lendacky Subject: [PATCH 2/7] KVM: SVM: Use unsigned integers when dealing with ASIDs Date: Mon, 18 Mar 2024 18:09:57 -0400 Message-ID: <20240318221002.2712738-3-pbonzini@redhat.com> In-Reply-To: <20240318221002.2712738-1-pbonzini@redhat.com> References: <20240318221002.2712738-1-pbonzini@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Convert all local ASID variables and parameters throughout the SEV code from signed integers to unsigned integers. As ASIDs are fundamentally unsigned values, and the global min/max variables are appropriately unsigned integers, too. Functionally, this is a glorified nop as KVM guarantees min_sev_asid is non-zero, and no CPU supports -1u as the _only_ asid, i.e. the signed vs. unsigned goof won't cause problems in practice. Opportunistically use sev_get_asid() in sev_flush_encrypted_page() instead of open coding an equivalent. Reviewed-by: Tom Lendacky Link: https://lore.kernel.org/r/20240131235609.4161407-3-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/sev.c | 18 ++++++++++-------- arch/x86/kvm/trace.h | 10 +++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7c000088bca6..eeef43c795d8 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -84,9 +84,10 @@ struct enc_region { }; =20 /* Called with the sev_bitmap_lock held, or on shutdown */ -static int sev_flush_asids(int min_asid, int max_asid) +static int sev_flush_asids(unsigned int min_asid, unsigned int max_asid) { - int ret, asid, error =3D 0; + int ret, error =3D 0; + unsigned int asid; =20 /* Check if there are any ASIDs to reclaim before performing a flush */ asid =3D find_next_bit(sev_reclaim_asid_bitmap, nr_asids, min_asid); @@ -116,7 +117,7 @@ static inline bool is_mirroring_enc_context(struct kvm = *kvm) } =20 /* Must be called with the sev_bitmap_lock held */ -static bool __sev_recycle_asids(int min_asid, int max_asid) +static bool __sev_recycle_asids(unsigned int min_asid, unsigned int max_as= id) { if (sev_flush_asids(min_asid, max_asid)) return false; @@ -143,8 +144,9 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *s= ev) =20 static int sev_asid_new(struct kvm_sev_info *sev) { - int asid, min_asid, max_asid, ret; + unsigned int asid, min_asid, max_asid; bool retry =3D true; + int ret; =20 WARN_ON(sev->misc_cg); sev->misc_cg =3D get_current_misc_cg(); @@ -188,7 +190,7 @@ static int sev_asid_new(struct kvm_sev_info *sev) return ret; } =20 -static int sev_get_asid(struct kvm *kvm) +static unsigned int sev_get_asid(struct kvm *kvm) { struct kvm_sev_info *sev =3D &to_kvm_svm(kvm)->sev_info; =20 @@ -284,8 +286,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_s= ev_cmd *argp) =20 static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) { + unsigned int asid =3D sev_get_asid(kvm); struct sev_data_activate activate; - int asid =3D sev_get_asid(kvm); int ret; =20 /* activate ASID on the given handle */ @@ -2312,7 +2314,7 @@ int sev_cpu_init(struct svm_cpu_data *sd) */ static void sev_flush_encrypted_page(struct kvm_vcpu *vcpu, void *va) { - int asid =3D to_kvm_svm(vcpu->kvm)->sev_info.asid; + unsigned int asid =3D sev_get_asid(vcpu->kvm); =20 /* * Note! The address must be a kernel address, as regular page walk @@ -2630,7 +2632,7 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm) void pre_sev_run(struct vcpu_svm *svm, int cpu) { struct svm_cpu_data *sd =3D per_cpu_ptr(&svm_data, cpu); - int asid =3D sev_get_asid(svm->vcpu.kvm); + unsigned int asid =3D sev_get_asid(svm->vcpu.kvm); =20 /* Assign the asid allocated with this SEV guest */ svm->asid =3D asid; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 88659de4d2a7..c6b4b1728006 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -735,13 +735,13 @@ TRACE_EVENT(kvm_nested_intr_vmexit, * Tracepoint for nested #vmexit because of interrupt pending */ TRACE_EVENT(kvm_invlpga, - TP_PROTO(__u64 rip, int asid, u64 address), + TP_PROTO(__u64 rip, unsigned int asid, u64 address), TP_ARGS(rip, asid, address), =20 TP_STRUCT__entry( - __field( __u64, rip ) - __field( int, asid ) - __field( __u64, address ) + __field( __u64, rip ) + __field( unsigned int, asid ) + __field( __u64, address ) ), =20 TP_fast_assign( @@ -750,7 +750,7 @@ TRACE_EVENT(kvm_invlpga, __entry->address =3D address; ), =20 - TP_printk("rip: 0x%016llx asid: %d address: 0x%016llx", + TP_printk("rip: 0x%016llx asid: %u address: 0x%016llx", __entry->rip, __entry->asid, __entry->address) ); =20 --=20 2.43.0