kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: Fix rmap allocation for very large memslots
@ 2022-01-28 21:36 Maciej S. Szmigiero
  2022-01-28 21:47 ` Sean Christopherson
  0 siblings, 1 reply; 3+ messages in thread
From: Maciej S. Szmigiero @ 2022-01-28 21:36 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Michal Hocko, kvm, linux-kernel

From: "Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>

Commit 7661809d493b ("mm: don't allow oversized kvmalloc() calls") has
forbidden using kvmalloc() to make allocations larger than INT_MAX (2 GiB).

Unfortunately, adding a memslot exceeding 1 TiB in size will result in rmap
code trying to make an allocation exceeding this limit.
Besides failing this allocation, such operation will also trigger a
WARN_ON_ONCE() added by the aforementioned commit.

Since we probably still want to use kernel slab for small rmap allocations
let's only redirect such oversized allocations to vmalloc.

A possible alternative would be to add some kind of a __GFP_LARGE flag to
skip the INT_MAX check behind kvmalloc(), however this will impact the
common kernel memory allocation code, not just KVM.

Fixes: a7c3e901a4 ("mm: introduce kv[mz]alloc helpers")
Cc: stable@vger.kernel.org
Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
---
 arch/x86/kvm/x86.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 8033eca6f3a1..c64bac8614c7 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11806,24 +11806,36 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
 
 int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages)
 {
-	const int sz = sizeof(*slot->arch.rmap[0]);
+	const size_t sz = sizeof(*slot->arch.rmap[0]);
 	int i;
 
 	for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
 		int level = i + 1;
-		int lpages = __kvm_mmu_slot_lpages(slot, npages, level);
+		size_t lpages = __kvm_mmu_slot_lpages(slot, npages, level);
+		size_t rmap_size;
 
 		if (slot->arch.rmap[i])
 			continue;
 
-		slot->arch.rmap[i] = kvcalloc(lpages, sz, GFP_KERNEL_ACCOUNT);
-		if (!slot->arch.rmap[i]) {
-			memslot_rmap_free(slot);
-			return -ENOMEM;
-		}
+		if (unlikely(check_mul_overflow(lpages, sz, &rmap_size)))
+			goto ret_fail;
+
+		/* kvzalloc() only allows sizes up to INT_MAX */
+		if (unlikely(rmap_size > INT_MAX))
+			slot->arch.rmap[i] = __vmalloc(rmap_size,
+						       GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+		else
+			slot->arch.rmap[i] = kvzalloc(rmap_size, GFP_KERNEL_ACCOUNT);
+
+		if (!slot->arch.rmap[i])
+			goto ret_fail;
 	}
 
 	return 0;
+
+ret_fail:
+	memslot_rmap_free(slot);
+	return -ENOMEM;
 }
 
 static int kvm_alloc_memslot_metadata(struct kvm *kvm,

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

* Re: [PATCH] KVM: x86: Fix rmap allocation for very large memslots
  2022-01-28 21:36 [PATCH] KVM: x86: Fix rmap allocation for very large memslots Maciej S. Szmigiero
@ 2022-01-28 21:47 ` Sean Christopherson
  2022-02-10 21:00   ` Maciej S. Szmigiero
  0 siblings, 1 reply; 3+ messages in thread
From: Sean Christopherson @ 2022-01-28 21:47 UTC (permalink / raw)
  To: Maciej S. Szmigiero
  Cc: Paolo Bonzini, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Michal Hocko, kvm, linux-kernel

On Fri, Jan 28, 2022, Maciej S. Szmigiero wrote:
> From: "Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>
> 
> Commit 7661809d493b ("mm: don't allow oversized kvmalloc() calls") has
> forbidden using kvmalloc() to make allocations larger than INT_MAX (2 GiB).
> 
> Unfortunately, adding a memslot exceeding 1 TiB in size will result in rmap
> code trying to make an allocation exceeding this limit.
> Besides failing this allocation, such operation will also trigger a
> WARN_ON_ONCE() added by the aforementioned commit.
> 
> Since we probably still want to use kernel slab for small rmap allocations
> let's only redirect such oversized allocations to vmalloc.
> 
> A possible alternative would be to add some kind of a __GFP_LARGE flag to
> skip the INT_MAX check behind kvmalloc(), however this will impact the
> common kernel memory allocation code, not just KVM.

Paolo has a cleaner fix for this[1][2], but it appears to have stalled out somewhere.

Paolo???

[1] https://lore.kernel.org/all/20211015165519.135670-1-pbonzini@redhat.com
[2] https://lore.kernel.org/all/20211016064302.165220-1-pbonzini@redhat.com

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

* Re: [PATCH] KVM: x86: Fix rmap allocation for very large memslots
  2022-01-28 21:47 ` Sean Christopherson
@ 2022-02-10 21:00   ` Maciej S. Szmigiero
  0 siblings, 0 replies; 3+ messages in thread
From: Maciej S. Szmigiero @ 2022-02-10 21:00 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: Vitaly Kuznetsov, Wanpeng Li, Jim Mattson, Joerg Roedel,
	Michal Hocko, kvm, linux-kernel, Kees Cook, Willy Tarreau

On 28.01.2022 22:47, Sean Christopherson wrote:
> On Fri, Jan 28, 2022, Maciej S. Szmigiero wrote:
>> From: "Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>
>>
>> Commit 7661809d493b ("mm: don't allow oversized kvmalloc() calls") has
>> forbidden using kvmalloc() to make allocations larger than INT_MAX (2 GiB).
>>
>> Unfortunately, adding a memslot exceeding 1 TiB in size will result in rmap
>> code trying to make an allocation exceeding this limit.
>> Besides failing this allocation, such operation will also trigger a
>> WARN_ON_ONCE() added by the aforementioned commit.
>>
>> Since we probably still want to use kernel slab for small rmap allocations
>> let's only redirect such oversized allocations to vmalloc.
>>
>> A possible alternative would be to add some kind of a __GFP_LARGE flag to
>> skip the INT_MAX check behind kvmalloc(), however this will impact the
>> common kernel memory allocation code, not just KVM.
> 
> Paolo has a cleaner fix for this[1][2], but it appears to have stalled out somewhere.
> 
> Paolo???
> 
> [1] https://lore.kernel.org/all/20211015165519.135670-1-pbonzini@redhat.com
> [2] https://lore.kernel.org/all/20211016064302.165220-1-pbonzini@redhat.com

So, what we do here?

Apparently the cleaner fix at [2] wasn't picked up despite Kees giving
it his "Reviewed-by".

Thanks,
Maciej

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

end of thread, other threads:[~2022-02-10 21:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-28 21:36 [PATCH] KVM: x86: Fix rmap allocation for very large memslots Maciej S. Szmigiero
2022-01-28 21:47 ` Sean Christopherson
2022-02-10 21:00   ` Maciej S. Szmigiero

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