page_alloc.c 70 KB

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