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=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,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 7C513C433FE for ; Sat, 5 Dec 2020 06:58:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1887022287 for ; Sat, 5 Dec 2020 06:58:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1887022287 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 A2BD26B0036; Sat, 5 Dec 2020 01:58:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9DBE46B007B; Sat, 5 Dec 2020 01:58:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8553A6B007D; Sat, 5 Dec 2020 01:58:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0145.hostedemail.com [216.40.44.145]) by kanga.kvack.org (Postfix) with ESMTP id 669646B0036 for ; Sat, 5 Dec 2020 01:58:46 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 2C93F363C for ; Sat, 5 Dec 2020 06:58:46 +0000 (UTC) X-FDA: 77558325852.15.hen01_0114cdb273cb Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin15.hostedemail.com (Postfix) with ESMTP id 0E06C1814B0C1 for ; Sat, 5 Dec 2020 06:58:46 +0000 (UTC) X-HE-Tag: hen01_0114cdb273cb X-Filterd-Recvd-Size: 17896 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf08.hostedemail.com (Postfix) with ESMTP for ; Sat, 5 Dec 2020 06:58:45 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id i38so4946641pgb.5 for ; Fri, 04 Dec 2020 22:58:45 -0800 (PST) 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=lRZepDZOFk10bQegSr670rdh7UW+D3+yhfDJQ0KUAZI=; b=o+Zc9b/1KrMxlVfGkFkl+BbssxM7bQc350ndlo/QIX05kStVvt5mo+8yObCo/dHBvQ Jv0EgCQd08L+XQUazZ83zn9VYQHgz1wz1o8cPfikxWN1dyHtqGoM7Z+LU1TAbtCV6qVy 1bE8x+InISioqdO8fu85kwFrFpItaQjhT0rF435p3kq/iB9/y0CU6VrXy8DSUY4Euir6 jTSkMpQHalnod8tOwjgiKtcmd1eMgg/LdeUNJAbKckDivjVmy2Vx2kZRzvCvfl5xpnCu o8OWH0y2zk1WQ7zlN/Hy5kNTuW0kFq7M7pTQ5WYxAoQlHSEMa2f0aWISX3pE9riF0Gj8 oSkg== 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=lRZepDZOFk10bQegSr670rdh7UW+D3+yhfDJQ0KUAZI=; b=PeNKGnwoDoo/wr8Ulbd/BYsuEI3Ik/vVd9cUGGznAQZ77LuBxvWTGBzHRACyaG7t77 VrMopMRAXd95epIXhf1PqkKSdanMqKFH5ilX4OB0JlPrYWc4798T+F+QjW0/uGURIUO3 9eAvLjVkN49ixi5/bG+tmp64w8gQJG5KsejQSPgFDZqaVB9dNtDpW4dPmNaYQpM+tdDK GAq0XxAhcYubVps8IhqCnW4AI+cXMsrH8IBLMNE1Tf1HJRIam9J9/pxwHGCLmfxMKXNR NnP69TkEmGyletM5WYCse25Ai+UV5GKXWSlDEAr9CzDoTUMfKU1Db+gTVxmer+OQFbqN H0Jw== X-Gm-Message-State: AOAM533Gy9YJnK9ad5Pa+ALfK58G38h9J9rw1xB3bDF2AXA1KNrpknQI HneQBvrfFvCaRVxFMdFQJSHUwqDZ3cEozg== X-Google-Smtp-Source: ABdhPJxrb67x3Gnv7/5cTUmtLQdbJLP6bJ+o2x3Seddk+PHjS9PwEyBkBrfa1SKhM5AYAvpqa12zMw== X-Received: by 2002:a63:4c5d:: with SMTP id m29mr1006255pgl.368.1607151524320; Fri, 04 Dec 2020 22:58:44 -0800 (PST) Received: from bobo.ozlabs.ibm.com ([1.129.145.238]) by smtp.gmail.com with ESMTPSA id a14sm1110848pfl.141.2020.12.04.22.58.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2020 22:58:43 -0800 (PST) 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 , Rick Edgecombe Subject: [PATCH v9 11/12] mm/vmalloc: Hugepage vmalloc mappings Date: Sat, 5 Dec 2020 16:57:24 +1000 Message-Id: <20201205065725.1286370-12-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20201205065725.1286370-1-npiggin@gmail.com> References: <20201205065725.1286370-1-npiggin@gmail.com> MIME-Version: 1.0 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 or larger, and fall back to small pages if that was unsuccessful. Architectures must ensure that any arch specific vmalloc allocations that require PAGE_SIZE mappings (e.g., module allocations vs strict module rwx) use the VM_NOHUGE flag to inhibit larger mappings. When hugepage vmalloc mappings are enabled in the next patch, this reduces TLB misses by nearly 30x on a `git diff` workload on a 2-node 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 | 10 +++ include/linux/vmalloc.h | 18 ++++ mm/page_alloc.c | 5 +- mm/vmalloc.c | 191 ++++++++++++++++++++++++++++++---------- 4 files changed, 178 insertions(+), 46 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index 56b6ccc0e32d..d8f056fc27b4 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -662,6 +662,16 @@ 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 + help + Archs that select this would be capable of PMD-sized vmaps (i.e., + arch_vmap_pmd_supported() returns true), and they must make no + assumptions that vmalloc memory is mapped with PAGE_SIZE ptes. The + VM_NOHUGE flag can be used to prohibit arch-specific allocations from + using hugepages to help with this (e.g., modules may require it). + config ARCH_WANT_HUGE_PMD_SHARE bool =20 diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index a5ae791dc1e0..db018b531745 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -25,6 +25,7 @@ struct notifier_block; /* in notifier.h */ #define VM_NO_GUARD 0x00000040 /* don't add guard page */ #define VM_KASAN 0x00000080 /* has allocated kasan shadow memory *= / #define VM_MAP_PUT_PAGES 0x00000100 /* put pages and free array in vfree= */ +#define VM_NOHUGE 0x00000200 /* force PAGE_SIZE pte mapping */ =20 /* * VM_KASAN is used slighly differently depending on CONFIG_KASAN_VMALLO= C. @@ -59,6 +60,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; @@ -196,6 +198,18 @@ static inline void set_vm_flush_reset_perms(void *ad= dr) if (vm) vm->flags |=3D VM_FLUSH_RESET_PERMS; } + +static inline bool is_vm_area_hugepages(const void *addr) +{ + /* + * This may not 100% tell if the area is mapped with > PAGE_SIZE + * page table entries, if for some reason the architecture indicates + * larger sizes are available but decides not to use them, nothing + * prevents that. This only indicates the size of the physical page + * allocated in the vmalloc layer. + */ + return (find_vm_area(addr)->page_order > 0); +} #else static inline int map_kernel_range_noflush(unsigned long start, unsigned long size, @@ -212,6 +226,10 @@ unmap_kernel_range_noflush(unsigned long addr, unsig= ned long size) static inline void set_vm_flush_reset_perms(void *addr) { } +static inline bool is_vm_area_hugepages(const void *addr) +{ + return false; +} #endif =20 /* for /dev/kmem */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index eaa227a479e4..d907da0ad349 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -70,6 +70,7 @@ #include #include #include +#include =20 #include #include @@ -8171,6 +8172,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) { @@ -8238,6 +8240,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 is_vm_area_hugepages(table); } else { /* * If bucketsize is not a power-of-two, we may free @@ -8254,7 +8257,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 ee9c3bee67f5..3800380b474f 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -42,6 +42,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 @@ -2674,12 +2729,17 @@ EXPORT_SYMBOL_GPL(vmap_pfn); #endif /* CONFIG_VMAP_PFN */ =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) { const gfp_t nested_gfp =3D (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO; - unsigned int nr_pages =3D get_vm_area_size(area) >> PAGE_SHIFT; - unsigned int array_size =3D nr_pages * sizeof(struct page *), i; + unsigned int page_order =3D page_shift - PAGE_SHIFT; + unsigned long addr =3D (unsigned long)area->addr; + unsigned long size =3D get_vm_area_size(area); + unsigned int nr_small_pages =3D size >> PAGE_SHIFT; + unsigned int array_size =3D nr_small_pages * sizeof(struct page *); struct page **pages; + unsigned int i; =20 gfp_mask |=3D __GFP_NOWARN; if (!(gfp_mask & (GFP_DMA | GFP_DMA32))) @@ -2700,30 +2760,35 @@ 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->nr_pages =3D nr_small_pages; + area->page_order =3D page_order; =20 - for (i =3D 0; i < area->nr_pages; i++) { + /* + * Careful, we allocate and map page_order pages, but tracking is done + * per PAGE_SIZE page so as to keep the vm_struct APIs independent of + * the physical/mapped size. + */ + 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(gfp_mask); - else - page =3D alloc_pages_node(node, gfp_mask, 0); - + page =3D alloc_pages_node(node, gfp_mask, page_order); if (unlikely(!page)) { /* Successfully allocated i pages, free them in __vfree() */ 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; @@ -2731,7 +2796,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; } @@ -2762,19 +2827,44 @@ 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 && !(vm_flags & VM_NOHUGE) && + arch_vmap_pmd_supported(prot) && + (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 @@ -2788,8 +2878,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 --=20 2.23.0