page_alloc.c 88 KB

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