page_alloc.c 156 KB

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