page_alloc.c 65 KB

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