Free some vmemmap pages of hugetlb page
Subsystems (9)
v4 cover Free some vmemmap pages of hugetlb page
Hi all,
This patch series will free some vmemmap pages(struct page structures) associated with each hugetlbpage when preallocated to save memory.
Nowadays we track the status of physical page frames using struct page structures arranged in one or more arrays. And here exists one-to-one mapping between the physical page frame and the corresponding struct page structure.
The HugeTLB support is built on top of multiple page size support that is provided by most modern architectures. For example, x86 CPUs normally support 4K and 2M (1G if architecturally supported) page sizes. Every HugeTLB has more than one struct page structure. The 2M HugeTLB has 512 struct page structure and 1G HugeTLB has 4096 struct page structures. But in the core of HugeTLB only uses the first 4 (Use of first 4 struct page structures comes from HUGETLB_CGROUP_MIN_ORDER.) struct page structures to store metadata associated with each HugeTLB. The rest of the struct page structures are usually read the compound_head field which are all the same value. If we can free some struct page memory to buddy system so that we can save a lot of memory.
When the system boot up, every 2M HugeTLB has 512 struct page structures which size is 8 pages(sizeof(struct page) * 512 / PAGE_SIZE).
hugetlbpage struct pages(8 pages) page frame(8 pages)
+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | | 1 | -------------> | 1 | | | | 2 | -------------> | 2 | | | | 3 | -------------> | 3 | | | | 4 | -------------> | 4 | | 2M | | 5 | -------------> | 5 | | | | 6 | -------------> | 6 | | | | 7 | -------------> | 7 | | | +-----------+ +-----------+ | | | | +-----------+
When a hugetlbpage is preallocated, we can change the mapping from above to bellow.
hugetlbpage struct pages(8 pages) page frame(8 pages)
+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | | 1 | -------------> | 1 | | | | 2 | -------------> +-----------+ | | | 3 | -----------------^ ^ ^ ^ ^ | | | 4 | -------------------+ | | | | 2M | | 5 | ---------------------+ | | | | | 6 | -----------------------+ | | | | 7 | -------------------------+ | | +-----------+ | | | | +-----------+
For tail pages, the value of compound_head is the same. So we can reuse first page of tail page structs. We map the virtual addresses of the remaining 6 pages of tail page structs to the first tail page struct, and then free these 6 pages. Therefore, we need to reserve at least 2 pages as vmemmap areas.
When a hugetlbpage is freed to the buddy system, we should allocate six pages for vmemmap pages and restore the previous mapping relationship.
If we uses the 1G hugetlbpage, we can save 4088 pages(There are 4096 pages for struct page structures, we reserve 2 pages for vmemmap and 8 pages for page tables. So we can save 4088 pages). This is a very substantial gain. On our server, run some SPDK/QEMU applications which will use 1024GB hugetlbpage. With this feature enabled, we can save ~16GB(1G hugepage)/~11GB(2MB hugepage) memory.
Because there are vmemmap page tables reconstruction on the freeing/allocating path, it increases some overhead. Here are some overhead analysis.
1) Allocating 10240 2MB hugetlb pages.
a) With this patch series applied:
# time echo 10240 > /proc/sys/vm/nr_hugepages
real 0m0.166s user 0m0.000s sys 0m0.166s
# bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; } kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency = hist(nsecs - @start[tid]); delete(@start[tid]); }'
Attaching 2 probes...@latency:
[8K, 16K) 8360 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [16K, 32K) 1868 |@@@@@@@@@@@ | [32K, 64K) 10 | | [64K, 128K) 2 | |
b) Without this patch series:
# time echo 10240 > /proc/sys/vm/nr_hugepages
real 0m0.066s user 0m0.000s sys 0m0.066s
# bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; } kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency = hist(nsecs - @start[tid]); delete(@start[tid]); }'
Attaching 2 probes...@latency:
[4K, 8K) 10176 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [8K, 16K) 62 | | [16K, 32K) 2 | |
Summarize: this feature is about ~2x slower than before.
2) Freeing 10240 @MB hugetlb pages.
a) With this patch series applied:
# time echo 0 > /proc/sys/vm/nr_hugepages
real 0m0.004s user 0m0.000s sys 0m0.002s
# bpftrace -e 'kprobe:__free_hugepage { @start[tid] = nsecs; } kretprobe:__free_hugepage /@start[tid]/ { @latency = hist(nsecs - @start[tid]); delete(@start[tid]); }'
Attaching 2 probes... @latency:
[16K, 32K) 10240 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
b) Without this patch series:
# time echo 0 > /proc/sys/vm/nr_hugepages
real 0m0.077s user 0m0.001s sys 0m0.075s
# bpftrace -e 'kprobe:__free_hugepage { @start[tid] = nsecs; } kretprobe:__free_hugepage /@start[tid]/ { @latency = hist(nsecs - @start[tid]); delete(@start[tid]); }'
Attaching 2 probes...@latency:
[4K, 8K) 9950 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [8K, 16K) 287 |@ | [16K, 32K) 3 | |
Summarize: The overhead of __free_hugepage is about ~2-4x slower than before.
But according to the allocation test above, I think that here is also ~2x slower than before.
But why the 'real' time of patched is smaller than before? Because In this patch series, the freeing hugetlb is asynchronous(through kwoker).
Although the overhead has increased, the overhead is not significant. Like MIke said, "However, remember that the majority of use cases create hugetlb pages at or shortly after boot time and add them to the pool. So, additional overhead is at pool creation time. There is no change to 'normal run time' operations of getting a page from or returning a page to the pool (think page fault/unmap)".
changelog in v4:
1. Move all the vmemmap functions to hugetlb_vmemmap.c.
2. Make the CONFIG_HUGETLB_PAGE_FREE_VMEMMAP default to y, if we want to disable this feature, we should disable it by a boot/kernel command line.
3. Remove vmemmap_pgtable_{init, deposit, withdraw}() helper functions.
4. Initialize page table lock for vmemmap through core_initcall mechanism.
Thanks for Mike and Oscar's suggestions.
changelog in v3:
1. Rename some helps function name. Thanks Mike.
2. Rework some code. Thanks Mike and Oscar.
3. Remap the tail vmemmap page with PAGE_KERNEL_RO instead of PAGE_KERNEL. Thanks Matthew.
4. Add some overhead analysis in the cover letter.
5. Use vmemap pmd table lock instead of a hugetlb specific global lock.
changelog in v2:
1. Fix do not call dissolve_compound_page in alloc_huge_page_vmemmap().
2. Fix some typo and code style problems.
3. Remove unused handle_vmemmap_fault().
4. Merge some commits to one commit suggested by Mike.
Muchun Song (21):
mm/memory_hotplug: Move bootmem info registration API to
bootmem_info.c
mm/memory_hotplug: Move {get,put}_page_bootmem() to bootmem_info.c mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate mm/hugetlb: Introduce pgtable allocation/freeing helpers mm/bootmem_info: Introduce {free,prepare}_vmemmap_page() mm/bootmem_info: Combine bootmem info and type into page->freelist mm/hugetlb: Initialize page table lock for vmemmap mm/hugetlb: Free the vmemmap pages associated with each hugetlb page mm/hugetlb: Defer freeing of hugetlb pages
mm/hugetlb: Allocate the vmemmap pages associated with each hugetlb
page
mm/hugetlb: Introduce remap_huge_page_pmd_vmemmap helper mm/hugetlb: Use PG_slab to indicate split pmd
mm/hugetlb: Support freeing vmemmap pages of gigantic page mm/hugetlb: Set the PageHWPoison to the raw error page mm/hugetlb: Flush work when dissolving hugetlb page mm/hugetlb: Add a kernel parameter hugetlb_free_vmemmap mm/hugetlb: Merge pte to huge pmd only for gigantic page mm/hugetlb: Gather discrete indexes of tail page mm/hugetlb: Add BUILD_BUG_ON to catch invalid usage of tail struct
page
mm/hugetlb: Disable freeing vmemmap if struct page size is not power
of two
Documentation/admin-guide/kernel-parameters.txt | 9 + Documentation/admin-guide/mm/hugetlbpage.rst | 3 + arch/x86/include/asm/hugetlb.h | 17 + arch/x86/include/asm/pgtable_64_types.h | 8 + arch/x86/mm/init_64.c | 7 +- fs/Kconfig | 14 + include/linux/bootmem_info.h | 78 +++ include/linux/hugetlb.h | 19 + include/linux/hugetlb_cgroup.h | 15 +- include/linux/memory_hotplug.h | 27 - mm/Makefile | 2 + mm/bootmem_info.c | 124 ++++ mm/hugetlb.c | 163 +++++- mm/hugetlb_vmemmap.c | 732 ++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 104 ++++ mm/memory_hotplug.c | 116 ---- mm/sparse.c | 5 +- 17 files changed, 1263 insertions(+), 180 deletions(-) create mode 100644 include/linux/bootmem_info.h create mode 100644 mm/bootmem_info.c create mode 100644 mm/hugetlb_vmemmap.c create mode 100644 mm/hugetlb_vmemmap.h
Signature · 1 line
2.11.0
PATCH v4 1/21 mm/memory_hotplug: Move bootmem info registration API to bootmem_info.c
Move bootmem info registration common API to individual bootmem_info.c for later patch use. This is just code movement without any functional change.
--- arch/x86/mm/init_64.c | 1 + include/linux/bootmem_info.h | 27 ++++++++++++ include/linux/memory_hotplug.h | 23 ---------- mm/Makefile | 1 + mm/bootmem_info.c | 99 ++++++++++++++++++++++++++++++++++++++++++ mm/memory_hotplug.c | 91 +------------------------------------- 6 files changed, 129 insertions(+), 113 deletions(-) create mode 100644 include/linux/bootmem_info.h create mode 100644 mm/bootmem_info.c
arch/x86/mm/init_64.c +1 -0
@@ -33,6 +33,7 @@#include <linux/nmi.h> #include <linux/gfp.h> #include <linux/kcore.h> +#include <linux/bootmem_info.h> #include <asm/processor.h> #include <asm/bios_ebda.h>
include/linux/bootmem_info.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_BOOTMEM_INFO_H +#define __LINUX_BOOTMEM_INFO_H + +#include <linux/mmzone.h> + +/* +* Types for free bootmem stored in page->lru.next. These have to be in +* some random range in unsigned long space for debugging purposes. +*/ +enum { +MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE =12 , +SECTION_INFO =MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE , +MIX_SECTION_INFO , +NODE_INFO , +MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE =NODE_INFO , +}; + +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE +void __init register_page_bootmem_info_node (struct pglist_data *pgdat); +#else +static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) +{ +} +#endif + +#endif /* __LINUX_BOOTMEM_INFO_H */
include/linux/memory_hotplug.h +0 -23
@@ -33,18 +33,6 @@ struct vmem_altmap; ___page; \ }) -/* -* Types for free bootmem stored in page->lru.next. These have to be in -* some random range in unsigned long space for debugging purposes. -*/ -enum { -MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE =12 , -SECTION_INFO =MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE , -MIX_SECTION_INFO , -NODE_INFO , -MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE =NODE_INFO , -}; -/* Types for control the zone type of onlined and offlined memory */ enum {/* Offline the memory. */
@@ -209,13 +197,6 @@ static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat)#endif /* CONFIG_NUMA */ #endif /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */ -#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE -extern void __init register_page_bootmem_info_node (struct pglist_data *pgdat); -#else -static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) -{ -} -#endif extern void put_page_bootmem (struct page *page);extern void get_page_bootmem (unsigned long ingo,struct page *page,unsigned long type);
@@ -254,10 +235,6 @@ static inline int mhp_notimplemented(const char *func)return -ENOSYS ; } -static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) -{ -} -static inline int try_online_node (int nid) {return 0 ;
mm/Makefile +1 -0
@@ -82,6 +82,7 @@ obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_FAILSLAB) += failslab.o +obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-$(CONFIG_MEMTEST) += memtest.o obj-$(CONFIG_MIGRATION) += migrate.o
mm/bootmem_info.c new file +99 -0
99 lines not on this page · raw message
mm/memory_hotplug.c +1 -90
103 lines not on this page · raw message
Signature · 1 line
2.11.0
Re: [PATCH v4 01/21] mm/memory_hotplug: Move bootmem info registration API to bootmem_info.c
Quoted 6 lines · Muchun Song
Move bootmem info registration common API to individual
bootmem_info.cfor later patch use. This is just code movement without any functional change.Signed-off-by Muchun Song <[email protected]>Acked-by Mike Kravetz <[email protected]>
Signature · 2 lines
Oscar Salvador SUSE L3
PATCH v4 2/21 mm/memory_hotplug: Move {get,put}_page_bootmem() to bootmem_info.c
In reply to Muchun SongIn the later patch, we will use {get,put}_page_bootmem() to initialize the page for vmemmap or free vmemmap page to buddy. So move them out of CONFIG_MEMORY_HOTPLUG_SPARSE. This is just code movement without any functional change.
--- arch/x86/mm/init_64.c | 2 +- include/linux/bootmem_info.h | 13 +++++++++++++ include/linux/memory_hotplug.h | 4 ---- mm/bootmem_info.c | 25 +++++++++++++++++++++++++ mm/memory_hotplug.c | 27 --------------------------- mm/sparse.c | 1 + 6 files changed, 40 insertions(+), 32 deletions(-)
arch/x86/mm/init_64.c +1 -1
@@ -1572,7 +1572,7 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,return err; } -#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE ) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE ) +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void register_page_bootmem_memmap (unsigned long section_nr,struct page *start_page,unsigned long nr_pages) {
include/linux/bootmem_info.h +13 -0
@@ -18,10 +18,23 @@ enum {#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void __init register_page_bootmem_info_node (struct pglist_data *pgdat); + +void get_page_bootmem (unsigned long info,struct page *page, +unsigned long type); +void put_page_bootmem (struct page *page);#else static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) { } + +static inline void put_page_bootmem (struct page *page) +{ +} + +static inline void get_page_bootmem (unsigned long info,struct page *page, +unsigned long type) +{ +}#endif #endif /* __LINUX_BOOTMEM_INFO_H */
include/linux/memory_hotplug.h +0 -4
@@ -197,10 +197,6 @@ static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat)#endif /* CONFIG_NUMA */ #endif /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */ -extern void put_page_bootmem (struct page *page); -extern void get_page_bootmem (unsigned long ingo,struct page *page, -unsigned long type); -void get_online_mems (void );void put_online_mems (void );
mm/bootmem_info.c +25 -0
@@ -10,6 +10,31 @@#include <linux/bootmem_info.h> #include <linux/memory_hotplug.h> +void get_page_bootmem (unsigned long info,struct page *page,unsigned long type) +{ + page->freelist = (void *)type; +SetPagePrivate (page); +set_page_private (page, info); +page_ref_inc (page); +} + +void put_page_bootmem (struct page *page) +{ +unsigned long type; + + type = (unsigned long ) page->freelist; +BUG_ON (type <MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || + type >MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE ); + +if (page_ref_dec_return (page) ==1 ) { + page->freelist =NULL ; +ClearPagePrivate (page); +set_page_private (page,0 ); +INIT_LIST_HEAD (&page->lru); +free_reserved_page (page); + } +} +#ifndef CONFIG_SPARSEMEM_VMEMMAP static void register_page_bootmem_info_section (unsigned long start_pfn) {
mm/memory_hotplug.c +0 -27
@@ -21,7 +21,6 @@#include <linux/memory.h> #include <linux/memremap.h> #include <linux/memory_hotplug.h> -#include <linux/bootmem_info.h> #include <linux/highmem.h> #include <linux/vmalloc.h> #include <linux/ioport.h>
@@ -142,32 +141,6 @@ static void release_memory_resource(struct resource *res) }#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE -void get_page_bootmem (unsigned long info,struct page *page, -unsigned long type) -{ - page->freelist = (void *)type; -SetPagePrivate (page); -set_page_private (page, info); -page_ref_inc (page); -} - -void put_page_bootmem (struct page *page) -{ -unsigned long type; - - type = (unsigned long ) page->freelist; -BUG_ON (type <MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || - type >MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE ); - -if (page_ref_dec_return (page) ==1 ) { - page->freelist =NULL ; -ClearPagePrivate (page); -set_page_private (page,0 ); -INIT_LIST_HEAD (&page->lru); -free_reserved_page (page); - } -} -static int check_pfn_span (unsigned long pfn,unsigned long nr_pages,const char *reason) {
mm/sparse.c +1 -0
@@ -13,6 +13,7 @@#include <linux/vmalloc.h> #include <linux/swap.h> #include <linux/swapops.h> +#include <linux/bootmem_info.h> #include "internal.h" #include <asm/dma.h>
Signature · 1 line
2.11.0
Re: [PATCH v4 02/21] mm/memory_hotplug: Move {get,put}_page_bootmem() to bootmem_info.c
Quoted 7 lines · Muchun Song
In the later patch, we will use {get,put}_page_bootmem() to initialize the page for vmemmap or free vmemmap page to buddy. So move them out of
CONFIG_MEMORY_HOTPLUG_SPARSE. This is just code movement without any functional change.Signed-off-by Muchun Song <[email protected]>Acked-by Mike Kravetz <[email protected]>
The change itself makes sense.
I tried to check the possibles scenarios with different CONFIG_ options toggled, but I will trust you that you ran the checks :-)
Signature · 2 lines
Oscar Salvador SUSE L3
PATCH v4 3/21 mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
In reply to Muchun SongThe purpose of introducing HUGETLB_PAGE_FREE_VMEMMAP is to configure whether to enable the feature of freeing unused vmemmap associated with HugeTLB pages. Now only support x86.
--- arch/x86/mm/init_64.c | 2 +- fs/Kconfig | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)
arch/x86/mm/init_64.c +1 -1
@@ -1225,7 +1225,7 @@ static struct kcore_list kcore_vsyscall;static void __init register_page_bootmem_info (void ) { -#ifdef CONFIG_NUMA +#if defined(CONFIG_NUMA ) || defined(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP )int i;for_each_online_node (i)
fs/Kconfig +14 -0
@@ -245,6 +245,20 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + def_bool HUGETLB_PAGE + depends on X86 + depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + When using SPARSEMEM_VMEMMAP, the system can save up some memory + from pre-allocated HugeTLB pages when they are not used. 6 pages + per 2MB HugeTLB page and 4094 per 1GB HugeTLB page. + + When the pages are going to be used or freed up, the vmemmap array + representing that range needs to be remapped again and the pages + we discarded earlier need to be rellocated again. + config MEMFD_CREATE def_bool TMPFS || HUGETLBFS
Signature · 1 line
2.11.0
Re: [PATCH v4 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
Quoted 28 lines · Muchun Song
The purpose of introducing
HUGETLB_PAGE_FREE_VMEMMAPis to configure whether to enable the feature of freeing unused vmemmap associated with HugeTLB pages. Now only support x86.Signed-off-by Muchun Song <[email protected]>--- arch/x86/mm/init_64.c | 2 +- fs/Kconfig | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)arch/x86/mm/init_64.c +1
-1@@ -1225,7 +1225,7 @@ static struct kcore_list kcore_vsyscall;static void __init register_page_bootmem_info (void ) { -#ifdef CONFIG_NUMA +#if defined(CONFIG_NUMA ) || defined(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP )int i;for_each_online_node (i)fs/Kconfig +0
-0
fs/Kconfig config HUGETLBFS · line 254
Quoted 10 lines
fs/Kconfig +7
-0@@ -245,6 +245,20 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + def_bool HUGETLB_PAGE + depends on X86 + depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + When using SPARSEMEM_VMEMMAP, the system can save up some memory
Should that read,
When using HUGETLB_PAGE_FREE_VMEMMAP, ...
as the help message is for this config option.
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v4 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
Quoted 43 lines · Mike Kravetz
Quoted 28 lines · Muchun Song
The purpose of introducing
HUGETLB_PAGE_FREE_VMEMMAPis to configure whether to enable the feature of freeing unused vmemmap associated with HugeTLB pages. Now only support x86.Signed-off-by Muchun Song <[email protected]>--- arch/x86/mm/init_64.c | 2 +- fs/Kconfig | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)arch/x86/mm/init_64.c +1
-1@@ -1225,7 +1225,7 @@ static struct kcore_list kcore_vsyscall;static void __init register_page_bootmem_info (void ) { -#ifdef CONFIG_NUMA +#if defined(CONFIG_NUMA ) || defined(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP )int i;for_each_online_node (i)fs/Kconfig +0
-0fs/Kconfig config HUGETLBFS · line 254
Quoted 10 lines
fs/Kconfig +7
-0@@ -245,6 +245,20 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + def_bool HUGETLB_PAGE + depends on X86 + depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + When using SPARSEMEM_VMEMMAP, the system can save up some memoryShould that read,
Quoted 3 lines
When using
HUGETLB_PAGE_FREE_VMEMMAP, ...as the help message is for this config option.
Got it. Thanks
Quoted 3 lines
--
Mike Kravetz
Signature · 2 lines
Yours, Muchun
PATCH v4 4/21 mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
In reply to Muchun SongIf the size of HugeTLB page is 2MB, we need 512 struct page structures (8 pages) to be associated with it. As far as I know, we only use the first 4 struct page structures. Use of first 4 struct page structures comes from HUGETLB_CGROUP_MIN_ORDER.
For tail pages, the value of compound_head is the same. So we can reuse first page of tail page structs. We map the virtual addresses of the remaining 6 pages of tail page structs to the first tail page struct, and then free these 6 pages. Therefore, we need to reserve at least 2 pages as vmemmap areas.
So we introduce a new nr_free_vmemmap_pages field in the hstate to indicate how many vmemmap pages associated with a HugeTLB page that we can free to buddy system.
--- include/linux/hugetlb.h | 3 ++ mm/Makefile | 1 + mm/hugetlb.c | 3 ++ mm/hugetlb_vmemmap.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 20 +++++++++ 5 files changed, 135 insertions(+) create mode 100644 mm/hugetlb_vmemmap.c create mode 100644 mm/hugetlb_vmemmap.h
include/linux/hugetlb.h +3 -0
@@ -492,6 +492,9 @@ struct hstate {unsigned int nr_huge_pages_node[MAX_NUMNODES ];unsigned int free_huge_pages_node[MAX_NUMNODES ];unsigned int surplus_huge_pages_node[MAX_NUMNODES ]; +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +unsigned int nr_free_vmemmap_pages; +#endif #ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ struct cftype cgroup_files_dfl[7 ];
mm/Makefile +1 -0
@@ -71,6 +71,7 @@ obj-$(CONFIG_FRONTSWAP) += frontswap.o obj-$(CONFIG_ZSWAP) += zswap.o obj-$(CONFIG_HAS_DMA) += dmapool.o obj-$(CONFIG_HUGETLBFS) += hugetlb.o +obj-$(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP) += hugetlb_vmemmap.o obj-$(CONFIG_NUMA) += mempolicy.o obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o
mm/hugetlb.c +3 -0
@@ -42,6 +42,7 @@#include <linux/userfaultfd_k.h> #include <linux/page_owner.h> #include "internal.h" +#include "hugetlb_vmemmap.h" int hugetlb_max_hstate__read_mostly ;unsigned int default_hstate_idx;
@@ -3285,6 +3286,8 @@ void __init hugetlb_add_hstate(unsigned int order)snprintf (h->name,HSTATE_NAME_LEN ,"hugepages-%lukB" ,huge_page_size (h)/1024 ); +hugetlb_vmemmap_init (h); + parsed_hstate = h; }
mm/hugetlb_vmemmap.c new file +108 -0
108 lines not on this page · raw message
mm/hugetlb_vmemmap.h new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Free some vmemmap pages of HugeTLB +* +* Copyright (c) 2020, Bytedance. All rights reserved. +* +* Author: Muchun Song <[email protected]> +*/ +#ifndef _LINUX_HUGETLB_VMEMMAP_H +#define _LINUX_HUGETLB_VMEMMAP_H +#include <linux/hugetlb.h> + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +void __init hugetlb_vmemmap_init (struct hstate *h); +#else +static inline void hugetlb_vmemmap_init (struct hstate *h) +{ +} +#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ +#endif /* _LINUX_HUGETLB_VMEMMAP_H */
Signature · 1 line
2.11.0
Oscar Salvador
maintainerRe: [PATCH v4 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
Quoted 4 lines · Muchun Song
If the size of HugeTLB page is 2MB, we need 512 struct page structures (8 pages) to be associated with it. As far as I know, we only use the first 4 struct page structures. Use of first 4 struct page structures comes from
HUGETLB_CGROUP_MIN_ORDER.
Once you mention 2MB HugeTLB page and its specific I would also mention 1GB HugeTLB pages, maybe something along these lines.
I would supress "As far as I know", we __know__ that we only use the first 4 struct page structures to track metadata information.
mm/hugetlb_vmemmap.c line 84
Quoted 9 lines
mm/hugetlb_vmemmap.c new file +9
-0+/* +* There are 512 struct page structures(8 pages) associated with each 2MB +* hugetlb page. For tail pages, the value of compound_head is the same. +* So we can reuse first page of tail page structures. We map the virtual +* addresses of the remaining 6 pages of tail page structures to the first +* tail page struct, and then free these 6 pages. Therefore, we need to +* reserve at least 2 pages as vmemmap areas. +*/ +#define RESERVE_VMEMMAP_NR 2U
Either I would include 1GB specific there as well, or I would not add any specifics at all and just go by saying that first two pages are used, and the rest can be remapped to the first page that contains the tails.
mm/hugetlb_vmemmap.c line 94
Quoted 9 lines
mm/hugetlb_vmemmap.c new file +9
-0+void __init hugetlb_vmemmap_init (struct hstate *h) +{ +unsigned int order =huge_page_order (h); +unsigned int vmemmap_pages; + + vmemmap_pages = ((1 << order) *sizeof (struct page )) >>PAGE_SHIFT ; +/* +* The head page and the first tail page are not to be freed to buddy +* system, the others page will map to the first tail page. So there
"the remaining pages" might be more clear.
mm/hugetlb_vmemmap.c line 95
Quoted 1 line
mm/hugetlb_vmemmap.c new file +1
-0+* are (@vmemmap_pages - RESERVE_VMEMMAP_NR) pages can be freed.
"that can be freed"
mm/hugetlb_vmemmap.c line 104
Quoted 9 lines
mm/hugetlb_vmemmap.c new file +9
-0+* +* Could RESERVE_VMEMMAP_NR be greater than @vmemmap_pages? This is +* not expected to happen unless the system is corrupted. So on the +* safe side, it is only a safety net. +*/ +if (likely (vmemmap_pages >RESERVE_VMEMMAP_NR )) + h->nr_free_vmemmap_pages = vmemmap_pages -RESERVE_VMEMMAP_NR ; +else + h->nr_free_vmemmap_pages =0 ;
This made think of something.
Since struct hstate hstates is global, all the fields should be defined to 0. So, the following assignments in hugetlb_add_hstate:
h->nr_huge_pages = 0;
h->free_huge_pages = 0;should not be needed.
Actually, we do not initialize other values like resv_huge_pages or surplus_huge_pages.
If that is the case, the "else" could go.
Mike?
The changes itself look good to me.
I think that putting all the vemmap stuff into hugetlb-vmemmap.* was the right choice.
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v4 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
Quoted 11 lines · Oscar Salvador
Quoted 4 lines · Muchun Song
If the size of HugeTLB page is 2MB, we need 512 struct page structures (8 pages) to be associated with it. As far as I know, we only use the first 4 struct page structures. Use of first 4 struct page structures comes from
HUGETLB_CGROUP_MIN_ORDER.Once you mention 2MB HugeTLB page and its specific I would also mention 1GB HugeTLB pages, maybe something along these lines.
I would supress "As far as I know", we__know__that we only use the first 4 struct page structures to track metadata information.
Thanks. Will do.
Quoted 14 lines
mm/hugetlb_vmemmap.c line 84
Quoted 10 lines
mm/hugetlb_vmemmap.c new file +9
-0+/* +* There are 512 struct page structures(8 pages) associated with each 2MB +* hugetlb page. For tail pages, the value of compound_head is the same. +* So we can reuse first page of tail page structures. We map the virtual +* addresses of the remaining 6 pages of tail page structures to the first +* tail page struct, and then free these 6 pages. Therefore, we need to +* reserve at least 2 pages as vmemmap areas. +*/ +#define RESERVE_VMEMMAP_NR 2UEither I would include 1GB specific there as well, or I would not add any specifics at all and just go by saying that first two pages are used, and the rest can be remapped to the first page that contains the tails.
Thanks. Will do.
Quoted 12 lines
mm/hugetlb_vmemmap.c line 94
Quoted 11 lines
mm/hugetlb_vmemmap.c new file +9
-0+void __init hugetlb_vmemmap_init (struct hstate *h) +{ +unsigned int order =huge_page_order (h); +unsigned int vmemmap_pages; + + vmemmap_pages = ((1 << order) *sizeof (struct page )) >>PAGE_SHIFT ; +/* +* The head page and the first tail page are not to be freed to buddy +* system, the others page will map to the first tail page. So there "the remaining pages" might be more clear.
Thanks.
Quoted 3 lines
Quoted 2 lines
+ * are (@vmemmap_pages -
RESERVE_VMEMMAP_NR) pages can be freed."that can be freed"
Thanks.
Quoted 15 lines
mm/hugetlb_vmemmap.c line 104
Quoted 10 lines
mm/hugetlb_vmemmap.c new file +9
-0+* +* Could RESERVE_VMEMMAP_NR be greater than @vmemmap_pages? This is +* not expected to happen unless the system is corrupted. So on the +* safe side, it is only a safety net. +*/ +if (likely (vmemmap_pages >RESERVE_VMEMMAP_NR )) + h->nr_free_vmemmap_pages = vmemmap_pages -RESERVE_VMEMMAP_NR ; +else + h->nr_free_vmemmap_pages =0 ;This made think of something.
Since struct hstate hstates is global, all the fields should be defined to 0. So, the following assignments inhugetlb_add_hstate:
Quoted 8 lines
h->nr_huge_pages = 0; h->free_huge_pages = 0;should not be needed.
Actually, we do not initialize other values likeresv_huge_pagesorsurplus_huge_pages.If that is the case, the "else" could go.
Yeah, I agree with you.
Quoted 11 lines
Mike?
The changes itself look good to me.
I think that putting all the vemmap stuff into hugetlb-vmemmap.* was the right choice.--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun
Re: [PATCH v4 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
In reply to Oscar SalvadorQuoted 32 lines · Oscar Salvador
mm/hugetlb_vmemmap.c line 94
Quoted 9 lines · Muchun Song
mm/hugetlb_vmemmap.c new file +9
-0+void __init hugetlb_vmemmap_init (struct hstate *h) +{ +unsigned int order =huge_page_order (h); +unsigned int vmemmap_pages; + + vmemmap_pages = ((1 << order) *sizeof (struct page )) >>PAGE_SHIFT ; +/* +* The head page and the first tail page are not to be freed to buddy +* system, the others page will map to the first tail page. So there "the remaining pages" might be more clear.
mm/hugetlb_vmemmap.c line 95
Quoted 1 line
mm/hugetlb_vmemmap.c new file +1
-0+* are (@vmemmap_pages - RESERVE_VMEMMAP_NR) pages can be freed. "that can be freed"
mm/hugetlb_vmemmap.c line 104
Quoted 9 lines
mm/hugetlb_vmemmap.c new file +9
-0+* +* Could RESERVE_VMEMMAP_NR be greater than @vmemmap_pages? This is +* not expected to happen unless the system is corrupted. So on the +* safe side, it is only a safety net. +*/ +if (likely (vmemmap_pages >RESERVE_VMEMMAP_NR )) + h->nr_free_vmemmap_pages = vmemmap_pages -RESERVE_VMEMMAP_NR ; +else + h->nr_free_vmemmap_pages =0 ;This made think of something.
Since struct hstate hstates is global, all the fields should be defined to 0. So, the following assignments inhugetlb_add_hstate:h->nr_huge_pages = 0; h->free_huge_pages = 0;
Quoted 7 lines
should not be needed.
Actually, we do not initialize other values likeresv_huge_pagesorsurplus_huge_pages.If that is the case, the "else" could go.
Mike?
Correct. Those assignments have been in the code for a very long time.
Quoted 3 lines
The changes itself look good to me.
I think that putting all the vemmap stuff into hugetlb-vmemmap.* was the right choice.
Agree!
Signature · 1 line
Mike Kravetz
Re: [PATCH v4 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
In reply to Muchun SongQuoted 6 lines · Muchun Song
mm/hugetlb_vmemmap.c new file +0
-0
mm/hugetlb_vmemmap.c line 8
Quoted 8 lines
mm/hugetlb_vmemmap.c new file +8
-0@@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Free some vmemmap pages of HugeTLB +* +* Copyright (c) 2020, Bytedance. All rights reserved. +* +* Author: Muchun Song <[email protected]> +*
Oscar has already made some suggestions to change comments. I would suggest changing the below text to something like the following.
Quoted 7 lines
mm/hugetlb_vmemmap.c new file +7
-0+* Nowadays we track the status of physical page frames using struct page +* structures arranged in one or more arrays. And here exists one-to-one +* mapping between the physical page frame and the corresponding struct page +* structure. +* +* The HugeTLB support is built on top of multiple page size support that +* is provided by most modern architectures. For example, x86 CPUs normally
mm/hugetlb_vmemmap.c line 25
Quoted 10 lines
mm/hugetlb_vmemmap.c new file +10
-0+* support 4K and 2M (1G if architecturally supported) page sizes. Every +* HugeTLB has more than one struct page structure. The 2M HugeTLB has 512 +* struct page structure and 1G HugeTLB has 4096 struct page structures. But +* in the core of HugeTLB only uses the first 4 (Use of first 4 struct page +* structures comes from HUGETLB_CGROUP_MIN_ORDER.) struct page structures to +* store metadata associated with each HugeTLB. The rest of the struct page +* structures are usually read the compound_head field which are all the same +* value. If we can free some struct page memory to buddy system so that we +* can save a lot of memory. +*
struct page structures (page structs) are used to describe a physical page frame. By default, there is a one-to-one mapping from a page frame to it's corresponding page struct.
HugeTLB pages consist of multiple base page size pages and is supported by many architectures. See hugetlbpage.rst in the Documentation directory for more details. On the x86 architecture, HugeTLB pages of size 2MB and 1GB are currently supported. Since the base page size on x86 is 4KB, a 2MB HugeTLB page consists of 512 base pages and a 1GB HugeTLB page consists of 4096 base pages. For each base page, there is a corresponding page struct.
Within the HugeTLB subsystem, only the first 4 page structs are used to contain unique information about a HugeTLB page. HUGETLB_CGROUP_MIN_ORDER provides this upper limit. The only 'useful' information in the remaining page structs is the compound_head field, and this field is the same for all tail pages.
By removing redundant page structs for HugeTLB pages, memory can returned to the buddy allocator for other uses.
Quoted 9 lines
mm/hugetlb_vmemmap.c new file +9
-0+* When the system boot up, every 2M HugeTLB has 512 struct page structures +* which size is 8 pages(sizeof(struct page) * 512 / PAGE_SIZE). +* +* HugeTLB struct pages(8 pages) page frame(8 pages) +* +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ +* | | | 0 | -------------> | 0 | +* | | | 1 | -------------> | 1 | +* | | | 2 | -------------> | 2 | +* | | | 3 | -------------> | 3 |
mm/hugetlb_vmemmap.c line 44
Quoted 10 lines
mm/hugetlb_vmemmap.c new file +10
-0+* | | | 4 | -------------> | 4 | +* | 2M | | 5 | -------------> | 5 | +* | | | 6 | -------------> | 6 | +* | | | 7 | -------------> | 7 | +* | | +-----------+ +-----------+ +* | | +* | | +* +-----------+ +* +*
I think we want the description before the next diagram.
Reworded description here:
The value of compound_head is the same for all tail pages. The first page of page structs (page 0) associated with the HugeTLB page contains the 4 page structs necessary to describe the HugeTLB. The only use of the remaining pages of page structs (page 1 to page 7) is to point to compound_head. Therefore, we can remap pages 2 to 7 to page 1. Only 2 pages of page structs will be used for each HugeTLB page. This will allow us to free the remaining 6 pages to the buddy allocator.
Here is how things look after remapping.
Quoted 15 lines
mm/hugetlb_vmemmap.c new file +15
-0+* +* HugeTLB struct pages(8 pages) page frame(8 pages) +* +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ +* | | | 0 | -------------> | 0 | +* | | | 1 | -------------> | 1 | +* | | | 2 | -------------> +-----------+ +* | | | 3 | -----------------^ ^ ^ ^ ^ +* | | | 4 | -------------------+ | | | +* | 2M | | 5 | ---------------------+ | | +* | | | 6 | -----------------------+ | +* | | | 7 | -------------------------+ +* | | +-----------+ +* | | +* | | +* +-----------+
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v4 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
Quoted 11 lines · Mike Kravetz
Quoted 9 lines · Muchun Song
mm/hugetlb_vmemmap.c new file +3
-0@@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Free some vmemmap pages of HugeTLB
Quoted 8 lines
mm/hugetlb_vmemmap.c line 8
Quoted 5 lines
mm/hugetlb_vmemmap.c new file +5
-0+* +* Copyright (c) 2020, Bytedance. All rights reserved. +* +* Author: Muchun Song <[email protected]> +* Oscar has already made some suggestions to change comments. I would suggest changing the below text to something like the following.
Thanks Mike. I will change the below comments.
Quoted 91 lines
Quoted 8 lines
mm/hugetlb_vmemmap.c new file +7
-0+* Nowadays we track the status of physical page frames using struct page +* structures arranged in one or more arrays. And here exists one-to-one +* mapping between the physical page frame and the corresponding struct page +* structure. +* +* The HugeTLB support is built on top of multiple page size support that +* is provided by most modern architectures. For example, x86 CPUs normally mm/hugetlb_vmemmap.c line 25
Quoted 10 lines
mm/hugetlb_vmemmap.c new file +10
-0+* support 4K and 2M (1G if architecturally supported) page sizes. Every +* HugeTLB has more than one struct page structure. The 2M HugeTLB has 512 +* struct page structure and 1G HugeTLB has 4096 struct page structures. But +* in the core of HugeTLB only uses the first 4 (Use of first 4 struct page +* structures comes from HUGETLB_CGROUP_MIN_ORDER.) struct page structures to +* store metadata associated with each HugeTLB. The rest of the struct page +* structures are usually read the compound_head field which are all the same +* value. If we can free some struct page memory to buddy system so that we +* can save a lot of memory. +* struct page structures (page structs) are used to describe a physical page frame. By default, there is a one-to-one mapping from a page frame to it's corresponding page struct.
HugeTLB pages consist of multiple base page size pages and is supported by many architectures. See
hugetlbpage.rstin the Documentation directory for more details. On the x86 architecture, HugeTLB pages of size 2MB and 1GB are currently supported. Since the base page size on x86 is 4KB, a 2MB HugeTLB page consists of 512 base pages and a 1GB HugeTLB page consists of 4096 base pages. For each base page, there is a corresponding page struct.Within the HugeTLB subsystem, only the first 4 page structs are used to contain unique information about a HugeTLB page.
HUGETLB_CGROUP_MIN_ORDERprovides this upper limit. The only 'useful' information in the remaining page structs is thecompound_headfield, and this field is the same for all tail pages.By removing redundant page structs for HugeTLB pages, memory can returned to the buddy allocator for other uses.
Quoted 9 lines
mm/hugetlb_vmemmap.c new file +9
-0+* When the system boot up, every 2M HugeTLB has 512 struct page structures +* which size is 8 pages(sizeof(struct page) * 512 / PAGE_SIZE). +* +* HugeTLB struct pages(8 pages) page frame(8 pages) +* +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ +* | | | 0 | -------------> | 0 | +* | | | 1 | -------------> | 1 | +* | | | 2 | -------------> | 2 | +* | | | 3 | -------------> | 3 | mm/hugetlb_vmemmap.c line 44
Quoted 10 lines
mm/hugetlb_vmemmap.c new file +10
-0+* | | | 4 | -------------> | 4 | +* | 2M | | 5 | -------------> | 5 | +* | | | 6 | -------------> | 6 | +* | | | 7 | -------------> | 7 | +* | | +-----------+ +-----------+ +* | | +* | | +* +-----------+ +* +* I think we want the description before the next diagram.
Reworded description here:
The value of
compound_headis the same for all tail pages. The first page of page structs (page 0) associated with the HugeTLB page contains the 4 page structs necessary to describe the HugeTLB. The only use of the remaining pages of page structs (page 1 to page 7) is to point tocompound_head. Therefore, we can remap pages 2 to 7 to page 1. Only 2 pages of page structs will be used for each HugeTLB page. This will allow us to free the remaining 6 pages to the buddy allocator.Here is how things look after remapping.
Quoted 5 lines
mm/hugetlb_vmemmap.c new file +5
-0+* +* HugeTLB struct pages(8 pages) page frame(8 pages) +* +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ +* | | | 0 | -------------> | 0 | +* | | | 1 | -------------> | 1 | mm/hugetlb_vmemmap.c line 61
Quoted 10 lines
mm/hugetlb_vmemmap.c new file +10
-0+* | | | 2 | -------------> +-----------+ +* | | | 3 | -----------------^ ^ ^ ^ ^ +* | | | 4 | -------------------+ | | | +* | 2M | | 5 | ---------------------+ | | +* | | | 6 | -----------------------+ | +* | | | 7 | -------------------------+ +* | | +-----------+ +* | | +* | | +* +-----------+ --
Mike Kravetz
Signature · 2 lines
Yours, Muchun
PATCH v4 5/21 mm/hugetlb: Introduce pgtable allocation/freeing helpers
In reply to Muchun SongOn x86_64, vmemmap is always PMD mapped if the machine has hugepages support and if we have 2MB contiguos pages and PMD aligned. If we want to free the unused vmemmap pages, we have to split the huge pmd firstly. So we should pre-allocate pgtable to split PMD to PTE.
--- mm/hugetlb_vmemmap.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 12 +++++++++ 2 files changed, 85 insertions(+)
mm/hugetlb_vmemmap.c +73 -0
@@ -71,6 +71,8 @@*/ #define pr_fmt (fmt) "HugeTLB Vmemmap: " fmt +#include <linux/list.h> +#include <asm/pgalloc.h> #include "hugetlb_vmemmap.h" /*
@@ -83,6 +85,77 @@*/ #define RESERVE_VMEMMAP_NR 2U +#ifndef VMEMMAP_HPAGE_SHIFT +#define VMEMMAP_HPAGE_SHIFT HPAGE_SHIFT +#endif +#define VMEMMAP_HPAGE_ORDER (VMEMMAP_HPAGE_SHIFT - PAGE_SHIFT) +#define VMEMMAP_HPAGE_NR (1 << VMEMMAP_HPAGE_ORDER) +#define VMEMMAP_HPAGE_SIZE ((1UL) << VMEMMAP_HPAGE_SHIFT) +#define VMEMMAP_HPAGE_MASK (~(VMEMMAP_HPAGE_SIZE - 1)) + +#define page_huge_pte (page) ((page)->pmd_huge_pte) + +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) +{ +return h->nr_free_vmemmap_pages; +} + +static inline unsigned int vmemmap_pages_per_hpage (struct hstate *h) +{ +return free_vmemmap_pages_per_hpage (h) +RESERVE_VMEMMAP_NR ; +} + +static inline unsigned long vmemmap_pages_size_per_hpage (struct hstate *h) +{ +return (unsigned long )vmemmap_pages_per_hpage (h) <<PAGE_SHIFT ; +} + +static inline unsigned int pgtable_pages_to_prealloc_per_hpage (struct hstate *h) +{ +unsigned long vmemmap_size =vmemmap_pages_size_per_hpage (h); + +/* +* No need pre-allocate page tables when there is no vmemmap pages +* to free. +*/ +if (!free_vmemmap_pages_per_hpage (h)) +return 0 ; + +return ALIGN (vmemmap_size,VMEMMAP_HPAGE_SIZE ) >>VMEMMAP_HPAGE_SHIFT ; +} + +void vmemmap_pgtable_free (struct page *page) +{ +struct page *pte_page, *t_page; + +list_for_each_entry_safe (pte_page, t_page, &page->lru, lru) { +list_del (&pte_page->lru); +pte_free_kernel (&init_mm,page_to_virt (pte_page)); + } +} + +int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +/* Store preallocated pages on huge page lru list */ +INIT_LIST_HEAD (&page->lru); + +while (nr--) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +list_add (&virt_to_page (pte_p)->lru, &page->lru); + } + +return 0 ; +out: +vmemmap_pgtable_free (page); +return -ENOMEM ; +} +void __init hugetlb_vmemmap_init (struct hstate *h) {unsigned int order =huge_page_order (h);
mm/hugetlb_vmemmap.h +12 -0
@@ -9,12 +9,24 @@#ifndef _LINUX_HUGETLB_VMEMMAP_H#define _LINUX_HUGETLB_VMEMMAP_H#include <linux/hugetlb.h> +#include <linux/mm.h> #ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP void __init hugetlb_vmemmap_init (struct hstate *h); +int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page); +void vmemmap_pgtable_free (struct page *page);#else static inline void hugetlb_vmemmap_init (struct hstate *h) { } + +static inline int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +return 0 ; +} + +static inline void vmemmap_pgtable_free (struct page *page) +{ +}#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */
Signature · 1 line
2.11.0
Oscar Salvador
maintainerRe: [PATCH v4 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 1 line · Muchun Song
+#define
page_huge_pte(page)((page)->pmd_huge_pte)
Seems you do not need this one anymore.
Quoted 15 lines
mm/hugetlb_vmemmap.c +15
-0+void vmemmap_pgtable_free (struct page *page) +{ +struct page *pte_page, *t_page; + +list_for_each_entry_safe (pte_page, t_page, &page->lru, lru) { +list_del (&pte_page->lru); +pte_free_kernel (&init_mm,page_to_virt (pte_page)); + } +} + +int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +/* Store preallocated pages on huge page lru list */
mm/hugetlb_vmemmap.c line 151
Quoted 10 lines
mm/hugetlb_vmemmap.c +10
-0+INIT_LIST_HEAD (&page->lru); + +while (nr--) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +list_add (&virt_to_page (pte_p)->lru, &page->lru); + }
Definetely this looks better and easier to handle.
Btw, did you explore Matthew's hint about instead of allocating a new page, using one of the ones you are going to free to store the ptes? I am not sure whether it is feasible at all though.
mm/hugetlb_vmemmap.h line 12
Quoted 7 lines
mm/hugetlb_vmemmap.h +1
-0@@ -9,12 +9,24 @@#ifndef _LINUX_HUGETLB_VMEMMAP_H#define _LINUX_HUGETLB_VMEMMAP_H#include <linux/hugetlb.h> +#include <linux/mm.h>
why do we need this here?
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v4 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 3 lines · Oscar Salvador
Quoted 1 line · Muchun Song
+#define
page_huge_pte(page)((page)->pmd_huge_pte)
Yeah, I forgot to remove it. Thanks.
Quoted 24 lines
Seems you do not need this one anymore.
Quoted 21 lines
mm/hugetlb_vmemmap.c +21
-0+void vmemmap_pgtable_free (struct page *page) +{ +struct page *pte_page, *t_page; + +list_for_each_entry_safe (pte_page, t_page, &page->lru, lru) { +list_del (&pte_page->lru); +pte_free_kernel (&init_mm,page_to_virt (pte_page)); + } +} + +int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +/* Store preallocated pages on huge page lru list */ +INIT_LIST_HEAD (&page->lru); + +while (nr--) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm);
Quoted 8 lines
mm/hugetlb_vmemmap.c line 151
Quoted 4 lines
mm/hugetlb_vmemmap.c +4
-0+if (!pte_p) + goto out; +list_add (&virt_to_page (pte_p)->lru, &page->lru); + }Definetely this looks better and easier to handle.
Btw, did you explore Matthew's hint about instead of allocating a new page, using one of the ones you are going to free to store the ptes?
Oh, sorry for missing his reply. It is a good idea. I will start an investigation.
Thanks for reminding me.
Quoted 12 lines
I am not sure whether it is feasible at all though.
mm/hugetlb_vmemmap.h line 12
Quoted 7 lines
mm/hugetlb_vmemmap.h +1
-0@@ -9,12 +9,24 @@#ifndef _LINUX_HUGETLB_VMEMMAP_H#define _LINUX_HUGETLB_VMEMMAP_H#include <linux/hugetlb.h> +#include <linux/mm.h> why do we need this here?
Yeah, also can remove:).
Quoted 4 lines
--
Oscar Salvador
SUSE L3
--
Yours,
Muchun
Re: [External] Re: [PATCH v4 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
In reply to Oscar SalvadorQuoted 28 lines · Oscar Salvador
Quoted 1 line · Muchun Song
+#define
page_huge_pte(page)((page)->pmd_huge_pte)Seems you do not need this one anymore.
Quoted 22 lines
mm/hugetlb_vmemmap.c +22
-0+void vmemmap_pgtable_free (struct page *page) +{ +struct page *pte_page, *t_page; + +list_for_each_entry_safe (pte_page, t_page, &page->lru, lru) { +list_del (&pte_page->lru); +pte_free_kernel (&init_mm,page_to_virt (pte_page)); + } +} + +int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +/* Store preallocated pages on huge page lru list */ +INIT_LIST_HEAD (&page->lru); + +while (nr--) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p)
Quoted 8 lines
mm/hugetlb_vmemmap.c line 151
Quoted 3 lines
mm/hugetlb_vmemmap.c +3
-0+ goto out; +list_add (&virt_to_page (pte_p)->lru, &page->lru); + }Definetely this looks better and easier to handle.
Btw, did you explore Matthew's hint about instead of allocating a new page, using one of the ones you are going to free to store the ptes? I am not sure whether it is feasible at all though.
Hi Oscar and Matthew,
I have started an investigation about this. Finally, I think that it may not be feasible. If we use a vmemmap page frame as a page table when we split the PMD table firstly, in this stage, we need to set 512 pte entry to the vmemmap page frame. If someone reads the tail struct page struct of the HugeTLB, it can get the arbitrary value (I am not sure it actually exists, maybe the memory compaction module can do this). So on the safe side, I think that allocating a new page is a good choice.
Thanks.
Quoted 15 lines
mm/hugetlb_vmemmap.h line 12
Quoted 9 lines
mm/hugetlb_vmemmap.h +1
-0@@ -9,12 +9,24 @@#ifndef _LINUX_HUGETLB_VMEMMAP_H#define _LINUX_HUGETLB_VMEMMAP_H#include <linux/hugetlb.h> +#include <linux/mm.h> why do we need this here?
--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun
Re: [External] Re: [PATCH v4 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 40 lines · Muchun Song
Quoted 28 lines · Oscar Salvador
Quoted 1 line · Muchun Song
+#define
page_huge_pte(page)((page)->pmd_huge_pte)Seems you do not need this one anymore.
Quoted 22 lines
mm/hugetlb_vmemmap.c +22
-0+void vmemmap_pgtable_free (struct page *page) +{ +struct page *pte_page, *t_page; + +list_for_each_entry_safe (pte_page, t_page, &page->lru, lru) { +list_del (&pte_page->lru); +pte_free_kernel (&init_mm,page_to_virt (pte_page)); + } +} + +int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +/* Store preallocated pages on huge page lru list */ +INIT_LIST_HEAD (&page->lru); + +while (nr--) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p)Quoted 8 lines
mm/hugetlb_vmemmap.c line 151
Quoted 3 lines
mm/hugetlb_vmemmap.c +3
-0+ goto out; +list_add (&virt_to_page (pte_p)->lru, &page->lru); + }Definetely this looks better and easier to handle.
Btw, did you explore Matthew's hint about instead of allocating a new page, using one of the ones you are going to free to store the ptes? I am not sure whether it is feasible at all though.Hi Oscar and Matthew,
Quoted 9 lines
I have started an investigation about this. Finally, I think that it may not be feasible. If we use a vmemmap page frame as a page table when we split the PMD table firstly, in this stage, we need to set 512 pte entry to the vmemmap page frame. If someone reads the tail struct page struct of the HugeTLB, it can get the arbitrary value (I am not sure it actually exists, maybe the memory compaction module can do this). So on the safe side, I think that allocating a new page is a good choice.
Thanks for looking into this.
If I understand correctly, the issue is that you need the pte page to set up the new mappings. In your current code, this is done before removing the pages of struct pages. This keeps everything 'consistent' as things are remapped.
If you want to use one of the 'pages of struct pages' for the new pte page, then there will be a period of time when things are inconsistent. Before setting up the mapping, some code could potentially access that pages of struct pages.
I tend to agree that allocating allocating a new page is the safest thing to do here. Or, perhaps someone can think of a way make this safe.
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v4 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 59 lines · Mike Kravetz
Quoted 40 lines · Muchun Song
Quoted 28 lines · Oscar Salvador
Quoted 1 line · Muchun Song
+#define
page_huge_pte(page)((page)->pmd_huge_pte)Seems you do not need this one anymore.
Quoted 22 lines
mm/hugetlb_vmemmap.c +22
-0+void vmemmap_pgtable_free (struct page *page) +{ +struct page *pte_page, *t_page; + +list_for_each_entry_safe (pte_page, t_page, &page->lru, lru) { +list_del (&pte_page->lru); +pte_free_kernel (&init_mm,page_to_virt (pte_page)); + } +} + +int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +/* Store preallocated pages on huge page lru list */ +INIT_LIST_HEAD (&page->lru); + +while (nr--) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p)Quoted 8 lines
mm/hugetlb_vmemmap.c line 151
Quoted 3 lines
mm/hugetlb_vmemmap.c +3
-0+ goto out; +list_add (&virt_to_page (pte_p)->lru, &page->lru); + }Definetely this looks better and easier to handle.
Btw, did you explore Matthew's hint about instead of allocating a new page, using one of the ones you are going to free to store the ptes? I am not sure whether it is feasible at all though.Hi Oscar and Matthew,
Quoted 9 lines
I have started an investigation about this. Finally, I think that it may not be feasible. If we use a vmemmap page frame as a page table when we split the PMD table firstly, in this stage, we need to set 512 pte entry to the vmemmap page frame. If someone reads the tail struct page struct of the HugeTLB, it can get the arbitrary value (I am not sure it actually exists, maybe the memory compaction module can do this). So on the safe side, I think that allocating a new page is a good choice.
Thanks for looking into this.
If I understand correctly, the issue is that you need the pte page to set up the new mappings. In your current code, this is done before removing the pages of struct pages. This keeps everything 'consistent' as things are remapped.
Quoted 4 lines
If you want to use one of the 'pages of struct pages' for the new pte page, then there will be a period of time when things are inconsistent. Before setting up the mapping, some code could potentially access that pages of struct pages.
Yeah, you are right.
Quoted 5 lines
I tend to agree that allocating allocating a new page is the safest thing to do here. Or, perhaps someone can think of a way make this safe. --
Mike Kravetz
Signature · 2 lines
Yours, Muchun
Re: [PATCH v4 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
In reply to Muchun SongQuoted 2 lines · Muchun Song
On
x86_64, vmemmap is always PMD mapped if the machine has hugepages support and if we have 2MB contiguos pages and PMD aligned. If we want
contiguous alignment
Quoted 8 lines
to free the unused vmemmap pages, we have to split the huge pmd firstly. So we should pre-allocate pgtable to split PMD to PTE.
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb_vmemmap.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 12 +++++++++ 2 files changed, 85 insertions(+)
Thanks for the cleanup.
Oscar made some other comments. I only have one additional minor comment below.
With those minor cleanups,
Quoted 1 line
mm/hugetlb_vmemmap.c +0
-0
...
mm/hugetlb_vmemmap.c line 141
Quoted 5 lines
mm/hugetlb_vmemmap.c +5
-0+int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +/* Store preallocated pages on huge page lru list */
Let's expland the above comment to something like this:
/*
* Use the huge page lru list to temporarily store the preallocated
* pages. The preallocated pages are used and the list is emptied
* before the huge page is put into use. When the huge page is put
* into use by prep_new_huge_page() the list will be reinitialized. */
Quoted 16 lines
mm/hugetlb_vmemmap.c +16
-0+INIT_LIST_HEAD (&page->lru); + +while (nr--) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +list_add (&virt_to_page (pte_p)->lru, &page->lru); + } + +return 0 ; +out: +vmemmap_pgtable_free (page); +return -ENOMEM ; +}
Signature · 1 line
Mike Kravetz
PATCH v4 6/21 mm/bootmem_info: Introduce {free,prepare}_vmemmap_page()
In reply to Muchun SongIn the later patch, we can use the free_vmemmap_page() to free the unused vmemmap pages and initialize a page for vmemmap page using via prepare_vmemmap_page().
--- include/linux/bootmem_info.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
include/linux/bootmem_info.h +24 -0
@@ -3,6 +3,7 @@#define __LINUX_BOOTMEM_INFO_H#include <linux/mmzone.h> +#include <linux/mm.h> /* * Types for free bootmem stored in page->lru.next. These have to be in
@@ -22,6 +23,29 @@ void __init register_page_bootmem_info_node(struct pglist_data *pgdat);void get_page_bootmem (unsigned long info,struct page *page,unsigned long type);void put_page_bootmem (struct page *page); + +static inline void free_vmemmap_page (struct page *page) +{ +VM_WARN_ON (!PageReserved (page) ||page_ref_count (page) !=2 ); + +/* bootmem page has reserved flag in the reserve_bootmem_region */ +if (PageReserved (page)) { +unsigned long magic = (unsigned long )page->freelist; + +if (magic ==SECTION_INFO || magic ==MIX_SECTION_INFO ) +put_page_bootmem (page); +else +WARN_ON (1 ); + } +} + +static inline void prepare_vmemmap_page (struct page *page) +{ +unsigned long section_nr =pfn_to_section_nr (page_to_pfn (page)); + +get_page_bootmem (section_nr, page,SECTION_INFO ); +mark_page_reserved (page); +}#else static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) {
Signature · 1 line
2.11.0
PATCH v4 7/21 mm/bootmem_info: Combine bootmem info and type into page->freelist
In reply to Muchun SongThe page->private shares storage with page->ptl. In the later patch, we will use the page->ptl. So here we combine bootmem info and type into page->freelist so that we can do not use page->private.
--- arch/x86/mm/init_64.c | 2 +- include/linux/bootmem_info.h | 18 ++++++++++++++++-- mm/bootmem_info.c | 12 ++++++------ mm/sparse.c | 4 ++-- 4 files changed, 25 insertions(+), 11 deletions(-)
arch/x86/mm/init_64.c +1 -1
@@ -883,7 +883,7 @@ static void __meminit free_pagetable(struct page *page, int order)if (PageReserved (page)) {__ClearPageReserved (page); - magic = (unsigned long )page->freelist; + magic =page_bootmem_type (page);if (magic ==SECTION_INFO || magic ==MIX_SECTION_INFO ) {while (nr_pages--)put_page_bootmem (page++);
include/linux/bootmem_info.h +16 -2
@@ -6,7 +6,7 @@#include <linux/mm.h> /* -* Types for free bootmem stored in page->lru.next. These have to be in +* Types for free bootmem stored in page->freelist. These have to be in * some random range in unsigned long space for debugging purposes. */ enum {
@@ -17,6 +17,20 @@ enum { MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE = NODE_INFO, }; +#define BOOTMEM_TYPE_BITS (ilog2(MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE) + 1) +#define BOOTMEM_TYPE_MAX ((1UL << BOOTMEM_TYPE_BITS) - 1) +#define BOOTMEM_INFO_MAX (ULONG_MAX >> BOOTMEM_TYPE_BITS) + +static inline unsigned long page_bootmem_type (struct page *page) +{ +return (unsigned long )page->freelist &BOOTMEM_TYPE_MAX ; +} + +static inline unsigned long page_bootmem_info (struct page *page) +{ +return (unsigned long )page->freelist >>BOOTMEM_TYPE_BITS ; +} +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void __init register_page_bootmem_info_node (struct pglist_data *pgdat);
@@ -30,7 +44,7 @@ static inline void free_vmemmap_page(struct page *page)/* bootmem page has reserved flag in the reserve_bootmem_region */ if (PageReserved (page)) { -unsigned long magic = (unsigned long )page->freelist; +unsigned long magic =page_bootmem_type (page);if (magic ==SECTION_INFO || magic ==MIX_SECTION_INFO )put_page_bootmem (page);
mm/bootmem_info.c +6 -6
@@ -12,9 +12,9 @@void get_page_bootmem (unsigned long info,struct page *page,unsigned long type) { - page->freelist = (void *)type; -SetPagePrivate (page); -set_page_private (page, info); +BUG_ON (info >BOOTMEM_INFO_MAX ); +BUG_ON (type >BOOTMEM_TYPE_MAX ); + page->freelist = (void *)((info <<BOOTMEM_TYPE_BITS ) | type);page_ref_inc (page); }
@@ -22,14 +22,12 @@ void put_page_bootmem(struct page *page) {unsigned long type; - type = (unsigned long ) page->freelist; + type =page_bootmem_type (page);BUG_ON (type <MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || type >MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE );if (page_ref_dec_return (page) ==1 ) { page->freelist =NULL ; -ClearPagePrivate (page); -set_page_private (page,0 );INIT_LIST_HEAD (&page->lru);free_reserved_page (page); }
@@ -101,6 +99,8 @@ void __init register_page_bootmem_info_node(struct pglist_data *pgdat)int node = pgdat->node_id;struct page *page; +BUILD_BUG_ON (MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE >BOOTMEM_TYPE_MAX ); + nr_pages =PAGE_ALIGN (sizeof (struct pglist_data )) >>PAGE_SHIFT ; page =virt_to_page (pgdat);
mm/sparse.c +2 -2
@@ -740,12 +740,12 @@ static void free_map_bootmem(struct page *memmap) >> PAGE_SHIFT;for (i =0 ; i < nr_pages; i++, page++) { - magic = (unsigned long ) page->freelist; + magic =page_bootmem_type (page);BUG_ON (magic ==NODE_INFO ); maps_section_nr =pfn_to_section_nr (page_to_pfn (page)); - removing_section_nr =page_private (page); + removing_section_nr =page_bootmem_info (page);/* * When this function is called, the removing section is
Signature · 1 line
2.11.0
PATCH v4 8/21 mm/hugetlb: Initialize page table lock for vmemmap
In reply to Muchun SongIn the later patch, we will use the vmemmap page table lock to guard the splitting of the vmemmap PMD. So initialize the vmemmap page table lock.
--- mm/hugetlb_vmemmap.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
mm/hugetlb_vmemmap.c +69 -0
@@ -71,6 +71,8 @@*/ #define pr_fmt (fmt) "HugeTLB Vmemmap: " fmt +#include <linux/pagewalk.h> +#include <linux/mmzone.h> #include <linux/list.h> #include <asm/pgalloc.h> #include "hugetlb_vmemmap.h"
@@ -179,3 +181,70 @@ void __init hugetlb_vmemmap_init(struct hstate *h)pr_debug ("can free %d vmemmap pages for %s\n" , h->nr_free_vmemmap_pages, h->name); } + +static int __init vmemmap_pud_entry (pud_t *pud,unsigned long addr, +unsigned long next,struct mm_walk *walk) +{ +struct page *page =pud_page (*pud); + +/* +* The page->private shares storage with page->ptl. So make sure +* that the PG_private is not set and initialize page->private to +* zero. +*/ +VM_BUG_ON_PAGE (PagePrivate (page), page); +set_page_private (page,0 ); + +BUG_ON (!pmd_ptlock_init (page)); + +return 0 ; +} + +static void __init vmemmap_ptlock_init_section (unsigned long start_pfn) +{ +unsigned long section_nr; +struct mem_section *ms; +struct page *memmap, *memmap_end; +struct mm_struct *mm = &init_mm; + +const struct mm_walk_ops ops = { + .pud_entry = vmemmap_pud_entry, + }; + + section_nr =pfn_to_section_nr (start_pfn); + ms =__nr_to_section (section_nr); + memmap =sparse_decode_mem_map (ms->section_mem_map, section_nr); + memmap_end = memmap +PAGES_PER_SECTION ; + +mmap_read_lock (mm); +BUG_ON (walk_page_range_novma (mm, (unsigned long )memmap, + (unsigned long )memmap_end, + &ops,NULL ,NULL )); +mmap_read_unlock (mm); +} + +static void __init vmemmap_ptlock_init_node (int nid) +{ +unsigned long pfn, end_pfn; +struct pglist_data *pgdat =NODE_DATA (nid); + + pfn = pgdat->node_start_pfn; + end_pfn =pgdat_end_pfn (pgdat); + +for (; pfn < end_pfn; pfn +=PAGES_PER_SECTION ) +vmemmap_ptlock_init_section (pfn); +} + +static int __init vmemmap_ptlock_init (void ) +{ +int nid; + +if (!hugepages_supported ()) +return 0 ; + +for_each_online_node (nid) +vmemmap_ptlock_init_node (nid); + +return 0 ; +} +core_initcall (vmemmap_ptlock_init);
Signature · 1 line
2.11.0
PATCH v4 9/21 mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
In reply to Muchun SongWhen we allocate a hugetlb page from the buddy, we should free the unused vmemmap pages associated with it. We can do that in the prep_new_huge_page().
--- arch/x86/include/asm/hugetlb.h | 9 ++ arch/x86/include/asm/pgtable_64_types.h | 8 ++ mm/hugetlb.c | 16 +++ mm/hugetlb_vmemmap.c | 188 ++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 5 + 5 files changed, 226 insertions(+)
arch/x86/include/asm/hugetlb.h +9 -0
@@ -4,6 +4,15 @@#include <asm/page.h> #include <asm-generic/hugetlb.h> +#include <asm/pgtable.h> + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge (pmd_t *pmd) +{ +return pmd_large (*pmd); +} +#endif #define hugepages_supported () boot_cpu_has(X86_FEATURE_PSE)
arch/x86/include/asm/pgtable_64_types.h +8 -0
@@ -139,6 +139,14 @@ extern unsigned int ptrs_per_p4d;# define VMEMMAP_START __VMEMMAP_BASE_L4#endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ +/* +* VMEMMAP_SIZE - allows the whole linear region to be covered by +* a struct page array. +*/ +#define VMEMMAP_SIZE (1UL << (__VIRTUAL_MASK_SHIFT - PAGE_SHIFT - \ + 1 + ilog2(sizeof(struct page)))) +#define VMEMMAP_END (VMEMMAP_START + VMEMMAP_SIZE) +#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1)#define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE)
mm/hugetlb.c +16 -0
@@ -1499,6 +1499,14 @@ void free_huge_page(struct page *page)static void prep_new_huge_page (struct hstate *h,struct page *page,int nid) { +free_huge_page_vmemmap (h, page); +/* +* Because we store preallocated pages on @page->lru, +* vmemmap_pgtable_free() must be called before the +* initialization of @page->lru in INIT_LIST_HEAD(). +*/ +vmemmap_pgtable_free (page); +INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );set_hugetlb_cgroup (page,NULL );
@@ -1751,6 +1759,14 @@ static struct page *alloc_fresh_huge_page(struct hstate *h,if (!page)return NULL ; +if (vmemmap_pgtable_prealloc (h, page)) { +if (hstate_is_gigantic (h)) +free_gigantic_page (page,huge_page_order (h)); +else +put_page (page); +return NULL ; + } +if (hstate_is_gigantic (h))prep_compound_gigantic_page (page,huge_page_order (h));prep_new_huge_page (h, page,page_to_nid (page));
mm/hugetlb_vmemmap.c +188 -0
212 lines not on this page · raw message
mm/hugetlb_vmemmap.h +5 -0
@@ -15,6 +15,7 @@void __init hugetlb_vmemmap_init (struct hstate *h);int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page);void vmemmap_pgtable_free (struct page *page); +void free_huge_page_vmemmap (struct hstate *h,struct page *head);#else static inline void hugetlb_vmemmap_init (struct hstate *h) {
@@ -28,5 +29,9 @@ static inline int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)static inline void vmemmap_pgtable_free (struct page *page) { } + +static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +}#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */
Signature · 1 line
2.11.0
RE: [PATCH v4 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 259 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Muchun Song
Sent: Saturday, November 14, 2020 12:00 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Muchun Song <[email protected]>
Subject: [PATCH v4 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb pageWhen we allocate a hugetlb page from the buddy, we should free the unused vmemmap pages associated with it. We can do that in the
prep_new_huge_page().Signed-off-by Muchun Song <[email protected]>--- arch/x86/include/asm/hugetlb.h | 9 ++ arch/x86/include/asm/pgtable_64_types.h | 8 ++ mm/hugetlb.c | 16 +++ mm/hugetlb_vmemmap.c | 188 ++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 5 + 5 files changed, 226 insertions(+)arch/x86/include/asm/hugetlb.h +9
-0@@ -4,6 +4,15 @@#include <asm/page.h> #include <asm-generic/hugetlb.h> +#include <asm/pgtable.h> + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge (pmd_t *pmd) +{ +return pmd_large (*pmd); +} +#endif #define hugepages_supported () boot_cpu_has(X86_FEATURE_PSE)arch/x86/include/asm/pgtable_64_types.h +8
-0@@ -139,6 +139,14 @@ extern unsigned int ptrs_per_p4d;# define VMEMMAP_START __VMEMMAP_BASE_L4#endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ +/* +* VMEMMAP_SIZE - allows the whole linear region to be covered by +* a struct page array. +*/ +#define VMEMMAP_SIZE (1UL << (__VIRTUAL_MASK_SHIFT - PAGE_SHIFT - \ + 1 + ilog2(sizeof(struct page)))) +#define VMEMMAP_END (VMEMMAP_START + VMEMMAP_SIZE) +#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1)#define
MODULES_VADDR(__START_KERNEL_map+KERNEL_IMAGE_SIZE)mm/hugetlb.c +16
-0@@ -1499,6 +1499,14 @@ void free_huge_page(struct page *page)static void prep_new_huge_page (struct hstate *h,struct page *page,int nid) { +free_huge_page_vmemmap (h, page); +/* +* Because we store preallocated pages on @page->lru, +* vmemmap_pgtable_free() must be called before the +* initialization of @page->lru in INIT_LIST_HEAD(). +*/ +vmemmap_pgtable_free (page); +INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );set_hugetlb_cgroup (page,NULL );@@ -1751,6 +1759,14 @@ static struct page *alloc_fresh_huge_page(struct hstate *h, if (!page)return NULL ; +if (vmemmap_pgtable_prealloc (h, page)) { +if (hstate_is_gigantic (h)) +free_gigantic_page (page,huge_page_order (h)); +else +put_page (page); +return NULL ; + } +if (hstate_is_gigantic (h))prep_compound_gigantic_page (page,huge_page_order (h));mm/hugetlb.c +0
-0prep_new_huge_page (h, page,page_to_nid (page));mm/hugetlb_vmemmap.c +3
-0@@ -74,6 +74,7 @@#include <linux/pagewalk.h> #include <linux/mmzone.h> #include <linux/list.h> +#include <linux/bootmem_info.h> #include <asm/pgalloc.h> #include "hugetlb_vmemmap.h" @@ -86,6 +87,8 @@* reserve at least 2 pages as vmemmap areas. */ #define RESERVE_VMEMMAP_NR 2U +#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) +#define TAIL_PAGE_REUSE -1#ifndef VMEMMAP_HPAGE_SHIFT #define
VMEMMAP_HPAGE_SHIFTHPAGE_SHIFTmm/hugetlb_vmemmap.c +15
-0@@ -97,6 +100,21 @@#define page_huge_pte (page) ((page)->pmd_huge_pte) +#define vmemmap_hpage_addr_end (addr, end) \ +({ \ + unsigned long __boundary; \ + __boundary = ((addr) + VMEMMAP_HPAGE_SIZE) &VMEMMAP_HPAGE_MASK ; \ + (__boundary -1 < (end) -1 ) ? __boundary : (end); \ +}) + +#ifndef vmemmap_pmd_huge +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge (pmd_t *pmd) +{ +return pmd_huge (*pmd); +} +#endif +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) {mm/hugetlb_vmemmap.c +97
-0return h->nr_free_vmemmap_pages;@@ -158,6 +176,176 @@ int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)return -ENOMEM ; } +/* +* Walk a vmemmap address to the pmd it maps. +*/ +static pmd_t *vmemmap_to_pmd (unsigned long page) +{ +pgd_t *pgd; +p4d_t *p4d; +pud_t *pud; +pmd_t *pmd; + +if (page <VMEMMAP_START || page >=VMEMMAP_END ) +return NULL ; + + pgd =pgd_offset_k (page); +if (pgd_none (*pgd)) +return NULL ; + p4d =p4d_offset (pgd, page); +if (p4d_none (*p4d)) +return NULL ; + pud =pud_offset (p4d, page); + +if (pud_none (*pud) ||pud_bad (*pud)) +return NULL ; + pmd =pmd_offset (pud, page); + +return pmd; +} + +static inline spinlock_t *vmemmap_pmd_lock (pmd_t *pmd) +{ +return pmd_lock (&init_mm, pmd); +} + +static inline int freed_vmemmap_hpage (struct page *page) +{ +return atomic_read (&page->_mapcount) +1 ; +} + +static inline int freed_vmemmap_hpage_inc (struct page *page) +{ +return atomic_inc_return_relaxed (&page->_mapcount) +1 ; +} + +static inline int freed_vmemmap_hpage_dec (struct page *page) +{ +return atomic_dec_return_relaxed (&page->_mapcount) +1 ; +} + +static inline void free_vmemmap_page_list (struct list_head *list) +{ +struct page *page, *next; + +list_for_each_entry_safe (page, next, list, lru) { +list_del (&page->lru); +free_vmemmap_page (page); + } +} + +static void __free_huge_page_pte_vmemmap (struct page *reuse,pte_t *ptep, +unsigned long start, +unsigned long end, +struct list_head *free_pages) +{ +/* Make the tail pages are mapped read-only. */ +pgprot_t pgprot =PAGE_KERNEL_RO ; +pte_t entry =mk_pte (reuse, pgprot); +unsigned long addr; + +for (addr = start; addr < end; addr +=PAGE_SIZE , ptep++) { +struct page *page; +pte_t old = *ptep; + +VM_WARN_ON (!pte_present (old)); + page =pte_page (old); +list_add (&page->lru, free_pages); + +set_pte_at (&init_mm, addr, ptep, entry); + } +} + +static void __free_huge_page_pmd_vmemmap (struct hstate *h,pmd_t *pmd, +unsigned long addr, +struct list_head *free_pages) +{ +unsigned long next; +unsigned long start = addr +RESERVE_VMEMMAP_SIZE ; +unsigned long end = addr +vmemmap_pages_size_per_hpage (h); +struct page *reuse =NULL ; + + addr = start; + do { + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[TAIL_PAGE_REUSE]); +
mm/hugetlb_vmemmap.c int vmemmap_pgtable_prealloc(struct hstate *h, · line 286
Quoted 10 lines
mm/hugetlb_vmemmap.c +9
-0+ next =vmemmap_hpage_addr_end (addr, end); +__free_huge_page_pte_vmemmap (reuse, ptep, addr, next, + free_pages); + }while (pmd++, addr = next, addr != end); + +flush_tlb_kernel_range (start, end); +} + +static void split_vmemmap_pmd (pmd_t *pmd,pte_t *pte_p,unsigned long addr)
Hi Muchun,
Are you going to restore the pmd mapping after you free the hugetlb? I mean, When you free continuous 128MB hugetlb pages with 2MB size, will you redo the PMD vmemmap since 2MB PMD can just contain the page struct of 128MB memory?
If no, wouldn't it be simpler to only use base pages while populating vmemmap? I mean, once we enable the Kconfig option you add for VMEMMAP_FREE, we only use base pages to place "page struct" but not split PMD into base pages afterwards.
One negative side effect might be that base pages are also used for those pages which won't be hugetlb later. but if most pages of host will be hugetlb for guest and SPDK, it shouldn't hurt too much.
Or at least this can be done for hugetlb reserved by cmdline?
Quoted 90 lines
mm/hugetlb_vmemmap.c +64
-0+{ +int i; +pgprot_t pgprot =PAGE_KERNEL ; +struct mm_struct *mm = &init_mm; +struct page *page; +pmd_t old_pmd, _pmd; + + old_pmd =READ_ONCE (*pmd); + page =pmd_page (old_pmd); +pmd_populate_kernel (mm, &_pmd, pte_p); + +for (i =0 ; i <VMEMMAP_HPAGE_NR ; i++, addr +=PAGE_SIZE ) { +pte_t entry, *pte; + + entry =mk_pte (page + i, pgprot); + pte =pte_offset_kernel (&_pmd, addr); +VM_BUG_ON (!pte_none (*pte)); +set_pte_at (mm, addr, pte, entry); + } + +/* make pte visible before pmd */ +smp_wmb (); +pmd_populate_kernel (mm, pmd, pte_p); +} + +static void split_vmemmap_huge_page (struct page *head,pmd_t *pmd) +{ +struct page *pte_page, *t_page; +unsigned long start = (unsigned long )head &VMEMMAP_HPAGE_MASK ; +unsigned long addr = start; + +list_for_each_entry_safe (pte_page, t_page, &head->lru, lru) { +list_del (&pte_page->lru); +VM_BUG_ON (freed_vmemmap_hpage (pte_page)); +split_vmemmap_pmd (pmd++,page_to_virt (pte_page), addr); + addr +=VMEMMAP_HPAGE_SIZE ; + } + +flush_tlb_kernel_range (start, addr); +} + +void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +pmd_t *pmd; +spinlock_t *ptl; +LIST_HEAD (free_pages); + +if (!free_vmemmap_pages_per_hpage (h)) +return ; + + pmd =vmemmap_to_pmd ((unsigned long )head); +BUG_ON (!pmd); + + ptl =vmemmap_pmd_lock (pmd); +if (vmemmap_pmd_huge (pmd)) +split_vmemmap_huge_page (head, pmd); + +__free_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, &free_pages); +freed_vmemmap_hpage_inc (pmd_page (*pmd)); +spin_unlock (ptl); + +free_vmemmap_page_list (&free_pages); +} +void
__inithugetlb_vmemmap_init(struct hstate *h){mm/hugetlb_vmemmap.c +0
-0unsigned int order =huge_page_order (h);mm/hugetlb_vmemmap.h +5
-0@@ -15,6 +15,7 @@void __init hugetlb_vmemmap_init (struct hstate *h);int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page);void vmemmap_pgtable_free (struct page *page); +void free_huge_page_vmemmap (struct hstate *h,struct page *head);#else static inline void hugetlb_vmemmap_init (struct hstate *h) {@@ -28,5 +29,9 @@ static inline int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)static inline void vmemmap_pgtable_free (struct page *page) { } + +static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +}
Quoted 5 lines
#endif /*
CONFIG_HUGETLB_PAGE_FREE_VMEMMAP*/
#endif /*_LINUX_HUGETLB_VMEMMAP_H*/
--
2.11.0
Thanks
Barry
Re: [External] RE: [PATCH v4 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 273 lines · Song Bao Hua (Barry Song)
Quoted 260 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Muchun Song
Sent: Saturday, November 14, 2020 12:00 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Muchun Song <[email protected]>
Subject: [PATCH v4 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb pageWhen we allocate a hugetlb page from the buddy, we should free the unused vmemmap pages associated with it. We can do that in the
prep_new_huge_page().Signed-off-by Muchun Song <[email protected]>--- arch/x86/include/asm/hugetlb.h | 9 ++ arch/x86/include/asm/pgtable_64_types.h | 8 ++ mm/hugetlb.c | 16 +++ mm/hugetlb_vmemmap.c | 188 ++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 5 + 5 files changed, 226 insertions(+)arch/x86/include/asm/hugetlb.h +9
-0@@ -4,6 +4,15 @@#include <asm/page.h> #include <asm-generic/hugetlb.h> +#include <asm/pgtable.h> + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge (pmd_t *pmd) +{ +return pmd_large (*pmd); +} +#endif #define hugepages_supported () boot_cpu_has(X86_FEATURE_PSE)arch/x86/include/asm/pgtable_64_types.h +8
-0@@ -139,6 +139,14 @@ extern unsigned int ptrs_per_p4d;# define VMEMMAP_START __VMEMMAP_BASE_L4#endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ +/* +* VMEMMAP_SIZE - allows the whole linear region to be covered by +* a struct page array. +*/ +#define VMEMMAP_SIZE (1UL << (__VIRTUAL_MASK_SHIFT - PAGE_SHIFT - \ + 1 + ilog2(sizeof(struct page)))) +#define VMEMMAP_END (VMEMMAP_START + VMEMMAP_SIZE) +#define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1)#define
MODULES_VADDR(__START_KERNEL_map+KERNEL_IMAGE_SIZE)mm/hugetlb.c +16
-0@@ -1499,6 +1499,14 @@ void free_huge_page(struct page *page)static void prep_new_huge_page (struct hstate *h,struct page *page,int nid) { +free_huge_page_vmemmap (h, page); +/* +* Because we store preallocated pages on @page->lru, +* vmemmap_pgtable_free() must be called before the +* initialization of @page->lru in INIT_LIST_HEAD(). +*/ +vmemmap_pgtable_free (page); +INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );set_hugetlb_cgroup (page,NULL );@@ -1751,6 +1759,14 @@ static struct page *alloc_fresh_huge_page(struct hstate *h, if (!page)return NULL ; +if (vmemmap_pgtable_prealloc (h, page)) { +if (hstate_is_gigantic (h)) +free_gigantic_page (page,huge_page_order (h)); +else +put_page (page); +return NULL ; + } +if (hstate_is_gigantic (h))prep_compound_gigantic_page (page,huge_page_order (h));prep_new_huge_page(h, page, page_to_nid(page));mm/hugetlb_vmemmap.c +3
-0@@ -74,6 +74,7 @@#include <linux/pagewalk.h> #include <linux/mmzone.h> #include <linux/list.h> +#include <linux/bootmem_info.h> #include <asm/pgalloc.h> #include "hugetlb_vmemmap.h" @@ -86,6 +87,8 @@* reserve at least 2 pages as vmemmap areas. */ #define RESERVE_VMEMMAP_NR 2U +#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) +#define TAIL_PAGE_REUSE -1#ifndef VMEMMAP_HPAGE_SHIFT #define
VMEMMAP_HPAGE_SHIFTHPAGE_SHIFTmm/hugetlb_vmemmap.c +15
-0@@ -97,6 +100,21 @@#define page_huge_pte (page) ((page)->pmd_huge_pte) +#define vmemmap_hpage_addr_end (addr, end) \ +({ \ + unsigned long __boundary; \ + __boundary = ((addr) + VMEMMAP_HPAGE_SIZE) &VMEMMAP_HPAGE_MASK ; \ + (__boundary -1 < (end) -1 ) ? __boundary : (end); \ +}) + +#ifndef vmemmap_pmd_huge +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge (pmd_t *pmd) +{ +return pmd_huge (*pmd); +} +#endif +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) {return h->nr_free_vmemmap_pages;mm/hugetlb_vmemmap.c +97
-0@@ -158,6 +176,176 @@ int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)return -ENOMEM ; } +/* +* Walk a vmemmap address to the pmd it maps. +*/ +static pmd_t *vmemmap_to_pmd (unsigned long page) +{ +pgd_t *pgd; +p4d_t *p4d; +pud_t *pud; +pmd_t *pmd; + +if (page <VMEMMAP_START || page >=VMEMMAP_END ) +return NULL ; + + pgd =pgd_offset_k (page); +if (pgd_none (*pgd)) +return NULL ; + p4d =p4d_offset (pgd, page); +if (p4d_none (*p4d)) +return NULL ; + pud =pud_offset (p4d, page); + +if (pud_none (*pud) ||pud_bad (*pud)) +return NULL ; + pmd =pmd_offset (pud, page); + +return pmd; +} + +static inline spinlock_t *vmemmap_pmd_lock (pmd_t *pmd) +{ +return pmd_lock (&init_mm, pmd); +} + +static inline int freed_vmemmap_hpage (struct page *page) +{ +return atomic_read (&page->_mapcount) +1 ; +} + +static inline int freed_vmemmap_hpage_inc (struct page *page) +{ +return atomic_inc_return_relaxed (&page->_mapcount) +1 ; +} + +static inline int freed_vmemmap_hpage_dec (struct page *page) +{ +return atomic_dec_return_relaxed (&page->_mapcount) +1 ; +} + +static inline void free_vmemmap_page_list (struct list_head *list) +{ +struct page *page, *next; + +list_for_each_entry_safe (page, next, list, lru) { +list_del (&page->lru); +free_vmemmap_page (page); + } +} + +static void __free_huge_page_pte_vmemmap (struct page *reuse,pte_t *ptep, +unsigned long start, +unsigned long end, +struct list_head *free_pages) +{ +/* Make the tail pages are mapped read-only. */ +pgprot_t pgprot =PAGE_KERNEL_RO ; +pte_t entry =mk_pte (reuse, pgprot); +unsigned long addr; + +for (addr = start; addr < end; addr +=PAGE_SIZE , ptep++) { +struct page *page; +pte_t old = *ptep; + +VM_WARN_ON (!pte_present (old)); + page =pte_page (old); +list_add (&page->lru, free_pages); + +set_pte_at (&init_mm, addr, ptep, entry); + } +} + +static void __free_huge_page_pmd_vmemmap (struct hstate *h,pmd_t *pmd, +unsigned long addr, +struct list_head *free_pages) +{ +unsigned long next; +unsigned long start = addr +RESERVE_VMEMMAP_SIZE ; +unsigned long end = addr +vmemmap_pages_size_per_hpage (h); +struct page *reuse =NULL ; + + addr = start; + do { + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[TAIL_PAGE_REUSE]); +mm/hugetlb_vmemmap.c int vmemmap_pgtable_prealloc(struct hstate *h, · line 286
Quoted 10 lines
mm/hugetlb_vmemmap.c +9
-0+ next =vmemmap_hpage_addr_end (addr, end); +__free_huge_page_pte_vmemmap (reuse, ptep, addr, next, + free_pages); + }while (pmd++, addr = next, addr != end); + +flush_tlb_kernel_range (start, end); +} + +static void split_vmemmap_pmd (pmd_t *pmd,pte_t *pte_p,unsigned long addr)Hi Muchun,
Quoted 4 lines
Are you going to restore the pmd mapping after you free the hugetlb? I mean, When you free continuous 128MB hugetlb pages with 2MB size, will you redo the PMD vmemmap since 2MB PMD can just contain the page struct of 128MB memory?
Now we only restore the pmd mapping for the 1GB HugeTLB page. For the 2MB HugeTLB page, we do not(I haven't figured out how to handle it gracefully).
Quoted 9 lines
If no, wouldn't it be simpler to only use base pages while populating vmemmap? I mean, once we enable the
Kconfigoption you add forVMEMMAP_FREE, we only use base pages to place "page struct" but not split PMD into base pages afterwards.One negative side effect might be that base pages are also used for those pages which won't be hugetlb later. but if most pages of host will be hugetlb for guest and SPDK, it shouldn't hurt too much.
Yeah, I agree with you. If the user uses a lot of HugeTLB pages(e.g. SPDK/Guest),
it shouldn't hurt too much. And using base pages while populating vmemmap also can decrease the overhead(of splitting PMD). In the end, if we don’t come up with
a more suitable solution to deal with it(mentioned above for 2MB HugeTLB page). Maybe this is also an idea.
Thanks.
Quoted 102 lines
Or at least this can be done for hugetlb reserved by cmdline?
Quoted 90 lines
mm/hugetlb_vmemmap.c +64
-0+{ +int i; +pgprot_t pgprot =PAGE_KERNEL ; +struct mm_struct *mm = &init_mm; +struct page *page; +pmd_t old_pmd, _pmd; + + old_pmd =READ_ONCE (*pmd); + page =pmd_page (old_pmd); +pmd_populate_kernel (mm, &_pmd, pte_p); + +for (i =0 ; i <VMEMMAP_HPAGE_NR ; i++, addr +=PAGE_SIZE ) { +pte_t entry, *pte; + + entry =mk_pte (page + i, pgprot); + pte =pte_offset_kernel (&_pmd, addr); +VM_BUG_ON (!pte_none (*pte)); +set_pte_at (mm, addr, pte, entry); + } + +/* make pte visible before pmd */ +smp_wmb (); +pmd_populate_kernel (mm, pmd, pte_p); +} + +static void split_vmemmap_huge_page (struct page *head,pmd_t *pmd) +{ +struct page *pte_page, *t_page; +unsigned long start = (unsigned long )head &VMEMMAP_HPAGE_MASK ; +unsigned long addr = start; + +list_for_each_entry_safe (pte_page, t_page, &head->lru, lru) { +list_del (&pte_page->lru); +VM_BUG_ON (freed_vmemmap_hpage (pte_page)); +split_vmemmap_pmd (pmd++,page_to_virt (pte_page), addr); + addr +=VMEMMAP_HPAGE_SIZE ; + } + +flush_tlb_kernel_range (start, addr); +} + +void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +pmd_t *pmd; +spinlock_t *ptl; +LIST_HEAD (free_pages); + +if (!free_vmemmap_pages_per_hpage (h)) +return ; + + pmd =vmemmap_to_pmd ((unsigned long )head); +BUG_ON (!pmd); + + ptl =vmemmap_pmd_lock (pmd); +if (vmemmap_pmd_huge (pmd)) +split_vmemmap_huge_page (head, pmd); + +__free_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, &free_pages); +freed_vmemmap_hpage_inc (pmd_page (*pmd)); +spin_unlock (ptl); + +free_vmemmap_page_list (&free_pages); +} +void
__inithugetlb_vmemmap_init(struct hstate *h){ unsigned int order = huge_page_order(h);mm/hugetlb_vmemmap.h +5
-0@@ -15,6 +15,7 @@void __init hugetlb_vmemmap_init (struct hstate *h);int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page);void vmemmap_pgtable_free (struct page *page); +void free_huge_page_vmemmap (struct hstate *h,struct page *head);#else static inline void hugetlb_vmemmap_init (struct hstate *h) {@@ -28,5 +29,9 @@ static inline int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)static inline void vmemmap_pgtable_free (struct page *page) { } + +static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +}Quoted 5 lines
#endif /*
CONFIG_HUGETLB_PAGE_FREE_VMEMMAP*/
#endif /*_LINUX_HUGETLB_VMEMMAP_H*/
--
2.11.0Thanks
Barry
Signature · 2 lines
Yours, Muchun
RE: [External] RE: [PATCH v4 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 315 lines
-----Original Message-----
From: Muchun Song [mailto:[email protected]]
Sent: Tuesday, November 17, 2020 11:27 PM
To: Song Bao Hua (Barry Song) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [External] RE: [PATCH v4 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb pageQuoted 13 lines · Song Bao Hua (Barry Song)
Quoted 13 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Muchun Song
Sent: Saturday, November 14, 2020 12:00 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected];Quoted 5 lines
Quoted 5 lines
[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Muchun SongQuoted 1 line
Quoted 1 line
Subject: [PATCH v4 09/21] mm/hugetlb: Free the vmemmap pages
associated
Quoted 16 lines
Quoted 16 lines
with each hugetlb page
When we allocate a hugetlb page from the buddy, we should free the unused vmemmap pages associated with it. We can do that in the
prep_new_huge_page().Signed-off-by Muchun Song <[email protected]>--- arch/x86/include/asm/hugetlb.h | 9 ++ arch/x86/include/asm/pgtable_64_types.h | 8 ++ mm/hugetlb.c | 16 +++ mm/hugetlb_vmemmap.c | 188 ++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 5 + 5 files changed, 226 insertions(+)Quoted 1 line
Review
Quoted 1 line
(quoted diff) +0
-0
b/arch/x86/include/asm/hugetlb.hQuoted 28 lines
Quoted 28 lines
index
1721b1aadeb1..c601fe042832100644arch/x86/include/asm/hugetlb.h +9
-0@@ -4,6 +4,15 @@#include <asm/page.h> #include <asm-generic/hugetlb.h> +#include <asm/pgtable.h> + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge (pmd_t *pmd) +{ +return pmd_large (*pmd); +} +#endif #define hugepages_supported () boot_cpu_has(X86_FEATURE_PSE)arch/x86/include/asm/pgtable_64_types.h +0
-0@@ -139,6 +139,14 @@ extern unsigned int ptrs_per_p4d;# define VMEMMAP_START __VMEMMAP_BASE_L4#endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ Quoted 7 lines
arch/x86/include/asm/pgtable_64_types.h extern unsigned int ptrs_per_p4d;
Quoted 7 lines
arch/x86/include/asm/pgtable_64_types.h +6
-0+/* +* VMEMMAP_SIZE - allows the whole linear region to be covered by +* a struct page array. +*/ +#define VMEMMAP_SIZE (1UL << (__VIRTUAL_MASK_SHIFT -PAGE_SHIFT - \ + 1 + ilog2(sizeof(structpage))))
Quoted 1 line
arch/x86/include/asm/pgtable_64_types.h extern unsigned int ptrs_per_p4d; · line 149
Quoted 1 line
arch/x86/include/asm/pgtable_64_types.h +1
-0+#define VMEMMAP_END (VMEMMAP_START +
VMEMMAP_SIZE)Quoted 2 lines
arch/x86/include/asm/pgtable_64_types.h extern unsigned int ptrs_per_p4d; · line 151
Quoted 2 lines
arch/x86/include/asm/pgtable_64_types.h +1
-0+#define VMALLOC_END (VMALLOC_START +(
VMALLOC_SIZE_TB<<Quoted 11 lines
mm/hugetlb.c void free_huge_page(struct page *page) · line 1500
Quoted 11 lines
arch/x86/include/asm/pgtable_64_types.h +0
-040) - 1)#define
MODULES_VADDR(__START_KERNEL_map+KERNEL_IMAGE_SIZE)mm/hugetlb.c +0
-0@@ -1499,6 +1499,14 @@ void free_huge_page(struct page *page) static void prep_new_huge_page(struct hstate *h, struct page *page, intnid)
Quoted 5 lines
Quoted 5 lines
mm/hugetlb.c +4
-0{ +free_huge_page_vmemmap (h, page); +/* +* Because we store preallocated pages on @page->lru, +* vmemmap_pgtable_free() must be called before the Quoted 8 lines
mm/hugetlb.c static struct page
Quoted 8 lines
mm/hugetlb.c +4
-0+* initialization of @page->lru in INIT_LIST_HEAD(). +*/ +vmemmap_pgtable_free (page); +INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );set_hugetlb_cgroup (page,NULL );@@ -1751,6 +1759,14 @@ static struct page*alloc_fresh_huge_page(struct
Quoted 4 lines
Quoted 4 lines
mm/hugetlb.c +0
-0hstate *h, if (!page)return NULL ;Quoted 10 lines
mm/hugetlb.c static struct page *alloc_fresh_huge_page(struct hstate *h,
Quoted 10 lines
mm/hugetlb.c +8
-0+if (vmemmap_pgtable_prealloc (h, page)) { +if (hstate_is_gigantic (h)) +free_gigantic_page (page,huge_page_order (h)); +else +put_page (page); +return NULL ; + } +if (hstate_is_gigantic (h)) prep_compound_gigantic_page(page,
huge_page_order(h));Quoted 13 lines
Quoted 13 lines
prep_new_huge_page(h, page, page_to_nid(page));mm/hugetlb_vmemmap.c +1
-0@@ -74,6 +74,7 @@#include <linux/pagewalk.h> #include <linux/mmzone.h> #include <linux/list.h> +#include <linux/bootmem_info.h> #include <asm/pgalloc.h> #include "hugetlb_vmemmap.h" Quoted 5 lines
mm/hugetlb_vmemmap.c line 90
Quoted 5 lines
mm/hugetlb_vmemmap.c +1
-0@@ -86,6 +87,8 @@* reserve at least 2 pages as vmemmap areas. */ #define RESERVE_VMEMMAP_NR 2U +#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR<<
Quoted 10 lines
mm/hugetlb_vmemmap.c line 103
Quoted 10 lines
mm/hugetlb_vmemmap.c +1
-0PAGE_SHIFT) +#define TAIL_PAGE_REUSE -1#ifndef VMEMMAP_HPAGE_SHIFT #define
VMEMMAP_HPAGE_SHIFTHPAGE_SHIFTmm/hugetlb_vmemmap.c +1
-0@@ -97,6 +100,21 @@#define page_huge_pte (page) ((page)->pmd_huge_pte) +#define vmemmap_hpage_addr_end (addr, end)\
Quoted 1 line
Quoted 1 line
+({ \Quoted 1 line
Quoted 1 line
+ unsigned long
__boundary;\
Quoted 6 lines
Quoted 6 lines
+
__boundary= ((addr) +VMEMMAP_HPAGE_SIZE) &VMEMMAP_HPAGE_MASK; \mm/hugetlb_vmemmap.c +4
-0+ (__boundary -1 < (end) -1 ) ? __boundary : (end); \ +}) + +#ifndef vmemmap_pmd_hugeQuoted 8 lines
mm/hugetlb_vmemmap.c
Quoted 8 lines
mm/hugetlb_vmemmap.c +7
-0+#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge (pmd_t *pmd) +{ +return pmd_huge (*pmd); +} +#endif + static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate*h)
Quoted 58 lines
Quoted 58 lines
{ return h->nr_free_vmemmap_pages;mm/hugetlb_vmemmap.c +51
-0@@ -158,6 +176,176 @@ int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)return -ENOMEM ; } +/* +* Walk a vmemmap address to the pmd it maps. +*/ +static pmd_t *vmemmap_to_pmd (unsigned long page) +{ +pgd_t *pgd; +p4d_t *p4d; +pud_t *pud; +pmd_t *pmd; + +if (page <VMEMMAP_START || page >=VMEMMAP_END ) +return NULL ; + + pgd =pgd_offset_k (page); +if (pgd_none (*pgd)) +return NULL ; + p4d =p4d_offset (pgd, page); +if (p4d_none (*p4d)) +return NULL ; + pud =pud_offset (p4d, page); + +if (pud_none (*pud) ||pud_bad (*pud)) +return NULL ; + pmd =pmd_offset (pud, page); + +return pmd; +} + +static inline spinlock_t *vmemmap_pmd_lock (pmd_t *pmd) +{ +return pmd_lock (&init_mm, pmd); +} + +static inline int freed_vmemmap_hpage (struct page *page) +{ +return atomic_read (&page->_mapcount) +1 ; +} + +static inline int freed_vmemmap_hpage_inc (struct page *page) +{ +return atomic_inc_return_relaxed (&page->_mapcount) +1 ; +} + +static inline int freed_vmemmap_hpage_dec (struct page *page) +{ +return atomic_dec_return_relaxed (&page->_mapcount) +1 ; +} + +static inline void free_vmemmap_page_list (struct list_head *list) +{ +struct page *page, *next;Quoted 8 lines
mm/hugetlb_vmemmap.c int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)
Quoted 8 lines
mm/hugetlb_vmemmap.c +8
-0+ +list_for_each_entry_safe (page, next, list, lru) { +list_del (&page->lru); +free_vmemmap_page (page); + } +} + +static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t*ptep,
Quoted 14 lines
Quoted 14 lines
mm/hugetlb_vmemmap.c +14
-0+unsigned long start, + unsigned long end, + struct list_head *free_pages) +{ +/* Make the tail pages are mapped read-only. */ + pgprot_t pgprot = PAGE_KERNEL_RO; +pte_t entry =mk_pte (reuse, pgprot); +unsigned long addr; + +for (addr = start; addr < end; addr +=PAGE_SIZE , ptep++) { +struct page *page; +pte_t old = *ptep; + +VM_WARN_ON (!pte_present (old));Quoted 8 lines
mm/hugetlb_vmemmap.c int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)
Quoted 8 lines
mm/hugetlb_vmemmap.c +8
-0+ page =pte_page (old); +list_add (&page->lru, free_pages); + +set_pte_at (&init_mm, addr, ptep, entry); + } +} + +static void __free_huge_page_pmd_vmemmap(struct hstate *h, pmd_t*pmd,
Quoted 17 lines
Quoted 17 lines
mm/hugetlb_vmemmap.c +17
-0+unsigned long addr, + struct list_head *free_pages) +{ + unsigned long next; +unsigned long start = addr +RESERVE_VMEMMAP_SIZE ; +unsigned long end = addr +vmemmap_pages_size_per_hpage (h); +struct page *reuse =NULL ; + + addr = start; + do { + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[TAIL_PAGE_REUSE]); + + next = vmemmap_hpage_addr_end(addr, end);Quoted 8 lines
mm/hugetlb_vmemmap.c int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)
Quoted 8 lines
mm/hugetlb_vmemmap.c +8
-0+__free_huge_page_pte_vmemmap (reuse, ptep, addr, next, + free_pages); + }while (pmd++, addr = next, addr != end); + +flush_tlb_kernel_range (start, end); +} + +static void split_vmemmap_pmd(pmd_t *pmd, pte_t *pte_p, unsignedlong
Quoted 5 lines
mm/hugetlb_vmemmap.c int vmemmap_pgtable_prealloc(struct hstate *h, · line 286
Quoted 1 line
mm/hugetlb_vmemmap.c +0
-0addr)Hi Muchun,
Are you going to restore the pmd mapping after you free the hugetlb? I
Quoted 8 lines
mean,
Quoted 3 lines
When you free continuous 128MB hugetlb pages with 2MB size, will you redo the PMD vmemmap since 2MB PMD can just contain the page struct of 128MB memory?
Now we only restore the pmd mapping for the 1GB HugeTLB page. For the 2MB HugeTLB page, we do not(I haven't figured out how to handle it gracefully).
Actually I don't expect PMD mapping is restored for this stage. If users don't change hugetlb much. that means they won't return hugetlb to kernel, then restoring the original mapping or not wouldn't be a big problem.
Quoted 13 lines
Quoted 3 lines
If no, wouldn't it be simpler to only use base pages while populating
vmemmap?
Quoted 5 lines
I mean, once we enable the
Kconfigoption you add forVMEMMAP_FREE, we only use base pages to place "page struct" but not split PMD into base pages afterwards.One negative side effect might be that base pages are also used for those
pages
Quoted 2 lines
which won't be hugetlb later. but if most pages of host will be hugetlb for guest and SPDK, it shouldn't hurt too much.
Quoted 9 lines
Yeah, I agree with you. If the user uses a lot of HugeTLB pages(e.g. SPDK/Guest),
it shouldn't hurt too much. And using base pages while populating vmemmap also
can decrease the overhead(of splitting PMD). In the end, if we don’t come up with
a more suitable solution to deal with it(mentioned above for 2MB HugeTLB page).
Maybe this is also an idea.
On ARM64, if and only if we enable "ARM64_SWAPPER_USES_SECTION_MAP", ARM64 will try to use PMD mapping for vmemmap. Otherwise, all mem_section are populated by base pages.
Right now, the patchset is very big. If in any way we can make it simpler for the first step by only using base pages, it would make the whole patchset much smaller.
PMD mapping of vmemmap would be able to help save PTE page tables for vmemmap. Eg. 32MB for each 1TB non-hugetlb.
Thanks
Barry
PATCH v4 10/21 mm/hugetlb: Defer freeing of hugetlb pages
In reply to Muchun SongIn the subsequent patch, we will allocate the vmemmap pages when free huge pages. But update_and_free_page() is be called from a non-task context(and hold hugetlb_lock), we can defer the actual freeing in a workqueue to prevent use GFP_ATOMIC to allocate the vmemmap pages.
--- mm/hugetlb.c | 98 +++++++++++++++++++++++++++++++++++++++++++++------- mm/hugetlb_vmemmap.c | 5 --- mm/hugetlb_vmemmap.h | 10 ++++++ 3 files changed, 96 insertions(+), 17 deletions(-)
mm/hugetlb.c +86 -12
@@ -1221,7 +1221,7 @@ static void destroy_compound_gigantic_page(struct page *page,__ClearPageHead (page); } -static void free_gigantic_page (struct page *page,unsigned int order) +static void __free_gigantic_page (struct page *page,unsigned int order) {/* * If the page isn't allocated using the cma allocator,
@@ -1288,20 +1288,100 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask, {return NULL ; } -static inline void free_gigantic_page (struct page *page,unsigned int order) { } +static inline void __free_gigantic_page (struct page *page, +unsigned int order) { }static inline void destroy_compound_gigantic_page (struct page *page,unsigned int order) { }#endif -static void update_and_free_page (struct hstate *h,struct page *page) +static void __free_hugepage (struct hstate *h,struct page *page); + +/* +* As update_and_free_page() is be called from a non-task context(and hold +* hugetlb_lock), we can defer the actual freeing in a workqueue to prevent +* use GFP_ATOMIC to allocate a lot of vmemmap pages. +* +* update_hpage_vmemmap_workfn() locklessly retrieves the linked list of +* pages to be freed and frees them one-by-one. As the page->mapping pointer +* is going to be cleared in update_hpage_vmemmap_workfn() anyway, it is +* reused as the llist_node structure of a lockless linked list of huge +* pages to be freed. +*/ +static LLIST_HEAD (hpage_update_freelist); + +static void update_hpage_vmemmap_workfn (struct work_struct *work) { -int i; +struct llist_node *node; +struct page *page; + + node =llist_del_all (&hpage_update_freelist); + +while (node) { + page =container_of ((struct address_space **)node, + struct page, mapping); + node = node->next; + page->mapping =NULL ; +__free_hugepage (page_hstate (page), page); +cond_resched (); + } +} +static DECLARE_WORK(hpage_update_work, update_hpage_vmemmap_workfn); + +static inline void __update_and_free_page (struct hstate *h,struct page *page) +{ +/* No need to allocate vmemmap pages */ +if (!free_vmemmap_pages_per_hpage (h)) { +__free_hugepage (h, page); +return ; + } + +/* +* Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap +* pages. +* +* Only call schedule_work() if hpage_update_freelist is previously +* empty. Otherwise, schedule_work() had been called but the workfn +* hasn't retrieved the list yet. +*/ +if (llist_add ((struct llist_node *)&page->mapping, + &hpage_update_freelist)) +schedule_work (&hpage_update_work); +} + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +static inline void free_gigantic_page (struct hstate *h,struct page *page) +{ +__free_gigantic_page (page,huge_page_order (h)); +} +#else +static inline void free_gigantic_page (struct hstate *h,struct page *page) +{ +/* +* Temporarily drop the hugetlb_lock, because +* we might block in __free_gigantic_page(). +*/ +spin_unlock (&hugetlb_lock); +__free_gigantic_page (page,huge_page_order (h)); +spin_lock (&hugetlb_lock); +} +#endif + +static void update_and_free_page (struct hstate *h,struct page *page) +{if (hstate_is_gigantic (h) && !gigantic_page_runtime_supported ())return ; h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid (page)]--; + +__update_and_free_page (h, page); +} + +static void __free_hugepage (struct hstate *h,struct page *page) +{ +int i; +for (i =0 ; i <pages_per_huge_page (h); i++) { page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | 1 << PG_dirty |
@@ -1313,14 +1393,8 @@ static void update_and_free_page(struct hstate *h, struct page *page)set_compound_page_dtor (page,NULL_COMPOUND_DTOR );set_page_refcounted (page);if (hstate_is_gigantic (h)) { -/* -* Temporarily drop the hugetlb_lock, because -* we might block in free_gigantic_page(). -*/ -spin_unlock (&hugetlb_lock);destroy_compound_gigantic_page (page,huge_page_order (h)); -free_gigantic_page (page,huge_page_order (h)); -spin_lock (&hugetlb_lock); +free_gigantic_page (h, page); }else {__free_pages (page,huge_page_order (h)); }
@@ -1761,7 +1835,7 @@ static struct page *alloc_fresh_huge_page(struct hstate *h,if (vmemmap_pgtable_prealloc (h, page)) {if (hstate_is_gigantic (h)) -free_gigantic_page (page,huge_page_order (h)); +free_gigantic_page (h, page);else put_page (page);return NULL ;
mm/hugetlb_vmemmap.c +0 -5
@@ -115,11 +115,6 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) }#endif -static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) -{ -return h->nr_free_vmemmap_pages; -} -static inline unsigned int vmemmap_pages_per_hpage (struct hstate *h) {return free_vmemmap_pages_per_hpage (h) +RESERVE_VMEMMAP_NR ;
mm/hugetlb_vmemmap.h +10 -0
@@ -16,6 +16,11 @@ void __init hugetlb_vmemmap_init(struct hstate *h);int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page);void vmemmap_pgtable_free (struct page *page);void free_huge_page_vmemmap (struct hstate *h,struct page *head); + +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) +{ +return h->nr_free_vmemmap_pages; +}#else static inline void hugetlb_vmemmap_init (struct hstate *h) {
@@ -33,5 +38,10 @@ static inline void vmemmap_pgtable_free(struct page *page)static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) { } + +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) +{ +return 0 ; +}#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */
Signature · 1 line
2.11.0
PATCH v4 11/21 mm/hugetlb: Allocate the vmemmap pages associated with each hugetlb page
In reply to Muchun SongWhen we free a hugetlb page to the buddy, we should allocate the vmemmap pages associated with it. We can do that in the __free_hugepage().
--- mm/hugetlb.c | 2 ++ mm/hugetlb_vmemmap.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 5 +++ 3 files changed, 107 insertions(+)
mm/hugetlb.c +2 -0
@@ -1382,6 +1382,8 @@ static void __free_hugepage(struct hstate *h, struct page *page) {int i; +alloc_huge_page_vmemmap (h, page); +for (i =0 ; i <pages_per_huge_page (h); i++) { page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | 1 << PG_dirty |
mm/hugetlb_vmemmap.c +100 -0
112 lines not on this page · raw message
mm/hugetlb_vmemmap.h +5 -0
@@ -15,6 +15,7 @@void __init hugetlb_vmemmap_init (struct hstate *h);int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page);void vmemmap_pgtable_free (struct page *page); +void alloc_huge_page_vmemmap (struct hstate *h,struct page *head);void free_huge_page_vmemmap (struct hstate *h,struct page *head);static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h)
@@ -35,6 +36,10 @@ static inline void vmemmap_pgtable_free(struct page *page) { } +static inline void alloc_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +} +static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) { }
Signature · 1 line
2.11.0
PATCH v4 12/21 mm/hugetlb: Introduce remap_huge_page_pmd_vmemmap helper
In reply to Muchun SongThe __free_huge_page_pmd_vmemmap and __remap_huge_page_pmd_vmemmap are almost the same code. So introduce remap_free_huge_page_pmd_vmemmap helper to simplify the code.
--- mm/hugetlb_vmemmap.c | 108 +++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 63 deletions(-)
mm/hugetlb_vmemmap.c +45 -63
@@ -221,6 +221,47 @@ static inline int freed_vmemmap_hpage_dec(struct page *page)return atomic_dec_return_relaxed (&page->_mapcount) +1 ; } +static inline void free_vmemmap_page_list (struct list_head *list) +{ +struct page *page, *next; + +list_for_each_entry_safe (page, next, list, lru) { +list_del (&page->lru); +free_vmemmap_page (page); + } +} + +typedef void (*remap_pte_fn )(struct page *reuse,pte_t *ptep, +unsigned long start,unsigned long end, +struct list_head *pages); + +static void remap_huge_page_pmd_vmemmap (struct hstate *h,pmd_t *pmd, +unsigned long addr, +struct list_head *pages, +remap_pte_fn remap_fn) +{ +unsigned long next; +unsigned long start = addr +RESERVE_VMEMMAP_SIZE ; +unsigned long end = addr +vmemmap_pages_size_per_hpage (h); +struct page *reuse =NULL ; + +flush_cache_vunmap (start, end); + + addr = start; +do { +pte_t *ptep; + + ptep =pte_offset_kernel (pmd, addr); +if (!reuse) + reuse =pte_page (ptep[TAIL_PAGE_REUSE ]); + + next =vmemmap_hpage_addr_end (addr, end); +remap_fn (reuse, ptep, addr, next, pages); + }while (pmd++, addr = next, addr != end); + +flush_tlb_kernel_range (start, end); +} + static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, unsigned long end,
@@ -255,31 +296,6 @@ static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, } } -static void __remap_huge_page_pmd_vmemmap (struct hstate *h,pmd_t *pmd, -unsigned long addr, -struct list_head *remap_pages) -{ -unsigned long next; -unsigned long start = addr +RESERVE_VMEMMAP_SIZE ; -unsigned long end = addr +vmemmap_pages_size_per_hpage (h); -struct page *reuse =NULL ; - - addr = start; -do { -pte_t *ptep; - - ptep =pte_offset_kernel (pmd, addr); -if (!reuse) - reuse =pte_page (ptep[TAIL_PAGE_REUSE ]); - - next =vmemmap_hpage_addr_end (addr, end); -__remap_huge_page_pte_vmemmap (reuse, ptep, addr, next, - remap_pages); - }while (pmd++, addr = next, addr != end); - -flush_tlb_kernel_range (start, end); -} -static inline void alloc_vmemmap_pages (struct hstate *h,struct list_head *list) {int i;
@@ -308,8 +324,8 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head)BUG_ON (!pmd); ptl =vmemmap_pmd_lock (pmd); -__remap_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, - &remap_pages); +remap_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, &remap_pages, + __remap_huge_page_pte_vmemmap);if (!freed_vmemmap_hpage_dec (pmd_page (*pmd))) {/* * Todo:
@@ -319,16 +335,6 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head)spin_unlock (ptl); } -static inline void free_vmemmap_page_list (struct list_head *list) -{ -struct page *page, *next; - -list_for_each_entry_safe (page, next, list, lru) { -list_del (&page->lru); -free_vmemmap_page (page); - } -} - static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, unsigned long end,
@@ -351,31 +357,6 @@ static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, } } -static void __free_huge_page_pmd_vmemmap (struct hstate *h,pmd_t *pmd, -unsigned long addr, -struct list_head *free_pages) -{ -unsigned long next; -unsigned long start = addr +RESERVE_VMEMMAP_SIZE ; -unsigned long end = addr +vmemmap_pages_size_per_hpage (h); -struct page *reuse =NULL ; - - addr = start; -do { -pte_t *ptep; - - ptep =pte_offset_kernel (pmd, addr); -if (!reuse) - reuse =pte_page (ptep[TAIL_PAGE_REUSE ]); - - next =vmemmap_hpage_addr_end (addr, end); -__free_huge_page_pte_vmemmap (reuse, ptep, addr, next, - free_pages); - }while (pmd++, addr = next, addr != end); - -flush_tlb_kernel_range (start, end); -} -static void split_vmemmap_pmd (pmd_t *pmd,pte_t *pte_p,unsigned long addr) {int i;
@@ -434,7 +415,8 @@ void free_huge_page_vmemmap(struct hstate *h, struct page *head)if (vmemmap_pmd_huge (pmd))split_vmemmap_huge_page (head, pmd); -__free_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, &free_pages); +remap_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, &free_pages, + __free_huge_page_pte_vmemmap);freed_vmemmap_hpage_inc (pmd_page (*pmd));spin_unlock (ptl);
Signature · 1 line
2.11.0
PATCH v4 13/21 mm/hugetlb: Use PG_slab to indicate split pmd
In reply to Muchun SongWhen we allocate hugetlb page from buddy, we may need split huge pmd to pte. When we free the hugetlb page, we can merge pte to pmd. So we need to distinguish whether the previous pmd has been split. The page table is not allocated from slab. So we can reuse the PG_slab to indicate that the pmd has been split.
--- mm/hugetlb_vmemmap.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
mm/hugetlb_vmemmap.c +24 -2
@@ -262,6 +262,25 @@ static void remap_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd,flush_tlb_kernel_range (start, end); } +static inline bool pmd_split (pmd_t *pmd) +{ +return PageSlab (pmd_page (*pmd)); +} + +static inline void set_pmd_split (pmd_t *pmd) +{ +/* +* We should not use slab for page table allocation. So we can set +* PG_slab to indicate that the pmd has been split. +*/ +__SetPageSlab (pmd_page (*pmd)); +} + +static inline void clear_pmd_split (pmd_t *pmd) +{ +__ClearPageSlab (pmd_page (*pmd)); +} + static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, unsigned long end,
@@ -326,11 +345,12 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) ptl =vmemmap_pmd_lock (pmd);remap_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, &remap_pages, __remap_huge_page_pte_vmemmap); -if (!freed_vmemmap_hpage_dec (pmd_page (*pmd))) { +if (!freed_vmemmap_hpage_dec (pmd_page (*pmd)) &&pmd_split (pmd)) {/* * Todo: * Merge pte to huge pmd if it has ever been split. */ +clear_pmd_split (pmd); }spin_unlock (ptl); }
@@ -412,8 +432,10 @@ void free_huge_page_vmemmap(struct hstate *h, struct page *head)BUG_ON (!pmd); ptl =vmemmap_pmd_lock (pmd); -if (vmemmap_pmd_huge (pmd)) +if (vmemmap_pmd_huge (pmd)) {split_vmemmap_huge_page (head, pmd); +set_pmd_split (pmd); + }remap_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, &free_pages, __free_huge_page_pte_vmemmap);
Signature · 1 line
2.11.0
PATCH v4 14/21 mm/hugetlb: Support freeing vmemmap pages of gigantic page
In reply to Muchun SongThe gigantic page is allocated by bootmem, if we want to free the unused vmemmap pages. We also should allocate the page table. So we also allocate page tables from bootmem.
--- include/linux/hugetlb.h | 3 +++ mm/hugetlb.c | 5 +++++ mm/hugetlb_vmemmap.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 13 ++++++++++++ 4 files changed, 76 insertions(+)
include/linux/hugetlb.h +3 -0
@@ -506,6 +506,9 @@ struct hstate {struct huge_bootmem_page {struct list_head list;struct hstate *hstate; +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +pte_t *vmemmap_pte; +#endif }; struct page *alloc_huge_page(struct vm_area_struct *vma,
mm/hugetlb.c +5 -0
@@ -2607,6 +2607,7 @@ static void __init gather_bootmem_prealloc(void)WARN_ON (page_count (page) !=1 );prep_compound_huge_page (page, h->order);WARN_ON (PageReserved (page)); +gather_vmemmap_pgtable_init (m, page);prep_new_huge_page (h, page,page_to_nid (page));put_page (page);/* free it into the hugepage allocator */
@@ -2659,6 +2660,10 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)break ;cond_resched (); } + +if (hstate_is_gigantic (h)) + i -=gather_vmemmap_pgtable_prealloc (); +if (i < h->max_huge_pages) {char buf[32 ];
mm/hugetlb_vmemmap.c +55 -0
67 lines not on this page · raw message
mm/hugetlb_vmemmap.h +13 -0
@@ -15,6 +15,9 @@void __init hugetlb_vmemmap_init (struct hstate *h);int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page);void vmemmap_pgtable_free (struct page *page); +unsigned long __init gather_vmemmap_pgtable_prealloc (void ); +void __init gather_vmemmap_pgtable_init (struct huge_bootmem_page *m, +struct page *page);void alloc_huge_page_vmemmap (struct hstate *h,struct page *head);void free_huge_page_vmemmap (struct hstate *h,struct page *head);
@@ -36,6 +39,16 @@ static inline void vmemmap_pgtable_free(struct page *page) { } +static inline unsigned long gather_vmemmap_pgtable_prealloc (void ) +{ +return 0 ; +} + +static inline void gather_vmemmap_pgtable_init (struct huge_bootmem_page *m, +struct page *page) +{ +} +static inline void alloc_huge_page_vmemmap (struct hstate *h,struct page *head) { }
Signature · 1 line
2.11.0
PATCH v4 15/21 mm/hugetlb: Set the PageHWPoison to the raw error page
In reply to Muchun SongBecause we reuse the first tail page, if we set PageHWPosion on a tail page. It indicates that we may set PageHWPoison on a series of pages. So we can use the head[4].mapping to record the real error page index and set the raw error page PageHWPoison later.
--- mm/hugetlb.c | 11 +++-------- mm/hugetlb_vmemmap.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-)
mm/hugetlb.c +3 -8
@@ -1383,6 +1383,7 @@ static void __free_hugepage(struct hstate *h, struct page *page)int i;alloc_huge_page_vmemmap (h, page); +subpage_hwpoison_deliver (page);for (i =0 ; i <pages_per_huge_page (h); i++) { page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
@@ -1944,14 +1945,8 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; -/* -* Move PageHWPoison flag from head page to the raw error page, -* which makes any subpages rather than the error page reusable. -*/ -if (PageHWPoison (head) && page != head) { -SetPageHWPoison (page); -ClearPageHWPoison (head); - } + +set_subpage_hwpoison (head, page);list_del (&head->lru); h->free_huge_pages--; h->free_huge_pages_node[nid]--;
mm/hugetlb_vmemmap.h +39 -0
@@ -21,6 +21,29 @@ void __init gather_vmemmap_pgtable_init(struct huge_bootmem_page *m,void alloc_huge_page_vmemmap (struct hstate *h,struct page *head);void free_huge_page_vmemmap (struct hstate *h,struct page *head); +static inline void subpage_hwpoison_deliver (struct page *head) +{ +struct page *page = head; + +if (PageHWPoison (head)) + page = head +page_private (head +4 ); + +/* +* Move PageHWPoison flag from head page to the raw error page, +* which makes any subpages rather than the error page reusable. +*/ +if (page != head) { +SetPageHWPoison (page); +ClearPageHWPoison (head); + } +} + +static inline void set_subpage_hwpoison (struct page *head,struct page *page) +{ +if (PageHWPoison (head)) +set_page_private (head +4 , page - head); +} +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) {return h->nr_free_vmemmap_pages;
@@ -57,6 +80,22 @@ static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head) { } +static inline void subpage_hwpoison_deliver (struct page *head) +{ +} + +static inline void set_subpage_hwpoison (struct page *head,struct page *page) +{ +/* +* Move PageHWPoison flag from head page to the raw error page, +* which makes any subpages rather than the error page reusable. +*/ +if (PageHWPoison (head) && page != head) { +SetPageHWPoison (page); +ClearPageHWPoison (head); + } +} +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) {return 0 ;
Signature · 1 line
2.11.0
PATCH v4 16/21 mm/hugetlb: Flush work when dissolving hugetlb page
In reply to Muchun SongWe should flush work when dissolving a hugetlb page to make sure that the hugetlb page is freed to the buddy.
--- mm/hugetlb.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
mm/hugetlb.c +17 -1
@@ -1328,6 +1328,12 @@ static void update_hpage_vmemmap_workfn(struct work_struct *work) } static DECLARE_WORK(hpage_update_work, update_hpage_vmemmap_workfn); +static inline void flush_hpage_update_work (struct hstate *h) +{ +if (free_vmemmap_pages_per_hpage (h)) +flush_work (&hpage_update_work); +} +static inline void __update_and_free_page (struct hstate *h,struct page *page) {/* No need to allocate vmemmap pages */
@@ -1928,6 +1934,7 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,int dissolve_free_huge_page (struct page *page) {int rc = -EBUSY ; +struct hstate *h =NULL ;/* Not to disrupt normal path by vainly holding hugetlb_lock */ if (!PageHuge (page))
@@ -1941,8 +1948,9 @@ int dissolve_free_huge_page(struct page *page)if (!page_count (page)) {struct page *head =compound_head (page); -struct hstate *h =page_hstate (head);int nid =page_to_nid (head); + + h =page_hstate (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out;
@@ -1956,6 +1964,14 @@ int dissolve_free_huge_page(struct page *page) } out:spin_unlock (&hugetlb_lock); + +/* +* We should flush work before return to make sure that +* the HugeTLB page is freed to the buddy. +*/ +if (!rc && h) +flush_hpage_update_work (h); +return rc; }
Signature · 1 line
2.11.0
PATCH v4 17/21 mm/hugetlb: Add a kernel parameter hugetlb_free_vmemmap
In reply to Muchun SongAdd a kernel parameter hugetlb_free_vmemmap to disable the feature of freeing unused vmemmap pages associated with each hugetlb page on boot.
--- Documentation/admin-guide/kernel-parameters.txt | 9 +++++++++ Documentation/admin-guide/mm/hugetlbpage.rst | 3 +++ mm/hugetlb_vmemmap.c | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +9 -0
@@ -1551,6 +1551,15 @@ Documentation/admin-guide/mm/hugetlbpage.rst. Format: size[KMG] + hugetlb_free_vmemmap= + [KNL] When CONFIG_HUGETLB_PAGE_FREE_VMEMMAP is set, + this controls freeing unused vmemmap pages associated + with each HugeTLB page. + Format: { on (default) | off } + + on: enable the feature + off: disable the feature + hung_task_panic= [KNL] Should the hung task detector generate panics. Format: 0 | 1
Documentation/admin-guide/mm/hugetlbpage.rst +3 -0
@@ -145,6 +145,9 @@ default_hugepagesz will all result in 256 2M huge pages being allocated. Valid default huge page size is architecture dependent. +hugetlb_free_vmemmap + When CONFIG_HUGETLB_PAGE_FREE_VMEMMAP is set, this disables freeing + unused vmemmap pages associated each HugeTLB page. When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages`` indicates the current number of pre-allocated huge pages of the default size.
mm/hugetlb_vmemmap.c +22 -0
@@ -118,6 +118,22 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) }#endif +static bool hugetlb_free_vmemmap_disabled__initdata ; + +static int __init early_hugetlb_free_vmemmap_param (char *buf) +{ +if (!buf) +return -EINVAL ; + +if (!strcmp (buf,"off" )) + hugetlb_free_vmemmap_disabled = true; +else if (strcmp (buf,"on" )) +return -EINVAL ; + +return 0 ; +} +early_param ("hugetlb_free_vmemmap" , early_hugetlb_free_vmemmap_param); +static inline unsigned int vmemmap_pages_per_hpage (struct hstate *h) {return free_vmemmap_pages_per_hpage (h) +RESERVE_VMEMMAP_NR ;
@@ -505,6 +521,12 @@ void __init hugetlb_vmemmap_init(struct hstate *h)unsigned int order =huge_page_order (h);unsigned int vmemmap_pages; +if (hugetlb_free_vmemmap_disabled) { + h->nr_free_vmemmap_pages =0 ; +pr_info ("disable free vmemmap pages for %s\n" , h->name); +return ; + } + vmemmap_pages = ((1 << order) *sizeof (struct page )) >>PAGE_SHIFT ;/* * The head page and the first tail page are not to be freed to buddy
Signature · 1 line
2.11.0
PATCH v4 18/21 mm/hugetlb: Merge pte to huge pmd only for gigantic page
In reply to Muchun SongMerge pte to huge pmd if it has ever been split. Now only support gigantic page which's vmemmap pages size is an integer multiple of PMD_SIZE. This is the simplest case to handle.
--- arch/x86/include/asm/hugetlb.h | 8 +++ mm/hugetlb_vmemmap.c | 118 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-)
arch/x86/include/asm/hugetlb.h +8 -0
@@ -12,6 +12,14 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) {return pmd_large (*pmd); } + +#define vmemmap_pmd_mkhuge vmemmap_pmd_mkhuge +static inline pmd_t vmemmap_pmd_mkhuge (struct page *page) +{ +pte_t entry =pfn_pte (page_to_pfn (page),PAGE_KERNEL_LARGE ); + +return __pmd (pte_val (entry)); +}#endif #define hugepages_supported () boot_cpu_has(X86_FEATURE_PSE)
mm/hugetlb_vmemmap.c +116 -2
138 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v4 19/21 mm/hugetlb: Gather discrete indexes of tail page
In reply to Muchun SongFor hugetlb page, there are more metadata to save in the struct page. But the head struct page cannot meet our needs, so we have to abuse other tail struct page to store the metadata. In order to avoid conflicts caused by subsequent use of more tail struct pages, we can gather these discrete indexes of tail struct page In this case, it will be easier to add a new tail page index later.
--- include/linux/hugetlb.h | 13 +++++++++++++ include/linux/hugetlb_cgroup.h | 15 +++++++++------ mm/hugetlb.c | 12 ++++++------ mm/hugetlb_vmemmap.h | 4 ++-- 4 files changed, 30 insertions(+), 14 deletions(-)
include/linux/hugetlb.h +13 -0
@@ -28,6 +28,19 @@ typedef struct { unsigned long pd; } hugepd_t;#include <linux/shm.h> #include <asm/tlbflush.h> +enum { +SUBPAGE_INDEX_ACTIVE =1 ,/* reuse page flags of PG_private */ +SUBPAGE_INDEX_TEMPORARY ,/* reuse page->mapping */ +#ifdef CONFIG_CGROUP_HUGETLB +SUBPAGE_INDEX_CGROUP =SUBPAGE_INDEX_TEMPORARY ,/* reuse page->private */ +SUBPAGE_INDEX_CGROUP_RSVD ,/* reuse page->private */ +#endif +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +SUBPAGE_INDEX_HWPOISON ,/* reuse page->private */ +#endif +NR_USED_SUBPAGE , +}; +struct hugepage_subpool {spinlock_t lock;long count;
include/linux/hugetlb_cgroup.h +9 -6
@@ -24,8 +24,9 @@ struct file_region;/* * Minimum page order trackable by hugetlb cgroup. * At least 4 pages are necessary for all the tracking information. -* The second tail page (hpage[2]) is the fault usage cgroup. -* The third tail page (hpage[3]) is the reservation usage cgroup. +* The second tail page (hpage[SUBPAGE_INDEX_CGROUP]) is the fault +* usage cgroup. The third tail page (hpage[SUBPAGE_INDEX_CGROUP_RSVD]) +* is the reservation usage cgroup. */ #define HUGETLB_CGROUP_MIN_ORDER 2
@@ -66,9 +67,9 @@ __hugetlb_cgroup_from_page(struct page *page, bool rsvd)if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return NULL ;if (rsvd) -return (struct hugetlb_cgroup *)page[3 ].private; +return (void *)page_private (page +SUBPAGE_INDEX_CGROUP_RSVD );else -return (struct hugetlb_cgroup *)page[2 ].private; +return (void *)page_private (page +SUBPAGE_INDEX_CGROUP ); }static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page)
@@ -90,9 +91,11 @@ static inline int __set_hugetlb_cgroup(struct page *page,if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return -1 ;if (rsvd) - page[3 ].private = (unsigned long )h_cg; +set_page_private (page +SUBPAGE_INDEX_CGROUP_RSVD , + (unsigned long )h_cg);else - page[2 ].private = (unsigned long )h_cg; +set_page_private (page +SUBPAGE_INDEX_CGROUP , + (unsigned long )h_cg);return 0 ; }
mm/hugetlb.c +6 -6
@@ -1429,20 +1429,20 @@ struct hstate *size_to_hstate(unsigned long size)bool page_huge_active (struct page *page) {VM_BUG_ON_PAGE (!PageHuge (page), page); -return PageHead (page) &&PagePrivate (&page[1 ]); +return PageHead (page) &&PagePrivate (&page[SUBPAGE_INDEX_ACTIVE ]); }/* never called for tail page */ static void set_page_huge_active (struct page *page) {VM_BUG_ON_PAGE (!PageHeadHuge (page), page); -SetPagePrivate (&page[1 ]); +SetPagePrivate (&page[SUBPAGE_INDEX_ACTIVE ]); }static void clear_page_huge_active (struct page *page) {VM_BUG_ON_PAGE (!PageHeadHuge (page), page); -ClearPagePrivate (&page[1 ]); +ClearPagePrivate (&page[SUBPAGE_INDEX_ACTIVE ]); }/*
@@ -1454,17 +1454,17 @@ static inline bool PageHugeTemporary(struct page *page)if (!PageHuge (page))return false; -return (unsigned long )page[2 ].mapping ==-1U ; +return (unsigned long )page[SUBPAGE_INDEX_TEMPORARY ].mapping ==-1U ; }static inline void SetPageHugeTemporary (struct page *page) { - page[2 ].mapping = (void *)-1U ; + page[SUBPAGE_INDEX_TEMPORARY ].mapping = (void *)-1U ; }static inline void ClearPageHugeTemporary (struct page *page) { - page[2 ].mapping =NULL ; + page[SUBPAGE_INDEX_TEMPORARY ].mapping =NULL ; }static void __free_huge_page (struct page *page)
mm/hugetlb_vmemmap.h +2 -2
@@ -26,7 +26,7 @@ static inline void subpage_hwpoison_deliver(struct page *head)struct page *page = head;if (PageHWPoison (head)) - page = head +page_private (head +4 ); + page = head +page_private (head +SUBPAGE_INDEX_HWPOISON );/* * Move PageHWPoison flag from head page to the raw error page,
@@ -41,7 +41,7 @@ static inline void subpage_hwpoison_deliver(struct page *head)static inline void set_subpage_hwpoison (struct page *head,struct page *page) {if (PageHWPoison (head)) -set_page_private (head +4 , page - head); +set_page_private (head +SUBPAGE_INDEX_HWPOISON , page - head); }static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h)
Signature · 1 line
2.11.0
PATCH v4 20/21 mm/hugetlb: Add BUILD_BUG_ON to catch invalid usage of tail struct page
In reply to Muchun SongThere are only RESERVE_VMEMMAP_SIZE / sizeof(struct page) struct pages can be used when CONFIG_HUGETLB_PAGE_FREE_VMEMMAP, so add a BUILD_BUG_ON to catch this invalid usage of tail struct page.
--- mm/hugetlb_vmemmap.c | 3 +++ 1 file changed, 3 insertions(+)
mm/hugetlb_vmemmap.c +3 -0
@@ -717,6 +717,9 @@ static int __init vmemmap_ptlock_init(void) {int nid; +BUILD_BUG_ON (NR_USED_SUBPAGE >= +RESERVE_VMEMMAP_SIZE /sizeof (struct page )); +if (!hugepages_supported ())return 0 ;
Signature · 1 line
2.11.0
PATCH v4 21/21 mm/hugetlb: Disable freeing vmemmap if struct page size is not power of two
In reply to Muchun SongWe only can free the unused vmemmap to the buddy system when the size of struct page is a power of two.
--- mm/hugetlb_vmemmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/hugetlb_vmemmap.c +2 -1
@@ -635,7 +635,8 @@ void __init hugetlb_vmemmap_init(struct hstate *h)unsigned int order =huge_page_order (h);unsigned int vmemmap_pages; -if (hugetlb_free_vmemmap_disabled) { +if (hugetlb_free_vmemmap_disabled || + !is_power_of_2 (sizeof (struct page ))) { h->nr_free_vmemmap_pages =0 ;pr_info ("disable free vmemmap pages for %s\n" , h->name);return ;
Signature · 1 line
2.11.0
Quoted 106 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Muchun Song
Sent: Saturday, November 14, 2020 12:00 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Muchun Song <[email protected]>
Subject: [PATCH v4 00/21] Free some vmemmap pages of hugetlb pageHi all,
This patch series will free some vmemmap pages(struct page structures) associated with each hugetlbpage when preallocated to save memory.
Nowadays we track the status of physical page frames using struct page structures arranged in one or more arrays. And here exists one-to-one mapping between the physical page frame and the corresponding struct page structure.
The HugeTLB support is built on top of multiple page size support that is provided by most modern architectures. For example, x86 CPUs normally support 4K and 2M (1G if architecturally supported) page sizes. Every HugeTLB has more than one struct page structure. The 2M HugeTLB has 512 struct page structure and 1G HugeTLB has 4096 struct page structures. But in the core of HugeTLB only uses the first 4 (Use of first 4 struct page structures comes from
HUGETLB_CGROUP_MIN_ORDER.) struct page structures to
store metadata associated with each HugeTLB. The rest of the struct page structures are usually read thecompound_headfield which are all the same value. If we can free some struct page memory to buddy system so that we can save a lot of memory.When the system boot up, every 2M HugeTLB has 512 struct page structures which size is 8
pages(sizeof(struct page) * 512 / PAGE_SIZE).hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | | 1 | -------------> | 1 | | | | 2 | -------------> | 2 | | | | 3 | -------------> | 3 | | | | 4 | -------------> | 4 | | 2M | | 5 | -------------> | 5 | | | | 6 | -------------> | 6 | | | | 7 | -------------> | 7 | | | +-----------+ +-----------+ | | | | +-----------+When a hugetlbpage is preallocated, we can change the mapping from above to
bellow.hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | | 1 | -------------> | 1 | | | | 2 | -------------> +-----------+ | | | 3 | -----------------^ ^ ^ ^ ^ | | | 4 | -------------------+ | | | | 2M | | 5 | ---------------------+ | | | | | 6 | -----------------------+ | | | | 7 | -------------------------+ | | +-----------+ | | | | +-----------+For tail pages, the value of
compound_headis the same. So we can reuse first page of tail page structs. We map the virtual addresses of the remaining 6 pages of tail page structs to the first tail page struct, and then free these 6 pages. Therefore, we need to reserve at least 2 pages as vmemmap areas.When a hugetlbpage is freed to the buddy system, we should allocate six pages for vmemmap pages and restore the previous mapping relationship.
Quoted 7 lines
If we uses the 1G hugetlbpage, we can save 4088 pages(There are 4096 pages for
struct page structures, we reserve 2 pages for vmemmap and 8 pages for page tables. So we can save 4088 pages). This is a very substantial gain. On our server, run some SPDK/QEMU applications which will use 1024GB hugetlbpage. With this feature enabled, we can save ~16GB(1G hugepage)/~11GB(2MB hugepage)
Hi Muchun,
Do we really save 11GB for 2MB hugepage?
How much do we save if we only get one 2MB hugetlb from one 128MB mem_section? It seems we need to get at least one page for the PTEs since we are splitting PMD of vmemmap into PTE?
Quoted 54 lines
memory.
Because there are vmemmap page tables reconstruction on the freeing/allocating
path, it increases some overhead. Here are some overhead analysis.1) Allocating 10240 2MB hugetlb pages.
a) With this patch series applied: # time echo 10240 > /proc/sys/vm/nr_hugepages real 0m0.166s user 0m0.000s sys 0m0.166s # bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; }kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency =
hist(nsecs - @start[tid]);delete(@start[tid]); }'
Attaching 2 probes...@latency: [8K, 16K) 8360|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| [16K, 32K) 1868 |@@@@@@@@@@@|
[32K, 64K) 10 |
|
[64K, 128K) 2 |
|b) Without this patch series: # time echo 10240 > /proc/sys/vm/nr_hugepages real 0m0.066s user 0m0.000s sys 0m0.066s # bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; }kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency =
hist(nsecs - @start[tid]);delete(@start[tid]); }'
Attaching 2 probes...@latency: [4K, 8K) 10176|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| [8K, 16K) 62 ||
[16K, 32K) 2 |
|Summarize: this feature is about ~2x slower than before.
Quoted 9 lines
2) Freeing 10240 @MB hugetlb pages.
a) With this patch series applied: # time echo 0 > /proc/sys/vm/nr_hugepages real 0m0.004s user 0m0.000s sys 0m0.002s
Something is wrong here, it is faster than the case without this patchset:
0.004s vs. 0m0.077s
Quoted 41 lines
# bpftrace -e 'kprobe:__free_hugepage { @start[tid] = nsecs; } kretprobe:__free_hugepage /@start[tid]/ { @latency = hist(nsecs - @start[tid]); delete(@start[tid]); }' Attaching 2 probes...@latency: [16K, 32K) 10240|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| b) Without this patch series: # time echo 0 > /proc/sys/vm/nr_hugepages real 0m0.077s user 0m0.001s sys 0m0.075s # bpftrace -e 'kprobe:__free_hugepage { @start[tid] = nsecs; }kretprobe:__free_hugepage /@start[tid]/ { @latency =
hist(nsecs - @start[tid]);delete(@start[tid]); }'
Attaching 2 probes...@latency: [4K, 8K) 9950|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| [8K, 16K) 287 |@|
[16K, 32K) 3 |
|Summarize: The overhead of
__free_hugepageis about ~2-4x slower than
before.
But according to the allocation test above, I think that here is also ~2x slower than before.But why the 'real' time of patched is smaller than before?
Because
In this patch series, the freeing hugetlb is asynchronous(through kwoker).
Quoted 7 lines
Although the overhead has increased, the overhead is not significant. Like MIke said, "However, remember that the majority of use cases create hugetlb pages at
or shortly after boot time and add them to the pool. So, additional overhead is at pool creation time. There is no change to 'normal run time' operations of getting a page from or returning a page to the pool (think page fault/unmap)".
It seems it is true. At runtime, people normally don't change hugetlb.
Quoted 66 lines
changelog in v4:
1. Move all the vmemmap functions tohugetlb_vmemmap.c.
2. Make theCONFIG_HUGETLB_PAGE_FREE_VMEMMAPdefault to y, if we
want to
disable this feature, we should disable it by a boot/kernel command line.
3. Remove vmemmap_pgtable_{init, deposit, withdraw}() helper functions.
4. Initialize page table lock for vmemmap throughcore_initcallmechanism.Thanks for Mike and Oscar's suggestions.
changelog in v3:
1. Rename some helps function name. Thanks Mike.
2. Rework some code. Thanks Mike and Oscar.
3. Remap the tail vmemmap page withPAGE_KERNEL_ROinstead ofPAGE_KERNEL. Thanks Matthew.
4. Add some overhead analysis in the cover letter.
5. Use vmemap pmd table lock instead of a hugetlb specific global lock.changelog in v2:
1. Fix do not calldissolve_compound_pageinalloc_huge_page_vmemmap().
2. Fix some typo and code style problems.
3. Remove unusedhandle_vmemmap_fault().
4. Merge some commits to one commit suggested by Mike.Muchun Song (21):
mm/memory_hotplug: Move bootmem info registration API to
bootmem_info.c
mm/memory_hotplug: Move {get,put}_page_bootmem() tobootmem_info.cmm/hugetlb: Introduce a new configHUGETLB_PAGE_FREE_VMEMMAPmm/hugetlb: Introducenr_free_vmemmap_pagesin the struct hstate mm/hugetlb: Introduce pgtable allocation/freeing helpersmm/bootmem_info: Introduce {free,prepare}_vmemmap_page()mm/bootmem_info: Combine bootmem info and type intopage->freelistmm/hugetlb: Initialize page table lock for vmemmap mm/hugetlb: Free the vmemmap pages associated with each hugetlb page mm/hugetlb: Defer freeing of hugetlb pages
mm/hugetlb: Allocate the vmemmap pages associated with each hugetlb
page
mm/hugetlb: Introduceremap_huge_page_pmd_vmemmaphelper mm/hugetlb: UsePG_slabto indicate split pmd
mm/hugetlb: Support freeing vmemmap pages of gigantic page mm/hugetlb: Set the PageHWPoison to the raw error page mm/hugetlb: Flush work when dissolving hugetlb page mm/hugetlb: Add a kernel parameterhugetlb_free_vmemmapmm/hugetlb: Merge pte to huge pmd only for gigantic page mm/hugetlb: Gather discrete indexes of tail page mm/hugetlb: AddBUILD_BUG_ONto catch invalid usage of tail struct
page
mm/hugetlb: Disable freeing vmemmap if struct page size is not power
of twoDocumentation/admin-guide/kernel-parameters.txt | 9 + Documentation/admin-guide/mm/hugetlbpage.rst | 3 + arch/x86/include/asm/hugetlb.h | 17 + arch/x86/include/asm/pgtable_64_types.h | 8 + arch/x86/mm/init_64.c | 7 +- fs/Kconfig | 14 + include/linux/bootmem_info.h | 78 +++ include/linux/hugetlb.h | 19 + include/linux/hugetlb_cgroup.h | 15 +- include/linux/memory_hotplug.h | 27 - mm/Makefile | 2 + mm/bootmem_info.c | 124 ++++ mm/hugetlb.c | 163 +++++- mm/hugetlb_vmemmap.c | 732++++++++++++++++++++++++
Quoted 9 lines
mm/hugetlb_vmemmap.h | 104 ++++ mm/memory_hotplug.c | 116 ---- mm/sparse.c | 5 +- 17 files changed, 1263 insertions(+), 180 deletions(-) create mode 100644 include/linux/bootmem_info.h create mode 100644 mm/bootmem_info.c create mode 100644 mm/hugetlb_vmemmap.c create mode 100644 mm/hugetlb_vmemmap.h
Thanks
Barry
Quoted 115 lines · Song Bao Hua (Barry Song)
Quoted 107 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Muchun Song
Sent: Saturday, November 14, 2020 12:00 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Muchun Song <[email protected]>
Subject: [PATCH v4 00/21] Free some vmemmap pages of hugetlb pageHi all,
This patch series will free some vmemmap pages(struct page structures) associated with each hugetlbpage when preallocated to save memory.
Nowadays we track the status of physical page frames using struct page structures arranged in one or more arrays. And here exists one-to-one mapping between the physical page frame and the corresponding struct page structure.
The HugeTLB support is built on top of multiple page size support that is provided by most modern architectures. For example, x86 CPUs normally support 4K and 2M (1G if architecturally supported) page sizes. Every HugeTLB has more than one struct page structure. The 2M HugeTLB has 512 struct page structure and 1G HugeTLB has 4096 struct page structures. But in the core of HugeTLB only uses the first 4 (Use of first 4 struct page structures comes from
HUGETLB_CGROUP_MIN_ORDER.) struct page structures to
store metadata associated with each HugeTLB. The rest of the struct page structures are usually read thecompound_headfield which are all the same value. If we can free some struct page memory to buddy system so that we can save a lot of memory.When the system boot up, every 2M HugeTLB has 512 struct page structures which size is 8
pages(sizeof(struct page) * 512 / PAGE_SIZE).hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | | 1 | -------------> | 1 | | | | 2 | -------------> | 2 | | | | 3 | -------------> | 3 | | | | 4 | -------------> | 4 | | 2M | | 5 | -------------> | 5 | | | | 6 | -------------> | 6 | | | | 7 | -------------> | 7 | | | +-----------+ +-----------+ | | | | +-----------+When a hugetlbpage is preallocated, we can change the mapping from above to
bellow.hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> | 0 | | | | 1 | -------------> | 1 | | | | 2 | -------------> +-----------+ | | | 3 | -----------------^ ^ ^ ^ ^ | | | 4 | -------------------+ | | | | 2M | | 5 | ---------------------+ | | | | | 6 | -----------------------+ | | | | 7 | -------------------------+ | | +-----------+ | | | | +-----------+For tail pages, the value of
compound_headis the same. So we can reuse first page of tail page structs. We map the virtual addresses of the remaining 6 pages of tail page structs to the first tail page struct, and then free these 6 pages. Therefore, we need to reserve at least 2 pages as vmemmap areas.When a hugetlbpage is freed to the buddy system, we should allocate six pages for vmemmap pages and restore the previous mapping relationship.
Quoted 7 lines
If we uses the 1G hugetlbpage, we can save 4088 pages(There are 4096 pages for
struct page structures, we reserve 2 pages for vmemmap and 8 pages for page tables. So we can save 4088 pages). This is a very substantial gain. On our server, run some SPDK/QEMU applications which will use 1024GB hugetlbpage. With this feature enabled, we can save ~16GB(1G hugepage)/~11GB(2MB hugepage)
Quoted 6 lines
Hi Muchun,
Do we really save 11GB for 2MB hugepage?
How much do we save if we only get one 2MB hugetlb from one 128MBmem_section? It seems we need to get at least one page for the PTEs since we are splitting PMD of vmemmap into PTE?
There are 524288(1024GB/2MB) 2MB HugeTLB pages. We can save 6 pages for each 2MB HugeTLB page. So we can save 3145728 pages. But we need to split PMD page table for every one 128MB mem_section and every section need one page as PTE page
table. So we need 8192(1024GB/128MB) pages as PTE page tables. Finally, we can save
3137536(3145728-8192) pages which is 11.97GB.
Thanks Barry.
Quoted 60 lines
Quoted 60 lines
memory.
Because there are vmemmap page tables reconstruction on the freeing/allocating
path, it increases some overhead. Here are some overhead analysis.1) Allocating 10240 2MB hugetlb pages.
a) With this patch series applied: # time echo 10240 > /proc/sys/vm/nr_hugepages real 0m0.166s user 0m0.000s sys 0m0.166s # bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; }kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency =
hist(nsecs - @start[tid]);delete(@start[tid]); }'
Attaching 2 probes...@latency: [8K, 16K) 8360|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| [16K, 32K) 1868 |@@@@@@@@@@@|
[32K, 64K) 10 |
|
[64K, 128K) 2 |
|b) Without this patch series: # time echo 10240 > /proc/sys/vm/nr_hugepages real 0m0.066s user 0m0.000s sys 0m0.066s # bpftrace -e 'kprobe:alloc_fresh_huge_page { @start[tid] = nsecs; }kretprobe:alloc_fresh_huge_page /@start[tid]/ { @latency =
hist(nsecs - @start[tid]);delete(@start[tid]); }'
Attaching 2 probes...@latency: [4K, 8K) 10176|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| [8K, 16K) 62 ||
[16K, 32K) 2 |
|Summarize: this feature is about ~2x slower than before.
2) Freeing 10240 @MB hugetlb pages.
a) With this patch series applied:
# time echo 0 > /proc/sys/vm/nr_hugepages
Quoted 7 lines
Quoted 4 lines
real 0m0.004s user 0m0.000s sys 0m0.002sSomething is wrong here, it is faster than the case without this patchset:
0.004s vs. 0m0.077s
Yeah, it is faster. And Why the 'real' time of patched is smaller than before? Because in this patch series, the freeing HugeTLB is asynchronous(through worker).
Quoted 131 lines
Quoted 42 lines
# bpftrace -e 'kprobe:__free_hugepage { @start[tid] = nsecs; } kretprobe:__free_hugepage /@start[tid]/ { @latency = hist(nsecs - @start[tid]); delete(@start[tid]); }' Attaching 2 probes...@latency: [16K, 32K) 10240|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| b) Without this patch series: # time echo 0 > /proc/sys/vm/nr_hugepages real 0m0.077s user 0m0.001s sys 0m0.075s # bpftrace -e 'kprobe:__free_hugepage { @start[tid] = nsecs; }kretprobe:__free_hugepage /@start[tid]/ { @latency =
hist(nsecs - @start[tid]);delete(@start[tid]); }'
Attaching 2 probes...@latency: [4K, 8K) 9950|@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
(quoted diff) +0
-0@@@@@@@@@@@@@@@@| [8K, 16K) 287 |@|
[16K, 32K) 3 |
|Summarize: The overhead of
__free_hugepageis about ~2-4x slower than
before.
But according to the allocation test above, I think that here is
also ~2x slower than before.But why the 'real' time of patched is smaller than before?
Because
In this patch series, the freeing hugetlb is asynchronous(through kwoker).Quoted 7 lines
Although the overhead has increased, the overhead is not significant. Like MIke said, "However, remember that the majority of use cases create hugetlb pages at
or shortly after boot time and add them to the pool. So, additional overhead is at pool creation time. There is no change to 'normal run time' operations of getting a page from or returning a page to the pool (think page fault/unmap)".It seems it is true. At runtime, people normally don't change hugetlb.
Quoted 66 lines
changelog in v4:
1. Move all the vmemmap functions tohugetlb_vmemmap.c.
2. Make theCONFIG_HUGETLB_PAGE_FREE_VMEMMAPdefault to y, if we
want to
disable this feature, we should disable it by a boot/kernel command line.
3. Remove vmemmap_pgtable_{init, deposit, withdraw}() helper functions.
4. Initialize page table lock for vmemmap throughcore_initcallmechanism.Thanks for Mike and Oscar's suggestions.
changelog in v3:
1. Rename some helps function name. Thanks Mike.
2. Rework some code. Thanks Mike and Oscar.
3. Remap the tail vmemmap page withPAGE_KERNEL_ROinstead ofPAGE_KERNEL. Thanks Matthew.
4. Add some overhead analysis in the cover letter.
5. Use vmemap pmd table lock instead of a hugetlb specific global lock.changelog in v2:
1. Fix do not calldissolve_compound_pageinalloc_huge_page_vmemmap().
2. Fix some typo and code style problems.
3. Remove unusedhandle_vmemmap_fault().
4. Merge some commits to one commit suggested by Mike.Muchun Song (21):
mm/memory_hotplug: Move bootmem info registration API to
bootmem_info.c
mm/memory_hotplug: Move {get,put}_page_bootmem() tobootmem_info.cmm/hugetlb: Introduce a new configHUGETLB_PAGE_FREE_VMEMMAPmm/hugetlb: Introducenr_free_vmemmap_pagesin the struct hstate mm/hugetlb: Introduce pgtable allocation/freeing helpersmm/bootmem_info: Introduce {free,prepare}_vmemmap_page()mm/bootmem_info: Combine bootmem info and type intopage->freelistmm/hugetlb: Initialize page table lock for vmemmap mm/hugetlb: Free the vmemmap pages associated with each hugetlb page mm/hugetlb: Defer freeing of hugetlb pages
mm/hugetlb: Allocate the vmemmap pages associated with each hugetlb
page
mm/hugetlb: Introduceremap_huge_page_pmd_vmemmaphelper mm/hugetlb: UsePG_slabto indicate split pmd
mm/hugetlb: Support freeing vmemmap pages of gigantic page mm/hugetlb: Set the PageHWPoison to the raw error page mm/hugetlb: Flush work when dissolving hugetlb page mm/hugetlb: Add a kernel parameterhugetlb_free_vmemmapmm/hugetlb: Merge pte to huge pmd only for gigantic page mm/hugetlb: Gather discrete indexes of tail page mm/hugetlb: AddBUILD_BUG_ONto catch invalid usage of tail struct
page
mm/hugetlb: Disable freeing vmemmap if struct page size is not power
of twoDocumentation/admin-guide/kernel-parameters.txt | 9 + Documentation/admin-guide/mm/hugetlbpage.rst | 3 + arch/x86/include/asm/hugetlb.h | 17 + arch/x86/include/asm/pgtable_64_types.h | 8 + arch/x86/mm/init_64.c | 7 +- fs/Kconfig | 14 + include/linux/bootmem_info.h | 78 +++ include/linux/hugetlb.h | 19 + include/linux/hugetlb_cgroup.h | 15 +- include/linux/memory_hotplug.h | 27 - mm/Makefile | 2 + mm/bootmem_info.c | 124 ++++ mm/hugetlb.c | 163 +++++- mm/hugetlb_vmemmap.c | 732++++++++++++++++++++++++
Quoted 9 lines
mm/hugetlb_vmemmap.h | 104 ++++ mm/memory_hotplug.c | 116 ---- mm/sparse.c | 5 +- 17 files changed, 1263 insertions(+), 180 deletions(-) create mode 100644 include/linux/bootmem_info.h create mode 100644 mm/bootmem_info.c create mode 100644 mm/hugetlb_vmemmap.c create mode 100644 mm/hugetlb_vmemmap.hThanks
Barry
Signature · 2 lines
Yours, Muchun
Quoted 169 lines
-----Original Message-----
From: Muchun Song [mailto:[email protected]]
Sent: Tuesday, November 17, 2020 11:50 PM
To: Song Bao Hua (Barry Song) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [External] RE: [PATCH v4 00/21] Free some vmemmap pages of hugetlb pageQuoted 13 lines · Song Bao Hua (Barry Song)
Quoted 13 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Muchun Song
Sent: Saturday, November 14, 2020 12:00 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected];Quoted 5 lines
Quoted 5 lines
[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Muchun SongQuoted 10 lines
Quoted 10 lines
Subject: [PATCH v4 00/21] Free some vmemmap pages of hugetlb page
Hi all,
This patch series will free some vmemmap pages(struct page structures) associated with each hugetlbpage when preallocated to save memory.
Nowadays we track the status of physical page frames using struct page structures arranged in one or more arrays. And here exists one-to-one mapping between the physical page frame and the corresponding struct
page
Quoted 6 lines
Quoted 6 lines
structure.
The HugeTLB support is built on top of multiple page size support that is provided by most modern architectures. For example, x86 CPUs normally support 4K and 2M (1G if architecturally supported) page sizes. Every HugeTLB has more than one struct page structure. The 2M HugeTLB has
512
Quoted 10 lines
Quoted 10 lines
struct page structure and 1G HugeTLB has 4096 struct page structures. But in the core of HugeTLB only uses the first 4 (Use of first 4 struct page structures comes from
HUGETLB_CGROUP_MIN_ORDER.) struct page structures to
store metadata associated with each HugeTLB. The rest of the struct page structures are usually read thecompound_headfield which are all the same value. If we can free some struct page memory to buddy system so that we can save a lot of memory.When the system boot up, every 2M HugeTLB has 512 struct page
structures
Quoted 6 lines
Quoted 6 lines
which size is 8
pages(sizeof(struct page) * 512 / PAGE_SIZE).hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> |0
Quoted 2 lines
Quoted 2 lines
|
| | | 1 | -------------> |1
Quoted 2 lines
Quoted 2 lines
|
| | | 2 | -------------> |2
Quoted 2 lines
Quoted 2 lines
|
| | | 3 | -------------> |3
Quoted 2 lines
Quoted 2 lines
|
| | | 4 | -------------> |4
Quoted 4 lines
Quoted 4 lines
|
| 2M | | 5 | -------------> | 5 | | | | 6 | -------------> |6
Quoted 2 lines
Quoted 2 lines
|
| | | 7 | -------------> |7
Quoted 9 lines
Quoted 9 lines
|
| | +-----------+ +-----------+ | | | | +-----------+When a hugetlbpage is preallocated, we can change the mapping from
above
Quoted 7 lines
Quoted 7 lines
to
bellow.hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> |0
Quoted 2 lines
Quoted 2 lines
|
| | | 1 | -------------> |1
Quoted 4 lines
Quoted 4 lines
|
| | | 2 | -------------> +-----------+ | | | 3 | -----------------^ ^^ ^
Quoted 2 lines
Quoted 2 lines
^
| | | 4 | -------------------+| | > > | > > | 2M | | 5 | ---------------------+ | > > | > > | | | 6 | -----------------------+ | > > | | | 7 |-------------------------+
Quoted 11 lines
Quoted 11 lines
| | +-----------+ | | | | +-----------+For tail pages, the value of
compound_headis the same. So we can reuse first page of tail page structs. We map the virtual addresses of the remaining 6 pages of tail page structs to the first tail page struct, and then free these 6 pages. Therefore, we need to reserve at least 2 pages as vmemmap areas.Quoted 4 lines
Quoted 4 lines
When a hugetlbpage is freed to the buddy system, we should allocate six pages for vmemmap pages and restore the previous mapping relationship.
If we uses the 1G hugetlbpage, we can save 4088 pages(There are 4096
pages
Quoted 2 lines
Quoted 2 lines
for
struct page structures, we reserve 2 pages for vmemmap and 8 pages forpage
Quoted 2 lines
Quoted 2 lines
tables. So we can save 4088 pages). This is a very substantial gain. On our server, run some SPDK/QEMU applications which will use 1024GB
hugetlbpage.
Quoted 7 lines
Quoted 2 lines
With this feature enabled, we can save ~16GB(1G hugepage)/~11GB(2MB hugepage)
Hi Muchun,
Do we really save 11GB for 2MB hugepage?
How much do we save if we only get one 2MB hugetlb from one 128MB
mem_section?Quoted 1 line
It seems we need to get at least one page for the PTEs since we are splitting
PMD of
Quoted 1 line
vmemmap into PTE?
Quoted 9 lines
There are 524288(1024GB/2MB) 2MB HugeTLB pages. We can save 6 pages for each
2MB HugeTLB page. So we can save 3145728 pages. But we need to split PMD page
table for every one 128MBmem_sectionand every section need one page as PTE page
table. So we need 8192(1024GB/128MB) pages as PTE page tables. Finally, we can save
3137536(3145728-8192) pages which is 11.97GB.
The worst case I can see is that:
if we get 100 hugetlb with 2MB size, but the 100 hugetlb comes from different mem_section, we won't save 11.97GB. we only save 5/8 * 16GB=10GB.
Anyway, it seems 11GB is in the middle of 10GB and 11.97GB, so sounds sensible :-)
ideally, we should be able to free PageTail if we change struct page in some way. Then we will save much more for 2MB hugetlb. but it seems it is not easy.
Thanks
Barry
Quoted 184 lines · Song Bao Hua (Barry Song)
Quoted 170 lines
-----Original Message-----
From: Muchun Song [mailto:[email protected]]
Sent: Tuesday, November 17, 2020 11:50 PM
To: Song Bao Hua (Barry Song) <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [External] RE: [PATCH v4 00/21] Free some vmemmap pages of hugetlb pageQuoted 13 lines · Song Bao Hua (Barry Song)
Quoted 13 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Muchun Song
Sent: Saturday, November 14, 2020 12:00 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected];Quoted 5 lines
Quoted 5 lines
[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Muchun SongQuoted 10 lines
Quoted 10 lines
Subject: [PATCH v4 00/21] Free some vmemmap pages of hugetlb page
Hi all,
This patch series will free some vmemmap pages(struct page structures) associated with each hugetlbpage when preallocated to save memory.
Nowadays we track the status of physical page frames using struct page structures arranged in one or more arrays. And here exists one-to-one mapping between the physical page frame and the corresponding struct
page
Quoted 6 lines
Quoted 6 lines
structure.
The HugeTLB support is built on top of multiple page size support that is provided by most modern architectures. For example, x86 CPUs normally support 4K and 2M (1G if architecturally supported) page sizes. Every HugeTLB has more than one struct page structure. The 2M HugeTLB has
512
Quoted 10 lines
Quoted 10 lines
struct page structure and 1G HugeTLB has 4096 struct page structures. But in the core of HugeTLB only uses the first 4 (Use of first 4 struct page structures comes from
HUGETLB_CGROUP_MIN_ORDER.) struct page structures to
store metadata associated with each HugeTLB. The rest of the struct page structures are usually read thecompound_headfield which are all the same value. If we can free some struct page memory to buddy system so that we can save a lot of memory.When the system boot up, every 2M HugeTLB has 512 struct page
structures
Quoted 6 lines
Quoted 6 lines
which size is 8
pages(sizeof(struct page) * 512 / PAGE_SIZE).hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> |0
Quoted 2 lines
Quoted 2 lines
|
| | | 1 | -------------> |1
Quoted 2 lines
Quoted 2 lines
|
| | | 2 | -------------> |2
Quoted 2 lines
Quoted 2 lines
|
| | | 3 | -------------> |3
Quoted 2 lines
Quoted 2 lines
|
| | | 4 | -------------> |4
Quoted 4 lines
Quoted 4 lines
|
| 2M | | 5 | -------------> | 5 | | | | 6 | -------------> |6
Quoted 2 lines
Quoted 2 lines
|
| | | 7 | -------------> |7
Quoted 9 lines
Quoted 9 lines
|
| | +-----------+ +-----------+ | | | | +-----------+When a hugetlbpage is preallocated, we can change the mapping from
above
Quoted 7 lines
Quoted 7 lines
to
bellow.hugetlbpage struct pages(8 pages) page
frame(8 pages)+-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ | | | 0 | -------------> |0
Quoted 2 lines
Quoted 2 lines
|
| | | 1 | -------------> |1
Quoted 4 lines
Quoted 4 lines
|
| | | 2 | -------------> +-----------+ | | | 3 | -----------------^ ^^ ^
Quoted 2 lines
Quoted 2 lines
^
| | | 4 | -------------------+| | > > | > > | 2M | | 5 | ---------------------+ | > > | > > | | | 6 | -----------------------+ | > > | | | 7 |-------------------------+
Quoted 11 lines
Quoted 11 lines
| | +-----------+ | | | | +-----------+For tail pages, the value of
compound_headis the same. So we can reuse first page of tail page structs. We map the virtual addresses of the remaining 6 pages of tail page structs to the first tail page struct, and then free these 6 pages. Therefore, we need to reserve at least 2 pages as vmemmap areas.Quoted 4 lines
Quoted 4 lines
When a hugetlbpage is freed to the buddy system, we should allocate six pages for vmemmap pages and restore the previous mapping relationship.
If we uses the 1G hugetlbpage, we can save 4088 pages(There are 4096
pages
Quoted 2 lines
Quoted 2 lines
for
struct page structures, we reserve 2 pages for vmemmap and 8 pages forpage
Quoted 2 lines
Quoted 2 lines
tables. So we can save 4088 pages). This is a very substantial gain. On our server, run some SPDK/QEMU applications which will use 1024GB
hugetlbpage.
Quoted 7 lines
Quoted 2 lines
With this feature enabled, we can save ~16GB(1G hugepage)/~11GB(2MB hugepage)
Hi Muchun,
Do we really save 11GB for 2MB hugepage?
How much do we save if we only get one 2MB hugetlb from one 128MB
mem_section?Quoted 1 line
It seems we need to get at least one page for the PTEs since we are splitting
PMD of
Quoted 1 line
vmemmap into PTE?
Quoted 9 lines
There are 524288(1024GB/2MB) 2MB HugeTLB pages. We can save 6 pages for each
2MB HugeTLB page. So we can save 3145728 pages. But we need to split PMD page
table for every one 128MBmem_sectionand every section need one page as PTE page
table. So we need 8192(1024GB/128MB) pages as PTE page tables. Finally, we can save
3137536(3145728-8192) pages which is 11.97GB.The worst case I can see is that:
if we get 100 hugetlb with 2MB size, but the 100 hugetlb comes from differentmem_section, we won't save 11.97GB. we only save 5/8 * 16GB=10GB.
Quoted 5 lines
Anyway, it seems 11GB is in the middle of 10GB and 11.97GB, so sounds sensible :-)
ideally, we should be able to free PageTail if we change struct page in some way. Then we will save much more for 2MB hugetlb. but it seems it is not easy.
Now for the 2MB HugrTLB page, we only free 6 vmemmap pages. But your words woke me up. Maybe we really can free 7 vmemmap pages. In this case, we can see 8 of the 512 struct page structures has beed set PG_head flag. If we can adjust compound_head() slightly and make compound_head() return the real head struct page when the parameter is the tail struct page but with PG_head flag set. I will start an investigation and a test.
Thanks.
Quoted 3 lines
Thanks
Barry
Signature · 2 lines
Yours, Muchun
Quoted 10 lines · Muchun Song
Quoted 2 lines
ideally, we should be able to free PageTail if we change struct page in some way. Then we will save much more for 2MB hugetlb. but it seems it is not easy.
Now for the 2MB HugrTLB page, we only free 6 vmemmap pages. But your words woke me up. Maybe we really can free 7 vmemmap pages. In this case, we can see 8 of the 512 struct page structures has beed set
PG_headflag. If we can adjustcompound_head()slightly and makecompound_head()return the real head struct page when the parameter is the tail struct page but withPG_headflag set. I will start an investigation and a test.
What are you thinking?
static inline struct page *compound_head(struct page *page)
{
unsigned long head = READ_ONCE(page->compound_head);
if (unlikely(head & 1))
return (struct page *) (head - 1);
+ if (unlikely(page->flags & PG_head))
+ return (struct page *)(page[1]->compound_head - 1)
return page;
}... because if it's that, there are code paths which also just test PageHead, and so we'd actually need to change PageHead to be something like:
static inline bool PageHead(struct page *page)
{
return (page->flags & PG_head) &&
(page[1]->compound_head == (unsigned long)page + 1);
}I'm not sure if that's worth doing -- there may be other things I haven't thought of.
Quoted 21 lines · Matthew Wilcox
Quoted 10 lines · Muchun Song
Quoted 2 lines
ideally, we should be able to free PageTail if we change struct page in some way. Then we will save much more for 2MB hugetlb. but it seems it is not easy.
Now for the 2MB HugrTLB page, we only free 6 vmemmap pages. But your words woke me up. Maybe we really can free 7 vmemmap pages. In this case, we can see 8 of the 512 struct page structures has beed set
PG_headflag. If we can adjustcompound_head()slightly and makecompound_head()return the real head struct page when the parameter is the tail struct page but withPG_headflag set. I will start an investigation and a test.What are you thinking?
static inline struct page *compound_head(struct page *page)
{ unsigned long head = READ_ONCE(page->compound_head); if (unlikely(head & 1)) return (struct page *) (head - 1);
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (unlikely(page->flags & PG_head)) + return (struct page *)(page[1]->compound_head - 1)
Yeah, I think so too. Maybe adding an align check is better.
+ if ((test_bit(PG_head, &page->flags) &&
+ IS_ALIGNED((unsigned long)page, PAGE_SIZE))
Quoted 6 lines
return page; }... because if it's that, there are code paths which also just test PageHead, and so we'd actually need to change PageHead to be something like:
Yeah, I also think that rework compound_head() and PageHead() is enough.
Thanks.
Quoted 9 lines
static inline bool
PageHead(struct page *page){ return (page->flags & PG_head) && (page[1]->compound_head == (unsigned long)page + 1); }I'm not sure if that's worth doing -- there may be other things I haven't thought of.
Signature · 2 lines
Yours, Muchun
Oscar Salvador
maintainerQuoted 7 lines · Muchun Song
Now for the 2MB HugrTLB page, we only free 6 vmemmap pages. But your words woke me up. Maybe we really can free 7 vmemmap pages. In this case, we can see 8 of the 512 struct page structures has beed set
PG_headflag. If we can adjustcompound_head()slightly and makecompound_head()return the real head struct page when the parameter is the tail struct page but withPG_headflag set. I will start an investigation and a test.
I would not overcomplicate things at this stage, but rather keep it simple as the code is already tricky enough(without counting the LOC thatvit adds). We can always build on top later on in order to improve things.
Signature · 2 lines
Oscar Salvador SUSE L3
Quoted 29 lines
-----Original Message-----
From: Oscar Salvador [mailto:[email protected]]
Sent: Wednesday, November 18, 2020 8:45 AM
To: Muchun Song <[email protected]>
Cc: Song Bao Hua (Barry Song) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [External] RE: [PATCH v4 00/21] Free some vmemmap pages of hugetlb pageQuoted 7 lines · Muchun Song
Now for the 2MB HugrTLB page, we only free 6 vmemmap pages. But your words woke me up. Maybe we really can free 7 vmemmap pages. In this case, we can see 8 of the 512 struct page structures has beed set
PG_headflag. If we can adjustcompound_head()slightly and makecompound_head()return the real head struct page when the parameter is the tail struct page but withPG_headflag set. I will start an investigation and a test.
Quoted 4 lines
I would not overcomplicate things at this stage, but rather keep it simple as the code is already tricky enough(without counting the LOC thatvit adds).
We can always build on top later on in order to improve things.
Yep. I am not expecting freeing tail page to be done at this stage. This could be something in the todo list after the first patchset is solid.
Quoted 4 lines
--
Oscar Salvador
SUSE L3
Thanks
Barry
Quoted 10 lines · Oscar Salvador
Quoted 7 lines · Muchun Song
Now for the 2MB HugrTLB page, we only free 6 vmemmap pages. But your words woke me up. Maybe we really can free 7 vmemmap pages. In this case, we can see 8 of the 512 struct page structures has beed set
PG_headflag. If we can adjustcompound_head()slightly and makecompound_head()return the real head struct page when the parameter is the tail struct page but withPG_headflag set. I will start an investigation and a test.
Quoted 4 lines
I would not overcomplicate things at this stage, but rather keep it simple as the code is already tricky enough(without counting the LOC thatvit adds).
We can always build on top later on in order to improve things.
I think that this improvement can be a separate patch. In this case, it can make the code evolution route clearer.
Thanks.
Quoted 4 lines
--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun