page_alloc.c 158 KB

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