page_alloc.c 55 KB

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