Skip to contentSkip to conversation

KVM: Use previously computed array_size()

PATCH In review Denis Efremov 5 participants 11 messages May 30, 2020 – Jun 18, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: Use previously computed array_size()

array_size() is used in alloc calls to compute the allocation size. Next, "raw" multiplication is used to compute the size for copy_from_user(). The patch removes duplicated computation by saving the size in a var. No security concerns, just a small optimization.

---
 arch/x86/kvm/cpuid.c | 9 ++++-----
 virt/kvm/kvm_main.c  | 8 ++++----
 2 files changed, 8 insertions(+), 9 deletions(-)
arch/x86/kvm/cpuid.c +4 -5
index 901cd1fdecd9..3363b7531af1 100644
@@ -184,14 +184,13 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 		goto out;
 	r = -ENOMEM;
 	if (cpuid->nent) {
-		cpuid_entries =
-			vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
-					   cpuid->nent));
+		const size_t size = array_size(sizeof(struct kvm_cpuid_entry),
+					       cpuid->nent);
+		cpuid_entries = vmalloc(size);
 		if (!cpuid_entries)
 			goto out;
 		r = -EFAULT;
-		if (copy_from_user(cpuid_entries, entries,
-				   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+		if (copy_from_user(cpuid_entries, entries, size))
 			goto out;
 	}
 	for (i = 0; i < cpuid->nent; i++) {
virt/kvm/kvm_main.c +4 -4
index 731c1e517716..001e1929e01c 100644
@@ -3722,15 +3722,15 @@ static long kvm_vm_ioctl(struct file *filp,
 		if (routing.flags)
 			goto out;
 		if (routing.nr) {
+			const size_t size = array_size(sizeof(*entries),
+						       routing.nr);
 			r = -ENOMEM;
-			entries = vmalloc(array_size(sizeof(*entries),
-						     routing.nr));
+			entries = vmalloc(size);
 			if (!entries)
 				goto out;
 			r = -EFAULT;
 			urouting = argp;
-			if (copy_from_user(entries, urouting->entries,
-					   routing.nr * sizeof(*entries)))
+			if (copy_from_user(entries, urouting->entries, size))
 				goto out_free_irq_routing;
 		}
 		r = kvm_set_irq_routing(kvm, entries, routing.nr,
Signature · 1 line
2.26.2
#1 lore raw
Quoted 7 lines · Denis Efremov

array_size() is used in alloc calls to compute the allocation size. Next, "raw" multiplication is used to compute the size for copy_from_user(). The patch removes duplicated computation by saving the size in a var. No security concerns, just a small optimization.

Perhaps use vmemdup_user?

arch/x86/kvm/cpuid.c

Quoted 1 line
arch/x86/kvm/cpuid.c +0 -0

[]

Quoted 6 lines
arch/x86/kvm/cpuid.c +0 -2
@@ -184,14 +184,13 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 		goto out;
 	r = -ENOMEM;
 	if (cpuid->nent) {
-		cpuid_entries =
-			vmalloc(array_size(sizeof(struct kvm_cpuid_entry),

arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, · line 193

Quoted 10 lines
arch/x86/kvm/cpuid.c +4 -3
-					   cpuid->nent));
+		const size_t size = array_size(sizeof(struct kvm_cpuid_entry),
+					       cpuid->nent);
+		cpuid_entries = vmalloc(size);
 		if (!cpuid_entries)
 			goto out;
 		r = -EFAULT;
-		if (copy_from_user(cpuid_entries, entries,
-				   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+		if (copy_from_user(cpuid_entries, entries, size))
		cpuid_entries = vmemdup_user(entries,
					     array_size(sizeof(struct kvm_cpuid_entry), cpuid->nent));
		if (IS_ERR(cpuid_entries))
			...

etc...

#2 lore raw
Quoted 10 lines · Joe Perches
Quoted 7 lines · Denis Efremov

array_size() is used in alloc calls to compute the allocation size. Next, "raw" multiplication is used to compute the size for copy_from_user(). The patch removes duplicated computation by saving the size in a var. No security concerns, just a small optimization.

Perhaps use vmemdup_user?

vmemdup_user() uses kvmalloc internally. I think it will also require changing vfree to kvfree.

Quoted 27 lines

arch/x86/kvm/cpuid.c

Quoted 2 lines
arch/x86/kvm/cpuid.c +0 -0

[]

Quoted 6 lines
arch/x86/kvm/cpuid.c +0 -2
@@ -184,14 +184,13 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 		goto out;
 	r = -ENOMEM;
 	if (cpuid->nent) {
-		cpuid_entries =
-			vmalloc(array_size(sizeof(struct kvm_cpuid_entry),

arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, · line 193

Quoted 10 lines
arch/x86/kvm/cpuid.c +4 -3
-					   cpuid->nent));
+		const size_t size = array_size(sizeof(struct kvm_cpuid_entry),
+					       cpuid->nent);
+		cpuid_entries = vmalloc(size);
 		if (!cpuid_entries)
 			goto out;
 		r = -EFAULT;
-		if (copy_from_user(cpuid_entries, entries,
-				   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+		if (copy_from_user(cpuid_entries, entries, size))
		cpuid_entries = vmemdup_user(entries,
					     array_size(sizeof(struct kvm_cpuid_entry), cpuid->nent));
		if (IS_ERR(cpuid_entries))
			...

etc...

maintainer
#3 lore raw
Quoted 7 lines · Denis Efremov
Quoted 7 lines · Denis Efremov
Quoted 6 lines

array_size() is used in alloc calls to compute the allocation size. Next, "raw" multiplication is used to compute the size for copy_from_user(). The patch removes duplicated computation by saving the size in a var. No security concerns, just a small optimization.

Quoted 5 lines
Quoted 2 lines
Quoted 1 line

Perhaps use vmemdup_user?

vmemdup_user() uses kvmalloc internally. I think it will also require changing vfree to kvfree.

Yes, it would be a good idea.

Paolo

#4 lore raw

PATCH 1/1 KVM: Use vmemdup_user()

In reply to Joe Perches

Replace opencoded alloc and copy with vmemdup_user().

---
Looks like these are the only places in KVM that are suitable for vmemdup_user().

 arch/x86/kvm/cpuid.c | 17 +++++++----------
 virt/kvm/kvm_main.c  | 19 ++++++++-----------
 2 files changed, 15 insertions(+), 21 deletions(-)
arch/x86/kvm/cpuid.c +7 -10
index 901cd1fdecd9..27438a2bdb62 100644
@@ -182,17 +182,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 	r = -E2BIG;
 	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
 		goto out;
-	r = -ENOMEM;
 	if (cpuid->nent) {
-		cpuid_entries =
-			vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
-					   cpuid->nent));
-		if (!cpuid_entries)
-			goto out;
-		r = -EFAULT;
-		if (copy_from_user(cpuid_entries, entries,
-				   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+		cpuid_entries = vmemdup_user(entries,
+					     array_size(sizeof(struct kvm_cpuid_entry),
+							cpuid->nent));
+		if (IS_ERR(cpuid_entries)) {
+			r = PTR_ERR(cpuid_entries);
 			goto out;
+		}
 	}
 	for (i = 0; i < cpuid->nent; i++) {
 		vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
@@ -212,8 +209,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
 
+	kvfree(cpuid_entries);
 out:
-	vfree(cpuid_entries);
 	return r;
 }
 
virt/kvm/kvm_main.c +8 -11
index 731c1e517716..46a3743e95ff 100644
@@ -3722,21 +3722,18 @@ static long kvm_vm_ioctl(struct file *filp,
 		if (routing.flags)
 			goto out;
 		if (routing.nr) {
-			r = -ENOMEM;
-			entries = vmalloc(array_size(sizeof(*entries),
-						     routing.nr));
-			if (!entries)
-				goto out;
-			r = -EFAULT;
 			urouting = argp;
-			if (copy_from_user(entries, urouting->entries,
-					   routing.nr * sizeof(*entries)))
-				goto out_free_irq_routing;
+			entries = vmemdup_user(urouting->entries,
+					       array_size(sizeof(*entries),
+							  routing.nr));
+			if (IS_ERR(entries)) {
+				r = PTR_ERR(entries);
+				goto out;
+			}
 		}
 		r = kvm_set_irq_routing(kvm, entries, routing.nr,
 					routing.flags);
-out_free_irq_routing:
-		vfree(entries);
+		kvfree(entries);
 		break;
 	}
 #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
Signature · 1 line
2.26.2
maintainer Applied
#5 lore raw

Re: [PATCH] KVM: Use vmemdup_user()

Quoted 74 lines · Denis Efremov

Replace opencoded alloc and copy with vmemdup_user().

---
Looks like these are the only places in KVM that are suitable for vmemdup_user().

 arch/x86/kvm/cpuid.c | 17 +++++++----------
 virt/kvm/kvm_main.c  | 19 ++++++++-----------
 2 files changed, 15 insertions(+), 21 deletions(-)
arch/x86/kvm/cpuid.c +7 -10
index 901cd1fdecd9..27438a2bdb62 100644
@@ -182,17 +182,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 	r = -E2BIG;
 	if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
 		goto out;
-	r = -ENOMEM;
 	if (cpuid->nent) {
-		cpuid_entries =
-			vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
-					   cpuid->nent));
-		if (!cpuid_entries)
-			goto out;
-		r = -EFAULT;
-		if (copy_from_user(cpuid_entries, entries,
-				   cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+		cpuid_entries = vmemdup_user(entries,
+					     array_size(sizeof(struct kvm_cpuid_entry),
+							cpuid->nent));
+		if (IS_ERR(cpuid_entries)) {
+			r = PTR_ERR(cpuid_entries);
 			goto out;
+		}
 	}
 	for (i = 0; i < cpuid->nent; i++) {
 		vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
@@ -212,8 +209,8 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
 	kvm_x86_ops.cpuid_update(vcpu);
 	r = kvm_update_cpuid(vcpu);
 
+	kvfree(cpuid_entries);
 out:
-	vfree(cpuid_entries);
 	return r;
 }
 
virt/kvm/kvm_main.c +6 -9
index 731c1e517716..46a3743e95ff 100644
@@ -3722,21 +3722,18 @@ static long kvm_vm_ioctl(struct file *filp,
 		if (routing.flags)
 			goto out;
 		if (routing.nr) {
-			r = -ENOMEM;
-			entries = vmalloc(array_size(sizeof(*entries),
-						     routing.nr));
-			if (!entries)
-				goto out;
-			r = -EFAULT;
 			urouting = argp;
-			if (copy_from_user(entries, urouting->entries,
-					   routing.nr * sizeof(*entries)))
-				goto out_free_irq_routing;
+			entries = vmemdup_user(urouting->entries,
+					       array_size(sizeof(*entries),
+							  routing.nr));
+			if (IS_ERR(entries)) {
+				r = PTR_ERR(entries);
+				goto out;

virt/kvm/kvm_main.c static long kvm_vm_ioctl(struct file *filp, · line 3739

Quoted 10 lines
virt/kvm/kvm_main.c +2 -2
+			}
 		}
 		r = kvm_set_irq_routing(kvm, entries, routing.nr,
 					routing.flags);
-out_free_irq_routing:
-		vfree(entries);
+		kvfree(entries);
 		break;
 	}
 #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */

Queued, thanks.

Paolo

#6 lore raw

Re: [PATCH] KVM: Use vmemdup_user()

In reply to Denis Efremov
Quoted 23 lines · Denis Efremov

Replace opencoded alloc and copy with vmemdup_user().

---
Looks like these are the only places in KVM that are suitable for vmemdup_user().

 arch/x86/kvm/cpuid.c | 17 +++++++----------
 virt/kvm/kvm_main.c  | 19 ++++++++-----------
 2 files changed, 15 insertions(+), 21 deletions(-)
arch/x86/kvm/cpuid.c +0 -2
index 901cd1fdecd9..27438a2bdb62 100644
@@ -182,17 +182,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
        r = -E2BIG;
        if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
                goto out;
-       r = -ENOMEM;
        if (cpuid->nent) {
-               cpuid_entries =

arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, · line 188

Quoted 10 lines
arch/x86/kvm/cpuid.c +3 -7
-                       vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
-                                          cpuid->nent));
-               if (!cpuid_entries)
-                       goto out;
-               r = -EFAULT;
-               if (copy_from_user(cpuid_entries, entries,
-                                  cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+               cpuid_entries = vmemdup_user(entries,
+                                            array_size(sizeof(struct kvm_cpuid_entry),
+                                                       cpuid->nent));

Does this break memcg accounting? I ask, because I'm really not sure.

#7 lore raw

Re: [PATCH] KVM: Use vmemdup_user()

Quoted 28 lines · Jim Mattson
Quoted 27 lines · Denis Efremov

Replace opencoded alloc and copy with vmemdup_user().

---
Looks like these are the only places in KVM that are suitable for vmemdup_user().

 arch/x86/kvm/cpuid.c | 17 +++++++----------
 virt/kvm/kvm_main.c  | 19 ++++++++-----------
 2 files changed, 15 insertions(+), 21 deletions(-)
arch/x86/kvm/cpuid.c +0 -6
index 901cd1fdecd9..27438a2bdb62 100644
@@ -182,17 +182,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
        r = -E2BIG;
        if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
                goto out;
-       r = -ENOMEM;
        if (cpuid->nent) {
-               cpuid_entries =
-                       vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
-                                          cpuid->nent));
-               if (!cpuid_entries)
-                       goto out;
Quoted 8 lines

arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, · line 188

Quoted 6 lines
arch/x86/kvm/cpuid.c +3 -3
-               r = -EFAULT;
-               if (copy_from_user(cpuid_entries, entries,
-                                  cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+               cpuid_entries = vmemdup_user(entries,
+                                            array_size(sizeof(struct kvm_cpuid_entry),
+                                                       cpuid->nent));

Does this break memcg accounting? I ask, because I'm really not sure.

What do you mean by that? The original code uses plain vmalloc so the allocation is not memcg accounted (please note that __GFP_ACCOUNT needs to be specified explicitly). vmemdup_user is the same in that regards.

Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw

Re: [PATCH] KVM: Use vmemdup_user()

Quoted 39 lines · Michal Hocko
Quoted 28 lines · Jim Mattson
Quoted 27 lines · Denis Efremov

Replace opencoded alloc and copy with vmemdup_user().

---
Looks like these are the only places in KVM that are suitable for vmemdup_user().

 arch/x86/kvm/cpuid.c | 17 +++++++----------
 virt/kvm/kvm_main.c  | 19 ++++++++-----------
 2 files changed, 15 insertions(+), 21 deletions(-)
arch/x86/kvm/cpuid.c +0 -6
index 901cd1fdecd9..27438a2bdb62 100644
@@ -182,17 +182,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
        r = -E2BIG;
        if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
                goto out;
-       r = -ENOMEM;
        if (cpuid->nent) {
-               cpuid_entries =
-                       vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
-                                          cpuid->nent));
-               if (!cpuid_entries)
-                       goto out;
Quoted 8 lines

arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, · line 188

Quoted 6 lines
arch/x86/kvm/cpuid.c +3 -3
-               r = -EFAULT;
-               if (copy_from_user(cpuid_entries, entries,
-                                  cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+               cpuid_entries = vmemdup_user(entries,
+                                            array_size(sizeof(struct kvm_cpuid_entry),
+                                                       cpuid->nent));

Does this break memcg accounting? I ask, because I'm really not sure.

Quoted 3 lines

What do you mean by that? The original code uses plain vmalloc so the allocation is not memcg accounted (please note that __GFP_ACCOUNT needs to be specified explicitly). vmemdup_user is the same in that regards.

I asked, because I wasn't sure if plain vmalloc was accounted or not.

In any case, these allocations *should* be accounted, shouldn't they?

#9 lore raw

Re: [PATCH] KVM: Use vmemdup_user()

Quoted 40 lines · Jim Mattson
Quoted 39 lines · Michal Hocko
Quoted 36 lines · Jim Mattson
Quoted 23 lines · Denis Efremov

Replace opencoded alloc and copy with vmemdup_user().

---
Looks like these are the only places in KVM that are suitable for vmemdup_user().

 arch/x86/kvm/cpuid.c | 17 +++++++----------
 virt/kvm/kvm_main.c  | 19 ++++++++-----------
 2 files changed, 15 insertions(+), 21 deletions(-)
arch/x86/kvm/cpuid.c +0 -2
index 901cd1fdecd9..27438a2bdb62 100644
@@ -182,17 +182,14 @@ int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
        r = -E2BIG;
        if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
                goto out;
-       r = -ENOMEM;
        if (cpuid->nent) {
-               cpuid_entries =

arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu, · line 188

Quoted 10 lines
arch/x86/kvm/cpuid.c +3 -7
-                       vmalloc(array_size(sizeof(struct kvm_cpuid_entry),
-                                          cpuid->nent));
-               if (!cpuid_entries)
-                       goto out;
-               r = -EFAULT;
-               if (copy_from_user(cpuid_entries, entries,
-                                  cpuid->nent * sizeof(struct kvm_cpuid_entry)))
+               cpuid_entries = vmemdup_user(entries,
+                                            array_size(sizeof(struct kvm_cpuid_entry),
+                                                       cpuid->nent));

Does this break memcg accounting? I ask, because I'm really not sure.

Quoted 7 lines
Quoted 3 lines

What do you mean by that? The original code uses plain vmalloc so the allocation is not memcg accounted (please note that __GFP_ACCOUNT needs to be specified explicitly). vmemdup_user is the same in that regards.

I asked, because I wasn't sure if plain vmalloc was accounted or not.

In any case, these allocations *should* be accounted, shouldn't they?

This is more of a question to maintainers. Are these objects easy to request by userspace without any bounds?

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#10 lore raw

Re: [PATCH] KVM: Use vmemdup_user()

Quoted 5 lines · Michal Hocko
Quoted 1 line · Jim Mattson

In any case, these allocations *should* be accounted, shouldn't they?

This is more of a question to maintainers. Are these objects easy to request by userspace without any bounds?

This particular one need not be accounted because the allocation only lasts for the duration of the ioctl. The allocation below in kvm_vcpu_ioctl_set_cpuid

     e2 = kvmalloc_array(cpuid->nent, sizeof(*e2), GFP_KERNEL_ACCOUNT);

is long term and is already accounted for.

kvm_vcpu_ioctl_set_cpuid2 should also use kvmalloc_array and GFP_KERNEL_ACCOUNT. However, it wasn't doing so before this patch went in, either.

Paolo