page_alloc.c 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713
  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/jiffies.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/memblock.h>
  24. #include <linux/compiler.h>
  25. #include <linux/kernel.h>
  26. #include <linux/kmemcheck.h>
  27. #include <linux/module.h>
  28. #include <linux/suspend.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/slab.h>
  32. #include <linux/oom.h>
  33. #include <linux/notifier.h>
  34. #include <linux/topology.h>
  35. #include <linux/sysctl.h>
  36. #include <linux/cpu.h>
  37. #include <linux/cpuset.h>
  38. #include <linux/memory_hotplug.h>
  39. #include <linux/nodemask.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/vmstat.h>
  42. #include <linux/mempolicy.h>
  43. #include <linux/stop_machine.h>
  44. #include <linux/sort.h>
  45. #include <linux/pfn.h>
  46. #include <linux/backing-dev.h>
  47. #include <linux/fault-inject.h>
  48. #include <linux/page-isolation.h>
  49. #include <linux/page_cgroup.h>
  50. #include <linux/debugobjects.h>
  51. #include <linux/kmemleak.h>
  52. #include <linux/memory.h>
  53. #include <linux/compaction.h>
  54. #include <trace/events/kmem.h>
  55. #include <linux/ftrace_event.h>
  56. #include <linux/memcontrol.h>
  57. #include <linux/prefetch.h>
  58. #include <asm/tlbflush.h>
  59. #include <asm/div64.h>
  60. #include "internal.h"
  61. #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
  62. DEFINE_PER_CPU(int, numa_node);
  63. EXPORT_PER_CPU_SYMBOL(numa_node);
  64. #endif
  65. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  66. /*
  67. * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
  68. * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
  69. * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
  70. * defined in <linux/topology.h>.
  71. */
  72. DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
  73. EXPORT_PER_CPU_SYMBOL(_numa_mem_);
  74. #endif
  75. /*
  76. * Array of node states.
  77. */
  78. nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
  79. [N_POSSIBLE] = NODE_MASK_ALL,
  80. [N_ONLINE] = { { [0] = 1UL } },
  81. #ifndef CONFIG_NUMA
  82. [N_NORMAL_MEMORY] = { { [0] = 1UL } },
  83. #ifdef CONFIG_HIGHMEM
  84. [N_HIGH_MEMORY] = { { [0] = 1UL } },
  85. #endif
  86. [N_CPU] = { { [0] = 1UL } },
  87. #endif /* NUMA */
  88. };
  89. EXPORT_SYMBOL(node_states);
  90. unsigned long totalram_pages __read_mostly;
  91. unsigned long totalreserve_pages __read_mostly;
  92. int percpu_pagelist_fraction;
  93. gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
  94. #ifdef CONFIG_PM_SLEEP
  95. /*
  96. * The following functions are used by the suspend/hibernate code to temporarily
  97. * change gfp_allowed_mask in order to avoid using I/O during memory allocations
  98. * while devices are suspended. To avoid races with the suspend/hibernate code,
  99. * they should always be called with pm_mutex held (gfp_allowed_mask also should
  100. * only be modified with pm_mutex held, unless the suspend/hibernate code is
  101. * guaranteed not to run in parallel with that modification).
  102. */
  103. static gfp_t saved_gfp_mask;
  104. void pm_restore_gfp_mask(void)
  105. {
  106. WARN_ON(!mutex_is_locked(&pm_mutex));
  107. if (saved_gfp_mask) {
  108. gfp_allowed_mask = saved_gfp_mask;
  109. saved_gfp_mask = 0;
  110. }
  111. }
  112. void pm_restrict_gfp_mask(void)
  113. {
  114. WARN_ON(!mutex_is_locked(&pm_mutex));
  115. WARN_ON(saved_gfp_mask);
  116. saved_gfp_mask = gfp_allowed_mask;
  117. gfp_allowed_mask &= ~GFP_IOFS;
  118. }
  119. #endif /* CONFIG_PM_SLEEP */
  120. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  121. int pageblock_order __read_mostly;
  122. #endif
  123. static void __free_pages_ok(struct page *page, unsigned int order);
  124. /*
  125. * results with 256, 32 in the lowmem_reserve sysctl:
  126. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  127. * 1G machine -> (16M dma, 784M normal, 224M high)
  128. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  129. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  130. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  131. *
  132. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  133. * don't need any ZONE_NORMAL reservation
  134. */
  135. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
  136. #ifdef CONFIG_ZONE_DMA
  137. 256,
  138. #endif
  139. #ifdef CONFIG_ZONE_DMA32
  140. 256,
  141. #endif
  142. #ifdef CONFIG_HIGHMEM
  143. 32,
  144. #endif
  145. 32,
  146. };
  147. EXPORT_SYMBOL(totalram_pages);
  148. static char * const zone_names[MAX_NR_ZONES] = {
  149. #ifdef CONFIG_ZONE_DMA
  150. "DMA",
  151. #endif
  152. #ifdef CONFIG_ZONE_DMA32
  153. "DMA32",
  154. #endif
  155. "Normal",
  156. #ifdef CONFIG_HIGHMEM
  157. "HighMem",
  158. #endif
  159. "Movable",
  160. };
  161. int min_free_kbytes = 1024;
  162. static unsigned long __meminitdata nr_kernel_pages;
  163. static unsigned long __meminitdata nr_all_pages;
  164. static unsigned long __meminitdata dma_reserve;
  165. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  166. /*
  167. * MAX_ACTIVE_REGIONS determines the maximum number of distinct
  168. * ranges of memory (RAM) that may be registered with add_active_range().
  169. * Ranges passed to add_active_range() will be merged if possible
  170. * so the number of times add_active_range() can be called is
  171. * related to the number of nodes and the number of holes
  172. */
  173. #ifdef CONFIG_MAX_ACTIVE_REGIONS
  174. /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
  175. #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
  176. #else
  177. #if MAX_NUMNODES >= 32
  178. /* If there can be many nodes, allow up to 50 holes per node */
  179. #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
  180. #else
  181. /* By default, allow up to 256 distinct regions */
  182. #define MAX_ACTIVE_REGIONS 256
  183. #endif
  184. #endif
  185. static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
  186. static int __meminitdata nr_nodemap_entries;
  187. static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
  188. static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
  189. static unsigned long __initdata required_kernelcore;
  190. static unsigned long __initdata required_movablecore;
  191. static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
  192. /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
  193. int movable_zone;
  194. EXPORT_SYMBOL(movable_zone);
  195. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  196. #if MAX_NUMNODES > 1
  197. int nr_node_ids __read_mostly = MAX_NUMNODES;
  198. int nr_online_nodes __read_mostly = 1;
  199. EXPORT_SYMBOL(nr_node_ids);
  200. EXPORT_SYMBOL(nr_online_nodes);
  201. #endif
  202. int page_group_by_mobility_disabled __read_mostly;
  203. static void set_pageblock_migratetype(struct page *page, int migratetype)
  204. {
  205. if (unlikely(page_group_by_mobility_disabled))
  206. migratetype = MIGRATE_UNMOVABLE;
  207. set_pageblock_flags_group(page, (unsigned long)migratetype,
  208. PB_migrate, PB_migrate_end);
  209. }
  210. bool oom_killer_disabled __read_mostly;
  211. #ifdef CONFIG_DEBUG_VM
  212. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  213. {
  214. int ret = 0;
  215. unsigned seq;
  216. unsigned long pfn = page_to_pfn(page);
  217. do {
  218. seq = zone_span_seqbegin(zone);
  219. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  220. ret = 1;
  221. else if (pfn < zone->zone_start_pfn)
  222. ret = 1;
  223. } while (zone_span_seqretry(zone, seq));
  224. return ret;
  225. }
  226. static int page_is_consistent(struct zone *zone, struct page *page)
  227. {
  228. if (!pfn_valid_within(page_to_pfn(page)))
  229. return 0;
  230. if (zone != page_zone(page))
  231. return 0;
  232. return 1;
  233. }
  234. /*
  235. * Temporary debugging check for pages not lying within a given zone.
  236. */
  237. static int bad_range(struct zone *zone, struct page *page)
  238. {
  239. if (page_outside_zone_boundaries(zone, page))
  240. return 1;
  241. if (!page_is_consistent(zone, page))
  242. return 1;
  243. return 0;
  244. }
  245. #else
  246. static inline int bad_range(struct zone *zone, struct page *page)
  247. {
  248. return 0;
  249. }
  250. #endif
  251. static void bad_page(struct page *page)
  252. {
  253. static unsigned long resume;
  254. static unsigned long nr_shown;
  255. static unsigned long nr_unshown;
  256. /* Don't complain about poisoned pages */
  257. if (PageHWPoison(page)) {
  258. reset_page_mapcount(page); /* remove PageBuddy */
  259. return;
  260. }
  261. /*
  262. * Allow a burst of 60 reports, then keep quiet for that minute;
  263. * or allow a steady drip of one report per second.
  264. */
  265. if (nr_shown == 60) {
  266. if (time_before(jiffies, resume)) {
  267. nr_unshown++;
  268. goto out;
  269. }
  270. if (nr_unshown) {
  271. printk(KERN_ALERT
  272. "BUG: Bad page state: %lu messages suppressed\n",
  273. nr_unshown);
  274. nr_unshown = 0;
  275. }
  276. nr_shown = 0;
  277. }
  278. if (nr_shown++ == 0)
  279. resume = jiffies + 60 * HZ;
  280. printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n",
  281. current->comm, page_to_pfn(page));
  282. dump_page(page);
  283. dump_stack();
  284. out:
  285. /* Leave bad fields for debug, except PageBuddy could make trouble */
  286. reset_page_mapcount(page); /* remove PageBuddy */
  287. add_taint(TAINT_BAD_PAGE);
  288. }
  289. /*
  290. * Higher-order pages are called "compound pages". They are structured thusly:
  291. *
  292. * The first PAGE_SIZE page is called the "head page".
  293. *
  294. * The remaining PAGE_SIZE pages are called "tail pages".
  295. *
  296. * All pages have PG_compound set. All pages have their ->private pointing at
  297. * the head page (even the head page has this).
  298. *
  299. * The first tail page's ->lru.next holds the address of the compound page's
  300. * put_page() function. Its ->lru.prev holds the order of allocation.
  301. * This usage means that zero-order pages may not be compound.
  302. */
  303. static void free_compound_page(struct page *page)
  304. {
  305. __free_pages_ok(page, compound_order(page));
  306. }
  307. void prep_compound_page(struct page *page, unsigned long order)
  308. {
  309. int i;
  310. int nr_pages = 1 << order;
  311. set_compound_page_dtor(page, free_compound_page);
  312. set_compound_order(page, order);
  313. __SetPageHead(page);
  314. for (i = 1; i < nr_pages; i++) {
  315. struct page *p = page + i;
  316. __SetPageTail(p);
  317. p->first_page = page;
  318. }
  319. }
  320. /* update __split_huge_page_refcount if you change this function */
  321. static int destroy_compound_page(struct page *page, unsigned long order)
  322. {
  323. int i;
  324. int nr_pages = 1 << order;
  325. int bad = 0;
  326. if (unlikely(compound_order(page) != order) ||
  327. unlikely(!PageHead(page))) {
  328. bad_page(page);
  329. bad++;
  330. }
  331. __ClearPageHead(page);
  332. for (i = 1; i < nr_pages; i++) {
  333. struct page *p = page + i;
  334. if (unlikely(!PageTail(p) || (p->first_page != page))) {
  335. bad_page(page);
  336. bad++;
  337. }
  338. __ClearPageTail(p);
  339. }
  340. return bad;
  341. }
  342. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  343. {
  344. int i;
  345. /*
  346. * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
  347. * and __GFP_HIGHMEM from hard or soft interrupt context.
  348. */
  349. VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
  350. for (i = 0; i < (1 << order); i++)
  351. clear_highpage(page + i);
  352. }
  353. static inline void set_page_order(struct page *page, int order)
  354. {
  355. set_page_private(page, order);
  356. __SetPageBuddy(page);
  357. }
  358. static inline void rmv_page_order(struct page *page)
  359. {
  360. __ClearPageBuddy(page);
  361. set_page_private(page, 0);
  362. }
  363. /*
  364. * Locate the struct page for both the matching buddy in our
  365. * pair (buddy1) and the combined O(n+1) page they form (page).
  366. *
  367. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  368. * the following equation:
  369. * B2 = B1 ^ (1 << O)
  370. * For example, if the starting buddy (buddy2) is #8 its order
  371. * 1 buddy is #10:
  372. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  373. *
  374. * 2) Any buddy B will have an order O+1 parent P which
  375. * satisfies the following equation:
  376. * P = B & ~(1 << O)
  377. *
  378. * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
  379. */
  380. static inline unsigned long
  381. __find_buddy_index(unsigned long page_idx, unsigned int order)
  382. {
  383. return page_idx ^ (1 << order);
  384. }
  385. /*
  386. * This function checks whether a page is free && is the buddy
  387. * we can do coalesce a page and its buddy if
  388. * (a) the buddy is not in a hole &&
  389. * (b) the buddy is in the buddy system &&
  390. * (c) a page and its buddy have the same order &&
  391. * (d) a page and its buddy are in the same zone.
  392. *
  393. * For recording whether a page is in the buddy system, we set ->_mapcount -2.
  394. * Setting, clearing, and testing _mapcount -2 is serialized by zone->lock.
  395. *
  396. * For recording page's order, we use page_private(page).
  397. */
  398. static inline int page_is_buddy(struct page *page, struct page *buddy,
  399. int order)
  400. {
  401. if (!pfn_valid_within(page_to_pfn(buddy)))
  402. return 0;
  403. if (page_zone_id(page) != page_zone_id(buddy))
  404. return 0;
  405. if (PageBuddy(buddy) && page_order(buddy) == order) {
  406. VM_BUG_ON(page_count(buddy) != 0);
  407. return 1;
  408. }
  409. return 0;
  410. }
  411. /*
  412. * Freeing function for a buddy system allocator.
  413. *
  414. * The concept of a buddy system is to maintain direct-mapped table
  415. * (containing bit values) for memory blocks of various "orders".
  416. * The bottom level table contains the map for the smallest allocatable
  417. * units of memory (here, pages), and each level above it describes
  418. * pairs of units from the levels below, hence, "buddies".
  419. * At a high level, all that happens here is marking the table entry
  420. * at the bottom level available, and propagating the changes upward
  421. * as necessary, plus some accounting needed to play nicely with other
  422. * parts of the VM system.
  423. * At each level, we keep a list of pages, which are heads of continuous
  424. * free pages of length of (1 << order) and marked with _mapcount -2. Page's
  425. * order is recorded in page_private(page) field.
  426. * So when we are allocating or freeing one, we can derive the state of the
  427. * other. That is, if we allocate a small block, and both were
  428. * free, the remainder of the region must be split into blocks.
  429. * If a block is freed, and its buddy is also free, then this
  430. * triggers coalescing into a block of larger size.
  431. *
  432. * -- wli
  433. */
  434. static inline void __free_one_page(struct page *page,
  435. struct zone *zone, unsigned int order,
  436. int migratetype)
  437. {
  438. unsigned long page_idx;
  439. unsigned long combined_idx;
  440. unsigned long uninitialized_var(buddy_idx);
  441. struct page *buddy;
  442. if (unlikely(PageCompound(page)))
  443. if (unlikely(destroy_compound_page(page, order)))
  444. return;
  445. VM_BUG_ON(migratetype == -1);
  446. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  447. VM_BUG_ON(page_idx & ((1 << order) - 1));
  448. VM_BUG_ON(bad_range(zone, page));
  449. while (order < MAX_ORDER-1) {
  450. buddy_idx = __find_buddy_index(page_idx, order);
  451. buddy = page + (buddy_idx - page_idx);
  452. if (!page_is_buddy(page, buddy, order))
  453. break;
  454. /* Our buddy is free, merge with it and move up one order. */
  455. list_del(&buddy->lru);
  456. zone->free_area[order].nr_free--;
  457. rmv_page_order(buddy);
  458. combined_idx = buddy_idx & page_idx;
  459. page = page + (combined_idx - page_idx);
  460. page_idx = combined_idx;
  461. order++;
  462. }
  463. set_page_order(page, order);
  464. /*
  465. * If this is not the largest possible page, check if the buddy
  466. * of the next-highest order is free. If it is, it's possible
  467. * that pages are being freed that will coalesce soon. In case,
  468. * that is happening, add the free page to the tail of the list
  469. * so it's less likely to be used soon and more likely to be merged
  470. * as a higher order page
  471. */
  472. if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
  473. struct page *higher_page, *higher_buddy;
  474. combined_idx = buddy_idx & page_idx;
  475. higher_page = page + (combined_idx - page_idx);
  476. buddy_idx = __find_buddy_index(combined_idx, order + 1);
  477. higher_buddy = page + (buddy_idx - combined_idx);
  478. if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
  479. list_add_tail(&page->lru,
  480. &zone->free_area[order].free_list[migratetype]);
  481. goto out;
  482. }
  483. }
  484. list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
  485. out:
  486. zone->free_area[order].nr_free++;
  487. }
  488. /*
  489. * free_page_mlock() -- clean up attempts to free and mlocked() page.
  490. * Page should not be on lru, so no need to fix that up.
  491. * free_pages_check() will verify...
  492. */
  493. static inline void free_page_mlock(struct page *page)
  494. {
  495. __dec_zone_page_state(page, NR_MLOCK);
  496. __count_vm_event(UNEVICTABLE_MLOCKFREED);
  497. }
  498. static inline int free_pages_check(struct page *page)
  499. {
  500. if (unlikely(page_mapcount(page) |
  501. (page->mapping != NULL) |
  502. (atomic_read(&page->_count) != 0) |
  503. (page->flags & PAGE_FLAGS_CHECK_AT_FREE) |
  504. (mem_cgroup_bad_page_check(page)))) {
  505. bad_page(page);
  506. return 1;
  507. }
  508. if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
  509. page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  510. return 0;
  511. }
  512. /*
  513. * Frees a number of pages from the PCP lists
  514. * Assumes all pages on list are in same zone, and of same order.
  515. * count is the number of pages to free.
  516. *
  517. * If the zone was previously in an "all pages pinned" state then look to
  518. * see if this freeing clears that state.
  519. *
  520. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  521. * pinned" detection logic.
  522. */
  523. static void free_pcppages_bulk(struct zone *zone, int count,
  524. struct per_cpu_pages *pcp)
  525. {
  526. int migratetype = 0;
  527. int batch_free = 0;
  528. int to_free = count;
  529. spin_lock(&zone->lock);
  530. zone->all_unreclaimable = 0;
  531. zone->pages_scanned = 0;
  532. while (to_free) {
  533. struct page *page;
  534. struct list_head *list;
  535. /*
  536. * Remove pages from lists in a round-robin fashion. A
  537. * batch_free count is maintained that is incremented when an
  538. * empty list is encountered. This is so more pages are freed
  539. * off fuller lists instead of spinning excessively around empty
  540. * lists
  541. */
  542. do {
  543. batch_free++;
  544. if (++migratetype == MIGRATE_PCPTYPES)
  545. migratetype = 0;
  546. list = &pcp->lists[migratetype];
  547. } while (list_empty(list));
  548. /* This is the only non-empty list. Free them all. */
  549. if (batch_free == MIGRATE_PCPTYPES)
  550. batch_free = to_free;
  551. do {
  552. page = list_entry(list->prev, struct page, lru);
  553. /* must delete as __free_one_page list manipulates */
  554. list_del(&page->lru);
  555. /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
  556. __free_one_page(page, zone, 0, page_private(page));
  557. trace_mm_page_pcpu_drain(page, 0, page_private(page));
  558. } while (--to_free && --batch_free && !list_empty(list));
  559. }
  560. __mod_zone_page_state(zone, NR_FREE_PAGES, count);
  561. spin_unlock(&zone->lock);
  562. }
  563. static void free_one_page(struct zone *zone, struct page *page, int order,
  564. int migratetype)
  565. {
  566. spin_lock(&zone->lock);
  567. zone->all_unreclaimable = 0;
  568. zone->pages_scanned = 0;
  569. __free_one_page(page, zone, order, migratetype);
  570. __mod_zone_page_state(zone, NR_FREE_PAGES, 1 << order);
  571. spin_unlock(&zone->lock);
  572. }
  573. static bool free_pages_prepare(struct page *page, unsigned int order)
  574. {
  575. int i;
  576. int bad = 0;
  577. trace_mm_page_free_direct(page, order);
  578. kmemcheck_free_shadow(page, order);
  579. if (PageAnon(page))
  580. page->mapping = NULL;
  581. for (i = 0; i < (1 << order); i++)
  582. bad += free_pages_check(page + i);
  583. if (bad)
  584. return false;
  585. if (!PageHighMem(page)) {
  586. debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
  587. debug_check_no_obj_freed(page_address(page),
  588. PAGE_SIZE << order);
  589. }
  590. arch_free_page(page, order);
  591. kernel_map_pages(page, 1 << order, 0);
  592. return true;
  593. }
  594. static void __free_pages_ok(struct page *page, unsigned int order)
  595. {
  596. unsigned long flags;
  597. int wasMlocked = __TestClearPageMlocked(page);
  598. if (!free_pages_prepare(page, order))
  599. return;
  600. local_irq_save(flags);
  601. if (unlikely(wasMlocked))
  602. free_page_mlock(page);
  603. __count_vm_events(PGFREE, 1 << order);
  604. free_one_page(page_zone(page), page, order,
  605. get_pageblock_migratetype(page));
  606. local_irq_restore(flags);
  607. }
  608. /*
  609. * permit the bootmem allocator to evade page validation on high-order frees
  610. */
  611. void __meminit __free_pages_bootmem(struct page *page, unsigned int order)
  612. {
  613. if (order == 0) {
  614. __ClearPageReserved(page);
  615. set_page_count(page, 0);
  616. set_page_refcounted(page);
  617. __free_page(page);
  618. } else {
  619. int loop;
  620. prefetchw(page);
  621. for (loop = 0; loop < BITS_PER_LONG; loop++) {
  622. struct page *p = &page[loop];
  623. if (loop + 1 < BITS_PER_LONG)
  624. prefetchw(p + 1);
  625. __ClearPageReserved(p);
  626. set_page_count(p, 0);
  627. }
  628. set_page_refcounted(page);
  629. __free_pages(page, order);
  630. }
  631. }
  632. /*
  633. * The order of subdivision here is critical for the IO subsystem.
  634. * Please do not alter this order without good reasons and regression
  635. * testing. Specifically, as large blocks of memory are subdivided,
  636. * the order in which smaller blocks are delivered depends on the order
  637. * they're subdivided in this function. This is the primary factor
  638. * influencing the order in which pages are delivered to the IO
  639. * subsystem according to empirical testing, and this is also justified
  640. * by considering the behavior of a buddy system containing a single
  641. * large block of memory acted on by a series of small allocations.
  642. * This behavior is a critical factor in sglist merging's success.
  643. *
  644. * -- wli
  645. */
  646. static inline void expand(struct zone *zone, struct page *page,
  647. int low, int high, struct free_area *area,
  648. int migratetype)
  649. {
  650. unsigned long size = 1 << high;
  651. while (high > low) {
  652. area--;
  653. high--;
  654. size >>= 1;
  655. VM_BUG_ON(bad_range(zone, &page[size]));
  656. list_add(&page[size].lru, &area->free_list[migratetype]);
  657. area->nr_free++;
  658. set_page_order(&page[size], high);
  659. }
  660. }
  661. /*
  662. * This page is about to be returned from the page allocator
  663. */
  664. static inline int check_new_page(struct page *page)
  665. {
  666. if (unlikely(page_mapcount(page) |
  667. (page->mapping != NULL) |
  668. (atomic_read(&page->_count) != 0) |
  669. (page->flags & PAGE_FLAGS_CHECK_AT_PREP) |
  670. (mem_cgroup_bad_page_check(page)))) {
  671. bad_page(page);
  672. return 1;
  673. }
  674. return 0;
  675. }
  676. static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
  677. {
  678. int i;
  679. for (i = 0; i < (1 << order); i++) {
  680. struct page *p = page + i;
  681. if (unlikely(check_new_page(p)))
  682. return 1;
  683. }
  684. set_page_private(page, 0);
  685. set_page_refcounted(page);
  686. arch_alloc_page(page, order);
  687. kernel_map_pages(page, 1 << order, 1);
  688. if (gfp_flags & __GFP_ZERO)
  689. prep_zero_page(page, order, gfp_flags);
  690. if (order && (gfp_flags & __GFP_COMP))
  691. prep_compound_page(page, order);
  692. return 0;
  693. }
  694. /*
  695. * Go through the free lists for the given migratetype and remove
  696. * the smallest available page from the freelists
  697. */
  698. static inline
  699. struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
  700. int migratetype)
  701. {
  702. unsigned int current_order;
  703. struct free_area * area;
  704. struct page *page;
  705. /* Find a page of the appropriate size in the preferred list */
  706. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  707. area = &(zone->free_area[current_order]);
  708. if (list_empty(&area->free_list[migratetype]))
  709. continue;
  710. page = list_entry(area->free_list[migratetype].next,
  711. struct page, lru);
  712. list_del(&page->lru);
  713. rmv_page_order(page);
  714. area->nr_free--;
  715. expand(zone, page, order, current_order, area, migratetype);
  716. return page;
  717. }
  718. return NULL;
  719. }
  720. /*
  721. * This array describes the order lists are fallen back to when
  722. * the free lists for the desirable migrate type are depleted
  723. */
  724. static int fallbacks[MIGRATE_TYPES][MIGRATE_TYPES-1] = {
  725. [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
  726. [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
  727. [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
  728. [MIGRATE_RESERVE] = { MIGRATE_RESERVE, MIGRATE_RESERVE, MIGRATE_RESERVE }, /* Never used */
  729. };
  730. /*
  731. * Move the free pages in a range to the free lists of the requested type.
  732. * Note that start_page and end_pages are not aligned on a pageblock
  733. * boundary. If alignment is required, use move_freepages_block()
  734. */
  735. static int move_freepages(struct zone *zone,
  736. struct page *start_page, struct page *end_page,
  737. int migratetype)
  738. {
  739. struct page *page;
  740. unsigned long order;
  741. int pages_moved = 0;
  742. #ifndef CONFIG_HOLES_IN_ZONE
  743. /*
  744. * page_zone is not safe to call in this context when
  745. * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
  746. * anyway as we check zone boundaries in move_freepages_block().
  747. * Remove at a later date when no bug reports exist related to
  748. * grouping pages by mobility
  749. */
  750. BUG_ON(page_zone(start_page) != page_zone(end_page));
  751. #endif
  752. for (page = start_page; page <= end_page;) {
  753. /* Make sure we are not inadvertently changing nodes */
  754. VM_BUG_ON(page_to_nid(page) != zone_to_nid(zone));
  755. if (!pfn_valid_within(page_to_pfn(page))) {
  756. page++;
  757. continue;
  758. }
  759. if (!PageBuddy(page)) {
  760. page++;
  761. continue;
  762. }
  763. order = page_order(page);
  764. list_move(&page->lru,
  765. &zone->free_area[order].free_list[migratetype]);
  766. page += 1 << order;
  767. pages_moved += 1 << order;
  768. }
  769. return pages_moved;
  770. }
  771. static int move_freepages_block(struct zone *zone, struct page *page,
  772. int migratetype)
  773. {
  774. unsigned long start_pfn, end_pfn;
  775. struct page *start_page, *end_page;
  776. start_pfn = page_to_pfn(page);
  777. start_pfn = start_pfn & ~(pageblock_nr_pages-1);
  778. start_page = pfn_to_page(start_pfn);
  779. end_page = start_page + pageblock_nr_pages - 1;
  780. end_pfn = start_pfn + pageblock_nr_pages - 1;
  781. /* Do not cross zone boundaries */
  782. if (start_pfn < zone->zone_start_pfn)
  783. start_page = page;
  784. if (end_pfn >= zone->zone_start_pfn + zone->spanned_pages)
  785. return 0;
  786. return move_freepages(zone, start_page, end_page, migratetype);
  787. }
  788. static void change_pageblock_range(struct page *pageblock_page,
  789. int start_order, int migratetype)
  790. {
  791. int nr_pageblocks = 1 << (start_order - pageblock_order);
  792. while (nr_pageblocks--) {
  793. set_pageblock_migratetype(pageblock_page, migratetype);
  794. pageblock_page += pageblock_nr_pages;
  795. }
  796. }
  797. /* Remove an element from the buddy allocator from the fallback list */
  798. static inline struct page *
  799. __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
  800. {
  801. struct free_area * area;
  802. int current_order;
  803. struct page *page;
  804. int migratetype, i;
  805. /* Find the largest possible block of pages in the other list */
  806. for (current_order = MAX_ORDER-1; current_order >= order;
  807. --current_order) {
  808. for (i = 0; i < MIGRATE_TYPES - 1; i++) {
  809. migratetype = fallbacks[start_migratetype][i];
  810. /* MIGRATE_RESERVE handled later if necessary */
  811. if (migratetype == MIGRATE_RESERVE)
  812. continue;
  813. area = &(zone->free_area[current_order]);
  814. if (list_empty(&area->free_list[migratetype]))
  815. continue;
  816. page = list_entry(area->free_list[migratetype].next,
  817. struct page, lru);
  818. area->nr_free--;
  819. /*
  820. * If breaking a large block of pages, move all free
  821. * pages to the preferred allocation list. If falling
  822. * back for a reclaimable kernel allocation, be more
  823. * aggressive about taking ownership of free pages
  824. */
  825. if (unlikely(current_order >= (pageblock_order >> 1)) ||
  826. start_migratetype == MIGRATE_RECLAIMABLE ||
  827. page_group_by_mobility_disabled) {
  828. unsigned long pages;
  829. pages = move_freepages_block(zone, page,
  830. start_migratetype);
  831. /* Claim the whole block if over half of it is free */
  832. if (pages >= (1 << (pageblock_order-1)) ||
  833. page_group_by_mobility_disabled)
  834. set_pageblock_migratetype(page,
  835. start_migratetype);
  836. migratetype = start_migratetype;
  837. }
  838. /* Remove the page from the freelists */
  839. list_del(&page->lru);
  840. rmv_page_order(page);
  841. /* Take ownership for orders >= pageblock_order */
  842. if (current_order >= pageblock_order)
  843. change_pageblock_range(page, current_order,
  844. start_migratetype);
  845. expand(zone, page, order, current_order, area, migratetype);
  846. trace_mm_page_alloc_extfrag(page, order, current_order,
  847. start_migratetype, migratetype);
  848. return page;
  849. }
  850. }
  851. return NULL;
  852. }
  853. /*
  854. * Do the hard work of removing an element from the buddy allocator.
  855. * Call me with the zone->lock already held.
  856. */
  857. static struct page *__rmqueue(struct zone *zone, unsigned int order,
  858. int migratetype)
  859. {
  860. struct page *page;
  861. retry_reserve:
  862. page = __rmqueue_smallest(zone, order, migratetype);
  863. if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {
  864. page = __rmqueue_fallback(zone, order, migratetype);
  865. /*
  866. * Use MIGRATE_RESERVE rather than fail an allocation. goto
  867. * is used because __rmqueue_smallest is an inline function
  868. * and we want just one call site
  869. */
  870. if (!page) {
  871. migratetype = MIGRATE_RESERVE;
  872. goto retry_reserve;
  873. }
  874. }
  875. trace_mm_page_alloc_zone_locked(page, order, migratetype);
  876. return page;
  877. }
  878. /*
  879. * Obtain a specified number of elements from the buddy allocator, all under
  880. * a single hold of the lock, for efficiency. Add them to the supplied list.
  881. * Returns the number of new pages which were placed at *list.
  882. */
  883. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  884. unsigned long count, struct list_head *list,
  885. int migratetype, int cold)
  886. {
  887. int i;
  888. spin_lock(&zone->lock);
  889. for (i = 0; i < count; ++i) {
  890. struct page *page = __rmqueue(zone, order, migratetype);
  891. if (unlikely(page == NULL))
  892. break;
  893. /*
  894. * Split buddy pages returned by expand() are received here
  895. * in physical page order. The page is added to the callers and
  896. * list and the list head then moves forward. From the callers
  897. * perspective, the linked list is ordered by page number in
  898. * some conditions. This is useful for IO devices that can
  899. * merge IO requests if the physical pages are ordered
  900. * properly.
  901. */
  902. if (likely(cold == 0))
  903. list_add(&page->lru, list);
  904. else
  905. list_add_tail(&page->lru, list);
  906. set_page_private(page, migratetype);
  907. list = &page->lru;
  908. }
  909. __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
  910. spin_unlock(&zone->lock);
  911. return i;
  912. }
  913. #ifdef CONFIG_NUMA
  914. /*
  915. * Called from the vmstat counter updater to drain pagesets of this
  916. * currently executing processor on remote nodes after they have
  917. * expired.
  918. *
  919. * Note that this function must be called with the thread pinned to
  920. * a single processor.
  921. */
  922. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  923. {
  924. unsigned long flags;
  925. int to_drain;
  926. local_irq_save(flags);
  927. if (pcp->count >= pcp->batch)
  928. to_drain = pcp->batch;
  929. else
  930. to_drain = pcp->count;
  931. free_pcppages_bulk(zone, to_drain, pcp);
  932. pcp->count -= to_drain;
  933. local_irq_restore(flags);
  934. }
  935. #endif
  936. /*
  937. * Drain pages of the indicated processor.
  938. *
  939. * The processor must either be the current processor and the
  940. * thread pinned to the current processor or a processor that
  941. * is not online.
  942. */
  943. static void drain_pages(unsigned int cpu)
  944. {
  945. unsigned long flags;
  946. struct zone *zone;
  947. for_each_populated_zone(zone) {
  948. struct per_cpu_pageset *pset;
  949. struct per_cpu_pages *pcp;
  950. local_irq_save(flags);
  951. pset = per_cpu_ptr(zone->pageset, cpu);
  952. pcp = &pset->pcp;
  953. if (pcp->count) {
  954. free_pcppages_bulk(zone, pcp->count, pcp);
  955. pcp->count = 0;
  956. }
  957. local_irq_restore(flags);
  958. }
  959. }
  960. /*
  961. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  962. */
  963. void drain_local_pages(void *arg)
  964. {
  965. drain_pages(smp_processor_id());
  966. }
  967. /*
  968. * Spill all the per-cpu pages from all CPUs back into the buddy allocator
  969. */
  970. void drain_all_pages(void)
  971. {
  972. on_each_cpu(drain_local_pages, NULL, 1);
  973. }
  974. #ifdef CONFIG_HIBERNATION
  975. void mark_free_pages(struct zone *zone)
  976. {
  977. unsigned long pfn, max_zone_pfn;
  978. unsigned long flags;
  979. int order, t;
  980. struct list_head *curr;
  981. if (!zone->spanned_pages)
  982. return;
  983. spin_lock_irqsave(&zone->lock, flags);
  984. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  985. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  986. if (pfn_valid(pfn)) {
  987. struct page *page = pfn_to_page(pfn);
  988. if (!swsusp_page_is_forbidden(page))
  989. swsusp_unset_page_free(page);
  990. }
  991. for_each_migratetype_order(order, t) {
  992. list_for_each(curr, &zone->free_area[order].free_list[t]) {
  993. unsigned long i;
  994. pfn = page_to_pfn(list_entry(curr, struct page, lru));
  995. for (i = 0; i < (1UL << order); i++)
  996. swsusp_set_page_free(pfn_to_page(pfn + i));
  997. }
  998. }
  999. spin_unlock_irqrestore(&zone->lock, flags);
  1000. }
  1001. #endif /* CONFIG_PM */
  1002. /*
  1003. * Free a 0-order page
  1004. * cold == 1 ? free a cold page : free a hot page
  1005. */
  1006. void free_hot_cold_page(struct page *page, int cold)
  1007. {
  1008. struct zone *zone = page_zone(page);
  1009. struct per_cpu_pages *pcp;
  1010. unsigned long flags;
  1011. int migratetype;
  1012. int wasMlocked = __TestClearPageMlocked(page);
  1013. if (!free_pages_prepare(page, 0))
  1014. return;
  1015. migratetype = get_pageblock_migratetype(page);
  1016. set_page_private(page, migratetype);
  1017. local_irq_save(flags);
  1018. if (unlikely(wasMlocked))
  1019. free_page_mlock(page);
  1020. __count_vm_event(PGFREE);
  1021. /*
  1022. * We only track unmovable, reclaimable and movable on pcp lists.
  1023. * Free ISOLATE pages back to the allocator because they are being
  1024. * offlined but treat RESERVE as movable pages so we can get those
  1025. * areas back if necessary. Otherwise, we may have to free
  1026. * excessively into the page allocator
  1027. */
  1028. if (migratetype >= MIGRATE_PCPTYPES) {
  1029. if (unlikely(migratetype == MIGRATE_ISOLATE)) {
  1030. free_one_page(zone, page, 0, migratetype);
  1031. goto out;
  1032. }
  1033. migratetype = MIGRATE_MOVABLE;
  1034. }
  1035. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  1036. if (cold)
  1037. list_add_tail(&page->lru, &pcp->lists[migratetype]);
  1038. else
  1039. list_add(&page->lru, &pcp->lists[migratetype]);
  1040. pcp->count++;
  1041. if (pcp->count >= pcp->high) {
  1042. free_pcppages_bulk(zone, pcp->batch, pcp);
  1043. pcp->count -= pcp->batch;
  1044. }
  1045. out:
  1046. local_irq_restore(flags);
  1047. }
  1048. /*
  1049. * split_page takes a non-compound higher-order page, and splits it into
  1050. * n (1<<order) sub-pages: page[0..n]
  1051. * Each sub-page must be freed individually.
  1052. *
  1053. * Note: this is probably too low level an operation for use in drivers.
  1054. * Please consult with lkml before using this in your driver.
  1055. */
  1056. void split_page(struct page *page, unsigned int order)
  1057. {
  1058. int i;
  1059. VM_BUG_ON(PageCompound(page));
  1060. VM_BUG_ON(!page_count(page));
  1061. #ifdef CONFIG_KMEMCHECK
  1062. /*
  1063. * Split shadow pages too, because free(page[0]) would
  1064. * otherwise free the whole shadow.
  1065. */
  1066. if (kmemcheck_page_is_tracked(page))
  1067. split_page(virt_to_page(page[0].shadow), order);
  1068. #endif
  1069. for (i = 1; i < (1 << order); i++)
  1070. set_page_refcounted(page + i);
  1071. }
  1072. /*
  1073. * Similar to split_page except the page is already free. As this is only
  1074. * being used for migration, the migratetype of the block also changes.
  1075. * As this is called with interrupts disabled, the caller is responsible
  1076. * for calling arch_alloc_page() and kernel_map_page() after interrupts
  1077. * are enabled.
  1078. *
  1079. * Note: this is probably too low level an operation for use in drivers.
  1080. * Please consult with lkml before using this in your driver.
  1081. */
  1082. int split_free_page(struct page *page)
  1083. {
  1084. unsigned int order;
  1085. unsigned long watermark;
  1086. struct zone *zone;
  1087. BUG_ON(!PageBuddy(page));
  1088. zone = page_zone(page);
  1089. order = page_order(page);
  1090. /* Obey watermarks as if the page was being allocated */
  1091. watermark = low_wmark_pages(zone) + (1 << order);
  1092. if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
  1093. return 0;
  1094. /* Remove page from free list */
  1095. list_del(&page->lru);
  1096. zone->free_area[order].nr_free--;
  1097. rmv_page_order(page);
  1098. __mod_zone_page_state(zone, NR_FREE_PAGES, -(1UL << order));
  1099. /* Split into individual pages */
  1100. set_page_refcounted(page);
  1101. split_page(page, order);
  1102. if (order >= pageblock_order - 1) {
  1103. struct page *endpage = page + (1 << order) - 1;
  1104. for (; page < endpage; page += pageblock_nr_pages)
  1105. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  1106. }
  1107. return 1 << order;
  1108. }
  1109. /*
  1110. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  1111. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  1112. * or two.
  1113. */
  1114. static inline
  1115. struct page *buffered_rmqueue(struct zone *preferred_zone,
  1116. struct zone *zone, int order, gfp_t gfp_flags,
  1117. int migratetype)
  1118. {
  1119. unsigned long flags;
  1120. struct page *page;
  1121. int cold = !!(gfp_flags & __GFP_COLD);
  1122. again:
  1123. if (likely(order == 0)) {
  1124. struct per_cpu_pages *pcp;
  1125. struct list_head *list;
  1126. local_irq_save(flags);
  1127. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  1128. list = &pcp->lists[migratetype];
  1129. if (list_empty(list)) {
  1130. pcp->count += rmqueue_bulk(zone, 0,
  1131. pcp->batch, list,
  1132. migratetype, cold);
  1133. if (unlikely(list_empty(list)))
  1134. goto failed;
  1135. }
  1136. if (cold)
  1137. page = list_entry(list->prev, struct page, lru);
  1138. else
  1139. page = list_entry(list->next, struct page, lru);
  1140. list_del(&page->lru);
  1141. pcp->count--;
  1142. } else {
  1143. if (unlikely(gfp_flags & __GFP_NOFAIL)) {
  1144. /*
  1145. * __GFP_NOFAIL is not to be used in new code.
  1146. *
  1147. * All __GFP_NOFAIL callers should be fixed so that they
  1148. * properly detect and handle allocation failures.
  1149. *
  1150. * We most definitely don't want callers attempting to
  1151. * allocate greater than order-1 page units with
  1152. * __GFP_NOFAIL.
  1153. */
  1154. WARN_ON_ONCE(order > 1);
  1155. }
  1156. spin_lock_irqsave(&zone->lock, flags);
  1157. page = __rmqueue(zone, order, migratetype);
  1158. spin_unlock(&zone->lock);
  1159. if (!page)
  1160. goto failed;
  1161. __mod_zone_page_state(zone, NR_FREE_PAGES, -(1 << order));
  1162. }
  1163. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  1164. zone_statistics(preferred_zone, zone, gfp_flags);
  1165. local_irq_restore(flags);
  1166. VM_BUG_ON(bad_range(zone, page));
  1167. if (prep_new_page(page, order, gfp_flags))
  1168. goto again;
  1169. return page;
  1170. failed:
  1171. local_irq_restore(flags);
  1172. return NULL;
  1173. }
  1174. /* The ALLOC_WMARK bits are used as an index to zone->watermark */
  1175. #define ALLOC_WMARK_MIN WMARK_MIN
  1176. #define ALLOC_WMARK_LOW WMARK_LOW
  1177. #define ALLOC_WMARK_HIGH WMARK_HIGH
  1178. #define ALLOC_NO_WATERMARKS 0x04 /* don't check watermarks at all */
  1179. /* Mask to get the watermark bits */
  1180. #define ALLOC_WMARK_MASK (ALLOC_NO_WATERMARKS-1)
  1181. #define ALLOC_HARDER 0x10 /* try to alloc harder */
  1182. #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
  1183. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  1184. #ifdef CONFIG_FAIL_PAGE_ALLOC
  1185. static struct fail_page_alloc_attr {
  1186. struct fault_attr attr;
  1187. u32 ignore_gfp_highmem;
  1188. u32 ignore_gfp_wait;
  1189. u32 min_order;
  1190. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  1191. struct dentry *ignore_gfp_highmem_file;
  1192. struct dentry *ignore_gfp_wait_file;
  1193. struct dentry *min_order_file;
  1194. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  1195. } fail_page_alloc = {
  1196. .attr = FAULT_ATTR_INITIALIZER,
  1197. .ignore_gfp_wait = 1,
  1198. .ignore_gfp_highmem = 1,
  1199. .min_order = 1,
  1200. };
  1201. static int __init setup_fail_page_alloc(char *str)
  1202. {
  1203. return setup_fault_attr(&fail_page_alloc.attr, str);
  1204. }
  1205. __setup("fail_page_alloc=", setup_fail_page_alloc);
  1206. static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  1207. {
  1208. if (order < fail_page_alloc.min_order)
  1209. return 0;
  1210. if (gfp_mask & __GFP_NOFAIL)
  1211. return 0;
  1212. if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
  1213. return 0;
  1214. if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
  1215. return 0;
  1216. return should_fail(&fail_page_alloc.attr, 1 << order);
  1217. }
  1218. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  1219. static int __init fail_page_alloc_debugfs(void)
  1220. {
  1221. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  1222. struct dentry *dir;
  1223. int err;
  1224. err = init_fault_attr_dentries(&fail_page_alloc.attr,
  1225. "fail_page_alloc");
  1226. if (err)
  1227. return err;
  1228. dir = fail_page_alloc.attr.dentries.dir;
  1229. fail_page_alloc.ignore_gfp_wait_file =
  1230. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  1231. &fail_page_alloc.ignore_gfp_wait);
  1232. fail_page_alloc.ignore_gfp_highmem_file =
  1233. debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  1234. &fail_page_alloc.ignore_gfp_highmem);
  1235. fail_page_alloc.min_order_file =
  1236. debugfs_create_u32("min-order", mode, dir,
  1237. &fail_page_alloc.min_order);
  1238. if (!fail_page_alloc.ignore_gfp_wait_file ||
  1239. !fail_page_alloc.ignore_gfp_highmem_file ||
  1240. !fail_page_alloc.min_order_file) {
  1241. err = -ENOMEM;
  1242. debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
  1243. debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
  1244. debugfs_remove(fail_page_alloc.min_order_file);
  1245. cleanup_fault_attr_dentries(&fail_page_alloc.attr);
  1246. }
  1247. return err;
  1248. }
  1249. late_initcall(fail_page_alloc_debugfs);
  1250. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  1251. #else /* CONFIG_FAIL_PAGE_ALLOC */
  1252. static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  1253. {
  1254. return 0;
  1255. }
  1256. #endif /* CONFIG_FAIL_PAGE_ALLOC */
  1257. /*
  1258. * Return true if free pages are above 'mark'. This takes into account the order
  1259. * of the allocation.
  1260. */
  1261. static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  1262. int classzone_idx, int alloc_flags, long free_pages)
  1263. {
  1264. /* free_pages my go negative - that's OK */
  1265. long min = mark;
  1266. int o;
  1267. free_pages -= (1 << order) + 1;
  1268. if (alloc_flags & ALLOC_HIGH)
  1269. min -= min / 2;
  1270. if (alloc_flags & ALLOC_HARDER)
  1271. min -= min / 4;
  1272. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  1273. return false;
  1274. for (o = 0; o < order; o++) {
  1275. /* At the next order, this order's pages become unavailable */
  1276. free_pages -= z->free_area[o].nr_free << o;
  1277. /* Require fewer higher order pages to be free */
  1278. min >>= 1;
  1279. if (free_pages <= min)
  1280. return false;
  1281. }
  1282. return true;
  1283. }
  1284. bool zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  1285. int classzone_idx, int alloc_flags)
  1286. {
  1287. return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
  1288. zone_page_state(z, NR_FREE_PAGES));
  1289. }
  1290. bool zone_watermark_ok_safe(struct zone *z, int order, unsigned long mark,
  1291. int classzone_idx, int alloc_flags)
  1292. {
  1293. long free_pages = zone_page_state(z, NR_FREE_PAGES);
  1294. if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
  1295. free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
  1296. return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
  1297. free_pages);
  1298. }
  1299. #ifdef CONFIG_NUMA
  1300. /*
  1301. * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
  1302. * skip over zones that are not allowed by the cpuset, or that have
  1303. * been recently (in last second) found to be nearly full. See further
  1304. * comments in mmzone.h. Reduces cache footprint of zonelist scans
  1305. * that have to skip over a lot of full or unallowed zones.
  1306. *
  1307. * If the zonelist cache is present in the passed in zonelist, then
  1308. * returns a pointer to the allowed node mask (either the current
  1309. * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
  1310. *
  1311. * If the zonelist cache is not available for this zonelist, does
  1312. * nothing and returns NULL.
  1313. *
  1314. * If the fullzones BITMAP in the zonelist cache is stale (more than
  1315. * a second since last zap'd) then we zap it out (clear its bits.)
  1316. *
  1317. * We hold off even calling zlc_setup, until after we've checked the
  1318. * first zone in the zonelist, on the theory that most allocations will
  1319. * be satisfied from that first zone, so best to examine that zone as
  1320. * quickly as we can.
  1321. */
  1322. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  1323. {
  1324. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1325. nodemask_t *allowednodes; /* zonelist_cache approximation */
  1326. zlc = zonelist->zlcache_ptr;
  1327. if (!zlc)
  1328. return NULL;
  1329. if (time_after(jiffies, zlc->last_full_zap + HZ)) {
  1330. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  1331. zlc->last_full_zap = jiffies;
  1332. }
  1333. allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
  1334. &cpuset_current_mems_allowed :
  1335. &node_states[N_HIGH_MEMORY];
  1336. return allowednodes;
  1337. }
  1338. /*
  1339. * Given 'z' scanning a zonelist, run a couple of quick checks to see
  1340. * if it is worth looking at further for free memory:
  1341. * 1) Check that the zone isn't thought to be full (doesn't have its
  1342. * bit set in the zonelist_cache fullzones BITMAP).
  1343. * 2) Check that the zones node (obtained from the zonelist_cache
  1344. * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
  1345. * Return true (non-zero) if zone is worth looking at further, or
  1346. * else return false (zero) if it is not.
  1347. *
  1348. * This check -ignores- the distinction between various watermarks,
  1349. * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
  1350. * found to be full for any variation of these watermarks, it will
  1351. * be considered full for up to one second by all requests, unless
  1352. * we are so low on memory on all allowed nodes that we are forced
  1353. * into the second scan of the zonelist.
  1354. *
  1355. * In the second scan we ignore this zonelist cache and exactly
  1356. * apply the watermarks to all zones, even it is slower to do so.
  1357. * We are low on memory in the second scan, and should leave no stone
  1358. * unturned looking for a free page.
  1359. */
  1360. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
  1361. nodemask_t *allowednodes)
  1362. {
  1363. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1364. int i; /* index of *z in zonelist zones */
  1365. int n; /* node that zone *z is on */
  1366. zlc = zonelist->zlcache_ptr;
  1367. if (!zlc)
  1368. return 1;
  1369. i = z - zonelist->_zonerefs;
  1370. n = zlc->z_to_n[i];
  1371. /* This zone is worth trying if it is allowed but not full */
  1372. return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
  1373. }
  1374. /*
  1375. * Given 'z' scanning a zonelist, set the corresponding bit in
  1376. * zlc->fullzones, so that subsequent attempts to allocate a page
  1377. * from that zone don't waste time re-examining it.
  1378. */
  1379. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
  1380. {
  1381. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1382. int i; /* index of *z in zonelist zones */
  1383. zlc = zonelist->zlcache_ptr;
  1384. if (!zlc)
  1385. return;
  1386. i = z - zonelist->_zonerefs;
  1387. set_bit(i, zlc->fullzones);
  1388. }
  1389. #else /* CONFIG_NUMA */
  1390. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  1391. {
  1392. return NULL;
  1393. }
  1394. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
  1395. nodemask_t *allowednodes)
  1396. {
  1397. return 1;
  1398. }
  1399. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
  1400. {
  1401. }
  1402. #endif /* CONFIG_NUMA */
  1403. /*
  1404. * get_page_from_freelist goes through the zonelist trying to allocate
  1405. * a page.
  1406. */
  1407. static struct page *
  1408. get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
  1409. struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
  1410. struct zone *preferred_zone, int migratetype)
  1411. {
  1412. struct zoneref *z;
  1413. struct page *page = NULL;
  1414. int classzone_idx;
  1415. struct zone *zone;
  1416. nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
  1417. int zlc_active = 0; /* set if using zonelist_cache */
  1418. int did_zlc_setup = 0; /* just call zlc_setup() one time */
  1419. classzone_idx = zone_idx(preferred_zone);
  1420. zonelist_scan:
  1421. /*
  1422. * Scan zonelist, looking for a zone with enough free.
  1423. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1424. */
  1425. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1426. high_zoneidx, nodemask) {
  1427. if (NUMA_BUILD && zlc_active &&
  1428. !zlc_zone_worth_trying(zonelist, z, allowednodes))
  1429. continue;
  1430. if ((alloc_flags & ALLOC_CPUSET) &&
  1431. !cpuset_zone_allowed_softwall(zone, gfp_mask))
  1432. goto try_next_zone;
  1433. BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
  1434. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  1435. unsigned long mark;
  1436. int ret;
  1437. mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
  1438. if (zone_watermark_ok(zone, order, mark,
  1439. classzone_idx, alloc_flags))
  1440. goto try_this_zone;
  1441. if (zone_reclaim_mode == 0)
  1442. goto this_zone_full;
  1443. ret = zone_reclaim(zone, gfp_mask, order);
  1444. switch (ret) {
  1445. case ZONE_RECLAIM_NOSCAN:
  1446. /* did not scan */
  1447. goto try_next_zone;
  1448. case ZONE_RECLAIM_FULL:
  1449. /* scanned but unreclaimable */
  1450. goto this_zone_full;
  1451. default:
  1452. /* did we reclaim enough */
  1453. if (!zone_watermark_ok(zone, order, mark,
  1454. classzone_idx, alloc_flags))
  1455. goto this_zone_full;
  1456. }
  1457. }
  1458. try_this_zone:
  1459. page = buffered_rmqueue(preferred_zone, zone, order,
  1460. gfp_mask, migratetype);
  1461. if (page)
  1462. break;
  1463. this_zone_full:
  1464. if (NUMA_BUILD)
  1465. zlc_mark_zone_full(zonelist, z);
  1466. try_next_zone:
  1467. if (NUMA_BUILD && !did_zlc_setup && nr_online_nodes > 1) {
  1468. /*
  1469. * we do zlc_setup after the first zone is tried but only
  1470. * if there are multiple nodes make it worthwhile
  1471. */
  1472. allowednodes = zlc_setup(zonelist, alloc_flags);
  1473. zlc_active = 1;
  1474. did_zlc_setup = 1;
  1475. }
  1476. }
  1477. if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
  1478. /* Disable zlc cache for second zonelist scan */
  1479. zlc_active = 0;
  1480. goto zonelist_scan;
  1481. }
  1482. return page;
  1483. }
  1484. /*
  1485. * Large machines with many possible nodes should not always dump per-node
  1486. * meminfo in irq context.
  1487. */
  1488. static inline bool should_suppress_show_mem(void)
  1489. {
  1490. bool ret = false;
  1491. #if NODES_SHIFT > 8
  1492. ret = in_interrupt();
  1493. #endif
  1494. return ret;
  1495. }
  1496. static inline int
  1497. should_alloc_retry(gfp_t gfp_mask, unsigned int order,
  1498. unsigned long pages_reclaimed)
  1499. {
  1500. /* Do not loop if specifically requested */
  1501. if (gfp_mask & __GFP_NORETRY)
  1502. return 0;
  1503. /*
  1504. * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
  1505. * means __GFP_NOFAIL, but that may not be true in other
  1506. * implementations.
  1507. */
  1508. if (order <= PAGE_ALLOC_COSTLY_ORDER)
  1509. return 1;
  1510. /*
  1511. * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
  1512. * specified, then we retry until we no longer reclaim any pages
  1513. * (above), or we've reclaimed an order of pages at least as
  1514. * large as the allocation's order. In both cases, if the
  1515. * allocation still fails, we stop retrying.
  1516. */
  1517. if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
  1518. return 1;
  1519. /*
  1520. * Don't let big-order allocations loop unless the caller
  1521. * explicitly requests that.
  1522. */
  1523. if (gfp_mask & __GFP_NOFAIL)
  1524. return 1;
  1525. return 0;
  1526. }
  1527. static inline struct page *
  1528. __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
  1529. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1530. nodemask_t *nodemask, struct zone *preferred_zone,
  1531. int migratetype)
  1532. {
  1533. struct page *page;
  1534. /* Acquire the OOM killer lock for the zones in zonelist */
  1535. if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
  1536. schedule_timeout_uninterruptible(1);
  1537. return NULL;
  1538. }
  1539. /*
  1540. * Go through the zonelist yet one more time, keep very high watermark
  1541. * here, this is only to catch a parallel oom killing, we must fail if
  1542. * we're still under heavy pressure.
  1543. */
  1544. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
  1545. order, zonelist, high_zoneidx,
  1546. ALLOC_WMARK_HIGH|ALLOC_CPUSET,
  1547. preferred_zone, migratetype);
  1548. if (page)
  1549. goto out;
  1550. if (!(gfp_mask & __GFP_NOFAIL)) {
  1551. /* The OOM killer will not help higher order allocs */
  1552. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1553. goto out;
  1554. /* The OOM killer does not needlessly kill tasks for lowmem */
  1555. if (high_zoneidx < ZONE_NORMAL)
  1556. goto out;
  1557. /*
  1558. * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
  1559. * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
  1560. * The caller should handle page allocation failure by itself if
  1561. * it specifies __GFP_THISNODE.
  1562. * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
  1563. */
  1564. if (gfp_mask & __GFP_THISNODE)
  1565. goto out;
  1566. }
  1567. /* Exhausted what can be done so it's blamo time */
  1568. out_of_memory(zonelist, gfp_mask, order, nodemask);
  1569. out:
  1570. clear_zonelist_oom(zonelist, gfp_mask);
  1571. return page;
  1572. }
  1573. #ifdef CONFIG_COMPACTION
  1574. /* Try memory compaction for high-order allocations before reclaim */
  1575. static struct page *
  1576. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  1577. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1578. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1579. int migratetype, unsigned long *did_some_progress,
  1580. bool sync_migration)
  1581. {
  1582. struct page *page;
  1583. if (!order || compaction_deferred(preferred_zone))
  1584. return NULL;
  1585. current->flags |= PF_MEMALLOC;
  1586. *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
  1587. nodemask, sync_migration);
  1588. current->flags &= ~PF_MEMALLOC;
  1589. if (*did_some_progress != COMPACT_SKIPPED) {
  1590. /* Page migration frees to the PCP lists but we want merging */
  1591. drain_pages(get_cpu());
  1592. put_cpu();
  1593. page = get_page_from_freelist(gfp_mask, nodemask,
  1594. order, zonelist, high_zoneidx,
  1595. alloc_flags, preferred_zone,
  1596. migratetype);
  1597. if (page) {
  1598. preferred_zone->compact_considered = 0;
  1599. preferred_zone->compact_defer_shift = 0;
  1600. count_vm_event(COMPACTSUCCESS);
  1601. return page;
  1602. }
  1603. /*
  1604. * It's bad if compaction run occurs and fails.
  1605. * The most likely reason is that pages exist,
  1606. * but not enough to satisfy watermarks.
  1607. */
  1608. count_vm_event(COMPACTFAIL);
  1609. defer_compaction(preferred_zone);
  1610. cond_resched();
  1611. }
  1612. return NULL;
  1613. }
  1614. #else
  1615. static inline struct page *
  1616. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  1617. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1618. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1619. int migratetype, unsigned long *did_some_progress,
  1620. bool sync_migration)
  1621. {
  1622. return NULL;
  1623. }
  1624. #endif /* CONFIG_COMPACTION */
  1625. /* The really slow allocator path where we enter direct reclaim */
  1626. static inline struct page *
  1627. __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
  1628. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1629. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1630. int migratetype, unsigned long *did_some_progress)
  1631. {
  1632. struct page *page = NULL;
  1633. struct reclaim_state reclaim_state;
  1634. bool drained = false;
  1635. cond_resched();
  1636. /* We now go into synchronous reclaim */
  1637. cpuset_memory_pressure_bump();
  1638. current->flags |= PF_MEMALLOC;
  1639. lockdep_set_current_reclaim_state(gfp_mask);
  1640. reclaim_state.reclaimed_slab = 0;
  1641. current->reclaim_state = &reclaim_state;
  1642. *did_some_progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
  1643. current->reclaim_state = NULL;
  1644. lockdep_clear_current_reclaim_state();
  1645. current->flags &= ~PF_MEMALLOC;
  1646. cond_resched();
  1647. if (unlikely(!(*did_some_progress)))
  1648. return NULL;
  1649. retry:
  1650. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1651. zonelist, high_zoneidx,
  1652. alloc_flags, preferred_zone,
  1653. migratetype);
  1654. /*
  1655. * If an allocation failed after direct reclaim, it could be because
  1656. * pages are pinned on the per-cpu lists. Drain them and try again
  1657. */
  1658. if (!page && !drained) {
  1659. drain_all_pages();
  1660. drained = true;
  1661. goto retry;
  1662. }
  1663. return page;
  1664. }
  1665. /*
  1666. * This is called in the allocator slow-path if the allocation request is of
  1667. * sufficient urgency to ignore watermarks and take other desperate measures
  1668. */
  1669. static inline struct page *
  1670. __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
  1671. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1672. nodemask_t *nodemask, struct zone *preferred_zone,
  1673. int migratetype)
  1674. {
  1675. struct page *page;
  1676. do {
  1677. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1678. zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
  1679. preferred_zone, migratetype);
  1680. if (!page && gfp_mask & __GFP_NOFAIL)
  1681. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
  1682. } while (!page && (gfp_mask & __GFP_NOFAIL));
  1683. return page;
  1684. }
  1685. static inline
  1686. void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
  1687. enum zone_type high_zoneidx,
  1688. enum zone_type classzone_idx)
  1689. {
  1690. struct zoneref *z;
  1691. struct zone *zone;
  1692. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
  1693. wakeup_kswapd(zone, order, classzone_idx);
  1694. }
  1695. static inline int
  1696. gfp_to_alloc_flags(gfp_t gfp_mask)
  1697. {
  1698. int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
  1699. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1700. /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
  1701. BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
  1702. /*
  1703. * The caller may dip into page reserves a bit more if the caller
  1704. * cannot run direct reclaim, or if the caller has realtime scheduling
  1705. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  1706. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  1707. */
  1708. alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
  1709. if (!wait) {
  1710. /*
  1711. * Not worth trying to allocate harder for
  1712. * __GFP_NOMEMALLOC even if it can't schedule.
  1713. */
  1714. if (!(gfp_mask & __GFP_NOMEMALLOC))
  1715. alloc_flags |= ALLOC_HARDER;
  1716. /*
  1717. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  1718. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1719. */
  1720. alloc_flags &= ~ALLOC_CPUSET;
  1721. } else if (unlikely(rt_task(current)) && !in_interrupt())
  1722. alloc_flags |= ALLOC_HARDER;
  1723. if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
  1724. if (!in_interrupt() &&
  1725. ((current->flags & PF_MEMALLOC) ||
  1726. unlikely(test_thread_flag(TIF_MEMDIE))))
  1727. alloc_flags |= ALLOC_NO_WATERMARKS;
  1728. }
  1729. return alloc_flags;
  1730. }
  1731. static inline struct page *
  1732. __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
  1733. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1734. nodemask_t *nodemask, struct zone *preferred_zone,
  1735. int migratetype)
  1736. {
  1737. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1738. struct page *page = NULL;
  1739. int alloc_flags;
  1740. unsigned long pages_reclaimed = 0;
  1741. unsigned long did_some_progress;
  1742. bool sync_migration = false;
  1743. /*
  1744. * In the slowpath, we sanity check order to avoid ever trying to
  1745. * reclaim >= MAX_ORDER areas which will never succeed. Callers may
  1746. * be using allocators in order of preference for an area that is
  1747. * too large.
  1748. */
  1749. if (order >= MAX_ORDER) {
  1750. WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
  1751. return NULL;
  1752. }
  1753. /*
  1754. * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
  1755. * __GFP_NOWARN set) should not cause reclaim since the subsystem
  1756. * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
  1757. * using a larger set of nodes after it has established that the
  1758. * allowed per node queues are empty and that nodes are
  1759. * over allocated.
  1760. */
  1761. if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
  1762. goto nopage;
  1763. restart:
  1764. if (!(gfp_mask & __GFP_NO_KSWAPD))
  1765. wake_all_kswapd(order, zonelist, high_zoneidx,
  1766. zone_idx(preferred_zone));
  1767. /*
  1768. * OK, we're below the kswapd watermark and have kicked background
  1769. * reclaim. Now things get more complex, so set up alloc_flags according
  1770. * to how we want to proceed.
  1771. */
  1772. alloc_flags = gfp_to_alloc_flags(gfp_mask);
  1773. /*
  1774. * Find the true preferred zone if the allocation is unconstrained by
  1775. * cpusets.
  1776. */
  1777. if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)
  1778. first_zones_zonelist(zonelist, high_zoneidx, NULL,
  1779. &preferred_zone);
  1780. /* This is the last chance, in general, before the goto nopage. */
  1781. page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
  1782. high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
  1783. preferred_zone, migratetype);
  1784. if (page)
  1785. goto got_pg;
  1786. rebalance:
  1787. /* Allocate without watermarks if the context allows */
  1788. if (alloc_flags & ALLOC_NO_WATERMARKS) {
  1789. page = __alloc_pages_high_priority(gfp_mask, order,
  1790. zonelist, high_zoneidx, nodemask,
  1791. preferred_zone, migratetype);
  1792. if (page)
  1793. goto got_pg;
  1794. }
  1795. /* Atomic allocations - we can't balance anything */
  1796. if (!wait)
  1797. goto nopage;
  1798. /* Avoid recursion of direct reclaim */
  1799. if (current->flags & PF_MEMALLOC)
  1800. goto nopage;
  1801. /* Avoid allocations with no watermarks from looping endlessly */
  1802. if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
  1803. goto nopage;
  1804. /*
  1805. * Try direct compaction. The first pass is asynchronous. Subsequent
  1806. * attempts after direct reclaim are synchronous
  1807. */
  1808. page = __alloc_pages_direct_compact(gfp_mask, order,
  1809. zonelist, high_zoneidx,
  1810. nodemask,
  1811. alloc_flags, preferred_zone,
  1812. migratetype, &did_some_progress,
  1813. sync_migration);
  1814. if (page)
  1815. goto got_pg;
  1816. sync_migration = !(gfp_mask & __GFP_NO_KSWAPD);
  1817. /* Try direct reclaim and then allocating */
  1818. page = __alloc_pages_direct_reclaim(gfp_mask, order,
  1819. zonelist, high_zoneidx,
  1820. nodemask,
  1821. alloc_flags, preferred_zone,
  1822. migratetype, &did_some_progress);
  1823. if (page)
  1824. goto got_pg;
  1825. /*
  1826. * If we failed to make any progress reclaiming, then we are
  1827. * running out of options and have to consider going OOM
  1828. */
  1829. if (!did_some_progress) {
  1830. if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  1831. if (oom_killer_disabled)
  1832. goto nopage;
  1833. page = __alloc_pages_may_oom(gfp_mask, order,
  1834. zonelist, high_zoneidx,
  1835. nodemask, preferred_zone,
  1836. migratetype);
  1837. if (page)
  1838. goto got_pg;
  1839. if (!(gfp_mask & __GFP_NOFAIL)) {
  1840. /*
  1841. * The oom killer is not called for high-order
  1842. * allocations that may fail, so if no progress
  1843. * is being made, there are no other options and
  1844. * retrying is unlikely to help.
  1845. */
  1846. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1847. goto nopage;
  1848. /*
  1849. * The oom killer is not called for lowmem
  1850. * allocations to prevent needlessly killing
  1851. * innocent tasks.
  1852. */
  1853. if (high_zoneidx < ZONE_NORMAL)
  1854. goto nopage;
  1855. }
  1856. goto restart;
  1857. }
  1858. }
  1859. /* Check if we should retry the allocation */
  1860. pages_reclaimed += did_some_progress;
  1861. if (should_alloc_retry(gfp_mask, order, pages_reclaimed)) {
  1862. /* Wait for some write requests to complete then retry */
  1863. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
  1864. goto rebalance;
  1865. } else {
  1866. /*
  1867. * High-order allocations do not necessarily loop after
  1868. * direct reclaim and reclaim/compaction depends on compaction
  1869. * being called after reclaim so call directly if necessary
  1870. */
  1871. page = __alloc_pages_direct_compact(gfp_mask, order,
  1872. zonelist, high_zoneidx,
  1873. nodemask,
  1874. alloc_flags, preferred_zone,
  1875. migratetype, &did_some_progress,
  1876. sync_migration);
  1877. if (page)
  1878. goto got_pg;
  1879. }
  1880. nopage:
  1881. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  1882. unsigned int filter = SHOW_MEM_FILTER_NODES;
  1883. /*
  1884. * This documents exceptions given to allocations in certain
  1885. * contexts that are allowed to allocate outside current's set
  1886. * of allowed nodes.
  1887. */
  1888. if (!(gfp_mask & __GFP_NOMEMALLOC))
  1889. if (test_thread_flag(TIF_MEMDIE) ||
  1890. (current->flags & (PF_MEMALLOC | PF_EXITING)))
  1891. filter &= ~SHOW_MEM_FILTER_NODES;
  1892. if (in_interrupt() || !wait)
  1893. filter &= ~SHOW_MEM_FILTER_NODES;
  1894. pr_warning("%s: page allocation failure. order:%d, mode:0x%x\n",
  1895. current->comm, order, gfp_mask);
  1896. dump_stack();
  1897. if (!should_suppress_show_mem())
  1898. show_mem(filter);
  1899. }
  1900. return page;
  1901. got_pg:
  1902. if (kmemcheck_enabled)
  1903. kmemcheck_pagealloc_alloc(page, order, gfp_mask);
  1904. return page;
  1905. }
  1906. /*
  1907. * This is the 'heart' of the zoned buddy allocator.
  1908. */
  1909. struct page *
  1910. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
  1911. struct zonelist *zonelist, nodemask_t *nodemask)
  1912. {
  1913. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  1914. struct zone *preferred_zone;
  1915. struct page *page;
  1916. int migratetype = allocflags_to_migratetype(gfp_mask);
  1917. gfp_mask &= gfp_allowed_mask;
  1918. lockdep_trace_alloc(gfp_mask);
  1919. might_sleep_if(gfp_mask & __GFP_WAIT);
  1920. if (should_fail_alloc_page(gfp_mask, order))
  1921. return NULL;
  1922. /*
  1923. * Check the zones suitable for the gfp_mask contain at least one
  1924. * valid zone. It's possible to have an empty zonelist as a result
  1925. * of GFP_THISNODE and a memoryless node
  1926. */
  1927. if (unlikely(!zonelist->_zonerefs->zone))
  1928. return NULL;
  1929. get_mems_allowed();
  1930. /* The preferred zone is used for statistics later */
  1931. first_zones_zonelist(zonelist, high_zoneidx,
  1932. nodemask ? : &cpuset_current_mems_allowed,
  1933. &preferred_zone);
  1934. if (!preferred_zone) {
  1935. put_mems_allowed();
  1936. return NULL;
  1937. }
  1938. /* First allocation attempt */
  1939. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
  1940. zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET,
  1941. preferred_zone, migratetype);
  1942. if (unlikely(!page))
  1943. page = __alloc_pages_slowpath(gfp_mask, order,
  1944. zonelist, high_zoneidx, nodemask,
  1945. preferred_zone, migratetype);
  1946. put_mems_allowed();
  1947. trace_mm_page_alloc(page, order, gfp_mask, migratetype);
  1948. return page;
  1949. }
  1950. EXPORT_SYMBOL(__alloc_pages_nodemask);
  1951. /*
  1952. * Common helper functions.
  1953. */
  1954. unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  1955. {
  1956. struct page *page;
  1957. /*
  1958. * __get_free_pages() returns a 32-bit address, which cannot represent
  1959. * a highmem page
  1960. */
  1961. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1962. page = alloc_pages(gfp_mask, order);
  1963. if (!page)
  1964. return 0;
  1965. return (unsigned long) page_address(page);
  1966. }
  1967. EXPORT_SYMBOL(__get_free_pages);
  1968. unsigned long get_zeroed_page(gfp_t gfp_mask)
  1969. {
  1970. return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
  1971. }
  1972. EXPORT_SYMBOL(get_zeroed_page);
  1973. void __pagevec_free(struct pagevec *pvec)
  1974. {
  1975. int i = pagevec_count(pvec);
  1976. while (--i >= 0) {
  1977. trace_mm_pagevec_free(pvec->pages[i], pvec->cold);
  1978. free_hot_cold_page(pvec->pages[i], pvec->cold);
  1979. }
  1980. }
  1981. void __free_pages(struct page *page, unsigned int order)
  1982. {
  1983. if (put_page_testzero(page)) {
  1984. if (order == 0)
  1985. free_hot_cold_page(page, 0);
  1986. else
  1987. __free_pages_ok(page, order);
  1988. }
  1989. }
  1990. EXPORT_SYMBOL(__free_pages);
  1991. void free_pages(unsigned long addr, unsigned int order)
  1992. {
  1993. if (addr != 0) {
  1994. VM_BUG_ON(!virt_addr_valid((void *)addr));
  1995. __free_pages(virt_to_page((void *)addr), order);
  1996. }
  1997. }
  1998. EXPORT_SYMBOL(free_pages);
  1999. static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
  2000. {
  2001. if (addr) {
  2002. unsigned long alloc_end = addr + (PAGE_SIZE << order);
  2003. unsigned long used = addr + PAGE_ALIGN(size);
  2004. split_page(virt_to_page((void *)addr), order);
  2005. while (used < alloc_end) {
  2006. free_page(used);
  2007. used += PAGE_SIZE;
  2008. }
  2009. }
  2010. return (void *)addr;
  2011. }
  2012. /**
  2013. * alloc_pages_exact - allocate an exact number physically-contiguous pages.
  2014. * @size: the number of bytes to allocate
  2015. * @gfp_mask: GFP flags for the allocation
  2016. *
  2017. * This function is similar to alloc_pages(), except that it allocates the
  2018. * minimum number of pages to satisfy the request. alloc_pages() can only
  2019. * allocate memory in power-of-two pages.
  2020. *
  2021. * This function is also limited by MAX_ORDER.
  2022. *
  2023. * Memory allocated by this function must be released by free_pages_exact().
  2024. */
  2025. void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
  2026. {
  2027. unsigned int order = get_order(size);
  2028. unsigned long addr;
  2029. addr = __get_free_pages(gfp_mask, order);
  2030. return make_alloc_exact(addr, order, size);
  2031. }
  2032. EXPORT_SYMBOL(alloc_pages_exact);
  2033. /**
  2034. * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
  2035. * pages on a node.
  2036. * @nid: the preferred node ID where memory should be allocated
  2037. * @size: the number of bytes to allocate
  2038. * @gfp_mask: GFP flags for the allocation
  2039. *
  2040. * Like alloc_pages_exact(), but try to allocate on node nid first before falling
  2041. * back.
  2042. * Note this is not alloc_pages_exact_node() which allocates on a specific node,
  2043. * but is not exact.
  2044. */
  2045. void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
  2046. {
  2047. unsigned order = get_order(size);
  2048. struct page *p = alloc_pages_node(nid, gfp_mask, order);
  2049. if (!p)
  2050. return NULL;
  2051. return make_alloc_exact((unsigned long)page_address(p), order, size);
  2052. }
  2053. EXPORT_SYMBOL(alloc_pages_exact_nid);
  2054. /**
  2055. * free_pages_exact - release memory allocated via alloc_pages_exact()
  2056. * @virt: the value returned by alloc_pages_exact.
  2057. * @size: size of allocation, same value as passed to alloc_pages_exact().
  2058. *
  2059. * Release the memory allocated by a previous call to alloc_pages_exact.
  2060. */
  2061. void free_pages_exact(void *virt, size_t size)
  2062. {
  2063. unsigned long addr = (unsigned long)virt;
  2064. unsigned long end = addr + PAGE_ALIGN(size);
  2065. while (addr < end) {
  2066. free_page(addr);
  2067. addr += PAGE_SIZE;
  2068. }
  2069. }
  2070. EXPORT_SYMBOL(free_pages_exact);
  2071. static unsigned int nr_free_zone_pages(int offset)
  2072. {
  2073. struct zoneref *z;
  2074. struct zone *zone;
  2075. /* Just pick one node, since fallback list is circular */
  2076. unsigned int sum = 0;
  2077. struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
  2078. for_each_zone_zonelist(zone, z, zonelist, offset) {
  2079. unsigned long size = zone->present_pages;
  2080. unsigned long high = high_wmark_pages(zone);
  2081. if (size > high)
  2082. sum += size - high;
  2083. }
  2084. return sum;
  2085. }
  2086. /*
  2087. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  2088. */
  2089. unsigned int nr_free_buffer_pages(void)
  2090. {
  2091. return nr_free_zone_pages(gfp_zone(GFP_USER));
  2092. }
  2093. EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
  2094. /*
  2095. * Amount of free RAM allocatable within all zones
  2096. */
  2097. unsigned int nr_free_pagecache_pages(void)
  2098. {
  2099. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  2100. }
  2101. static inline void show_node(struct zone *zone)
  2102. {
  2103. if (NUMA_BUILD)
  2104. printk("Node %d ", zone_to_nid(zone));
  2105. }
  2106. void si_meminfo(struct sysinfo *val)
  2107. {
  2108. val->totalram = totalram_pages;
  2109. val->sharedram = 0;
  2110. val->freeram = global_page_state(NR_FREE_PAGES);
  2111. val->bufferram = nr_blockdev_pages();
  2112. val->totalhigh = totalhigh_pages;
  2113. val->freehigh = nr_free_highpages();
  2114. val->mem_unit = PAGE_SIZE;
  2115. }
  2116. EXPORT_SYMBOL(si_meminfo);
  2117. #ifdef CONFIG_NUMA
  2118. void si_meminfo_node(struct sysinfo *val, int nid)
  2119. {
  2120. pg_data_t *pgdat = NODE_DATA(nid);
  2121. val->totalram = pgdat->node_present_pages;
  2122. val->freeram = node_page_state(nid, NR_FREE_PAGES);
  2123. #ifdef CONFIG_HIGHMEM
  2124. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  2125. val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  2126. NR_FREE_PAGES);
  2127. #else
  2128. val->totalhigh = 0;
  2129. val->freehigh = 0;
  2130. #endif
  2131. val->mem_unit = PAGE_SIZE;
  2132. }
  2133. #endif
  2134. /*
  2135. * Determine whether the node should be displayed or not, depending on whether
  2136. * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
  2137. */
  2138. bool skip_free_areas_node(unsigned int flags, int nid)
  2139. {
  2140. bool ret = false;
  2141. if (!(flags & SHOW_MEM_FILTER_NODES))
  2142. goto out;
  2143. get_mems_allowed();
  2144. ret = !node_isset(nid, cpuset_current_mems_allowed);
  2145. put_mems_allowed();
  2146. out:
  2147. return ret;
  2148. }
  2149. #define K(x) ((x) << (PAGE_SHIFT-10))
  2150. /*
  2151. * Show free area list (used inside shift_scroll-lock stuff)
  2152. * We also calculate the percentage fragmentation. We do this by counting the
  2153. * memory on each free list with the exception of the first item on the list.
  2154. * Suppresses nodes that are not allowed by current's cpuset if
  2155. * SHOW_MEM_FILTER_NODES is passed.
  2156. */
  2157. void show_free_areas(unsigned int filter)
  2158. {
  2159. int cpu;
  2160. struct zone *zone;
  2161. for_each_populated_zone(zone) {
  2162. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2163. continue;
  2164. show_node(zone);
  2165. printk("%s per-cpu:\n", zone->name);
  2166. for_each_online_cpu(cpu) {
  2167. struct per_cpu_pageset *pageset;
  2168. pageset = per_cpu_ptr(zone->pageset, cpu);
  2169. printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
  2170. cpu, pageset->pcp.high,
  2171. pageset->pcp.batch, pageset->pcp.count);
  2172. }
  2173. }
  2174. printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
  2175. " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
  2176. " unevictable:%lu"
  2177. " dirty:%lu writeback:%lu unstable:%lu\n"
  2178. " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
  2179. " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n",
  2180. global_page_state(NR_ACTIVE_ANON),
  2181. global_page_state(NR_INACTIVE_ANON),
  2182. global_page_state(NR_ISOLATED_ANON),
  2183. global_page_state(NR_ACTIVE_FILE),
  2184. global_page_state(NR_INACTIVE_FILE),
  2185. global_page_state(NR_ISOLATED_FILE),
  2186. global_page_state(NR_UNEVICTABLE),
  2187. global_page_state(NR_FILE_DIRTY),
  2188. global_page_state(NR_WRITEBACK),
  2189. global_page_state(NR_UNSTABLE_NFS),
  2190. global_page_state(NR_FREE_PAGES),
  2191. global_page_state(NR_SLAB_RECLAIMABLE),
  2192. global_page_state(NR_SLAB_UNRECLAIMABLE),
  2193. global_page_state(NR_FILE_MAPPED),
  2194. global_page_state(NR_SHMEM),
  2195. global_page_state(NR_PAGETABLE),
  2196. global_page_state(NR_BOUNCE));
  2197. for_each_populated_zone(zone) {
  2198. int i;
  2199. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2200. continue;
  2201. show_node(zone);
  2202. printk("%s"
  2203. " free:%lukB"
  2204. " min:%lukB"
  2205. " low:%lukB"
  2206. " high:%lukB"
  2207. " active_anon:%lukB"
  2208. " inactive_anon:%lukB"
  2209. " active_file:%lukB"
  2210. " inactive_file:%lukB"
  2211. " unevictable:%lukB"
  2212. " isolated(anon):%lukB"
  2213. " isolated(file):%lukB"
  2214. " present:%lukB"
  2215. " mlocked:%lukB"
  2216. " dirty:%lukB"
  2217. " writeback:%lukB"
  2218. " mapped:%lukB"
  2219. " shmem:%lukB"
  2220. " slab_reclaimable:%lukB"
  2221. " slab_unreclaimable:%lukB"
  2222. " kernel_stack:%lukB"
  2223. " pagetables:%lukB"
  2224. " unstable:%lukB"
  2225. " bounce:%lukB"
  2226. " writeback_tmp:%lukB"
  2227. " pages_scanned:%lu"
  2228. " all_unreclaimable? %s"
  2229. "\n",
  2230. zone->name,
  2231. K(zone_page_state(zone, NR_FREE_PAGES)),
  2232. K(min_wmark_pages(zone)),
  2233. K(low_wmark_pages(zone)),
  2234. K(high_wmark_pages(zone)),
  2235. K(zone_page_state(zone, NR_ACTIVE_ANON)),
  2236. K(zone_page_state(zone, NR_INACTIVE_ANON)),
  2237. K(zone_page_state(zone, NR_ACTIVE_FILE)),
  2238. K(zone_page_state(zone, NR_INACTIVE_FILE)),
  2239. K(zone_page_state(zone, NR_UNEVICTABLE)),
  2240. K(zone_page_state(zone, NR_ISOLATED_ANON)),
  2241. K(zone_page_state(zone, NR_ISOLATED_FILE)),
  2242. K(zone->present_pages),
  2243. K(zone_page_state(zone, NR_MLOCK)),
  2244. K(zone_page_state(zone, NR_FILE_DIRTY)),
  2245. K(zone_page_state(zone, NR_WRITEBACK)),
  2246. K(zone_page_state(zone, NR_FILE_MAPPED)),
  2247. K(zone_page_state(zone, NR_SHMEM)),
  2248. K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
  2249. K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
  2250. zone_page_state(zone, NR_KERNEL_STACK) *
  2251. THREAD_SIZE / 1024,
  2252. K(zone_page_state(zone, NR_PAGETABLE)),
  2253. K(zone_page_state(zone, NR_UNSTABLE_NFS)),
  2254. K(zone_page_state(zone, NR_BOUNCE)),
  2255. K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
  2256. zone->pages_scanned,
  2257. (zone->all_unreclaimable ? "yes" : "no")
  2258. );
  2259. printk("lowmem_reserve[]:");
  2260. for (i = 0; i < MAX_NR_ZONES; i++)
  2261. printk(" %lu", zone->lowmem_reserve[i]);
  2262. printk("\n");
  2263. }
  2264. for_each_populated_zone(zone) {
  2265. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  2266. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2267. continue;
  2268. show_node(zone);
  2269. printk("%s: ", zone->name);
  2270. spin_lock_irqsave(&zone->lock, flags);
  2271. for (order = 0; order < MAX_ORDER; order++) {
  2272. nr[order] = zone->free_area[order].nr_free;
  2273. total += nr[order] << order;
  2274. }
  2275. spin_unlock_irqrestore(&zone->lock, flags);
  2276. for (order = 0; order < MAX_ORDER; order++)
  2277. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  2278. printk("= %lukB\n", K(total));
  2279. }
  2280. printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
  2281. show_swap_cache_info();
  2282. }
  2283. static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
  2284. {
  2285. zoneref->zone = zone;
  2286. zoneref->zone_idx = zone_idx(zone);
  2287. }
  2288. /*
  2289. * Builds allocation fallback zone lists.
  2290. *
  2291. * Add all populated zones of a node to the zonelist.
  2292. */
  2293. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  2294. int nr_zones, enum zone_type zone_type)
  2295. {
  2296. struct zone *zone;
  2297. BUG_ON(zone_type >= MAX_NR_ZONES);
  2298. zone_type++;
  2299. do {
  2300. zone_type--;
  2301. zone = pgdat->node_zones + zone_type;
  2302. if (populated_zone(zone)) {
  2303. zoneref_set_zone(zone,
  2304. &zonelist->_zonerefs[nr_zones++]);
  2305. check_highest_zone(zone_type);
  2306. }
  2307. } while (zone_type);
  2308. return nr_zones;
  2309. }
  2310. /*
  2311. * zonelist_order:
  2312. * 0 = automatic detection of better ordering.
  2313. * 1 = order by ([node] distance, -zonetype)
  2314. * 2 = order by (-zonetype, [node] distance)
  2315. *
  2316. * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
  2317. * the same zonelist. So only NUMA can configure this param.
  2318. */
  2319. #define ZONELIST_ORDER_DEFAULT 0
  2320. #define ZONELIST_ORDER_NODE 1
  2321. #define ZONELIST_ORDER_ZONE 2
  2322. /* zonelist order in the kernel.
  2323. * set_zonelist_order() will set this to NODE or ZONE.
  2324. */
  2325. static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2326. static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
  2327. #ifdef CONFIG_NUMA
  2328. /* The value user specified ....changed by config */
  2329. static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2330. /* string for sysctl */
  2331. #define NUMA_ZONELIST_ORDER_LEN 16
  2332. char numa_zonelist_order[16] = "default";
  2333. /*
  2334. * interface for configure zonelist ordering.
  2335. * command line option "numa_zonelist_order"
  2336. * = "[dD]efault - default, automatic configuration.
  2337. * = "[nN]ode - order by node locality, then by zone within node
  2338. * = "[zZ]one - order by zone, then by locality within zone
  2339. */
  2340. static int __parse_numa_zonelist_order(char *s)
  2341. {
  2342. if (*s == 'd' || *s == 'D') {
  2343. user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2344. } else if (*s == 'n' || *s == 'N') {
  2345. user_zonelist_order = ZONELIST_ORDER_NODE;
  2346. } else if (*s == 'z' || *s == 'Z') {
  2347. user_zonelist_order = ZONELIST_ORDER_ZONE;
  2348. } else {
  2349. printk(KERN_WARNING
  2350. "Ignoring invalid numa_zonelist_order value: "
  2351. "%s\n", s);
  2352. return -EINVAL;
  2353. }
  2354. return 0;
  2355. }
  2356. static __init int setup_numa_zonelist_order(char *s)
  2357. {
  2358. int ret;
  2359. if (!s)
  2360. return 0;
  2361. ret = __parse_numa_zonelist_order(s);
  2362. if (ret == 0)
  2363. strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
  2364. return ret;
  2365. }
  2366. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  2367. /*
  2368. * sysctl handler for numa_zonelist_order
  2369. */
  2370. int numa_zonelist_order_handler(ctl_table *table, int write,
  2371. void __user *buffer, size_t *length,
  2372. loff_t *ppos)
  2373. {
  2374. char saved_string[NUMA_ZONELIST_ORDER_LEN];
  2375. int ret;
  2376. static DEFINE_MUTEX(zl_order_mutex);
  2377. mutex_lock(&zl_order_mutex);
  2378. if (write)
  2379. strcpy(saved_string, (char*)table->data);
  2380. ret = proc_dostring(table, write, buffer, length, ppos);
  2381. if (ret)
  2382. goto out;
  2383. if (write) {
  2384. int oldval = user_zonelist_order;
  2385. if (__parse_numa_zonelist_order((char*)table->data)) {
  2386. /*
  2387. * bogus value. restore saved string
  2388. */
  2389. strncpy((char*)table->data, saved_string,
  2390. NUMA_ZONELIST_ORDER_LEN);
  2391. user_zonelist_order = oldval;
  2392. } else if (oldval != user_zonelist_order) {
  2393. mutex_lock(&zonelists_mutex);
  2394. build_all_zonelists(NULL);
  2395. mutex_unlock(&zonelists_mutex);
  2396. }
  2397. }
  2398. out:
  2399. mutex_unlock(&zl_order_mutex);
  2400. return ret;
  2401. }
  2402. #define MAX_NODE_LOAD (nr_online_nodes)
  2403. static int node_load[MAX_NUMNODES];
  2404. /**
  2405. * find_next_best_node - find the next node that should appear in a given node's fallback list
  2406. * @node: node whose fallback list we're appending
  2407. * @used_node_mask: nodemask_t of already used nodes
  2408. *
  2409. * We use a number of factors to determine which is the next node that should
  2410. * appear on a given node's fallback list. The node should not have appeared
  2411. * already in @node's fallback list, and it should be the next closest node
  2412. * according to the distance array (which contains arbitrary distance values
  2413. * from each node to each node in the system), and should also prefer nodes
  2414. * with no CPUs, since presumably they'll have very little allocation pressure
  2415. * on them otherwise.
  2416. * It returns -1 if no node is found.
  2417. */
  2418. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  2419. {
  2420. int n, val;
  2421. int min_val = INT_MAX;
  2422. int best_node = -1;
  2423. const struct cpumask *tmp = cpumask_of_node(0);
  2424. /* Use the local node if we haven't already */
  2425. if (!node_isset(node, *used_node_mask)) {
  2426. node_set(node, *used_node_mask);
  2427. return node;
  2428. }
  2429. for_each_node_state(n, N_HIGH_MEMORY) {
  2430. /* Don't want a node to appear more than once */
  2431. if (node_isset(n, *used_node_mask))
  2432. continue;
  2433. /* Use the distance array to find the distance */
  2434. val = node_distance(node, n);
  2435. /* Penalize nodes under us ("prefer the next node") */
  2436. val += (n < node);
  2437. /* Give preference to headless and unused nodes */
  2438. tmp = cpumask_of_node(n);
  2439. if (!cpumask_empty(tmp))
  2440. val += PENALTY_FOR_NODE_WITH_CPUS;
  2441. /* Slight preference for less loaded node */
  2442. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  2443. val += node_load[n];
  2444. if (val < min_val) {
  2445. min_val = val;
  2446. best_node = n;
  2447. }
  2448. }
  2449. if (best_node >= 0)
  2450. node_set(best_node, *used_node_mask);
  2451. return best_node;
  2452. }
  2453. /*
  2454. * Build zonelists ordered by node and zones within node.
  2455. * This results in maximum locality--normal zone overflows into local
  2456. * DMA zone, if any--but risks exhausting DMA zone.
  2457. */
  2458. static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
  2459. {
  2460. int j;
  2461. struct zonelist *zonelist;
  2462. zonelist = &pgdat->node_zonelists[0];
  2463. for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
  2464. ;
  2465. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2466. MAX_NR_ZONES - 1);
  2467. zonelist->_zonerefs[j].zone = NULL;
  2468. zonelist->_zonerefs[j].zone_idx = 0;
  2469. }
  2470. /*
  2471. * Build gfp_thisnode zonelists
  2472. */
  2473. static void build_thisnode_zonelists(pg_data_t *pgdat)
  2474. {
  2475. int j;
  2476. struct zonelist *zonelist;
  2477. zonelist = &pgdat->node_zonelists[1];
  2478. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2479. zonelist->_zonerefs[j].zone = NULL;
  2480. zonelist->_zonerefs[j].zone_idx = 0;
  2481. }
  2482. /*
  2483. * Build zonelists ordered by zone and nodes within zones.
  2484. * This results in conserving DMA zone[s] until all Normal memory is
  2485. * exhausted, but results in overflowing to remote node while memory
  2486. * may still exist in local DMA zone.
  2487. */
  2488. static int node_order[MAX_NUMNODES];
  2489. static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
  2490. {
  2491. int pos, j, node;
  2492. int zone_type; /* needs to be signed */
  2493. struct zone *z;
  2494. struct zonelist *zonelist;
  2495. zonelist = &pgdat->node_zonelists[0];
  2496. pos = 0;
  2497. for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
  2498. for (j = 0; j < nr_nodes; j++) {
  2499. node = node_order[j];
  2500. z = &NODE_DATA(node)->node_zones[zone_type];
  2501. if (populated_zone(z)) {
  2502. zoneref_set_zone(z,
  2503. &zonelist->_zonerefs[pos++]);
  2504. check_highest_zone(zone_type);
  2505. }
  2506. }
  2507. }
  2508. zonelist->_zonerefs[pos].zone = NULL;
  2509. zonelist->_zonerefs[pos].zone_idx = 0;
  2510. }
  2511. static int default_zonelist_order(void)
  2512. {
  2513. int nid, zone_type;
  2514. unsigned long low_kmem_size,total_size;
  2515. struct zone *z;
  2516. int average_size;
  2517. /*
  2518. * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
  2519. * If they are really small and used heavily, the system can fall
  2520. * into OOM very easily.
  2521. * This function detect ZONE_DMA/DMA32 size and configures zone order.
  2522. */
  2523. /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
  2524. low_kmem_size = 0;
  2525. total_size = 0;
  2526. for_each_online_node(nid) {
  2527. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2528. z = &NODE_DATA(nid)->node_zones[zone_type];
  2529. if (populated_zone(z)) {
  2530. if (zone_type < ZONE_NORMAL)
  2531. low_kmem_size += z->present_pages;
  2532. total_size += z->present_pages;
  2533. } else if (zone_type == ZONE_NORMAL) {
  2534. /*
  2535. * If any node has only lowmem, then node order
  2536. * is preferred to allow kernel allocations
  2537. * locally; otherwise, they can easily infringe
  2538. * on other nodes when there is an abundance of
  2539. * lowmem available to allocate from.
  2540. */
  2541. return ZONELIST_ORDER_NODE;
  2542. }
  2543. }
  2544. }
  2545. if (!low_kmem_size || /* there are no DMA area. */
  2546. low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
  2547. return ZONELIST_ORDER_NODE;
  2548. /*
  2549. * look into each node's config.
  2550. * If there is a node whose DMA/DMA32 memory is very big area on
  2551. * local memory, NODE_ORDER may be suitable.
  2552. */
  2553. average_size = total_size /
  2554. (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
  2555. for_each_online_node(nid) {
  2556. low_kmem_size = 0;
  2557. total_size = 0;
  2558. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2559. z = &NODE_DATA(nid)->node_zones[zone_type];
  2560. if (populated_zone(z)) {
  2561. if (zone_type < ZONE_NORMAL)
  2562. low_kmem_size += z->present_pages;
  2563. total_size += z->present_pages;
  2564. }
  2565. }
  2566. if (low_kmem_size &&
  2567. total_size > average_size && /* ignore small node */
  2568. low_kmem_size > total_size * 70/100)
  2569. return ZONELIST_ORDER_NODE;
  2570. }
  2571. return ZONELIST_ORDER_ZONE;
  2572. }
  2573. static void set_zonelist_order(void)
  2574. {
  2575. if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
  2576. current_zonelist_order = default_zonelist_order();
  2577. else
  2578. current_zonelist_order = user_zonelist_order;
  2579. }
  2580. static void build_zonelists(pg_data_t *pgdat)
  2581. {
  2582. int j, node, load;
  2583. enum zone_type i;
  2584. nodemask_t used_mask;
  2585. int local_node, prev_node;
  2586. struct zonelist *zonelist;
  2587. int order = current_zonelist_order;
  2588. /* initialize zonelists */
  2589. for (i = 0; i < MAX_ZONELISTS; i++) {
  2590. zonelist = pgdat->node_zonelists + i;
  2591. zonelist->_zonerefs[0].zone = NULL;
  2592. zonelist->_zonerefs[0].zone_idx = 0;
  2593. }
  2594. /* NUMA-aware ordering of nodes */
  2595. local_node = pgdat->node_id;
  2596. load = nr_online_nodes;
  2597. prev_node = local_node;
  2598. nodes_clear(used_mask);
  2599. memset(node_order, 0, sizeof(node_order));
  2600. j = 0;
  2601. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  2602. int distance = node_distance(local_node, node);
  2603. /*
  2604. * If another node is sufficiently far away then it is better
  2605. * to reclaim pages in a zone before going off node.
  2606. */
  2607. if (distance > RECLAIM_DISTANCE)
  2608. zone_reclaim_mode = 1;
  2609. /*
  2610. * We don't want to pressure a particular node.
  2611. * So adding penalty to the first node in same
  2612. * distance group to make it round-robin.
  2613. */
  2614. if (distance != node_distance(local_node, prev_node))
  2615. node_load[node] = load;
  2616. prev_node = node;
  2617. load--;
  2618. if (order == ZONELIST_ORDER_NODE)
  2619. build_zonelists_in_node_order(pgdat, node);
  2620. else
  2621. node_order[j++] = node; /* remember order */
  2622. }
  2623. if (order == ZONELIST_ORDER_ZONE) {
  2624. /* calculate node order -- i.e., DMA last! */
  2625. build_zonelists_in_zone_order(pgdat, j);
  2626. }
  2627. build_thisnode_zonelists(pgdat);
  2628. }
  2629. /* Construct the zonelist performance cache - see further mmzone.h */
  2630. static void build_zonelist_cache(pg_data_t *pgdat)
  2631. {
  2632. struct zonelist *zonelist;
  2633. struct zonelist_cache *zlc;
  2634. struct zoneref *z;
  2635. zonelist = &pgdat->node_zonelists[0];
  2636. zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
  2637. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  2638. for (z = zonelist->_zonerefs; z->zone; z++)
  2639. zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
  2640. }
  2641. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  2642. /*
  2643. * Return node id of node used for "local" allocations.
  2644. * I.e., first node id of first zone in arg node's generic zonelist.
  2645. * Used for initializing percpu 'numa_mem', which is used primarily
  2646. * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
  2647. */
  2648. int local_memory_node(int node)
  2649. {
  2650. struct zone *zone;
  2651. (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
  2652. gfp_zone(GFP_KERNEL),
  2653. NULL,
  2654. &zone);
  2655. return zone->node;
  2656. }
  2657. #endif
  2658. #else /* CONFIG_NUMA */
  2659. static void set_zonelist_order(void)
  2660. {
  2661. current_zonelist_order = ZONELIST_ORDER_ZONE;
  2662. }
  2663. static void build_zonelists(pg_data_t *pgdat)
  2664. {
  2665. int node, local_node;
  2666. enum zone_type j;
  2667. struct zonelist *zonelist;
  2668. local_node = pgdat->node_id;
  2669. zonelist = &pgdat->node_zonelists[0];
  2670. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2671. /*
  2672. * Now we build the zonelist so that it contains the zones
  2673. * of all the other nodes.
  2674. * We don't want to pressure a particular node, so when
  2675. * building the zones for node N, we make sure that the
  2676. * zones coming right after the local ones are those from
  2677. * node N+1 (modulo N)
  2678. */
  2679. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  2680. if (!node_online(node))
  2681. continue;
  2682. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2683. MAX_NR_ZONES - 1);
  2684. }
  2685. for (node = 0; node < local_node; node++) {
  2686. if (!node_online(node))
  2687. continue;
  2688. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2689. MAX_NR_ZONES - 1);
  2690. }
  2691. zonelist->_zonerefs[j].zone = NULL;
  2692. zonelist->_zonerefs[j].zone_idx = 0;
  2693. }
  2694. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  2695. static void build_zonelist_cache(pg_data_t *pgdat)
  2696. {
  2697. pgdat->node_zonelists[0].zlcache_ptr = NULL;
  2698. }
  2699. #endif /* CONFIG_NUMA */
  2700. /*
  2701. * Boot pageset table. One per cpu which is going to be used for all
  2702. * zones and all nodes. The parameters will be set in such a way
  2703. * that an item put on a list will immediately be handed over to
  2704. * the buddy list. This is safe since pageset manipulation is done
  2705. * with interrupts disabled.
  2706. *
  2707. * The boot_pagesets must be kept even after bootup is complete for
  2708. * unused processors and/or zones. They do play a role for bootstrapping
  2709. * hotplugged processors.
  2710. *
  2711. * zoneinfo_show() and maybe other functions do
  2712. * not check if the processor is online before following the pageset pointer.
  2713. * Other parts of the kernel may not check if the zone is available.
  2714. */
  2715. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
  2716. static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
  2717. static void setup_zone_pageset(struct zone *zone);
  2718. /*
  2719. * Global mutex to protect against size modification of zonelists
  2720. * as well as to serialize pageset setup for the new populated zone.
  2721. */
  2722. DEFINE_MUTEX(zonelists_mutex);
  2723. /* return values int ....just for stop_machine() */
  2724. static __init_refok int __build_all_zonelists(void *data)
  2725. {
  2726. int nid;
  2727. int cpu;
  2728. #ifdef CONFIG_NUMA
  2729. memset(node_load, 0, sizeof(node_load));
  2730. #endif
  2731. for_each_online_node(nid) {
  2732. pg_data_t *pgdat = NODE_DATA(nid);
  2733. build_zonelists(pgdat);
  2734. build_zonelist_cache(pgdat);
  2735. }
  2736. /*
  2737. * Initialize the boot_pagesets that are going to be used
  2738. * for bootstrapping processors. The real pagesets for
  2739. * each zone will be allocated later when the per cpu
  2740. * allocator is available.
  2741. *
  2742. * boot_pagesets are used also for bootstrapping offline
  2743. * cpus if the system is already booted because the pagesets
  2744. * are needed to initialize allocators on a specific cpu too.
  2745. * F.e. the percpu allocator needs the page allocator which
  2746. * needs the percpu allocator in order to allocate its pagesets
  2747. * (a chicken-egg dilemma).
  2748. */
  2749. for_each_possible_cpu(cpu) {
  2750. setup_pageset(&per_cpu(boot_pageset, cpu), 0);
  2751. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  2752. /*
  2753. * We now know the "local memory node" for each node--
  2754. * i.e., the node of the first zone in the generic zonelist.
  2755. * Set up numa_mem percpu variable for on-line cpus. During
  2756. * boot, only the boot cpu should be on-line; we'll init the
  2757. * secondary cpus' numa_mem as they come on-line. During
  2758. * node/memory hotplug, we'll fixup all on-line cpus.
  2759. */
  2760. if (cpu_online(cpu))
  2761. set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
  2762. #endif
  2763. }
  2764. return 0;
  2765. }
  2766. /*
  2767. * Called with zonelists_mutex held always
  2768. * unless system_state == SYSTEM_BOOTING.
  2769. */
  2770. void __ref build_all_zonelists(void *data)
  2771. {
  2772. set_zonelist_order();
  2773. if (system_state == SYSTEM_BOOTING) {
  2774. __build_all_zonelists(NULL);
  2775. mminit_verify_zonelist();
  2776. cpuset_init_current_mems_allowed();
  2777. } else {
  2778. /* we have to stop all cpus to guarantee there is no user
  2779. of zonelist */
  2780. #ifdef CONFIG_MEMORY_HOTPLUG
  2781. if (data)
  2782. setup_zone_pageset((struct zone *)data);
  2783. #endif
  2784. stop_machine(__build_all_zonelists, NULL, NULL);
  2785. /* cpuset refresh routine should be here */
  2786. }
  2787. vm_total_pages = nr_free_pagecache_pages();
  2788. /*
  2789. * Disable grouping by mobility if the number of pages in the
  2790. * system is too low to allow the mechanism to work. It would be
  2791. * more accurate, but expensive to check per-zone. This check is
  2792. * made on memory-hotadd so a system can start with mobility
  2793. * disabled and enable it later
  2794. */
  2795. if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
  2796. page_group_by_mobility_disabled = 1;
  2797. else
  2798. page_group_by_mobility_disabled = 0;
  2799. printk("Built %i zonelists in %s order, mobility grouping %s. "
  2800. "Total pages: %ld\n",
  2801. nr_online_nodes,
  2802. zonelist_order_name[current_zonelist_order],
  2803. page_group_by_mobility_disabled ? "off" : "on",
  2804. vm_total_pages);
  2805. #ifdef CONFIG_NUMA
  2806. printk("Policy zone: %s\n", zone_names[policy_zone]);
  2807. #endif
  2808. }
  2809. /*
  2810. * Helper functions to size the waitqueue hash table.
  2811. * Essentially these want to choose hash table sizes sufficiently
  2812. * large so that collisions trying to wait on pages are rare.
  2813. * But in fact, the number of active page waitqueues on typical
  2814. * systems is ridiculously low, less than 200. So this is even
  2815. * conservative, even though it seems large.
  2816. *
  2817. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  2818. * waitqueues, i.e. the size of the waitq table given the number of pages.
  2819. */
  2820. #define PAGES_PER_WAITQUEUE 256
  2821. #ifndef CONFIG_MEMORY_HOTPLUG
  2822. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2823. {
  2824. unsigned long size = 1;
  2825. pages /= PAGES_PER_WAITQUEUE;
  2826. while (size < pages)
  2827. size <<= 1;
  2828. /*
  2829. * Once we have dozens or even hundreds of threads sleeping
  2830. * on IO we've got bigger problems than wait queue collision.
  2831. * Limit the size of the wait table to a reasonable size.
  2832. */
  2833. size = min(size, 4096UL);
  2834. return max(size, 4UL);
  2835. }
  2836. #else
  2837. /*
  2838. * A zone's size might be changed by hot-add, so it is not possible to determine
  2839. * a suitable size for its wait_table. So we use the maximum size now.
  2840. *
  2841. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  2842. *
  2843. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  2844. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  2845. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  2846. *
  2847. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  2848. * or more by the traditional way. (See above). It equals:
  2849. *
  2850. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  2851. * ia64(16K page size) : = ( 8G + 4M)byte.
  2852. * powerpc (64K page size) : = (32G +16M)byte.
  2853. */
  2854. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2855. {
  2856. return 4096UL;
  2857. }
  2858. #endif
  2859. /*
  2860. * This is an integer logarithm so that shifts can be used later
  2861. * to extract the more random high bits from the multiplicative
  2862. * hash function before the remainder is taken.
  2863. */
  2864. static inline unsigned long wait_table_bits(unsigned long size)
  2865. {
  2866. return ffz(~size);
  2867. }
  2868. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  2869. /*
  2870. * Mark a number of pageblocks as MIGRATE_RESERVE. The number
  2871. * of blocks reserved is based on min_wmark_pages(zone). The memory within
  2872. * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
  2873. * higher will lead to a bigger reserve which will get freed as contiguous
  2874. * blocks as reclaim kicks in
  2875. */
  2876. static void setup_zone_migrate_reserve(struct zone *zone)
  2877. {
  2878. unsigned long start_pfn, pfn, end_pfn;
  2879. struct page *page;
  2880. unsigned long block_migratetype;
  2881. int reserve;
  2882. /* Get the start pfn, end pfn and the number of blocks to reserve */
  2883. start_pfn = zone->zone_start_pfn;
  2884. end_pfn = start_pfn + zone->spanned_pages;
  2885. reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
  2886. pageblock_order;
  2887. /*
  2888. * Reserve blocks are generally in place to help high-order atomic
  2889. * allocations that are short-lived. A min_free_kbytes value that
  2890. * would result in more than 2 reserve blocks for atomic allocations
  2891. * is assumed to be in place to help anti-fragmentation for the
  2892. * future allocation of hugepages at runtime.
  2893. */
  2894. reserve = min(2, reserve);
  2895. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  2896. if (!pfn_valid(pfn))
  2897. continue;
  2898. page = pfn_to_page(pfn);
  2899. /* Watch out for overlapping nodes */
  2900. if (page_to_nid(page) != zone_to_nid(zone))
  2901. continue;
  2902. /* Blocks with reserved pages will never free, skip them. */
  2903. if (PageReserved(page))
  2904. continue;
  2905. block_migratetype = get_pageblock_migratetype(page);
  2906. /* If this block is reserved, account for it */
  2907. if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) {
  2908. reserve--;
  2909. continue;
  2910. }
  2911. /* Suitable for reserving if this block is movable */
  2912. if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) {
  2913. set_pageblock_migratetype(page, MIGRATE_RESERVE);
  2914. move_freepages_block(zone, page, MIGRATE_RESERVE);
  2915. reserve--;
  2916. continue;
  2917. }
  2918. /*
  2919. * If the reserve is met and this is a previous reserved block,
  2920. * take it back
  2921. */
  2922. if (block_migratetype == MIGRATE_RESERVE) {
  2923. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  2924. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  2925. }
  2926. }
  2927. }
  2928. /*
  2929. * Initially all pages are reserved - free ones are freed
  2930. * up by free_all_bootmem() once the early boot process is
  2931. * done. Non-atomic initialization, single-pass.
  2932. */
  2933. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  2934. unsigned long start_pfn, enum memmap_context context)
  2935. {
  2936. struct page *page;
  2937. unsigned long end_pfn = start_pfn + size;
  2938. unsigned long pfn;
  2939. struct zone *z;
  2940. if (highest_memmap_pfn < end_pfn - 1)
  2941. highest_memmap_pfn = end_pfn - 1;
  2942. z = &NODE_DATA(nid)->node_zones[zone];
  2943. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  2944. /*
  2945. * There can be holes in boot-time mem_map[]s
  2946. * handed to this function. They do not
  2947. * exist on hotplugged memory.
  2948. */
  2949. if (context == MEMMAP_EARLY) {
  2950. if (!early_pfn_valid(pfn))
  2951. continue;
  2952. if (!early_pfn_in_nid(pfn, nid))
  2953. continue;
  2954. }
  2955. page = pfn_to_page(pfn);
  2956. set_page_links(page, zone, nid, pfn);
  2957. mminit_verify_page_links(page, zone, nid, pfn);
  2958. init_page_count(page);
  2959. reset_page_mapcount(page);
  2960. SetPageReserved(page);
  2961. /*
  2962. * Mark the block movable so that blocks are reserved for
  2963. * movable at startup. This will force kernel allocations
  2964. * to reserve their blocks rather than leaking throughout
  2965. * the address space during boot when many long-lived
  2966. * kernel allocations are made. Later some blocks near
  2967. * the start are marked MIGRATE_RESERVE by
  2968. * setup_zone_migrate_reserve()
  2969. *
  2970. * bitmap is created for zone's valid pfn range. but memmap
  2971. * can be created for invalid pages (for alignment)
  2972. * check here not to call set_pageblock_migratetype() against
  2973. * pfn out of zone.
  2974. */
  2975. if ((z->zone_start_pfn <= pfn)
  2976. && (pfn < z->zone_start_pfn + z->spanned_pages)
  2977. && !(pfn & (pageblock_nr_pages - 1)))
  2978. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  2979. INIT_LIST_HEAD(&page->lru);
  2980. #ifdef WANT_PAGE_VIRTUAL
  2981. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  2982. if (!is_highmem_idx(zone))
  2983. set_page_address(page, __va(pfn << PAGE_SHIFT));
  2984. #endif
  2985. }
  2986. }
  2987. static void __meminit zone_init_free_lists(struct zone *zone)
  2988. {
  2989. int order, t;
  2990. for_each_migratetype_order(order, t) {
  2991. INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
  2992. zone->free_area[order].nr_free = 0;
  2993. }
  2994. }
  2995. #ifndef __HAVE_ARCH_MEMMAP_INIT
  2996. #define memmap_init(size, nid, zone, start_pfn) \
  2997. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  2998. #endif
  2999. static int zone_batchsize(struct zone *zone)
  3000. {
  3001. #ifdef CONFIG_MMU
  3002. int batch;
  3003. /*
  3004. * The per-cpu-pages pools are set to around 1000th of the
  3005. * size of the zone. But no more than 1/2 of a meg.
  3006. *
  3007. * OK, so we don't know how big the cache is. So guess.
  3008. */
  3009. batch = zone->present_pages / 1024;
  3010. if (batch * PAGE_SIZE > 512 * 1024)
  3011. batch = (512 * 1024) / PAGE_SIZE;
  3012. batch /= 4; /* We effectively *= 4 below */
  3013. if (batch < 1)
  3014. batch = 1;
  3015. /*
  3016. * Clamp the batch to a 2^n - 1 value. Having a power
  3017. * of 2 value was found to be more likely to have
  3018. * suboptimal cache aliasing properties in some cases.
  3019. *
  3020. * For example if 2 tasks are alternately allocating
  3021. * batches of pages, one task can end up with a lot
  3022. * of pages of one half of the possible page colors
  3023. * and the other with pages of the other colors.
  3024. */
  3025. batch = rounddown_pow_of_two(batch + batch/2) - 1;
  3026. return batch;
  3027. #else
  3028. /* The deferral and batching of frees should be suppressed under NOMMU
  3029. * conditions.
  3030. *
  3031. * The problem is that NOMMU needs to be able to allocate large chunks
  3032. * of contiguous memory as there's no hardware page translation to
  3033. * assemble apparent contiguous memory from discontiguous pages.
  3034. *
  3035. * Queueing large contiguous runs of pages for batching, however,
  3036. * causes the pages to actually be freed in smaller chunks. As there
  3037. * can be a significant delay between the individual batches being
  3038. * recycled, this leads to the once large chunks of space being
  3039. * fragmented and becoming unavailable for high-order allocations.
  3040. */
  3041. return 0;
  3042. #endif
  3043. }
  3044. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  3045. {
  3046. struct per_cpu_pages *pcp;
  3047. int migratetype;
  3048. memset(p, 0, sizeof(*p));
  3049. pcp = &p->pcp;
  3050. pcp->count = 0;
  3051. pcp->high = 6 * batch;
  3052. pcp->batch = max(1UL, 1 * batch);
  3053. for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
  3054. INIT_LIST_HEAD(&pcp->lists[migratetype]);
  3055. }
  3056. /*
  3057. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  3058. * to the value high for the pageset p.
  3059. */
  3060. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  3061. unsigned long high)
  3062. {
  3063. struct per_cpu_pages *pcp;
  3064. pcp = &p->pcp;
  3065. pcp->high = high;
  3066. pcp->batch = max(1UL, high/4);
  3067. if ((high/4) > (PAGE_SHIFT * 8))
  3068. pcp->batch = PAGE_SHIFT * 8;
  3069. }
  3070. static void setup_zone_pageset(struct zone *zone)
  3071. {
  3072. int cpu;
  3073. zone->pageset = alloc_percpu(struct per_cpu_pageset);
  3074. for_each_possible_cpu(cpu) {
  3075. struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
  3076. setup_pageset(pcp, zone_batchsize(zone));
  3077. if (percpu_pagelist_fraction)
  3078. setup_pagelist_highmark(pcp,
  3079. (zone->present_pages /
  3080. percpu_pagelist_fraction));
  3081. }
  3082. }
  3083. /*
  3084. * Allocate per cpu pagesets and initialize them.
  3085. * Before this call only boot pagesets were available.
  3086. */
  3087. void __init setup_per_cpu_pageset(void)
  3088. {
  3089. struct zone *zone;
  3090. for_each_populated_zone(zone)
  3091. setup_zone_pageset(zone);
  3092. }
  3093. static noinline __init_refok
  3094. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  3095. {
  3096. int i;
  3097. struct pglist_data *pgdat = zone->zone_pgdat;
  3098. size_t alloc_size;
  3099. /*
  3100. * The per-page waitqueue mechanism uses hashed waitqueues
  3101. * per zone.
  3102. */
  3103. zone->wait_table_hash_nr_entries =
  3104. wait_table_hash_nr_entries(zone_size_pages);
  3105. zone->wait_table_bits =
  3106. wait_table_bits(zone->wait_table_hash_nr_entries);
  3107. alloc_size = zone->wait_table_hash_nr_entries
  3108. * sizeof(wait_queue_head_t);
  3109. if (!slab_is_available()) {
  3110. zone->wait_table = (wait_queue_head_t *)
  3111. alloc_bootmem_node_nopanic(pgdat, alloc_size);
  3112. } else {
  3113. /*
  3114. * This case means that a zone whose size was 0 gets new memory
  3115. * via memory hot-add.
  3116. * But it may be the case that a new node was hot-added. In
  3117. * this case vmalloc() will not be able to use this new node's
  3118. * memory - this wait_table must be initialized to use this new
  3119. * node itself as well.
  3120. * To use this new node's memory, further consideration will be
  3121. * necessary.
  3122. */
  3123. zone->wait_table = vmalloc(alloc_size);
  3124. }
  3125. if (!zone->wait_table)
  3126. return -ENOMEM;
  3127. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  3128. init_waitqueue_head(zone->wait_table + i);
  3129. return 0;
  3130. }
  3131. static int __zone_pcp_update(void *data)
  3132. {
  3133. struct zone *zone = data;
  3134. int cpu;
  3135. unsigned long batch = zone_batchsize(zone), flags;
  3136. for_each_possible_cpu(cpu) {
  3137. struct per_cpu_pageset *pset;
  3138. struct per_cpu_pages *pcp;
  3139. pset = per_cpu_ptr(zone->pageset, cpu);
  3140. pcp = &pset->pcp;
  3141. local_irq_save(flags);
  3142. free_pcppages_bulk(zone, pcp->count, pcp);
  3143. setup_pageset(pset, batch);
  3144. local_irq_restore(flags);
  3145. }
  3146. return 0;
  3147. }
  3148. void zone_pcp_update(struct zone *zone)
  3149. {
  3150. stop_machine(__zone_pcp_update, zone, NULL);
  3151. }
  3152. static __meminit void zone_pcp_init(struct zone *zone)
  3153. {
  3154. /*
  3155. * per cpu subsystem is not up at this point. The following code
  3156. * relies on the ability of the linker to provide the
  3157. * offset of a (static) per cpu variable into the per cpu area.
  3158. */
  3159. zone->pageset = &boot_pageset;
  3160. if (zone->present_pages)
  3161. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
  3162. zone->name, zone->present_pages,
  3163. zone_batchsize(zone));
  3164. }
  3165. __meminit int init_currently_empty_zone(struct zone *zone,
  3166. unsigned long zone_start_pfn,
  3167. unsigned long size,
  3168. enum memmap_context context)
  3169. {
  3170. struct pglist_data *pgdat = zone->zone_pgdat;
  3171. int ret;
  3172. ret = zone_wait_table_init(zone, size);
  3173. if (ret)
  3174. return ret;
  3175. pgdat->nr_zones = zone_idx(zone) + 1;
  3176. zone->zone_start_pfn = zone_start_pfn;
  3177. mminit_dprintk(MMINIT_TRACE, "memmap_init",
  3178. "Initialising map node %d zone %lu pfns %lu -> %lu\n",
  3179. pgdat->node_id,
  3180. (unsigned long)zone_idx(zone),
  3181. zone_start_pfn, (zone_start_pfn + size));
  3182. zone_init_free_lists(zone);
  3183. return 0;
  3184. }
  3185. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3186. /*
  3187. * Basic iterator support. Return the first range of PFNs for a node
  3188. * Note: nid == MAX_NUMNODES returns first region regardless of node
  3189. */
  3190. static int __meminit first_active_region_index_in_nid(int nid)
  3191. {
  3192. int i;
  3193. for (i = 0; i < nr_nodemap_entries; i++)
  3194. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  3195. return i;
  3196. return -1;
  3197. }
  3198. /*
  3199. * Basic iterator support. Return the next active range of PFNs for a node
  3200. * Note: nid == MAX_NUMNODES returns next region regardless of node
  3201. */
  3202. static int __meminit next_active_region_index_in_nid(int index, int nid)
  3203. {
  3204. for (index = index + 1; index < nr_nodemap_entries; index++)
  3205. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  3206. return index;
  3207. return -1;
  3208. }
  3209. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  3210. /*
  3211. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  3212. * Architectures may implement their own version but if add_active_range()
  3213. * was used and there are no special requirements, this is a convenient
  3214. * alternative
  3215. */
  3216. int __meminit __early_pfn_to_nid(unsigned long pfn)
  3217. {
  3218. int i;
  3219. for (i = 0; i < nr_nodemap_entries; i++) {
  3220. unsigned long start_pfn = early_node_map[i].start_pfn;
  3221. unsigned long end_pfn = early_node_map[i].end_pfn;
  3222. if (start_pfn <= pfn && pfn < end_pfn)
  3223. return early_node_map[i].nid;
  3224. }
  3225. /* This is a memory hole */
  3226. return -1;
  3227. }
  3228. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  3229. int __meminit early_pfn_to_nid(unsigned long pfn)
  3230. {
  3231. int nid;
  3232. nid = __early_pfn_to_nid(pfn);
  3233. if (nid >= 0)
  3234. return nid;
  3235. /* just returns 0 */
  3236. return 0;
  3237. }
  3238. #ifdef CONFIG_NODES_SPAN_OTHER_NODES
  3239. bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
  3240. {
  3241. int nid;
  3242. nid = __early_pfn_to_nid(pfn);
  3243. if (nid >= 0 && nid != node)
  3244. return false;
  3245. return true;
  3246. }
  3247. #endif
  3248. /* Basic iterator support to walk early_node_map[] */
  3249. #define for_each_active_range_index_in_nid(i, nid) \
  3250. for (i = first_active_region_index_in_nid(nid); i != -1; \
  3251. i = next_active_region_index_in_nid(i, nid))
  3252. /**
  3253. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  3254. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  3255. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  3256. *
  3257. * If an architecture guarantees that all ranges registered with
  3258. * add_active_ranges() contain no holes and may be freed, this
  3259. * this function may be used instead of calling free_bootmem() manually.
  3260. */
  3261. void __init free_bootmem_with_active_regions(int nid,
  3262. unsigned long max_low_pfn)
  3263. {
  3264. int i;
  3265. for_each_active_range_index_in_nid(i, nid) {
  3266. unsigned long size_pages = 0;
  3267. unsigned long end_pfn = early_node_map[i].end_pfn;
  3268. if (early_node_map[i].start_pfn >= max_low_pfn)
  3269. continue;
  3270. if (end_pfn > max_low_pfn)
  3271. end_pfn = max_low_pfn;
  3272. size_pages = end_pfn - early_node_map[i].start_pfn;
  3273. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  3274. PFN_PHYS(early_node_map[i].start_pfn),
  3275. size_pages << PAGE_SHIFT);
  3276. }
  3277. }
  3278. #ifdef CONFIG_HAVE_MEMBLOCK
  3279. /*
  3280. * Basic iterator support. Return the last range of PFNs for a node
  3281. * Note: nid == MAX_NUMNODES returns last region regardless of node
  3282. */
  3283. static int __meminit last_active_region_index_in_nid(int nid)
  3284. {
  3285. int i;
  3286. for (i = nr_nodemap_entries - 1; i >= 0; i--)
  3287. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  3288. return i;
  3289. return -1;
  3290. }
  3291. /*
  3292. * Basic iterator support. Return the previous active range of PFNs for a node
  3293. * Note: nid == MAX_NUMNODES returns next region regardless of node
  3294. */
  3295. static int __meminit previous_active_region_index_in_nid(int index, int nid)
  3296. {
  3297. for (index = index - 1; index >= 0; index--)
  3298. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  3299. return index;
  3300. return -1;
  3301. }
  3302. #define for_each_active_range_index_in_nid_reverse(i, nid) \
  3303. for (i = last_active_region_index_in_nid(nid); i != -1; \
  3304. i = previous_active_region_index_in_nid(i, nid))
  3305. u64 __init find_memory_core_early(int nid, u64 size, u64 align,
  3306. u64 goal, u64 limit)
  3307. {
  3308. int i;
  3309. /* Need to go over early_node_map to find out good range for node */
  3310. for_each_active_range_index_in_nid_reverse(i, nid) {
  3311. u64 addr;
  3312. u64 ei_start, ei_last;
  3313. u64 final_start, final_end;
  3314. ei_last = early_node_map[i].end_pfn;
  3315. ei_last <<= PAGE_SHIFT;
  3316. ei_start = early_node_map[i].start_pfn;
  3317. ei_start <<= PAGE_SHIFT;
  3318. final_start = max(ei_start, goal);
  3319. final_end = min(ei_last, limit);
  3320. if (final_start >= final_end)
  3321. continue;
  3322. addr = memblock_find_in_range(final_start, final_end, size, align);
  3323. if (addr == MEMBLOCK_ERROR)
  3324. continue;
  3325. return addr;
  3326. }
  3327. return MEMBLOCK_ERROR;
  3328. }
  3329. #endif
  3330. int __init add_from_early_node_map(struct range *range, int az,
  3331. int nr_range, int nid)
  3332. {
  3333. int i;
  3334. u64 start, end;
  3335. /* need to go over early_node_map to find out good range for node */
  3336. for_each_active_range_index_in_nid(i, nid) {
  3337. start = early_node_map[i].start_pfn;
  3338. end = early_node_map[i].end_pfn;
  3339. nr_range = add_range(range, az, nr_range, start, end);
  3340. }
  3341. return nr_range;
  3342. }
  3343. void __init work_with_active_regions(int nid, work_fn_t work_fn, void *data)
  3344. {
  3345. int i;
  3346. int ret;
  3347. for_each_active_range_index_in_nid(i, nid) {
  3348. ret = work_fn(early_node_map[i].start_pfn,
  3349. early_node_map[i].end_pfn, data);
  3350. if (ret)
  3351. break;
  3352. }
  3353. }
  3354. /**
  3355. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  3356. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  3357. *
  3358. * If an architecture guarantees that all ranges registered with
  3359. * add_active_ranges() contain no holes and may be freed, this
  3360. * function may be used instead of calling memory_present() manually.
  3361. */
  3362. void __init sparse_memory_present_with_active_regions(int nid)
  3363. {
  3364. int i;
  3365. for_each_active_range_index_in_nid(i, nid)
  3366. memory_present(early_node_map[i].nid,
  3367. early_node_map[i].start_pfn,
  3368. early_node_map[i].end_pfn);
  3369. }
  3370. /**
  3371. * get_pfn_range_for_nid - Return the start and end page frames for a node
  3372. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  3373. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  3374. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  3375. *
  3376. * It returns the start and end page frame of a node based on information
  3377. * provided by an arch calling add_active_range(). If called for a node
  3378. * with no available memory, a warning is printed and the start and end
  3379. * PFNs will be 0.
  3380. */
  3381. void __meminit get_pfn_range_for_nid(unsigned int nid,
  3382. unsigned long *start_pfn, unsigned long *end_pfn)
  3383. {
  3384. int i;
  3385. *start_pfn = -1UL;
  3386. *end_pfn = 0;
  3387. for_each_active_range_index_in_nid(i, nid) {
  3388. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  3389. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  3390. }
  3391. if (*start_pfn == -1UL)
  3392. *start_pfn = 0;
  3393. }
  3394. /*
  3395. * This finds a zone that can be used for ZONE_MOVABLE pages. The
  3396. * assumption is made that zones within a node are ordered in monotonic
  3397. * increasing memory addresses so that the "highest" populated zone is used
  3398. */
  3399. static void __init find_usable_zone_for_movable(void)
  3400. {
  3401. int zone_index;
  3402. for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
  3403. if (zone_index == ZONE_MOVABLE)
  3404. continue;
  3405. if (arch_zone_highest_possible_pfn[zone_index] >
  3406. arch_zone_lowest_possible_pfn[zone_index])
  3407. break;
  3408. }
  3409. VM_BUG_ON(zone_index == -1);
  3410. movable_zone = zone_index;
  3411. }
  3412. /*
  3413. * The zone ranges provided by the architecture do not include ZONE_MOVABLE
  3414. * because it is sized independent of architecture. Unlike the other zones,
  3415. * the starting point for ZONE_MOVABLE is not fixed. It may be different
  3416. * in each node depending on the size of each node and how evenly kernelcore
  3417. * is distributed. This helper function adjusts the zone ranges
  3418. * provided by the architecture for a given node by using the end of the
  3419. * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
  3420. * zones within a node are in order of monotonic increases memory addresses
  3421. */
  3422. static void __meminit adjust_zone_range_for_zone_movable(int nid,
  3423. unsigned long zone_type,
  3424. unsigned long node_start_pfn,
  3425. unsigned long node_end_pfn,
  3426. unsigned long *zone_start_pfn,
  3427. unsigned long *zone_end_pfn)
  3428. {
  3429. /* Only adjust if ZONE_MOVABLE is on this node */
  3430. if (zone_movable_pfn[nid]) {
  3431. /* Size ZONE_MOVABLE */
  3432. if (zone_type == ZONE_MOVABLE) {
  3433. *zone_start_pfn = zone_movable_pfn[nid];
  3434. *zone_end_pfn = min(node_end_pfn,
  3435. arch_zone_highest_possible_pfn[movable_zone]);
  3436. /* Adjust for ZONE_MOVABLE starting within this range */
  3437. } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
  3438. *zone_end_pfn > zone_movable_pfn[nid]) {
  3439. *zone_end_pfn = zone_movable_pfn[nid];
  3440. /* Check if this whole range is within ZONE_MOVABLE */
  3441. } else if (*zone_start_pfn >= zone_movable_pfn[nid])
  3442. *zone_start_pfn = *zone_end_pfn;
  3443. }
  3444. }
  3445. /*
  3446. * Return the number of pages a zone spans in a node, including holes
  3447. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  3448. */
  3449. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3450. unsigned long zone_type,
  3451. unsigned long *ignored)
  3452. {
  3453. unsigned long node_start_pfn, node_end_pfn;
  3454. unsigned long zone_start_pfn, zone_end_pfn;
  3455. /* Get the start and end of the node and zone */
  3456. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3457. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  3458. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  3459. adjust_zone_range_for_zone_movable(nid, zone_type,
  3460. node_start_pfn, node_end_pfn,
  3461. &zone_start_pfn, &zone_end_pfn);
  3462. /* Check that this node has pages within the zone's required range */
  3463. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  3464. return 0;
  3465. /* Move the zone boundaries inside the node if necessary */
  3466. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  3467. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  3468. /* Return the spanned pages */
  3469. return zone_end_pfn - zone_start_pfn;
  3470. }
  3471. /*
  3472. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  3473. * then all holes in the requested range will be accounted for.
  3474. */
  3475. unsigned long __meminit __absent_pages_in_range(int nid,
  3476. unsigned long range_start_pfn,
  3477. unsigned long range_end_pfn)
  3478. {
  3479. int i = 0;
  3480. unsigned long prev_end_pfn = 0, hole_pages = 0;
  3481. unsigned long start_pfn;
  3482. /* Find the end_pfn of the first active range of pfns in the node */
  3483. i = first_active_region_index_in_nid(nid);
  3484. if (i == -1)
  3485. return 0;
  3486. prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3487. /* Account for ranges before physical memory on this node */
  3488. if (early_node_map[i].start_pfn > range_start_pfn)
  3489. hole_pages = prev_end_pfn - range_start_pfn;
  3490. /* Find all holes for the zone within the node */
  3491. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  3492. /* No need to continue if prev_end_pfn is outside the zone */
  3493. if (prev_end_pfn >= range_end_pfn)
  3494. break;
  3495. /* Make sure the end of the zone is not within the hole */
  3496. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3497. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  3498. /* Update the hole size cound and move on */
  3499. if (start_pfn > range_start_pfn) {
  3500. BUG_ON(prev_end_pfn > start_pfn);
  3501. hole_pages += start_pfn - prev_end_pfn;
  3502. }
  3503. prev_end_pfn = early_node_map[i].end_pfn;
  3504. }
  3505. /* Account for ranges past physical memory on this node */
  3506. if (range_end_pfn > prev_end_pfn)
  3507. hole_pages += range_end_pfn -
  3508. max(range_start_pfn, prev_end_pfn);
  3509. return hole_pages;
  3510. }
  3511. /**
  3512. * absent_pages_in_range - Return number of page frames in holes within a range
  3513. * @start_pfn: The start PFN to start searching for holes
  3514. * @end_pfn: The end PFN to stop searching for holes
  3515. *
  3516. * It returns the number of pages frames in memory holes within a range.
  3517. */
  3518. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  3519. unsigned long end_pfn)
  3520. {
  3521. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  3522. }
  3523. /* Return the number of page frames in holes in a zone on a node */
  3524. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  3525. unsigned long zone_type,
  3526. unsigned long *ignored)
  3527. {
  3528. unsigned long node_start_pfn, node_end_pfn;
  3529. unsigned long zone_start_pfn, zone_end_pfn;
  3530. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3531. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  3532. node_start_pfn);
  3533. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  3534. node_end_pfn);
  3535. adjust_zone_range_for_zone_movable(nid, zone_type,
  3536. node_start_pfn, node_end_pfn,
  3537. &zone_start_pfn, &zone_end_pfn);
  3538. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  3539. }
  3540. #else
  3541. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3542. unsigned long zone_type,
  3543. unsigned long *zones_size)
  3544. {
  3545. return zones_size[zone_type];
  3546. }
  3547. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  3548. unsigned long zone_type,
  3549. unsigned long *zholes_size)
  3550. {
  3551. if (!zholes_size)
  3552. return 0;
  3553. return zholes_size[zone_type];
  3554. }
  3555. #endif
  3556. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  3557. unsigned long *zones_size, unsigned long *zholes_size)
  3558. {
  3559. unsigned long realtotalpages, totalpages = 0;
  3560. enum zone_type i;
  3561. for (i = 0; i < MAX_NR_ZONES; i++)
  3562. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  3563. zones_size);
  3564. pgdat->node_spanned_pages = totalpages;
  3565. realtotalpages = totalpages;
  3566. for (i = 0; i < MAX_NR_ZONES; i++)
  3567. realtotalpages -=
  3568. zone_absent_pages_in_node(pgdat->node_id, i,
  3569. zholes_size);
  3570. pgdat->node_present_pages = realtotalpages;
  3571. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  3572. realtotalpages);
  3573. }
  3574. #ifndef CONFIG_SPARSEMEM
  3575. /*
  3576. * Calculate the size of the zone->blockflags rounded to an unsigned long
  3577. * Start by making sure zonesize is a multiple of pageblock_order by rounding
  3578. * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
  3579. * round what is now in bits to nearest long in bits, then return it in
  3580. * bytes.
  3581. */
  3582. static unsigned long __init usemap_size(unsigned long zonesize)
  3583. {
  3584. unsigned long usemapsize;
  3585. usemapsize = roundup(zonesize, pageblock_nr_pages);
  3586. usemapsize = usemapsize >> pageblock_order;
  3587. usemapsize *= NR_PAGEBLOCK_BITS;
  3588. usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
  3589. return usemapsize / 8;
  3590. }
  3591. static void __init setup_usemap(struct pglist_data *pgdat,
  3592. struct zone *zone, unsigned long zonesize)
  3593. {
  3594. unsigned long usemapsize = usemap_size(zonesize);
  3595. zone->pageblock_flags = NULL;
  3596. if (usemapsize)
  3597. zone->pageblock_flags = alloc_bootmem_node_nopanic(pgdat,
  3598. usemapsize);
  3599. }
  3600. #else
  3601. static inline void setup_usemap(struct pglist_data *pgdat,
  3602. struct zone *zone, unsigned long zonesize) {}
  3603. #endif /* CONFIG_SPARSEMEM */
  3604. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  3605. /* Return a sensible default order for the pageblock size. */
  3606. static inline int pageblock_default_order(void)
  3607. {
  3608. if (HPAGE_SHIFT > PAGE_SHIFT)
  3609. return HUGETLB_PAGE_ORDER;
  3610. return MAX_ORDER-1;
  3611. }
  3612. /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
  3613. static inline void __init set_pageblock_order(unsigned int order)
  3614. {
  3615. /* Check that pageblock_nr_pages has not already been setup */
  3616. if (pageblock_order)
  3617. return;
  3618. /*
  3619. * Assume the largest contiguous order of interest is a huge page.
  3620. * This value may be variable depending on boot parameters on IA64
  3621. */
  3622. pageblock_order = order;
  3623. }
  3624. #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3625. /*
  3626. * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
  3627. * and pageblock_default_order() are unused as pageblock_order is set
  3628. * at compile-time. See include/linux/pageblock-flags.h for the values of
  3629. * pageblock_order based on the kernel config
  3630. */
  3631. static inline int pageblock_default_order(unsigned int order)
  3632. {
  3633. return MAX_ORDER-1;
  3634. }
  3635. #define set_pageblock_order(x) do {} while (0)
  3636. #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3637. /*
  3638. * Set up the zone data structures:
  3639. * - mark all pages reserved
  3640. * - mark all memory queues empty
  3641. * - clear the memory bitmaps
  3642. */
  3643. static void __paginginit free_area_init_core(struct pglist_data *pgdat,
  3644. unsigned long *zones_size, unsigned long *zholes_size)
  3645. {
  3646. enum zone_type j;
  3647. int nid = pgdat->node_id;
  3648. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  3649. int ret;
  3650. pgdat_resize_init(pgdat);
  3651. pgdat->nr_zones = 0;
  3652. init_waitqueue_head(&pgdat->kswapd_wait);
  3653. pgdat->kswapd_max_order = 0;
  3654. pgdat_page_cgroup_init(pgdat);
  3655. for (j = 0; j < MAX_NR_ZONES; j++) {
  3656. struct zone *zone = pgdat->node_zones + j;
  3657. unsigned long size, realsize, memmap_pages;
  3658. enum lru_list l;
  3659. size = zone_spanned_pages_in_node(nid, j, zones_size);
  3660. realsize = size - zone_absent_pages_in_node(nid, j,
  3661. zholes_size);
  3662. /*
  3663. * Adjust realsize so that it accounts for how much memory
  3664. * is used by this zone for memmap. This affects the watermark
  3665. * and per-cpu initialisations
  3666. */
  3667. memmap_pages =
  3668. PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT;
  3669. if (realsize >= memmap_pages) {
  3670. realsize -= memmap_pages;
  3671. if (memmap_pages)
  3672. printk(KERN_DEBUG
  3673. " %s zone: %lu pages used for memmap\n",
  3674. zone_names[j], memmap_pages);
  3675. } else
  3676. printk(KERN_WARNING
  3677. " %s zone: %lu pages exceeds realsize %lu\n",
  3678. zone_names[j], memmap_pages, realsize);
  3679. /* Account for reserved pages */
  3680. if (j == 0 && realsize > dma_reserve) {
  3681. realsize -= dma_reserve;
  3682. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  3683. zone_names[0], dma_reserve);
  3684. }
  3685. if (!is_highmem_idx(j))
  3686. nr_kernel_pages += realsize;
  3687. nr_all_pages += realsize;
  3688. zone->spanned_pages = size;
  3689. zone->present_pages = realsize;
  3690. #ifdef CONFIG_NUMA
  3691. zone->node = nid;
  3692. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  3693. / 100;
  3694. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  3695. #endif
  3696. zone->name = zone_names[j];
  3697. spin_lock_init(&zone->lock);
  3698. spin_lock_init(&zone->lru_lock);
  3699. zone_seqlock_init(zone);
  3700. zone->zone_pgdat = pgdat;
  3701. zone_pcp_init(zone);
  3702. for_each_lru(l) {
  3703. INIT_LIST_HEAD(&zone->lru[l].list);
  3704. zone->reclaim_stat.nr_saved_scan[l] = 0;
  3705. }
  3706. zone->reclaim_stat.recent_rotated[0] = 0;
  3707. zone->reclaim_stat.recent_rotated[1] = 0;
  3708. zone->reclaim_stat.recent_scanned[0] = 0;
  3709. zone->reclaim_stat.recent_scanned[1] = 0;
  3710. zap_zone_vm_stats(zone);
  3711. zone->flags = 0;
  3712. if (!size)
  3713. continue;
  3714. set_pageblock_order(pageblock_default_order());
  3715. setup_usemap(pgdat, zone, size);
  3716. ret = init_currently_empty_zone(zone, zone_start_pfn,
  3717. size, MEMMAP_EARLY);
  3718. BUG_ON(ret);
  3719. memmap_init(size, nid, j, zone_start_pfn);
  3720. zone_start_pfn += size;
  3721. }
  3722. }
  3723. static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
  3724. {
  3725. /* Skip empty nodes */
  3726. if (!pgdat->node_spanned_pages)
  3727. return;
  3728. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3729. /* ia64 gets its own node_mem_map, before this, without bootmem */
  3730. if (!pgdat->node_mem_map) {
  3731. unsigned long size, start, end;
  3732. struct page *map;
  3733. /*
  3734. * The zone's endpoints aren't required to be MAX_ORDER
  3735. * aligned but the node_mem_map endpoints must be in order
  3736. * for the buddy allocator to function correctly.
  3737. */
  3738. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  3739. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  3740. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  3741. size = (end - start) * sizeof(struct page);
  3742. map = alloc_remap(pgdat->node_id, size);
  3743. if (!map)
  3744. map = alloc_bootmem_node_nopanic(pgdat, size);
  3745. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  3746. }
  3747. #ifndef CONFIG_NEED_MULTIPLE_NODES
  3748. /*
  3749. * With no DISCONTIG, the global mem_map is just set as node 0's
  3750. */
  3751. if (pgdat == NODE_DATA(0)) {
  3752. mem_map = NODE_DATA(0)->node_mem_map;
  3753. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3754. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  3755. mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
  3756. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  3757. }
  3758. #endif
  3759. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  3760. }
  3761. void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
  3762. unsigned long node_start_pfn, unsigned long *zholes_size)
  3763. {
  3764. pg_data_t *pgdat = NODE_DATA(nid);
  3765. pgdat->node_id = nid;
  3766. pgdat->node_start_pfn = node_start_pfn;
  3767. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  3768. alloc_node_mem_map(pgdat);
  3769. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3770. printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
  3771. nid, (unsigned long)pgdat,
  3772. (unsigned long)pgdat->node_mem_map);
  3773. #endif
  3774. free_area_init_core(pgdat, zones_size, zholes_size);
  3775. }
  3776. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3777. #if MAX_NUMNODES > 1
  3778. /*
  3779. * Figure out the number of possible node ids.
  3780. */
  3781. static void __init setup_nr_node_ids(void)
  3782. {
  3783. unsigned int node;
  3784. unsigned int highest = 0;
  3785. for_each_node_mask(node, node_possible_map)
  3786. highest = node;
  3787. nr_node_ids = highest + 1;
  3788. }
  3789. #else
  3790. static inline void setup_nr_node_ids(void)
  3791. {
  3792. }
  3793. #endif
  3794. /**
  3795. * add_active_range - Register a range of PFNs backed by physical memory
  3796. * @nid: The node ID the range resides on
  3797. * @start_pfn: The start PFN of the available physical memory
  3798. * @end_pfn: The end PFN of the available physical memory
  3799. *
  3800. * These ranges are stored in an early_node_map[] and later used by
  3801. * free_area_init_nodes() to calculate zone sizes and holes. If the
  3802. * range spans a memory hole, it is up to the architecture to ensure
  3803. * the memory is not freed by the bootmem allocator. If possible
  3804. * the range being registered will be merged with existing ranges.
  3805. */
  3806. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  3807. unsigned long end_pfn)
  3808. {
  3809. int i;
  3810. mminit_dprintk(MMINIT_TRACE, "memory_register",
  3811. "Entering add_active_range(%d, %#lx, %#lx) "
  3812. "%d entries of %d used\n",
  3813. nid, start_pfn, end_pfn,
  3814. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  3815. mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
  3816. /* Merge with existing active regions if possible */
  3817. for (i = 0; i < nr_nodemap_entries; i++) {
  3818. if (early_node_map[i].nid != nid)
  3819. continue;
  3820. /* Skip if an existing region covers this new one */
  3821. if (start_pfn >= early_node_map[i].start_pfn &&
  3822. end_pfn <= early_node_map[i].end_pfn)
  3823. return;
  3824. /* Merge forward if suitable */
  3825. if (start_pfn <= early_node_map[i].end_pfn &&
  3826. end_pfn > early_node_map[i].end_pfn) {
  3827. early_node_map[i].end_pfn = end_pfn;
  3828. return;
  3829. }
  3830. /* Merge backward if suitable */
  3831. if (start_pfn < early_node_map[i].start_pfn &&
  3832. end_pfn >= early_node_map[i].start_pfn) {
  3833. early_node_map[i].start_pfn = start_pfn;
  3834. return;
  3835. }
  3836. }
  3837. /* Check that early_node_map is large enough */
  3838. if (i >= MAX_ACTIVE_REGIONS) {
  3839. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  3840. MAX_ACTIVE_REGIONS);
  3841. return;
  3842. }
  3843. early_node_map[i].nid = nid;
  3844. early_node_map[i].start_pfn = start_pfn;
  3845. early_node_map[i].end_pfn = end_pfn;
  3846. nr_nodemap_entries = i + 1;
  3847. }
  3848. /**
  3849. * remove_active_range - Shrink an existing registered range of PFNs
  3850. * @nid: The node id the range is on that should be shrunk
  3851. * @start_pfn: The new PFN of the range
  3852. * @end_pfn: The new PFN of the range
  3853. *
  3854. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  3855. * The map is kept near the end physical page range that has already been
  3856. * registered. This function allows an arch to shrink an existing registered
  3857. * range.
  3858. */
  3859. void __init remove_active_range(unsigned int nid, unsigned long start_pfn,
  3860. unsigned long end_pfn)
  3861. {
  3862. int i, j;
  3863. int removed = 0;
  3864. printk(KERN_DEBUG "remove_active_range (%d, %lu, %lu)\n",
  3865. nid, start_pfn, end_pfn);
  3866. /* Find the old active region end and shrink */
  3867. for_each_active_range_index_in_nid(i, nid) {
  3868. if (early_node_map[i].start_pfn >= start_pfn &&
  3869. early_node_map[i].end_pfn <= end_pfn) {
  3870. /* clear it */
  3871. early_node_map[i].start_pfn = 0;
  3872. early_node_map[i].end_pfn = 0;
  3873. removed = 1;
  3874. continue;
  3875. }
  3876. if (early_node_map[i].start_pfn < start_pfn &&
  3877. early_node_map[i].end_pfn > start_pfn) {
  3878. unsigned long temp_end_pfn = early_node_map[i].end_pfn;
  3879. early_node_map[i].end_pfn = start_pfn;
  3880. if (temp_end_pfn > end_pfn)
  3881. add_active_range(nid, end_pfn, temp_end_pfn);
  3882. continue;
  3883. }
  3884. if (early_node_map[i].start_pfn >= start_pfn &&
  3885. early_node_map[i].end_pfn > end_pfn &&
  3886. early_node_map[i].start_pfn < end_pfn) {
  3887. early_node_map[i].start_pfn = end_pfn;
  3888. continue;
  3889. }
  3890. }
  3891. if (!removed)
  3892. return;
  3893. /* remove the blank ones */
  3894. for (i = nr_nodemap_entries - 1; i > 0; i--) {
  3895. if (early_node_map[i].nid != nid)
  3896. continue;
  3897. if (early_node_map[i].end_pfn)
  3898. continue;
  3899. /* we found it, get rid of it */
  3900. for (j = i; j < nr_nodemap_entries - 1; j++)
  3901. memcpy(&early_node_map[j], &early_node_map[j+1],
  3902. sizeof(early_node_map[j]));
  3903. j = nr_nodemap_entries - 1;
  3904. memset(&early_node_map[j], 0, sizeof(early_node_map[j]));
  3905. nr_nodemap_entries--;
  3906. }
  3907. }
  3908. /**
  3909. * remove_all_active_ranges - Remove all currently registered regions
  3910. *
  3911. * During discovery, it may be found that a table like SRAT is invalid
  3912. * and an alternative discovery method must be used. This function removes
  3913. * all currently registered regions.
  3914. */
  3915. void __init remove_all_active_ranges(void)
  3916. {
  3917. memset(early_node_map, 0, sizeof(early_node_map));
  3918. nr_nodemap_entries = 0;
  3919. }
  3920. /* Compare two active node_active_regions */
  3921. static int __init cmp_node_active_region(const void *a, const void *b)
  3922. {
  3923. struct node_active_region *arange = (struct node_active_region *)a;
  3924. struct node_active_region *brange = (struct node_active_region *)b;
  3925. /* Done this way to avoid overflows */
  3926. if (arange->start_pfn > brange->start_pfn)
  3927. return 1;
  3928. if (arange->start_pfn < brange->start_pfn)
  3929. return -1;
  3930. return 0;
  3931. }
  3932. /* sort the node_map by start_pfn */
  3933. void __init sort_node_map(void)
  3934. {
  3935. sort(early_node_map, (size_t)nr_nodemap_entries,
  3936. sizeof(struct node_active_region),
  3937. cmp_node_active_region, NULL);
  3938. }
  3939. /* Find the lowest pfn for a node */
  3940. static unsigned long __init find_min_pfn_for_node(int nid)
  3941. {
  3942. int i;
  3943. unsigned long min_pfn = ULONG_MAX;
  3944. /* Assuming a sorted map, the first range found has the starting pfn */
  3945. for_each_active_range_index_in_nid(i, nid)
  3946. min_pfn = min(min_pfn, early_node_map[i].start_pfn);
  3947. if (min_pfn == ULONG_MAX) {
  3948. printk(KERN_WARNING
  3949. "Could not find start_pfn for node %d\n", nid);
  3950. return 0;
  3951. }
  3952. return min_pfn;
  3953. }
  3954. /**
  3955. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  3956. *
  3957. * It returns the minimum PFN based on information provided via
  3958. * add_active_range().
  3959. */
  3960. unsigned long __init find_min_pfn_with_active_regions(void)
  3961. {
  3962. return find_min_pfn_for_node(MAX_NUMNODES);
  3963. }
  3964. /*
  3965. * early_calculate_totalpages()
  3966. * Sum pages in active regions for movable zone.
  3967. * Populate N_HIGH_MEMORY for calculating usable_nodes.
  3968. */
  3969. static unsigned long __init early_calculate_totalpages(void)
  3970. {
  3971. int i;
  3972. unsigned long totalpages = 0;
  3973. for (i = 0; i < nr_nodemap_entries; i++) {
  3974. unsigned long pages = early_node_map[i].end_pfn -
  3975. early_node_map[i].start_pfn;
  3976. totalpages += pages;
  3977. if (pages)
  3978. node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
  3979. }
  3980. return totalpages;
  3981. }
  3982. /*
  3983. * Find the PFN the Movable zone begins in each node. Kernel memory
  3984. * is spread evenly between nodes as long as the nodes have enough
  3985. * memory. When they don't, some nodes will have more kernelcore than
  3986. * others
  3987. */
  3988. static void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
  3989. {
  3990. int i, nid;
  3991. unsigned long usable_startpfn;
  3992. unsigned long kernelcore_node, kernelcore_remaining;
  3993. /* save the state before borrow the nodemask */
  3994. nodemask_t saved_node_state = node_states[N_HIGH_MEMORY];
  3995. unsigned long totalpages = early_calculate_totalpages();
  3996. int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
  3997. /*
  3998. * If movablecore was specified, calculate what size of
  3999. * kernelcore that corresponds so that memory usable for
  4000. * any allocation type is evenly spread. If both kernelcore
  4001. * and movablecore are specified, then the value of kernelcore
  4002. * will be used for required_kernelcore if it's greater than
  4003. * what movablecore would have allowed.
  4004. */
  4005. if (required_movablecore) {
  4006. unsigned long corepages;
  4007. /*
  4008. * Round-up so that ZONE_MOVABLE is at least as large as what
  4009. * was requested by the user
  4010. */
  4011. required_movablecore =
  4012. roundup(required_movablecore, MAX_ORDER_NR_PAGES);
  4013. corepages = totalpages - required_movablecore;
  4014. required_kernelcore = max(required_kernelcore, corepages);
  4015. }
  4016. /* If kernelcore was not specified, there is no ZONE_MOVABLE */
  4017. if (!required_kernelcore)
  4018. goto out;
  4019. /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
  4020. find_usable_zone_for_movable();
  4021. usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
  4022. restart:
  4023. /* Spread kernelcore memory as evenly as possible throughout nodes */
  4024. kernelcore_node = required_kernelcore / usable_nodes;
  4025. for_each_node_state(nid, N_HIGH_MEMORY) {
  4026. /*
  4027. * Recalculate kernelcore_node if the division per node
  4028. * now exceeds what is necessary to satisfy the requested
  4029. * amount of memory for the kernel
  4030. */
  4031. if (required_kernelcore < kernelcore_node)
  4032. kernelcore_node = required_kernelcore / usable_nodes;
  4033. /*
  4034. * As the map is walked, we track how much memory is usable
  4035. * by the kernel using kernelcore_remaining. When it is
  4036. * 0, the rest of the node is usable by ZONE_MOVABLE
  4037. */
  4038. kernelcore_remaining = kernelcore_node;
  4039. /* Go through each range of PFNs within this node */
  4040. for_each_active_range_index_in_nid(i, nid) {
  4041. unsigned long start_pfn, end_pfn;
  4042. unsigned long size_pages;
  4043. start_pfn = max(early_node_map[i].start_pfn,
  4044. zone_movable_pfn[nid]);
  4045. end_pfn = early_node_map[i].end_pfn;
  4046. if (start_pfn >= end_pfn)
  4047. continue;
  4048. /* Account for what is only usable for kernelcore */
  4049. if (start_pfn < usable_startpfn) {
  4050. unsigned long kernel_pages;
  4051. kernel_pages = min(end_pfn, usable_startpfn)
  4052. - start_pfn;
  4053. kernelcore_remaining -= min(kernel_pages,
  4054. kernelcore_remaining);
  4055. required_kernelcore -= min(kernel_pages,
  4056. required_kernelcore);
  4057. /* Continue if range is now fully accounted */
  4058. if (end_pfn <= usable_startpfn) {
  4059. /*
  4060. * Push zone_movable_pfn to the end so
  4061. * that if we have to rebalance
  4062. * kernelcore across nodes, we will
  4063. * not double account here
  4064. */
  4065. zone_movable_pfn[nid] = end_pfn;
  4066. continue;
  4067. }
  4068. start_pfn = usable_startpfn;
  4069. }
  4070. /*
  4071. * The usable PFN range for ZONE_MOVABLE is from
  4072. * start_pfn->end_pfn. Calculate size_pages as the
  4073. * number of pages used as kernelcore
  4074. */
  4075. size_pages = end_pfn - start_pfn;
  4076. if (size_pages > kernelcore_remaining)
  4077. size_pages = kernelcore_remaining;
  4078. zone_movable_pfn[nid] = start_pfn + size_pages;
  4079. /*
  4080. * Some kernelcore has been met, update counts and
  4081. * break if the kernelcore for this node has been
  4082. * satisified
  4083. */
  4084. required_kernelcore -= min(required_kernelcore,
  4085. size_pages);
  4086. kernelcore_remaining -= size_pages;
  4087. if (!kernelcore_remaining)
  4088. break;
  4089. }
  4090. }
  4091. /*
  4092. * If there is still required_kernelcore, we do another pass with one
  4093. * less node in the count. This will push zone_movable_pfn[nid] further
  4094. * along on the nodes that still have memory until kernelcore is
  4095. * satisified
  4096. */
  4097. usable_nodes--;
  4098. if (usable_nodes && required_kernelcore > usable_nodes)
  4099. goto restart;
  4100. /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
  4101. for (nid = 0; nid < MAX_NUMNODES; nid++)
  4102. zone_movable_pfn[nid] =
  4103. roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
  4104. out:
  4105. /* restore the node_state */
  4106. node_states[N_HIGH_MEMORY] = saved_node_state;
  4107. }
  4108. /* Any regular memory on that node ? */
  4109. static void check_for_regular_memory(pg_data_t *pgdat)
  4110. {
  4111. #ifdef CONFIG_HIGHMEM
  4112. enum zone_type zone_type;
  4113. for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
  4114. struct zone *zone = &pgdat->node_zones[zone_type];
  4115. if (zone->present_pages)
  4116. node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  4117. }
  4118. #endif
  4119. }
  4120. /**
  4121. * free_area_init_nodes - Initialise all pg_data_t and zone data
  4122. * @max_zone_pfn: an array of max PFNs for each zone
  4123. *
  4124. * This will call free_area_init_node() for each active node in the system.
  4125. * Using the page ranges provided by add_active_range(), the size of each
  4126. * zone in each node and their holes is calculated. If the maximum PFN
  4127. * between two adjacent zones match, it is assumed that the zone is empty.
  4128. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  4129. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  4130. * starts where the previous one ended. For example, ZONE_DMA32 starts
  4131. * at arch_max_dma_pfn.
  4132. */
  4133. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  4134. {
  4135. unsigned long nid;
  4136. int i;
  4137. /* Sort early_node_map as initialisation assumes it is sorted */
  4138. sort_node_map();
  4139. /* Record where the zone boundaries are */
  4140. memset(arch_zone_lowest_possible_pfn, 0,
  4141. sizeof(arch_zone_lowest_possible_pfn));
  4142. memset(arch_zone_highest_possible_pfn, 0,
  4143. sizeof(arch_zone_highest_possible_pfn));
  4144. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  4145. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  4146. for (i = 1; i < MAX_NR_ZONES; i++) {
  4147. if (i == ZONE_MOVABLE)
  4148. continue;
  4149. arch_zone_lowest_possible_pfn[i] =
  4150. arch_zone_highest_possible_pfn[i-1];
  4151. arch_zone_highest_possible_pfn[i] =
  4152. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  4153. }
  4154. arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
  4155. arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
  4156. /* Find the PFNs that ZONE_MOVABLE begins at in each node */
  4157. memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
  4158. find_zone_movable_pfns_for_nodes(zone_movable_pfn);
  4159. /* Print out the zone ranges */
  4160. printk("Zone PFN ranges:\n");
  4161. for (i = 0; i < MAX_NR_ZONES; i++) {
  4162. if (i == ZONE_MOVABLE)
  4163. continue;
  4164. printk(" %-8s ", zone_names[i]);
  4165. if (arch_zone_lowest_possible_pfn[i] ==
  4166. arch_zone_highest_possible_pfn[i])
  4167. printk("empty\n");
  4168. else
  4169. printk("%0#10lx -> %0#10lx\n",
  4170. arch_zone_lowest_possible_pfn[i],
  4171. arch_zone_highest_possible_pfn[i]);
  4172. }
  4173. /* Print out the PFNs ZONE_MOVABLE begins at in each node */
  4174. printk("Movable zone start PFN for each node\n");
  4175. for (i = 0; i < MAX_NUMNODES; i++) {
  4176. if (zone_movable_pfn[i])
  4177. printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
  4178. }
  4179. /* Print out the early_node_map[] */
  4180. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  4181. for (i = 0; i < nr_nodemap_entries; i++)
  4182. printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid,
  4183. early_node_map[i].start_pfn,
  4184. early_node_map[i].end_pfn);
  4185. /* Initialise every node */
  4186. mminit_verify_pageflags_layout();
  4187. setup_nr_node_ids();
  4188. for_each_online_node(nid) {
  4189. pg_data_t *pgdat = NODE_DATA(nid);
  4190. free_area_init_node(nid, NULL,
  4191. find_min_pfn_for_node(nid), NULL);
  4192. /* Any memory on that node */
  4193. if (pgdat->node_present_pages)
  4194. node_set_state(nid, N_HIGH_MEMORY);
  4195. check_for_regular_memory(pgdat);
  4196. }
  4197. }
  4198. static int __init cmdline_parse_core(char *p, unsigned long *core)
  4199. {
  4200. unsigned long long coremem;
  4201. if (!p)
  4202. return -EINVAL;
  4203. coremem = memparse(p, &p);
  4204. *core = coremem >> PAGE_SHIFT;
  4205. /* Paranoid check that UL is enough for the coremem value */
  4206. WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
  4207. return 0;
  4208. }
  4209. /*
  4210. * kernelcore=size sets the amount of memory for use for allocations that
  4211. * cannot be reclaimed or migrated.
  4212. */
  4213. static int __init cmdline_parse_kernelcore(char *p)
  4214. {
  4215. return cmdline_parse_core(p, &required_kernelcore);
  4216. }
  4217. /*
  4218. * movablecore=size sets the amount of memory for use for allocations that
  4219. * can be reclaimed or migrated.
  4220. */
  4221. static int __init cmdline_parse_movablecore(char *p)
  4222. {
  4223. return cmdline_parse_core(p, &required_movablecore);
  4224. }
  4225. early_param("kernelcore", cmdline_parse_kernelcore);
  4226. early_param("movablecore", cmdline_parse_movablecore);
  4227. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  4228. /**
  4229. * set_dma_reserve - set the specified number of pages reserved in the first zone
  4230. * @new_dma_reserve: The number of pages to mark reserved
  4231. *
  4232. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  4233. * In the DMA zone, a significant percentage may be consumed by kernel image
  4234. * and other unfreeable allocations which can skew the watermarks badly. This
  4235. * function may optionally be used to account for unfreeable pages in the
  4236. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  4237. * smaller per-cpu batchsize.
  4238. */
  4239. void __init set_dma_reserve(unsigned long new_dma_reserve)
  4240. {
  4241. dma_reserve = new_dma_reserve;
  4242. }
  4243. void __init free_area_init(unsigned long *zones_size)
  4244. {
  4245. free_area_init_node(0, zones_size,
  4246. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  4247. }
  4248. static int page_alloc_cpu_notify(struct notifier_block *self,
  4249. unsigned long action, void *hcpu)
  4250. {
  4251. int cpu = (unsigned long)hcpu;
  4252. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  4253. drain_pages(cpu);
  4254. /*
  4255. * Spill the event counters of the dead processor
  4256. * into the current processors event counters.
  4257. * This artificially elevates the count of the current
  4258. * processor.
  4259. */
  4260. vm_events_fold_cpu(cpu);
  4261. /*
  4262. * Zero the differential counters of the dead processor
  4263. * so that the vm statistics are consistent.
  4264. *
  4265. * This is only okay since the processor is dead and cannot
  4266. * race with what we are doing.
  4267. */
  4268. refresh_cpu_vm_stats(cpu);
  4269. }
  4270. return NOTIFY_OK;
  4271. }
  4272. void __init page_alloc_init(void)
  4273. {
  4274. hotcpu_notifier(page_alloc_cpu_notify, 0);
  4275. }
  4276. /*
  4277. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  4278. * or min_free_kbytes changes.
  4279. */
  4280. static void calculate_totalreserve_pages(void)
  4281. {
  4282. struct pglist_data *pgdat;
  4283. unsigned long reserve_pages = 0;
  4284. enum zone_type i, j;
  4285. for_each_online_pgdat(pgdat) {
  4286. for (i = 0; i < MAX_NR_ZONES; i++) {
  4287. struct zone *zone = pgdat->node_zones + i;
  4288. unsigned long max = 0;
  4289. /* Find valid and maximum lowmem_reserve in the zone */
  4290. for (j = i; j < MAX_NR_ZONES; j++) {
  4291. if (zone->lowmem_reserve[j] > max)
  4292. max = zone->lowmem_reserve[j];
  4293. }
  4294. /* we treat the high watermark as reserved pages. */
  4295. max += high_wmark_pages(zone);
  4296. if (max > zone->present_pages)
  4297. max = zone->present_pages;
  4298. reserve_pages += max;
  4299. }
  4300. }
  4301. totalreserve_pages = reserve_pages;
  4302. }
  4303. /*
  4304. * setup_per_zone_lowmem_reserve - called whenever
  4305. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  4306. * has a correct pages reserved value, so an adequate number of
  4307. * pages are left in the zone after a successful __alloc_pages().
  4308. */
  4309. static void setup_per_zone_lowmem_reserve(void)
  4310. {
  4311. struct pglist_data *pgdat;
  4312. enum zone_type j, idx;
  4313. for_each_online_pgdat(pgdat) {
  4314. for (j = 0; j < MAX_NR_ZONES; j++) {
  4315. struct zone *zone = pgdat->node_zones + j;
  4316. unsigned long present_pages = zone->present_pages;
  4317. zone->lowmem_reserve[j] = 0;
  4318. idx = j;
  4319. while (idx) {
  4320. struct zone *lower_zone;
  4321. idx--;
  4322. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  4323. sysctl_lowmem_reserve_ratio[idx] = 1;
  4324. lower_zone = pgdat->node_zones + idx;
  4325. lower_zone->lowmem_reserve[j] = present_pages /
  4326. sysctl_lowmem_reserve_ratio[idx];
  4327. present_pages += lower_zone->present_pages;
  4328. }
  4329. }
  4330. }
  4331. /* update totalreserve_pages */
  4332. calculate_totalreserve_pages();
  4333. }
  4334. /**
  4335. * setup_per_zone_wmarks - called when min_free_kbytes changes
  4336. * or when memory is hot-{added|removed}
  4337. *
  4338. * Ensures that the watermark[min,low,high] values for each zone are set
  4339. * correctly with respect to min_free_kbytes.
  4340. */
  4341. void setup_per_zone_wmarks(void)
  4342. {
  4343. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  4344. unsigned long lowmem_pages = 0;
  4345. struct zone *zone;
  4346. unsigned long flags;
  4347. /* Calculate total number of !ZONE_HIGHMEM pages */
  4348. for_each_zone(zone) {
  4349. if (!is_highmem(zone))
  4350. lowmem_pages += zone->present_pages;
  4351. }
  4352. for_each_zone(zone) {
  4353. u64 tmp;
  4354. spin_lock_irqsave(&zone->lock, flags);
  4355. tmp = (u64)pages_min * zone->present_pages;
  4356. do_div(tmp, lowmem_pages);
  4357. if (is_highmem(zone)) {
  4358. /*
  4359. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  4360. * need highmem pages, so cap pages_min to a small
  4361. * value here.
  4362. *
  4363. * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
  4364. * deltas controls asynch page reclaim, and so should
  4365. * not be capped for highmem.
  4366. */
  4367. int min_pages;
  4368. min_pages = zone->present_pages / 1024;
  4369. if (min_pages < SWAP_CLUSTER_MAX)
  4370. min_pages = SWAP_CLUSTER_MAX;
  4371. if (min_pages > 128)
  4372. min_pages = 128;
  4373. zone->watermark[WMARK_MIN] = min_pages;
  4374. } else {
  4375. /*
  4376. * If it's a lowmem zone, reserve a number of pages
  4377. * proportionate to the zone's size.
  4378. */
  4379. zone->watermark[WMARK_MIN] = tmp;
  4380. }
  4381. zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
  4382. zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
  4383. setup_zone_migrate_reserve(zone);
  4384. spin_unlock_irqrestore(&zone->lock, flags);
  4385. }
  4386. /* update totalreserve_pages */
  4387. calculate_totalreserve_pages();
  4388. }
  4389. /*
  4390. * The inactive anon list should be small enough that the VM never has to
  4391. * do too much work, but large enough that each inactive page has a chance
  4392. * to be referenced again before it is swapped out.
  4393. *
  4394. * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
  4395. * INACTIVE_ANON pages on this zone's LRU, maintained by the
  4396. * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
  4397. * the anonymous pages are kept on the inactive list.
  4398. *
  4399. * total target max
  4400. * memory ratio inactive anon
  4401. * -------------------------------------
  4402. * 10MB 1 5MB
  4403. * 100MB 1 50MB
  4404. * 1GB 3 250MB
  4405. * 10GB 10 0.9GB
  4406. * 100GB 31 3GB
  4407. * 1TB 101 10GB
  4408. * 10TB 320 32GB
  4409. */
  4410. static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
  4411. {
  4412. unsigned int gb, ratio;
  4413. /* Zone size in gigabytes */
  4414. gb = zone->present_pages >> (30 - PAGE_SHIFT);
  4415. if (gb)
  4416. ratio = int_sqrt(10 * gb);
  4417. else
  4418. ratio = 1;
  4419. zone->inactive_ratio = ratio;
  4420. }
  4421. static void __meminit setup_per_zone_inactive_ratio(void)
  4422. {
  4423. struct zone *zone;
  4424. for_each_zone(zone)
  4425. calculate_zone_inactive_ratio(zone);
  4426. }
  4427. /*
  4428. * Initialise min_free_kbytes.
  4429. *
  4430. * For small machines we want it small (128k min). For large machines
  4431. * we want it large (64MB max). But it is not linear, because network
  4432. * bandwidth does not increase linearly with machine size. We use
  4433. *
  4434. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  4435. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  4436. *
  4437. * which yields
  4438. *
  4439. * 16MB: 512k
  4440. * 32MB: 724k
  4441. * 64MB: 1024k
  4442. * 128MB: 1448k
  4443. * 256MB: 2048k
  4444. * 512MB: 2896k
  4445. * 1024MB: 4096k
  4446. * 2048MB: 5792k
  4447. * 4096MB: 8192k
  4448. * 8192MB: 11584k
  4449. * 16384MB: 16384k
  4450. */
  4451. int __meminit init_per_zone_wmark_min(void)
  4452. {
  4453. unsigned long lowmem_kbytes;
  4454. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  4455. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  4456. if (min_free_kbytes < 128)
  4457. min_free_kbytes = 128;
  4458. if (min_free_kbytes > 65536)
  4459. min_free_kbytes = 65536;
  4460. setup_per_zone_wmarks();
  4461. refresh_zone_stat_thresholds();
  4462. setup_per_zone_lowmem_reserve();
  4463. setup_per_zone_inactive_ratio();
  4464. return 0;
  4465. }
  4466. module_init(init_per_zone_wmark_min)
  4467. /*
  4468. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  4469. * that we can call two helper functions whenever min_free_kbytes
  4470. * changes.
  4471. */
  4472. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  4473. void __user *buffer, size_t *length, loff_t *ppos)
  4474. {
  4475. proc_dointvec(table, write, buffer, length, ppos);
  4476. if (write)
  4477. setup_per_zone_wmarks();
  4478. return 0;
  4479. }
  4480. #ifdef CONFIG_NUMA
  4481. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  4482. void __user *buffer, size_t *length, loff_t *ppos)
  4483. {
  4484. struct zone *zone;
  4485. int rc;
  4486. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4487. if (rc)
  4488. return rc;
  4489. for_each_zone(zone)
  4490. zone->min_unmapped_pages = (zone->present_pages *
  4491. sysctl_min_unmapped_ratio) / 100;
  4492. return 0;
  4493. }
  4494. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  4495. void __user *buffer, size_t *length, loff_t *ppos)
  4496. {
  4497. struct zone *zone;
  4498. int rc;
  4499. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4500. if (rc)
  4501. return rc;
  4502. for_each_zone(zone)
  4503. zone->min_slab_pages = (zone->present_pages *
  4504. sysctl_min_slab_ratio) / 100;
  4505. return 0;
  4506. }
  4507. #endif
  4508. /*
  4509. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  4510. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  4511. * whenever sysctl_lowmem_reserve_ratio changes.
  4512. *
  4513. * The reserve ratio obviously has absolutely no relation with the
  4514. * minimum watermarks. The lowmem reserve ratio can only make sense
  4515. * if in function of the boot time zone sizes.
  4516. */
  4517. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  4518. void __user *buffer, size_t *length, loff_t *ppos)
  4519. {
  4520. proc_dointvec_minmax(table, write, buffer, length, ppos);
  4521. setup_per_zone_lowmem_reserve();
  4522. return 0;
  4523. }
  4524. /*
  4525. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  4526. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  4527. * can have before it gets flushed back to buddy allocator.
  4528. */
  4529. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  4530. void __user *buffer, size_t *length, loff_t *ppos)
  4531. {
  4532. struct zone *zone;
  4533. unsigned int cpu;
  4534. int ret;
  4535. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4536. if (!write || (ret == -EINVAL))
  4537. return ret;
  4538. for_each_populated_zone(zone) {
  4539. for_each_possible_cpu(cpu) {
  4540. unsigned long high;
  4541. high = zone->present_pages / percpu_pagelist_fraction;
  4542. setup_pagelist_highmark(
  4543. per_cpu_ptr(zone->pageset, cpu), high);
  4544. }
  4545. }
  4546. return 0;
  4547. }
  4548. int hashdist = HASHDIST_DEFAULT;
  4549. #ifdef CONFIG_NUMA
  4550. static int __init set_hashdist(char *str)
  4551. {
  4552. if (!str)
  4553. return 0;
  4554. hashdist = simple_strtoul(str, &str, 0);
  4555. return 1;
  4556. }
  4557. __setup("hashdist=", set_hashdist);
  4558. #endif
  4559. /*
  4560. * allocate a large system hash table from bootmem
  4561. * - it is assumed that the hash table must contain an exact power-of-2
  4562. * quantity of entries
  4563. * - limit is the number of hash buckets, not the total allocation size
  4564. */
  4565. void *__init alloc_large_system_hash(const char *tablename,
  4566. unsigned long bucketsize,
  4567. unsigned long numentries,
  4568. int scale,
  4569. int flags,
  4570. unsigned int *_hash_shift,
  4571. unsigned int *_hash_mask,
  4572. unsigned long limit)
  4573. {
  4574. unsigned long long max = limit;
  4575. unsigned long log2qty, size;
  4576. void *table = NULL;
  4577. /* allow the kernel cmdline to have a say */
  4578. if (!numentries) {
  4579. /* round applicable memory size up to nearest megabyte */
  4580. numentries = nr_kernel_pages;
  4581. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  4582. numentries >>= 20 - PAGE_SHIFT;
  4583. numentries <<= 20 - PAGE_SHIFT;
  4584. /* limit to 1 bucket per 2^scale bytes of low memory */
  4585. if (scale > PAGE_SHIFT)
  4586. numentries >>= (scale - PAGE_SHIFT);
  4587. else
  4588. numentries <<= (PAGE_SHIFT - scale);
  4589. /* Make sure we've got at least a 0-order allocation.. */
  4590. if (unlikely(flags & HASH_SMALL)) {
  4591. /* Makes no sense without HASH_EARLY */
  4592. WARN_ON(!(flags & HASH_EARLY));
  4593. if (!(numentries >> *_hash_shift)) {
  4594. numentries = 1UL << *_hash_shift;
  4595. BUG_ON(!numentries);
  4596. }
  4597. } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  4598. numentries = PAGE_SIZE / bucketsize;
  4599. }
  4600. numentries = roundup_pow_of_two(numentries);
  4601. /* limit allocation size to 1/16 total memory by default */
  4602. if (max == 0) {
  4603. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  4604. do_div(max, bucketsize);
  4605. }
  4606. if (numentries > max)
  4607. numentries = max;
  4608. log2qty = ilog2(numentries);
  4609. do {
  4610. size = bucketsize << log2qty;
  4611. if (flags & HASH_EARLY)
  4612. table = alloc_bootmem_nopanic(size);
  4613. else if (hashdist)
  4614. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  4615. else {
  4616. /*
  4617. * If bucketsize is not a power-of-two, we may free
  4618. * some pages at the end of hash table which
  4619. * alloc_pages_exact() automatically does
  4620. */
  4621. if (get_order(size) < MAX_ORDER) {
  4622. table = alloc_pages_exact(size, GFP_ATOMIC);
  4623. kmemleak_alloc(table, size, 1, GFP_ATOMIC);
  4624. }
  4625. }
  4626. } while (!table && size > PAGE_SIZE && --log2qty);
  4627. if (!table)
  4628. panic("Failed to allocate %s hash table\n", tablename);
  4629. printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
  4630. tablename,
  4631. (1UL << log2qty),
  4632. ilog2(size) - PAGE_SHIFT,
  4633. size);
  4634. if (_hash_shift)
  4635. *_hash_shift = log2qty;
  4636. if (_hash_mask)
  4637. *_hash_mask = (1 << log2qty) - 1;
  4638. return table;
  4639. }
  4640. /* Return a pointer to the bitmap storing bits affecting a block of pages */
  4641. static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
  4642. unsigned long pfn)
  4643. {
  4644. #ifdef CONFIG_SPARSEMEM
  4645. return __pfn_to_section(pfn)->pageblock_flags;
  4646. #else
  4647. return zone->pageblock_flags;
  4648. #endif /* CONFIG_SPARSEMEM */
  4649. }
  4650. static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
  4651. {
  4652. #ifdef CONFIG_SPARSEMEM
  4653. pfn &= (PAGES_PER_SECTION-1);
  4654. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4655. #else
  4656. pfn = pfn - zone->zone_start_pfn;
  4657. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4658. #endif /* CONFIG_SPARSEMEM */
  4659. }
  4660. /**
  4661. * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
  4662. * @page: The page within the block of interest
  4663. * @start_bitidx: The first bit of interest to retrieve
  4664. * @end_bitidx: The last bit of interest
  4665. * returns pageblock_bits flags
  4666. */
  4667. unsigned long get_pageblock_flags_group(struct page *page,
  4668. int start_bitidx, int end_bitidx)
  4669. {
  4670. struct zone *zone;
  4671. unsigned long *bitmap;
  4672. unsigned long pfn, bitidx;
  4673. unsigned long flags = 0;
  4674. unsigned long value = 1;
  4675. zone = page_zone(page);
  4676. pfn = page_to_pfn(page);
  4677. bitmap = get_pageblock_bitmap(zone, pfn);
  4678. bitidx = pfn_to_bitidx(zone, pfn);
  4679. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4680. if (test_bit(bitidx + start_bitidx, bitmap))
  4681. flags |= value;
  4682. return flags;
  4683. }
  4684. /**
  4685. * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
  4686. * @page: The page within the block of interest
  4687. * @start_bitidx: The first bit of interest
  4688. * @end_bitidx: The last bit of interest
  4689. * @flags: The flags to set
  4690. */
  4691. void set_pageblock_flags_group(struct page *page, unsigned long flags,
  4692. int start_bitidx, int end_bitidx)
  4693. {
  4694. struct zone *zone;
  4695. unsigned long *bitmap;
  4696. unsigned long pfn, bitidx;
  4697. unsigned long value = 1;
  4698. zone = page_zone(page);
  4699. pfn = page_to_pfn(page);
  4700. bitmap = get_pageblock_bitmap(zone, pfn);
  4701. bitidx = pfn_to_bitidx(zone, pfn);
  4702. VM_BUG_ON(pfn < zone->zone_start_pfn);
  4703. VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
  4704. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4705. if (flags & value)
  4706. __set_bit(bitidx + start_bitidx, bitmap);
  4707. else
  4708. __clear_bit(bitidx + start_bitidx, bitmap);
  4709. }
  4710. /*
  4711. * This is designed as sub function...plz see page_isolation.c also.
  4712. * set/clear page block's type to be ISOLATE.
  4713. * page allocater never alloc memory from ISOLATE block.
  4714. */
  4715. static int
  4716. __count_immobile_pages(struct zone *zone, struct page *page, int count)
  4717. {
  4718. unsigned long pfn, iter, found;
  4719. /*
  4720. * For avoiding noise data, lru_add_drain_all() should be called
  4721. * If ZONE_MOVABLE, the zone never contains immobile pages
  4722. */
  4723. if (zone_idx(zone) == ZONE_MOVABLE)
  4724. return true;
  4725. if (get_pageblock_migratetype(page) == MIGRATE_MOVABLE)
  4726. return true;
  4727. pfn = page_to_pfn(page);
  4728. for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
  4729. unsigned long check = pfn + iter;
  4730. if (!pfn_valid_within(check))
  4731. continue;
  4732. page = pfn_to_page(check);
  4733. if (!page_count(page)) {
  4734. if (PageBuddy(page))
  4735. iter += (1 << page_order(page)) - 1;
  4736. continue;
  4737. }
  4738. if (!PageLRU(page))
  4739. found++;
  4740. /*
  4741. * If there are RECLAIMABLE pages, we need to check it.
  4742. * But now, memory offline itself doesn't call shrink_slab()
  4743. * and it still to be fixed.
  4744. */
  4745. /*
  4746. * If the page is not RAM, page_count()should be 0.
  4747. * we don't need more check. This is an _used_ not-movable page.
  4748. *
  4749. * The problematic thing here is PG_reserved pages. PG_reserved
  4750. * is set to both of a memory hole page and a _used_ kernel
  4751. * page at boot.
  4752. */
  4753. if (found > count)
  4754. return false;
  4755. }
  4756. return true;
  4757. }
  4758. bool is_pageblock_removable_nolock(struct page *page)
  4759. {
  4760. struct zone *zone = page_zone(page);
  4761. return __count_immobile_pages(zone, page, 0);
  4762. }
  4763. int set_migratetype_isolate(struct page *page)
  4764. {
  4765. struct zone *zone;
  4766. unsigned long flags, pfn;
  4767. struct memory_isolate_notify arg;
  4768. int notifier_ret;
  4769. int ret = -EBUSY;
  4770. zone = page_zone(page);
  4771. spin_lock_irqsave(&zone->lock, flags);
  4772. pfn = page_to_pfn(page);
  4773. arg.start_pfn = pfn;
  4774. arg.nr_pages = pageblock_nr_pages;
  4775. arg.pages_found = 0;
  4776. /*
  4777. * It may be possible to isolate a pageblock even if the
  4778. * migratetype is not MIGRATE_MOVABLE. The memory isolation
  4779. * notifier chain is used by balloon drivers to return the
  4780. * number of pages in a range that are held by the balloon
  4781. * driver to shrink memory. If all the pages are accounted for
  4782. * by balloons, are free, or on the LRU, isolation can continue.
  4783. * Later, for example, when memory hotplug notifier runs, these
  4784. * pages reported as "can be isolated" should be isolated(freed)
  4785. * by the balloon driver through the memory notifier chain.
  4786. */
  4787. notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
  4788. notifier_ret = notifier_to_errno(notifier_ret);
  4789. if (notifier_ret)
  4790. goto out;
  4791. /*
  4792. * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
  4793. * We just check MOVABLE pages.
  4794. */
  4795. if (__count_immobile_pages(zone, page, arg.pages_found))
  4796. ret = 0;
  4797. /*
  4798. * immobile means "not-on-lru" paes. If immobile is larger than
  4799. * removable-by-driver pages reported by notifier, we'll fail.
  4800. */
  4801. out:
  4802. if (!ret) {
  4803. set_pageblock_migratetype(page, MIGRATE_ISOLATE);
  4804. move_freepages_block(zone, page, MIGRATE_ISOLATE);
  4805. }
  4806. spin_unlock_irqrestore(&zone->lock, flags);
  4807. if (!ret)
  4808. drain_all_pages();
  4809. return ret;
  4810. }
  4811. void unset_migratetype_isolate(struct page *page)
  4812. {
  4813. struct zone *zone;
  4814. unsigned long flags;
  4815. zone = page_zone(page);
  4816. spin_lock_irqsave(&zone->lock, flags);
  4817. if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  4818. goto out;
  4819. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  4820. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  4821. out:
  4822. spin_unlock_irqrestore(&zone->lock, flags);
  4823. }
  4824. #ifdef CONFIG_MEMORY_HOTREMOVE
  4825. /*
  4826. * All pages in the range must be isolated before calling this.
  4827. */
  4828. void
  4829. __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  4830. {
  4831. struct page *page;
  4832. struct zone *zone;
  4833. int order, i;
  4834. unsigned long pfn;
  4835. unsigned long flags;
  4836. /* find the first valid pfn */
  4837. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  4838. if (pfn_valid(pfn))
  4839. break;
  4840. if (pfn == end_pfn)
  4841. return;
  4842. zone = page_zone(pfn_to_page(pfn));
  4843. spin_lock_irqsave(&zone->lock, flags);
  4844. pfn = start_pfn;
  4845. while (pfn < end_pfn) {
  4846. if (!pfn_valid(pfn)) {
  4847. pfn++;
  4848. continue;
  4849. }
  4850. page = pfn_to_page(pfn);
  4851. BUG_ON(page_count(page));
  4852. BUG_ON(!PageBuddy(page));
  4853. order = page_order(page);
  4854. #ifdef CONFIG_DEBUG_VM
  4855. printk(KERN_INFO "remove from free list %lx %d %lx\n",
  4856. pfn, 1 << order, end_pfn);
  4857. #endif
  4858. list_del(&page->lru);
  4859. rmv_page_order(page);
  4860. zone->free_area[order].nr_free--;
  4861. __mod_zone_page_state(zone, NR_FREE_PAGES,
  4862. - (1UL << order));
  4863. for (i = 0; i < (1 << order); i++)
  4864. SetPageReserved((page+i));
  4865. pfn += (1 << order);
  4866. }
  4867. spin_unlock_irqrestore(&zone->lock, flags);
  4868. }
  4869. #endif
  4870. #ifdef CONFIG_MEMORY_FAILURE
  4871. bool is_free_buddy_page(struct page *page)
  4872. {
  4873. struct zone *zone = page_zone(page);
  4874. unsigned long pfn = page_to_pfn(page);
  4875. unsigned long flags;
  4876. int order;
  4877. spin_lock_irqsave(&zone->lock, flags);
  4878. for (order = 0; order < MAX_ORDER; order++) {
  4879. struct page *page_head = page - (pfn & ((1 << order) - 1));
  4880. if (PageBuddy(page_head) && page_order(page_head) >= order)
  4881. break;
  4882. }
  4883. spin_unlock_irqrestore(&zone->lock, flags);
  4884. return order < MAX_ORDER;
  4885. }
  4886. #endif
  4887. static struct trace_print_flags pageflag_names[] = {
  4888. {1UL << PG_locked, "locked" },
  4889. {1UL << PG_error, "error" },
  4890. {1UL << PG_referenced, "referenced" },
  4891. {1UL << PG_uptodate, "uptodate" },
  4892. {1UL << PG_dirty, "dirty" },
  4893. {1UL << PG_lru, "lru" },
  4894. {1UL << PG_active, "active" },
  4895. {1UL << PG_slab, "slab" },
  4896. {1UL << PG_owner_priv_1, "owner_priv_1" },
  4897. {1UL << PG_arch_1, "arch_1" },
  4898. {1UL << PG_reserved, "reserved" },
  4899. {1UL << PG_private, "private" },
  4900. {1UL << PG_private_2, "private_2" },
  4901. {1UL << PG_writeback, "writeback" },
  4902. #ifdef CONFIG_PAGEFLAGS_EXTENDED
  4903. {1UL << PG_head, "head" },
  4904. {1UL << PG_tail, "tail" },
  4905. #else
  4906. {1UL << PG_compound, "compound" },
  4907. #endif
  4908. {1UL << PG_swapcache, "swapcache" },
  4909. {1UL << PG_mappedtodisk, "mappedtodisk" },
  4910. {1UL << PG_reclaim, "reclaim" },
  4911. {1UL << PG_swapbacked, "swapbacked" },
  4912. {1UL << PG_unevictable, "unevictable" },
  4913. #ifdef CONFIG_MMU
  4914. {1UL << PG_mlocked, "mlocked" },
  4915. #endif
  4916. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  4917. {1UL << PG_uncached, "uncached" },
  4918. #endif
  4919. #ifdef CONFIG_MEMORY_FAILURE
  4920. {1UL << PG_hwpoison, "hwpoison" },
  4921. #endif
  4922. {-1UL, NULL },
  4923. };
  4924. static void dump_page_flags(unsigned long flags)
  4925. {
  4926. const char *delim = "";
  4927. unsigned long mask;
  4928. int i;
  4929. printk(KERN_ALERT "page flags: %#lx(", flags);
  4930. /* remove zone id */
  4931. flags &= (1UL << NR_PAGEFLAGS) - 1;
  4932. for (i = 0; pageflag_names[i].name && flags; i++) {
  4933. mask = pageflag_names[i].mask;
  4934. if ((flags & mask) != mask)
  4935. continue;
  4936. flags &= ~mask;
  4937. printk("%s%s", delim, pageflag_names[i].name);
  4938. delim = "|";
  4939. }
  4940. /* check for left over flags */
  4941. if (flags)
  4942. printk("%s%#lx", delim, flags);
  4943. printk(")\n");
  4944. }
  4945. void dump_page(struct page *page)
  4946. {
  4947. printk(KERN_ALERT
  4948. "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
  4949. page, atomic_read(&page->_count), page_mapcount(page),
  4950. page->mapping, page->index);
  4951. dump_page_flags(page->flags);
  4952. mem_cgroup_print_bad_page(page);
  4953. }