public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] kvm tool: Stop init if check_extensions failed
@ 2012-02-10  9:55 Yang Bai
  2012-02-10  9:55 ` [PATCH 2/4] kvm tool: unite the error handle in kvm__init Yang Bai
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Yang Bai @ 2012-02-10  9:55 UTC (permalink / raw)
  To: penberg; +Cc: kvm, Yang Bai

If kvm__check_extensions found that some of the required
KVM extention is not supported by OS, we should stop the
init and free all allocated resources.

Signed-off-by: Yang Bai <hamo.by@gmail.com>
---
 tools/kvm/kvm.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 9a0bd67..8e749ad 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -384,6 +384,7 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s
 	if (kvm__check_extensions(kvm)) {
 		pr_err("A required KVM extention is not supported by OS");
 		ret = -ENOSYS;
+		goto err;
 	}
 
 	kvm__arch_init(kvm, hugetlbfs_path, ram_size);
-- 
1.7.8.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] kvm tool: unite the error handle in kvm__init
  2012-02-10  9:55 [PATCH 1/4] kvm tool: Stop init if check_extensions failed Yang Bai
@ 2012-02-10  9:55 ` Yang Bai
  2012-02-10  9:55 ` [PATCH 3/4] kvm tool: if kvm_ipc__start failed, return negative Yang Bai
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Yang Bai @ 2012-02-10  9:55 UTC (permalink / raw)
  To: penberg; +Cc: kvm, Yang Bai

When error occurs, just set the ret to the reason,
then jump to the error handle labels.
This makes the code more readable.

Signed-off-by: Yang Bai <hamo.by@gmail.com>
---
 tools/kvm/kvm.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 8e749ad..192d70e 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -339,7 +339,8 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s
 
 	if (!kvm__arch_cpu_supports_vm()) {
 		pr_err("Your CPU does not support hardware virtualization");
-		return ERR_PTR(-ENOSYS);
+		ret = -ENOSYS;
+		goto err;
 	}
 
 	kvm = kvm__new();
@@ -378,13 +379,13 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s
 	kvm->name = strdup(name);
 	if (!kvm->name) {
 		ret = -ENOMEM;
-		goto err;
+		goto err_vm_fd;
 	}
 
 	if (kvm__check_extensions(kvm)) {
 		pr_err("A required KVM extention is not supported by OS");
 		ret = -ENOSYS;
-		goto err;
+		goto err_vm_fd;
 	}
 
 	kvm__arch_init(kvm, hugetlbfs_path, ram_size);
@@ -394,13 +395,13 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s
 
 	return kvm;
 
-err:
+err_vm_fd:
 	close(kvm->vm_fd);
 err_sys_fd:
 	close(kvm->sys_fd);
 err_free:
 	free(kvm);
-
+err:
 	return ERR_PTR(ret);
 }
 
-- 
1.7.8.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] kvm tool: if kvm_ipc__start failed, return negative
  2012-02-10  9:55 [PATCH 1/4] kvm tool: Stop init if check_extensions failed Yang Bai
  2012-02-10  9:55 ` [PATCH 2/4] kvm tool: unite the error handle in kvm__init Yang Bai
