Skip to contentSkip to conversation

kvm tools: Add kvm__has_cap() to check whether a cap is available on the host

PATCH In review Sasha Levin 2 participants 4 messages Dec 14, 2011 – Dec 15, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 kvm tools: Add kvm__has_cap() to check whether a cap is available on the host

---
 tools/kvm/include/kvm/kvm.h |    2 ++
 tools/kvm/kvm.c             |    5 +++++
 2 files changed, 7 insertions(+), 0 deletions(-)
tools/kvm/include/kvm/kvm.h +2 -0
index 7159952..d24b70a 100644
@@ -79,4 +79,6 @@ static inline void *guest_flat_to_host(struct kvm *kvm, unsigned long offset)
 	return kvm->ram_start + offset;
 }
 
+bool kvm__has_cap(struct kvm *kvm, u32 cap);
+
 #endif /* KVM__KVM_H */
tools/kvm/kvm.c +5 -0
index 35ca2c5..a2f7a89 100644
@@ -517,3 +517,8 @@ void kvm__notify_paused(void)
 	mutex_lock(&pause_lock);
 	mutex_unlock(&pause_lock);
 }
+
+bool kvm__has_cap(struct kvm *kvm, u32 cap)
+{
+	return ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, cap) == 0;
+}
Signature · 1 line
1.7.8
#1 lore raw

PATCH 2/2 kvm tools: Don't use ioeventfds if no KVM_CAP_IOEVENTFD

Check KVM_CAP_IOEVENTFD before using ioeventfds.

---
 tools/kvm/builtin-run.c           |    2 +-
 tools/kvm/include/kvm/ioeventfd.h |    2 +-
 tools/kvm/ioeventfd.c             |   16 +++++++++++++++-
 3 files changed, 17 insertions(+), 3 deletions(-)
tools/kvm/builtin-run.c +1 -1
index 76f1a8c..47e4ea8 100644
@@ -932,7 +932,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	kvm->single_step = single_step;
 
-	ioeventfd__init();
+	ioeventfd__init(kvm);
 
 	max_cpus = kvm__max_cpus(kvm);
 	recommended_cpus = kvm__recommended_cpus(kvm);
tools/kvm/include/kvm/ioeventfd.h +1 -1
index df01750..3a95788 100644
@@ -19,7 +19,7 @@ struct ioevent {
 	struct list_head	list;
 };
 
-void ioeventfd__init(void);
+void ioeventfd__init(struct kvm *kvm);
 void ioeventfd__start(void);
 void ioeventfd__add_event(struct ioevent *ioevent);
 void ioeventfd__del_event(u64 addr, u64 datamatch);
tools/kvm/ioeventfd.c +15 -1
index 3a240e4..75dd3f2 100644
@@ -18,9 +18,14 @@
 static struct	epoll_event events[IOEVENTFD_MAX_EVENTS];
 static int	epoll_fd;
 static LIST_HEAD(used_ioevents);
+static bool	ioeventfd_avail;
 
