page_alloc.c 62 KB

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