1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/mm/compaction.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Memory compaction for the reduction of external fragmentation. Note that
6*4882a593Smuzhiyun * this heavily depends upon page migration to do all the real heavy
7*4882a593Smuzhiyun * lifting
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun #include <linux/cpu.h>
12*4882a593Smuzhiyun #include <linux/swap.h>
13*4882a593Smuzhiyun #include <linux/migrate.h>
14*4882a593Smuzhiyun #include <linux/compaction.h>
15*4882a593Smuzhiyun #include <linux/mm_inline.h>
16*4882a593Smuzhiyun #include <linux/sched/signal.h>
17*4882a593Smuzhiyun #include <linux/backing-dev.h>
18*4882a593Smuzhiyun #include <linux/sysctl.h>
19*4882a593Smuzhiyun #include <linux/sysfs.h>
20*4882a593Smuzhiyun #include <linux/page-isolation.h>
21*4882a593Smuzhiyun #include <linux/kasan.h>
22*4882a593Smuzhiyun #include <linux/kthread.h>
23*4882a593Smuzhiyun #include <linux/freezer.h>
24*4882a593Smuzhiyun #include <linux/page_owner.h>
25*4882a593Smuzhiyun #include <linux/psi.h>
26*4882a593Smuzhiyun #include "internal.h"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #ifdef CONFIG_COMPACTION
count_compact_event(enum vm_event_item item)29*4882a593Smuzhiyun static inline void count_compact_event(enum vm_event_item item)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun count_vm_event(item);
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun
count_compact_events(enum vm_event_item item,long delta)34*4882a593Smuzhiyun static inline void count_compact_events(enum vm_event_item item, long delta)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun count_vm_events(item, delta);
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun #else
39*4882a593Smuzhiyun #define count_compact_event(item) do { } while (0)
40*4882a593Smuzhiyun #define count_compact_events(item, delta) do { } while (0)
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #if defined CONFIG_COMPACTION || defined CONFIG_CMA
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
46*4882a593Smuzhiyun #include <trace/events/compaction.h>
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order))
49*4882a593Smuzhiyun #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order))
50*4882a593Smuzhiyun #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order)
51*4882a593Smuzhiyun #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order)
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * Fragmentation score check interval for proactive compaction purposes.
55*4882a593Smuzhiyun */
56*4882a593Smuzhiyun static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * Page order with-respect-to which proactive compaction
60*4882a593Smuzhiyun * calculates external fragmentation, which is used as
61*4882a593Smuzhiyun * the "fragmentation score" of a node/zone.
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun #if defined CONFIG_TRANSPARENT_HUGEPAGE
64*4882a593Smuzhiyun #define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER
65*4882a593Smuzhiyun #elif defined CONFIG_HUGETLBFS
66*4882a593Smuzhiyun #define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER
67*4882a593Smuzhiyun #else
68*4882a593Smuzhiyun #define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT)
69*4882a593Smuzhiyun #endif
70*4882a593Smuzhiyun
release_freepages(struct list_head * freelist)71*4882a593Smuzhiyun static unsigned long release_freepages(struct list_head *freelist)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun struct page *page, *next;
74*4882a593Smuzhiyun unsigned long high_pfn = 0;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun list_for_each_entry_safe(page, next, freelist, lru) {
77*4882a593Smuzhiyun unsigned long pfn = page_to_pfn(page);
78*4882a593Smuzhiyun list_del(&page->lru);
79*4882a593Smuzhiyun __free_page(page);
80*4882a593Smuzhiyun if (pfn > high_pfn)
81*4882a593Smuzhiyun high_pfn = pfn;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun return high_pfn;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
split_map_pages(struct list_head * list)87*4882a593Smuzhiyun static void split_map_pages(struct list_head *list)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun unsigned int i, order, nr_pages;
90*4882a593Smuzhiyun struct page *page, *next;
91*4882a593Smuzhiyun LIST_HEAD(tmp_list);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun list_for_each_entry_safe(page, next, list, lru) {
94*4882a593Smuzhiyun list_del(&page->lru);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun order = page_private(page);
97*4882a593Smuzhiyun nr_pages = 1 << order;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun post_alloc_hook(page, order, __GFP_MOVABLE);
100*4882a593Smuzhiyun if (order)
101*4882a593Smuzhiyun split_page(page, order);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun for (i = 0; i < nr_pages; i++) {
104*4882a593Smuzhiyun list_add(&page->lru, &tmp_list);
105*4882a593Smuzhiyun page++;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun list_splice(&tmp_list, list);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun #ifdef CONFIG_COMPACTION
113*4882a593Smuzhiyun
PageMovable(struct page * page)114*4882a593Smuzhiyun int PageMovable(struct page *page)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun struct address_space *mapping;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun VM_BUG_ON_PAGE(!PageLocked(page), page);
119*4882a593Smuzhiyun if (!__PageMovable(page))
120*4882a593Smuzhiyun return 0;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun mapping = page_mapping(page);
123*4882a593Smuzhiyun if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
124*4882a593Smuzhiyun return 1;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun return 0;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun EXPORT_SYMBOL(PageMovable);
129*4882a593Smuzhiyun
__SetPageMovable(struct page * page,struct address_space * mapping)130*4882a593Smuzhiyun void __SetPageMovable(struct page *page, struct address_space *mapping)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun VM_BUG_ON_PAGE(!PageLocked(page), page);
133*4882a593Smuzhiyun VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
134*4882a593Smuzhiyun page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun EXPORT_SYMBOL(__SetPageMovable);
137*4882a593Smuzhiyun
__ClearPageMovable(struct page * page)138*4882a593Smuzhiyun void __ClearPageMovable(struct page *page)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun VM_BUG_ON_PAGE(!PageLocked(page), page);
141*4882a593Smuzhiyun VM_BUG_ON_PAGE(!PageMovable(page), page);
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
144*4882a593Smuzhiyun * flag so that VM can catch up released page by driver after isolation.
145*4882a593Smuzhiyun * With it, VM migration doesn't try to put it back.
146*4882a593Smuzhiyun */
147*4882a593Smuzhiyun page->mapping = (void *)((unsigned long)page->mapping &
148*4882a593Smuzhiyun PAGE_MAPPING_MOVABLE);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun EXPORT_SYMBOL(__ClearPageMovable);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* Do not skip compaction more than 64 times */
153*4882a593Smuzhiyun #define COMPACT_MAX_DEFER_SHIFT 6
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * Compaction is deferred when compaction fails to result in a page
157*4882a593Smuzhiyun * allocation success. 1 << compact_defer_shift, compactions are skipped up
158*4882a593Smuzhiyun * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
159*4882a593Smuzhiyun */
defer_compaction(struct zone * zone,int order)160*4882a593Smuzhiyun void defer_compaction(struct zone *zone, int order)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun zone->compact_considered = 0;
163*4882a593Smuzhiyun zone->compact_defer_shift++;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun if (order < zone->compact_order_failed)
166*4882a593Smuzhiyun zone->compact_order_failed = order;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
169*4882a593Smuzhiyun zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun trace_mm_compaction_defer_compaction(zone, order);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* Returns true if compaction should be skipped this time */
compaction_deferred(struct zone * zone,int order)175*4882a593Smuzhiyun bool compaction_deferred(struct zone *zone, int order)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun unsigned long defer_limit = 1UL << zone->compact_defer_shift;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun if (order < zone->compact_order_failed)
180*4882a593Smuzhiyun return false;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /* Avoid possible overflow */
183*4882a593Smuzhiyun if (++zone->compact_considered >= defer_limit) {
184*4882a593Smuzhiyun zone->compact_considered = defer_limit;
185*4882a593Smuzhiyun return false;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun trace_mm_compaction_deferred(zone, order);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun return true;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun * Update defer tracking counters after successful compaction of given order,
195*4882a593Smuzhiyun * which means an allocation either succeeded (alloc_success == true) or is
196*4882a593Smuzhiyun * expected to succeed.
197*4882a593Smuzhiyun */
compaction_defer_reset(struct zone * zone,int order,bool alloc_success)198*4882a593Smuzhiyun void compaction_defer_reset(struct zone *zone, int order,
199*4882a593Smuzhiyun bool alloc_success)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun if (alloc_success) {
202*4882a593Smuzhiyun zone->compact_considered = 0;
203*4882a593Smuzhiyun zone->compact_defer_shift = 0;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun if (order >= zone->compact_order_failed)
206*4882a593Smuzhiyun zone->compact_order_failed = order + 1;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun trace_mm_compaction_defer_reset(zone, order);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /* Returns true if restarting compaction after many failures */
compaction_restarting(struct zone * zone,int order)212*4882a593Smuzhiyun bool compaction_restarting(struct zone *zone, int order)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun if (order < zone->compact_order_failed)
215*4882a593Smuzhiyun return false;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
218*4882a593Smuzhiyun zone->compact_considered >= 1UL << zone->compact_defer_shift;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* Returns true if the pageblock should be scanned for pages to isolate. */
isolation_suitable(struct compact_control * cc,struct page * page)222*4882a593Smuzhiyun static inline bool isolation_suitable(struct compact_control *cc,
223*4882a593Smuzhiyun struct page *page)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun if (cc->ignore_skip_hint)
226*4882a593Smuzhiyun return true;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun return !get_pageblock_skip(page);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
reset_cached_positions(struct zone * zone)231*4882a593Smuzhiyun static void reset_cached_positions(struct zone *zone)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
234*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
235*4882a593Smuzhiyun zone->compact_cached_free_pfn =
236*4882a593Smuzhiyun pageblock_start_pfn(zone_end_pfn(zone) - 1);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /*
240*4882a593Smuzhiyun * Compound pages of >= pageblock_order should consistenly be skipped until
241*4882a593Smuzhiyun * released. It is always pointless to compact pages of such order (if they are
242*4882a593Smuzhiyun * migratable), and the pageblocks they occupy cannot contain any free pages.
243*4882a593Smuzhiyun */
pageblock_skip_persistent(struct page * page)244*4882a593Smuzhiyun static bool pageblock_skip_persistent(struct page *page)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun if (!PageCompound(page))
247*4882a593Smuzhiyun return false;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun page = compound_head(page);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun if (compound_order(page) >= pageblock_order)
252*4882a593Smuzhiyun return true;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun return false;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun static bool
__reset_isolation_pfn(struct zone * zone,unsigned long pfn,bool check_source,bool check_target)258*4882a593Smuzhiyun __reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
259*4882a593Smuzhiyun bool check_target)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun struct page *page = pfn_to_online_page(pfn);
262*4882a593Smuzhiyun struct page *block_page;
263*4882a593Smuzhiyun struct page *end_page;
264*4882a593Smuzhiyun unsigned long block_pfn;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun if (!page)
267*4882a593Smuzhiyun return false;
268*4882a593Smuzhiyun if (zone != page_zone(page))
269*4882a593Smuzhiyun return false;
270*4882a593Smuzhiyun if (pageblock_skip_persistent(page))
271*4882a593Smuzhiyun return false;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /*
274*4882a593Smuzhiyun * If skip is already cleared do no further checking once the
275*4882a593Smuzhiyun * restart points have been set.
276*4882a593Smuzhiyun */
277*4882a593Smuzhiyun if (check_source && check_target && !get_pageblock_skip(page))
278*4882a593Smuzhiyun return true;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /*
281*4882a593Smuzhiyun * If clearing skip for the target scanner, do not select a
282*4882a593Smuzhiyun * non-movable pageblock as the starting point.
283*4882a593Smuzhiyun */
284*4882a593Smuzhiyun if (!check_source && check_target &&
285*4882a593Smuzhiyun get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
286*4882a593Smuzhiyun return false;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* Ensure the start of the pageblock or zone is online and valid */
289*4882a593Smuzhiyun block_pfn = pageblock_start_pfn(pfn);
290*4882a593Smuzhiyun block_pfn = max(block_pfn, zone->zone_start_pfn);
291*4882a593Smuzhiyun block_page = pfn_to_online_page(block_pfn);
292*4882a593Smuzhiyun if (block_page) {
293*4882a593Smuzhiyun page = block_page;
294*4882a593Smuzhiyun pfn = block_pfn;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /* Ensure the end of the pageblock or zone is online and valid */
298*4882a593Smuzhiyun block_pfn = pageblock_end_pfn(pfn) - 1;
299*4882a593Smuzhiyun block_pfn = min(block_pfn, zone_end_pfn(zone) - 1);
300*4882a593Smuzhiyun end_page = pfn_to_online_page(block_pfn);
301*4882a593Smuzhiyun if (!end_page)
302*4882a593Smuzhiyun return false;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun /*
305*4882a593Smuzhiyun * Only clear the hint if a sample indicates there is either a
306*4882a593Smuzhiyun * free page or an LRU page in the block. One or other condition
307*4882a593Smuzhiyun * is necessary for the block to be a migration source/target.
308*4882a593Smuzhiyun */
309*4882a593Smuzhiyun do {
310*4882a593Smuzhiyun if (pfn_valid_within(pfn)) {
311*4882a593Smuzhiyun if (check_source && PageLRU(page)) {
312*4882a593Smuzhiyun clear_pageblock_skip(page);
313*4882a593Smuzhiyun return true;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun if (check_target && PageBuddy(page)) {
317*4882a593Smuzhiyun clear_pageblock_skip(page);
318*4882a593Smuzhiyun return true;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun page += (1 << PAGE_ALLOC_COSTLY_ORDER);
323*4882a593Smuzhiyun pfn += (1 << PAGE_ALLOC_COSTLY_ORDER);
324*4882a593Smuzhiyun } while (page <= end_page);
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun return false;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /*
330*4882a593Smuzhiyun * This function is called to clear all cached information on pageblocks that
331*4882a593Smuzhiyun * should be skipped for page isolation when the migrate and free page scanner
332*4882a593Smuzhiyun * meet.
333*4882a593Smuzhiyun */
__reset_isolation_suitable(struct zone * zone)334*4882a593Smuzhiyun static void __reset_isolation_suitable(struct zone *zone)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun unsigned long migrate_pfn = zone->zone_start_pfn;
337*4882a593Smuzhiyun unsigned long free_pfn = zone_end_pfn(zone) - 1;
338*4882a593Smuzhiyun unsigned long reset_migrate = free_pfn;
339*4882a593Smuzhiyun unsigned long reset_free = migrate_pfn;
340*4882a593Smuzhiyun bool source_set = false;
341*4882a593Smuzhiyun bool free_set = false;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun if (!zone->compact_blockskip_flush)
344*4882a593Smuzhiyun return;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun zone->compact_blockskip_flush = false;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /*
349*4882a593Smuzhiyun * Walk the zone and update pageblock skip information. Source looks
350*4882a593Smuzhiyun * for PageLRU while target looks for PageBuddy. When the scanner
351*4882a593Smuzhiyun * is found, both PageBuddy and PageLRU are checked as the pageblock
352*4882a593Smuzhiyun * is suitable as both source and target.
353*4882a593Smuzhiyun */
354*4882a593Smuzhiyun for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
355*4882a593Smuzhiyun free_pfn -= pageblock_nr_pages) {
356*4882a593Smuzhiyun cond_resched();
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /* Update the migrate PFN */
359*4882a593Smuzhiyun if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
360*4882a593Smuzhiyun migrate_pfn < reset_migrate) {
361*4882a593Smuzhiyun source_set = true;
362*4882a593Smuzhiyun reset_migrate = migrate_pfn;
363*4882a593Smuzhiyun zone->compact_init_migrate_pfn = reset_migrate;
364*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[0] = reset_migrate;
365*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[1] = reset_migrate;
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun /* Update the free PFN */
369*4882a593Smuzhiyun if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
370*4882a593Smuzhiyun free_pfn > reset_free) {
371*4882a593Smuzhiyun free_set = true;
372*4882a593Smuzhiyun reset_free = free_pfn;
373*4882a593Smuzhiyun zone->compact_init_free_pfn = reset_free;
374*4882a593Smuzhiyun zone->compact_cached_free_pfn = reset_free;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun /* Leave no distance if no suitable block was reset */
379*4882a593Smuzhiyun if (reset_migrate >= reset_free) {
380*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[0] = migrate_pfn;
381*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[1] = migrate_pfn;
382*4882a593Smuzhiyun zone->compact_cached_free_pfn = free_pfn;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
reset_isolation_suitable(pg_data_t * pgdat)386*4882a593Smuzhiyun void reset_isolation_suitable(pg_data_t *pgdat)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun int zoneid;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
391*4882a593Smuzhiyun struct zone *zone = &pgdat->node_zones[zoneid];
392*4882a593Smuzhiyun if (!populated_zone(zone))
393*4882a593Smuzhiyun continue;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun /* Only flush if a full compaction finished recently */
396*4882a593Smuzhiyun if (zone->compact_blockskip_flush)
397*4882a593Smuzhiyun __reset_isolation_suitable(zone);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun /*
402*4882a593Smuzhiyun * Sets the pageblock skip bit if it was clear. Note that this is a hint as
403*4882a593Smuzhiyun * locks are not required for read/writers. Returns true if it was already set.
404*4882a593Smuzhiyun */
test_and_set_skip(struct compact_control * cc,struct page * page,unsigned long pfn)405*4882a593Smuzhiyun static bool test_and_set_skip(struct compact_control *cc, struct page *page,
406*4882a593Smuzhiyun unsigned long pfn)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun bool skip;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /* Do no update if skip hint is being ignored */
411*4882a593Smuzhiyun if (cc->ignore_skip_hint)
412*4882a593Smuzhiyun return false;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun if (!IS_ALIGNED(pfn, pageblock_nr_pages))
415*4882a593Smuzhiyun return false;
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun skip = get_pageblock_skip(page);
418*4882a593Smuzhiyun if (!skip && !cc->no_set_skip_hint)
419*4882a593Smuzhiyun set_pageblock_skip(page);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun return skip;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
update_cached_migrate(struct compact_control * cc,unsigned long pfn)424*4882a593Smuzhiyun static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun struct zone *zone = cc->zone;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun pfn = pageblock_end_pfn(pfn);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /* Set for isolation rather than compaction */
431*4882a593Smuzhiyun if (cc->no_set_skip_hint)
432*4882a593Smuzhiyun return;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun if (pfn > zone->compact_cached_migrate_pfn[0])
435*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[0] = pfn;
436*4882a593Smuzhiyun if (cc->mode != MIGRATE_ASYNC &&
437*4882a593Smuzhiyun pfn > zone->compact_cached_migrate_pfn[1])
438*4882a593Smuzhiyun zone->compact_cached_migrate_pfn[1] = pfn;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /*
442*4882a593Smuzhiyun * If no pages were isolated then mark this pageblock to be skipped in the
443*4882a593Smuzhiyun * future. The information is later cleared by __reset_isolation_suitable().
444*4882a593Smuzhiyun */
update_pageblock_skip(struct compact_control * cc,struct page * page,unsigned long pfn)445*4882a593Smuzhiyun static void update_pageblock_skip(struct compact_control *cc,
446*4882a593Smuzhiyun struct page *page, unsigned long pfn)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun struct zone *zone = cc->zone;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun if (cc->no_set_skip_hint)
451*4882a593Smuzhiyun return;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun if (!page)
454*4882a593Smuzhiyun return;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun set_pageblock_skip(page);
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /* Update where async and sync compaction should restart */
459*4882a593Smuzhiyun if (pfn < zone->compact_cached_free_pfn)
460*4882a593Smuzhiyun zone->compact_cached_free_pfn = pfn;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun #else
isolation_suitable(struct compact_control * cc,struct page * page)463*4882a593Smuzhiyun static inline bool isolation_suitable(struct compact_control *cc,
464*4882a593Smuzhiyun struct page *page)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun return true;
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun
pageblock_skip_persistent(struct page * page)469*4882a593Smuzhiyun static inline bool pageblock_skip_persistent(struct page *page)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun return false;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
update_pageblock_skip(struct compact_control * cc,struct page * page,unsigned long pfn)474*4882a593Smuzhiyun static inline void update_pageblock_skip(struct compact_control *cc,
475*4882a593Smuzhiyun struct page *page, unsigned long pfn)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun
update_cached_migrate(struct compact_control * cc,unsigned long pfn)479*4882a593Smuzhiyun static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
test_and_set_skip(struct compact_control * cc,struct page * page,unsigned long pfn)483*4882a593Smuzhiyun static bool test_and_set_skip(struct compact_control *cc, struct page *page,
484*4882a593Smuzhiyun unsigned long pfn)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun return false;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun #endif /* CONFIG_COMPACTION */
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun * Compaction requires the taking of some coarse locks that are potentially
492*4882a593Smuzhiyun * very heavily contended. For async compaction, trylock and record if the
493*4882a593Smuzhiyun * lock is contended. The lock will still be acquired but compaction will
494*4882a593Smuzhiyun * abort when the current block is finished regardless of success rate.
495*4882a593Smuzhiyun * Sync compaction acquires the lock.
496*4882a593Smuzhiyun *
497*4882a593Smuzhiyun * Always returns true which makes it easier to track lock state in callers.
498*4882a593Smuzhiyun */
compact_lock_irqsave(spinlock_t * lock,unsigned long * flags,struct compact_control * cc)499*4882a593Smuzhiyun static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
500*4882a593Smuzhiyun struct compact_control *cc)
501*4882a593Smuzhiyun __acquires(lock)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun /* Track if the lock is contended in async mode */
504*4882a593Smuzhiyun if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
505*4882a593Smuzhiyun if (spin_trylock_irqsave(lock, *flags))
506*4882a593Smuzhiyun return true;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun cc->contended = true;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun spin_lock_irqsave(lock, *flags);
512*4882a593Smuzhiyun return true;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /*
516*4882a593Smuzhiyun * Compaction requires the taking of some coarse locks that are potentially
517*4882a593Smuzhiyun * very heavily contended. The lock should be periodically unlocked to avoid
518*4882a593Smuzhiyun * having disabled IRQs for a long time, even when there is nobody waiting on
519*4882a593Smuzhiyun * the lock. It might also be that allowing the IRQs will result in
520*4882a593Smuzhiyun * need_resched() becoming true. If scheduling is needed, async compaction
521*4882a593Smuzhiyun * aborts. Sync compaction schedules.
522*4882a593Smuzhiyun * Either compaction type will also abort if a fatal signal is pending.
523*4882a593Smuzhiyun * In either case if the lock was locked, it is dropped and not regained.
524*4882a593Smuzhiyun *
525*4882a593Smuzhiyun * Returns true if compaction should abort due to fatal signal pending, or
526*4882a593Smuzhiyun * async compaction due to need_resched()
527*4882a593Smuzhiyun * Returns false when compaction can continue (sync compaction might have
528*4882a593Smuzhiyun * scheduled)
529*4882a593Smuzhiyun */
compact_unlock_should_abort(spinlock_t * lock,unsigned long flags,bool * locked,struct compact_control * cc)530*4882a593Smuzhiyun static bool compact_unlock_should_abort(spinlock_t *lock,
531*4882a593Smuzhiyun unsigned long flags, bool *locked, struct compact_control *cc)
532*4882a593Smuzhiyun {
533*4882a593Smuzhiyun if (*locked) {
534*4882a593Smuzhiyun spin_unlock_irqrestore(lock, flags);
535*4882a593Smuzhiyun *locked = false;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (fatal_signal_pending(current)) {
539*4882a593Smuzhiyun cc->contended = true;
540*4882a593Smuzhiyun return true;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun cond_resched();
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun return false;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun /*
549*4882a593Smuzhiyun * Isolate free pages onto a private freelist. If @strict is true, will abort
550*4882a593Smuzhiyun * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
551*4882a593Smuzhiyun * (even though it may still end up isolating some pages).
552*4882a593Smuzhiyun */
isolate_freepages_block(struct compact_control * cc,unsigned long * start_pfn,unsigned long end_pfn,struct list_head * freelist,unsigned int stride,bool strict)553*4882a593Smuzhiyun static unsigned long isolate_freepages_block(struct compact_control *cc,
554*4882a593Smuzhiyun unsigned long *start_pfn,
555*4882a593Smuzhiyun unsigned long end_pfn,
556*4882a593Smuzhiyun struct list_head *freelist,
557*4882a593Smuzhiyun unsigned int stride,
558*4882a593Smuzhiyun bool strict)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun int nr_scanned = 0, total_isolated = 0;
561*4882a593Smuzhiyun struct page *cursor;
562*4882a593Smuzhiyun unsigned long flags = 0;
563*4882a593Smuzhiyun bool locked = false;
564*4882a593Smuzhiyun unsigned long blockpfn = *start_pfn;
565*4882a593Smuzhiyun unsigned int order;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun /* Strict mode is for isolation, speed is secondary */
568*4882a593Smuzhiyun if (strict)
569*4882a593Smuzhiyun stride = 1;
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun cursor = pfn_to_page(blockpfn);
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun /* Isolate free pages. */
574*4882a593Smuzhiyun for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) {
575*4882a593Smuzhiyun int isolated;
576*4882a593Smuzhiyun struct page *page = cursor;
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun /*
579*4882a593Smuzhiyun * Periodically drop the lock (if held) regardless of its
580*4882a593Smuzhiyun * contention, to give chance to IRQs. Abort if fatal signal
581*4882a593Smuzhiyun * pending or async compaction detects need_resched()
582*4882a593Smuzhiyun */
583*4882a593Smuzhiyun if (!(blockpfn % SWAP_CLUSTER_MAX)
584*4882a593Smuzhiyun && compact_unlock_should_abort(&cc->zone->lock, flags,
585*4882a593Smuzhiyun &locked, cc))
586*4882a593Smuzhiyun break;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun nr_scanned++;
589*4882a593Smuzhiyun if (!pfn_valid_within(blockpfn))
590*4882a593Smuzhiyun goto isolate_fail;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /*
593*4882a593Smuzhiyun * For compound pages such as THP and hugetlbfs, we can save
594*4882a593Smuzhiyun * potentially a lot of iterations if we skip them at once.
595*4882a593Smuzhiyun * The check is racy, but we can consider only valid values
596*4882a593Smuzhiyun * and the only danger is skipping too much.
597*4882a593Smuzhiyun */
598*4882a593Smuzhiyun if (PageCompound(page)) {
599*4882a593Smuzhiyun const unsigned int order = compound_order(page);
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun if (likely(order < MAX_ORDER)) {
602*4882a593Smuzhiyun blockpfn += (1UL << order) - 1;
603*4882a593Smuzhiyun cursor += (1UL << order) - 1;
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun goto isolate_fail;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun if (!PageBuddy(page))
609*4882a593Smuzhiyun goto isolate_fail;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun /*
612*4882a593Smuzhiyun * If we already hold the lock, we can skip some rechecking.
613*4882a593Smuzhiyun * Note that if we hold the lock now, checked_pageblock was
614*4882a593Smuzhiyun * already set in some previous iteration (or strict is true),
615*4882a593Smuzhiyun * so it is correct to skip the suitable migration target
616*4882a593Smuzhiyun * recheck as well.
617*4882a593Smuzhiyun */
618*4882a593Smuzhiyun if (!locked) {
619*4882a593Smuzhiyun locked = compact_lock_irqsave(&cc->zone->lock,
620*4882a593Smuzhiyun &flags, cc);
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun /* Recheck this is a buddy page under lock */
623*4882a593Smuzhiyun if (!PageBuddy(page))
624*4882a593Smuzhiyun goto isolate_fail;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun /* Found a free page, will break it into order-0 pages */
628*4882a593Smuzhiyun order = buddy_order(page);
629*4882a593Smuzhiyun isolated = __isolate_free_page(page, order);
630*4882a593Smuzhiyun if (!isolated)
631*4882a593Smuzhiyun break;
632*4882a593Smuzhiyun set_page_private(page, order);
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun total_isolated += isolated;
635*4882a593Smuzhiyun cc->nr_freepages += isolated;
636*4882a593Smuzhiyun list_add_tail(&page->lru, freelist);
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
639*4882a593Smuzhiyun blockpfn += isolated;
640*4882a593Smuzhiyun break;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun /* Advance to the end of split page */
643*4882a593Smuzhiyun blockpfn += isolated - 1;
644*4882a593Smuzhiyun cursor += isolated - 1;
645*4882a593Smuzhiyun continue;
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun isolate_fail:
648*4882a593Smuzhiyun if (strict)
649*4882a593Smuzhiyun break;
650*4882a593Smuzhiyun else
651*4882a593Smuzhiyun continue;
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun if (locked)
656*4882a593Smuzhiyun spin_unlock_irqrestore(&cc->zone->lock, flags);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun /*
659*4882a593Smuzhiyun * There is a tiny chance that we have read bogus compound_order(),
660*4882a593Smuzhiyun * so be careful to not go outside of the pageblock.
661*4882a593Smuzhiyun */
662*4882a593Smuzhiyun if (unlikely(blockpfn > end_pfn))
663*4882a593Smuzhiyun blockpfn = end_pfn;
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
666*4882a593Smuzhiyun nr_scanned, total_isolated);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun /* Record how far we have got within the block */
669*4882a593Smuzhiyun *start_pfn = blockpfn;
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun /*
672*4882a593Smuzhiyun * If strict isolation is requested by CMA then check that all the
673*4882a593Smuzhiyun * pages requested were isolated. If there were any failures, 0 is
674*4882a593Smuzhiyun * returned and CMA will fail.
675*4882a593Smuzhiyun */
676*4882a593Smuzhiyun if (strict && blockpfn < end_pfn)
677*4882a593Smuzhiyun total_isolated = 0;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun cc->total_free_scanned += nr_scanned;
680*4882a593Smuzhiyun if (total_isolated)
681*4882a593Smuzhiyun count_compact_events(COMPACTISOLATED, total_isolated);
682*4882a593Smuzhiyun return total_isolated;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun /**
686*4882a593Smuzhiyun * isolate_freepages_range() - isolate free pages.
687*4882a593Smuzhiyun * @cc: Compaction control structure.
688*4882a593Smuzhiyun * @start_pfn: The first PFN to start isolating.
689*4882a593Smuzhiyun * @end_pfn: The one-past-last PFN.
690*4882a593Smuzhiyun *
691*4882a593Smuzhiyun * Non-free pages, invalid PFNs, or zone boundaries within the
692*4882a593Smuzhiyun * [start_pfn, end_pfn) range are considered errors, cause function to
693*4882a593Smuzhiyun * undo its actions and return zero.
694*4882a593Smuzhiyun *
695*4882a593Smuzhiyun * Otherwise, function returns one-past-the-last PFN of isolated page
696*4882a593Smuzhiyun * (which may be greater then end_pfn if end fell in a middle of
697*4882a593Smuzhiyun * a free page).
698*4882a593Smuzhiyun */
699*4882a593Smuzhiyun unsigned long
isolate_freepages_range(struct compact_control * cc,unsigned long start_pfn,unsigned long end_pfn)700*4882a593Smuzhiyun isolate_freepages_range(struct compact_control *cc,
701*4882a593Smuzhiyun unsigned long start_pfn, unsigned long end_pfn)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
704*4882a593Smuzhiyun LIST_HEAD(freelist);
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun pfn = start_pfn;
707*4882a593Smuzhiyun block_start_pfn = pageblock_start_pfn(pfn);
708*4882a593Smuzhiyun if (block_start_pfn < cc->zone->zone_start_pfn)
709*4882a593Smuzhiyun block_start_pfn = cc->zone->zone_start_pfn;
710*4882a593Smuzhiyun block_end_pfn = pageblock_end_pfn(pfn);
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun for (; pfn < end_pfn; pfn += isolated,
713*4882a593Smuzhiyun block_start_pfn = block_end_pfn,
714*4882a593Smuzhiyun block_end_pfn += pageblock_nr_pages) {
715*4882a593Smuzhiyun /* Protect pfn from changing by isolate_freepages_block */
716*4882a593Smuzhiyun unsigned long isolate_start_pfn = pfn;
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun block_end_pfn = min(block_end_pfn, end_pfn);
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun /*
721*4882a593Smuzhiyun * pfn could pass the block_end_pfn if isolated freepage
722*4882a593Smuzhiyun * is more than pageblock order. In this case, we adjust
723*4882a593Smuzhiyun * scanning range to right one.
724*4882a593Smuzhiyun */
725*4882a593Smuzhiyun if (pfn >= block_end_pfn) {
726*4882a593Smuzhiyun block_start_pfn = pageblock_start_pfn(pfn);
727*4882a593Smuzhiyun block_end_pfn = pageblock_end_pfn(pfn);
728*4882a593Smuzhiyun block_end_pfn = min(block_end_pfn, end_pfn);
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun if (!pageblock_pfn_to_page(block_start_pfn,
732*4882a593Smuzhiyun block_end_pfn, cc->zone))
733*4882a593Smuzhiyun break;
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun isolated = isolate_freepages_block(cc, &isolate_start_pfn,
736*4882a593Smuzhiyun block_end_pfn, &freelist, 0, true);
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun /*
739*4882a593Smuzhiyun * In strict mode, isolate_freepages_block() returns 0 if
740*4882a593Smuzhiyun * there are any holes in the block (ie. invalid PFNs or
741*4882a593Smuzhiyun * non-free pages).
742*4882a593Smuzhiyun */
743*4882a593Smuzhiyun if (!isolated)
744*4882a593Smuzhiyun break;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun /*
747*4882a593Smuzhiyun * If we managed to isolate pages, it is always (1 << n) *
748*4882a593Smuzhiyun * pageblock_nr_pages for some non-negative n. (Max order
749*4882a593Smuzhiyun * page may span two pageblocks).
750*4882a593Smuzhiyun */
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun /* __isolate_free_page() does not map the pages */
754*4882a593Smuzhiyun split_map_pages(&freelist);
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun if (pfn < end_pfn) {
757*4882a593Smuzhiyun /* Loop terminated early, cleanup. */
758*4882a593Smuzhiyun release_freepages(&freelist);
759*4882a593Smuzhiyun return 0;
760*4882a593Smuzhiyun }
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun /* We don't use freelists for anything. */
763*4882a593Smuzhiyun return pfn;
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun #ifdef CONFIG_COMPACTION
isolate_and_split_free_page(struct page * page,struct list_head * list)767*4882a593Smuzhiyun unsigned long isolate_and_split_free_page(struct page *page,
768*4882a593Smuzhiyun struct list_head *list)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun unsigned long isolated;
771*4882a593Smuzhiyun unsigned int order;
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun if (!PageBuddy(page))
774*4882a593Smuzhiyun return 0;
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun order = buddy_order(page);
777*4882a593Smuzhiyun isolated = __isolate_free_page(page, order);
778*4882a593Smuzhiyun if (!isolated)
779*4882a593Smuzhiyun return 0;
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun set_page_private(page, order);
782*4882a593Smuzhiyun list_add(&page->lru, list);
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun split_map_pages(list);
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun return isolated;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(isolate_and_split_free_page);
789*4882a593Smuzhiyun #endif
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun /* Similar to reclaim, but different enough that they don't share logic */
too_many_isolated(pg_data_t * pgdat)792*4882a593Smuzhiyun static bool too_many_isolated(pg_data_t *pgdat)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun unsigned long active, inactive, isolated;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun inactive = node_page_state(pgdat, NR_INACTIVE_FILE) +
797*4882a593Smuzhiyun node_page_state(pgdat, NR_INACTIVE_ANON);
798*4882a593Smuzhiyun active = node_page_state(pgdat, NR_ACTIVE_FILE) +
799*4882a593Smuzhiyun node_page_state(pgdat, NR_ACTIVE_ANON);
800*4882a593Smuzhiyun isolated = node_page_state(pgdat, NR_ISOLATED_FILE) +
801*4882a593Smuzhiyun node_page_state(pgdat, NR_ISOLATED_ANON);
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun return isolated > (inactive + active) / 2;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun /**
807*4882a593Smuzhiyun * isolate_migratepages_block() - isolate all migrate-able pages within
808*4882a593Smuzhiyun * a single pageblock
809*4882a593Smuzhiyun * @cc: Compaction control structure.
810*4882a593Smuzhiyun * @low_pfn: The first PFN to isolate
811*4882a593Smuzhiyun * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
812*4882a593Smuzhiyun * @isolate_mode: Isolation mode to be used.
813*4882a593Smuzhiyun *
814*4882a593Smuzhiyun * Isolate all pages that can be migrated from the range specified by
815*4882a593Smuzhiyun * [low_pfn, end_pfn). The range is expected to be within same pageblock.
816*4882a593Smuzhiyun * Returns zero if there is a fatal signal pending, otherwise PFN of the
817*4882a593Smuzhiyun * first page that was not scanned (which may be both less, equal to or more
818*4882a593Smuzhiyun * than end_pfn).
819*4882a593Smuzhiyun *
820*4882a593Smuzhiyun * The pages are isolated on cc->migratepages list (not required to be empty),
821*4882a593Smuzhiyun * and cc->nr_migratepages is updated accordingly. The cc->migrate_pfn field
822*4882a593Smuzhiyun * is neither read nor updated.
823*4882a593Smuzhiyun */
824*4882a593Smuzhiyun static unsigned long
isolate_migratepages_block(struct compact_control * cc,unsigned long low_pfn,unsigned long end_pfn,isolate_mode_t isolate_mode)825*4882a593Smuzhiyun isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
826*4882a593Smuzhiyun unsigned long end_pfn, isolate_mode_t isolate_mode)
827*4882a593Smuzhiyun {
828*4882a593Smuzhiyun pg_data_t *pgdat = cc->zone->zone_pgdat;
829*4882a593Smuzhiyun unsigned long nr_scanned = 0, nr_isolated = 0;
830*4882a593Smuzhiyun struct lruvec *lruvec;
831*4882a593Smuzhiyun unsigned long flags = 0;
832*4882a593Smuzhiyun bool locked = false;
833*4882a593Smuzhiyun struct page *page = NULL, *valid_page = NULL;
834*4882a593Smuzhiyun unsigned long start_pfn = low_pfn;
835*4882a593Smuzhiyun bool skip_on_failure = false;
836*4882a593Smuzhiyun unsigned long next_skip_pfn = 0;
837*4882a593Smuzhiyun bool skip_updated = false;
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun /*
840*4882a593Smuzhiyun * Ensure that there are not too many pages isolated from the LRU
841*4882a593Smuzhiyun * list by either parallel reclaimers or compaction. If there are,
842*4882a593Smuzhiyun * delay for some time until fewer pages are isolated
843*4882a593Smuzhiyun */
844*4882a593Smuzhiyun while (unlikely(too_many_isolated(pgdat))) {
845*4882a593Smuzhiyun /* stop isolation if there are still pages not migrated */
846*4882a593Smuzhiyun if (cc->nr_migratepages)
847*4882a593Smuzhiyun return 0;
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun /* async migration should just abort */
850*4882a593Smuzhiyun if (cc->mode == MIGRATE_ASYNC)
851*4882a593Smuzhiyun return 0;
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun congestion_wait(BLK_RW_ASYNC, HZ/10);
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun if (fatal_signal_pending(current))
856*4882a593Smuzhiyun return 0;
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun cond_resched();
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
862*4882a593Smuzhiyun skip_on_failure = true;
863*4882a593Smuzhiyun next_skip_pfn = block_end_pfn(low_pfn, cc->order);
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun /* Time to isolate some pages for migration */
867*4882a593Smuzhiyun for (; low_pfn < end_pfn; low_pfn++) {
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun if (skip_on_failure && low_pfn >= next_skip_pfn) {
870*4882a593Smuzhiyun /*
871*4882a593Smuzhiyun * We have isolated all migration candidates in the
872*4882a593Smuzhiyun * previous order-aligned block, and did not skip it due
873*4882a593Smuzhiyun * to failure. We should migrate the pages now and
874*4882a593Smuzhiyun * hopefully succeed compaction.
875*4882a593Smuzhiyun */
876*4882a593Smuzhiyun if (nr_isolated)
877*4882a593Smuzhiyun break;
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun /*
880*4882a593Smuzhiyun * We failed to isolate in the previous order-aligned
881*4882a593Smuzhiyun * block. Set the new boundary to the end of the
882*4882a593Smuzhiyun * current block. Note we can't simply increase
883*4882a593Smuzhiyun * next_skip_pfn by 1 << order, as low_pfn might have
884*4882a593Smuzhiyun * been incremented by a higher number due to skipping
885*4882a593Smuzhiyun * a compound or a high-order buddy page in the
886*4882a593Smuzhiyun * previous loop iteration.
887*4882a593Smuzhiyun */
888*4882a593Smuzhiyun next_skip_pfn = block_end_pfn(low_pfn, cc->order);
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun /*
892*4882a593Smuzhiyun * Periodically drop the lock (if held) regardless of its
893*4882a593Smuzhiyun * contention, to give chance to IRQs. Abort completely if
894*4882a593Smuzhiyun * a fatal signal is pending.
895*4882a593Smuzhiyun */
896*4882a593Smuzhiyun if (!(low_pfn % SWAP_CLUSTER_MAX)
897*4882a593Smuzhiyun && compact_unlock_should_abort(&pgdat->lru_lock,
898*4882a593Smuzhiyun flags, &locked, cc)) {
899*4882a593Smuzhiyun low_pfn = 0;
900*4882a593Smuzhiyun goto fatal_pending;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun if (!pfn_valid_within(low_pfn))
904*4882a593Smuzhiyun goto isolate_fail;
905*4882a593Smuzhiyun nr_scanned++;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun page = pfn_to_page(low_pfn);
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun /*
910*4882a593Smuzhiyun * Check if the pageblock has already been marked skipped.
911*4882a593Smuzhiyun * Only the aligned PFN is checked as the caller isolates
912*4882a593Smuzhiyun * COMPACT_CLUSTER_MAX at a time so the second call must
913*4882a593Smuzhiyun * not falsely conclude that the block should be skipped.
914*4882a593Smuzhiyun */
915*4882a593Smuzhiyun if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) {
916*4882a593Smuzhiyun if (!cc->ignore_skip_hint && get_pageblock_skip(page)) {
917*4882a593Smuzhiyun low_pfn = end_pfn;
918*4882a593Smuzhiyun goto isolate_abort;
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun valid_page = page;
921*4882a593Smuzhiyun }
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun /*
924*4882a593Smuzhiyun * Skip if free. We read page order here without zone lock
925*4882a593Smuzhiyun * which is generally unsafe, but the race window is small and
926*4882a593Smuzhiyun * the worst thing that can happen is that we skip some
927*4882a593Smuzhiyun * potential isolation targets.
928*4882a593Smuzhiyun */
929*4882a593Smuzhiyun if (PageBuddy(page)) {
930*4882a593Smuzhiyun unsigned long freepage_order = buddy_order_unsafe(page);
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun /*
933*4882a593Smuzhiyun * Without lock, we cannot be sure that what we got is
934*4882a593Smuzhiyun * a valid page order. Consider only values in the
935*4882a593Smuzhiyun * valid order range to prevent low_pfn overflow.
936*4882a593Smuzhiyun */
937*4882a593Smuzhiyun if (freepage_order > 0 && freepage_order < MAX_ORDER)
938*4882a593Smuzhiyun low_pfn += (1UL << freepage_order) - 1;
939*4882a593Smuzhiyun continue;
940*4882a593Smuzhiyun }
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun /*
943*4882a593Smuzhiyun * Regardless of being on LRU, compound pages such as THP and
944*4882a593Smuzhiyun * hugetlbfs are not to be compacted unless we are attempting
945*4882a593Smuzhiyun * an allocation much larger than the huge page size (eg CMA).
946*4882a593Smuzhiyun * We can potentially save a lot of iterations if we skip them
947*4882a593Smuzhiyun * at once. The check is racy, but we can consider only valid
948*4882a593Smuzhiyun * values and the only danger is skipping too much.
949*4882a593Smuzhiyun */
950*4882a593Smuzhiyun if (PageCompound(page) && !cc->alloc_contig) {
951*4882a593Smuzhiyun const unsigned int order = compound_order(page);
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun if (likely(order < MAX_ORDER))
954*4882a593Smuzhiyun low_pfn += (1UL << order) - 1;
955*4882a593Smuzhiyun goto isolate_fail;
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun /*
959*4882a593Smuzhiyun * Check may be lockless but that's ok as we recheck later.
960*4882a593Smuzhiyun * It's possible to migrate LRU and non-lru movable pages.
961*4882a593Smuzhiyun * Skip any other type of page
962*4882a593Smuzhiyun */
963*4882a593Smuzhiyun if (!PageLRU(page)) {
964*4882a593Smuzhiyun /*
965*4882a593Smuzhiyun * __PageMovable can return false positive so we need
966*4882a593Smuzhiyun * to verify it under page_lock.
967*4882a593Smuzhiyun */
968*4882a593Smuzhiyun if (unlikely(__PageMovable(page)) &&
969*4882a593Smuzhiyun !PageIsolated(page)) {
970*4882a593Smuzhiyun if (locked) {
971*4882a593Smuzhiyun spin_unlock_irqrestore(&pgdat->lru_lock,
972*4882a593Smuzhiyun flags);
973*4882a593Smuzhiyun locked = false;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun if (!isolate_movable_page(page, isolate_mode))
977*4882a593Smuzhiyun goto isolate_success;
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun goto isolate_fail;
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun
983*4882a593Smuzhiyun /*
984*4882a593Smuzhiyun * Migration will fail if an anonymous page is pinned in memory,
985*4882a593Smuzhiyun * so avoid taking lru_lock and isolating it unnecessarily in an
986*4882a593Smuzhiyun * admittedly racy check.
987*4882a593Smuzhiyun */
988*4882a593Smuzhiyun if (!page_mapping(page) &&
989*4882a593Smuzhiyun page_count(page) > page_mapcount(page))
990*4882a593Smuzhiyun goto isolate_fail;
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun /*
993*4882a593Smuzhiyun * Only allow to migrate anonymous pages in GFP_NOFS context
994*4882a593Smuzhiyun * because those do not depend on fs locks.
995*4882a593Smuzhiyun */
996*4882a593Smuzhiyun if (!(cc->gfp_mask & __GFP_FS) && page_mapping(page))
997*4882a593Smuzhiyun goto isolate_fail;
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun /* If we already hold the lock, we can skip some rechecking */
1000*4882a593Smuzhiyun if (!locked) {
1001*4882a593Smuzhiyun locked = compact_lock_irqsave(&pgdat->lru_lock,
1002*4882a593Smuzhiyun &flags, cc);
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun /* Try get exclusive access under lock */
1005*4882a593Smuzhiyun if (!skip_updated) {
1006*4882a593Smuzhiyun skip_updated = true;
1007*4882a593Smuzhiyun if (test_and_set_skip(cc, page, low_pfn))
1008*4882a593Smuzhiyun goto isolate_abort;
1009*4882a593Smuzhiyun }
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun /* Recheck PageLRU and PageCompound under lock */
1012*4882a593Smuzhiyun if (!PageLRU(page))
1013*4882a593Smuzhiyun goto isolate_fail;
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun /*
1016*4882a593Smuzhiyun * Page become compound since the non-locked check,
1017*4882a593Smuzhiyun * and it's on LRU. It can only be a THP so the order
1018*4882a593Smuzhiyun * is safe to read and it's 0 for tail pages.
1019*4882a593Smuzhiyun */
1020*4882a593Smuzhiyun if (unlikely(PageCompound(page) && !cc->alloc_contig)) {
1021*4882a593Smuzhiyun low_pfn += compound_nr(page) - 1;
1022*4882a593Smuzhiyun goto isolate_fail;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun }
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun lruvec = mem_cgroup_page_lruvec(page, pgdat);
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun /* Try isolate the page */
1029*4882a593Smuzhiyun if (__isolate_lru_page(page, isolate_mode) != 0)
1030*4882a593Smuzhiyun goto isolate_fail;
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun /* The whole page is taken off the LRU; skip the tail pages. */
1033*4882a593Smuzhiyun if (PageCompound(page))
1034*4882a593Smuzhiyun low_pfn += compound_nr(page) - 1;
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun /* Successfully isolated */
1037*4882a593Smuzhiyun del_page_from_lru_list(page, lruvec, page_lru(page));
1038*4882a593Smuzhiyun mod_node_page_state(page_pgdat(page),
1039*4882a593Smuzhiyun NR_ISOLATED_ANON + page_is_file_lru(page),
1040*4882a593Smuzhiyun thp_nr_pages(page));
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun isolate_success:
1043*4882a593Smuzhiyun list_add(&page->lru, &cc->migratepages);
1044*4882a593Smuzhiyun cc->nr_migratepages += compound_nr(page);
1045*4882a593Smuzhiyun nr_isolated += compound_nr(page);
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun /*
1048*4882a593Smuzhiyun * Avoid isolating too much unless this block is being
1049*4882a593Smuzhiyun * rescanned (e.g. dirty/writeback pages, parallel allocation)
1050*4882a593Smuzhiyun * or a lock is contended. For contention, isolate quickly to
1051*4882a593Smuzhiyun * potentially remove one source of contention.
1052*4882a593Smuzhiyun */
1053*4882a593Smuzhiyun if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
1054*4882a593Smuzhiyun !cc->rescan && !cc->contended) {
1055*4882a593Smuzhiyun ++low_pfn;
1056*4882a593Smuzhiyun break;
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun
1059*4882a593Smuzhiyun continue;
1060*4882a593Smuzhiyun isolate_fail:
1061*4882a593Smuzhiyun if (!skip_on_failure)
1062*4882a593Smuzhiyun continue;
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun /*
1065*4882a593Smuzhiyun * We have isolated some pages, but then failed. Release them
1066*4882a593Smuzhiyun * instead of migrating, as we cannot form the cc->order buddy
1067*4882a593Smuzhiyun * page anyway.
1068*4882a593Smuzhiyun */
1069*4882a593Smuzhiyun if (nr_isolated) {
1070*4882a593Smuzhiyun if (locked) {
1071*4882a593Smuzhiyun spin_unlock_irqrestore(&pgdat->lru_lock, flags);
1072*4882a593Smuzhiyun locked = false;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun putback_movable_pages(&cc->migratepages);
1075*4882a593Smuzhiyun cc->nr_migratepages = 0;
1076*4882a593Smuzhiyun nr_isolated = 0;
1077*4882a593Smuzhiyun }
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun if (low_pfn < next_skip_pfn) {
1080*4882a593Smuzhiyun low_pfn = next_skip_pfn - 1;
1081*4882a593Smuzhiyun /*
1082*4882a593Smuzhiyun * The check near the loop beginning would have updated
1083*4882a593Smuzhiyun * next_skip_pfn too, but this is a bit simpler.
1084*4882a593Smuzhiyun */
1085*4882a593Smuzhiyun next_skip_pfn += 1UL << cc->order;
1086*4882a593Smuzhiyun }
1087*4882a593Smuzhiyun }
1088*4882a593Smuzhiyun
1089*4882a593Smuzhiyun /*
1090*4882a593Smuzhiyun * The PageBuddy() check could have potentially brought us outside
1091*4882a593Smuzhiyun * the range to be scanned.
1092*4882a593Smuzhiyun */
1093*4882a593Smuzhiyun if (unlikely(low_pfn > end_pfn))
1094*4882a593Smuzhiyun low_pfn = end_pfn;
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun isolate_abort:
1097*4882a593Smuzhiyun if (locked)
1098*4882a593Smuzhiyun spin_unlock_irqrestore(&pgdat->lru_lock, flags);
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun /*
1101*4882a593Smuzhiyun * Updated the cached scanner pfn once the pageblock has been scanned
1102*4882a593Smuzhiyun * Pages will either be migrated in which case there is no point
1103*4882a593Smuzhiyun * scanning in the near future or migration failed in which case the
1104*4882a593Smuzhiyun * failure reason may persist. The block is marked for skipping if
1105*4882a593Smuzhiyun * there were no pages isolated in the block or if the block is
1106*4882a593Smuzhiyun * rescanned twice in a row.
1107*4882a593Smuzhiyun */
1108*4882a593Smuzhiyun if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) {
1109*4882a593Smuzhiyun if (valid_page && !skip_updated)
1110*4882a593Smuzhiyun set_pageblock_skip(valid_page);
1111*4882a593Smuzhiyun update_cached_migrate(cc, low_pfn);
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
1115*4882a593Smuzhiyun nr_scanned, nr_isolated);
1116*4882a593Smuzhiyun
1117*4882a593Smuzhiyun fatal_pending:
1118*4882a593Smuzhiyun cc->total_migrate_scanned += nr_scanned;
1119*4882a593Smuzhiyun if (nr_isolated)
1120*4882a593Smuzhiyun count_compact_events(COMPACTISOLATED, nr_isolated);
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun return low_pfn;
1123*4882a593Smuzhiyun }
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun /**
1126*4882a593Smuzhiyun * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
1127*4882a593Smuzhiyun * @cc: Compaction control structure.
1128*4882a593Smuzhiyun * @start_pfn: The first PFN to start isolating.
1129*4882a593Smuzhiyun * @end_pfn: The one-past-last PFN.
1130*4882a593Smuzhiyun *
1131*4882a593Smuzhiyun * Returns zero if isolation fails fatally due to e.g. pending signal.
1132*4882a593Smuzhiyun * Otherwise, function returns one-past-the-last PFN of isolated page
1133*4882a593Smuzhiyun * (which may be greater than end_pfn if end fell in a middle of a THP page).
1134*4882a593Smuzhiyun */
1135*4882a593Smuzhiyun unsigned long
isolate_migratepages_range(struct compact_control * cc,unsigned long start_pfn,unsigned long end_pfn)1136*4882a593Smuzhiyun isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
1137*4882a593Smuzhiyun unsigned long end_pfn)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun unsigned long pfn, block_start_pfn, block_end_pfn;
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /* Scan block by block. First and last block may be incomplete */
1142*4882a593Smuzhiyun pfn = start_pfn;
1143*4882a593Smuzhiyun block_start_pfn = pageblock_start_pfn(pfn);
1144*4882a593Smuzhiyun if (block_start_pfn < cc->zone->zone_start_pfn)
1145*4882a593Smuzhiyun block_start_pfn = cc->zone->zone_start_pfn;
1146*4882a593Smuzhiyun block_end_pfn = pageblock_end_pfn(pfn);
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun for (; pfn < end_pfn; pfn = block_end_pfn,
1149*4882a593Smuzhiyun block_start_pfn = block_end_pfn,
1150*4882a593Smuzhiyun block_end_pfn += pageblock_nr_pages) {
1151*4882a593Smuzhiyun
1152*4882a593Smuzhiyun block_end_pfn = min(block_end_pfn, end_pfn);
1153*4882a593Smuzhiyun
1154*4882a593Smuzhiyun if (!pageblock_pfn_to_page(block_start_pfn,
1155*4882a593Smuzhiyun block_end_pfn, cc->zone))
1156*4882a593Smuzhiyun continue;
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
1159*4882a593Smuzhiyun ISOLATE_UNEVICTABLE);
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun if (!pfn)
1162*4882a593Smuzhiyun break;
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
1165*4882a593Smuzhiyun break;
1166*4882a593Smuzhiyun }
1167*4882a593Smuzhiyun
1168*4882a593Smuzhiyun return pfn;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun #endif /* CONFIG_COMPACTION || CONFIG_CMA */
1172*4882a593Smuzhiyun #ifdef CONFIG_COMPACTION
1173*4882a593Smuzhiyun
suitable_migration_source(struct compact_control * cc,struct page * page)1174*4882a593Smuzhiyun static bool suitable_migration_source(struct compact_control *cc,
1175*4882a593Smuzhiyun struct page *page)
1176*4882a593Smuzhiyun {
1177*4882a593Smuzhiyun int block_mt;
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun if (pageblock_skip_persistent(page))
1180*4882a593Smuzhiyun return false;
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
1183*4882a593Smuzhiyun return true;
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun block_mt = get_pageblock_migratetype(page);
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun if (cc->migratetype == MIGRATE_MOVABLE)
1188*4882a593Smuzhiyun return is_migrate_movable(block_mt);
1189*4882a593Smuzhiyun else
1190*4882a593Smuzhiyun return block_mt == cc->migratetype;
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun /* Returns true if the page is within a block suitable for migration to */
suitable_migration_target(struct compact_control * cc,struct page * page)1194*4882a593Smuzhiyun static bool suitable_migration_target(struct compact_control *cc,
1195*4882a593Smuzhiyun struct page *page)
1196*4882a593Smuzhiyun {
1197*4882a593Smuzhiyun /* If the page is a large free page, then disallow migration */
1198*4882a593Smuzhiyun if (PageBuddy(page)) {
1199*4882a593Smuzhiyun /*
1200*4882a593Smuzhiyun * We are checking page_order without zone->lock taken. But
1201*4882a593Smuzhiyun * the only small danger is that we skip a potentially suitable
1202*4882a593Smuzhiyun * pageblock, so it's not worth to check order for valid range.
1203*4882a593Smuzhiyun */
1204*4882a593Smuzhiyun if (buddy_order_unsafe(page) >= pageblock_order)
1205*4882a593Smuzhiyun return false;
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun if (cc->ignore_block_suitable)
1209*4882a593Smuzhiyun return true;
1210*4882a593Smuzhiyun
1211*4882a593Smuzhiyun /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
1212*4882a593Smuzhiyun if (is_migrate_movable(get_pageblock_migratetype(page)))
1213*4882a593Smuzhiyun return true;
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun /* Otherwise skip the block */
1216*4882a593Smuzhiyun return false;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun static inline unsigned int
freelist_scan_limit(struct compact_control * cc)1220*4882a593Smuzhiyun freelist_scan_limit(struct compact_control *cc)
1221*4882a593Smuzhiyun {
1222*4882a593Smuzhiyun unsigned short shift = BITS_PER_LONG - 1;
1223*4882a593Smuzhiyun
1224*4882a593Smuzhiyun return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1;
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun /*
1228*4882a593Smuzhiyun * Test whether the free scanner has reached the same or lower pageblock than
1229*4882a593Smuzhiyun * the migration scanner, and compaction should thus terminate.
1230*4882a593Smuzhiyun */
compact_scanners_met(struct compact_control * cc)1231*4882a593Smuzhiyun static inline bool compact_scanners_met(struct compact_control *cc)
1232*4882a593Smuzhiyun {
1233*4882a593Smuzhiyun return (cc->free_pfn >> pageblock_order)
1234*4882a593Smuzhiyun <= (cc->migrate_pfn >> pageblock_order);
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun /*
1238*4882a593Smuzhiyun * Used when scanning for a suitable migration target which scans freelists
1239*4882a593Smuzhiyun * in reverse. Reorders the list such as the unscanned pages are scanned
1240*4882a593Smuzhiyun * first on the next iteration of the free scanner
1241*4882a593Smuzhiyun */
1242*4882a593Smuzhiyun static void
move_freelist_head(struct list_head * freelist,struct page * freepage)1243*4882a593Smuzhiyun move_freelist_head(struct list_head *freelist, struct page *freepage)
1244*4882a593Smuzhiyun {
1245*4882a593Smuzhiyun LIST_HEAD(sublist);
1246*4882a593Smuzhiyun
1247*4882a593Smuzhiyun if (!list_is_last(freelist, &freepage->lru)) {
1248*4882a593Smuzhiyun list_cut_before(&sublist, freelist, &freepage->lru);
1249*4882a593Smuzhiyun if (!list_empty(&sublist))
1250*4882a593Smuzhiyun list_splice_tail(&sublist, freelist);
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun
1254*4882a593Smuzhiyun /*
1255*4882a593Smuzhiyun * Similar to move_freelist_head except used by the migration scanner
1256*4882a593Smuzhiyun * when scanning forward. It's possible for these list operations to
1257*4882a593Smuzhiyun * move against each other if they search the free list exactly in
1258*4882a593Smuzhiyun * lockstep.
1259*4882a593Smuzhiyun */
1260*4882a593Smuzhiyun static void
move_freelist_tail(struct list_head * freelist,struct page * freepage)1261*4882a593Smuzhiyun move_freelist_tail(struct list_head *freelist, struct page *freepage)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun LIST_HEAD(sublist);
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun if (!list_is_first(freelist, &freepage->lru)) {
1266*4882a593Smuzhiyun list_cut_position(&sublist, freelist, &freepage->lru);
1267*4882a593Smuzhiyun if (!list_empty(&sublist))
1268*4882a593Smuzhiyun list_splice_tail(&sublist, freelist);
1269*4882a593Smuzhiyun }
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun static void
fast_isolate_around(struct compact_control * cc,unsigned long pfn,unsigned long nr_isolated)1273*4882a593Smuzhiyun fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun unsigned long start_pfn, end_pfn;
1276*4882a593Smuzhiyun struct page *page;
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun /* Do not search around if there are enough pages already */
1279*4882a593Smuzhiyun if (cc->nr_freepages >= cc->nr_migratepages)
1280*4882a593Smuzhiyun return;
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun /* Minimise scanning during async compaction */
1283*4882a593Smuzhiyun if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
1284*4882a593Smuzhiyun return;
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun /* Pageblock boundaries */
1287*4882a593Smuzhiyun start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn);
1288*4882a593Smuzhiyun end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone));
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone);
1291*4882a593Smuzhiyun if (!page)
1292*4882a593Smuzhiyun return;
1293*4882a593Smuzhiyun
1294*4882a593Smuzhiyun /* Scan before */
1295*4882a593Smuzhiyun if (start_pfn != pfn) {
1296*4882a593Smuzhiyun isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false);
1297*4882a593Smuzhiyun if (cc->nr_freepages >= cc->nr_migratepages)
1298*4882a593Smuzhiyun return;
1299*4882a593Smuzhiyun }
1300*4882a593Smuzhiyun
1301*4882a593Smuzhiyun /* Scan after */
1302*4882a593Smuzhiyun start_pfn = pfn + nr_isolated;
1303*4882a593Smuzhiyun if (start_pfn < end_pfn)
1304*4882a593Smuzhiyun isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun /* Skip this pageblock in the future as it's full or nearly full */
1307*4882a593Smuzhiyun if (cc->nr_freepages < cc->nr_migratepages)
1308*4882a593Smuzhiyun set_pageblock_skip(page);
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun /* Search orders in round-robin fashion */
next_search_order(struct compact_control * cc,int order)1312*4882a593Smuzhiyun static int next_search_order(struct compact_control *cc, int order)
1313*4882a593Smuzhiyun {
1314*4882a593Smuzhiyun order--;
1315*4882a593Smuzhiyun if (order < 0)
1316*4882a593Smuzhiyun order = cc->order - 1;
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun /* Search wrapped around? */
1319*4882a593Smuzhiyun if (order == cc->search_order) {
1320*4882a593Smuzhiyun cc->search_order--;
1321*4882a593Smuzhiyun if (cc->search_order < 0)
1322*4882a593Smuzhiyun cc->search_order = cc->order - 1;
1323*4882a593Smuzhiyun return -1;
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun return order;
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun static unsigned long
fast_isolate_freepages(struct compact_control * cc)1330*4882a593Smuzhiyun fast_isolate_freepages(struct compact_control *cc)
1331*4882a593Smuzhiyun {
1332*4882a593Smuzhiyun unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1);
1333*4882a593Smuzhiyun unsigned int nr_scanned = 0;
1334*4882a593Smuzhiyun unsigned long low_pfn, min_pfn, highest = 0;
1335*4882a593Smuzhiyun unsigned long nr_isolated = 0;
1336*4882a593Smuzhiyun unsigned long distance;
1337*4882a593Smuzhiyun struct page *page = NULL;
1338*4882a593Smuzhiyun bool scan_start = false;
1339*4882a593Smuzhiyun int order;
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun /* Full compaction passes in a negative order */
1342*4882a593Smuzhiyun if (cc->order <= 0)
1343*4882a593Smuzhiyun return cc->free_pfn;
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun /*
1346*4882a593Smuzhiyun * If starting the scan, use a deeper search and use the highest
1347*4882a593Smuzhiyun * PFN found if a suitable one is not found.
1348*4882a593Smuzhiyun */
1349*4882a593Smuzhiyun if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
1350*4882a593Smuzhiyun limit = pageblock_nr_pages >> 1;
1351*4882a593Smuzhiyun scan_start = true;
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun /*
1355*4882a593Smuzhiyun * Preferred point is in the top quarter of the scan space but take
1356*4882a593Smuzhiyun * a pfn from the top half if the search is problematic.
1357*4882a593Smuzhiyun */
1358*4882a593Smuzhiyun distance = (cc->free_pfn - cc->migrate_pfn);
1359*4882a593Smuzhiyun low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
1360*4882a593Smuzhiyun min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun if (WARN_ON_ONCE(min_pfn > low_pfn))
1363*4882a593Smuzhiyun low_pfn = min_pfn;
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun /*
1366*4882a593Smuzhiyun * Search starts from the last successful isolation order or the next
1367*4882a593Smuzhiyun * order to search after a previous failure
1368*4882a593Smuzhiyun */
1369*4882a593Smuzhiyun cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun for (order = cc->search_order;
1372*4882a593Smuzhiyun !page && order >= 0;
1373*4882a593Smuzhiyun order = next_search_order(cc, order)) {
1374*4882a593Smuzhiyun struct free_area *area = &cc->zone->free_area[order];
1375*4882a593Smuzhiyun struct list_head *freelist;
1376*4882a593Smuzhiyun struct page *freepage;
1377*4882a593Smuzhiyun unsigned long flags;
1378*4882a593Smuzhiyun unsigned int order_scanned = 0;
1379*4882a593Smuzhiyun unsigned long high_pfn = 0;
1380*4882a593Smuzhiyun
1381*4882a593Smuzhiyun if (!area->nr_free)
1382*4882a593Smuzhiyun continue;
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun spin_lock_irqsave(&cc->zone->lock, flags);
1385*4882a593Smuzhiyun freelist = &area->free_list[MIGRATE_MOVABLE];
1386*4882a593Smuzhiyun list_for_each_entry_reverse(freepage, freelist, lru) {
1387*4882a593Smuzhiyun unsigned long pfn;
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun order_scanned++;
1390*4882a593Smuzhiyun nr_scanned++;
1391*4882a593Smuzhiyun pfn = page_to_pfn(freepage);
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun if (pfn >= highest)
1394*4882a593Smuzhiyun highest = max(pageblock_start_pfn(pfn),
1395*4882a593Smuzhiyun cc->zone->zone_start_pfn);
1396*4882a593Smuzhiyun
1397*4882a593Smuzhiyun if (pfn >= low_pfn) {
1398*4882a593Smuzhiyun cc->fast_search_fail = 0;
1399*4882a593Smuzhiyun cc->search_order = order;
1400*4882a593Smuzhiyun page = freepage;
1401*4882a593Smuzhiyun break;
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun if (pfn >= min_pfn && pfn > high_pfn) {
1405*4882a593Smuzhiyun high_pfn = pfn;
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun /* Shorten the scan if a candidate is found */
1408*4882a593Smuzhiyun limit >>= 1;
1409*4882a593Smuzhiyun }
1410*4882a593Smuzhiyun
1411*4882a593Smuzhiyun if (order_scanned >= limit)
1412*4882a593Smuzhiyun break;
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun
1415*4882a593Smuzhiyun /* Use a minimum pfn if a preferred one was not found */
1416*4882a593Smuzhiyun if (!page && high_pfn) {
1417*4882a593Smuzhiyun page = pfn_to_page(high_pfn);
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun /* Update freepage for the list reorder below */
1420*4882a593Smuzhiyun freepage = page;
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun /* Reorder to so a future search skips recent pages */
1424*4882a593Smuzhiyun move_freelist_head(freelist, freepage);
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun /* Isolate the page if available */
1427*4882a593Smuzhiyun if (page) {
1428*4882a593Smuzhiyun if (__isolate_free_page(page, order)) {
1429*4882a593Smuzhiyun set_page_private(page, order);
1430*4882a593Smuzhiyun nr_isolated = 1 << order;
1431*4882a593Smuzhiyun cc->nr_freepages += nr_isolated;
1432*4882a593Smuzhiyun list_add_tail(&page->lru, &cc->freepages);
1433*4882a593Smuzhiyun count_compact_events(COMPACTISOLATED, nr_isolated);
1434*4882a593Smuzhiyun } else {
1435*4882a593Smuzhiyun /* If isolation fails, abort the search */
1436*4882a593Smuzhiyun order = cc->search_order + 1;
1437*4882a593Smuzhiyun page = NULL;
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun spin_unlock_irqrestore(&cc->zone->lock, flags);
1442*4882a593Smuzhiyun
1443*4882a593Smuzhiyun /*
1444*4882a593Smuzhiyun * Smaller scan on next order so the total scan ig related
1445*4882a593Smuzhiyun * to freelist_scan_limit.
1446*4882a593Smuzhiyun */
1447*4882a593Smuzhiyun if (order_scanned >= limit)
1448*4882a593Smuzhiyun limit = min(1U, limit >> 1);
1449*4882a593Smuzhiyun }
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun if (!page) {
1452*4882a593Smuzhiyun cc->fast_search_fail++;
1453*4882a593Smuzhiyun if (scan_start) {
1454*4882a593Smuzhiyun /*
1455*4882a593Smuzhiyun * Use the highest PFN found above min. If one was
1456*4882a593Smuzhiyun * not found, be pessimistic for direct compaction
1457*4882a593Smuzhiyun * and use the min mark.
1458*4882a593Smuzhiyun */
1459*4882a593Smuzhiyun if (highest) {
1460*4882a593Smuzhiyun page = pfn_to_page(highest);
1461*4882a593Smuzhiyun cc->free_pfn = highest;
1462*4882a593Smuzhiyun } else {
1463*4882a593Smuzhiyun if (cc->direct_compaction && pfn_valid(min_pfn)) {
1464*4882a593Smuzhiyun page = pageblock_pfn_to_page(min_pfn,
1465*4882a593Smuzhiyun min(pageblock_end_pfn(min_pfn),
1466*4882a593Smuzhiyun zone_end_pfn(cc->zone)),
1467*4882a593Smuzhiyun cc->zone);
1468*4882a593Smuzhiyun cc->free_pfn = min_pfn;
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1475*4882a593Smuzhiyun highest -= pageblock_nr_pages;
1476*4882a593Smuzhiyun cc->zone->compact_cached_free_pfn = highest;
1477*4882a593Smuzhiyun }
1478*4882a593Smuzhiyun
1479*4882a593Smuzhiyun cc->total_free_scanned += nr_scanned;
1480*4882a593Smuzhiyun if (!page)
1481*4882a593Smuzhiyun return cc->free_pfn;
1482*4882a593Smuzhiyun
1483*4882a593Smuzhiyun low_pfn = page_to_pfn(page);
1484*4882a593Smuzhiyun fast_isolate_around(cc, low_pfn, nr_isolated);
1485*4882a593Smuzhiyun return low_pfn;
1486*4882a593Smuzhiyun }
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun /*
1489*4882a593Smuzhiyun * Based on information in the current compact_control, find blocks
1490*4882a593Smuzhiyun * suitable for isolating free pages from and then isolate them.
1491*4882a593Smuzhiyun */
isolate_freepages(struct compact_control * cc)1492*4882a593Smuzhiyun static void isolate_freepages(struct compact_control *cc)
1493*4882a593Smuzhiyun {
1494*4882a593Smuzhiyun struct zone *zone = cc->zone;
1495*4882a593Smuzhiyun struct page *page;
1496*4882a593Smuzhiyun unsigned long block_start_pfn; /* start of current pageblock */
1497*4882a593Smuzhiyun unsigned long isolate_start_pfn; /* exact pfn we start at */
1498*4882a593Smuzhiyun unsigned long block_end_pfn; /* end of current pageblock */
1499*4882a593Smuzhiyun unsigned long low_pfn; /* lowest pfn scanner is able to scan */
1500*4882a593Smuzhiyun struct list_head *freelist = &cc->freepages;
1501*4882a593Smuzhiyun unsigned int stride;
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun /* Try a small search of the free lists for a candidate */
1504*4882a593Smuzhiyun isolate_start_pfn = fast_isolate_freepages(cc);
1505*4882a593Smuzhiyun if (cc->nr_freepages)
1506*4882a593Smuzhiyun goto splitmap;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun /*
1509*4882a593Smuzhiyun * Initialise the free scanner. The starting point is where we last
1510*4882a593Smuzhiyun * successfully isolated from, zone-cached value, or the end of the
1511*4882a593Smuzhiyun * zone when isolating for the first time. For looping we also need
1512*4882a593Smuzhiyun * this pfn aligned down to the pageblock boundary, because we do
1513*4882a593Smuzhiyun * block_start_pfn -= pageblock_nr_pages in the for loop.
1514*4882a593Smuzhiyun * For ending point, take care when isolating in last pageblock of a
1515*4882a593Smuzhiyun * zone which ends in the middle of a pageblock.
1516*4882a593Smuzhiyun * The low boundary is the end of the pageblock the migration scanner
1517*4882a593Smuzhiyun * is using.
1518*4882a593Smuzhiyun */
1519*4882a593Smuzhiyun isolate_start_pfn = cc->free_pfn;
1520*4882a593Smuzhiyun block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
1521*4882a593Smuzhiyun block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1522*4882a593Smuzhiyun zone_end_pfn(zone));
1523*4882a593Smuzhiyun low_pfn = pageblock_end_pfn(cc->migrate_pfn);
1524*4882a593Smuzhiyun stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
1525*4882a593Smuzhiyun
1526*4882a593Smuzhiyun /*
1527*4882a593Smuzhiyun * Isolate free pages until enough are available to migrate the
1528*4882a593Smuzhiyun * pages on cc->migratepages. We stop searching if the migrate
1529*4882a593Smuzhiyun * and free page scanners meet or enough free pages are isolated.
1530*4882a593Smuzhiyun */
1531*4882a593Smuzhiyun for (; block_start_pfn >= low_pfn;
1532*4882a593Smuzhiyun block_end_pfn = block_start_pfn,
1533*4882a593Smuzhiyun block_start_pfn -= pageblock_nr_pages,
1534*4882a593Smuzhiyun isolate_start_pfn = block_start_pfn) {
1535*4882a593Smuzhiyun unsigned long nr_isolated;
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun /*
1538*4882a593Smuzhiyun * This can iterate a massively long zone without finding any
1539*4882a593Smuzhiyun * suitable migration targets, so periodically check resched.
1540*4882a593Smuzhiyun */
1541*4882a593Smuzhiyun if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1542*4882a593Smuzhiyun cond_resched();
1543*4882a593Smuzhiyun
1544*4882a593Smuzhiyun page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1545*4882a593Smuzhiyun zone);
1546*4882a593Smuzhiyun if (!page)
1547*4882a593Smuzhiyun continue;
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun /* Check the block is suitable for migration */
1550*4882a593Smuzhiyun if (!suitable_migration_target(cc, page))
1551*4882a593Smuzhiyun continue;
1552*4882a593Smuzhiyun
1553*4882a593Smuzhiyun /* If isolation recently failed, do not retry */
1554*4882a593Smuzhiyun if (!isolation_suitable(cc, page))
1555*4882a593Smuzhiyun continue;
1556*4882a593Smuzhiyun
1557*4882a593Smuzhiyun /* Found a block suitable for isolating free pages from. */
1558*4882a593Smuzhiyun nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
1559*4882a593Smuzhiyun block_end_pfn, freelist, stride, false);
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun /* Update the skip hint if the full pageblock was scanned */
1562*4882a593Smuzhiyun if (isolate_start_pfn == block_end_pfn)
1563*4882a593Smuzhiyun update_pageblock_skip(cc, page, block_start_pfn);
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun /* Are enough freepages isolated? */
1566*4882a593Smuzhiyun if (cc->nr_freepages >= cc->nr_migratepages) {
1567*4882a593Smuzhiyun if (isolate_start_pfn >= block_end_pfn) {
1568*4882a593Smuzhiyun /*
1569*4882a593Smuzhiyun * Restart at previous pageblock if more
1570*4882a593Smuzhiyun * freepages can be isolated next time.
1571*4882a593Smuzhiyun */
1572*4882a593Smuzhiyun isolate_start_pfn =
1573*4882a593Smuzhiyun block_start_pfn - pageblock_nr_pages;
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun break;
1576*4882a593Smuzhiyun } else if (isolate_start_pfn < block_end_pfn) {
1577*4882a593Smuzhiyun /*
1578*4882a593Smuzhiyun * If isolation failed early, do not continue
1579*4882a593Smuzhiyun * needlessly.
1580*4882a593Smuzhiyun */
1581*4882a593Smuzhiyun break;
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun
1584*4882a593Smuzhiyun /* Adjust stride depending on isolation */
1585*4882a593Smuzhiyun if (nr_isolated) {
1586*4882a593Smuzhiyun stride = 1;
1587*4882a593Smuzhiyun continue;
1588*4882a593Smuzhiyun }
1589*4882a593Smuzhiyun stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
1590*4882a593Smuzhiyun }
1591*4882a593Smuzhiyun
1592*4882a593Smuzhiyun /*
1593*4882a593Smuzhiyun * Record where the free scanner will restart next time. Either we
1594*4882a593Smuzhiyun * broke from the loop and set isolate_start_pfn based on the last
1595*4882a593Smuzhiyun * call to isolate_freepages_block(), or we met the migration scanner
1596*4882a593Smuzhiyun * and the loop terminated due to isolate_start_pfn < low_pfn
1597*4882a593Smuzhiyun */
1598*4882a593Smuzhiyun cc->free_pfn = isolate_start_pfn;
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun splitmap:
1601*4882a593Smuzhiyun /* __isolate_free_page() does not map the pages */
1602*4882a593Smuzhiyun split_map_pages(freelist);
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun /*
1606*4882a593Smuzhiyun * This is a migrate-callback that "allocates" freepages by taking pages
1607*4882a593Smuzhiyun * from the isolated freelists in the block we are migrating to.
1608*4882a593Smuzhiyun */
compaction_alloc(struct page * migratepage,unsigned long data)1609*4882a593Smuzhiyun static struct page *compaction_alloc(struct page *migratepage,
1610*4882a593Smuzhiyun unsigned long data)
1611*4882a593Smuzhiyun {
1612*4882a593Smuzhiyun struct compact_control *cc = (struct compact_control *)data;
1613*4882a593Smuzhiyun struct page *freepage;
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun if (list_empty(&cc->freepages)) {
1616*4882a593Smuzhiyun isolate_freepages(cc);
1617*4882a593Smuzhiyun
1618*4882a593Smuzhiyun if (list_empty(&cc->freepages))
1619*4882a593Smuzhiyun return NULL;
1620*4882a593Smuzhiyun }
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun freepage = list_entry(cc->freepages.next, struct page, lru);
1623*4882a593Smuzhiyun list_del(&freepage->lru);
1624*4882a593Smuzhiyun cc->nr_freepages--;
1625*4882a593Smuzhiyun
1626*4882a593Smuzhiyun return freepage;
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun /*
1630*4882a593Smuzhiyun * This is a migrate-callback that "frees" freepages back to the isolated
1631*4882a593Smuzhiyun * freelist. All pages on the freelist are from the same zone, so there is no
1632*4882a593Smuzhiyun * special handling needed for NUMA.
1633*4882a593Smuzhiyun */
compaction_free(struct page * page,unsigned long data)1634*4882a593Smuzhiyun static void compaction_free(struct page *page, unsigned long data)
1635*4882a593Smuzhiyun {
1636*4882a593Smuzhiyun struct compact_control *cc = (struct compact_control *)data;
1637*4882a593Smuzhiyun
1638*4882a593Smuzhiyun list_add(&page->lru, &cc->freepages);
1639*4882a593Smuzhiyun cc->nr_freepages++;
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun /* possible outcome of isolate_migratepages */
1643*4882a593Smuzhiyun typedef enum {
1644*4882a593Smuzhiyun ISOLATE_ABORT, /* Abort compaction now */
1645*4882a593Smuzhiyun ISOLATE_NONE, /* No pages isolated, continue scanning */
1646*4882a593Smuzhiyun ISOLATE_SUCCESS, /* Pages isolated, migrate */
1647*4882a593Smuzhiyun } isolate_migrate_t;
1648*4882a593Smuzhiyun
1649*4882a593Smuzhiyun /*
1650*4882a593Smuzhiyun * Allow userspace to control policy on scanning the unevictable LRU for
1651*4882a593Smuzhiyun * compactable pages.
1652*4882a593Smuzhiyun */
1653*4882a593Smuzhiyun #ifdef CONFIG_PREEMPT_RT
1654*4882a593Smuzhiyun int sysctl_compact_unevictable_allowed __read_mostly = 0;
1655*4882a593Smuzhiyun #else
1656*4882a593Smuzhiyun int sysctl_compact_unevictable_allowed __read_mostly = 1;
1657*4882a593Smuzhiyun #endif
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun static inline void
update_fast_start_pfn(struct compact_control * cc,unsigned long pfn)1660*4882a593Smuzhiyun update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
1661*4882a593Smuzhiyun {
1662*4882a593Smuzhiyun if (cc->fast_start_pfn == ULONG_MAX)
1663*4882a593Smuzhiyun return;
1664*4882a593Smuzhiyun
1665*4882a593Smuzhiyun if (!cc->fast_start_pfn)
1666*4882a593Smuzhiyun cc->fast_start_pfn = pfn;
1667*4882a593Smuzhiyun
1668*4882a593Smuzhiyun cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
1669*4882a593Smuzhiyun }
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun static inline unsigned long
reinit_migrate_pfn(struct compact_control * cc)1672*4882a593Smuzhiyun reinit_migrate_pfn(struct compact_control *cc)
1673*4882a593Smuzhiyun {
1674*4882a593Smuzhiyun if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
1675*4882a593Smuzhiyun return cc->migrate_pfn;
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun cc->migrate_pfn = cc->fast_start_pfn;
1678*4882a593Smuzhiyun cc->fast_start_pfn = ULONG_MAX;
1679*4882a593Smuzhiyun
1680*4882a593Smuzhiyun return cc->migrate_pfn;
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun
1683*4882a593Smuzhiyun /*
1684*4882a593Smuzhiyun * Briefly search the free lists for a migration source that already has
1685*4882a593Smuzhiyun * some free pages to reduce the number of pages that need migration
1686*4882a593Smuzhiyun * before a pageblock is free.
1687*4882a593Smuzhiyun */
fast_find_migrateblock(struct compact_control * cc)1688*4882a593Smuzhiyun static unsigned long fast_find_migrateblock(struct compact_control *cc)
1689*4882a593Smuzhiyun {
1690*4882a593Smuzhiyun unsigned int limit = freelist_scan_limit(cc);
1691*4882a593Smuzhiyun unsigned int nr_scanned = 0;
1692*4882a593Smuzhiyun unsigned long distance;
1693*4882a593Smuzhiyun unsigned long pfn = cc->migrate_pfn;
1694*4882a593Smuzhiyun unsigned long high_pfn;
1695*4882a593Smuzhiyun int order;
1696*4882a593Smuzhiyun bool found_block = false;
1697*4882a593Smuzhiyun
1698*4882a593Smuzhiyun /* Skip hints are relied on to avoid repeats on the fast search */
1699*4882a593Smuzhiyun if (cc->ignore_skip_hint)
1700*4882a593Smuzhiyun return pfn;
1701*4882a593Smuzhiyun
1702*4882a593Smuzhiyun /*
1703*4882a593Smuzhiyun * If the migrate_pfn is not at the start of a zone or the start
1704*4882a593Smuzhiyun * of a pageblock then assume this is a continuation of a previous
1705*4882a593Smuzhiyun * scan restarted due to COMPACT_CLUSTER_MAX.
1706*4882a593Smuzhiyun */
1707*4882a593Smuzhiyun if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
1708*4882a593Smuzhiyun return pfn;
1709*4882a593Smuzhiyun
1710*4882a593Smuzhiyun /*
1711*4882a593Smuzhiyun * For smaller orders, just linearly scan as the number of pages
1712*4882a593Smuzhiyun * to migrate should be relatively small and does not necessarily
1713*4882a593Smuzhiyun * justify freeing up a large block for a small allocation.
1714*4882a593Smuzhiyun */
1715*4882a593Smuzhiyun if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
1716*4882a593Smuzhiyun return pfn;
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun /*
1719*4882a593Smuzhiyun * Only allow kcompactd and direct requests for movable pages to
1720*4882a593Smuzhiyun * quickly clear out a MOVABLE pageblock for allocation. This
1721*4882a593Smuzhiyun * reduces the risk that a large movable pageblock is freed for
1722*4882a593Smuzhiyun * an unmovable/reclaimable small allocation.
1723*4882a593Smuzhiyun */
1724*4882a593Smuzhiyun if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
1725*4882a593Smuzhiyun return pfn;
1726*4882a593Smuzhiyun
1727*4882a593Smuzhiyun /*
1728*4882a593Smuzhiyun * When starting the migration scanner, pick any pageblock within the
1729*4882a593Smuzhiyun * first half of the search space. Otherwise try and pick a pageblock
1730*4882a593Smuzhiyun * within the first eighth to reduce the chances that a migration
1731*4882a593Smuzhiyun * target later becomes a source.
1732*4882a593Smuzhiyun */
1733*4882a593Smuzhiyun distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
1734*4882a593Smuzhiyun if (cc->migrate_pfn != cc->zone->zone_start_pfn)
1735*4882a593Smuzhiyun distance >>= 2;
1736*4882a593Smuzhiyun high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
1737*4882a593Smuzhiyun
1738*4882a593Smuzhiyun for (order = cc->order - 1;
1739*4882a593Smuzhiyun order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit;
1740*4882a593Smuzhiyun order--) {
1741*4882a593Smuzhiyun struct free_area *area = &cc->zone->free_area[order];
1742*4882a593Smuzhiyun struct list_head *freelist;
1743*4882a593Smuzhiyun unsigned long flags;
1744*4882a593Smuzhiyun struct page *freepage;
1745*4882a593Smuzhiyun
1746*4882a593Smuzhiyun if (!area->nr_free)
1747*4882a593Smuzhiyun continue;
1748*4882a593Smuzhiyun
1749*4882a593Smuzhiyun spin_lock_irqsave(&cc->zone->lock, flags);
1750*4882a593Smuzhiyun freelist = &area->free_list[MIGRATE_MOVABLE];
1751*4882a593Smuzhiyun list_for_each_entry(freepage, freelist, lru) {
1752*4882a593Smuzhiyun unsigned long free_pfn;
1753*4882a593Smuzhiyun
1754*4882a593Smuzhiyun if (nr_scanned++ >= limit) {
1755*4882a593Smuzhiyun move_freelist_tail(freelist, freepage);
1756*4882a593Smuzhiyun break;
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun
1759*4882a593Smuzhiyun free_pfn = page_to_pfn(freepage);
1760*4882a593Smuzhiyun if (free_pfn < high_pfn) {
1761*4882a593Smuzhiyun /*
1762*4882a593Smuzhiyun * Avoid if skipped recently. Ideally it would
1763*4882a593Smuzhiyun * move to the tail but even safe iteration of
1764*4882a593Smuzhiyun * the list assumes an entry is deleted, not
1765*4882a593Smuzhiyun * reordered.
1766*4882a593Smuzhiyun */
1767*4882a593Smuzhiyun if (get_pageblock_skip(freepage))
1768*4882a593Smuzhiyun continue;
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun /* Reorder to so a future search skips recent pages */
1771*4882a593Smuzhiyun move_freelist_tail(freelist, freepage);
1772*4882a593Smuzhiyun
1773*4882a593Smuzhiyun update_fast_start_pfn(cc, free_pfn);
1774*4882a593Smuzhiyun pfn = pageblock_start_pfn(free_pfn);
1775*4882a593Smuzhiyun if (pfn < cc->zone->zone_start_pfn)
1776*4882a593Smuzhiyun pfn = cc->zone->zone_start_pfn;
1777*4882a593Smuzhiyun cc->fast_search_fail = 0;
1778*4882a593Smuzhiyun found_block = true;
1779*4882a593Smuzhiyun set_pageblock_skip(freepage);
1780*4882a593Smuzhiyun break;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun }
1783*4882a593Smuzhiyun spin_unlock_irqrestore(&cc->zone->lock, flags);
1784*4882a593Smuzhiyun }
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun cc->total_migrate_scanned += nr_scanned;
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun /*
1789*4882a593Smuzhiyun * If fast scanning failed then use a cached entry for a page block
1790*4882a593Smuzhiyun * that had free pages as the basis for starting a linear scan.
1791*4882a593Smuzhiyun */
1792*4882a593Smuzhiyun if (!found_block) {
1793*4882a593Smuzhiyun cc->fast_search_fail++;
1794*4882a593Smuzhiyun pfn = reinit_migrate_pfn(cc);
1795*4882a593Smuzhiyun }
1796*4882a593Smuzhiyun return pfn;
1797*4882a593Smuzhiyun }
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun /*
1800*4882a593Smuzhiyun * Isolate all pages that can be migrated from the first suitable block,
1801*4882a593Smuzhiyun * starting at the block pointed to by the migrate scanner pfn within
1802*4882a593Smuzhiyun * compact_control.
1803*4882a593Smuzhiyun */
isolate_migratepages(struct compact_control * cc)1804*4882a593Smuzhiyun static isolate_migrate_t isolate_migratepages(struct compact_control *cc)
1805*4882a593Smuzhiyun {
1806*4882a593Smuzhiyun unsigned long block_start_pfn;
1807*4882a593Smuzhiyun unsigned long block_end_pfn;
1808*4882a593Smuzhiyun unsigned long low_pfn;
1809*4882a593Smuzhiyun struct page *page;
1810*4882a593Smuzhiyun const isolate_mode_t isolate_mode =
1811*4882a593Smuzhiyun (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
1812*4882a593Smuzhiyun (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
1813*4882a593Smuzhiyun bool fast_find_block;
1814*4882a593Smuzhiyun
1815*4882a593Smuzhiyun /*
1816*4882a593Smuzhiyun * Start at where we last stopped, or beginning of the zone as
1817*4882a593Smuzhiyun * initialized by compact_zone(). The first failure will use
1818*4882a593Smuzhiyun * the lowest PFN as the starting point for linear scanning.
1819*4882a593Smuzhiyun */
1820*4882a593Smuzhiyun low_pfn = fast_find_migrateblock(cc);
1821*4882a593Smuzhiyun block_start_pfn = pageblock_start_pfn(low_pfn);
1822*4882a593Smuzhiyun if (block_start_pfn < cc->zone->zone_start_pfn)
1823*4882a593Smuzhiyun block_start_pfn = cc->zone->zone_start_pfn;
1824*4882a593Smuzhiyun
1825*4882a593Smuzhiyun /*
1826*4882a593Smuzhiyun * fast_find_migrateblock marks a pageblock skipped so to avoid
1827*4882a593Smuzhiyun * the isolation_suitable check below, check whether the fast
1828*4882a593Smuzhiyun * search was successful.
1829*4882a593Smuzhiyun */
1830*4882a593Smuzhiyun fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
1831*4882a593Smuzhiyun
1832*4882a593Smuzhiyun /* Only scan within a pageblock boundary */
1833*4882a593Smuzhiyun block_end_pfn = pageblock_end_pfn(low_pfn);
1834*4882a593Smuzhiyun
1835*4882a593Smuzhiyun /*
1836*4882a593Smuzhiyun * Iterate over whole pageblocks until we find the first suitable.
1837*4882a593Smuzhiyun * Do not cross the free scanner.
1838*4882a593Smuzhiyun */
1839*4882a593Smuzhiyun for (; block_end_pfn <= cc->free_pfn;
1840*4882a593Smuzhiyun fast_find_block = false,
1841*4882a593Smuzhiyun low_pfn = block_end_pfn,
1842*4882a593Smuzhiyun block_start_pfn = block_end_pfn,
1843*4882a593Smuzhiyun block_end_pfn += pageblock_nr_pages) {
1844*4882a593Smuzhiyun
1845*4882a593Smuzhiyun /*
1846*4882a593Smuzhiyun * This can potentially iterate a massively long zone with
1847*4882a593Smuzhiyun * many pageblocks unsuitable, so periodically check if we
1848*4882a593Smuzhiyun * need to schedule.
1849*4882a593Smuzhiyun */
1850*4882a593Smuzhiyun if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1851*4882a593Smuzhiyun cond_resched();
1852*4882a593Smuzhiyun
1853*4882a593Smuzhiyun page = pageblock_pfn_to_page(block_start_pfn,
1854*4882a593Smuzhiyun block_end_pfn, cc->zone);
1855*4882a593Smuzhiyun if (!page)
1856*4882a593Smuzhiyun continue;
1857*4882a593Smuzhiyun
1858*4882a593Smuzhiyun /*
1859*4882a593Smuzhiyun * If isolation recently failed, do not retry. Only check the
1860*4882a593Smuzhiyun * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
1861*4882a593Smuzhiyun * to be visited multiple times. Assume skip was checked
1862*4882a593Smuzhiyun * before making it "skip" so other compaction instances do
1863*4882a593Smuzhiyun * not scan the same block.
1864*4882a593Smuzhiyun */
1865*4882a593Smuzhiyun if (IS_ALIGNED(low_pfn, pageblock_nr_pages) &&
1866*4882a593Smuzhiyun !fast_find_block && !isolation_suitable(cc, page))
1867*4882a593Smuzhiyun continue;
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun /*
1870*4882a593Smuzhiyun * For async compaction, also only scan in MOVABLE blocks
1871*4882a593Smuzhiyun * without huge pages. Async compaction is optimistic to see
1872*4882a593Smuzhiyun * if the minimum amount of work satisfies the allocation.
1873*4882a593Smuzhiyun * The cached PFN is updated as it's possible that all
1874*4882a593Smuzhiyun * remaining blocks between source and target are unsuitable
1875*4882a593Smuzhiyun * and the compaction scanners fail to meet.
1876*4882a593Smuzhiyun */
1877*4882a593Smuzhiyun if (!suitable_migration_source(cc, page)) {
1878*4882a593Smuzhiyun update_cached_migrate(cc, block_end_pfn);
1879*4882a593Smuzhiyun continue;
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun
1882*4882a593Smuzhiyun /* Perform the isolation */
1883*4882a593Smuzhiyun low_pfn = isolate_migratepages_block(cc, low_pfn,
1884*4882a593Smuzhiyun block_end_pfn, isolate_mode);
1885*4882a593Smuzhiyun
1886*4882a593Smuzhiyun if (!low_pfn)
1887*4882a593Smuzhiyun return ISOLATE_ABORT;
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun /*
1890*4882a593Smuzhiyun * Either we isolated something and proceed with migration. Or
1891*4882a593Smuzhiyun * we failed and compact_zone should decide if we should
1892*4882a593Smuzhiyun * continue or not.
1893*4882a593Smuzhiyun */
1894*4882a593Smuzhiyun break;
1895*4882a593Smuzhiyun }
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun /* Record where migration scanner will be restarted. */
1898*4882a593Smuzhiyun cc->migrate_pfn = low_pfn;
1899*4882a593Smuzhiyun
1900*4882a593Smuzhiyun return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
1901*4882a593Smuzhiyun }
1902*4882a593Smuzhiyun
1903*4882a593Smuzhiyun /*
1904*4882a593Smuzhiyun * order == -1 is expected when compacting via
1905*4882a593Smuzhiyun * /proc/sys/vm/compact_memory
1906*4882a593Smuzhiyun */
is_via_compact_memory(int order)1907*4882a593Smuzhiyun static inline bool is_via_compact_memory(int order)
1908*4882a593Smuzhiyun {
1909*4882a593Smuzhiyun return order == -1;
1910*4882a593Smuzhiyun }
1911*4882a593Smuzhiyun
kswapd_is_running(pg_data_t * pgdat)1912*4882a593Smuzhiyun static bool kswapd_is_running(pg_data_t *pgdat)
1913*4882a593Smuzhiyun {
1914*4882a593Smuzhiyun return pgdat->kswapd && (pgdat->kswapd->state == TASK_RUNNING);
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun
1917*4882a593Smuzhiyun /*
1918*4882a593Smuzhiyun * A zone's fragmentation score is the external fragmentation wrt to the
1919*4882a593Smuzhiyun * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100].
1920*4882a593Smuzhiyun */
fragmentation_score_zone(struct zone * zone)1921*4882a593Smuzhiyun static unsigned int fragmentation_score_zone(struct zone *zone)
1922*4882a593Smuzhiyun {
1923*4882a593Smuzhiyun return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER);
1924*4882a593Smuzhiyun }
1925*4882a593Smuzhiyun
1926*4882a593Smuzhiyun /*
1927*4882a593Smuzhiyun * A weighted zone's fragmentation score is the external fragmentation
1928*4882a593Smuzhiyun * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It
1929*4882a593Smuzhiyun * returns a value in the range [0, 100].
1930*4882a593Smuzhiyun *
1931*4882a593Smuzhiyun * The scaling factor ensures that proactive compaction focuses on larger
1932*4882a593Smuzhiyun * zones like ZONE_NORMAL, rather than smaller, specialized zones like
1933*4882a593Smuzhiyun * ZONE_DMA32. For smaller zones, the score value remains close to zero,
1934*4882a593Smuzhiyun * and thus never exceeds the high threshold for proactive compaction.
1935*4882a593Smuzhiyun */
fragmentation_score_zone_weighted(struct zone * zone)1936*4882a593Smuzhiyun static unsigned int fragmentation_score_zone_weighted(struct zone *zone)
1937*4882a593Smuzhiyun {
1938*4882a593Smuzhiyun unsigned long score;
1939*4882a593Smuzhiyun
1940*4882a593Smuzhiyun score = zone->present_pages * fragmentation_score_zone(zone);
1941*4882a593Smuzhiyun return div64_ul(score, zone->zone_pgdat->node_present_pages + 1);
1942*4882a593Smuzhiyun }
1943*4882a593Smuzhiyun
1944*4882a593Smuzhiyun /*
1945*4882a593Smuzhiyun * The per-node proactive (background) compaction process is started by its
1946*4882a593Smuzhiyun * corresponding kcompactd thread when the node's fragmentation score
1947*4882a593Smuzhiyun * exceeds the high threshold. The compaction process remains active till
1948*4882a593Smuzhiyun * the node's score falls below the low threshold, or one of the back-off
1949*4882a593Smuzhiyun * conditions is met.
1950*4882a593Smuzhiyun */
fragmentation_score_node(pg_data_t * pgdat)1951*4882a593Smuzhiyun static unsigned int fragmentation_score_node(pg_data_t *pgdat)
1952*4882a593Smuzhiyun {
1953*4882a593Smuzhiyun unsigned int score = 0;
1954*4882a593Smuzhiyun int zoneid;
1955*4882a593Smuzhiyun
1956*4882a593Smuzhiyun for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1957*4882a593Smuzhiyun struct zone *zone;
1958*4882a593Smuzhiyun
1959*4882a593Smuzhiyun zone = &pgdat->node_zones[zoneid];
1960*4882a593Smuzhiyun score += fragmentation_score_zone_weighted(zone);
1961*4882a593Smuzhiyun }
1962*4882a593Smuzhiyun
1963*4882a593Smuzhiyun return score;
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun
fragmentation_score_wmark(pg_data_t * pgdat,bool low)1966*4882a593Smuzhiyun static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low)
1967*4882a593Smuzhiyun {
1968*4882a593Smuzhiyun unsigned int wmark_low;
1969*4882a593Smuzhiyun
1970*4882a593Smuzhiyun /*
1971*4882a593Smuzhiyun * Cap the low watermak to avoid excessive compaction
1972*4882a593Smuzhiyun * activity in case a user sets the proactivess tunable
1973*4882a593Smuzhiyun * close to 100 (maximum).
1974*4882a593Smuzhiyun */
1975*4882a593Smuzhiyun wmark_low = max(100U - sysctl_compaction_proactiveness, 5U);
1976*4882a593Smuzhiyun return low ? wmark_low : min(wmark_low + 10, 100U);
1977*4882a593Smuzhiyun }
1978*4882a593Smuzhiyun
should_proactive_compact_node(pg_data_t * pgdat)1979*4882a593Smuzhiyun static bool should_proactive_compact_node(pg_data_t *pgdat)
1980*4882a593Smuzhiyun {
1981*4882a593Smuzhiyun int wmark_high;
1982*4882a593Smuzhiyun
1983*4882a593Smuzhiyun if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat))
1984*4882a593Smuzhiyun return false;
1985*4882a593Smuzhiyun
1986*4882a593Smuzhiyun wmark_high = fragmentation_score_wmark(pgdat, false);
1987*4882a593Smuzhiyun return fragmentation_score_node(pgdat) > wmark_high;
1988*4882a593Smuzhiyun }
1989*4882a593Smuzhiyun
__compact_finished(struct compact_control * cc)1990*4882a593Smuzhiyun static enum compact_result __compact_finished(struct compact_control *cc)
1991*4882a593Smuzhiyun {
1992*4882a593Smuzhiyun unsigned int order;
1993*4882a593Smuzhiyun const int migratetype = cc->migratetype;
1994*4882a593Smuzhiyun int ret;
1995*4882a593Smuzhiyun
1996*4882a593Smuzhiyun /* Compaction run completes if the migrate and free scanner meet */
1997*4882a593Smuzhiyun if (compact_scanners_met(cc)) {
1998*4882a593Smuzhiyun /* Let the next compaction start anew. */
1999*4882a593Smuzhiyun reset_cached_positions(cc->zone);
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun /*
2002*4882a593Smuzhiyun * Mark that the PG_migrate_skip information should be cleared
2003*4882a593Smuzhiyun * by kswapd when it goes to sleep. kcompactd does not set the
2004*4882a593Smuzhiyun * flag itself as the decision to be clear should be directly
2005*4882a593Smuzhiyun * based on an allocation request.
2006*4882a593Smuzhiyun */
2007*4882a593Smuzhiyun if (cc->direct_compaction)
2008*4882a593Smuzhiyun cc->zone->compact_blockskip_flush = true;
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun if (cc->whole_zone)
2011*4882a593Smuzhiyun return COMPACT_COMPLETE;
2012*4882a593Smuzhiyun else
2013*4882a593Smuzhiyun return COMPACT_PARTIAL_SKIPPED;
2014*4882a593Smuzhiyun }
2015*4882a593Smuzhiyun
2016*4882a593Smuzhiyun if (cc->proactive_compaction) {
2017*4882a593Smuzhiyun int score, wmark_low;
2018*4882a593Smuzhiyun pg_data_t *pgdat;
2019*4882a593Smuzhiyun
2020*4882a593Smuzhiyun pgdat = cc->zone->zone_pgdat;
2021*4882a593Smuzhiyun if (kswapd_is_running(pgdat))
2022*4882a593Smuzhiyun return COMPACT_PARTIAL_SKIPPED;
2023*4882a593Smuzhiyun
2024*4882a593Smuzhiyun score = fragmentation_score_zone(cc->zone);
2025*4882a593Smuzhiyun wmark_low = fragmentation_score_wmark(pgdat, true);
2026*4882a593Smuzhiyun
2027*4882a593Smuzhiyun if (score > wmark_low)
2028*4882a593Smuzhiyun ret = COMPACT_CONTINUE;
2029*4882a593Smuzhiyun else
2030*4882a593Smuzhiyun ret = COMPACT_SUCCESS;
2031*4882a593Smuzhiyun
2032*4882a593Smuzhiyun goto out;
2033*4882a593Smuzhiyun }
2034*4882a593Smuzhiyun
2035*4882a593Smuzhiyun if (is_via_compact_memory(cc->order))
2036*4882a593Smuzhiyun return COMPACT_CONTINUE;
2037*4882a593Smuzhiyun
2038*4882a593Smuzhiyun /*
2039*4882a593Smuzhiyun * Always finish scanning a pageblock to reduce the possibility of
2040*4882a593Smuzhiyun * fallbacks in the future. This is particularly important when
2041*4882a593Smuzhiyun * migration source is unmovable/reclaimable but it's not worth
2042*4882a593Smuzhiyun * special casing.
2043*4882a593Smuzhiyun */
2044*4882a593Smuzhiyun if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
2045*4882a593Smuzhiyun return COMPACT_CONTINUE;
2046*4882a593Smuzhiyun
2047*4882a593Smuzhiyun /* Direct compactor: Is a suitable page free? */
2048*4882a593Smuzhiyun ret = COMPACT_NO_SUITABLE_PAGE;
2049*4882a593Smuzhiyun for (order = cc->order; order < MAX_ORDER; order++) {
2050*4882a593Smuzhiyun struct free_area *area = &cc->zone->free_area[order];
2051*4882a593Smuzhiyun bool can_steal;
2052*4882a593Smuzhiyun
2053*4882a593Smuzhiyun /* Job done if page is free of the right migratetype */
2054*4882a593Smuzhiyun if (!free_area_empty(area, migratetype))
2055*4882a593Smuzhiyun return COMPACT_SUCCESS;
2056*4882a593Smuzhiyun
2057*4882a593Smuzhiyun #ifdef CONFIG_CMA
2058*4882a593Smuzhiyun /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
2059*4882a593Smuzhiyun if (migratetype == MIGRATE_MOVABLE &&
2060*4882a593Smuzhiyun !free_area_empty(area, MIGRATE_CMA))
2061*4882a593Smuzhiyun return COMPACT_SUCCESS;
2062*4882a593Smuzhiyun #endif
2063*4882a593Smuzhiyun /*
2064*4882a593Smuzhiyun * Job done if allocation would steal freepages from
2065*4882a593Smuzhiyun * other migratetype buddy lists.
2066*4882a593Smuzhiyun */
2067*4882a593Smuzhiyun if (find_suitable_fallback(area, order, migratetype,
2068*4882a593Smuzhiyun true, &can_steal) != -1) {
2069*4882a593Smuzhiyun
2070*4882a593Smuzhiyun /* movable pages are OK in any pageblock */
2071*4882a593Smuzhiyun if (migratetype == MIGRATE_MOVABLE)
2072*4882a593Smuzhiyun return COMPACT_SUCCESS;
2073*4882a593Smuzhiyun
2074*4882a593Smuzhiyun /*
2075*4882a593Smuzhiyun * We are stealing for a non-movable allocation. Make
2076*4882a593Smuzhiyun * sure we finish compacting the current pageblock
2077*4882a593Smuzhiyun * first so it is as free as possible and we won't
2078*4882a593Smuzhiyun * have to steal another one soon. This only applies
2079*4882a593Smuzhiyun * to sync compaction, as async compaction operates
2080*4882a593Smuzhiyun * on pageblocks of the same migratetype.
2081*4882a593Smuzhiyun */
2082*4882a593Smuzhiyun if (cc->mode == MIGRATE_ASYNC ||
2083*4882a593Smuzhiyun IS_ALIGNED(cc->migrate_pfn,
2084*4882a593Smuzhiyun pageblock_nr_pages)) {
2085*4882a593Smuzhiyun return COMPACT_SUCCESS;
2086*4882a593Smuzhiyun }
2087*4882a593Smuzhiyun
2088*4882a593Smuzhiyun ret = COMPACT_CONTINUE;
2089*4882a593Smuzhiyun break;
2090*4882a593Smuzhiyun }
2091*4882a593Smuzhiyun }
2092*4882a593Smuzhiyun
2093*4882a593Smuzhiyun out:
2094*4882a593Smuzhiyun if (cc->contended || fatal_signal_pending(current))
2095*4882a593Smuzhiyun ret = COMPACT_CONTENDED;
2096*4882a593Smuzhiyun
2097*4882a593Smuzhiyun return ret;
2098*4882a593Smuzhiyun }
2099*4882a593Smuzhiyun
compact_finished(struct compact_control * cc)2100*4882a593Smuzhiyun static enum compact_result compact_finished(struct compact_control *cc)
2101*4882a593Smuzhiyun {
2102*4882a593Smuzhiyun int ret;
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun ret = __compact_finished(cc);
2105*4882a593Smuzhiyun trace_mm_compaction_finished(cc->zone, cc->order, ret);
2106*4882a593Smuzhiyun if (ret == COMPACT_NO_SUITABLE_PAGE)
2107*4882a593Smuzhiyun ret = COMPACT_CONTINUE;
2108*4882a593Smuzhiyun
2109*4882a593Smuzhiyun return ret;
2110*4882a593Smuzhiyun }
2111*4882a593Smuzhiyun
2112*4882a593Smuzhiyun /*
2113*4882a593Smuzhiyun * compaction_suitable: Is this suitable to run compaction on this zone now?
2114*4882a593Smuzhiyun * Returns
2115*4882a593Smuzhiyun * COMPACT_SKIPPED - If there are too few free pages for compaction
2116*4882a593Smuzhiyun * COMPACT_SUCCESS - If the allocation would succeed without compaction
2117*4882a593Smuzhiyun * COMPACT_CONTINUE - If compaction should run now
2118*4882a593Smuzhiyun */
__compaction_suitable(struct zone * zone,int order,unsigned int alloc_flags,int highest_zoneidx,unsigned long wmark_target)2119*4882a593Smuzhiyun static enum compact_result __compaction_suitable(struct zone *zone, int order,
2120*4882a593Smuzhiyun unsigned int alloc_flags,
2121*4882a593Smuzhiyun int highest_zoneidx,
2122*4882a593Smuzhiyun unsigned long wmark_target)
2123*4882a593Smuzhiyun {
2124*4882a593Smuzhiyun unsigned long watermark;
2125*4882a593Smuzhiyun
2126*4882a593Smuzhiyun if (is_via_compact_memory(order))
2127*4882a593Smuzhiyun return COMPACT_CONTINUE;
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
2130*4882a593Smuzhiyun /*
2131*4882a593Smuzhiyun * If watermarks for high-order allocation are already met, there
2132*4882a593Smuzhiyun * should be no need for compaction at all.
2133*4882a593Smuzhiyun */
2134*4882a593Smuzhiyun if (zone_watermark_ok(zone, order, watermark, highest_zoneidx,
2135*4882a593Smuzhiyun alloc_flags))
2136*4882a593Smuzhiyun return COMPACT_SUCCESS;
2137*4882a593Smuzhiyun
2138*4882a593Smuzhiyun /*
2139*4882a593Smuzhiyun * Watermarks for order-0 must be met for compaction to be able to
2140*4882a593Smuzhiyun * isolate free pages for migration targets. This means that the
2141*4882a593Smuzhiyun * watermark and alloc_flags have to match, or be more pessimistic than
2142*4882a593Smuzhiyun * the check in __isolate_free_page(). We don't use the direct
2143*4882a593Smuzhiyun * compactor's alloc_flags, as they are not relevant for freepage
2144*4882a593Smuzhiyun * isolation. We however do use the direct compactor's highest_zoneidx
2145*4882a593Smuzhiyun * to skip over zones where lowmem reserves would prevent allocation
2146*4882a593Smuzhiyun * even if compaction succeeds.
2147*4882a593Smuzhiyun * For costly orders, we require low watermark instead of min for
2148*4882a593Smuzhiyun * compaction to proceed to increase its chances.
2149*4882a593Smuzhiyun * ALLOC_CMA is used, as pages in CMA pageblocks are considered
2150*4882a593Smuzhiyun * suitable migration targets
2151*4882a593Smuzhiyun */
2152*4882a593Smuzhiyun watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
2153*4882a593Smuzhiyun low_wmark_pages(zone) : min_wmark_pages(zone);
2154*4882a593Smuzhiyun watermark += compact_gap(order);
2155*4882a593Smuzhiyun if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx,
2156*4882a593Smuzhiyun ALLOC_CMA, wmark_target))
2157*4882a593Smuzhiyun return COMPACT_SKIPPED;
2158*4882a593Smuzhiyun
2159*4882a593Smuzhiyun return COMPACT_CONTINUE;
2160*4882a593Smuzhiyun }
2161*4882a593Smuzhiyun
compaction_suitable(struct zone * zone,int order,unsigned int alloc_flags,int highest_zoneidx)2162*4882a593Smuzhiyun enum compact_result compaction_suitable(struct zone *zone, int order,
2163*4882a593Smuzhiyun unsigned int alloc_flags,
2164*4882a593Smuzhiyun int highest_zoneidx)
2165*4882a593Smuzhiyun {
2166*4882a593Smuzhiyun enum compact_result ret;
2167*4882a593Smuzhiyun int fragindex;
2168*4882a593Smuzhiyun
2169*4882a593Smuzhiyun ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx,
2170*4882a593Smuzhiyun zone_page_state(zone, NR_FREE_PAGES));
2171*4882a593Smuzhiyun /*
2172*4882a593Smuzhiyun * fragmentation index determines if allocation failures are due to
2173*4882a593Smuzhiyun * low memory or external fragmentation
2174*4882a593Smuzhiyun *
2175*4882a593Smuzhiyun * index of -1000 would imply allocations might succeed depending on
2176*4882a593Smuzhiyun * watermarks, but we already failed the high-order watermark check
2177*4882a593Smuzhiyun * index towards 0 implies failure is due to lack of memory
2178*4882a593Smuzhiyun * index towards 1000 implies failure is due to fragmentation
2179*4882a593Smuzhiyun *
2180*4882a593Smuzhiyun * Only compact if a failure would be due to fragmentation. Also
2181*4882a593Smuzhiyun * ignore fragindex for non-costly orders where the alternative to
2182*4882a593Smuzhiyun * a successful reclaim/compaction is OOM. Fragindex and the
2183*4882a593Smuzhiyun * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
2184*4882a593Smuzhiyun * excessive compaction for costly orders, but it should not be at the
2185*4882a593Smuzhiyun * expense of system stability.
2186*4882a593Smuzhiyun */
2187*4882a593Smuzhiyun if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
2188*4882a593Smuzhiyun fragindex = fragmentation_index(zone, order);
2189*4882a593Smuzhiyun if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
2190*4882a593Smuzhiyun ret = COMPACT_NOT_SUITABLE_ZONE;
2191*4882a593Smuzhiyun }
2192*4882a593Smuzhiyun
2193*4882a593Smuzhiyun trace_mm_compaction_suitable(zone, order, ret);
2194*4882a593Smuzhiyun if (ret == COMPACT_NOT_SUITABLE_ZONE)
2195*4882a593Smuzhiyun ret = COMPACT_SKIPPED;
2196*4882a593Smuzhiyun
2197*4882a593Smuzhiyun return ret;
2198*4882a593Smuzhiyun }
2199*4882a593Smuzhiyun
compaction_zonelist_suitable(struct alloc_context * ac,int order,int alloc_flags)2200*4882a593Smuzhiyun bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
2201*4882a593Smuzhiyun int alloc_flags)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun struct zone *zone;
2204*4882a593Smuzhiyun struct zoneref *z;
2205*4882a593Smuzhiyun
2206*4882a593Smuzhiyun /*
2207*4882a593Smuzhiyun * Make sure at least one zone would pass __compaction_suitable if we continue
2208*4882a593Smuzhiyun * retrying the reclaim.
2209*4882a593Smuzhiyun */
2210*4882a593Smuzhiyun for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2211*4882a593Smuzhiyun ac->highest_zoneidx, ac->nodemask) {
2212*4882a593Smuzhiyun unsigned long available;
2213*4882a593Smuzhiyun enum compact_result compact_result;
2214*4882a593Smuzhiyun
2215*4882a593Smuzhiyun /*
2216*4882a593Smuzhiyun * Do not consider all the reclaimable memory because we do not
2217*4882a593Smuzhiyun * want to trash just for a single high order allocation which
2218*4882a593Smuzhiyun * is even not guaranteed to appear even if __compaction_suitable
2219*4882a593Smuzhiyun * is happy about the watermark check.
2220*4882a593Smuzhiyun */
2221*4882a593Smuzhiyun available = zone_reclaimable_pages(zone) / order;
2222*4882a593Smuzhiyun available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
2223*4882a593Smuzhiyun compact_result = __compaction_suitable(zone, order, alloc_flags,
2224*4882a593Smuzhiyun ac->highest_zoneidx, available);
2225*4882a593Smuzhiyun if (compact_result != COMPACT_SKIPPED)
2226*4882a593Smuzhiyun return true;
2227*4882a593Smuzhiyun }
2228*4882a593Smuzhiyun
2229*4882a593Smuzhiyun return false;
2230*4882a593Smuzhiyun }
2231*4882a593Smuzhiyun
2232*4882a593Smuzhiyun static enum compact_result
compact_zone(struct compact_control * cc,struct capture_control * capc)2233*4882a593Smuzhiyun compact_zone(struct compact_control *cc, struct capture_control *capc)
2234*4882a593Smuzhiyun {
2235*4882a593Smuzhiyun enum compact_result ret;
2236*4882a593Smuzhiyun unsigned long start_pfn = cc->zone->zone_start_pfn;
2237*4882a593Smuzhiyun unsigned long end_pfn = zone_end_pfn(cc->zone);
2238*4882a593Smuzhiyun unsigned long last_migrated_pfn;
2239*4882a593Smuzhiyun const bool sync = cc->mode != MIGRATE_ASYNC;
2240*4882a593Smuzhiyun bool update_cached;
2241*4882a593Smuzhiyun
2242*4882a593Smuzhiyun /*
2243*4882a593Smuzhiyun * These counters track activities during zone compaction. Initialize
2244*4882a593Smuzhiyun * them before compacting a new zone.
2245*4882a593Smuzhiyun */
2246*4882a593Smuzhiyun cc->total_migrate_scanned = 0;
2247*4882a593Smuzhiyun cc->total_free_scanned = 0;
2248*4882a593Smuzhiyun cc->nr_migratepages = 0;
2249*4882a593Smuzhiyun cc->nr_freepages = 0;
2250*4882a593Smuzhiyun INIT_LIST_HEAD(&cc->freepages);
2251*4882a593Smuzhiyun INIT_LIST_HEAD(&cc->migratepages);
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun cc->migratetype = gfp_migratetype(cc->gfp_mask);
2254*4882a593Smuzhiyun ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags,
2255*4882a593Smuzhiyun cc->highest_zoneidx);
2256*4882a593Smuzhiyun /* Compaction is likely to fail */
2257*4882a593Smuzhiyun if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
2258*4882a593Smuzhiyun return ret;
2259*4882a593Smuzhiyun
2260*4882a593Smuzhiyun /* huh, compaction_suitable is returning something unexpected */
2261*4882a593Smuzhiyun VM_BUG_ON(ret != COMPACT_CONTINUE);
2262*4882a593Smuzhiyun
2263*4882a593Smuzhiyun /*
2264*4882a593Smuzhiyun * Clear pageblock skip if there were failures recently and compaction
2265*4882a593Smuzhiyun * is about to be retried after being deferred.
2266*4882a593Smuzhiyun */
2267*4882a593Smuzhiyun if (compaction_restarting(cc->zone, cc->order))
2268*4882a593Smuzhiyun __reset_isolation_suitable(cc->zone);
2269*4882a593Smuzhiyun
2270*4882a593Smuzhiyun /*
2271*4882a593Smuzhiyun * Setup to move all movable pages to the end of the zone. Used cached
2272*4882a593Smuzhiyun * information on where the scanners should start (unless we explicitly
2273*4882a593Smuzhiyun * want to compact the whole zone), but check that it is initialised
2274*4882a593Smuzhiyun * by ensuring the values are within zone boundaries.
2275*4882a593Smuzhiyun */
2276*4882a593Smuzhiyun cc->fast_start_pfn = 0;
2277*4882a593Smuzhiyun if (cc->whole_zone) {
2278*4882a593Smuzhiyun cc->migrate_pfn = start_pfn;
2279*4882a593Smuzhiyun cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
2280*4882a593Smuzhiyun } else {
2281*4882a593Smuzhiyun cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
2282*4882a593Smuzhiyun cc->free_pfn = cc->zone->compact_cached_free_pfn;
2283*4882a593Smuzhiyun if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
2284*4882a593Smuzhiyun cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
2285*4882a593Smuzhiyun cc->zone->compact_cached_free_pfn = cc->free_pfn;
2286*4882a593Smuzhiyun }
2287*4882a593Smuzhiyun if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
2288*4882a593Smuzhiyun cc->migrate_pfn = start_pfn;
2289*4882a593Smuzhiyun cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
2290*4882a593Smuzhiyun cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
2291*4882a593Smuzhiyun }
2292*4882a593Smuzhiyun
2293*4882a593Smuzhiyun if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
2294*4882a593Smuzhiyun cc->whole_zone = true;
2295*4882a593Smuzhiyun }
2296*4882a593Smuzhiyun
2297*4882a593Smuzhiyun last_migrated_pfn = 0;
2298*4882a593Smuzhiyun
2299*4882a593Smuzhiyun /*
2300*4882a593Smuzhiyun * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
2301*4882a593Smuzhiyun * the basis that some migrations will fail in ASYNC mode. However,
2302*4882a593Smuzhiyun * if the cached PFNs match and pageblocks are skipped due to having
2303*4882a593Smuzhiyun * no isolation candidates, then the sync state does not matter.
2304*4882a593Smuzhiyun * Until a pageblock with isolation candidates is found, keep the
2305*4882a593Smuzhiyun * cached PFNs in sync to avoid revisiting the same blocks.
2306*4882a593Smuzhiyun */
2307*4882a593Smuzhiyun update_cached = !sync &&
2308*4882a593Smuzhiyun cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
2309*4882a593Smuzhiyun
2310*4882a593Smuzhiyun trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
2311*4882a593Smuzhiyun cc->free_pfn, end_pfn, sync);
2312*4882a593Smuzhiyun
2313*4882a593Smuzhiyun /* lru_add_drain_all could be expensive with involving other CPUs */
2314*4882a593Smuzhiyun lru_add_drain();
2315*4882a593Smuzhiyun
2316*4882a593Smuzhiyun while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
2317*4882a593Smuzhiyun int err;
2318*4882a593Smuzhiyun unsigned long start_pfn = cc->migrate_pfn;
2319*4882a593Smuzhiyun
2320*4882a593Smuzhiyun /*
2321*4882a593Smuzhiyun * Avoid multiple rescans which can happen if a page cannot be
2322*4882a593Smuzhiyun * isolated (dirty/writeback in async mode) or if the migrated
2323*4882a593Smuzhiyun * pages are being allocated before the pageblock is cleared.
2324*4882a593Smuzhiyun * The first rescan will capture the entire pageblock for
2325*4882a593Smuzhiyun * migration. If it fails, it'll be marked skip and scanning
2326*4882a593Smuzhiyun * will proceed as normal.
2327*4882a593Smuzhiyun */
2328*4882a593Smuzhiyun cc->rescan = false;
2329*4882a593Smuzhiyun if (pageblock_start_pfn(last_migrated_pfn) ==
2330*4882a593Smuzhiyun pageblock_start_pfn(start_pfn)) {
2331*4882a593Smuzhiyun cc->rescan = true;
2332*4882a593Smuzhiyun }
2333*4882a593Smuzhiyun
2334*4882a593Smuzhiyun switch (isolate_migratepages(cc)) {
2335*4882a593Smuzhiyun case ISOLATE_ABORT:
2336*4882a593Smuzhiyun ret = COMPACT_CONTENDED;
2337*4882a593Smuzhiyun putback_movable_pages(&cc->migratepages);
2338*4882a593Smuzhiyun cc->nr_migratepages = 0;
2339*4882a593Smuzhiyun goto out;
2340*4882a593Smuzhiyun case ISOLATE_NONE:
2341*4882a593Smuzhiyun if (update_cached) {
2342*4882a593Smuzhiyun cc->zone->compact_cached_migrate_pfn[1] =
2343*4882a593Smuzhiyun cc->zone->compact_cached_migrate_pfn[0];
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun
2346*4882a593Smuzhiyun /*
2347*4882a593Smuzhiyun * We haven't isolated and migrated anything, but
2348*4882a593Smuzhiyun * there might still be unflushed migrations from
2349*4882a593Smuzhiyun * previous cc->order aligned block.
2350*4882a593Smuzhiyun */
2351*4882a593Smuzhiyun goto check_drain;
2352*4882a593Smuzhiyun case ISOLATE_SUCCESS:
2353*4882a593Smuzhiyun update_cached = false;
2354*4882a593Smuzhiyun last_migrated_pfn = start_pfn;
2355*4882a593Smuzhiyun ;
2356*4882a593Smuzhiyun }
2357*4882a593Smuzhiyun
2358*4882a593Smuzhiyun err = migrate_pages(&cc->migratepages, compaction_alloc,
2359*4882a593Smuzhiyun compaction_free, (unsigned long)cc, cc->mode,
2360*4882a593Smuzhiyun MR_COMPACTION);
2361*4882a593Smuzhiyun
2362*4882a593Smuzhiyun trace_mm_compaction_migratepages(cc->nr_migratepages, err,
2363*4882a593Smuzhiyun &cc->migratepages);
2364*4882a593Smuzhiyun
2365*4882a593Smuzhiyun /* All pages were either migrated or will be released */
2366*4882a593Smuzhiyun cc->nr_migratepages = 0;
2367*4882a593Smuzhiyun if (err) {
2368*4882a593Smuzhiyun putback_movable_pages(&cc->migratepages);
2369*4882a593Smuzhiyun /*
2370*4882a593Smuzhiyun * migrate_pages() may return -ENOMEM when scanners meet
2371*4882a593Smuzhiyun * and we want compact_finished() to detect it
2372*4882a593Smuzhiyun */
2373*4882a593Smuzhiyun if (err == -ENOMEM && !compact_scanners_met(cc)) {
2374*4882a593Smuzhiyun ret = COMPACT_CONTENDED;
2375*4882a593Smuzhiyun goto out;
2376*4882a593Smuzhiyun }
2377*4882a593Smuzhiyun /*
2378*4882a593Smuzhiyun * We failed to migrate at least one page in the current
2379*4882a593Smuzhiyun * order-aligned block, so skip the rest of it.
2380*4882a593Smuzhiyun */
2381*4882a593Smuzhiyun if (cc->direct_compaction &&
2382*4882a593Smuzhiyun (cc->mode == MIGRATE_ASYNC)) {
2383*4882a593Smuzhiyun cc->migrate_pfn = block_end_pfn(
2384*4882a593Smuzhiyun cc->migrate_pfn - 1, cc->order);
2385*4882a593Smuzhiyun /* Draining pcplists is useless in this case */
2386*4882a593Smuzhiyun last_migrated_pfn = 0;
2387*4882a593Smuzhiyun }
2388*4882a593Smuzhiyun }
2389*4882a593Smuzhiyun
2390*4882a593Smuzhiyun check_drain:
2391*4882a593Smuzhiyun /*
2392*4882a593Smuzhiyun * Has the migration scanner moved away from the previous
2393*4882a593Smuzhiyun * cc->order aligned block where we migrated from? If yes,
2394*4882a593Smuzhiyun * flush the pages that were freed, so that they can merge and
2395*4882a593Smuzhiyun * compact_finished() can detect immediately if allocation
2396*4882a593Smuzhiyun * would succeed.
2397*4882a593Smuzhiyun */
2398*4882a593Smuzhiyun if (cc->order > 0 && last_migrated_pfn) {
2399*4882a593Smuzhiyun unsigned long current_block_start =
2400*4882a593Smuzhiyun block_start_pfn(cc->migrate_pfn, cc->order);
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun if (last_migrated_pfn < current_block_start) {
2403*4882a593Smuzhiyun lru_add_drain_cpu_zone(cc->zone);
2404*4882a593Smuzhiyun /* No more flushing until we migrate again */
2405*4882a593Smuzhiyun last_migrated_pfn = 0;
2406*4882a593Smuzhiyun }
2407*4882a593Smuzhiyun }
2408*4882a593Smuzhiyun
2409*4882a593Smuzhiyun /* Stop if a page has been captured */
2410*4882a593Smuzhiyun if (capc && capc->page) {
2411*4882a593Smuzhiyun ret = COMPACT_SUCCESS;
2412*4882a593Smuzhiyun break;
2413*4882a593Smuzhiyun }
2414*4882a593Smuzhiyun }
2415*4882a593Smuzhiyun
2416*4882a593Smuzhiyun out:
2417*4882a593Smuzhiyun /*
2418*4882a593Smuzhiyun * Release free pages and update where the free scanner should restart,
2419*4882a593Smuzhiyun * so we don't leave any returned pages behind in the next attempt.
2420*4882a593Smuzhiyun */
2421*4882a593Smuzhiyun if (cc->nr_freepages > 0) {
2422*4882a593Smuzhiyun unsigned long free_pfn = release_freepages(&cc->freepages);
2423*4882a593Smuzhiyun
2424*4882a593Smuzhiyun cc->nr_freepages = 0;
2425*4882a593Smuzhiyun VM_BUG_ON(free_pfn == 0);
2426*4882a593Smuzhiyun /* The cached pfn is always the first in a pageblock */
2427*4882a593Smuzhiyun free_pfn = pageblock_start_pfn(free_pfn);
2428*4882a593Smuzhiyun /*
2429*4882a593Smuzhiyun * Only go back, not forward. The cached pfn might have been
2430*4882a593Smuzhiyun * already reset to zone end in compact_finished()
2431*4882a593Smuzhiyun */
2432*4882a593Smuzhiyun if (free_pfn > cc->zone->compact_cached_free_pfn)
2433*4882a593Smuzhiyun cc->zone->compact_cached_free_pfn = free_pfn;
2434*4882a593Smuzhiyun }
2435*4882a593Smuzhiyun
2436*4882a593Smuzhiyun count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
2437*4882a593Smuzhiyun count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
2438*4882a593Smuzhiyun
2439*4882a593Smuzhiyun trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
2440*4882a593Smuzhiyun cc->free_pfn, end_pfn, sync, ret);
2441*4882a593Smuzhiyun
2442*4882a593Smuzhiyun return ret;
2443*4882a593Smuzhiyun }
2444*4882a593Smuzhiyun
compact_zone_order(struct zone * zone,int order,gfp_t gfp_mask,enum compact_priority prio,unsigned int alloc_flags,int highest_zoneidx,struct page ** capture)2445*4882a593Smuzhiyun static enum compact_result compact_zone_order(struct zone *zone, int order,
2446*4882a593Smuzhiyun gfp_t gfp_mask, enum compact_priority prio,
2447*4882a593Smuzhiyun unsigned int alloc_flags, int highest_zoneidx,
2448*4882a593Smuzhiyun struct page **capture)
2449*4882a593Smuzhiyun {
2450*4882a593Smuzhiyun enum compact_result ret;
2451*4882a593Smuzhiyun struct compact_control cc = {
2452*4882a593Smuzhiyun .order = order,
2453*4882a593Smuzhiyun .search_order = order,
2454*4882a593Smuzhiyun .gfp_mask = gfp_mask,
2455*4882a593Smuzhiyun .zone = zone,
2456*4882a593Smuzhiyun .mode = (prio == COMPACT_PRIO_ASYNC) ?
2457*4882a593Smuzhiyun MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
2458*4882a593Smuzhiyun .alloc_flags = alloc_flags,
2459*4882a593Smuzhiyun .highest_zoneidx = highest_zoneidx,
2460*4882a593Smuzhiyun .direct_compaction = true,
2461*4882a593Smuzhiyun .whole_zone = (prio == MIN_COMPACT_PRIORITY),
2462*4882a593Smuzhiyun .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
2463*4882a593Smuzhiyun .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
2464*4882a593Smuzhiyun };
2465*4882a593Smuzhiyun struct capture_control capc = {
2466*4882a593Smuzhiyun .cc = &cc,
2467*4882a593Smuzhiyun .page = NULL,
2468*4882a593Smuzhiyun };
2469*4882a593Smuzhiyun
2470*4882a593Smuzhiyun /*
2471*4882a593Smuzhiyun * Make sure the structs are really initialized before we expose the
2472*4882a593Smuzhiyun * capture control, in case we are interrupted and the interrupt handler
2473*4882a593Smuzhiyun * frees a page.
2474*4882a593Smuzhiyun */
2475*4882a593Smuzhiyun barrier();
2476*4882a593Smuzhiyun WRITE_ONCE(current->capture_control, &capc);
2477*4882a593Smuzhiyun
2478*4882a593Smuzhiyun ret = compact_zone(&cc, &capc);
2479*4882a593Smuzhiyun
2480*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.freepages));
2481*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.migratepages));
2482*4882a593Smuzhiyun
2483*4882a593Smuzhiyun /*
2484*4882a593Smuzhiyun * Make sure we hide capture control first before we read the captured
2485*4882a593Smuzhiyun * page pointer, otherwise an interrupt could free and capture a page
2486*4882a593Smuzhiyun * and we would leak it.
2487*4882a593Smuzhiyun */
2488*4882a593Smuzhiyun WRITE_ONCE(current->capture_control, NULL);
2489*4882a593Smuzhiyun *capture = READ_ONCE(capc.page);
2490*4882a593Smuzhiyun
2491*4882a593Smuzhiyun return ret;
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun
2494*4882a593Smuzhiyun int sysctl_extfrag_threshold = 500;
2495*4882a593Smuzhiyun
2496*4882a593Smuzhiyun /**
2497*4882a593Smuzhiyun * try_to_compact_pages - Direct compact to satisfy a high-order allocation
2498*4882a593Smuzhiyun * @gfp_mask: The GFP mask of the current allocation
2499*4882a593Smuzhiyun * @order: The order of the current allocation
2500*4882a593Smuzhiyun * @alloc_flags: The allocation flags of the current allocation
2501*4882a593Smuzhiyun * @ac: The context of current allocation
2502*4882a593Smuzhiyun * @prio: Determines how hard direct compaction should try to succeed
2503*4882a593Smuzhiyun * @capture: Pointer to free page created by compaction will be stored here
2504*4882a593Smuzhiyun *
2505*4882a593Smuzhiyun * This is the main entry point for direct page compaction.
2506*4882a593Smuzhiyun */
try_to_compact_pages(gfp_t gfp_mask,unsigned int order,unsigned int alloc_flags,const struct alloc_context * ac,enum compact_priority prio,struct page ** capture)2507*4882a593Smuzhiyun enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
2508*4882a593Smuzhiyun unsigned int alloc_flags, const struct alloc_context *ac,
2509*4882a593Smuzhiyun enum compact_priority prio, struct page **capture)
2510*4882a593Smuzhiyun {
2511*4882a593Smuzhiyun int may_perform_io = gfp_mask & __GFP_IO;
2512*4882a593Smuzhiyun struct zoneref *z;
2513*4882a593Smuzhiyun struct zone *zone;
2514*4882a593Smuzhiyun enum compact_result rc = COMPACT_SKIPPED;
2515*4882a593Smuzhiyun
2516*4882a593Smuzhiyun /*
2517*4882a593Smuzhiyun * Check if the GFP flags allow compaction - GFP_NOIO is really
2518*4882a593Smuzhiyun * tricky context because the migration might require IO
2519*4882a593Smuzhiyun */
2520*4882a593Smuzhiyun if (!may_perform_io)
2521*4882a593Smuzhiyun return COMPACT_SKIPPED;
2522*4882a593Smuzhiyun
2523*4882a593Smuzhiyun trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
2524*4882a593Smuzhiyun
2525*4882a593Smuzhiyun /* Compact each zone in the list */
2526*4882a593Smuzhiyun for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2527*4882a593Smuzhiyun ac->highest_zoneidx, ac->nodemask) {
2528*4882a593Smuzhiyun enum compact_result status;
2529*4882a593Smuzhiyun
2530*4882a593Smuzhiyun if (prio > MIN_COMPACT_PRIORITY
2531*4882a593Smuzhiyun && compaction_deferred(zone, order)) {
2532*4882a593Smuzhiyun rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
2533*4882a593Smuzhiyun continue;
2534*4882a593Smuzhiyun }
2535*4882a593Smuzhiyun
2536*4882a593Smuzhiyun status = compact_zone_order(zone, order, gfp_mask, prio,
2537*4882a593Smuzhiyun alloc_flags, ac->highest_zoneidx, capture);
2538*4882a593Smuzhiyun rc = max(status, rc);
2539*4882a593Smuzhiyun
2540*4882a593Smuzhiyun /* The allocation should succeed, stop compacting */
2541*4882a593Smuzhiyun if (status == COMPACT_SUCCESS) {
2542*4882a593Smuzhiyun /*
2543*4882a593Smuzhiyun * We think the allocation will succeed in this zone,
2544*4882a593Smuzhiyun * but it is not certain, hence the false. The caller
2545*4882a593Smuzhiyun * will repeat this with true if allocation indeed
2546*4882a593Smuzhiyun * succeeds in this zone.
2547*4882a593Smuzhiyun */
2548*4882a593Smuzhiyun compaction_defer_reset(zone, order, false);
2549*4882a593Smuzhiyun
2550*4882a593Smuzhiyun break;
2551*4882a593Smuzhiyun }
2552*4882a593Smuzhiyun
2553*4882a593Smuzhiyun if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
2554*4882a593Smuzhiyun status == COMPACT_PARTIAL_SKIPPED))
2555*4882a593Smuzhiyun /*
2556*4882a593Smuzhiyun * We think that allocation won't succeed in this zone
2557*4882a593Smuzhiyun * so we defer compaction there. If it ends up
2558*4882a593Smuzhiyun * succeeding after all, it will be reset.
2559*4882a593Smuzhiyun */
2560*4882a593Smuzhiyun defer_compaction(zone, order);
2561*4882a593Smuzhiyun
2562*4882a593Smuzhiyun /*
2563*4882a593Smuzhiyun * We might have stopped compacting due to need_resched() in
2564*4882a593Smuzhiyun * async compaction, or due to a fatal signal detected. In that
2565*4882a593Smuzhiyun * case do not try further zones
2566*4882a593Smuzhiyun */
2567*4882a593Smuzhiyun if ((prio == COMPACT_PRIO_ASYNC && need_resched())
2568*4882a593Smuzhiyun || fatal_signal_pending(current))
2569*4882a593Smuzhiyun break;
2570*4882a593Smuzhiyun }
2571*4882a593Smuzhiyun
2572*4882a593Smuzhiyun return rc;
2573*4882a593Smuzhiyun }
2574*4882a593Smuzhiyun
2575*4882a593Smuzhiyun /*
2576*4882a593Smuzhiyun * Compact all zones within a node till each zone's fragmentation score
2577*4882a593Smuzhiyun * reaches within proactive compaction thresholds (as determined by the
2578*4882a593Smuzhiyun * proactiveness tunable).
2579*4882a593Smuzhiyun *
2580*4882a593Smuzhiyun * It is possible that the function returns before reaching score targets
2581*4882a593Smuzhiyun * due to various back-off conditions, such as, contention on per-node or
2582*4882a593Smuzhiyun * per-zone locks.
2583*4882a593Smuzhiyun */
proactive_compact_node(pg_data_t * pgdat)2584*4882a593Smuzhiyun static void proactive_compact_node(pg_data_t *pgdat)
2585*4882a593Smuzhiyun {
2586*4882a593Smuzhiyun int zoneid;
2587*4882a593Smuzhiyun struct zone *zone;
2588*4882a593Smuzhiyun struct compact_control cc = {
2589*4882a593Smuzhiyun .order = -1,
2590*4882a593Smuzhiyun .mode = MIGRATE_SYNC_LIGHT,
2591*4882a593Smuzhiyun .ignore_skip_hint = true,
2592*4882a593Smuzhiyun .whole_zone = true,
2593*4882a593Smuzhiyun .gfp_mask = GFP_KERNEL,
2594*4882a593Smuzhiyun .proactive_compaction = true,
2595*4882a593Smuzhiyun };
2596*4882a593Smuzhiyun
2597*4882a593Smuzhiyun for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2598*4882a593Smuzhiyun zone = &pgdat->node_zones[zoneid];
2599*4882a593Smuzhiyun if (!populated_zone(zone))
2600*4882a593Smuzhiyun continue;
2601*4882a593Smuzhiyun
2602*4882a593Smuzhiyun cc.zone = zone;
2603*4882a593Smuzhiyun
2604*4882a593Smuzhiyun compact_zone(&cc, NULL);
2605*4882a593Smuzhiyun
2606*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.freepages));
2607*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.migratepages));
2608*4882a593Smuzhiyun }
2609*4882a593Smuzhiyun }
2610*4882a593Smuzhiyun
2611*4882a593Smuzhiyun /* Compact all zones within a node */
compact_node(int nid)2612*4882a593Smuzhiyun static void compact_node(int nid)
2613*4882a593Smuzhiyun {
2614*4882a593Smuzhiyun pg_data_t *pgdat = NODE_DATA(nid);
2615*4882a593Smuzhiyun int zoneid;
2616*4882a593Smuzhiyun struct zone *zone;
2617*4882a593Smuzhiyun struct compact_control cc = {
2618*4882a593Smuzhiyun .order = -1,
2619*4882a593Smuzhiyun .mode = MIGRATE_SYNC,
2620*4882a593Smuzhiyun .ignore_skip_hint = true,
2621*4882a593Smuzhiyun .whole_zone = true,
2622*4882a593Smuzhiyun .gfp_mask = GFP_KERNEL,
2623*4882a593Smuzhiyun };
2624*4882a593Smuzhiyun
2625*4882a593Smuzhiyun
2626*4882a593Smuzhiyun for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2627*4882a593Smuzhiyun
2628*4882a593Smuzhiyun zone = &pgdat->node_zones[zoneid];
2629*4882a593Smuzhiyun if (!populated_zone(zone))
2630*4882a593Smuzhiyun continue;
2631*4882a593Smuzhiyun
2632*4882a593Smuzhiyun cc.zone = zone;
2633*4882a593Smuzhiyun
2634*4882a593Smuzhiyun compact_zone(&cc, NULL);
2635*4882a593Smuzhiyun
2636*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.freepages));
2637*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.migratepages));
2638*4882a593Smuzhiyun }
2639*4882a593Smuzhiyun }
2640*4882a593Smuzhiyun
2641*4882a593Smuzhiyun /* Compact all nodes in the system */
compact_nodes(void)2642*4882a593Smuzhiyun static void compact_nodes(void)
2643*4882a593Smuzhiyun {
2644*4882a593Smuzhiyun int nid;
2645*4882a593Smuzhiyun
2646*4882a593Smuzhiyun /* Flush pending updates to the LRU lists */
2647*4882a593Smuzhiyun lru_add_drain_all();
2648*4882a593Smuzhiyun
2649*4882a593Smuzhiyun for_each_online_node(nid)
2650*4882a593Smuzhiyun compact_node(nid);
2651*4882a593Smuzhiyun }
2652*4882a593Smuzhiyun
2653*4882a593Smuzhiyun /* The written value is actually unused, all memory is compacted */
2654*4882a593Smuzhiyun int sysctl_compact_memory;
2655*4882a593Smuzhiyun
2656*4882a593Smuzhiyun /*
2657*4882a593Smuzhiyun * Tunable for proactive compaction. It determines how
2658*4882a593Smuzhiyun * aggressively the kernel should compact memory in the
2659*4882a593Smuzhiyun * background. It takes values in the range [0, 100].
2660*4882a593Smuzhiyun */
2661*4882a593Smuzhiyun unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
2662*4882a593Smuzhiyun
compaction_proactiveness_sysctl_handler(struct ctl_table * table,int write,void * buffer,size_t * length,loff_t * ppos)2663*4882a593Smuzhiyun int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
2664*4882a593Smuzhiyun void *buffer, size_t *length, loff_t *ppos)
2665*4882a593Smuzhiyun {
2666*4882a593Smuzhiyun int rc, nid;
2667*4882a593Smuzhiyun
2668*4882a593Smuzhiyun rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
2669*4882a593Smuzhiyun if (rc)
2670*4882a593Smuzhiyun return rc;
2671*4882a593Smuzhiyun
2672*4882a593Smuzhiyun if (write && sysctl_compaction_proactiveness) {
2673*4882a593Smuzhiyun for_each_online_node(nid) {
2674*4882a593Smuzhiyun pg_data_t *pgdat = NODE_DATA(nid);
2675*4882a593Smuzhiyun
2676*4882a593Smuzhiyun if (pgdat->proactive_compact_trigger)
2677*4882a593Smuzhiyun continue;
2678*4882a593Smuzhiyun
2679*4882a593Smuzhiyun pgdat->proactive_compact_trigger = true;
2680*4882a593Smuzhiyun wake_up_interruptible(&pgdat->kcompactd_wait);
2681*4882a593Smuzhiyun }
2682*4882a593Smuzhiyun }
2683*4882a593Smuzhiyun
2684*4882a593Smuzhiyun return 0;
2685*4882a593Smuzhiyun }
2686*4882a593Smuzhiyun
2687*4882a593Smuzhiyun /*
2688*4882a593Smuzhiyun * This is the entry point for compacting all nodes via
2689*4882a593Smuzhiyun * /proc/sys/vm/compact_memory
2690*4882a593Smuzhiyun */
sysctl_compaction_handler(struct ctl_table * table,int write,void * buffer,size_t * length,loff_t * ppos)2691*4882a593Smuzhiyun int sysctl_compaction_handler(struct ctl_table *table, int write,
2692*4882a593Smuzhiyun void *buffer, size_t *length, loff_t *ppos)
2693*4882a593Smuzhiyun {
2694*4882a593Smuzhiyun if (write)
2695*4882a593Smuzhiyun compact_nodes();
2696*4882a593Smuzhiyun
2697*4882a593Smuzhiyun return 0;
2698*4882a593Smuzhiyun }
2699*4882a593Smuzhiyun
2700*4882a593Smuzhiyun #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
sysfs_compact_node(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2701*4882a593Smuzhiyun static ssize_t sysfs_compact_node(struct device *dev,
2702*4882a593Smuzhiyun struct device_attribute *attr,
2703*4882a593Smuzhiyun const char *buf, size_t count)
2704*4882a593Smuzhiyun {
2705*4882a593Smuzhiyun int nid = dev->id;
2706*4882a593Smuzhiyun
2707*4882a593Smuzhiyun if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
2708*4882a593Smuzhiyun /* Flush pending updates to the LRU lists */
2709*4882a593Smuzhiyun lru_add_drain_all();
2710*4882a593Smuzhiyun
2711*4882a593Smuzhiyun compact_node(nid);
2712*4882a593Smuzhiyun }
2713*4882a593Smuzhiyun
2714*4882a593Smuzhiyun return count;
2715*4882a593Smuzhiyun }
2716*4882a593Smuzhiyun static DEVICE_ATTR(compact, 0200, NULL, sysfs_compact_node);
2717*4882a593Smuzhiyun
compaction_register_node(struct node * node)2718*4882a593Smuzhiyun int compaction_register_node(struct node *node)
2719*4882a593Smuzhiyun {
2720*4882a593Smuzhiyun return device_create_file(&node->dev, &dev_attr_compact);
2721*4882a593Smuzhiyun }
2722*4882a593Smuzhiyun
compaction_unregister_node(struct node * node)2723*4882a593Smuzhiyun void compaction_unregister_node(struct node *node)
2724*4882a593Smuzhiyun {
2725*4882a593Smuzhiyun return device_remove_file(&node->dev, &dev_attr_compact);
2726*4882a593Smuzhiyun }
2727*4882a593Smuzhiyun #endif /* CONFIG_SYSFS && CONFIG_NUMA */
2728*4882a593Smuzhiyun
kcompactd_work_requested(pg_data_t * pgdat)2729*4882a593Smuzhiyun static inline bool kcompactd_work_requested(pg_data_t *pgdat)
2730*4882a593Smuzhiyun {
2731*4882a593Smuzhiyun return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
2732*4882a593Smuzhiyun pgdat->proactive_compact_trigger;
2733*4882a593Smuzhiyun }
2734*4882a593Smuzhiyun
kcompactd_node_suitable(pg_data_t * pgdat)2735*4882a593Smuzhiyun static bool kcompactd_node_suitable(pg_data_t *pgdat)
2736*4882a593Smuzhiyun {
2737*4882a593Smuzhiyun int zoneid;
2738*4882a593Smuzhiyun struct zone *zone;
2739*4882a593Smuzhiyun enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
2740*4882a593Smuzhiyun
2741*4882a593Smuzhiyun for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
2742*4882a593Smuzhiyun zone = &pgdat->node_zones[zoneid];
2743*4882a593Smuzhiyun
2744*4882a593Smuzhiyun if (!populated_zone(zone))
2745*4882a593Smuzhiyun continue;
2746*4882a593Smuzhiyun
2747*4882a593Smuzhiyun if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
2748*4882a593Smuzhiyun highest_zoneidx) == COMPACT_CONTINUE)
2749*4882a593Smuzhiyun return true;
2750*4882a593Smuzhiyun }
2751*4882a593Smuzhiyun
2752*4882a593Smuzhiyun return false;
2753*4882a593Smuzhiyun }
2754*4882a593Smuzhiyun
kcompactd_do_work(pg_data_t * pgdat)2755*4882a593Smuzhiyun static void kcompactd_do_work(pg_data_t *pgdat)
2756*4882a593Smuzhiyun {
2757*4882a593Smuzhiyun /*
2758*4882a593Smuzhiyun * With no special task, compact all zones so that a page of requested
2759*4882a593Smuzhiyun * order is allocatable.
2760*4882a593Smuzhiyun */
2761*4882a593Smuzhiyun int zoneid;
2762*4882a593Smuzhiyun struct zone *zone;
2763*4882a593Smuzhiyun struct compact_control cc = {
2764*4882a593Smuzhiyun .order = pgdat->kcompactd_max_order,
2765*4882a593Smuzhiyun .search_order = pgdat->kcompactd_max_order,
2766*4882a593Smuzhiyun .highest_zoneidx = pgdat->kcompactd_highest_zoneidx,
2767*4882a593Smuzhiyun .mode = MIGRATE_SYNC_LIGHT,
2768*4882a593Smuzhiyun .ignore_skip_hint = false,
2769*4882a593Smuzhiyun .gfp_mask = GFP_KERNEL,
2770*4882a593Smuzhiyun };
2771*4882a593Smuzhiyun trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
2772*4882a593Smuzhiyun cc.highest_zoneidx);
2773*4882a593Smuzhiyun count_compact_event(KCOMPACTD_WAKE);
2774*4882a593Smuzhiyun
2775*4882a593Smuzhiyun for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) {
2776*4882a593Smuzhiyun int status;
2777*4882a593Smuzhiyun
2778*4882a593Smuzhiyun zone = &pgdat->node_zones[zoneid];
2779*4882a593Smuzhiyun if (!populated_zone(zone))
2780*4882a593Smuzhiyun continue;
2781*4882a593Smuzhiyun
2782*4882a593Smuzhiyun if (compaction_deferred(zone, cc.order))
2783*4882a593Smuzhiyun continue;
2784*4882a593Smuzhiyun
2785*4882a593Smuzhiyun if (compaction_suitable(zone, cc.order, 0, zoneid) !=
2786*4882a593Smuzhiyun COMPACT_CONTINUE)
2787*4882a593Smuzhiyun continue;
2788*4882a593Smuzhiyun
2789*4882a593Smuzhiyun if (kthread_should_stop())
2790*4882a593Smuzhiyun return;
2791*4882a593Smuzhiyun
2792*4882a593Smuzhiyun cc.zone = zone;
2793*4882a593Smuzhiyun status = compact_zone(&cc, NULL);
2794*4882a593Smuzhiyun
2795*4882a593Smuzhiyun if (status == COMPACT_SUCCESS) {
2796*4882a593Smuzhiyun compaction_defer_reset(zone, cc.order, false);
2797*4882a593Smuzhiyun } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
2798*4882a593Smuzhiyun /*
2799*4882a593Smuzhiyun * Buddy pages may become stranded on pcps that could
2800*4882a593Smuzhiyun * otherwise coalesce on the zone's free area for
2801*4882a593Smuzhiyun * order >= cc.order. This is ratelimited by the
2802*4882a593Smuzhiyun * upcoming deferral.
2803*4882a593Smuzhiyun */
2804*4882a593Smuzhiyun drain_all_pages(zone);
2805*4882a593Smuzhiyun
2806*4882a593Smuzhiyun /*
2807*4882a593Smuzhiyun * We use sync migration mode here, so we defer like
2808*4882a593Smuzhiyun * sync direct compaction does.
2809*4882a593Smuzhiyun */
2810*4882a593Smuzhiyun defer_compaction(zone, cc.order);
2811*4882a593Smuzhiyun }
2812*4882a593Smuzhiyun
2813*4882a593Smuzhiyun count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
2814*4882a593Smuzhiyun cc.total_migrate_scanned);
2815*4882a593Smuzhiyun count_compact_events(KCOMPACTD_FREE_SCANNED,
2816*4882a593Smuzhiyun cc.total_free_scanned);
2817*4882a593Smuzhiyun
2818*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.freepages));
2819*4882a593Smuzhiyun VM_BUG_ON(!list_empty(&cc.migratepages));
2820*4882a593Smuzhiyun }
2821*4882a593Smuzhiyun
2822*4882a593Smuzhiyun /*
2823*4882a593Smuzhiyun * Regardless of success, we are done until woken up next. But remember
2824*4882a593Smuzhiyun * the requested order/highest_zoneidx in case it was higher/tighter
2825*4882a593Smuzhiyun * than our current ones
2826*4882a593Smuzhiyun */
2827*4882a593Smuzhiyun if (pgdat->kcompactd_max_order <= cc.order)
2828*4882a593Smuzhiyun pgdat->kcompactd_max_order = 0;
2829*4882a593Smuzhiyun if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx)
2830*4882a593Smuzhiyun pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
2831*4882a593Smuzhiyun }
2832*4882a593Smuzhiyun
wakeup_kcompactd(pg_data_t * pgdat,int order,int highest_zoneidx)2833*4882a593Smuzhiyun void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx)
2834*4882a593Smuzhiyun {
2835*4882a593Smuzhiyun if (!order)
2836*4882a593Smuzhiyun return;
2837*4882a593Smuzhiyun
2838*4882a593Smuzhiyun if (pgdat->kcompactd_max_order < order)
2839*4882a593Smuzhiyun pgdat->kcompactd_max_order = order;
2840*4882a593Smuzhiyun
2841*4882a593Smuzhiyun if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx)
2842*4882a593Smuzhiyun pgdat->kcompactd_highest_zoneidx = highest_zoneidx;
2843*4882a593Smuzhiyun
2844*4882a593Smuzhiyun /*
2845*4882a593Smuzhiyun * Pairs with implicit barrier in wait_event_freezable()
2846*4882a593Smuzhiyun * such that wakeups are not missed.
2847*4882a593Smuzhiyun */
2848*4882a593Smuzhiyun if (!wq_has_sleeper(&pgdat->kcompactd_wait))
2849*4882a593Smuzhiyun return;
2850*4882a593Smuzhiyun
2851*4882a593Smuzhiyun if (!kcompactd_node_suitable(pgdat))
2852*4882a593Smuzhiyun return;
2853*4882a593Smuzhiyun
2854*4882a593Smuzhiyun trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2855*4882a593Smuzhiyun highest_zoneidx);
2856*4882a593Smuzhiyun wake_up_interruptible(&pgdat->kcompactd_wait);
2857*4882a593Smuzhiyun }
2858*4882a593Smuzhiyun
2859*4882a593Smuzhiyun /*
2860*4882a593Smuzhiyun * The background compaction daemon, started as a kernel thread
2861*4882a593Smuzhiyun * from the init process.
2862*4882a593Smuzhiyun */
kcompactd(void * p)2863*4882a593Smuzhiyun static int kcompactd(void *p)
2864*4882a593Smuzhiyun {
2865*4882a593Smuzhiyun pg_data_t *pgdat = (pg_data_t*)p;
2866*4882a593Smuzhiyun struct task_struct *tsk = current;
2867*4882a593Smuzhiyun unsigned int proactive_defer = 0;
2868*4882a593Smuzhiyun
2869*4882a593Smuzhiyun const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
2870*4882a593Smuzhiyun
2871*4882a593Smuzhiyun if (!cpumask_empty(cpumask))
2872*4882a593Smuzhiyun set_cpus_allowed_ptr(tsk, cpumask);
2873*4882a593Smuzhiyun
2874*4882a593Smuzhiyun set_freezable();
2875*4882a593Smuzhiyun
2876*4882a593Smuzhiyun pgdat->kcompactd_max_order = 0;
2877*4882a593Smuzhiyun pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
2878*4882a593Smuzhiyun
2879*4882a593Smuzhiyun while (!kthread_should_stop()) {
2880*4882a593Smuzhiyun unsigned long pflags;
2881*4882a593Smuzhiyun long timeout;
2882*4882a593Smuzhiyun
2883*4882a593Smuzhiyun timeout = sysctl_compaction_proactiveness ?
2884*4882a593Smuzhiyun msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC) :
2885*4882a593Smuzhiyun MAX_SCHEDULE_TIMEOUT;
2886*4882a593Smuzhiyun trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2887*4882a593Smuzhiyun if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
2888*4882a593Smuzhiyun kcompactd_work_requested(pgdat), timeout) &&
2889*4882a593Smuzhiyun !pgdat->proactive_compact_trigger) {
2890*4882a593Smuzhiyun
2891*4882a593Smuzhiyun psi_memstall_enter(&pflags);
2892*4882a593Smuzhiyun kcompactd_do_work(pgdat);
2893*4882a593Smuzhiyun psi_memstall_leave(&pflags);
2894*4882a593Smuzhiyun continue;
2895*4882a593Smuzhiyun }
2896*4882a593Smuzhiyun
2897*4882a593Smuzhiyun /* kcompactd wait timeout */
2898*4882a593Smuzhiyun if (should_proactive_compact_node(pgdat)) {
2899*4882a593Smuzhiyun unsigned int prev_score, score;
2900*4882a593Smuzhiyun
2901*4882a593Smuzhiyun /*
2902*4882a593Smuzhiyun * On wakeup of proactive compaction by sysctl
2903*4882a593Smuzhiyun * write, ignore the accumulated defer score.
2904*4882a593Smuzhiyun * Anyway, if the proactive compaction didn't
2905*4882a593Smuzhiyun * make any progress for the new value, it will
2906*4882a593Smuzhiyun * be further deferred by 2^COMPACT_MAX_DEFER_SHIFT
2907*4882a593Smuzhiyun * times.
2908*4882a593Smuzhiyun */
2909*4882a593Smuzhiyun if (proactive_defer &&
2910*4882a593Smuzhiyun !pgdat->proactive_compact_trigger) {
2911*4882a593Smuzhiyun proactive_defer--;
2912*4882a593Smuzhiyun continue;
2913*4882a593Smuzhiyun }
2914*4882a593Smuzhiyun
2915*4882a593Smuzhiyun prev_score = fragmentation_score_node(pgdat);
2916*4882a593Smuzhiyun proactive_compact_node(pgdat);
2917*4882a593Smuzhiyun score = fragmentation_score_node(pgdat);
2918*4882a593Smuzhiyun /*
2919*4882a593Smuzhiyun * Defer proactive compaction if the fragmentation
2920*4882a593Smuzhiyun * score did not go down i.e. no progress made.
2921*4882a593Smuzhiyun */
2922*4882a593Smuzhiyun proactive_defer = score < prev_score ?
2923*4882a593Smuzhiyun 0 : 1 << COMPACT_MAX_DEFER_SHIFT;
2924*4882a593Smuzhiyun }
2925*4882a593Smuzhiyun if (pgdat->proactive_compact_trigger)
2926*4882a593Smuzhiyun pgdat->proactive_compact_trigger = false;
2927*4882a593Smuzhiyun }
2928*4882a593Smuzhiyun
2929*4882a593Smuzhiyun return 0;
2930*4882a593Smuzhiyun }
2931*4882a593Smuzhiyun
2932*4882a593Smuzhiyun /*
2933*4882a593Smuzhiyun * This kcompactd start function will be called by init and node-hot-add.
2934*4882a593Smuzhiyun * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
2935*4882a593Smuzhiyun */
kcompactd_run(int nid)2936*4882a593Smuzhiyun int kcompactd_run(int nid)
2937*4882a593Smuzhiyun {
2938*4882a593Smuzhiyun pg_data_t *pgdat = NODE_DATA(nid);
2939*4882a593Smuzhiyun int ret = 0;
2940*4882a593Smuzhiyun
2941*4882a593Smuzhiyun if (pgdat->kcompactd)
2942*4882a593Smuzhiyun return 0;
2943*4882a593Smuzhiyun
2944*4882a593Smuzhiyun pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
2945*4882a593Smuzhiyun if (IS_ERR(pgdat->kcompactd)) {
2946*4882a593Smuzhiyun pr_err("Failed to start kcompactd on node %d\n", nid);
2947*4882a593Smuzhiyun ret = PTR_ERR(pgdat->kcompactd);
2948*4882a593Smuzhiyun pgdat->kcompactd = NULL;
2949*4882a593Smuzhiyun }
2950*4882a593Smuzhiyun return ret;
2951*4882a593Smuzhiyun }
2952*4882a593Smuzhiyun
2953*4882a593Smuzhiyun /*
2954*4882a593Smuzhiyun * Called by memory hotplug when all memory in a node is offlined. Caller must
2955*4882a593Smuzhiyun * hold mem_hotplug_begin/end().
2956*4882a593Smuzhiyun */
kcompactd_stop(int nid)2957*4882a593Smuzhiyun void kcompactd_stop(int nid)
2958*4882a593Smuzhiyun {
2959*4882a593Smuzhiyun struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
2960*4882a593Smuzhiyun
2961*4882a593Smuzhiyun if (kcompactd) {
2962*4882a593Smuzhiyun kthread_stop(kcompactd);
2963*4882a593Smuzhiyun NODE_DATA(nid)->kcompactd = NULL;
2964*4882a593Smuzhiyun }
2965*4882a593Smuzhiyun }
2966*4882a593Smuzhiyun
2967*4882a593Smuzhiyun /*
2968*4882a593Smuzhiyun * It's optimal to keep kcompactd on the same CPUs as their memory, but
2969*4882a593Smuzhiyun * not required for correctness. So if the last cpu in a node goes
2970*4882a593Smuzhiyun * away, we get changed to run anywhere: as the first one comes back,
2971*4882a593Smuzhiyun * restore their cpu bindings.
2972*4882a593Smuzhiyun */
kcompactd_cpu_online(unsigned int cpu)2973*4882a593Smuzhiyun static int kcompactd_cpu_online(unsigned int cpu)
2974*4882a593Smuzhiyun {
2975*4882a593Smuzhiyun int nid;
2976*4882a593Smuzhiyun
2977*4882a593Smuzhiyun for_each_node_state(nid, N_MEMORY) {
2978*4882a593Smuzhiyun pg_data_t *pgdat = NODE_DATA(nid);
2979*4882a593Smuzhiyun const struct cpumask *mask;
2980*4882a593Smuzhiyun
2981*4882a593Smuzhiyun mask = cpumask_of_node(pgdat->node_id);
2982*4882a593Smuzhiyun
2983*4882a593Smuzhiyun if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
2984*4882a593Smuzhiyun /* One of our CPUs online: restore mask */
2985*4882a593Smuzhiyun set_cpus_allowed_ptr(pgdat->kcompactd, mask);
2986*4882a593Smuzhiyun }
2987*4882a593Smuzhiyun return 0;
2988*4882a593Smuzhiyun }
2989*4882a593Smuzhiyun
kcompactd_init(void)2990*4882a593Smuzhiyun static int __init kcompactd_init(void)
2991*4882a593Smuzhiyun {
2992*4882a593Smuzhiyun int nid;
2993*4882a593Smuzhiyun int ret;
2994*4882a593Smuzhiyun
2995*4882a593Smuzhiyun ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2996*4882a593Smuzhiyun "mm/compaction:online",
2997*4882a593Smuzhiyun kcompactd_cpu_online, NULL);
2998*4882a593Smuzhiyun if (ret < 0) {
2999*4882a593Smuzhiyun pr_err("kcompactd: failed to register hotplug callbacks.\n");
3000*4882a593Smuzhiyun return ret;
3001*4882a593Smuzhiyun }
3002*4882a593Smuzhiyun
3003*4882a593Smuzhiyun for_each_node_state(nid, N_MEMORY)
3004*4882a593Smuzhiyun kcompactd_run(nid);
3005*4882a593Smuzhiyun return 0;
3006*4882a593Smuzhiyun }
3007*4882a593Smuzhiyun subsys_initcall(kcompactd_init)
3008*4882a593Smuzhiyun
3009*4882a593Smuzhiyun #endif /* CONFIG_COMPACTION */
3010