page_alloc.c 62 KB

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