From nobody Fri Dec 19 15:48:17 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