Skip to content

Commit 2672391

Browse files
Peter Zijlstratorvalds
authored andcommitted
mm, powerpc: move the RCU page-table freeing into generic code
In case other architectures require RCU freed page-tables to implement gup_fast() and software filled hashes and similar things, provide the means to do so by moving the logic into generic code. Signed-off-by: Peter Zijlstra <[email protected]> Requested-by: David Miller <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Martin Schwidefsky <[email protected]> Cc: Russell King <[email protected]> Cc: Paul Mundt <[email protected]> Cc: Jeff Dike <[email protected]> Cc: Richard Weinberger <[email protected]> Cc: Tony Luck <[email protected]> Cc: KAMEZAWA Hiroyuki <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Mel Gorman <[email protected]> Cc: KOSAKI Motohiro <[email protected]> Cc: Nick Piggin <[email protected]> Cc: Namhyung Kim <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 1c39517 commit 2672391

File tree

10 files changed

+150
-125
lines changed

10 files changed

+150
-125
lines changed

arch/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,7 @@ config HAVE_ARCH_JUMP_LABEL
175175
config HAVE_ARCH_MUTEX_CPU_RELAX
176176
bool
177177

178+
config HAVE_RCU_TABLE_FREE
179+
bool
180+
178181
source "kernel/gcov/Kconfig"

arch/powerpc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ config PPC
140140
select IRQ_PER_CPU
141141
select GENERIC_IRQ_SHOW
142142
select GENERIC_IRQ_SHOW_LEVEL
143+
select HAVE_RCU_TABLE_FREE if SMP
143144

144145
config EARLY_PRINTK
145146
bool

arch/powerpc/include/asm/pgalloc.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,29 @@ static inline void pte_free(struct mm_struct *mm, pgtable_t ptepage)
3131
#endif
3232

3333
#ifdef CONFIG_SMP
34-
extern void pgtable_free_tlb(struct mmu_gather *tlb, void *table, unsigned shift);
35-
extern void pte_free_finish(struct mmu_gather *tlb);
34+
struct mmu_gather;
35+
extern void tlb_remove_table(struct mmu_gather *, void *);
36+
37+
static inline void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int shift)
38+
{
39+
unsigned long pgf = (unsigned long)table;
40+
BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE);
41+
pgf |= shift;
42+
tlb_remove_table(tlb, (void *)pgf);
43+
}
44+
45+
static inline void __tlb_remove_table(void *_table)
46+
{
47+
void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE);
48+
unsigned shift = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE;
49+
50+
pgtable_free(table, shift);
51+
}
3652
#else /* CONFIG_SMP */
3753
static inline void pgtable_free_tlb(struct mmu_gather *tlb, void *table, unsigned shift)
3854
{
3955
pgtable_free(table, shift);
4056
}
41-
static inline void pte_free_finish(struct mmu_gather *tlb) { }
4257
#endif /* !CONFIG_SMP */
4358

