hugetlbfs: close race between MADV_DONTNEED and page fault
Subsystems (4)
v7 cover hugetlbfs: close race between MADV_DONTNEED and page fault
v7: fix !vma->vm_file and hfill2 cases
v6: move a fix from patch 3 to patch 2, more locking fixes
v5: somehow a __vma_private_lock(vma) test failed to make it from my tree into the v4 series, fix that
v4: fix unmap_vmas locking issue pointed out by Mike Kravetz, and resulting lockdep fallout
v3: fix compile error w/ lockdep and test case errors with patch 3
v2: fix the locking bug found with the libhugetlbfs tests.
Malloc libraries, like jemalloc and tcalloc, take decisions on when to call madvise independently from the code in the main application.
This sometimes results in the application page faulting on an address, right after the malloc library has shot down the backing memory with MADV_DONTNEED.
Usually this is harmless, because we always have some 4kB pages sitting around to satisfy a page fault. However, with hugetlbfs systems often allocate only the exact number of huge pages that the application wants.
Due to TLB batching, hugetlbfs MADV_DONTNEED will free pages outside of any lock taken on the page fault path, which can open up the following race condition:
CPU 1 CPU 2
MADV_DONTNEED
unmap page
shoot down TLB entry
page fault
fail to allocate a huge page killed with SIGBUS
free page
Fix that race by extending the hugetlb_vma_lock locking scheme to also cover private hugetlb mappings (with resv_map), and pulling the locking from __unmap_hugepage_final_range into helper functions called from zap_page_range_single. This ensures page faults stay locked out of the MADV_DONTNEED VMA until the huge pages have actually been freed.
The third patch in the series is more of an RFC. Using the invalidate_lock instead of the hugetlb_vma_lock greatly simplifies the code, but at the cost of turning a per-VMA lock into a lock per backing hugetlbfs file, which could slow things down when multiple processes are mapping the same hugetlbfs file.
PATCH v7 1/4 hugetlbfs: clear resv_map pointer if mmap fails
From: Rik van Riel <[email protected]>
Hugetlbfs leaves a dangling pointer in the VMA if mmap fails. This has not been a problem so far, but other code in this patch series tries to follow that pointer.
--- mm/hugetlb.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
mm/hugetlb.c +4 -3
@@ -1138,8 +1138,7 @@ static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)VM_BUG_ON_VMA (!is_vm_hugetlb_page (vma), vma);VM_BUG_ON_VMA (vma->vm_flags &VM_MAYSHARE , vma); -set_vma_private_data (vma, (get_vma_private_data (vma) & -HPAGE_RESV_MASK ) | (unsigned long )map); +set_vma_private_data (vma, (unsigned long )map); }static void set_vma_resv_flags (struct vm_area_struct *vma,unsigned long flags)
@@ -6806,8 +6805,10 @@ bool hugetlb_reserve_pages(struct inode *inode,*/ if (chg >=0 && add <0 )region_abort (resv_map, from, to, regions_needed); -if (vma &&is_vma_resv_set (vma,HPAGE_RESV_OWNER )) +if (vma &&is_vma_resv_set (vma,HPAGE_RESV_OWNER )) {kref_put (&resv_map->refs, resv_map_release); +set_vma_resv_map (vma,NULL ); + }return false; }
Signature · 1 line
2.41.0
PATCH v7 2/4 hugetlbfs: extend hugetlb_vma_lock to private VMAs
In reply to [email protected]From: Rik van Riel <[email protected]>
Extend the locking scheme used to protect shared hugetlb mappings from truncate vs page fault races, in order to protect private hugetlb mappings (with resv_map) against MADV_DONTNEED.
Add a read-write semaphore to the resv_map data structure, and use that from the hugetlb_vma_(un)lock_* functions, in preparation for closing the race between MADV_DONTNEED and page faults.
--- include/linux/hugetlb.h | 6 ++++++ mm/hugetlb.c | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-)
include/linux/hugetlb.h +6 -0
@@ -60,6 +60,7 @@ struct resv_map {long adds_in_progress;struct list_head region_cache;long region_cache_count; +struct rw_semaphore rw_sema;#ifdef CONFIG_CGROUP_HUGETLB /* * On private mappings, the counter to uncharge reservations is stored
@@ -1231,6 +1232,11 @@ static inline bool __vma_shareable_lock(struct vm_area_struct *vma)return (vma->vm_flags &VM_MAYSHARE ) && vma->vm_private_data; } +static inline bool __vma_private_lock (struct vm_area_struct *vma) +{ +return (!(vma->vm_flags &VM_MAYSHARE )) && vma->vm_private_data; +} +/* * Safe version of huge_pte_offset() to check the locks. See comments * above huge_pte_offset().
mm/hugetlb.c +37 -4
96 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [PATCH 2/4] hugetlbfs: extend hugetlb_vma_lock to private VMAs
Quoted 56 lines · On Thu, Oct 05, 2023 at 11:59:07PM -0400, [email protected] wrote:
From: Rik van Riel <[email protected]>
Extend the locking scheme used to protect shared hugetlb mappings from truncate vs page fault races, in order to protect private hugetlb mappings (with
resv_map) againstMADV_DONTNEED.Add a read-write semaphore to the
resv_mapdata structure, and use that from the hugetlb_vma_(un)lock_* functions, in preparation for closing the race betweenMADV_DONTNEEDand page faults.Signed-off-by Rik van Riel <[email protected]>Reviewed-by Mike Kravetz <[email protected]>Fixes 04ada095dcfc ("hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing")--- include/linux/hugetlb.h | 6 ++++++ mm/hugetlb.c | 41 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-)include/linux/hugetlb.h +6
-0@@ -60,6 +60,7 @@ struct resv_map {long adds_in_progress;struct list_head region_cache;long region_cache_count; +struct rw_semaphore rw_sema;#ifdef CONFIG_CGROUP_HUGETLB /* * On private mappings, the counter to uncharge reservations is stored @@ -1231,6 +1232,11 @@ static inline bool __vma_shareable_lock(struct vm_area_struct *vma)return (vma->vm_flags &VM_MAYSHARE ) && vma->vm_private_data; } +static inline bool __vma_private_lock (struct vm_area_struct *vma) +{ +return (!(vma->vm_flags &VM_MAYSHARE )) && vma->vm_private_data; +} +/* * Safe version of huge_pte_offset() to check the locks. See comments * above huge_pte_offset(). mm/hugetlb.c +1
-0@@ -97,6 +97,7 @@ static void hugetlb_vma_lock_alloc(struct vm_area_struct *vma);static void __hugetlb_vma_unlock_write_free (struct vm_area_struct *vma);static void hugetlb_unshare_pmds (struct vm_area_struct *vma,unsigned long start,unsigned long end); +static struct resv_map *vma_resv_map (struct vm_area_struct *vma);static inline bool subpool_is_free (struct hugepage_subpool *spool) {
mm/hugetlb.c void hugetlb_vma_lock_read(struct vm_area_struct *vma) · line 276
Quoted 9 lines
mm/hugetlb.c +4
-0@@ -267,6 +268,10 @@ void hugetlb_vma_lock_read(struct vm_area_struct *vma)struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;down_read (&vma_lock->rw_sema); + }else if (__vma_private_lock (vma)) { +struct resv_map *resv_map =vma_resv_map (vma); + +down_read (&resv_map->rw_sema); } }
+Ackerley +Oscar
I'm reading the resv code recently and just stumbled upon this. So want to raise this question.
IIUC __vma_private_lock() will return false for MAP_PRIVATE hugetlb vma if the vma is dup()ed from a fork(), with/without commit 187da0f8250a ("hugetlb: fix null-ptr-deref in hugetlb_vma_lock_write") which fixed a slightly different issue.
The problem is the current vma lock for private mmap() is based on the resv map, and the resv map only belongs to the process that mmap()ed this private vma. E.g. dup_mmap() has:
if (is_vm_hugetlb_page(tmp))
hugetlb_dup_vma_private(tmp);Which does:
if (vma->vm_flags & VM_MAYSHARE) {
...
} else
vma->vm_private_data = NULL; <---------------------So even if I don't know how many of us are even using hugetlb PRIVATE + fork(), assuming that's the most controversial use case that I'm aware of on hugetlb that people complains about.. with some tricky changes like 04f2cbe35699.. Just still want to raise this pure question, that after a fork() on private vma, and if I read it alright, lock/unlock operations may become noop..
Thanks,
Quoted 90 lines
mm/hugetlb.c +32
-4@@ -276,6 +281,10 @@ void hugetlb_vma_unlock_read(struct vm_area_struct *vma)struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;up_read (&vma_lock->rw_sema); + }else if (__vma_private_lock (vma)) { +struct resv_map *resv_map =vma_resv_map (vma); + +up_read (&resv_map->rw_sema); } }@@ -285,6 +294,10 @@ void hugetlb_vma_lock_write(struct vm_area_struct *vma)struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;down_write (&vma_lock->rw_sema); + }else if (__vma_private_lock (vma)) { +struct resv_map *resv_map =vma_resv_map (vma); + +down_write (&resv_map->rw_sema); } }@@ -294,17 +307,27 @@ void hugetlb_vma_unlock_write(struct vm_area_struct *vma)struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;up_write (&vma_lock->rw_sema); + }else if (__vma_private_lock (vma)) { +struct resv_map *resv_map =vma_resv_map (vma); + +up_write (&resv_map->rw_sema); } }int hugetlb_vma_trylock_write (struct vm_area_struct *vma) { -struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; -if (!__vma_shareable_lock (vma)) -return 1 ; +if (__vma_shareable_lock (vma)) { +struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; -return down_write_trylock (&vma_lock->rw_sema); +return down_write_trylock (&vma_lock->rw_sema); + }else if (__vma_private_lock (vma)) { +struct resv_map *resv_map =vma_resv_map (vma); + +return down_write_trylock (&resv_map->rw_sema); + } + +return 1 ; }void hugetlb_vma_assert_locked (struct vm_area_struct *vma)@@ -313,6 +336,10 @@ void hugetlb_vma_assert_locked(struct vm_area_struct *vma)struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;lockdep_assert_held (&vma_lock->rw_sema); + }else if (__vma_private_lock (vma)) { +struct resv_map *resv_map =vma_resv_map (vma); + +lockdep_assert_held (&resv_map->rw_sema); } }@@ -345,6 +372,11 @@ static void __hugetlb_vma_unlock_write_free(struct vm_area_struct *vma)struct hugetlb_vma_lock *vma_lock = vma->vm_private_data;__hugetlb_vma_unlock_write_put (vma_lock); + }else if (__vma_private_lock (vma)) { +struct resv_map *resv_map =vma_resv_map (vma); + +/* no free for anon vmas, but still need to unlock */ +up_write (&resv_map->rw_sema); } }@@ -1068,6 +1100,7 @@ struct resv_map *resv_map_alloc(void)kref_init (&resv_map->refs);spin_lock_init (&resv_map->lock);INIT_LIST_HEAD (&resv_map->regions); +init_rwsem (&resv_map->rw_sema); resv_map->adds_in_progress =0 ;/* Signature · 1 line
2.41.0
Signature · 1 line
Peter Xu
Oscar Salvador
maintainerRe: [PATCH 2/4] hugetlbfs: extend hugetlb_vma_lock to private VMAs
Quoted 24 lines · Peter Xu
+Ackerley +Oscar
I'm reading the resv code recently and just stumbled upon this. So want to raise this question.
IIUC
__vma_private_lock()will return false forMAP_PRIVATEhugetlb vma if the vma is dup()ed from afork(), with/without commit187da0f8250a("hugetlb: fix null-ptr-deref inhugetlb_vma_lock_write") which fixed a slightly different issue.The problem is the current vma lock for private
mmap()is based on the resv map, and the resv map only belongs to the process that mmap()ed this private vma. E.g.dup_mmap()has:if (is_vm_hugetlb_page(tmp)) hugetlb_dup_vma_private(tmp);Which does:
if (vma->vm_flags & VM_MAYSHARE) { ... } else vma->vm_private_data = NULL; <---------------------
Quoted 6 lines
So even if I don't know how many of us are even using hugetlb PRIVATE +
fork(), assuming that's the most controversial use case that I'm aware of on hugetlb that people complains about.. with some tricky changes like04f2cbe35699.. Just still want to raise this pure question, that after afork()on private vma, and if I read it alright, lock/unlock operations may become noop..
I have been taking a look at this, and yes, __vma_private_lock will return false for private hugetlb mappings that were forked .
I quickly checked what protects what and we currently have:
hugetlb_vma_lock_read - copy_hugetlb_page_range (only sharing) hugetlb_vma_lock_read - hugetlb_wp (only for HPAGE_RESV_OWNER) hugetlb_vma_lock_read - hugetlb_fault , protects huge_pmd_unshare? hugetlb_vma_lock_read - pagewalks
hugetlb_vma_lock_write - hugetlb_change_protection hugetlb_vma_lock_write - hugetlb_unshare_pmds hugetlb_vma_lock_wirte - move_hugetlb_page_tables hugetlb_vma_lock_wirte - _hugetlb_zap_begin (unmap_vmas)
the ones taking the hugetlb_vma_lock in write (so, the last four) also take the i_mmap_lock_write (vma->vm_file->f_mapping), and AFAIK, hugetlb mappings, private or not, should have vma->vm_file->f_mapping set.
Which means that technically we cannot race between hugetlb_change_protection and move_hugetlb_page_tables etc.
But, checking
commit bf4916922c60f43efaa329744b3eef539aa6a2b2
Author: Rik van Riel <[email protected]>
Date: Thu Oct 5 23:59:07 2023 -0400
hugetlbfs: extend hugetlb_vma_lock to private VMAs
which its motivation was to protect MADV_DONTNEED vs page_faults, I do not see how it gets protected with private hugetlb mappings that were dupped (forked).
madvise_dontneed_single_vma
zap_page_range_single
_hugetlb_zap_begin
hugetlb_vma_lock_write - noop for mappings that do not own the reservation i_mmap_lock_write
But the hugetlb_fault path only takes hugetlb_vma_lock_*, so theorically we still could race between page_fault vs madvise_dontneed_single_vma?
A quick way to prove would be map a hugetlb private mapping, fork it and have two threads tryong to madvise(MADV_DONTNEED) and the other trying to write to it?
I do not know, maybe we are protected in some other way I cannot see right now.
I will have a look.
Signature · 2 lines
Oscar Salvador SUSE Labs
PATCH v7 3/4 hugetlbfs: close race between MADV_DONTNEED and page fault
In reply to [email protected]From: Rik van Riel <[email protected]>
Malloc libraries, like jemalloc and tcalloc, take decisions on when to call madvise independently from the code in the main application.
This sometimes results in the application page faulting on an address, right after the malloc library has shot down the backing memory with MADV_DONTNEED.
Usually this is harmless, because we always have some 4kB pages sitting around to satisfy a page fault. However, with hugetlbfs systems often allocate only the exact number of huge pages that the application wants.
Due to TLB batching, hugetlbfs MADV_DONTNEED will free pages outside of any lock taken on the page fault path, which can open up the following race condition:
CPU 1 CPU 2
MADV_DONTNEED
unmap page
shoot down TLB entry
page fault
fail to allocate a huge page killed with SIGBUS
free page
Fix that race by pulling the locking from __unmap_hugepage_final_range into helper functions called from zap_page_range_single. This ensures page faults stay locked out of the MADV_DONTNEED VMA until the huge pages have actually been freed.
--- include/linux/hugetlb.h | 35 +++++++++++++++++++++++++++++++++-- mm/hugetlb.c | 34 ++++++++++++++++++++++------------ mm/memory.c | 13 ++++++++----- 3 files changed, 63 insertions(+), 19 deletions(-)
include/linux/hugetlb.h +33 -2
@@ -139,7 +139,7 @@ struct page *hugetlb_follow_page_mask(struct vm_area_struct *vma,void unmap_hugepage_range (struct vm_area_struct *,unsigned long ,unsigned long ,struct page *,zap_flags_t ); -void __unmap_hugepage_range_final (struct mmu_gather *tlb, +void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start,unsigned long end,struct page *ref_page,zap_flags_t zap_flags);
@@ -246,6 +246,25 @@ int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma,void adjust_range_if_pmd_sharing_possible (struct vm_area_struct *vma,unsigned long *start,unsigned long *end); +extern void __hugetlb_zap_begin (struct vm_area_struct *vma, +unsigned long *begin,unsigned long *end); +extern void __hugetlb_zap_end (struct vm_area_struct *vma, +struct zap_details *details); + +static inline void hugetlb_zap_begin (struct vm_area_struct *vma, +unsigned long *start,unsigned long *end) +{ +if (is_vm_hugetlb_page (vma)) +__hugetlb_zap_begin (vma, start, end); +} + +static inline void hugetlb_zap_end (struct vm_area_struct *vma, +struct zap_details *details) +{ +if (is_vm_hugetlb_page (vma)) +__hugetlb_zap_end (vma, details); +} +void hugetlb_vma_lock_read (struct vm_area_struct *vma);void hugetlb_vma_unlock_read (struct vm_area_struct *vma);void hugetlb_vma_lock_write (struct vm_area_struct *vma);
@@ -297,6 +316,18 @@ static inline void adjust_range_if_pmd_sharing_possible( { } +static inline void hugetlb_zap_begin ( +struct vm_area_struct *vma, +unsigned long *start,unsigned long *end) +{ +} + +static inline void hugetlb_zap_end ( +struct vm_area_struct *vma, +struct zap_details *details) +{ +} +static inline struct page *hugetlb_follow_page_mask (struct vm_area_struct *vma,unsigned long address,unsigned int flags,unsigned int *page_mask)
@@ -442,7 +473,7 @@ static inline long hugetlb_change_protection(return 0 ; } -static inline void __unmap_hugepage_range_final (struct mmu_gather *tlb, +static inline void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start,unsigned long end,struct page *ref_page,zap_flags_t zap_flags)
mm/hugetlb.c +22 -12
@@ -5305,9 +5305,9 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma,return len + old_addr - old_end; } -static void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long start,unsigned long end, -struct page *ref_page,zap_flags_t zap_flags) +void __unmap_hugepage_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long start,unsigned long end, +struct page *ref_page,zap_flags_t zap_flags) {struct mm_struct *mm = vma->vm_mm;unsigned long address;
@@ -5434,16 +5434,25 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_structtlb_flush_mmu_tlbonly (tlb); } -void __unmap_hugepage_range_final (struct mmu_gather *tlb, -struct vm_area_struct *vma,unsigned long start, -unsigned long end,struct page *ref_page, -zap_flags_t zap_flags) +void __hugetlb_zap_begin (struct vm_area_struct *vma, +unsigned long *start,unsigned long *end) { +if (!vma->vm_file)/* hugetlbfs_file_mmap error */ +return ; + +adjust_range_if_pmd_sharing_possible (vma, start, end);hugetlb_vma_lock_write (vma); -i_mmap_lock_write (vma->vm_file->f_mapping); +if (vma->vm_file) +i_mmap_lock_write (vma->vm_file->f_mapping); +} -/* mmu notification performed in caller */ -__unmap_hugepage_range (tlb, vma, start, end, ref_page, zap_flags); +void __hugetlb_zap_end (struct vm_area_struct *vma, +struct zap_details *details) +{ +zap_flags_t zap_flags = details ? details->zap_flags :0 ; + +if (!vma->vm_file)/* hugetlbfs_file_mmap error */ +return ;if (zap_flags &ZAP_FLAG_UNMAP ) {/* final unmap */ /*
@@ -5456,11 +5465,12 @@ void __unmap_hugepage_range_final(struct mmu_gather *tlb,* someone else. */ __hugetlb_vma_unlock_write_free (vma); -i_mmap_unlock_write (vma->vm_file->f_mapping); }else { -i_mmap_unlock_write (vma->vm_file->f_mapping);hugetlb_vma_unlock_write (vma); } + +if (vma->vm_file) +i_mmap_unlock_write (vma->vm_file->f_mapping); } void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
mm/memory.c +8 -5
@@ -1683,7 +1683,7 @@ static void unmap_single_vma(struct mmu_gather *tlb,if (vma->vm_file) {zap_flags_t zap_flags = details ? details->zap_flags :0 ; -__unmap_hugepage_range_final (tlb, vma, start, end, +__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); } }else
@@ -1728,8 +1728,12 @@ void unmap_vmas(struct mmu_gather *tlb, struct ma_state *mas, start_addr, end_addr);mmu_notifier_invalidate_range_start (&range);do { -unmap_single_vma (tlb, vma, start_addr, end_addr, &details, +unsigned long start = start_addr; +unsigned long end = end_addr; +hugetlb_zap_begin (vma, &start, &end); +unmap_single_vma (tlb, vma, start, end, &details, mm_wr_locked); +hugetlb_zap_end (vma, &details); }while ((vma =mas_find (mas, tree_end -1 )) !=NULL );mmu_notifier_invalidate_range_end (&range); }
@@ -1753,9 +1757,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,lru_add_drain ();mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, address, end); -if (is_vm_hugetlb_page (vma)) -adjust_range_if_pmd_sharing_possible (vma, &range.start, - &range.end); +hugetlb_zap_begin (vma, &range.start, &range.end);tlb_gather_mmu (&tlb, vma->vm_mm);update_hiwater_rss (vma->vm_mm);mmu_notifier_invalidate_range_start (&range);
@@ -1766,6 +1768,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,unmap_single_vma (&tlb, vma, address, end, details, false);mmu_notifier_invalidate_range_end (&range);tlb_finish_mmu (&tlb); +hugetlb_zap_end (vma, details); }/**
Signature · 1 line
2.41.0
Quoted 33 lines · On 10/05/23 23:59, [email protected] wrote:
From: Rik van Riel <[email protected]>
Malloc libraries, like jemalloc and tcalloc, take decisions on when to call madvise independently from the code in the main application.
This sometimes results in the application page faulting on an address, right after the malloc library has shot down the backing memory with
MADV_DONTNEED.Usually this is harmless, because we always have some 4kB pages sitting around to satisfy a page fault. However, with hugetlbfs systems often allocate only the exact number of huge pages that the application wants.
Due to TLB batching, hugetlbfs
MADV_DONTNEEDwill free pages outside of any lock taken on the page fault path, which can open up the following race condition:CPU 1 CPU 2
MADV_DONTNEED
unmap page
shoot down TLB entry
page fault
fail to allocate a huge page killed with SIGBUS
free pageFix that race by pulling the locking from
__unmap_hugepage_final_rangeinto helper functions called fromzap_page_range_single. This ensures page faults stay locked out of theMADV_DONTNEEDVMA until the huge pages have actually been freed.
Quoted 8 lines
Signed-off-by Rik van Riel <[email protected]>Fixes 04ada095dcfc ("hugetlb: don't delete vma_lock in hugetlb MADV_DONTNEED processing")--- include/linux/hugetlb.h | 35 +++++++++++++++++++++++++++++++++-- mm/hugetlb.c | 34 ++++++++++++++++++++++------------ mm/memory.c | 13 ++++++++----- 3 files changed, 63 insertions(+), 19 deletions(-)
Thanks for all the revisions,
Signature · 1 line
Mike Kravetz
PATCH v7 4/4 hugetlbfs: replace hugetlb_vma_lock with invalidate_lock
In reply to [email protected]From: Rik van Riel <[email protected]>
Replace the custom hugetlbfs VMA locking code with the recently introduced invalidate_lock. This greatly simplifies things.
However, this is a large enough change that it should probably go in separately from the other changes.
Another question is whether this simplification hurts scalability for certain workloads.
--- fs/hugetlbfs/inode.c | 71 ++---------- include/linux/fs.h | 6 + include/linux/hugetlb.h | 21 +--- mm/hugetlb.c | 237 ++++------------------------------------ 4 files changed, 35 insertions(+), 300 deletions(-)
fs/hugetlbfs/inode.c +7 -64
@@ -485,7 +485,6 @@ static void hugetlb_unmap_file_folio(struct hstate *h, struct folio *folio, pgoff_t index) {struct rb_root_cached *root = &mapping->i_mmap; -struct hugetlb_vma_lock *vma_lock;struct page *page = &folio->page;struct vm_area_struct *vma;unsigned long v_start;
@@ -495,9 +494,9 @@ static void hugetlb_unmap_file_folio(struct hstate *h, start = index *pages_per_huge_page (h); end = (index +1 ) *pages_per_huge_page (h); +filemap_invalidate_lock (mapping);i_mmap_lock_write (mapping); -retry: - vma_lock =NULL ; + vma_interval_tree_foreach(vma, root, start, end - 1) { v_start =vma_offset_start (vma, start); v_end =vma_offset_end (vma, end);
@@ -505,62 +504,12 @@ static void hugetlb_unmap_file_folio(struct hstate *h,if (!hugetlb_vma_maps_page (vma, v_start, page))continue ; -if (!hugetlb_vma_trylock_write (vma)) { - vma_lock = vma->vm_private_data; -/* -* If we can not get vma lock, we need to drop -* immap_sema and take locks in order. First, -* take a ref on the vma_lock structure so that -* we can be guaranteed it will not go away when -* dropping immap_sema. -*/ -kref_get (&vma_lock->refs); -break ; - } -unmap_hugepage_range (vma, v_start, v_end,NULL ,ZAP_FLAG_DROP_MARKER ); -hugetlb_vma_unlock_write (vma); } +filemap_invalidate_unlock (mapping);i_mmap_unlock_write (mapping); - -if (vma_lock) { -/* -* Wait on vma_lock. We know it is still valid as we have -* a reference. We must 'open code' vma locking as we do -* not know if vma_lock is still attached to vma. -*/ -down_write (&vma_lock->rw_sema); -i_mmap_lock_write (mapping); - - vma = vma_lock->vma; -if (!vma) { -/* -* If lock is no longer attached to vma, then just -* unlock, drop our reference and retry looking for -* other vmas. -*/ -up_write (&vma_lock->rw_sema); -kref_put (&vma_lock->refs, hugetlb_vma_lock_release); - goto retry; - } - -/* -* vma_lock is still attached to vma. Check to see if vma -* still maps page and if so, unmap. -*/ - v_start =vma_offset_start (vma, start); - v_end =vma_offset_end (vma, end); -if (hugetlb_vma_maps_page (vma, v_start, page)) -unmap_hugepage_range (vma, v_start, v_end,NULL , -ZAP_FLAG_DROP_MARKER ); - -kref_put (&vma_lock->refs, hugetlb_vma_lock_release); -hugetlb_vma_unlock_write (vma); - - goto retry; - } } static void
@@ -578,20 +527,10 @@ hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end,unsigned long v_start;unsigned long v_end; -if (!hugetlb_vma_trylock_write (vma)) -continue ; - v_start =vma_offset_start (vma, start); v_end =vma_offset_end (vma, end);unmap_hugepage_range (vma, v_start, v_end,NULL , zap_flags); - -/* -* Note that vma lock only exists for shared/non-private -* vmas. Therefore, lock is not held when calling -* unmap_hugepage_range for private vmas. -*/ -hugetlb_vma_unlock_write (vma); } }
@@ -725,10 +664,12 @@ static void hugetlb_vmtruncate(struct inode *inode, loff_t offset) pgoff = offset >>PAGE_SHIFT ;i_size_write (inode, offset); +filemap_invalidate_lock (mapping);i_mmap_lock_write (mapping);if (!RB_EMPTY_ROOT (&mapping->i_mmap.rb_root))hugetlb_vmdelete_list (&mapping->i_mmap, pgoff,0 ,ZAP_FLAG_DROP_MARKER ); +filemap_invalidate_unlock (mapping);i_mmap_unlock_write (mapping);remove_inode_hugepages (inode, offset,LLONG_MAX ); }
@@ -778,6 +719,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)return -EPERM ; } +filemap_invalidate_lock (mapping);i_mmap_lock_write (mapping);/* If range starts before first full page, zero partial page. */
@@ -799,6 +741,7 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) hole_end, offset + len);i_mmap_unlock_write (mapping); +filemap_invalidate_unlock (mapping);/* Remove full pages from the file. */ if (hole_end > hole_start)
include/linux/fs.h +6 -0
@@ -847,6 +847,12 @@ static inline void filemap_invalidate_lock(struct address_space *mapping)down_write (&mapping->invalidate_lock); } +static inline int filemap_invalidate_trylock ( +struct address_space *mapping) +{ +return down_write_trylock (&mapping->invalidate_lock); +} +static inline void filemap_invalidate_unlock (struct address_space *mapping) {up_write (&mapping->invalidate_lock);
include/linux/hugetlb.h +3 -18
@@ -60,7 +60,6 @@ struct resv_map {long adds_in_progress;struct list_head region_cache;long region_cache_count; -struct rw_semaphore rw_sema;#ifdef CONFIG_CGROUP_HUGETLB /* * On private mappings, the counter to uncharge reservations is stored
@@ -107,12 +106,6 @@ struct file_region {#endif }; -struct hugetlb_vma_lock { -struct kref refs; -struct rw_semaphore rw_sema; -struct vm_area_struct *vma; -}; -extern struct resv_map *resv_map_alloc (void );void resv_map_release (struct kref *ref);
@@ -1277,17 +1270,9 @@ hugetlb_walk(struct vm_area_struct *vma, unsigned long addr, unsigned long sz) {#if defined(CONFIG_HUGETLB_PAGE ) && \ defined(CONFIG_ARCH_WANT_HUGE_PMD_SHARE ) && defined(CONFIG_LOCKDEP ) -struct hugetlb_vma_lock *vma_lock = vma->vm_private_data; - -/* -* If pmd sharing possible, locking needed to safely walk the -* hugetlb pgtables. More information can be found at the comment -* above huge_pte_offset() in the same file. -* -* NOTE: lockdep_is_held() is only defined with CONFIG_LOCKDEP. -*/ -if (__vma_shareable_lock (vma)) -WARN_ON_ONCE (!lockdep_is_held (&vma_lock->rw_sema) && +if (vma->vm_file) +WARN_ON_ONCE (!lockdep_is_held ( + &vma->vm_file->f_mapping->invalidate_lock) && !lockdep_is_held ( &vma->vm_file->f_mapping->i_mmap_rwsem));#endif
mm/hugetlb.c +19 -218
354 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [PATCH 4/4] hugetlbfs: replace hugetlb_vma_lock with invalidate_lock
Quoted 10 lines · On 10/05/23 23:59, [email protected] wrote:
From: Rik van Riel <[email protected]>
Replace the custom hugetlbfs VMA locking code with the recently introduced
invalidate_lock. This greatly simplifies things.However, this is a large enough change that it should probably go in separately from the other changes.
Another question is whether this simplification hurts scalability for certain workloads.
Finally got around to running some performance tests on this.
As a reminder, the hugetlb specific vma lock was introduced as a result of this report:
https://lore.kernel.org/linux-mm/[email protected]/
I do not have access to the database or applications to recreate the issues originally reported. However, while working the issue I did use a simulated workload that showed the regression and improvements moving to a vma specific lock. Here is part of the commit log describing the testing when the vma lock was introduced.
"The recent regression report [1] notes page fault and fork latency of shared hugetlb mappings. To measure this, I created two simple programs:
1) map a shared hugetlb area, write fault all pages, unmap area Do this in a continuous loop to measure faults per second
2) map a shared hugetlb area, write fault a few pages, fork and exit Do this in a continuous loop to measure forks per second
These programs were run on a 48 CPU VM with 320GB memory. The shared mapping size was 250GB. For comparison, a single instance of the program was run. Then, multiple instances were run in parallel to introduce lock contention. Changing the locking scheme results in a significant performance benefit.
test instances unmodified revert vma
--------------------------------------------------------------------------
faults per sec 1 393043 395680 389932 faults per sec 24 71405 81191 79048 forks per sec 1 2802 2747 2725 forks per sec 24 439 536 500
Combined faults 24 1621 68070 53662
Combined forks 24 358 67 142
Combined test is when running both faulting program and forking program simultaneously."
Ray Fucillo (on Cc) verified the performance regression was removed when the vma lock was introduced.
I have run the same benchmark on this patch.
test instances before after
--------------------------------------------------------------------------
faults per sec 1 385135 386253 faults per sec 24 95922 75665 forks per sec 1 3392 3207 forks per sec 24 683 704
Combined faults 24 76004 30407
Combined forks 24 241 278
The Combined faults number drops by over 50%. This is not nearly as dramatic as the changes originally seen. However, I do expect that there will be a noticeable performance regression. Ray may be able to help running real workloads on real applications and database.
I suggest we hold off on adding this change until further, more real world analysis can be performed. The simplification of the code is nice, but I would hate to regress any workloads.
Signature · 1 line
Mike Kravetz
Re: [PATCH 4/4] hugetlbfs: replace hugetlb_vma_lock with invalidate_lock
Quoted 9 lines · Mike Kravetz
The Combined faults number drops by over 50%. This is not nearly as dramatic
as the changes originally seen. However, I do expect that there will be
a noticeable performance regression. Ray may be able to help running real
workloads on real applications and database.
Quoted 5 lines
I suggest we hold off on adding this change until further, more real world
analysis can be performed. The simplification of the code is nice, but I
would hate to regress any workloads.
Agreed. Thank you for running those tests.
Andrew, would it be possible to drop patch 4/4 from -mm?
Signature · 1 line
All Rights Reversed.