page_alloc.c 89 KB

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