Skip to contentSkip to conversation

kvm-userspace: fix memslot assignment

PATCH In review Christian Borntraeger 3 participants 10 messages Jul 25, 2008 – Jul 31, 2008 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm-userspace: fix memslot assignment

Hello Avi,

seems that I mixed up the slot initialization, instead of making the first slot always 0 I made it always 1. Lets go back to Carstens variant, since I dont like nested ifdefs. The compiler will remove the dead code anyway.

---
 libkvm.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
libkvm.c +5 -2
Index: libkvm/libkvm.c
===================================================================
@@ -78,7 +78,7 @@ int get_free_slot(kvm_context_t kvm)
 	int i;
 	int tss_ext;
 
-#if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
+#if defined(KVM_CAP_SET_TSS_ADDR)
 	tss_ext = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
 #else
 	tss_ext = 0;
@@ -92,8 +92,11 @@ int get_free_slot(kvm_context_t kvm)
 	if (tss_ext > 0)
 		i = 0;
 	else
+#if !defined(__s390__)
 		i = 1;
-
+#else
+		i = 0;
+#endif
 	for (; i < KVM_MAX_NUM_MEM_REGIONS; ++i)
 		if (!slots[i].len)
 			return i;
#1 lore raw
Quoted 16 lines · Christian Borntraeger

Hello Avi,

seems that I mixed up the slot initialization, instead of making the first slot always 0 I made it always 1. Lets go back to Carstens variant, since I dont like nested ifdefs. The compiler will remove the dead code anyway.

---
 libkvm.c |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Index: libkvm/libkvm.c
===================================================================

libkvm.c int get_free_slot(kvm_context_t kvm) · line 84

Quoted 8 lines
libkvm.c +1 -1
@@ -78,7 +78,7 @@ int get_free_slot(kvm_context_t kvm)
 	int i;
 	int tss_ext;
 
-#if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
+#if defined(KVM_CAP_SET_TSS_ADDR)
 	tss_ext = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
 #else
 	tss_ext = 0;

This is really a no-op, since s390 will nack a KVM_CAP_SET_TSS_ADDR query. Of course, the change is an improvement.

libkvm.c int get_free_slot(kvm_context_t kvm) · line 100

Quoted 11 lines
libkvm.c +4 -1
@@ -92,8 +92,11 @@ int get_free_slot(kvm_context_t kvm)
 	if (tss_ext > 0)
 		i = 0;
 	else
+#if !defined(__s390__)
 		i = 1;
-
+#else
+		i = 0;
+#endif
 

ppc and ia64 also don't care about TSS_ADDR. Care to make this depend on x86, rather than on s390?

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw

Am Sonntag, 27. Juli 2008 schrieb Avi Kivity:

Quoted 17 lines

This is really a no-op, since s390 will nack a KVM_CAP_SET_TSS_ADDR query. Of course, the change is an improvement.

libkvm.c int get_free_slot(kvm_context_t kvm) · line 100

Quoted 11 lines
libkvm.c +4 -1
@@ -92,8 +92,11 @@ int get_free_slot(kvm_context_t kvm)
 	if (tss_ext > 0)
 		i = 0;
 	else
+#if !defined(__s390__)
 		i = 1;
-
+#else
+		i = 0;
+#endif
 

ppc and ia64 also don't care about TSS_ADDR. Care to make this depend on x86, rather than on s390?

Can do. Should ppc and ia64 start with i=1 or i=0, do they need a reserved slot?

Christian

#3 lore raw
Quoted 20 lines · Christian Borntraeger

Am Sonntag, 27. Juli 2008 schrieb Avi Kivity:

Quoted 17 lines

This is really a no-op, since s390 will nack a KVM_CAP_SET_TSS_ADDR query. Of course, the change is an improvement.

libkvm.c int get_free_slot(kvm_context_t kvm) · line 101

Quoted 12 lines
libkvm.c +4 -1
@@ -92,8 +92,11 @@ int get_free_slot(kvm_context_t kvm)
 	if (tss_ext > 0)
 		i = 0;
 	else
+#if !defined(__s390__)
 		i = 1;
-
+#else
+		i = 0;
+#endif
 
 

ppc and ia64 also don't care about TSS_ADDR. Care to make this depend on x86, rather than on s390?

Quoted 3 lines

Can do. Should ppc and ia64 start with i=1 or i=0, do they need a reserved slot?

They all start with 0 AFAIK (kvm also starts with 0, it just wants it to be a special slot).

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Am Dienstag, 29. Juli 2008 schrieb Avi Kivity:

Quoted 2 lines

They all start with 0 AFAIK (kvm also starts with 0, it just wants it to be a special slot).

