Skip to contentSkip to conversation

KVM: Introduce ioeventfd read support

PATCH In review Amey Narkhede 3 participants 6 messages Oct 20, 2020 – Oct 31, 2020 KVM

lore
Share & tools
lore
#0 lore raw

cover KVM: Introduce ioeventfd read support

The first patch updates linux headers to add ioeventfd read support while the second patch can be used to test the ioeventfd read feature with kvm-unit-test which reads from specified guest addres. Make sure the address provided in kvm_set_ioeventfd_read matches with address in x86/ioeventfd_read test in kvm-unit-tests.

Amey Narkhede (2):
linux-headers: Add support for reads in ioeventfd
kvm: Add ioeventfd read test code

 accel/kvm/kvm-all.c       | 55 +++++++++++++++++++++++++++++++++++++++
 linux-headers/linux/kvm.h |  5 +++-
 2 files changed, 59 insertions(+), 1 deletion(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/2 linux-headers: Add support for reads in ioeventfd

This patch introduces a new flag KVM_IOEVENTFD_FLAG_DATAREAD in ioeventfd to enable receiving a notification when a guest reads from registered PIO/MMIO address.

---
 linux-headers/linux/kvm.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
linux-headers/linux/kvm.h +4 -1
index 43580c767c..3e71d15a53 100644
@@ -695,6 +695,7 @@ struct kvm_guest_debug {
 
 enum {
 	kvm_ioeventfd_flag_nr_datamatch,
+	kvm_ioeventfd_flag_nr_dataread,
 	kvm_ioeventfd_flag_nr_pio,
 	kvm_ioeventfd_flag_nr_deassign,
 	kvm_ioeventfd_flag_nr_virtio_ccw_notify,
@@ -703,6 +704,7 @@ enum {
 };
 
 #define KVM_IOEVENTFD_FLAG_DATAMATCH (1 << kvm_ioeventfd_flag_nr_datamatch)
+#define KVM_IOEVENTFD_FLAG_DATAREAD (1 << kvm_ioeventfd_flag_nr_dataread)
 #define KVM_IOEVENTFD_FLAG_PIO       (1 << kvm_ioeventfd_flag_nr_pio)
 #define KVM_IOEVENTFD_FLAG_DEASSIGN  (1 << kvm_ioeventfd_flag_nr_deassign)
 #define KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY \
@@ -712,11 +714,12 @@ enum {
 
 struct kvm_ioeventfd {
 	__u64 datamatch;
+	__u64 dataread;
 	__u64 addr;        /* legal pio/mmio address */
 	__u32 len;         /* 1, 2, 4, or 8 bytes; or 0 to ignore length */
 	__s32 fd;
 	__u32 flags;
-	__u8  pad[36];
+	__u8  pad[28];
 };
 
 #define KVM_X86_DISABLE_EXITS_MWAIT          (1 << 0)
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/2 kvm: Add ioeventfd read test code

In reply to Amey Narkhede

This patch adds kvm_set_ioeventfd_read and dummy_notifier_read functons to test ioeventfd read support. When the guess writes to address provided in kvm_set_ioeventfd_read function, dummy_notifier_read prints to stdio.

---
 accel/kvm/kvm-all.c | 55 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
accel/kvm/kvm-all.c +55 -0
index 9ef5daf4c5..357e74d84c 100644
@@ -1006,6 +1006,43 @@ static uint32_t adjust_ioeventfd_endianness(uint32_t val, uint32_t size)
     return val;
 }
 
+static void dummy_notifier_read(EventNotifier *n)
+{
+    printf("Received ioeventfd read event\n");
+    event_notifier_test_and_clear(n);
+}
+
+static int kvm_set_ioeventfd_read(int fd, hwaddr addr, uint64_t val,
+				  uint64_t size, bool datamatch)
+{
+	int ret;
+	struct kvm_ioeventfd ioevent = {
+        	.datamatch = datamatch ? adjust_ioeventfd_endianness(val, size) : 0,
+		.dataread = val,
+		.addr = addr,
+		.len = size,
+		.flags = KVM_IOEVENTFD_FLAG_DATAREAD,
+		.fd = fd,
+	};
+
+	if (!kvm_enabled()) {
+		return -ENOSYS;
+	}
+
+	if (datamatch) {
+            ioevent.flags |= KVM_IOEVENTFD_FLAG_DATAMATCH;
+	}
+
+	ret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &ioevent);
+
+	if (ret < 0) {
+		return -errno;
+	}
+
+	return 0;
+}
+
+
 static int kvm_set_ioeventfd_mmio(int fd, hwaddr addr, uint32_t val,
                                   bool assign, uint32_t size, bool datamatch)
 {
@@ -2012,6 +2049,7 @@ static int kvm_init(MachineState *ms)
     KVMState *s;
     const KVMCapabilityInfo *missing_cap;
     int ret;
+    int efd = -1;
     int type = 0;
     const char *kvm_type;
     uint64_t dirty_log_manual_caps;
@@ -2253,6 +2291,22 @@ static int kvm_init(MachineState *ms)
     }
 
     cpus_register_accel(&kvm_cpus);
+
+    EventNotifier *e = g_malloc0(sizeof(EventNotifier));
+    ret = event_notifier_init(e, false);
+    if (ret < 0) {
+	printf("Failed to initialize EventNotifier\n");
+    }
+    else {
+        AioContext *ctx = qemu_get_aio_context();
+        efd = event_notifier_get_fd(e);
+        aio_set_event_notifier(ctx, e, false, dummy_notifier_read, NULL);
+        ret = kvm_set_ioeventfd_read(efd, 0xff01003f, 123, 8, false);
+        if (ret < 0)
+            printf("ioeventfd read failed\n");
+    }
+
+
     return 0;
 
 err:
@@ -2268,6 +2322,7 @@ err:
     return ret;
 }
 
+
 void kvm_set_sigmask_len(KVMState *s, unsigned int sigmask_len)
 {
     s->sigmask_len = sigmask_len;
Signature · 1 line
2.28.0
#3 lore raw
In reply to Amey Narkhede
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [PATCH 0/2] KVM: Introduce ioeventfd read support

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
- [tag update] patchew/[email protected] -> patchew/[email protected]
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
eccca33 kvm: Add ioeventfd read test code 33b3de6 linux-headers: Add support for reads in ioeventfd

=== OUTPUT BEGIN ===
1/2 Checking commit 33b3de672219 (linux-headers: Add support for reads in ioeventfd) 2/2 Checking commit eccca33098c6 (kvm: Add ioeventfd read test code)
ERROR: code indent should never use tabs
#31: FILE: accel/kvm/kvm-all.c:1016:
+^I^I^I^I uint64_t size, bool datamatch)$

ERROR: code indent should never use tabs
#33: FILE: accel/kvm/kvm-all.c:1018:
+^Iint ret;$

ERROR: code indent should never use tabs
#34: FILE: accel/kvm/kvm-all.c:1019:
+^Istruct kvm_ioeventfd ioevent = {$

WARNING: line over 80 characters
#35: FILE: accel/kvm/kvm-all.c:1020:
+ .datamatch = datamatch ? adjust_ioeventfd_endianness(val, size) : 0,

ERROR: code indent should never use tabs
#35: FILE: accel/kvm/kvm-all.c:1020:
+ ^I.datamatch = datamatch ? adjust_ioeventfd_endianness(val, size) : 0,$

ERROR: code indent should never use tabs
#36: FILE: accel/kvm/kvm-all.c:1021:
+^I^I.dataread = val,$

ERROR: code indent should never use tabs
#37: FILE: accel/kvm/kvm-all.c:1022:
+^I^I.addr = addr,$

ERROR: code indent should never use tabs
#38: FILE: accel/kvm/kvm-all.c:1023:
+^I^I.len = size,$

ERROR: code indent should never use tabs
#39: FILE: accel/kvm/kvm-all.c:1024:
+^I^I.flags = KVM_IOEVENTFD_FLAG_DATAREAD,$

ERROR: code indent should never use tabs
#40: FILE: accel/kvm/kvm-all.c:1025:
+^I^I.fd = fd,$

ERROR: code indent should never use tabs
#41: FILE: accel/kvm/kvm-all.c:1026:
+^I};$

ERROR: code indent should never use tabs
#43: FILE: accel/kvm/kvm-all.c:1028:
+^Iif (!kvm_enabled()) {$

ERROR: code indent should never use tabs
#44: FILE: accel/kvm/kvm-all.c:1029:
+^I^Ireturn -ENOSYS;$

ERROR: code indent should never use tabs
#45: FILE: accel/kvm/kvm-all.c:1030:
+^I}$

ERROR: code indent should never use tabs
#47: FILE: accel/kvm/kvm-all.c:1032:
+^Iif (datamatch) {$

ERROR: code indent should never use tabs
#49: FILE: accel/kvm/kvm-all.c:1034:
+^I}$

ERROR: code indent should never use tabs
#51: FILE: accel/kvm/kvm-all.c:1036:
+^Iret = kvm_vm_ioctl(kvm_state, KVM_IOEVENTFD, &ioevent);$

ERROR: code indent should never use tabs
#53: FILE: accel/kvm/kvm-all.c:1038:
+^Iif (ret < 0) {$

ERROR: code indent should never use tabs
#54: FILE: accel/kvm/kvm-all.c:1039:
+^I^Ireturn -errno;$

ERROR: code indent should never use tabs
#55: FILE: accel/kvm/kvm-all.c:1040:
+^I}$

ERROR: code indent should never use tabs
#57: FILE: accel/kvm/kvm-all.c:1042:
+^Ireturn 0;$

ERROR: code indent should never use tabs #80: FILE: accel/kvm/kvm-all.c:2298:
+^Iprintf("Failed to initialize EventNotifier\n");$

ERROR: else should follow close brace '}'
#82: FILE: accel/kvm/kvm-all.c:2300:
+ }
+ else {

ERROR: braces {} are necessary for all arms of this statement #87: FILE: accel/kvm/kvm-all.c:2305:
+ if (ret < 0)
[...]

total: 23 errors, 1 warnings, 79 lines checked

Patch 2/2 has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

=== OUTPUT END ===

Test command exited with code: 1

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#4 lore raw
In reply to Amey Narkhede

On 20/10/20 10:30PM, Amey Narkhede wrote:

Please ignore this mail. I meant to send this mail to stefan but due to me being careless I forgot that I previously configured git-publish for qemu and didn't check the final message before hitting send.

Quoted 20 lines

The first patch updates linux headers to add ioeventfd read support while the second patch can be used to test the ioeventfd read feature with kvm-unit-test which reads from specified guest addres. Make sure the address provided in kvm_set_ioeventfd_read matches with address in x86/ioeventfd_read test in kvm-unit-tests.

Amey Narkhede (2):
linux-headers: Add support for reads in ioeventfd
kvm: Add ioeventfd read test code

 accel/kvm/kvm-all.c       | 55 +++++++++++++++++++++++++++++++++++++++
 linux-headers/linux/kvm.h |  5 +++-
 2 files changed, 59 insertions(+), 1 deletion(-)

--
2.28.0

maintainer
#5 lore raw
In reply to Amey Narkhede
Quoted 9 lines · Amey Narkhede

The first patch updates linux headers to add ioeventfd read support while the second patch can be used to test the ioeventfd read feature with kvm-unit-test which reads from specified guest addres. Make sure the address provided in kvm_set_ioeventfd_read matches with address in x86/ioeventfd_read test in kvm-unit-tests.

Quoted 8 lines

Amey Narkhede (2):
linux-headers: Add support for reads in ioeventfd
kvm: Add ioeventfd read test code

 accel/kvm/kvm-all.c       | 55 +++++++++++++++++++++++++++++++++++++++
 linux-headers/linux/kvm.h |  5 +++-
 2 files changed, 59 insertions(+), 1 deletion(-)

Hi,

in what occasions is this useful?

Paolo