page_alloc.c 173 KB

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