From patchwork Wed Jan 31 23:56:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 195086 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:106:209c:c626 with SMTP id mn5csp100753dyc; Wed, 31 Jan 2024 15:58:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IG59yG4307iSUkFdEIN5v0WDR7eU89WpJeongBKLL2bKats1FanusEMSF7yBUhP19tGGAYU X-Received: by 2002:a05:6808:2383:b0:3be:2ff8:c2fd with SMTP id bp3-20020a056808238300b003be2ff8c2fdmr3679019oib.23.1706745497341; Wed, 31 Jan 2024 15:58:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706745497; cv=pass; d=google.com; s=arc-20160816; b=m/EjM6hoJ+b/wm0gISpAmalbS4IerqSdmbDZVGwmB4e2JM3iiZXzrKB0Zua3ox99Bs 9Ta2H3mqpG6OChA0ehojHfgEjjepP7rhcUjBgmLzphHttKoAciLsCSXXt4myViKB74uU jqK0vexfmO7o8Jas7mX0Y8v0p/IGAqvEaWXnA4o4UY+L90DPOcU/uET4wGjVys2UoUpu rGsyAauiZ5e/DZrOuKIE8o3qro2FLKzyDhHDM2ySvuOmNo9Gn8VIOEY4WoM+b5nkULfi fGyD08i3UA6vryn2TYZoMTaTZxBttzAN/a2PMnOfEoO7CMXPpx+4b43aGNi3/wlWuxN3 pmEw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=wDrpEbS6CmtfJ+E5uJL5CN/JlvMOfJl9phijYT5xNg4=; fh=gSRNhBKxQ4nw0fKlokqaiSE4gahvFKJkGm1JvySfH7s=; b=uFZ1+KN8ibpMD7IhbIH7uZXhNw8Bo9nT7t0gtXUUJLMh+7bkwlqLOoAG484A8vk8Zn LpgqOMTJZFKOkD1CWrVq5E3DBLCatTfxCM7U2wIw1ql/w6outXQg9WKYjiPivQ9SBilH Ao6uiUKTdErQ7ZbkS+Q9bqJqUzp5J1+KKB7T+0nyVZ+DkOQOveDYuhhNQ7cxeSwgCxf7 /t9KNPi5qmS1TBfe5d6jqL2mAS7mrSkM67FP2OHOAhBS+Bcc1X2d5Ex0lN29aadjm9TN 9ly6o+su9UaN24w+x3gRZBJLuiT1GOJsOYwgjftR0ZXkmLe/FNTuOS3y09351Sm+0oed HbAg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nE6k6GwO; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47402-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47402-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCX+yYTbddvVArq4gNWmIwsV4bGcp+D4dUehYVnYCeCknqoLUtDSdh3p8RcRDuNytVgd/wvHWS5cTTi6mgogRWdOzsPuZg== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g2-20020ac87f42000000b0042ab1abe300si5547680qtk.69.2024.01.31.15.58.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 15:58:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47402-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nE6k6GwO; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-47402-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47402-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 026251C233D3 for ; Wed, 31 Jan 2024 23:58:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CCB2E3FE5B; Wed, 31 Jan 2024 23:56:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nE6k6GwO" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 779C43BB29 for ; Wed, 31 Jan 2024 23:56:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706745380; cv=none; b=XK9FbRp6VWzLNFvnapMu9ZonOztMWaydFh0X1MxMLdiz8TCSExohGSWaVRFSQQpJQveQmztvvxIlk0Du3R7d1POHu42PA0cSV6JUqmxssy4Nzn11aKvF83rTA/K7bI+QveS7ajEjFF7Cq2tsA/KmyR1Tjf8YWF7YU32s/2wR5Wc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706745380; c=relaxed/simple; bh=Y2eTrWyIvoI9BY4Vorbbz847bJmJSiZQnQACclMHzyM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=osB5s5z9ZqaGOL0C2iGLZ6sCL3CTKYb5G9UJoRYwtu4GGGMG2RVfiI4kOfgce2L5gVNvPiShNuGU1RxtH448dA+sJHRG2kezbGNiSCs/Bh/UCu0jiirON+iSreji2YdP9ZUyPccqLnLg5/ax+PBl+yPCcroB56F3awVn6zugaw0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=nE6k6GwO; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6040597d005so6844047b3.2 for ; Wed, 31 Jan 2024 15:56:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706745375; x=1707350175; darn=vger.kernel.org; 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=wDrpEbS6CmtfJ+E5uJL5CN/JlvMOfJl9phijYT5xNg4=; b=nE6k6GwOgPbzqBzDgNDXdpItFoEceXrLJfmpvHlwcgPlaUbtKdpXDQIZbJkpbJ8XN3 YQlCPCTG49J0ezo4eRtgwoplBXIgraqjEuogEM3OgshhL6XRok1l3lxP4ssI6pcF0jWA A4TAD/fV0uJ2mhwU61jYzyU3vt0XMy2H7Zez8nmR7AUsXycLTttGvyrNd+B8YPQ64nvb XpdM6zlwXClEJlVlrCwYeGgzZ9DZQwIeWKEyXI4YPsqn8G86B6ilsOjetPE7zbR1Ug6K 2zFRg1EkP/oXdeppVZan34sZ5Uy9ohgNVVFIDNPIdwVFABl7gBJywozQ7lki4uk8ybMh 2XgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706745375; x=1707350175; 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=wDrpEbS6CmtfJ+E5uJL5CN/JlvMOfJl9phijYT5xNg4=; b=sfI3ptrBa2WtwlTKjH9Ic1EmLYOdfD5irPOkiXPQ/H19tbnsUIqTvgB7iWW9GuuLws 68jKKSnLAmRN1q8daoWP9/XB9FOlTYebzOaMCrhWtxRzvE8T47RAG2rghg0As1KTW8RH Owv9o3u8KSBdXVU8/mgVpt7PSZCMdnMNlWOjwdc2n11ERzRz6eKaOgFbxyhRDl7xGSUw smX1B7GFFE3gUdSIB7trB6Bq0TACA/d2Z3drGLnLco3SfyX09ewN0KvdZBWlR5McO+5K QG/iFJ+LKUVhM6CVa9/ANRwd4M0VHife+5yZCrmNKJN3Ps26IhSskg7e6GB1Kdxdfw88 OHHA== X-Gm-Message-State: AOJu0YzgCRqdd7EgDs/eocBPMfy4Ezc+QDS4nupVymLF9eSjQsKIJjkj qJaFJVh8RtCb997FG/A/VsmYHl0tiYd7Vgto8yX8G2amcA8jRCU3biqpr9QdrZSveNSBDMb+BuT kBA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:9e:b0:603:ea10:c37c with SMTP id be30-20020a05690c009e00b00603ea10c37cmr719982ywb.7.1706745375516; Wed, 31 Jan 2024 15:56:15 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 31 Jan 2024 15:56:07 -0800 In-Reply-To: <20240131235609.4161407-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240131235609.4161407-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240131235609.4161407-3-seanjc@google.com> Subject: [PATCH v4 2/4] KVM: SVM: Use unsigned integers when dealing with ASIDs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ashish Kalra , Tom Lendacky X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789652366857030731 X-GMAIL-MSGID: 1789652366857030731 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. Signed-off-by: Sean Christopherson Reviewed-by: Tom Lendacky --- 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..04c4c14473fd 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -84,9 +84,10 @@ struct enc_region { }; /* 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 = 0; + int ret, error = 0; + unsigned int asid; /* Check if there are any ASIDs to reclaim before performing a flush */ asid = 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) } /* 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_asid) { if (sev_flush_asids(min_asid, max_asid)) return false; @@ -143,8 +144,9 @@ static void sev_misc_cg_uncharge(struct kvm_sev_info *sev) 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 = true; + int ret; WARN_ON(sev->misc_cg); sev->misc_cg = get_current_misc_cg(); @@ -188,7 +190,7 @@ static int sev_asid_new(struct kvm_sev_info *sev) return ret; } -static int sev_get_asid(struct kvm *kvm) +static unsigned int sev_get_asid(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -284,8 +286,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error) { + unsigned int asid = sev_get_asid(kvm); struct sev_data_activate activate; - int asid = sev_get_asid(kvm); int ret; /* 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 = to_kvm_svm(vcpu->kvm)->sev_info.asid; + unsigned int asid = to_kvm_svm(vcpu->kvm)->sev_info.asid; /* * 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 = per_cpu_ptr(&svm_data, cpu); - int asid = sev_get_asid(svm->vcpu.kvm); + unsigned int asid = sev_get_asid(svm->vcpu.kvm); /* Assign the asid allocated with this SEV guest */ svm->asid = asid; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 83843379813e..b82e6ed4f024 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -732,13 +732,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), TP_STRUCT__entry( - __field( __u64, rip ) - __field( int, asid ) - __field( __u64, address ) + __field( __u64, rip ) + __field( unsigned int, asid ) + __field( __u64, address ) ), TP_fast_assign( @@ -747,7 +747,7 @@ TRACE_EVENT(kvm_invlpga, __entry->address = address; ), - 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) );