From nobody Wed Dec 17 06:02:48 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 6EAE624CEE5 for ; Fri, 4 Apr 2025 19:41:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795691; cv=none; b=LabKHwPWZOctQoLzT6Syy0aayKRu2zBoyMh10mk/bQM28B+Yra1svk/myt1ELa96ofSfG0LV8oH+jXPc+FJiD9Fm8zhEryuUQVAEIblHd0krziUy7H/+J75aaPSifHn5QzD+In9Y9nTU2G78JRVUiqYb2HVo1ZUIpVn9ruOXd08= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795691; c=relaxed/simple; bh=U7Bv3Mou19gPLSngEjr+1RsKLIdn0l5YTp9cdhCcRAg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sml8F6G8ZBk/NZIvhet3vVahrRP5+rWSGIph6HbnEOa1+p82rvrmZV3kc/aVDSO+2DTwZoaWUggrVEco+7lpchW6vhxNeaESLqJquGYCXmqf0OL5IuUVM8vZUMlLyZPdYGSGuv3mvZpNrN8Us2MN5bNUBIvvpLg+fAo3iA7cqew= 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=m1gZrpLm; arc=none smtp.client-ip=209.85.210.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="m1gZrpLm" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7369b559169so1803629b3a.1 for ; Fri, 04 Apr 2025 12:41:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795689; x=1744400489; 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=mt07Qxkb97s5FkZiQg7yKT2x8NR3KghJCfR+NZxwIRc=; b=m1gZrpLmuzLk2+Q7XZiElC4WNAo3q94iiOcWVxm9o009DCtc5TiPC5tGRvMHS3TkHP auwSvrur18wQbPFnY+s8NTtOWzHA9bNYyhgdp9u0WMnmrcJvLfLzh3TyO1W+c8Jxsng3 4VT1APrBNGKz/PfDTYdTOd+MgsaZLL7tOgEkas88Q4VAOCZ411fe6gArtzo0rtOszYqq wahV17wVJdWrkDCdudDBrufQKi2ojDkMxe56H1zaOh1HO+EzidFU4RjnRQ8yt5Dr9GMC ij26bY6+HWaQNwD7FkOVjoousWF6kKImvXPmAFBRhMc4zLkh7a7pfFutnbNcM9plx/p+ VF+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795689; x=1744400489; 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=mt07Qxkb97s5FkZiQg7yKT2x8NR3KghJCfR+NZxwIRc=; b=YI8t6u2VYjaog8/auIf8+5EV76/jKh9LYFJ21S2v2cI4t03h5M0I4Ou1ntwIzK/Q56 dthr/H/b38NJGF1kH3sWTPxiuGm44I0yWpLqN8sz9GH0oRwe/WN7XqQz+kBl14sPgZ8C T0oqqUP6LdIgXHpOnFN8PbizFiqOrMhev/YkBy+1ntdl3Oxj3ExmboScxz3dticyZIN/ tZZhaCP0NauFl5/2h3R6K/Cjdpc8fTuRvV8vcwlUHapymW7TZsIq/zz/BqCjG+OMS/rA 1j13iGM+fAaYLMBOuL+rVY4E0VWtNtPiBsVB7RTNUpqX2HVePkZX9ZbB6ck5OwLdx7+l SqPg== X-Forwarded-Encrypted: i=1; AJvYcCXAKF3EBuPOlSAel6yi0KRJ3KGSsLlNy87OW2Ip0LrTDtWK+kitI4ESsqyhKSZxc1E7qtwvJym3AF7SD/w=@vger.kernel.org X-Gm-Message-State: AOJu0YxrYmqoIn5ZjkQrKmzeodrjMR7HupH/stAegPOKmk0dUNwj3+Fq s+FU8A74+d3ujEkwlYY7/N+Sfe2fSnTpuPq7zmbVS8sxPHr1/blkBrj62fuaA/+o8FjBiderDMZ QlA== X-Google-Smtp-Source: AGHT+IHKRRqtQ+5sxIr+azJfU1pTEj52X3kWKBhqR+uBWAf1O7lXz3snAlLOZvwHP7CdcZyeKm5xblX214Q= X-Received: from pfbdf8.prod.google.com ([2002:a05:6a00:4708:b0:737:6b9f:8ab4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:10cb:b0:736:5486:7820 with SMTP id d2e1a72fcca58-739e48573c2mr5416904b3a.13.1743795688779; Fri, 04 Apr 2025 12:41:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:14 -0700 In-Reply-To: <20250404193923.1413163-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: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-60-seanjc@google.com> Subject: [PATCH 59/67] KVM: SVM: Use vcpu_idx, not vcpu_id, for GA log tag/metadata From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky , Joao Martins , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use a vCPU's index, not its ID, for the GA log tag/metadata that's used to find and kick vCPUs when a device posted interrupt serves as a wake event. Lookups on a vCPU index are O(fast) (not sure what xa_load() actually provides), whereas a vCPU ID lookup is O(n) if a vCPU's ID doesn't match its index. Unlike the Physical APIC Table, which is accessed by hardware when virtualizing IPIs, hardware doesn't consume the GA tag, i.e. KVM _must_ use APIC IDs to fill the Physical APIC Table, but KVM has free rein over the format/meaning of the GA tag. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index d2cbb7ac91f4..d567d62463ac 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -29,36 +29,39 @@ #include "svm.h" =20 /* - * Encode the arbitrary VM ID and the vCPU's default APIC ID, i.e the vCPU= ID, - * into the GATag so that KVM can retrieve the correct vCPU from a GALog e= ntry - * if an interrupt can't be delivered, e.g. because the vCPU isn't running. + * Encode the arbitrary VM ID and the vCPU's _index_ into the GATag so that + * KVM can retrieve the correct vCPU from a GALog entry if an interrupt ca= n't + * be delivered, e.g. because the vCPU isn't running. Use the vCPU's index + * instead of its ID (a.k.a. its default APIC ID), as KVM is guaranteed a = fast + * lookup on the index, where as vCPUs whose index doesn't match their ID = need + * to walk the entire xarray of vCPUs in the worst case scenario. * - * For the vCPU ID, use however many bits are currently allowed for the max + * For the vCPU index, use however many bits are currently allowed for the= max * guest physical APIC ID (limited by the size of the physical ID table), = and * use whatever bits remain to assign arbitrary AVIC IDs to VMs. Note, the * size of the GATag is defined by hardware (32 bits), but is an opaque va= lue * as far as hardware is concerned. */ -#define AVIC_VCPU_ID_MASK AVIC_PHYSICAL_MAX_INDEX_MASK +#define AVIC_VCPU_IDX_MASK AVIC_PHYSICAL_MAX_INDEX_MASK =20 #define AVIC_VM_ID_SHIFT HWEIGHT32(AVIC_PHYSICAL_MAX_INDEX_MASK) #define AVIC_VM_ID_MASK (GENMASK(31, AVIC_VM_ID_SHIFT) >> AVIC_VM_ID_SHI= FT) =20 #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VM_ID_SHIFT) & AVIC_VM_ID_MASK) -#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) +#define AVIC_GATAG_TO_VCPUIDX(x) (x & AVIC_VCPU_IDX_MASK) =20 -#define __AVIC_GATAG(vm_id, vcpu_id) ((((vm_id) & AVIC_VM_ID_MASK) << AVIC= _VM_ID_SHIFT) | \ - ((vcpu_id) & AVIC_VCPU_ID_MASK)) -#define AVIC_GATAG(vm_id, vcpu_id) \ +#define __AVIC_GATAG(vm_id, vcpu_idx) ((((vm_id) & AVIC_VM_ID_MASK) << AVI= C_VM_ID_SHIFT) | \ + ((vcpu_idx) & AVIC_VCPU_IDX_MASK)) +#define AVIC_GATAG(vm_id, vcpu_idx) \ ({ \ - u32 ga_tag =3D __AVIC_GATAG(vm_id, vcpu_id); \ + u32 ga_tag =3D __AVIC_GATAG(vm_id, vcpu_idx); \ \ - WARN_ON_ONCE(AVIC_GATAG_TO_VCPUID(ga_tag) !=3D (vcpu_id)); \ + WARN_ON_ONCE(AVIC_GATAG_TO_VCPUIDX(ga_tag) !=3D (vcpu_idx)); \ WARN_ON_ONCE(AVIC_GATAG_TO_VMID(ga_tag) !=3D (vm_id)); \ ga_tag; \ }) =20 -static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_ID_MASK) =3D=3D -1u); +static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_IDX_MASK) =3D=3D -1u= ); =20 static bool force_avic; module_param_unsafe(force_avic, bool, 0444); @@ -148,16 +151,16 @@ int avic_ga_log_notifier(u32 ga_tag) struct kvm_svm *kvm_svm; struct kvm_vcpu *vcpu =3D NULL; u32 vm_id =3D AVIC_GATAG_TO_VMID(ga_tag); - u32 vcpu_id =3D AVIC_GATAG_TO_VCPUID(ga_tag); + u32 vcpu_idx =3D AVIC_GATAG_TO_VCPUIDX(ga_tag); =20 - pr_debug("SVM: %s: vm_id=3D%#x, vcpu_id=3D%#x\n", __func__, vm_id, vcpu_i= d); - trace_kvm_avic_ga_log(vm_id, vcpu_id); + pr_debug("SVM: %s: vm_id=3D%#x, vcpu_idx=3D%#x\n", __func__, vm_id, vcpu_= idx); + trace_kvm_avic_ga_log(vm_id, vcpu_idx); =20 spin_lock_irqsave(&svm_vm_data_hash_lock, flags); hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) { if (kvm_svm->avic_vm_id !=3D vm_id) continue; - vcpu =3D kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id); + vcpu =3D kvm_get_vcpu(&kvm_svm->kvm, vcpu_idx); break; } spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); @@ -793,7 +796,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, */ struct amd_iommu_pi_data pi_data =3D { .ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, - vcpu->vcpu_id), + vcpu->vcpu_idx), .is_guest_mode =3D kvm_vcpu_apicv_active(vcpu), .vapic_addr =3D avic_get_backing_page_address(to_svm(vcpu)), .vector =3D vector, --=20 2.49.0.504.g3bcea36a83-goog