Skip to contentSkip to conversation

kvm: level irqfd and new eoifd

PATCH Superseded Alex Williamson 4 participants 96 messages Jul 16, 2012 – Jul 22, 2012

Subsystems (4)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover kvm: level irqfd and new eoifd

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.
- dropped eoifd gsi support patch as it was only an FYI.

Thanks,

Alex

---

Alex Williamson (4):
kvm: Convert eoifd to use kvm_clear_irq
kvm: Create kvm_clear_irq()
kvm: KVM_EOIFD, an eventfd for EOIs
kvm: Extend irqfd to support level interrupts

 Documentation/virtual/kvm/api.txt |   28 +++
 arch/x86/kvm/x86.c                |    3 
 include/linux/kvm.h               |   18 ++
 include/linux/kvm_host.h          |   16 ++
 virt/kvm/eventfd.c                |  333 +++++++++++++++++++++++++++++++++++++
 virt/kvm/irq_comm.c               |   78 +++++++++
 virt/kvm/kvm_main.c               |   11 +
 7 files changed, 483 insertions(+), 4 deletions(-)
#1 lore raw

PATCH v5 1/4 kvm: Extend irqfd to support level interrupts

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +110 -4

171 lines not on this page · raw message

#2 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

virt/kvm/eventfd.c irqfd_shutdown(struct work_struct *work) · line 191

Quoted 10 lines · Alex Williamson
virt/kvm/eventfd.c +3 -0
@@ -96,6 +183,9 @@ irqfd_shutdown(struct work_struct *work)
 	 * It is now safe to release the object's resources
 	 */
 	eventfd_ctx_put(irqfd->eventfd);
+
+	_irq_source_put(irqfd->source);
+
 	kfree(irqfd);
 }
 

Hang on, this is a bug I think. This is done asynchronously. So this means that I can assign MAX number of irqfds, then close one, and now assign will fail because deassign did not get freed.

Maybe we can solve this by flushing wq before assign? Looks a bit fragile but may be enough - need to document well.

Signature · 1 line
MST
#3 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 14 lines · Michael S. Tsirkin

virt/kvm/eventfd.c irqfd_shutdown(struct work_struct *work) · line 191

Quoted 10 lines · Alex Williamson
virt/kvm/eventfd.c +3 -0
@@ -96,6 +183,9 @@ irqfd_shutdown(struct work_struct *work)
 	 * It is now safe to release the object's resources
 	 */
 	eventfd_ctx_put(irqfd->eventfd);
+
+	_irq_source_put(irqfd->source);
+
 	kfree(irqfd);
 }
 

Hang on, this is a bug I think. This is done asynchronously. So this means that I can assign MAX number of irqfds, then close one, and now

                         ^^^^^^^^^^^^^^^^^^^^ What is this?

Do you mean max irq source ids?

Quoted 5 lines

assign will fail because deassign did not get freed.

Maybe we can solve this by flushing wq before assign? Looks a bit fragile but may be enough - need to document well.

#4 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 15 lines · Alex Williamson
Quoted 14 lines · Michael S. Tsirkin

virt/kvm/eventfd.c irqfd_shutdown(struct work_struct *work) · line 191

Quoted 10 lines · Alex Williamson
virt/kvm/eventfd.c +3 -0
@@ -96,6 +183,9 @@ irqfd_shutdown(struct work_struct *work)
 	 * It is now safe to release the object's resources
 	 */
 	eventfd_ctx_put(irqfd->eventfd);
+
+	_irq_source_put(irqfd->source);
+
 	kfree(irqfd);
 }
 

Hang on, this is a bug I think. This is done asynchronously. So this means that I can assign MAX number of irqfds, then close one, and now

Quoted 2 lines
                         ^^^^^^^^^^^^^^^^^^^^ What is this?

Do you mean max irq source ids?

Yes, this is what I meant. Sorry about being unclear.

Quoted 7 lines
Quoted 5 lines

assign will fail because deassign did not get freed.

Maybe we can solve this by flushing wq before assign? Looks a bit fragile but may be enough - need to document well.

#5 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 16 lines · Michael S. Tsirkin
Quoted 15 lines · Alex Williamson
Quoted 14 lines · Michael S. Tsirkin

virt/kvm/eventfd.c irqfd_shutdown(struct work_struct *work) · line 191

Quoted 10 lines · Alex Williamson
virt/kvm/eventfd.c +3 -0
@@ -96,6 +183,9 @@ irqfd_shutdown(struct work_struct *work)
 	 * It is now safe to release the object's resources
 	 */
 	eventfd_ctx_put(irqfd->eventfd);
+
+	_irq_source_put(irqfd->source);
+
 	kfree(irqfd);
 }
 

Hang on, this is a bug I think. This is done asynchronously. So this means that I can assign MAX number of irqfds, then close one, and now

Quoted 9 lines
Quoted 2 lines
                         ^^^^^^^^^^^^^^^^^^^^ What is this?

Do you mean max irq source ids?

Yes, this is what I meant. Sorry about being unclear.

Quoted 4 lines
Quoted 4 lines

assign will fail because deassign did not get freed.

Maybe we can solve this by flushing wq before assign? Looks a bit fragile but may be enough - need to document well.

Why is this fragile? We could even make it part of a retry so we don't call it unless we need to.

#6 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 22 lines · Alex Williamson
Quoted 21 lines · Michael S. Tsirkin
Quoted 15 lines · Alex Williamson
Quoted 14 lines · Michael S. Tsirkin

virt/kvm/eventfd.c irqfd_shutdown(struct work_struct *work) · line 191

Quoted 10 lines · Alex Williamson
virt/kvm/eventfd.c +3 -0
@@ -96,6 +183,9 @@ irqfd_shutdown(struct work_struct *work)
 	 * It is now safe to release the object's resources
 	 */
 	eventfd_ctx_put(irqfd->eventfd);
+
+	_irq_source_put(irqfd->source);
+
 	kfree(irqfd);
 }
 

Hang on, this is a bug I think. This is done asynchronously. So this means that I can assign MAX number of irqfds, then close one, and now

Quoted 2 lines
                         ^^^^^^^^^^^^^^^^^^^^ What is this?

Do you mean max irq source ids?

Yes, this is what I meant. Sorry about being unclear.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

assign will fail because deassign did not get freed.

Maybe we can solve this by flushing wq before assign? Looks a bit fragile but may be enough - need to document well.

Why is this fragile? We could even make it part of a retry so we don't call it unless we need to.

It just ties in assign and deassign. Maybe it's ok - but pls add a comment explaining the whole design.

Signature · 1 line
MST
#7 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

In reply to Alex Williamson
Quoted 177 lines · Alex Williamson

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +78 -1
index 7d7e2aa..ecdbfea 100644
@@ -36,6 +36,68 @@
 #include "iodev.h"
 
 /*
+ * An irq_source_id can be created from KVM_IRQFD for level interrupt
+ * injections and shared with other interfaces for EOI or de-assert.
+ * Create an object with reference counting to make it easy to use.
+ */
+struct _irq_source {
+	int id; /* the IRQ source ID */
+	bool level_asserted; /* Track assertion state and protect with lock */
+	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
+	struct kvm *kvm;
+	struct kref kref;
+};
+
+static void _irq_source_release(struct kref *kref)
+{
+	struct _irq_source *source;
+
+	source = container_of(kref, struct _irq_source, kref);
+
+	/* This also de-asserts */
+	kvm_free_irq_source_id(source->kvm, source->id);
+	kfree(source);
+}
+
+static void _irq_source_put(struct _irq_source *source)
+{
+	if (source)
+		kref_put(&source->kref, _irq_source_release);
+}
+
+static struct _irq_source *__attribute__ ((used)) /* white lie for now */
+_irq_source_get(struct _irq_source *source)
+{
+	if (source)
+		kref_get(&source->kref);
+
+	return source;
+}
+
+static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
+{
+	struct _irq_source *source;
+	int id;
+
+	source = kzalloc(sizeof(*source), GFP_KERNEL);
+	if (!source)
+		return ERR_PTR(-ENOMEM);
+
+	id = kvm_request_irq_source_id(kvm);
+	if (id < 0) {
+		kfree(source);
+		return ERR_PTR(id);
+	}
+
+	kref_init(&source->kref);
+	spin_lock_init(&source->lock);
+	source->kvm = kvm;
+	source->id = id;
+
+	return source;
+}
+
+/*
  * --------------------------------------------------------------------
  * irqfd: Allows an fd to be used to inject an interrupt to the guest
  *
@@ -52,6 +114,8 @@ struct _irqfd {
 	/* Used for level IRQ fast-path */
 	int gsi;
 	struct work_struct inject;
+	/* IRQ source ID for level triggered irqfds */
+	struct _irq_source *source;
 	/* Used for setup/shutdown */
 	struct eventfd_ctx *eventfd;
 	struct list_head list;
@@ -62,7 +126,7 @@ struct _irqfd {
 static struct workqueue_struct *irqfd_cleanup_wq;
 
 static void
-irqfd_inject(struct work_struct *work)
+irqfd_inject_edge(struct work_struct *work)
 {
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 	struct kvm *kvm = irqfd->kvm;
@@ -71,6 +135,29 @@ irqfd_inject(struct work_struct *work)
 	kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
 }
 
+static void
+irqfd_inject_level(struct work_struct *work)
+{
+	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
+
+	/*
+	 * Inject an interrupt only if not already asserted.
+	 *
+	 * We can safely ignore the kvm_set_irq return value here.  If
+	 * masked, the irr bit is still set and will eventually be serviced.
+	 * This interface does not guarantee immediate injection.  If
+	 * coalesced, an eoi will be coming where we can de-assert and
+	 * re-inject if necessary.  NB, if you need to know if an interrupt

virt/kvm/eventfd.c irqfd_inject(struct work_struct *work) · line 160

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+	 * was coalesced, this interface is not for you.
+	 */
+	spin_lock(&irqfd->source->lock);
+	if (!irqfd->source->level_asserted) {
+		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
+		irqfd->source->level_asserted = true;
+	}
+	spin_unlock(&irqfd->source->lock);
+}
+

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

Quoted 66 lines
virt/kvm/eventfd.c +22 -3
 /*
  * Race-free decouple logic (ordering is critical)
  */
@@ -96,6 +183,9 @@ irqfd_shutdown(struct work_struct *work)
 	 * It is now safe to release the object's resources
 	 */
 	eventfd_ctx_put(irqfd->eventfd);
+
+	_irq_source_put(irqfd->source);
+
 	kfree(irqfd);
 }
 
@@ -202,6 +292,7 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
 {
 	struct kvm_irq_routing_table *irq_rt;
 	struct _irqfd *irqfd, *tmp;
+	struct _irq_source *source = NULL;
 	struct file *file = NULL;
 	struct eventfd_ctx *eventfd = NULL;
 	int ret;
@@ -214,7 +305,19 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
 	irqfd->kvm = kvm;
 	irqfd->gsi = args->gsi;
 	INIT_LIST_HEAD(&irqfd->list);
-	INIT_WORK(&irqfd->inject, irqfd_inject);
+
+	if (args->flags & KVM_IRQFD_FLAG_LEVEL) {
+		source = _irq_source_alloc(kvm);
+		if (IS_ERR(source)) {
+			ret = PTR_ERR(source);
+			goto fail;
+		}
+
+		irqfd->source = source;
+		INIT_WORK(&irqfd->inject, irqfd_inject_level);
+	} else
+		INIT_WORK(&irqfd->inject, irqfd_inject_edge);
+
 	INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
 
 	file = eventfd_fget(args->fd);
@@ -276,10 +379,13 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
 	return 0;
 
 fail:
+	if (source && !IS_ERR(source))
+		_irq_source_put(source);
+
 	if (eventfd && !IS_ERR(eventfd))
 		eventfd_ctx_put(eventfd);
 
-	if (!IS_ERR(file))
+	if (file && !IS_ERR(file))
 		fput(file);
 
 	kfree(irqfd);
@@ -340,7 +446,7 @@ kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args)
 int
 kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
 {
-	if (args->flags & ~KVM_IRQFD_FLAG_DEASSIGN)
+	if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_LEVEL))
 		return -EINVAL;
 
 	if (args->flags & KVM_IRQFD_FLAG_DEASSIGN)
#8 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 185 lines · Michael S. Tsirkin
Quoted 184 lines · Alex Williamson

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +85 -1
index 7d7e2aa..ecdbfea 100644
@@ -36,6 +36,68 @@
 #include "iodev.h"
 
 /*
+ * An irq_source_id can be created from KVM_IRQFD for level interrupt
+ * injections and shared with other interfaces for EOI or de-assert.
+ * Create an object with reference counting to make it easy to use.
+ */
+struct _irq_source {
+	int id; /* the IRQ source ID */
+	bool level_asserted; /* Track assertion state and protect with lock */
+	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
+	struct kvm *kvm;
+	struct kref kref;
+};
+
+static void _irq_source_release(struct kref *kref)
+{
+	struct _irq_source *source;
+
+	source = container_of(kref, struct _irq_source, kref);
+
+	/* This also de-asserts */
+	kvm_free_irq_source_id(source->kvm, source->id);
+	kfree(source);
+}
+
+static void _irq_source_put(struct _irq_source *source)
+{
+	if (source)
+		kref_put(&source->kref, _irq_source_release);
+}
+
+static struct _irq_source *__attribute__ ((used)) /* white lie for now */
+_irq_source_get(struct _irq_source *source)
+{
+	if (source)
+		kref_get(&source->kref);
+
+	return source;
+}
+
+static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
+{
+	struct _irq_source *source;
+	int id;
+
+	source = kzalloc(sizeof(*source), GFP_KERNEL);
+	if (!source)
+		return ERR_PTR(-ENOMEM);
+
+	id = kvm_request_irq_source_id(kvm);
+	if (id < 0) {
+		kfree(source);
+		return ERR_PTR(id);
+	}
+
+	kref_init(&source->kref);
+	spin_lock_init(&source->lock);
+	source->kvm = kvm;
+	source->id = id;
+
+	return source;
+}
+
+/*
  * --------------------------------------------------------------------
  * irqfd: Allows an fd to be used to inject an interrupt to the guest
  *
@@ -52,6 +114,8 @@ struct _irqfd {
 	/* Used for level IRQ fast-path */
 	int gsi;
 	struct work_struct inject;
+	/* IRQ source ID for level triggered irqfds */
+	struct _irq_source *source;
 	/* Used for setup/shutdown */
 	struct eventfd_ctx *eventfd;
 	struct list_head list;
