15 #include <linux/sysctl.h>
19 #if defined CONFIG_COMPACTION || defined CONFIG_CMA
21 #define CREATE_TRACE_POINTS
24 static unsigned long release_freepages(
struct list_head *freelist)
27 unsigned long count = 0;
48 static inline bool migrate_async_suitable(
int migratetype)
53 #ifdef CONFIG_COMPACTION
55 static inline bool isolation_suitable(
struct compact_control *
cc,
58 if (cc->ignore_skip_hint)
61 return !get_pageblock_skip(page);
69 static void __reset_isolation_suitable(
struct zone *
zone)
75 zone->compact_cached_migrate_pfn = start_pfn;
76 zone->compact_cached_free_pfn = end_pfn;
77 zone->compact_blockskip_flush =
false;
89 if (zone != page_zone(page))
92 clear_pageblock_skip(page);
96 void reset_isolation_suitable(
pg_data_t *pgdat)
100 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
101 struct zone *zone = &pgdat->
node_zones[zoneid];
102 if (!populated_zone(zone))
106 if (zone->compact_blockskip_flush)
107 __reset_isolation_suitable(zone);
115 static void update_pageblock_skip(
struct compact_control *cc,
116 struct page *page,
unsigned long nr_isolated,
117 bool migrate_scanner)
119 struct zone *zone = cc->zone;
125 set_pageblock_skip(page);
128 if (migrate_scanner) {
129 if (!cc->finished_update_migrate &&
130 pfn > zone->compact_cached_migrate_pfn)
131 zone->compact_cached_migrate_pfn = pfn;
133 if (!cc->finished_update_free &&
134 pfn < zone->compact_cached_free_pfn)
135 zone->compact_cached_free_pfn = pfn;
140 static inline bool isolation_suitable(
struct compact_control *cc,
146 static void update_pageblock_skip(
struct compact_control *cc,
147 struct page *page,
unsigned long nr_isolated,
148 bool migrate_scanner)
155 return need_resched() || spin_is_contended(lock);
168 bool locked,
struct compact_control *cc)
170 if (should_release_lock(lock)) {
172 spin_unlock_irqrestore(lock, *flags);
178 cc->contended =
true;
190 static inline bool compact_trylock_irqsave(
spinlock_t *lock,
191 unsigned long *flags,
struct compact_control *cc)
193 return compact_checklock_irqsave(lock, flags,
false, cc);
197 static bool suitable_migration_target(
struct page *page)
199 int migratetype = get_pageblock_migratetype(page);
210 if (migrate_async_suitable(migratetype))
217 static void compact_capture_page(
struct compact_control *cc)
220 int mtype, mtype_low, mtype_high;
222 if (!cc->page || *cc->page)
239 mtype_low = cc->migratetype;
240 mtype_high = cc->migratetype + 1;
244 for (mtype = mtype_low; mtype < mtype_high; mtype++) {
246 for (order = cc->order; order <
MAX_ORDER; order++) {
249 area = &(cc->zone->free_area[
order]);
254 if (!compact_trylock_irqsave(&cc->zone->lock, &flags, cc))
256 if (!list_empty(&area->
free_list[mtype])) {
260 spin_unlock_irqrestore(&cc->zone->lock,
266 spin_unlock_irqrestore(&cc->zone->lock, flags);
277 static unsigned long isolate_freepages_block(
struct compact_control *cc,
278 unsigned long blockpfn,
279 unsigned long end_pfn,
283 int nr_scanned = 0, total_isolated = 0;
284 struct page *cursor, *valid_page =
NULL;
285 unsigned long nr_strict_required = end_pfn - blockpfn;
292 for (; blockpfn < end_pfn; blockpfn++, cursor++) {
294 struct page *page = cursor;
301 if (!PageBuddy(page))
312 locked = compact_checklock_irqsave(&cc->zone->lock, &flags,
318 if (!strict && !suitable_migration_target(page))
322 if (!PageBuddy(page))
327 if (!isolated && strict)
329 total_isolated += isolated;
330 for (i = 0; i < isolated; i++) {
331 list_add(&page->
lru, freelist);
337 blockpfn += isolated - 1;
338 cursor += isolated - 1;
342 trace_mm_compaction_isolate_freepages(nr_scanned, total_isolated);
349 if (strict && nr_strict_required > total_isolated)
353 spin_unlock_irqrestore(&cc->zone->lock, flags);
356 if (blockpfn == end_pfn)
357 update_pageblock_skip(cc, valid_page, total_isolated,
false);
359 return total_isolated;
376 isolate_freepages_range(
struct compact_control *cc,
377 unsigned long start_pfn,
unsigned long end_pfn)
379 unsigned long isolated, pfn, block_end_pfn;
382 for (pfn = start_pfn; pfn < end_pfn; pfn += isolated) {
391 block_end_pfn =
min(block_end_pfn, end_pfn);
393 isolated = isolate_freepages_block(cc, pfn, block_end_pfn,
412 map_pages(&freelist);
416 release_freepages(&freelist);
425 static void acct_isolated(
struct zone *zone,
bool locked,
struct compact_control *cc)
428 unsigned int count[2] = { 0, };
431 count[!!page_is_file_cache(page)]++;
444 static bool too_many_isolated(
struct zone *zone)
455 return isolated > (inactive +
active) / 2;
479 isolate_migratepages_range(
struct zone *zone,
struct compact_control *cc,
480 unsigned long low_pfn,
unsigned long end_pfn,
bool unevictable)
482 unsigned long last_pageblock_nr = 0, pageblock_nr;
483 unsigned long nr_scanned = 0, nr_isolated = 0;
484 struct list_head *migratelist = &cc->migratepages;
489 struct page *page =
NULL, *valid_page =
NULL;
496 while (
unlikely(too_many_isolated(zone))) {
503 if (fatal_signal_pending(
current))
509 for (; low_pfn < end_pfn; low_pfn++) {
511 if (locked && !((low_pfn+1) % SWAP_CLUSTER_MAX)) {
512 if (should_release_lock(&zone->
lru_lock)) {
513 spin_unlock_irqrestore(&zone->
lru_lock, flags);
542 if (page_zone(page) != zone)
550 if (!isolation_suitable(cc, page))
562 if (!cc->sync && last_pageblock_nr != pageblock_nr &&
563 !migrate_async_suitable(get_pageblock_migratetype(page))) {
564 cc->finished_update_migrate =
true;
582 if (PageTransHuge(page)) {
585 low_pfn += (1 << compound_order(page)) - 1;
590 locked = compact_checklock_irqsave(&zone->
lru_lock, &flags,
592 if (!locked || fatal_signal_pending(
current))
598 if (PageTransHuge(page)) {
599 low_pfn += (1 << compound_order(page)) - 1;
618 cc->finished_update_migrate =
true;
619 del_page_from_lru_list(page, lruvec, page_lru(page));
620 list_add(&page->
lru, migratelist);
621 cc->nr_migratepages++;
625 if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
635 last_pageblock_nr = pageblock_nr;
638 acct_isolated(zone, locked, cc);
641 spin_unlock_irqrestore(&zone->
lru_lock, flags);
644 if (low_pfn == end_pfn)
645 update_pageblock_skip(cc, valid_page, nr_isolated,
true);
647 trace_mm_compaction_isolate_migratepages(nr_scanned, nr_isolated);
653 #ifdef CONFIG_COMPACTION
658 static void isolate_freepages(
struct zone *zone,
659 struct compact_control *cc)
662 unsigned long high_pfn, low_pfn, pfn, zone_end_pfn, end_pfn;
663 int nr_freepages = cc->nr_freepages;
664 struct list_head *freelist = &cc->freepages;
679 high_pfn =
min(low_pfn, pfn);
688 for (; pfn > low_pfn && cc->nr_migratepages > nr_freepages;
690 unsigned long isolated;
703 if (page_zone(page) != zone)
707 if (!suitable_migration_target(page))
711 if (!isolation_suitable(cc, page))
724 end_pfn =
min(end_pfn, zone_end_pfn);
725 isolated = isolate_freepages_block(cc, pfn, end_pfn,
727 nr_freepages += isolated;
735 cc->finished_update_free =
true;
736 high_pfn =
max(high_pfn, pfn);
743 cc->free_pfn = high_pfn;
744 cc->nr_freepages = nr_freepages;
751 static struct page *compaction_alloc(
struct page *migratepage,
755 struct compact_control *cc = (
struct compact_control *)data;
756 struct page *freepage;
759 if (list_empty(&cc->freepages)) {
760 isolate_freepages(cc->zone, cc);
762 if (list_empty(&cc->freepages))
778 static void update_nr_listpages(
struct compact_control *cc)
780 int nr_migratepages = 0;
781 int nr_freepages = 0;
789 cc->nr_migratepages = nr_migratepages;
790 cc->nr_freepages = nr_freepages;
804 static isolate_migrate_t isolate_migratepages(
struct zone *zone,
805 struct compact_control *cc)
807 unsigned long low_pfn, end_pfn;
816 if (end_pfn > cc->free_pfn || !
pfn_valid(low_pfn)) {
817 cc->migrate_pfn = end_pfn;
822 low_pfn = isolate_migratepages_range(zone, cc, low_pfn, end_pfn,
false);
823 if (!low_pfn || cc->contended)
824 return ISOLATE_ABORT;
826 cc->migrate_pfn = low_pfn;
828 return ISOLATE_SUCCESS;
831 static int compact_finished(
struct zone *zone,
832 struct compact_control *cc)
834 unsigned long watermark;
836 if (fatal_signal_pending(
current))
840 if (cc->free_pfn <= cc->migrate_pfn) {
847 if (!current_is_kswapd())
848 zone->compact_blockskip_flush =
true;
862 watermark += (1 << cc->order);
874 for (order = cc->order; order <
MAX_ORDER; order++) {
877 if (!list_empty(&area->
free_list[cc->migratetype]))
881 if (cc->order >= pageblock_order && area->
nr_free)
896 unsigned long compaction_suitable(
struct zone *zone,
int order)
899 unsigned long watermark;
928 fragindex = fragmentation_index(zone, order);
929 if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
939 static int compact_zone(
struct zone *zone,
struct compact_control *cc)
945 ret = compaction_suitable(zone, cc->order);
961 cc->migrate_pfn = zone->compact_cached_migrate_pfn;
962 cc->free_pfn = zone->compact_cached_free_pfn;
963 if (cc->free_pfn < start_pfn || cc->free_pfn > end_pfn) {
965 zone->compact_cached_free_pfn = cc->free_pfn;
967 if (cc->migrate_pfn < start_pfn || cc->migrate_pfn > end_pfn) {
968 cc->migrate_pfn = start_pfn;
969 zone->compact_cached_migrate_pfn = cc->migrate_pfn;
977 if (compaction_restarting(zone, cc->order) && !current_is_kswapd())
978 __reset_isolation_suitable(zone);
983 unsigned long nr_migrate, nr_remaining;
986 switch (isolate_migratepages(zone, cc)) {
990 cc->nr_migratepages = 0;
994 case ISOLATE_SUCCESS:
998 nr_migrate = cc->nr_migratepages;
1000 (
unsigned long)cc,
false,
1002 update_nr_listpages(cc);
1003 nr_remaining = cc->nr_migratepages;
1005 count_vm_event(COMPACTBLOCKS);
1006 count_vm_events(COMPACTPAGES, nr_migrate - nr_remaining);
1008 count_vm_events(COMPACTPAGEFAILED, nr_remaining);
1009 trace_mm_compaction_migratepages(nr_migrate - nr_remaining,
1015 cc->nr_migratepages = 0;
1023 compact_capture_page(cc);
1028 cc->nr_freepages -= release_freepages(&cc->freepages);
1034 static unsigned long compact_zone_order(
struct zone *zone,
1036 bool sync,
bool *contended,
1040 struct compact_control cc = {
1042 .nr_migratepages = 0,
1044 .migratetype = allocflags_to_migratetype(gfp_mask),
1049 INIT_LIST_HEAD(&cc.freepages);
1050 INIT_LIST_HEAD(&cc.migratepages);
1052 ret = compact_zone(zone, &cc);
1055 VM_BUG_ON(!list_empty(&cc.migratepages));
1057 *contended = cc.contended;
1061 int sysctl_extfrag_threshold = 500;
1077 bool sync,
bool *contended,
struct page **page)
1079 enum zone_type high_zoneidx = gfp_zone(gfp_mask);
1080 int may_enter_fs = gfp_mask &
__GFP_FS;
1081 int may_perform_io = gfp_mask &
__GFP_IO;
1085 int alloc_flags = 0;
1088 if (!order || !may_enter_fs || !may_perform_io)
1091 count_vm_event(COMPACTSTALL);
1102 status = compact_zone_order(zone, order, gfp_mask, sync,
1104 rc =
max(status, rc);
1117 static int __compact_pgdat(
pg_data_t *pgdat,
struct compact_control *cc)
1122 for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
1125 if (!populated_zone(zone))
1128 cc->nr_freepages = 0;
1129 cc->nr_migratepages = 0;
1131 INIT_LIST_HEAD(&cc->freepages);
1132 INIT_LIST_HEAD(&cc->migratepages);
1134 if (cc->order == -1 || !compaction_deferred(zone, cc->order))
1135 compact_zone(zone, cc);
1137 if (cc->order > 0) {
1140 if (ok && cc->order >= zone->compact_order_failed)
1141 zone->compact_order_failed = cc->order + 1;
1143 else if (!ok && cc->sync)
1144 defer_compaction(zone, cc->order);
1148 VM_BUG_ON(!list_empty(&cc->migratepages));
1154 int compact_pgdat(
pg_data_t *pgdat,
int order)
1156 struct compact_control cc = {
1162 return __compact_pgdat(pgdat, &cc);
1165 static int compact_node(
int nid)
1167 struct compact_control cc = {
1173 return __compact_pgdat(
NODE_DATA(nid), &cc);
1177 static int compact_nodes(
void)
1191 int sysctl_compact_memory;
1198 return compact_nodes();
1203 int sysctl_extfrag_handler(
struct ctl_table *table,
int write,
1204 void __user *buffer,
size_t *length, loff_t *ppos)
1211 #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
1229 int compaction_register_node(
struct node *
node)
1234 void compaction_unregister_node(
struct node *
node)