Received: by 2002:a05:6a10:8a4f:0:0:0:0 with SMTP id dn15csp2600725pxb; Mon, 31 Jan 2022 03:40:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJxfIxVVccV7/9an2xsqDc5FxvUHZXvyeGy/MIeEk/1wqoLFwSrl14sIQpn7nMlDgPhZCmKy X-Received: by 2002:a17:90b:3841:: with SMTP id nl1mr23665861pjb.24.1643629251312; Mon, 31 Jan 2022 03:40:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643629251; cv=none; d=google.com; s=arc-20160816; b=t738q5TP32QWBsh/e6glT/oc7BJOz+hYn31tlXfVBAxMbWn1M3Si9wekFvVoPNlflF /Lez9PL7BxMr8UjOFXBGPNsa1A1+Myb2YQM8ynjREsj1BY0sFTvvnIeK0QP0eXG9U6RI p8w+JHKcp4zFsqRBK2aYAR2ANAO+vuyfO5pjFA+mhg9MS+kaWy5FXV4TlOyD7o7mczQz BfzAqc86CDrbxGqZkcJZ6pxn6otQVbqvbDTPfkNZWVxJZhR6h9mZOHWIhZEHTWlqZF4b yvu4g4rivU6N+RomjTPVQ9g9gch42pN4yVjbfGLdl2iMxpgWyj1ZqRmLC1DmAbZ7E5aq gQJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=qqTtCpBWqdxTWnoFxBEeev4vx/d5hQhJ8aaqXuDkk1g=; b=IVEF8eHzcklseLXlX3VnP95RcQ/bYHmHytD6rj14gkZljyRoisPFHJGI0k5m0tok83 jXKf7WgZUGqnUzQDJXQfo6kZHa+EXr3APFHrHQFjGBTsvMdahvt5WLbQV8REm8NRTSfu 2RX2lCbTVSgPGQ85NPdm3WZkyJ7oMUsPARoyJ+0Eh4DnZT21/z4dHBdARBywCU7kaOUj jrdB9cFWbtvCj3my95xGjGBdUi1y1GjVUG/wiQkI7pac1aG0bjnJdvVMmQp9Lk4mcvWR r+zf/M/AVt6YbhsI+AwhmGW8fqZ/Na27TS1WdB/N00fy6wuZ21uL6c3mng/tReDLs8w9 RKSw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ij25si12224114plb.514.2022.01.31.03.40.40; Mon, 31 Jan 2022 03:40:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351504AbiA1VhH (ORCPT + 99 others); Fri, 28 Jan 2022 16:37:07 -0500 Received: from vps-vb.mhejs.net ([37.28.154.113]:34544 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347668AbiA1VhF (ORCPT ); Fri, 28 Jan 2022 16:37:05 -0500 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nDYvI-0008TI-5c; Fri, 28 Jan 2022 22:36:48 +0100 From: "Maciej S. Szmigiero" To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Michal Hocko , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] KVM: x86: Fix rmap allocation for very large memslots Date: Fri, 28 Jan 2022 22:36:42 +0100 Message-Id: <1acaee7fa7ef7ab91e51f4417572b099caf2f400.1643405658.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Maciej S. Szmigiero" 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 --- 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,