@@ -62,7 +126,7 @@ struct _irqfd {
 static struct workqueue_struct *irqfd_cleanup_wq;
 
 static void
-irqfd_inject(struct work_struct *work)
+irqfd_inject_edge(struct work_struct *work)
 {
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 	struct kvm *kvm = irqfd->kvm;
@@ -71,6 +135,29 @@ irqfd_inject(struct work_struct *work)
 	kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
 }
 
+static void
+irqfd_inject_level(struct work_struct *work)
+{
+	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
+
+	/*
+	 * Inject an interrupt only if not already asserted.
+	 *
+	 * We can safely ignore the kvm_set_irq return value here.  If
+	 * masked, the irr bit is still set and will eventually be serviced.
+	 * This interface does not guarantee immediate injection.  If
+	 * coalesced, an eoi will be coming where we can de-assert and
+	 * re-inject if necessary.  NB, if you need to know if an interrupt
+	 * was coalesced, this interface is not for you.
+	 */
+	spin_lock(&irqfd->source->lock);
+	if (!irqfd->source->level_asserted) {
+		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
+		irqfd->source->level_asserted = true;
+	}
Quoted 9 lines

virt/kvm/eventfd.c irqfd_inject(struct work_struct *work) · line 160

Quoted 3 lines
virt/kvm/eventfd.c +3 -0
+	spin_unlock(&irqfd->source->lock);
+}
+

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

--
Gleb.

#9 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 195 lines · Gleb Natapov
Quoted 185 lines · Michael S. Tsirkin
Quoted 184 lines · Alex Williamson

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +85 -1
index 7d7e2aa..ecdbfea 100644
@@ -36,6 +36,68 @@
 #include "iodev.h"
 
 /*
+ * An irq_source_id can be created from KVM_IRQFD for level interrupt
+ * injections and shared with other interfaces for EOI or de-assert.
+ * Create an object with reference counting to make it easy to use.
+ */
+struct _irq_source {
+	int id; /* the IRQ source ID */
+	bool level_asserted; /* Track assertion state and protect with lock */
+	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
+	struct kvm *kvm;
+	struct kref kref;
+};
+
+static void _irq_source_release(struct kref *kref)
+{
+	struct _irq_source *source;
+
+	source = container_of(kref, struct _irq_source, kref);
+
+	/* This also de-asserts */
+	kvm_free_irq_source_id(source->kvm, source->id);
+	kfree(source);
+}
+
+static void _irq_source_put(struct _irq_source *source)
+{
+	if (source)
+		kref_put(&source->kref, _irq_source_release);
+}
+
+static struct _irq_source *__attribute__ ((used)) /* white lie for now */
+_irq_source_get(struct _irq_source *source)
+{
+	if (source)
+		kref_get(&source->kref);
+
+	return source;
+}
+
+static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
+{
+	struct _irq_source *source;
+	int id;
+
+	source = kzalloc(sizeof(*source), GFP_KERNEL);
+	if (!source)
+		return ERR_PTR(-ENOMEM);
+
+	id = kvm_request_irq_source_id(kvm);
+	if (id < 0) {
+		kfree(source);
+		return ERR_PTR(id);
+	}
+
+	kref_init(&source->kref);
+	spin_lock_init(&source->lock);
+	source->kvm = kvm;
+	source->id = id;
+
+	return source;
+}
+
+/*
  * --------------------------------------------------------------------
  * irqfd: Allows an fd to be used to inject an interrupt to the guest
  *
@@ -52,6 +114,8 @@ struct _irqfd {
 	/* Used for level IRQ fast-path */
 	int gsi;
 	struct work_struct inject;
+	/* IRQ source ID for level triggered irqfds */
+	struct _irq_source *source;
 	/* Used for setup/shutdown */
 	struct eventfd_ctx *eventfd;
 	struct list_head list;
@@ -62,7 +126,7 @@ struct _irqfd {
 static struct workqueue_struct *irqfd_cleanup_wq;
 
 static void
-irqfd_inject(struct work_struct *work)
+irqfd_inject_edge(struct work_struct *work)
 {
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 	struct kvm *kvm = irqfd->kvm;
@@ -71,6 +135,29 @@ irqfd_inject(struct work_struct *work)
 	kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
 }
 
+static void
+irqfd_inject_level(struct work_struct *work)
+{
+	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
+
+	/*
+	 * Inject an interrupt only if not already asserted.
+	 *
+	 * We can safely ignore the kvm_set_irq return value here.  If
+	 * masked, the irr bit is still set and will eventually be serviced.
+	 * This interface does not guarantee immediate injection.  If
+	 * coalesced, an eoi will be coming where we can de-assert and
+	 * re-inject if necessary.  NB, if you need to know if an interrupt
+	 * was coalesced, this interface is not for you.
+	 */
+	spin_lock(&irqfd->source->lock);
+	if (!irqfd->source->level_asserted) {
+		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
+		irqfd->source->level_asserted = true;
+	}
Quoted 9 lines

virt/kvm/eventfd.c irqfd_inject(struct work_struct *work) · line 160

Quoted 3 lines
virt/kvm/eventfd.c +3 -0
+	spin_unlock(&irqfd->source->lock);
+}
+

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

Quoted 2 lines

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

Quoted 2 lines

--
Gleb.

#10 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 196 lines · Michael S. Tsirkin
Quoted 195 lines · Gleb Natapov
Quoted 194 lines · Michael S. Tsirkin
Quoted 177 lines · Alex Williamson

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +78 -1
index 7d7e2aa..ecdbfea 100644
@@ -36,6 +36,68 @@
 #include "iodev.h"
 
 /*
+ * An irq_source_id can be created from KVM_IRQFD for level interrupt
+ * injections and shared with other interfaces for EOI or de-assert.
+ * Create an object with reference counting to make it easy to use.
+ */
+struct _irq_source {
+	int id; /* the IRQ source ID */
+	bool level_asserted; /* Track assertion state and protect with lock */
+	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
+	struct kvm *kvm;
+	struct kref kref;
+};
+
+static void _irq_source_release(struct kref *kref)
+{
+	struct _irq_source *source;
+
+	source = container_of(kref, struct _irq_source, kref);
+
+	/* This also de-asserts */
+	kvm_free_irq_source_id(source->kvm, source->id);
+	kfree(source);
+}
+
+static void _irq_source_put(struct _irq_source *source)
+{
+	if (source)
+		kref_put(&source->kref, _irq_source_release);
+}
+
+static struct _irq_source *__attribute__ ((used)) /* white lie for now */
+_irq_source_get(struct _irq_source *source)
+{
+	if (source)
+		kref_get(&source->kref);
+
+	return source;
+}
+
+static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
+{
+	struct _irq_source *source;
+	int id;
+
+	source = kzalloc(sizeof(*source), GFP_KERNEL);
+	if (!source)
+		return ERR_PTR(-ENOMEM);
+
+	id = kvm_request_irq_source_id(kvm);
+	if (id < 0) {
+		kfree(source);
+		return ERR_PTR(id);
+	}
+
+	kref_init(&source->kref);
+	spin_lock_init(&source->lock);
+	source->kvm = kvm;
+	source->id = id;
+
+	return source;
+}
+
+/*
  * --------------------------------------------------------------------
  * irqfd: Allows an fd to be used to inject an interrupt to the guest
  *
@@ -52,6 +114,8 @@ struct _irqfd {
 	/* Used for level IRQ fast-path */
 	int gsi;
 	struct work_struct inject;
+	/* IRQ source ID for level triggered irqfds */
+	struct _irq_source *source;
 	/* Used for setup/shutdown */
 	struct eventfd_ctx *eventfd;
 	struct list_head list;
@@ -62,7 +126,7 @@ struct _irqfd {
 static struct workqueue_struct *irqfd_cleanup_wq;
 
 static void
-irqfd_inject(struct work_struct *work)
+irqfd_inject_edge(struct work_struct *work)
 {
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 	struct kvm *kvm = irqfd->kvm;
@@ -71,6 +135,29 @@ irqfd_inject(struct work_struct *work)
 	kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
 }
 
+static void
+irqfd_inject_level(struct work_struct *work)
+{
+	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
+
+	/*
+	 * Inject an interrupt only if not already asserted.
+	 *
+	 * We can safely ignore the kvm_set_irq return value here.  If
+	 * masked, the irr bit is still set and will eventually be serviced.
+	 * This interface does not guarantee immediate injection.  If
+	 * coalesced, an eoi will be coming where we can de-assert and
+	 * re-inject if necessary.  NB, if you need to know if an interrupt

virt/kvm/eventfd.c irqfd_inject(struct work_struct *work) · line 160

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+	 * was coalesced, this interface is not for you.
+	 */
+	spin_lock(&irqfd->source->lock);
+	if (!irqfd->source->level_asserted) {
+		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
+		irqfd->source->level_asserted = true;
+	}
+	spin_unlock(&irqfd->source->lock);
+}
+

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

Quoted 5 lines
Quoted 2 lines

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

--
Gleb.

#11 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 197 lines · Gleb Natapov
Quoted 196 lines · Michael S. Tsirkin
Quoted 195 lines · Gleb Natapov
Quoted 194 lines · Michael S. Tsirkin
Quoted 177 lines · Alex Williamson

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +78 -1
index 7d7e2aa..ecdbfea 100644
@@ -36,6 +36,68 @@
 #include "iodev.h"
 
 /*
+ * An irq_source_id can be created from KVM_IRQFD for level interrupt
+ * injections and shared with other interfaces for EOI or de-assert.
+ * Create an object with reference counting to make it easy to use.
+ */
+struct _irq_source {
+	int id; /* the IRQ source ID */
+	bool level_asserted; /* Track assertion state and protect with lock */
+	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
+	struct kvm *kvm;
+	struct kref kref;
+};
+
+static void _irq_source_release(struct kref *kref)
+{
+	struct _irq_source *source;
+
+	source = container_of(kref, struct _irq_source, kref);
+
+	/* This also de-asserts */
+	kvm_free_irq_source_id(source->kvm, source->id);
+	kfree(source);
+}
+
+static void _irq_source_put(struct _irq_source *source)
+{
+	if (source)
+		kref_put(&source->kref, _irq_source_release);
+}
+
+static struct _irq_source *__attribute__ ((used)) /* white lie for now */
+_irq_source_get(struct _irq_source *source)
+{
+	if (source)
+		kref_get(&source->kref);
+
+	return source;
+}
+
+static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
+{
+	struct _irq_source *source;
+	int id;
+
+	source = kzalloc(sizeof(*source), GFP_KERNEL);
+	if (!source)
+		return ERR_PTR(-ENOMEM);
+
+	id = kvm_request_irq_source_id(kvm);
+	if (id < 0) {
+		kfree(source);
+		return ERR_PTR(id);
+	}
+
+	kref_init(&source->kref);
+	spin_lock_init(&source->lock);
+	source->kvm = kvm;
+	source->id = id;
+
+	return source;
+}
+
+/*
  * --------------------------------------------------------------------
  * irqfd: Allows an fd to be used to inject an interrupt to the guest
  *
@@ -52,6 +114,8 @@ struct _irqfd {
 	/* Used for level IRQ fast-path */
 	int gsi;
 	struct work_struct inject;
+	/* IRQ source ID for level triggered irqfds */
+	struct _irq_source *source;
 	/* Used for setup/shutdown */
 	struct eventfd_ctx *eventfd;
 	struct list_head list;
@@ -62,7 +126,7 @@ struct _irqfd {
 static struct workqueue_struct *irqfd_cleanup_wq;
 
 static void
-irqfd_inject(struct work_struct *work)
+irqfd_inject_edge(struct work_struct *work)
 {
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 	struct kvm *kvm = irqfd->kvm;
@@ -71,6 +135,29 @@ irqfd_inject(struct work_struct *work)
 	kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
 }
 
+static void
+irqfd_inject_level(struct work_struct *work)
+{
+	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
+
+	/*
+	 * Inject an interrupt only if not already asserted.
+	 *
+	 * We can safely ignore the kvm_set_irq return value here.  If
+	 * masked, the irr bit is still set and will eventually be serviced.
+	 * This interface does not guarantee immediate injection.  If
+	 * coalesced, an eoi will be coming where we can de-assert and
+	 * re-inject if necessary.  NB, if you need to know if an interrupt

virt/kvm/eventfd.c irqfd_inject(struct work_struct *work) · line 160

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+	 * was coalesced, this interface is not for you.
+	 */
+	spin_lock(&irqfd->source->lock);
+	if (!irqfd->source->level_asserted) {
+		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
+		irqfd->source->level_asserted = true;
+	}
+	spin_unlock(&irqfd->source->lock);
+}
+

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$

?

Quoted 2 lines

--
Gleb.

#12 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 201 lines · Michael S. Tsirkin
Quoted 200 lines · Gleb Natapov
Quoted 199 lines · Michael S. Tsirkin
Quoted 197 lines · Gleb Natapov
Quoted 185 lines · Michael S. Tsirkin
Quoted 184 lines · Alex Williamson

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +85 -1
index 7d7e2aa..ecdbfea 100644
@@ -36,6 +36,68 @@
 #include "iodev.h"
 
 /*
+ * An irq_source_id can be created from KVM_IRQFD for level interrupt
+ * injections and shared with other interfaces for EOI or de-assert.
+ * Create an object with reference counting to make it easy to use.
+ */
+struct _irq_source {
+	int id; /* the IRQ source ID */
+	bool level_asserted; /* Track assertion state and protect with lock */
+	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
+	struct kvm *kvm;
+	struct kref kref;
+};
+
+static void _irq_source_release(struct kref *kref)
+{
+	struct _irq_source *source;
+
+	source = container_of(kref, struct _irq_source, kref);
+
+	/* This also de-asserts */
+	kvm_free_irq_source_id(source->kvm, source->id);
+	kfree(source);
+}
+
+static void _irq_source_put(struct _irq_source *source)
+{
+	if (source)
+		kref_put(&source->kref, _irq_source_release);
+}
+
+static struct _irq_source *__attribute__ ((used)) /* white lie for now */
+_irq_source_get(struct _irq_source *source)
+{
+	if (source)
+		kref_get(&source->kref);
+
+	return source;
+}
+
+static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
+{
+	struct _irq_source *source;
+	int id;
+
+	source = kzalloc(sizeof(*source), GFP_KERNEL);
+	if (!source)
+		return ERR_PTR(-ENOMEM);
+
+	id = kvm_request_irq_source_id(kvm);
+	if (id < 0) {
+		kfree(source);
+		return ERR_PTR(id);
+	}
+
+	kref_init(&source->kref);
+	spin_lock_init(&source->lock);
+	source->kvm = kvm;
+	source->id = id;
+
+	return source;
+}
+
+/*
  * --------------------------------------------------------------------
  * irqfd: Allows an fd to be used to inject an interrupt to the guest
  *
@@ -52,6 +114,8 @@ struct _irqfd {
 	/* Used for level IRQ fast-path */
 	int gsi;
 	struct work_struct inject;
+	/* IRQ source ID for level triggered irqfds */
+	struct _irq_source *source;
 	/* Used for setup/shutdown */
 	struct eventfd_ctx *eventfd;
 	struct list_head list;
@@ -62,7 +126,7 @@ struct _irqfd {
 static struct workqueue_struct *irqfd_cleanup_wq;
 
 static void
-irqfd_inject(struct work_struct *work)
+irqfd_inject_edge(struct work_struct *work)
 {
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 	struct kvm *kvm = irqfd->kvm;
@@ -71,6 +135,29 @@ irqfd_inject(struct work_struct *work)
 	kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
 }
 
+static void
+irqfd_inject_level(struct work_struct *work)
+{
+	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
+
+	/*
+	 * Inject an interrupt only if not already asserted.
+	 *
+	 * We can safely ignore the kvm_set_irq return value here.  If
+	 * masked, the irr bit is still set and will eventually be serviced.
+	 * This interface does not guarantee immediate injection.  If
+	 * coalesced, an eoi will be coming where we can de-assert and
+	 * re-inject if necessary.  NB, if you need to know if an interrupt
+	 * was coalesced, this interface is not for you.
+	 */
+	spin_lock(&irqfd->source->lock);
+	if (!irqfd->source->level_asserted) {
+		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
+		irqfd->source->level_asserted = true;
+	}
Quoted 9 lines

virt/kvm/eventfd.c irqfd_inject(struct work_struct *work) · line 160

Quoted 3 lines
virt/kvm/eventfd.c +3 -0
+	spin_unlock(&irqfd->source->lock);
+}
+

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Quoted 9 lines
Quoted 3 lines
Quoted 2 lines

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

--
Gleb.

#13 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 209 lines · Gleb Natapov
Quoted 208 lines · Michael S. Tsirkin
Quoted 197 lines · Gleb Natapov
Quoted 196 lines · Michael S. Tsirkin
Quoted 195 lines · Gleb Natapov
Quoted 194 lines · Michael S. Tsirkin
Quoted 177 lines · Alex Williamson

In order to inject a level interrupt from an external source using an irqfd, we need to allocate a new irq_source_id. This allows us to assert and (later) de-assert an interrupt line independently from users of KVM_IRQ_LINE and avoid lost interrupts.

We also add what may appear like a bit of excessive infrastructure around an object for storing this irq_source_id. However, notice that we only provide a way to assert the interrupt here. A follow-on interface will make use of the same irq_source_id to allow de-assert.

---

 Documentation/virtual/kvm/api.txt |    6 ++
 arch/x86/kvm/x86.c                |    1 
 include/linux/kvm.h               |    3 +
 virt/kvm/eventfd.c                |  114 ++++++++++++++++++++++++++++++++++++-
 4 files changed, 120 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +6 -0
index 100acde..c7267d5 100644
@@ -1981,6 +1981,12 @@ the guest using the specified gsi pin.  The irqfd is removed using
 the KVM_IRQFD_FLAG_DEASSIGN flag, specifying both kvm_irqfd.fd
 and kvm_irqfd.gsi.
 
+The KVM_IRQFD_FLAG_LEVEL flag indicates the gsi input is for a level
+triggered interrupt.  In this case a new irqchip input is allocated
+which is logically OR'd with other inputs allowing multiple sources
+to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
+is only necessary on setup, teardown is identical to that above.
+KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
 5. The kvm_run structure
 ------------------------
arch/x86/kvm/x86.c +1 -0
index a01a424..80bed07 100644
@@ -2148,6 +2148,7 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_GET_TSC_KHZ:
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
+	case KVM_CAP_IRQFD_LEVEL:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +3 -0
index 2ce09aa..b2e6e4f 100644
@@ -618,6 +618,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_GET_SMMU_INFO 78
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
+#define KVM_CAP_IRQFD_LEVEL 81
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -683,6 +684,8 @@ struct kvm_xen_hvm_config {
 #endif
 
 #define KVM_IRQFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_IRQFD_LEVEL */
+#define KVM_IRQFD_FLAG_LEVEL (1 << 1)
 
 struct kvm_irqfd {
 	__u32 fd;
virt/kvm/eventfd.c +78 -1
index 7d7e2aa..ecdbfea 100644
@@ -36,6 +36,68 @@
 #include "iodev.h"
 
 /*
+ * An irq_source_id can be created from KVM_IRQFD for level interrupt
+ * injections and shared with other interfaces for EOI or de-assert.
+ * Create an object with reference counting to make it easy to use.
+ */
+struct _irq_source {
+	int id; /* the IRQ source ID */
+	bool level_asserted; /* Track assertion state and protect with lock */
+	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
+	struct kvm *kvm;
+	struct kref kref;
+};
+
+static void _irq_source_release(struct kref *kref)
+{
+	struct _irq_source *source;
+
+	source = container_of(kref, struct _irq_source, kref);
+
+	/* This also de-asserts */
+	kvm_free_irq_source_id(source->kvm, source->id);
+	kfree(source);
+}
+
+static void _irq_source_put(struct _irq_source *source)
+{
+	if (source)
+		kref_put(&source->kref, _irq_source_release);
+}
+
+static struct _irq_source *__attribute__ ((used)) /* white lie for now */
+_irq_source_get(struct _irq_source *source)
+{
+	if (source)
+		kref_get(&source->kref);
+
+	return source;
+}
+
+static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
+{
+	struct _irq_source *source;
+	int id;
+
+	source = kzalloc(sizeof(*source), GFP_KERNEL);
+	if (!source)
+		return ERR_PTR(-ENOMEM);
+
+	id = kvm_request_irq_source_id(kvm);
+	if (id < 0) {
+		kfree(source);
+		return ERR_PTR(id);
+	}
+
+	kref_init(&source->kref);
+	spin_lock_init(&source->lock);
+	source->kvm = kvm;
+	source->id = id;
+
+	return source;
+}
+
+/*
  * --------------------------------------------------------------------
  * irqfd: Allows an fd to be used to inject an interrupt to the guest
  *
@@ -52,6 +114,8 @@ struct _irqfd {
 	/* Used for level IRQ fast-path */
 	int gsi;
 	struct work_struct inject;
+	/* IRQ source ID for level triggered irqfds */
+	struct _irq_source *source;
 	/* Used for setup/shutdown */
 	struct eventfd_ctx *eventfd;
 	struct list_head list;
@@ -62,7 +126,7 @@ struct _irqfd {
 static struct workqueue_struct *irqfd_cleanup_wq;
 
 static void
-irqfd_inject(struct work_struct *work)
+irqfd_inject_edge(struct work_struct *work)
 {
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 	struct kvm *kvm = irqfd->kvm;
@@ -71,6 +135,29 @@ irqfd_inject(struct work_struct *work)
 	kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
 }
 
+static void
+irqfd_inject_level(struct work_struct *work)
+{
+	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
+
+	/*
+	 * Inject an interrupt only if not already asserted.
+	 *
+	 * We can safely ignore the kvm_set_irq return value here.  If
+	 * masked, the irr bit is still set and will eventually be serviced.
+	 * This interface does not guarantee immediate injection.  If
+	 * coalesced, an eoi will be coming where we can de-assert and
+	 * re-inject if necessary.  NB, if you need to know if an interrupt

virt/kvm/eventfd.c irqfd_inject(struct work_struct *work) · line 160

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+	 * was coalesced, this interface is not for you.
+	 */
+	spin_lock(&irqfd->source->lock);
+	if (!irqfd->source->level_asserted) {
+		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
+		irqfd->source->level_asserted = true;
+	}
+	spin_unlock(&irqfd->source->lock);
+}
+

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

Quoted 2 lines

--
Gleb.

#14 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 22 lines · Michael S. Tsirkin
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Quoted 6 lines

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

No it isn't, I misread the code.

Anyway, maybe not trivially but this looks fixable to me: we could drop the ioapic lock before calling kvm_irq_delivery_to_apic.

Quoted 2 lines
Quoted 2 lines

--
Gleb.

#15 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 30 lines · Michael S. Tsirkin
Quoted 22 lines · Michael S. Tsirkin
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Quoted 6 lines

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

Quoted 6 lines

No it isn't, I misread the code.

Anyway, maybe not trivially but this looks fixable to me: we could drop the ioapic lock before calling kvm_irq_delivery_to_apic.

May be, may be not. Just saying "lets drop lock whenever we don't feel like holding one" does not cut it. Back to original point though current situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

--
Gleb.

#16 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Michael S. Tsirkin
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

Quoted 8 lines
Quoted 6 lines

No it isn't, I misread the code.

Anyway, maybe not trivially but this looks fixable to me: we could drop the ioapic lock before calling kvm_irq_delivery_to_apic.

May be, may be not. Just saying "lets drop lock whenever we don't feel like holding one" does not cut it.

One thing we do is set remote_irr if interrupt was injected. I agree these things are tricky.

One other question:

static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)

{
        union kvm_ioapic_redirect_entry *pent;
        int injected = -1;

        pent = &ioapic->redirtbl[idx];

        if (!pent->fields.mask) {
                injected = ioapic_deliver(ioapic, idx);
                if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
                        pent->fields.remote_irr = 1;
        }

        return injected;
}

This if (injected) looks a bit strange since ioapic_deliver returns -1 if no matching destinations. Should be if (injected > 0)?

Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Quoted 2 lines

--
Gleb.

#17 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 68 lines · Michael S. Tsirkin
Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Michael S. Tsirkin
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

Quoted 8 lines
Quoted 6 lines

No it isn't, I misread the code.

Anyway, maybe not trivially but this looks fixable to me: we could drop the ioapic lock before calling kvm_irq_delivery_to_apic.

May be, may be not. Just saying "lets drop lock whenever we don't feel like holding one" does not cut it.

One thing we do is set remote_irr if interrupt was injected. I agree these things are tricky.

One other question:

static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)

{
        union kvm_ioapic_redirect_entry *pent;
        int injected = -1;

        pent = &ioapic->redirtbl[idx];

        if (!pent->fields.mask) {
                injected = ioapic_deliver(ioapic, idx);
                if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
                        pent->fields.remote_irr = 1;
        }

        return injected;
}

This if (injected) looks a bit strange since ioapic_deliver returns -1 if no matching destinations. Should be if (injected > 0)?

Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

#18 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 79 lines · Alex Williamson
Quoted 68 lines · Michael S. Tsirkin
Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Michael S. Tsirkin
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

Quoted 8 lines
Quoted 6 lines

No it isn't, I misread the code.

Anyway, maybe not trivially but this looks fixable to me: we could drop the ioapic lock before calling kvm_irq_delivery_to_apic.

May be, may be not. Just saying "lets drop lock whenever we don't feel like holding one" does not cut it.

One thing we do is set remote_irr if interrupt was injected. I agree these things are tricky.

One other question:

static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)

{
        union kvm_ioapic_redirect_entry *pent;
        int injected = -1;

        pent = &ioapic->redirtbl[idx];

        if (!pent->fields.mask) {
                injected = ioapic_deliver(ioapic, idx);
                if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
                        pent->fields.remote_irr = 1;
        }

        return injected;
}

This if (injected) looks a bit strange since ioapic_deliver returns -1 if no matching destinations. Should be if (injected > 0)?

Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

Quoted 9 lines

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Signature · 1 line
MST
#19 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 87 lines · Michael S. Tsirkin
Quoted 86 lines · Alex Williamson
Quoted 68 lines · Michael S. Tsirkin
Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Michael S. Tsirkin
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

So as was discussed kvm_set_irq under spinlock is bad for scalability with multiple VCPUs. Why do we need a spinlock simply to protect level_asserted? Let's use an atomic test and set/test and clear and the problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

Quoted 8 lines
Quoted 6 lines

No it isn't, I misread the code.

Anyway, maybe not trivially but this looks fixable to me: we could drop the ioapic lock before calling kvm_irq_delivery_to_apic.

May be, may be not. Just saying "lets drop lock whenever we don't feel like holding one" does not cut it.

One thing we do is set remote_irr if interrupt was injected. I agree these things are tricky.

One other question:

static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)

{
        union kvm_ioapic_redirect_entry *pent;
        int injected = -1;

        pent = &ioapic->redirtbl[idx];

        if (!pent->fields.mask) {
                injected = ioapic_deliver(ioapic, idx);
                if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
                        pent->fields.remote_irr = 1;
        }

        return injected;
}

This if (injected) looks a bit strange since ioapic_deliver returns -1 if no matching destinations. Should be if (injected > 0)?

Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

Quoted 5 lines
Quoted 2 lines

The interrupt is now stuck on until another interrupt is injected.

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

#20 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 91 lines · Alex Williamson
Quoted 90 lines · Michael S. Tsirkin
Quoted 88 lines · Alex Williamson
Quoted 68 lines · Michael S. Tsirkin
Quoted 31 lines · Gleb Natapov
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Michael S. Tsirkin
Quoted 15 lines
Quoted 15 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

So as was discussed kvm_set_irq under spinlock is bad for scalability
with multiple VCPUs. Why do we need a spinlock simply to protect
level_asserted? Let's use an atomic test and set/test and clear and the
problem goes away.

That sad reality is that for level interrupt we already scan all vcpus under spinlock.

Where?

ioapic

$ grep kvm_for_each_vcpu virt/kvm/ioapic.c
$
Quoted 6 lines
Quoted 2 lines

?

Come on Michael. You can do better than grep and actually look at what code does. The code that loops over all vcpus while delivering an irq is in kvm_irq_delivery_to_apic(). Now grep for that.

Hmm, I see, it's actually done for edge if injected from ioapic too, right?

So set_irq does a linear scan, and for each matching CPU it calls kvm_irq_delivery_to_apic which is another scan?
So it's actually N^2 worst case for a broadcast?

Quoted 8 lines
Quoted 6 lines

No it isn't, I misread the code.

Anyway, maybe not trivially but this looks fixable to me: we could drop the ioapic lock before calling kvm_irq_delivery_to_apic.

May be, may be not. Just saying "lets drop lock whenever we don't feel like holding one" does not cut it.

One thing we do is set remote_irr if interrupt was injected. I agree these things are tricky.

One other question:

static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)

{
        union kvm_ioapic_redirect_entry *pent;
        int injected = -1;

        pent = &ioapic->redirtbl[idx];

        if (!pent->fields.mask) {
                injected = ioapic_deliver(ioapic, idx);
                if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
                        pent->fields.remote_irr = 1;
        }

        return injected;
}

This if (injected) looks a bit strange since ioapic_deliver returns -1 if no matching destinations. Should be if (injected > 0)?

Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

Quoted 6 lines
Quoted 2 lines

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

I guess if Avi OKs adding another kvm_set_irq under spinlock that's the best we can do for now.

If not, maybe we can teach kvm_set_irq to return an indication of the previous status. Specifically kvm_irq_line_state could do test_and_set/test_and_clear and if already set/clear we return 0 immediately.

Signature · 1 line
MST
#21 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 23 lines · Michael S. Tsirkin
Quoted 23 lines
Quoted 22 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

Quoted 9 lines

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Quoted 6 lines
Quoted 3 lines

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

I guess if Avi OKs adding another kvm_set_irq under spinlock that's the best we can do for now.

Why can't a mutex be used instead of a spinlock again?

#22 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Michael S. Tsirkin
Quoted 23 lines
Quoted 22 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

Quoted 9 lines

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

I guess if Avi OKs adding another kvm_set_irq under spinlock that's the best we can do for now.

Why can't a mutex be used instead of a spinlock again?

Why was it changed at the first place? Commit says that the function is called from unsleepable context, but no stack trace.

--
Gleb.

#23 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

In reply to Marcelo Tosatti
Quoted 24 lines · Marcelo Tosatti
Quoted 23 lines · Michael S. Tsirkin
Quoted 23 lines
Quoted 22 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

Quoted 9 lines

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

I guess if Avi OKs adding another kvm_set_irq under spinlock that's the best we can do for now.

Why can't a mutex be used instead of a spinlock again?

eventfd_signal calls the inject function from atomic context.

#24 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 29 lines · Alex Williamson
Quoted 28 lines · Marcelo Tosatti
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

Quoted 5 lines
Quoted 2 lines

The interrupt is now stuck on until another interrupt is injected.

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

I guess if Avi OKs adding another kvm_set_irq under spinlock that's the best we can do for now.

Why can't a mutex be used instead of a spinlock again?

eventfd_signal calls the inject function from atomic context.

Actually, that's called from a workq. I'll have to switch it back and turn on lockdep to remember why I couldn't sleep there.

#25 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 33 lines · Alex Williamson
Quoted 32 lines · Alex Williamson
Quoted 31 lines · Marcelo Tosatti
Quoted 29 lines · Michael S. Tsirkin
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

Quoted 6 lines
Quoted 2 lines

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

I guess if Avi OKs adding another kvm_set_irq under spinlock that's the best we can do for now.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Why can't a mutex be used instead of a spinlock again?

eventfd_signal calls the inject function from atomic context.

Actually, that's called from a workq. I'll have to switch it back and turn on lockdep to remember why I couldn't sleep there.

I'll try to fix kvm_set_irq so it returns 0 if level was already 0. Then you do not need extra state.

Signature · 1 line
MST
#26 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

In reply to Alex Williamson
Quoted 33 lines · Alex Williamson
Quoted 32 lines · Alex Williamson
Quoted 31 lines · Marcelo Tosatti
Quoted 29 lines · Michael S. Tsirkin
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 6 lines
Quoted 3 lines

Back to original point though current
situation is that calling kvm_set_irq() under spinlock is not worse for scalability than calling it not under one.

Yes. Still the specific use can just use an atomic flag, lock+bool is not needed, and we won't need to undo it later.

Actually, no, replacing it with an atomic is racy.

CPU0 (inject) CPU1 (EOI)
atomic_cmpxchg(&asserted, 0, 1)

                                    atomic_cmpxchg(&asserted, 1, 0)
                                    kvm_set_irq(0)

kvm_set_irq(1)
eventfd_signal

The interrupt is now stuck on until another interrupt is injected.

Quoted 6 lines
Quoted 2 lines

Well EOI somehow happened here before interrupt so it's a bug somewhere else?

Interrupts can be shared. We also can't guarantee that the guest won't write a bogus EOI to the ioapic. The irq ack notifier doesn't filter on irq source id... I'm not sure it can.

I guess if Avi OKs adding another kvm_set_irq under spinlock that's the best we can do for now.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Why can't a mutex be used instead of a spinlock again?

eventfd_signal calls the inject function from atomic context.

Actually, that's called from a workq. I'll have to switch it back and turn on lockdep to remember why I couldn't sleep there.

switching to a mutex results in:

BUG: sleeping function called from invalid context at kernel/mutex.c:269
in_atomic(): 1, irqs_disabled(): 0, pid: 30025, name: qemu-system-x86
INFO: lockdep is turned off.
Pid: 30025, comm: qemu-system-x86 Not tainted 3.5.0-rc4+ #109
Call Trace:
 [<ffffffff81088425>] __might_sleep+0xf5/0x130
 [<ffffffff81564c6f>] mutex_lock_nested+0x2f/0x60
 [<ffffffffa07db7d5>] eoifd_event+0x25/0x70 [kvm]
 [<ffffffffa07daea4>] kvm_notify_acked_irq+0xa4/0x140 [kvm]
 [<ffffffffa07dae2a>] ? kvm_notify_acked_irq+0x2a/0x140 [kvm]
 [<ffffffffa07d9bb4>] kvm_ioapic_update_eoi+0x84/0xf0 [kvm]
 [<ffffffffa0806c43>] apic_set_eoi+0x123/0x130 [kvm]
 [<ffffffffa0806fd8>] apic_reg_write+0x388/0x670 [kvm]
 [<ffffffffa07eb03c>] ? vcpu_enter_guest+0x32c/0x740 [kvm]
 [<ffffffffa0807481>] kvm_lapic_set_eoi+0x21/0x30 [kvm]
 [<ffffffffa04ba3f9>] handle_apic_access+0x69/0x80 [kvm_intel]
 [<ffffffffa04ba02a>] vmx_handle_exit+0xaa/0x260 [kvm_intel]
#27 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 13 lines · Alex Williamson
Quoted 1 line

turn on lockdep to remember why I couldn't sleep there.

switching to a mutex results in:

BUG: sleeping function called from invalid context at kernel/mutex.c:269
in_atomic(): 1, irqs_disabled(): 0, pid: 30025, name: qemu-system-x86
INFO: lockdep is turned off.
Pid: 30025, comm: qemu-system-x86 Not tainted 3.5.0-rc4+ #109
Call Trace:
 [<ffffffff81088425>] __might_sleep+0xf5/0x130
 [<ffffffff81564c6f>] mutex_lock_nested+0x2f/0x60
 [<ffffffffa07db7d5>] eoifd_event+0x25/0x70 [kvm]
 [<ffffffffa07daea4>] kvm_notify_acked_irq+0xa4/0x140 [kvm]
Quoted 8 lines
 [<ffffffffa07dae2a>] ? kvm_notify_acked_irq+0x2a/0x140 [kvm]
 [<ffffffffa07d9bb4>] kvm_ioapic_update_eoi+0x84/0xf0 [kvm]
 [<ffffffffa0806c43>] apic_set_eoi+0x123/0x130 [kvm]
 [<ffffffffa0806fd8>] apic_reg_write+0x388/0x670 [kvm]
 [<ffffffffa07eb03c>] ? vcpu_enter_guest+0x32c/0x740 [kvm]
 [<ffffffffa0807481>] kvm_lapic_set_eoi+0x21/0x30 [kvm]
 [<ffffffffa04ba3f9>] handle_apic_access+0x69/0x80 [kvm_intel]
 [<ffffffffa04ba02a>] vmx_handle_exit+0xaa/0x260 [kvm_intel]

Its RCU from ack notifiers, OK.

#28 lore raw

Re: [PATCH v5 1/4] kvm: Extend irqfd to support level interrupts

Quoted 23 lines · Marcelo Tosatti
Quoted 13 lines · Alex Williamson
Quoted 1 line

turn on lockdep to remember why I couldn't sleep there.

switching to a mutex results in:

BUG: sleeping function called from invalid context at kernel/mutex.c:269
in_atomic(): 1, irqs_disabled(): 0, pid: 30025, name: qemu-system-x86
INFO: lockdep is turned off.
Pid: 30025, comm: qemu-system-x86 Not tainted 3.5.0-rc4+ #109
Call Trace:
 [<ffffffff81088425>] __might_sleep+0xf5/0x130
 [<ffffffff81564c6f>] mutex_lock_nested+0x2f/0x60
 [<ffffffffa07db7d5>] eoifd_event+0x25/0x70 [kvm]
 [<ffffffffa07daea4>] kvm_notify_acked_irq+0xa4/0x140 [kvm]
Quoted 8 lines
 [<ffffffffa07dae2a>] ? kvm_notify_acked_irq+0x2a/0x140 [kvm]
 [<ffffffffa07d9bb4>] kvm_ioapic_update_eoi+0x84/0xf0 [kvm]
 [<ffffffffa0806c43>] apic_set_eoi+0x123/0x130 [kvm]
 [<ffffffffa0806fd8>] apic_reg_write+0x388/0x670 [kvm]
 [<ffffffffa07eb03c>] ? vcpu_enter_guest+0x32c/0x740 [kvm]
 [<ffffffffa0807481>] kvm_lapic_set_eoi+0x21/0x30 [kvm]
 [<ffffffffa04ba3f9>] handle_apic_access+0x69/0x80 [kvm_intel]
 [<ffffffffa04ba02a>] vmx_handle_exit+0xaa/0x260 [kvm_intel]
Quoted 1 line

Its RCU from ack notifiers, OK.

I'm testing a patch that moves all bitmap handling to under pic/ioapic lock. After this we can teach kvm_set_irq to report when a bit that is cleared/set is already clear/set, without races.

And then no tracking will be necessary in irqfd - we can just call kvm_set_irq(..., 0) and look at the return status.

Signature · 1 line
MST
#29 lore raw

PATCH v5 2/4 kvm: KVM_EOIFD, an eventfd for EOIs

In reply to Alex Williamson

This new ioctl enables an eventfd to be triggered when an EOI is written for a specified irqchip pin. The first user of this will be external device assignment through VFIO, using a level irqfd for asserting a PCI INTx interrupt and this interface for de-assert and notification once the interrupt is serviced.

Here we make use of the reference counting of the _irq_source object allowing us to share it with an irqfd and cleanup regardless of the release order.

---

 Documentation/virtual/kvm/api.txt |   22 +++
 arch/x86/kvm/x86.c                |    2 
 include/linux/kvm.h               |   15 ++
 include/linux/kvm_host.h          |   13 ++
 virt/kvm/eventfd.c                |  239 +++++++++++++++++++++++++++++++++++++
 virt/kvm/kvm_main.c               |   11 ++
 6 files changed, 300 insertions(+), 2 deletions(-)
Documentation/virtual/kvm/api.txt +22 -0
index c7267d5..9761f78 100644
@@ -1988,6 +1988,28 @@ to independently assert level interrupts.  The KVM_IRQFD_FLAG_LEVEL
 is only necessary on setup, teardown is identical to that above.
 KVM_IRQFD_FLAG_LEVEL support is indicated by KVM_CAP_IRQFD_LEVEL.
 
+4.77 KVM_EOIFD
+
+Capability: KVM_CAP_EOIFD
+Architectures: x86
+Type: vm ioctl
+Parameters: struct kvm_eoifd (in)
+Returns: 0 on success, -1 on error
+
+KVM_EOIFD allows userspace to receive interrupt EOI notification
+through an eventfd.  kvm_eoifd.fd specifies the eventfd used for
+notification.  KVM_EOIFD_FLAG_DEASSIGN is used to de-assign an eoifd
+once assigned.  KVM_EOIFD also requires additional bits set in
+kvm_eoifd.flags to bind to the proper interrupt line.  The
+KVM_EOIFD_FLAG_LEVEL_IRQFD indicates that kvm_eoifd.irqfd is provided
+and is an irqfd for a level triggered interrupt (configured from
+KVM_IRQFD using KVM_IRQFD_FLAG_LEVEL).  The EOI notification is bound
+to the same GSI and irqchip input as the irqfd.  Both kvm_eoifd.irqfd
+and KVM_EOIFD_FLAG_LEVEL_IRQFD must be specified both on assignment
+and de-assignment of KVM_EOIFD.  A level irqfd may only be bound to
+a single eoifd.  KVM_CAP_EOIFD_LEVEL_IRQFD indicates support of
+KVM_EOIFD_FLAG_LEVEL_IRQFD.
+
 5. The kvm_run structure
 ------------------------
 
arch/x86/kvm/x86.c +2 -0
index 80bed07..cc47e31 100644
@@ -2149,6 +2149,8 @@ int kvm_dev_ioctl_check_extension(long ext)
 	case KVM_CAP_PCI_2_3:
 	case KVM_CAP_KVMCLOCK_CTRL:
 	case KVM_CAP_IRQFD_LEVEL:
+	case KVM_CAP_EOIFD:
+	case KVM_CAP_EOIFD_LEVEL_IRQFD:
 		r = 1;
 		break;
 	case KVM_CAP_COALESCED_MMIO:
include/linux/kvm.h +15 -0
index b2e6e4f..5ca887d 100644
@@ -619,6 +619,8 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_S390_COW 79
 #define KVM_CAP_PPC_ALLOC_HTAB 80
 #define KVM_CAP_IRQFD_LEVEL 81
+#define KVM_CAP_EOIFD 82
+#define KVM_CAP_EOIFD_LEVEL_IRQFD 83
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -694,6 +696,17 @@ struct kvm_irqfd {
 	__u8  pad[20];
 };
 
+#define KVM_EOIFD_FLAG_DEASSIGN (1 << 0)
+/* Available with KVM_CAP_EOIFD_LEVEL_IRQFD */
+#define KVM_EOIFD_FLAG_LEVEL_IRQFD (1 << 1)
+
+struct kvm_eoifd {
+	__u32 fd;
+	__u32 flags;
+	__u32 irqfd;
+	__u8 pad[20];
+};
+
 struct kvm_clock_data {
 	__u64 clock;
 	__u32 flags;
@@ -834,6 +847,8 @@ struct kvm_s390_ucas_mapping {
 #define KVM_PPC_GET_SMMU_INFO	  _IOR(KVMIO,  0xa6, struct kvm_ppc_smmu_info)
 /* Available with KVM_CAP_PPC_ALLOC_HTAB */
 #define KVM_PPC_ALLOCATE_HTAB	  _IOWR(KVMIO, 0xa7, __u32)
+/* Available with KVM_CAP_EOIFD */
+#define KVM_EOIFD                 _IOW(KVMIO,  0xa8, struct kvm_eoifd)
 
 /*
  * ioctls for vcpu fds
include/linux/kvm_host.h +13 -0
index ae3b426..a7661c0 100644
@@ -285,6 +285,10 @@ struct kvm {
 		struct list_head  items;
 	} irqfds;
 	struct list_head ioeventfds;
+	struct {
+		struct mutex lock;
+		struct list_head items;
+	} eoifds;
 #endif
 	struct kvm_vm_stat stat;
 	struct kvm_arch arch;
@@ -828,6 +832,8 @@ int kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args);
 void kvm_irqfd_release(struct kvm *kvm);
 void kvm_irq_routing_update(struct kvm *, struct kvm_irq_routing_table *);
 int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args);
+int kvm_eoifd(struct kvm *kvm, struct kvm_eoifd *args);
+void kvm_eoifd_release(struct kvm *kvm);
 
 #else
 
@@ -853,6 +859,13 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 	return -ENOSYS;
 }
 
+static inline int kvm_eoifd(struct kvm *kvm, struct kvm_eoifd *args)
+{
+	return -ENOSYS;
+}
+
+static inline void kvm_eoifd_release(struct kvm *kvm) {}
+
 #endif /* CONFIG_HAVE_KVM_EVENTFD */
 
 #ifdef CONFIG_KVM_APIC_ARCHITECTURE
virt/kvm/eventfd.c +237 -2

260 lines not on this page · raw message

virt/kvm/kvm_main.c +11 -0
index b4ad14cc..5b41df1 100644
@@ -620,6 +620,8 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
 
 	kvm_irqfd_release(kvm);
 
+	kvm_eoifd_release(kvm);
+
 	kvm_put_kvm(kvm);
 	return 0;
 }
@@ -2093,6 +2095,15 @@ static long kvm_vm_ioctl(struct file *filp,
 		break;
 	}
 #endif
+	case KVM_EOIFD: {
+		struct kvm_eoifd data;
+
+		r = -EFAULT;
+		if (copy_from_user(&data, argp, sizeof data))
+			goto out;
+		r = kvm_eoifd(kvm, &data);
+		break;
+	}
 	default:
 		r = kvm_arch_vm_ioctl(filp, ioctl, arg);
 		if (r == -ENOTTY)
#30 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 17 lines · Alex Williamson
virt/kvm/eventfd.c +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

virt/kvm/eventfd.c kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 890

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Signature · 1 line
MST
#31 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Alex Williamson
virt/kvm/eventfd.c +23 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
Quoted 8 lines

virt/kvm/eventfd.c kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 890

Quoted 4 lines
virt/kvm/eventfd.c +4 -0
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

#32 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 34 lines · Alex Williamson
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Alex Williamson
virt/kvm/eventfd.c +23 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
Quoted 8 lines

virt/kvm/eventfd.c kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 890

Quoted 4 lines
virt/kvm/eventfd.c +4 -0
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Signature · 1 line
MST
#33 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
virt/kvm/eventfd.c +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

virt/kvm/eventfd.c kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 890

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

#34 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 43 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
virt/kvm/eventfd.c +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

virt/kvm/eventfd.c kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 890

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Quoted 11 lines

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

eoifd should prevent irqfd from being released. It already keeps a reference to it so it prevents irqfd from going away by userspace closing the fd. But it can still be released with deassign. An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

Signature · 1 line
MST
#35 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 56 lines · Michael S. Tsirkin
Quoted 43 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
virt/kvm/eventfd.c +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

virt/kvm/eventfd.c kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args) · line 890

Quoted 10 lines
virt/kvm/eventfd.c +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Quoted 11 lines

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

#36 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 57 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
(quoted diff) +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 8 lines
Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

No no. You *already* prevent it. You take a reference to the eventfd context.

Quoted 6 lines
Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

When the fd is no going away an ioctl is the only other way for it to go away.

Quoted 7 lines
Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

Well eventfd gets an irqfd. What does it mean if said irqfd gets deassigned, and potentially assigned an unrelated interrupt? I think what I would expect is for it to handle the new interrupt. This is hard to implement so let us fail this.

Signature · 1 line
MST
#37 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 67 lines · Michael S. Tsirkin
Quoted 57 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
(quoted diff) +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 8 lines
Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

When the fd is no going away an ioctl is the only other way for it to go away.

It doesn't do any good to fail the ioctl if close(fd) allows it.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

Quoted 8 lines
Quoted 3 lines

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

Well eventfd gets an irqfd. What does it mean if said irqfd gets deassigned, and potentially assigned an unrelated interrupt? I think what I would expect is for it to handle the new interrupt. This is hard to implement so let us fail this.

Ah, so an actual problem, let's solve this. Why wouldn't we just search the list of eoifds and see if this level_irqfd is already used? If we find it and it's compatible, we can get a reference to the _irq_source and "re-attach" the irqfd. If it's not compatible, fail the KVM_IRQFD. If the KVM_IRQFD is for an edge irqfd, I think we let it go.

#38 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 68 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 65 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 43 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
(quoted diff) +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Quoted 11 lines

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 4 lines
Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

When the fd is no going away an ioctl is the only other way for it to go away.

It doesn't do any good to fail the ioctl if close(fd) allows it.

allows what? It does nothing.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

Quoted 8 lines
Quoted 3 lines

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

Well eventfd gets an irqfd. What does it mean if said irqfd gets deassigned, and potentially assigned an unrelated interrupt? I think what I would expect is for it to handle the new interrupt. This is hard to implement so let us fail this.

Quoted 5 lines

Ah, so an actual problem, let's solve this. Why wouldn't we just search the list of eoifds and see if this level_irqfd is already used? If we find it and it's compatible, we can get a reference to the _irq_source and "re-attach" the irqfd. If it's not compatible, fail the KVM_IRQFD. If the KVM_IRQFD is for an edge irqfd, I think we let it go.

This is just confusing. Userspace has no idea that you are reusing fds behind the scenes. assign is not the problem, deassign is. So fail *that*.

Signature · 1 line
MST
#39 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 69 lines · Michael S. Tsirkin
Quoted 68 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 65 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 43 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
(quoted diff) +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Quoted 11 lines

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

How so?

Quoted 36 lines
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

When the fd is no going away an ioctl is the only other way for it to go away.

It doesn't do any good to fail the ioctl if close(fd) allows it.

allows what? It does nothing.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

Quoted 8 lines
Quoted 3 lines

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

Well eventfd gets an irqfd. What does it mean if said irqfd gets deassigned, and potentially assigned an unrelated interrupt? I think what I would expect is for it to handle the new interrupt. This is hard to implement so let us fail this.

Quoted 5 lines

Ah, so an actual problem, let's solve this. Why wouldn't we just search the list of eoifds and see if this level_irqfd is already used? If we find it and it's compatible, we can get a reference to the _irq_source and "re-attach" the irqfd. If it's not compatible, fail the KVM_IRQFD. If the KVM_IRQFD is for an edge irqfd, I think we let it go.

This is just confusing. Userspace has no idea that you are reusing fds behind the scenes. assign is not the problem, deassign is. So fail *that*.

#40 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 73 lines · Alex Williamson
Quoted 72 lines · Michael S. Tsirkin
Quoted 71 lines · Alex Williamson
Quoted 69 lines · Michael S. Tsirkin
Quoted 57 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
(quoted diff) +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 8 lines
Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

No no. You *already* prevent it. You take a reference to the eventfd context.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

Quoted 36 lines
Quoted 36 lines
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

When the fd is no going away an ioctl is the only other way for it to go away.

It doesn't do any good to fail the ioctl if close(fd) allows it.

allows what? It does nothing.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

Quoted 8 lines
Quoted 3 lines

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

Well eventfd gets an irqfd. What does it mean if said irqfd gets deassigned, and potentially assigned an unrelated interrupt? I think what I would expect is for it to handle the new interrupt. This is hard to implement so let us fail this.

Quoted 5 lines

Ah, so an actual problem, let's solve this. Why wouldn't we just search the list of eoifds and see if this level_irqfd is already used? If we find it and it's compatible, we can get a reference to the _irq_source and "re-attach" the irqfd. If it's not compatible, fail the KVM_IRQFD. If the KVM_IRQFD is for an edge irqfd, I think we let it go.

This is just confusing. Userspace has no idea that you are reusing fds behind the scenes. assign is not the problem, deassign is. So fail *that*.

#41 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 76 lines · Michael S. Tsirkin
Quoted 75 lines · Alex Williamson
Quoted 74 lines · Michael S. Tsirkin
Quoted 72 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 57 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 17 lines · Alex Williamson
(quoted diff) +17 -0
+	if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+		struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+		if (IS_ERR(irqfd)) {
+			ret = PTR_ERR(irqfd);
+			goto fail;
+		}
+
+		gsi = irqfd->gsi;
+		level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+		source = _irq_source_get(irqfd->source);
+		_irqfd_put_unlock(irqfd);
+		if (!source) {
+			ret = -EINVAL;
+			goto fail;
+		}
+	} else {
+		ret = -EINVAL;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		goto fail;
+	}
+
+	INIT_LIST_HEAD(&eoifd->list);
+	eoifd->kvm = kvm;
+	eoifd->eventfd = eventfd;
+	eoifd->source = source;
+	eoifd->level_irqfd = level_irqfd;
+	eoifd->notifier.gsi = gsi;
+	eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 8 lines
Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

_irqfd too.

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

When the fd is no going away an ioctl is the only other way for it to go away.

It doesn't do any good to fail the ioctl if close(fd) allows it.

allows what? It does nothing.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

Quoted 8 lines
Quoted 3 lines

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

Well eventfd gets an irqfd. What does it mean if said irqfd gets deassigned, and potentially assigned an unrelated interrupt? I think what I would expect is for it to handle the new interrupt. This is hard to implement so let us fail this.

Quoted 5 lines

Ah, so an actual problem, let's solve this. Why wouldn't we just search the list of eoifds and see if this level_irqfd is already used? If we find it and it's compatible, we can get a reference to the _irq_source and "re-attach" the irqfd. If it's not compatible, fail the KVM_IRQFD. If the KVM_IRQFD is for an edge irqfd, I think we let it go.

This is just confusing. Userspace has no idea that you are reusing fds behind the scenes. assign is not the problem, deassign is. So fail *that*.

#42 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 80 lines · Alex Williamson
Quoted 79 lines · Michael S. Tsirkin
Quoted 78 lines · Alex Williamson
Quoted 75 lines · Michael S. Tsirkin
Quoted 68 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 65 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 43 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin
Quoted 27 lines · Alex Williamson

+ if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+ struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+ if (IS_ERR(irqfd)) {
+ ret = PTR_ERR(irqfd);
+ goto fail;
+ }
+
+ gsi = irqfd->gsi;
+ level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+ source = _irq_source_get(irqfd->source);
+ _irqfd_put_unlock(irqfd);
+ if (!source) {
+ ret = -EINVAL;
+ goto fail;
+ }
+ } else {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ INIT_LIST_HEAD(&eoifd->list);
+ eoifd->kvm = kvm;
+ eoifd->eventfd = eventfd;
+ eoifd->source = source;
+ eoifd->level_irqfd = level_irqfd;
+ eoifd->notifier.gsi = gsi;
+ eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd. And since this is the case, can't we drop the reference counting around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Quoted 11 lines

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 4 lines
Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

True. I was wrong. so close has the same bug as deassign. To fix, how about eoifd will hold a reference to the irqfd instead of the eventfd context?

Quoted 38 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

It already keeps
a reference to it so it prevents irqfd from going away by userspace closing the fd.

Wrong, eoifd holds a reference to the eventfd for the irqfd, so it prevents the fd from going away, not the irqfd.

When the fd is no going away an ioctl is the only other way for it to go away.

It doesn't do any good to fail the ioctl if close(fd) allows it.

allows what? It does nothing.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

But it can still be released with deassign.
An easy solution is to fail deassign of irqfd if there is eoifd bound to it.

Quoted 8 lines
Quoted 3 lines

I don't know why we would impose such a bizarre usage model when reference counting on struct _irq_source seems to handle this nicely already.

Well eventfd gets an irqfd. What does it mean if said irqfd gets deassigned, and potentially assigned an unrelated interrupt? I think what I would expect is for it to handle the new interrupt. This is hard to implement so let us fail this.

Quoted 5 lines

Ah, so an actual problem, let's solve this. Why wouldn't we just search the list of eoifds and see if this level_irqfd is already used? If we find it and it's compatible, we can get a reference to the _irq_source and "re-attach" the irqfd. If it's not compatible, fail the KVM_IRQFD. If the KVM_IRQFD is for an edge irqfd, I think we let it go.

This is just confusing. Userspace has no idea that you are reusing fds behind the scenes. assign is not the problem, deassign is. So fail *that*.

#43 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 86 lines · Michael S. Tsirkin
Quoted 85 lines · Alex Williamson
Quoted 83 lines · Michael S. Tsirkin
Quoted 73 lines · Alex Williamson
Quoted 72 lines · Michael S. Tsirkin
Quoted 71 lines · Alex Williamson
Quoted 69 lines · Michael S. Tsirkin
Quoted 57 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson
Quoted 32 lines · Michael S. Tsirkin

On Mon, Jul 16, 2012 at 02:33:55PM -0600, Alex Williamson wrote:
+ if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+ struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+ if (IS_ERR(irqfd)) {
+ ret = PTR_ERR(irqfd);
+ goto fail;
+ }
+
+ gsi = irqfd->gsi;
+ level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+ source = _irq_source_get(irqfd->source);
+ _irqfd_put_unlock(irqfd);
+ if (!source) {
+ ret = -EINVAL;
+ goto fail;
+ }
+ } else {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ INIT_LIST_HEAD(&eoifd->list);
+ eoifd->kvm = kvm;
+ eoifd->eventfd = eventfd;
+ eoifd->source = source;
+ eoifd->level_irqfd = level_irqfd;
+ eoifd->notifier.gsi = gsi;
+ eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd.
And since this is the case, can't we drop the reference counting
around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same thing. What if another module holds a reference to this eventfd? How do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 8 lines
Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

No no. You *already* prevent it. You take a reference to the eventfd context.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

Quoted 7 lines
Quoted 3 lines

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

True. I was wrong. so close has the same bug as deassign. To fix, how about eoifd will hold a reference to the irqfd instead of the eventfd context?

What does it mean to hold a reference to the irqfd? What state of functionality is an irqfd that has been closed/de-assigned but is still attached to an eoifd? It can't continue to fire interrupts into the guest. I don't think close or de-assign have a bug, assign has a bug that it can allow re-assignment using an in-use eventfd. I think I'd rather fix that.

#44 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 91 lines · Alex Williamson
Quoted 90 lines · Michael S. Tsirkin
Quoted 88 lines · Alex Williamson
Quoted 76 lines · Michael S. Tsirkin
Quoted 75 lines · Alex Williamson
Quoted 74 lines · Michael S. Tsirkin
Quoted 72 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 57 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson
Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Alex Williamson

On Tue, 2012-07-17 at 13:21 +0300, Michael S. Tsirkin wrote:
On Mon, Jul 16, 2012 at 02:33:55PM -0600, Alex Williamson wrote:
+ if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+ struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+ if (IS_ERR(irqfd)) {
+ ret = PTR_ERR(irqfd);
+ goto fail;
+ }
+
+ gsi = irqfd->gsi;
+ level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+ source = _irq_source_get(irqfd->source);
+ _irqfd_put_unlock(irqfd);
+ if (!source) {
+ ret = -EINVAL;
+ goto fail;
+ }
+ } else {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ INIT_LIST_HEAD(&eoifd->list);
+ eoifd->kvm = kvm;
+ eoifd->eventfd = eventfd;
+ eoifd->source = source;
+ eoifd->level_irqfd = level_irqfd;
+ eoifd->notifier.gsi = gsi;
+ eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd.
And since this is the case, can't we drop the reference counting
around source ids now? Everything is referenced through irqfd.

Quoted 6 lines
Quoted 3 lines

Holding a reference and using it as a reference count are not the same
thing. What if another module holds a reference to this eventfd? How
do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 8 lines
Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

_irqfd too.

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

Quoted 5 lines
Quoted 3 lines

True. I was wrong. so close has the same bug as deassign. To fix, how about eoifd will hold a reference to the irqfd instead of the eventfd context?

What does it mean to hold a reference to the irqfd?

I meant file *reference: eventfd_fget. But there are other options see below.

Quoted 7 lines

What state of functionality is an irqfd that has been closed/de-assigned but is still attached to an eoifd? It can't continue to fire interrupts into the guest.

I don't think close or de-assign have a bug, assign has a bug that it can allow re-assignment using an in-use eventfd. I think I'd rather fix that.

Let me show you that the bug is in deassign:
assign irqfd for fd=1
assign for eoifd fd=2, irqfd=1
deassign irqfd 1

At this point eoifd has no meaning and there is also no way to deassign it, so the bug already triggered.

I can see two ways out:
1. easy way - fail deassign
2. elegant way - shut down eoifd on irqfd deassign too

I'm fine with both approaches.

Signature · 1 line
MST
#45 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 92 lines · Michael S. Tsirkin
Quoted 91 lines · Alex Williamson
Quoted 90 lines · Michael S. Tsirkin
Quoted 88 lines · Alex Williamson
Quoted 76 lines · Michael S. Tsirkin
Quoted 75 lines · Alex Williamson
Quoted 74 lines · Michael S. Tsirkin
Quoted 72 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 57 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson
Quoted 41 lines · Michael S. Tsirkin

On Tue, Jul 17, 2012 at 07:59:16AM -0600, Alex Williamson wrote:
On Tue, 2012-07-17 at 13:21 +0300, Michael S. Tsirkin wrote:
On Mon, Jul 16, 2012 at 02:33:55PM -0600, Alex Williamson wrote:
+ if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+ struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+ if (IS_ERR(irqfd)) {
+ ret = PTR_ERR(irqfd);
+ goto fail;
+ }
+
+ gsi = irqfd->gsi;
+ level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+ source = _irq_source_get(irqfd->source);
+ _irqfd_put_unlock(irqfd);
+ if (!source) {
+ ret = -EINVAL;
+ goto fail;
+ }
+ } else {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ INIT_LIST_HEAD(&eoifd->list);
+ eoifd->kvm = kvm;
+ eoifd->eventfd = eventfd;
+ eoifd->source = source;
+ eoifd->level_irqfd = level_irqfd;
+ eoifd->notifier.gsi = gsi;
+ eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd.
And since this is the case, can't we drop the reference counting
around source ids now? Everything is referenced through irqfd.

Holding a reference and using it as a reference count are not the same
thing. What if another module holds a reference to this eventfd? How
do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't see it. We have an irq source id that needs to be allocated by irqfd and returned when it's unused. It becomes unused when neither irqfd nor eoifd are making use of it. irqfd and eoifd may be closed in any order. Use of the source id is what we're reference counting, which is why it's in struct _irq_source. How can I use an eventfd reference for the same? I don't know when it's unused. I don't know who else holds a reference to it... Doesn't make sense to me. Feels like attempting to squat on someone else's object.

Quoted 8 lines
Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

_irqfd too.

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

True. I was wrong. so close has the same bug as deassign. To fix, how about eoifd will hold a reference to the irqfd instead of the eventfd context?

What does it mean to hold a reference to the irqfd?

I meant file *reference: eventfd_fget. But there are other options see below.

That's no better than the eventfd context we already hold.

Quoted 8 lines
Quoted 7 lines

What state of functionality is an irqfd that has been closed/de-assigned but is still attached to an eoifd? It can't continue to fire interrupts into the guest.

I don't think close or de-assign have a bug, assign has a bug that it can allow re-assignment using an in-use eventfd. I think I'd rather fix that.

Quoted 7 lines

Let me show you that the bug is in deassign:
assign irqfd for fd=1
assign for eoifd fd=2, irqfd=1
deassign irqfd 1

At this point eoifd has no meaning and there is also no way to deassign it,

Yes, there is. This is exactly why I hold a reference to the eventfd ctx. It can still be deassigned by passing irqfd=1, we'll do an eventfd_ctx_get and match it to that stored.

Quoted 4 lines

so the bug already triggered.

I can see two ways out:
1. easy way - fail deassign

Then close() and deassign are not the same.

Quoted 1 line

2. elegant way - shut down eoifd on irqfd deassign too

Sorry, I've always been told it's a bad idea to have one interface kill another from inside the kernel.

Given that your assertion above is incorrect, I still stand by fixing assign.

#46 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 97 lines · Alex Williamson
Quoted 96 lines · Michael S. Tsirkin
Quoted 95 lines · Alex Williamson
Quoted 93 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 79 lines · Michael S. Tsirkin
Quoted 78 lines · Alex Williamson
Quoted 75 lines · Michael S. Tsirkin
Quoted 68 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 65 lines · Alex Williamson
Quoted 56 lines · Michael S. Tsirkin
Quoted 54 lines · Alex Williamson

On Tue, 2012-07-17 at 17:10 +0300, Michael S. Tsirkin wrote:
On Tue, Jul 17, 2012 at 07:59:16AM -0600, Alex Williamson wrote:
On Tue, 2012-07-17 at 13:21 +0300, Michael S. Tsirkin wrote:
On Mon, Jul 16, 2012 at 02:33:55PM -0600, Alex Williamson wrote:
+ if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+ struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+ if (IS_ERR(irqfd)) {
+ ret = PTR_ERR(irqfd);
+ goto fail;
+ }
+
+ gsi = irqfd->gsi;
+ level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+ source = _irq_source_get(irqfd->source);
+ _irqfd_put_unlock(irqfd);
+ if (!source) {
+ ret = -EINVAL;
+ goto fail;
+ }
+ } else {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ INIT_LIST_HEAD(&eoifd->list);
+ eoifd->kvm = kvm;
+ eoifd->eventfd = eventfd;
+ eoifd->source = source;
+ eoifd->level_irqfd = level_irqfd;
+ eoifd->notifier.gsi = gsi;
+ eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd.
And since this is the case, can't we drop the reference counting
around source ids now? Everything is referenced through irqfd.

Holding a reference and using it as a reference count are not the same
thing. What if another module holds a reference to this eventfd? How
do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't
see it. We have an irq source id that needs to be allocated by irqfd
and returned when it's unused. It becomes unused when neither irqfd nor
eoifd are making use of it. irqfd and eoifd may be closed in any order.
Use of the source id is what we're reference counting, which is why it's
in struct _irq_source. How can I use an eventfd reference for the same?
I don't know when it's unused. I don't know who else holds a reference
to it... Doesn't make sense to me. Feels like attempting to squat on
someone else's object.

Quoted 1 line

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 4 lines
Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

True. I was wrong. so close has the same bug as deassign. To fix, how about eoifd will hold a reference to the irqfd instead of the eventfd context?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

What does it mean to hold a reference to the irqfd?

I meant file *reference: eventfd_fget. But there are other options see below.

That's no better than the eventfd context we already hold.

It means POLLHUP is not invoked until eoifd is closed.

Quoted 13 lines
Quoted 13 lines
Quoted 7 lines

What state of functionality is an irqfd that has been closed/de-assigned but is still attached to an eoifd? It can't continue to fire interrupts into the guest.

I don't think close or de-assign have a bug, assign has a bug that it can allow re-assignment using an in-use eventfd. I think I'd rather fix that.

Let me show you that the bug is in deassign:
assign irqfd for fd=1
assign for eoifd fd=2, irqfd=1
deassign irqfd 1

Quoted 6 lines
Quoted 2 lines

At this point eoifd has no meaning and there is also no way to deassign it,

Yes, there is. This is exactly why I hold a reference to the eventfd ctx. It can still be deassigned by passing irqfd=1, we'll do an eventfd_ctx_get and match it to that stored.

OK.
What if instead we close irqfd 1?

Quoted 11 lines
Quoted 4 lines

so the bug already triggered.

I can see two ways out:
1. easy way - fail deassign

Then close() and deassign are not the same.

Quoted 1 line

2. elegant way - shut down eoifd on irqfd deassign too

Sorry, I've always been told it's a bad idea to have one interface kill another from inside the kernel.

Not kill merely deassign.

Quoted 2 lines

Given that your assertion above is incorrect, I still stand by fixing assign.

OK, but then you also would need to protect against someone binding an irqfd that is not level to same GSI.

Also if we go ahead with fixing assign - I do not think we need to rebind to the same source id - just failing assign of this irqfd with EBUSY should be enough.

Signature · 1 line
MST
#47 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 123 lines · Michael S. Tsirkin
Quoted 97 lines · Alex Williamson
Quoted 96 lines · Michael S. Tsirkin
Quoted 95 lines · Alex Williamson
Quoted 93 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 79 lines · Michael S. Tsirkin
Quoted 78 lines · Alex Williamson
Quoted 75 lines · Michael S. Tsirkin
Quoted 68 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 65 lines · Alex Williamson
Quoted 57 lines · Michael S. Tsirkin

On Tue, Jul 17, 2012 at 08:29:43AM -0600, Alex Williamson wrote:
On Tue, 2012-07-17 at 17:10 +0300, Michael S. Tsirkin wrote:
On Tue, Jul 17, 2012 at 07:59:16AM -0600, Alex Williamson wrote:
On Tue, 2012-07-17 at 13:21 +0300, Michael S. Tsirkin wrote:
On Mon, Jul 16, 2012 at 02:33:55PM -0600, Alex Williamson wrote:
+ if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+ struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+ if (IS_ERR(irqfd)) {
+ ret = PTR_ERR(irqfd);
+ goto fail;
+ }
+
+ gsi = irqfd->gsi;
+ level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+ source = _irq_source_get(irqfd->source);
+ _irqfd_put_unlock(irqfd);
+ if (!source) {
+ ret = -EINVAL;
+ goto fail;
+ }
+ } else {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ INIT_LIST_HEAD(&eoifd->list);
+ eoifd->kvm = kvm;
+ eoifd->eventfd = eventfd;
+ eoifd->source = source;
+ eoifd->level_irqfd = level_irqfd;
+ eoifd->notifier.gsi = gsi;
+ eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd.
And since this is the case, can't we drop the reference counting
around source ids now? Everything is referenced through irqfd.

Holding a reference and using it as a reference count are not the same
thing. What if another module holds a reference to this eventfd? How
do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't
see it. We have an irq source id that needs to be allocated by irqfd
and returned when it's unused. It becomes unused when neither irqfd nor
eoifd are making use of it. irqfd and eoifd may be closed in any order.
Use of the source id is what we're reference counting, which is why it's
in struct _irq_source. How can I use an eventfd reference for the same?
I don't know when it's unused. I don't know who else holds a reference
to it... Doesn't make sense to me. Feels like attempting to squat on
someone else's object.

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a release, nobody checks close(3p) return. Blocking a release is likely to cause all sorts of problems, so what you mean is that irqfd should linger around until there are no references to it... but that's exactly what struct _irq_source is for, is to hold the bits that we care about references to and automatically release it when there are none.

Quoted 4 lines
Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

True. I was wrong. so close has the same bug as deassign. To fix, how about eoifd will hold a reference to the irqfd instead of the eventfd context?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

What does it mean to hold a reference to the irqfd?

I meant file *reference: eventfd_fget. But there are other options see below.

That's no better than the eventfd context we already hold.

It means POLLHUP is not invoked until eoifd is closed.

Quoted 16 lines
Quoted 15 lines
Quoted 7 lines

What state of functionality is an irqfd that has been closed/de-assigned but is still attached to an eoifd? It can't continue to fire interrupts into the guest.

I don't think close or de-assign have a bug, assign has a bug that it can allow re-assignment using an in-use eventfd. I think I'd rather fix that.

Let me show you that the bug is in deassign:
assign irqfd for fd=1
assign for eoifd fd=2, irqfd=1
deassign irqfd 1

At this point eoifd has no meaning and there is also no way to deassign it,

Quoted 6 lines
Quoted 3 lines

Yes, there is. This is exactly why I hold a reference to the eventfd ctx. It can still be deassigned by passing irqfd=1, we'll do an eventfd_ctx_get and match it to that stored.

OK.
What if instead we close irqfd 1?

Then the user isn't reading directions very well because the API clearly indicates to pass the irqfd on both assign and de-assign of the eoifd. However, it will still get de-assigned if they close the eoifd.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

so the bug already triggered.

I can see two ways out:
1. easy way - fail deassign

Quoted 8 lines
Quoted 6 lines

Then close() and deassign are not the same.

Quoted 1 line

2. elegant way - shut down eoifd on irqfd deassign too

Sorry, I've always been told it's a bad idea to have one interface kill another from inside the kernel.

Not kill merely deassign.

That's what I mean. Unintended consequences should not be designed in.

Quoted 10 lines
Quoted 2 lines

Given that your assertion above is incorrect, I still stand by fixing assign.

OK, but then you also would need to protect against someone binding an irqfd that is not level to same GSI.

Also if we go ahead with fixing assign - I do not think we need to rebind to the same source id - just failing assign of this irqfd with EBUSY should be enough.

#48 lore raw

Re: [PATCH v5 2/4] kvm: KVM_EOIFD, an eventfd for EOIs

Quoted 128 lines · Alex Williamson
Quoted 127 lines · Michael S. Tsirkin
Quoted 97 lines · Alex Williamson
Quoted 96 lines · Michael S. Tsirkin
Quoted 95 lines · Alex Williamson
Quoted 93 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 79 lines · Michael S. Tsirkin
Quoted 78 lines · Alex Williamson
Quoted 75 lines · Michael S. Tsirkin
Quoted 68 lines · Alex Williamson
Quoted 67 lines · Michael S. Tsirkin
Quoted 65 lines · Alex Williamson

On Tue, 2012-07-17 at 17:42 +0300, Michael S. Tsirkin wrote:
On Tue, Jul 17, 2012 at 08:29:43AM -0600, Alex Williamson wrote:
On Tue, 2012-07-17 at 17:10 +0300, Michael S. Tsirkin wrote:
On Tue, Jul 17, 2012 at 07:59:16AM -0600, Alex Williamson wrote:
On Tue, 2012-07-17 at 13:21 +0300, Michael S. Tsirkin wrote:
On Mon, Jul 16, 2012 at 02:33:55PM -0600, Alex Williamson wrote:
+ if (args->flags & KVM_EOIFD_FLAG_LEVEL_IRQFD) {
+ struct _irqfd *irqfd = _irqfd_fdget_lock(kvm, args->irqfd);
+ if (IS_ERR(irqfd)) {
+ ret = PTR_ERR(irqfd);
+ goto fail;
+ }
+
+ gsi = irqfd->gsi;
+ level_irqfd = eventfd_ctx_get(irqfd->eventfd);
+ source = _irq_source_get(irqfd->source);
+ _irqfd_put_unlock(irqfd);
+ if (!source) {
+ ret = -EINVAL;
+ goto fail;
+ }
+ } else {
+ ret = -EINVAL;
+ goto fail;
+ }
+
+ INIT_LIST_HEAD(&eoifd->list);
+ eoifd->kvm = kvm;
+ eoifd->eventfd = eventfd;
+ eoifd->source = source;
+ eoifd->level_irqfd = level_irqfd;
+ eoifd->notifier.gsi = gsi;
+ eoifd->notifier.irq_acked = eoifd_event;

OK so this means eoifd keeps a reference to the irqfd.
And since this is the case, can't we drop the reference counting
around source ids now? Everything is referenced through irqfd.

Holding a reference and using it as a reference count are not the same
thing. What if another module holds a reference to this eventfd? How
do we do anything on release?

We don't as there is no release, and using kref on source id does not
help: it just never gets invoked.

Please work out how you think it should work and let me know, I don't
see it. We have an irq source id that needs to be allocated by irqfd
and returned when it's unused. It becomes unused when neither irqfd nor
eoifd are making use of it. irqfd and eoifd may be closed in any order.
Use of the source id is what we're reference counting, which is why it's
in struct _irq_source. How can I use an eventfd reference for the same?
I don't know when it's unused. I don't know who else holds a reference
to it... Doesn't make sense to me. Feels like attempting to squat on
someone else's object.

eoifd should prevent irqfd from being released.

Why? Note that this is actually quite difficult too. We can't fail a
release, nobody checks close(3p) return. Blocking a release is likely
to cause all sorts of problems, so what you mean is that irqfd should
linger around until there are no references to it... but that's exactly
what struct _irq_source is for, is to hold the bits that we care about
references to and automatically release it when there are none.

Quoted 4 lines
Quoted 2 lines

No no. You *already* prevent it. You take a reference to the eventfd context.

Right, which keeps the fd from going away, not the struct _irqfd.

_irqfd too.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

How so?

Normally irqfd_wakeup is called with POLLHUP and calls irqfd_deactivate. If you get a ctx reference this does not happen.

I think you're mistaken. wake_up_poll(,POLLHUP) is called from eventfd_release (file_operations.release), not from ctx reference release.

True. I was wrong. so close has the same bug as deassign. To fix, how about eoifd will hold a reference to the irqfd instead of the eventfd context?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

What does it mean to hold a reference to the irqfd?

I meant file *reference: eventfd_fget. But there are other options see below.

That's no better than the eventfd context we already hold.

It means POLLHUP is not invoked until eoifd is closed.

Quoted 19 lines
Quoted 8 lines
Quoted 7 lines

What state of functionality is an irqfd that has been closed/de-assigned but is still attached to an eoifd? It can't continue to fire interrupts into the guest.

I don't think close or de-assign have a bug, assign has a bug that it can allow re-assignment using an in-use eventfd. I think I'd rather fix that.

Quoted 7 lines

Let me show you that the bug is in deassign:
assign irqfd for fd=1
assign for eoifd fd=2, irqfd=1
deassign irqfd 1

At this point eoifd has no meaning and there is also no way to deassign it,

Yes, there is. This is exactly why I hold a reference to the eventfd ctx. It can still be deassigned by passing irqfd=1, we'll do an eventfd_ctx_get and match it to that stored.

Quoted 6 lines
Quoted 2 lines

OK.
What if instead we close irqfd 1?

Then the user isn't reading directions very well because the API clearly indicates to pass the irqfd on both assign and de-assign of the eoifd. However, it will still get de-assigned if they close the eoifd.

Well you are hanging on the source id, this is an undocumented side effect, so the following can fail:

assign irqfd
assign eoifd
deassign irqfd
assign irqfd2
close eoifd

Simply source id should stay alive only while irqfd is around. Instead of hanging on to it from eoifd with reference counting, you should simply deactivate eoifd when irqfd goes away.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

so the bug already triggered.

I can see two ways out:
1. easy way - fail deassign

Then close() and deassign are not the same.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

2. elegant way - shut down eoifd on irqfd deassign too

Sorry, I've always been told it's a bad idea to have one interface kill another from inside the kernel.

Not kill merely deassign.

That's what I mean. Unintended consequences should not be designed in.

But source id is an internal to kvm, users do not know about it.

So what is unintended here? You bind eoifd to irqfd. This means give me indication of eoi when I send this interrupt. Now you deassign or close irqfd. You will not get any more eoi indications. All that is needed is fixing a bug: eoi still hangs on to source id so attempts to create new level irqfd

Quoted 11 lines
Quoted 10 lines
Quoted 2 lines

Given that your assertion above is incorrect, I still stand by fixing assign.

OK, but then you also would need to protect against someone binding an irqfd that is not level to same GSI.

Also if we go ahead with fixing assign - I do not think we need to rebind to the same source id - just failing assign of this irqfd with EBUSY should be enough.

#49 lore raw

PATCH v5 3/4 kvm: Create kvm_clear_irq()

In reply to Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +78 -0

106 lines not on this page · raw message

#50 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 49 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +10 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 90

Quoted 10 lines
virt/kvm/irq_comm.c +10 -0
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;
+#else
+	return -1;
+#endif

What does this ifdef exclude exactly?

Quoted 85 lines
virt/kvm/irq_comm.c +58 -0
+}
+
+static int kvm_clear_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
+				struct kvm *kvm, int irq_source_id)
+{
+	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	int level;
+
+	level = kvm_clear_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+					 irq_source_id);
+	if (level)
+		kvm_ioapic_set_irq(ioapic, e->irqchip.pin,
+				   !!ioapic->irq_states[e->irqchip.pin]);
+	return level;
+}
+
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
 #ifdef CONFIG_IA64
