Skip to contentSkip to conversation

kvm tool: rewrite kvm__init

PATCH In review Yang Bai 4 participants 8 messages Feb 9, 2012 – Feb 10, 2012 KVM

lore
Share & tools
lore

Yang Bai

author
#0 lore raw

PATCH 1/1 kvm tool: rewrite kvm__init

Since the different issues have been handled in the internal of kvm__init, it can only return NULL if error happened.

---
 tools/kvm/builtin-run.c |    4 ++--
 tools/kvm/kvm.c         |   20 +++++++-------------
 2 files changed, 9 insertions(+), 15 deletions(-)
tools/kvm/builtin-run.c +2 -2
index 466169e..b71816f 100644
@@ -997,8 +997,8 @@ static int kvm_cmd_run_init(int argc, const char **argv)
 	}
 
 	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
-	if (IS_ERR(kvm)) {
-		r = PTR_ERR(kvm);
+	if (!kvm) {
+		r = -EFAULT;
 		goto fail;
 	}
 
tools/kvm/kvm.c +7 -13
index 9a0bd67..e3280a8 100644
@@ -334,17 +334,17 @@ int kvm__max_cpus(struct kvm *kvm)
 
 struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_size, const char *name)
 {
-	struct kvm *kvm;
+	struct kvm *kvm = NULL;
 	int ret;
 
 	if (!kvm__arch_cpu_supports_vm()) {
 		pr_err("Your CPU does not support hardware virtualization");
-		return ERR_PTR(-ENOSYS);
+		return NULL;
 	}
 
 	kvm = kvm__new();
 	if (IS_ERR(kvm))
-		return kvm;
+		return NULL;
 
 	kvm->sys_fd = open(kvm_dev, O_RDWR);
 	if (kvm->sys_fd < 0) {
@@ -358,32 +358,26 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s
 		else
 			pr_err("Could not open %s: ", kvm_dev);
 
-		ret = -errno;
 		goto err_free;
 	}
 
 	ret = ioctl(kvm->sys_fd, KVM_GET_API_VERSION, 0);
 	if (ret != KVM_API_VERSION) {
 		pr_err("KVM_API_VERSION ioctl");
-		ret = -errno;
 		goto err_sys_fd;
 	}
 
 	kvm->vm_fd = ioctl(kvm->sys_fd, KVM_CREATE_VM, 0);
-	if (kvm->vm_fd < 0) {
-		ret = kvm->vm_fd;
+	if (kvm->vm_fd < 0)
 		goto err_sys_fd;
-	}
 
 	kvm->name = strdup(name);
-	if (!kvm->name) {
-		ret = -ENOMEM;
+	if (!kvm->name)
 		goto err;
-	}
 
 	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);
@@ -400,7 +394,7 @@ err_sys_fd:
 err_free:
 	free(kvm);
 
-	return ERR_PTR(ret);
+	return NULL;
 }
 
 /* RFC 1952 */
Signature · 1 line
1.7.8.3
#1 lore raw
Quoted 5 lines · Yang Bai

Since the different issues have been handled in the internal of kvm__init, it can only return NULL if error happened.

Sorry, I don't understand what this patch is attempting to fix? Why do you think it's better to drop the explicit error codes and always return NULL upon error?

Quoted 97 lines

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

 2 files changed, 9 insertions(+), 15 deletions(-)
tools/kvm/builtin-run.c +2 -2
index 466169e..b71816f 100644
@@ -997,8 +997,8 @@ static int kvm_cmd_run_init(int argc, const char **argv)
        }
 
        kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
