page_alloc.c 70 KB

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