Skip to content

Commit e303297

Browse files
Peter Zijlstratorvalds
authored andcommitted
mm: extended batches for generic mmu_gather
Instead of using a single batch (the small on-stack, or an allocated page), try and extend the batch every time it runs out and only flush once either the extend fails or we're done. Signed-off-by: Peter Zijlstra <[email protected]> Requested-by: Nick Piggin <[email protected]> Reviewed-by: KAMEZAWA Hiroyuki <[email protected]> Acked-by: Hugh Dickins <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: David Miller <[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: 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 2672391 commit e303297

File tree

2 files changed

+84
-47
lines changed

2 files changed

+84
-47
lines changed

include/asm-generic/tlb.h

Lines changed: 83 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919
#include <asm/pgalloc.h>
2020
#include <asm/tlbflush.h>
2121

22-
/*
23-
* For UP we don't need to worry about TLB flush
24-
* and page free order so much..
25-
*/
26-
#ifdef CONFIG_SMP
27-
#define tlb_fast_mode(tlb) ((tlb)->nr == ~0U)
28-
#else
29-
#define tlb_fast_mode(tlb) 1
30-
#endif
31-
3222
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
3323
/*
3424
* Semi RCU freeing of the page directories.
@@ -78,6 +68,16 @@ extern void tlb_remove_table(struct mmu_gather *tlb, void *table);
7868
*/
7969
#define MMU_GATHER_BUNDLE 8
8070

71+
struct mmu_gather_batch {
72+
struct mmu_gather_batch *next;
73+
unsigned int nr;
74+
unsigned int max;
75+
struct page *pages[0];
76+
};
77+
78+
#define MAX_GATHER_BATCH \
79+
((PAGE_SIZE - sizeof(struct mmu_gather_batch)) / sizeof(void *))
80+
8181
/* struct mmu_gather is an opaque type used by the mm code for passing around
8282
* any data needed by arch specific code for tlb_remove_page.
8383
*/
@@ -86,22 +86,48 @@ struct mmu_gather {
8686
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
8787
struct mmu_table_batch *batch;
8888
#endif
89-
unsigned int nr; /* set to ~0U means fast mode */
90-
unsigned int max; /* nr < max */
91-
unsigned int need_flush;/* Really unmapped some ptes? */
92-
unsigned int fullmm; /* non-zero means full mm flush */
93-
struct page **pages;
94-
struct page *local[MMU_GATHER_BUNDLE];
89+
unsigned int need_flush : 1, /* Did free PTEs */
90+
fast_mode : 1; /* No batching */
91+
92+
unsigned int fullmm;
93+
94+
struct mmu_gather_batch *active;
95+
struct mmu_gather_batch local;
96+
struct page *__pages[MMU_GATHER_BUNDLE];
9597
};
9698

