page_alloc.c 64 KB

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