page_alloc.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615
  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_each_cpu(cpu) {
  1140. struct per_cpu_pageset *pageset;
  1141. pageset = zone_pcp(zone, cpu);
  1142. for (temperature = 0; temperature < 2; temperature++)
  1143. printk("cpu %d %s: low %d, high %d, batch %d used:%d\n",
  1144. cpu,
  1145. temperature ? "cold" : "hot",
  1146. pageset->pcp[temperature].low,
  1147. pageset->pcp[temperature].high,
  1148. pageset->pcp[temperature].batch,
  1149. pageset->pcp[temperature].count);
  1150. }
  1151. }
  1152. get_page_state(&ps);
  1153. get_zone_counts(&active, &inactive, &free);
  1154. printk("Free pages: %11ukB (%ukB HighMem)\n",
  1155. K(nr_free_pages()),
  1156. K(nr_free_highpages()));
  1157. printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
  1158. "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
  1159. active,
  1160. inactive,
  1161. ps.nr_dirty,
  1162. ps.nr_writeback,
  1163. ps.nr_unstable,
  1164. nr_free_pages(),
  1165. ps.nr_slab,
  1166. ps.nr_mapped,
  1167. ps.nr_page_table_pages);
  1168. for_each_zone(zone) {
  1169. int i;
  1170. show_node(zone);
  1171. printk("%s"
  1172. " free:%lukB"
  1173. " min:%lukB"
  1174. " low:%lukB"
  1175. " high:%lukB"
  1176. " active:%lukB"
  1177. " inactive:%lukB"
  1178. " present:%lukB"
  1179. " pages_scanned:%lu"
  1180. " all_unreclaimable? %s"
  1181. "\n",
  1182. zone->name,
  1183. K(zone->free_pages),
  1184. K(zone->pages_min),
  1185. K(zone->pages_low),
  1186. K(zone->pages_high),
  1187. K(zone->nr_active),
  1188. K(zone->nr_inactive),
  1189. K(zone->present_pages),
  1190. zone->pages_scanned,
  1191. (zone->all_unreclaimable ? "yes" : "no")
  1192. );
  1193. printk("lowmem_reserve[]:");
  1194. for (i = 0; i < MAX_NR_ZONES; i++)
  1195. printk(" %lu", zone->lowmem_reserve[i]);
  1196. printk("\n");
  1197. }
  1198. for_each_zone(zone) {
  1199. unsigned long nr, flags, order, total = 0;
  1200. show_node(zone);
  1201. printk("%s: ", zone->name);
  1202. if (!zone->present_pages) {
  1203. printk("empty\n");
  1204. continue;
  1205. }
  1206. spin_lock_irqsave(&zone->lock, flags);
  1207. for (order = 0; order < MAX_ORDER; order++) {
  1208. nr = zone->free_area[order].nr_free;
  1209. total += nr << order;
  1210. printk("%lu*%lukB ", nr, K(1UL) << order);
  1211. }
  1212. spin_unlock_irqrestore(&zone->lock, flags);
  1213. printk("= %lukB\n", K(total));
  1214. }
  1215. show_swap_cache_info();
  1216. }
  1217. /*
  1218. * Builds allocation fallback zone lists.
  1219. */
  1220. static int __init build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist, int j, int k)
  1221. {
  1222. switch (k) {
  1223. struct zone *zone;
  1224. default:
  1225. BUG();
  1226. case ZONE_HIGHMEM:
  1227. zone = pgdat->node_zones + ZONE_HIGHMEM;
  1228. if (zone->present_pages) {
  1229. #ifndef CONFIG_HIGHMEM
  1230. BUG();
  1231. #endif
  1232. zonelist->zones[j++] = zone;
  1233. }
  1234. case ZONE_NORMAL:
  1235. zone = pgdat->node_zones + ZONE_NORMAL;
  1236. if (zone->present_pages)
  1237. zonelist->zones[j++] = zone;
  1238. case ZONE_DMA:
  1239. zone = pgdat->node_zones + ZONE_DMA;
  1240. if (zone->present_pages)
  1241. zonelist->zones[j++] = zone;
  1242. }
  1243. return j;
  1244. }
  1245. static inline int highest_zone(int zone_bits)
  1246. {
  1247. int res = ZONE_NORMAL;
  1248. if (zone_bits & (__force int)__GFP_HIGHMEM)
  1249. res = ZONE_HIGHMEM;
  1250. if (zone_bits & (__force int)__GFP_DMA)
  1251. res = ZONE_DMA;
  1252. return res;
  1253. }
  1254. #ifdef CONFIG_NUMA
  1255. #define MAX_NODE_LOAD (num_online_nodes())
  1256. static int __initdata node_load[MAX_NUMNODES];
  1257. /**
  1258. * find_next_best_node - find the next node that should appear in a given node's fallback list
  1259. * @node: node whose fallback list we're appending
  1260. * @used_node_mask: nodemask_t of already used nodes
  1261. *
  1262. * We use a number of factors to determine which is the next node that should
  1263. * appear on a given node's fallback list. The node should not have appeared
  1264. * already in @node's fallback list, and it should be the next closest node
  1265. * according to the distance array (which contains arbitrary distance values
  1266. * from each node to each node in the system), and should also prefer nodes
  1267. * with no CPUs, since presumably they'll have very little allocation pressure
  1268. * on them otherwise.
  1269. * It returns -1 if no node is found.
  1270. */
  1271. static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
  1272. {
  1273. int i, n, val;
  1274. int min_val = INT_MAX;
  1275. int best_node = -1;
  1276. for_each_online_node(i) {
  1277. cpumask_t tmp;
  1278. /* Start from local node */
  1279. n = (node+i) % num_online_nodes();
  1280. /* Don't want a node to appear more than once */
  1281. if (node_isset(n, *used_node_mask))
  1282. continue;
  1283. /* Use the local node if we haven't already */
  1284. if (!node_isset(node, *used_node_mask)) {
  1285. best_node = node;
  1286. break;
  1287. }
  1288. /* Use the distance array to find the distance */
  1289. val = node_distance(node, n);
  1290. /* Give preference to headless and unused nodes */
  1291. tmp = node_to_cpumask(n);
  1292. if (!cpus_empty(tmp))
  1293. val += PENALTY_FOR_NODE_WITH_CPUS;
  1294. /* Slight preference for less loaded node */
  1295. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  1296. val += node_load[n];
  1297. if (val < min_val) {
  1298. min_val = val;
  1299. best_node = n;
  1300. }
  1301. }
  1302. if (best_node >= 0)
  1303. node_set(best_node, *used_node_mask);
  1304. return best_node;
  1305. }
  1306. static void __init build_zonelists(pg_data_t *pgdat)
  1307. {
  1308. int i, j, k, node, local_node;
  1309. int prev_node, load;
  1310. struct zonelist *zonelist;
  1311. nodemask_t used_mask;
  1312. /* initialize zonelists */
  1313. for (i = 0; i < GFP_ZONETYPES; i++) {
  1314. zonelist = pgdat->node_zonelists + i;
  1315. zonelist->zones[0] = NULL;
  1316. }
  1317. /* NUMA-aware ordering of nodes */
  1318. local_node = pgdat->node_id;
  1319. load = num_online_nodes();
  1320. prev_node = local_node;
  1321. nodes_clear(used_mask);
  1322. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  1323. /*
  1324. * We don't want to pressure a particular node.
  1325. * So adding penalty to the first node in same
  1326. * distance group to make it round-robin.
  1327. */
  1328. if (node_distance(local_node, node) !=
  1329. node_distance(local_node, prev_node))
  1330. node_load[node] += load;
  1331. prev_node = node;
  1332. load--;
  1333. for (i = 0; i < GFP_ZONETYPES; i++) {
  1334. zonelist = pgdat->node_zonelists + i;
  1335. for (j = 0; zonelist->zones[j] != NULL; j++);
  1336. k = highest_zone(i);
  1337. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1338. zonelist->zones[j] = NULL;
  1339. }
  1340. }
  1341. }
  1342. #else /* CONFIG_NUMA */
  1343. static void __init build_zonelists(pg_data_t *pgdat)
  1344. {
  1345. int i, j, k, node, local_node;
  1346. local_node = pgdat->node_id;
  1347. for (i = 0; i < GFP_ZONETYPES; i++) {
  1348. struct zonelist *zonelist;
  1349. zonelist = pgdat->node_zonelists + i;
  1350. j = 0;
  1351. k = highest_zone(i);
  1352. j = build_zonelists_node(pgdat, zonelist, j, k);
  1353. /*
  1354. * Now we build the zonelist so that it contains the zones
  1355. * of all the other nodes.
  1356. * We don't want to pressure a particular node, so when
  1357. * building the zones for node N, we make sure that the
  1358. * zones coming right after the local ones are those from
  1359. * node N+1 (modulo N)
  1360. */
  1361. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  1362. if (!node_online(node))
  1363. continue;
  1364. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1365. }
  1366. for (node = 0; node < local_node; node++) {
  1367. if (!node_online(node))
  1368. continue;
  1369. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1370. }
  1371. zonelist->zones[j] = NULL;
  1372. }
  1373. }
  1374. #endif /* CONFIG_NUMA */
  1375. void __init build_all_zonelists(void)
  1376. {
  1377. int i;
  1378. for_each_online_node(i)
  1379. build_zonelists(NODE_DATA(i));
  1380. printk("Built %i zonelists\n", num_online_nodes());
  1381. cpuset_init_current_mems_allowed();
  1382. }
  1383. /*
  1384. * Helper functions to size the waitqueue hash table.
  1385. * Essentially these want to choose hash table sizes sufficiently
  1386. * large so that collisions trying to wait on pages are rare.
  1387. * But in fact, the number of active page waitqueues on typical
  1388. * systems is ridiculously low, less than 200. So this is even
  1389. * conservative, even though it seems large.
  1390. *
  1391. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  1392. * waitqueues, i.e. the size of the waitq table given the number of pages.
  1393. */
  1394. #define PAGES_PER_WAITQUEUE 256
  1395. static inline unsigned long wait_table_size(unsigned long pages)
  1396. {
  1397. unsigned long size = 1;
  1398. pages /= PAGES_PER_WAITQUEUE;
  1399. while (size < pages)
  1400. size <<= 1;
  1401. /*
  1402. * Once we have dozens or even hundreds of threads sleeping
  1403. * on IO we've got bigger problems than wait queue collision.
  1404. * Limit the size of the wait table to a reasonable size.
  1405. */
  1406. size = min(size, 4096UL);
  1407. return max(size, 4UL);
  1408. }
  1409. /*
  1410. * This is an integer logarithm so that shifts can be used later
  1411. * to extract the more random high bits from the multiplicative
  1412. * hash function before the remainder is taken.
  1413. */
  1414. static inline unsigned long wait_table_bits(unsigned long size)
  1415. {
  1416. return ffz(~size);
  1417. }
  1418. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  1419. static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
  1420. unsigned long *zones_size, unsigned long *zholes_size)
  1421. {
  1422. unsigned long realtotalpages, totalpages = 0;
  1423. int i;
  1424. for (i = 0; i < MAX_NR_ZONES; i++)
  1425. totalpages += zones_size[i];
  1426. pgdat->node_spanned_pages = totalpages;
  1427. realtotalpages = totalpages;
  1428. if (zholes_size)
  1429. for (i = 0; i < MAX_NR_ZONES; i++)
  1430. realtotalpages -= zholes_size[i];
  1431. pgdat->node_present_pages = realtotalpages;
  1432. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
  1433. }
  1434. /*
  1435. * Initially all pages are reserved - free ones are freed
  1436. * up by free_all_bootmem() once the early boot process is
  1437. * done. Non-atomic initialization, single-pass.
  1438. */
  1439. void __devinit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  1440. unsigned long start_pfn)
  1441. {
  1442. struct page *page;
  1443. unsigned long end_pfn = start_pfn + size;
  1444. unsigned long pfn;
  1445. for (pfn = start_pfn; pfn < end_pfn; pfn++, page++) {
  1446. if (!early_pfn_valid(pfn))
  1447. continue;
  1448. if (!early_pfn_in_nid(pfn, nid))
  1449. continue;
  1450. page = pfn_to_page(pfn);
  1451. set_page_links(page, zone, nid, pfn);
  1452. set_page_count(page, 1);
  1453. reset_page_mapcount(page);
  1454. SetPageReserved(page);
  1455. INIT_LIST_HEAD(&page->lru);
  1456. #ifdef WANT_PAGE_VIRTUAL
  1457. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1458. if (!is_highmem_idx(zone))
  1459. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1460. #endif
  1461. }
  1462. }
  1463. void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
  1464. unsigned long size)
  1465. {
  1466. int order;
  1467. for (order = 0; order < MAX_ORDER ; order++) {
  1468. INIT_LIST_HEAD(&zone->free_area[order].free_list);
  1469. zone->free_area[order].nr_free = 0;
  1470. }
  1471. }
  1472. #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
  1473. void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
  1474. unsigned long size)
  1475. {
  1476. unsigned long snum = pfn_to_section_nr(pfn);
  1477. unsigned long end = pfn_to_section_nr(pfn + size);
  1478. if (FLAGS_HAS_NODE)
  1479. zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
  1480. else
  1481. for (; snum <= end; snum++)
  1482. zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
  1483. }
  1484. #ifndef __HAVE_ARCH_MEMMAP_INIT
  1485. #define memmap_init(size, nid, zone, start_pfn) \
  1486. memmap_init_zone((size), (nid), (zone), (start_pfn))
  1487. #endif
  1488. static int __devinit zone_batchsize(struct zone *zone)
  1489. {
  1490. int batch;
  1491. /*
  1492. * The per-cpu-pages pools are set to around 1000th of the
  1493. * size of the zone. But no more than 1/2 of a meg.
  1494. *
  1495. * OK, so we don't know how big the cache is. So guess.
  1496. */
  1497. batch = zone->present_pages / 1024;
  1498. if (batch * PAGE_SIZE > 512 * 1024)
  1499. batch = (512 * 1024) / PAGE_SIZE;
  1500. batch /= 4; /* We effectively *= 4 below */
  1501. if (batch < 1)
  1502. batch = 1;
  1503. /*
  1504. * We will be trying to allcoate bigger chunks of contiguous
  1505. * memory of the order of fls(batch). This should result in
  1506. * better cache coloring.
  1507. *
  1508. * A sanity check also to ensure that batch is still in limits.
  1509. */
  1510. batch = (1 << fls(batch + batch/2));
  1511. if (fls(batch) >= (PAGE_SHIFT + MAX_ORDER - 2))
  1512. batch = PAGE_SHIFT + ((MAX_ORDER - 1 - PAGE_SHIFT)/2);
  1513. return batch;
  1514. }
  1515. inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  1516. {
  1517. struct per_cpu_pages *pcp;
  1518. memset(p, 0, sizeof(*p));
  1519. pcp = &p->pcp[0]; /* hot */
  1520. pcp->count = 0;
  1521. pcp->low = 0;
  1522. pcp->high = 6 * batch;
  1523. pcp->batch = max(1UL, 1 * batch);
  1524. INIT_LIST_HEAD(&pcp->list);
  1525. pcp = &p->pcp[1]; /* cold*/
  1526. pcp->count = 0;
  1527. pcp->low = 0;
  1528. pcp->high = 2 * batch;
  1529. pcp->batch = max(1UL, batch/2);
  1530. INIT_LIST_HEAD(&pcp->list);
  1531. }
  1532. #ifdef CONFIG_NUMA
  1533. /*
  1534. * Boot pageset table. One per cpu which is going to be used for all
  1535. * zones and all nodes. The parameters will be set in such a way
  1536. * that an item put on a list will immediately be handed over to
  1537. * the buddy list. This is safe since pageset manipulation is done
  1538. * with interrupts disabled.
  1539. *
  1540. * Some NUMA counter updates may also be caught by the boot pagesets.
  1541. *
  1542. * The boot_pagesets must be kept even after bootup is complete for
  1543. * unused processors and/or zones. They do play a role for bootstrapping
  1544. * hotplugged processors.
  1545. *
  1546. * zoneinfo_show() and maybe other functions do
  1547. * not check if the processor is online before following the pageset pointer.
  1548. * Other parts of the kernel may not check if the zone is available.
  1549. */
  1550. static struct per_cpu_pageset
  1551. boot_pageset[NR_CPUS];
  1552. /*
  1553. * Dynamically allocate memory for the
  1554. * per cpu pageset array in struct zone.
  1555. */
  1556. static int __devinit process_zones(int cpu)
  1557. {
  1558. struct zone *zone, *dzone;
  1559. for_each_zone(zone) {
  1560. zone->pageset[cpu] = kmalloc_node(sizeof(struct per_cpu_pageset),
  1561. GFP_KERNEL, cpu_to_node(cpu));
  1562. if (!zone->pageset[cpu])
  1563. goto bad;
  1564. setup_pageset(zone->pageset[cpu], zone_batchsize(zone));
  1565. }
  1566. return 0;
  1567. bad:
  1568. for_each_zone(dzone) {
  1569. if (dzone == zone)
  1570. break;
  1571. kfree(dzone->pageset[cpu]);
  1572. dzone->pageset[cpu] = NULL;
  1573. }
  1574. return -ENOMEM;
  1575. }
  1576. static inline void free_zone_pagesets(int cpu)
  1577. {
  1578. #ifdef CONFIG_NUMA
  1579. struct zone *zone;
  1580. for_each_zone(zone) {
  1581. struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
  1582. zone_pcp(zone, cpu) = NULL;
  1583. kfree(pset);
  1584. }
  1585. #endif
  1586. }
  1587. static int __devinit pageset_cpuup_callback(struct notifier_block *nfb,
  1588. unsigned long action,
  1589. void *hcpu)
  1590. {
  1591. int cpu = (long)hcpu;
  1592. int ret = NOTIFY_OK;
  1593. switch (action) {
  1594. case CPU_UP_PREPARE:
  1595. if (process_zones(cpu))
  1596. ret = NOTIFY_BAD;
  1597. break;
  1598. #ifdef CONFIG_HOTPLUG_CPU
  1599. case CPU_DEAD:
  1600. free_zone_pagesets(cpu);
  1601. break;
  1602. #endif
  1603. default:
  1604. break;
  1605. }
  1606. return ret;
  1607. }
  1608. static struct notifier_block pageset_notifier =
  1609. { &pageset_cpuup_callback, NULL, 0 };
  1610. void __init setup_per_cpu_pageset()
  1611. {
  1612. int err;
  1613. /* Initialize per_cpu_pageset for cpu 0.
  1614. * A cpuup callback will do this for every cpu
  1615. * as it comes online
  1616. */
  1617. err = process_zones(smp_processor_id());
  1618. BUG_ON(err);
  1619. register_cpu_notifier(&pageset_notifier);
  1620. }
  1621. #endif
  1622. static __devinit
  1623. void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  1624. {
  1625. int i;
  1626. struct pglist_data *pgdat = zone->zone_pgdat;
  1627. /*
  1628. * The per-page waitqueue mechanism uses hashed waitqueues
  1629. * per zone.
  1630. */
  1631. zone->wait_table_size = wait_table_size(zone_size_pages);
  1632. zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
  1633. zone->wait_table = (wait_queue_head_t *)
  1634. alloc_bootmem_node(pgdat, zone->wait_table_size
  1635. * sizeof(wait_queue_head_t));
  1636. for(i = 0; i < zone->wait_table_size; ++i)
  1637. init_waitqueue_head(zone->wait_table + i);
  1638. }
  1639. static __devinit void zone_pcp_init(struct zone *zone)
  1640. {
  1641. int cpu;
  1642. unsigned long batch = zone_batchsize(zone);
  1643. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  1644. #ifdef CONFIG_NUMA
  1645. /* Early boot. Slab allocator not functional yet */
  1646. zone->pageset[cpu] = &boot_pageset[cpu];
  1647. setup_pageset(&boot_pageset[cpu],0);
  1648. #else
  1649. setup_pageset(zone_pcp(zone,cpu), batch);
  1650. #endif
  1651. }
  1652. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
  1653. zone->name, zone->present_pages, batch);
  1654. }
  1655. static __devinit void init_currently_empty_zone(struct zone *zone,
  1656. unsigned long zone_start_pfn, unsigned long size)
  1657. {
  1658. struct pglist_data *pgdat = zone->zone_pgdat;
  1659. zone_wait_table_init(zone, size);
  1660. pgdat->nr_zones = zone_idx(zone) + 1;
  1661. zone->zone_mem_map = pfn_to_page(zone_start_pfn);
  1662. zone->zone_start_pfn = zone_start_pfn;
  1663. memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
  1664. zone_init_free_lists(pgdat, zone, zone->spanned_pages);
  1665. }
  1666. /*
  1667. * Set up the zone data structures:
  1668. * - mark all pages reserved
  1669. * - mark all memory queues empty
  1670. * - clear the memory bitmaps
  1671. */
  1672. static void __init free_area_init_core(struct pglist_data *pgdat,
  1673. unsigned long *zones_size, unsigned long *zholes_size)
  1674. {
  1675. unsigned long j;
  1676. int nid = pgdat->node_id;
  1677. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  1678. pgdat_resize_init(pgdat);
  1679. pgdat->nr_zones = 0;
  1680. init_waitqueue_head(&pgdat->kswapd_wait);
  1681. pgdat->kswapd_max_order = 0;
  1682. for (j = 0; j < MAX_NR_ZONES; j++) {
  1683. struct zone *zone = pgdat->node_zones + j;
  1684. unsigned long size, realsize;
  1685. realsize = size = zones_size[j];
  1686. if (zholes_size)
  1687. realsize -= zholes_size[j];
  1688. if (j == ZONE_DMA || j == ZONE_NORMAL)
  1689. nr_kernel_pages += realsize;
  1690. nr_all_pages += realsize;
  1691. zone->spanned_pages = size;
  1692. zone->present_pages = realsize;
  1693. zone->name = zone_names[j];
  1694. spin_lock_init(&zone->lock);
  1695. spin_lock_init(&zone->lru_lock);
  1696. zone_seqlock_init(zone);
  1697. zone->zone_pgdat = pgdat;
  1698. zone->free_pages = 0;
  1699. zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
  1700. zone_pcp_init(zone);
  1701. INIT_LIST_HEAD(&zone->active_list);
  1702. INIT_LIST_HEAD(&zone->inactive_list);
  1703. zone->nr_scan_active = 0;
  1704. zone->nr_scan_inactive = 0;
  1705. zone->nr_active = 0;
  1706. zone->nr_inactive = 0;
  1707. atomic_set(&zone->reclaim_in_progress, 0);
  1708. if (!size)
  1709. continue;
  1710. zonetable_add(zone, nid, j, zone_start_pfn, size);
  1711. init_currently_empty_zone(zone, zone_start_pfn, size);
  1712. zone_start_pfn += size;
  1713. }
  1714. }
  1715. static void __init alloc_node_mem_map(struct pglist_data *pgdat)
  1716. {
  1717. /* Skip empty nodes */
  1718. if (!pgdat->node_spanned_pages)
  1719. return;
  1720. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1721. /* ia64 gets its own node_mem_map, before this, without bootmem */
  1722. if (!pgdat->node_mem_map) {
  1723. unsigned long size;
  1724. struct page *map;
  1725. size = (pgdat->node_spanned_pages + 1) * sizeof(struct page);
  1726. map = alloc_remap(pgdat->node_id, size);
  1727. if (!map)
  1728. map = alloc_bootmem_node(pgdat, size);
  1729. pgdat->node_mem_map = map;
  1730. }
  1731. #ifdef CONFIG_FLATMEM
  1732. /*
  1733. * With no DISCONTIG, the global mem_map is just set as node 0's
  1734. */
  1735. if (pgdat == NODE_DATA(0))
  1736. mem_map = NODE_DATA(0)->node_mem_map;
  1737. #endif
  1738. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  1739. }
  1740. void __init free_area_init_node(int nid, struct pglist_data *pgdat,
  1741. unsigned long *zones_size, unsigned long node_start_pfn,
  1742. unsigned long *zholes_size)
  1743. {
  1744. pgdat->node_id = nid;
  1745. pgdat->node_start_pfn = node_start_pfn;
  1746. calculate_zone_totalpages(pgdat, zones_size, zholes_size);
  1747. alloc_node_mem_map(pgdat);
  1748. free_area_init_core(pgdat, zones_size, zholes_size);
  1749. }
  1750. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1751. static bootmem_data_t contig_bootmem_data;
  1752. struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
  1753. EXPORT_SYMBOL(contig_page_data);
  1754. #endif
  1755. void __init free_area_init(unsigned long *zones_size)
  1756. {
  1757. free_area_init_node(0, NODE_DATA(0), zones_size,
  1758. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  1759. }
  1760. #ifdef CONFIG_PROC_FS
  1761. #include <linux/seq_file.h>
  1762. static void *frag_start(struct seq_file *m, loff_t *pos)
  1763. {
  1764. pg_data_t *pgdat;
  1765. loff_t node = *pos;
  1766. for (pgdat = pgdat_list; pgdat && node; pgdat = pgdat->pgdat_next)
  1767. --node;
  1768. return pgdat;
  1769. }
  1770. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  1771. {
  1772. pg_data_t *pgdat = (pg_data_t *)arg;
  1773. (*pos)++;
  1774. return pgdat->pgdat_next;
  1775. }
  1776. static void frag_stop(struct seq_file *m, void *arg)
  1777. {
  1778. }
  1779. /*
  1780. * This walks the free areas for each zone.
  1781. */
  1782. static int frag_show(struct seq_file *m, void *arg)
  1783. {
  1784. pg_data_t *pgdat = (pg_data_t *)arg;
  1785. struct zone *zone;
  1786. struct zone *node_zones = pgdat->node_zones;
  1787. unsigned long flags;
  1788. int order;
  1789. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  1790. if (!zone->present_pages)
  1791. continue;
  1792. spin_lock_irqsave(&zone->lock, flags);
  1793. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1794. for (order = 0; order < MAX_ORDER; ++order)
  1795. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  1796. spin_unlock_irqrestore(&zone->lock, flags);
  1797. seq_putc(m, '\n');
  1798. }
  1799. return 0;
  1800. }
  1801. struct seq_operations fragmentation_op = {
  1802. .start = frag_start,
  1803. .next = frag_next,
  1804. .stop = frag_stop,
  1805. .show = frag_show,
  1806. };
  1807. /*
  1808. * Output information about zones in @pgdat.
  1809. */
  1810. static int zoneinfo_show(struct seq_file *m, void *arg)
  1811. {
  1812. pg_data_t *pgdat = arg;
  1813. struct zone *zone;
  1814. struct zone *node_zones = pgdat->node_zones;
  1815. unsigned long flags;
  1816. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
  1817. int i;
  1818. if (!zone->present_pages)
  1819. continue;
  1820. spin_lock_irqsave(&zone->lock, flags);
  1821. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  1822. seq_printf(m,
  1823. "\n pages free %lu"
  1824. "\n min %lu"
  1825. "\n low %lu"
  1826. "\n high %lu"
  1827. "\n active %lu"
  1828. "\n inactive %lu"
  1829. "\n scanned %lu (a: %lu i: %lu)"
  1830. "\n spanned %lu"
  1831. "\n present %lu",
  1832. zone->free_pages,
  1833. zone->pages_min,
  1834. zone->pages_low,
  1835. zone->pages_high,
  1836. zone->nr_active,
  1837. zone->nr_inactive,
  1838. zone->pages_scanned,
  1839. zone->nr_scan_active, zone->nr_scan_inactive,
  1840. zone->spanned_pages,
  1841. zone->present_pages);
  1842. seq_printf(m,
  1843. "\n protection: (%lu",
  1844. zone->lowmem_reserve[0]);
  1845. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  1846. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  1847. seq_printf(m,
  1848. ")"
  1849. "\n pagesets");
  1850. for (i = 0; i < ARRAY_SIZE(zone->pageset); i++) {
  1851. struct per_cpu_pageset *pageset;
  1852. int j;
  1853. pageset = zone_pcp(zone, i);
  1854. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  1855. if (pageset->pcp[j].count)
  1856. break;
  1857. }
  1858. if (j == ARRAY_SIZE(pageset->pcp))
  1859. continue;
  1860. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  1861. seq_printf(m,
  1862. "\n cpu: %i pcp: %i"
  1863. "\n count: %i"
  1864. "\n low: %i"
  1865. "\n high: %i"
  1866. "\n batch: %i",
  1867. i, j,
  1868. pageset->pcp[j].count,
  1869. pageset->pcp[j].low,
  1870. pageset->pcp[j].high,
  1871. pageset->pcp[j].batch);
  1872. }
  1873. #ifdef CONFIG_NUMA
  1874. seq_printf(m,
  1875. "\n numa_hit: %lu"
  1876. "\n numa_miss: %lu"
  1877. "\n numa_foreign: %lu"
  1878. "\n interleave_hit: %lu"
  1879. "\n local_node: %lu"
  1880. "\n other_node: %lu",
  1881. pageset->numa_hit,
  1882. pageset->numa_miss,
  1883. pageset->numa_foreign,
  1884. pageset->interleave_hit,
  1885. pageset->local_node,
  1886. pageset->other_node);
  1887. #endif
  1888. }
  1889. seq_printf(m,
  1890. "\n all_unreclaimable: %u"
  1891. "\n prev_priority: %i"
  1892. "\n temp_priority: %i"
  1893. "\n start_pfn: %lu",
  1894. zone->all_unreclaimable,
  1895. zone->prev_priority,
  1896. zone->temp_priority,
  1897. zone->zone_start_pfn);
  1898. spin_unlock_irqrestore(&zone->lock, flags);
  1899. seq_putc(m, '\n');
  1900. }
  1901. return 0;
  1902. }
  1903. struct seq_operations zoneinfo_op = {
  1904. .start = frag_start, /* iterate over all zones. The same as in
  1905. * fragmentation. */
  1906. .next = frag_next,
  1907. .stop = frag_stop,
  1908. .show = zoneinfo_show,
  1909. };
  1910. static char *vmstat_text[] = {
  1911. "nr_dirty",
  1912. "nr_writeback",
  1913. "nr_unstable",
  1914. "nr_page_table_pages",
  1915. "nr_mapped",
  1916. "nr_slab",
  1917. "pgpgin",
  1918. "pgpgout",
  1919. "pswpin",
  1920. "pswpout",
  1921. "pgalloc_high",
  1922. "pgalloc_normal",
  1923. "pgalloc_dma",
  1924. "pgfree",
  1925. "pgactivate",
  1926. "pgdeactivate",
  1927. "pgfault",
  1928. "pgmajfault",
  1929. "pgrefill_high",
  1930. "pgrefill_normal",
  1931. "pgrefill_dma",
  1932. "pgsteal_high",
  1933. "pgsteal_normal",
  1934. "pgsteal_dma",
  1935. "pgscan_kswapd_high",
  1936. "pgscan_kswapd_normal",
  1937. "pgscan_kswapd_dma",
  1938. "pgscan_direct_high",
  1939. "pgscan_direct_normal",
  1940. "pgscan_direct_dma",
  1941. "pginodesteal",
  1942. "slabs_scanned",
  1943. "kswapd_steal",
  1944. "kswapd_inodesteal",
  1945. "pageoutrun",
  1946. "allocstall",
  1947. "pgrotated",
  1948. "nr_bounce",
  1949. };
  1950. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  1951. {
  1952. struct page_state *ps;
  1953. if (*pos >= ARRAY_SIZE(vmstat_text))
  1954. return NULL;
  1955. ps = kmalloc(sizeof(*ps), GFP_KERNEL);
  1956. m->private = ps;
  1957. if (!ps)
  1958. return ERR_PTR(-ENOMEM);
  1959. get_full_page_state(ps);
  1960. ps->pgpgin /= 2; /* sectors -> kbytes */
  1961. ps->pgpgout /= 2;
  1962. return (unsigned long *)ps + *pos;
  1963. }
  1964. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  1965. {
  1966. (*pos)++;
  1967. if (*pos >= ARRAY_SIZE(vmstat_text))
  1968. return NULL;
  1969. return (unsigned long *)m->private + *pos;
  1970. }
  1971. static int vmstat_show(struct seq_file *m, void *arg)
  1972. {
  1973. unsigned long *l = arg;
  1974. unsigned long off = l - (unsigned long *)m->private;
  1975. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  1976. return 0;
  1977. }
  1978. static void vmstat_stop(struct seq_file *m, void *arg)
  1979. {
  1980. kfree(m->private);
  1981. m->private = NULL;
  1982. }
  1983. struct seq_operations vmstat_op = {
  1984. .start = vmstat_start,
  1985. .next = vmstat_next,
  1986. .stop = vmstat_stop,
  1987. .show = vmstat_show,
  1988. };
  1989. #endif /* CONFIG_PROC_FS */
  1990. #ifdef CONFIG_HOTPLUG_CPU
  1991. static int page_alloc_cpu_notify(struct notifier_block *self,
  1992. unsigned long action, void *hcpu)
  1993. {
  1994. int cpu = (unsigned long)hcpu;
  1995. long *count;
  1996. unsigned long *src, *dest;
  1997. if (action == CPU_DEAD) {
  1998. int i;
  1999. /* Drain local pagecache count. */
  2000. count = &per_cpu(nr_pagecache_local, cpu);
  2001. atomic_add(*count, &nr_pagecache);
  2002. *count = 0;
  2003. local_irq_disable();
  2004. __drain_pages(cpu);
  2005. /* Add dead cpu's page_states to our own. */
  2006. dest = (unsigned long *)&__get_cpu_var(page_states);
  2007. src = (unsigned long *)&per_cpu(page_states, cpu);
  2008. for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
  2009. i++) {
  2010. dest[i] += src[i];
  2011. src[i] = 0;
  2012. }
  2013. local_irq_enable();
  2014. }
  2015. return NOTIFY_OK;
  2016. }
  2017. #endif /* CONFIG_HOTPLUG_CPU */
  2018. void __init page_alloc_init(void)
  2019. {
  2020. hotcpu_notifier(page_alloc_cpu_notify, 0);
  2021. }
  2022. /*
  2023. * setup_per_zone_lowmem_reserve - called whenever
  2024. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  2025. * has a correct pages reserved value, so an adequate number of
  2026. * pages are left in the zone after a successful __alloc_pages().
  2027. */
  2028. static void setup_per_zone_lowmem_reserve(void)
  2029. {
  2030. struct pglist_data *pgdat;
  2031. int j, idx;
  2032. for_each_pgdat(pgdat) {
  2033. for (j = 0; j < MAX_NR_ZONES; j++) {
  2034. struct zone *zone = pgdat->node_zones + j;
  2035. unsigned long present_pages = zone->present_pages;
  2036. zone->lowmem_reserve[j] = 0;
  2037. for (idx = j-1; idx >= 0; idx--) {
  2038. struct zone *lower_zone;
  2039. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  2040. sysctl_lowmem_reserve_ratio[idx] = 1;
  2041. lower_zone = pgdat->node_zones + idx;
  2042. lower_zone->lowmem_reserve[j] = present_pages /
  2043. sysctl_lowmem_reserve_ratio[idx];
  2044. present_pages += lower_zone->present_pages;
  2045. }
  2046. }
  2047. }
  2048. }
  2049. /*
  2050. * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
  2051. * that the pages_{min,low,high} values for each zone are set correctly
  2052. * with respect to min_free_kbytes.
  2053. */
  2054. void setup_per_zone_pages_min(void)
  2055. {
  2056. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  2057. unsigned long lowmem_pages = 0;
  2058. struct zone *zone;
  2059. unsigned long flags;
  2060. /* Calculate total number of !ZONE_HIGHMEM pages */
  2061. for_each_zone(zone) {
  2062. if (!is_highmem(zone))
  2063. lowmem_pages += zone->present_pages;
  2064. }
  2065. for_each_zone(zone) {
  2066. spin_lock_irqsave(&zone->lru_lock, flags);
  2067. if (is_highmem(zone)) {
  2068. /*
  2069. * Often, highmem doesn't need to reserve any pages.
  2070. * But the pages_min/low/high values are also used for
  2071. * batching up page reclaim activity so we need a
  2072. * decent value here.
  2073. */
  2074. int min_pages;
  2075. min_pages = zone->present_pages / 1024;
  2076. if (min_pages < SWAP_CLUSTER_MAX)
  2077. min_pages = SWAP_CLUSTER_MAX;
  2078. if (min_pages > 128)
  2079. min_pages = 128;
  2080. zone->pages_min = min_pages;
  2081. } else {
  2082. /* if it's a lowmem zone, reserve a number of pages
  2083. * proportionate to the zone's size.
  2084. */
  2085. zone->pages_min = (pages_min * zone->present_pages) /
  2086. lowmem_pages;
  2087. }
  2088. /*
  2089. * When interpreting these watermarks, just keep in mind that:
  2090. * zone->pages_min == (zone->pages_min * 4) / 4;
  2091. */
  2092. zone->pages_low = (zone->pages_min * 5) / 4;
  2093. zone->pages_high = (zone->pages_min * 6) / 4;
  2094. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2095. }
  2096. }
  2097. /*
  2098. * Initialise min_free_kbytes.
  2099. *
  2100. * For small machines we want it small (128k min). For large machines
  2101. * we want it large (64MB max). But it is not linear, because network
  2102. * bandwidth does not increase linearly with machine size. We use
  2103. *
  2104. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  2105. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  2106. *
  2107. * which yields
  2108. *
  2109. * 16MB: 512k
  2110. * 32MB: 724k
  2111. * 64MB: 1024k
  2112. * 128MB: 1448k
  2113. * 256MB: 2048k
  2114. * 512MB: 2896k
  2115. * 1024MB: 4096k
  2116. * 2048MB: 5792k
  2117. * 4096MB: 8192k
  2118. * 8192MB: 11584k
  2119. * 16384MB: 16384k
  2120. */
  2121. static int __init init_per_zone_pages_min(void)
  2122. {
  2123. unsigned long lowmem_kbytes;
  2124. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  2125. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  2126. if (min_free_kbytes < 128)
  2127. min_free_kbytes = 128;
  2128. if (min_free_kbytes > 65536)
  2129. min_free_kbytes = 65536;
  2130. setup_per_zone_pages_min();
  2131. setup_per_zone_lowmem_reserve();
  2132. return 0;
  2133. }
  2134. module_init(init_per_zone_pages_min)
  2135. /*
  2136. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  2137. * that we can call two helper functions whenever min_free_kbytes
  2138. * changes.
  2139. */
  2140. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  2141. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2142. {
  2143. proc_dointvec(table, write, file, buffer, length, ppos);
  2144. setup_per_zone_pages_min();
  2145. return 0;
  2146. }
  2147. /*
  2148. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  2149. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  2150. * whenever sysctl_lowmem_reserve_ratio changes.
  2151. *
  2152. * The reserve ratio obviously has absolutely no relation with the
  2153. * pages_min watermarks. The lowmem reserve ratio can only make sense
  2154. * if in function of the boot time zone sizes.
  2155. */
  2156. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  2157. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2158. {
  2159. proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2160. setup_per_zone_lowmem_reserve();
  2161. return 0;
  2162. }
  2163. __initdata int hashdist = HASHDIST_DEFAULT;
  2164. #ifdef CONFIG_NUMA
  2165. static int __init set_hashdist(char *str)
  2166. {
  2167. if (!str)
  2168. return 0;
  2169. hashdist = simple_strtoul(str, &str, 0);
  2170. return 1;
  2171. }
  2172. __setup("hashdist=", set_hashdist);
  2173. #endif
  2174. /*
  2175. * allocate a large system hash table from bootmem
  2176. * - it is assumed that the hash table must contain an exact power-of-2
  2177. * quantity of entries
  2178. * - limit is the number of hash buckets, not the total allocation size
  2179. */
  2180. void *__init alloc_large_system_hash(const char *tablename,
  2181. unsigned long bucketsize,
  2182. unsigned long numentries,
  2183. int scale,
  2184. int flags,
  2185. unsigned int *_hash_shift,
  2186. unsigned int *_hash_mask,
  2187. unsigned long limit)
  2188. {
  2189. unsigned long long max = limit;
  2190. unsigned long log2qty, size;
  2191. void *table = NULL;
  2192. /* allow the kernel cmdline to have a say */
  2193. if (!numentries) {
  2194. /* round applicable memory size up to nearest megabyte */
  2195. numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
  2196. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  2197. numentries >>= 20 - PAGE_SHIFT;
  2198. numentries <<= 20 - PAGE_SHIFT;
  2199. /* limit to 1 bucket per 2^scale bytes of low memory */
  2200. if (scale > PAGE_SHIFT)
  2201. numentries >>= (scale - PAGE_SHIFT);
  2202. else
  2203. numentries <<= (PAGE_SHIFT - scale);
  2204. }
  2205. /* rounded up to nearest power of 2 in size */
  2206. numentries = 1UL << (long_log2(numentries) + 1);
  2207. /* limit allocation size to 1/16 total memory by default */
  2208. if (max == 0) {
  2209. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  2210. do_div(max, bucketsize);
  2211. }
  2212. if (numentries > max)
  2213. numentries = max;
  2214. log2qty = long_log2(numentries);
  2215. do {
  2216. size = bucketsize << log2qty;
  2217. if (flags & HASH_EARLY)
  2218. table = alloc_bootmem(size);
  2219. else if (hashdist)
  2220. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  2221. else {
  2222. unsigned long order;
  2223. for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
  2224. ;
  2225. table = (void*) __get_free_pages(GFP_ATOMIC, order);
  2226. }
  2227. } while (!table && size > PAGE_SIZE && --log2qty);
  2228. if (!table)
  2229. panic("Failed to allocate %s hash table\n", tablename);
  2230. printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
  2231. tablename,
  2232. (1U << log2qty),
  2233. long_log2(size) - PAGE_SHIFT,
  2234. size);
  2235. if (_hash_shift)
  2236. *_hash_shift = log2qty;
  2237. if (_hash_mask)
  2238. *_hash_mask = (1 << log2qty) - 1;
  2239. return table;
  2240. }