page_alloc.c 79 KB

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