Skip to content

Commit 7f43d57

Browse files
Merge patch series "riscv: support fast gup"
Jisheng Zhang <[email protected]> says: This series adds fast gup support to riscv. The First patch fixes a bug in __p*d_free_tlb(). Per the riscv privileged spec, if non-leaf PTEs I.E pmd, pud or p4d is modified, a sfence.vma is a must. The 2nd patch is a preparation patch. The last two patches do the real work: 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. So after the 3rd patch, everything is well prepared, let's select HAVE_FAST_GUP if MMU. * b4-shazam-merge: riscv: enable HAVE_FAST_GUP if MMU riscv: enable MMU_GATHER_RCU_TABLE_FREE for SMP && MMU riscv: tlb: convert __p*d_free_tlb() to inline functions riscv: tlb: fix __p*d_free_tlb() Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
2 parents d7e76ce + 3f910b7 commit 7f43d57

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

arch/riscv/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ config RISCV
127127
select HAVE_FUNCTION_GRAPH_RETVAL if HAVE_FUNCTION_GRAPH_TRACER
128128
select HAVE_FUNCTION_TRACER if !XIP_KERNEL && !PREEMPTION
129129
select HAVE_EBPF_JIT if MMU
130+
select HAVE_FAST_GUP if MMU
130131
select HAVE_FUNCTION_ARG_ACCESS_API
131132
select HAVE_FUNCTION_ERROR_INJECTION
132133
select HAVE_GCC_PLUGINS
@@ -157,6 +158,7 @@ config RISCV
157158
select IRQ_FORCED_THREADING
158159
select KASAN_VMALLOC if KASAN
159160
select LOCK_MM_AND_FIND_VMA
161+
select MMU_GATHER_RCU_TABLE_FREE if SMP && MMU
160162
select MODULES_USE_ELF_RELA if MODULES
161163
select MODULE_SECTIONS if MODULES
162164
select OF

arch/riscv/include/asm/pgalloc.h

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,19 @@ static inline void pud_free(struct mm_struct *mm, pud_t *pud)
9595
__pud_free(mm, pud);
9696
}
9797

98-
#define __pud_free_tlb(tlb, pud, addr) pud_free((tlb)->mm, pud)
98+
static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud,
99+
unsigned long addr)
100+
{
101+
if (pgtable_l4_enabled) {
102+
struct ptdesc *ptdesc = virt_to_ptdesc(pud);
103+
104+
pagetable_pud_dtor(ptdesc);
105+
if (riscv_use_ipi_for_rfence())
106+
tlb_remove_page_ptdesc(tlb, ptdesc);
107+
else
108+
tlb_remove_ptdesc(tlb, ptdesc);
109+
}
110+
}
99111

100112
#define p4d_alloc_one p4d_alloc_one
101113
static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr)
@@ -124,7 +136,16 @@ static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
124136
__p4d_free(mm, p4d);
125137
}
126138

127-
#define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d)
139+
static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d,
140+
unsigned long addr)
141+
{
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+
}
148+
}
128149
#endif /* __PAGETABLE_PMD_FOLDED */
129150

130151
static inline void sync_kernel_mappings(pgd_t *pgd)
@@ -149,15 +170,31 @@ static inline pgd_t *pgd_alloc(struct mm_struct *mm)
149170

150171
#ifndef __PAGETABLE_PMD_FOLDED
151172

152-
#define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tlb)->mm, pmd)
173+
static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd,
174+
unsigned long addr)
175+
{
176+
struct ptdesc *ptdesc = virt_to_ptdesc(pmd);
177+
178+
pagetable_pmd_dtor(ptdesc);
179+
if (riscv_use_ipi_for_rfence())
180+
tlb_remove_page_ptdesc(tlb, ptdesc);
181+
else
182+
tlb_remove_ptdesc(tlb, ptdesc);
183+
}
153184

154185
#endif /* __PAGETABLE_PMD_FOLDED */
155186

156-
#define __pte_free_tlb(tlb, pte, buf) \
157-
do { \
158-
pagetable_pte_dtor(page_ptdesc(pte)); \
159-
tlb_remove_page_ptdesc((tlb), page_ptdesc(pte));\
160-
} while (0)
187+
static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte,
188+
unsigned long addr)
189+
{
190+
struct ptdesc *ptdesc = page_ptdesc(pte);
191+
192+
pagetable_pte_dtor(ptdesc);
193+
if (riscv_use_ipi_for_rfence())
194+
tlb_remove_page_ptdesc(tlb, ptdesc);
195+
else
196+
tlb_remove_ptdesc(tlb, ptdesc);
197+
}
161198
#endif /* CONFIG_MMU */
162199

163200
#endif /* _ASM_RISCV_PGALLOC_H */

arch/riscv/include/asm/pgtable.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,12 @@ static inline int pmd_write(pmd_t pmd)
656656
return pte_write(pmd_pte(pmd));
657657
}
658658

659+
#define pud_write pud_write
660+
static inline int pud_write(pud_t pud)
661+
{
662+
return pte_write(pud_pte(pud));
663+
}
664+
659665
#define pmd_dirty pmd_dirty
660666
static inline int pmd_dirty(pmd_t pmd)
661667
{

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)