Free some vmemmap pages of hugetlb page
Subsystems (9)
v3 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 arranged in one or more arrays. And here exists one-to-one mapping between the physical page frame and the corresponding struct page.
The hugetlbpage 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 hugetlbpage has more than one struct page. The 2M hugetlbpage has 512 struct page and 1G hugetlbpage has 4096 struct page. But in the core of hugetlbpage only uses the first 4 struct page to store metadata associated with each hugetlbpage. The rest of the struct page 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 hugetlbpage has 512 struct page which 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 4095 pages. This is a very substantial gain. On our server, run some SPDK/QEMU applications which will use 1000GB 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. But the overhead is not on the allocating/freeing of each hugetlb page, it is only once when we reserve some hugetlb pages through /proc/sys/vm/nr_hugepages. Once the reservation is successful, the subsequent allocating, freeing and using are the same as before (not patched). So I think that the overhead is acceptable.
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/vmemmap: 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: Add a BUILD_BUG_ON to check if struct page size is a power
of two
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
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 | 16 + include/linux/bootmem_info.h | 79 +++ include/linux/hugetlb.h | 45 ++ include/linux/hugetlb_cgroup.h | 15 +- include/linux/memory_hotplug.h | 27 - include/linux/mm.h | 49 ++ mm/Makefile | 1 + mm/bootmem_info.c | 124 ++++ mm/hugetlb.c | 806 +++++++++++++++++++++++- mm/memory_hotplug.c | 116 ---- mm/sparse-vmemmap.c | 31 + mm/sparse.c | 5 +- 17 files changed, 1181 insertions(+), 177 deletions(-) create mode 100644 include/linux/bootmem_info.h create mode 100644 mm/bootmem_info.c
Signature · 1 line
2.11.0
PATCH v3 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
PATCH v3 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 | 26 ++++++++++++++++++++++++++ mm/memory_hotplug.c | 27 --------------------------- mm/sparse.c | 1 + 6 files changed, 41 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 +26 -0
@@ -10,6 +10,32 @@#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
PATCH v3 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 | 16 ++++++++++++++++ mm/bootmem_info.c | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-)
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 +16 -0
@@ -245,6 +245,22 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + bool "Free unused vmemmap associated with HugeTLB pages" + default y + depends on X86 + depends on HUGETLB_PAGE + depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + There are many struct page structures associated with each HugeTLB + page. But we only use a few struct page structures. In this case, + it wastes some memory. It is better to free the unused struct page + structures to buddy system which can save some memory. For + architectures that support it, say Y here. + + If unsure, say N. + config MEMFD_CREATE def_bool TMPFS || HUGETLBFS
mm/bootmem_info.c +1 -2
@@ -10,8 +10,7 @@#include <linux/bootmem_info.h> #include <linux/memory_hotplug.h> -void get_page_bootmem (unsigned long info,struct page *page, -unsigned long type) +void get_page_bootmem (unsigned long info,struct page *page,unsigned long type) { page->freelist = (void *)type;SetPagePrivate (page);
Signature · 1 line
2.11.0
Oscar Salvador
maintainerRe: [PATCH v3 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
Quoted 37 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 | 16 ++++++++++++++++ mm/bootmem_info.c | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-)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 +5
-0@@ -245,6 +245,22 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + bool "Free unused vmemmap associated with HugeTLB pages" + default y + depends on X86 + depends on HUGETLB_PAGE
fs/Kconfig config HUGETLBFS · line 262
Quoted 10 lines
fs/Kconfig +10
-0+ depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + There are many struct page structures associated with each HugeTLB + page. But we only use a few struct page structures. In this case, + it wastes some memory. It is better to free the unused struct page + structures to buddy system which can save some memory. For + architectures that support it, say Y here. + + If unsure, say N.
I am not sure the above is useful for someone who needs to decide whether he needs/wants to enable this or not.
I think the above fits better in a Documentation part.
I suck at this, but what about the following, or something along those lines?
"
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 4095 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. Therefore, this is a trade-off between saving memory and increasing time in allocation/free path.
"
It would be also great to point out that this might be a trade-off between saving up memory and increasing the cost of certain operations on allocation/free path. That is why I mentioned it there.
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
Quoted 57 lines · Oscar Salvador
Quoted 37 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 | 16 ++++++++++++++++ mm/bootmem_info.c | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-)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 +5
-0@@ -245,6 +245,22 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + bool "Free unused vmemmap associated with HugeTLB pages" + default y + depends on X86 + depends on HUGETLB_PAGEfs/Kconfig config HUGETLBFS · line 262
Quoted 10 lines
fs/Kconfig +10
-0+ depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + There are many struct page structures associated with each HugeTLB + page. But we only use a few struct page structures. In this case, + it wastes some memory. It is better to free the unused struct page + structures to buddy system which can save some memory. For + architectures that support it, say Y here. + + If unsure, say N.I am not sure the above is useful for someone who needs to decide whether he needs/wants to enable this or not.
I think the above fits better in a Documentation part.I suck at this, but what about the following, or something along those lines?
Quoted 10 lines
"
When usingSPARSEMEM_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 4095 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. Therefore, this is a trade-off between saving memory and increasing time in allocation/free path.
"
Will do. Thanks for your suggestions.
Quoted 5 lines
It would be also great to point out that this might be a trade-off between saving up memory and increasing the cost of certain operations on allocation/free path. That is why I mentioned it there.
OK, I will add this to the Documentation part, thanks.
Quoted 4 lines
--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun
Re: [PATCH v3 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
In reply to Oscar SalvadorQuoted 67 lines · Oscar Salvador
Quoted 37 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 | 16 ++++++++++++++++ mm/bootmem_info.c | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-)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 +5
-0@@ -245,6 +245,22 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + bool "Free unused vmemmap associated with HugeTLB pages" + default y + depends on X86 + depends on HUGETLB_PAGEfs/Kconfig config HUGETLBFS · line 262
Quoted 10 lines
fs/Kconfig +10
-0+ depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + There are many struct page structures associated with each HugeTLB + page. But we only use a few struct page structures. In this case, + it wastes some memory. It is better to free the unused struct page + structures to buddy system which can save some memory. For + architectures that support it, say Y here. + + If unsure, say N.I am not sure the above is useful for someone who needs to decide whether he needs/wants to enable this or not.
I think the above fits better in a Documentation part.I suck at this, but what about the following, or something along those lines?
"
When usingSPARSEMEM_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 4095 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. Therefore, this is a trade-off between saving memory and increasing time in allocation/free path.
"
Quoted 4 lines
It would be also great to point out that this might be a trade-off between saving up memory and increasing the cost of certain operations on allocation/free path. That is why I mentioned it there.
Yes, this is somewhat a trade-off.
As a config option, this is something that would likely be decided by distros. I almost hate to suggest this, but is it something that an end user would want to decide? Is this something that perhaps should be a boot/kernel command line option?
Signature · 1 line
Mike Kravetz
Re: [PATCH v3 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
Quoted 73 lines · Mike Kravetz
Quoted 67 lines · Oscar Salvador
Quoted 37 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 | 16 ++++++++++++++++ mm/bootmem_info.c | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-)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 +5
-0@@ -245,6 +245,22 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + bool "Free unused vmemmap associated with HugeTLB pages" + default y + depends on X86 + depends on HUGETLB_PAGEfs/Kconfig config HUGETLBFS · line 262
Quoted 10 lines
fs/Kconfig +10
-0+ depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + There are many struct page structures associated with each HugeTLB + page. But we only use a few struct page structures. In this case, + it wastes some memory. It is better to free the unused struct page + structures to buddy system which can save some memory. For + architectures that support it, say Y here. + + If unsure, say N.I am not sure the above is useful for someone who needs to decide whether he needs/wants to enable this or not.
I think the above fits better in a Documentation part.I suck at this, but what about the following, or something along those lines?
"
When usingSPARSEMEM_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 4095 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. Therefore, this is a trade-off between saving memory and increasing time in allocation/free path.
"Quoted 4 lines
It would be also great to point out that this might be a trade-off between saving up memory and increasing the cost of certain operations on allocation/free path. That is why I mentioned it there.
Quoted 6 lines
Yes, this is somewhat a trade-off.
As a config option, this is something that would likely be decided by distros. I almost hate to suggest this, but is it something that an end user would want to decide? Is this something that perhaps should be a boot/kernel command line option?
I don't like config options. I like boot options even less. I don't know how to describe to an end-user whether they should select this or not. Is there a way to make this not a tradeoff? Or make the tradeoff so minimal as to be not worth describing? (do we have numbers for the worst possible situation when enabling this option?)
I haven't read through these patches in detail, so maybe we do this already, but when we free the pages to the buddy allocator, do we retain the third page to use for the PTEs (and free pages 3-7), or do we allocate a separate page for the PTES and free pages 2-7?
Re: [PATCH v3 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
Quoted 9 lines · Matthew Wilcox
Quoted 2 lines · Mike Kravetz
Quoted 1 line · Oscar Salvador
On Sun, Nov 08, 2020 at 10:10:55PM +0800, Muchun Song wrote:
I don't like config options. I like boot options even less. I don't know how to describe to an end-user whether they should select this or not. Is there a way to make this not a tradeoff? Or make the tradeoff so minimal as to be not worth describing? (do we have numbers for the worst possible situation when enabling this option?)
It is not exactly worst case, but Muchun provided some simple benchmarking results in the cover letter. Quick summary is that hugetlb page creation and free time is "~2x slower". At first glance, one would say that is terrible. 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).
Quoted 4 lines
I haven't read through these patches in detail, so maybe we do this already, but when we free the pages to the buddy allocator, do we retain the third page to use for the PTEs (and free pages 3-7), or do we allocate a separate page for the PTES and free pages 2-7?
I haven't got there in this latest series. But, in previous revisions the code did allocate a separate page.
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v3 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
In reply to Matthew WilcoxQuoted 88 lines · Matthew Wilcox
Quoted 73 lines · Mike Kravetz
Quoted 67 lines · Oscar Salvador
Quoted 37 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 | 16 ++++++++++++++++ mm/bootmem_info.c | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-)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 +5
-0@@ -245,6 +245,22 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + bool "Free unused vmemmap associated with HugeTLB pages" + default y + depends on X86 + depends on HUGETLB_PAGEfs/Kconfig config HUGETLBFS · line 262
Quoted 10 lines
fs/Kconfig +10
-0+ depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + There are many struct page structures associated with each HugeTLB + page. But we only use a few struct page structures. In this case, + it wastes some memory. It is better to free the unused struct page + structures to buddy system which can save some memory. For + architectures that support it, say Y here. + + If unsure, say N.I am not sure the above is useful for someone who needs to decide whether he needs/wants to enable this or not.
I think the above fits better in a Documentation part.I suck at this, but what about the following, or something along those lines?
"
When usingSPARSEMEM_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 4095 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. Therefore, this is a trade-off between saving memory and increasing time in allocation/free path.
"Quoted 4 lines
It would be also great to point out that this might be a trade-off between saving up memory and increasing the cost of certain operations on allocation/free path. That is why I mentioned it there.
Quoted 6 lines
Yes, this is somewhat a trade-off.
As a config option, this is something that would likely be decided by distros. I almost hate to suggest this, but is it something that an end user would want to decide? Is this something that perhaps should be a boot/kernel command line option?
I don't like config options. I like boot options even less. I don't know how to describe to an end-user whether they should select this or not. Is there a way to make this not a tradeoff? Or make the tradeoff so minimal as to be not worth describing? (do we have numbers for the worst possible situation when enabling this option?)
Quoted 4 lines
I haven't read through these patches in detail, so maybe we do this already, but when we free the pages to the buddy allocator, do we retain the third page to use for the PTEs (and free pages 3-7), or do we allocate a separate page for the PTES and free pages 2-7?
Sorry for missing this reply. It is a good idea. I will start an investigation and implement this. Thanks Matthew.
Signature · 2 lines
Yours, Muchun
Re: [External] Re: [PATCH v3 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP
In reply to Mike KravetzQuoted 74 lines · Mike Kravetz
Quoted 67 lines · Oscar Salvador
Quoted 37 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 | 16 ++++++++++++++++ mm/bootmem_info.c | 3 +-- 3 files changed, 18 insertions(+), 3 deletions(-)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 +5
-0@@ -245,6 +245,22 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + bool "Free unused vmemmap associated with HugeTLB pages" + default y + depends on X86 + depends on HUGETLB_PAGEfs/Kconfig config HUGETLBFS · line 262
Quoted 10 lines
fs/Kconfig +10
-0+ depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + There are many struct page structures associated with each HugeTLB + page. But we only use a few struct page structures. In this case, + it wastes some memory. It is better to free the unused struct page + structures to buddy system which can save some memory. For + architectures that support it, say Y here. + + If unsure, say N.I am not sure the above is useful for someone who needs to decide whether he needs/wants to enable this or not.
I think the above fits better in a Documentation part.I suck at this, but what about the following, or something along those lines?
"
When usingSPARSEMEM_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 4095 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. Therefore, this is a trade-off between saving memory and increasing time in allocation/free path.
"Quoted 4 lines
It would be also great to point out that this might be a trade-off between saving up memory and increasing the cost of certain operations on allocation/free path. That is why I mentioned it there.
Quoted 6 lines
Yes, this is somewhat a trade-off.
As a config option, this is something that would likely be decided by distros. I almost hate to suggest this, but is it something that an end user would want to decide? Is this something that perhaps should be a boot/kernel command line option?
Yeah, it already has a boot/kernel command line option named "hugetlb_free_vmemmap". We can refer to
[PATCH v3 18/21] mm/hugetlb: Add a kernel parameter hugetlb_free_vmemmap
Thanks :)
Quoted 3 lines
--
Mike Kravetz
--
Yours,
Muchun
PATCH v3 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/hugetlb.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+)
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/hugetlb.c +38 -0
@@ -1292,6 +1292,42 @@ static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { }#endif +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +/* +* There are 512 struct page structs(8 pages) associated with each 2MB +* hugetlb page. For tail pages, the value of compound_dtor 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. +*/ +#define RESERVE_VMEMMAP_NR 2U + +static 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 not free to buddy system, +* the others page will map to the first tail page. So there are +* (@vmemmap_pages - RESERVE_VMEMMAP_NR) pages can be freed. +*/ +if (likely (vmemmap_pages >RESERVE_VMEMMAP_NR )) + h->nr_free_vmemmap_pages = vmemmap_pages -RESERVE_VMEMMAP_NR ; +else + h->nr_free_vmemmap_pages =0 ; + +pr_debug ("HugeTLB: can free %d vmemmap pages for %s\n" , + h->nr_free_vmemmap_pages, h->name); +} +#else +static inline void hugetlb_vmemmap_init (struct hstate *h) +{ +} +#endif +static void update_and_free_page (struct hstate *h,struct page *page) {int i;
@@ -3285,6 +3321,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; }
Signature · 1 line
2.11.0
Oscar Salvador
maintainerRe: [PATCH v3 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1298
Quoted 4 lines · Muchun Song
mm/hugetlb.c +4
-0+#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +/* +* There are 512 struct page structs(8 pages) associated with each 2MB +* hugetlb page. For tail pages, the value of compound_dtor is the same.
I gess you meant "For tail pages, the value of compound_head ...", right?
Quoted 5 lines
+ * 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.
+ */
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1313
Quoted 10 lines
mm/hugetlb.c +10
-0+#define RESERVE_VMEMMAP_NR 2U + +static 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 not free to buddy system,
"The head page and the first tail page are not to be freed to..." better?
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1315
Quoted 2 lines
mm/hugetlb.c +2
-0+* the others page will map to the first tail page. So there are +* (@vmemmap_pages - RESERVE_VMEMMAP_NR) pages can be freed.
^^^
thatmm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1320
Quoted 2 lines
mm/hugetlb.c +2
-0+else + h->nr_free_vmemmap_pages =0 ;
I would specify that this is not expected to happen.
(At least I could not come up with a real scenario unless the system is corrupted)
So, I would drop a brief comment pointing out that it is only a safety net.
Unrelated to this patch but related in general, I am not sure about Mike but would it be cleaner to move all the vmemmap functions to hugetlb_vmemmap.c? hugetlb code is quite tricky, so I am not sure about stuffing more code in there.
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
Quoted 7 lines · Oscar Salvador
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1298
Quoted 4 lines · Muchun Song
mm/hugetlb.c +4
-0+#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +/* +* There are 512 struct page structs(8 pages) associated with each 2MB +* hugetlb page. For tail pages, the value of compound_dtor is the same. I gess you meant "For tail pages, the value of
compound_head...", right?
Yeah, Thanks.
Quoted 10 lines
Quoted 10 lines
+ * 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.
+ */mm/hugetlb.c +4
-0+#define RESERVE_VMEMMAP_NR 2U + +static void __init hugetlb_vmemmap_init (struct hstate *h) +{
Quoted 8 lines
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1313
Quoted 6 lines
mm/hugetlb.c +6
-0+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 not free to buddy system, "The head page and the first tail page are not to be freed to..." better?
Yeah, sorry for my poor English :).
Quoted 7 lines
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1315
Quoted 4 lines
mm/hugetlb.c +2
-0+* the others page will map to the first tail page. So there are +* (@vmemmap_pages - RESERVE_VMEMMAP_NR) pages can be freed. ^^^ that
Quoted 8 lines
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1320
Quoted 2 lines
mm/hugetlb.c +2
-0+else + h->nr_free_vmemmap_pages =0 ;I would specify that this is not expected to happen.
(At least I could not come up with a real scenario unless the system is corrupted)
So, I would drop a brief comment pointing out that it is only a safety net.
I will add a comment to point out this.
Quoted 7 lines
Unrelated to this patch but related in general, I am not sure about Mike but would it be cleaner to move all the vmemmap functions to
hugetlb_vmemmap.c? hugetlb code is quite tricky, so I am not sure about stuffing more code in there.
I also think that you are right, moving all the vmemmap functions to hugetlb_vmemmap.c may make the code cleaner.
Hi Mike, what's your opinion?
Thanks.
Quoted 3 lines
--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun
Re: [External] Re: [PATCH v3 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
Quoted 10 lines · Muchun Song
Quoted 8 lines · Oscar Salvador
On Sun, Nov 08, 2020 at 10:10:56PM +0800, Muchun Song wrote:
Unrelated to this patch but related in general, I am not sure about Mike but would it be cleaner to move all the vmemmap functions to
hugetlb_vmemmap.c? hugetlb code is quite tricky, so I am not sure about stuffing more code in there.
Quoted 4 lines
I also think that you are right, moving all the vmemmap functions to
hugetlb_vmemmap.cmay make the code cleaner.Hi Mike, what's your opinion?
I would be happy to see this in a separate file. As Oscar mentions, the hugetlb.c file/code is already somethat difficult to read and understand.
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v3 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate
Quoted 12 lines · Mike Kravetz
Quoted 10 lines · Muchun Song
Quoted 8 lines · Oscar Salvador
On Sun, Nov 08, 2020 at 10:10:56PM +0800, Muchun Song wrote:
Unrelated to this patch but related in general, I am not sure about Mike but would it be cleaner to move all the vmemmap functions to
hugetlb_vmemmap.c? hugetlb code is quite tricky, so I am not sure about stuffing more code in there.
Quoted 7 lines
Quoted 4 lines
I also think that you are right, moving all the vmemmap functions to
hugetlb_vmemmap.cmay make the code cleaner.Hi Mike, what's your opinion?
I would be happy to see this in a separate file. As Oscar mentions, the
hugetlb.cfile/code is already somethat difficult to read and understand.
Got it. I will do this. Thanks.
Quoted 2 lines
--
Mike Kravetz
Signature · 2 lines
Yours, Muchun
PATCH v3 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.
--- include/linux/hugetlb.h | 10 +++++ mm/hugetlb.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+)
include/linux/hugetlb.h +10 -0
@@ -593,6 +593,16 @@ static inline unsigned int blocks_per_huge_page(struct hstate *h)#include <asm/hugetlb.h> +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +#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)) +#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ +#ifndef is_hugepage_only_rangestatic inline int is_hugepage_only_range (struct mm_struct *mm,unsigned long addr,unsigned long len)
mm/hugetlb.c +111 -0
123 lines not on this page · raw message
Signature · 1 line
2.11.0
Oscar Salvador
maintainerRe: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1329
Quoted 7 lines · Muchun Song
mm/hugetlb.c +7
-0+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 tabels when there is no vmemmap pages +* to free.
s /tabels/tables/
Quoted 14 lines
mm/hugetlb.c +14
-0+static int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +int i; +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return 0 ; + +vmemmap_pgtable_init (page); + +for (i =0 ; i < nr; i++) { +pte_t *pte_p; +
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1388
Quoted 10 lines
mm/hugetlb.c +10
-0+ pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +vmemmap_pgtable_deposit (page,virt_to_page (pte_p)); + } + +return 0 ; +out: +while (i-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable));
would not be enough to:
while (pgtable = vmemmap_pgtable_withdrag(page))
pte_free_kernel(&init_mm, page_to_virt(pgtable));
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1398
Quoted 10 lines
mm/hugetlb.c +10
-0+return -ENOMEM ; +} + +static void vmemmap_pgtable_free (struct hstate *h,struct page *page) +{ +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return ;
We can get rid of "nr" and its check and keep only the check below, right? AFAICS, they go together, e.g: if page_huge_pte does not return null, it means that we preallocated a pagetable, and viceversa.
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1405
Quoted 7 lines
mm/hugetlb.c +7
-0+ + pgtable =page_huge_pte (page); +if (!pgtable) +return ; + +while (nr-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable));
Same as above, that "nr" can go?
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 10 lines · Oscar Salvador
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1329
Quoted 7 lines · Muchun Song
mm/hugetlb.c +7
-0+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 tabels when there is no vmemmap pages +* to free. s /tabels/tables/
Thanks.
Quoted 28 lines
Quoted 15 lines
mm/hugetlb.c +14
-0+static int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +int i; +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return 0 ; + +vmemmap_pgtable_init (page); + +for (i =0 ; i < nr; i++) { +pte_t *pte_p; +mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1388
Quoted 10 lines
mm/hugetlb.c +10
-0+ pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +vmemmap_pgtable_deposit (page,virt_to_page (pte_p)); + } + +return 0 ; +out: +while (i-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable));would not be enough to:
Quoted 2 lines
while (pgtable =
vmemmap_pgtable_withdrag(page))
pte_free_kernel(&init_mm, page_to_virt(pgtable));
The vmemmap_pgtable_withdraw can not return NULL. So we can not drop the "i--".
Quoted 13 lines
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1398
Quoted 11 lines
mm/hugetlb.c +10
-0+return -ENOMEM ; +} + +static void vmemmap_pgtable_free (struct hstate *h,struct page *page) +{ +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return ;We can get rid of "nr" and its check and keep only the check below, right?
Great, the check can go away.
Quoted 4 lines
AFAICS, they go together, e.g: if
page_huge_ptedoes not return null, it means that we preallocated a pagetable, and viceversa.
Quoted 9 lines
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1405
Quoted 7 lines
mm/hugetlb.c +7
-0+ + pgtable =page_huge_pte (page); +if (!pgtable) +return ; + +while (nr-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable));Same as above, that "nr" can go?
Here "nr" can not go. Because the vmemmap_pgtable_withdraw can not return NULL.
Thanks.
Quoted 4 lines
--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun
Oscar Salvador
maintainerRe: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 42 lines · Muchun Song
Quoted 10 lines · Oscar Salvador
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1329
Quoted 7 lines · Muchun Song
mm/hugetlb.c +7
-0+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 tabels when there is no vmemmap pages +* to free. s /tabels/tables/
Thanks.
Quoted 28 lines
Quoted 15 lines
mm/hugetlb.c +14
-0+static int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +int i; +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return 0 ; + +vmemmap_pgtable_init (page); + +for (i =0 ; i < nr; i++) { +pte_t *pte_p; +mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1388
Quoted 10 lines
mm/hugetlb.c +10
-0+ pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +vmemmap_pgtable_deposit (page,virt_to_page (pte_p)); + } + +return 0 ; +out: +while (i-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable));would not be enough to:
Quoted 5 lines
Quoted 2 lines
while (pgtable =
vmemmap_pgtable_withdrag(page))
pte_free_kernel(&init_mm, page_to_virt(pgtable));The
vmemmap_pgtable_withdrawcan not return NULL. So we can not drop the "i--".
Yeah, you are right, I managed to confuse myself.
But why not make it return null, something like:
static pgtable_t vmemmap_pgtable_withdraw(struct page *page)
{
pgtable_t pgtable;
/* FIFO */
pgtable = page_huge_pte(page);
page_huge_pte(page) = list_first_entry_or_null(&pgtable->lru,
struct page, lru);
if (page_huge_pte(page))
list_del(&pgtable->lru);
return page_huge_pte(page) ? pgtable : NULL;
}What do you think?
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 53 lines · Oscar Salvador
Quoted 42 lines · Muchun Song
Quoted 10 lines · Oscar Salvador
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1329
Quoted 7 lines · Muchun Song
mm/hugetlb.c +7
-0+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 tabels when there is no vmemmap pages +* to free. s /tabels/tables/
Thanks.
Quoted 28 lines
Quoted 15 lines
mm/hugetlb.c +14
-0+static int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +int i; +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return 0 ; + +vmemmap_pgtable_init (page); + +for (i =0 ; i < nr; i++) { +pte_t *pte_p; +mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1388
Quoted 10 lines
mm/hugetlb.c +10
-0+ pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +vmemmap_pgtable_deposit (page,virt_to_page (pte_p)); + } + +return 0 ; +out: +while (i-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable));would not be enough to:
Quoted 5 lines
Quoted 2 lines
while (pgtable =
vmemmap_pgtable_withdrag(page))
pte_free_kernel(&init_mm, page_to_virt(pgtable));The
vmemmap_pgtable_withdrawcan not return NULL. So we can not drop the "i--".Yeah, you are right, I managed to confuse myself.
But why not make it return null, something like:
Quoted 6 lines
static
pgtable_tvmemmap_pgtable_withdraw(struct page *page){ pgtable_t pgtable; /* FIFO */ pgtable = page_huge_pte(page);
The check should be added here.
if (!pgtable)
return NULL;Just like my previous v2 patch does. In this case, we can drop those checks. What do you think?
Quoted 14 lines
page_huge_pte(page) = list_first_entry_or_null(&pgtable->lru, struct page, lru); if (page_huge_pte(page)) list_del(&pgtable->lru); return page_huge_pte(page) ? pgtable : NULL; }What do you think?
--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun
Oscar Salvador
maintainerRe: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 7 lines · Muchun Song
The check should be added here.
if (!pgtable) return NULL;Just like my previous v2 patch does. In this case, we can drop those checks. What do you think?
It is too early for me, so bear with me.
page_huge_pte will only return NULL in case we did not get to preallocate any pgtable right?
What I was talimg about is that
Quoted 3 lines
Quoted 3 lines
page_huge_pte(page) = list_first_entry_or_null(&pgtable->lru, struct page, lru);
here we will get the either a pgtable entry or NULL in case we already consumed all entries from the list.
If that is the case, we can return NULL and let the caller known that we are done.
Am I missing anything?
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 7 lines · Oscar Salvador
Quoted 5 lines · Muchun Song
The check should be added here.
if (!pgtable) return NULL;
Quoted 7 lines
Quoted 2 lines
Just like my previous v2 patch does. In this case, we can drop those checks. What do you think?
It is too early for me, so bear with me.
page_huge_ptewill only return NULL in case we did not get to preallocate any pgtable right?
The page_huge_pte only returns NULL when we did consume the page tables. Not each HugeTLB page need to split the vmemmap page tables. We preallocate page tables for each HugeTLB page, if we do not need to split PMD. We should free the preallocated page tables.
Maybe you can see the comments of the other thread.
[PATCH v3 09/21] mm/hugetlb: Free the vmemmap pages associated with
each hugetlb page
Thanks.
Quoted 11 lines
What I was talimg about is that
Quoted 3 lines
Quoted 3 lines
page_huge_pte(page) = list_first_entry_or_null(&pgtable->lru, struct page, lru);here we will get the either a pgtable entry or NULL in case we already consumed all entries from the list.
If that is the case, we can return NULL and let the caller known that we are done.
Quoted 8 lines
Am I missing anything?
--
Oscar Salvador
SUSE L3
--
Yours,
Muchun
Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
In reply to Muchun SongQuoted 10 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 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.c +0
-0@@ -1303,6 +1303,108 @@ static inline void destroy_compound_gigantic_page(struct page *page,
...
Quoted 60 lines
mm/hugetlb.c +60
-0+static inline void vmemmap_pgtable_init (struct page *page) +{ +page_huge_pte (page) =NULL ; +} + +static void vmemmap_pgtable_deposit (struct page *page,pgtable_t pgtable) +{ +/* FIFO */ +if (!page_huge_pte (page)) +INIT_LIST_HEAD (&pgtable->lru); +else +list_add (&pgtable->lru, &page_huge_pte (page)->lru); +page_huge_pte (page) = pgtable; +} + +static pgtable_t vmemmap_pgtable_withdraw (struct page *page) +{ +pgtable_t pgtable; + +/* FIFO */ + pgtable =page_huge_pte (page); +page_huge_pte (page) =list_first_entry_or_null (&pgtable->lru, + struct page, lru); +if (page_huge_pte (page)) +list_del (&pgtable->lru); +return pgtable; +} + +static int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +int i; +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return 0 ; + +vmemmap_pgtable_init (page); + +for (i =0 ; i < nr; i++) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +vmemmap_pgtable_deposit (page,virt_to_page (pte_p)); + } + +return 0 ; +out: +while (i-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable)); +return -ENOMEM ; +} + +static void vmemmap_pgtable_free (struct hstate *h,struct page *page) +{ +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); +
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1406
Quoted 10 lines
mm/hugetlb.c +10
-0+if (!nr) +return ; + + pgtable =page_huge_pte (page); +if (!pgtable) +return ; + +while (nr-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable)); +}
I may be confused.
In patch 9 of this series, the first call to vmemmap_pgtable_free() is made:
mm/hugetlb.c void free_huge_page(struct page *page) · line 1808
Quoted 11 lines
mm/hugetlb.c +4
-0@@ -1645,6 +1799,10 @@ 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); +/* Must be called before the initialization of @page->lru */ +vmemmap_pgtable_free (h, page); +INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );set_hugetlb_cgroup (page,NULL );
When I saw that comment in previous patch series, I assumed page->lru was being used to store preallocated pages and pages to free. However, unless I am reading the code incorrectly it does not appear page->lru (of the huge page) is being used for this purpose. Is that correct?
If it is correct, would using page->lru of the huge page make this code simpler? I am just missing the reason why you are using page_huge_pte(page)->lru
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 90 lines · Mike Kravetz
Quoted 10 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 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.c +0
-0@@ -1303,6 +1303,108 @@ static inline void destroy_compound_gigantic_page(struct page *page,...
Quoted 60 lines
mm/hugetlb.c +60
-0+static inline void vmemmap_pgtable_init (struct page *page) +{ +page_huge_pte (page) =NULL ; +} + +static void vmemmap_pgtable_deposit (struct page *page,pgtable_t pgtable) +{ +/* FIFO */ +if (!page_huge_pte (page)) +INIT_LIST_HEAD (&pgtable->lru); +else +list_add (&pgtable->lru, &page_huge_pte (page)->lru); +page_huge_pte (page) = pgtable; +} + +static pgtable_t vmemmap_pgtable_withdraw (struct page *page) +{ +pgtable_t pgtable; + +/* FIFO */ + pgtable =page_huge_pte (page); +page_huge_pte (page) =list_first_entry_or_null (&pgtable->lru, + struct page, lru); +if (page_huge_pte (page)) +list_del (&pgtable->lru); +return pgtable; +} + +static int vmemmap_pgtable_prealloc (struct hstate *h,struct page *page) +{ +int i; +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +if (!nr) +return 0 ; + +vmemmap_pgtable_init (page); + +for (i =0 ; i < nr; i++) { +pte_t *pte_p; + + pte_p =pte_alloc_one_kernel (&init_mm); +if (!pte_p) + goto out; +vmemmap_pgtable_deposit (page,virt_to_page (pte_p)); + } + +return 0 ; +out: +while (i-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable)); +return -ENOMEM ; +} + +static void vmemmap_pgtable_free (struct hstate *h,struct page *page) +{ +pgtable_t pgtable; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); +mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1406
Quoted 10 lines
mm/hugetlb.c +10
-0+if (!nr) +return ; + + pgtable =page_huge_pte (page); +if (!pgtable) +return ; + +while (nr-- && (pgtable =vmemmap_pgtable_withdraw (page))) +pte_free_kernel (&init_mm,page_to_virt (pgtable)); +}I may be confused.
In patch 9 of this series, the first call to
vmemmap_pgtable_free()is made:Quoted 2 lines
mm/hugetlb.c +0
-0@@ -1645,6 +1799,10 @@ void free_huge_page(struct page *page)
Quoted 12 lines
mm/hugetlb.c void free_huge_page(struct page *page) · line 1808
Quoted 9 lines
mm/hugetlb.c +4
-0static void prep_new_huge_page (struct hstate *h,struct page *page,int nid) { +free_huge_page_vmemmap (h, page); +/* Must be called before the initialization of @page->lru */ +vmemmap_pgtable_free (h, page); +INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );set_hugetlb_cgroup (page,NULL );When I saw that comment in previous patch series, I assumed
page->lruwas being used to store preallocated pages and pages to free. However, unless
Yeah, you are right.
Quoted 6 lines
I am reading the code incorrectly it does not appear
page->lru(of the huge page) is being used for this purpose. Is that correct?If it is correct, would using
page->lruof the huge page make this code simpler? I am just missing the reason why you are using page_huge_pte(page)->lru
For 1GB HugeTLB pages, we should pre-allocate more than one page table. So I use a linked list. The page_huge_pte(page) is the list head. Because the page->lru shares storage with page->pmd_huge_pte.
+ /* Must be called before the initialization of @page->lru */
+ vmemmap_pgtable_free(h, page);
+
INIT_LIST_HEAD(&page->lru);
Here we initialize the lru. So the vmemmap_pgtable_free should be called before this. It seems like that I should point out this "share" in the comment.
Thanks.
Quoted 3 lines
--
Mike Kravetz
Signature · 2 lines
Yours, Muchun
Re: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 6 lines · Muchun Song
Quoted 5 lines · Mike Kravetz
On 11/8/20 6:10 AM, Muchun Song wrote:
I am reading the code incorrectly it does not appearpage->lru(of the huge page) is being used for this purpose. Is that correct?
Quoted 7 lines
Quoted 3 lines
If it is correct, would using
page->lruof the huge page make this code simpler? I am just missing the reason why you are using page_huge_pte(page)->lruFor 1GB HugeTLB pages, we should pre-allocate more than one page table. So I use a linked list. The
page_huge_pte(page)is the list head. Because thepage->lrushares storage withpage->pmd_huge_pte.
Sorry, but I do not understand the statement page->lru shares storage with page->pmd_huge_pte. Are you saying they are both in head struct page of the huge page?
Here is what I was suggesting. If we just use page->lru for the list then vmemmap_pgtable_prealloc() could be coded like the following:
static int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page)
{
struct page *pte_page, *t_page;
unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h);
if (!nr)
return 0;
/* 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:
list_for_each_entry_safe(pte_page, t_page, &page->lru, lru)
pte_free_kernel(&init_mm, page_to_virt(pte_page));
return -ENOMEM;
}By doing this we could eliminate the routines,vmemmap_pgtable_init()vmemmap_pgtable_deposit()vmemmap_pgtable_withdraw()
and simply use the list manipulation routines.
To me, that looks simpler than the proposed code in this patch.
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
Quoted 37 lines · Mike Kravetz
Quoted 6 lines · Muchun Song
Quoted 5 lines · Mike Kravetz
On 11/8/20 6:10 AM, Muchun Song wrote:
I am reading the code incorrectly it does not appearpage->lru(of the huge page) is being used for this purpose. Is that correct?Quoted 7 lines
Quoted 3 lines
If it is correct, would using
page->lruof the huge page make this code simpler? I am just missing the reason why you are using page_huge_pte(page)->lruFor 1GB HugeTLB pages, we should pre-allocate more than one page table. So I use a linked list. The
page_huge_pte(page)is the list head. Because thepage->lrushares storage withpage->pmd_huge_pte.Sorry, but I do not understand the statement
page->lrushares storage withpage->pmd_huge_pte. Are you saying they are both in head struct page of the huge page?Here is what I was suggesting. If we just use
page->lrufor the list thenvmemmap_pgtable_prealloc()could be coded like the following:static int
vmemmap_pgtable_prealloc(struct hstate *h, struct page *page){ struct page *pte_page, *t_page; unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h); if (!nr) return 0; /* 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
if (!pte_p) goto out; list_add(&virt_to_page(pte_p)->lru, &page->lru); } return 0; out: list_for_each_entry_safe(pte_page, t_page, &page->lru, lru)
Forgot the list_del(&pte_page->lru)
Perhaps it is not simpler after all. :)
Signature · 1 line
Mike Kravetz
Quoted 12 lines
pte_free_kernel(&init_mm, page_to_virt(pte_page)); return -ENOMEM; }By doing this we could eliminate the routines,
vmemmap_pgtable_init()vmemmap_pgtable_deposit()vmemmap_pgtable_withdraw()
and simply use the list manipulation routines.To me, that looks simpler than the proposed code in this patch.
Re: [External] Re: [PATCH v3 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers
In reply to Mike KravetzQuoted 50 lines · Mike Kravetz
Quoted 6 lines · Muchun Song
Quoted 5 lines · Mike Kravetz
On 11/8/20 6:10 AM, Muchun Song wrote:
I am reading the code incorrectly it does not appearpage->lru(of the huge page) is being used for this purpose. Is that correct?Quoted 7 lines
Quoted 3 lines
If it is correct, would using
page->lruof the huge page make this code simpler? I am just missing the reason why you are using page_huge_pte(page)->lruFor 1GB HugeTLB pages, we should pre-allocate more than one page table. So I use a linked list. The
page_huge_pte(page)is the list head. Because thepage->lrushares storage withpage->pmd_huge_pte.Sorry, but I do not understand the statement
page->lrushares storage withpage->pmd_huge_pte. Are you saying they are both in head struct page of the huge page?Here is what I was suggesting. If we just use
page->lrufor the list thenvmemmap_pgtable_prealloc()could be coded like the following:static int
vmemmap_pgtable_prealloc(struct hstate *h, struct page *page){ struct page *pte_page, *t_page; unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h); if (!nr) return 0; /* 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: list_for_each_entry_safe(pte_page, t_page, &page->lru, lru) pte_free_kernel(&init_mm, page_to_virt(pte_page)); return -ENOMEM; }
Quoted 5 lines
By doing this we could eliminate the routines,
vmemmap_pgtable_init()vmemmap_pgtable_deposit()vmemmap_pgtable_withdraw()
and simply use the list manipulation routines.
Now I know what you mean. Yeah, just use page->lru can make code simply. Thanks for your suggestions.
Quoted 4 lines
To me, that looks simpler than the proposed code in this patch. --
Mike Kravetz
Signature · 2 lines
Yours, Muchun
PATCH v3 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 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
include/linux/bootmem_info.h +25 -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,30 @@ 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 ); +__SetPageReserved (page); +adjust_managed_page_count (page,-1 ); +}#else static inline void register_page_bootmem_info_node (struct pglist_data *pgdat) {
Signature · 1 line
2.11.0
PATCH v3 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.
--- include/linux/bootmem_info.h | 18 ++++++++++++++++-- mm/bootmem_info.c | 12 ++++++------ mm/sparse.c | 4 ++-- 3 files changed, 24 insertions(+), 10 deletions(-)
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 4 +#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 v3 8/21 mm/vmemmap: Initialize page table lock for vmemmap
In reply to Muchun SongIn the register_page_bootmem_memmap, the slab allocator is not ready yet. So when ALLOC_SPLIT_PTLOCKS, we use init_mm.page_table_lock. otherwise we use per page table lock(page->ptl). In the later patch, we will use the vmemmap page table lock to guard the splitting of the vmemmap huge PMD.
--- arch/x86/mm/init_64.c | 2 ++ include/linux/mm.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
arch/x86/mm/init_64.c +2 -0
@@ -1610,6 +1610,8 @@ void register_page_bootmem_memmap(unsigned long section_nr, }get_page_bootmem (section_nr,pud_page (*pud),MIX_SECTION_INFO ); +vmemmap_ptlock_init (pud_page (*pud)); +if (!boot_cpu_has (X86_FEATURE_PSE )) { next = (addr +PAGE_SIZE ) &PAGE_MASK ; pmd =pmd_offset (pud, addr);
include/linux/mm.h +45 -0
75 lines not on this page · raw message
Signature · 1 line
2.11.0
Oscar Salvador
maintainerRe: [PATCH v3 08/21] mm/vmemmap: Initialize page table lock for vmemmap
Quoted 5 lines · Muchun Song
In the
register_page_bootmem_memmap, the slab allocator is not ready yet. So whenALLOC_SPLIT_PTLOCKS, we use init_mm.page_table_lock. otherwise we use per page tablelock(page->ptl). In the later patch, we will use the vmemmap page table lock to guard the splitting of the vmemmap huge PMD.
I am not sure about this one.
Grabbing init_mm's pagetable lock for specific hugetlb operations does not seem like a good idea, and we do not know how contented is that one.
I think a better fit would be to find another hook to initialize page_table_lock at a later stage.
Anyway, we do not need till we are going to perform an operation on the range, right?
Unless I am missing something, this should be doable in hugetlb_init.
hugetlb_init is part from a init_call that gets called during do_initcalls. At this time, slab is fully operative.
start_kernel
kmem_cache_init_late
kmem_cache_init_late
...
arch_call_rest_init
rest_init
kernel_init_freeable
do_basic_setup
do_initcalls
hugetlb_init
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 08/21] mm/vmemmap: Initialize page table lock for vmemmap
Quoted 11 lines · Oscar Salvador
Quoted 5 lines · Muchun Song
In the
register_page_bootmem_memmap, the slab allocator is not ready yet. So whenALLOC_SPLIT_PTLOCKS, we use init_mm.page_table_lock. otherwise we use per page tablelock(page->ptl). In the later patch, we will use the vmemmap page table lock to guard the splitting of the vmemmap huge PMD.I am not sure about this one.
Grabbing init_mm's pagetable lock for specific hugetlb operations does not seem like a good idea, and we do not know how contented is that one.
These APIs are used to guard the operations on vmemmap page tables. For now, it is only for specific hugetlb operations. But maybe in the future, someone also wants to modify the vmemmap page tables, he also can use these APIs. Yeah, we do not know how contented is init_mm's pagetable lock. Grabbing this one may not be a good idea.
Quoted 5 lines
I think a better fit would be to find another hook to initialize
page_table_lockat a later stage.
Anyway, we do not need till we are going to perform an operation on the range, right?
Yeah. You are right.
Quoted 5 lines
Unless I am missing something, this should be doable in
hugetlb_init.
hugetlb_initis part from ainit_callthat gets called duringdo_initcalls. At this time, slab is fully operative.
If we initialize the page_table_lock in the hugetlb_init, we need to walk the vmemmap page tables again. But the vmemmap pages size is small, maybe the overhead of this is also small. And doing this in hugetlb_init can make the code cleaner. Thanks very much.
Quoted 12 lines
start_kernel
kmem_cache_init_late
kmem_cache_init_late
...
arch_call_rest_init
rest_init
kernel_init_freeable
do_basic_setup
do_initcalls
hugetlb_init
Quoted 3 lines
--
Oscar Salvador
SUSE L3
--
Yours,
Muchun
PATCH v3 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 ++ include/linux/hugetlb.h | 8 ++ include/linux/mm.h | 4 + mm/hugetlb.c | 166 ++++++++++++++++++++++++++++++++ mm/sparse-vmemmap.c | 31 ++++++ 6 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)
include/linux/hugetlb.h +8 -0
@@ -594,6 +594,14 @@ static inline unsigned int blocks_per_huge_page(struct hstate *h)#include <asm/hugetlb.h> #ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +#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 +#ifndef VMEMMAP_HPAGE_SHIFT #define VMEMMAP_HPAGE_SHIFT HPAGE_SHIFT#endif
include/linux/mm.h +4 -0
@@ -3025,6 +3025,10 @@ static inline void print_vma_addr(char *prefix, unsigned long rip) }#endif +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +pmd_t *vmemmap_to_pmd (const void *page); +#endif +void *sparse_buffer_alloc (unsigned long size);struct page *__populate_section_memmap (unsigned long pfn,unsigned long nr_pages,int nid,struct vmem_altmap *altmap);
mm/hugetlb.c +166 -0
202 lines not on this page · raw message
mm/sparse-vmemmap.c +31 -0
@@ -263,3 +263,34 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,return pfn_to_page (pfn); } + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +/* +* Walk a vmemmap address to the pmd it maps. +*/ +pmd_t *vmemmap_to_pmd (const void *page) +{ +unsigned long addr = (unsigned long )page; +pgd_t *pgd; +p4d_t *p4d; +pud_t *pud; +pmd_t *pmd; + +if (addr <VMEMMAP_START || addr >=VMEMMAP_END ) +return NULL ; + + pgd =pgd_offset_k (addr); +if (pgd_none (*pgd)) +return NULL ; + p4d =p4d_offset (pgd, addr); +if (p4d_none (*p4d)) +return NULL ; + pud =pud_offset (p4d, addr); + +if (pud_none (*pud) ||pud_bad (*pud)) +return NULL ; + pmd =pmd_offset (pud, addr); + +return pmd; +} +#endif
Signature · 1 line
2.11.0
Oscar Salvador
maintainerRe: [PATCH v3 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 4 lines · Muchun Song
mm/hugetlb.c +4
-0+static inline int freed_vmemmap_hpage (struct page *page) +{ +return atomic_read (&page->_mapcount) +1 ; +}
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1450
Quoted 10 lines
mm/hugetlb.c +10
-0+ +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 ; +}
Are these relaxed any different that the normal ones on x86_64? I got confused following the macros.
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1471
Quoted 10 lines
mm/hugetlb.c +10
-0+static void __free_huge_page_pte_vmemmap (struct page *reuse,pte_t *ptep, +unsigned long start, +unsigned int nr_free, +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; +unsigned long end = start + (nr_free <<PAGE_SHIFT );
See below.
Quoted 7 lines
mm/hugetlb.c +7
-0+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_NR *PAGE_SIZE ; +unsigned long end = addr +vmemmap_pages_size_per_hpage (h);
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1501
Quoted 10 lines
mm/hugetlb.c +10
-0+struct page *reuse =NULL ; + + addr = start; + do { + unsigned int nr_pages; + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[-1]);
Can we define a proper name for that instead of -1?
e.g: TAIL_PAGE_REUSE or something like that.
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1506
Quoted 5 lines
mm/hugetlb.c +5
-0+ + next =vmemmap_hpage_addr_end (addr, end); + nr_pages = (next - addr) >>PAGE_SHIFT ; +__free_huge_page_pte_vmemmap (reuse, ptep, addr, nr_pages, + free_pages);
Why not passing next instead of nr_pages? I think it makes more sense. As a bonus we can kill the variable.
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1546
Quoted 9 lines
mm/hugetlb.c +9
-0+static void split_vmemmap_huge_page (struct hstate *h,struct page *head, +pmd_t *pmd) +{ +pgtable_t pgtable; +unsigned long start = (unsigned long )head &VMEMMAP_HPAGE_MASK ; +unsigned long addr = start; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +while (nr-- && (pgtable =vmemmap_pgtable_withdraw (head))) {
The same with previous patches, I would scrap "nr" and its use.
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1547
Quoted 1 line
mm/hugetlb.c +1
-0+VM_BUG_ON (freed_vmemmap_hpage (pgtable));
I guess here we want to check whether we already call free_huge_page_vmemmap on this range?
For this to have happened, the locking should have failed, right?
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1567
Quoted 13 lines
mm/hugetlb.c +13
-0+static 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 (head); + ptl =vmemmap_pmd_lock (pmd); +if (vmemmap_pmd_huge (pmd)) { +VM_BUG_ON (!pgtable_pages_to_prealloc_per_hpage (h));
I think that checking for free_vmemmap_pages_per_hpage is enough.
In the end, pgtable_pages_to_prealloc_per_hpage uses free_vmemmap_pages_per_hpage.
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 11 lines · Oscar Salvador
Quoted 9 lines · Muchun Song
mm/hugetlb.c +9
-0+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 ; +}
Quoted 8 lines
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1450
Quoted 5 lines
mm/hugetlb.c +5
-0+ +static inline int freed_vmemmap_hpage_dec (struct page *page) +{ +return atomic_dec_return_relaxed (&page->_mapcount) +1 ; +}Are these relaxed any different that the normal ones on
x86_64? I got confused following the macros.
A PTE table can contain 64 HugeTLB(2MB) page's struct page structures. So I use the freed_vmemmap_hpage to indicate how many HugeTLB pages that it's vmemmap pages are already freed to buddy.
Once vmemmap pages of a HugeTLB page are freed, we call the freed_vmemmap_hpage_inc, when freeing a HugeTLB to the buddy, we should call freed_vmemmap_hpage_dec.
If the freed_vmemmap_hpage hit zero when free HugeTLB, we try to merge the PTE table to PMD(now only support gigantic pages). This can refer to
[PATCH v3 19/21] mm/hugetlb: Merge pte to huge pmd only for gigantic
Thanks.
Quoted 34 lines
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1471
Quoted 11 lines
mm/hugetlb.c +10
-0+static void __free_huge_page_pte_vmemmap (struct page *reuse,pte_t *ptep, +unsigned long start, +unsigned int nr_free, +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; +unsigned long end = start + (nr_free <<PAGE_SHIFT );See below.
Quoted 7 lines
mm/hugetlb.c +7
-0+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_NR *PAGE_SIZE ; +unsigned long end = addr +vmemmap_pages_size_per_hpage (h);mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1501
Quoted 10 lines
mm/hugetlb.c +10
-0+struct page *reuse =NULL ; + + addr = start; + do { + unsigned int nr_pages; + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[-1]);Can we define a proper name for that instead of -1?
Quoted 1 line
e.g:
TAIL_PAGE_REUSEor something like that.
OK, will do.
Quoted 9 lines
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1506
Quoted 6 lines
mm/hugetlb.c +5
-0+ + next =vmemmap_hpage_addr_end (addr, end); + nr_pages = (next - addr) >>PAGE_SHIFT ; +__free_huge_page_pte_vmemmap (reuse, ptep, addr, nr_pages, + free_pages);Why not passing next instead of
nr_pages? I think it makes more sense. As a bonus we can kill the variable.
Good catch. We can pass next instead of nr_pages. Thanks.
Quoted 13 lines
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1546
Quoted 10 lines
mm/hugetlb.c +9
-0+static void split_vmemmap_huge_page (struct hstate *h,struct page *head, +pmd_t *pmd) +{ +pgtable_t pgtable; +unsigned long start = (unsigned long )head &VMEMMAP_HPAGE_MASK ; +unsigned long addr = start; +unsigned int nr =pgtable_pages_to_prealloc_per_hpage (h); + +while (nr-- && (pgtable =vmemmap_pgtable_withdraw (head))) {The same with previous patches, I would scrap "nr" and its use.
Quoted 5 lines
Quoted 1 line
+ VM_BUG_ON(freed_vmemmap_hpage(pgtable));
I guess here we want to check whether we already call
free_huge_page_vmemmapon this range?
For this to have happened, the locking should have failed, right?
Only the first HugeTLB page should split the PMD to PTE. The other 63 HugeTLB pages
do not need to split. Here I want to make sure we are the first.
Quoted 17 lines
Quoted 4 lines
mm/hugetlb.c +3
-0+static void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +pmd_t *pmd;mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1567
Quoted 10 lines
mm/hugetlb.c +10
-0+spinlock_t *ptl; +LIST_HEAD (free_pages); + +if (!free_vmemmap_pages_per_hpage (h)) +return ; + + pmd =vmemmap_to_pmd (head); + ptl =vmemmap_pmd_lock (pmd); +if (vmemmap_pmd_huge (pmd)) { +VM_BUG_ON (!pgtable_pages_to_prealloc_per_hpage (h));I think that checking for
free_vmemmap_pages_per_hpageis enough.
In the end,pgtable_pages_to_prealloc_per_hpageusesfree_vmemmap_pages_per_hpage.
The free_vmemmap_pages_per_hpage is not enough. See the comments above.
Thanks.
Quoted 5 lines
--
Oscar Salvador
SUSE L3
--
Yours,
Muchun
Oscar Salvador
maintainerRe: [External] Re: [PATCH v3 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 3 lines · Muchun Song
Only the first HugeTLB page should split the PMD to PTE. The other 63 HugeTLB pages
do not need to split. Here I want to make sure we are the first.
I think terminology is loosing me here.
Say you allocate a 2MB HugeTLB page at ffffea0004100000.
The vmemmap range that the represents this is ffffea0004000000 - ffffea0004200000. That is a 2MB chunk PMD-mapped.
So, in order to free some of those vmemmap pages, we need to break down that area, remapping it to PTE-based.
I know what you mean, but we are not really splitting hugetlg pages, but the memmap range they are represented with.
About:
"Only the first HugeTLB page should split the PMD to PTE. The other 63 HugeTLB pages
do not need to split. Here I want to make sure we are the first."
That only refers to gigantic pages, right?
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
mm/hugetlb.c +8
-0+static 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 ;Quoted 8 lines
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1567
Quoted 5 lines
mm/hugetlb.c +5
-0+ + pmd =vmemmap_to_pmd (head); + ptl =vmemmap_pmd_lock (pmd); +if (vmemmap_pmd_huge (pmd)) { +VM_BUG_ON (!pgtable_pages_to_prealloc_per_hpage (h));I think that checking for
free_vmemmap_pages_per_hpageis enough.
In the end,pgtable_pages_to_prealloc_per_hpageusesfree_vmemmap_pages_per_hpage.
Quoted 1 line
The
free_vmemmap_pages_per_hpageis not enough. See the comments above.
My comment was about the VM_BUG_ON.
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 8 lines · Oscar Salvador
Quoted 3 lines · Muchun Song
Only the first HugeTLB page should split the PMD to PTE. The other 63 HugeTLB pages
do not need to split. Here I want to make sure we are the first.I think terminology is loosing me here.
Quoted 8 lines
Say you allocate a 2MB HugeTLB page at
ffffea0004100000.The vmemmap range that the represents this is
ffffea0004000000-ffffea0004200000. That is a 2MB chunk PMD-mapped.
So, in order to free some of those vmemmap pages, we need to break down that area, remapping it to PTE-based.
I know what you mean, but we are not really splitting hugetlg pages, but the memmap range they are represented with.
Yeah, you are right. We are splitting the vmemmap instead of hugetlb. Sorry for the confusion.
Quoted 8 lines
About:
"Only the first HugeTLB page should split the PMD to PTE. The other 63 HugeTLB pages
do not need to split. Here I want to make sure we are the first."That only refers to gigantic pages, right?
Yeah, now it only refers to gigantic pages. Originally, I also wanted to merge vmemmap PTE to PMD for normal 2MB HugeTLB pages. So I introduced those macros(e.g. freed_vmemmap_hpage). For 2MB HugeTLB pages, I haven't found an elegant solution. Hopefully, when you or someone have read all of the patch series, we can come up with an elegant solution to merge PTE.
Thanks.
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
mm/hugetlb.c +3
-0+static void free_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +pmd_t *pmd;mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1567
Quoted 10 lines
mm/hugetlb.c +10
-0+spinlock_t *ptl; +LIST_HEAD (free_pages); + +if (!free_vmemmap_pages_per_hpage (h)) +return ; + + pmd =vmemmap_to_pmd (head); + ptl =vmemmap_pmd_lock (pmd); +if (vmemmap_pmd_huge (pmd)) { +VM_BUG_ON (!pgtable_pages_to_prealloc_per_hpage (h));I think that checking for
free_vmemmap_pages_per_hpageis enough.
In the end,pgtable_pages_to_prealloc_per_hpageusesfree_vmemmap_pages_per_hpage.
Quoted 3 lines
Quoted 1 line
The
free_vmemmap_pages_per_hpageis not enough. See the comments above.My comment was about the
VM_BUG_ON.
Sorry, yeah, we can drop it. Thanks.
Quoted 5 lines
--
Oscar Salvador
SUSE L3
Signature · 2 lines
Yours, Muchun
Oscar Salvador
maintainerRe: [External] Re: [PATCH v3 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 8 lines · Muchun Song
Quoted 1 line
That only refers to gigantic pages, right?
Yeah, now it only refers to gigantic pages. Originally, I also wanted to merge vmemmap PTE to PMD for normal 2MB HugeTLB pages. So I introduced those macros(e.g.
freed_vmemmap_hpage). For 2MB HugeTLB pages, I haven't found an elegant solution. Hopefully, when you or someone have read all of the patch series, we can come up with an elegant solution to merge PTE.
Well, it is quite a lot of "tricky" code, so it takes some time.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1565
Quoted 11 lines
mm/hugetlb.c +11
-0+static 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 (head); + ptl =vmemmap_pmd_lock (pmd);
I forgot about this one.
You might want to check whether vmemmap_to_pmd returns NULL or not. If it does means that something went wrong anyways, but still we should handle such case (and print a fat warning or something like that).
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [External] Re: [PATCH v3 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page
Quoted 13 lines · Oscar Salvador
Quoted 8 lines · Muchun Song
Quoted 1 line
That only refers to gigantic pages, right?
Yeah, now it only refers to gigantic pages. Originally, I also wanted to merge vmemmap PTE to PMD for normal 2MB HugeTLB pages. So I introduced those macros(e.g.
freed_vmemmap_hpage). For 2MB HugeTLB pages, I haven't found an elegant solution. Hopefully, when you or someone have read all of the patch series, we can come up with an elegant solution to merge PTE.Well, it is quite a lot of "tricky" code, so it takes some time.
Quoted 16 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
mm/hugetlb.c static void __init hugetlb_vmemmap_init(struct hstate *h) · line 1565
Quoted 11 lines
mm/hugetlb.c +11
-0+static 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 (head); + ptl =vmemmap_pmd_lock (pmd);I forgot about this one.
You might want to check whethervmemmap_to_pmdreturns NULL or not. If it does means that something went wrong anyways, but still we should handle such case (and print a fat warning or something like that).
Yeah, maybe add a BUG_ON or WARN_ON. Do you think which one would be more suitable?
Quoted 5 lines
--
Oscar Salvador
SUSE L3
--
Yours,
Muchun
PATCH v3 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 | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 12 deletions(-)
mm/hugetlb.c +89 -12
@@ -1220,7 +1220,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,
@@ -1287,11 +1287,14 @@ 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 __free_hugepage (struct hstate *h,struct page *page); +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP #include <linux/bootmem_info.h>
@@ -1574,6 +1577,64 @@ static void free_huge_page_vmemmap(struct hstate *h, struct page *head) free_vmemmap_page_list(&free_pages); } + +/* +* 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) +{ +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); +} + +static inline void free_gigantic_page (struct hstate *h,struct page *page) +{ +__free_gigantic_page (page,huge_page_order (h)); +}#else static inline void hugetlb_vmemmap_init (struct hstate *h) {
@@ -1591,17 +1652,39 @@ static inline void vmemmap_pgtable_free(struct hstate *h, struct page *page)static inline void free_huge_page_vmemmap (struct hstate *h,struct page *head) { } + +static inline void __update_and_free_page (struct hstate *h,struct page *page) +{ +__free_hugepage (h, page); +} + +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) { -int i; -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 |
@@ -1613,14 +1696,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)); }
@@ -2057,7 +2134,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 ;
Signature · 1 line
2.11.0
PATCH v3 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 | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-)
mm/hugetlb.c +109 -1
@@ -1307,6 +1307,8 @@ static void __free_hugepage(struct hstate *h, struct page *page);* reserve at least 2 pages as vmemmap areas. */ #define RESERVE_VMEMMAP_NR 2U +#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) +#define GFP_VMEMMAP_PAGE (GFP_KERNEL | __GFP_NOFAIL | __GFP_MEMALLOC)#define page_huge_pte (page) ((page)->pmd_huge_pte)
@@ -1490,7 +1492,7 @@ static void __free_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd, struct list_head *free_pages) {unsigned long next; -unsigned long start = addr +RESERVE_VMEMMAP_NR *PAGE_SIZE ; +unsigned long start = addr +RESERVE_VMEMMAP_SIZE ;unsigned long end = addr +vmemmap_pages_size_per_hpage (h);struct page *reuse =NULL ;
@@ -1578,6 +1580,106 @@ static void free_huge_page_vmemmap(struct hstate *h, struct page *head)free_vmemmap_page_list (&free_pages); } +static void __remap_huge_page_pte_vmemmap (struct page *reuse,pte_t *ptep, +unsigned long start, +unsigned int nr_remap, +struct list_head *remap_pages) +{ +pgprot_t pgprot =PAGE_KERNEL ; +void *from = (void *)page_private (reuse); +unsigned long addr, end = start + (nr_remap <<PAGE_SHIFT ); + +for (addr = start; addr < end; addr +=PAGE_SIZE ) { +void *to; +struct page *page; +pte_t entry, old = *ptep; + + page = list_first_entry_or_null(remap_pages, struct page, lru); +list_del (&page->lru); + to =page_to_virt (page); +copy_page (to, from); + +/* +* Make sure that any data that writes to the @to is made +* visible to the physical page. +*/ +flush_kernel_vmap_range (to,PAGE_SIZE ); + +prepare_vmemmap_page (page); + + entry =mk_pte (page, pgprot); +set_pte_at (&init_mm, addr, ptep++, entry); + +VM_BUG_ON (!pte_present (old) ||pte_page (old) != reuse); + } +} + +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_NR *PAGE_SIZE ; +unsigned long end = addr +vmemmap_pages_size_per_hpage (h); +struct page *reuse =NULL ; + + addr = start; +do { +unsigned int nr_pages; +pte_t *ptep; + + ptep =pte_offset_kernel (pmd, addr); +if (!reuse) { + reuse =pte_page (ptep[-1 ]); +set_page_private (reuse, addr -PAGE_SIZE ); + } + + next =vmemmap_hpage_addr_end (addr, end); + nr_pages = (next - addr) >>PAGE_SHIFT ; +__remap_huge_page_pte_vmemmap (reuse, ptep, addr, nr_pages, + 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; + +for (i =0 ; i <free_vmemmap_pages_per_hpage (h); i++) { +struct page *page; + +/* This should not fail */ + page =alloc_page (GFP_VMEMMAP_PAGE ); +list_add_tail (&page->lru, list); + } +} + +static void alloc_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +pmd_t *pmd; +spinlock_t *ptl; +LIST_HEAD (remap_pages); + +if (!free_vmemmap_pages_per_hpage (h)) +return ; + +alloc_vmemmap_pages (h, &remap_pages); + + pmd =vmemmap_to_pmd (head); + ptl =vmemmap_pmd_lock (pmd); +__remap_huge_page_pmd_vmemmap (h, pmd, (unsigned long )head, + &remap_pages); +if (!freed_vmemmap_hpage_dec (pmd_page (*pmd))) { +/* +* Todo: +* Merge pte to huge pmd if it has ever been split. +*/ + } +spin_unlock (ptl); +} +/* * 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
@@ -1653,6 +1755,10 @@ static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head) { } +static inline void alloc_huge_page_vmemmap (struct hstate *h,struct page *head) +{ +} +static inline void __update_and_free_page (struct hstate *h,struct page *page) {__free_hugepage (h, page);
@@ -1685,6 +1791,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 |
Signature · 1 line
2.11.0
PATCH v3 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.c | 98 ++++++++++++++++++++++++------------------------------------ 1 file changed, 39 insertions(+), 59 deletions(-)
mm/hugetlb.c +39 -59
@@ -1454,6 +1454,41 @@ static inline int freed_vmemmap_hpage_dec(struct page *page)return atomic_dec_return_relaxed (&page->_mapcount) +1 ; } +typedef void (*remap_pte_fn )(struct page *reuse,pte_t *ptep, +unsigned long start,unsigned int nr_pages, +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 { +unsigned int nr_pages; +pte_t *ptep; + + ptep =pte_offset_kernel (pmd, addr); +if (!reuse) { + reuse =pte_page (ptep[-1 ]); +set_page_private (reuse, addr -PAGE_SIZE ); + } + + next =vmemmap_hpage_addr_end (addr, end); + nr_pages = (next - addr) >>PAGE_SHIFT ; +remap_fn (reuse, ptep, addr, nr_pages, pages); + }while (pmd++, addr = next, addr != end); + +flush_tlb_kernel_range (start, end); +} +static inline void free_vmemmap_page_list (struct list_head *list) {struct page *page, *next;
@@ -1487,33 +1522,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 { -unsigned int nr_pages; -pte_t *ptep; - - ptep =pte_offset_kernel (pmd, addr); -if (!reuse) - reuse =pte_page (ptep[-1 ]); - - next =vmemmap_hpage_addr_end (addr, end); - nr_pages = (next - addr) >>PAGE_SHIFT ; -__free_huge_page_pte_vmemmap (reuse, ptep, addr, nr_pages, - 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;
@@ -1573,7 +1581,8 @@ static void free_huge_page_vmemmap(struct hstate *h, struct page *head)split_vmemmap_huge_page (h, 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);
@@ -1614,35 +1623,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_NR *PAGE_SIZE ; -unsigned long end = addr +vmemmap_pages_size_per_hpage (h); -struct page *reuse =NULL ; - - addr = start; -do { -unsigned int nr_pages; -pte_t *ptep; - - ptep =pte_offset_kernel (pmd, addr); -if (!reuse) { - reuse =pte_page (ptep[-1 ]); -set_page_private (reuse, addr -PAGE_SIZE ); - } - - next =vmemmap_hpage_addr_end (addr, end); - nr_pages = (next - addr) >>PAGE_SHIFT ; -__remap_huge_page_pte_vmemmap (reuse, ptep, addr, nr_pages, - 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;
@@ -1669,8 +1649,8 @@ static void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) pmd =vmemmap_to_pmd (head); 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:
Signature · 1 line
2.11.0
PATCH v3 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.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
mm/hugetlb.c +22 -1
@@ -1565,6 +1565,25 @@ static void split_vmemmap_huge_page(struct hstate *h, struct page *head,flush_tlb_kernel_range (start, addr); } +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 free_huge_page_vmemmap (struct hstate *h,struct page *head) {pmd_t *pmd;
@@ -1579,6 +1598,7 @@ static void free_huge_page_vmemmap(struct hstate *h, struct page *head)if (vmemmap_pmd_huge (pmd)) {VM_BUG_ON (!pgtable_pages_to_prealloc_per_hpage (h));split_vmemmap_huge_page (h, head, pmd); +set_pmd_split (pmd); } remap_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, &free_pages,
@@ -1651,11 +1671,12 @@ static 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); }
Signature · 1 line
2.11.0
PATCH v3 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 | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 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 +71 -0
95 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v3 15/21 mm/hugetlb: Add a BUILD_BUG_ON to check if struct page size is a 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. So add a BUILD_BUG_ON to check the illegal case.
--- mm/hugetlb.c | 4 ++++ 1 file changed, 4 insertions(+)
mm/hugetlb.c +4 -0
@@ -3764,6 +3764,10 @@ static int __init hugetlb_init(void) {int i; +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +BUILD_BUG_ON_NOT_POWER_OF_2 (sizeof (struct page )); +#endif +if (!hugepages_supported ()) {if (hugetlb_max_hstate || default_hstate_max_huge_pages)pr_warn ("HugeTLB: huge pages not supported, ignoring associated command-line parameters\n" );
Signature · 1 line
2.11.0
PATCH v3 16/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 | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-)
mm/hugetlb.c +42 -8
@@ -1794,6 +1794,29 @@ static inline void free_gigantic_page(struct hstate *h, struct page *page) {__free_gigantic_page (page,huge_page_order (h)); } + +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); +}#else static inline void hugetlb_vmemmap_init (struct hstate *h) {
@@ -1841,6 +1864,22 @@ static inline void free_gigantic_page(struct hstate *h, struct page *page)__free_gigantic_page (page,huge_page_order (h)); spin_lock(&hugetlb_lock); } + +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); + } +}#endif static void update_and_free_page (struct hstate *h,struct page *page)
@@ -1859,6 +1898,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 |
@@ -2416,14 +2456,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]--;
Signature · 1 line
2.11.0
PATCH v3 17/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 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
mm/hugetlb.c +19 -0
@@ -1795,6 +1795,11 @@ static inline void free_gigantic_page(struct hstate *h, struct page *page)__free_gigantic_page (page,huge_page_order (h)); } +static inline void flush_free_huge_page_work (void ) +{ +flush_work (&hpage_update_work); +} +static inline void subpage_hwpoison_deliver (struct page *head) {struct page *page = head;
@@ -1865,6 +1870,10 @@ static inline void free_gigantic_page(struct hstate *h, struct page *page)spin_lock (&hugetlb_lock); } +static inline void flush_free_huge_page_work (void ) +{ +} +static inline void subpage_hwpoison_deliver (struct page *head) { }
@@ -2439,6 +2448,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 ; +bool need_flush = false;/* Not to disrupt normal path by vainly holding hugetlb_lock */ if (!PageHuge (page))
@@ -2463,10 +2473,19 @@ int dissolve_free_huge_page(struct page *page) h->free_huge_pages_node[nid]--; h->max_huge_pages--;update_and_free_page (h, head); + need_flush = true; rc =0 ; } out:spin_unlock (&hugetlb_lock); + +/* +* We should flush work before return to make sure that +* the hugetlb page is freed to the buddy. +*/ +if (need_flush) +flush_free_huge_page_work (); +return rc; }
Signature · 1 line
2.11.0
PATCH v3 18/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.c | 23 +++++++++++++++++++++++ 3 files changed, 35 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.c +23 -0
@@ -1319,6 +1319,8 @@ static void __free_hugepage(struct hstate *h, struct page *page); (__boundary -1 < (end) -1 ) ? __boundary : (end); \ }) +static bool hugetlb_free_vmemmap_disabled__initdata ; +static inline unsigned int free_vmemmap_pages_per_hpage (struct hstate *h) {return h->nr_free_vmemmap_pages;
@@ -1480,6 +1482,13 @@ static 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 ("HugeTLB: 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 not free to buddy system,
@@ -1822,6 +1831,20 @@ static inline void set_subpage_hwpoison(struct page *head, struct page *page)if (PageHWPoison (head)) set_page_private(head + 4, page - head); } + +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);#else static inline void hugetlb_vmemmap_init (struct hstate *h) {
Signature · 1 line
2.11.0
PATCH v3 19/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 +++ include/linux/hugetlb.h | 8 +++ mm/hugetlb.c | 108 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 122 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)
include/linux/hugetlb.h +8 -0
@@ -605,6 +605,14 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) }#endif +#ifndef vmemmap_pmd_mkhuge +#define vmemmap_pmd_mkhuge vmemmap_pmd_mkhuge +static inline pmd_t vmemmap_pmd_mkhuge (struct page *page) +{ +return pmd_mkhuge (mk_pmd (page,PAGE_KERNEL )); +} +#endif +#ifndef VMEMMAP_HPAGE_SHIFT #define VMEMMAP_HPAGE_SHIFT HPAGE_SHIFT#endif
mm/hugetlb.c +106 -2
128 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v3 20/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 | 16 ++++++++-------- 3 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 +8 -8
@@ -1918,7 +1918,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,
@@ -1933,7 +1933,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 int __init early_hugetlb_free_vmemmap_param (char *buf)
@@ -2074,20 +2074,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 ]); }/*
@@ -2099,17 +2099,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)
Signature · 1 line
2.11.0
PATCH v3 21/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.c | 2 ++ 1 file changed, 2 insertions(+)
mm/hugetlb.c +2 -0
@@ -3946,6 +3946,8 @@ static int __init hugetlb_init(void)#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP BUILD_BUG_ON_NOT_POWER_OF_2 (sizeof (struct page )); +BUILD_BUG_ON (NR_USED_SUBPAGE >= +RESERVE_VMEMMAP_SIZE /sizeof (struct page ));#endif if (!hugepages_supported ()) {
Signature · 1 line
2.11.0
Thanks for continuing to work this Muchun!
On 11/8/20 6:10 AM, Muchun Song wrote:
...
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.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 4095 pages. This is a very substantial gain.
Is that 4095 number accurate? Are we not using two pages of struct pages as in the 2MB case?
Also, because we are splitting the huge page mappings in the vmemmap additional PTE pages will need to be allocated. Therefore, some additional page table pages may need to be allocated so that we can free the pages of struct pages. The net savings may be less than what is stated above.
Perhaps this should mention that allocation of additional page table pages may be required?
...
Quoted 76 lines
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_hugepagesreal 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_hugepagesreal 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_hugepagesreal 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_hugepagesreal 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_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 5 lines
Although the overhead has increased. But the overhead is not on the allocating/freeing of each hugetlb page, it is only once when we reserve some hugetlb pages through
/proc/sys/vm/nr_hugepages. Once the reservation is successful, the subsequent allocating, freeing and using are the same as before (not patched). So I think that the overhead is acceptable.
Thank you for benchmarking. There are still some instances where huge pages are allocated 'on the fly' instead of being pulled from the pool. Michal pointed out the case of page migration. It is also possible for someone to use hugetlbfs without pre-allocating huge pages to the pool. I remember the use case pointed out in commit 099730d67417. It says, "I have a hugetlbfs user which is never explicitly allocating huge pages with 'nr_hugepages'. They only set 'nr_overcommit_hugepages' and then let the pages be allocated from the buddy allocator at fault time." In this case, I suspect they were using 'page fault' allocation for initialization much like someone using /proc/sys/vm/nr_hugepages. So, the overhead may not be as noticeable.
Signature · 1 line
Mike Kravetz
Quoted 12 lines · Mike Kravetz
Thanks for continuing to work this Muchun!
On 11/8/20 6:10 AM, Muchun Song wrote:
...Quoted 6 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 8 lines
Quoted 5 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 4095 pages. This is a very substantial gain.
Is that 4095 number accurate? Are we not using two pages of struct pages as in the 2MB case?
Oh, yeah, here should be 4094 and subtract page tables. For a 1GB HugeTLB page, it should be 4086 pages. Thanks for pointing out this problem.
Quoted 8 lines
Also, because we are splitting the huge page mappings in the vmemmap additional PTE pages will need to be allocated. Therefore, some additional page table pages may need to be allocated so that we can free the pages of struct pages. The net savings may be less than what is stated above.
Perhaps this should mention that allocation of additional page table pages may be required?
Yeah, you are right. In the later patch, I will rework the analysis here. Make it
more clear and accurate.
Quoted 84 lines
...
Quoted 76 lines
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_hugepagesreal 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_hugepagesreal 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_hugepagesreal 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_hugepagesreal 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_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 5 lines
Although the overhead has increased. But the overhead is not on the allocating/freeing of each hugetlb page, it is only once when we reserve some hugetlb pages through
/proc/sys/vm/nr_hugepages. Once the reservation is successful, the subsequent allocating, freeing and using are the same as before (not patched). So I think that the overhead is acceptable.
Quoted 10 lines
Thank you for benchmarking. There are still some instances where huge pages are allocated 'on the fly' instead of being pulled from the pool. Michal pointed out the case of page migration. It is also possible for someone to use hugetlbfs without pre-allocating huge pages to the pool. I remember the use case pointed out in commit
099730d67417. It says, "I have a hugetlbfs user which is never explicitly allocating huge pages with 'nr_hugepages'. They only set 'nr_overcommit_hugepages' and then let the pages be allocated from the buddy allocator at fault time." In this case, I suspect they were using 'page fault' allocation for initialization much like someone using/proc/sys/vm/nr_hugepages. So, the overhead may not be as noticeable.
Thanks for pointing out this using case.
Quoted 3 lines
--
Mike Kravetz
Signature · 2 lines
Yours, Muchun