-       if (IS_ERR(kvm)) {
-               r = PTR_ERR(kvm);
+       if (!kvm) {
+               r = -EFAULT;
                goto fail;
        }
 
tools/kvm/kvm.c +3 -3
index 9a0bd67..e3280a8 100644
@@ -334,17 +334,17 @@ int kvm__max_cpus(struct kvm *kvm)
 
  struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_size, const char *name)
  {
-       struct kvm *kvm;
+       struct kvm *kvm = NULL;
        int ret;
 
        if (!kvm__arch_cpu_supports_vm()) {
                pr_err("Your CPU does not support hardware virtualization");
-               return ERR_PTR(-ENOSYS);
+               return NULL;
        }
 
        kvm = kvm__new();
        if (IS_ERR(kvm))
-               return kvm;
+               return NULL;
 
        kvm->sys_fd = open(kvm_dev, O_RDWR);
        if (kvm->sys_fd < 0) {
@@ -358,32 +358,26 @@ struct kvm *kvm__init(const char *kvm_dev, const char *hugetlbfs_path, u64 ram_s

               else
                       pr_err("Could not open %s: ", kvm_dev);

-               ret = -errno;
               goto err_free;
       }

       ret = ioctl(kvm->sys_fd, KVM_GET_API_VERSION, 0); if (ret != KVM_API_VERSION) { pr_err("KVM_API_VERSION ioctl");
-               ret = -errno;
               goto err_sys_fd;
       }

       kvm->vm_fd = ioctl(kvm->sys_fd, KVM_CREATE_VM, 0);
-       if (kvm->vm_fd < 0) {
-               ret = kvm->vm_fd;
+       if (kvm->vm_fd < 0)
               goto err_sys_fd;
-       }

       kvm->name = strdup(name);
-       if (!kvm->name) {
-               ret = -ENOMEM;
+       if (!kvm->name)
               goto err;
-       }

       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);

tools/kvm/kvm.c +0 -0
@@ -400,7 +394,7 @@ err_sys_fd:

 err_free:
       free(kvm);

-       return ERR_PTR(ret);
+       return NULL;
 }

 /* RFC 1952 */
--
1.7.8.3

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 11 lines · Pekka Enberg
Quoted 5 lines · Yang Bai

Since the different issues have been handled in the internal of kvm__init, it can only return NULL if error happened.

Sorry, I don't understand what this patch is attempting to fix? Why do you think it's better to drop the explicit error codes and always return NULL upon error?

Yeah, I somehow don't get it as well.

Cyrill

author
#3 lore raw
Quoted 6 lines · Cyrill Gorcunov
Quoted 5 lines · Pekka Enberg
Quoted 4 lines · Yang Bai

Since the different issues have been handled in the internal of kvm__init, it can only return NULL if error happened.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Sorry, I don't understand what this patch is attempting to fix? Why do you think it's better to drop the explicit error codes and always return NULL upon error?

Ok. Since the different issues have been handled in the internal of this function and the caller does not care about the real error reasons. So just return NULL if error will simplify the error handle of the caller.

Quoted 4 lines

Yeah, I somehow don't get it as well.

       Cyrill

Signature · 9 lines
    """
    Keep It Simple,Stupid.
    """

Chinese Name: 白杨
Nick Name: Hamo
Homepage: http://hamobai.com/
GPG KEY ID: 0xA4691A33
Key fingerprint = 09D5 2D78 8E2B 0995 CF8E  4331 33C4 3D24 A469 1A33
#4 lore raw
Quoted 14 lines · Yang Bai
Quoted 6 lines · Cyrill Gorcunov
Quoted 5 lines · Pekka Enberg
Quoted 4 lines · Yang Bai

Since the different issues have been handled in the internal of kvm__init, it can only return NULL if error happened.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Sorry, I don't understand what this patch is attempting to fix? Why do you think it's better to drop the explicit error codes and always return NULL upon error?

Quoted 4 lines

Ok. Since the different issues have been handled in the internal of this function and the caller does not care about the real error reasons. So just return NULL if error will simplify the error handle of the caller.

Um... why doesn't the caller care about the real error? It's whats being sent back to userspace and can help the caller determine whats going on.

Signature · 1 line
Sasha.
author
#5 lore raw
Quoted 15 lines · Sasha Levin
Quoted 14 lines · Yang Bai
Quoted 12 lines · Cyrill Gorcunov
Quoted 11 lines · Pekka Enberg
Quoted 5 lines · Yang Bai

Since the different issues have been handled in the internal of kvm__init, it can only return NULL if error happened.

Sorry, I don't understand what this patch is attempting to fix? Why do you think it's better to drop the explicit error codes and always return NULL upon error?

Quoted 8 lines
Quoted 4 lines

Ok. Since the different issues have been handled in the internal of this function and the caller does not care about the real error reasons. So just return NULL if error will simplify the error handle of the caller.

Um... why doesn't the caller care about the real error? It's whats being sent back to userspace and can help the caller determine whats going on.

Reading the source code, I found that the caller handle it as following:

static int kvm_cmd_run_init(int argc, const char **argv) {
[snip]

	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
	if (IS_ERR(kvm)) {
		r = PTR_ERR(kvm);
		goto fail;
	}
[snip]
fail:
		return r;
}

int kvm_cmd_run(int argc, const char **argv, const char *prefix) {
[snip]

	r = kvm_cmd_run_init(argc, argv);
	if (r < 0)
		return r;

[snip]
}

So the real reason is ignored.

Quoted 4 lines

--

Sasha.

Signature · 9 lines
    """
    Keep It Simple,Stupid.
    """

Chinese Name: 白杨
Nick Name: Hamo
Homepage: http://hamobai.com/
GPG KEY ID: 0xA4691A33
Key fingerprint = 09D5 2D78 8E2B 0995 CF8E  4331 33C4 3D24 A469 1A33
#6 lore raw
Quoted 42 lines · Yang Bai
Quoted 15 lines · Sasha Levin
Quoted 14 lines · Yang Bai
Quoted 12 lines · Cyrill Gorcunov
Quoted 11 lines · Pekka Enberg
Quoted 5 lines · Yang Bai

Since the different issues have been handled in the internal of kvm__init, it can only return NULL if error happened.

Sorry, I don't understand what this patch is attempting to fix? Why do you think it's better to drop the explicit error codes and always return NULL upon error?

Quoted 8 lines
Quoted 4 lines

Ok. Since the different issues have been handled in the internal of this function and the caller does not care about the real error reasons. So just return NULL if error will simplify the error handle of the caller.

Um... why doesn't the caller care about the real error? It's whats being sent back to userspace and can help the caller determine whats going on.

Reading the source code, I found that the caller handle it as following:

static int kvm_cmd_run_init(int argc, const char **argv) {
[snip]

	kvm = kvm__init(dev, hugetlbfs_path, ram_size, guest_name);
	if (IS_ERR(kvm)) {
		r = PTR_ERR(kvm);
		goto fail;
	}
[snip]
fail:
		return r;
}

int kvm_cmd_run(int argc, const char **argv, const char *prefix) {
[snip]

Quoted 7 lines
	r = kvm_cmd_run_init(argc, argv);
	if (r < 0)
		return r;

[snip]
}

From what I gather from the code snippet you pasted is that we return

the error to the user, which can help him determine whats going on...

Signature · 1 line
Sasha.
author
#7 lore raw
Quoted 9 lines · Sasha Levin
Quoted 5 lines · Yang Bai
Quoted 4 lines · Sasha Levin
Quoted 3 lines · Yang Bai
Quoted 2 lines · Cyrill Gorcunov
Quoted 1 line · Pekka Enberg

On Thu, Feb 9, 2012 at 7:40 AM, Yang Bai <[email protected]> wrote:

From what I gather from the code snippet you pasted is that we return the error to the user, which can help him determine whats going on...

Fine. Thanks for you pointing it out. Just drop this patch.

Thanks.

Quoted 5 lines

--

Sasha.

Signature · 9 lines
    """
    Keep It Simple,Stupid.
    """

Chinese Name: 白杨
Nick Name: Hamo
Homepage: http://hamobai.com/
GPG KEY ID: 0xA4691A33
Key fingerprint = 09D5 2D78 8E2B 0995 CF8E  4331 33C4 3D24 A469 1A33