page_alloc.c 91 KB

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