page_alloc.c 68 KB

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