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