@@ -190,6 +226,45 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 	return ret;
 }
 
+/*
+ * Return value:
+ *  < 0   Error
+ *  = 0   Interrupt was not set, did nothing
+ *  > 0   Interrupt was pending, cleared
+ */
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq)
+{
+	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
+	int ret = -EINVAL, i = 0;
+	struct kvm_irq_routing_table *irq_rt;
+	struct hlist_node *n;
+
+	/* Not possible to detect if the guest uses the PIC or the
+	 * IOAPIC.  So clear the bit in both. The guest will ignore
+	 * writes to the unused one.
+	 */
+	rcu_read_lock();
+	irq_rt = rcu_dereference(kvm->irq_routing);
+	if (irq < irq_rt->nr_rt_entries)
+		hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+			irq_set[i++] = *e;
+	rcu_read_unlock();
+
+	while (i--) {
+		int r = -EINVAL;
+
+		if (irq_set[i].clear)
+			r = irq_set[i].clear(&irq_set[i], kvm, irq_source_id);
+
+		if (r < 0)
+			continue;
+
+		ret = r + ((ret < 0) ? 0 : ret);
+	}
+
+	return ret;
+}
+
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 {
 	struct kvm_irq_ack_notifier *kian;
@@ -344,16 +419,19 @@ static int setup_routing_entry(struct kvm_irq_routing_table *rt,
 		switch (ue->u.irqchip.irqchip) {
 		case KVM_IRQCHIP_PIC_MASTER:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			break;
 		case KVM_IRQCHIP_PIC_SLAVE:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			delta = 8;
 			break;
 		case KVM_IRQCHIP_IOAPIC:
 			max_pin = KVM_IOAPIC_NUM_PINS;
 			e->set = kvm_set_ioapic_irq;
+			e->clear = kvm_clear_ioapic_irq;
 			break;
 		default:
 			goto out;
#51 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 54 lines · Michael S. Tsirkin
Quoted 53 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +14 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
+					     irq_source_id);
+	if (level)
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 90

Quoted 6 lines
virt/kvm/irq_comm.c +6 -0
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;
+#else
+	return -1;
+#endif

What does this ifdef exclude exactly?

No pic on ia64. Not that it works, but I figured the consistency with kvm_set_pic_irq would make more sense whenever someone goes through and cleans out the code. Thanks,

Alex

Quoted 85 lines
Quoted 85 lines
virt/kvm/irq_comm.c +58 -0
+}
+
+static int kvm_clear_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
+				struct kvm *kvm, int irq_source_id)
+{
+	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	int level;
+
+	level = kvm_clear_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+					 irq_source_id);
+	if (level)
+		kvm_ioapic_set_irq(ioapic, e->irqchip.pin,
+				   !!ioapic->irq_states[e->irqchip.pin]);
+	return level;
+}
+
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
 #ifdef CONFIG_IA64
