page_alloc.c 105 KB

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