mm: madvise: use per_vma lock for MADV_FREE
Subsystems (3)
RFC PATCH 1/1 mm: madvise: use per_vma lock for MADV_FREE
From: Barry Song <[email protected]>
MADV_FREE is another option, besides MADV_DONTNEED, for dynamic memory freeing in user-space native or Java heap memory management. For example, jemalloc can be configured to use MADV_FREE, and recent versions of the Android Java heap have also increasingly adopted MADV_FREE. Supporting per-VMA locking for MADV_FREE thus appears increasingly necessary.
We have replaced walk_page_range() with walk_page_range_vma(). Along with the proposed madvise_lock_mode by Lorenzo, the necessary infrastructure is now in place to begin exploring per-VMA locking support for MADV_FREE and potentially other madvise using walk_page_range_vma().
This patch adds support for the PGWALK_VMA_RDLOCK walk_lock mode in walk_page_range_vma(), and leverages madvise_lock_mode from madv_behavior to select the appropriate walk_lock—either mmap_lock or per-VMA lock—based on the context.
To ensure thread safety, madvise_free_walk_ops is now defined as a stack variable instead of a global constant.
--- include/linux/pagewalk.h | 2 ++ mm/madvise.c | 20 ++++++++++++++------ mm/pagewalk.c | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-)
include/linux/pagewalk.h +2 -0
@@ -14,6 +14,8 @@ enum page_walk_lock {PGWALK_WRLOCK =1 ,/* vma is expected to be already write-locked during the walk */ PGWALK_WRLOCK_VERIFY =2 , +/* vma is expected to be already read-locked during the walk */ + PGWALK_VMA_RDLOCK_VERIFY = 3, };/**
mm/madvise.c +14 -6
@@ -775,10 +775,14 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,return 0 ; } -static const struct mm_walk_ops madvise_free_walk_ops = { - .pmd_entry = madvise_free_pte_range, - .walk_lock =PGWALK_RDLOCK , -}; +static inline enum page_walk_lock get_walk_lock (enum madvise_lock_mode mode) +{ +/* Other modes don't require fixing up the walk_lock. */ +VM_WARN_ON_ONCE (mode !=MADVISE_VMA_READ_LOCK && + mode !=MADVISE_MMAP_READ_LOCK ); +return mode ==MADVISE_VMA_READ_LOCK ? +PGWALK_VMA_RDLOCK_VERIFY :PGWALK_RDLOCK ; +} static int madvise_free_single_vma(struct madvise_behavior *madv_behavior, struct vm_area_struct *vma,
@@ -787,6 +791,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,struct mm_struct *mm = vma->vm_mm;struct mmu_notifier_range range;struct mmu_gather *tlb = madv_behavior->tlb; +struct mm_walk_ops walk_ops = { + .pmd_entry = madvise_free_pte_range, + };/* MADV_FREE works for only anon vma at the moment */ if (!vma_is_anonymous (vma))
@@ -806,8 +813,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,mmu_notifier_invalidate_range_start (&range);tlb_start_vma (tlb, vma); + walk_ops.walk_lock =get_walk_lock (madv_behavior->lock_mode);walk_page_range_vma (vma, range.start, range.end, - &madvise_free_walk_ops, tlb); + &walk_ops, tlb);tlb_end_vma (tlb, vma);mmu_notifier_invalidate_range_end (&range);return 0 ;
@@ -1653,7 +1661,6 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_WILLNEED :case MADV_COLD :case MADV_PAGEOUT : -case MADV_FREE :case MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE :
@@ -1662,6 +1669,7 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavireturn MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED : +case MADV_FREE :return MADVISE_VMA_READ_LOCK ;default :return MADVISE_MMAP_WRITE_LOCK ;
mm/pagewalk.c +6 -0
@@ -420,6 +420,9 @@ static int __walk_page_range(unsigned long start, unsigned long end,static inline void process_mm_walk_lock (struct mm_struct *mm,enum page_walk_lock walk_lock) { +if (walk_lock ==PGWALK_VMA_RDLOCK_VERIFY ) +return ; +if (walk_lock ==PGWALK_RDLOCK )mmap_assert_locked (mm);else
@@ -437,6 +440,9 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,case PGWALK_WRLOCK_VERIFY :vma_assert_write_locked (vma);break ; +case PGWALK_VMA_RDLOCK_VERIFY : +vma_assert_locked (vma); +break ;case PGWALK_RDLOCK :/* PGWALK_RDLOCK is handled by process_mm_walk_lock */ break ;
Signature · 1 line
2.39.3 (Apple Git-146)
Quoted 117 lines · Barry Song
From: Barry Song <[email protected]>
MADV_FREEis another option, besidesMADV_DONTNEED, for dynamic memory freeing in user-space native or Java heap memory management. For example, jemalloc can be configured to useMADV_FREE, and recent versions of the Android Java heap have also increasingly adoptedMADV_FREE. Supporting per-VMA locking forMADV_FREEthus appears increasingly necessary.We have replaced
walk_page_range()withwalk_page_range_vma(). Along with the proposedmadvise_lock_modeby Lorenzo, the necessary infrastructure is now in place to begin exploring per-VMA locking support forMADV_FREEand potentially other madvise usingwalk_page_range_vma().This patch adds support for the
PGWALK_VMA_RDLOCKwalk_lockmode inwalk_page_range_vma(), and leveragesmadvise_lock_modefrommadv_behaviorto select the appropriate walk_lock—eithermmap_lockor per-VMA lock—based on the context.To ensure thread safety,
madvise_free_walk_opsis now defined as a stack variable instead of a global constant.Cc Lorenzo Stoakes <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc David Hildenbrand <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Jann Horn <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Cc Mike Rapoport <[email protected]>Cc Michal Hocko <[email protected]>Cc Tangquan Zheng <[email protected]>Cc Qi Zheng <[email protected]>Signed-off-by Barry Song <[email protected]>--- include/linux/pagewalk.h | 2 ++ mm/madvise.c | 20 ++++++++++++++------ mm/pagewalk.c | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-)include/linux/pagewalk.h +2
-0@@ -14,6 +14,8 @@ enum page_walk_lock {PGWALK_WRLOCK =1 ,/* vma is expected to be already write-locked during the walk */ PGWALK_WRLOCK_VERIFY =2 , +/* vma is expected to be already read-locked during the walk */ + PGWALK_VMA_RDLOCK_VERIFY = 3, };/** mm/madvise.c +14
-6@@ -775,10 +775,14 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,return 0 ; } -static const struct mm_walk_ops madvise_free_walk_ops = { - .pmd_entry = madvise_free_pte_range, - .walk_lock =PGWALK_RDLOCK , -}; +static inline enum page_walk_lock get_walk_lock (enum madvise_lock_mode mode) +{ +/* Other modes don't require fixing up the walk_lock. */ +VM_WARN_ON_ONCE (mode !=MADVISE_VMA_READ_LOCK && + mode !=MADVISE_MMAP_READ_LOCK ); +return mode ==MADVISE_VMA_READ_LOCK ? +PGWALK_VMA_RDLOCK_VERIFY :PGWALK_RDLOCK ; +} static int madvise_free_single_vma(struct madvise_behavior *madv_behavior, struct vm_area_struct *vma,@@ -787,6 +791,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,struct mm_struct *mm = vma->vm_mm;struct mmu_notifier_range range;struct mmu_gather *tlb = madv_behavior->tlb; +struct mm_walk_ops walk_ops = { + .pmd_entry = madvise_free_pte_range, + };/* MADV_FREE works for only anon vma at the moment */ if (!vma_is_anonymous (vma))@@ -806,8 +813,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,mmu_notifier_invalidate_range_start (&range);tlb_start_vma (tlb, vma); + walk_ops.walk_lock =get_walk_lock (madv_behavior->lock_mode);walk_page_range_vma (vma, range.start, range.end, - &madvise_free_walk_ops, tlb); + &walk_ops, tlb);tlb_end_vma (tlb, vma);mmu_notifier_invalidate_range_end (&range);return 0 ;@@ -1653,7 +1661,6 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_WILLNEED :case MADV_COLD :case MADV_PAGEOUT : -case MADV_FREE :case MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE :@@ -1662,6 +1669,7 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavireturn MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED : +case MADV_FREE :return MADVISE_VMA_READ_LOCK ;default :return MADVISE_MMAP_WRITE_LOCK ;mm/pagewalk.c +0
-0
mm/pagewalk.c static int __walk_page_range(unsigned long start, unsigned long end, · line 427
Quoted 8 lines
mm/pagewalk.c +3
-0@@ -420,6 +420,9 @@ static int __walk_page_range(unsigned long start, unsigned long end,static inline void process_mm_walk_lock (struct mm_struct *mm,enum page_walk_lock walk_lock) { +if (walk_lock ==PGWALK_VMA_RDLOCK_VERIFY ) +return ; +if (walk_lock ==PGWALK_RDLOCK )mmap_assert_locked (mm);
Nit: I'd have converted the "else" into "else if (walk_lock != PGWALK_VMA_RDLOCK_VERIFY)
mm/pagewalk.c static inline void process_vma_walk_lock(struct vm_area_struct *vma, · line 448
Quoted 11 lines
mm/pagewalk.c +3
-0else @@ -437,6 +440,9 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,case PGWALK_WRLOCK_VERIFY :vma_assert_write_locked (vma);break ; +case PGWALK_VMA_RDLOCK_VERIFY : +vma_assert_locked (vma); +break ;case PGWALK_RDLOCK :/* PGWALK_RDLOCK is handled by process_mm_walk_lock */ break ;
Nothing jumped at me an I think this should be ok
Signature · 3 lines
Cheers, David / dhildenb
Quoted 122 lines · David Hildenbrand
Quoted 120 lines · Barry Song
From: Barry Song <[email protected]>
MADV_FREEis another option, besidesMADV_DONTNEED, for dynamic memory freeing in user-space native or Java heap memory management. For example, jemalloc can be configured to useMADV_FREE, and recent versions of the Android Java heap have also increasingly adoptedMADV_FREE. Supporting per-VMA locking forMADV_FREEthus appears increasingly necessary.We have replaced
walk_page_range()withwalk_page_range_vma(). Along with the proposedmadvise_lock_modeby Lorenzo, the necessary infrastructure is now in place to begin exploring per-VMA locking support forMADV_FREEand potentially other madvise usingwalk_page_range_vma().This patch adds support for the
PGWALK_VMA_RDLOCKwalk_lockmode inwalk_page_range_vma(), and leveragesmadvise_lock_modefrommadv_behaviorto select the appropriate walk_lock—eithermmap_lockor per-VMA lock—based on the context.To ensure thread safety,
madvise_free_walk_opsis now defined as a stack variable instead of a global constant.Cc Lorenzo Stoakes <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc David Hildenbrand <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Jann Horn <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Cc Mike Rapoport <[email protected]>Cc Michal Hocko <[email protected]>Cc Tangquan Zheng <[email protected]>Cc Qi Zheng <[email protected]>Signed-off-by Barry Song <[email protected]>--- include/linux/pagewalk.h | 2 ++ mm/madvise.c | 20 ++++++++++++++------ mm/pagewalk.c | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-)include/linux/pagewalk.h +2
-0@@ -14,6 +14,8 @@ enum page_walk_lock {PGWALK_WRLOCK =1 ,/* vma is expected to be already write-locked during the walk */ PGWALK_WRLOCK_VERIFY =2 , +/* vma is expected to be already read-locked during the walk */ + PGWALK_VMA_RDLOCK_VERIFY = 3, };/** mm/madvise.c +14
-6@@ -775,10 +775,14 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,return 0 ; } -static const struct mm_walk_ops madvise_free_walk_ops = { - .pmd_entry = madvise_free_pte_range, - .walk_lock =PGWALK_RDLOCK , -}; +static inline enum page_walk_lock get_walk_lock (enum madvise_lock_mode mode) +{ +/* Other modes don't require fixing up the walk_lock. */ +VM_WARN_ON_ONCE (mode !=MADVISE_VMA_READ_LOCK && + mode !=MADVISE_MMAP_READ_LOCK ); +return mode ==MADVISE_VMA_READ_LOCK ? +PGWALK_VMA_RDLOCK_VERIFY :PGWALK_RDLOCK ; +} static int madvise_free_single_vma(struct madvise_behavior *madv_behavior, struct vm_area_struct *vma,@@ -787,6 +791,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,struct mm_struct *mm = vma->vm_mm;struct mmu_notifier_range range;struct mmu_gather *tlb = madv_behavior->tlb; +struct mm_walk_ops walk_ops = { + .pmd_entry = madvise_free_pte_range, + };/* MADV_FREE works for only anon vma at the moment */ if (!vma_is_anonymous (vma))@@ -806,8 +813,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,mmu_notifier_invalidate_range_start (&range);tlb_start_vma (tlb, vma); + walk_ops.walk_lock =get_walk_lock (madv_behavior->lock_mode);walk_page_range_vma (vma, range.start, range.end, - &madvise_free_walk_ops, tlb); + &walk_ops, tlb);tlb_end_vma (tlb, vma);mmu_notifier_invalidate_range_end (&range);return 0 ;@@ -1653,7 +1661,6 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_WILLNEED :case MADV_COLD :case MADV_PAGEOUT : -case MADV_FREE :case MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE :@@ -1662,6 +1669,7 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavireturn MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED : +case MADV_FREE :return MADVISE_VMA_READ_LOCK ;default :return MADVISE_MMAP_WRITE_LOCK ;mm/pagewalk.c +0
-0@@ -420,6 +420,9 @@ static int __walk_page_range(unsigned long start, unsigned long end,static inline void process_mm_walk_lock (struct mm_struct *mm,enum page_walk_lock walk_lock) {
Quoted 8 lines
mm/pagewalk.c static int __walk_page_range(unsigned long start, unsigned long end, · line 427
Quoted 5 lines
mm/pagewalk.c +3
-0+if (walk_lock ==PGWALK_VMA_RDLOCK_VERIFY ) +return ; +if (walk_lock ==PGWALK_RDLOCK )mmap_assert_locked (mm);Nit: I'd have converted the "else" into "else if (
walk_lock!=PGWALK_VMA_RDLOCK_VERIFY)
Seems good to me.
Quoted 15 lines
mm/pagewalk.c static inline void process_vma_walk_lock(struct vm_area_struct *vma, · line 448
Quoted 12 lines
mm/pagewalk.c +3
-0else @@ -437,6 +440,9 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,case PGWALK_WRLOCK_VERIFY :vma_assert_write_locked (vma);break ; +case PGWALK_VMA_RDLOCK_VERIFY : +vma_assert_locked (vma); +break ;case PGWALK_RDLOCK :/* PGWALK_RDLOCK is handled by process_mm_walk_lock */ break ;Nothing jumped at me an I think this should be ok
Quoted 1 line
Thanks!
Quoted 6 lines
--
Cheers,David / dhildenb
Quoted 8 lines · Barry Song
From: Barry Song <[email protected]>
MADV_FREEis another option, besidesMADV_DONTNEED, for dynamic memory freeing in user-space native or Java heap memory management. For example, jemalloc can be configured to useMADV_FREE, and recent versions of the Android Java heap have also increasingly adoptedMADV_FREE. Supporting per-VMA locking forMADV_FREEthus appears increasingly necessary.
Quoted 4 lines
We have replaced
walk_page_range()withwalk_page_range_vma(). Along with the proposedmadvise_lock_modeby Lorenzo, the necessary infrastructure is now in place to begin exploring per-VMA locking support forMADV_FREEand potentially other madvise usingwalk_page_range_vma().
Thanks :)
Quoted 8 lines
This patch adds support for the
PGWALK_VMA_RDLOCKwalk_lockmode inwalk_page_range_vma(), and leveragesmadvise_lock_modefrommadv_behaviorto select the appropriate walk_lock—eithermmap_lockor per-VMA lock—based on the context.To ensure thread safety,
madvise_free_walk_opsis now defined as a stack variable instead of a global constant.
A nit but I'd add 'because we now dynamically update the walk_ops->walk_lock field we must make sure this is thread safe' or something like this to clarify the need for this
Did we not have to worry about this before I guess because the mmap lock would exclude other threads?
An aside, but I wonder if we have this implicit assumption elsewhere that VMA locks defeat... hm :)
Quoted 13 lines
Cc Lorenzo Stoakes <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc David Hildenbrand <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Jann Horn <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Cc Mike Rapoport <[email protected]>Cc Michal Hocko <[email protected]>Cc Tangquan Zheng <[email protected]>Cc Qi Zheng <[email protected]>Signed-off-by Barry Song <[email protected]>
Looks good to me, kinda neat how the previous work for the MADV_DONTNEED under VMA lock stuff made this pretty straightforward :)
So:
Quoted 26 lines
--- include/linux/pagewalk.h | 2 ++ mm/madvise.c | 20 ++++++++++++++------ mm/pagewalk.c | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-)include/linux/pagewalk.h +2
-0@@ -14,6 +14,8 @@ enum page_walk_lock {PGWALK_WRLOCK =1 ,/* vma is expected to be already write-locked during the walk */ PGWALK_WRLOCK_VERIFY =2 , +/* vma is expected to be already read-locked during the walk */ + PGWALK_VMA_RDLOCK_VERIFY = 3, };/** mm/madvise.c +0
-0@@ -775,10 +775,14 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,return 0 ; }
mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 782
Quoted 10 lines
mm/madvise.c +5
-4-static const struct mm_walk_ops madvise_free_walk_ops = { - .pmd_entry = madvise_free_pte_range, - .walk_lock =PGWALK_RDLOCK , -}; +static inline enum page_walk_lock get_walk_lock (enum madvise_lock_mode mode) +{ +/* Other modes don't require fixing up the walk_lock. */ +VM_WARN_ON_ONCE (mode !=MADVISE_VMA_READ_LOCK && + mode !=MADVISE_MMAP_READ_LOCK );
I find this a bit hard to parse...
mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 784
Quoted 2 lines
mm/madvise.c +2
-0+return mode ==MADVISE_VMA_READ_LOCK ? +PGWALK_VMA_RDLOCK_VERIFY :PGWALK_RDLOCK ;
...might be better as something like:
switch (mode) {
case MADVISE_VMA_READ_LOCK:
return PGWALK_VMA_RDLOCK_VERIFY;
case MADVISE_MMAP_READ_LOCK:
return PGWALK_RDLOCK;
default:
/* Invalid. */
WARN_ON_ONCE(1);
return PGWALK_RDLOCK;
}Quoted 68 lines
mm/madvise.c +7
-2+} static int madvise_free_single_vma(struct madvise_behavior *madv_behavior, struct vm_area_struct *vma,@@ -787,6 +791,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,struct mm_struct *mm = vma->vm_mm;struct mmu_notifier_range range;struct mmu_gather *tlb = madv_behavior->tlb; +struct mm_walk_ops walk_ops = { + .pmd_entry = madvise_free_pte_range, + };/* MADV_FREE works for only anon vma at the moment */ if (!vma_is_anonymous (vma))@@ -806,8 +813,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,mmu_notifier_invalidate_range_start (&range);tlb_start_vma (tlb, vma); + walk_ops.walk_lock =get_walk_lock (madv_behavior->lock_mode);walk_page_range_vma (vma, range.start, range.end, - &madvise_free_walk_ops, tlb); + &walk_ops, tlb);tlb_end_vma (tlb, vma);mmu_notifier_invalidate_range_end (&range);return 0 ;@@ -1653,7 +1661,6 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_WILLNEED :case MADV_COLD :case MADV_PAGEOUT : -case MADV_FREE :case MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE :@@ -1662,6 +1669,7 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavireturn MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED : +case MADV_FREE :return MADVISE_VMA_READ_LOCK ;default :return MADVISE_MMAP_WRITE_LOCK ;mm/pagewalk.c +6
-1@@ -420,6 +420,9 @@ static int __walk_page_range(unsigned long start, unsigned long end,static inline void process_mm_walk_lock (struct mm_struct *mm,enum page_walk_lock walk_lock) { +if (walk_lock ==PGWALK_VMA_RDLOCK_VERIFY ) +return ; +if (walk_lock ==PGWALK_RDLOCK )mmap_assert_locked (mm);else @@ -437,6 +440,9 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,case PGWALK_WRLOCK_VERIFY :vma_assert_write_locked (vma);break ; +case PGWALK_VMA_RDLOCK_VERIFY : +vma_assert_locked (vma); +break ;case PGWALK_RDLOCK :/* PGWALK_RDLOCK is handled by process_mm_walk_lock */ break ; --2.39.3 (Apple Git-146)
Quoted 23 lines · Lorenzo Stoakes
Quoted 8 lines · Barry Song
From: Barry Song <[email protected]>
MADV_FREEis another option, besidesMADV_DONTNEED, for dynamic memory freeing in user-space native or Java heap memory management. For example, jemalloc can be configured to useMADV_FREE, and recent versions of the Android Java heap have also increasingly adoptedMADV_FREE. Supporting per-VMA locking forMADV_FREEthus appears increasingly necessary.Quoted 4 lines
We have replaced
walk_page_range()withwalk_page_range_vma(). Along with the proposedmadvise_lock_modeby Lorenzo, the necessary infrastructure is now in place to begin exploring per-VMA locking support forMADV_FREEand potentially other madvise usingwalk_page_range_vma().Thanks :)
Quoted 6 lines
This patch adds support for the
PGWALK_VMA_RDLOCKwalk_lockmode inwalk_page_range_vma(), and leveragesmadvise_lock_modefrommadv_behaviorto select the appropriate walk_lock—eithermmap_lockor per-VMA lock—based on the context.
Quoted 6 lines
Quoted 2 lines
To ensure thread safety,
madvise_free_walk_opsis now defined as a stack variable instead of a global constant.A nit but I'd add 'because we now dynamically update the
walk_ops->walk_lockfield we must make sure this is thread safe' or something like this to clarify the need for this
Sure.
Quoted 3 lines
Did we not have to worry about this before I guess because the mmap lock would exclude other threads?
Probably not. It was a constant, and no one needed to modify it before, no matter how many threads called MADV_FREE.
Quoted 18 lines
An aside, but I wonder if we have this implicit assumption elsewhere that VMA locks defeat... hm :)
Quoted 13 lines
Cc Lorenzo Stoakes <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc David Hildenbrand <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Jann Horn <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Cc Mike Rapoport <[email protected]>Cc Michal Hocko <[email protected]>Cc Tangquan Zheng <[email protected]>Cc Qi Zheng <[email protected]>Signed-off-by Barry Song <[email protected]>
Quoted 6 lines
Looks good to me, kinda neat how the previous work for the
MADV_DONTNEEDunder VMA lock stuff made this pretty straightforward :)So:
Reviewed-by Lorenzo Stoakes <[email protected]>
Thanks!
Quoted 45 lines
Quoted 27 lines
--- include/linux/pagewalk.h | 2 ++ mm/madvise.c | 20 ++++++++++++++------ mm/pagewalk.c | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-)include/linux/pagewalk.h +2
-0@@ -14,6 +14,8 @@ enum page_walk_lock {PGWALK_WRLOCK =1 ,/* vma is expected to be already write-locked during the walk */ PGWALK_WRLOCK_VERIFY =2 , +/* vma is expected to be already read-locked during the walk */ + PGWALK_VMA_RDLOCK_VERIFY = 3, };/** mm/madvise.c +0
-0@@ -775,10 +775,14 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,return 0 ; }mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 782
Quoted 10 lines
mm/madvise.c +5
-4-static const struct mm_walk_ops madvise_free_walk_ops = { - .pmd_entry = madvise_free_pte_range, - .walk_lock =PGWALK_RDLOCK , -}; +static inline enum page_walk_lock get_walk_lock (enum madvise_lock_mode mode) +{ +/* Other modes don't require fixing up the walk_lock. */ +VM_WARN_ON_ONCE (mode !=MADVISE_VMA_READ_LOCK && + mode !=MADVISE_MMAP_READ_LOCK );I find this a bit hard to parse...
mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 784
Quoted 2 lines
mm/madvise.c +2
-0+return mode ==MADVISE_VMA_READ_LOCK ? +PGWALK_VMA_RDLOCK_VERIFY :PGWALK_RDLOCK ;...might be better as something like:
Quoted 10 lines
switch (mode) {
case MADVISE_VMA_READ_LOCK:
return PGWALK_VMA_RDLOCK_VERIFY;
case MADVISE_MMAP_READ_LOCK:
return PGWALK_RDLOCK;
default:
/* Invalid. */
WARN_ON_ONCE(1);
return PGWALK_RDLOCK;
}I actually tried both before sending and, for some reason, preferred the one I sent. But I'm totally happy to go with whichever approach you prefer:-)
Quoted 61 lines
Quoted 61 lines
mm/madvise.c +7
-2+} static int madvise_free_single_vma(struct madvise_behavior *madv_behavior, struct vm_area_struct *vma,@@ -787,6 +791,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,struct mm_struct *mm = vma->vm_mm;struct mmu_notifier_range range;struct mmu_gather *tlb = madv_behavior->tlb; +struct mm_walk_ops walk_ops = { + .pmd_entry = madvise_free_pte_range, + };/* MADV_FREE works for only anon vma at the moment */ if (!vma_is_anonymous (vma))@@ -806,8 +813,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,mmu_notifier_invalidate_range_start (&range);tlb_start_vma (tlb, vma); + walk_ops.walk_lock =get_walk_lock (madv_behavior->lock_mode);walk_page_range_vma (vma, range.start, range.end, - &madvise_free_walk_ops, tlb); + &walk_ops, tlb);tlb_end_vma (tlb, vma);mmu_notifier_invalidate_range_end (&range);return 0 ;@@ -1653,7 +1661,6 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_WILLNEED :case MADV_COLD :case MADV_PAGEOUT : -case MADV_FREE :case MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE :@@ -1662,6 +1669,7 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavireturn MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED : +case MADV_FREE :return MADVISE_VMA_READ_LOCK ;default :return MADVISE_MMAP_WRITE_LOCK ;mm/pagewalk.c +4
-0@@ -420,6 +420,9 @@ static int __walk_page_range(unsigned long start, unsigned long end,static inline void process_mm_walk_lock (struct mm_struct *mm,enum page_walk_lock walk_lock) { +if (walk_lock ==PGWALK_VMA_RDLOCK_VERIFY ) +return ; +if (walk_lock ==PGWALK_RDLOCK )mmap_assert_locked (mm);else @@ -437,6 +440,9 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,case PGWALK_WRLOCK_VERIFY :vma_assert_write_locked (vma);break ; +case PGWALK_VMA_RDLOCK_VERIFY :
Quoted 8 lines
Quoted 8 lines
mm/pagewalk.c +2
-1+vma_assert_locked (vma); +break ;case PGWALK_RDLOCK :/* PGWALK_RDLOCK is handled by process_mm_walk_lock */ break ; --2.39.3 (Apple Git-146)
Thanks
Barry
Quoted 25 lines · Barry Song
Quoted 23 lines · Lorenzo Stoakes
Quoted 8 lines · Barry Song
From: Barry Song <[email protected]>
MADV_FREEis another option, besidesMADV_DONTNEED, for dynamic memory freeing in user-space native or Java heap memory management. For example, jemalloc can be configured to useMADV_FREE, and recent versions of the Android Java heap have also increasingly adoptedMADV_FREE. Supporting per-VMA locking forMADV_FREEthus appears increasingly necessary.Quoted 4 lines
We have replaced
walk_page_range()withwalk_page_range_vma(). Along with the proposedmadvise_lock_modeby Lorenzo, the necessary infrastructure is now in place to begin exploring per-VMA locking support forMADV_FREEand potentially other madvise usingwalk_page_range_vma().Thanks :)
Quoted 6 lines
This patch adds support for the
PGWALK_VMA_RDLOCKwalk_lockmode inwalk_page_range_vma(), and leveragesmadvise_lock_modefrommadv_behaviorto select the appropriate walk_lock—eithermmap_lockor per-VMA lock—based on the context.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
To ensure thread safety,
madvise_free_walk_opsis now defined as a stack variable instead of a global constant.A nit but I'd add 'because we now dynamically update the
walk_ops->walk_lockfield we must make sure this is thread safe' or something like this to clarify the need for thisSure.
Thanks!
Quoted 7 lines
Quoted 4 lines
Did we not have to worry about this before I guess because the mmap lock would exclude other threads?
Probably not. It was a constant, and no one needed to modify it before, no matter how many threads called
MADV_FREE.
Yeah of course, I wrote this before I went and looked more carefully and comment preceding this and... anyway yeah :)
Quoted 4 lines
Quoted 4 lines
An aside, but I wonder if we have this implicit assumption elsewhere that VMA locks defeat... hm :)
This is more the concern, but not to do with your series but more a general thing 'in how many places do we make implicit assumptions about things like thread concurrency due to locking we presume we have?'
Hopefully this isn't common :>)
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Cc Lorenzo Stoakes <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc David Hildenbrand <[email protected]>Cc Vlastimil Babka <[email protected]>Cc Jann Horn <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Lokesh Gidra <[email protected]>Cc Mike Rapoport <[email protected]>Cc Michal Hocko <[email protected]>Cc Tangquan Zheng <[email protected]>Cc Qi Zheng <[email protected]>Signed-off-by Barry Song <[email protected]>
Quoted 8 lines
Quoted 6 lines
Looks good to me, kinda neat how the previous work for the
MADV_DONTNEEDunder VMA lock stuff made this pretty straightforward :)So:
Reviewed-by Lorenzo Stoakes <[email protected]>Thanks!
No problem! Thanks for the patch, this is a nice step forward.
Quoted 57 lines
Quoted 46 lines
Quoted 28 lines
--- include/linux/pagewalk.h | 2 ++ mm/madvise.c | 20 ++++++++++++++------ mm/pagewalk.c | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-)include/linux/pagewalk.h +2
-0@@ -14,6 +14,8 @@ enum page_walk_lock {PGWALK_WRLOCK =1 ,/* vma is expected to be already write-locked during the walk */ PGWALK_WRLOCK_VERIFY =2 , +/* vma is expected to be already read-locked during the walk */ + PGWALK_VMA_RDLOCK_VERIFY = 3, };/** mm/madvise.c +0
-0@@ -775,10 +775,14 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,return 0 ; }mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 782
Quoted 10 lines
mm/madvise.c +5
-4-static const struct mm_walk_ops madvise_free_walk_ops = { - .pmd_entry = madvise_free_pte_range, - .walk_lock =PGWALK_RDLOCK , -}; +static inline enum page_walk_lock get_walk_lock (enum madvise_lock_mode mode) +{ +/* Other modes don't require fixing up the walk_lock. */ +VM_WARN_ON_ONCE (mode !=MADVISE_VMA_READ_LOCK && + mode !=MADVISE_MMAP_READ_LOCK );I find this a bit hard to parse...
mm/madvise.c static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, · line 784
Quoted 2 lines
mm/madvise.c +2
-0+return mode ==MADVISE_VMA_READ_LOCK ? +PGWALK_VMA_RDLOCK_VERIFY :PGWALK_RDLOCK ;...might be better as something like:
Quoted 10 lines
switch (mode) { case MADVISE_VMA_READ_LOCK: return PGWALK_VMA_RDLOCK_VERIFY; case MADVISE_MMAP_READ_LOCK: return PGWALK_RDLOCK; default: /* Invalid. */ WARN_ON_ONCE(1); return PGWALK_RDLOCK; }
Quoted 3 lines
I actually tried both before sending and, for some reason, preferred the one I sent. But I'm totally happy to go with whichever approach you prefer:-)
It's not a big deal, obviously I'd prefer this version but not a blocker!
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
mm/madvise.c +7
-2+} static int madvise_free_single_vma(struct madvise_behavior *madv_behavior, struct vm_area_struct *vma,@@ -787,6 +791,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,struct mm_struct *mm = vma->vm_mm;struct mmu_notifier_range range;struct mmu_gather *tlb = madv_behavior->tlb; +struct mm_walk_ops walk_ops = { + .pmd_entry = madvise_free_pte_range, + };/* MADV_FREE works for only anon vma at the moment */ if (!vma_is_anonymous (vma))@@ -806,8 +813,9 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior,mmu_notifier_invalidate_range_start (&range);tlb_start_vma (tlb, vma); + walk_ops.walk_lock =get_walk_lock (madv_behavior->lock_mode);walk_page_range_vma (vma, range.start, range.end, - &madvise_free_walk_ops, tlb); + &walk_ops, tlb);tlb_end_vma (tlb, vma);mmu_notifier_invalidate_range_end (&range);return 0 ;@@ -1653,7 +1661,6 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavicase MADV_WILLNEED :case MADV_COLD :case MADV_PAGEOUT : -case MADV_FREE :case MADV_POPULATE_READ :case MADV_POPULATE_WRITE :case MADV_COLLAPSE :@@ -1662,6 +1669,7 @@ static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavireturn MADVISE_MMAP_READ_LOCK ;case MADV_DONTNEED :case MADV_DONTNEED_LOCKED : +case MADV_FREE :return MADVISE_VMA_READ_LOCK ;default :return MADVISE_MMAP_WRITE_LOCK ;mm/pagewalk.c +6
-0@@ -420,6 +420,9 @@ static int __walk_page_range(unsigned long start, unsigned long end,static inline void process_mm_walk_lock (struct mm_struct *mm,enum page_walk_lock walk_lock) { +if (walk_lock ==PGWALK_VMA_RDLOCK_VERIFY ) +return ; +if (walk_lock ==PGWALK_RDLOCK )mmap_assert_locked (mm);else @@ -437,6 +440,9 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma,case PGWALK_WRLOCK_VERIFY :vma_assert_write_locked (vma);break ; +case PGWALK_VMA_RDLOCK_VERIFY : +vma_assert_locked (vma); +break ;case PGWALK_RDLOCK :
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
mm/pagewalk.c +0
-1/* PGWALK_RDLOCK is handled by process_mm_walk_lock */ break ; --2.39.3 (Apple Git-146)
Thanks
Barry
Cheers, Lorenzo