Now my brain hurts....
Ok, so I read this as: ppc, ia64 and s390 can start with slot 0 and it is not a special slot. On x86 slot 0 is special, if
* KVM_CAP_SET_TSS_ADDR is not available
or
* The ioctl VM_CAP_SET_TSS_ADDR returns <=0

Since my opteron box died recently I cannot test that on x86 at the moment. What do you think about the following patch

---
 libkvm/libkvm.c |   28 ++++++++++++----------------
 1 file changed, 12 insertions(+), 16 deletions(-)
libkvm/libkvm.c +12 -16
Index: kvm-userspace/libkvm/libkvm.c
===================================================================
@@ -75,25 +75,21 @@ void init_slots(void)
 
 int get_free_slot(kvm_context_t kvm)
 {
-	int i;
-	int tss_ext;
-
-#if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
-	tss_ext = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
-#else
-	tss_ext = 0;
-#endif
+	int i = 0;
 
 	/*
-	 * on older kernels where the set tss ioctl is not supprted we must save
-	 * slot 0 to hold the extended memory, as the vmx will use the last 3
-	 * pages of this slot.
+	 * on older x86 kernels where the set tss ioctl is not supported we
+	 * must save slot 0 to hold the extended memory, as the vmx will
+	 * use the last 3 pages of this slot.
 	 */
-	if (tss_ext > 0)
-		i = 0;
-	else
-		i = 1;
-
+#if defined(__x86_64__) || defined(__i386__)
+#if defined(KVM_CAP_SET_TSS_ADDR)
+	if (ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR) > 0)
+		i++;
+#else
+	i++;
+#endif
+#endif
 	for (; i < KVM_MAX_NUM_MEM_REGIONS; ++i)
 		if (!slots[i].len)
 			return i;
#5 lore raw
Quoted 5 lines · Christian Borntraeger

Am Dienstag, 29. Juli 2008 schrieb Avi Kivity:

Quoted 2 lines

They all start with 0 AFAIK (kvm also starts with 0, it just wants it to be a special slot).

Quoted 8 lines

Now my brain hurts....
Ok, so I read this as: ppc, ia64 and s390 can start with slot 0 and it is not a special slot. On x86 slot 0 is special, if
* KVM_CAP_SET_TSS_ADDR is not available
or
* The ioctl VM_CAP_SET_TSS_ADDR returns <=0

I think this is right. Izik can you confirm?

libkvm/libkvm.c void init_slots(void) · line 87

Quoted 4 lines
libkvm/libkvm.c +3 -0
+#if defined(__x86_64__) || defined(__i386__)
+#if defined(KVM_CAP_SET_TSS_ADDR)
+	if (ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR) > 0)

Should be <= here?

libkvm/libkvm.c void init_slots(void) · line 92

Quoted 6 lines
libkvm/libkvm.c +5 -0
+		i++;
+#else
+	i++;
+#endif
+#endif

Suggest a helper, kvm_supports_set_tss_addr(), to reduce further braindamage.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
Quoted 15 lines · Avi Kivity
Quoted 5 lines · Christian Borntraeger

Am Dienstag, 29. Juli 2008 schrieb Avi Kivity:

Quoted 2 lines

They all start with 0 AFAIK (kvm also starts with 0, it just wants it to be a special slot).

Quoted 8 lines

Now my brain hurts....
Ok, so I read this as: ppc, ia64 and s390 can start with slot 0 and it is not a special slot. On x86 slot 0 is special, if
* KVM_CAP_SET_TSS_ADDR is not available
or
* The ioctl VM_CAP_SET_TSS_ADDR returns <=0

Quoted 1 line

I think this is right. Izik can you confirm?

yea kernels that dont have KVM_CAP_SET_TSS_ADDR should be treated like that.

Quoted 17 lines

libkvm/libkvm.c void init_slots(void) · line 87

Quoted 5 lines
libkvm/libkvm.c +3 -0
+#if defined(__x86_64__) || defined(__i386__)
+#if defined(KVM_CAP_SET_TSS_ADDR)
+    if (ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR) > 0)

Should be <= here?

libkvm/libkvm.c void init_slots(void) · line 92

Quoted 6 lines
libkvm/libkvm.c +5 -0
+        i++;
+#else
+    i++;
+#endif
+#endif

Suggest a helper, kvm_supports_set_tss_addr(), to reduce further braindamage.

#7 lore raw
In reply to Avi Kivity

Am Donnerstag, 31. Juli 2008 schrieb Avi Kivity:

Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +3 -0
+#if defined(__x86_64__) || defined(__i386__)
+#if defined(KVM_CAP_SET_TSS_ADDR)
+	if (ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR) > 0)

Should be <= here?

yes. But with the wrapper you suggest below its again >.

Quoted 10 lines

Review

Quoted 7 lines
(quoted diff) +5 -0
+		i++;
+#else
+	i++;
+#endif
+#endif

