Skip to content

Commit 4109823

Browse files
willdeaconctmarinas
authored andcommitted
arm64: hugetlb: Restore TLB invalidation for BBM on contiguous ptes
Commit fb396bb ("arm64/hugetlb: Drop TLB flush from get_clear_flush()") removed TLB invalidation from get_clear_flush() [now get_clear_contig()] on the basis that the core TLB invalidation code is aware of hugetlb mappings backed by contiguous page-table entries and will cover the correct virtual address range. However, this change also resulted in the TLB invalidation being removed from the "break" step in the break-before-make (BBM) sequence used internally by huge_ptep_set_{access_flags,wrprotect}(), therefore making the BBM sequence unsafe irrespective of later invalidation. Although the architecture is desperately unclear about how exactly contiguous ptes should be updated in a live page-table, restore TLB invalidation to our BBM sequence under the assumption that BBM is the right thing to be doing in the first place. Fixes: fb396bb ("arm64/hugetlb: Drop TLB flush from get_clear_flush()") Cc: Ard Biesheuvel <[email protected]> Cc: Steve Capper <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Marc Zyngier <[email protected]> Signed-off-by: Will Deacon <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Anshuman Khandual <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent c50f11c commit 4109823

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

arch/arm64/mm/hugetlbpage.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,19 @@ static pte_t get_clear_contig(struct mm_struct *mm,
214214
return orig_pte;
215215
}
216216

217+
static pte_t get_clear_contig_flush(struct mm_struct *mm,
218+
unsigned long addr,
219+
pte_t *ptep,
220+
unsigned long pgsize,
221+
unsigned long ncontig)
222+
{
223+
pte_t orig_pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig);
224+
struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0);
225+
226+
flush_tlb_range(&vma, addr, addr + (pgsize * ncontig));
227+
return orig_pte;
228+
}
229+
217230
/*
218231
* Changing some bits of contiguous entries requires us to follow a
219232
* Break-Before-Make approach, breaking the whole contiguous set
@@ -447,19 +460,20 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
447460
int ncontig, i;
448461
size_t pgsize = 0;
449462
unsigned long pfn = pte_pfn(pte), dpfn;
463+
struct mm_struct *mm = vma->vm_mm;
450464
pgprot_t hugeprot;
451465
pte_t orig_pte;
452466

453467
if (!pte_cont(pte))
454468
return ptep_set_access_flags(vma, addr, ptep, pte, dirty);
455469

456-
ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
470+
ncontig = find_num_contig(mm, addr, ptep, &pgsize);
457471
dpfn = pgsize >> PAGE_SHIFT;
458472

459473
if (!__cont_access_flags_changed(ptep, pte, ncontig))
460474
return 0;
461475

462-
orig_pte = get_clear_contig(vma->vm_mm, addr, ptep, pgsize, ncontig);
476+
orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
463477

464478
/* Make sure we don't lose the dirty or young state */
465479
if (pte_dirty(orig_pte))
@@ -470,7 +484,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma,
470484

471485
hugeprot = pte_pgprot(pte);
472486
for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn)
473-
set_pte_at(vma->vm_mm, addr, ptep, pfn_pte(pfn, hugeprot));
487+
set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot));
474488

475489
return 1;
476490
}
@@ -492,7 +506,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
492506
ncontig = find_num_contig(mm, addr, ptep, &pgsize);
493507
dpfn = pgsize >> PAGE_SHIFT;
494508

495-
pte = get_clear_contig(mm, addr, ptep, pgsize, ncontig);
509+
pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
496510
pte = pte_wrprotect(pte);
497511

498512
hugeprot = pte_pgprot(pte);
@@ -505,17 +519,15 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm,
505519
pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
506520
unsigned long addr, pte_t *ptep)
507521
{
522+
struct mm_struct *mm = vma->vm_mm;
508523
size_t pgsize;
509524
int ncontig;
510-
pte_t orig_pte;
511525

512526
if (!pte_cont(READ_ONCE(*ptep)))
513527
return ptep_clear_flush(vma, addr, ptep);
514528

515-
ncontig = find_num_contig(vma->vm_mm, addr, ptep, &pgsize);
516-
orig_pte = get_clear_contig(vma->vm_mm, addr, ptep, pgsize, ncontig);
517-
flush_tlb_range(vma, addr, addr + pgsize * ncontig);
518-
return orig_pte;
529+
ncontig = find_num_contig(mm, addr, ptep, &pgsize);
530+
return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig);
519531
}
520532

521533
static int __init hugetlbpage_init(void)

0 commit comments

Comments
 (0)