page_alloc.c 63 KB

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