page_alloc.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635
  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/stddef.h>
  17. #include <linux/mm.h>
  18. #include <linux/swap.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/compiler.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/suspend.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/slab.h>
  29. #include <linux/notifier.h>
  30. #include <linux/topology.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/cpu.h>
  33. #include <linux/cpuset.h>
  34. #include <linux/memory_hotplug.h>
  35. #include <linux/nodemask.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/mempolicy.h>
  38. #include <linux/stop_machine.h>
  39. #include <linux/sort.h>
  40. #include <linux/pfn.h>
  41. #include <linux/backing-dev.h>
  42. #include <linux/fault-inject.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/div64.h>
  45. #include "internal.h"
  46. /*
  47. * MCD - HACK: Find somewhere to initialize this EARLY, or make this
  48. * initializer cleaner
  49. */
  50. nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
  51. EXPORT_SYMBOL(node_online_map);
  52. nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
  53. EXPORT_SYMBOL(node_possible_map);
  54. unsigned long totalram_pages __read_mostly;
  55. unsigned long totalreserve_pages __read_mostly;
  56. long nr_swap_pages;
  57. int percpu_pagelist_fraction;
  58. static void __free_pages_ok(struct page *page, unsigned int order);
  59. /*
  60. * results with 256, 32 in the lowmem_reserve sysctl:
  61. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  62. * 1G machine -> (16M dma, 784M normal, 224M high)
  63. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  64. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  65. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  66. *
  67. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  68. * don't need any ZONE_NORMAL reservation
  69. */
  70. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
  71. #ifdef CONFIG_ZONE_DMA
  72. 256,
  73. #endif
  74. #ifdef CONFIG_ZONE_DMA32
  75. 256,
  76. #endif
  77. #ifdef CONFIG_HIGHMEM
  78. 32
  79. #endif
  80. };
  81. EXPORT_SYMBOL(totalram_pages);
  82. static char * const zone_names[MAX_NR_ZONES] = {
  83. #ifdef CONFIG_ZONE_DMA
  84. "DMA",
  85. #endif
  86. #ifdef CONFIG_ZONE_DMA32
  87. "DMA32",
  88. #endif
  89. "Normal",
  90. #ifdef CONFIG_HIGHMEM
  91. "HighMem"
  92. #endif
  93. };
  94. int min_free_kbytes = 1024;
  95. unsigned long __meminitdata nr_kernel_pages;
  96. unsigned long __meminitdata nr_all_pages;
  97. static unsigned long __meminitdata dma_reserve;
  98. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  99. /*
  100. * MAX_ACTIVE_REGIONS determines the maxmimum number of distinct
  101. * ranges of memory (RAM) that may be registered with add_active_range().
  102. * Ranges passed to add_active_range() will be merged if possible
  103. * so the number of times add_active_range() can be called is
  104. * related to the number of nodes and the number of holes
  105. */
  106. #ifdef CONFIG_MAX_ACTIVE_REGIONS
  107. /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
  108. #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
  109. #else
  110. #if MAX_NUMNODES >= 32
  111. /* If there can be many nodes, allow up to 50 holes per node */
  112. #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
  113. #else
  114. /* By default, allow up to 256 distinct regions */
  115. #define MAX_ACTIVE_REGIONS 256
  116. #endif
  117. #endif
  118. static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
  119. static int __meminitdata nr_nodemap_entries;
  120. static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
  121. static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
  122. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  123. static unsigned long __meminitdata node_boundary_start_pfn[MAX_NUMNODES];
  124. static unsigned long __meminitdata node_boundary_end_pfn[MAX_NUMNODES];
  125. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  126. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  127. #if MAX_NUMNODES > 1
  128. int nr_node_ids __read_mostly = MAX_NUMNODES;
  129. EXPORT_SYMBOL(nr_node_ids);
  130. #endif
  131. #ifdef CONFIG_DEBUG_VM
  132. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  133. {
  134. int ret = 0;
  135. unsigned seq;
  136. unsigned long pfn = page_to_pfn(page);
  137. do {
  138. seq = zone_span_seqbegin(zone);
  139. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  140. ret = 1;
  141. else if (pfn < zone->zone_start_pfn)
  142. ret = 1;
  143. } while (zone_span_seqretry(zone, seq));
  144. return ret;
  145. }
  146. static int page_is_consistent(struct zone *zone, struct page *page)
  147. {
  148. if (!pfn_valid_within(page_to_pfn(page)))
  149. return 0;
  150. if (zone != page_zone(page))
  151. return 0;
  152. return 1;
  153. }
  154. /*
  155. * Temporary debugging check for pages not lying within a given zone.
  156. */
  157. static int bad_range(struct zone *zone, struct page *page)
  158. {
  159. if (page_outside_zone_boundaries(zone, page))
  160. return 1;
  161. if (!page_is_consistent(zone, page))
  162. return 1;
  163. return 0;
  164. }
  165. #else
  166. static inline int bad_range(struct zone *zone, struct page *page)
  167. {
  168. return 0;
  169. }
  170. #endif
  171. static void bad_page(struct page *page)
  172. {
  173. printk(KERN_EMERG "Bad page state in process '%s'\n"
  174. KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
  175. KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
  176. KERN_EMERG "Backtrace:\n",
  177. current->comm, page, (int)(2*sizeof(unsigned long)),
  178. (unsigned long)page->flags, page->mapping,
  179. page_mapcount(page), page_count(page));
  180. dump_stack();
  181. page->flags &= ~(1 << PG_lru |
  182. 1 << PG_private |
  183. 1 << PG_locked |
  184. 1 << PG_active |
  185. 1 << PG_dirty |
  186. 1 << PG_reclaim |
  187. 1 << PG_slab |
  188. 1 << PG_swapcache |
  189. 1 << PG_writeback |
  190. 1 << PG_buddy );
  191. set_page_count(page, 0);
  192. reset_page_mapcount(page);
  193. page->mapping = NULL;
  194. add_taint(TAINT_BAD_PAGE);
  195. }
  196. /*
  197. * Higher-order pages are called "compound pages". They are structured thusly:
  198. *
  199. * The first PAGE_SIZE page is called the "head page".
  200. *
  201. * The remaining PAGE_SIZE pages are called "tail pages".
  202. *
  203. * All pages have PG_compound set. All pages have their ->private pointing at
  204. * the head page (even the head page has this).
  205. *
  206. * The first tail page's ->lru.next holds the address of the compound page's
  207. * put_page() function. Its ->lru.prev holds the order of allocation.
  208. * This usage means that zero-order pages may not be compound.
  209. */
  210. static void free_compound_page(struct page *page)
  211. {
  212. __free_pages_ok(page, compound_order(page));
  213. }
  214. static void prep_compound_page(struct page *page, unsigned long order)
  215. {
  216. int i;
  217. int nr_pages = 1 << order;
  218. set_compound_page_dtor(page, free_compound_page);
  219. set_compound_order(page, order);
  220. __SetPageHead(page);
  221. for (i = 1; i < nr_pages; i++) {
  222. struct page *p = page + i;
  223. __SetPageTail(p);
  224. p->first_page = page;
  225. }
  226. }
  227. static void destroy_compound_page(struct page *page, unsigned long order)
  228. {
  229. int i;
  230. int nr_pages = 1 << order;
  231. if (unlikely(compound_order(page) != order))
  232. bad_page(page);
  233. if (unlikely(!PageHead(page)))
  234. bad_page(page);
  235. __ClearPageHead(page);
  236. for (i = 1; i < nr_pages; i++) {
  237. struct page *p = page + i;
  238. if (unlikely(!PageTail(p) |
  239. (p->first_page != page)))
  240. bad_page(page);
  241. __ClearPageTail(p);
  242. }
  243. }
  244. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  245. {
  246. int i;
  247. VM_BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
  248. /*
  249. * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
  250. * and __GFP_HIGHMEM from hard or soft interrupt context.
  251. */
  252. VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
  253. for (i = 0; i < (1 << order); i++)
  254. clear_highpage(page + i);
  255. }
  256. /*
  257. * function for dealing with page's order in buddy system.
  258. * zone->lock is already acquired when we use these.
  259. * So, we don't need atomic page->flags operations here.
  260. */
  261. static inline unsigned long page_order(struct page *page)
  262. {
  263. return page_private(page);
  264. }
  265. static inline void set_page_order(struct page *page, int order)
  266. {
  267. set_page_private(page, order);
  268. __SetPageBuddy(page);
  269. }
  270. static inline void rmv_page_order(struct page *page)
  271. {
  272. __ClearPageBuddy(page);
  273. set_page_private(page, 0);
  274. }
  275. /*
  276. * Locate the struct page for both the matching buddy in our
  277. * pair (buddy1) and the combined O(n+1) page they form (page).
  278. *
  279. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  280. * the following equation:
  281. * B2 = B1 ^ (1 << O)
  282. * For example, if the starting buddy (buddy2) is #8 its order
  283. * 1 buddy is #10:
  284. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  285. *
  286. * 2) Any buddy B will have an order O+1 parent P which
  287. * satisfies the following equation:
  288. * P = B & ~(1 << O)
  289. *
  290. * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
  291. */
  292. static inline struct page *
  293. __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
  294. {
  295. unsigned long buddy_idx = page_idx ^ (1 << order);
  296. return page + (buddy_idx - page_idx);
  297. }
  298. static inline unsigned long
  299. __find_combined_index(unsigned long page_idx, unsigned int order)
  300. {
  301. return (page_idx & ~(1 << order));
  302. }
  303. /*
  304. * This function checks whether a page is free && is the buddy
  305. * we can do coalesce a page and its buddy if
  306. * (a) the buddy is not in a hole &&
  307. * (b) the buddy is in the buddy system &&
  308. * (c) a page and its buddy have the same order &&
  309. * (d) a page and its buddy are in the same zone.
  310. *
  311. * For recording whether a page is in the buddy system, we use PG_buddy.
  312. * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
  313. *
  314. * For recording page's order, we use page_private(page).
  315. */
  316. static inline int page_is_buddy(struct page *page, struct page *buddy,
  317. int order)
  318. {
  319. if (!pfn_valid_within(page_to_pfn(buddy)))
  320. return 0;
  321. if (page_zone_id(page) != page_zone_id(buddy))
  322. return 0;
  323. if (PageBuddy(buddy) && page_order(buddy) == order) {
  324. BUG_ON(page_count(buddy) != 0);
  325. return 1;
  326. }
  327. return 0;
  328. }
  329. /*
  330. * Freeing function for a buddy system allocator.
  331. *
  332. * The concept of a buddy system is to maintain direct-mapped table
  333. * (containing bit values) for memory blocks of various "orders".
  334. * The bottom level table contains the map for the smallest allocatable
  335. * units of memory (here, pages), and each level above it describes
  336. * pairs of units from the levels below, hence, "buddies".
  337. * At a high level, all that happens here is marking the table entry
  338. * at the bottom level available, and propagating the changes upward
  339. * as necessary, plus some accounting needed to play nicely with other
  340. * parts of the VM system.
  341. * At each level, we keep a list of pages, which are heads of continuous
  342. * free pages of length of (1 << order) and marked with PG_buddy. Page's
  343. * order is recorded in page_private(page) field.
  344. * So when we are allocating or freeing one, we can derive the state of the
  345. * other. That is, if we allocate a small block, and both were
  346. * free, the remainder of the region must be split into blocks.
  347. * If a block is freed, and its buddy is also free, then this
  348. * triggers coalescing into a block of larger size.
  349. *
  350. * -- wli
  351. */
  352. static inline void __free_one_page(struct page *page,
  353. struct zone *zone, unsigned int order)
  354. {
  355. unsigned long page_idx;
  356. int order_size = 1 << order;
  357. if (unlikely(PageCompound(page)))
  358. destroy_compound_page(page, order);
  359. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  360. VM_BUG_ON(page_idx & (order_size - 1));
  361. VM_BUG_ON(bad_range(zone, page));
  362. __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
  363. while (order < MAX_ORDER-1) {
  364. unsigned long combined_idx;
  365. struct free_area *area;
  366. struct page *buddy;
  367. buddy = __page_find_buddy(page, page_idx, order);
  368. if (!page_is_buddy(page, buddy, order))
  369. break; /* Move the buddy up one level. */
  370. list_del(&buddy->lru);
  371. area = zone->free_area + order;
  372. area->nr_free--;
  373. rmv_page_order(buddy);
  374. combined_idx = __find_combined_index(page_idx, order);
  375. page = page + (combined_idx - page_idx);
  376. page_idx = combined_idx;
  377. order++;
  378. }
  379. set_page_order(page, order);
  380. list_add(&page->lru, &zone->free_area[order].free_list);
  381. zone->free_area[order].nr_free++;
  382. }
  383. static inline int free_pages_check(struct page *page)
  384. {
  385. if (unlikely(page_mapcount(page) |
  386. (page->mapping != NULL) |
  387. (page_count(page) != 0) |
  388. (page->flags & (
  389. 1 << PG_lru |
  390. 1 << PG_private |
  391. 1 << PG_locked |
  392. 1 << PG_active |
  393. 1 << PG_slab |
  394. 1 << PG_swapcache |
  395. 1 << PG_writeback |
  396. 1 << PG_reserved |
  397. 1 << PG_buddy ))))
  398. bad_page(page);
  399. /*
  400. * PageReclaim == PageTail. It is only an error
  401. * for PageReclaim to be set if PageCompound is clear.
  402. */
  403. if (unlikely(!PageCompound(page) && PageReclaim(page)))
  404. bad_page(page);
  405. if (PageDirty(page))
  406. __ClearPageDirty(page);
  407. /*
  408. * For now, we report if PG_reserved was found set, but do not
  409. * clear it, and do not free the page. But we shall soon need
  410. * to do more, for when the ZERO_PAGE count wraps negative.
  411. */
  412. return PageReserved(page);
  413. }
  414. /*
  415. * Frees a list of pages.
  416. * Assumes all pages on list are in same zone, and of same order.
  417. * count is the number of pages to free.
  418. *
  419. * If the zone was previously in an "all pages pinned" state then look to
  420. * see if this freeing clears that state.
  421. *
  422. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  423. * pinned" detection logic.
  424. */
  425. static void free_pages_bulk(struct zone *zone, int count,
  426. struct list_head *list, int order)
  427. {
  428. spin_lock(&zone->lock);
  429. zone->all_unreclaimable = 0;
  430. zone->pages_scanned = 0;
  431. while (count--) {
  432. struct page *page;
  433. VM_BUG_ON(list_empty(list));
  434. page = list_entry(list->prev, struct page, lru);
  435. /* have to delete it as __free_one_page list manipulates */
  436. list_del(&page->lru);
  437. __free_one_page(page, zone, order);
  438. }
  439. spin_unlock(&zone->lock);
  440. }
  441. static void free_one_page(struct zone *zone, struct page *page, int order)
  442. {
  443. spin_lock(&zone->lock);
  444. zone->all_unreclaimable = 0;
  445. zone->pages_scanned = 0;
  446. __free_one_page(page, zone, order);
  447. spin_unlock(&zone->lock);
  448. }
  449. static void __free_pages_ok(struct page *page, unsigned int order)
  450. {
  451. unsigned long flags;
  452. int i;
  453. int reserved = 0;
  454. for (i = 0 ; i < (1 << order) ; ++i)
  455. reserved += free_pages_check(page + i);
  456. if (reserved)
  457. return;
  458. if (!PageHighMem(page))
  459. debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
  460. arch_free_page(page, order);
  461. kernel_map_pages(page, 1 << order, 0);
  462. local_irq_save(flags);
  463. __count_vm_events(PGFREE, 1 << order);
  464. free_one_page(page_zone(page), page, order);
  465. local_irq_restore(flags);
  466. }
  467. /*
  468. * permit the bootmem allocator to evade page validation on high-order frees
  469. */
  470. void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
  471. {
  472. if (order == 0) {
  473. __ClearPageReserved(page);
  474. set_page_count(page, 0);
  475. set_page_refcounted(page);
  476. __free_page(page);
  477. } else {
  478. int loop;
  479. prefetchw(page);
  480. for (loop = 0; loop < BITS_PER_LONG; loop++) {
  481. struct page *p = &page[loop];
  482. if (loop + 1 < BITS_PER_LONG)
  483. prefetchw(p + 1);
  484. __ClearPageReserved(p);
  485. set_page_count(p, 0);
  486. }
  487. set_page_refcounted(page);
  488. __free_pages(page, order);
  489. }
  490. }
  491. /*
  492. * The order of subdivision here is critical for the IO subsystem.
  493. * Please do not alter this order without good reasons and regression
  494. * testing. Specifically, as large blocks of memory are subdivided,
  495. * the order in which smaller blocks are delivered depends on the order
  496. * they're subdivided in this function. This is the primary factor
  497. * influencing the order in which pages are delivered to the IO
  498. * subsystem according to empirical testing, and this is also justified
  499. * by considering the behavior of a buddy system containing a single
  500. * large block of memory acted on by a series of small allocations.
  501. * This behavior is a critical factor in sglist merging's success.
  502. *
  503. * -- wli
  504. */
  505. static inline void expand(struct zone *zone, struct page *page,
  506. int low, int high, struct free_area *area)
  507. {
  508. unsigned long size = 1 << high;
  509. while (high > low) {
  510. area--;
  511. high--;
  512. size >>= 1;
  513. VM_BUG_ON(bad_range(zone, &page[size]));
  514. list_add(&page[size].lru, &area->free_list);
  515. area->nr_free++;
  516. set_page_order(&page[size], high);
  517. }
  518. }
  519. /*
  520. * This page is about to be returned from the page allocator
  521. */
  522. static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
  523. {
  524. if (unlikely(page_mapcount(page) |
  525. (page->mapping != NULL) |
  526. (page_count(page) != 0) |
  527. (page->flags & (
  528. 1 << PG_lru |
  529. 1 << PG_private |
  530. 1 << PG_locked |
  531. 1 << PG_active |
  532. 1 << PG_dirty |
  533. 1 << PG_reclaim |
  534. 1 << PG_slab |
  535. 1 << PG_swapcache |
  536. 1 << PG_writeback |
  537. 1 << PG_reserved |
  538. 1 << PG_buddy ))))
  539. bad_page(page);
  540. /*
  541. * For now, we report if PG_reserved was found set, but do not
  542. * clear it, and do not allocate the page: as a safety net.
  543. */
  544. if (PageReserved(page))
  545. return 1;
  546. page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
  547. 1 << PG_referenced | 1 << PG_arch_1 |
  548. 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk);
  549. set_page_private(page, 0);
  550. set_page_refcounted(page);
  551. arch_alloc_page(page, order);
  552. kernel_map_pages(page, 1 << order, 1);
  553. if (gfp_flags & __GFP_ZERO)
  554. prep_zero_page(page, order, gfp_flags);
  555. if (order && (gfp_flags & __GFP_COMP))
  556. prep_compound_page(page, order);
  557. return 0;
  558. }
  559. /*
  560. * Do the hard work of removing an element from the buddy allocator.
  561. * Call me with the zone->lock already held.
  562. */
  563. static struct page *__rmqueue(struct zone *zone, unsigned int order)
  564. {
  565. struct free_area * area;
  566. unsigned int current_order;
  567. struct page *page;
  568. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  569. area = zone->free_area + current_order;
  570. if (list_empty(&area->free_list))
  571. continue;
  572. page = list_entry(area->free_list.next, struct page, lru);
  573. list_del(&page->lru);
  574. rmv_page_order(page);
  575. area->nr_free--;
  576. __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
  577. expand(zone, page, order, current_order, area);
  578. return page;
  579. }
  580. return NULL;
  581. }
  582. /*
  583. * Obtain a specified number of elements from the buddy allocator, all under
  584. * a single hold of the lock, for efficiency. Add them to the supplied list.
  585. * Returns the number of new pages which were placed at *list.
  586. */
  587. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  588. unsigned long count, struct list_head *list)
  589. {
  590. int i;
  591. spin_lock(&zone->lock);
  592. for (i = 0; i < count; ++i) {
  593. struct page *page = __rmqueue(zone, order);
  594. if (unlikely(page == NULL))
  595. break;
  596. list_add_tail(&page->lru, list);
  597. }
  598. spin_unlock(&zone->lock);
  599. return i;
  600. }
  601. #ifdef CONFIG_NUMA
  602. /*
  603. * Called from the vmstat counter updater to drain pagesets of this
  604. * currently executing processor on remote nodes after they have
  605. * expired.
  606. *
  607. * Note that this function must be called with the thread pinned to
  608. * a single processor.
  609. */
  610. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  611. {
  612. unsigned long flags;
  613. int to_drain;
  614. local_irq_save(flags);
  615. if (pcp->count >= pcp->batch)
  616. to_drain = pcp->batch;
  617. else
  618. to_drain = pcp->count;
  619. free_pages_bulk(zone, to_drain, &pcp->list, 0);
  620. pcp->count -= to_drain;
  621. local_irq_restore(flags);
  622. }
  623. #endif
  624. static void __drain_pages(unsigned int cpu)
  625. {
  626. unsigned long flags;
  627. struct zone *zone;
  628. int i;
  629. for_each_zone(zone) {
  630. struct per_cpu_pageset *pset;
  631. if (!populated_zone(zone))
  632. continue;
  633. pset = zone_pcp(zone, cpu);
  634. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  635. struct per_cpu_pages *pcp;
  636. pcp = &pset->pcp[i];
  637. local_irq_save(flags);
  638. free_pages_bulk(zone, pcp->count, &pcp->list, 0);
  639. pcp->count = 0;
  640. local_irq_restore(flags);
  641. }
  642. }
  643. }
  644. #ifdef CONFIG_PM
  645. void mark_free_pages(struct zone *zone)
  646. {
  647. unsigned long pfn, max_zone_pfn;
  648. unsigned long flags;
  649. int order;
  650. struct list_head *curr;
  651. if (!zone->spanned_pages)
  652. return;
  653. spin_lock_irqsave(&zone->lock, flags);
  654. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  655. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  656. if (pfn_valid(pfn)) {
  657. struct page *page = pfn_to_page(pfn);
  658. if (!swsusp_page_is_forbidden(page))
  659. swsusp_unset_page_free(page);
  660. }
  661. for (order = MAX_ORDER - 1; order >= 0; --order)
  662. list_for_each(curr, &zone->free_area[order].free_list) {
  663. unsigned long i;
  664. pfn = page_to_pfn(list_entry(curr, struct page, lru));
  665. for (i = 0; i < (1UL << order); i++)
  666. swsusp_set_page_free(pfn_to_page(pfn + i));
  667. }
  668. spin_unlock_irqrestore(&zone->lock, flags);
  669. }
  670. /*
  671. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  672. */
  673. void drain_local_pages(void)
  674. {
  675. unsigned long flags;
  676. local_irq_save(flags);
  677. __drain_pages(smp_processor_id());
  678. local_irq_restore(flags);
  679. }
  680. #endif /* CONFIG_PM */
  681. /*
  682. * Free a 0-order page
  683. */
  684. static void fastcall free_hot_cold_page(struct page *page, int cold)
  685. {
  686. struct zone *zone = page_zone(page);
  687. struct per_cpu_pages *pcp;
  688. unsigned long flags;
  689. if (PageAnon(page))
  690. page->mapping = NULL;
  691. if (free_pages_check(page))
  692. return;
  693. if (!PageHighMem(page))
  694. debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
  695. arch_free_page(page, 0);
  696. kernel_map_pages(page, 1, 0);
  697. pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
  698. local_irq_save(flags);
  699. __count_vm_event(PGFREE);
  700. list_add(&page->lru, &pcp->list);
  701. pcp->count++;
  702. if (pcp->count >= pcp->high) {
  703. free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
  704. pcp->count -= pcp->batch;
  705. }
  706. local_irq_restore(flags);
  707. put_cpu();
  708. }
  709. void fastcall free_hot_page(struct page *page)
  710. {
  711. free_hot_cold_page(page, 0);
  712. }
  713. void fastcall free_cold_page(struct page *page)
  714. {
  715. free_hot_cold_page(page, 1);
  716. }
  717. /*
  718. * split_page takes a non-compound higher-order page, and splits it into
  719. * n (1<<order) sub-pages: page[0..n]
  720. * Each sub-page must be freed individually.
  721. *
  722. * Note: this is probably too low level an operation for use in drivers.
  723. * Please consult with lkml before using this in your driver.
  724. */
  725. void split_page(struct page *page, unsigned int order)
  726. {
  727. int i;
  728. VM_BUG_ON(PageCompound(page));
  729. VM_BUG_ON(!page_count(page));
  730. for (i = 1; i < (1 << order); i++)
  731. set_page_refcounted(page + i);
  732. }
  733. /*
  734. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  735. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  736. * or two.
  737. */
  738. static struct page *buffered_rmqueue(struct zonelist *zonelist,
  739. struct zone *zone, int order, gfp_t gfp_flags)
  740. {
  741. unsigned long flags;
  742. struct page *page;
  743. int cold = !!(gfp_flags & __GFP_COLD);
  744. int cpu;
  745. again:
  746. cpu = get_cpu();
  747. if (likely(order == 0)) {
  748. struct per_cpu_pages *pcp;
  749. pcp = &zone_pcp(zone, cpu)->pcp[cold];
  750. local_irq_save(flags);
  751. if (!pcp->count) {
  752. pcp->count = rmqueue_bulk(zone, 0,
  753. pcp->batch, &pcp->list);
  754. if (unlikely(!pcp->count))
  755. goto failed;
  756. }
  757. page = list_entry(pcp->list.next, struct page, lru);
  758. list_del(&page->lru);
  759. pcp->count--;
  760. } else {
  761. spin_lock_irqsave(&zone->lock, flags);
  762. page = __rmqueue(zone, order);
  763. spin_unlock(&zone->lock);
  764. if (!page)
  765. goto failed;
  766. }
  767. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  768. zone_statistics(zonelist, zone);
  769. local_irq_restore(flags);
  770. put_cpu();
  771. VM_BUG_ON(bad_range(zone, page));
  772. if (prep_new_page(page, order, gfp_flags))
  773. goto again;
  774. return page;
  775. failed:
  776. local_irq_restore(flags);
  777. put_cpu();
  778. return NULL;
  779. }
  780. #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
  781. #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
  782. #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
  783. #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
  784. #define ALLOC_HARDER 0x10 /* try to alloc harder */
  785. #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
  786. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  787. #ifdef CONFIG_FAIL_PAGE_ALLOC
  788. static struct fail_page_alloc_attr {
  789. struct fault_attr attr;
  790. u32 ignore_gfp_highmem;
  791. u32 ignore_gfp_wait;
  792. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  793. struct dentry *ignore_gfp_highmem_file;
  794. struct dentry *ignore_gfp_wait_file;
  795. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  796. } fail_page_alloc = {
  797. .attr = FAULT_ATTR_INITIALIZER,
  798. .ignore_gfp_wait = 1,
  799. .ignore_gfp_highmem = 1,
  800. };
  801. static int __init setup_fail_page_alloc(char *str)
  802. {
  803. return setup_fault_attr(&fail_page_alloc.attr, str);
  804. }
  805. __setup("fail_page_alloc=", setup_fail_page_alloc);
  806. static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  807. {
  808. if (gfp_mask & __GFP_NOFAIL)
  809. return 0;
  810. if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
  811. return 0;
  812. if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
  813. return 0;
  814. return should_fail(&fail_page_alloc.attr, 1 << order);
  815. }
  816. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  817. static int __init fail_page_alloc_debugfs(void)
  818. {
  819. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  820. struct dentry *dir;
  821. int err;
  822. err = init_fault_attr_dentries(&fail_page_alloc.attr,
  823. "fail_page_alloc");
  824. if (err)
  825. return err;
  826. dir = fail_page_alloc.attr.dentries.dir;
  827. fail_page_alloc.ignore_gfp_wait_file =
  828. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  829. &fail_page_alloc.ignore_gfp_wait);
  830. fail_page_alloc.ignore_gfp_highmem_file =
  831. debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  832. &fail_page_alloc.ignore_gfp_highmem);
  833. if (!fail_page_alloc.ignore_gfp_wait_file ||
  834. !fail_page_alloc.ignore_gfp_highmem_file) {
  835. err = -ENOMEM;
  836. debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
  837. debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
  838. cleanup_fault_attr_dentries(&fail_page_alloc.attr);
  839. }
  840. return err;
  841. }
  842. late_initcall(fail_page_alloc_debugfs);
  843. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  844. #else /* CONFIG_FAIL_PAGE_ALLOC */
  845. static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  846. {
  847. return 0;
  848. }
  849. #endif /* CONFIG_FAIL_PAGE_ALLOC */
  850. /*
  851. * Return 1 if free pages are above 'mark'. This takes into account the order
  852. * of the allocation.
  853. */
  854. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  855. int classzone_idx, int alloc_flags)
  856. {
  857. /* free_pages my go negative - that's OK */
  858. long min = mark;
  859. long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
  860. int o;
  861. if (alloc_flags & ALLOC_HIGH)
  862. min -= min / 2;
  863. if (alloc_flags & ALLOC_HARDER)
  864. min -= min / 4;
  865. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  866. return 0;
  867. for (o = 0; o < order; o++) {
  868. /* At the next order, this order's pages become unavailable */
  869. free_pages -= z->free_area[o].nr_free << o;
  870. /* Require fewer higher order pages to be free */
  871. min >>= 1;
  872. if (free_pages <= min)
  873. return 0;
  874. }
  875. return 1;
  876. }
  877. #ifdef CONFIG_NUMA
  878. /*
  879. * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
  880. * skip over zones that are not allowed by the cpuset, or that have
  881. * been recently (in last second) found to be nearly full. See further
  882. * comments in mmzone.h. Reduces cache footprint of zonelist scans
  883. * that have to skip over alot of full or unallowed zones.
  884. *
  885. * If the zonelist cache is present in the passed in zonelist, then
  886. * returns a pointer to the allowed node mask (either the current
  887. * tasks mems_allowed, or node_online_map.)
  888. *
  889. * If the zonelist cache is not available for this zonelist, does
  890. * nothing and returns NULL.
  891. *
  892. * If the fullzones BITMAP in the zonelist cache is stale (more than
  893. * a second since last zap'd) then we zap it out (clear its bits.)
  894. *
  895. * We hold off even calling zlc_setup, until after we've checked the
  896. * first zone in the zonelist, on the theory that most allocations will
  897. * be satisfied from that first zone, so best to examine that zone as
  898. * quickly as we can.
  899. */
  900. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  901. {
  902. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  903. nodemask_t *allowednodes; /* zonelist_cache approximation */
  904. zlc = zonelist->zlcache_ptr;
  905. if (!zlc)
  906. return NULL;
  907. if (jiffies - zlc->last_full_zap > 1 * HZ) {
  908. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  909. zlc->last_full_zap = jiffies;
  910. }
  911. allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
  912. &cpuset_current_mems_allowed :
  913. &node_online_map;
  914. return allowednodes;
  915. }
  916. /*
  917. * Given 'z' scanning a zonelist, run a couple of quick checks to see
  918. * if it is worth looking at further for free memory:
  919. * 1) Check that the zone isn't thought to be full (doesn't have its
  920. * bit set in the zonelist_cache fullzones BITMAP).
  921. * 2) Check that the zones node (obtained from the zonelist_cache
  922. * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
  923. * Return true (non-zero) if zone is worth looking at further, or
  924. * else return false (zero) if it is not.
  925. *
  926. * This check -ignores- the distinction between various watermarks,
  927. * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
  928. * found to be full for any variation of these watermarks, it will
  929. * be considered full for up to one second by all requests, unless
  930. * we are so low on memory on all allowed nodes that we are forced
  931. * into the second scan of the zonelist.
  932. *
  933. * In the second scan we ignore this zonelist cache and exactly
  934. * apply the watermarks to all zones, even it is slower to do so.
  935. * We are low on memory in the second scan, and should leave no stone
  936. * unturned looking for a free page.
  937. */
  938. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
  939. nodemask_t *allowednodes)
  940. {
  941. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  942. int i; /* index of *z in zonelist zones */
  943. int n; /* node that zone *z is on */
  944. zlc = zonelist->zlcache_ptr;
  945. if (!zlc)
  946. return 1;
  947. i = z - zonelist->zones;
  948. n = zlc->z_to_n[i];
  949. /* This zone is worth trying if it is allowed but not full */
  950. return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
  951. }
  952. /*
  953. * Given 'z' scanning a zonelist, set the corresponding bit in
  954. * zlc->fullzones, so that subsequent attempts to allocate a page
  955. * from that zone don't waste time re-examining it.
  956. */
  957. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
  958. {
  959. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  960. int i; /* index of *z in zonelist zones */
  961. zlc = zonelist->zlcache_ptr;
  962. if (!zlc)
  963. return;
  964. i = z - zonelist->zones;
  965. set_bit(i, zlc->fullzones);
  966. }
  967. #else /* CONFIG_NUMA */
  968. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  969. {
  970. return NULL;
  971. }
  972. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
  973. nodemask_t *allowednodes)
  974. {
  975. return 1;
  976. }
  977. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
  978. {
  979. }
  980. #endif /* CONFIG_NUMA */
  981. /*
  982. * get_page_from_freelist goes through the zonelist trying to allocate
  983. * a page.
  984. */
  985. static struct page *
  986. get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
  987. struct zonelist *zonelist, int alloc_flags)
  988. {
  989. struct zone **z;
  990. struct page *page = NULL;
  991. int classzone_idx = zone_idx(zonelist->zones[0]);
  992. struct zone *zone;
  993. nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
  994. int zlc_active = 0; /* set if using zonelist_cache */
  995. int did_zlc_setup = 0; /* just call zlc_setup() one time */
  996. zonelist_scan:
  997. /*
  998. * Scan zonelist, looking for a zone with enough free.
  999. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1000. */
  1001. z = zonelist->zones;
  1002. do {
  1003. if (NUMA_BUILD && zlc_active &&
  1004. !zlc_zone_worth_trying(zonelist, z, allowednodes))
  1005. continue;
  1006. zone = *z;
  1007. if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
  1008. zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
  1009. break;
  1010. if ((alloc_flags & ALLOC_CPUSET) &&
  1011. !cpuset_zone_allowed_softwall(zone, gfp_mask))
  1012. goto try_next_zone;
  1013. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  1014. unsigned long mark;
  1015. if (alloc_flags & ALLOC_WMARK_MIN)
  1016. mark = zone->pages_min;
  1017. else if (alloc_flags & ALLOC_WMARK_LOW)
  1018. mark = zone->pages_low;
  1019. else
  1020. mark = zone->pages_high;
  1021. if (!zone_watermark_ok(zone, order, mark,
  1022. classzone_idx, alloc_flags)) {
  1023. if (!zone_reclaim_mode ||
  1024. !zone_reclaim(zone, gfp_mask, order))
  1025. goto this_zone_full;
  1026. }
  1027. }
  1028. page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
  1029. if (page)
  1030. break;
  1031. this_zone_full:
  1032. if (NUMA_BUILD)
  1033. zlc_mark_zone_full(zonelist, z);
  1034. try_next_zone:
  1035. if (NUMA_BUILD && !did_zlc_setup) {
  1036. /* we do zlc_setup after the first zone is tried */
  1037. allowednodes = zlc_setup(zonelist, alloc_flags);
  1038. zlc_active = 1;
  1039. did_zlc_setup = 1;
  1040. }
  1041. } while (*(++z) != NULL);
  1042. if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
  1043. /* Disable zlc cache for second zonelist scan */
  1044. zlc_active = 0;
  1045. goto zonelist_scan;
  1046. }
  1047. return page;
  1048. }
  1049. /*
  1050. * This is the 'heart' of the zoned buddy allocator.
  1051. */
  1052. struct page * fastcall
  1053. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  1054. struct zonelist *zonelist)
  1055. {
  1056. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1057. struct zone **z;
  1058. struct page *page;
  1059. struct reclaim_state reclaim_state;
  1060. struct task_struct *p = current;
  1061. int do_retry;
  1062. int alloc_flags;
  1063. int did_some_progress;
  1064. might_sleep_if(wait);
  1065. if (should_fail_alloc_page(gfp_mask, order))
  1066. return NULL;
  1067. restart:
  1068. z = zonelist->zones; /* the list of zones suitable for gfp_mask */
  1069. if (unlikely(*z == NULL)) {
  1070. /* Should this ever happen?? */
  1071. return NULL;
  1072. }
  1073. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  1074. zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
  1075. if (page)
  1076. goto got_pg;
  1077. /*
  1078. * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
  1079. * __GFP_NOWARN set) should not cause reclaim since the subsystem
  1080. * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
  1081. * using a larger set of nodes after it has established that the
  1082. * allowed per node queues are empty and that nodes are
  1083. * over allocated.
  1084. */
  1085. if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
  1086. goto nopage;
  1087. for (z = zonelist->zones; *z; z++)
  1088. wakeup_kswapd(*z, order);
  1089. /*
  1090. * OK, we're below the kswapd watermark and have kicked background
  1091. * reclaim. Now things get more complex, so set up alloc_flags according
  1092. * to how we want to proceed.
  1093. *
  1094. * The caller may dip into page reserves a bit more if the caller
  1095. * cannot run direct reclaim, or if the caller has realtime scheduling
  1096. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  1097. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  1098. */
  1099. alloc_flags = ALLOC_WMARK_MIN;
  1100. if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
  1101. alloc_flags |= ALLOC_HARDER;
  1102. if (gfp_mask & __GFP_HIGH)
  1103. alloc_flags |= ALLOC_HIGH;
  1104. if (wait)
  1105. alloc_flags |= ALLOC_CPUSET;
  1106. /*
  1107. * Go through the zonelist again. Let __GFP_HIGH and allocations
  1108. * coming from realtime tasks go deeper into reserves.
  1109. *
  1110. * This is the last chance, in general, before the goto nopage.
  1111. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  1112. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1113. */
  1114. page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
  1115. if (page)
  1116. goto got_pg;
  1117. /* This allocation should allow future memory freeing. */
  1118. rebalance:
  1119. if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
  1120. && !in_interrupt()) {
  1121. if (!(gfp_mask & __GFP_NOMEMALLOC)) {
  1122. nofail_alloc:
  1123. /* go through the zonelist yet again, ignoring mins */
  1124. page = get_page_from_freelist(gfp_mask, order,
  1125. zonelist, ALLOC_NO_WATERMARKS);
  1126. if (page)
  1127. goto got_pg;
  1128. if (gfp_mask & __GFP_NOFAIL) {
  1129. congestion_wait(WRITE, HZ/50);
  1130. goto nofail_alloc;
  1131. }
  1132. }
  1133. goto nopage;
  1134. }
  1135. /* Atomic allocations - we can't balance anything */
  1136. if (!wait)
  1137. goto nopage;
  1138. cond_resched();
  1139. /* We now go into synchronous reclaim */
  1140. cpuset_memory_pressure_bump();
  1141. p->flags |= PF_MEMALLOC;
  1142. reclaim_state.reclaimed_slab = 0;
  1143. p->reclaim_state = &reclaim_state;
  1144. did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
  1145. p->reclaim_state = NULL;
  1146. p->flags &= ~PF_MEMALLOC;
  1147. cond_resched();
  1148. if (likely(did_some_progress)) {
  1149. page = get_page_from_freelist(gfp_mask, order,
  1150. zonelist, alloc_flags);
  1151. if (page)
  1152. goto got_pg;
  1153. } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  1154. /*
  1155. * Go through the zonelist yet one more time, keep
  1156. * very high watermark here, this is only to catch
  1157. * a parallel oom killing, we must fail if we're still
  1158. * under heavy pressure.
  1159. */
  1160. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  1161. zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
  1162. if (page)
  1163. goto got_pg;
  1164. out_of_memory(zonelist, gfp_mask, order);
  1165. goto restart;
  1166. }
  1167. /*
  1168. * Don't let big-order allocations loop unless the caller explicitly
  1169. * requests that. Wait for some write requests to complete then retry.
  1170. *
  1171. * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
  1172. * <= 3, but that may not be true in other implementations.
  1173. */
  1174. do_retry = 0;
  1175. if (!(gfp_mask & __GFP_NORETRY)) {
  1176. if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
  1177. do_retry = 1;
  1178. if (gfp_mask & __GFP_NOFAIL)
  1179. do_retry = 1;
  1180. }
  1181. if (do_retry) {
  1182. congestion_wait(WRITE, HZ/50);
  1183. goto rebalance;
  1184. }
  1185. nopage:
  1186. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  1187. printk(KERN_WARNING "%s: page allocation failure."
  1188. " order:%d, mode:0x%x\n",
  1189. p->comm, order, gfp_mask);
  1190. dump_stack();
  1191. show_mem();
  1192. }
  1193. got_pg:
  1194. return page;
  1195. }
  1196. EXPORT_SYMBOL(__alloc_pages);
  1197. /*
  1198. * Common helper functions.
  1199. */
  1200. fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  1201. {
  1202. struct page * page;
  1203. page = alloc_pages(gfp_mask, order);
  1204. if (!page)
  1205. return 0;
  1206. return (unsigned long) page_address(page);
  1207. }
  1208. EXPORT_SYMBOL(__get_free_pages);
  1209. fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
  1210. {
  1211. struct page * page;
  1212. /*
  1213. * get_zeroed_page() returns a 32-bit address, which cannot represent
  1214. * a highmem page
  1215. */
  1216. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1217. page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
  1218. if (page)
  1219. return (unsigned long) page_address(page);
  1220. return 0;
  1221. }
  1222. EXPORT_SYMBOL(get_zeroed_page);
  1223. void __pagevec_free(struct pagevec *pvec)
  1224. {
  1225. int i = pagevec_count(pvec);
  1226. while (--i >= 0)
  1227. free_hot_cold_page(pvec->pages[i], pvec->cold);
  1228. }
  1229. fastcall void __free_pages(struct page *page, unsigned int order)
  1230. {
  1231. if (put_page_testzero(page)) {
  1232. if (order == 0)
  1233. free_hot_page(page);
  1234. else
  1235. __free_pages_ok(page, order);
  1236. }
  1237. }
  1238. EXPORT_SYMBOL(__free_pages);
  1239. fastcall void free_pages(unsigned long addr, unsigned int order)
  1240. {
  1241. if (addr != 0) {
  1242. VM_BUG_ON(!virt_addr_valid((void *)addr));
  1243. __free_pages(virt_to_page((void *)addr), order);
  1244. }
  1245. }
  1246. EXPORT_SYMBOL(free_pages);
  1247. static unsigned int nr_free_zone_pages(int offset)
  1248. {
  1249. /* Just pick one node, since fallback list is circular */
  1250. pg_data_t *pgdat = NODE_DATA(numa_node_id());
  1251. unsigned int sum = 0;
  1252. struct zonelist *zonelist = pgdat->node_zonelists + offset;
  1253. struct zone **zonep = zonelist->zones;
  1254. struct zone *zone;
  1255. for (zone = *zonep++; zone; zone = *zonep++) {
  1256. unsigned long size = zone->present_pages;
  1257. unsigned long high = zone->pages_high;
  1258. if (size > high)
  1259. sum += size - high;
  1260. }
  1261. return sum;
  1262. }
  1263. /*
  1264. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  1265. */
  1266. unsigned int nr_free_buffer_pages(void)
  1267. {
  1268. return nr_free_zone_pages(gfp_zone(GFP_USER));
  1269. }
  1270. /*
  1271. * Amount of free RAM allocatable within all zones
  1272. */
  1273. unsigned int nr_free_pagecache_pages(void)
  1274. {
  1275. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
  1276. }
  1277. static inline void show_node(struct zone *zone)
  1278. {
  1279. if (NUMA_BUILD)
  1280. printk("Node %d ", zone_to_nid(zone));
  1281. }
  1282. void si_meminfo(struct sysinfo *val)
  1283. {
  1284. val->totalram = totalram_pages;
  1285. val->sharedram = 0;
  1286. val->freeram = global_page_state(NR_FREE_PAGES);
  1287. val->bufferram = nr_blockdev_pages();
  1288. val->totalhigh = totalhigh_pages;
  1289. val->freehigh = nr_free_highpages();
  1290. val->mem_unit = PAGE_SIZE;
  1291. }
  1292. EXPORT_SYMBOL(si_meminfo);
  1293. #ifdef CONFIG_NUMA
  1294. void si_meminfo_node(struct sysinfo *val, int nid)
  1295. {
  1296. pg_data_t *pgdat = NODE_DATA(nid);
  1297. val->totalram = pgdat->node_present_pages;
  1298. val->freeram = node_page_state(nid, NR_FREE_PAGES);
  1299. #ifdef CONFIG_HIGHMEM
  1300. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  1301. val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  1302. NR_FREE_PAGES);
  1303. #else
  1304. val->totalhigh = 0;
  1305. val->freehigh = 0;
  1306. #endif
  1307. val->mem_unit = PAGE_SIZE;
  1308. }
  1309. #endif
  1310. #define K(x) ((x) << (PAGE_SHIFT-10))
  1311. /*
  1312. * Show free area list (used inside shift_scroll-lock stuff)
  1313. * We also calculate the percentage fragmentation. We do this by counting the
  1314. * memory on each free list with the exception of the first item on the list.
  1315. */
  1316. void show_free_areas(void)
  1317. {
  1318. int cpu;
  1319. struct zone *zone;
  1320. for_each_zone(zone) {
  1321. if (!populated_zone(zone))
  1322. continue;
  1323. show_node(zone);
  1324. printk("%s per-cpu:\n", zone->name);
  1325. for_each_online_cpu(cpu) {
  1326. struct per_cpu_pageset *pageset;
  1327. pageset = zone_pcp(zone, cpu);
  1328. printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
  1329. "Cold: hi:%5d, btch:%4d usd:%4d\n",
  1330. cpu, pageset->pcp[0].high,
  1331. pageset->pcp[0].batch, pageset->pcp[0].count,
  1332. pageset->pcp[1].high, pageset->pcp[1].batch,
  1333. pageset->pcp[1].count);
  1334. }
  1335. }
  1336. printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu\n"
  1337. " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
  1338. global_page_state(NR_ACTIVE),
  1339. global_page_state(NR_INACTIVE),
  1340. global_page_state(NR_FILE_DIRTY),
  1341. global_page_state(NR_WRITEBACK),
  1342. global_page_state(NR_UNSTABLE_NFS),
  1343. global_page_state(NR_FREE_PAGES),
  1344. global_page_state(NR_SLAB_RECLAIMABLE) +
  1345. global_page_state(NR_SLAB_UNRECLAIMABLE),
  1346. global_page_state(NR_FILE_MAPPED),
  1347. global_page_state(NR_PAGETABLE),
  1348. global_page_state(NR_BOUNCE));
  1349. for_each_zone(zone) {
  1350. int i;
  1351. if (!populated_zone(zone))
  1352. continue;
  1353. show_node(zone);
  1354. printk("%s"
  1355. " free:%lukB"
  1356. " min:%lukB"
  1357. " low:%lukB"
  1358. " high:%lukB"
  1359. " active:%lukB"
  1360. " inactive:%lukB"
  1361. " present:%lukB"
  1362. " pages_scanned:%lu"
  1363. " all_unreclaimable? %s"
  1364. "\n",
  1365. zone->name,
  1366. K(zone_page_state(zone, NR_FREE_PAGES)),
  1367. K(zone->pages_min),
  1368. K(zone->pages_low),
  1369. K(zone->pages_high),
  1370. K(zone_page_state(zone, NR_ACTIVE)),
  1371. K(zone_page_state(zone, NR_INACTIVE)),
  1372. K(zone->present_pages),
  1373. zone->pages_scanned,
  1374. (zone->all_unreclaimable ? "yes" : "no")
  1375. );
  1376. printk("lowmem_reserve[]:");
  1377. for (i = 0; i < MAX_NR_ZONES; i++)
  1378. printk(" %lu", zone->lowmem_reserve[i]);
  1379. printk("\n");
  1380. }
  1381. for_each_zone(zone) {
  1382. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  1383. if (!populated_zone(zone))
  1384. continue;
  1385. show_node(zone);
  1386. printk("%s: ", zone->name);
  1387. spin_lock_irqsave(&zone->lock, flags);
  1388. for (order = 0; order < MAX_ORDER; order++) {
  1389. nr[order] = zone->free_area[order].nr_free;
  1390. total += nr[order] << order;
  1391. }
  1392. spin_unlock_irqrestore(&zone->lock, flags);
  1393. for (order = 0; order < MAX_ORDER; order++)
  1394. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  1395. printk("= %lukB\n", K(total));
  1396. }
  1397. show_swap_cache_info();
  1398. }
  1399. /*
  1400. * Builds allocation fallback zone lists.
  1401. *
  1402. * Add all populated zones of a node to the zonelist.
  1403. */
  1404. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  1405. int nr_zones, enum zone_type zone_type)
  1406. {
  1407. struct zone *zone;
  1408. BUG_ON(zone_type >= MAX_NR_ZONES);
  1409. zone_type++;
  1410. do {
  1411. zone_type--;
  1412. zone = pgdat->node_zones + zone_type;
  1413. if (populated_zone(zone)) {
  1414. zonelist->zones[nr_zones++] = zone;
  1415. check_highest_zone(zone_type);
  1416. }
  1417. } while (zone_type);
  1418. return nr_zones;
  1419. }
  1420. /*
  1421. * zonelist_order:
  1422. * 0 = automatic detection of better ordering.
  1423. * 1 = order by ([node] distance, -zonetype)
  1424. * 2 = order by (-zonetype, [node] distance)
  1425. *
  1426. * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
  1427. * the same zonelist. So only NUMA can configure this param.
  1428. */
  1429. #define ZONELIST_ORDER_DEFAULT 0
  1430. #define ZONELIST_ORDER_NODE 1
  1431. #define ZONELIST_ORDER_ZONE 2
  1432. /* zonelist order in the kernel.
  1433. * set_zonelist_order() will set this to NODE or ZONE.
  1434. */
  1435. static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1436. static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
  1437. #ifdef CONFIG_NUMA
  1438. /* The value user specified ....changed by config */
  1439. static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1440. /* string for sysctl */
  1441. #define NUMA_ZONELIST_ORDER_LEN 16
  1442. char numa_zonelist_order[16] = "default";
  1443. /*
  1444. * interface for configure zonelist ordering.
  1445. * command line option "numa_zonelist_order"
  1446. * = "[dD]efault - default, automatic configuration.
  1447. * = "[nN]ode - order by node locality, then by zone within node
  1448. * = "[zZ]one - order by zone, then by locality within zone
  1449. */
  1450. static int __parse_numa_zonelist_order(char *s)
  1451. {
  1452. if (*s == 'd' || *s == 'D') {
  1453. user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1454. } else if (*s == 'n' || *s == 'N') {
  1455. user_zonelist_order = ZONELIST_ORDER_NODE;
  1456. } else if (*s == 'z' || *s == 'Z') {
  1457. user_zonelist_order = ZONELIST_ORDER_ZONE;
  1458. } else {
  1459. printk(KERN_WARNING
  1460. "Ignoring invalid numa_zonelist_order value: "
  1461. "%s\n", s);
  1462. return -EINVAL;
  1463. }
  1464. return 0;
  1465. }
  1466. static __init int setup_numa_zonelist_order(char *s)
  1467. {
  1468. if (s)
  1469. return __parse_numa_zonelist_order(s);
  1470. return 0;
  1471. }
  1472. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  1473. /*
  1474. * sysctl handler for numa_zonelist_order
  1475. */
  1476. int numa_zonelist_order_handler(ctl_table *table, int write,
  1477. struct file *file, void __user *buffer, size_t *length,
  1478. loff_t *ppos)
  1479. {
  1480. char saved_string[NUMA_ZONELIST_ORDER_LEN];
  1481. int ret;
  1482. if (write)
  1483. strncpy(saved_string, (char*)table->data,
  1484. NUMA_ZONELIST_ORDER_LEN);
  1485. ret = proc_dostring(table, write, file, buffer, length, ppos);
  1486. if (ret)
  1487. return ret;
  1488. if (write) {
  1489. int oldval = user_zonelist_order;
  1490. if (__parse_numa_zonelist_order((char*)table->data)) {
  1491. /*
  1492. * bogus value. restore saved string
  1493. */
  1494. strncpy((char*)table->data, saved_string,
  1495. NUMA_ZONELIST_ORDER_LEN);
  1496. user_zonelist_order = oldval;
  1497. } else if (oldval != user_zonelist_order)
  1498. build_all_zonelists();
  1499. }
  1500. return 0;
  1501. }
  1502. #define MAX_NODE_LOAD (num_online_nodes())
  1503. static int node_load[MAX_NUMNODES];
  1504. /**
  1505. * find_next_best_node - find the next node that should appear in a given node's fallback list
  1506. * @node: node whose fallback list we're appending
  1507. * @used_node_mask: nodemask_t of already used nodes
  1508. *
  1509. * We use a number of factors to determine which is the next node that should
  1510. * appear on a given node's fallback list. The node should not have appeared
  1511. * already in @node's fallback list, and it should be the next closest node
  1512. * according to the distance array (which contains arbitrary distance values
  1513. * from each node to each node in the system), and should also prefer nodes
  1514. * with no CPUs, since presumably they'll have very little allocation pressure
  1515. * on them otherwise.
  1516. * It returns -1 if no node is found.
  1517. */
  1518. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  1519. {
  1520. int n, val;
  1521. int min_val = INT_MAX;
  1522. int best_node = -1;
  1523. /* Use the local node if we haven't already */
  1524. if (!node_isset(node, *used_node_mask)) {
  1525. node_set(node, *used_node_mask);
  1526. return node;
  1527. }
  1528. for_each_online_node(n) {
  1529. cpumask_t tmp;
  1530. /* Don't want a node to appear more than once */
  1531. if (node_isset(n, *used_node_mask))
  1532. continue;
  1533. /* Use the distance array to find the distance */
  1534. val = node_distance(node, n);
  1535. /* Penalize nodes under us ("prefer the next node") */
  1536. val += (n < node);
  1537. /* Give preference to headless and unused nodes */
  1538. tmp = node_to_cpumask(n);
  1539. if (!cpus_empty(tmp))
  1540. val += PENALTY_FOR_NODE_WITH_CPUS;
  1541. /* Slight preference for less loaded node */
  1542. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  1543. val += node_load[n];
  1544. if (val < min_val) {
  1545. min_val = val;
  1546. best_node = n;
  1547. }
  1548. }
  1549. if (best_node >= 0)
  1550. node_set(best_node, *used_node_mask);
  1551. return best_node;
  1552. }
  1553. /*
  1554. * Build zonelists ordered by node and zones within node.
  1555. * This results in maximum locality--normal zone overflows into local
  1556. * DMA zone, if any--but risks exhausting DMA zone.
  1557. */
  1558. static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
  1559. {
  1560. enum zone_type i;
  1561. int j;
  1562. struct zonelist *zonelist;
  1563. for (i = 0; i < MAX_NR_ZONES; i++) {
  1564. zonelist = pgdat->node_zonelists + i;
  1565. for (j = 0; zonelist->zones[j] != NULL; j++)
  1566. ;
  1567. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1568. zonelist->zones[j] = NULL;
  1569. }
  1570. }
  1571. /*
  1572. * Build zonelists ordered by zone and nodes within zones.
  1573. * This results in conserving DMA zone[s] until all Normal memory is
  1574. * exhausted, but results in overflowing to remote node while memory
  1575. * may still exist in local DMA zone.
  1576. */
  1577. static int node_order[MAX_NUMNODES];
  1578. static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
  1579. {
  1580. enum zone_type i;
  1581. int pos, j, node;
  1582. int zone_type; /* needs to be signed */
  1583. struct zone *z;
  1584. struct zonelist *zonelist;
  1585. for (i = 0; i < MAX_NR_ZONES; i++) {
  1586. zonelist = pgdat->node_zonelists + i;
  1587. pos = 0;
  1588. for (zone_type = i; zone_type >= 0; zone_type--) {
  1589. for (j = 0; j < nr_nodes; j++) {
  1590. node = node_order[j];
  1591. z = &NODE_DATA(node)->node_zones[zone_type];
  1592. if (populated_zone(z)) {
  1593. zonelist->zones[pos++] = z;
  1594. check_highest_zone(zone_type);
  1595. }
  1596. }
  1597. }
  1598. zonelist->zones[pos] = NULL;
  1599. }
  1600. }
  1601. static int default_zonelist_order(void)
  1602. {
  1603. int nid, zone_type;
  1604. unsigned long low_kmem_size,total_size;
  1605. struct zone *z;
  1606. int average_size;
  1607. /*
  1608. * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
  1609. * If they are really small and used heavily, the system can fall
  1610. * into OOM very easily.
  1611. * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
  1612. */
  1613. /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
  1614. low_kmem_size = 0;
  1615. total_size = 0;
  1616. for_each_online_node(nid) {
  1617. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  1618. z = &NODE_DATA(nid)->node_zones[zone_type];
  1619. if (populated_zone(z)) {
  1620. if (zone_type < ZONE_NORMAL)
  1621. low_kmem_size += z->present_pages;
  1622. total_size += z->present_pages;
  1623. }
  1624. }
  1625. }
  1626. if (!low_kmem_size || /* there are no DMA area. */
  1627. low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
  1628. return ZONELIST_ORDER_NODE;
  1629. /*
  1630. * look into each node's config.
  1631. * If there is a node whose DMA/DMA32 memory is very big area on
  1632. * local memory, NODE_ORDER may be suitable.
  1633. */
  1634. average_size = total_size / (num_online_nodes() + 1);
  1635. for_each_online_node(nid) {
  1636. low_kmem_size = 0;
  1637. total_size = 0;
  1638. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  1639. z = &NODE_DATA(nid)->node_zones[zone_type];
  1640. if (populated_zone(z)) {
  1641. if (zone_type < ZONE_NORMAL)
  1642. low_kmem_size += z->present_pages;
  1643. total_size += z->present_pages;
  1644. }
  1645. }
  1646. if (low_kmem_size &&
  1647. total_size > average_size && /* ignore small node */
  1648. low_kmem_size > total_size * 70/100)
  1649. return ZONELIST_ORDER_NODE;
  1650. }
  1651. return ZONELIST_ORDER_ZONE;
  1652. }
  1653. static void set_zonelist_order(void)
  1654. {
  1655. if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
  1656. current_zonelist_order = default_zonelist_order();
  1657. else
  1658. current_zonelist_order = user_zonelist_order;
  1659. }
  1660. static void build_zonelists(pg_data_t *pgdat)
  1661. {
  1662. int j, node, load;
  1663. enum zone_type i;
  1664. nodemask_t used_mask;
  1665. int local_node, prev_node;
  1666. struct zonelist *zonelist;
  1667. int order = current_zonelist_order;
  1668. /* initialize zonelists */
  1669. for (i = 0; i < MAX_NR_ZONES; i++) {
  1670. zonelist = pgdat->node_zonelists + i;
  1671. zonelist->zones[0] = NULL;
  1672. }
  1673. /* NUMA-aware ordering of nodes */
  1674. local_node = pgdat->node_id;
  1675. load = num_online_nodes();
  1676. prev_node = local_node;
  1677. nodes_clear(used_mask);
  1678. memset(node_load, 0, sizeof(node_load));
  1679. memset(node_order, 0, sizeof(node_order));
  1680. j = 0;
  1681. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  1682. int distance = node_distance(local_node, node);
  1683. /*
  1684. * If another node is sufficiently far away then it is better
  1685. * to reclaim pages in a zone before going off node.
  1686. */
  1687. if (distance > RECLAIM_DISTANCE)
  1688. zone_reclaim_mode = 1;
  1689. /*
  1690. * We don't want to pressure a particular node.
  1691. * So adding penalty to the first node in same
  1692. * distance group to make it round-robin.
  1693. */
  1694. if (distance != node_distance(local_node, prev_node))
  1695. node_load[node] = load;
  1696. prev_node = node;
  1697. load--;
  1698. if (order == ZONELIST_ORDER_NODE)
  1699. build_zonelists_in_node_order(pgdat, node);
  1700. else
  1701. node_order[j++] = node; /* remember order */
  1702. }
  1703. if (order == ZONELIST_ORDER_ZONE) {
  1704. /* calculate node order -- i.e., DMA last! */
  1705. build_zonelists_in_zone_order(pgdat, j);
  1706. }
  1707. }
  1708. /* Construct the zonelist performance cache - see further mmzone.h */
  1709. static void build_zonelist_cache(pg_data_t *pgdat)
  1710. {
  1711. int i;
  1712. for (i = 0; i < MAX_NR_ZONES; i++) {
  1713. struct zonelist *zonelist;
  1714. struct zonelist_cache *zlc;
  1715. struct zone **z;
  1716. zonelist = pgdat->node_zonelists + i;
  1717. zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
  1718. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  1719. for (z = zonelist->zones; *z; z++)
  1720. zlc->z_to_n[z - zonelist->zones] = zone_to_nid(*z);
  1721. }
  1722. }
  1723. #else /* CONFIG_NUMA */
  1724. static void set_zonelist_order(void)
  1725. {
  1726. current_zonelist_order = ZONELIST_ORDER_ZONE;
  1727. }
  1728. static void build_zonelists(pg_data_t *pgdat)
  1729. {
  1730. int node, local_node;
  1731. enum zone_type i,j;
  1732. local_node = pgdat->node_id;
  1733. for (i = 0; i < MAX_NR_ZONES; i++) {
  1734. struct zonelist *zonelist;
  1735. zonelist = pgdat->node_zonelists + i;
  1736. j = build_zonelists_node(pgdat, zonelist, 0, i);
  1737. /*
  1738. * Now we build the zonelist so that it contains the zones
  1739. * of all the other nodes.
  1740. * We don't want to pressure a particular node, so when
  1741. * building the zones for node N, we make sure that the
  1742. * zones coming right after the local ones are those from
  1743. * node N+1 (modulo N)
  1744. */
  1745. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  1746. if (!node_online(node))
  1747. continue;
  1748. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1749. }
  1750. for (node = 0; node < local_node; node++) {
  1751. if (!node_online(node))
  1752. continue;
  1753. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1754. }
  1755. zonelist->zones[j] = NULL;
  1756. }
  1757. }
  1758. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  1759. static void build_zonelist_cache(pg_data_t *pgdat)
  1760. {
  1761. int i;
  1762. for (i = 0; i < MAX_NR_ZONES; i++)
  1763. pgdat->node_zonelists[i].zlcache_ptr = NULL;
  1764. }
  1765. #endif /* CONFIG_NUMA */
  1766. /* return values int ....just for stop_machine_run() */
  1767. static int __build_all_zonelists(void *dummy)
  1768. {
  1769. int nid;
  1770. for_each_online_node(nid) {
  1771. build_zonelists(NODE_DATA(nid));
  1772. build_zonelist_cache(NODE_DATA(nid));
  1773. }
  1774. return 0;
  1775. }
  1776. void build_all_zonelists(void)
  1777. {
  1778. set_zonelist_order();
  1779. if (system_state == SYSTEM_BOOTING) {
  1780. __build_all_zonelists(NULL);
  1781. cpuset_init_current_mems_allowed();
  1782. } else {
  1783. /* we have to stop all cpus to guaranntee there is no user
  1784. of zonelist */
  1785. stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
  1786. /* cpuset refresh routine should be here */
  1787. }
  1788. vm_total_pages = nr_free_pagecache_pages();
  1789. printk("Built %i zonelists in %s order. Total pages: %ld\n",
  1790. num_online_nodes(),
  1791. zonelist_order_name[current_zonelist_order],
  1792. vm_total_pages);
  1793. #ifdef CONFIG_NUMA
  1794. printk("Policy zone: %s\n", zone_names[policy_zone]);
  1795. #endif
  1796. }
  1797. /*
  1798. * Helper functions to size the waitqueue hash table.
  1799. * Essentially these want to choose hash table sizes sufficiently
  1800. * large so that collisions trying to wait on pages are rare.
  1801. * But in fact, the number of active page waitqueues on typical
  1802. * systems is ridiculously low, less than 200. So this is even
  1803. * conservative, even though it seems large.
  1804. *
  1805. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  1806. * waitqueues, i.e. the size of the waitq table given the number of pages.
  1807. */
  1808. #define PAGES_PER_WAITQUEUE 256
  1809. #ifndef CONFIG_MEMORY_HOTPLUG
  1810. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1811. {
  1812. unsigned long size = 1;
  1813. pages /= PAGES_PER_WAITQUEUE;
  1814. while (size < pages)
  1815. size <<= 1;
  1816. /*
  1817. * Once we have dozens or even hundreds of threads sleeping
  1818. * on IO we've got bigger problems than wait queue collision.
  1819. * Limit the size of the wait table to a reasonable size.
  1820. */
  1821. size = min(size, 4096UL);
  1822. return max(size, 4UL);
  1823. }
  1824. #else
  1825. /*
  1826. * A zone's size might be changed by hot-add, so it is not possible to determine
  1827. * a suitable size for its wait_table. So we use the maximum size now.
  1828. *
  1829. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  1830. *
  1831. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  1832. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  1833. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  1834. *
  1835. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  1836. * or more by the traditional way. (See above). It equals:
  1837. *
  1838. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  1839. * ia64(16K page size) : = ( 8G + 4M)byte.
  1840. * powerpc (64K page size) : = (32G +16M)byte.
  1841. */
  1842. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1843. {
  1844. return 4096UL;
  1845. }
  1846. #endif
  1847. /*
  1848. * This is an integer logarithm so that shifts can be used later
  1849. * to extract the more random high bits from the multiplicative
  1850. * hash function before the remainder is taken.
  1851. */
  1852. static inline unsigned long wait_table_bits(unsigned long size)
  1853. {
  1854. return ffz(~size);
  1855. }
  1856. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  1857. /*
  1858. * Initially all pages are reserved - free ones are freed
  1859. * up by free_all_bootmem() once the early boot process is
  1860. * done. Non-atomic initialization, single-pass.
  1861. */
  1862. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  1863. unsigned long start_pfn, enum memmap_context context)
  1864. {
  1865. struct page *page;
  1866. unsigned long end_pfn = start_pfn + size;
  1867. unsigned long pfn;
  1868. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  1869. /*
  1870. * There can be holes in boot-time mem_map[]s
  1871. * handed to this function. They do not
  1872. * exist on hotplugged memory.
  1873. */
  1874. if (context == MEMMAP_EARLY) {
  1875. if (!early_pfn_valid(pfn))
  1876. continue;
  1877. if (!early_pfn_in_nid(pfn, nid))
  1878. continue;
  1879. }
  1880. page = pfn_to_page(pfn);
  1881. set_page_links(page, zone, nid, pfn);
  1882. init_page_count(page);
  1883. reset_page_mapcount(page);
  1884. SetPageReserved(page);
  1885. INIT_LIST_HEAD(&page->lru);
  1886. #ifdef WANT_PAGE_VIRTUAL
  1887. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1888. if (!is_highmem_idx(zone))
  1889. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1890. #endif
  1891. }
  1892. }
  1893. static void __meminit zone_init_free_lists(struct pglist_data *pgdat,
  1894. struct zone *zone, unsigned long size)
  1895. {
  1896. int order;
  1897. for (order = 0; order < MAX_ORDER ; order++) {
  1898. INIT_LIST_HEAD(&zone->free_area[order].free_list);
  1899. zone->free_area[order].nr_free = 0;
  1900. }
  1901. }
  1902. #ifndef __HAVE_ARCH_MEMMAP_INIT
  1903. #define memmap_init(size, nid, zone, start_pfn) \
  1904. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  1905. #endif
  1906. static int __devinit zone_batchsize(struct zone *zone)
  1907. {
  1908. int batch;
  1909. /*
  1910. * The per-cpu-pages pools are set to around 1000th of the
  1911. * size of the zone. But no more than 1/2 of a meg.
  1912. *
  1913. * OK, so we don't know how big the cache is. So guess.
  1914. */
  1915. batch = zone->present_pages / 1024;
  1916. if (batch * PAGE_SIZE > 512 * 1024)
  1917. batch = (512 * 1024) / PAGE_SIZE;
  1918. batch /= 4; /* We effectively *= 4 below */
  1919. if (batch < 1)
  1920. batch = 1;
  1921. /*
  1922. * Clamp the batch to a 2^n - 1 value. Having a power
  1923. * of 2 value was found to be more likely to have
  1924. * suboptimal cache aliasing properties in some cases.
  1925. *
  1926. * For example if 2 tasks are alternately allocating
  1927. * batches of pages, one task can end up with a lot
  1928. * of pages of one half of the possible page colors
  1929. * and the other with pages of the other colors.
  1930. */
  1931. batch = (1 << (fls(batch + batch/2)-1)) - 1;
  1932. return batch;
  1933. }
  1934. inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  1935. {
  1936. struct per_cpu_pages *pcp;
  1937. memset(p, 0, sizeof(*p));
  1938. pcp = &p->pcp[0]; /* hot */
  1939. pcp->count = 0;
  1940. pcp->high = 6 * batch;
  1941. pcp->batch = max(1UL, 1 * batch);
  1942. INIT_LIST_HEAD(&pcp->list);
  1943. pcp = &p->pcp[1]; /* cold*/
  1944. pcp->count = 0;
  1945. pcp->high = 2 * batch;
  1946. pcp->batch = max(1UL, batch/2);
  1947. INIT_LIST_HEAD(&pcp->list);
  1948. }
  1949. /*
  1950. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  1951. * to the value high for the pageset p.
  1952. */
  1953. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  1954. unsigned long high)
  1955. {
  1956. struct per_cpu_pages *pcp;
  1957. pcp = &p->pcp[0]; /* hot list */
  1958. pcp->high = high;
  1959. pcp->batch = max(1UL, high/4);
  1960. if ((high/4) > (PAGE_SHIFT * 8))
  1961. pcp->batch = PAGE_SHIFT * 8;
  1962. }
  1963. #ifdef CONFIG_NUMA
  1964. /*
  1965. * Boot pageset table. One per cpu which is going to be used for all
  1966. * zones and all nodes. The parameters will be set in such a way
  1967. * that an item put on a list will immediately be handed over to
  1968. * the buddy list. This is safe since pageset manipulation is done
  1969. * with interrupts disabled.
  1970. *
  1971. * Some NUMA counter updates may also be caught by the boot pagesets.
  1972. *
  1973. * The boot_pagesets must be kept even after bootup is complete for
  1974. * unused processors and/or zones. They do play a role for bootstrapping
  1975. * hotplugged processors.
  1976. *
  1977. * zoneinfo_show() and maybe other functions do
  1978. * not check if the processor is online before following the pageset pointer.
  1979. * Other parts of the kernel may not check if the zone is available.
  1980. */
  1981. static struct per_cpu_pageset boot_pageset[NR_CPUS];
  1982. /*
  1983. * Dynamically allocate memory for the
  1984. * per cpu pageset array in struct zone.
  1985. */
  1986. static int __cpuinit process_zones(int cpu)
  1987. {
  1988. struct zone *zone, *dzone;
  1989. for_each_zone(zone) {
  1990. if (!populated_zone(zone))
  1991. continue;
  1992. zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
  1993. GFP_KERNEL, cpu_to_node(cpu));
  1994. if (!zone_pcp(zone, cpu))
  1995. goto bad;
  1996. setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
  1997. if (percpu_pagelist_fraction)
  1998. setup_pagelist_highmark(zone_pcp(zone, cpu),
  1999. (zone->present_pages / percpu_pagelist_fraction));
  2000. }
  2001. return 0;
  2002. bad:
  2003. for_each_zone(dzone) {
  2004. if (dzone == zone)
  2005. break;
  2006. kfree(zone_pcp(dzone, cpu));
  2007. zone_pcp(dzone, cpu) = NULL;
  2008. }
  2009. return -ENOMEM;
  2010. }
  2011. static inline void free_zone_pagesets(int cpu)
  2012. {
  2013. struct zone *zone;
  2014. for_each_zone(zone) {
  2015. struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
  2016. /* Free per_cpu_pageset if it is slab allocated */
  2017. if (pset != &boot_pageset[cpu])
  2018. kfree(pset);
  2019. zone_pcp(zone, cpu) = NULL;
  2020. }
  2021. }
  2022. static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
  2023. unsigned long action,
  2024. void *hcpu)
  2025. {
  2026. int cpu = (long)hcpu;
  2027. int ret = NOTIFY_OK;
  2028. switch (action) {
  2029. case CPU_UP_PREPARE:
  2030. case CPU_UP_PREPARE_FROZEN:
  2031. if (process_zones(cpu))
  2032. ret = NOTIFY_BAD;
  2033. break;
  2034. case CPU_UP_CANCELED:
  2035. case CPU_UP_CANCELED_FROZEN:
  2036. case CPU_DEAD:
  2037. case CPU_DEAD_FROZEN:
  2038. free_zone_pagesets(cpu);
  2039. break;
  2040. default:
  2041. break;
  2042. }
  2043. return ret;
  2044. }
  2045. static struct notifier_block __cpuinitdata pageset_notifier =
  2046. { &pageset_cpuup_callback, NULL, 0 };
  2047. void __init setup_per_cpu_pageset(void)
  2048. {
  2049. int err;
  2050. /* Initialize per_cpu_pageset for cpu 0.
  2051. * A cpuup callback will do this for every cpu
  2052. * as it comes online
  2053. */
  2054. err = process_zones(smp_processor_id());
  2055. BUG_ON(err);
  2056. register_cpu_notifier(&pageset_notifier);
  2057. }
  2058. #endif
  2059. static noinline __init_refok
  2060. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  2061. {
  2062. int i;
  2063. struct pglist_data *pgdat = zone->zone_pgdat;
  2064. size_t alloc_size;
  2065. /*
  2066. * The per-page waitqueue mechanism uses hashed waitqueues
  2067. * per zone.
  2068. */
  2069. zone->wait_table_hash_nr_entries =
  2070. wait_table_hash_nr_entries(zone_size_pages);
  2071. zone->wait_table_bits =
  2072. wait_table_bits(zone->wait_table_hash_nr_entries);
  2073. alloc_size = zone->wait_table_hash_nr_entries
  2074. * sizeof(wait_queue_head_t);
  2075. if (system_state == SYSTEM_BOOTING) {
  2076. zone->wait_table = (wait_queue_head_t *)
  2077. alloc_bootmem_node(pgdat, alloc_size);
  2078. } else {
  2079. /*
  2080. * This case means that a zone whose size was 0 gets new memory
  2081. * via memory hot-add.
  2082. * But it may be the case that a new node was hot-added. In
  2083. * this case vmalloc() will not be able to use this new node's
  2084. * memory - this wait_table must be initialized to use this new
  2085. * node itself as well.
  2086. * To use this new node's memory, further consideration will be
  2087. * necessary.
  2088. */
  2089. zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
  2090. }
  2091. if (!zone->wait_table)
  2092. return -ENOMEM;
  2093. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  2094. init_waitqueue_head(zone->wait_table + i);
  2095. return 0;
  2096. }
  2097. static __meminit void zone_pcp_init(struct zone *zone)
  2098. {
  2099. int cpu;
  2100. unsigned long batch = zone_batchsize(zone);
  2101. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  2102. #ifdef CONFIG_NUMA
  2103. /* Early boot. Slab allocator not functional yet */
  2104. zone_pcp(zone, cpu) = &boot_pageset[cpu];
  2105. setup_pageset(&boot_pageset[cpu],0);
  2106. #else
  2107. setup_pageset(zone_pcp(zone,cpu), batch);
  2108. #endif
  2109. }
  2110. if (zone->present_pages)
  2111. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
  2112. zone->name, zone->present_pages, batch);
  2113. }
  2114. __meminit int init_currently_empty_zone(struct zone *zone,
  2115. unsigned long zone_start_pfn,
  2116. unsigned long size,
  2117. enum memmap_context context)
  2118. {
  2119. struct pglist_data *pgdat = zone->zone_pgdat;
  2120. int ret;
  2121. ret = zone_wait_table_init(zone, size);
  2122. if (ret)
  2123. return ret;
  2124. pgdat->nr_zones = zone_idx(zone) + 1;
  2125. zone->zone_start_pfn = zone_start_pfn;
  2126. memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
  2127. zone_init_free_lists(pgdat, zone, zone->spanned_pages);
  2128. return 0;
  2129. }
  2130. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2131. /*
  2132. * Basic iterator support. Return the first range of PFNs for a node
  2133. * Note: nid == MAX_NUMNODES returns first region regardless of node
  2134. */
  2135. static int __meminit first_active_region_index_in_nid(int nid)
  2136. {
  2137. int i;
  2138. for (i = 0; i < nr_nodemap_entries; i++)
  2139. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  2140. return i;
  2141. return -1;
  2142. }
  2143. /*
  2144. * Basic iterator support. Return the next active range of PFNs for a node
  2145. * Note: nid == MAX_NUMNODES returns next region regardles of node
  2146. */
  2147. static int __meminit next_active_region_index_in_nid(int index, int nid)
  2148. {
  2149. for (index = index + 1; index < nr_nodemap_entries; index++)
  2150. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  2151. return index;
  2152. return -1;
  2153. }
  2154. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  2155. /*
  2156. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  2157. * Architectures may implement their own version but if add_active_range()
  2158. * was used and there are no special requirements, this is a convenient
  2159. * alternative
  2160. */
  2161. int __meminit early_pfn_to_nid(unsigned long pfn)
  2162. {
  2163. int i;
  2164. for (i = 0; i < nr_nodemap_entries; i++) {
  2165. unsigned long start_pfn = early_node_map[i].start_pfn;
  2166. unsigned long end_pfn = early_node_map[i].end_pfn;
  2167. if (start_pfn <= pfn && pfn < end_pfn)
  2168. return early_node_map[i].nid;
  2169. }
  2170. return 0;
  2171. }
  2172. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  2173. /* Basic iterator support to walk early_node_map[] */
  2174. #define for_each_active_range_index_in_nid(i, nid) \
  2175. for (i = first_active_region_index_in_nid(nid); i != -1; \
  2176. i = next_active_region_index_in_nid(i, nid))
  2177. /**
  2178. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  2179. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  2180. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  2181. *
  2182. * If an architecture guarantees that all ranges registered with
  2183. * add_active_ranges() contain no holes and may be freed, this
  2184. * this function may be used instead of calling free_bootmem() manually.
  2185. */
  2186. void __init free_bootmem_with_active_regions(int nid,
  2187. unsigned long max_low_pfn)
  2188. {
  2189. int i;
  2190. for_each_active_range_index_in_nid(i, nid) {
  2191. unsigned long size_pages = 0;
  2192. unsigned long end_pfn = early_node_map[i].end_pfn;
  2193. if (early_node_map[i].start_pfn >= max_low_pfn)
  2194. continue;
  2195. if (end_pfn > max_low_pfn)
  2196. end_pfn = max_low_pfn;
  2197. size_pages = end_pfn - early_node_map[i].start_pfn;
  2198. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  2199. PFN_PHYS(early_node_map[i].start_pfn),
  2200. size_pages << PAGE_SHIFT);
  2201. }
  2202. }
  2203. /**
  2204. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  2205. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  2206. *
  2207. * If an architecture guarantees that all ranges registered with
  2208. * add_active_ranges() contain no holes and may be freed, this
  2209. * function may be used instead of calling memory_present() manually.
  2210. */
  2211. void __init sparse_memory_present_with_active_regions(int nid)
  2212. {
  2213. int i;
  2214. for_each_active_range_index_in_nid(i, nid)
  2215. memory_present(early_node_map[i].nid,
  2216. early_node_map[i].start_pfn,
  2217. early_node_map[i].end_pfn);
  2218. }
  2219. /**
  2220. * push_node_boundaries - Push node boundaries to at least the requested boundary
  2221. * @nid: The nid of the node to push the boundary for
  2222. * @start_pfn: The start pfn of the node
  2223. * @end_pfn: The end pfn of the node
  2224. *
  2225. * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
  2226. * time. Specifically, on x86_64, SRAT will report ranges that can potentially
  2227. * be hotplugged even though no physical memory exists. This function allows
  2228. * an arch to push out the node boundaries so mem_map is allocated that can
  2229. * be used later.
  2230. */
  2231. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  2232. void __init push_node_boundaries(unsigned int nid,
  2233. unsigned long start_pfn, unsigned long end_pfn)
  2234. {
  2235. printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
  2236. nid, start_pfn, end_pfn);
  2237. /* Initialise the boundary for this node if necessary */
  2238. if (node_boundary_end_pfn[nid] == 0)
  2239. node_boundary_start_pfn[nid] = -1UL;
  2240. /* Update the boundaries */
  2241. if (node_boundary_start_pfn[nid] > start_pfn)
  2242. node_boundary_start_pfn[nid] = start_pfn;
  2243. if (node_boundary_end_pfn[nid] < end_pfn)
  2244. node_boundary_end_pfn[nid] = end_pfn;
  2245. }
  2246. /* If necessary, push the node boundary out for reserve hotadd */
  2247. static void __meminit account_node_boundary(unsigned int nid,
  2248. unsigned long *start_pfn, unsigned long *end_pfn)
  2249. {
  2250. printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
  2251. nid, *start_pfn, *end_pfn);
  2252. /* Return if boundary information has not been provided */
  2253. if (node_boundary_end_pfn[nid] == 0)
  2254. return;
  2255. /* Check the boundaries and update if necessary */
  2256. if (node_boundary_start_pfn[nid] < *start_pfn)
  2257. *start_pfn = node_boundary_start_pfn[nid];
  2258. if (node_boundary_end_pfn[nid] > *end_pfn)
  2259. *end_pfn = node_boundary_end_pfn[nid];
  2260. }
  2261. #else
  2262. void __init push_node_boundaries(unsigned int nid,
  2263. unsigned long start_pfn, unsigned long end_pfn) {}
  2264. static void __meminit account_node_boundary(unsigned int nid,
  2265. unsigned long *start_pfn, unsigned long *end_pfn) {}
  2266. #endif
  2267. /**
  2268. * get_pfn_range_for_nid - Return the start and end page frames for a node
  2269. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  2270. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  2271. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  2272. *
  2273. * It returns the start and end page frame of a node based on information
  2274. * provided by an arch calling add_active_range(). If called for a node
  2275. * with no available memory, a warning is printed and the start and end
  2276. * PFNs will be 0.
  2277. */
  2278. void __meminit get_pfn_range_for_nid(unsigned int nid,
  2279. unsigned long *start_pfn, unsigned long *end_pfn)
  2280. {
  2281. int i;
  2282. *start_pfn = -1UL;
  2283. *end_pfn = 0;
  2284. for_each_active_range_index_in_nid(i, nid) {
  2285. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  2286. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  2287. }
  2288. if (*start_pfn == -1UL) {
  2289. printk(KERN_WARNING "Node %u active with no memory\n", nid);
  2290. *start_pfn = 0;
  2291. }
  2292. /* Push the node boundaries out if requested */
  2293. account_node_boundary(nid, start_pfn, end_pfn);
  2294. }
  2295. /*
  2296. * Return the number of pages a zone spans in a node, including holes
  2297. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  2298. */
  2299. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  2300. unsigned long zone_type,
  2301. unsigned long *ignored)
  2302. {
  2303. unsigned long node_start_pfn, node_end_pfn;
  2304. unsigned long zone_start_pfn, zone_end_pfn;
  2305. /* Get the start and end of the node and zone */
  2306. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  2307. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  2308. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  2309. /* Check that this node has pages within the zone's required range */
  2310. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  2311. return 0;
  2312. /* Move the zone boundaries inside the node if necessary */
  2313. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  2314. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  2315. /* Return the spanned pages */
  2316. return zone_end_pfn - zone_start_pfn;
  2317. }
  2318. /*
  2319. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  2320. * then all holes in the requested range will be accounted for.
  2321. */
  2322. unsigned long __meminit __absent_pages_in_range(int nid,
  2323. unsigned long range_start_pfn,
  2324. unsigned long range_end_pfn)
  2325. {
  2326. int i = 0;
  2327. unsigned long prev_end_pfn = 0, hole_pages = 0;
  2328. unsigned long start_pfn;
  2329. /* Find the end_pfn of the first active range of pfns in the node */
  2330. i = first_active_region_index_in_nid(nid);
  2331. if (i == -1)
  2332. return 0;
  2333. /* Account for ranges before physical memory on this node */
  2334. if (early_node_map[i].start_pfn > range_start_pfn)
  2335. hole_pages = early_node_map[i].start_pfn - range_start_pfn;
  2336. prev_end_pfn = early_node_map[i].start_pfn;
  2337. /* Find all holes for the zone within the node */
  2338. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  2339. /* No need to continue if prev_end_pfn is outside the zone */
  2340. if (prev_end_pfn >= range_end_pfn)
  2341. break;
  2342. /* Make sure the end of the zone is not within the hole */
  2343. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  2344. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  2345. /* Update the hole size cound and move on */
  2346. if (start_pfn > range_start_pfn) {
  2347. BUG_ON(prev_end_pfn > start_pfn);
  2348. hole_pages += start_pfn - prev_end_pfn;
  2349. }
  2350. prev_end_pfn = early_node_map[i].end_pfn;
  2351. }
  2352. /* Account for ranges past physical memory on this node */
  2353. if (range_end_pfn > prev_end_pfn)
  2354. hole_pages += range_end_pfn -
  2355. max(range_start_pfn, prev_end_pfn);
  2356. return hole_pages;
  2357. }
  2358. /**
  2359. * absent_pages_in_range - Return number of page frames in holes within a range
  2360. * @start_pfn: The start PFN to start searching for holes
  2361. * @end_pfn: The end PFN to stop searching for holes
  2362. *
  2363. * It returns the number of pages frames in memory holes within a range.
  2364. */
  2365. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  2366. unsigned long end_pfn)
  2367. {
  2368. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  2369. }
  2370. /* Return the number of page frames in holes in a zone on a node */
  2371. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  2372. unsigned long zone_type,
  2373. unsigned long *ignored)
  2374. {
  2375. unsigned long node_start_pfn, node_end_pfn;
  2376. unsigned long zone_start_pfn, zone_end_pfn;
  2377. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  2378. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  2379. node_start_pfn);
  2380. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  2381. node_end_pfn);
  2382. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  2383. }
  2384. #else
  2385. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  2386. unsigned long zone_type,
  2387. unsigned long *zones_size)
  2388. {
  2389. return zones_size[zone_type];
  2390. }
  2391. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  2392. unsigned long zone_type,
  2393. unsigned long *zholes_size)
  2394. {
  2395. if (!zholes_size)
  2396. return 0;
  2397. return zholes_size[zone_type];
  2398. }
  2399. #endif
  2400. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  2401. unsigned long *zones_size, unsigned long *zholes_size)
  2402. {
  2403. unsigned long realtotalpages, totalpages = 0;
  2404. enum zone_type i;
  2405. for (i = 0; i < MAX_NR_ZONES; i++)
  2406. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  2407. zones_size);
  2408. pgdat->node_spanned_pages = totalpages;
  2409. realtotalpages = totalpages;
  2410. for (i = 0; i < MAX_NR_ZONES; i++)
  2411. realtotalpages -=
  2412. zone_absent_pages_in_node(pgdat->node_id, i,
  2413. zholes_size);
  2414. pgdat->node_present_pages = realtotalpages;
  2415. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  2416. realtotalpages);
  2417. }
  2418. /*
  2419. * Set up the zone data structures:
  2420. * - mark all pages reserved
  2421. * - mark all memory queues empty
  2422. * - clear the memory bitmaps
  2423. */
  2424. static void __meminit free_area_init_core(struct pglist_data *pgdat,
  2425. unsigned long *zones_size, unsigned long *zholes_size)
  2426. {
  2427. enum zone_type j;
  2428. int nid = pgdat->node_id;
  2429. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  2430. int ret;
  2431. pgdat_resize_init(pgdat);
  2432. pgdat->nr_zones = 0;
  2433. init_waitqueue_head(&pgdat->kswapd_wait);
  2434. pgdat->kswapd_max_order = 0;
  2435. for (j = 0; j < MAX_NR_ZONES; j++) {
  2436. struct zone *zone = pgdat->node_zones + j;
  2437. unsigned long size, realsize, memmap_pages;
  2438. size = zone_spanned_pages_in_node(nid, j, zones_size);
  2439. realsize = size - zone_absent_pages_in_node(nid, j,
  2440. zholes_size);
  2441. /*
  2442. * Adjust realsize so that it accounts for how much memory
  2443. * is used by this zone for memmap. This affects the watermark
  2444. * and per-cpu initialisations
  2445. */
  2446. memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
  2447. if (realsize >= memmap_pages) {
  2448. realsize -= memmap_pages;
  2449. printk(KERN_DEBUG
  2450. " %s zone: %lu pages used for memmap\n",
  2451. zone_names[j], memmap_pages);
  2452. } else
  2453. printk(KERN_WARNING
  2454. " %s zone: %lu pages exceeds realsize %lu\n",
  2455. zone_names[j], memmap_pages, realsize);
  2456. /* Account for reserved pages */
  2457. if (j == 0 && realsize > dma_reserve) {
  2458. realsize -= dma_reserve;
  2459. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  2460. zone_names[0], dma_reserve);
  2461. }
  2462. if (!is_highmem_idx(j))
  2463. nr_kernel_pages += realsize;
  2464. nr_all_pages += realsize;
  2465. zone->spanned_pages = size;
  2466. zone->present_pages = realsize;
  2467. #ifdef CONFIG_NUMA
  2468. zone->node = nid;
  2469. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  2470. / 100;
  2471. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  2472. #endif
  2473. zone->name = zone_names[j];
  2474. spin_lock_init(&zone->lock);
  2475. spin_lock_init(&zone->lru_lock);
  2476. zone_seqlock_init(zone);
  2477. zone->zone_pgdat = pgdat;
  2478. zone->prev_priority = DEF_PRIORITY;
  2479. zone_pcp_init(zone);
  2480. INIT_LIST_HEAD(&zone->active_list);
  2481. INIT_LIST_HEAD(&zone->inactive_list);
  2482. zone->nr_scan_active = 0;
  2483. zone->nr_scan_inactive = 0;
  2484. zap_zone_vm_stats(zone);
  2485. atomic_set(&zone->reclaim_in_progress, 0);
  2486. if (!size)
  2487. continue;
  2488. ret = init_currently_empty_zone(zone, zone_start_pfn,
  2489. size, MEMMAP_EARLY);
  2490. BUG_ON(ret);
  2491. zone_start_pfn += size;
  2492. }
  2493. }
  2494. static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
  2495. {
  2496. /* Skip empty nodes */
  2497. if (!pgdat->node_spanned_pages)
  2498. return;
  2499. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  2500. /* ia64 gets its own node_mem_map, before this, without bootmem */
  2501. if (!pgdat->node_mem_map) {
  2502. unsigned long size, start, end;
  2503. struct page *map;
  2504. /*
  2505. * The zone's endpoints aren't required to be MAX_ORDER
  2506. * aligned but the node_mem_map endpoints must be in order
  2507. * for the buddy allocator to function correctly.
  2508. */
  2509. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  2510. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  2511. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  2512. size = (end - start) * sizeof(struct page);
  2513. map = alloc_remap(pgdat->node_id, size);
  2514. if (!map)
  2515. map = alloc_bootmem_node(pgdat, size);
  2516. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  2517. }
  2518. #ifndef CONFIG_NEED_MULTIPLE_NODES
  2519. /*
  2520. * With no DISCONTIG, the global mem_map is just set as node 0's
  2521. */
  2522. if (pgdat == NODE_DATA(0)) {
  2523. mem_map = NODE_DATA(0)->node_mem_map;
  2524. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2525. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  2526. mem_map -= pgdat->node_start_pfn;
  2527. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  2528. }
  2529. #endif
  2530. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  2531. }
  2532. void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
  2533. unsigned long *zones_size, unsigned long node_start_pfn,
  2534. unsigned long *zholes_size)
  2535. {
  2536. pgdat->node_id = nid;
  2537. pgdat->node_start_pfn = node_start_pfn;
  2538. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  2539. alloc_node_mem_map(pgdat);
  2540. free_area_init_core(pgdat, zones_size, zholes_size);
  2541. }
  2542. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2543. #if MAX_NUMNODES > 1
  2544. /*
  2545. * Figure out the number of possible node ids.
  2546. */
  2547. static void __init setup_nr_node_ids(void)
  2548. {
  2549. unsigned int node;
  2550. unsigned int highest = 0;
  2551. for_each_node_mask(node, node_possible_map)
  2552. highest = node;
  2553. nr_node_ids = highest + 1;
  2554. }
  2555. #else
  2556. static inline void setup_nr_node_ids(void)
  2557. {
  2558. }
  2559. #endif
  2560. /**
  2561. * add_active_range - Register a range of PFNs backed by physical memory
  2562. * @nid: The node ID the range resides on
  2563. * @start_pfn: The start PFN of the available physical memory
  2564. * @end_pfn: The end PFN of the available physical memory
  2565. *
  2566. * These ranges are stored in an early_node_map[] and later used by
  2567. * free_area_init_nodes() to calculate zone sizes and holes. If the
  2568. * range spans a memory hole, it is up to the architecture to ensure
  2569. * the memory is not freed by the bootmem allocator. If possible
  2570. * the range being registered will be merged with existing ranges.
  2571. */
  2572. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  2573. unsigned long end_pfn)
  2574. {
  2575. int i;
  2576. printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
  2577. "%d entries of %d used\n",
  2578. nid, start_pfn, end_pfn,
  2579. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  2580. /* Merge with existing active regions if possible */
  2581. for (i = 0; i < nr_nodemap_entries; i++) {
  2582. if (early_node_map[i].nid != nid)
  2583. continue;
  2584. /* Skip if an existing region covers this new one */
  2585. if (start_pfn >= early_node_map[i].start_pfn &&
  2586. end_pfn <= early_node_map[i].end_pfn)
  2587. return;
  2588. /* Merge forward if suitable */
  2589. if (start_pfn <= early_node_map[i].end_pfn &&
  2590. end_pfn > early_node_map[i].end_pfn) {
  2591. early_node_map[i].end_pfn = end_pfn;
  2592. return;
  2593. }
  2594. /* Merge backward if suitable */
  2595. if (start_pfn < early_node_map[i].end_pfn &&
  2596. end_pfn >= early_node_map[i].start_pfn) {
  2597. early_node_map[i].start_pfn = start_pfn;
  2598. return;
  2599. }
  2600. }
  2601. /* Check that early_node_map is large enough */
  2602. if (i >= MAX_ACTIVE_REGIONS) {
  2603. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  2604. MAX_ACTIVE_REGIONS);
  2605. return;
  2606. }
  2607. early_node_map[i].nid = nid;
  2608. early_node_map[i].start_pfn = start_pfn;
  2609. early_node_map[i].end_pfn = end_pfn;
  2610. nr_nodemap_entries = i + 1;
  2611. }
  2612. /**
  2613. * shrink_active_range - Shrink an existing registered range of PFNs
  2614. * @nid: The node id the range is on that should be shrunk
  2615. * @old_end_pfn: The old end PFN of the range
  2616. * @new_end_pfn: The new PFN of the range
  2617. *
  2618. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  2619. * The map is kept at the end physical page range that has already been
  2620. * registered with add_active_range(). This function allows an arch to shrink
  2621. * an existing registered range.
  2622. */
  2623. void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
  2624. unsigned long new_end_pfn)
  2625. {
  2626. int i;
  2627. /* Find the old active region end and shrink */
  2628. for_each_active_range_index_in_nid(i, nid)
  2629. if (early_node_map[i].end_pfn == old_end_pfn) {
  2630. early_node_map[i].end_pfn = new_end_pfn;
  2631. break;
  2632. }
  2633. }
  2634. /**
  2635. * remove_all_active_ranges - Remove all currently registered regions
  2636. *
  2637. * During discovery, it may be found that a table like SRAT is invalid
  2638. * and an alternative discovery method must be used. This function removes
  2639. * all currently registered regions.
  2640. */
  2641. void __init remove_all_active_ranges(void)
  2642. {
  2643. memset(early_node_map, 0, sizeof(early_node_map));
  2644. nr_nodemap_entries = 0;
  2645. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  2646. memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
  2647. memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
  2648. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  2649. }
  2650. /* Compare two active node_active_regions */
  2651. static int __init cmp_node_active_region(const void *a, const void *b)
  2652. {
  2653. struct node_active_region *arange = (struct node_active_region *)a;
  2654. struct node_active_region *brange = (struct node_active_region *)b;
  2655. /* Done this way to avoid overflows */
  2656. if (arange->start_pfn > brange->start_pfn)
  2657. return 1;
  2658. if (arange->start_pfn < brange->start_pfn)
  2659. return -1;
  2660. return 0;
  2661. }
  2662. /* sort the node_map by start_pfn */
  2663. static void __init sort_node_map(void)
  2664. {
  2665. sort(early_node_map, (size_t)nr_nodemap_entries,
  2666. sizeof(struct node_active_region),
  2667. cmp_node_active_region, NULL);
  2668. }
  2669. /* Find the lowest pfn for a node */
  2670. unsigned long __init find_min_pfn_for_node(unsigned long nid)
  2671. {
  2672. int i;
  2673. unsigned long min_pfn = ULONG_MAX;
  2674. /* Assuming a sorted map, the first range found has the starting pfn */
  2675. for_each_active_range_index_in_nid(i, nid)
  2676. min_pfn = min(min_pfn, early_node_map[i].start_pfn);
  2677. if (min_pfn == ULONG_MAX) {
  2678. printk(KERN_WARNING
  2679. "Could not find start_pfn for node %lu\n", nid);
  2680. return 0;
  2681. }
  2682. return min_pfn;
  2683. }
  2684. /**
  2685. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  2686. *
  2687. * It returns the minimum PFN based on information provided via
  2688. * add_active_range().
  2689. */
  2690. unsigned long __init find_min_pfn_with_active_regions(void)
  2691. {
  2692. return find_min_pfn_for_node(MAX_NUMNODES);
  2693. }
  2694. /**
  2695. * find_max_pfn_with_active_regions - Find the maximum PFN registered
  2696. *
  2697. * It returns the maximum PFN based on information provided via
  2698. * add_active_range().
  2699. */
  2700. unsigned long __init find_max_pfn_with_active_regions(void)
  2701. {
  2702. int i;
  2703. unsigned long max_pfn = 0;
  2704. for (i = 0; i < nr_nodemap_entries; i++)
  2705. max_pfn = max(max_pfn, early_node_map[i].end_pfn);
  2706. return max_pfn;
  2707. }
  2708. /**
  2709. * free_area_init_nodes - Initialise all pg_data_t and zone data
  2710. * @max_zone_pfn: an array of max PFNs for each zone
  2711. *
  2712. * This will call free_area_init_node() for each active node in the system.
  2713. * Using the page ranges provided by add_active_range(), the size of each
  2714. * zone in each node and their holes is calculated. If the maximum PFN
  2715. * between two adjacent zones match, it is assumed that the zone is empty.
  2716. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  2717. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  2718. * starts where the previous one ended. For example, ZONE_DMA32 starts
  2719. * at arch_max_dma_pfn.
  2720. */
  2721. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  2722. {
  2723. unsigned long nid;
  2724. enum zone_type i;
  2725. /* Sort early_node_map as initialisation assumes it is sorted */
  2726. sort_node_map();
  2727. /* Record where the zone boundaries are */
  2728. memset(arch_zone_lowest_possible_pfn, 0,
  2729. sizeof(arch_zone_lowest_possible_pfn));
  2730. memset(arch_zone_highest_possible_pfn, 0,
  2731. sizeof(arch_zone_highest_possible_pfn));
  2732. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  2733. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  2734. for (i = 1; i < MAX_NR_ZONES; i++) {
  2735. arch_zone_lowest_possible_pfn[i] =
  2736. arch_zone_highest_possible_pfn[i-1];
  2737. arch_zone_highest_possible_pfn[i] =
  2738. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  2739. }
  2740. /* Print out the zone ranges */
  2741. printk("Zone PFN ranges:\n");
  2742. for (i = 0; i < MAX_NR_ZONES; i++)
  2743. printk(" %-8s %8lu -> %8lu\n",
  2744. zone_names[i],
  2745. arch_zone_lowest_possible_pfn[i],
  2746. arch_zone_highest_possible_pfn[i]);
  2747. /* Print out the early_node_map[] */
  2748. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  2749. for (i = 0; i < nr_nodemap_entries; i++)
  2750. printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
  2751. early_node_map[i].start_pfn,
  2752. early_node_map[i].end_pfn);
  2753. /* Initialise every node */
  2754. setup_nr_node_ids();
  2755. for_each_online_node(nid) {
  2756. pg_data_t *pgdat = NODE_DATA(nid);
  2757. free_area_init_node(nid, pgdat, NULL,
  2758. find_min_pfn_for_node(nid), NULL);
  2759. }
  2760. }
  2761. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  2762. /**
  2763. * set_dma_reserve - set the specified number of pages reserved in the first zone
  2764. * @new_dma_reserve: The number of pages to mark reserved
  2765. *
  2766. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  2767. * In the DMA zone, a significant percentage may be consumed by kernel image
  2768. * and other unfreeable allocations which can skew the watermarks badly. This
  2769. * function may optionally be used to account for unfreeable pages in the
  2770. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  2771. * smaller per-cpu batchsize.
  2772. */
  2773. void __init set_dma_reserve(unsigned long new_dma_reserve)
  2774. {
  2775. dma_reserve = new_dma_reserve;
  2776. }
  2777. #ifndef CONFIG_NEED_MULTIPLE_NODES
  2778. static bootmem_data_t contig_bootmem_data;
  2779. struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
  2780. EXPORT_SYMBOL(contig_page_data);
  2781. #endif
  2782. void __init free_area_init(unsigned long *zones_size)
  2783. {
  2784. free_area_init_node(0, NODE_DATA(0), zones_size,
  2785. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  2786. }
  2787. static int page_alloc_cpu_notify(struct notifier_block *self,
  2788. unsigned long action, void *hcpu)
  2789. {
  2790. int cpu = (unsigned long)hcpu;
  2791. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  2792. local_irq_disable();
  2793. __drain_pages(cpu);
  2794. vm_events_fold_cpu(cpu);
  2795. local_irq_enable();
  2796. refresh_cpu_vm_stats(cpu);
  2797. }
  2798. return NOTIFY_OK;
  2799. }
  2800. void __init page_alloc_init(void)
  2801. {
  2802. hotcpu_notifier(page_alloc_cpu_notify, 0);
  2803. }
  2804. /*
  2805. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  2806. * or min_free_kbytes changes.
  2807. */
  2808. static void calculate_totalreserve_pages(void)
  2809. {
  2810. struct pglist_data *pgdat;
  2811. unsigned long reserve_pages = 0;
  2812. enum zone_type i, j;
  2813. for_each_online_pgdat(pgdat) {
  2814. for (i = 0; i < MAX_NR_ZONES; i++) {
  2815. struct zone *zone = pgdat->node_zones + i;
  2816. unsigned long max = 0;
  2817. /* Find valid and maximum lowmem_reserve in the zone */
  2818. for (j = i; j < MAX_NR_ZONES; j++) {
  2819. if (zone->lowmem_reserve[j] > max)
  2820. max = zone->lowmem_reserve[j];
  2821. }
  2822. /* we treat pages_high as reserved pages. */
  2823. max += zone->pages_high;
  2824. if (max > zone->present_pages)
  2825. max = zone->present_pages;
  2826. reserve_pages += max;
  2827. }
  2828. }
  2829. totalreserve_pages = reserve_pages;
  2830. }
  2831. /*
  2832. * setup_per_zone_lowmem_reserve - called whenever
  2833. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  2834. * has a correct pages reserved value, so an adequate number of
  2835. * pages are left in the zone after a successful __alloc_pages().
  2836. */
  2837. static void setup_per_zone_lowmem_reserve(void)
  2838. {
  2839. struct pglist_data *pgdat;
  2840. enum zone_type j, idx;
  2841. for_each_online_pgdat(pgdat) {
  2842. for (j = 0; j < MAX_NR_ZONES; j++) {
  2843. struct zone *zone = pgdat->node_zones + j;
  2844. unsigned long present_pages = zone->present_pages;
  2845. zone->lowmem_reserve[j] = 0;
  2846. idx = j;
  2847. while (idx) {
  2848. struct zone *lower_zone;
  2849. idx--;
  2850. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  2851. sysctl_lowmem_reserve_ratio[idx] = 1;
  2852. lower_zone = pgdat->node_zones + idx;
  2853. lower_zone->lowmem_reserve[j] = present_pages /
  2854. sysctl_lowmem_reserve_ratio[idx];
  2855. present_pages += lower_zone->present_pages;
  2856. }
  2857. }
  2858. }
  2859. /* update totalreserve_pages */
  2860. calculate_totalreserve_pages();
  2861. }
  2862. /**
  2863. * setup_per_zone_pages_min - called when min_free_kbytes changes.
  2864. *
  2865. * Ensures that the pages_{min,low,high} values for each zone are set correctly
  2866. * with respect to min_free_kbytes.
  2867. */
  2868. void setup_per_zone_pages_min(void)
  2869. {
  2870. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  2871. unsigned long lowmem_pages = 0;
  2872. struct zone *zone;
  2873. unsigned long flags;
  2874. /* Calculate total number of !ZONE_HIGHMEM pages */
  2875. for_each_zone(zone) {
  2876. if (!is_highmem(zone))
  2877. lowmem_pages += zone->present_pages;
  2878. }
  2879. for_each_zone(zone) {
  2880. u64 tmp;
  2881. spin_lock_irqsave(&zone->lru_lock, flags);
  2882. tmp = (u64)pages_min * zone->present_pages;
  2883. do_div(tmp, lowmem_pages);
  2884. if (is_highmem(zone)) {
  2885. /*
  2886. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  2887. * need highmem pages, so cap pages_min to a small
  2888. * value here.
  2889. *
  2890. * The (pages_high-pages_low) and (pages_low-pages_min)
  2891. * deltas controls asynch page reclaim, and so should
  2892. * not be capped for highmem.
  2893. */
  2894. int min_pages;
  2895. min_pages = zone->present_pages / 1024;
  2896. if (min_pages < SWAP_CLUSTER_MAX)
  2897. min_pages = SWAP_CLUSTER_MAX;
  2898. if (min_pages > 128)
  2899. min_pages = 128;
  2900. zone->pages_min = min_pages;
  2901. } else {
  2902. /*
  2903. * If it's a lowmem zone, reserve a number of pages
  2904. * proportionate to the zone's size.
  2905. */
  2906. zone->pages_min = tmp;
  2907. }
  2908. zone->pages_low = zone->pages_min + (tmp >> 2);
  2909. zone->pages_high = zone->pages_min + (tmp >> 1);
  2910. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2911. }
  2912. /* update totalreserve_pages */
  2913. calculate_totalreserve_pages();
  2914. }
  2915. /*
  2916. * Initialise min_free_kbytes.
  2917. *
  2918. * For small machines we want it small (128k min). For large machines
  2919. * we want it large (64MB max). But it is not linear, because network
  2920. * bandwidth does not increase linearly with machine size. We use
  2921. *
  2922. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  2923. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  2924. *
  2925. * which yields
  2926. *
  2927. * 16MB: 512k
  2928. * 32MB: 724k
  2929. * 64MB: 1024k
  2930. * 128MB: 1448k
  2931. * 256MB: 2048k
  2932. * 512MB: 2896k
  2933. * 1024MB: 4096k
  2934. * 2048MB: 5792k
  2935. * 4096MB: 8192k
  2936. * 8192MB: 11584k
  2937. * 16384MB: 16384k
  2938. */
  2939. static int __init init_per_zone_pages_min(void)
  2940. {
  2941. unsigned long lowmem_kbytes;
  2942. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  2943. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  2944. if (min_free_kbytes < 128)
  2945. min_free_kbytes = 128;
  2946. if (min_free_kbytes > 65536)
  2947. min_free_kbytes = 65536;
  2948. setup_per_zone_pages_min();
  2949. setup_per_zone_lowmem_reserve();
  2950. return 0;
  2951. }
  2952. module_init(init_per_zone_pages_min)
  2953. /*
  2954. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  2955. * that we can call two helper functions whenever min_free_kbytes
  2956. * changes.
  2957. */
  2958. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  2959. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2960. {
  2961. proc_dointvec(table, write, file, buffer, length, ppos);
  2962. if (write)
  2963. setup_per_zone_pages_min();
  2964. return 0;
  2965. }
  2966. #ifdef CONFIG_NUMA
  2967. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  2968. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2969. {
  2970. struct zone *zone;
  2971. int rc;
  2972. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2973. if (rc)
  2974. return rc;
  2975. for_each_zone(zone)
  2976. zone->min_unmapped_pages = (zone->present_pages *
  2977. sysctl_min_unmapped_ratio) / 100;
  2978. return 0;
  2979. }
  2980. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  2981. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2982. {
  2983. struct zone *zone;
  2984. int rc;
  2985. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2986. if (rc)
  2987. return rc;
  2988. for_each_zone(zone)
  2989. zone->min_slab_pages = (zone->present_pages *
  2990. sysctl_min_slab_ratio) / 100;
  2991. return 0;
  2992. }
  2993. #endif
  2994. /*
  2995. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  2996. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  2997. * whenever sysctl_lowmem_reserve_ratio changes.
  2998. *
  2999. * The reserve ratio obviously has absolutely no relation with the
  3000. * pages_min watermarks. The lowmem reserve ratio can only make sense
  3001. * if in function of the boot time zone sizes.
  3002. */
  3003. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  3004. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3005. {
  3006. proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3007. setup_per_zone_lowmem_reserve();
  3008. return 0;
  3009. }
  3010. /*
  3011. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  3012. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  3013. * can have before it gets flushed back to buddy allocator.
  3014. */
  3015. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  3016. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3017. {
  3018. struct zone *zone;
  3019. unsigned int cpu;
  3020. int ret;
  3021. ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3022. if (!write || (ret == -EINVAL))
  3023. return ret;
  3024. for_each_zone(zone) {
  3025. for_each_online_cpu(cpu) {
  3026. unsigned long high;
  3027. high = zone->present_pages / percpu_pagelist_fraction;
  3028. setup_pagelist_highmark(zone_pcp(zone, cpu), high);
  3029. }
  3030. }
  3031. return 0;
  3032. }
  3033. int hashdist = HASHDIST_DEFAULT;
  3034. #ifdef CONFIG_NUMA
  3035. static int __init set_hashdist(char *str)
  3036. {
  3037. if (!str)
  3038. return 0;
  3039. hashdist = simple_strtoul(str, &str, 0);
  3040. return 1;
  3041. }
  3042. __setup("hashdist=", set_hashdist);
  3043. #endif
  3044. /*
  3045. * allocate a large system hash table from bootmem
  3046. * - it is assumed that the hash table must contain an exact power-of-2
  3047. * quantity of entries
  3048. * - limit is the number of hash buckets, not the total allocation size
  3049. */
  3050. void *__init alloc_large_system_hash(const char *tablename,
  3051. unsigned long bucketsize,
  3052. unsigned long numentries,
  3053. int scale,
  3054. int flags,
  3055. unsigned int *_hash_shift,
  3056. unsigned int *_hash_mask,
  3057. unsigned long limit)
  3058. {
  3059. unsigned long long max = limit;
  3060. unsigned long log2qty, size;
  3061. void *table = NULL;
  3062. /* allow the kernel cmdline to have a say */
  3063. if (!numentries) {
  3064. /* round applicable memory size up to nearest megabyte */
  3065. numentries = nr_kernel_pages;
  3066. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  3067. numentries >>= 20 - PAGE_SHIFT;
  3068. numentries <<= 20 - PAGE_SHIFT;
  3069. /* limit to 1 bucket per 2^scale bytes of low memory */
  3070. if (scale > PAGE_SHIFT)
  3071. numentries >>= (scale - PAGE_SHIFT);
  3072. else
  3073. numentries <<= (PAGE_SHIFT - scale);
  3074. /* Make sure we've got at least a 0-order allocation.. */
  3075. if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  3076. numentries = PAGE_SIZE / bucketsize;
  3077. }
  3078. numentries = roundup_pow_of_two(numentries);
  3079. /* limit allocation size to 1/16 total memory by default */
  3080. if (max == 0) {
  3081. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  3082. do_div(max, bucketsize);
  3083. }
  3084. if (numentries > max)
  3085. numentries = max;
  3086. log2qty = ilog2(numentries);
  3087. do {
  3088. size = bucketsize << log2qty;
  3089. if (flags & HASH_EARLY)
  3090. table = alloc_bootmem(size);
  3091. else if (hashdist)
  3092. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  3093. else {
  3094. unsigned long order;
  3095. for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
  3096. ;
  3097. table = (void*) __get_free_pages(GFP_ATOMIC, order);
  3098. /*
  3099. * If bucketsize is not a power-of-two, we may free
  3100. * some pages at the end of hash table.
  3101. */
  3102. if (table) {
  3103. unsigned long alloc_end = (unsigned long)table +
  3104. (PAGE_SIZE << order);
  3105. unsigned long used = (unsigned long)table +
  3106. PAGE_ALIGN(size);
  3107. split_page(virt_to_page(table), order);
  3108. while (used < alloc_end) {
  3109. free_page(used);
  3110. used += PAGE_SIZE;
  3111. }
  3112. }
  3113. }
  3114. } while (!table && size > PAGE_SIZE && --log2qty);
  3115. if (!table)
  3116. panic("Failed to allocate %s hash table\n", tablename);
  3117. printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
  3118. tablename,
  3119. (1U << log2qty),
  3120. ilog2(size) - PAGE_SHIFT,
  3121. size);
  3122. if (_hash_shift)
  3123. *_hash_shift = log2qty;
  3124. if (_hash_mask)
  3125. *_hash_mask = (1 << log2qty) - 1;
  3126. return table;
  3127. }
  3128. #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
  3129. struct page *pfn_to_page(unsigned long pfn)
  3130. {
  3131. return __pfn_to_page(pfn);
  3132. }
  3133. unsigned long page_to_pfn(struct page *page)
  3134. {
  3135. return __page_to_pfn(page);
  3136. }
  3137. EXPORT_SYMBOL(pfn_to_page);
  3138. EXPORT_SYMBOL(page_to_pfn);
  3139. #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */