page_alloc.c 69 KB

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