page_alloc.c 184 KB

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