page_alloc.c 63 KB

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