Skip to contentSkip to conversation

kvm tools: add signal placeholder to avoid balloon crash

PATCH In review Liming Wang 3 participants 12 messages Aug 11, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 kvm tools: add signal placeholder to avoid balloon crash

If "kvm run" without balloon option, use "kvm balloon" may crash kvm. Add signal placeholder to avoid this.

---
 tools/kvm/builtin-run.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +7 -0
index fa5de27..d725834 100644
@@ -280,6 +280,10 @@ static void handle_sigstop(int sig)
 	kvm_cpu__reboot();
 }
 
+static void handle_empty(int sig)
+{
+}
+
 static void *kvm_cpu_thread(void *arg)
 {
 	current_kvm_cpu		= arg;
@@ -469,6 +473,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	signal(SIGUSR2, handle_sigusr2);
 	signal(SIGKVMSTOP, handle_sigstop);
 	signal(SIGKVMRESUME, handle_sigusr2);
+	/* signal placeholder if not enable balloon */
+	signal(SIGKVMADDMEM, handle_empty);
+	signal(SIGKVMDELMEM, handle_empty);
 
 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
Signature · 1 line
1.7.0.4
#1 lore raw

PATCH 2/3 kvm tools: check negative value of num_pages

If num_pages is negative, balloon will make kernel crash with "out of memory". So we check this value to avoid it to be negative.

---
 tools/kvm/virtio/balloon.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
tools/kvm/virtio/balloon.c +6 -1
index 854d04b..0223ee4 100644
@@ -222,8 +222,13 @@ static void handle_sigmem(int sig)
 {
 	if (sig == SIGKVMADDMEM)
 		bdev.config.num_pages += 256;
-	else
+	else {
 		bdev.config.num_pages -= 256;
+		if ((s32)bdev.config.num_pages < 0){
+			bdev.config.num_pages = 0;
+			return;
+		}
+	}
 
 	/* Notify that the configuration space has changed */
 	bdev.isr = VIRTIO_PCI_ISR_CONFIG;
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [PATCH 2/3] kvm tools: check negative value of num_pages

Quoted 13 lines · Liming Wang

If num_pages is negative, balloon will make kernel crash with "out of memory". So we check this value to avoid it to be negative.

---
 tools/kvm/virtio/balloon.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
tools/kvm/virtio/balloon.c +0 -0
index 854d04b..0223ee4 100644

tools/kvm/virtio/balloon.c static void handle_sigmem(int sig) · line 227

Quoted 7 lines
tools/kvm/virtio/balloon.c +2 -1
@@ -222,8 +222,13 @@ static void handle_sigmem(int sig)
 {
 	if (sig == SIGKVMADDMEM)
 		bdev.config.num_pages += 256;
-	else
+	else {
 		bdev.config.num_pages -= 256;
+		if ((s32)bdev.config.num_pages < 0){

imo it's worth doing this check before the decrement instead of casting to signed here.

you also need to wrap the 'if ()' with parenthesis if you add them to the 'else' case.

Quoted 7 lines
tools/kvm/virtio/balloon.c +4 -0
+			bdev.config.num_pages = 0;
+			return;
+		}
+	}
 
 	/* Notify that the configuration space has changed */
 	bdev.isr = VIRTIO_PCI_ISR_CONFIG;
Signature · 1 line
Sasha.
author
#3 lore raw

Re: [PATCH 2/3] kvm tools: check negative value of num_pages

Quoted 9 lines · Sasha Levin
Quoted 8 lines · Liming Wang

If num_pages is negative, balloon will make kernel crash with "out of memory". So we check this value to avoid it to be negative.

---
 tools/kvm/virtio/balloon.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
Quoted 15 lines

tools/kvm/virtio/balloon.c static void handle_sigmem(int sig) · line 227

Quoted 12 lines
tools/kvm/virtio/balloon.c +2 -1
index 854d04b..0223ee4 100644
@@ -222,8 +222,13 @@ static void handle_sigmem(int sig)
 {
 	if (sig == SIGKVMADDMEM)
 		bdev.config.num_pages += 256;
-	else
+	else {
 		bdev.config.num_pages -= 256;
+		if ((s32)bdev.config.num_pages < 0){

imo it's worth doing this check before the decrement instead of casting to signed here.

You mean that check whether num_pages less than 256 or equal to 0?

Quoted 3 lines

you also need to wrap the 'if ()' with parenthesis if you add them to the 'else' case.

Sorry, I'm not clear what that does mean?

walimis

Quoted 13 lines
Quoted 8 lines
tools/kvm/virtio/balloon.c +4 -0
+			bdev.config.num_pages = 0;
+			return;
+		}
+	}
 
 	/* Notify that the configuration space has changed */
 	bdev.isr = VIRTIO_PCI_ISR_CONFIG;
Signature · 1 line
Sasha.
#4 lore raw

Re: [PATCH 2/3] kvm tools: check negative value of num_pages

Quoted 25 lines · walimis
Quoted 9 lines · Sasha Levin
Quoted 8 lines · Liming Wang

If num_pages is negative, balloon will make kernel crash with "out of memory". So we check this value to avoid it to be negative.

---
 tools/kvm/virtio/balloon.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
Quoted 15 lines

tools/kvm/virtio/balloon.c static void handle_sigmem(int sig) · line 227

Quoted 12 lines
tools/kvm/virtio/balloon.c +2 -1
index 854d04b..0223ee4 100644
@@ -222,8 +222,13 @@ static void handle_sigmem(int sig)
 {
 	if (sig == SIGKVMADDMEM)
 		bdev.config.num_pages += 256;
-	else
+	else {
 		bdev.config.num_pages -= 256;
+		if ((s32)bdev.config.num_pages < 0){

imo it's worth doing this check before the decrement instead of casting to signed here.

Quoted 2 lines

You mean that check whether num_pages less than 256 or equal to 0?

If it's less than 256.

Quoted 5 lines
Quoted 3 lines

you also need to wrap the 'if ()' with parenthesis if you add them to the 'else' case.

Sorry, I'm not clear what that does mean?

It's a coding style thing. If you have braces in one branch you should have them in all branches.

For example:

if (something)

	do_this();
else {
	do_this();
	do_that();
}

should be:

if (something) {
	do_this();
} else {
	do_this();
	do_that();
}

We follow the kernel coding style, you can find the full documentation under the kernel tree in Documentation/CodingStyle .

Quoted 14 lines

walimis

Quoted 13 lines
Quoted 8 lines
tools/kvm/virtio/balloon.c +4 -0
+			bdev.config.num_pages = 0;
+			return;
+		}
+	}
 
 	/* Notify that the configuration space has changed */
 	bdev.isr = VIRTIO_PCI_ISR_CONFIG;
Signature · 1 line
Sasha.
Signature · 1 line
Sasha.
#5 lore raw

PATCH 2/3 kvm tools: check negative value of num_pages

If num_pages is negative, balloon will make kernel crash with "out of memory". So we check this value to avoid it to be negative.

---
 tools/kvm/virtio/balloon.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
tools/kvm/virtio/balloon.c +6 -2
index 854d04b..941023e 100644
@@ -220,10 +220,14 @@ static struct ioport_operations virtio_bln_io_ops = {
 
 static void handle_sigmem(int sig)
 {
-	if (sig == SIGKVMADDMEM)
+	if (sig == SIGKVMADDMEM) {
 		bdev.config.num_pages += 256;
-	else
+	} else {
+		if (bdev.config.num_pages < 256){
+			return;
+		}
 		bdev.config.num_pages -= 256;
+	}
 
 	/* Notify that the configuration space has changed */
 	bdev.isr = VIRTIO_PCI_ISR_CONFIG;
Signature · 1 line
1.7.0.4
#6 lore raw

Re: [PATCH 2/3] kvm tools: check negative value of num_pages

Quoted 13 lines · Liming Wang

If num_pages is negative, balloon will make kernel crash with "out of memory". So we check this value to avoid it to be negative.

---
tools/kvm/virtio/balloon.c | 8 ++++++--

1 files changed, 6 insertions(+), 2 deletions(-)
tools/kvm/virtio/balloon.c +0 -0
index 854d04b..941023e 100644

tools/kvm/virtio/balloon.c static struct ioport_operations virtio_bln_io_ops = { · line 226

Quoted 9 lines
tools/kvm/virtio/balloon.c +3 -2
@@ -220,10 +220,14 @@ static struct ioport_operations virtio_bln_io_ops = {
 
 static void handle_sigmem(int sig)
 {
-	if (sig == SIGKVMADDMEM)
+	if (sig == SIGKVMADDMEM) {
 		bdev.config.num_pages += 256;
-	else
+	} else {
+		if (bdev.config.num_pages < 256){

Space missing before opening brace.

tools/kvm/virtio/balloon.c static struct ioport_operations virtio_bln_io_ops = { · line 228

Quoted 2 lines
tools/kvm/virtio/balloon.c +2 -0
+			return;
+		}

Please don't use braces for single line if statements.

I'll fix it up myself. You can use the scripts/checkpatch script to check for this kind of trivial coding style issues before sending a patch out.

Quoted 9 lines
tools/kvm/virtio/balloon.c +1 -0
 		bdev.config.num_pages -= 256;
+	}
 
 	/* Notify that the configuration space has changed */
 	bdev.isr = VIRTIO_PCI_ISR_CONFIG;
Signature · 1 line
1.7.0.4
author
#7 lore raw

Re: [PATCH 2/3] kvm tools: check negative value of num_pages

Quoted 26 lines · Pekka Enberg
Quoted 13 lines · Liming Wang

If num_pages is negative, balloon will make kernel crash with "out of memory". So we check this value to avoid it to be negative.

---
tools/kvm/virtio/balloon.c | 8 ++++++--

1 files changed, 6 insertions(+), 2 deletions(-)
tools/kvm/virtio/balloon.c +0 -0
index 854d04b..941023e 100644

tools/kvm/virtio/balloon.c static struct ioport_operations virtio_bln_io_ops = { · line 226

Quoted 9 lines
tools/kvm/virtio/balloon.c +3 -2
@@ -220,10 +220,14 @@ static struct ioport_operations virtio_bln_io_ops = {
 
 static void handle_sigmem(int sig)
 {
-	if (sig == SIGKVMADDMEM)
+	if (sig == SIGKVMADDMEM) {
 		bdev.config.num_pages += 256;
-	else
+	} else {
+		if (bdev.config.num_pages < 256){

Space missing before opening brace.

Quoted 8 lines

tools/kvm/virtio/balloon.c static struct ioport_operations virtio_bln_io_ops = { · line 228

Quoted 2 lines
tools/kvm/virtio/balloon.c +2 -0
+			return;
+		}

Please don't use braces for single line if statements.

I'll fix it up myself. You can use the scripts/checkpatch script to check for this kind of trivial coding style issues before sending a patch out.

OK, I will take note next time.

walimis

Quoted 10 lines
Quoted 10 lines
tools/kvm/virtio/balloon.c +1 -0
 		bdev.config.num_pages -= 256;
+	}
 
 	/* Notify that the configuration space has changed */
 	bdev.isr = VIRTIO_PCI_ISR_CONFIG;
Signature · 1 line
1.7.0.4
#8 lore raw

PATCH 3/3 kvm tools: enable keyboard press repeat for sdl

In reply to Liming Wang

Set keyboard repeat rate to enable keyboard press repeat.

It means that don't repeat the key value every 50 milliseconds until 200 milliseconds later when the key is pressed.

---
 tools/kvm/ui/sdl.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
tools/kvm/ui/sdl.c +2 -0
index 088cd29..6320ce7 100644
@@ -99,6 +99,8 @@ static void *sdl__thread(void *p)
 	if (!screen)
 		die("Unable to set SDL video mode");
 
+	SDL_EnableKeyRepeat(200, 50);
+
 	for (;;) {
 		SDL_BlitSurface(guest_screen, NULL, screen, NULL);
 		SDL_Flip(screen);
Signature · 1 line
1.7.0.4
#9 lore raw
In reply to Liming Wang
Quoted 24 lines · Liming Wang

If "kvm run" without balloon option, use "kvm balloon" may crash kvm. Add signal placeholder to avoid this.

---
 tools/kvm/builtin-run.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +4 -0
index fa5de27..d725834 100644
@@ -280,6 +280,10 @@ static void handle_sigstop(int sig)
 	kvm_cpu__reboot();
 }
 
+static void handle_empty(int sig)
+{
+}
+
 static void *kvm_cpu_thread(void *arg)
 {
 	current_kvm_cpu		= arg;

tools/kvm/builtin-run.c int kvm_cmd_run(int argc, const char **argv, const char *prefix) · line 477

Quoted 5 lines
tools/kvm/builtin-run.c +2 -0
@@ -469,6 +473,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	signal(SIGUSR2, handle_sigusr2);
 	signal(SIGKVMSTOP, handle_sigstop);
 	signal(SIGKVMRESUME, handle_sigusr2);
+	/* signal placeholder if not enable balloon */
+	signal(SIGKVMADDMEM, handle_empty);

You can just do this:

	signal(SIGKVMADDMEM, SIG_IGN);

For both, and avoid the empty handler.

Quoted 4 lines
tools/kvm/builtin-run.c +1 -0
+	signal(SIGKVMDELMEM, handle_empty);
 
 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
Signature · 1 line
Sasha.
author
#10 lore raw
Quoted 32 lines · Sasha Levin
Quoted 24 lines · Liming Wang

If "kvm run" without balloon option, use "kvm balloon" may crash kvm. Add signal placeholder to avoid this.

---
 tools/kvm/builtin-run.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +4 -0
index fa5de27..d725834 100644
@@ -280,6 +280,10 @@ static void handle_sigstop(int sig)
 	kvm_cpu__reboot();
 }
 
+static void handle_empty(int sig)
+{
+}
+
 static void *kvm_cpu_thread(void *arg)
 {
 	current_kvm_cpu		= arg;

tools/kvm/builtin-run.c int kvm_cmd_run(int argc, const char **argv, const char *prefix) · line 477

Quoted 5 lines
tools/kvm/builtin-run.c +2 -0
@@ -469,6 +473,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	signal(SIGUSR2, handle_sigusr2);
 	signal(SIGKVMSTOP, handle_sigstop);
 	signal(SIGKVMRESUME, handle_sigusr2);
+	/* signal placeholder if not enable balloon */
+	signal(SIGKVMADDMEM, handle_empty);

You can just do this:

Quoted 3 lines
	signal(SIGKVMADDMEM, SIG_IGN);

For both, and avoid the empty handler.

good. New patch will be sent.

walimis

Quoted 10 lines
Quoted 5 lines
tools/kvm/builtin-run.c +1 -0
+	signal(SIGKVMDELMEM, handle_empty);
 
 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
Signature · 1 line
Sasha.
#11 lore raw

PATCH 1/3 kvm tools: ignore balloon signals by default to avoid balloon crash

In reply to Sasha Levin

If "kvm run" without balloon option, use "kvm balloon" may crash kvm. So ignore balloon signals by default to avoid this.

---
 tools/kvm/builtin-run.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +3 -0
index fa5de27..1ab7122 100644
@@ -469,6 +469,9 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
 	signal(SIGUSR2, handle_sigusr2);
 	signal(SIGKVMSTOP, handle_sigstop);
 	signal(SIGKVMRESUME, handle_sigusr2);
+	/* ignore balloon signal by default if not enable balloon optiion */
+	signal(SIGKVMADDMEM, SIG_IGN);
+	signal(SIGKVMDELMEM, SIG_IGN);
 
 	nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
 
Signature · 1 line
1.7.0.4