mm: cleanups around unmapping / zapping
Subsystems (28)
David Hildenbrand (Arm)
maintainercover mm: cleanups around unmapping / zapping
A bunch of cleanups around unmapping and zapping. Mostly simplifications, code movements, documentation and renaming of zapping functions.
With this series, we'll have the following high-level zap/unmap functions (excluding high-level folio zapping):
* unmap_vmas() for actual unmapping (vmas will go away)
* zap_vma(): zap all page table entries in a vma
* zap_vma_for_reaping(): zap_vma() that must not block
* zap_vma_range(): zap a range of page table entries
* zap_vma_range_batched(): zap_vma_range() with more options and batching
* zap_special_vma_range(): limited zap_vma_range() for modules
* __zap_vma_range(): internal helper
Patch #1 is not about unmapping/zapping, but I stumbled over it while verifying MADV_DONTNEED range handling.
Patch #16 is related to [1], but makes sense even independent of that.
[1] https://lore.kernel.org/r/[email protected]
The CC list is already long enough. As these are simple changes to drivers/arch code, I'm only CCing maintainers of all changes but only reviewers of the MM bits.
David Hildenbrand (Arm) (16):
mm/madvise: drop range checks in madvise_free_single_vma() mm/memory: remove "zap_details" parameter from zap_page_range_single() mm/memory: inline unmap_mapping_range_vma() into
unmap_mapping_range_tree()
mm/memory: simplify calculation in unmap_mapping_range_tree() mm/oom_kill: use MMU_NOTIFY_CLEAR in __oom_reap_task_mm() mm/oom_kill: factor out zapping of VMA into zap_vma_for_reaping() mm/memory: rename unmap_single_vma() to __zap_vma_range() mm/memory: move adjusting of address range to unmap_vmas() mm/memory: convert details->even_cows into details->skip_cows mm/memory: use __zap_vma_range() in zap_vma_for_reaping() mm/memory: inline unmap_page_range() into __zap_vma_range()
mm: rename zap_vma_pages() to zap_vma()
mm: rename zap_page_range_single_batched() to zap_vma_range_batched()
mm: rename zap_page_range_single() to zap_vma_range()
mm: rename zap_vma_ptes() to zap_special_vma_range() mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
arch/powerpc/platforms/book3s/vas-api.c | 2 +- arch/powerpc/platforms/pseries/vas.c | 2 +- arch/s390/mm/gmap_helpers.c | 2 +- arch/x86/kernel/cpu/sgx/encl.c | 2 +- drivers/android/binder/page_range.rs | 4 +- drivers/android/binder_alloc.c | 2 +- drivers/comedi/comedi_fops.c | 2 +- drivers/gpu/drm/i915/i915_mm.c | 4 +- drivers/infiniband/core/uverbs_main.c | 6 +- drivers/misc/sgi-gru/grumain.c | 2 +- include/linux/mm.h | 23 ++- kernel/bpf/arena.c | 3 +- kernel/events/core.c | 2 +- lib/vdso/datastore.c | 2 +- mm/internal.h | 7 +- mm/interval_tree.c | 5 - mm/madvise.c | 24 +-- mm/memory.c | 217 ++++++++++++------------ mm/oom_kill.c | 15 +- mm/page-writeback.c | 2 +- net/ipv4/tcp.c | 7 +- rust/kernel/mm/virt.rs | 4 +- 22 files changed, 162 insertions(+), 177 deletions(-)
base-commit: df9c51269a5e2a6fbca2884a756a4011a5e78748
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 1/16 mm/madvise: drop range checks in madvise_free_single_vma()
madvise_vma_behavior()-> madvise_dontneed_free()->madvise_free_single_vma() is only called from madvise_walk_vmas()
(a) After try_vma_read_lock() confirmed that the whole range falls into a single VMA (see is_vma_lock_sufficient()).
(b) After adjusting the range to the VMA in the loop afterwards.
madvise_dontneed_free() might drop the MM lock when handling userfaultfd, but it properly looks up the VMA again to adjust the range.
So in madvise_free_single_vma(), the given range should always fall into a single VMA and should also span at least one page.
Let's drop the error checks.
The code now matches what we do in madvise_dontneed_single_vma(), where we call zap_vma_range_batched() that documents: "The range must fit into one VMA.". Although that function still adjusts that range, we'll change that soon.
--- mm/madvise.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
mm/madvise.c +4 -9
@@ -799,9 +799,10 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior) {struct mm_struct *mm = madv_behavior->mm;struct vm_area_struct *vma = madv_behavior->vma; -unsigned long start_addr = madv_behavior->range.start; -unsigned long end_addr = madv_behavior->range.end; -struct mmu_notifier_range range; +struct mmu_notifier_range range = { + .start = madv_behavior->range.start, + .end = madv_behavior->range.end, + };struct mmu_gather *tlb = madv_behavior->tlb;struct mm_walk_ops walk_ops = { .pmd_entry = madvise_free_pte_range,
@@ -811,12 +812,6 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)if (!vma_is_anonymous (vma))return -EINVAL ; - range.start =max (vma->vm_start, start_addr); -if (range.start >= vma->vm_end) -return -EINVAL ; - range.end =min (vma->vm_end, end_addr); -if (range.end <= vma->vm_start) -return -EINVAL ;mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, range.start, range.end);
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 2/16 mm/memory: remove "zap_details" parameter from zap_page_range_single()
In reply to David Hildenbrand (Arm)Nobody except memory.c should really set that parameter to non-NULL. So let's just drop it and make unmap_mapping_range_vma() use zap_page_range_single_batched() instead.
--- arch/s390/mm/gmap_helpers.c | 2 +- drivers/android/binder_alloc.c | 2 +- include/linux/mm.h | 5 ++--- kernel/bpf/arena.c | 3 +-- kernel/events/core.c | 2 +- mm/madvise.c | 3 +-- mm/memory.c | 16 ++++++++++------ net/ipv4/tcp.c | 5 ++--- rust/kernel/mm/virt.rs | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-)
arch/s390/mm/gmap_helpers.c +1 -1
@@ -89,7 +89,7 @@ void gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned loif (!vma)return ;if (!is_vm_hugetlb_page (vma)) -zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr,NULL ); +zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr); vmaddr = vma->vm_end; } }
drivers/android/binder_alloc.c +1 -1
@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ,NULL ); +zap_page_range_single (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index); }
include/linux/mm.h +2 -3
@@ -2835,11 +2835,10 @@ struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr,void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address,unsigned long size);void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, -unsigned long size,struct zap_details *details); +unsigned long size);static inline void zap_vma_pages (struct vm_area_struct *vma) { -zap_page_range_single (vma, vma->vm_start, - vma->vm_end - vma->vm_start,NULL ); +zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); }struct mmu_notifier_range ;
kernel/bpf/arena.c +1 -2
@@ -656,8 +656,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt)guard (mutex)(&arena->lock);/* iterate link list under lock */ list_for_each_entry (vml, &arena->vma_list, head) -zap_page_range_single (vml->vma, uaddr, -PAGE_SIZE * page_cnt,NULL ); +zap_page_range_single (vml->vma, uaddr,PAGE_SIZE * page_cnt); }static void arena_free_pages (struct bpf_arena *arena,long uaddr,long page_cnt,bool sleepable)
kernel/events/core.c +1 -1
@@ -7215,7 +7215,7 @@ static int map_range(struct perf_buffer *rb, struct vm_area_struct *vma)#ifdef CONFIG_MMU /* Clear any partial mappings on error. */ if (err) -zap_page_range_single (vma, vma->vm_start, nr_pages *PAGE_SIZE ,NULL ); +zap_page_range_single (vma, vma->vm_start, nr_pages *PAGE_SIZE );#endif return err;
mm/madvise.c +1 -2
@@ -1193,8 +1193,7 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)* OK some of the range have non-guard pages mapped, zap * them. This leaves existing guard pages in place. */ -zap_page_range_single (vma, range->start, - range->end - range->start,NULL ); +zap_page_range_single (vma, range->start, range->end - range->start); }/*
mm/memory.c +10 -6
@@ -2203,17 +2203,16 @@ void zap_page_range_single_batched(struct mmu_gather *tlb,* @vma: vm_area_struct holding the applicable pages * @address: starting address of pages to zap * @size: number of bytes to zap -* @details: details of shared cache invalidation * * The range must fit into one VMA. */ void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, -unsigned long size,struct zap_details *details) +unsigned long size) {struct mmu_gather tlb;tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, address, size, details); +zap_page_range_single_batched (&tlb, vma, address, size,NULL );tlb_finish_mmu (&tlb); }
@@ -2235,7 +2234,7 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, !(vma->vm_flags & VM_PFNMAP))return ; -zap_page_range_single (vma, address, size,NULL ); +zap_page_range_single (vma, address, size); }EXPORT_SYMBOL_GPL (zap_vma_ptes);
@@ -3003,7 +3002,7 @@ static int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long add* maintain page reference counts, and callers may free * pages due to the error. So zap it early. */ -zap_page_range_single (vma, addr, size,NULL ); +zap_page_range_single (vma, addr, size);return error; }
@@ -4226,7 +4225,12 @@ static void unmap_mapping_range_vma(struct vm_area_struct *vma, unsigned long start_addr, unsigned long end_addr, struct zap_details *details) { - zap_page_range_single(vma, start_addr, end_addr - start_addr, details); + struct mmu_gather tlb; + +tlb_gather_mmu (&tlb, vma->vm_mm); +zap_page_range_single_batched (&tlb, vma, start_addr, + end_addr - start_addr, details); +tlb_finish_mmu (&tlb); } static inline void unmap_mapping_range_tree(struct rb_root_cached *root,
net/ipv4/tcp.c +2 -3
@@ -2104,7 +2104,7 @@ static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma, maybe_zap_len = total_bytes_to_map -/* All bytes to map */ *length +/* Mapped or pending */ (pages_remaining *PAGE_SIZE );/* Failed map. */ -zap_page_range_single (vma, *address, maybe_zap_len,NULL ); +zap_page_range_single (vma, *address, maybe_zap_len); err =0 ; }
@@ -2269,8 +2269,7 @@ static int tcp_zerocopy_receive(struct sock *sk, total_bytes_to_map = avail_len & ~(PAGE_SIZE -1 );if (total_bytes_to_map) {if (!(zc->flags &TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT )) -zap_page_range_single (vma, address, total_bytes_to_map, -NULL ); +zap_page_range_single (vma, address, total_bytes_to_map); zc->length = total_bytes_to_map; zc->recv_skip_hint =0 ; }else {
rust/kernel/mm/virt.rs +1 -1
@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size, core::ptr::null_mut ()) + bindings::zap_page_range_single (self.as_ptr (), address, size) }; }
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 3/16 mm/memory: inline unmap_mapping_range_vma() into unmap_mapping_range_tree()
In reply to David Hildenbrand (Arm)Let's remove the number of unmap-related functions that cause confusion by inlining unmap_mapping_range_vma() into its single caller. The end result looks pretty readable.
--- mm/memory.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-)
mm/memory.c +7 -16
@@ -4221,18 +4221,6 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)return wp_page_copy (vmf); } -static void unmap_mapping_range_vma (struct vm_area_struct *vma, -unsigned long start_addr,unsigned long end_addr, -struct zap_details *details) -{ -struct mmu_gather tlb; - -tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, start_addr, - end_addr - start_addr, details); -tlb_finish_mmu (&tlb); -} - static inline void unmap_mapping_range_tree(struct rb_root_cached *root, pgoff_t first_index, pgoff_t last_index,
@@ -4240,17 +4228,20 @@ static inline void unmap_mapping_range_tree(struct rb_root_cached *root, {struct vm_area_struct *vma;pgoff_t vba, vea, zba, zea; +unsigned long start, size; +struct mmu_gather tlb; vma_interval_tree_foreach(vma, root, first_index, last_index) { vba = vma->vm_pgoff; vea = vba +vma_pages (vma) -1 ; zba =max (first_index, vba); zea =min (last_index, vea); + start = ((zba - vba) <<PAGE_SHIFT ) + vma->vm_start; + size = (zea - zba +1 ) <<PAGE_SHIFT ; -unmap_mapping_range_vma (vma, - ((zba - vba) <<PAGE_SHIFT ) + vma->vm_start, - ((zea - vba +1 ) <<PAGE_SHIFT ) + vma->vm_start, - details); +tlb_gather_mmu (&tlb, vma->vm_mm); +zap_page_range_single_batched (&tlb, vma, start, size, details); +tlb_finish_mmu (&tlb); } }
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 4/16 mm/memory: simplify calculation in unmap_mapping_range_tree()
In reply to David Hildenbrand (Arm)Let's simplify the calculation a bit further to make it easier to get, reusing vma_last_pgoff() which we move from interval_tree.c to mm.h.
--- include/linux/mm.h | 5 +++++ mm/interval_tree.c | 5 ----- mm/memory.c | 12 +++++------- 3 files changed, 10 insertions(+), 12 deletions(-)
include/linux/mm.h +5 -0
@@ -4000,6 +4000,11 @@ static inline unsigned long vma_pages(const struct vm_area_struct *vma)return (vma->vm_end - vma->vm_start) >>PAGE_SHIFT ; } +static inline unsigned long vma_last_pgoff (struct vm_area_struct *vma) +{ +return vma->vm_pgoff +vma_pages (vma) -1 ; +} +static inline unsigned long vma_desc_size (const struct vm_area_desc *desc) {return desc->end - desc->start;
mm/interval_tree.c +0 -5
@@ -15,11 +15,6 @@ static inline unsigned long vma_start_pgoff(struct vm_area_struct *v)return v->vm_pgoff; } -static inline unsigned long vma_last_pgoff (struct vm_area_struct *v) -{ -return v->vm_pgoff +vma_pages (v) -1 ; -} - INTERVAL_TREE_DEFINE(struct vm_area_struct, shared.rb, unsigned long, shared.rb_subtree_last, vma_start_pgoff, vma_last_pgoff, /* empty */, vma_interval_tree)
mm/memory.c +5 -7
@@ -4227,17 +4227,15 @@ static inline void unmap_mapping_range_tree(struct rb_root_cached *root, struct zap_details *details) {struct vm_area_struct *vma; -pgoff_t vba, vea, zba, zea;unsigned long start, size;struct mmu_gather tlb; vma_interval_tree_foreach(vma, root, first_index, last_index) { - vba = vma->vm_pgoff; - vea = vba +vma_pages (vma) -1 ; - zba =max (first_index, vba); - zea =min (last_index, vea); - start = ((zba - vba) <<PAGE_SHIFT ) + vma->vm_start; - size = (zea - zba +1 ) <<PAGE_SHIFT ; +const pgoff_t start_idx =max (first_index, vma->vm_pgoff); +const pgoff_t end_idx =min (last_index,vma_last_pgoff (vma)) +1 ; + + start = vma->vm_start + ((start_idx - vma->vm_pgoff) <<PAGE_SHIFT ); + size = (end_idx - start_idx) <<PAGE_SHIFT ;tlb_gather_mmu (&tlb, vma->vm_mm);zap_page_range_single_batched (&tlb, vma, start, size, details);
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 5/16 mm/oom_kill: use MMU_NOTIFY_CLEAR in __oom_reap_task_mm()
In reply to David Hildenbrand (Arm)In commit 7269f999934b ("mm/mmu_notifier: use correct mmu_notifier events for each invalidation") we converted all MMU_NOTIFY_UNMAP to MMU_NOTIFY_CLEAR, except the ones that actually perform munmap() or mremap() as documented.
__oom_reap_task_mm() behaves much more like MADV_DONTNEED. So use MMU_NOTIFY_CLEAR as well.
This is a preparation for further changes.
--- mm/oom_kill.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/oom_kill.c +1 -1
@@ -551,7 +551,7 @@ static bool __oom_reap_task_mm(struct mm_struct *mm)struct mmu_notifier_range range;struct mmu_gather tlb; -mmu_notifier_range_init (&range,MMU_NOTIFY_UNMAP ,0 , +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, vma->vm_start, vma->vm_end);tlb_gather_mmu (&tlb, mm);
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 6/16 mm/oom_kill: factor out zapping of VMA into zap_vma_for_reaping()
In reply to David Hildenbrand (Arm)Let's factor it out so we can turn unmap_page_range() into a static function instead, and so oom reaping has a clean interface to call.
Note that hugetlb is not supported, because it would require a bunch of hugetlb-specific further actions (see zap_page_range_single_batched()).
--- mm/internal.h | 5 +---- mm/memory.c | 36 ++++++++++++++++++++++++++++++++---- mm/oom_kill.c | 15 +-------------- 3 files changed, 34 insertions(+), 22 deletions(-)
mm/internal.h +1 -4
@@ -536,13 +536,10 @@ static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) }struct zap_details ; -void unmap_page_range (struct mmu_gather *tlb, -struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details);void zap_page_range_single_batched (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long addr,unsigned long size,struct zap_details *details); +int zap_vma_for_reaping (struct vm_area_struct *vma);int folio_unmap_invalidate (struct address_space *mapping,struct folio *folio,gfp_t gfp);
mm/memory.c +32 -4
@@ -2054,10 +2054,9 @@ static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,return addr; } -void unmap_page_range (struct mmu_gather *tlb, -struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details) +static void unmap_page_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long addr,unsigned long end, +struct zap_details *details) {pgd_t *pgd;unsigned long next;
@@ -2115,6 +2114,35 @@ static void unmap_single_vma(struct mmu_gather *tlb, } } +/** +* zap_vma_for_reaping - zap all page table entries in the vma without blocking +* @vma: The vma to zap. +* +* Zap all page table entries in the vma without blocking for use by the oom +* killer. Hugetlb vmas are not supported. +* +* Returns: 0 on success, -EBUSY if we would have to block. +*/ +int zap_vma_for_reaping (struct vm_area_struct *vma) +{ +struct mmu_notifier_range range; +struct mmu_gather tlb; + +VM_WARN_ON_ONCE (is_vm_hugetlb_page (vma)); + +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, + vma->vm_start, vma->vm_end); +tlb_gather_mmu (&tlb, vma->vm_mm); +if (mmu_notifier_invalidate_range_start_nonblock (&range)) { +tlb_finish_mmu (&tlb); +return -EBUSY ; + } +unmap_page_range (&tlb, vma, range.start, range.end,NULL ); +mmu_notifier_invalidate_range_end (&range); +tlb_finish_mmu (&tlb); +return 0 ; +} +/** * unmap_vmas - unmap a range of memory covered by a list of vma's * @tlb: address of the caller's struct mmu_gather
mm/oom_kill.c +1 -14
@@ -548,21 +548,8 @@ static bool __oom_reap_task_mm(struct mm_struct *mm)* count elevated without a good reason. */ if (vma_is_anonymous (vma) || !(vma->vm_flags &VM_SHARED )) { -struct mmu_notifier_range range; -struct mmu_gather tlb; - -mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , - mm, vma->vm_start, - vma->vm_end); -tlb_gather_mmu (&tlb, mm); -if (mmu_notifier_invalidate_range_start_nonblock (&range)) { -tlb_finish_mmu (&tlb); +if (zap_vma_for_reaping (vma)) ret = false; -continue ; - } -unmap_page_range (&tlb, vma, range.start, range.end,NULL ); -mmu_notifier_invalidate_range_end (&range); -tlb_finish_mmu (&tlb); } }
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 7/16 mm/memory: rename unmap_single_vma() to __zap_vma_range()
In reply to David Hildenbrand (Arm)Let's rename it to better fit our new naming scheme.
--- mm/memory.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/memory.c +3 -3
@@ -2074,7 +2074,7 @@ static void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma, } -static void unmap_single_vma (struct mmu_gather *tlb, +static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start_addr,unsigned long end_addr,struct zap_details *details) {
@@ -2177,7 +2177,7 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap)unsigned long start = unmap->vma_start;unsigned long end = unmap->vma_end;hugetlb_zap_begin (vma, &start, &end); -unmap_single_vma (tlb, vma, start, end, &details); +__zap_vma_range (tlb, vma, start, end, &details);hugetlb_zap_end (vma, &details); vma =mas_find (unmap->mas, unmap->tree_end -1 ); }while (vma);
@@ -2213,7 +2213,7 @@ void zap_page_range_single_batched(struct mmu_gather *tlb,* unmap 'address-end' not 'range.start-range.end' as range * could have been expanded for hugetlb pmd sharing. */ -unmap_single_vma (tlb, vma, address, end, details); +__zap_vma_range (tlb, vma, address, end, details);mmu_notifier_invalidate_range_end (&range);if (is_vm_hugetlb_page (vma)) {/*
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 8/16 mm/memory: move adjusting of address range to unmap_vmas()
In reply to David Hildenbrand (Arm)__zap_vma_range() has two callers, whereby zap_page_range_single_batched() documents that the range must fit into the VMA range.
So move adjusting the range to unmap_vmas() where it is actually required and add a safety check in __zap_vma_range() instead. In unmap_vmas(), we'd never expect to have empty ranges (otherwise, why have the vma in there in the first place).
__zap_vma_range() will no longer be called with start == end, so cleanup the function a bit. While at it, simplify the overly long comment to its core message.
We will no longer call uprobe_munmap() for start == end, which actually seems to be the right thing to do.
Note that hugetlb_zap_begin()->...->adjust_range_if_pmd_sharing_possible() cannot result in the range exceeding the vma range.
--- mm/memory.c | 58 +++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 35 deletions(-)
mm/memory.c +23 -35
@@ -2073,44 +2073,28 @@ static void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,tlb_end_vma (tlb, vma); } - -static void __zap_vma_range (struct mmu_gather *tlb, -struct vm_area_struct *vma,unsigned long start_addr, -unsigned long end_addr,struct zap_details *details) +static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long start,unsigned long end, +struct zap_details *details) { -unsigned long start =max (vma->vm_start, start_addr); -unsigned long end; - -if (start >= vma->vm_end) -return ; - end =min (vma->vm_end, end_addr); -if (end <= vma->vm_start) -return ; +VM_WARN_ON_ONCE (start >= end || !range_in_vma (vma, start, end));if (vma->vm_file)uprobe_munmap (vma, start, end); -if (start != end) { -if (unlikely (is_vm_hugetlb_page (vma))) { -/* -* It is undesirable to test vma->vm_file as it -* should be non-null for valid hugetlb area. -* However, vm_file will be NULL in the error -* cleanup path of mmap_region. When -* hugetlbfs ->mmap method fails, -* mmap_region() nullifies vma->vm_file -* before calling this function to clean up. -* Since no pte has actually been setup, it is -* safe to do nothing in this case. -*/ -if (vma->vm_file) { -zap_flags_t zap_flags = details ? - details->zap_flags :0 ; -__unmap_hugepage_range (tlb, vma, start, end, -NULL , zap_flags); - } - }else -unmap_page_range (tlb, vma, start, end, details); +if (unlikely (is_vm_hugetlb_page (vma))) { +zap_flags_t zap_flags = details ? details->zap_flags :0 ; + +/* +* vm_file will be NULL when we fail early while instantiating +* a new mapping. In this case, no pages were mapped yet and +* there is nothing to do. +*/ +if (!vma->vm_file) +return ; +__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); + }else { +unmap_page_range (tlb, vma, start, end, details); } }
@@ -2174,8 +2158,9 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap) unmap->vma_start, unmap->vma_end);mmu_notifier_invalidate_range_start (&range); do { - unsigned long start = unmap->vma_start; - unsigned long end = unmap->vma_end; + unsigned long start = max(vma->vm_start, unmap->vma_start); + unsigned long end = min(vma->vm_end, unmap->vma_end); + hugetlb_zap_begin(vma, &start, &end); __zap_vma_range(tlb, vma, start, end, &details); hugetlb_zap_end(vma, &details);
@@ -2204,6 +2189,9 @@ void zap_page_range_single_batched(struct mmu_gather *tlb,VM_WARN_ON_ONCE (!tlb || tlb->mm != vma->vm_mm); +if (unlikely (!size)) +return ; +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, address, end);hugetlb_zap_begin (vma, &range.start, &range.end);
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 9/16 mm/memory: convert details->even_cows into details->skip_cows
In reply to David Hildenbrand (Arm)The current semantics are confusing: simply because someone specifies an empty zap_detail struct suddenly makes should_zap_cows() behave differently. The default should be to also zap CoW'ed anonymous pages.
Really only unmap_mapping_pages() and friends want to skip zapping of these anon folios.
So let's invert the meaning; turn the confusing "reclaim_pt" check that overrides other properties in should_zap_cows() into a safety check.
Note that the only caller that sets reclaim_pt=true is madvise_dontneed_single_vma(), which wants to zap any pages.
--- include/linux/mm.h | 2 +- mm/madvise.c | 1 - mm/memory.c | 12 ++++++------ 3 files changed, 7 insertions(+), 8 deletions(-)
include/linux/mm.h +1 -1
@@ -2798,7 +2798,7 @@ extern void pagefault_out_of_memory(void);*/ struct zap_details {struct folio *single_folio;/* Locked folio to be unmapped */ -bool even_cows;/* Zap COWed private pages too? */ +bool skip_cows;/* Do not zap COWed private pages */ bool reclaim_pt;/* Need reclaim page tables? */ zap_flags_t zap_flags;/* Extra flags for zapping */ };
mm/madvise.c +0 -1
@@ -853,7 +853,6 @@ static long madvise_dontneed_single_vma(struct madvise_behavior *madv_behavior)struct madvise_behavior_range *range = &madv_behavior->range;struct zap_details details = { .reclaim_pt = true, - .even_cows = true, }; zap_page_range_single_batched(
mm/memory.c +6 -6
@@ -1554,11 +1554,13 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)static inline bool should_zap_cows (struct zap_details *details) {/* By default, zap all pages */ -if (!details || details->reclaim_pt) +if (!details)return true; +VM_WARN_ON_ONCE (details->skip_cows && details->reclaim_pt); +/* Or, we zap COWed pages only if the caller wants to */ -return details->even_cows; +return !details->skip_cows; }/* Decides whether we should zap this folio with the folio pointer specified */
@@ -2149,8 +2151,6 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap)struct mmu_notifier_range range;struct zap_details details = { .zap_flags =ZAP_FLAG_DROP_MARKER |ZAP_FLAG_UNMAP , -/* Careful - we need to zap private pages too! */ - .even_cows = true, }; vma = unmap->first;
@@ -4282,7 +4282,7 @@ void unmap_mapping_folio(struct folio *folio) first_index = folio->index; last_index =folio_next_index (folio) -1 ; - details.even_cows = false; + details.skip_cows = true; details.single_folio = folio; details.zap_flags =ZAP_FLAG_DROP_MARKER ;
@@ -4312,7 +4312,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,pgoff_t first_index = start;pgoff_t last_index = start + nr -1 ; - details.even_cows = even_cows; + details.skip_cows = !even_cows;if (last_index < first_index) last_index =ULONG_MAX ;
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 10/16 mm/memory: use __zap_vma_range() in zap_vma_for_reaping()
In reply to David Hildenbrand (Arm)Let's call __zap_vma_range() instead of unmap_page_range() to prepare for further cleanups.
To keep the existing behavior, whereby we do not call uprobe_munmap() which could block, add a new "reaping" member to zap_details and use it.
Likely we should handle the possible blocking in uprobe_munmap() differently, but for now keep it unchanged.
--- include/linux/mm.h | 1 + mm/memory.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)
include/linux/mm.h +1 -0
@@ -2800,6 +2800,7 @@ struct zap_details {struct folio *single_folio;/* Locked folio to be unmapped */ bool skip_cows;/* Do not zap COWed private pages */ bool reclaim_pt;/* Need reclaim page tables? */ +bool reaping;/* Reaping, do not block. */ zap_flags_t zap_flags;/* Extra flags for zapping */ };
mm/memory.c +9 -4
@@ -2079,14 +2079,18 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long start, unsigned long end, struct zap_details *details) { + const bool reaping = details && details->reaping; +VM_WARN_ON_ONCE (start >= end || !range_in_vma (vma, start, end)); -if (vma->vm_file) +/* uprobe_munmap() might sleep, so skip it when reaping. */ +if (vma->vm_file && !reaping)uprobe_munmap (vma, start, end);if (unlikely (is_vm_hugetlb_page (vma))) {zap_flags_t zap_flags = details ? details->zap_flags :0 ; +VM_WARN_ON_ONCE (reaping);/* * vm_file will be NULL when we fail early while instantiating * a new mapping. In this case, no pages were mapped yet and
@@ -2111,11 +2115,12 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,*/ int zap_vma_for_reaping (struct vm_area_struct *vma) { +struct zap_details details = { + .reaping = true, + };struct mmu_notifier_range range;struct mmu_gather tlb; -VM_WARN_ON_ONCE (is_vm_hugetlb_page (vma)); -mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, vma->vm_start, vma->vm_end);tlb_gather_mmu (&tlb, vma->vm_mm);
@@ -2123,7 +2128,7 @@ int zap_vma_for_reaping(struct vm_area_struct *vma)tlb_finish_mmu (&tlb);return -EBUSY ; } -unmap_page_range (&tlb, vma, range.start, range.end,NULL ); +__zap_vma_range (&tlb, vma, range.start, range.end, &details);mmu_notifier_invalidate_range_end (&range);tlb_finish_mmu (&tlb);return 0 ;
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 11/16 mm/memory: inline unmap_page_range() into __zap_vma_range()
In reply to David Hildenbrand (Arm)Let's inline it into the single caller to reduce the number of confusing unmap/zap helpers.
Get rid of the unnecessary BUG_ON().
--- mm/memory.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)
mm/memory.c +12 -20
@@ -2056,25 +2056,6 @@ static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,return addr; } -static void unmap_page_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details) -{ -pgd_t *pgd; -unsigned long next; - -BUG_ON (addr >= end); -tlb_start_vma (tlb, vma); - pgd =pgd_offset (vma->vm_mm, addr); -do { - next =pgd_addr_end (addr, end); -if (pgd_none_or_clear_bad (pgd)) -continue ; - next =zap_p4d_range (tlb, vma, pgd, addr, next, details); - }while (pgd++, addr = next, addr != end); -tlb_end_vma (tlb, vma); -} -static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start,unsigned long end,struct zap_details *details)
@@ -2100,7 +2081,18 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,return ;__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); }else { -unmap_page_range (tlb, vma, start, end, details); +unsigned long next, cur = start; +pgd_t *pgd; + +tlb_start_vma (tlb, vma); + pgd =pgd_offset (vma->vm_mm, cur); +do { + next =pgd_addr_end (cur, end); +if (pgd_none_or_clear_bad (pgd)) +continue ; + next =zap_p4d_range (tlb, vma, pgd, cur, next, details); + }while (pgd++, cur = next, cur != end); +tlb_end_vma (tlb, vma); } }
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 12/16 mm: rename zap_vma_pages() to zap_vma()
In reply to David Hildenbrand (Arm)Let's rename it to an even simpler name. While at it, add some simplistic kernel doc.
--- arch/powerpc/platforms/book3s/vas-api.c | 2 +- arch/powerpc/platforms/pseries/vas.c | 2 +- include/linux/mm.h | 6 +++++- lib/vdso/datastore.c | 2 +- mm/page-writeback.c | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-)
arch/powerpc/platforms/book3s/vas-api.c +1 -1
@@ -414,7 +414,7 @@ static vm_fault_t vas_mmap_fault(struct vm_fault *vmf)/* * When the LPAR lost credits due to core removal or during * migration, invalidate the existing mapping for the current -* paste addresses and set windows in-active (zap_vma_pages in +* paste addresses and set windows in-active (zap_vma() in * reconfig_close_windows()). * New mapping will be done later after migration or new credits * available. So continue to receive faults if the user space
arch/powerpc/platforms/pseries/vas.c +1 -1
@@ -807,7 +807,7 @@ static int reconfig_close_windows(struct vas_caps *vcap, int excess_creds,* is done before the original mmap() and after the ioctl. */ if (vma) -zap_vma_pages (vma); +zap_vma (vma);mutex_unlock (&task_ref->mmap_mutex);mmap_write_unlock (task_ref->mm);
include/linux/mm.h +5 -1
@@ -2837,7 +2837,11 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, unsigned long size);void zap_page_range_single (struct vm_area_struct *vma,unsigned long address,unsigned long size); -static inline void zap_vma_pages (struct vm_area_struct *vma) +/** +* zap_vma - zap all page table entries in a vma +* @vma: The vma to zap. +*/ +static inline void zap_vma (struct vm_area_struct *vma) {zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); }
lib/vdso/datastore.c +1 -1
@@ -121,7 +121,7 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)mmap_read_lock (mm);for_each_vma (vmi, vma) {if (vma_is_special_mapping (vma, &vdso_vvar_mapping)) -zap_vma_pages (vma); +zap_vma (vma); }mmap_read_unlock (mm);
mm/page-writeback.c +1 -1
@@ -2645,7 +2645,7 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)* while this function is in progress, although it may have been truncated * before this function is called. Most callers have the folio locked. * A few have the folio blocked from truncation through other means (e.g. -* zap_vma_pages() has it mapped and is holding the page table lock). +* zap_vma() has it mapped and is holding the page table lock). * When called from mark_buffer_dirty(), the filesystem should hold a * reference to the buffer_head that is being marked dirty, which causes * try_to_free_buffers() to fail.
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 13/16 mm: rename zap_page_range_single_batched() to zap_vma_range_batched()
In reply to David Hildenbrand (Arm)Let's make the naming more consistent with our new naming scheme.
While at it, polish the kerneldoc a bit.
--- mm/internal.h | 2 +- mm/madvise.c | 5 ++--- mm/memory.c | 23 +++++++++++++---------- 3 files changed, 16 insertions(+), 14 deletions(-)
mm/internal.h +1 -1
@@ -536,7 +536,7 @@ static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) }struct zap_details ; -void zap_page_range_single_batched (struct mmu_gather *tlb, +void zap_vma_range_batched (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long addr,unsigned long size,struct zap_details *details);int zap_vma_for_reaping (struct vm_area_struct *vma);
mm/madvise.c +2 -3
@@ -855,9 +855,8 @@ static long madvise_dontneed_single_vma(struct madvise_behavior *madv_behavior) .reclaim_pt = true, }; -zap_page_range_single_batched ( - madv_behavior->tlb, madv_behavior->vma, range->start, - range->end - range->start, &details); +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details);return 0 ; }
mm/memory.c +13 -10
@@ -2167,17 +2167,20 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap) }/** -* zap_page_range_single_batched - remove user pages in a given range +* zap_vma_range_batched - zap page table entries in a vma range * @tlb: pointer to the caller's struct mmu_gather -* @vma: vm_area_struct holding the applicable pages -* @address: starting address of pages to remove -* @size: number of bytes to remove -* @details: details of shared cache invalidation +* @vma: the vma covering the range to zap +* @address: starting address of the range to zap +* @size: number of bytes to zap +* @details: details specifying zapping behavior +* +* @tlb must not be NULL. The provided address range must be fully +* contained within @vma. If @vma is for hugetlb, @tlb is flushed and +* re-initialized by this function. * -* @tlb shouldn't be NULL. The range must fit into one VMA. If @vma is for -* hugetlb, @tlb is flushed and re-initialized by this function. +* If @details is NULL, this function will zap all page table entries. */ -void zap_page_range_single_batched (struct mmu_gather *tlb, +void zap_vma_range_batched (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long address,unsigned long size,struct zap_details *details) {
@@ -2225,7 +2228,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,struct mmu_gather tlb;tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, address, size,NULL ); +zap_vma_range_batched (&tlb, vma, address, size,NULL );tlb_finish_mmu (&tlb); }
@@ -4251,7 +4254,7 @@ static inline void unmap_mapping_range_tree(struct rb_root_cached *root, size = (end_idx - start_idx) <<PAGE_SHIFT ;tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, start, size, details); +zap_vma_range_batched (&tlb, vma, start, size, details);tlb_finish_mmu (&tlb); } }
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 14/16 mm: rename zap_page_range_single() to zap_vma_range()
In reply to David Hildenbrand (Arm)Let's rename it to make it better match our new naming scheme.
While at it, polish the kerneldoc.
--- arch/s390/mm/gmap_helpers.c | 2 +- drivers/android/binder/page_range.rs | 4 ++-- drivers/android/binder_alloc.c | 2 +- include/linux/mm.h | 4 ++-- kernel/bpf/arena.c | 2 +- kernel/events/core.c | 2 +- mm/madvise.c | 4 ++-- mm/memory.c | 14 +++++++------- net/ipv4/tcp.c | 6 +++--- rust/kernel/mm/virt.rs | 4 ++-- 10 files changed, 22 insertions(+), 22 deletions(-)
arch/s390/mm/gmap_helpers.c +1 -1
@@ -89,7 +89,7 @@ void gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned loif (!vma)return ;if (!is_vm_hugetlb_page (vma)) -zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr); +zap_vma_range (vma, vmaddr,min (end, vma->vm_end) - vmaddr); vmaddr = vma->vm_end; } }
drivers/android/binder/page_range.rs +2 -2
@@ -130,7 +130,7 @@ pub(crate) struct ShrinkablePageRange { pid: Pid,/// The mm for the relevant process. mm: ARef<Mm>, -/// Used to synchronize calls to `vm_insert_page` and `zap_page_range_single`. +/// Used to synchronize calls to `vm_insert_page` and `zap_vma_range`. #[pin] mm_lock: Mutex<()>,/// Spinlock protecting changes to pages.
@@ -719,7 +719,7 @@ fn drop(self: Pin<&mut Self>) {if let Some (vma) = mmap_read.vma_lookup (vma_addr) {let user_page_addr = vma_addr + (page_index <<PAGE_SHIFT ); - vma.zap_page_range_single (user_page_addr,PAGE_SIZE ); + vma.zap_vma_range (user_page_addr,PAGE_SIZE ); }drop (mmap_read);
drivers/android/binder_alloc.c +1 -1
@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ); +zap_vma_range (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index); }
include/linux/mm.h +2 -2
@@ -2835,7 +2835,7 @@ struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr,void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address,unsigned long size); -void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, +void zap_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size);/** * zap_vma - zap all page table entries in a vma
@@ -2843,7 +2843,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,*/ static inline void zap_vma (struct vm_area_struct *vma) { -zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); +zap_vma_range (vma, vma->vm_start, vma->vm_end - vma->vm_start); }struct mmu_notifier_range ;
kernel/bpf/arena.c +1 -1
@@ -656,7 +656,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt)guard (mutex)(&arena->lock);/* iterate link list under lock */ list_for_each_entry (vml, &arena->vma_list, head) -zap_page_range_single (vml->vma, uaddr,PAGE_SIZE * page_cnt); +zap_vma_range (vml->vma, uaddr,PAGE_SIZE * page_cnt); }static void arena_free_pages (struct bpf_arena *arena,long uaddr,long page_cnt,bool sleepable)
kernel/events/core.c +1 -1
@@ -7215,7 +7215,7 @@ static int map_range(struct perf_buffer *rb, struct vm_area_struct *vma)#ifdef CONFIG_MMU /* Clear any partial mappings on error. */ if (err) -zap_page_range_single (vma, vma->vm_start, nr_pages *PAGE_SIZE ); +zap_vma_range (vma, vma->vm_start, nr_pages *PAGE_SIZE );#endif return err;
mm/madvise.c +2 -2
@@ -832,7 +832,7 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)* Application no longer needs these pages. If the pages are dirty, * it's OK to just throw them away. The app will be more careful about * data it wants to keep. Be sure to free swap resources too. The -* zap_page_range_single call sets things up for shrink_active_list to actually +* zap_vma_range call sets things up for shrink_active_list to actually * free these pages later if no one else has touched them in the meantime, * although we could add these pages to a global reuse list for * shrink_active_list to pick up before reclaiming other pages.
@@ -1191,7 +1191,7 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)* OK some of the range have non-guard pages mapped, zap * them. This leaves existing guard pages in place. */ -zap_page_range_single (vma, range->start, range->end - range->start); +zap_vma_range (vma, range->start, range->end - range->start); }/*
mm/memory.c +7 -7
@@ -2215,14 +2215,14 @@ void zap_vma_range_batched(struct mmu_gather *tlb, }/** -* zap_page_range_single - remove user pages in a given range -* @vma: vm_area_struct holding the applicable pages -* @address: starting address of pages to zap +* zap_vma_range - zap all page table entries in a vma range +* @vma: the vma covering the range to zap +* @address: starting address of the range to zap * @size: number of bytes to zap * -* The range must fit into one VMA. +* The provided address range must be fully contained within @vma. */ -void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, +void zap_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size) {struct mmu_gather tlb;
@@ -2250,7 +2250,7 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, !(vma->vm_flags & VM_PFNMAP))return ; -zap_page_range_single (vma, address, size); +zap_vma_range (vma, address, size); }EXPORT_SYMBOL_GPL (zap_vma_ptes);
@@ -3018,7 +3018,7 @@ static int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long add* maintain page reference counts, and callers may free * pages due to the error. So zap it early. */ -zap_page_range_single (vma, addr, size); +zap_vma_range (vma, addr, size);return error; }
net/ipv4/tcp.c +3 -3
@@ -2104,7 +2104,7 @@ static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma, maybe_zap_len = total_bytes_to_map -/* All bytes to map */ *length +/* Mapped or pending */ (pages_remaining *PAGE_SIZE );/* Failed map. */ -zap_page_range_single (vma, *address, maybe_zap_len); +zap_vma_range (vma, *address, maybe_zap_len); err =0 ; }
@@ -2112,7 +2112,7 @@ static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma,unsigned long leftover_pages = pages_remaining;int bytes_mapped; -/* We called zap_page_range_single, try to reinsert. */ +/* We called zap_vma_range, try to reinsert. */ err =vm_insert_pages (vma, *address, pending_pages, &pages_remaining);
@@ -2269,7 +2269,7 @@ static int tcp_zerocopy_receive(struct sock *sk, total_bytes_to_map = avail_len & ~(PAGE_SIZE -1 );if (total_bytes_to_map) {if (!(zc->flags &TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT )) -zap_page_range_single (vma, address, total_bytes_to_map); +zap_vma_range (vma, address, total_bytes_to_map); zc->length = total_bytes_to_map; zc->recv_skip_hint =0 ; }else {
rust/kernel/mm/virt.rs +2 -2
@@ -113,7 +113,7 @@ pub fn end(&self) -> usize {/// kernel goes further in freeing unused page tables, but for the purposes of this operation /// we must only assume that the leaf level is cleared. #[inline] -pub fn zap_page_range_single (&self, address:usize , size:usize ) { +pub fn zap_vma_range (&self, address:usize , size:usize ) {let (end, did_overflow) = address.overflowing_add (size);if did_overflow || address < self.start () || self.end () < end {// TODO: call WARN_ONCE once Rust version of it is added
@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size) + bindings::zap_vma_range (self.as_ptr (), address, size) }; }
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 15/16 mm: rename zap_vma_ptes() to zap_special_vma_range()
In reply to David Hildenbrand (Arm)zap_vma_ptes() is the only zapping function we export to modules.
It's essentially a wrapper around zap_vma_range(), however, with some safety checks:
* That the passed range fits fully into the VMA
* That it's only used for VM_PFNMAP
We might want to support VM_MIXEDMAP soon as well, so use the more-generic term "special vma", although "special" is a bit overloaded. Maybe we'll later just support any VM_SPECIAL flag.
While at it, improve the kerneldoc.
--- arch/x86/kernel/cpu/sgx/encl.c | 2 +- drivers/comedi/comedi_fops.c | 2 +- drivers/gpu/drm/i915/i915_mm.c | 4 ++-- drivers/infiniband/core/uverbs_main.c | 6 +++--- drivers/misc/sgi-gru/grumain.c | 2 +- include/linux/mm.h | 2 +- mm/memory.c | 16 +++++++--------- 7 files changed, 16 insertions(+), 18 deletions(-)
arch/x86/kernel/cpu/sgx/encl.c +1 -1
@@ -1220,7 +1220,7 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr) ret =sgx_encl_find (encl_mm->mm, addr, &vma);if (!ret && encl == vma->vm_private_data) -zap_vma_ptes (vma, addr,PAGE_SIZE ); +zap_special_vma_range (vma, addr,PAGE_SIZE );mmap_read_unlock (encl_mm->mm);
drivers/comedi/comedi_fops.c +1 -1
@@ -2588,7 +2588,7 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)* remap_pfn_range() because we call remap_pfn_range() in a loop. */ if (retval) -zap_vma_ptes (vma, vma->vm_start, size); +zap_special_vma_range (vma, vma->vm_start, size);#endif if (retval ==0 ) {
drivers/gpu/drm/i915/i915_mm.c +2 -2
@@ -108,7 +108,7 @@ int remap_io_mapping(struct vm_area_struct *vma, err =apply_to_page_range (r.mm, addr, size, remap_pfn, &r);if (unlikely (err)) { -zap_vma_ptes (vma, addr, (r.pfn - pfn) <<PAGE_SHIFT ); +zap_special_vma_range (vma, addr, (r.pfn - pfn) <<PAGE_SHIFT );return err; }
@@ -156,7 +156,7 @@ int remap_io_sg(struct vm_area_struct *vma, err =apply_to_page_range (r.mm, addr, size, remap_sg, &r);if (unlikely (err)) { -zap_vma_ptes (vma, addr, r.pfn <<PAGE_SHIFT ); +zap_special_vma_range (vma, addr, r.pfn <<PAGE_SHIFT );return err; }
drivers/infiniband/core/uverbs_main.c +3 -3
@@ -756,7 +756,7 @@ static void rdma_umap_open(struct vm_area_struct *vma)* point, so zap it. */ vma->vm_private_data =NULL ; -zap_vma_ptes (vma, vma->vm_start, vma->vm_end - vma->vm_start); +zap_special_vma_range (vma, vma->vm_start, vma->vm_end - vma->vm_start); }static void rdma_umap_close (struct vm_area_struct *vma)
@@ -782,7 +782,7 @@ static void rdma_umap_close(struct vm_area_struct *vma) }/* -* Once the zap_vma_ptes has been called touches to the VMA will come here and +* Once the zap_special_vma_range has been called touches to the VMA will come here and * we return a dummy writable zero page for all the pfns. */ static vm_fault_t rdma_umap_fault (struct vm_fault *vmf)
@@ -878,7 +878,7 @@ void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)continue ;list_del_init (&priv->list); -zap_vma_ptes (vma, vma->vm_start, +zap_special_vma_range (vma, vma->vm_start, vma->vm_end - vma->vm_start);if (priv->entry) {
drivers/misc/sgi-gru/grumain.c +1 -1
@@ -542,7 +542,7 @@ void gru_unload_context(struct gru_thread_state *gts, int savestate)int ctxnum = gts->ts_ctxnum;if (!is_kernel_context (gts)) -zap_vma_ptes (gts->ts_vma,UGRUADDR (gts),GRU_GSEG_PAGESIZE ); +zap_special_vma_range (gts->ts_vma,UGRUADDR (gts),GRU_GSEG_PAGESIZE ); cch =get_cch (gru->gs_gru_base_vaddr, ctxnum); gru_dbg(grudev, "gts %p, cbrmap 0x%lx, dsrmap 0x%lx\n",
include/linux/mm.h +1 -1
@@ -2833,7 +2833,7 @@ struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,struct page *vm_normal_page_pud (struct vm_area_struct *vma,unsigned long addr,pud_t pud); -void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address, +void zap_special_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size);void zap_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size);
mm/memory.c +7 -9
@@ -2233,17 +2233,15 @@ void zap_vma_range(struct vm_area_struct *vma, unsigned long address, }/** -* zap_vma_ptes - remove ptes mapping the vma -* @vma: vm_area_struct holding ptes to be zapped -* @address: starting address of pages to zap +* zap_special_vma_range - zap all page table entries in a special vma range +* @vma: the vma covering the range to zap +* @address: starting address of the range to zap * @size: number of bytes to zap * -* This function only unmaps ptes assigned to VM_PFNMAP vmas. -* -* The entire address range must be fully contained within the vma. -* +* This function does nothing when the provided address range is not fully +* contained in @vma, or when the @vma is not VM_PFNMAP. */ -void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address, +void zap_special_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size) { if (!range_in_vma(vma, address, address + size) ||
@@ -2252,7 +2250,7 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,zap_vma_range (vma, address, size); } -EXPORT_SYMBOL_GPL (zap_vma_ptes); +EXPORT_SYMBOL_GPL (zap_special_vma_range);static pmd_t *walk_to_pmd (struct mm_struct *mm,unsigned long addr) {
Signature · 1 line
2.43.0
David Hildenbrand (Arm)
maintainerPATCH 16/16 mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
In reply to David Hildenbrand (Arm)There is demand for also zapping page table entries by drivers in VM_MIXEDMAP VMAs[1].
Nothing really speaks against supporting VM_MIXEDMAP for driver use. We just don't want arbitrary drivers to zap in ordinary (non-special) VMAs.
[1] https://lore.kernel.org/r/[email protected]
--- mm/memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/memory.c +2 -2
@@ -2239,13 +2239,13 @@ void zap_vma_range(struct vm_area_struct *vma, unsigned long address,* @size: number of bytes to zap * * This function does nothing when the provided address range is not fully -* contained in @vma, or when the @vma is not VM_PFNMAP. +* contained in @vma, or when the @vma is not VM_PFNMAP or VM_MIXEDMAP. */ void zap_special_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size) {if (!range_in_vma (vma, address, address + size) || - !(vma->vm_flags &VM_PFNMAP )) + !(vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP )))return ;zap_vma_range (vma, address, size);
Signature · 1 line
2.43.0
Re: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
In reply to David Hildenbrand (Arm)Quoted 11 lines · David Hildenbrand (Arm)
Nobody except
memory.cshould really set that parameter to non-NULL. So let's just drop it and makeunmap_mapping_range_vma()usezap_page_range_single_batched()instead.Signed-off-by David Hildenbrand (Arm) <[email protected]>rust/kernel/mm/virt.rs +0
-0
rust/kernel/mm/virt.rs pub fn zap_page_range_single(&self, address: usize, size: usize) { · line 128
Quoted 6 lines
rust/kernel/mm/virt.rs +1
-1@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size, core::ptr::null_mut ()) + bindings::zap_page_range_single (self.as_ptr (), address, size) };
Please run rustfmt on Rust changes. Here, rustfmt leads to this being formatted on a single line:
unsafe { bindings::zap_page_range_single(self.as_ptr(), address, size) };
with the above changed:
Alice
Re: [PATCH v1 14/16] mm: rename zap_page_range_single() to zap_vma_range()
In reply to David Hildenbrand (Arm)Quoted 14 lines · David Hildenbrand (Arm)
drivers/android/binder/page_range.rs +1
-1@@ -130,7 +130,7 @@ pub(crate) struct ShrinkablePageRange { pid: Pid,/// The mm for the relevant process. mm: ARef<Mm>, -/// Used to synchronize calls to `vm_insert_page` and `zap_page_range_single`. +/// Used to synchronize calls to `vm_insert_page` and `zap_vma_range`. #[pin] mm_lock: Mutex<()>,/// Spinlock protecting changes to pages.
drivers/android/binder/page_range.rs fn drop(self: Pin<&mut Self>) { · line 723
Quoted 6 lines
drivers/android/binder/page_range.rs +1
-1@@ -719,7 +719,7 @@ fn drop(self: Pin<&mut Self>) {if let Some (vma) = mmap_read.vma_lookup (vma_addr) {let user_page_addr = vma_addr + (page_index <<PAGE_SHIFT ); - vma.zap_page_range_single (user_page_addr,PAGE_SIZE ); + vma.zap_vma_range (user_page_addr,PAGE_SIZE ); }
LGTM. Be aware that this will have a merge conflict with patches currently in char-misc-linus that are scheduled to land in an -rc.
drivers/android/binder_alloc.c enum lru_status binder_alloc_free_page(struct list_head *item, · line 1190
Quoted 12 lines
drivers/android/binder_alloc.c +1
-1@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ); +zap_vma_range (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index);
LGTM.
Quoted 14 lines
rust/kernel/mm/virt.rs +1
-1@@ -113,7 +113,7 @@ pub fn end(&self) -> usize {/// kernel goes further in freeing unused page tables, but for the purposes of this operation /// we must only assume that the leaf level is cleared. #[inline] -pub fn zap_page_range_single (&self, address:usize , size:usize ) { +pub fn zap_vma_range (&self, address:usize , size:usize ) {let (end, did_overflow) = address.overflowing_add (size);if did_overflow || address < self.start () || self.end () < end {// TODO: call WARN_ONCE once Rust version of it is added
rust/kernel/mm/virt.rs pub fn zap_page_range_single(&self, address: usize, size: usize) { · line 129
Quoted 7 lines
rust/kernel/mm/virt.rs +1
-1@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size) + bindings::zap_vma_range (self.as_ptr (), address, size) }; }
Same as previous patch: please run rustfmt. It will format on a single line, like this:
unsafe { bindings::zap_vma_range(self.as_ptr(), address, size) };with the above change applied:
Alice
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
In reply to Alice RyhlQuoted 7 lines · Alice Ryhl
Quoted 5 lines · David Hildenbrand (Arm)
Nobody except
memory.cshould really set that parameter to non-NULL. So let's just drop it and makeunmap_mapping_range_vma()usezap_page_range_single_batched()instead.Signed-off-by David Hildenbrand (Arm) <[email protected]>
Quoted 14 lines
rust/kernel/mm/virt.rs pub fn zap_page_range_single(&self, address: usize, size: usize) { · line 128
Quoted 11 lines
rust/kernel/mm/virt.rs +1
-1@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size, core::ptr::null_mut ()) + bindings::zap_page_range_single (self.as_ptr (), address, size) };Please run rustfmt on Rust changes. Here, rustfmt leads to this being formatted on a single line:
Having to run tooling I don't even have installed when removing a single function parameter; did not expect that :)
Quoted 3 lines
unsafe {
bindings::zap_page_range_single(self.as_ptr(), address, size)};
@Andrew, can you squash:
From 5128f2e34d219722a8883c1fa94e057bd34601f2 Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Mon, 2 Mar 2026 09:17:19 +0100
Subject: [PATCH] fixup: mm/memory: remove zap_details parameter from
zap_page_range_single()
--- rust/kernel/mm/virt.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
rust/kernel/mm/virt.rs +1 -3
@@ -123,9 +123,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// SAFETY: By the type invariants, the caller has read access to this VMA, which is // sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. -unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size) - }; +unsafe { bindings::zap_page_range_single (self.as_ptr (), address, size) }; }/// If the [`VM_MIXEDMAP`] flag is set, returns a [`VmaMixedMap`] to this VMA, otherwise
Signature · 1 line
2.43.0
Quoted 3 lines
with the above changed:
Thanks!
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 14/16] mm: rename zap_page_range_single() to zap_vma_range()
In reply to Alice RyhlQuoted 16 lines · Alice Ryhl
Quoted 15 lines · David Hildenbrand (Arm)
drivers/android/binder/page_range.rs +1
-1@@ -130,7 +130,7 @@ pub(crate) struct ShrinkablePageRange { pid: Pid,/// The mm for the relevant process. mm: ARef<Mm>, -/// Used to synchronize calls to `vm_insert_page` and `zap_page_range_single`. +/// Used to synchronize calls to `vm_insert_page` and `zap_vma_range`. #[pin] mm_lock: Mutex<()>,/// Spinlock protecting changes to pages. @@ -719,7 +719,7 @@ fn drop(self: Pin<&mut Self>) {
Quoted 8 lines
drivers/android/binder/page_range.rs fn drop(self: Pin<&mut Self>) { · line 723
Quoted 5 lines
drivers/android/binder/page_range.rs +1
-1if let Some (vma) = mmap_read.vma_lookup (vma_addr) {let user_page_addr = vma_addr + (page_index <<PAGE_SHIFT ); - vma.zap_page_range_single (user_page_addr,PAGE_SIZE ); + vma.zap_vma_range (user_page_addr,PAGE_SIZE ); }LGTM. Be aware that this will have a merge conflict with patches currently in char-misc-linus that are scheduled to land in an -rc.
Thanks. @Andrew will likely run into that when rebasing, where we can fix it up.
Quoted 41 lines
drivers/android/binder_alloc.c enum lru_status binder_alloc_free_page(struct list_head *item, · line 1190
Quoted 13 lines
drivers/android/binder_alloc.c +1
-1@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ); +zap_vma_range (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index);LGTM.
Quoted 14 lines
rust/kernel/mm/virt.rs +1
-1@@ -113,7 +113,7 @@ pub fn end(&self) -> usize {/// kernel goes further in freeing unused page tables, but for the purposes of this operation /// we must only assume that the leaf level is cleared. #[inline] -pub fn zap_page_range_single (&self, address:usize , size:usize ) { +pub fn zap_vma_range (&self, address:usize , size:usize ) {let (end, did_overflow) = address.overflowing_add (size);if did_overflow || address < self.start () || self.end () < end {// TODO: call WARN_ONCE once Rust version of it is added rust/kernel/mm/virt.rs pub fn zap_page_range_single(&self, address: usize, size: usize) { · line 129
Quoted 7 lines
rust/kernel/mm/virt.rs +1
-1@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size) + bindings::zap_vma_range (self.as_ptr (), address, size) }; }Same as previous patch: please run rustfmt. It will format on a single line, like this:
Quoted 2 lines
unsafe { bindings::zap_vma_range(self.as_ptr(), address, size) };@Andrew, after squashing the fixup into patch #2, this hunk should look like this:
rust/kernel/mm/virt.rs +2 -2
@@ -113,7 +113,7 @@ pub fn end(&self) -> usize {/// kernel goes further in freeing unused page tables, but for the purposes of this operation /// we must only assume that the leaf level is cleared. #[inline] -pub fn zap_page_range_single (&self, address:usize , size:usize ) { +pub fn zap_vma_range (&self, address:usize , size:usize ) {let (end, did_overflow) = address.overflowing_add (size);if did_overflow || address < self.start () || self.end () < end {// TODO: call WARN_ONCE once Rust version of it is added
@@ -123,7 +123,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// SAFETY: By the type invariants, the caller has read access to this VMA, which is // sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. -unsafe { bindings::zap_page_range_single (self.as_ptr (), address, size) }; +unsafe { bindings::zap_vma_range (self.as_ptr (), address, size) }; }/// If the [`VM_MIXEDMAP`] flag is set, returns a [`VmaMixedMap`] to this VMA, otherwise
Quoted 3 lines
with the above change applied:
Thanks!
Signature · 3 lines
Cheers, David
Alice Ryhl
maintainerRe: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
In reply to David Hildenbrand (Arm)Quoted 23 lines · David Hildenbrand (Arm)
Quoted 7 lines · Alice Ryhl
Quoted 5 lines · David Hildenbrand (Arm)
Nobody except
memory.cshould really set that parameter to non-NULL. So let's just drop it and makeunmap_mapping_range_vma()usezap_page_range_single_batched()instead.Signed-off-by David Hildenbrand (Arm) <[email protected]>Quoted 14 lines
rust/kernel/mm/virt.rs pub fn zap_page_range_single(&self, address: usize, size: usize) { · line 128
Quoted 11 lines
rust/kernel/mm/virt.rs +1
-1@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size, core::ptr::null_mut ()) + bindings::zap_page_range_single (self.as_ptr (), address, size) };Please run rustfmt on Rust changes. Here, rustfmt leads to this being formatted on a single line:
Quoted 2 lines
Having to run tooling I don't even have installed when removing a single function parameter; did not expect that :)
Well, rustfmt comes with the compiler, and it would be ideal to build test changes before sending them :)
But no worries, I took care of testing it. Thanks for taking the time to update the Rust code as well.
Documentation/rust/quick-start.rst has details for most distros.
Alice
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
Quoted 4 lines · Alice Ryhl
Quoted 3 lines · David Hildenbrand (Arm)
Quoted 2 lines · Alice Ryhl
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Please run rustfmt on Rust changes. Here, rustfmt leads to this being formatted on a single line:
Having to run tooling I don't even have installed when removing a single function parameter; did not expect that :)
Well, rustfmt comes with the compiler, and it would be ideal to build test changes before sending them :)
At least on Ubuntu on my notebook where I do most of the coding+patch submissions it's a separate package?
I do all my builds on a different (more powerful) machine where the whole rust machinery's in place. Further, build bots that run on my private branches did not report any issues.
Quoted 2 lines
But no worries, I took care of testing it. Thanks for taking the time to update the Rust code as well.
I just did an allyesconfig and it does not report any warnings.
So apparently, rustfmt problems not result in the compiler complaining?
Or something else is off here that rust/kernel/mm/virt.rs won't get compiled on my machine, even with allyesconfig. I can definitely see some RUSTC stuff happening in the logs, like
RUSTC L rust/kernel.o
Thanks for the review and for pointing out rustfmt!
Quoted 2 lines
Documentation/rust/quick-start.rsthas details for most distros.
Will read that the next time I feel brave enough to touch rust code.
Signature · 3 lines
Cheers, David
Alice Ryhl
maintainerRe: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
Quoted 14 lines · David Hildenbrand (Arm)
Quoted 4 lines · Alice Ryhl
Quoted 3 lines · David Hildenbrand (Arm)
Quoted 2 lines · Alice Ryhl
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Please run rustfmt on Rust changes. Here, rustfmt leads to this being formatted on a single line:
Having to run tooling I don't even have installed when removing a single function parameter; did not expect that :)
Well, rustfmt comes with the compiler, and it would be ideal to build test changes before sending them :)
Quoted 6 lines
At least on Ubuntu on my notebook where I do most of the coding+patch submissions it's a separate package?
I do all my builds on a different (more powerful) machine where the whole rust machinery's in place. Further, build bots that run on my private branches did not report any issues.
There are some build bots that check for rustfmt, though not all of them.
Quoted 7 lines
Quoted 2 lines
But no worries, I took care of testing it. Thanks for taking the time to update the Rust code as well.
I just did an allyesconfig and it does not report any warnings.
So apparently, rustfmt problems not result in the compiler complaining?
Quoted 7 lines
Or something else is off here that
rust/kernel/mm/virt.rswon't get compiled on my machine, even with allyesconfig. I can definitely see some RUSTC stuff happening in the logs, likeRUSTC L
rust/kernel.oThanks for the review and for pointing out rustfmt!
Similar to kerneldoc and other similar targets, formatting isn't checked in the normal build, but make can be invoked on the rustfmtcheck target to check it.
Alice
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
Quoted 27 lines · Alice Ryhl
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 3 lines · Alice Ryhl
Well, rustfmt comes with the compiler, and it would be ideal to build test changes before sending them :)
At least on Ubuntu on my notebook where I do most of the coding+patch submissions it's a separate package?
I do all my builds on a different (more powerful) machine where the whole rust machinery's in place. Further, build bots that run on my private branches did not report any issues.
There are some build bots that check for rustfmt, though not all of them.
Quoted 11 lines
Quoted 2 lines
But no worries, I took care of testing it. Thanks for taking the time to update the Rust code as well.
I just did an allyesconfig and it does not report any warnings.
So apparently, rustfmt problems not result in the compiler complaining?
Or something else is off here that
rust/kernel/mm/virt.rswon't get compiled on my machine, even with allyesconfig. I can definitely see some RUSTC stuff happening in the logs, like
Quoted 7 lines
Quoted 3 lines
RUSTC L
rust/kernel.oThanks for the review and for pointing out rustfmt!
Similar to kerneldoc and other similar targets, formatting isn't checked in the normal build, but make can be invoked on the rustfmtcheck target to check it.
Thanks adding that to my cross-compile chain.
Signature · 3 lines
Cheers, David
Alice Ryhl
maintainerRe: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
Quoted 30 lines · David Hildenbrand (Arm)
Quoted 29 lines · Alice Ryhl
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 3 lines · Alice Ryhl
Well, rustfmt comes with the compiler, and it would be ideal to build test changes before sending them :)
At least on Ubuntu on my notebook where I do most of the coding+patch submissions it's a separate package?
I do all my builds on a different (more powerful) machine where the whole rust machinery's in place. Further, build bots that run on my private branches did not report any issues.
There are some build bots that check for rustfmt, though not all of them.
Quoted 13 lines
Quoted 2 lines
But no worries, I took care of testing it. Thanks for taking the time to update the Rust code as well.
I just did an allyesconfig and it does not report any warnings.
So apparently, rustfmt problems not result in the compiler complaining?
Or something else is off here that
rust/kernel/mm/virt.rswon't get compiled on my machine, even with allyesconfig. I can definitely see some RUSTC stuff happening in the logs, likeRUSTC L
rust/kernel.o
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Thanks for the review and for pointing out rustfmt!
Similar to kerneldoc and other similar targets, formatting isn't checked in the normal build, but make can be invoked on the rustfmtcheck target to check it.
Thanks adding that to my cross-compile chain.
Awesome, thanks!
It's not relevant in this patch, but another thing that may be useful is to add CLIPPY=1 to the make invocation when building normally. This causes additional warnings to be checked using a tool called clippy.
Alice
Andrew Morton
maintainerQuoted 2 lines · On Fri, 27 Feb 2026 21:08:31 +0100 "David Hildenbrand (Arm)"…
A bunch of cleanups around unmapping and zapping. Mostly simplifications, code movements, documentation and renaming of zapping functions.
Thanks, I added this (and the below) to mm.git
I suppressed the dded-to-mm emails to protect the innocent.
rust/kernel/mm/virt.rs renamed from rust/kernel/mm/virt.rs~mm-memory-remove-zap_details-parameter-from-zap_page_range_single-fix +1 -3
@@ -123,9 +123,7 @@ impl VmaRef {// SAFETY: By the type invariants, the caller has read access to this VMA, which is // sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. -unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size) - }; +unsafe { bindings::zap_page_range_single (self.as_ptr (), address, size) }; }/// If the [`VM_MIXEDMAP`] flag is set, returns a [`VmaMixedMap`] to this VMA, otherwise
_
Re: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
In reply to Alice RyhlQuoted 4 lines · Alice Ryhl
It's not relevant in this patch, but another thing that may be useful is to add CLIPPY=1 to the make invocation when building normally. This causes additional warnings to be checked using a tool called clippy.
Yes, please do use CLIPPY=1 -- the build should be Clippy clean modulo exceptional cases that may slip through (and soon linux-next will probably start reporting those warnings too).
Thanks!
Cheers,
Miguel
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
Quoted 9 lines · Miguel Ojeda
Quoted 4 lines · Alice Ryhl
It's not relevant in this patch, but another thing that may be useful is to add CLIPPY=1 to the make invocation when building normally. This causes additional warnings to be checked using a tool called clippy.
Yes, please do use
CLIPPY=1-- the build should be Clippy clean modulo exceptional cases that may slip through (and soon linux-next will probably start reporting those warnings too).
Understood. One thing I realized is that most (or all?) defconfigs will not enable rust.
$ make defconfig ... $ grep RUST .config CONFIG_RUST_IS_AVAILABLE=y # CONFIG_RUST is not set CONFIG_HAVE_RUST=y # CONFIG_HID_THRUSTMASTER is not set # CONFIG_TRUSTED_KEYS is not set # CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT is not set CONFIG_SYSTEM_TRUSTED_KEYRING=y CONFIG_SYSTEM_TRUSTED_KEYS="" # CONFIG_SECONDARY_TRUSTED_KEYRING is not set
That could imply that it might not receive as much build testing from people/build bots.
Signature · 3 lines
Cheers, David
Re: [PATCH v1 15/16] mm: rename zap_vma_ptes() to zap_special_vma_range()
In reply to David Hildenbrand (Arm)Quoted 14 lines · David Hildenbrand (Arm)
zap_vma_ptes()is the only zapping function we export to modules.It's essentially a wrapper around
zap_vma_range(), however, with some safety checks:
* That the passed range fits fully into the VMA
* That it's only used forVM_PFNMAPWe might want to support
VM_MIXEDMAPsoon as well, so use the more-generic term "special vma", although "special" is a bit overloaded. Maybe we'll later just support anyVM_SPECIALflag.While at it, improve the kerneldoc.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
Quoted 9 lines
--- arch/x86/kernel/cpu/sgx/encl.c | 2 +- drivers/comedi/comedi_fops.c | 2 +- drivers/gpu/drm/i915/i915_mm.c | 4 ++-- drivers/infiniband/core/uverbs_main.c | 6 +++--- drivers/misc/sgi-gru/grumain.c | 2 +- include/linux/mm.h | 2 +- mm/memory.c | 16 +++++++--------- 7 files changed, 16 insertions(+), 18 deletions(-)
Thanks,
Re: [PATCH v1 01/16] mm/madvise: drop range checks in madvise_free_single_vma()
In reply to David Hildenbrand (Arm)Quoted 14 lines · David Hildenbrand (Arm)
madvise_vma_behavior()-> madvise_dontneed_free()->madvise_free_single_vma() is only called from
madvise_walk_vmas()(a) After
try_vma_read_lock()confirmed that the whole range falls into a single VMA (seeis_vma_lock_sufficient()).(b) After adjusting the range to the VMA in the loop afterwards.
madvise_dontneed_free()might drop the MM lock when handling userfaultfd, but it properly looks up the VMA again to adjust the range.So in
madvise_free_single_vma(), the given range should always fall into a single VMA and should also span at least one page.
Quoted 8 lines
Let's drop the error checks.
The code now matches what we do in
madvise_dontneed_single_vma(), where we callzap_vma_range_batched()that documents: "The range must fit into one VMA.". Although that function still adjusts that range, we'll change that soon.Signed-off-by David Hildenbrand (Arm) <[email protected]>
Yeah I did wonder about some of these checks, thanks for going through and confirming these are useless.
Checked the madvise_dontneed_free() case to be sure and LGTM so overall:
Quoted 38 lines
--- mm/madvise.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)mm/madvise.c +4
-10@@ -799,9 +799,10 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior) {struct mm_struct *mm = madv_behavior->mm;struct vm_area_struct *vma = madv_behavior->vma; -unsigned long start_addr = madv_behavior->range.start; -unsigned long end_addr = madv_behavior->range.end; -struct mmu_notifier_range range; +struct mmu_notifier_range range = { + .start = madv_behavior->range.start, + .end = madv_behavior->range.end, + };struct mmu_gather *tlb = madv_behavior->tlb;struct mm_walk_ops walk_ops = { .pmd_entry = madvise_free_pte_range,@@ -811,12 +812,6 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)if (!vma_is_anonymous (vma))return -EINVAL ; - range.start =max (vma->vm_start, start_addr); -if (range.start >= vma->vm_end) -return -EINVAL ; - range.end =min (vma->vm_end, end_addr); -if (range.end <= vma->vm_start) -return -EINVAL ;mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, range.start, range.end); --2.43.0
Re: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
In reply to David Hildenbrand (Arm)Quoted 5 lines · David Hildenbrand (Arm)
Nobody except
memory.cshould really set that parameter to non-NULL. So let's just drop it and makeunmap_mapping_range_vma()usezap_page_range_single_batched()instead.Signed-off-by David Hildenbrand (Arm) <[email protected]>
This is nice, good cleanup.
Assuming rust side is all sorted (seems it from thread)... LGTM, so:
Quoted 192 lines
--- arch/s390/mm/gmap_helpers.c | 2 +- drivers/android/binder_alloc.c | 2 +- include/linux/mm.h | 5 ++--- kernel/bpf/arena.c | 3 +-- kernel/events/core.c | 2 +- mm/madvise.c | 3 +-- mm/memory.c | 16 ++++++++++------ net/ipv4/tcp.c | 5 ++--- rust/kernel/mm/virt.rs | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-)arch/s390/mm/gmap_helpers.c +1
-1@@ -89,7 +89,7 @@ void gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned loif (!vma)return ;if (!is_vm_hugetlb_page (vma)) -zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr,NULL ); +zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr); vmaddr = vma->vm_end; } }drivers/android/binder_alloc.c +1
-1@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ,NULL ); +zap_page_range_single (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index); }include/linux/mm.h +2
-3@@ -2835,11 +2835,10 @@ struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr,void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address,unsigned long size);void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, -unsigned long size,struct zap_details *details); +unsigned long size);static inline void zap_vma_pages (struct vm_area_struct *vma) { -zap_page_range_single (vma, vma->vm_start, - vma->vm_end - vma->vm_start,NULL ); +zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); }struct mmu_notifier_range ;kernel/bpf/arena.c +1
-2@@ -656,8 +656,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt)guard (mutex)(&arena->lock);/* iterate link list under lock */ list_for_each_entry (vml, &arena->vma_list, head) -zap_page_range_single (vml->vma, uaddr, -PAGE_SIZE * page_cnt,NULL ); +zap_page_range_single (vml->vma, uaddr,PAGE_SIZE * page_cnt); }static void arena_free_pages (struct bpf_arena *arena,long uaddr,long page_cnt,bool sleepable)kernel/events/core.c +1
-1@@ -7215,7 +7215,7 @@ static int map_range(struct perf_buffer *rb, struct vm_area_struct *vma)#ifdef CONFIG_MMU /* Clear any partial mappings on error. */ if (err) -zap_page_range_single (vma, vma->vm_start, nr_pages *PAGE_SIZE ,NULL ); +zap_page_range_single (vma, vma->vm_start, nr_pages *PAGE_SIZE );#endif return err;mm/madvise.c +1
-2@@ -1193,8 +1193,7 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)* OK some of the range have non-guard pages mapped, zap * them. This leaves existing guard pages in place. */ -zap_page_range_single (vma, range->start, - range->end - range->start,NULL ); +zap_page_range_single (vma, range->start, range->end - range->start); }/* mm/memory.c +10
-6@@ -2203,17 +2203,16 @@ void zap_page_range_single_batched(struct mmu_gather *tlb,* @vma: vm_area_struct holding the applicable pages * @address: starting address of pages to zap * @size: number of bytes to zap -* @details: details of shared cache invalidation * * The range must fit into one VMA. */ void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, -unsigned long size,struct zap_details *details) +unsigned long size) {struct mmu_gather tlb;tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, address, size, details); +zap_page_range_single_batched (&tlb, vma, address, size,NULL );tlb_finish_mmu (&tlb); }@@ -2235,7 +2234,7 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, !(vma->vm_flags & VM_PFNMAP))return ; -zap_page_range_single (vma, address, size,NULL ); +zap_page_range_single (vma, address, size); }EXPORT_SYMBOL_GPL (zap_vma_ptes);@@ -3003,7 +3002,7 @@ static int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long add* maintain page reference counts, and callers may free * pages due to the error. So zap it early. */ -zap_page_range_single (vma, addr, size,NULL ); +zap_page_range_single (vma, addr, size);return error; }@@ -4226,7 +4225,12 @@ static void unmap_mapping_range_vma(struct vm_area_struct *vma, unsigned long start_addr, unsigned long end_addr, struct zap_details *details) { - zap_page_range_single(vma, start_addr, end_addr - start_addr, details); + struct mmu_gather tlb; + +tlb_gather_mmu (&tlb, vma->vm_mm); +zap_page_range_single_batched (&tlb, vma, start_addr, + end_addr - start_addr, details); +tlb_finish_mmu (&tlb); } static inline void unmap_mapping_range_tree(struct rb_root_cached *root,net/ipv4/tcp.c +2
-3@@ -2104,7 +2104,7 @@ static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma, maybe_zap_len = total_bytes_to_map -/* All bytes to map */ *length +/* Mapped or pending */ (pages_remaining *PAGE_SIZE );/* Failed map. */ -zap_page_range_single (vma, *address, maybe_zap_len,NULL ); +zap_page_range_single (vma, *address, maybe_zap_len); err =0 ; }@@ -2269,8 +2269,7 @@ static int tcp_zerocopy_receive(struct sock *sk, total_bytes_to_map = avail_len & ~(PAGE_SIZE -1 );if (total_bytes_to_map) {if (!(zc->flags &TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT )) -zap_page_range_single (vma, address, total_bytes_to_map, -NULL ); +zap_page_range_single (vma, address, total_bytes_to_map); zc->length = total_bytes_to_map; zc->recv_skip_hint =0 ; }else {rust/kernel/mm/virt.rs +1
-2@@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size, core::ptr::null_mut ()) + bindings::zap_page_range_single (self.as_ptr (), address, size) }; } --2.43.0
Re: [PATCH v1 03/16] mm/memory: inline unmap_mapping_range_vma() into unmap_mapping_range_tree()
In reply to David Hildenbrand (Arm)Quoted 5 lines · David Hildenbrand (Arm)
Let's remove the number of unmap-related functions that cause confusion by inlining
unmap_mapping_range_vma()into its single caller. The end result looks pretty readable.Signed-off-by David Hildenbrand (Arm) <[email protected]>
Yeah that's fine, and while I usually like having lots of smaller functions to break up logic, I always felt when reading it that the zap logic had _too many_ so this is welcome.
LGTM, so:
Quoted 55 lines
--- mm/memory.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-)mm/memory.c +7
-17@@ -4221,18 +4221,6 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)return wp_page_copy (vmf); } -static void unmap_mapping_range_vma (struct vm_area_struct *vma, -unsigned long start_addr,unsigned long end_addr, -struct zap_details *details) -{ -struct mmu_gather tlb; - -tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, start_addr, - end_addr - start_addr, details); -tlb_finish_mmu (&tlb); -} - static inline void unmap_mapping_range_tree(struct rb_root_cached *root, pgoff_t first_index, pgoff_t last_index,@@ -4240,17 +4228,20 @@ static inline void unmap_mapping_range_tree(struct rb_root_cached *root, {struct vm_area_struct *vma;pgoff_t vba, vea, zba, zea; +unsigned long start, size; +struct mmu_gather tlb; vma_interval_tree_foreach(vma, root, first_index, last_index) { vba = vma->vm_pgoff; vea = vba +vma_pages (vma) -1 ; zba =max (first_index, vba); zea =min (last_index, vea); + start = ((zba - vba) <<PAGE_SHIFT ) + vma->vm_start; + size = (zea - zba +1 ) <<PAGE_SHIFT ; -unmap_mapping_range_vma (vma, - ((zba - vba) <<PAGE_SHIFT ) + vma->vm_start, - ((zea - vba +1 ) <<PAGE_SHIFT ) + vma->vm_start, - details); +tlb_gather_mmu (&tlb, vma->vm_mm); +zap_page_range_single_batched (&tlb, vma, start, size, details); +tlb_finish_mmu (&tlb); } } --2.43.0
Re: [PATCH v1 04/16] mm/memory: simplify calculation in unmap_mapping_range_tree()
In reply to David Hildenbrand (Arm)Quoted 4 lines · David Hildenbrand (Arm)
Let's simplify the calculation a bit further to make it easier to get, reusing
vma_last_pgoff()which we move frominterval_tree.ctomm.h.Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks, some crusty old code here much improved. LGTM, so:
Quoted 45 lines
--- include/linux/mm.h | 5 +++++ mm/interval_tree.c | 5 ----- mm/memory.c | 12 +++++------- 3 files changed, 10 insertions(+), 12 deletions(-)include/linux/mm.h +5
-0@@ -4000,6 +4000,11 @@ static inline unsigned long vma_pages(const struct vm_area_struct *vma)return (vma->vm_end - vma->vm_start) >>PAGE_SHIFT ; } +static inline unsigned long vma_last_pgoff (struct vm_area_struct *vma) +{ +return vma->vm_pgoff +vma_pages (vma) -1 ; +} +static inline unsigned long vma_desc_size (const struct vm_area_desc *desc) {return desc->end - desc->start;mm/interval_tree.c +0
-5@@ -15,11 +15,6 @@ static inline unsigned long vma_start_pgoff(struct vm_area_struct *v)return v->vm_pgoff; } -static inline unsigned long vma_last_pgoff (struct vm_area_struct *v) -{ -return v->vm_pgoff +vma_pages (v) -1 ; -} - INTERVAL_TREE_DEFINE(struct vm_area_struct, shared.rb, unsigned long, shared.rb_subtree_last, vma_start_pgoff, vma_last_pgoff, /* empty */, vma_interval_tree)mm/memory.c +0
-0@@ -4227,17 +4227,15 @@ static inline void unmap_mapping_range_tree(struct rb_root_cached *root, struct zap_details *details) {
mm/memory.c static inline void unmap_mapping_range_tree(struct rb_root_cached *root, · line 4238
Quoted 10 lines
mm/memory.c +0
-5struct vm_area_struct *vma; -pgoff_t vba, vea, zba, zea;unsigned long start, size;struct mmu_gather tlb; vma_interval_tree_foreach(vma, root, first_index, last_index) { - vba = vma->vm_pgoff; - vea = vba +vma_pages (vma) -1 ; - zba =max (first_index, vba); - zea =min (last_index, vea);
These variable names... Lord.
mm/memory.c static inline void unmap_mapping_range_tree(struct rb_root_cached *root, · line 4235
Quoted 4 lines
mm/memory.c +2
-2- start = ((zba - vba) <<PAGE_SHIFT ) + vma->vm_start; - size = (zea - zba +1 ) <<PAGE_SHIFT ; +const pgoff_t start_idx =max (first_index, vma->vm_pgoff); +const pgoff_t end_idx =min (last_index,vma_last_pgoff (vma)) +1 ;
I guess since 'end' is by-convention the +1 of last this is fine
Quoted 9 lines
mm/memory.c +3
-1+ + start = vma->vm_start + ((start_idx - vma->vm_pgoff) <<PAGE_SHIFT ); + size = (end_idx - start_idx) <<PAGE_SHIFT ;tlb_gather_mmu (&tlb, vma->vm_mm);zap_page_range_single_batched (&tlb, vma, start, size, details); --2.43.0
Re: [PATCH v1 05/16] mm/oom_kill: use MMU_NOTIFY_CLEAR in __oom_reap_task_mm()
In reply to David Hildenbrand (Arm)Quoted 11 lines · David Hildenbrand (Arm)
In commit
7269f999934b("mm/mmu_notifier: use correctmmu_notifierevents for each invalidation") we converted allMMU_NOTIFY_UNMAPtoMMU_NOTIFY_CLEAR, except the ones that actually performmunmap()ormremap()as documented.
__oom_reap_task_mm()behaves much more likeMADV_DONTNEED. So useMMU_NOTIFY_CLEARas well.This is a preparation for further changes.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM, so:
Quoted 20 lines
--- mm/oom_kill.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/oom_kill.c +1
-2@@ -551,7 +551,7 @@ static bool __oom_reap_task_mm(struct mm_struct *mm)struct mmu_notifier_range range;struct mmu_gather tlb; -mmu_notifier_range_init (&range,MMU_NOTIFY_UNMAP ,0 , +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , mm, vma->vm_start, vma->vm_end);tlb_gather_mmu (&tlb, mm); --2.43.0
Re: [PATCH v1 06/16] mm/oom_kill: factor out zapping of VMA into zap_vma_for_reaping()
In reply to David Hildenbrand (Arm)Quoted 5 lines · David Hildenbrand (Arm)
Let's factor it out so we can turn
unmap_page_range()into a static function instead, and so oom reaping has a clean interface to call.Note that hugetlb is not supported, because it would require a bunch of hugetlb-specific further actions (see
zap_page_range_single_batched()).
Ugh gawd. Hugetlb.
Quoted 2 lines
Signed-off-by David Hildenbrand (Arm) <[email protected]>
Seems reasonable, so:
Quoted 109 lines
--- mm/internal.h | 5 +---- mm/memory.c | 36 ++++++++++++++++++++++++++++++++---- mm/oom_kill.c | 15 +-------------- 3 files changed, 34 insertions(+), 22 deletions(-)mm/internal.h +1
-4@@ -536,13 +536,10 @@ static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) }struct zap_details ; -void unmap_page_range (struct mmu_gather *tlb, -struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details);void zap_page_range_single_batched (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long addr,unsigned long size,struct zap_details *details); +int zap_vma_for_reaping (struct vm_area_struct *vma);int folio_unmap_invalidate (struct address_space *mapping,struct folio *folio,gfp_t gfp);mm/memory.c +32
-4@@ -2054,10 +2054,9 @@ static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,return addr; } -void unmap_page_range (struct mmu_gather *tlb, -struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details) +static void unmap_page_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long addr,unsigned long end, +struct zap_details *details) {pgd_t *pgd;unsigned long next;@@ -2115,6 +2114,35 @@ static void unmap_single_vma(struct mmu_gather *tlb, } } +/** +* zap_vma_for_reaping - zap all page table entries in the vma without blocking +* @vma: The vma to zap. +* +* Zap all page table entries in the vma without blocking for use by the oom +* killer. Hugetlb vmas are not supported. +* +* Returns: 0 on success, -EBUSY if we would have to block. +*/ +int zap_vma_for_reaping (struct vm_area_struct *vma) +{ +struct mmu_notifier_range range; +struct mmu_gather tlb; + +VM_WARN_ON_ONCE (is_vm_hugetlb_page (vma)); + +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, + vma->vm_start, vma->vm_end); +tlb_gather_mmu (&tlb, vma->vm_mm); +if (mmu_notifier_invalidate_range_start_nonblock (&range)) { +tlb_finish_mmu (&tlb); +return -EBUSY ; + } +unmap_page_range (&tlb, vma, range.start, range.end,NULL ); +mmu_notifier_invalidate_range_end (&range); +tlb_finish_mmu (&tlb); +return 0 ; +} +/** * unmap_vmas - unmap a range of memory covered by a list of vma's * @tlb: address of the caller's struct mmu_gather mm/oom_kill.c +1
-15@@ -548,21 +548,8 @@ static bool __oom_reap_task_mm(struct mm_struct *mm)* count elevated without a good reason. */ if (vma_is_anonymous (vma) || !(vma->vm_flags &VM_SHARED )) { -struct mmu_notifier_range range; -struct mmu_gather tlb; - -mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , - mm, vma->vm_start, - vma->vm_end); -tlb_gather_mmu (&tlb, mm); -if (mmu_notifier_invalidate_range_start_nonblock (&range)) { -tlb_finish_mmu (&tlb); +if (zap_vma_for_reaping (vma)) ret = false; -continue ; - } -unmap_page_range (&tlb, vma, range.start, range.end,NULL ); -mmu_notifier_invalidate_range_end (&range); -tlb_finish_mmu (&tlb); } } --2.43.0
Re: [PATCH v1 07/16] mm/memory: rename unmap_single_vma() to __zap_vma_range()
In reply to David Hildenbrand (Arm)Quoted 3 lines · David Hildenbrand (Arm)
Let's rename it to better fit our new naming scheme.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
Yesssss thank you! I hate[d] the rando 'sometimes zap sometimes unmap' naming convention here.
LGTM, so:
Quoted 38 lines
--- mm/memory.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)mm/memory.c +3
-4@@ -2074,7 +2074,7 @@ static void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma, } -static void unmap_single_vma (struct mmu_gather *tlb, +static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start_addr,unsigned long end_addr,struct zap_details *details) {@@ -2177,7 +2177,7 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap)unsigned long start = unmap->vma_start;unsigned long end = unmap->vma_end;hugetlb_zap_begin (vma, &start, &end); -unmap_single_vma (tlb, vma, start, end, &details); +__zap_vma_range (tlb, vma, start, end, &details);hugetlb_zap_end (vma, &details); vma =mas_find (unmap->mas, unmap->tree_end -1 ); }while (vma);@@ -2213,7 +2213,7 @@ void zap_page_range_single_batched(struct mmu_gather *tlb,* unmap 'address-end' not 'range.start-range.end' as range * could have been expanded for hugetlb pmd sharing. */ -unmap_single_vma (tlb, vma, address, end, details); +__zap_vma_range (tlb, vma, address, end, details);mmu_notifier_invalidate_range_end (&range);if (is_vm_hugetlb_page (vma)) {/* -- 2.43.0
Re: [PATCH v1 09/16] mm/memory: convert details->even_cows into details->skip_cows
In reply to David Hildenbrand (Arm)Quoted 7 lines · David Hildenbrand (Arm)
The current semantics are confusing: simply because someone specifies an empty
zap_detailstruct suddenly makesshould_zap_cows()behave differently. The default should be to also zap CoW'ed anonymous pages.Really only
unmap_mapping_pages()and friends want to skip zapping of these anon folios.
Quoted 7 lines
So let's invert the meaning; turn the confusing "
reclaim_pt" check that overrides other properties inshould_zap_cows()into a safety check.Note that the only caller that sets reclaim_pt=true is
madvise_dontneed_single_vma(), which wants to zap any pages.Signed-off-by David Hildenbrand (Arm) <[email protected]>
This is another nice change. I have some tweak suggestions below, but past experience suggests to me it's quite possible you do what I ask in subsequent patches, will see :)
Anyway overall LGTM, so:
Quoted 36 lines
--- include/linux/mm.h | 2 +- mm/madvise.c | 1 - mm/memory.c | 12 ++++++------ 3 files changed, 7 insertions(+), 8 deletions(-)include/linux/mm.h +1
-1@@ -2798,7 +2798,7 @@ extern void pagefault_out_of_memory(void);*/ struct zap_details {struct folio *single_folio;/* Locked folio to be unmapped */ -bool even_cows;/* Zap COWed private pages too? */ +bool skip_cows;/* Do not zap COWed private pages */ bool reclaim_pt;/* Need reclaim page tables? */ zap_flags_t zap_flags;/* Extra flags for zapping */ };mm/madvise.c +0
-1@@ -853,7 +853,6 @@ static long madvise_dontneed_single_vma(struct madvise_behavior *madv_behavior)struct madvise_behavior_range *range = &madv_behavior->range;struct zap_details details = { .reclaim_pt = true, - .even_cows = true, }; zap_page_range_single_batched(mm/memory.c +0
-0
mm/memory.c copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) · line 1554
Quoted 1 line
mm/memory.c +0
-0@@ -1554,11 +1554,13 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)static inline bool should_zap_cows (struct zap_details *details)
Not sure if you fix up later, but we should probably change this function to should_skip_cows() to keep everything consistent, otherwise this is a bit weird and confusing.
Quoted 33 lines
mm/memory.c +5
-5{/* By default, zap all pages */ -if (!details || details->reclaim_pt) +if (!details)return true; +VM_WARN_ON_ONCE (details->skip_cows && details->reclaim_pt); +/* Or, we zap COWed pages only if the caller wants to */ -return details->even_cows; +return !details->skip_cows; }/* Decides whether we should zap this folio with the folio pointer specified */ @@ -2149,8 +2151,6 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap)struct mmu_notifier_range range;struct zap_details details = { .zap_flags =ZAP_FLAG_DROP_MARKER |ZAP_FLAG_UNMAP , -/* Careful - we need to zap private pages too! */ - .even_cows = true, }; vma = unmap->first;@@ -4282,7 +4282,7 @@ void unmap_mapping_folio(struct folio *folio) first_index = folio->index; last_index =folio_next_index (folio) -1 ; - details.even_cows = false; + details.skip_cows = true; details.single_folio = folio; details.zap_flags =ZAP_FLAG_DROP_MARKER ;
mm/memory.c void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, · line 4315
Quoted 5 lines
mm/memory.c +1
-1@@ -4312,7 +4312,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,pgoff_t first_index = start;pgoff_t last_index = start + nr -1 ; - details.even_cows = even_cows; + details.skip_cows = !even_cows;
Not sure if you clean up later, but seems sensible to cascade the change into the local boolean here.
Quoted 6 lines
mm/memory.c +0
-1if (last_index < first_index) last_index =ULONG_MAX ; --2.43.0
Re: [PATCH v1 10/16] mm/memory: use __zap_vma_range() in zap_vma_for_reaping()
In reply to David Hildenbrand (Arm)Quoted 5 lines · David Hildenbrand (Arm)
Let's call
__zap_vma_range()instead ofunmap_page_range()to prepare for further cleanups.To keep the existing behavior, whereby we do not call
uprobe_munmap()which could block, add a new "reaping" member tozap_detailsand use it.
I am always in favour of making further use of helper structs :)
Quoted 5 lines
Likely we should handle the possible blocking in
uprobe_munmap()differently, but for now keep it unchanged.Signed-off-by David Hildenbrand (Arm) <[email protected]>
OK this looks like it's doing the equivalent of what was there before, so:
Quoted 43 lines
--- include/linux/mm.h | 1 + mm/memory.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-)include/linux/mm.h +1
-0@@ -2800,6 +2800,7 @@ struct zap_details {struct folio *single_folio;/* Locked folio to be unmapped */ bool skip_cows;/* Do not zap COWed private pages */ bool reclaim_pt;/* Need reclaim page tables? */ +bool reaping;/* Reaping, do not block. */ zap_flags_t zap_flags;/* Extra flags for zapping */ };mm/memory.c +5
-1@@ -2079,14 +2079,18 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma, unsigned long start, unsigned long end, struct zap_details *details) { + const bool reaping = details && details->reaping; +VM_WARN_ON_ONCE (start >= end || !range_in_vma (vma, start, end)); -if (vma->vm_file) +/* uprobe_munmap() might sleep, so skip it when reaping. */ +if (vma->vm_file && !reaping)uprobe_munmap (vma, start, end);if (unlikely (is_vm_hugetlb_page (vma))) {zap_flags_t zap_flags = details ? details->zap_flags :0 ; +VM_WARN_ON_ONCE (reaping);/* * vm_file will be NULL when we fail early while instantiating * a new mapping. In this case, no pages were mapped yet and @@ -2111,11 +2115,12 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,*/
mm/memory.c static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2118
Quoted 10 lines
mm/memory.c +3
-2int zap_vma_for_reaping (struct vm_area_struct *vma) { +struct zap_details details = { + .reaping = true, + };struct mmu_notifier_range range;struct mmu_gather tlb; -VM_WARN_ON_ONCE (is_vm_hugetlb_page (vma)); -
I guess because you've moved this safety check into __zap_vma_range()?
Quoted 15 lines
mm/memory.c +1
-2mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, vma->vm_start, vma->vm_end);tlb_gather_mmu (&tlb, vma->vm_mm);@@ -2123,7 +2128,7 @@ int zap_vma_for_reaping(struct vm_area_struct *vma)tlb_finish_mmu (&tlb);return -EBUSY ; } -unmap_page_range (&tlb, vma, range.start, range.end,NULL ); +__zap_vma_range (&tlb, vma, range.start, range.end, &details);mmu_notifier_invalidate_range_end (&range);tlb_finish_mmu (&tlb);return 0 ; --2.43.0
Re: [PATCH v1 11/16] mm/memory: inline unmap_page_range() into __zap_vma_range()
In reply to David Hildenbrand (Arm)Quoted 6 lines · David Hildenbrand (Arm)
Let's inline it into the single caller to reduce the number of confusing unmap/zap helpers.
Get rid of the unnecessary
BUG_ON().Signed-off-by David Hildenbrand (Arm) <[email protected]>
Small nits below, but overall LGTM so:
Quoted 35 lines
--- mm/memory.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)mm/memory.c +0
-19@@ -2056,25 +2056,6 @@ static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,return addr; } -static void unmap_page_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details) -{ -pgd_t *pgd; -unsigned long next; - -BUG_ON (addr >= end); -tlb_start_vma (tlb, vma); - pgd =pgd_offset (vma->vm_mm, addr); -do { - next =pgd_addr_end (addr, end); -if (pgd_none_or_clear_bad (pgd)) -continue ; - next =zap_p4d_range (tlb, vma, pgd, addr, next, details); - }while (pgd++, addr = next, addr != end); -tlb_end_vma (tlb, vma); -} -static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start,unsigned long end,struct zap_details *details)
mm/memory.c static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2084
Quoted 5 lines
mm/memory.c +1
-1@@ -2100,7 +2081,18 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,return ;__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); }else { -unmap_page_range (tlb, vma, start, end, details); +unsigned long next, cur = start;
VERY nitty, but generally current is abbreviated to curr not cur.
It's not a big deal, but why not addr?
Quoted 17 lines
mm/memory.c +11
-1+pgd_t *pgd; + +tlb_start_vma (tlb, vma); + pgd =pgd_offset (vma->vm_mm, cur); +do { + next =pgd_addr_end (cur, end); +if (pgd_none_or_clear_bad (pgd)) +continue ; + next =zap_p4d_range (tlb, vma, pgd, cur, next, details); + }while (pgd++, cur = next, cur != end); +tlb_end_vma (tlb, vma); } } --2.43.0
Re: [PATCH v1 12/16] mm: rename zap_vma_pages() to zap_vma()
In reply to David Hildenbrand (Arm)Quoted 4 lines · David Hildenbrand (Arm)
Let's rename it to an even simpler name. While at it, add some simplistic kernel doc.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM, so:
Quoted 80 lines
--- arch/powerpc/platforms/book3s/vas-api.c | 2 +- arch/powerpc/platforms/pseries/vas.c | 2 +- include/linux/mm.h | 6 +++++- lib/vdso/datastore.c | 2 +- mm/page-writeback.c | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-)arch/powerpc/platforms/book3s/vas-api.c +1
-1@@ -414,7 +414,7 @@ static vm_fault_t vas_mmap_fault(struct vm_fault *vmf)/* * When the LPAR lost credits due to core removal or during * migration, invalidate the existing mapping for the current -* paste addresses and set windows in-active (zap_vma_pages in +* paste addresses and set windows in-active (zap_vma() in * reconfig_close_windows()). * New mapping will be done later after migration or new credits * available. So continue to receive faults if the user space arch/powerpc/platforms/pseries/vas.c +1
-1@@ -807,7 +807,7 @@ static int reconfig_close_windows(struct vas_caps *vcap, int excess_creds,* is done before the original mmap() and after the ioctl. */ if (vma) -zap_vma_pages (vma); +zap_vma (vma);mutex_unlock (&task_ref->mmap_mutex);mmap_write_unlock (task_ref->mm);include/linux/mm.h +5
-1@@ -2837,7 +2837,11 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, unsigned long size);void zap_page_range_single (struct vm_area_struct *vma,unsigned long address,unsigned long size); -static inline void zap_vma_pages (struct vm_area_struct *vma) +/** +* zap_vma - zap all page table entries in a vma +* @vma: The vma to zap. +*/ +static inline void zap_vma (struct vm_area_struct *vma) {zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); }lib/vdso/datastore.c +1
-1@@ -121,7 +121,7 @@ int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)mmap_read_lock (mm);for_each_vma (vmi, vma) {if (vma_is_special_mapping (vma, &vdso_vvar_mapping)) -zap_vma_pages (vma); +zap_vma (vma); }mmap_read_unlock (mm);mm/page-writeback.c +1
-2@@ -2645,7 +2645,7 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb)* while this function is in progress, although it may have been truncated * before this function is called. Most callers have the folio locked. * A few have the folio blocked from truncation through other means (e.g. -* zap_vma_pages() has it mapped and is holding the page table lock). +* zap_vma() has it mapped and is holding the page table lock). * When called from mark_buffer_dirty(), the filesystem should hold a * reference to the buffer_head that is being marked dirty, which causes * try_to_free_buffers() to fail. -- 2.43.0
Re: [PATCH v1 13/16] mm: rename zap_page_range_single_batched() to zap_vma_range_batched()
In reply to David Hildenbrand (Arm)Quoted 5 lines · David Hildenbrand (Arm)
Let's make the naming more consistent with our new naming scheme.
While at it, polish the kerneldoc a bit.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM, so:
Quoted 89 lines
--- mm/internal.h | 2 +- mm/madvise.c | 5 ++--- mm/memory.c | 23 +++++++++++++---------- 3 files changed, 16 insertions(+), 14 deletions(-)mm/internal.h +1
-1@@ -536,7 +536,7 @@ static inline void sync_with_folio_pmd_zap(struct mm_struct *mm, pmd_t *pmdp) }struct zap_details ; -void zap_page_range_single_batched (struct mmu_gather *tlb, +void zap_vma_range_batched (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long addr,unsigned long size,struct zap_details *details);int zap_vma_for_reaping (struct vm_area_struct *vma);mm/madvise.c +2
-3@@ -855,9 +855,8 @@ static long madvise_dontneed_single_vma(struct madvise_behavior *madv_behavior) .reclaim_pt = true, }; -zap_page_range_single_batched ( - madv_behavior->tlb, madv_behavior->vma, range->start, - range->end - range->start, &details); +zap_vma_range_batched (madv_behavior->tlb, madv_behavior->vma, + range->start, range->end - range->start, &details);return 0 ; }mm/memory.c +13
-11@@ -2167,17 +2167,20 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap) }/** -* zap_page_range_single_batched - remove user pages in a given range +* zap_vma_range_batched - zap page table entries in a vma range * @tlb: pointer to the caller's struct mmu_gather -* @vma: vm_area_struct holding the applicable pages -* @address: starting address of pages to remove -* @size: number of bytes to remove -* @details: details of shared cache invalidation +* @vma: the vma covering the range to zap +* @address: starting address of the range to zap +* @size: number of bytes to zap +* @details: details specifying zapping behavior +* +* @tlb must not be NULL. The provided address range must be fully +* contained within @vma. If @vma is for hugetlb, @tlb is flushed and +* re-initialized by this function. * -* @tlb shouldn't be NULL. The range must fit into one VMA. If @vma is for -* hugetlb, @tlb is flushed and re-initialized by this function. +* If @details is NULL, this function will zap all page table entries. */ -void zap_page_range_single_batched (struct mmu_gather *tlb, +void zap_vma_range_batched (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long address,unsigned long size,struct zap_details *details) {@@ -2225,7 +2228,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,struct mmu_gather tlb;tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, address, size,NULL ); +zap_vma_range_batched (&tlb, vma, address, size,NULL );tlb_finish_mmu (&tlb); }@@ -4251,7 +4254,7 @@ static inline void unmap_mapping_range_tree(struct rb_root_cached *root, size = (end_idx - start_idx) <<PAGE_SHIFT ;tlb_gather_mmu (&tlb, vma->vm_mm); -zap_page_range_single_batched (&tlb, vma, start, size, details); +zap_vma_range_batched (&tlb, vma, start, size, details);tlb_finish_mmu (&tlb); } } --2.43.0
Re: [PATCH v1 14/16] mm: rename zap_page_range_single() to zap_vma_range()
In reply to David Hildenbrand (Arm)Quoted 5 lines · David Hildenbrand (Arm)
Let's rename it to make it better match our new naming scheme.
While at it, polish the kerneldoc.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM, again obviously assuming the rustfmt stuff is sorted :>), so:
Quoted 229 lines
--- arch/s390/mm/gmap_helpers.c | 2 +- drivers/android/binder/page_range.rs | 4 ++-- drivers/android/binder_alloc.c | 2 +- include/linux/mm.h | 4 ++-- kernel/bpf/arena.c | 2 +- kernel/events/core.c | 2 +- mm/madvise.c | 4 ++-- mm/memory.c | 14 +++++++------- net/ipv4/tcp.c | 6 +++--- rust/kernel/mm/virt.rs | 4 ++-- 10 files changed, 22 insertions(+), 22 deletions(-)arch/s390/mm/gmap_helpers.c +1
-1@@ -89,7 +89,7 @@ void gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned loif (!vma)return ;if (!is_vm_hugetlb_page (vma)) -zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr); +zap_vma_range (vma, vmaddr,min (end, vma->vm_end) - vmaddr); vmaddr = vma->vm_end; } }drivers/android/binder/page_range.rs +2
-2@@ -130,7 +130,7 @@ pub(crate) struct ShrinkablePageRange { pid: Pid,/// The mm for the relevant process. mm: ARef<Mm>, -/// Used to synchronize calls to `vm_insert_page` and `zap_page_range_single`. +/// Used to synchronize calls to `vm_insert_page` and `zap_vma_range`. #[pin] mm_lock: Mutex<()>,/// Spinlock protecting changes to pages. @@ -719,7 +719,7 @@ fn drop(self: Pin<&mut Self>) {if let Some (vma) = mmap_read.vma_lookup (vma_addr) {let user_page_addr = vma_addr + (page_index <<PAGE_SHIFT ); - vma.zap_page_range_single (user_page_addr,PAGE_SIZE ); + vma.zap_vma_range (user_page_addr,PAGE_SIZE ); }drop (mmap_read);drivers/android/binder_alloc.c +1
-1@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ); +zap_vma_range (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index); }include/linux/mm.h +2
-2@@ -2835,7 +2835,7 @@ struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr,void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address,unsigned long size); -void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, +void zap_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size);/** * zap_vma - zap all page table entries in a vma @@ -2843,7 +2843,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,*/ static inline void zap_vma (struct vm_area_struct *vma) { -zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); +zap_vma_range (vma, vma->vm_start, vma->vm_end - vma->vm_start); }struct mmu_notifier_range ;kernel/bpf/arena.c +1
-1@@ -656,7 +656,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt)guard (mutex)(&arena->lock);/* iterate link list under lock */ list_for_each_entry (vml, &arena->vma_list, head) -zap_page_range_single (vml->vma, uaddr,PAGE_SIZE * page_cnt); +zap_vma_range (vml->vma, uaddr,PAGE_SIZE * page_cnt); }static void arena_free_pages (struct bpf_arena *arena,long uaddr,long page_cnt,bool sleepable)kernel/events/core.c +1
-1@@ -7215,7 +7215,7 @@ static int map_range(struct perf_buffer *rb, struct vm_area_struct *vma)#ifdef CONFIG_MMU /* Clear any partial mappings on error. */ if (err) -zap_page_range_single (vma, vma->vm_start, nr_pages *PAGE_SIZE ); +zap_vma_range (vma, vma->vm_start, nr_pages *PAGE_SIZE );#endif return err;mm/madvise.c +2
-2@@ -832,7 +832,7 @@ static int madvise_free_single_vma(struct madvise_behavior *madv_behavior)* Application no longer needs these pages. If the pages are dirty, * it's OK to just throw them away. The app will be more careful about * data it wants to keep. Be sure to free swap resources too. The -* zap_page_range_single call sets things up for shrink_active_list to actually +* zap_vma_range call sets things up for shrink_active_list to actually * free these pages later if no one else has touched them in the meantime, * although we could add these pages to a global reuse list for * shrink_active_list to pick up before reclaiming other pages. @@ -1191,7 +1191,7 @@ static long madvise_guard_install(struct madvise_behavior *madv_behavior)* OK some of the range have non-guard pages mapped, zap * them. This leaves existing guard pages in place. */ -zap_page_range_single (vma, range->start, range->end - range->start); +zap_vma_range (vma, range->start, range->end - range->start); }/* mm/memory.c +7
-7@@ -2215,14 +2215,14 @@ void zap_vma_range_batched(struct mmu_gather *tlb, }/** -* zap_page_range_single - remove user pages in a given range -* @vma: vm_area_struct holding the applicable pages -* @address: starting address of pages to zap +* zap_vma_range - zap all page table entries in a vma range +* @vma: the vma covering the range to zap +* @address: starting address of the range to zap * @size: number of bytes to zap * -* The range must fit into one VMA. +* The provided address range must be fully contained within @vma. */ -void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, +void zap_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size) {struct mmu_gather tlb;@@ -2250,7 +2250,7 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address, !(vma->vm_flags & VM_PFNMAP))return ; -zap_page_range_single (vma, address, size); +zap_vma_range (vma, address, size); }EXPORT_SYMBOL_GPL (zap_vma_ptes);@@ -3018,7 +3018,7 @@ static int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long add* maintain page reference counts, and callers may free * pages due to the error. So zap it early. */ -zap_page_range_single (vma, addr, size); +zap_vma_range (vma, addr, size);return error; }net/ipv4/tcp.c +3
-3@@ -2104,7 +2104,7 @@ static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma, maybe_zap_len = total_bytes_to_map -/* All bytes to map */ *length +/* Mapped or pending */ (pages_remaining *PAGE_SIZE );/* Failed map. */ -zap_page_range_single (vma, *address, maybe_zap_len); +zap_vma_range (vma, *address, maybe_zap_len); err =0 ; }@@ -2112,7 +2112,7 @@ static int tcp_zerocopy_vm_insert_batch_error(struct vm_area_struct *vma,unsigned long leftover_pages = pages_remaining;int bytes_mapped; -/* We called zap_page_range_single, try to reinsert. */ +/* We called zap_vma_range, try to reinsert. */ err =vm_insert_pages (vma, *address, pending_pages, &pages_remaining);@@ -2269,7 +2269,7 @@ static int tcp_zerocopy_receive(struct sock *sk, total_bytes_to_map = avail_len & ~(PAGE_SIZE -1 );if (total_bytes_to_map) {if (!(zc->flags &TCP_RECEIVE_ZEROCOPY_FLAG_TLB_CLEAN_HINT )) -zap_page_range_single (vma, address, total_bytes_to_map); +zap_vma_range (vma, address, total_bytes_to_map); zc->length = total_bytes_to_map; zc->recv_skip_hint =0 ; }else {rust/kernel/mm/virt.rs +2
-3@@ -113,7 +113,7 @@ pub fn end(&self) -> usize {/// kernel goes further in freeing unused page tables, but for the purposes of this operation /// we must only assume that the leaf level is cleared. #[inline] -pub fn zap_page_range_single (&self, address:usize , size:usize ) { +pub fn zap_vma_range (&self, address:usize , size:usize ) {let (end, did_overflow) = address.overflowing_add (size);if did_overflow || address < self.start () || self.end () < end {// TODO: call WARN_ONCE once Rust version of it is added @@ -124,7 +124,7 @@ pub fn zap_page_range_single(&self, address: usize, size: usize) {// sufficient for this method call. This method has no requirements on the vma flags. The // address range is checked to be within the vma. unsafe { - bindings::zap_page_range_single (self.as_ptr (), address, size) + bindings::zap_vma_range (self.as_ptr (), address, size) }; } --2.43.0
Re: [PATCH v1 08/16] mm/memory: move adjusting of address range to unmap_vmas()
In reply to David Hildenbrand (Arm)Quoted 13 lines · David Hildenbrand (Arm)
__zap_vma_range()has two callers, wherebyzap_page_range_single_batched()documents that the range must fit into the VMA range.So move adjusting the range to
unmap_vmas()where it is actually required and add a safety check in__zap_vma_range()instead. Inunmap_vmas(), we'd never expect to have empty ranges (otherwise, why have the vma in there in the first place).
__zap_vma_range()will no longer be called with start == end, so cleanup the function a bit. While at it, simplify the overly long comment to its core message.
Quoted 7 lines
We will no longer call
uprobe_munmap()for start == end, which actually seems to be the right thing to do.Note that hugetlb_zap_begin()->...->adjust_range_if_pmd_sharing_possible() cannot result in the range exceeding the vma range.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM, So:
Quoted 95 lines
--- mm/memory.c | 58 +++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 35 deletions(-)mm/memory.c +23
-36@@ -2073,44 +2073,28 @@ static void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,tlb_end_vma (tlb, vma); } - -static void __zap_vma_range (struct mmu_gather *tlb, -struct vm_area_struct *vma,unsigned long start_addr, -unsigned long end_addr,struct zap_details *details) +static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma, +unsigned long start,unsigned long end, +struct zap_details *details) { -unsigned long start =max (vma->vm_start, start_addr); -unsigned long end; - -if (start >= vma->vm_end) -return ; - end =min (vma->vm_end, end_addr); -if (end <= vma->vm_start) -return ; +VM_WARN_ON_ONCE (start >= end || !range_in_vma (vma, start, end));if (vma->vm_file)uprobe_munmap (vma, start, end); -if (start != end) { -if (unlikely (is_vm_hugetlb_page (vma))) { -/* -* It is undesirable to test vma->vm_file as it -* should be non-null for valid hugetlb area. -* However, vm_file will be NULL in the error -* cleanup path of mmap_region. When -* hugetlbfs ->mmap method fails, -* mmap_region() nullifies vma->vm_file -* before calling this function to clean up. -* Since no pte has actually been setup, it is -* safe to do nothing in this case. -*/ -if (vma->vm_file) { -zap_flags_t zap_flags = details ? - details->zap_flags :0 ; -__unmap_hugepage_range (tlb, vma, start, end, -NULL , zap_flags); - } - }else -unmap_page_range (tlb, vma, start, end, details); +if (unlikely (is_vm_hugetlb_page (vma))) { +zap_flags_t zap_flags = details ? details->zap_flags :0 ; + +/* +* vm_file will be NULL when we fail early while instantiating +* a new mapping. In this case, no pages were mapped yet and +* there is nothing to do. +*/ +if (!vma->vm_file) +return ; +__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); + }else { +unmap_page_range (tlb, vma, start, end, details); } }@@ -2174,8 +2158,9 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap) unmap->vma_start, unmap->vma_end);mmu_notifier_invalidate_range_start (&range); do { - unsigned long start = unmap->vma_start; - unsigned long end = unmap->vma_end; + unsigned long start = max(vma->vm_start, unmap->vma_start); + unsigned long end = min(vma->vm_end, unmap->vma_end); + hugetlb_zap_begin(vma, &start, &end); __zap_vma_range(tlb, vma, start, end, &details); hugetlb_zap_end(vma, &details);@@ -2204,6 +2189,9 @@ void zap_page_range_single_batched(struct mmu_gather *tlb,VM_WARN_ON_ONCE (!tlb || tlb->mm != vma->vm_mm); +if (unlikely (!size)) +return ; +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , vma->vm_mm, address, end);hugetlb_zap_begin (vma, &range.start, &range.end); --2.43.0
Re: [PATCH v1 15/16] mm: rename zap_vma_ptes() to zap_special_vma_range()
In reply to David Hildenbrand (Arm)Quoted 8 lines · David Hildenbrand (Arm)
zap_vma_ptes()is the only zapping function we export to modules.It's essentially a wrapper around
zap_vma_range(), however, with some safety checks:
* That the passed range fits fully into the VMA
* That it's only used forVM_PFNMAPWe might want to support
VM_MIXEDMAPsoon as well, so use the
I think you should say we _are_ going to since patch 16/16 does that :)
Quoted 2 lines
more-generic term "special vma", although "special" is a bit overloaded. Maybe we'll later just support any
VM_SPECIALflag.
Yeah I hate the name, but I guess it's probably the best we can manage because VM_SPECIAL and both VM_PFNMAP and VM_MIXEDMAP _are_ 'special' in this sense so meh I guess it's the best we can do.
Quoted 4 lines
While at it, improve the kerneldoc.
Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM, so:
Quoted 155 lines
--- arch/x86/kernel/cpu/sgx/encl.c | 2 +- drivers/comedi/comedi_fops.c | 2 +- drivers/gpu/drm/i915/i915_mm.c | 4 ++-- drivers/infiniband/core/uverbs_main.c | 6 +++--- drivers/misc/sgi-gru/grumain.c | 2 +- include/linux/mm.h | 2 +- mm/memory.c | 16 +++++++--------- 7 files changed, 16 insertions(+), 18 deletions(-)arch/x86/kernel/cpu/sgx/encl.c +1
-1@@ -1220,7 +1220,7 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr) ret =sgx_encl_find (encl_mm->mm, addr, &vma);if (!ret && encl == vma->vm_private_data) -zap_vma_ptes (vma, addr,PAGE_SIZE ); +zap_special_vma_range (vma, addr,PAGE_SIZE );mmap_read_unlock (encl_mm->mm);drivers/comedi/comedi_fops.c +1
-1@@ -2588,7 +2588,7 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)* remap_pfn_range() because we call remap_pfn_range() in a loop. */ if (retval) -zap_vma_ptes (vma, vma->vm_start, size); +zap_special_vma_range (vma, vma->vm_start, size);#endif if (retval ==0 ) {drivers/gpu/drm/i915/i915_mm.c +2
-2@@ -108,7 +108,7 @@ int remap_io_mapping(struct vm_area_struct *vma, err =apply_to_page_range (r.mm, addr, size, remap_pfn, &r);if (unlikely (err)) { -zap_vma_ptes (vma, addr, (r.pfn - pfn) <<PAGE_SHIFT ); +zap_special_vma_range (vma, addr, (r.pfn - pfn) <<PAGE_SHIFT );return err; }@@ -156,7 +156,7 @@ int remap_io_sg(struct vm_area_struct *vma, err =apply_to_page_range (r.mm, addr, size, remap_sg, &r);if (unlikely (err)) { -zap_vma_ptes (vma, addr, r.pfn <<PAGE_SHIFT ); +zap_special_vma_range (vma, addr, r.pfn <<PAGE_SHIFT );return err; }drivers/infiniband/core/uverbs_main.c +3
-3@@ -756,7 +756,7 @@ static void rdma_umap_open(struct vm_area_struct *vma)* point, so zap it. */ vma->vm_private_data =NULL ; -zap_vma_ptes (vma, vma->vm_start, vma->vm_end - vma->vm_start); +zap_special_vma_range (vma, vma->vm_start, vma->vm_end - vma->vm_start); }static void rdma_umap_close (struct vm_area_struct *vma)@@ -782,7 +782,7 @@ static void rdma_umap_close(struct vm_area_struct *vma) }/* -* Once the zap_vma_ptes has been called touches to the VMA will come here and +* Once the zap_special_vma_range has been called touches to the VMA will come here and * we return a dummy writable zero page for all the pfns. */ static vm_fault_t rdma_umap_fault (struct vm_fault *vmf)@@ -878,7 +878,7 @@ void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)continue ;list_del_init (&priv->list); -zap_vma_ptes (vma, vma->vm_start, +zap_special_vma_range (vma, vma->vm_start, vma->vm_end - vma->vm_start);if (priv->entry) {drivers/misc/sgi-gru/grumain.c +1
-1@@ -542,7 +542,7 @@ void gru_unload_context(struct gru_thread_state *gts, int savestate)int ctxnum = gts->ts_ctxnum;if (!is_kernel_context (gts)) -zap_vma_ptes (gts->ts_vma,UGRUADDR (gts),GRU_GSEG_PAGESIZE ); +zap_special_vma_range (gts->ts_vma,UGRUADDR (gts),GRU_GSEG_PAGESIZE ); cch =get_cch (gru->gs_gru_base_vaddr, ctxnum); gru_dbg(grudev, "gts %p, cbrmap 0x%lx, dsrmap 0x%lx\n",include/linux/mm.h +1
-1@@ -2833,7 +2833,7 @@ struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,struct page *vm_normal_page_pud (struct vm_area_struct *vma,unsigned long addr,pud_t pud); -void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address, +void zap_special_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size);void zap_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size);mm/memory.c +7
-10@@ -2233,17 +2233,15 @@ void zap_vma_range(struct vm_area_struct *vma, unsigned long address, }/** -* zap_vma_ptes - remove ptes mapping the vma -* @vma: vm_area_struct holding ptes to be zapped -* @address: starting address of pages to zap +* zap_special_vma_range - zap all page table entries in a special vma range +* @vma: the vma covering the range to zap +* @address: starting address of the range to zap * @size: number of bytes to zap * -* This function only unmaps ptes assigned to VM_PFNMAP vmas. -* -* The entire address range must be fully contained within the vma. -* +* This function does nothing when the provided address range is not fully +* contained in @vma, or when the @vma is not VM_PFNMAP. */ -void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address, +void zap_special_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size) { if (!range_in_vma(vma, address, address + size) ||@@ -2252,7 +2250,7 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,zap_vma_range (vma, address, size); } -EXPORT_SYMBOL_GPL (zap_vma_ptes); +EXPORT_SYMBOL_GPL (zap_special_vma_range);static pmd_t *walk_to_pmd (struct mm_struct *mm,unsigned long addr) { --2.43.0
Re: [PATCH v1 16/16] mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
In reply to David Hildenbrand (Arm)Quoted 7 lines · David Hildenbrand (Arm)
There is demand for also zapping page table entries by drivers in
VM_MIXEDMAPVMAs[1].Nothing really speaks against supporting
VM_MIXEDMAPfor driver use. We just don't want arbitrary drivers to zap in ordinary (non-special) VMAs.
Dude, you have to start your indices with 0 what kind of programmer are you? ;)
Quoted 2 lines
Signed-off-by David Hildenbrand (Arm) <[email protected]>
Agreed that there's no reason not to allow this, so LGTM and:
Thanks for this series overall, very nice cleanup and I was frustra^Whappy to see that there was nothing major or really of note to comment on here, a rare occurrence in review :)
Quoted 25 lines
--- mm/memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/memory.c +2
-3@@ -2239,13 +2239,13 @@ void zap_vma_range(struct vm_area_struct *vma, unsigned long address,* @size: number of bytes to zap * * This function does nothing when the provided address range is not fully -* contained in @vma, or when the @vma is not VM_PFNMAP. +* contained in @vma, or when the @vma is not VM_PFNMAP or VM_MIXEDMAP. */ void zap_special_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size) {if (!range_in_vma (vma, address, address + size) || - !(vma->vm_flags &VM_PFNMAP )) + !(vma->vm_flags & (VM_PFNMAP |VM_MIXEDMAP )))return ;zap_vma_range (vma, address, size); --
Quoted 2 lines
2.43.0
Cheers, Lorenzo
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 11/16] mm/memory: inline unmap_page_range() into __zap_vma_range()
In reply to Lorenzo Stoakes (Oracle)Quoted 46 lines · Lorenzo Stoakes (Oracle)
Quoted 6 lines · David Hildenbrand (Arm)
Let's inline it into the single caller to reduce the number of confusing unmap/zap helpers.
Get rid of the unnecessary
BUG_ON().Signed-off-by David Hildenbrand (Arm) <[email protected]>Small nits below, but overall LGTM so:
Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>Quoted 34 lines
--- mm/memory.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)mm/memory.c +0
-19@@ -2056,25 +2056,6 @@ static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,return addr; } -static void unmap_page_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details) -{ -pgd_t *pgd; -unsigned long next; - -BUG_ON (addr >= end); -tlb_start_vma (tlb, vma); - pgd =pgd_offset (vma->vm_mm, addr); -do { - next =pgd_addr_end (addr, end); -if (pgd_none_or_clear_bad (pgd)) -continue ; - next =zap_p4d_range (tlb, vma, pgd, addr, next, details); - }while (pgd++, addr = next, addr != end); -tlb_end_vma (tlb, vma); -} -static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start,unsigned long end,struct zap_details *details)
Quoted 9 lines
mm/memory.c static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2084
Quoted 6 lines
mm/memory.c +1
-1@@ -2100,7 +2081,18 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,return ;__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); }else { -unmap_page_range (tlb, vma, start, end, details); +unsigned long next, cur = start;VERY nitty, but generally current is abbreviated to curr not cur.
The following might not be fully expressive (but simple to grep):
$ git grep "cur_" | wc -l 24261 $ git grep "curr_" | wc -l 9288
Tells me that "cur" is rather common.
Quoted 1 line
It's not a big deal, but why not addr?
Good question, I can just do that. :)
Signature · 3 lines
Cheers, David
Lorenzo Stoakes (Oracle)
maintainerRe: [PATCH v1 11/16] mm/memory: inline unmap_page_range() into __zap_vma_range()
Quoted 57 lines · David Hildenbrand (Arm)
Quoted 46 lines · Lorenzo Stoakes (Oracle)
Quoted 6 lines · David Hildenbrand (Arm)
Let's inline it into the single caller to reduce the number of confusing unmap/zap helpers.
Get rid of the unnecessary
BUG_ON().Signed-off-by David Hildenbrand (Arm) <[email protected]>Small nits below, but overall LGTM so:
Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>Quoted 34 lines
--- mm/memory.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)mm/memory.c +0
-19@@ -2056,25 +2056,6 @@ static inline unsigned long zap_p4d_range(struct mmu_gather *tlb,return addr; } -static void unmap_page_range (struct mmu_gather *tlb,struct vm_area_struct *vma, -unsigned long addr,unsigned long end, -struct zap_details *details) -{ -pgd_t *pgd; -unsigned long next; - -BUG_ON (addr >= end); -tlb_start_vma (tlb, vma); - pgd =pgd_offset (vma->vm_mm, addr); -do { - next =pgd_addr_end (addr, end); -if (pgd_none_or_clear_bad (pgd)) -continue ; - next =zap_p4d_range (tlb, vma, pgd, addr, next, details); - }while (pgd++, addr = next, addr != end); -tlb_end_vma (tlb, vma); -} -static void __zap_vma_range (struct mmu_gather *tlb,struct vm_area_struct *vma,unsigned long start,unsigned long end,struct zap_details *details)Quoted 9 lines
mm/memory.c static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2084
Quoted 6 lines
mm/memory.c +1
-1@@ -2100,7 +2081,18 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,return ;__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); }else { -unmap_page_range (tlb, vma, start, end, details); +unsigned long next, cur = start;VERY nitty, but generally current is abbreviated to curr not cur.
Quoted 6 lines
The following might not be fully expressive (but simple to grep):
$ git grep "cur_" | wc -l 24261 $ git grep "curr_" | wc -l 9288
Lol hmm...
Quoted 6 lines
Tells me that "cur" is rather common.
Quoted 1 line
It's not a big deal, but why not addr?
Good question, I can just do that. :)
Thanks!
Quoted 5 lines
--
Cheers,David
Cheers, Lorenzo
Re: [PATCH v1 16/16] mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
In reply to David Hildenbrand (Arm)Quoted 7 lines · David Hildenbrand (Arm)
There is demand for also zapping page table entries by drivers in
VM_MIXEDMAPVMAs[1].Nothing really speaks against supporting
VM_MIXEDMAPfor driver use. We just don't want arbitrary drivers to zap in ordinary (non-special) VMAs.
Are we sure about this?
This whole function seems like a hack to support drivers that are not using an address_space.
I say that as one of the five driver authors who have made this mistake.
The locking to safely use this function is really hard to do properly, IDK if binder can shift to use address_space ??
Jason
Re: [PATCH v1 02/16] mm/memory: remove "zap_details" parameter from zap_page_range_single()
In reply to David Hildenbrand (Arm)Quoted 66 lines · David Hildenbrand (Arm)
Nobody except
memory.cshould really set that parameter to non-NULL. So let's just drop it and makeunmap_mapping_range_vma()usezap_page_range_single_batched()instead.Signed-off-by David Hildenbrand (Arm) <[email protected]>--- arch/s390/mm/gmap_helpers.c | 2 +- drivers/android/binder_alloc.c | 2 +- include/linux/mm.h | 5 ++--- kernel/bpf/arena.c | 3 +-- kernel/events/core.c | 2 +- mm/madvise.c | 3 +-- mm/memory.c | 16 ++++++++++------ net/ipv4/tcp.c | 5 ++--- rust/kernel/mm/virt.rs | 2 +- 9 files changed, 20 insertions(+), 20 deletions(-)arch/s390/mm/gmap_helpers.c +1
-1@@ -89,7 +89,7 @@ void gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned loif (!vma)return ;if (!is_vm_hugetlb_page (vma)) -zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr,NULL ); +zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr); vmaddr = vma->vm_end; } }drivers/android/binder_alloc.c +1
-1@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ,NULL ); +zap_page_range_single (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index); }include/linux/mm.h +2
-3@@ -2835,11 +2835,10 @@ struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr,void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address,unsigned long size);void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, -unsigned long size,struct zap_details *details); +unsigned long size);static inline void zap_vma_pages (struct vm_area_struct *vma) { -zap_page_range_single (vma, vma->vm_start, - vma->vm_end - vma->vm_start,NULL ); +zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); }struct mmu_notifier_range ;kernel/bpf/arena.c +0
-0
kernel/bpf/arena.c static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt) · line 660
Quoted 7 lines
kernel/bpf/arena.c +1
-2@@ -656,8 +656,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt)guard (mutex)(&arena->lock);/* iterate link list under lock */ list_for_each_entry (vml, &arena->vma_list, head) -zap_page_range_single (vml->vma, uaddr, -PAGE_SIZE * page_cnt,NULL ); +zap_page_range_single (vml->vma, uaddr,PAGE_SIZE * page_cnt); }
Re: [PATCH v1 14/16] mm: rename zap_page_range_single() to zap_vma_range()
In reply to David Hildenbrand (Arm)Quoted 93 lines · David Hildenbrand (Arm)
Let's rename it to make it better match our new naming scheme.
While at it, polish the kerneldoc.
Signed-off-by David Hildenbrand (Arm) <[email protected]>--- arch/s390/mm/gmap_helpers.c | 2 +- drivers/android/binder/page_range.rs | 4 ++-- drivers/android/binder_alloc.c | 2 +- include/linux/mm.h | 4 ++-- kernel/bpf/arena.c | 2 +- kernel/events/core.c | 2 +- mm/madvise.c | 4 ++-- mm/memory.c | 14 +++++++------- net/ipv4/tcp.c | 6 +++--- rust/kernel/mm/virt.rs | 4 ++-- 10 files changed, 22 insertions(+), 22 deletions(-)arch/s390/mm/gmap_helpers.c +1
-1@@ -89,7 +89,7 @@ void gmap_helper_discard(struct mm_struct *mm, unsigned long vmaddr, unsigned loif (!vma)return ;if (!is_vm_hugetlb_page (vma)) -zap_page_range_single (vma, vmaddr,min (end, vma->vm_end) - vmaddr); +zap_vma_range (vma, vmaddr,min (end, vma->vm_end) - vmaddr); vmaddr = vma->vm_end; } }drivers/android/binder/page_range.rs +2
-2@@ -130,7 +130,7 @@ pub(crate) struct ShrinkablePageRange { pid: Pid,/// The mm for the relevant process. mm: ARef<Mm>, -/// Used to synchronize calls to `vm_insert_page` and `zap_page_range_single`. +/// Used to synchronize calls to `vm_insert_page` and `zap_vma_range`. #[pin] mm_lock: Mutex<()>,/// Spinlock protecting changes to pages. @@ -719,7 +719,7 @@ fn drop(self: Pin<&mut Self>) {if let Some (vma) = mmap_read.vma_lookup (vma_addr) {let user_page_addr = vma_addr + (page_index <<PAGE_SHIFT ); - vma.zap_page_range_single (user_page_addr,PAGE_SIZE ); + vma.zap_vma_range (user_page_addr,PAGE_SIZE ); }drop (mmap_read);drivers/android/binder_alloc.c +1
-1@@ -1185,7 +1185,7 @@ enum lru_status binder_alloc_free_page(struct list_head *item,if (vma) {trace_binder_unmap_user_start (alloc, index); -zap_page_range_single (vma, page_addr,PAGE_SIZE ); +zap_vma_range (vma, page_addr,PAGE_SIZE );trace_binder_unmap_user_end (alloc, index); }include/linux/mm.h +2
-2@@ -2835,7 +2835,7 @@ struct page *vm_normal_page_pud(struct vm_area_struct *vma, unsigned long addr,void zap_vma_ptes (struct vm_area_struct *vma,unsigned long address,unsigned long size); -void zap_page_range_single (struct vm_area_struct *vma,unsigned long address, +void zap_vma_range (struct vm_area_struct *vma,unsigned long address,unsigned long size);/** * zap_vma - zap all page table entries in a vma @@ -2843,7 +2843,7 @@ void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,*/ static inline void zap_vma (struct vm_area_struct *vma) { -zap_page_range_single (vma, vma->vm_start, vma->vm_end - vma->vm_start); +zap_vma_range (vma, vma->vm_start, vma->vm_end - vma->vm_start); }struct mmu_notifier_range ;kernel/bpf/arena.c +0
-0
kernel/bpf/arena.c static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt) · line 660
Quoted 6 lines
kernel/bpf/arena.c +1
-1@@ -656,7 +656,7 @@ static void zap_pages(struct bpf_arena *arena, long uaddr, long page_cnt)guard (mutex)(&arena->lock);/* iterate link list under lock */ list_for_each_entry (vml, &arena->vma_list, head) -zap_page_range_single (vml->vma, uaddr,PAGE_SIZE * page_cnt); +zap_vma_range (vml->vma, uaddr,PAGE_SIZE * page_cnt); }
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 04/16] mm/memory: simplify calculation in unmap_mapping_range_tree()
In reply to Lorenzo Stoakes (Oracle)Quoted 8 lines
mm/memory.c static inline void unmap_mapping_range_tree(struct rb_root_cached *root, · line 4235
Quoted 6 lines
mm/memory.c +2
-2- start = ((zba - vba) <<PAGE_SHIFT ) + vma->vm_start; - size = (zea - zba +1 ) <<PAGE_SHIFT ; +const pgoff_t start_idx =max (first_index, vma->vm_pgoff); +const pgoff_t end_idx =min (last_index,vma_last_pgoff (vma)) +1 ;I guess since 'end' is by-convention the +1 of last this is fine
Exactly, the calculation itself is not changed.
The +1 was previously hiding inside the "(zea - zba + 1)"
Thanks!
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 10/16] mm/memory: use __zap_vma_range() in zap_vma_for_reaping()
In reply to Lorenzo Stoakes (Oracle)Quoted 18 lines · Lorenzo Stoakes (Oracle)
Quoted 5 lines · David Hildenbrand (Arm)
Let's call
__zap_vma_range()instead ofunmap_page_range()to prepare for further cleanups.To keep the existing behavior, whereby we do not call
uprobe_munmap()which could block, add a new "reaping" member tozap_detailsand use it.I am always in favour of making further use of helper structs :)
Quoted 5 lines
Likely we should handle the possible blocking in
uprobe_munmap()differently, but for now keep it unchanged.Signed-off-by David Hildenbrand (Arm) <[email protected]>OK this looks like it's doing the equivalent of what was there before, so:
Reviewed-by Lorenzo Stoakes (Oracle) <[email protected]>
[...]
Quoted 10 lines
Quoted 10 lines
mm/memory.c +3
-0@@ -2111,11 +2115,12 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,*/ int zap_vma_for_reaping (struct vm_area_struct *vma) { +struct zap_details details = { + .reaping = true, + };struct mmu_notifier_range range;struct mmu_gather tlb;
Quoted 4 lines
mm/memory.c static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2118
Quoted 2 lines
mm/memory.c +0
-2-VM_WARN_ON_ONCE (is_vm_hugetlb_page (vma)); -I guess because you've moved this safety check into
__zap_vma_range()?
Yes exactly, considered the change too trivial to mention it in the patch description.
Thanks!
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 15/16] mm: rename zap_vma_ptes() to zap_special_vma_range()
In reply to Lorenzo Stoakes (Oracle)Quoted 11 lines · Lorenzo Stoakes (Oracle)
Quoted 8 lines · David Hildenbrand (Arm)
zap_vma_ptes()is the only zapping function we export to modules.It's essentially a wrapper around
zap_vma_range(), however, with some safety checks:
* That the passed range fits fully into the VMA
* That it's only used forVM_PFNMAPWe might want to support
VM_MIXEDMAPsoon as well, so use theI think you should say we _are_ going to since patch 16/16 does that :)
Heh, had that patch separate first before I decided to support VM_MIXEDMAP in the same series.
@Andrew, can you fix that up? Thanks!
"We will add support for VM_MIXEDMAP next, so ..."
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 09/16] mm/memory: convert details->even_cows into details->skip_cows
In reply to Lorenzo Stoakes (Oracle)Quoted 13 lines
mm/memory.c copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) · line 1554
Quoted 9 lines
mm/madvise.c +0
-0zap_page_range_single_batched(mm/memory.c +0
-0@@ -1554,11 +1554,13 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)static inline bool should_zap_cows (struct zap_details *details)Not sure if you fix up later, but we should probably change this function to
should_skip_cows()to keep everything consistent, otherwise this is a bit weird and confusing.
should_skip_cows() is a bit misleading on its own IMHO, as we skip the "zap" context.
Would have to be something like "should_skip_cows_when_zapping()", and I am not sure if that's really worth it.
So I think we can leave it as is.
Thanks!
Quoted 37 lines
Quoted 37 lines
mm/memory.c +5
-5{/* By default, zap all pages */ -if (!details || details->reclaim_pt) +if (!details)return true; +VM_WARN_ON_ONCE (details->skip_cows && details->reclaim_pt); +/* Or, we zap COWed pages only if the caller wants to */ -return details->even_cows; +return !details->skip_cows; }/* Decides whether we should zap this folio with the folio pointer specified */ @@ -2149,8 +2151,6 @@ void unmap_vmas(struct mmu_gather *tlb, struct unmap_desc *unmap)struct mmu_notifier_range range;struct zap_details details = { .zap_flags =ZAP_FLAG_DROP_MARKER |ZAP_FLAG_UNMAP , -/* Careful - we need to zap private pages too! */ - .even_cows = true, }; vma = unmap->first;@@ -4282,7 +4282,7 @@ void unmap_mapping_folio(struct folio *folio) first_index = folio->index; last_index =folio_next_index (folio) -1 ; - details.even_cows = false; + details.skip_cows = true; details.single_folio = folio; details.zap_flags =ZAP_FLAG_DROP_MARKER ;@@ -4312,7 +4312,7 @@ void unmap_mapping_pages(struct address_space *mapping, pgoff_t start,pgoff_t first_index = start;pgoff_t last_index = start + nr -1 ;
Quoted 5 lines
mm/memory.c void unmap_mapping_pages(struct address_space *mapping, pgoff_t start, · line 4315
Quoted 2 lines
mm/memory.c +1
-1- details.even_cows = even_cows; + details.skip_cows = !even_cows;Not sure if you clean up later, but seems sensible to cascade the change into the local boolean here.
There are too many unmap_mapping_range() users for me to want to change the external interface :)
What I was thinking is that probably there should be two independent user-facing functions, instead of having this magical bool passed around
Because I am sure, most users don't really know whether to set it to true or false ...
But that's something for another cleanup. (I left the whole unmap_mapping_pages() interface etc alone in this series)
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 16/16] mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
In reply to Jason GunthorpeQuoted 10 lines · Jason Gunthorpe
Quoted 7 lines · David Hildenbrand (Arm)
There is demand for also zapping page table entries by drivers in
VM_MIXEDMAPVMAs[1].Nothing really speaks against supporting
VM_MIXEDMAPfor driver use. We just don't want arbitrary drivers to zap in ordinary (non-special) VMAs.Are we sure about this?
Yes, I don't think relaxing this for drivers to use it on VM_MIXEDMAP is a problem.
Quoted 3 lines
This whole function seems like a hack to support drivers that are not using an
address_space.
I assume, then using unmap_mapping_folio()/unmap_mapping_pages()/unmap_mapping_range() instead.
Quoted 6 lines
I say that as one of the five driver authors who have made this mistake.
The locking to safely use this function is really hard to do properly, IDK if binder can shift to use
address_space??
I cannot really tell.
Skimming over the code, it looks like it really always handles "single VMA" stuff ("Since a binder_alloc can only be mapped once, we ensure the vma corresponds to this mapping by checking whether the binder_alloc is still mapped"), which makes the locking rather trivial.
It does seem to mostly allocate/free pages in a single VMA, where I think the existing usage of zap_vma_range() makes sense.
So I'm not sure if using address_space would really be an improvement there.
Having that said, maybe binder folks can be motivated to look into that. But I would consider that future work.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v1 11/16] mm/memory: inline unmap_page_range() into __zap_vma_range()
In reply to Lorenzo Stoakes (Oracle)Quoted 8 lines
Quoted 6 lines
Tells me that "cur" is rather common.
Quoted 1 line
It's not a big deal, but why not addr?
Good question, I can just do that. :)
Thanks!
@Andrew, the following fixup on top
From 8470e94c57b89819753c8f31e23320f33c14cd58 Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Wed, 11 Mar 2026 10:19:00 +0100
Subject: [PATCH] fixup: mm/memory: inline unmap_page_range() into
__zap_vma_range()
Let's call the local variable simply "addr".
--- mm/memory.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
mm/memory.c +5 -5
@@ -2081,17 +2081,17 @@ static void __zap_vma_range(struct mmu_gather *tlb, struct vm_area_struct *vma,return ;__unmap_hugepage_range (tlb, vma, start, end,NULL , zap_flags); }else { -unsigned long next, cur = start; +unsigned long next, addr = start;pgd_t *pgd;tlb_start_vma (tlb, vma); - pgd =pgd_offset (vma->vm_mm, cur); + pgd =pgd_offset (vma->vm_mm, addr);do { - next =pgd_addr_end (cur, end); + next =pgd_addr_end (addr, end);if (pgd_none_or_clear_bad (pgd))continue ; - next =zap_p4d_range (tlb, vma, pgd, cur, next, details); - }while (pgd++, cur = next, cur != end); + next =zap_p4d_range (tlb, vma, pgd, addr, next, details); + }while (pgd++, addr = next, addr != end);tlb_end_vma (tlb, vma); } }
Signature · 7 lines
2.43.0 -- Cheers, David
Alice Ryhl
maintainerRe: [PATCH v1 16/16] mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
In reply to David Hildenbrand (Arm)Quoted 35 lines · David Hildenbrand (Arm)
Quoted 10 lines · Jason Gunthorpe
Quoted 7 lines · David Hildenbrand (Arm)
There is demand for also zapping page table entries by drivers in
VM_MIXEDMAPVMAs[1].Nothing really speaks against supporting
VM_MIXEDMAPfor driver use. We just don't want arbitrary drivers to zap in ordinary (non-special) VMAs.Are we sure about this?
Yes, I don't think relaxing this for drivers to use it on
VM_MIXEDMAPis a problem.Quoted 3 lines
This whole function seems like a hack to support drivers that are not using an
address_space.I assume, then using unmap_mapping_folio()/unmap_mapping_pages()/unmap_mapping_range() instead.
Quoted 6 lines
I say that as one of the five driver authors who have made this mistake.
The locking to safely use this function is really hard to do properly, IDK if binder can shift to use
address_space??I cannot really tell.
Skimming over the code, it looks like it really always handles "single VMA" stuff ("Since a
binder_alloccan only be mapped once, we ensure the vma corresponds to this mapping by checking whether thebinder_allocis still mapped"), which makes the locking rather trivial.
Quoted 7 lines
It does seem to mostly allocate/free pages in a single VMA, where I think the existing usage of
zap_vma_range()makes sense.So I'm not sure if using
address_spacewould really be an improvement there.Having that said, maybe binder folks can be motivated to look into that. But I would consider that future work.
It doesn't really make sense to have multiple binder VMAs. What happens with Rust Binder is that process A is receiving transactions and has the VMA mapped once.
* Process B sends a transaction to process A, and the ioctl (running in process B) will memcpy the message to A directly into the pages of A's VMA.
* Then, B wakes up A, which causes A to return from the receive ioctl.
* The return value of the receive ioctl is a pointer, which points somewhere inside A's VMA to the location containing the message from B.
* Process A will deref the pointer to read the message from B.
* Once Process A is done handling the transaction, it invokes another ioctl to tell the kernel that it is done with this transaction, that is, it is not safe for the kernel to reuse that subset of the VMA for new incoming transactions.
When Binder returns from its ioctl and gives you a pointer, it needs to know where the VMA is mapped, because otherwise it can't really give you a pointer into the VMA.
It's generally not safe for userspace to touch its Binder VMA unless it has been told that there is a message there. Pages that do not contain any messages may be entirely missing, and trying to read them leads to segfault. (Though such pages may also be present if there was previously a message in the page. The unused pages are kept around to reuse them for future messages, unless there is memory pressure.)
Alice
Re: [PATCH v1 16/16] mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
Quoted 3 lines · Alice Ryhl
It doesn't really make sense to have multiple binder VMAs. What happens with Rust Binder is that process A is receiving transactions and has the VMA mapped once.
IIRC the problem is the kernel doesn't guarentee singleton VMAs, userspace can always clone them with fork or something. Did binder solve this somehow?
Since you can't assume there is only one VMA the locking becomes a mess to cover all the cases where userspace can trigger a VMA clone.
address space deals with this internally.
Thus, zap_special_vma_range() is extremely hard to use.
Jason
Alice Ryhl
maintainerRe: [PATCH v1 16/16] mm/memory: support VM_MIXEDMAP in zap_special_vma_range()
Quoted 8 lines · Jason Gunthorpe
Quoted 3 lines · Alice Ryhl
It doesn't really make sense to have multiple binder VMAs. What happens with Rust Binder is that process A is receiving transactions and has the VMA mapped once.
IIRC the problem is the kernel doesn't guarentee singleton VMAs, userspace can always clone them with fork or something. Did binder solve this somehow?
The Binder VMA is DONTCOPY, so it will not be present after fork.
Quoted 6 lines
Since you can't assume there is only one VMA the locking becomes a mess to cover all the cases where userspace can trigger a VMA clone.
address space deals with this internally.
Thus,
zap_special_vma_range()is extremely hard to use.
I mean, the hard part about the locking is keeping them in sync. Binder just doesn't do that. Only the original VMA gets pages inserted or zapped. If you create a second VMA, you just get a useless read-only VMA that you can't do anything with.
Alice