Skip to content

Commit a899b7d

Browse files
Ryan Robertswilldeacon
authored andcommitted
arm64: hugetlb: Use __set_ptes_anysz() and __ptep_get_and_clear_anysz()
Refactor the huge_pte helpers to use the new common __set_ptes_anysz() and __ptep_get_and_clear_anysz() APIs. This provides 2 benefits; First, when page_table_check=on, hugetlb is now properly/fully checked. Previously only the first page of a hugetlb folio was checked. Second, instead of having to call __set_ptes(nr=1) for each pte in a loop, the whole contiguous batch can now be set in one go, which enables some efficiencies and cleans up the code. One detail to note is that huge_ptep_clear_flush() was previously calling ptep_clear_flush() for a non-contiguous pte (i.e. a pud or pmd block mapping). This has a couple of disadvantages; first ptep_clear_flush() calls ptep_get_and_clear() which transparently handles contpte. Given we only call for non-contiguous ptes, it would be safe, but a waste of effort. It's preferable to go straight to the layer below. However, more problematic is that ptep_get_and_clear() is for PAGE_SIZE entries so it calls page_table_check_pte_clear() and would not clear the whole hugetlb folio. So let's stop special-casing the non-cont case and just rely on get_clear_contig_flush() to do the right thing for non-cont entries. Reviewed-by: Catalin Marinas <[email protected]> Signed-off-by: Ryan Roberts <[email protected]> Reviewed-by: Anshuman Khandual <[email protected]> Tested-by: Luiz Capitulino <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent ef493d2 commit a899b7d

File tree

1 file changed

+10
-43
lines changed

1 file changed

+10
-43
lines changed

arch/arm64/mm/hugetlbpage.c

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,11 @@ static pte_t get_clear_contig(struct mm_struct *mm,
159159
pte_t pte, tmp_pte;
160160
bool present;
161161

162-
pte = __ptep_get_and_clear(mm, addr, ptep);
162+
pte = __ptep_get_and_clear_anysz(mm, ptep, pgsize);
163163
present = pte_present(pte);
164164
while (--ncontig) {
165165
ptep++;
166-
addr += pgsize;
167-
tmp_pte = __ptep_get_and_clear(mm, addr, ptep);
166+
tmp_pte = __ptep_get_and_clear_anysz(mm, ptep, pgsize);
168167
if (present) {
169168
if (pte_dirty(tmp_pte))
170169
pte = pte_mkdirty(pte);
@@ -208,7 +207,7 @@ static void clear_flush(struct mm_struct *mm,
208207
unsigned long i, saddr = addr;
209208

210209
for (i = 0; i < ncontig; i++, addr += pgsize, ptep++)
211-
__ptep_get_and_clear(mm, addr, ptep);
210+
__ptep_get_and_clear_anysz(mm, ptep, pgsize);
212211

213212
__flush_hugetlb_tlb_range(&vma, saddr, addr, pgsize, true);
214213
}
@@ -219,32 +218,20 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
219218
size_t pgsize;
220219
int i;
221220
int ncontig;
222-
unsigned long pfn, dpfn;
223-
pgprot_t hugeprot;
224221

225222
ncontig = num_contig_ptes(sz, &pgsize);
226223

227224
if (!pte_present(pte)) {
228225
for (i = 0; i < ncontig; i++, ptep++, addr += pgsize)
229-
__set_ptes(mm, addr, ptep, pte, 1);
226+
__set_ptes_anysz(mm, ptep, pte, 1, pgsize);
230227
return;
231228
}
232229

233-
if (!pte_cont(pte)) {
234-
__set_ptes(mm, addr, ptep, pte, 1);
235-
return;
236-
}
237-
238-
pfn = pte_pfn(pte);
239-
dpfn = pgsize >> PAGE_SHIFT;
240-
hugeprot = pte_pgprot(pte);
241-
242230
/* Only need to "break" if transitioning valid -> valid. */
243-
if (pte_valid(__ptep_get(ptep)))
231+
if (pte_cont(pte) && pte_valid(__ptep_get(ptep)))
244232
clear_flush(mm, addr, ptep, pgsize, ncontig);
245233

246-
for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
247-
__set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
234+
__set_ptes_anysz(mm, ptep, pte, ncontig, pgsize);
248235
}
249236

250237
pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
@@ -434,11 +421,9 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
434421
unsigned long addr, pte_t *ptep,
435422
pte_t pte, int dirty)
436423
{
437-
int ncontig, i;
424+
int ncontig;
438425
size_t pgsize = 0;
439-
unsigned long pfn = pte_pfn(pte), dpfn;
440426
struct mm_struct *mm = vma->vm_mm;
441-
pgprot_t hugeprot;
442427
pte_t orig_pte;
443428

444429
VM_WARN_ON(!pte_present(pte));
@@ -447,7 +432,6 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
447432
return __ptep_set_access_flags(vma, addr, ptep, pte, dirty);
448433

449434
ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize);
450-
dpfn = pgsize >> PAGE_SHIFT;
451435

452436
if (!__cont_access_flags_changed(ptep, pte, ncontig))
453437
return 0;
@@ -462,19 +446,14 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
462446
if (pte_young(orig_pte))
463447
pte = pte_mkyoung(pte);
464448

465-
hugeprot = pte_pgprot(pte);
466-
for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
467-
__set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
468-
449+
__set_ptes_anysz(mm, ptep, pte, ncontig, pgsize);
469450
return 1;
470451
}
471452

472453
void huge_ptep_set_wrprotect(struct mm_struct *mm,
473454
unsigned long addr, pte_t *ptep)
474455
{
475-
unsigned long pfn, dpfn;
476-
pgprot_t hugeprot;
477-
int ncontig, i;
456+
int ncontig;
478457
size_t pgsize;
479458
pte_t pte;
480459

@@ -487,16 +466,11 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
487466
}
488467

489468
ncontig = find_num_contig(mm, addr, ptep, &pgsize);
490-
dpfn = pgsize >> PAGE_SHIFT;
491469

492470
pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
493471
pte = pte_wrprotect(pte);
494472

495-
hugeprot = pte_pgprot(pte);
496-
pfn = pte_pfn(pte);
497-
498-
for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
499-
__set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1);
473+
__set_ptes_anysz(mm, ptep, pte, ncontig, pgsize);
500474
}
501475

502476
pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
@@ -505,13 +479,6 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
505479
struct mm_struct *mm = vma->vm_mm;
506480
size_t pgsize;
507481
int ncontig;
508-
pte_t pte;
509-
510-
pte = __ptep_get(ptep);
511-
VM_WARN_ON(!pte_present(pte));
512-
513-
if (!pte_cont(pte))
514-
return ptep_clear_flush(vma, addr, ptep);
515482

516483
ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize);
517484
return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);

0 commit comments

Comments
 (0)