4459
static inline void __pte_free_tlb(struct mmu_gather *tlb, struct page *ptepage,

arch/powerpc/include/asm/tlb.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@
2828
#define tlb_start_vma(tlb, vma) do { } while (0)
2929
#define tlb_end_vma(tlb, vma) do { } while (0)
3030

31-
#define HAVE_ARCH_MMU_GATHER 1
32-
33-
struct pte_freelist_batch;
34-
35-
struct arch_mmu_gather {
36-
struct pte_freelist_batch *batch;
37-
};
38-
39-
#define ARCH_MMU_GATHER_INIT (struct arch_mmu_gather){ .batch = NULL, }
40-
4131
extern void tlb_flush(struct mmu_gather *tlb);
4232

4333
/* Get the generic bits... */

arch/powerpc/mm/pgtable.c

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -33,104 +33,6 @@
3333

3434
#include "mmu_decl.h"
3535

36-
#ifdef CONFIG_SMP
37-
38-
/*
39-
* Handle batching of page table freeing on SMP. Page tables are
40-
* queued up and send to be freed later by RCU in order to avoid
41-
* freeing a page table page that is being walked without locks
42-
*/
43-
44-
static unsigned long pte_freelist_forced_free;
45-
46-
struct pte_freelist_batch
47-
{
48-
struct rcu_head rcu;
49-
unsigned int index;
50-
unsigned long tables[0];
51-
};
52-
53-
#define PTE_FREELIST_SIZE \
54-
((PAGE_SIZE - sizeof(struct pte_freelist_batch)) \
55-
/ sizeof(unsigned long))
56-
57-
static void pte_free_smp_sync(void *arg)
58-
{
59-
/* Do nothing, just ensure we sync with all CPUs */
60-
}
61-
62-
/* This is only called when we are critically out of memory
63-
* (and fail to get a page in pte_free_tlb).
64-
*/
65-
static void pgtable_free_now(void *table, unsigned shift)
66-
{
67-
pte_freelist_forced_free++;
68-
69-
smp_call_function(pte_free_smp_sync, NULL, 1);
70-
71-
pgtable_free(table, shift);
72-
}
73-
74-
static void pte_free_rcu_callback(struct rcu_head *head)
75-
{
76-
struct pte_freelist_batch *batch =
77-
container_of(head, struct pte_freelist_batch, rcu);
78-
unsigned int i;
79-
80-
for (i = 0; i < batch->index; i++) {
81-
void *table = (void *)(batch->tables[i] & ~MAX_PGTABLE_INDEX_SIZE);
82-
unsigned shift = batch->tables[i] & MAX_PGTABLE_INDEX_SIZE;
83-
84-
pgtable_free(table, shift);
85-
}
86-
87-
free_page((unsigned long)batch);
88-
}
89-
90-
static void pte_free_submit(struct pte_freelist_batch *batch)
91-
{
92-
call_rcu_sched(&batch->rcu, pte_free_rcu_callback);
93-
}
94-
95-
void pgtable_free_tlb(struct mmu_gather *tlb, void *table, unsigned shift)
96-
{
97-
struct pte_freelist_batch **batchp = &tlb->arch.batch;
98-
unsigned long pgf;
99-
100-
if (atomic_read(&tlb->mm->mm_users) < 2) {
101-
pgtable_free(table, shift);
102-
return;
103-
}
104-
105-
if (*batchp == NULL) {
106-
*batchp = (struct pte_freelist_batch *)__get_free_page(GFP_ATOMIC);
107-
if (*batchp == NULL) {
108-
pgtable_free_now(table, shift);
109-
return;
110-
}
111-
(*batchp)->index = 0;
112-
}
113-
BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE);
114-
pgf = (unsigned long)table | shift;
115-
(*batchp)->tables[(*batchp)->index++] = pgf;
116-
if ((*batchp)->index == PTE_FREELIST_SIZE) {
117-
pte_free_submit(*batchp);
118-
*batchp = NULL;
119-
}
120-
}
121-
122-
void pte_free_finish(struct mmu_gather *tlb)
123-
{
124-
struct pte_freelist_batch **batchp = &tlb->arch.batch;
125-
126-
if (*batchp == NULL)
127-
return;
128-
pte_free_submit(*batchp);
129-
*batchp = NULL;
130-
}
131-
132-
#endif /* CONFIG_SMP */
133-
13436
static inline int is_exec_fault(void)
13537
{
13638
return current->thread.regs && TRAP(current->thread.regs) == 0x400;

arch/powerpc/mm/tlb_hash32.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ void tlb_flush(struct mmu_gather *tlb)
7171
*/
7272
_tlbia();
7373
}
74-
75-
/* Push out batch of freed page tables */
76-
pte_free_finish(tlb);
7774
}
7875

7976
/*

arch/powerpc/mm/tlb_hash64.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ void tlb_flush(struct mmu_gather *tlb)
165165
__flush_tlb_pending(tlbbatch);
166166

167167
put_cpu_var(ppc64_tlb_batch);
168-
169-
/* Push out batch of freed page tables */
170-
pte_free_finish(tlb);
171168
}
172169

173170
/**

arch/powerpc/mm/tlb_nohash.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,6 @@ EXPORT_SYMBOL(flush_tlb_range);
299299
void tlb_flush(struct mmu_gather *tlb)
300300
{
301301
flush_tlb_mm(tlb->mm);
302-
303-
/* Push out batch of freed page tables */
304-
pte_free_finish(tlb);
305302
}
306303

