page_alloc.c 184 KB

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