From nobody Fri Dec 19 17:18:56 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 5E151281351 for ; Fri, 16 May 2025 23:07:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747436865; cv=none; b=u/FcpLfqN3G98RoJ9M5h8HG6m9Bwc6eoBysz/P3IP51GJQAD8DgwLBc8jeZVxTTUQOr23AthN9INO8wuqdN+/4Z0PhJwok2RKu+kJxYOJlBEO1ra3mTTO9KJ0d/ZAJld6vnWc0tVbOrqMpd9J4u+bPY6LAAnmSiOqaMjho3EJtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747436865; c=relaxed/simple; bh=3tOfy0wPMzmmtgf16NbWP4Jo1oouiVgnLtYKdu2JdA8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hlUBrP+Ha8xTl8lKHGOm5OtZrvn6uSE8ooc/1X238HHicIc09OPzZNsuOs1AcK30VOtlaIKb2aav064xU+M+zBTfHmLtFlwuHJxnYKnvv2Ue8DPVhFiXdRO8mGjlF1GhuPDFe/RahYgAj8qRm3Qjxk/1jZqa3feaEUNKG8SZWvI= 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=qeM2KjK1; arc=none smtp.client-ip=209.85.210.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="qeM2KjK1" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-74089884644so2361395b3a.3 for ; Fri, 16 May 2025 16:07:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747436863; x=1748041663; 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=8ro5pucdXOXxQU01a4cvnDr2WLudpDkRjBvhYSrYOz0=; b=qeM2KjK1AGeOG36OoO3gmjhu7u6xb6qFo5F6OoNB27v58xzgwBfmxCetFDR4BM07SS ASTVaCJl0xXcg9frD4OPrSbna8W7BeKPB135ZtjFksl3hKoROFgQM/GZ9zV/Mp6x907A 4JDYzawIv+I3fdnJ/7iPdVluWbRkbti+ynqXuMcgD4P73z3CrBBGhE3aXkntnz2I/m6X FTopR2jbvjEEwfZiXVWNINsVpk0UvjI6MMGHo3xTCkvbGyc4P6lsawYAUTwRtgJsox2Z UC+rzlUaasl5PJob62xax8bm1X34CNNRp51F7CiUgDUYtCRvBK6M2C7XdYS8AJGg/Hdi uSEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747436863; x=1748041663; 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=8ro5pucdXOXxQU01a4cvnDr2WLudpDkRjBvhYSrYOz0=; b=J7VMRgcFlUrmB2iyeofGnoUTApYnQ40GuIPLIWwcHyvo7C3ChLYjZKfsy5o5guu3s5 WjYVAaEH+ucQtcHJMynZVTzA3VbP03CufwQn+DsvOajMeBK7mOWxITlW1k1RkKU5Vqnl KCTBWtySOb4h7AYYB5Zjgws8G2lzNBw+Yx3T0vWlcMHI4SlXqtne8JBFG0oxfnyAVJ8B ZCA9qZUnj+mFOkx2TcI2s+sDMz3ysVGH0aaifs421HZLb2P5jNMRkA17aKtZPwX2B4gL xmPYwbIXRDylqxKyn0PHTZwRmAl89xJ5wh7FMwwbCt81BYUe/dZUwq0gCCKnValKa8mr LtUg== X-Forwarded-Encrypted: i=1; AJvYcCW/FDlE3+PVF8dOUXdZ+XapLAQv0OKsDQn6DzJBOwAo9Bfp5snLdLIgszxIdadj1dN8/MI5bsEzALr0TBo=@vger.kernel.org X-Gm-Message-State: AOJu0YwWBsVAHNqvPMLTSTLK1dZO+Fd+bJwBeutgxfO/w022oWCi/LLg xCHY40QsfuigXYc9LjwvwuaCbXqPu0BvynohOvbZ4GTZWSMQ9mEg5FUWSfyps27P54de04qbrxI kwomXSg== X-Google-Smtp-Source: AGHT+IHuBd6leiNTcn0nzvJq72WI9YGScV8jwRnPwk8riDF4/dQUj4oqlwemFkXW+9J9DbTmCWfYlt852cA= X-Received: from pfbkm11.prod.google.com ([2002:a05:6a00:3c4b:b0:740:813:f7bb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:b56:b0:740:a52f:a126 with SMTP id d2e1a72fcca58-742accc52cbmr5169945b3a.9.1747436862766; Fri, 16 May 2025 16:07:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 16 May 2025 16:07:29 -0700 In-Reply-To: <20250516230734.2564775-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: <20250516230734.2564775-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1112.g889b7c5bd8-goog Message-ID: <20250516230734.2564775-4-seanjc@google.com> Subject: [PATCH v2 3/8] irqbypass: Take ownership of producer/consumer token tracking From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , "Michael S. Tsirkin" , Jason Wang , Alex Williamson Cc: kvm@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Kevin Tian , Oliver Upton , David Matlack , Like Xu , Binbin Wu , Yong He Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move ownership of IRQ bypass token tracking into irqbypass.ko, and explicitly require callers to pass an eventfd_ctx structure instead of a completely opaque token. Relying on producers and consumers to set the token appropriately is error prone, and hiding the fact that the token must be an eventfd_ctx pointer (for all intents and purposes) unnecessarily obfuscates the code and makes it more brittle. Reviewed-by: Kevin Tian Acked-by: Michael S. Tsirkin Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 4 +-- drivers/vfio/pci/vfio_pci_intrs.c | 9 +++---- drivers/vhost/vdpa.c | 8 +++--- include/linux/irqbypass.h | 35 +++++++++++++----------- virt/kvm/eventfd.c | 7 +++-- virt/lib/irqbypass.c | 44 ++++++++++++++++++++----------- 6 files changed, 58 insertions(+), 49 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f9f798f286ce..c219aab2187f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13667,8 +13667,8 @@ void kvm_arch_irq_bypass_del_producer(struct irq_by= pass_consumer *cons, ret =3D kvm_x86_call(pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 0); if (ret) - printk(KERN_INFO "irq bypass consumer (token %p) unregistration" - " fails: %d\n", irqfd->consumer.token, ret); + printk(KERN_INFO "irq bypass consumer (eventfd %p) unregistration" + " fails: %d\n", irqfd->consumer.eventfd, ret); =20 spin_unlock_irq(&kvm->irqfds.lock); =20 diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_= intrs.c index 565966351dfa..d87fe116762a 100644 --- a/drivers/vfio/pci/vfio_pci_intrs.c +++ b/drivers/vfio/pci/vfio_pci_intrs.c @@ -505,15 +505,12 @@ static int vfio_msi_set_vector_signal(struct vfio_pci= _core_device *vdev, if (ret) goto out_put_eventfd_ctx; =20 - ctx->producer.token =3D trigger; ctx->producer.irq =3D irq; - ret =3D irq_bypass_register_producer(&ctx->producer); + ret =3D irq_bypass_register_producer(&ctx->producer, trigger); if (unlikely(ret)) { dev_info(&pdev->dev, - "irq bypass producer (token %p) registration fails: %d\n", - ctx->producer.token, ret); - - ctx->producer.token =3D NULL; + "irq bypass producer (eventfd %p) registration fails: %d\n", + trigger, ret); } ctx->trigger =3D trigger; =20 diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 5a49b5a6d496..7b265ffda697 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -213,10 +213,10 @@ static void vhost_vdpa_setup_vq_irq(struct vhost_vdpa= *v, u16 qid) return; =20 vq->call_ctx.producer.irq =3D irq; - ret =3D irq_bypass_register_producer(&vq->call_ctx.producer); + ret =3D irq_bypass_register_producer(&vq->call_ctx.producer, vq->call_ctx= .ctx); if (unlikely(ret)) - dev_info(&v->dev, "vq %u, irq bypass producer (token %p) registration fa= ils, ret =3D %d\n", - qid, vq->call_ctx.producer.token, ret); + dev_info(&v->dev, "vq %u, irq bypass producer (eventfd %p) registration = fails, ret =3D %d\n", + qid, vq->call_ctx.ctx, ret); } =20 static void vhost_vdpa_unsetup_vq_irq(struct vhost_vdpa *v, u16 qid) @@ -712,7 +712,6 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v= , unsigned int cmd, if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_DRIVER_OK) vhost_vdpa_unsetup_vq_irq(v, idx); - vq->call_ctx.producer.token =3D NULL; } break; } @@ -753,7 +752,6 @@ static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v= , unsigned int cmd, cb.callback =3D vhost_vdpa_virtqueue_cb; cb.private =3D vq; cb.trigger =3D vq->call_ctx.ctx; - vq->call_ctx.producer.token =3D vq->call_ctx.ctx; if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_DRIVER_OK) vhost_vdpa_setup_vq_irq(v, idx); diff --git a/include/linux/irqbypass.h b/include/linux/irqbypass.h index 9bdb2a781841..1b57d15ac4cf 100644 --- a/include/linux/irqbypass.h +++ b/include/linux/irqbypass.h @@ -10,6 +10,7 @@ =20 #include =20 +struct eventfd_ctx; struct irq_bypass_consumer; =20 /* @@ -18,20 +19,20 @@ struct irq_bypass_consumer; * The IRQ bypass manager is a simple set of lists and callbacks that allo= ws * IRQ producers (ex. physical interrupt sources) to be matched to IRQ * consumers (ex. virtualization hardware that allows IRQ bypass or offloa= d) - * via a shared token (ex. eventfd_ctx). Producers and consumers register - * independently. When a token match is found, the optional @stop callback - * will be called for each participant. The pair will then be connected v= ia - * the @add_* callbacks, and finally the optional @start callback will all= ow - * any final coordination. When either participant is unregistered, the - * process is repeated using the @del_* callbacks in place of the @add_* - * callbacks. Match tokens must be unique per producer/consumer, 1:N pair= ings - * are not supported. + * via a shared eventfd_ctx. Producers and consumers register independent= ly. + * When a producer and consumer are paired, i.e. an eventfd match is found= , the + * optional @stop callback will be called for each participant. The pair = will + * then be connected via the @add_* callbacks, and finally the optional @s= tart + * callback will allow any final coordination. When either participant is + * unregistered, the process is repeated using the @del_* callbacks in pla= ce of + * the @add_* callbacks. eventfds must be unique per producer/consumer, 1= :N + * pairings are not supported. */ =20 /** * struct irq_bypass_producer - IRQ bypass producer definition * @node: IRQ bypass manager private list management - * @token: opaque token to match between producer and consumer (non-NULL) + * @eventfd: eventfd context used to match producers and consumers * @irq: Linux IRQ number for the producer device * @add_consumer: Connect the IRQ producer to an IRQ consumer (optional) * @del_consumer: Disconnect the IRQ producer from an IRQ consumer (option= al) @@ -44,7 +45,7 @@ struct irq_bypass_consumer; */ struct irq_bypass_producer { struct list_head node; - void *token; + struct eventfd_ctx *eventfd; int irq; int (*add_consumer)(struct irq_bypass_producer *, struct irq_bypass_consumer *); @@ -57,7 +58,7 @@ struct irq_bypass_producer { /** * struct irq_bypass_consumer - IRQ bypass consumer definition * @node: IRQ bypass manager private list management - * @token: opaque token to match between producer and consumer (non-NULL) + * @eventfd: eventfd context used to match producers and consumers * @add_producer: Connect the IRQ consumer to an IRQ producer * @del_producer: Disconnect the IRQ consumer from an IRQ producer * @stop: Perform any quiesce operations necessary prior to add/del (optio= nal) @@ -70,7 +71,7 @@ struct irq_bypass_producer { */ struct irq_bypass_consumer { struct list_head node; - void *token; + struct eventfd_ctx *eventfd; int (*add_producer)(struct irq_bypass_consumer *, struct irq_bypass_producer *); void (*del_producer)(struct irq_bypass_consumer *, @@ -79,9 +80,11 @@ struct irq_bypass_consumer { void (*start)(struct irq_bypass_consumer *); }; =20 -int irq_bypass_register_producer(struct irq_bypass_producer *); -void irq_bypass_unregister_producer(struct irq_bypass_producer *); -int irq_bypass_register_consumer(struct irq_bypass_consumer *); -void irq_bypass_unregister_consumer(struct irq_bypass_consumer *); +int irq_bypass_register_producer(struct irq_bypass_producer *producer, + struct eventfd_ctx *eventfd); +void irq_bypass_unregister_producer(struct irq_bypass_producer *producer); +int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer, + struct eventfd_ctx *eventfd); +void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer); =20 #endif /* IRQBYPASS_H */ diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 11e5d1e3f12e..5bc6abe30748 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -426,15 +426,14 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *a= rgs) =20 #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS) if (kvm_arch_has_irq_bypass()) { - irqfd->consumer.token =3D (void *)irqfd->eventfd; irqfd->consumer.add_producer =3D kvm_arch_irq_bypass_add_producer; irqfd->consumer.del_producer =3D kvm_arch_irq_bypass_del_producer; irqfd->consumer.stop =3D kvm_arch_irq_bypass_stop; irqfd->consumer.start =3D kvm_arch_irq_bypass_start; - ret =3D irq_bypass_register_consumer(&irqfd->consumer); + ret =3D irq_bypass_register_consumer(&irqfd->consumer, irqfd->eventfd); if (ret) - pr_info("irq bypass consumer (token %p) registration fails: %d\n", - irqfd->consumer.token, ret); + pr_info("irq bypass consumer (eventfd %p) registration fails: %d\n", + irqfd->eventfd, ret); } #endif =20 diff --git a/virt/lib/irqbypass.c b/virt/lib/irqbypass.c index 28a4d933569a..e8d7c420db52 100644 --- a/virt/lib/irqbypass.c +++ b/virt/lib/irqbypass.c @@ -77,30 +77,32 @@ static void __disconnect(struct irq_bypass_producer *pr= od, /** * irq_bypass_register_producer - register IRQ bypass producer * @producer: pointer to producer structure + * @eventfd: pointer to the eventfd context associated with the producer * * Add the provided IRQ producer to the list of producers and connect - * with any matching token found on the IRQ consumers list. + * with any matching eventfd found on the IRQ consumers list. */ -int irq_bypass_register_producer(struct irq_bypass_producer *producer) +int irq_bypass_register_producer(struct irq_bypass_producer *producer, + struct eventfd_ctx *eventfd) { struct irq_bypass_producer *tmp; struct irq_bypass_consumer *consumer; int ret; =20 - if (!producer->token) + if (WARN_ON_ONCE(producer->eventfd)) return -EINVAL; =20 mutex_lock(&lock); =20 list_for_each_entry(tmp, &producers, node) { - if (tmp->token =3D=3D producer->token) { + if (tmp->eventfd =3D=3D eventfd) { ret =3D -EBUSY; goto out_err; } } =20 list_for_each_entry(consumer, &consumers, node) { - if (consumer->token =3D=3D producer->token) { + if (consumer->eventfd =3D=3D eventfd) { ret =3D __connect(producer, consumer); if (ret) goto out_err; @@ -108,6 +110,7 @@ int irq_bypass_register_producer(struct irq_bypass_prod= ucer *producer) } } =20 + producer->eventfd =3D eventfd; list_add(&producer->node, &producers); =20 mutex_unlock(&lock); @@ -131,26 +134,28 @@ void irq_bypass_unregister_producer(struct irq_bypass= _producer *producer) struct irq_bypass_producer *tmp; struct irq_bypass_consumer *consumer; =20 - if (!producer->token) + if (!producer->eventfd) return; =20 mutex_lock(&lock); =20 list_for_each_entry(tmp, &producers, node) { - if (tmp->token !=3D producer->token) + if (tmp->eventfd !=3D producer->eventfd) continue; =20 list_for_each_entry(consumer, &consumers, node) { - if (consumer->token =3D=3D producer->token) { + if (consumer->eventfd =3D=3D producer->eventfd) { __disconnect(producer, consumer); break; } } =20 + producer->eventfd =3D NULL; list_del(&producer->node); break; } =20 + WARN_ON_ONCE(producer->eventfd); mutex_unlock(&lock); } EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer); @@ -158,31 +163,35 @@ EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer); /** * irq_bypass_register_consumer - register IRQ bypass consumer * @consumer: pointer to consumer structure + * @eventfd: pointer to the eventfd context associated with the consumer * * Add the provided IRQ consumer to the list of consumers and connect - * with any matching token found on the IRQ producer list. + * with any matching eventfd found on the IRQ producer list. */ -int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer) +int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer, + struct eventfd_ctx *eventfd) { struct irq_bypass_consumer *tmp; struct irq_bypass_producer *producer; int ret; =20 - if (!consumer->token || - !consumer->add_producer || !consumer->del_producer) + if (WARN_ON_ONCE(consumer->eventfd)) + return -EINVAL; + + if (!consumer->add_producer || !consumer->del_producer) return -EINVAL; =20 mutex_lock(&lock); =20 list_for_each_entry(tmp, &consumers, node) { - if (tmp->token =3D=3D consumer->token || tmp =3D=3D consumer) { + if (tmp->eventfd =3D=3D eventfd) { ret =3D -EBUSY; goto out_err; } } =20 list_for_each_entry(producer, &producers, node) { - if (producer->token =3D=3D consumer->token) { + if (producer->eventfd =3D=3D eventfd) { ret =3D __connect(producer, consumer); if (ret) goto out_err; @@ -190,6 +199,7 @@ int irq_bypass_register_consumer(struct irq_bypass_cons= umer *consumer) } } =20 + consumer->eventfd =3D eventfd; list_add(&consumer->node, &consumers); =20 mutex_unlock(&lock); @@ -213,7 +223,7 @@ void irq_bypass_unregister_consumer(struct irq_bypass_c= onsumer *consumer) struct irq_bypass_consumer *tmp; struct irq_bypass_producer *producer; =20 - if (!consumer->token) + if (!consumer->eventfd) return; =20 mutex_lock(&lock); @@ -223,16 +233,18 @@ void irq_bypass_unregister_consumer(struct irq_bypass= _consumer *consumer) continue; =20 list_for_each_entry(producer, &producers, node) { - if (producer->token =3D=3D consumer->token) { + if (producer->eventfd =3D=3D consumer->eventfd) { __disconnect(producer, consumer); break; } } =20 + consumer->eventfd =3D NULL; list_del(&consumer->node); break; } =20 + WARN_ON_ONCE(consumer->eventfd); mutex_unlock(&lock); } EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer); --=20 2.49.0.1112.g889b7c5bd8-goog