From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.3 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5DEAFC433DF for ; Tue, 25 Aug 2020 14:59:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 06582207D3 for ; Tue, 25 Aug 2020 14:59:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G4/MMiKI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 06582207D3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9C8038E0019; Tue, 25 Aug 2020 10:59:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 99E8A8D0001; Tue, 25 Aug 2020 10:59:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 865308E0019; Tue, 25 Aug 2020 10:59:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 6EA888D0001 for ; Tue, 25 Aug 2020 10:59:00 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 359E91EE6 for ; Tue, 25 Aug 2020 14:59:00 +0000 (UTC) X-FDA: 77189398440.09.flag95_341287b2705c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 0655B180AD801 for ; Tue, 25 Aug 2020 14:58:59 +0000 (UTC) X-HE-Tag: flag95_341287b2705c X-Filterd-Recvd-Size: 16164 Received: from mail-pj1-f66.google.com (mail-pj1-f66.google.com [209.85.216.66]) by imf07.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Aug 2020 14:58:59 +0000 (UTC) Received: by mail-pj1-f66.google.com with SMTP id g6so1376096pjl.0 for ; Tue, 25 Aug 2020 07:58:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6KJeQP8NI0Gmq28+Z9mGp8SWZDiLwVnET1WJkFSU9fg=; b=G4/MMiKI3zAB9GZaRHWlNv9lxtyaITj1HOneVEnczu7qEBJuf+V670gslFcluD9DfV dKFhpN/WL4LNfDDfqNgVKdmCkWZtOQD8AR87AfG5JuTiroU9qsz6dQkIQISu8fQcNQpX brfM6EH/XQVEv4CyH06jAc2CUPxwD6+60MbRIOyzUHJi5XVsEEe8gkMYcR1WSQYp9r5u XP+Rkwj4ZIVDgOOaCKn9ONyOoYAOP4aBBQ9Gg8T/6H+kHiTY60o59ZUHO0d0sqG7Rd4P uU1lQsBRXasq3jcdCOP+kdgE/+A2BLKF21czW067Wh8qyrY+uN2Dd8I5PEt6zRFb/3NR QL3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6KJeQP8NI0Gmq28+Z9mGp8SWZDiLwVnET1WJkFSU9fg=; b=hBK94iohxEBacZdY0LT/2uwz/l6AIFZslVqhzTfCByOs4QJVcaORYCoIGd2+FIxX4o NlHhc8wcCSsn4S5ZMpP7oV+wMBw6TYcHctpUugut3KwFbk8ZQCrp5VGPA4qm9deetmXT WZZ74feJkyYA3zsNu290UMAVXNkzzBtuvCPglxELlapY3iO6cDWVunaebWB/LZXV6qHV OGD02kN9O1HCHCy/NpHy5DgI/vusLWGTL7hGJU33Q/b+4Sr55I30YdvBjkXXtdEQHSYt 5Ux38+KQv+OaRT8GAiILTSQq9j/dVu753wgKVS2DoQLuz/L06Di+cmUZwi4hAqLS7kjw AdAA== X-Gm-Message-State: AOAM532DTfHWFq4BDZtyaHjfHMfNL5NmXxu8ILggiFydKPAinhvUfwoC h2S4hUMB6FtSVlGei1GSkIu5ywmO5ZY= X-Google-Smtp-Source: ABdhPJwxOdeZP69xC+JAEexbE9+iKYE0HYeXxKfVEDE0GXMN513IqNTakJmuHRjF51c8KGciI3/RNg== X-Received: by 2002:a17:90a:1e65:: with SMTP id w92mr1861914pjw.187.1598367538048; Tue, 25 Aug 2020 07:58:58 -0700 (PDT) Received: from bobo.ozlabs.ibm.com (61-68-212-105.tpgi.com.au. [61.68.212.105]) by smtp.gmail.com with ESMTPSA id e29sm15755956pfj.92.2020.08.25.07.58.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2020 07:58:57 -0700 (PDT) From: Nicholas Piggin To: linux-mm@kvack.org, Andrew Morton Cc: Nicholas Piggin , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Zefan Li , Jonathan Cameron , Christoph Hellwig , Christophe Leroy Subject: [PATCH v7 11/12] mm/vmalloc: Hugepage vmalloc mappings Date: Wed, 26 Aug 2020 00:57:52 +1000 Message-Id: <20200825145753.529284-12-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200825145753.529284-1-npiggin@gmail.com> References: <20200825145753.529284-1-npiggin@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0655B180AD801 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 Content-Transfer-Encoding: quoted-printable 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: Support huge page vmalloc mappings. Config option HAVE_ARCH_HUGE_VMALLOC enables support on architectures that define HAVE_ARCH_HUGE_VMAP and supports PMD sized vmap mappings. vmalloc will attempt to allocate PMD-sized pages if allocating PMD size o= r larger, and fall back to small pages if that was unsuccessful. Allocations that do not use PAGE_KERNEL prot are not permitted to use hug= e pages, because not all callers expect this (e.g., module allocations vs strict module rwx). This reduces TLB misses by nearly 30x on a `git diff` workload on a 2-nod= e POWER9 (59,800 -> 2,100) and reduces CPU cycles by 0.54%. This can result in more internal fragmentation and memory overhead for a given allocation, an option nohugevmalloc is added to disable at boot. Signed-off-by: Nicholas Piggin --- arch/Kconfig | 4 + include/linux/vmalloc.h | 1 + mm/page_alloc.c | 5 +- mm/vmalloc.c | 180 ++++++++++++++++++++++++++++++---------- 4 files changed, 145 insertions(+), 45 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index af14a567b493..b2b89d629317 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -616,6 +616,10 @@ config HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD config HAVE_ARCH_HUGE_VMAP bool =20 +config HAVE_ARCH_HUGE_VMALLOC + depends on HAVE_ARCH_HUGE_VMAP + bool + config ARCH_WANT_HUGE_PMD_SHARE bool =20 diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 15adb9a14fb6..a7449064fe35 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -58,6 +58,7 @@ struct vm_struct { unsigned long size; unsigned long flags; struct page **pages; + unsigned int page_order; unsigned int nr_pages; phys_addr_t phys_addr; const void *caller; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 0e2bab486fea..b6427cc7b838 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -69,6 +69,7 @@ #include #include #include +#include =20 #include #include @@ -8102,6 +8103,7 @@ void *__init alloc_large_system_hash(const char *ta= blename, void *table =3D NULL; gfp_t gfp_flags; bool virt; + bool huge; =20 /* allow the kernel cmdline to have a say */ if (!numentries) { @@ -8169,6 +8171,7 @@ void *__init alloc_large_system_hash(const char *ta= blename, } else if (get_order(size) >=3D MAX_ORDER || hashdist) { table =3D __vmalloc(size, gfp_flags); virt =3D true; + huge =3D (find_vm_area(table)->page_order > 0); } else { /* * If bucketsize is not a power-of-two, we may free @@ -8185,7 +8188,7 @@ void *__init alloc_large_system_hash(const char *ta= blename, =20 pr_info("%s hash table entries: %ld (order: %d, %lu bytes, %s)\n", tablename, 1UL << log2qty, ilog2(size) - PAGE_SHIFT, size, - virt ? "vmalloc" : "linear"); + virt ? (huge ? "vmalloc hugepage" : "vmalloc") : "linear"); =20 if (_hash_shift) *_hash_shift =3D log2qty; diff --git a/mm/vmalloc.c b/mm/vmalloc.c index 1d6cad16bda3..8db53c2d7f72 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -44,6 +44,19 @@ #include "internal.h" #include "pgalloc-track.h" =20 +#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC +static bool __ro_after_init vmap_allow_huge =3D true; + +static int __init set_nohugevmalloc(char *str) +{ + vmap_allow_huge =3D false; + return 0; +} +early_param("nohugevmalloc", set_nohugevmalloc); +#else /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ +static const bool vmap_allow_huge =3D false; +#endif /* CONFIG_HAVE_ARCH_HUGE_VMALLOC */ + bool is_vmalloc_addr(const void *x) { unsigned long addr =3D (unsigned long)x; @@ -477,31 +490,12 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigne= d long addr, return 0; } =20 -/** - * map_kernel_range_noflush - map kernel VM area with the specified page= s - * @addr: start of the VM area to map - * @size: size of the VM area to map - * @prot: page protection flags to use - * @pages: pages to map - * - * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size specif= y should - * have been allocated using get_vm_area() and its friends. - * - * NOTE: - * This function does NOT do any cache flushing. The caller is responsi= ble for - * calling flush_cache_vmap() on to-be-mapped areas before calling this - * function. - * - * RETURNS: - * 0 on success, -errno on failure. - */ -int map_kernel_range_noflush(unsigned long addr, unsigned long size, - pgprot_t prot, struct page **pages) +static int vmap_small_pages_range_noflush(unsigned long addr, unsigned l= ong end, + pgprot_t prot, struct page **pages) { unsigned long start =3D addr; - unsigned long end =3D addr + size; - unsigned long next; pgd_t *pgd; + unsigned long next; int err =3D 0; int nr =3D 0; pgtbl_mod_mask mask =3D 0; @@ -523,6 +517,65 @@ int map_kernel_range_noflush(unsigned long addr, uns= igned long size, return 0; } =20 +static int vmap_pages_range_noflush(unsigned long addr, unsigned long en= d, + pgprot_t prot, struct page **pages, unsigned int page_shift) +{ + unsigned int i, nr =3D (end - addr) >> PAGE_SHIFT; + + WARN_ON(page_shift < PAGE_SHIFT); + + if (page_shift =3D=3D PAGE_SHIFT) + return vmap_small_pages_range_noflush(addr, end, prot, pages); + + for (i =3D 0; i < nr; i +=3D 1U << (page_shift - PAGE_SHIFT)) { + int err; + + err =3D vmap_range_noflush(addr, addr + (1UL << page_shift), + __pa(page_address(pages[i])), prot, + page_shift); + if (err) + return err; + + addr +=3D 1UL << page_shift; + } + + return 0; +} + +static int vmap_pages_range(unsigned long addr, unsigned long end, + pgprot_t prot, struct page **pages, unsigned int page_shift) +{ + int err; + + err =3D vmap_pages_range_noflush(addr, end, prot, pages, page_shift); + flush_cache_vmap(addr, end); + return err; +} + +/** + * map_kernel_range_noflush - map kernel VM area with the specified page= s + * @addr: start of the VM area to map + * @size: size of the VM area to map + * @prot: page protection flags to use + * @pages: pages to map + * + * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size specif= y should + * have been allocated using get_vm_area() and its friends. + * + * NOTE: + * This function does NOT do any cache flushing. The caller is responsi= ble for + * calling flush_cache_vmap() on to-be-mapped areas before calling this + * function. + * + * RETURNS: + * 0 on success, -errno on failure. + */ +int map_kernel_range_noflush(unsigned long addr, unsigned long size, + pgprot_t prot, struct page **pages) +{ + return vmap_pages_range_noflush(addr, addr + size, prot, pages, PAGE_SH= IFT); +} + int map_kernel_range(unsigned long start, unsigned long size, pgprot_t p= rot, struct page **pages) { @@ -2400,6 +2453,7 @@ static inline void set_area_direct_map(const struct= vm_struct *area, { int i; =20 + /* HUGE_VMALLOC passes small pages to set_direct_map */ for (i =3D 0; i < area->nr_pages; i++) if (page_address(area->pages[i])) set_direct_map(area->pages[i]); @@ -2433,11 +2487,12 @@ static void vm_remove_mappings(struct vm_struct *= area, int deallocate_pages) * map. Find the start and end range of the direct mappings to make sur= e * the vm_unmap_aliases() flush includes the direct map. */ - for (i =3D 0; i < area->nr_pages; i++) { + for (i =3D 0; i < area->nr_pages; i +=3D 1U << area->page_order) { unsigned long addr =3D (unsigned long)page_address(area->pages[i]); if (addr) { + unsigned long page_size =3D PAGE_SIZE << area->page_order; start =3D min(addr, start); - end =3D max(addr + PAGE_SIZE, end); + end =3D max(addr + page_size, end); flush_dmap =3D 1; } } @@ -2480,11 +2535,11 @@ static void __vunmap(const void *addr, int deallo= cate_pages) if (deallocate_pages) { int i; =20 - for (i =3D 0; i < area->nr_pages; i++) { + for (i =3D 0; i < area->nr_pages; i +=3D 1U << area->page_order) { struct page *page =3D area->pages[i]; =20 BUG_ON(!page); - __free_pages(page, 0); + __free_pages(page, area->page_order); } atomic_long_sub(area->nr_pages, &nr_vmalloc_pages); =20 @@ -2623,9 +2678,12 @@ void *vmap(struct page **pages, unsigned int count= , EXPORT_SYMBOL(vmap); =20 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, - pgprot_t prot, int node) + pgprot_t prot, unsigned int page_shift, int node) { struct page **pages; + unsigned long addr =3D (unsigned long)area->addr; + unsigned long size =3D get_vm_area_size(area); + unsigned int page_order =3D page_shift - PAGE_SHIFT; unsigned int nr_pages, array_size, i; const gfp_t nested_gfp =3D (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; const gfp_t alloc_mask =3D gfp_mask | __GFP_NOWARN; @@ -2633,7 +2691,7 @@ static void *__vmalloc_area_node(struct vm_struct *= area, gfp_t gfp_mask, 0 : __GFP_HIGHMEM; =20 - nr_pages =3D get_vm_area_size(area) >> PAGE_SHIFT; + nr_pages =3D size >> PAGE_SHIFT; array_size =3D (nr_pages * sizeof(struct page *)); =20 /* Please note that the recursion is strictly bounded. */ @@ -2652,29 +2710,29 @@ static void *__vmalloc_area_node(struct vm_struct= *area, gfp_t gfp_mask, =20 area->pages =3D pages; area->nr_pages =3D nr_pages; + area->page_order =3D page_order; =20 - for (i =3D 0; i < area->nr_pages; i++) { + for (i =3D 0; i < area->nr_pages; i +=3D 1U << page_order) { struct page *page; + int p; =20 - if (node =3D=3D NUMA_NO_NODE) - page =3D alloc_page(alloc_mask|highmem_mask); - else - page =3D alloc_pages_node(node, alloc_mask|highmem_mask, 0); - + page =3D alloc_pages_node(node, alloc_mask|highmem_mask, page_order); if (unlikely(!page)) { /* Successfully allocated i pages, free them in __vunmap() */ area->nr_pages =3D i; atomic_long_add(area->nr_pages, &nr_vmalloc_pages); goto fail; } - area->pages[i] =3D page; + + for (p =3D 0; p < (1U << page_order); p++) + area->pages[i + p] =3D page + p; + if (gfpflags_allow_blocking(gfp_mask)) cond_resched(); } atomic_long_add(area->nr_pages, &nr_vmalloc_pages); =20 - if (map_kernel_range((unsigned long)area->addr, get_vm_area_size(area), - prot, pages) < 0) + if (vmap_pages_range(addr, addr + size, prot, pages, page_shift) < 0) goto fail; =20 return area->addr; @@ -2682,7 +2740,7 @@ static void *__vmalloc_area_node(struct vm_struct *= area, gfp_t gfp_mask, fail: warn_alloc(gfp_mask, NULL, "vmalloc: allocation failure, allocated %ld of %ld bytes", - (area->nr_pages*PAGE_SIZE), area->size); + (area->nr_pages*PAGE_SIZE), size); __vfree(area->addr); return NULL; } @@ -2713,19 +2771,42 @@ void *__vmalloc_node_range(unsigned long size, un= signed long align, struct vm_struct *area; void *addr; unsigned long real_size =3D size; + unsigned long real_align =3D align; + unsigned int shift =3D PAGE_SHIFT; =20 - size =3D PAGE_ALIGN(size); if (!size || (size >> PAGE_SHIFT) > totalram_pages()) goto fail; =20 - area =3D __get_vm_area_node(real_size, align, VM_ALLOC | VM_UNINITIALIZ= ED | + if (vmap_allow_huge && (pgprot_val(prot) =3D=3D pgprot_val(PAGE_KERNEL)= )) { + unsigned long size_per_node; + + /* + * Try huge pages. Only try for PAGE_KERNEL allocations, + * others like modules don't yet expect huge pages in + * their allocations due to apply_to_page_range not + * supporting them. + */ + + size_per_node =3D size; + if (node =3D=3D NUMA_NO_NODE) + size_per_node /=3D num_online_nodes(); + if (size_per_node >=3D PMD_SIZE) { + shift =3D PMD_SHIFT; + align =3D max(real_align, 1UL << shift); + size =3D ALIGN(real_size, 1UL << shift); + } + } + +again: + size =3D PAGE_ALIGN(size); + area =3D __get_vm_area_node(size, align, VM_ALLOC | VM_UNINITIALIZED | vm_flags, start, end, node, gfp_mask, caller); if (!area) goto fail; =20 - addr =3D __vmalloc_area_node(area, gfp_mask, prot, node); + addr =3D __vmalloc_area_node(area, gfp_mask, prot, shift, node); if (!addr) - return NULL; + goto fail; =20 /* * In this function, newly allocated vm_struct has VM_UNINITIALIZED @@ -2739,8 +2820,19 @@ void *__vmalloc_node_range(unsigned long size, uns= igned long align, return addr; =20 fail: - warn_alloc(gfp_mask, NULL, + if (shift > PAGE_SHIFT) { + free_vm_area(area); + shift =3D PAGE_SHIFT; + align =3D real_align; + size =3D real_size; + goto again; + } + + if (!area) { + /* Warn for area allocation, page allocations already warn */ + warn_alloc(gfp_mask, NULL, "vmalloc: allocation failure: %lu bytes", real_size); + } return NULL; } =20 @@ -3739,7 +3831,7 @@ static int s_show(struct seq_file *m, void *p) seq_printf(m, " %pS", v->caller); =20 if (v->nr_pages) - seq_printf(m, " pages=3D%d", v->nr_pages); + seq_printf(m, " pages=3D%d order=3D%d", v->nr_pages, v->page_order); =20 if (v->phys_addr) seq_printf(m, " phys=3D%pa", &v->phys_addr); --=20 2.23.0