From nobody Fri Dec 19 15:49:15 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 E9C08221F32 for ; Mon, 19 May 2025 23:28:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697308; cv=none; b=BUNX40Yg3D/N6wIkM3qLX1lf/qd88lxX/DT0gq0N+Yswb49qgLqCGJvwMHd5FZ+/pgomuImy2sVNwfZTIJztiwbW65roLakMij695T/9fnkoUKEXwUsU71p36TdMGLJR2Z3ITndM2AXO1y5oEVkisy55O/GCTRUjXBKJo4YP5Nk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697308; c=relaxed/simple; bh=v27lpHZToMipvRUY1lUmbKWwt8EtNtWm7nK3+oLgxfA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cc7PlrOYxyIjG4xPlWWEnNk/CmWLUhONElKU7YxXCw8mwYsBQLAIclp0NqN3y+DwJ+5dBZZ0U8wXZLlRwekGfAgHo8oMLZM1j+J4HVRWGYn2SQHNa7BRBBZoTwwEjc/wr4zmMlF5QZ4krJTK8RWsEZZNf+wM11PpGKwqxJQz/A8= 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=1zq/cwN0; arc=none smtp.client-ip=209.85.216.73 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="1zq/cwN0" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30f0ffde3ecso1109882a91.0 for ; Mon, 19 May 2025 16:28:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697306; x=1748302106; 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=CYNfSz3c0MEIbufxqfSo89Xbj3evJh8YTPDSU4jQxx8=; b=1zq/cwN0rVSkRru9SQ2KsxsySymkGirUyyPgXRMVpxsRD+JmlatYLqUhlv3vpuWZGL 5dUWOd5w4itj+igPcFUb0kknmNvbytmKVinrMk+NuVYlPQ8escyWIfHMM+yuBZRgjdpZ 0f/+GT6u4U2Jyk/fj/jSf49CImKOzf22zC4EOCSC9lbU8ghEv5ATlTEuRLt53ocaetYU HpHIPlvC88F4auqcJdCTFgG/DdgwyCtWsfVecul02GK+3Co/7zFWMXn2i8HEa5+OtGFz gxfrE7Jnzea5lP1MRpY/cOvYMY44hBYS25mmOBg9RyrZRw+RK1gXbVDhuRztsLW6S/cf 0PsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697306; x=1748302106; 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=CYNfSz3c0MEIbufxqfSo89Xbj3evJh8YTPDSU4jQxx8=; b=WWK3nvJLImb7QbFHgfp77eqjJsdx9tvpU9hFm8viITSUE33O8fp4wH6fnbdCj0QAlw xmHp2t3bx0cnHCe5kMymDNiQdbux9sy5Zd6Dsu/EcGsCWvZdytkI5exnQE9E8yCKifa+ qio/mLN61kam/17hlo4HGNKm93AwJpHB2lkMHQyU3HZP2MMyHcpldzTKZL5jejESZ+IQ ak2/FX4dMizUeqRs5lmdumeIRdK+1YRQgXngAWgSjyvBWcecnKmZQORKAt//TE7WGgPW XV2hlPO9SU5qG3F40rXaEc3aTfc7xkWayQD5fE7hS+1aRYDi+43GJaQvhG++OhVnovvD 5O3A== X-Forwarded-Encrypted: i=1; AJvYcCUtogJ6JFjODyjPnc6nVftDnYrhST7HHZuCnZ99DZbWV22+RDYi/2EomX9aLL4YGmI2n9BjCUPtLgdKics=@vger.kernel.org X-Gm-Message-State: AOJu0YxtVjH6R2phMPoMh5JYMQ56TlmzRW1Cp7pWoxv5hZqAp/LQ7R8V bEA7U1L6XEBkbGJd9/rnqLawDRrlLu2BrDzYgmkhSz81bDz3XM/z7aRi24UO/luZT43Rha9tTRt uYlai7Q== X-Google-Smtp-Source: AGHT+IHD3d0FUo1F+its1m5Q/ZuLYZpcWvtxoKmFmQSmXNfoYPiCCl0K+3fw4yCk8ORbs3n5QKanZNe6CZM= X-Received: from pjbso6.prod.google.com ([2002:a17:90b:1f86:b0:2ee:3128:390f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b86:b0:30e:8c5d:8f8 with SMTP id 98e67ed59e1d1-30e8c5d09b3mr23125425a91.14.1747697306228; Mon, 19 May 2025 16:28:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:00 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-8-seanjc@google.com> Subject: [PATCH 07/15] KVM: x86: Hardcode the PIT IRQ source ID to '2' From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hardcode the PIT's source IRQ ID to '2' instead of "finding" that bit 2 is always the first available bit in irq_sources_bitmap. Bits 0 and 1 are set/reserved by kvm_arch_init_vm(), i.e. long before kvm_create_pit() can be invoked, and KVM allows at most one in-kernel PIT instance, i.e. it's impossible for the PIT to find a different free bit (there are no other users of kvm_request_irq_source_id(). Delete the now-defunct irq_sources_bitmap and all its associated code. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/i8254.c | 55 +++++---------------------------- arch/x86/kvm/i8254.h | 1 - arch/x86/kvm/x86.c | 6 ---- include/linux/kvm_host.h | 1 + 5 files changed, 8 insertions(+), 56 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index f25ec3ec5ce4..c8654e461933 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1396,7 +1396,6 @@ struct kvm_arch { bool pause_in_guest; bool cstate_in_guest; =20 - unsigned long irq_sources_bitmap; s64 kvmclock_offset; =20 /* diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index 2a0964c859af..d4fc20c265b2 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c @@ -248,8 +248,8 @@ static void pit_do_work(struct kthread_work *work) if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0)) return; =20 - kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false); - kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false); + kvm_set_irq(kvm, KVM_PIT_IRQ_SOURCE_ID, 0, 1, false); + kvm_set_irq(kvm, KVM_PIT_IRQ_SOURCE_ID, 0, 0, false); =20 /* * Provides NMI watchdog support via Virtual Wire mode. @@ -641,47 +641,11 @@ static void kvm_pit_reset(struct kvm_pit *pit) kvm_pit_reset_reinject(pit); } =20 -static int kvm_request_irq_source_id(struct kvm *kvm) +static void kvm_pit_clear_all(struct kvm *kvm) { - unsigned long *bitmap =3D &kvm->arch.irq_sources_bitmap; - int irq_source_id; - mutex_lock(&kvm->irq_lock); - irq_source_id =3D find_first_zero_bit(bitmap, BITS_PER_LONG); - - if (irq_source_id >=3D BITS_PER_LONG) { - pr_warn("exhausted allocatable IRQ sources!\n"); - irq_source_id =3D -EFAULT; - goto unlock; - } - - ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); - ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); - set_bit(irq_source_id, bitmap); -unlock: - mutex_unlock(&kvm->irq_lock); - - return irq_source_id; -} - -static void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) -{ - ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); - ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); - - mutex_lock(&kvm->irq_lock); - if (irq_source_id < 0 || - irq_source_id >=3D BITS_PER_LONG) { - pr_err("IRQ source ID out of range!\n"); - goto unlock; - } - clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); - if (!irqchip_kernel(kvm)) - goto unlock; - - kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id); - kvm_pic_clear_all(kvm->arch.vpic, irq_source_id); -unlock: + kvm_ioapic_clear_all(kvm->arch.vioapic, KVM_PIT_IRQ_SOURCE_ID); + kvm_pic_clear_all(kvm->arch.vpic, KVM_PIT_IRQ_SOURCE_ID); mutex_unlock(&kvm->irq_lock); } =20 @@ -715,10 +679,6 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 fl= ags) if (!pit) return NULL; =20 - pit->irq_source_id =3D kvm_request_irq_source_id(kvm); - if (pit->irq_source_id < 0) - goto fail_request; - mutex_init(&pit->pit_state.lock); =20 pid =3D get_pid(task_tgid(current)); @@ -770,8 +730,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 fla= gs) kvm_pit_set_reinject(pit, false); kthread_destroy_worker(pit->worker); fail_kthread: - kvm_free_irq_source_id(kvm, pit->irq_source_id); -fail_request: + kvm_pit_clear_all(kvm); kfree(pit); return NULL; } @@ -788,7 +747,7 @@ void kvm_free_pit(struct kvm *kvm) kvm_pit_set_reinject(pit, false); hrtimer_cancel(&pit->pit_state.timer); kthread_destroy_worker(pit->worker); - kvm_free_irq_source_id(kvm, pit->irq_source_id); + kvm_pit_clear_all(kvm); kfree(pit); } } diff --git a/arch/x86/kvm/i8254.h b/arch/x86/kvm/i8254.h index a768212ba821..14fb310357f2 100644 --- a/arch/x86/kvm/i8254.h +++ b/arch/x86/kvm/i8254.h @@ -42,7 +42,6 @@ struct kvm_pit { struct kvm_io_device speaker_dev; struct kvm *kvm; struct kvm_kpit_state pit_state; - int irq_source_id; struct kvm_irq_mask_notifier mask_notifier; struct kthread_worker *worker; struct kthread_work expired; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4a9c252c9dab..9e2c249d45ca 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12790,12 +12790,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned lon= g type) INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); atomic_set(&kvm->arch.noncoherent_dma_count, 0); =20 - /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ - set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); - /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ - set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, - &kvm->arch.irq_sources_bitmap); - raw_spin_lock_init(&kvm->arch.tsc_write_lock); mutex_init(&kvm->arch.apic_map_lock); seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lo= ck); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0d4506598d62..44b439c5fcf4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -190,6 +190,7 @@ bool kvm_make_all_cpus_request(struct kvm *kvm, unsigne= d int req); =20 #define KVM_USERSPACE_IRQ_SOURCE_ID 0 #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1 +#define KVM_PIT_IRQ_SOURCE_ID 2 =20 extern struct mutex kvm_lock; extern struct list_head vm_list; --=20 2.49.0.1101.gccaa498523-goog