@@ -190,6 +226,45 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 	return ret;
 }
 
+/*
+ * Return value:
+ *  < 0   Error
+ *  = 0   Interrupt was not set, did nothing
+ *  > 0   Interrupt was pending, cleared
+ */
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq)
+{
+	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
+	int ret = -EINVAL, i = 0;
+	struct kvm_irq_routing_table *irq_rt;
+	struct hlist_node *n;
+
+	/* Not possible to detect if the guest uses the PIC or the
+	 * IOAPIC.  So clear the bit in both. The guest will ignore
+	 * writes to the unused one.
+	 */
+	rcu_read_lock();
+	irq_rt = rcu_dereference(kvm->irq_routing);
+	if (irq < irq_rt->nr_rt_entries)
+		hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+			irq_set[i++] = *e;
+	rcu_read_unlock();
+
+	while (i--) {
+		int r = -EINVAL;
+
+		if (irq_set[i].clear)
+			r = irq_set[i].clear(&irq_set[i], kvm, irq_source_id);
+
+		if (r < 0)
+			continue;
+
+		ret = r + ((ret < 0) ? 0 : ret);
+	}
+
+	return ret;
+}
+
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 {
 	struct kvm_irq_ack_notifier *kian;
@@ -344,16 +419,19 @@ static int setup_routing_entry(struct kvm_irq_routing_table *rt,
 		switch (ue->u.irqchip.irqchip) {
 		case KVM_IRQCHIP_PIC_MASTER:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			break;
 		case KVM_IRQCHIP_PIC_SLAVE:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			delta = 8;
 			break;
 		case KVM_IRQCHIP_IOAPIC:
 			max_pin = KVM_IOAPIC_NUM_PINS;
 			e->set = kvm_set_ioapic_irq;
+			e->clear = kvm_clear_ioapic_irq;
 			break;
 		default:
 			goto out;
#52 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

In reply to Alex Williamson
Quoted 45 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +6 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 86

Quoted 10 lines
virt/kvm/irq_comm.c +10 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);

This is a bit tricky: add a comment explaining the logic?

Quoted 7 lines
virt/kvm/irq_comm.c +7 -0
+	return level;
+#else
+	return -1;
+#endif
+}
+
+static int kvm_clear_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 103

Quoted 10 lines
virt/kvm/irq_comm.c +10 -0
+				struct kvm *kvm, int irq_source_id)
+{
+	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	int level;
+
+	level = kvm_clear_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+					 irq_source_id);
+	if (level)
+		kvm_ioapic_set_irq(ioapic, e->irqchip.pin,
+				   !!ioapic->irq_states[e->irqchip.pin]);

