Skip to content

Commit cf1b80d

Browse files
Dev Jainakpm00
authored andcommitted
mm: pass page directly instead of using folio_page
In commit_anon_folio_batch(), we iterate over all pages pointed to by the PTE batch. Therefore we need to know the first page of the batch; currently we derive that via folio_page(folio, 0), but, that takes us to the first (head) page of the folio instead - our PTE batch may lie in the middle of the folio, leading to incorrectness. Bite the bullet and throw away the micro-optimization of reusing the folio in favour of code simplicity. Derive the page and the folio in change_pte_range, and pass the page too to commit_anon_folio_batch to fix the aforementioned issue. Link: https://lkml.kernel.org/r/[email protected] Fixes: cac1db8 ("mm: optimize mprotect() by PTE batching") Reported-by: [email protected] Signed-off-by: Dev Jain <[email protected]> Reviewed-by: Lorenzo Stoakes <[email protected]> Debugged-by: David Hildenbrand <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: Barry Song <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Christophe Leroy <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jann Horn <[email protected]> Cc: Joey Gouly <[email protected]> Cc: Kevin Brodsky <[email protected]> Cc: Lance Yang <[email protected]> Cc: Liam Howlett <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Peter Xu <[email protected]> Cc: Ryan Roberts <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Will Deacon <[email protected]> Cc: Yang Shi <[email protected]> Cc: Yicong Yang <[email protected]> Cc: Zhenhua Huang <[email protected]> Cc: Zi Yan <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent ab5ac78 commit cf1b80d

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

mm/mprotect.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ static int mprotect_folio_pte_batch(struct folio *folio, pte_t *ptep,
120120

121121
static bool prot_numa_skip(struct vm_area_struct *vma, unsigned long addr,
122122
pte_t oldpte, pte_t *pte, int target_node,
123-
struct folio **foliop)
123+
struct folio *folio)
124124
{
125-
struct folio *folio = NULL;
126125
bool ret = true;
127126
bool toptier;
128127
int nid;
@@ -131,7 +130,6 @@ static bool prot_numa_skip(struct vm_area_struct *vma, unsigned long addr,
131130
if (pte_protnone(oldpte))
132131
goto skip;
133132

134-
folio = vm_normal_folio(vma, addr, oldpte);
135133
if (!folio)
136134
goto skip;
137135

@@ -173,7 +171,6 @@ static bool prot_numa_skip(struct vm_area_struct *vma, unsigned long addr,
173171
folio_xchg_access_time(folio, jiffies_to_msecs(jiffies));
174172

175173
skip:
176-
*foliop = folio;
177174
return ret;
178175
}
179176

@@ -231,10 +228,9 @@ static int page_anon_exclusive_sub_batch(int start_idx, int max_len,
231228
* retrieve sub-batches.
232229
*/
233230
static void commit_anon_folio_batch(struct vm_area_struct *vma,
234-
struct folio *folio, unsigned long addr, pte_t *ptep,
231+
struct folio *folio, struct page *first_page, unsigned long addr, pte_t *ptep,
235232
pte_t oldpte, pte_t ptent, int nr_ptes, struct mmu_gather *tlb)
236233
{
237-
struct page *first_page = folio_page(folio, 0);
238234
bool expected_anon_exclusive;
239235
int sub_batch_idx = 0;
240236
int len;
@@ -251,7 +247,7 @@ static void commit_anon_folio_batch(struct vm_area_struct *vma,
251247
}
252248

253249
static void set_write_prot_commit_flush_ptes(struct vm_area_struct *vma,
254-
struct folio *folio, unsigned long addr, pte_t *ptep,
250+
struct folio *folio, struct page *page, unsigned long addr, pte_t *ptep,
255251
pte_t oldpte, pte_t ptent, int nr_ptes, struct mmu_gather *tlb)
256252
{
257253
bool set_write;
@@ -270,7 +266,7 @@ static void set_write_prot_commit_flush_ptes(struct vm_area_struct *vma,
270266
/* idx = */ 0, set_write, tlb);
271267
return;
272268
}
273-
commit_anon_folio_batch(vma, folio, addr, ptep, oldpte, ptent, nr_ptes, tlb);
269+
commit_anon_folio_batch(vma, folio, page, addr, ptep, oldpte, ptent, nr_ptes, tlb);
274270
}
275271

276272
static long change_pte_range(struct mmu_gather *tlb,
@@ -305,15 +301,19 @@ static long change_pte_range(struct mmu_gather *tlb,
305301
const fpb_t flags = FPB_RESPECT_SOFT_DIRTY | FPB_RESPECT_WRITE;
306302
int max_nr_ptes = (end - addr) >> PAGE_SHIFT;
307303
struct folio *folio = NULL;
304+
struct page *page;
308305
pte_t ptent;
309306

307+
page = vm_normal_page(vma, addr, oldpte);
308+
if (page)
309+
folio = page_folio(page);
310310
/*
311311
* Avoid trapping faults against the zero or KSM
312312
* pages. See similar comment in change_huge_pmd.
313313
*/
314314
if (prot_numa) {
315315
int ret = prot_numa_skip(vma, addr, oldpte, pte,
316-
target_node, &folio);
316+
target_node, folio);
317317
if (ret) {
318318

319319
/* determine batch to skip */
@@ -323,9 +323,6 @@ static long change_pte_range(struct mmu_gather *tlb,
323323
}
324324
}
325325

326-
if (!folio)
327-
folio = vm_normal_folio(vma, addr, oldpte);
328-
329326
nr_ptes = mprotect_folio_pte_batch(folio, pte, oldpte, max_nr_ptes, flags);
330327

331328
oldpte = modify_prot_start_ptes(vma, addr, pte, nr_ptes);
@@ -351,7 +348,7 @@ static long change_pte_range(struct mmu_gather *tlb,
351348
*/
352349
if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) &&
353350
!pte_write(ptent))
354-
set_write_prot_commit_flush_ptes(vma, folio,
351+
set_write_prot_commit_flush_ptes(vma, folio, page,
355352
addr, pte, oldpte, ptent, nr_ptes, tlb);
356353
else
357354
prot_commit_flush_ptes(vma, addr, pte, oldpte, ptent,

0 commit comments

Comments
 (0)