page_alloc.c 84 KB

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