From patchwork Tue Dec 7 09:46:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Yu X-Patchwork-Id: 12661457 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 420C4C433EF for ; Tue, 7 Dec 2021 09:47:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D1D86B0098; Tue, 7 Dec 2021 04:46:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 980BA6B0099; Tue, 7 Dec 2021 04:46:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 870E46B009A; Tue, 7 Dec 2021 04:46:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0192.hostedemail.com [216.40.44.192]) by kanga.kvack.org (Postfix) with ESMTP id 79E9D6B0098 for ; Tue, 7 Dec 2021 04:46:53 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 388A5180989FC for ; Tue, 7 Dec 2021 09:46:43 +0000 (UTC) X-FDA: 78890518686.22.D4EB8CB Received: from out4436.biz.mail.alibaba.com (out4436.biz.mail.alibaba.com [47.88.44.36]) by imf26.hostedemail.com (Postfix) with ESMTP id 0CD1220019C0 for ; Tue, 7 Dec 2021 09:46:41 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R241e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e01424;MF=xuyu@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0UzlQ6.C_1638870377; Received: from localhost(mailfrom:xuyu@linux.alibaba.com fp:SMTPD_---0UzlQ6.C_1638870377) by smtp.aliyun-inc.com(127.0.0.1); Tue, 07 Dec 2021 17:46:18 +0800 From: Xu Yu To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, npiggin@gmail.com Subject: [PATCH] mm/vmalloc: allocate small pages for area->pages Date: Tue, 7 Dec 2021 17:46:12 +0800 Message-Id: X-Mailer: git-send-email 2.20.1.2432.ga663e714 MIME-Version: 1.0 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 0CD1220019C0 X-Stat-Signature: atb6jhd185prr6sm3zf7je5xfss98aoa Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf26.hostedemail.com: domain of xuyu@linux.alibaba.com designates 47.88.44.36 as permitted sender) smtp.mailfrom=xuyu@linux.alibaba.com X-HE-Tag: 1638870401-889137 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The area->pages stores the struct pages allocated for vmalloc mappings. The allocated memory can be hugepage if arch has HAVE_ARCH_HUGE_VMALLOC set, while area->pages itself does not have to be hugepage backed. Suppose that we want to vmalloc 1026M of memory, then area->pages is 2052K in size, which is large than PMD_SIZE when the pagesize is 4K. Currently, 4096K will be allocated for area->pages, wherein 2044K is wasted. This introduces __vmalloc_node_no_huge, and makes area->pages backed by small pages, because I think to allocate hugepage for area->pages is unnecessary and vulnerable to abuse. Signed-off-by: Xu Yu --- include/linux/vmalloc.h | 2 ++ mm/vmalloc.c | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 6e022cc712e6..e93f39eb46a5 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -150,6 +150,8 @@ extern void *__vmalloc_node_range(unsigned long size, unsigned long align, const void *caller) __alloc_size(1); void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask, int node, const void *caller) __alloc_size(1); +void *__vmalloc_node_no_huge(unsigned long size, unsigned long align, + gfp_t gfp_mask, int node, const void *caller) __alloc_size(1); void *vmalloc_no_huge(unsigned long size) __alloc_size(1); extern void vfree(const void *addr); diff --git a/mm/vmalloc.c b/mm/vmalloc.c index d2a00ad4e1dd..0bdbb96d3e3f 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2925,17 +2925,18 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, unsigned long size = get_vm_area_size(area); unsigned long array_size; unsigned int nr_small_pages = size >> PAGE_SHIFT; + unsigned int max_small_pages = ALIGN(size, 1UL << page_shift) >> PAGE_SHIFT; unsigned int page_order; - array_size = (unsigned long)nr_small_pages * sizeof(struct page *); + array_size = (unsigned long)max_small_pages * sizeof(struct page *); gfp_mask |= __GFP_NOWARN; if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) gfp_mask |= __GFP_HIGHMEM; /* Please note that the recursion is strictly bounded. */ if (array_size > PAGE_SIZE) { - area->pages = __vmalloc_node(array_size, 1, nested_gfp, node, - area->caller); + area->pages = __vmalloc_node_no_huge(array_size, 1, nested_gfp, + node, area->caller); } else { area->pages = kmalloc_node(array_size, nested_gfp, node); } @@ -3114,6 +3115,14 @@ void *__vmalloc_node(unsigned long size, unsigned long align, return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, gfp_mask, PAGE_KERNEL, 0, node, caller); } + +void *__vmalloc_node_no_huge(unsigned long size, unsigned long align, + gfp_t gfp_mask, int node, const void *caller) +{ + return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END, + gfp_mask, PAGE_KERNEL, VM_NO_HUGE_VMAP, node, caller); +} + /* * This is only for performance analysis of vmalloc and stress purpose. * It is required by vmalloc test module, therefore do not use it other