-void ioeventfd__init(void)
+void ioeventfd__init(struct kvm *kvm)
 {
+	ioeventfd_avail = kvm__has_cap(kvm, KVM_CAP_IOEVENTFD);
+	if (!ioeventfd_avail)
+		return;
+
 	epoll_fd = epoll_create(IOEVENTFD_MAX_EVENTS);
 	if (epoll_fd < 0)
 		die("Failed creating epoll fd");
@@ -33,6 +38,9 @@ void ioeventfd__add_event(struct ioevent *ioevent)
 	struct ioevent *new_ioevent;
 	int event;
 
+	if (!ioeventfd_avail)
+		return;
+
 	new_ioevent = malloc(sizeof(*new_ioevent));
 	if (new_ioevent == NULL)
 		die("Failed allocating memory for new ioevent");
@@ -68,6 +76,9 @@ void ioeventfd__del_event(u64 addr, u64 datamatch)
 	struct ioevent *ioevent;
 	u8 found = 0;
 
+	if (!ioeventfd_avail)
+		return;
+
 	list_for_each_entry(ioevent, &used_ioevents, list) {
 		if (ioevent->io_addr == addr) {
 			found = 1;
@@ -123,6 +134,9 @@ void ioeventfd__start(void)
 {
 	pthread_t thread;
 
+	if (!ioeventfd_avail)
+		return;
+
 	if (pthread_create(&thread, NULL, ioeventfd__thread, NULL) != 0)
 		die("Failed starting ioeventfd thread");
 }
Signature · 1 line
1.7.8
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] kvm tools: Don't use ioeventfds if no KVM_CAP_IOEVENTFD

Quoted 3 lines · Sasha Levin

Check KVM_CAP_IOEVENTFD before using ioeventfds.

A much nicer solution than my "[PATCH V2 2/2] kvm tools: Make virtio-pci's ioeventfd__add_event() fall back gracefully if ioeventfds unavailable".

matt.nastyhacks--;

Quoted 82 lines
---
 tools/kvm/builtin-run.c           |    2 +-
 tools/kvm/include/kvm/ioeventfd.h |    2 +-
 tools/kvm/ioeventfd.c             |   16 +++++++++++++++-
 3 files changed, 17 insertions(+), 3 deletions(-)
tools/kvm/builtin-run.c +1 -1
index 76f1a8c..47e4ea8 100644
@@ -932,7 +932,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 
 	kvm->single_step = single_step;
 
-	ioeventfd__init();
+	ioeventfd__init(kvm);
 
 	max_cpus = kvm__max_cpus(kvm);
 	recommended_cpus = kvm__recommended_cpus(kvm);
tools/kvm/include/kvm/ioeventfd.h +1 -1
index df01750..3a95788 100644
@@ -19,7 +19,7 @@ struct ioevent {
 	struct list_head	list;
 };
 
-void ioeventfd__init(void);
+void ioeventfd__init(struct kvm *kvm);
 void ioeventfd__start(void);
 void ioeventfd__add_event(struct ioevent *ioevent);
 void ioeventfd__del_event(u64 addr, u64 datamatch);
tools/kvm/ioeventfd.c +15 -1
index 3a240e4..75dd3f2 100644
@@ -18,9 +18,14 @@
 static struct	epoll_event events[IOEVENTFD_MAX_EVENTS];
 static int	epoll_fd;
 static LIST_HEAD(used_ioevents);
+static bool	ioeventfd_avail;
 
-void ioeventfd__init(void)
+void ioeventfd__init(struct kvm *kvm)
 {
+	ioeventfd_avail = kvm__has_cap(kvm, KVM_CAP_IOEVENTFD);
+	if (!ioeventfd_avail)
+		return;
+
 	epoll_fd = epoll_create(IOEVENTFD_MAX_EVENTS);
 	if (epoll_fd < 0)
 		die("Failed creating epoll fd");
@@ -33,6 +38,9 @@ void ioeventfd__add_event(struct ioevent *ioevent)
 	struct ioevent *new_ioevent;
 	int event;
 
+	if (!ioeventfd_avail)
+		return;
+
 	new_ioevent = malloc(sizeof(*new_ioevent));
 	if (new_ioevent == NULL)
 		die("Failed allocating memory for new ioevent");
@@ -68,6 +76,9 @@ void ioeventfd__del_event(u64 addr, u64 datamatch)
 	struct ioevent *ioevent;
 	u8 found = 0;
 
+	if (!ioeventfd_avail)
+		return;
+
 	list_for_each_entry(ioevent, &used_ioevents, list) {
 		if (ioevent->io_addr == addr) {
 			found = 1;
@@ -123,6 +134,9 @@ void ioeventfd__start(void)
 {
 	pthread_t thread;
 
+	if (!ioeventfd_avail)
+		return;
+
 	if (pthread_create(&thread, NULL, ioeventfd__thread, NULL) != 0)
 		die("Failed starting ioeventfd thread");
 }
#3 lore raw
In reply to Sasha Levin
Quoted 22 lines · Sasha Levin
---
 tools/kvm/include/kvm/kvm.h |    2 ++
 tools/kvm/kvm.c             |    5 +++++
 2 files changed, 7 insertions(+), 0 deletions(-)
tools/kvm/include/kvm/kvm.h +2 -0
index 7159952..d24b70a 100644
@@ -79,4 +79,6 @@ static inline void *guest_flat_to_host(struct kvm *kvm, unsigned long offset)
 	return kvm->ram_start + offset;
 }
 
+bool kvm__has_cap(struct kvm *kvm, u32 cap);
+
 #endif /* KVM__KVM_H */
tools/kvm/kvm.c +0 -0
index 35ca2c5..a2f7a89 100644

tools/kvm/kvm.c void kvm__notify_paused(void) · line 524

Quoted 8 lines
tools/kvm/kvm.c +5 -0
@@ -517,3 +517,8 @@ void kvm__notify_paused(void)
 	mutex_lock(&pause_lock);
 	mutex_unlock(&pause_lock);
 }
+
+bool kvm__has_cap(struct kvm *kvm, u32 cap)
+{
+	return ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, cap) == 0;
+}
                                                            ^^^^

D'oh, this needs to be != 0, not == 0.

Cheers,

Matt

---

From: Matt Evans <[email protected]>
Date: Thu, 15 Dec 2011 15:19:47 +1100
Subject: [PATCH] kvm tools: Fix inverted logic bug in kvm__has_cap

Commit 42efb1abf4ebebeedd14af34c073e673923e2898 compared KVM_CHECK_EXTENSION's result wrong, stating 'has cap' true if 0. Invert the comparison.

---
 tools/kvm/kvm.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
tools/kvm/kvm.c +1 -1
index 9583ab2..25f1419 100644
@@ -523,5 +523,5 @@ void kvm__notify_paused(void)
 
 bool kvm__has_cap(struct kvm *kvm, u32 cap)
 {
-	return ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, cap) == 0;
+	return ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, cap) != 0;
 }
Signature · 1 line
1.7.0.4