KVM: Use previously computed array_size()
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
@@ -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
@@ -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
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 forcopy_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 <[email protected]>
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...
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 forcopy_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 <[email protected]>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...
Paolo Bonzini
maintainerQuoted 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 forcopy_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
Signed-off-by Denis Efremov <[email protected]>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
PATCH 1/1 KVM: Use vmemdup_user()
In reply to Joe PerchesReplace 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
@@ -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
@@ -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
Re: [PATCH] KVM: Use vmemdup_user()
Quoted 74 lines · Denis Efremov
Replace opencoded alloc and copy with
vmemdup_user().Signed-off-by Denis Efremov <[email protected]>---
Looks like these are the only places in KVM that are suitable forvmemdup_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@@ -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@@ -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
Re: [PATCH] KVM: Use vmemdup_user()
In reply to Denis EfremovQuoted 23 lines · Denis Efremov
Replace opencoded alloc and copy with
vmemdup_user().Signed-off-by Denis Efremov <[email protected]>---
Looks like these are the only places in KVM that are suitable forvmemdup_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@@ -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.
Re: [PATCH] KVM: Use vmemdup_user()
Quoted 28 lines · Jim Mattson
Quoted 27 lines · Denis Efremov
Replace opencoded alloc and copy with
vmemdup_user().Signed-off-by Denis Efremov <[email protected]>---
Looks like these are the only places in KVM that are suitable forvmemdup_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@@ -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
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().Signed-off-by Denis Efremov <[email protected]>---
Looks like these are the only places in KVM that are suitable forvmemdup_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@@ -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_ACCOUNTneeds to be specified explicitly).vmemdup_useris 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?
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().Signed-off-by Denis Efremov <[email protected]>---
Looks like these are the only places in KVM that are suitable forvmemdup_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@@ -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_ACCOUNTneeds to be specified explicitly).vmemdup_useris 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
Paolo Bonzini
maintainerRe: [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