page_alloc.c 184 KB

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