page_alloc.c 63 KB

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