97-
static inline void __tlb_alloc_page(struct mmu_gather *tlb)
99+
/*
100+
* For UP we don't need to worry about TLB flush
101+
* and page free order so much..
102+
*/
103+
#ifdef CONFIG_SMP
104+
#define tlb_fast_mode(tlb) (tlb->fast_mode)
105+
#else
106+
#define tlb_fast_mode(tlb) 1
107+
#endif
108+
109+
static inline int tlb_next_batch(struct mmu_gather *tlb)
98110
{
99-
unsigned long addr = __get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
111+
struct mmu_gather_batch *batch;
100112

101-
if (addr) {
102-
tlb->pages = (void *)addr;
103-
tlb->max = PAGE_SIZE / sizeof(struct page *);
113+
batch = tlb->active;
114+
if (batch->next) {
115+
tlb->active = batch->next;
116+
return 1;
104117
}
118+
119+
batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
120+
if (!batch)
121+
return 0;
122+
123+
batch->next = NULL;
124+
batch->nr = 0;
125+
batch->max = MAX_GATHER_BATCH;
126+
127+
tlb->active->next = batch;
128+
tlb->active = batch;
129+
130+
return 1;
105131
}
106132

107133
/* tlb_gather_mmu
@@ -114,16 +140,13 @@ tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
114140
{
115141
tlb->mm = mm;
116142

117-
tlb->max = ARRAY_SIZE(tlb->local);
118-
tlb->pages = tlb->local;
119-
120-
if (num_online_cpus() > 1) {
121-
tlb->nr = 0;
122-
__tlb_alloc_page(tlb);
123-
} else /* Use fast mode if only one CPU is online */
124-
tlb->nr = ~0U;
125-
126-
tlb->fullmm = fullmm;
143+
tlb->fullmm = fullmm;
144+
tlb->need_flush = 0;
145+
tlb->fast_mode = (num_possible_cpus() == 1);
146+
tlb->local.next = NULL;
147+
tlb->local.nr = 0;
148+
tlb->local.max = ARRAY_SIZE(tlb->__pages);
149+
tlb->active = &tlb->local;
127150

128151
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
129152
tlb->batch = NULL;
@@ -133,24 +156,24 @@ tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
133156
static inline void
134157
tlb_flush_mmu(struct mmu_gather *tlb)
135158
{
159+
struct mmu_gather_batch *batch;
160+
136161
if (!tlb->need_flush)
137162
return;
138163
tlb->need_flush = 0;
139164
tlb_flush(tlb);
140165
#ifdef CONFIG_HAVE_RCU_TABLE_FREE
141166
tlb_table_flush(tlb);
142167
#endif
143-
if (!tlb_fast_mode(tlb)) {
144-
free_pages_and_swap_cache(tlb->pages, tlb->nr);
145-
tlb->nr = 0;
146-
/*
147-
* If we are using the local on-stack array of pages for MMU
148-
* gather, try allocating an off-stack array again as we have
149-
* recently freed pages.
150-
*/
151-
if (tlb->pages == tlb->local)
152-
__tlb_alloc_page(tlb);
168+
169+
if (tlb_fast_mode(tlb))
170+
return;
171+
172+
for (batch = &tlb->local; batch; batch = batch->next) {
173+
free_pages_and_swap_cache(batch->pages, batch->nr);
174+
batch->nr = 0;
153175
}
176+
tlb->active = &tlb->local;
154177
}
155178

156179
/* tlb_finish_mmu
@@ -160,13 +183,18 @@ tlb_flush_mmu(struct mmu_gather *tlb)
160183
static inline void
161184
tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
162185
{
186+
struct mmu_gather_batch *batch, *next;
187+
163188
tlb_flush_mmu(tlb);
164189

165190
/* keep the page table cache within bounds */
166191
check_pgt_cache();
167192

168-
if (tlb->pages != tlb->local)
169-
free_pages((unsigned long)tlb->pages, 0);
193+
for (batch = tlb->local.next; batch; batch = next) {
194+
next = batch->next;
195+
free_pages((unsigned long)batch, 0);
196+
}
197+
tlb->local.next = NULL;
170198
}
171199

172200
/* __tlb_remove_page
@@ -177,15 +205,24 @@ tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
177205
*/
178206
static inline int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
179207
{
208+
struct mmu_gather_batch *batch;
209+
180210
tlb->need_flush = 1;
211+
181212
if (tlb_fast_mode(tlb)) {
182213
free_page_and_swap_cache(page);
183214
return 1; /* avoid calling tlb_flush_mmu() */
184215
}
185-
tlb->pages[tlb->nr++] = page;
186-
VM_BUG_ON(tlb->nr > tlb->max);
187216

188-
return tlb->max - tlb->nr;
217+
batch = tlb->active;
218+
batch->pages[batch->nr++] = page;
219+
VM_BUG_ON(batch->nr > batch->max);
220+
if (batch->nr == batch->max) {
221+
if (!tlb_next_batch(tlb))
222+
return 0;
223+
}
224+
225+
return batch->max - batch->nr;
189226
}
190227

191228
/* tlb_remove_page

mm/memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,8 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb,
994994
spinlock_t *ptl;
995995
int rss[NR_MM_COUNTERS];
996996

997-
init_rss_vec(rss);
998997
again:
998+
init_rss_vec(rss);
999999
pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
10001000
arch_enter_lazy_mmu_mode();
10011001
do {

0 commit comments

Comments
 (0)