linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: Use previously computed array_size()
@ 2020-05-30 14:35 Denis Efremov
  2020-05-30 15:58 ` Joe Perches
  0 siblings, 1 reply; 11+ messages in thread
From: Denis Efremov @ 2020-05-30 14:35 UTC (permalink / raw)
  To: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov
  Cc: Denis Efremov, kvm, linux-kernel

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.

Signed-off-by: Denis Efremov <efremov@linux.com>
---
 arch/x86/kvm/cpuid.c | 9 ++++-----
 virt/kvm/kvm_main.c  | 8 ++++----
 2 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 901cd1fdecd9..3363b7531af1 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -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++) {
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 731c1e517716..001e1929e01c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -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,
-- 
2.26.2


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

end of thread, other threads:[~2021-06-18 17:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-30 14:35 [PATCH] KVM: Use previously computed array_size() Denis Efremov
2020-05-30 15:58 ` Joe Perches
2020-05-30 17:28   ` Denis Efremov
2020-06-01  8:46     ` Paolo Bonzini
2020-06-03 10:11   ` [PATCH] KVM: Use vmemdup_user() Denis Efremov
2020-06-04 18:41     ` Paolo Bonzini
2021-06-18  0:25     ` Jim Mattson
2021-06-18  6:00       ` Michal Hocko
2021-06-18 16:53         ` Jim Mattson
2021-06-18 17:04           ` Michal Hocko
2021-06-18 17:32             ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).