Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp507627rdb; Fri, 26 Jan 2024 02:18:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IEx2O5iBjhVA38EJqD1qx/bUGQLEVnwlc45itlS1PCqcsyujsqCxI7uDQufplUwFfHXEzsQ X-Received: by 2002:a17:906:88d:b0:a2d:2378:3396 with SMTP id n13-20020a170906088d00b00a2d23783396mr514455eje.3.1706264328200; Fri, 26 Jan 2024 02:18:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706264328; cv=pass; d=google.com; s=arc-20160816; b=olWHle7a27HrX25UILo5LPG17aFztXTFwe6pWp2AZVj61qzCbCN/VRSrliDAKzK7Bi cdq1k2R3ZmedYxc/XnWfScLI8rNgGwL5jsjEyEMFT5bJnC0C3iyZvkXd7SalxVjFIxod bMGPJu1f/lYoeOoIpF2i1lBRuRMvCr8DqHnYaiVcoYA546szjHbQ0xcLNjVBO3Z94Qv5 Qam2OBRTeE+Ld+PGPGfVG1L+DBKbUUCcJGYZ3gdplMlG+nbk3B+vfagziApAZRB23/0X lnSg3TguA/PCdNrEdmJnU4AE2mJvj1bKg5UsN/VuSG31PFGiMt4s4zMdxZq9MZF+H56n jJsA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=TUNSqgyX//RHkRl8fbYjvjtYYU5j8fStf+SgMudzE1M=; fh=10drZrV4RvU/HdzZ5olGYr65ukKIXS5J6sTo3dhHKmg=; b=KzvMRBHQZln7HzPAbWfl/weaNcgvQnGvF2Xk/fTWyMF9UyUfYWczNLxPfhqaill4QB FqyrJVq24h32tBgeG83lpulgcmGJxW0idl7LKlACyFD5YpaRE0YpTy1UDfes0S54KL++ GNJ+Ngsrhe0kKoyTPhN4f6vFFuMvuNw7wKek8g12ls2m9guJ/5hxZN9yHkIH9HURZoIE U36WXMI8JbQIzcY4WwEdKv6d5E7e+69xvorQogCauVlQzEgm5UnDLXc4PtUPaPv9BRZf Amgm9zS1z2A6Ve/RZSLXtu+jO0KD/5XBReCuVkUoky/L2I1QpfJ1VXozzPk3hY7l4i7i 0TzA== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=altlinux.org); spf=pass (google.com: domain of linux-kernel+bounces-39951-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39951-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id ch5-20020a170906c2c500b00a34e303e91esi317504ejb.621.2024.01.26.02.18.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jan 2024 02:18:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-39951-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=altlinux.org); spf=pass (google.com: domain of linux-kernel+bounces-39951-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-39951-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id ECF7A1F30E6E for ; Fri, 26 Jan 2024 10:18:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E164474E2F; Fri, 26 Jan 2024 09:41:05 +0000 (UTC) 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: 8bit 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_mask, int node, const void *caller); +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); 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; } +/** + * __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) { -- 2.42.1