page_alloc.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618
  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/config.h>
  17. #include <linux/stddef.h>
  18. #include <linux/mm.h>
  19. #include <linux/swap.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/compiler.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/suspend.h>
  27. #include <linux/pagevec.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/slab.h>
  30. #include <linux/notifier.h>
  31. #include <linux/topology.h>
  32. #include <linux/sysctl.h>
  33. #include <linux/cpu.h>
  34. #include <linux/cpuset.h>
  35. #include <linux/memory_hotplug.h>
  36. #include <linux/nodemask.h>
  37. #include <linux/vmalloc.h>
  38. #include <asm/tlbflush.h>
  39. #include "internal.h"
  40. /*
  41. * MCD - HACK: Find somewhere to initialize this EARLY, or make this
  42. * initializer cleaner
  43. */
  44. nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
  45. EXPORT_SYMBOL(node_online_map);
  46. nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
  47. EXPORT_SYMBOL(node_possible_map);
  48. struct pglist_data *pgdat_list __read_mostly;
  49. unsigned long totalram_pages __read_mostly;
  50. unsigned long totalhigh_pages __read_mostly;
  51. long nr_swap_pages;
  52. /*
  53. * results with 256, 32 in the lowmem_reserve sysctl:
  54. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  55. * 1G machine -> (16M dma, 784M normal, 224M high)
  56. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  57. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  58. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  59. */
  60. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 32 };
  61. EXPORT_SYMBOL(totalram_pages);
  62. EXPORT_SYMBOL(nr_swap_pages);
  63. /*
  64. * Used by page_zone() to look up the address of the struct zone whose
  65. * id is encoded in the upper bits of page->flags
  66. */
  67. struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
  68. EXPORT_SYMBOL(zone_table);
  69. static char *zone_names[MAX_NR_ZONES] = { "DMA", "Normal", "HighMem" };
  70. int min_free_kbytes = 1024;
  71. unsigned long __initdata nr_kernel_pages;
  72. unsigned long __initdata nr_all_pages;
  73. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  74. {
  75. int ret = 0;
  76. unsigned seq;
  77. unsigned long pfn = page_to_pfn(page);
  78. do {
  79. seq = zone_span_seqbegin(zone);
  80. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  81. ret = 1;
  82. else if (pfn < zone->zone_start_pfn)
  83. ret = 1;
  84. } while (zone_span_seqretry(zone, seq));
  85. return ret;
  86. }
  87. static int page_is_consistent(struct zone *zone, struct page *page)
  88. {
  89. #ifdef CONFIG_HOLES_IN_ZONE
  90. if (!pfn_valid(page_to_pfn(page)))
  91. return 0;
  92. #endif
  93. if (zone != page_zone(page))
  94. return 0;
  95. return 1;
  96. }
  97. /*
  98. * Temporary debugging check for pages not lying within a given zone.
  99. */
  100. static int bad_range(struct zone *zone, struct page *page)
  101. {
  102. if (page_outside_zone_boundaries(zone, page))
  103. return 1;
  104. if (!page_is_consistent(zone, page))
  105. return 1;
  106. return 0;
  107. }
  108. static void bad_page(const char *function, struct page *page)
  109. {
  110. printk(KERN_EMERG "Bad page state at %s (in process '%s', page %p)\n",
  111. function, current->comm, page);
  112. printk(KERN_EMERG "flags:0x%0*lx mapping:%p mapcount:%d count:%d\n",
  113. (int)(2*sizeof(page_flags_t)), (unsigned long)page->flags,
  114. page->mapping, page_mapcount(page), page_count(page));
  115. printk(KERN_EMERG "Backtrace:\n");
  116. dump_stack();
  117. printk(KERN_EMERG "Trying to fix it up, but a reboot is needed\n");
  118. page->flags &= ~(1 << PG_lru |
  119. 1 << PG_private |
  120. 1 << PG_locked |
  121. 1 << PG_active |
  122. 1 << PG_dirty |
  123. 1 << PG_reclaim |
  124. 1 << PG_slab |
  125. 1 << PG_swapcache |
  126. 1 << PG_writeback |
  127. 1 << PG_reserved );
  128. set_page_count(page, 0);
  129. reset_page_mapcount(page);
  130. page->mapping = NULL;
  131. add_taint(TAINT_BAD_PAGE);
  132. }
  133. #ifndef CONFIG_HUGETLB_PAGE
  134. #define prep_compound_page(page, order) do { } while (0)
  135. #define destroy_compound_page(page, order) do { } while (0)
  136. #else
  137. /*
  138. * Higher-order pages are called "compound pages". They are structured thusly:
  139. *
  140. * The first PAGE_SIZE page is called the "head page".
  141. *
  142. * The remaining PAGE_SIZE pages are called "tail pages".
  143. *
  144. * All pages have PG_compound set. All pages have their ->private pointing at
  145. * the head page (even the head page has this).
  146. *
  147. * The first tail page's ->mapping, if non-zero, holds the address of the
  148. * compound page's put_page() function.
  149. *
  150. * The order of the allocation is stored in the first tail page's ->index
  151. * This is only for debug at present. This usage means that zero-order pages
  152. * may not be compound.
  153. */
  154. static void prep_compound_page(struct page *page, unsigned long order)
  155. {
  156. int i;
  157. int nr_pages = 1 << order;
  158. page[1].mapping = NULL;
  159. page[1].index = order;
  160. for (i = 0; i < nr_pages; i++) {
  161. struct page *p = page + i;
  162. SetPageCompound(p);
  163. set_page_private(p, (unsigned long)page);
  164. }
  165. }
  166. static void destroy_compound_page(struct page *page, unsigned long order)
  167. {
  168. int i;
  169. int nr_pages = 1 << order;
  170. if (!PageCompound(page))
  171. return;
  172. if (page[1].index != order)
  173. bad_page(__FUNCTION__, page);
  174. for (i = 0; i < nr_pages; i++) {
  175. struct page *p = page + i;
  176. if (!PageCompound(p))
  177. bad_page(__FUNCTION__, page);
  178. if (page_private(p) != (unsigned long)page)
  179. bad_page(__FUNCTION__, page);
  180. ClearPageCompound(p);
  181. }
  182. }
  183. #endif /* CONFIG_HUGETLB_PAGE */
  184. /*
  185. * function for dealing with page's order in buddy system.
  186. * zone->lock is already acquired when we use these.
  187. * So, we don't need atomic page->flags operations here.
  188. */
  189. static inline unsigned long page_order(struct page *page) {
  190. return page_private(page);
  191. }
  192. static inline void set_page_order(struct page *page, int order) {
  193. set_page_private(page, order);
  194. __SetPagePrivate(page);
  195. }
  196. static inline void rmv_page_order(struct page *page)
  197. {
  198. __ClearPagePrivate(page);
  199. set_page_private(page, 0);
  200. }
  201. /*
  202. * Locate the struct page for both the matching buddy in our
  203. * pair (buddy1) and the combined O(n+1) page they form (page).
  204. *
  205. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  206. * the following equation:
  207. * B2 = B1 ^ (1 << O)
  208. * For example, if the starting buddy (buddy2) is #8 its order
  209. * 1 buddy is #10:
  210. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  211. *
  212. * 2) Any buddy B will have an order O+1 parent P which
  213. * satisfies the following equation:
  214. * P = B & ~(1 << O)
  215. *
  216. * Assumption: *_mem_map is contigious at least up to MAX_ORDER
  217. */
  218. static inline struct page *
  219. __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
  220. {
  221. unsigned long buddy_idx = page_idx ^ (1 << order);
  222. return page + (buddy_idx - page_idx);
  223. }
  224. static inline unsigned long
  225. __find_combined_index(unsigned long page_idx, unsigned int order)
  226. {
  227. return (page_idx & ~(1 << order));
  228. }
  229. /*
  230. * This function checks whether a page is free && is the buddy
  231. * we can do coalesce a page and its buddy if
  232. * (a) the buddy is free &&
  233. * (b) the buddy is on the buddy system &&
  234. * (c) a page and its buddy have the same order.
  235. * for recording page's order, we use page_private(page) and PG_private.
  236. *
  237. */
  238. static inline int page_is_buddy(struct page *page, int order)
  239. {
  240. if (PagePrivate(page) &&
  241. (page_order(page) == order) &&
  242. page_count(page) == 0)
  243. return 1;
  244. return 0;
  245. }
  246. /*
  247. * Freeing function for a buddy system allocator.
  248. *
  249. * The concept of a buddy system is to maintain direct-mapped table
  250. * (containing bit values) for memory blocks of various "orders".
  251. * The bottom level table contains the map for the smallest allocatable
  252. * units of memory (here, pages), and each level above it describes
  253. * pairs of units from the levels below, hence, "buddies".
  254. * At a high level, all that happens here is marking the table entry
  255. * at the bottom level available, and propagating the changes upward
  256. * as necessary, plus some accounting needed to play nicely with other
  257. * parts of the VM system.
  258. * At each level, we keep a list of pages, which are heads of continuous
  259. * free pages of length of (1 << order) and marked with PG_Private.Page's
  260. * order is recorded in page_private(page) field.
  261. * So when we are allocating or freeing one, we can derive the state of the
  262. * other. That is, if we allocate a small block, and both were
  263. * free, the remainder of the region must be split into blocks.
  264. * If a block is freed, and its buddy is also free, then this
  265. * triggers coalescing into a block of larger size.
  266. *
  267. * -- wli
  268. */
  269. static inline void __free_pages_bulk (struct page *page,
  270. struct zone *zone, unsigned int order)
  271. {
  272. unsigned long page_idx;
  273. int order_size = 1 << order;
  274. if (unlikely(order))
  275. destroy_compound_page(page, order);
  276. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  277. BUG_ON(page_idx & (order_size - 1));
  278. BUG_ON(bad_range(zone, page));
  279. zone->free_pages += order_size;
  280. while (order < MAX_ORDER-1) {
  281. unsigned long combined_idx;
  282. struct free_area *area;
  283. struct page *buddy;
  284. combined_idx = __find_combined_index(page_idx, order);
  285. buddy = __page_find_buddy(page, page_idx, order);
  286. if (bad_range(zone, buddy))
  287. break;
  288. if (!page_is_buddy(buddy, order))
  289. break; /* Move the buddy up one level. */
  290. list_del(&buddy->lru);
  291. area = zone->free_area + order;
  292. area->nr_free--;
  293. rmv_page_order(buddy);
  294. page = page + (combined_idx - page_idx);
  295. page_idx = combined_idx;
  296. order++;
  297. }
  298. set_page_order(page, order);
  299. list_add(&page->lru, &zone->free_area[order].free_list);
  300. zone->free_area[order].nr_free++;
  301. }
  302. static inline void free_pages_check(const char *function, struct page *page)
  303. {
  304. if ( page_mapcount(page) ||
  305. page->mapping != NULL ||
  306. page_count(page) != 0 ||
  307. (page->flags & (
  308. 1 << PG_lru |
  309. 1 << PG_private |
  310. 1 << PG_locked |
  311. 1 << PG_active |
  312. 1 << PG_reclaim |
  313. 1 << PG_slab |
  314. 1 << PG_swapcache |
  315. 1 << PG_writeback |
  316. 1 << PG_reserved )))
  317. bad_page(function, page);
  318. if (PageDirty(page))
  319. __ClearPageDirty(page);
  320. }
  321. /*
  322. * Frees a list of pages.
  323. * Assumes all pages on list are in same zone, and of same order.
  324. * count is the number of pages to free.
  325. *
  326. * If the zone was previously in an "all pages pinned" state then look to
  327. * see if this freeing clears that state.
  328. *
  329. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  330. * pinned" detection logic.
  331. */
  332. static int
  333. free_pages_bulk(struct zone *zone, int count,
  334. struct list_head *list, unsigned int order)
  335. {
  336. unsigned long flags;
  337. struct page *page = NULL;
  338. int ret = 0;
  339. spin_lock_irqsave(&zone->lock, flags);
  340. zone->all_unreclaimable = 0;
  341. zone->pages_scanned = 0;
  342. while (!list_empty(list) && count--) {
  343. page = list_entry(list->prev, struct page, lru);
  344. /* have to delete it as __free_pages_bulk list manipulates */
  345. list_del(&page->lru);
  346. __free_pages_bulk(page, zone, order);
  347. ret++;
  348. }
  349. spin_unlock_irqrestore(&zone->lock, flags);
  350. return ret;
  351. }
  352. void __free_pages_ok(struct page *page, unsigned int order)
  353. {
  354. LIST_HEAD(list);
  355. int i;
  356. arch_free_page(page, order);
  357. mod_page_state(pgfree, 1 << order);
  358. #ifndef CONFIG_MMU
  359. if (order > 0)
  360. for (i = 1 ; i < (1 << order) ; ++i)
  361. __put_page(page + i);
  362. #endif
  363. for (i = 0 ; i < (1 << order) ; ++i)
  364. free_pages_check(__FUNCTION__, page + i);
  365. list_add(&page->lru, &list);
  366. kernel_map_pages(page, 1<<order, 0);
  367. free_pages_bulk(page_zone(page), 1, &list, order);
  368. }
  369. /*
  370. * The order of subdivision here is critical for the IO subsystem.
  371. * Please do not alter this order without good reasons and regression
  372. * testing. Specifically, as large blocks of memory are subdivided,
  373. * the order in which smaller blocks are delivered depends on the order
  374. * they're subdivided in this function. This is the primary factor
  375. * influencing the order in which pages are delivered to the IO
  376. * subsystem according to empirical testing, and this is also justified
  377. * by considering the behavior of a buddy system containing a single
  378. * large block of memory acted on by a series of small allocations.
  379. * This behavior is a critical factor in sglist merging's success.
  380. *
  381. * -- wli
  382. */
  383. static inline struct page *
  384. expand(struct zone *zone, struct page *page,
  385. int low, int high, struct free_area *area)
  386. {
  387. unsigned long size = 1 << high;
  388. while (high > low) {
  389. area--;
  390. high--;
  391. size >>= 1;
  392. BUG_ON(bad_range(zone, &page[size]));
  393. list_add(&page[size].lru, &area->free_list);
  394. area->nr_free++;
  395. set_page_order(&page[size], high);
  396. }
  397. return page;
  398. }
  399. void set_page_refs(struct page *page, int order)
  400. {
  401. #ifdef CONFIG_MMU
  402. set_page_count(page, 1);
  403. #else
  404. int i;
  405. /*
  406. * We need to reference all the pages for this order, otherwise if
  407. * anyone accesses one of the pages with (get/put) it will be freed.
  408. * - eg: access_process_vm()
  409. */
  410. for (i = 0; i < (1 << order); i++)
  411. set_page_count(page + i, 1);
  412. #endif /* CONFIG_MMU */
  413. }
  414. /*
  415. * This page is about to be returned from the page allocator
  416. */
  417. static void prep_new_page(struct page *page, int order)
  418. {
  419. if ( page_mapcount(page) ||
  420. page->mapping != NULL ||
  421. page_count(page) != 0 ||
  422. (page->flags & (
  423. 1 << PG_lru |
  424. 1 << PG_private |
  425. 1 << PG_locked |
  426. 1 << PG_active |
  427. 1 << PG_dirty |
  428. 1 << PG_reclaim |
  429. 1 << PG_slab |
  430. 1 << PG_swapcache |
  431. 1 << PG_writeback |
  432. 1 << PG_reserved )))
  433. bad_page(__FUNCTION__, page);
  434. page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
  435. 1 << PG_referenced | 1 << PG_arch_1 |
  436. 1 << PG_checked | 1 << PG_mappedtodisk);
  437. set_page_private(page, 0);
  438. set_page_refs(page, order);
  439. kernel_map_pages(page, 1 << order, 1);
  440. }
  441. /*
  442. * Do the hard work of removing an element from the buddy allocator.
  443. * Call me with the zone->lock already held.
  444. */
  445. static struct page *__rmqueue(struct zone *zone, unsigned int order)
  446. {
  447. struct free_area * area;
  448. unsigned int current_order;
  449. struct page *page;
  450. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  451. area = zone->free_area + current_order;
  452. if (list_empty(&area->free_list))
  453. continue;
  454. page = list_entry(area->free_list.next, struct page, lru);
  455. list_del(&page->lru);
  456. rmv_page_order(page);
  457. area->nr_free--;
  458. zone->free_pages -= 1UL << order;
  459. return expand(zone, page, order, current_order, area);
  460. }
  461. return NULL;
  462. }
  463. /*
  464. * Obtain a specified number of elements from the buddy allocator, all under
  465. * a single hold of the lock, for efficiency. Add them to the supplied list.
  466. * Returns the number of new pages which were placed at *list.
  467. */
  468. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  469. unsigned long count, struct list_head *list)
  470. {
  471. unsigned long flags;
  472. int i;
  473. int allocated = 0;
  474. struct page *page;
  475. spin_lock_irqsave(&zone->lock, flags);
  476. for (i = 0; i < count; ++i) {
  477. page = __rmqueue(zone, order);
  478. if (page == NULL)
  479. break;
  480. allocated++;
  481. list_add_tail(&page->lru, list);
  482. }
  483. spin_unlock_irqrestore(&zone->lock, flags);
  484. return allocated;
  485. }
  486. #ifdef CONFIG_NUMA
  487. /* Called from the slab reaper to drain remote pagesets */
  488. void drain_remote_pages(void)
  489. {
  490. struct zone *zone;
  491. int i;
  492. unsigned long flags;
  493. local_irq_save(flags);
  494. for_each_zone(zone) {
  495. struct per_cpu_pageset *pset;
  496. /* Do not drain local pagesets */
  497. if (zone->zone_pgdat->node_id == numa_node_id())
  498. continue;
  499. pset = zone->pageset[smp_processor_id()];
  500. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  501. struct per_cpu_pages *pcp;
  502. pcp = &pset->pcp[i];
  503. if (pcp->count)
  504. pcp->count -= free_pages_bulk(zone, pcp->count,
  505. &pcp->list, 0);
  506. }
  507. }
  508. local_irq_restore(flags);
  509. }
  510. #endif
  511. #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
  512. static void __drain_pages(unsigned int cpu)
  513. {
  514. struct zone *zone;
  515. int i;
  516. for_each_zone(zone) {
  517. struct per_cpu_pageset *pset;
  518. pset = zone_pcp(zone, cpu);
  519. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  520. struct per_cpu_pages *pcp;
  521. pcp = &pset->pcp[i];
  522. pcp->count -= free_pages_bulk(zone, pcp->count,
  523. &pcp->list, 0);
  524. }
  525. }
  526. }
  527. #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
  528. #ifdef CONFIG_PM
  529. void mark_free_pages(struct zone *zone)
  530. {
  531. unsigned long zone_pfn, flags;
  532. int order;
  533. struct list_head *curr;
  534. if (!zone->spanned_pages)
  535. return;
  536. spin_lock_irqsave(&zone->lock, flags);
  537. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  538. ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
  539. for (order = MAX_ORDER - 1; order >= 0; --order)
  540. list_for_each(curr, &zone->free_area[order].free_list) {
  541. unsigned long start_pfn, i;
  542. start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
  543. for (i=0; i < (1<<order); i++)
  544. SetPageNosaveFree(pfn_to_page(start_pfn+i));
  545. }
  546. spin_unlock_irqrestore(&zone->lock, flags);
  547. }
  548. /*
  549. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  550. */
  551. void drain_local_pages(void)
  552. {
  553. unsigned long flags;
  554. local_irq_save(flags);
  555. __drain_pages(smp_processor_id());
  556. local_irq_restore(flags);
  557. }
  558. #endif /* CONFIG_PM */
  559. static void zone_statistics(struct zonelist *zonelist, struct zone *z)
  560. {
  561. #ifdef CONFIG_NUMA
  562. unsigned long flags;
  563. int cpu;
  564. pg_data_t *pg = z->zone_pgdat;
  565. pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
  566. struct per_cpu_pageset *p;
  567. local_irq_save(flags);
  568. cpu = smp_processor_id();
  569. p = zone_pcp(z,cpu);
  570. if (pg == orig) {
  571. p->numa_hit++;
  572. } else {
  573. p->numa_miss++;
  574. zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
  575. }
  576. if (pg == NODE_DATA(numa_node_id()))
  577. p->local_node++;
  578. else
  579. p->other_node++;
  580. local_irq_restore(flags);
  581. #endif
  582. }
  583. /*
  584. * Free a 0-order page
  585. */
  586. static void FASTCALL(free_hot_cold_page(struct page *page, int cold));
  587. static void fastcall free_hot_cold_page(struct page *page, int cold)
  588. {
  589. struct zone *zone = page_zone(page);
  590. struct per_cpu_pages *pcp;
  591. unsigned long flags;
  592. arch_free_page(page, 0);
  593. kernel_map_pages(page, 1, 0);
  594. inc_page_state(pgfree);
  595. if (PageAnon(page))
  596. page->mapping = NULL;
  597. free_pages_check(__FUNCTION__, page);
  598. pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
  599. local_irq_save(flags);
  600. list_add(&page->lru, &pcp->list);
  601. pcp->count++;
  602. if (pcp->count >= pcp->high)
  603. pcp->count -= free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
  604. local_irq_restore(flags);
  605. put_cpu();
  606. }
  607. void fastcall free_hot_page(struct page *page)
  608. {
  609. free_hot_cold_page(page, 0);
  610. }
  611. void fastcall free_cold_page(struct page *page)
  612. {
  613. free_hot_cold_page(page, 1);
  614. }
  615. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  616. {
  617. int i;
  618. BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
  619. for(i = 0; i < (1 << order); i++)
  620. clear_highpage(page + i);
  621. }
  622. /*
  623. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  624. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  625. * or two.
  626. */
  627. static struct page *
  628. buffered_rmqueue(struct zone *zone, int order, gfp_t gfp_flags)
  629. {
  630. unsigned long flags;
  631. struct page *page = NULL;
  632. int cold = !!(gfp_flags & __GFP_COLD);
  633. if (order == 0) {
  634. struct per_cpu_pages *pcp;
  635. pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
  636. local_irq_save(flags);
  637. if (pcp->count <= pcp->low)
  638. pcp->count += rmqueue_bulk(zone, 0,
  639. pcp->batch, &pcp->list);
  640. if (pcp->count) {
  641. page = list_entry(pcp->list.next, struct page, lru);
  642. list_del(&page->lru);
  643. pcp->count--;
  644. }
  645. local_irq_restore(flags);
  646. put_cpu();
  647. }
  648. if (page == NULL) {
  649. spin_lock_irqsave(&zone->lock, flags);
  650. page = __rmqueue(zone, order);
  651. spin_unlock_irqrestore(&zone->lock, flags);
  652. }
  653. if (page != NULL) {
  654. BUG_ON(bad_range(zone, page));
  655. mod_page_state_zone(zone, pgalloc, 1 << order);
  656. prep_new_page(page, order);
  657. if (gfp_flags & __GFP_ZERO)
  658. prep_zero_page(page, order, gfp_flags);
  659. if (order && (gfp_flags & __GFP_COMP))
  660. prep_compound_page(page, order);
  661. }
  662. return page;
  663. }
  664. /*
  665. * Return 1 if free pages are above 'mark'. This takes into account the order
  666. * of the allocation.
  667. */
  668. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  669. int classzone_idx, int can_try_harder, gfp_t gfp_high)
  670. {
  671. /* free_pages my go negative - that's OK */
  672. long min = mark, free_pages = z->free_pages - (1 << order) + 1;
  673. int o;
  674. if (gfp_high)
  675. min -= min / 2;
  676. if (can_try_harder)
  677. min -= min / 4;
  678. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  679. return 0;
  680. for (o = 0; o < order; o++) {
  681. /* At the next order, this order's pages become unavailable */
  682. free_pages -= z->free_area[o].nr_free << o;
  683. /* Require fewer higher order pages to be free */
  684. min >>= 1;
  685. if (free_pages <= min)
  686. return 0;
  687. }
  688. return 1;
  689. }
  690. static inline int
  691. should_reclaim_zone(struct zone *z, gfp_t gfp_mask)
  692. {
  693. if (!z->reclaim_pages)
  694. return 0;
  695. if (gfp_mask & __GFP_NORECLAIM)
  696. return 0;
  697. return 1;
  698. }
  699. /*
  700. * This is the 'heart' of the zoned buddy allocator.
  701. */
  702. struct page * fastcall
  703. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  704. struct zonelist *zonelist)
  705. {
  706. const gfp_t wait = gfp_mask & __GFP_WAIT;
  707. struct zone **zones, *z;
  708. struct page *page;
  709. struct reclaim_state reclaim_state;
  710. struct task_struct *p = current;
  711. int i;
  712. int classzone_idx;
  713. int do_retry;
  714. int can_try_harder;
  715. int did_some_progress;
  716. might_sleep_if(wait);
  717. /*
  718. * The caller may dip into page reserves a bit more if the caller
  719. * cannot run direct reclaim, or is the caller has realtime scheduling
  720. * policy
  721. */
  722. can_try_harder = (unlikely(rt_task(p)) && !in_interrupt()) || !wait;
  723. zones = zonelist->zones; /* the list of zones suitable for gfp_mask */
  724. if (unlikely(zones[0] == NULL)) {
  725. /* Should this ever happen?? */
  726. return NULL;
  727. }
  728. classzone_idx = zone_idx(zones[0]);
  729. restart:
  730. /*
  731. * Go through the zonelist once, looking for a zone with enough free.
  732. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  733. */
  734. for (i = 0; (z = zones[i]) != NULL; i++) {
  735. int do_reclaim = should_reclaim_zone(z, gfp_mask);
  736. if (!cpuset_zone_allowed(z, __GFP_HARDWALL))
  737. continue;
  738. /*
  739. * If the zone is to attempt early page reclaim then this loop
  740. * will try to reclaim pages and check the watermark a second
  741. * time before giving up and falling back to the next zone.
  742. */
  743. zone_reclaim_retry:
  744. if (!zone_watermark_ok(z, order, z->pages_low,
  745. classzone_idx, 0, 0)) {
  746. if (!do_reclaim)
  747. continue;
  748. else {
  749. zone_reclaim(z, gfp_mask, order);
  750. /* Only try reclaim once */
  751. do_reclaim = 0;
  752. goto zone_reclaim_retry;
  753. }
  754. }
  755. page = buffered_rmqueue(z, order, gfp_mask);
  756. if (page)
  757. goto got_pg;
  758. }
  759. for (i = 0; (z = zones[i]) != NULL; i++)
  760. wakeup_kswapd(z, order);
  761. /*
  762. * Go through the zonelist again. Let __GFP_HIGH and allocations
  763. * coming from realtime tasks to go deeper into reserves
  764. *
  765. * This is the last chance, in general, before the goto nopage.
  766. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  767. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  768. */
  769. for (i = 0; (z = zones[i]) != NULL; i++) {
  770. if (!zone_watermark_ok(z, order, z->pages_min,
  771. classzone_idx, can_try_harder,
  772. gfp_mask & __GFP_HIGH))
  773. continue;
  774. if (wait && !cpuset_zone_allowed(z, gfp_mask))
  775. continue;
  776. page = buffered_rmqueue(z, order, gfp_mask);
  777. if (page)
  778. goto got_pg;
  779. }
  780. /* This allocation should allow future memory freeing. */
  781. if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
  782. && !in_interrupt()) {
  783. if (!(gfp_mask & __GFP_NOMEMALLOC)) {
  784. /* go through the zonelist yet again, ignoring mins */
  785. for (i = 0; (z = zones[i]) != NULL; i++) {
  786. if (!cpuset_zone_allowed(z, gfp_mask))
  787. continue;
  788. page = buffered_rmqueue(z, order, gfp_mask);
  789. if (page)
  790. goto got_pg;
  791. }
  792. }
  793. goto nopage;
  794. }
  795. /* Atomic allocations - we can't balance anything */
  796. if (!wait)
  797. goto nopage;
  798. rebalance:
  799. cond_resched();
  800. /* We now go into synchronous reclaim */
  801. p->flags |= PF_MEMALLOC;
  802. reclaim_state.reclaimed_slab = 0;
  803. p->reclaim_state = &reclaim_state;
  804. did_some_progress = try_to_free_pages(zones, gfp_mask);
  805. p->reclaim_state = NULL;
  806. p->flags &= ~PF_MEMALLOC;
  807. cond_resched();
  808. if (likely(did_some_progress)) {
  809. for (i = 0; (z = zones[i]) != NULL; i++) {
  810. if (!zone_watermark_ok(z, order, z->pages_min,
  811. classzone_idx, can_try_harder,
  812. gfp_mask & __GFP_HIGH))
  813. continue;
  814. if (!cpuset_zone_allowed(z, gfp_mask))
  815. continue;
  816. page = buffered_rmqueue(z, order, gfp_mask);
  817. if (page)
  818. goto got_pg;
  819. }
  820. } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  821. /*
  822. * Go through the zonelist yet one more time, keep
  823. * very high watermark here, this is only to catch
  824. * a parallel oom killing, we must fail if we're still
  825. * under heavy pressure.
  826. */
  827. for (i = 0; (z = zones[i]) != NULL; i++) {
  828. if (!zone_watermark_ok(z, order, z->pages_high,
  829. classzone_idx, 0, 0))
  830. continue;
  831. if (!cpuset_zone_allowed(z, __GFP_HARDWALL))
  832. continue;
  833. page = buffered_rmqueue(z, order, gfp_mask);
  834. if (page)
  835. goto got_pg;
  836. }
  837. out_of_memory(gfp_mask, order);
  838. goto restart;
  839. }
  840. /*
  841. * Don't let big-order allocations loop unless the caller explicitly
  842. * requests that. Wait for some write requests to complete then retry.
  843. *
  844. * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
  845. * <= 3, but that may not be true in other implementations.
  846. */
  847. do_retry = 0;
  848. if (!(gfp_mask & __GFP_NORETRY)) {
  849. if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
  850. do_retry = 1;
  851. if (gfp_mask & __GFP_NOFAIL)
  852. do_retry = 1;
  853. }
  854. if (do_retry) {
  855. blk_congestion_wait(WRITE, HZ/50);
  856. goto rebalance;
  857. }
  858. nopage:
  859. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  860. printk(KERN_WARNING "%s: page allocation failure."
  861. " order:%d, mode:0x%x\n",
  862. p->comm, order, gfp_mask);
  863. dump_stack();
  864. show_mem();
  865. }
  866. return NULL;
  867. got_pg:
  868. zone_statistics(zonelist, z);
  869. return page;
  870. }
  871. EXPORT_SYMBOL(__alloc_pages);
  872. /*
  873. * Common helper functions.
  874. */
  875. fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  876. {
  877. struct page * page;
  878. page = alloc_pages(gfp_mask, order);
  879. if (!page)
  880. return 0;
  881. return (unsigned long) page_address(page);
  882. }
  883. EXPORT_SYMBOL(__get_free_pages);
  884. fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
  885. {
  886. struct page * page;
  887. /*
  888. * get_zeroed_page() returns a 32-bit address, which cannot represent
  889. * a highmem page
  890. */
  891. BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  892. page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
  893. if (page)
  894. return (unsigned long) page_address(page);
  895. return 0;
  896. }
  897. EXPORT_SYMBOL(get_zeroed_page);
  898. void __pagevec_free(struct pagevec *pvec)
  899. {
  900. int i = pagevec_count(pvec);
  901. while (--i >= 0)
  902. free_hot_cold_page(pvec->pages[i], pvec->cold);
  903. }
  904. fastcall void __free_pages(struct page *page, unsigned int order)
  905. {
  906. if (put_page_testzero(page)) {
  907. if (order == 0)
  908. free_hot_page(page);
  909. else
  910. __free_pages_ok(page, order);
  911. }
  912. }
  913. EXPORT_SYMBOL(__free_pages);
  914. fastcall void free_pages(unsigned long addr, unsigned int order)
  915. {
  916. if (addr != 0) {
  917. BUG_ON(!virt_addr_valid((void *)addr));
  918. __free_pages(virt_to_page((void *)addr), order);
  919. }
  920. }
  921. EXPORT_SYMBOL(free_pages);
  922. /*
  923. * Total amount of free (allocatable) RAM:
  924. */
  925. unsigned int nr_free_pages(void)
  926. {
  927. unsigned int sum = 0;
  928. struct zone *zone;
  929. for_each_zone(zone)
  930. sum += zone->free_pages;
  931. return sum;
  932. }
  933. EXPORT_SYMBOL(nr_free_pages);
  934. #ifdef CONFIG_NUMA
  935. unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
  936. {
  937. unsigned int i, sum = 0;
  938. for (i = 0; i < MAX_NR_ZONES; i++)
  939. sum += pgdat->node_zones[i].free_pages;
  940. return sum;
  941. }
  942. #endif
  943. static unsigned int nr_free_zone_pages(int offset)
  944. {
  945. /* Just pick one node, since fallback list is circular */
  946. pg_data_t *pgdat = NODE_DATA(numa_node_id());
  947. unsigned int sum = 0;
  948. struct zonelist *zonelist = pgdat->node_zonelists + offset;
  949. struct zone **zonep = zonelist->zones;
  950. struct zone *zone;
  951. for (zone = *zonep++; zone; zone = *zonep++) {
  952. unsigned long size = zone->present_pages;
  953. unsigned long high = zone->pages_high;
  954. if (size > high)
  955. sum += size - high;
  956. }
  957. return sum;
  958. }
  959. /*
  960. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  961. */
  962. unsigned int nr_free_buffer_pages(void)
  963. {
  964. return nr_free_zone_pages(gfp_zone(GFP_USER));
  965. }
  966. /*
  967. * Amount of free RAM allocatable within all zones
  968. */
  969. unsigned int nr_free_pagecache_pages(void)
  970. {
  971. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
  972. }
  973. #ifdef CONFIG_HIGHMEM
  974. unsigned int nr_free_highpages (void)
  975. {
  976. pg_data_t *pgdat;
  977. unsigned int pages = 0;
  978. for_each_pgdat(pgdat)
  979. pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
  980. return pages;
  981. }
  982. #endif
  983. #ifdef CONFIG_NUMA
  984. static void show_node(struct zone *zone)
  985. {
  986. printk("Node %d ", zone->zone_pgdat->node_id);
  987. }
  988. #else
  989. #define show_node(zone) do { } while (0)
  990. #endif
  991. /*
  992. * Accumulate the page_state information across all CPUs.
  993. * The result is unavoidably approximate - it can change
  994. * during and after execution of this function.
  995. */
  996. static DEFINE_PER_CPU(struct page_state, page_states) = {0};
  997. atomic_t nr_pagecache = ATOMIC_INIT(0);
  998. EXPORT_SYMBOL(nr_pagecache);
  999. #ifdef CONFIG_SMP
  1000. DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
  1001. #endif
  1002. void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
  1003. {
  1004. int cpu = 0;
  1005. memset(ret, 0, sizeof(*ret));
  1006. cpus_and(*cpumask, *cpumask, cpu_online_map);
  1007. cpu = first_cpu(*cpumask);
  1008. while (cpu < NR_CPUS) {
  1009. unsigned long *in, *out, off;
  1010. in = (unsigned long *)&per_cpu(page_states, cpu);
  1011. cpu = next_cpu(cpu, *cpumask);
  1012. if (cpu < NR_CPUS)
  1013. prefetch(&per_cpu(page_states, cpu));
  1014. out = (unsigned long *)ret;
  1015. for (off = 0; off < nr; off++)
  1016. *out++ += *in++;
  1017. }
  1018. }
  1019. void get_page_state_node(struct page_state *ret, int node)
  1020. {
  1021. int nr;
  1022. cpumask_t mask = node_to_cpumask(node);
  1023. nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
  1024. nr /= sizeof(unsigned long);
  1025. __get_page_state(ret, nr+1, &mask);
  1026. }
  1027. void get_page_state(struct page_state *ret)
  1028. {
  1029. int nr;
  1030. cpumask_t mask = CPU_MASK_ALL;
  1031. nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
  1032. nr /= sizeof(unsigned long);
  1033. __get_page_state(ret, nr + 1, &mask);
  1034. }
  1035. void get_full_page_state(struct page_state *ret)
  1036. {
  1037. cpumask_t mask = CPU_MASK_ALL;
  1038. __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
  1039. }
  1040. unsigned long __read_page_state(unsigned long offset)
  1041. {
  1042. unsigned long ret = 0;
  1043. int cpu;
  1044. for_each_online_cpu(cpu) {
  1045. unsigned long in;
  1046. in = (unsigned long)&per_cpu(page_states, cpu) + offset;
  1047. ret += *((unsigned long *)in);
  1048. }
  1049. return ret;
  1050. }
  1051. void __mod_page_state(unsigned long offset, unsigned long delta)
  1052. {
  1053. unsigned long flags;
  1054. void* ptr;
  1055. local_irq_save(flags);
  1056. ptr = &__get_cpu_var(page_states);
  1057. *(unsigned long*)(ptr + offset) += delta;
  1058. local_irq_restore(flags);
  1059. }
  1060. EXPORT_SYMBOL(__mod_page_state);
  1061. void __get_zone_counts(unsigned long *active, unsigned long *inactive,
  1062. unsigned long *free, struct pglist_data *pgdat)
  1063. {
  1064. struct zone *zones = pgdat->node_zones;
  1065. int i;
  1066. *active = 0;
  1067. *inactive = 0;
  1068. *free = 0;
  1069. for (i = 0; i < MAX_NR_ZONES; i++) {
  1070. *active += zones[i].nr_active;
  1071. *inactive += zones[i].nr_inactive;
  1072. *free += zones[i].free_pages;
  1073. }
  1074. }
  1075. void get_zone_counts(unsigned long *active,
  1076. unsigned long *inactive, unsigned long *free)
  1077. {
  1078. struct pglist_data *pgdat;
  1079. *active = 0;
  1080. *inactive = 0;
  1081. *free = 0;
  1082. for_each_pgdat(pgdat) {
  1083. unsigned long l, m, n;
  1084. __get_zone_counts(&l, &m, &n, pgdat);
  1085. *active += l;
  1086. *inactive += m;
  1087. *free += n;
  1088. }
  1089. }
  1090. void si_meminfo(struct sysinfo *val)
  1091. {
  1092. val->totalram = totalram_pages;
  1093. val->sharedram = 0;
  1094. val->freeram = nr_free_pages();
  1095. val->bufferram = nr_blockdev_pages();
  1096. #ifdef CONFIG_HIGHMEM
  1097. val->totalhigh = totalhigh_pages;
  1098. val->freehigh = nr_free_highpages();
  1099. #else
  1100. val->totalhigh = 0;
  1101. val->freehigh = 0;
  1102. #endif
  1103. val->mem_unit = PAGE_SIZE;
  1104. }
  1105. EXPORT_SYMBOL(si_meminfo);
  1106. #ifdef CONFIG_NUMA
  1107. void si_meminfo_node(struct sysinfo *val, int nid)
  1108. {
  1109. pg_data_t *pgdat = NODE_DATA(nid);
  1110. val->totalram = pgdat->node_present_pages;
  1111. val->freeram = nr_free_pages_pgdat(pgdat);
  1112. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  1113. val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
  1114. val->mem_unit = PAGE_SIZE;
  1115. }
  1116. #endif
  1117. #define K(x) ((x) << (PAGE_SHIFT-10))
  1118. /*
  1119. * Show free area list (used inside shift_scroll-lock stuff)
  1120. * We also calculate the percentage fragmentation. We do this by counting the
  1121. * memory on each free list with the exception of the first item on the list.
  1122. */
  1123. void show_free_areas(void)
  1124. {
  1125. struct page_state ps;
  1126. int cpu, temperature;
  1127. unsigned long active;
  1128. unsigned long inactive;
  1129. unsigned long free;
  1130. struct zone *zone;
  1131. for_each_zone(zone) {
  1132. show_node(zone);
  1133. printk("%s per-cpu:", zone->name);
  1134. if (!zone->present_pages) {
  1135. printk(" empty\n");
  1136. continue;
  1137. } else
  1138. printk("\n");
  1139. for (cpu = 0; cpu < NR_CPUS; ++cpu) {
  1140. struct per_cpu_pageset *pageset;
  1141. if (!cpu_possible(cpu))
  1142. continue;
  1143. pageset = zone_pcp(zone, cpu);
  1144. for (temperature = 0; temperature < 2; temperature++)
  1145. printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
  1146. cpu,
  1147. temperature ? "cold" : "hot",
  1148. pageset->pcp[temperature].low,
  1149. pageset->pcp[temperature].high,
  1150. pageset->pcp[temperature].batch,
  1151. pageset->pcp[temperature].count);
  1152. }
  1153. }
  1154. get_page_state(&ps);
  1155. get_zone_counts(&active, &inactive, &free);
  1156. printk("Free pages: %11ukB (%ukB HighMem)\n",
  1157. K(nr_free_pages()),
  1158. K(nr_free_highpages()));
  1159. printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
  1160. "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
  1161. active,
  1162. inactive,
  1163. ps.nr_dirty,
  1164. ps.nr_writeback,
  1165. ps.nr_unstable,
  1166. nr_free_pages(),
  1167. ps.nr_slab,
  1168. ps.nr_mapped,
  1169. ps.nr_page_table_pages);
  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, flags, order, total = 0;
  1202. show_node(zone);
  1203. printk("%s: ", zone->name);
  1204. if (!zone->present_pages) {
  1205. printk("empty\n");
  1206. continue;
  1207. }
  1208. spin_lock_irqsave(&zone->lock, flags);
  1209. for (order = 0; order < MAX_ORDER; order++) {
  1210. nr = zone->free_area[order].nr_free;
  1211. total += nr << order;
  1212. printk("%lu*%lukB ", nr, K(1UL) << order);
  1213. }
  1214. spin_unlock_irqrestore(&zone->lock, flags);
  1215. printk("= %lukB\n", K(total));
  1216. }
  1217. show_swap_cache_info();
  1218. }
  1219. /*
  1220. * Builds allocation fallback zone lists.
  1221. */
  1222. static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
  1223. {
  1224. switch (k) {
  1225. struct zone *zone;
  1226. default:
  1227. BUG();
  1228. case ZONE_HIGHMEM:
  1229. zone = pgdat->node_zones + ZONE_HIGHMEM;
  1230. if (zone->present_pages) {
  1231. #ifndef CONFIG_HIGHMEM
  1232. BUG();
  1233. #endif
  1234. zonelist->zones[j++] = zone;
  1235. }
  1236. case ZONE_NORMAL:
  1237. zone = pgdat->node_zones + ZONE_NORMAL;
  1238. if (zone->present_pages)
  1239. zonelist->zones[j++] = zone;
  1240. case ZONE_DMA:
  1241. zone = pgdat->node_zones + ZONE_DMA;
  1242. if (zone->present_pages)
  1243. zonelist->zones[j++] = zone;
  1244. }
  1245. return j;
  1246. }
  1247. static inline int highest_zone(int zone_bits)
  1248. {
  1249. int res = ZONE_NORMAL;
  1250. if (zone_bits & (__force int)__GFP_HIGHMEM)
  1251. res = ZONE_HIGHMEM;
  1252. if (zone_bits & (__force int)__GFP_DMA)
  1253. res = ZONE_DMA;
  1254. return res;
  1255. }
  1256. #ifdef CONFIG_NUMA
  1257. #define MAX_NODE_LOAD (num_online_nodes())
  1258. static int __initdata node_load[MAX_NUMNODES];
  1259. /**
  1260. * find_next_best_node - find the next node that should appear in a given node's fallback list
  1261. * @node: node whose fallback list we're appending
  1262. * @used_node_mask: nodemask_t of already used nodes
  1263. *
  1264. * We use a number of factors to determine which is the next node that should
  1265. * appear on a given node's fallback list. The node should not have appeared
  1266. * already in @node's fallback list, and it should be the next closest node
  1267. * according to the distance array (which contains arbitrary distance values
  1268. * from each node to each node in the system), and should also prefer nodes
  1269. * with no CPUs, since presumably they'll have very little allocation pressure
  1270. * on them otherwise.
  1271. * It returns -1 if no node is found.
  1272. */
  1273. static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
  1274. {
  1275. int i, n, val;
  1276. int min_val = INT_MAX;
  1277. int best_node = -1;
  1278. for_each_online_node(i) {
  1279. cpumask_t tmp;
  1280. /* Start from local node */
  1281. n = (node+i) % num_online_nodes();
  1282. /* Don't want a node to appear more than once */
  1283. if (node_isset(n, *used_node_mask))
  1284. continue;
  1285. /* Use the local node if we haven't already */
  1286. if (!node_isset(node, *used_node_mask)) {
  1287. best_node = node;
  1288. break;
  1289. }
  1290. /* Use the distance array to find the distance */
  1291. val = node_distance(node, n);
  1292. /* Give preference to headless and unused nodes */
  1293. tmp = node_to_cpumask(n);
  1294. if (!cpus_empty(tmp))
  1295. val += PENALTY_FOR_NODE_WITH_CPUS;
  1296. /* Slight preference for less loaded node */
  1297. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  1298. val += node_load[n];
  1299. if (val < min_val) {
  1300. min_val = val;
  1301. best_node = n;
  1302. }
  1303. }
  1304. if (best_node >= 0)
  1305. node_set(best_node, *used_node_mask);
  1306. return best_node;
  1307. }
  1308. static void __init build_zonelists(pg_data_t *pgdat)
  1309. {
  1310. int i, j, k, node, local_node;
  1311. int prev_node, load;
  1312. struct zonelist *zonelist;
  1313. nodemask_t used_mask;
  1314. /* initialize zonelists */
  1315. for (i = 0; i < GFP_ZONETYPES; i++) {
  1316. zonelist = pgdat->node_zonelists + i;
  1317. zonelist->zones[0] = NULL;
  1318. }
  1319. /* NUMA-aware ordering of nodes */
  1320. local_node = pgdat->node_id;
  1321. load = num_online_nodes();
  1322. prev_node = local_node;
  1323. nodes_clear(used_mask);
  1324. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  1325. /*
  1326. * We don't want to pressure a particular node.
  1327. * So adding penalty to the first node in same
  1328. * distance group to make it round-robin.
  1329. */
  1330. if (node_distance(local_node, node) !=
  1331. node_distance(local_node, prev_node))
  1332. node_load[node] += load;
  1333. prev_node = node;
  1334. load--;
  1335. for (i = 0; i < GFP_ZONETYPES; i++) {
  1336. zonelist = pgdat->node_zonelists + i;
  1337. for (j = 0; zonelist->zones[j] != NULL; j++);
  1338. k = highest_zone(i);
  1339. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1340. zonelist->zones[j] = NULL;
  1341. }
  1342. }
  1343. }
  1344. #else /* CONFIG_NUMA */
  1345. static void __init build_zonelists(pg_data_t *pgdat)
  1346. {
  1347. int i, j, k, node, local_node;
  1348. local_node = pgdat->node_id;
  1349. for (i = 0; i < GFP_ZONETYPES; i++) {
  1350. struct zonelist *zonelist;
  1351. zonelist = pgdat->node_zonelists + i;
  1352. j = 0;
  1353. k = highest_zone(i);
  1354. j = build_zonelists_node(pgdat, zonelist, j, k);
  1355. /*
  1356. * Now we build the zonelist so that it contains the zones
  1357. * of all the other nodes.
  1358. * We don't want to pressure a particular node, so when
  1359. * building the zones for node N, we make sure that the
  1360. * zones coming right after the local ones are those from
  1361. * node N+1 (modulo N)
  1362. */
  1363. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  1364. if (!node_online(node))
  1365. continue;
  1366. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1367. }
  1368. for (node = 0; node < local_node; node++) {
  1369. if (!node_online(node))
  1370. continue;
  1371. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1372. }
  1373. zonelist->zones[j] = NULL;
  1374. }
  1375. }
  1376. #endif /* CONFIG_NUMA */
  1377. void __init build_all_zonelists(void)
  1378. {
  1379. int i;
  1380. for_each_online_node(i)
  1381. build_zonelists(NODE_DATA(i));
  1382. printk("Built %i zonelists\n", num_online_nodes());
  1383. cpuset_init_current_mems_allowed();
  1384. }
  1385. /*
  1386. * Helper functions to size the waitqueue hash table.
  1387. * Essentially these want to choose hash table sizes sufficiently
  1388. * large so that collisions trying to wait on pages are rare.
  1389. * But in fact, the number of active page waitqueues on typical
  1390. * systems is ridiculously low, less than 200. So this is even
  1391. * conservative, even though it seems large.
  1392. *
  1393. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  1394. * waitqueues, i.e. the size of the waitq table given the number of pages.
  1395. */
  1396. #define PAGES_PER_WAITQUEUE 256
  1397. static inline unsigned long wait_table_size(unsigned long pages)
  1398. {
  1399. unsigned long size = 1;
  1400. pages /= PAGES_PER_WAITQUEUE;
  1401. while (size < pages)
  1402. size <<= 1;
  1403. /*
  1404. * Once we have dozens or even hundreds of threads sleeping
  1405. * on IO we've got bigger problems than wait queue collision.
  1406. * Limit the size of the wait table to a reasonable size.
  1407. */
  1408. size = min(size, 4096UL);
  1409. return max(size, 4UL);
  1410. }
  1411. /*
  1412. * This is an integer logarithm so that shifts can be used later
  1413. * to extract the more random high bits from the multiplicative
  1414. * hash function before the remainder is taken.
  1415. */
  1416. static inline unsigned long wait_table_bits(unsigned long size)
  1417. {
  1418. return ffz(~size);
  1419. }
  1420. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  1421. static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
  1422. unsigned long *zones_size, unsigned long *zholes_size)
  1423. {
  1424. unsigned long realtotalpages, totalpages = 0;
  1425. int i;
  1426. for (i = 0; i < MAX_NR_ZONES; i++)
  1427. totalpages += zones_size[i];
  1428. pgdat->node_spanned_pages = totalpages;
  1429. realtotalpages = totalpages;
  1430. if (zholes_size)
  1431. for (i = 0; i < MAX_NR_ZONES; i++)
  1432. realtotalpages -= zholes_size[i];
  1433. pgdat->node_present_pages = realtotalpages;
  1434. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
  1435. }
  1436. /*
  1437. * Initially all pages are reserved - free ones are freed
  1438. * up by free_all_bootmem() once the early boot process is
  1439. * done. Non-atomic initialization, single-pass.
  1440. */
  1441. void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  1442. unsigned long start_pfn)
  1443. {
  1444. struct page *page;
  1445. unsigned long end_pfn = start_pfn + size;
  1446. unsigned long pfn;
  1447. for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
  1448. if (!early_pfn_valid(pfn))
  1449. continue;
  1450. if (!early_pfn_in_nid(pfn, nid))
  1451. continue;
  1452. page = pfn_to_page(pfn);
  1453. set_page_links(page, zone, nid, pfn);
  1454. set_page_count(page, 1);
  1455. reset_page_mapcount(page);
  1456. SetPageReserved(page);
  1457. INIT_LIST_HEAD(&page->lru);
  1458. #ifdef WANT_PAGE_VIRTUAL
  1459. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1460. if (!is_highmem_idx(zone))
  1461. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1462. #endif
  1463. }
  1464. }
  1465. void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
  1466. unsigned long size)
  1467. {
  1468. int order;
  1469. for (order = 0; order < MAX_ORDER ; order++) {
  1470. INIT_LIST_HEAD(&zone->free_area[order].free_list);
  1471. zone->free_area[order].nr_free = 0;
  1472. }
  1473. }
  1474. #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
  1475. void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
  1476. unsigned long size)
  1477. {
  1478. unsigned long snum = pfn_to_section_nr(pfn);
  1479. unsigned long end = pfn_to_section_nr(pfn + size);
  1480. if (FLAGS_HAS_NODE)
  1481. zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
  1482. else
  1483. for (; snum <= end; snum++)
  1484. zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
  1485. }
  1486. #ifndef __HAVE_ARCH_MEMMAP_INIT
  1487. #define memmap_init(size, nid, zone, start_pfn) \
  1488. memmap_init_zone((size), (nid), (zone), (start_pfn))
  1489. #endif
  1490. static int __devinit zone_batchsize(struct zone *zone)
  1491. {
  1492. int batch;
  1493. /*
  1494. * The per-cpu-pages pools are set to around 1000th of the
  1495. * size of the zone. But no more than 1/2 of a meg.
  1496. *
  1497. * OK, so we don't know how big the cache is. So guess.
  1498. */
  1499. batch = zone->present_pages / 1024;
  1500. if (batch * PAGE_SIZE > 512 * 1024)
  1501. batch = (512 * 1024) / PAGE_SIZE;
  1502. batch /= 4; /* We effectively *= 4 below */
  1503. if (batch < 1)
  1504. batch = 1;
  1505. /*
  1506. * We will be trying to allcoate bigger chunks of contiguous
  1507. * memory of the order of fls(batch). This should result in
  1508. * better cache coloring.
  1509. *
  1510. * A sanity check also to ensure that batch is still in limits.
  1511. */
  1512. batch = (1 << fls(batch + batch/2));
  1513. if (fls(batch) >= (PAGE_SHIFT + MAX_ORDER - 2))
  1514. batch = PAGE_SHIFT + ((MAX_ORDER - 1 - PAGE_SHIFT)/2);
  1515. return batch;
  1516. }
  1517. inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  1518. {
  1519. struct per_cpu_pages *pcp;
  1520. memset(p, 0, sizeof(*p));
  1521. pcp = &p->pcp[0]; /* hot */
  1522. pcp->count = 0;
  1523. pcp->low = 0;
  1524. pcp->high = 6 * batch;
  1525. pcp->batch = max(1UL, 1 * batch);
  1526. INIT_LIST_HEAD(&pcp->list);
  1527. pcp = &p->pcp[1]; /* cold*/
  1528. pcp->count = 0;
  1529. pcp->low = 0;
  1530. pcp->high = 2 * batch;
  1531. pcp->batch = max(1UL, batch/2);
  1532. INIT_LIST_HEAD(&pcp->list);
  1533. }
  1534. #ifdef CONFIG_NUMA
  1535. /*
  1536. * Boot pageset table. One per cpu which is going to be used for all
  1537. * zones and all nodes. The parameters will be set in such a way
  1538. * that an item put on a list will immediately be handed over to
  1539. * the buddy list. This is safe since pageset manipulation is done
  1540. * with interrupts disabled.
  1541. *
  1542. * Some NUMA counter updates may also be caught by the boot pagesets.
  1543. *
  1544. * The boot_pagesets must be kept even after bootup is complete for
  1545. * unused processors and/or zones. They do play a role for bootstrapping
  1546. * hotplugged processors.
  1547. *
  1548. * zoneinfo_show() and maybe other functions do
  1549. * not check if the processor is online before following the pageset pointer.
  1550. * Other parts of the kernel may not check if the zone is available.
  1551. */
  1552. static struct per_cpu_pageset
  1553. boot_pageset[NR_CPUS];
  1554. /*
  1555. * Dynamically allocate memory for the
  1556. * per cpu pageset array in struct zone.
  1557. */
  1558. static int __devinit process_zones(int cpu)
  1559. {
  1560. struct zone *zone, *dzone;
  1561. for_each_zone(zone) {
  1562. zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
  1563. GFP_KERNEL, cpu_to_node(cpu));
  1564. if (!zone->pageset[cpu])
  1565. goto bad;
  1566. setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
  1567. }
  1568. return 0;
  1569. bad:
  1570. for_each_zone(dzone) {
  1571. if (dzone == zone)
  1572. break;
  1573. kfree(dzone->pageset[cpu]);
  1574. dzone->pageset[cpu] = NULL;
  1575. }
  1576. return -ENOMEM;
  1577. }
  1578. static inline void free_zone_pagesets(int cpu)
  1579. {
  1580. #ifdef CONFIG_NUMA
  1581. struct zone *zone;
  1582. for_each_zone(zone) {
  1583. struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
  1584. zone_pcp(zone, cpu) = NULL;
  1585. kfree(pset);
  1586. }
  1587. #endif
  1588. }
  1589. static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
  1590. unsigned long action,
  1591. void *hcpu)
  1592. {
  1593. int cpu = (long)hcpu;
  1594. int ret = NOTIFY_OK;
  1595. switch (action) {
  1596. case CPU_UP_PREPARE:
  1597. if (process_zones(cpu))
  1598. ret = NOTIFY_BAD;
  1599. break;
  1600. #ifdef CONFIG_HOTPLUG_CPU
  1601. case CPU_DEAD:
  1602. free_zone_pagesets(cpu);
  1603. break;
  1604. #endif
  1605. default:
  1606. break;
  1607. }
  1608. return ret;
  1609. }
  1610. static struct notifier_block pageset_notifier =
  1611. { &pageset_cpuup_callback, NULL, 0 };
  1612. void __init setup_per_cpu_pageset()
  1613. {
  1614. int err;
  1615. /* Initialize per_cpu_pageset for cpu 0.
  1616. * A cpuup callback will do this for every cpu
  1617. * as it comes online
  1618. */
  1619. err = process_zones(smp_processor_id());
  1620. BUG_ON(err);
  1621. register_cpu_notifier(&pageset_notifier);
  1622. }
  1623. #endif
  1624. static __devinit
  1625. void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  1626. {
  1627. int i;
  1628. struct pglist_data *pgdat = zone->zone_pgdat;
  1629. /*
  1630. * The per-page waitqueue mechanism uses hashed waitqueues
  1631. * per zone.
  1632. */
  1633. zone->wait_table_size = wait_table_size(zone_size_pages);
  1634. zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
  1635. zone->wait_table = (wait_queue_head_t *)
  1636. alloc_bootmem_node(pgdat, zone->wait_table_size
  1637. * sizeof(wait_queue_head_t));
  1638. for(i = 0; i < zone->wait_table_size; ++i)
  1639. init_waitqueue_head(zone->wait_table + i);
  1640. }
  1641. static __devinit void zone_pcp_init(struct zone *zone)
  1642. {
  1643. int cpu;
  1644. unsigned long batch = zone_batchsize(zone);
  1645. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  1646. #ifdef CONFIG_NUMA
  1647. /* Early boot. Slab allocator not functional yet */
  1648. zone->pageset[cpu] = &boot_pageset[cpu];
  1649. setup_pageset(&boot_pageset[cpu],0);
  1650. #else
  1651. setup_pageset(zone_pcp(zone,cpu), batch);
  1652. #endif
  1653. }
  1654. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
  1655. zone->name, zone->present_pages, batch);
  1656. }
  1657. static __devinit void init_currently_empty_zone(struct zone *zone,
  1658. unsigned long zone_start_pfn, unsigned long size)
  1659. {
  1660. struct pglist_data *pgdat = zone->zone_pgdat;
  1661. zone_wait_table_init(zone, size);
  1662. pgdat->nr_zones = zone_idx(zone) + 1;
  1663. zone->zone_mem_map = pfn_to_page(zone_start_pfn);
  1664. zone->zone_start_pfn = zone_start_pfn;
  1665. memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
  1666. zone_init_free_lists(pgdat, zone, zone->spanned_pages);
  1667. }
  1668. /*
  1669. * Set up the zone data structures:
  1670. * - mark all pages reserved
  1671. * - mark all memory queues empty
  1672. * - clear the memory bitmaps
  1673. */
  1674. static void __init free_area_init_core(struct pglist_data *pgdat,
  1675. unsigned long *zones_size, unsigned long *zholes_size)
  1676. {
  1677. unsigned long j;
  1678. int nid = pgdat->node_id;
  1679. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  1680. pgdat_resize_init(pgdat);
  1681. pgdat->nr_zones = 0;
  1682. init_waitqueue_head(&pgdat->kswapd_wait);
  1683. pgdat->kswapd_max_order = 0;
  1684. for (j = 0; j < MAX_NR_ZONES; j++) {
  1685. struct zone *zone = pgdat->node_zones + j;
  1686. unsigned long size, realsize;
  1687. realsize = size = zones_size[j];
  1688. if (zholes_size)
  1689. realsize -= zholes_size[j];
  1690. if (j == ZONE_DMA || j == ZONE_NORMAL)
  1691. nr_kernel_pages += realsize;
  1692. nr_all_pages += realsize;
  1693. zone->spanned_pages = size;
  1694. zone->present_pages = realsize;
  1695. zone->name = zone_names[j];
  1696. spin_lock_init(&zone->lock);
  1697. spin_lock_init(&zone->lru_lock);
  1698. zone_seqlock_init(zone);
  1699. zone->zone_pgdat = pgdat;
  1700. zone->free_pages = 0;
  1701. zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
  1702. zone_pcp_init(zone);
  1703. INIT_LIST_HEAD(&zone->active_list);
  1704. INIT_LIST_HEAD(&zone->inactive_list);
  1705. zone->nr_scan_active = 0;
  1706. zone->nr_scan_inactive = 0;
  1707. zone->nr_active = 0;
  1708. zone->nr_inactive = 0;
  1709. atomic_set(&zone->reclaim_in_progress, 0);
  1710. if (!size)
  1711. continue;
  1712. zonetable_add(zone, nid, j, zone_start_pfn, size);
  1713. init_currently_empty_zone(zone, zone_start_pfn, size);
  1714. zone_start_pfn += size;
  1715. }
  1716. }
  1717. static void __init alloc_node_mem_map(struct pglist_data *pgdat)
  1718. {
  1719. /* Skip empty nodes */
  1720. if (!pgdat->node_spanned_pages)
  1721. return;
  1722. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1723. /* ia64 gets its own node_mem_map, before this, without bootmem */
  1724. if (!pgdat->node_mem_map) {
  1725. unsigned long size;
  1726. struct page *map;
  1727. size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
  1728. map = alloc_remap(pgdat->node_id, size);
  1729. if (!map)
  1730. map = alloc_bootmem_node(pgdat, size);
  1731. pgdat->node_mem_map = map;
  1732. }
  1733. #ifdef CONFIG_FLATMEM
  1734. /*
  1735. * With no DISCONTIG, the global mem_map is just set as node 0's
  1736. */
  1737. if (pgdat == NODE_DATA(0))
  1738. mem_map = NODE_DATA(0)->node_mem_map;
  1739. #endif
  1740. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  1741. }
  1742. void __init free_area_init_node(int nid, struct pglist_data *pgdat,
  1743. unsigned long *zones_size, unsigned long node_start_pfn,
  1744. unsigned long *zholes_size)
  1745. {
  1746. pgdat->node_id = nid;
  1747. pgdat->node_start_pfn = node_start_pfn;
  1748. calculate_zone_totalpages(pgdat, zones_size, zholes_size);
  1749. alloc_node_mem_map(pgdat);
  1750. free_area_init_core(pgdat, zones_size, zholes_size);
  1751. }
  1752. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1753. static bootmem_data_t contig_bootmem_data;
  1754. struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
  1755. EXPORT_SYMBOL(contig_page_data);
  1756. #endif
  1757. void __init free_area_init(unsigned long *zones_size)
  1758. {
  1759. free_area_init_node(0, NODE_DATA(0), zones_size,
  1760. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  1761. }
  1762. #ifdef CONFIG_PROC_FS
  1763. #include <linux/seq_file.h>
  1764. static void *frag_start(struct seq_file *m, loff_t *pos)
  1765. {
  1766. pg_data_t *pgdat;
  1767. loff_t node = *pos;
  1768. for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
  1769. --node;
  1770. return pgdat;
  1771. }
  1772. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  1773. {
  1774. pg_data_t *pgdat = (pg_data_t *)arg;
  1775. (*pos)++;
  1776. return pgdat->pgdat_next;
  1777. }
  1778. static void frag_stop(struct seq_file *m, void *arg)
  1779. {
  1780. }
  1781. /*
  1782. * This walks the free areas for each zone.
  1783. */
  1784. static int frag_show(struct seq_file *m, void *arg)
  1785. {
  1786. pg_data_t *pgdat = (pg_data_t *)arg;
  1787. struct zone *zone;
  1788. struct zone *node_zones = pgdat->node_zones;
  1789. unsigned long flags;
  1790. int order;
  1791. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  1792. if (!zone->present_pages)
  1793. continue;
  1794. spin_lock_irqsave(&zone->lock, flags);
  1795. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1796. for (order = 0; order < MAX_ORDER; ++order)
  1797. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  1798. spin_unlock_irqrestore(&zone->lock, flags);
  1799. seq_putc(m, '\n');
  1800. }
  1801. return 0;
  1802. }
  1803. struct seq_operations fragmentation_op = {
  1804. .start = frag_start,
  1805. .next = frag_next,
  1806. .stop = frag_stop,
  1807. .show = frag_show,
  1808. };
  1809. /*
  1810. * Output information about zones in @pgdat.
  1811. */
  1812. static int zoneinfo_show(struct seq_file *m, void *arg)
  1813. {
  1814. pg_data_t *pgdat = arg;
  1815. struct zone *zone;
  1816. struct zone *node_zones = pgdat->node_zones;
  1817. unsigned long flags;
  1818. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
  1819. int i;
  1820. if (!zone->present_pages)
  1821. continue;
  1822. spin_lock_irqsave(&zone->lock, flags);
  1823. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  1824. seq_printf(m,
  1825. "\n pages free %lu"
  1826. "\n min %lu"
  1827. "\n low %lu"
  1828. "\n high %lu"
  1829. "\n active %lu"
  1830. "\n inactive %lu"
  1831. "\n scanned %lu (a: %lu i: %lu)"
  1832. "\n spanned %lu"
  1833. "\n present %lu",
  1834. zone->free_pages,
  1835. zone->pages_min,
  1836. zone->pages_low,
  1837. zone->pages_high,
  1838. zone->nr_active,
  1839. zone->nr_inactive,
  1840. zone->pages_scanned,
  1841. zone->nr_scan_active, zone->nr_scan_inactive,
  1842. zone->spanned_pages,
  1843. zone->present_pages);
  1844. seq_printf(m,
  1845. "\n protection: (%lu",
  1846. zone->lowmem_reserve[0]);
  1847. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  1848. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  1849. seq_printf(m,
  1850. ")"
  1851. "\n pagesets");
  1852. for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
  1853. struct per_cpu_pageset *pageset;
  1854. int j;
  1855. pageset = zone_pcp(zone, i);
  1856. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  1857. if (pageset->pcp[j].count)
  1858. break;
  1859. }
  1860. if (j == ARRAY_SIZE(pageset->pcp))
  1861. continue;
  1862. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  1863. seq_printf(m,
  1864. "\n cpu: %i pcp: %i"
  1865. "\n count: %i"
  1866. "\n low: %i"
  1867. "\n high: %i"
  1868. "\n batch: %i",
  1869. i, j,
  1870. pageset->pcp[j].count,
  1871. pageset->pcp[j].low,
  1872. pageset->pcp[j].high,
  1873. pageset->pcp[j].batch);
  1874. }
  1875. #ifdef CONFIG_NUMA
  1876. seq_printf(m,
  1877. "\n numa_hit: %lu"
  1878. "\n numa_miss: %lu"
  1879. "\n numa_foreign: %lu"
  1880. "\n interleave_hit: %lu"
  1881. "\n local_node: %lu"
  1882. "\n other_node: %lu",
  1883. pageset->numa_hit,
  1884. pageset->numa_miss,
  1885. pageset->numa_foreign,
  1886. pageset->interleave_hit,
  1887. pageset->local_node,
  1888. pageset->other_node);
  1889. #endif
  1890. }
  1891. seq_printf(m,
  1892. "\n all_unreclaimable: %u"
  1893. "\n prev_priority: %i"
  1894. "\n temp_priority: %i"
  1895. "\n start_pfn: %lu",
  1896. zone->all_unreclaimable,
  1897. zone->prev_priority,
  1898. zone->temp_priority,
  1899. zone->zone_start_pfn);
  1900. spin_unlock_irqrestore(&zone->lock, flags);
  1901. seq_putc(m, '\n');
  1902. }
  1903. return 0;
  1904. }
  1905. struct seq_operations zoneinfo_op = {
  1906. .start = frag_start, /* iterate over all zones. The same as in
  1907. * fragmentation. */
  1908. .next = frag_next,
  1909. .stop = frag_stop,
  1910. .show = zoneinfo_show,
  1911. };
  1912. static char *vmstat_text[] = {
  1913. "nr_dirty",
  1914. "nr_writeback",
  1915. "nr_unstable",
  1916. "nr_page_table_pages",
  1917. "nr_mapped",
  1918. "nr_slab",
  1919. "pgpgin",
  1920. "pgpgout",
  1921. "pswpin",
  1922. "pswpout",
  1923. "pgalloc_high",
  1924. "pgalloc_normal",
  1925. "pgalloc_dma",
  1926. "pgfree",
  1927. "pgactivate",
  1928. "pgdeactivate",
  1929. "pgfault",
  1930. "pgmajfault",
  1931. "pgrefill_high",
  1932. "pgrefill_normal",
  1933. "pgrefill_dma",
  1934. "pgsteal_high",
  1935. "pgsteal_normal",
  1936. "pgsteal_dma",
  1937. "pgscan_kswapd_high",
  1938. "pgscan_kswapd_normal",
  1939. "pgscan_kswapd_dma",
  1940. "pgscan_direct_high",
  1941. "pgscan_direct_normal",
  1942. "pgscan_direct_dma",
  1943. "pginodesteal",
  1944. "slabs_scanned",
  1945. "kswapd_steal",
  1946. "kswapd_inodesteal",
  1947. "pageoutrun",
  1948. "allocstall",
  1949. "pgrotated",
  1950. "nr_bounce",
  1951. };
  1952. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  1953. {
  1954. struct page_state *ps;
  1955. if (*pos >= ARRAY_SIZE(vmstat_text))
  1956. return NULL;
  1957. ps = kmalloc(sizeof(*ps), GFP_KERNEL);
  1958. m->private = ps;
  1959. if (!ps)
  1960. return ERR_PTR(-ENOMEM);
  1961. get_full_page_state(ps);
  1962. ps->pgpgin /= 2; /* sectors -> kbytes */
  1963. ps->pgpgout /= 2;
  1964. return (unsigned long *)ps + *pos;
  1965. }
  1966. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1967. {
  1968. (*pos)++;
  1969. if (*pos >= ARRAY_SIZE(vmstat_text))
  1970. return NULL;
  1971. return (unsigned long *)m->private + *pos;
  1972. }
  1973. static int vmstat_show(struct seq_file *m, void *arg)
  1974. {
  1975. unsigned long *l = arg;
  1976. unsigned long off = l - (unsigned long *)m->private;
  1977. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  1978. return 0;
  1979. }
  1980. static void vmstat_stop(struct seq_file *m, void *arg)
  1981. {
  1982. kfree(m->private);
  1983. m->private = NULL;
  1984. }
  1985. struct seq_operations vmstat_op = {
  1986. .start = vmstat_start,
  1987. .next = vmstat_next,
  1988. .stop = vmstat_stop,
  1989. .show = vmstat_show,
  1990. };
  1991. #endif /* CONFIG_PROC_FS */
  1992. #ifdef CONFIG_HOTPLUG_CPU
  1993. static int page_alloc_cpu_notify(struct notifier_block *self,
  1994. unsigned long action, void *hcpu)
  1995. {
  1996. int cpu = (unsigned long)hcpu;
  1997. long *count;
  1998. unsigned long *src, *dest;
  1999. if (action == CPU_DEAD) {
  2000. int i;
  2001. /* Drain local pagecache count. */
  2002. count = &per_cpu(nr_pagecache_local, cpu);
  2003. atomic_add(*count, &nr_pagecache);
  2004. *count = 0;
  2005. local_irq_disable();
  2006. __drain_pages(cpu);
  2007. /* Add dead cpu's page_states to our own. */
  2008. dest = (unsigned long *)&__get_cpu_var(page_states);
  2009. src = (unsigned long *)&per_cpu(page_states, cpu);
  2010. for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
  2011. i++) {
  2012. dest[i] += src[i];
  2013. src[i] = 0;
  2014. }
  2015. local_irq_enable();
  2016. }
  2017. return NOTIFY_OK;
  2018. }
  2019. #endif /* CONFIG_HOTPLUG_CPU */
  2020. void __init page_alloc_init(void)
  2021. {
  2022. hotcpu_notifier(page_alloc_cpu_notify, 0);
  2023. }
  2024. /*
  2025. * setup_per_zone_lowmem_reserve - called whenever
  2026. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  2027. * has a correct pages reserved value, so an adequate number of
  2028. * pages are left in the zone after a successful __alloc_pages().
  2029. */
  2030. static void setup_per_zone_lowmem_reserve(void)
  2031. {
  2032. struct pglist_data *pgdat;
  2033. int j, idx;
  2034. for_each_pgdat(pgdat) {
  2035. for (j = 0; j < MAX_NR_ZONES; j++) {
  2036. struct zone *zone = pgdat->node_zones + j;
  2037. unsigned long present_pages = zone->present_pages;
  2038. zone->lowmem_reserve[j] = 0;
  2039. for (idx = j-1; idx >= 0; idx--) {
  2040. struct zone *lower_zone;
  2041. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  2042. sysctl_lowmem_reserve_ratio[idx] = 1;
  2043. lower_zone = pgdat->node_zones + idx;
  2044. lower_zone->lowmem_reserve[j] = present_pages /
  2045. sysctl_lowmem_reserve_ratio[idx];
  2046. present_pages += lower_zone->present_pages;
  2047. }
  2048. }
  2049. }
  2050. }
  2051. /*
  2052. * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
  2053. * that the pages_{min,low,high} values for each zone are set correctly
  2054. * with respect to min_free_kbytes.
  2055. */
  2056. void setup_per_zone_pages_min(void)
  2057. {
  2058. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  2059. unsigned long lowmem_pages = 0;
  2060. struct zone *zone;
  2061. unsigned long flags;
  2062. /* Calculate total number of !ZONE_HIGHMEM pages */
  2063. for_each_zone(zone) {
  2064. if (!is_highmem(zone))
  2065. lowmem_pages += zone->present_pages;
  2066. }
  2067. for_each_zone(zone) {
  2068. spin_lock_irqsave(&zone->lru_lock, flags);
  2069. if (is_highmem(zone)) {
  2070. /*
  2071. * Often, highmem doesn't need to reserve any pages.
  2072. * But the pages_min/low/high values are also used for
  2073. * batching up page reclaim activity so we need a
  2074. * decent value here.
  2075. */
  2076. int min_pages;
  2077. min_pages = zone->present_pages / 1024;
  2078. if (min_pages < SWAP_CLUSTER_MAX)
  2079. min_pages = SWAP_CLUSTER_MAX;
  2080. if (min_pages > 128)
  2081. min_pages = 128;
  2082. zone->pages_min = min_pages;
  2083. } else {
  2084. /* if it's a lowmem zone, reserve a number of pages
  2085. * proportionate to the zone's size.
  2086. */
  2087. zone->pages_min = (pages_min * zone->present_pages) /
  2088. lowmem_pages;
  2089. }
  2090. /*
  2091. * When interpreting these watermarks, just keep in mind that:
  2092. * zone->pages_min == (zone->pages_min * 4) / 4;
  2093. */
  2094. zone->pages_low = (zone->pages_min * 5) / 4;
  2095. zone->pages_high = (zone->pages_min * 6) / 4;
  2096. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2097. }
  2098. }
  2099. /*
  2100. * Initialise min_free_kbytes.
  2101. *
  2102. * For small machines we want it small (128k min). For large machines
  2103. * we want it large (64MB max). But it is not linear, because network
  2104. * bandwidth does not increase linearly with machine size. We use
  2105. *
  2106. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  2107. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  2108. *
  2109. * which yields
  2110. *
  2111. * 16MB: 512k
  2112. * 32MB: 724k
  2113. * 64MB: 1024k
  2114. * 128MB: 1448k
  2115. * 256MB: 2048k
  2116. * 512MB: 2896k
  2117. * 1024MB: 4096k
  2118. * 2048MB: 5792k
  2119. * 4096MB: 8192k
  2120. * 8192MB: 11584k
  2121. * 16384MB: 16384k
  2122. */
  2123. static int __init init_per_zone_pages_min(void)
  2124. {
  2125. unsigned long lowmem_kbytes;
  2126. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  2127. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  2128. if (min_free_kbytes < 128)
  2129. min_free_kbytes = 128;
  2130. if (min_free_kbytes > 65536)
  2131. min_free_kbytes = 65536;
  2132. setup_per_zone_pages_min();
  2133. setup_per_zone_lowmem_reserve();
  2134. return 0;
  2135. }
  2136. module_init(init_per_zone_pages_min)
  2137. /*
  2138. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  2139. * that we can call two helper functions whenever min_free_kbytes
  2140. * changes.
  2141. */
  2142. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  2143. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2144. {
  2145. proc_dointvec(table, write, file, buffer, length, ppos);
  2146. setup_per_zone_pages_min();
  2147. return 0;
  2148. }
  2149. /*
  2150. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  2151. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  2152. * whenever sysctl_lowmem_reserve_ratio changes.
  2153. *
  2154. * The reserve ratio obviously has absolutely no relation with the
  2155. * pages_min watermarks. The lowmem reserve ratio can only make sense
  2156. * if in function of the boot time zone sizes.
  2157. */
  2158. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  2159. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2160. {
  2161. proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2162. setup_per_zone_lowmem_reserve();
  2163. return 0;
  2164. }
  2165. __initdata int hashdist = HASHDIST_DEFAULT;
  2166. #ifdef CONFIG_NUMA
  2167. static int __init set_hashdist(char *str)
  2168. {
  2169. if (!str)
  2170. return 0;
  2171. hashdist = simple_strtoul(str, &str, 0);
  2172. return 1;
  2173. }
  2174. __setup("hashdist=", set_hashdist);
  2175. #endif
  2176. /*
  2177. * allocate a large system hash table from bootmem
  2178. * - it is assumed that the hash table must contain an exact power-of-2
  2179. * quantity of entries
  2180. * - limit is the number of hash buckets, not the total allocation size
  2181. */
  2182. void *__init alloc_large_system_hash(const char *tablename,
  2183. unsigned long bucketsize,
  2184. unsigned long numentries,
  2185. int scale,
  2186. int flags,
  2187. unsigned int *_hash_shift,
  2188. unsigned int *_hash_mask,
  2189. unsigned long limit)
  2190. {
  2191. unsigned long long max = limit;
  2192. unsigned long log2qty, size;
  2193. void *table = NULL;
  2194. /* allow the kernel cmdline to have a say */
  2195. if (!numentries) {
  2196. /* round applicable memory size up to nearest megabyte */
  2197. numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
  2198. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  2199. numentries >>= 20 - PAGE_SHIFT;
  2200. numentries <<= 20 - PAGE_SHIFT;
  2201. /* limit to 1 bucket per 2^scale bytes of low memory */
  2202. if (scale > PAGE_SHIFT)
  2203. numentries >>= (scale - PAGE_SHIFT);
  2204. else
  2205. numentries <<= (PAGE_SHIFT - scale);
  2206. }
  2207. /* rounded up to nearest power of 2 in size */
  2208. numentries = 1UL << (long_log2(numentries) + 1);
  2209. /* limit allocation size to 1/16 total memory by default */
  2210. if (max == 0) {
  2211. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  2212. do_div(max, bucketsize);
  2213. }
  2214. if (numentries > max)
  2215. numentries = max;
  2216. log2qty = long_log2(numentries);
  2217. do {
  2218. size = bucketsize << log2qty;
  2219. if (flags & HASH_EARLY)
  2220. table = alloc_bootmem(size);
  2221. else if (hashdist)
  2222. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  2223. else {
  2224. unsigned long order;
  2225. for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
  2226. ;
  2227. table = (void*) __get_free_pages(GFP_ATOMIC, order);
  2228. }
  2229. } while (!table && size > PAGE_SIZE && --log2qty);
  2230. if (!table)
  2231. panic("Failed to allocate %s hash table\n", tablename);
  2232. printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
  2233. tablename,
  2234. (1U << log2qty),
  2235. long_log2(size) - PAGE_SHIFT,
  2236. size);
  2237. if (_hash_shift)
  2238. *_hash_shift = log2qty;
  2239. if (_hash_mask)
  2240. *_hash_mask = (1 << log2qty) - 1;
  2241. return table;
  2242. }