summaryrefslogtreecommitdiff
path: root/mm/memory.c
diff options
context:
space:
mode:
authorLorenzo Stoakes <ljs@kernel.org>2026-07-10 21:16:59 +0100
committerAndrew Morton <akpm@linux-foundation.org>2026-08-04 19:19:00 -0700
commit5872168de5d5c1f69a2efcf80e331c32aa08ec9a (patch)
tree69b6d34c05bfe47857acda86eea6d5a3a2ff9057 /mm/memory.c
parent1268e1572ba545b75e11fbc33380f85a5d1ec1db (diff)
mm/vma: use vma_start_pgoff(), linear_page_index() in mm code
There are many instances in which linear_page_index() (as well as linear_page_delta()) is open-coded, which is confusing and inconsistent. Additionally, vma->vm_pgoff doesn't necessarily make it clear that this is the page offset of the start of the VMA range. Doing so also aids greppability. So use vma_start_pgoff() in favour of directly accessing vma->vm_pgoff, and linear_page_index() where we can. This also lays the ground for future changes which will add an anonymous page offset in order to be able to index MAP_PRIVATE-file backed anon folios in terms of their virtual page offset. No functional change intended. Link: https://lore.kernel.org/20260710-b4-pre-scalable-cow-v2-18-2a5aa403d977@kernel.org Signed-off-by: Lorenzo Stoakes <ljs@kernel.org> Reviewed-by: Gregory Price <gourry@gourry.net> Reviewed-by: SJ Park <sj@kernel.org> Reviewed-by: Pedro Falcato <pfalcato@suse.de> Reviewed-by: Vlastimil Babka (SUSE) <vbabka@kernel.org> Cc: Ackerley Tng <ackerleytng@google.com> Cc: David Hildenbrand (Arm) <david@kernel.org> Cc: Kai Huang <kai.huang@intel.com> Cc: Marek Szyprowski <m.szyprowski@samsung.com> Cc: Thomas Zimmermann <tzimmermann@suse.de> Cc: Liam R. Howlett (Oracle) <liam@infradead.org> Cc: Zi Yan <ziy@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'mm/memory.c')
-rw-r--r--mm/memory.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/mm/memory.c b/mm/memory.c
index b7e9a7e53516..fb7a246487ac 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -725,10 +725,10 @@ static inline struct page *__vm_normal_page(struct vm_area_struct *vma,
if (!pfn_valid(pfn))
return NULL;
} else {
- unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT;
+ const pgoff_t index = linear_page_index(vma, addr);
/* Only CoW'ed anon folios are "normal". */
- if (pfn == vma->vm_pgoff + off)
+ if (pfn == index)
return NULL;
if (!is_cow_mapping(vma->vm_flags))
return NULL;
@@ -2663,7 +2663,7 @@ static int __vm_map_pages(struct vm_area_struct *vma, struct page **pages,
int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
unsigned long num)
{
- return __vm_map_pages(vma, pages, num, vma->vm_pgoff);
+ return __vm_map_pages(vma, pages, num, vma_start_pgoff(vma));
}
EXPORT_SYMBOL(vm_map_pages);
@@ -3318,7 +3318,8 @@ int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long
unsigned long pfn;
int err;
- err = __simple_ioremap_prep(vm_len, vma->vm_pgoff, start, len, &pfn);
+ err = __simple_ioremap_prep(vm_len, vma_start_pgoff(vma), start, len,
+ &pfn);
if (err)
return err;
@@ -4366,15 +4367,15 @@ static inline void unmap_mapping_range_tree(struct address_space *mapping,
struct zap_details *details)
{
struct vm_area_struct *vma;
- unsigned long start, size;
struct mmu_gather tlb;
mapping_rmap_tree_foreach(vma, mapping, first_index, last_index) {
- const pgoff_t start_idx = max(first_index, vma->vm_pgoff);
+ const pgoff_t start_idx = max(first_index, vma_start_pgoff(vma));
const pgoff_t end_idx = min(last_index, vma_last_pgoff(vma)) + 1;
-
- start = vma->vm_start + ((start_idx - vma->vm_pgoff) << PAGE_SHIFT);
- size = (end_idx - start_idx) << PAGE_SHIFT;
+ const pgoff_t offset = start_idx - vma_start_pgoff(vma);
+ const unsigned long offset_bytes = offset << PAGE_SHIFT;
+ const unsigned long start = vma->vm_start + offset_bytes;
+ const unsigned long size = (end_idx - start_idx) << PAGE_SHIFT;
tlb_gather_mmu(&tlb, vma->vm_mm);
zap_vma_range_batched(&tlb, vma, start, size, details);
@@ -5712,7 +5713,7 @@ fallback:
} else if (nr_pages > 1) {
pgoff_t idx = folio_page_idx(folio, page);
/* The page offset of vmf->address within the VMA. */
- pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff;
+ pgoff_t vma_off = vmf->pgoff - vma_start_pgoff(vmf->vma);
/* The index of the entry in the pagetable for fault page. */
pgoff_t pte_off = pte_index(vmf->address);
@@ -5824,7 +5825,7 @@ static vm_fault_t do_fault_around(struct vm_fault *vmf)
pgoff_t nr_pages = READ_ONCE(fault_around_pages);
pgoff_t pte_off = pte_index(vmf->address);
/* The page offset of vmf->address within the VMA. */
- pgoff_t vma_off = vmf->pgoff - vmf->vma->vm_pgoff;
+ pgoff_t vma_off = vmf->pgoff - vma_start_pgoff(vmf->vma);
pgoff_t from_pte, to_pte;
vm_fault_t ret;
@@ -7352,7 +7353,7 @@ void print_vma_addr(char *prefix, unsigned long ip)
if (vma && vma->vm_file) {
struct file *f = vma->vm_file;
ip -= vma->vm_start;
- ip += vma->vm_pgoff << PAGE_SHIFT;
+ ip += vma_start_pgoff(vma) << PAGE_SHIFT;
printk("%s%pD[%lx,%lx+%lx]", prefix, f, ip,
vma->vm_start,
vma->vm_end - vma->vm_start);