1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101 |
- #include <linux/cpu.h>
- #include <linux/swap.h>
- #include <linux/migrate.h>
- #include <linux/compaction.h>
- #include <linux/mm_inline.h>
- #include <linux/backing-dev.h>
- #include <linux/sysctl.h>
- #include <linux/sysfs.h>
- #include <linux/page-isolation.h>
- #include <linux/kasan.h>
- #include <linux/kthread.h>
- #include <linux/freezer.h>
- #include <linux/page_owner.h>
- #include <linux/psi.h>
- #include "internal.h"
- #ifdef CONFIG_COMPACTION
- static inline void count_compact_event(enum vm_event_item item)
- {
- count_vm_event(item);
- }
- static inline void count_compact_events(enum vm_event_item item, long delta)
- {
- count_vm_events(item, delta);
- }
- #else
- #define count_compact_event(item) do { } while (0)
- #define count_compact_events(item, delta) do { } while (0)
- #endif
- #if defined CONFIG_COMPACTION || defined CONFIG_CMA
- #define CREATE_TRACE_POINTS
- #include <trace/events/compaction.h>
- #define block_start_pfn(pfn, order) round_down(pfn, 1UL << (order))
- #define block_end_pfn(pfn, order) ALIGN((pfn) + 1, 1UL << (order))
- #define pageblock_start_pfn(pfn) block_start_pfn(pfn, pageblock_order)
- #define pageblock_end_pfn(pfn) block_end_pfn(pfn, pageblock_order)
- static unsigned long release_freepages(struct list_head *freelist)
- {
- struct page *page, *next;
- unsigned long high_pfn = 0;
- list_for_each_entry_safe(page, next, freelist, lru) {
- unsigned long pfn = page_to_pfn(page);
- list_del(&page->lru);
- __free_page(page);
- if (pfn > high_pfn)
- high_pfn = pfn;
- }
- return high_pfn;
- }
- static void map_pages(struct list_head *list)
- {
- unsigned int i, order, nr_pages;
- struct page *page, *next;
- LIST_HEAD(tmp_list);
- list_for_each_entry_safe(page, next, list, lru) {
- list_del(&page->lru);
- order = page_private(page);
- nr_pages = 1 << order;
- post_alloc_hook(page, order, __GFP_MOVABLE);
- if (order)
- split_page(page, order);
- for (i = 0; i < nr_pages; i++) {
- list_add(&page->lru, &tmp_list);
- page++;
- }
- }
- list_splice(&tmp_list, list);
- }
- static inline bool migrate_async_suitable(int migratetype)
- {
- return is_migrate_cma(migratetype) || migratetype == MIGRATE_MOVABLE;
- }
- #ifdef CONFIG_COMPACTION
- int PageMovable(struct page *page)
- {
- struct address_space *mapping;
- VM_BUG_ON_PAGE(!PageLocked(page), page);
- if (!__PageMovable(page))
- return 0;
- mapping = page_mapping(page);
- if (mapping && mapping->a_ops && mapping->a_ops->isolate_page)
- return 1;
- return 0;
- }
- EXPORT_SYMBOL(PageMovable);
- void __SetPageMovable(struct page *page, struct address_space *mapping)
- {
- VM_BUG_ON_PAGE(!PageLocked(page), page);
- VM_BUG_ON_PAGE((unsigned long)mapping & PAGE_MAPPING_MOVABLE, page);
- page->mapping = (void *)((unsigned long)mapping | PAGE_MAPPING_MOVABLE);
- }
- EXPORT_SYMBOL(__SetPageMovable);
- void __ClearPageMovable(struct page *page)
- {
- VM_BUG_ON_PAGE(!PageLocked(page), page);
- VM_BUG_ON_PAGE(!PageMovable(page), page);
-
- page->mapping = (void *)((unsigned long)page->mapping &
- PAGE_MAPPING_MOVABLE);
- }
- EXPORT_SYMBOL(__ClearPageMovable);
- #define COMPACT_MAX_DEFER_SHIFT 6
- void defer_compaction(struct zone *zone, int order)
- {
- zone->compact_considered = 0;
- zone->compact_defer_shift++;
- if (order < zone->compact_order_failed)
- zone->compact_order_failed = order;
- if (zone->compact_defer_shift > COMPACT_MAX_DEFER_SHIFT)
- zone->compact_defer_shift = COMPACT_MAX_DEFER_SHIFT;
- trace_mm_compaction_defer_compaction(zone, order);
- }
- bool compaction_deferred(struct zone *zone, int order)
- {
- unsigned long defer_limit = 1UL << zone->compact_defer_shift;
- if (order < zone->compact_order_failed)
- return false;
-
- if (++zone->compact_considered > defer_limit)
- zone->compact_considered = defer_limit;
- if (zone->compact_considered >= defer_limit)
- return false;
- trace_mm_compaction_deferred(zone, order);
- return true;
- }
- void compaction_defer_reset(struct zone *zone, int order,
- bool alloc_success)
- {
- if (alloc_success) {
- zone->compact_considered = 0;
- zone->compact_defer_shift = 0;
- }
- if (order >= zone->compact_order_failed)
- zone->compact_order_failed = order + 1;
- trace_mm_compaction_defer_reset(zone, order);
- }
- bool compaction_restarting(struct zone *zone, int order)
- {
- if (order < zone->compact_order_failed)
- return false;
- return zone->compact_defer_shift == COMPACT_MAX_DEFER_SHIFT &&
- zone->compact_considered >= 1UL << zone->compact_defer_shift;
- }
- static inline bool isolation_suitable(struct compact_control *cc,
- struct page *page)
- {
- if (cc->ignore_skip_hint)
- return true;
- return !get_pageblock_skip(page);
- }
- static void reset_cached_positions(struct zone *zone)
- {
- zone->compact_cached_migrate_pfn[0] = zone->zone_start_pfn;
- zone->compact_cached_migrate_pfn[1] = zone->zone_start_pfn;
- zone->compact_cached_free_pfn =
- pageblock_start_pfn(zone_end_pfn(zone) - 1);
- }
- static void __reset_isolation_suitable(struct zone *zone)
- {
- unsigned long start_pfn = zone->zone_start_pfn;
- unsigned long end_pfn = zone_end_pfn(zone);
- unsigned long pfn;
- zone->compact_blockskip_flush = false;
-
- for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
- struct page *page;
- cond_resched();
- if (!pfn_valid(pfn))
- continue;
- page = pfn_to_page(pfn);
- if (zone != page_zone(page))
- continue;
- clear_pageblock_skip(page);
- }
- reset_cached_positions(zone);
- }
- void reset_isolation_suitable(pg_data_t *pgdat)
- {
- int zoneid;
- for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
- struct zone *zone = &pgdat->node_zones[zoneid];
- if (!populated_zone(zone))
- continue;
-
- if (zone->compact_blockskip_flush)
- __reset_isolation_suitable(zone);
- }
- }
- static void update_pageblock_skip(struct compact_control *cc,
- struct page *page, unsigned long nr_isolated,
- bool migrate_scanner)
- {
- struct zone *zone = cc->zone;
- unsigned long pfn;
- if (cc->ignore_skip_hint)
- return;
- if (!page)
- return;
- if (nr_isolated)
- return;
- set_pageblock_skip(page);
- pfn = page_to_pfn(page);
-
- if (migrate_scanner) {
- if (pfn > zone->compact_cached_migrate_pfn[0])
- zone->compact_cached_migrate_pfn[0] = pfn;
- if (cc->mode != MIGRATE_ASYNC &&
- pfn > zone->compact_cached_migrate_pfn[1])
- zone->compact_cached_migrate_pfn[1] = pfn;
- } else {
- if (pfn < zone->compact_cached_free_pfn)
- zone->compact_cached_free_pfn = pfn;
- }
- }
- #else
- static inline bool isolation_suitable(struct compact_control *cc,
- struct page *page)
- {
- return true;
- }
- static void update_pageblock_skip(struct compact_control *cc,
- struct page *page, unsigned long nr_isolated,
- bool migrate_scanner)
- {
- }
- #endif
- static bool compact_trylock_irqsave(spinlock_t *lock, unsigned long *flags,
- struct compact_control *cc)
- {
- if (cc->mode == MIGRATE_ASYNC) {
- if (!spin_trylock_irqsave(lock, *flags)) {
- cc->contended = true;
- return false;
- }
- } else {
- spin_lock_irqsave(lock, *flags);
- }
- return true;
- }
- static bool compact_unlock_should_abort(spinlock_t *lock,
- unsigned long flags, bool *locked, struct compact_control *cc)
- {
- if (*locked) {
- spin_unlock_irqrestore(lock, flags);
- *locked = false;
- }
- if (fatal_signal_pending(current)) {
- cc->contended = true;
- return true;
- }
- if (need_resched()) {
- if (cc->mode == MIGRATE_ASYNC) {
- cc->contended = true;
- return true;
- }
- cond_resched();
- }
- return false;
- }
- static inline bool compact_should_abort(struct compact_control *cc)
- {
-
- if (need_resched()) {
- if (cc->mode == MIGRATE_ASYNC) {
- cc->contended = true;
- return true;
- }
- cond_resched();
- }
- return false;
- }
- static unsigned long isolate_freepages_block(struct compact_control *cc,
- unsigned long *start_pfn,
- unsigned long end_pfn,
- struct list_head *freelist,
- bool strict)
- {
- int nr_scanned = 0, total_isolated = 0;
- struct page *cursor, *valid_page = NULL;
- unsigned long flags = 0;
- bool locked = false;
- unsigned long blockpfn = *start_pfn;
- unsigned int order;
- cursor = pfn_to_page(blockpfn);
-
- for (; blockpfn < end_pfn; blockpfn++, cursor++) {
- int isolated;
- struct page *page = cursor;
-
- if (!(blockpfn % SWAP_CLUSTER_MAX)
- && compact_unlock_should_abort(&cc->zone->lock, flags,
- &locked, cc))
- break;
- nr_scanned++;
- if (!pfn_valid_within(blockpfn))
- goto isolate_fail;
- if (!valid_page)
- valid_page = page;
-
- if (PageCompound(page)) {
- unsigned int comp_order = compound_order(page);
- if (likely(comp_order < MAX_ORDER)) {
- blockpfn += (1UL << comp_order) - 1;
- cursor += (1UL << comp_order) - 1;
- }
- goto isolate_fail;
- }
- if (!PageBuddy(page))
- goto isolate_fail;
-
- if (!locked) {
-
- locked = compact_trylock_irqsave(&cc->zone->lock,
- &flags, cc);
- if (!locked)
- break;
-
- if (!PageBuddy(page))
- goto isolate_fail;
- }
-
- order = page_order(page);
- isolated = __isolate_free_page(page, order);
- if (!isolated)
- break;
- set_page_private(page, order);
- total_isolated += isolated;
- cc->nr_freepages += isolated;
- list_add_tail(&page->lru, freelist);
- if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
- blockpfn += isolated;
- break;
- }
-
- blockpfn += isolated - 1;
- cursor += isolated - 1;
- continue;
- isolate_fail:
- if (strict)
- break;
- else
- continue;
- }
- if (locked)
- spin_unlock_irqrestore(&cc->zone->lock, flags);
-
- if (unlikely(blockpfn > end_pfn))
- blockpfn = end_pfn;
- trace_mm_compaction_isolate_freepages(*start_pfn, blockpfn,
- nr_scanned, total_isolated);
-
- *start_pfn = blockpfn;
-
- if (strict && blockpfn < end_pfn)
- total_isolated = 0;
-
- if (blockpfn == end_pfn)
- update_pageblock_skip(cc, valid_page, total_isolated, false);
- count_compact_events(COMPACTFREE_SCANNED, nr_scanned);
- if (total_isolated)
- count_compact_events(COMPACTISOLATED, total_isolated);
- return total_isolated;
- }
- unsigned long
- isolate_freepages_range(struct compact_control *cc,
- unsigned long start_pfn, unsigned long end_pfn)
- {
- unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
- LIST_HEAD(freelist);
- pfn = start_pfn;
- block_start_pfn = pageblock_start_pfn(pfn);
- if (block_start_pfn < cc->zone->zone_start_pfn)
- block_start_pfn = cc->zone->zone_start_pfn;
- block_end_pfn = pageblock_end_pfn(pfn);
- for (; pfn < end_pfn; pfn += isolated,
- block_start_pfn = block_end_pfn,
- block_end_pfn += pageblock_nr_pages) {
-
- unsigned long isolate_start_pfn = pfn;
- block_end_pfn = min(block_end_pfn, end_pfn);
-
- if (pfn >= block_end_pfn) {
- block_start_pfn = pageblock_start_pfn(pfn);
- block_end_pfn = pageblock_end_pfn(pfn);
- block_end_pfn = min(block_end_pfn, end_pfn);
- }
- if (!pageblock_pfn_to_page(block_start_pfn,
- block_end_pfn, cc->zone))
- break;
- isolated = isolate_freepages_block(cc, &isolate_start_pfn,
- block_end_pfn, &freelist, true);
-
- if (!isolated)
- break;
-
- }
-
- map_pages(&freelist);
- if (pfn < end_pfn) {
-
- release_freepages(&freelist);
- return 0;
- }
-
- return pfn;
- }
- static bool __too_many_isolated(struct zone *zone, int safe)
- {
- unsigned long active, inactive, isolated;
- if (safe) {
- inactive = node_page_state_snapshot(zone->zone_pgdat,
- NR_INACTIVE_FILE) +
- node_page_state_snapshot(zone->zone_pgdat,
- NR_INACTIVE_ANON);
- active = node_page_state_snapshot(zone->zone_pgdat,
- NR_ACTIVE_FILE) +
- node_page_state_snapshot(zone->zone_pgdat,
- NR_ACTIVE_ANON);
- isolated = node_page_state_snapshot(zone->zone_pgdat,
- NR_ISOLATED_FILE) +
- node_page_state_snapshot(zone->zone_pgdat,
- NR_ISOLATED_ANON);
- } else {
- inactive = node_page_state(zone->zone_pgdat, NR_INACTIVE_FILE) +
- node_page_state(zone->zone_pgdat, NR_INACTIVE_ANON);
- active = node_page_state(zone->zone_pgdat, NR_ACTIVE_FILE) +
- node_page_state(zone->zone_pgdat, NR_ACTIVE_ANON);
- isolated = node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE) +
- node_page_state(zone->zone_pgdat, NR_ISOLATED_ANON);
- }
- return isolated > (inactive + active) / 2;
- }
- static bool too_many_isolated(struct compact_control *cc)
- {
-
- if (unlikely(__too_many_isolated(cc->zone, 0))) {
- if (cc->mode != MIGRATE_ASYNC)
- return __too_many_isolated(cc->zone, 1);
- }
- return false;
- }
- static unsigned long
- isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
- unsigned long end_pfn, isolate_mode_t isolate_mode)
- {
- struct zone *zone = cc->zone;
- unsigned long nr_scanned = 0, nr_isolated = 0;
- struct lruvec *lruvec;
- unsigned long flags = 0;
- bool locked = false;
- struct page *page = NULL, *valid_page = NULL;
- unsigned long start_pfn = low_pfn;
- bool skip_on_failure = false;
- unsigned long next_skip_pfn = 0;
-
- while (unlikely(too_many_isolated(cc))) {
-
- if (cc->mode == MIGRATE_ASYNC)
- return 0;
- congestion_wait(BLK_RW_ASYNC, HZ/10);
- if (fatal_signal_pending(current))
- return 0;
- }
- if (compact_should_abort(cc))
- return 0;
- if (cc->direct_compaction && (cc->mode == MIGRATE_ASYNC)) {
- skip_on_failure = true;
- next_skip_pfn = block_end_pfn(low_pfn, cc->order);
- }
-
- for (; low_pfn < end_pfn; low_pfn++) {
- if (skip_on_failure && low_pfn >= next_skip_pfn) {
-
- if (nr_isolated)
- break;
-
- next_skip_pfn = block_end_pfn(low_pfn, cc->order);
- }
-
- if (!(low_pfn % SWAP_CLUSTER_MAX)
- && compact_unlock_should_abort(zone_lru_lock(zone), flags,
- &locked, cc))
- break;
- if (!pfn_valid_within(low_pfn))
- goto isolate_fail;
- nr_scanned++;
- page = pfn_to_page(low_pfn);
- if (!valid_page)
- valid_page = page;
-
- if (PageBuddy(page)) {
- unsigned long freepage_order = page_order_unsafe(page);
-
- if (freepage_order > 0 && freepage_order < MAX_ORDER)
- low_pfn += (1UL << freepage_order) - 1;
- continue;
- }
-
- if (PageCompound(page)) {
- unsigned int comp_order = compound_order(page);
- if (likely(comp_order < MAX_ORDER))
- low_pfn += (1UL << comp_order) - 1;
- goto isolate_fail;
- }
-
- if (!PageLRU(page)) {
-
- if (unlikely(__PageMovable(page)) &&
- !PageIsolated(page)) {
- if (locked) {
- spin_unlock_irqrestore(zone_lru_lock(zone),
- flags);
- locked = false;
- }
- if (isolate_movable_page(page, isolate_mode))
- goto isolate_success;
- }
- goto isolate_fail;
- }
-
- if (!page_mapping(page) &&
- page_count(page) > page_mapcount(page))
- goto isolate_fail;
-
- if (!locked) {
- locked = compact_trylock_irqsave(zone_lru_lock(zone),
- &flags, cc);
- if (!locked)
- break;
-
- if (!PageLRU(page))
- goto isolate_fail;
-
- if (unlikely(PageCompound(page))) {
- low_pfn += (1UL << compound_order(page)) - 1;
- goto isolate_fail;
- }
- }
- lruvec = mem_cgroup_page_lruvec(page, zone->zone_pgdat);
-
- if (__isolate_lru_page(page, isolate_mode) != 0)
- goto isolate_fail;
- VM_BUG_ON_PAGE(PageCompound(page), page);
-
- del_page_from_lru_list(page, lruvec, page_lru(page));
- inc_node_page_state(page,
- NR_ISOLATED_ANON + page_is_file_cache(page));
- isolate_success:
- list_add(&page->lru, &cc->migratepages);
- cc->nr_migratepages++;
- nr_isolated++;
-
- if (!cc->last_migrated_pfn)
- cc->last_migrated_pfn = low_pfn;
-
- if (cc->nr_migratepages == COMPACT_CLUSTER_MAX) {
- ++low_pfn;
- break;
- }
- continue;
- isolate_fail:
- if (!skip_on_failure)
- continue;
-
- if (nr_isolated) {
- if (locked) {
- spin_unlock_irqrestore(zone_lru_lock(zone), flags);
- locked = false;
- }
- putback_movable_pages(&cc->migratepages);
- cc->nr_migratepages = 0;
- cc->last_migrated_pfn = 0;
- nr_isolated = 0;
- }
- if (low_pfn < next_skip_pfn) {
- low_pfn = next_skip_pfn - 1;
-
- next_skip_pfn += 1UL << cc->order;
- }
- }
-
- if (unlikely(low_pfn > end_pfn))
- low_pfn = end_pfn;
- if (locked)
- spin_unlock_irqrestore(zone_lru_lock(zone), flags);
-
- if (low_pfn == end_pfn)
- update_pageblock_skip(cc, valid_page, nr_isolated, true);
- trace_mm_compaction_isolate_migratepages(start_pfn, low_pfn,
- nr_scanned, nr_isolated);
- count_compact_events(COMPACTMIGRATE_SCANNED, nr_scanned);
- if (nr_isolated)
- count_compact_events(COMPACTISOLATED, nr_isolated);
- return low_pfn;
- }
- unsigned long
- isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
- unsigned long end_pfn)
- {
- unsigned long pfn, block_start_pfn, block_end_pfn;
-
- pfn = start_pfn;
- block_start_pfn = pageblock_start_pfn(pfn);
- if (block_start_pfn < cc->zone->zone_start_pfn)
- block_start_pfn = cc->zone->zone_start_pfn;
- block_end_pfn = pageblock_end_pfn(pfn);
- for (; pfn < end_pfn; pfn = block_end_pfn,
- block_start_pfn = block_end_pfn,
- block_end_pfn += pageblock_nr_pages) {
- block_end_pfn = min(block_end_pfn, end_pfn);
- if (!pageblock_pfn_to_page(block_start_pfn,
- block_end_pfn, cc->zone))
- continue;
- pfn = isolate_migratepages_block(cc, pfn, block_end_pfn,
- ISOLATE_UNEVICTABLE);
- if (!pfn)
- break;
- if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
- break;
- }
- return pfn;
- }
- #endif
- #ifdef CONFIG_COMPACTION
- static bool suitable_migration_target(struct compact_control *cc,
- struct page *page)
- {
- if (cc->ignore_block_suitable)
- return true;
-
- if (PageBuddy(page)) {
-
- if (page_order_unsafe(page) >= pageblock_order)
- return false;
- }
-
- if (migrate_async_suitable(get_pageblock_migratetype(page)))
- return true;
-
- return false;
- }
- static inline bool compact_scanners_met(struct compact_control *cc)
- {
- return (cc->free_pfn >> pageblock_order)
- <= (cc->migrate_pfn >> pageblock_order);
- }
- static void isolate_freepages(struct compact_control *cc)
- {
- struct zone *zone = cc->zone;
- struct page *page;
- unsigned long block_start_pfn;
- unsigned long isolate_start_pfn;
- unsigned long block_end_pfn;
- unsigned long low_pfn;
- struct list_head *freelist = &cc->freepages;
-
- isolate_start_pfn = cc->free_pfn;
- block_start_pfn = pageblock_start_pfn(cc->free_pfn);
- block_end_pfn = min(block_start_pfn + pageblock_nr_pages,
- zone_end_pfn(zone));
- low_pfn = pageblock_end_pfn(cc->migrate_pfn);
-
- for (; block_start_pfn >= low_pfn;
- block_end_pfn = block_start_pfn,
- block_start_pfn -= pageblock_nr_pages,
- isolate_start_pfn = block_start_pfn) {
-
- if (!(block_start_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
- && compact_should_abort(cc))
- break;
- page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
- zone);
- if (!page)
- continue;
-
- if (!suitable_migration_target(cc, page))
- continue;
-
- if (!isolation_suitable(cc, page))
- continue;
-
- isolate_freepages_block(cc, &isolate_start_pfn, block_end_pfn,
- freelist, false);
-
- if ((cc->nr_freepages >= cc->nr_migratepages)
- || cc->contended) {
- if (isolate_start_pfn >= block_end_pfn) {
-
- isolate_start_pfn =
- block_start_pfn - pageblock_nr_pages;
- }
- break;
- } else if (isolate_start_pfn < block_end_pfn) {
-
- break;
- }
- }
-
- map_pages(freelist);
-
- cc->free_pfn = isolate_start_pfn;
- }
- static struct page *compaction_alloc(struct page *migratepage,
- unsigned long data,
- int **result)
- {
- struct compact_control *cc = (struct compact_control *)data;
- struct page *freepage;
-
- if (list_empty(&cc->freepages)) {
- if (!cc->contended)
- isolate_freepages(cc);
- if (list_empty(&cc->freepages))
- return NULL;
- }
- freepage = list_entry(cc->freepages.next, struct page, lru);
- list_del(&freepage->lru);
- cc->nr_freepages--;
- return freepage;
- }
- static void compaction_free(struct page *page, unsigned long data)
- {
- struct compact_control *cc = (struct compact_control *)data;
- list_add(&page->lru, &cc->freepages);
- cc->nr_freepages++;
- }
- typedef enum {
- ISOLATE_ABORT,
- ISOLATE_NONE,
- ISOLATE_SUCCESS,
- } isolate_migrate_t;
- int sysctl_compact_unevictable_allowed __read_mostly = 1;
- static isolate_migrate_t isolate_migratepages(struct zone *zone,
- struct compact_control *cc)
- {
- unsigned long block_start_pfn;
- unsigned long block_end_pfn;
- unsigned long low_pfn;
- struct page *page;
- const isolate_mode_t isolate_mode =
- (sysctl_compact_unevictable_allowed ? ISOLATE_UNEVICTABLE : 0) |
- (cc->mode != MIGRATE_SYNC ? ISOLATE_ASYNC_MIGRATE : 0);
-
- low_pfn = cc->migrate_pfn;
- block_start_pfn = pageblock_start_pfn(low_pfn);
- if (block_start_pfn < zone->zone_start_pfn)
- block_start_pfn = zone->zone_start_pfn;
-
- block_end_pfn = pageblock_end_pfn(low_pfn);
-
- for (; block_end_pfn <= cc->free_pfn;
- low_pfn = block_end_pfn,
- block_start_pfn = block_end_pfn,
- block_end_pfn += pageblock_nr_pages) {
-
- if (!(low_pfn % (SWAP_CLUSTER_MAX * pageblock_nr_pages))
- && compact_should_abort(cc))
- break;
- page = pageblock_pfn_to_page(block_start_pfn, block_end_pfn,
- zone);
- if (!page)
- continue;
-
- if (!isolation_suitable(cc, page))
- continue;
-
- if (cc->mode == MIGRATE_ASYNC &&
- !migrate_async_suitable(get_pageblock_migratetype(page)))
- continue;
-
- low_pfn = isolate_migratepages_block(cc, low_pfn,
- block_end_pfn, isolate_mode);
- if (!low_pfn || cc->contended)
- return ISOLATE_ABORT;
-
- break;
- }
-
- cc->migrate_pfn = low_pfn;
- return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
- }
- static inline bool is_via_compact_memory(int order)
- {
- return order == -1;
- }
- static enum compact_result __compact_finished(struct zone *zone, struct compact_control *cc,
- const int migratetype)
- {
- unsigned int order;
- unsigned long watermark;
- if (cc->contended || fatal_signal_pending(current))
- return COMPACT_CONTENDED;
-
- if (compact_scanners_met(cc)) {
-
- reset_cached_positions(zone);
-
- if (cc->direct_compaction)
- zone->compact_blockskip_flush = true;
- if (cc->whole_zone)
- return COMPACT_COMPLETE;
- else
- return COMPACT_PARTIAL_SKIPPED;
- }
- if (is_via_compact_memory(cc->order))
- return COMPACT_CONTINUE;
-
- watermark = zone->watermark[cc->alloc_flags & ALLOC_WMARK_MASK];
- if (!zone_watermark_ok(zone, cc->order, watermark, cc->classzone_idx,
- cc->alloc_flags))
- return COMPACT_CONTINUE;
-
- for (order = cc->order; order < MAX_ORDER; order++) {
- struct free_area *area = &zone->free_area[order];
- bool can_steal;
-
- if (!list_empty(&area->free_list[migratetype]))
- return COMPACT_SUCCESS;
- #ifdef CONFIG_CMA
-
- if (migratetype == MIGRATE_MOVABLE &&
- !list_empty(&area->free_list[MIGRATE_CMA]))
- return COMPACT_SUCCESS;
- #endif
-
- if (find_suitable_fallback(area, order, migratetype,
- true, &can_steal) != -1)
- return COMPACT_SUCCESS;
- }
- return COMPACT_NO_SUITABLE_PAGE;
- }
- static enum compact_result compact_finished(struct zone *zone,
- struct compact_control *cc,
- const int migratetype)
- {
- int ret;
- ret = __compact_finished(zone, cc, migratetype);
- trace_mm_compaction_finished(zone, cc->order, ret);
- if (ret == COMPACT_NO_SUITABLE_PAGE)
- ret = COMPACT_CONTINUE;
- return ret;
- }
- static enum compact_result __compaction_suitable(struct zone *zone, int order,
- unsigned int alloc_flags,
- int classzone_idx,
- unsigned long wmark_target)
- {
- unsigned long watermark;
- if (is_via_compact_memory(order))
- return COMPACT_CONTINUE;
- watermark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
-
- if (zone_watermark_ok(zone, order, watermark, classzone_idx,
- alloc_flags))
- return COMPACT_SUCCESS;
-
- watermark = (order > PAGE_ALLOC_COSTLY_ORDER) ?
- low_wmark_pages(zone) : min_wmark_pages(zone);
- watermark += compact_gap(order);
- if (!__zone_watermark_ok(zone, 0, watermark, classzone_idx,
- ALLOC_CMA, wmark_target))
- return COMPACT_SKIPPED;
- return COMPACT_CONTINUE;
- }
- enum compact_result compaction_suitable(struct zone *zone, int order,
- unsigned int alloc_flags,
- int classzone_idx)
- {
- enum compact_result ret;
- int fragindex;
- ret = __compaction_suitable(zone, order, alloc_flags, classzone_idx,
- zone_page_state(zone, NR_FREE_PAGES));
-
- if (ret == COMPACT_CONTINUE && (order > PAGE_ALLOC_COSTLY_ORDER)) {
- fragindex = fragmentation_index(zone, order);
- if (fragindex >= 0 && fragindex <= sysctl_extfrag_threshold)
- ret = COMPACT_NOT_SUITABLE_ZONE;
- }
- trace_mm_compaction_suitable(zone, order, ret);
- if (ret == COMPACT_NOT_SUITABLE_ZONE)
- ret = COMPACT_SKIPPED;
- return ret;
- }
- bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
- int alloc_flags)
- {
- struct zone *zone;
- struct zoneref *z;
-
- for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
- ac->nodemask) {
- unsigned long available;
- enum compact_result compact_result;
-
- available = zone_reclaimable_pages(zone) / order;
- available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
- compact_result = __compaction_suitable(zone, order, alloc_flags,
- ac_classzone_idx(ac), available);
- if (compact_result != COMPACT_SKIPPED)
- return true;
- }
- return false;
- }
- static enum compact_result compact_zone(struct zone *zone, struct compact_control *cc)
- {
- enum compact_result ret;
- unsigned long start_pfn = zone->zone_start_pfn;
- unsigned long end_pfn = zone_end_pfn(zone);
- const int migratetype = gfpflags_to_migratetype(cc->gfp_mask);
- const bool sync = cc->mode != MIGRATE_ASYNC;
- ktime_t event_ts;
- ret = compaction_suitable(zone, cc->order, cc->alloc_flags,
- cc->classzone_idx);
-
- if (ret == COMPACT_SUCCESS || ret == COMPACT_SKIPPED)
- return ret;
-
- VM_BUG_ON(ret != COMPACT_CONTINUE);
-
- if (compaction_restarting(zone, cc->order))
- __reset_isolation_suitable(zone);
-
- if (cc->whole_zone) {
- cc->migrate_pfn = start_pfn;
- cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
- } else {
- cc->migrate_pfn = zone->compact_cached_migrate_pfn[sync];
- cc->free_pfn = zone->compact_cached_free_pfn;
- if (cc->free_pfn < start_pfn || cc->free_pfn >= end_pfn) {
- cc->free_pfn = pageblock_start_pfn(end_pfn - 1);
- zone->compact_cached_free_pfn = cc->free_pfn;
- }
- if (cc->migrate_pfn < start_pfn || cc->migrate_pfn >= end_pfn) {
- cc->migrate_pfn = start_pfn;
- zone->compact_cached_migrate_pfn[0] = cc->migrate_pfn;
- zone->compact_cached_migrate_pfn[1] = cc->migrate_pfn;
- }
- if (cc->migrate_pfn == start_pfn)
- cc->whole_zone = true;
- }
- cc->last_migrated_pfn = 0;
- mm_event_start(&event_ts);
- trace_mm_compaction_begin(start_pfn, cc->migrate_pfn,
- cc->free_pfn, end_pfn, sync);
- migrate_prep_local();
- while ((ret = compact_finished(zone, cc, migratetype)) ==
- COMPACT_CONTINUE) {
- int err;
- switch (isolate_migratepages(zone, cc)) {
- case ISOLATE_ABORT:
- ret = COMPACT_CONTENDED;
- putback_movable_pages(&cc->migratepages);
- cc->nr_migratepages = 0;
- goto out;
- case ISOLATE_NONE:
-
- goto check_drain;
- case ISOLATE_SUCCESS:
- ;
- }
- err = migrate_pages(&cc->migratepages, compaction_alloc,
- compaction_free, (unsigned long)cc, cc->mode,
- MR_COMPACTION);
- trace_mm_compaction_migratepages(cc->nr_migratepages, err,
- &cc->migratepages);
-
- cc->nr_migratepages = 0;
- if (err) {
- putback_movable_pages(&cc->migratepages);
-
- if (err == -ENOMEM && !compact_scanners_met(cc)) {
- ret = COMPACT_CONTENDED;
- goto out;
- }
-
- if (cc->direct_compaction &&
- (cc->mode == MIGRATE_ASYNC)) {
- cc->migrate_pfn = block_end_pfn(
- cc->migrate_pfn - 1, cc->order);
-
- cc->last_migrated_pfn = 0;
- }
- }
- check_drain:
-
- if (cc->order > 0 && cc->last_migrated_pfn) {
- int cpu;
- unsigned long current_block_start =
- block_start_pfn(cc->migrate_pfn, cc->order);
- if (cc->last_migrated_pfn < current_block_start) {
- cpu = get_cpu();
- lru_add_drain_cpu(cpu);
- drain_local_pages(zone);
- put_cpu();
-
- cc->last_migrated_pfn = 0;
- }
- }
- }
- out:
- mm_event_end(MM_COMPACTION, event_ts);
-
- if (cc->nr_freepages > 0) {
- unsigned long free_pfn = release_freepages(&cc->freepages);
- cc->nr_freepages = 0;
- VM_BUG_ON(free_pfn == 0);
-
- free_pfn = pageblock_start_pfn(free_pfn);
-
- if (free_pfn > zone->compact_cached_free_pfn)
- zone->compact_cached_free_pfn = free_pfn;
- }
- trace_mm_compaction_end(start_pfn, cc->migrate_pfn,
- cc->free_pfn, end_pfn, sync, ret);
- return ret;
- }
- static enum compact_result compact_zone_order(struct zone *zone, int order,
- gfp_t gfp_mask, enum compact_priority prio,
- unsigned int alloc_flags, int classzone_idx)
- {
- enum compact_result ret;
- struct compact_control cc = {
- .nr_freepages = 0,
- .nr_migratepages = 0,
- .order = order,
- .gfp_mask = gfp_mask,
- .zone = zone,
- .mode = (prio == COMPACT_PRIO_ASYNC) ?
- MIGRATE_ASYNC : MIGRATE_SYNC_LIGHT,
- .alloc_flags = alloc_flags,
- .classzone_idx = classzone_idx,
- .direct_compaction = true,
- .whole_zone = (prio == MIN_COMPACT_PRIORITY),
- .ignore_skip_hint = (prio == MIN_COMPACT_PRIORITY),
- .ignore_block_suitable = (prio == MIN_COMPACT_PRIORITY)
- };
- INIT_LIST_HEAD(&cc.freepages);
- INIT_LIST_HEAD(&cc.migratepages);
- ret = compact_zone(zone, &cc);
- VM_BUG_ON(!list_empty(&cc.freepages));
- VM_BUG_ON(!list_empty(&cc.migratepages));
- return ret;
- }
- int sysctl_extfrag_threshold = 500;
- enum compact_result try_to_compact_pages(gfp_t gfp_mask, unsigned int order,
- unsigned int alloc_flags, const struct alloc_context *ac,
- enum compact_priority prio)
- {
- int may_enter_fs = gfp_mask & __GFP_FS;
- int may_perform_io = gfp_mask & __GFP_IO;
- struct zoneref *z;
- struct zone *zone;
- enum compact_result rc = COMPACT_SKIPPED;
- ktime_t event_ts;
-
- if (!may_enter_fs || !may_perform_io)
- return COMPACT_SKIPPED;
- mm_event_start(&event_ts);
- trace_mm_compaction_try_to_compact_pages(order, gfp_mask, prio);
-
- for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
- ac->nodemask) {
- enum compact_result status;
- if (prio > MIN_COMPACT_PRIORITY
- && compaction_deferred(zone, order)) {
- rc = max_t(enum compact_result, COMPACT_DEFERRED, rc);
- continue;
- }
- status = compact_zone_order(zone, order, gfp_mask, prio,
- alloc_flags, ac_classzone_idx(ac));
- rc = max(status, rc);
-
- if (status == COMPACT_SUCCESS) {
-
- compaction_defer_reset(zone, order, false);
- break;
- }
- if (prio != COMPACT_PRIO_ASYNC && (status == COMPACT_COMPLETE ||
- status == COMPACT_PARTIAL_SKIPPED))
-
- defer_compaction(zone, order);
-
- if ((prio == COMPACT_PRIO_ASYNC && need_resched())
- || fatal_signal_pending(current))
- break;
- }
- mm_event_end(MM_COMPACTION, event_ts);
- return rc;
- }
- static void compact_node(int nid)
- {
- pg_data_t *pgdat = NODE_DATA(nid);
- int zoneid;
- struct zone *zone;
- struct compact_control cc = {
- .order = -1,
- .mode = MIGRATE_SYNC,
- .ignore_skip_hint = true,
- .whole_zone = true,
- };
- for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
- zone = &pgdat->node_zones[zoneid];
- if (!populated_zone(zone))
- continue;
- cc.nr_freepages = 0;
- cc.nr_migratepages = 0;
- cc.zone = zone;
- INIT_LIST_HEAD(&cc.freepages);
- INIT_LIST_HEAD(&cc.migratepages);
- compact_zone(zone, &cc);
- VM_BUG_ON(!list_empty(&cc.freepages));
- VM_BUG_ON(!list_empty(&cc.migratepages));
- }
- }
- static void compact_nodes(void)
- {
- int nid;
-
- lru_add_drain_all();
- for_each_online_node(nid)
- compact_node(nid);
- }
- int sysctl_compact_memory;
- int sysctl_compaction_handler(struct ctl_table *table, int write,
- void __user *buffer, size_t *length, loff_t *ppos)
- {
- if (write)
- compact_nodes();
- return 0;
- }
- int sysctl_extfrag_handler(struct ctl_table *table, int write,
- void __user *buffer, size_t *length, loff_t *ppos)
- {
- proc_dointvec_minmax(table, write, buffer, length, ppos);
- return 0;
- }
- #if defined(CONFIG_SYSFS) && defined(CONFIG_NUMA)
- static ssize_t sysfs_compact_node(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- int nid = dev->id;
- if (nid >= 0 && nid < nr_node_ids && node_online(nid)) {
-
- lru_add_drain_all();
- compact_node(nid);
- }
- return count;
- }
- static DEVICE_ATTR(compact, S_IWUSR, NULL, sysfs_compact_node);
- int compaction_register_node(struct node *node)
- {
- return device_create_file(&node->dev, &dev_attr_compact);
- }
- void compaction_unregister_node(struct node *node)
- {
- return device_remove_file(&node->dev, &dev_attr_compact);
- }
- #endif
- static inline bool kcompactd_work_requested(pg_data_t *pgdat)
- {
- return pgdat->kcompactd_max_order > 0 || kthread_should_stop();
- }
- static bool kcompactd_node_suitable(pg_data_t *pgdat)
- {
- int zoneid;
- struct zone *zone;
- enum zone_type classzone_idx = pgdat->kcompactd_classzone_idx;
- for (zoneid = 0; zoneid <= classzone_idx; zoneid++) {
- zone = &pgdat->node_zones[zoneid];
- if (!populated_zone(zone))
- continue;
- if (compaction_suitable(zone, pgdat->kcompactd_max_order, 0,
- classzone_idx) == COMPACT_CONTINUE)
- return true;
- }
- return false;
- }
- static void kcompactd_do_work(pg_data_t *pgdat)
- {
-
- int zoneid;
- struct zone *zone;
- struct compact_control cc = {
- .order = pgdat->kcompactd_max_order,
- .classzone_idx = pgdat->kcompactd_classzone_idx,
- .mode = MIGRATE_SYNC_LIGHT,
- .ignore_skip_hint = true,
- };
- trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
- cc.classzone_idx);
- count_vm_event(KCOMPACTD_WAKE);
- for (zoneid = 0; zoneid <= cc.classzone_idx; zoneid++) {
- int status;
- zone = &pgdat->node_zones[zoneid];
- if (!populated_zone(zone))
- continue;
- if (compaction_deferred(zone, cc.order))
- continue;
- if (compaction_suitable(zone, cc.order, 0, zoneid) !=
- COMPACT_CONTINUE)
- continue;
- cc.nr_freepages = 0;
- cc.nr_migratepages = 0;
- cc.zone = zone;
- INIT_LIST_HEAD(&cc.freepages);
- INIT_LIST_HEAD(&cc.migratepages);
- if (kthread_should_stop())
- return;
- status = compact_zone(zone, &cc);
- if (status == COMPACT_SUCCESS) {
- compaction_defer_reset(zone, cc.order, false);
- } else if (status == COMPACT_PARTIAL_SKIPPED || status == COMPACT_COMPLETE) {
-
- defer_compaction(zone, cc.order);
- }
- VM_BUG_ON(!list_empty(&cc.freepages));
- VM_BUG_ON(!list_empty(&cc.migratepages));
- }
-
- if (pgdat->kcompactd_max_order <= cc.order)
- pgdat->kcompactd_max_order = 0;
- if (pgdat->kcompactd_classzone_idx >= cc.classzone_idx)
- pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
- }
- void wakeup_kcompactd(pg_data_t *pgdat, int order, int classzone_idx)
- {
- if (!order)
- return;
- if (pgdat->kcompactd_max_order < order)
- pgdat->kcompactd_max_order = order;
- if (pgdat->kcompactd_classzone_idx > classzone_idx)
- pgdat->kcompactd_classzone_idx = classzone_idx;
- if (!waitqueue_active(&pgdat->kcompactd_wait))
- return;
- if (!kcompactd_node_suitable(pgdat))
- return;
- trace_mm_compaction_wakeup_kcompactd(pgdat->node_id, order,
- classzone_idx);
- wake_up_interruptible(&pgdat->kcompactd_wait);
- }
- static int kcompactd(void *p)
- {
- pg_data_t *pgdat = (pg_data_t*)p;
- struct task_struct *tsk = current;
- const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
- if (!cpumask_empty(cpumask))
- set_cpus_allowed_ptr(tsk, cpumask);
- set_freezable();
- pgdat->kcompactd_max_order = 0;
- pgdat->kcompactd_classzone_idx = pgdat->nr_zones - 1;
- while (!kthread_should_stop()) {
- unsigned long pflags;
- trace_mm_compaction_kcompactd_sleep(pgdat->node_id);
- wait_event_freezable(pgdat->kcompactd_wait,
- kcompactd_work_requested(pgdat));
- psi_memstall_enter(&pflags);
- kcompactd_do_work(pgdat);
- psi_memstall_leave(&pflags);
- }
- return 0;
- }
- int kcompactd_run(int nid)
- {
- pg_data_t *pgdat = NODE_DATA(nid);
- int ret = 0;
- if (pgdat->kcompactd)
- return 0;
- pgdat->kcompactd = kthread_run(kcompactd, pgdat, "kcompactd%d", nid);
- if (IS_ERR(pgdat->kcompactd)) {
- pr_err("Failed to start kcompactd on node %d\n", nid);
- ret = PTR_ERR(pgdat->kcompactd);
- pgdat->kcompactd = NULL;
- }
- return ret;
- }
- void kcompactd_stop(int nid)
- {
- struct task_struct *kcompactd = NODE_DATA(nid)->kcompactd;
- if (kcompactd) {
- kthread_stop(kcompactd);
- NODE_DATA(nid)->kcompactd = NULL;
- }
- }
- static int cpu_callback(struct notifier_block *nfb, unsigned long action,
- void *hcpu)
- {
- int nid;
- if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) {
- for_each_node_state(nid, N_MEMORY) {
- pg_data_t *pgdat = NODE_DATA(nid);
- const struct cpumask *mask;
- mask = cpumask_of_node(pgdat->node_id);
- if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
-
- set_cpus_allowed_ptr(pgdat->kcompactd, mask);
- }
- }
- return NOTIFY_OK;
- }
- static int __init kcompactd_init(void)
- {
- int nid;
- for_each_node_state(nid, N_MEMORY)
- kcompactd_run(nid);
- hotcpu_notifier(cpu_callback, 0);
- return 0;
- }
- subsys_initcall(kcompactd_init)
- #endif
|