mm: rewrite pfnmap tracking and remove VM_PAT
Subsystems (13)
cover mm: rewrite pfnmap tracking and remove VM_PAT
On top of mm-unstable.
VM_PAT annoyed me too much and wasted too much of my time, let's clean PAT handling up and remove VM_PAT.
This should sort out various issues with VM_PAT we discovered recently, and will hopefully make the whole code more stable and easier to maintain.
In essence: we stop letting PAT mode mess with VMAs and instead lift what to track/untrack to the MM core. We remember per VMA which pfn range we tracked in a new struct we attach to a VMA (we have space without exceeding 192 bytes), use a kref to share it among VMAs during split/mremap/fork, and automatically untrack once the kref drops to 0.
This implies that we'll keep tracking a full pfn range even after partially unmapping it, until fully unmapping it; but as that case was mostly broken before, this at least makes it work in a way that is least intrusive to VMA handling.
Shrinking with mremap() used to work in a hacky way, now we'll similarly keep the original pfn range tacked even after this form of partial unmap. Does anybody care about that? Unlikely. If we run into issues, we could likely handled that (adjust the tracking) when our kref drops to 1 while freeing a VMA. But it adds more complexity, so avoid that for now.
Briefly tested
There will be some clash with [1], but nothing that cannot be sorted out easily by moving the functions added to kernel/fork.c to wherever the vma bits will live.
Briefly tested with some basic /dev/mem test I crafted. I want to convert them to selftests, but that might or might not require a bit of more work (e.g., /dev/mem accessibility).
[1] lkml.kernel.org/r/[email protected]
David Hildenbrand (11):
x86/mm/pat: factor out setting cachemode into pgprot_set_cachemode()
mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot() x86/mm/pat: introduce pfnmap_track() and pfnmap_untrack() mm/memremap: convert to pfnmap_track() + pfnmap_untrack()
mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack() x86/mm/pat: remove old pfnmap tracking interface
mm: remove VM_PAT
x86/mm/pat: remove strict_prot parameter from reserve_pfn_range() x86/mm/pat: remove MEMTYPE_*_MATCH
drm/i915: track_pfn() -> "pfnmap tracking"
mm/io-mapping: track_pfn() -> "pfnmap tracking"
arch/x86/mm/pat/memtype.c | 194 ++++------------------------- arch/x86/mm/pat/memtype_interval.c | 44 +------ drivers/gpu/drm/i915/i915_mm.c | 4 +- include/linux/mm.h | 4 +- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++ include/linux/pgtable.h | 101 ++++++--------- include/trace/events/mmflags.h | 4 +- kernel/fork.c | 54 +++++++- mm/huge_memory.c | 7 +- mm/io-mapping.c | 2 +- mm/memory.c | 85 ++++++++++--- mm/memremap.c | 8 +- mm/mremap.c | 4 - 14 files changed, 212 insertions(+), 312 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/11 x86/mm/pat: factor out setting cachemode into pgprot_set_cachemode()
Let's factor it out to make the code easier to grasp.
Use it also in pgprot_writecombine()/pgprot_writethrough() where clearing the old cachemode might not be required, but given that we are already doing a function call, no need to care about this micro-optimization.
--- arch/x86/mm/pat/memtype.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-)
arch/x86/mm/pat/memtype.c +15 -18
@@ -800,6 +800,12 @@ static inline int range_is_allowed(unsigned long pfn, unsigned long size) }#endif /* CONFIG_STRICT_DEVMEM */ +static inline void pgprot_set_cachemode (pgprot_t *prot,enum page_cache_mode pcm) +{ + *prot =__pgprot ((pgprot_val (*prot) & ~_PAGE_CACHE_MASK) | +cachemode2protval (pcm)); +} +int phys_mem_access_prot_allowed (struct file *file,unsigned long pfn,unsigned long size,pgprot_t *vma_prot) {
@@ -811,8 +817,7 @@ int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,if (file->f_flags &O_DSYNC ) pcm = _PAGE_CACHE_MODE_UC_MINUS; - *vma_prot =__pgprot ((pgprot_val (*vma_prot) & ~_PAGE_CACHE_MASK) | -cachemode2protval (pcm)); +pgprot_set_cachemode (vma_prot, pcm);return 1 ; }
@@ -880,9 +885,7 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot, (unsigned long long )paddr, (unsigned long long )(paddr + size -1 ), cattr_name(pcm)); - *vma_prot =__pgprot ((pgprot_val (*vma_prot) & - (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); +pgprot_set_cachemode (vma_prot, pcm); }return 0 ; }
@@ -907,9 +910,7 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,* We allow returning different type than the one requested in * non strict case. */ - *vma_prot =__pgprot ((pgprot_val (*vma_prot) & - (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); +pgprot_set_cachemode (vma_prot, pcm); }if (memtype_kernel_map_sync (paddr, size, pcm) <0 ) {
@@ -1060,9 +1061,7 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return -EINVAL ; } - *prot =__pgprot ((pgprot_val (*prot) & (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); - +pgprot_set_cachemode (prot, pcm);return 0 ; }
@@ -1073,10 +1072,8 @@ void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, pfn_t pfn)if (!pat_enabled ())return ; -/* Set prot based on lookup */ pcm =lookup_memtype (pfn_t_to_phys (pfn)); - *prot =__pgprot ((pgprot_val (*prot) & (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); +pgprot_set_cachemode (prot, pcm); }/*
@@ -1115,15 +1112,15 @@ void untrack_pfn_clear(struct vm_area_struct *vma)pgprot_t pgprot_writecombine (pgprot_t prot) { -return __pgprot (pgprot_val (prot) | -cachemode2protval (_PAGE_CACHE_MODE_WC)); +pgprot_set_cachemode (&prot, _PAGE_CACHE_MODE_WC); +return prot; }EXPORT_SYMBOL_GPL (pgprot_writecombine);pgprot_t pgprot_writethrough (pgprot_t prot) { -return __pgprot (pgprot_val (prot) | -cachemode2protval (_PAGE_CACHE_MODE_WT)); +pgprot_set_cachemode (&prot, _PAGE_CACHE_MODE_WT); +return prot; }EXPORT_SYMBOL_GPL (pgprot_writethrough);
Signature · 1 line
2.49.0
PATCH 2/11 mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
In reply to David Hildenbrand... by factoring it out from track_pfn_remap().
For PMDs/PUDs, actually check the full range, and trigger a fallback if we run into this "different memory types / cachemodes" scenario.
Add some documentation.
Will checking each page result in undesired overhead? We'll have to learn. Not checking each page looks wrong, though. Maybe we could optimize the lookup internally.
--- arch/x86/mm/pat/memtype.c | 24 ++++++++---------------- include/linux/pgtable.h | 28 ++++++++++++++++++++-------- mm/huge_memory.c | 7 +++++-- mm/memory.c | 4 ++-- 4 files changed, 35 insertions(+), 28 deletions(-)
arch/x86/mm/pat/memtype.c +8 -16
@@ -1031,7 +1031,6 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, unsigned long pfn, unsigned long addr, unsigned long size) { resource_size_t paddr = (resource_size_t)pfn << PAGE_SHIFT; -enum page_cache_mode pcm;/* reserve the whole chunk starting from paddr */ if (!vma || (addr == vma->vm_start
@@ -1044,13 +1043,17 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return ret; } +return pfnmap_sanitize_pgprot (pfn, size, prot); +} + +int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size,pgprot_t *prot) +{ +resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; +enum page_cache_mode pcm; +if (!pat_enabled ())return 0 ; -/* -* For anything smaller than the vma size we set prot based on the -* lookup. -*/ pcm =lookup_memtype (paddr);/* Check memtype for the remaining pages */
@@ -1065,17 +1068,6 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return 0 ; } -void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot,pfn_t pfn) -{ -enum page_cache_mode pcm; - -if (!pat_enabled ()) -return ; - - pcm =lookup_memtype (pfn_t_to_phys (pfn)); -pgprot_set_cachemode (prot, pcm); -} -/* * untrack_pfn is called while unmapping a pfnmap for a region. * untrack can be called for a specific region indicated by pfn and size or
include/linux/pgtable.h +20 -8
@@ -1500,13 +1500,10 @@ static inline int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return 0 ; } -/* -* track_pfn_insert is called when a _new_ single pfn is established -* by vmf_insert_pfn(). -*/ -static inline void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot, -pfn_t pfn) +static inline int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot) { +return 0 ; }/*
@@ -1556,8 +1553,23 @@ static inline void untrack_pfn_clear(struct vm_area_struct *vma)extern int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot,unsigned long pfn,unsigned long addr,unsigned long size); -extern void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot, -pfn_t pfn); + +/** +* pfnmap_sanitize_pgprot - sanitize the pgprot for a pfn range +* @pfn: the start of the pfn range +* @size: the size of the pfn range +* @prot: the pgprot to sanitize +* +* Sanitize the given pgprot for a pfn range, for example, adjusting the +* cachemode. +* +* This function cannot fail for a single page, but can fail for multiple +* pages. +* +* Returns 0 on success and -EINVAL on error. +*/ +int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,
mm/huge_memory.c +5 -2
@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ; + ptl =pmd_lock (vma->vm_mm, vmf->pmd); error =insert_pfn_pmd (vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);
@@ -1577,7 +1579,8 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write)if (addr < vma->vm_start || addr >= vma->vm_end)return VM_FAULT_SIGBUS ; -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ; ptl =pud_lock (vma->vm_mm, vmf->pud);insert_pfn_pud (vma, addr, vmf->pud, pfn, write);
mm/memory.c +2 -2
@@ -2563,7 +2563,7 @@ vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,if (!pfn_modify_allowed (pfn, pgprot))return VM_FAULT_SIGBUS ; -track_pfn_insert (vma, &pgprot,__pfn_to_pfn_t (pfn,PFN_DEV )); +pfnmap_sanitize_pgprot (pfn,PAGE_SIZE , &pgprot);return insert_pfn (vma, addr,__pfn_to_pfn_t (pfn,PFN_DEV ), pgprot, false);
@@ -2626,7 +2626,7 @@ static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,if (addr < vma->vm_start || addr >= vma->vm_end)return VM_FAULT_SIGBUS ; -track_pfn_insert (vma, &pgprot, pfn); +pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot);if (!pfn_modify_allowed (pfn_t_to_pfn (pfn), pgprot))return VM_FAULT_SIGBUS ;
Signature · 1 line
2.49.0
PATCH 3/11 x86/mm/pat: introduce pfnmap_track() and pfnmap_untrack()
In reply to David HildenbrandLet's provide variants of track_pfn_remap() and untrack_pfn() that won't mess with VMAs, to replace the existing interface step-by-step.
Add some documentation.
--- arch/x86/mm/pat/memtype.c | 14 ++++++++++++++ include/linux/pgtable.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
arch/x86/mm/pat/memtype.c +14 -0
@@ -1068,6 +1068,20 @@ int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size, pgprot_t *protreturn 0 ; } +int pfnmap_track (unsigned long pfn,unsigned long size,pgprot_t *prot) +{ +const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; + +return reserve_pfn_range (paddr, size, prot,0 ); +} + +void pfnmap_untrack (unsigned long pfn,unsigned long size) +{ +const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; + +free_pfn_range (paddr, size); +} +/* * untrack_pfn is called while unmapping a pfnmap for a region. * untrack can be called for a specific region indicated by pfn and size or
include/linux/pgtable.h +33 -0
@@ -1506,6 +1506,16 @@ static inline int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size,return 0 ; } +static inline int pfnmap_track (unsigned long pfn,unsigned long size, +pgprot_t *prot) +{ +return 0 ; +} + +static inline void pfnmap_untrack (unsigned long pfn,unsigned long size) +{ +} +/* * track_pfn_copy is called when a VM_PFNMAP VMA is about to get the page * tables copied during copy_page_range(). Will store the pfn to be
@@ -1570,6 +1580,29 @@ extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,*/ int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size,pgprot_t *prot); + +/** +* pfnmap_track - track a pfn range +* @pfn: the start of the pfn range +* @size: the size of the pfn range +* @prot: the pgprot to track +* +* Tracking a pfnmap range involves conditionally reserving a pfn range and +* sanitizing the pgprot -- see pfnmap_sanitize_pgprot(). +* +* Returns 0 on success and -EINVAL on error. +*/ +int pfnmap_track (unsigned long pfn,unsigned long size,pgprot_t *prot); + +/** +* pfnmap_untrack - untrack a pfn range +* @pfn: the start of the pfn range +* @size: the size of the pfn range +* +* Untrack a pfn range previously tracked through pfnmap_track(), for example, +* un-doing any reservation. +*/ +void pfnmap_untrack (unsigned long pfn,unsigned long size);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,
Signature · 1 line
2.49.0
PATCH 4/11 mm/memremap: convert to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandLet's use the new, cleaner interface.
--- mm/memremap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/memremap.c +4 -4
@@ -130,7 +130,7 @@ static void pageunmap_range(struct dev_pagemap *pgmap, int range_id) }mem_hotplug_done (); -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));pgmap_array_delete (range); }
@@ -211,8 +211,8 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (nid <0 ) nid =numa_mem_id (); - error =track_pfn_remap (NULL , ¶ms->pgprot,PHYS_PFN (range->start),0 , -range_len (range)); + error =pfnmap_track (PHYS_PFN (range->start),range_len (range), + ¶ms->pgprot);if (error) goto err_pfn_remap;
@@ -277,7 +277,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (!is_private)kasan_remove_zero_shadow (__va (range->start),range_len (range)); err_kasan: -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range)); err_pfn_remap:pgmap_array_delete (range);return error;
Signature · 1 line
2.49.0
PATCH 5/11 mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandLet's use our new interface. In remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).
Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.
--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)
include/linux/mm_inline.h +2 -0
@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );
include/linux/mm_types.h +11 -0
@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { +struct kref kref; +unsigned long pfn; +unsigned long size; +}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory
@@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif } __randomize_layout;#ifdef CONFIG_NUMA
kernel/fork.c +50 -4
79 lines not on this page · raw message
mm/memory.c +65 -16
127 lines not on this page · raw message
mm/mremap.c +0 -4
@@ -1191,10 +1191,6 @@ static int copy_vma_and_data(struct vma_remap_struct *vrm,if (is_vm_hugetlb_page (vma))clear_vma_resv_huge_pages (vma); -/* Tell pfnmap has moved from this vma */ -if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn_clear (vma); - *new_vma_ptr = new_vma;return err; }
Signature · 1 line
2.49.0
PATCH 6/11 x86/mm/pat: remove old pfnmap tracking interface
In reply to David HildenbrandWe can now get rid of the old interface along with get_pat_info() and follow_phys().
--- arch/x86/mm/pat/memtype.c | 147 -------------------------------------- include/linux/pgtable.h | 66 ----------------- 2 files changed, 213 deletions(-)
arch/x86/mm/pat/memtype.c +0 -147
@@ -933,119 +933,6 @@ static void free_pfn_range(u64 paddr, unsigned long size)memtype_free (paddr, paddr + size); } -static int follow_phys (struct vm_area_struct *vma,unsigned long *prot, -resource_size_t *phys) -{ -struct follow_pfnmap_args args = { .vma = vma, .address = vma->vm_start }; - -if (follow_pfnmap_start (&args)) -return -EINVAL ; - -/* Never return PFNs of anon folios in COW mappings. */ -if (!args.special) { -follow_pfnmap_end (&args); -return -EINVAL ; - } - - *prot =pgprot_val (args.pgprot); - *phys = (resource_size_t )args.pfn <<PAGE_SHIFT ; -follow_pfnmap_end (&args); -return 0 ; -} - -static int get_pat_info (struct vm_area_struct *vma,resource_size_t *paddr, -pgprot_t *pgprot) -{ -unsigned long prot; - -VM_WARN_ON_ONCE (!(vma->vm_flags &VM_PAT )); - -/* -* We need the starting PFN and cachemode used for track_pfn_remap() -* that covered the whole VMA. For most mappings, we can obtain that -* information from the page tables. For COW mappings, we might now -* suddenly have anon folios mapped and follow_phys() will fail. -* -* Fallback to using vma->vm_pgoff, see remap_pfn_range_notrack(), to -* detect the PFN. If we need the cachemode as well, we're out of luck -* for now and have to fail fork(). -*/ -if (!follow_phys (vma, &prot, paddr)) { -if (pgprot) - *pgprot =__pgprot (prot); -return 0 ; - } -if (is_cow_mapping (vma->vm_flags)) { -if (pgprot) -return -EINVAL ; - *paddr = (resource_size_t )vma->vm_pgoff <<PAGE_SHIFT ; -return 0 ; - } -WARN_ON_ONCE (1 ); -return -EINVAL ; -} - -int track_pfn_copy (struct vm_area_struct *dst_vma, -struct vm_area_struct *src_vma,unsigned long *pfn) -{ -const unsigned long vma_size = src_vma->vm_end - src_vma->vm_start; -resource_size_t paddr; -pgprot_t pgprot; -int rc; - -if (!(src_vma->vm_flags &VM_PAT )) -return 0 ; - -/* -* Duplicate the PAT information for the dst VMA based on the src -* VMA. -*/ -if (get_pat_info (src_vma, &paddr, &pgprot)) -return -EINVAL ; - rc =reserve_pfn_range (paddr, vma_size, &pgprot,1 ); -if (rc) -return rc; - -/* Reservation for the destination VMA succeeded. */ -vm_flags_set (dst_vma,VM_PAT ); - *pfn =PHYS_PFN (paddr); -return 0 ; -} - -void untrack_pfn_copy (struct vm_area_struct *dst_vma,unsigned long pfn) -{ -untrack_pfn (dst_vma, pfn, dst_vma->vm_end - dst_vma->vm_start, true); -/* -* Reservation was freed, any copied page tables will get cleaned -* up later, but without getting PAT involved again. -*/ -} - -/* -* prot is passed in as a parameter for the new mapping. If the vma has -* a linear pfn mapping for the entire range, or no vma is provided, -* reserve the entire pfn + size range with single reserve_pfn_range -* call. -*/ -int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot, -unsigned long pfn,unsigned long addr,unsigned long size) -{ -resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; - -/* reserve the whole chunk starting from paddr */ -if (!vma || (addr == vma->vm_start - && size == (vma->vm_end - vma->vm_start))) { -int ret; - - ret =reserve_pfn_range (paddr, size, prot,0 ); -if (ret ==0 && vma) -vm_flags_set (vma,VM_PAT ); -return ret; - } - -return pfnmap_sanitize_pgprot (pfn, size, prot); -} -int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size,pgprot_t *prot) {resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ;
@@ -1082,40 +969,6 @@ void pfnmap_untrack(unsigned long pfn, unsigned long size)free_pfn_range (paddr, size); } -/* -* untrack_pfn is called while unmapping a pfnmap for a region. -* untrack can be called for a specific region indicated by pfn and size or -* can be for the entire vma (in which case pfn, size are zero). -*/ -void untrack_pfn (struct vm_area_struct *vma,unsigned long pfn, -unsigned long size,bool mm_wr_locked) -{ -resource_size_t paddr; - -if (vma && !(vma->vm_flags &VM_PAT )) -return ; - -/* free the chunk starting from pfn or the whole chunk */ - paddr = (resource_size_t )pfn <<PAGE_SHIFT ; -if (!paddr && !size) { -if (get_pat_info (vma, &paddr,NULL )) -return ; - size = vma->vm_end - vma->vm_start; - } -free_pfn_range (paddr, size); -if (vma) { -if (mm_wr_locked) -vm_flags_clear (vma,VM_PAT ); -else -__vm_flags_mod (vma,0 ,VM_PAT ); - } -} - -void untrack_pfn_clear (struct vm_area_struct *vma) -{ -vm_flags_clear (vma,VM_PAT ); -} -pgprot_t pgprot_writecombine (pgprot_t prot) {pgprot_set_cachemode (&prot, _PAGE_CACHE_MODE_WC);
include/linux/pgtable.h +0 -66
85 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH 7/11 mm: remove VM_PAT
In reply to David HildenbrandIt's unused, so let's remove it.
--- include/linux/mm.h | 4 +--- include/trace/events/mmflags.h | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-)
include/linux/mm.h +1 -3
@@ -357,9 +357,7 @@ extern unsigned int kobjsize(const void *objp);# define VM_SHADOW_STACK VM_NONE#endif -#if defined(CONFIG_X86 ) -# define VM_PAT VM_ARCH_1/* PAT reserves whole VMA at once (x86) */ -#elif defined(CONFIG_PPC64 ) +#if defined(CONFIG_PPC64 )# define VM_SAO VM_ARCH_1/* Strong Access Ordering (powerpc) */ #elif defined(CONFIG_PARISC )# define VM_GROWSUP VM_ARCH_1
include/trace/events/mmflags.h +1 -3
@@ -172,9 +172,7 @@ IF_HAVE_PG_ARCH_3(arch_3) __def_pageflag_names \ ) : "none" -#if defined(CONFIG_X86 ) -#define __VM_ARCH_SPECIFIC_1 {VM_PAT, "pat" } -#elif defined(CONFIG_PPC64 ) +#if defined(CONFIG_PPC64 )#define __VM_ARCH_SPECIFIC_1 {VM_SAO, "sao" }#elif defined(CONFIG_PARISC )#define __VM_ARCH_SPECIFIC_1 {VM_GROWSUP, "growsup" }
Signature · 1 line
2.49.0
PATCH 8/11 x86/mm/pat: remove strict_prot parameter from reserve_pfn_range()
In reply to David HildenbrandAlways set to 0, so let's remove it.
--- arch/x86/mm/pat/memtype.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
arch/x86/mm/pat/memtype.c +3 -9
@@ -858,8 +858,7 @@ int memtype_kernel_map_sync(u64 base, unsigned long size,* Reserved non RAM regions only and after successful memtype_reserve, * this func also keeps identity mapping (if any) in sync with this new prot. */ -static int reserve_pfn_range (u64 paddr,unsigned long size,pgprot_t *vma_prot, -int strict_prot) +static int reserve_pfn_range (u64 paddr,unsigned long size,pgprot_t *vma_prot) {int is_ram =0 ;int ret;
@@ -895,8 +894,7 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,return ret;if (pcm != want_pcm) { -if (strict_prot || - !is_new_memtype_allowed (paddr, size, want_pcm, pcm)) { +if (!is_new_memtype_allowed (paddr, size, want_pcm, pcm)) {memtype_free (paddr, paddr + size); pr_err("x86/PAT: %s:%d map pfn expected mapping type %s for [mem %#010Lx-%#010Lx], got %s\n", current->comm, current->pid,
@@ -906,10 +904,6 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot, cattr_name(pcm));return -EINVAL ; } -/* -* We allow returning different type than the one requested in -* non strict case. -*/ pgprot_set_cachemode (vma_prot, pcm); }
@@ -959,7 +953,7 @@ int pfnmap_track(unsigned long pfn, unsigned long size, pgprot_t *prot) {const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; -return reserve_pfn_range (paddr, size, prot,0 ); +return reserve_pfn_range (paddr, size, prot); }void pfnmap_untrack (unsigned long pfn,unsigned long size)
Signature · 1 line
2.49.0
PATCH 9/11 x86/mm/pat: remove MEMTYPE_*_MATCH
In reply to David HildenbrandThe "memramp() shrinking" scenario no longer applies, so let's remove that now-unnecessary handling.
--- arch/x86/mm/pat/memtype_interval.c | 44 ++++-------------------------- 1 file changed, 6 insertions(+), 38 deletions(-)
arch/x86/mm/pat/memtype_interval.c +6 -38
@@ -49,26 +49,15 @@ INTERVAL_TREE_DEFINE(struct memtype, rb, u64, subtree_max_end,static struct rb_root_cached memtype_rbroot =RB_ROOT_CACHED ; -enum { -MEMTYPE_EXACT_MATCH =0 , -MEMTYPE_END_MATCH =1 -}; - -static struct memtype *memtype_match (u64 start,u64 end,int match_type) +static struct memtype *memtype_match (u64 start,u64 end) {struct memtype *entry_match; entry_match =interval_iter_first (&memtype_rbroot, start, end-1 );while (entry_match !=NULL && entry_match->start < end) { -if ((match_type ==MEMTYPE_EXACT_MATCH ) && - (entry_match->start == start) && (entry_match->end == end)) -return entry_match; - -if ((match_type ==MEMTYPE_END_MATCH ) && - (entry_match->start < start) && (entry_match->end == end)) +if (entry_match->start == start && entry_match->end == end)return entry_match; - entry_match =interval_iter_next (entry_match, start, end-1 ); }
@@ -132,32 +121,11 @@ struct memtype *memtype_erase(u64 start, u64 end) {struct memtype *entry_old; -/* -* Since the memtype_rbroot tree allows overlapping ranges, -* memtype_erase() checks with EXACT_MATCH first, i.e. free -* a whole node for the munmap case. If no such entry is found, -* it then checks with END_MATCH, i.e. shrink the size of a node -* from the end for the mremap case. -*/ - entry_old =memtype_match (start, end,MEMTYPE_EXACT_MATCH ); -if (!entry_old) { - entry_old =memtype_match (start, end,MEMTYPE_END_MATCH ); -if (!entry_old) -return ERR_PTR (-EINVAL ); - } - -if (entry_old->start == start) { -/* munmap: erase this node */ -interval_remove (entry_old, &memtype_rbroot); - }else { -/* mremap: update the end value of this node */ -interval_remove (entry_old, &memtype_rbroot); - entry_old->end = start; -interval_insert (entry_old, &memtype_rbroot); - -return NULL ; - } + entry_old =memtype_match (start, end); +if (!entry_old) +return ERR_PTR (-EINVAL ); +interval_remove (entry_old, &memtype_rbroot);return entry_old; }
Signature · 1 line
2.49.0
PATCH 10/11 drm/i915: track_pfn() -> "pfnmap tracking"
In reply to David Hildenbrandtrack_pfn() does not exist, let's simply refer to it as "pfnmap tracking".
--- drivers/gpu/drm/i915/i915_mm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/i915/i915_mm.c +2 -2
@@ -100,7 +100,7 @@ int remap_io_mapping(struct vm_area_struct *vma,GEM_BUG_ON ((vma->vm_flags &EXPECTED_FLAGS ) !=EXPECTED_FLAGS ); -/* We rely on prevalidation of the io-mapping to skip track_pfn(). */ +/* We rely on prevalidation of the io-mapping to skip pfnmap tracking. */ r.mm = vma->vm_mm; r.pfn = pfn; r.prot = __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
@@ -140,7 +140,7 @@ int remap_io_sg(struct vm_area_struct *vma, };int err; -/* We rely on prevalidation of the io-mapping to skip track_pfn(). */ +/* We rely on prevalidation of the io-mapping to skip pfnmap tracking. */ GEM_BUG_ON ((vma->vm_flags &EXPECTED_FLAGS ) !=EXPECTED_FLAGS );while (offset >= r.sgt.max >>PAGE_SHIFT ) {
Signature · 1 line
2.49.0
PATCH 11/11 mm/io-mapping: track_pfn() -> "pfnmap tracking"
In reply to David Hildenbrandtrack_pfn() does not exist, let's simply refer to it as "pfnmap tracking".
--- mm/io-mapping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/io-mapping.c +1 -1
@@ -21,7 +21,7 @@ int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma,if (WARN_ON_ONCE ((vma->vm_flags & expected_flags) != expected_flags))return -EINVAL ; -/* We rely on prevalidation of the io-mapping to skip track_pfn(). */ +/* We rely on prevalidation of the io-mapping to skip pfnmap tracking. */ return remap_pfn_range_notrack (vma, addr, pfn, size,__pgprot ((pgprot_val (iomap->prot) & _PAGE_CACHE_MASK) | (pgprot_val (vma->vm_page_prot) & ~_PAGE_CACHE_MASK)));
Signature · 1 line
2.49.0
Quoted 5 lines · David Hildenbrand
On top of mm-unstable.
VM_PATannoyed me too much and wasted too much of my time, let's clean PAT handling up and removeVM_PAT.
Quoted 8 lines
This should sort out various issues with
VM_PATwe discovered recently, and will hopefully make the whole code more stable and easier to maintain.In essence: we stop letting PAT mode mess with VMAs and instead lift what to track/untrack to the MM core. We remember per VMA which pfn range we tracked in a new struct we attach to a VMA (we have space without exceeding 192 bytes), use a kref to share it among VMAs during split/mremap/fork, and automatically untrack once the kref drops to 0.
Yay!
The extra pointer in vm_area_struct is a small price to pay IMHO.
Quoted 13 lines
This implies that we'll keep tracking a full pfn range even after partially unmapping it, until fully unmapping it; but as that case was mostly broken before, this at least makes it work in a way that is least intrusive to VMA handling.
Shrinking with
mremap()used to work in a hacky way, now we'll similarly keep the original pfn range tacked even after this form of partial unmap. Does anybody care about that? Unlikely. If we run into issues, we could likely handled that (adjust the tracking) when our kref drops to 1 while freeing a VMA. But it adds more complexity, so avoid that for now.Briefly tested
Quoted 7 lines
There will be some clash with [1], but nothing that cannot be sorted out easily by moving the functions added to
kernel/fork.cto wherever the vma bits will live.Briefly tested with some basic
/dev/memtest I crafted. I want to convert them to selftests, but that might or might not require a bit of more work (e.g.,/dev/memaccessibility).
So for the x86 bits, once it passes review by the fine MM folks:
And I suppose this rewrite will be carried in -mm?
Thanks,
Ingo
Quoted 4 lines
Quoted 4 lines
There will be some clash with [1], but nothing that cannot be sorted out easily by moving the functions added to
kernel/fork.cto wherever the vma bits will live.
Quoted 8 lines
Quoted 3 lines
Briefly tested with some basic
/dev/memtest I crafted. I want to convert them to selftests, but that might or might not require a bit of more work (e.g.,/dev/memaccessibility).So for the x86 bits, once it passes review by the fine MM folks:
Acked-by Ingo Molnar <[email protected]>
Thanks!
Quoted 1 line
And I suppose this rewrite will be carried in -mm?
Yes, will make conflicts with Lorenzo's work easier to resolve (in whatever order this ends up going in). I suspect there are not many PAT related things on the horizon.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
In reply to David HildenbrandQuoted 4 lines · David Hildenbrand
... by factoring it out from
track_pfn_remap().For PMDs/PUDs, actually check the full range, and trigger a fallback if we run into this "different memory types / cachemodes" scenario.
The current patch looks like to still pass PAGE_SIZE into the new helper at all track_pfn_insert() call sites, so it seems this comment does not 100% match with the code? Or I may have misread somewhere.
Maybe it's still easier to keep the single-pfn lookup to never fail.. more below.
Quoted 89 lines
Add some documentation.
Will checking each page result in undesired overhead? We'll have to learn. Not checking each page looks wrong, though. Maybe we could optimize the lookup internally.
Signed-off-by David Hildenbrand <[email protected]>--- arch/x86/mm/pat/memtype.c | 24 ++++++++---------------- include/linux/pgtable.h | 28 ++++++++++++++++++++-------- mm/huge_memory.c | 7 +++++-- mm/memory.c | 4 ++-- 4 files changed, 35 insertions(+), 28 deletions(-)arch/x86/mm/pat/memtype.c +8
-16@@ -1031,7 +1031,6 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, unsigned long pfn, unsigned long addr, unsigned long size) { resource_size_t paddr = (resource_size_t)pfn << PAGE_SHIFT; -enum page_cache_mode pcm;/* reserve the whole chunk starting from paddr */ if (!vma || (addr == vma->vm_start@@ -1044,13 +1043,17 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return ret; } +return pfnmap_sanitize_pgprot (pfn, size, prot); +} + +int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size,pgprot_t *prot) +{ +resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; +enum page_cache_mode pcm; +if (!pat_enabled ())return 0 ; -/* -* For anything smaller than the vma size we set prot based on the -* lookup. -*/ pcm =lookup_memtype (paddr);/* Check memtype for the remaining pages */ @@ -1065,17 +1068,6 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return 0 ; } -void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot,pfn_t pfn) -{ -enum page_cache_mode pcm; - -if (!pat_enabled ()) -return ; - - pcm =lookup_memtype (pfn_t_to_phys (pfn)); -pgprot_set_cachemode (prot, pcm); -} -/* * untrack_pfn is called while unmapping a pfnmap for a region. * untrack can be called for a specific region indicated by pfn and size or include/linux/pgtable.h +3
-6@@ -1500,13 +1500,10 @@ static inline int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return 0 ; } -/* -* track_pfn_insert is called when a _new_ single pfn is established -* by vmf_insert_pfn(). -*/ -static inline void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot, -pfn_t pfn) +static inline int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot) { +return 0 ; }/*
include/linux/pgtable.h static inline void untrack_pfn_clear(struct vm_area_struct *vma) · line 1558
Quoted 8 lines
include/linux/pgtable.h +3
-2@@ -1556,8 +1553,23 @@ static inline void untrack_pfn_clear(struct vm_area_struct *vma)extern int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot,unsigned long pfn,unsigned long addr,unsigned long size); -extern void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot, -pfn_t pfn); + +/** +* pfnmap_sanitize_pgprot - sanitize the pgprot for a pfn range
Nit: s/sanitize/update|setup|.../?
But maybe you have good reason to use sanitize. No strong opinions.
Quoted 22 lines
include/linux/pgtable.h +14
-0+* @pfn: the start of the pfn range +* @size: the size of the pfn range +* @prot: the pgprot to sanitize +* +* Sanitize the given pgprot for a pfn range, for example, adjusting the +* cachemode. +* +* This function cannot fail for a single page, but can fail for multiple +* pages. +* +* Returns 0 on success and -EINVAL on error. +*/ +int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0
mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1459
Quoted 6 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;
Would "pgtable" leak if it fails? If it's PAGE_SIZE, IIUC it won't ever trigger, though.
Maybe we could have a "void pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replace track_pfn_insert() and never fail? Dropping vma ref is definitely a win already in all cases.
Quoted 39 lines
mm/huge_memory.c +3
-1+ ptl =pmd_lock (vma->vm_mm, vmf->pmd); error =insert_pfn_pmd (vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);@@ -1577,7 +1579,8 @@ vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write)if (addr < vma->vm_start || addr >= vma->vm_end)return VM_FAULT_SIGBUS ; -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ; ptl =pud_lock (vma->vm_mm, vmf->pud);insert_pfn_pud (vma, addr, vmf->pud, pfn, write);mm/memory.c +2
-2@@ -2563,7 +2563,7 @@ vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,if (!pfn_modify_allowed (pfn, pgprot))return VM_FAULT_SIGBUS ; -track_pfn_insert (vma, &pgprot,__pfn_to_pfn_t (pfn,PFN_DEV )); +pfnmap_sanitize_pgprot (pfn,PAGE_SIZE , &pgprot);return insert_pfn (vma, addr,__pfn_to_pfn_t (pfn,PFN_DEV ), pgprot, false);@@ -2626,7 +2626,7 @@ static vm_fault_t __vm_insert_mixed(struct vm_area_struct *vma,if (addr < vma->vm_start || addr >= vma->vm_end)return VM_FAULT_SIGBUS ; -track_pfn_insert (vma, &pgprot, pfn); +pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot);if (!pfn_modify_allowed (pfn_t_to_pfn (pfn), pgprot))return VM_FAULT_SIGBUS ;Signature · 1 line
2.49.0
Signature · 1 line
Peter Xu
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
Quoted 9 lines · Peter Xu
Quoted 4 lines · David Hildenbrand
... by factoring it out from
track_pfn_remap().For PMDs/PUDs, actually check the full range, and trigger a fallback if we run into this "different memory types / cachemodes" scenario.
The current patch looks like to still pass
PAGE_SIZEinto the new helper at alltrack_pfn_insert()call sites, so it seems this comment does not 100% match with the code? Or I may have misread somewhere.
No, you're right, while reshuffling the patches I forgot to add the actual PMD/PUD size.
Quoted 4 lines
Maybe it's still easier to keep the single-pfn lookup to never fail.. more below.
[...]
Quoted 13 lines
include/linux/pgtable.h static inline void untrack_pfn_clear(struct vm_area_struct *vma) · line 1558
Quoted 10 lines
/*
include/linux/pgtable.h +3
-2@@ -1556,8 +1553,23 @@ static inline void untrack_pfn_clear(struct vm_area_struct *vma)extern int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot,unsigned long pfn,unsigned long addr,unsigned long size); -extern void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot, -pfn_t pfn); + +/** +* pfnmap_sanitize_pgprot - sanitize the pgprot for a pfn range Nit: s/sanitize/update|setup|.../?
Quoted 1 line
But maybe you have good reason to use sanitize. No strong opinions.
What it does on PAT (only implementation so far ...) is looking up the memory type to select the caching mode that can be use.
"sanitize" was IMHO a good fit, because we must make sure that we don't use the wrong caching mode.
update/setup/... don't make that quite clear. Any other suggestions?
Quoted 33 lines
Quoted 23 lines
include/linux/pgtable.h +14
-0+* @pfn: the start of the pfn range +* @size: the size of the pfn range +* @prot: the pgprot to sanitize +* +* Sanitize the given pgprot for a pfn range, for example, adjusting the +* cachemode. +* +* This function cannot fail for a single page, but can fail for multiple +* pages. +* +* Returns 0 on success and -EINVAL on error. +*/ +int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1459
Quoted 6 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.
Quoted 3 lines
Maybe we could have a "void
pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replacetrack_pfn_insert()and never fail? Dropping vma ref is definitely a win already in all cases.
It could be a simple wrapper around pfnmap_sanitize_pgprot(), yes. That's certainly helpful for the single-page case.
Regarding never failing here: we should check the whole range. We have to make sure that none of the pages has a memory type / caching mode that is incompatible with what we setup.
Thanks a bunch for the review!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
In reply to Peter XuQuoted 7 lines
mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1459
Quoted 4 lines
mm/huge_memory.c +2
-1-track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.
Missed that comment. I can document that pgprot will only be touched if the function succeeds.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 04/11] mm/memremap: convert to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandQuoted 32 lines · David Hildenbrand
Let's use the new, cleaner interface.
Signed-off-by David Hildenbrand <[email protected]>--- mm/memremap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)mm/memremap.c +3
-3@@ -130,7 +130,7 @@ static void pageunmap_range(struct dev_pagemap *pgmap, int range_id) }mem_hotplug_done (); -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));pgmap_array_delete (range); }@@ -211,8 +211,8 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (nid <0 ) nid =numa_mem_id (); - error =track_pfn_remap (NULL , ¶ms->pgprot,PHYS_PFN (range->start),0 , -range_len (range)); + error =pfnmap_track (PHYS_PFN (range->start),range_len (range), + ¶ms->pgprot);if (error) goto err_pfn_remap;
mm/memremap.c static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params, · line 280
Quoted 5 lines
mm/memremap.c +1
-1@@ -277,7 +277,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (!is_private)kasan_remove_zero_shadow (__va (range->start),range_len (range)); err_kasan: -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));
Not a huge deal, but maybe we could merge this and previous patch? It might be easier to reference the impl when reading the call site changes.
Quoted 6 lines
mm/memremap.c +0
-0err_pfn_remap:pgmap_array_delete (range);return error;Signature · 1 line
2.49.0
Signature · 1 line
Peter Xu
Re: [PATCH v1 04/11] mm/memremap: convert to pfnmap_track() + pfnmap_untrack()
Quoted 32 lines · Peter Xu
Quoted 31 lines · David Hildenbrand
Let's use the new, cleaner interface.
Signed-off-by David Hildenbrand <[email protected]>--- mm/memremap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)mm/memremap.c +3
-3@@ -130,7 +130,7 @@ static void pageunmap_range(struct dev_pagemap *pgmap, int range_id) }mem_hotplug_done (); -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));pgmap_array_delete (range); }@@ -211,8 +211,8 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (nid <0 ) nid =numa_mem_id (); - error =track_pfn_remap (NULL , ¶ms->pgprot,PHYS_PFN (range->start),0 , -range_len (range)); + error =pfnmap_track (PHYS_PFN (range->start),range_len (range), + ¶ms->pgprot);if (error) goto err_pfn_remap;
Quoted 9 lines
mm/memremap.c static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params, · line 280
Quoted 6 lines
mm/memremap.c +1
-1@@ -277,7 +277,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (!is_private)kasan_remove_zero_shadow (__va (range->start),range_len (range)); err_kasan: -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));Not a huge deal, but maybe we could merge this and previous patch? It might be easier to reference the impl when reading the call site changes.
Yes, I can do that. The important part to me is to split #5 of, to keep that patch somewhat reasonable in size.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandQuoted 61 lines · David Hildenbrand
Let's use our new interface. In
remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.Signed-off-by David Hildenbrand <[email protected]>--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +8
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { +struct kref kref; +unsigned long pfn; +unsigned long size; +}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory
include/linux/mm_types.h struct vm_area_struct { · line 890
Quoted 6 lines
include/linux/mm_types.h +3
-0@@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif
So this was originally the small concern (or is it small?) that this will grow every vma on x86, am I right?
After all pfnmap vmas are the minority, I was thinking whether we could work it out without extending vma struct.
I had a quick thought quite a while ago, but never tried out (it was almost off-track since vfio switched away from remap_pfn_range..), which is to have x86 maintain its own mapping of vma <-> pfn tracking using a global stucture. After all, the memtype code did it already with the memtype_rbroot, so I was thinking if vma info can be memorized as well, so as to get rid of get_pat_info() too.
Maybe it also needs the 2nd layer like what you did with the track ctx, but the tree maintains the mapping instead of adding the ctx pointer into vma.
Maybe it could work with squashing the two layers (or say, extending memtype rbtree), but maybe not..
It could make it slightly slower than vma->pfnmap_track_ctx ref when looking up pfn when holding a vma ref, but I assume it's ok considering that track/untrack should be slow path for pfnmaps, and pfnmaps shouldn't be a huge lot.
I didn't think further, but if that'll work it'll definitely avoids the additional fields on x86 vmas. I'm curious whether you explored that direction, or maybe it's a known decision that the 8 bytes isn't a concern.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
Quoted 63 lines · Peter Xu
Quoted 62 lines · David Hildenbrand
Let's use our new interface. In
remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.Signed-off-by David Hildenbrand <[email protected]>--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +8
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { +struct kref kref; +unsigned long pfn; +unsigned long size; +}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory @@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;
Quoted 8 lines
include/linux/mm_types.h struct vm_area_struct { · line 890
Quoted 5 lines
include/linux/mm_types.h +3
-0#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif So this was originally the small concern (or is it small?) that this will grow every vma on x86, am I right?
Yeah, and last time I looked into this, it would have grown it such that it would require a bigger slab. Right now:
Before this change:
struct vm_area_struct {
union {
struct {
long unsigned int vm_start; /* 0 8 */
long unsigned int vm_end; /* 8 8 */
}; /* 0 16 */
freeptr_t vm_freeptr; /* 0 8 */
}; /* 0 16 */
struct mm_struct * vm_mm; /* 16 8 */
pgprot_t vm_page_prot; /* 24 8 */
union {
const vm_flags_t vm_flags; /* 32 8 */
vm_flags_t __vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned int vm_lock_seq; /* 40 4 */
/* XXX 4 bytes hole, try to pack */
struct list_head anon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
struct anon_vma * anon_vma; /* 64 8 */
const struct vm_operations_struct * vm_ops; /* 72 8 */
long unsigned int vm_pgoff; /* 80 8 */
struct file * vm_file; /* 88 8 */
void * vm_private_data; /* 96 8 */
atomic_long_t swap_readahead_info; /* 104 8 */
struct mempolicy * vm_policy; /* 112 8 */
struct vma_numab_state * numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_t vm_refcnt __attribute__((__aligned__(64))); /* 128 4 */
/* XXX 4 bytes hole, try to pack */
struct {
struct rb_node rb __attribute__((__aligned__(8))); /* 136 24 */
long unsigned int rb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared __attribute__((__aligned__(8))); /* 136 32 */
struct anon_vma_name * anon_name; /* 168 8 */
struct vm_userfaultfd_ctx vm_userfaultfd_ctx; /* 176 0 */
/* size: 192, cachelines: 3, members: 18 */
/* sum members: 168, holes: 2, sum holes: 8 */
/* padding: 16 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));
After this change:
struct vm_area_struct {
union {
struct {
long unsigned int vm_start; /* 0 8 */
long unsigned int vm_end; /* 8 8 */
}; /* 0 16 */
freeptr_t vm_freeptr; /* 0 8 */
}; /* 0 16 */
struct mm_struct * vm_mm; /* 16 8 */
pgprot_t vm_page_prot; /* 24 8 */
union {
const vm_flags_t vm_flags; /* 32 8 */
vm_flags_t __vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned int vm_lock_seq; /* 40 4 */
/* XXX 4 bytes hole, try to pack */
struct list_head anon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
struct anon_vma * anon_vma; /* 64 8 */
const struct vm_operations_struct * vm_ops; /* 72 8 */
long unsigned int vm_pgoff; /* 80 8 */
struct file * vm_file; /* 88 8 */
void * vm_private_data; /* 96 8 */
atomic_long_t swap_readahead_info; /* 104 8 */
struct mempolicy * vm_policy; /* 112 8 */
struct vma_numab_state * numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_t vm_refcnt __attribute__((__aligned__(64))); /* 128 4 */
/* XXX 4 bytes hole, try to pack */
struct {
struct rb_node rb __attribute__((__aligned__(8))); /* 136 24 */
long unsigned int rb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared __attribute__((__aligned__(8))); /* 136 32 */
struct anon_vma_name * anon_name; /* 168 8 */
struct vm_userfaultfd_ctx vm_userfaultfd_ctx; /* 176 0 */
struct pfnmap_track_ctx * pfnmap_track_ctx; /* 176 8 */
/* size: 192, cachelines: 3, members: 19 */
/* sum members: 176, holes: 2, sum holes: 8 */
/* padding: 8 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));
Observe that we allocate 192 bytes with or without pfnmap_track_ctx. (IIRC, slab sizes are ... 128, 192, 256, 512, ...)
Quoted 3 lines
After all pfnmap vmas are the minority, I was thinking whether we could work it out without extending vma struct.
Heh, similar to userfaultfd on most systems, or ones with a mempolicy, or anon vma names, ... :)
But yeah, pfnmap is certainly a minority as well.
Quoted 14 lines
I had a quick thought quite a while ago, but never tried out (it was almost off-track since vfio switched away from
remap_pfn_range..), which is to have x86 maintain its own mapping of vma <-> pfn tracking using a global stucture. After all, the memtype code did it already with thememtype_rbroot, so I was thinking if vma info can be memorized as well, so as to get rid ofget_pat_info()too.Maybe it also needs the 2nd layer like what you did with the track ctx, but the tree maintains the mapping instead of adding the ctx pointer into vma.
Maybe it could work with squashing the two layers (or say, extending memtype rbtree), but maybe not..
Quoted 8 lines
It could make it slightly slower than
vma->pfnmap_track_ctxref when looking up pfn when holding a vma ref, but I assume it's ok considering that track/untrack should be slow path for pfnmaps, and pfnmaps shouldn't be a huge lot.I didn't think further, but if that'll work it'll definitely avoids the additional fields on x86 vmas. I'm curious whether you explored that direction, or maybe it's a known decision that the 8 bytes isn't a concern.
When discussing this approach with Lorenzo, I raised that we could simply map using an xarray the VMA to that structure.
But then, if we're not effectively allocating any more space, then probably not worth adding more complexity right now.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
In reply to David HildenbrandQuoted 36 lines · David Hildenbrand
Quoted 9 lines · Peter Xu
Quoted 4 lines · David Hildenbrand
... by factoring it out from
track_pfn_remap().For PMDs/PUDs, actually check the full range, and trigger a fallback if we run into this "different memory types / cachemodes" scenario.
The current patch looks like to still pass
PAGE_SIZEinto the new helper at alltrack_pfn_insert()call sites, so it seems this comment does not 100% match with the code? Or I may have misread somewhere.No, you're right, while reshuffling the patches I forgot to add the actual PMD/PUD size.
Quoted 4 lines
Maybe it's still easier to keep the single-pfn lookup to never fail.. more below.
[...]
Quoted 13 lines
include/linux/pgtable.h static inline void untrack_pfn_clear(struct vm_area_struct *vma) · line 1558
Quoted 10 lines
/*
include/linux/pgtable.h +3
-2@@ -1556,8 +1553,23 @@ static inline void untrack_pfn_clear(struct vm_area_struct *vma)extern int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot,unsigned long pfn,unsigned long addr,unsigned long size); -extern void track_pfn_insert (struct vm_area_struct *vma,pgprot_t *prot, -pfn_t pfn); + +/** +* pfnmap_sanitize_pgprot - sanitize the pgprot for a pfn range Nit: s/sanitize/update|setup|.../?
Quoted 1 line
But maybe you have good reason to use sanitize. No strong opinions.
Quoted 7 lines
What it does on PAT (only implementation so far ...) is looking up the memory type to select the caching mode that can be use.
"sanitize" was IMHO a good fit, because we must make sure that we don't use the wrong caching mode.
update/setup/... don't make that quite clear. Any other suggestions?
I'm very poor on naming.. :( So far anything seems slightly better than sanitize to me, as the word "sanitize" is actually also used in memtype.c for other purpose.. see sanitize_phys().
Quoted 37 lines
Quoted 33 lines
Quoted 24 lines
include/linux/pgtable.h +14
-0+* @pfn: the start of the pfn range +* @size: the size of the pfn range +* @prot: the pgprot to sanitize +* +* Sanitize the given pgprot for a pfn range, for example, adjusting the +* cachemode. +* +* This function cannot fail for a single page, but can fail for multiple +* pages. +* +* Returns 0 on success and -EINVAL on error. +*/ +int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1458
Quoted 5 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.Quoted 3 lines
Maybe we could have a "void
pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replacetrack_pfn_insert()and never fail? Dropping vma ref is definitely a win already in all cases.
Quoted 6 lines
It could be a simple wrapper around
pfnmap_sanitize_pgprot(), yes. That's certainly helpful for the single-page case.Regarding never failing here: we should check the whole range. We have to make sure that none of the pages has a memory type / caching mode that is incompatible with what we setup.
Would it happen in real world?
IIUC per-vma registration needs to happen first, which checks for memtype conflicts in the first place, or reserve_pfn_range() could already have failed.
Here it's the fault path looking up the memtype, so I would expect it is guaranteed all pfns under the same vma is following the verified (and same) memtype?
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
Quoted 4 lines
Quoted 4 lines
What it does on PAT (only implementation so far ...) is looking up the memory type to select the caching mode that can be use.
Quoted 8 lines
Quoted 4 lines
"sanitize" was IMHO a good fit, because we must make sure that we don't use the wrong caching mode.
update/setup/... don't make that quite clear. Any other suggestions?
I'm very poor on naming.. :( So far anything seems slightly better than sanitize to me, as the word "sanitize" is actually also used in
memtype.cfor other purpose.. seesanitize_phys().
Sure, one can sanitize a lot of things. Here it's the cachemode/pgrpot, in the other functions it's an address.
Likely we should just call it pfnmap_X_cachemode()/
Set/update don't really fit for X in case pfnmap_X_cachemode() is a NOP.
pfnmap_setup_cachemode() ? Hm.
Quoted 45 lines
Quoted 38 lines
Quoted 34 lines
Quoted 25 lines
+ * @pfn: the start of the pfn range
+ * @size: the size of the pfn range
+ * @prot: the pgprot to sanitize
+ *
+ * Sanitize the given pgprot for a pfn range, for example, adjusting the
+ * cachemode.
+ *
+ * This function cannot fail for a single page, but can fail for multiple
+ * pages.
+ *
+ * Returns 0 on success and-EINVALon error.
+ */include/linux/pgtable.h +2
-0+int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1458
Quoted 5 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.Quoted 3 lines
Maybe we could have a "void
pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replacetrack_pfn_insert()and never fail? Dropping vma ref is definitely a win already in all cases.Quoted 6 lines
It could be a simple wrapper around
pfnmap_sanitize_pgprot(), yes. That's certainly helpful for the single-page case.Regarding never failing here: we should check the whole range. We have to make sure that none of the pages has a memory type / caching mode that is incompatible with what we setup.
Quoted 7 lines
Would it happen in real world?
Quoted 1 line
IIUC per-vma registration needs to happen first, which checks for memtype
conflicts in the first place, or
reserve_pfn_range()could already have failed.Quoted 1 line
Here it's the fault path looking up the memtype, so I would expect it is
guaranteed all pfns under the same vma is following the verified (and same) memtype?
The whole point of track_pfn_insert() is that it is used when we *don't* use reserve_pfn_range()->track_pfn_remap(), no?
track_pfn_remap() would check the whole range that gets mapped, so track_pfn_insert() user must similarly check the whole range that gets mapped.
Note that even track_pfn_insert() is already pretty clear on the intended usage: "called when a _new_ single pfn is established"
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 11/11] mm/io-mapping: track_pfn() -> "pfnmap tracking"
In reply to David HildenbrandQuoted 4 lines · David Hildenbrand
track_pfn()does not exist, let's simply refer to it as "pfnmap tracking".Signed-off-by David Hildenbrand <[email protected]>
LGTM, so:
Quoted 18 lines
--- mm/io-mapping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/io-mapping.c +1
-2@@ -21,7 +21,7 @@ int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma,if (WARN_ON_ONCE ((vma->vm_flags & expected_flags) != expected_flags))return -EINVAL ; -/* We rely on prevalidation of the io-mapping to skip track_pfn(). */ +/* We rely on prevalidation of the io-mapping to skip pfnmap tracking. */ return remap_pfn_range_notrack (vma, addr, pfn, size,__pgprot ((pgprot_val (iomap->prot) & _PAGE_CACHE_MASK) | (pgprot_val (vma->vm_page_prot) & ~_PAGE_CACHE_MASK))); --
Quoted 2 lines
2.49.0
However this doesn't apply after commit b8d8f1830bab ("mm/io-mapping:
precompute remap protection flags for clarity"), so will need a rebase :) seems this was cleaned up to separate the __pgprot() bit from the remap_pfn_range_notrack().
Note of course this commit hash is from mm-new so quite changeable... :)
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandQuoted 70 lines · David Hildenbrand
Quoted 60 lines · Peter Xu
Quoted 59 lines · David Hildenbrand
Let's use our new interface. In
remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.Signed-off-by David Hildenbrand <[email protected]>--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +8
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { +struct kref kref; +unsigned long pfn; +unsigned long size; +}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory @@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;Quoted 8 lines
include/linux/mm_types.h struct vm_area_struct { · line 890
Quoted 5 lines
include/linux/mm_types.h +3
-0#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif So this was originally the small concern (or is it small?) that this will grow every vma on x86, am I right?
Quoted 2 lines
Yeah, and last time I looked into this, it would have grown it such that it would require a bigger slab. Right now:
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Quoted 92 lines
Before this change:
struct
vm_area_struct{
union {
struct {
long unsigned intvm_start; /* 0 8 */
long unsigned intvm_end; /* 8 8 */
}; /* 0 16 */
freeptr_tvm_freeptr; /* 0 8 */
}; /* 0 16 */
structmm_struct*vm_mm; /* 16 8 */
pgprot_tvm_page_prot; /* 24 8 */
union {
constvm_flags_tvm_flags; /* 32 8 */
vm_flags_t__vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned intvm_lock_seq; /* 40 4 *//* XXX 4 bytes hole, try to pack */
struct
list_headanon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
structanon_vma*anon_vma; /* 64 8 */
const structvm_operations_struct*vm_ops; /* 72 8 */
long unsigned intvm_pgoff; /* 80 8 */
struct file *vm_file; /* 88 8 */
void *vm_private_data; /* 96 8 */
atomic_long_tswap_readahead_info; /* 104 8 */
struct mempolicy *vm_policy; /* 112 8 */
structvma_numab_state*numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_tvm_refcnt__attribute__((__aligned__(64))); /* 128 4 *//* XXX 4 bytes hole, try to pack */
struct {
structrb_noderb __attribute__((__aligned__(8))); /* 136 24 */
long unsigned intrb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared __attribute__((__aligned__(8))); /* 136 32 */
structanon_vma_name*anon_name; /* 168 8 */
structvm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 0 *//* size: 192, cachelines: 3, members: 18 */
/* sum members: 168, holes: 2, sum holes: 8 */
/* padding: 16 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));After this change:
struct
vm_area_struct{
union {
struct {
long unsigned intvm_start; /* 0 8 */
long unsigned intvm_end; /* 8 8 */
}; /* 0 16 */
freeptr_tvm_freeptr; /* 0 8 */
}; /* 0 16 */
structmm_struct*vm_mm; /* 16 8 */
pgprot_tvm_page_prot; /* 24 8 */
union {
constvm_flags_tvm_flags; /* 32 8 */
vm_flags_t__vm_flags; /* 32 8 */
}; /* 32 8 */
unsigned intvm_lock_seq; /* 40 4 *//* XXX 4 bytes hole, try to pack */
struct
list_headanon_vma_chain; /* 48 16 */
/* --- cacheline 1 boundary (64 bytes) --- */
structanon_vma*anon_vma; /* 64 8 */
const structvm_operations_struct*vm_ops; /* 72 8 */
long unsigned intvm_pgoff; /* 80 8 */
struct file *vm_file; /* 88 8 */
void *vm_private_data; /* 96 8 */
atomic_long_tswap_readahead_info; /* 104 8 */
struct mempolicy *vm_policy; /* 112 8 */
structvma_numab_state*numab_state; /* 120 8 */
/* --- cacheline 2 boundary (128 bytes) --- */
refcount_tvm_refcnt__attribute__((__aligned__(64))); /* 128 4 *//* XXX 4 bytes hole, try to pack */
struct {
structrb_noderb __attribute__((__aligned__(8))); /* 136 24 */
long unsigned intrb_subtree_last; /* 160 8 */
} __attribute__((__aligned__(8))) shared __attribute__((__aligned__(8))); /* 136 32 */
structanon_vma_name*anon_name; /* 168 8 */
structvm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 0 */
structpfnmap_track_ctx*pfnmap_track_ctx; /* 176 8 */
Quoted 8 lines
/* size: 192, cachelines: 3, members: 19 */
/* sum members: 176, holes: 2, sum holes: 8 */
/* padding: 8 */
/* forced alignments: 2, forced holes: 1, sum forced holes: 4 */
} __attribute__((__aligned__(64)));Observe that we allocate 192 bytes with or without
pfnmap_track_ctx. (IIRC, slab sizes are ... 128, 192, 256, 512, ...)
True. I just double checked, vm_area_cachep has SLAB_HWCACHE_ALIGN set, I think it means it's working like that on x86_64 at least indeed. So looks like the new field at least isn't an immediate concern.
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v1 11/11] mm/io-mapping: track_pfn() -> "pfnmap tracking"
In reply to Lorenzo StoakesQuoted 22 lines · Lorenzo Stoakes
Quoted 4 lines · David Hildenbrand
track_pfn()does not exist, let's simply refer to it as "pfnmap tracking".Signed-off-by David Hildenbrand <[email protected]>LGTM, so:
Reviewed-by Lorenzo Stoakes <[email protected]>Quoted 12 lines
--- mm/io-mapping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/io-mapping.c +0
-0@@ -21,7 +21,7 @@ int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma,if (WARN_ON_ONCE ((vma->vm_flags & expected_flags) != expected_flags))return -EINVAL ;
Quoted 13 lines
Quoted 8 lines
mm/io-mapping.c +1
-2-/* We rely on prevalidation of the io-mapping to skip track_pfn(). */ +/* We rely on prevalidation of the io-mapping to skip pfnmap tracking. */ return remap_pfn_range_notrack (vma, addr, pfn, size,__pgprot ((pgprot_val (iomap->prot) & _PAGE_CACHE_MASK) | (pgprot_val (vma->vm_page_prot) & ~_PAGE_CACHE_MASK))); --2.49.0
However this doesn't apply after commit
b8d8f1830bab("mm/io-mapping:
precompute remap protection flags for clarity"), so will need a rebase :) seems this was cleaned up to separate the__pgprot()bit from theremap_pfn_range_notrack().
Yeah, I reviewed that just today. Trivial conflict :)
Thanks!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Peter XuQuoted 71 lines · Peter Xu
Quoted 70 lines · David Hildenbrand
Quoted 68 lines · Peter Xu
Quoted 58 lines · David Hildenbrand
Let's use our new interface. In
remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.Signed-off-by David Hildenbrand <[email protected]>--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +8
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { +struct kref kref; +unsigned long pfn; +unsigned long size; +}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory include/linux/mm_types.h struct vm_area_struct { · line 890
Quoted 6 lines
include/linux/mm_types.h +3
-0@@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif So this was originally the small concern (or is it small?) that this will grow every vma on x86, am I right?
Quoted 6 lines
Quoted 2 lines
Yeah, and last time I looked into this, it would have grown it such that it would require a bigger slab. Right now:
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Note that I enabled everything that you would expect on a production system (incld. userfaultfd, mempolicy, per-vma locks), so I didn't enable lockep.
Thanks for verifying!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 01/11] x86/mm/pat: factor out setting cachemode into pgprot_set_cachemode()
In reply to David HildenbrandQuoted 6 lines · David Hildenbrand
Let's factor it out to make the code easier to grasp.
Use it also in pgprot_writecombine()/pgprot_writethrough() where clearing the old cachemode might not be required, but given that we are already doing a function call, no need to care about this micro-optimization.
Ah my kind of patch :)
Quoted 2 lines
Signed-off-by David Hildenbrand <[email protected]>
LGTM, so:
Quoted 65 lines
--- arch/x86/mm/pat/memtype.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-)arch/x86/mm/pat/memtype.c +10
-11@@ -800,6 +800,12 @@ static inline int range_is_allowed(unsigned long pfn, unsigned long size) }#endif /* CONFIG_STRICT_DEVMEM */ +static inline void pgprot_set_cachemode (pgprot_t *prot,enum page_cache_mode pcm) +{ + *prot =__pgprot ((pgprot_val (*prot) & ~_PAGE_CACHE_MASK) | +cachemode2protval (pcm)); +} +int phys_mem_access_prot_allowed (struct file *file,unsigned long pfn,unsigned long size,pgprot_t *vma_prot) {@@ -811,8 +817,7 @@ int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,if (file->f_flags &O_DSYNC ) pcm = _PAGE_CACHE_MODE_UC_MINUS; - *vma_prot =__pgprot ((pgprot_val (*vma_prot) & ~_PAGE_CACHE_MASK) | -cachemode2protval (pcm)); +pgprot_set_cachemode (vma_prot, pcm);return 1 ; }@@ -880,9 +885,7 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot, (unsigned long long )paddr, (unsigned long long )(paddr + size -1 ), cattr_name(pcm)); - *vma_prot =__pgprot ((pgprot_val (*vma_prot) & - (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); +pgprot_set_cachemode (vma_prot, pcm); }return 0 ; }@@ -907,9 +910,7 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,* We allow returning different type than the one requested in * non strict case. */ - *vma_prot =__pgprot ((pgprot_val (*vma_prot) & - (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); +pgprot_set_cachemode (vma_prot, pcm); }if (memtype_kernel_map_sync (paddr, size, pcm) <0 ) {@@ -1060,9 +1061,7 @@ int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,return -EINVAL ; } - *prot =__pgprot ((pgprot_val (*prot) & (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); - +pgprot_set_cachemode (prot, pcm);return 0 ; }
arch/x86/mm/pat/memtype.c void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, pfn_t pfn) · line 1076
Quoted 4 lines
arch/x86/mm/pat/memtype.c +0
-1@@ -1073,10 +1072,8 @@ void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, pfn_t pfn)if (!pat_enabled ())return ; -/* Set prot based on lookup */
We're losing a comment here but who cares, it's obvious what's happening.
Quoted 30 lines
arch/x86/mm/pat/memtype.c +5
-7pcm =lookup_memtype (pfn_t_to_phys (pfn)); - *prot =__pgprot ((pgprot_val (*prot) & (~_PAGE_CACHE_MASK)) | -cachemode2protval (pcm)); +pgprot_set_cachemode (prot, pcm); }/* @@ -1115,15 +1112,15 @@ void untrack_pfn_clear(struct vm_area_struct *vma)pgprot_t pgprot_writecombine (pgprot_t prot) { -return __pgprot (pgprot_val (prot) | -cachemode2protval (_PAGE_CACHE_MODE_WC)); +pgprot_set_cachemode (&prot, _PAGE_CACHE_MODE_WC); +return prot; }EXPORT_SYMBOL_GPL (pgprot_writecombine);pgprot_t pgprot_writethrough (pgprot_t prot) { -return __pgprot (pgprot_val (prot) | -cachemode2protval (_PAGE_CACHE_MODE_WT)); +pgprot_set_cachemode (&prot, _PAGE_CACHE_MODE_WT); +return prot; }EXPORT_SYMBOL_GPL (pgprot_writethrough); --2.49.0
Re: [PATCH v1 01/11] x86/mm/pat: factor out setting cachemode into pgprot_set_cachemode()
n 0;
Quoted 10 lines
arch/x86/mm/pat/memtype.c void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, pfn_t pfn) · line 1076
Quoted 7 lines
}arch/x86/mm/pat/memtype.c +0
-1@@ -1073,10 +1072,8 @@ void track_pfn_insert(struct vm_area_struct *vma, pgprot_t *prot, pfn_t pfn)if (!pat_enabled ())return ; -/* Set prot based on lookup */ We're losing a comment here but who cares, it's obvious what's happening.
Yeah, it's now self-documented :)
lookup ... set cachemode
Thanks!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
In reply to David HildenbrandQuoted 13 lines · David Hildenbrand
Quoted 4 lines
Quoted 4 lines
What it does on PAT (only implementation so far ...) is looking up the memory type to select the caching mode that can be use.
Quoted 8 lines
Quoted 4 lines
"sanitize" was IMHO a good fit, because we must make sure that we don't use the wrong caching mode.
update/setup/... don't make that quite clear. Any other suggestions?
I'm very poor on naming.. :( So far anything seems slightly better than sanitize to me, as the word "sanitize" is actually also used in
memtype.cfor other purpose.. seesanitize_phys().
Quoted 8 lines
Sure, one can sanitize a lot of things. Here it's the cachemode/pgrpot, in the other functions it's an address.
Likely we should just call it pfnmap_X_cachemode()/
Set/update don't really fit for X in case
pfnmap_X_cachemode()is a NOP.
pfnmap_setup_cachemode()? Hm.
Sounds good here.
Quoted 58 lines
Quoted 39 lines
Quoted 39 lines
Quoted 38 lines
Quoted 26 lines
+ * @pfn: the start of the pfn range
+ * @size: the size of the pfn range
+ * @prot: the pgprot to sanitize
+ *
+ * Sanitize the given pgprot for a pfn range, for example, adjusting the
+ * cachemode.
+ *
+ * This function cannot fail for a single page, but can fail for multiple
+ * pages.
+ *
+ * Returns 0 on success and-EINVALon error.
+ */include/linux/pgtable.h +2
-0+int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1458
Quoted 5 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.Maybe we could have a "void
pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replacetrack_pfn_insert()and never fail? Dropping vma ref is definitely a win already in all cases.Quoted 9 lines
Quoted 6 lines
It could be a simple wrapper around
pfnmap_sanitize_pgprot(), yes. That's certainly helpful for the single-page case.Regarding never failing here: we should check the whole range. We have to make sure that none of the pages has a memory type / caching mode that is incompatible with what we setup.
Would it happen in real world?
Quoted 1 line
IIUC per-vma registration needs to happen first, which checks for
memtype
Quoted 5 lines
conflicts in the first place, or
reserve_pfn_range()could already have failed.Quoted 1 line
Here it's the fault path looking up the memtype, so I would expect it is
guaranteed all pfns under the same vma is following the verified (and same) memtype?
The whole point of
track_pfn_insert()is that it is used when we *don't* use reserve_pfn_range()->track_pfn_remap(), no?
Quoted 6 lines
track_pfn_remap()would check the whole range that gets mapped, sotrack_pfn_insert()user must similarly check the whole range that gets mapped.Note that even
track_pfn_insert()is already pretty clear on the intended
usage: "called when a _new_ single pfn is established"
We need to define "new" then.. But I agree it's not crystal clear at least. I think I just wasn't the first to assume it was reserved, see this (especially, the "Expectation" part..):
commit 5180da410db6369d1f95c9014da1c9bc33fb043e
Author: Suresh Siddha <[email protected]> Date: Mon Oct 8 16:28:29 2012 -0700
x86, pat: separate the pfn attribute tracking for remap_pfn_range and vm_insert_pfn
With PAT enabled, vm_insert_pfn() looks up the existing pfn memory attribute and uses it. Expectation is that the driver reserves the memory attributes for the pfn before calling vm_insert_pfn().
Signature · 1 line
Peter Xu
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandQuoted 6 lines · David Hildenbrand
Quoted 3 lines
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Note that I enabled everything that you would expect on a production system (incld. userfaultfd, mempolicy, per-vma locks), so I didn't enable lockep.
I still doubt whether you at least enabled userfaultfd, e.g., your previous paste has:
struct vm_userfaultfd_ctx vm_userfaultfd_ctx; /* 176 0 */
Not something that matters.. but just in case you didn't use the expected config file you wanted to use..
Signature · 1 line
Peter Xu
Re: [PATCH v1 03/11] x86/mm/pat: introduce pfnmap_track() and pfnmap_untrack()
In reply to David HildenbrandQuoted 6 lines · David Hildenbrand
Let's provide variants of
track_pfn_remap()anduntrack_pfn()that won't mess with VMAs, to replace the existing interface step-by-step.Add some documentation.
Signed-off-by David Hildenbrand <[email protected]>
There's some pedantry below, but this looks fine generally, so notwithstanding that,
Quoted 10 lines
--- arch/x86/mm/pat/memtype.c | 14 ++++++++++++++ include/linux/pgtable.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)arch/x86/mm/pat/memtype.c +0
-0
arch/x86/mm/pat/memtype.c int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size, pgprot_t *prot · line 1075
Quoted 8 lines
arch/x86/mm/pat/memtype.c +5
-0@@ -1068,6 +1068,20 @@ int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size, pgprot_t *protreturn 0 ; } +int pfnmap_track (unsigned long pfn,unsigned long size,pgprot_t *prot) +{ +const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; + +return reserve_pfn_range (paddr, size, prot,0 );
Nitty, but a pattern established by Liam which we've followed consistently in VMA code is to prefix parameters that might be less than obvious, especially boolean parameters, with a comment naming the parameter, e.g.:
return reserve_pfn_range(paddr, size, prot, /*strict_prot=*/0);
Quoted 34 lines
arch/x86/mm/pat/memtype.c +9
-0+} + +void pfnmap_untrack (unsigned long pfn,unsigned long size) +{ +const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; + +free_pfn_range (paddr, size); +} +/* * untrack_pfn is called while unmapping a pfnmap for a region. * untrack can be called for a specific region indicated by pfn and size or include/linux/pgtable.h +10
-0@@ -1506,6 +1506,16 @@ static inline int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size,return 0 ; } +static inline int pfnmap_track (unsigned long pfn,unsigned long size, +pgprot_t *prot) +{ +return 0 ; +} + +static inline void pfnmap_untrack (unsigned long pfn,unsigned long size) +{ +} +/* * track_pfn_copy is called when a VM_PFNMAP VMA is about to get the page * tables copied during copy_page_range(). Will store the pfn to be
include/linux/pgtable.h extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, · line 1585
Quoted 6 lines
include/linux/pgtable.h +3
-0@@ -1570,6 +1580,29 @@ extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot,*/ int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size,pgprot_t *prot); + +/** +* pfnmap_track - track a pfn range
To risk sounding annoyingly pedantic and giving the kind of review that is annoying, this really needs to be expanded, I think perhaps this description is stating the obvious :)
To me the confusing thing is that the 'generic' sounding pfnmap_track() is actually PAT-specific, so surely the description should give a brief overview of PAT here, saying it's applicable on x86-64 etc. etc.
I'm not sure there's much use in keeping this generic when it clearly is not at this point?
include/linux/pgtable.h extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, · line 1587
Quoted 2 lines
include/linux/pgtable.h +2
-0+* @pfn: the start of the pfn range +* @size: the size of the pfn range
In what units? Given it's a pfn range it's a bit ambiguous as to whether it should be expressed in pages/bytes.
include/linux/pgtable.h extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, · line 1600
Quoted 13 lines
include/linux/pgtable.h +13
-0+* @prot: the pgprot to track +* +* Tracking a pfnmap range involves conditionally reserving a pfn range and +* sanitizing the pgprot -- see pfnmap_sanitize_pgprot(). +* +* Returns 0 on success and -EINVAL on error. +*/ +int pfnmap_track (unsigned long pfn,unsigned long size,pgprot_t *prot); + +/** +* pfnmap_untrack - untrack a pfn range +* @pfn: the start of the pfn range +* @size: the size of the pfn range
Same comment as above re: units.
Quoted 11 lines
include/linux/pgtable.h +5
-1+* +* Untrack a pfn range previously tracked through pfnmap_track(), for example, +* un-doing any reservation. +*/ +void pfnmap_untrack (unsigned long pfn,unsigned long size);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma, --2.49.0
Re: [PATCH v1 04/11] mm/memremap: convert to pfnmap_track() + pfnmap_untrack()
In reply to Peter XuQuoted 32 lines · Peter Xu
Quoted 31 lines · David Hildenbrand
Let's use the new, cleaner interface.
Signed-off-by David Hildenbrand <[email protected]>--- mm/memremap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)mm/memremap.c +3
-3@@ -130,7 +130,7 @@ static void pageunmap_range(struct dev_pagemap *pgmap, int range_id) }mem_hotplug_done (); -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));pgmap_array_delete (range); }@@ -211,8 +211,8 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (nid <0 ) nid =numa_mem_id (); - error =track_pfn_remap (NULL , ¶ms->pgprot,PHYS_PFN (range->start),0 , -range_len (range)); + error =pfnmap_track (PHYS_PFN (range->start),range_len (range), + ¶ms->pgprot);if (error) goto err_pfn_remap;
Quoted 9 lines
mm/memremap.c static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params, · line 280
Quoted 6 lines
mm/memremap.c +1
-1@@ -277,7 +277,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (!is_private)kasan_remove_zero_shadow (__va (range->start),range_len (range)); err_kasan: -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));Not a huge deal, but maybe we could merge this and previous patch? It might be easier to reference the impl when reading the call site changes.
Agreed, it would add a sort of built-in justification for the prior patch as in 'here is where we use it, this is why I'm doing this' kind of thing also.
And I think this is small enough combined with the previous to not be a big deal to have both together.
Thanks!
Quoted 11 lines
Quoted 7 lines
mm/memremap.c +0
-1err_pfn_remap:pgmap_array_delete (range);return error; --2.49.0
--
Peter Xu
Lorenzo Stoakes
Reviewed-byRe: [PATCH v1 04/11] mm/memremap: convert to pfnmap_track() + pfnmap_untrack()
In reply to Peter XuQuoted 4 lines · Peter Xu
Quoted 3 lines · David Hildenbrand
Let's use the new, cleaner interface.
Signed-off-by David Hildenbrand <[email protected]>
LGTM, albeit though, as discussed elsewhere in thread, this should be merged with prior patch, though FWIW:
Quoted 48 lines
Quoted 29 lines
--- mm/memremap.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)mm/memremap.c +3
-3@@ -130,7 +130,7 @@ static void pageunmap_range(struct dev_pagemap *pgmap, int range_id) }mem_hotplug_done (); -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));pgmap_array_delete (range); }@@ -211,8 +211,8 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (nid <0 ) nid =numa_mem_id (); - error =track_pfn_remap (NULL , ¶ms->pgprot,PHYS_PFN (range->start),0 , -range_len (range)); + error =pfnmap_track (PHYS_PFN (range->start),range_len (range), + ¶ms->pgprot);if (error) goto err_pfn_remap;mm/memremap.c static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params, · line 280
Quoted 5 lines
mm/memremap.c +1
-1@@ -277,7 +277,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,if (!is_private)kasan_remove_zero_shadow (__va (range->start),range_len (range)); err_kasan: -untrack_pfn (NULL ,PHYS_PFN (range->start),range_len (range), true); +pfnmap_untrack (PHYS_PFN (range->start),range_len (range));Not a huge deal, but maybe we could merge this and previous patch? It might be easier to reference the impl when reading the call site changes.
Quoted 6 lines
mm/memremap.c +0
-1err_pfn_remap:pgmap_array_delete (range);return error; --2.49.0
--
Peter Xu
Re: [PATCH v1 03/11] x86/mm/pat: introduce pfnmap_track() and pfnmap_untrack()
In reply to Lorenzo StoakesQuoted 12 lines
arch/x86/mm/pat/memtype.c int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size, pgprot_t *prot · line 1075
Quoted 7 lines
arch/x86/mm/pat/memtype.c +5
-0+int pfnmap_track (unsigned long pfn,unsigned long size,pgprot_t *prot) +{ +const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; + +return reserve_pfn_range (paddr, size, prot,0 );Nitty, but a pattern established by Liam which we've followed consistently in VMA code is to prefix parameters that might be less than obvious, especially boolean parameters, with a comment naming the parameter, e.g.:
Quoted 1 line
return reserve_pfn_range(paddr, size, prot, /*strict_prot=*/0);
Not sure I like that. But as this parameter goes away patch #8, I'll leave it as is in this patch and not start a bigger discussion on better alternatives (don't use these stupid boolean variables ...) ;)
[...]
Quoted 8 lines
include/linux/pgtable.h extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, · line 1585
Quoted 3 lines
include/linux/pgtable.h +3
-0+ +/** +* pfnmap_track - track a pfn range To risk sounding annoyingly pedantic and giving the kind of review that is annoying, this really needs to be expanded, I think perhaps this description is stating the obvious :)
Quoted 6 lines
To me the confusing thing is that the 'generic' sounding
pfnmap_track()is actually PAT-specific, so surely the description should give a brief overview of PAT here, saying it's applicable on x86-64 etc. etc.I'm not sure there's much use in keeping this generic when it clearly is not at this point?
Sorry, is your suggestion to document more PAT stuff or what exactly?
As you know, I'm a busy man ... so instructions/suggestions please :)
Quoted 6 lines
Quoted 3 lines
+ * @pfn: the start of the pfn range
+ * @size: the size of the pfn rangeIn what units? Given it's a pfn range it's a bit ambiguous as to whether it should be expressed in pages/bytes.
Agreed. It's bytes. (not my favorite here, but good enough)
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Peter XuQuoted 8 lines · Peter Xu
Quoted 6 lines · David Hildenbrand
Quoted 3 lines
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Note that I enabled everything that you would expect on a production system (incld. userfaultfd, mempolicy, per-vma locks), so I didn't enable lockep.
Quoted 7 lines
I still doubt whether you at least enabled userfaultfd, e.g., your previous paste has:
struct
vm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 0 */Not something that matters.. but just in case you didn't use the expected config file you wanted to use..
You're absolutely right. I only briefly rechecked for this purpose here on my notebook, and only looked for the existence of members, not expecting that we have confusing stuff like vm_userfaultfd_ctx.
I checked again and the size stays at 192 with allyesconfig and then disabling debug options.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 03/11] x86/mm/pat: introduce pfnmap_track() and pfnmap_untrack()
In reply to David HildenbrandQuoted 31 lines · David Hildenbrand
Quoted 12 lines
arch/x86/mm/pat/memtype.c int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size, pgprot_t *prot · line 1075
Quoted 7 lines
arch/x86/mm/pat/memtype.c +5
-0+int pfnmap_track (unsigned long pfn,unsigned long size,pgprot_t *prot) +{ +const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; + +return reserve_pfn_range (paddr, size, prot,0 );Nitty, but a pattern established by Liam which we've followed consistently in VMA code is to prefix parameters that might be less than obvious, especially boolean parameters, with a comment naming the parameter, e.g.:
Quoted 1 line
return reserve_pfn_range(paddr, size, prot, /*strict_prot=*/0);Not sure I like that. But as this parameter goes away patch #8, I'll leave it as is in this patch and not start a bigger discussion on better alternatives (don't use these stupid boolean variables ...) ;)
[...]
Quoted 12 lines
include/linux/pgtable.h extern int track_pfn_remap(struct vm_area_struct *vma, pgprot_t *prot, · line 1585
Quoted 3 lines
include/linux/pgtable.h +3
-0+ +/** +* pfnmap_track - track a pfn range To risk sounding annoyingly pedantic and giving the kind of review that is annoying, this really needs to be expanded, I think perhaps this description is stating the obvious :)
To me the confusing thing is that the 'generic' sounding
pfnmap_track()is actually PAT-specific, so surely the description should give a brief overview of PAT here, saying it's applicable on x86-64 etc. etc.
Quoted 6 lines
Quoted 2 lines
I'm not sure there's much use in keeping this generic when it clearly is not at this point?
Sorry, is your suggestion to document more PAT stuff or what exactly?
As you know, I'm a busy man ... so instructions/suggestions please :)
Haha sure, I _think_ the model here is to have a brief summary then underneath a more detailed explanation, so that could be:
This address range is requested to be 'tracked' by a hardware implementation allowing fine-grained control of memory attributes at page level granularity.
This allows for fine-grained control of memory cache behaviour. Tracking memory this way is persisted across VMA split and merge.
Currently there is only one implementation for this - x86 Page Attribute Table (PAT). See Documentation/arch/x86/pat.rst for more details.
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
+ * @pfn: the start of the pfn range
+ * @size: the size of the pfn rangeIn what units? Given it's a pfn range it's a bit ambiguous as to whether it should be expressed in pages/bytes.
Agreed. It's bytes. (not my favorite here, but good enough)
Ack, definitely need to spell it out here! Cheers :)
Quoted 7 lines
--
Cheers,David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandQuoted 17 lines · David Hildenbrand
Quoted 8 lines · Peter Xu
Quoted 6 lines · David Hildenbrand
Quoted 3 lines
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Note that I enabled everything that you would expect on a production system (incld. userfaultfd, mempolicy, per-vma locks), so I didn't enable lockep.
Quoted 7 lines
I still doubt whether you at least enabled userfaultfd, e.g., your previous paste has:
struct
vm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 0 */Not something that matters.. but just in case you didn't use the expected config file you wanted to use..
Quoted 6 lines
You're absolutely right. I only briefly rechecked for this purpose here on my notebook, and only looked for the existence of members, not expecting that we have confusing stuff like
vm_userfaultfd_ctx.I checked again and the size stays at 192 with allyesconfig and then disabling debug options.
I think a reasonable case is everything on, except CONFIG_DEBUG_LOCK_ALLOC and I don't care about nommu.
So:
CONFIG_PER_VMA_LOCKCONFIG_SWAPCONFIG_MMU (exclude the nommu vm_region field)CONFIG_NUMACONFIG_NUMA_BALANCINGCONFIG_PER_VMA_LOCKCONFIG_ANON_VMA_NAME__HAVE_PFNMAP_TRACKING
So to be clear - allyesconfig w/o debug gives us this yes? And we don't add a cache line? In which case all good :)
Quoted 6 lines
--
Cheers,David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David Hildenbrand+cc Suren, who has worked HEAVILY on VMA field manipulation and such :)
Suren - David is proposing adding a new field. AFAICT this does not add a new cache line so I think we're all good.
But FYI!
Quoted 312 lines · David Hildenbrand
Let's use our new interface. In
remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.Signed-off-by David Hildenbrand <[email protected]>--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +11
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { +struct kref kref; +unsigned long pfn; +unsigned long size; +}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory @@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif } __randomize_layout;#ifdef CONFIG_NUMA kernel/fork.c +50
-4@@ -481,7 +481,51 @@ static void vm_area_init_from(const struct vm_area_struct *src,#ifdef CONFIG_NUMA dest->vm_policy = src->vm_policy;#endif +#ifdef __HAVE_PFNMAP_TRACKING + dest->pfnmap_track_ctx =NULL ; +#endif +} + +#ifdef __HAVE_PFNMAP_TRACKING +static inline int vma_pfnmap_track_ctx_dup (struct vm_area_struct *orig, +struct vm_area_struct *new) +{ +struct pfnmap_track_ctx *ctx = orig->pfnmap_track_ctx; + +if (likely (!ctx)) +return 0 ; + +/* +* We don't expect to ever hit this. If ever required, we would have +* to duplicate the tracking. +*/ +if (unlikely (kref_read (&ctx->kref) >=REFCOUNT_MAX )) +return -ENOMEM ; +kref_get (&ctx->kref); + new->pfnmap_track_ctx = ctx; +return 0 ; +} + +static inline void vma_pfnmap_track_ctx_release (struct vm_area_struct *vma) +{ +struct pfnmap_track_ctx *ctx = vma->pfnmap_track_ctx; + +if (likely (!ctx)) +return ; + +kref_put (&ctx->kref, pfnmap_track_ctx_release); + vma->pfnmap_track_ctx =NULL ; +} +#else +static inline int vma_pfnmap_track_ctx_dup (struct vm_area_struct *orig, +struct vm_area_struct *new) +{ +return 0 ; } +static inline void vma_pfnmap_track_ctx_release (struct vm_area_struct *vma) +{ +} +#endif struct vm_area_struct *vm_area_dup (struct vm_area_struct *orig) {@@ -493,6 +537,11 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)ASSERT_EXCLUSIVE_WRITER (orig->vm_flags);ASSERT_EXCLUSIVE_WRITER (orig->vm_file);vm_area_init_from (orig, new); + +if (vma_pfnmap_track_ctx_dup (orig, new)) { +kmem_cache_free (vm_area_cachep, new); +return NULL ; + }vma_lock_init (new, true);INIT_LIST_HEAD (&new->anon_vma_chain);vma_numab_state_init (new);@@ -507,6 +556,7 @@ void vm_area_free(struct vm_area_struct *vma)vma_assert_detached (vma);vma_numab_state_free (vma);free_anon_vma_name (vma); +vma_pfnmap_track_ctx_release (vma);kmem_cache_free (vm_area_cachep, vma); }@@ -669,10 +719,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,if (!tmp) goto fail_nomem; -/* track_pfn_copy() will later take care of copying internal state. */ -if (unlikely (tmp->vm_flags &VM_PFNMAP )) -untrack_pfn_clear (tmp); - retval =vma_dup_policy (mpnt, tmp);if (retval) goto fail_nomem_policy;mm/memory.c +65
-16@@ -1370,7 +1370,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)struct mm_struct *dst_mm = dst_vma->vm_mm;struct mm_struct *src_mm = src_vma->vm_mm;struct mmu_notifier_range range; -unsigned long next, pfn =0 ; +unsigned long next;bool is_cow;int ret;@@ -1380,12 +1380,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)if (is_vm_hugetlb_page (src_vma))return copy_hugetlb_page_range (dst_mm, src_mm, dst_vma, src_vma); -if (unlikely (src_vma->vm_flags &VM_PFNMAP )) { - ret =track_pfn_copy (dst_vma, src_vma, &pfn); -if (ret) -return ret; - } -/* * We need to invalidate the secondary MMU mappings only when * there could be a permission downgrade on the ptes of the @@ -1427,8 +1421,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)raw_write_seqcount_end (&src_mm->write_protect_seq);mmu_notifier_invalidate_range_end (&range); } -if (ret &&unlikely (src_vma->vm_flags &VM_PFNMAP )) -untrack_pfn_copy (dst_vma, pfn);return ret; }@@ -1923,9 +1915,6 @@ static void unmap_single_vma(struct mmu_gather *tlb,if (vma->vm_file)uprobe_munmap (vma, start, end); -if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn (vma,0 ,0 , mm_wr_locked); -if (start != end) {if (unlikely (is_vm_hugetlb_page (vma))) {/* @@ -2871,6 +2860,36 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,return error; } +#ifdef __HAVE_PFNMAP_TRACKING +static inline struct pfnmap_track_ctx *pfnmap_track_ctx_alloc (unsigned long pfn, +unsigned long size,pgprot_t *prot) +{ +struct pfnmap_track_ctx *ctx; + +if (pfnmap_track (pfn, size, prot)) +return ERR_PTR (-EINVAL ); + + ctx =kmalloc (sizeof (*ctx),GFP_KERNEL ); +if (unlikely (!ctx)) { +pfnmap_untrack (pfn, size); +return ERR_PTR (-ENOMEM ); + } + + ctx->pfn = pfn; + ctx->size = size; +kref_init (&ctx->kref); +return ctx; +} + +void pfnmap_track_ctx_release (struct kref *ref) +{ + struct pfnmap_track_ctx *ctx = container_of(ref, struct pfnmap_track_ctx, kref); + +pfnmap_untrack (ctx->pfn, ctx->size); +kfree (ctx); +} +#endif /* __HAVE_PFNMAP_TRACKING */ +/** * remap_pfn_range - remap kernel memory to userspace * @vma: user vma to map to @@ -2883,20 +2902,50 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,* * Return: %0 on success, negative error code otherwise. */ +#ifdef __HAVE_PFNMAP_TRACKINGint remap_pfn_range (struct vm_area_struct *vma,unsigned long addr,unsigned long pfn,unsigned long size,pgprot_t prot) { +struct pfnmap_track_ctx *ctx =NULL ;int err; - err =track_pfn_remap (vma, &prot, pfn, addr,PAGE_ALIGN (size)); -if (err) + size =PAGE_ALIGN (size); + +/* +* If we cover the full VMA, we'll perform actual tracking, and +* remember to untrack when the last reference to our tracking +* context from a VMA goes away. +* +* If we only cover parts of the VMA, we'll only sanitize the +* pgprot. +*/ +if (addr == vma->vm_start && addr + size == vma->vm_end) { +if (vma->pfnmap_track_ctx) +return -EINVAL ; + ctx =pfnmap_track_ctx_alloc (pfn, size, &prot); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); + }else if (pfnmap_sanitize_pgprot (pfn, size, &prot)) {return -EINVAL ; + } err =remap_pfn_range_notrack (vma, addr, pfn, size, prot); -if (err) -untrack_pfn (vma, pfn,PAGE_ALIGN (size), true); +if (ctx) { +if (err) +kref_put (&ctx->kref, pfnmap_track_ctx_release); +else + vma->pfnmap_track_ctx = ctx; + }return err; } + +#else +int remap_pfn_range (struct vm_area_struct *vma,unsigned long addr, +unsigned long pfn,unsigned long size,pgprot_t prot) +{ +return remap_pfn_range_notrack (vma, addr, pfn, size, prot); +} +#endif EXPORT_SYMBOL (remap_pfn_range);/** mm/mremap.c +0
-5@@ -1191,10 +1191,6 @@ static int copy_vma_and_data(struct vma_remap_struct *vrm,if (is_vm_hugetlb_page (vma))clear_vma_resv_huge_pages (vma); -/* Tell pfnmap has moved from this vma */ -if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn_clear (vma); - *new_vma_ptr = new_vma;return err; } --2.49.0
Suren Baghdasaryan
reviewerRe: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Lorenzo StoakesQuoted 23 lines · Lorenzo Stoakes
Quoted 21 lines · David Hildenbrand
Quoted 8 lines · Peter Xu
Quoted 6 lines · David Hildenbrand
Quoted 3 lines
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Note that I enabled everything that you would expect on a production system (incld. userfaultfd, mempolicy, per-vma locks), so I didn't enable lockep.
Quoted 7 lines
I still doubt whether you at least enabled userfaultfd, e.g., your previous paste has:
struct
vm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 0 */Not something that matters.. but just in case you didn't use the expected config file you wanted to use..
You're absolutely right. I only briefly rechecked for this purpose here on my notebook, and only looked for the existence of members, not expecting that we have confusing stuff like
vm_userfaultfd_ctx.
Quoted 5 lines
Quoted 2 lines
I checked again and the size stays at 192 with allyesconfig and then disabling debug options.
I think a reasonable case is everything on, except
CONFIG_DEBUG_LOCK_ALLOCand I don't care about nommu.
I think it's safe to assume that production systems would disable lockdep due to the performance overhead. At least that's what we do on Android - enable it on development branches but disable in production.
Quoted 23 lines
So:
CONFIG_PER_VMA_LOCKCONFIG_SWAPCONFIG_MMU(exclude the nommuvm_regionfield)CONFIG_NUMACONFIG_NUMA_BALANCINGCONFIG_PER_VMA_LOCKCONFIG_ANON_VMA_NAME__HAVE_PFNMAP_TRACKINGSo to be clear - allyesconfig w/o debug gives us this yes? And we don't add a cache line? In which case all good :)
Quoted 6 lines
--
Cheers,David / dhildenb
Suren Baghdasaryan
reviewerRe: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Lorenzo StoakesQuoted 7 lines · Lorenzo Stoakes
+cc Suren, who has worked HEAVILY on VMA field manipulation and such :)
Suren - David is proposing adding a new field. AFAICT this does not add a new cache line so I think we're all good.
But FYI!
Thanks! Yes, there should be some space in the last cacheline after my last field reshuffling.
Quoted 314 lines · David Hildenbrand
Quoted 312 lines
Let's use our new interface. In
remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.Signed-off-by David Hildenbrand <[email protected]>--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +11
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { +struct kref kref; +unsigned long pfn; +unsigned long size; +}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory @@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif } __randomize_layout;#ifdef CONFIG_NUMA kernel/fork.c +50
-4@@ -481,7 +481,51 @@ static void vm_area_init_from(const struct vm_area_struct *src,#ifdef CONFIG_NUMA dest->vm_policy = src->vm_policy;#endif +#ifdef __HAVE_PFNMAP_TRACKING + dest->pfnmap_track_ctx =NULL ; +#endif +} + +#ifdef __HAVE_PFNMAP_TRACKING +static inline int vma_pfnmap_track_ctx_dup (struct vm_area_struct *orig, +struct vm_area_struct *new) +{ +struct pfnmap_track_ctx *ctx = orig->pfnmap_track_ctx; + +if (likely (!ctx)) +return 0 ; + +/* +* We don't expect to ever hit this. If ever required, we would have +* to duplicate the tracking. +*/ +if (unlikely (kref_read (&ctx->kref) >=REFCOUNT_MAX )) +return -ENOMEM ; +kref_get (&ctx->kref); + new->pfnmap_track_ctx = ctx; +return 0 ; +} + +static inline void vma_pfnmap_track_ctx_release (struct vm_area_struct *vma) +{ +struct pfnmap_track_ctx *ctx = vma->pfnmap_track_ctx; + +if (likely (!ctx)) +return ; + +kref_put (&ctx->kref, pfnmap_track_ctx_release); + vma->pfnmap_track_ctx =NULL ; +} +#else +static inline int vma_pfnmap_track_ctx_dup (struct vm_area_struct *orig, +struct vm_area_struct *new) +{ +return 0 ; } +static inline void vma_pfnmap_track_ctx_release (struct vm_area_struct *vma) +{ +} +#endif struct vm_area_struct *vm_area_dup (struct vm_area_struct *orig) {@@ -493,6 +537,11 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)ASSERT_EXCLUSIVE_WRITER (orig->vm_flags);ASSERT_EXCLUSIVE_WRITER (orig->vm_file);vm_area_init_from (orig, new); + +if (vma_pfnmap_track_ctx_dup (orig, new)) { +kmem_cache_free (vm_area_cachep, new); +return NULL ; + }vma_lock_init (new, true);INIT_LIST_HEAD (&new->anon_vma_chain);vma_numab_state_init (new);@@ -507,6 +556,7 @@ void vm_area_free(struct vm_area_struct *vma)vma_assert_detached (vma);vma_numab_state_free (vma);free_anon_vma_name (vma); +vma_pfnmap_track_ctx_release (vma);kmem_cache_free (vm_area_cachep, vma); }@@ -669,10 +719,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,if (!tmp) goto fail_nomem; -/* track_pfn_copy() will later take care of copying internal state. */ -if (unlikely (tmp->vm_flags &VM_PFNMAP )) -untrack_pfn_clear (tmp); - retval =vma_dup_policy (mpnt, tmp);if (retval) goto fail_nomem_policy;mm/memory.c +65
-16@@ -1370,7 +1370,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)struct mm_struct *dst_mm = dst_vma->vm_mm;struct mm_struct *src_mm = src_vma->vm_mm;struct mmu_notifier_range range; -unsigned long next, pfn =0 ; +unsigned long next;bool is_cow;int ret;@@ -1380,12 +1380,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)if (is_vm_hugetlb_page (src_vma))return copy_hugetlb_page_range (dst_mm, src_mm, dst_vma, src_vma); -if (unlikely (src_vma->vm_flags &VM_PFNMAP )) { - ret =track_pfn_copy (dst_vma, src_vma, &pfn); -if (ret) -return ret; - } -/* * We need to invalidate the secondary MMU mappings only when * there could be a permission downgrade on the ptes of the @@ -1427,8 +1421,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)raw_write_seqcount_end (&src_mm->write_protect_seq);mmu_notifier_invalidate_range_end (&range); } -if (ret &&unlikely (src_vma->vm_flags &VM_PFNMAP )) -untrack_pfn_copy (dst_vma, pfn);return ret; }@@ -1923,9 +1915,6 @@ static void unmap_single_vma(struct mmu_gather *tlb,if (vma->vm_file)uprobe_munmap (vma, start, end); -if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn (vma,0 ,0 , mm_wr_locked); -if (start != end) {if (unlikely (is_vm_hugetlb_page (vma))) {/* @@ -2871,6 +2860,36 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,return error; } +#ifdef __HAVE_PFNMAP_TRACKING +static inline struct pfnmap_track_ctx *pfnmap_track_ctx_alloc (unsigned long pfn, +unsigned long size,pgprot_t *prot) +{ +struct pfnmap_track_ctx *ctx; + +if (pfnmap_track (pfn, size, prot)) +return ERR_PTR (-EINVAL ); + + ctx =kmalloc (sizeof (*ctx),GFP_KERNEL ); +if (unlikely (!ctx)) { +pfnmap_untrack (pfn, size); +return ERR_PTR (-ENOMEM ); + } + + ctx->pfn = pfn; + ctx->size = size; +kref_init (&ctx->kref); +return ctx; +} + +void pfnmap_track_ctx_release (struct kref *ref) +{ + struct pfnmap_track_ctx *ctx = container_of(ref, struct pfnmap_track_ctx, kref); + +pfnmap_untrack (ctx->pfn, ctx->size); +kfree (ctx); +} +#endif /* __HAVE_PFNMAP_TRACKING */ +/** * remap_pfn_range - remap kernel memory to userspace * @vma: user vma to map to @@ -2883,20 +2902,50 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,* * Return: %0 on success, negative error code otherwise. */ +#ifdef __HAVE_PFNMAP_TRACKINGint remap_pfn_range (struct vm_area_struct *vma,unsigned long addr,unsigned long pfn,unsigned long size,pgprot_t prot) { +struct pfnmap_track_ctx *ctx =NULL ;int err; - err =track_pfn_remap (vma, &prot, pfn, addr,PAGE_ALIGN (size)); -if (err) + size =PAGE_ALIGN (size); + +/* +* If we cover the full VMA, we'll perform actual tracking, and +* remember to untrack when the last reference to our tracking +* context from a VMA goes away. +* +* If we only cover parts of the VMA, we'll only sanitize the +* pgprot. +*/ +if (addr == vma->vm_start && addr + size == vma->vm_end) { +if (vma->pfnmap_track_ctx) +return -EINVAL ; + ctx =pfnmap_track_ctx_alloc (pfn, size, &prot); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); + }else if (pfnmap_sanitize_pgprot (pfn, size, &prot)) {return -EINVAL ; + } err =remap_pfn_range_notrack (vma, addr, pfn, size, prot); -if (err) -untrack_pfn (vma, pfn,PAGE_ALIGN (size), true); +if (ctx) { +if (err) +kref_put (&ctx->kref, pfnmap_track_ctx_release); +else + vma->pfnmap_track_ctx = ctx; + }return err; } + +#else +int remap_pfn_range (struct vm_area_struct *vma,unsigned long addr, +unsigned long pfn,unsigned long size,pgprot_t prot) +{ +return remap_pfn_range_notrack (vma, addr, pfn, size, prot); +} +#endif EXPORT_SYMBOL (remap_pfn_range);/** mm/mremap.c +0
-5@@ -1191,10 +1191,6 @@ static int copy_vma_and_data(struct vma_remap_struct *vrm,if (is_vm_hugetlb_page (vma))clear_vma_resv_huge_pages (vma); -/* Tell pfnmap has moved from this vma */ -if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn_clear (vma); - *new_vma_ptr = new_vma;return err; } --2.49.0
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandQuoted 3 lines · David Hildenbrand
Let's use our new interface. In
remap_pfn_range(), we'll now decide whether we have to track (full VMA covered) or only sanitize the pgprot (partial VMA covered).
Yeah I do agree with Peter that 'sanitize' is not great here, but naming is hard :) anyway was discussed in that thread elsewhere...
Quoted 4 lines
Remember what we have to untrack by linking it from the VMA. When duplicating VMAs (e.g., splitting, mremap, fork), we'll handle it similar to anon VMA names, and use a kref to share the tracking.
Yes this is sensible.
Quoted 4 lines
Once the last VMA un-refs our tracking data, we'll do the untracking, which simplifies things a lot and should sort our various issues we saw recently, for example, when partially unmapping/zapping a tracked VMA.
Lovely!
Quoted 7 lines
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.
Hm, but what if we shrink a VMA, then map another one, might it be incorrectly storing PAT attributes for part of the range that is now mapped elsewhere?
Also my god re: the 'kind of working' aspects of PAT, so frustrating.
Quoted 2 lines
Signed-off-by David Hildenbrand <[email protected]>
Generally looking good, afaict, but maybe let's get some input from Suren on VMA size.
Are there actually any PAT tests out here? I had a quick glance in tools/testing/selftests/x86,mm and couldn't find any, but didn't look _that_ card.
Thanks in general for tackling this, this is a big improvement!
Quoted 26 lines
--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +0
-0
include/linux/mm_types.h struct vma_numab_state { · line 771
Quoted 8 lines
include/linux/mm_types.h +5
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { + struct kref kref; + unsigned long pfn; + unsigned long size;
Again, (super) nitty, but we really should express units. I suppose 'size' implies bytes to be honest as you'd unlikely say 'size' for number of pages (you'd go with nr_pages or something). But maybe a trailing /* in bytes */ would help.
Not a big deal though!
include/linux/mm_types.h struct vm_area_struct { · line 888
Quoted 11 lines
include/linux/mm_types.h +4
-0+}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory @@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKING
An aside, but absolutely hate '__HAVE_PFNMAP_TRACKING' as a name here. But you didn't create it, and it's not really sensible to change it in this series so. Just a rumble...
Quoted 51 lines
include/linux/mm_types.h +2
-0+struct pfnmap_track_ctx *pfnmap_track_ctx; +#endif } __randomize_layout;#ifdef CONFIG_NUMA kernel/fork.c +38
-0@@ -481,7 +481,51 @@ static void vm_area_init_from(const struct vm_area_struct *src,#ifdef CONFIG_NUMA dest->vm_policy = src->vm_policy;#endif +#ifdef __HAVE_PFNMAP_TRACKING + dest->pfnmap_track_ctx =NULL ; +#endif +} + +#ifdef __HAVE_PFNMAP_TRACKING +static inline int vma_pfnmap_track_ctx_dup (struct vm_area_struct *orig, +struct vm_area_struct *new) +{ +struct pfnmap_track_ctx *ctx = orig->pfnmap_track_ctx; + +if (likely (!ctx)) +return 0 ; + +/* +* We don't expect to ever hit this. If ever required, we would have +* to duplicate the tracking. +*/ +if (unlikely (kref_read (&ctx->kref) >=REFCOUNT_MAX )) +return -ENOMEM ; +kref_get (&ctx->kref); + new->pfnmap_track_ctx = ctx; +return 0 ; +} + +static inline void vma_pfnmap_track_ctx_release (struct vm_area_struct *vma) +{ +struct pfnmap_track_ctx *ctx = vma->pfnmap_track_ctx; + +if (likely (!ctx)) +return ; + +kref_put (&ctx->kref, pfnmap_track_ctx_release); + vma->pfnmap_track_ctx =NULL ; +} +#else +static inline int vma_pfnmap_track_ctx_dup (struct vm_area_struct *orig, +struct vm_area_struct *new)
kernel/fork.c static void vm_area_init_from(const struct vm_area_struct *src, · line 531
Quoted 10 lines
kernel/fork.c +6
-0+{ +return 0 ; } +static inline void vma_pfnmap_track_ctx_release (struct vm_area_struct *vma) +{ +} +#endif struct vm_area_struct *vm_area_dup (struct vm_area_struct *orig) {
Obviously my series will break this but should be _fairly_ trivial to update.
You will however have to make sure to update tools/testing/vma/* to handle the new functions in userland testing (they need to be stubbed otu).
If it makes life easier, you can even send it to me off-list, or just send it without changing this in a respin and I can fix it up fairly quick for you.
Quoted 45 lines
kernel/fork.c +6
-4@@ -493,6 +537,11 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)ASSERT_EXCLUSIVE_WRITER (orig->vm_flags);ASSERT_EXCLUSIVE_WRITER (orig->vm_file);vm_area_init_from (orig, new); + +if (vma_pfnmap_track_ctx_dup (orig, new)) { +kmem_cache_free (vm_area_cachep, new); +return NULL ; + }vma_lock_init (new, true);INIT_LIST_HEAD (&new->anon_vma_chain);vma_numab_state_init (new);@@ -507,6 +556,7 @@ void vm_area_free(struct vm_area_struct *vma)vma_assert_detached (vma);vma_numab_state_free (vma);free_anon_vma_name (vma); +vma_pfnmap_track_ctx_release (vma);kmem_cache_free (vm_area_cachep, vma); }@@ -669,10 +719,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,if (!tmp) goto fail_nomem; -/* track_pfn_copy() will later take care of copying internal state. */ -if (unlikely (tmp->vm_flags &VM_PFNMAP )) -untrack_pfn_clear (tmp); - retval =vma_dup_policy (mpnt, tmp);if (retval) goto fail_nomem_policy;mm/memory.c +1
-1@@ -1370,7 +1370,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)struct mm_struct *dst_mm = dst_vma->vm_mm;struct mm_struct *src_mm = src_vma->vm_mm;struct mmu_notifier_range range; -unsigned long next, pfn =0 ; +unsigned long next;bool is_cow;int ret;
mm/memory.c copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) · line 1388
Quoted 9 lines
mm/memory.c +0
-6@@ -1380,12 +1380,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)if (is_vm_hugetlb_page (src_vma))return copy_hugetlb_page_range (dst_mm, src_mm, dst_vma, src_vma); -if (unlikely (src_vma->vm_flags &VM_PFNMAP )) { - ret =track_pfn_copy (dst_vma, src_vma, &pfn); -if (ret) -return ret; - } -
So lovely to see this kind of thing go...
Quoted 60 lines
mm/memory.c +30
-5/* * We need to invalidate the secondary MMU mappings only when * there could be a permission downgrade on the ptes of the @@ -1427,8 +1421,6 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)raw_write_seqcount_end (&src_mm->write_protect_seq);mmu_notifier_invalidate_range_end (&range); } -if (ret &&unlikely (src_vma->vm_flags &VM_PFNMAP )) -untrack_pfn_copy (dst_vma, pfn);return ret; }@@ -1923,9 +1915,6 @@ static void unmap_single_vma(struct mmu_gather *tlb,if (vma->vm_file)uprobe_munmap (vma, start, end); -if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn (vma,0 ,0 , mm_wr_locked); -if (start != end) {if (unlikely (is_vm_hugetlb_page (vma))) {/* @@ -2871,6 +2860,36 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,return error; } +#ifdef __HAVE_PFNMAP_TRACKING +static inline struct pfnmap_track_ctx *pfnmap_track_ctx_alloc (unsigned long pfn, +unsigned long size,pgprot_t *prot) +{ +struct pfnmap_track_ctx *ctx; + +if (pfnmap_track (pfn, size, prot)) +return ERR_PTR (-EINVAL ); + + ctx =kmalloc (sizeof (*ctx),GFP_KERNEL ); +if (unlikely (!ctx)) { +pfnmap_untrack (pfn, size); +return ERR_PTR (-ENOMEM ); + } + + ctx->pfn = pfn; + ctx->size = size; +kref_init (&ctx->kref); +return ctx; +} + +void pfnmap_track_ctx_release (struct kref *ref) +{ + struct pfnmap_track_ctx *ctx = container_of(ref, struct pfnmap_track_ctx, kref); + +pfnmap_untrack (ctx->pfn, ctx->size); +kfree (ctx); +} +#endif /* __HAVE_PFNMAP_TRACKING */ +/** * remap_pfn_range - remap kernel memory to userspace * @vma: user vma to map to
mm/memory.c int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, · line 2907
Quoted 6 lines
mm/memory.c +1
-0@@ -2883,20 +2902,50 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,* * Return: %0 on success, negative error code otherwise. */ +#ifdef __HAVE_PFNMAP_TRACKINGint remap_pfn_range (struct vm_area_struct *vma,unsigned long addr,unsigned long pfn,unsigned long size,pgprot_t prot)
OK so to expose some of my lack-of-knowledge of PAT - is this the 'entrypoint' to PAT tracking?
So we have some kernel memory we remap to userland as PFN map, the kind that very well might be sensible to use PAT the change cache behaviour for, and each time this happens, it's mapped as PAT?
Quoted 53 lines
mm/memory.c +33
-4{ +struct pfnmap_track_ctx *ctx =NULL ;int err; - err =track_pfn_remap (vma, &prot, pfn, addr,PAGE_ALIGN (size)); -if (err) + size =PAGE_ALIGN (size); + +/* +* If we cover the full VMA, we'll perform actual tracking, and +* remember to untrack when the last reference to our tracking +* context from a VMA goes away. +* +* If we only cover parts of the VMA, we'll only sanitize the +* pgprot. +*/ +if (addr == vma->vm_start && addr + size == vma->vm_end) { +if (vma->pfnmap_track_ctx) +return -EINVAL ; + ctx =pfnmap_track_ctx_alloc (pfn, size, &prot); +if (IS_ERR (ctx)) +return PTR_ERR (ctx); + }else if (pfnmap_sanitize_pgprot (pfn, size, &prot)) {return -EINVAL ; + } err =remap_pfn_range_notrack (vma, addr, pfn, size, prot); -if (err) -untrack_pfn (vma, pfn,PAGE_ALIGN (size), true); +if (ctx) { +if (err) +kref_put (&ctx->kref, pfnmap_track_ctx_release); +else + vma->pfnmap_track_ctx = ctx; + }return err; } + +#else +int remap_pfn_range (struct vm_area_struct *vma,unsigned long addr, +unsigned long pfn,unsigned long size,pgprot_t prot) +{ +return remap_pfn_range_notrack (vma, addr, pfn, size, prot); +} +#endif EXPORT_SYMBOL (remap_pfn_range);/** mm/mremap.c +0
-0
mm/mremap.c static int copy_vma_and_data(struct vma_remap_struct *vrm, · line 1197
Quoted 7 lines
mm/mremap.c +0
-4@@ -1191,10 +1191,6 @@ static int copy_vma_and_data(struct vma_remap_struct *vrm,if (is_vm_hugetlb_page (vma))clear_vma_resv_huge_pages (vma); -/* Tell pfnmap has moved from this vma */ -if (unlikely (vma->vm_flags &VM_PFNMAP )) -untrack_pfn_clear (vma); -
Thanks! <3
Quoted 6 lines
mm/mremap.c +0
-1*new_vma_ptr = new_vma;return err; } --2.49.0
Re: [PATCH v1 07/11] mm: remove VM_PAT
In reply to David HildenbrandQuoted 3 lines · David Hildenbrand
It's unused, so let's remove it.
Signed-off-by David Hildenbrand <[email protected]>
I have only <3 for this patch :) byyyyeee VM_PAT!
Quoted 38 lines
--- include/linux/mm.h | 4 +--- include/trace/events/mmflags.h | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-)include/linux/mm.h +1
-3@@ -357,9 +357,7 @@ extern unsigned int kobjsize(const void *objp);# define VM_SHADOW_STACK VM_NONE#endif -#if defined(CONFIG_X86 ) -# define VM_PAT VM_ARCH_1/* PAT reserves whole VMA at once (x86) */ -#elif defined(CONFIG_PPC64 ) +#if defined(CONFIG_PPC64 )# define VM_SAO VM_ARCH_1/* Strong Access Ordering (powerpc) */ #elif defined(CONFIG_PARISC )# define VM_GROWSUP VM_ARCH_1include/trace/events/mmflags.h +1
-4@@ -172,9 +172,7 @@ IF_HAVE_PG_ARCH_3(arch_3) __def_pageflag_names \ ) : "none" -#if defined(CONFIG_X86 ) -#define __VM_ARCH_SPECIFIC_1 {VM_PAT, "pat" } -#elif defined(CONFIG_PPC64 ) +#if defined(CONFIG_PPC64 )#define __VM_ARCH_SPECIFIC_1 {VM_SAO, "sao" }#elif defined(CONFIG_PARISC )#define __VM_ARCH_SPECIFIC_1 {VM_GROWSUP, "growsup" } --2.49.0
Re: [PATCH v1 06/11] x86/mm/pat: remove old pfnmap tracking interface
In reply to David HildenbrandQuoted 4 lines · David Hildenbrand
We can now get rid of the old interface along with
get_pat_info()andfollow_phys().Signed-off-by David Hildenbrand <[email protected]>
Oh what a glorious glorious screen of red I see before me... deleted code is the best code!
Quoted 265 lines
--- arch/x86/mm/pat/memtype.c | 147 -------------------------------------- include/linux/pgtable.h | 66 ----------------- 2 files changed, 213 deletions(-)arch/x86/mm/pat/memtype.c +0
-147@@ -933,119 +933,6 @@ static void free_pfn_range(u64 paddr, unsigned long size)memtype_free (paddr, paddr + size); } -static int follow_phys (struct vm_area_struct *vma,unsigned long *prot, -resource_size_t *phys) -{ -struct follow_pfnmap_args args = { .vma = vma, .address = vma->vm_start }; - -if (follow_pfnmap_start (&args)) -return -EINVAL ; - -/* Never return PFNs of anon folios in COW mappings. */ -if (!args.special) { -follow_pfnmap_end (&args); -return -EINVAL ; - } - - *prot =pgprot_val (args.pgprot); - *phys = (resource_size_t )args.pfn <<PAGE_SHIFT ; -follow_pfnmap_end (&args); -return 0 ; -} - -static int get_pat_info (struct vm_area_struct *vma,resource_size_t *paddr, -pgprot_t *pgprot) -{ -unsigned long prot; - -VM_WARN_ON_ONCE (!(vma->vm_flags &VM_PAT )); - -/* -* We need the starting PFN and cachemode used for track_pfn_remap() -* that covered the whole VMA. For most mappings, we can obtain that -* information from the page tables. For COW mappings, we might now -* suddenly have anon folios mapped and follow_phys() will fail. -* -* Fallback to using vma->vm_pgoff, see remap_pfn_range_notrack(), to -* detect the PFN. If we need the cachemode as well, we're out of luck -* for now and have to fail fork(). -*/ -if (!follow_phys (vma, &prot, paddr)) { -if (pgprot) - *pgprot =__pgprot (prot); -return 0 ; - } -if (is_cow_mapping (vma->vm_flags)) { -if (pgprot) -return -EINVAL ; - *paddr = (resource_size_t )vma->vm_pgoff <<PAGE_SHIFT ; -return 0 ; - } -WARN_ON_ONCE (1 ); -return -EINVAL ; -} - -int track_pfn_copy (struct vm_area_struct *dst_vma, -struct vm_area_struct *src_vma,unsigned long *pfn) -{ -const unsigned long vma_size = src_vma->vm_end - src_vma->vm_start; -resource_size_t paddr; -pgprot_t pgprot; -int rc; - -if (!(src_vma->vm_flags &VM_PAT )) -return 0 ; - -/* -* Duplicate the PAT information for the dst VMA based on the src -* VMA. -*/ -if (get_pat_info (src_vma, &paddr, &pgprot)) -return -EINVAL ; - rc =reserve_pfn_range (paddr, vma_size, &pgprot,1 ); -if (rc) -return rc; - -/* Reservation for the destination VMA succeeded. */ -vm_flags_set (dst_vma,VM_PAT ); - *pfn =PHYS_PFN (paddr); -return 0 ; -} - -void untrack_pfn_copy (struct vm_area_struct *dst_vma,unsigned long pfn) -{ -untrack_pfn (dst_vma, pfn, dst_vma->vm_end - dst_vma->vm_start, true); -/* -* Reservation was freed, any copied page tables will get cleaned -* up later, but without getting PAT involved again. -*/ -} - -/* -* prot is passed in as a parameter for the new mapping. If the vma has -* a linear pfn mapping for the entire range, or no vma is provided, -* reserve the entire pfn + size range with single reserve_pfn_range -* call. -*/ -int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot, -unsigned long pfn,unsigned long addr,unsigned long size) -{ -resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; - -/* reserve the whole chunk starting from paddr */ -if (!vma || (addr == vma->vm_start - && size == (vma->vm_end - vma->vm_start))) { -int ret; - - ret =reserve_pfn_range (paddr, size, prot,0 ); -if (ret ==0 && vma) -vm_flags_set (vma,VM_PAT ); -return ret; - } - -return pfnmap_sanitize_pgprot (pfn, size, prot); -} -int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size,pgprot_t *prot) {resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ;@@ -1082,40 +969,6 @@ void pfnmap_untrack(unsigned long pfn, unsigned long size)free_pfn_range (paddr, size); } -/* -* untrack_pfn is called while unmapping a pfnmap for a region. -* untrack can be called for a specific region indicated by pfn and size or -* can be for the entire vma (in which case pfn, size are zero). -*/ -void untrack_pfn (struct vm_area_struct *vma,unsigned long pfn, -unsigned long size,bool mm_wr_locked) -{ -resource_size_t paddr; - -if (vma && !(vma->vm_flags &VM_PAT )) -return ; - -/* free the chunk starting from pfn or the whole chunk */ - paddr = (resource_size_t )pfn <<PAGE_SHIFT ; -if (!paddr && !size) { -if (get_pat_info (vma, &paddr,NULL )) -return ; - size = vma->vm_end - vma->vm_start; - } -free_pfn_range (paddr, size); -if (vma) { -if (mm_wr_locked) -vm_flags_clear (vma,VM_PAT ); -else -__vm_flags_mod (vma,0 ,VM_PAT ); - } -} - -void untrack_pfn_clear (struct vm_area_struct *vma) -{ -vm_flags_clear (vma,VM_PAT ); -} -pgprot_t pgprot_writecombine (pgprot_t prot) {pgprot_set_cachemode (&prot, _PAGE_CACHE_MODE_WC);include/linux/pgtable.h +0
-67@@ -1489,17 +1489,6 @@ static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd)* vmf_insert_pfn. */ -/* -* track_pfn_remap is called when a _new_ pfn mapping is being established -* by remap_pfn_range() for physical range indicated by pfn and size. -*/ -static inline int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot, -unsigned long pfn,unsigned long addr, -unsigned long size) -{ -return 0 ; -} -static inline int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size,pgprot_t *prot) {@@ -1515,55 +1504,7 @@ static inline int pfnmap_track(unsigned long pfn, unsigned long size,static inline void pfnmap_untrack (unsigned long pfn,unsigned long size) { } - -/* -* track_pfn_copy is called when a VM_PFNMAP VMA is about to get the page -* tables copied during copy_page_range(). Will store the pfn to be -* passed to untrack_pfn_copy() only if there is something to be untracked. -* Callers should initialize the pfn to 0. -*/ -static inline int track_pfn_copy (struct vm_area_struct *dst_vma, -struct vm_area_struct *src_vma,unsigned long *pfn) -{ -return 0 ; -} - -/* -* untrack_pfn_copy is called when a VM_PFNMAP VMA failed to copy during -* copy_page_range(), but after track_pfn_copy() was already called. Can -* be called even if track_pfn_copy() did not actually track anything: -* handled internally. -*/ -static inline void untrack_pfn_copy (struct vm_area_struct *dst_vma, -unsigned long pfn) -{ -} - -/* -* untrack_pfn is called while unmapping a pfnmap for a region. -* untrack can be called for a specific region indicated by pfn and size or -* can be for the entire vma (in which case pfn, size are zero). -*/ -static inline void untrack_pfn (struct vm_area_struct *vma, -unsigned long pfn,unsigned long size, -bool mm_wr_locked) -{ -} - -/* -* untrack_pfn_clear is called in the following cases on a VM_PFNMAP VMA: -* -* 1) During mremap() on the src VMA after the page tables were moved. -* 2) During fork() on the dst VMA, immediately after duplicating the src VMA. -*/ -static inline void untrack_pfn_clear (struct vm_area_struct *vma) -{ -}#else -extern int track_pfn_remap (struct vm_area_struct *vma,pgprot_t *prot, -unsigned long pfn,unsigned long addr, -unsigned long size); -/** * pfnmap_sanitize_pgprot - sanitize the pgprot for a pfn range * @pfn: the start of the pfn range @@ -1603,13 +1544,6 @@ int pfnmap_track(unsigned long pfn, unsigned long size, pgprot_t *prot);* un-doing any reservation. */ void pfnmap_untrack (unsigned long pfn,unsigned long size); -extern int track_pfn_copy (struct vm_area_struct *dst_vma, -struct vm_area_struct *src_vma,unsigned long *pfn); -extern void untrack_pfn_copy (struct vm_area_struct *dst_vma, -unsigned long pfn); -extern void untrack_pfn (struct vm_area_struct *vma,unsigned long pfn, -unsigned long size,bool mm_wr_locked); -extern void untrack_pfn_clear (struct vm_area_struct *vma);#endif #ifdef CONFIG_MMU --2.49.0
Re: [PATCH v1 08/11] x86/mm/pat: remove strict_prot parameter from reserve_pfn_range()
In reply to David HildenbrandQuoted 3 lines · David Hildenbrand
Always set to 0, so let's remove it.
Signed-off-by David Hildenbrand <[email protected]>
Ah yes here is where you remove it :)
Quoted 51 lines
--- arch/x86/mm/pat/memtype.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)arch/x86/mm/pat/memtype.c +3
-10@@ -858,8 +858,7 @@ int memtype_kernel_map_sync(u64 base, unsigned long size,* Reserved non RAM regions only and after successful memtype_reserve, * this func also keeps identity mapping (if any) in sync with this new prot. */ -static int reserve_pfn_range (u64 paddr,unsigned long size,pgprot_t *vma_prot, -int strict_prot) +static int reserve_pfn_range (u64 paddr,unsigned long size,pgprot_t *vma_prot) {int is_ram =0 ;int ret;@@ -895,8 +894,7 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot,return ret;if (pcm != want_pcm) { -if (strict_prot || - !is_new_memtype_allowed (paddr, size, want_pcm, pcm)) { +if (!is_new_memtype_allowed (paddr, size, want_pcm, pcm)) {memtype_free (paddr, paddr + size); pr_err("x86/PAT: %s:%d map pfn expected mapping type %s for [mem %#010Lx-%#010Lx], got %s\n", current->comm, current->pid,@@ -906,10 +904,6 @@ static int reserve_pfn_range(u64 paddr, unsigned long size, pgprot_t *vma_prot, cattr_name(pcm));return -EINVAL ; } -/* -* We allow returning different type than the one requested in -* non strict case. -*/ pgprot_set_cachemode (vma_prot, pcm); }@@ -959,7 +953,7 @@ int pfnmap_track(unsigned long pfn, unsigned long size, pgprot_t *prot) {const resource_size_t paddr = (resource_size_t )pfn <<PAGE_SHIFT ; -return reserve_pfn_range (paddr, size, prot,0 ); +return reserve_pfn_range (paddr, size, prot); }void pfnmap_untrack (unsigned long pfn,unsigned long size) --2.49.0
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Lorenzo StoakesQuoted 30 lines · Lorenzo Stoakes
Quoted 17 lines · David Hildenbrand
Quoted 8 lines · Peter Xu
Quoted 6 lines · David Hildenbrand
Quoted 3 lines
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Note that I enabled everything that you would expect on a production system (incld. userfaultfd, mempolicy, per-vma locks), so I didn't enable lockep.
Quoted 7 lines
I still doubt whether you at least enabled userfaultfd, e.g., your previous paste has:
struct
vm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 0 */Not something that matters.. but just in case you didn't use the expected config file you wanted to use..
Quoted 6 lines
You're absolutely right. I only briefly rechecked for this purpose here on my notebook, and only looked for the existence of members, not expecting that we have confusing stuff like
vm_userfaultfd_ctx.I checked again and the size stays at 192 with allyesconfig and then disabling debug options.
I think a reasonable case is everything on, except
CONFIG_DEBUG_LOCK_ALLOCand I don't care about nommu.So:
Quoted 8 lines
CONFIG_PER_VMA_LOCKCONFIG_SWAPCONFIG_MMU(exclude the nommuvm_regionfield)CONFIG_NUMACONFIG_NUMA_BALANCINGCONFIG_PER_VMA_LOCKCONFIG_ANON_VMA_NAME__HAVE_PFNMAP_TRACKING
Yes.
And our ugly friend CONFIG_USERFAULTFD
that is
struct vm_userfaultfd_ctx {
struct userfaultfd_ctx *ctx;
};#else /* CONFIG_USERFAULTFD */
#define NULL_VM_UFFD_CTX ((struct vm_userfaultfd_ctx) {}) struct vm_userfaultfd_ctx {};
#endif /* CONFIG_USERFAULTFD */
(yes, you made the same mistake as I made when skimming if everything relevant was enabled)
Quoted 3 lines
So to be clear - allyesconfig w/o debug gives us this yes? And we don't add a cache line? In which case all good :)
Looks like it!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Suren BaghdasaryanQuoted 5 lines · Suren Baghdasaryan
Quoted 3 lines · Lorenzo Stoakes
+cc Suren, who has worked HEAVILY on VMA field manipulation and such :)
Quoted 7 lines
Quoted 4 lines
Suren - David is proposing adding a new field. AFAICT this does not add a new cache line so I think we're all good.
But FYI!
Thanks! Yes, there should be some space in the last cacheline after my last field reshuffling.
That explains why -- that last time I looked at this -- there was no easy space available. Thanks for that!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 09/11] x86/mm/pat: remove MEMTYPE_*_MATCH
In reply to David HildenbrandQuoted 2 lines · David Hildenbrand
The "
memramp()shrinking" scenario no longer applies, so let's remove that now-unnecessary handling.
I wonder if we could remove even more of the code here given the simplifications here? But not a big deal.
Quoted 2 lines
Signed-off-by David Hildenbrand <[email protected]>
More lovely removal...
Quoted 77 lines
--- arch/x86/mm/pat/memtype_interval.c | 44 ++++-------------------------- 1 file changed, 6 insertions(+), 38 deletions(-)arch/x86/mm/pat/memtype_interval.c +6
-39@@ -49,26 +49,15 @@ INTERVAL_TREE_DEFINE(struct memtype, rb, u64, subtree_max_end,static struct rb_root_cached memtype_rbroot =RB_ROOT_CACHED ; -enum { -MEMTYPE_EXACT_MATCH =0 , -MEMTYPE_END_MATCH =1 -}; - -static struct memtype *memtype_match (u64 start,u64 end,int match_type) +static struct memtype *memtype_match (u64 start,u64 end) {struct memtype *entry_match; entry_match =interval_iter_first (&memtype_rbroot, start, end-1 );while (entry_match !=NULL && entry_match->start < end) { -if ((match_type ==MEMTYPE_EXACT_MATCH ) && - (entry_match->start == start) && (entry_match->end == end)) -return entry_match; - -if ((match_type ==MEMTYPE_END_MATCH ) && - (entry_match->start < start) && (entry_match->end == end)) +if (entry_match->start == start && entry_match->end == end)return entry_match; - entry_match =interval_iter_next (entry_match, start, end-1 ); }@@ -132,32 +121,11 @@ struct memtype *memtype_erase(u64 start, u64 end) {struct memtype *entry_old; -/* -* Since the memtype_rbroot tree allows overlapping ranges, -* memtype_erase() checks with EXACT_MATCH first, i.e. free -* a whole node for the munmap case. If no such entry is found, -* it then checks with END_MATCH, i.e. shrink the size of a node -* from the end for the mremap case. -*/ - entry_old =memtype_match (start, end,MEMTYPE_EXACT_MATCH ); -if (!entry_old) { - entry_old =memtype_match (start, end,MEMTYPE_END_MATCH ); -if (!entry_old) -return ERR_PTR (-EINVAL ); - } - -if (entry_old->start == start) { -/* munmap: erase this node */ -interval_remove (entry_old, &memtype_rbroot); - }else { -/* mremap: update the end value of this node */ -interval_remove (entry_old, &memtype_rbroot); - entry_old->end = start; -interval_insert (entry_old, &memtype_rbroot); - -return NULL ; - } + entry_old =memtype_match (start, end); +if (!entry_old) +return ERR_PTR (-EINVAL ); +interval_remove (entry_old, &memtype_rbroot);return entry_old; } --2.49.0
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Suren BaghdasaryanQuoted 28 lines · Suren Baghdasaryan
Quoted 26 lines · Lorenzo Stoakes
Quoted 23 lines · David Hildenbrand
Quoted 8 lines · Peter Xu
Quoted 6 lines · David Hildenbrand
Quoted 3 lines
Probably due to what config you have. E.g., when I'm looking mine it's much bigger and already consuming 256B, but it's because I enabled more things (userfaultfd, lockdep, etc.).
Note that I enabled everything that you would expect on a production system (incld. userfaultfd, mempolicy, per-vma locks), so I didn't enable lockep.
Quoted 7 lines
I still doubt whether you at least enabled userfaultfd, e.g., your previous paste has:
struct
vm_userfaultfd_ctxvm_userfaultfd_ctx; /* 176 0 */Not something that matters.. but just in case you didn't use the expected config file you wanted to use..
You're absolutely right. I only briefly rechecked for this purpose here on my notebook, and only looked for the existence of members, not expecting that we have confusing stuff like
vm_userfaultfd_ctx.I checked again and the size stays at 192 with allyesconfig and then disabling debug options.
Quoted 6 lines
Quoted 2 lines
I think a reasonable case is everything on, except
CONFIG_DEBUG_LOCK_ALLOCand I don't care about nommu.I think it's safe to assume that production systems would disable lockdep due to the performance overhead. At least that's what we do on Android - enable it on development branches but disable in production.
Right, and "struct lockdep_map" is ... significantly larger than 8 bytes. With that enabled, one is already paying for extra VMA space ...
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 10/11] drm/i915: track_pfn() -> "pfnmap tracking"
In reply to David HildenbrandQuoted 4 lines · David Hildenbrand
track_pfn()does not exist, let's simply refer to it as "pfnmap tracking".Signed-off-by David Hildenbrand <[email protected]>
Quoted 29 lines
--- drivers/gpu/drm/i915/i915_mm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/gpu/drm/i915/i915_mm.c +2
-3@@ -100,7 +100,7 @@ int remap_io_mapping(struct vm_area_struct *vma,GEM_BUG_ON ((vma->vm_flags &EXPECTED_FLAGS ) !=EXPECTED_FLAGS ); -/* We rely on prevalidation of the io-mapping to skip track_pfn(). */ +/* We rely on prevalidation of the io-mapping to skip pfnmap tracking. */ r.mm = vma->vm_mm; r.pfn = pfn; r.prot = __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |@@ -140,7 +140,7 @@ int remap_io_sg(struct vm_area_struct *vma, };int err; -/* We rely on prevalidation of the io-mapping to skip track_pfn(). */ +/* We rely on prevalidation of the io-mapping to skip pfnmap tracking. */ GEM_BUG_ON ((vma->vm_flags &EXPECTED_FLAGS ) !=EXPECTED_FLAGS );while (offset >= r.sgt.max >>PAGE_SHIFT ) { --2.49.0
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
In reply to Peter XuQuoted 17 lines · Peter Xu
Quoted 16 lines · David Hildenbrand
Quoted 4 lines
Quoted 4 lines
What it does on PAT (only implementation so far ...) is looking up the memory type to select the caching mode that can be use.
Quoted 8 lines
Quoted 4 lines
"sanitize" was IMHO a good fit, because we must make sure that we don't use the wrong caching mode.
update/setup/... don't make that quite clear. Any other suggestions?
I'm very poor on naming.. :( So far anything seems slightly better than sanitize to me, as the word "sanitize" is actually also used in
memtype.cfor other purpose.. seesanitize_phys().Sure, one can sanitize a lot of things. Here it's the cachemode/pgrpot, in the other functions it's an address.
Quoted 7 lines
Quoted 5 lines
Likely we should just call it pfnmap_X_cachemode()/
Set/update don't really fit for X in case
pfnmap_X_cachemode()is a NOP.
pfnmap_setup_cachemode()? Hm.Sounds good here.
Okay, I'll use that one. If ever something else besides PAT would require different semantics, they can bother with finding a better name :)
Quoted 71 lines
Quoted 59 lines
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Quoted 27 lines
+ * @pfn: the start of the pfn range
+ * @size: the size of the pfn range
+ * @prot: the pgprot to sanitize
+ *
+ * Sanitize the given pgprot for a pfn range, for example, adjusting the
+ * cachemode.
+ *
+ * This function cannot fail for a single page, but can fail for multiple
+ * pages.
+ *
+ * Returns 0 on success and-EINVALon error.
+ */include/linux/pgtable.h +2
-0+int pfnmap_sanitize_pgprot (unsigned long pfn,unsigned long size, +pgprot_t *prot);extern int track_pfn_copy (struct vm_area_struct *dst_vma,struct vm_area_struct *src_vma,unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1458
Quoted 5 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.Maybe we could have a "void
pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replacetrack_pfn_insert()and never fail? Dropping vma ref is definitely a win already in all cases.Quoted 9 lines
Quoted 6 lines
It could be a simple wrapper around
pfnmap_sanitize_pgprot(), yes. That's certainly helpful for the single-page case.Regarding never failing here: we should check the whole range. We have to make sure that none of the pages has a memory type / caching mode that is incompatible with what we setup.
Would it happen in real world?
Quoted 1 line
IIUC per-vma registration needs to happen first, which checks for
memtype
Quoted 5 lines
conflicts in the first place, or
reserve_pfn_range()could already have failed.Quoted 1 line
Here it's the fault path looking up the memtype, so I would expect it is
guaranteed all pfns under the same vma is following the verified (and same) memtype?
The whole point of
track_pfn_insert()is that it is used when we *don't* use reserve_pfn_range()->track_pfn_remap(), no?Quoted 6 lines
track_pfn_remap()would check the whole range that gets mapped, sotrack_pfn_insert()user must similarly check the whole range that gets mapped.Note that even
track_pfn_insert()is already pretty clear on the intended
usage: "called when a _new_ single pfn is established"We need to define "new" then.. But I agree it's not crystal clear at least. I think I just wasn't the first to assume it was reserved, see this (especially, the "Expectation" part..):
commit
5180da410db6369d1f95c9014da1c9bc33fb043e
Quoted 8 lines
Author: Suresh Siddha <[email protected]> Date: Mon Oct 8 16:28:29 2012 -0700x86, pat: separate the pfn attribute tracking for
remap_pfn_rangeandvm_insert_pfnWith PAT enabled,
vm_insert_pfn()looks up the existing pfn memory attribute and uses it. Expectation is that the driver reserves the memory attributes for the pfn before callingvm_insert_pfn().
It's all confusing.
We do have the following functions relevant in pat code:
(1) memtype_reserve(): used by ioremap and set_memory_XX
(2) memtype_reserve_io(): used by iomap
(3) reserve_pfn_range(): only remap_pfn_range() calls it
(4) arch_io_reserve_memtype_wc()
Which one would perform the reservation for, say, vfio?
I agree that if there would be a guarantee/expectation that all PFNs have the same memtype (from previous reservation), it would be sufficient to check a single PFN, and we could document that. I just don't easily see where that reservation is happening.
So a pointer to that would be appreciated!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
Quoted 114 lines · David Hildenbrand
Quoted 17 lines · Peter Xu
Quoted 16 lines · David Hildenbrand
Quoted 4 lines
Quoted 4 lines
What it does on PAT (only implementation so far ...) is looking up the memory type to select the caching mode that can be use.
Quoted 8 lines
Quoted 4 lines
"sanitize" was IMHO a good fit, because we must make sure that we don't use the wrong caching mode.
update/setup/... don't make that quite clear. Any other suggestions?
I'm very poor on naming.. :( So far anything seems slightly better than sanitize to me, as the word "sanitize" is actually also used in
memtype.cfor other purpose.. seesanitize_phys().Sure, one can sanitize a lot of things. Here it's the cachemode/pgrpot, in the other functions it's an address.
Quoted 7 lines
Quoted 5 lines
Likely we should just call it pfnmap_X_cachemode()/
Set/update don't really fit for X in case
pfnmap_X_cachemode()is a NOP.
pfnmap_setup_cachemode()? Hm.Sounds good here.
Okay, I'll use that one. If ever something else besides PAT would require different semantics, they can bother with finding a better name :)
Quoted 70 lines
Quoted 59 lines
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Quoted 27 lines
+ * @pfn: the start of the pfn range
+ * @size: the size of the pfn range
+ * @prot: the pgprot to sanitize
+ *
+ * Sanitize the given pgprot for a pfn range, for example, adjusting the
+ * cachemode.
+ *
+ * This function cannot fail for a single page, but can fail for multiple
+ * pages.
+ *
+ * Returns 0 on success and-EINVALon error.
+ */(quoted diff) +2
-0+int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size, + pgprot_t *prot); extern int track_pfn_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1458
Quoted 5 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.Maybe we could have a "void
pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replacetrack_pfn_insert()and never fail? Dropping vma ref is definitely a win already in all cases.Quoted 9 lines
Quoted 6 lines
It could be a simple wrapper around
pfnmap_sanitize_pgprot(), yes. That's certainly helpful for the single-page case.Regarding never failing here: we should check the whole range. We have to make sure that none of the pages has a memory type / caching mode that is incompatible with what we setup.
Would it happen in real world?
Quoted 1 line
IIUC per-vma registration needs to happen first, which checks for
memtype
Quoted 5 lines
conflicts in the first place, or
reserve_pfn_range()could already have failed.Quoted 1 line
Here it's the fault path looking up the memtype, so I would expect it is
guaranteed all pfns under the same vma is following the verified (and same) memtype?
The whole point of
track_pfn_insert()is that it is used when we *don't* use reserve_pfn_range()->track_pfn_remap(), no?Quoted 6 lines
track_pfn_remap()would check the whole range that gets mapped, sotrack_pfn_insert()user must similarly check the whole range that gets mapped.Note that even
track_pfn_insert()is already pretty clear on the intended
usage: "called when a _new_ single pfn is established"We need to define "new" then.. But I agree it's not crystal clear at least. I think I just wasn't the first to assume it was reserved, see this (especially, the "Expectation" part..):
Quoted 8 lines
commit
5180da410db6369d1f95c9014da1c9bc33fb043eAuthor: Suresh Siddha <[email protected]> Date: Mon Oct 8 16:28:29 2012 -0700x86, pat: separate the pfn attribute tracking for
remap_pfn_rangeandvm_insert_pfnWith PAT enabled,vm_insert_pfn()looks up the existing pfn memory attribute and uses it. Expectation is that the driver reserves the memory attributes for the pfn before callingvm_insert_pfn().It's all confusing.
We do have the following functions relevant in pat code:
(1)
memtype_reserve(): used by ioremap andset_memory_XX
Quoted 8 lines
(2)
memtype_reserve_io(): used by iomap(3)
reserve_pfn_range(): onlyremap_pfn_range()calls it(4)
arch_io_reserve_memtype_wc()Which one would perform the reservation for, say, vfio?
My understanding is it was done via barmap. See this stack:
vfio_pci_core_mmap
pci_iomap
pci_iomap_range
...
__ioremap_caller
memtype_reserveQuoted 8 lines
I agree that if there would be a guarantee/expectation that all PFNs have the same memtype (from previous reservation), it would be sufficient to check a single PFN, and we could document that. I just don't easily see where that reservation is happening.
So a pointer to that would be appreciated!
I am not aware of any pointer.. maybe others could chime in.
IMHO, if there's anything uncertain, for this one we could always decouple this issue from the core issue you're working on, so at least it keeps the old behavior (which is pure lookup on pfn injections) until a solid issue occurs? It avoids the case where we could introduce unnecessary code but then it's much harder to justify a removal. What do you think?
Thanks,
Signature · 1 line
Peter Xu
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
Quoted 142 lines · Peter Xu
Quoted 114 lines · David Hildenbrand
Quoted 17 lines · Peter Xu
Quoted 16 lines · David Hildenbrand
Quoted 4 lines
Quoted 4 lines
What it does on PAT (only implementation so far ...) is looking up the memory type to select the caching mode that can be use.
Quoted 8 lines
Quoted 4 lines
"sanitize" was IMHO a good fit, because we must make sure that we don't use the wrong caching mode.
update/setup/... don't make that quite clear. Any other suggestions?
I'm very poor on naming.. :( So far anything seems slightly better than sanitize to me, as the word "sanitize" is actually also used in
memtype.cfor other purpose.. seesanitize_phys().Sure, one can sanitize a lot of things. Here it's the cachemode/pgrpot, in the other functions it's an address.
Quoted 7 lines
Quoted 5 lines
Likely we should just call it pfnmap_X_cachemode()/
Set/update don't really fit for X in case
pfnmap_X_cachemode()is a NOP.
pfnmap_setup_cachemode()? Hm.Sounds good here.
Okay, I'll use that one. If ever something else besides PAT would require different semantics, they can bother with finding a better name :)
Quoted 70 lines
Quoted 59 lines
Quoted 40 lines
Quoted 40 lines
Quoted 39 lines
Quoted 27 lines
+ * @pfn: the start of the pfn range
+ * @size: the size of the pfn range
+ * @prot: the pgprot to sanitize
+ *
+ * Sanitize the given pgprot for a pfn range, for example, adjusting the
+ * cachemode.
+ *
+ * This function cannot fail for a single page, but can fail for multiple
+ * pages.
+ *
+ * Returns 0 on success and-EINVALon error.
+ */(quoted diff) +2
-0+int pfnmap_sanitize_pgprot(unsigned long pfn, unsigned long size, + pgprot_t *prot); extern int track_pfn_copy(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, unsigned long *pfn); extern void untrack_pfn_copy(struct vm_area_struct *dst_vma,mm/huge_memory.c +0
-0mm/huge_memory.c vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write) · line 1458
Quoted 5 lines
mm/huge_memory.c +2
-1@@ -1455,7 +1455,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)return VM_FAULT_OOM ; } -track_pfn_insert (vma, &pgprot, pfn); +if (pfnmap_sanitize_pgprot (pfn_t_to_pfn (pfn),PAGE_SIZE , &pgprot)) +return VM_FAULT_FALLBACK ;Would "pgtable" leak if it fails? If it's
PAGE_SIZE, IIUC it won't ever trigger, though.Maybe we could have a "void
pfnmap_sanitize_pgprot_pfn(&pgprot, pfn)" to replacetrack_pfn_insert()and never fail? Dropping vma ref is definitely a win already in all cases.Quoted 9 lines
Quoted 6 lines
It could be a simple wrapper around
pfnmap_sanitize_pgprot(), yes. That's certainly helpful for the single-page case.Regarding never failing here: we should check the whole range. We have to make sure that none of the pages has a memory type / caching mode that is incompatible with what we setup.
Would it happen in real world?
Quoted 1 line
IIUC per-vma registration needs to happen first, which checks for
memtype
Quoted 5 lines
conflicts in the first place, or
reserve_pfn_range()could already have failed.Quoted 1 line
Here it's the fault path looking up the memtype, so I would expect it is
guaranteed all pfns under the same vma is following the verified (and same) memtype?
The whole point of
track_pfn_insert()is that it is used when we *don't* use reserve_pfn_range()->track_pfn_remap(), no?Quoted 6 lines
track_pfn_remap()would check the whole range that gets mapped, sotrack_pfn_insert()user must similarly check the whole range that gets mapped.Note that even
track_pfn_insert()is already pretty clear on the intended
usage: "called when a _new_ single pfn is established"We need to define "new" then.. But I agree it's not crystal clear at least. I think I just wasn't the first to assume it was reserved, see this (especially, the "Expectation" part..):
Quoted 8 lines
commit
5180da410db6369d1f95c9014da1c9bc33fb043eAuthor: Suresh Siddha <[email protected]> Date: Mon Oct 8 16:28:29 2012 -0700x86, pat: separate the pfn attribute tracking for
remap_pfn_rangeandvm_insert_pfnWith PAT enabled,vm_insert_pfn()looks up the existing pfn memory attribute and uses it. Expectation is that the driver reserves the memory attributes for the pfn before callingvm_insert_pfn().It's all confusing.
We do have the following functions relevant in pat code:
(1)
memtype_reserve(): used by ioremap andset_memory_XXQuoted 8 lines
(2)
memtype_reserve_io(): used by iomap(3)
reserve_pfn_range(): onlyremap_pfn_range()calls it(4)
arch_io_reserve_memtype_wc()Which one would perform the reservation for, say, vfio?
My understanding is it was done via barmap. See this stack:
vfio_pci_core_mmap
pci_iomappci_iomap_range ... __ioremap_caller memtype_reserveQuoted 8 lines
I agree that if there would be a guarantee/expectation that all PFNs have the same memtype (from previous reservation), it would be sufficient to check a single PFN, and we could document that. I just don't easily see where that reservation is happening.
So a pointer to that would be appreciated!
Quoted 7 lines
I am not aware of any pointer.. maybe others could chime in.
IMHO, if there's anything uncertain, for this one we could always decouple this issue from the core issue you're working on, so at least it keeps the old behavior (which is pure lookup on pfn injections) until a solid issue occurs? It avoids the case where we could introduce unnecessary code but then it's much harder to justify a removal. What do you think?
I'll use the _pfn variant and document the behavior.
I do wonder why we even have to lookup the memtype again if the caller apparently reserved it (which implied checking it). All a bit weird.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 02/11] mm: convert track_pfn_insert() to pfnmap_sanitize_pgprot()
Quoted 2 lines · David Hildenbrand
I do wonder why we even have to lookup the memtype again if the caller apparently reserved it (which implied checking it). All a bit weird.
Maybe it's because the memtype info isn't always visible to the upper layers, e.g. default pci_iomap() for MMIOs doesn't need to specify anything on cache mode. There's some pci_iomap_wc() variance, but still looks like only the internal has full control..
Signature · 1 line
Peter Xu
Re: [PATCH v1 09/11] x86/mm/pat: remove MEMTYPE_*_MATCH
In reply to Lorenzo StoakesQuoted 6 lines · Lorenzo Stoakes
Quoted 2 lines · David Hildenbrand
The "
memramp()shrinking" scenario no longer applies, so let's remove that now-unnecessary handling.I wonder if we could remove even more of the code here given the simplifications here? But not a big deal.
It might make sense to inline memtype_match().
arch/x86/mm/pat/memtype_interval.c +10 -23
@@ -49,21 +49,6 @@ INTERVAL_TREE_DEFINE(struct memtype, rb, u64, subtree_max_end,static struct rb_root_cached memtype_rbroot =RB_ROOT_CACHED ; -static struct memtype *memtype_match (u64 start,u64 end) -{ -struct memtype *entry_match; - - entry_match =interval_iter_first (&memtype_rbroot, start, end-1 ); - -while (entry_match !=NULL && entry_match->start < end) { -if (entry_match->start == start && entry_match->end == end) -return entry_match; - entry_match =interval_iter_next (entry_match, start, end-1 ); - } - -return NULL ;/* Returns NULL if there is no match */ -} -static int memtype_check_conflict (u64 start,u64 end,enum page_cache_mode reqtype,enum page_cache_mode *newtype)
@@ -119,14 +104,16 @@ int memtype_check_insert(struct memtype *entry_new, enum page_cache_mode *ret_tystruct memtype *memtype_erase (u64 start,u64 end) { -struct memtype *entry_old; - - entry_old =memtype_match (start, end); -if (!entry_old) -return ERR_PTR (-EINVAL ); - -interval_remove (entry_old, &memtype_rbroot); -return entry_old; +struct memtype *entry =interval_iter_first (&memtype_rbroot, start, end -1 ); + +while (entry && entry->start < end) { +if (entry->start == start && entry->end == end) { +interval_remove (entry, &memtype_rbroot); +return entry; + } + entry =interval_iter_next (entry, start, end -1 ); + } +return ERR_PTR (-EINVAL ); }struct memtype *memtype_lookup (u64 addr)
Thanks for all the review!
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to Lorenzo StoakesQuoted 11 lines
Quoted 7 lines
This change implies that we'll keep tracking the original PFN range even after splitting + partially unmapping it: not too bad, because it was not working reliably before. The only thing that kind-of worked before was shrinking such a mapping using
mremap(): we managed to adjust the reservation in a hacky way, now we won't adjust the reservation but leave it around until all involved VMAs are gone.Hm, but what if we shrink a VMA, then map another one, might it be incorrectly storing PAT attributes for part of the range that is now mapped elsewhere?
Not "incorrectly". We'll simply undo the reservation of the cachemode for the original PFN range once everything of the original VMA is gone.
AFAIK, one can usually mmap() the "unmapped" part after shrinking again with the same cachemode, which should be the main use case.
Supporting partial un-tracking will require hooking into vma splitting code ... not something I am super happy about. :)
Quoted 4 lines
Also my god re: the 'kind of working' aspects of PAT, so frustrating.
Quoted 1 line
Quoted 8 lines
Quoted 1 line
Signed-off-by David Hildenbrand <[email protected]>Generally looking good, afaict, but maybe let's get some input from Suren on VMA size.
Are there actually any PAT tests out here? I had a quick glance in
tools/testing/selftests/x86,mmand couldn't find any, but didn't look _that_ card.
Heh, booting a simple VM gets PAT involved. I suspect because /dev/mem and BIOS/GPU/whatever hacks.
In the cover letter I have
"Briefly tested with some basic /dev/mem test I crafted. I want to convert them to selftests, but that might or might not require a bit of more work (e.g., /dev/mem accessibility)."
Quoted 43 lines
Thanks in general for tackling this, this is a big improvement!
Quoted 26 lines
--- include/linux/mm_inline.h | 2 + include/linux/mm_types.h | 11 ++++++ kernel/fork.c | 54 ++++++++++++++++++++++++-- mm/memory.c | 81 +++++++++++++++++++++++++++++++-------- mm/mremap.c | 4 -- 5 files changed, 128 insertions(+), 24 deletions(-)include/linux/mm_inline.h +2
-0@@ -447,6 +447,8 @@ static inline bool anon_vma_name_eq(struct anon_vma_name *anon_name1,#endif /* CONFIG_ANON_VMA_NAME */ +void pfnmap_track_ctx_release (struct kref *ref); +static inline void init_tlb_flush_pending (struct mm_struct *mm) {atomic_set (&mm->tlb_flush_pending,0 );include/linux/mm_types.h +0
-0include/linux/mm_types.h struct vma_numab_state { · line 771
Quoted 8 lines
include/linux/mm_types.h +5
-0@@ -764,6 +764,14 @@ struct vma_numab_state {int prev_scan_seq; }; +#ifdef __HAVE_PFNMAP_TRACKING +struct pfnmap_track_ctx { + struct kref kref; + unsigned long pfn; + unsigned long size;Again, (super) nitty, but we really should express units. I suppose 'size' implies bytes to be honest as you'd unlikely say 'size' for number of pages (you'd go with
nr_pagesor something). But maybe a trailing /* in bytes */ would help.
Quoted 1 line
Not a big deal though!
"size" in the kernel is usually bytes, never pages ... but I might be wrong.
Anyhow, I can use "/* in bytes*/" here, although I doubt that many will benefit from this comment :)
Quoted 16 lines
include/linux/mm_types.h struct vm_area_struct { · line 888
Quoted 12 lines
include/linux/mm_types.h +4
-0+}; +#endif +/* * This struct describes a virtual memory area. There is one of these * per VM-area/task. A VM area is any part of the process virtual memory @@ -877,6 +885,9 @@ struct vm_area_struct {struct anon_vma_name *anon_name;#endif struct vm_userfaultfd_ctx vm_userfaultfd_ctx; +#ifdef __HAVE_PFNMAP_TRACKINGAn aside, but absolutely hate '
__HAVE_PFNMAP_TRACKING' as a name here. But you didn't create it, and it's not really sensible to change it in this series so. Just a rumble...
I cannot argue with that ... same here.
To be clear: I hate all of this with passion ;) With this series, I hate it a bit less.
[...]
Quoted 6 lines
Obviously my series will break this but should be _fairly_ trivial to update.
You will however have to make sure to update
tools/testing/vma/*to handle the new functions in userland testing (they need to be stubbed otu).
Ah, I was happy it compiled but looks like I'll have to mess with that as well.
Quoted 4 lines
If it makes life easier, you can even send it to me off-list, or just send it without changing this in a respin and I can fix it up fairly quick for you.
Let me give it a try first, I'll let you know if it takes me too long.
Thanks!
[...]
Quoted 5 lines
Quoted 5 lines
include/linux/mm_types.h +0
-0/** * remap_pfn_range - remap kernel memory to userspace * @vma: user vma to map to @@ -2883,20 +2902,50 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr,*
Quoted 8 lines
include/linux/mm_types.h int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, · line 2907
Quoted 5 lines
include/linux/mm_types.h +1
-0* Return: %0 on success, negative error code otherwise. */ +#ifdef __HAVE_PFNMAP_TRACKINGint remap_pfn_range (struct vm_area_struct *vma,unsigned long addr,unsigned long pfn,unsigned long size,pgprot_t prot)OK so to expose some of my lack-of-knowledge of PAT - is this the 'entrypoint' to PAT tracking?
Only if you're using remap_pfn_range() ... there is other low-level tracking/reservation using the memtype_reserve() interface and friends.
Quoted 4 lines
So we have some kernel memory we remap to userland as PFN map, the kind that very well might be sensible to use PAT the change cache behaviour for, and each time this happens, it's mapped as PAT?
Right, anytime someone uses remap_pfn_range() on the full VMA, we track it (depending on RAM vs. !RAM this "tracking" has different semantics).
For RAM, we seem to only lookup the cachemode. For !RAM, we seem to reserve the memtype for the PFN range, which will fail if there already is an incompatible memtype reserved.
It's all ... very weird.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 09/11] x86/mm/pat: remove MEMTYPE_*_MATCH
In reply to David HildenbrandQuoted 59 lines · David Hildenbrand
Quoted 6 lines · Lorenzo Stoakes
Quoted 2 lines · David Hildenbrand
The "
memramp()shrinking" scenario no longer applies, so let's remove that now-unnecessary handling.I wonder if we could remove even more of the code here given the simplifications here? But not a big deal.
It might make sense to inline
memtype_match().arch/x86/mm/pat/memtype_interval.c +10
-23@@ -49,21 +49,6 @@ INTERVAL_TREE_DEFINE(struct memtype, rb, u64, subtree_max_end,static struct rb_root_cached memtype_rbroot =RB_ROOT_CACHED ; -static struct memtype *memtype_match (u64 start,u64 end) -{ -struct memtype *entry_match; - - entry_match =interval_iter_first (&memtype_rbroot, start, end-1 ); - -while (entry_match !=NULL && entry_match->start < end) { -if (entry_match->start == start && entry_match->end == end) -return entry_match; - entry_match =interval_iter_next (entry_match, start, end-1 ); - } - -return NULL ;/* Returns NULL if there is no match */ -} -static int memtype_check_conflict (u64 start,u64 end,enum page_cache_mode reqtype,enum page_cache_mode *newtype)@@ -119,14 +104,16 @@ int memtype_check_insert(struct memtype *entry_new, enum page_cache_mode *ret_tystruct memtype *memtype_erase (u64 start,u64 end) { -struct memtype *entry_old; - - entry_old =memtype_match (start, end); -if (!entry_old) -return ERR_PTR (-EINVAL ); - -interval_remove (entry_old, &memtype_rbroot); -return entry_old; +struct memtype *entry =interval_iter_first (&memtype_rbroot, start, end -1 ); + +while (entry && entry->start < end) { +if (entry->start == start && entry->end == end) { +interval_remove (entry, &memtype_rbroot); +return entry; + } + entry =interval_iter_next (entry, start, end -1 ); + } +return ERR_PTR (-EINVAL ); }struct memtype *memtype_lookup (u64 addr)
Quoted 1 line
Thanks for all the review!
You're welcome :)
I _think_ I'm all caught up on my side of this review, ping me if there's anything more you need from me.
Quoted 6 lines
--
Cheers,David / dhildenb
Cheers, Lorenzo
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
In reply to David HildenbrandQuoted 6 lines
Quoted 6 lines
Obviously my series will break this but should be _fairly_ trivial to update.
You will however have to make sure to update
tools/testing/vma/*to handle the new functions in userland testing (they need to be stubbed otu).
Hmm, seems to compile. I guess, because we won't have "__HAVE_PFNMAP_TRACKING" defined in the test environment, so the existing stubs in there already seem to do the trick.
+#else
+static inline int vma_pfnmap_track_ctx_dup(struct vm_area_struct *orig,
+ struct vm_area_struct *new)
+{
+ return 0;
}
+static inline void vma_pfnmap_track_ctx_release(struct vm_area_struct *vma) +{
+}
+#endif
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 05/11] mm: convert VM_PFNMAP tracking to pfnmap_track() + pfnmap_untrack()
Quoted 13 lines · David Hildenbrand
Quoted 6 lines
Quoted 6 lines
Obviously my series will break this but should be _fairly_ trivial to update.
You will however have to make sure to update
tools/testing/vma/*to handle the new functions in userland testing (they need to be stubbed otu).Hmm, seems to compile. I guess, because we won't have "
__HAVE_PFNMAP_TRACKING" defined in the test environment, so the existing stubs in there already seem to do the trick.
kernel/fork.c static void vm_area_init_from(const struct vm_area_struct *src, · line 528
Quoted 10 lines
kernel/fork.c +9
-0+#else +static inline int vma_pfnmap_track_ctx_dup (struct vm_area_struct *orig, +struct vm_area_struct *new) +{ +return 0 ; } +static inline void vma_pfnmap_track_ctx_release (struct vm_area_struct *vma) +{ +} +#endif
OK cool! Then we're good :>)
Quoted 6 lines
--
Cheers,David / dhildenb