blob: 2d05f1a97c1afd0d7e43568cd83659643572514c [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
Mel Gorman748446b2010-05-24 14:32:27 -070030
Minchan Kim010fc292012-12-20 15:05:06 -080031#ifdef CONFIG_COMPACTION
32static inline void count_compact_event(enum vm_event_item item)
33{
34 count_vm_event(item);
35}
36
37static inline void count_compact_events(enum vm_event_item item, long delta)
38{
39 count_vm_events(item, delta);
40}
41#else
42#define count_compact_event(item) do { } while (0)
43#define count_compact_events(item, delta) do { } while (0)
44#endif
45
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010046#if defined CONFIG_COMPACTION || defined CONFIG_CMA
47
Mel Gormanb7aba692011-01-13 15:45:54 -080048#define CREATE_TRACE_POINTS
49#include <trace/events/compaction.h>
Robin Hsu1eeadb42022-05-13 10:19:50 +080050#undef CREATE_TRACE_POINTS
51#include <trace/hooks/mm.h>
Mel Gormanb7aba692011-01-13 15:45:54 -080052
Vlastimil Babka06b66402016-05-19 17:11:48 -070053#define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order))
54#define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order))
55#define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order)
56#define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order)
57
Nitin Guptafacdaa92020-08-11 18:31:00 -070058/*
59 * Fragmentation score check interval for proactive compaction purposes.
60 */
Nitin Guptad34c0a72020-08-11 18:31:07 -070061static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500;
Nitin Guptafacdaa92020-08-11 18:31:00 -070062
63/*
64 * Page order with-respect-to which proactive compaction
65 * calculates external fragmentation, which is used as
66 * the "fragmentation score" of a node/zone.
67 */
68#if defined CONFIG_TRANSPARENT_HUGEPAGE
69#define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER
Nitin Gupta25788732020-08-11 18:31:04 -070070#elif defined CONFIG_HUGETLBFS
Nitin Guptafacdaa92020-08-11 18:31:00 -070071#define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER
72#else
73#define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT)
74#endif
75
Mel Gorman748446b2010-05-24 14:32:27 -070076static unsigned long release_freepages(struct list_head *freelist)
77{
78 struct page *page, *next;
Vlastimil Babka6bace092014-12-10 15:43:31 -080079 unsigned long high_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -070080
81 list_for_each_entry_safe(page, next, freelist, lru) {
Vlastimil Babka6bace092014-12-10 15:43:31 -080082 unsigned long pfn = page_to_pfn(page);
Mel Gorman748446b2010-05-24 14:32:27 -070083 list_del(&page->lru);
84 __free_page(page);
Vlastimil Babka6bace092014-12-10 15:43:31 -080085 if (pfn > high_pfn)
86 high_pfn = pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070087 }
88
Vlastimil Babka6bace092014-12-10 15:43:31 -080089 return high_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -070090}
91
Mel Gorman4469ab92019-03-05 15:44:39 -080092static void split_map_pages(struct list_head *list)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010093{
Joonsoo Kim66c64222016-07-26 15:23:40 -070094 unsigned int i, order, nr_pages;
95 struct page *page, *next;
96 LIST_HEAD(tmp_list);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +010097
Joonsoo Kim66c64222016-07-26 15:23:40 -070098 list_for_each_entry_safe(page, next, list, lru) {
99 list_del(&page->lru);
100
101 order = page_private(page);
102 nr_pages = 1 << order;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700103
Joonsoo Kim46f24fd2016-07-26 15:23:58 -0700104 post_alloc_hook(page, order, __GFP_MOVABLE);
Joonsoo Kim66c64222016-07-26 15:23:40 -0700105 if (order)
106 split_page(page, order);
107
108 for (i = 0; i < nr_pages; i++) {
109 list_add(&page->lru, &tmp_list);
110 page++;
111 }
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100112 }
Joonsoo Kim66c64222016-07-26 15:23:40 -0700113
114 list_splice(&tmp_list, list);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100115}
116
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700117#ifdef CONFIG_COMPACTION
Joonsoo Kim24e27162015-02-11 15:27:09 -0800118
Minchan Kimbda807d2016-07-26 15:23:05 -0700119int PageMovable(struct page *page)
120{
121 struct address_space *mapping;
122
123 VM_BUG_ON_PAGE(!PageLocked(page), page);
124 if (!__PageMovable(page))
125 return 0;
126
127 mapping = page_mapping(page);
128 if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
129 return 1;
130
131 return 0;
132}
133EXPORT_SYMBOL(PageMovable);
134
135void __SetPageMovable(struct page *page, struct address_space *mapping)
136{
137 VM_BUG_ON_PAGE(!PageLocked(page), page);
138 VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
139 page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
140}
141EXPORT_SYMBOL(__SetPageMovable);
142
143void __ClearPageMovable(struct page *page)
144{
Minchan Kimbda807d2016-07-26 15:23:05 -0700145 VM_BUG_ON_PAGE(!PageMovable(page), page);
146 /*
147 * Clear registered address_space val with keeping PAGE_MAPPING_MOVABLE
148 * flag so that VM can catch up released page by driver after isolation.
149 * With it, VM migration doesn't try to put it back.
150 */
151 page->mapping = (void *)((unsigned long)page->mapping &
152 PAGE_MAPPING_MOVABLE);
153}
154EXPORT_SYMBOL(__ClearPageMovable);
155
Joonsoo Kim24e27162015-02-11 15:27:09 -0800156/* Do not skip compaction more than 64 times */
157#define COMPACT_MAX_DEFER_SHIFT 6
158
159/*
160 * Compaction is deferred when compaction fails to result in a page
Alex Shi860b3272020-08-11 18:31:10 -0700161 * allocation success. 1 << compact_defer_shift, compactions are skipped up
Joonsoo Kim24e27162015-02-11 15:27:09 -0800162 * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
163 */
Hui Su2271b012020-12-14 19:12:46 -0800164static void defer_compaction(struct zone *zone, int order)
Joonsoo Kim24e27162015-02-11 15:27:09 -0800165{
166 zone->compact_considered = 0;
167 zone->compact_defer_shift++;
168
169 if (order < zone->compact_order_failed)
170 zone->compact_order_failed = order;
171
172 if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
173 zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
174
175 trace_mm_compaction_defer_compaction(zone, order);
176}
177
178/* Returns true if compaction should be skipped this time */
Hui Su2271b012020-12-14 19:12:46 -0800179static bool compaction_deferred(struct zone *zone, int order)
Joonsoo Kim24e27162015-02-11 15:27:09 -0800180{
181 unsigned long defer_limit = 1UL << zone->compact_defer_shift;
182
183 if (order < zone->compact_order_failed)
184 return false;
185
186 /* Avoid possible overflow */
Mateusz Nosek62b35fe2020-10-13 16:56:58 -0700187 if (++zone->compact_considered >= defer_limit) {
Joonsoo Kim24e27162015-02-11 15:27:09 -0800188 zone->compact_considered = defer_limit;
Joonsoo Kim24e27162015-02-11 15:27:09 -0800189 return false;
Mateusz Nosek62b35fe2020-10-13 16:56:58 -0700190 }
Joonsoo Kim24e27162015-02-11 15:27:09 -0800191
192 trace_mm_compaction_deferred(zone, order);
193
194 return true;
195}
196
197/*
198 * Update defer tracking counters after successful compaction of given order,
199 * which means an allocation either succeeded (alloc_success == true) or is
200 * expected to succeed.
201 */
202void compaction_defer_reset(struct zone *zone, int order,
203 bool alloc_success)
204{
205 if (alloc_success) {
206 zone->compact_considered = 0;
207 zone->compact_defer_shift = 0;
208 }
209 if (order >= zone->compact_order_failed)
210 zone->compact_order_failed = order + 1;
211
212 trace_mm_compaction_defer_reset(zone, order);
213}
214
215/* Returns true if restarting compaction after many failures */
Hui Su2271b012020-12-14 19:12:46 -0800216static bool compaction_restarting(struct zone *zone, int order)
Joonsoo Kim24e27162015-02-11 15:27:09 -0800217{
218 if (order < zone->compact_order_failed)
219 return false;
220
221 return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
222 zone->compact_considered >= 1UL << zone->compact_defer_shift;
223}
224
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700225/* Returns true if the pageblock should be scanned for pages to isolate. */
226static inline bool isolation_suitable(struct compact_control *cc,
227 struct page *page)
228{
229 if (cc->ignore_skip_hint)
230 return true;
231
232 return !get_pageblock_skip(page);
233}
234
Vlastimil Babka023336412015-09-08 15:02:42 -0700235static void reset_cached_positions(struct zone *zone)
236{
237 zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
238 zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
Joonsoo Kim623446e2016-03-15 14:57:45 -0700239 zone->compact_cached_free_pfn =
Vlastimil Babka06b66402016-05-19 17:11:48 -0700240 pageblock_start_pfn(zone_end_pfn(zone) - 1);
Vlastimil Babka023336412015-09-08 15:02:42 -0700241}
242
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700243/*
Hui Su2271b012020-12-14 19:12:46 -0800244 * Compound pages of >= pageblock_order should consistently be skipped until
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800245 * released. It is always pointless to compact pages of such order (if they are
246 * migratable), and the pageblocks they occupy cannot contain any free pages.
David Rientjes21dc7e02017-11-17 15:26:30 -0800247 */
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800248static bool pageblock_skip_persistent(struct page *page)
David Rientjes21dc7e02017-11-17 15:26:30 -0800249{
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800250 if (!PageCompound(page))
David Rientjes21dc7e02017-11-17 15:26:30 -0800251 return false;
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800252
253 page = compound_head(page);
254
255 if (compound_order(page) >= pageblock_order)
256 return true;
257
258 return false;
David Rientjes21dc7e02017-11-17 15:26:30 -0800259}
260
Mel Gormane332f742019-03-05 15:45:38 -0800261static bool
262__reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
263 bool check_target)
264{
265 struct page *page = pfn_to_online_page(pfn);
Mel Gorman6b0868c2019-04-04 11:54:09 +0100266 struct page *block_page;
Mel Gormane332f742019-03-05 15:45:38 -0800267 struct page *end_page;
268 unsigned long block_pfn;
269
270 if (!page)
271 return false;
272 if (zone != page_zone(page))
273 return false;
274 if (pageblock_skip_persistent(page))
275 return false;
276
277 /*
278 * If skip is already cleared do no further checking once the
279 * restart points have been set.
280 */
281 if (check_source && check_target && !get_pageblock_skip(page))
282 return true;
283
284 /*
285 * If clearing skip for the target scanner, do not select a
286 * non-movable pageblock as the starting point.
287 */
288 if (!check_source && check_target &&
289 get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
290 return false;
291
Mel Gorman6b0868c2019-04-04 11:54:09 +0100292 /* Ensure the start of the pageblock or zone is online and valid */
293 block_pfn = pageblock_start_pfn(pfn);
Vlastimil Babkaa2e9a5a2019-10-14 14:12:07 -0700294 block_pfn = max(block_pfn, zone->zone_start_pfn);
295 block_page = pfn_to_online_page(block_pfn);
Mel Gorman6b0868c2019-04-04 11:54:09 +0100296 if (block_page) {
297 page = block_page;
298 pfn = block_pfn;
299 }
300
301 /* Ensure the end of the pageblock or zone is online and valid */
Vlastimil Babkaa2e9a5a2019-10-14 14:12:07 -0700302 block_pfn = pageblock_end_pfn(pfn) - 1;
Mel Gorman6b0868c2019-04-04 11:54:09 +0100303 block_pfn = min(block_pfn, zone_end_pfn(zone) - 1);
304 end_page = pfn_to_online_page(block_pfn);
305 if (!end_page)
306 return false;
307
Mel Gormane332f742019-03-05 15:45:38 -0800308 /*
309 * Only clear the hint if a sample indicates there is either a
310 * free page or an LRU page in the block. One or other condition
311 * is necessary for the block to be a migration source/target.
312 */
Mel Gormane332f742019-03-05 15:45:38 -0800313 do {
Mike Rapoport859a85d2021-09-07 19:54:52 -0700314 if (check_source && PageLRU(page)) {
315 clear_pageblock_skip(page);
316 return true;
317 }
Mel Gormane332f742019-03-05 15:45:38 -0800318
Mike Rapoport859a85d2021-09-07 19:54:52 -0700319 if (check_target && PageBuddy(page)) {
320 clear_pageblock_skip(page);
321 return true;
Mel Gormane332f742019-03-05 15:45:38 -0800322 }
323
324 page += (1 << PAGE_ALLOC_COSTLY_ORDER);
325 pfn += (1 << PAGE_ALLOC_COSTLY_ORDER);
Vlastimil Babkaa2e9a5a2019-10-14 14:12:07 -0700326 } while (page <= end_page);
Mel Gormane332f742019-03-05 15:45:38 -0800327
328 return false;
329}
330
David Rientjes21dc7e02017-11-17 15:26:30 -0800331/*
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700332 * This function is called to clear all cached information on pageblocks that
333 * should be skipped for page isolation when the migrate and free page scanner
334 * meet.
335 */
Mel Gorman62997022012-10-08 16:32:47 -0700336static void __reset_isolation_suitable(struct zone *zone)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700337{
Mel Gormane332f742019-03-05 15:45:38 -0800338 unsigned long migrate_pfn = zone->zone_start_pfn;
Mel Gorman6b0868c2019-04-04 11:54:09 +0100339 unsigned long free_pfn = zone_end_pfn(zone) - 1;
Mel Gormane332f742019-03-05 15:45:38 -0800340 unsigned long reset_migrate = free_pfn;
341 unsigned long reset_free = migrate_pfn;
342 bool source_set = false;
343 bool free_set = false;
344
345 if (!zone->compact_blockskip_flush)
346 return;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700347
Mel Gorman62997022012-10-08 16:32:47 -0700348 zone->compact_blockskip_flush = false;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700349
Mel Gormane332f742019-03-05 15:45:38 -0800350 /*
351 * Walk the zone and update pageblock skip information. Source looks
352 * for PageLRU while target looks for PageBuddy. When the scanner
353 * is found, both PageBuddy and PageLRU are checked as the pageblock
354 * is suitable as both source and target.
355 */
356 for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
357 free_pfn -= pageblock_nr_pages) {
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700358 cond_resched();
359
Mel Gormane332f742019-03-05 15:45:38 -0800360 /* Update the migrate PFN */
361 if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
362 migrate_pfn < reset_migrate) {
363 source_set = true;
364 reset_migrate = migrate_pfn;
365 zone->compact_init_migrate_pfn = reset_migrate;
366 zone->compact_cached_migrate_pfn[0] = reset_migrate;
367 zone->compact_cached_migrate_pfn[1] = reset_migrate;
368 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700369
Mel Gormane332f742019-03-05 15:45:38 -0800370 /* Update the free PFN */
371 if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
372 free_pfn > reset_free) {
373 free_set = true;
374 reset_free = free_pfn;
375 zone->compact_init_free_pfn = reset_free;
376 zone->compact_cached_free_pfn = reset_free;
377 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700378 }
Vlastimil Babka023336412015-09-08 15:02:42 -0700379
Mel Gormane332f742019-03-05 15:45:38 -0800380 /* Leave no distance if no suitable block was reset */
381 if (reset_migrate >= reset_free) {
382 zone->compact_cached_migrate_pfn[0] = migrate_pfn;
383 zone->compact_cached_migrate_pfn[1] = migrate_pfn;
384 zone->compact_cached_free_pfn = free_pfn;
385 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700386}
387
Mel Gorman62997022012-10-08 16:32:47 -0700388void reset_isolation_suitable(pg_data_t *pgdat)
389{
390 int zoneid;
391
392 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
393 struct zone *zone = &pgdat->node_zones[zoneid];
394 if (!populated_zone(zone))
395 continue;
396
397 /* Only flush if a full compaction finished recently */
398 if (zone->compact_blockskip_flush)
399 __reset_isolation_suitable(zone);
400 }
401}
402
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700403/*
Mel Gormane380beb2019-03-05 15:44:58 -0800404 * Sets the pageblock skip bit if it was clear. Note that this is a hint as
405 * locks are not required for read/writers. Returns true if it was already set.
406 */
407static bool test_and_set_skip(struct compact_control *cc, struct page *page,
408 unsigned long pfn)
409{
410 bool skip;
411
412 /* Do no update if skip hint is being ignored */
413 if (cc->ignore_skip_hint)
414 return false;
415
416 if (!IS_ALIGNED(pfn, pageblock_nr_pages))
417 return false;
418
419 skip = get_pageblock_skip(page);
420 if (!skip && !cc->no_set_skip_hint)
421 set_pageblock_skip(page);
422
423 return skip;
424}
425
426static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
427{
428 struct zone *zone = cc->zone;
429
430 pfn = pageblock_end_pfn(pfn);
431
432 /* Set for isolation rather than compaction */
433 if (cc->no_set_skip_hint)
434 return;
435
436 if (pfn > zone->compact_cached_migrate_pfn[0])
437 zone->compact_cached_migrate_pfn[0] = pfn;
438 if (cc->mode != MIGRATE_ASYNC &&
439 pfn > zone->compact_cached_migrate_pfn[1])
440 zone->compact_cached_migrate_pfn[1] = pfn;
441}
442
443/*
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700444 * If no pages were isolated then mark this pageblock to be skipped in the
Mel Gorman62997022012-10-08 16:32:47 -0700445 * future. The information is later cleared by __reset_isolation_suitable().
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700446 */
Mel Gormanc89511a2012-10-08 16:32:45 -0700447static void update_pageblock_skip(struct compact_control *cc,
Mel Gormand097a6f2019-03-05 15:45:28 -0800448 struct page *page, unsigned long pfn)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700449{
Mel Gormanc89511a2012-10-08 16:32:45 -0700450 struct zone *zone = cc->zone;
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800451
Vlastimil Babka2583d672017-11-17 15:26:38 -0800452 if (cc->no_set_skip_hint)
Joonsoo Kim6815bf32013-12-18 17:08:52 -0800453 return;
454
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700455 if (!page)
456 return;
457
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700458 set_pageblock_skip(page);
Mel Gormanc89511a2012-10-08 16:32:45 -0700459
David Rientjes35979ef2014-06-04 16:08:27 -0700460 /* Update where async and sync compaction should restart */
Mel Gormane380beb2019-03-05 15:44:58 -0800461 if (pfn < zone->compact_cached_free_pfn)
462 zone->compact_cached_free_pfn = pfn;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700463}
464#else
465static inline bool isolation_suitable(struct compact_control *cc,
466 struct page *page)
467{
468 return true;
469}
470
Vlastimil Babkab527cfe2017-11-17 15:26:34 -0800471static inline bool pageblock_skip_persistent(struct page *page)
David Rientjes21dc7e02017-11-17 15:26:30 -0800472{
473 return false;
474}
475
476static inline void update_pageblock_skip(struct compact_control *cc,
Mel Gormand097a6f2019-03-05 15:45:28 -0800477 struct page *page, unsigned long pfn)
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700478{
479}
Mel Gormane380beb2019-03-05 15:44:58 -0800480
481static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
482{
483}
484
485static bool test_and_set_skip(struct compact_control *cc, struct page *page,
486 unsigned long pfn)
487{
488 return false;
489}
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700490#endif /* CONFIG_COMPACTION */
491
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700492/*
493 * Compaction requires the taking of some coarse locks that are potentially
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800494 * very heavily contended. For async compaction, trylock and record if the
495 * lock is contended. The lock will still be acquired but compaction will
496 * abort when the current block is finished regardless of success rate.
497 * Sync compaction acquires the lock.
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700498 *
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800499 * Always returns true which makes it easier to track lock state in callers.
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700500 */
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800501static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700502 struct compact_control *cc)
Jules Irenge77337ed2020-04-06 20:08:06 -0700503 __acquires(lock)
Mel Gorman2a1402a2012-10-08 16:32:33 -0700504{
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800505 /* Track if the lock is contended in async mode */
506 if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
507 if (spin_trylock_irqsave(lock, *flags))
508 return true;
509
510 cc->contended = true;
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700511 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700512
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800513 spin_lock_irqsave(lock, *flags);
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700514 return true;
Mel Gorman2a1402a2012-10-08 16:32:33 -0700515}
516
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100517/*
Mel Gormanc67fe372012-08-21 16:16:17 -0700518 * Compaction requires the taking of some coarse locks that are potentially
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700519 * very heavily contended. The lock should be periodically unlocked to avoid
520 * having disabled IRQs for a long time, even when there is nobody waiting on
521 * the lock. It might also be that allowing the IRQs will result in
522 * need_resched() becoming true. If scheduling is needed, async compaction
523 * aborts. Sync compaction schedules.
524 * Either compaction type will also abort if a fatal signal is pending.
525 * In either case if the lock was locked, it is dropped and not regained.
Mel Gormanc67fe372012-08-21 16:16:17 -0700526 *
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700527 * Returns true if compaction should abort due to fatal signal pending, or
528 * async compaction due to need_resched()
529 * Returns false when compaction can continue (sync compaction might have
530 * scheduled)
Mel Gormanc67fe372012-08-21 16:16:17 -0700531 */
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700532static bool compact_unlock_should_abort(spinlock_t *lock,
533 unsigned long flags, bool *locked, struct compact_control *cc)
Mel Gormanc67fe372012-08-21 16:16:17 -0700534{
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700535 if (*locked) {
536 spin_unlock_irqrestore(lock, flags);
537 *locked = false;
538 }
Vlastimil Babka1f9efde2014-10-09 15:27:14 -0700539
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700540 if (fatal_signal_pending(current)) {
Vlastimil Babkac3486f52016-07-28 15:49:30 -0700541 cc->contended = true;
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700542 return true;
543 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700544
Mel Gormancf66f072019-03-05 15:45:24 -0800545 cond_resched();
Vlastimil Babkabe976572014-06-04 16:10:41 -0700546
547 return false;
548}
549
Mel Gormanc67fe372012-08-21 16:16:17 -0700550/*
Jerome Marchand9e4be472013-11-12 15:07:12 -0800551 * Isolate free pages onto a private freelist. If @strict is true, will abort
552 * returning 0 on any invalid PFNs or non-free pages inside of the pageblock
553 * (even though it may still end up isolating some pages).
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100554 */
Mel Gormanf40d1e42012-10-08 16:32:36 -0700555static unsigned long isolate_freepages_block(struct compact_control *cc,
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700556 unsigned long *start_pfn,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100557 unsigned long end_pfn,
558 struct list_head *freelist,
Mel Gorman4fca9732019-03-05 15:45:34 -0800559 unsigned int stride,
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100560 bool strict)
Mel Gorman748446b2010-05-24 14:32:27 -0700561{
Mel Gormanb7aba692011-01-13 15:45:54 -0800562 int nr_scanned = 0, total_isolated = 0;
Mel Gormand097a6f2019-03-05 15:45:28 -0800563 struct page *cursor;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700564 unsigned long flags = 0;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700565 bool locked = false;
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700566 unsigned long blockpfn = *start_pfn;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700567 unsigned int order;
Mel Gorman748446b2010-05-24 14:32:27 -0700568
Mel Gorman4fca9732019-03-05 15:45:34 -0800569 /* Strict mode is for isolation, speed is secondary */
570 if (strict)
571 stride = 1;
572
Mel Gorman748446b2010-05-24 14:32:27 -0700573 cursor = pfn_to_page(blockpfn);
574
Mel Gormanf40d1e42012-10-08 16:32:36 -0700575 /* Isolate free pages. */
Mel Gorman4fca9732019-03-05 15:45:34 -0800576 for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) {
Joonsoo Kim66c64222016-07-26 15:23:40 -0700577 int isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700578 struct page *page = cursor;
579
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700580 /*
581 * Periodically drop the lock (if held) regardless of its
582 * contention, to give chance to IRQs. Abort if fatal signal
583 * pending or async compaction detects need_resched()
584 */
585 if (!(blockpfn % SWAP_CLUSTER_MAX)
586 && compact_unlock_should_abort(&cc->zone->lock, flags,
587 &locked, cc))
588 break;
589
Mel Gormanb7aba692011-01-13 15:45:54 -0800590 nr_scanned++;
Laura Abbott2af120b2014-03-10 15:49:44 -0700591
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700592 /*
593 * For compound pages such as THP and hugetlbfs, we can save
594 * potentially a lot of iterations if we skip them at once.
595 * The check is racy, but we can consider only valid values
596 * and the only danger is skipping too much.
597 */
598 if (PageCompound(page)) {
David Rientjes21dc7e02017-11-17 15:26:30 -0800599 const unsigned int order = compound_order(page);
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700600
Vlastimil Babkad3c85ba2017-11-17 15:26:41 -0800601 if (likely(order < MAX_ORDER)) {
David Rientjes21dc7e02017-11-17 15:26:30 -0800602 blockpfn += (1UL << order) - 1;
603 cursor += (1UL << order) - 1;
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700604 }
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700605 goto isolate_fail;
606 }
607
Mel Gormanf40d1e42012-10-08 16:32:36 -0700608 if (!PageBuddy(page))
Laura Abbott2af120b2014-03-10 15:49:44 -0700609 goto isolate_fail;
Mel Gormanf40d1e42012-10-08 16:32:36 -0700610
611 /*
Vlastimil Babka69b71892014-10-09 15:27:18 -0700612 * If we already hold the lock, we can skip some rechecking.
613 * Note that if we hold the lock now, checked_pageblock was
614 * already set in some previous iteration (or strict is true),
615 * so it is correct to skip the suitable migration target
616 * recheck as well.
Mel Gormanf40d1e42012-10-08 16:32:36 -0700617 */
Vlastimil Babka69b71892014-10-09 15:27:18 -0700618 if (!locked) {
Mel Gormancb2dcaf2019-03-05 15:45:11 -0800619 locked = compact_lock_irqsave(&cc->zone->lock,
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700620 &flags, cc);
Mel Gormanf40d1e42012-10-08 16:32:36 -0700621
Vlastimil Babka69b71892014-10-09 15:27:18 -0700622 /* Recheck this is a buddy page under lock */
623 if (!PageBuddy(page))
624 goto isolate_fail;
625 }
Mel Gorman748446b2010-05-24 14:32:27 -0700626
Joonsoo Kim66c64222016-07-26 15:23:40 -0700627 /* Found a free page, will break it into order-0 pages */
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700628 order = buddy_order(page);
Joonsoo Kim66c64222016-07-26 15:23:40 -0700629 isolated = __isolate_free_page(page, order);
David Rientjesa4f04f22016-06-24 14:50:10 -0700630 if (!isolated)
631 break;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700632 set_page_private(page, order);
David Rientjesa4f04f22016-06-24 14:50:10 -0700633
Mel Gorman748446b2010-05-24 14:32:27 -0700634 total_isolated += isolated;
David Rientjesa4f04f22016-06-24 14:50:10 -0700635 cc->nr_freepages += isolated;
Joonsoo Kim66c64222016-07-26 15:23:40 -0700636 list_add_tail(&page->lru, freelist);
637
David Rientjesa4f04f22016-06-24 14:50:10 -0700638 if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
639 blockpfn += isolated;
640 break;
Mel Gorman748446b2010-05-24 14:32:27 -0700641 }
David Rientjesa4f04f22016-06-24 14:50:10 -0700642 /* Advance to the end of split page */
643 blockpfn += isolated - 1;
644 cursor += isolated - 1;
645 continue;
Laura Abbott2af120b2014-03-10 15:49:44 -0700646
647isolate_fail:
648 if (strict)
649 break;
650 else
651 continue;
652
Mel Gorman748446b2010-05-24 14:32:27 -0700653 }
654
David Rientjesa4f04f22016-06-24 14:50:10 -0700655 if (locked)
656 spin_unlock_irqrestore(&cc->zone->lock, flags);
657
Vlastimil Babka9fcd6d22015-09-08 15:02:49 -0700658 /*
659 * There is a tiny chance that we have read bogus compound_order(),
660 * so be careful to not go outside of the pageblock.
661 */
662 if (unlikely(blockpfn > end_pfn))
663 blockpfn = end_pfn;
664
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800665 trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
666 nr_scanned, total_isolated);
667
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700668 /* Record how far we have got within the block */
669 *start_pfn = blockpfn;
670
Mel Gormanf40d1e42012-10-08 16:32:36 -0700671 /*
672 * If strict isolation is requested by CMA then check that all the
673 * pages requested were isolated. If there were any failures, 0 is
674 * returned and CMA will fail.
675 */
Laura Abbott2af120b2014-03-10 15:49:44 -0700676 if (strict && blockpfn < end_pfn)
Mel Gormanf40d1e42012-10-08 16:32:36 -0700677 total_isolated = 0;
678
David Rientjes7f354a52017-02-22 15:44:50 -0800679 cc->total_free_scanned += nr_scanned;
Mel Gorman397487d2012-10-19 12:00:10 +0100680 if (total_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -0800681 count_compact_events(COMPACTISOLATED, total_isolated);
Mel Gorman748446b2010-05-24 14:32:27 -0700682 return total_isolated;
683}
684
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100685/**
686 * isolate_freepages_range() - isolate free pages.
Mike Rapoporte8b098f2018-04-05 16:24:57 -0700687 * @cc: Compaction control structure.
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100688 * @start_pfn: The first PFN to start isolating.
689 * @end_pfn: The one-past-last PFN.
690 *
691 * Non-free pages, invalid PFNs, or zone boundaries within the
692 * [start_pfn, end_pfn) range are considered errors, cause function to
693 * undo its actions and return zero.
694 *
695 * Otherwise, function returns one-past-the-last PFN of isolated page
696 * (which may be greater then end_pfn if end fell in a middle of
697 * a free page).
698 */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +0100699unsigned long
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700700isolate_freepages_range(struct compact_control *cc,
701 unsigned long start_pfn, unsigned long end_pfn)
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100702{
Joonsoo Kime1409c32016-03-15 14:57:48 -0700703 unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100704 LIST_HEAD(freelist);
705
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700706 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700707 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -0700708 if (block_start_pfn < cc->zone->zone_start_pfn)
709 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -0700710 block_end_pfn = pageblock_end_pfn(pfn);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100711
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700712 for (; pfn < end_pfn; pfn += isolated,
Joonsoo Kime1409c32016-03-15 14:57:48 -0700713 block_start_pfn = block_end_pfn,
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700714 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700715 /* Protect pfn from changing by isolate_freepages_block */
716 unsigned long isolate_start_pfn = pfn;
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700717
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100718 block_end_pfn = min(block_end_pfn, end_pfn);
719
Joonsoo Kim58420012014-11-13 15:19:07 -0800720 /*
721 * pfn could pass the block_end_pfn if isolated freepage
722 * is more than pageblock order. In this case, we adjust
723 * scanning range to right one.
724 */
725 if (pfn >= block_end_pfn) {
Vlastimil Babka06b66402016-05-19 17:11:48 -0700726 block_start_pfn = pageblock_start_pfn(pfn);
727 block_end_pfn = pageblock_end_pfn(pfn);
Joonsoo Kim58420012014-11-13 15:19:07 -0800728 block_end_pfn = min(block_end_pfn, end_pfn);
729 }
730
Joonsoo Kime1409c32016-03-15 14:57:48 -0700731 if (!pageblock_pfn_to_page(block_start_pfn,
732 block_end_pfn, cc->zone))
Vlastimil Babka7d49d882014-10-09 15:27:11 -0700733 break;
734
Vlastimil Babkae14c7202014-10-09 15:27:20 -0700735 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
Mel Gorman4fca9732019-03-05 15:45:34 -0800736 block_end_pfn, &freelist, 0, true);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100737
738 /*
739 * In strict mode, isolate_freepages_block() returns 0 if
740 * there are any holes in the block (ie. invalid PFNs or
741 * non-free pages).
742 */
743 if (!isolated)
744 break;
745
746 /*
747 * If we managed to isolate pages, it is always (1 << n) *
748 * pageblock_nr_pages for some non-negative n. (Max order
749 * page may span two pageblocks).
750 */
751 }
752
Joonsoo Kim66c64222016-07-26 15:23:40 -0700753 /* __isolate_free_page() does not map the pages */
Mel Gorman4469ab92019-03-05 15:44:39 -0800754 split_map_pages(&freelist);
Michal Nazarewicz85aa1252012-01-30 13:24:03 +0100755
756 if (pfn < end_pfn) {
757 /* Loop terminated early, cleanup. */
758 release_freepages(&freelist);
759 return 0;
760 }
761
762 /* We don't use freelists for anything. */
763 return pfn;
764}
765
Carlos Llamas061e34c2021-04-23 19:20:54 +0000766#ifdef CONFIG_COMPACTION
Charan Teja Reddyf47b8522021-02-16 13:59:45 +0530767unsigned long isolate_and_split_free_page(struct page *page,
768 struct list_head *list)
769{
770 unsigned long isolated;
771 unsigned int order;
772
773 if (!PageBuddy(page))
774 return 0;
775
776 order = buddy_order(page);
777 isolated = __isolate_free_page(page, order);
778 if (!isolated)
779 return 0;
780
781 set_page_private(page, order);
782 list_add(&page->lru, list);
783
784 split_map_pages(list);
785
786 return isolated;
787}
788EXPORT_SYMBOL_GPL(isolate_and_split_free_page);
Carlos Llamas061e34c2021-04-23 19:20:54 +0000789#endif
Charan Teja Reddyf47b8522021-02-16 13:59:45 +0530790
Mel Gorman748446b2010-05-24 14:32:27 -0700791/* Similar to reclaim, but different enough that they don't share logic */
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800792static bool too_many_isolated(pg_data_t *pgdat)
Mel Gorman748446b2010-05-24 14:32:27 -0700793{
Minchan Kimbc693042010-09-09 16:38:00 -0700794 unsigned long active, inactive, isolated;
Mel Gorman748446b2010-05-24 14:32:27 -0700795
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800796 inactive = node_page_state(pgdat, NR_INACTIVE_FILE) +
797 node_page_state(pgdat, NR_INACTIVE_ANON);
798 active = node_page_state(pgdat, NR_ACTIVE_FILE) +
799 node_page_state(pgdat, NR_ACTIVE_ANON);
800 isolated = node_page_state(pgdat, NR_ISOLATED_FILE) +
801 node_page_state(pgdat, NR_ISOLATED_ANON);
Mel Gorman748446b2010-05-24 14:32:27 -0700802
Minchan Kimbc693042010-09-09 16:38:00 -0700803 return isolated > (inactive + active) / 2;
Mel Gorman748446b2010-05-24 14:32:27 -0700804}
805
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100806/**
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700807 * isolate_migratepages_block() - isolate all migrate-able pages within
808 * a single pageblock
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100809 * @cc: Compaction control structure.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700810 * @low_pfn: The first PFN to isolate
811 * @end_pfn: The one-past-the-last PFN to isolate, within same pageblock
Hugh Dickinsc5eda602022-03-22 14:45:41 -0700812 * @mode: Isolation mode to be used.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100813 *
814 * Isolate all pages that can be migrated from the range specified by
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700815 * [low_pfn, end_pfn). The range is expected to be within same pageblock.
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700816 * Returns errno, like -EAGAIN or -EINTR in case e.g signal pending or congestion,
Oscar Salvador369fa222021-05-04 18:35:26 -0700817 * -ENOMEM in case we could not allocate a page, or 0.
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700818 * cc->migrate_pfn will contain the next pfn to scan.
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +0100819 *
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -0700820 * The pages are isolated on cc->migratepages list (not required to be empty),
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700821 * and cc->nr_migratepages is updated accordingly.
Mel Gorman748446b2010-05-24 14:32:27 -0700822 */
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700823static int
Minchan Kima934b9232023-01-12 09:52:49 -0800824isolate_migratepages_block(struct compact_control_ext *cc_ext, unsigned long low_pfn,
Hugh Dickinsc5eda602022-03-22 14:45:41 -0700825 unsigned long end_pfn, isolate_mode_t mode)
Mel Gorman748446b2010-05-24 14:32:27 -0700826{
Minchan Kima934b9232023-01-12 09:52:49 -0800827 struct compact_control *cc = cc_ext->cc;
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800828 pg_data_t *pgdat = cc->zone->zone_pgdat;
Mel Gormanb7aba692011-01-13 15:45:54 -0800829 unsigned long nr_scanned = 0, nr_isolated = 0;
Hugh Dickinsfa9add62012-05-29 15:07:09 -0700830 struct lruvec *lruvec;
Xiubo Lib8b2d822014-10-09 15:28:21 -0700831 unsigned long flags = 0;
Alex Shi6168d0d2020-12-15 12:34:29 -0800832 struct lruvec *locked = NULL;
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700833 struct page *page = NULL, *valid_page = NULL;
Hugh Dickinsc5eda602022-03-22 14:45:41 -0700834 struct address_space *mapping;
Joonsoo Kime34d85f2015-02-11 15:27:04 -0800835 unsigned long start_pfn = low_pfn;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700836 bool skip_on_failure = false;
837 unsigned long next_skip_pfn = 0;
Mel Gormane380beb2019-03-05 15:44:58 -0800838 bool skip_updated = false;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700839 int ret = 0;
840
841 cc->migrate_pfn = low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -0700842
Mel Gorman748446b2010-05-24 14:32:27 -0700843 /*
844 * Ensure that there are not too many pages isolated from the LRU
845 * list by either parallel reclaimers or compaction. If there are,
846 * delay for some time until fewer pages are isolated
847 */
Andrey Ryabinin5f438ee2019-03-05 15:49:42 -0800848 while (unlikely(too_many_isolated(pgdat))) {
Zi Yand20bdd572020-11-13 22:51:43 -0800849 /* stop isolation if there are still pages not migrated */
850 if (cc->nr_migratepages)
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700851 return -EAGAIN;
Zi Yand20bdd572020-11-13 22:51:43 -0800852
Mel Gormanf9e35b32011-06-15 15:08:52 -0700853 /* async migration should just abort */
David Rientjese0b9dae2014-06-04 16:08:28 -0700854 if (cc->mode == MIGRATE_ASYNC)
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700855 return -EAGAIN;
Mel Gormanf9e35b32011-06-15 15:08:52 -0700856
Mel Gorman748446b2010-05-24 14:32:27 -0700857 congestion_wait(BLK_RW_ASYNC, HZ/10);
858
859 if (fatal_signal_pending(current))
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700860 return -EINTR;
Mel Gorman748446b2010-05-24 14:32:27 -0700861 }
862
Mel Gormancf66f072019-03-05 15:45:24 -0800863 cond_resched();
David Rientjesaeef4b82014-06-04 16:08:31 -0700864
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700865 if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
866 skip_on_failure = true;
867 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
868 }
869
Mel Gorman748446b2010-05-24 14:32:27 -0700870 /* Time to isolate some pages for migration */
Mel Gorman748446b2010-05-24 14:32:27 -0700871 for (; low_pfn < end_pfn; low_pfn++) {
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700872
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700873 if (skip_on_failure && low_pfn >= next_skip_pfn) {
874 /*
875 * We have isolated all migration candidates in the
876 * previous order-aligned block, and did not skip it due
877 * to failure. We should migrate the pages now and
878 * hopefully succeed compaction.
879 */
880 if (nr_isolated)
881 break;
882
883 /*
884 * We failed to isolate in the previous order-aligned
885 * block. Set the new boundary to the end of the
886 * current block. Note we can't simply increase
887 * next_skip_pfn by 1 << order, as low_pfn might have
888 * been incremented by a higher number due to skipping
889 * a compound or a high-order buddy page in the
890 * previous loop iteration.
891 */
892 next_skip_pfn = block_end_pfn(low_pfn, cc->order);
893 }
894
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700895 /*
896 * Periodically drop the lock (if held) regardless of its
Mel Gorman670105a2019-08-02 21:48:51 -0700897 * contention, to give chance to IRQs. Abort completely if
898 * a fatal signal is pending.
Vlastimil Babka8b44d272014-10-09 15:27:16 -0700899 */
Alex Shi6168d0d2020-12-15 12:34:29 -0800900 if (!(low_pfn % SWAP_CLUSTER_MAX)) {
901 if (locked) {
902 unlock_page_lruvec_irqrestore(locked, flags);
903 locked = NULL;
904 }
905
906 if (fatal_signal_pending(current)) {
907 cc->contended = true;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -0700908 ret = -EINTR;
Alex Shi6168d0d2020-12-15 12:34:29 -0800909
Alex Shi6168d0d2020-12-15 12:34:29 -0800910 goto fatal_pending;
911 }
912
913 cond_resched();
Mel Gorman670105a2019-08-02 21:48:51 -0700914 }
Mel Gormanc67fe372012-08-21 16:16:17 -0700915
Mel Gormanb7aba692011-01-13 15:45:54 -0800916 nr_scanned++;
Mel Gorman748446b2010-05-24 14:32:27 -0700917
Mel Gorman748446b2010-05-24 14:32:27 -0700918 page = pfn_to_page(low_pfn);
Mel Gormandc908602012-02-08 17:13:38 -0800919
Mel Gormane380beb2019-03-05 15:44:58 -0800920 /*
921 * Check if the pageblock has already been marked skipped.
922 * Only the aligned PFN is checked as the caller isolates
923 * COMPACT_CLUSTER_MAX at a time so the second call must
924 * not falsely conclude that the block should be skipped.
925 */
926 if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) {
927 if (!cc->ignore_skip_hint && get_pageblock_skip(page)) {
928 low_pfn = end_pfn;
Alex Shi9df41312020-12-15 12:34:20 -0800929 page = NULL;
Mel Gormane380beb2019-03-05 15:44:58 -0800930 goto isolate_abort;
931 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700932 valid_page = page;
Mel Gormane380beb2019-03-05 15:44:58 -0800933 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -0700934
Oscar Salvador369fa222021-05-04 18:35:26 -0700935 if (PageHuge(page) && cc->alloc_contig) {
Oscar Salvadorae37c7f2021-05-04 18:35:29 -0700936 ret = isolate_or_dissolve_huge_page(page, &cc->migratepages);
Oscar Salvador369fa222021-05-04 18:35:26 -0700937
938 /*
939 * Fail isolation in case isolate_or_dissolve_huge_page()
940 * reports an error. In case of -ENOMEM, abort right away.
941 */
942 if (ret < 0) {
943 /* Do not report -EBUSY down the chain */
944 if (ret == -EBUSY)
945 ret = 0;
946 low_pfn += (1UL << compound_order(page)) - 1;
947 goto isolate_fail;
948 }
949
Oscar Salvadorae37c7f2021-05-04 18:35:29 -0700950 if (PageHuge(page)) {
951 /*
952 * Hugepage was successfully isolated and placed
953 * on the cc->migratepages list.
954 */
955 low_pfn += compound_nr(page) - 1;
956 goto isolate_success_no_list;
957 }
958
Oscar Salvador369fa222021-05-04 18:35:26 -0700959 /*
960 * Ok, the hugepage was dissolved. Now these pages are
961 * Buddy and cannot be re-allocated because they are
962 * isolated. Fall-through as the check below handles
963 * Buddy pages.
964 */
965 }
966
Mel Gorman6c144662014-01-23 15:53:38 -0800967 /*
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700968 * Skip if free. We read page order here without zone lock
969 * which is generally unsafe, but the race window is small and
970 * the worst thing that can happen is that we skip some
971 * potential isolation targets.
Mel Gorman6c144662014-01-23 15:53:38 -0800972 */
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700973 if (PageBuddy(page)) {
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -0700974 unsigned long freepage_order = buddy_order_unsafe(page);
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700975
976 /*
977 * Without lock, we cannot be sure that what we got is
978 * a valid page order. Consider only values in the
979 * valid order range to prevent low_pfn overflow.
980 */
981 if (freepage_order > 0 && freepage_order < MAX_ORDER)
982 low_pfn += (1UL << freepage_order) - 1;
Mel Gorman748446b2010-05-24 14:32:27 -0700983 continue;
Vlastimil Babka99c0fd52014-10-09 15:27:23 -0700984 }
Mel Gorman748446b2010-05-24 14:32:27 -0700985
Mel Gorman9927af742011-01-13 15:45:59 -0800986 /*
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700987 * Regardless of being on LRU, compound pages such as THP and
Rik van Riel1da2f322020-04-01 21:10:31 -0700988 * hugetlbfs are not to be compacted unless we are attempting
989 * an allocation much larger than the huge page size (eg CMA).
990 * We can potentially save a lot of iterations if we skip them
991 * at once. The check is racy, but we can consider only valid
992 * values and the only danger is skipping too much.
Andrea Arcangelibc835012011-01-13 15:47:08 -0800993 */
Rik van Riel1da2f322020-04-01 21:10:31 -0700994 if (PageCompound(page) && !cc->alloc_contig) {
David Rientjes21dc7e02017-11-17 15:26:30 -0800995 const unsigned int order = compound_order(page);
Vlastimil Babka29c0dde2015-09-08 15:02:46 -0700996
Vlastimil Babkad3c85ba2017-11-17 15:26:41 -0800997 if (likely(order < MAX_ORDER))
David Rientjes21dc7e02017-11-17 15:26:30 -0800998 low_pfn += (1UL << order) - 1;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -0700999 goto isolate_fail;
Mel Gorman2a1402a2012-10-08 16:32:33 -07001000 }
1001
Minchan Kimbda807d2016-07-26 15:23:05 -07001002 /*
1003 * Check may be lockless but that's ok as we recheck later.
1004 * It's possible to migrate LRU and non-lru movable pages.
1005 * Skip any other type of page
1006 */
1007 if (!PageLRU(page)) {
Minchan Kimbda807d2016-07-26 15:23:05 -07001008 /*
1009 * __PageMovable can return false positive so we need
1010 * to verify it under page_lock.
1011 */
1012 if (unlikely(__PageMovable(page)) &&
1013 !PageIsolated(page)) {
1014 if (locked) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001015 unlock_page_lruvec_irqrestore(locked, flags);
1016 locked = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -07001017 }
1018
Hugh Dickinsc5eda602022-03-22 14:45:41 -07001019 if (!isolate_movable_page(page, mode))
Minchan Kimbda807d2016-07-26 15:23:05 -07001020 goto isolate_success;
1021 }
1022
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001023 goto isolate_fail;
Minchan Kimbda807d2016-07-26 15:23:05 -07001024 }
Vlastimil Babka29c0dde2015-09-08 15:02:46 -07001025
David Rientjes119d6d52014-04-03 14:48:00 -07001026 /*
Alex Shi9df41312020-12-15 12:34:20 -08001027 * Be careful not to clear PageLRU until after we're
1028 * sure the page is not being freed elsewhere -- the
1029 * page release code relies on it.
1030 */
1031 if (unlikely(!get_page_unless_zero(page)))
1032 goto isolate_fail;
1033
Gavin Shanb951ab42022-11-24 17:55:23 +08001034 /*
1035 * Migration will fail if an anonymous page is pinned in memory,
1036 * so avoid taking lru_lock and isolating it unnecessarily in an
1037 * admittedly racy check.
1038 */
1039 mapping = page_mapping(page);
1040 if (!mapping && (page_count(page) - 1) > total_mapcount(page))
Alex Shi9df41312020-12-15 12:34:20 -08001041 goto isolate_fail_put;
1042
Gavin Shanb951ab42022-11-24 17:55:23 +08001043 /*
1044 * Only allow to migrate anonymous pages in GFP_NOFS context
1045 * because those do not depend on fs locks.
1046 */
1047 if (!(cc->gfp_mask & __GFP_FS) && mapping)
1048 goto isolate_fail_put;
1049
Hugh Dickinsc5eda602022-03-22 14:45:41 -07001050 /* Only take pages on LRU: a check now makes later tests safe */
1051 if (!PageLRU(page))
Alex Shi9df41312020-12-15 12:34:20 -08001052 goto isolate_fail_put;
1053
Hugh Dickinsc5eda602022-03-22 14:45:41 -07001054 /* Compaction might skip unevictable pages but CMA takes them */
1055 if (!(mode & ISOLATE_UNEVICTABLE) && PageUnevictable(page))
1056 goto isolate_fail_put;
1057
1058 /*
1059 * To minimise LRU disruption, the caller can indicate with
1060 * ISOLATE_ASYNC_MIGRATE that it only wants to isolate pages
1061 * it will be able to migrate without blocking - clean pages
1062 * for the most part. PageWriteback would require blocking.
1063 */
1064 if ((mode & ISOLATE_ASYNC_MIGRATE) && PageWriteback(page))
1065 goto isolate_fail_put;
1066
1067 if ((mode & ISOLATE_ASYNC_MIGRATE) && PageDirty(page)) {
1068 bool migrate_dirty;
1069
1070 /*
1071 * Only pages without mappings or that have a
1072 * ->migratepage callback are possible to migrate
1073 * without blocking. However, we can be racing with
1074 * truncation so it's necessary to lock the page
1075 * to stabilise the mapping as truncation holds
1076 * the page lock until after the page is removed
1077 * from the page cache.
1078 */
1079 if (!trylock_page(page))
1080 goto isolate_fail_put;
1081
1082 mapping = page_mapping(page);
1083 migrate_dirty = !mapping || mapping->a_ops->migratepage;
1084 unlock_page(page);
1085 if (!migrate_dirty)
1086 goto isolate_fail_put;
1087 }
1088
Alex Shi9df41312020-12-15 12:34:20 -08001089 /* Try isolate the page */
1090 if (!TestClearPageLRU(page))
1091 goto isolate_fail_put;
1092
Muchun Songa9842262021-06-28 19:37:53 -07001093 lruvec = mem_cgroup_page_lruvec(page);
Alex Shi6168d0d2020-12-15 12:34:29 -08001094
Vlastimil Babka69b71892014-10-09 15:27:18 -07001095 /* If we already hold the lock, we can skip some rechecking */
Alex Shi6168d0d2020-12-15 12:34:29 -08001096 if (lruvec != locked) {
1097 if (locked)
1098 unlock_page_lruvec_irqrestore(locked, flags);
1099
1100 compact_lock_irqsave(&lruvec->lru_lock, &flags, cc);
1101 locked = lruvec;
Alex Shi6168d0d2020-12-15 12:34:29 -08001102
1103 lruvec_memcg_debug(lruvec, page);
Mel Gormane380beb2019-03-05 15:44:58 -08001104
Mel Gormane380beb2019-03-05 15:44:58 -08001105 /* Try get exclusive access under lock */
1106 if (!skip_updated) {
1107 skip_updated = true;
1108 if (test_and_set_skip(cc, page, low_pfn))
1109 goto isolate_abort;
1110 }
Mel Gorman2a1402a2012-10-08 16:32:33 -07001111
Vlastimil Babka29c0dde2015-09-08 15:02:46 -07001112 /*
1113 * Page become compound since the non-locked check,
1114 * and it's on LRU. It can only be a THP so the order
1115 * is safe to read and it's 0 for tail pages.
1116 */
Rik van Riel1da2f322020-04-01 21:10:31 -07001117 if (unlikely(PageCompound(page) && !cc->alloc_contig)) {
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001118 low_pfn += compound_nr(page) - 1;
Alex Shi9df41312020-12-15 12:34:20 -08001119 SetPageLRU(page);
1120 goto isolate_fail_put;
Vlastimil Babka69b71892014-10-09 15:27:18 -07001121 }
Alex Shid99fd5f2021-02-24 12:09:25 -08001122 }
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001123
Rik van Riel1da2f322020-04-01 21:10:31 -07001124 /* The whole page is taken off the LRU; skip the tail pages. */
1125 if (PageCompound(page))
1126 low_pfn += compound_nr(page) - 1;
Andrea Arcangelibc835012011-01-13 15:47:08 -08001127
Mel Gorman748446b2010-05-24 14:32:27 -07001128 /* Successfully isolated */
Yu Zhao46ae6b22021-02-24 12:08:25 -08001129 del_page_from_lru_list(page, lruvec);
Rik van Riel1da2f322020-04-01 21:10:31 -07001130 mod_node_page_state(page_pgdat(page),
Huang Ying9de4f222020-04-06 20:04:41 -07001131 NR_ISOLATED_ANON + page_is_file_lru(page),
Matthew Wilcox (Oracle)6c357842020-08-14 17:30:37 -07001132 thp_nr_pages(page));
Joonsoo Kimb6c75012014-04-07 15:37:07 -07001133
1134isolate_success:
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001135 list_add(&page->lru, &cc->migratepages);
Oscar Salvadorae37c7f2021-05-04 18:35:29 -07001136isolate_success_no_list:
Zi Yan38935862020-11-13 22:51:40 -08001137 cc->nr_migratepages += compound_nr(page);
Minchan Kima934b9232023-01-12 09:52:49 -08001138 if (!PageAnon(page))
1139 cc_ext->nr_migrate_file_pages += compound_nr(page);
Zi Yan38935862020-11-13 22:51:40 -08001140 nr_isolated += compound_nr(page);
Mel Gorman748446b2010-05-24 14:32:27 -07001141
Mel Gorman804d3122019-03-05 15:45:07 -08001142 /*
1143 * Avoid isolating too much unless this block is being
Mel Gormancb2dcaf2019-03-05 15:45:11 -08001144 * rescanned (e.g. dirty/writeback pages, parallel allocation)
1145 * or a lock is contended. For contention, isolate quickly to
1146 * potentially remove one source of contention.
Mel Gorman804d3122019-03-05 15:45:07 -08001147 */
Zi Yan38935862020-11-13 22:51:40 -08001148 if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
Mel Gormancb2dcaf2019-03-05 15:45:11 -08001149 !cc->rescan && !cc->contended) {
Hillf Danton31b83842012-01-10 15:07:59 -08001150 ++low_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07001151 break;
Hillf Danton31b83842012-01-10 15:07:59 -08001152 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001153
1154 continue;
Alex Shi9df41312020-12-15 12:34:20 -08001155
1156isolate_fail_put:
1157 /* Avoid potential deadlock in freeing page under lru_lock */
1158 if (locked) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001159 unlock_page_lruvec_irqrestore(locked, flags);
1160 locked = NULL;
Alex Shi9df41312020-12-15 12:34:20 -08001161 }
1162 put_page(page);
1163
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001164isolate_fail:
Oscar Salvador369fa222021-05-04 18:35:26 -07001165 if (!skip_on_failure && ret != -ENOMEM)
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001166 continue;
1167
1168 /*
1169 * We have isolated some pages, but then failed. Release them
1170 * instead of migrating, as we cannot form the cc->order buddy
1171 * page anyway.
1172 */
1173 if (nr_isolated) {
1174 if (locked) {
Alex Shi6168d0d2020-12-15 12:34:29 -08001175 unlock_page_lruvec_irqrestore(locked, flags);
1176 locked = NULL;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001177 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001178 putback_movable_pages(&cc->migratepages);
1179 cc->nr_migratepages = 0;
Minchan Kima934b9232023-01-12 09:52:49 -08001180 cc_ext->nr_migrate_file_pages = 0;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07001181 nr_isolated = 0;
1182 }
1183
1184 if (low_pfn < next_skip_pfn) {
1185 low_pfn = next_skip_pfn - 1;
1186 /*
1187 * The check near the loop beginning would have updated
1188 * next_skip_pfn too, but this is a bit simpler.
1189 */
1190 next_skip_pfn += 1UL << cc->order;
1191 }
Oscar Salvador369fa222021-05-04 18:35:26 -07001192
1193 if (ret == -ENOMEM)
1194 break;
Mel Gorman748446b2010-05-24 14:32:27 -07001195 }
1196
Vlastimil Babka99c0fd52014-10-09 15:27:23 -07001197 /*
1198 * The PageBuddy() check could have potentially brought us outside
1199 * the range to be scanned.
1200 */
1201 if (unlikely(low_pfn > end_pfn))
1202 low_pfn = end_pfn;
1203
Alex Shi9df41312020-12-15 12:34:20 -08001204 page = NULL;
1205
Mel Gormane380beb2019-03-05 15:44:58 -08001206isolate_abort:
Mel Gormanc67fe372012-08-21 16:16:17 -07001207 if (locked)
Alex Shi6168d0d2020-12-15 12:34:29 -08001208 unlock_page_lruvec_irqrestore(locked, flags);
Alex Shi9df41312020-12-15 12:34:20 -08001209 if (page) {
1210 SetPageLRU(page);
1211 put_page(page);
1212 }
Mel Gorman748446b2010-05-24 14:32:27 -07001213
Vlastimil Babka50b5b092014-01-21 15:51:10 -08001214 /*
Mel Gorman804d3122019-03-05 15:45:07 -08001215 * Updated the cached scanner pfn once the pageblock has been scanned
1216 * Pages will either be migrated in which case there is no point
1217 * scanning in the near future or migration failed in which case the
1218 * failure reason may persist. The block is marked for skipping if
1219 * there were no pages isolated in the block or if the block is
1220 * rescanned twice in a row.
Vlastimil Babka50b5b092014-01-21 15:51:10 -08001221 */
Mel Gorman804d3122019-03-05 15:45:07 -08001222 if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) {
Mel Gormane380beb2019-03-05 15:44:58 -08001223 if (valid_page && !skip_updated)
1224 set_pageblock_skip(valid_page);
1225 update_cached_migrate(cc, low_pfn);
1226 }
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001227
Joonsoo Kime34d85f2015-02-11 15:27:04 -08001228 trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
1229 nr_scanned, nr_isolated);
Mel Gormanb7aba692011-01-13 15:45:54 -08001230
Mel Gorman670105a2019-08-02 21:48:51 -07001231fatal_pending:
David Rientjes7f354a52017-02-22 15:44:50 -08001232 cc->total_migrate_scanned += nr_scanned;
Mel Gorman397487d2012-10-19 12:00:10 +01001233 if (nr_isolated)
Minchan Kim010fc292012-12-20 15:05:06 -08001234 count_compact_events(COMPACTISOLATED, nr_isolated);
Mel Gorman397487d2012-10-19 12:00:10 +01001235
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001236 cc->migrate_pfn = low_pfn;
1237
1238 return ret;
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001239}
1240
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001241/**
1242 * isolate_migratepages_range() - isolate migrate-able pages in a PFN range
1243 * @cc: Compaction control structure.
1244 * @start_pfn: The first PFN to start isolating.
1245 * @end_pfn: The one-past-last PFN.
1246 *
Oscar Salvador369fa222021-05-04 18:35:26 -07001247 * Returns -EAGAIN when contented, -EINTR in case of a signal pending, -ENOMEM
1248 * in case we could not allocate a page, or 0.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001249 */
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001250int
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001251isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
1252 unsigned long end_pfn)
1253{
Minchan Kima934b9232023-01-12 09:52:49 -08001254 struct compact_control_ext cc_ext = { .cc = cc };
Joonsoo Kime1409c32016-03-15 14:57:48 -07001255 unsigned long pfn, block_start_pfn, block_end_pfn;
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001256 int ret = 0;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001257
1258 /* Scan block by block. First and last block may be incomplete */
1259 pfn = start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001260 block_start_pfn = pageblock_start_pfn(pfn);
Joonsoo Kime1409c32016-03-15 14:57:48 -07001261 if (block_start_pfn < cc->zone->zone_start_pfn)
1262 block_start_pfn = cc->zone->zone_start_pfn;
Vlastimil Babka06b66402016-05-19 17:11:48 -07001263 block_end_pfn = pageblock_end_pfn(pfn);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001264
1265 for (; pfn < end_pfn; pfn = block_end_pfn,
Joonsoo Kime1409c32016-03-15 14:57:48 -07001266 block_start_pfn = block_end_pfn,
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001267 block_end_pfn += pageblock_nr_pages) {
1268
1269 block_end_pfn = min(block_end_pfn, end_pfn);
1270
Joonsoo Kime1409c32016-03-15 14:57:48 -07001271 if (!pageblock_pfn_to_page(block_start_pfn,
1272 block_end_pfn, cc->zone))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001273 continue;
1274
Suren Baghdasaryan3fd32dc172024-03-14 15:53:20 -07001275 ret = isolate_migratepages_block(&cc_ext, pfn, block_end_pfn,
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001276 ISOLATE_UNEVICTABLE);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001277
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001278 if (ret)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001279 break;
Joonsoo Kim6ea41c02014-10-29 14:50:20 -07001280
Zi Yan38935862020-11-13 22:51:40 -08001281 if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
Joonsoo Kim6ea41c02014-10-29 14:50:20 -07001282 break;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001283 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001284
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001285 return ret;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001286}
1287
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001288#endif /* CONFIG_COMPACTION || CONFIG_CMA */
1289#ifdef CONFIG_COMPACTION
Andrew Morton018e9a42015-04-15 16:15:20 -07001290
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001291static bool suitable_migration_source(struct compact_control *cc,
1292 struct page *page)
1293{
Vlastimil Babka282722b2017-05-08 15:54:49 -07001294 int block_mt;
1295
Mel Gorman9bebefd2019-03-05 15:45:14 -08001296 if (pageblock_skip_persistent(page))
1297 return false;
1298
Vlastimil Babka282722b2017-05-08 15:54:49 -07001299 if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001300 return true;
1301
Vlastimil Babka282722b2017-05-08 15:54:49 -07001302 block_mt = get_pageblock_migratetype(page);
1303
1304 if (cc->migratetype == MIGRATE_MOVABLE)
1305 return is_migrate_movable(block_mt);
1306 else
1307 return block_mt == cc->migratetype;
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001308}
1309
Andrew Morton018e9a42015-04-15 16:15:20 -07001310/* Returns true if the page is within a block suitable for migration to */
Minchan Kima934b9232023-01-12 09:52:49 -08001311static bool suitable_migration_target(struct compact_control_ext *cc_ext,
Vlastimil Babka9f7e3382016-10-07 17:00:37 -07001312 struct page *page)
Andrew Morton018e9a42015-04-15 16:15:20 -07001313{
Minchan Kima934b9232023-01-12 09:52:49 -08001314 struct compact_control *cc = cc_ext->cc;
Andrew Morton018e9a42015-04-15 16:15:20 -07001315 /* If the page is a large free page, then disallow migration */
1316 if (PageBuddy(page)) {
1317 /*
1318 * We are checking page_order without zone->lock taken. But
1319 * the only small danger is that we skip a potentially suitable
1320 * pageblock, so it's not worth to check order for valid range.
1321 */
Matthew Wilcox (Oracle)ab130f912020-10-15 20:10:15 -07001322 if (buddy_order_unsafe(page) >= pageblock_order)
Andrew Morton018e9a42015-04-15 16:15:20 -07001323 return false;
1324 }
1325
Yisheng Xie1ef36db2017-05-03 14:53:54 -07001326 if (cc->ignore_block_suitable)
1327 return true;
1328
Minchan Kima934b9232023-01-12 09:52:49 -08001329 /* Allow file pages to migrate only into MIGRATE_MOVABLE blocks */
1330 if (cc_ext->nr_migrate_file_pages)
1331 return get_pageblock_migratetype(page) == MIGRATE_MOVABLE;
1332
Andrew Morton018e9a42015-04-15 16:15:20 -07001333 /* If the block is MIGRATE_MOVABLE or MIGRATE_CMA, allow migration */
Vlastimil Babkab682deb2017-05-08 15:54:43 -07001334 if (is_migrate_movable(get_pageblock_migratetype(page)))
Andrew Morton018e9a42015-04-15 16:15:20 -07001335 return true;
1336
1337 /* Otherwise skip the block */
1338 return false;
1339}
1340
Mel Gorman70b44592019-03-05 15:44:54 -08001341static inline unsigned int
1342freelist_scan_limit(struct compact_control *cc)
1343{
Qian Caidd7ef7b2019-05-13 17:17:38 -07001344 unsigned short shift = BITS_PER_LONG - 1;
1345
1346 return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1;
Mel Gorman70b44592019-03-05 15:44:54 -08001347}
1348
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001349/*
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001350 * Test whether the free scanner has reached the same or lower pageblock than
1351 * the migration scanner, and compaction should thus terminate.
1352 */
1353static inline bool compact_scanners_met(struct compact_control *cc)
1354{
1355 return (cc->free_pfn >> pageblock_order)
1356 <= (cc->migrate_pfn >> pageblock_order);
1357}
1358
Mel Gorman5a811882019-03-05 15:45:01 -08001359/*
1360 * Used when scanning for a suitable migration target which scans freelists
1361 * in reverse. Reorders the list such as the unscanned pages are scanned
1362 * first on the next iteration of the free scanner
1363 */
1364static void
1365move_freelist_head(struct list_head *freelist, struct page *freepage)
1366{
1367 LIST_HEAD(sublist);
1368
1369 if (!list_is_last(freelist, &freepage->lru)) {
1370 list_cut_before(&sublist, freelist, &freepage->lru);
Liu Xiangd2155fe2021-06-30 18:50:51 -07001371 list_splice_tail(&sublist, freelist);
Mel Gorman5a811882019-03-05 15:45:01 -08001372 }
1373}
1374
1375/*
1376 * Similar to move_freelist_head except used by the migration scanner
1377 * when scanning forward. It's possible for these list operations to
1378 * move against each other if they search the free list exactly in
1379 * lockstep.
1380 */
Mel Gorman70b44592019-03-05 15:44:54 -08001381static void
1382move_freelist_tail(struct list_head *freelist, struct page *freepage)
1383{
1384 LIST_HEAD(sublist);
1385
1386 if (!list_is_first(freelist, &freepage->lru)) {
1387 list_cut_position(&sublist, freelist, &freepage->lru);
Liu Xiangd2155fe2021-06-30 18:50:51 -07001388 list_splice_tail(&sublist, freelist);
Mel Gorman70b44592019-03-05 15:44:54 -08001389 }
1390}
1391
Mel Gorman5a811882019-03-05 15:45:01 -08001392static void
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001393fast_isolate_around(struct compact_control *cc, unsigned long pfn)
Mel Gorman5a811882019-03-05 15:45:01 -08001394{
1395 unsigned long start_pfn, end_pfn;
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001396 struct page *page;
Mel Gorman5a811882019-03-05 15:45:01 -08001397
1398 /* Do not search around if there are enough pages already */
1399 if (cc->nr_freepages >= cc->nr_migratepages)
1400 return;
1401
1402 /* Minimise scanning during async compaction */
1403 if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
1404 return;
1405
1406 /* Pageblock boundaries */
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001407 start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn);
1408 end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone));
1409
1410 page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone);
1411 if (!page)
1412 return;
Mel Gorman5a811882019-03-05 15:45:01 -08001413
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001414 isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
Mel Gorman5a811882019-03-05 15:45:01 -08001415
1416 /* Skip this pageblock in the future as it's full or nearly full */
1417 if (cc->nr_freepages < cc->nr_migratepages)
1418 set_pageblock_skip(page);
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001419
1420 return;
Mel Gorman5a811882019-03-05 15:45:01 -08001421}
1422
Mel Gormandbe2d4e2019-03-05 15:45:31 -08001423/* Search orders in round-robin fashion */
1424static int next_search_order(struct compact_control *cc, int order)
1425{
1426 order--;
1427 if (order < 0)
1428 order = cc->order - 1;
1429
1430 /* Search wrapped around? */
1431 if (order == cc->search_order) {
1432 cc->search_order--;
1433 if (cc->search_order < 0)
1434 cc->search_order = cc->order - 1;
1435 return -1;
1436 }
1437
1438 return order;
1439}
1440
Mel Gorman5a811882019-03-05 15:45:01 -08001441static unsigned long
1442fast_isolate_freepages(struct compact_control *cc)
1443{
Wonhyuk Yangb55ca522021-06-30 18:50:53 -07001444 unsigned int limit = max(1U, freelist_scan_limit(cc) >> 1);
Mel Gorman5a811882019-03-05 15:45:01 -08001445 unsigned int nr_scanned = 0;
Rokudo Yan74e21482021-02-04 18:32:20 -08001446 unsigned long low_pfn, min_pfn, highest = 0;
Mel Gorman5a811882019-03-05 15:45:01 -08001447 unsigned long nr_isolated = 0;
1448 unsigned long distance;
1449 struct page *page = NULL;
1450 bool scan_start = false;
1451 int order;
1452
1453 /* Full compaction passes in a negative order */
1454 if (cc->order <= 0)
1455 return cc->free_pfn;
1456
1457 /*
1458 * If starting the scan, use a deeper search and use the highest
1459 * PFN found if a suitable one is not found.
1460 */
Mel Gormane332f742019-03-05 15:45:38 -08001461 if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
Mel Gorman5a811882019-03-05 15:45:01 -08001462 limit = pageblock_nr_pages >> 1;
1463 scan_start = true;
1464 }
1465
1466 /*
1467 * Preferred point is in the top quarter of the scan space but take
1468 * a pfn from the top half if the search is problematic.
1469 */
1470 distance = (cc->free_pfn - cc->migrate_pfn);
1471 low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
1472 min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
1473
1474 if (WARN_ON_ONCE(min_pfn > low_pfn))
1475 low_pfn = min_pfn;
1476
Mel Gormandbe2d4e2019-03-05 15:45:31 -08001477 /*
1478 * Search starts from the last successful isolation order or the next
1479 * order to search after a previous failure
1480 */
1481 cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1482
1483 for (order = cc->search_order;
1484 !page && order >= 0;
1485 order = next_search_order(cc, order)) {
Mel Gorman5a811882019-03-05 15:45:01 -08001486 struct free_area *area = &cc->zone->free_area[order];
1487 struct list_head *freelist;
1488 struct page *freepage;
1489 unsigned long flags;
1490 unsigned int order_scanned = 0;
Rokudo Yan74e21482021-02-04 18:32:20 -08001491 unsigned long high_pfn = 0;
Mel Gorman5a811882019-03-05 15:45:01 -08001492
1493 if (!area->nr_free)
1494 continue;
1495
1496 spin_lock_irqsave(&cc->zone->lock, flags);
1497 freelist = &area->free_list[MIGRATE_MOVABLE];
1498 list_for_each_entry_reverse(freepage, freelist, lru) {
1499 unsigned long pfn;
1500
1501 order_scanned++;
1502 nr_scanned++;
1503 pfn = page_to_pfn(freepage);
1504
1505 if (pfn >= highest)
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001506 highest = max(pageblock_start_pfn(pfn),
1507 cc->zone->zone_start_pfn);
Mel Gorman5a811882019-03-05 15:45:01 -08001508
1509 if (pfn >= low_pfn) {
1510 cc->fast_search_fail = 0;
Mel Gormandbe2d4e2019-03-05 15:45:31 -08001511 cc->search_order = order;
Mel Gorman5a811882019-03-05 15:45:01 -08001512 page = freepage;
1513 break;
1514 }
1515
1516 if (pfn >= min_pfn && pfn > high_pfn) {
1517 high_pfn = pfn;
1518
1519 /* Shorten the scan if a candidate is found */
1520 limit >>= 1;
1521 }
1522
1523 if (order_scanned >= limit)
1524 break;
1525 }
1526
1527 /* Use a minimum pfn if a preferred one was not found */
1528 if (!page && high_pfn) {
1529 page = pfn_to_page(high_pfn);
1530
1531 /* Update freepage for the list reorder below */
1532 freepage = page;
1533 }
1534
1535 /* Reorder to so a future search skips recent pages */
1536 move_freelist_head(freelist, freepage);
1537
1538 /* Isolate the page if available */
1539 if (page) {
1540 if (__isolate_free_page(page, order)) {
1541 set_page_private(page, order);
1542 nr_isolated = 1 << order;
1543 cc->nr_freepages += nr_isolated;
1544 list_add_tail(&page->lru, &cc->freepages);
1545 count_compact_events(COMPACTISOLATED, nr_isolated);
1546 } else {
1547 /* If isolation fails, abort the search */
Qian Cai5b56d992019-04-04 11:54:41 +01001548 order = cc->search_order + 1;
Mel Gorman5a811882019-03-05 15:45:01 -08001549 page = NULL;
1550 }
1551 }
1552
1553 spin_unlock_irqrestore(&cc->zone->lock, flags);
1554
1555 /*
Wonhyuk Yangb55ca522021-06-30 18:50:53 -07001556 * Smaller scan on next order so the total scan is related
Mel Gorman5a811882019-03-05 15:45:01 -08001557 * to freelist_scan_limit.
1558 */
1559 if (order_scanned >= limit)
Wonhyuk Yangb55ca522021-06-30 18:50:53 -07001560 limit = max(1U, limit >> 1);
Mel Gorman5a811882019-03-05 15:45:01 -08001561 }
1562
1563 if (!page) {
1564 cc->fast_search_fail++;
1565 if (scan_start) {
1566 /*
1567 * Use the highest PFN found above min. If one was
Ethon Paulf3867752020-06-04 16:49:13 -07001568 * not found, be pessimistic for direct compaction
Mel Gorman5a811882019-03-05 15:45:01 -08001569 * and use the min mark.
1570 */
1571 if (highest) {
1572 page = pfn_to_page(highest);
1573 cc->free_pfn = highest;
1574 } else {
Suzuki K Poulosee577c8b2019-05-31 22:30:59 -07001575 if (cc->direct_compaction && pfn_valid(min_pfn)) {
Baoquan He73a6e472020-06-03 15:57:55 -07001576 page = pageblock_pfn_to_page(min_pfn,
Vlastimil Babka6e2b7042021-02-24 12:09:39 -08001577 min(pageblock_end_pfn(min_pfn),
1578 zone_end_pfn(cc->zone)),
Baoquan He73a6e472020-06-03 15:57:55 -07001579 cc->zone);
Mel Gorman5a811882019-03-05 15:45:01 -08001580 cc->free_pfn = min_pfn;
1581 }
1582 }
1583 }
1584 }
1585
Mel Gormand097a6f2019-03-05 15:45:28 -08001586 if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1587 highest -= pageblock_nr_pages;
Mel Gorman5a811882019-03-05 15:45:01 -08001588 cc->zone->compact_cached_free_pfn = highest;
Mel Gormand097a6f2019-03-05 15:45:28 -08001589 }
Mel Gorman5a811882019-03-05 15:45:01 -08001590
1591 cc->total_free_scanned += nr_scanned;
1592 if (!page)
1593 return cc->free_pfn;
1594
1595 low_pfn = page_to_pfn(page);
NARIBAYASHI Akira35d8a892022-10-26 20:24:38 +09001596 fast_isolate_around(cc, low_pfn);
Mel Gorman5a811882019-03-05 15:45:01 -08001597 return low_pfn;
1598}
1599
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07001600/*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001601 * Based on information in the current compact_control, find blocks
1602 * suitable for isolating free pages from and then isolate them.
1603 */
Minchan Kima934b9232023-01-12 09:52:49 -08001604static void isolate_freepages(struct compact_control_ext *cc_ext)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001605{
Minchan Kima934b9232023-01-12 09:52:49 -08001606 struct compact_control *cc = cc_ext->cc;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001607 struct zone *zone = cc->zone;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001608 struct page *page;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001609 unsigned long block_start_pfn; /* start of current pageblock */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001610 unsigned long isolate_start_pfn; /* exact pfn we start at */
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001611 unsigned long block_end_pfn; /* end of current pageblock */
1612 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001613 struct list_head *freelist = &cc->freepages;
Mel Gorman4fca9732019-03-05 15:45:34 -08001614 unsigned int stride;
qinglin.li0681d572023-11-06 15:02:04 +08001615 bool bypass = false;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001616
Mel Gorman5a811882019-03-05 15:45:01 -08001617 /* Try a small search of the free lists for a candidate */
1618 isolate_start_pfn = fast_isolate_freepages(cc);
1619 if (cc->nr_freepages)
1620 goto splitmap;
1621
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001622 /*
1623 * Initialise the free scanner. The starting point is where we last
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001624 * successfully isolated from, zone-cached value, or the end of the
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001625 * zone when isolating for the first time. For looping we also need
1626 * this pfn aligned down to the pageblock boundary, because we do
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001627 * block_start_pfn -= pageblock_nr_pages in the for loop.
1628 * For ending point, take care when isolating in last pageblock of a
Randy Dunlapa1c1dbe2020-08-11 18:32:49 -07001629 * zone which ends in the middle of a pageblock.
Vlastimil Babka49e068f2014-05-06 12:50:03 -07001630 * The low boundary is the end of the pageblock the migration scanner
1631 * is using.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001632 */
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001633 isolate_start_pfn = cc->free_pfn;
Mel Gorman5a811882019-03-05 15:45:01 -08001634 block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001635 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
1636 zone_end_pfn(zone));
Vlastimil Babka06b66402016-05-19 17:11:48 -07001637 low_pfn = pageblock_end_pfn(cc->migrate_pfn);
Mel Gorman4fca9732019-03-05 15:45:34 -08001638 stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001639
1640 /*
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001641 * Isolate free pages until enough are available to migrate the
1642 * pages on cc->migratepages. We stop searching if the migrate
1643 * and free page scanners meet or enough free pages are isolated.
1644 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001645 for (; block_start_pfn >= low_pfn;
Vlastimil Babkac96b9e52014-06-04 16:07:26 -07001646 block_end_pfn = block_start_pfn,
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001647 block_start_pfn -= pageblock_nr_pages,
1648 isolate_start_pfn = block_start_pfn) {
Mel Gorman4fca9732019-03-05 15:45:34 -08001649 unsigned long nr_isolated;
1650
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001651 /*
1652 * This can iterate a massively long zone without finding any
Mel Gormancb810ad2019-03-05 15:45:21 -08001653 * suitable migration targets, so periodically check resched.
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001654 */
Mel Gormancb810ad2019-03-05 15:45:21 -08001655 if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
Mel Gormancf66f072019-03-05 15:45:24 -08001656 cond_resched();
David Rientjesf6ea3ad2013-09-30 13:45:03 -07001657
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001658 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1659 zone);
1660 if (!page)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001661 continue;
1662
1663 /* Check the block is suitable for migration */
Minchan Kima934b9232023-01-12 09:52:49 -08001664 if (!suitable_migration_target(cc_ext, page))
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001665 continue;
Linus Torvalds68e3e922012-06-03 20:05:57 -07001666
Mel Gormanbb13ffe2012-10-08 16:32:41 -07001667 /* If isolation recently failed, do not retry */
1668 if (!isolation_suitable(cc, page))
1669 continue;
1670
qinglin.li0681d572023-11-06 15:02:04 +08001671 trace_android_vh_isolate_freepages(cc, page, &bypass);
1672 if (bypass)
1673 continue;
1674
Vlastimil Babkae14c7202014-10-09 15:27:20 -07001675 /* Found a block suitable for isolating free pages from. */
Mel Gorman4fca9732019-03-05 15:45:34 -08001676 nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
1677 block_end_pfn, freelist, stride, false);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001678
Mel Gormand097a6f2019-03-05 15:45:28 -08001679 /* Update the skip hint if the full pageblock was scanned */
1680 if (isolate_start_pfn == block_end_pfn)
1681 update_pageblock_skip(cc, page, block_start_pfn);
1682
Mel Gormancb2dcaf2019-03-05 15:45:11 -08001683 /* Are enough freepages isolated? */
1684 if (cc->nr_freepages >= cc->nr_migratepages) {
David Rientjesa46cbf32016-07-14 12:06:50 -07001685 if (isolate_start_pfn >= block_end_pfn) {
1686 /*
1687 * Restart at previous pageblock if more
1688 * freepages can be isolated next time.
1689 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001690 isolate_start_pfn =
1691 block_start_pfn - pageblock_nr_pages;
David Rientjesa46cbf32016-07-14 12:06:50 -07001692 }
Vlastimil Babkabe976572014-06-04 16:10:41 -07001693 break;
David Rientjesa46cbf32016-07-14 12:06:50 -07001694 } else if (isolate_start_pfn < block_end_pfn) {
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001695 /*
David Rientjesa46cbf32016-07-14 12:06:50 -07001696 * If isolation failed early, do not continue
1697 * needlessly.
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001698 */
David Rientjesa46cbf32016-07-14 12:06:50 -07001699 break;
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001700 }
Mel Gorman4fca9732019-03-05 15:45:34 -08001701
1702 /* Adjust stride depending on isolation */
1703 if (nr_isolated) {
1704 stride = 1;
1705 continue;
1706 }
1707 stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
Michal Nazarewicz2fe86e02012-01-30 13:16:26 +01001708 }
1709
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001710 /*
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001711 * Record where the free scanner will restart next time. Either we
1712 * broke from the loop and set isolate_start_pfn based on the last
1713 * call to isolate_freepages_block(), or we met the migration scanner
1714 * and the loop terminated due to isolate_start_pfn < low_pfn
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08001715 */
Vlastimil Babkaf5f61a32015-09-08 15:02:39 -07001716 cc->free_pfn = isolate_start_pfn;
Mel Gorman5a811882019-03-05 15:45:01 -08001717
1718splitmap:
1719 /* __isolate_free_page() does not map the pages */
1720 split_map_pages(freelist);
Mel Gorman748446b2010-05-24 14:32:27 -07001721}
1722
1723/*
1724 * This is a migrate-callback that "allocates" freepages by taking pages
1725 * from the isolated freelists in the block we are migrating to.
1726 */
1727static struct page *compaction_alloc(struct page *migratepage,
Michal Hocko666feb22018-04-10 16:30:03 -07001728 unsigned long data)
Mel Gorman748446b2010-05-24 14:32:27 -07001729{
Minchan Kima934b9232023-01-12 09:52:49 -08001730 struct compact_control_ext *cc_ext = (struct compact_control_ext *)data;
1731 struct compact_control *cc = cc_ext->cc;
Mel Gorman748446b2010-05-24 14:32:27 -07001732 struct page *freepage;
1733
Mel Gorman748446b2010-05-24 14:32:27 -07001734 if (list_empty(&cc->freepages)) {
Minchan Kima934b9232023-01-12 09:52:49 -08001735 isolate_freepages(cc_ext);
Mel Gorman748446b2010-05-24 14:32:27 -07001736
1737 if (list_empty(&cc->freepages))
1738 return NULL;
1739 }
1740
1741 freepage = list_entry(cc->freepages.next, struct page, lru);
1742 list_del(&freepage->lru);
1743 cc->nr_freepages--;
Tao Zenga1db7dc2022-01-20 15:09:43 +08001744#ifdef CONFIG_AMLOGIC_PAGE_TRACE
1745 replace_page_trace(freepage, migratepage);
1746#endif
Mel Gorman748446b2010-05-24 14:32:27 -07001747
1748 return freepage;
1749}
1750
1751/*
David Rientjesd53aea32014-06-04 16:08:26 -07001752 * This is a migrate-callback that "frees" freepages back to the isolated
1753 * freelist. All pages on the freelist are from the same zone, so there is no
1754 * special handling needed for NUMA.
1755 */
1756static void compaction_free(struct page *page, unsigned long data)
1757{
Minchan Kima934b9232023-01-12 09:52:49 -08001758 struct compact_control_ext *cc_ext = (struct compact_control_ext *)data;
1759 struct compact_control *cc = cc_ext->cc;
David Rientjesd53aea32014-06-04 16:08:26 -07001760
1761 list_add(&page->lru, &cc->freepages);
1762 cc->nr_freepages++;
1763}
1764
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001765/* possible outcome of isolate_migratepages */
1766typedef enum {
1767 ISOLATE_ABORT, /* Abort compaction now */
1768 ISOLATE_NONE, /* No pages isolated, continue scanning */
1769 ISOLATE_SUCCESS, /* Pages isolated, migrate */
1770} isolate_migrate_t;
1771
1772/*
Eric B Munson5bbe3542015-04-15 16:13:20 -07001773 * Allow userspace to control policy on scanning the unevictable LRU for
1774 * compactable pages.
1775 */
Sebastian Andrzej Siewior6923aa02020-04-01 21:10:42 -07001776#ifdef CONFIG_PREEMPT_RT
1777int sysctl_compact_unevictable_allowed __read_mostly = 0;
1778#else
Eric B Munson5bbe3542015-04-15 16:13:20 -07001779int sysctl_compact_unevictable_allowed __read_mostly = 1;
Sebastian Andrzej Siewior6923aa02020-04-01 21:10:42 -07001780#endif
Eric B Munson5bbe3542015-04-15 16:13:20 -07001781
Mel Gorman70b44592019-03-05 15:44:54 -08001782static inline void
1783update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
1784{
1785 if (cc->fast_start_pfn == ULONG_MAX)
1786 return;
1787
1788 if (!cc->fast_start_pfn)
1789 cc->fast_start_pfn = pfn;
1790
1791 cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
1792}
1793
1794static inline unsigned long
1795reinit_migrate_pfn(struct compact_control *cc)
1796{
1797 if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
1798 return cc->migrate_pfn;
1799
1800 cc->migrate_pfn = cc->fast_start_pfn;
1801 cc->fast_start_pfn = ULONG_MAX;
1802
1803 return cc->migrate_pfn;
1804}
1805
1806/*
1807 * Briefly search the free lists for a migration source that already has
1808 * some free pages to reduce the number of pages that need migration
1809 * before a pageblock is free.
1810 */
1811static unsigned long fast_find_migrateblock(struct compact_control *cc)
1812{
1813 unsigned int limit = freelist_scan_limit(cc);
1814 unsigned int nr_scanned = 0;
1815 unsigned long distance;
1816 unsigned long pfn = cc->migrate_pfn;
1817 unsigned long high_pfn;
1818 int order;
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001819 bool found_block = false;
Mel Gorman70b44592019-03-05 15:44:54 -08001820
1821 /* Skip hints are relied on to avoid repeats on the fast search */
1822 if (cc->ignore_skip_hint)
1823 return pfn;
1824
1825 /*
1826 * If the migrate_pfn is not at the start of a zone or the start
1827 * of a pageblock then assume this is a continuation of a previous
1828 * scan restarted due to COMPACT_CLUSTER_MAX.
1829 */
1830 if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
1831 return pfn;
1832
1833 /*
1834 * For smaller orders, just linearly scan as the number of pages
1835 * to migrate should be relatively small and does not necessarily
1836 * justify freeing up a large block for a small allocation.
1837 */
1838 if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
1839 return pfn;
1840
1841 /*
1842 * Only allow kcompactd and direct requests for movable pages to
1843 * quickly clear out a MOVABLE pageblock for allocation. This
1844 * reduces the risk that a large movable pageblock is freed for
1845 * an unmovable/reclaimable small allocation.
1846 */
1847 if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
1848 return pfn;
1849
1850 /*
1851 * When starting the migration scanner, pick any pageblock within the
1852 * first half of the search space. Otherwise try and pick a pageblock
1853 * within the first eighth to reduce the chances that a migration
1854 * target later becomes a source.
1855 */
1856 distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
1857 if (cc->migrate_pfn != cc->zone->zone_start_pfn)
1858 distance >>= 2;
1859 high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
1860
1861 for (order = cc->order - 1;
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001862 order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit;
Mel Gorman70b44592019-03-05 15:44:54 -08001863 order--) {
1864 struct free_area *area = &cc->zone->free_area[order];
1865 struct list_head *freelist;
1866 unsigned long flags;
1867 struct page *freepage;
1868
1869 if (!area->nr_free)
1870 continue;
1871
1872 spin_lock_irqsave(&cc->zone->lock, flags);
1873 freelist = &area->free_list[MIGRATE_MOVABLE];
1874 list_for_each_entry(freepage, freelist, lru) {
1875 unsigned long free_pfn;
1876
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001877 if (nr_scanned++ >= limit) {
1878 move_freelist_tail(freelist, freepage);
1879 break;
1880 }
1881
Mel Gorman70b44592019-03-05 15:44:54 -08001882 free_pfn = page_to_pfn(freepage);
1883 if (free_pfn < high_pfn) {
Mel Gorman70b44592019-03-05 15:44:54 -08001884 /*
1885 * Avoid if skipped recently. Ideally it would
1886 * move to the tail but even safe iteration of
1887 * the list assumes an entry is deleted, not
1888 * reordered.
1889 */
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001890 if (get_pageblock_skip(freepage))
Mel Gorman70b44592019-03-05 15:44:54 -08001891 continue;
Mel Gorman70b44592019-03-05 15:44:54 -08001892
1893 /* Reorder to so a future search skips recent pages */
1894 move_freelist_tail(freelist, freepage);
1895
Mel Gormane380beb2019-03-05 15:44:58 -08001896 update_fast_start_pfn(cc, free_pfn);
Mel Gorman70b44592019-03-05 15:44:54 -08001897 pfn = pageblock_start_pfn(free_pfn);
Rei Yamamoto20e6ec72022-05-13 16:48:57 -07001898 if (pfn < cc->zone->zone_start_pfn)
1899 pfn = cc->zone->zone_start_pfn;
Mel Gorman70b44592019-03-05 15:44:54 -08001900 cc->fast_search_fail = 0;
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001901 found_block = true;
Mel Gorman70b44592019-03-05 15:44:54 -08001902 set_pageblock_skip(freepage);
1903 break;
1904 }
Mel Gorman70b44592019-03-05 15:44:54 -08001905 }
1906 spin_unlock_irqrestore(&cc->zone->lock, flags);
1907 }
1908
1909 cc->total_migrate_scanned += nr_scanned;
1910
1911 /*
1912 * If fast scanning failed then use a cached entry for a page block
1913 * that had free pages as the basis for starting a linear scan.
1914 */
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001915 if (!found_block) {
1916 cc->fast_search_fail++;
Mel Gorman70b44592019-03-05 15:44:54 -08001917 pfn = reinit_migrate_pfn(cc);
Wonhyuk Yang15d28d02021-02-24 12:09:36 -08001918 }
Mel Gorman70b44592019-03-05 15:44:54 -08001919 return pfn;
1920}
1921
Eric B Munson5bbe3542015-04-15 16:13:20 -07001922/*
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001923 * Isolate all pages that can be migrated from the first suitable block,
1924 * starting at the block pointed to by the migrate scanner pfn within
1925 * compact_control.
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001926 */
Minchan Kima934b9232023-01-12 09:52:49 -08001927static isolate_migrate_t isolate_migratepages(struct compact_control_ext *cc_ext)
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001928{
Minchan Kima934b9232023-01-12 09:52:49 -08001929 struct compact_control *cc = cc_ext->cc;
Joonsoo Kime1409c32016-03-15 14:57:48 -07001930 unsigned long block_start_pfn;
1931 unsigned long block_end_pfn;
1932 unsigned long low_pfn;
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001933 struct page *page;
1934 const isolate_mode_t isolate_mode =
Eric B Munson5bbe3542015-04-15 16:13:20 -07001935 (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
Hugh Dickins1d2047f2016-07-28 15:48:41 -07001936 (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
Mel Gorman70b44592019-03-05 15:44:54 -08001937 bool fast_find_block;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001938
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001939 /*
1940 * Start at where we last stopped, or beginning of the zone as
Mel Gorman70b44592019-03-05 15:44:54 -08001941 * initialized by compact_zone(). The first failure will use
1942 * the lowest PFN as the starting point for linear scanning.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001943 */
Mel Gorman70b44592019-03-05 15:44:54 -08001944 low_pfn = fast_find_migrateblock(cc);
Vlastimil Babka06b66402016-05-19 17:11:48 -07001945 block_start_pfn = pageblock_start_pfn(low_pfn);
Pengfei Li32aaf052019-09-23 15:36:58 -07001946 if (block_start_pfn < cc->zone->zone_start_pfn)
1947 block_start_pfn = cc->zone->zone_start_pfn;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001948
Mel Gorman70b44592019-03-05 15:44:54 -08001949 /*
1950 * fast_find_migrateblock marks a pageblock skipped so to avoid
1951 * the isolation_suitable check below, check whether the fast
1952 * search was successful.
1953 */
1954 fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
1955
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001956 /* Only scan within a pageblock boundary */
Vlastimil Babka06b66402016-05-19 17:11:48 -07001957 block_end_pfn = pageblock_end_pfn(low_pfn);
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01001958
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001959 /*
1960 * Iterate over whole pageblocks until we find the first suitable.
1961 * Do not cross the free scanner.
1962 */
Joonsoo Kime1409c32016-03-15 14:57:48 -07001963 for (; block_end_pfn <= cc->free_pfn;
Mel Gorman70b44592019-03-05 15:44:54 -08001964 fast_find_block = false,
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07001965 cc->migrate_pfn = low_pfn = block_end_pfn,
Joonsoo Kime1409c32016-03-15 14:57:48 -07001966 block_start_pfn = block_end_pfn,
1967 block_end_pfn += pageblock_nr_pages) {
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001968
1969 /*
1970 * This can potentially iterate a massively long zone with
1971 * many pageblocks unsuitable, so periodically check if we
Mel Gormancb810ad2019-03-05 15:45:21 -08001972 * need to schedule.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001973 */
Mel Gormancb810ad2019-03-05 15:45:21 -08001974 if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
Mel Gormancf66f072019-03-05 15:45:24 -08001975 cond_resched();
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001976
Pengfei Li32aaf052019-09-23 15:36:58 -07001977 page = pageblock_pfn_to_page(block_start_pfn,
1978 block_end_pfn, cc->zone);
Vlastimil Babka7d49d882014-10-09 15:27:11 -07001979 if (!page)
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001980 continue;
1981
Mel Gormane380beb2019-03-05 15:44:58 -08001982 /*
1983 * If isolation recently failed, do not retry. Only check the
1984 * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
1985 * to be visited multiple times. Assume skip was checked
1986 * before making it "skip" so other compaction instances do
1987 * not scan the same block.
1988 */
1989 if (IS_ALIGNED(low_pfn, pageblock_nr_pages) &&
1990 !fast_find_block && !isolation_suitable(cc, page))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07001991 continue;
1992
1993 /*
Mel Gorman9bebefd2019-03-05 15:45:14 -08001994 * For async compaction, also only scan in MOVABLE blocks
1995 * without huge pages. Async compaction is optimistic to see
1996 * if the minimum amount of work satisfies the allocation.
1997 * The cached PFN is updated as it's possible that all
1998 * remaining blocks between source and target are unsuitable
1999 * and the compaction scanners fail to meet.
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002000 */
Mel Gorman9bebefd2019-03-05 15:45:14 -08002001 if (!suitable_migration_source(cc, page)) {
2002 update_cached_migrate(cc, block_end_pfn);
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002003 continue;
Mel Gorman9bebefd2019-03-05 15:45:14 -08002004 }
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002005
2006 /* Perform the isolation */
Minchan Kima934b9232023-01-12 09:52:49 -08002007 if (isolate_migratepages_block(cc_ext, low_pfn, block_end_pfn,
Oscar Salvadorc2ad7a12021-05-04 18:35:17 -07002008 isolate_mode))
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002009 return ISOLATE_ABORT;
2010
2011 /*
2012 * Either we isolated something and proceed with migration. Or
2013 * we failed and compact_zone should decide if we should
2014 * continue or not.
2015 */
2016 break;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002017 }
2018
Vlastimil Babkaedc2ca62014-10-09 15:27:09 -07002019 return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002020}
2021
Yaowei Bai21c527a2015-11-05 18:47:20 -08002022/*
2023 * order == -1 is expected when compacting via
2024 * /proc/sys/vm/compact_memory
2025 */
2026static inline bool is_via_compact_memory(int order)
2027{
2028 return order == -1;
2029}
2030
Nitin Guptafacdaa92020-08-11 18:31:00 -07002031static bool kswapd_is_running(pg_data_t *pgdat)
2032{
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02002033 return pgdat->kswapd && task_is_running(pgdat->kswapd);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002034}
2035
2036/*
2037 * A zone's fragmentation score is the external fragmentation wrt to the
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002038 * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100].
2039 */
2040static unsigned int fragmentation_score_zone(struct zone *zone)
2041{
2042 return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER);
2043}
2044
2045/*
2046 * A weighted zone's fragmentation score is the external fragmentation
2047 * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It
2048 * returns a value in the range [0, 100].
Nitin Guptafacdaa92020-08-11 18:31:00 -07002049 *
2050 * The scaling factor ensures that proactive compaction focuses on larger
2051 * zones like ZONE_NORMAL, rather than smaller, specialized zones like
2052 * ZONE_DMA32. For smaller zones, the score value remains close to zero,
2053 * and thus never exceeds the high threshold for proactive compaction.
2054 */
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002055static unsigned int fragmentation_score_zone_weighted(struct zone *zone)
Nitin Guptafacdaa92020-08-11 18:31:00 -07002056{
2057 unsigned long score;
2058
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002059 score = zone->present_pages * fragmentation_score_zone(zone);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002060 return div64_ul(score, zone->zone_pgdat->node_present_pages + 1);
2061}
2062
2063/*
2064 * The per-node proactive (background) compaction process is started by its
2065 * corresponding kcompactd thread when the node's fragmentation score
2066 * exceeds the high threshold. The compaction process remains active till
2067 * the node's score falls below the low threshold, or one of the back-off
2068 * conditions is met.
2069 */
Nitin Guptad34c0a72020-08-11 18:31:07 -07002070static unsigned int fragmentation_score_node(pg_data_t *pgdat)
Nitin Guptafacdaa92020-08-11 18:31:00 -07002071{
Nitin Guptad34c0a72020-08-11 18:31:07 -07002072 unsigned int score = 0;
Nitin Guptafacdaa92020-08-11 18:31:00 -07002073 int zoneid;
2074
2075 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2076 struct zone *zone;
2077
2078 zone = &pgdat->node_zones[zoneid];
Charan Teja Reddy40d7e202021-02-24 12:09:32 -08002079 score += fragmentation_score_zone_weighted(zone);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002080 }
2081
2082 return score;
2083}
2084
Nitin Guptad34c0a72020-08-11 18:31:07 -07002085static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low)
Nitin Guptafacdaa92020-08-11 18:31:00 -07002086{
Nitin Guptad34c0a72020-08-11 18:31:07 -07002087 unsigned int wmark_low;
Nitin Guptafacdaa92020-08-11 18:31:00 -07002088
2089 /*
Ingo Molnarf0953a12021-05-06 18:06:47 -07002090 * Cap the low watermark to avoid excessive compaction
2091 * activity in case a user sets the proactiveness tunable
Nitin Guptafacdaa92020-08-11 18:31:00 -07002092 * close to 100 (maximum).
2093 */
Nitin Guptad34c0a72020-08-11 18:31:07 -07002094 wmark_low = max(100U - sysctl_compaction_proactiveness, 5U);
2095 return low ? wmark_low : min(wmark_low + 10, 100U);
Nitin Guptafacdaa92020-08-11 18:31:00 -07002096}
2097
2098static bool should_proactive_compact_node(pg_data_t *pgdat)
2099{
2100 int wmark_high;
2101
2102 if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat))
2103 return false;
2104
2105 wmark_high = fragmentation_score_wmark(pgdat, false);
2106 return fragmentation_score_node(pgdat) > wmark_high;
2107}
2108
Mel Gorman40cacbc2019-03-05 15:44:36 -08002109static enum compact_result __compact_finished(struct compact_control *cc)
Mel Gorman748446b2010-05-24 14:32:27 -07002110{
Mel Gorman8fb74b92013-01-11 14:32:16 -08002111 unsigned int order;
Vlastimil Babkad39773a2017-05-08 15:54:46 -07002112 const int migratetype = cc->migratetype;
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002113 int ret;
Mel Gorman748446b2010-05-24 14:32:27 -07002114
Mel Gorman753341a2012-10-08 16:32:40 -07002115 /* Compaction run completes if the migrate and free scanner meet */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07002116 if (compact_scanners_met(cc)) {
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08002117 /* Let the next compaction start anew. */
Mel Gorman40cacbc2019-03-05 15:44:36 -08002118 reset_cached_positions(cc->zone);
Vlastimil Babka55b7c4c2014-01-21 15:51:11 -08002119
Mel Gorman62997022012-10-08 16:32:47 -07002120 /*
2121 * Mark that the PG_migrate_skip information should be cleared
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002122 * by kswapd when it goes to sleep. kcompactd does not set the
Mel Gorman62997022012-10-08 16:32:47 -07002123 * flag itself as the decision to be clear should be directly
2124 * based on an allocation request.
2125 */
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002126 if (cc->direct_compaction)
Mel Gorman40cacbc2019-03-05 15:44:36 -08002127 cc->zone->compact_blockskip_flush = true;
Mel Gorman62997022012-10-08 16:32:47 -07002128
Michal Hockoc8f7de02016-05-20 16:56:47 -07002129 if (cc->whole_zone)
2130 return COMPACT_COMPLETE;
2131 else
2132 return COMPACT_PARTIAL_SKIPPED;
Mel Gormanbb13ffe2012-10-08 16:32:41 -07002133 }
Mel Gorman748446b2010-05-24 14:32:27 -07002134
Nitin Guptafacdaa92020-08-11 18:31:00 -07002135 if (cc->proactive_compaction) {
2136 int score, wmark_low;
2137 pg_data_t *pgdat;
2138
2139 pgdat = cc->zone->zone_pgdat;
2140 if (kswapd_is_running(pgdat))
2141 return COMPACT_PARTIAL_SKIPPED;
2142
2143 score = fragmentation_score_zone(cc->zone);
2144 wmark_low = fragmentation_score_wmark(pgdat, true);
2145
2146 if (score > wmark_low)
2147 ret = COMPACT_CONTINUE;
2148 else
2149 ret = COMPACT_SUCCESS;
2150
2151 goto out;
2152 }
2153
Yaowei Bai21c527a2015-11-05 18:47:20 -08002154 if (is_via_compact_memory(cc->order))
Mel Gorman56de7262010-05-24 14:32:30 -07002155 return COMPACT_CONTINUE;
2156
Mel Gormanefe771c2019-03-05 15:44:46 -08002157 /*
2158 * Always finish scanning a pageblock to reduce the possibility of
2159 * fallbacks in the future. This is particularly important when
2160 * migration source is unmovable/reclaimable but it's not worth
2161 * special casing.
2162 */
2163 if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
2164 return COMPACT_CONTINUE;
Vlastimil Babkabaf6a9a2017-05-08 15:54:52 -07002165
Mel Gorman56de7262010-05-24 14:32:30 -07002166 /* Direct compactor: Is a suitable page free? */
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002167 ret = COMPACT_NO_SUITABLE_PAGE;
Mel Gorman8fb74b92013-01-11 14:32:16 -08002168 for (order = cc->order; order < MAX_ORDER; order++) {
Mel Gorman40cacbc2019-03-05 15:44:36 -08002169 struct free_area *area = &cc->zone->free_area[order];
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002170 bool can_steal;
Mel Gorman56de7262010-05-24 14:32:30 -07002171
Mel Gorman8fb74b92013-01-11 14:32:16 -08002172 /* Job done if page is free of the right migratetype */
Dan Williamsb03641a2019-05-14 15:41:32 -07002173 if (!free_area_empty(area, migratetype))
Vlastimil Babkacf378312016-10-07 16:57:41 -07002174 return COMPACT_SUCCESS;
Mel Gorman8fb74b92013-01-11 14:32:16 -08002175
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002176#ifdef CONFIG_CMA
2177 /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
2178 if (migratetype == MIGRATE_MOVABLE &&
Dan Williamsb03641a2019-05-14 15:41:32 -07002179 !free_area_empty(area, MIGRATE_CMA))
Vlastimil Babkacf378312016-10-07 16:57:41 -07002180 return COMPACT_SUCCESS;
Joonsoo Kim2149cda2015-04-14 15:45:21 -07002181#endif
2182 /*
2183 * Job done if allocation would steal freepages from
2184 * other migratetype buddy lists.
2185 */
2186 if (find_suitable_fallback(area, order, migratetype,
Vlastimil Babkabaf6a9a2017-05-08 15:54:52 -07002187 true, &can_steal) != -1) {
2188
2189 /* movable pages are OK in any pageblock */
2190 if (migratetype == MIGRATE_MOVABLE)
2191 return COMPACT_SUCCESS;
2192
2193 /*
2194 * We are stealing for a non-movable allocation. Make
2195 * sure we finish compacting the current pageblock
2196 * first so it is as free as possible and we won't
2197 * have to steal another one soon. This only applies
2198 * to sync compaction, as async compaction operates
2199 * on pageblocks of the same migratetype.
2200 */
2201 if (cc->mode == MIGRATE_ASYNC ||
2202 IS_ALIGNED(cc->migrate_pfn,
2203 pageblock_nr_pages)) {
2204 return COMPACT_SUCCESS;
2205 }
2206
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002207 ret = COMPACT_CONTINUE;
2208 break;
Vlastimil Babkabaf6a9a2017-05-08 15:54:52 -07002209 }
Mel Gorman56de7262010-05-24 14:32:30 -07002210 }
2211
Nitin Guptafacdaa92020-08-11 18:31:00 -07002212out:
Mel Gormancb2dcaf2019-03-05 15:45:11 -08002213 if (cc->contended || fatal_signal_pending(current))
2214 ret = COMPACT_CONTENDED;
2215
2216 return ret;
Joonsoo Kim837d0262015-02-11 15:27:06 -08002217}
2218
Mel Gorman40cacbc2019-03-05 15:44:36 -08002219static enum compact_result compact_finished(struct compact_control *cc)
Joonsoo Kim837d0262015-02-11 15:27:06 -08002220{
2221 int ret;
2222
Mel Gorman40cacbc2019-03-05 15:44:36 -08002223 ret = __compact_finished(cc);
2224 trace_mm_compaction_finished(cc->zone, cc->order, ret);
Joonsoo Kim837d0262015-02-11 15:27:06 -08002225 if (ret == COMPACT_NO_SUITABLE_PAGE)
2226 ret = COMPACT_CONTINUE;
2227
2228 return ret;
Mel Gorman748446b2010-05-24 14:32:27 -07002229}
2230
Michal Hockoea7ab982016-05-20 16:56:38 -07002231static enum compact_result __compaction_suitable(struct zone *zone, int order,
Mel Gormanc6038442016-05-19 17:13:38 -07002232 unsigned int alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002233 int highest_zoneidx,
Michal Hocko86a294a2016-05-20 16:57:12 -07002234 unsigned long wmark_target)
Mel Gorman3e7d3442011-01-13 15:45:56 -08002235{
Mel Gorman3e7d3442011-01-13 15:45:56 -08002236 unsigned long watermark;
2237
Yaowei Bai21c527a2015-11-05 18:47:20 -08002238 if (is_via_compact_memory(order))
Michal Hocko3957c772011-06-15 15:08:25 -07002239 return COMPACT_CONTINUE;
2240
Mel Gormana9214442018-12-28 00:35:44 -08002241 watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002242 /*
2243 * If watermarks for high-order allocation are already met, there
2244 * should be no need for compaction at all.
2245 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002246 if (zone_watermark_ok(zone, order, watermark, highest_zoneidx,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002247 alloc_flags))
Vlastimil Babkacf378312016-10-07 16:57:41 -07002248 return COMPACT_SUCCESS;
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002249
Michal Hocko3957c772011-06-15 15:08:25 -07002250 /*
Vlastimil Babka9861a622016-10-07 16:57:53 -07002251 * Watermarks for order-0 must be met for compaction to be able to
Vlastimil Babka984fdba2016-10-07 16:57:57 -07002252 * isolate free pages for migration targets. This means that the
2253 * watermark and alloc_flags have to match, or be more pessimistic than
2254 * the check in __isolate_free_page(). We don't use the direct
2255 * compactor's alloc_flags, as they are not relevant for freepage
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002256 * isolation. We however do use the direct compactor's highest_zoneidx
2257 * to skip over zones where lowmem reserves would prevent allocation
2258 * even if compaction succeeds.
Vlastimil Babka8348faf2016-10-07 16:58:00 -07002259 * For costly orders, we require low watermark instead of min for
2260 * compaction to proceed to increase its chances.
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09002261 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
2262 * suitable migration targets
Mel Gorman3e7d3442011-01-13 15:45:56 -08002263 */
Vlastimil Babka8348faf2016-10-07 16:58:00 -07002264 watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
2265 low_wmark_pages(zone) : min_wmark_pages(zone);
2266 watermark += compact_gap(order);
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002267 if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx,
Joonsoo Kimd883c6c2018-05-23 10:18:21 +09002268 ALLOC_CMA, wmark_target))
Mel Gorman3e7d3442011-01-13 15:45:56 -08002269 return COMPACT_SKIPPED;
2270
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002271 return COMPACT_CONTINUE;
2272}
2273
Hui Su2b1a20c2020-12-14 19:12:42 -08002274/*
2275 * compaction_suitable: Is this suitable to run compaction on this zone now?
2276 * Returns
2277 * COMPACT_SKIPPED - If there are too few free pages for compaction
2278 * COMPACT_SUCCESS - If the allocation would succeed without compaction
2279 * COMPACT_CONTINUE - If compaction should run now
2280 */
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002281enum compact_result compaction_suitable(struct zone *zone, int order,
2282 unsigned int alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002283 int highest_zoneidx)
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002284{
2285 enum compact_result ret;
2286 int fragindex;
2287
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002288 ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx,
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002289 zone_page_state(zone, NR_FREE_PAGES));
Mel Gorman3e7d3442011-01-13 15:45:56 -08002290 /*
2291 * fragmentation index determines if allocation failures are due to
2292 * low memory or external fragmentation
2293 *
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002294 * index of -1000 would imply allocations might succeed depending on
2295 * watermarks, but we already failed the high-order watermark check
Mel Gorman3e7d3442011-01-13 15:45:56 -08002296 * index towards 0 implies failure is due to lack of memory
2297 * index towards 1000 implies failure is due to fragmentation
2298 *
Vlastimil Babka20311422016-10-07 17:00:46 -07002299 * Only compact if a failure would be due to fragmentation. Also
2300 * ignore fragindex for non-costly orders where the alternative to
2301 * a successful reclaim/compaction is OOM. Fragindex and the
2302 * vm.extfrag_threshold sysctl is meant as a heuristic to prevent
2303 * excessive compaction for costly orders, but it should not be at the
2304 * expense of system stability.
Mel Gorman3e7d3442011-01-13 15:45:56 -08002305 */
Vlastimil Babka20311422016-10-07 17:00:46 -07002306 if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002307 fragindex = fragmentation_index(zone, order);
2308 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
2309 ret = COMPACT_NOT_SUITABLE_ZONE;
2310 }
Mel Gorman3e7d3442011-01-13 15:45:56 -08002311
Joonsoo Kim837d0262015-02-11 15:27:06 -08002312 trace_mm_compaction_suitable(zone, order, ret);
2313 if (ret == COMPACT_NOT_SUITABLE_ZONE)
2314 ret = COMPACT_SKIPPED;
2315
2316 return ret;
2317}
2318
Michal Hocko86a294a2016-05-20 16:57:12 -07002319bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
2320 int alloc_flags)
2321{
2322 struct zone *zone;
2323 struct zoneref *z;
2324
2325 /*
2326 * Make sure at least one zone would pass __compaction_suitable if we continue
2327 * retrying the reclaim.
2328 */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002329 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2330 ac->highest_zoneidx, ac->nodemask) {
Michal Hocko86a294a2016-05-20 16:57:12 -07002331 unsigned long available;
2332 enum compact_result compact_result;
2333
2334 /*
2335 * Do not consider all the reclaimable memory because we do not
2336 * want to trash just for a single high order allocation which
2337 * is even not guaranteed to appear even if __compaction_suitable
2338 * is happy about the watermark check.
2339 */
Mel Gorman5a1c84b2016-07-28 15:47:31 -07002340 available = zone_reclaimable_pages(zone) / order;
Michal Hocko86a294a2016-05-20 16:57:12 -07002341 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
2342 compact_result = __compaction_suitable(zone, order, alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002343 ac->highest_zoneidx, available);
Vlastimil Babkacc5c9f02016-10-07 17:00:43 -07002344 if (compact_result != COMPACT_SKIPPED)
Michal Hocko86a294a2016-05-20 16:57:12 -07002345 return true;
2346 }
2347
2348 return false;
2349}
2350
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002351static enum compact_result
2352compact_zone(struct compact_control *cc, struct capture_control *capc)
Mel Gorman748446b2010-05-24 14:32:27 -07002353{
Michal Hockoea7ab982016-05-20 16:56:38 -07002354 enum compact_result ret;
Mel Gorman40cacbc2019-03-05 15:44:36 -08002355 unsigned long start_pfn = cc->zone->zone_start_pfn;
2356 unsigned long end_pfn = zone_end_pfn(cc->zone);
Mel Gorman566e54e2019-03-05 15:44:32 -08002357 unsigned long last_migrated_pfn;
David Rientjese0b9dae2014-06-04 16:08:28 -07002358 const bool sync = cc->mode != MIGRATE_ASYNC;
Mel Gorman8854c552019-03-05 15:45:18 -08002359 bool update_cached;
Robin Hsu1eeadb42022-05-13 10:19:50 +08002360 long vendor_ret;
Minchan Kima934b9232023-01-12 09:52:49 -08002361 struct compact_control_ext cc_ext = {
2362 .cc = cc,
2363 .nr_migrate_file_pages = 0,
2364 };
Mel Gorman748446b2010-05-24 14:32:27 -07002365
Yafang Shaoa94b5252019-09-23 15:36:54 -07002366 /*
2367 * These counters track activities during zone compaction. Initialize
2368 * them before compacting a new zone.
2369 */
2370 cc->total_migrate_scanned = 0;
2371 cc->total_free_scanned = 0;
2372 cc->nr_migratepages = 0;
2373 cc->nr_freepages = 0;
2374 INIT_LIST_HEAD(&cc->freepages);
2375 INIT_LIST_HEAD(&cc->migratepages);
2376
Wei Yang01c0bfe2020-06-03 15:59:08 -07002377 cc->migratetype = gfp_migratetype(cc->gfp_mask);
Mel Gorman40cacbc2019-03-05 15:44:36 -08002378 ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002379 cc->highest_zoneidx);
Michal Hockoc46649d2016-05-20 16:56:41 -07002380 /* Compaction is likely to fail */
Vlastimil Babkacf378312016-10-07 16:57:41 -07002381 if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
Mel Gorman3e7d3442011-01-13 15:45:56 -08002382 return ret;
Michal Hockoc46649d2016-05-20 16:56:41 -07002383
2384 /* huh, compaction_suitable is returning something unexpected */
2385 VM_BUG_ON(ret != COMPACT_CONTINUE);
Mel Gorman3e7d3442011-01-13 15:45:56 -08002386
Mel Gormanc89511a2012-10-08 16:32:45 -07002387 /*
Vlastimil Babkad3132e42014-01-21 15:51:08 -08002388 * Clear pageblock skip if there were failures recently and compaction
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002389 * is about to be retried after being deferred.
Vlastimil Babkad3132e42014-01-21 15:51:08 -08002390 */
Mel Gorman40cacbc2019-03-05 15:44:36 -08002391 if (compaction_restarting(cc->zone, cc->order))
2392 __reset_isolation_suitable(cc->zone);
Vlastimil Babkad3132e42014-01-21 15:51:08 -08002393
2394 /*
Mel Gormanc89511a2012-10-08 16:32:45 -07002395 * Setup to move all movable pages to the end of the zone. Used cached
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002396 * information on where the scanners should start (unless we explicitly
2397 * want to compact the whole zone), but check that it is initialised
2398 * by ensuring the values are within zone boundaries.
Mel Gormanc89511a2012-10-08 16:32:45 -07002399 */
Mel Gorman70b44592019-03-05 15:44:54 -08002400 cc->fast_start_pfn = 0;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002401 if (cc->whole_zone) {
Mel Gormanc89511a2012-10-08 16:32:45 -07002402 cc->migrate_pfn = start_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002403 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
2404 } else {
Mel Gorman40cacbc2019-03-05 15:44:36 -08002405 cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
2406 cc->free_pfn = cc->zone->compact_cached_free_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002407 if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
2408 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
Mel Gorman40cacbc2019-03-05 15:44:36 -08002409 cc->zone->compact_cached_free_pfn = cc->free_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002410 }
2411 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
2412 cc->migrate_pfn = start_pfn;
Mel Gorman40cacbc2019-03-05 15:44:36 -08002413 cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
2414 cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002415 }
Michal Hockoc8f7de02016-05-20 16:56:47 -07002416
Mel Gormane332f742019-03-05 15:45:38 -08002417 if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002418 cc->whole_zone = true;
2419 }
Michal Hockoc8f7de02016-05-20 16:56:47 -07002420
Mel Gorman566e54e2019-03-05 15:44:32 -08002421 last_migrated_pfn = 0;
Mel Gorman748446b2010-05-24 14:32:27 -07002422
Mel Gorman8854c552019-03-05 15:45:18 -08002423 /*
2424 * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
2425 * the basis that some migrations will fail in ASYNC mode. However,
2426 * if the cached PFNs match and pageblocks are skipped due to having
2427 * no isolation candidates, then the sync state does not matter.
2428 * Until a pageblock with isolation candidates is found, keep the
2429 * cached PFNs in sync to avoid revisiting the same blocks.
2430 */
2431 update_cached = !sync &&
2432 cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
2433
Joonsoo Kim16c4a092015-02-11 15:27:01 -08002434 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
2435 cc->free_pfn, end_pfn, sync);
Robin Hsu1eeadb42022-05-13 10:19:50 +08002436 trace_android_vh_mm_compaction_begin(cc, &vendor_ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08002437
Minchan Kim361a2a22021-05-04 18:36:57 -07002438 /* lru_add_drain_all could be expensive with involving other CPUs */
2439 lru_add_drain();
Mel Gorman748446b2010-05-24 14:32:27 -07002440
Mel Gorman40cacbc2019-03-05 15:44:36 -08002441 while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
Minchan Kim9d502c12011-03-22 16:30:39 -07002442 int err;
Yanfei Xu19d3cf92020-12-14 19:12:39 -08002443 unsigned long iteration_start_pfn = cc->migrate_pfn;
Mel Gorman748446b2010-05-24 14:32:27 -07002444
Mel Gorman804d3122019-03-05 15:45:07 -08002445 /*
2446 * Avoid multiple rescans which can happen if a page cannot be
2447 * isolated (dirty/writeback in async mode) or if the migrated
2448 * pages are being allocated before the pageblock is cleared.
2449 * The first rescan will capture the entire pageblock for
2450 * migration. If it fails, it'll be marked skip and scanning
2451 * will proceed as normal.
2452 */
2453 cc->rescan = false;
2454 if (pageblock_start_pfn(last_migrated_pfn) ==
Yanfei Xu19d3cf92020-12-14 19:12:39 -08002455 pageblock_start_pfn(iteration_start_pfn)) {
Mel Gorman804d3122019-03-05 15:45:07 -08002456 cc->rescan = true;
2457 }
2458
Minchan Kima934b9232023-01-12 09:52:49 -08002459 switch (isolate_migratepages(&cc_ext)) {
Mel Gormanf9e35b32011-06-15 15:08:52 -07002460 case ISOLATE_ABORT:
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08002461 ret = COMPACT_CONTENDED;
Rafael Aquini5733c7d2012-12-11 16:02:47 -08002462 putback_movable_pages(&cc->migratepages);
Shaohua Lie64c5232012-10-08 16:32:27 -07002463 cc->nr_migratepages = 0;
Minchan Kima934b9232023-01-12 09:52:49 -08002464 cc_ext.nr_migrate_file_pages = 0;
Mel Gormanf9e35b32011-06-15 15:08:52 -07002465 goto out;
2466 case ISOLATE_NONE:
Mel Gorman8854c552019-03-05 15:45:18 -08002467 if (update_cached) {
2468 cc->zone->compact_cached_migrate_pfn[1] =
2469 cc->zone->compact_cached_migrate_pfn[0];
2470 }
2471
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002472 /*
2473 * We haven't isolated and migrated anything, but
2474 * there might still be unflushed migrations from
2475 * previous cc->order aligned block.
2476 */
2477 goto check_drain;
Mel Gormanf9e35b32011-06-15 15:08:52 -07002478 case ISOLATE_SUCCESS:
Mel Gorman8854c552019-03-05 15:45:18 -08002479 update_cached = false;
Yanfei Xu19d3cf92020-12-14 19:12:39 -08002480 last_migrated_pfn = iteration_start_pfn;
Mel Gormanf9e35b32011-06-15 15:08:52 -07002481 }
Mel Gorman748446b2010-05-24 14:32:27 -07002482
David Rientjesd53aea32014-06-04 16:08:26 -07002483 err = migrate_pages(&cc->migratepages, compaction_alloc,
Minchan Kima934b9232023-01-12 09:52:49 -08002484 compaction_free, (unsigned long)&cc_ext, cc->mode,
Yang Shi5ac95882021-09-02 14:59:13 -07002485 MR_COMPACTION, NULL);
Mel Gorman748446b2010-05-24 14:32:27 -07002486
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07002487 trace_mm_compaction_migratepages(cc->nr_migratepages, err,
2488 &cc->migratepages);
Mel Gorman748446b2010-05-24 14:32:27 -07002489
Vlastimil Babkaf8c93012014-06-04 16:08:32 -07002490 /* All pages were either migrated or will be released */
2491 cc->nr_migratepages = 0;
Minchan Kima934b9232023-01-12 09:52:49 -08002492 cc_ext.nr_migrate_file_pages = 0;
Minchan Kim9d502c12011-03-22 16:30:39 -07002493 if (err) {
Rafael Aquini5733c7d2012-12-11 16:02:47 -08002494 putback_movable_pages(&cc->migratepages);
Vlastimil Babka7ed695e2014-01-21 15:51:09 -08002495 /*
2496 * migrate_pages() may return -ENOMEM when scanners meet
2497 * and we want compact_finished() to detect it
2498 */
Vlastimil Babkaf2849aa2015-09-08 15:02:36 -07002499 if (err == -ENOMEM && !compact_scanners_met(cc)) {
Vlastimil Babka2d1e1042015-11-05 18:48:02 -08002500 ret = COMPACT_CONTENDED;
David Rientjes4bf2bba2012-07-11 14:02:13 -07002501 goto out;
2502 }
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07002503 /*
2504 * We failed to migrate at least one page in the current
2505 * order-aligned block, so skip the rest of it.
2506 */
2507 if (cc->direct_compaction &&
2508 (cc->mode == MIGRATE_ASYNC)) {
2509 cc->migrate_pfn = block_end_pfn(
2510 cc->migrate_pfn - 1, cc->order);
2511 /* Draining pcplists is useless in this case */
Mel Gorman566e54e2019-03-05 15:44:32 -08002512 last_migrated_pfn = 0;
Vlastimil Babkafdd048e2016-05-19 17:11:55 -07002513 }
Mel Gorman748446b2010-05-24 14:32:27 -07002514 }
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002515
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002516check_drain:
2517 /*
2518 * Has the migration scanner moved away from the previous
2519 * cc->order aligned block where we migrated from? If yes,
2520 * flush the pages that were freed, so that they can merge and
2521 * compact_finished() can detect immediately if allocation
2522 * would succeed.
2523 */
Mel Gorman566e54e2019-03-05 15:44:32 -08002524 if (cc->order > 0 && last_migrated_pfn) {
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002525 unsigned long current_block_start =
Vlastimil Babka06b66402016-05-19 17:11:48 -07002526 block_start_pfn(cc->migrate_pfn, cc->order);
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002527
Mel Gorman566e54e2019-03-05 15:44:32 -08002528 if (last_migrated_pfn < current_block_start) {
Ingo Molnarb01b2142020-05-27 22:11:15 +02002529 lru_add_drain_cpu_zone(cc->zone);
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002530 /* No more flushing until we migrate again */
Mel Gorman566e54e2019-03-05 15:44:32 -08002531 last_migrated_pfn = 0;
Vlastimil Babkafdaf7f52014-12-10 15:43:34 -08002532 }
2533 }
2534
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002535 /* Stop if a page has been captured */
2536 if (capc && capc->page) {
2537 ret = COMPACT_SUCCESS;
2538 break;
2539 }
Mel Gorman748446b2010-05-24 14:32:27 -07002540 }
2541
Mel Gormanf9e35b32011-06-15 15:08:52 -07002542out:
Vlastimil Babka6bace092014-12-10 15:43:31 -08002543 /*
2544 * Release free pages and update where the free scanner should restart,
2545 * so we don't leave any returned pages behind in the next attempt.
2546 */
2547 if (cc->nr_freepages > 0) {
2548 unsigned long free_pfn = release_freepages(&cc->freepages);
2549
2550 cc->nr_freepages = 0;
2551 VM_BUG_ON(free_pfn == 0);
2552 /* The cached pfn is always the first in a pageblock */
Vlastimil Babka06b66402016-05-19 17:11:48 -07002553 free_pfn = pageblock_start_pfn(free_pfn);
Vlastimil Babka6bace092014-12-10 15:43:31 -08002554 /*
2555 * Only go back, not forward. The cached pfn might have been
2556 * already reset to zone end in compact_finished()
2557 */
Mel Gorman40cacbc2019-03-05 15:44:36 -08002558 if (free_pfn > cc->zone->compact_cached_free_pfn)
2559 cc->zone->compact_cached_free_pfn = free_pfn;
Vlastimil Babka6bace092014-12-10 15:43:31 -08002560 }
Mel Gorman748446b2010-05-24 14:32:27 -07002561
David Rientjes7f354a52017-02-22 15:44:50 -08002562 count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
2563 count_compact_events(COMPACTFREE_SCANNED, cc->total_free_scanned);
2564
Robin Hsu1eeadb42022-05-13 10:19:50 +08002565 trace_android_vh_mm_compaction_end(cc, vendor_ret);
Joonsoo Kim16c4a092015-02-11 15:27:01 -08002566 trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
2567 cc->free_pfn, end_pfn, sync, ret);
Mel Gorman0eb927c2014-01-21 15:51:05 -08002568
Mel Gorman748446b2010-05-24 14:32:27 -07002569 return ret;
2570}
Mel Gorman76ab0f52010-05-24 14:32:28 -07002571
Michal Hockoea7ab982016-05-20 16:56:38 -07002572static enum compact_result compact_zone_order(struct zone *zone, int order,
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002573 gfp_t gfp_mask, enum compact_priority prio,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002574 unsigned int alloc_flags, int highest_zoneidx,
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002575 struct page **capture)
Mel Gorman56de7262010-05-24 14:32:30 -07002576{
Michal Hockoea7ab982016-05-20 16:56:38 -07002577 enum compact_result ret;
Mel Gorman56de7262010-05-24 14:32:30 -07002578 struct compact_control cc = {
Mel Gorman56de7262010-05-24 14:32:30 -07002579 .order = order,
Mel Gormandbe2d4e2019-03-05 15:45:31 -08002580 .search_order = order,
David Rientjes6d7ce552014-10-09 15:27:27 -07002581 .gfp_mask = gfp_mask,
Mel Gorman56de7262010-05-24 14:32:30 -07002582 .zone = zone,
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002583 .mode = (prio == COMPACT_PRIO_ASYNC) ?
2584 MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
Vlastimil Babkaebff3982014-12-10 15:43:22 -08002585 .alloc_flags = alloc_flags,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002586 .highest_zoneidx = highest_zoneidx,
Vlastimil Babkaaccf6242016-03-17 14:18:15 -07002587 .direct_compaction = true,
Vlastimil Babkaa8e025e2016-10-07 16:57:47 -07002588 .whole_zone = (prio == MIN_COMPACT_PRIORITY),
Vlastimil Babka9f7e3382016-10-07 17:00:37 -07002589 .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
2590 .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
Mel Gorman56de7262010-05-24 14:32:30 -07002591 };
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002592 struct capture_control capc = {
2593 .cc = &cc,
2594 .page = NULL,
2595 };
2596
Vlastimil Babkab9e20f02020-06-25 20:29:24 -07002597 /*
2598 * Make sure the structs are really initialized before we expose the
2599 * capture control, in case we are interrupted and the interrupt handler
2600 * frees a page.
2601 */
2602 barrier();
2603 WRITE_ONCE(current->capture_control, &capc);
Mel Gorman56de7262010-05-24 14:32:30 -07002604
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002605 ret = compact_zone(&cc, &capc);
Shaohua Lie64c5232012-10-08 16:32:27 -07002606
2607 VM_BUG_ON(!list_empty(&cc.freepages));
2608 VM_BUG_ON(!list_empty(&cc.migratepages));
2609
Vlastimil Babkab9e20f02020-06-25 20:29:24 -07002610 /*
2611 * Make sure we hide capture control first before we read the captured
2612 * page pointer, otherwise an interrupt could free and capture a page
2613 * and we would leak it.
2614 */
2615 WRITE_ONCE(current->capture_control, NULL);
2616 *capture = READ_ONCE(capc.page);
Charan Teja Reddy06dac2f2021-05-04 18:36:51 -07002617 /*
2618 * Technically, it is also possible that compaction is skipped but
2619 * the page is still captured out of luck(IRQ came and freed the page).
2620 * Returning COMPACT_SUCCESS in such cases helps in properly accounting
2621 * the COMPACT[STALL|FAIL] when compaction is skipped.
2622 */
2623 if (*capture)
2624 ret = COMPACT_SUCCESS;
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002625
Shaohua Lie64c5232012-10-08 16:32:27 -07002626 return ret;
Mel Gorman56de7262010-05-24 14:32:30 -07002627}
2628
Mel Gorman5e771902010-05-24 14:32:31 -07002629int sysctl_extfrag_threshold = 500;
2630
Mel Gorman56de7262010-05-24 14:32:30 -07002631/**
2632 * try_to_compact_pages - Direct compact to satisfy a high-order allocation
Mel Gorman56de7262010-05-24 14:32:30 -07002633 * @gfp_mask: The GFP mask of the current allocation
Vlastimil Babka1a6d53a2015-02-11 15:25:44 -08002634 * @order: The order of the current allocation
2635 * @alloc_flags: The allocation flags of the current allocation
2636 * @ac: The context of current allocation
Yang Shi112d2d22018-01-31 16:20:23 -08002637 * @prio: Determines how hard direct compaction should try to succeed
Vlastimil Babka64675522020-04-01 21:10:35 -07002638 * @capture: Pointer to free page created by compaction will be stored here
Mel Gorman56de7262010-05-24 14:32:30 -07002639 *
2640 * This is the main entry point for direct page compaction.
2641 */
Michal Hockoea7ab982016-05-20 16:56:38 -07002642enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
Mel Gormanc6038442016-05-19 17:13:38 -07002643 unsigned int alloc_flags, const struct alloc_context *ac,
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002644 enum compact_priority prio, struct page **capture)
Mel Gorman56de7262010-05-24 14:32:30 -07002645{
Mel Gorman56de7262010-05-24 14:32:30 -07002646 int may_perform_io = gfp_mask & __GFP_IO;
Mel Gorman56de7262010-05-24 14:32:30 -07002647 struct zoneref *z;
2648 struct zone *zone;
Michal Hocko1d4746d2016-05-20 16:56:44 -07002649 enum compact_result rc = COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07002650
Michal Hocko73e64c52016-12-14 15:04:07 -08002651 /*
2652 * Check if the GFP flags allow compaction - GFP_NOIO is really
2653 * tricky context because the migration might require IO
2654 */
2655 if (!may_perform_io)
Vlastimil Babka53853e22014-10-09 15:27:02 -07002656 return COMPACT_SKIPPED;
Mel Gorman56de7262010-05-24 14:32:30 -07002657
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002658 trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
Joonsoo Kim837d0262015-02-11 15:27:06 -08002659
Mel Gorman56de7262010-05-24 14:32:30 -07002660 /* Compact each zone in the list */
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002661 for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2662 ac->highest_zoneidx, ac->nodemask) {
Michal Hockoea7ab982016-05-20 16:56:38 -07002663 enum compact_result status;
Mel Gorman56de7262010-05-24 14:32:30 -07002664
Vlastimil Babkaa8e025e2016-10-07 16:57:47 -07002665 if (prio > MIN_COMPACT_PRIORITY
2666 && compaction_deferred(zone, order)) {
Michal Hocko1d4746d2016-05-20 16:56:44 -07002667 rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
Vlastimil Babka53853e22014-10-09 15:27:02 -07002668 continue;
Michal Hocko1d4746d2016-05-20 16:56:44 -07002669 }
Vlastimil Babka53853e22014-10-09 15:27:02 -07002670
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002671 status = compact_zone_order(zone, order, gfp_mask, prio,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002672 alloc_flags, ac->highest_zoneidx, capture);
Mel Gorman56de7262010-05-24 14:32:30 -07002673 rc = max(status, rc);
2674
Vlastimil Babka7ceb0092016-10-07 16:57:44 -07002675 /* The allocation should succeed, stop compacting */
2676 if (status == COMPACT_SUCCESS) {
Vlastimil Babka53853e22014-10-09 15:27:02 -07002677 /*
2678 * We think the allocation will succeed in this zone,
2679 * but it is not certain, hence the false. The caller
2680 * will repeat this with true if allocation indeed
2681 * succeeds in this zone.
2682 */
2683 compaction_defer_reset(zone, order, false);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002684
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002685 break;
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002686 }
2687
Vlastimil Babkaa5508cd2016-07-28 15:49:28 -07002688 if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002689 status == COMPACT_PARTIAL_SKIPPED))
Vlastimil Babka53853e22014-10-09 15:27:02 -07002690 /*
2691 * We think that allocation won't succeed in this zone
2692 * so we defer compaction there. If it ends up
2693 * succeeding after all, it will be reset.
2694 */
2695 defer_compaction(zone, order);
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002696
2697 /*
2698 * We might have stopped compacting due to need_resched() in
2699 * async compaction, or due to a fatal signal detected. In that
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002700 * case do not try further zones
Vlastimil Babka1f9efde2014-10-09 15:27:14 -07002701 */
Vlastimil Babkac3486f52016-07-28 15:49:30 -07002702 if ((prio == COMPACT_PRIO_ASYNC && need_resched())
2703 || fatal_signal_pending(current))
2704 break;
Mel Gorman56de7262010-05-24 14:32:30 -07002705 }
2706
2707 return rc;
2708}
2709
Nitin Guptafacdaa92020-08-11 18:31:00 -07002710/*
2711 * Compact all zones within a node till each zone's fragmentation score
2712 * reaches within proactive compaction thresholds (as determined by the
2713 * proactiveness tunable).
2714 *
2715 * It is possible that the function returns before reaching score targets
2716 * due to various back-off conditions, such as, contention on per-node or
2717 * per-zone locks.
2718 */
2719static void proactive_compact_node(pg_data_t *pgdat)
2720{
2721 int zoneid;
2722 struct zone *zone;
2723 struct compact_control cc = {
2724 .order = -1,
2725 .mode = MIGRATE_SYNC_LIGHT,
2726 .ignore_skip_hint = true,
2727 .whole_zone = true,
2728 .gfp_mask = GFP_KERNEL,
2729 .proactive_compaction = true,
2730 };
2731
2732 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2733 zone = &pgdat->node_zones[zoneid];
2734 if (!populated_zone(zone))
2735 continue;
2736
2737 cc.zone = zone;
2738
2739 compact_zone(&cc, NULL);
2740
2741 VM_BUG_ON(!list_empty(&cc.freepages));
2742 VM_BUG_ON(!list_empty(&cc.migratepages));
2743 }
2744}
Mel Gorman56de7262010-05-24 14:32:30 -07002745
Mel Gorman76ab0f52010-05-24 14:32:28 -07002746/* Compact all zones within a node */
Andrew Morton7103f162013-02-22 16:32:33 -08002747static void compact_node(int nid)
Rik van Riel7be62de2012-03-21 16:33:52 -07002748{
Vlastimil Babka791cae92016-10-07 16:57:38 -07002749 pg_data_t *pgdat = NODE_DATA(nid);
2750 int zoneid;
2751 struct zone *zone;
Rik van Riel7be62de2012-03-21 16:33:52 -07002752 struct compact_control cc = {
2753 .order = -1,
David Rientjese0b9dae2014-06-04 16:08:28 -07002754 .mode = MIGRATE_SYNC,
David Rientjes91ca9182014-04-03 14:47:23 -07002755 .ignore_skip_hint = true,
Vlastimil Babka06ed2992016-10-07 16:57:35 -07002756 .whole_zone = true,
Michal Hocko73e64c52016-12-14 15:04:07 -08002757 .gfp_mask = GFP_KERNEL,
Rik van Riel7be62de2012-03-21 16:33:52 -07002758 };
2759
Vlastimil Babka791cae92016-10-07 16:57:38 -07002760
2761 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2762
2763 zone = &pgdat->node_zones[zoneid];
2764 if (!populated_zone(zone))
2765 continue;
2766
Vlastimil Babka791cae92016-10-07 16:57:38 -07002767 cc.zone = zone;
Vlastimil Babka791cae92016-10-07 16:57:38 -07002768
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002769 compact_zone(&cc, NULL);
Vlastimil Babka791cae92016-10-07 16:57:38 -07002770
2771 VM_BUG_ON(!list_empty(&cc.freepages));
2772 VM_BUG_ON(!list_empty(&cc.migratepages));
2773 }
Rik van Riel7be62de2012-03-21 16:33:52 -07002774}
2775
Mel Gorman76ab0f52010-05-24 14:32:28 -07002776/* Compact all nodes in the system */
Jason Liu7964c062013-01-11 14:31:47 -08002777static void compact_nodes(void)
Mel Gorman76ab0f52010-05-24 14:32:28 -07002778{
2779 int nid;
2780
Hugh Dickins8575ec22012-03-21 16:33:53 -07002781 /* Flush pending updates to the LRU lists */
2782 lru_add_drain_all();
2783
Mel Gorman76ab0f52010-05-24 14:32:28 -07002784 for_each_online_node(nid)
2785 compact_node(nid);
Mel Gorman76ab0f52010-05-24 14:32:28 -07002786}
2787
Yaowei Baifec4eb22016-01-14 15:20:09 -08002788/*
Nitin Guptafacdaa92020-08-11 18:31:00 -07002789 * Tunable for proactive compaction. It determines how
2790 * aggressively the kernel should compact memory in the
2791 * background. It takes values in the range [0, 100].
2792 */
Nitin Guptad34c0a72020-08-11 18:31:07 -07002793unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
Nitin Guptafacdaa92020-08-11 18:31:00 -07002794
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07002795int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
2796 void *buffer, size_t *length, loff_t *ppos)
2797{
2798 int rc, nid;
2799
2800 rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
2801 if (rc)
2802 return rc;
2803
2804 if (write && sysctl_compaction_proactiveness) {
2805 for_each_online_node(nid) {
2806 pg_data_t *pgdat = NODE_DATA(nid);
2807
2808 if (pgdat->proactive_compact_trigger)
2809 continue;
2810
2811 pgdat->proactive_compact_trigger = true;
2812 wake_up_interruptible(&pgdat->kcompactd_wait);
2813 }
2814 }
2815
2816 return 0;
2817}
2818
Nitin Guptafacdaa92020-08-11 18:31:00 -07002819/*
Yaowei Baifec4eb22016-01-14 15:20:09 -08002820 * This is the entry point for compacting all nodes via
2821 * /proc/sys/vm/compact_memory
2822 */
Mel Gorman76ab0f52010-05-24 14:32:28 -07002823int sysctl_compaction_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02002824 void *buffer, size_t *length, loff_t *ppos)
Mel Gorman76ab0f52010-05-24 14:32:28 -07002825{
2826 if (write)
Jason Liu7964c062013-01-11 14:31:47 -08002827 compact_nodes();
Mel Gorman76ab0f52010-05-24 14:32:28 -07002828
2829 return 0;
2830}
Mel Gormaned4a6d72010-05-24 14:32:29 -07002831
2832#if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
YueHaibing17adb232021-06-30 18:50:48 -07002833static ssize_t compact_store(struct device *dev,
2834 struct device_attribute *attr,
2835 const char *buf, size_t count)
Mel Gormaned4a6d72010-05-24 14:32:29 -07002836{
Hugh Dickins8575ec22012-03-21 16:33:53 -07002837 int nid = dev->id;
2838
2839 if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
2840 /* Flush pending updates to the LRU lists */
2841 lru_add_drain_all();
2842
2843 compact_node(nid);
2844 }
Mel Gormaned4a6d72010-05-24 14:32:29 -07002845
2846 return count;
2847}
YueHaibing17adb232021-06-30 18:50:48 -07002848static DEVICE_ATTR_WO(compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07002849
2850int compaction_register_node(struct node *node)
2851{
Kay Sievers10fbcf42011-12-21 14:48:43 -08002852 return device_create_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07002853}
2854
2855void compaction_unregister_node(struct node *node)
2856{
Kay Sievers10fbcf42011-12-21 14:48:43 -08002857 return device_remove_file(&node->dev, &dev_attr_compact);
Mel Gormaned4a6d72010-05-24 14:32:29 -07002858}
2859#endif /* CONFIG_SYSFS && CONFIG_NUMA */
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01002860
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002861static inline bool kcompactd_work_requested(pg_data_t *pgdat)
2862{
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07002863 return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
2864 pgdat->proactive_compact_trigger;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002865}
2866
2867static bool kcompactd_node_suitable(pg_data_t *pgdat)
2868{
2869 int zoneid;
2870 struct zone *zone;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002871 enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002872
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002873 for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002874 zone = &pgdat->node_zones[zoneid];
2875
2876 if (!populated_zone(zone))
2877 continue;
2878
2879 if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002880 highest_zoneidx) == COMPACT_CONTINUE)
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002881 return true;
2882 }
2883
2884 return false;
2885}
2886
2887static void kcompactd_do_work(pg_data_t *pgdat)
2888{
2889 /*
2890 * With no special task, compact all zones so that a page of requested
2891 * order is allocatable.
2892 */
2893 int zoneid;
2894 struct zone *zone;
2895 struct compact_control cc = {
2896 .order = pgdat->kcompactd_max_order,
Mel Gormandbe2d4e2019-03-05 15:45:31 -08002897 .search_order = pgdat->kcompactd_max_order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002898 .highest_zoneidx = pgdat->kcompactd_highest_zoneidx,
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002899 .mode = MIGRATE_SYNC_LIGHT,
David Rientjesa0647dc2017-11-17 15:26:27 -08002900 .ignore_skip_hint = false,
Michal Hocko73e64c52016-12-14 15:04:07 -08002901 .gfp_mask = GFP_KERNEL,
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002902 };
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002903 trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002904 cc.highest_zoneidx);
David Rientjes7f354a52017-02-22 15:44:50 -08002905 count_compact_event(KCOMPACTD_WAKE);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002906
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002907 for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002908 int status;
2909
2910 zone = &pgdat->node_zones[zoneid];
2911 if (!populated_zone(zone))
2912 continue;
2913
2914 if (compaction_deferred(zone, cc.order))
2915 continue;
2916
2917 if (compaction_suitable(zone, cc.order, 0, zoneid) !=
2918 COMPACT_CONTINUE)
2919 continue;
2920
Vlastimil Babka172400c2016-05-05 16:22:32 -07002921 if (kthread_should_stop())
2922 return;
Yafang Shaoa94b5252019-09-23 15:36:54 -07002923
2924 cc.zone = zone;
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002925 status = compact_zone(&cc, NULL);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002926
Vlastimil Babka7ceb0092016-10-07 16:57:44 -07002927 if (status == COMPACT_SUCCESS) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002928 compaction_defer_reset(zone, cc.order, false);
Michal Hockoc8f7de02016-05-20 16:56:47 -07002929 } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002930 /*
David Rientjesbc3106b2018-04-05 16:24:02 -07002931 * Buddy pages may become stranded on pcps that could
2932 * otherwise coalesce on the zone's free area for
2933 * order >= cc.order. This is ratelimited by the
2934 * upcoming deferral.
2935 */
2936 drain_all_pages(zone);
2937
2938 /*
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002939 * We use sync migration mode here, so we defer like
2940 * sync direct compaction does.
2941 */
2942 defer_compaction(zone, cc.order);
2943 }
2944
David Rientjes7f354a52017-02-22 15:44:50 -08002945 count_compact_events(KCOMPACTD_MIGRATE_SCANNED,
2946 cc.total_migrate_scanned);
2947 count_compact_events(KCOMPACTD_FREE_SCANNED,
2948 cc.total_free_scanned);
2949
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002950 VM_BUG_ON(!list_empty(&cc.freepages));
2951 VM_BUG_ON(!list_empty(&cc.migratepages));
2952 }
2953
2954 /*
2955 * Regardless of success, we are done until woken up next. But remember
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002956 * the requested order/highest_zoneidx in case it was higher/tighter
2957 * than our current ones
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002958 */
2959 if (pgdat->kcompactd_max_order <= cc.order)
2960 pgdat->kcompactd_max_order = 0;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002961 if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx)
2962 pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002963}
2964
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002965void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx)
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002966{
2967 if (!order)
2968 return;
2969
2970 if (pgdat->kcompactd_max_order < order)
2971 pgdat->kcompactd_max_order = order;
2972
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002973 if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx)
2974 pgdat->kcompactd_highest_zoneidx = highest_zoneidx;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002975
Davidlohr Bueso68186002017-10-03 16:15:03 -07002976 /*
2977 * Pairs with implicit barrier in wait_event_freezable()
2978 * such that wakeups are not missed.
2979 */
2980 if (!wq_has_sleeper(&pgdat->kcompactd_wait))
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002981 return;
2982
2983 if (!kcompactd_node_suitable(pgdat))
2984 return;
2985
2986 trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
Joonsoo Kim97a225e2020-06-03 15:59:01 -07002987 highest_zoneidx);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002988 wake_up_interruptible(&pgdat->kcompactd_wait);
2989}
2990
2991/*
2992 * The background compaction daemon, started as a kernel thread
2993 * from the init process.
2994 */
2995static int kcompactd(void *p)
2996{
Zhiyuan Dai68d68ff2021-05-04 18:40:12 -07002997 pg_data_t *pgdat = (pg_data_t *)p;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07002998 struct task_struct *tsk = current;
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07002999 long default_timeout = msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC);
3000 long timeout = default_timeout;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003001
3002 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
3003
3004 if (!cpumask_empty(cpumask))
3005 set_cpus_allowed_ptr(tsk, cpumask);
3006
3007 set_freezable();
3008
3009 pgdat->kcompactd_max_order = 0;
Joonsoo Kim97a225e2020-06-03 15:59:01 -07003010 pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003011
3012 while (!kthread_should_stop()) {
Johannes Weinereb414682018-10-26 15:06:27 -07003013 unsigned long pflags;
3014
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07003015 /*
3016 * Avoid the unnecessary wakeup for proactive compaction
3017 * when it is disabled.
3018 */
3019 if (!sysctl_compaction_proactiveness)
3020 timeout = MAX_SCHEDULE_TIMEOUT;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003021 trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
Nitin Guptafacdaa92020-08-11 18:31:00 -07003022 if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07003023 kcompactd_work_requested(pgdat), timeout) &&
3024 !pgdat->proactive_compact_trigger) {
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003025
Nitin Guptafacdaa92020-08-11 18:31:00 -07003026 psi_memstall_enter(&pflags);
3027 kcompactd_do_work(pgdat);
3028 psi_memstall_leave(&pflags);
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07003029 /*
3030 * Reset the timeout value. The defer timeout from
3031 * proactive compaction is lost here but that is fine
3032 * as the condition of the zone changing substantionally
3033 * then carrying on with the previous defer interval is
3034 * not useful.
3035 */
3036 timeout = default_timeout;
Nitin Guptafacdaa92020-08-11 18:31:00 -07003037 continue;
3038 }
3039
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07003040 /*
3041 * Start the proactive work with default timeout. Based
3042 * on the fragmentation score, this timeout is updated.
3043 */
3044 timeout = default_timeout;
Nitin Guptafacdaa92020-08-11 18:31:00 -07003045 if (should_proactive_compact_node(pgdat)) {
3046 unsigned int prev_score, score;
3047
Nitin Guptafacdaa92020-08-11 18:31:00 -07003048 prev_score = fragmentation_score_node(pgdat);
3049 proactive_compact_node(pgdat);
3050 score = fragmentation_score_node(pgdat);
3051 /*
3052 * Defer proactive compaction if the fragmentation
3053 * score did not go down i.e. no progress made.
3054 */
Charan Teja Reddye1e92bf2021-09-02 14:59:56 -07003055 if (unlikely(score >= prev_score))
3056 timeout =
3057 default_timeout << COMPACT_MAX_DEFER_SHIFT;
Nitin Guptafacdaa92020-08-11 18:31:00 -07003058 }
Charan Teja Reddy65d759c2021-09-02 14:59:59 -07003059 if (unlikely(pgdat->proactive_compact_trigger))
3060 pgdat->proactive_compact_trigger = false;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003061 }
3062
3063 return 0;
3064}
3065
3066/*
3067 * This kcompactd start function will be called by init and node-hot-add.
3068 * On node-hot-add, kcompactd will moved to proper cpus if cpus are hot-added.
3069 */
3070int kcompactd_run(int nid)
3071{
3072 pg_data_t *pgdat = NODE_DATA(nid);
3073 int ret = 0;
3074
3075 if (pgdat->kcompactd)
3076 return 0;
3077
3078 pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
3079 if (IS_ERR(pgdat->kcompactd)) {
3080 pr_err("Failed to start kcompactd on node %d\n", nid);
3081 ret = PTR_ERR(pgdat->kcompactd);
3082 pgdat->kcompactd = NULL;
3083 }
3084 return ret;
3085}
3086
3087/*
3088 * Called by memory hotplug when all memory in a node is offlined. Caller must
3089 * hold mem_hotplug_begin/end().
3090 */
3091void kcompactd_stop(int nid)
3092{
3093 struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
3094
3095 if (kcompactd) {
3096 kthread_stop(kcompactd);
3097 NODE_DATA(nid)->kcompactd = NULL;
3098 }
3099}
3100
3101/*
3102 * It's optimal to keep kcompactd on the same CPUs as their memory, but
3103 * not required for correctness. So if the last cpu in a node goes
3104 * away, we get changed to run anywhere: as the first one comes back,
3105 * restore their cpu bindings.
3106 */
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003107static int kcompactd_cpu_online(unsigned int cpu)
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003108{
3109 int nid;
3110
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003111 for_each_node_state(nid, N_MEMORY) {
3112 pg_data_t *pgdat = NODE_DATA(nid);
3113 const struct cpumask *mask;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003114
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003115 mask = cpumask_of_node(pgdat->node_id);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003116
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003117 if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
3118 /* One of our CPUs online: restore mask */
3119 set_cpus_allowed_ptr(pgdat->kcompactd, mask);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003120 }
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003121 return 0;
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003122}
3123
3124static int __init kcompactd_init(void)
3125{
3126 int nid;
Anna-Maria Gleixnere46b1db2016-11-27 00:13:42 +01003127 int ret;
3128
3129 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
3130 "mm/compaction:online",
3131 kcompactd_cpu_online, NULL);
3132 if (ret < 0) {
3133 pr_err("kcompactd: failed to register hotplug callbacks.\n");
3134 return ret;
3135 }
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003136
3137 for_each_node_state(nid, N_MEMORY)
3138 kcompactd_run(nid);
Vlastimil Babka698b1b32016-03-17 14:18:08 -07003139 return 0;
3140}
3141subsys_initcall(kcompactd_init)
3142
Michal Nazarewiczff9543f2011-12-29 13:09:50 +01003143#endif /* CONFIG_COMPACTION */