page_alloc.c 183 KB

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