Skip to content

Commit 69be3fb

Browse files
xhackerustcpalmer-dabbelt
authored andcommitted
riscv: enable MMU_GATHER_RCU_TABLE_FREE for SMP && MMU
In order to implement fast gup we need to ensure that the page table walker is protected from page table pages being freed from under it. riscv situation is more complicated than other architectures: some riscv platforms may use IPI to perform TLB shootdown, for example, those platforms which support AIA, usually the riscv_ipi_for_rfence is true on these platforms; some riscv platforms may rely on the SBI to perform TLB shootdown, usually the riscv_ipi_for_rfence is false on these platforms. To keep software pagetable walkers safe in this case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h for more details. This patch enables MMU_GATHER_RCU_TABLE_FREE, then use *tlb_remove_page_ptdesc() for those platforms which use IPI to perform TLB shootdown; *tlb_remove_ptdesc() for those platforms which use SBI to perform TLB shootdown; Both case mean that disabling interrupts will block the free and protect the fast gup page walker. Signed-off-by: Jisheng Zhang <[email protected]> Reviewed-by: Alexandre Ghiti <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 40d1bb9 commit 69be3fb

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ config RISCV
147147
select IRQ_FORCED_THREADING
148148
select KASAN_VMALLOC if KASAN
149149
select LOCK_MM_AND_FIND_VMA
150+
select MMU_GATHER_RCU_TABLE_FREE if SMP && MMU
150151
select MODULES_USE_ELF_RELA if MODULES
151152
select MODULE_SECTIONS if MODULES
152153
select OF

arch/riscv/include/asm/pgalloc.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,
102102
struct ptdesc *ptdesc = virt_to_ptdesc(pud);
103103

104104
pagetable_pud_dtor(ptdesc);
105-
tlb_remove_page_ptdesc(tlb, ptdesc);
105+
if (riscv_use_ipi_for_rfence())
106+
tlb_remove_page_ptdesc(tlb, ptdesc);
107+
else
108+
tlb_remove_ptdesc(tlb, ptdesc);
106109
}
107110
}
108111

@@ -136,8 +139,12 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
136139
static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,
137140
unsigned long addr)
138141
{
139-
if (pgtable_l5_enabled)
140-
tlb_remove_page_ptdesc(tlb, virt_to_ptdesc(p4d));
142+
if (pgtable_l5_enabled) {
143+
if (riscv_use_ipi_for_rfence())
144+
tlb_remove_page_ptdesc(tlb, virt_to_ptdesc(p4d));
145+
else
146+
tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d));
147+
}
141148
}
142149
#endif /* __PAGETABLE_PMD_FOLDED */
143150

@@ -169,7 +176,10 @@ static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
169176
struct ptdesc *ptdesc = virt_to_ptdesc(pmd);
170177

171178
pagetable_pmd_dtor(ptdesc);
172-
tlb_remove_page_ptdesc(tlb, ptdesc);
179+
if (riscv_use_ipi_for_rfence())
180+
tlb_remove_page_ptdesc(tlb, ptdesc);
181+
else
182+
tlb_remove_ptdesc(tlb, ptdesc);
173183
}
174184

175185
#endif /* __PAGETABLE_PMD_FOLDED */
@@ -180,7 +190,10 @@ static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
180190
struct ptdesc *ptdesc = page_ptdesc(pte);
181191

182192
pagetable_pte_dtor(ptdesc);
183-
tlb_remove_page_ptdesc(tlb, ptdesc);
193+
if (riscv_use_ipi_for_rfence())
194+
tlb_remove_page_ptdesc(tlb, ptdesc);
195+
else
196+
tlb_remove_ptdesc(tlb, ptdesc);
184197
}
185198
#endif /* CONFIG_MMU */
186199

arch/riscv/include/asm/tlb.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ struct mmu_gather;
1010

1111
static void tlb_flush(struct mmu_gather *tlb);
1212

13+
#ifdef CONFIG_MMU
14+
#include <linux/swap.h>
15+
16+
/*
17+
* While riscv platforms with riscv_ipi_for_rfence as true require an IPI to
18+
* perform TLB shootdown, some platforms with riscv_ipi_for_rfence as false use
19+
* SBI to perform TLB shootdown. To keep software pagetable walkers safe in this
20+
* case we switch to RCU based table free (MMU_GATHER_RCU_TABLE_FREE). See the
21+
* comment below 'ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE' in include/asm-generic/tlb.h
22+
* for more details.
23+
*/
24+
static inline void __tlb_remove_table(void *table)
25+
{
26+
free_page_and_swap_cache(table);
27+
}
28+
29+
#endif /* CONFIG_MMU */
30+
1331
#define tlb_flush tlb_flush
1432
#include <asm-generic/tlb.h>
1533

0 commit comments

Comments
 (0)