@ 2012-02-10  9:55 ` Yang Bai
  2012-02-10  9:55 ` [PATCH 4/4] kvm tool: ensure kvm_ipc__register_handler success Yang Bai
  2012-02-10 11:38 ` [PATCH 1/4] kvm tool: Stop init if check_extensions failed Pekka Enberg
  3 siblings, 0 replies; 5+ messages in thread
From: Yang Bai @ 2012-02-10  9:55 UTC (permalink / raw)
  To: penberg; +Cc: kvm, Yang Bai

If kvm_ipc__start failed, it returns a negative and by checking
this return value, we can ensure that it succeeds.

Signed-off-by: Yang Bai <hamo.by@gmail.com>
---
 tools/kvm/kvm-ipc.c |   38 ++++++++++++++++++++++++++++++++------
 tools/kvm/kvm.c     |    7 ++++++-
 2 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/tools/kvm/kvm-ipc.c b/tools/kvm/kvm-ipc.c
index 6a0bd21..257c806c 100644
--- a/tools/kvm/kvm-ipc.c
+++ b/tools/kvm/kvm-ipc.c
@@ -166,27 +166,53 @@ static void *kvm_ipc__thread(void *param)
 
 int kvm_ipc__start(int sock)
 {
+	int ret;
 	struct epoll_event ev = {0};
 
 	server_fd = sock;
 
 	epoll_fd = epoll_create(KVM_IPC_MAX_MSGS);
+	if (epoll_fd < 0) {
+		ret = epoll_fd;
+		goto err;
+	}
 
 	ev.events = EPOLLIN | EPOLLET;
 	ev.data.fd = sock;
-	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) < 0)
-		die("Failed starting IPC thread");
+	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) < 0) {
+		pr_err("Failed starting IPC thread");
+		ret = -EFAULT;
+		goto err_epoll;
+	}
 
 	stop_fd = eventfd(0, 0);
+	if (stop_fd < 0) {
+		ret = stop_fd;
+		goto err_epoll;
+	}
+
 	ev.events = EPOLLIN | EPOLLET;
 	ev.data.fd = stop_fd;
-	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, stop_fd, &ev) < 0)
-		die("Failed adding stop event to epoll");
+	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, stop_fd, &ev) < 0) {
+		pr_err("Failed adding stop event to epoll");
+		ret = -EFAULT;
+		goto err_stop;
+	}
 
-	if (pthread_create(&thread, NULL, kvm_ipc__thread, NULL) != 0)
-		die("Failed starting IPC thread");
+	if (pthread_create(&thread, NULL, kvm_ipc__thread, NULL) != 0) {
+		pr_err("Failed starting IPC thread");
+		ret = -EFAULT;
+		goto err_stop;
+	}
 
 	return 0;
+
+err_stop:
+	close(stop_fd);
+err_epoll:
+	close(epoll_fd);
+err:
+	return ret;
 }
 
 int kvm_ipc__stop(void)
diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index 192d70e..f02d5df 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -390,7 +390,12 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s
 
 	kvm__arch_init(kvm, hugetlbfs_path, ram_size);
 
-	kvm_ipc__start(kvm__create_socket(kvm));
+	ret = kvm_ipc__start(kvm__create_socket(kvm));
+	if (ret < 0) {
+		pr_err("Starting ipc failed.");
+		goto err_vm_fd;
+	}
+
 	kvm_ipc__register_handler(KVM_IPC_PID, kvm__pid);
 
 	return kvm;
-- 
1.7.8.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] kvm tool: ensure kvm_ipc__register_handler success
  2012-02-10  9:55 [PATCH 1/4] kvm tool: Stop init if check_extensions failed Yang Bai
  2012-02-10  9:55 ` [PATCH 2/4] kvm tool: unite the error handle in kvm__init Yang Bai
  2012-02-10  9:55 ` [PATCH 3/4] kvm tool: if kvm_ipc__start failed, return negative Yang Bai
@ 2012-02-10  9:55 ` Yang Bai
  2012-02-10 11:38 ` [PATCH 1/4] kvm tool: Stop init if check_extensions failed Pekka Enberg
  3 siblings, 0 replies; 5+ messages in thread
From: Yang Bai @ 2012-02-10  9:55 UTC (permalink / raw)
  To: penberg; +Cc: kvm, Yang Bai

By checking the return value from kvm_ipc__register_handler,
we can ensure that it succeeds.

Signed-off-by: Yang Bai <hamo.by@gmail.com>
---
 tools/kvm/kvm.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c
index f02d5df..99bcef4 100644
--- a/tools/kvm/kvm.c
+++ b/tools/kvm/kvm.c
@@ -396,10 +396,16 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s
 		goto err_vm_fd;
 	}
 
-	kvm_ipc__register_handler(KVM_IPC_PID, kvm__pid);
+	ret = kvm_ipc__register_handler(KVM_IPC_PID, kvm__pid);
+	if (ret < 0) {
+		pr_err("Register ipc handler failed.");
+		goto err_ipc;
+	}
 
 	return kvm;
 
+err_ipc:
+	kvm_ipc__stop();
 err_vm_fd:
 	close(kvm->vm_fd);
 err_sys_fd:
-- 
1.7.8.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/4] kvm tool: Stop init if check_extensions failed
  2012-02-10  9:55 [PATCH 1/4] kvm tool: Stop init if check_extensions failed Yang Bai
                   ` (2 preceding siblings ...)
  2012-02-10  9:55 ` [PATCH 4/4] kvm tool: ensure kvm_ipc__register_handler success Yang Bai
@ 2012-02-10 11:38 ` Pekka Enberg
  3 siblings, 0 replies; 5+ messages in thread
From: Pekka Enberg @ 2012-02-10 11:38 UTC (permalink / raw)
  To: Yang Bai; +Cc: kvm, Sasha Levin, Cyrill Gorcunov, Ingo Molnar, Asias He

On Fri, Feb 10, 2012 at 11:55 AM, Yang Bai <hamo.by@gmail.com> wrote:
> If kvm__check_extensions found that some of the required
> KVM extention is not supported by OS, we should stop the
> init and free all allocated resources.
>
> Signed-off-by: Yang Bai <hamo.by@gmail.com>

Applied all four patches, thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-02-10 11:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-10  9:55 [PATCH 1/4] kvm tool: Stop init if check_extensions failed Yang Bai
2012-02-10  9:55 ` [PATCH 2/4] kvm tool: unite the error handle in kvm__init Yang Bai
2012-02-10  9:55 ` [PATCH 3/4] kvm tool: if kvm_ipc__start failed, return negative Yang Bai
2012-02-10  9:55 ` [PATCH 4/4] kvm tool: ensure kvm_ipc__register_handler success Yang Bai
2012-02-10 11:38 ` [PATCH 1/4] kvm tool: Stop init if check_extensions failed Pekka Enberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox