page_alloc.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160
  1. /*
  2. * linux/mm/page_alloc.c
  3. *
  4. * Manages the free list, the system allocates free pages here.
  5. * Note that kmalloc() lives in slab.c
  6. *
  7. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  8. * Swap reorganised 29.12.95, Stephen Tweedie
  9. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
  10. * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
  11. * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
  12. * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
  13. * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
  14. * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
  15. */
  16. #include <linux/stddef.h>
  17. #include <linux/mm.h>
  18. #include <linux/swap.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/compiler.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kmemcheck.h>
  26. #include <linux/module.h>
  27. #include <linux/suspend.h>
  28. #include <linux/pagevec.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/slab.h>
  31. #include <linux/oom.h>
  32. #include <linux/notifier.h>
  33. #include <linux/topology.h>
  34. #include <linux/sysctl.h>
  35. #include <linux/cpu.h>
  36. #include <linux/cpuset.h>
  37. #include <linux/memory_hotplug.h>
  38. #include <linux/nodemask.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/mempolicy.h>
  41. #include <linux/stop_machine.h>
  42. #include <linux/sort.h>
  43. #include <linux/pfn.h>
  44. #include <linux/backing-dev.h>
  45. #include <linux/fault-inject.h>
  46. #include <linux/page-isolation.h>
  47. #include <linux/page_cgroup.h>
  48. #include <linux/debugobjects.h>
  49. #include <linux/kmemleak.h>
  50. #include <linux/memory.h>
  51. #include <trace/events/kmem.h>
  52. #include <asm/tlbflush.h>
  53. #include <asm/div64.h>
  54. #include "internal.h"
  55. /*
  56. * Array of node states.
  57. */
  58. nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
  59. [N_POSSIBLE] = NODE_MASK_ALL,
  60. [N_ONLINE] = { { [0] = 1UL } },
  61. #ifndef CONFIG_NUMA
  62. [N_NORMAL_MEMORY] = { { [0] = 1UL } },
  63. #ifdef CONFIG_HIGHMEM
  64. [N_HIGH_MEMORY] = { { [0] = 1UL } },
  65. #endif
  66. [N_CPU] = { { [0] = 1UL } },
  67. #endif /* NUMA */
  68. };
  69. EXPORT_SYMBOL(node_states);
  70. unsigned long totalram_pages __read_mostly;
  71. unsigned long totalreserve_pages __read_mostly;
  72. int percpu_pagelist_fraction;
  73. gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
  74. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  75. int pageblock_order __read_mostly;
  76. #endif
  77. static void __free_pages_ok(struct page *page, unsigned int order);
  78. /*
  79. * results with 256, 32 in the lowmem_reserve sysctl:
  80. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  81. * 1G machine -> (16M dma, 784M normal, 224M high)
  82. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  83. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  84. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  85. *
  86. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  87. * don't need any ZONE_NORMAL reservation
  88. */
  89. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
  90. #ifdef CONFIG_ZONE_DMA
  91. 256,
  92. #endif
  93. #ifdef CONFIG_ZONE_DMA32
  94. 256,
  95. #endif
  96. #ifdef CONFIG_HIGHMEM
  97. 32,
  98. #endif
  99. 32,
  100. };
  101. EXPORT_SYMBOL(totalram_pages);
  102. static char * const zone_names[MAX_NR_ZONES] = {
  103. #ifdef CONFIG_ZONE_DMA
  104. "DMA",
  105. #endif
  106. #ifdef CONFIG_ZONE_DMA32
  107. "DMA32",
  108. #endif
  109. "Normal",
  110. #ifdef CONFIG_HIGHMEM
  111. "HighMem",
  112. #endif
  113. "Movable",
  114. };
  115. int min_free_kbytes = 1024;
  116. static unsigned long __meminitdata nr_kernel_pages;
  117. static unsigned long __meminitdata nr_all_pages;
  118. static unsigned long __meminitdata dma_reserve;
  119. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  120. /*
  121. * MAX_ACTIVE_REGIONS determines the maximum number of distinct
  122. * ranges of memory (RAM) that may be registered with add_active_range().
  123. * Ranges passed to add_active_range() will be merged if possible
  124. * so the number of times add_active_range() can be called is
  125. * related to the number of nodes and the number of holes
  126. */
  127. #ifdef CONFIG_MAX_ACTIVE_REGIONS
  128. /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
  129. #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
  130. #else
  131. #if MAX_NUMNODES >= 32
  132. /* If there can be many nodes, allow up to 50 holes per node */
  133. #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
  134. #else
  135. /* By default, allow up to 256 distinct regions */
  136. #define MAX_ACTIVE_REGIONS 256
  137. #endif
  138. #endif
  139. static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
  140. static int __meminitdata nr_nodemap_entries;
  141. static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
  142. static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
  143. static unsigned long __initdata required_kernelcore;
  144. static unsigned long __initdata required_movablecore;
  145. static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
  146. /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
  147. int movable_zone;
  148. EXPORT_SYMBOL(movable_zone);
  149. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  150. #if MAX_NUMNODES > 1
  151. int nr_node_ids __read_mostly = MAX_NUMNODES;
  152. int nr_online_nodes __read_mostly = 1;
  153. EXPORT_SYMBOL(nr_node_ids);
  154. EXPORT_SYMBOL(nr_online_nodes);
  155. #endif
  156. int page_group_by_mobility_disabled __read_mostly;
  157. static void set_pageblock_migratetype(struct page *page, int migratetype)
  158. {
  159. if (unlikely(page_group_by_mobility_disabled))
  160. migratetype = MIGRATE_UNMOVABLE;
  161. set_pageblock_flags_group(page, (unsigned long)migratetype,
  162. PB_migrate, PB_migrate_end);
  163. }
  164. bool oom_killer_disabled __read_mostly;
  165. #ifdef CONFIG_DEBUG_VM
  166. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  167. {
  168. int ret = 0;
  169. unsigned seq;
  170. unsigned long pfn = page_to_pfn(page);
  171. do {
  172. seq = zone_span_seqbegin(zone);
  173. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  174. ret = 1;
  175. else if (pfn < zone->zone_start_pfn)
  176. ret = 1;
  177. } while (zone_span_seqretry(zone, seq));
  178. return ret;
  179. }
  180. static int page_is_consistent(struct zone *zone, struct page *page)
  181. {
  182. if (!pfn_valid_within(page_to_pfn(page)))
  183. return 0;
  184. if (zone != page_zone(page))
  185. return 0;
  186. return 1;
  187. }
  188. /*
  189. * Temporary debugging check for pages not lying within a given zone.
  190. */
  191. static int bad_range(struct zone *zone, struct page *page)
  192. {
  193. if (page_outside_zone_boundaries(zone, page))
  194. return 1;
  195. if (!page_is_consistent(zone, page))
  196. return 1;
  197. return 0;
  198. }
  199. #else
  200. static inline int bad_range(struct zone *zone, struct page *page)
  201. {
  202. return 0;
  203. }
  204. #endif
  205. static void bad_page(struct page *page)
  206. {
  207. static unsigned long resume;
  208. static unsigned long nr_shown;
  209. static unsigned long nr_unshown;
  210. /* Don't complain about poisoned pages */
  211. if (PageHWPoison(page)) {
  212. __ClearPageBuddy(page);
  213. return;
  214. }
  215. /*
  216. * Allow a burst of 60 reports, then keep quiet for that minute;
  217. * or allow a steady drip of one report per second.
  218. */
  219. if (nr_shown == 60) {
  220. if (time_before(jiffies, resume)) {
  221. nr_unshown++;
  222. goto out;
  223. }
  224. if (nr_unshown) {
  225. printk(KERN_ALERT
  226. "BUG: Bad page state: %lu messages suppressed\n",
  227. nr_unshown);
  228. nr_unshown = 0;
  229. }
  230. nr_shown = 0;
  231. }
  232. if (nr_shown++ == 0)
  233. resume = jiffies + 60 * HZ;
  234. printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n",
  235. current->comm, page_to_pfn(page));
  236. printk(KERN_ALERT
  237. "page:%p flags:%p count:%d mapcount:%d mapping:%p index:%lx\n",
  238. page, (void *)page->flags, page_count(page),
  239. page_mapcount(page), page->mapping, page->index);
  240. dump_stack();
  241. out:
  242. /* Leave bad fields for debug, except PageBuddy could make trouble */
  243. __ClearPageBuddy(page);
  244. add_taint(TAINT_BAD_PAGE);
  245. }
  246. /*
  247. * Higher-order pages are called "compound pages". They are structured thusly:
  248. *
  249. * The first PAGE_SIZE page is called the "head page".
  250. *
  251. * The remaining PAGE_SIZE pages are called "tail pages".
  252. *
  253. * All pages have PG_compound set. All pages have their ->private pointing at
  254. * the head page (even the head page has this).
  255. *
  256. * The first tail page's ->lru.next holds the address of the compound page's
  257. * put_page() function. Its ->lru.prev holds the order of allocation.
  258. * This usage means that zero-order pages may not be compound.
  259. */
  260. static void free_compound_page(struct page *page)
  261. {
  262. __free_pages_ok(page, compound_order(page));
  263. }
  264. void prep_compound_page(struct page *page, unsigned long order)
  265. {
  266. int i;
  267. int nr_pages = 1 << order;
  268. set_compound_page_dtor(page, free_compound_page);
  269. set_compound_order(page, order);
  270. __SetPageHead(page);
  271. for (i = 1; i < nr_pages; i++) {
  272. struct page *p = page + i;
  273. __SetPageTail(p);
  274. p->first_page = page;
  275. }
  276. }
  277. static int destroy_compound_page(struct page *page, unsigned long order)
  278. {
  279. int i;
  280. int nr_pages = 1 << order;
  281. int bad = 0;
  282. if (unlikely(compound_order(page) != order) ||
  283. unlikely(!PageHead(page))) {
  284. bad_page(page);
  285. bad++;
  286. }
  287. __ClearPageHead(page);
  288. for (i = 1; i < nr_pages; i++) {
  289. struct page *p = page + i;
  290. if (unlikely(!PageTail(p) || (p->first_page != page))) {
  291. bad_page(page);
  292. bad++;
  293. }
  294. __ClearPageTail(p);
  295. }
  296. return bad;
  297. }
  298. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  299. {
  300. int i;
  301. /*
  302. * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
  303. * and __GFP_HIGHMEM from hard or soft interrupt context.
  304. */
  305. VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
  306. for (i = 0; i < (1 << order); i++)
  307. clear_highpage(page + i);
  308. }
  309. static inline void set_page_order(struct page *page, int order)
  310. {
  311. set_page_private(page, order);
  312. __SetPageBuddy(page);
  313. }
  314. static inline void rmv_page_order(struct page *page)
  315. {
  316. __ClearPageBuddy(page);
  317. set_page_private(page, 0);
  318. }
  319. /*
  320. * Locate the struct page for both the matching buddy in our
  321. * pair (buddy1) and the combined O(n+1) page they form (page).
  322. *
  323. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  324. * the following equation:
  325. * B2 = B1 ^ (1 << O)
  326. * For example, if the starting buddy (buddy2) is #8 its order
  327. * 1 buddy is #10:
  328. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  329. *
  330. * 2) Any buddy B will have an order O+1 parent P which
  331. * satisfies the following equation:
  332. * P = B & ~(1 << O)
  333. *
  334. * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
  335. */
  336. static inline struct page *
  337. __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
  338. {
  339. unsigned long buddy_idx = page_idx ^ (1 << order);
  340. return page + (buddy_idx - page_idx);
  341. }
  342. static inline unsigned long
  343. __find_combined_index(unsigned long page_idx, unsigned int order)
  344. {
  345. return (page_idx & ~(1 << order));
  346. }
  347. /*
  348. * This function checks whether a page is free && is the buddy
  349. * we can do coalesce a page and its buddy if
  350. * (a) the buddy is not in a hole &&
  351. * (b) the buddy is in the buddy system &&
  352. * (c) a page and its buddy have the same order &&
  353. * (d) a page and its buddy are in the same zone.
  354. *
  355. * For recording whether a page is in the buddy system, we use PG_buddy.
  356. * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
  357. *
  358. * For recording page's order, we use page_private(page).
  359. */
  360. static inline int page_is_buddy(struct page *page, struct page *buddy,
  361. int order)
  362. {
  363. if (!pfn_valid_within(page_to_pfn(buddy)))
  364. return 0;
  365. if (page_zone_id(page) != page_zone_id(buddy))
  366. return 0;
  367. if (PageBuddy(buddy) && page_order(buddy) == order) {
  368. VM_BUG_ON(page_count(buddy) != 0);
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. /*
  374. * Freeing function for a buddy system allocator.
  375. *
  376. * The concept of a buddy system is to maintain direct-mapped table
  377. * (containing bit values) for memory blocks of various "orders".
  378. * The bottom level table contains the map for the smallest allocatable
  379. * units of memory (here, pages), and each level above it describes
  380. * pairs of units from the levels below, hence, "buddies".
  381. * At a high level, all that happens here is marking the table entry
  382. * at the bottom level available, and propagating the changes upward
  383. * as necessary, plus some accounting needed to play nicely with other
  384. * parts of the VM system.
  385. * At each level, we keep a list of pages, which are heads of continuous
  386. * free pages of length of (1 << order) and marked with PG_buddy. Page's
  387. * order is recorded in page_private(page) field.
  388. * So when we are allocating or freeing one, we can derive the state of the
  389. * other. That is, if we allocate a small block, and both were
  390. * free, the remainder of the region must be split into blocks.
  391. * If a block is freed, and its buddy is also free, then this
  392. * triggers coalescing into a block of larger size.
  393. *
  394. * -- wli
  395. */
  396. static inline void __free_one_page(struct page *page,
  397. struct zone *zone, unsigned int order,
  398. int migratetype)
  399. {
  400. unsigned long page_idx;
  401. if (unlikely(PageCompound(page)))
  402. if (unlikely(destroy_compound_page(page, order)))
  403. return;
  404. VM_BUG_ON(migratetype == -1);
  405. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  406. VM_BUG_ON(page_idx & ((1 << order) - 1));
  407. VM_BUG_ON(bad_range(zone, page));
  408. while (order < MAX_ORDER-1) {
  409. unsigned long combined_idx;
  410. struct page *buddy;
  411. buddy = __page_find_buddy(page, page_idx, order);
  412. if (!page_is_buddy(page, buddy, order))
  413. break;
  414. /* Our buddy is free, merge with it and move up one order. */
  415. list_del(&buddy->lru);
  416. zone->free_area[order].nr_free--;
  417. rmv_page_order(buddy);
  418. combined_idx = __find_combined_index(page_idx, order);
  419. page = page + (combined_idx - page_idx);
  420. page_idx = combined_idx;
  421. order++;
  422. }
  423. set_page_order(page, order);
  424. list_add(&page->lru,
  425. &zone->free_area[order].free_list[migratetype]);
  426. zone->free_area[order].nr_free++;
  427. }
  428. /*
  429. * free_page_mlock() -- clean up attempts to free and mlocked() page.
  430. * Page should not be on lru, so no need to fix that up.
  431. * free_pages_check() will verify...
  432. */
  433. static inline void free_page_mlock(struct page *page)
  434. {
  435. __dec_zone_page_state(page, NR_MLOCK);
  436. __count_vm_event(UNEVICTABLE_MLOCKFREED);
  437. }
  438. static inline int free_pages_check(struct page *page)
  439. {
  440. if (unlikely(page_mapcount(page) |
  441. (page->mapping != NULL) |
  442. (atomic_read(&page->_count) != 0) |
  443. (page->flags & PAGE_FLAGS_CHECK_AT_FREE))) {
  444. bad_page(page);
  445. return 1;
  446. }
  447. if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
  448. page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  449. return 0;
  450. }
  451. /*
  452. * Frees a number of pages from the PCP lists
  453. * Assumes all pages on list are in same zone, and of same order.
  454. * count is the number of pages to free.
  455. *
  456. * If the zone was previously in an "all pages pinned" state then look to
  457. * see if this freeing clears that state.
  458. *
  459. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  460. * pinned" detection logic.
  461. */
  462. static void free_pcppages_bulk(struct zone *zone, int count,
  463. struct per_cpu_pages *pcp)
  464. {
  465. int migratetype = 0;
  466. int batch_free = 0;
  467. spin_lock(&zone->lock);
  468. zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
  469. zone->pages_scanned = 0;
  470. __mod_zone_page_state(zone, NR_FREE_PAGES, count);
  471. while (count) {
  472. struct page *page;
  473. struct list_head *list;
  474. /*
  475. * Remove pages from lists in a round-robin fashion. A
  476. * batch_free count is maintained that is incremented when an
  477. * empty list is encountered. This is so more pages are freed
  478. * off fuller lists instead of spinning excessively around empty
  479. * lists
  480. */
  481. do {
  482. batch_free++;
  483. if (++migratetype == MIGRATE_PCPTYPES)
  484. migratetype = 0;
  485. list = &pcp->lists[migratetype];
  486. } while (list_empty(list));
  487. do {
  488. page = list_entry(list->prev, struct page, lru);
  489. /* must delete as __free_one_page list manipulates */
  490. list_del(&page->lru);
  491. /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  492. __free_one_page(page, zone, 0, page_private(page));
  493. trace_mm_page_pcpu_drain(page, 0, page_private(page));
  494. } while (--count && --batch_free && !list_empty(list));
  495. }
  496. spin_unlock(&zone->lock);
  497. }
  498. static void free_one_page(struct zone *zone, struct page *page, int order,
  499. int migratetype)
  500. {
  501. spin_lock(&zone->lock);
  502. zone_clear_flag(zone, ZONE_ALL_UNRECLAIMABLE);
  503. zone->pages_scanned = 0;
  504. __mod_zone_page_state(zone, NR_FREE_PAGES, 1 << order);
  505. __free_one_page(page, zone, order, migratetype);
  506. spin_unlock(&zone->lock);
  507. }
  508. static void __free_pages_ok(struct page *page, unsigned int order)
  509. {
  510. unsigned long flags;
  511. int i;
  512. int bad = 0;
  513. int wasMlocked = __TestClearPageMlocked(page);
  514. kmemcheck_free_shadow(page, order);
  515. for (i = 0 ; i < (1 << order) ; ++i)
  516. bad += free_pages_check(page + i);
  517. if (bad)
  518. return;
  519. if (!PageHighMem(page)) {
  520. debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
  521. debug_check_no_obj_freed(page_address(page),
  522. PAGE_SIZE << order);
  523. }
  524. arch_free_page(page, order);
  525. kernel_map_pages(page, 1 << order, 0);
  526. local_irq_save(flags);
  527. if (unlikely(wasMlocked))
  528. free_page_mlock(page);
  529. __count_vm_events(PGFREE, 1 << order);
  530. free_one_page(page_zone(page), page, order,
  531. get_pageblock_migratetype(page));
  532. local_irq_restore(flags);
  533. }
  534. /*
  535. * permit the bootmem allocator to evade page validation on high-order frees
  536. */
  537. void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
  538. {
  539. if (order == 0) {
  540. __ClearPageReserved(page);
  541. set_page_count(page, 0);
  542. set_page_refcounted(page);
  543. __free_page(page);
  544. } else {
  545. int loop;
  546. prefetchw(page);
  547. for (loop = 0; loop < BITS_PER_LONG; loop++) {
  548. struct page *p = &page[loop];
  549. if (loop + 1 < BITS_PER_LONG)
  550. prefetchw(p + 1);
  551. __ClearPageReserved(p);
  552. set_page_count(p, 0);
  553. }
  554. set_page_refcounted(page);
  555. __free_pages(page, order);
  556. }
  557. }
  558. /*
  559. * The order of subdivision here is critical for the IO subsystem.
  560. * Please do not alter this order without good reasons and regression
  561. * testing. Specifically, as large blocks of memory are subdivided,
  562. * the order in which smaller blocks are delivered depends on the order
  563. * they're subdivided in this function. This is the primary factor
  564. * influencing the order in which pages are delivered to the IO
  565. * subsystem according to empirical testing, and this is also justified
  566. * by considering the behavior of a buddy system containing a single
  567. * large block of memory acted on by a series of small allocations.
  568. * This behavior is a critical factor in sglist merging's success.
  569. *
  570. * -- wli
  571. */
  572. static inline void expand(struct zone *zone, struct page *page,
  573. int low, int high, struct free_area *area,
  574. int migratetype)
  575. {
  576. unsigned long size = 1 << high;
  577. while (high > low) {
  578. area--;
  579. high--;
  580. size >>= 1;
  581. VM_BUG_ON(bad_range(zone, &page[size]));
  582. list_add(&page[size].lru, &area->free_list[migratetype]);
  583. area->nr_free++;
  584. set_page_order(&page[size], high);
  585. }
  586. }
  587. /*
  588. * This page is about to be returned from the page allocator
  589. */
  590. static inline int check_new_page(struct page *page)
  591. {
  592. if (unlikely(page_mapcount(page) |
  593. (page->mapping != NULL) |
  594. (atomic_read(&page->_count) != 0) |
  595. (page->flags & PAGE_FLAGS_CHECK_AT_PREP))) {
  596. bad_page(page);
  597. return 1;
  598. }
  599. return 0;
  600. }
  601. static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
  602. {
  603. int i;
  604. for (i = 0; i < (1 << order); i++) {
  605. struct page *p = page + i;
  606. if (unlikely(check_new_page(p)))
  607. return 1;
  608. }
  609. set_page_private(page, 0);
  610. set_page_refcounted(page);
  611. arch_alloc_page(page, order);
  612. kernel_map_pages(page, 1 << order, 1);
  613. if (gfp_flags & __GFP_ZERO)
  614. prep_zero_page(page, order, gfp_flags);
  615. if (order && (gfp_flags & __GFP_COMP))
  616. prep_compound_page(page, order);
  617. return 0;
  618. }
  619. /*
  620. * Go through the free lists for the given migratetype and remove
  621. * the smallest available page from the freelists
  622. */
  623. static inline
  624. struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
  625. int migratetype)
  626. {
  627. unsigned int current_order;
  628. struct free_area * area;
  629. struct page *page;
  630. /* Find a page of the appropriate size in the preferred list */
  631. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  632. area = &(zone->free_area[current_order]);
  633. if (list_empty(&area->free_list[migratetype]))
  634. continue;
  635. page = list_entry(area->free_list[migratetype].next,
  636. struct page, lru);
  637. list_del(&page->lru);
  638. rmv_page_order(page);
  639. area->nr_free--;
  640. expand(zone, page, order, current_order, area, migratetype);
  641. return page;
  642. }
  643. return NULL;
  644. }
  645. /*
  646. * This array describes the order lists are fallen back to when
  647. * the free lists for the desirable migrate type are depleted
  648. */
  649. static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = {
  650. [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
  651. [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
  652. [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
  653. [MIGRATE_RESERVE] = { MIGRATE_RESERVE, MIGRATE_RESERVE, MIGRATE_RESERVE }, /* Never used */
  654. };
  655. /*
  656. * Move the free pages in a range to the free lists of the requested type.
  657. * Note that start_page and end_pages are not aligned on a pageblock
  658. * boundary. If alignment is required, use move_freepages_block()
  659. */
  660. static int move_freepages(struct zone *zone,
  661. struct page *start_page, struct page *end_page,
  662. int migratetype)
  663. {
  664. struct page *page;
  665. unsigned long order;
  666. int pages_moved = 0;
  667. #ifndef CONFIG_HOLES_IN_ZONE
  668. /*
  669. * page_zone is not safe to call in this context when
  670. * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
  671. * anyway as we check zone boundaries in move_freepages_block().
  672. * Remove at a later date when no bug reports exist related to
  673. * grouping pages by mobility
  674. */
  675. BUG_ON(page_zone(start_page) != page_zone(end_page));
  676. #endif
  677. for (page = start_page; page <= end_page;) {
  678. /* Make sure we are not inadvertently changing nodes */
  679. VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
  680. if (!pfn_valid_within(page_to_pfn(page))) {
  681. page++;
  682. continue;
  683. }
  684. if (!PageBuddy(page)) {
  685. page++;
  686. continue;
  687. }
  688. order = page_order(page);
  689. list_del(&page->lru);
  690. list_add(&page->lru,
  691. &zone->free_area[order].free_list[migratetype]);
  692. page += 1 << order;
  693. pages_moved += 1 << order;
  694. }
  695. return pages_moved;
  696. }
  697. static int move_freepages_block(struct zone *zone, struct page *page,
  698. int migratetype)
  699. {
  700. unsigned long start_pfn, end_pfn;
  701. struct page *start_page, *end_page;
  702. start_pfn = page_to_pfn(page);
  703. start_pfn = start_pfn & ~(pageblock_nr_pages-1);
  704. start_page = pfn_to_page(start_pfn);
  705. end_page = start_page + pageblock_nr_pages - 1;
  706. end_pfn = start_pfn + pageblock_nr_pages - 1;
  707. /* Do not cross zone boundaries */
  708. if (start_pfn < zone->zone_start_pfn)
  709. start_page = page;
  710. if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
  711. return 0;
  712. return move_freepages(zone, start_page, end_page, migratetype);
  713. }
  714. static void change_pageblock_range(struct page *pageblock_page,
  715. int start_order, int migratetype)
  716. {
  717. int nr_pageblocks = 1 << (start_order - pageblock_order);
  718. while (nr_pageblocks--) {
  719. set_pageblock_migratetype(pageblock_page, migratetype);
  720. pageblock_page += pageblock_nr_pages;
  721. }
  722. }
  723. /* Remove an element from the buddy allocator from the fallback list */
  724. static inline struct page *
  725. __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
  726. {
  727. struct free_area * area;
  728. int current_order;
  729. struct page *page;
  730. int migratetype, i;
  731. /* Find the largest possible block of pages in the other list */
  732. for (current_order = MAX_ORDER-1; current_order >= order;
  733. --current_order) {
  734. for (i = 0; i < MIGRATE_TYPES - 1; i++) {
  735. migratetype = fallbacks[start_migratetype][i];
  736. /* MIGRATE_RESERVE handled later if necessary */
  737. if (migratetype == MIGRATE_RESERVE)
  738. continue;
  739. area = &(zone->free_area[current_order]);
  740. if (list_empty(&area->free_list[migratetype]))
  741. continue;
  742. page = list_entry(area->free_list[migratetype].next,
  743. struct page, lru);
  744. area->nr_free--;
  745. /*
  746. * If breaking a large block of pages, move all free
  747. * pages to the preferred allocation list. If falling
  748. * back for a reclaimable kernel allocation, be more
  749. * agressive about taking ownership of free pages
  750. */
  751. if (unlikely(current_order >= (pageblock_order >> 1)) ||
  752. start_migratetype == MIGRATE_RECLAIMABLE ||
  753. page_group_by_mobility_disabled) {
  754. unsigned long pages;
  755. pages = move_freepages_block(zone, page,
  756. start_migratetype);
  757. /* Claim the whole block if over half of it is free */
  758. if (pages >= (1 << (pageblock_order-1)) ||
  759. page_group_by_mobility_disabled)
  760. set_pageblock_migratetype(page,
  761. start_migratetype);
  762. migratetype = start_migratetype;
  763. }
  764. /* Remove the page from the freelists */
  765. list_del(&page->lru);
  766. rmv_page_order(page);
  767. /* Take ownership for orders >= pageblock_order */
  768. if (current_order >= pageblock_order)
  769. change_pageblock_range(page, current_order,
  770. start_migratetype);
  771. expand(zone, page, order, current_order, area, migratetype);
  772. trace_mm_page_alloc_extfrag(page, order, current_order,
  773. start_migratetype, migratetype);
  774. return page;
  775. }
  776. }
  777. return NULL;
  778. }
  779. /*
  780. * Do the hard work of removing an element from the buddy allocator.
  781. * Call me with the zone->lock already held.
  782. */
  783. static struct page *__rmqueue(struct zone *zone, unsigned int order,
  784. int migratetype)
  785. {
  786. struct page *page;
  787. retry_reserve:
  788. page = __rmqueue_smallest(zone, order, migratetype);
  789. if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {
  790. page = __rmqueue_fallback(zone, order, migratetype);
  791. /*
  792. * Use MIGRATE_RESERVE rather than fail an allocation. goto
  793. * is used because __rmqueue_smallest is an inline function
  794. * and we want just one call site
  795. */
  796. if (!page) {
  797. migratetype = MIGRATE_RESERVE;
  798. goto retry_reserve;
  799. }
  800. }
  801. trace_mm_page_alloc_zone_locked(page, order, migratetype);
  802. return page;
  803. }
  804. /*
  805. * Obtain a specified number of elements from the buddy allocator, all under
  806. * a single hold of the lock, for efficiency. Add them to the supplied list.
  807. * Returns the number of new pages which were placed at *list.
  808. */
  809. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  810. unsigned long count, struct list_head *list,
  811. int migratetype, int cold)
  812. {
  813. int i;
  814. spin_lock(&zone->lock);
  815. for (i = 0; i < count; ++i) {
  816. struct page *page = __rmqueue(zone, order, migratetype);
  817. if (unlikely(page == NULL))
  818. break;
  819. /*
  820. * Split buddy pages returned by expand() are received here
  821. * in physical page order. The page is added to the callers and
  822. * list and the list head then moves forward. From the callers
  823. * perspective, the linked list is ordered by page number in
  824. * some conditions. This is useful for IO devices that can
  825. * merge IO requests if the physical pages are ordered
  826. * properly.
  827. */
  828. if (likely(cold == 0))
  829. list_add(&page->lru, list);
  830. else
  831. list_add_tail(&page->lru, list);
  832. set_page_private(page, migratetype);
  833. list = &page->lru;
  834. }
  835. __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
  836. spin_unlock(&zone->lock);
  837. return i;
  838. }
  839. #ifdef CONFIG_NUMA
  840. /*
  841. * Called from the vmstat counter updater to drain pagesets of this
  842. * currently executing processor on remote nodes after they have
  843. * expired.
  844. *
  845. * Note that this function must be called with the thread pinned to
  846. * a single processor.
  847. */
  848. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  849. {
  850. unsigned long flags;
  851. int to_drain;
  852. local_irq_save(flags);
  853. if (pcp->count >= pcp->batch)
  854. to_drain = pcp->batch;
  855. else
  856. to_drain = pcp->count;
  857. free_pcppages_bulk(zone, to_drain, pcp);
  858. pcp->count -= to_drain;
  859. local_irq_restore(flags);
  860. }
  861. #endif
  862. /*
  863. * Drain pages of the indicated processor.
  864. *
  865. * The processor must either be the current processor and the
  866. * thread pinned to the current processor or a processor that
  867. * is not online.
  868. */
  869. static void drain_pages(unsigned int cpu)
  870. {
  871. unsigned long flags;
  872. struct zone *zone;
  873. for_each_populated_zone(zone) {
  874. struct per_cpu_pageset *pset;
  875. struct per_cpu_pages *pcp;
  876. local_irq_save(flags);
  877. pset = per_cpu_ptr(zone->pageset, cpu);
  878. pcp = &pset->pcp;
  879. free_pcppages_bulk(zone, pcp->count, pcp);
  880. pcp->count = 0;
  881. local_irq_restore(flags);
  882. }
  883. }
  884. /*
  885. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  886. */
  887. void drain_local_pages(void *arg)
  888. {
  889. drain_pages(smp_processor_id());
  890. }
  891. /*
  892. * Spill all the per-cpu pages from all CPUs back into the buddy allocator
  893. */
  894. void drain_all_pages(void)
  895. {
  896. on_each_cpu(drain_local_pages, NULL, 1);
  897. }
  898. #ifdef CONFIG_HIBERNATION
  899. void mark_free_pages(struct zone *zone)
  900. {
  901. unsigned long pfn, max_zone_pfn;
  902. unsigned long flags;
  903. int order, t;
  904. struct list_head *curr;
  905. if (!zone->spanned_pages)
  906. return;
  907. spin_lock_irqsave(&zone->lock, flags);
  908. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  909. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  910. if (pfn_valid(pfn)) {
  911. struct page *page = pfn_to_page(pfn);
  912. if (!swsusp_page_is_forbidden(page))
  913. swsusp_unset_page_free(page);
  914. }
  915. for_each_migratetype_order(order, t) {
  916. list_for_each(curr, &zone->free_area[order].free_list[t]) {
  917. unsigned long i;
  918. pfn = page_to_pfn(list_entry(curr, struct page, lru));
  919. for (i = 0; i < (1UL << order); i++)
  920. swsusp_set_page_free(pfn_to_page(pfn + i));
  921. }
  922. }
  923. spin_unlock_irqrestore(&zone->lock, flags);
  924. }
  925. #endif /* CONFIG_PM */
  926. /*
  927. * Free a 0-order page
  928. */
  929. static void free_hot_cold_page(struct page *page, int cold)
  930. {
  931. struct zone *zone = page_zone(page);
  932. struct per_cpu_pages *pcp;
  933. unsigned long flags;
  934. int migratetype;
  935. int wasMlocked = __TestClearPageMlocked(page);
  936. kmemcheck_free_shadow(page, 0);
  937. if (PageAnon(page))
  938. page->mapping = NULL;
  939. if (free_pages_check(page))
  940. return;
  941. if (!PageHighMem(page)) {
  942. debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
  943. debug_check_no_obj_freed(page_address(page), PAGE_SIZE);
  944. }
  945. arch_free_page(page, 0);
  946. kernel_map_pages(page, 1, 0);
  947. migratetype = get_pageblock_migratetype(page);
  948. set_page_private(page, migratetype);
  949. local_irq_save(flags);
  950. if (unlikely(wasMlocked))
  951. free_page_mlock(page);
  952. __count_vm_event(PGFREE);
  953. /*
  954. * We only track unmovable, reclaimable and movable on pcp lists.
  955. * Free ISOLATE pages back to the allocator because they are being
  956. * offlined but treat RESERVE as movable pages so we can get those
  957. * areas back if necessary. Otherwise, we may have to free
  958. * excessively into the page allocator
  959. */
  960. if (migratetype >= MIGRATE_PCPTYPES) {
  961. if (unlikely(migratetype == MIGRATE_ISOLATE)) {
  962. free_one_page(zone, page, 0, migratetype);
  963. goto out;
  964. }
  965. migratetype = MIGRATE_MOVABLE;
  966. }
  967. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  968. if (cold)
  969. list_add_tail(&page->lru, &pcp->lists[migratetype]);
  970. else
  971. list_add(&page->lru, &pcp->lists[migratetype]);
  972. pcp->count++;
  973. if (pcp->count >= pcp->high) {
  974. free_pcppages_bulk(zone, pcp->batch, pcp);
  975. pcp->count -= pcp->batch;
  976. }
  977. out:
  978. local_irq_restore(flags);
  979. }
  980. void free_hot_page(struct page *page)
  981. {
  982. trace_mm_page_free_direct(page, 0);
  983. free_hot_cold_page(page, 0);
  984. }
  985. /*
  986. * split_page takes a non-compound higher-order page, and splits it into
  987. * n (1<<order) sub-pages: page[0..n]
  988. * Each sub-page must be freed individually.
  989. *
  990. * Note: this is probably too low level an operation for use in drivers.
  991. * Please consult with lkml before using this in your driver.
  992. */
  993. void split_page(struct page *page, unsigned int order)
  994. {
  995. int i;
  996. VM_BUG_ON(PageCompound(page));
  997. VM_BUG_ON(!page_count(page));
  998. #ifdef CONFIG_KMEMCHECK
  999. /*
  1000. * Split shadow pages too, because free(page[0]) would
  1001. * otherwise free the whole shadow.
  1002. */
  1003. if (kmemcheck_page_is_tracked(page))
  1004. split_page(virt_to_page(page[0].shadow), order);
  1005. #endif
  1006. for (i = 1; i < (1 << order); i++)
  1007. set_page_refcounted(page + i);
  1008. }
  1009. /*
  1010. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  1011. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  1012. * or two.
  1013. */
  1014. static inline
  1015. struct page *buffered_rmqueue(struct zone *preferred_zone,
  1016. struct zone *zone, int order, gfp_t gfp_flags,
  1017. int migratetype)
  1018. {
  1019. unsigned long flags;
  1020. struct page *page;
  1021. int cold = !!(gfp_flags & __GFP_COLD);
  1022. again:
  1023. if (likely(order == 0)) {
  1024. struct per_cpu_pages *pcp;
  1025. struct list_head *list;
  1026. local_irq_save(flags);
  1027. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  1028. list = &pcp->lists[migratetype];
  1029. if (list_empty(list)) {
  1030. pcp->count += rmqueue_bulk(zone, 0,
  1031. pcp->batch, list,
  1032. migratetype, cold);
  1033. if (unlikely(list_empty(list)))
  1034. goto failed;
  1035. }
  1036. if (cold)
  1037. page = list_entry(list->prev, struct page, lru);
  1038. else
  1039. page = list_entry(list->next, struct page, lru);
  1040. list_del(&page->lru);
  1041. pcp->count--;
  1042. } else {
  1043. if (unlikely(gfp_flags & __GFP_NOFAIL)) {
  1044. /*
  1045. * __GFP_NOFAIL is not to be used in new code.
  1046. *
  1047. * All __GFP_NOFAIL callers should be fixed so that they
  1048. * properly detect and handle allocation failures.
  1049. *
  1050. * We most definitely don't want callers attempting to
  1051. * allocate greater than order-1 page units with
  1052. * __GFP_NOFAIL.
  1053. */
  1054. WARN_ON_ONCE(order > 1);
  1055. }
  1056. spin_lock_irqsave(&zone->lock, flags);
  1057. page = __rmqueue(zone, order, migratetype);
  1058. spin_unlock(&zone->lock);
  1059. if (!page)
  1060. goto failed;
  1061. __mod_zone_page_state(zone, NR_FREE_PAGES, -(1 << order));
  1062. }
  1063. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  1064. zone_statistics(preferred_zone, zone);
  1065. local_irq_restore(flags);
  1066. VM_BUG_ON(bad_range(zone, page));
  1067. if (prep_new_page(page, order, gfp_flags))
  1068. goto again;
  1069. return page;
  1070. failed:
  1071. local_irq_restore(flags);
  1072. return NULL;
  1073. }
  1074. /* The ALLOC_WMARK bits are used as an index to zone->watermark */
  1075. #define ALLOC_WMARK_MIN WMARK_MIN
  1076. #define ALLOC_WMARK_LOW WMARK_LOW
  1077. #define ALLOC_WMARK_HIGH WMARK_HIGH
  1078. #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */
  1079. /* Mask to get the watermark bits */
  1080. #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1)
  1081. #define ALLOC_HARDER 0x10 /* try to alloc harder */
  1082. #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
  1083. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  1084. #ifdef CONFIG_FAIL_PAGE_ALLOC
  1085. static struct fail_page_alloc_attr {
  1086. struct fault_attr attr;
  1087. u32 ignore_gfp_highmem;
  1088. u32 ignore_gfp_wait;
  1089. u32 min_order;
  1090. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  1091. struct dentry *ignore_gfp_highmem_file;
  1092. struct dentry *ignore_gfp_wait_file;
  1093. struct dentry *min_order_file;
  1094. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  1095. } fail_page_alloc = {
  1096. .attr = FAULT_ATTR_INITIALIZER,
  1097. .ignore_gfp_wait = 1,
  1098. .ignore_gfp_highmem = 1,
  1099. .min_order = 1,
  1100. };
  1101. static int __init setup_fail_page_alloc(char *str)
  1102. {
  1103. return setup_fault_attr(&fail_page_alloc.attr, str);
  1104. }
  1105. __setup("fail_page_alloc=", setup_fail_page_alloc);
  1106. static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  1107. {
  1108. if (order < fail_page_alloc.min_order)
  1109. return 0;
  1110. if (gfp_mask & __GFP_NOFAIL)
  1111. return 0;
  1112. if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
  1113. return 0;
  1114. if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
  1115. return 0;
  1116. return should_fail(&fail_page_alloc.attr, 1 << order);
  1117. }
  1118. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  1119. static int __init fail_page_alloc_debugfs(void)
  1120. {
  1121. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  1122. struct dentry *dir;
  1123. int err;
  1124. err = init_fault_attr_dentries(&fail_page_alloc.attr,
  1125. "fail_page_alloc");
  1126. if (err)
  1127. return err;
  1128. dir = fail_page_alloc.attr.dentries.dir;
  1129. fail_page_alloc.ignore_gfp_wait_file =
  1130. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  1131. &fail_page_alloc.ignore_gfp_wait);
  1132. fail_page_alloc.ignore_gfp_highmem_file =
  1133. debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  1134. &fail_page_alloc.ignore_gfp_highmem);
  1135. fail_page_alloc.min_order_file =
  1136. debugfs_create_u32("min-order", mode, dir,
  1137. &fail_page_alloc.min_order);
  1138. if (!fail_page_alloc.ignore_gfp_wait_file ||
  1139. !fail_page_alloc.ignore_gfp_highmem_file ||
  1140. !fail_page_alloc.min_order_file) {
  1141. err = -ENOMEM;
  1142. debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
  1143. debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
  1144. debugfs_remove(fail_page_alloc.min_order_file);
  1145. cleanup_fault_attr_dentries(&fail_page_alloc.attr);
  1146. }
  1147. return err;
  1148. }
  1149. late_initcall(fail_page_alloc_debugfs);
  1150. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  1151. #else /* CONFIG_FAIL_PAGE_ALLOC */
  1152. static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  1153. {
  1154. return 0;
  1155. }
  1156. #endif /* CONFIG_FAIL_PAGE_ALLOC */
  1157. /*
  1158. * Return 1 if free pages are above 'mark'. This takes into account the order
  1159. * of the allocation.
  1160. */
  1161. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  1162. int classzone_idx, int alloc_flags)
  1163. {
  1164. /* free_pages my go negative - that's OK */
  1165. long min = mark;
  1166. long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
  1167. int o;
  1168. if (alloc_flags & ALLOC_HIGH)
  1169. min -= min / 2;
  1170. if (alloc_flags & ALLOC_HARDER)
  1171. min -= min / 4;
  1172. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  1173. return 0;
  1174. for (o = 0; o < order; o++) {
  1175. /* At the next order, this order's pages become unavailable */
  1176. free_pages -= z->free_area[o].nr_free << o;
  1177. /* Require fewer higher order pages to be free */
  1178. min >>= 1;
  1179. if (free_pages <= min)
  1180. return 0;
  1181. }
  1182. return 1;
  1183. }
  1184. #ifdef CONFIG_NUMA
  1185. /*
  1186. * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
  1187. * skip over zones that are not allowed by the cpuset, or that have
  1188. * been recently (in last second) found to be nearly full. See further
  1189. * comments in mmzone.h. Reduces cache footprint of zonelist scans
  1190. * that have to skip over a lot of full or unallowed zones.
  1191. *
  1192. * If the zonelist cache is present in the passed in zonelist, then
  1193. * returns a pointer to the allowed node mask (either the current
  1194. * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
  1195. *
  1196. * If the zonelist cache is not available for this zonelist, does
  1197. * nothing and returns NULL.
  1198. *
  1199. * If the fullzones BITMAP in the zonelist cache is stale (more than
  1200. * a second since last zap'd) then we zap it out (clear its bits.)
  1201. *
  1202. * We hold off even calling zlc_setup, until after we've checked the
  1203. * first zone in the zonelist, on the theory that most allocations will
  1204. * be satisfied from that first zone, so best to examine that zone as
  1205. * quickly as we can.
  1206. */
  1207. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  1208. {
  1209. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1210. nodemask_t *allowednodes; /* zonelist_cache approximation */
  1211. zlc = zonelist->zlcache_ptr;
  1212. if (!zlc)
  1213. return NULL;
  1214. if (time_after(jiffies, zlc->last_full_zap + HZ)) {
  1215. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  1216. zlc->last_full_zap = jiffies;
  1217. }
  1218. allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
  1219. &cpuset_current_mems_allowed :
  1220. &node_states[N_HIGH_MEMORY];
  1221. return allowednodes;
  1222. }
  1223. /*
  1224. * Given 'z' scanning a zonelist, run a couple of quick checks to see
  1225. * if it is worth looking at further for free memory:
  1226. * 1) Check that the zone isn't thought to be full (doesn't have its
  1227. * bit set in the zonelist_cache fullzones BITMAP).
  1228. * 2) Check that the zones node (obtained from the zonelist_cache
  1229. * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
  1230. * Return true (non-zero) if zone is worth looking at further, or
  1231. * else return false (zero) if it is not.
  1232. *
  1233. * This check -ignores- the distinction between various watermarks,
  1234. * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
  1235. * found to be full for any variation of these watermarks, it will
  1236. * be considered full for up to one second by all requests, unless
  1237. * we are so low on memory on all allowed nodes that we are forced
  1238. * into the second scan of the zonelist.
  1239. *
  1240. * In the second scan we ignore this zonelist cache and exactly
  1241. * apply the watermarks to all zones, even it is slower to do so.
  1242. * We are low on memory in the second scan, and should leave no stone
  1243. * unturned looking for a free page.
  1244. */
  1245. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
  1246. nodemask_t *allowednodes)
  1247. {
  1248. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1249. int i; /* index of *z in zonelist zones */
  1250. int n; /* node that zone *z is on */
  1251. zlc = zonelist->zlcache_ptr;
  1252. if (!zlc)
  1253. return 1;
  1254. i = z - zonelist->_zonerefs;
  1255. n = zlc->z_to_n[i];
  1256. /* This zone is worth trying if it is allowed but not full */
  1257. return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
  1258. }
  1259. /*
  1260. * Given 'z' scanning a zonelist, set the corresponding bit in
  1261. * zlc->fullzones, so that subsequent attempts to allocate a page
  1262. * from that zone don't waste time re-examining it.
  1263. */
  1264. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
  1265. {
  1266. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1267. int i; /* index of *z in zonelist zones */
  1268. zlc = zonelist->zlcache_ptr;
  1269. if (!zlc)
  1270. return;
  1271. i = z - zonelist->_zonerefs;
  1272. set_bit(i, zlc->fullzones);
  1273. }
  1274. #else /* CONFIG_NUMA */
  1275. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  1276. {
  1277. return NULL;
  1278. }
  1279. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
  1280. nodemask_t *allowednodes)
  1281. {
  1282. return 1;
  1283. }
  1284. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
  1285. {
  1286. }
  1287. #endif /* CONFIG_NUMA */
  1288. /*
  1289. * get_page_from_freelist goes through the zonelist trying to allocate
  1290. * a page.
  1291. */
  1292. static struct page *
  1293. get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
  1294. struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
  1295. struct zone *preferred_zone, int migratetype)
  1296. {
  1297. struct zoneref *z;
  1298. struct page *page = NULL;
  1299. int classzone_idx;
  1300. struct zone *zone;
  1301. nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
  1302. int zlc_active = 0; /* set if using zonelist_cache */
  1303. int did_zlc_setup = 0; /* just call zlc_setup() one time */
  1304. classzone_idx = zone_idx(preferred_zone);
  1305. zonelist_scan:
  1306. /*
  1307. * Scan zonelist, looking for a zone with enough free.
  1308. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1309. */
  1310. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1311. high_zoneidx, nodemask) {
  1312. if (NUMA_BUILD && zlc_active &&
  1313. !zlc_zone_worth_trying(zonelist, z, allowednodes))
  1314. continue;
  1315. if ((alloc_flags & ALLOC_CPUSET) &&
  1316. !cpuset_zone_allowed_softwall(zone, gfp_mask))
  1317. goto try_next_zone;
  1318. BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
  1319. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  1320. unsigned long mark;
  1321. int ret;
  1322. mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
  1323. if (zone_watermark_ok(zone, order, mark,
  1324. classzone_idx, alloc_flags))
  1325. goto try_this_zone;
  1326. if (zone_reclaim_mode == 0)
  1327. goto this_zone_full;
  1328. ret = zone_reclaim(zone, gfp_mask, order);
  1329. switch (ret) {
  1330. case ZONE_RECLAIM_NOSCAN:
  1331. /* did not scan */
  1332. goto try_next_zone;
  1333. case ZONE_RECLAIM_FULL:
  1334. /* scanned but unreclaimable */
  1335. goto this_zone_full;
  1336. default:
  1337. /* did we reclaim enough */
  1338. if (!zone_watermark_ok(zone, order, mark,
  1339. classzone_idx, alloc_flags))
  1340. goto this_zone_full;
  1341. }
  1342. }
  1343. try_this_zone:
  1344. page = buffered_rmqueue(preferred_zone, zone, order,
  1345. gfp_mask, migratetype);
  1346. if (page)
  1347. break;
  1348. this_zone_full:
  1349. if (NUMA_BUILD)
  1350. zlc_mark_zone_full(zonelist, z);
  1351. try_next_zone:
  1352. if (NUMA_BUILD && !did_zlc_setup && nr_online_nodes > 1) {
  1353. /*
  1354. * we do zlc_setup after the first zone is tried but only
  1355. * if there are multiple nodes make it worthwhile
  1356. */
  1357. allowednodes = zlc_setup(zonelist, alloc_flags);
  1358. zlc_active = 1;
  1359. did_zlc_setup = 1;
  1360. }
  1361. }
  1362. if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
  1363. /* Disable zlc cache for second zonelist scan */
  1364. zlc_active = 0;
  1365. goto zonelist_scan;
  1366. }
  1367. return page;
  1368. }
  1369. static inline int
  1370. should_alloc_retry(gfp_t gfp_mask, unsigned int order,
  1371. unsigned long pages_reclaimed)
  1372. {
  1373. /* Do not loop if specifically requested */
  1374. if (gfp_mask & __GFP_NORETRY)
  1375. return 0;
  1376. /*
  1377. * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
  1378. * means __GFP_NOFAIL, but that may not be true in other
  1379. * implementations.
  1380. */
  1381. if (order <= PAGE_ALLOC_COSTLY_ORDER)
  1382. return 1;
  1383. /*
  1384. * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
  1385. * specified, then we retry until we no longer reclaim any pages
  1386. * (above), or we've reclaimed an order of pages at least as
  1387. * large as the allocation's order. In both cases, if the
  1388. * allocation still fails, we stop retrying.
  1389. */
  1390. if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
  1391. return 1;
  1392. /*
  1393. * Don't let big-order allocations loop unless the caller
  1394. * explicitly requests that.
  1395. */
  1396. if (gfp_mask & __GFP_NOFAIL)
  1397. return 1;
  1398. return 0;
  1399. }
  1400. static inline struct page *
  1401. __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
  1402. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1403. nodemask_t *nodemask, struct zone *preferred_zone,
  1404. int migratetype)
  1405. {
  1406. struct page *page;
  1407. /* Acquire the OOM killer lock for the zones in zonelist */
  1408. if (!try_set_zone_oom(zonelist, gfp_mask)) {
  1409. schedule_timeout_uninterruptible(1);
  1410. return NULL;
  1411. }
  1412. /*
  1413. * Go through the zonelist yet one more time, keep very high watermark
  1414. * here, this is only to catch a parallel oom killing, we must fail if
  1415. * we're still under heavy pressure.
  1416. */
  1417. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
  1418. order, zonelist, high_zoneidx,
  1419. ALLOC_WMARK_HIGH|ALLOC_CPUSET,
  1420. preferred_zone, migratetype);
  1421. if (page)
  1422. goto out;
  1423. if (!(gfp_mask & __GFP_NOFAIL)) {
  1424. /* The OOM killer will not help higher order allocs */
  1425. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1426. goto out;
  1427. /*
  1428. * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
  1429. * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
  1430. * The caller should handle page allocation failure by itself if
  1431. * it specifies __GFP_THISNODE.
  1432. * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
  1433. */
  1434. if (gfp_mask & __GFP_THISNODE)
  1435. goto out;
  1436. }
  1437. /* Exhausted what can be done so it's blamo time */
  1438. out_of_memory(zonelist, gfp_mask, order, nodemask);
  1439. out:
  1440. clear_zonelist_oom(zonelist, gfp_mask);
  1441. return page;
  1442. }
  1443. /* The really slow allocator path where we enter direct reclaim */
  1444. static inline struct page *
  1445. __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
  1446. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1447. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1448. int migratetype, unsigned long *did_some_progress)
  1449. {
  1450. struct page *page = NULL;
  1451. struct reclaim_state reclaim_state;
  1452. struct task_struct *p = current;
  1453. cond_resched();
  1454. /* We now go into synchronous reclaim */
  1455. cpuset_memory_pressure_bump();
  1456. p->flags |= PF_MEMALLOC;
  1457. lockdep_set_current_reclaim_state(gfp_mask);
  1458. reclaim_state.reclaimed_slab = 0;
  1459. p->reclaim_state = &reclaim_state;
  1460. *did_some_progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
  1461. p->reclaim_state = NULL;
  1462. lockdep_clear_current_reclaim_state();
  1463. p->flags &= ~PF_MEMALLOC;
  1464. cond_resched();
  1465. if (order != 0)
  1466. drain_all_pages();
  1467. if (likely(*did_some_progress))
  1468. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1469. zonelist, high_zoneidx,
  1470. alloc_flags, preferred_zone,
  1471. migratetype);
  1472. return page;
  1473. }
  1474. /*
  1475. * This is called in the allocator slow-path if the allocation request is of
  1476. * sufficient urgency to ignore watermarks and take other desperate measures
  1477. */
  1478. static inline struct page *
  1479. __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
  1480. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1481. nodemask_t *nodemask, struct zone *preferred_zone,
  1482. int migratetype)
  1483. {
  1484. struct page *page;
  1485. do {
  1486. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1487. zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
  1488. preferred_zone, migratetype);
  1489. if (!page && gfp_mask & __GFP_NOFAIL)
  1490. congestion_wait(BLK_RW_ASYNC, HZ/50);
  1491. } while (!page && (gfp_mask & __GFP_NOFAIL));
  1492. return page;
  1493. }
  1494. static inline
  1495. void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
  1496. enum zone_type high_zoneidx)
  1497. {
  1498. struct zoneref *z;
  1499. struct zone *zone;
  1500. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
  1501. wakeup_kswapd(zone, order);
  1502. }
  1503. static inline int
  1504. gfp_to_alloc_flags(gfp_t gfp_mask)
  1505. {
  1506. struct task_struct *p = current;
  1507. int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
  1508. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1509. /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
  1510. BUILD_BUG_ON(__GFP_HIGH != ALLOC_HIGH);
  1511. /*
  1512. * The caller may dip into page reserves a bit more if the caller
  1513. * cannot run direct reclaim, or if the caller has realtime scheduling
  1514. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  1515. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  1516. */
  1517. alloc_flags |= (gfp_mask & __GFP_HIGH);
  1518. if (!wait) {
  1519. alloc_flags |= ALLOC_HARDER;
  1520. /*
  1521. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  1522. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1523. */
  1524. alloc_flags &= ~ALLOC_CPUSET;
  1525. } else if (unlikely(rt_task(p)) && !in_interrupt())
  1526. alloc_flags |= ALLOC_HARDER;
  1527. if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
  1528. if (!in_interrupt() &&
  1529. ((p->flags & PF_MEMALLOC) ||
  1530. unlikely(test_thread_flag(TIF_MEMDIE))))
  1531. alloc_flags |= ALLOC_NO_WATERMARKS;
  1532. }
  1533. return alloc_flags;
  1534. }
  1535. static inline struct page *
  1536. __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
  1537. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1538. nodemask_t *nodemask, struct zone *preferred_zone,
  1539. int migratetype)
  1540. {
  1541. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1542. struct page *page = NULL;
  1543. int alloc_flags;
  1544. unsigned long pages_reclaimed = 0;
  1545. unsigned long did_some_progress;
  1546. struct task_struct *p = current;
  1547. /*
  1548. * In the slowpath, we sanity check order to avoid ever trying to
  1549. * reclaim >= MAX_ORDER areas which will never succeed. Callers may
  1550. * be using allocators in order of preference for an area that is
  1551. * too large.
  1552. */
  1553. if (order >= MAX_ORDER) {
  1554. WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
  1555. return NULL;
  1556. }
  1557. /*
  1558. * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
  1559. * __GFP_NOWARN set) should not cause reclaim since the subsystem
  1560. * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
  1561. * using a larger set of nodes after it has established that the
  1562. * allowed per node queues are empty and that nodes are
  1563. * over allocated.
  1564. */
  1565. if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
  1566. goto nopage;
  1567. restart:
  1568. wake_all_kswapd(order, zonelist, high_zoneidx);
  1569. /*
  1570. * OK, we're below the kswapd watermark and have kicked background
  1571. * reclaim. Now things get more complex, so set up alloc_flags according
  1572. * to how we want to proceed.
  1573. */
  1574. alloc_flags = gfp_to_alloc_flags(gfp_mask);
  1575. /* This is the last chance, in general, before the goto nopage. */
  1576. page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
  1577. high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
  1578. preferred_zone, migratetype);
  1579. if (page)
  1580. goto got_pg;
  1581. rebalance:
  1582. /* Allocate without watermarks if the context allows */
  1583. if (alloc_flags & ALLOC_NO_WATERMARKS) {
  1584. page = __alloc_pages_high_priority(gfp_mask, order,
  1585. zonelist, high_zoneidx, nodemask,
  1586. preferred_zone, migratetype);
  1587. if (page)
  1588. goto got_pg;
  1589. }
  1590. /* Atomic allocations - we can't balance anything */
  1591. if (!wait)
  1592. goto nopage;
  1593. /* Avoid recursion of direct reclaim */
  1594. if (p->flags & PF_MEMALLOC)
  1595. goto nopage;
  1596. /* Avoid allocations with no watermarks from looping endlessly */
  1597. if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
  1598. goto nopage;
  1599. /* Try direct reclaim and then allocating */
  1600. page = __alloc_pages_direct_reclaim(gfp_mask, order,
  1601. zonelist, high_zoneidx,
  1602. nodemask,
  1603. alloc_flags, preferred_zone,
  1604. migratetype, &did_some_progress);
  1605. if (page)
  1606. goto got_pg;
  1607. /*
  1608. * If we failed to make any progress reclaiming, then we are
  1609. * running out of options and have to consider going OOM
  1610. */
  1611. if (!did_some_progress) {
  1612. if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  1613. if (oom_killer_disabled)
  1614. goto nopage;
  1615. page = __alloc_pages_may_oom(gfp_mask, order,
  1616. zonelist, high_zoneidx,
  1617. nodemask, preferred_zone,
  1618. migratetype);
  1619. if (page)
  1620. goto got_pg;
  1621. /*
  1622. * The OOM killer does not trigger for high-order
  1623. * ~__GFP_NOFAIL allocations so if no progress is being
  1624. * made, there are no other options and retrying is
  1625. * unlikely to help.
  1626. */
  1627. if (order > PAGE_ALLOC_COSTLY_ORDER &&
  1628. !(gfp_mask & __GFP_NOFAIL))
  1629. goto nopage;
  1630. goto restart;
  1631. }
  1632. }
  1633. /* Check if we should retry the allocation */
  1634. pages_reclaimed += did_some_progress;
  1635. if (should_alloc_retry(gfp_mask, order, pages_reclaimed)) {
  1636. /* Wait for some write requests to complete then retry */
  1637. congestion_wait(BLK_RW_ASYNC, HZ/50);
  1638. goto rebalance;
  1639. }
  1640. nopage:
  1641. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  1642. printk(KERN_WARNING "%s: page allocation failure."
  1643. " order:%d, mode:0x%x\n",
  1644. p->comm, order, gfp_mask);
  1645. dump_stack();
  1646. show_mem();
  1647. }
  1648. return page;
  1649. got_pg:
  1650. if (kmemcheck_enabled)
  1651. kmemcheck_pagealloc_alloc(page, order, gfp_mask);
  1652. return page;
  1653. }
  1654. /*
  1655. * This is the 'heart' of the zoned buddy allocator.
  1656. */
  1657. struct page *
  1658. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
  1659. struct zonelist *zonelist, nodemask_t *nodemask)
  1660. {
  1661. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  1662. struct zone *preferred_zone;
  1663. struct page *page;
  1664. int migratetype = allocflags_to_migratetype(gfp_mask);
  1665. gfp_mask &= gfp_allowed_mask;
  1666. lockdep_trace_alloc(gfp_mask);
  1667. might_sleep_if(gfp_mask & __GFP_WAIT);
  1668. if (should_fail_alloc_page(gfp_mask, order))
  1669. return NULL;
  1670. /*
  1671. * Check the zones suitable for the gfp_mask contain at least one
  1672. * valid zone. It's possible to have an empty zonelist as a result
  1673. * of GFP_THISNODE and a memoryless node
  1674. */
  1675. if (unlikely(!zonelist->_zonerefs->zone))
  1676. return NULL;
  1677. /* The preferred zone is used for statistics later */
  1678. first_zones_zonelist(zonelist, high_zoneidx, nodemask, &preferred_zone);
  1679. if (!preferred_zone)
  1680. return NULL;
  1681. /* First allocation attempt */
  1682. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
  1683. zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET,
  1684. preferred_zone, migratetype);
  1685. if (unlikely(!page))
  1686. page = __alloc_pages_slowpath(gfp_mask, order,
  1687. zonelist, high_zoneidx, nodemask,
  1688. preferred_zone, migratetype);
  1689. trace_mm_page_alloc(page, order, gfp_mask, migratetype);
  1690. return page;
  1691. }
  1692. EXPORT_SYMBOL(__alloc_pages_nodemask);
  1693. /*
  1694. * Common helper functions.
  1695. */
  1696. unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  1697. {
  1698. struct page *page;
  1699. /*
  1700. * __get_free_pages() returns a 32-bit address, which cannot represent
  1701. * a highmem page
  1702. */
  1703. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1704. page = alloc_pages(gfp_mask, order);
  1705. if (!page)
  1706. return 0;
  1707. return (unsigned long) page_address(page);
  1708. }
  1709. EXPORT_SYMBOL(__get_free_pages);
  1710. unsigned long get_zeroed_page(gfp_t gfp_mask)
  1711. {
  1712. return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
  1713. }
  1714. EXPORT_SYMBOL(get_zeroed_page);
  1715. void __pagevec_free(struct pagevec *pvec)
  1716. {
  1717. int i = pagevec_count(pvec);
  1718. while (--i >= 0) {
  1719. trace_mm_pagevec_free(pvec->pages[i], pvec->cold);
  1720. free_hot_cold_page(pvec->pages[i], pvec->cold);
  1721. }
  1722. }
  1723. void __free_pages(struct page *page, unsigned int order)
  1724. {
  1725. if (put_page_testzero(page)) {
  1726. trace_mm_page_free_direct(page, order);
  1727. if (order == 0)
  1728. free_hot_page(page);
  1729. else
  1730. __free_pages_ok(page, order);
  1731. }
  1732. }
  1733. EXPORT_SYMBOL(__free_pages);
  1734. void free_pages(unsigned long addr, unsigned int order)
  1735. {
  1736. if (addr != 0) {
  1737. VM_BUG_ON(!virt_addr_valid((void *)addr));
  1738. __free_pages(virt_to_page((void *)addr), order);
  1739. }
  1740. }
  1741. EXPORT_SYMBOL(free_pages);
  1742. /**
  1743. * alloc_pages_exact - allocate an exact number physically-contiguous pages.
  1744. * @size: the number of bytes to allocate
  1745. * @gfp_mask: GFP flags for the allocation
  1746. *
  1747. * This function is similar to alloc_pages(), except that it allocates the
  1748. * minimum number of pages to satisfy the request. alloc_pages() can only
  1749. * allocate memory in power-of-two pages.
  1750. *
  1751. * This function is also limited by MAX_ORDER.
  1752. *
  1753. * Memory allocated by this function must be released by free_pages_exact().
  1754. */
  1755. void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
  1756. {
  1757. unsigned int order = get_order(size);
  1758. unsigned long addr;
  1759. addr = __get_free_pages(gfp_mask, order);
  1760. if (addr) {
  1761. unsigned long alloc_end = addr + (PAGE_SIZE << order);
  1762. unsigned long used = addr + PAGE_ALIGN(size);
  1763. split_page(virt_to_page((void *)addr), order);
  1764. while (used < alloc_end) {
  1765. free_page(used);
  1766. used += PAGE_SIZE;
  1767. }
  1768. }
  1769. return (void *)addr;
  1770. }
  1771. EXPORT_SYMBOL(alloc_pages_exact);
  1772. /**
  1773. * free_pages_exact - release memory allocated via alloc_pages_exact()
  1774. * @virt: the value returned by alloc_pages_exact.
  1775. * @size: size of allocation, same value as passed to alloc_pages_exact().
  1776. *
  1777. * Release the memory allocated by a previous call to alloc_pages_exact.
  1778. */
  1779. void free_pages_exact(void *virt, size_t size)
  1780. {
  1781. unsigned long addr = (unsigned long)virt;
  1782. unsigned long end = addr + PAGE_ALIGN(size);
  1783. while (addr < end) {
  1784. free_page(addr);
  1785. addr += PAGE_SIZE;
  1786. }
  1787. }
  1788. EXPORT_SYMBOL(free_pages_exact);
  1789. static unsigned int nr_free_zone_pages(int offset)
  1790. {
  1791. struct zoneref *z;
  1792. struct zone *zone;
  1793. /* Just pick one node, since fallback list is circular */
  1794. unsigned int sum = 0;
  1795. struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
  1796. for_each_zone_zonelist(zone, z, zonelist, offset) {
  1797. unsigned long size = zone->present_pages;
  1798. unsigned long high = high_wmark_pages(zone);
  1799. if (size > high)
  1800. sum += size - high;
  1801. }
  1802. return sum;
  1803. }
  1804. /*
  1805. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  1806. */
  1807. unsigned int nr_free_buffer_pages(void)
  1808. {
  1809. return nr_free_zone_pages(gfp_zone(GFP_USER));
  1810. }
  1811. EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
  1812. /*
  1813. * Amount of free RAM allocatable within all zones
  1814. */
  1815. unsigned int nr_free_pagecache_pages(void)
  1816. {
  1817. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  1818. }
  1819. static inline void show_node(struct zone *zone)
  1820. {
  1821. if (NUMA_BUILD)
  1822. printk("Node %d ", zone_to_nid(zone));
  1823. }
  1824. void si_meminfo(struct sysinfo *val)
  1825. {
  1826. val->totalram = totalram_pages;
  1827. val->sharedram = 0;
  1828. val->freeram = global_page_state(NR_FREE_PAGES);
  1829. val->bufferram = nr_blockdev_pages();
  1830. val->totalhigh = totalhigh_pages;
  1831. val->freehigh = nr_free_highpages();
  1832. val->mem_unit = PAGE_SIZE;
  1833. }
  1834. EXPORT_SYMBOL(si_meminfo);
  1835. #ifdef CONFIG_NUMA
  1836. void si_meminfo_node(struct sysinfo *val, int nid)
  1837. {
  1838. pg_data_t *pgdat = NODE_DATA(nid);
  1839. val->totalram = pgdat->node_present_pages;
  1840. val->freeram = node_page_state(nid, NR_FREE_PAGES);
  1841. #ifdef CONFIG_HIGHMEM
  1842. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  1843. val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  1844. NR_FREE_PAGES);
  1845. #else
  1846. val->totalhigh = 0;
  1847. val->freehigh = 0;
  1848. #endif
  1849. val->mem_unit = PAGE_SIZE;
  1850. }
  1851. #endif
  1852. #define K(x) ((x) << (PAGE_SHIFT-10))
  1853. /*
  1854. * Show free area list (used inside shift_scroll-lock stuff)
  1855. * We also calculate the percentage fragmentation. We do this by counting the
  1856. * memory on each free list with the exception of the first item on the list.
  1857. */
  1858. void show_free_areas(void)
  1859. {
  1860. int cpu;
  1861. struct zone *zone;
  1862. for_each_populated_zone(zone) {
  1863. show_node(zone);
  1864. printk("%s per-cpu:\n", zone->name);
  1865. for_each_online_cpu(cpu) {
  1866. struct per_cpu_pageset *pageset;
  1867. pageset = per_cpu_ptr(zone->pageset, cpu);
  1868. printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
  1869. cpu, pageset->pcp.high,
  1870. pageset->pcp.batch, pageset->pcp.count);
  1871. }
  1872. }
  1873. printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
  1874. " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
  1875. " unevictable:%lu"
  1876. " dirty:%lu writeback:%lu unstable:%lu\n"
  1877. " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
  1878. " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n",
  1879. global_page_state(NR_ACTIVE_ANON),
  1880. global_page_state(NR_INACTIVE_ANON),
  1881. global_page_state(NR_ISOLATED_ANON),
  1882. global_page_state(NR_ACTIVE_FILE),
  1883. global_page_state(NR_INACTIVE_FILE),
  1884. global_page_state(NR_ISOLATED_FILE),
  1885. global_page_state(NR_UNEVICTABLE),
  1886. global_page_state(NR_FILE_DIRTY),
  1887. global_page_state(NR_WRITEBACK),
  1888. global_page_state(NR_UNSTABLE_NFS),
  1889. global_page_state(NR_FREE_PAGES),
  1890. global_page_state(NR_SLAB_RECLAIMABLE),
  1891. global_page_state(NR_SLAB_UNRECLAIMABLE),
  1892. global_page_state(NR_FILE_MAPPED),
  1893. global_page_state(NR_SHMEM),
  1894. global_page_state(NR_PAGETABLE),
  1895. global_page_state(NR_BOUNCE));
  1896. for_each_populated_zone(zone) {
  1897. int i;
  1898. show_node(zone);
  1899. printk("%s"
  1900. " free:%lukB"
  1901. " min:%lukB"
  1902. " low:%lukB"
  1903. " high:%lukB"
  1904. " active_anon:%lukB"
  1905. " inactive_anon:%lukB"
  1906. " active_file:%lukB"
  1907. " inactive_file:%lukB"
  1908. " unevictable:%lukB"
  1909. " isolated(anon):%lukB"
  1910. " isolated(file):%lukB"
  1911. " present:%lukB"
  1912. " mlocked:%lukB"
  1913. " dirty:%lukB"
  1914. " writeback:%lukB"
  1915. " mapped:%lukB"
  1916. " shmem:%lukB"
  1917. " slab_reclaimable:%lukB"
  1918. " slab_unreclaimable:%lukB"
  1919. " kernel_stack:%lukB"
  1920. " pagetables:%lukB"
  1921. " unstable:%lukB"
  1922. " bounce:%lukB"
  1923. " writeback_tmp:%lukB"
  1924. " pages_scanned:%lu"
  1925. " all_unreclaimable? %s"
  1926. "\n",
  1927. zone->name,
  1928. K(zone_page_state(zone, NR_FREE_PAGES)),
  1929. K(min_wmark_pages(zone)),
  1930. K(low_wmark_pages(zone)),
  1931. K(high_wmark_pages(zone)),
  1932. K(zone_page_state(zone, NR_ACTIVE_ANON)),
  1933. K(zone_page_state(zone, NR_INACTIVE_ANON)),
  1934. K(zone_page_state(zone, NR_ACTIVE_FILE)),
  1935. K(zone_page_state(zone, NR_INACTIVE_FILE)),
  1936. K(zone_page_state(zone, NR_UNEVICTABLE)),
  1937. K(zone_page_state(zone, NR_ISOLATED_ANON)),
  1938. K(zone_page_state(zone, NR_ISOLATED_FILE)),
  1939. K(zone->present_pages),
  1940. K(zone_page_state(zone, NR_MLOCK)),
  1941. K(zone_page_state(zone, NR_FILE_DIRTY)),
  1942. K(zone_page_state(zone, NR_WRITEBACK)),
  1943. K(zone_page_state(zone, NR_FILE_MAPPED)),
  1944. K(zone_page_state(zone, NR_SHMEM)),
  1945. K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
  1946. K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
  1947. zone_page_state(zone, NR_KERNEL_STACK) *
  1948. THREAD_SIZE / 1024,
  1949. K(zone_page_state(zone, NR_PAGETABLE)),
  1950. K(zone_page_state(zone, NR_UNSTABLE_NFS)),
  1951. K(zone_page_state(zone, NR_BOUNCE)),
  1952. K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
  1953. zone->pages_scanned,
  1954. (zone_is_all_unreclaimable(zone) ? "yes" : "no")
  1955. );
  1956. printk("lowmem_reserve[]:");
  1957. for (i = 0; i < MAX_NR_ZONES; i++)
  1958. printk(" %lu", zone->lowmem_reserve[i]);
  1959. printk("\n");
  1960. }
  1961. for_each_populated_zone(zone) {
  1962. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  1963. show_node(zone);
  1964. printk("%s: ", zone->name);
  1965. spin_lock_irqsave(&zone->lock, flags);
  1966. for (order = 0; order < MAX_ORDER; order++) {
  1967. nr[order] = zone->free_area[order].nr_free;
  1968. total += nr[order] << order;
  1969. }
  1970. spin_unlock_irqrestore(&zone->lock, flags);
  1971. for (order = 0; order < MAX_ORDER; order++)
  1972. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  1973. printk("= %lukB\n", K(total));
  1974. }
  1975. printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
  1976. show_swap_cache_info();
  1977. }
  1978. static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
  1979. {
  1980. zoneref->zone = zone;
  1981. zoneref->zone_idx = zone_idx(zone);
  1982. }
  1983. /*
  1984. * Builds allocation fallback zone lists.
  1985. *
  1986. * Add all populated zones of a node to the zonelist.
  1987. */
  1988. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  1989. int nr_zones, enum zone_type zone_type)
  1990. {
  1991. struct zone *zone;
  1992. BUG_ON(zone_type >= MAX_NR_ZONES);
  1993. zone_type++;
  1994. do {
  1995. zone_type--;
  1996. zone = pgdat->node_zones + zone_type;
  1997. if (populated_zone(zone)) {
  1998. zoneref_set_zone(zone,
  1999. &zonelist->_zonerefs[nr_zones++]);
  2000. check_highest_zone(zone_type);
  2001. }
  2002. } while (zone_type);
  2003. return nr_zones;
  2004. }
  2005. /*
  2006. * zonelist_order:
  2007. * 0 = automatic detection of better ordering.
  2008. * 1 = order by ([node] distance, -zonetype)
  2009. * 2 = order by (-zonetype, [node] distance)
  2010. *
  2011. * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
  2012. * the same zonelist. So only NUMA can configure this param.
  2013. */
  2014. #define ZONELIST_ORDER_DEFAULT 0
  2015. #define ZONELIST_ORDER_NODE 1
  2016. #define ZONELIST_ORDER_ZONE 2
  2017. /* zonelist order in the kernel.
  2018. * set_zonelist_order() will set this to NODE or ZONE.
  2019. */
  2020. static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2021. static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
  2022. #ifdef CONFIG_NUMA
  2023. /* The value user specified ....changed by config */
  2024. static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2025. /* string for sysctl */
  2026. #define NUMA_ZONELIST_ORDER_LEN 16
  2027. char numa_zonelist_order[16] = "default";
  2028. /*
  2029. * interface for configure zonelist ordering.
  2030. * command line option "numa_zonelist_order"
  2031. * = "[dD]efault - default, automatic configuration.
  2032. * = "[nN]ode - order by node locality, then by zone within node
  2033. * = "[zZ]one - order by zone, then by locality within zone
  2034. */
  2035. static int __parse_numa_zonelist_order(char *s)
  2036. {
  2037. if (*s == 'd' || *s == 'D') {
  2038. user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2039. } else if (*s == 'n' || *s == 'N') {
  2040. user_zonelist_order = ZONELIST_ORDER_NODE;
  2041. } else if (*s == 'z' || *s == 'Z') {
  2042. user_zonelist_order = ZONELIST_ORDER_ZONE;
  2043. } else {
  2044. printk(KERN_WARNING
  2045. "Ignoring invalid numa_zonelist_order value: "
  2046. "%s\n", s);
  2047. return -EINVAL;
  2048. }
  2049. return 0;
  2050. }
  2051. static __init int setup_numa_zonelist_order(char *s)
  2052. {
  2053. if (s)
  2054. return __parse_numa_zonelist_order(s);
  2055. return 0;
  2056. }
  2057. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  2058. /*
  2059. * sysctl handler for numa_zonelist_order
  2060. */
  2061. int numa_zonelist_order_handler(ctl_table *table, int write,
  2062. void __user *buffer, size_t *length,
  2063. loff_t *ppos)
  2064. {
  2065. char saved_string[NUMA_ZONELIST_ORDER_LEN];
  2066. int ret;
  2067. static DEFINE_MUTEX(zl_order_mutex);
  2068. mutex_lock(&zl_order_mutex);
  2069. if (write)
  2070. strcpy(saved_string, (char*)table->data);
  2071. ret = proc_dostring(table, write, buffer, length, ppos);
  2072. if (ret)
  2073. goto out;
  2074. if (write) {
  2075. int oldval = user_zonelist_order;
  2076. if (__parse_numa_zonelist_order((char*)table->data)) {
  2077. /*
  2078. * bogus value. restore saved string
  2079. */
  2080. strncpy((char*)table->data, saved_string,
  2081. NUMA_ZONELIST_ORDER_LEN);
  2082. user_zonelist_order = oldval;
  2083. } else if (oldval != user_zonelist_order)
  2084. build_all_zonelists();
  2085. }
  2086. out:
  2087. mutex_unlock(&zl_order_mutex);
  2088. return ret;
  2089. }
  2090. #define MAX_NODE_LOAD (nr_online_nodes)
  2091. static int node_load[MAX_NUMNODES];
  2092. /**
  2093. * find_next_best_node - find the next node that should appear in a given node's fallback list
  2094. * @node: node whose fallback list we're appending
  2095. * @used_node_mask: nodemask_t of already used nodes
  2096. *
  2097. * We use a number of factors to determine which is the next node that should
  2098. * appear on a given node's fallback list. The node should not have appeared
  2099. * already in @node's fallback list, and it should be the next closest node
  2100. * according to the distance array (which contains arbitrary distance values
  2101. * from each node to each node in the system), and should also prefer nodes
  2102. * with no CPUs, since presumably they'll have very little allocation pressure
  2103. * on them otherwise.
  2104. * It returns -1 if no node is found.
  2105. */
  2106. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  2107. {
  2108. int n, val;
  2109. int min_val = INT_MAX;
  2110. int best_node = -1;
  2111. const struct cpumask *tmp = cpumask_of_node(0);
  2112. /* Use the local node if we haven't already */
  2113. if (!node_isset(node, *used_node_mask)) {
  2114. node_set(node, *used_node_mask);
  2115. return node;
  2116. }
  2117. for_each_node_state(n, N_HIGH_MEMORY) {
  2118. /* Don't want a node to appear more than once */
  2119. if (node_isset(n, *used_node_mask))
  2120. continue;
  2121. /* Use the distance array to find the distance */
  2122. val = node_distance(node, n);
  2123. /* Penalize nodes under us ("prefer the next node") */
  2124. val += (n < node);
  2125. /* Give preference to headless and unused nodes */
  2126. tmp = cpumask_of_node(n);
  2127. if (!cpumask_empty(tmp))
  2128. val += PENALTY_FOR_NODE_WITH_CPUS;
  2129. /* Slight preference for less loaded node */
  2130. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  2131. val += node_load[n];
  2132. if (val < min_val) {
  2133. min_val = val;
  2134. best_node = n;
  2135. }
  2136. }
  2137. if (best_node >= 0)
  2138. node_set(best_node, *used_node_mask);
  2139. return best_node;
  2140. }
  2141. /*
  2142. * Build zonelists ordered by node and zones within node.
  2143. * This results in maximum locality--normal zone overflows into local
  2144. * DMA zone, if any--but risks exhausting DMA zone.
  2145. */
  2146. static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
  2147. {
  2148. int j;
  2149. struct zonelist *zonelist;
  2150. zonelist = &pgdat->node_zonelists[0];
  2151. for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
  2152. ;
  2153. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2154. MAX_NR_ZONES - 1);
  2155. zonelist->_zonerefs[j].zone = NULL;
  2156. zonelist->_zonerefs[j].zone_idx = 0;
  2157. }
  2158. /*
  2159. * Build gfp_thisnode zonelists
  2160. */
  2161. static void build_thisnode_zonelists(pg_data_t *pgdat)
  2162. {
  2163. int j;
  2164. struct zonelist *zonelist;
  2165. zonelist = &pgdat->node_zonelists[1];
  2166. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2167. zonelist->_zonerefs[j].zone = NULL;
  2168. zonelist->_zonerefs[j].zone_idx = 0;
  2169. }
  2170. /*
  2171. * Build zonelists ordered by zone and nodes within zones.
  2172. * This results in conserving DMA zone[s] until all Normal memory is
  2173. * exhausted, but results in overflowing to remote node while memory
  2174. * may still exist in local DMA zone.
  2175. */
  2176. static int node_order[MAX_NUMNODES];
  2177. static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
  2178. {
  2179. int pos, j, node;
  2180. int zone_type; /* needs to be signed */
  2181. struct zone *z;
  2182. struct zonelist *zonelist;
  2183. zonelist = &pgdat->node_zonelists[0];
  2184. pos = 0;
  2185. for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
  2186. for (j = 0; j < nr_nodes; j++) {
  2187. node = node_order[j];
  2188. z = &NODE_DATA(node)->node_zones[zone_type];
  2189. if (populated_zone(z)) {
  2190. zoneref_set_zone(z,
  2191. &zonelist->_zonerefs[pos++]);
  2192. check_highest_zone(zone_type);
  2193. }
  2194. }
  2195. }
  2196. zonelist->_zonerefs[pos].zone = NULL;
  2197. zonelist->_zonerefs[pos].zone_idx = 0;
  2198. }
  2199. static int default_zonelist_order(void)
  2200. {
  2201. int nid, zone_type;
  2202. unsigned long low_kmem_size,total_size;
  2203. struct zone *z;
  2204. int average_size;
  2205. /*
  2206. * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
  2207. * If they are really small and used heavily, the system can fall
  2208. * into OOM very easily.
  2209. * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
  2210. */
  2211. /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
  2212. low_kmem_size = 0;
  2213. total_size = 0;
  2214. for_each_online_node(nid) {
  2215. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2216. z = &NODE_DATA(nid)->node_zones[zone_type];
  2217. if (populated_zone(z)) {
  2218. if (zone_type < ZONE_NORMAL)
  2219. low_kmem_size += z->present_pages;
  2220. total_size += z->present_pages;
  2221. }
  2222. }
  2223. }
  2224. if (!low_kmem_size || /* there are no DMA area. */
  2225. low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
  2226. return ZONELIST_ORDER_NODE;
  2227. /*
  2228. * look into each node's config.
  2229. * If there is a node whose DMA/DMA32 memory is very big area on
  2230. * local memory, NODE_ORDER may be suitable.
  2231. */
  2232. average_size = total_size /
  2233. (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
  2234. for_each_online_node(nid) {
  2235. low_kmem_size = 0;
  2236. total_size = 0;
  2237. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2238. z = &NODE_DATA(nid)->node_zones[zone_type];
  2239. if (populated_zone(z)) {
  2240. if (zone_type < ZONE_NORMAL)
  2241. low_kmem_size += z->present_pages;
  2242. total_size += z->present_pages;
  2243. }
  2244. }
  2245. if (low_kmem_size &&
  2246. total_size > average_size && /* ignore small node */
  2247. low_kmem_size > total_size * 70/100)
  2248. return ZONELIST_ORDER_NODE;
  2249. }
  2250. return ZONELIST_ORDER_ZONE;
  2251. }
  2252. static void set_zonelist_order(void)
  2253. {
  2254. if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
  2255. current_zonelist_order = default_zonelist_order();
  2256. else
  2257. current_zonelist_order = user_zonelist_order;
  2258. }
  2259. static void build_zonelists(pg_data_t *pgdat)
  2260. {
  2261. int j, node, load;
  2262. enum zone_type i;
  2263. nodemask_t used_mask;
  2264. int local_node, prev_node;
  2265. struct zonelist *zonelist;
  2266. int order = current_zonelist_order;
  2267. /* initialize zonelists */
  2268. for (i = 0; i < MAX_ZONELISTS; i++) {
  2269. zonelist = pgdat->node_zonelists + i;
  2270. zonelist->_zonerefs[0].zone = NULL;
  2271. zonelist->_zonerefs[0].zone_idx = 0;
  2272. }
  2273. /* NUMA-aware ordering of nodes */
  2274. local_node = pgdat->node_id;
  2275. load = nr_online_nodes;
  2276. prev_node = local_node;
  2277. nodes_clear(used_mask);
  2278. memset(node_order, 0, sizeof(node_order));
  2279. j = 0;
  2280. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  2281. int distance = node_distance(local_node, node);
  2282. /*
  2283. * If another node is sufficiently far away then it is better
  2284. * to reclaim pages in a zone before going off node.
  2285. */
  2286. if (distance > RECLAIM_DISTANCE)
  2287. zone_reclaim_mode = 1;
  2288. /*
  2289. * We don't want to pressure a particular node.
  2290. * So adding penalty to the first node in same
  2291. * distance group to make it round-robin.
  2292. */
  2293. if (distance != node_distance(local_node, prev_node))
  2294. node_load[node] = load;
  2295. prev_node = node;
  2296. load--;
  2297. if (order == ZONELIST_ORDER_NODE)
  2298. build_zonelists_in_node_order(pgdat, node);
  2299. else
  2300. node_order[j++] = node; /* remember order */
  2301. }
  2302. if (order == ZONELIST_ORDER_ZONE) {
  2303. /* calculate node order -- i.e., DMA last! */
  2304. build_zonelists_in_zone_order(pgdat, j);
  2305. }
  2306. build_thisnode_zonelists(pgdat);
  2307. }
  2308. /* Construct the zonelist performance cache - see further mmzone.h */
  2309. static void build_zonelist_cache(pg_data_t *pgdat)
  2310. {
  2311. struct zonelist *zonelist;
  2312. struct zonelist_cache *zlc;
  2313. struct zoneref *z;
  2314. zonelist = &pgdat->node_zonelists[0];
  2315. zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
  2316. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  2317. for (z = zonelist->_zonerefs; z->zone; z++)
  2318. zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
  2319. }
  2320. #else /* CONFIG_NUMA */
  2321. static void set_zonelist_order(void)
  2322. {
  2323. current_zonelist_order = ZONELIST_ORDER_ZONE;
  2324. }
  2325. static void build_zonelists(pg_data_t *pgdat)
  2326. {
  2327. int node, local_node;
  2328. enum zone_type j;
  2329. struct zonelist *zonelist;
  2330. local_node = pgdat->node_id;
  2331. zonelist = &pgdat->node_zonelists[0];
  2332. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2333. /*
  2334. * Now we build the zonelist so that it contains the zones
  2335. * of all the other nodes.
  2336. * We don't want to pressure a particular node, so when
  2337. * building the zones for node N, we make sure that the
  2338. * zones coming right after the local ones are those from
  2339. * node N+1 (modulo N)
  2340. */
  2341. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  2342. if (!node_online(node))
  2343. continue;
  2344. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2345. MAX_NR_ZONES - 1);
  2346. }
  2347. for (node = 0; node < local_node; node++) {
  2348. if (!node_online(node))
  2349. continue;
  2350. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2351. MAX_NR_ZONES - 1);
  2352. }
  2353. zonelist->_zonerefs[j].zone = NULL;
  2354. zonelist->_zonerefs[j].zone_idx = 0;
  2355. }
  2356. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  2357. static void build_zonelist_cache(pg_data_t *pgdat)
  2358. {
  2359. pgdat->node_zonelists[0].zlcache_ptr = NULL;
  2360. }
  2361. #endif /* CONFIG_NUMA */
  2362. /*
  2363. * Boot pageset table. One per cpu which is going to be used for all
  2364. * zones and all nodes. The parameters will be set in such a way
  2365. * that an item put on a list will immediately be handed over to
  2366. * the buddy list. This is safe since pageset manipulation is done
  2367. * with interrupts disabled.
  2368. *
  2369. * The boot_pagesets must be kept even after bootup is complete for
  2370. * unused processors and/or zones. They do play a role for bootstrapping
  2371. * hotplugged processors.
  2372. *
  2373. * zoneinfo_show() and maybe other functions do
  2374. * not check if the processor is online before following the pageset pointer.
  2375. * Other parts of the kernel may not check if the zone is available.
  2376. */
  2377. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
  2378. static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
  2379. /* return values int ....just for stop_machine() */
  2380. static int __build_all_zonelists(void *dummy)
  2381. {
  2382. int nid;
  2383. int cpu;
  2384. #ifdef CONFIG_NUMA
  2385. memset(node_load, 0, sizeof(node_load));
  2386. #endif
  2387. for_each_online_node(nid) {
  2388. pg_data_t *pgdat = NODE_DATA(nid);
  2389. build_zonelists(pgdat);
  2390. build_zonelist_cache(pgdat);
  2391. }
  2392. /*
  2393. * Initialize the boot_pagesets that are going to be used
  2394. * for bootstrapping processors. The real pagesets for
  2395. * each zone will be allocated later when the per cpu
  2396. * allocator is available.
  2397. *
  2398. * boot_pagesets are used also for bootstrapping offline
  2399. * cpus if the system is already booted because the pagesets
  2400. * are needed to initialize allocators on a specific cpu too.
  2401. * F.e. the percpu allocator needs the page allocator which
  2402. * needs the percpu allocator in order to allocate its pagesets
  2403. * (a chicken-egg dilemma).
  2404. */
  2405. for_each_possible_cpu(cpu)
  2406. setup_pageset(&per_cpu(boot_pageset, cpu), 0);
  2407. return 0;
  2408. }
  2409. void build_all_zonelists(void)
  2410. {
  2411. set_zonelist_order();
  2412. if (system_state == SYSTEM_BOOTING) {
  2413. __build_all_zonelists(NULL);
  2414. mminit_verify_zonelist();
  2415. cpuset_init_current_mems_allowed();
  2416. } else {
  2417. /* we have to stop all cpus to guarantee there is no user
  2418. of zonelist */
  2419. stop_machine(__build_all_zonelists, NULL, NULL);
  2420. /* cpuset refresh routine should be here */
  2421. }
  2422. vm_total_pages = nr_free_pagecache_pages();
  2423. /*
  2424. * Disable grouping by mobility if the number of pages in the
  2425. * system is too low to allow the mechanism to work. It would be
  2426. * more accurate, but expensive to check per-zone. This check is
  2427. * made on memory-hotadd so a system can start with mobility
  2428. * disabled and enable it later
  2429. */
  2430. if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
  2431. page_group_by_mobility_disabled = 1;
  2432. else
  2433. page_group_by_mobility_disabled = 0;
  2434. printk("Built %i zonelists in %s order, mobility grouping %s. "
  2435. "Total pages: %ld\n",
  2436. nr_online_nodes,
  2437. zonelist_order_name[current_zonelist_order],
  2438. page_group_by_mobility_disabled ? "off" : "on",
  2439. vm_total_pages);
  2440. #ifdef CONFIG_NUMA
  2441. printk("Policy zone: %s\n", zone_names[policy_zone]);
  2442. #endif
  2443. }
  2444. /*
  2445. * Helper functions to size the waitqueue hash table.
  2446. * Essentially these want to choose hash table sizes sufficiently
  2447. * large so that collisions trying to wait on pages are rare.
  2448. * But in fact, the number of active page waitqueues on typical
  2449. * systems is ridiculously low, less than 200. So this is even
  2450. * conservative, even though it seems large.
  2451. *
  2452. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  2453. * waitqueues, i.e. the size of the waitq table given the number of pages.
  2454. */
  2455. #define PAGES_PER_WAITQUEUE 256
  2456. #ifndef CONFIG_MEMORY_HOTPLUG
  2457. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2458. {
  2459. unsigned long size = 1;
  2460. pages /= PAGES_PER_WAITQUEUE;
  2461. while (size < pages)
  2462. size <<= 1;
  2463. /*
  2464. * Once we have dozens or even hundreds of threads sleeping
  2465. * on IO we've got bigger problems than wait queue collision.
  2466. * Limit the size of the wait table to a reasonable size.
  2467. */
  2468. size = min(size, 4096UL);
  2469. return max(size, 4UL);
  2470. }
  2471. #else
  2472. /*
  2473. * A zone's size might be changed by hot-add, so it is not possible to determine
  2474. * a suitable size for its wait_table. So we use the maximum size now.
  2475. *
  2476. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  2477. *
  2478. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  2479. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  2480. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  2481. *
  2482. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  2483. * or more by the traditional way. (See above). It equals:
  2484. *
  2485. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  2486. * ia64(16K page size) : = ( 8G + 4M)byte.
  2487. * powerpc (64K page size) : = (32G +16M)byte.
  2488. */
  2489. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2490. {
  2491. return 4096UL;
  2492. }
  2493. #endif
  2494. /*
  2495. * This is an integer logarithm so that shifts can be used later
  2496. * to extract the more random high bits from the multiplicative
  2497. * hash function before the remainder is taken.
  2498. */
  2499. static inline unsigned long wait_table_bits(unsigned long size)
  2500. {
  2501. return ffz(~size);
  2502. }
  2503. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  2504. /*
  2505. * Mark a number of pageblocks as MIGRATE_RESERVE. The number
  2506. * of blocks reserved is based on min_wmark_pages(zone). The memory within
  2507. * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
  2508. * higher will lead to a bigger reserve which will get freed as contiguous
  2509. * blocks as reclaim kicks in
  2510. */
  2511. static void setup_zone_migrate_reserve(struct zone *zone)
  2512. {
  2513. unsigned long start_pfn, pfn, end_pfn;
  2514. struct page *page;
  2515. unsigned long block_migratetype;
  2516. int reserve;
  2517. /* Get the start pfn, end pfn and the number of blocks to reserve */
  2518. start_pfn = zone->zone_start_pfn;
  2519. end_pfn = start_pfn + zone->spanned_pages;
  2520. reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
  2521. pageblock_order;
  2522. /*
  2523. * Reserve blocks are generally in place to help high-order atomic
  2524. * allocations that are short-lived. A min_free_kbytes value that
  2525. * would result in more than 2 reserve blocks for atomic allocations
  2526. * is assumed to be in place to help anti-fragmentation for the
  2527. * future allocation of hugepages at runtime.
  2528. */
  2529. reserve = min(2, reserve);
  2530. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  2531. if (!pfn_valid(pfn))
  2532. continue;
  2533. page = pfn_to_page(pfn);
  2534. /* Watch out for overlapping nodes */
  2535. if (page_to_nid(page) != zone_to_nid(zone))
  2536. continue;
  2537. /* Blocks with reserved pages will never free, skip them. */
  2538. if (PageReserved(page))
  2539. continue;
  2540. block_migratetype = get_pageblock_migratetype(page);
  2541. /* If this block is reserved, account for it */
  2542. if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) {
  2543. reserve--;
  2544. continue;
  2545. }
  2546. /* Suitable for reserving if this block is movable */
  2547. if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) {
  2548. set_pageblock_migratetype(page, MIGRATE_RESERVE);
  2549. move_freepages_block(zone, page, MIGRATE_RESERVE);
  2550. reserve--;
  2551. continue;
  2552. }
  2553. /*
  2554. * If the reserve is met and this is a previous reserved block,
  2555. * take it back
  2556. */
  2557. if (block_migratetype == MIGRATE_RESERVE) {
  2558. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  2559. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  2560. }
  2561. }
  2562. }
  2563. /*
  2564. * Initially all pages are reserved - free ones are freed
  2565. * up by free_all_bootmem() once the early boot process is
  2566. * done. Non-atomic initialization, single-pass.
  2567. */
  2568. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  2569. unsigned long start_pfn, enum memmap_context context)
  2570. {
  2571. struct page *page;
  2572. unsigned long end_pfn = start_pfn + size;
  2573. unsigned long pfn;
  2574. struct zone *z;
  2575. if (highest_memmap_pfn < end_pfn - 1)
  2576. highest_memmap_pfn = end_pfn - 1;
  2577. z = &NODE_DATA(nid)->node_zones[zone];
  2578. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  2579. /*
  2580. * There can be holes in boot-time mem_map[]s
  2581. * handed to this function. They do not
  2582. * exist on hotplugged memory.
  2583. */
  2584. if (context == MEMMAP_EARLY) {
  2585. if (!early_pfn_valid(pfn))
  2586. continue;
  2587. if (!early_pfn_in_nid(pfn, nid))
  2588. continue;
  2589. }
  2590. page = pfn_to_page(pfn);
  2591. set_page_links(page, zone, nid, pfn);
  2592. mminit_verify_page_links(page, zone, nid, pfn);
  2593. init_page_count(page);
  2594. reset_page_mapcount(page);
  2595. SetPageReserved(page);
  2596. /*
  2597. * Mark the block movable so that blocks are reserved for
  2598. * movable at startup. This will force kernel allocations
  2599. * to reserve their blocks rather than leaking throughout
  2600. * the address space during boot when many long-lived
  2601. * kernel allocations are made. Later some blocks near
  2602. * the start are marked MIGRATE_RESERVE by
  2603. * setup_zone_migrate_reserve()
  2604. *
  2605. * bitmap is created for zone's valid pfn range. but memmap
  2606. * can be created for invalid pages (for alignment)
  2607. * check here not to call set_pageblock_migratetype() against
  2608. * pfn out of zone.
  2609. */
  2610. if ((z->zone_start_pfn <= pfn)
  2611. && (pfn < z->zone_start_pfn + z->spanned_pages)
  2612. && !(pfn & (pageblock_nr_pages - 1)))
  2613. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  2614. INIT_LIST_HEAD(&page->lru);
  2615. #ifdef WANT_PAGE_VIRTUAL
  2616. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  2617. if (!is_highmem_idx(zone))
  2618. set_page_address(page, __va(pfn << PAGE_SHIFT));
  2619. #endif
  2620. }
  2621. }
  2622. static void __meminit zone_init_free_lists(struct zone *zone)
  2623. {
  2624. int order, t;
  2625. for_each_migratetype_order(order, t) {
  2626. INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
  2627. zone->free_area[order].nr_free = 0;
  2628. }
  2629. }
  2630. #ifndef __HAVE_ARCH_MEMMAP_INIT
  2631. #define memmap_init(size, nid, zone, start_pfn) \
  2632. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  2633. #endif
  2634. static int zone_batchsize(struct zone *zone)
  2635. {
  2636. #ifdef CONFIG_MMU
  2637. int batch;
  2638. /*
  2639. * The per-cpu-pages pools are set to around 1000th of the
  2640. * size of the zone. But no more than 1/2 of a meg.
  2641. *
  2642. * OK, so we don't know how big the cache is. So guess.
  2643. */
  2644. batch = zone->present_pages / 1024;
  2645. if (batch * PAGE_SIZE > 512 * 1024)
  2646. batch = (512 * 1024) / PAGE_SIZE;
  2647. batch /= 4; /* We effectively *= 4 below */
  2648. if (batch < 1)
  2649. batch = 1;
  2650. /*
  2651. * Clamp the batch to a 2^n - 1 value. Having a power
  2652. * of 2 value was found to be more likely to have
  2653. * suboptimal cache aliasing properties in some cases.
  2654. *
  2655. * For example if 2 tasks are alternately allocating
  2656. * batches of pages, one task can end up with a lot
  2657. * of pages of one half of the possible page colors
  2658. * and the other with pages of the other colors.
  2659. */
  2660. batch = rounddown_pow_of_two(batch + batch/2) - 1;
  2661. return batch;
  2662. #else
  2663. /* The deferral and batching of frees should be suppressed under NOMMU
  2664. * conditions.
  2665. *
  2666. * The problem is that NOMMU needs to be able to allocate large chunks
  2667. * of contiguous memory as there's no hardware page translation to
  2668. * assemble apparent contiguous memory from discontiguous pages.
  2669. *
  2670. * Queueing large contiguous runs of pages for batching, however,
  2671. * causes the pages to actually be freed in smaller chunks. As there
  2672. * can be a significant delay between the individual batches being
  2673. * recycled, this leads to the once large chunks of space being
  2674. * fragmented and becoming unavailable for high-order allocations.
  2675. */
  2676. return 0;
  2677. #endif
  2678. }
  2679. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  2680. {
  2681. struct per_cpu_pages *pcp;
  2682. int migratetype;
  2683. memset(p, 0, sizeof(*p));
  2684. pcp = &p->pcp;
  2685. pcp->count = 0;
  2686. pcp->high = 6 * batch;
  2687. pcp->batch = max(1UL, 1 * batch);
  2688. for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
  2689. INIT_LIST_HEAD(&pcp->lists[migratetype]);
  2690. }
  2691. /*
  2692. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  2693. * to the value high for the pageset p.
  2694. */
  2695. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  2696. unsigned long high)
  2697. {
  2698. struct per_cpu_pages *pcp;
  2699. pcp = &p->pcp;
  2700. pcp->high = high;
  2701. pcp->batch = max(1UL, high/4);
  2702. if ((high/4) > (PAGE_SHIFT * 8))
  2703. pcp->batch = PAGE_SHIFT * 8;
  2704. }
  2705. /*
  2706. * Allocate per cpu pagesets and initialize them.
  2707. * Before this call only boot pagesets were available.
  2708. * Boot pagesets will no longer be used by this processorr
  2709. * after setup_per_cpu_pageset().
  2710. */
  2711. void __init setup_per_cpu_pageset(void)
  2712. {
  2713. struct zone *zone;
  2714. int cpu;
  2715. for_each_populated_zone(zone) {
  2716. zone->pageset = alloc_percpu(struct per_cpu_pageset);
  2717. for_each_possible_cpu(cpu) {
  2718. struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
  2719. setup_pageset(pcp, zone_batchsize(zone));
  2720. if (percpu_pagelist_fraction)
  2721. setup_pagelist_highmark(pcp,
  2722. (zone->present_pages /
  2723. percpu_pagelist_fraction));
  2724. }
  2725. }
  2726. }
  2727. static noinline __init_refok
  2728. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  2729. {
  2730. int i;
  2731. struct pglist_data *pgdat = zone->zone_pgdat;
  2732. size_t alloc_size;
  2733. /*
  2734. * The per-page waitqueue mechanism uses hashed waitqueues
  2735. * per zone.
  2736. */
  2737. zone->wait_table_hash_nr_entries =
  2738. wait_table_hash_nr_entries(zone_size_pages);
  2739. zone->wait_table_bits =
  2740. wait_table_bits(zone->wait_table_hash_nr_entries);
  2741. alloc_size = zone->wait_table_hash_nr_entries
  2742. * sizeof(wait_queue_head_t);
  2743. if (!slab_is_available()) {
  2744. zone->wait_table = (wait_queue_head_t *)
  2745. alloc_bootmem_node(pgdat, alloc_size);
  2746. } else {
  2747. /*
  2748. * This case means that a zone whose size was 0 gets new memory
  2749. * via memory hot-add.
  2750. * But it may be the case that a new node was hot-added. In
  2751. * this case vmalloc() will not be able to use this new node's
  2752. * memory - this wait_table must be initialized to use this new
  2753. * node itself as well.
  2754. * To use this new node's memory, further consideration will be
  2755. * necessary.
  2756. */
  2757. zone->wait_table = vmalloc(alloc_size);
  2758. }
  2759. if (!zone->wait_table)
  2760. return -ENOMEM;
  2761. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  2762. init_waitqueue_head(zone->wait_table + i);
  2763. return 0;
  2764. }
  2765. static int __zone_pcp_update(void *data)
  2766. {
  2767. struct zone *zone = data;
  2768. int cpu;
  2769. unsigned long batch = zone_batchsize(zone), flags;
  2770. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  2771. struct per_cpu_pageset *pset;
  2772. struct per_cpu_pages *pcp;
  2773. pset = per_cpu_ptr(zone->pageset, cpu);
  2774. pcp = &pset->pcp;
  2775. local_irq_save(flags);
  2776. free_pcppages_bulk(zone, pcp->count, pcp);
  2777. setup_pageset(pset, batch);
  2778. local_irq_restore(flags);
  2779. }
  2780. return 0;
  2781. }
  2782. void zone_pcp_update(struct zone *zone)
  2783. {
  2784. stop_machine(__zone_pcp_update, zone, NULL);
  2785. }
  2786. static __meminit void zone_pcp_init(struct zone *zone)
  2787. {
  2788. /*
  2789. * per cpu subsystem is not up at this point. The following code
  2790. * relies on the ability of the linker to provide the
  2791. * offset of a (static) per cpu variable into the per cpu area.
  2792. */
  2793. zone->pageset = &boot_pageset;
  2794. if (zone->present_pages)
  2795. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
  2796. zone->name, zone->present_pages,
  2797. zone_batchsize(zone));
  2798. }
  2799. __meminit int init_currently_empty_zone(struct zone *zone,
  2800. unsigned long zone_start_pfn,
  2801. unsigned long size,
  2802. enum memmap_context context)
  2803. {
  2804. struct pglist_data *pgdat = zone->zone_pgdat;
  2805. int ret;
  2806. ret = zone_wait_table_init(zone, size);
  2807. if (ret)
  2808. return ret;
  2809. pgdat->nr_zones = zone_idx(zone) + 1;
  2810. zone->zone_start_pfn = zone_start_pfn;
  2811. mminit_dprintk(MMINIT_TRACE, "memmap_init",
  2812. "Initialising map node %d zone %lu pfns %lu -> %lu\n",
  2813. pgdat->node_id,
  2814. (unsigned long)zone_idx(zone),
  2815. zone_start_pfn, (zone_start_pfn + size));
  2816. zone_init_free_lists(zone);
  2817. return 0;
  2818. }
  2819. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2820. /*
  2821. * Basic iterator support. Return the first range of PFNs for a node
  2822. * Note: nid == MAX_NUMNODES returns first region regardless of node
  2823. */
  2824. static int __meminit first_active_region_index_in_nid(int nid)
  2825. {
  2826. int i;
  2827. for (i = 0; i < nr_nodemap_entries; i++)
  2828. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  2829. return i;
  2830. return -1;
  2831. }
  2832. /*
  2833. * Basic iterator support. Return the next active range of PFNs for a node
  2834. * Note: nid == MAX_NUMNODES returns next region regardless of node
  2835. */
  2836. static int __meminit next_active_region_index_in_nid(int index, int nid)
  2837. {
  2838. for (index = index + 1; index < nr_nodemap_entries; index++)
  2839. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  2840. return index;
  2841. return -1;
  2842. }
  2843. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  2844. /*
  2845. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  2846. * Architectures may implement their own version but if add_active_range()
  2847. * was used and there are no special requirements, this is a convenient
  2848. * alternative
  2849. */
  2850. int __meminit __early_pfn_to_nid(unsigned long pfn)
  2851. {
  2852. int i;
  2853. for (i = 0; i < nr_nodemap_entries; i++) {
  2854. unsigned long start_pfn = early_node_map[i].start_pfn;
  2855. unsigned long end_pfn = early_node_map[i].end_pfn;
  2856. if (start_pfn <= pfn && pfn < end_pfn)
  2857. return early_node_map[i].nid;
  2858. }
  2859. /* This is a memory hole */
  2860. return -1;
  2861. }
  2862. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  2863. int __meminit early_pfn_to_nid(unsigned long pfn)
  2864. {
  2865. int nid;
  2866. nid = __early_pfn_to_nid(pfn);
  2867. if (nid >= 0)
  2868. return nid;
  2869. /* just returns 0 */
  2870. return 0;
  2871. }
  2872. #ifdef CONFIG_NODES_SPAN_OTHER_NODES
  2873. bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
  2874. {
  2875. int nid;
  2876. nid = __early_pfn_to_nid(pfn);
  2877. if (nid >= 0 && nid != node)
  2878. return false;
  2879. return true;
  2880. }
  2881. #endif
  2882. /* Basic iterator support to walk early_node_map[] */
  2883. #define for_each_active_range_index_in_nid(i, nid) \
  2884. for (i = first_active_region_index_in_nid(nid); i != -1; \
  2885. i = next_active_region_index_in_nid(i, nid))
  2886. /**
  2887. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  2888. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  2889. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  2890. *
  2891. * If an architecture guarantees that all ranges registered with
  2892. * add_active_ranges() contain no holes and may be freed, this
  2893. * this function may be used instead of calling free_bootmem() manually.
  2894. */
  2895. void __init free_bootmem_with_active_regions(int nid,
  2896. unsigned long max_low_pfn)
  2897. {
  2898. int i;
  2899. for_each_active_range_index_in_nid(i, nid) {
  2900. unsigned long size_pages = 0;
  2901. unsigned long end_pfn = early_node_map[i].end_pfn;
  2902. if (early_node_map[i].start_pfn >= max_low_pfn)
  2903. continue;
  2904. if (end_pfn > max_low_pfn)
  2905. end_pfn = max_low_pfn;
  2906. size_pages = end_pfn - early_node_map[i].start_pfn;
  2907. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  2908. PFN_PHYS(early_node_map[i].start_pfn),
  2909. size_pages << PAGE_SHIFT);
  2910. }
  2911. }
  2912. int __init add_from_early_node_map(struct range *range, int az,
  2913. int nr_range, int nid)
  2914. {
  2915. int i;
  2916. u64 start, end;
  2917. /* need to go over early_node_map to find out good range for node */
  2918. for_each_active_range_index_in_nid(i, nid) {
  2919. start = early_node_map[i].start_pfn;
  2920. end = early_node_map[i].end_pfn;
  2921. nr_range = add_range(range, az, nr_range, start, end);
  2922. }
  2923. return nr_range;
  2924. }
  2925. #ifdef CONFIG_NO_BOOTMEM
  2926. void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,
  2927. u64 goal, u64 limit)
  2928. {
  2929. int i;
  2930. void *ptr;
  2931. /* need to go over early_node_map to find out good range for node */
  2932. for_each_active_range_index_in_nid(i, nid) {
  2933. u64 addr;
  2934. u64 ei_start, ei_last;
  2935. ei_last = early_node_map[i].end_pfn;
  2936. ei_last <<= PAGE_SHIFT;
  2937. ei_start = early_node_map[i].start_pfn;
  2938. ei_start <<= PAGE_SHIFT;
  2939. addr = find_early_area(ei_start, ei_last,
  2940. goal, limit, size, align);
  2941. if (addr == -1ULL)
  2942. continue;
  2943. #if 0
  2944. printk(KERN_DEBUG "alloc (nid=%d %llx - %llx) (%llx - %llx) %llx %llx => %llx\n",
  2945. nid,
  2946. ei_start, ei_last, goal, limit, size,
  2947. align, addr);
  2948. #endif
  2949. ptr = phys_to_virt(addr);
  2950. memset(ptr, 0, size);
  2951. reserve_early_without_check(addr, addr + size, "BOOTMEM");
  2952. return ptr;
  2953. }
  2954. return NULL;
  2955. }
  2956. #endif
  2957. void __init work_with_active_regions(int nid, work_fn_t work_fn, void *data)
  2958. {
  2959. int i;
  2960. int ret;
  2961. for_each_active_range_index_in_nid(i, nid) {
  2962. ret = work_fn(early_node_map[i].start_pfn,
  2963. early_node_map[i].end_pfn, data);
  2964. if (ret)
  2965. break;
  2966. }
  2967. }
  2968. /**
  2969. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  2970. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  2971. *
  2972. * If an architecture guarantees that all ranges registered with
  2973. * add_active_ranges() contain no holes and may be freed, this
  2974. * function may be used instead of calling memory_present() manually.
  2975. */
  2976. void __init sparse_memory_present_with_active_regions(int nid)
  2977. {
  2978. int i;
  2979. for_each_active_range_index_in_nid(i, nid)
  2980. memory_present(early_node_map[i].nid,
  2981. early_node_map[i].start_pfn,
  2982. early_node_map[i].end_pfn);
  2983. }
  2984. /**
  2985. * get_pfn_range_for_nid - Return the start and end page frames for a node
  2986. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  2987. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  2988. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  2989. *
  2990. * It returns the start and end page frame of a node based on information
  2991. * provided by an arch calling add_active_range(). If called for a node
  2992. * with no available memory, a warning is printed and the start and end
  2993. * PFNs will be 0.
  2994. */
  2995. void __meminit get_pfn_range_for_nid(unsigned int nid,
  2996. unsigned long *start_pfn, unsigned long *end_pfn)
  2997. {
  2998. int i;
  2999. *start_pfn = -1UL;
  3000. *end_pfn = 0;
  3001. for_each_active_range_index_in_nid(i, nid) {
  3002. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  3003. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  3004. }
  3005. if (*start_pfn == -1UL)
  3006. *start_pfn = 0;
  3007. }
  3008. /*
  3009. * This finds a zone that can be used for ZONE_MOVABLE pages. The
  3010. * assumption is made that zones within a node are ordered in monotonic
  3011. * increasing memory addresses so that the "highest" populated zone is used
  3012. */
  3013. static void __init find_usable_zone_for_movable(void)
  3014. {
  3015. int zone_index;
  3016. for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
  3017. if (zone_index == ZONE_MOVABLE)
  3018. continue;
  3019. if (arch_zone_highest_possible_pfn[zone_index] >
  3020. arch_zone_lowest_possible_pfn[zone_index])
  3021. break;
  3022. }
  3023. VM_BUG_ON(zone_index == -1);
  3024. movable_zone = zone_index;
  3025. }
  3026. /*
  3027. * The zone ranges provided by the architecture do not include ZONE_MOVABLE
  3028. * because it is sized independant of architecture. Unlike the other zones,
  3029. * the starting point for ZONE_MOVABLE is not fixed. It may be different
  3030. * in each node depending on the size of each node and how evenly kernelcore
  3031. * is distributed. This helper function adjusts the zone ranges
  3032. * provided by the architecture for a given node by using the end of the
  3033. * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
  3034. * zones within a node are in order of monotonic increases memory addresses
  3035. */
  3036. static void __meminit adjust_zone_range_for_zone_movable(int nid,
  3037. unsigned long zone_type,
  3038. unsigned long node_start_pfn,
  3039. unsigned long node_end_pfn,
  3040. unsigned long *zone_start_pfn,
  3041. unsigned long *zone_end_pfn)
  3042. {
  3043. /* Only adjust if ZONE_MOVABLE is on this node */
  3044. if (zone_movable_pfn[nid]) {
  3045. /* Size ZONE_MOVABLE */
  3046. if (zone_type == ZONE_MOVABLE) {
  3047. *zone_start_pfn = zone_movable_pfn[nid];
  3048. *zone_end_pfn = min(node_end_pfn,
  3049. arch_zone_highest_possible_pfn[movable_zone]);
  3050. /* Adjust for ZONE_MOVABLE starting within this range */
  3051. } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
  3052. *zone_end_pfn > zone_movable_pfn[nid]) {
  3053. *zone_end_pfn = zone_movable_pfn[nid];
  3054. /* Check if this whole range is within ZONE_MOVABLE */
  3055. } else if (*zone_start_pfn >= zone_movable_pfn[nid])
  3056. *zone_start_pfn = *zone_end_pfn;
  3057. }
  3058. }
  3059. /*
  3060. * Return the number of pages a zone spans in a node, including holes
  3061. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  3062. */
  3063. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3064. unsigned long zone_type,
  3065. unsigned long *ignored)
  3066. {
  3067. unsigned long node_start_pfn, node_end_pfn;
  3068. unsigned long zone_start_pfn, zone_end_pfn;
  3069. /* Get the start and end of the node and zone */
  3070. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3071. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  3072. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  3073. adjust_zone_range_for_zone_movable(nid, zone_type,
  3074. node_start_pfn, node_end_pfn,
  3075. &zone_start_pfn, &zone_end_pfn);
  3076. /* Check that this node has pages within the zone's required range */
  3077. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  3078. return 0;
  3079. /* Move the zone boundaries inside the node if necessary */
  3080. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  3081. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  3082. /* Return the spanned pages */
  3083. return zone_end_pfn - zone_start_pfn;
  3084. }
  3085. /*
  3086. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  3087. * then all holes in the requested range will be accounted for.
  3088. */
  3089. unsigned long __meminit __absent_pages_in_range(int nid,
  3090. unsigned long range_start_pfn,
  3091. unsigned long range_end_pfn)
  3092. {
  3093. int i = 0;
  3094. unsigned long prev_end_pfn = 0, hole_pages = 0;
  3095. unsigned long start_pfn;
  3096. /* Find the end_pfn of the first active range of pfns in the node */
  3097. i = first_active_region_index_in_nid(nid);
  3098. if (i == -1)
  3099. return 0;
  3100. prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3101. /* Account for ranges before physical memory on this node */
  3102. if (early_node_map[i].start_pfn > range_start_pfn)
  3103. hole_pages = prev_end_pfn - range_start_pfn;
  3104. /* Find all holes for the zone within the node */
  3105. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  3106. /* No need to continue if prev_end_pfn is outside the zone */
  3107. if (prev_end_pfn >= range_end_pfn)
  3108. break;
  3109. /* Make sure the end of the zone is not within the hole */
  3110. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3111. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  3112. /* Update the hole size cound and move on */
  3113. if (start_pfn > range_start_pfn) {
  3114. BUG_ON(prev_end_pfn > start_pfn);
  3115. hole_pages += start_pfn - prev_end_pfn;
  3116. }
  3117. prev_end_pfn = early_node_map[i].end_pfn;
  3118. }
  3119. /* Account for ranges past physical memory on this node */
  3120. if (range_end_pfn > prev_end_pfn)
  3121. hole_pages += range_end_pfn -
  3122. max(range_start_pfn, prev_end_pfn);
  3123. return hole_pages;
  3124. }
  3125. /**
  3126. * absent_pages_in_range - Return number of page frames in holes within a range
  3127. * @start_pfn: The start PFN to start searching for holes
  3128. * @end_pfn: The end PFN to stop searching for holes
  3129. *
  3130. * It returns the number of pages frames in memory holes within a range.
  3131. */
  3132. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  3133. unsigned long end_pfn)
  3134. {
  3135. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  3136. }
  3137. /* Return the number of page frames in holes in a zone on a node */
  3138. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  3139. unsigned long zone_type,
  3140. unsigned long *ignored)
  3141. {
  3142. unsigned long node_start_pfn, node_end_pfn;
  3143. unsigned long zone_start_pfn, zone_end_pfn;
  3144. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3145. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  3146. node_start_pfn);
  3147. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  3148. node_end_pfn);
  3149. adjust_zone_range_for_zone_movable(nid, zone_type,
  3150. node_start_pfn, node_end_pfn,
  3151. &zone_start_pfn, &zone_end_pfn);
  3152. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  3153. }
  3154. #else
  3155. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3156. unsigned long zone_type,
  3157. unsigned long *zones_size)
  3158. {
  3159. return zones_size[zone_type];
  3160. }
  3161. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  3162. unsigned long zone_type,
  3163. unsigned long *zholes_size)
  3164. {
  3165. if (!zholes_size)
  3166. return 0;
  3167. return zholes_size[zone_type];
  3168. }
  3169. #endif
  3170. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  3171. unsigned long *zones_size, unsigned long *zholes_size)
  3172. {
  3173. unsigned long realtotalpages, totalpages = 0;
  3174. enum zone_type i;
  3175. for (i = 0; i < MAX_NR_ZONES; i++)
  3176. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  3177. zones_size);
  3178. pgdat->node_spanned_pages = totalpages;
  3179. realtotalpages = totalpages;
  3180. for (i = 0; i < MAX_NR_ZONES; i++)
  3181. realtotalpages -=
  3182. zone_absent_pages_in_node(pgdat->node_id, i,
  3183. zholes_size);
  3184. pgdat->node_present_pages = realtotalpages;
  3185. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  3186. realtotalpages);
  3187. }
  3188. #ifndef CONFIG_SPARSEMEM
  3189. /*
  3190. * Calculate the size of the zone->blockflags rounded to an unsigned long
  3191. * Start by making sure zonesize is a multiple of pageblock_order by rounding
  3192. * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
  3193. * round what is now in bits to nearest long in bits, then return it in
  3194. * bytes.
  3195. */
  3196. static unsigned long __init usemap_size(unsigned long zonesize)
  3197. {
  3198. unsigned long usemapsize;
  3199. usemapsize = roundup(zonesize, pageblock_nr_pages);
  3200. usemapsize = usemapsize >> pageblock_order;
  3201. usemapsize *= NR_PAGEBLOCK_BITS;
  3202. usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
  3203. return usemapsize / 8;
  3204. }
  3205. static void __init setup_usemap(struct pglist_data *pgdat,
  3206. struct zone *zone, unsigned long zonesize)
  3207. {
  3208. unsigned long usemapsize = usemap_size(zonesize);
  3209. zone->pageblock_flags = NULL;
  3210. if (usemapsize)
  3211. zone->pageblock_flags = alloc_bootmem_node(pgdat, usemapsize);
  3212. }
  3213. #else
  3214. static void inline setup_usemap(struct pglist_data *pgdat,
  3215. struct zone *zone, unsigned long zonesize) {}
  3216. #endif /* CONFIG_SPARSEMEM */
  3217. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  3218. /* Return a sensible default order for the pageblock size. */
  3219. static inline int pageblock_default_order(void)
  3220. {
  3221. if (HPAGE_SHIFT > PAGE_SHIFT)
  3222. return HUGETLB_PAGE_ORDER;
  3223. return MAX_ORDER-1;
  3224. }
  3225. /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
  3226. static inline void __init set_pageblock_order(unsigned int order)
  3227. {
  3228. /* Check that pageblock_nr_pages has not already been setup */
  3229. if (pageblock_order)
  3230. return;
  3231. /*
  3232. * Assume the largest contiguous order of interest is a huge page.
  3233. * This value may be variable depending on boot parameters on IA64
  3234. */
  3235. pageblock_order = order;
  3236. }
  3237. #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3238. /*
  3239. * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
  3240. * and pageblock_default_order() are unused as pageblock_order is set
  3241. * at compile-time. See include/linux/pageblock-flags.h for the values of
  3242. * pageblock_order based on the kernel config
  3243. */
  3244. static inline int pageblock_default_order(unsigned int order)
  3245. {
  3246. return MAX_ORDER-1;
  3247. }
  3248. #define set_pageblock_order(x) do {} while (0)
  3249. #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3250. /*
  3251. * Set up the zone data structures:
  3252. * - mark all pages reserved
  3253. * - mark all memory queues empty
  3254. * - clear the memory bitmaps
  3255. */
  3256. static void __paginginit free_area_init_core(struct pglist_data *pgdat,
  3257. unsigned long *zones_size, unsigned long *zholes_size)
  3258. {
  3259. enum zone_type j;
  3260. int nid = pgdat->node_id;
  3261. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  3262. int ret;
  3263. pgdat_resize_init(pgdat);
  3264. pgdat->nr_zones = 0;
  3265. init_waitqueue_head(&pgdat->kswapd_wait);
  3266. pgdat->kswapd_max_order = 0;
  3267. pgdat_page_cgroup_init(pgdat);
  3268. for (j = 0; j < MAX_NR_ZONES; j++) {
  3269. struct zone *zone = pgdat->node_zones + j;
  3270. unsigned long size, realsize, memmap_pages;
  3271. enum lru_list l;
  3272. size = zone_spanned_pages_in_node(nid, j, zones_size);
  3273. realsize = size - zone_absent_pages_in_node(nid, j,
  3274. zholes_size);
  3275. /*
  3276. * Adjust realsize so that it accounts for how much memory
  3277. * is used by this zone for memmap. This affects the watermark
  3278. * and per-cpu initialisations
  3279. */
  3280. memmap_pages =
  3281. PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT;
  3282. if (realsize >= memmap_pages) {
  3283. realsize -= memmap_pages;
  3284. if (memmap_pages)
  3285. printk(KERN_DEBUG
  3286. " %s zone: %lu pages used for memmap\n",
  3287. zone_names[j], memmap_pages);
  3288. } else
  3289. printk(KERN_WARNING
  3290. " %s zone: %lu pages exceeds realsize %lu\n",
  3291. zone_names[j], memmap_pages, realsize);
  3292. /* Account for reserved pages */
  3293. if (j == 0 && realsize > dma_reserve) {
  3294. realsize -= dma_reserve;
  3295. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  3296. zone_names[0], dma_reserve);
  3297. }
  3298. if (!is_highmem_idx(j))
  3299. nr_kernel_pages += realsize;
  3300. nr_all_pages += realsize;
  3301. zone->spanned_pages = size;
  3302. zone->present_pages = realsize;
  3303. #ifdef CONFIG_NUMA
  3304. zone->node = nid;
  3305. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  3306. / 100;
  3307. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  3308. #endif
  3309. zone->name = zone_names[j];
  3310. spin_lock_init(&zone->lock);
  3311. spin_lock_init(&zone->lru_lock);
  3312. zone_seqlock_init(zone);
  3313. zone->zone_pgdat = pgdat;
  3314. zone->prev_priority = DEF_PRIORITY;
  3315. zone_pcp_init(zone);
  3316. for_each_lru(l) {
  3317. INIT_LIST_HEAD(&zone->lru[l].list);
  3318. zone->reclaim_stat.nr_saved_scan[l] = 0;
  3319. }
  3320. zone->reclaim_stat.recent_rotated[0] = 0;
  3321. zone->reclaim_stat.recent_rotated[1] = 0;
  3322. zone->reclaim_stat.recent_scanned[0] = 0;
  3323. zone->reclaim_stat.recent_scanned[1] = 0;
  3324. zap_zone_vm_stats(zone);
  3325. zone->flags = 0;
  3326. if (!size)
  3327. continue;
  3328. set_pageblock_order(pageblock_default_order());
  3329. setup_usemap(pgdat, zone, size);
  3330. ret = init_currently_empty_zone(zone, zone_start_pfn,
  3331. size, MEMMAP_EARLY);
  3332. BUG_ON(ret);
  3333. memmap_init(size, nid, j, zone_start_pfn);
  3334. zone_start_pfn += size;
  3335. }
  3336. }
  3337. static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
  3338. {
  3339. /* Skip empty nodes */
  3340. if (!pgdat->node_spanned_pages)
  3341. return;
  3342. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3343. /* ia64 gets its own node_mem_map, before this, without bootmem */
  3344. if (!pgdat->node_mem_map) {
  3345. unsigned long size, start, end;
  3346. struct page *map;
  3347. /*
  3348. * The zone's endpoints aren't required to be MAX_ORDER
  3349. * aligned but the node_mem_map endpoints must be in order
  3350. * for the buddy allocator to function correctly.
  3351. */
  3352. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  3353. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  3354. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  3355. size = (end - start) * sizeof(struct page);
  3356. map = alloc_remap(pgdat->node_id, size);
  3357. if (!map)
  3358. map = alloc_bootmem_node(pgdat, size);
  3359. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  3360. }
  3361. #ifndef CONFIG_NEED_MULTIPLE_NODES
  3362. /*
  3363. * With no DISCONTIG, the global mem_map is just set as node 0's
  3364. */
  3365. if (pgdat == NODE_DATA(0)) {
  3366. mem_map = NODE_DATA(0)->node_mem_map;
  3367. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3368. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  3369. mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
  3370. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  3371. }
  3372. #endif
  3373. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  3374. }
  3375. void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
  3376. unsigned long node_start_pfn, unsigned long *zholes_size)
  3377. {
  3378. pg_data_t *pgdat = NODE_DATA(nid);
  3379. pgdat->node_id = nid;
  3380. pgdat->node_start_pfn = node_start_pfn;
  3381. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  3382. alloc_node_mem_map(pgdat);
  3383. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3384. printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
  3385. nid, (unsigned long)pgdat,
  3386. (unsigned long)pgdat->node_mem_map);
  3387. #endif
  3388. free_area_init_core(pgdat, zones_size, zholes_size);
  3389. }
  3390. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3391. #if MAX_NUMNODES > 1
  3392. /*
  3393. * Figure out the number of possible node ids.
  3394. */
  3395. static void __init setup_nr_node_ids(void)
  3396. {
  3397. unsigned int node;
  3398. unsigned int highest = 0;
  3399. for_each_node_mask(node, node_possible_map)
  3400. highest = node;
  3401. nr_node_ids = highest + 1;
  3402. }
  3403. #else
  3404. static inline void setup_nr_node_ids(void)
  3405. {
  3406. }
  3407. #endif
  3408. /**
  3409. * add_active_range - Register a range of PFNs backed by physical memory
  3410. * @nid: The node ID the range resides on
  3411. * @start_pfn: The start PFN of the available physical memory
  3412. * @end_pfn: The end PFN of the available physical memory
  3413. *
  3414. * These ranges are stored in an early_node_map[] and later used by
  3415. * free_area_init_nodes() to calculate zone sizes and holes. If the
  3416. * range spans a memory hole, it is up to the architecture to ensure
  3417. * the memory is not freed by the bootmem allocator. If possible
  3418. * the range being registered will be merged with existing ranges.
  3419. */
  3420. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  3421. unsigned long end_pfn)
  3422. {
  3423. int i;
  3424. mminit_dprintk(MMINIT_TRACE, "memory_register",
  3425. "Entering add_active_range(%d, %#lx, %#lx) "
  3426. "%d entries of %d used\n",
  3427. nid, start_pfn, end_pfn,
  3428. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  3429. mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
  3430. /* Merge with existing active regions if possible */
  3431. for (i = 0; i < nr_nodemap_entries; i++) {
  3432. if (early_node_map[i].nid != nid)
  3433. continue;
  3434. /* Skip if an existing region covers this new one */
  3435. if (start_pfn >= early_node_map[i].start_pfn &&
  3436. end_pfn <= early_node_map[i].end_pfn)
  3437. return;
  3438. /* Merge forward if suitable */
  3439. if (start_pfn <= early_node_map[i].end_pfn &&
  3440. end_pfn > early_node_map[i].end_pfn) {
  3441. early_node_map[i].end_pfn = end_pfn;
  3442. return;
  3443. }
  3444. /* Merge backward if suitable */
  3445. if (start_pfn < early_node_map[i].start_pfn &&
  3446. end_pfn >= early_node_map[i].start_pfn) {
  3447. early_node_map[i].start_pfn = start_pfn;
  3448. return;
  3449. }
  3450. }
  3451. /* Check that early_node_map is large enough */
  3452. if (i >= MAX_ACTIVE_REGIONS) {
  3453. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  3454. MAX_ACTIVE_REGIONS);
  3455. return;
  3456. }
  3457. early_node_map[i].nid = nid;
  3458. early_node_map[i].start_pfn = start_pfn;
  3459. early_node_map[i].end_pfn = end_pfn;
  3460. nr_nodemap_entries = i + 1;
  3461. }
  3462. /**
  3463. * remove_active_range - Shrink an existing registered range of PFNs
  3464. * @nid: The node id the range is on that should be shrunk
  3465. * @start_pfn: The new PFN of the range
  3466. * @end_pfn: The new PFN of the range
  3467. *
  3468. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  3469. * The map is kept near the end physical page range that has already been
  3470. * registered. This function allows an arch to shrink an existing registered
  3471. * range.
  3472. */
  3473. void __init remove_active_range(unsigned int nid, unsigned long start_pfn,
  3474. unsigned long end_pfn)
  3475. {
  3476. int i, j;
  3477. int removed = 0;
  3478. printk(KERN_DEBUG "remove_active_range (%d, %lu, %lu)\n",
  3479. nid, start_pfn, end_pfn);
  3480. /* Find the old active region end and shrink */
  3481. for_each_active_range_index_in_nid(i, nid) {
  3482. if (early_node_map[i].start_pfn >= start_pfn &&
  3483. early_node_map[i].end_pfn <= end_pfn) {
  3484. /* clear it */
  3485. early_node_map[i].start_pfn = 0;
  3486. early_node_map[i].end_pfn = 0;
  3487. removed = 1;
  3488. continue;
  3489. }
  3490. if (early_node_map[i].start_pfn < start_pfn &&
  3491. early_node_map[i].end_pfn > start_pfn) {
  3492. unsigned long temp_end_pfn = early_node_map[i].end_pfn;
  3493. early_node_map[i].end_pfn = start_pfn;
  3494. if (temp_end_pfn > end_pfn)
  3495. add_active_range(nid, end_pfn, temp_end_pfn);
  3496. continue;
  3497. }
  3498. if (early_node_map[i].start_pfn >= start_pfn &&
  3499. early_node_map[i].end_pfn > end_pfn &&
  3500. early_node_map[i].start_pfn < end_pfn) {
  3501. early_node_map[i].start_pfn = end_pfn;
  3502. continue;
  3503. }
  3504. }
  3505. if (!removed)
  3506. return;
  3507. /* remove the blank ones */
  3508. for (i = nr_nodemap_entries - 1; i > 0; i--) {
  3509. if (early_node_map[i].nid != nid)
  3510. continue;
  3511. if (early_node_map[i].end_pfn)
  3512. continue;
  3513. /* we found it, get rid of it */
  3514. for (j = i; j < nr_nodemap_entries - 1; j++)
  3515. memcpy(&early_node_map[j], &early_node_map[j+1],
  3516. sizeof(early_node_map[j]));
  3517. j = nr_nodemap_entries - 1;
  3518. memset(&early_node_map[j], 0, sizeof(early_node_map[j]));
  3519. nr_nodemap_entries--;
  3520. }
  3521. }
  3522. /**
  3523. * remove_all_active_ranges - Remove all currently registered regions
  3524. *
  3525. * During discovery, it may be found that a table like SRAT is invalid
  3526. * and an alternative discovery method must be used. This function removes
  3527. * all currently registered regions.
  3528. */
  3529. void __init remove_all_active_ranges(void)
  3530. {
  3531. memset(early_node_map, 0, sizeof(early_node_map));
  3532. nr_nodemap_entries = 0;
  3533. }
  3534. /* Compare two active node_active_regions */
  3535. static int __init cmp_node_active_region(const void *a, const void *b)
  3536. {
  3537. struct node_active_region *arange = (struct node_active_region *)a;
  3538. struct node_active_region *brange = (struct node_active_region *)b;
  3539. /* Done this way to avoid overflows */
  3540. if (arange->start_pfn > brange->start_pfn)
  3541. return 1;
  3542. if (arange->start_pfn < brange->start_pfn)
  3543. return -1;
  3544. return 0;
  3545. }
  3546. /* sort the node_map by start_pfn */
  3547. void __init sort_node_map(void)
  3548. {
  3549. sort(early_node_map, (size_t)nr_nodemap_entries,
  3550. sizeof(struct node_active_region),
  3551. cmp_node_active_region, NULL);
  3552. }
  3553. /* Find the lowest pfn for a node */
  3554. static unsigned long __init find_min_pfn_for_node(int nid)
  3555. {
  3556. int i;
  3557. unsigned long min_pfn = ULONG_MAX;
  3558. /* Assuming a sorted map, the first range found has the starting pfn */
  3559. for_each_active_range_index_in_nid(i, nid)
  3560. min_pfn = min(min_pfn, early_node_map[i].start_pfn);
  3561. if (min_pfn == ULONG_MAX) {
  3562. printk(KERN_WARNING
  3563. "Could not find start_pfn for node %d\n", nid);
  3564. return 0;
  3565. }
  3566. return min_pfn;
  3567. }
  3568. /**
  3569. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  3570. *
  3571. * It returns the minimum PFN based on information provided via
  3572. * add_active_range().
  3573. */
  3574. unsigned long __init find_min_pfn_with_active_regions(void)
  3575. {
  3576. return find_min_pfn_for_node(MAX_NUMNODES);
  3577. }
  3578. /*
  3579. * early_calculate_totalpages()
  3580. * Sum pages in active regions for movable zone.
  3581. * Populate N_HIGH_MEMORY for calculating usable_nodes.
  3582. */
  3583. static unsigned long __init early_calculate_totalpages(void)
  3584. {
  3585. int i;
  3586. unsigned long totalpages = 0;
  3587. for (i = 0; i < nr_nodemap_entries; i++) {
  3588. unsigned long pages = early_node_map[i].end_pfn -
  3589. early_node_map[i].start_pfn;
  3590. totalpages += pages;
  3591. if (pages)
  3592. node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
  3593. }
  3594. return totalpages;
  3595. }
  3596. /*
  3597. * Find the PFN the Movable zone begins in each node. Kernel memory
  3598. * is spread evenly between nodes as long as the nodes have enough
  3599. * memory. When they don't, some nodes will have more kernelcore than
  3600. * others
  3601. */
  3602. static void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
  3603. {
  3604. int i, nid;
  3605. unsigned long usable_startpfn;
  3606. unsigned long kernelcore_node, kernelcore_remaining;
  3607. /* save the state before borrow the nodemask */
  3608. nodemask_t saved_node_state = node_states[N_HIGH_MEMORY];
  3609. unsigned long totalpages = early_calculate_totalpages();
  3610. int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
  3611. /*
  3612. * If movablecore was specified, calculate what size of
  3613. * kernelcore that corresponds so that memory usable for
  3614. * any allocation type is evenly spread. If both kernelcore
  3615. * and movablecore are specified, then the value of kernelcore
  3616. * will be used for required_kernelcore if it's greater than
  3617. * what movablecore would have allowed.
  3618. */
  3619. if (required_movablecore) {
  3620. unsigned long corepages;
  3621. /*
  3622. * Round-up so that ZONE_MOVABLE is at least as large as what
  3623. * was requested by the user
  3624. */
  3625. required_movablecore =
  3626. roundup(required_movablecore, MAX_ORDER_NR_PAGES);
  3627. corepages = totalpages - required_movablecore;
  3628. required_kernelcore = max(required_kernelcore, corepages);
  3629. }
  3630. /* If kernelcore was not specified, there is no ZONE_MOVABLE */
  3631. if (!required_kernelcore)
  3632. goto out;
  3633. /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
  3634. find_usable_zone_for_movable();
  3635. usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
  3636. restart:
  3637. /* Spread kernelcore memory as evenly as possible throughout nodes */
  3638. kernelcore_node = required_kernelcore / usable_nodes;
  3639. for_each_node_state(nid, N_HIGH_MEMORY) {
  3640. /*
  3641. * Recalculate kernelcore_node if the division per node
  3642. * now exceeds what is necessary to satisfy the requested
  3643. * amount of memory for the kernel
  3644. */
  3645. if (required_kernelcore < kernelcore_node)
  3646. kernelcore_node = required_kernelcore / usable_nodes;
  3647. /*
  3648. * As the map is walked, we track how much memory is usable
  3649. * by the kernel using kernelcore_remaining. When it is
  3650. * 0, the rest of the node is usable by ZONE_MOVABLE
  3651. */
  3652. kernelcore_remaining = kernelcore_node;
  3653. /* Go through each range of PFNs within this node */
  3654. for_each_active_range_index_in_nid(i, nid) {
  3655. unsigned long start_pfn, end_pfn;
  3656. unsigned long size_pages;
  3657. start_pfn = max(early_node_map[i].start_pfn,
  3658. zone_movable_pfn[nid]);
  3659. end_pfn = early_node_map[i].end_pfn;
  3660. if (start_pfn >= end_pfn)
  3661. continue;
  3662. /* Account for what is only usable for kernelcore */
  3663. if (start_pfn < usable_startpfn) {
  3664. unsigned long kernel_pages;
  3665. kernel_pages = min(end_pfn, usable_startpfn)
  3666. - start_pfn;
  3667. kernelcore_remaining -= min(kernel_pages,
  3668. kernelcore_remaining);
  3669. required_kernelcore -= min(kernel_pages,
  3670. required_kernelcore);
  3671. /* Continue if range is now fully accounted */
  3672. if (end_pfn <= usable_startpfn) {
  3673. /*
  3674. * Push zone_movable_pfn to the end so
  3675. * that if we have to rebalance
  3676. * kernelcore across nodes, we will
  3677. * not double account here
  3678. */
  3679. zone_movable_pfn[nid] = end_pfn;
  3680. continue;
  3681. }
  3682. start_pfn = usable_startpfn;
  3683. }
  3684. /*
  3685. * The usable PFN range for ZONE_MOVABLE is from
  3686. * start_pfn->end_pfn. Calculate size_pages as the
  3687. * number of pages used as kernelcore
  3688. */
  3689. size_pages = end_pfn - start_pfn;
  3690. if (size_pages > kernelcore_remaining)
  3691. size_pages = kernelcore_remaining;
  3692. zone_movable_pfn[nid] = start_pfn + size_pages;
  3693. /*
  3694. * Some kernelcore has been met, update counts and
  3695. * break if the kernelcore for this node has been
  3696. * satisified
  3697. */
  3698. required_kernelcore -= min(required_kernelcore,
  3699. size_pages);
  3700. kernelcore_remaining -= size_pages;
  3701. if (!kernelcore_remaining)
  3702. break;
  3703. }
  3704. }
  3705. /*
  3706. * If there is still required_kernelcore, we do another pass with one
  3707. * less node in the count. This will push zone_movable_pfn[nid] further
  3708. * along on the nodes that still have memory until kernelcore is
  3709. * satisified
  3710. */
  3711. usable_nodes--;
  3712. if (usable_nodes && required_kernelcore > usable_nodes)
  3713. goto restart;
  3714. /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
  3715. for (nid = 0; nid < MAX_NUMNODES; nid++)
  3716. zone_movable_pfn[nid] =
  3717. roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
  3718. out:
  3719. /* restore the node_state */
  3720. node_states[N_HIGH_MEMORY] = saved_node_state;
  3721. }
  3722. /* Any regular memory on that node ? */
  3723. static void check_for_regular_memory(pg_data_t *pgdat)
  3724. {
  3725. #ifdef CONFIG_HIGHMEM
  3726. enum zone_type zone_type;
  3727. for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
  3728. struct zone *zone = &pgdat->node_zones[zone_type];
  3729. if (zone->present_pages)
  3730. node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  3731. }
  3732. #endif
  3733. }
  3734. /**
  3735. * free_area_init_nodes - Initialise all pg_data_t and zone data
  3736. * @max_zone_pfn: an array of max PFNs for each zone
  3737. *
  3738. * This will call free_area_init_node() for each active node in the system.
  3739. * Using the page ranges provided by add_active_range(), the size of each
  3740. * zone in each node and their holes is calculated. If the maximum PFN
  3741. * between two adjacent zones match, it is assumed that the zone is empty.
  3742. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  3743. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  3744. * starts where the previous one ended. For example, ZONE_DMA32 starts
  3745. * at arch_max_dma_pfn.
  3746. */
  3747. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  3748. {
  3749. unsigned long nid;
  3750. int i;
  3751. /* Sort early_node_map as initialisation assumes it is sorted */
  3752. sort_node_map();
  3753. /* Record where the zone boundaries are */
  3754. memset(arch_zone_lowest_possible_pfn, 0,
  3755. sizeof(arch_zone_lowest_possible_pfn));
  3756. memset(arch_zone_highest_possible_pfn, 0,
  3757. sizeof(arch_zone_highest_possible_pfn));
  3758. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  3759. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  3760. for (i = 1; i < MAX_NR_ZONES; i++) {
  3761. if (i == ZONE_MOVABLE)
  3762. continue;
  3763. arch_zone_lowest_possible_pfn[i] =
  3764. arch_zone_highest_possible_pfn[i-1];
  3765. arch_zone_highest_possible_pfn[i] =
  3766. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  3767. }
  3768. arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
  3769. arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
  3770. /* Find the PFNs that ZONE_MOVABLE begins at in each node */
  3771. memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
  3772. find_zone_movable_pfns_for_nodes(zone_movable_pfn);
  3773. /* Print out the zone ranges */
  3774. printk("Zone PFN ranges:\n");
  3775. for (i = 0; i < MAX_NR_ZONES; i++) {
  3776. if (i == ZONE_MOVABLE)
  3777. continue;
  3778. printk(" %-8s %0#10lx -> %0#10lx\n",
  3779. zone_names[i],
  3780. arch_zone_lowest_possible_pfn[i],
  3781. arch_zone_highest_possible_pfn[i]);
  3782. }
  3783. /* Print out the PFNs ZONE_MOVABLE begins at in each node */
  3784. printk("Movable zone start PFN for each node\n");
  3785. for (i = 0; i < MAX_NUMNODES; i++) {
  3786. if (zone_movable_pfn[i])
  3787. printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
  3788. }
  3789. /* Print out the early_node_map[] */
  3790. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  3791. for (i = 0; i < nr_nodemap_entries; i++)
  3792. printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid,
  3793. early_node_map[i].start_pfn,
  3794. early_node_map[i].end_pfn);
  3795. /* Initialise every node */
  3796. mminit_verify_pageflags_layout();
  3797. setup_nr_node_ids();
  3798. for_each_online_node(nid) {
  3799. pg_data_t *pgdat = NODE_DATA(nid);
  3800. free_area_init_node(nid, NULL,
  3801. find_min_pfn_for_node(nid), NULL);
  3802. /* Any memory on that node */
  3803. if (pgdat->node_present_pages)
  3804. node_set_state(nid, N_HIGH_MEMORY);
  3805. check_for_regular_memory(pgdat);
  3806. }
  3807. }
  3808. static int __init cmdline_parse_core(char *p, unsigned long *core)
  3809. {
  3810. unsigned long long coremem;
  3811. if (!p)
  3812. return -EINVAL;
  3813. coremem = memparse(p, &p);
  3814. *core = coremem >> PAGE_SHIFT;
  3815. /* Paranoid check that UL is enough for the coremem value */
  3816. WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
  3817. return 0;
  3818. }
  3819. /*
  3820. * kernelcore=size sets the amount of memory for use for allocations that
  3821. * cannot be reclaimed or migrated.
  3822. */
  3823. static int __init cmdline_parse_kernelcore(char *p)
  3824. {
  3825. return cmdline_parse_core(p, &required_kernelcore);
  3826. }
  3827. /*
  3828. * movablecore=size sets the amount of memory for use for allocations that
  3829. * can be reclaimed or migrated.
  3830. */
  3831. static int __init cmdline_parse_movablecore(char *p)
  3832. {
  3833. return cmdline_parse_core(p, &required_movablecore);
  3834. }
  3835. early_param("kernelcore", cmdline_parse_kernelcore);
  3836. early_param("movablecore", cmdline_parse_movablecore);
  3837. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  3838. /**
  3839. * set_dma_reserve - set the specified number of pages reserved in the first zone
  3840. * @new_dma_reserve: The number of pages to mark reserved
  3841. *
  3842. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  3843. * In the DMA zone, a significant percentage may be consumed by kernel image
  3844. * and other unfreeable allocations which can skew the watermarks badly. This
  3845. * function may optionally be used to account for unfreeable pages in the
  3846. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  3847. * smaller per-cpu batchsize.
  3848. */
  3849. void __init set_dma_reserve(unsigned long new_dma_reserve)
  3850. {
  3851. dma_reserve = new_dma_reserve;
  3852. }
  3853. #ifndef CONFIG_NEED_MULTIPLE_NODES
  3854. struct pglist_data __refdata contig_page_data = {
  3855. #ifndef CONFIG_NO_BOOTMEM
  3856. .bdata = &bootmem_node_data[0]
  3857. #endif
  3858. };
  3859. EXPORT_SYMBOL(contig_page_data);
  3860. #endif
  3861. void __init free_area_init(unsigned long *zones_size)
  3862. {
  3863. free_area_init_node(0, zones_size,
  3864. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  3865. }
  3866. static int page_alloc_cpu_notify(struct notifier_block *self,
  3867. unsigned long action, void *hcpu)
  3868. {
  3869. int cpu = (unsigned long)hcpu;
  3870. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  3871. drain_pages(cpu);
  3872. /*
  3873. * Spill the event counters of the dead processor
  3874. * into the current processors event counters.
  3875. * This artificially elevates the count of the current
  3876. * processor.
  3877. */
  3878. vm_events_fold_cpu(cpu);
  3879. /*
  3880. * Zero the differential counters of the dead processor
  3881. * so that the vm statistics are consistent.
  3882. *
  3883. * This is only okay since the processor is dead and cannot
  3884. * race with what we are doing.
  3885. */
  3886. refresh_cpu_vm_stats(cpu);
  3887. }
  3888. return NOTIFY_OK;
  3889. }
  3890. void __init page_alloc_init(void)
  3891. {
  3892. hotcpu_notifier(page_alloc_cpu_notify, 0);
  3893. }
  3894. /*
  3895. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  3896. * or min_free_kbytes changes.
  3897. */
  3898. static void calculate_totalreserve_pages(void)
  3899. {
  3900. struct pglist_data *pgdat;
  3901. unsigned long reserve_pages = 0;
  3902. enum zone_type i, j;
  3903. for_each_online_pgdat(pgdat) {
  3904. for (i = 0; i < MAX_NR_ZONES; i++) {
  3905. struct zone *zone = pgdat->node_zones + i;
  3906. unsigned long max = 0;
  3907. /* Find valid and maximum lowmem_reserve in the zone */
  3908. for (j = i; j < MAX_NR_ZONES; j++) {
  3909. if (zone->lowmem_reserve[j] > max)
  3910. max = zone->lowmem_reserve[j];
  3911. }
  3912. /* we treat the high watermark as reserved pages. */
  3913. max += high_wmark_pages(zone);
  3914. if (max > zone->present_pages)
  3915. max = zone->present_pages;
  3916. reserve_pages += max;
  3917. }
  3918. }
  3919. totalreserve_pages = reserve_pages;
  3920. }
  3921. /*
  3922. * setup_per_zone_lowmem_reserve - called whenever
  3923. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  3924. * has a correct pages reserved value, so an adequate number of
  3925. * pages are left in the zone after a successful __alloc_pages().
  3926. */
  3927. static void setup_per_zone_lowmem_reserve(void)
  3928. {
  3929. struct pglist_data *pgdat;
  3930. enum zone_type j, idx;
  3931. for_each_online_pgdat(pgdat) {
  3932. for (j = 0; j < MAX_NR_ZONES; j++) {
  3933. struct zone *zone = pgdat->node_zones + j;
  3934. unsigned long present_pages = zone->present_pages;
  3935. zone->lowmem_reserve[j] = 0;
  3936. idx = j;
  3937. while (idx) {
  3938. struct zone *lower_zone;
  3939. idx--;
  3940. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  3941. sysctl_lowmem_reserve_ratio[idx] = 1;
  3942. lower_zone = pgdat->node_zones + idx;
  3943. lower_zone->lowmem_reserve[j] = present_pages /
  3944. sysctl_lowmem_reserve_ratio[idx];
  3945. present_pages += lower_zone->present_pages;
  3946. }
  3947. }
  3948. }
  3949. /* update totalreserve_pages */
  3950. calculate_totalreserve_pages();
  3951. }
  3952. /**
  3953. * setup_per_zone_wmarks - called when min_free_kbytes changes
  3954. * or when memory is hot-{added|removed}
  3955. *
  3956. * Ensures that the watermark[min,low,high] values for each zone are set
  3957. * correctly with respect to min_free_kbytes.
  3958. */
  3959. void setup_per_zone_wmarks(void)
  3960. {
  3961. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  3962. unsigned long lowmem_pages = 0;
  3963. struct zone *zone;
  3964. unsigned long flags;
  3965. /* Calculate total number of !ZONE_HIGHMEM pages */
  3966. for_each_zone(zone) {
  3967. if (!is_highmem(zone))
  3968. lowmem_pages += zone->present_pages;
  3969. }
  3970. for_each_zone(zone) {
  3971. u64 tmp;
  3972. spin_lock_irqsave(&zone->lock, flags);
  3973. tmp = (u64)pages_min * zone->present_pages;
  3974. do_div(tmp, lowmem_pages);
  3975. if (is_highmem(zone)) {
  3976. /*
  3977. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  3978. * need highmem pages, so cap pages_min to a small
  3979. * value here.
  3980. *
  3981. * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
  3982. * deltas controls asynch page reclaim, and so should
  3983. * not be capped for highmem.
  3984. */
  3985. int min_pages;
  3986. min_pages = zone->present_pages / 1024;
  3987. if (min_pages < SWAP_CLUSTER_MAX)
  3988. min_pages = SWAP_CLUSTER_MAX;
  3989. if (min_pages > 128)
  3990. min_pages = 128;
  3991. zone->watermark[WMARK_MIN] = min_pages;
  3992. } else {
  3993. /*
  3994. * If it's a lowmem zone, reserve a number of pages
  3995. * proportionate to the zone's size.
  3996. */
  3997. zone->watermark[WMARK_MIN] = tmp;
  3998. }
  3999. zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
  4000. zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
  4001. setup_zone_migrate_reserve(zone);
  4002. spin_unlock_irqrestore(&zone->lock, flags);
  4003. }
  4004. /* update totalreserve_pages */
  4005. calculate_totalreserve_pages();
  4006. }
  4007. /*
  4008. * The inactive anon list should be small enough that the VM never has to
  4009. * do too much work, but large enough that each inactive page has a chance
  4010. * to be referenced again before it is swapped out.
  4011. *
  4012. * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
  4013. * INACTIVE_ANON pages on this zone's LRU, maintained by the
  4014. * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
  4015. * the anonymous pages are kept on the inactive list.
  4016. *
  4017. * total target max
  4018. * memory ratio inactive anon
  4019. * -------------------------------------
  4020. * 10MB 1 5MB
  4021. * 100MB 1 50MB
  4022. * 1GB 3 250MB
  4023. * 10GB 10 0.9GB
  4024. * 100GB 31 3GB
  4025. * 1TB 101 10GB
  4026. * 10TB 320 32GB
  4027. */
  4028. void calculate_zone_inactive_ratio(struct zone *zone)
  4029. {
  4030. unsigned int gb, ratio;
  4031. /* Zone size in gigabytes */
  4032. gb = zone->present_pages >> (30 - PAGE_SHIFT);
  4033. if (gb)
  4034. ratio = int_sqrt(10 * gb);
  4035. else
  4036. ratio = 1;
  4037. zone->inactive_ratio = ratio;
  4038. }
  4039. static void __init setup_per_zone_inactive_ratio(void)
  4040. {
  4041. struct zone *zone;
  4042. for_each_zone(zone)
  4043. calculate_zone_inactive_ratio(zone);
  4044. }
  4045. /*
  4046. * Initialise min_free_kbytes.
  4047. *
  4048. * For small machines we want it small (128k min). For large machines
  4049. * we want it large (64MB max). But it is not linear, because network
  4050. * bandwidth does not increase linearly with machine size. We use
  4051. *
  4052. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  4053. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  4054. *
  4055. * which yields
  4056. *
  4057. * 16MB: 512k
  4058. * 32MB: 724k
  4059. * 64MB: 1024k
  4060. * 128MB: 1448k
  4061. * 256MB: 2048k
  4062. * 512MB: 2896k
  4063. * 1024MB: 4096k
  4064. * 2048MB: 5792k
  4065. * 4096MB: 8192k
  4066. * 8192MB: 11584k
  4067. * 16384MB: 16384k
  4068. */
  4069. static int __init init_per_zone_wmark_min(void)
  4070. {
  4071. unsigned long lowmem_kbytes;
  4072. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  4073. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  4074. if (min_free_kbytes < 128)
  4075. min_free_kbytes = 128;
  4076. if (min_free_kbytes > 65536)
  4077. min_free_kbytes = 65536;
  4078. setup_per_zone_wmarks();
  4079. setup_per_zone_lowmem_reserve();
  4080. setup_per_zone_inactive_ratio();
  4081. return 0;
  4082. }
  4083. module_init(init_per_zone_wmark_min)
  4084. /*
  4085. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  4086. * that we can call two helper functions whenever min_free_kbytes
  4087. * changes.
  4088. */
  4089. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  4090. void __user *buffer, size_t *length, loff_t *ppos)
  4091. {
  4092. proc_dointvec(table, write, buffer, length, ppos);
  4093. if (write)
  4094. setup_per_zone_wmarks();
  4095. return 0;
  4096. }
  4097. #ifdef CONFIG_NUMA
  4098. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  4099. void __user *buffer, size_t *length, loff_t *ppos)
  4100. {
  4101. struct zone *zone;
  4102. int rc;
  4103. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4104. if (rc)
  4105. return rc;
  4106. for_each_zone(zone)
  4107. zone->min_unmapped_pages = (zone->present_pages *
  4108. sysctl_min_unmapped_ratio) / 100;
  4109. return 0;
  4110. }
  4111. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  4112. void __user *buffer, size_t *length, loff_t *ppos)
  4113. {
  4114. struct zone *zone;
  4115. int rc;
  4116. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4117. if (rc)
  4118. return rc;
  4119. for_each_zone(zone)
  4120. zone->min_slab_pages = (zone->present_pages *
  4121. sysctl_min_slab_ratio) / 100;
  4122. return 0;
  4123. }
  4124. #endif
  4125. /*
  4126. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  4127. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  4128. * whenever sysctl_lowmem_reserve_ratio changes.
  4129. *
  4130. * The reserve ratio obviously has absolutely no relation with the
  4131. * minimum watermarks. The lowmem reserve ratio can only make sense
  4132. * if in function of the boot time zone sizes.
  4133. */
  4134. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  4135. void __user *buffer, size_t *length, loff_t *ppos)
  4136. {
  4137. proc_dointvec_minmax(table, write, buffer, length, ppos);
  4138. setup_per_zone_lowmem_reserve();
  4139. return 0;
  4140. }
  4141. /*
  4142. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  4143. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  4144. * can have before it gets flushed back to buddy allocator.
  4145. */
  4146. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  4147. void __user *buffer, size_t *length, loff_t *ppos)
  4148. {
  4149. struct zone *zone;
  4150. unsigned int cpu;
  4151. int ret;
  4152. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4153. if (!write || (ret == -EINVAL))
  4154. return ret;
  4155. for_each_populated_zone(zone) {
  4156. for_each_possible_cpu(cpu) {
  4157. unsigned long high;
  4158. high = zone->present_pages / percpu_pagelist_fraction;
  4159. setup_pagelist_highmark(
  4160. per_cpu_ptr(zone->pageset, cpu), high);
  4161. }
  4162. }
  4163. return 0;
  4164. }
  4165. int hashdist = HASHDIST_DEFAULT;
  4166. #ifdef CONFIG_NUMA
  4167. static int __init set_hashdist(char *str)
  4168. {
  4169. if (!str)
  4170. return 0;
  4171. hashdist = simple_strtoul(str, &str, 0);
  4172. return 1;
  4173. }
  4174. __setup("hashdist=", set_hashdist);
  4175. #endif
  4176. /*
  4177. * allocate a large system hash table from bootmem
  4178. * - it is assumed that the hash table must contain an exact power-of-2
  4179. * quantity of entries
  4180. * - limit is the number of hash buckets, not the total allocation size
  4181. */
  4182. void *__init alloc_large_system_hash(const char *tablename,
  4183. unsigned long bucketsize,
  4184. unsigned long numentries,
  4185. int scale,
  4186. int flags,
  4187. unsigned int *_hash_shift,
  4188. unsigned int *_hash_mask,
  4189. unsigned long limit)
  4190. {
  4191. unsigned long long max = limit;
  4192. unsigned long log2qty, size;
  4193. void *table = NULL;
  4194. /* allow the kernel cmdline to have a say */
  4195. if (!numentries) {
  4196. /* round applicable memory size up to nearest megabyte */
  4197. numentries = nr_kernel_pages;
  4198. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  4199. numentries >>= 20 - PAGE_SHIFT;
  4200. numentries <<= 20 - PAGE_SHIFT;
  4201. /* limit to 1 bucket per 2^scale bytes of low memory */
  4202. if (scale > PAGE_SHIFT)
  4203. numentries >>= (scale - PAGE_SHIFT);
  4204. else
  4205. numentries <<= (PAGE_SHIFT - scale);
  4206. /* Make sure we've got at least a 0-order allocation.. */
  4207. if (unlikely(flags & HASH_SMALL)) {
  4208. /* Makes no sense without HASH_EARLY */
  4209. WARN_ON(!(flags & HASH_EARLY));
  4210. if (!(numentries >> *_hash_shift)) {
  4211. numentries = 1UL << *_hash_shift;
  4212. BUG_ON(!numentries);
  4213. }
  4214. } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  4215. numentries = PAGE_SIZE / bucketsize;
  4216. }
  4217. numentries = roundup_pow_of_two(numentries);
  4218. /* limit allocation size to 1/16 total memory by default */
  4219. if (max == 0) {
  4220. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  4221. do_div(max, bucketsize);
  4222. }
  4223. if (numentries > max)
  4224. numentries = max;
  4225. log2qty = ilog2(numentries);
  4226. do {
  4227. size = bucketsize << log2qty;
  4228. if (flags & HASH_EARLY)
  4229. table = alloc_bootmem_nopanic(size);
  4230. else if (hashdist)
  4231. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  4232. else {
  4233. /*
  4234. * If bucketsize is not a power-of-two, we may free
  4235. * some pages at the end of hash table which
  4236. * alloc_pages_exact() automatically does
  4237. */
  4238. if (get_order(size) < MAX_ORDER) {
  4239. table = alloc_pages_exact(size, GFP_ATOMIC);
  4240. kmemleak_alloc(table, size, 1, GFP_ATOMIC);
  4241. }
  4242. }
  4243. } while (!table && size > PAGE_SIZE && --log2qty);
  4244. if (!table)
  4245. panic("Failed to allocate %s hash table\n", tablename);
  4246. printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
  4247. tablename,
  4248. (1U << log2qty),
  4249. ilog2(size) - PAGE_SHIFT,
  4250. size);
  4251. if (_hash_shift)
  4252. *_hash_shift = log2qty;
  4253. if (_hash_mask)
  4254. *_hash_mask = (1 << log2qty) - 1;
  4255. return table;
  4256. }
  4257. /* Return a pointer to the bitmap storing bits affecting a block of pages */
  4258. static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
  4259. unsigned long pfn)
  4260. {
  4261. #ifdef CONFIG_SPARSEMEM
  4262. return __pfn_to_section(pfn)->pageblock_flags;
  4263. #else
  4264. return zone->pageblock_flags;
  4265. #endif /* CONFIG_SPARSEMEM */
  4266. }
  4267. static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
  4268. {
  4269. #ifdef CONFIG_SPARSEMEM
  4270. pfn &= (PAGES_PER_SECTION-1);
  4271. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4272. #else
  4273. pfn = pfn - zone->zone_start_pfn;
  4274. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4275. #endif /* CONFIG_SPARSEMEM */
  4276. }
  4277. /**
  4278. * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
  4279. * @page: The page within the block of interest
  4280. * @start_bitidx: The first bit of interest to retrieve
  4281. * @end_bitidx: The last bit of interest
  4282. * returns pageblock_bits flags
  4283. */
  4284. unsigned long get_pageblock_flags_group(struct page *page,
  4285. int start_bitidx, int end_bitidx)
  4286. {
  4287. struct zone *zone;
  4288. unsigned long *bitmap;
  4289. unsigned long pfn, bitidx;
  4290. unsigned long flags = 0;
  4291. unsigned long value = 1;
  4292. zone = page_zone(page);
  4293. pfn = page_to_pfn(page);
  4294. bitmap = get_pageblock_bitmap(zone, pfn);
  4295. bitidx = pfn_to_bitidx(zone, pfn);
  4296. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4297. if (test_bit(bitidx + start_bitidx, bitmap))
  4298. flags |= value;
  4299. return flags;
  4300. }
  4301. /**
  4302. * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
  4303. * @page: The page within the block of interest
  4304. * @start_bitidx: The first bit of interest
  4305. * @end_bitidx: The last bit of interest
  4306. * @flags: The flags to set
  4307. */
  4308. void set_pageblock_flags_group(struct page *page, unsigned long flags,
  4309. int start_bitidx, int end_bitidx)
  4310. {
  4311. struct zone *zone;
  4312. unsigned long *bitmap;
  4313. unsigned long pfn, bitidx;
  4314. unsigned long value = 1;
  4315. zone = page_zone(page);
  4316. pfn = page_to_pfn(page);
  4317. bitmap = get_pageblock_bitmap(zone, pfn);
  4318. bitidx = pfn_to_bitidx(zone, pfn);
  4319. VM_BUG_ON(pfn < zone->zone_start_pfn);
  4320. VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
  4321. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4322. if (flags & value)
  4323. __set_bit(bitidx + start_bitidx, bitmap);
  4324. else
  4325. __clear_bit(bitidx + start_bitidx, bitmap);
  4326. }
  4327. /*
  4328. * This is designed as sub function...plz see page_isolation.c also.
  4329. * set/clear page block's type to be ISOLATE.
  4330. * page allocater never alloc memory from ISOLATE block.
  4331. */
  4332. int set_migratetype_isolate(struct page *page)
  4333. {
  4334. struct zone *zone;
  4335. struct page *curr_page;
  4336. unsigned long flags, pfn, iter;
  4337. unsigned long immobile = 0;
  4338. struct memory_isolate_notify arg;
  4339. int notifier_ret;
  4340. int ret = -EBUSY;
  4341. int zone_idx;
  4342. zone = page_zone(page);
  4343. zone_idx = zone_idx(zone);
  4344. spin_lock_irqsave(&zone->lock, flags);
  4345. if (get_pageblock_migratetype(page) == MIGRATE_MOVABLE ||
  4346. zone_idx == ZONE_MOVABLE) {
  4347. ret = 0;
  4348. goto out;
  4349. }
  4350. pfn = page_to_pfn(page);
  4351. arg.start_pfn = pfn;
  4352. arg.nr_pages = pageblock_nr_pages;
  4353. arg.pages_found = 0;
  4354. /*
  4355. * It may be possible to isolate a pageblock even if the
  4356. * migratetype is not MIGRATE_MOVABLE. The memory isolation
  4357. * notifier chain is used by balloon drivers to return the
  4358. * number of pages in a range that are held by the balloon
  4359. * driver to shrink memory. If all the pages are accounted for
  4360. * by balloons, are free, or on the LRU, isolation can continue.
  4361. * Later, for example, when memory hotplug notifier runs, these
  4362. * pages reported as "can be isolated" should be isolated(freed)
  4363. * by the balloon driver through the memory notifier chain.
  4364. */
  4365. notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
  4366. notifier_ret = notifier_to_errno(notifier_ret);
  4367. if (notifier_ret || !arg.pages_found)
  4368. goto out;
  4369. for (iter = pfn; iter < (pfn + pageblock_nr_pages); iter++) {
  4370. if (!pfn_valid_within(pfn))
  4371. continue;
  4372. curr_page = pfn_to_page(iter);
  4373. if (!page_count(curr_page) || PageLRU(curr_page))
  4374. continue;
  4375. immobile++;
  4376. }
  4377. if (arg.pages_found == immobile)
  4378. ret = 0;
  4379. out:
  4380. if (!ret) {
  4381. set_pageblock_migratetype(page, MIGRATE_ISOLATE);
  4382. move_freepages_block(zone, page, MIGRATE_ISOLATE);
  4383. }
  4384. spin_unlock_irqrestore(&zone->lock, flags);
  4385. if (!ret)
  4386. drain_all_pages();
  4387. return ret;
  4388. }
  4389. void unset_migratetype_isolate(struct page *page)
  4390. {
  4391. struct zone *zone;
  4392. unsigned long flags;
  4393. zone = page_zone(page);
  4394. spin_lock_irqsave(&zone->lock, flags);
  4395. if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  4396. goto out;
  4397. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  4398. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  4399. out:
  4400. spin_unlock_irqrestore(&zone->lock, flags);
  4401. }
  4402. #ifdef CONFIG_MEMORY_HOTREMOVE
  4403. /*
  4404. * All pages in the range must be isolated before calling this.
  4405. */
  4406. void
  4407. __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  4408. {
  4409. struct page *page;
  4410. struct zone *zone;
  4411. int order, i;
  4412. unsigned long pfn;
  4413. unsigned long flags;
  4414. /* find the first valid pfn */
  4415. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  4416. if (pfn_valid(pfn))
  4417. break;
  4418. if (pfn == end_pfn)
  4419. return;
  4420. zone = page_zone(pfn_to_page(pfn));
  4421. spin_lock_irqsave(&zone->lock, flags);
  4422. pfn = start_pfn;
  4423. while (pfn < end_pfn) {
  4424. if (!pfn_valid(pfn)) {
  4425. pfn++;
  4426. continue;
  4427. }
  4428. page = pfn_to_page(pfn);
  4429. BUG_ON(page_count(page));
  4430. BUG_ON(!PageBuddy(page));
  4431. order = page_order(page);
  4432. #ifdef CONFIG_DEBUG_VM
  4433. printk(KERN_INFO "remove from free list %lx %d %lx\n",
  4434. pfn, 1 << order, end_pfn);
  4435. #endif
  4436. list_del(&page->lru);
  4437. rmv_page_order(page);
  4438. zone->free_area[order].nr_free--;
  4439. __mod_zone_page_state(zone, NR_FREE_PAGES,
  4440. - (1UL << order));
  4441. for (i = 0; i < (1 << order); i++)
  4442. SetPageReserved((page+i));
  4443. pfn += (1 << order);
  4444. }
  4445. spin_unlock_irqrestore(&zone->lock, flags);
  4446. }
  4447. #endif
  4448. #ifdef CONFIG_MEMORY_FAILURE
  4449. bool is_free_buddy_page(struct page *page)
  4450. {
  4451. struct zone *zone = page_zone(page);
  4452. unsigned long pfn = page_to_pfn(page);
  4453. unsigned long flags;
  4454. int order;
  4455. spin_lock_irqsave(&zone->lock, flags);
  4456. for (order = 0; order < MAX_ORDER; order++) {
  4457. struct page *page_head = page - (pfn & ((1 << order) - 1));
  4458. if (PageBuddy(page_head) && page_order(page_head) >= order)
  4459. break;
  4460. }
  4461. spin_unlock_irqrestore(&zone->lock, flags);
  4462. return order < MAX_ORDER;
  4463. }
  4464. #endif