page_alloc.c 72 KB

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