This is a bit tricky: add a comment explaining the logic?

Quoted 72 lines
virt/kvm/irq_comm.c +45 -0
+	return level;
+}
+
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
 #ifdef CONFIG_IA64
@@ -190,6 +226,45 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 	return ret;
 }
 
+/*
+ * Return value:
+ *  < 0   Error
+ *  = 0   Interrupt was not set, did nothing
+ *  > 0   Interrupt was pending, cleared
+ */
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq)
+{
+	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
+	int ret = -EINVAL, i = 0;
+	struct kvm_irq_routing_table *irq_rt;
+	struct hlist_node *n;
+
+	/* Not possible to detect if the guest uses the PIC or the
+	 * IOAPIC.  So clear the bit in both. The guest will ignore
+	 * writes to the unused one.
+	 */
+	rcu_read_lock();
+	irq_rt = rcu_dereference(kvm->irq_routing);
+	if (irq < irq_rt->nr_rt_entries)
+		hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+			irq_set[i++] = *e;
+	rcu_read_unlock();
+
+	while (i--) {
+		int r = -EINVAL;
+
+		if (irq_set[i].clear)
+			r = irq_set[i].clear(&irq_set[i], kvm, irq_source_id);
+
+		if (r < 0)
+			continue;
+
+		ret = r + ((ret < 0) ? 0 : ret);
+	}
+
+	return ret;
+}
+
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 {
 	struct kvm_irq_ack_notifier *kian;
@@ -344,16 +419,19 @@ static int setup_routing_entry(struct kvm_irq_routing_table *rt,
 		switch (ue->u.irqchip.irqchip) {
 		case KVM_IRQCHIP_PIC_MASTER:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			break;
 		case KVM_IRQCHIP_PIC_SLAVE:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			delta = 8;
 			break;
 		case KVM_IRQCHIP_IOAPIC:
 			max_pin = KVM_IOAPIC_NUM_PINS;
 			e->set = kvm_set_ioapic_irq;
+			e->clear = kvm_clear_ioapic_irq;
 			break;
 		default:
 			goto out;
#53 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

In reply to Alex Williamson
Quoted 46 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +7 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 10 lines
virt/kvm/irq_comm.c +10 -0
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed? I think it's worthwhile to rename level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

Quoted 88 lines
virt/kvm/irq_comm.c +61 -0
+#else
+	return -1;
+#endif
+}
+
+static int kvm_clear_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
+				struct kvm *kvm, int irq_source_id)
+{
+	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	int level;
+
+	level = kvm_clear_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+					 irq_source_id);
+	if (level)
+		kvm_ioapic_set_irq(ioapic, e->irqchip.pin,
+				   !!ioapic->irq_states[e->irqchip.pin]);
+	return level;
+}
+
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
 #ifdef CONFIG_IA64
@@ -190,6 +226,45 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 	return ret;
 }
 
+/*
+ * Return value:
+ *  < 0   Error
+ *  = 0   Interrupt was not set, did nothing
+ *  > 0   Interrupt was pending, cleared
+ */
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq)
+{
+	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
+	int ret = -EINVAL, i = 0;
+	struct kvm_irq_routing_table *irq_rt;
+	struct hlist_node *n;
+
+	/* Not possible to detect if the guest uses the PIC or the
+	 * IOAPIC.  So clear the bit in both. The guest will ignore
+	 * writes to the unused one.
+	 */
+	rcu_read_lock();
+	irq_rt = rcu_dereference(kvm->irq_routing);
+	if (irq < irq_rt->nr_rt_entries)
+		hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+			irq_set[i++] = *e;
+	rcu_read_unlock();
+
+	while (i--) {
+		int r = -EINVAL;
+
+		if (irq_set[i].clear)
+			r = irq_set[i].clear(&irq_set[i], kvm, irq_source_id);
+
+		if (r < 0)
+			continue;
+
+		ret = r + ((ret < 0) ? 0 : ret);
+	}
+
+	return ret;
+}
+
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 {
 	struct kvm_irq_ack_notifier *kian;
@@ -344,16 +419,19 @@ static int setup_routing_entry(struct kvm_irq_routing_table *rt,
 		switch (ue->u.irqchip.irqchip) {
 		case KVM_IRQCHIP_PIC_MASTER:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			break;
 		case KVM_IRQCHIP_PIC_SLAVE:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			delta = 8;
 			break;
 		case KVM_IRQCHIP_IOAPIC:
 			max_pin = KVM_IOAPIC_NUM_PINS;
 			e->set = kvm_set_ioapic_irq;
+			e->clear = kvm_clear_ioapic_irq;
 			break;
 		default:
 			goto out;
#54 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 88 lines
Quoted 88 lines
virt/kvm/irq_comm.c +61 -0
+#else
+	return -1;
+#endif
+}
+
+static int kvm_clear_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
+				struct kvm *kvm, int irq_source_id)
+{
+	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	int level;
+
+	level = kvm_clear_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+					 irq_source_id);
+	if (level)
+		kvm_ioapic_set_irq(ioapic, e->irqchip.pin,
+				   !!ioapic->irq_states[e->irqchip.pin]);
+	return level;
+}
+
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
 #ifdef CONFIG_IA64
