From nobody Sun Feb 8 10:44:09 2026 Received: from air.basealt.ru (air.basealt.ru [194.107.17.39]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69E626DD1C; Fri, 26 Jan 2024 09:41:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.107.17.39 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706262065; cv=none; b=c15fgPDMm45QZUDzzXQZy4zuxA9996z6I1ZeN/uqqxa+nq7giKOO0dsUp77MDd7mxCXHVgq366yCXoCW9xBsyBYV3J0AEnmglAPWF7bkgFBEr83yW1nKWy7XzR3ELmbW4bMmWbTYlOgD2SWR4m6b1z0uEgLASZ6T5pcteOPHk4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706262065; c=relaxed/simple; bh=7/sPTPfkh5HHh0Y4qWFhRtQv2Pg/0WpZTZgfJ7Tl+dQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eF3gtyO3sLA/d8RwNgspE/AvYTtVrYjOnCQhlcXlca0bMEFJKyBx6ozHHFYq2NEd3HX8UpB+d6XeTVV9mgkcbO7eP7rvsYWfhBqrl2/S1JeZkAnMz/CLmveaFVXYt065d7TP5KW+vIbZ+foGnppSLU+5vaaANRmuscE7H1I7nRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org; spf=pass smtp.mailfrom=altlinux.org; arc=none smtp.client-ip=194.107.17.39 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=altlinux.org Received: by air.basealt.ru (Postfix, from userid 490) id BE0392F2023C; Fri, 26 Jan 2024 09:40:53 +0000 (UTC) X-Spam-Level: Received: from shell.ipa.basealt.ru (unknown [176.12.98.74]) by air.basealt.ru (Postfix) with ESMTPSA id 0033E2F2024A; Fri, 26 Jan 2024 09:40:24 +0000 (UTC) From: oficerovas@altlinux.org To: oficerovas@altlinux.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: kovalev@altlinux.org Subject: [PATCH 1/2] mm: vmalloc: introduce array allocation functions Date: Fri, 26 Jan 2024 12:40:22 +0300 Message-ID: <20240126094023.2677376-2-oficerovas@altlinux.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240126094023.2677376-1-oficerovas@altlinux.org> References: <20240126094023.2677376-1-oficerovas@altlinux.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Alexander Ofitserov From: Paolo Bonzini commit a8749a35c399 ("mm: vmalloc: introduce array allocation functions") Linux has dozens of occurrences of vmalloc(array_size()) and vzalloc(array_size()). Allow to simplify the code by providing vmalloc_array and vcalloc, as well as the underscored variants that let the caller specify the GFP flags. Acked-by: Michal Hocko Signed-off-by: Paolo Bonzini Signed-off-by: Alexander Ofitserov --- include/linux/vmalloc.h | 5 +++++ mm/util.c | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 76dad53a410ac..0fd47f2f39eb0 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -112,6 +112,11 @@ extern void *__vmalloc_node_range(unsigned long size, = unsigned long align, void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_ma= sk, int node, const void *caller); =20 +extern void *__vmalloc_array(size_t n, size_t size, gfp_t flags); +extern void *vmalloc_array(size_t n, size_t size); +extern void *__vcalloc(size_t n, size_t size, gfp_t flags); +extern void *vcalloc(size_t n, size_t size); + extern void vfree(const void *addr); extern void vfree_atomic(const void *addr); =20 diff --git a/mm/util.c b/mm/util.c index 25bfda774f6fd..7fd3c2bb3e4f5 100644 --- a/mm/util.c +++ b/mm/util.c @@ -686,6 +686,56 @@ static inline void *__page_rmapping(struct page *page) return (void *)mapping; } =20 +/** + * __vmalloc_array - allocate memory for a virtually contiguous array. + * @n: number of elements. + * @size: element size. + * @flags: the type of memory to allocate (see kmalloc). + */ +void *__vmalloc_array(size_t n, size_t size, gfp_t flags) +{ + size_t bytes; + + if (unlikely(check_mul_overflow(n, size, &bytes))) + return NULL; + return __vmalloc(bytes, flags); +} +EXPORT_SYMBOL(__vmalloc_array); + +/** + * vmalloc_array - allocate memory for a virtually contiguous array. + * @n: number of elements. + * @size: element size. + */ +void *vmalloc_array(size_t n, size_t size) +{ + return __vmalloc_array(n, size, GFP_KERNEL); +} +EXPORT_SYMBOL(vmalloc_array); + +/** + * __vcalloc - allocate and zero memory for a virtually contiguous array. + * @n: number of elements. + * @size: element size. + * @flags: the type of memory to allocate (see kmalloc). + */ +void *__vcalloc(size_t n, size_t size, gfp_t flags) +{ + return __vmalloc_array(n, size, flags | __GFP_ZERO); +} +EXPORT_SYMBOL(__vcalloc); + +/** + * vcalloc - allocate and zero memory for a virtually contiguous array. + * @n: number of elements. + * @size: element size. + */ +void *vcalloc(size_t n, size_t size) +{ + return __vmalloc_array(n, size, GFP_KERNEL | __GFP_ZERO); +} +EXPORT_SYMBOL(vcalloc); + /* Neutral page->mapping pointer to address_space or anon_vma or other */ void *page_rmapping(struct page *page) { --=20 2.42.1 From nobody Sun Feb 8 10:44:09 2026 Received: from air.basealt.ru (air.basealt.ru [194.107.17.39]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB94C1B7F9; Fri, 26 Jan 2024 09:40:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=194.107.17.39 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706262062; cv=none; b=pXG5EWwSlt4cQADIXWQkqjvbWJJXc3S8Mj7tF19WjwHQk3pdmnjpEpdUn+aSGraRYFvFztoOcV3tFNRCmCg5gIfpbumCRLvklpToQW0U/SwnbjZVmfXfttjMGoLdUiUClOtOcX4GTG9/32dHjbxs0azcsKw0AQRcBAkDX5XkeUg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706262062; c=relaxed/simple; bh=TXRpBSVjCJUcc+Ou62Ur6MM+Di8M71iBf31d0N8EyxU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bCZ7edqunJ7PQ7NVv5rtxMJaXCLZCw1XX59qbHWdFQC/FoqjGO52QU87TQdIJEGiDUNgpp6oFTlTi7Xv5crzmp0URBa4yjT3t4R/jONTyQLBIDvp4cVkbK2Pz9n2ENRdeuSNZyes8X+cCpTv5xBYd1V8R4RZCn+Ky5yhg4xjfZI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org; spf=pass smtp.mailfrom=altlinux.org; arc=none smtp.client-ip=194.107.17.39 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=altlinux.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=altlinux.org Received: by air.basealt.ru (Postfix, from userid 490) id DA8152F2023B; Fri, 26 Jan 2024 09:40:50 +0000 (UTC) X-Spam-Level: Received: from shell.ipa.basealt.ru (unknown [176.12.98.74]) by air.basealt.ru (Postfix) with ESMTPSA id 17CBB2F2024B; Fri, 26 Jan 2024 09:40:25 +0000 (UTC) From: oficerovas@altlinux.org To: oficerovas@altlinux.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: kovalev@altlinux.org Subject: [PATCH 2/2] KVM: use __vcalloc for very large allocations Date: Fri, 26 Jan 2024 12:40:23 +0300 Message-ID: <20240126094023.2677376-3-oficerovas@altlinux.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240126094023.2677376-1-oficerovas@altlinux.org> References: <20240126094023.2677376-1-oficerovas@altlinux.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Alexander Ofitserov From: Paolo Bonzini commit 37b2a6510a48 ("KVM: use __vcalloc for very large allocations") Allocations whose size is related to the memslot size can be arbitrarily large. Do not use kvzalloc/kvcalloc, as those are limited to "not crazy" sizes that fit in 32 bits. URL: https://lore.kernel.org/lkml/20220711090606.962822924@linuxfoundation.= org/ Reviewed-by: David Hildenbrand Signed-off-by: Paolo Bonzini Signed-off-by: Alexander Ofitserov --- arch/powerpc/kvm/book3s_hv_uvmem.c | 2 +- arch/x86/kvm/mmu/page_track.c | 2 +- arch/x86/kvm/x86.c | 4 ++-- virt/kvm/kvm_main.c | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/powerpc/kvm/book3s_hv_uvmem.c b/arch/powerpc/kvm/book3s_h= v_uvmem.c index 3dd58b4ee33e5..5f6b3f80023de 100644 --- a/arch/powerpc/kvm/book3s_hv_uvmem.c +++ b/arch/powerpc/kvm/book3s_hv_uvmem.c @@ -250,7 +250,7 @@ int kvmppc_uvmem_slot_init(struct kvm *kvm, const struc= t kvm_memory_slot *slot) p =3D kzalloc(sizeof(*p), GFP_KERNEL); if (!p) return -ENOMEM; - p->pfns =3D vzalloc(array_size(slot->npages, sizeof(*p->pfns))); + p->pfns =3D vcalloc(slot->npages, sizeof(*p->pfns)); if (!p->pfns) { kfree(p); return -ENOMEM; diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c index 81cf4babbd0b4..3c379335ea477 100644 --- a/arch/x86/kvm/mmu/page_track.c +++ b/arch/x86/kvm/mmu/page_track.c @@ -35,7 +35,7 @@ int kvm_page_track_create_memslot(struct kvm_memory_slot = *slot, =20 for (i =3D 0; i < KVM_PAGE_TRACK_MAX; i++) { slot->arch.gfn_track[i] =3D - kvcalloc(npages, sizeof(*slot->arch.gfn_track[i]), + __vcalloc(npages, sizeof(*slot->arch.gfn_track[i]), GFP_KERNEL_ACCOUNT); if (!slot->arch.gfn_track[i]) goto track_free; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 13e4699a0744f..6c2bf7cd7aec6 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10826,14 +10826,14 @@ static int kvm_alloc_memslot_metadata(struct kvm_= memory_slot *slot, slot->base_gfn, level) + 1; =20 slot->arch.rmap[i] =3D - kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), + __vcalloc(lpages, sizeof(*slot->arch.rmap[i]), GFP_KERNEL_ACCOUNT); if (!slot->arch.rmap[i]) goto out_free; if (i =3D=3D 0) continue; =20 - linfo =3D kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); + linfo =3D __vcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); if (!linfo) goto out_free; =20 diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 356fd5d1a4285..b7638c3c9eb7d 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1008,9 +1008,9 @@ static int kvm_vm_release(struct inode *inode, struct= file *filp) */ static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot) { - unsigned long dirty_bytes =3D 2 * kvm_dirty_bitmap_bytes(memslot); + unsigned long dirty_bytes =3D kvm_dirty_bitmap_bytes(memslot); =20 - memslot->dirty_bitmap =3D kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT); + memslot->dirty_bitmap =3D __vcalloc(2, dirty_bytes, GFP_KERNEL_ACCOUNT); if (!memslot->dirty_bitmap) return -ENOMEM; =20 --=20 2.42.1