page_alloc.c 63 KB

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