@@ -190,6 +226,45 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 	return ret;
 }
 
+/*
+ * Return value:
+ *  < 0   Error
+ *  = 0   Interrupt was not set, did nothing
+ *  > 0   Interrupt was pending, cleared
+ */
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq)
+{
+	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
+	int ret = -EINVAL, i = 0;
+	struct kvm_irq_routing_table *irq_rt;
+	struct hlist_node *n;
+
+	/* Not possible to detect if the guest uses the PIC or the
+	 * IOAPIC.  So clear the bit in both. The guest will ignore
+	 * writes to the unused one.
+	 */
+	rcu_read_lock();
+	irq_rt = rcu_dereference(kvm->irq_routing);
+	if (irq < irq_rt->nr_rt_entries)
+		hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+			irq_set[i++] = *e;
+	rcu_read_unlock();
+
+	while (i--) {
+		int r = -EINVAL;
+
+		if (irq_set[i].clear)
+			r = irq_set[i].clear(&irq_set[i], kvm, irq_source_id);
+
+		if (r < 0)
+			continue;
+
+		ret = r + ((ret < 0) ? 0 : ret);
+	}
+
+	return ret;
+}
+
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 {
 	struct kvm_irq_ack_notifier *kian;
@@ -344,16 +419,19 @@ static int setup_routing_entry(struct kvm_irq_routing_table *rt,
 		switch (ue->u.irqchip.irqchip) {
 		case KVM_IRQCHIP_PIC_MASTER:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			break;
 		case KVM_IRQCHIP_PIC_SLAVE:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			delta = 8;
 			break;
 		case KVM_IRQCHIP_IOAPIC:
 			max_pin = KVM_IOAPIC_NUM_PINS;
 			e->set = kvm_set_ioapic_irq;
+			e->clear = kvm_clear_ioapic_irq;
 			break;
 		default:
 			goto out;
#55 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 74 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

Quoted 6 lines

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);

above doesn't

}

why the insonsistency?

Quoted 90 lines
Quoted 88 lines
Quoted 88 lines
virt/kvm/irq_comm.c +61 -0
+#else
+	return -1;
+#endif
+}
+
+static int kvm_clear_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
+				struct kvm *kvm, int irq_source_id)
+{
+	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	int level;
+
+	level = kvm_clear_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+					 irq_source_id);
+	if (level)
+		kvm_ioapic_set_irq(ioapic, e->irqchip.pin,
+				   !!ioapic->irq_states[e->irqchip.pin]);
+	return level;
+}
+
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
 #ifdef CONFIG_IA64
@@ -190,6 +226,45 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 	return ret;
 }
 
+/*
+ * Return value:
+ *  < 0   Error
+ *  = 0   Interrupt was not set, did nothing
+ *  > 0   Interrupt was pending, cleared
+ */
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq)
+{
+	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
+	int ret = -EINVAL, i = 0;
+	struct kvm_irq_routing_table *irq_rt;
+	struct hlist_node *n;
+
+	/* Not possible to detect if the guest uses the PIC or the
+	 * IOAPIC.  So clear the bit in both. The guest will ignore
+	 * writes to the unused one.
+	 */
+	rcu_read_lock();
+	irq_rt = rcu_dereference(kvm->irq_routing);
+	if (irq < irq_rt->nr_rt_entries)
+		hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+			irq_set[i++] = *e;
+	rcu_read_unlock();
+
+	while (i--) {
+		int r = -EINVAL;
+
+		if (irq_set[i].clear)
+			r = irq_set[i].clear(&irq_set[i], kvm, irq_source_id);
+
+		if (r < 0)
+			continue;
+
+		ret = r + ((ret < 0) ? 0 : ret);
+	}
+
+	return ret;
+}
+
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 {
 	struct kvm_irq_ack_notifier *kian;
@@ -344,16 +419,19 @@ static int setup_routing_entry(struct kvm_irq_routing_table *rt,
 		switch (ue->u.irqchip.irqchip) {
 		case KVM_IRQCHIP_PIC_MASTER:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			break;
 		case KVM_IRQCHIP_PIC_SLAVE:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			delta = 8;
 			break;
 		case KVM_IRQCHIP_IOAPIC:
 			max_pin = KVM_IOAPIC_NUM_PINS;
 			e->set = kvm_set_ioapic_irq;
+			e->clear = kvm_clear_ioapic_irq;
 			break;
 		default:
 			goto out;
#56 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 82 lines · Michael S. Tsirkin
Quoted 74 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

Quoted 6 lines

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions, but atomic instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

#57 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex. And current code looks buggy if yes we need to fix it somehow.

Signature · 1 line
MST
#58 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);

Locking doesn't get much more straightforward than that

Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

#59 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Signature · 1 line
MST
#60 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 173 lines · Michael S. Tsirkin
Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 6 lines
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

#61 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 176 lines · Alex Williamson
Quoted 175 lines · Michael S. Tsirkin
Quoted 173 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);

Locking doesn't get much more straightforward than that

Quoted 7 lines
Quoted 4 lines

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Only for MSI. That's an exception (and it's also a messy one).

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

Signature · 1 line
MST
#62 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 182 lines · Michael S. Tsirkin
Quoted 181 lines · Alex Williamson
Quoted 179 lines · Michael S. Tsirkin
Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Quoted 5 lines
Quoted 2 lines

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Only for MSI. That's an exception (and it's also a messy one).

ioapic/pic used to use mutexes for locking. But this is not longer the case. See 46a47b1e for instance. I wasn't able to find the reason for the commit.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

Quoted 11 lines
Quoted 5 lines
Quoted 2 lines

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

Signature · 1 line
MST

--
Gleb.

#63 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

In reply to Michael S. Tsirkin
Quoted 182 lines · Michael S. Tsirkin
Quoted 181 lines · Alex Williamson
Quoted 179 lines · Michael S. Tsirkin
Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Quoted 4 lines
Quoted 2 lines

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Only for MSI. That's an exception (and it's also a messy one).

Nope, I see past code that used it for INTx as well.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

#64 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 199 lines · Alex Williamson
Quoted 182 lines · Michael S. Tsirkin
Quoted 181 lines · Alex Williamson
Quoted 179 lines · Michael S. Tsirkin
Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Quoted 4 lines
Quoted 2 lines

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Only for MSI. That's an exception (and it's also a messy one).

Nope, I see past code that used it for INTx as well.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

Signature · 1 line
MST
#65 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 228 lines · Michael S. Tsirkin
Quoted 199 lines · Alex Williamson
Quoted 182 lines · Michael S. Tsirkin
Quoted 181 lines · Alex Williamson
Quoted 179 lines · Michael S. Tsirkin
Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Quoted 4 lines
Quoted 2 lines

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Only for MSI. That's an exception (and it's also a messy one).

Nope, I see past code that used it for INTx as well.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

Quoted 8 lines

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

#66 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+ struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+ struct kvm_pic *pic = pic_irqchip(kvm);
+ int level =
kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
+ irq_source_id);
+ if (level)
+ kvm_pic_set_irq(pic, e->irqchip.pin,
+ !!pic->irq_states[e->irqchip.pin]);
+ return level;
+#else
+ return -1;
+#endif
+}
+

it seems racy in the same way.

Signature · 1 line
MST
#67 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress. Those patches are just users of kvm_set_irq.

#68 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 80 lines · Alex Williamson
Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress.

I'm asking for bugs to get fixed and not duplicated. Adding more bugs is not progress. Or maybe there is no bug. Let's see why and add a comment.

Quoted 1 line

Those patches are just users of kvm_set_irq.

Well these add calls to kvm_set_irq which scans all vcpus under spinlock. In the past Avi thought this is not a good idea too. Maybe things changed.

Signature · 1 line
MST
#69 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 93 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress.

I'm asking for bugs to get fixed and not duplicated. Adding more bugs is not progress. Or maybe there is no bug. Let's see why and add a comment.

Quoted 6 lines
Quoted 1 line

Those patches are just users of kvm_set_irq.

Well these add calls to kvm_set_irq which scans all vcpus under spinlock. In the past Avi thought this is not a good idea too. Maybe things changed.

We can drop the spinlock if we don't care about spurious EOIs, which is only a theoretical scalability problem anyway. We're talking about level interrupts here, how scalable do we need to be?

#70 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 97 lines · Alex Williamson
Quoted 96 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress.

I'm asking for bugs to get fixed and not duplicated. Adding more bugs is not progress. Or maybe there is no bug. Let's see why and add a comment.

Quoted 1 line

Those patches are just users of kvm_set_irq.

Quoted 6 lines
Quoted 3 lines

Well these add calls to kvm_set_irq which scans all vcpus under spinlock. In the past Avi thought this is not a good idea too. Maybe things changed.

We can drop the spinlock if we don't care about spurious EOIs, which is only a theoretical scalability problem anyway.

Not theoretical at all IMO. We see the problem with virtio with old guests today.

Quoted 3 lines

We're talking about
level interrupts here, how scalable do we need to be?

The reason we are moving them into kernel at all is for speed, no?

Signature · 1 line
MST
#71 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 102 lines · Michael S. Tsirkin
Quoted 101 lines · Alex Williamson
Quoted 99 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress.

I'm asking for bugs to get fixed and not duplicated. Adding more bugs is not progress. Or maybe there is no bug. Let's see why and add a comment.

Quoted 1 line

Those patches are just users of kvm_set_irq.

Well these add calls to kvm_set_irq which scans all vcpus under spinlock. In the past Avi thought this is not a good idea too. Maybe things changed.

Quoted 5 lines
Quoted 2 lines

We can drop the spinlock if we don't care about spurious EOIs, which is only a theoretical scalability problem anyway.

Not theoretical at all IMO. We see the problem with virtio with old guests today.

And how are you injecting level interrupts with virtio today w/o this interface?

Quoted 5 lines
Quoted 3 lines

We're talking about
level interrupts here, how scalable do we need to be?

The reason we are moving them into kernel at all is for speed, no?

Come on, if we take that approach why aren't we writing all of this in assembly for speed?! All I'm suggesting is there's a limit to return on investment at some point. Maybe it's here.

#72 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 103 lines · Alex Williamson
Quoted 102 lines · Michael S. Tsirkin
Quoted 101 lines · Alex Williamson
Quoted 99 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate
effort.

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress.

I'm asking for bugs to get fixed and not duplicated. Adding more bugs is not progress. Or maybe there is no bug. Let's see why and add a comment.

Quoted 1 line

Those patches are just users of kvm_set_irq.

Well these add calls to kvm_set_irq which scans all vcpus under spinlock. In the past Avi thought this is not a good idea too. Maybe things changed.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

We can drop the spinlock if we don't care about spurious EOIs, which is only a theoretical scalability problem anyway.

Not theoretical at all IMO. We see the problem with virtio with old guests today.

And how are you injecting level interrupts with virtio today w/o this interface?

Not well at all. Bad performance with interrupt sharing.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

We're talking about
level interrupts here, how scalable do we need to be?

The reason we are moving them into kernel at all is for speed, no?

Come on, if we take that approach why aren't we writing all of this in assembly for speed?! All I'm suggesting is there's a limit to return on investment at some point. Maybe it's here.

Well I am just warning about known problems: don't wake up (typically) many eoifds on a single interrupt or you will have scalability problems that we already see with emulated devices.

Signature · 1 line
MST
#73 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 121 lines · Michael S. Tsirkin
Quoted 103 lines · Alex Williamson
Quoted 102 lines · Michael S. Tsirkin
Quoted 101 lines · Alex Williamson
Quoted 99 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate
effort.

A separate patchset, sure. But likely a prerequisite: we still need to
look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress.

I'm asking for bugs to get fixed and not duplicated. Adding more bugs is not progress. Or maybe there is no bug. Let's see why and add a comment.

Quoted 1 line

Those patches are just users of kvm_set_irq.

Well these add calls to kvm_set_irq which scans all vcpus under spinlock. In the past Avi thought this is not a good idea too. Maybe things changed.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

We can drop the spinlock if we don't care about spurious EOIs, which is only a theoretical scalability problem anyway.

Not theoretical at all IMO. We see the problem with virtio with old guests today.

And how are you injecting level interrupts with virtio today w/o this interface?

Not well at all. Bad performance with interrupt sharing.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

We're talking about
level interrupts here, how scalable do we need to be?

The reason we are moving them into kernel at all is for speed, no?

Quoted 7 lines
Quoted 3 lines

Come on, if we take that approach why aren't we writing all of this in assembly for speed?! All I'm suggesting is there's a limit to return on investment at some point. Maybe it's here.

Well I am just warning about known problems: don't wake up (typically) many eoifds on a single interrupt or you will have scalability problems that we already see with emulated devices.

Well, that's why we don't want to bounce back to userspace. All we need to do in VFIO for each callback is note that the interrupt wasn't previously masked and do nothing. So we're talking about acquiring a spinlock and a few data references. Sure, I'd like to avoid doing that, but not if it means blocking this patch series until some unknown number of patches fixes all the tangential problems you find.

#74 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 130 lines · Alex Williamson
Quoted 121 lines · Michael S. Tsirkin
Quoted 103 lines · Alex Williamson
Quoted 102 lines · Michael S. Tsirkin
Quoted 101 lines · Alex Williamson
Quoted 99 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 70 lines · Michael S. Tsirkin
Quoted 42 lines · Alex Williamson
Quoted 42 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate
effort.

A separate patchset, sure. But likely a prerequisite: we still need to
look at all the code. Let's not copy bugs, need to fix them.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This looks tangential to me unless you can come up with an actual reason
the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 6 lines
Quoted 3 lines

Maybe this is not a problem. But in that case IMO it needs a comment explaining why and why it's not a problem in your code.

So you want to close the door on anything that uses kvm_set_irq until this gets fixed... that's insane.

What does kvm_set_irq use have to do with it? You posted this patch:

(quoted diff) +6 -0
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+                            struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+       struct kvm_pic *pic = pic_irqchip(kvm);
+       int level =

kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],

(quoted diff) +10 -0
+                                            irq_source_id);
+       if (level)
+               kvm_pic_set_irq(pic, e->irqchip.pin,
+                               !!pic->irq_states[e->irqchip.pin]);
+       return level;
+#else
+       return -1;
+#endif
+}
+
Quoted 1 line

it seems racy in the same way.

Now you're just misrepresenting how we got here, which was:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

IMHO, we're going off into the weeds again with these last
two patches. It may be a valid optimization, but it really has no
bearing on the meat of the series (and afaict, no significant
performance difference either).

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

So I'm happy to drop the last 2 patches, which were done at your request anyway, but you've failed to show how the locking in patches 1&2 is messy, inconsistent, or complex and now you're asking to block all progress.

I'm asking for bugs to get fixed and not duplicated. Adding more bugs is not progress. Or maybe there is no bug. Let's see why and add a comment.

Quoted 1 line

Those patches are just users of kvm_set_irq.

Well these add calls to kvm_set_irq which scans all vcpus under spinlock. In the past Avi thought this is not a good idea too. Maybe things changed.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

We can drop the spinlock if we don't care about spurious EOIs, which is only a theoretical scalability problem anyway.

Not theoretical at all IMO. We see the problem with virtio with old guests today.

And how are you injecting level interrupts with virtio today w/o this interface?

Not well at all. Bad performance with interrupt sharing.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

We're talking about
level interrupts here, how scalable do we need to be?

The reason we are moving them into kernel at all is for speed, no?

Quoted 7 lines
Quoted 3 lines

Come on, if we take that approach why aren't we writing all of this in assembly for speed?! All I'm suggesting is there's a limit to return on investment at some point. Maybe it's here.

Well I am just warning about known problems: don't wake up (typically) many eoifds on a single interrupt or you will have scalability problems that we already see with emulated devices.

Quoted 6 lines

Well, that's why we don't want to bounce back to userspace. All we need to do in VFIO for each callback is note that the interrupt wasn't previously masked and do nothing. So we're talking about acquiring a spinlock and a few data references. Sure, I'd like to avoid doing that, but not if it means blocking this patch series until some unknown number of patches fixes all the tangential problems you find.

I'd like Avi's take on whether kvm_set_irq under a spinlock here is OK. He nacked this in the past.

Meanwhile fixing the tangential problems is time well spent too. If there are races in kvm irq handling it seems more important to fix them than add an optimization of level interrupts.

Signature · 1 line
MST
#75 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

In reply to Michael S. Tsirkin
Quoted 17 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}
Quoted 8 lines

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

This will hardly happen on x86 especially since bit is set with serialized instruction. But there is actually a race here. CPU 0 clears bit 0. CPU 0 read irq_state as 0. CPU 1 sets level to 1. CPU 1 calls kvm_ioapic_set_irq(1). CPU 0 calls kvm_ioapic_set_irq(0). No ioapic thinks the level is 0 but irq_state is not 0.

This untested and un-compiled patch should fix it.

arch/x86/include/asm/kvm_host.h +1 -1
index ef91d79..e22c78b 100644
@@ -825,7 +825,7 @@ int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
 bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
 
-int kvm_pic_set_irq(void *opaque, int irq, int level);
+int kvm_pic_set_irq(void *opaque, int irq);
 
 void kvm_inject_nmi(struct kvm_vcpu *vcpu);
 
arch/x86/kvm/i8259.c +3 -2
index 81cf4fa..0d6988f 100644
@@ -188,12 +188,13 @@ void kvm_pic_update_irq(struct kvm_pic *s)
 	pic_unlock(s);
 }
 
-int kvm_pic_set_irq(void *opaque, int irq, int level)
+int kvm_pic_set_irq(void *opaque, int irq)
 {
 	struct kvm_pic *s = opaque;
-	int ret = -1;
+	int ret = -1, level;
 
 	pic_lock(s);
+	level = !!s->irq_states[irq];
 	if (irq >= 0 && irq < PIC_NUM_PINS) {
 		ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
 		pic_update_irq(s);
virt/kvm/ioapic.c +3 -2
index 26fd54d..6ad6a6b 100644
@@ -191,14 +191,15 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
 }
 
-int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
+int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq)
 {
 	u32 old_irr;
 	u32 mask = 1 << irq;
 	union kvm_ioapic_redirect_entry entry;
-	int ret = 1;
+	int ret = 1, level;
 
 	spin_lock(&ioapic->lock);
+	level = !!ioapic->irq_states[irq];
 	old_irr = ioapic->irr;
 	if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
 		entry = ioapic->redirtbl[irq];
virt/kvm/ioapic.h +1 -1
index 32872a0..65894dd 100644
@@ -74,7 +74,7 @@ void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode);
 bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector);
 int kvm_ioapic_init(struct kvm *kvm);
 void kvm_ioapic_destroy(struct kvm *kvm);
-int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level);
+int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq);
 void kvm_ioapic_reset(struct kvm_ioapic *ioapic);
 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
 		struct kvm_lapic_irq *irq);
virt/kvm/irq_comm.c +5 -7
index a6a0365..db0ccef 100644
@@ -33,7 +33,7 @@
 
 #include "ioapic.h"
 
-static inline int kvm_irq_line_state(unsigned long *irq_state,
+static inline void kvm_irq_line_state(unsigned long *irq_state,
 				     int irq_source_id, int level)
 {
 	/* Logical OR for level trig interrupt */
@@ -41,8 +41,6 @@ static inline int kvm_irq_line_state(unsigned long *irq_state,
 		set_bit(irq_source_id, irq_state);
 	else
 		clear_bit(irq_source_id, irq_state);
-
-	return !!(*irq_state);
 }
 
 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
@@ -50,9 +48,9 @@ static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
 {
 #ifdef CONFIG_X86
 	struct kvm_pic *pic = pic_irqchip(kvm);
-	level = kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
+	kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
 				   irq_source_id, level);
-	return kvm_pic_set_irq(pic, e->irqchip.pin, level);
+	return kvm_pic_set_irq(pic, e->irqchip.pin);
 #else
 	return -1;
 #endif
@@ -62,10 +60,10 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 			      struct kvm *kvm, int irq_source_id, int level)
 {
 	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
-	level = kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+	kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
 				   irq_source_id, level);
 
-	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
+	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin);
 }
 
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)

