page_alloc.c 54 KB

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