Transparent Hugepage Support #32
Subsystems (35)
Re: [PATCH 30 of 66] transparent hugepage core
In reply to Andrea ArcangeliQuoted 262 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
Lately I've been working to make KVM use hugepages transparently without the usual restrictions of hugetlbfs. Some of the restrictions I'd like to see removed:
1) hugepages have to be swappable or the guest physical memory remains locked in RAM and can't be paged out to swap
2) if a hugepage allocation fails, regular pages should be allocated instead and mixed in the same vma without any failure and without userland noticing
3) if some task quits and more hugepages become available in the buddy, guest physical memory backed by regular pages should be relocated on hugepages automatically in regions under
madvise(MADV_HUGEPAGE)(ideally event driven by waking up the kernel deamon if the order=HPAGE_PMD_SHIFT-PAGE_SHIFT list becomes not null)4) avoidance of reservation and maximization of use of hugepages whenever possible. Reservation (needed to avoid runtime fatal faliures) may be ok for 1 machine with 1 database with 1 database cache with 1 database cache size known at boot time. It's definitely not feasible with a virtualization hypervisor usage like RHEV-H that runs an unknown number of virtual machines with an unknown size of each virtual machine with an unknown amount of pagecache that could be potentially useful in the host for guest not using
O_DIRECT(aka cache=off).hugepages in the virtualization hypervisor (and also in the guest!) are much more important than in a regular host not using virtualization, becasue with NPT/EPT they decrease the tlb-miss cacheline accesses from 24 to 19 in case only the hypervisor uses transparent hugepages, and they decrease the tlb-miss cacheline accesses from 19 to 15 in case both the linux hypervisor and the linux guest both uses this patch (though the guest will limit the addition speedup to anonymous regions only for now...). Even more important is that the tlb miss handler is much slower on a NPT/EPT guest than for a regular shadow paging or no-virtualization scenario. So maximizing the amount of virtual memory cached by the TLB pays off significantly more with NPT/EPT than without (even if there would be no significant speedup in the tlb-miss runtime).
The first (and more tedious) part of this work requires allowing the VM to handle anonymous hugepages mixed with regular pages transparently on regular anonymous vmas. This is what this patch tries to achieve in the least intrusive possible way. We want hugepages and hugetlb to be used in a way so that all applications can benefit without changes (as usual we leverage the KVM virtualization design: by improving the Linux VM at large, KVM gets the performance boost too).
The most important design choice is: always fallback to 4k allocation if the hugepage allocation fails! This is the _very_ opposite of some large pagecache patches that failed with
-EIOback then if a 64k (or similar) allocation failed...Second important decision (to reduce the impact of the feature on the existing pagetable handling code) is that at any time we can split an hugepage into 512 regular pages and it has to be done with an operation that can't fail. This way the reliability of the swapping isn't decreased (no need to allocate memory when we are short on memory to swap) and it's trivial to plug a split_huge_page* one-liner where needed without polluting the VM. Over time we can teach mprotect, mremap and friends to handle
pmd_trans_hugenatively without calling split_huge_page*. The fact it can't fail isn't just for swap:
ifsplit_huge_pagewould return-ENOMEM(instead of the current void) we'd need to rollback the mprotect from the middle of it (ideally including undoing thesplit_vma) which would be a big change and in the very wrong direction (it'd likely be simpler not to callsplit_huge_pageat all and to teach mprotect and friends to handle hugepages instead of rolling them back from the middle). In short the very value ofsplit_huge_pageis that it can't fail.The collapsing and
madvise(MADV_HUGEPAGE)part will remain separated and incremental and it'll just be an "harmless" addition later if this initial part is agreed upon. It also should be noted that locking-wise replacing regular pages with hugepages is going to be very easy if compared to what I'm doing below insplit_huge_page, as it will only happen whenpage_count(page)matchespage_mapcount(page)if we can take thePG_lockandmmap_semin write mode.collapse_huge_pagewill be a "best effort" that (unlikesplit_huge_page) can fail at the minimal sign of trouble and we can try again later.collapse_huge_pagewill be similar to how KSM works and themadvise(MADV_HUGEPAGE)will work similar tomadvise(MADV_MERGEABLE).The default I like is that transparent hugepages are used at page fault time. This can be changed with
/sys/kernel/mm/transparent_hugepage/enabled. The control knob can be set to three values "always", "madvise", "never" which mean respectively that hugepages are always used, or only insidemadvise(MADV_HUGEPAGE)regions, or never used./sys/kernel/mm/transparent_hugepage/defraginstead controls if the hugepage allocation should defrag memory aggressively "always", only inside "madvise" regions, or "never".The pmd_trans_splitting/pmd_trans_huge locking is very solid. The
put_page(fromget_user_pageusers that can't use mmu notifier likeO_DIRECT) that runs against a__split_huge_page_refcountinstead was a pain to serialize in a way that would result always in a coherent page count for both tail and head. I think my locking solution with acompound_locktaken only after thepage_firstis valid and is still a PageHead should be safe but it surely needs review from SMP race point of view. In short there is no current existing way to serialize theO_DIRECTfinalput_pageagainstsplit_huge_page_refcountso I had to invent a new one (O_DIRECTloses knowledge on the mapping status by the timegup_fastreturns so...). And I didn't want to impact all gup/gup_fast users for now, maybe if we change the gup interface substantially we can avoid this locking, I admit I didn't think too much about it because changing the gup unpinning interface would be invasive.If we ignored
O_DIRECTwe could stick to the existing compound refcounting code, by simply adding aget_user_pages_fast_flags(foll_flags)where KVM (and any other mmu notifier user) would call it withoutFOLL_GET(and ifFOLL_GETisn't set we'd justBUG_ONif nobody registered itself in the current task mmu notifier list yet). ButO_DIRECTis fundamental for decent performance of virtualized I/O on fast storage so we can't avoid it to solve the race ofput_pageagainstsplit_huge_page_refcountto achieve a complete hugepage feature for KVM.Swap and oom works fine (well just like with regular pages ;). MMU notifier is handled transparently too, with the exception of the young bit on the pmd, that didn't have a range check but I think KVM will be fine because the whole point of hugepages is that EPT/NPT will also use a huge pmd when they notice gup returns pages with PageCompound set, so they won't care of a range and there's just the pmd young bit to check in that case.
NOTE: in some cases if the L2 cache is small, this may slowdown and waste memory during COWs because 4M of memory are accessed in a single fault instead of 8k (the payoff is that after COW the program can run faster). So we might want to switch the
copy_huge_page(andclear_huge_pagetoo) to not temporal stores. I also extensively researched ways to avoid this cache trashing with a full prefault logic that would cow in 8k/16k/32k/64k up to 1M (I can send those patches that fully implemented prefault) but I concluded they're not worth it and they add an huge additional complexity and they remove all tlb benefits until the full hugepage has been faulted in, to save a little bit of memory and some cache during app startup, but they still don't improve substantially the cache-trashing during startup if the prefault happens in >4k chunks. One reason is that those 4k pte entries copied are still mapped on a perfectly cache-colored hugepage, so the trashing is the worst one can generate in those copies (cow of 4k page copies aren't so well colored so they trashes less, but again this results in software running faster after the page fault). Those prefault patches allowed things like a pte where post-cow pages were local 4k regular anon pages and the not-yet-cowed pte entries were pointing in the middle of some hugepage mapped read-only. If it doesn't payoff substantially with todays hardware it will payoff even less in the future with larger l2 caches, and the prefault logic would blot the VM a lot. If one is emebddedtransparent_hugepagecan be disabled during boot with sysfs or with the boot commandline parameter transparent_hugepage=0 (or transparent_hugepage=2 to restrict hugepages inside madvise regions) that will ensure not a single hugepage is allocated at boot time. It is simple enough to just disable transparent hugepage globally and let transparent hugepages be allocated selectively by applications in theMADV_HUGEPAGEregion (both at page fault time, and if enabled with thecollapse_huge_pagetoo through the kernel daemon).This patch supports only hugepages mapped in the pmd, archs that have smaller hugepages will not fit in this patch alone. Also some archs like power have certain tlb limits that prevents mixing different page size in the same regions so they will not fit in this framework that requires "graceful fallback" to basic
PAGE_SIZEin case of physical memory fragmentation. hugetlbfs remains a perfect fit for those because its software limits happen to match the hardware limits. hugetlbfs also remains a perfect fit for hugepage sizes like 1GByte that cannot be hoped to be found not fragmented after a certain system uptime and that would be very expensive to defragment with relocation, so requiring reservation. hugetlbfs is the "reservation way", the point of transparent hugepages is not to have any reservation at all and maximizing the use of cache and hugepages at all times automatically.Some performance result:
vmx andrea # LD_PRELOAD=/usr/lib64/libhugetlbfs.so HUGETLB_MORECORE=yes HUGETLB_PATH=/mnt/huge/ ./largep ages3
memset page fault 1566023
memset tlb miss 453854
memset second tlb miss 453321
random access tlb miss 41635
random access second tlb miss 41658
vmx andrea # LD_PRELOAD=/usr/lib64/libhugetlbfs.so HUGETLB_MORECORE=yes HUGETLB_PATH=/mnt/huge/ ./largepages3 memset page fault 1566471
memset tlb miss 453375
memset second tlb miss 453320
random access tlb miss 41636
random access second tlb miss 41637
vmx andrea # ./largepages3
memset page fault 1566642
memset tlb miss 453417
memset second tlb miss 453313
random access tlb miss 41630
random access second tlb miss 41647
vmx andrea # ./largepages3
memset page fault 1566872
memset tlb miss 453418
memset second tlb miss 453315
random access tlb miss 41618
random access second tlb miss 41659
vmx andrea # echo 0 >/proc/sys/vm/transparent_hugepage
vmx andrea # ./largepages3
memset page fault 2182476
memset tlb miss 460305
memset second tlb miss 460179
random access tlb miss 44483
random access second tlb miss 44186
vmx andrea # ./largepages3
memset page fault 2182791
memset tlb miss 460742
memset second tlb miss 459962
random access tlb miss 43981
random access second tlb miss 43988============
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>#define SIZE (3UL*1024*1024*1024)
int
main(){ char *p = malloc(SIZE), *p2; struct timeval before, after; gettimeofday(&before, NULL); memset(p, 0, SIZE); gettimeofday(&after, NULL); printf("memset page fault %Lu\n", (after.tv_sec-before.tv_sec)*1000000UL + after.tv_usec-before.tv_usec); gettimeofday(&before, NULL); memset(p, 0, SIZE); gettimeofday(&after, NULL); printf("memset tlb miss %Lu\n", (after.tv_sec-before.tv_sec)*1000000UL + after.tv_usec-before.tv_usec); gettimeofday(&before, NULL); memset(p, 0, SIZE); gettimeofday(&after, NULL); printf("memset second tlb miss %Lu\n", (after.tv_sec-before.tv_sec)*1000000UL + after.tv_usec-before.tv_usec); gettimeofday(&before, NULL); for (p2 = p; p2 < p+SIZE; p2 += 4096) *p2 = 0; gettimeofday(&after, NULL); printf("random access tlb miss %Lu\n", (after.tv_sec-before.tv_sec)*1000000UL + after.tv_usec-before.tv_usec); gettimeofday(&before, NULL); for (p2 = p; p2 < p+SIZE; p2 += 4096) *p2 = 0; gettimeofday(&after, NULL); printf("random access second tlb miss %Lu\n", (after.tv_sec-before.tv_sec)*1000000UL + after.tv_usec-before.tv_usec); return 0; }
Quoted 2 lines
============
All that seems fine to me. Nits in part that are simply not worth calling out. In principal, I Agree With This :)
Quoted 138 lines
Signed-off-by Andrea Arcangeli <[email protected]>Acked-by Rik van Riel <[email protected]>Signed-off-by Johannes Weiner <[email protected]>---
* * *
adapt tomm_counterin -mmFrom: Andrea Arcangeli <[email protected]>
The interface changed slightly.
Signed-off-by Andrea Arcangeli <[email protected]>Acked-by Rik van Riel <[email protected]>---
* * *
transparent hugepage bootparamFrom: Andrea Arcangeli <[email protected]>
Allow transparent_hugepage=always|never|madvise at boot.
Signed-off-by Andrea Arcangeli <[email protected]>---
arch/x86/include/asm/pgtable_64.h +5
-0@@ -286,6 +286,11 @@ static inline pmd_t pmd_mkwrite(pmd_t pmreturn pmd_set_flags (pmd, _PAGE_RW); } +static inline pmd_t pmd_mknotpresent (pmd_t pmd) +{ +return pmd_clear_flags (pmd, _PAGE_PRESENT); +} +#endif /* !__ASSEMBLY__ */ #endif /* _ASM_X86_PGTABLE_64_H */ include/linux/gfp.h +3
-0@@ -108,6 +108,9 @@ struct vm_area_struct; __GFP_HARDWALL | __GFP_HIGHMEM | \ __GFP_MOVABLE)#define GFP_IOFS (__GFP_IO | __GFP_FS) +#define GFP_TRANSHUGE (GFP_HIGHUSER_MOVABLE | __GFP_COMP | \ + __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | \ + __GFP_NO_KSWAPD)#ifdef CONFIG_NUMA #define GFP_THISNODE (__GFP_THISNODE | __GFP_NOWARN | __GFP_NORETRY)include/linux/huge_mm.h new file +81
-0@@ -0,0 +1,126 @@ +#ifndef _LINUX_HUGE_MM_H +#define _LINUX_HUGE_MM_H + +extern int do_huge_pmd_anonymous_page (struct mm_struct *mm, +struct vm_area_struct *vma, +unsigned long address,pmd_t *pmd, +unsigned int flags); +extern int copy_huge_pmd (struct mm_struct *dst_mm,struct mm_struct *src_mm, +pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr, +struct vm_area_struct *vma); +extern int do_huge_pmd_wp_page (struct mm_struct *mm,struct vm_area_struct *vma, +unsigned long address,pmd_t *pmd, +pmd_t orig_pmd); +extern pgtable_t get_pmd_huge_pte (struct mm_struct *mm); +extern struct page *follow_trans_huge_pmd (struct mm_struct *mm, +unsigned long addr, +pmd_t *pmd, +unsigned int flags); +extern int zap_huge_pmd (struct mmu_gather *tlb, +struct vm_area_struct *vma, +pmd_t *pmd); + +enum transparent_hugepage_flag { +TRANSPARENT_HUGEPAGE_FLAG , +TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , +TRANSPARENT_HUGEPAGE_DEFRAG_FLAG , +TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG , +#ifdef CONFIG_DEBUG_VM +TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG , +#endif +}; + +enum page_check_address_pmd_flag { +PAGE_CHECK_ADDRESS_PMD_FLAG , +PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG , +PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG , +}; +extern pmd_t *page_check_address_pmd (struct page *page, +struct mm_struct *mm, +unsigned long address, +enum page_check_address_pmd_flag flag); + +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#define HPAGE_PMD_SHIFT HPAGE_SHIFT +#define HPAGE_PMD_MASK HPAGE_MASK +#define HPAGE_PMD_SIZE HPAGE_SIZE + +#define transparent_hugepage_enabled (__vma) \ + (transparent_hugepage_flags & (1<<TRANSPARENT_HUGEPAGE_FLAG) || \ + (transparent_hugepage_flags & \ + (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG) && \ + (__vma)->vm_flags & VM_HUGEPAGE)) +#define transparent_hugepage_defrag (__vma) \ + ((transparent_hugepage_flags & \ + (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)) || \ + (transparent_hugepage_flags & \ + (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG) && \ + (__vma)->vm_flags & VM_HUGEPAGE)) +#ifdef CONFIG_DEBUG_VM +#define transparent_hugepage_debug_cow () \ + (transparent_hugepage_flags & \ + (1<<TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG)) +#else /* CONFIG_DEBUG_VM */ +#define transparent_hugepage_debug_cow () 0 +#endif /* CONFIG_DEBUG_VM */ + +extern unsigned long transparent_hugepage_flags; +extern int copy_pte_range (struct mm_struct *dst_mm,struct mm_struct *src_mm, +pmd_t *dst_pmd,pmd_t *src_pmd, +struct vm_area_struct *vma, +unsigned long addr,unsigned long end); +extern int handle_pte_fault (struct mm_struct *mm, +struct vm_area_struct *vma,unsigned long address, +pte_t *pte,pmd_t *pmd,unsigned int flags); +extern int split_huge_page (struct page *page); +extern void __split_huge_page_pmd (struct mm_struct *mm,pmd_t *pmd); +#define split_huge_page_pmd (__mm, __pmd) \ + do { \ + pmd_t *____pmd = (__pmd); \ + if (unlikely(pmd_trans_huge(*____pmd))) \ + __split_huge_page_pmd(__mm, ____pmd); \
include/linux/huge_mm.h line 91
Quoted 10 lines
include/linux/huge_mm.h new file +10
-0+ } while (0) +#define wait_split_huge_page (__anon_vma, __pmd) \ + do { \ + pmd_t *____pmd = (__pmd); \ + spin_unlock_wait(&(__anon_vma)->root->lock); \ +/* \ +* spin_unlock_wait() is just a loop in C and so the \ +* CPU can reorder anything around it. \ +*/ \ +smp_mb (); \
Just a note as I see nothing wrong with this but that's a good spot. The unlock isn't a memory barrier. Out of curiousity, does it really need to be a full barrier or would a write barrier have been enough?
Quoted 5 lines
include/linux/huge_mm.h new file +5
-0+BUG_ON (pmd_trans_splitting (*____pmd) || \ +pmd_trans_huge (*____pmd)); \ + } while (0) +#define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT) +#define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER)
include/linux/huge_mm.h line 106
Quoted 10 lines
include/linux/huge_mm.h new file +10
-0+#if HPAGE_PMD_ORDER >MAX_ORDER +#error "hugepages can't be allocated by the buddy allocator" +#endif + +extern unsigned long vma_address (struct page *page,struct vm_area_struct *vma); +static inline int PageTransHuge (struct page *page) +{ +VM_BUG_ON (PageTail (page)); +return PageHead (page); +}
gfp.h seems an odd place for these. Should the flags go in page-flags.h and maybe put vma_address() in internal.h?
Not a biggie.
Quoted 55 lines
include/linux/huge_mm.h new file +20
-0+#else /* CONFIG_TRANSPARENT_HUGEPAGE */ +#define HPAGE_PMD_SHIFT ({ BUG(); 0; }) +#define HPAGE_PMD_MASK ({ BUG(); 0; }) +#define HPAGE_PMD_SIZE ({ BUG(); 0; }) + +#define transparent_hugepage_enabled (__vma) 0 + +#define transparent_hugepage_flags 0UL +static inline int split_huge_page (struct page *page) +{ +return 0 ; +} +#define split_huge_page_pmd (__mm, __pmd) \ + do { } while (0) +#define wait_split_huge_page (__anon_vma, __pmd) \ + do { } while (0) +#define PageTransHuge (page) 0 +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + +#endif /* _LINUX_HUGE_MM_H */ include/linux/mm.h +4
-0@@ -111,6 +111,9 @@ extern unsigned int kobjsize(const void#define VM_SAO 0x20000000/* Strong Access Ordering (powerpc) */ #define VM_PFN_AT_MMAP 0x40000000/* PFNMAP vma that is fully mapped at mmap time */ #define VM_MERGEABLE 0x80000000/* KSM may merge identical pages */ +#if BITS_PER_LONG >32 +#define VM_HUGEPAGE 0x100000000UL/* MADV_HUGEPAGE marked this vma */ +#endif #ifndef VM_STACK_DEFAULT_FLAGS /* arch can override this */ #define VM_STACK_DEFAULT_FLAGS VM_DATA_DEFAULT_FLAGS@@ -240,6 +243,7 @@ struct inode;* files which need it (119 of them) */ #include <linux/page-flags.h> +#include <linux/huge_mm.h> /* * Methods to modify the page usage count. include/linux/mm_inline.h +7
-0@@ -20,11 +20,18 @@ static inline int page_is_file_cache(str }static inline void +__add_page_to_lru_list (struct zone *zone,struct page *page,enum lru_list l, +struct list_head *head) +{ +list_add (&page->lru, head); +__inc_zone_state (zone,NR_LRU_BASE + l); +mem_cgroup_add_lru_list (page, l); +}
include/linux/mm_inline.h static inline int page_is_file_cache(str · line 36
Quoted 10 lines
include/linux/mm_inline.h +3
-3+ +static inline void add_page_to_lru_list (struct zone *zone,struct page *page,enum lru_list l) { - list_add(&page->lru, &zone->lru[l].list); -__inc_zone_state (zone,NR_LRU_BASE + l); -mem_cgroup_add_lru_list (page, l); +__add_page_to_lru_list (zone, page, l, &zone->lru[l].list); }
Do these really need to be in a public header or can they move to mm/swap.c?
Quoted 225 lines
include/linux/mm_inline.h +0
-0static inline voidinclude/linux/swap.h +2
-0@@ -208,6 +208,8 @@ extern unsigned int nr_free_pagecache_pa/* linux/mm/swap.c */ extern void __lru_cache_add (struct page *,enum lru_list lru);extern void lru_cache_add_lru (struct page *,enum lru_list lru); +extern void lru_add_page_tail (struct zone * zone, +struct page *page,struct page *page_tail);extern void activate_page (struct page *);extern void mark_page_accessed (struct page *);extern void lru_add_drain (void );mm/Makefile +1
-0@@ -42,3 +42,4 @@ obj-$(CONFIG_MEMORY_FAILURE) += memory-f obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o +obj-$(CONFIG_TRANSPARENT_HUGEPAGE) += huge_memory.omm/huge_memory.c new file +199
-0@@ -0,0 +1,899 @@ +/* +* Copyright (C) 2009 Red Hat, Inc. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#include <linux/mm.h> +#include <linux/sched.h> +#include <linux/highmem.h> +#include <linux/hugetlb.h> +#include <linux/mmu_notifier.h> +#include <linux/rmap.h> +#include <linux/swap.h> +#include <asm/tlb.h> +#include <asm/pgalloc.h> +#include "internal.h" + +unsigned long transparent_hugepage_flags__read_mostly = + (1 <<TRANSPARENT_HUGEPAGE_FLAG ); + +#ifdef CONFIG_SYSFS +static ssize_t double_flag_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf, +enum transparent_hugepage_flag enabled, +enum transparent_hugepage_flag req_madv) +{ +if (test_bit (enabled, &transparent_hugepage_flags)) { +VM_BUG_ON (test_bit (req_madv, &transparent_hugepage_flags)); +return sprintf (buf,"[always] madvise never\n" ); + }else if (test_bit (req_madv, &transparent_hugepage_flags)) +return sprintf (buf,"always [madvise] never\n" ); +else +return sprintf (buf,"always madvise [never]\n" ); +} +static ssize_t double_flag_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count, +enum transparent_hugepage_flag enabled, +enum transparent_hugepage_flag req_madv) +{ +if (!memcmp ("always" , buf, +min (sizeof ("always" )-1 , count))) { +set_bit (enabled, &transparent_hugepage_flags); +clear_bit (req_madv, &transparent_hugepage_flags); + }else if (!memcmp ("madvise" , buf, +min (sizeof ("madvise" )-1 , count))) { +clear_bit (enabled, &transparent_hugepage_flags); +set_bit (req_madv, &transparent_hugepage_flags); + }else if (!memcmp ("never" , buf, +min (sizeof ("never" )-1 , count))) { +clear_bit (enabled, &transparent_hugepage_flags); +clear_bit (req_madv, &transparent_hugepage_flags); + }else +return -EINVAL ; + +return count; +} + +static ssize_t enabled_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return double_flag_show (kobj, attr, buf, +TRANSPARENT_HUGEPAGE_FLAG , +TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG ); +} +static ssize_t enabled_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +return double_flag_store (kobj, attr, buf, count, +TRANSPARENT_HUGEPAGE_FLAG , +TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG ); +} +static struct kobj_attribute enabled_attr = +__ATTR (enabled,0644 , enabled_show, enabled_store); + +static ssize_t single_flag_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf, +enum transparent_hugepage_flag flag) +{ +if (test_bit (flag, &transparent_hugepage_flags)) +return sprintf (buf,"[yes] no\n" ); +else +return sprintf (buf,"yes [no]\n" ); +} +static ssize_t single_flag_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count, +enum transparent_hugepage_flag flag) +{ +if (!memcmp ("yes" , buf, +min (sizeof ("yes" )-1 , count))) { +set_bit (flag, &transparent_hugepage_flags); + }else if (!memcmp ("no" , buf, +min (sizeof ("no" )-1 , count))) { +clear_bit (flag, &transparent_hugepage_flags); + }else +return -EINVAL ; + +return count; +} + +/* +* Currently defrag only disables __GFP_NOWAIT for allocation. A blind +* __GFP_REPEAT is too aggressive, it's never worth swapping tons of +* memory just to allocate one more hugepage. +*/ +static ssize_t defrag_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return double_flag_show (kobj, attr, buf, +TRANSPARENT_HUGEPAGE_DEFRAG_FLAG , +TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG ); +} +static ssize_t defrag_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +return double_flag_store (kobj, attr, buf, count, +TRANSPARENT_HUGEPAGE_DEFRAG_FLAG , +TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG ); +} +static struct kobj_attribute defrag_attr = +__ATTR (defrag,0644 , defrag_show, defrag_store); + +#ifdef CONFIG_DEBUG_VM +static ssize_t debug_cow_show (struct kobject *kobj, +struct kobj_attribute *attr,char *buf) +{ +return single_flag_show (kobj, attr, buf, +TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG ); +} +static ssize_t debug_cow_store (struct kobject *kobj, +struct kobj_attribute *attr, +const char *buf,size_t count) +{ +return single_flag_store (kobj, attr, buf, count, +TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG ); +} +static struct kobj_attribute debug_cow_attr = +__ATTR (debug_cow,0644 , debug_cow_show, debug_cow_store); +#endif /* CONFIG_DEBUG_VM */ + +static struct attribute *hugepage_attr[] = { + &enabled_attr.attr, + &defrag_attr.attr, +#ifdef CONFIG_DEBUG_VM + &debug_cow_attr.attr, +#endif +NULL , +}; + +static struct attribute_group hugepage_attr_group = { + .attrs = hugepage_attr, + .name ="transparent_hugepage" , +}; +#endif /* CONFIG_SYSFS */ + +static int __init hugepage_init (void ) +{ +#ifdef CONFIG_SYSFS +int err; + + err =sysfs_create_group (mm_kobj, &hugepage_attr_group); +if (err) +printk (KERN_ERR "hugepage: register sysfs failed\n" ); +#endif +return 0 ; +} +module_init (hugepage_init) + +static int __init setup_transparent_hugepage(char *str) +{ +int ret =0 ; +if (!str) + goto out; +if (!strcmp (str,"always" )) { +set_bit (TRANSPARENT_HUGEPAGE_FLAG , + &transparent_hugepage_flags); +clear_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , + &transparent_hugepage_flags); + ret =1 ; + }else if (!strcmp (str,"madvise" )) { +clear_bit (TRANSPARENT_HUGEPAGE_FLAG , + &transparent_hugepage_flags); +set_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , + &transparent_hugepage_flags); + ret =1 ; + }else if (!strcmp (str,"never" )) { +clear_bit (TRANSPARENT_HUGEPAGE_FLAG , + &transparent_hugepage_flags); +clear_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , + &transparent_hugepage_flags); + ret =1 ; + } +out: +if (!ret) + printk(KERN_WARNING
mm/huge_memory.c line 209
Quoted 10 lines
mm/huge_memory.c new file +10
-0+ "transparent_hugepage= cannot parse, ignored\n"); +return ret; +} +__setup ("transparent_hugepage=" , setup_transparent_hugepage); + +static void prepare_pmd_huge_pte (pgtable_t pgtable, +struct mm_struct *mm) +{ +VM_BUG_ON (spin_can_lock (&mm->page_table_lock)); +
assert_spin_locked() ?
Quoted 784 lines
mm/huge_memory.c new file +690
-0+/* FIFO */ +if (!mm->pmd_huge_pte) +INIT_LIST_HEAD (&pgtable->lru); +else +list_add (&pgtable->lru, &mm->pmd_huge_pte->lru); + mm->pmd_huge_pte = pgtable; +} + +static inline pmd_t maybe_pmd_mkwrite (pmd_t pmd,struct vm_area_struct *vma) +{ +if (likely (vma->vm_flags &VM_WRITE )) + pmd =pmd_mkwrite (pmd); +return pmd; +} + +static int __do_huge_pmd_anonymous_page (struct mm_struct *mm, +struct vm_area_struct *vma, +unsigned long haddr,pmd_t *pmd, +struct page *page) +{ +int ret =0 ; +pgtable_t pgtable; + +VM_BUG_ON (!PageCompound (page)); + pgtable =pte_alloc_one (mm, haddr); +if (unlikely (!pgtable)) { +put_page (page); +return VM_FAULT_OOM ; + } + +clear_huge_page (page, haddr,HPAGE_PMD_NR ); +__SetPageUptodate (page); + +spin_lock (&mm->page_table_lock); +if (unlikely (!pmd_none (*pmd))) { +spin_unlock (&mm->page_table_lock); +put_page (page); +pte_free (mm, pgtable); + }else { +pmd_t entry; + entry =mk_pmd (page, vma->vm_page_prot); + entry =maybe_pmd_mkwrite (pmd_mkdirty (entry), vma); + entry =pmd_mkhuge (entry); +/* +* The spinlocking to take the lru_lock inside +* page_add_new_anon_rmap() acts as a full memory +* barrier to be sure clear_huge_page writes become +* visible after the set_pmd_at() write. +*/ +page_add_new_anon_rmap (page, vma, haddr); +set_pmd_at (mm, haddr, pmd, entry); +prepare_pmd_huge_pte (pgtable, mm); +add_mm_counter (mm,MM_ANONPAGES ,HPAGE_PMD_NR ); +spin_unlock (&mm->page_table_lock); + } + +return ret; +} + +static inline struct page *alloc_hugepage (int defrag) +{ +return alloc_pages (GFP_TRANSHUGE & ~(defrag ?0 : __GFP_WAIT), +HPAGE_PMD_ORDER ); +} + +int do_huge_pmd_anonymous_page (struct mm_struct *mm,struct vm_area_struct *vma, +unsigned long address,pmd_t *pmd, +unsigned int flags) +{ +struct page *page; +unsigned long haddr = address &HPAGE_PMD_MASK ; +pte_t *pte; + +if (haddr >= vma->vm_start && haddr +HPAGE_PMD_SIZE <= vma->vm_end) { +if (unlikely (anon_vma_prepare (vma))) +return VM_FAULT_OOM ; + page =alloc_hugepage (transparent_hugepage_defrag (vma)); +if (unlikely (!page)) + goto out; + +return __do_huge_pmd_anonymous_page (mm, vma, haddr, pmd, page); + } +out: +/* +* Use __pte_alloc instead of pte_alloc_map, because we can't +* run pte_offset_map on the pmd, if an huge pmd could +* materialize from under us from a different thread. +*/ +if (unlikely (__pte_alloc (mm, vma, pmd, address))) +return VM_FAULT_OOM ; +/* if an huge pmd materialized from under us just retry later */ +if (unlikely (pmd_trans_huge (*pmd))) +return 0 ; +/* +* A regular pmd is established and it can't morph into a huge pmd +* from under us anymore at this point because we hold the mmap_sem +* read mode and khugepaged takes it in write mode. So now it's +* safe to run pte_offset_map(). +*/ + pte =pte_offset_map (pmd, address); +return handle_pte_fault (mm, vma, address, pte, pmd, flags); +} + +int copy_huge_pmd (struct mm_struct *dst_mm,struct mm_struct *src_mm, +pmd_t *dst_pmd,pmd_t *src_pmd,unsigned long addr, +struct vm_area_struct *vma) +{ +struct page *src_page; +pmd_t pmd; +pgtable_t pgtable; +int ret; + + ret = -ENOMEM ; + pgtable =pte_alloc_one (dst_mm, addr); +if (unlikely (!pgtable)) + goto out; + +spin_lock (&dst_mm->page_table_lock); +spin_lock_nested (&src_mm->page_table_lock,SINGLE_DEPTH_NESTING ); + + ret = -EAGAIN ; + pmd = *src_pmd; +if (unlikely (!pmd_trans_huge (pmd))) { +pte_free (dst_mm, pgtable); + goto out_unlock; + } +if (unlikely (pmd_trans_splitting (pmd))) { +/* split huge page running from under us */ +spin_unlock (&src_mm->page_table_lock); +spin_unlock (&dst_mm->page_table_lock); +pte_free (dst_mm, pgtable); + +wait_split_huge_page (vma->anon_vma, src_pmd);/* src_vma */ + goto out; + } + src_page =pmd_page (pmd); +VM_BUG_ON (!PageHead (src_page)); +get_page (src_page); +page_dup_rmap (src_page); +add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR ); + +pmdp_set_wrprotect (src_mm, addr, src_pmd); + pmd =pmd_mkold (pmd_wrprotect (pmd)); +set_pmd_at (dst_mm, addr, dst_pmd, pmd); +prepare_pmd_huge_pte (pgtable, dst_mm); + + ret =0 ; +out_unlock: +spin_unlock (&src_mm->page_table_lock); +spin_unlock (&dst_mm->page_table_lock); +out: +return ret; +} + +/* no "address" argument so destroys page coloring of some arch */ +pgtable_t get_pmd_huge_pte (struct mm_struct *mm) +{ +pgtable_t pgtable; + +VM_BUG_ON (spin_can_lock (&mm->page_table_lock)); + +/* FIFO */ + pgtable = mm->pmd_huge_pte; +if (list_empty (&pgtable->lru)) + mm->pmd_huge_pte =NULL ; +else { + mm->pmd_huge_pte =list_entry (pgtable->lru.next, + struct page, lru); +list_del (&pgtable->lru); + } +return pgtable; +} + +static int do_huge_pmd_wp_page_fallback (struct mm_struct *mm, +struct vm_area_struct *vma, +unsigned long address, +pmd_t *pmd,pmd_t orig_pmd, +struct page *page, +unsigned long haddr) +{ +pgtable_t pgtable; +pmd_t _pmd; +int ret =0 , i; +struct page **pages; + + pages =kmalloc (sizeof (struct page *) *HPAGE_PMD_NR , +GFP_KERNEL ); +if (unlikely (!pages)) { + ret |=VM_FAULT_OOM ; + goto out; + } + +for (i =0 ; i <HPAGE_PMD_NR ; i++) { + pages[i] =alloc_page_vma (GFP_HIGHUSER_MOVABLE , + vma, address); +if (unlikely (!pages[i])) { +while (--i >=0 ) +put_page (pages[i]); +kfree (pages); + ret |=VM_FAULT_OOM ; + goto out; + } + } + +for (i =0 ; i <HPAGE_PMD_NR ; i++) { +copy_user_highpage (pages[i], page + i, + haddr +PAGE_SHIFT *i, vma); +__SetPageUptodate (pages[i]); +cond_resched (); + } + +spin_lock (&mm->page_table_lock); +if (unlikely (!pmd_same (*pmd, orig_pmd))) + goto out_free_pages; +VM_BUG_ON (!PageHead (page)); + +pmdp_clear_flush_notify (vma, haddr, pmd); +/* leave pmd empty until pte is filled */ + + pgtable =get_pmd_huge_pte (mm); +pmd_populate (mm, &_pmd, pgtable); + +for (i =0 ; i <HPAGE_PMD_NR ; i++, haddr +=PAGE_SIZE ) { +pte_t *pte, entry; + entry =mk_pte (pages[i], vma->vm_page_prot); + entry =maybe_mkwrite (pte_mkdirty (entry), vma); +page_add_new_anon_rmap (pages[i], vma, haddr); + pte =pte_offset_map (&_pmd, haddr); +VM_BUG_ON (!pte_none (*pte)); +set_pte_at (mm, haddr, pte, entry); +pte_unmap (pte); + } +kfree (pages); + + mm->nr_ptes++; +smp_wmb ();/* make pte visible before pmd */ +pmd_populate (mm, pmd, pgtable); +page_remove_rmap (page); +spin_unlock (&mm->page_table_lock); + + ret |=VM_FAULT_WRITE ; +put_page (page); + +out: +return ret; + +out_free_pages: +spin_unlock (&mm->page_table_lock); +for (i =0 ; i <HPAGE_PMD_NR ; i++) +put_page (pages[i]); +kfree (pages); + goto out; +} + +int do_huge_pmd_wp_page (struct mm_struct *mm,struct vm_area_struct *vma, +unsigned long address,pmd_t *pmd,pmd_t orig_pmd) +{ +int ret =0 ; +struct page *page, *new_page; +unsigned long haddr; + +VM_BUG_ON (!vma->anon_vma); +spin_lock (&mm->page_table_lock); +if (unlikely (!pmd_same (*pmd, orig_pmd))) + goto out_unlock; + + page =pmd_page (orig_pmd); +VM_BUG_ON (!PageCompound (page) || !PageHead (page)); + haddr = address &HPAGE_PMD_MASK ; +if (page_mapcount (page) ==1 ) { +pmd_t entry; + entry =pmd_mkyoung (orig_pmd); + entry =maybe_pmd_mkwrite (pmd_mkdirty (entry), vma); +if (pmdp_set_access_flags (vma, haddr, pmd, entry,1 )) +update_mmu_cache (vma, address, entry); + ret |=VM_FAULT_WRITE ; + goto out_unlock; + } +get_page (page); +spin_unlock (&mm->page_table_lock); + +if (transparent_hugepage_enabled (vma) && + !transparent_hugepage_debug_cow ()) + new_page =alloc_hugepage (transparent_hugepage_defrag (vma)); +else + new_page =NULL ; + +if (unlikely (!new_page)) { + ret =do_huge_pmd_wp_page_fallback (mm, vma, address, + pmd, orig_pmd, page, haddr); +put_page (page); + goto out; + } + +copy_user_huge_page (new_page, page, haddr, vma,HPAGE_PMD_NR ); +__SetPageUptodate (new_page); + +spin_lock (&mm->page_table_lock); +put_page (page); +if (unlikely (!pmd_same (*pmd, orig_pmd))) +put_page (new_page); +else { +pmd_t entry; +VM_BUG_ON (!PageHead (page)); + entry =mk_pmd (new_page, vma->vm_page_prot); + entry =maybe_pmd_mkwrite (pmd_mkdirty (entry), vma); + entry =pmd_mkhuge (entry); +pmdp_clear_flush_notify (vma, haddr, pmd); +page_add_new_anon_rmap (new_page, vma, haddr); +set_pmd_at (mm, haddr, pmd, entry); +update_mmu_cache (vma, address, entry); +page_remove_rmap (page); +put_page (page); + ret |=VM_FAULT_WRITE ; + } +out_unlock: +spin_unlock (&mm->page_table_lock); +out: +return ret; +} + +struct page *follow_trans_huge_pmd (struct mm_struct *mm, +unsigned long addr, +pmd_t *pmd, +unsigned int flags) +{ +struct page *page =NULL ; + +VM_BUG_ON (spin_can_lock (&mm->page_table_lock)); + +if (flags &FOLL_WRITE && !pmd_write (*pmd)) + goto out; + + page =pmd_page (*pmd); +VM_BUG_ON (!PageHead (page)); +if (flags &FOLL_TOUCH ) { +pmd_t _pmd; +/* +* We should set the dirty bit only for FOLL_WRITE but +* for now the dirty bit in the pmd is meaningless. +* And if the dirty bit will become meaningful and +* we'll only set it with FOLL_WRITE, an atomic +* set_bit will be required on the pmd to set the +* young bit, instead of the current set_pmd_at. +*/ + _pmd =pmd_mkyoung (pmd_mkdirty (*pmd)); +set_pmd_at (mm, addr &HPAGE_PMD_MASK , pmd, _pmd); + } + page += (addr & ~HPAGE_PMD_MASK ) >>PAGE_SHIFT ; +VM_BUG_ON (!PageCompound (page)); +if (flags &FOLL_GET ) +get_page (page); + +out: +return page; +} + +int zap_huge_pmd (struct mmu_gather *tlb,struct vm_area_struct *vma, +pmd_t *pmd) +{ +int ret =0 ; + +spin_lock (&tlb->mm->page_table_lock); +if (likely (pmd_trans_huge (*pmd))) { +if (unlikely (pmd_trans_splitting (*pmd))) { +spin_unlock (&tlb->mm->page_table_lock); +wait_split_huge_page (vma->anon_vma, + pmd); + }else { +struct page *page; +pgtable_t pgtable; + pgtable =get_pmd_huge_pte (tlb->mm); + page =pmd_page (*pmd); +pmd_clear (pmd); +page_remove_rmap (page); +VM_BUG_ON (page_mapcount (page) <0 ); +add_mm_counter (tlb->mm,MM_ANONPAGES , -HPAGE_PMD_NR ); +VM_BUG_ON (!PageHead (page)); +spin_unlock (&tlb->mm->page_table_lock); +tlb_remove_page (tlb, page); +pte_free (tlb->mm, pgtable); + ret =1 ; + } + }else +spin_unlock (&tlb->mm->page_table_lock); + +return ret; +} + +pmd_t *page_check_address_pmd (struct page *page, +struct mm_struct *mm, +unsigned long address, +enum page_check_address_pmd_flag flag) +{ +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd, *ret =NULL ; + +if (address & ~HPAGE_PMD_MASK ) + goto out; + + pgd =pgd_offset (mm, address); +if (!pgd_present (*pgd)) + goto out; + + pud =pud_offset (pgd, address); +if (!pud_present (*pud)) + goto out; + + pmd =pmd_offset (pud, address); +if (pmd_none (*pmd)) + goto out; +if (pmd_page (*pmd) != page) + goto out; +VM_BUG_ON (flag ==PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG && +pmd_trans_splitting (*pmd)); +if (pmd_trans_huge (*pmd)) { +VM_BUG_ON (flag ==PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG && + !pmd_trans_splitting (*pmd)); + ret = pmd; + } +out: +return ret; +} + +static int __split_huge_page_splitting (struct page *page, +struct vm_area_struct *vma, +unsigned long address) +{ +struct mm_struct *mm = vma->vm_mm; +pmd_t *pmd; +int ret =0 ; + +spin_lock (&mm->page_table_lock); + pmd =page_check_address_pmd (page, mm, address, +PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG ); +if (pmd) { +/* +* We can't temporarily set the pmd to null in order +* to split it, the pmd must remain marked huge at all +* times or the VM won't take the pmd_trans_huge paths +* and it won't wait on the anon_vma->root->lock to +* serialize against split_huge_page*. +*/ +pmdp_splitting_flush_notify (vma, address, pmd); + ret =1 ; + } +spin_unlock (&mm->page_table_lock); + +return ret; +} + +static void __split_huge_page_refcount (struct page *page) +{ +int i; +unsigned long head_index = page->index; +struct zone *zone =page_zone (page); + +/* prevent PageLRU to go away from under us, and freeze lru stats */ +spin_lock_irq (&zone->lru_lock); +compound_lock (page); + +for (i =1 ; i <HPAGE_PMD_NR ; i++) { +struct page *page_tail = page + i; + +/* tail_page->_count cannot change */ +atomic_sub (atomic_read (&page_tail->_count), &page->_count); +BUG_ON (page_count (page) <=0 ); +atomic_add (page_mapcount (page) +1 , &page_tail->_count); +BUG_ON (atomic_read (&page_tail->_count) <=0 ); + +/* after clearing PageTail the gup refcount can be released */ +smp_mb (); + + page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP ; + page_tail->flags |= (page->flags & + ((1L << PG_referenced) | + (1L << PG_swapbacked) | + (1L << PG_mlocked) | + (1L << PG_uptodate))); + page_tail->flags |= (1L << PG_dirty); + +/* +* 1) clear PageTail before overwriting first_page +* 2) clear PageTail before clearing PageHead for VM_BUG_ON +*/ +smp_wmb (); + +/* +* __split_huge_page_splitting() already set the +* splitting bit in all pmd that could map this +* hugepage, that will ensure no CPU can alter the +* mapcount on the head page. The mapcount is only +* accounted in the head page and it has to be +* transferred to all tail pages in the below code. So +* for this code to be safe, the split the mapcount +* can't change. But that doesn't mean userland can't +* keep changing and reading the page contents while +* we transfer the mapcount, so the pmd splitting +* status is achieved setting a reserved bit in the +* pmd, not by clearing the present bit. +*/ +BUG_ON (page_mapcount (page_tail)); + page_tail->_mapcount = page->_mapcount; + +BUG_ON (page_tail->mapping); + page_tail->mapping = page->mapping; + + page_tail->index = ++head_index; + +BUG_ON (!PageAnon (page_tail)); +BUG_ON (!PageUptodate (page_tail)); +BUG_ON (!PageDirty (page_tail)); +BUG_ON (!PageSwapBacked (page_tail)); + +lru_add_page_tail (zone, page, page_tail); + } + +ClearPageCompound (page); +compound_unlock (page); +spin_unlock_irq (&zone->lru_lock); + +for (i =1 ; i <HPAGE_PMD_NR ; i++) { +struct page *page_tail = page + i; +BUG_ON (page_count (page_tail) <=0 ); +/* +* Tail pages may be freed if there wasn't any mapping +* like if add_to_swap() is running on a lru page that +* had its mapping zapped. And freeing these pages +* requires taking the lru_lock so we do the put_page +* of the tail pages after the split is complete. +*/ +put_page (page_tail); + } + +/* +* Only the head page (now become a regular page) is required +* to be pinned by the caller. +*/ +BUG_ON (page_count (page) <=0 ); +} + +static int __split_huge_page_map (struct page *page, +struct vm_area_struct *vma, +unsigned long address) +{ +struct mm_struct *mm = vma->vm_mm; +pmd_t *pmd, _pmd; +int ret =0 , i; +pgtable_t pgtable; +unsigned long haddr; + +spin_lock (&mm->page_table_lock); + pmd =page_check_address_pmd (page, mm, address, +PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG ); +if (pmd) { + pgtable =get_pmd_huge_pte (mm); +pmd_populate (mm, &_pmd, pgtable); + +for (i =0 , haddr = address; i <HPAGE_PMD_NR ; + i++, haddr +=PAGE_SIZE ) { +pte_t *pte, entry; +BUG_ON (PageCompound (page+i)); + entry =mk_pte (page + i, vma->vm_page_prot); + entry =maybe_mkwrite (pte_mkdirty (entry), vma); +if (!pmd_write (*pmd)) + entry =pte_wrprotect (entry); +else +BUG_ON (page_mapcount (page) !=1 ); +if (!pmd_young (*pmd)) + entry =pte_mkold (entry); + pte =pte_offset_map (&_pmd, haddr); +BUG_ON (!pte_none (*pte)); +set_pte_at (mm, haddr, pte, entry); +pte_unmap (pte); + } + + mm->nr_ptes++; +smp_wmb ();/* make pte visible before pmd */ +/* +* Up to this point the pmd is present and huge and +* userland has the whole access to the hugepage +* during the split (which happens in place). If we +* overwrite the pmd with the not-huge version +* pointing to the pte here (which of course we could +* if all CPUs were bug free), userland could trigger +* a small page size TLB miss on the small sized TLB +* while the hugepage TLB entry is still established +* in the huge TLB. Some CPU doesn't like that. See +* http://support.amd.com/us/Processor_TechDocs/41322.pdf, +* Erratum 383 on page 93. Intel should be safe but is +* also warns that it's only safe if the permission +* and cache attributes of the two entries loaded in +* the two TLB is identical (which should be the case +* here). But it is generally safer to never allow +* small and huge TLB entries for the same virtual +* address to be loaded simultaneously. So instead of +* doing "pmd_populate(); flush_tlb_range();" we first +* mark the current pmd notpresent (atomically because +* here the pmd_trans_huge and pmd_trans_splitting +* must remain set at all times on the pmd until the +* split is complete for this pmd), then we flush the +* SMP TLB and finally we write the non-huge version +* of the pmd entry with pmd_populate. +*/ +set_pmd_at (mm, address, pmd,pmd_mknotpresent (*pmd)); +flush_tlb_range (vma, address, address +HPAGE_PMD_SIZE ); +pmd_populate (mm, pmd, pgtable); + ret =1 ; + } +spin_unlock (&mm->page_table_lock); + +return ret; +} + +/* must be called with anon_vma->root->lock hold */ +static void __split_huge_page (struct page *page, +struct anon_vma *anon_vma) +{ +int mapcount, mapcount2; +struct anon_vma_chain *avc; + +BUG_ON (!PageHead (page)); +BUG_ON (PageTail (page)); + + mapcount =0 ; +list_for_each_entry (avc, &anon_vma->head, same_anon_vma) { +struct vm_area_struct *vma = avc->vma; +unsigned long addr =vma_address (page, vma); +if (addr == -EFAULT ) +continue ; + mapcount +=__split_huge_page_splitting (page, vma, addr); + } +BUG_ON (mapcount !=page_mapcount (page)); + +__split_huge_page_refcount (page); + + mapcount2 =0 ; +list_for_each_entry (avc, &anon_vma->head, same_anon_vma) { +struct vm_area_struct *vma = avc->vma; +unsigned long addr =vma_address (page, vma); +if (addr == -EFAULT ) +continue ; + mapcount2 +=__split_huge_page_map (page, vma, addr); + } +BUG_ON (mapcount != mapcount2); +} + +int split_huge_page (struct page *page) +{ +struct anon_vma *anon_vma; +int ret =1 ; + +BUG_ON (!PageAnon (page)); + anon_vma =page_lock_anon_vma (page); +if (!anon_vma) + goto out; + ret =0 ; +if (!PageCompound (page)) + goto out_unlock; + +BUG_ON (!PageSwapBacked (page)); +__split_huge_page (page, anon_vma); + +BUG_ON (PageCompound (page)); +out_unlock: +page_unlock_anon_vma (anon_vma); +out: +return ret; +} + +void __split_huge_page_pmd (struct mm_struct *mm,pmd_t *pmd) +{ +struct page *page; + +spin_lock (&mm->page_table_lock); +if (unlikely (!pmd_trans_huge (*pmd))) { +spin_unlock (&mm->page_table_lock); +return ; + } + page =pmd_page (*pmd); +VM_BUG_ON (!page_count (page)); +get_page (page); +spin_unlock (&mm->page_table_lock); + +split_huge_page (page); + +put_page (page); +BUG_ON (pmd_trans_huge (*pmd)); +}mm/memory.c +42
-7@@ -726,9 +726,9 @@ out_set_pte:return 0 ; } -static int copy_pte_range (struct mm_struct *dst_mm,struct mm_struct *src_mm, -pmd_t *dst_pmd,pmd_t *src_pmd,struct vm_area_struct *vma, -unsigned long addr,unsigned long end) +int copy_pte_range (struct mm_struct *dst_mm,struct mm_struct *src_mm, +pmd_t *dst_pmd,pmd_t *src_pmd,struct vm_area_struct *vma, +unsigned long addr,unsigned long end) {pte_t *orig_src_pte, *orig_dst_pte;pte_t *src_pte, *dst_pte;@@ -802,6 +802,16 @@ static inline int copy_pmd_range(struct src_pmd =pmd_offset (src_pud, addr); do { next = pmd_addr_end(addr, end); + if (pmd_trans_huge(*src_pmd)) { + int err; + err = copy_huge_pmd(dst_mm, src_mm, + dst_pmd, src_pmd, addr, vma); + if (err == -ENOMEM) + return -ENOMEM; + if (!err) + continue; + /* fall through */ + } if (pmd_none_or_clear_bad(src_pmd)) continue; if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,@@ -1004,6 +1014,15 @@ static inline unsigned long zap_pmd_rang pmd =pmd_offset (pud, addr); do { next = pmd_addr_end(addr, end); + if (pmd_trans_huge(*pmd)) { + if (next-addr != HPAGE_PMD_SIZE) + split_huge_page_pmd(vma->vm_mm, pmd); + else if (zap_huge_pmd(tlb, vma, pmd)) { + (*zap_work)--; + continue; + } + /* fall through */ + } if (pmd_none_or_clear_bad(pmd)) { (*zap_work)--; continue;@@ -1280,11 +1299,27 @@ struct page *follow_page(struct vm_area_ pmd =pmd_offset (pud, address);if (pmd_none (*pmd)) goto no_page_table; -if (pmd_huge (*pmd)) { +if (pmd_huge (*pmd) && vma->vm_flags &VM_HUGETLB ) {BUG_ON (flags &FOLL_GET ); page =follow_huge_pmd (mm, address, pmd, flags &FOLL_WRITE ); goto out; } +if (pmd_trans_huge (*pmd)) { +spin_lock (&mm->page_table_lock); +if (likely (pmd_trans_huge (*pmd))) { +if (unlikely (pmd_trans_splitting (*pmd))) { +spin_unlock (&mm->page_table_lock); +wait_split_huge_page (vma->anon_vma, pmd); + }else { + page =follow_trans_huge_pmd (mm, address, + pmd, flags); +spin_unlock (&mm->page_table_lock); + goto out; + } + }else +spin_unlock (&mm->page_table_lock); +/* fall through */ + }if (unlikely (pmd_bad (*pmd))) goto no_page_table;@@ -3141,9 +3176,9 @@ static int do_nonlinear_fault(struct mm_* but allow concurrent faults), and pte mapped but not yet locked. * We return with mmap_sem still held, but pte unmapped and unlocked. */ -static inline int handle_pte_fault (struct mm_struct *mm, -struct vm_area_struct *vma,unsigned long address, -pte_t *pte,pmd_t *pmd,unsigned int flags) +int handle_pte_fault (struct mm_struct *mm, +struct vm_area_struct *vma,unsigned long address, +pte_t *pte,pmd_t *pmd,unsigned int flags) {pte_t entry;spinlock_t *ptl;@@ -3222,9 +3257,40 @@ int handle_mm_fault(struct mm_struct *mm pmd =pmd_alloc (mm, pud, address);if (!pmd)
mm/memory.c int handle_mm_fault(struct mm_struct *mm · line 3266
Quoted 10 lines
mm/memory.c +7
-2return VM_FAULT_OOM ; - pte =pte_alloc_map (mm, vma, pmd, address); -if (!pte) +if (pmd_none (*pmd) &&transparent_hugepage_enabled (vma)) { +if (!vma->vm_ops) +return do_huge_pmd_anonymous_page (mm, vma, address, + pmd, flags); + }else { +pmd_t orig_pmd = *pmd; +barrier ();
What is this barrier for?
Quoted 169 lines
mm/memory.c +26
-0+if (pmd_trans_huge (orig_pmd)) { +if (flags &FAULT_FLAG_WRITE && + !pmd_write (orig_pmd) && + !pmd_trans_splitting (orig_pmd)) +return do_huge_pmd_wp_page (mm, vma, address, + pmd, orig_pmd); +return 0 ; + } + } + +/* +* Use __pte_alloc instead of pte_alloc_map, because we can't +* run pte_offset_map on the pmd, if an huge pmd could +* materialize from under us from a different thread. +*/ +if (unlikely (__pte_alloc (mm, vma, pmd, address)))return VM_FAULT_OOM ; +/* if an huge pmd materialized from under us just retry later */ +if (unlikely (pmd_trans_huge (*pmd))) +return 0 ; +/* +* A regular pmd is established and it can't morph into a huge pmd +* from under us anymore at this point because we hold the mmap_sem +* read mode and khugepaged takes it in write mode. So now it's +* safe to run pte_offset_map(). +*/ + pte =pte_offset_map (pmd, address);return handle_pte_fault (mm, vma, address, pte, pmd, flags); }mm/rmap.c +37
-23@@ -360,7 +360,7 @@ void page_unlock_anon_vma(struct anon_vm* Returns virtual address or -EFAULT if page's index/offset is not * within the range mapped the @vma. */ -static inline unsigned long +inline unsigned long vma_address (struct page *page,struct vm_area_struct *vma) {pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT -PAGE_SHIFT );@@ -435,6 +435,8 @@ pte_t *__page_check_address(struct page pmd =pmd_offset (pud, address);if (!pmd_present (*pmd))return NULL ; +if (pmd_trans_huge (*pmd)) +return NULL ; pte =pte_offset_map (pmd, address);/* Make a quick check before getting the lock */ @@ -489,35 +491,17 @@ int page_referenced_one(struct page *pag unsigned long *vm_flags) {struct mm_struct *mm = vma->vm_mm; -pte_t *pte; -spinlock_t *ptl;int referenced =0 ; - pte =page_check_address (page, mm, address, &ptl,0 ); -if (!pte) - goto out; -/* * Don't want to elevate referenced for mlocked page that gets this far, * in order that it progresses to try_to_unmap and is moved to the * unevictable list. */ if (vma->vm_flags &VM_LOCKED ) { - *mapcount =1 ;/* break early from loop */ + *mapcount =0 ;/* break early from loop */ *vm_flags |=VM_LOCKED ; - goto out_unmap; - } - -if (ptep_clear_flush_young_notify (vma, address, pte)) { -/* -* Don't treat a reference through a sequentially read -* mapping as such. If the page has been used in -* another mapping, we will catch it; if this other -* mapping is already gone, the unmap path will have -* set PG_referenced or activated the page. -*/ -if (likely (!VM_SequentialReadHint (vma))) - referenced++; + goto out; }/* Pretend the page is referenced if the task has the @@ -526,9 +510,39 @@ int page_referenced_one(struct page *pag rwsem_is_locked(&mm->mmap_sem)) referenced++; -out_unmap: +if (unlikely (PageTransHuge (page))) { +pmd_t *pmd; + +spin_lock (&mm->page_table_lock); + pmd =page_check_address_pmd (page, mm, address, +PAGE_CHECK_ADDRESS_PMD_FLAG ); +if (pmd && !pmd_trans_splitting (*pmd) && +pmdp_clear_flush_young_notify (vma, address, pmd)) + referenced++; +spin_unlock (&mm->page_table_lock); + }else { +pte_t *pte; +spinlock_t *ptl; + + pte =page_check_address (page, mm, address, &ptl,0 ); +if (!pte) + goto out; + +if (ptep_clear_flush_young_notify (vma, address, pte)) { +/* +* Don't treat a reference through a sequentially read +* mapping as such. If the page has been used in +* another mapping, we will catch it; if this other +* mapping is already gone, the unmap path will have +* set PG_referenced or activated the page. +*/ +if (likely (!VM_SequentialReadHint (vma))) + referenced++; + } +pte_unmap_unlock (pte, ptl); + } + (*mapcount)--; -pte_unmap_unlock (pte, ptl);if (referenced) *vm_flags |= vma->vm_flags;mm/swap.c +30
-0@@ -465,6 +465,43 @@ void __pagevec_release(struct pagevec *pEXPORT_SYMBOL (__pagevec_release); +/* used by __split_huge_page_refcount() */ +void lru_add_page_tail (struct zone * zone, +struct page *page,struct page *page_tail) +{ +int active; +enum lru_list lru; +const int file =0 ; +struct list_head *head; + +VM_BUG_ON (!PageHead (page)); +VM_BUG_ON (PageCompound (page_tail)); +VM_BUG_ON (PageLRU (page_tail)); +VM_BUG_ON (!spin_is_locked (&zone->lru_lock)); + +SetPageLRU (page_tail); + +if (page_evictable (page_tail,NULL )) { +if (PageActive (page)) { +SetPageActive (page_tail); + active =1 ; + lru =LRU_ACTIVE_ANON ; + }else { + active =0 ; + lru =LRU_INACTIVE_ANON ; + } +update_page_reclaim_stat (zone, page_tail, file, active); +if (likely (PageLRU (page))) + head = page->lru.prev; +else + head = &zone->lru[lru].list;
mm/swap.c void __pagevec_release(struct pagevec *p · line 507
Quoted 10 lines
mm/swap.c +7
-0+__add_page_to_lru_list (zone, page_tail, lru, head); + }else { +SetPageUnevictable (page_tail); +add_page_to_lru_list (zone, page_tail,LRU_UNEVICTABLE ); + } +} +/* * Add the passed pages to the LRU, then drop the caller's refcount * on them. Reinitialises the caller's pagevec.
Other than a few minor questions, these seems very similar to what you had before. There is a lot going on in this patch but I did not find anything wrong.
Signature · 3 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab
Re: [PATCH 30 of 66] transparent hugepage core
Quoted 2 lines · Mel Gorman
All that seems fine to me. Nits in part that are simply not worth calling out. In principal, I Agree With This :)
I didn't understand what is not worth calling out, but I like the end of your sentence ;).
Quoted 14 lines
Review
Quoted 9 lines
(quoted diff) +9
-0+#define wait_split_huge_page(__anon_vma, __pmd) \ + do { \ + pmd_t *____pmd = (__pmd); \ + spin_unlock_wait(&(__anon_vma)->root->lock); \ + /* \ + * spin_unlock_wait() is just a loop in C and so the \ + * CPU can reorder anything around it. \ + */ \ + smp_mb(); \Just a note as I see nothing wrong with this but that's a good spot. The unlock isn't a memory barrier. Out of curiousity, does it really need to be a full barrier or would a write barrier have been enough?
Quoted 2 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ BUG_ON(pmd_trans_splitting(*____pmd) || \ + pmd_trans_huge(*____pmd)); \
spin_unlock reads, the BUG_ON reads, so even if we ignore what happens before and after wait_split_huge_page(), at most it could be a read memory barrier. It can't be a write memory barrier as the spin_unlock_wait would pass it too.
I think it better be a full memory barrier to be sure the writes after wait_split_huge_page return don't happen before spin_unlock_wait. It's hard to see how that could happen though.
Quoted 17 lines
Review
Quoted 13 lines
(quoted diff) +13
-0+ } while (0) +#define HPAGE_PMD_ORDER (HPAGE_PMD_SHIFT-PAGE_SHIFT) +#define HPAGE_PMD_NR (1<<HPAGE_PMD_ORDER) +#if HPAGE_PMD_ORDER > MAX_ORDER +#error "hugepages can't be allocated by the buddy allocator" +#endif + +extern unsigned long vma_address(struct page *page, struct vm_area_struct *vma); +static inline int PageTransHuge(struct page *page) +{ + VM_BUG_ON(PageTail(page)); + return PageHead(page); +}
gfp.hseems an odd place for these. Should the flags go inpage-flags.hand maybe putvma_address()ininternal.h?
Quoted 1 line
Not a biggie.
Cleaned up thanks.
include/linux/huge_mm.h +0 -8
@@ -97,13 +97,6 @@ extern void __split_huge_page_pmd(struct#if HPAGE_PMD_ORDER >MAX_ORDER #error "hugepages can't be allocated by the buddy allocator"#endif - -extern unsigned long vma_address (struct page *page,struct vm_area_struct *vma); -static inline int PageTransHuge (struct page *page) -{ -VM_BUG_ON (PageTail (page)); -return PageHead (page); -}#else /* CONFIG_TRANSPARENT_HUGEPAGE */ #define HPAGE_PMD_SHIFT ({ BUG(); 0; })#define HPAGE_PMD_MASK ({ BUG(); 0; })
@@ -120,7 +113,6 @@ static inline int split_huge_page(struct do { } while (0)#define wait_split_huge_page (__anon_vma, __pmd) \ do { } while (0) -#define PageTransHuge (page) 0#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif /* _LINUX_HUGE_MM_H */
include/linux/page-flags.h +13 -0
@@ -409,6 +409,19 @@ static inline void ClearPageCompound(str#endif /* !PAGEFLAGS_EXTENDED */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static inline int PageTransHuge (struct page *page) +{ +VM_BUG_ON (PageTail (page)); +return PageHead (page); +} +#else +static inline int PageTransHuge (struct page *page) +{ +return 0 ; +} +#endif +#ifdef CONFIG_MMU #define __PG_MLOCKED (1 << PG_mlocked)#else
mm/internal.h +4 -0
@@ -134,6 +134,10 @@ static inline void mlock_migrate_page(st } } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +extern unsigned long vma_address (struct page *page, +struct vm_area_struct *vma); +#endif #else /* !CONFIG_MMU */ static inline int is_mlocked_vma (struct vm_area_struct *v,struct page *p) {
Quoted 19 lines
Quoted 19 lines
include/linux/mm_inline.h +9
-1@@ -20,11 +20,18 @@ static inline int page_is_file_cache(str }static inline void +__add_page_to_lru_list (struct zone *zone,struct page *page,enum lru_list l, +struct list_head *head) +{ +list_add (&page->lru, head); +__inc_zone_state (zone,NR_LRU_BASE + l); +mem_cgroup_add_lru_list (page, l); +} + +static inline void add_page_to_lru_list (struct zone *zone,struct page *page,enum lru_list l) { -list_add (&page->lru, &zone->lru[l].list);
Quoted 8 lines
include/linux/mm_inline.h static inline int page_is_file_cache(str · line 36
Quoted 5 lines
include/linux/mm_inline.h +1
-2-__inc_zone_state (zone,NR_LRU_BASE + l); -mem_cgroup_add_lru_list (page, l); +__add_page_to_lru_list (zone, page, l, &zone->lru[l].list); }Do these really need to be in a public header or can they move to
mm/swap.c?
The above quoted change is a noop as far as the old code is concerned, and moving it to swap.c would alter the old code. I think list_add and __mod_zone_page_state is pretty small and fast so probably it's worth keeping it as inline.
Quoted 7 lines
include/linux/mm_inline.h static inline int page_is_file_cache(str
Quoted 5 lines
include/linux/mm_inline.h +5
-0+static void prepare_pmd_huge_pte (pgtable_t pgtable, +struct mm_struct *mm) +{ +VM_BUG_ON (spin_can_lock (&mm->page_table_lock)); +
assert_spin_locked()?
Changed.
Quoted 13 lines
Quoted 13 lines
include/linux/mm_inline.h +4
-2+int handle_pte_fault (struct mm_struct *mm, +struct vm_area_struct *vma,unsigned long address, +pte_t *pte,pmd_t *pmd,unsigned int flags) {pte_t entry;spinlock_t *ptl;@@ -3222,9 +3257,40 @@ int handle_mm_fault(struct mm_struct *mm pmd =pmd_alloc (mm, pud, address);if (!pmd)return VM_FAULT_OOM ; - pte =pte_alloc_map (mm, vma, pmd, address); -if (!pte) +if (pmd_none (*pmd) &&transparent_hugepage_enabled (vma)) {
Quoted 8 lines
include/linux/mm_inline.h int handle_mm_fault(struct mm_struct *mm · line 3266
Quoted 6 lines
include/linux/mm_inline.h +6
-0+if (!vma->vm_ops) +return do_huge_pmd_anonymous_page (mm, vma, address, + pmd, flags); + }else { +pmd_t orig_pmd = *pmd; +barrier ();What is this barrier for?
This is to be guaranteed gcc doesn't re-read the *pmd after the barrier and it instead always read it from orig_pmd variable on the local kernel stack. gcc doesn't know *pmd can still change from under us until after we take some lock and the code relies on orig_pmd not to change after barrier().
Quoted 5 lines
Other than a few minor questions, these seems very similar to what you had before. There is a lot going on in this patch but I did not find anything wrong.
Acked-by Mel Gorman <[email protected]>
Great thanks!
Andrea
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 update futex compound knowledge
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
Futex code is smarter than most other gup_fast O_DIRECT code and knows about the compound internals. However now doing a put_page(head_page) will not release the pin on the tail page taken by gup-fast, leading to all sort of refcounting bugchecks. Getting a stable head_page is a little tricky.
page_head = page is there because if this is not a tail page it's also the page_head. Only in case this is a tail page, compound_head is called, otherwise it's guaranteed unnecessary. And if it's a tail page compound_head has to run atomically inside irq disabled section __get_user_pages_fast before returning. Otherwise ->first_page won't be a stable pointer.
Disableing irq before __get_user_page_fast and releasing irq after running compound_head is needed because if __get_user_page_fast returns == 1, it means the huge pmd is established and cannot go away from under us. pmdp_splitting_flush_notify in __split_huge_page_splitting will have to wait for local_irq_enable before the IPI delivery can return. This means __split_huge_page_refcount can't be running from under us, and in turn when we run compound_head(page) we're not reading a dangling pointer from tailpage->first_page. Then after we get to stable head page, we are always safe to call compound_lock and after taking the compound lock on head page we can finally re-check if the page returned by gup-fast is still a tail page. in which case we're set and we didn't need to split the hugepage in order to take a futex on it.
---
kernel/futex.c +45 -10
@@ -219,7 +219,7 @@ get_futex_key(u32 __user *uaddr, int fsh {unsigned long address = (unsigned long )uaddr;struct mm_struct *mm = current->mm; -struct page *page; +struct page *page, *page_head;int err;/*
@@ -251,11 +251,46 @@ again:if (err <0 )return err; - page =compound_head (page); -lock_page (page); -if (!page->mapping) { -unlock_page (page); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + page_head = page; +if (unlikely (PageTail (page))) {put_page (page); +/* serialize against __split_huge_page_splitting() */ +local_irq_disable (); +if (likely (__get_user_pages_fast (address,1 ,1 , &page) ==1 )) { + page_head =compound_head (page); +/* +* page_head is valid pointer but we must pin +* it before taking the PG_lock and/or +* PG_compound_lock. The moment we re-enable +* irqs __split_huge_page_splitting() can +* return and the head page can be freed from +* under us. We can't take the PG_lock and/or +* PG_compound_lock on a page that could be +* freed from under us. +*/ +if (page != page_head) { +get_page (page_head); +put_page (page); + } +local_irq_enable (); + }else { +local_irq_enable (); + goto again; + } + } +#else + page_head =compound_head (page); +if (page != page_head) { +get_page (page_head); +put_page (page); + } +#endif + +lock_page (page_head); +if (!page_head->mapping) { +unlock_page (page_head); +put_page (page_head); goto again; }
@@ -266,20 +301,20 @@ again:* it's a read-only handle, it's expected that futexes attach to * the object not the particular process. */ -if (PageAnon (page)) { +if (PageAnon (page_head)) { key->both.offset |=FUT_OFF_MMSHARED ;/* ref taken on mm */ key->private.mm = mm; key->private.address = address; }else { key->both.offset |=FUT_OFF_INODE ;/* inode-based key */ - key->shared.inode = page->mapping->host; - key->shared.pgoff = page->index; + key->shared.inode = page_head->mapping->host; + key->shared.pgoff = page_head->index; }get_futex_key_refs (key); -unlock_page (page); -put_page (page); +unlock_page (page_head); +put_page (page_head);return 0 ; }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 avoid breaking huge pmd invariants in case of vma_adjust failures
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
An huge pmd can only be mapped if the corresponding 2M virtual range is fully contained in the vma. At times the VM calls split_vma twice, if the first split_vma succeeds and the second fail, the first split_vma remains in effect and it's not rolled back. For split_vma or vma_adjust to fail an allocation failure is needed so it's a very unlikely event (the out of memory killer would normally fire before any allocation failure is visible to kernel and userland and if an out of memory condition happens it's unlikely to happen exactly here). Nevertheless it's safer to ensure that no huge pmd can be left around if the vma is adjusted in a way that can't fit hugepages anymore at the new vm_start/vm_end address.
---
include/linux/huge_mm.h +19 -0
@@ -106,6 +106,19 @@ extern void __split_huge_page_pmd(structextern unsigned long vma_address (struct page *page,struct vm_area_struct *vma);extern int hugepage_madvise (unsigned long *vm_flags); +extern void __vma_adjust_trans_huge (struct vm_area_struct *vma, +unsigned long start, +unsigned long end, +long adjust_next); +static inline void vma_adjust_trans_huge (struct vm_area_struct *vma, +unsigned long start, +unsigned long end, +long adjust_next) +{ +if (!vma->anon_vma || vma->vm_ops || vma->vm_file) +return ; +__vma_adjust_trans_huge (vma, start, end, adjust_next); +}static inline int PageTransHuge (struct page *page) {VM_BUG_ON (PageTail (page));
@@ -138,6 +151,12 @@ static inline int hugepage_madvise(unsigBUG_ON (0 );return 0 ; } +static inline void vma_adjust_trans_huge (struct vm_area_struct *vma, +unsigned long start, +unsigned long end, +long adjust_next) +{ +}#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif /* _LINUX_HUGE_MM_H */
mm/huge_memory.c +78 -2
89 lines not on this page · raw message
mm/mmap.c +2 -0
@@ -589,6 +589,8 @@ again: remove_next = 1 + (end > next-> } } +vma_adjust_trans_huge (vma, start, end, adjust_next); +/* * When changing only vma->vm_end, we don't really need anon_vma * lock. This is a fairly rare case by itself, but the anon_vma
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 mprotect: pass vma down to page table walkers
In reply to Andrea ArcangeliFrom: Johannes Weiner <[email protected]>
Flushing the tlb for huge pmds requires the vma's anon_vma, so pass along the vma instead of the mm, we can always get the latter when we need it.
---
mm/mprotect.c +9 -6
@@ -78,7 +78,7 @@ static void change_pte_range(struct mm_spte_unmap_unlock (pte -1 , ptl); } -static inline void change_pmd_range (struct mm_struct *mm,pud_t *pud, +static inline void change_pmd_range (struct vm_area_struct *vma,pud_t *pud,unsigned long addr,unsigned long end,pgprot_t newprot,int dirty_accountable) {
@@ -88,14 +88,15 @@ static inline void change_pmd_range(stru pmd =pmd_offset (pud, addr);do { next =pmd_addr_end (addr, end); -split_huge_page_pmd (mm, pmd); +split_huge_page_pmd (vma->vm_mm, pmd);if (pmd_none_or_clear_bad (pmd))continue ; -change_pte_range (mm, pmd, addr, next, newprot, dirty_accountable); +change_pte_range (vma->vm_mm, pmd, addr, next, newprot, + dirty_accountable); }while (pmd++, addr = next, addr != end); } -static inline void change_pud_range (struct mm_struct *mm,pgd_t *pgd, +static inline void change_pud_range (struct vm_area_struct *vma,pgd_t *pgd,unsigned long addr,unsigned long end,pgprot_t newprot,int dirty_accountable) {
@@ -107,7 +108,8 @@ static inline void change_pud_range(stru next =pud_addr_end (addr, end);if (pud_none_or_clear_bad (pud))continue ; -change_pmd_range (mm, pud, addr, next, newprot, dirty_accountable); +change_pmd_range (vma, pud, addr, next, newprot, + dirty_accountable); }while (pud++, addr = next, addr != end); }
@@ -127,7 +129,8 @@ static void change_protection(struct vm_ next =pgd_addr_end (addr, end);if (pgd_none_or_clear_bad (pgd))continue ; -change_pud_range (mm, pgd, addr, next, newprot, dirty_accountable); +change_pud_range (vma, pgd, addr, next, newprot, + dirty_accountable); }while (pgd++, addr = next, addr != end);flush_tlb_range (vma, start, end); }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 mprotect: transparent huge page support
In reply to Andrea ArcangeliFrom: Johannes Weiner <[email protected]>
Natively handle huge pmds when changing page tables on behalf of mprotect().
I left out update_mmu_cache() because we do not need it on x86 anyway but more importantly the interface works on ptes, not pmds.
---
include/linux/huge_mm.h +2 -0
@@ -22,6 +22,8 @@ extern int zap_huge_pmd(struct mmu_gatheextern int mincore_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd,unsigned long addr,unsigned long end,unsigned char *vec); +extern int change_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd, +unsigned long addr,pgprot_t newprot);enum transparent_hugepage_flag {TRANSPARENT_HUGEPAGE_FLAG ,
mm/huge_memory.c +27 -0
@@ -944,6 +944,33 @@ int mincore_huge_pmd(struct vm_area_strureturn ret; } +int change_huge_pmd (struct vm_area_struct *vma,pmd_t *pmd, +unsigned long addr,pgprot_t newprot) +{ +struct mm_struct *mm = vma->vm_mm; +int ret =0 ; + +spin_lock (&mm->page_table_lock); +if (likely (pmd_trans_huge (*pmd))) { +if (unlikely (pmd_trans_splitting (*pmd))) { +spin_unlock (&mm->page_table_lock); +wait_split_huge_page (vma->anon_vma, pmd); + }else { +pmd_t entry; + + entry =pmdp_get_and_clear (mm, addr, pmd); + entry =pmd_modify (entry, newprot); +set_pmd_at (mm, addr, pmd, entry); +spin_unlock (&vma->vm_mm->page_table_lock); +flush_tlb_range (vma, addr, addr +HPAGE_PMD_SIZE ); + ret =1 ; + } + }else +spin_unlock (&vma->vm_mm->page_table_lock); + +return ret; +} + pmd_t *page_check_address_pmd(struct page *page, struct mm_struct *mm, unsigned long address,
mm/mprotect.c +7 -1
@@ -88,7 +88,13 @@ static inline void change_pmd_range(stru pmd =pmd_offset (pud, addr); do { next = pmd_addr_end(addr, end); - split_huge_page_pmd(vma->vm_mm, pmd); + if (pmd_trans_huge(*pmd)) { + if (next - addr != HPAGE_PMD_SIZE) + split_huge_page_pmd(vma->vm_mm, pmd); + else if (change_huge_pmd(vma, pmd, addr, newprot)) + continue; + /* fall through */ + } if (pmd_none_or_clear_bad(pmd)) continue; change_pte_range(vma->vm_mm, pmd, addr, next, newprot,
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 set recommended min free kbytes
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
If transparent hugepage is enabled initialize min_free_kbytes to an optimal value by default. This moves the hugeadm algorithm in kernel.
---
mm/huge_memory.c +52 -0
@@ -85,6 +85,47 @@ struct khugepaged_scan { .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), }; + +static int set_recommended_min_free_kbytes (void ) +{ +struct zone *zone; +int nr_zones =0 ; +unsigned long recommended_min; +extern int min_free_kbytes; + +if (!test_bit (TRANSPARENT_HUGEPAGE_FLAG , + &transparent_hugepage_flags) && + !test_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , + &transparent_hugepage_flags)) +return 0 ; + +for_each_populated_zone (zone) + nr_zones++; + +/* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */ + recommended_min =HPAGE_PMD_NR * nr_zones *2 ; + +/* +* Make sure that on average at least two pageblocks are almost free +* of another type, one for a migratetype to fall back to and a +* second to avoid subsequent fallbacks of other types There are 3 +* MIGRATE_TYPES we care about. +*/ + recommended_min +=HPAGE_PMD_NR * nr_zones *3 *3 ; + +/* don't ever allow to reserve more than 5% of the lowmem */ + recommended_min =min (recommended_min, + (unsigned long )nr_free_buffer_pages () /20 ); + recommended_min <<= (PAGE_SHIFT -10 ); + +if (recommended_min > min_free_kbytes) { + min_free_kbytes = recommended_min; +setup_per_zone_wmarks (); + } +return 0 ; +} +late_initcall (set_recommended_min_free_kbytes); +static int start_khugepaged (void ) {int err =0 ;
@@ -108,6 +149,8 @@ static int start_khugepaged(void)mutex_unlock (&khugepaged_mutex);if (wakeup)wake_up_interruptible (&khugepaged_wait); + +set_recommended_min_free_kbytes (); }else /* wakeup to exit */ wake_up_interruptible (&khugepaged_wait);
@@ -177,6 +220,13 @@ static ssize_t enabled_store(struct kobj ret = err; } +if (ret >0 && + (test_bit (TRANSPARENT_HUGEPAGE_FLAG , + &transparent_hugepage_flags) || +test_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , + &transparent_hugepage_flags))) +set_recommended_min_free_kbytes (); +return ret; } static struct kobj_attribute enabled_attr =
@@ -464,6 +514,8 @@ static int __init hugepage_init(void)start_khugepaged (); +set_recommended_min_free_kbytes (); + out:return err; }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 51 of 66] set recommended min free kbytes
Quoted 25 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
If transparent hugepage is enabled initialize
min_free_kbytesto an optimal value by default. This moves the hugeadm algorithm in kernel.Signed-off-by Andrea Arcangeli <[email protected]>---
mm/huge_memory.c +10
-0@@ -85,6 +85,47 @@ struct khugepaged_scan { .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), }; + +static int set_recommended_min_free_kbytes (void ) +{ +struct zone *zone; +int nr_zones =0 ; +unsigned long recommended_min; +extern int min_free_kbytes; + + if (!test_bit(TRANSPARENT_HUGEPAGE_FLAG, + &transparent_hugepage_flags) &&
mm/huge_memory.c struct khugepaged_scan { · line 107
Quoted 10 lines
mm/huge_memory.c +10
-0+ !test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, + &transparent_hugepage_flags)) +return 0 ; + +for_each_populated_zone (zone) + nr_zones++; + +/* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */ + recommended_min =HPAGE_PMD_NR * nr_zones *2 ; +
The really important value is pageblock_nr_pages here. It'll just happen to work on x86 and x86-64 but anti-fragmentation is really about pageblocks, not PMDs.
mm/huge_memory.c struct khugepaged_scan { · line 115
Quoted 8 lines
mm/huge_memory.c +8
-0+/* +* Make sure that on average at least two pageblocks are almost free +* of another type, one for a migratetype to fall back to and a +* second to avoid subsequent fallbacks of other types There are 3 +* MIGRATE_TYPES we care about. +*/ + recommended_min +=HPAGE_PMD_NR * nr_zones *3 *3 ; +
Same on the use of pageblock_nr_pages. Also, you can replace 3 with MIGRATE_PCPTYPES.
mm/huge_memory.c struct khugepaged_scan { · line 124
Quoted 9 lines
mm/huge_memory.c +9
-0+/* don't ever allow to reserve more than 5% of the lowmem */ + recommended_min =min (recommended_min, + (unsigned long )nr_free_buffer_pages () /20 ); + recommended_min <<= (PAGE_SHIFT -10 ); + +if (recommended_min > min_free_kbytes) { + min_free_kbytes = recommended_min; +setup_per_zone_wmarks (); + }
The timing this is called is important. Would you mind doing a quick debugging check by adding a printk to setup_zone_migrate_reserve() to ensure MIGRATE_RESERVE is getting set on sensible pageblocks? (see where the comment Suitable for reserving if this block is movable is) If MIGRATE_RESERVE blocks are not being created in a sensible fashion, atomic high-order allocations will suffer in mysterious ways.
SEtting the higher min free kbytes from userspace happens to work because the system is initialised and MIGRATE_MOVABLE exists but that might not be the case when automatically set like this patch.
Quoted 40 lines
mm/huge_memory.c +15
-0+return 0 ; +} +late_initcall (set_recommended_min_free_kbytes); +static int start_khugepaged (void ) {int err =0 ;@@ -108,6 +149,8 @@ static int start_khugepaged(void)mutex_unlock (&khugepaged_mutex);if (wakeup)wake_up_interruptible (&khugepaged_wait); + +set_recommended_min_free_kbytes (); }else /* wakeup to exit */ wake_up_interruptible (&khugepaged_wait);@@ -177,6 +220,13 @@ static ssize_t enabled_store(struct kobj ret = err; } +if (ret >0 && + (test_bit (TRANSPARENT_HUGEPAGE_FLAG , + &transparent_hugepage_flags) || +test_bit (TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG , + &transparent_hugepage_flags))) +set_recommended_min_free_kbytes (); +return ret; } static struct kobj_attribute enabled_attr =@@ -464,6 +514,8 @@ static int __init hugepage_init(void)start_khugepaged (); +set_recommended_min_free_kbytes (); + out:return err; }
Signature · 3 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab
Re: [PATCH 51 of 66] set recommended min free kbytes
Quoted 38 lines · Mel Gorman
Review
Quoted 3 lines
(quoted diff) +3
-0+ /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */ + recommended_min = HPAGE_PMD_NR * nr_zones * 2; +The really important value is
pageblock_nr_pageshere. It'll just happen to work on x86 and x86-64 but anti-fragmentation is really about pageblocks, not PMDs.Review
Quoted 8 lines
(quoted diff) +8
-0+ /* + * Make sure that on average at least two pageblocks are almost free + * of another type, one for a migratetype to fall back to and a + * second to avoid subsequent fallbacks of other types There are 3 + * MIGRATE_TYPES we care about. + */ + recommended_min += HPAGE_PMD_NR * nr_zones * 3 * 3; +Same on the use of
pageblock_nr_pages. Also, you can replace 3 withMIGRATE_PCPTYPES.Review
Quoted 9 lines
(quoted diff) +9
-0+ /* don't ever allow to reserve more than 5% of the lowmem */ + recommended_min = min(recommended_min, + (unsigned long) nr_free_buffer_pages() / 20); + recommended_min <<= (PAGE_SHIFT-10); + + if (recommended_min > min_free_kbytes) { + min_free_kbytes = recommended_min; + setup_per_zone_wmarks(); + }The timing this is called is important. Would you mind doing a quick debugging check by adding a printk to
setup_zone_migrate_reserve()to ensureMIGRATE_RESERVEis getting set on sensible pageblocks? (see where the comment Suitable for reserving if this block is movable is) IfMIGRATE_RESERVEblocks are not being created in a sensible fashion, atomic high-order allocations will suffer in mysterious ways.
Quoted 3 lines
SEtting the higher min free kbytes from userspace happens to work because the system is initialised and
MIGRATE_MOVABLEexists but that might not be the case when automatically set like this patch.
When min_free_kbytes doesn't need to be increased (like huge system with lots of ram) setup_per_zone_wmarks wasn't called in the late_initcall where this code runs, and the original setup_per_zone_wmarks apparently wasn't calling setup_zone_migrate_reserve (how can it be?).
Anyway now I patched it like this and it seems to work properly with the unconditional setup_per_zone_wmarks in late_initcall(set_recommended_min_free_kbytes).
mm/huge_memory.c +5 -5
@@ -103,7 +103,7 @@ static int set_recommended_min_free_kbyt nr_zones++;/* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */ - recommended_min =HPAGE_PMD_NR * nr_zones *2 ; + recommended_min = pageblock_nr_pages * nr_zones *2 ;/* * Make sure that on average at least two pageblocks are almost free
@@ -111,17 +111,17 @@ static int set_recommended_min_free_kbyt* second to avoid subsequent fallbacks of other types There are 3 * MIGRATE_TYPES we care about. */ - recommended_min +=HPAGE_PMD_NR * nr_zones *3 *3 ; + recommended_min += pageblock_nr_pages * nr_zones * +MIGRATE_PCPTYPES *MIGRATE_PCPTYPES ;/* don't ever allow to reserve more than 5% of the lowmem */ recommended_min =min (recommended_min, (unsigned long )nr_free_buffer_pages () /20 ); recommended_min <<= (PAGE_SHIFT -10 ); -if (recommended_min > min_free_kbytes) { +if (recommended_min > min_free_kbytes) min_free_kbytes = recommended_min; -setup_per_zone_wmarks (); - } +setup_per_zone_wmarks ();return 0 ; }late_initcall (set_recommended_min_free_kbytes);
mm/page_alloc.c +2 -0
@@ -3188,6 +3188,7 @@ static void setup_zone_migrate_reserve(s*/ reserve =min (2 , reserve); +printk ("reserve start %d\n" , reserve);for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {if (!pfn_valid (pfn))continue ;
@@ -3226,6 +3227,7 @@ static void setup_zone_migrate_reserve(smove_freepages_block (zone, page,MIGRATE_MOVABLE ); } } +printk ("reserve end %d\n" , reserve); }/*
hugeadm with CONFIG_TRANSPARENT_HUGEPAGE=n leads to:
reserve start 1
reserve end 0
reserve start 2
reserve end 0
reserve start 2
reserve end 0
reserve start 0
reserve end 0
reserve start 0
reserve end 0
reserve start 0
reserve end 0
reserve start 2
reserve end 0
reserve start 0
reserve end 0
With CONFIG_TRANSPARENT_HUGEPAGE=Y I boot I see:
reserve start 1
reserve end 0
reserve start 2
reserve end 0
reserve start 2
reserve end 0
reserve start 0
reserve end 0
reserve start 0
reserve end 0
reserve start 0
reserve end 0
reserve start 2
reserve end 0
reserve start 0
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 transparent hugepage config choice
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
Allow to choose between the always|madvise default for page faults and khugepaged at config time. madvise guarantees zero risk of higher memory footprint for applications (applications using madvise(MADV_HUGEPAGE) won't risk to use any more memory by backing their virtual regions with hugepages).
Initially set the default to N and don't depend on EMBEDDED.
---
mm/Kconfig +25 -2
@@ -303,9 +303,8 @@ config NOMMU_INITIAL_TRIM_EXCESS See Documentation/nommu-mmap.txt for more information. config TRANSPARENT_HUGEPAGE - bool "Transparent Hugepage Support" if EMBEDDED + bool "Transparent Hugepage Support" depends on X86 && MMU - default y help Transparent Hugepages allows the kernel to use huge pages and huge tlb transparently to the applications whenever possible.
@@ -316,6 +315,30 @@ config TRANSPARENT_HUGEPAGE If memory constrained on embedded, you may want to say N. +choice + prompt "Transparent Hugepage Support sysfs defaults" + depends on TRANSPARENT_HUGEPAGE + default TRANSPARENT_HUGEPAGE_ALWAYS + help + Selects the sysfs defaults for Transparent Hugepage Support. + + config TRANSPARENT_HUGEPAGE_ALWAYS + bool "always" + help + Enabling Transparent Hugepage always, can increase the + memory footprint of applications without a guaranteed + benefit but it will work automatically for all applications. + + config TRANSPARENT_HUGEPAGE_MADVISE + bool "madvise" + help + Enabling Transparent Hugepage madvise, will only provide a + performance improvement benefit to the applications using + madvise(MADV_HUGEPAGE) but it won't risk to increase the + memory footprint of applications without a guaranteed + benefit. +endchoice + # # UP and nommu archs use km based percpu allocator #
mm/huge_memory.c +5 -0
@@ -27,7 +27,12 @@* allocations. */ unsigned long transparent_hugepage_flags__read_mostly = +#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS (1 <<TRANSPARENT_HUGEPAGE_FLAG )| +#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE + (1 <<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG )| +#endif (1 <<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG )| (1 <<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG );
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 use compaction for GFP_ATOMIC order > 0
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
This takes advantage of memory compaction to properly generate pages of order > 0 if regular page reclaim fails and priority level becomes more severe and we don't reach the proper watermarks.
---
include/linux/compaction.h +13 -0
@@ -11,6 +11,9 @@/* The full zone was compacted */ #define COMPACT_COMPLETE 3 +#define COMPACT_MODE_DIRECT_RECLAIM 0 +#define COMPACT_MODE_KSWAPD 1 +#ifdef CONFIG_COMPACTION extern int sysctl_compact_memory; extern int sysctl_compaction_handler(struct ctl_table *table, int write,
@@ -20,6 +23,9 @@ extern int sysctl_extfrag_handler(struct void __user *buffer, size_t *length, loff_t *ppos);extern int fragmentation_index (struct zone *zone,unsigned int order); +extern unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode);extern unsigned long try_to_compact_pages (struct zonelist *zonelist,int order,gfp_t gfp_mask,nodemask_t *mask);
@@ -59,6 +65,13 @@ static inline unsigned long try_to_compareturn COMPACT_CONTINUE ; } +static inline unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode) +{ +return COMPACT_CONTINUE ; +} +static inline void defer_compaction (struct zone *zone) { }
mm/compaction.c +27 -6
@@ -38,6 +38,8 @@ struct compact_control {unsigned int order;/* order a direct compactor needs */ int migratetype;/* MOVABLE, RECLAIMABLE etc */ struct zone *zone; + +int compact_mode; };static unsigned long release_freepages (struct list_head *freelist)
@@ -357,10 +359,10 @@ static void update_nr_listpages(struct c }static int compact_finished (struct zone *zone, -struct compact_control *cc) +struct compact_control *cc) {unsigned int order; -unsigned long watermark =low_wmark_pages (zone) + (1 << cc->order); +unsigned long watermark;if (fatal_signal_pending (current))return COMPACT_PARTIAL ;
@@ -370,12 +372,27 @@ static int compact_finished(struct zonereturn COMPACT_COMPLETE ;/* Compaction run is not finished if the watermark is not met */ +if (cc->compact_mode !=COMPACT_MODE_KSWAPD ) + watermark =low_wmark_pages (zone); +else + watermark =high_wmark_pages (zone); + watermark += (1 << cc->order); +if (!zone_watermark_ok (zone, cc->order, watermark,0 ,0 ))return COMPACT_CONTINUE ;if (cc->order ==-1 )return COMPACT_CONTINUE ; +/* +* Generating only one page of the right order is not enough +* for kswapd, we must continue until we're above the high +* watermark as a pool for high order GFP_ATOMIC allocations +* too. +*/ +if (cc->compact_mode ==COMPACT_MODE_KSWAPD ) +return COMPACT_CONTINUE ; +/* Direct compactor: Is a suitable page free? */ for (order = cc->order; order <MAX_ORDER ; order++) {/* Job done if page is free of the right migratetype */
@@ -433,8 +450,9 @@ static int compact_zone(struct zone *zonreturn ret; } -static unsigned long compact_zone_order (struct zone *zone, -int order,gfp_t gfp_mask) +unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode) {struct compact_control cc = { .nr_freepages =0 ,
@@ -442,6 +460,7 @@ static unsigned long compact_zone_order( .order = order, .migratetype = allocflags_to_migratetype(gfp_mask), .zone = zone, + .compact_mode = compact_mode, };INIT_LIST_HEAD (&cc.freepages);INIT_LIST_HEAD (&cc.migratepages);
@@ -476,7 +495,7 @@ unsigned long try_to_compact_pages(struc* made because an assumption is made that the page allocator can satisfy * the "cheaper" orders without taking special steps */ -if (order <=PAGE_ALLOC_COSTLY_ORDER || !may_enter_fs || !may_perform_io) +if (!order || !may_enter_fs || !may_perform_io)return rc;count_vm_event (COMPACTSTALL );
@@ -517,7 +536,8 @@ unsigned long try_to_compact_pages(strucbreak ; } - status =compact_zone_order (zone, order, gfp_mask); + status =compact_zone_order (zone, order, gfp_mask, +COMPACT_MODE_DIRECT_RECLAIM ); rc =max (status, rc);if (zone_watermark_ok (zone, order, watermark,0 ,0 ))
@@ -547,6 +567,7 @@ static int compact_node(int nid) .nr_freepages = 0, .nr_migratepages = 0, .order = -1, + .compact_mode = COMPACT_MODE_DIRECT_RECLAIM, }; zone = &pgdat->node_zones[zoneid];
mm/vmscan.c +20 -1
@@ -40,6 +40,7 @@#include <linux/memcontrol.h> #include <linux/delayacct.h> #include <linux/sysctl.h> +#include <linux/compaction.h> #include <asm/tlbflush.h> #include <asm/div64.h>
@@ -2254,6 +2255,7 @@ loop_again:* cause too much scanning of the lower zones. */ for (i =0 ; i <= end_zone; i++) { +int compaction;struct zone *zone = pgdat->node_zones + i;int nr_slab;
@@ -2283,9 +2285,26 @@ loop_again: lru_pages); sc.nr_reclaimed += reclaim_state->reclaimed_slab; total_scanned += sc.nr_scanned; + + compaction =0 ; +if (order && +zone_watermark_ok (zone,0 , +high_wmark_pages (zone), + end_zone,0 ) && + !zone_watermark_ok (zone, order, +high_wmark_pages (zone), + end_zone,0 )) { +compact_zone_order (zone, + order, + sc.gfp_mask, +COMPACT_MODE_KSWAPD ); + compaction =1 ; + } +if (zone->all_unreclaimable)continue ; -if (nr_slab ==0 && !zone_reclaimable (zone)) +if (!compaction && nr_slab ==0 && + !zone_reclaimable (zone)) zone->all_unreclaimable =1 ;/* * If we've done a decent amount of scanning and
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 61 of 66] use compaction for GFP_ATOMIC order > 0
Quoted 7 lines
From: Andrea Arcangeli <[email protected]>
This takes advantage of memory compaction to properly generate pages of order > 0 if regular page reclaim fails and priority level becomes more severe and we don't reach the proper watermarks.
Signed-off-by Andrea Arcangeli <[email protected]>
First, I don't think this patch is related to GFP_ATOMIC. So, I think the patch title is a bit misleading.
Second, this patch has two changes. 1) remove PAGE_ALLOC_COSTLY_ORDER threshold 2) implement background compaction. please separate them.
Third, This patch makes a lot of PFN order page scan and churn LRU aggressively. I'm not sure this aggressive lru shuffling is safe and works effective. I hope you provide some demonstration and/or show benchmark result.
Thanks.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 61 of 66] use compaction for GFP_ATOMIC order > 0
Quoted 6 lines · KOSAKI Motohiro
Quoted 6 lines
From: Andrea Arcangeli <[email protected]>
This takes advantage of memory compaction to properly generate pages of order > 0 if regular page reclaim fails and priority level becomes more severe and we don't reach the proper watermarks.
Quoted 7 lines
Quoted 1 line
Signed-off-by Andrea Arcangeli <[email protected]>First, I don't think this patch is related to
GFP_ATOMIC. So, I think the patch title is a bit misleading.Second, this patch has two changes. 1) remove
PAGE_ALLOC_COSTLY_ORDERthreshold 2) implement background compaction. please separate them.
Well the subject isn't entirely misleading: background compaction in kswapd is only for the GFP_ATOMIC so GFP_ATOMIC order >0 allocations are definitely related to this patch.
Then I ended up then allowing compaction for all order of allocations as it doesn't make sense to fail order 2 for the kernel stack and succeed order 9 but it's true I can split that off, I will split it for #33, thanks for allowing me to clean up the stuff better.
Quoted 4 lines
Third, This patch makes a lot of PFN order page scan and churn LRU aggressively. I'm not sure this aggressive lru shuffling is safe and works effective. I hope you provide some demonstration and/or show benchmark result.
The patch will increase the amount of compaction for GFP_ATOMIC order
Quoted 1 line
0, but it won't alter the amount of free pages in the system, but
it'll satisfy the in-function-of order watermarks that are right now ignored. If user asked GFP_ATOMIC order > 0, this is what it asks, it's up to the user not to ask for it if it's not worthwhile. If user doesn't want this but it just wants to poll the LRU it should use GFP_ATOMIC|__GFP_NO_KSWAPD.
The benchmark results I don't have at the moment but this has been tested with tg3 with jumbo packets that trigger order 2 allocation and no degradation was noticed. To be fair it didn't significantly improve the amount of order 2 (9046 bytes large skb) allocated from irq though, but I thought it was good idea to keep it in case there are less aggressive/frequent users doing similar things.
Overall the more important part of the patch is the point 2) that I can make it cleaner by splitting it off as you noticed and I will do it.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 61 of 66] use compaction for GFP_ATOMIC order > 0
In reply to Andrea ArcangeliQuoted 6 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
This takes advantage of memory compaction to properly generate pages of order > 0 if regular page reclaim fails and priority level becomes more severe and we don't reach the proper watermarks.
I don't think this is related to THP although I see what you're doing. It should be handled on its own. I'd also wonder if some of the tg3 failures are due to MIGRATE_RESERVE not being set properly when min_free_kbytes is automatically resized.
Quoted 188 lines
Signed-off-by Andrea Arcangeli <[email protected]>---
include/linux/compaction.h +13
-0@@ -11,6 +11,9 @@/* The full zone was compacted */ #define COMPACT_COMPLETE 3 +#define COMPACT_MODE_DIRECT_RECLAIM 0 +#define COMPACT_MODE_KSWAPD 1 +#ifdef CONFIG_COMPACTION extern int sysctl_compact_memory; extern int sysctl_compaction_handler(struct ctl_table *table, int write,@@ -20,6 +23,9 @@ extern int sysctl_extfrag_handler(struct void __user *buffer, size_t *length, loff_t *ppos);extern int fragmentation_index (struct zone *zone,unsigned int order); +extern unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode);extern unsigned long try_to_compact_pages (struct zonelist *zonelist,int order,gfp_t gfp_mask,nodemask_t *mask);@@ -59,6 +65,13 @@ static inline unsigned long try_to_compareturn COMPACT_CONTINUE ; } +static inline unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode) +{ +return COMPACT_CONTINUE ; +} +static inline void defer_compaction (struct zone *zone) { }mm/compaction.c +27
-6@@ -38,6 +38,8 @@ struct compact_control {unsigned int order;/* order a direct compactor needs */ int migratetype;/* MOVABLE, RECLAIMABLE etc */ struct zone *zone; + +int compact_mode; };static unsigned long release_freepages (struct list_head *freelist)@@ -357,10 +359,10 @@ static void update_nr_listpages(struct c }static int compact_finished (struct zone *zone, -struct compact_control *cc) +struct compact_control *cc) {unsigned int order; -unsigned long watermark =low_wmark_pages (zone) + (1 << cc->order); +unsigned long watermark;if (fatal_signal_pending (current))return COMPACT_PARTIAL ;@@ -370,12 +372,27 @@ static int compact_finished(struct zonereturn COMPACT_COMPLETE ;/* Compaction run is not finished if the watermark is not met */ +if (cc->compact_mode !=COMPACT_MODE_KSWAPD ) + watermark =low_wmark_pages (zone); +else + watermark =high_wmark_pages (zone); + watermark += (1 << cc->order); +if (!zone_watermark_ok (zone, cc->order, watermark,0 ,0 ))return COMPACT_CONTINUE ;if (cc->order ==-1 )return COMPACT_CONTINUE ; +/* +* Generating only one page of the right order is not enough +* for kswapd, we must continue until we're above the high +* watermark as a pool for high order GFP_ATOMIC allocations +* too. +*/ +if (cc->compact_mode ==COMPACT_MODE_KSWAPD ) +return COMPACT_CONTINUE ; +/* Direct compactor: Is a suitable page free? */ for (order = cc->order; order <MAX_ORDER ; order++) {/* Job done if page is free of the right migratetype */ @@ -433,8 +450,9 @@ static int compact_zone(struct zone *zonreturn ret; } -static unsigned long compact_zone_order (struct zone *zone, -int order,gfp_t gfp_mask) +unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode) {struct compact_control cc = { .nr_freepages =0 ,@@ -442,6 +460,7 @@ static unsigned long compact_zone_order( .order = order, .migratetype = allocflags_to_migratetype(gfp_mask), .zone = zone, + .compact_mode = compact_mode, };INIT_LIST_HEAD (&cc.freepages);INIT_LIST_HEAD (&cc.migratepages);@@ -476,7 +495,7 @@ unsigned long try_to_compact_pages(struc* made because an assumption is made that the page allocator can satisfy * the "cheaper" orders without taking special steps */ -if (order <=PAGE_ALLOC_COSTLY_ORDER || !may_enter_fs || !may_perform_io) +if (!order || !may_enter_fs || !may_perform_io)return rc;count_vm_event (COMPACTSTALL );@@ -517,7 +536,8 @@ unsigned long try_to_compact_pages(strucbreak ; } - status =compact_zone_order (zone, order, gfp_mask); + status =compact_zone_order (zone, order, gfp_mask, +COMPACT_MODE_DIRECT_RECLAIM ); rc =max (status, rc);if (zone_watermark_ok (zone, order, watermark,0 ,0 ))@@ -547,6 +567,7 @@ static int compact_node(int nid) .nr_freepages = 0, .nr_migratepages = 0, .order = -1, + .compact_mode = COMPACT_MODE_DIRECT_RECLAIM, }; zone = &pgdat->node_zones[zoneid];mm/vmscan.c +20
-1@@ -40,6 +40,7 @@#include <linux/memcontrol.h> #include <linux/delayacct.h> #include <linux/sysctl.h> +#include <linux/compaction.h> #include <asm/tlbflush.h> #include <asm/div64.h> @@ -2254,6 +2255,7 @@ loop_again:* cause too much scanning of the lower zones. */ for (i =0 ; i <= end_zone; i++) { +int compaction;struct zone *zone = pgdat->node_zones + i;int nr_slab;@@ -2283,9 +2285,26 @@ loop_again: lru_pages); sc.nr_reclaimed += reclaim_state->reclaimed_slab; total_scanned += sc.nr_scanned; + + compaction =0 ; +if (order && +zone_watermark_ok (zone,0 , +high_wmark_pages (zone), + end_zone,0 ) && + !zone_watermark_ok (zone, order, +high_wmark_pages (zone), + end_zone,0 )) { +compact_zone_order (zone, + order, + sc.gfp_mask, +COMPACT_MODE_KSWAPD ); + compaction =1 ; + } +if (zone->all_unreclaimable)continue ; -if (nr_slab ==0 && !zone_reclaimable (zone)) +if (!compaction && nr_slab ==0 && + !zone_reclaimable (zone)) zone->all_unreclaimable =1 ;/* * If we've done a decent amount of scanning and
Signature · 10 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 61 of 66] use compaction for GFP_ATOMIC order > 0
Quoted 4 lines · Mel Gorman
I don't think this is related to THP although I see what you're doing. It should be handled on its own. I'd also wonder if some of the tg3 failures are due to
MIGRATE_RESERVEnot being set properly whenmin_free_kbytesis automatically resized.
The failures also happened on older kernels except they wouldn't print it in the kernel logs, but you're right we may get better with migrate reserve enabled also on huge systems (it seems hugeadm --set_recommended-min_free_kbytes was doing a little more than its kernel counterpart with large systems with tons of ram, now fixed).
My status as far as this patch is concerned:
=========
Subject: use compaction for all allocation orders
From: Andrea Arcangeli <[email protected]>
It makes no sense not to enable compaction for small order pages as we don't want to end up with bad order 2 allocations and good and graceful order 9 allocations.
---
mm/compaction.c +1 -1
@@ -476,7 +495,7 @@ unsigned long try_to_compact_pages(struc* made because an assumption is made that the page allocator can satisfy * the "cheaper" orders without taking special steps */ -if (order <=PAGE_ALLOC_COSTLY_ORDER || !may_enter_fs || !may_perform_io) +if (!order || !may_enter_fs || !may_perform_io)return rc;count_vm_event (COMPACTSTALL );
===========
Subject: use compaction in kswapd for GFP_ATOMIC order > 0
From: Andrea Arcangeli <[email protected]>
This takes advantage of memory compaction to properly generate pages of order > 0 if regular page reclaim fails and priority level becomes more severe and we don't reach the proper watermarks.
---
include/linux/compaction.h +13 -0
@@ -11,6 +11,9 @@/* The full zone was compacted */ #define COMPACT_COMPLETE 3 +#define COMPACT_MODE_DIRECT_RECLAIM 0 +#define COMPACT_MODE_KSWAPD 1 +#ifdef CONFIG_COMPACTION extern int sysctl_compact_memory; extern int sysctl_compaction_handler(struct ctl_table *table, int write,
@@ -20,6 +23,9 @@ extern int sysctl_extfrag_handler(struct void __user *buffer, size_t *length, loff_t *ppos);extern int fragmentation_index (struct zone *zone,unsigned int order); +extern unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode);extern unsigned long try_to_compact_pages (struct zonelist *zonelist,int order,gfp_t gfp_mask,nodemask_t *mask);
@@ -59,6 +65,13 @@ static inline unsigned long try_to_compareturn COMPACT_CONTINUE ; } +static inline unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode) +{ +return COMPACT_CONTINUE ; +} +static inline void defer_compaction (struct zone *zone) { }
mm/compaction.c +26 -5
@@ -38,6 +38,8 @@ struct compact_control {unsigned int order;/* order a direct compactor needs */ int migratetype;/* MOVABLE, RECLAIMABLE etc */ struct zone *zone; + +int compact_mode; };static unsigned long release_freepages (struct list_head *freelist)
@@ -357,10 +359,10 @@ static void update_nr_listpages(struct c }static int compact_finished (struct zone *zone, -struct compact_control *cc) +struct compact_control *cc) {unsigned int order; -unsigned long watermark =low_wmark_pages (zone) + (1 << cc->order); +unsigned long watermark;if (fatal_signal_pending (current))return COMPACT_PARTIAL ;
@@ -370,12 +372,27 @@ static int compact_finished(struct zonereturn COMPACT_COMPLETE ;/* Compaction run is not finished if the watermark is not met */ +if (cc->compact_mode !=COMPACT_MODE_KSWAPD ) + watermark =low_wmark_pages (zone); +else + watermark =high_wmark_pages (zone); + watermark += (1 << cc->order); +if (!zone_watermark_ok (zone, cc->order, watermark,0 ,0 ))return COMPACT_CONTINUE ;if (cc->order ==-1 )return COMPACT_CONTINUE ; +/* +* Generating only one page of the right order is not enough +* for kswapd, we must continue until we're above the high +* watermark as a pool for high order GFP_ATOMIC allocations +* too. +*/ +if (cc->compact_mode ==COMPACT_MODE_KSWAPD ) +return COMPACT_CONTINUE ; +/* Direct compactor: Is a suitable page free? */ for (order = cc->order; order <MAX_ORDER ; order++) {/* Job done if page is free of the right migratetype */
@@ -433,8 +450,9 @@ static int compact_zone(struct zone *zonreturn ret; } -static unsigned long compact_zone_order (struct zone *zone, -int order,gfp_t gfp_mask) +unsigned long compact_zone_order (struct zone *zone, +int order,gfp_t gfp_mask, +int compact_mode) {struct compact_control cc = { .nr_freepages =0 ,
@@ -442,6 +460,7 @@ static unsigned long compact_zone_order( .order = order, .migratetype = allocflags_to_migratetype(gfp_mask), .zone = zone, + .compact_mode = compact_mode, };INIT_LIST_HEAD (&cc.freepages);INIT_LIST_HEAD (&cc.migratepages);
@@ -517,7 +536,8 @@ unsigned long try_to_compact_pages(strucbreak ; } - status =compact_zone_order (zone, order, gfp_mask); + status =compact_zone_order (zone, order, gfp_mask, +COMPACT_MODE_DIRECT_RECLAIM ); rc =max (status, rc);if (zone_watermark_ok (zone, order, watermark,0 ,0 ))
@@ -547,6 +567,7 @@ static int compact_node(int nid) .nr_freepages = 0, .nr_migratepages = 0, .order = -1, + .compact_mode = COMPACT_MODE_DIRECT_RECLAIM, }; zone = &pgdat->node_zones[zoneid];
mm/vmscan.c +20 -1
@@ -40,6 +40,7 @@#include <linux/memcontrol.h> #include <linux/delayacct.h> #include <linux/sysctl.h> +#include <linux/compaction.h> #include <asm/tlbflush.h> #include <asm/div64.h>
@@ -2148,6 +2149,7 @@ loop_again:* cause too much scanning of the lower zones. */ for (i =0 ; i <= end_zone; i++) { +int compaction;struct zone *zone = pgdat->node_zones + i;int nr_slab;
@@ -2177,9 +2179,26 @@ loop_again: lru_pages); sc.nr_reclaimed += reclaim_state->reclaimed_slab; total_scanned += sc.nr_scanned; + + compaction =0 ; +if (order && +zone_watermark_ok (zone,0 , +high_wmark_pages (zone), + end_zone,0 ) && + !zone_watermark_ok (zone, order, +high_wmark_pages (zone), + end_zone,0 )) { +compact_zone_order (zone, + order, + sc.gfp_mask, +COMPACT_MODE_KSWAPD ); + compaction =1 ; + } +if (zone->all_unreclaimable)continue ; -if (nr_slab ==0 && !zone_reclaimable (zone)) +if (!compaction && nr_slab ==0 && + !zone_reclaimable (zone)) zone->all_unreclaimable =1 ;/* * If we've done a decent amount of scanning and
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 add debug checks for mapcount related invariants
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
Add debug checks for invariants that if broken could lead to mapcount vs page_mapcount debug checks to trigger later in split_huge_page.
---
mm/memory.c +4 -2
@@ -804,6 +804,7 @@ static inline int copy_pmd_range(struct next =pmd_addr_end (addr, end);if (pmd_trans_huge (*src_pmd)) {int err; +VM_BUG_ON (next-addr !=HPAGE_PMD_SIZE ); err =copy_huge_pmd (dst_mm, src_mm, dst_pmd, src_pmd, addr, vma);if (err == -ENOMEM )
@@ -1015,9 +1016,10 @@ static inline unsigned long zap_pmd_rang do { next = pmd_addr_end(addr, end); if (pmd_trans_huge(*pmd)) { - if (next-addr != HPAGE_PMD_SIZE) + if (next-addr != HPAGE_PMD_SIZE) { + VM_BUG_ON(!rwsem_is_locked(&tlb->mm->mmap_sem)); split_huge_page_pmd(vma->vm_mm, pmd); - else if (zap_huge_pmd(tlb, vma, pmd)) { + } else if (zap_huge_pmd(tlb, vma, pmd)) { (*zap_work)--; continue; }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 madvise(MADV_HUGEPAGE)
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
Add madvise MADV_HUGEPAGE to mark regions that are important to be hugepage backed. Return -EINVAL if the vma is not of an anonymous type, or the feature isn't built into the kernel. Never silently return success.
---
include/linux/huge_mm.h +6 -0
@@ -99,6 +99,7 @@ extern void __split_huge_page_pmd(struct#endif extern unsigned long vma_address (struct page *page,struct vm_area_struct *vma); +extern int hugepage_madvise (unsigned long *vm_flags);static inline int PageTransHuge (struct page *page) {VM_BUG_ON (PageTail (page));
@@ -121,6 +122,11 @@ static inline int split_huge_page(struct#define wait_split_huge_page (__anon_vma, __pmd) \ do { } while (0)#define PageTransHuge (page) 0 +static inline int hugepage_madvise (unsigned long *vm_flags) +{ +BUG_ON (0 ); +return 0 ; +}#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif /* _LINUX_HUGE_MM_H */
mm/huge_memory.c +16 -0
@@ -894,6 +894,22 @@ out:return ret; } +int hugepage_madvise (unsigned long *vm_flags) +{ +/* +* Be somewhat over-protective like KSM for now! +*/ +if (*vm_flags & (VM_HUGEPAGE |VM_SHARED |VM_MAYSHARE | +VM_PFNMAP |VM_IO |VM_DONTEXPAND | +VM_RESERVED |VM_HUGETLB |VM_INSERTPAGE | +VM_MIXEDMAP |VM_SAO )) +return -EINVAL ; + + *vm_flags |=VM_HUGEPAGE ; + +return 0 ; +} +void __split_huge_page_pmd (struct mm_struct *mm,pmd_t *pmd) {struct page *page;
mm/madvise.c +8 -0
@@ -71,6 +71,11 @@ static long madvise_behavior(struct vm_aif (error) goto out;break ; +case MADV_HUGEPAGE : + error =hugepage_madvise (&new_flags); +if (error) + goto out; +break ; }if (new_flags == vma->vm_flags) {
@@ -283,6 +288,9 @@ madvise_behavior_valid(int behavior)case MADV_MERGEABLE :case MADV_UNMERGEABLE :#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +case MADV_HUGEPAGE : +#endif return 1 ;default :
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 33 of 66] madvise(MADV_HUGEPAGE)
Quoted 22 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
Add madvise
MADV_HUGEPAGEto mark regions that are important to be hugepage backed. Return-EINVALif the vma is not of an anonymous type, or the feature isn't built into the kernel. Never silently return success.Signed-off-by Andrea Arcangeli <[email protected]>Acked-by Rik van Riel <[email protected]>---
include/linux/huge_mm.h +1
-0@@ -99,6 +99,7 @@ extern void __split_huge_page_pmd(struct#endif extern unsigned long vma_address (struct page *page,struct vm_area_struct *vma); +extern int hugepage_madvise (unsigned long *vm_flags);static inline int PageTransHuge (struct page *page) {VM_BUG_ON (PageTail (page));
include/linux/huge_mm.h static inline int split_huge_page(struct · line 127
Quoted 6 lines
include/linux/huge_mm.h +3
-0@@ -121,6 +122,11 @@ static inline int split_huge_page(struct#define wait_split_huge_page (__anon_vma, __pmd) \ do { } while (0)#define PageTransHuge (page) 0 +static inline int hugepage_madvise (unsigned long *vm_flags) +{ +BUG_ON (0 );
What's BUG_ON(0) in aid of?
Quoted 35 lines
include/linux/huge_mm.h +2
-0+return 0 ; +}#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ #endif /* _LINUX_HUGE_MM_H */ mm/huge_memory.c +16
-0@@ -894,6 +894,22 @@ out:return ret; } +int hugepage_madvise (unsigned long *vm_flags) +{ +/* +* Be somewhat over-protective like KSM for now! +*/ +if (*vm_flags & (VM_HUGEPAGE |VM_SHARED |VM_MAYSHARE | +VM_PFNMAP |VM_IO |VM_DONTEXPAND | +VM_RESERVED |VM_HUGETLB |VM_INSERTPAGE | +VM_MIXEDMAP |VM_SAO )) +return -EINVAL ; + + *vm_flags |=VM_HUGEPAGE ; + +return 0 ; +} +void __split_huge_page_pmd (struct mm_struct *mm,pmd_t *pmd) {struct page *page;
mm/madvise.c static long madvise_behavior(struct vm_a · line 74
Quoted 4 lines
mm/madvise.c +1
-0@@ -71,6 +71,11 @@ static long madvise_behavior(struct vm_aif (error) goto out;break ; +case MADV_HUGEPAGE :
I should have said it at patch 4 but don't forget that Michael Kerrisk should be made aware of MADV_HUGEPAGE so it makes it to a manual page at some point.
Quoted 18 lines
mm/madvise.c +7
-0+ error =hugepage_madvise (&new_flags); +if (error) + goto out; +break ; }if (new_flags == vma->vm_flags) {@@ -283,6 +288,9 @@ madvise_behavior_valid(int behavior)case MADV_MERGEABLE :case MADV_UNMERGEABLE :#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +case MADV_HUGEPAGE : +#endif return 1 ;default :
Signature · 3 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab
Re: [PATCH 33 of 66] madvise(MADV_HUGEPAGE)
Quoted 10 lines · Mel Gorman
include/linux/huge_mm.h static inline int split_huge_page(struct · line 127
Quoted 7 lines · Andrea Arcangeli
include/linux/huge_mm.h +3
-0@@ -121,6 +122,11 @@ static inline int split_huge_page(struct#define wait_split_huge_page (__anon_vma, __pmd) \ do { } while (0)#define PageTransHuge (page) 0 +static inline int hugepage_madvise (unsigned long *vm_flags) +{ +BUG_ON (0 );What's
BUG_ON(0)in aid of?
When CONFIG_TRANSPARENT_HUGEPAGE is disabled, nothing must call that function (madvise must return -EINVAL like older kernels instead). But I guess you meant I should convert the BUG_ON(0) to a BUG() instead? (done)
Quoted 3 lines
I should have said it at patch 4 but don't forget that Michael Kerrisk should be made aware of
MADV_HUGEPAGEso it makes it to a manual page at some point.
Ok, I'll forward patch 4.
Thanks,
Andrea
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 skip transhuge pages in ksm for now
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
Skip transhuge pages in ksm for now.
---
mm/ksm.c +15 -4
@@ -430,7 +430,7 @@ static struct page *get_mergeable_page(s page =follow_page (vma, addr,FOLL_GET );if (IS_ERR_OR_NULL (page)) goto out; -if (PageAnon (page)) { +if (PageAnon (page) && !PageTransCompound (page)) {flush_anon_page (vma, page, addr);flush_dcache_page (page); }else {
@@ -1288,7 +1288,19 @@ next_mm:if (ksm_test_exit (mm))break ; *page =follow_page (vma, ksm_scan.address,FOLL_GET ); -if (!IS_ERR_OR_NULL (*page) &&PageAnon (*page)) { +if (IS_ERR_OR_NULL (*page)) { + ksm_scan.address +=PAGE_SIZE ; +cond_resched (); +continue ; + } +if (PageTransCompound (*page)) { +put_page (*page); + ksm_scan.address &=HPAGE_PMD_MASK ; + ksm_scan.address +=HPAGE_PMD_SIZE ; +cond_resched (); +continue ; + } +if (PageAnon (*page)) {flush_anon_page (vma, *page, ksm_scan.address);flush_dcache_page (*page); rmap_item = get_next_rmap_item(slot,
@@ -1302,8 +1314,7 @@ next_mm:up_read (&mm->mmap_sem);return rmap_item; } -if (!IS_ERR_OR_NULL (*page)) -put_page (*page); +put_page (*page); ksm_scan.address +=PAGE_SIZE ;cond_resched (); }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 44 of 66] skip transhuge pages in ksm for now
Quoted 6 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
Skip transhuge pages in ksm for now.
Signed-off-by Andrea Arcangeli <[email protected]>Reviewed-by Rik van Riel <[email protected]>
This is an idle concern that I haven't looked into but is there any conflict between khugepaged scanning the KSM scanning?
Specifically, I *think* the impact of this patch is that KSM will not accidentally split a huge page. Is that right? If so, it could do with being included in the changelog.
On the other hand, can khugepaged be prevented from promoting a hugepage because of KSM?
Signature · 3 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab
Re: [PATCH 44 of 66] skip transhuge pages in ksm for now
Quoted 8 lines · Mel Gorman
Quoted 6 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
Skip transhuge pages in ksm for now.
Signed-off-by Andrea Arcangeli <[email protected]>Reviewed-by Rik van Riel <[email protected]>
Quoted 8 lines
Acked-by Mel Gorman <[email protected]>This is an idle concern that I haven't looked into but is there any conflict between khugepaged scanning the KSM scanning?
Specifically, I *think* the impact of this patch is that KSM will not accidentally split a huge page. Is that right? If so, it could do with being included in the changelog.
KSM wasn't aware about hugepages and in turn it'd never split them anyway. We want KSM to split hugepages only when if finds two equal subpages. That will happen later.
Right now there is no collision of ksmd and khugepaged, regular pages, hugepages and ksm pages will co-exist fine in the same vma. The only problem is that the system has now to start swapping before KSM has a chance to find equal pages and we'll fix it in the future so KSM can scan inside hugepages too and split them and merge the subpages as needed before the memory pressure starts.
Quoted 2 lines
On the other hand, can khugepaged be prevented from promoting a hugepage because of KSM?
Sure, khugepaged won't promote if there's any ksm page in the range. That's not going to change. When KSM is started, the priority remains in saving memory. If people uses enabled=madvise and MADV_HUGEPAGE+MADV_MERGEABLE there is actually zero memory loss because of THP and there is a speed improvement for all pages that aren't equal. So it's an ideal setup even for embedded. Regular cloud setup would be enabled=always + MADV_MERGEABLE (with enabled=always MADV_HUGEPAGE becomes a noop).
On a related note I'm also going to introduce a MADV_NO_HUGEPAGE, is that a good name for it? cloud management wants to be able to disable THP per-VM basis (when the VM are totally idle, and low priority, this currently also helps to maximize the power of KSM that would otherwise be activated only after initial sawpping, but the KSM part will be fixed). It could be achieved also with enabled=madvise and MADV_HUGEPAGE but we don't want to change the system wide default in order to disable THP on a per-VM basis: it's much nicer if the default behavior of the host remains the same in case it's not a pure hypervisor usage but there are other loads running in parallel to the virt load. In theory a prctl(PR_NO_HUGEPAGE) could also do it and it'd be possible to use from a wrapper (madvise can't be wrapped), but I think MADV_NO_HUGEPAGE is cleaner and it won't require brand new per-process info.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 44 of 66] skip transhuge pages in ksm for now
Quoted 14 lines · Andrea Arcangeli
Quoted 13 lines · Mel Gorman
Quoted 6 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
Skip transhuge pages in ksm for now.
Signed-off-by Andrea Arcangeli <[email protected]>Reviewed-by Rik van Riel <[email protected]>Acked-by Mel Gorman <[email protected]>This is an idle concern that I haven't looked into but is there any conflict between khugepaged scanning the KSM scanning?
Quoted 8 lines
Quoted 3 lines
Specifically, I *think* the impact of this patch is that KSM will not accidentally split a huge page. Is that right? If so, it could do with being included in the changelog.
KSM wasn't aware about hugepages and in turn it'd never split them anyway. We want KSM to split hugepages only when if finds two equal subpages. That will happen later.
Ok.
Quoted 7 lines
Right now there is no collision of ksmd and khugepaged, regular pages, hugepages and ksm pages will co-exist fine in the same vma. The only problem is that the system has now to start swapping before KSM has a chance to find equal pages and we'll fix it in the future so KSM can scan inside hugepages too and split them and merge the subpages as needed before the memory pressure starts.
Ok. So it's not a perfect mesh but it's not broken either.
Quoted 12 lines
Quoted 2 lines
On the other hand, can khugepaged be prevented from promoting a hugepage because of KSM?
Sure, khugepaged won't promote if there's any ksm page in the range. That's not going to change. When KSM is started, the priority remains in saving memory. If people uses enabled=madvise and MADV_HUGEPAGE+MADV_MERGEABLE there is actually zero memory loss because of THP and there is a speed improvement for all pages that aren't equal. So it's an ideal setup even for embedded. Regular cloud setup would be enabled=always +
MADV_MERGEABLE(with enabled=alwaysMADV_HUGEPAGEbecomes a noop).
That's a reasonable compromise. Thanks for clarifying.
Quoted 6 lines
On a related note I'm also going to introduce a
MADV_NO_HUGEPAGE, is that a good name for it? cloud management wants to be able to disable THP per-VM basis (when the VM are totally idle, and low priority, this currently also helps to maximize the power of KSM that would otherwise be activated only after initial sawpping, but the KSM part will be fixed). It could be achieved also with enabled=madvise and
Quoted 9 lines
MADV_HUGEPAGEbut we don't want to change the system wide default in order to disable THP on a per-VM basis: it's much nicer if the default behavior of the host remains the same in case it's not a pure hypervisor usage but there are other loads running in parallel to the virt load. In theory aprctl(PR_NO_HUGEPAGE)could also do it and it'd be possible to use from a wrapper (madvise can't be wrapped), but I thinkMADV_NO_HUGEPAGEis cleaner and it won't require brand new per-process info.
I see no problem with the proposal. The name seems as good as any other name. I guess the only other sensible alternative might be MADV_BASEPAGE.
Signature · 10 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 transparent hugepage sysfs meminfo
In reply to Andrea ArcangeliFrom: David Rientjes <[email protected]>
Add hugepage statistics to per-node sysfs meminfo
---
drivers/base/node.c +18 -3
@@ -117,12 +117,21 @@ static ssize_t node_read_meminfo(struct"Node %d WritebackTmp: %8lu kB\n" "Node %d Slab: %8lu kB\n" "Node %d SReclaimable: %8lu kB\n" -"Node %d SUnreclaim: %8lu kB\n" , +"Node %d SUnreclaim: %8lu kB\n" +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +"Node %d AnonHugePages: %8lu kB\n" +#endif + , nid,K (node_page_state (nid,NR_FILE_DIRTY )), nid,K (node_page_state (nid,NR_WRITEBACK )), nid,K (node_page_state (nid,NR_FILE_PAGES )), nid,K (node_page_state (nid,NR_FILE_MAPPED )), - nid,K (node_page_state (nid,NR_ANON_PAGES )), + nid,K (node_page_state (nid,NR_ANON_PAGES ) +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + +node_page_state (nid,NR_ANON_TRANSPARENT_HUGEPAGES ) * +HPAGE_PMD_NR +#endif + ), nid,K (node_page_state (nid,NR_SHMEM )), nid,node_page_state (nid,NR_KERNEL_STACK ) * THREAD_SIZE / 1024,
@@ -133,7 +142,13 @@ static ssize_t node_read_meminfo(struct nid,K (node_page_state (nid,NR_SLAB_RECLAIMABLE ) +node_page_state (nid,NR_SLAB_UNRECLAIMABLE )), nid,K (node_page_state (nid,NR_SLAB_RECLAIMABLE )), - nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE))); + nid,K (node_page_state (nid,NR_SLAB_UNRECLAIMABLE )) +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + , nid, +K (node_page_state (nid,NR_ANON_TRANSPARENT_HUGEPAGES ) * +HPAGE_PMD_NR ) +#endif + ); n +=hugetlb_report_node_meminfo (nid, buf + n);return n; }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 define MADV_HUGEPAGE
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
Define MADV_HUGEPAGE.
---
arch/alpha/include/asm/mman.h +2 -0
@@ -53,6 +53,8 @@#define MADV_MERGEABLE 12/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0
arch/mips/include/asm/mman.h +2 -0
@@ -77,6 +77,8 @@#define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ #define MADV_HWPOISON 100/* poison a page for testing */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0
arch/parisc/include/asm/mman.h +2 -0
@@ -59,6 +59,8 @@#define MADV_MERGEABLE 65/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 66/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 67/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0#define MAP_VARIABLE 0
arch/xtensa/include/asm/mman.h +2 -0
@@ -83,6 +83,8 @@#define MADV_MERGEABLE 12/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0
include/asm-generic/mman-common.h +2 -0
@@ -45,6 +45,8 @@#define MADV_MERGEABLE 12/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 04 of 66] define MADV_HUGEPAGE
On just the subject, I've been hassled before to add information to the subject on what is being affected. In this case, it would be just mm:
because you are not affecting any subsystem but others might be
mm: migration: something something
On a practical point of view, it means if you sort mmotm's series file, you can get an approximate breakdown of how many patches affect each subsystem. No idea if it's required or not but don't be surprised if someone complains :)
Quoted 7 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
Define
MADV_HUGEPAGE.Signed-off-by Andrea Arcangeli <[email protected]>Acked-by Rik van Riel <[email protected]>Acked-by Arnd Bergmann <[email protected]>
Otherwise;
Quoted 63 lines
---
arch/alpha/include/asm/mman.h +2
-0@@ -53,6 +53,8 @@#define MADV_MERGEABLE 12/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0arch/mips/include/asm/mman.h +2
-0@@ -77,6 +77,8 @@#define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ #define MADV_HWPOISON 100/* poison a page for testing */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0arch/parisc/include/asm/mman.h +2
-0@@ -59,6 +59,8 @@#define MADV_MERGEABLE 65/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 66/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 67/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0#define MAP_VARIABLE 0arch/xtensa/include/asm/mman.h +2
-0@@ -83,6 +83,8 @@#define MADV_MERGEABLE 12/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0include/asm-generic/mman-common.h +2
-0@@ -45,6 +45,8 @@#define MADV_MERGEABLE 12/* KSM may merge identical pages */ #define MADV_UNMERGEABLE 13/* KSM may not merge identical pages */ +#define MADV_HUGEPAGE 14/* Worth backing with hugepages */ +/* compatibility flags */ #define MAP_FILE 0
Signature · 10 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 disable lumpy when compaction is enabled
In reply to Andrea ArcangeliFrom: Andrea Arcangeli <[email protected]>
Compaction is more reliable than lumpy, and lumpy makes the system unusable when it runs.
---
mm/vmscan.c +6 -0
@@ -274,6 +274,7 @@ unsigned long shrink_slab(unsigned longstatic void set_lumpy_reclaim_mode (int priority,struct scan_control *sc,bool sync) { +#ifndef CONFIG_COMPACTION enum lumpy_mode mode = sync ?LUMPY_MODE_SYNC :LUMPY_MODE_ASYNC ;/*
@@ -294,11 +295,14 @@ static void set_lumpy_reclaim_mode(int p sc->lumpy_reclaim_mode = mode; else sc->lumpy_reclaim_mode = LUMPY_MODE_NONE; +#endif }static void disable_lumpy_reclaim_mode (struct scan_control *sc) { +#ifndef CONFIG_COMPACTION sc->lumpy_reclaim_mode =LUMPY_MODE_NONE ; +#endif }static inline int is_page_cache_freeable (struct page *page)
@@ -321,9 +325,11 @@ static int may_write_to_queue(struct bacif (bdi == current->backing_dev_info)return 1 ; +#ifndef CONFIG_COMPACTION /* lumpy reclaim for hugepage often need a lot of write */ if (sc->order >PAGE_ALLOC_COSTLY_ORDER )return 1 ; +#endif return 0 ; }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 01 of 66] disable lumpy when compaction is enabled
Quoted 20 lines
From: Andrea Arcangeli <[email protected]>
Compaction is more reliable than lumpy, and lumpy makes the system unusable when it runs.
Signed-off-by Andrea Arcangeli <[email protected]>---
mm/vmscan.c +1
-0@@ -274,6 +274,7 @@ unsigned long shrink_slab(unsigned longstatic void set_lumpy_reclaim_mode (int priority,struct scan_control *sc,bool sync) { +#ifndef CONFIG_COMPACTION enum lumpy_mode mode = sync ?LUMPY_MODE_SYNC :LUMPY_MODE_ASYNC ;/*
mm/vmscan.c static void set_lumpy_reclaim_mode(int p · line 299
Quoted 5 lines
mm/vmscan.c +1
-0@@ -294,11 +295,14 @@ static void set_lumpy_reclaim_mode(int p sc->lumpy_reclaim_mode = mode; else sc->lumpy_reclaim_mode = LUMPY_MODE_NONE; +#endif }
I'm talking very personal thing now. I'm usually testing both feature. Then, runtime switching makes my happy :-)
However I don't know what are you and Mel talking and agree about this. So, If many developer prefer this approach, I don't oppose anymore.
But, I bet almost all distro choose CONFIG_COMPACTION=y. then, lumpy code will become nearly dead code. So, I like just kill than dead code. however it is also only my preference. ;)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 01 of 66] disable lumpy when compaction is enabled
Quoted 4 lines · KOSAKI Motohiro
I'm talking very personal thing now. I'm usually testing both feature. Then, runtime switching makes my happy :-)
However I don't know what are you and Mel talking and agree about this. So, If many developer prefer this approach, I don't oppose anymore.
Mel seem to still prefer I allow lumpy for hugetlbfs with a __GFP_LUMPY specified only for hugetlbfs. But he measured compaction is more reliable than lumpy at creating hugepages so he seems to be ok with this too.
Quoted 3 lines
But, I bet almost all distro choose
CONFIG_COMPACTION=y. then, lumpy code will become nearly dead code. So, I like just kill than dead code. however it is also only my preference. ;)
Killing dead code is my preference too indeed. But then it's fine with me to delete it only later. In short this is least intrusive modification I could make to the VM that wouldn't than hang the system when THP is selected because all pte young bits are ignored for >50% of page reclaim invocations like lumpy requires.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 01 of 66] disable lumpy when compaction is enabled
Quoted 11 lines · Andrea Arcangeli
Quoted 4 lines · KOSAKI Motohiro
I'm talking very personal thing now. I'm usually testing both feature. Then, runtime switching makes my happy :-)
However I don't know what are you and Mel talking and agree about this. So, If many developer prefer this approach, I don't oppose anymore.Mel seem to still prefer I allow lumpy for hugetlbfs with a
__GFP_LUMPYspecified only for hugetlbfs. But he measured compaction is more reliable than lumpy at creating hugepages so he seems to be ok with this too.
Specifically, I measured that lumpy in combination with compaction is more reliable and lower latency but that's not the same as deleting it.
That said, lumpy does hurt the system a lot. I'm prototyping a series at the moment that pushes lumpy reclaim to the side and for the majority of cases replaces it with "lumpy compaction". I'd hoping this will be sufficient for THP and alleviate the need to delete it entirely - at least until we are 100% sure that compaction can replace it in all cases.
Unfortunately, in the process of testing it today I also found out that 2.6.37-rc1 had regressed severely in terms of huge page allocations so I'm side-tracked trying to chase that down. My initial theories for the regression have shown up nothing so I'm currently preparing to do a bisection. This will take a long time though because the test is very slow :(
I can still post the series as an RFC if you like to show what direction I'm thinking of but at the moment, I'm unable to test it until I pin the regression down.
Quoted 10 lines
Quoted 3 lines
But, I bet almost all distro choose
CONFIG_COMPACTION=y. then, lumpy code will become nearly dead code. So, I like just kill than dead code. however it is also only my preference. ;)Killing dead code is my preference too indeed. But then it's fine with me to delete it only later. In short this is least intrusive modification I could make to the VM that wouldn't than hang the system when THP is selected because all pte young bits are ignored for >50% of page reclaim invocations like lumpy requires.
Signature · 10 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 01 of 66] disable lumpy when compaction is enabled
Hi Mel,
Quoted 2 lines · Mel Gorman
Specifically, I measured that lumpy in combination with compaction is more reliable and lower latency but that's not the same as deleting it.
Thanks for the clarification. Well no doubt that using both could only increase the success rate. So the thing with hugetlbfs you may want to run both, but with THP we want to stop at compaction. So this would then require a __GFP_LUMPY if we want hugetlbfs to fallback on lumpy whenever compaction isn't successful. We can't just nuke and ignore young bits in pte if compaction fails. Trying later in khugepaged once every 10 seconds is a lot better.
Quoted 11 lines
That said, lumpy does hurt the system a lot. I'm prototyping a series at the moment that pushes lumpy reclaim to the side and for the majority of cases replaces it with "lumpy compaction". I'd hoping this will be sufficient for THP and alleviate the need to delete it entirely - at least until we are 100% sure that compaction can replace it in all cases.
Unfortunately, in the process of testing it today I also found out that 2.6.37-rc1 had regressed severely in terms of huge page allocations so I'm side-tracked trying to chase that down. My initial theories for the regression have shown up nothing so I'm currently preparing to do a bisection. This will take a long time though because the test is very slow :(
On my side (unrelated) I also found 37-rc1 broke my mic by changing soundcard type (luckily csipsimple and skype on my cellphone are now working better than laptop for making voip calls so it was easy to workaround) and my backlight goes blank forever after a "xset dpms force standby" (so I'm stuck in presentation mode to workaround it, suspend to ram was successful to avoid having to reboot too as the bios restarts the backlight during boot).
Quoted 3 lines
I can still post the series as an RFC if you like to show what direction I'm thinking of but at the moment, I'm unable to test it until I pin the regression down.
Sure feel free to post it, if it's already worth testing it, I can keep at the end of the patchset considering it's new code while what I posted had lots of testing.
With THP we have khugepaged in the background, nothing is mandatory at allocation time. I don't want a super aggressive thing at allocation time, and lumpy by ignoring all young bits is too aggressive and generates swap storms for every single allocation. We need to fail order 9 allocation quick even if compaction fails (like if more than 90% of the ram is asked in hugepages so having to use ram in the unmovable page blocks selected by anti-frag) to avoid hanging the system during allocations. Looking my stats things seem to be working ok with compaction in 37-rc1, so maybe it's just the lumpy changes that introduced your regression?
Thanks,
Andrea
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 01 of 66] disable lumpy when compaction is enabled
Quoted 9 lines · Andrea Arcangeli
Hi Mel,
Quoted 2 lines · Mel Gorman
Specifically, I measured that lumpy in combination with compaction is more reliable and lower latency but that's not the same as deleting it.
Thanks for the clarification. Well no doubt that using both could only increase the success rate. So the thing with hugetlbfs you may want to run both, but with THP we want to stop at compaction.
Agreed. Any performance increase from THP is not likely to offset the cost of lumpy reclaim.
Quoted 6 lines
So this would
then require a__GFP_LUMPYif we want hugetlbfs to fallback on lumpy whenever compaction isn't successful. We can't just nuke and ignore young bits in pte if compaction fails. Trying later in khugepaged once every 10 seconds is a lot better.
Again agreed, I have no problem with lumpy reclaim being pushed aside. I'm just less keen on it being disabled altogether. I have high hopes for the series I'm working on that it can be extended slightly to suit the needs of THP.
Quoted 12 lines
Quoted 11 lines
That said, lumpy does hurt the system a lot. I'm prototyping a series at the moment that pushes lumpy reclaim to the side and for the majority of cases replaces it with "lumpy compaction". I'd hoping this will be sufficient for THP and alleviate the need to delete it entirely - at least until we are 100% sure that compaction can replace it in all cases.
Unfortunately, in the process of testing it today I also found out that 2.6.37-rc1 had regressed severely in terms of huge page allocations so I'm side-tracked trying to chase that down. My initial theories for the regression have shown up nothing so I'm currently preparing to do a bisection. This will take a long time though because the test is very slow :(
Quoted 8 lines
On my side (unrelated) I also found 37-rc1 broke my mic by changing soundcard type (luckily csipsimple and skype on my cellphone are now working better than laptop for making voip calls so it was easy to workaround) and my backlight goes blank forever after a "xset dpms force standby" (so I'm stuck in presentation mode to workaround it, suspend to ram was successful to avoid having to reboot too as the bios restarts the backlight during boot).
I do not believe they are related. Fortunately, I did not have to do a full bisect but I know roughly what area the problem must be in. The problem commit looks like d065bd81. I'm running further tests with it reverted to see if it's true but it'll take a few hours to complete.
Quoted 8 lines
Quoted 3 lines
I can still post the series as an RFC if you like to show what direction I'm thinking of but at the moment, I'm unable to test it until I pin the regression down.
Sure feel free to post it, if it's already worth testing it, I can keep at the end of the patchset considering it's new code while what I posted had lots of testing.
As I hopefully have pinned down the problem commit, I'm going to hold off for another day to see can I get real data.
Quoted 11 lines
With THP we have khugepaged in the background, nothing is mandatory at allocation time. I don't want a super aggressive thing at allocation time, and lumpy by ignoring all young bits is too aggressive and generates swap storms for every single allocation. We need to fail order 9 allocation quick even if compaction fails (like if more than 90% of the ram is asked in hugepages so having to use ram in the unmovable page blocks selected by anti-frag) to avoid hanging the system during allocations. Looking my stats things seem to be working ok with compaction in 37-rc1, so maybe it's just the lumpy changes that introduced your regression?
Nah, the first thing I did was eliminate being "my fault" :). It would have surprised me because the patches in isolation worked fine. It thought the inode changes might have had something to do with it so I was chasing blind alleys for a while. Hopefully d065bd81 will prove to be the real problem.
Signature · 3 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab
Re: [PATCH 01 of 66] disable lumpy when compaction is enabled
Quoted 2 lines · Mel Gorman
Agreed. Any performance increase from THP is not likely to offset the cost of lumpy reclaim.
Exactly. Furthermore the improvement will still happen later by polling compaction once every 10 sec with khugepaged (this is also required in case some other guest or application quit releasing tons of ram maybe natively order 9 in the buddy without requiring any further compaction invocation).
What the default should be I don't know, but I like a default that fails without causing swap storms. If you want the swap storms and to drop all ptes regardless of their young bits, you should ask explicitly for it I think. Anybody asking for high order allocation and pretending to succeed despite the anti-frag and movable pageblocks migrated with compaction aren't enough to succeed should be able to handle a full graceful failure like THP does by design (or worst case to return error to userland). As far as I can tell tg3 atomic order 2 allocation also provides for a graceful fallback for the same reason (however in new mainline it floods the dmesg with tons of printk, which it didn't used to with older kernels but it's not an actual regression).
Quoted 4 lines
Again agreed, I have no problem with lumpy reclaim being pushed aside. I'm just less keen on it being disabled altogether. I have high hopes for the series I'm working on that it can be extended slightly to suit the needs of THP.
Great. Well this is also why I disabled it with the smallest possible modification, to avoid stepping on your toes.
Quoted 5 lines
Nah, the first thing I did was eliminate being "my fault" :). It would have surprised me because the patches in isolation worked fine. It thought the inode changes might have had something to do with it so I was chasing blind alleys for a while. Hopefully
d065bd81will prove to be the real problem.
Well I wasn't sure if you tested it already on that very workload, the patches weren't from you (even if you were in the signoffs). I mentioned it just in case, glad it's not related :).
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 01 of 66] disable lumpy when compaction is enabled
In reply to Andrea ArcangeliQuoted 5 lines · Andrea Arcangeli
From: Andrea Arcangeli <[email protected]>
Compaction is more reliable than lumpy, and lumpy makes the system unusable when it runs.
It took me a while but is "[PATCH 0/8] Use memory compaction instead of lumpy reclaim during high-order allocations" a suitable replacement for this patch?
Signature · 10 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH 1/1 kvm mmu transparent hugepage support
In reply to Andrea ArcangeliFrom: Marcelo Tosatti <[email protected]>
This should work for both hugetlbfs and transparent hugepages.
---
arch/x86/kvm/mmu.c +18 -8
@@ -543,10 +543,20 @@ static int has_wrprotected_page(struct kstatic int host_mapping_level (struct kvm *kvm,gfn_t gfn) { -unsigned long page_size; +unsigned long page_size, addr;int i, ret =0 ; - page_size =kvm_host_page_size (kvm, gfn); + page_size =kvm_host_page_size (kvm, gfn, &addr); + +/* check for transparent hugepages */ +if (page_size ==PAGE_SIZE && !kvm_is_error_hva (addr)) { +pfn_t pfn =hva_to_pfn (kvm, addr,0 ); + +if (!is_error_pfn (pfn) && !kvm_is_mmio_pfn (pfn) && +PageTransCompound (pfn_to_page (pfn))) + page_size =KVM_HPAGE_SIZE (2 ); +kvm_release_pfn_clean (pfn); + }for (i =PT_PAGE_TABLE_LEVEL ; i < (PT_PAGE_TABLE_LEVEL +KVM_NR_PAGE_SIZES ); ++i) {
@@ -2283,6 +2293,8 @@ static int nonpaging_map(struct kvm_vcpupfn_t pfn;unsigned long mmu_seq; + mmu_seq = vcpu->kvm->mmu_notifier_seq; +smp_rmb (); level =mapping_level (vcpu, gfn);/*
@@ -2294,8 +2306,6 @@ static int nonpaging_map(struct kvm_vcpu gfn &= ~(KVM_PAGES_PER_HPAGE (level) -1 ); - mmu_seq = vcpu->kvm->mmu_notifier_seq; -smp_rmb (); pfn =gfn_to_pfn (vcpu->kvm, gfn);/* mmio */
@@ -2601,12 +2611,12 @@ static int tdp_page_fault(struct kvm_vcpif (r)return r; - level =mapping_level (vcpu, gfn); - - gfn &= ~(KVM_PAGES_PER_HPAGE (level) -1 ); - mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); + level =mapping_level (vcpu, gfn); + + gfn &= ~(KVM_PAGES_PER_HPAGE (level) -1 ); + pfn =gfn_to_pfn (vcpu->kvm, gfn);if (is_error_pfn (pfn))return kvm_handle_bad_page (vcpu->kvm, gfn, pfn);
arch/x86/kvm/paging_tmpl.h +2 -2
@@ -561,13 +561,13 @@ static int FNAME(page_fault)(struct kvm_return 0 ; } + mmu_seq = vcpu->kvm->mmu_notifier_seq; +smp_rmb ();if (walker.level >=PT_DIRECTORY_LEVEL ) { level =min (walker.level,mapping_level (vcpu, walker.gfn)); walker.gfn = walker.gfn & ~(KVM_PAGES_PER_HPAGE (level) -1 ); } - mmu_seq = vcpu->kvm->mmu_notifier_seq; -smp_rmb (); pfn =gfn_to_pfn (vcpu->kvm, walker.gfn);/* mmio */
include/linux/kvm_host.h +3 -1
@@ -301,6 +301,7 @@ void kvm_set_page_dirty(struct page *pagvoid kvm_set_page_accessed (struct page *page);pfn_t hva_to_pfn_atomic (struct kvm *kvm,unsigned long addr); +pfn_t hva_to_pfn (struct kvm *kvm,unsigned long addr,bool atomic);pfn_t gfn_to_pfn_atomic (struct kvm *kvm,gfn_t gfn);pfn_t gfn_to_pfn (struct kvm *kvm,gfn_t gfn); pfn_t gfn_to_pfn_memslot(struct kvm *kvm,
@@ -325,7 +326,8 @@ int kvm_clear_guest_page(struct kvm *kvmint kvm_clear_guest (struct kvm *kvm,gpa_t gpa,unsigned long len);struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn);int kvm_is_visible_gfn (struct kvm *kvm,gfn_t gfn); -unsigned long kvm_host_page_size (struct kvm *kvm,gfn_t gfn); +unsigned long kvm_host_page_size (struct kvm *kvm,gfn_t gfn, +unsigned long *addr);void mark_page_dirty (struct kvm *kvm,gfn_t gfn);void kvm_vcpu_block (struct kvm_vcpu *vcpu);
virt/kvm/iommu.c +1 -1
@@ -83,7 +83,7 @@ int kvm_iommu_map_pages(struct kvm *kvm, }/* Get the page size we could use to map */ - page_size =kvm_host_page_size (kvm, gfn); + page_size =kvm_host_page_size (kvm, gfn,NULL );/* Make sure the page_size does not exceed the memslot */ while ((gfn + (page_size >>PAGE_SHIFT )) > end_gfn)
virt/kvm/kvm_main.c +35 -4
@@ -102,8 +102,36 @@ static pfn_t fault_pfn;inline int kvm_is_mmio_pfn (pfn_t pfn) {if (pfn_valid (pfn)) { -struct page *page =compound_head (pfn_to_page (pfn)); -return PageReserved (page); +struct page *head; +struct page *tail =pfn_to_page (pfn); + head =compound_head (tail); +if (head != tail) { +smp_rmb (); +/* +* head may be a dangling pointer. +* __split_huge_page_refcount clears PageTail +* before overwriting first_page, so if +* PageTail is still there it means the head +* pointer isn't dangling. +*/ +if (PageTail (tail)) { +/* +* the "head" is not a dangling +* pointer but the hugepage may have +* been splitted from under us (and we +* may not hold a reference count on +* the head page so it can be reused +* before we run PageReferenced), so +* we've to recheck PageTail before +* returning what we just read. +*/ +int reserved =PageReserved (head); +smp_rmb (); +if (PageTail (tail)) +return reserved; + } + } +return PageReserved (tail); }return true;
@@ -884,7 +912,8 @@ int kvm_is_visible_gfn(struct kvm *kvm, }EXPORT_SYMBOL_GPL (kvm_is_visible_gfn); -unsigned long kvm_host_page_size (struct kvm *kvm,gfn_t gfn) +unsigned long kvm_host_page_size (struct kvm *kvm,gfn_t gfn, +unsigned long *addrp) {struct vm_area_struct *vma;unsigned long addr, size;
@@ -892,6 +921,8 @@ unsigned long kvm_host_page_size(struct size =PAGE_SIZE ; addr =gfn_to_hva (kvm, gfn); +if (addrp) + *addrp = addr;if (kvm_is_error_hva (addr))return PAGE_SIZE ;
@@ -946,7 +977,7 @@ unsigned long gfn_to_hva(struct kvm *kvm }EXPORT_SYMBOL_GPL (gfn_to_hva); -static pfn_t hva_to_pfn (struct kvm *kvm,unsigned long addr,bool atomic) +pfn_t hva_to_pfn (struct kvm *kvm,unsigned long addr,bool atomic) {struct page *page[1 ];int npages;
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 10 lines · Andrea Arcangeli
Some of some relevant user of the project:
KVM Virtualization
GCC (kernel build included, requires a few liner patch to enable) JVM
VMware Workstation
HPCIt would be great if it could go in -mm.
FWIW, I saw nothing of major concern while reading through the series other than the delete-lumpy-reclaim portion. I've posted a series that should address both the lumpy reclaim concerns while also improving the performance of compaction in general.
For the rest of the series, the vast majority of my comments were nits as my questions were covered (and addressed) in previous revisions. I'll try and set time aside to convert some of the libhugetlbfs-orientated to run against these patches but I'm not anticipating any problems and it'd be nice to see merged at some point.
Signature · 10 lines
Mel Gorman Part-time Phd Student Linux Technology Center University of Limerick IBM Dublin Software Lab -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/ Don't email: <a href=mailto:"[email protected]"> [email protected] </a>