page_alloc.c 149 KB

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