page_alloc.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935
  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/bootmem.h>
  22. #include <linux/compiler.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/suspend.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/slab.h>
  29. #include <linux/notifier.h>
  30. #include <linux/topology.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/cpu.h>
  33. #include <linux/cpuset.h>
  34. #include <linux/memory_hotplug.h>
  35. #include <linux/nodemask.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/mempolicy.h>
  38. #include <linux/stop_machine.h>
  39. #include <linux/sort.h>
  40. #include <linux/pfn.h>
  41. #include <linux/backing-dev.h>
  42. #include <linux/fault-inject.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/div64.h>
  45. #include "internal.h"
  46. /*
  47. * MCD - HACK: Find somewhere to initialize this EARLY, or make this
  48. * initializer cleaner
  49. */
  50. nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
  51. EXPORT_SYMBOL(node_online_map);
  52. nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
  53. EXPORT_SYMBOL(node_possible_map);
  54. unsigned long totalram_pages __read_mostly;
  55. unsigned long totalreserve_pages __read_mostly;
  56. long nr_swap_pages;
  57. int percpu_pagelist_fraction;
  58. static void __free_pages_ok(struct page *page, unsigned int order);
  59. /*
  60. * results with 256, 32 in the lowmem_reserve sysctl:
  61. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  62. * 1G machine -> (16M dma, 784M normal, 224M high)
  63. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  64. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  65. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  66. *
  67. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  68. * don't need any ZONE_NORMAL reservation
  69. */
  70. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
  71. #ifdef CONFIG_ZONE_DMA
  72. 256,
  73. #endif
  74. #ifdef CONFIG_ZONE_DMA32
  75. 256,
  76. #endif
  77. #ifdef CONFIG_HIGHMEM
  78. 32,
  79. #endif
  80. 32,
  81. };
  82. EXPORT_SYMBOL(totalram_pages);
  83. static char * const zone_names[MAX_NR_ZONES] = {
  84. #ifdef CONFIG_ZONE_DMA
  85. "DMA",
  86. #endif
  87. #ifdef CONFIG_ZONE_DMA32
  88. "DMA32",
  89. #endif
  90. "Normal",
  91. #ifdef CONFIG_HIGHMEM
  92. "HighMem",
  93. #endif
  94. "Movable",
  95. };
  96. int min_free_kbytes = 1024;
  97. unsigned long __meminitdata nr_kernel_pages;
  98. unsigned long __meminitdata nr_all_pages;
  99. static unsigned long __meminitdata dma_reserve;
  100. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  101. /*
  102. * MAX_ACTIVE_REGIONS determines the maxmimum number of distinct
  103. * ranges of memory (RAM) that may be registered with add_active_range().
  104. * Ranges passed to add_active_range() will be merged if possible
  105. * so the number of times add_active_range() can be called is
  106. * related to the number of nodes and the number of holes
  107. */
  108. #ifdef CONFIG_MAX_ACTIVE_REGIONS
  109. /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
  110. #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
  111. #else
  112. #if MAX_NUMNODES >= 32
  113. /* If there can be many nodes, allow up to 50 holes per node */
  114. #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
  115. #else
  116. /* By default, allow up to 256 distinct regions */
  117. #define MAX_ACTIVE_REGIONS 256
  118. #endif
  119. #endif
  120. static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
  121. static int __meminitdata nr_nodemap_entries;
  122. static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
  123. static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
  124. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  125. static unsigned long __meminitdata node_boundary_start_pfn[MAX_NUMNODES];
  126. static unsigned long __meminitdata node_boundary_end_pfn[MAX_NUMNODES];
  127. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  128. unsigned long __initdata required_kernelcore;
  129. unsigned long __initdata required_movablecore;
  130. unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
  131. /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
  132. int movable_zone;
  133. EXPORT_SYMBOL(movable_zone);
  134. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  135. #if MAX_NUMNODES > 1
  136. int nr_node_ids __read_mostly = MAX_NUMNODES;
  137. EXPORT_SYMBOL(nr_node_ids);
  138. #endif
  139. #ifdef CONFIG_DEBUG_VM
  140. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  141. {
  142. int ret = 0;
  143. unsigned seq;
  144. unsigned long pfn = page_to_pfn(page);
  145. do {
  146. seq = zone_span_seqbegin(zone);
  147. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  148. ret = 1;
  149. else if (pfn < zone->zone_start_pfn)
  150. ret = 1;
  151. } while (zone_span_seqretry(zone, seq));
  152. return ret;
  153. }
  154. static int page_is_consistent(struct zone *zone, struct page *page)
  155. {
  156. if (!pfn_valid_within(page_to_pfn(page)))
  157. return 0;
  158. if (zone != page_zone(page))
  159. return 0;
  160. return 1;
  161. }
  162. /*
  163. * Temporary debugging check for pages not lying within a given zone.
  164. */
  165. static int bad_range(struct zone *zone, struct page *page)
  166. {
  167. if (page_outside_zone_boundaries(zone, page))
  168. return 1;
  169. if (!page_is_consistent(zone, page))
  170. return 1;
  171. return 0;
  172. }
  173. #else
  174. static inline int bad_range(struct zone *zone, struct page *page)
  175. {
  176. return 0;
  177. }
  178. #endif
  179. static void bad_page(struct page *page)
  180. {
  181. printk(KERN_EMERG "Bad page state in process '%s'\n"
  182. KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
  183. KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
  184. KERN_EMERG "Backtrace:\n",
  185. current->comm, page, (int)(2*sizeof(unsigned long)),
  186. (unsigned long)page->flags, page->mapping,
  187. page_mapcount(page), page_count(page));
  188. dump_stack();
  189. page->flags &= ~(1 << PG_lru |
  190. 1 << PG_private |
  191. 1 << PG_locked |
  192. 1 << PG_active |
  193. 1 << PG_dirty |
  194. 1 << PG_reclaim |
  195. 1 << PG_slab |
  196. 1 << PG_swapcache |
  197. 1 << PG_writeback |
  198. 1 << PG_buddy );
  199. set_page_count(page, 0);
  200. reset_page_mapcount(page);
  201. page->mapping = NULL;
  202. add_taint(TAINT_BAD_PAGE);
  203. }
  204. /*
  205. * Higher-order pages are called "compound pages". They are structured thusly:
  206. *
  207. * The first PAGE_SIZE page is called the "head page".
  208. *
  209. * The remaining PAGE_SIZE pages are called "tail pages".
  210. *
  211. * All pages have PG_compound set. All pages have their ->private pointing at
  212. * the head page (even the head page has this).
  213. *
  214. * The first tail page's ->lru.next holds the address of the compound page's
  215. * put_page() function. Its ->lru.prev holds the order of allocation.
  216. * This usage means that zero-order pages may not be compound.
  217. */
  218. static void free_compound_page(struct page *page)
  219. {
  220. __free_pages_ok(page, compound_order(page));
  221. }
  222. static void prep_compound_page(struct page *page, unsigned long order)
  223. {
  224. int i;
  225. int nr_pages = 1 << order;
  226. set_compound_page_dtor(page, free_compound_page);
  227. set_compound_order(page, order);
  228. __SetPageHead(page);
  229. for (i = 1; i < nr_pages; i++) {
  230. struct page *p = page + i;
  231. __SetPageTail(p);
  232. p->first_page = page;
  233. }
  234. }
  235. static void destroy_compound_page(struct page *page, unsigned long order)
  236. {
  237. int i;
  238. int nr_pages = 1 << order;
  239. if (unlikely(compound_order(page) != order))
  240. bad_page(page);
  241. if (unlikely(!PageHead(page)))
  242. bad_page(page);
  243. __ClearPageHead(page);
  244. for (i = 1; i < nr_pages; i++) {
  245. struct page *p = page + i;
  246. if (unlikely(!PageTail(p) |
  247. (p->first_page != page)))
  248. bad_page(page);
  249. __ClearPageTail(p);
  250. }
  251. }
  252. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  253. {
  254. int i;
  255. VM_BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
  256. /*
  257. * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
  258. * and __GFP_HIGHMEM from hard or soft interrupt context.
  259. */
  260. VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
  261. for (i = 0; i < (1 << order); i++)
  262. clear_highpage(page + i);
  263. }
  264. /*
  265. * function for dealing with page's order in buddy system.
  266. * zone->lock is already acquired when we use these.
  267. * So, we don't need atomic page->flags operations here.
  268. */
  269. static inline unsigned long page_order(struct page *page)
  270. {
  271. return page_private(page);
  272. }
  273. static inline void set_page_order(struct page *page, int order)
  274. {
  275. set_page_private(page, order);
  276. __SetPageBuddy(page);
  277. }
  278. static inline void rmv_page_order(struct page *page)
  279. {
  280. __ClearPageBuddy(page);
  281. set_page_private(page, 0);
  282. }
  283. /*
  284. * Locate the struct page for both the matching buddy in our
  285. * pair (buddy1) and the combined O(n+1) page they form (page).
  286. *
  287. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  288. * the following equation:
  289. * B2 = B1 ^ (1 << O)
  290. * For example, if the starting buddy (buddy2) is #8 its order
  291. * 1 buddy is #10:
  292. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  293. *
  294. * 2) Any buddy B will have an order O+1 parent P which
  295. * satisfies the following equation:
  296. * P = B & ~(1 << O)
  297. *
  298. * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
  299. */
  300. static inline struct page *
  301. __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
  302. {
  303. unsigned long buddy_idx = page_idx ^ (1 << order);
  304. return page + (buddy_idx - page_idx);
  305. }
  306. static inline unsigned long
  307. __find_combined_index(unsigned long page_idx, unsigned int order)
  308. {
  309. return (page_idx & ~(1 << order));
  310. }
  311. /*
  312. * This function checks whether a page is free && is the buddy
  313. * we can do coalesce a page and its buddy if
  314. * (a) the buddy is not in a hole &&
  315. * (b) the buddy is in the buddy system &&
  316. * (c) a page and its buddy have the same order &&
  317. * (d) a page and its buddy are in the same zone.
  318. *
  319. * For recording whether a page is in the buddy system, we use PG_buddy.
  320. * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
  321. *
  322. * For recording page's order, we use page_private(page).
  323. */
  324. static inline int page_is_buddy(struct page *page, struct page *buddy,
  325. int order)
  326. {
  327. if (!pfn_valid_within(page_to_pfn(buddy)))
  328. return 0;
  329. if (page_zone_id(page) != page_zone_id(buddy))
  330. return 0;
  331. if (PageBuddy(buddy) && page_order(buddy) == order) {
  332. BUG_ON(page_count(buddy) != 0);
  333. return 1;
  334. }
  335. return 0;
  336. }
  337. /*
  338. * Freeing function for a buddy system allocator.
  339. *
  340. * The concept of a buddy system is to maintain direct-mapped table
  341. * (containing bit values) for memory blocks of various "orders".
  342. * The bottom level table contains the map for the smallest allocatable
  343. * units of memory (here, pages), and each level above it describes
  344. * pairs of units from the levels below, hence, "buddies".
  345. * At a high level, all that happens here is marking the table entry
  346. * at the bottom level available, and propagating the changes upward
  347. * as necessary, plus some accounting needed to play nicely with other
  348. * parts of the VM system.
  349. * At each level, we keep a list of pages, which are heads of continuous
  350. * free pages of length of (1 << order) and marked with PG_buddy. Page's
  351. * order is recorded in page_private(page) field.
  352. * So when we are allocating or freeing one, we can derive the state of the
  353. * other. That is, if we allocate a small block, and both were
  354. * free, the remainder of the region must be split into blocks.
  355. * If a block is freed, and its buddy is also free, then this
  356. * triggers coalescing into a block of larger size.
  357. *
  358. * -- wli
  359. */
  360. static inline void __free_one_page(struct page *page,
  361. struct zone *zone, unsigned int order)
  362. {
  363. unsigned long page_idx;
  364. int order_size = 1 << order;
  365. if (unlikely(PageCompound(page)))
  366. destroy_compound_page(page, order);
  367. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  368. VM_BUG_ON(page_idx & (order_size - 1));
  369. VM_BUG_ON(bad_range(zone, page));
  370. __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
  371. while (order < MAX_ORDER-1) {
  372. unsigned long combined_idx;
  373. struct free_area *area;
  374. struct page *buddy;
  375. buddy = __page_find_buddy(page, page_idx, order);
  376. if (!page_is_buddy(page, buddy, order))
  377. break; /* Move the buddy up one level. */
  378. list_del(&buddy->lru);
  379. area = zone->free_area + order;
  380. area->nr_free--;
  381. rmv_page_order(buddy);
  382. combined_idx = __find_combined_index(page_idx, order);
  383. page = page + (combined_idx - page_idx);
  384. page_idx = combined_idx;
  385. order++;
  386. }
  387. set_page_order(page, order);
  388. list_add(&page->lru, &zone->free_area[order].free_list);
  389. zone->free_area[order].nr_free++;
  390. }
  391. static inline int free_pages_check(struct page *page)
  392. {
  393. if (unlikely(page_mapcount(page) |
  394. (page->mapping != NULL) |
  395. (page_count(page) != 0) |
  396. (page->flags & (
  397. 1 << PG_lru |
  398. 1 << PG_private |
  399. 1 << PG_locked |
  400. 1 << PG_active |
  401. 1 << PG_slab |
  402. 1 << PG_swapcache |
  403. 1 << PG_writeback |
  404. 1 << PG_reserved |
  405. 1 << PG_buddy ))))
  406. bad_page(page);
  407. if (PageDirty(page))
  408. __ClearPageDirty(page);
  409. /*
  410. * For now, we report if PG_reserved was found set, but do not
  411. * clear it, and do not free the page. But we shall soon need
  412. * to do more, for when the ZERO_PAGE count wraps negative.
  413. */
  414. return PageReserved(page);
  415. }
  416. /*
  417. * Frees a list of pages.
  418. * Assumes all pages on list are in same zone, and of same order.
  419. * count is the number of pages to free.
  420. *
  421. * If the zone was previously in an "all pages pinned" state then look to
  422. * see if this freeing clears that state.
  423. *
  424. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  425. * pinned" detection logic.
  426. */
  427. static void free_pages_bulk(struct zone *zone, int count,
  428. struct list_head *list, int order)
  429. {
  430. spin_lock(&zone->lock);
  431. zone->all_unreclaimable = 0;
  432. zone->pages_scanned = 0;
  433. while (count--) {
  434. struct page *page;
  435. VM_BUG_ON(list_empty(list));
  436. page = list_entry(list->prev, struct page, lru);
  437. /* have to delete it as __free_one_page list manipulates */
  438. list_del(&page->lru);
  439. __free_one_page(page, zone, order);
  440. }
  441. spin_unlock(&zone->lock);
  442. }
  443. static void free_one_page(struct zone *zone, struct page *page, int order)
  444. {
  445. spin_lock(&zone->lock);
  446. zone->all_unreclaimable = 0;
  447. zone->pages_scanned = 0;
  448. __free_one_page(page, zone, order);
  449. spin_unlock(&zone->lock);
  450. }
  451. static void __free_pages_ok(struct page *page, unsigned int order)
  452. {
  453. unsigned long flags;
  454. int i;
  455. int reserved = 0;
  456. for (i = 0 ; i < (1 << order) ; ++i)
  457. reserved += free_pages_check(page + i);
  458. if (reserved)
  459. return;
  460. if (!PageHighMem(page))
  461. debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
  462. arch_free_page(page, order);
  463. kernel_map_pages(page, 1 << order, 0);
  464. local_irq_save(flags);
  465. __count_vm_events(PGFREE, 1 << order);
  466. free_one_page(page_zone(page), page, order);
  467. local_irq_restore(flags);
  468. }
  469. /*
  470. * permit the bootmem allocator to evade page validation on high-order frees
  471. */
  472. void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
  473. {
  474. if (order == 0) {
  475. __ClearPageReserved(page);
  476. set_page_count(page, 0);
  477. set_page_refcounted(page);
  478. __free_page(page);
  479. } else {
  480. int loop;
  481. prefetchw(page);
  482. for (loop = 0; loop < BITS_PER_LONG; loop++) {
  483. struct page *p = &page[loop];
  484. if (loop + 1 < BITS_PER_LONG)
  485. prefetchw(p + 1);
  486. __ClearPageReserved(p);
  487. set_page_count(p, 0);
  488. }
  489. set_page_refcounted(page);
  490. __free_pages(page, order);
  491. }
  492. }
  493. /*
  494. * The order of subdivision here is critical for the IO subsystem.
  495. * Please do not alter this order without good reasons and regression
  496. * testing. Specifically, as large blocks of memory are subdivided,
  497. * the order in which smaller blocks are delivered depends on the order
  498. * they're subdivided in this function. This is the primary factor
  499. * influencing the order in which pages are delivered to the IO
  500. * subsystem according to empirical testing, and this is also justified
  501. * by considering the behavior of a buddy system containing a single
  502. * large block of memory acted on by a series of small allocations.
  503. * This behavior is a critical factor in sglist merging's success.
  504. *
  505. * -- wli
  506. */
  507. static inline void expand(struct zone *zone, struct page *page,
  508. int low, int high, struct free_area *area)
  509. {
  510. unsigned long size = 1 << high;
  511. while (high > low) {
  512. area--;
  513. high--;
  514. size >>= 1;
  515. VM_BUG_ON(bad_range(zone, &page[size]));
  516. list_add(&page[size].lru, &area->free_list);
  517. area->nr_free++;
  518. set_page_order(&page[size], high);
  519. }
  520. }
  521. /*
  522. * This page is about to be returned from the page allocator
  523. */
  524. static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
  525. {
  526. if (unlikely(page_mapcount(page) |
  527. (page->mapping != NULL) |
  528. (page_count(page) != 0) |
  529. (page->flags & (
  530. 1 << PG_lru |
  531. 1 << PG_private |
  532. 1 << PG_locked |
  533. 1 << PG_active |
  534. 1 << PG_dirty |
  535. 1 << PG_slab |
  536. 1 << PG_swapcache |
  537. 1 << PG_writeback |
  538. 1 << PG_reserved |
  539. 1 << PG_buddy ))))
  540. bad_page(page);
  541. /*
  542. * For now, we report if PG_reserved was found set, but do not
  543. * clear it, and do not allocate the page: as a safety net.
  544. */
  545. if (PageReserved(page))
  546. return 1;
  547. page->flags &= ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_readahead |
  548. 1 << PG_referenced | 1 << PG_arch_1 |
  549. 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk);
  550. set_page_private(page, 0);
  551. set_page_refcounted(page);
  552. arch_alloc_page(page, order);
  553. kernel_map_pages(page, 1 << order, 1);
  554. if (gfp_flags & __GFP_ZERO)
  555. prep_zero_page(page, order, gfp_flags);
  556. if (order && (gfp_flags & __GFP_COMP))
  557. prep_compound_page(page, order);
  558. return 0;
  559. }
  560. /*
  561. * Do the hard work of removing an element from the buddy allocator.
  562. * Call me with the zone->lock already held.
  563. */
  564. static struct page *__rmqueue(struct zone *zone, unsigned int order)
  565. {
  566. struct free_area * area;
  567. unsigned int current_order;
  568. struct page *page;
  569. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  570. area = zone->free_area + current_order;
  571. if (list_empty(&area->free_list))
  572. continue;
  573. page = list_entry(area->free_list.next, struct page, lru);
  574. list_del(&page->lru);
  575. rmv_page_order(page);
  576. area->nr_free--;
  577. __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
  578. expand(zone, page, order, current_order, area);
  579. return page;
  580. }
  581. return NULL;
  582. }
  583. /*
  584. * Obtain a specified number of elements from the buddy allocator, all under
  585. * a single hold of the lock, for efficiency. Add them to the supplied list.
  586. * Returns the number of new pages which were placed at *list.
  587. */
  588. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  589. unsigned long count, struct list_head *list)
  590. {
  591. int i;
  592. spin_lock(&zone->lock);
  593. for (i = 0; i < count; ++i) {
  594. struct page *page = __rmqueue(zone, order);
  595. if (unlikely(page == NULL))
  596. break;
  597. list_add_tail(&page->lru, list);
  598. }
  599. spin_unlock(&zone->lock);
  600. return i;
  601. }
  602. #ifdef CONFIG_NUMA
  603. /*
  604. * Called from the vmstat counter updater to drain pagesets of this
  605. * currently executing processor on remote nodes after they have
  606. * expired.
  607. *
  608. * Note that this function must be called with the thread pinned to
  609. * a single processor.
  610. */
  611. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  612. {
  613. unsigned long flags;
  614. int to_drain;
  615. local_irq_save(flags);
  616. if (pcp->count >= pcp->batch)
  617. to_drain = pcp->batch;
  618. else
  619. to_drain = pcp->count;
  620. free_pages_bulk(zone, to_drain, &pcp->list, 0);
  621. pcp->count -= to_drain;
  622. local_irq_restore(flags);
  623. }
  624. #endif
  625. static void __drain_pages(unsigned int cpu)
  626. {
  627. unsigned long flags;
  628. struct zone *zone;
  629. int i;
  630. for_each_zone(zone) {
  631. struct per_cpu_pageset *pset;
  632. if (!populated_zone(zone))
  633. continue;
  634. pset = zone_pcp(zone, cpu);
  635. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  636. struct per_cpu_pages *pcp;
  637. pcp = &pset->pcp[i];
  638. local_irq_save(flags);
  639. free_pages_bulk(zone, pcp->count, &pcp->list, 0);
  640. pcp->count = 0;
  641. local_irq_restore(flags);
  642. }
  643. }
  644. }
  645. #ifdef CONFIG_HIBERNATION
  646. void mark_free_pages(struct zone *zone)
  647. {
  648. unsigned long pfn, max_zone_pfn;
  649. unsigned long flags;
  650. int order;
  651. struct list_head *curr;
  652. if (!zone->spanned_pages)
  653. return;
  654. spin_lock_irqsave(&zone->lock, flags);
  655. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  656. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  657. if (pfn_valid(pfn)) {
  658. struct page *page = pfn_to_page(pfn);
  659. if (!swsusp_page_is_forbidden(page))
  660. swsusp_unset_page_free(page);
  661. }
  662. for (order = MAX_ORDER - 1; order >= 0; --order)
  663. list_for_each(curr, &zone->free_area[order].free_list) {
  664. unsigned long i;
  665. pfn = page_to_pfn(list_entry(curr, struct page, lru));
  666. for (i = 0; i < (1UL << order); i++)
  667. swsusp_set_page_free(pfn_to_page(pfn + i));
  668. }
  669. spin_unlock_irqrestore(&zone->lock, flags);
  670. }
  671. /*
  672. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  673. */
  674. void drain_local_pages(void)
  675. {
  676. unsigned long flags;
  677. local_irq_save(flags);
  678. __drain_pages(smp_processor_id());
  679. local_irq_restore(flags);
  680. }
  681. #endif /* CONFIG_HIBERNATION */
  682. /*
  683. * Free a 0-order page
  684. */
  685. static void fastcall free_hot_cold_page(struct page *page, int cold)
  686. {
  687. struct zone *zone = page_zone(page);
  688. struct per_cpu_pages *pcp;
  689. unsigned long flags;
  690. if (PageAnon(page))
  691. page->mapping = NULL;
  692. if (free_pages_check(page))
  693. return;
  694. if (!PageHighMem(page))
  695. debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
  696. arch_free_page(page, 0);
  697. kernel_map_pages(page, 1, 0);
  698. pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
  699. local_irq_save(flags);
  700. __count_vm_event(PGFREE);
  701. list_add(&page->lru, &pcp->list);
  702. pcp->count++;
  703. if (pcp->count >= pcp->high) {
  704. free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
  705. pcp->count -= pcp->batch;
  706. }
  707. local_irq_restore(flags);
  708. put_cpu();
  709. }
  710. void fastcall free_hot_page(struct page *page)
  711. {
  712. free_hot_cold_page(page, 0);
  713. }
  714. void fastcall free_cold_page(struct page *page)
  715. {
  716. free_hot_cold_page(page, 1);
  717. }
  718. /*
  719. * split_page takes a non-compound higher-order page, and splits it into
  720. * n (1<<order) sub-pages: page[0..n]
  721. * Each sub-page must be freed individually.
  722. *
  723. * Note: this is probably too low level an operation for use in drivers.
  724. * Please consult with lkml before using this in your driver.
  725. */
  726. void split_page(struct page *page, unsigned int order)
  727. {
  728. int i;
  729. VM_BUG_ON(PageCompound(page));
  730. VM_BUG_ON(!page_count(page));
  731. for (i = 1; i < (1 << order); i++)
  732. set_page_refcounted(page + i);
  733. }
  734. /*
  735. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  736. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  737. * or two.
  738. */
  739. static struct page *buffered_rmqueue(struct zonelist *zonelist,
  740. struct zone *zone, int order, gfp_t gfp_flags)
  741. {
  742. unsigned long flags;
  743. struct page *page;
  744. int cold = !!(gfp_flags & __GFP_COLD);
  745. int cpu;
  746. again:
  747. cpu = get_cpu();
  748. if (likely(order == 0)) {
  749. struct per_cpu_pages *pcp;
  750. pcp = &zone_pcp(zone, cpu)->pcp[cold];
  751. local_irq_save(flags);
  752. if (!pcp->count) {
  753. pcp->count = rmqueue_bulk(zone, 0,
  754. pcp->batch, &pcp->list);
  755. if (unlikely(!pcp->count))
  756. goto failed;
  757. }
  758. page = list_entry(pcp->list.next, struct page, lru);
  759. list_del(&page->lru);
  760. pcp->count--;
  761. } else {
  762. spin_lock_irqsave(&zone->lock, flags);
  763. page = __rmqueue(zone, order);
  764. spin_unlock(&zone->lock);
  765. if (!page)
  766. goto failed;
  767. }
  768. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  769. zone_statistics(zonelist, zone);
  770. local_irq_restore(flags);
  771. put_cpu();
  772. VM_BUG_ON(bad_range(zone, page));
  773. if (prep_new_page(page, order, gfp_flags))
  774. goto again;
  775. return page;
  776. failed:
  777. local_irq_restore(flags);
  778. put_cpu();
  779. return NULL;
  780. }
  781. #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
  782. #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
  783. #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
  784. #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
  785. #define ALLOC_HARDER 0x10 /* try to alloc harder */
  786. #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
  787. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  788. #ifdef CONFIG_FAIL_PAGE_ALLOC
  789. static struct fail_page_alloc_attr {
  790. struct fault_attr attr;
  791. u32 ignore_gfp_highmem;
  792. u32 ignore_gfp_wait;
  793. u32 min_order;
  794. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  795. struct dentry *ignore_gfp_highmem_file;
  796. struct dentry *ignore_gfp_wait_file;
  797. struct dentry *min_order_file;
  798. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  799. } fail_page_alloc = {
  800. .attr = FAULT_ATTR_INITIALIZER,
  801. .ignore_gfp_wait = 1,
  802. .ignore_gfp_highmem = 1,
  803. .min_order = 1,
  804. };
  805. static int __init setup_fail_page_alloc(char *str)
  806. {
  807. return setup_fault_attr(&fail_page_alloc.attr, str);
  808. }
  809. __setup("fail_page_alloc=", setup_fail_page_alloc);
  810. static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  811. {
  812. if (order < fail_page_alloc.min_order)
  813. return 0;
  814. if (gfp_mask & __GFP_NOFAIL)
  815. return 0;
  816. if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
  817. return 0;
  818. if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
  819. return 0;
  820. return should_fail(&fail_page_alloc.attr, 1 << order);
  821. }
  822. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  823. static int __init fail_page_alloc_debugfs(void)
  824. {
  825. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  826. struct dentry *dir;
  827. int err;
  828. err = init_fault_attr_dentries(&fail_page_alloc.attr,
  829. "fail_page_alloc");
  830. if (err)
  831. return err;
  832. dir = fail_page_alloc.attr.dentries.dir;
  833. fail_page_alloc.ignore_gfp_wait_file =
  834. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  835. &fail_page_alloc.ignore_gfp_wait);
  836. fail_page_alloc.ignore_gfp_highmem_file =
  837. debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  838. &fail_page_alloc.ignore_gfp_highmem);
  839. fail_page_alloc.min_order_file =
  840. debugfs_create_u32("min-order", mode, dir,
  841. &fail_page_alloc.min_order);
  842. if (!fail_page_alloc.ignore_gfp_wait_file ||
  843. !fail_page_alloc.ignore_gfp_highmem_file ||
  844. !fail_page_alloc.min_order_file) {
  845. err = -ENOMEM;
  846. debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
  847. debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
  848. debugfs_remove(fail_page_alloc.min_order_file);
  849. cleanup_fault_attr_dentries(&fail_page_alloc.attr);
  850. }
  851. return err;
  852. }
  853. late_initcall(fail_page_alloc_debugfs);
  854. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  855. #else /* CONFIG_FAIL_PAGE_ALLOC */
  856. static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  857. {
  858. return 0;
  859. }
  860. #endif /* CONFIG_FAIL_PAGE_ALLOC */
  861. /*
  862. * Return 1 if free pages are above 'mark'. This takes into account the order
  863. * of the allocation.
  864. */
  865. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  866. int classzone_idx, int alloc_flags)
  867. {
  868. /* free_pages my go negative - that's OK */
  869. long min = mark;
  870. long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
  871. int o;
  872. if (alloc_flags & ALLOC_HIGH)
  873. min -= min / 2;
  874. if (alloc_flags & ALLOC_HARDER)
  875. min -= min / 4;
  876. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  877. return 0;
  878. for (o = 0; o < order; o++) {
  879. /* At the next order, this order's pages become unavailable */
  880. free_pages -= z->free_area[o].nr_free << o;
  881. /* Require fewer higher order pages to be free */
  882. min >>= 1;
  883. if (free_pages <= min)
  884. return 0;
  885. }
  886. return 1;
  887. }
  888. #ifdef CONFIG_NUMA
  889. /*
  890. * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
  891. * skip over zones that are not allowed by the cpuset, or that have
  892. * been recently (in last second) found to be nearly full. See further
  893. * comments in mmzone.h. Reduces cache footprint of zonelist scans
  894. * that have to skip over alot of full or unallowed zones.
  895. *
  896. * If the zonelist cache is present in the passed in zonelist, then
  897. * returns a pointer to the allowed node mask (either the current
  898. * tasks mems_allowed, or node_online_map.)
  899. *
  900. * If the zonelist cache is not available for this zonelist, does
  901. * nothing and returns NULL.
  902. *
  903. * If the fullzones BITMAP in the zonelist cache is stale (more than
  904. * a second since last zap'd) then we zap it out (clear its bits.)
  905. *
  906. * We hold off even calling zlc_setup, until after we've checked the
  907. * first zone in the zonelist, on the theory that most allocations will
  908. * be satisfied from that first zone, so best to examine that zone as
  909. * quickly as we can.
  910. */
  911. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  912. {
  913. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  914. nodemask_t *allowednodes; /* zonelist_cache approximation */
  915. zlc = zonelist->zlcache_ptr;
  916. if (!zlc)
  917. return NULL;
  918. if (jiffies - zlc->last_full_zap > 1 * HZ) {
  919. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  920. zlc->last_full_zap = jiffies;
  921. }
  922. allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
  923. &cpuset_current_mems_allowed :
  924. &node_online_map;
  925. return allowednodes;
  926. }
  927. /*
  928. * Given 'z' scanning a zonelist, run a couple of quick checks to see
  929. * if it is worth looking at further for free memory:
  930. * 1) Check that the zone isn't thought to be full (doesn't have its
  931. * bit set in the zonelist_cache fullzones BITMAP).
  932. * 2) Check that the zones node (obtained from the zonelist_cache
  933. * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
  934. * Return true (non-zero) if zone is worth looking at further, or
  935. * else return false (zero) if it is not.
  936. *
  937. * This check -ignores- the distinction between various watermarks,
  938. * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
  939. * found to be full for any variation of these watermarks, it will
  940. * be considered full for up to one second by all requests, unless
  941. * we are so low on memory on all allowed nodes that we are forced
  942. * into the second scan of the zonelist.
  943. *
  944. * In the second scan we ignore this zonelist cache and exactly
  945. * apply the watermarks to all zones, even it is slower to do so.
  946. * We are low on memory in the second scan, and should leave no stone
  947. * unturned looking for a free page.
  948. */
  949. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
  950. nodemask_t *allowednodes)
  951. {
  952. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  953. int i; /* index of *z in zonelist zones */
  954. int n; /* node that zone *z is on */
  955. zlc = zonelist->zlcache_ptr;
  956. if (!zlc)
  957. return 1;
  958. i = z - zonelist->zones;
  959. n = zlc->z_to_n[i];
  960. /* This zone is worth trying if it is allowed but not full */
  961. return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
  962. }
  963. /*
  964. * Given 'z' scanning a zonelist, set the corresponding bit in
  965. * zlc->fullzones, so that subsequent attempts to allocate a page
  966. * from that zone don't waste time re-examining it.
  967. */
  968. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
  969. {
  970. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  971. int i; /* index of *z in zonelist zones */
  972. zlc = zonelist->zlcache_ptr;
  973. if (!zlc)
  974. return;
  975. i = z - zonelist->zones;
  976. set_bit(i, zlc->fullzones);
  977. }
  978. #else /* CONFIG_NUMA */
  979. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  980. {
  981. return NULL;
  982. }
  983. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
  984. nodemask_t *allowednodes)
  985. {
  986. return 1;
  987. }
  988. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
  989. {
  990. }
  991. #endif /* CONFIG_NUMA */
  992. /*
  993. * get_page_from_freelist goes through the zonelist trying to allocate
  994. * a page.
  995. */
  996. static struct page *
  997. get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
  998. struct zonelist *zonelist, int alloc_flags)
  999. {
  1000. struct zone **z;
  1001. struct page *page = NULL;
  1002. int classzone_idx = zone_idx(zonelist->zones[0]);
  1003. struct zone *zone;
  1004. nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
  1005. int zlc_active = 0; /* set if using zonelist_cache */
  1006. int did_zlc_setup = 0; /* just call zlc_setup() one time */
  1007. enum zone_type highest_zoneidx = -1; /* Gets set for policy zonelists */
  1008. zonelist_scan:
  1009. /*
  1010. * Scan zonelist, looking for a zone with enough free.
  1011. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1012. */
  1013. z = zonelist->zones;
  1014. do {
  1015. /*
  1016. * In NUMA, this could be a policy zonelist which contains
  1017. * zones that may not be allowed by the current gfp_mask.
  1018. * Check the zone is allowed by the current flags
  1019. */
  1020. if (unlikely(alloc_should_filter_zonelist(zonelist))) {
  1021. if (highest_zoneidx == -1)
  1022. highest_zoneidx = gfp_zone(gfp_mask);
  1023. if (zone_idx(*z) > highest_zoneidx)
  1024. continue;
  1025. }
  1026. if (NUMA_BUILD && zlc_active &&
  1027. !zlc_zone_worth_trying(zonelist, z, allowednodes))
  1028. continue;
  1029. zone = *z;
  1030. if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
  1031. zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
  1032. break;
  1033. if ((alloc_flags & ALLOC_CPUSET) &&
  1034. !cpuset_zone_allowed_softwall(zone, gfp_mask))
  1035. goto try_next_zone;
  1036. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  1037. unsigned long mark;
  1038. if (alloc_flags & ALLOC_WMARK_MIN)
  1039. mark = zone->pages_min;
  1040. else if (alloc_flags & ALLOC_WMARK_LOW)
  1041. mark = zone->pages_low;
  1042. else
  1043. mark = zone->pages_high;
  1044. if (!zone_watermark_ok(zone, order, mark,
  1045. classzone_idx, alloc_flags)) {
  1046. if (!zone_reclaim_mode ||
  1047. !zone_reclaim(zone, gfp_mask, order))
  1048. goto this_zone_full;
  1049. }
  1050. }
  1051. page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
  1052. if (page)
  1053. break;
  1054. this_zone_full:
  1055. if (NUMA_BUILD)
  1056. zlc_mark_zone_full(zonelist, z);
  1057. try_next_zone:
  1058. if (NUMA_BUILD && !did_zlc_setup) {
  1059. /* we do zlc_setup after the first zone is tried */
  1060. allowednodes = zlc_setup(zonelist, alloc_flags);
  1061. zlc_active = 1;
  1062. did_zlc_setup = 1;
  1063. }
  1064. } while (*(++z) != NULL);
  1065. if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
  1066. /* Disable zlc cache for second zonelist scan */
  1067. zlc_active = 0;
  1068. goto zonelist_scan;
  1069. }
  1070. return page;
  1071. }
  1072. /*
  1073. * This is the 'heart' of the zoned buddy allocator.
  1074. */
  1075. struct page * fastcall
  1076. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  1077. struct zonelist *zonelist)
  1078. {
  1079. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1080. struct zone **z;
  1081. struct page *page;
  1082. struct reclaim_state reclaim_state;
  1083. struct task_struct *p = current;
  1084. int do_retry;
  1085. int alloc_flags;
  1086. int did_some_progress;
  1087. might_sleep_if(wait);
  1088. if (should_fail_alloc_page(gfp_mask, order))
  1089. return NULL;
  1090. restart:
  1091. z = zonelist->zones; /* the list of zones suitable for gfp_mask */
  1092. if (unlikely(*z == NULL)) {
  1093. /* Should this ever happen?? */
  1094. return NULL;
  1095. }
  1096. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  1097. zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
  1098. if (page)
  1099. goto got_pg;
  1100. /*
  1101. * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
  1102. * __GFP_NOWARN set) should not cause reclaim since the subsystem
  1103. * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
  1104. * using a larger set of nodes after it has established that the
  1105. * allowed per node queues are empty and that nodes are
  1106. * over allocated.
  1107. */
  1108. if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
  1109. goto nopage;
  1110. for (z = zonelist->zones; *z; z++)
  1111. wakeup_kswapd(*z, order);
  1112. /*
  1113. * OK, we're below the kswapd watermark and have kicked background
  1114. * reclaim. Now things get more complex, so set up alloc_flags according
  1115. * to how we want to proceed.
  1116. *
  1117. * The caller may dip into page reserves a bit more if the caller
  1118. * cannot run direct reclaim, or if the caller has realtime scheduling
  1119. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  1120. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  1121. */
  1122. alloc_flags = ALLOC_WMARK_MIN;
  1123. if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
  1124. alloc_flags |= ALLOC_HARDER;
  1125. if (gfp_mask & __GFP_HIGH)
  1126. alloc_flags |= ALLOC_HIGH;
  1127. if (wait)
  1128. alloc_flags |= ALLOC_CPUSET;
  1129. /*
  1130. * Go through the zonelist again. Let __GFP_HIGH and allocations
  1131. * coming from realtime tasks go deeper into reserves.
  1132. *
  1133. * This is the last chance, in general, before the goto nopage.
  1134. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  1135. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1136. */
  1137. page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
  1138. if (page)
  1139. goto got_pg;
  1140. /* This allocation should allow future memory freeing. */
  1141. rebalance:
  1142. if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
  1143. && !in_interrupt()) {
  1144. if (!(gfp_mask & __GFP_NOMEMALLOC)) {
  1145. nofail_alloc:
  1146. /* go through the zonelist yet again, ignoring mins */
  1147. page = get_page_from_freelist(gfp_mask, order,
  1148. zonelist, ALLOC_NO_WATERMARKS);
  1149. if (page)
  1150. goto got_pg;
  1151. if (gfp_mask & __GFP_NOFAIL) {
  1152. congestion_wait(WRITE, HZ/50);
  1153. goto nofail_alloc;
  1154. }
  1155. }
  1156. goto nopage;
  1157. }
  1158. /* Atomic allocations - we can't balance anything */
  1159. if (!wait)
  1160. goto nopage;
  1161. cond_resched();
  1162. /* We now go into synchronous reclaim */
  1163. cpuset_memory_pressure_bump();
  1164. p->flags |= PF_MEMALLOC;
  1165. reclaim_state.reclaimed_slab = 0;
  1166. p->reclaim_state = &reclaim_state;
  1167. did_some_progress = try_to_free_pages(zonelist->zones, order, gfp_mask);
  1168. p->reclaim_state = NULL;
  1169. p->flags &= ~PF_MEMALLOC;
  1170. cond_resched();
  1171. if (likely(did_some_progress)) {
  1172. page = get_page_from_freelist(gfp_mask, order,
  1173. zonelist, alloc_flags);
  1174. if (page)
  1175. goto got_pg;
  1176. } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  1177. /*
  1178. * Go through the zonelist yet one more time, keep
  1179. * very high watermark here, this is only to catch
  1180. * a parallel oom killing, we must fail if we're still
  1181. * under heavy pressure.
  1182. */
  1183. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  1184. zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
  1185. if (page)
  1186. goto got_pg;
  1187. /* The OOM killer will not help higher order allocs so fail */
  1188. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1189. goto nopage;
  1190. out_of_memory(zonelist, gfp_mask, order);
  1191. goto restart;
  1192. }
  1193. /*
  1194. * Don't let big-order allocations loop unless the caller explicitly
  1195. * requests that. Wait for some write requests to complete then retry.
  1196. *
  1197. * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
  1198. * <= 3, but that may not be true in other implementations.
  1199. */
  1200. do_retry = 0;
  1201. if (!(gfp_mask & __GFP_NORETRY)) {
  1202. if ((order <= PAGE_ALLOC_COSTLY_ORDER) ||
  1203. (gfp_mask & __GFP_REPEAT))
  1204. do_retry = 1;
  1205. if (gfp_mask & __GFP_NOFAIL)
  1206. do_retry = 1;
  1207. }
  1208. if (do_retry) {
  1209. congestion_wait(WRITE, HZ/50);
  1210. goto rebalance;
  1211. }
  1212. nopage:
  1213. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  1214. printk(KERN_WARNING "%s: page allocation failure."
  1215. " order:%d, mode:0x%x\n",
  1216. p->comm, order, gfp_mask);
  1217. dump_stack();
  1218. show_mem();
  1219. }
  1220. got_pg:
  1221. return page;
  1222. }
  1223. EXPORT_SYMBOL(__alloc_pages);
  1224. /*
  1225. * Common helper functions.
  1226. */
  1227. fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  1228. {
  1229. struct page * page;
  1230. page = alloc_pages(gfp_mask, order);
  1231. if (!page)
  1232. return 0;
  1233. return (unsigned long) page_address(page);
  1234. }
  1235. EXPORT_SYMBOL(__get_free_pages);
  1236. fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
  1237. {
  1238. struct page * page;
  1239. /*
  1240. * get_zeroed_page() returns a 32-bit address, which cannot represent
  1241. * a highmem page
  1242. */
  1243. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1244. page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
  1245. if (page)
  1246. return (unsigned long) page_address(page);
  1247. return 0;
  1248. }
  1249. EXPORT_SYMBOL(get_zeroed_page);
  1250. void __pagevec_free(struct pagevec *pvec)
  1251. {
  1252. int i = pagevec_count(pvec);
  1253. while (--i >= 0)
  1254. free_hot_cold_page(pvec->pages[i], pvec->cold);
  1255. }
  1256. fastcall void __free_pages(struct page *page, unsigned int order)
  1257. {
  1258. if (put_page_testzero(page)) {
  1259. if (order == 0)
  1260. free_hot_page(page);
  1261. else
  1262. __free_pages_ok(page, order);
  1263. }
  1264. }
  1265. EXPORT_SYMBOL(__free_pages);
  1266. fastcall void free_pages(unsigned long addr, unsigned int order)
  1267. {
  1268. if (addr != 0) {
  1269. VM_BUG_ON(!virt_addr_valid((void *)addr));
  1270. __free_pages(virt_to_page((void *)addr), order);
  1271. }
  1272. }
  1273. EXPORT_SYMBOL(free_pages);
  1274. static unsigned int nr_free_zone_pages(int offset)
  1275. {
  1276. /* Just pick one node, since fallback list is circular */
  1277. pg_data_t *pgdat = NODE_DATA(numa_node_id());
  1278. unsigned int sum = 0;
  1279. struct zonelist *zonelist = pgdat->node_zonelists + offset;
  1280. struct zone **zonep = zonelist->zones;
  1281. struct zone *zone;
  1282. for (zone = *zonep++; zone; zone = *zonep++) {
  1283. unsigned long size = zone->present_pages;
  1284. unsigned long high = zone->pages_high;
  1285. if (size > high)
  1286. sum += size - high;
  1287. }
  1288. return sum;
  1289. }
  1290. /*
  1291. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  1292. */
  1293. unsigned int nr_free_buffer_pages(void)
  1294. {
  1295. return nr_free_zone_pages(gfp_zone(GFP_USER));
  1296. }
  1297. EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
  1298. /*
  1299. * Amount of free RAM allocatable within all zones
  1300. */
  1301. unsigned int nr_free_pagecache_pages(void)
  1302. {
  1303. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  1304. }
  1305. static inline void show_node(struct zone *zone)
  1306. {
  1307. if (NUMA_BUILD)
  1308. printk("Node %d ", zone_to_nid(zone));
  1309. }
  1310. void si_meminfo(struct sysinfo *val)
  1311. {
  1312. val->totalram = totalram_pages;
  1313. val->sharedram = 0;
  1314. val->freeram = global_page_state(NR_FREE_PAGES);
  1315. val->bufferram = nr_blockdev_pages();
  1316. val->totalhigh = totalhigh_pages;
  1317. val->freehigh = nr_free_highpages();
  1318. val->mem_unit = PAGE_SIZE;
  1319. }
  1320. EXPORT_SYMBOL(si_meminfo);
  1321. #ifdef CONFIG_NUMA
  1322. void si_meminfo_node(struct sysinfo *val, int nid)
  1323. {
  1324. pg_data_t *pgdat = NODE_DATA(nid);
  1325. val->totalram = pgdat->node_present_pages;
  1326. val->freeram = node_page_state(nid, NR_FREE_PAGES);
  1327. #ifdef CONFIG_HIGHMEM
  1328. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  1329. val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  1330. NR_FREE_PAGES);
  1331. #else
  1332. val->totalhigh = 0;
  1333. val->freehigh = 0;
  1334. #endif
  1335. val->mem_unit = PAGE_SIZE;
  1336. }
  1337. #endif
  1338. #define K(x) ((x) << (PAGE_SHIFT-10))
  1339. /*
  1340. * Show free area list (used inside shift_scroll-lock stuff)
  1341. * We also calculate the percentage fragmentation. We do this by counting the
  1342. * memory on each free list with the exception of the first item on the list.
  1343. */
  1344. void show_free_areas(void)
  1345. {
  1346. int cpu;
  1347. struct zone *zone;
  1348. for_each_zone(zone) {
  1349. if (!populated_zone(zone))
  1350. continue;
  1351. show_node(zone);
  1352. printk("%s per-cpu:\n", zone->name);
  1353. for_each_online_cpu(cpu) {
  1354. struct per_cpu_pageset *pageset;
  1355. pageset = zone_pcp(zone, cpu);
  1356. printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
  1357. "Cold: hi:%5d, btch:%4d usd:%4d\n",
  1358. cpu, pageset->pcp[0].high,
  1359. pageset->pcp[0].batch, pageset->pcp[0].count,
  1360. pageset->pcp[1].high, pageset->pcp[1].batch,
  1361. pageset->pcp[1].count);
  1362. }
  1363. }
  1364. printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu\n"
  1365. " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
  1366. global_page_state(NR_ACTIVE),
  1367. global_page_state(NR_INACTIVE),
  1368. global_page_state(NR_FILE_DIRTY),
  1369. global_page_state(NR_WRITEBACK),
  1370. global_page_state(NR_UNSTABLE_NFS),
  1371. global_page_state(NR_FREE_PAGES),
  1372. global_page_state(NR_SLAB_RECLAIMABLE) +
  1373. global_page_state(NR_SLAB_UNRECLAIMABLE),
  1374. global_page_state(NR_FILE_MAPPED),
  1375. global_page_state(NR_PAGETABLE),
  1376. global_page_state(NR_BOUNCE));
  1377. for_each_zone(zone) {
  1378. int i;
  1379. if (!populated_zone(zone))
  1380. continue;
  1381. show_node(zone);
  1382. printk("%s"
  1383. " free:%lukB"
  1384. " min:%lukB"
  1385. " low:%lukB"
  1386. " high:%lukB"
  1387. " active:%lukB"
  1388. " inactive:%lukB"
  1389. " present:%lukB"
  1390. " pages_scanned:%lu"
  1391. " all_unreclaimable? %s"
  1392. "\n",
  1393. zone->name,
  1394. K(zone_page_state(zone, NR_FREE_PAGES)),
  1395. K(zone->pages_min),
  1396. K(zone->pages_low),
  1397. K(zone->pages_high),
  1398. K(zone_page_state(zone, NR_ACTIVE)),
  1399. K(zone_page_state(zone, NR_INACTIVE)),
  1400. K(zone->present_pages),
  1401. zone->pages_scanned,
  1402. (zone->all_unreclaimable ? "yes" : "no")
  1403. );
  1404. printk("lowmem_reserve[]:");
  1405. for (i = 0; i < MAX_NR_ZONES; i++)
  1406. printk(" %lu", zone->lowmem_reserve[i]);
  1407. printk("\n");
  1408. }
  1409. for_each_zone(zone) {
  1410. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  1411. if (!populated_zone(zone))
  1412. continue;
  1413. show_node(zone);
  1414. printk("%s: ", zone->name);
  1415. spin_lock_irqsave(&zone->lock, flags);
  1416. for (order = 0; order < MAX_ORDER; order++) {
  1417. nr[order] = zone->free_area[order].nr_free;
  1418. total += nr[order] << order;
  1419. }
  1420. spin_unlock_irqrestore(&zone->lock, flags);
  1421. for (order = 0; order < MAX_ORDER; order++)
  1422. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  1423. printk("= %lukB\n", K(total));
  1424. }
  1425. show_swap_cache_info();
  1426. }
  1427. /*
  1428. * Builds allocation fallback zone lists.
  1429. *
  1430. * Add all populated zones of a node to the zonelist.
  1431. */
  1432. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  1433. int nr_zones, enum zone_type zone_type)
  1434. {
  1435. struct zone *zone;
  1436. BUG_ON(zone_type >= MAX_NR_ZONES);
  1437. zone_type++;
  1438. do {
  1439. zone_type--;
  1440. zone = pgdat->node_zones + zone_type;
  1441. if (populated_zone(zone)) {
  1442. zonelist->zones[nr_zones++] = zone;
  1443. check_highest_zone(zone_type);
  1444. }
  1445. } while (zone_type);
  1446. return nr_zones;
  1447. }
  1448. /*
  1449. * zonelist_order:
  1450. * 0 = automatic detection of better ordering.
  1451. * 1 = order by ([node] distance, -zonetype)
  1452. * 2 = order by (-zonetype, [node] distance)
  1453. *
  1454. * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
  1455. * the same zonelist. So only NUMA can configure this param.
  1456. */
  1457. #define ZONELIST_ORDER_DEFAULT 0
  1458. #define ZONELIST_ORDER_NODE 1
  1459. #define ZONELIST_ORDER_ZONE 2
  1460. /* zonelist order in the kernel.
  1461. * set_zonelist_order() will set this to NODE or ZONE.
  1462. */
  1463. static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1464. static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
  1465. #ifdef CONFIG_NUMA
  1466. /* The value user specified ....changed by config */
  1467. static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1468. /* string for sysctl */
  1469. #define NUMA_ZONELIST_ORDER_LEN 16
  1470. char numa_zonelist_order[16] = "default";
  1471. /*
  1472. * interface for configure zonelist ordering.
  1473. * command line option "numa_zonelist_order"
  1474. * = "[dD]efault - default, automatic configuration.
  1475. * = "[nN]ode - order by node locality, then by zone within node
  1476. * = "[zZ]one - order by zone, then by locality within zone
  1477. */
  1478. static int __parse_numa_zonelist_order(char *s)
  1479. {
  1480. if (*s == 'd' || *s == 'D') {
  1481. user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1482. } else if (*s == 'n' || *s == 'N') {
  1483. user_zonelist_order = ZONELIST_ORDER_NODE;
  1484. } else if (*s == 'z' || *s == 'Z') {
  1485. user_zonelist_order = ZONELIST_ORDER_ZONE;
  1486. } else {
  1487. printk(KERN_WARNING
  1488. "Ignoring invalid numa_zonelist_order value: "
  1489. "%s\n", s);
  1490. return -EINVAL;
  1491. }
  1492. return 0;
  1493. }
  1494. static __init int setup_numa_zonelist_order(char *s)
  1495. {
  1496. if (s)
  1497. return __parse_numa_zonelist_order(s);
  1498. return 0;
  1499. }
  1500. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  1501. /*
  1502. * sysctl handler for numa_zonelist_order
  1503. */
  1504. int numa_zonelist_order_handler(ctl_table *table, int write,
  1505. struct file *file, void __user *buffer, size_t *length,
  1506. loff_t *ppos)
  1507. {
  1508. char saved_string[NUMA_ZONELIST_ORDER_LEN];
  1509. int ret;
  1510. if (write)
  1511. strncpy(saved_string, (char*)table->data,
  1512. NUMA_ZONELIST_ORDER_LEN);
  1513. ret = proc_dostring(table, write, file, buffer, length, ppos);
  1514. if (ret)
  1515. return ret;
  1516. if (write) {
  1517. int oldval = user_zonelist_order;
  1518. if (__parse_numa_zonelist_order((char*)table->data)) {
  1519. /*
  1520. * bogus value. restore saved string
  1521. */
  1522. strncpy((char*)table->data, saved_string,
  1523. NUMA_ZONELIST_ORDER_LEN);
  1524. user_zonelist_order = oldval;
  1525. } else if (oldval != user_zonelist_order)
  1526. build_all_zonelists();
  1527. }
  1528. return 0;
  1529. }
  1530. #define MAX_NODE_LOAD (num_online_nodes())
  1531. static int node_load[MAX_NUMNODES];
  1532. /**
  1533. * find_next_best_node - find the next node that should appear in a given node's fallback list
  1534. * @node: node whose fallback list we're appending
  1535. * @used_node_mask: nodemask_t of already used nodes
  1536. *
  1537. * We use a number of factors to determine which is the next node that should
  1538. * appear on a given node's fallback list. The node should not have appeared
  1539. * already in @node's fallback list, and it should be the next closest node
  1540. * according to the distance array (which contains arbitrary distance values
  1541. * from each node to each node in the system), and should also prefer nodes
  1542. * with no CPUs, since presumably they'll have very little allocation pressure
  1543. * on them otherwise.
  1544. * It returns -1 if no node is found.
  1545. */
  1546. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  1547. {
  1548. int n, val;
  1549. int min_val = INT_MAX;
  1550. int best_node = -1;
  1551. /* Use the local node if we haven't already */
  1552. if (!node_isset(node, *used_node_mask)) {
  1553. node_set(node, *used_node_mask);
  1554. return node;
  1555. }
  1556. for_each_online_node(n) {
  1557. cpumask_t tmp;
  1558. /* Don't want a node to appear more than once */
  1559. if (node_isset(n, *used_node_mask))
  1560. continue;
  1561. /* Use the distance array to find the distance */
  1562. val = node_distance(node, n);
  1563. /* Penalize nodes under us ("prefer the next node") */
  1564. val += (n < node);
  1565. /* Give preference to headless and unused nodes */
  1566. tmp = node_to_cpumask(n);
  1567. if (!cpus_empty(tmp))
  1568. val += PENALTY_FOR_NODE_WITH_CPUS;
  1569. /* Slight preference for less loaded node */
  1570. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  1571. val += node_load[n];
  1572. if (val < min_val) {
  1573. min_val = val;
  1574. best_node = n;
  1575. }
  1576. }
  1577. if (best_node >= 0)
  1578. node_set(best_node, *used_node_mask);
  1579. return best_node;
  1580. }
  1581. /*
  1582. * Build zonelists ordered by node and zones within node.
  1583. * This results in maximum locality--normal zone overflows into local
  1584. * DMA zone, if any--but risks exhausting DMA zone.
  1585. */
  1586. static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
  1587. {
  1588. enum zone_type i;
  1589. int j;
  1590. struct zonelist *zonelist;
  1591. for (i = 0; i < MAX_NR_ZONES; i++) {
  1592. zonelist = pgdat->node_zonelists + i;
  1593. for (j = 0; zonelist->zones[j] != NULL; j++)
  1594. ;
  1595. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1596. zonelist->zones[j] = NULL;
  1597. }
  1598. }
  1599. /*
  1600. * Build zonelists ordered by zone and nodes within zones.
  1601. * This results in conserving DMA zone[s] until all Normal memory is
  1602. * exhausted, but results in overflowing to remote node while memory
  1603. * may still exist in local DMA zone.
  1604. */
  1605. static int node_order[MAX_NUMNODES];
  1606. static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
  1607. {
  1608. enum zone_type i;
  1609. int pos, j, node;
  1610. int zone_type; /* needs to be signed */
  1611. struct zone *z;
  1612. struct zonelist *zonelist;
  1613. for (i = 0; i < MAX_NR_ZONES; i++) {
  1614. zonelist = pgdat->node_zonelists + i;
  1615. pos = 0;
  1616. for (zone_type = i; zone_type >= 0; zone_type--) {
  1617. for (j = 0; j < nr_nodes; j++) {
  1618. node = node_order[j];
  1619. z = &NODE_DATA(node)->node_zones[zone_type];
  1620. if (populated_zone(z)) {
  1621. zonelist->zones[pos++] = z;
  1622. check_highest_zone(zone_type);
  1623. }
  1624. }
  1625. }
  1626. zonelist->zones[pos] = NULL;
  1627. }
  1628. }
  1629. static int default_zonelist_order(void)
  1630. {
  1631. int nid, zone_type;
  1632. unsigned long low_kmem_size,total_size;
  1633. struct zone *z;
  1634. int average_size;
  1635. /*
  1636. * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
  1637. * If they are really small and used heavily, the system can fall
  1638. * into OOM very easily.
  1639. * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
  1640. */
  1641. /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
  1642. low_kmem_size = 0;
  1643. total_size = 0;
  1644. for_each_online_node(nid) {
  1645. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  1646. z = &NODE_DATA(nid)->node_zones[zone_type];
  1647. if (populated_zone(z)) {
  1648. if (zone_type < ZONE_NORMAL)
  1649. low_kmem_size += z->present_pages;
  1650. total_size += z->present_pages;
  1651. }
  1652. }
  1653. }
  1654. if (!low_kmem_size || /* there are no DMA area. */
  1655. low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
  1656. return ZONELIST_ORDER_NODE;
  1657. /*
  1658. * look into each node's config.
  1659. * If there is a node whose DMA/DMA32 memory is very big area on
  1660. * local memory, NODE_ORDER may be suitable.
  1661. */
  1662. average_size = total_size / (num_online_nodes() + 1);
  1663. for_each_online_node(nid) {
  1664. low_kmem_size = 0;
  1665. total_size = 0;
  1666. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  1667. z = &NODE_DATA(nid)->node_zones[zone_type];
  1668. if (populated_zone(z)) {
  1669. if (zone_type < ZONE_NORMAL)
  1670. low_kmem_size += z->present_pages;
  1671. total_size += z->present_pages;
  1672. }
  1673. }
  1674. if (low_kmem_size &&
  1675. total_size > average_size && /* ignore small node */
  1676. low_kmem_size > total_size * 70/100)
  1677. return ZONELIST_ORDER_NODE;
  1678. }
  1679. return ZONELIST_ORDER_ZONE;
  1680. }
  1681. static void set_zonelist_order(void)
  1682. {
  1683. if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
  1684. current_zonelist_order = default_zonelist_order();
  1685. else
  1686. current_zonelist_order = user_zonelist_order;
  1687. }
  1688. static void build_zonelists(pg_data_t *pgdat)
  1689. {
  1690. int j, node, load;
  1691. enum zone_type i;
  1692. nodemask_t used_mask;
  1693. int local_node, prev_node;
  1694. struct zonelist *zonelist;
  1695. int order = current_zonelist_order;
  1696. /* initialize zonelists */
  1697. for (i = 0; i < MAX_NR_ZONES; i++) {
  1698. zonelist = pgdat->node_zonelists + i;
  1699. zonelist->zones[0] = NULL;
  1700. }
  1701. /* NUMA-aware ordering of nodes */
  1702. local_node = pgdat->node_id;
  1703. load = num_online_nodes();
  1704. prev_node = local_node;
  1705. nodes_clear(used_mask);
  1706. memset(node_load, 0, sizeof(node_load));
  1707. memset(node_order, 0, sizeof(node_order));
  1708. j = 0;
  1709. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  1710. int distance = node_distance(local_node, node);
  1711. /*
  1712. * If another node is sufficiently far away then it is better
  1713. * to reclaim pages in a zone before going off node.
  1714. */
  1715. if (distance > RECLAIM_DISTANCE)
  1716. zone_reclaim_mode = 1;
  1717. /*
  1718. * We don't want to pressure a particular node.
  1719. * So adding penalty to the first node in same
  1720. * distance group to make it round-robin.
  1721. */
  1722. if (distance != node_distance(local_node, prev_node))
  1723. node_load[node] = load;
  1724. prev_node = node;
  1725. load--;
  1726. if (order == ZONELIST_ORDER_NODE)
  1727. build_zonelists_in_node_order(pgdat, node);
  1728. else
  1729. node_order[j++] = node; /* remember order */
  1730. }
  1731. if (order == ZONELIST_ORDER_ZONE) {
  1732. /* calculate node order -- i.e., DMA last! */
  1733. build_zonelists_in_zone_order(pgdat, j);
  1734. }
  1735. }
  1736. /* Construct the zonelist performance cache - see further mmzone.h */
  1737. static void build_zonelist_cache(pg_data_t *pgdat)
  1738. {
  1739. int i;
  1740. for (i = 0; i < MAX_NR_ZONES; i++) {
  1741. struct zonelist *zonelist;
  1742. struct zonelist_cache *zlc;
  1743. struct zone **z;
  1744. zonelist = pgdat->node_zonelists + i;
  1745. zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
  1746. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  1747. for (z = zonelist->zones; *z; z++)
  1748. zlc->z_to_n[z - zonelist->zones] = zone_to_nid(*z);
  1749. }
  1750. }
  1751. #else /* CONFIG_NUMA */
  1752. static void set_zonelist_order(void)
  1753. {
  1754. current_zonelist_order = ZONELIST_ORDER_ZONE;
  1755. }
  1756. static void build_zonelists(pg_data_t *pgdat)
  1757. {
  1758. int node, local_node;
  1759. enum zone_type i,j;
  1760. local_node = pgdat->node_id;
  1761. for (i = 0; i < MAX_NR_ZONES; i++) {
  1762. struct zonelist *zonelist;
  1763. zonelist = pgdat->node_zonelists + i;
  1764. j = build_zonelists_node(pgdat, zonelist, 0, i);
  1765. /*
  1766. * Now we build the zonelist so that it contains the zones
  1767. * of all the other nodes.
  1768. * We don't want to pressure a particular node, so when
  1769. * building the zones for node N, we make sure that the
  1770. * zones coming right after the local ones are those from
  1771. * node N+1 (modulo N)
  1772. */
  1773. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  1774. if (!node_online(node))
  1775. continue;
  1776. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1777. }
  1778. for (node = 0; node < local_node; node++) {
  1779. if (!node_online(node))
  1780. continue;
  1781. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1782. }
  1783. zonelist->zones[j] = NULL;
  1784. }
  1785. }
  1786. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  1787. static void build_zonelist_cache(pg_data_t *pgdat)
  1788. {
  1789. int i;
  1790. for (i = 0; i < MAX_NR_ZONES; i++)
  1791. pgdat->node_zonelists[i].zlcache_ptr = NULL;
  1792. }
  1793. #endif /* CONFIG_NUMA */
  1794. /* return values int ....just for stop_machine_run() */
  1795. static int __build_all_zonelists(void *dummy)
  1796. {
  1797. int nid;
  1798. for_each_online_node(nid) {
  1799. build_zonelists(NODE_DATA(nid));
  1800. build_zonelist_cache(NODE_DATA(nid));
  1801. }
  1802. return 0;
  1803. }
  1804. void build_all_zonelists(void)
  1805. {
  1806. set_zonelist_order();
  1807. if (system_state == SYSTEM_BOOTING) {
  1808. __build_all_zonelists(NULL);
  1809. cpuset_init_current_mems_allowed();
  1810. } else {
  1811. /* we have to stop all cpus to guaranntee there is no user
  1812. of zonelist */
  1813. stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
  1814. /* cpuset refresh routine should be here */
  1815. }
  1816. vm_total_pages = nr_free_pagecache_pages();
  1817. printk("Built %i zonelists in %s order. Total pages: %ld\n",
  1818. num_online_nodes(),
  1819. zonelist_order_name[current_zonelist_order],
  1820. vm_total_pages);
  1821. #ifdef CONFIG_NUMA
  1822. printk("Policy zone: %s\n", zone_names[policy_zone]);
  1823. #endif
  1824. }
  1825. /*
  1826. * Helper functions to size the waitqueue hash table.
  1827. * Essentially these want to choose hash table sizes sufficiently
  1828. * large so that collisions trying to wait on pages are rare.
  1829. * But in fact, the number of active page waitqueues on typical
  1830. * systems is ridiculously low, less than 200. So this is even
  1831. * conservative, even though it seems large.
  1832. *
  1833. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  1834. * waitqueues, i.e. the size of the waitq table given the number of pages.
  1835. */
  1836. #define PAGES_PER_WAITQUEUE 256
  1837. #ifndef CONFIG_MEMORY_HOTPLUG
  1838. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1839. {
  1840. unsigned long size = 1;
  1841. pages /= PAGES_PER_WAITQUEUE;
  1842. while (size < pages)
  1843. size <<= 1;
  1844. /*
  1845. * Once we have dozens or even hundreds of threads sleeping
  1846. * on IO we've got bigger problems than wait queue collision.
  1847. * Limit the size of the wait table to a reasonable size.
  1848. */
  1849. size = min(size, 4096UL);
  1850. return max(size, 4UL);
  1851. }
  1852. #else
  1853. /*
  1854. * A zone's size might be changed by hot-add, so it is not possible to determine
  1855. * a suitable size for its wait_table. So we use the maximum size now.
  1856. *
  1857. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  1858. *
  1859. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  1860. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  1861. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  1862. *
  1863. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  1864. * or more by the traditional way. (See above). It equals:
  1865. *
  1866. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  1867. * ia64(16K page size) : = ( 8G + 4M)byte.
  1868. * powerpc (64K page size) : = (32G +16M)byte.
  1869. */
  1870. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1871. {
  1872. return 4096UL;
  1873. }
  1874. #endif
  1875. /*
  1876. * This is an integer logarithm so that shifts can be used later
  1877. * to extract the more random high bits from the multiplicative
  1878. * hash function before the remainder is taken.
  1879. */
  1880. static inline unsigned long wait_table_bits(unsigned long size)
  1881. {
  1882. return ffz(~size);
  1883. }
  1884. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  1885. /*
  1886. * Initially all pages are reserved - free ones are freed
  1887. * up by free_all_bootmem() once the early boot process is
  1888. * done. Non-atomic initialization, single-pass.
  1889. */
  1890. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  1891. unsigned long start_pfn, enum memmap_context context)
  1892. {
  1893. struct page *page;
  1894. unsigned long end_pfn = start_pfn + size;
  1895. unsigned long pfn;
  1896. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  1897. /*
  1898. * There can be holes in boot-time mem_map[]s
  1899. * handed to this function. They do not
  1900. * exist on hotplugged memory.
  1901. */
  1902. if (context == MEMMAP_EARLY) {
  1903. if (!early_pfn_valid(pfn))
  1904. continue;
  1905. if (!early_pfn_in_nid(pfn, nid))
  1906. continue;
  1907. }
  1908. page = pfn_to_page(pfn);
  1909. set_page_links(page, zone, nid, pfn);
  1910. init_page_count(page);
  1911. reset_page_mapcount(page);
  1912. SetPageReserved(page);
  1913. INIT_LIST_HEAD(&page->lru);
  1914. #ifdef WANT_PAGE_VIRTUAL
  1915. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1916. if (!is_highmem_idx(zone))
  1917. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1918. #endif
  1919. }
  1920. }
  1921. static void __meminit zone_init_free_lists(struct pglist_data *pgdat,
  1922. struct zone *zone, unsigned long size)
  1923. {
  1924. int order;
  1925. for (order = 0; order < MAX_ORDER ; order++) {
  1926. INIT_LIST_HEAD(&zone->free_area[order].free_list);
  1927. zone->free_area[order].nr_free = 0;
  1928. }
  1929. }
  1930. #ifndef __HAVE_ARCH_MEMMAP_INIT
  1931. #define memmap_init(size, nid, zone, start_pfn) \
  1932. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  1933. #endif
  1934. static int __devinit zone_batchsize(struct zone *zone)
  1935. {
  1936. int batch;
  1937. /*
  1938. * The per-cpu-pages pools are set to around 1000th of the
  1939. * size of the zone. But no more than 1/2 of a meg.
  1940. *
  1941. * OK, so we don't know how big the cache is. So guess.
  1942. */
  1943. batch = zone->present_pages / 1024;
  1944. if (batch * PAGE_SIZE > 512 * 1024)
  1945. batch = (512 * 1024) / PAGE_SIZE;
  1946. batch /= 4; /* We effectively *= 4 below */
  1947. if (batch < 1)
  1948. batch = 1;
  1949. /*
  1950. * Clamp the batch to a 2^n - 1 value. Having a power
  1951. * of 2 value was found to be more likely to have
  1952. * suboptimal cache aliasing properties in some cases.
  1953. *
  1954. * For example if 2 tasks are alternately allocating
  1955. * batches of pages, one task can end up with a lot
  1956. * of pages of one half of the possible page colors
  1957. * and the other with pages of the other colors.
  1958. */
  1959. batch = (1 << (fls(batch + batch/2)-1)) - 1;
  1960. return batch;
  1961. }
  1962. inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  1963. {
  1964. struct per_cpu_pages *pcp;
  1965. memset(p, 0, sizeof(*p));
  1966. pcp = &p->pcp[0]; /* hot */
  1967. pcp->count = 0;
  1968. pcp->high = 6 * batch;
  1969. pcp->batch = max(1UL, 1 * batch);
  1970. INIT_LIST_HEAD(&pcp->list);
  1971. pcp = &p->pcp[1]; /* cold*/
  1972. pcp->count = 0;
  1973. pcp->high = 2 * batch;
  1974. pcp->batch = max(1UL, batch/2);
  1975. INIT_LIST_HEAD(&pcp->list);
  1976. }
  1977. /*
  1978. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  1979. * to the value high for the pageset p.
  1980. */
  1981. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  1982. unsigned long high)
  1983. {
  1984. struct per_cpu_pages *pcp;
  1985. pcp = &p->pcp[0]; /* hot list */
  1986. pcp->high = high;
  1987. pcp->batch = max(1UL, high/4);
  1988. if ((high/4) > (PAGE_SHIFT * 8))
  1989. pcp->batch = PAGE_SHIFT * 8;
  1990. }
  1991. #ifdef CONFIG_NUMA
  1992. /*
  1993. * Boot pageset table. One per cpu which is going to be used for all
  1994. * zones and all nodes. The parameters will be set in such a way
  1995. * that an item put on a list will immediately be handed over to
  1996. * the buddy list. This is safe since pageset manipulation is done
  1997. * with interrupts disabled.
  1998. *
  1999. * Some NUMA counter updates may also be caught by the boot pagesets.
  2000. *
  2001. * The boot_pagesets must be kept even after bootup is complete for
  2002. * unused processors and/or zones. They do play a role for bootstrapping
  2003. * hotplugged processors.
  2004. *
  2005. * zoneinfo_show() and maybe other functions do
  2006. * not check if the processor is online before following the pageset pointer.
  2007. * Other parts of the kernel may not check if the zone is available.
  2008. */
  2009. static struct per_cpu_pageset boot_pageset[NR_CPUS];
  2010. /*
  2011. * Dynamically allocate memory for the
  2012. * per cpu pageset array in struct zone.
  2013. */
  2014. static int __cpuinit process_zones(int cpu)
  2015. {
  2016. struct zone *zone, *dzone;
  2017. for_each_zone(zone) {
  2018. if (!populated_zone(zone))
  2019. continue;
  2020. zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
  2021. GFP_KERNEL, cpu_to_node(cpu));
  2022. if (!zone_pcp(zone, cpu))
  2023. goto bad;
  2024. setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
  2025. if (percpu_pagelist_fraction)
  2026. setup_pagelist_highmark(zone_pcp(zone, cpu),
  2027. (zone->present_pages / percpu_pagelist_fraction));
  2028. }
  2029. return 0;
  2030. bad:
  2031. for_each_zone(dzone) {
  2032. if (dzone == zone)
  2033. break;
  2034. kfree(zone_pcp(dzone, cpu));
  2035. zone_pcp(dzone, cpu) = NULL;
  2036. }
  2037. return -ENOMEM;
  2038. }
  2039. static inline void free_zone_pagesets(int cpu)
  2040. {
  2041. struct zone *zone;
  2042. for_each_zone(zone) {
  2043. struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
  2044. /* Free per_cpu_pageset if it is slab allocated */
  2045. if (pset != &boot_pageset[cpu])
  2046. kfree(pset);
  2047. zone_pcp(zone, cpu) = NULL;
  2048. }
  2049. }
  2050. static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
  2051. unsigned long action,
  2052. void *hcpu)
  2053. {
  2054. int cpu = (long)hcpu;
  2055. int ret = NOTIFY_OK;
  2056. switch (action) {
  2057. case CPU_UP_PREPARE:
  2058. case CPU_UP_PREPARE_FROZEN:
  2059. if (process_zones(cpu))
  2060. ret = NOTIFY_BAD;
  2061. break;
  2062. case CPU_UP_CANCELED:
  2063. case CPU_UP_CANCELED_FROZEN:
  2064. case CPU_DEAD:
  2065. case CPU_DEAD_FROZEN:
  2066. free_zone_pagesets(cpu);
  2067. break;
  2068. default:
  2069. break;
  2070. }
  2071. return ret;
  2072. }
  2073. static struct notifier_block __cpuinitdata pageset_notifier =
  2074. { &pageset_cpuup_callback, NULL, 0 };
  2075. void __init setup_per_cpu_pageset(void)
  2076. {
  2077. int err;
  2078. /* Initialize per_cpu_pageset for cpu 0.
  2079. * A cpuup callback will do this for every cpu
  2080. * as it comes online
  2081. */
  2082. err = process_zones(smp_processor_id());
  2083. BUG_ON(err);
  2084. register_cpu_notifier(&pageset_notifier);
  2085. }
  2086. #endif
  2087. static noinline __init_refok
  2088. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  2089. {
  2090. int i;
  2091. struct pglist_data *pgdat = zone->zone_pgdat;
  2092. size_t alloc_size;
  2093. /*
  2094. * The per-page waitqueue mechanism uses hashed waitqueues
  2095. * per zone.
  2096. */
  2097. zone->wait_table_hash_nr_entries =
  2098. wait_table_hash_nr_entries(zone_size_pages);
  2099. zone->wait_table_bits =
  2100. wait_table_bits(zone->wait_table_hash_nr_entries);
  2101. alloc_size = zone->wait_table_hash_nr_entries
  2102. * sizeof(wait_queue_head_t);
  2103. if (system_state == SYSTEM_BOOTING) {
  2104. zone->wait_table = (wait_queue_head_t *)
  2105. alloc_bootmem_node(pgdat, alloc_size);
  2106. } else {
  2107. /*
  2108. * This case means that a zone whose size was 0 gets new memory
  2109. * via memory hot-add.
  2110. * But it may be the case that a new node was hot-added. In
  2111. * this case vmalloc() will not be able to use this new node's
  2112. * memory - this wait_table must be initialized to use this new
  2113. * node itself as well.
  2114. * To use this new node's memory, further consideration will be
  2115. * necessary.
  2116. */
  2117. zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
  2118. }
  2119. if (!zone->wait_table)
  2120. return -ENOMEM;
  2121. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  2122. init_waitqueue_head(zone->wait_table + i);
  2123. return 0;
  2124. }
  2125. static __meminit void zone_pcp_init(struct zone *zone)
  2126. {
  2127. int cpu;
  2128. unsigned long batch = zone_batchsize(zone);
  2129. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  2130. #ifdef CONFIG_NUMA
  2131. /* Early boot. Slab allocator not functional yet */
  2132. zone_pcp(zone, cpu) = &boot_pageset[cpu];
  2133. setup_pageset(&boot_pageset[cpu],0);
  2134. #else
  2135. setup_pageset(zone_pcp(zone,cpu), batch);
  2136. #endif
  2137. }
  2138. if (zone->present_pages)
  2139. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
  2140. zone->name, zone->present_pages, batch);
  2141. }
  2142. __meminit int init_currently_empty_zone(struct zone *zone,
  2143. unsigned long zone_start_pfn,
  2144. unsigned long size,
  2145. enum memmap_context context)
  2146. {
  2147. struct pglist_data *pgdat = zone->zone_pgdat;
  2148. int ret;
  2149. ret = zone_wait_table_init(zone, size);
  2150. if (ret)
  2151. return ret;
  2152. pgdat->nr_zones = zone_idx(zone) + 1;
  2153. zone->zone_start_pfn = zone_start_pfn;
  2154. memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
  2155. zone_init_free_lists(pgdat, zone, zone->spanned_pages);
  2156. return 0;
  2157. }
  2158. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2159. /*
  2160. * Basic iterator support. Return the first range of PFNs for a node
  2161. * Note: nid == MAX_NUMNODES returns first region regardless of node
  2162. */
  2163. static int __meminit first_active_region_index_in_nid(int nid)
  2164. {
  2165. int i;
  2166. for (i = 0; i < nr_nodemap_entries; i++)
  2167. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  2168. return i;
  2169. return -1;
  2170. }
  2171. /*
  2172. * Basic iterator support. Return the next active range of PFNs for a node
  2173. * Note: nid == MAX_NUMNODES returns next region regardles of node
  2174. */
  2175. static int __meminit next_active_region_index_in_nid(int index, int nid)
  2176. {
  2177. for (index = index + 1; index < nr_nodemap_entries; index++)
  2178. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  2179. return index;
  2180. return -1;
  2181. }
  2182. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  2183. /*
  2184. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  2185. * Architectures may implement their own version but if add_active_range()
  2186. * was used and there are no special requirements, this is a convenient
  2187. * alternative
  2188. */
  2189. int __meminit early_pfn_to_nid(unsigned long pfn)
  2190. {
  2191. int i;
  2192. for (i = 0; i < nr_nodemap_entries; i++) {
  2193. unsigned long start_pfn = early_node_map[i].start_pfn;
  2194. unsigned long end_pfn = early_node_map[i].end_pfn;
  2195. if (start_pfn <= pfn && pfn < end_pfn)
  2196. return early_node_map[i].nid;
  2197. }
  2198. return 0;
  2199. }
  2200. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  2201. /* Basic iterator support to walk early_node_map[] */
  2202. #define for_each_active_range_index_in_nid(i, nid) \
  2203. for (i = first_active_region_index_in_nid(nid); i != -1; \
  2204. i = next_active_region_index_in_nid(i, nid))
  2205. /**
  2206. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  2207. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  2208. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  2209. *
  2210. * If an architecture guarantees that all ranges registered with
  2211. * add_active_ranges() contain no holes and may be freed, this
  2212. * this function may be used instead of calling free_bootmem() manually.
  2213. */
  2214. void __init free_bootmem_with_active_regions(int nid,
  2215. unsigned long max_low_pfn)
  2216. {
  2217. int i;
  2218. for_each_active_range_index_in_nid(i, nid) {
  2219. unsigned long size_pages = 0;
  2220. unsigned long end_pfn = early_node_map[i].end_pfn;
  2221. if (early_node_map[i].start_pfn >= max_low_pfn)
  2222. continue;
  2223. if (end_pfn > max_low_pfn)
  2224. end_pfn = max_low_pfn;
  2225. size_pages = end_pfn - early_node_map[i].start_pfn;
  2226. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  2227. PFN_PHYS(early_node_map[i].start_pfn),
  2228. size_pages << PAGE_SHIFT);
  2229. }
  2230. }
  2231. /**
  2232. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  2233. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  2234. *
  2235. * If an architecture guarantees that all ranges registered with
  2236. * add_active_ranges() contain no holes and may be freed, this
  2237. * function may be used instead of calling memory_present() manually.
  2238. */
  2239. void __init sparse_memory_present_with_active_regions(int nid)
  2240. {
  2241. int i;
  2242. for_each_active_range_index_in_nid(i, nid)
  2243. memory_present(early_node_map[i].nid,
  2244. early_node_map[i].start_pfn,
  2245. early_node_map[i].end_pfn);
  2246. }
  2247. /**
  2248. * push_node_boundaries - Push node boundaries to at least the requested boundary
  2249. * @nid: The nid of the node to push the boundary for
  2250. * @start_pfn: The start pfn of the node
  2251. * @end_pfn: The end pfn of the node
  2252. *
  2253. * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
  2254. * time. Specifically, on x86_64, SRAT will report ranges that can potentially
  2255. * be hotplugged even though no physical memory exists. This function allows
  2256. * an arch to push out the node boundaries so mem_map is allocated that can
  2257. * be used later.
  2258. */
  2259. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  2260. void __init push_node_boundaries(unsigned int nid,
  2261. unsigned long start_pfn, unsigned long end_pfn)
  2262. {
  2263. printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
  2264. nid, start_pfn, end_pfn);
  2265. /* Initialise the boundary for this node if necessary */
  2266. if (node_boundary_end_pfn[nid] == 0)
  2267. node_boundary_start_pfn[nid] = -1UL;
  2268. /* Update the boundaries */
  2269. if (node_boundary_start_pfn[nid] > start_pfn)
  2270. node_boundary_start_pfn[nid] = start_pfn;
  2271. if (node_boundary_end_pfn[nid] < end_pfn)
  2272. node_boundary_end_pfn[nid] = end_pfn;
  2273. }
  2274. /* If necessary, push the node boundary out for reserve hotadd */
  2275. static void __meminit account_node_boundary(unsigned int nid,
  2276. unsigned long *start_pfn, unsigned long *end_pfn)
  2277. {
  2278. printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
  2279. nid, *start_pfn, *end_pfn);
  2280. /* Return if boundary information has not been provided */
  2281. if (node_boundary_end_pfn[nid] == 0)
  2282. return;
  2283. /* Check the boundaries and update if necessary */
  2284. if (node_boundary_start_pfn[nid] < *start_pfn)
  2285. *start_pfn = node_boundary_start_pfn[nid];
  2286. if (node_boundary_end_pfn[nid] > *end_pfn)
  2287. *end_pfn = node_boundary_end_pfn[nid];
  2288. }
  2289. #else
  2290. void __init push_node_boundaries(unsigned int nid,
  2291. unsigned long start_pfn, unsigned long end_pfn) {}
  2292. static void __meminit account_node_boundary(unsigned int nid,
  2293. unsigned long *start_pfn, unsigned long *end_pfn) {}
  2294. #endif
  2295. /**
  2296. * get_pfn_range_for_nid - Return the start and end page frames for a node
  2297. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  2298. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  2299. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  2300. *
  2301. * It returns the start and end page frame of a node based on information
  2302. * provided by an arch calling add_active_range(). If called for a node
  2303. * with no available memory, a warning is printed and the start and end
  2304. * PFNs will be 0.
  2305. */
  2306. void __meminit get_pfn_range_for_nid(unsigned int nid,
  2307. unsigned long *start_pfn, unsigned long *end_pfn)
  2308. {
  2309. int i;
  2310. *start_pfn = -1UL;
  2311. *end_pfn = 0;
  2312. for_each_active_range_index_in_nid(i, nid) {
  2313. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  2314. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  2315. }
  2316. if (*start_pfn == -1UL) {
  2317. printk(KERN_WARNING "Node %u active with no memory\n", nid);
  2318. *start_pfn = 0;
  2319. }
  2320. /* Push the node boundaries out if requested */
  2321. account_node_boundary(nid, start_pfn, end_pfn);
  2322. }
  2323. /*
  2324. * This finds a zone that can be used for ZONE_MOVABLE pages. The
  2325. * assumption is made that zones within a node are ordered in monotonic
  2326. * increasing memory addresses so that the "highest" populated zone is used
  2327. */
  2328. void __init find_usable_zone_for_movable(void)
  2329. {
  2330. int zone_index;
  2331. for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
  2332. if (zone_index == ZONE_MOVABLE)
  2333. continue;
  2334. if (arch_zone_highest_possible_pfn[zone_index] >
  2335. arch_zone_lowest_possible_pfn[zone_index])
  2336. break;
  2337. }
  2338. VM_BUG_ON(zone_index == -1);
  2339. movable_zone = zone_index;
  2340. }
  2341. /*
  2342. * The zone ranges provided by the architecture do not include ZONE_MOVABLE
  2343. * because it is sized independant of architecture. Unlike the other zones,
  2344. * the starting point for ZONE_MOVABLE is not fixed. It may be different
  2345. * in each node depending on the size of each node and how evenly kernelcore
  2346. * is distributed. This helper function adjusts the zone ranges
  2347. * provided by the architecture for a given node by using the end of the
  2348. * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
  2349. * zones within a node are in order of monotonic increases memory addresses
  2350. */
  2351. void __meminit adjust_zone_range_for_zone_movable(int nid,
  2352. unsigned long zone_type,
  2353. unsigned long node_start_pfn,
  2354. unsigned long node_end_pfn,
  2355. unsigned long *zone_start_pfn,
  2356. unsigned long *zone_end_pfn)
  2357. {
  2358. /* Only adjust if ZONE_MOVABLE is on this node */
  2359. if (zone_movable_pfn[nid]) {
  2360. /* Size ZONE_MOVABLE */
  2361. if (zone_type == ZONE_MOVABLE) {
  2362. *zone_start_pfn = zone_movable_pfn[nid];
  2363. *zone_end_pfn = min(node_end_pfn,
  2364. arch_zone_highest_possible_pfn[movable_zone]);
  2365. /* Adjust for ZONE_MOVABLE starting within this range */
  2366. } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
  2367. *zone_end_pfn > zone_movable_pfn[nid]) {
  2368. *zone_end_pfn = zone_movable_pfn[nid];
  2369. /* Check if this whole range is within ZONE_MOVABLE */
  2370. } else if (*zone_start_pfn >= zone_movable_pfn[nid])
  2371. *zone_start_pfn = *zone_end_pfn;
  2372. }
  2373. }
  2374. /*
  2375. * Return the number of pages a zone spans in a node, including holes
  2376. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  2377. */
  2378. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  2379. unsigned long zone_type,
  2380. unsigned long *ignored)
  2381. {
  2382. unsigned long node_start_pfn, node_end_pfn;
  2383. unsigned long zone_start_pfn, zone_end_pfn;
  2384. /* Get the start and end of the node and zone */
  2385. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  2386. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  2387. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  2388. adjust_zone_range_for_zone_movable(nid, zone_type,
  2389. node_start_pfn, node_end_pfn,
  2390. &zone_start_pfn, &zone_end_pfn);
  2391. /* Check that this node has pages within the zone's required range */
  2392. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  2393. return 0;
  2394. /* Move the zone boundaries inside the node if necessary */
  2395. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  2396. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  2397. /* Return the spanned pages */
  2398. return zone_end_pfn - zone_start_pfn;
  2399. }
  2400. /*
  2401. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  2402. * then all holes in the requested range will be accounted for.
  2403. */
  2404. unsigned long __meminit __absent_pages_in_range(int nid,
  2405. unsigned long range_start_pfn,
  2406. unsigned long range_end_pfn)
  2407. {
  2408. int i = 0;
  2409. unsigned long prev_end_pfn = 0, hole_pages = 0;
  2410. unsigned long start_pfn;
  2411. /* Find the end_pfn of the first active range of pfns in the node */
  2412. i = first_active_region_index_in_nid(nid);
  2413. if (i == -1)
  2414. return 0;
  2415. prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  2416. /* Account for ranges before physical memory on this node */
  2417. if (early_node_map[i].start_pfn > range_start_pfn)
  2418. hole_pages = prev_end_pfn - range_start_pfn;
  2419. /* Find all holes for the zone within the node */
  2420. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  2421. /* No need to continue if prev_end_pfn is outside the zone */
  2422. if (prev_end_pfn >= range_end_pfn)
  2423. break;
  2424. /* Make sure the end of the zone is not within the hole */
  2425. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  2426. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  2427. /* Update the hole size cound and move on */
  2428. if (start_pfn > range_start_pfn) {
  2429. BUG_ON(prev_end_pfn > start_pfn);
  2430. hole_pages += start_pfn - prev_end_pfn;
  2431. }
  2432. prev_end_pfn = early_node_map[i].end_pfn;
  2433. }
  2434. /* Account for ranges past physical memory on this node */
  2435. if (range_end_pfn > prev_end_pfn)
  2436. hole_pages += range_end_pfn -
  2437. max(range_start_pfn, prev_end_pfn);
  2438. return hole_pages;
  2439. }
  2440. /**
  2441. * absent_pages_in_range - Return number of page frames in holes within a range
  2442. * @start_pfn: The start PFN to start searching for holes
  2443. * @end_pfn: The end PFN to stop searching for holes
  2444. *
  2445. * It returns the number of pages frames in memory holes within a range.
  2446. */
  2447. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  2448. unsigned long end_pfn)
  2449. {
  2450. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  2451. }
  2452. /* Return the number of page frames in holes in a zone on a node */
  2453. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  2454. unsigned long zone_type,
  2455. unsigned long *ignored)
  2456. {
  2457. unsigned long node_start_pfn, node_end_pfn;
  2458. unsigned long zone_start_pfn, zone_end_pfn;
  2459. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  2460. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  2461. node_start_pfn);
  2462. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  2463. node_end_pfn);
  2464. adjust_zone_range_for_zone_movable(nid, zone_type,
  2465. node_start_pfn, node_end_pfn,
  2466. &zone_start_pfn, &zone_end_pfn);
  2467. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  2468. }
  2469. #else
  2470. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  2471. unsigned long zone_type,
  2472. unsigned long *zones_size)
  2473. {
  2474. return zones_size[zone_type];
  2475. }
  2476. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  2477. unsigned long zone_type,
  2478. unsigned long *zholes_size)
  2479. {
  2480. if (!zholes_size)
  2481. return 0;
  2482. return zholes_size[zone_type];
  2483. }
  2484. #endif
  2485. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  2486. unsigned long *zones_size, unsigned long *zholes_size)
  2487. {
  2488. unsigned long realtotalpages, totalpages = 0;
  2489. enum zone_type i;
  2490. for (i = 0; i < MAX_NR_ZONES; i++)
  2491. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  2492. zones_size);
  2493. pgdat->node_spanned_pages = totalpages;
  2494. realtotalpages = totalpages;
  2495. for (i = 0; i < MAX_NR_ZONES; i++)
  2496. realtotalpages -=
  2497. zone_absent_pages_in_node(pgdat->node_id, i,
  2498. zholes_size);
  2499. pgdat->node_present_pages = realtotalpages;
  2500. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  2501. realtotalpages);
  2502. }
  2503. /*
  2504. * Set up the zone data structures:
  2505. * - mark all pages reserved
  2506. * - mark all memory queues empty
  2507. * - clear the memory bitmaps
  2508. */
  2509. static void __meminit free_area_init_core(struct pglist_data *pgdat,
  2510. unsigned long *zones_size, unsigned long *zholes_size)
  2511. {
  2512. enum zone_type j;
  2513. int nid = pgdat->node_id;
  2514. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  2515. int ret;
  2516. pgdat_resize_init(pgdat);
  2517. pgdat->nr_zones = 0;
  2518. init_waitqueue_head(&pgdat->kswapd_wait);
  2519. pgdat->kswapd_max_order = 0;
  2520. for (j = 0; j < MAX_NR_ZONES; j++) {
  2521. struct zone *zone = pgdat->node_zones + j;
  2522. unsigned long size, realsize, memmap_pages;
  2523. size = zone_spanned_pages_in_node(nid, j, zones_size);
  2524. realsize = size - zone_absent_pages_in_node(nid, j,
  2525. zholes_size);
  2526. /*
  2527. * Adjust realsize so that it accounts for how much memory
  2528. * is used by this zone for memmap. This affects the watermark
  2529. * and per-cpu initialisations
  2530. */
  2531. memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
  2532. if (realsize >= memmap_pages) {
  2533. realsize -= memmap_pages;
  2534. printk(KERN_DEBUG
  2535. " %s zone: %lu pages used for memmap\n",
  2536. zone_names[j], memmap_pages);
  2537. } else
  2538. printk(KERN_WARNING
  2539. " %s zone: %lu pages exceeds realsize %lu\n",
  2540. zone_names[j], memmap_pages, realsize);
  2541. /* Account for reserved pages */
  2542. if (j == 0 && realsize > dma_reserve) {
  2543. realsize -= dma_reserve;
  2544. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  2545. zone_names[0], dma_reserve);
  2546. }
  2547. if (!is_highmem_idx(j))
  2548. nr_kernel_pages += realsize;
  2549. nr_all_pages += realsize;
  2550. zone->spanned_pages = size;
  2551. zone->present_pages = realsize;
  2552. #ifdef CONFIG_NUMA
  2553. zone->node = nid;
  2554. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  2555. / 100;
  2556. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  2557. #endif
  2558. zone->name = zone_names[j];
  2559. spin_lock_init(&zone->lock);
  2560. spin_lock_init(&zone->lru_lock);
  2561. zone_seqlock_init(zone);
  2562. zone->zone_pgdat = pgdat;
  2563. zone->prev_priority = DEF_PRIORITY;
  2564. zone_pcp_init(zone);
  2565. INIT_LIST_HEAD(&zone->active_list);
  2566. INIT_LIST_HEAD(&zone->inactive_list);
  2567. zone->nr_scan_active = 0;
  2568. zone->nr_scan_inactive = 0;
  2569. zap_zone_vm_stats(zone);
  2570. atomic_set(&zone->reclaim_in_progress, 0);
  2571. if (!size)
  2572. continue;
  2573. ret = init_currently_empty_zone(zone, zone_start_pfn,
  2574. size, MEMMAP_EARLY);
  2575. BUG_ON(ret);
  2576. zone_start_pfn += size;
  2577. }
  2578. }
  2579. static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
  2580. {
  2581. /* Skip empty nodes */
  2582. if (!pgdat->node_spanned_pages)
  2583. return;
  2584. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  2585. /* ia64 gets its own node_mem_map, before this, without bootmem */
  2586. if (!pgdat->node_mem_map) {
  2587. unsigned long size, start, end;
  2588. struct page *map;
  2589. /*
  2590. * The zone's endpoints aren't required to be MAX_ORDER
  2591. * aligned but the node_mem_map endpoints must be in order
  2592. * for the buddy allocator to function correctly.
  2593. */
  2594. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  2595. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  2596. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  2597. size = (end - start) * sizeof(struct page);
  2598. map = alloc_remap(pgdat->node_id, size);
  2599. if (!map)
  2600. map = alloc_bootmem_node(pgdat, size);
  2601. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  2602. }
  2603. #ifndef CONFIG_NEED_MULTIPLE_NODES
  2604. /*
  2605. * With no DISCONTIG, the global mem_map is just set as node 0's
  2606. */
  2607. if (pgdat == NODE_DATA(0)) {
  2608. mem_map = NODE_DATA(0)->node_mem_map;
  2609. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2610. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  2611. mem_map -= pgdat->node_start_pfn;
  2612. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  2613. }
  2614. #endif
  2615. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  2616. }
  2617. void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
  2618. unsigned long *zones_size, unsigned long node_start_pfn,
  2619. unsigned long *zholes_size)
  2620. {
  2621. pgdat->node_id = nid;
  2622. pgdat->node_start_pfn = node_start_pfn;
  2623. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  2624. alloc_node_mem_map(pgdat);
  2625. free_area_init_core(pgdat, zones_size, zholes_size);
  2626. }
  2627. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2628. #if MAX_NUMNODES > 1
  2629. /*
  2630. * Figure out the number of possible node ids.
  2631. */
  2632. static void __init setup_nr_node_ids(void)
  2633. {
  2634. unsigned int node;
  2635. unsigned int highest = 0;
  2636. for_each_node_mask(node, node_possible_map)
  2637. highest = node;
  2638. nr_node_ids = highest + 1;
  2639. }
  2640. #else
  2641. static inline void setup_nr_node_ids(void)
  2642. {
  2643. }
  2644. #endif
  2645. /**
  2646. * add_active_range - Register a range of PFNs backed by physical memory
  2647. * @nid: The node ID the range resides on
  2648. * @start_pfn: The start PFN of the available physical memory
  2649. * @end_pfn: The end PFN of the available physical memory
  2650. *
  2651. * These ranges are stored in an early_node_map[] and later used by
  2652. * free_area_init_nodes() to calculate zone sizes and holes. If the
  2653. * range spans a memory hole, it is up to the architecture to ensure
  2654. * the memory is not freed by the bootmem allocator. If possible
  2655. * the range being registered will be merged with existing ranges.
  2656. */
  2657. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  2658. unsigned long end_pfn)
  2659. {
  2660. int i;
  2661. printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
  2662. "%d entries of %d used\n",
  2663. nid, start_pfn, end_pfn,
  2664. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  2665. /* Merge with existing active regions if possible */
  2666. for (i = 0; i < nr_nodemap_entries; i++) {
  2667. if (early_node_map[i].nid != nid)
  2668. continue;
  2669. /* Skip if an existing region covers this new one */
  2670. if (start_pfn >= early_node_map[i].start_pfn &&
  2671. end_pfn <= early_node_map[i].end_pfn)
  2672. return;
  2673. /* Merge forward if suitable */
  2674. if (start_pfn <= early_node_map[i].end_pfn &&
  2675. end_pfn > early_node_map[i].end_pfn) {
  2676. early_node_map[i].end_pfn = end_pfn;
  2677. return;
  2678. }
  2679. /* Merge backward if suitable */
  2680. if (start_pfn < early_node_map[i].end_pfn &&
  2681. end_pfn >= early_node_map[i].start_pfn) {
  2682. early_node_map[i].start_pfn = start_pfn;
  2683. return;
  2684. }
  2685. }
  2686. /* Check that early_node_map is large enough */
  2687. if (i >= MAX_ACTIVE_REGIONS) {
  2688. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  2689. MAX_ACTIVE_REGIONS);
  2690. return;
  2691. }
  2692. early_node_map[i].nid = nid;
  2693. early_node_map[i].start_pfn = start_pfn;
  2694. early_node_map[i].end_pfn = end_pfn;
  2695. nr_nodemap_entries = i + 1;
  2696. }
  2697. /**
  2698. * shrink_active_range - Shrink an existing registered range of PFNs
  2699. * @nid: The node id the range is on that should be shrunk
  2700. * @old_end_pfn: The old end PFN of the range
  2701. * @new_end_pfn: The new PFN of the range
  2702. *
  2703. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  2704. * The map is kept at the end physical page range that has already been
  2705. * registered with add_active_range(). This function allows an arch to shrink
  2706. * an existing registered range.
  2707. */
  2708. void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
  2709. unsigned long new_end_pfn)
  2710. {
  2711. int i;
  2712. /* Find the old active region end and shrink */
  2713. for_each_active_range_index_in_nid(i, nid)
  2714. if (early_node_map[i].end_pfn == old_end_pfn) {
  2715. early_node_map[i].end_pfn = new_end_pfn;
  2716. break;
  2717. }
  2718. }
  2719. /**
  2720. * remove_all_active_ranges - Remove all currently registered regions
  2721. *
  2722. * During discovery, it may be found that a table like SRAT is invalid
  2723. * and an alternative discovery method must be used. This function removes
  2724. * all currently registered regions.
  2725. */
  2726. void __init remove_all_active_ranges(void)
  2727. {
  2728. memset(early_node_map, 0, sizeof(early_node_map));
  2729. nr_nodemap_entries = 0;
  2730. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  2731. memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
  2732. memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
  2733. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  2734. }
  2735. /* Compare two active node_active_regions */
  2736. static int __init cmp_node_active_region(const void *a, const void *b)
  2737. {
  2738. struct node_active_region *arange = (struct node_active_region *)a;
  2739. struct node_active_region *brange = (struct node_active_region *)b;
  2740. /* Done this way to avoid overflows */
  2741. if (arange->start_pfn > brange->start_pfn)
  2742. return 1;
  2743. if (arange->start_pfn < brange->start_pfn)
  2744. return -1;
  2745. return 0;
  2746. }
  2747. /* sort the node_map by start_pfn */
  2748. static void __init sort_node_map(void)
  2749. {
  2750. sort(early_node_map, (size_t)nr_nodemap_entries,
  2751. sizeof(struct node_active_region),
  2752. cmp_node_active_region, NULL);
  2753. }
  2754. /* Find the lowest pfn for a node */
  2755. unsigned long __init find_min_pfn_for_node(unsigned long nid)
  2756. {
  2757. int i;
  2758. unsigned long min_pfn = ULONG_MAX;
  2759. /* Assuming a sorted map, the first range found has the starting pfn */
  2760. for_each_active_range_index_in_nid(i, nid)
  2761. min_pfn = min(min_pfn, early_node_map[i].start_pfn);
  2762. if (min_pfn == ULONG_MAX) {
  2763. printk(KERN_WARNING
  2764. "Could not find start_pfn for node %lu\n", nid);
  2765. return 0;
  2766. }
  2767. return min_pfn;
  2768. }
  2769. /**
  2770. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  2771. *
  2772. * It returns the minimum PFN based on information provided via
  2773. * add_active_range().
  2774. */
  2775. unsigned long __init find_min_pfn_with_active_regions(void)
  2776. {
  2777. return find_min_pfn_for_node(MAX_NUMNODES);
  2778. }
  2779. /**
  2780. * find_max_pfn_with_active_regions - Find the maximum PFN registered
  2781. *
  2782. * It returns the maximum PFN based on information provided via
  2783. * add_active_range().
  2784. */
  2785. unsigned long __init find_max_pfn_with_active_regions(void)
  2786. {
  2787. int i;
  2788. unsigned long max_pfn = 0;
  2789. for (i = 0; i < nr_nodemap_entries; i++)
  2790. max_pfn = max(max_pfn, early_node_map[i].end_pfn);
  2791. return max_pfn;
  2792. }
  2793. unsigned long __init early_calculate_totalpages(void)
  2794. {
  2795. int i;
  2796. unsigned long totalpages = 0;
  2797. for (i = 0; i < nr_nodemap_entries; i++)
  2798. totalpages += early_node_map[i].end_pfn -
  2799. early_node_map[i].start_pfn;
  2800. return totalpages;
  2801. }
  2802. /*
  2803. * Find the PFN the Movable zone begins in each node. Kernel memory
  2804. * is spread evenly between nodes as long as the nodes have enough
  2805. * memory. When they don't, some nodes will have more kernelcore than
  2806. * others
  2807. */
  2808. void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
  2809. {
  2810. int i, nid;
  2811. unsigned long usable_startpfn;
  2812. unsigned long kernelcore_node, kernelcore_remaining;
  2813. int usable_nodes = num_online_nodes();
  2814. /*
  2815. * If movablecore was specified, calculate what size of
  2816. * kernelcore that corresponds so that memory usable for
  2817. * any allocation type is evenly spread. If both kernelcore
  2818. * and movablecore are specified, then the value of kernelcore
  2819. * will be used for required_kernelcore if it's greater than
  2820. * what movablecore would have allowed.
  2821. */
  2822. if (required_movablecore) {
  2823. unsigned long totalpages = early_calculate_totalpages();
  2824. unsigned long corepages;
  2825. /*
  2826. * Round-up so that ZONE_MOVABLE is at least as large as what
  2827. * was requested by the user
  2828. */
  2829. required_movablecore =
  2830. roundup(required_movablecore, MAX_ORDER_NR_PAGES);
  2831. corepages = totalpages - required_movablecore;
  2832. required_kernelcore = max(required_kernelcore, corepages);
  2833. }
  2834. /* If kernelcore was not specified, there is no ZONE_MOVABLE */
  2835. if (!required_kernelcore)
  2836. return;
  2837. /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
  2838. find_usable_zone_for_movable();
  2839. usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
  2840. restart:
  2841. /* Spread kernelcore memory as evenly as possible throughout nodes */
  2842. kernelcore_node = required_kernelcore / usable_nodes;
  2843. for_each_online_node(nid) {
  2844. /*
  2845. * Recalculate kernelcore_node if the division per node
  2846. * now exceeds what is necessary to satisfy the requested
  2847. * amount of memory for the kernel
  2848. */
  2849. if (required_kernelcore < kernelcore_node)
  2850. kernelcore_node = required_kernelcore / usable_nodes;
  2851. /*
  2852. * As the map is walked, we track how much memory is usable
  2853. * by the kernel using kernelcore_remaining. When it is
  2854. * 0, the rest of the node is usable by ZONE_MOVABLE
  2855. */
  2856. kernelcore_remaining = kernelcore_node;
  2857. /* Go through each range of PFNs within this node */
  2858. for_each_active_range_index_in_nid(i, nid) {
  2859. unsigned long start_pfn, end_pfn;
  2860. unsigned long size_pages;
  2861. start_pfn = max(early_node_map[i].start_pfn,
  2862. zone_movable_pfn[nid]);
  2863. end_pfn = early_node_map[i].end_pfn;
  2864. if (start_pfn >= end_pfn)
  2865. continue;
  2866. /* Account for what is only usable for kernelcore */
  2867. if (start_pfn < usable_startpfn) {
  2868. unsigned long kernel_pages;
  2869. kernel_pages = min(end_pfn, usable_startpfn)
  2870. - start_pfn;
  2871. kernelcore_remaining -= min(kernel_pages,
  2872. kernelcore_remaining);
  2873. required_kernelcore -= min(kernel_pages,
  2874. required_kernelcore);
  2875. /* Continue if range is now fully accounted */
  2876. if (end_pfn <= usable_startpfn) {
  2877. /*
  2878. * Push zone_movable_pfn to the end so
  2879. * that if we have to rebalance
  2880. * kernelcore across nodes, we will
  2881. * not double account here
  2882. */
  2883. zone_movable_pfn[nid] = end_pfn;
  2884. continue;
  2885. }
  2886. start_pfn = usable_startpfn;
  2887. }
  2888. /*
  2889. * The usable PFN range for ZONE_MOVABLE is from
  2890. * start_pfn->end_pfn. Calculate size_pages as the
  2891. * number of pages used as kernelcore
  2892. */
  2893. size_pages = end_pfn - start_pfn;
  2894. if (size_pages > kernelcore_remaining)
  2895. size_pages = kernelcore_remaining;
  2896. zone_movable_pfn[nid] = start_pfn + size_pages;
  2897. /*
  2898. * Some kernelcore has been met, update counts and
  2899. * break if the kernelcore for this node has been
  2900. * satisified
  2901. */
  2902. required_kernelcore -= min(required_kernelcore,
  2903. size_pages);
  2904. kernelcore_remaining -= size_pages;
  2905. if (!kernelcore_remaining)
  2906. break;
  2907. }
  2908. }
  2909. /*
  2910. * If there is still required_kernelcore, we do another pass with one
  2911. * less node in the count. This will push zone_movable_pfn[nid] further
  2912. * along on the nodes that still have memory until kernelcore is
  2913. * satisified
  2914. */
  2915. usable_nodes--;
  2916. if (usable_nodes && required_kernelcore > usable_nodes)
  2917. goto restart;
  2918. /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
  2919. for (nid = 0; nid < MAX_NUMNODES; nid++)
  2920. zone_movable_pfn[nid] =
  2921. roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
  2922. }
  2923. /**
  2924. * free_area_init_nodes - Initialise all pg_data_t and zone data
  2925. * @max_zone_pfn: an array of max PFNs for each zone
  2926. *
  2927. * This will call free_area_init_node() for each active node in the system.
  2928. * Using the page ranges provided by add_active_range(), the size of each
  2929. * zone in each node and their holes is calculated. If the maximum PFN
  2930. * between two adjacent zones match, it is assumed that the zone is empty.
  2931. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  2932. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  2933. * starts where the previous one ended. For example, ZONE_DMA32 starts
  2934. * at arch_max_dma_pfn.
  2935. */
  2936. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  2937. {
  2938. unsigned long nid;
  2939. enum zone_type i;
  2940. /* Sort early_node_map as initialisation assumes it is sorted */
  2941. sort_node_map();
  2942. /* Record where the zone boundaries are */
  2943. memset(arch_zone_lowest_possible_pfn, 0,
  2944. sizeof(arch_zone_lowest_possible_pfn));
  2945. memset(arch_zone_highest_possible_pfn, 0,
  2946. sizeof(arch_zone_highest_possible_pfn));
  2947. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  2948. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  2949. for (i = 1; i < MAX_NR_ZONES; i++) {
  2950. if (i == ZONE_MOVABLE)
  2951. continue;
  2952. arch_zone_lowest_possible_pfn[i] =
  2953. arch_zone_highest_possible_pfn[i-1];
  2954. arch_zone_highest_possible_pfn[i] =
  2955. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  2956. }
  2957. arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
  2958. arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
  2959. /* Find the PFNs that ZONE_MOVABLE begins at in each node */
  2960. memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
  2961. find_zone_movable_pfns_for_nodes(zone_movable_pfn);
  2962. /* Print out the zone ranges */
  2963. printk("Zone PFN ranges:\n");
  2964. for (i = 0; i < MAX_NR_ZONES; i++) {
  2965. if (i == ZONE_MOVABLE)
  2966. continue;
  2967. printk(" %-8s %8lu -> %8lu\n",
  2968. zone_names[i],
  2969. arch_zone_lowest_possible_pfn[i],
  2970. arch_zone_highest_possible_pfn[i]);
  2971. }
  2972. /* Print out the PFNs ZONE_MOVABLE begins at in each node */
  2973. printk("Movable zone start PFN for each node\n");
  2974. for (i = 0; i < MAX_NUMNODES; i++) {
  2975. if (zone_movable_pfn[i])
  2976. printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
  2977. }
  2978. /* Print out the early_node_map[] */
  2979. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  2980. for (i = 0; i < nr_nodemap_entries; i++)
  2981. printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
  2982. early_node_map[i].start_pfn,
  2983. early_node_map[i].end_pfn);
  2984. /* Initialise every node */
  2985. setup_nr_node_ids();
  2986. for_each_online_node(nid) {
  2987. pg_data_t *pgdat = NODE_DATA(nid);
  2988. free_area_init_node(nid, pgdat, NULL,
  2989. find_min_pfn_for_node(nid), NULL);
  2990. }
  2991. }
  2992. static int __init cmdline_parse_core(char *p, unsigned long *core)
  2993. {
  2994. unsigned long long coremem;
  2995. if (!p)
  2996. return -EINVAL;
  2997. coremem = memparse(p, &p);
  2998. *core = coremem >> PAGE_SHIFT;
  2999. /* Paranoid check that UL is enough for the coremem value */
  3000. WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
  3001. return 0;
  3002. }
  3003. /*
  3004. * kernelcore=size sets the amount of memory for use for allocations that
  3005. * cannot be reclaimed or migrated.
  3006. */
  3007. static int __init cmdline_parse_kernelcore(char *p)
  3008. {
  3009. return cmdline_parse_core(p, &required_kernelcore);
  3010. }
  3011. /*
  3012. * movablecore=size sets the amount of memory for use for allocations that
  3013. * can be reclaimed or migrated.
  3014. */
  3015. static int __init cmdline_parse_movablecore(char *p)
  3016. {
  3017. return cmdline_parse_core(p, &required_movablecore);
  3018. }
  3019. early_param("kernelcore", cmdline_parse_kernelcore);
  3020. early_param("movablecore", cmdline_parse_movablecore);
  3021. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  3022. /**
  3023. * set_dma_reserve - set the specified number of pages reserved in the first zone
  3024. * @new_dma_reserve: The number of pages to mark reserved
  3025. *
  3026. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  3027. * In the DMA zone, a significant percentage may be consumed by kernel image
  3028. * and other unfreeable allocations which can skew the watermarks badly. This
  3029. * function may optionally be used to account for unfreeable pages in the
  3030. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  3031. * smaller per-cpu batchsize.
  3032. */
  3033. void __init set_dma_reserve(unsigned long new_dma_reserve)
  3034. {
  3035. dma_reserve = new_dma_reserve;
  3036. }
  3037. #ifndef CONFIG_NEED_MULTIPLE_NODES
  3038. static bootmem_data_t contig_bootmem_data;
  3039. struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
  3040. EXPORT_SYMBOL(contig_page_data);
  3041. #endif
  3042. void __init free_area_init(unsigned long *zones_size)
  3043. {
  3044. free_area_init_node(0, NODE_DATA(0), zones_size,
  3045. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  3046. }
  3047. static int page_alloc_cpu_notify(struct notifier_block *self,
  3048. unsigned long action, void *hcpu)
  3049. {
  3050. int cpu = (unsigned long)hcpu;
  3051. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  3052. local_irq_disable();
  3053. __drain_pages(cpu);
  3054. vm_events_fold_cpu(cpu);
  3055. local_irq_enable();
  3056. refresh_cpu_vm_stats(cpu);
  3057. }
  3058. return NOTIFY_OK;
  3059. }
  3060. void __init page_alloc_init(void)
  3061. {
  3062. hotcpu_notifier(page_alloc_cpu_notify, 0);
  3063. }
  3064. /*
  3065. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  3066. * or min_free_kbytes changes.
  3067. */
  3068. static void calculate_totalreserve_pages(void)
  3069. {
  3070. struct pglist_data *pgdat;
  3071. unsigned long reserve_pages = 0;
  3072. enum zone_type i, j;
  3073. for_each_online_pgdat(pgdat) {
  3074. for (i = 0; i < MAX_NR_ZONES; i++) {
  3075. struct zone *zone = pgdat->node_zones + i;
  3076. unsigned long max = 0;
  3077. /* Find valid and maximum lowmem_reserve in the zone */
  3078. for (j = i; j < MAX_NR_ZONES; j++) {
  3079. if (zone->lowmem_reserve[j] > max)
  3080. max = zone->lowmem_reserve[j];
  3081. }
  3082. /* we treat pages_high as reserved pages. */
  3083. max += zone->pages_high;
  3084. if (max > zone->present_pages)
  3085. max = zone->present_pages;
  3086. reserve_pages += max;
  3087. }
  3088. }
  3089. totalreserve_pages = reserve_pages;
  3090. }
  3091. /*
  3092. * setup_per_zone_lowmem_reserve - called whenever
  3093. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  3094. * has a correct pages reserved value, so an adequate number of
  3095. * pages are left in the zone after a successful __alloc_pages().
  3096. */
  3097. static void setup_per_zone_lowmem_reserve(void)
  3098. {
  3099. struct pglist_data *pgdat;
  3100. enum zone_type j, idx;
  3101. for_each_online_pgdat(pgdat) {
  3102. for (j = 0; j < MAX_NR_ZONES; j++) {
  3103. struct zone *zone = pgdat->node_zones + j;
  3104. unsigned long present_pages = zone->present_pages;
  3105. zone->lowmem_reserve[j] = 0;
  3106. idx = j;
  3107. while (idx) {
  3108. struct zone *lower_zone;
  3109. idx--;
  3110. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  3111. sysctl_lowmem_reserve_ratio[idx] = 1;
  3112. lower_zone = pgdat->node_zones + idx;
  3113. lower_zone->lowmem_reserve[j] = present_pages /
  3114. sysctl_lowmem_reserve_ratio[idx];
  3115. present_pages += lower_zone->present_pages;
  3116. }
  3117. }
  3118. }
  3119. /* update totalreserve_pages */
  3120. calculate_totalreserve_pages();
  3121. }
  3122. /**
  3123. * setup_per_zone_pages_min - called when min_free_kbytes changes.
  3124. *
  3125. * Ensures that the pages_{min,low,high} values for each zone are set correctly
  3126. * with respect to min_free_kbytes.
  3127. */
  3128. void setup_per_zone_pages_min(void)
  3129. {
  3130. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  3131. unsigned long lowmem_pages = 0;
  3132. struct zone *zone;
  3133. unsigned long flags;
  3134. /* Calculate total number of !ZONE_HIGHMEM pages */
  3135. for_each_zone(zone) {
  3136. if (!is_highmem(zone))
  3137. lowmem_pages += zone->present_pages;
  3138. }
  3139. for_each_zone(zone) {
  3140. u64 tmp;
  3141. spin_lock_irqsave(&zone->lru_lock, flags);
  3142. tmp = (u64)pages_min * zone->present_pages;
  3143. do_div(tmp, lowmem_pages);
  3144. if (is_highmem(zone)) {
  3145. /*
  3146. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  3147. * need highmem pages, so cap pages_min to a small
  3148. * value here.
  3149. *
  3150. * The (pages_high-pages_low) and (pages_low-pages_min)
  3151. * deltas controls asynch page reclaim, and so should
  3152. * not be capped for highmem.
  3153. */
  3154. int min_pages;
  3155. min_pages = zone->present_pages / 1024;
  3156. if (min_pages < SWAP_CLUSTER_MAX)
  3157. min_pages = SWAP_CLUSTER_MAX;
  3158. if (min_pages > 128)
  3159. min_pages = 128;
  3160. zone->pages_min = min_pages;
  3161. } else {
  3162. /*
  3163. * If it's a lowmem zone, reserve a number of pages
  3164. * proportionate to the zone's size.
  3165. */
  3166. zone->pages_min = tmp;
  3167. }
  3168. zone->pages_low = zone->pages_min + (tmp >> 2);
  3169. zone->pages_high = zone->pages_min + (tmp >> 1);
  3170. spin_unlock_irqrestore(&zone->lru_lock, flags);
  3171. }
  3172. /* update totalreserve_pages */
  3173. calculate_totalreserve_pages();
  3174. }
  3175. /*
  3176. * Initialise min_free_kbytes.
  3177. *
  3178. * For small machines we want it small (128k min). For large machines
  3179. * we want it large (64MB max). But it is not linear, because network
  3180. * bandwidth does not increase linearly with machine size. We use
  3181. *
  3182. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  3183. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  3184. *
  3185. * which yields
  3186. *
  3187. * 16MB: 512k
  3188. * 32MB: 724k
  3189. * 64MB: 1024k
  3190. * 128MB: 1448k
  3191. * 256MB: 2048k
  3192. * 512MB: 2896k
  3193. * 1024MB: 4096k
  3194. * 2048MB: 5792k
  3195. * 4096MB: 8192k
  3196. * 8192MB: 11584k
  3197. * 16384MB: 16384k
  3198. */
  3199. static int __init init_per_zone_pages_min(void)
  3200. {
  3201. unsigned long lowmem_kbytes;
  3202. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  3203. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  3204. if (min_free_kbytes < 128)
  3205. min_free_kbytes = 128;
  3206. if (min_free_kbytes > 65536)
  3207. min_free_kbytes = 65536;
  3208. setup_per_zone_pages_min();
  3209. setup_per_zone_lowmem_reserve();
  3210. return 0;
  3211. }
  3212. module_init(init_per_zone_pages_min)
  3213. /*
  3214. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  3215. * that we can call two helper functions whenever min_free_kbytes
  3216. * changes.
  3217. */
  3218. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  3219. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3220. {
  3221. proc_dointvec(table, write, file, buffer, length, ppos);
  3222. if (write)
  3223. setup_per_zone_pages_min();
  3224. return 0;
  3225. }
  3226. #ifdef CONFIG_NUMA
  3227. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  3228. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3229. {
  3230. struct zone *zone;
  3231. int rc;
  3232. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3233. if (rc)
  3234. return rc;
  3235. for_each_zone(zone)
  3236. zone->min_unmapped_pages = (zone->present_pages *
  3237. sysctl_min_unmapped_ratio) / 100;
  3238. return 0;
  3239. }
  3240. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  3241. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3242. {
  3243. struct zone *zone;
  3244. int rc;
  3245. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3246. if (rc)
  3247. return rc;
  3248. for_each_zone(zone)
  3249. zone->min_slab_pages = (zone->present_pages *
  3250. sysctl_min_slab_ratio) / 100;
  3251. return 0;
  3252. }
  3253. #endif
  3254. /*
  3255. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  3256. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  3257. * whenever sysctl_lowmem_reserve_ratio changes.
  3258. *
  3259. * The reserve ratio obviously has absolutely no relation with the
  3260. * pages_min watermarks. The lowmem reserve ratio can only make sense
  3261. * if in function of the boot time zone sizes.
  3262. */
  3263. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  3264. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3265. {
  3266. proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3267. setup_per_zone_lowmem_reserve();
  3268. return 0;
  3269. }
  3270. /*
  3271. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  3272. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  3273. * can have before it gets flushed back to buddy allocator.
  3274. */
  3275. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  3276. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3277. {
  3278. struct zone *zone;
  3279. unsigned int cpu;
  3280. int ret;
  3281. ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3282. if (!write || (ret == -EINVAL))
  3283. return ret;
  3284. for_each_zone(zone) {
  3285. for_each_online_cpu(cpu) {
  3286. unsigned long high;
  3287. high = zone->present_pages / percpu_pagelist_fraction;
  3288. setup_pagelist_highmark(zone_pcp(zone, cpu), high);
  3289. }
  3290. }
  3291. return 0;
  3292. }
  3293. int hashdist = HASHDIST_DEFAULT;
  3294. #ifdef CONFIG_NUMA
  3295. static int __init set_hashdist(char *str)
  3296. {
  3297. if (!str)
  3298. return 0;
  3299. hashdist = simple_strtoul(str, &str, 0);
  3300. return 1;
  3301. }
  3302. __setup("hashdist=", set_hashdist);
  3303. #endif
  3304. /*
  3305. * allocate a large system hash table from bootmem
  3306. * - it is assumed that the hash table must contain an exact power-of-2
  3307. * quantity of entries
  3308. * - limit is the number of hash buckets, not the total allocation size
  3309. */
  3310. void *__init alloc_large_system_hash(const char *tablename,
  3311. unsigned long bucketsize,
  3312. unsigned long numentries,
  3313. int scale,
  3314. int flags,
  3315. unsigned int *_hash_shift,
  3316. unsigned int *_hash_mask,
  3317. unsigned long limit)
  3318. {
  3319. unsigned long long max = limit;
  3320. unsigned long log2qty, size;
  3321. void *table = NULL;
  3322. /* allow the kernel cmdline to have a say */
  3323. if (!numentries) {
  3324. /* round applicable memory size up to nearest megabyte */
  3325. numentries = nr_kernel_pages;
  3326. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  3327. numentries >>= 20 - PAGE_SHIFT;
  3328. numentries <<= 20 - PAGE_SHIFT;
  3329. /* limit to 1 bucket per 2^scale bytes of low memory */
  3330. if (scale > PAGE_SHIFT)
  3331. numentries >>= (scale - PAGE_SHIFT);
  3332. else
  3333. numentries <<= (PAGE_SHIFT - scale);
  3334. /* Make sure we've got at least a 0-order allocation.. */
  3335. if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  3336. numentries = PAGE_SIZE / bucketsize;
  3337. }
  3338. numentries = roundup_pow_of_two(numentries);
  3339. /* limit allocation size to 1/16 total memory by default */
  3340. if (max == 0) {
  3341. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  3342. do_div(max, bucketsize);
  3343. }
  3344. if (numentries > max)
  3345. numentries = max;
  3346. log2qty = ilog2(numentries);
  3347. do {
  3348. size = bucketsize << log2qty;
  3349. if (flags & HASH_EARLY)
  3350. table = alloc_bootmem(size);
  3351. else if (hashdist)
  3352. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  3353. else {
  3354. unsigned long order;
  3355. for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
  3356. ;
  3357. table = (void*) __get_free_pages(GFP_ATOMIC, order);
  3358. /*
  3359. * If bucketsize is not a power-of-two, we may free
  3360. * some pages at the end of hash table.
  3361. */
  3362. if (table) {
  3363. unsigned long alloc_end = (unsigned long)table +
  3364. (PAGE_SIZE << order);
  3365. unsigned long used = (unsigned long)table +
  3366. PAGE_ALIGN(size);
  3367. split_page(virt_to_page(table), order);
  3368. while (used < alloc_end) {
  3369. free_page(used);
  3370. used += PAGE_SIZE;
  3371. }
  3372. }
  3373. }
  3374. } while (!table && size > PAGE_SIZE && --log2qty);
  3375. if (!table)
  3376. panic("Failed to allocate %s hash table\n", tablename);
  3377. printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
  3378. tablename,
  3379. (1U << log2qty),
  3380. ilog2(size) - PAGE_SHIFT,
  3381. size);
  3382. if (_hash_shift)
  3383. *_hash_shift = log2qty;
  3384. if (_hash_mask)
  3385. *_hash_mask = (1 << log2qty) - 1;
  3386. return table;
  3387. }
  3388. #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
  3389. struct page *pfn_to_page(unsigned long pfn)
  3390. {
  3391. return __pfn_to_page(pfn);
  3392. }
  3393. unsigned long page_to_pfn(struct page *page)
  3394. {
  3395. return __page_to_pfn(page);
  3396. }
  3397. EXPORT_SYMBOL(pfn_to_page);
  3398. EXPORT_SYMBOL(page_to_pfn);
  3399. #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */