blob: 6713fb45659dea2368049cb300acd1c8c85cec81 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mel Gorman748446b2010-05-24 14:32:27 -07002/*
3 * linux/mm/compaction.c
4 *
5 * Memory compaction for the reduction of external fragmentation. Note that
6 * this heavily depends upon page migration to do all the real heavy
7 * lifting
8 *
9 * Copyright IBM Corp. 2007-2010 Mel Gorman <mel@csn.ul.ie>
10 */
Vlastimil Babka698b1b32016-03-17 14:18:08 -070011#include <linux/cpu.h>
Mel Gorman748446b2010-05-24 14:32:27 -070012#include <linux/swap.h>
13#include <linux/migrate.h>
14#include <linux/compaction.h>
15#include <linux/mm_inline.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010016#include <linux/sched/signal.h>
Mel Gorman748446b2010-05-24 14:32:27 -070017#include <linux/backing-dev.h>
Mel Gorman76ab0f52010-05-24 14:32:28 -070018#include <linux/sysctl.h>
Mel Gormaned4a6d72010-05-24 14:32:29 -070019#include <linux/sysfs.h>
Minchan Kim194159f2013-02-22 16:33:58 -080020#include <linux/page-isolation.h>
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080021#include <linux/kasan.h>
Vlastimil Babka698b1b32016-03-17 14:18:08 -070022#include <linux/kthread.h>
23#include <linux/freezer.h>
Joonsoo Kim83358ec2016-07-26 15:23:43 -070024#include <linux/page_owner.h>
Johannes Weinereb414682018-10-26 15:06:27 -070025#include <linux/psi.h>
Mel Gorman748446b2010-05-24 14:32:27 -070026#include "internal.h"
Tao Zenga1db7dc2022-01-20 15:09:43 +080027#ifdef CONFIG_AMLOGIC_PAGE_TRACE
28#include <linux/amlogic/page_trace.h>
29#endif
Tao Zeng54f845e2022-01-26 16:04:08 +080030#ifdef CONFIG_AMLOGIC_CMA
31#include <linux/amlogic/aml_cma.h>
32#endif
Mel Gorman748446b2010-05-24 14:32:27 -070033
Minchan Kim010fc292012-12-20 15:05:06 -080034#ifdef CONFIG_COMPACTION
35static inline void count_compact_event(enum vm_event_item item)
36{
37 count_vm_event(item);
38}
39
40static inline void count_compact_events(enum vm_event_item item, long delta)
41{
42 count_vm_events(item, delta);
43}
44#else
45#define count_compact_event(item) do { } while (0)
46#define count_compact_events(item, delta) do { } while (0)
47#endif
48
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010049#if defined CONFIG_COMPACTION || defined CONFIG_CMA
50
Mel Gormanb7aba692011-01-13 15:45:54 -080051#define CREATE_TRACE_POINTS
52#include <trace/events/compaction.h>
Robin Hsu1eeadb42022-05-13 10:19:50 +080053#undef CREATE_TRACE_POINTS
54#include <trace/hooks/mm.h>
Mel Gormanb7aba692011-01-13 15:45:54 -080055
Vlastimil Babka06b66402016-05-19 17:11:48 -070056#define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order))
57#define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order))
58#define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order)
59#define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order)
60
Nitin Guptafacdaa92020-08-11 18:31:00 -070061/*
62 * Fragmentation score check interval for proactive compaction purposes.
63 */
Nitin Guptad34c0a72020-08-11 18:31:07 -070064static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500;
Nitin Guptafacdaa92020-08-11 18:31:00 -070065
66/*
67 * Page order with-respect-to which proactive compaction
68 * calculates external fragmentation, which is used as
69 * the "fragmentation score" of a node/zone.
70 */
71#if defined CONFIG_TRANSPARENT_HUGEPAGE
72#define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER
Nitin Gupta25788732020-08-11 18:31:04 -070073#elif defined CONFIG_HUGETLBFS
Nitin Guptafacdaa92020-08-11 18:31:00 -070074#define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER
75#else
76#define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT)
77#endif
78
Mel Gorman748446b2010-05-24 14:32:27 -070079static unsigned long release_freepages(struct list_head *freelist)
80{
81 struct page *page, *next;
Vlastimil Babka6bace092014-12-10 15:43:31 -080082 unsigned long high_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -070083
84 list_for_each_entry_safe(page, next, freelist, lru) {
Vlastimil Babka6bace092014-12-10 15:43:31 -080085 unsigned long pfn = page_to_pfn(page);
Mel Gorman748446b2010-05-24 14:32:27 -070086 list_del(&page->lru);
87 __free_page(page);
Vlastimil Babka6bace092014-12-10 15:43:31 -080088 if (pfn > high_pfn)
89 high_pfn = pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070090 }
91
Vlastimil Babka6bace092014-12-10 15:43:31 -080092 return high_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070093}
94
Mel Gorman4469ab92019-03-05 15:44:39 -080095static void split_map_pages(struct list_head *list)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010096{
Joonsoo Kim66c64222016-07-26 15:23:40 -070097 unsigned int i, order, nr_pages;
98 struct page *page, *next;
99 LIST_HEAD(tmp_list);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100100
Joonsoo Kim66c64222016-07-26 15:23:40 -0700101 list_for_each_entry_safe(page, next, list, lru) {
102 list_del(&page->lru);
103
104 order = page_private(page);
105 nr_pages = 1 << order;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700106
Joonsoo Kim46f24fd2016-07-26 15:23:58 -0700107 post_alloc_hook(page, order, __GFP_MOVABLE);
Joonsoo Kim66c64222016-07-26 15:23:40 -0700108 if (order)
109 split_page(page, order);
110
111 for (i = 0; i < nr_pages; i++) {
112 list_add(&page->lru, &tmp_list);
113 page++;
114 }
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100115 }
Joonsoo Kim66c64222016-07-26 15:23:40 -0700116
117 list_splice(&tmp_list, list);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100118}
119
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700120#ifdef CONFIG_COMPACTION
Joonsoo Kim24e27162015-02-11 15:27:09 -0800121
Minchan Kimbda807d2016-07-26 15:23:05 -0700122int PageMovable(struct page *page)
123{
124 struct address_space *mapping;
125
126 VM_BUG_ON_PAGE(!PageLocked(page), page);
127 if (!__PageMovable(page))
128 return 0;
129
130 mapping = page_mapping(page);
131 if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
132 return 1;
133
134 return 0;
135}
136EXPORT_SYMBOL(PageMovable);
137
138void __SetPageMovable(struct page *page, struct address_space *mapping)
139{
140 VM_BUG_ON_PAGE(!PageLocked(page), page);
141 VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
142 page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
143}
144EXPORT_SYMBOL(__SetPageMovable);
145
146void __ClearPageMovable(struct page *page)
147{
Minchan Kimbda807d2016-07-26 15:23:05 -0700148 VM_BUG_ON_PAGE(!PageMovable(page), page);
149 /*
150 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
151 * flag so that VM can catch up released page by driver after isolation.
152 * With it, VM migration doesn't try to put it back.
153 */
154 page->mapping = (void *)((unsigned long)page->mapping &
155 PAGE_MAPPING_MOVABLE);
156}
157EXPORT_SYMBOL(__ClearPageMovable);
158
Joonsoo Kim24e27162015-02-11 15:27:09 -0800159/* Do not skip compaction more than 64 times */
160#define COMPACT_MAX_DEFER_SHIFT 6
161
162/*
163 * Compaction is deferred when compaction fails to result in a page
Alex Shi860b3272020-08-11 18:31:10 -0700164 * allocation success. 1 << compact_defer_shift, compactions are skipped up
Joonsoo Kim24e27162015-02-11 15:27:09 -0800165 * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
166 */
Hui Su2271b012020-12-14 19:12:46 -0800167static void defer_compaction(struct zone *zone, int order)
Joonsoo Kim24e27162015-02-11 15:27:09 -0800168{
169 zone->compact_considered = 0;
170 zone->compact_defer_shift++;
171
172 if (order < zone->compact_order_failed)
173 zone->compact_order_failed = order;
174
175 if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
176 zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
177
178 trace_mm_compaction_defer_compaction(zone, order);
179}
180
181/* Returns true if compaction should be skipped this time */
Hui Su2271b012020-12-14 19:12:46 -0800182static bool compaction_deferred(struct zone *zone, int order)
Joonsoo Kim24e27162015-02-11 15:27:09 -0800183{
184 unsigned long defer_limit = 1UL << zone->compact_defer_shift;
185
186 if (order < zone->compact_order_failed)
187 return false;
188
189 /* Avoid possible overflow */
Mateusz Nosek62b35fe2020-10-13 16:56:58 -0700190 if (++zone->compact_considered >= defer_limit) {
Joonsoo Kim24e27162015-02-11 15:27:09 -0800191 zone->compact_considered = defer_limit;
Joonsoo Kim24e27162015-02-11 15:27:09 -0800192 return false;
Mateusz Nosek62b35fe2020-10-13 16:56:58 -0700193 }
Joonsoo Kim24e27162015-02-11 15:27:09 -0800194
195 trace_mm_compaction_deferred(zone, order);
196
197 return true;
198}
199
200/*
201 * Update defer tracking counters after successful compaction of given order,
202 * which means an allocation either succeeded (alloc_success == true) or is
203 * expected to succeed.
204 */
205void compaction_defer_reset(struct zone *zone, int order,
206 bool alloc_success)
207{
208 if (alloc_success) {
209 zone->compact_considered = 0;
210 zone->compact_defer_shift = 0;
211 }
212 if (order >= zone->compact_order_failed)
213 zone->compact_order_failed = order + 1;
214
215 trace_mm_compaction_defer_reset(zone, order);
216}
217
218/* Returns true if restarting compaction after many failures */
Hui Su2271b012020-12-14 19:12:46 -0800219static bool compaction_restarting(struct zone *zone, int order)
Joonsoo Kim24e27162015-02-11 15:27:09 -0800220{
221 if (order < zone->compact_order_failed)
222 return false;
223
224 return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
225 zone->compact_considered >= 1UL << zone->compact_defer_shift;
226}
227
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700228/* Returns true if the pageblock should be scanned for pages to isolate. */
229static inline bool isolation_suitable(struct compact_control *cc,
230 struct page *page)
231{
232 if (cc->ignore_skip_hint)
233 return true;
234
235 return !get_pageblock_skip(page);
236}
237
Vlastimil Babka023336412015-09-08 15:02:42 -0700238static void reset_cached_positions(struct zone *zone)
239{
240 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
241 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Joonsoo Kim623446e2016-03-15 14:57:45 -0700242 zone->compact_cached_free_pfn =
Vlastimil Babka06b66402016-05-19 17:11:48 -0700243 pageblock_start_pfn(zone_end_pfn(zone) - 1);
Vlastimil Babka023336412015-09-08 15:02:42 -0700244}
245
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700246/*
Hui Su2271b012020-12-14 19:12:46 -0800247 * Compound pages of >= pageblock_order should consistently be skipped until
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800248 * released. It is always pointless to compact pages of such order (if they are
249 * migratable), and the pageblocks they occupy cannot contain any free pages.
David Rientjes21dc7e02017-11-17 15:26:30 -0800250 */
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800251static bool pageblock_skip_persistent(struct page *page)
David Rientjes21dc7e02017-11-17 15:26:30 -0800252{
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800253 if (!PageCompound(page))
David Rientjes21dc7e02017-11-17 15:26:30 -0800254 return false;
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800255
256 page = compound_head(page);
257
258 if (compound_order(page) >= pageblock_order)
259 return true;
260
261 return false;
David Rientjes21dc7e02017-11-17 15:26:30 -0800262}
263
Mel Gormane332f742019-03-05 15:45:38 -0800264static bool
265__reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
266 bool check_target)
267{
268 struct page *page = pfn_to_online_page(pfn);
Mel Gorman6b0868c2019-04-04 11:54:09 +0100269 struct page *block_page;
Mel Gormane332f742019-03-05 15:45:38 -0800270 struct page *end_page;
271 unsigned long block_pfn;
272
273 if (!page)
274 return false;
275 if (zone != page_zone(page))
276 return false;
277 if (pageblock_skip_persistent(page))
278 return false;
279
280 /*
281 * If skip is already cleared do no further checking once the
282 * restart points have been set.
283 */
284 if (check_source && check_target && !get_pageblock_skip(page))
285 return true;
286
287 /*
288 * If clearing skip for the target scanner, do not select a
289 * non-movable pageblock as the starting point.
290 */
291 if (!check_source && check_target &&
292 get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
293 return false;
294
Mel Gorman6b0868c2019-04-04 11:54:09 +0100295 /* Ensure the start of the pageblock or zone is online and valid */
296 block_pfn = pageblock_start_pfn(pfn);
Vlastimil Babkaa2e9a5a2019-10-14 14:12:07 -0700297 block_pfn = max(block_pfn, zone->zone_start_pfn);
298 block_page = pfn_to_online_page(block_pfn);
Mel Gorman6b0868c2019-04-04 11:54:09 +0100299 if (block_page) {
300 page = block_page;
301 pfn = block_pfn;
302 }
303
304 /* Ensure the end of the pageblock or zone is online and valid */
Vlastimil Babkaa2e9a5a2019-10-14 14:12:07 -0700305 block_pfn = pageblock_end_pfn(pfn) - 1;
Mel Gorman6b0868c2019-04-04 11:54:09 +0100306 block_pfn = min(block_pfn, zone_end_pfn(zone) - 1);
307 end_page = pfn_to_online_page(block_pfn);
308 if (!end_page)
309 return false;
310
Mel Gormane332f742019-03-05 15:45:38 -0800311 /*
312 * Only clear the hint if a sample indicates there is either a
313 * free page or an LRU page in the block. One or other condition
314 * is necessary for the block to be a migration source/target.
315 */
Mel Gormane332f742019-03-05 15:45:38 -0800316 do {
Mike Rapoport859a85d2021-09-07 19:54:52 -0700317 if (check_source && PageLRU(page)) {
318 clear_pageblock_skip(page);
319 return true;
320 }
Mel Gormane332f742019-03-05 15:45:38 -0800321
Mike Rapoport859a85d2021-09-07 19:54:52 -0700322 if (check_target && PageBuddy(page)) {
323 clear_pageblock_skip(page);
324 return true;
Mel Gormane332f742019-03-05 15:45:38 -0800325 }
326
327 page += (1 << PAGE_ALLOC_COSTLY_ORDER);
328 pfn += (1 << PAGE_ALLOC_COSTLY_ORDER);
Vlastimil Babkaa2e9a5a2019-10-14 14:12:07 -0700329 } while (page <= end_page);
Mel Gormane332f742019-03-05 15:45:38 -0800330
331 return false;
332}
333
David Rientjes21dc7e02017-11-17 15:26:30 -0800334/*
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700335 * This function is called to clear all cached information on pageblocks that
336 * should be skipped for page isolation when the migrate and free page scanner
337 * meet.
338 */
Mel Gorman62997022012-10-08 16:32:47 -0700339static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700340{
Mel Gormane332f742019-03-05 15:45:38 -0800341 unsigned long migrate_pfn = zone->zone_start_pfn;
Mel Gorman6b0868c2019-04-04 11:54:09 +0100342 unsigned long free_pfn = zone_end_pfn(zone) - 1;
Mel Gormane332f742019-03-05 15:45:38 -0800343 unsigned long reset_migrate = free_pfn;
344 unsigned long reset_free = migrate_pfn;
345 bool source_set = false;
346 bool free_set = false;
347
348 if (!zone->compact_blockskip_flush)
349 return;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700350
Mel Gorman62997022012-10-08 16:32:47 -0700351 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700352
Mel Gormane332f742019-03-05 15:45:38 -0800353 /*
354 * Walk the zone and update pageblock skip information. Source looks
355 * for PageLRU while target looks for PageBuddy. When the scanner
356 * is found, both PageBuddy and PageLRU are checked as the pageblock
357 * is suitable as both source and target.
358 */
359 for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
360 free_pfn -= pageblock_nr_pages) {
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700361 cond_resched();
362
Mel Gormane332f742019-03-05 15:45:38 -0800363 /* Update the migrate PFN */
364 if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
365 migrate_pfn < reset_migrate) {
366 source_set = true;
367 reset_migrate = migrate_pfn;
368 zone->compact_init_migrate_pfn = reset_migrate;
369 zone->compact_cached_migrate_pfn[0] = reset_migrate;
370 zone->compact_cached_migrate_pfn[1] = reset_migrate;
371 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700372
Mel Gormane332f742019-03-05 15:45:38 -0800373 /* Update the free PFN */
374 if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
375 free_pfn > reset_free) {
376 free_set = true;
377 reset_free = free_pfn;
378 zone->compact_init_free_pfn = reset_free;
379 zone->compact_cached_free_pfn = reset_free;
380 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700381 }
Vlastimil Babka023336412015-09-08 15:02:42 -0700382
Mel Gormane332f742019-03-05 15:45:38 -0800383 /* Leave no distance if no suitable block was reset */
384 if (reset_migrate >= reset_free) {
385 zone->compact_cached_migrate_pfn[0] = migrate_pfn;
386 zone->compact_cached_migrate_pfn[1] = migrate_pfn;
387 zone->compact_cached_free_pfn = free_pfn;
388 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700389}
390
Mel Gorman62997022012-10-08 16:32:47 -0700391void reset_isolation_suitable(pg_data_t *pgdat)
392{
393 int zoneid;
394
395 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
396 struct zone *zone = &pgdat->node_zones[zoneid];
397 if (!populated_zone(zone))
398 continue;
399
400 /* Only flush if a full compaction finished recently */
401 if (zone->compact_blockskip_flush)
402 __reset_isolation_suitable(zone);
403 }
404}
405
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700406/*
Mel Gormane380beb2019-03-05 15:44:58 -0800407 * Sets the pageblock skip bit if it was clear. Note that this is a hint as
408 * locks are not required for read/writers. Returns true if it was already set.
409 */
410static bool test_and_set_skip(struct compact_control *cc, struct page *page,
411 unsigned long pfn)
412{
413 bool skip;
414
415 /* Do no update if skip hint is being ignored */
416 if (cc->ignore_skip_hint)
417 return false;
418
419 if (!IS_ALIGNED(pfn, pageblock_nr_pages))
420 return false;
421
422 skip = get_pageblock_skip(page);
423 if (!skip && !cc->no_set_skip_hint)
424 set_pageblock_skip(page);
425
426 return skip;
427}
428
429static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
430{
431 struct zone *zone = cc->zone;
432
433 pfn = pageblock_end_pfn(pfn);
434
435 /* Set for isolation rather than compaction */
436 if (cc->no_set_skip_hint)
437 return;
438
439 if (pfn > zone->compact_cached_migrate_pfn[0])
440 zone->compact_cached_migrate_pfn[0] = pfn;
441 if (cc->mode != MIGRATE_ASYNC &&
442 pfn > zone->compact_cached_migrate_pfn[1])
443 zone->compact_cached_migrate_pfn[1] = pfn;
444}
445
446/*
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700447 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700448 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700449 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700450static void update_pageblock_skip(struct compact_control *cc,
Mel Gormand097a6f2019-03-05 15:45:28 -0800451 struct page *page, unsigned long pfn)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700452{
Mel Gormanc89511a2012-10-08 16:32:45 -0700453 struct zone *zone = cc->zone;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800454
Vlastimil Babka2583d672017-11-17 15:26:38 -0800455 if (cc->no_set_skip_hint)
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800456 return;
457
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700458 if (!page)
459 return;
460
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700461 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700462
David Rientjes35979ef2014-06-04 16:08:27 -0700463 /* Update where async and sync compaction should restart */
Mel Gormane380beb2019-03-05 15:44:58 -0800464 if (pfn < zone->compact_cached_free_pfn)
465 zone->compact_cached_free_pfn = pfn;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700466}
467#else
468static inline bool isolation_suitable(struct compact_control *cc,
469 struct page *page)
470{
471 return true;
472}
473
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800474static inline bool pageblock_skip_persistent(struct page *page)
David Rientjes21dc7e02017-11-17 15:26:30 -0800475{
476 return false;
477}
478
479static inline void update_pageblock_skip(struct compact_control *cc,
Mel Gormand097a6f2019-03-05 15:45:28 -0800480 struct page *page, unsigned long pfn)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700481{
482}
Mel Gormane380beb2019-03-05 15:44:58 -0800483
484static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
485{
486}
487
488static bool test_and_set_skip(struct compact_control *cc, struct page *page,
489 unsigned long pfn)
490{
491 return false;
492}
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700493#endif /* CONFIG_COMPACTION */
494
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700495/*
496 * Compaction requires the taking of some coarse locks that are potentially
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800497 * very heavily contended. For async compaction, trylock and record if the
498 * lock is contended. The lock will still be acquired but compaction will
499 * abort when the current block is finished regardless of success rate.
500 * Sync compaction acquires the lock.
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700501 *
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800502 * Always returns true which makes it easier to track lock state in callers.
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700503 */
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800504static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700505 struct compact_control *cc)
Jules Irenge77337ed2020-04-06 20:08:06 -0700506 __acquires(lock)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700507{
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800508 /* Track if the lock is contended in async mode */
509 if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
510 if (spin_trylock_irqsave(lock, *flags))
511 return true;
512
513 cc->contended = true;
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700514 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700515
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800516 spin_lock_irqsave(lock, *flags);
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700517 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700518}
519
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100520/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700521 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700522 * very heavily contended. The lock should be periodically unlocked to avoid
523 * having disabled IRQs for a long time, even when there is nobody waiting on
524 * the lock. It might also be that allowing the IRQs will result in
525 * need_resched() becoming true. If scheduling is needed, async compaction
526 * aborts. Sync compaction schedules.
527 * Either compaction type will also abort if a fatal signal is pending.
528 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700529 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700530 * Returns true if compaction should abort due to fatal signal pending, or
531 * async compaction due to need_resched()
532 * Returns false when compaction can continue (sync compaction might have
533 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700534 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700535static bool compact_unlock_should_abort(spinlock_t *lock,
536 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700537{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700538 if (*locked) {
539 spin_unlock_irqrestore(lock, flags);
540 *locked = false;
541 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700542
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700543 if (fatal_signal_pending(current)) {
Vlastimil Babkac3486f52016-07-28 15:49:30 -0700544 cc->contended = true;
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700545 return true;
546 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700547
Mel Gormancf66f072019-03-05 15:45:24 -0800548 cond_resched();
Vlastimil Babkabe976572014-06-04 16:10:41 -0700549
550 return false;
551}
552
Mel Gormanc67fe372012-08-21 16:16:17 -0700553/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800554 * Isolate free pages onto a private freelist. If @strict is true, will abort
555 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
556 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100557 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700558static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700559 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100560 unsigned long end_pfn,
561 struct list_head *freelist,
Mel Gorman4fca9732019-03-05 15:45:34 -0800562 unsigned int stride,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100563 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700564{
Mel Gormanb7aba692011-01-13 15:45:54 -0800565 int nr_scanned = 0, total_isolated = 0;
Mel Gormand097a6f2019-03-05 15:45:28 -0800566 struct page *cursor;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700567 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700568 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700569 unsigned long blockpfn = *start_pfn;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700570 unsigned int order;
Mel Gorman748446b2010-05-24 14:32:27 -0700571
Mel Gorman4fca9732019-03-05 15:45:34 -0800572 /* Strict mode is for isolation, speed is secondary */
573 if (strict)
574 stride = 1;
575
Mel Gorman748446b2010-05-24 14:32:27 -0700576 cursor = pfn_to_page(blockpfn);
577
Mel Gormanf40d1e42012-10-08 16:32:36 -0700578 /* Isolate free pages. */
Mel Gorman4fca9732019-03-05 15:45:34 -0800579 for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) {
Joonsoo Kim66c64222016-07-26 15:23:40 -0700580 int isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700581 struct page *page = cursor;
582
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700583 /*
584 * Periodically drop the lock (if held) regardless of its
585 * contention, to give chance to IRQs. Abort if fatal signal
586 * pending or async compaction detects need_resched()
587 */
588 if (!(blockpfn % SWAP_CLUSTER_MAX)
589 && compact_unlock_should_abort(&cc->zone->lock, flags,
590 &locked, cc))
591 break;
592
Mel Gormanb7aba692011-01-13 15:45:54 -0800593 nr_scanned++;
Laura Abbott2af120b2014-03-10 15:49:44 -0700594
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700595 /*
596 * For compound pages such as THP and hugetlbfs, we can save
597 * potentially a lot of iterations if we skip them at once.
598 * The check is racy, but we can consider only valid values
599 * and the only danger is skipping too much.
600 */
601 if (PageCompound(page)) {
David Rientjes21dc7e02017-11-17 15:26:30 -0800602 const unsigned int order = compound_order(page);
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700603
Vlastimil Babkad3c85ba2017-11-17 15:26:41 -0800604 if (likely(order < MAX_ORDER)) {
David Rientjes21dc7e02017-11-17 15:26:30 -0800605 blockpfn += (1UL << order) - 1;
606 cursor += (1UL << order) - 1;
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700607 }
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700608 goto isolate_fail;
609 }
610
Mel Gormanf40d1e42012-10-08 16:32:36 -0700611 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700612 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700613
614 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700615 * If we already hold the lock, we can skip some rechecking.
616 * Note that if we hold the lock now, checked_pageblock was
617 * already set in some previous iteration (or strict is true),
618 * so it is correct to skip the suitable migration target
619 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700620 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700621 if (!locked) {
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800622 locked = compact_lock_irqsave(&cc->zone->lock,
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700623 &flags, cc);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700624
Vlastimil Babka69b71892014-10-09 15:27:18 -0700625 /* Recheck this is a buddy page under lock */
626 if (!PageBuddy(page))
627 goto isolate_fail;
628 }
Mel Gorman748446b2010-05-24 14:32:27 -0700629
Joonsoo Kim66c64222016-07-26 15:23:40 -0700630 /* Found a free page, will break it into order-0 pages */
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700631 order = buddy_order(page);
Joonsoo Kim66c64222016-07-26 15:23:40 -0700632 isolated = __isolate_free_page(page, order);
David Rientjesa4f04f22016-06-24 14:50:10 -0700633 if (!isolated)
634 break;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700635 set_page_private(page, order);
David Rientjesa4f04f22016-06-24 14:50:10 -0700636
Mel Gorman748446b2010-05-24 14:32:27 -0700637 total_isolated += isolated;
David Rientjesa4f04f22016-06-24 14:50:10 -0700638 cc->nr_freepages += isolated;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700639 list_add_tail(&page->lru, freelist);
640
David Rientjesa4f04f22016-06-24 14:50:10 -0700641 if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
642 blockpfn += isolated;
643 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700644 }
David Rientjesa4f04f22016-06-24 14:50:10 -0700645 /* Advance to the end of split page */
646 blockpfn += isolated - 1;
647 cursor += isolated - 1;
648 continue;
Laura Abbott2af120b2014-03-10 15:49:44 -0700649
650isolate_fail:
651 if (strict)
652 break;
653 else
654 continue;
655
Mel Gorman748446b2010-05-24 14:32:27 -0700656 }
657
David Rientjesa4f04f22016-06-24 14:50:10 -0700658 if (locked)
659 spin_unlock_irqrestore(&cc->zone->lock, flags);
660
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700661 /*
662 * There is a tiny chance that we have read bogus compound_order(),
663 * so be careful to not go outside of the pageblock.
664 */
665 if (unlikely(blockpfn > end_pfn))
666 blockpfn = end_pfn;
667
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800668 trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
669 nr_scanned, total_isolated);
670
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700671 /* Record how far we have got within the block */
672 *start_pfn = blockpfn;
673
Mel Gormanf40d1e42012-10-08 16:32:36 -0700674 /*
675 * If strict isolation is requested by CMA then check that all the
676 * pages requested were isolated. If there were any failures, 0 is
677 * returned and CMA will fail.
678 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700679 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700680 total_isolated = 0;
681
David Rientjes7f354a52017-02-22 15:44:50 -0800682 cc->total_free_scanned += nr_scanned;
Mel Gorman397487d2012-10-19 12:00:10 +0100683 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800684 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700685 return total_isolated;
686}
687
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100688/**
689 * isolate_freepages_range() - isolate free pages.
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700690 * @cc: Compaction control structure.
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100691 * @start_pfn: The first PFN to start isolating.
692 * @end_pfn: The one-past-last PFN.
693 *
694 * Non-free pages, invalid PFNs, or zone boundaries within the
695 * [start_pfn, end_pfn) range are considered errors, cause function to
696 * undo its actions and return zero.
697 *
698 * Otherwise, function returns one-past-the-last PFN of isolated page
699 * (which may be greater then end_pfn if end fell in a middle of
700 * a free page).
701 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100702unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700703isolate_freepages_range(struct compact_control *cc,
704 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100705{
Joonsoo Kime1409c32016-03-15 14:57:48 -0700706 unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100707 LIST_HEAD(freelist);
708
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700709 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700710 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -0700711 if (block_start_pfn < cc->zone->zone_start_pfn)
712 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700713 block_end_pfn = pageblock_end_pfn(pfn);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100714
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700715 for (; pfn < end_pfn; pfn += isolated,
Joonsoo Kime1409c32016-03-15 14:57:48 -0700716 block_start_pfn = block_end_pfn,
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700717 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700718 /* Protect pfn from changing by isolate_freepages_block */
719 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700720
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100721 block_end_pfn = min(block_end_pfn, end_pfn);
722
Joonsoo Kim58420012014-11-13 15:19:07 -0800723 /*
724 * pfn could pass the block_end_pfn if isolated freepage
725 * is more than pageblock order. In this case, we adjust
726 * scanning range to right one.
727 */
728 if (pfn >= block_end_pfn) {
Vlastimil Babka06b66402016-05-19 17:11:48 -0700729 block_start_pfn = pageblock_start_pfn(pfn);
730 block_end_pfn = pageblock_end_pfn(pfn);
Joonsoo Kim58420012014-11-13 15:19:07 -0800731 block_end_pfn = min(block_end_pfn, end_pfn);
732 }
733
Joonsoo Kime1409c32016-03-15 14:57:48 -0700734 if (!pageblock_pfn_to_page(block_start_pfn,
735 block_end_pfn, cc->zone))
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700736 break;
737
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700738 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
Mel Gorman4fca9732019-03-05 15:45:34 -0800739 block_end_pfn, &freelist, 0, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100740
741 /*
742 * In strict mode, isolate_freepages_block() returns 0 if
743 * there are any holes in the block (ie. invalid PFNs or
744 * non-free pages).
745 */
746 if (!isolated)
747 break;
748
749 /*
750 * If we managed to isolate pages, it is always (1 << n) *
751 * pageblock_nr_pages for some non-negative n. (Max order
752 * page may span two pageblocks).
753 */
754 }
755
Joonsoo Kim66c64222016-07-26 15:23:40 -0700756 /* __isolate_free_page() does not map the pages */
Mel Gorman4469ab92019-03-05 15:44:39 -0800757 split_map_pages(&freelist);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100758
759 if (pfn < end_pfn) {
760 /* Loop terminated early, cleanup. */
761 release_freepages(&freelist);
762 return 0;
763 }
764
765 /* We don't use freelists for anything. */
766 return pfn;
767}
768
Carlos Llamas061e34c2021-04-23 19:20:54 +0000769#ifdef CONFIG_COMPACTION
Charan Teja Reddyf47b8522021-02-16 13:59:45 +0530770unsigned long isolate_and_split_free_page(struct page *page,
771 struct list_head *list)
772{
773 unsigned long isolated;
774 unsigned int order;
775
776 if (!PageBuddy(page))
777 return 0;
778
779 order = buddy_order(page);
780 isolated = __isolate_free_page(page, order);
781 if (!isolated)
782 return 0;
783
784 set_page_private(page, order);
785 list_add(&page->lru, list);
786
787 split_map_pages(list);
788
789 return isolated;
790}
791EXPORT_SYMBOL_GPL(isolate_and_split_free_page);
Carlos Llamas061e34c2021-04-23 19:20:54 +0000792#endif
Charan Teja Reddyf47b8522021-02-16 13:59:45 +0530793
Mel Gorman748446b2010-05-24 14:32:27 -0700794/* Similar to reclaim, but different enough that they don't share logic */
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800795static bool too_many_isolated(pg_data_t *pgdat)
Mel Gorman748446b2010-05-24 14:32:27 -0700796{
Minchan Kimbc693042010-09-09 16:38:00 -0700797 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700798
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800799 inactive = node_page_state(pgdat, NR_INACTIVE_FILE) +
800 node_page_state(pgdat, NR_INACTIVE_ANON);
801 active = node_page_state(pgdat, NR_ACTIVE_FILE) +
802 node_page_state(pgdat, NR_ACTIVE_ANON);
803 isolated = node_page_state(pgdat, NR_ISOLATED_FILE) +
804 node_page_state(pgdat, NR_ISOLATED_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700805
Tao Zeng54f845e2022-01-26 16:04:08 +0800806#ifdef CONFIG_AMLOGIC_CMA
807 check_cma_isolated(&isolated, active, inactive);
808#endif
Minchan Kimbc693042010-09-09 16:38:00 -0700809 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700810}
811
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100812/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700813 * isolate_migratepages_block() - isolate all migrate-able pages within
814 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100815 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700816 * @low_pfn: The first PFN to isolate
817 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
Hugh Dickinsc5eda602022-03-22 14:45:41 -0700818 * @mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100819 *
820 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700821 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700822 * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion,
Oscar Salvador369fa222021-05-04 18:35:26 -0700823 * -ENOMEM in case we could not allocate a page, or 0.
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700824 * cc->migrate_pfn will contain the next pfn to scan.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100825 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700826 * The pages are isolated on cc->migratepages list (not required to be empty),
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700827 * and cc->nr_migratepages is updated accordingly.
Mel Gorman748446b2010-05-24 14:32:27 -0700828 */
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700829static int
Minchan Kima934b9232023-01-12 09:52:49 -0800830isolate_migratepages_block(struct compact_control_ext *cc_ext, unsigned long low_pfn,
Hugh Dickinsc5eda602022-03-22 14:45:41 -0700831 unsigned long end_pfn, isolate_mode_t mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700832{
Minchan Kima934b9232023-01-12 09:52:49 -0800833 struct compact_control *cc = cc_ext->cc;
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800834 pg_data_t *pgdat = cc->zone->zone_pgdat;
Mel Gormanb7aba692011-01-13 15:45:54 -0800835 unsigned long nr_scanned = 0, nr_isolated = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700836 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700837 unsigned long flags = 0;
Alex Shi6168d0d2020-12-15 12:34:29 -0800838 struct lruvec *locked = NULL;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700839 struct page *page = NULL, *valid_page = NULL;
Hugh Dickinsc5eda602022-03-22 14:45:41 -0700840 struct address_space *mapping;
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800841 unsigned long start_pfn = low_pfn;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700842 bool skip_on_failure = false;
843 unsigned long next_skip_pfn = 0;
Mel Gormane380beb2019-03-05 15:44:58 -0800844 bool skip_updated = false;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700845 int ret = 0;
846
847 cc->migrate_pfn = low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700848
Mel Gorman748446b2010-05-24 14:32:27 -0700849 /*
850 * Ensure that there are not too many pages isolated from the LRU
851 * list by either parallel reclaimers or compaction. If there are,
852 * delay for some time until fewer pages are isolated
853 */
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800854 while (unlikely(too_many_isolated(pgdat))) {
Zi Yand20bdd572020-11-13 22:51:43 -0800855 /* stop isolation if there are still pages not migrated */
856 if (cc->nr_migratepages)
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700857 return -EAGAIN;
Zi Yand20bdd572020-11-13 22:51:43 -0800858
Mel Gormanf9e35b32011-06-15 15:08:52 -0700859 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700860 if (cc->mode == MIGRATE_ASYNC)
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700861 return -EAGAIN;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700862
Mel Gorman748446b2010-05-24 14:32:27 -0700863 congestion_wait(BLK_RW_ASYNC, HZ/10);
864
865 if (fatal_signal_pending(current))
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700866 return -EINTR;
Mel Gorman748446b2010-05-24 14:32:27 -0700867 }
868
Mel Gormancf66f072019-03-05 15:45:24 -0800869 cond_resched();
David Rientjesaeef4b82014-06-04 16:08:31 -0700870
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700871 if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
872 skip_on_failure = true;
873 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
874 }
875
Mel Gorman748446b2010-05-24 14:32:27 -0700876 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700877 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700878
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700879 if (skip_on_failure && low_pfn >= next_skip_pfn) {
880 /*
881 * We have isolated all migration candidates in the
882 * previous order-aligned block, and did not skip it due
883 * to failure. We should migrate the pages now and
884 * hopefully succeed compaction.
885 */
886 if (nr_isolated)
887 break;
888
889 /*
890 * We failed to isolate in the previous order-aligned
891 * block. Set the new boundary to the end of the
892 * current block. Note we can't simply increase
893 * next_skip_pfn by 1 << order, as low_pfn might have
894 * been incremented by a higher number due to skipping
895 * a compound or a high-order buddy page in the
896 * previous loop iteration.
897 */
898 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
899 }
900
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700901 /*
902 * Periodically drop the lock (if held) regardless of its
Mel Gorman670105a2019-08-02 21:48:51 -0700903 * contention, to give chance to IRQs. Abort completely if
904 * a fatal signal is pending.
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700905 */
Alex Shi6168d0d2020-12-15 12:34:29 -0800906 if (!(low_pfn % SWAP_CLUSTER_MAX)) {
907 if (locked) {
908 unlock_page_lruvec_irqrestore(locked, flags);
909 locked = NULL;
910 }
911
912 if (fatal_signal_pending(current)) {
913 cc->contended = true;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700914 ret = -EINTR;
Alex Shi6168d0d2020-12-15 12:34:29 -0800915
Tao Zeng54f845e2022-01-26 16:04:08 +0800916 #ifdef CONFIG_AMLOGIC_CMA
917 if (cc->alloc_contig)
918 cma_debug(1, page, "abort by sig, low_pfn:%lx, swap:%ld\n",
919 low_pfn, SWAP_CLUSTER_MAX);
920 #endif
Alex Shi6168d0d2020-12-15 12:34:29 -0800921 goto fatal_pending;
922 }
923
924 cond_resched();
Mel Gorman670105a2019-08-02 21:48:51 -0700925 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700926
Mel Gormanb7aba692011-01-13 15:45:54 -0800927 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700928
Mel Gorman748446b2010-05-24 14:32:27 -0700929 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800930
Mel Gormane380beb2019-03-05 15:44:58 -0800931 /*
932 * Check if the pageblock has already been marked skipped.
933 * Only the aligned PFN is checked as the caller isolates
934 * COMPACT_CLUSTER_MAX at a time so the second call must
935 * not falsely conclude that the block should be skipped.
936 */
937 if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) {
938 if (!cc->ignore_skip_hint && get_pageblock_skip(page)) {
939 low_pfn = end_pfn;
Alex Shi9df41312020-12-15 12:34:20 -0800940 page = NULL;
Tao Zeng54f845e2022-01-26 16:04:08 +0800941 #ifdef CONFIG_AMLOGIC_CMA
942 if (cc->alloc_contig)
943 cma_debug(1, page, "abort by skip, low_pfn:%lx\n",
944 low_pfn);
945 #endif
Mel Gormane380beb2019-03-05 15:44:58 -0800946 goto isolate_abort;
947 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700948 valid_page = page;
Mel Gormane380beb2019-03-05 15:44:58 -0800949 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700950
Oscar Salvador369fa222021-05-04 18:35:26 -0700951 if (PageHuge(page) && cc->alloc_contig) {
Oscar Salvadorae37c7f2021-05-04 18:35:29 -0700952 ret = isolate_or_dissolve_huge_page(page, &cc->migratepages);
Oscar Salvador369fa222021-05-04 18:35:26 -0700953
954 /*
955 * Fail isolation in case isolate_or_dissolve_huge_page()
956 * reports an error. In case of -ENOMEM, abort right away.
957 */
958 if (ret < 0) {
959 /* Do not report -EBUSY down the chain */
960 if (ret == -EBUSY)
961 ret = 0;
962 low_pfn += (1UL << compound_order(page)) - 1;
Tao Zeng54f845e2022-01-26 16:04:08 +0800963 #ifdef CONFIG_AMLOGIC_CMA
964 if (cc->alloc_contig)
965 cma_debug(1, page, "abort by huge, low_pfn:%lx\n",
966 low_pfn);
967 #endif
Oscar Salvador369fa222021-05-04 18:35:26 -0700968 goto isolate_fail;
969 }
970
Oscar Salvadorae37c7f2021-05-04 18:35:29 -0700971 if (PageHuge(page)) {
972 /*
973 * Hugepage was successfully isolated and placed
974 * on the cc->migratepages list.
975 */
976 low_pfn += compound_nr(page) - 1;
977 goto isolate_success_no_list;
978 }
979
Oscar Salvador369fa222021-05-04 18:35:26 -0700980 /*
981 * Ok, the hugepage was dissolved. Now these pages are
982 * Buddy and cannot be re-allocated because they are
983 * isolated. Fall-through as the check below handles
984 * Buddy pages.
985 */
986 }
987
Mel Gorman6c144662014-01-23 15:53:38 -0800988 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700989 * Skip if free. We read page order here without zone lock
990 * which is generally unsafe, but the race window is small and
991 * the worst thing that can happen is that we skip some
992 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800993 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700994 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700995 unsigned long freepage_order = buddy_order_unsafe(page);
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700996
997 /*
998 * Without lock, we cannot be sure that what we got is
999 * a valid page order. Consider only values in the
1000 * valid order range to prevent low_pfn overflow.
1001 */
1002 if (freepage_order > 0 && freepage_order < MAX_ORDER)
1003 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -07001004 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -07001005 }
Mel Gorman748446b2010-05-24 14:32:27 -07001006
Mel Gorman9927af742011-01-13 15:45:59 -08001007 /*
Vlastimil Babka29c0dde2015-09-08 15:02:46 -07001008 * Regardless of being on LRU, compound pages such as THP and
Rik van Riel1da2f322020-04-01 21:10:31 -07001009 * hugetlbfs are not to be compacted unless we are attempting
1010 * an allocation much larger than the huge page size (eg CMA).
1011 * We can potentially save a lot of iterations if we skip them
1012 * at once. The check is racy, but we can consider only valid
1013 * values and the only danger is skipping too much.
Andrea Arcangelibc835012011-01-13 15:47:08 -08001014 */
Rik van Riel1da2f322020-04-01 21:10:31 -07001015 if (PageCompound(page) && !cc->alloc_contig) {
David Rientjes21dc7e02017-11-17 15:26:30 -08001016 const unsigned int order = compound_order(page);
Vlastimil Babka29c0dde2015-09-08 15:02:46 -07001017
Vlastimil Babkad3c85ba2017-11-17 15:26:41 -08001018 if (likely(order < MAX_ORDER))
David Rientjes21dc7e02017-11-17 15:26:30 -08001019 low_pfn += (1UL << order) - 1;
Tao Zeng54f845e2022-01-26 16:04:08 +08001020 #ifdef CONFIG_AMLOGIC_CMA
1021 if (cc->alloc_contig)
1022 cma_debug(1, page, "abort by compound, low_pfn:%lx\n",
1023 low_pfn);
1024 #endif
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001025 goto isolate_fail;
Mel Gorman2a1402a2012-10-08 16:32:33 -07001026 }
1027
Minchan Kimbda807d2016-07-26 15:23:05 -07001028 /*
1029 * Check may be lockless but that's ok as we recheck later.
1030 * It's possible to migrate LRU and non-lru movable pages.
1031 * Skip any other type of page
1032 */
1033 if (!PageLRU(page)) {
Minchan Kimbda807d2016-07-26 15:23:05 -07001034 /*
1035 * __PageMovable can return false positive so we need
1036 * to verify it under page_lock.
1037 */
1038 if (unlikely(__PageMovable(page)) &&
1039 !PageIsolated(page)) {
1040 if (locked) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001041 unlock_page_lruvec_irqrestore(locked, flags);
1042 locked = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -07001043 }
1044
Hugh Dickinsc5eda602022-03-22 14:45:41 -07001045 if (!isolate_movable_page(page, mode))
Minchan Kimbda807d2016-07-26 15:23:05 -07001046 goto isolate_success;
1047 }
1048
Tao Zeng54f845e2022-01-26 16:04:08 +08001049 #ifdef CONFIG_AMLOGIC_CMA
1050 if (cc->alloc_contig && page_count(page))
1051 cma_debug(1, page, "abort by LRU, low_pfn:%lx\n",
1052 low_pfn);
1053 #endif
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001054 goto isolate_fail;
Minchan Kimbda807d2016-07-26 15:23:05 -07001055 }
Vlastimil Babka29c0dde2015-09-08 15:02:46 -07001056
David Rientjes119d6d52014-04-03 14:48:00 -07001057 /*
Alex Shi9df41312020-12-15 12:34:20 -08001058 * Be careful not to clear PageLRU until after we're
1059 * sure the page is not being freed elsewhere -- the
1060 * page release code relies on it.
1061 */
Tao Zeng54f845e2022-01-26 16:04:08 +08001062 #ifdef CONFIG_AMLOGIC_CMA
1063 if (unlikely(!get_page_unless_zero(page))) {
1064 if (cc->alloc_contig)
1065 cma_debug(1, page, "none zero ref, low_pfn:%lx\n",
1066 low_pfn);
1067 goto isolate_fail;
1068 }
1069 #else
Alex Shi9df41312020-12-15 12:34:20 -08001070 if (unlikely(!get_page_unless_zero(page)))
1071 goto isolate_fail;
Tao Zeng54f845e2022-01-26 16:04:08 +08001072 #endif
Alex Shi9df41312020-12-15 12:34:20 -08001073
Gavin Shanb951ab42022-11-24 17:55:23 +08001074 /*
1075 * Migration will fail if an anonymous page is pinned in memory,
1076 * so avoid taking lru_lock and isolating it unnecessarily in an
1077 * admittedly racy check.
1078 */
1079 mapping = page_mapping(page);
Tao Zeng54f845e2022-01-26 16:04:08 +08001080 #ifdef CONFIG_AMLOGIC_CMA
1081 if (!mapping && (page_count(page) - 1) > total_mapcount(page)) {
1082 if (cc->alloc_contig)
1083 cma_debug(1, page, "mc/rc miss match, low_pfn:%lx\n",
1084 low_pfn);
1085 goto isolate_fail_put;
1086 }
1087 check_page_to_cma(cc, mapping, page);
1088 #else
Gavin Shanb951ab42022-11-24 17:55:23 +08001089 if (!mapping && (page_count(page) - 1) > total_mapcount(page))
Alex Shi9df41312020-12-15 12:34:20 -08001090 goto isolate_fail_put;
Tao Zeng54f845e2022-01-26 16:04:08 +08001091 #endif
Alex Shi9df41312020-12-15 12:34:20 -08001092
Gavin Shanb951ab42022-11-24 17:55:23 +08001093 /*
1094 * Only allow to migrate anonymous pages in GFP_NOFS context
1095 * because those do not depend on fs locks.
1096 */
Tao Zeng54f845e2022-01-26 16:04:08 +08001097 #ifdef CONFIG_AMLOGIC_CMA
1098 if (!(cc->gfp_mask & __GFP_FS) && mapping) {
1099 if (cc->alloc_contig)
1100 cma_debug(1, page, "no fs ctx, low_pfn:%lx\n",
1101 low_pfn);
1102 goto isolate_fail_put;
1103 }
1104 #else
Gavin Shanb951ab42022-11-24 17:55:23 +08001105 if (!(cc->gfp_mask & __GFP_FS) && mapping)
1106 goto isolate_fail_put;
Tao Zeng54f845e2022-01-26 16:04:08 +08001107 #endif
Gavin Shanb951ab42022-11-24 17:55:23 +08001108
Hugh Dickinsc5eda602022-03-22 14:45:41 -07001109 /* Only take pages on LRU: a check now makes later tests safe */
1110 if (!PageLRU(page))
Alex Shi9df41312020-12-15 12:34:20 -08001111 goto isolate_fail_put;
1112
Hugh Dickinsc5eda602022-03-22 14:45:41 -07001113 /* Compaction might skip unevictable pages but CMA takes them */
1114 if (!(mode & ISOLATE_UNEVICTABLE) && PageUnevictable(page))
1115 goto isolate_fail_put;
1116
1117 /*
1118 * To minimise LRU disruption, the caller can indicate with
1119 * ISOLATE_ASYNC_MIGRATE that it only wants to isolate pages
1120 * it will be able to migrate without blocking - clean pages
1121 * for the most part. PageWriteback would require blocking.
1122 */
1123 if ((mode & ISOLATE_ASYNC_MIGRATE) && PageWriteback(page))
1124 goto isolate_fail_put;
1125
1126 if ((mode & ISOLATE_ASYNC_MIGRATE) && PageDirty(page)) {
1127 bool migrate_dirty;
1128
1129 /*
1130 * Only pages without mappings or that have a
1131 * ->migratepage callback are possible to migrate
1132 * without blocking. However, we can be racing with
1133 * truncation so it's necessary to lock the page
1134 * to stabilise the mapping as truncation holds
1135 * the page lock until after the page is removed
1136 * from the page cache.
1137 */
1138 if (!trylock_page(page))
1139 goto isolate_fail_put;
1140
1141 mapping = page_mapping(page);
1142 migrate_dirty = !mapping || mapping->a_ops->migratepage;
1143 unlock_page(page);
1144 if (!migrate_dirty)
1145 goto isolate_fail_put;
1146 }
1147
Alex Shi9df41312020-12-15 12:34:20 -08001148 /* Try isolate the page */
Tao Zeng54f845e2022-01-26 16:04:08 +08001149 #ifdef CONFIG_AMLOGIC_CMA
1150 if (!TestClearPageLRU(page)) {
1151 if (cc->alloc_contig)
1152 cma_debug(1, page, "clear lru fail, low_pfn:%lx, mode:%x\n",
1153 low_pfn, mode);
1154 goto isolate_fail_put;
1155 }
1156 #else
Alex Shi9df41312020-12-15 12:34:20 -08001157 if (!TestClearPageLRU(page))
1158 goto isolate_fail_put;
Tao Zeng54f845e2022-01-26 16:04:08 +08001159 #endif
Alex Shi9df41312020-12-15 12:34:20 -08001160
Muchun Songa9842262021-06-28 19:37:53 -07001161 lruvec = mem_cgroup_page_lruvec(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001162
Vlastimil Babka69b71892014-10-09 15:27:18 -07001163 /* If we already hold the lock, we can skip some rechecking */
Alex Shi6168d0d2020-12-15 12:34:29 -08001164 if (lruvec != locked) {
1165 if (locked)
1166 unlock_page_lruvec_irqrestore(locked, flags);
1167
1168 compact_lock_irqsave(&lruvec->lru_lock, &flags, cc);
1169 locked = lruvec;
Alex Shi6168d0d2020-12-15 12:34:29 -08001170
1171 lruvec_memcg_debug(lruvec, page);
Mel Gormane380beb2019-03-05 15:44:58 -08001172
Mel Gormane380beb2019-03-05 15:44:58 -08001173 /* Try get exclusive access under lock */
1174 if (!skip_updated) {
1175 skip_updated = true;
Tao Zeng54f845e2022-01-26 16:04:08 +08001176 #ifdef CONFIG_AMLOGIC_CMA
1177 if (test_and_set_skip(cc, page, low_pfn)) {
1178 if (cc->alloc_contig)
1179 cma_debug(1, page, "skip fail, low_pfn:%lx, mode:%x\n",
1180 low_pfn, mode);
1181 goto isolate_abort;
1182 }
1183 #else
Mel Gormane380beb2019-03-05 15:44:58 -08001184 if (test_and_set_skip(cc, page, low_pfn))
1185 goto isolate_abort;
Tao Zeng54f845e2022-01-26 16:04:08 +08001186 #endif
Mel Gormane380beb2019-03-05 15:44:58 -08001187 }
Mel Gorman2a1402a2012-10-08 16:32:33 -07001188
Vlastimil Babka29c0dde2015-09-08 15:02:46 -07001189 /*
1190 * Page become compound since the non-locked check,
1191 * and it's on LRU. It can only be a THP so the order
1192 * is safe to read and it's 0 for tail pages.
1193 */
Rik van Riel1da2f322020-04-01 21:10:31 -07001194 if (unlikely(PageCompound(page) && !cc->alloc_contig)) {
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001195 low_pfn += compound_nr(page) - 1;
Alex Shi9df41312020-12-15 12:34:20 -08001196 SetPageLRU(page);
1197 goto isolate_fail_put;
Vlastimil Babka69b71892014-10-09 15:27:18 -07001198 }
Alex Shid99fd5f2021-02-24 12:09:25 -08001199 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001200
Rik van Riel1da2f322020-04-01 21:10:31 -07001201 /* The whole page is taken off the LRU; skip the tail pages. */
1202 if (PageCompound(page))
1203 low_pfn += compound_nr(page) - 1;
Andrea Arcangelibc835012011-01-13 15:47:08 -08001204
Mel Gorman748446b2010-05-24 14:32:27 -07001205 /* Successfully isolated */
Yu Zhao46ae6b22021-02-24 12:08:25 -08001206 del_page_from_lru_list(page, lruvec);
Rik van Riel1da2f322020-04-01 21:10:31 -07001207 mod_node_page_state(page_pgdat(page),
Huang Ying9de4f222020-04-06 20:04:41 -07001208 NR_ISOLATED_ANON + page_is_file_lru(page),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001209 thp_nr_pages(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -07001210
1211isolate_success:
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001212 list_add(&page->lru, &cc->migratepages);
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07001213isolate_success_no_list:
Zi Yan38935862020-11-13 22:51:40 -08001214 cc->nr_migratepages += compound_nr(page);
Minchan Kima934b9232023-01-12 09:52:49 -08001215 if (!PageAnon(page))
1216 cc_ext->nr_migrate_file_pages += compound_nr(page);
Zi Yan38935862020-11-13 22:51:40 -08001217 nr_isolated += compound_nr(page);
Mel Gorman748446b2010-05-24 14:32:27 -07001218
Mel Gorman804d3122019-03-05 15:45:07 -08001219 /*
1220 * Avoid isolating too much unless this block is being
Mel Gormancb2dcaf2019-03-05 15:45:11 -08001221 * rescanned (e.g. dirty/writeback pages, parallel allocation)
1222 * or a lock is contended. For contention, isolate quickly to
1223 * potentially remove one source of contention.
Mel Gorman804d3122019-03-05 15:45:07 -08001224 */
Zi Yan38935862020-11-13 22:51:40 -08001225 if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
Mel Gormancb2dcaf2019-03-05 15:45:11 -08001226 !cc->rescan && !cc->contended) {
Hillf Danton31b83842012-01-10 15:07:59 -08001227 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07001228 break;
Hillf Danton31b83842012-01-10 15:07:59 -08001229 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001230
1231 continue;
Alex Shi9df41312020-12-15 12:34:20 -08001232
1233isolate_fail_put:
1234 /* Avoid potential deadlock in freeing page under lru_lock */
1235 if (locked) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001236 unlock_page_lruvec_irqrestore(locked, flags);
1237 locked = NULL;
Alex Shi9df41312020-12-15 12:34:20 -08001238 }
1239 put_page(page);
1240
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001241isolate_fail:
Oscar Salvador369fa222021-05-04 18:35:26 -07001242 if (!skip_on_failure && ret != -ENOMEM)
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001243 continue;
1244
1245 /*
1246 * We have isolated some pages, but then failed. Release them
1247 * instead of migrating, as we cannot form the cc->order buddy
1248 * page anyway.
1249 */
1250 if (nr_isolated) {
1251 if (locked) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001252 unlock_page_lruvec_irqrestore(locked, flags);
1253 locked = NULL;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001254 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001255 putback_movable_pages(&cc->migratepages);
1256 cc->nr_migratepages = 0;
Minchan Kima934b9232023-01-12 09:52:49 -08001257 cc_ext->nr_migrate_file_pages = 0;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001258 nr_isolated = 0;
1259 }
1260
1261 if (low_pfn < next_skip_pfn) {
1262 low_pfn = next_skip_pfn - 1;
1263 /*
1264 * The check near the loop beginning would have updated
1265 * next_skip_pfn too, but this is a bit simpler.
1266 */
1267 next_skip_pfn += 1UL << cc->order;
1268 }
Oscar Salvador369fa222021-05-04 18:35:26 -07001269
1270 if (ret == -ENOMEM)
1271 break;
Mel Gorman748446b2010-05-24 14:32:27 -07001272 }
1273
Vlastimil Babka99c0fd52014-10-09 15:27:23 -07001274 /*
1275 * The PageBuddy() check could have potentially brought us outside
1276 * the range to be scanned.
1277 */
1278 if (unlikely(low_pfn > end_pfn))
1279 low_pfn = end_pfn;
1280
Alex Shi9df41312020-12-15 12:34:20 -08001281 page = NULL;
1282
Mel Gormane380beb2019-03-05 15:44:58 -08001283isolate_abort:
Mel Gormanc67fe372012-08-21 16:16:17 -07001284 if (locked)
Alex Shi6168d0d2020-12-15 12:34:29 -08001285 unlock_page_lruvec_irqrestore(locked, flags);
Alex Shi9df41312020-12-15 12:34:20 -08001286 if (page) {
1287 SetPageLRU(page);
1288 put_page(page);
1289 }
Mel Gorman748446b2010-05-24 14:32:27 -07001290
Vlastimil Babka50b5b092014-01-21 15:51:10 -08001291 /*
Mel Gorman804d3122019-03-05 15:45:07 -08001292 * Updated the cached scanner pfn once the pageblock has been scanned
1293 * Pages will either be migrated in which case there is no point
1294 * scanning in the near future or migration failed in which case the
1295 * failure reason may persist. The block is marked for skipping if
1296 * there were no pages isolated in the block or if the block is
1297 * rescanned twice in a row.
Vlastimil Babka50b5b092014-01-21 15:51:10 -08001298 */
Mel Gorman804d3122019-03-05 15:45:07 -08001299 if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) {
Mel Gormane380beb2019-03-05 15:44:58 -08001300 if (valid_page && !skip_updated)
1301 set_pageblock_skip(valid_page);
1302 update_cached_migrate(cc, low_pfn);
1303 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001304
Joonsoo Kime34d85f2015-02-11 15:27:04 -08001305 trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
1306 nr_scanned, nr_isolated);
Mel Gormanb7aba692011-01-13 15:45:54 -08001307
Mel Gorman670105a2019-08-02 21:48:51 -07001308fatal_pending:
David Rientjes7f354a52017-02-22 15:44:50 -08001309 cc->total_migrate_scanned += nr_scanned;
Mel Gorman397487d2012-10-19 12:00:10 +01001310 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -08001311 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +01001312
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001313 cc->migrate_pfn = low_pfn;
1314
1315 return ret;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001316}
1317
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001318/**
1319 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
1320 * @cc: Compaction control structure.
1321 * @start_pfn: The first PFN to start isolating.
1322 * @end_pfn: The one-past-last PFN.
1323 *
Oscar Salvador369fa222021-05-04 18:35:26 -07001324 * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM
1325 * in case we could not allocate a page, or 0.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001326 */
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001327int
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001328isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
1329 unsigned long end_pfn)
1330{
Minchan Kima934b9232023-01-12 09:52:49 -08001331 struct compact_control_ext cc_ext = { .cc = cc };
Joonsoo Kime1409c32016-03-15 14:57:48 -07001332 unsigned long pfn, block_start_pfn, block_end_pfn;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001333 int ret = 0;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001334
1335 /* Scan block by block. First and last block may be incomplete */
1336 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001337 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -07001338 if (block_start_pfn < cc->zone->zone_start_pfn)
1339 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001340 block_end_pfn = pageblock_end_pfn(pfn);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001341
1342 for (; pfn < end_pfn; pfn = block_end_pfn,
Joonsoo Kime1409c32016-03-15 14:57:48 -07001343 block_start_pfn = block_end_pfn,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001344 block_end_pfn += pageblock_nr_pages) {
1345
1346 block_end_pfn = min(block_end_pfn, end_pfn);
1347
Joonsoo Kime1409c32016-03-15 14:57:48 -07001348 if (!pageblock_pfn_to_page(block_start_pfn,
1349 block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001350 continue;
1351
Suren Baghdasaryan3fd32dc172024-03-14 15:53:20 -07001352 ret = isolate_migratepages_block(&cc_ext, pfn, block_end_pfn,
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001353 ISOLATE_UNEVICTABLE);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001354
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001355 if (ret)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001356 break;
Joonsoo Kim6ea41c02014-10-29 14:50:20 -07001357
Zi Yan38935862020-11-13 22:51:40 -08001358 if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
Joonsoo Kim6ea41c02014-10-29 14:50:20 -07001359 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001360 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001361
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001362 return ret;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001363}
1364
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001365#endif /* CONFIG_COMPACTION || CONFIG_CMA */
1366#ifdef CONFIG_COMPACTION
Andrew Morton018e9a42015-04-15 16:15:20 -07001367
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001368static bool suitable_migration_source(struct compact_control *cc,
1369 struct page *page)
1370{
Vlastimil Babka282722b2017-05-08 15:54:49 -07001371 int block_mt;
1372
Mel Gorman9bebefd2019-03-05 15:45:14 -08001373 if (pageblock_skip_persistent(page))
1374 return false;
1375
Vlastimil Babka282722b2017-05-08 15:54:49 -07001376 if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001377 return true;
1378
Vlastimil Babka282722b2017-05-08 15:54:49 -07001379 block_mt = get_pageblock_migratetype(page);
1380
1381 if (cc->migratetype == MIGRATE_MOVABLE)
1382 return is_migrate_movable(block_mt);
1383 else
1384 return block_mt == cc->migratetype;
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001385}
1386
Andrew Morton018e9a42015-04-15 16:15:20 -07001387/* Returns true if the page is within a block suitable for migration to */
Minchan Kima934b9232023-01-12 09:52:49 -08001388static bool suitable_migration_target(struct compact_control_ext *cc_ext,
Vlastimil Babka9f7e3382016-10-07 17:00:37 -07001389 struct page *page)
Andrew Morton018e9a42015-04-15 16:15:20 -07001390{
Minchan Kima934b9232023-01-12 09:52:49 -08001391 struct compact_control *cc = cc_ext->cc;
Andrew Morton018e9a42015-04-15 16:15:20 -07001392 /* If the page is a large free page, then disallow migration */
1393 if (PageBuddy(page)) {
1394 /*
1395 * We are checking page_order without zone->lock taken. But
1396 * the only small danger is that we skip a potentially suitable
1397 * pageblock, so it's not worth to check order for valid range.
1398 */
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07001399 if (buddy_order_unsafe(page) >= pageblock_order)
Andrew Morton018e9a42015-04-15 16:15:20 -07001400 return false;
1401 }
1402
Yisheng Xie1ef36db2017-05-03 14:53:54 -07001403 if (cc->ignore_block_suitable)
1404 return true;
1405
Minchan Kima934b9232023-01-12 09:52:49 -08001406 /* Allow file pages to migrate only into MIGRATE_MOVABLE blocks */
1407 if (cc_ext->nr_migrate_file_pages)
1408 return get_pageblock_migratetype(page) == MIGRATE_MOVABLE;
1409
Andrew Morton018e9a42015-04-15 16:15:20 -07001410 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001411 if (is_migrate_movable(get_pageblock_migratetype(page)))
Andrew Morton018e9a42015-04-15 16:15:20 -07001412 return true;
1413
1414 /* Otherwise skip the block */
1415 return false;
1416}
1417
Mel Gorman70b44592019-03-05 15:44:54 -08001418static inline unsigned int
1419freelist_scan_limit(struct compact_control *cc)
1420{
Qian Caidd7ef7b2019-05-13 17:17:38 -07001421 unsigned short shift = BITS_PER_LONG - 1;
1422
1423 return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1;
Mel Gorman70b44592019-03-05 15:44:54 -08001424}
1425
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001426/*
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001427 * Test whether the free scanner has reached the same or lower pageblock than
1428 * the migration scanner, and compaction should thus terminate.
1429 */
1430static inline bool compact_scanners_met(struct compact_control *cc)
1431{
1432 return (cc->free_pfn >> pageblock_order)
1433 <= (cc->migrate_pfn >> pageblock_order);
1434}
1435
Mel Gorman5a811882019-03-05 15:45:01 -08001436/*
1437 * Used when scanning for a suitable migration target which scans freelists
1438 * in reverse. Reorders the list such as the unscanned pages are scanned
1439 * first on the next iteration of the free scanner
1440 */
1441static void
1442move_freelist_head(struct list_head *freelist, struct page *freepage)
1443{
1444 LIST_HEAD(sublist);
1445
1446 if (!list_is_last(freelist, &freepage->lru)) {
1447 list_cut_before(&sublist, freelist, &freepage->lru);
Liu Xiangd2155fe2021-06-30 18:50:51 -07001448 list_splice_tail(&sublist, freelist);
Mel Gorman5a811882019-03-05 15:45:01 -08001449 }
1450}
1451
1452/*
1453 * Similar to move_freelist_head except used by the migration scanner
1454 * when scanning forward. It's possible for these list operations to
1455 * move against each other if they search the free list exactly in
1456 * lockstep.
1457 */
Mel Gorman70b44592019-03-05 15:44:54 -08001458static void
1459move_freelist_tail(struct list_head *freelist, struct page *freepage)
1460{
1461 LIST_HEAD(sublist);
1462
1463 if (!list_is_first(freelist, &freepage->lru)) {
1464 list_cut_position(&sublist, freelist, &freepage->lru);
Liu Xiangd2155fe2021-06-30 18:50:51 -07001465 list_splice_tail(&sublist, freelist);
Mel Gorman70b44592019-03-05 15:44:54 -08001466 }
1467}
1468
Mel Gorman5a811882019-03-05 15:45:01 -08001469static void
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001470fast_isolate_around(struct compact_control *cc, unsigned long pfn)
Mel Gorman5a811882019-03-05 15:45:01 -08001471{
1472 unsigned long start_pfn, end_pfn;
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001473 struct page *page;
Mel Gorman5a811882019-03-05 15:45:01 -08001474
1475 /* Do not search around if there are enough pages already */
1476 if (cc->nr_freepages >= cc->nr_migratepages)
1477 return;
1478
1479 /* Minimise scanning during async compaction */
1480 if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
1481 return;
1482
1483 /* Pageblock boundaries */
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001484 start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn);
1485 end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone));
1486
1487 page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone);
1488 if (!page)
1489 return;
Mel Gorman5a811882019-03-05 15:45:01 -08001490
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001491 isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
Mel Gorman5a811882019-03-05 15:45:01 -08001492
1493 /* Skip this pageblock in the future as it's full or nearly full */
1494 if (cc->nr_freepages < cc->nr_migratepages)
1495 set_pageblock_skip(page);
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001496
1497 return;
Mel Gorman5a811882019-03-05 15:45:01 -08001498}
1499
Mel Gormandbe2d4e2019-03-05 15:45:31 -08001500/* Search orders in round-robin fashion */
1501static int next_search_order(struct compact_control *cc, int order)
1502{
1503 order--;
1504 if (order < 0)
1505 order = cc->order - 1;
1506
1507 /* Search wrapped around? */
1508 if (order == cc->search_order) {
1509 cc->search_order--;
1510 if (cc->search_order < 0)
1511 cc->search_order = cc->order - 1;
1512 return -1;
1513 }
1514
1515 return order;
1516}
1517
Mel Gorman5a811882019-03-05 15:45:01 -08001518static unsigned long
1519fast_isolate_freepages(struct compact_control *cc)
1520{
Wonhyuk Yangb55ca522021-06-30 18:50:53 -07001521 unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1);
Mel Gorman5a811882019-03-05 15:45:01 -08001522 unsigned int nr_scanned = 0;
Rokudo Yan74e21482021-02-04 18:32:20 -08001523 unsigned long low_pfn, min_pfn, highest = 0;
Mel Gorman5a811882019-03-05 15:45:01 -08001524 unsigned long nr_isolated = 0;
1525 unsigned long distance;
1526 struct page *page = NULL;
1527 bool scan_start = false;
1528 int order;
1529
1530 /* Full compaction passes in a negative order */
1531 if (cc->order <= 0)
1532 return cc->free_pfn;
1533
1534 /*
1535 * If starting the scan, use a deeper search and use the highest
1536 * PFN found if a suitable one is not found.
1537 */
Mel Gormane332f742019-03-05 15:45:38 -08001538 if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
Mel Gorman5a811882019-03-05 15:45:01 -08001539 limit = pageblock_nr_pages >> 1;
1540 scan_start = true;
1541 }
1542
1543 /*
1544 * Preferred point is in the top quarter of the scan space but take
1545 * a pfn from the top half if the search is problematic.
1546 */
1547 distance = (cc->free_pfn - cc->migrate_pfn);
1548 low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
1549 min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
1550
1551 if (WARN_ON_ONCE(min_pfn > low_pfn))
1552 low_pfn = min_pfn;
1553
Mel Gormandbe2d4e2019-03-05 15:45:31 -08001554 /*
1555 * Search starts from the last successful isolation order or the next
1556 * order to search after a previous failure
1557 */
1558 cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1559
1560 for (order = cc->search_order;
1561 !page && order >= 0;
1562 order = next_search_order(cc, order)) {
Mel Gorman5a811882019-03-05 15:45:01 -08001563 struct free_area *area = &cc->zone->free_area[order];
1564 struct list_head *freelist;
1565 struct page *freepage;
1566 unsigned long flags;
1567 unsigned int order_scanned = 0;
Rokudo Yan74e21482021-02-04 18:32:20 -08001568 unsigned long high_pfn = 0;
Mel Gorman5a811882019-03-05 15:45:01 -08001569
1570 if (!area->nr_free)
1571 continue;
1572
1573 spin_lock_irqsave(&cc->zone->lock, flags);
1574 freelist = &area->free_list[MIGRATE_MOVABLE];
1575 list_for_each_entry_reverse(freepage, freelist, lru) {
1576 unsigned long pfn;
1577
1578 order_scanned++;
1579 nr_scanned++;
1580 pfn = page_to_pfn(freepage);
1581
1582 if (pfn >= highest)
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001583 highest = max(pageblock_start_pfn(pfn),
1584 cc->zone->zone_start_pfn);
Mel Gorman5a811882019-03-05 15:45:01 -08001585
1586 if (pfn >= low_pfn) {
1587 cc->fast_search_fail = 0;
Mel Gormandbe2d4e2019-03-05 15:45:31 -08001588 cc->search_order = order;
Mel Gorman5a811882019-03-05 15:45:01 -08001589 page = freepage;
1590 break;
1591 }
1592
1593 if (pfn >= min_pfn && pfn > high_pfn) {
1594 high_pfn = pfn;
1595
1596 /* Shorten the scan if a candidate is found */
1597 limit >>= 1;
1598 }
1599
1600 if (order_scanned >= limit)
1601 break;
1602 }
1603
1604 /* Use a minimum pfn if a preferred one was not found */
1605 if (!page && high_pfn) {
1606 page = pfn_to_page(high_pfn);
1607
1608 /* Update freepage for the list reorder below */
1609 freepage = page;
1610 }
1611
1612 /* Reorder to so a future search skips recent pages */
1613 move_freelist_head(freelist, freepage);
1614
1615 /* Isolate the page if available */
1616 if (page) {
1617 if (__isolate_free_page(page, order)) {
1618 set_page_private(page, order);
1619 nr_isolated = 1 << order;
1620 cc->nr_freepages += nr_isolated;
1621 list_add_tail(&page->lru, &cc->freepages);
1622 count_compact_events(COMPACTISOLATED, nr_isolated);
1623 } else {
1624 /* If isolation fails, abort the search */
Qian Cai5b56d992019-04-04 11:54:41 +01001625 order = cc->search_order + 1;
Mel Gorman5a811882019-03-05 15:45:01 -08001626 page = NULL;
1627 }
1628 }
1629
1630 spin_unlock_irqrestore(&cc->zone->lock, flags);
1631
1632 /*
Wonhyuk Yangb55ca522021-06-30 18:50:53 -07001633 * Smaller scan on next order so the total scan is related
Mel Gorman5a811882019-03-05 15:45:01 -08001634 * to freelist_scan_limit.
1635 */
1636 if (order_scanned >= limit)
Wonhyuk Yangb55ca522021-06-30 18:50:53 -07001637 limit = max(1U, limit >> 1);
Mel Gorman5a811882019-03-05 15:45:01 -08001638 }
1639
1640 if (!page) {
1641 cc->fast_search_fail++;
1642 if (scan_start) {
1643 /*
1644 * Use the highest PFN found above min. If one was
Ethon Paulf3867752020-06-04 16:49:13 -07001645 * not found, be pessimistic for direct compaction
Mel Gorman5a811882019-03-05 15:45:01 -08001646 * and use the min mark.
1647 */
1648 if (highest) {
1649 page = pfn_to_page(highest);
1650 cc->free_pfn = highest;
1651 } else {
Suzuki K Poulosee577c8b2019-05-31 22:30:59 -07001652 if (cc->direct_compaction && pfn_valid(min_pfn)) {
Baoquan He73a6e472020-06-03 15:57:55 -07001653 page = pageblock_pfn_to_page(min_pfn,
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001654 min(pageblock_end_pfn(min_pfn),
1655 zone_end_pfn(cc->zone)),
Baoquan He73a6e472020-06-03 15:57:55 -07001656 cc->zone);
Mel Gorman5a811882019-03-05 15:45:01 -08001657 cc->free_pfn = min_pfn;
1658 }
1659 }
1660 }
1661 }
1662
Mel Gormand097a6f2019-03-05 15:45:28 -08001663 if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1664 highest -= pageblock_nr_pages;
Mel Gorman5a811882019-03-05 15:45:01 -08001665 cc->zone->compact_cached_free_pfn = highest;
Mel Gormand097a6f2019-03-05 15:45:28 -08001666 }
Mel Gorman5a811882019-03-05 15:45:01 -08001667
1668 cc->total_free_scanned += nr_scanned;
1669 if (!page)
1670 return cc->free_pfn;
1671
1672 low_pfn = page_to_pfn(page);
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001673 fast_isolate_around(cc, low_pfn);
Mel Gorman5a811882019-03-05 15:45:01 -08001674 return low_pfn;
1675}
1676
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001677/*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001678 * Based on information in the current compact_control, find blocks
1679 * suitable for isolating free pages from and then isolate them.
1680 */
Minchan Kima934b9232023-01-12 09:52:49 -08001681static void isolate_freepages(struct compact_control_ext *cc_ext)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001682{
Minchan Kima934b9232023-01-12 09:52:49 -08001683 struct compact_control *cc = cc_ext->cc;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001684 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001685 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001686 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001687 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001688 unsigned long block_end_pfn; /* end of current pageblock */
1689 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001690 struct list_head *freelist = &cc->freepages;
Mel Gorman4fca9732019-03-05 15:45:34 -08001691 unsigned int stride;
qinglin.li0681d572023-11-06 15:02:04 +08001692 bool bypass = false;
Tao Zeng54f845e2022-01-26 16:04:08 +08001693#ifdef CONFIG_AMLOGIC_CMA
1694 int migrate_type;
1695#endif /* CONFIG_AMLOGIC_CMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001696
Mel Gorman5a811882019-03-05 15:45:01 -08001697 /* Try a small search of the free lists for a candidate */
1698 isolate_start_pfn = fast_isolate_freepages(cc);
1699 if (cc->nr_freepages)
1700 goto splitmap;
1701
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001702 /*
1703 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001704 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001705 * zone when isolating for the first time. For looping we also need
1706 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001707 * block_start_pfn -= pageblock_nr_pages in the for loop.
1708 * For ending point, take care when isolating in last pageblock of a
Randy Dunlapa1c1dbe2020-08-11 18:32:49 -07001709 * zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001710 * The low boundary is the end of the pageblock the migration scanner
1711 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001712 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001713 isolate_start_pfn = cc->free_pfn;
Mel Gorman5a811882019-03-05 15:45:01 -08001714 block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001715 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1716 zone_end_pfn(zone));
Vlastimil Babka06b66402016-05-19 17:11:48 -07001717 low_pfn = pageblock_end_pfn(cc->migrate_pfn);
Mel Gorman4fca9732019-03-05 15:45:34 -08001718 stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001719
1720 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001721 * Isolate free pages until enough are available to migrate the
1722 * pages on cc->migratepages. We stop searching if the migrate
1723 * and free page scanners meet or enough free pages are isolated.
1724 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001725 for (; block_start_pfn >= low_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001726 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001727 block_start_pfn -= pageblock_nr_pages,
1728 isolate_start_pfn = block_start_pfn) {
Mel Gorman4fca9732019-03-05 15:45:34 -08001729 unsigned long nr_isolated;
1730
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001731 /*
1732 * This can iterate a massively long zone without finding any
Mel Gormancb810ad2019-03-05 15:45:21 -08001733 * suitable migration targets, so periodically check resched.
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001734 */
Mel Gormancb810ad2019-03-05 15:45:21 -08001735 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
Mel Gormancf66f072019-03-05 15:45:24 -08001736 cond_resched();
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001737
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001738 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1739 zone);
1740 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001741 continue;
1742
1743 /* Check the block is suitable for migration */
Minchan Kima934b9232023-01-12 09:52:49 -08001744 if (!suitable_migration_target(cc_ext, page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001745 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -07001746
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001747 /* If isolation recently failed, do not retry */
1748 if (!isolation_suitable(cc, page))
1749 continue;
1750
qinglin.li0681d572023-11-06 15:02:04 +08001751 trace_android_vh_isolate_freepages(cc, page, &bypass);
1752 if (bypass)
1753 continue;
1754
Tao Zeng54f845e2022-01-26 16:04:08 +08001755 #ifdef CONFIG_AMLOGIC_CMA
1756 /* avoid compact to cma area */
1757 migrate_type = get_pageblock_migratetype(page);
1758 if (is_migrate_isolate(migrate_type))
1759 continue;
1760 if (is_migrate_cma(migrate_type) &&
1761 test_bit(FORBID_TO_CMA_BIT, &cc->total_migrate_scanned))
1762 continue;
1763 #endif /* CONFIG_AMLOGIC_CMA */
1764
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001765 /* Found a block suitable for isolating free pages from. */
Mel Gorman4fca9732019-03-05 15:45:34 -08001766 nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
1767 block_end_pfn, freelist, stride, false);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001768
Mel Gormand097a6f2019-03-05 15:45:28 -08001769 /* Update the skip hint if the full pageblock was scanned */
1770 if (isolate_start_pfn == block_end_pfn)
1771 update_pageblock_skip(cc, page, block_start_pfn);
1772
Mel Gormancb2dcaf2019-03-05 15:45:11 -08001773 /* Are enough freepages isolated? */
1774 if (cc->nr_freepages >= cc->nr_migratepages) {
David Rientjesa46cbf32016-07-14 12:06:50 -07001775 if (isolate_start_pfn >= block_end_pfn) {
1776 /*
1777 * Restart at previous pageblock if more
1778 * freepages can be isolated next time.
1779 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001780 isolate_start_pfn =
1781 block_start_pfn - pageblock_nr_pages;
David Rientjesa46cbf32016-07-14 12:06:50 -07001782 }
Vlastimil Babkabe976572014-06-04 16:10:41 -07001783 break;
David Rientjesa46cbf32016-07-14 12:06:50 -07001784 } else if (isolate_start_pfn < block_end_pfn) {
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001785 /*
David Rientjesa46cbf32016-07-14 12:06:50 -07001786 * If isolation failed early, do not continue
1787 * needlessly.
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001788 */
David Rientjesa46cbf32016-07-14 12:06:50 -07001789 break;
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001790 }
Mel Gorman4fca9732019-03-05 15:45:34 -08001791
1792 /* Adjust stride depending on isolation */
1793 if (nr_isolated) {
1794 stride = 1;
1795 continue;
1796 }
1797 stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001798 }
1799
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001800 /*
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001801 * Record where the free scanner will restart next time. Either we
1802 * broke from the loop and set isolate_start_pfn based on the last
1803 * call to isolate_freepages_block(), or we met the migration scanner
1804 * and the loop terminated due to isolate_start_pfn < low_pfn
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001805 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001806 cc->free_pfn = isolate_start_pfn;
Mel Gorman5a811882019-03-05 15:45:01 -08001807
1808splitmap:
1809 /* __isolate_free_page() does not map the pages */
1810 split_map_pages(freelist);
Mel Gorman748446b2010-05-24 14:32:27 -07001811}
1812
1813/*
1814 * This is a migrate-callback that "allocates" freepages by taking pages
1815 * from the isolated freelists in the block we are migrating to.
1816 */
1817static struct page *compaction_alloc(struct page *migratepage,
Michal Hocko666feb22018-04-10 16:30:03 -07001818 unsigned long data)
Mel Gorman748446b2010-05-24 14:32:27 -07001819{
Minchan Kima934b9232023-01-12 09:52:49 -08001820 struct compact_control_ext *cc_ext = (struct compact_control_ext *)data;
1821 struct compact_control *cc = cc_ext->cc;
Mel Gorman748446b2010-05-24 14:32:27 -07001822 struct page *freepage;
1823
Mel Gorman748446b2010-05-24 14:32:27 -07001824 if (list_empty(&cc->freepages)) {
Minchan Kima934b9232023-01-12 09:52:49 -08001825 isolate_freepages(cc_ext);
Mel Gorman748446b2010-05-24 14:32:27 -07001826
1827 if (list_empty(&cc->freepages))
1828 return NULL;
1829 }
1830
Tao Zeng54f845e2022-01-26 16:04:08 +08001831#ifdef CONFIG_AMLOGIC_CMA
1832 freepage = get_compact_page(migratepage, cc);
1833#else
Mel Gorman748446b2010-05-24 14:32:27 -07001834 freepage = list_entry(cc->freepages.next, struct page, lru);
1835 list_del(&freepage->lru);
1836 cc->nr_freepages--;
Tao Zeng54f845e2022-01-26 16:04:08 +08001837#endif
Tao Zenga1db7dc2022-01-20 15:09:43 +08001838#ifdef CONFIG_AMLOGIC_PAGE_TRACE
1839 replace_page_trace(freepage, migratepage);
1840#endif
Mel Gorman748446b2010-05-24 14:32:27 -07001841
1842 return freepage;
1843}
1844
1845/*
David Rientjesd53aea32014-06-04 16:08:26 -07001846 * This is a migrate-callback that "frees" freepages back to the isolated
1847 * freelist. All pages on the freelist are from the same zone, so there is no
1848 * special handling needed for NUMA.
1849 */
1850static void compaction_free(struct page *page, unsigned long data)
1851{
Minchan Kima934b9232023-01-12 09:52:49 -08001852 struct compact_control_ext *cc_ext = (struct compact_control_ext *)data;
1853 struct compact_control *cc = cc_ext->cc;
David Rientjesd53aea32014-06-04 16:08:26 -07001854
1855 list_add(&page->lru, &cc->freepages);
1856 cc->nr_freepages++;
1857}
1858
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001859/* possible outcome of isolate_migratepages */
1860typedef enum {
1861 ISOLATE_ABORT, /* Abort compaction now */
1862 ISOLATE_NONE, /* No pages isolated, continue scanning */
1863 ISOLATE_SUCCESS, /* Pages isolated, migrate */
1864} isolate_migrate_t;
1865
1866/*
Eric B Munson5bbe3542015-04-15 16:13:20 -07001867 * Allow userspace to control policy on scanning the unevictable LRU for
1868 * compactable pages.
1869 */
Sebastian Andrzej Siewior6923aa02020-04-01 21:10:42 -07001870#ifdef CONFIG_PREEMPT_RT
1871int sysctl_compact_unevictable_allowed __read_mostly = 0;
1872#else
Eric B Munson5bbe3542015-04-15 16:13:20 -07001873int sysctl_compact_unevictable_allowed __read_mostly = 1;
Sebastian Andrzej Siewior6923aa02020-04-01 21:10:42 -07001874#endif
Eric B Munson5bbe3542015-04-15 16:13:20 -07001875
Mel Gorman70b44592019-03-05 15:44:54 -08001876static inline void
1877update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
1878{
1879 if (cc->fast_start_pfn == ULONG_MAX)
1880 return;
1881
1882 if (!cc->fast_start_pfn)
1883 cc->fast_start_pfn = pfn;
1884
1885 cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
1886}
1887
1888static inline unsigned long
1889reinit_migrate_pfn(struct compact_control *cc)
1890{
1891 if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
1892 return cc->migrate_pfn;
1893
1894 cc->migrate_pfn = cc->fast_start_pfn;
1895 cc->fast_start_pfn = ULONG_MAX;
1896
1897 return cc->migrate_pfn;
1898}
1899
1900/*
1901 * Briefly search the free lists for a migration source that already has
1902 * some free pages to reduce the number of pages that need migration
1903 * before a pageblock is free.
1904 */
1905static unsigned long fast_find_migrateblock(struct compact_control *cc)
1906{
1907 unsigned int limit = freelist_scan_limit(cc);
1908 unsigned int nr_scanned = 0;
1909 unsigned long distance;
1910 unsigned long pfn = cc->migrate_pfn;
1911 unsigned long high_pfn;
1912 int order;
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001913 bool found_block = false;
Mel Gorman70b44592019-03-05 15:44:54 -08001914
1915 /* Skip hints are relied on to avoid repeats on the fast search */
1916 if (cc->ignore_skip_hint)
1917 return pfn;
1918
1919 /*
1920 * If the migrate_pfn is not at the start of a zone or the start
1921 * of a pageblock then assume this is a continuation of a previous
1922 * scan restarted due to COMPACT_CLUSTER_MAX.
1923 */
1924 if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
1925 return pfn;
1926
1927 /*
1928 * For smaller orders, just linearly scan as the number of pages
1929 * to migrate should be relatively small and does not necessarily
1930 * justify freeing up a large block for a small allocation.
1931 */
1932 if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
1933 return pfn;
1934
1935 /*
1936 * Only allow kcompactd and direct requests for movable pages to
1937 * quickly clear out a MOVABLE pageblock for allocation. This
1938 * reduces the risk that a large movable pageblock is freed for
1939 * an unmovable/reclaimable small allocation.
1940 */
1941 if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
1942 return pfn;
1943
1944 /*
1945 * When starting the migration scanner, pick any pageblock within the
1946 * first half of the search space. Otherwise try and pick a pageblock
1947 * within the first eighth to reduce the chances that a migration
1948 * target later becomes a source.
1949 */
1950 distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
1951 if (cc->migrate_pfn != cc->zone->zone_start_pfn)
1952 distance >>= 2;
1953 high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
1954
1955 for (order = cc->order - 1;
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001956 order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit;
Mel Gorman70b44592019-03-05 15:44:54 -08001957 order--) {
1958 struct free_area *area = &cc->zone->free_area[order];
1959 struct list_head *freelist;
1960 unsigned long flags;
1961 struct page *freepage;
1962
1963 if (!area->nr_free)
1964 continue;
1965
1966 spin_lock_irqsave(&cc->zone->lock, flags);
1967 freelist = &area->free_list[MIGRATE_MOVABLE];
1968 list_for_each_entry(freepage, freelist, lru) {
1969 unsigned long free_pfn;
1970
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001971 if (nr_scanned++ >= limit) {
1972 move_freelist_tail(freelist, freepage);
1973 break;
1974 }
1975
Mel Gorman70b44592019-03-05 15:44:54 -08001976 free_pfn = page_to_pfn(freepage);
1977 if (free_pfn < high_pfn) {
Mel Gorman70b44592019-03-05 15:44:54 -08001978 /*
1979 * Avoid if skipped recently. Ideally it would
1980 * move to the tail but even safe iteration of
1981 * the list assumes an entry is deleted, not
1982 * reordered.
1983 */
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001984 if (get_pageblock_skip(freepage))
Mel Gorman70b44592019-03-05 15:44:54 -08001985 continue;
Mel Gorman70b44592019-03-05 15:44:54 -08001986
1987 /* Reorder to so a future search skips recent pages */
1988 move_freelist_tail(freelist, freepage);
1989
Mel Gormane380beb2019-03-05 15:44:58 -08001990 update_fast_start_pfn(cc, free_pfn);
Mel Gorman70b44592019-03-05 15:44:54 -08001991 pfn = pageblock_start_pfn(free_pfn);
Rei Yamamoto20e6ec72022-05-13 16:48:57 -07001992 if (pfn < cc->zone->zone_start_pfn)
1993 pfn = cc->zone->zone_start_pfn;
Mel Gorman70b44592019-03-05 15:44:54 -08001994 cc->fast_search_fail = 0;
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001995 found_block = true;
Mel Gorman70b44592019-03-05 15:44:54 -08001996 set_pageblock_skip(freepage);
1997 break;
1998 }
Mel Gorman70b44592019-03-05 15:44:54 -08001999 }
2000 spin_unlock_irqrestore(&cc->zone->lock, flags);
2001 }
2002
2003 cc->total_migrate_scanned += nr_scanned;
2004
2005 /*
2006 * If fast scanning failed then use a cached entry for a page block
2007 * that had free pages as the basis for starting a linear scan.
2008 */
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08002009 if (!found_block) {
2010 cc->fast_search_fail++;
Mel Gorman70b44592019-03-05 15:44:54 -08002011 pfn = reinit_migrate_pfn(cc);
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08002012 }
Mel Gorman70b44592019-03-05 15:44:54 -08002013 return pfn;
2014}
2015
Eric B Munson5bbe3542015-04-15 16:13:20 -07002016/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002017 * Isolate all pages that can be migrated from the first suitable block,
2018 * starting at the block pointed to by the migrate scanner pfn within
2019 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002020 */
Minchan Kima934b9232023-01-12 09:52:49 -08002021static isolate_migrate_t isolate_migratepages(struct compact_control_ext *cc_ext)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002022{
Minchan Kima934b9232023-01-12 09:52:49 -08002023 struct compact_control *cc = cc_ext->cc;
Joonsoo Kime1409c32016-03-15 14:57:48 -07002024 unsigned long block_start_pfn;
2025 unsigned long block_end_pfn;
2026 unsigned long low_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002027 struct page *page;
2028 const isolate_mode_t isolate_mode =
Eric B Munson5bbe3542015-04-15 16:13:20 -07002029 (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
Hugh Dickins1d2047f2016-07-28 15:48:41 -07002030 (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Mel Gorman70b44592019-03-05 15:44:54 -08002031 bool fast_find_block;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002032
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002033 /*
2034 * Start at where we last stopped, or beginning of the zone as
Mel Gorman70b44592019-03-05 15:44:54 -08002035 * initialized by compact_zone(). The first failure will use
2036 * the lowest PFN as the starting point for linear scanning.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002037 */
Mel Gorman70b44592019-03-05 15:44:54 -08002038 low_pfn = fast_find_migrateblock(cc);
Vlastimil Babka06b66402016-05-19 17:11:48 -07002039 block_start_pfn = pageblock_start_pfn(low_pfn);
Pengfei Li32aaf052019-09-23 15:36:58 -07002040 if (block_start_pfn < cc->zone->zone_start_pfn)
2041 block_start_pfn = cc->zone->zone_start_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002042
Mel Gorman70b44592019-03-05 15:44:54 -08002043 /*
2044 * fast_find_migrateblock marks a pageblock skipped so to avoid
2045 * the isolation_suitable check below, check whether the fast
2046 * search was successful.
2047 */
2048 fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
2049
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002050 /* Only scan within a pageblock boundary */
Vlastimil Babka06b66402016-05-19 17:11:48 -07002051 block_end_pfn = pageblock_end_pfn(low_pfn);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002052
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002053 /*
2054 * Iterate over whole pageblocks until we find the first suitable.
2055 * Do not cross the free scanner.
2056 */
Joonsoo Kime1409c32016-03-15 14:57:48 -07002057 for (; block_end_pfn <= cc->free_pfn;
Mel Gorman70b44592019-03-05 15:44:54 -08002058 fast_find_block = false,
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07002059 cc->migrate_pfn = low_pfn = block_end_pfn,
Joonsoo Kime1409c32016-03-15 14:57:48 -07002060 block_start_pfn = block_end_pfn,
2061 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002062
2063 /*
2064 * This can potentially iterate a massively long zone with
2065 * many pageblocks unsuitable, so periodically check if we
Mel Gormancb810ad2019-03-05 15:45:21 -08002066 * need to schedule.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002067 */
Mel Gormancb810ad2019-03-05 15:45:21 -08002068 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
Mel Gormancf66f072019-03-05 15:45:24 -08002069 cond_resched();
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002070
Pengfei Li32aaf052019-09-23 15:36:58 -07002071 page = pageblock_pfn_to_page(block_start_pfn,
2072 block_end_pfn, cc->zone);
Vlastimil Babka7d49d882014-10-09 15:27:11 -07002073 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002074 continue;
2075
Mel Gormane380beb2019-03-05 15:44:58 -08002076 /*
2077 * If isolation recently failed, do not retry. Only check the
2078 * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
2079 * to be visited multiple times. Assume skip was checked
2080 * before making it "skip" so other compaction instances do
2081 * not scan the same block.
2082 */
2083 if (IS_ALIGNED(low_pfn, pageblock_nr_pages) &&
2084 !fast_find_block && !isolation_suitable(cc, page))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002085 continue;
2086
2087 /*
Mel Gorman9bebefd2019-03-05 15:45:14 -08002088 * For async compaction, also only scan in MOVABLE blocks
2089 * without huge pages. Async compaction is optimistic to see
2090 * if the minimum amount of work satisfies the allocation.
2091 * The cached PFN is updated as it's possible that all
2092 * remaining blocks between source and target are unsuitable
2093 * and the compaction scanners fail to meet.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002094 */
Mel Gorman9bebefd2019-03-05 15:45:14 -08002095 if (!suitable_migration_source(cc, page)) {
2096 update_cached_migrate(cc, block_end_pfn);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002097 continue;
Mel Gorman9bebefd2019-03-05 15:45:14 -08002098 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002099
2100 /* Perform the isolation */
Minchan Kima934b9232023-01-12 09:52:49 -08002101 if (isolate_migratepages_block(cc_ext, low_pfn, block_end_pfn,
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07002102 isolate_mode))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002103 return ISOLATE_ABORT;
2104
2105 /*
2106 * Either we isolated something and proceed with migration. Or
2107 * we failed and compact_zone should decide if we should
2108 * continue or not.
2109 */
2110 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002111 }
2112
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002113 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002114}
2115
Yaowei Bai21c527a2015-11-05 18:47:20 -08002116/*
2117 * order == -1 is expected when compacting via
2118 * /proc/sys/vm/compact_memory
2119 */
2120static inline bool is_via_compact_memory(int order)
2121{
2122 return order == -1;
2123}
2124
Nitin Guptafacdaa92020-08-11 18:31:00 -07002125static bool kswapd_is_running(pg_data_t *pgdat)
2126{
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02002127 return pgdat->kswapd && task_is_running(pgdat->kswapd);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002128}
2129
2130/*
2131 * A zone's fragmentation score is the external fragmentation wrt to the
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002132 * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100].
2133 */
2134static unsigned int fragmentation_score_zone(struct zone *zone)
2135{
2136 return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER);
2137}
2138
2139/*
2140 * A weighted zone's fragmentation score is the external fragmentation
2141 * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It
2142 * returns a value in the range [0, 100].
Nitin Guptafacdaa92020-08-11 18:31:00 -07002143 *
2144 * The scaling factor ensures that proactive compaction focuses on larger
2145 * zones like ZONE_NORMAL, rather than smaller, specialized zones like
2146 * ZONE_DMA32. For smaller zones, the score value remains close to zero,
2147 * and thus never exceeds the high threshold for proactive compaction.
2148 */
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002149static unsigned int fragmentation_score_zone_weighted(struct zone *zone)
Nitin Guptafacdaa92020-08-11 18:31:00 -07002150{
2151 unsigned long score;
2152
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002153 score = zone->present_pages * fragmentation_score_zone(zone);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002154 return div64_ul(score, zone->zone_pgdat->node_present_pages + 1);
2155}
2156
2157/*
2158 * The per-node proactive (background) compaction process is started by its
2159 * corresponding kcompactd thread when the node's fragmentation score
2160 * exceeds the high threshold. The compaction process remains active till
2161 * the node's score falls below the low threshold, or one of the back-off
2162 * conditions is met.
2163 */
Nitin Guptad34c0a72020-08-11 18:31:07 -07002164static unsigned int fragmentation_score_node(pg_data_t *pgdat)
Nitin Guptafacdaa92020-08-11 18:31:00 -07002165{
Nitin Guptad34c0a72020-08-11 18:31:07 -07002166 unsigned int score = 0;
Nitin Guptafacdaa92020-08-11 18:31:00 -07002167 int zoneid;
2168
2169 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2170 struct zone *zone;
2171
2172 zone = &pgdat->node_zones[zoneid];
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002173 score += fragmentation_score_zone_weighted(zone);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002174 }
2175
2176 return score;
2177}
2178
Nitin Guptad34c0a72020-08-11 18:31:07 -07002179static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low)
Nitin Guptafacdaa92020-08-11 18:31:00 -07002180{
Nitin Guptad34c0a72020-08-11 18:31:07 -07002181 unsigned int wmark_low;
Nitin Guptafacdaa92020-08-11 18:31:00 -07002182
2183 /*
Ingo Molnarf0953a12021-05-06 18:06:47 -07002184 * Cap the low watermark to avoid excessive compaction
2185 * activity in case a user sets the proactiveness tunable
Nitin Guptafacdaa92020-08-11 18:31:00 -07002186 * close to 100 (maximum).
2187 */
Nitin Guptad34c0a72020-08-11 18:31:07 -07002188 wmark_low = max(100U - sysctl_compaction_proactiveness, 5U);
2189 return low ? wmark_low : min(wmark_low + 10, 100U);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002190}
2191
2192static bool should_proactive_compact_node(pg_data_t *pgdat)
2193{
2194 int wmark_high;
2195
2196 if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat))
2197 return false;
2198
2199 wmark_high = fragmentation_score_wmark(pgdat, false);
2200 return fragmentation_score_node(pgdat) > wmark_high;
2201}
2202
Mel Gorman40cacbc2019-03-05 15:44:36 -08002203static enum compact_result __compact_finished(struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -07002204{
Mel Gorman8fb74b92013-01-11 14:32:16 -08002205 unsigned int order;
Vlastimil Babkad39773a2017-05-08 15:54:46 -07002206 const int migratetype = cc->migratetype;
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002207 int ret;
Mel Gorman748446b2010-05-24 14:32:27 -07002208
Mel Gorman753341a2012-10-08 16:32:40 -07002209 /* Compaction run completes if the migrate and free scanner meet */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07002210 if (compact_scanners_met(cc)) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08002211 /* Let the next compaction start anew. */
Mel Gorman40cacbc2019-03-05 15:44:36 -08002212 reset_cached_positions(cc->zone);
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08002213
Mel Gorman62997022012-10-08 16:32:47 -07002214 /*
2215 * Mark that the PG_migrate_skip information should be cleared
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002216 * by kswapd when it goes to sleep. kcompactd does not set the
Mel Gorman62997022012-10-08 16:32:47 -07002217 * flag itself as the decision to be clear should be directly
2218 * based on an allocation request.
2219 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002220 if (cc->direct_compaction)
Mel Gorman40cacbc2019-03-05 15:44:36 -08002221 cc->zone->compact_blockskip_flush = true;
Mel Gorman62997022012-10-08 16:32:47 -07002222
Michal Hockoc8f7de02016-05-20 16:56:47 -07002223 if (cc->whole_zone)
2224 return COMPACT_COMPLETE;
2225 else
2226 return COMPACT_PARTIAL_SKIPPED;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07002227 }
Mel Gorman748446b2010-05-24 14:32:27 -07002228
Nitin Guptafacdaa92020-08-11 18:31:00 -07002229 if (cc->proactive_compaction) {
2230 int score, wmark_low;
2231 pg_data_t *pgdat;
2232
2233 pgdat = cc->zone->zone_pgdat;
2234 if (kswapd_is_running(pgdat))
2235 return COMPACT_PARTIAL_SKIPPED;
2236
2237 score = fragmentation_score_zone(cc->zone);
2238 wmark_low = fragmentation_score_wmark(pgdat, true);
2239
2240 if (score > wmark_low)
2241 ret = COMPACT_CONTINUE;
2242 else
2243 ret = COMPACT_SUCCESS;
2244
2245 goto out;
2246 }
2247
Yaowei Bai21c527a2015-11-05 18:47:20 -08002248 if (is_via_compact_memory(cc->order))
Mel Gorman56de7262010-05-24 14:32:30 -07002249 return COMPACT_CONTINUE;
2250
Mel Gormanefe771c2019-03-05 15:44:46 -08002251 /*
2252 * Always finish scanning a pageblock to reduce the possibility of
2253 * fallbacks in the future. This is particularly important when
2254 * migration source is unmovable/reclaimable but it's not worth
2255 * special casing.
2256 */
2257 if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
2258 return COMPACT_CONTINUE;
Vlastimil Babkabaf6a9a2017-05-08 15:54:52 -07002259
Mel Gorman56de7262010-05-24 14:32:30 -07002260 /* Direct compactor: Is a suitable page free? */
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002261 ret = COMPACT_NO_SUITABLE_PAGE;
Mel Gorman8fb74b92013-01-11 14:32:16 -08002262 for (order = cc->order; order < MAX_ORDER; order++) {
Mel Gorman40cacbc2019-03-05 15:44:36 -08002263 struct free_area *area = &cc->zone->free_area[order];
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002264 bool can_steal;
Mel Gorman56de7262010-05-24 14:32:30 -07002265
Mel Gorman8fb74b92013-01-11 14:32:16 -08002266 /* Job done if page is free of the right migratetype */
Dan Williamsb03641a2019-05-14 15:41:32 -07002267 if (!free_area_empty(area, migratetype))
Vlastimil Babkacf378312016-10-07 16:57:41 -07002268 return COMPACT_SUCCESS;
Mel Gorman8fb74b92013-01-11 14:32:16 -08002269
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002270#ifdef CONFIG_CMA
2271 /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
2272 if (migratetype == MIGRATE_MOVABLE &&
Dan Williamsb03641a2019-05-14 15:41:32 -07002273 !free_area_empty(area, MIGRATE_CMA))
Vlastimil Babkacf378312016-10-07 16:57:41 -07002274 return COMPACT_SUCCESS;
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002275#endif
2276 /*
2277 * Job done if allocation would steal freepages from
2278 * other migratetype buddy lists.
2279 */
2280 if (find_suitable_fallback(area, order, migratetype,
Vlastimil Babkabaf6a9a2017-05-08 15:54:52 -07002281 true, &can_steal) != -1) {
2282
2283 /* movable pages are OK in any pageblock */
2284 if (migratetype == MIGRATE_MOVABLE)
2285 return COMPACT_SUCCESS;
2286
2287 /*
2288 * We are stealing for a non-movable allocation. Make
2289 * sure we finish compacting the current pageblock
2290 * first so it is as free as possible and we won't
2291 * have to steal another one soon. This only applies
2292 * to sync compaction, as async compaction operates
2293 * on pageblocks of the same migratetype.
2294 */
2295 if (cc->mode == MIGRATE_ASYNC ||
2296 IS_ALIGNED(cc->migrate_pfn,
2297 pageblock_nr_pages)) {
2298 return COMPACT_SUCCESS;
2299 }
2300
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002301 ret = COMPACT_CONTINUE;
2302 break;
Vlastimil Babkabaf6a9a2017-05-08 15:54:52 -07002303 }
Mel Gorman56de7262010-05-24 14:32:30 -07002304 }
2305
Nitin Guptafacdaa92020-08-11 18:31:00 -07002306out:
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002307 if (cc->contended || fatal_signal_pending(current))
2308 ret = COMPACT_CONTENDED;
2309
2310 return ret;
Joonsoo Kim837d0262015-02-11 15:27:06 -08002311}
2312
Mel Gorman40cacbc2019-03-05 15:44:36 -08002313static enum compact_result compact_finished(struct compact_control *cc)
Joonsoo Kim837d0262015-02-11 15:27:06 -08002314{
2315 int ret;
2316
Mel Gorman40cacbc2019-03-05 15:44:36 -08002317 ret = __compact_finished(cc);
2318 trace_mm_compaction_finished(cc->zone, cc->order, ret);
Joonsoo Kim837d0262015-02-11 15:27:06 -08002319 if (ret == COMPACT_NO_SUITABLE_PAGE)
2320 ret = COMPACT_CONTINUE;
2321
2322 return ret;
Mel Gorman748446b2010-05-24 14:32:27 -07002323}
2324
Michal Hockoea7ab982016-05-20 16:56:38 -07002325static enum compact_result __compaction_suitable(struct zone *zone, int order,
Mel Gormanc6038442016-05-19 17:13:38 -07002326 unsigned int alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002327 int highest_zoneidx,
Michal Hocko86a294a2016-05-20 16:57:12 -07002328 unsigned long wmark_target)
Mel Gorman3e7d3442011-01-13 15:45:56 -08002329{
Mel Gorman3e7d3442011-01-13 15:45:56 -08002330 unsigned long watermark;
2331
Yaowei Bai21c527a2015-11-05 18:47:20 -08002332 if (is_via_compact_memory(order))
Michal Hocko3957c772011-06-15 15:08:25 -07002333 return COMPACT_CONTINUE;
2334
Mel Gormana9214442018-12-28 00:35:44 -08002335 watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002336 /*
2337 * If watermarks for high-order allocation are already met, there
2338 * should be no need for compaction at all.
2339 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002340 if (zone_watermark_ok(zone, order, watermark, highest_zoneidx,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002341 alloc_flags))
Vlastimil Babkacf378312016-10-07 16:57:41 -07002342 return COMPACT_SUCCESS;
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002343
Michal Hocko3957c772011-06-15 15:08:25 -07002344 /*
Vlastimil Babka9861a622016-10-07 16:57:53 -07002345 * Watermarks for order-0 must be met for compaction to be able to
Vlastimil Babka984fdba2016-10-07 16:57:57 -07002346 * isolate free pages for migration targets. This means that the
2347 * watermark and alloc_flags have to match, or be more pessimistic than
2348 * the check in __isolate_free_page(). We don't use the direct
2349 * compactor's alloc_flags, as they are not relevant for freepage
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002350 * isolation. We however do use the direct compactor's highest_zoneidx
2351 * to skip over zones where lowmem reserves would prevent allocation
2352 * even if compaction succeeds.
Vlastimil Babka8348faf2016-10-07 16:58:00 -07002353 * For costly orders, we require low watermark instead of min for
2354 * compaction to proceed to increase its chances.
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09002355 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
2356 * suitable migration targets
Mel Gorman3e7d3442011-01-13 15:45:56 -08002357 */
Vlastimil Babka8348faf2016-10-07 16:58:00 -07002358 watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
2359 low_wmark_pages(zone) : min_wmark_pages(zone);
2360 watermark += compact_gap(order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002361 if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx,
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09002362 ALLOC_CMA, wmark_target))
Mel Gorman3e7d3442011-01-13 15:45:56 -08002363 return COMPACT_SKIPPED;
2364
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002365 return COMPACT_CONTINUE;
2366}
2367
Hui Su2b1a20c2020-12-14 19:12:42 -08002368/*
2369 * compaction_suitable: Is this suitable to run compaction on this zone now?
2370 * Returns
2371 * COMPACT_SKIPPED - If there are too few free pages for compaction
2372 * COMPACT_SUCCESS - If the allocation would succeed without compaction
2373 * COMPACT_CONTINUE - If compaction should run now
2374 */
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002375enum compact_result compaction_suitable(struct zone *zone, int order,
2376 unsigned int alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002377 int highest_zoneidx)
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002378{
2379 enum compact_result ret;
2380 int fragindex;
2381
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002382 ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx,
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002383 zone_page_state(zone, NR_FREE_PAGES));
Mel Gorman3e7d3442011-01-13 15:45:56 -08002384 /*
2385 * fragmentation index determines if allocation failures are due to
2386 * low memory or external fragmentation
2387 *
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002388 * index of -1000 would imply allocations might succeed depending on
2389 * watermarks, but we already failed the high-order watermark check
Mel Gorman3e7d3442011-01-13 15:45:56 -08002390 * index towards 0 implies failure is due to lack of memory
2391 * index towards 1000 implies failure is due to fragmentation
2392 *
Vlastimil Babka20311422016-10-07 17:00:46 -07002393 * Only compact if a failure would be due to fragmentation. Also
2394 * ignore fragindex for non-costly orders where the alternative to
2395 * a successful reclaim/compaction is OOM. Fragindex and the
2396 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
2397 * excessive compaction for costly orders, but it should not be at the
2398 * expense of system stability.
Mel Gorman3e7d3442011-01-13 15:45:56 -08002399 */
Vlastimil Babka20311422016-10-07 17:00:46 -07002400 if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002401 fragindex = fragmentation_index(zone, order);
2402 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
2403 ret = COMPACT_NOT_SUITABLE_ZONE;
2404 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002405
Joonsoo Kim837d0262015-02-11 15:27:06 -08002406 trace_mm_compaction_suitable(zone, order, ret);
2407 if (ret == COMPACT_NOT_SUITABLE_ZONE)
2408 ret = COMPACT_SKIPPED;
2409
2410 return ret;
2411}
2412
Michal Hocko86a294a2016-05-20 16:57:12 -07002413bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
2414 int alloc_flags)
2415{
2416 struct zone *zone;
2417 struct zoneref *z;
2418
2419 /*
2420 * Make sure at least one zone would pass __compaction_suitable if we continue
2421 * retrying the reclaim.
2422 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002423 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2424 ac->highest_zoneidx, ac->nodemask) {
Michal Hocko86a294a2016-05-20 16:57:12 -07002425 unsigned long available;
2426 enum compact_result compact_result;
2427
2428 /*
2429 * Do not consider all the reclaimable memory because we do not
2430 * want to trash just for a single high order allocation which
2431 * is even not guaranteed to appear even if __compaction_suitable
2432 * is happy about the watermark check.
2433 */
Mel Gorman5a1c84b2016-07-28 15:47:31 -07002434 available = zone_reclaimable_pages(zone) / order;
Michal Hocko86a294a2016-05-20 16:57:12 -07002435 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
2436 compact_result = __compaction_suitable(zone, order, alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002437 ac->highest_zoneidx, available);
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002438 if (compact_result != COMPACT_SKIPPED)
Michal Hocko86a294a2016-05-20 16:57:12 -07002439 return true;
2440 }
2441
2442 return false;
2443}
2444
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002445static enum compact_result
2446compact_zone(struct compact_control *cc, struct capture_control *capc)
Mel Gorman748446b2010-05-24 14:32:27 -07002447{
Michal Hockoea7ab982016-05-20 16:56:38 -07002448 enum compact_result ret;
Mel Gorman40cacbc2019-03-05 15:44:36 -08002449 unsigned long start_pfn = cc->zone->zone_start_pfn;
2450 unsigned long end_pfn = zone_end_pfn(cc->zone);
Mel Gorman566e54e2019-03-05 15:44:32 -08002451 unsigned long last_migrated_pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -07002452 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman8854c552019-03-05 15:45:18 -08002453 bool update_cached;
Robin Hsu1eeadb42022-05-13 10:19:50 +08002454 long vendor_ret;
Minchan Kima934b9232023-01-12 09:52:49 -08002455 struct compact_control_ext cc_ext = {
2456 .cc = cc,
2457 .nr_migrate_file_pages = 0,
2458 };
Mel Gorman748446b2010-05-24 14:32:27 -07002459
Yafang Shaoa94b5252019-09-23 15:36:54 -07002460 /*
2461 * These counters track activities during zone compaction. Initialize
2462 * them before compacting a new zone.
2463 */
2464 cc->total_migrate_scanned = 0;
2465 cc->total_free_scanned = 0;
2466 cc->nr_migratepages = 0;
2467 cc->nr_freepages = 0;
2468 INIT_LIST_HEAD(&cc->freepages);
2469 INIT_LIST_HEAD(&cc->migratepages);
2470
Wei Yang01c0bfe2020-06-03 15:59:08 -07002471 cc->migratetype = gfp_migratetype(cc->gfp_mask);
Mel Gorman40cacbc2019-03-05 15:44:36 -08002472 ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002473 cc->highest_zoneidx);
Michal Hockoc46649d2016-05-20 16:56:41 -07002474 /* Compaction is likely to fail */
Vlastimil Babkacf378312016-10-07 16:57:41 -07002475 if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
Mel Gorman3e7d3442011-01-13 15:45:56 -08002476 return ret;
Michal Hockoc46649d2016-05-20 16:56:41 -07002477
2478 /* huh, compaction_suitable is returning something unexpected */
2479 VM_BUG_ON(ret != COMPACT_CONTINUE);
Mel Gorman3e7d3442011-01-13 15:45:56 -08002480
Mel Gormanc89511a2012-10-08 16:32:45 -07002481 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08002482 * Clear pageblock skip if there were failures recently and compaction
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002483 * is about to be retried after being deferred.
Vlastimil Babkad3132e42014-01-21 15:51:08 -08002484 */
Mel Gorman40cacbc2019-03-05 15:44:36 -08002485 if (compaction_restarting(cc->zone, cc->order))
2486 __reset_isolation_suitable(cc->zone);
Vlastimil Babkad3132e42014-01-21 15:51:08 -08002487
2488 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07002489 * Setup to move all movable pages to the end of the zone. Used cached
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002490 * information on where the scanners should start (unless we explicitly
2491 * want to compact the whole zone), but check that it is initialised
2492 * by ensuring the values are within zone boundaries.
Mel Gormanc89511a2012-10-08 16:32:45 -07002493 */
Mel Gorman70b44592019-03-05 15:44:54 -08002494 cc->fast_start_pfn = 0;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002495 if (cc->whole_zone) {
Mel Gormanc89511a2012-10-08 16:32:45 -07002496 cc->migrate_pfn = start_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002497 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
2498 } else {
Mel Gorman40cacbc2019-03-05 15:44:36 -08002499 cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
2500 cc->free_pfn = cc->zone->compact_cached_free_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002501 if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
2502 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
Mel Gorman40cacbc2019-03-05 15:44:36 -08002503 cc->zone->compact_cached_free_pfn = cc->free_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002504 }
2505 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
2506 cc->migrate_pfn = start_pfn;
Mel Gorman40cacbc2019-03-05 15:44:36 -08002507 cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
2508 cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002509 }
Michal Hockoc8f7de02016-05-20 16:56:47 -07002510
Mel Gormane332f742019-03-05 15:45:38 -08002511 if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002512 cc->whole_zone = true;
2513 }
Michal Hockoc8f7de02016-05-20 16:56:47 -07002514
Mel Gorman566e54e2019-03-05 15:44:32 -08002515 last_migrated_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -07002516
Mel Gorman8854c552019-03-05 15:45:18 -08002517 /*
2518 * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
2519 * the basis that some migrations will fail in ASYNC mode. However,
2520 * if the cached PFNs match and pageblocks are skipped due to having
2521 * no isolation candidates, then the sync state does not matter.
2522 * Until a pageblock with isolation candidates is found, keep the
2523 * cached PFNs in sync to avoid revisiting the same blocks.
2524 */
2525 update_cached = !sync &&
2526 cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
2527
Joonsoo Kim16c4a092015-02-11 15:27:01 -08002528 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
2529 cc->free_pfn, end_pfn, sync);
Robin Hsu1eeadb42022-05-13 10:19:50 +08002530 trace_android_vh_mm_compaction_begin(cc, &vendor_ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08002531
Minchan Kim361a2a22021-05-04 18:36:57 -07002532 /* lru_add_drain_all could be expensive with involving other CPUs */
2533 lru_add_drain();
Mel Gorman748446b2010-05-24 14:32:27 -07002534
Mel Gorman40cacbc2019-03-05 15:44:36 -08002535 while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07002536 int err;
Yanfei Xu19d3cf92020-12-14 19:12:39 -08002537 unsigned long iteration_start_pfn = cc->migrate_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07002538
Mel Gorman804d3122019-03-05 15:45:07 -08002539 /*
2540 * Avoid multiple rescans which can happen if a page cannot be
2541 * isolated (dirty/writeback in async mode) or if the migrated
2542 * pages are being allocated before the pageblock is cleared.
2543 * The first rescan will capture the entire pageblock for
2544 * migration. If it fails, it'll be marked skip and scanning
2545 * will proceed as normal.
2546 */
2547 cc->rescan = false;
2548 if (pageblock_start_pfn(last_migrated_pfn) ==
Yanfei Xu19d3cf92020-12-14 19:12:39 -08002549 pageblock_start_pfn(iteration_start_pfn)) {
Mel Gorman804d3122019-03-05 15:45:07 -08002550 cc->rescan = true;
2551 }
2552
Minchan Kima934b9232023-01-12 09:52:49 -08002553 switch (isolate_migratepages(&cc_ext)) {
Mel Gormanf9e35b32011-06-15 15:08:52 -07002554 case ISOLATE_ABORT:
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08002555 ret = COMPACT_CONTENDED;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08002556 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07002557 cc->nr_migratepages = 0;
Minchan Kima934b9232023-01-12 09:52:49 -08002558 cc_ext.nr_migrate_file_pages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07002559 goto out;
2560 case ISOLATE_NONE:
Mel Gorman8854c552019-03-05 15:45:18 -08002561 if (update_cached) {
2562 cc->zone->compact_cached_migrate_pfn[1] =
2563 cc->zone->compact_cached_migrate_pfn[0];
2564 }
2565
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002566 /*
2567 * We haven't isolated and migrated anything, but
2568 * there might still be unflushed migrations from
2569 * previous cc->order aligned block.
2570 */
2571 goto check_drain;
Mel Gormanf9e35b32011-06-15 15:08:52 -07002572 case ISOLATE_SUCCESS:
Mel Gorman8854c552019-03-05 15:45:18 -08002573 update_cached = false;
Yanfei Xu19d3cf92020-12-14 19:12:39 -08002574 last_migrated_pfn = iteration_start_pfn;
Mel Gormanf9e35b32011-06-15 15:08:52 -07002575 }
Mel Gorman748446b2010-05-24 14:32:27 -07002576
David Rientjesd53aea32014-06-04 16:08:26 -07002577 err = migrate_pages(&cc->migratepages, compaction_alloc,
Minchan Kima934b9232023-01-12 09:52:49 -08002578 compaction_free, (unsigned long)&cc_ext, cc->mode,
Yang Shi5ac95882021-09-02 14:59:13 -07002579 MR_COMPACTION, NULL);
Mel Gorman748446b2010-05-24 14:32:27 -07002580
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07002581 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
2582 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07002583
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07002584 /* All pages were either migrated or will be released */
2585 cc->nr_migratepages = 0;
Minchan Kima934b9232023-01-12 09:52:49 -08002586 cc_ext.nr_migrate_file_pages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07002587 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08002588 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08002589 /*
2590 * migrate_pages() may return -ENOMEM when scanners meet
2591 * and we want compact_finished() to detect it
2592 */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07002593 if (err == -ENOMEM && !compact_scanners_met(cc)) {
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08002594 ret = COMPACT_CONTENDED;
David Rientjes4bf2bba2012-07-11 14:02:13 -07002595 goto out;
2596 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07002597 /*
2598 * We failed to migrate at least one page in the current
2599 * order-aligned block, so skip the rest of it.
2600 */
2601 if (cc->direct_compaction &&
2602 (cc->mode == MIGRATE_ASYNC)) {
2603 cc->migrate_pfn = block_end_pfn(
2604 cc->migrate_pfn - 1, cc->order);
2605 /* Draining pcplists is useless in this case */
Mel Gorman566e54e2019-03-05 15:44:32 -08002606 last_migrated_pfn = 0;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07002607 }
Mel Gorman748446b2010-05-24 14:32:27 -07002608 }
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002609
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002610check_drain:
2611 /*
2612 * Has the migration scanner moved away from the previous
2613 * cc->order aligned block where we migrated from? If yes,
2614 * flush the pages that were freed, so that they can merge and
2615 * compact_finished() can detect immediately if allocation
2616 * would succeed.
2617 */
Mel Gorman566e54e2019-03-05 15:44:32 -08002618 if (cc->order > 0 && last_migrated_pfn) {
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002619 unsigned long current_block_start =
Vlastimil Babka06b66402016-05-19 17:11:48 -07002620 block_start_pfn(cc->migrate_pfn, cc->order);
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002621
Mel Gorman566e54e2019-03-05 15:44:32 -08002622 if (last_migrated_pfn < current_block_start) {
Ingo Molnarb01b2142020-05-27 22:11:15 +02002623 lru_add_drain_cpu_zone(cc->zone);
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002624 /* No more flushing until we migrate again */
Mel Gorman566e54e2019-03-05 15:44:32 -08002625 last_migrated_pfn = 0;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002626 }
2627 }
2628
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002629 /* Stop if a page has been captured */
2630 if (capc && capc->page) {
2631 ret = COMPACT_SUCCESS;
2632 break;
2633 }
Mel Gorman748446b2010-05-24 14:32:27 -07002634 }
2635
Mel Gormanf9e35b32011-06-15 15:08:52 -07002636out:
Vlastimil Babka6bace092014-12-10 15:43:31 -08002637 /*
2638 * Release free pages and update where the free scanner should restart,
2639 * so we don't leave any returned pages behind in the next attempt.
2640 */
2641 if (cc->nr_freepages > 0) {
2642 unsigned long free_pfn = release_freepages(&cc->freepages);
2643
2644 cc->nr_freepages = 0;
2645 VM_BUG_ON(free_pfn == 0);
2646 /* The cached pfn is always the first in a pageblock */
Vlastimil Babka06b66402016-05-19 17:11:48 -07002647 free_pfn = pageblock_start_pfn(free_pfn);
Vlastimil Babka6bace092014-12-10 15:43:31 -08002648 /*
2649 * Only go back, not forward. The cached pfn might have been
2650 * already reset to zone end in compact_finished()
2651 */
Mel Gorman40cacbc2019-03-05 15:44:36 -08002652 if (free_pfn > cc->zone->compact_cached_free_pfn)
2653 cc->zone->compact_cached_free_pfn = free_pfn;
Vlastimil Babka6bace092014-12-10 15:43:31 -08002654 }
Mel Gorman748446b2010-05-24 14:32:27 -07002655
Tao Zeng54f845e2022-01-26 16:04:08 +08002656#ifdef CONFIG_AMLOGIC_CMA
2657 __clear_bit(FORBID_TO_CMA_BIT, &cc->total_migrate_scanned);
2658#endif
David Rientjes7f354a52017-02-22 15:44:50 -08002659 count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
2660 count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
2661
Robin Hsu1eeadb42022-05-13 10:19:50 +08002662 trace_android_vh_mm_compaction_end(cc, vendor_ret);
Joonsoo Kim16c4a092015-02-11 15:27:01 -08002663 trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
2664 cc->free_pfn, end_pfn, sync, ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08002665
Mel Gorman748446b2010-05-24 14:32:27 -07002666 return ret;
2667}
Mel Gorman76ab0f52010-05-24 14:32:28 -07002668
Michal Hockoea7ab982016-05-20 16:56:38 -07002669static enum compact_result compact_zone_order(struct zone *zone, int order,
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002670 gfp_t gfp_mask, enum compact_priority prio,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002671 unsigned int alloc_flags, int highest_zoneidx,
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002672 struct page **capture)
Mel Gorman56de7262010-05-24 14:32:30 -07002673{
Michal Hockoea7ab982016-05-20 16:56:38 -07002674 enum compact_result ret;
Mel Gorman56de7262010-05-24 14:32:30 -07002675 struct compact_control cc = {
Mel Gorman56de7262010-05-24 14:32:30 -07002676 .order = order,
Mel Gormandbe2d4e2019-03-05 15:45:31 -08002677 .search_order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07002678 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07002679 .zone = zone,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002680 .mode = (prio == COMPACT_PRIO_ASYNC) ?
2681 MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002682 .alloc_flags = alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002683 .highest_zoneidx = highest_zoneidx,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002684 .direct_compaction = true,
Vlastimil Babkaa8e025e2016-10-07 16:57:47 -07002685 .whole_zone = (prio == MIN_COMPACT_PRIORITY),
Vlastimil Babka9f7e3382016-10-07 17:00:37 -07002686 .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
2687 .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
Mel Gorman56de7262010-05-24 14:32:30 -07002688 };
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002689 struct capture_control capc = {
2690 .cc = &cc,
2691 .page = NULL,
2692 };
2693
Vlastimil Babkab9e20f02020-06-25 20:29:24 -07002694 /*
2695 * Make sure the structs are really initialized before we expose the
2696 * capture control, in case we are interrupted and the interrupt handler
2697 * frees a page.
2698 */
2699 barrier();
2700 WRITE_ONCE(current->capture_control, &capc);
Mel Gorman56de7262010-05-24 14:32:30 -07002701
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002702 ret = compact_zone(&cc, &capc);
Shaohua Lie64c5232012-10-08 16:32:27 -07002703
2704 VM_BUG_ON(!list_empty(&cc.freepages));
2705 VM_BUG_ON(!list_empty(&cc.migratepages));
2706
Vlastimil Babkab9e20f02020-06-25 20:29:24 -07002707 /*
2708 * Make sure we hide capture control first before we read the captured
2709 * page pointer, otherwise an interrupt could free and capture a page
2710 * and we would leak it.
2711 */
2712 WRITE_ONCE(current->capture_control, NULL);
2713 *capture = READ_ONCE(capc.page);
Charan Teja Reddy06dac2f2021-05-04 18:36:51 -07002714 /*
2715 * Technically, it is also possible that compaction is skipped but
2716 * the page is still captured out of luck(IRQ came and freed the page).
2717 * Returning COMPACT_SUCCESS in such cases helps in properly accounting
2718 * the COMPACT[STALL|FAIL] when compaction is skipped.
2719 */
2720 if (*capture)
2721 ret = COMPACT_SUCCESS;
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002722
Shaohua Lie64c5232012-10-08 16:32:27 -07002723 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07002724}
2725
Mel Gorman5e771902010-05-24 14:32:31 -07002726int sysctl_extfrag_threshold = 500;
2727
Mel Gorman56de7262010-05-24 14:32:30 -07002728/**
2729 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
Mel Gorman56de7262010-05-24 14:32:30 -07002730 * @gfp_mask: The GFP mask of the current allocation
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08002731 * @order: The order of the current allocation
2732 * @alloc_flags: The allocation flags of the current allocation
2733 * @ac: The context of current allocation
Yang Shi112d2d22018-01-31 16:20:23 -08002734 * @prio: Determines how hard direct compaction should try to succeed
Vlastimil Babka64675522020-04-01 21:10:35 -07002735 * @capture: Pointer to free page created by compaction will be stored here
Mel Gorman56de7262010-05-24 14:32:30 -07002736 *
2737 * This is the main entry point for direct page compaction.
2738 */
Michal Hockoea7ab982016-05-20 16:56:38 -07002739enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07002740 unsigned int alloc_flags, const struct alloc_context *ac,
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002741 enum compact_priority prio, struct page **capture)
Mel Gorman56de7262010-05-24 14:32:30 -07002742{
Mel Gorman56de7262010-05-24 14:32:30 -07002743 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07002744 struct zoneref *z;
2745 struct zone *zone;
Michal Hocko1d4746d2016-05-20 16:56:44 -07002746 enum compact_result rc = COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07002747
Michal Hocko73e64c52016-12-14 15:04:07 -08002748 /*
2749 * Check if the GFP flags allow compaction - GFP_NOIO is really
2750 * tricky context because the migration might require IO
2751 */
2752 if (!may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07002753 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07002754
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002755 trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
Joonsoo Kim837d0262015-02-11 15:27:06 -08002756
Mel Gorman56de7262010-05-24 14:32:30 -07002757 /* Compact each zone in the list */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002758 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2759 ac->highest_zoneidx, ac->nodemask) {
Michal Hockoea7ab982016-05-20 16:56:38 -07002760 enum compact_result status;
Mel Gorman56de7262010-05-24 14:32:30 -07002761
Vlastimil Babkaa8e025e2016-10-07 16:57:47 -07002762 if (prio > MIN_COMPACT_PRIORITY
2763 && compaction_deferred(zone, order)) {
Michal Hocko1d4746d2016-05-20 16:56:44 -07002764 rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
Vlastimil Babka53853e22014-10-09 15:27:02 -07002765 continue;
Michal Hocko1d4746d2016-05-20 16:56:44 -07002766 }
Vlastimil Babka53853e22014-10-09 15:27:02 -07002767
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002768 status = compact_zone_order(zone, order, gfp_mask, prio,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002769 alloc_flags, ac->highest_zoneidx, capture);
Mel Gorman56de7262010-05-24 14:32:30 -07002770 rc = max(status, rc);
2771
Vlastimil Babka7ceb0092016-10-07 16:57:44 -07002772 /* The allocation should succeed, stop compacting */
2773 if (status == COMPACT_SUCCESS) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07002774 /*
2775 * We think the allocation will succeed in this zone,
2776 * but it is not certain, hence the false. The caller
2777 * will repeat this with true if allocation indeed
2778 * succeeds in this zone.
2779 */
2780 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002781
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002782 break;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002783 }
2784
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002785 if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002786 status == COMPACT_PARTIAL_SKIPPED))
Vlastimil Babka53853e22014-10-09 15:27:02 -07002787 /*
2788 * We think that allocation won't succeed in this zone
2789 * so we defer compaction there. If it ends up
2790 * succeeding after all, it will be reset.
2791 */
2792 defer_compaction(zone, order);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002793
2794 /*
2795 * We might have stopped compacting due to need_resched() in
2796 * async compaction, or due to a fatal signal detected. In that
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002797 * case do not try further zones
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002798 */
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002799 if ((prio == COMPACT_PRIO_ASYNC && need_resched())
2800 || fatal_signal_pending(current))
2801 break;
Mel Gorman56de7262010-05-24 14:32:30 -07002802 }
2803
2804 return rc;
2805}
2806
Nitin Guptafacdaa92020-08-11 18:31:00 -07002807/*
2808 * Compact all zones within a node till each zone's fragmentation score
2809 * reaches within proactive compaction thresholds (as determined by the
2810 * proactiveness tunable).
2811 *
2812 * It is possible that the function returns before reaching score targets
2813 * due to various back-off conditions, such as, contention on per-node or
2814 * per-zone locks.
2815 */
2816static void proactive_compact_node(pg_data_t *pgdat)
2817{
2818 int zoneid;
2819 struct zone *zone;
2820 struct compact_control cc = {
2821 .order = -1,
2822 .mode = MIGRATE_SYNC_LIGHT,
2823 .ignore_skip_hint = true,
2824 .whole_zone = true,
2825 .gfp_mask = GFP_KERNEL,
2826 .proactive_compaction = true,
2827 };
2828
2829 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2830 zone = &pgdat->node_zones[zoneid];
2831 if (!populated_zone(zone))
2832 continue;
2833
2834 cc.zone = zone;
2835
2836 compact_zone(&cc, NULL);
2837
2838 VM_BUG_ON(!list_empty(&cc.freepages));
2839 VM_BUG_ON(!list_empty(&cc.migratepages));
2840 }
2841}
Mel Gorman56de7262010-05-24 14:32:30 -07002842
Mel Gorman76ab0f52010-05-24 14:32:28 -07002843/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08002844static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07002845{
Vlastimil Babka791cae92016-10-07 16:57:38 -07002846 pg_data_t *pgdat = NODE_DATA(nid);
2847 int zoneid;
2848 struct zone *zone;
Rik van Riel7be62de2012-03-21 16:33:52 -07002849 struct compact_control cc = {
2850 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07002851 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07002852 .ignore_skip_hint = true,
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002853 .whole_zone = true,
Michal Hocko73e64c52016-12-14 15:04:07 -08002854 .gfp_mask = GFP_KERNEL,
Rik van Riel7be62de2012-03-21 16:33:52 -07002855 };
2856
Vlastimil Babka791cae92016-10-07 16:57:38 -07002857
2858 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2859
2860 zone = &pgdat->node_zones[zoneid];
2861 if (!populated_zone(zone))
2862 continue;
2863
Vlastimil Babka791cae92016-10-07 16:57:38 -07002864 cc.zone = zone;
Vlastimil Babka791cae92016-10-07 16:57:38 -07002865
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002866 compact_zone(&cc, NULL);
Vlastimil Babka791cae92016-10-07 16:57:38 -07002867
2868 VM_BUG_ON(!list_empty(&cc.freepages));
2869 VM_BUG_ON(!list_empty(&cc.migratepages));
2870 }
Rik van Riel7be62de2012-03-21 16:33:52 -07002871}
2872
Mel Gorman76ab0f52010-05-24 14:32:28 -07002873/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08002874static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07002875{
2876 int nid;
2877
Hugh Dickins8575ec22012-03-21 16:33:53 -07002878 /* Flush pending updates to the LRU lists */
2879 lru_add_drain_all();
2880
Mel Gorman76ab0f52010-05-24 14:32:28 -07002881 for_each_online_node(nid)
2882 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07002883}
2884
Yaowei Baifec4eb22016-01-14 15:20:09 -08002885/*
Nitin Guptafacdaa92020-08-11 18:31:00 -07002886 * Tunable for proactive compaction. It determines how
2887 * aggressively the kernel should compact memory in the
2888 * background. It takes values in the range [0, 100].
2889 */
Nitin Guptad34c0a72020-08-11 18:31:07 -07002890unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
Nitin Guptafacdaa92020-08-11 18:31:00 -07002891
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07002892int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
2893 void *buffer, size_t *length, loff_t *ppos)
2894{
2895 int rc, nid;
2896
2897 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
2898 if (rc)
2899 return rc;
2900
2901 if (write && sysctl_compaction_proactiveness) {
2902 for_each_online_node(nid) {
2903 pg_data_t *pgdat = NODE_DATA(nid);
2904
2905 if (pgdat->proactive_compact_trigger)
2906 continue;
2907
2908 pgdat->proactive_compact_trigger = true;
2909 wake_up_interruptible(&pgdat->kcompactd_wait);
2910 }
2911 }
2912
2913 return 0;
2914}
2915
Nitin Guptafacdaa92020-08-11 18:31:00 -07002916/*
Yaowei Baifec4eb22016-01-14 15:20:09 -08002917 * This is the entry point for compacting all nodes via
2918 * /proc/sys/vm/compact_memory
2919 */
Mel Gorman76ab0f52010-05-24 14:32:28 -07002920int sysctl_compaction_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02002921 void *buffer, size_t *length, loff_t *ppos)
Mel Gorman76ab0f52010-05-24 14:32:28 -07002922{
2923 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08002924 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07002925
2926 return 0;
2927}
Mel Gormaned4a6d72010-05-24 14:32:29 -07002928
2929#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
YueHaibing17adb232021-06-30 18:50:48 -07002930static ssize_t compact_store(struct device *dev,
2931 struct device_attribute *attr,
2932 const char *buf, size_t count)
Mel Gormaned4a6d72010-05-24 14:32:29 -07002933{
Hugh Dickins8575ec22012-03-21 16:33:53 -07002934 int nid = dev->id;
2935
2936 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
2937 /* Flush pending updates to the LRU lists */
2938 lru_add_drain_all();
2939
2940 compact_node(nid);
2941 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07002942
2943 return count;
2944}
YueHaibing17adb232021-06-30 18:50:48 -07002945static DEVICE_ATTR_WO(compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07002946
2947int compaction_register_node(struct node *node)
2948{
Kay Sievers10fbcf42011-12-21 14:48:43 -08002949 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07002950}
2951
2952void compaction_unregister_node(struct node *node)
2953{
Kay Sievers10fbcf42011-12-21 14:48:43 -08002954 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07002955}
2956#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002957
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002958static inline bool kcompactd_work_requested(pg_data_t *pgdat)
2959{
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07002960 return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
2961 pgdat->proactive_compact_trigger;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002962}
2963
2964static bool kcompactd_node_suitable(pg_data_t *pgdat)
2965{
2966 int zoneid;
2967 struct zone *zone;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002968 enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002969
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002970 for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002971 zone = &pgdat->node_zones[zoneid];
2972
2973 if (!populated_zone(zone))
2974 continue;
2975
2976 if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002977 highest_zoneidx) == COMPACT_CONTINUE)
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002978 return true;
2979 }
2980
2981 return false;
2982}
2983
2984static void kcompactd_do_work(pg_data_t *pgdat)
2985{
2986 /*
2987 * With no special task, compact all zones so that a page of requested
2988 * order is allocatable.
2989 */
2990 int zoneid;
2991 struct zone *zone;
2992 struct compact_control cc = {
2993 .order = pgdat->kcompactd_max_order,
Mel Gormandbe2d4e2019-03-05 15:45:31 -08002994 .search_order = pgdat->kcompactd_max_order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002995 .highest_zoneidx = pgdat->kcompactd_highest_zoneidx,
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002996 .mode = MIGRATE_SYNC_LIGHT,
David Rientjesa0647dc2017-11-17 15:26:27 -08002997 .ignore_skip_hint = false,
Michal Hocko73e64c52016-12-14 15:04:07 -08002998 .gfp_mask = GFP_KERNEL,
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002999 };
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003000 trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003001 cc.highest_zoneidx);
David Rientjes7f354a52017-02-22 15:44:50 -08003002 count_compact_event(KCOMPACTD_WAKE);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003003
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003004 for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003005 int status;
3006
3007 zone = &pgdat->node_zones[zoneid];
3008 if (!populated_zone(zone))
3009 continue;
3010
3011 if (compaction_deferred(zone, cc.order))
3012 continue;
3013
3014 if (compaction_suitable(zone, cc.order, 0, zoneid) !=
3015 COMPACT_CONTINUE)
3016 continue;
3017
Vlastimil Babka172400c2016-05-05 16:22:32 -07003018 if (kthread_should_stop())
3019 return;
Yafang Shaoa94b5252019-09-23 15:36:54 -07003020
3021 cc.zone = zone;
Mel Gorman5e1f0f02019-03-05 15:45:41 -08003022 status = compact_zone(&cc, NULL);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003023
Vlastimil Babka7ceb0092016-10-07 16:57:44 -07003024 if (status == COMPACT_SUCCESS) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003025 compaction_defer_reset(zone, cc.order, false);
Michal Hockoc8f7de02016-05-20 16:56:47 -07003026 } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003027 /*
David Rientjesbc3106b2018-04-05 16:24:02 -07003028 * Buddy pages may become stranded on pcps that could
3029 * otherwise coalesce on the zone's free area for
3030 * order >= cc.order. This is ratelimited by the
3031 * upcoming deferral.
3032 */
3033 drain_all_pages(zone);
3034
3035 /*
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003036 * We use sync migration mode here, so we defer like
3037 * sync direct compaction does.
3038 */
3039 defer_compaction(zone, cc.order);
3040 }
3041
David Rientjes7f354a52017-02-22 15:44:50 -08003042 count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
3043 cc.total_migrate_scanned);
3044 count_compact_events(KCOMPACTD_FREE_SCANNED,
3045 cc.total_free_scanned);
3046
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003047 VM_BUG_ON(!list_empty(&cc.freepages));
3048 VM_BUG_ON(!list_empty(&cc.migratepages));
3049 }
3050
3051 /*
3052 * Regardless of success, we are done until woken up next. But remember
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003053 * the requested order/highest_zoneidx in case it was higher/tighter
3054 * than our current ones
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003055 */
3056 if (pgdat->kcompactd_max_order <= cc.order)
3057 pgdat->kcompactd_max_order = 0;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003058 if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx)
3059 pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003060}
3061
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003062void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx)
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003063{
3064 if (!order)
3065 return;
3066
3067 if (pgdat->kcompactd_max_order < order)
3068 pgdat->kcompactd_max_order = order;
3069
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003070 if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx)
3071 pgdat->kcompactd_highest_zoneidx = highest_zoneidx;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003072
Davidlohr Bueso68186002017-10-03 16:15:03 -07003073 /*
3074 * Pairs with implicit barrier in wait_event_freezable()
3075 * such that wakeups are not missed.
3076 */
3077 if (!wq_has_sleeper(&pgdat->kcompactd_wait))
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003078 return;
3079
3080 if (!kcompactd_node_suitable(pgdat))
3081 return;
3082
3083 trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003084 highest_zoneidx);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003085 wake_up_interruptible(&pgdat->kcompactd_wait);
3086}
3087
3088/*
3089 * The background compaction daemon, started as a kernel thread
3090 * from the init process.
3091 */
3092static int kcompactd(void *p)
3093{
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07003094 pg_data_t *pgdat = (pg_data_t *)p;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003095 struct task_struct *tsk = current;
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07003096 long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC);
3097 long timeout = default_timeout;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003098
3099 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
3100
3101 if (!cpumask_empty(cpumask))
3102 set_cpus_allowed_ptr(tsk, cpumask);
3103
3104 set_freezable();
3105
3106 pgdat->kcompactd_max_order = 0;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003107 pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003108
3109 while (!kthread_should_stop()) {
Johannes Weinereb414682018-10-26 15:06:27 -07003110 unsigned long pflags;
3111
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07003112 /*
3113 * Avoid the unnecessary wakeup for proactive compaction
3114 * when it is disabled.
3115 */
3116 if (!sysctl_compaction_proactiveness)
3117 timeout = MAX_SCHEDULE_TIMEOUT;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003118 trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
Nitin Guptafacdaa92020-08-11 18:31:00 -07003119 if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07003120 kcompactd_work_requested(pgdat), timeout) &&
3121 !pgdat->proactive_compact_trigger) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003122
Nitin Guptafacdaa92020-08-11 18:31:00 -07003123 psi_memstall_enter(&pflags);
3124 kcompactd_do_work(pgdat);
3125 psi_memstall_leave(&pflags);
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07003126 /*
3127 * Reset the timeout value. The defer timeout from
3128 * proactive compaction is lost here but that is fine
3129 * as the condition of the zone changing substantionally
3130 * then carrying on with the previous defer interval is
3131 * not useful.
3132 */
3133 timeout = default_timeout;
Nitin Guptafacdaa92020-08-11 18:31:00 -07003134 continue;
3135 }
3136
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07003137 /*
3138 * Start the proactive work with default timeout. Based
3139 * on the fragmentation score, this timeout is updated.
3140 */
3141 timeout = default_timeout;
Nitin Guptafacdaa92020-08-11 18:31:00 -07003142 if (should_proactive_compact_node(pgdat)) {
3143 unsigned int prev_score, score;
3144
Nitin Guptafacdaa92020-08-11 18:31:00 -07003145 prev_score = fragmentation_score_node(pgdat);
3146 proactive_compact_node(pgdat);
3147 score = fragmentation_score_node(pgdat);
3148 /*
3149 * Defer proactive compaction if the fragmentation
3150 * score did not go down i.e. no progress made.
3151 */
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07003152 if (unlikely(score >= prev_score))
3153 timeout =
3154 default_timeout << COMPACT_MAX_DEFER_SHIFT;
Nitin Guptafacdaa92020-08-11 18:31:00 -07003155 }
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07003156 if (unlikely(pgdat->proactive_compact_trigger))
3157 pgdat->proactive_compact_trigger = false;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003158 }
3159
3160 return 0;
3161}
3162
3163/*
3164 * This kcompactd start function will be called by init and node-hot-add.
3165 * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
3166 */
3167int kcompactd_run(int nid)
3168{
3169 pg_data_t *pgdat = NODE_DATA(nid);
3170 int ret = 0;
3171
3172 if (pgdat->kcompactd)
3173 return 0;
3174
3175 pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
3176 if (IS_ERR(pgdat->kcompactd)) {
3177 pr_err("Failed to start kcompactd on node %d\n", nid);
3178 ret = PTR_ERR(pgdat->kcompactd);
3179 pgdat->kcompactd = NULL;
3180 }
3181 return ret;
3182}
3183
3184/*
3185 * Called by memory hotplug when all memory in a node is offlined. Caller must
3186 * hold mem_hotplug_begin/end().
3187 */
3188void kcompactd_stop(int nid)
3189{
3190 struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
3191
3192 if (kcompactd) {
3193 kthread_stop(kcompactd);
3194 NODE_DATA(nid)->kcompactd = NULL;
3195 }
3196}
3197
3198/*
3199 * It's optimal to keep kcompactd on the same CPUs as their memory, but
3200 * not required for correctness. So if the last cpu in a node goes
3201 * away, we get changed to run anywhere: as the first one comes back,
3202 * restore their cpu bindings.
3203 */
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003204static int kcompactd_cpu_online(unsigned int cpu)
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003205{
3206 int nid;
3207
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003208 for_each_node_state(nid, N_MEMORY) {
3209 pg_data_t *pgdat = NODE_DATA(nid);
3210 const struct cpumask *mask;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003211
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003212 mask = cpumask_of_node(pgdat->node_id);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003213
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003214 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
3215 /* One of our CPUs online: restore mask */
3216 set_cpus_allowed_ptr(pgdat->kcompactd, mask);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003217 }
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003218 return 0;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003219}
3220
3221static int __init kcompactd_init(void)
3222{
3223 int nid;
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003224 int ret;
3225
3226 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
3227 "mm/compaction:online",
3228 kcompactd_cpu_online, NULL);
3229 if (ret < 0) {
3230 pr_err("kcompactd: failed to register hotplug callbacks.\n");
3231 return ret;
3232 }
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003233
3234 for_each_node_state(nid, N_MEMORY)
3235 kcompactd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003236 return 0;
3237}
3238subsys_initcall(kcompactd_init)
3239
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01003240#endif /* CONFIG_COMPACTION */