page_alloc.c 68 KB

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