page_alloc.c 69 KB

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