Skip to contentSkip to conversation

kvm tools: Fix segfault when failing to initialize KVM

PATCH In review Sasha Levin 3 participants 6 messages Feb 1, 2012 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm tools: Fix segfault when failing to initialize KVM

Might happen when hardware virtualization is not supported.

---
 tools/kvm/builtin-run.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +4 -0
index 6ded1d2..a67faf8 100644
@@ -997,6 +997,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 	}
 
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR_OR_NULL(kvm)) {
+		r = PTR_ERR(kvm);
+		goto fail;
+	}
 
 	kvm->single_step = single_step;
 
Signature · 1 line
1.7.8.4
#1 lore raw
Quoted 13 lines · Sasha Levin

Might happen when hardware virtualization is not supported.

---
tools/kvm/builtin-run.c | 4 ++++

1 files changed, 4 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +0 -0
index 6ded1d2..a67faf8 100644

tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, const char **argv) · line 1001

Quoted 5 lines
tools/kvm/builtin-run.c +2 -0
@@ -997,6 +997,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 	}
 
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR_OR_NULL(kvm)) {
+		r = PTR_ERR(kvm);

How is this going to work when 'kvm' is NULL? It'd be best if kvm_init() never returned NULL on error.

Quoted 9 lines
tools/kvm/builtin-run.c +2 -0
+		goto fail;
+	}
 
 	kvm->single_step = single_step;
 
Signature · 1 line
1.7.8.4
#2 lore raw
Quoted 23 lines · Pekka Enberg
Quoted 13 lines · Sasha Levin

Might happen when hardware virtualization is not supported.

---
tools/kvm/builtin-run.c | 4 ++++

1 files changed, 4 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +0 -0
index 6ded1d2..a67faf8 100644

tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, const char **argv) · line 1001

Quoted 5 lines
tools/kvm/builtin-run.c +2 -0
@@ -997,6 +997,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 	}
 
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR_OR_NULL(kvm)) {
+		r = PTR_ERR(kvm);

How is this going to work when 'kvm' is NULL? It'd be best if kvm_init() never returned NULL on error.

Quoted 5 lines

tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, const char **argv) · line 1006

Quoted 5 lines
tools/kvm/builtin-run.c +2 -0
+		goto fail;
+	}
 
 	kvm->single_step = single_step;
 

I suspect we need something like

---
 tools/kvm/builtin-run.c |    5 +++++
 tools/kvm/kvm.c         |    2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)
tools/kvm/builtin-run.c +5 -0
Index: linux-2.6.git/tools/kvm/builtin-run.c
===================================================================
@@ -997,6 +997,11 @@ static int kvm_cmd_run_init(int argc, co
 	}
 
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR(kvm)) {
+		r = (int)PTR_ERR(kvm);
+		pr_err("Can't initialize KVM, failed with error %d\n", r);
+		goto fail;
+	}
 
 	kvm->single_step = single_step;
 
tools/kvm/kvm.c +1 -1
Index: linux-2.6.git/tools/kvm/kvm.c
===================================================================
@@ -340,7 +340,7 @@ struct kvm *kvm__init(const char *kvm_de
 	}
 
 	kvm = kvm__new();
-	if (IS_ERR_OR_NULL(kvm))
+	if (IS_ERR(kvm))
 		return kvm;
 
 	kvm->sys_fd = open(kvm_dev, O_RDWR);
#3 lore raw
Quoted 11 lines · Cyrill Gorcunov

I suspect we need something like
---

tools/kvm/builtin-run.c |    5 +++++
tools/kvm/kvm.c         |    2 +-
2 files changed, 6 insertions(+), 1 deletion(-)

Index: linux-2.6.git/tools/kvm/builtin-run.c
===================================================================

tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, co · line 1001

Quoted 5 lines
tools/kvm/builtin-run.c +2 -0
@@ -997,6 +997,11 @@ static int kvm_cmd_run_init(int argc, co
 	}
 
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR(kvm)) {
+		r = (int)PTR_ERR(kvm);

The cast is not needed.

Quoted 3 lines
tools/kvm/builtin-run.c +3 -0
+		pr_err("Can't initialize KVM, failed with error %d\n", r);
+		goto fail;
+	}
#4 lore raw
Quoted 14 lines · Pekka Enberg
Quoted 13 lines · Cyrill Gorcunov

I suspect we need something like
---

tools/kvm/builtin-run.c |    5 +++++
tools/kvm/kvm.c         |    2 +-
2 files changed, 6 insertions(+), 1 deletion(-)

Index: linux-2.6.git/tools/kvm/builtin-run.c
===================================================================

tools/kvm/builtin-run.c +0 -0
@@ -997,6 +997,11 @@ static int kvm_cmd_run_init(int argc, co
 	}
 
Quoted 6 lines

tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, co · line 1001

Quoted 3 lines
tools/kvm/builtin-run.c +2 -0
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR(kvm)) {
+		r = (int)PTR_ERR(kvm);

The cast is not needed.

Yeah, it's leftover from draft patch ;) Sasha, would you make some new version or I should create new patch?

Cyrill

#5 lore raw
In reply to Pekka Enberg
Quoted 16 lines · Pekka Enberg
Quoted 15 lines · Sasha Levin

Might happen when hardware virtualization is not supported.

---
tools/kvm/builtin-run.c | 4 ++++

1 files changed, 4 insertions(+), 0 deletions(-)
tools/kvm/builtin-run.c +0 -0
index 6ded1d2..a67faf8 100644
@@ -997,6 +997,10 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 	}
 
Quoted 6 lines

tools/kvm/builtin-run.c static int kvm_cmd_run_init(int argc, const char **argv) · line 1001

Quoted 3 lines
tools/kvm/builtin-run.c +2 -0
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
+	if (IS_ERR_OR_NULL(kvm)) {
+		r = PTR_ERR(kvm);

How is this going to work when 'kvm' is NULL? It'd be best if kvm_init() never returned NULL on error.

Actually, it never does, I'm not sure why I put IS_ERR_OR_NULL there.

I'll resend that one.

Signature · 1 line
Sasha.