From nobody Fri Dec 19 14:21:14 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 1C98B1EFF8F for ; Mon, 19 May 2025 18:55:20 +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=1747680923; cv=none; b=ss6h5/IrFHFQWPiSXfcyqRdN23APLrAwCOtbncejX8mdmjMjpi3r8noMXiuK45/kjg9zBfyzZ4JFH1OEnfXaLxvRnXifL6wE5KN0ULrsGQQrVVDLHflyVFVE5ygy7YDfaY6IlcUPcfo9nrafygzZxRIMKB//HfEOtijUqDxmqSM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680923; c=relaxed/simple; bh=8ktdrLUAiBXw/pB6xl3QteVK8vmsyuV4GRrjhK4M1uk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nO1bXvriZbmyy2hCHfDEJ2DLGMbeR/PjFTm4lvMSAaq0yLZVCuhWPmrrtirL6mSiJjADmP+ZkAok4ZCuU7V5KBjX+FmWg2E7qXEzW408xht17pRdP+ZbQfCyesh3A8+w16m9B3lXGTAmW1sgBw7W8K/xD8S6K603v8DOTL/VDCA= 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=zgrg4IZJ; 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="zgrg4IZJ" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e9b2e7a34so2087659a91.0 for ; Mon, 19 May 2025 11:55:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680920; x=1748285720; 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=ID0K77cu4OHC+wFfljvoyA4ejd82lVYZb+u9j8jbS6w=; b=zgrg4IZJlF83sIQo4ec3zWuR7bxitiitKVCJa+rxAUWQX+Z/DYUbLQ3iFma1V1CVyu qIPykXj0uRU67lF8qjf7wT2lNILDX6z/AAx0N/QuLOSGbK36R9vcyRmu4Suhm4rlhBEr jd5Zm02KspkaWwQL5elLbh/coh/bCsNFLYYj286iBNMZ4k3cXoWgXQH+JzTCbA0yaxVq cnqLad8Ul5pN0fW2+2ooRQt9XyH52IVL3tesZzWwcSfBvEy0tt41btbZV5QhGE3Rfu3L KBTjgF1yk/g/jIp1DQQocoav9iDn+MEN+Ug8w3dD/+FnhBCMnczKbshd7TUXGwuxWzF6 Me9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680920; x=1748285720; 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=ID0K77cu4OHC+wFfljvoyA4ejd82lVYZb+u9j8jbS6w=; b=nCKt1415XpYz0OfdKQV2AEkuVlHiY/p1dAC25NMPx+euW1fV6AwGHU0+DBEsiUIHPE xkKzkwR5Ggf5EvO2+ZE4wCMriW3BQkE6iEow9wlOkQxSMxINVFcI5e7lBF+86mUFgFaP BJHKfnrlUWClf3FnFzN7JovSutLlR+7/xkK8O6yy9Q4x3RO44ebPpGXlD6mdKj1eo8+l xqx9iOHTJvFDVWxoc3AZqaODkMQfM5+pDjDoc2HXYoAWToOEMNY4taqIfxhKwYx3l7N0 YFMkTD9en8S/52Atw+DzAG+lHeN0vX1dvwF5MsTL4Vfs0qjyXUjJ1eobSwNTMCCmdW/6 LDDg== X-Forwarded-Encrypted: i=1; AJvYcCVSuqjaGsMVaVnz1IeCs9egALF/FkmFa6Bek4hksu3rINfEJ0VkBtg/7/aul27MhpPaHwq7VRc3QGYGULc=@vger.kernel.org X-Gm-Message-State: AOJu0Yy86SHCK/gnxs8UMF7HmoCGeLt/+mZkuNf9lzDUfQNIvCbUySS0 TFp7uZhAdAq/XoFW4SOy/QAbio/0OiM2UNeNsqniZ87BqBokDKUuZ9hhJ2IAoula02o6NYQC9HB RVlR3zA== X-Google-Smtp-Source: AGHT+IEaLF1s48wE5AIuvexHP+HKA2rzrNNO6wCebHCQJZafvZj5yhtA+uxhZqD9qAgjgxLgy4Xu2cmiqA0= X-Received: from pjtu3.prod.google.com ([2002:a17:90a:c883:b0:2ff:6e58:8a0a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d604:b0:30a:9316:75a2 with SMTP id 98e67ed59e1d1-30e7d52adcfmr23209313a91.10.1747680920311; Mon, 19 May 2025 11:55:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:03 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-2-seanjc@google.com> Subject: [PATCH v2 01/12] KVM: Use a local struct to do the initial vfs_poll() on an irqfd From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use a function-local struct for the poll_table passed to vfs_poll(), as nothing in the vfs_poll() callchain grabs a long-term reference to the structure, i.e. its lifetime doesn't need to be tied to the irqfd. Using a local structure will also allow propagating failures out of the polling callback without further polluting kvm_kernel_irqfd. Opportunstically rename irqfd_ptable_queue_proc() to kvm_irqfd_register() to capture what it actually does. Signed-off-by: Sean Christopherson --- include/linux/kvm_irqfd.h | 1 - virt/kvm/eventfd.c | 26 +++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/linux/kvm_irqfd.h b/include/linux/kvm_irqfd.h index 8ad43692e3bb..44fd2a20b09e 100644 --- a/include/linux/kvm_irqfd.h +++ b/include/linux/kvm_irqfd.h @@ -55,7 +55,6 @@ struct kvm_kernel_irqfd { /* Used for setup/shutdown */ struct eventfd_ctx *eventfd; struct list_head list; - poll_table pt; struct work_struct shutdown; struct irq_bypass_consumer consumer; struct irq_bypass_producer *producer; diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 11e5d1e3f12e..39e42b19d9f7 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -245,12 +245,17 @@ irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode,= int sync, void *key) return ret; } =20 -static void -irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh, - poll_table *pt) +struct kvm_irqfd_pt { + struct kvm_kernel_irqfd *irqfd; + poll_table pt; +}; + +static void kvm_irqfd_register(struct file *file, wait_queue_head_t *wqh, + poll_table *pt) { - struct kvm_kernel_irqfd *irqfd =3D - container_of(pt, struct kvm_kernel_irqfd, pt); + struct kvm_irqfd_pt *p =3D container_of(pt, struct kvm_irqfd_pt, pt); + struct kvm_kernel_irqfd *irqfd =3D p->irqfd; + add_wait_queue_priority(wqh, &irqfd->wait); } =20 @@ -305,6 +310,7 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *arg= s) { struct kvm_kernel_irqfd *irqfd, *tmp; struct eventfd_ctx *eventfd =3D NULL, *resamplefd =3D NULL; + struct kvm_irqfd_pt irqfd_pt; int ret; __poll_t events; int idx; @@ -394,7 +400,6 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *arg= s) * a callback whenever someone signals the underlying eventfd */ init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); - init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc); =20 spin_lock_irq(&kvm->irqfds.lock); =20 @@ -416,11 +421,14 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *a= rgs) spin_unlock_irq(&kvm->irqfds.lock); =20 /* - * Check if there was an event already pending on the eventfd - * before we registered, and trigger it as if we didn't miss it. + * Register the irqfd with the eventfd by polling on the eventfd. If + * there was en event pending on the eventfd prior to registering, + * manually trigger IRQ injection. */ - events =3D vfs_poll(fd_file(f), &irqfd->pt); + irqfd_pt.irqfd =3D irqfd; + init_poll_funcptr(&irqfd_pt.pt, kvm_irqfd_register); =20 + events =3D vfs_poll(fd_file(f), &irqfd_pt.pt); if (events & EPOLLIN) schedule_work(&irqfd->inject); =20 --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 C1BBC20F07E for ; Mon, 19 May 2025 18:55:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680924; cv=none; b=pzhwQSTTuInnmqfoed2w4XK9Jjb0SrWUKoFHs0iU9Bi+lVYsX4L2h7kTKElBQoYOaGlTHdZsqZpkVLbvc7EG6hWqshmMI2+elApYG0J8QlQI7ca/5YtmdNlEqo4wAPODBYDRJkhdUhrk9NOhOJjt96SPMq3hgXtikXpcgEJhZ94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680924; c=relaxed/simple; bh=YEjJ+gCdMZpQ1F4rNk5UqTqRmHjDj6r7FHrcRYcEQlk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c/hgcepV5arLx03OlmSXHBCc7XVh0j2rng0nuOkA688rlNCpSki/jklCy0jm7sH3LuXVyu/pAdM9ldTaJ5Uxc+ErFcGVdfj0oRyhp/Esz00GHEjRDN8TT9jWSkgRtYGuavucmWzlkrazwJJBsJNm8Cs0kVrJMUT/tMTyaInFrLA= 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=RhKrrhef; arc=none smtp.client-ip=209.85.215.201 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="RhKrrhef" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b19226b5f7dso3348278a12.0 for ; Mon, 19 May 2025 11:55:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680922; x=1748285722; 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=EvW/ttC5JRh/LPYdQJ6J3cVAwkFh6wSGOl9jEVKaPhQ=; b=RhKrrhefDZSctMhVI5qmH+CyZSdpv/i6OUdE6dOk8kATntZMmJdDiHGovZEPEoul5X GUX2GgjLte1XzJlMnKf97xmUi5v9+1fUid6b8/tehhXVwB2bL+aiLvGYt8gOS/h0rMTg EPIyAsO40AKq4gYgtGd3nEd3zyAqWLEIHhtoyufFzS0oVn43gPnLVzHouq/4z0nsGzJ6 xHCYYL6FPrq93rW+tx/xinxG1dcHXQdsQHDypgwqQ1TXJ8LNZaWpzNcBAPwtUXGWWQzA gI96V4VsW2ZDvcwiHI2H5+K31+T/4y2VH54TKQ+r7V9AvMg8x0HoVzg0uzxaqhxrjGCr 3Gfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680922; x=1748285722; 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=EvW/ttC5JRh/LPYdQJ6J3cVAwkFh6wSGOl9jEVKaPhQ=; b=DfzAG/C4USU2oTs/qHFV2aaYXeNTXZDeTOGRJ13xp/XliuZXzWjIHkxquC20e8gUK/ H4+j1N9s3Nj4ZwPAMPqKyXqgMLYgMu+5m5CJcluims1/ibUwqXlwz6UpP2Wa9Lx7dB0r BmPNxa+uQoZrVSaXSkyFvbdqQcAUC5XYA92PbQY0kkMd8ctBbb67fZdrOixp2TD+PHLu Ez8lFaU9TR1sfaVZa8w5DrIhTorA5S5L5TK6k7DZNRAp0Adf/8qnvhKNoskasuley2U7 4lCDkrGT5CScYZpzDYcVyUYXc2y5En94kAq5S5yyzG6UTnL6bhQASEpzrz1R1z6AHdVU PkRA== X-Forwarded-Encrypted: i=1; AJvYcCWh4z/voZCkNptjSdVFoJXL3+iBktNN+mm31gOzFBNG2f0KSyPTOqr99+JkMShBAr278f43A96FsJL2Vjc=@vger.kernel.org X-Gm-Message-State: AOJu0YwZCc3OQ40IvlEb7XOhl3gqbzVeROInDq28hkMPiCEKm9bxvg0g f39K3eqUm0Y3ZfNpKgclWZVIV4U2PKF1oco+oy9D5l9b8TE1/z6a1wHUiV77bn6HqvmKnqaDF5E muYBvqg== X-Google-Smtp-Source: AGHT+IH9JoaEOfOB8zI2vxbbMIYbrh+JbpOCKy1k7VOcQtUTf5dKAMywqOhhzpJYNuTO32YplhBdVZbLjmE= X-Received: from pliy13.prod.google.com ([2002:a17:903:3d0d:b0:223:4788:2e83]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2986:b0:232:2b90:1410 with SMTP id d9443c01a7336-2322b901585mr92377195ad.10.1747680922171; Mon, 19 May 2025 11:55:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:04 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-3-seanjc@google.com> Subject: [PATCH v2 02/12] KVM: Acquire SCRU lock outside of irqfds.lock during assignment From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Acquire SRCU outside of irqfds.lock so that the locking is symmetrical, and add a comment explaining why on earth KVM holds SRCU for so long. Signed-off-by: Sean Christopherson --- virt/kvm/eventfd.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 39e42b19d9f7..42c02c35e542 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -401,6 +401,18 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *ar= gs) */ init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); =20 + /* + * Set the irqfd routing and add it to KVM's list before registering + * the irqfd with the eventfd, so that the routing information is valid + * and stays valid, e.g. if there are GSI routing changes, prior to + * making the irqfd visible, i.e. before it might be signaled. + * + * Note, holding SRCU ensures a stable read of routing information, and + * also prevents irqfd_shutdown() from freeing the irqfd before it's + * fully initialized. + */ + idx =3D srcu_read_lock(&kvm->irq_srcu); + spin_lock_irq(&kvm->irqfds.lock); =20 ret =3D 0; @@ -409,11 +421,9 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *ar= gs) continue; /* This fd is used for another irq already. */ ret =3D -EBUSY; - spin_unlock_irq(&kvm->irqfds.lock); - goto fail; + goto fail_duplicate; } =20 - idx =3D srcu_read_lock(&kvm->irq_srcu); irqfd_update(kvm, irqfd); =20 list_add_tail(&irqfd->list, &kvm->irqfds.items); @@ -449,6 +459,9 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *arg= s) srcu_read_unlock(&kvm->irq_srcu, idx); return 0; =20 +fail_duplicate: + spin_unlock_irq(&kvm->irqfds.lock); + srcu_read_unlock(&kvm->irq_srcu, idx); fail: if (irqfd->resampler) irqfd_resampler_shutdown(irqfd); --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 9576B211A0E for ; Mon, 19 May 2025 18:55:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680926; cv=none; b=Nw4GND1K/Iv8Y2AeW7D91P+bETFXyMLScMExrTEiXwZ/UAbGe9mfut2bFKBrlkZyU6O9tAG5hCjtSRg0WFOKZFKYhHbebMKZXA+TeFwkJzVrK9rEkPngQbyz+LZEHUqcAhIOIt3+TorIMKkPqX5CQGapCERxvB9htj23XAQuWLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680926; c=relaxed/simple; bh=0kgGSVpa4vF1VM/Tm68ip0ARKg16qtEPfsv+8/D7xvs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uIgBuqCdx7vmZ+OcbDbskyY/yK5hzgK7YtgOumuolf1P7bL5mMXXjqlUvXjN3fKlyGti6SEaenQsU8sporPXTPnjUnmSRmsUZhOK0WptYJK7tOwKY2rZcFB7ZZKMTtPfOYxb+2lnQVsygM185DgtdUi/83Pck0H0eDcm3CnRE/g= 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=hCaOgk59; arc=none smtp.client-ip=209.85.216.74 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="hCaOgk59" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30e9e8d3e85so3209422a91.1 for ; Mon, 19 May 2025 11:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680924; x=1748285724; 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=+VBvSn0/1OEly0dhJhpJ5/xbewUf32J5agAZ9eTZnMo=; b=hCaOgk59U49wmSFx3wTSCz4I0DbWkzyfCFy7KZm83tBhq4KrFF1B3wB+YpcvKJH8pG tVZX6REd3W8J7gskhQ4sF5iWUEOPgWjceUXBf7+u64PFOxeVNksTlsy1cBA14VI8TppC RZ/2sM4XQ6WYSngvGIFKId26tGWz3lu6vGErU5CIFubuwm+6QZvqQrmobTmGo2rUFeMV 007T3Dl/67+3N+VA0CYoYHo9Q5RIss5/xGrc4KuKlW4SYx/jjI4QrVAL80xRVgeXKME3 aCxZKydP3otWuTt27ePpFd0RlFUrGj4zSlzwDl55kGLWcuW8H0iaQIcLrxOu9L55kkgM vufQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680924; x=1748285724; 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=+VBvSn0/1OEly0dhJhpJ5/xbewUf32J5agAZ9eTZnMo=; b=qniWf9YJy7dDYmLp+3n5lookpPK1Oq/WY45xRfqBb4i5DD+jiHoJrcpE27+YKXTLE7 yVHnpM5p5RS2tpLwc8SY7wvmdOU+qrDqpwInz8nduv9+PvC6ZQem2ZdzxotsTAMjkoFv PQP7NzBcRPSkJ/09fWiDqR2z3RU1G4dntawO6N/gVmO2VJtoo8/+vjxm6OZidsPmEb4b hPSiF4/ze/M1ggyx0YUQlIylNAVlR6sb2f6xwov/a9QT6YuABcHSW2I1ulWBudBoGkmF 9iz5PQ9FqlJ9t4w3jADhBmyI/SR5TfYDurn+/mnqWpu2JXqmDIP/nUlZStmJ1n0ub2Vh dHMA== X-Forwarded-Encrypted: i=1; AJvYcCXOzbnctna6LTGUutBI34RjknuQkKIDDRJkfuDmQ6DSf+OD6OTkumlWtGjv/7/6BEdHHqpT0zuoeC2+kQw=@vger.kernel.org X-Gm-Message-State: AOJu0Yw68Z97KgLY6WZ+AvbsY9V7lIt3GPOtvjxNQof9UvdHDG/iah/3 pPAZnlxtnlVFRsRYlsp6PRrsCuxmk07K/5nMLBjevbDtbjyaUinfAjACWZy026XiY1RsHTmDFp6 Ydayg+g== X-Google-Smtp-Source: AGHT+IGXg/F9gxTP1pqaycZUwOUHp+ak6N7UkX/Xq88IlBNICngR1I/Ww9ctAKD/SX/RDw3McfSZyBJElK0= X-Received: from pjg13.prod.google.com ([2002:a17:90b:3f4d:b0:2ea:9d23:79a0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c08:b0:30e:6a10:bb41 with SMTP id 98e67ed59e1d1-30e7d5a85c1mr21315362a91.27.1747680923944; Mon, 19 May 2025 11:55:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:05 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-4-seanjc@google.com> Subject: [PATCH v2 03/12] KVM: Initialize irqfd waitqueue callback when adding to the queue From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Initialize the irqfd waitqueue callback immediately prior to inserting the irqfd into the eventfd's waitqueue. Pre-initializing the state in a completely different context is all kinds of confusing, and incorrectly suggests that the waitqueue function needs to be initialize prior to vfs_poll(). Signed-off-by: Sean Christopherson --- virt/kvm/eventfd.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 42c02c35e542..8b9a87daa2bb 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -256,6 +256,13 @@ static void kvm_irqfd_register(struct file *file, wait= _queue_head_t *wqh, struct kvm_irqfd_pt *p =3D container_of(pt, struct kvm_irqfd_pt, pt); struct kvm_kernel_irqfd *irqfd =3D p->irqfd; =20 + /* + * Add the irqfd as a priority waiter on the eventfd, with a custom + * wake-up handler, so that KVM *and only KVM* is notified whenever the + * underlying eventfd is signaled. + */ + init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); + add_wait_queue_priority(wqh, &irqfd->wait); } =20 @@ -395,12 +402,6 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *ar= gs) mutex_unlock(&kvm->irqfds.resampler_lock); } =20 - /* - * Install our own custom wake-up handling so we are notified via - * a callback whenever someone signals the underlying eventfd - */ - init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); - /* * Set the irqfd routing and add it to KVM's list before registering * the irqfd with the eventfd, so that the routing information is valid --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 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 093182165E9 for ; Mon, 19 May 2025 18:55: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=1747680928; cv=none; b=mU5KQowaL58KzGIyEH2/mjviH94CU0VHBWZUhr35uga8pUbvVvYHsgzqU8OPjdImrX5AdPqrAePVF/0XbPEyE7COE9sDDRLxkU1lAuglZ2gvzG+peGfguJWh0wRFB5gzlzKen0/bwhf8TpWNIKPB9DDswx3D48CPaSoBUoUWUoA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680928; c=relaxed/simple; bh=Eg9zFEBWogv292Cj09cTKdE0faIuxbBzttgoF0OU4WU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JRm9m1URQ9zZr8EokhfxNmldfKLy8oQ93/Hr4lkTs3zcsmJeg2ZDfXZQk6aR+LgBjq96WfDAOiIusNdKvipCWQoeFHQbseYB/QuwfxJS9K2e/fQCYmwyUhMrGNpKeRQ5Ob/wQX7FJmigh6HfDNxM1Pb5f+7rX4mcpaAP7aNLHZg= 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=N14Fb8ea; 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="N14Fb8ea" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e8aec4689so3508805a91.0 for ; Mon, 19 May 2025 11:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680926; x=1748285726; 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=zbVEyigtTpYBUfAmA+RROb1PjlOCwyJF/+KZ2DppXv0=; b=N14Fb8eafBXACEYHLDL/6AwcFxiDFjBGAhvMV8a4C8wPsByCz4uAKGiQJSDQ/tMbz5 L2oUFpexvHE1qp8eghbYNpaZBS3kH9IgoH2PtSLgbTlG7AHgHvQqrGwfvo9ywc0IKysf X3HSRLfR6w5qgI0TwlML6ujFtRGSnvL4tNdjqzvDive7g62jUhsjQD56VCGBy0TEuGEK w60Q6qYX9FZeafpradsPj5uRbmJ9ZY7/qEtLjpu9tNLUtCiD2s1mQsC3IhullTsmIkWX 0bTPqVeCwttjuk45l3NqZBf7DTh2Fekjsn/A0iQbe2eNW/a563CKOMX800nOaQCI5Ns8 6rHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680926; x=1748285726; 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=zbVEyigtTpYBUfAmA+RROb1PjlOCwyJF/+KZ2DppXv0=; b=xVVmT/PPZi9bH1QDaFdHh40y9gmDFnFjNH2+lvWIR1QgSxKIs1LP4bjO1yuZuF7KaA KornKF1TNnk49hqJ9nE0h0iIlSCQ7heuqdISLG6AJOULtCCFCITk4buTqHa0YAAjB8Uy 5KCWWLh3Tm7jj/pNPgzZ20jFhBMMwVWTrNHwxT9PCE6n4gRS+9V152MTU11C2gfUsLaq /YY8eDJsKZC0lrFXS03YsD66Tou709O/0H2CJG8zfk20LyCRuLNmyO8V45UmvlaATtDb xiKheBKooIZFURiEhd1DOE7uhx9cn7zLOSKSiSWgprY1Qm2l0JZAWhlrUntXSIWc8xBu 8u4Q== X-Forwarded-Encrypted: i=1; AJvYcCXAg6te8XaFv+5Mk0burZSiWTGhdizTMA6Fig2A4AQKQFw15TJifNwyMKtV6QTWP266cyDfpNW/HS7zsTA=@vger.kernel.org X-Gm-Message-State: AOJu0YwTgUqIz+l/6ap/k3c+4aK/Pn4UPCQJjtGQXKqpYJvqRAKiRmY3 B3OrN+3oJin7qWMkxH+CuhTo/h3OSDSr1JHJfi86719fyHpBU18ZBysGXQ6MtE4xrvuXmd7xjdQ UP9e6NQ== X-Google-Smtp-Source: AGHT+IEwiAdRCUO6qDinbIwLc5KCjzKO7bUCCiwRhiB03/PtedWpTPViBkBaCXXTF0g4MeChAnr+N8qhDr0= X-Received: from pjbpv17.prod.google.com ([2002:a17:90b:3c91:b0:30c:4b1f:78ca]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1c05:b0:301:6343:1626 with SMTP id 98e67ed59e1d1-30e7d4f91ccmr18659853a91.1.1747680926230; Mon, 19 May 2025 11:55:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:06 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-5-seanjc@google.com> Subject: [PATCH v2 04/12] KVM: Add irqfd to KVM's list via the vfs_poll() callback From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the irqfd structure to KVM's list of irqfds in kvm_irqfd_register(), i.e. via the vfs_poll() callback. This will allow taking irqfds.lock across the entire registration sequence (add to waitqueue, add to list), and more importantly will allow inserting into KVM's list if and only if adding to the waitqueue succeeds (spoiler alert), without needing to juggle return codes in weird ways. Signed-off-by: Sean Christopherson --- virt/kvm/eventfd.c | 102 +++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 45 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 8b9a87daa2bb..99274d60335d 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -245,34 +245,14 @@ irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode,= int sync, void *key) return ret; } =20 -struct kvm_irqfd_pt { - struct kvm_kernel_irqfd *irqfd; - poll_table pt; -}; - -static void kvm_irqfd_register(struct file *file, wait_queue_head_t *wqh, - poll_table *pt) -{ - struct kvm_irqfd_pt *p =3D container_of(pt, struct kvm_irqfd_pt, pt); - struct kvm_kernel_irqfd *irqfd =3D p->irqfd; - - /* - * Add the irqfd as a priority waiter on the eventfd, with a custom - * wake-up handler, so that KVM *and only KVM* is notified whenever the - * underlying eventfd is signaled. - */ - init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); - - add_wait_queue_priority(wqh, &irqfd->wait); -} - -/* Must be called under irqfds.lock */ static void irqfd_update(struct kvm *kvm, struct kvm_kernel_irqfd *irqfd) { struct kvm_kernel_irq_routing_entry *e; struct kvm_kernel_irq_routing_entry entries[KVM_NR_IRQCHIPS]; int n_entries; =20 + lockdep_assert_held(&kvm->irqfds.lock); + n_entries =3D kvm_irq_map_gsi(kvm, entries, irqfd->gsi); =20 write_seqcount_begin(&irqfd->irq_entry_sc); @@ -286,6 +266,49 @@ static void irqfd_update(struct kvm *kvm, struct kvm_k= ernel_irqfd *irqfd) write_seqcount_end(&irqfd->irq_entry_sc); } =20 +struct kvm_irqfd_pt { + struct kvm_kernel_irqfd *irqfd; + struct kvm *kvm; + poll_table pt; + int ret; +}; + +static void kvm_irqfd_register(struct file *file, wait_queue_head_t *wqh, + poll_table *pt) +{ + struct kvm_irqfd_pt *p =3D container_of(pt, struct kvm_irqfd_pt, pt); + struct kvm_kernel_irqfd *irqfd =3D p->irqfd; + struct kvm_kernel_irqfd *tmp; + struct kvm *kvm =3D p->kvm; + + spin_lock_irq(&kvm->irqfds.lock); + + list_for_each_entry(tmp, &kvm->irqfds.items, list) { + if (irqfd->eventfd !=3D tmp->eventfd) + continue; + /* This fd is used for another irq already. */ + p->ret =3D -EBUSY; + spin_unlock_irq(&kvm->irqfds.lock); + return; + } + + irqfd_update(kvm, irqfd); + + list_add_tail(&irqfd->list, &kvm->irqfds.items); + + spin_unlock_irq(&kvm->irqfds.lock); + + /* + * Add the irqfd as a priority waiter on the eventfd, with a custom + * wake-up handler, so that KVM *and only KVM* is notified whenever the + * underlying eventfd is signaled. + */ + init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); + + add_wait_queue_priority(wqh, &irqfd->wait); + p->ret =3D 0; +} + #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS) void __attribute__((weak)) kvm_arch_irq_bypass_stop( struct irq_bypass_consumer *cons) @@ -315,7 +338,7 @@ bool __attribute__((weak)) kvm_arch_irqfd_route_changed( static int kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args) { - struct kvm_kernel_irqfd *irqfd, *tmp; + struct kvm_kernel_irqfd *irqfd; struct eventfd_ctx *eventfd =3D NULL, *resamplefd =3D NULL; struct kvm_irqfd_pt irqfd_pt; int ret; @@ -414,32 +437,22 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *a= rgs) */ idx =3D srcu_read_lock(&kvm->irq_srcu); =20 - spin_lock_irq(&kvm->irqfds.lock); - - ret =3D 0; - list_for_each_entry(tmp, &kvm->irqfds.items, list) { - if (irqfd->eventfd !=3D tmp->eventfd) - continue; - /* This fd is used for another irq already. */ - ret =3D -EBUSY; - goto fail_duplicate; - } - - irqfd_update(kvm, irqfd); - - list_add_tail(&irqfd->list, &kvm->irqfds.items); - - spin_unlock_irq(&kvm->irqfds.lock); - /* - * Register the irqfd with the eventfd by polling on the eventfd. If - * there was en event pending on the eventfd prior to registering, - * manually trigger IRQ injection. + * Register the irqfd with the eventfd by polling on the eventfd, and + * simultaneously and the irqfd to KVM's list. If there was en event + * pending on the eventfd prior to registering, manually trigger IRQ + * injection. */ irqfd_pt.irqfd =3D irqfd; + irqfd_pt.kvm =3D kvm; init_poll_funcptr(&irqfd_pt.pt, kvm_irqfd_register); =20 events =3D vfs_poll(fd_file(f), &irqfd_pt.pt); + + ret =3D irqfd_pt.ret; + if (ret) + goto fail_poll; + if (events & EPOLLIN) schedule_work(&irqfd->inject); =20 @@ -460,8 +473,7 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *arg= s) srcu_read_unlock(&kvm->irq_srcu, idx); return 0; =20 -fail_duplicate: - spin_unlock_irq(&kvm->irqfds.lock); +fail_poll: srcu_read_unlock(&kvm->irq_srcu, idx); fail: if (irqfd->resampler) --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 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 BABD5217F33 for ; Mon, 19 May 2025 18:55:28 +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=1747680930; cv=none; b=ej8JgRgSR7Ez1DjUKhr6fNfSXtopVAlfmWrRZtPNyk6HqxjpCzTjVoEU4QoGtuVqr/ceJBGbNI26Q3cS0gH5OEH+gISZp59zS0s5RO0FWvjG/XWFHuH4LUyTiRlhi3827UQBeOH5InALDpaXr1KL7nTeH3rbv2K/54JWxgBaxLI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680930; c=relaxed/simple; bh=WahQq1TbuZObX7S0cHAq9wgsYhpFJZsXmk63oXnHArs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PP0hOSY2MuZYpra92wqT3x7WYFyfFl1bEgA2oYbxA/pUGBqxkkopTxGPW8RnFhCzL/tjwc3xUG5cJMhCdcVrABUlPtS3DgbBBv+uo/ad9BiKwqHFR2v0pNHL8StmwztyhDH9dIcya+Va0LuZ3SlBgdiv+0FiTHVHAdfdrWGIfnM= 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=NviifZWn; 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="NviifZWn" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e8425926eso4991213a91.1 for ; Mon, 19 May 2025 11:55:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680928; x=1748285728; 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=yzOWtWZ17B97iG5Cps4S6OXY8vgm63oceMRcqA1Kf10=; b=NviifZWnLvkErOsgRxNpmnP7ws6JB2GrEddxR1fpVV1w+jbTlqIDuvGC4+A4qb0Foc hqZAFRwbWC/pd5DNMwo0e4FOYCeTjcVL/1gq5LEKlJ6JpX20/C1BJYP1Z5a82TVIQs9O UYgaG9N+QXZgwmBCMnLxvoN2F/Nkwccg+taPpXnFOpW4IekTf8CLF3R3W9SCp1nOvJKO sevd3I7A3oEoowhmi4uDcnhK6ivxLPY7LAuzb/UhJHkcLEXjG8VwQVxENqBp1CbOGZBh LxawlNKbZODgFEvBUZnKvQBXSPM94wEimIDFNjiHgN9RF18K8d13VEQ1dAz+rY54G6sJ zZ2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680928; x=1748285728; 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=yzOWtWZ17B97iG5Cps4S6OXY8vgm63oceMRcqA1Kf10=; b=TFkaxfGAnHA9y45ajtJ/qR7rH7enT6xY8k8aw0LlcF7bpAwthC7Ye2Lcd9OFL5FIyY /NDZwtr4xkOepa75iT8b1lwKBME2Sjlw1c/7II1LqjMu9x8IahUTyiCDDpgHj7ukfgci sV3wB47VXk7tdDiJA0+3Jq/9NrdC3IBhc7PFBjOgDFOLljqXxPiLbiRzUR4tLeoxaNWr oqwzd9EdbCC4FZ3g3Ps2f8WkDkICXcj5K20pgcHTtrRZoGoMe8/Cyjymj7EHKPe1wQjr T7AatePy8KrD6gmOoVWut11kNp55+vdNO+F0RrqP4xk2jXZEPDI93SLi5Y8ZsV6aJEYV Oh0A== X-Forwarded-Encrypted: i=1; AJvYcCXV4dPXkHW6QyXUtdw3MyrrHjzqVtTWkciTiAnMsI72h9r9PkhZQFVt/LwcYpZdOZ6f2GZjoNSNwVk5yQ0=@vger.kernel.org X-Gm-Message-State: AOJu0Yzom4dneWSgN+R+QkqQvda3DQFSa3U9yLsWK+VvX49fZJP4nLsV aZqcjE04lP4qAu3RLTcFh/lLEpFoADaMcqKhfCq3hPEXYo+1PMdSFDGea7skOz9TdcTu6FOOlGP Tf+4xxw== X-Google-Smtp-Source: AGHT+IF4CHreF9GYhizgGap1CjiBCuQFeZKtOe+ZDk0WbGkBJzfweABFcsSXBbTResROqvG8ccbJeQWMTR0= 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:1dd1:b0:2ff:6167:e92d with SMTP id 98e67ed59e1d1-30e8323ee6emr17930934a91.32.1747680928036; Mon, 19 May 2025 11:55:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:07 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-6-seanjc@google.com> Subject: [PATCH v2 05/12] KVM: Add irqfd to eventfd's waitqueue while holding irqfds.lock From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add an irqfd to its target eventfd's waitqueue while holding irqfds.lock, which is mildly terrifying but functionally safe. irqfds.lock is taken inside the waitqueue's lock, but if and only if the eventfd is being released, i.e. that path is mutually exclusive with registration as KVM holds a reference to the eventfd (and obviously must do so to avoid UAF). This will allow using the eventfd's waitqueue to enforce KVM's requirement that eventfd is assigned to at most one irqfd, without introducing races. Signed-off-by: Sean Christopherson --- virt/kvm/eventfd.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 99274d60335d..04877b297267 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -204,6 +204,11 @@ irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, = int sync, void *key) int ret =3D 0; =20 if (flags & EPOLLIN) { + /* + * WARNING: Do NOT take irqfds.lock in any path except EPOLLHUP, + * as KVM holds irqfds.lock when registering the irqfd with the + * eventfd. + */ u64 cnt; eventfd_ctx_do_read(irqfd->eventfd, &cnt); =20 @@ -225,6 +230,11 @@ irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, = int sync, void *key) /* The eventfd is closing, detach from KVM */ unsigned long iflags; =20 + /* + * Taking irqfds.lock is safe here, as KVM holds a reference to + * the eventfd when registering the irqfd, i.e. this path can't + * be reached while kvm_irqfd_add() is running. + */ spin_lock_irqsave(&kvm->irqfds.lock, iflags); =20 /* @@ -296,16 +306,21 @@ static void kvm_irqfd_register(struct file *file, wai= t_queue_head_t *wqh, =20 list_add_tail(&irqfd->list, &kvm->irqfds.items); =20 - spin_unlock_irq(&kvm->irqfds.lock); - /* * Add the irqfd as a priority waiter on the eventfd, with a custom * wake-up handler, so that KVM *and only KVM* is notified whenever the - * underlying eventfd is signaled. + * underlying eventfd is signaled. Temporarily lie to lockdep about + * holding irqfds.lock to avoid a false positive regarding potential + * deadlock with irqfd_wakeup() (see irqfd_wakeup() for details). */ init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); =20 + spin_release(&kvm->irqfds.lock.dep_map, _RET_IP_); add_wait_queue_priority(wqh, &irqfd->wait); + spin_acquire(&kvm->irqfds.lock.dep_map, 0, 0, _RET_IP_); + + spin_unlock_irq(&kvm->irqfds.lock); + p->ret =3D 0; } =20 --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 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 27A99218EB7 for ; Mon, 19 May 2025 18:55:29 +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=1747680931; cv=none; b=LJgDIT1Euewu5pV/yie6navggDv6ix4eyAJytZehvckvGu3cpMowA5+I9J8YYAfQb2Y1hnrhRcDkCOqC1rq1gRNaU33KXgOIrCGoRP/AnqUaeToU5NcaXwW2eUEw08eCS3t9W2XiZJC8bsMY/eS046pSs0yOr+R8rePtzTFxLMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680931; c=relaxed/simple; bh=Wnq3AEkwwCRKZdw0URszudHGV3cHf51uRQG8VfDhld0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oRCxN3jAgSHHnKDPYcX9xyMZhChMPINc9RsX2hVgjAYh2w/YQDnP3g1aGs0l5PdNBRy++gBoug/KYRL7beKrIHw54ZpuFCwgrYQfdjUfpUj0WeEJvsOMt3QqE8y4Vu8qZFpycHDQUiB1howm8HhoRavsqMcDIalMhTzHPFqtMvw= 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=3I63tJIs; 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="3I63tJIs" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e7c25aedaso2920335a91.2 for ; Mon, 19 May 2025 11:55:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680929; x=1748285729; 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=WPHkK6AJb2aIuP4Pg4NSQD55GV+jm/GYycppUOoBB24=; b=3I63tJIsoiXVzQdmP1nz9WGkgLdodnDB7+MbMDeYKgYWU2SEFroetyVwFDUZLixri6 ErESV/iwvuEskLnEI4JgBzdT69L+BqHQ9Y4ksYov6s763BXVQyddUagh4VXJovV6mohk bo8HWhBAijfAyEk4qK9+oR+wYKepjFxkydt7G6j2Np2h58bb//kTI2j8XP1WFW6FTHwo deS4z8cZG/bvH+b+tuglQKyLsPpi7QV1MiP/SRqEgqg/xF/mrlHaEswRQMJ8PNyOJ5LA 9NyDHyPatsUPzqGq+nCeOpTAJ3Go4cPlj083oPVclOJuwBe5HabtzBrPQwhiZbglFhRf EQ6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680929; x=1748285729; 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=WPHkK6AJb2aIuP4Pg4NSQD55GV+jm/GYycppUOoBB24=; b=HKdH1VECGg6sXEFVJ4xfQoXoMU6sYoXWg/yLb/isOCBMgepKo3uPUJUMDdhRHFjBgd NKBZkbTpIt8DVucbeosiWHPcL9PrV3LWxoMC8T3TYyla+UNy3naUiX/hqzwtO1idUC/D 45qUAeIyl/J8m1ksxskb1i1k9LlsAFboOjadhb/5hZ0dx42BmNCS3PyDSPt2hbgzM/yi B2r8IzLqTk8xaVEOYpjxMnZvvfQe2tmEDQ1fM2SzDL2KRXVZa0kLRTOaQ49EXEePVkye q5soezdff7R8hPQmWj+x7yUeXJv37/Xku00JAxBmk2AFkbWf0r8Na1RZltBOAQhHMW3L 0sIg== X-Forwarded-Encrypted: i=1; AJvYcCWJL0RNLWexfzynVzLuh3pwkST223o9N3PsJDQsTXKVS1C5mlfT81XixWuzGZEcL12WKwawpFDnpFPxiKo=@vger.kernel.org X-Gm-Message-State: AOJu0YyMRqUEqnrNGSlIO4h/YfVKeispd8bQAO/o2WmSGUb0Usa5gfQM 6JlruokCTEX0HfjX33FVe8rjge7fiAxTWUooDZU6SB3FT3X3oeVKhJgAykWZTKZD7sHB5lUBSsq +1blG0Q== X-Google-Smtp-Source: AGHT+IHK1Zr4h0D2c9hfvihT1HBAYjdqDVsYafOfJ/1g69C6YEZd33VFtqEq4B23tLL0mdweYF147yQWVb0= X-Received: from pjbpv10.prod.google.com ([2002:a17:90b:3c8a:b0:30e:5bd5:880d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d445:b0:30e:a1ec:68dd with SMTP id 98e67ed59e1d1-30ea1ec68e8mr16388245a91.8.1747680929524; Mon, 19 May 2025 11:55:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:08 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-7-seanjc@google.com> Subject: [PATCH v2 06/12] sched/wait: Add a waitqueue helper for fully exclusive priority waiters From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a waitqueue helper to add a priority waiter that requires exclusive wakeups, i.e. that requires that it be the _only_ priority waiter. The API will be used by KVM to ensure that at most one of KVM's irqfds is bound to a single eventfd (across the entire kernel). Open code the helper instead of using __add_wait_queue() so that the common path doesn't need to "handle" impossible failures. Note, the priority_exclusive() name is obviously confusing as the plain priority() API also sets WQ_FLAG_EXCLUSIVE. This will be remedied once KVM switches to add_wait_queue_priority_exclusive(), as the only other user of add_wait_queue_priority(), Xen's privcmd, doesn't actually operate in exclusive mode (more than likely, the detail was overlooked when privcmd copy-pasted (sorry, "was inspired by") KVM's implementation). Cc: K Prateek Nayak Signed-off-by: Sean Christopherson --- include/linux/wait.h | 2 ++ kernel/sched/wait.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/linux/wait.h b/include/linux/wait.h index 965a19809c7e..09855d819418 100644 --- a/include/linux/wait.h +++ b/include/linux/wait.h @@ -164,6 +164,8 @@ static inline bool wq_has_sleeper(struct wait_queue_hea= d *wq_head) extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_qu= eue_entry *wq_entry); extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, stru= ct wait_queue_entry *wq_entry); extern void add_wait_queue_priority(struct wait_queue_head *wq_head, struc= t wait_queue_entry *wq_entry); +extern int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_he= ad, + struct wait_queue_entry *wq_entry); extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait= _queue_entry *wq_entry); =20 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struc= t wait_queue_entry *wq_entry) diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index 51e38f5f4701..03252badb8e8 100644 --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c @@ -47,6 +47,24 @@ void add_wait_queue_priority(struct wait_queue_head *wq_= head, struct wait_queue_ } EXPORT_SYMBOL_GPL(add_wait_queue_priority); =20 +int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_head, + struct wait_queue_entry *wq_entry) +{ + struct list_head *head =3D &wq_head->head; + + wq_entry->flags |=3D WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY; + + guard(spinlock_irqsave)(&wq_head->lock); + + if (!list_empty(head) && + (list_first_entry(head, typeof(*wq_entry), entry)->flags & WQ_FLAG_PR= IORITY)) + return -EBUSY; + + list_add(&wq_entry->entry, head); + return 0; +} +EXPORT_SYMBOL(add_wait_queue_priority_exclusive); + void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_= entry *wq_entry) { unsigned long flags; --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 ECB2821A92F for ; Mon, 19 May 2025 18:55:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680933; cv=none; b=KiG1VtDxPpVxu5nFPMc1sdV7JAiDNCkZZUS7k43svhEWlyrvTky5O6bl+8TCY0GmPuAN7ZRRnNveVaPVaWOFaot/oEzyu9ZeFujSkRHoQPIEptuj7Ldz9Qg1rzAIzmYJPzyw1QEO8iVmiOMZfioNk4aG5o5yLQgLRUgqOYHb2I8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680933; c=relaxed/simple; bh=PVxAs9rpAxsfjcWBD9TZ2qf4+mcelQdBBqGnvo71euw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hnTj3cTrBrOTLGfFLsKR2wBOTnw/xJMWgBMv0j6NhNoaT0Hos5sq7t3wIGB1W0owuJXSW0wnlc9kwyXRcrmiDz6ci3NDl1g2lmOjbeYy2Ejr1MgesQDRIpVnO+4vKPuG0+obzSS4aut2KvaoIG9pT07cMWIPxsCUcNlb18vy+gg= 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=gfH2MtTY; arc=none smtp.client-ip=209.85.215.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="gfH2MtTY" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-af8e645a1d1so2988151a12.3 for ; Mon, 19 May 2025 11:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680931; x=1748285731; 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=z3P69QZyor5mPxi6VRLU63qMkHWb4fsadpuY2Dn6pwA=; b=gfH2MtTYaAJc5QiJ7viCJ5btgpnxchcgNEbZEvRIyrR9AGML5i/cxilMcoXI1Hh6VU FC73Zgeqy5eLZcIvQyfKYDB4FmDGZuQ0zjk3cFUPz0HkzsPEt5PUlY1+XbWIj3/YxHgq x6FiPSOBLH4DkC2LooYY3rRe8HAQ74UbUWN5EXzoi74ELfgKbfbbkhTPbSuhTFnic3IP kShGW9oVN4q6e+auo4HvPBUOknjQDlwxyxsNkv1Q6d72n+jB+3Q19hnl7sXquXRibio8 qQDBqnE4H0YxafSyvkQ9GeEdWqBLMetAGVwpdi34cL8U5zQnE+Lsz7sjjHTvoDoIGLCt mwqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680931; x=1748285731; 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=z3P69QZyor5mPxi6VRLU63qMkHWb4fsadpuY2Dn6pwA=; b=ONJyNi0i16E6gGpVZBpflMFfS1nNoNigM6/sH+A0yr+pqlmwjgZ8/B/HMxtXCX6Lx6 VKTqebQ7Yfw9Nbcc2Y37aXmnpZHKGXNUhoEbG8IJbUcbCOn++42nQRat99SqxNN763+Y qsmg5gnWAXCD3bVDinZL15f2py6BF0MSFiAeo5uIaZOcUMOLBwaX7SIMvYCA9NLHk9qN Rt7hGjWj8wvDrmEFIDVZTuc5zcW4digzW9RggoYEPzN8r8cR7bxAhtrDlTN/TXuPrTXs bSxdRBgnGHStqRoTbehjXvZJ9/1NlH2Z1PfTOdlRmZ1uppZsgEEn0bILfg6raEfZ7RiM eRYg== X-Forwarded-Encrypted: i=1; AJvYcCU+zKhG6wbLImzIMF2fuNk/Nlq+95M/J5VqgqUnQX9SELbDqQXDq9ui/ycPbldVKxJxpujg/jF0bnFlc8Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yxq8kuqTK0h6XGgRV5nQnUVRJwdImYGWhQ79T93hZd/HFUhY+i/ zGxvhzi4IMyCntTZCQdwehqKVjTehHUigbu1GViyIyfPW1Oc1gC2z8xcW4s+uQDwGFlcRDREl4a 7f6kfQw== X-Google-Smtp-Source: AGHT+IFA9j9P60jaD9boVYX8hbXZAkB3Zgs45T+xHk3Xso4t9vp7uJB4CdjShK0beY0cstYrv/15W251W2g= X-Received: from pjyp3.prod.google.com ([2002:a17:90a:e703:b0:2fa:a101:755]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c883:b0:2ee:e518:c1cb with SMTP id 98e67ed59e1d1-30e830c797dmr22228101a91.7.1747680931219; Mon, 19 May 2025 11:55:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:09 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-8-seanjc@google.com> Subject: [PATCH v2 07/12] KVM: Disallow binding multiple irqfds to an eventfd with a priority waiter From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disallow binding an irqfd to an eventfd that already has a priority waiter, i.e. to an eventfd that already has an attached irqfd. KVM always operates in exclusive mode for EPOLL_IN (unconditionally returns '1'), i.e. only the first waiter will be notified. KVM already disallows binding multiple irqfds to an eventfd in a single VM, but doesn't guard against multiple VMs binding to an eventfd. Adding the extra protection reduces the pain of a userspace VMM bug, e.g. if userspace fails to de-assign before re-assigning when transferring state for intra-host migration, then the migration will explicitly fail as opposed to dropping IRQs on the destination VM. Temporarily keep KVM's manual check on irqfds.items, but add a WARN, e.g. to allow sanity checking the waitqueue enforcement. Cc: Oliver Upton Cc: David Matlack Signed-off-by: Sean Christopherson --- virt/kvm/eventfd.c | 54 +++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 04877b297267..7b2e1f858f6d 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -291,37 +291,57 @@ static void kvm_irqfd_register(struct file *file, wai= t_queue_head_t *wqh, struct kvm_kernel_irqfd *tmp; struct kvm *kvm =3D p->kvm; =20 + /* + * Note, irqfds.lock protects the irqfd's irq_entry, i.e. its routing, + * and irqfds.items. It does NOT protect registering with the eventfd. + */ spin_lock_irq(&kvm->irqfds.lock); =20 - list_for_each_entry(tmp, &kvm->irqfds.items, list) { - if (irqfd->eventfd !=3D tmp->eventfd) - continue; - /* This fd is used for another irq already. */ - p->ret =3D -EBUSY; - spin_unlock_irq(&kvm->irqfds.lock); - return; - } - + /* + * Initialize the routing information prior to adding the irqfd to the + * eventfd's waitqueue, as irqfd_wakeup() can be invoked as soon as the + * irqfd is registered. + */ irqfd_update(kvm, irqfd); =20 - list_add_tail(&irqfd->list, &kvm->irqfds.items); - /* * Add the irqfd as a priority waiter on the eventfd, with a custom * wake-up handler, so that KVM *and only KVM* is notified whenever the - * underlying eventfd is signaled. Temporarily lie to lockdep about - * holding irqfds.lock to avoid a false positive regarding potential - * deadlock with irqfd_wakeup() (see irqfd_wakeup() for details). + * underlying eventfd is signaled. */ init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); =20 + /* + * Temporarily lie to lockdep about holding irqfds.lock to avoid a + * false positive regarding potential deadlock with irqfd_wakeup() + * (see irqfd_wakeup() for details). + * + * Adding to the wait queue will fail if there is already a priority + * waiter, i.e. if the eventfd is associated with another irqfd (in any + * VM). Note, kvm_irqfd_deassign() waits for all in-flight shutdown + * jobs to complete, i.e. ensures the irqfd has been removed from the + * eventfd's waitqueue before returning to userspace. + */ spin_release(&kvm->irqfds.lock.dep_map, _RET_IP_); - add_wait_queue_priority(wqh, &irqfd->wait); + p->ret =3D add_wait_queue_priority_exclusive(wqh, &irqfd->wait); spin_acquire(&kvm->irqfds.lock.dep_map, 0, 0, _RET_IP_); + if (p->ret) + goto out; =20 + list_for_each_entry(tmp, &kvm->irqfds.items, list) { + if (irqfd->eventfd !=3D tmp->eventfd) + continue; + + WARN_ON_ONCE(1); + /* This fd is used for another irq already. */ + p->ret =3D -EBUSY; + goto out; + } + + list_add_tail(&irqfd->list, &kvm->irqfds.items); + +out: spin_unlock_irq(&kvm->irqfds.lock); - - p->ret =3D 0; } =20 #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS) --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 8018B21B91D for ; Mon, 19 May 2025 18:55:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680935; cv=none; b=WniDdTyf0xfnhUhEsgr+/Pb60FSfm2zdNBxj6ntOBiTqZ8Dsh1rpvh0K6Juewxsuc40abKQN6TCYVGx5gR4/m6KGOWdTQGBC3MMHPboyR3NoqlSkyPRtImphwPPiSltj9Ki85JJBfQRbW+BrUzyHa0WccXh/JlsfCLUdyNoKJPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680935; c=relaxed/simple; bh=UE8/70vVcn8RJqxygSl+zg8PpaIOYhcmQEHooPSRrU8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IwnizLTC4SAsBIUg0Dpo3xknvmOyfbQd6Fmp1RKGbAHTw+/5bdrniW/jJH/30uAnOWXzmZ4J56wdqx4u/d4pMuxOGAAKavlfDfGSCUTJ2LScZe+FMICSaqZgG/tHqwFJCfnMbql3EgAX6kpMIBBAMxljnQhTuPlP1jlo65mh7qU= 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=F2udmMHR; arc=none smtp.client-ip=209.85.215.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="F2udmMHR" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b270145b864so1109446a12.3 for ; Mon, 19 May 2025 11:55:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680933; x=1748285733; 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=GosnGkQOmy9sXouNtT7tWd/cC5UULc5QRQ9laeft/XQ=; b=F2udmMHR62OgAJHdkjFVPxdzL91wGF2CXdp8dhihNxQF7fffCmJ4btSLN2CnwjizT3 UpFEPOXS3CkNWlxRLgKLehtwNVbHSKKrR0X5/s72B9781b/NEvbzxvLHojxCmjTfzqyz GL1RnCqpUYTQZoIsZw2bp19ZUM9Upqymr0bkYgS4dtuoG6UXHdDJ1DcsiYMZP3A3VoMN v9qeVQOo9usXAjKk//wi/MRC8mWZmdeWaaXAKxRaDTztGNdagkBd98PvLL9ANpXy2PI+ ihdCwNnnF4z2Yyo7C2ErCUIWKIUSwZ6RRPmKvB+AYTg9281qcCMrw+1jiU6p8XnT0H7m R4rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680933; x=1748285733; 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=GosnGkQOmy9sXouNtT7tWd/cC5UULc5QRQ9laeft/XQ=; b=IN4Vjdu7QuJM+vG6q0yNQbqwY0CINaQj5X2P8Dq8yJ8hsGfs5VE4w67Q2U7373iJf2 p9ZPfMLJp4WOsSo61rfdG2pf1yhQ9n8vySe1ALwYcVutflplDEJxvMgcOMLmVOoWJQYx Nf6/a9NMZqlp4M3B6sjiPVbjUTA70rNtHdChqnMlQpSgln2XTI5SekKo5W2Yhsco/99L 5LaYd66sexyIxZlrtHlYNh94+EJ+ekj67dfnaUZKvEPtzB+xz8nD1t+EpgKe6R5hW3Lt sy05gDFpC+FCJASqC76sYMsjgdtt5oyXNYUPI1BZJH/5fc916kCxEXfXlxHZUZlbRvUS L5Tw== X-Forwarded-Encrypted: i=1; AJvYcCUPJrVpVIUxNs8CbJbyyWo27vUb/Nhc6/vX5aliAWhQtIrdgY7VGdYvCV4idzV8rJmsCGPwQxL/C1BtKaQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyUQsTVu0qStEdki8YRjPryZ04y1yYSsopiXT2rhO+7/gBEPwtg Bw2OFwedoS3jojHc+/2HnYf6ASx3QHZWGxFwyzicfK6ROBx9nemBk4JD2mo4m3riG4ECp9FBvqp ehazYDg== X-Google-Smtp-Source: AGHT+IF5OGdkKzCp0nPm0pKGJ5Cn3yYZjvTFKZ+Ma7nDq38yX66bRfuqL3GsDOCYJOnomMI2NGvjW+Q4CXU= X-Received: from pjbdy14.prod.google.com ([2002:a17:90b:6ce:b0:2fc:1356:bcc3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3a89:b0:215:d9fc:382e with SMTP id adf61e73a8af0-2170cc5f59bmr19216538637.13.1747680932959; Mon, 19 May 2025 11:55:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:10 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-9-seanjc@google.com> Subject: [PATCH v2 08/12] sched/wait: Drop WQ_FLAG_EXCLUSIVE from add_wait_queue_priority() From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the setting of WQ_FLAG_EXCLUSIVE from add_wait_queue_priority() to differentiate it from add_wait_queue_priority_exclusive(). The one and only user add_wait_queue_priority(), Xen privcmd's irqfd_wakeup(), unconditionally returns '0', i.e. doesn't actually operate in exclusive mode. Cc: Juergen Gross Cc: Stefano Stabellini Cc: Oleksandr Tyshchenko Signed-off-by: Sean Christopherson --- kernel/sched/wait.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index 03252badb8e8..b8f6502372b0 100644 --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c @@ -40,7 +40,7 @@ void add_wait_queue_priority(struct wait_queue_head *wq_h= ead, struct wait_queue_ { unsigned long flags; =20 - wq_entry->flags |=3D WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY; + wq_entry->flags |=3D WQ_FLAG_PRIORITY; spin_lock_irqsave(&wq_head->lock, flags); __add_wait_queue(wq_head, wq_entry); spin_unlock_irqrestore(&wq_head->lock, flags); @@ -82,7 +82,7 @@ EXPORT_SYMBOL(remove_wait_queue); * the non-exclusive tasks. Normally, exclusive tasks will be at the end of * the list and any non-exclusive tasks will be woken first. A priority ta= sk * may be at the head of the list, and can consume the event without any o= ther - * tasks being woken. + * tasks being woken if it's also an exclusive task. * * There are circumstances in which we can try to wake a task which has al= ready * started to run but is not in state TASK_RUNNING. try_to_wake_up() retur= ns --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 779BF218EB7 for ; Mon, 19 May 2025 18:55:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680936; cv=none; b=EzR3KudZECAcBgw4gVgT6H/Pjn6RD1korFyQeKc0z7Zr0GM8EKZ1G18wm5ZHd1X1I/ZXVo33HTh+GRp5AYs5t5yqlCaLUkPelz7G6TleS7OYbmq/XACAtSuev9l6YF29/W2QNgAY0SeFbc0zOykOITJKStLo7Nl19OGLsnwN1bA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680936; c=relaxed/simple; bh=0X1petZgCpulFuo/nX3sHHh4xfNSuUzsnQprwr9yRsM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lNvH5nw3GTbkJrhWu+EWDcsgs/REAO2pQq1Ow+gUD564NtPzdOI07DmmJWjHgEupec8e89F+nXCyKfMryKNK8LYnBlBN62lxbP38QRAMHMR/wiIRjmxwkOJ6jdrdKKXkaLndUQPqySp6O5vxIoQHWpgJabx4GnUAfwC0DKWO5o0= 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=rauzdWHb; arc=none smtp.client-ip=209.85.216.74 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="rauzdWHb" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30a39fa0765so7235290a91.3 for ; Mon, 19 May 2025 11:55:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680935; x=1748285735; 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=ABmmeW4kIHS5pR1wBXW3o3AUaaWLHITX4FAWMNC72BE=; b=rauzdWHboSI+47vP1vr268zezoUmqdkgSu8z7bAhXk+gJ2HXg/dFupHrBYUTNkNFNB za+V7Co+4dxN8oV8HGm5oMlng+T4q2SOAzEEhw1OXjh+8FduXHEnlMkdqKbWVHwle5hl kc/JEQVWTbGnU5voFxELZEQiexfVs6VrlhSXw9luHOdk0OKpWcVTW/PD03d0RqWn4h+m MoEDwPySPGBQT8kThUQc0AWBnzJxEjdsZtgHuurolDkagayR8J3QtkFQmSg2+gdllKK+ Wm3G0E7yweP8Um0jiYMtiM7S5Yr8VLLfss72yBmPqx+0JcrSeDOAASYxPC8iS/dbsd9+ qILg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680935; x=1748285735; 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=ABmmeW4kIHS5pR1wBXW3o3AUaaWLHITX4FAWMNC72BE=; b=SYMSuw/NGhU7EoBGplCGbjo//xzsUGmqJLzjpEJMJfIUac1B6+gaUn1S/1S2d7Xgfb 7rposwc9D1u2PxujCCDu9ZPXmCChVDFE2C6r7x2qb4v5R8Lw6yXbj7XMovzw36Bsncc5 sjSIAq/d8qqpvzZeLrgvjwJNsJsvPo5Z23V0QIcxgxKxf6kXbJGvPGML+/NzjHx+LAwN UxJefjxi43F6PYrK0yCwWZ0Xxn3LDAH7H0EchEl/9RqhfWDh9hEMxQ5CpgwGVVd5AZHp Ext1k5wHKukiM6pUiwZesMFW1JjjO+xXYTUmYCB7a8BDlEIzoZmZPIu8xnif5u+l0CZX ymQQ== X-Forwarded-Encrypted: i=1; AJvYcCV3iet0dWWlQvgNQx3CXK3x6fZtgzv3YB+3VPU6weFPd2Goio0Vx5QhKg3+dXSVWt99Cw6DrhST8mFcV/M=@vger.kernel.org X-Gm-Message-State: AOJu0YxV8W7Wt9X0gxU34TMA8dDE1gg3xh5/8QkAHFeZHD1nW/BxEV0R rNx7xi/xeXclqAe6s/hCm6a4L6xobSk4cn2+tnTnO8LXjlOdBXqYlBhKbP28hyVajKKfZVG4/1I vREkY7A== X-Google-Smtp-Source: AGHT+IGEDc8FYs1ZWMzAv83wv6CIh5ESvR5WonpbBxgCgbPhV7/dILR6EKy6bxcesw6aCw28e8kHIpmzU9Y= X-Received: from pjbsn4.prod.google.com ([2002:a17:90b:2e84:b0:2fc:11a0:c549]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a50:b0:301:98fc:9b5a with SMTP id 98e67ed59e1d1-30e7d500953mr20784074a91.6.1747680934698; Mon, 19 May 2025 11:55:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:11 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-10-seanjc@google.com> Subject: [PATCH v2 09/12] KVM: Drop sanity check that per-VM list of irqfds is unique From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that the eventfd's waitqueue ensures it has at most one priority waiter, i.e. prevents KVM from binding multiple irqfds to one eventfd, drop KVM's sanity check that eventfds are unique for a single VM. Signed-off-by: Sean Christopherson --- virt/kvm/eventfd.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 7b2e1f858f6d..d5258fd16033 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -288,7 +288,6 @@ static void kvm_irqfd_register(struct file *file, wait_= queue_head_t *wqh, { struct kvm_irqfd_pt *p =3D container_of(pt, struct kvm_irqfd_pt, pt); struct kvm_kernel_irqfd *irqfd =3D p->irqfd; - struct kvm_kernel_irqfd *tmp; struct kvm *kvm =3D p->kvm; =20 /* @@ -328,16 +327,6 @@ static void kvm_irqfd_register(struct file *file, wait= _queue_head_t *wqh, if (p->ret) goto out; =20 - list_for_each_entry(tmp, &kvm->irqfds.items, list) { - if (irqfd->eventfd !=3D tmp->eventfd) - continue; - - WARN_ON_ONCE(1); - /* This fd is used for another irq already. */ - p->ret =3D -EBUSY; - goto out; - } - list_add_tail(&irqfd->list, &kvm->irqfds.items); =20 out: --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 2025 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 E980E21CC71 for ; Mon, 19 May 2025 18:55:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680938; cv=none; b=n+XTDhRNQqW6ve0LANDakOWq0GJDyKCgRqhGFLaqJxxEZLIloB/dNsK7GGH3xT5e9/JR/Ie4w4TjdRIdPfszzHdbtVcrQn8+CpaBD2tDl4NvGo9nU9mh9Lpn8hG5a1XxxhN3q8sHcNZmb8ROqYpG57VaF+zYFHHUnxu8sio1hm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680938; c=relaxed/simple; bh=lKEiDtco8Zjxw+6Ce1l/2Woozc6T4pKTzIMLnMhgjbo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jsWzUpiliOrjJMKToxyWY1atNSPDq2O0uUhUrT+d7EuKPi2VhYDWq8s02rX4BI7g8KLWgMzpOkrMXeN5K+C0JjbR4RppiCyePOvapK2eIZoVidrq4iap1Hm9La8VOM1fQA+tkOEDUWM1hS+kAwI/G59Gp6AUpwz6fKXJwkSjo9k= 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=C1qV5YPU; arc=none smtp.client-ip=209.85.215.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="C1qV5YPU" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b269789425bso5151439a12.0 for ; Mon, 19 May 2025 11:55:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680936; x=1748285736; 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=svZAfnlSTeaMolfrtcaOq/xEYvu0qCBcCbJ0k6qFXts=; b=C1qV5YPUAoBNllBUn6ZmJFZwSzjmhIqiIJySHKb79Tv36ERidwG57S63kTJDt3AQJQ WbDYxChybdc/zKc6hQCRt0IBQpk9lL6LIHFOQks+x9EM/XyEAohpG2UkIr1hie/XTcLR s1q/P+i6/tMKZUlkqOAbPDKNuYVUOvXRmXIrMP34veqb13n5vFLwUIxP/fmoS7ALl5f6 BKALDg/17qGA30pCUAR7ZEABHh/L5fVUWPI277D4SfjWHE+RspIbE7ZV1w2ZGIX5hm9/ +96jzgkvEn6fKra598JqEIbsi0JWy5tLDzQ2Plnq/70OzKTYsckduq1vxbA7JNzehFt3 2mBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680936; x=1748285736; 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=svZAfnlSTeaMolfrtcaOq/xEYvu0qCBcCbJ0k6qFXts=; b=oW1G1womQjMv0HNpNFkjpn4ZL756YLkKSdbvBw8HY4s+gfSl+NyXH4co3gOfPoVUep /Nhp6R8EwRWJicMv97ZpFasP4w1Nr81Xx5kA5h+0+0pbekktn6jQXK0ceDhLbi+ENwIi Ay3NbUaA8otqm9xnSrqsyMWYY3Wo5xToZ9aVYyMg6Jm//Yx9NC0dOvbrkwuHNfgTI1/v 3As1M2Up0hIyfdC4K3h4KHkwaPb8whO4w4ErL3mH2Nrf4wLMabpxVk2S5bQY4tiJK8L0 z8XF9XMfX5Wy1UM+cBbY1TLvMNlyR52Kbl7osyLB2v5PnQ55dC/+qT7EGijcoDa38waz vO4g== X-Forwarded-Encrypted: i=1; AJvYcCWDdb6lhGqH9dL1/0/2dwb7vNeMPugccS+8lDKAkV/ex8TSNBUkO6Z9e0mVhmjYMt80m/oG4vcoSRHae9w=@vger.kernel.org X-Gm-Message-State: AOJu0Yy8p80Kccn5S3jTIAz7l1/Ms81ieTyFp9MdPACNWHwZ76H3BGSp NUBfpHe5LK6pyW0dJgaDHV50puMDJcoH8DUAOAyuE2rdveKox0MJTv5DcBa8Gkd9ZMkoyusKMR3 leoWMwg== X-Google-Smtp-Source: AGHT+IHFHSV2Ke7uVRMojNlgnegTrMTQP9GQqHVO1VwR0CTBk3daaqGT0xYoclFSkFU0/yQOifv0EZ0lYtM= X-Received: from pjbsb5.prod.google.com ([2002:a17:90b:50c5:b0:301:2679:9d9]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d604:b0:2fe:ba82:ca5 with SMTP id 98e67ed59e1d1-30e7d52221bmr23752952a91.11.1747680936483; Mon, 19 May 2025 11:55:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:12 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-11-seanjc@google.com> Subject: [PATCH v2 10/12] KVM: selftests: Assert that eventfd() succeeds in Xen shinfo test From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Assert that eventfd() succeeds in the Xen shinfo test instead of skipping the associated testcase. While eventfd() is outside the scope of KVM, KVM unconditionally selects EVENTFD, i.e. the syscall should always succeed. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86/xen_shinfo_test.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/xen_shinfo_test.c b/tools/test= ing/selftests/kvm/x86/xen_shinfo_test.c index 287829f850f7..34d180cf4eed 100644 --- a/tools/testing/selftests/kvm/x86/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86/xen_shinfo_test.c @@ -548,14 +548,11 @@ int main(int argc, char *argv[]) =20 if (do_eventfd_tests) { irq_fd[0] =3D eventfd(0, 0); + TEST_ASSERT(irq_fd[0] >=3D 0, __KVM_SYSCALL_ERROR("eventfd()", irq_fd[0]= )); + irq_fd[1] =3D eventfd(0, 0); + TEST_ASSERT(irq_fd[1] >=3D 0, __KVM_SYSCALL_ERROR("eventfd()", irq_fd[1]= )); =20 - /* Unexpected, but not a KVM failure */ - if (irq_fd[0] =3D=3D -1 || irq_fd[1] =3D=3D -1) - do_evtchn_tests =3D do_eventfd_tests =3D false; - } - - if (do_eventfd_tests) { irq_routes.info.nr =3D 2; =20 irq_routes.entries[0].gsi =3D 32; --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 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 B73EF21D5A2 for ; Mon, 19 May 2025 18:55:38 +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=1747680940; cv=none; b=Jn68ftPd2kg9a71FSGSs0rYVgUYXx2DDvfd8k3dxH1rCsJXySxHYhFMgKtwl99o7fgOT1lJQd8p53hTjDNGQsQe9bI/fXgSv17dd2csrVVDNmd2VysKCpWrPExpRnjYvsyvgLs9BE8DkMh0q7u2YEaBjwECrArt3M2oJqXZDolc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680940; c=relaxed/simple; bh=02DhIDJiTY5SN586FcWil6j3M7CNOC2X358tokby4WI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qK7bXhbVPksnSvgtpICH0tnDkovMQLrIIltAhWKJWgfuJG5tWjvuat6yMZSqIiEhYf53ooFiwkOmqsqzI2BFBAR5de1OawiC9wEsj6+zz83k6L0b5Mkxf9cPb6llpqlkrQFCGCFt/VgzdM3x1iXok/nLs5x8hmWDdoab4SXOw1s= 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=MQLPdmGG; 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="MQLPdmGG" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30a39fa0765so7235407a91.3 for ; Mon, 19 May 2025 11:55:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680938; x=1748285738; 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=OKVb/rjt7xbg7iGYLrHxAz9ea9MJSgJFrfmn1LPAXvg=; b=MQLPdmGGNrurKK9Z76ndvFmSq/po1e8tqpkQfF319JrfT5pSrhOlFB66LQRT+SvzZr 9k77+XBCHjGsR9G6qtdArqASdCekVOSuv1pzyIs3tPtnB4B4kVFGqaXM3DNvHhyI/eKo 4HncYAjYVR8UILzZBeomjMxwaY3Bd9zyp/1qVXH+c4FPM+RWg2glq8+k+a9UWqt6iDrP bn5Kd+4eixnVzknvw/qfBcpv+vx3QiLCVUPSkhatdR3VsneW0+nc/NMD8p5bDXZICoK9 Ome9qK1UdMnD0pcX/l1QGRkRhutUfxn34pBGnT5l9QvtYCnxXrq4ZPYMUl9aXsewGH1D lLDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680938; x=1748285738; 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=OKVb/rjt7xbg7iGYLrHxAz9ea9MJSgJFrfmn1LPAXvg=; b=Jp7GRZREkgyb+CGqDcQNZlaMYJhcVPRwrQB04MRjAO+PyzOaAWeLKcwqfIPIpr2PJC j7OJQlqlxSiA9Xxhv6V12uYqFZvoi3/3Z4NPmNim6GgAYy/B8PndMrUKHAQx5IutrzjL /gaHk6/3cBJpxlALW/8EP6tx/f9i9m4+DD7tLYBzUFfn1kD9lY6ULq87eK/fqa/25bAP v8ODu/R/oiglKG9b4j8r7Qya4NibPYP3z0LAmoTm1zOMC+b+fbsM9ny0srEPbp/NyzO/ uJzvsne1OlqhIGvHnfiLwdBWcUkQBrEnnYEOZK+s146t+3NjnrW08X86cgAmtrpPrLWu pcTQ== X-Forwarded-Encrypted: i=1; AJvYcCXGC6ql9bh66DictphIlxa3i9RP+TbNev1CY7XvA+nRwsCXX+1zZa6xejMw1x0DLSQxN0fs4Q5ya6Gwls8=@vger.kernel.org X-Gm-Message-State: AOJu0YzLVVHS7D846kNizDc7Mdy7VdXuHsEVL7PBx4zwJIoN7Y2DnUDV ZIkMDo533yRCYvxTvgDjbTSXKUIBJZ+KQF3TFi73m5NYJewnJPaV9omJqT1JbB5H0kkBRKtvz0s yTDnBmg== X-Google-Smtp-Source: AGHT+IEj2WzLh67Ixl+YoFxokm7fFAzAUdnIzgu7IZZuNo0LIfTZvWOpLGE1fjEGyPSa8CLIT1wrF7Tf+5Y= X-Received: from pjbqa8.prod.google.com ([2002:a17:90b:4fc8:b0:30e:85f1:6fa5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c08:b0:30e:6a10:bb41 with SMTP id 98e67ed59e1d1-30e7d5a85c1mr21316229a91.27.1747680938275; Mon, 19 May 2025 11:55:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:13 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-12-seanjc@google.com> Subject: [PATCH v2 11/12] KVM: selftests: Add utilities to create eventfds and do KVM_IRQFD From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add helpers to create eventfds and to (de)assign eventfds via KVM_IRQFD. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/arm64/vgic_irq.c | 12 ++---- .../testing/selftests/kvm/include/kvm_util.h | 40 +++++++++++++++++++ .../selftests/kvm/x86/xen_shinfo_test.c | 18 ++------- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/tools/testing/selftests/kvm/arm64/vgic_irq.c b/tools/testing/s= elftests/kvm/arm64/vgic_irq.c index f4ac28d53747..a09dd423c2d7 100644 --- a/tools/testing/selftests/kvm/arm64/vgic_irq.c +++ b/tools/testing/selftests/kvm/arm64/vgic_irq.c @@ -620,18 +620,12 @@ static void kvm_routing_and_irqfd_check(struct kvm_vm= *vm, * that no actual interrupt was injected for those cases. */ =20 - for (f =3D 0, i =3D intid; i < (uint64_t)intid + num; i++, f++) { - fd[f] =3D eventfd(0, 0); - TEST_ASSERT(fd[f] !=3D -1, __KVM_SYSCALL_ERROR("eventfd()", fd[f])); - } + for (f =3D 0, i =3D intid; i < (uint64_t)intid + num; i++, f++) + fd[f] =3D kvm_new_eventfd(); =20 for (f =3D 0, i =3D intid; i < (uint64_t)intid + num; i++, f++) { - struct kvm_irqfd irqfd =3D { - .fd =3D fd[f], - .gsi =3D i - MIN_SPI, - }; assert(i <=3D (uint64_t)UINT_MAX); - vm_ioctl(vm, KVM_IRQFD, &irqfd); + kvm_assign_irqfd(vm, i - MIN_SPI, fd[f]); } =20 for (f =3D 0, i =3D intid; i < (uint64_t)intid + num; i++, f++) { diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index a1fc52bbdf7a..befa3d8ea84c 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -18,6 +18,7 @@ #include #include =20 +#include #include =20 #include "kvm_util_arch.h" @@ -496,6 +497,45 @@ static inline int vm_get_stats_fd(struct kvm_vm *vm) return fd; } =20 +static inline int __kvm_irqfd(struct kvm_vm *vm, uint32_t gsi, int eventfd, + uint32_t flags) +{ + struct kvm_irqfd irqfd =3D { + .fd =3D eventfd, + .gsi =3D gsi, + .flags =3D flags, + .resamplefd =3D -1, + }; + + return __vm_ioctl(vm, KVM_IRQFD, &irqfd); +} + +static inline void kvm_irqfd(struct kvm_vm *vm, uint32_t gsi, int eventfd, + uint32_t flags) +{ + int ret =3D __kvm_irqfd(vm, gsi, eventfd, flags); + + TEST_ASSERT_VM_VCPU_IOCTL(!ret, KVM_IRQFD, ret, vm); +} + +static inline void kvm_assign_irqfd(struct kvm_vm *vm, uint32_t gsi, int e= ventfd) +{ + kvm_irqfd(vm, gsi, eventfd, 0); +} + +static inline void kvm_deassign_irqfd(struct kvm_vm *vm, uint32_t gsi, int= eventfd) +{ + kvm_irqfd(vm, gsi, eventfd, KVM_IRQFD_FLAG_DEASSIGN); +} + +static inline int kvm_new_eventfd(void) +{ + int fd =3D eventfd(0, 0); + + TEST_ASSERT(fd >=3D 0, __KVM_SYSCALL_ERROR("eventfd()", fd)); + return fd; +} + static inline void read_stats_header(int stats_fd, struct kvm_stats_header= *header) { ssize_t ret; diff --git a/tools/testing/selftests/kvm/x86/xen_shinfo_test.c b/tools/test= ing/selftests/kvm/x86/xen_shinfo_test.c index 34d180cf4eed..23909b501ac2 100644 --- a/tools/testing/selftests/kvm/x86/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86/xen_shinfo_test.c @@ -547,11 +547,8 @@ int main(int argc, char *argv[]) int irq_fd[2] =3D { -1, -1 }; =20 if (do_eventfd_tests) { - irq_fd[0] =3D eventfd(0, 0); - TEST_ASSERT(irq_fd[0] >=3D 0, __KVM_SYSCALL_ERROR("eventfd()", irq_fd[0]= )); - - irq_fd[1] =3D eventfd(0, 0); - TEST_ASSERT(irq_fd[1] >=3D 0, __KVM_SYSCALL_ERROR("eventfd()", irq_fd[1]= )); + irq_fd[0] =3D kvm_new_eventfd(); + irq_fd[1] =3D kvm_new_eventfd(); =20 irq_routes.info.nr =3D 2; =20 @@ -569,15 +566,8 @@ int main(int argc, char *argv[]) =20 vm_ioctl(vm, KVM_SET_GSI_ROUTING, &irq_routes.info); =20 - struct kvm_irqfd ifd =3D { }; - - ifd.fd =3D irq_fd[0]; - ifd.gsi =3D 32; - vm_ioctl(vm, KVM_IRQFD, &ifd); - - ifd.fd =3D irq_fd[1]; - ifd.gsi =3D 33; - vm_ioctl(vm, KVM_IRQFD, &ifd); + kvm_assign_irqfd(vm, 32, irq_fd[0]); + kvm_assign_irqfd(vm, 33, irq_fd[1]); =20 struct sigaction sa =3D { }; sa.sa_handler =3D handle_alrm; --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:21:14 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 81B65220689 for ; Mon, 19 May 2025 18:55:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680943; cv=none; b=OEqUN6mfb2fTb272T7jD890dkh0CGsnCSVl59lK/tj2gEOWRYsuqTYp24kxM6hrCDNc1yWDDQUhsneyv+K8VMfzlp8hLhau8egIS/A5c6SkDiZj+zshLouuxiKbw/Nrpk9ZRdHEodvXB6c24EVioGQ868CRqDrxg2QGiCWilVgs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747680943; c=relaxed/simple; bh=aQcu4y4UtLWz9/5R61mPVfQF3kh3SVAwkRKMIhfaWQo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ajgsN0UofNK4SfTroCXOeLWWIJeOvMo0MfBptdKM6FWxQAlhMZhoOJfixmyFCrZ/SJds2Nc7UnFd2fD9jCjQlmMBoc1Su2nccsAAgCGL3al4sylqD9ItowJbsMik6tHJhifJkHAMdaAZEFSXa57exZ1pD3kJ6O5B+FROrsKEcZM= 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=Yj+7c6W8; arc=none smtp.client-ip=209.85.215.201 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="Yj+7c6W8" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-af5310c1ac1so2833036a12.2 for ; Mon, 19 May 2025 11:55:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747680940; x=1748285740; 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=epd5QhukG4RMUXX9jB19LOAHKrcbjUAqfegEAiRpQQ4=; b=Yj+7c6W8sPAiigXNDZ6EdH9W9/2pvWeSx9479WJiEwJH9SEHVcm5edkxOYVc7ajijT jAK3U/tjeLjJfslI7RRhZDTh99pFh16OJwJkaYbdED/AegdTMyN8YPXFGAV05F7KvEka M+JyZAKVMbS6MIw2+ioCqaIlk5mJ9C6cfXovKpYDvN/UWpDA20JirpLueLbJVdOZsDw/ GiuVGwGriiCt2w+e3LzrAZqrm1eHIAmBtfvAlzJwTLXL0DjDZFvlH8HOStmq7BYejX9M XHF7H5TRG4Psig9pJ/gHRIkp1V2vN9x3UUI3t9OnI84tUQ8Om+yNNuTrAjKerHx6idbu K7Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747680940; x=1748285740; 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=epd5QhukG4RMUXX9jB19LOAHKrcbjUAqfegEAiRpQQ4=; b=o5I7BHFPDDIiH6KsKyQRarsMdRFv26OKukURkt0XQ7jrz7mLAW5Ma/axUzNyTvgmSH 3ypz90jRtAQdgPNwe7zvJ+mMQWIZFsSsfUEWPzVMpnpSa1rEs/tJXBdHOPXjGrlIx632 f9Fbg1jZWq/tHBu1e+Igf6n+PkdOgjiWNus6zTOuAVHeR2xWF6mCDed3wpmIEj+jmBwr EU2l/KYOf+ekAVwsveGNaz9b+MieWQ3HgG3rFVv2c5Cg/HqnSQKYPf0QMRMvtzM7LK0e Ph2swDoxFO/2L/7FCT82kaQAIzE4IlMMcxdYeSDJ8u7YquOIvjtDQDgAb5tO7QEGHItp vnOQ== X-Forwarded-Encrypted: i=1; AJvYcCVo4V6pgCFuwXOLCDuY4Oje5FxWe9rq1OV8PeiSLfqKLR+cqUK/SHBVOdi2H7+m3AQtC2lsrk6fLENRFjY=@vger.kernel.org X-Gm-Message-State: AOJu0YzdiJ/lxYBi/n9dqXp4Qq8FDMfd4XmGT8YeZh4XjDG/zHGkBAct z4PSckd2u3UG9OnSj94NHfXrQEnvFFZFMzxWhog0T01TXKIBYXxmrbScmSXoWQh7Szc+Mzm7RzQ IXqONIQ== X-Google-Smtp-Source: AGHT+IGBgkTOOJjqrOCr7f/TpoYr6FJS7i8wklMdXOmCklTJv4b98ohj2f5O8xsS1rt/iLaAKTKHNpn9mBQ= X-Received: from pjbdy14.prod.google.com ([2002:a17:90b:6ce:b0:2fc:1356:bcc3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:33c2:b0:2fe:afa7:eaf8 with SMTP id 98e67ed59e1d1-30e7d50b040mr21142830a91.13.1747680939950; Mon, 19 May 2025 11:55:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 11:55:14 -0700 In-Reply-To: <20250519185514.2678456-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: <20250519185514.2678456-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519185514.2678456-13-seanjc@google.com> Subject: [PATCH v2 12/12] KVM: selftests: Add a KVM_IRQFD test to verify uniqueness requirements From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, K Prateek Nayak , David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a selftest to verify that eventfd+irqfd bindings are globally unique, i.e. that KVM doesn't allow multiple irqfds to bind to a single eventfd, even across VMs. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/irqfd_test.c | 130 +++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 tools/testing/selftests/kvm/irqfd_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index 307ef31d3557..d118b86f6acf 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -55,6 +55,7 @@ TEST_PROGS_x86 +=3D x86/nx_huge_pages_test.sh TEST_GEN_PROGS_COMMON =3D demand_paging_test TEST_GEN_PROGS_COMMON +=3D dirty_log_test TEST_GEN_PROGS_COMMON +=3D guest_print_test +TEST_GEN_PROGS_COMMON +=3D irqfd_test TEST_GEN_PROGS_COMMON +=3D kvm_binary_stats_test TEST_GEN_PROGS_COMMON +=3D kvm_create_max_vcpus TEST_GEN_PROGS_COMMON +=3D kvm_page_table_test diff --git a/tools/testing/selftests/kvm/irqfd_test.c b/tools/testing/selft= ests/kvm/irqfd_test.c new file mode 100644 index 000000000000..286f2b15fde6 --- /dev/null +++ b/tools/testing/selftests/kvm/irqfd_test.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kvm_util.h" + +static struct kvm_vm *vm1; +static struct kvm_vm *vm2; +static int __eventfd; +static bool done; + +/* + * KVM de-assigns based on eventfd *and* GSI, but requires unique eventfds= when + * assigning (the API isn't symmetrical). Abuse the oddity and use a per-= task + * GSI base to avoid false failures due to cross-task de-assign, i.e. so t= hat + * the secondary doesn't de-assign the primary's eventfd and cause assign = to + * unexpectedly succeed on the primary. + */ +#define GSI_BASE_PRIMARY 0x20 +#define GSI_BASE_SECONDARY 0x30 + +static void juggle_eventfd_secondary(struct kvm_vm *vm, int eventfd) +{ + int r, i; + + /* + * The secondary task can encounter EBADF since the primary can close + * the eventfd at any time. And because the primary can recreate the + * eventfd, at the safe fd in the file table, the secondary can also + * encounter "unexpected" success, e.g. if the close+recreate happens + * between the first and second assignments. The secondary's role is + * mostly to antagonize KVM, not to detect bugs. + */ + for (i =3D 0; i < 2; i++) { + r =3D __kvm_irqfd(vm, GSI_BASE_SECONDARY, eventfd, 0); + TEST_ASSERT(!r || errno =3D=3D EBUSY || errno =3D=3D EBADF, + "Wanted success, EBUSY, or EBADF, r =3D %d, errno =3D %d", + r, errno); + + /* De-assign should succeed unless the eventfd was closed. */ + r =3D __kvm_irqfd(vm, GSI_BASE_SECONDARY + i, eventfd, KVM_IRQFD_FLAG_DE= ASSIGN); + TEST_ASSERT(!r || errno =3D=3D EBADF, + "De-assign should succeed unless the fd was closed"); + } +} + +static void *secondary_irqfd_juggler(void *ign) +{ + while (!READ_ONCE(done)) { + juggle_eventfd_secondary(vm1, READ_ONCE(__eventfd)); + juggle_eventfd_secondary(vm2, READ_ONCE(__eventfd)); + } + + return NULL; +} + +static void juggle_eventfd_primary(struct kvm_vm *vm, int eventfd) +{ + int r1, r2; + + /* + * At least one of the assigns should fail. KVM disallows assigning a + * single eventfd to multiple GSIs (or VMs), so it's possible that both + * assignments can fail, too. + */ + r1 =3D __kvm_irqfd(vm, GSI_BASE_PRIMARY, eventfd, 0); + TEST_ASSERT(!r1 || errno =3D=3D EBUSY, + "Wanted success or EBUSY, r =3D %d, errno =3D %d", r1, errno); + + r2 =3D __kvm_irqfd(vm, GSI_BASE_PRIMARY + 1, eventfd, 0); + TEST_ASSERT(r1 || (r2 && errno =3D=3D EBUSY), + "Wanted failure (EBUSY), r1 =3D %d, r2 =3D %d, errno =3D %d", + r1, r2, errno); + + /* + * De-assign should always succeed, even if the corresponding assign + * failed. + */ + kvm_irqfd(vm, GSI_BASE_PRIMARY, eventfd, KVM_IRQFD_FLAG_DEASSIGN); + kvm_irqfd(vm, GSI_BASE_PRIMARY + 1, eventfd, KVM_IRQFD_FLAG_DEASSIGN); +} + +int main(int argc, char *argv[]) +{ + pthread_t racing_thread; + int r, i; + + /* Create "full" VMs, as KVM_IRQFD requires an in-kernel IRQ chip. */ + vm1 =3D vm_create(1); + vm2 =3D vm_create(1); + + WRITE_ONCE(__eventfd, kvm_new_eventfd()); + + kvm_irqfd(vm1, 10, __eventfd, 0); + + r =3D __kvm_irqfd(vm1, 11, __eventfd, 0); + TEST_ASSERT(r && errno =3D=3D EBUSY, + "Wanted EBUSY, r =3D %d, errno =3D %d", r, errno); + + r =3D __kvm_irqfd(vm2, 12, __eventfd, 0); + TEST_ASSERT(r && errno =3D=3D EBUSY, + "Wanted EBUSY, r =3D %d, errno =3D %d", r, errno); + + kvm_irqfd(vm1, 11, READ_ONCE(__eventfd), KVM_IRQFD_FLAG_DEASSIGN); + kvm_irqfd(vm1, 12, READ_ONCE(__eventfd), KVM_IRQFD_FLAG_DEASSIGN); + kvm_irqfd(vm1, 13, READ_ONCE(__eventfd), KVM_IRQFD_FLAG_DEASSIGN); + kvm_irqfd(vm1, 14, READ_ONCE(__eventfd), KVM_IRQFD_FLAG_DEASSIGN); + kvm_irqfd(vm1, 10, READ_ONCE(__eventfd), KVM_IRQFD_FLAG_DEASSIGN); + + close(__eventfd); + + pthread_create(&racing_thread, NULL, secondary_irqfd_juggler, vm2); + + for (i =3D 0; i < 10000; i++) { + WRITE_ONCE(__eventfd, kvm_new_eventfd()); + + juggle_eventfd_primary(vm1, __eventfd); + juggle_eventfd_primary(vm2, __eventfd); + close(__eventfd); + } + + WRITE_ONCE(done, true); + pthread_join(racing_thread, NULL); +} --=20 2.49.0.1101.gccaa498523-goog