page_alloc.c 65 KB

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