page_alloc.c 67 KB

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