--
Gleb.

#76 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 22 lines · Gleb Natapov
Quoted 21 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

Quoted 6 lines
Quoted 4 lines

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

Quoted 124 lines

But there is actually a race here.
CPU 0 clears bit 0. CPU 0 read irq_state as 0. CPU 1 sets level to 1. CPU 1 calls kvm_ioapic_set_irq(1). CPU 0 calls kvm_ioapic_set_irq(0). No ioapic thinks the level is 0 but irq_state is not 0.

This untested and un-compiled patch should fix it.

arch/x86/include/asm/kvm_host.h +1 -1
index ef91d79..e22c78b 100644
@@ -825,7 +825,7 @@ int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
 bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
 
-int kvm_pic_set_irq(void *opaque, int irq, int level);
+int kvm_pic_set_irq(void *opaque, int irq);
 
 void kvm_inject_nmi(struct kvm_vcpu *vcpu);
 
arch/x86/kvm/i8259.c +3 -2
index 81cf4fa..0d6988f 100644
@@ -188,12 +188,13 @@ void kvm_pic_update_irq(struct kvm_pic *s)
 	pic_unlock(s);
 }
 
-int kvm_pic_set_irq(void *opaque, int irq, int level)
+int kvm_pic_set_irq(void *opaque, int irq)
 {
 	struct kvm_pic *s = opaque;
-	int ret = -1;
+	int ret = -1, level;
 
 	pic_lock(s);
+	level = !!s->irq_states[irq];
 	if (irq >= 0 && irq < PIC_NUM_PINS) {
 		ret = pic_set_irq1(&s->pics[irq >> 3], irq & 7, level);
 		pic_update_irq(s);
virt/kvm/ioapic.c +3 -2
index 26fd54d..6ad6a6b 100644
@@ -191,14 +191,15 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
 }
 
-int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level)
+int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq)
 {
 	u32 old_irr;
 	u32 mask = 1 << irq;
 	union kvm_ioapic_redirect_entry entry;
-	int ret = 1;
+	int ret = 1, level;
 
 	spin_lock(&ioapic->lock);
+	level = !!ioapic->irq_states[irq];
 	old_irr = ioapic->irr;
 	if (irq >= 0 && irq < IOAPIC_NUM_PINS) {
 		entry = ioapic->redirtbl[irq];
virt/kvm/ioapic.h +1 -1
index 32872a0..65894dd 100644
@@ -74,7 +74,7 @@ void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode);
 bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector);
 int kvm_ioapic_init(struct kvm *kvm);
 void kvm_ioapic_destroy(struct kvm *kvm);
-int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int level);
+int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq);
 void kvm_ioapic_reset(struct kvm_ioapic *ioapic);
 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
 		struct kvm_lapic_irq *irq);
virt/kvm/irq_comm.c +5 -8
index a6a0365..db0ccef 100644
@@ -33,7 +33,7 @@
 
 #include "ioapic.h"
 
-static inline int kvm_irq_line_state(unsigned long *irq_state,
+static inline void kvm_irq_line_state(unsigned long *irq_state,
 				     int irq_source_id, int level)
 {
 	/* Logical OR for level trig interrupt */
@@ -41,8 +41,6 @@ static inline int kvm_irq_line_state(unsigned long *irq_state,
 		set_bit(irq_source_id, irq_state);
 	else
 		clear_bit(irq_source_id, irq_state);
-
-	return !!(*irq_state);
 }
 
 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
@@ -50,9 +48,9 @@ static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
 {
 #ifdef CONFIG_X86
 	struct kvm_pic *pic = pic_irqchip(kvm);
-	level = kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
+	kvm_irq_line_state(&pic->irq_states[e->irqchip.pin],
 				   irq_source_id, level);
-	return kvm_pic_set_irq(pic, e->irqchip.pin, level);
+	return kvm_pic_set_irq(pic, e->irqchip.pin);
 #else
 	return -1;
 #endif
@@ -62,10 +60,10 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 			      struct kvm *kvm, int irq_source_id, int level)
 {
 	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
-	level = kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+	kvm_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
 				   irq_source_id, level);
 
-	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
+	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin);
 }
 
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
--

Gleb.

#77 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Gleb Natapov
Quoted 25 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 9 lines
Quoted 2 lines

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

With my patch I do not see why we can't change them to unlocked variant without moving them anywhere. The only requirement is to not use RMW sequence to set/clear bits. The ordering of setting does not matter. The ordering of reading is.

--
Gleb.

#78 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 37 lines · Gleb Natapov
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Gleb Natapov
Quoted 25 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 9 lines
Quoted 2 lines

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

Quoted 4 lines

With my patch I do not see why we can't change them to unlocked variant without moving them anywhere. The only requirement is to not use RMW sequence to set/clear bits. The ordering of setting does not matter. The ordering of reading is.

You want to use __set_bit/__clear_bit on the same word from multiple CPUs, without locking?
Why won't this lose information?

In any case, it seems simpler and safer to do accesses under lock than rely on specific use.

Quoted 2 lines

--
Gleb.

#79 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 38 lines · Michael S. Tsirkin
Quoted 37 lines · Gleb Natapov
Quoted 36 lines · Michael S. Tsirkin
Quoted 22 lines · Gleb Natapov
Quoted 21 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

Quoted 6 lines
Quoted 4 lines

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

Quoted 8 lines
Quoted 4 lines

With my patch I do not see why we can't change them to unlocked variant without moving them anywhere. The only requirement is to not use RMW sequence to set/clear bits. The ordering of setting does not matter. The ordering of reading is.

You want to use __set_bit/__clear_bit on the same word from multiple CPUs, without locking?
Why won't this lose information?

Because it is not RMW. If it is then yes, you can't do that.

Quoted 6 lines

In any case, it seems simpler and safer to do accesses under lock than rely on specific use.

Quoted 2 lines

--
Gleb.

--
Gleb.

#80 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 44 lines · Gleb Natapov
Quoted 43 lines · Michael S. Tsirkin
Quoted 41 lines · Gleb Natapov
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Gleb Natapov
Quoted 25 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 9 lines
Quoted 2 lines

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

With my patch I do not see why we can't change them to unlocked variant without moving them anywhere. The only requirement is to not use RMW sequence to set/clear bits. The ordering of setting does not matter. The ordering of reading is.

Quoted 4 lines
Quoted 3 lines

You want to use __set_bit/__clear_bit on the same word from multiple CPUs, without locking?
Why won't this lose information?

Because it is not RMW. If it is then yes, you can't do that.

You are saying __set_bit does not do RMW on x86? Interesting. It's probably not a good idea to rely on this I think.

Quoted 9 lines
Quoted 6 lines

In any case, it seems simpler and safer to do accesses under lock than rely on specific use.

Quoted 2 lines

--
Gleb.

--
Gleb.

#81 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 45 lines · Michael S. Tsirkin
Quoted 44 lines · Gleb Natapov
Quoted 43 lines · Michael S. Tsirkin
Quoted 41 lines · Gleb Natapov
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Gleb Natapov
Quoted 25 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 9 lines
Quoted 2 lines

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

With my patch I do not see why we can't change them to unlocked variant without moving them anywhere. The only requirement is to not use RMW sequence to set/clear bits. The ordering of setting does not matter. The ordering of reading is.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

You want to use __set_bit/__clear_bit on the same word from multiple CPUs, without locking?
Why won't this lose information?

Because it is not RMW. If it is then yes, you can't do that.

You are saying __set_bit does not do RMW on x86? Interesting.

I think it doesn't.

Quoted 2 lines

It's probably not a good idea to rely on this I think.

The code is no in arch/x86 so probably no. Although it is used only on x86 (and ia64 which has broken kvm anyway).

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

In any case, it seems simpler and safer to do accesses under lock than rely on specific use.

Quoted 2 lines

--
Gleb.

--
Gleb.

--
Gleb.

#82 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 46 lines · Gleb Natapov
Quoted 45 lines · Michael S. Tsirkin
Quoted 44 lines · Gleb Natapov
Quoted 43 lines · Michael S. Tsirkin
Quoted 41 lines · Gleb Natapov
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Gleb Natapov
Quoted 25 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 9 lines
Quoted 2 lines

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

With my patch I do not see why we can't change them to unlocked variant without moving them anywhere. The only requirement is to not use RMW sequence to set/clear bits. The ordering of setting does not matter. The ordering of reading is.

Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

You want to use __set_bit/__clear_bit on the same word from multiple CPUs, without locking?
Why won't this lose information?

Because it is not RMW. If it is then yes, you can't do that.

You are saying __set_bit does not do RMW on x86? Interesting.

I think it doesn't.

Anywhere I can read about this?

Quoted 4 lines
Quoted 2 lines

It's probably not a good idea to rely on this I think.

The code is no in arch/x86 so probably no. Although it is used only on x86 (and ia64 which has broken kvm anyway).

Yes but exactly the reverse is documented.

/**
* __set_bit - Set a bit in memory
* @nr: the bit to set
* @addr: the address to start counting from
*
* Unlike set_bit(), this function is non-atomic and may be reordered.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

pls note the below

* If it's called on the same region of memory simultaneously, the effect
* may be that only one operation succeeds.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

until here

*/
static inline void __set_bit(int nr, volatile unsigned long *addr)

{
        asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
}
Quoted 12 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

In any case, it seems simpler and safer to do accesses under lock than rely on specific use.

Quoted 2 lines

--
Gleb.

--
Gleb.

--
Gleb.

#83 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 50 lines · Michael S. Tsirkin
Quoted 49 lines · Gleb Natapov
Quoted 48 lines · Michael S. Tsirkin
Quoted 47 lines · Gleb Natapov
Quoted 46 lines · Michael S. Tsirkin
Quoted 37 lines · Gleb Natapov
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Gleb Natapov
Quoted 25 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 9 lines
Quoted 2 lines

This will hardly happen on x86 especially since bit is set with serialized instruction.

Probably. But it does make me a bit uneasy. Why don't we pass irq_source_id to kvm_pic_set_irq/kvm_ioapic_set_irq, and move kvm_irq_line_state to under pic_lock/ioapic_lock? We can then use __set_bit/__clear_bit in kvm_irq_line_state, making the ordering simpler and saving an atomic op in the process.

Quoted 4 lines

With my patch I do not see why we can't change them to unlocked variant without moving them anywhere. The only requirement is to not use RMW sequence to set/clear bits. The ordering of setting does not matter. The ordering of reading is.

You want to use __set_bit/__clear_bit on the same word from multiple CPUs, without locking?
Why won't this lose information?

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Because it is not RMW. If it is then yes, you can't do that.

You are saying __set_bit does not do RMW on x86? Interesting.

I think it doesn't.

Anywhere I can read about this?

Well actually SDM says LOCK prefix is needed, so yes we cannot use __set_bit/__clear_bit without moving it under lock.

Quoted 19 lines
Quoted 4 lines
Quoted 2 lines

It's probably not a good idea to rely on this I think.

The code is no in arch/x86 so probably no. Although it is used only on x86 (and ia64 which has broken kvm anyway).

Yes but exactly the reverse is documented.

/**
* __set_bit - Set a bit in memory
* @nr: the bit to set
* @addr: the address to start counting from
*
* Unlike set_bit(), this function is non-atomic and may be reordered.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

pls note the below

* If it's called on the same region of memory simultaneously, the effect
* may be that only one operation succeeds.

Quoted 23 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

until here

*/
static inline void __set_bit(int nr, volatile unsigned long *addr)

{
        asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
}
Quoted 12 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

In any case, it seems simpler and safer to do accesses under lock than rely on specific use.

Quoted 2 lines

--
Gleb.

--
Gleb.

--
Gleb.

--
Gleb.

#84 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

In reply to Gleb Natapov
Quoted 26 lines · Gleb Natapov
Quoted 17 lines · Michael S. Tsirkin
Quoted 1 line

_Seems_ racy, or _is_ racy? Please identify the race.

Look at this:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

        return !!(*irq_state);
}
Quoted 8 lines

Now:
If other CPU changes some other bit after the atomic change, it looks like !!(*irq_state) might return a stale value.

CPU 0 clears bit 0. CPU 1 sets bit 1. CPU 1 sets level to 1. If CPU 0 sees a stale value now it will return 0 here and interrupt will get cleared.

Quoted 7 lines

This will hardly happen on x86 especially since bit is set with serialized instruction. But there is actually a race here. CPU 0 clears bit 0. CPU 0 read irq_state as 0. CPU 1 sets level to 1. CPU 1 calls kvm_ioapic_set_irq(1). CPU 0 calls kvm_ioapic_set_irq(0). No ioapic thinks the level is 0 but irq_state is not 0.

This untested and un-compiled patch should fix it.

Getting rid of atomics completely makes me more comfortable, and by moving all bitmap handling to under pic/ioapic lock we can do just that.
I just tested and posted a patch that fixes the race in this way. Could you take a look pls?

Signature · 1 line
MST
#85 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

In reply to Alex Williamson
Quoted 183 lines · Alex Williamson
Quoted 182 lines · Michael S. Tsirkin
Quoted 181 lines · Alex Williamson
Quoted 179 lines · Michael S. Tsirkin
Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Only for MSI. That's an exception (and it's also a messy one).

Nope, I see past code that used it for INTx as well.

While this looks like it will not crash, this scans all vcpus under a spinlock. A problem for big VMs.
Again, yes we have such uses now but we are looking for ways to fix them and not be adding more.

Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 1 line

And current code looks buggy if yes we need to fix it somehow.

Which to me seems to indicate this should be handled as a separate effort.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

A separate patchset, sure. But likely a prerequisite: we still need to look at all the code. Let's not copy bugs, need to fix them.

This looks tangential to me unless you can come up with an actual reason the above spinlock usage is incorrect or insufficient.

You copy the same pattern that seems racy. So you double the amount of code that woul need to be fixed.

_Seems_ racy, or _is_ racy? Please identify the race.

#86 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

Quoted 187 lines · Michael S. Tsirkin
Quoted 186 lines · Alex Williamson
Quoted 185 lines · Michael S. Tsirkin
Quoted 183 lines · Alex Williamson
Quoted 173 lines · Michael S. Tsirkin
Quoted 172 lines · Alex Williamson
Quoted 146 lines · Michael S. Tsirkin
Quoted 83 lines · Alex Williamson
Quoted 82 lines · Michael S. Tsirkin
Quoted 80 lines · Alex Williamson
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +12 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
Quoted 8 lines

virt/kvm/irq_comm.c static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, · line 87

Quoted 5 lines
virt/kvm/irq_comm.c +5 -0
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;

I think I begin to understand: if (level) checks it was previously set, and then we clear if needed?

It's actually very simple, if we change anything in irq_states, then update via the chip specific set_irq function.

Quoted 8 lines

I think it's worthwhile to rename
level to orig_level and rewrite as:

	if (orig_level && !pic->irq_states[e->irqchip.pin])
		kvm_pic_set_irq(pic, e->irqchip.pin, 0);

This both makes the logic clear without need for comments and saves some cycles on pic in case nothing actually changed.

That may work, but it's not actually the same thing. kvm_set_irq(,,,0) will clear the bit and call kvm_pic_set_irq with the new irq_states value, whether it's 0 or 1. The optimization I make is to only call kvm_pic_set_irq if we've "changed" irq_states. You're taking that one step further to "changed and is now 0". I don't know if that's correct behavior.

Quoted 7 lines
Quoted 2 lines

If not then I don't understand. You clear a bit
in a word. You never change it to 1, do you?

Correct, but kvm_set_irq(,,,0) may call kvm_pic_set_irq(,,1) if other source IDs are still asserting the interrupt. Your proposal assumes that unless irq_states is also 0 we don't need to call kvm_pic_set_irq, and I don't know if that's correct.

Well you are asked to clear some id and level was 1. So we know interrupt was asserted. Either we clear it or we don't. No?

Quoted 19 lines
Quoted 19 lines

But this brings another question:

static inline int kvm_irq_line_state(unsigned long *irq_state,
int irq_source_id, int level)

{
        /* Logical OR for level trig interrupt */
        if (level)
                set_bit(irq_source_id, irq_state);
        else
                clear_bit(irq_source_id, irq_state);

^^^^^^^^^^^
above uses locked instructions

        return !!(*irq_state);
Quoted 8 lines
Quoted 6 lines

above doesn't

}

why the insonsistency?

Note that set/clear_bit are not locked instructions,

On x86 they are:
static __always_inline void
set_bit(unsigned int nr, volatile unsigned long *addr)

{
        if (IS_IMMEDIATE(nr)) {
                asm volatile(LOCK_PREFIX "orb %1,%0"
                        : CONST_MASK_ADDR(nr, addr)
                        : "iq" ((u8)CONST_MASK(nr))
                        : "memory");
        } else {
                asm volatile(LOCK_PREFIX "bts %1,%0"
                        : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
        }
}
Quoted 7 lines

but atomic
instructions and it could be argued that reading the value is also atomic. At least that was my guess when I stumbled across the same yesterday. IMHO, we're going off into the weeds again with these last two patches. It may be a valid optimization, but it really has no bearing on the meat of the series (and afaict, no significant performance difference either).

Quoted 3 lines

For me it's not a performance thing. IMO code is cleaner without this locking:
we add a lock but only use it in some cases, so the rules become really complex.

Seriously?

        spin_lock(&irqfd->source->lock);
        if (!irqfd->source->level_asserted) {
                kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
                irqfd->source->level_asserted = true;
        }
        spin_unlock(&irqfd->source->lock);

...

        spin_lock(&eoifd->source->lock);
        if (eoifd->source->level_asserted) {
                kvm_set_irq(eoifd->kvm,
                            eoifd->source->id, eoifd->notifier.gsi, 0);
                eoifd->source->level_asserted = false;
                eventfd_signal(eoifd->eventfd, 1);
        }
        spin_unlock(&eoifd->source->lock);
Quoted 6 lines
Quoted 1 line

Locking doesn't get much more straightforward than that

Don't look at it in isolation. You are now calling kvm_set_irq from under a spinlock. You are saying it is always safe but this seems far from obvious. kvm_set_irq used to be unsafe from an atomic context.

Device assignment has been calling kvm_set_irq from atomic context for quite a long time.

Quoted 10 lines
Quoted 3 lines
Quoted 1 line

Only for MSI. That's an exception (and it's also a messy one).

Nope, I see past code that used it for INTx as well.

While this looks like it will not crash, this scans all vcpus under a spinlock. A problem for big VMs.
Again, yes we have such uses now but we are looking for ways to fix them and not be adding more.

Same as with MSI.

--
Gleb.

#87 lore raw

Re: [PATCH v5 3/4] kvm: Create kvm_clear_irq()

In reply to Alex Williamson
Quoted 100 lines · Alex Williamson

This is an alternative to kvm_set_irq(,,,0) which returns the previous assertion state of the interrupt and does nothing if it isn't changed.

---

 include/linux/kvm_host.h |    3 ++
 virt/kvm/irq_comm.c      |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+)
include/linux/kvm_host.h +3 -0
index a7661c0..6c168f1 100644
@@ -219,6 +219,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
 		   struct kvm *kvm, int irq_source_id, int level);
+	int (*clear)(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm *kvm, int irq_source_id);
 	union {
 		struct {
 			unsigned irqchip;
@@ -629,6 +631,7 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   unsigned long *deliver_bitmask);
 #endif
 int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
 		int irq_source_id, int level);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
virt/kvm/irq_comm.c +54 -0
index 5afb431..76e8f22 100644
@@ -68,6 +68,42 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, level);
 }
 
+static inline int kvm_clear_irq_line_state(unsigned long *irq_state,
+					    int irq_source_id)
+{
+	return !!test_and_clear_bit(irq_source_id, irq_state);
+}
+
+static int kvm_clear_pic_irq(struct kvm_kernel_irq_routing_entry *e,
+			     struct kvm *kvm, int irq_source_id)
+{
+#ifdef CONFIG_X86
+	struct kvm_pic *pic = pic_irqchip(kvm);
+	int level = kvm_clear_irq_line_state(&pic->irq_states[e->irqchip.pin],
+					     irq_source_id);
+	if (level)
+		kvm_pic_set_irq(pic, e->irqchip.pin,
+				!!pic->irq_states[e->irqchip.pin]);
+	return level;
+#else
+	return -1;
+#endif
+}
+
+static int kvm_clear_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
+				struct kvm *kvm, int irq_source_id)
+{
+	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	int level;
+
+	level = kvm_clear_irq_line_state(&ioapic->irq_states[e->irqchip.pin],
+					 irq_source_id);
+	if (level)
+		kvm_ioapic_set_irq(ioapic, e->irqchip.pin,
+				   !!ioapic->irq_states[e->irqchip.pin]);
+	return level;
+}
+
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
 #ifdef CONFIG_IA64