Suggest a helper, kvm_supports_set_tss_addr(), to reduce further braindamage.

Something like that? But then I get the following warning on non-x86:
libkvm.c:77: warning: 'kvm_supports_set_tss_addr' defined but not used

Should I mask the wrapper with config x86 as well?

---
 libkvm.c |   32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
libkvm.c +17 -15
Index: libkvm/libkvm.c
===================================================================
@@ -73,27 +73,29 @@ void init_slots(void)
 		slots[i].len = 0;
 }
 
+static int kvm_supports_set_tss_addr(kvm_context_t kvm)
+{
+#if defined(KVM_CAP_SET_TSS_ADDR)
+	if (ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR) > 0)
+		return 1;
+#endif
+	return 0;
+}
+
 int get_free_slot(kvm_context_t kvm)
 {
 	int i;
-	int tss_ext;
-
-#if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
-	tss_ext = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
-#else
-	tss_ext = 0;
-#endif
 
 	/*
-	 * on older kernels where the set tss ioctl is not supprted we must save
-	 * slot 0 to hold the extended memory, as the vmx will use the last 3
-	 * pages of this slot.
+	 * on older x86 kernels where the set tss ioctl is not supported we
+	 * must save slot 0 to hold the extended memory, as the vmx will
+	 * use the last 3 pages of this slot.
 	 */
-	if (tss_ext > 0)
-		i = 0;
-	else
-		i = 1;
-
+#if defined(__x86_64__) || defined(__i386__)
+	i = !kvm_supports_set_tss_addr(kvm);
+#else
+	i = 0;
+#endif
 	for (; i < KVM_MAX_NUM_MEM_REGIONS; ++i)
 		if (!slots[i].len)
 			return i;
#8 lore raw
Quoted 5 lines · Christian Borntraeger

Something like that? But then I get the following warning on non-x86:
libkvm.c:77: warning: 'kvm_supports_set_tss_addr' defined but not used

Should I mask the wrapper with config x86 as well?

If we rename the helper, kvm_wants_special_memslot_0() (reversing the meaning), we can have non-x86 return 0, x86 with KVM_CAP_SET_TSS_ADDR return 0, and otherwise return 1. No ifdefs outside the helper this way.

Quoted 5 lines
---
 libkvm.c |   32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)

(missing signoff)

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw

Am Donnerstag, 31. Juli 2008 schrieb Avi Kivity:

Quoted 3 lines

If we rename the helper, kvm_wants_special_memslot_0() (reversing the meaning), we can have non-x86 return 0, x86 with KVM_CAP_SET_TSS_ADDR return 0, and otherwise return 1. No ifdefs outside the helper this way.

Having two ifdefs is not going to be very pretty, but here is the latest version:

ppc, ia64 and s390 can start with slot 0 and it is not a special slot. On x86 slot 0 is special, if
* KVM_CAP_SET_TSS_ADDR is not available
or
* The ioctl VM_CAP_SET_TSS_ADDR returns <=0
Lets introduce a helper that checks if we need a special memslot.

---
 libkvm.c |   35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
libkvm.c +19 -16
Index: libkvm/libkvm.c
===================================================================
@@ -73,26 +73,29 @@ void init_slots(void)
 		slots[i].len = 0;
 }
 
-int get_free_slot(kvm_context_t kvm)
+static int kvm_wants_special_memslot_0(kvm_context_t kvm)
 {
-	int i;
-	int tss_ext;
-
-#if defined(KVM_CAP_SET_TSS_ADDR) && !defined(__s390__)
-	tss_ext = ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR);
+#if !defined(__x86_64__) && !defined(__i386__)
+	return 0;
 #else
-	tss_ext = 0;
-#endif
-
 	/*
-	 * on older kernels where the set tss ioctl is not supprted we must save
-	 * slot 0 to hold the extended memory, as the vmx will use the last 3
-	 * pages of this slot.
+	 * on older x86 kernels where the set tss ioctl is not supported we
+	 * must save slot 0 to hold the extended memory, as the vmx will
+	 * use the last 3 pages of this slot.
 	 */
-	if (tss_ext > 0)
-		i = 0;
-	else
-		i = 1;
+#if defined(KVM_CAP_SET_TSS_ADDR)
+	if (ioctl(kvm->fd, KVM_CHECK_EXTENSION, KVM_CAP_SET_TSS_ADDR) > 0)
+		return 0;
+#endif
+	return 1;
+#endif
+}
+
+int get_free_slot(kvm_context_t kvm)
+{
+	int i;
+
+	i = kvm_wants_special_memslot_0(kvm);
 
 	for (; i < KVM_MAX_NUM_MEM_REGIONS; ++i)
 		if (!slots[i].len)