page_alloc.c 106 KB

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