Skip to contentSkip to conversation

kvm: x86: hyperv: guest->host event signaling via eventfd

PATCH Superseded Roman Kagan 2 participants 8 messages Dec 7, 2017 – Dec 12, 2017

Subsystems (5)
Documentation x86 KVM KVM x86 +1 more subsystems

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 cover kvm: x86: hyperv: guest->host event signaling via eventfd

Make it possible for guests using Hyper-V emulation to do guest->host notification via EVENT_SIGNAL hypercall without a user exit.

v2 -> v3:
- expand docs on allowed values and return codes
- fix uninitialized return
- style fixes

v1 -> v2:
- make data types consistent
- get by without the recently dropped struct hv_input_signal_event
- fix subject prefixes

Roman Kagan (2):
kvm: x86: factor out kvm.arch.hyperv (de)init
kvm: x86: hyperv: guest->host event signaling via eventfd

 Documentation/virtual/kvm/api.txt |  31 ++++++++++
 arch/x86/include/asm/kvm_host.h   |   2 +
 arch/x86/kvm/hyperv.h             |   4 ++
 include/uapi/linux/kvm.h          |  13 ++++
 arch/x86/kvm/hyperv.c             | 123 +++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c                |  13 +++-
 6 files changed, 184 insertions(+), 2 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH v3 1/2 kvm: x86: factor out kvm.arch.hyperv (de)init

Move kvm.arch.hyperv initialization and cleanup to separate functions.

For now only a mutex is inited in the former, and the latter is empty; more stuff will go in there in a followup patch.

---
v2 -> v3:
- style fixes

v1 -> v2:
- fix subject prefix

 arch/x86/kvm/hyperv.h | 3 +++
 arch/x86/kvm/hyperv.c | 9 +++++++++
 arch/x86/kvm/x86.c    | 3 ++-
 3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/hyperv.h +3 -0
index e637631a9574..cc2468244ca2 100644
@@ -88,4 +88,7 @@ void kvm_hv_process_stimers(struct kvm_vcpu *vcpu);
 void kvm_hv_setup_tsc_page(struct kvm *kvm,
 			   struct pvclock_vcpu_time_info *hv_clock);
 
+void kvm_hv_init_vm(struct kvm *kvm);
+void kvm_hv_destroy_vm(struct kvm *kvm);
+
 #endif
arch/x86/kvm/hyperv.c +9 -0
index dc97f2544b6f..015fb06c7522 100644
@@ -1301,3 +1301,12 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 	kvm_hv_hypercall_set_result(vcpu, ret);
 	return 1;
 }
+
+void kvm_hv_init_vm(struct kvm *kvm)
+{
+	mutex_init(&kvm->arch.hyperv.hv_lock);
+}
+
+void kvm_hv_destroy_vm(struct kvm *kvm)
+{
+}
arch/x86/kvm/x86.c +2 -1
index eee8e7faf1af..d0ff5e1cf464 100644
@@ -8149,7 +8149,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 	raw_spin_lock_init(&kvm->arch.tsc_write_lock);
 	mutex_init(&kvm->arch.apic_map_lock);
-	mutex_init(&kvm->arch.hyperv.hv_lock);
 	spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
 
 	kvm->arch.kvmclock_offset = -ktime_get_boot_ns();
@@ -8158,6 +8157,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
 	INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
 
+	kvm_hv_init_vm(kvm);
 	kvm_page_track_init(kvm);
 	kvm_mmu_init_vm(kvm);
 
@@ -8292,6 +8292,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 	kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
 	kvm_mmu_uninit_vm(kvm);
 	kvm_page_track_cleanup(kvm);
+	kvm_hv_destroy_vm(kvm);
 }
 
 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Signature · 1 line
2.14.3
#2 lore raw

PATCH v3 2/2 kvm: x86: hyperv: guest->host event signaling via eventfd

In reply to Roman Kagan

In Hyper-V, the fast guest->host notification mechanism is the SIGNAL_EVENT hypercall, with a single parameter of the connection ID to signal.

Currently this hypercall incurs a user exit and requires the userspace to decode the parameters and trigger the notification of the potentially different I/O context.