@@ -190,6 +226,45 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 	return ret;
 }
 
+/*
+ * Return value:
+ *  < 0   Error
+ *  = 0   Interrupt was not set, did nothing
+ *  > 0   Interrupt was pending, cleared
+ */
+int kvm_clear_irq(struct kvm *kvm, int irq_source_id, u32 irq)
+{
+	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
+	int ret = -EINVAL, i = 0;
+	struct kvm_irq_routing_table *irq_rt;
+	struct hlist_node *n;
+
+	/* Not possible to detect if the guest uses the PIC or the
+	 * IOAPIC.  So clear the bit in both. The guest will ignore
+	 * writes to the unused one.
+	 */
+	rcu_read_lock();

virt/kvm/irq_comm.c int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level) · line 256

Quoted 10 lines
virt/kvm/irq_comm.c +10 -0
+	irq_rt = rcu_dereference(kvm->irq_routing);
+	if (irq < irq_rt->nr_rt_entries)
+		hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+			irq_set[i++] = *e;
+	rcu_read_unlock();
+
+	while (i--) {
+		int r = -EINVAL;
+
+		if (irq_set[i].clear)

I would normally suggest if (likely()) here but recently Marcelo started pushing back against these tags. Maybe add in a separate patch so it's easier to ignore ...

Quoted 34 lines
virt/kvm/irq_comm.c +14 -0
+			r = irq_set[i].clear(&irq_set[i], kvm, irq_source_id);
+
+		if (r < 0)
+			continue;
+
+		ret = r + ((ret < 0) ? 0 : ret);
+	}
+
+	return ret;
+}
+
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
 {
 	struct kvm_irq_ack_notifier *kian;
@@ -344,16 +419,19 @@ static int setup_routing_entry(struct kvm_irq_routing_table *rt,
 		switch (ue->u.irqchip.irqchip) {
 		case KVM_IRQCHIP_PIC_MASTER:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			break;
 		case KVM_IRQCHIP_PIC_SLAVE:
 			e->set = kvm_set_pic_irq;
+			e->clear = kvm_clear_pic_irq;
 			max_pin = 16;
 			delta = 8;
 			break;
 		case KVM_IRQCHIP_IOAPIC:
 			max_pin = KVM_IOAPIC_NUM_PINS;
 			e->set = kvm_set_ioapic_irq;
+			e->clear = kvm_clear_ioapic_irq;
 			break;
 		default:
 			goto out;
#88 lore raw

PATCH v5 4/4 kvm: Convert eoifd to use kvm_clear_irq

In reply to Alex Williamson

We can drop any kind of serialization on the injection side as we expect spurious injections to be both rare and safe. On the EOI side, this continues to filter out both the pic/ioapic work and the eventfd signaling if our source ID has not set the interrupt.

---

 virt/kvm/eventfd.c |   24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
virt/kvm/eventfd.c +4 -20
index 1f9412a..164b4c0 100644
@@ -42,8 +42,6 @@
  */
 struct _irq_source {
 	int id; /* the IRQ source ID */
-	bool level_asserted; /* Track assertion state and protect with lock */
-	spinlock_t lock;     /* to avoid unnecessary re-assert/spurious eoi. */
 	struct kvm *kvm;
 	struct kref kref;
 };
@@ -89,7 +87,6 @@ static struct _irq_source *_irq_source_alloc(struct kvm *kvm)
 	}
 
 	kref_init(&source->kref);
-	spin_lock_init(&source->lock);
 	source->kvm = kvm;
 	source->id = id;
 
@@ -173,8 +170,6 @@ irqfd_inject_level(struct work_struct *work)
 	struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
 
 	/*
-	 * Inject an interrupt only if not already asserted.
-	 *
 	 * We can safely ignore the kvm_set_irq return value here.  If
 	 * masked, the irr bit is still set and will eventually be serviced.
 	 * This interface does not guarantee immediate injection.  If
@@ -182,12 +177,7 @@ irqfd_inject_level(struct work_struct *work)
 	 * re-inject if necessary.  NB, if you need to know if an interrupt
 	 * was coalesced, this interface is not for you.
 	 */
-	spin_lock(&irqfd->source->lock);
-	if (!irqfd->source->level_asserted) {
-		kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
-		irqfd->source->level_asserted = true;
-	}
-	spin_unlock(&irqfd->source->lock);
+	kvm_set_irq(irqfd->kvm, irqfd->source->id, irqfd->gsi, 1);
 }
 
 /*
@@ -828,17 +818,11 @@ static void eoifd_event(struct kvm_irq_ack_notifier *notifier)
 
 	/*
 	 * Ack notifier is per GSI, which may be shared with others.
-	 * Only de-assert and send EOI if our source ID is asserted.
-	 * User needs to re-assert if device still requires service.
+	 * Only send EOI if pending from our source ID.  User needs to
+	 * re-assert if device still requires service.
 	 */
-	spin_lock(&eoifd->source->lock);
-	if (eoifd->source->level_asserted) {
-		kvm_set_irq(eoifd->kvm,
-			    eoifd->source->id, eoifd->notifier.gsi, 0);
-		eoifd->source->level_asserted = false;
+	if (kvm_clear_irq(eoifd->kvm, eoifd->source->id, notifier->gsi) > 0)
 		eventfd_signal(eoifd->eventfd, 1);
-	}
-	spin_unlock(&eoifd->source->lock);
 }
 
 static int kvm_assign_eoifd(struct kvm *kvm, struct kvm_eoifd *args)
#89 lore raw
In reply to Alex Williamson
Quoted 9 lines · Alex Williamson

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.

I have to agree to this, but we need to avoid invoking kvm_set_irq in atomic context, without introducing sprurious eois.

Can bool + spinlock that previous patch has be replaced by an atomic?

Quoted 23 lines

- dropped eoifd gsi support patch as it was only an FYI.

Thanks,

Alex

---

Alex Williamson (4):
kvm: Convert eoifd to use kvm_clear_irq
kvm: Create kvm_clear_irq()
kvm: KVM_EOIFD, an eventfd for EOIs
kvm: Extend irqfd to support level interrupts

 Documentation/virtual/kvm/api.txt |   28 +++
 arch/x86/kvm/x86.c                |    3 
 include/linux/kvm.h               |   18 ++
 include/linux/kvm_host.h          |   16 ++
 virt/kvm/eventfd.c                |  333 +++++++++++++++++++++++++++++++++++++
 virt/kvm/irq_comm.c               |   78 +++++++++
 virt/kvm/kvm_main.c               |   11 +
 7 files changed, 483 insertions(+), 4 deletions(-)
#90 lore raw
In reply to Alex Williamson
Quoted 11 lines · Alex Williamson

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.
- dropped eoifd gsi support patch as it was only an FYI.

Quoted 3 lines

Thanks,

Alex

So 3/4, 4/4 are racy and I think you convinced me it's best to drop it for now. I hope that fact that we already scan all vcpus under spinlock for level interrupts is enough to justify adding a lock here.

To summarize issues still outstanding with 1/2, 2/2:
- source id lingering after irqfd was destroyed/deassigned prevents assigning a new irqfd
- if same irqfd is deassigned and re-assigned, this seems to succeed but does not give any more EOIs
- document that user needs to re-inject interrupts injected by level IRQFD after migration as they are cleared

Hope this helps!

Quoted 17 lines

---

Alex Williamson (4):
kvm: Convert eoifd to use kvm_clear_irq
kvm: Create kvm_clear_irq()
kvm: KVM_EOIFD, an eventfd for EOIs
kvm: Extend irqfd to support level interrupts

 Documentation/virtual/kvm/api.txt |   28 +++
 arch/x86/kvm/x86.c                |    3 
 include/linux/kvm.h               |   18 ++
 include/linux/kvm_host.h          |   16 ++
 virt/kvm/eventfd.c                |  333 +++++++++++++++++++++++++++++++++++++
 virt/kvm/irq_comm.c               |   78 +++++++++
 virt/kvm/kvm_main.c               |   11 +
 7 files changed, 483 insertions(+), 4 deletions(-)
#91 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Alex Williamson

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.
- dropped eoifd gsi support patch as it was only an FYI.

Thanks,

Quoted 8 lines
Quoted 1 line

Alex

So 3/4, 4/4 are racy and I think you convinced me it's best to drop it for now. I hope that fact that we already scan all vcpus under spinlock for level interrupts is enough to justify adding a lock here.

To summarize issues still outstanding with 1/2, 2/2:

(a)

Quoted 2 lines

- source id lingering after irqfd was destroyed/deassigned prevents assigning a new irqfd

(b)

Quoted 2 lines

- if same irqfd is deassigned and re-assigned, this seems to succeed but does not give any more EOIs

(c)

Quoted 4 lines

- document that user needs to re-inject interrupts injected by level IRQFD after migration as they are cleared

Hope this helps!

Thanks, I'm refining and testing a re-write. One thing I also noticed is that we don't do anything when the eoifd is closed. We'll cleanup when kvm is closed, but that can leave a lot of stray eoifds, and therefore used irq_source_ids tied up. So, I think I need to pull in a lot of the irqfd code just to be able to catch the POLLHUP and do cleanup. Fixing (a) is a simple flush, so I already added that. To solve (b), I think that saving the irqfd eventfd ctx was a bad idea. The new api I will propose to solve it is that kvm_irqfd returns a token (or key) when used as a level irqfd (actually the irq source id, but the user shouldn't care what it is). We pass that into eoifd instead of the irqfd. That means that if the irqfd is closed and re-configured, the user will get a new key and should have no expectation that it's tied to the previous eoifd. I'll add a comment for (c). Thanks,

Alex

#92 lore raw
Quoted 33 lines · Alex Williamson
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Alex Williamson

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.
- dropped eoifd gsi support patch as it was only an FYI.

Thanks,

Quoted 8 lines
Quoted 1 line

Alex

So 3/4, 4/4 are racy and I think you convinced me it's best to drop it for now. I hope that fact that we already scan all vcpus under spinlock for level interrupts is enough to justify adding a lock here.

To summarize issues still outstanding with 1/2, 2/2:

(a)

Quoted 2 lines

- source id lingering after irqfd was destroyed/deassigned prevents assigning a new irqfd

(b)

Quoted 2 lines

- if same irqfd is deassigned and re-assigned, this seems to succeed but does not give any more EOIs

(c)

Quoted 3 lines

- document that user needs to re-inject interrupts injected by level IRQFD after migration as they are cleared

Quoted 8 lines
Quoted 1 line

Hope this helps!

Thanks, I'm refining and testing a re-write. One thing I also noticed is that we don't do anything when the eoifd is closed. We'll cleanup when kvm is closed, but that can leave a lot of stray eoifds, and therefore used irq_source_ids tied up. So, I think I need to pull in a lot of the irqfd code just to be able to catch the POLLHUP and do cleanup.

I don't think it's worth it. With ioeventfd we have the same issue and we don't care: userspace should just DEASSIGN before close. With irqfd we committed to support cleanup by close but it happens kind of naturally since we poll irqfd anyway.

It's there for irqfd for historical reasons.

Quoted 2 lines

Fixing (a) is a simple flush, so I already added that. To solve (b), I think that saving the irqfd eventfd ctx was a bad idea.

I actually think we should just fix it. Scan eoifds when closing/opening irqfds and bind/unbind source id.

Quoted 8 lines

The new api I will propose to solve it is that kvm_irqfd returns a token (or key) when used as a level irqfd (actually the irq source id, but the user shouldn't care what it is). We pass that into eoifd instead of the irqfd. That means that if the irqfd is closed and re-configured, the user will get a new key and should have no expectation that it's tied to the previous eoifd. I'll add a comment for (c). Thanks,

Alex

Hmm, another API rewrite, when I felt it is finally stabilizing. Maybe it's the right thing to do but it does feel like we change userspace ABI just because we have run into an implementation difficulty.

Pls note I'm offline next week so won't have time to review soon.

Signature · 1 line
MST
#93 lore raw
Quoted 43 lines · Michael S. Tsirkin
Quoted 33 lines · Alex Williamson
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Alex Williamson

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.
- dropped eoifd gsi support patch as it was only an FYI.

Thanks,

Quoted 8 lines
Quoted 1 line

Alex

So 3/4, 4/4 are racy and I think you convinced me it's best to drop it for now. I hope that fact that we already scan all vcpus under spinlock for level interrupts is enough to justify adding a lock here.

To summarize issues still outstanding with 1/2, 2/2:

(a)

Quoted 2 lines

- source id lingering after irqfd was destroyed/deassigned prevents assigning a new irqfd

(b)

Quoted 2 lines

- if same irqfd is deassigned and re-assigned, this seems to succeed but does not give any more EOIs

(c)

Quoted 3 lines

- document that user needs to re-inject interrupts injected by level IRQFD after migration as they are cleared

Quoted 8 lines
Quoted 1 line

Hope this helps!

Thanks, I'm refining and testing a re-write. One thing I also noticed is that we don't do anything when the eoifd is closed. We'll cleanup when kvm is closed, but that can leave a lot of stray eoifds, and therefore used irq_source_ids tied up. So, I think I need to pull in a lot of the irqfd code just to be able to catch the POLLHUP and do cleanup.

Quoted 6 lines

I don't think it's worth it. With ioeventfd we have the same issue and we don't care: userspace should just DEASSIGN before close. With irqfd we committed to support cleanup by close but it happens kind of naturally since we poll irqfd anyway.

It's there for irqfd for historical reasons.

You're not dealing with such a limited resource for ioeventfds though. It's pretty easily conceivable we could run out of irq source IDs.

Quoted 5 lines
Quoted 2 lines

Fixing (a) is a simple flush, so I already added that. To solve (b), I think that saving the irqfd eventfd ctx was a bad idea.

I actually think we should just fix it. Scan eoifds when closing/opening irqfds and bind/unbind source id.

Hmm, IMHO we had no business holding onto an eventfd ctx. That was an ugly implementation detail forced by the desire to allow the same eventfd to be used in multiple eoifds. The fallout from that leaves a lasting tie between the eoifd and the future use of that eventfd. I can imagine the scenario you present is just one of the glitches and I really don't want to have one interface disable another.

Quoted 7 lines
Quoted 7 lines

The new api I will propose to solve it is that kvm_irqfd returns a token (or key) when used as a level irqfd (actually the irq source id, but the user shouldn't care what it is). We pass that into eoifd instead of the irqfd. That means that if the irqfd is closed and re-configured, the user will get a new key and should have no expectation that it's tied to the previous eoifd. I'll add a comment for (c). Thanks,

Quoted 7 lines
Quoted 1 line

Alex

Hmm, another API rewrite, when I felt it is finally stabilizing. Maybe it's the right thing to do but it does feel like we change userspace ABI just because we have run into an implementation difficulty.

Pls note I'm offline next week so won't have time to review soon.

We could return the key in the struct kvm_irqfd if it adds anything, but I felt returning the key was preferable and is compatible with the existing ABI. Thanks,

Alex

#94 lore raw
Quoted 49 lines · Alex Williamson
Quoted 48 lines · Michael S. Tsirkin
Quoted 33 lines · Alex Williamson
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Alex Williamson

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.
- dropped eoifd gsi support patch as it was only an FYI.

Thanks,

Quoted 8 lines
Quoted 1 line

Alex

So 3/4, 4/4 are racy and I think you convinced me it's best to drop it for now. I hope that fact that we already scan all vcpus under spinlock for level interrupts is enough to justify adding a lock here.

To summarize issues still outstanding with 1/2, 2/2:

(a)

Quoted 2 lines

- source id lingering after irqfd was destroyed/deassigned prevents assigning a new irqfd

(b)

Quoted 2 lines

- if same irqfd is deassigned and re-assigned, this seems to succeed but does not give any more EOIs

(c)

Quoted 3 lines

- document that user needs to re-inject interrupts injected by level IRQFD after migration as they are cleared

Quoted 8 lines
Quoted 1 line

Hope this helps!

Thanks, I'm refining and testing a re-write. One thing I also noticed is that we don't do anything when the eoifd is closed. We'll cleanup when kvm is closed, but that can leave a lot of stray eoifds, and therefore used irq_source_ids tied up. So, I think I need to pull in a lot of the irqfd code just to be able to catch the POLLHUP and do cleanup.

I don't think it's worth it. With ioeventfd we have the same issue and we don't care: userspace should just DEASSIGN before close. With irqfd we committed to support cleanup by close but it happens kind of naturally since we poll irqfd anyway.

Quoted 4 lines
Quoted 1 line

It's there for irqfd for historical reasons.

You're not dealing with such a limited resource for ioeventfds though. It's pretty easily conceivable we could run out of irq source IDs.

Running out of fds is also very conceivable. Not deassigning before close is a userspace bug anyway.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Fixing (a) is a simple flush, so I already added that. To solve (b), I think that saving the irqfd eventfd ctx was a bad idea.

I actually think we should just fix it. Scan eoifds when closing/opening irqfds and bind/unbind source id.

Quoted 6 lines

Hmm, IMHO we had no business holding onto an eventfd ctx. That was an ugly implementation detail forced by the desire to allow the same eventfd to be used in multiple eoifds. The fallout from that leaves a lasting tie between the eoifd and the future use of that eventfd. I can imagine the scenario you present is just one of the glitches and I really don't want to have one interface disable another.

Looks like this disabling is inherent in what we want eoifd to do. You bind irqfd and eoifd. If irqfd is deassigned, eoifd will not get any more events, it is disabled. Whether it keeps the pointer to source id internally or not does not matter to the user.

Quoted 13 lines
Quoted 13 lines
Quoted 8 lines

The new api I will propose to solve it is that kvm_irqfd returns a token (or key) when used as a level irqfd (actually the irq source id, but the user shouldn't care what it is). We pass that into eoifd instead of the irqfd. That means that if the irqfd is closed and re-configured, the user will get a new key and should have no expectation that it's tied to the previous eoifd. I'll add a comment for (c). Thanks,

Alex

Hmm, another API rewrite, when I felt it is finally stabilizing. Maybe it's the right thing to do but it does feel like we change userspace ABI just because we have run into an implementation difficulty.

Quoted 7 lines
Quoted 1 line

Pls note I'm offline next week so won't have time to review soon.

We could return the key in the struct kvm_irqfd if it adds anything, but I felt returning the key was preferable and is compatible with the existing ABI. Thanks,

Alex

You say it is preferable but I wonder what does it buy users compared to using the fd directly - it is certainly more work for userspace to keep track of it.

Signature · 1 line
MST
#95 lore raw
Quoted 50 lines · Michael S. Tsirkin
Quoted 49 lines · Alex Williamson
Quoted 48 lines · Michael S. Tsirkin
Quoted 33 lines · Alex Williamson
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Alex Williamson

v5:
- irqfds now have a one-to-one mapping with eoifds to prevent users from consuming all of kernel memory by repeatedly creating eoifds from a single irqfd.
- implement a kvm_clear_irq() which does a test_and_clear_bit of the irq_state, only updating the pic/ioapic if changes and allowing the caller to know if anything was done. I added this onto the end as it's essentially an optimization on the previous design. It's hard to tell if there's an actual performance benefit to this.
- dropped eoifd gsi support patch as it was only an FYI.

Thanks,

Quoted 8 lines
Quoted 1 line

Alex

So 3/4, 4/4 are racy and I think you convinced me it's best to drop it for now. I hope that fact that we already scan all vcpus under spinlock for level interrupts is enough to justify adding a lock here.

To summarize issues still outstanding with 1/2, 2/2:

(a)

Quoted 2 lines

- source id lingering after irqfd was destroyed/deassigned prevents assigning a new irqfd

(b)

Quoted 2 lines

- if same irqfd is deassigned and re-assigned, this seems to succeed but does not give any more EOIs

(c)

Quoted 3 lines

- document that user needs to re-inject interrupts injected by level IRQFD after migration as they are cleared

Quoted 8 lines
Quoted 1 line

Hope this helps!

Thanks, I'm refining and testing a re-write. One thing I also noticed is that we don't do anything when the eoifd is closed. We'll cleanup when kvm is closed, but that can leave a lot of stray eoifds, and therefore used irq_source_ids tied up. So, I think I need to pull in a lot of the irqfd code just to be able to catch the POLLHUP and do cleanup.

I don't think it's worth it. With ioeventfd we have the same issue and we don't care: userspace should just DEASSIGN before close. With irqfd we committed to support cleanup by close but it happens kind of naturally since we poll irqfd anyway.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

It's there for irqfd for historical reasons.

You're not dealing with such a limited resource for ioeventfds though. It's pretty easily conceivable we could run out of irq source IDs.

Running out of fds is also very conceivable. Not deassigning before close is a userspace bug anyway.

Close should free all recourses allocated by an fd. What if a code that closes the fd have no idea what cleanup should be done (fd was passed by unix socket). Heck, the code may not have permission to call ioctl to deassign.

--
Gleb.