page_alloc.c 55 KB

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