To avoid the costly user exit, process this hypercall and signal the corresponding eventfd in KVM, similar to ioeventfd. The association between the connection id and the eventfd is established via the newly introduced KVM_HYPERV_EVENTFD ioctl, and maintained in an (srcu-protected) IDR.

---
v2 -> v3:
- expand docs on allowed values and return codes
- fix uninitialized return
- style fixes

v1 -> v2:
- make data types consistent
- get by without the recently dropped struct hv_input_signal_event
- fix subject prefix

 Documentation/virtual/kvm/api.txt |  31 +++++++++++
 arch/x86/include/asm/kvm_host.h   |   2 +
 arch/x86/kvm/hyperv.h             |   1 +
 include/uapi/linux/kvm.h          |  13 +++++
 arch/x86/kvm/hyperv.c             | 114 +++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c                |  10 ++++
 6 files changed, 170 insertions(+), 1 deletion(-)
Documentation/virtual/kvm/api.txt +31 -0
index f670e4b9e7f3..9c20d5d4a8f8 100644
@@ -3394,6 +3394,37 @@ invalid, if invalid pages are written to (e.g. after the end of memory)
 or if no page table is present for the addresses (e.g. when using
 hugepages).
 
+4.109 KVM_HYPERV_EVENTFD
+
+Capability: KVM_CAP_HYPERV_EVENTFD
+Architectures: x86
+Type: vm ioctl
+Parameters: struct kvm_hyperv_eventfd (in)
+
+This ioctl (un)registers an eventfd to receive notifications from the guest on
+the specified Hyper-V connection id through the SIGNAL_EVENT hypercall, without
+causing a user exit.
+
+struct kvm_hyperv_eventfd {
+	__u32 conn_id;
+	__s32 fd;
+	__u32 flags;
+	__u32 padding[3];
+};
+
+The conn_id field should fit within 24 bits:
+
+#define KVM_HYPERV_CONN_ID_MASK		0x00ffffff
+
+The acceptable values for the flags field are:
+
+#define KVM_HYPERV_EVENTFD_DEASSIGN	(1 << 0)
+
+Returns: 0 on success,
+	-EINVAL if conn_id or flags is outside the allowed range
+	-ENOENT on deassign if the conn_id isn't registered
+	-EEXIST on assign if the conn_id is already registered
+
 5. The kvm_run structure
 ------------------------
 