307304
/*

include/asm-generic/tlb.h

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,49 @@
2929
#define tlb_fast_mode(tlb) 1
3030
#endif
3131

32+
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
33+
/*
34+
* Semi RCU freeing of the page directories.
35+
*
36+
* This is needed by some architectures to implement software pagetable walkers.
37+
*
38+
* gup_fast() and other software pagetable walkers do a lockless page-table
39+
* walk and therefore needs some synchronization with the freeing of the page
40+
* directories. The chosen means to accomplish that is by disabling IRQs over
41+
* the walk.
42+
*
43+
* Architectures that use IPIs to flush TLBs will then automagically DTRT,
44+
* since we unlink the page, flush TLBs, free the page. Since the disabling of
45+
* IRQs delays the completion of the TLB flush we can never observe an already
46+
* freed page.
47+
*
48+
* Architectures that do not have this (PPC) need to delay the freeing by some
49+
* other means, this is that means.
50+
*
51+
* What we do is batch the freed directory pages (tables) and RCU free them.
52+
* We use the sched RCU variant, as that guarantees that IRQ/preempt disabling
53+
* holds off grace periods.
54+
*
55+
* However, in order to batch these pages we need to allocate storage, this
56+
* allocation is deep inside the MM code and can thus easily fail on memory
57+
* pressure. To guarantee progress we fall back to single table freeing, see
58+
* the implementation of tlb_remove_table_one().
59+
*
60+
*/
61+
struct mmu_table_batch {
62+
struct rcu_head rcu;
63+
unsigned int nr;
64+
void *tables[0];
65+
};
66+
67+
#define MAX_TABLE_BATCH \
68+
((PAGE_SIZE - sizeof(struct mmu_table_batch)) / sizeof(void *))
69+
70+
extern void tlb_table_flush(struct mmu_gather *tlb);
71+
extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
72+
73+
#endif
74+
3275
/*
3376
* If we can't allocate a page to make a big batch of page pointers
3477
* to work on, then just handle a few from the on-stack structure.
@@ -40,13 +83,13 @@
4083
*/
4184
struct mmu_gather {
4285
struct mm_struct *mm;
86+
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
87+
struct mmu_table_batch *batch;
88+
#endif
4389
unsigned int nr; /* set to ~0U means fast mode */
4490
unsigned int max; /* nr < max */
4591
unsigned int need_flush;/* Really unmapped some ptes? */
4692
unsigned int fullmm; /* non-zero means full mm flush */
47-
#ifdef HAVE_ARCH_MMU_GATHER
48-
struct arch_mmu_gather arch;
49-
#endif
5093
struct page **pages;
5194
struct page *local[MMU_GATHER_BUNDLE];
5295
};
@@ -82,8 +125,8 @@ tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
82125

83126
tlb->fullmm = fullmm;
84127

85-
#ifdef HAVE_ARCH_MMU_GATHER
86-
tlb->arch = ARCH_MMU_GATHER_INIT;
128+
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
129+
tlb->batch = NULL;
87130
#endif
88131
}
89132

@@ -94,6 +137,9 @@ tlb_flush_mmu(struct mmu_gather *tlb)
94137
return;
95138
tlb->need_flush = 0;
96139
tlb_flush(tlb);
140+
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
141+
tlb_table_flush(tlb);
142+
#endif
97143
if (!tlb_fast_mode(tlb)) {
98144
free_pages_and_swap_cache(tlb->pages, tlb->nr);
99145
tlb->nr = 0;

mm/memory.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,83 @@ static void check_sync_rss_stat(struct task_struct *task)
193193

194194
#endif
195195

196+
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
197+
198+
/*
199+
* See the comment near struct mmu_table_batch.
200+
*/
201+
202+
static void tlb_remove_table_smp_sync(void *arg)
203+
{
204+
/* Simply deliver the interrupt */
205+
}
206+
207+
static void tlb_remove_table_one(void *table)
208+
{
209+
/*
210+
* This isn't an RCU grace period and hence the page-tables cannot be
211+
* assumed to be actually RCU-freed.
212+
*
213+
* It is however sufficient for software page-table walkers that rely on
214+
* IRQ disabling. See the comment near struct mmu_table_batch.
215+
*/
216+
smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
217+
__tlb_remove_table(table);
218+
}
219+
220+
static void tlb_remove_table_rcu(struct rcu_head *head)
221+
{
222+
struct mmu_table_batch *batch;
223+
int i;
224+
225+
batch = container_of(head, struct mmu_table_batch, rcu);
226+
227+
for (i = 0; i < batch->nr; i++)
228+
__tlb_remove_table(batch->tables[i]);
229+
230+
free_page((unsigned long)batch);
231+
}
232+
233+
void tlb_table_flush(struct mmu_gather *tlb)
234+
{
235+
struct mmu_table_batch **batch = &tlb->batch;
236+
237+
if (*batch) {
238+
call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
239+
*batch = NULL;
240+
}
241+
}
242+
243+
void tlb_remove_table(struct mmu_gather *tlb, void *table)
244+
{
245+
struct mmu_table_batch **batch = &tlb->batch;
246+
247+
tlb->need_flush = 1;
248+
249+
/*
250+
* When there's less then two users of this mm there cannot be a
251+
* concurrent page-table walk.
252+
*/
253+
if (atomic_read(&tlb->mm->mm_users) < 2) {
254+
__tlb_remove_table(table);
255+
return;
256+
}
257+
258+
if (*batch == NULL) {
259+
*batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
260+
if (*batch == NULL) {
261+
tlb_remove_table_one(table);
262+
return;
263+
}
264+
(*batch)->nr = 0;
265+
}
266+
(*batch)->tables[(*batch)->nr++] = table;
267+
if ((*batch)->nr == MAX_TABLE_BATCH)
268+
tlb_table_flush(tlb);
269+
}
270+
271+
#endif
272+
196273
/*
197274
* If a p?d_bad entry is found while walking page tables, report
198275
* the error, before resetting entry to p?d_none. Usually (but

0 commit comments

Comments
 (0)