forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/mm/compaction.c
....@@ -50,6 +50,24 @@
5050 #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order)
5151 #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order)
5252
53
+/*
54
+ * Fragmentation score check interval for proactive compaction purposes.
55
+ */
56
+static const unsigned int HPAGE_FRAG_CHECK_INTERVAL_MSEC = 500;
57
+
58
+/*
59
+ * Page order with-respect-to which proactive compaction
60
+ * calculates external fragmentation, which is used as
61
+ * the "fragmentation score" of a node/zone.
62
+ */
63
+#if defined CONFIG_TRANSPARENT_HUGEPAGE
64
+#define COMPACTION_HPAGE_ORDER HPAGE_PMD_ORDER
65
+#elif defined CONFIG_HUGETLBFS
66
+#define COMPACTION_HPAGE_ORDER HUGETLB_PAGE_ORDER
67
+#else
68
+#define COMPACTION_HPAGE_ORDER (PMD_SHIFT - PAGE_SHIFT)
69
+#endif
70
+
5371 static unsigned long release_freepages(struct list_head *freelist)
5472 {
5573 struct page *page, *next;
....@@ -66,7 +84,7 @@
6684 return high_pfn;
6785 }
6886
69
-static void map_pages(struct list_head *list)
87
+static void split_map_pages(struct list_head *list)
7088 {
7189 unsigned int i, order, nr_pages;
7290 struct page *page, *next;
....@@ -136,7 +154,7 @@
136154
137155 /*
138156 * Compaction is deferred when compaction fails to result in a page
139
- * allocation success. 1 << compact_defer_limit compactions are skipped up
157
+ * allocation success. 1 << compact_defer_shift, compactions are skipped up
140158 * to a limit of 1 << COMPACT_MAX_DEFER_SHIFT
141159 */
142160 void defer_compaction(struct zone *zone, int order)
....@@ -162,11 +180,10 @@
162180 return false;
163181
164182 /* Avoid possible overflow */
165
- if (++zone->compact_considered > defer_limit)
183
+ if (++zone->compact_considered >= defer_limit) {
166184 zone->compact_considered = defer_limit;
167
-
168
- if (zone->compact_considered >= defer_limit)
169185 return false;
186
+ }
170187
171188 trace_mm_compaction_deferred(zone, order);
172189
....@@ -237,6 +254,78 @@
237254 return false;
238255 }
239256
257
+static bool
258
+__reset_isolation_pfn(struct zone *zone, unsigned long pfn, bool check_source,
259
+ bool check_target)
260
+{
261
+ struct page *page = pfn_to_online_page(pfn);
262
+ struct page *block_page;
263
+ struct page *end_page;
264
+ unsigned long block_pfn;
265
+
266
+ if (!page)
267
+ return false;
268
+ if (zone != page_zone(page))
269
+ return false;
270
+ if (pageblock_skip_persistent(page))
271
+ return false;
272
+
273
+ /*
274
+ * If skip is already cleared do no further checking once the
275
+ * restart points have been set.
276
+ */
277
+ if (check_source && check_target && !get_pageblock_skip(page))
278
+ return true;
279
+
280
+ /*
281
+ * If clearing skip for the target scanner, do not select a
282
+ * non-movable pageblock as the starting point.
283
+ */
284
+ if (!check_source && check_target &&
285
+ get_pageblock_migratetype(page) != MIGRATE_MOVABLE)
286
+ return false;
287
+
288
+ /* Ensure the start of the pageblock or zone is online and valid */
289
+ block_pfn = pageblock_start_pfn(pfn);
290
+ block_pfn = max(block_pfn, zone->zone_start_pfn);
291
+ block_page = pfn_to_online_page(block_pfn);
292
+ if (block_page) {
293
+ page = block_page;
294
+ pfn = block_pfn;
295
+ }
296
+
297
+ /* Ensure the end of the pageblock or zone is online and valid */
298
+ block_pfn = pageblock_end_pfn(pfn) - 1;
299
+ block_pfn = min(block_pfn, zone_end_pfn(zone) - 1);
300
+ end_page = pfn_to_online_page(block_pfn);
301
+ if (!end_page)
302
+ return false;
303
+
304
+ /*
305
+ * Only clear the hint if a sample indicates there is either a
306
+ * free page or an LRU page in the block. One or other condition
307
+ * is necessary for the block to be a migration source/target.
308
+ */
309
+ do {
310
+ if (pfn_valid_within(pfn)) {
311
+ if (check_source && PageLRU(page)) {
312
+ clear_pageblock_skip(page);
313
+ return true;
314
+ }
315
+
316
+ if (check_target && PageBuddy(page)) {
317
+ clear_pageblock_skip(page);
318
+ return true;
319
+ }
320
+ }
321
+
322
+ page += (1 << PAGE_ALLOC_COSTLY_ORDER);
323
+ pfn += (1 << PAGE_ALLOC_COSTLY_ORDER);
324
+ } while (page <= end_page);
325
+
326
+ return false;
327
+}
328
+
240329 /*
241330 * This function is called to clear all cached information on pageblocks that
242331 * should be skipped for page isolation when the migrate and free page scanner
....@@ -244,30 +333,54 @@
244333 */
245334 static void __reset_isolation_suitable(struct zone *zone)
246335 {
247
- unsigned long start_pfn = zone->zone_start_pfn;
248
- unsigned long end_pfn = zone_end_pfn(zone);
249
- unsigned long pfn;
336
+ unsigned long migrate_pfn = zone->zone_start_pfn;
337
+ unsigned long free_pfn = zone_end_pfn(zone) - 1;
338
+ unsigned long reset_migrate = free_pfn;
339
+ unsigned long reset_free = migrate_pfn;
340
+ bool source_set = false;
341
+ bool free_set = false;
342
+
343
+ if (!zone->compact_blockskip_flush)
344
+ return;
250345
251346 zone->compact_blockskip_flush = false;
252347
253
- /* Walk the zone and mark every pageblock as suitable for isolation */
254
- for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
255
- struct page *page;
256
-
348
+ /*
349
+ * Walk the zone and update pageblock skip information. Source looks
350
+ * for PageLRU while target looks for PageBuddy. When the scanner
351
+ * is found, both PageBuddy and PageLRU are checked as the pageblock
352
+ * is suitable as both source and target.
353
+ */
354
+ for (; migrate_pfn < free_pfn; migrate_pfn += pageblock_nr_pages,
355
+ free_pfn -= pageblock_nr_pages) {
257356 cond_resched();
258357
259
- page = pfn_to_online_page(pfn);
260
- if (!page)
261
- continue;
262
- if (zone != page_zone(page))
263
- continue;
264
- if (pageblock_skip_persistent(page))
265
- continue;
358
+ /* Update the migrate PFN */
359
+ if (__reset_isolation_pfn(zone, migrate_pfn, true, source_set) &&
360
+ migrate_pfn < reset_migrate) {
361
+ source_set = true;
362
+ reset_migrate = migrate_pfn;
363
+ zone->compact_init_migrate_pfn = reset_migrate;
364
+ zone->compact_cached_migrate_pfn[0] = reset_migrate;
365
+ zone->compact_cached_migrate_pfn[1] = reset_migrate;
366
+ }
266367
267
- clear_pageblock_skip(page);
368
+ /* Update the free PFN */
369
+ if (__reset_isolation_pfn(zone, free_pfn, free_set, true) &&
370
+ free_pfn > reset_free) {
371
+ free_set = true;
372
+ reset_free = free_pfn;
373
+ zone->compact_init_free_pfn = reset_free;
374
+ zone->compact_cached_free_pfn = reset_free;
375
+ }
268376 }
269377
270
- reset_cached_positions(zone);
378
+ /* Leave no distance if no suitable block was reset */
379
+ if (reset_migrate >= reset_free) {
380
+ zone->compact_cached_migrate_pfn[0] = migrate_pfn;
381
+ zone->compact_cached_migrate_pfn[1] = migrate_pfn;
382
+ zone->compact_cached_free_pfn = free_pfn;
383
+ }
271384 }
272385
273386 void reset_isolation_suitable(pg_data_t *pgdat)
....@@ -286,15 +399,53 @@
286399 }
287400
288401 /*
402
+ * Sets the pageblock skip bit if it was clear. Note that this is a hint as
403
+ * locks are not required for read/writers. Returns true if it was already set.
404
+ */
405
+static bool test_and_set_skip(struct compact_control *cc, struct page *page,
406
+ unsigned long pfn)
407
+{
408
+ bool skip;
409
+
410
+ /* Do no update if skip hint is being ignored */
411
+ if (cc->ignore_skip_hint)
412
+ return false;
413
+
414
+ if (!IS_ALIGNED(pfn, pageblock_nr_pages))
415
+ return false;
416
+
417
+ skip = get_pageblock_skip(page);
418
+ if (!skip && !cc->no_set_skip_hint)
419
+ set_pageblock_skip(page);
420
+
421
+ return skip;
422
+}
423
+
424
+static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
425
+{
426
+ struct zone *zone = cc->zone;
427
+
428
+ pfn = pageblock_end_pfn(pfn);
429
+
430
+ /* Set for isolation rather than compaction */
431
+ if (cc->no_set_skip_hint)
432
+ return;
433
+
434
+ if (pfn > zone->compact_cached_migrate_pfn[0])
435
+ zone->compact_cached_migrate_pfn[0] = pfn;
436
+ if (cc->mode != MIGRATE_ASYNC &&
437
+ pfn > zone->compact_cached_migrate_pfn[1])
438
+ zone->compact_cached_migrate_pfn[1] = pfn;
439
+}
440
+
441
+/*
289442 * If no pages were isolated then mark this pageblock to be skipped in the
290443 * future. The information is later cleared by __reset_isolation_suitable().
291444 */
292445 static void update_pageblock_skip(struct compact_control *cc,
293
- struct page *page, unsigned long nr_isolated,
294
- bool migrate_scanner)
446
+ struct page *page, unsigned long pfn)
295447 {
296448 struct zone *zone = cc->zone;
297
- unsigned long pfn;
298449
299450 if (cc->no_set_skip_hint)
300451 return;
....@@ -302,24 +453,11 @@
302453 if (!page)
303454 return;
304455
305
- if (nr_isolated)
306
- return;
307
-
308456 set_pageblock_skip(page);
309457
310
- pfn = page_to_pfn(page);
311
-
312458 /* Update where async and sync compaction should restart */
313
- if (migrate_scanner) {
314
- if (pfn > zone->compact_cached_migrate_pfn[0])
315
- zone->compact_cached_migrate_pfn[0] = pfn;
316
- if (cc->mode != MIGRATE_ASYNC &&
317
- pfn > zone->compact_cached_migrate_pfn[1])
318
- zone->compact_cached_migrate_pfn[1] = pfn;
319
- } else {
320
- if (pfn < zone->compact_cached_free_pfn)
321
- zone->compact_cached_free_pfn = pfn;
322
- }
459
+ if (pfn < zone->compact_cached_free_pfn)
460
+ zone->compact_cached_free_pfn = pfn;
323461 }
324462 #else
325463 static inline bool isolation_suitable(struct compact_control *cc,
....@@ -334,32 +472,43 @@
334472 }
335473
336474 static inline void update_pageblock_skip(struct compact_control *cc,
337
- struct page *page, unsigned long nr_isolated,
338
- bool migrate_scanner)
475
+ struct page *page, unsigned long pfn)
339476 {
477
+}
478
+
479
+static void update_cached_migrate(struct compact_control *cc, unsigned long pfn)
480
+{
481
+}
482
+
483
+static bool test_and_set_skip(struct compact_control *cc, struct page *page,
484
+ unsigned long pfn)
485
+{
486
+ return false;
340487 }
341488 #endif /* CONFIG_COMPACTION */
342489
343490 /*
344491 * Compaction requires the taking of some coarse locks that are potentially
345
- * very heavily contended. For async compaction, back out if the lock cannot
346
- * be taken immediately. For sync compaction, spin on the lock if needed.
492
+ * very heavily contended. For async compaction, trylock and record if the
493
+ * lock is contended. The lock will still be acquired but compaction will
494
+ * abort when the current block is finished regardless of success rate.
495
+ * Sync compaction acquires the lock.
347496 *
348
- * Returns true if the lock is held
349
- * Returns false if the lock is not held and compaction should abort
497
+ * Always returns true which makes it easier to track lock state in callers.
350498 */
351
-static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
499
+static bool compact_lock_irqsave(spinlock_t *lock, unsigned long *flags,
352500 struct compact_control *cc)
501
+ __acquires(lock)
353502 {
354
- if (cc->mode == MIGRATE_ASYNC) {
355
- if (!spin_trylock_irqsave(lock, *flags)) {
356
- cc->contended = true;
357
- return false;
358
- }
359
- } else {
360
- spin_lock_irqsave(lock, *flags);
503
+ /* Track if the lock is contended in async mode */
504
+ if (cc->mode == MIGRATE_ASYNC && !cc->contended) {
505
+ if (spin_trylock_irqsave(lock, *flags))
506
+ return true;
507
+
508
+ cc->contended = true;
361509 }
362510
511
+ spin_lock_irqsave(lock, *flags);
363512 return true;
364513 }
365514
....@@ -391,37 +540,7 @@
391540 return true;
392541 }
393542
394
- if (need_resched()) {
395
- if (cc->mode == MIGRATE_ASYNC) {
396
- cc->contended = true;
397
- return true;
398
- }
399
- cond_resched();
400
- }
401
-
402
- return false;
403
-}
404
-
405
-/*
406
- * Aside from avoiding lock contention, compaction also periodically checks
407
- * need_resched() and either schedules in sync compaction or aborts async
408
- * compaction. This is similar to what compact_unlock_should_abort() does, but
409
- * is used where no lock is concerned.
410
- *
411
- * Returns false when no scheduling was needed, or sync compaction scheduled.
412
- * Returns true when async compaction should abort.
413
- */
414
-static inline bool compact_should_abort(struct compact_control *cc)
415
-{
416
- /* async compaction aborts if contended */
417
- if (need_resched()) {
418
- if (cc->mode == MIGRATE_ASYNC) {
419
- cc->contended = true;
420
- return true;
421
- }
422
-
423
- cond_resched();
424
- }
543
+ cond_resched();
425544
426545 return false;
427546 }
....@@ -435,19 +554,24 @@
435554 unsigned long *start_pfn,
436555 unsigned long end_pfn,
437556 struct list_head *freelist,
557
+ unsigned int stride,
438558 bool strict)
439559 {
440560 int nr_scanned = 0, total_isolated = 0;
441
- struct page *cursor, *valid_page = NULL;
561
+ struct page *cursor;
442562 unsigned long flags = 0;
443563 bool locked = false;
444564 unsigned long blockpfn = *start_pfn;
445565 unsigned int order;
446566
567
+ /* Strict mode is for isolation, speed is secondary */
568
+ if (strict)
569
+ stride = 1;
570
+
447571 cursor = pfn_to_page(blockpfn);
448572
449573 /* Isolate free pages. */
450
- for (; blockpfn < end_pfn; blockpfn++, cursor++) {
574
+ for (; blockpfn < end_pfn; blockpfn += stride, cursor += stride) {
451575 int isolated;
452576 struct page *page = cursor;
453577
....@@ -464,9 +588,6 @@
464588 nr_scanned++;
465589 if (!pfn_valid_within(blockpfn))
466590 goto isolate_fail;
467
-
468
- if (!valid_page)
469
- valid_page = page;
470591
471592 /*
472593 * For compound pages such as THP and hugetlbfs, we can save
....@@ -495,18 +616,8 @@
495616 * recheck as well.
496617 */
497618 if (!locked) {
498
- /*
499
- * The zone lock must be held to isolate freepages.
500
- * Unfortunately this is a very coarse lock and can be
501
- * heavily contended if there are parallel allocations
502
- * or parallel compactions. For async compaction do not
503
- * spin on the lock and we acquire the lock as late as
504
- * possible.
505
- */
506
- locked = compact_trylock_irqsave(&cc->zone->lock,
619
+ locked = compact_lock_irqsave(&cc->zone->lock,
507620 &flags, cc);
508
- if (!locked)
509
- break;
510621
511622 /* Recheck this is a buddy page under lock */
512623 if (!PageBuddy(page))
....@@ -514,7 +625,7 @@
514625 }
515626
516627 /* Found a free page, will break it into order-0 pages */
517
- order = page_order(page);
628
+ order = buddy_order(page);
518629 isolated = __isolate_free_page(page, order);
519630 if (!isolated)
520631 break;
....@@ -564,10 +675,6 @@
564675 */
565676 if (strict && blockpfn < end_pfn)
566677 total_isolated = 0;
567
-
568
- /* Update the pageblock-skip if the whole pageblock was scanned */
569
- if (blockpfn == end_pfn)
570
- update_pageblock_skip(cc, valid_page, total_isolated, false);
571678
572679 cc->total_free_scanned += nr_scanned;
573680 if (total_isolated)
....@@ -626,7 +733,7 @@
626733 break;
627734
628735 isolated = isolate_freepages_block(cc, &isolate_start_pfn,
629
- block_end_pfn, &freelist, true);
736
+ block_end_pfn, &freelist, 0, true);
630737
631738 /*
632739 * In strict mode, isolate_freepages_block() returns 0 if
....@@ -644,7 +751,7 @@
644751 }
645752
646753 /* __isolate_free_page() does not map the pages */
647
- map_pages(&freelist);
754
+ split_map_pages(&freelist);
648755
649756 if (pfn < end_pfn) {
650757 /* Loop terminated early, cleanup. */
....@@ -656,17 +763,42 @@
656763 return pfn;
657764 }
658765
766
+#ifdef CONFIG_COMPACTION
767
+unsigned 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
+}
788
+EXPORT_SYMBOL_GPL(isolate_and_split_free_page);
789
+#endif
790
+
659791 /* Similar to reclaim, but different enough that they don't share logic */
660
-static bool too_many_isolated(struct zone *zone)
792
+static bool too_many_isolated(pg_data_t *pgdat)
661793 {
662794 unsigned long active, inactive, isolated;
663795
664
- inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) +
665
- node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON);
666
- active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) +
667
- node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON);
668
- isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) +
669
- node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON);
796
+ 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);
670802
671803 return isolated > (inactive + active) / 2;
672804 }
....@@ -693,7 +825,7 @@
693825 isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
694826 unsigned long end_pfn, isolate_mode_t isolate_mode)
695827 {
696
- struct zone *zone = cc->zone;
828
+ pg_data_t *pgdat = cc->zone->zone_pgdat;
697829 unsigned long nr_scanned = 0, nr_isolated = 0;
698830 struct lruvec *lruvec;
699831 unsigned long flags = 0;
....@@ -702,13 +834,18 @@
702834 unsigned long start_pfn = low_pfn;
703835 bool skip_on_failure = false;
704836 unsigned long next_skip_pfn = 0;
837
+ bool skip_updated = false;
705838
706839 /*
707840 * Ensure that there are not too many pages isolated from the LRU
708841 * list by either parallel reclaimers or compaction. If there are,
709842 * delay for some time until fewer pages are isolated
710843 */
711
- while (unlikely(too_many_isolated(zone))) {
844
+ while (unlikely(too_many_isolated(pgdat))) {
845
+ /* stop isolation if there are still pages not migrated */
846
+ if (cc->nr_migratepages)
847
+ return 0;
848
+
712849 /* async migration should just abort */
713850 if (cc->mode == MIGRATE_ASYNC)
714851 return 0;
....@@ -719,8 +856,7 @@
719856 return 0;
720857 }
721858
722
- if (compact_should_abort(cc))
723
- return 0;
859
+ cond_resched();
724860
725861 if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
726862 skip_on_failure = true;
....@@ -754,13 +890,15 @@
754890
755891 /*
756892 * Periodically drop the lock (if held) regardless of its
757
- * contention, to give chance to IRQs. Abort async compaction
758
- * if contended.
893
+ * contention, to give chance to IRQs. Abort completely if
894
+ * a fatal signal is pending.
759895 */
760896 if (!(low_pfn % SWAP_CLUSTER_MAX)
761
- && compact_unlock_should_abort(zone_lru_lock(zone), flags,
762
- &locked, cc))
763
- break;
897
+ && compact_unlock_should_abort(&pgdat->lru_lock,
898
+ flags, &locked, cc)) {
899
+ low_pfn = 0;
900
+ goto fatal_pending;
901
+ }
764902
765903 if (!pfn_valid_within(low_pfn))
766904 goto isolate_fail;
....@@ -768,8 +906,19 @@
768906
769907 page = pfn_to_page(low_pfn);
770908
771
- if (!valid_page)
909
+ /*
910
+ * Check if the pageblock has already been marked skipped.
911
+ * Only the aligned PFN is checked as the caller isolates
912
+ * COMPACT_CLUSTER_MAX at a time so the second call must
913
+ * not falsely conclude that the block should be skipped.
914
+ */
915
+ if (!valid_page && IS_ALIGNED(low_pfn, pageblock_nr_pages)) {
916
+ if (!cc->ignore_skip_hint && get_pageblock_skip(page)) {
917
+ low_pfn = end_pfn;
918
+ goto isolate_abort;
919
+ }
772920 valid_page = page;
921
+ }
773922
774923 /*
775924 * Skip if free. We read page order here without zone lock
....@@ -778,7 +927,7 @@
778927 * potential isolation targets.
779928 */
780929 if (PageBuddy(page)) {
781
- unsigned long freepage_order = page_order_unsafe(page);
930
+ unsigned long freepage_order = buddy_order_unsafe(page);
782931
783932 /*
784933 * Without lock, we cannot be sure that what we got is
....@@ -792,12 +941,13 @@
792941
793942 /*
794943 * Regardless of being on LRU, compound pages such as THP and
795
- * hugetlbfs are not to be compacted. We can potentially save
796
- * a lot of iterations if we skip them at once. The check is
797
- * racy, but we can consider only valid values and the only
798
- * danger is skipping too much.
944
+ * hugetlbfs are not to be compacted unless we are attempting
945
+ * an allocation much larger than the huge page size (eg CMA).
946
+ * We can potentially save a lot of iterations if we skip them
947
+ * at once. The check is racy, but we can consider only valid
948
+ * values and the only danger is skipping too much.
799949 */
800
- if (PageCompound(page)) {
950
+ if (PageCompound(page) && !cc->alloc_contig) {
801951 const unsigned int order = compound_order(page);
802952
803953 if (likely(order < MAX_ORDER))
....@@ -818,7 +968,7 @@
818968 if (unlikely(__PageMovable(page)) &&
819969 !PageIsolated(page)) {
820970 if (locked) {
821
- spin_unlock_irqrestore(zone_lru_lock(zone),
971
+ spin_unlock_irqrestore(&pgdat->lru_lock,
822972 flags);
823973 locked = false;
824974 }
....@@ -848,10 +998,15 @@
848998
849999 /* If we already hold the lock, we can skip some rechecking */
8501000 if (!locked) {
851
- locked = compact_trylock_irqsave(zone_lru_lock(zone),
1001
+ locked = compact_lock_irqsave(&pgdat->lru_lock,
8521002 &flags, cc);
853
- if (!locked)
854
- break;
1003
+
1004
+ /* Try get exclusive access under lock */
1005
+ if (!skip_updated) {
1006
+ skip_updated = true;
1007
+ if (test_and_set_skip(cc, page, low_pfn))
1008
+ goto isolate_abort;
1009
+ }
8551010
8561011 /* Recheck PageLRU and PageCompound under lock */
8571012 if (!PageLRU(page))
....@@ -862,41 +1017,41 @@
8621017 * and it's on LRU. It can only be a THP so the order
8631018 * is safe to read and it's 0 for tail pages.
8641019 */
865
- if (unlikely(PageCompound(page))) {
866
- low_pfn += (1UL << compound_order(page)) - 1;
1020
+ if (unlikely(PageCompound(page) && !cc->alloc_contig)) {
1021
+ low_pfn += compound_nr(page) - 1;
8671022 goto isolate_fail;
8681023 }
8691024 }
8701025
871
- lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
1026
+ lruvec = mem_cgroup_page_lruvec(page, pgdat);
8721027
8731028 /* Try isolate the page */
8741029 if (__isolate_lru_page(page, isolate_mode) != 0)
8751030 goto isolate_fail;
8761031
877
- VM_BUG_ON_PAGE(PageCompound(page), page);
1032
+ /* The whole page is taken off the LRU; skip the tail pages. */
1033
+ if (PageCompound(page))
1034
+ low_pfn += compound_nr(page) - 1;
8781035
8791036 /* Successfully isolated */
8801037 del_page_from_lru_list(page, lruvec, page_lru(page));
881
- inc_node_page_state(page,
882
- NR_ISOLATED_ANON + page_is_file_cache(page));
1038
+ mod_node_page_state(page_pgdat(page),
1039
+ NR_ISOLATED_ANON + page_is_file_lru(page),
1040
+ thp_nr_pages(page));
8831041
8841042 isolate_success:
8851043 list_add(&page->lru, &cc->migratepages);
886
- cc->nr_migratepages++;
887
- nr_isolated++;
1044
+ cc->nr_migratepages += compound_nr(page);
1045
+ nr_isolated += compound_nr(page);
8881046
8891047 /*
890
- * Record where we could have freed pages by migration and not
891
- * yet flushed them to buddy allocator.
892
- * - this is the lowest page that was isolated and likely be
893
- * then freed by migration.
1048
+ * Avoid isolating too much unless this block is being
1049
+ * rescanned (e.g. dirty/writeback pages, parallel allocation)
1050
+ * or a lock is contended. For contention, isolate quickly to
1051
+ * potentially remove one source of contention.
8941052 */
895
- if (!cc->last_migrated_pfn)
896
- cc->last_migrated_pfn = low_pfn;
897
-
898
- /* Avoid isolating too much */
899
- if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
1053
+ if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
1054
+ !cc->rescan && !cc->contended) {
9001055 ++low_pfn;
9011056 break;
9021057 }
....@@ -913,12 +1068,11 @@
9131068 */
9141069 if (nr_isolated) {
9151070 if (locked) {
916
- spin_unlock_irqrestore(zone_lru_lock(zone), flags);
1071
+ spin_unlock_irqrestore(&pgdat->lru_lock, flags);
9171072 locked = false;
9181073 }
9191074 putback_movable_pages(&cc->migratepages);
9201075 cc->nr_migratepages = 0;
921
- cc->last_migrated_pfn = 0;
9221076 nr_isolated = 0;
9231077 }
9241078
....@@ -939,19 +1093,28 @@
9391093 if (unlikely(low_pfn > end_pfn))
9401094 low_pfn = end_pfn;
9411095
1096
+isolate_abort:
9421097 if (locked)
943
- spin_unlock_irqrestore(zone_lru_lock(zone), flags);
1098
+ spin_unlock_irqrestore(&pgdat->lru_lock, flags);
9441099
9451100 /*
946
- * Update the pageblock-skip information and cached scanner pfn,
947
- * if the whole pageblock was scanned without isolating any page.
1101
+ * Updated the cached scanner pfn once the pageblock has been scanned
1102
+ * Pages will either be migrated in which case there is no point
1103
+ * scanning in the near future or migration failed in which case the
1104
+ * failure reason may persist. The block is marked for skipping if
1105
+ * there were no pages isolated in the block or if the block is
1106
+ * rescanned twice in a row.
9481107 */
949
- if (low_pfn == end_pfn)
950
- update_pageblock_skip(cc, valid_page, nr_isolated, true);
1108
+ if (low_pfn == end_pfn && (!nr_isolated || cc->rescan)) {
1109
+ if (valid_page && !skip_updated)
1110
+ set_pageblock_skip(valid_page);
1111
+ update_cached_migrate(cc, low_pfn);
1112
+ }
9511113
9521114 trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
9531115 nr_scanned, nr_isolated);
9541116
1117
+fatal_pending:
9551118 cc->total_migrate_scanned += nr_scanned;
9561119 if (nr_isolated)
9571120 count_compact_events(COMPACTISOLATED, nr_isolated);
....@@ -998,7 +1161,7 @@
9981161 if (!pfn)
9991162 break;
10001163
1001
- if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
1164
+ if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
10021165 break;
10031166 }
10041167
....@@ -1012,6 +1175,9 @@
10121175 struct page *page)
10131176 {
10141177 int block_mt;
1178
+
1179
+ if (pageblock_skip_persistent(page))
1180
+ return false;
10151181
10161182 if ((cc->mode != MIGRATE_ASYNC) || !cc->direct_compaction)
10171183 return true;
....@@ -1035,7 +1201,7 @@
10351201 * the only small danger is that we skip a potentially suitable
10361202 * pageblock, so it's not worth to check order for valid range.
10371203 */
1038
- if (page_order_unsafe(page) >= pageblock_order)
1204
+ if (buddy_order_unsafe(page) >= pageblock_order)
10391205 return false;
10401206 }
10411207
....@@ -1050,6 +1216,14 @@
10501216 return false;
10511217 }
10521218
1219
+static inline unsigned int
1220
+freelist_scan_limit(struct compact_control *cc)
1221
+{
1222
+ unsigned short shift = BITS_PER_LONG - 1;
1223
+
1224
+ return (COMPACT_CLUSTER_MAX >> min(shift, cc->fast_search_fail)) + 1;
1225
+}
1226
+
10531227 /*
10541228 * Test whether the free scanner has reached the same or lower pageblock than
10551229 * the migration scanner, and compaction should thus terminate.
....@@ -1058,6 +1232,257 @@
10581232 {
10591233 return (cc->free_pfn >> pageblock_order)
10601234 <= (cc->migrate_pfn >> pageblock_order);
1235
+}
1236
+
1237
+/*
1238
+ * Used when scanning for a suitable migration target which scans freelists
1239
+ * in reverse. Reorders the list such as the unscanned pages are scanned
1240
+ * first on the next iteration of the free scanner
1241
+ */
1242
+static void
1243
+move_freelist_head(struct list_head *freelist, struct page *freepage)
1244
+{
1245
+ LIST_HEAD(sublist);
1246
+
1247
+ if (!list_is_last(freelist, &freepage->lru)) {
1248
+ list_cut_before(&sublist, freelist, &freepage->lru);
1249
+ if (!list_empty(&sublist))
1250
+ list_splice_tail(&sublist, freelist);
1251
+ }
1252
+}
1253
+
1254
+/*
1255
+ * Similar to move_freelist_head except used by the migration scanner
1256
+ * when scanning forward. It's possible for these list operations to
1257
+ * move against each other if they search the free list exactly in
1258
+ * lockstep.
1259
+ */
1260
+static void
1261
+move_freelist_tail(struct list_head *freelist, struct page *freepage)
1262
+{
1263
+ LIST_HEAD(sublist);
1264
+
1265
+ if (!list_is_first(freelist, &freepage->lru)) {
1266
+ list_cut_position(&sublist, freelist, &freepage->lru);
1267
+ if (!list_empty(&sublist))
1268
+ list_splice_tail(&sublist, freelist);
1269
+ }
1270
+}
1271
+
1272
+static void
1273
+fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated)
1274
+{
1275
+ unsigned long start_pfn, end_pfn;
1276
+ struct page *page;
1277
+
1278
+ /* Do not search around if there are enough pages already */
1279
+ if (cc->nr_freepages >= cc->nr_migratepages)
1280
+ return;
1281
+
1282
+ /* Minimise scanning during async compaction */
1283
+ if (cc->direct_compaction && cc->mode == MIGRATE_ASYNC)
1284
+ return;
1285
+
1286
+ /* Pageblock boundaries */
1287
+ start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn);
1288
+ end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone));
1289
+
1290
+ page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone);
1291
+ if (!page)
1292
+ return;
1293
+
1294
+ /* Scan before */
1295
+ if (start_pfn != pfn) {
1296
+ isolate_freepages_block(cc, &start_pfn, pfn, &cc->freepages, 1, false);
1297
+ if (cc->nr_freepages >= cc->nr_migratepages)
1298
+ return;
1299
+ }
1300
+
1301
+ /* Scan after */
1302
+ start_pfn = pfn + nr_isolated;
1303
+ if (start_pfn < end_pfn)
1304
+ isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
1305
+
1306
+ /* Skip this pageblock in the future as it's full or nearly full */
1307
+ if (cc->nr_freepages < cc->nr_migratepages)
1308
+ set_pageblock_skip(page);
1309
+}
1310
+
1311
+/* Search orders in round-robin fashion */
1312
+static int next_search_order(struct compact_control *cc, int order)
1313
+{
1314
+ order--;
1315
+ if (order < 0)
1316
+ order = cc->order - 1;
1317
+
1318
+ /* Search wrapped around? */
1319
+ if (order == cc->search_order) {
1320
+ cc->search_order--;
1321
+ if (cc->search_order < 0)
1322
+ cc->search_order = cc->order - 1;
1323
+ return -1;
1324
+ }
1325
+
1326
+ return order;
1327
+}
1328
+
1329
+static unsigned long
1330
+fast_isolate_freepages(struct compact_control *cc)
1331
+{
1332
+ unsigned int limit = min(1U, freelist_scan_limit(cc) >> 1);
1333
+ unsigned int nr_scanned = 0;
1334
+ unsigned long low_pfn, min_pfn, highest = 0;
1335
+ unsigned long nr_isolated = 0;
1336
+ unsigned long distance;
1337
+ struct page *page = NULL;
1338
+ bool scan_start = false;
1339
+ int order;
1340
+
1341
+ /* Full compaction passes in a negative order */
1342
+ if (cc->order <= 0)
1343
+ return cc->free_pfn;
1344
+
1345
+ /*
1346
+ * If starting the scan, use a deeper search and use the highest
1347
+ * PFN found if a suitable one is not found.
1348
+ */
1349
+ if (cc->free_pfn >= cc->zone->compact_init_free_pfn) {
1350
+ limit = pageblock_nr_pages >> 1;
1351
+ scan_start = true;
1352
+ }
1353
+
1354
+ /*
1355
+ * Preferred point is in the top quarter of the scan space but take
1356
+ * a pfn from the top half if the search is problematic.
1357
+ */
1358
+ distance = (cc->free_pfn - cc->migrate_pfn);
1359
+ low_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 2));
1360
+ min_pfn = pageblock_start_pfn(cc->free_pfn - (distance >> 1));
1361
+
1362
+ if (WARN_ON_ONCE(min_pfn > low_pfn))
1363
+ low_pfn = min_pfn;
1364
+
1365
+ /*
1366
+ * Search starts from the last successful isolation order or the next
1367
+ * order to search after a previous failure
1368
+ */
1369
+ cc->search_order = min_t(unsigned int, cc->order - 1, cc->search_order);
1370
+
1371
+ for (order = cc->search_order;
1372
+ !page && order >= 0;
1373
+ order = next_search_order(cc, order)) {
1374
+ struct free_area *area = &cc->zone->free_area[order];
1375
+ struct list_head *freelist;
1376
+ struct page *freepage;
1377
+ unsigned long flags;
1378
+ unsigned int order_scanned = 0;
1379
+ unsigned long high_pfn = 0;
1380
+
1381
+ if (!area->nr_free)
1382
+ continue;
1383
+
1384
+ spin_lock_irqsave(&cc->zone->lock, flags);
1385
+ freelist = &area->free_list[MIGRATE_MOVABLE];
1386
+ list_for_each_entry_reverse(freepage, freelist, lru) {
1387
+ unsigned long pfn;
1388
+
1389
+ order_scanned++;
1390
+ nr_scanned++;
1391
+ pfn = page_to_pfn(freepage);
1392
+
1393
+ if (pfn >= highest)
1394
+ highest = max(pageblock_start_pfn(pfn),
1395
+ cc->zone->zone_start_pfn);
1396
+
1397
+ if (pfn >= low_pfn) {
1398
+ cc->fast_search_fail = 0;
1399
+ cc->search_order = order;
1400
+ page = freepage;
1401
+ break;
1402
+ }
1403
+
1404
+ if (pfn >= min_pfn && pfn > high_pfn) {
1405
+ high_pfn = pfn;
1406
+
1407
+ /* Shorten the scan if a candidate is found */
1408
+ limit >>= 1;
1409
+ }
1410
+
1411
+ if (order_scanned >= limit)
1412
+ break;
1413
+ }
1414
+
1415
+ /* Use a minimum pfn if a preferred one was not found */
1416
+ if (!page && high_pfn) {
1417
+ page = pfn_to_page(high_pfn);
1418
+
1419
+ /* Update freepage for the list reorder below */
1420
+ freepage = page;
1421
+ }
1422
+
1423
+ /* Reorder to so a future search skips recent pages */
1424
+ move_freelist_head(freelist, freepage);
1425
+
1426
+ /* Isolate the page if available */
1427
+ if (page) {
1428
+ if (__isolate_free_page(page, order)) {
1429
+ set_page_private(page, order);
1430
+ nr_isolated = 1 << order;
1431
+ cc->nr_freepages += nr_isolated;
1432
+ list_add_tail(&page->lru, &cc->freepages);
1433
+ count_compact_events(COMPACTISOLATED, nr_isolated);
1434
+ } else {
1435
+ /* If isolation fails, abort the search */
1436
+ order = cc->search_order + 1;
1437
+ page = NULL;
1438
+ }
1439
+ }
1440
+
1441
+ spin_unlock_irqrestore(&cc->zone->lock, flags);
1442
+
1443
+ /*
1444
+ * Smaller scan on next order so the total scan ig related
1445
+ * to freelist_scan_limit.
1446
+ */
1447
+ if (order_scanned >= limit)
1448
+ limit = min(1U, limit >> 1);
1449
+ }
1450
+
1451
+ if (!page) {
1452
+ cc->fast_search_fail++;
1453
+ if (scan_start) {
1454
+ /*
1455
+ * Use the highest PFN found above min. If one was
1456
+ * not found, be pessimistic for direct compaction
1457
+ * and use the min mark.
1458
+ */
1459
+ if (highest) {
1460
+ page = pfn_to_page(highest);
1461
+ cc->free_pfn = highest;
1462
+ } else {
1463
+ if (cc->direct_compaction && pfn_valid(min_pfn)) {
1464
+ page = pageblock_pfn_to_page(min_pfn,
1465
+ min(pageblock_end_pfn(min_pfn),
1466
+ zone_end_pfn(cc->zone)),
1467
+ cc->zone);
1468
+ cc->free_pfn = min_pfn;
1469
+ }
1470
+ }
1471
+ }
1472
+ }
1473
+
1474
+ if (highest && highest >= cc->zone->compact_cached_free_pfn) {
1475
+ highest -= pageblock_nr_pages;
1476
+ cc->zone->compact_cached_free_pfn = highest;
1477
+ }
1478
+
1479
+ cc->total_free_scanned += nr_scanned;
1480
+ if (!page)
1481
+ return cc->free_pfn;
1482
+
1483
+ low_pfn = page_to_pfn(page);
1484
+ fast_isolate_around(cc, low_pfn, nr_isolated);
1485
+ return low_pfn;
10611486 }
10621487
10631488 /*
....@@ -1073,6 +1498,12 @@
10731498 unsigned long block_end_pfn; /* end of current pageblock */
10741499 unsigned long low_pfn; /* lowest pfn scanner is able to scan */
10751500 struct list_head *freelist = &cc->freepages;
1501
+ unsigned int stride;
1502
+
1503
+ /* Try a small search of the free lists for a candidate */
1504
+ isolate_start_pfn = fast_isolate_freepages(cc);
1505
+ if (cc->nr_freepages)
1506
+ goto splitmap;
10761507
10771508 /*
10781509 * Initialise the free scanner. The starting point is where we last
....@@ -1081,15 +1512,16 @@
10811512 * this pfn aligned down to the pageblock boundary, because we do
10821513 * block_start_pfn -= pageblock_nr_pages in the for loop.
10831514 * For ending point, take care when isolating in last pageblock of a
1084
- * a zone which ends in the middle of a pageblock.
1515
+ * zone which ends in the middle of a pageblock.
10851516 * The low boundary is the end of the pageblock the migration scanner
10861517 * is using.
10871518 */
10881519 isolate_start_pfn = cc->free_pfn;
1089
- block_start_pfn = pageblock_start_pfn(cc->free_pfn);
1520
+ block_start_pfn = pageblock_start_pfn(isolate_start_pfn);
10901521 block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
10911522 zone_end_pfn(zone));
10921523 low_pfn = pageblock_end_pfn(cc->migrate_pfn);
1524
+ stride = cc->mode == MIGRATE_ASYNC ? COMPACT_CLUSTER_MAX : 1;
10931525
10941526 /*
10951527 * Isolate free pages until enough are available to migrate the
....@@ -1100,14 +1532,14 @@
11001532 block_end_pfn = block_start_pfn,
11011533 block_start_pfn -= pageblock_nr_pages,
11021534 isolate_start_pfn = block_start_pfn) {
1535
+ unsigned long nr_isolated;
1536
+
11031537 /*
11041538 * This can iterate a massively long zone without finding any
1105
- * suitable migration targets, so periodically check if we need
1106
- * to schedule, or even abort async compaction.
1539
+ * suitable migration targets, so periodically check resched.
11071540 */
1108
- if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1109
- && compact_should_abort(cc))
1110
- break;
1541
+ if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1542
+ cond_resched();
11111543
11121544 page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
11131545 zone);
....@@ -1123,15 +1555,15 @@
11231555 continue;
11241556
11251557 /* Found a block suitable for isolating free pages from. */
1126
- isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
1127
- freelist, false);
1558
+ nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
1559
+ block_end_pfn, freelist, stride, false);
11281560
1129
- /*
1130
- * If we isolated enough freepages, or aborted due to lock
1131
- * contention, terminate.
1132
- */
1133
- if ((cc->nr_freepages >= cc->nr_migratepages)
1134
- || cc->contended) {
1561
+ /* Update the skip hint if the full pageblock was scanned */
1562
+ if (isolate_start_pfn == block_end_pfn)
1563
+ update_pageblock_skip(cc, page, block_start_pfn);
1564
+
1565
+ /* Are enough freepages isolated? */
1566
+ if (cc->nr_freepages >= cc->nr_migratepages) {
11351567 if (isolate_start_pfn >= block_end_pfn) {
11361568 /*
11371569 * Restart at previous pageblock if more
....@@ -1148,10 +1580,14 @@
11481580 */
11491581 break;
11501582 }
1151
- }
11521583
1153
- /* __isolate_free_page() does not map the pages */
1154
- map_pages(freelist);
1584
+ /* Adjust stride depending on isolation */
1585
+ if (nr_isolated) {
1586
+ stride = 1;
1587
+ continue;
1588
+ }
1589
+ stride = min_t(unsigned int, COMPACT_CLUSTER_MAX, stride << 1);
1590
+ }
11551591
11561592 /*
11571593 * Record where the free scanner will restart next time. Either we
....@@ -1160,6 +1596,10 @@
11601596 * and the loop terminated due to isolate_start_pfn < low_pfn
11611597 */
11621598 cc->free_pfn = isolate_start_pfn;
1599
+
1600
+splitmap:
1601
+ /* __isolate_free_page() does not map the pages */
1602
+ split_map_pages(freelist);
11631603 }
11641604
11651605 /*
....@@ -1172,13 +1612,8 @@
11721612 struct compact_control *cc = (struct compact_control *)data;
11731613 struct page *freepage;
11741614
1175
- /*
1176
- * Isolate free pages if necessary, and if we are not aborting due to
1177
- * contention.
1178
- */
11791615 if (list_empty(&cc->freepages)) {
1180
- if (!cc->contended)
1181
- isolate_freepages(cc);
1616
+ isolate_freepages(cc);
11821617
11831618 if (list_empty(&cc->freepages))
11841619 return NULL;
....@@ -1215,15 +1650,158 @@
12151650 * Allow userspace to control policy on scanning the unevictable LRU for
12161651 * compactable pages.
12171652 */
1653
+#ifdef CONFIG_PREEMPT_RT
1654
+int sysctl_compact_unevictable_allowed __read_mostly = 0;
1655
+#else
12181656 int sysctl_compact_unevictable_allowed __read_mostly = 1;
1657
+#endif
1658
+
1659
+static inline void
1660
+update_fast_start_pfn(struct compact_control *cc, unsigned long pfn)
1661
+{
1662
+ if (cc->fast_start_pfn == ULONG_MAX)
1663
+ return;
1664
+
1665
+ if (!cc->fast_start_pfn)
1666
+ cc->fast_start_pfn = pfn;
1667
+
1668
+ cc->fast_start_pfn = min(cc->fast_start_pfn, pfn);
1669
+}
1670
+
1671
+static inline unsigned long
1672
+reinit_migrate_pfn(struct compact_control *cc)
1673
+{
1674
+ if (!cc->fast_start_pfn || cc->fast_start_pfn == ULONG_MAX)
1675
+ return cc->migrate_pfn;
1676
+
1677
+ cc->migrate_pfn = cc->fast_start_pfn;
1678
+ cc->fast_start_pfn = ULONG_MAX;
1679
+
1680
+ return cc->migrate_pfn;
1681
+}
1682
+
1683
+/*
1684
+ * Briefly search the free lists for a migration source that already has
1685
+ * some free pages to reduce the number of pages that need migration
1686
+ * before a pageblock is free.
1687
+ */
1688
+static unsigned long fast_find_migrateblock(struct compact_control *cc)
1689
+{
1690
+ unsigned int limit = freelist_scan_limit(cc);
1691
+ unsigned int nr_scanned = 0;
1692
+ unsigned long distance;
1693
+ unsigned long pfn = cc->migrate_pfn;
1694
+ unsigned long high_pfn;
1695
+ int order;
1696
+ bool found_block = false;
1697
+
1698
+ /* Skip hints are relied on to avoid repeats on the fast search */
1699
+ if (cc->ignore_skip_hint)
1700
+ return pfn;
1701
+
1702
+ /*
1703
+ * If the migrate_pfn is not at the start of a zone or the start
1704
+ * of a pageblock then assume this is a continuation of a previous
1705
+ * scan restarted due to COMPACT_CLUSTER_MAX.
1706
+ */
1707
+ if (pfn != cc->zone->zone_start_pfn && pfn != pageblock_start_pfn(pfn))
1708
+ return pfn;
1709
+
1710
+ /*
1711
+ * For smaller orders, just linearly scan as the number of pages
1712
+ * to migrate should be relatively small and does not necessarily
1713
+ * justify freeing up a large block for a small allocation.
1714
+ */
1715
+ if (cc->order <= PAGE_ALLOC_COSTLY_ORDER)
1716
+ return pfn;
1717
+
1718
+ /*
1719
+ * Only allow kcompactd and direct requests for movable pages to
1720
+ * quickly clear out a MOVABLE pageblock for allocation. This
1721
+ * reduces the risk that a large movable pageblock is freed for
1722
+ * an unmovable/reclaimable small allocation.
1723
+ */
1724
+ if (cc->direct_compaction && cc->migratetype != MIGRATE_MOVABLE)
1725
+ return pfn;
1726
+
1727
+ /*
1728
+ * When starting the migration scanner, pick any pageblock within the
1729
+ * first half of the search space. Otherwise try and pick a pageblock
1730
+ * within the first eighth to reduce the chances that a migration
1731
+ * target later becomes a source.
1732
+ */
1733
+ distance = (cc->free_pfn - cc->migrate_pfn) >> 1;
1734
+ if (cc->migrate_pfn != cc->zone->zone_start_pfn)
1735
+ distance >>= 2;
1736
+ high_pfn = pageblock_start_pfn(cc->migrate_pfn + distance);
1737
+
1738
+ for (order = cc->order - 1;
1739
+ order >= PAGE_ALLOC_COSTLY_ORDER && !found_block && nr_scanned < limit;
1740
+ order--) {
1741
+ struct free_area *area = &cc->zone->free_area[order];
1742
+ struct list_head *freelist;
1743
+ unsigned long flags;
1744
+ struct page *freepage;
1745
+
1746
+ if (!area->nr_free)
1747
+ continue;
1748
+
1749
+ spin_lock_irqsave(&cc->zone->lock, flags);
1750
+ freelist = &area->free_list[MIGRATE_MOVABLE];
1751
+ list_for_each_entry(freepage, freelist, lru) {
1752
+ unsigned long free_pfn;
1753
+
1754
+ if (nr_scanned++ >= limit) {
1755
+ move_freelist_tail(freelist, freepage);
1756
+ break;
1757
+ }
1758
+
1759
+ free_pfn = page_to_pfn(freepage);
1760
+ if (free_pfn < high_pfn) {
1761
+ /*
1762
+ * Avoid if skipped recently. Ideally it would
1763
+ * move to the tail but even safe iteration of
1764
+ * the list assumes an entry is deleted, not
1765
+ * reordered.
1766
+ */
1767
+ if (get_pageblock_skip(freepage))
1768
+ continue;
1769
+
1770
+ /* Reorder to so a future search skips recent pages */
1771
+ move_freelist_tail(freelist, freepage);
1772
+
1773
+ update_fast_start_pfn(cc, free_pfn);
1774
+ pfn = pageblock_start_pfn(free_pfn);
1775
+ if (pfn < cc->zone->zone_start_pfn)
1776
+ pfn = cc->zone->zone_start_pfn;
1777
+ cc->fast_search_fail = 0;
1778
+ found_block = true;
1779
+ set_pageblock_skip(freepage);
1780
+ break;
1781
+ }
1782
+ }
1783
+ spin_unlock_irqrestore(&cc->zone->lock, flags);
1784
+ }
1785
+
1786
+ cc->total_migrate_scanned += nr_scanned;
1787
+
1788
+ /*
1789
+ * If fast scanning failed then use a cached entry for a page block
1790
+ * that had free pages as the basis for starting a linear scan.
1791
+ */
1792
+ if (!found_block) {
1793
+ cc->fast_search_fail++;
1794
+ pfn = reinit_migrate_pfn(cc);
1795
+ }
1796
+ return pfn;
1797
+}
12191798
12201799 /*
12211800 * Isolate all pages that can be migrated from the first suitable block,
12221801 * starting at the block pointed to by the migrate scanner pfn within
12231802 * compact_control.
12241803 */
1225
-static isolate_migrate_t isolate_migratepages(struct zone *zone,
1226
- struct compact_control *cc)
1804
+static isolate_migrate_t isolate_migratepages(struct compact_control *cc)
12271805 {
12281806 unsigned long block_start_pfn;
12291807 unsigned long block_end_pfn;
....@@ -1232,15 +1810,24 @@
12321810 const isolate_mode_t isolate_mode =
12331811 (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
12341812 (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
1813
+ bool fast_find_block;
12351814
12361815 /*
12371816 * Start at where we last stopped, or beginning of the zone as
1238
- * initialized by compact_zone()
1817
+ * initialized by compact_zone(). The first failure will use
1818
+ * the lowest PFN as the starting point for linear scanning.
12391819 */
1240
- low_pfn = cc->migrate_pfn;
1820
+ low_pfn = fast_find_migrateblock(cc);
12411821 block_start_pfn = pageblock_start_pfn(low_pfn);
1242
- if (block_start_pfn < zone->zone_start_pfn)
1243
- block_start_pfn = zone->zone_start_pfn;
1822
+ if (block_start_pfn < cc->zone->zone_start_pfn)
1823
+ block_start_pfn = cc->zone->zone_start_pfn;
1824
+
1825
+ /*
1826
+ * fast_find_migrateblock marks a pageblock skipped so to avoid
1827
+ * the isolation_suitable check below, check whether the fast
1828
+ * search was successful.
1829
+ */
1830
+ fast_find_block = low_pfn != cc->migrate_pfn && !cc->fast_search_fail;
12441831
12451832 /* Only scan within a pageblock boundary */
12461833 block_end_pfn = pageblock_end_pfn(low_pfn);
....@@ -1250,6 +1837,7 @@
12501837 * Do not cross the free scanner.
12511838 */
12521839 for (; block_end_pfn <= cc->free_pfn;
1840
+ fast_find_block = false,
12531841 low_pfn = block_end_pfn,
12541842 block_start_pfn = block_end_pfn,
12551843 block_end_pfn += pageblock_nr_pages) {
....@@ -1257,34 +1845,45 @@
12571845 /*
12581846 * This can potentially iterate a massively long zone with
12591847 * many pageblocks unsuitable, so periodically check if we
1260
- * need to schedule, or even abort async compaction.
1848
+ * need to schedule.
12611849 */
1262
- if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
1263
- && compact_should_abort(cc))
1264
- break;
1850
+ if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages)))
1851
+ cond_resched();
12651852
1266
- page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
1267
- zone);
1853
+ page = pageblock_pfn_to_page(block_start_pfn,
1854
+ block_end_pfn, cc->zone);
12681855 if (!page)
12691856 continue;
12701857
1271
- /* If isolation recently failed, do not retry */
1272
- if (!isolation_suitable(cc, page))
1858
+ /*
1859
+ * If isolation recently failed, do not retry. Only check the
1860
+ * pageblock once. COMPACT_CLUSTER_MAX causes a pageblock
1861
+ * to be visited multiple times. Assume skip was checked
1862
+ * before making it "skip" so other compaction instances do
1863
+ * not scan the same block.
1864
+ */
1865
+ if (IS_ALIGNED(low_pfn, pageblock_nr_pages) &&
1866
+ !fast_find_block && !isolation_suitable(cc, page))
12731867 continue;
12741868
12751869 /*
1276
- * For async compaction, also only scan in MOVABLE blocks.
1277
- * Async compaction is optimistic to see if the minimum amount
1278
- * of work satisfies the allocation.
1870
+ * For async compaction, also only scan in MOVABLE blocks
1871
+ * without huge pages. Async compaction is optimistic to see
1872
+ * if the minimum amount of work satisfies the allocation.
1873
+ * The cached PFN is updated as it's possible that all
1874
+ * remaining blocks between source and target are unsuitable
1875
+ * and the compaction scanners fail to meet.
12791876 */
1280
- if (!suitable_migration_source(cc, page))
1877
+ if (!suitable_migration_source(cc, page)) {
1878
+ update_cached_migrate(cc, block_end_pfn);
12811879 continue;
1880
+ }
12821881
12831882 /* Perform the isolation */
12841883 low_pfn = isolate_migratepages_block(cc, low_pfn,
12851884 block_end_pfn, isolate_mode);
12861885
1287
- if (!low_pfn || cc->contended)
1886
+ if (!low_pfn)
12881887 return ISOLATE_ABORT;
12891888
12901889 /*
....@@ -1310,19 +1909,94 @@
13101909 return order == -1;
13111910 }
13121911
1313
-static enum compact_result __compact_finished(struct zone *zone,
1314
- struct compact_control *cc)
1912
+static bool kswapd_is_running(pg_data_t *pgdat)
1913
+{
1914
+ return pgdat->kswapd && (pgdat->kswapd->state == TASK_RUNNING);
1915
+}
1916
+
1917
+/*
1918
+ * A zone's fragmentation score is the external fragmentation wrt to the
1919
+ * COMPACTION_HPAGE_ORDER. It returns a value in the range [0, 100].
1920
+ */
1921
+static unsigned int fragmentation_score_zone(struct zone *zone)
1922
+{
1923
+ return extfrag_for_order(zone, COMPACTION_HPAGE_ORDER);
1924
+}
1925
+
1926
+/*
1927
+ * A weighted zone's fragmentation score is the external fragmentation
1928
+ * wrt to the COMPACTION_HPAGE_ORDER scaled by the zone's size. It
1929
+ * returns a value in the range [0, 100].
1930
+ *
1931
+ * The scaling factor ensures that proactive compaction focuses on larger
1932
+ * zones like ZONE_NORMAL, rather than smaller, specialized zones like
1933
+ * ZONE_DMA32. For smaller zones, the score value remains close to zero,
1934
+ * and thus never exceeds the high threshold for proactive compaction.
1935
+ */
1936
+static unsigned int fragmentation_score_zone_weighted(struct zone *zone)
1937
+{
1938
+ unsigned long score;
1939
+
1940
+ score = zone->present_pages * fragmentation_score_zone(zone);
1941
+ return div64_ul(score, zone->zone_pgdat->node_present_pages + 1);
1942
+}
1943
+
1944
+/*
1945
+ * The per-node proactive (background) compaction process is started by its
1946
+ * corresponding kcompactd thread when the node's fragmentation score
1947
+ * exceeds the high threshold. The compaction process remains active till
1948
+ * the node's score falls below the low threshold, or one of the back-off
1949
+ * conditions is met.
1950
+ */
1951
+static unsigned int fragmentation_score_node(pg_data_t *pgdat)
1952
+{
1953
+ unsigned int score = 0;
1954
+ int zoneid;
1955
+
1956
+ for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1957
+ struct zone *zone;
1958
+
1959
+ zone = &pgdat->node_zones[zoneid];
1960
+ score += fragmentation_score_zone_weighted(zone);
1961
+ }
1962
+
1963
+ return score;
1964
+}
1965
+
1966
+static unsigned int fragmentation_score_wmark(pg_data_t *pgdat, bool low)
1967
+{
1968
+ unsigned int wmark_low;
1969
+
1970
+ /*
1971
+ * Cap the low watermak to avoid excessive compaction
1972
+ * activity in case a user sets the proactivess tunable
1973
+ * close to 100 (maximum).
1974
+ */
1975
+ wmark_low = max(100U - sysctl_compaction_proactiveness, 5U);
1976
+ return low ? wmark_low : min(wmark_low + 10, 100U);
1977
+}
1978
+
1979
+static bool should_proactive_compact_node(pg_data_t *pgdat)
1980
+{
1981
+ int wmark_high;
1982
+
1983
+ if (!sysctl_compaction_proactiveness || kswapd_is_running(pgdat))
1984
+ return false;
1985
+
1986
+ wmark_high = fragmentation_score_wmark(pgdat, false);
1987
+ return fragmentation_score_node(pgdat) > wmark_high;
1988
+}
1989
+
1990
+static enum compact_result __compact_finished(struct compact_control *cc)
13151991 {
13161992 unsigned int order;
13171993 const int migratetype = cc->migratetype;
1318
-
1319
- if (cc->contended || fatal_signal_pending(current))
1320
- return COMPACT_CONTENDED;
1994
+ int ret;
13211995
13221996 /* Compaction run completes if the migrate and free scanner meet */
13231997 if (compact_scanners_met(cc)) {
13241998 /* Let the next compaction start anew. */
1325
- reset_cached_positions(zone);
1999
+ reset_cached_positions(cc->zone);
13262000
13272001 /*
13282002 * Mark that the PG_migrate_skip information should be cleared
....@@ -1331,7 +2005,7 @@
13312005 * based on an allocation request.
13322006 */
13332007 if (cc->direct_compaction)
1334
- zone->compact_blockskip_flush = true;
2008
+ cc->zone->compact_blockskip_flush = true;
13352009
13362010 if (cc->whole_zone)
13372011 return COMPACT_COMPLETE;
....@@ -1339,33 +2013,51 @@
13392013 return COMPACT_PARTIAL_SKIPPED;
13402014 }
13412015
2016
+ if (cc->proactive_compaction) {
2017
+ int score, wmark_low;
2018
+ pg_data_t *pgdat;
2019
+
2020
+ pgdat = cc->zone->zone_pgdat;
2021
+ if (kswapd_is_running(pgdat))
2022
+ return COMPACT_PARTIAL_SKIPPED;
2023
+
2024
+ score = fragmentation_score_zone(cc->zone);
2025
+ wmark_low = fragmentation_score_wmark(pgdat, true);
2026
+
2027
+ if (score > wmark_low)
2028
+ ret = COMPACT_CONTINUE;
2029
+ else
2030
+ ret = COMPACT_SUCCESS;
2031
+
2032
+ goto out;
2033
+ }
2034
+
13422035 if (is_via_compact_memory(cc->order))
13432036 return COMPACT_CONTINUE;
13442037
1345
- if (cc->finishing_block) {
1346
- /*
1347
- * We have finished the pageblock, but better check again that
1348
- * we really succeeded.
1349
- */
1350
- if (IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
1351
- cc->finishing_block = false;
1352
- else
1353
- return COMPACT_CONTINUE;
1354
- }
2038
+ /*
2039
+ * Always finish scanning a pageblock to reduce the possibility of
2040
+ * fallbacks in the future. This is particularly important when
2041
+ * migration source is unmovable/reclaimable but it's not worth
2042
+ * special casing.
2043
+ */
2044
+ if (!IS_ALIGNED(cc->migrate_pfn, pageblock_nr_pages))
2045
+ return COMPACT_CONTINUE;
13552046
13562047 /* Direct compactor: Is a suitable page free? */
2048
+ ret = COMPACT_NO_SUITABLE_PAGE;
13572049 for (order = cc->order; order < MAX_ORDER; order++) {
1358
- struct free_area *area = &zone->free_area[order];
2050
+ struct free_area *area = &cc->zone->free_area[order];
13592051 bool can_steal;
13602052
13612053 /* Job done if page is free of the right migratetype */
1362
- if (!list_empty(&area->free_list[migratetype]))
2054
+ if (!free_area_empty(area, migratetype))
13632055 return COMPACT_SUCCESS;
13642056
13652057 #ifdef CONFIG_CMA
13662058 /* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
13672059 if (migratetype == MIGRATE_MOVABLE &&
1368
- !list_empty(&area->free_list[MIGRATE_CMA]))
2060
+ !free_area_empty(area, MIGRATE_CMA))
13692061 return COMPACT_SUCCESS;
13702062 #endif
13712063 /*
....@@ -1393,21 +2085,24 @@
13932085 return COMPACT_SUCCESS;
13942086 }
13952087
1396
- cc->finishing_block = true;
1397
- return COMPACT_CONTINUE;
2088
+ ret = COMPACT_CONTINUE;
2089
+ break;
13982090 }
13992091 }
14002092
1401
- return COMPACT_NO_SUITABLE_PAGE;
2093
+out:
2094
+ if (cc->contended || fatal_signal_pending(current))
2095
+ ret = COMPACT_CONTENDED;
2096
+
2097
+ return ret;
14022098 }
14032099
1404
-static enum compact_result compact_finished(struct zone *zone,
1405
- struct compact_control *cc)
2100
+static enum compact_result compact_finished(struct compact_control *cc)
14062101 {
14072102 int ret;
14082103
1409
- ret = __compact_finished(zone, cc);
1410
- trace_mm_compaction_finished(zone, cc->order, ret);
2104
+ ret = __compact_finished(cc);
2105
+ trace_mm_compaction_finished(cc->zone, cc->order, ret);
14112106 if (ret == COMPACT_NO_SUITABLE_PAGE)
14122107 ret = COMPACT_CONTINUE;
14132108
....@@ -1423,7 +2118,7 @@
14232118 */
14242119 static enum compact_result __compaction_suitable(struct zone *zone, int order,
14252120 unsigned int alloc_flags,
1426
- int classzone_idx,
2121
+ int highest_zoneidx,
14272122 unsigned long wmark_target)
14282123 {
14292124 unsigned long watermark;
....@@ -1431,12 +2126,12 @@
14312126 if (is_via_compact_memory(order))
14322127 return COMPACT_CONTINUE;
14332128
1434
- watermark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
2129
+ watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
14352130 /*
14362131 * If watermarks for high-order allocation are already met, there
14372132 * should be no need for compaction at all.
14382133 */
1439
- if (zone_watermark_ok(zone, order, watermark, classzone_idx,
2134
+ if (zone_watermark_ok(zone, order, watermark, highest_zoneidx,
14402135 alloc_flags))
14412136 return COMPACT_SUCCESS;
14422137
....@@ -1446,9 +2141,9 @@
14462141 * watermark and alloc_flags have to match, or be more pessimistic than
14472142 * the check in __isolate_free_page(). We don't use the direct
14482143 * compactor's alloc_flags, as they are not relevant for freepage
1449
- * isolation. We however do use the direct compactor's classzone_idx to
1450
- * skip over zones where lowmem reserves would prevent allocation even
1451
- * if compaction succeeds.
2144
+ * isolation. We however do use the direct compactor's highest_zoneidx
2145
+ * to skip over zones where lowmem reserves would prevent allocation
2146
+ * even if compaction succeeds.
14522147 * For costly orders, we require low watermark instead of min for
14532148 * compaction to proceed to increase its chances.
14542149 * ALLOC_CMA is used, as pages in CMA pageblocks are considered
....@@ -1457,7 +2152,7 @@
14572152 watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
14582153 low_wmark_pages(zone) : min_wmark_pages(zone);
14592154 watermark += compact_gap(order);
1460
- if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
2155
+ if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx,
14612156 ALLOC_CMA, wmark_target))
14622157 return COMPACT_SKIPPED;
14632158
....@@ -1466,12 +2161,12 @@
14662161
14672162 enum compact_result compaction_suitable(struct zone *zone, int order,
14682163 unsigned int alloc_flags,
1469
- int classzone_idx)
2164
+ int highest_zoneidx)
14702165 {
14712166 enum compact_result ret;
14722167 int fragindex;
14732168
1474
- ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
2169
+ ret = __compaction_suitable(zone, order, alloc_flags, highest_zoneidx,
14752170 zone_page_state(zone, NR_FREE_PAGES));
14762171 /*
14772172 * fragmentation index determines if allocation failures are due to
....@@ -1512,8 +2207,8 @@
15122207 * Make sure at least one zone would pass __compaction_suitable if we continue
15132208 * retrying the reclaim.
15142209 */
1515
- for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1516
- ac->nodemask) {
2210
+ for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2211
+ ac->highest_zoneidx, ac->nodemask) {
15172212 unsigned long available;
15182213 enum compact_result compact_result;
15192214
....@@ -1526,7 +2221,7 @@
15262221 available = zone_reclaimable_pages(zone) / order;
15272222 available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
15282223 compact_result = __compaction_suitable(zone, order, alloc_flags,
1529
- ac_classzone_idx(ac), available);
2224
+ ac->highest_zoneidx, available);
15302225 if (compact_result != COMPACT_SKIPPED)
15312226 return true;
15322227 }
....@@ -1534,12 +2229,15 @@
15342229 return false;
15352230 }
15362231
1537
-static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
2232
+static enum compact_result
2233
+compact_zone(struct compact_control *cc, struct capture_control *capc)
15382234 {
15392235 enum compact_result ret;
1540
- unsigned long start_pfn = zone->zone_start_pfn;
1541
- unsigned long end_pfn = zone_end_pfn(zone);
2236
+ unsigned long start_pfn = cc->zone->zone_start_pfn;
2237
+ unsigned long end_pfn = zone_end_pfn(cc->zone);
2238
+ unsigned long last_migrated_pfn;
15422239 const bool sync = cc->mode != MIGRATE_ASYNC;
2240
+ bool update_cached;
15432241
15442242 /*
15452243 * These counters track activities during zone compaction. Initialize
....@@ -1552,9 +2250,9 @@
15522250 INIT_LIST_HEAD(&cc->freepages);
15532251 INIT_LIST_HEAD(&cc->migratepages);
15542252
1555
- cc->migratetype = gfpflags_to_migratetype(cc->gfp_mask);
1556
- ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
1557
- cc->classzone_idx);
2253
+ cc->migratetype = gfp_migratetype(cc->gfp_mask);
2254
+ ret = compaction_suitable(cc->zone, cc->order, cc->alloc_flags,
2255
+ cc->highest_zoneidx);
15582256 /* Compaction is likely to fail */
15592257 if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
15602258 return ret;
....@@ -1566,8 +2264,8 @@
15662264 * Clear pageblock skip if there were failures recently and compaction
15672265 * is about to be retried after being deferred.
15682266 */
1569
- if (compaction_restarting(zone, cc->order))
1570
- __reset_isolation_suitable(zone);
2267
+ if (compaction_restarting(cc->zone, cc->order))
2268
+ __reset_isolation_suitable(cc->zone);
15712269
15722270 /*
15732271 * Setup to move all movable pages to the end of the zone. Used cached
....@@ -1575,43 +2273,76 @@
15752273 * want to compact the whole zone), but check that it is initialised
15762274 * by ensuring the values are within zone boundaries.
15772275 */
2276
+ cc->fast_start_pfn = 0;
15782277 if (cc->whole_zone) {
15792278 cc->migrate_pfn = start_pfn;
15802279 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
15812280 } else {
1582
- cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
1583
- cc->free_pfn = zone->compact_cached_free_pfn;
2281
+ cc->migrate_pfn = cc->zone->compact_cached_migrate_pfn[sync];
2282
+ cc->free_pfn = cc->zone->compact_cached_free_pfn;
15842283 if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
15852284 cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
1586
- zone->compact_cached_free_pfn = cc->free_pfn;
2285
+ cc->zone->compact_cached_free_pfn = cc->free_pfn;
15872286 }
15882287 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
15892288 cc->migrate_pfn = start_pfn;
1590
- zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
1591
- zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
2289
+ cc->zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
2290
+ cc->zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
15922291 }
15932292
1594
- if (cc->migrate_pfn == start_pfn)
2293
+ if (cc->migrate_pfn <= cc->zone->compact_init_migrate_pfn)
15952294 cc->whole_zone = true;
15962295 }
15972296
1598
- cc->last_migrated_pfn = 0;
2297
+ last_migrated_pfn = 0;
2298
+
2299
+ /*
2300
+ * Migrate has separate cached PFNs for ASYNC and SYNC* migration on
2301
+ * the basis that some migrations will fail in ASYNC mode. However,
2302
+ * if the cached PFNs match and pageblocks are skipped due to having
2303
+ * no isolation candidates, then the sync state does not matter.
2304
+ * Until a pageblock with isolation candidates is found, keep the
2305
+ * cached PFNs in sync to avoid revisiting the same blocks.
2306
+ */
2307
+ update_cached = !sync &&
2308
+ cc->zone->compact_cached_migrate_pfn[0] == cc->zone->compact_cached_migrate_pfn[1];
15992309
16002310 trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
16012311 cc->free_pfn, end_pfn, sync);
16022312
1603
- migrate_prep_local();
2313
+ /* lru_add_drain_all could be expensive with involving other CPUs */
2314
+ lru_add_drain();
16042315
1605
- while ((ret = compact_finished(zone, cc)) == COMPACT_CONTINUE) {
2316
+ while ((ret = compact_finished(cc)) == COMPACT_CONTINUE) {
16062317 int err;
2318
+ unsigned long start_pfn = cc->migrate_pfn;
16072319
1608
- switch (isolate_migratepages(zone, cc)) {
2320
+ /*
2321
+ * Avoid multiple rescans which can happen if a page cannot be
2322
+ * isolated (dirty/writeback in async mode) or if the migrated
2323
+ * pages are being allocated before the pageblock is cleared.
2324
+ * The first rescan will capture the entire pageblock for
2325
+ * migration. If it fails, it'll be marked skip and scanning
2326
+ * will proceed as normal.
2327
+ */
2328
+ cc->rescan = false;
2329
+ if (pageblock_start_pfn(last_migrated_pfn) ==
2330
+ pageblock_start_pfn(start_pfn)) {
2331
+ cc->rescan = true;
2332
+ }
2333
+
2334
+ switch (isolate_migratepages(cc)) {
16092335 case ISOLATE_ABORT:
16102336 ret = COMPACT_CONTENDED;
16112337 putback_movable_pages(&cc->migratepages);
16122338 cc->nr_migratepages = 0;
16132339 goto out;
16142340 case ISOLATE_NONE:
2341
+ if (update_cached) {
2342
+ cc->zone->compact_cached_migrate_pfn[1] =
2343
+ cc->zone->compact_cached_migrate_pfn[0];
2344
+ }
2345
+
16152346 /*
16162347 * We haven't isolated and migrated anything, but
16172348 * there might still be unflushed migrations from
....@@ -1619,6 +2350,8 @@
16192350 */
16202351 goto check_drain;
16212352 case ISOLATE_SUCCESS:
2353
+ update_cached = false;
2354
+ last_migrated_pfn = start_pfn;
16222355 ;
16232356 }
16242357
....@@ -1650,8 +2383,7 @@
16502383 cc->migrate_pfn = block_end_pfn(
16512384 cc->migrate_pfn - 1, cc->order);
16522385 /* Draining pcplists is useless in this case */
1653
- cc->last_migrated_pfn = 0;
1654
-
2386
+ last_migrated_pfn = 0;
16552387 }
16562388 }
16572389
....@@ -1663,21 +2395,22 @@
16632395 * compact_finished() can detect immediately if allocation
16642396 * would succeed.
16652397 */
1666
- if (cc->order > 0 && cc->last_migrated_pfn) {
1667
- int cpu;
2398
+ if (cc->order > 0 && last_migrated_pfn) {
16682399 unsigned long current_block_start =
16692400 block_start_pfn(cc->migrate_pfn, cc->order);
16702401
1671
- if (cc->last_migrated_pfn < current_block_start) {
1672
- cpu = get_cpu();
1673
- lru_add_drain_cpu(cpu);
1674
- drain_local_pages(zone);
1675
- put_cpu();
2402
+ if (last_migrated_pfn < current_block_start) {
2403
+ lru_add_drain_cpu_zone(cc->zone);
16762404 /* No more flushing until we migrate again */
1677
- cc->last_migrated_pfn = 0;
2405
+ last_migrated_pfn = 0;
16782406 }
16792407 }
16802408
2409
+ /* Stop if a page has been captured */
2410
+ if (capc && capc->page) {
2411
+ ret = COMPACT_SUCCESS;
2412
+ break;
2413
+ }
16812414 }
16822415
16832416 out:
....@@ -1696,8 +2429,8 @@
16962429 * Only go back, not forward. The cached pfn might have been
16972430 * already reset to zone end in compact_finished()
16982431 */
1699
- if (free_pfn > zone->compact_cached_free_pfn)
1700
- zone->compact_cached_free_pfn = free_pfn;
2432
+ if (free_pfn > cc->zone->compact_cached_free_pfn)
2433
+ cc->zone->compact_cached_free_pfn = free_pfn;
17012434 }
17022435
17032436 count_compact_events(COMPACTMIGRATE_SCANNED, cc->total_migrate_scanned);
....@@ -1711,27 +2444,49 @@
17112444
17122445 static enum compact_result compact_zone_order(struct zone *zone, int order,
17132446 gfp_t gfp_mask, enum compact_priority prio,
1714
- unsigned int alloc_flags, int classzone_idx)
2447
+ unsigned int alloc_flags, int highest_zoneidx,
2448
+ struct page **capture)
17152449 {
17162450 enum compact_result ret;
17172451 struct compact_control cc = {
17182452 .order = order,
2453
+ .search_order = order,
17192454 .gfp_mask = gfp_mask,
17202455 .zone = zone,
17212456 .mode = (prio == COMPACT_PRIO_ASYNC) ?
17222457 MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
17232458 .alloc_flags = alloc_flags,
1724
- .classzone_idx = classzone_idx,
2459
+ .highest_zoneidx = highest_zoneidx,
17252460 .direct_compaction = true,
17262461 .whole_zone = (prio == MIN_COMPACT_PRIORITY),
17272462 .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
17282463 .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
17292464 };
2465
+ struct capture_control capc = {
2466
+ .cc = &cc,
2467
+ .page = NULL,
2468
+ };
17302469
1731
- ret = compact_zone(zone, &cc);
2470
+ /*
2471
+ * Make sure the structs are really initialized before we expose the
2472
+ * capture control, in case we are interrupted and the interrupt handler
2473
+ * frees a page.
2474
+ */
2475
+ barrier();
2476
+ WRITE_ONCE(current->capture_control, &capc);
2477
+
2478
+ ret = compact_zone(&cc, &capc);
17322479
17332480 VM_BUG_ON(!list_empty(&cc.freepages));
17342481 VM_BUG_ON(!list_empty(&cc.migratepages));
2482
+
2483
+ /*
2484
+ * Make sure we hide capture control first before we read the captured
2485
+ * page pointer, otherwise an interrupt could free and capture a page
2486
+ * and we would leak it.
2487
+ */
2488
+ WRITE_ONCE(current->capture_control, NULL);
2489
+ *capture = READ_ONCE(capc.page);
17352490
17362491 return ret;
17372492 }
....@@ -1745,12 +2500,13 @@
17452500 * @alloc_flags: The allocation flags of the current allocation
17462501 * @ac: The context of current allocation
17472502 * @prio: Determines how hard direct compaction should try to succeed
2503
+ * @capture: Pointer to free page created by compaction will be stored here
17482504 *
17492505 * This is the main entry point for direct page compaction.
17502506 */
17512507 enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
17522508 unsigned int alloc_flags, const struct alloc_context *ac,
1753
- enum compact_priority prio)
2509
+ enum compact_priority prio, struct page **capture)
17542510 {
17552511 int may_perform_io = gfp_mask & __GFP_IO;
17562512 struct zoneref *z;
....@@ -1767,8 +2523,8 @@
17672523 trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
17682524
17692525 /* Compact each zone in the list */
1770
- for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
1771
- ac->nodemask) {
2526
+ for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
2527
+ ac->highest_zoneidx, ac->nodemask) {
17722528 enum compact_result status;
17732529
17742530 if (prio > MIN_COMPACT_PRIORITY
....@@ -1778,7 +2534,7 @@
17782534 }
17792535
17802536 status = compact_zone_order(zone, order, gfp_mask, prio,
1781
- alloc_flags, ac_classzone_idx(ac));
2537
+ alloc_flags, ac->highest_zoneidx, capture);
17822538 rc = max(status, rc);
17832539
17842540 /* The allocation should succeed, stop compacting */
....@@ -1816,6 +2572,41 @@
18162572 return rc;
18172573 }
18182574
2575
+/*
2576
+ * Compact all zones within a node till each zone's fragmentation score
2577
+ * reaches within proactive compaction thresholds (as determined by the
2578
+ * proactiveness tunable).
2579
+ *
2580
+ * It is possible that the function returns before reaching score targets
2581
+ * due to various back-off conditions, such as, contention on per-node or
2582
+ * per-zone locks.
2583
+ */
2584
+static void proactive_compact_node(pg_data_t *pgdat)
2585
+{
2586
+ int zoneid;
2587
+ struct zone *zone;
2588
+ struct compact_control cc = {
2589
+ .order = -1,
2590
+ .mode = MIGRATE_SYNC_LIGHT,
2591
+ .ignore_skip_hint = true,
2592
+ .whole_zone = true,
2593
+ .gfp_mask = GFP_KERNEL,
2594
+ .proactive_compaction = true,
2595
+ };
2596
+
2597
+ for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
2598
+ zone = &pgdat->node_zones[zoneid];
2599
+ if (!populated_zone(zone))
2600
+ continue;
2601
+
2602
+ cc.zone = zone;
2603
+
2604
+ compact_zone(&cc, NULL);
2605
+
2606
+ VM_BUG_ON(!list_empty(&cc.freepages));
2607
+ VM_BUG_ON(!list_empty(&cc.migratepages));
2608
+ }
2609
+}
18192610
18202611 /* Compact all zones within a node */
18212612 static void compact_node(int nid)
....@@ -1840,7 +2631,7 @@
18402631
18412632 cc.zone = zone;
18422633
1843
- compact_zone(zone, &cc);
2634
+ compact_zone(&cc, NULL);
18442635
18452636 VM_BUG_ON(!list_empty(&cc.freepages));
18462637 VM_BUG_ON(!list_empty(&cc.migratepages));
....@@ -1863,22 +2654,45 @@
18632654 int sysctl_compact_memory;
18642655
18652656 /*
1866
- * This is the entry point for compacting all nodes via
1867
- * /proc/sys/vm/compact_memory
2657
+ * Tunable for proactive compaction. It determines how
2658
+ * aggressively the kernel should compact memory in the
2659
+ * background. It takes values in the range [0, 100].
18682660 */
1869
-int sysctl_compaction_handler(struct ctl_table *table, int write,
1870
- void __user *buffer, size_t *length, loff_t *ppos)
2661
+unsigned int __read_mostly sysctl_compaction_proactiveness = 20;
2662
+
2663
+int compaction_proactiveness_sysctl_handler(struct ctl_table *table, int write,
2664
+ void *buffer, size_t *length, loff_t *ppos)
18712665 {
1872
- if (write)
1873
- compact_nodes();
2666
+ int rc, nid;
2667
+
2668
+ rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
2669
+ if (rc)
2670
+ return rc;
2671
+
2672
+ if (write && sysctl_compaction_proactiveness) {
2673
+ for_each_online_node(nid) {
2674
+ pg_data_t *pgdat = NODE_DATA(nid);
2675
+
2676
+ if (pgdat->proactive_compact_trigger)
2677
+ continue;
2678
+
2679
+ pgdat->proactive_compact_trigger = true;
2680
+ wake_up_interruptible(&pgdat->kcompactd_wait);
2681
+ }
2682
+ }
18742683
18752684 return 0;
18762685 }
18772686
1878
-int sysctl_extfrag_handler(struct ctl_table *table, int write,
1879
- void __user *buffer, size_t *length, loff_t *ppos)
2687
+/*
2688
+ * This is the entry point for compacting all nodes via
2689
+ * /proc/sys/vm/compact_memory
2690
+ */
2691
+int sysctl_compaction_handler(struct ctl_table *table, int write,
2692
+ void *buffer, size_t *length, loff_t *ppos)
18802693 {
1881
- proc_dointvec_minmax(table, write, buffer, length, ppos);
2694
+ if (write)
2695
+ compact_nodes();
18822696
18832697 return 0;
18842698 }
....@@ -1914,23 +2728,24 @@
19142728
19152729 static inline bool kcompactd_work_requested(pg_data_t *pgdat)
19162730 {
1917
- return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
2731
+ return pgdat->kcompactd_max_order > 0 || kthread_should_stop() ||
2732
+ pgdat->proactive_compact_trigger;
19182733 }
19192734
19202735 static bool kcompactd_node_suitable(pg_data_t *pgdat)
19212736 {
19222737 int zoneid;
19232738 struct zone *zone;
1924
- enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
2739
+ enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
19252740
1926
- for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
2741
+ for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
19272742 zone = &pgdat->node_zones[zoneid];
19282743
19292744 if (!populated_zone(zone))
19302745 continue;
19312746
19322747 if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
1933
- classzone_idx) == COMPACT_CONTINUE)
2748
+ highest_zoneidx) == COMPACT_CONTINUE)
19342749 return true;
19352750 }
19362751
....@@ -1947,16 +2762,17 @@
19472762 struct zone *zone;
19482763 struct compact_control cc = {
19492764 .order = pgdat->kcompactd_max_order,
1950
- .classzone_idx = pgdat->kcompactd_classzone_idx,
2765
+ .search_order = pgdat->kcompactd_max_order,
2766
+ .highest_zoneidx = pgdat->kcompactd_highest_zoneidx,
19512767 .mode = MIGRATE_SYNC_LIGHT,
19522768 .ignore_skip_hint = false,
19532769 .gfp_mask = GFP_KERNEL,
19542770 };
19552771 trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
1956
- cc.classzone_idx);
2772
+ cc.highest_zoneidx);
19572773 count_compact_event(KCOMPACTD_WAKE);
19582774
1959
- for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
2775
+ for (zoneid = 0; zoneid <= cc.highest_zoneidx; zoneid++) {
19602776 int status;
19612777
19622778 zone = &pgdat->node_zones[zoneid];
....@@ -1974,7 +2790,7 @@
19742790 return;
19752791
19762792 cc.zone = zone;
1977
- status = compact_zone(zone, &cc);
2793
+ status = compact_zone(&cc, NULL);
19782794
19792795 if (status == COMPACT_SUCCESS) {
19802796 compaction_defer_reset(zone, cc.order, false);
....@@ -2005,16 +2821,16 @@
20052821
20062822 /*
20072823 * Regardless of success, we are done until woken up next. But remember
2008
- * the requested order/classzone_idx in case it was higher/tighter than
2009
- * our current ones
2824
+ * the requested order/highest_zoneidx in case it was higher/tighter
2825
+ * than our current ones
20102826 */
20112827 if (pgdat->kcompactd_max_order <= cc.order)
20122828 pgdat->kcompactd_max_order = 0;
2013
- if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
2014
- pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2829
+ if (pgdat->kcompactd_highest_zoneidx >= cc.highest_zoneidx)
2830
+ pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
20152831 }
20162832
2017
-void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
2833
+void wakeup_kcompactd(pg_data_t *pgdat, int order, int highest_zoneidx)
20182834 {
20192835 if (!order)
20202836 return;
....@@ -2022,8 +2838,8 @@
20222838 if (pgdat->kcompactd_max_order < order)
20232839 pgdat->kcompactd_max_order = order;
20242840
2025
- if (pgdat->kcompactd_classzone_idx > classzone_idx)
2026
- pgdat->kcompactd_classzone_idx = classzone_idx;
2841
+ if (pgdat->kcompactd_highest_zoneidx > highest_zoneidx)
2842
+ pgdat->kcompactd_highest_zoneidx = highest_zoneidx;
20272843
20282844 /*
20292845 * Pairs with implicit barrier in wait_event_freezable()
....@@ -2036,7 +2852,7 @@
20362852 return;
20372853
20382854 trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
2039
- classzone_idx);
2855
+ highest_zoneidx);
20402856 wake_up_interruptible(&pgdat->kcompactd_wait);
20412857 }
20422858
....@@ -2048,6 +2864,7 @@
20482864 {
20492865 pg_data_t *pgdat = (pg_data_t*)p;
20502866 struct task_struct *tsk = current;
2867
+ unsigned int proactive_defer = 0;
20512868
20522869 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
20532870
....@@ -2057,18 +2874,56 @@
20572874 set_freezable();
20582875
20592876 pgdat->kcompactd_max_order = 0;
2060
- pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
2877
+ pgdat->kcompactd_highest_zoneidx = pgdat->nr_zones - 1;
20612878
20622879 while (!kthread_should_stop()) {
20632880 unsigned long pflags;
2881
+ long timeout;
20642882
2883
+ timeout = sysctl_compaction_proactiveness ?
2884
+ msecs_to_jiffies(HPAGE_FRAG_CHECK_INTERVAL_MSEC) :
2885
+ MAX_SCHEDULE_TIMEOUT;
20652886 trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
2066
- wait_event_freezable(pgdat->kcompactd_wait,
2067
- kcompactd_work_requested(pgdat));
2887
+ if (wait_event_freezable_timeout(pgdat->kcompactd_wait,
2888
+ kcompactd_work_requested(pgdat), timeout) &&
2889
+ !pgdat->proactive_compact_trigger) {
20682890
2069
- psi_memstall_enter(&pflags);
2070
- kcompactd_do_work(pgdat);
2071
- psi_memstall_leave(&pflags);
2891
+ psi_memstall_enter(&pflags);
2892
+ kcompactd_do_work(pgdat);
2893
+ psi_memstall_leave(&pflags);
2894
+ continue;
2895
+ }
2896
+
2897
+ /* kcompactd wait timeout */
2898
+ if (should_proactive_compact_node(pgdat)) {
2899
+ unsigned int prev_score, score;
2900
+
2901
+ /*
2902
+ * On wakeup of proactive compaction by sysctl
2903
+ * write, ignore the accumulated defer score.
2904
+ * Anyway, if the proactive compaction didn't
2905
+ * make any progress for the new value, it will
2906
+ * be further deferred by 2^COMPACT_MAX_DEFER_SHIFT
2907
+ * times.
2908
+ */
2909
+ if (proactive_defer &&
2910
+ !pgdat->proactive_compact_trigger) {
2911
+ proactive_defer--;
2912
+ continue;
2913
+ }
2914
+
2915
+ prev_score = fragmentation_score_node(pgdat);
2916
+ proactive_compact_node(pgdat);
2917
+ score = fragmentation_score_node(pgdat);
2918
+ /*
2919
+ * Defer proactive compaction if the fragmentation
2920
+ * score did not go down i.e. no progress made.
2921
+ */
2922
+ proactive_defer = score < prev_score ?
2923
+ 0 : 1 << COMPACT_MAX_DEFER_SHIFT;
2924
+ }
2925
+ if (pgdat->proactive_compact_trigger)
2926
+ pgdat->proactive_compact_trigger = false;
20722927 }
20732928
20742929 return 0;