arch/x86/include/asm/kvm_host.h +2 -0
index 977de5fb968b..d2d318540c87 100644
@@ -739,6 +739,8 @@ struct kvm_hv {
 	u64 hv_crash_ctl;
 
 	HV_REFERENCE_TSC_PAGE tsc_ref;
+
+	struct idr conn_to_evt;
 };
 
 enum kvm_irqchip_mode {
arch/x86/kvm/hyperv.h +1 -0
index cc2468244ca2..837465d69c6d 100644
@@ -90,5 +90,6 @@ void kvm_hv_setup_tsc_page(struct kvm *kvm,
 
 void kvm_hv_init_vm(struct kvm *kvm);
 void kvm_hv_destroy_vm(struct kvm *kvm);
+int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args);
 
 #endif
include/uapi/linux/kvm.h +13 -0
index 282d7613fce8..0830d3d423b6 100644
@@ -932,6 +932,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_HYPERV_SYNIC2 148
 #define KVM_CAP_HYPERV_VP_INDEX 149
 #define KVM_CAP_S390_AIS_MIGRATION 150
+#define KVM_CAP_HYPERV_EVENTFD 151
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1359,6 +1360,8 @@ struct kvm_s390_ucas_mapping {
 #define KVM_S390_GET_CMMA_BITS      _IOWR(KVMIO, 0xb8, struct kvm_s390_cmma_log)
 #define KVM_S390_SET_CMMA_BITS      _IOW(KVMIO, 0xb9, struct kvm_s390_cmma_log)
 
+#define KVM_HYPERV_EVENTFD	_IOW(KVMIO,  0xba, struct kvm_hyperv_eventfd)
+
 #define KVM_DEV_ASSIGN_ENABLE_IOMMU	(1 << 0)
 #define KVM_DEV_ASSIGN_PCI_2_3		(1 << 1)
 #define KVM_DEV_ASSIGN_MASK_INTX	(1 << 2)
@@ -1419,4 +1422,14 @@ struct kvm_assigned_msix_entry {
 #define KVM_ARM_DEV_EL1_PTIMER		(1 << 1)
 #define KVM_ARM_DEV_PMU			(1 << 2)
 
+struct kvm_hyperv_eventfd {
+	__u32 conn_id;
+	__s32 fd;
+	__u32 flags;
+	__u32 padding[3];
+};
+
+#define KVM_HYPERV_CONN_ID_MASK		0x00ffffff
+#define KVM_HYPERV_EVENTFD_DEASSIGN	(1 << 0)
+
 #endif /* __LINUX_KVM_H */
arch/x86/kvm/hyperv.c +113 -1

141 lines not on this page · raw message

arch/x86/kvm/x86.c +10 -0
index d0ff5e1cf464..45d28b39edb7 100644
@@ -2701,6 +2701,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_HYPERV_SYNIC:
 	case KVM_CAP_HYPERV_SYNIC2:
 	case KVM_CAP_HYPERV_VP_INDEX:
+	case KVM_CAP_HYPERV_EVENTFD:
 	case KVM_CAP_PCI_SEGMENT:
 	case KVM_CAP_DEBUGREGS:
 	case KVM_CAP_X86_ROBUST_SINGLESTEP:
@@ -4296,6 +4297,15 @@ long kvm_arch_vm_ioctl(struct file *filp,
 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
 		break;
 	}
+	case KVM_HYPERV_EVENTFD: {
+		struct kvm_hyperv_eventfd hvevfd;
+
+		r = -EFAULT;
+		if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
+			goto out;
+		r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
+		break;
+	}
 	default:
 		r = -ENOTTY;
 	}
Signature · 1 line
2.14.3
#3 lore raw
Quoted 18 lines

#endif /* __LINUX_KVM_H */

arch/x86/kvm/hyperv.c +1 -0
index 015fb06c7522..0303bd8f4d6a 100644
@@ -29,6 +29,7 @@
 #include <linux/kvm_host.h>
 #include <linux/highmem.h>
 #include <linux/sched/cputime.h>
+#include <linux/eventfd.h>
 
 #include <asm/apicdef.h>
 #include <trace/events/kvm.h>
@@ -1226,6 +1227,54 @@ static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
 	return 1;
 }
 

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1239

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+static u16 hvcall_sigevent_param(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *param)
+{
+	struct page *page;
+	void *pg;
+	u64 *msg;
+
+	if ((gpa & (__alignof__(*msg) - 1)) ||
+	    offset_in_page(gpa) + sizeof(*msg) > PAGE_SIZE)
+		return HV_STATUS_INVALID_ALIGNMENT;
+

Don't we also need srcu_read_lock(&vcpu->kvm->srcu) for kvm_vcpu_gfn_to_page?

Quoted 16 lines
arch/x86/kvm/hyperv.c +16 -0
+	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
+	if (is_error_page(page))
+		return HV_STATUS_INSUFFICIENT_MEMORY;
+
+	pg = kmap_atomic(page);
+	msg = pg + offset_in_page(gpa);
+	*param = *msg;
+	kunmap_atomic(pg);
+	return HV_STATUS_SUCCESS;
+}
+
+static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
+{
+	u16 ret;
+	u32 conn_id;
+	int idx;

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1265

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+	struct eventfd_ctx *eventfd;
+
+	if (unlikely(!fast)) {
+		u64 gpa = param;
+		ret = hvcall_sigevent_param(vcpu, gpa, &param);
+		if (ret != HV_STATUS_SUCCESS)
+			return ret;
+	}
+
+	conn_id = (param & 0xffffffff) + ((param >> 32) & 0xffff);

conn_id = param + ((param >> 32) & 0xffff);

(as param will me automatically truncated to 32 bits - sizeof(conn_id))

Quoted 31 lines
arch/x86/kvm/hyperv.c +17 -1
+	if (conn_id & ~KVM_HYPERV_CONN_ID_MASK)
+		return HV_STATUS_INVALID_CONNECTION_ID;
+
+	idx = srcu_read_lock(&vcpu->kvm->srcu);
+	eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, conn_id);
+	if (eventfd)
+		eventfd_signal(eventfd, 1);
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+
+	return eventfd ? HV_STATUS_SUCCESS : HV_STATUS_INVALID_CONNECTION_ID;
+}
+
 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 {
 	u64 param, ingpa, outgpa, ret;
@@ -1276,8 +1325,12 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 	case HVCALL_NOTIFY_LONG_SPIN_WAIT:
 		kvm_vcpu_on_spin(vcpu, true);
 		break;
-	case HVCALL_POST_MESSAGE:
 	case HVCALL_SIGNAL_EVENT:
+		res = kvm_hvcall_signal_event(vcpu, fast, ingpa);
+		if (res != HV_STATUS_INVALID_CONNECTION_ID)
+			break;
+		/* maybe userspace knows this conn_id: fall through */
+	case HVCALL_POST_MESSAGE:
 		/* don't bother userspace if it has no way to handle it */
 		if (!vcpu_to_synic(vcpu)->active) {
 			res = HV_STATUS_INVALID_HYPERCALL_CODE;
@@ -1305,8 +1358,67 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 void kvm_hv_init_vm(struct kvm *kvm)

arch/x86/kvm/hyperv.c int kvm_hv_hypercall(struct kvm_vcpu *vcpu) · line 1368

Quoted 10 lines
arch/x86/kvm/hyperv.c +4 -0
 {
 	mutex_init(&kvm->arch.hyperv.hv_lock);
+	idr_init(&kvm->arch.hyperv.conn_to_evt);
 }
 
 void kvm_hv_destroy_vm(struct kvm *kvm)
 {
+	struct eventfd_ctx *eventfd;
+	int i;
+

At this point we are guaranteed that nobody else can call kvm_hv_eventfd_assign/unassign? (I assume so, just want to have it verified that we don't need the lock :) )

Quoted 22 lines
arch/x86/kvm/hyperv.c +22 -0
+	idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
+		eventfd_ctx_put(eventfd);
+	idr_destroy(&kvm->arch.hyperv.conn_to_evt);
+}
+
+static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
+{
+	struct kvm_hv *hv = &kvm->arch.hyperv;
+	struct eventfd_ctx *eventfd;
+	int ret;
+
+	eventfd = eventfd_ctx_fdget(fd);
+	if (IS_ERR(eventfd))
+		return PTR_ERR(eventfd);
+
+	mutex_lock(&hv->hv_lock);
+	ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
+			GFP_KERNEL);
+	mutex_unlock(&hv->hv_lock);
+
+	if (ret >= 0)
+		return 0;

arch/x86/kvm/hyperv.c int kvm_hv_hypercall(struct kvm_vcpu *vcpu) · line 1400

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+
+	if (ret == -ENOSPC)
+		ret = -EEXIST;
+	eventfd_ctx_put(eventfd);
+	return ret;
+}
+
+static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
+{
+	struct kvm_hv *hv = &kvm->arch.hyperv;

Guess we could move that inline without exceeding any line lengths, but just my opinion. (same applies in the function above where we already need two lines either way for idr_alloc)

Quoted 14 lines
arch/x86/kvm/hyperv.c +14 -0
+	struct eventfd_ctx *eventfd;
+
+	mutex_lock(&hv->hv_lock);
+	eventfd = idr_remove(&hv->conn_to_evt, conn_id);
+	mutex_unlock(&hv->hv_lock);
+
+	if (!eventfd)
+		return -ENOENT;
+
+	synchronize_srcu(&kvm->srcu);
+	eventfd_ctx_put(eventfd);
+	return 0;
+}
+
Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw
Quoted 18 lines · David Hildenbrand
Quoted 18 lines

#endif /* __LINUX_KVM_H */

arch/x86/kvm/hyperv.c +1 -0
index 015fb06c7522..0303bd8f4d6a 100644
@@ -29,6 +29,7 @@
 #include <linux/kvm_host.h>
 #include <linux/highmem.h>
 #include <linux/sched/cputime.h>
+#include <linux/eventfd.h>
 
 #include <asm/apicdef.h>
 #include <trace/events/kvm.h>
@@ -1226,6 +1227,54 @@ static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
Quoted 13 lines

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1239

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+static u16 hvcall_sigevent_param(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *param)
+{
+	struct page *page;
+	void *pg;
+	u64 *msg;
+
+	if ((gpa & (__alignof__(*msg) - 1)) ||
+	    offset_in_page(gpa) + sizeof(*msg) > PAGE_SIZE)
+		return HV_STATUS_INVALID_ALIGNMENT;
+

Don't we also need srcu_read_lock(&vcpu->kvm->srcu) for kvm_vcpu_gfn_to_page?

Seems so indeed. I also think I'll switch to using kvm_vcpu_read_guest().

Quoted 29 lines
Quoted 16 lines
arch/x86/kvm/hyperv.c +16 -0
+	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
+	if (is_error_page(page))
+		return HV_STATUS_INSUFFICIENT_MEMORY;
+
+	pg = kmap_atomic(page);
+	msg = pg + offset_in_page(gpa);
+	*param = *msg;
+	kunmap_atomic(pg);
+	return HV_STATUS_SUCCESS;
+}
+
+static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
+{
+	u16 ret;
+	u32 conn_id;
+	int idx;

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1265

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+	struct eventfd_ctx *eventfd;
+
+	if (unlikely(!fast)) {
+		u64 gpa = param;
+		ret = hvcall_sigevent_param(vcpu, gpa, &param);
+		if (ret != HV_STATUS_SUCCESS)
+			return ret;
+	}
+
+	conn_id = (param & 0xffffffff) + ((param >> 32) & 0xffff);

conn_id = param + ((param >> 32) & 0xffff);

Quoted 1 line

(as param will me automatically truncated to 32 bits - sizeof(conn_id))

Agreed. However, it looks like what I really need is

conn_id = (param & KVM_HYPERV_CONN_ID_MASK) + ((param >> 32) & 0xffff);

otherwise the overflown conn_id may pass the following check

Quoted 36 lines
Quoted 36 lines
arch/x86/kvm/hyperv.c +18 -1
+	if (conn_id & ~KVM_HYPERV_CONN_ID_MASK)
+		return HV_STATUS_INVALID_CONNECTION_ID;
+
+	idx = srcu_read_lock(&vcpu->kvm->srcu);
+	eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, conn_id);
+	if (eventfd)
+		eventfd_signal(eventfd, 1);
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+
+	return eventfd ? HV_STATUS_SUCCESS : HV_STATUS_INVALID_CONNECTION_ID;
+}
+
 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 {
 	u64 param, ingpa, outgpa, ret;
@@ -1276,8 +1325,12 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 	case HVCALL_NOTIFY_LONG_SPIN_WAIT:
 		kvm_vcpu_on_spin(vcpu, true);
 		break;
-	case HVCALL_POST_MESSAGE:
 	case HVCALL_SIGNAL_EVENT:
+		res = kvm_hvcall_signal_event(vcpu, fast, ingpa);
+		if (res != HV_STATUS_INVALID_CONNECTION_ID)
+			break;
+		/* maybe userspace knows this conn_id: fall through */
+	case HVCALL_POST_MESSAGE:
 		/* don't bother userspace if it has no way to handle it */
 		if (!vcpu_to_synic(vcpu)->active) {
 			res = HV_STATUS_INVALID_HYPERCALL_CODE;
@@ -1305,8 +1358,67 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
 void kvm_hv_init_vm(struct kvm *kvm)
 {
 	mutex_init(&kvm->arch.hyperv.hv_lock);
+	idr_init(&kvm->arch.hyperv.conn_to_evt);
 }
 
Quoted 9 lines

arch/x86/kvm/hyperv.c int kvm_hv_hypercall(struct kvm_vcpu *vcpu) · line 1368

Quoted 5 lines
arch/x86/kvm/hyperv.c +3 -0
 void kvm_hv_destroy_vm(struct kvm *kvm)
 {
+	struct eventfd_ctx *eventfd;
+	int i;
+

At this point we are guaranteed that nobody else can call kvm_hv_eventfd_assign/unassign? (I assume so, just want to have it verified that we don't need the lock :) )

Yes, it's in kvm_destroy_vm, which won't get called until the kvm vm fd is released, so no concurrency with kvm_vm_ioctl is possible.

Quoted 28 lines
Quoted 28 lines
arch/x86/kvm/hyperv.c +28 -0
+	idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
+		eventfd_ctx_put(eventfd);
+	idr_destroy(&kvm->arch.hyperv.conn_to_evt);
+}
+
+static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
+{
+	struct kvm_hv *hv = &kvm->arch.hyperv;
+	struct eventfd_ctx *eventfd;
+	int ret;
+
+	eventfd = eventfd_ctx_fdget(fd);
+	if (IS_ERR(eventfd))
+		return PTR_ERR(eventfd);
+
+	mutex_lock(&hv->hv_lock);
+	ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
+			GFP_KERNEL);
+	mutex_unlock(&hv->hv_lock);
+
+	if (ret >= 0)
+		return 0;
+
+	if (ret == -ENOSPC)
+		ret = -EEXIST;
+	eventfd_ctx_put(eventfd);
+	return ret;
+}
Quoted 8 lines

arch/x86/kvm/hyperv.c int kvm_hv_hypercall(struct kvm_vcpu *vcpu) · line 1400

Quoted 4 lines
arch/x86/kvm/hyperv.c +4 -0
+
+static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
+{
+	struct kvm_hv *hv = &kvm->arch.hyperv;

Guess we could move that inline without exceeding any line lengths, but just my opinion. (same applies in the function above where we already need two lines either way for idr_alloc)

Right, but it still reads easier with fewer dots and arrows IMHO.

Thanks,
Roman.

#5 lore raw
Quoted 33 lines · Roman Kagan
Quoted 18 lines · David Hildenbrand
Quoted 18 lines

#endif /* __LINUX_KVM_H */

arch/x86/kvm/hyperv.c +1 -0
index 015fb06c7522..0303bd8f4d6a 100644
@@ -29,6 +29,7 @@
 #include <linux/kvm_host.h>
 #include <linux/highmem.h>
 #include <linux/sched/cputime.h>
+#include <linux/eventfd.h>
 
 #include <asm/apicdef.h>
 #include <trace/events/kvm.h>
@@ -1226,6 +1227,54 @@ static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
 	return 1;
 }
 
Quoted 13 lines

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1239

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+static u16 hvcall_sigevent_param(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *param)
+{
+	struct page *page;
+	void *pg;
+	u64 *msg;
+
+	if ((gpa & (__alignof__(*msg) - 1)) ||
+	    offset_in_page(gpa) + sizeof(*msg) > PAGE_SIZE)
+		return HV_STATUS_INVALID_ALIGNMENT;
+

Don't we also need srcu_read_lock(&vcpu->kvm->srcu) for kvm_vcpu_gfn_to_page?

Quoted 2 lines

Seems so indeed. I also think I'll switch to using kvm_vcpu_read_guest().

Probably better (because I think you also missed releasing the page)

Quoted 30 lines
Quoted 30 lines
Quoted 17 lines
arch/x86/kvm/hyperv.c +16 -0
+	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
+	if (is_error_page(page))
+		return HV_STATUS_INSUFFICIENT_MEMORY;
+
+	pg = kmap_atomic(page);
+	msg = pg + offset_in_page(gpa);
+	*param = *msg;
+	kunmap_atomic(pg);
+	return HV_STATUS_SUCCESS;
+}
+
+static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
+{
+	u16 ret;
+	u32 conn_id;
+	int idx;

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1265

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+	struct eventfd_ctx *eventfd;
+
+	if (unlikely(!fast)) {
+		u64 gpa = param;
+		ret = hvcall_sigevent_param(vcpu, gpa, &param);
+		if (ret != HV_STATUS_SUCCESS)
+			return ret;
+	}
+
+	conn_id = (param & 0xffffffff) + ((param >> 32) & 0xffff);

conn_id = param + ((param >> 32) & 0xffff);

Quoted 7 lines
Quoted 1 line

(as param will me automatically truncated to 32 bits - sizeof(conn_id))

Agreed. However, it looks like what I really need is

conn_id = (param & KVM_HYPERV_CONN_ID_MASK) + ((param >> 32) & 0xffff);

otherwise the overflown conn_id may pass the following check

That would have been my next question :)

Signature · 3 lines
Thanks,

David / dhildenb
#6 lore raw
Quoted 34 lines · David Hildenbrand
Quoted 33 lines · Roman Kagan
Quoted 31 lines · David Hildenbrand
Quoted 18 lines

#endif /* __LINUX_KVM_H */

arch/x86/kvm/hyperv.c +1 -0
index 015fb06c7522..0303bd8f4d6a 100644
@@ -29,6 +29,7 @@
 #include <linux/kvm_host.h>
 #include <linux/highmem.h>
 #include <linux/sched/cputime.h>
+#include <linux/eventfd.h>
 
 #include <asm/apicdef.h>
 #include <trace/events/kvm.h>
@@ -1226,6 +1227,54 @@ static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
 	return 1;
 }
 

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1239

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+static u16 hvcall_sigevent_param(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *param)
+{
+	struct page *page;
+	void *pg;
+	u64 *msg;
+
+	if ((gpa & (__alignof__(*msg) - 1)) ||
+	    offset_in_page(gpa) + sizeof(*msg) > PAGE_SIZE)
+		return HV_STATUS_INVALID_ALIGNMENT;
+

Don't we also need srcu_read_lock(&vcpu->kvm->srcu) for kvm_vcpu_gfn_to_page?

Quoted 4 lines
Quoted 2 lines

Seems so indeed. I also think I'll switch to using kvm_vcpu_read_guest().

Probably better (because I think you also missed releasing the page)

Hmm, I wonder where? (kvm_vcpu_read_guest ended up being more concise so I'll stick with it but I'd like to learn from my errors anyway.)

Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 18 lines
arch/x86/kvm/hyperv.c +16 -0
+	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
+	if (is_error_page(page))
+		return HV_STATUS_INSUFFICIENT_MEMORY;
+
+	pg = kmap_atomic(page);
+	msg = pg + offset_in_page(gpa);
+	*param = *msg;
+	kunmap_atomic(pg);
+	return HV_STATUS_SUCCESS;
+}
+
+static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
+{
+	u16 ret;
+	u32 conn_id;
+	int idx;

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1265

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+	struct eventfd_ctx *eventfd;
+
+	if (unlikely(!fast)) {
+		u64 gpa = param;
+		ret = hvcall_sigevent_param(vcpu, gpa, &param);
+		if (ret != HV_STATUS_SUCCESS)
+			return ret;
+	}
+
+	conn_id = (param & 0xffffffff) + ((param >> 32) & 0xffff);

conn_id = param + ((param >> 32) & 0xffff);

(as param will me automatically truncated to 32 bits - sizeof(conn_id))

Quoted 7 lines
Quoted 5 lines

Agreed. However, it looks like what I really need is

conn_id = (param & KVM_HYPERV_CONN_ID_MASK) + ((param >> 32) & 0xffff);

otherwise the overflown conn_id may pass the following check

That would have been my next question :)

Thanks,
Roman.

#7 lore raw
Quoted 21 lines · Roman Kagan
Quoted 20 lines · David Hildenbrand
Quoted 19 lines · Roman Kagan
Quoted 17 lines · David Hildenbrand
Quoted 4 lines
arch/x86/kvm/hyperv.c +0 -0
@@ -1226,6 +1227,54 @@ static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
 	return 1;
 }
 

arch/x86/kvm/hyperv.c static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu) · line 1239

Quoted 10 lines
arch/x86/kvm/hyperv.c +10 -0
+static u16 hvcall_sigevent_param(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *param)
+{
+	struct page *page;
+	void *pg;
+	u64 *msg;
+
+	if ((gpa & (__alignof__(*msg) - 1)) ||
+	    offset_in_page(gpa) + sizeof(*msg) > PAGE_SIZE)
+		return HV_STATUS_INVALID_ALIGNMENT;
+

Don't we also need srcu_read_lock(&vcpu->kvm->srcu) for kvm_vcpu_gfn_to_page?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Seems so indeed. I also think I'll switch to using kvm_vcpu_read_guest().

Probably better (because I think you also missed releasing the page)

Hmm, I wonder where? (kvm_vcpu_read_guest ended up being more concise so I'll stick with it but I'd like to learn from my errors anyway.)

Nevermind, I screwed up my branches. The one I posted was missing kvm_release_page_clean() indeed.

Thanks,
Roman.