slab.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752
  1. /*
  2. * linux/mm/slab.c
  3. * Written by Mark Hemment, 1996/97.
  4. * (markhe@nextd.demon.co.uk)
  5. *
  6. * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
  7. *
  8. * Major cleanup, different bufctl logic, per-cpu arrays
  9. * (c) 2000 Manfred Spraul
  10. *
  11. * Cleanup, make the head arrays unconditional, preparation for NUMA
  12. * (c) 2002 Manfred Spraul
  13. *
  14. * An implementation of the Slab Allocator as described in outline in;
  15. * UNIX Internals: The New Frontiers by Uresh Vahalia
  16. * Pub: Prentice Hall ISBN 0-13-101908-2
  17. * or with a little more detail in;
  18. * The Slab Allocator: An Object-Caching Kernel Memory Allocator
  19. * Jeff Bonwick (Sun Microsystems).
  20. * Presented at: USENIX Summer 1994 Technical Conference
  21. *
  22. * The memory is organized in caches, one cache for each object type.
  23. * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
  24. * Each cache consists out of many slabs (they are small (usually one
  25. * page long) and always contiguous), and each slab contains multiple
  26. * initialized objects.
  27. *
  28. * This means, that your constructor is used only for newly allocated
  29. * slabs and you must pass objects with the same initializations to
  30. * kmem_cache_free.
  31. *
  32. * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
  33. * normal). If you need a special memory type, then must create a new
  34. * cache for that memory type.
  35. *
  36. * In order to reduce fragmentation, the slabs are sorted in 3 groups:
  37. * full slabs with 0 free objects
  38. * partial slabs
  39. * empty slabs with no allocated objects
  40. *
  41. * If partial slabs exist, then new allocations come from these slabs,
  42. * otherwise from empty slabs or new slabs are allocated.
  43. *
  44. * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
  45. * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
  46. *
  47. * Each cache has a short per-cpu head array, most allocs
  48. * and frees go into that array, and if that array overflows, then 1/2
  49. * of the entries in the array are given back into the global cache.
  50. * The head array is strictly LIFO and should improve the cache hit rates.
  51. * On SMP, it additionally reduces the spinlock operations.
  52. *
  53. * The c_cpuarray may not be read with enabled local interrupts -
  54. * it's changed with a smp_call_function().
  55. *
  56. * SMP synchronization:
  57. * constructors and destructors are called without any locking.
  58. * Several members in struct kmem_cache and struct slab never change, they
  59. * are accessed without any locking.
  60. * The per-cpu arrays are never accessed from the wrong cpu, no locking,
  61. * and local interrupts are disabled so slab code is preempt-safe.
  62. * The non-constant members are protected with a per-cache irq spinlock.
  63. *
  64. * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
  65. * in 2000 - many ideas in the current implementation are derived from
  66. * his patch.
  67. *
  68. * Further notes from the original documentation:
  69. *
  70. * 11 April '97. Started multi-threading - markhe
  71. * The global cache-chain is protected by the mutex 'slab_mutex'.
  72. * The sem is only needed when accessing/extending the cache-chain, which
  73. * can never happen inside an interrupt (kmem_cache_create(),
  74. * kmem_cache_shrink() and kmem_cache_reap()).
  75. *
  76. * At present, each engine can be growing a cache. This should be blocked.
  77. *
  78. * 15 March 2005. NUMA slab allocator.
  79. * Shai Fultheim <shai@scalex86.org>.
  80. * Shobhit Dayal <shobhit@calsoftinc.com>
  81. * Alok N Kataria <alokk@calsoftinc.com>
  82. * Christoph Lameter <christoph@lameter.com>
  83. *
  84. * Modified the slab allocator to be node aware on NUMA systems.
  85. * Each node has its own list of partial, free and full slabs.
  86. * All object allocations for a node occur from node specific slab lists.
  87. */
  88. #include <linux/slab.h>
  89. #include "slab.h"
  90. #include <linux/mm.h>
  91. #include <linux/poison.h>
  92. #include <linux/swap.h>
  93. #include <linux/cache.h>
  94. #include <linux/interrupt.h>
  95. #include <linux/init.h>
  96. #include <linux/compiler.h>
  97. #include <linux/cpuset.h>
  98. #include <linux/proc_fs.h>
  99. #include <linux/seq_file.h>
  100. #include <linux/notifier.h>
  101. #include <linux/kallsyms.h>
  102. #include <linux/cpu.h>
  103. #include <linux/sysctl.h>
  104. #include <linux/module.h>
  105. #include <linux/rcupdate.h>
  106. #include <linux/string.h>
  107. #include <linux/uaccess.h>
  108. #include <linux/nodemask.h>
  109. #include <linux/kmemleak.h>
  110. #include <linux/mempolicy.h>
  111. #include <linux/mutex.h>
  112. #include <linux/fault-inject.h>
  113. #include <linux/rtmutex.h>
  114. #include <linux/reciprocal_div.h>
  115. #include <linux/debugobjects.h>
  116. #include <linux/kmemcheck.h>
  117. #include <linux/memory.h>
  118. #include <linux/prefetch.h>
  119. #include <net/sock.h>
  120. #include <asm/cacheflush.h>
  121. #include <asm/tlbflush.h>
  122. #include <asm/page.h>
  123. #include <trace/events/kmem.h>
  124. #include "internal.h"
  125. /*
  126. * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
  127. * 0 for faster, smaller code (especially in the critical paths).
  128. *
  129. * STATS - 1 to collect stats for /proc/slabinfo.
  130. * 0 for faster, smaller code (especially in the critical paths).
  131. *
  132. * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
  133. */
  134. #ifdef CONFIG_DEBUG_SLAB
  135. #define DEBUG 1
  136. #define STATS 1
  137. #define FORCED_DEBUG 1
  138. #else
  139. #define DEBUG 0
  140. #define STATS 0
  141. #define FORCED_DEBUG 0
  142. #endif
  143. /* Shouldn't this be in a header file somewhere? */
  144. #define BYTES_PER_WORD sizeof(void *)
  145. #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
  146. #ifndef ARCH_KMALLOC_FLAGS
  147. #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
  148. #endif
  149. /*
  150. * true if a page was allocated from pfmemalloc reserves for network-based
  151. * swap
  152. */
  153. static bool pfmemalloc_active __read_mostly;
  154. /* Legal flag mask for kmem_cache_create(). */
  155. #if DEBUG
  156. # define CREATE_MASK (SLAB_RED_ZONE | \
  157. SLAB_POISON | SLAB_HWCACHE_ALIGN | \
  158. SLAB_CACHE_DMA | \
  159. SLAB_STORE_USER | \
  160. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  161. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
  162. SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
  163. #else
  164. # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
  165. SLAB_CACHE_DMA | \
  166. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  167. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
  168. SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
  169. #endif
  170. /*
  171. * kmem_bufctl_t:
  172. *
  173. * Bufctl's are used for linking objs within a slab
  174. * linked offsets.
  175. *
  176. * This implementation relies on "struct page" for locating the cache &
  177. * slab an object belongs to.
  178. * This allows the bufctl structure to be small (one int), but limits
  179. * the number of objects a slab (not a cache) can contain when off-slab
  180. * bufctls are used. The limit is the size of the largest general cache
  181. * that does not use off-slab slabs.
  182. * For 32bit archs with 4 kB pages, is this 56.
  183. * This is not serious, as it is only for large objects, when it is unwise
  184. * to have too many per slab.
  185. * Note: This limit can be raised by introducing a general cache whose size
  186. * is less than 512 (PAGE_SIZE<<3), but greater than 256.
  187. */
  188. typedef unsigned int kmem_bufctl_t;
  189. #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
  190. #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
  191. #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
  192. #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
  193. /*
  194. * struct slab_rcu
  195. *
  196. * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
  197. * arrange for kmem_freepages to be called via RCU. This is useful if
  198. * we need to approach a kernel structure obliquely, from its address
  199. * obtained without the usual locking. We can lock the structure to
  200. * stabilize it and check it's still at the given address, only if we
  201. * can be sure that the memory has not been meanwhile reused for some
  202. * other kind of object (which our subsystem's lock might corrupt).
  203. *
  204. * rcu_read_lock before reading the address, then rcu_read_unlock after
  205. * taking the spinlock within the structure expected at that address.
  206. */
  207. struct slab_rcu {
  208. struct rcu_head head;
  209. struct kmem_cache *cachep;
  210. void *addr;
  211. };
  212. /*
  213. * struct slab
  214. *
  215. * Manages the objs in a slab. Placed either at the beginning of mem allocated
  216. * for a slab, or allocated from an general cache.
  217. * Slabs are chained into three list: fully used, partial, fully free slabs.
  218. */
  219. struct slab {
  220. union {
  221. struct {
  222. struct list_head list;
  223. unsigned long colouroff;
  224. void *s_mem; /* including colour offset */
  225. unsigned int inuse; /* num of objs active in slab */
  226. kmem_bufctl_t free;
  227. unsigned short nodeid;
  228. };
  229. struct slab_rcu __slab_cover_slab_rcu;
  230. };
  231. };
  232. /*
  233. * struct array_cache
  234. *
  235. * Purpose:
  236. * - LIFO ordering, to hand out cache-warm objects from _alloc
  237. * - reduce the number of linked list operations
  238. * - reduce spinlock operations
  239. *
  240. * The limit is stored in the per-cpu structure to reduce the data cache
  241. * footprint.
  242. *
  243. */
  244. struct array_cache {
  245. unsigned int avail;
  246. unsigned int limit;
  247. unsigned int batchcount;
  248. unsigned int touched;
  249. spinlock_t lock;
  250. void *entry[]; /*
  251. * Must have this definition in here for the proper
  252. * alignment of array_cache. Also simplifies accessing
  253. * the entries.
  254. *
  255. * Entries should not be directly dereferenced as
  256. * entries belonging to slabs marked pfmemalloc will
  257. * have the lower bits set SLAB_OBJ_PFMEMALLOC
  258. */
  259. };
  260. #define SLAB_OBJ_PFMEMALLOC 1
  261. static inline bool is_obj_pfmemalloc(void *objp)
  262. {
  263. return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
  264. }
  265. static inline void set_obj_pfmemalloc(void **objp)
  266. {
  267. *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
  268. return;
  269. }
  270. static inline void clear_obj_pfmemalloc(void **objp)
  271. {
  272. *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
  273. }
  274. /*
  275. * bootstrap: The caches do not work without cpuarrays anymore, but the
  276. * cpuarrays are allocated from the generic caches...
  277. */
  278. #define BOOT_CPUCACHE_ENTRIES 1
  279. struct arraycache_init {
  280. struct array_cache cache;
  281. void *entries[BOOT_CPUCACHE_ENTRIES];
  282. };
  283. /*
  284. * The slab lists for all objects.
  285. */
  286. struct kmem_list3 {
  287. struct list_head slabs_partial; /* partial list first, better asm code */
  288. struct list_head slabs_full;
  289. struct list_head slabs_free;
  290. unsigned long free_objects;
  291. unsigned int free_limit;
  292. unsigned int colour_next; /* Per-node cache coloring */
  293. spinlock_t list_lock;
  294. struct array_cache *shared; /* shared per node */
  295. struct array_cache **alien; /* on other nodes */
  296. unsigned long next_reap; /* updated without locking */
  297. int free_touched; /* updated without locking */
  298. };
  299. /*
  300. * Need this for bootstrapping a per node allocator.
  301. */
  302. #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
  303. static struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
  304. #define CACHE_CACHE 0
  305. #define SIZE_AC MAX_NUMNODES
  306. #define SIZE_L3 (2 * MAX_NUMNODES)
  307. static int drain_freelist(struct kmem_cache *cache,
  308. struct kmem_list3 *l3, int tofree);
  309. static void free_block(struct kmem_cache *cachep, void **objpp, int len,
  310. int node);
  311. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
  312. static void cache_reap(struct work_struct *unused);
  313. /*
  314. * This function must be completely optimized away if a constant is passed to
  315. * it. Mostly the same as what is in linux/slab.h except it returns an index.
  316. */
  317. static __always_inline int index_of(const size_t size)
  318. {
  319. extern void __bad_size(void);
  320. if (__builtin_constant_p(size)) {
  321. int i = 0;
  322. #define CACHE(x) \
  323. if (size <=x) \
  324. return i; \
  325. else \
  326. i++;
  327. #include <linux/kmalloc_sizes.h>
  328. #undef CACHE
  329. __bad_size();
  330. } else
  331. __bad_size();
  332. return 0;
  333. }
  334. static int slab_early_init = 1;
  335. #define INDEX_AC index_of(sizeof(struct arraycache_init))
  336. #define INDEX_L3 index_of(sizeof(struct kmem_list3))
  337. static void kmem_list3_init(struct kmem_list3 *parent)
  338. {
  339. INIT_LIST_HEAD(&parent->slabs_full);
  340. INIT_LIST_HEAD(&parent->slabs_partial);
  341. INIT_LIST_HEAD(&parent->slabs_free);
  342. parent->shared = NULL;
  343. parent->alien = NULL;
  344. parent->colour_next = 0;
  345. spin_lock_init(&parent->list_lock);
  346. parent->free_objects = 0;
  347. parent->free_touched = 0;
  348. }
  349. #define MAKE_LIST(cachep, listp, slab, nodeid) \
  350. do { \
  351. INIT_LIST_HEAD(listp); \
  352. list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
  353. } while (0)
  354. #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
  355. do { \
  356. MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
  357. MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
  358. MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
  359. } while (0)
  360. #define CFLGS_OFF_SLAB (0x80000000UL)
  361. #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
  362. #define BATCHREFILL_LIMIT 16
  363. /*
  364. * Optimization question: fewer reaps means less probability for unnessary
  365. * cpucache drain/refill cycles.
  366. *
  367. * OTOH the cpuarrays can contain lots of objects,
  368. * which could lock up otherwise freeable slabs.
  369. */
  370. #define REAPTIMEOUT_CPUC (2*HZ)
  371. #define REAPTIMEOUT_LIST3 (4*HZ)
  372. #if STATS
  373. #define STATS_INC_ACTIVE(x) ((x)->num_active++)
  374. #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
  375. #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
  376. #define STATS_INC_GROWN(x) ((x)->grown++)
  377. #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
  378. #define STATS_SET_HIGH(x) \
  379. do { \
  380. if ((x)->num_active > (x)->high_mark) \
  381. (x)->high_mark = (x)->num_active; \
  382. } while (0)
  383. #define STATS_INC_ERR(x) ((x)->errors++)
  384. #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
  385. #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
  386. #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
  387. #define STATS_SET_FREEABLE(x, i) \
  388. do { \
  389. if ((x)->max_freeable < i) \
  390. (x)->max_freeable = i; \
  391. } while (0)
  392. #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
  393. #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
  394. #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
  395. #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
  396. #else
  397. #define STATS_INC_ACTIVE(x) do { } while (0)
  398. #define STATS_DEC_ACTIVE(x) do { } while (0)
  399. #define STATS_INC_ALLOCED(x) do { } while (0)
  400. #define STATS_INC_GROWN(x) do { } while (0)
  401. #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
  402. #define STATS_SET_HIGH(x) do { } while (0)
  403. #define STATS_INC_ERR(x) do { } while (0)
  404. #define STATS_INC_NODEALLOCS(x) do { } while (0)
  405. #define STATS_INC_NODEFREES(x) do { } while (0)
  406. #define STATS_INC_ACOVERFLOW(x) do { } while (0)
  407. #define STATS_SET_FREEABLE(x, i) do { } while (0)
  408. #define STATS_INC_ALLOCHIT(x) do { } while (0)
  409. #define STATS_INC_ALLOCMISS(x) do { } while (0)
  410. #define STATS_INC_FREEHIT(x) do { } while (0)
  411. #define STATS_INC_FREEMISS(x) do { } while (0)
  412. #endif
  413. #if DEBUG
  414. /*
  415. * memory layout of objects:
  416. * 0 : objp
  417. * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
  418. * the end of an object is aligned with the end of the real
  419. * allocation. Catches writes behind the end of the allocation.
  420. * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
  421. * redzone word.
  422. * cachep->obj_offset: The real object.
  423. * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
  424. * cachep->size - 1* BYTES_PER_WORD: last caller address
  425. * [BYTES_PER_WORD long]
  426. */
  427. static int obj_offset(struct kmem_cache *cachep)
  428. {
  429. return cachep->obj_offset;
  430. }
  431. static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
  432. {
  433. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  434. return (unsigned long long*) (objp + obj_offset(cachep) -
  435. sizeof(unsigned long long));
  436. }
  437. static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
  438. {
  439. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  440. if (cachep->flags & SLAB_STORE_USER)
  441. return (unsigned long long *)(objp + cachep->size -
  442. sizeof(unsigned long long) -
  443. REDZONE_ALIGN);
  444. return (unsigned long long *) (objp + cachep->size -
  445. sizeof(unsigned long long));
  446. }
  447. static void **dbg_userword(struct kmem_cache *cachep, void *objp)
  448. {
  449. BUG_ON(!(cachep->flags & SLAB_STORE_USER));
  450. return (void **)(objp + cachep->size - BYTES_PER_WORD);
  451. }
  452. #else
  453. #define obj_offset(x) 0
  454. #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
  455. #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
  456. #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
  457. #endif
  458. #ifdef CONFIG_TRACING
  459. size_t slab_buffer_size(struct kmem_cache *cachep)
  460. {
  461. return cachep->size;
  462. }
  463. EXPORT_SYMBOL(slab_buffer_size);
  464. #endif
  465. /*
  466. * Do not go above this order unless 0 objects fit into the slab or
  467. * overridden on the command line.
  468. */
  469. #define SLAB_MAX_ORDER_HI 1
  470. #define SLAB_MAX_ORDER_LO 0
  471. static int slab_max_order = SLAB_MAX_ORDER_LO;
  472. static bool slab_max_order_set __initdata;
  473. static inline struct kmem_cache *virt_to_cache(const void *obj)
  474. {
  475. struct page *page = virt_to_head_page(obj);
  476. return page->slab_cache;
  477. }
  478. static inline struct slab *virt_to_slab(const void *obj)
  479. {
  480. struct page *page = virt_to_head_page(obj);
  481. VM_BUG_ON(!PageSlab(page));
  482. return page->slab_page;
  483. }
  484. static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
  485. unsigned int idx)
  486. {
  487. return slab->s_mem + cache->size * idx;
  488. }
  489. /*
  490. * We want to avoid an expensive divide : (offset / cache->size)
  491. * Using the fact that size is a constant for a particular cache,
  492. * we can replace (offset / cache->size) by
  493. * reciprocal_divide(offset, cache->reciprocal_buffer_size)
  494. */
  495. static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  496. const struct slab *slab, void *obj)
  497. {
  498. u32 offset = (obj - slab->s_mem);
  499. return reciprocal_divide(offset, cache->reciprocal_buffer_size);
  500. }
  501. /*
  502. * These are the default caches for kmalloc. Custom caches can have other sizes.
  503. */
  504. struct cache_sizes malloc_sizes[] = {
  505. #define CACHE(x) { .cs_size = (x) },
  506. #include <linux/kmalloc_sizes.h>
  507. CACHE(ULONG_MAX)
  508. #undef CACHE
  509. };
  510. EXPORT_SYMBOL(malloc_sizes);
  511. /* Must match cache_sizes above. Out of line to keep cache footprint low. */
  512. struct cache_names {
  513. char *name;
  514. char *name_dma;
  515. };
  516. static struct cache_names __initdata cache_names[] = {
  517. #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
  518. #include <linux/kmalloc_sizes.h>
  519. {NULL,}
  520. #undef CACHE
  521. };
  522. static struct arraycache_init initarray_cache __initdata =
  523. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  524. static struct arraycache_init initarray_generic =
  525. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  526. /* internal cache of cache description objs */
  527. static struct kmem_list3 *cache_cache_nodelists[MAX_NUMNODES];
  528. static struct kmem_cache cache_cache = {
  529. .nodelists = cache_cache_nodelists,
  530. .batchcount = 1,
  531. .limit = BOOT_CPUCACHE_ENTRIES,
  532. .shared = 1,
  533. .size = sizeof(struct kmem_cache),
  534. .name = "kmem_cache",
  535. };
  536. #define BAD_ALIEN_MAGIC 0x01020304ul
  537. #ifdef CONFIG_LOCKDEP
  538. /*
  539. * Slab sometimes uses the kmalloc slabs to store the slab headers
  540. * for other slabs "off slab".
  541. * The locking for this is tricky in that it nests within the locks
  542. * of all other slabs in a few places; to deal with this special
  543. * locking we put on-slab caches into a separate lock-class.
  544. *
  545. * We set lock class for alien array caches which are up during init.
  546. * The lock annotation will be lost if all cpus of a node goes down and
  547. * then comes back up during hotplug
  548. */
  549. static struct lock_class_key on_slab_l3_key;
  550. static struct lock_class_key on_slab_alc_key;
  551. static struct lock_class_key debugobj_l3_key;
  552. static struct lock_class_key debugobj_alc_key;
  553. static void slab_set_lock_classes(struct kmem_cache *cachep,
  554. struct lock_class_key *l3_key, struct lock_class_key *alc_key,
  555. int q)
  556. {
  557. struct array_cache **alc;
  558. struct kmem_list3 *l3;
  559. int r;
  560. l3 = cachep->nodelists[q];
  561. if (!l3)
  562. return;
  563. lockdep_set_class(&l3->list_lock, l3_key);
  564. alc = l3->alien;
  565. /*
  566. * FIXME: This check for BAD_ALIEN_MAGIC
  567. * should go away when common slab code is taught to
  568. * work even without alien caches.
  569. * Currently, non NUMA code returns BAD_ALIEN_MAGIC
  570. * for alloc_alien_cache,
  571. */
  572. if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
  573. return;
  574. for_each_node(r) {
  575. if (alc[r])
  576. lockdep_set_class(&alc[r]->lock, alc_key);
  577. }
  578. }
  579. static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  580. {
  581. slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
  582. }
  583. static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  584. {
  585. int node;
  586. for_each_online_node(node)
  587. slab_set_debugobj_lock_classes_node(cachep, node);
  588. }
  589. static void init_node_lock_keys(int q)
  590. {
  591. struct cache_sizes *s = malloc_sizes;
  592. if (slab_state < UP)
  593. return;
  594. for (s = malloc_sizes; s->cs_size != ULONG_MAX; s++) {
  595. struct kmem_list3 *l3;
  596. l3 = s->cs_cachep->nodelists[q];
  597. if (!l3 || OFF_SLAB(s->cs_cachep))
  598. continue;
  599. slab_set_lock_classes(s->cs_cachep, &on_slab_l3_key,
  600. &on_slab_alc_key, q);
  601. }
  602. }
  603. static inline void init_lock_keys(void)
  604. {
  605. int node;
  606. for_each_node(node)
  607. init_node_lock_keys(node);
  608. }
  609. #else
  610. static void init_node_lock_keys(int q)
  611. {
  612. }
  613. static inline void init_lock_keys(void)
  614. {
  615. }
  616. static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  617. {
  618. }
  619. static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  620. {
  621. }
  622. #endif
  623. static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
  624. static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
  625. {
  626. return cachep->array[smp_processor_id()];
  627. }
  628. static inline struct kmem_cache *__find_general_cachep(size_t size,
  629. gfp_t gfpflags)
  630. {
  631. struct cache_sizes *csizep = malloc_sizes;
  632. #if DEBUG
  633. /* This happens if someone tries to call
  634. * kmem_cache_create(), or __kmalloc(), before
  635. * the generic caches are initialized.
  636. */
  637. BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
  638. #endif
  639. if (!size)
  640. return ZERO_SIZE_PTR;
  641. while (size > csizep->cs_size)
  642. csizep++;
  643. /*
  644. * Really subtle: The last entry with cs->cs_size==ULONG_MAX
  645. * has cs_{dma,}cachep==NULL. Thus no special case
  646. * for large kmalloc calls required.
  647. */
  648. #ifdef CONFIG_ZONE_DMA
  649. if (unlikely(gfpflags & GFP_DMA))
  650. return csizep->cs_dmacachep;
  651. #endif
  652. return csizep->cs_cachep;
  653. }
  654. static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
  655. {
  656. return __find_general_cachep(size, gfpflags);
  657. }
  658. static size_t slab_mgmt_size(size_t nr_objs, size_t align)
  659. {
  660. return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
  661. }
  662. /*
  663. * Calculate the number of objects and left-over bytes for a given buffer size.
  664. */
  665. static void cache_estimate(unsigned long gfporder, size_t buffer_size,
  666. size_t align, int flags, size_t *left_over,
  667. unsigned int *num)
  668. {
  669. int nr_objs;
  670. size_t mgmt_size;
  671. size_t slab_size = PAGE_SIZE << gfporder;
  672. /*
  673. * The slab management structure can be either off the slab or
  674. * on it. For the latter case, the memory allocated for a
  675. * slab is used for:
  676. *
  677. * - The struct slab
  678. * - One kmem_bufctl_t for each object
  679. * - Padding to respect alignment of @align
  680. * - @buffer_size bytes for each object
  681. *
  682. * If the slab management structure is off the slab, then the
  683. * alignment will already be calculated into the size. Because
  684. * the slabs are all pages aligned, the objects will be at the
  685. * correct alignment when allocated.
  686. */
  687. if (flags & CFLGS_OFF_SLAB) {
  688. mgmt_size = 0;
  689. nr_objs = slab_size / buffer_size;
  690. if (nr_objs > SLAB_LIMIT)
  691. nr_objs = SLAB_LIMIT;
  692. } else {
  693. /*
  694. * Ignore padding for the initial guess. The padding
  695. * is at most @align-1 bytes, and @buffer_size is at
  696. * least @align. In the worst case, this result will
  697. * be one greater than the number of objects that fit
  698. * into the memory allocation when taking the padding
  699. * into account.
  700. */
  701. nr_objs = (slab_size - sizeof(struct slab)) /
  702. (buffer_size + sizeof(kmem_bufctl_t));
  703. /*
  704. * This calculated number will be either the right
  705. * amount, or one greater than what we want.
  706. */
  707. if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
  708. > slab_size)
  709. nr_objs--;
  710. if (nr_objs > SLAB_LIMIT)
  711. nr_objs = SLAB_LIMIT;
  712. mgmt_size = slab_mgmt_size(nr_objs, align);
  713. }
  714. *num = nr_objs;
  715. *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
  716. }
  717. #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
  718. static void __slab_error(const char *function, struct kmem_cache *cachep,
  719. char *msg)
  720. {
  721. printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
  722. function, cachep->name, msg);
  723. dump_stack();
  724. add_taint(TAINT_BAD_PAGE);
  725. }
  726. /*
  727. * By default on NUMA we use alien caches to stage the freeing of
  728. * objects allocated from other nodes. This causes massive memory
  729. * inefficiencies when using fake NUMA setup to split memory into a
  730. * large number of small nodes, so it can be disabled on the command
  731. * line
  732. */
  733. static int use_alien_caches __read_mostly = 1;
  734. static int __init noaliencache_setup(char *s)
  735. {
  736. use_alien_caches = 0;
  737. return 1;
  738. }
  739. __setup("noaliencache", noaliencache_setup);
  740. static int __init slab_max_order_setup(char *str)
  741. {
  742. get_option(&str, &slab_max_order);
  743. slab_max_order = slab_max_order < 0 ? 0 :
  744. min(slab_max_order, MAX_ORDER - 1);
  745. slab_max_order_set = true;
  746. return 1;
  747. }
  748. __setup("slab_max_order=", slab_max_order_setup);
  749. #ifdef CONFIG_NUMA
  750. /*
  751. * Special reaping functions for NUMA systems called from cache_reap().
  752. * These take care of doing round robin flushing of alien caches (containing
  753. * objects freed on different nodes from which they were allocated) and the
  754. * flushing of remote pcps by calling drain_node_pages.
  755. */
  756. static DEFINE_PER_CPU(unsigned long, slab_reap_node);
  757. static void init_reap_node(int cpu)
  758. {
  759. int node;
  760. node = next_node(cpu_to_mem(cpu), node_online_map);
  761. if (node == MAX_NUMNODES)
  762. node = first_node(node_online_map);
  763. per_cpu(slab_reap_node, cpu) = node;
  764. }
  765. static void next_reap_node(void)
  766. {
  767. int node = __this_cpu_read(slab_reap_node);
  768. node = next_node(node, node_online_map);
  769. if (unlikely(node >= MAX_NUMNODES))
  770. node = first_node(node_online_map);
  771. __this_cpu_write(slab_reap_node, node);
  772. }
  773. #else
  774. #define init_reap_node(cpu) do { } while (0)
  775. #define next_reap_node(void) do { } while (0)
  776. #endif
  777. /*
  778. * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
  779. * via the workqueue/eventd.
  780. * Add the CPU number into the expiration time to minimize the possibility of
  781. * the CPUs getting into lockstep and contending for the global cache chain
  782. * lock.
  783. */
  784. static void __cpuinit start_cpu_timer(int cpu)
  785. {
  786. struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
  787. /*
  788. * When this gets called from do_initcalls via cpucache_init(),
  789. * init_workqueues() has already run, so keventd will be setup
  790. * at that time.
  791. */
  792. if (keventd_up() && reap_work->work.func == NULL) {
  793. init_reap_node(cpu);
  794. INIT_DELAYED_WORK_DEFERRABLE(reap_work, cache_reap);
  795. schedule_delayed_work_on(cpu, reap_work,
  796. __round_jiffies_relative(HZ, cpu));
  797. }
  798. }
  799. static struct array_cache *alloc_arraycache(int node, int entries,
  800. int batchcount, gfp_t gfp)
  801. {
  802. int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
  803. struct array_cache *nc = NULL;
  804. nc = kmalloc_node(memsize, gfp, node);
  805. /*
  806. * The array_cache structures contain pointers to free object.
  807. * However, when such objects are allocated or transferred to another
  808. * cache the pointers are not cleared and they could be counted as
  809. * valid references during a kmemleak scan. Therefore, kmemleak must
  810. * not scan such objects.
  811. */
  812. kmemleak_no_scan(nc);
  813. if (nc) {
  814. nc->avail = 0;
  815. nc->limit = entries;
  816. nc->batchcount = batchcount;
  817. nc->touched = 0;
  818. spin_lock_init(&nc->lock);
  819. }
  820. return nc;
  821. }
  822. static inline bool is_slab_pfmemalloc(struct slab *slabp)
  823. {
  824. struct page *page = virt_to_page(slabp->s_mem);
  825. return PageSlabPfmemalloc(page);
  826. }
  827. /* Clears pfmemalloc_active if no slabs have pfmalloc set */
  828. static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
  829. struct array_cache *ac)
  830. {
  831. struct kmem_list3 *l3 = cachep->nodelists[numa_mem_id()];
  832. struct slab *slabp;
  833. unsigned long flags;
  834. if (!pfmemalloc_active)
  835. return;
  836. spin_lock_irqsave(&l3->list_lock, flags);
  837. list_for_each_entry(slabp, &l3->slabs_full, list)
  838. if (is_slab_pfmemalloc(slabp))
  839. goto out;
  840. list_for_each_entry(slabp, &l3->slabs_partial, list)
  841. if (is_slab_pfmemalloc(slabp))
  842. goto out;
  843. list_for_each_entry(slabp, &l3->slabs_free, list)
  844. if (is_slab_pfmemalloc(slabp))
  845. goto out;
  846. pfmemalloc_active = false;
  847. out:
  848. spin_unlock_irqrestore(&l3->list_lock, flags);
  849. }
  850. static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
  851. gfp_t flags, bool force_refill)
  852. {
  853. int i;
  854. void *objp = ac->entry[--ac->avail];
  855. /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
  856. if (unlikely(is_obj_pfmemalloc(objp))) {
  857. struct kmem_list3 *l3;
  858. if (gfp_pfmemalloc_allowed(flags)) {
  859. clear_obj_pfmemalloc(&objp);
  860. return objp;
  861. }
  862. /* The caller cannot use PFMEMALLOC objects, find another one */
  863. for (i = 1; i < ac->avail; i++) {
  864. /* If a !PFMEMALLOC object is found, swap them */
  865. if (!is_obj_pfmemalloc(ac->entry[i])) {
  866. objp = ac->entry[i];
  867. ac->entry[i] = ac->entry[ac->avail];
  868. ac->entry[ac->avail] = objp;
  869. return objp;
  870. }
  871. }
  872. /*
  873. * If there are empty slabs on the slabs_free list and we are
  874. * being forced to refill the cache, mark this one !pfmemalloc.
  875. */
  876. l3 = cachep->nodelists[numa_mem_id()];
  877. if (!list_empty(&l3->slabs_free) && force_refill) {
  878. struct slab *slabp = virt_to_slab(objp);
  879. ClearPageSlabPfmemalloc(virt_to_page(slabp->s_mem));
  880. clear_obj_pfmemalloc(&objp);
  881. recheck_pfmemalloc_active(cachep, ac);
  882. return objp;
  883. }
  884. /* No !PFMEMALLOC objects available */
  885. ac->avail++;
  886. objp = NULL;
  887. }
  888. return objp;
  889. }
  890. static inline void *ac_get_obj(struct kmem_cache *cachep,
  891. struct array_cache *ac, gfp_t flags, bool force_refill)
  892. {
  893. void *objp;
  894. if (unlikely(sk_memalloc_socks()))
  895. objp = __ac_get_obj(cachep, ac, flags, force_refill);
  896. else
  897. objp = ac->entry[--ac->avail];
  898. return objp;
  899. }
  900. static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
  901. void *objp)
  902. {
  903. if (unlikely(pfmemalloc_active)) {
  904. /* Some pfmemalloc slabs exist, check if this is one */
  905. struct page *page = virt_to_page(objp);
  906. if (PageSlabPfmemalloc(page))
  907. set_obj_pfmemalloc(&objp);
  908. }
  909. return objp;
  910. }
  911. static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
  912. void *objp)
  913. {
  914. if (unlikely(sk_memalloc_socks()))
  915. objp = __ac_put_obj(cachep, ac, objp);
  916. ac->entry[ac->avail++] = objp;
  917. }
  918. /*
  919. * Transfer objects in one arraycache to another.
  920. * Locking must be handled by the caller.
  921. *
  922. * Return the number of entries transferred.
  923. */
  924. static int transfer_objects(struct array_cache *to,
  925. struct array_cache *from, unsigned int max)
  926. {
  927. /* Figure out how many entries to transfer */
  928. int nr = min3(from->avail, max, to->limit - to->avail);
  929. if (!nr)
  930. return 0;
  931. memcpy(to->entry + to->avail, from->entry + from->avail -nr,
  932. sizeof(void *) *nr);
  933. from->avail -= nr;
  934. to->avail += nr;
  935. return nr;
  936. }
  937. #ifndef CONFIG_NUMA
  938. #define drain_alien_cache(cachep, alien) do { } while (0)
  939. #define reap_alien(cachep, l3) do { } while (0)
  940. static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
  941. {
  942. return (struct array_cache **)BAD_ALIEN_MAGIC;
  943. }
  944. static inline void free_alien_cache(struct array_cache **ac_ptr)
  945. {
  946. }
  947. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  948. {
  949. return 0;
  950. }
  951. static inline void *alternate_node_alloc(struct kmem_cache *cachep,
  952. gfp_t flags)
  953. {
  954. return NULL;
  955. }
  956. static inline void *____cache_alloc_node(struct kmem_cache *cachep,
  957. gfp_t flags, int nodeid)
  958. {
  959. return NULL;
  960. }
  961. #else /* CONFIG_NUMA */
  962. static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
  963. static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
  964. static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
  965. {
  966. struct array_cache **ac_ptr;
  967. int memsize = sizeof(void *) * nr_node_ids;
  968. int i;
  969. if (limit > 1)
  970. limit = 12;
  971. ac_ptr = kzalloc_node(memsize, gfp, node);
  972. if (ac_ptr) {
  973. for_each_node(i) {
  974. if (i == node || !node_online(i))
  975. continue;
  976. ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
  977. if (!ac_ptr[i]) {
  978. for (i--; i >= 0; i--)
  979. kfree(ac_ptr[i]);
  980. kfree(ac_ptr);
  981. return NULL;
  982. }
  983. }
  984. }
  985. return ac_ptr;
  986. }
  987. static void free_alien_cache(struct array_cache **ac_ptr)
  988. {
  989. int i;
  990. if (!ac_ptr)
  991. return;
  992. for_each_node(i)
  993. kfree(ac_ptr[i]);
  994. kfree(ac_ptr);
  995. }
  996. static void __drain_alien_cache(struct kmem_cache *cachep,
  997. struct array_cache *ac, int node)
  998. {
  999. struct kmem_list3 *rl3 = cachep->nodelists[node];
  1000. if (ac->avail) {
  1001. spin_lock(&rl3->list_lock);
  1002. /*
  1003. * Stuff objects into the remote nodes shared array first.
  1004. * That way we could avoid the overhead of putting the objects
  1005. * into the free lists and getting them back later.
  1006. */
  1007. if (rl3->shared)
  1008. transfer_objects(rl3->shared, ac, ac->limit);
  1009. free_block(cachep, ac->entry, ac->avail, node);
  1010. ac->avail = 0;
  1011. spin_unlock(&rl3->list_lock);
  1012. }
  1013. }
  1014. /*
  1015. * Called from cache_reap() to regularly drain alien caches round robin.
  1016. */
  1017. static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
  1018. {
  1019. int node = __this_cpu_read(slab_reap_node);
  1020. if (l3->alien) {
  1021. struct array_cache *ac = l3->alien[node];
  1022. if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
  1023. __drain_alien_cache(cachep, ac, node);
  1024. spin_unlock_irq(&ac->lock);
  1025. }
  1026. }
  1027. }
  1028. static void drain_alien_cache(struct kmem_cache *cachep,
  1029. struct array_cache **alien)
  1030. {
  1031. int i = 0;
  1032. struct array_cache *ac;
  1033. unsigned long flags;
  1034. for_each_online_node(i) {
  1035. ac = alien[i];
  1036. if (ac) {
  1037. spin_lock_irqsave(&ac->lock, flags);
  1038. __drain_alien_cache(cachep, ac, i);
  1039. spin_unlock_irqrestore(&ac->lock, flags);
  1040. }
  1041. }
  1042. }
  1043. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  1044. {
  1045. struct slab *slabp = virt_to_slab(objp);
  1046. int nodeid = slabp->nodeid;
  1047. struct kmem_list3 *l3;
  1048. struct array_cache *alien = NULL;
  1049. int node;
  1050. node = numa_mem_id();
  1051. /*
  1052. * Make sure we are not freeing a object from another node to the array
  1053. * cache on this cpu.
  1054. */
  1055. if (likely(slabp->nodeid == node))
  1056. return 0;
  1057. l3 = cachep->nodelists[node];
  1058. STATS_INC_NODEFREES(cachep);
  1059. if (l3->alien && l3->alien[nodeid]) {
  1060. alien = l3->alien[nodeid];
  1061. spin_lock(&alien->lock);
  1062. if (unlikely(alien->avail == alien->limit)) {
  1063. STATS_INC_ACOVERFLOW(cachep);
  1064. __drain_alien_cache(cachep, alien, nodeid);
  1065. }
  1066. ac_put_obj(cachep, alien, objp);
  1067. spin_unlock(&alien->lock);
  1068. } else {
  1069. spin_lock(&(cachep->nodelists[nodeid])->list_lock);
  1070. free_block(cachep, &objp, 1, nodeid);
  1071. spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
  1072. }
  1073. return 1;
  1074. }
  1075. #endif
  1076. /*
  1077. * Allocates and initializes nodelists for a node on each slab cache, used for
  1078. * either memory or cpu hotplug. If memory is being hot-added, the kmem_list3
  1079. * will be allocated off-node since memory is not yet online for the new node.
  1080. * When hotplugging memory or a cpu, existing nodelists are not replaced if
  1081. * already in use.
  1082. *
  1083. * Must hold slab_mutex.
  1084. */
  1085. static int init_cache_nodelists_node(int node)
  1086. {
  1087. struct kmem_cache *cachep;
  1088. struct kmem_list3 *l3;
  1089. const int memsize = sizeof(struct kmem_list3);
  1090. list_for_each_entry(cachep, &slab_caches, list) {
  1091. /*
  1092. * Set up the size64 kmemlist for cpu before we can
  1093. * begin anything. Make sure some other cpu on this
  1094. * node has not already allocated this
  1095. */
  1096. if (!cachep->nodelists[node]) {
  1097. l3 = kmalloc_node(memsize, GFP_KERNEL, node);
  1098. if (!l3)
  1099. return -ENOMEM;
  1100. kmem_list3_init(l3);
  1101. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  1102. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1103. /*
  1104. * The l3s don't come and go as CPUs come and
  1105. * go. slab_mutex is sufficient
  1106. * protection here.
  1107. */
  1108. cachep->nodelists[node] = l3;
  1109. }
  1110. spin_lock_irq(&cachep->nodelists[node]->list_lock);
  1111. cachep->nodelists[node]->free_limit =
  1112. (1 + nr_cpus_node(node)) *
  1113. cachep->batchcount + cachep->num;
  1114. spin_unlock_irq(&cachep->nodelists[node]->list_lock);
  1115. }
  1116. return 0;
  1117. }
  1118. static void __cpuinit cpuup_canceled(long cpu)
  1119. {
  1120. struct kmem_cache *cachep;
  1121. struct kmem_list3 *l3 = NULL;
  1122. int node = cpu_to_mem(cpu);
  1123. const struct cpumask *mask = cpumask_of_node(node);
  1124. list_for_each_entry(cachep, &slab_caches, list) {
  1125. struct array_cache *nc;
  1126. struct array_cache *shared;
  1127. struct array_cache **alien;
  1128. /* cpu is dead; no one can alloc from it. */
  1129. nc = cachep->array[cpu];
  1130. cachep->array[cpu] = NULL;
  1131. l3 = cachep->nodelists[node];
  1132. if (!l3)
  1133. goto free_array_cache;
  1134. spin_lock_irq(&l3->list_lock);
  1135. /* Free limit for this kmem_list3 */
  1136. l3->free_limit -= cachep->batchcount;
  1137. if (nc)
  1138. free_block(cachep, nc->entry, nc->avail, node);
  1139. if (!cpumask_empty(mask)) {
  1140. spin_unlock_irq(&l3->list_lock);
  1141. goto free_array_cache;
  1142. }
  1143. shared = l3->shared;
  1144. if (shared) {
  1145. free_block(cachep, shared->entry,
  1146. shared->avail, node);
  1147. l3->shared = NULL;
  1148. }
  1149. alien = l3->alien;
  1150. l3->alien = NULL;
  1151. spin_unlock_irq(&l3->list_lock);
  1152. kfree(shared);
  1153. if (alien) {
  1154. drain_alien_cache(cachep, alien);
  1155. free_alien_cache(alien);
  1156. }
  1157. free_array_cache:
  1158. kfree(nc);
  1159. }
  1160. /*
  1161. * In the previous loop, all the objects were freed to
  1162. * the respective cache's slabs, now we can go ahead and
  1163. * shrink each nodelist to its limit.
  1164. */
  1165. list_for_each_entry(cachep, &slab_caches, list) {
  1166. l3 = cachep->nodelists[node];
  1167. if (!l3)
  1168. continue;
  1169. drain_freelist(cachep, l3, l3->free_objects);
  1170. }
  1171. }
  1172. static int __cpuinit cpuup_prepare(long cpu)
  1173. {
  1174. struct kmem_cache *cachep;
  1175. struct kmem_list3 *l3 = NULL;
  1176. int node = cpu_to_mem(cpu);
  1177. int err;
  1178. /*
  1179. * We need to do this right in the beginning since
  1180. * alloc_arraycache's are going to use this list.
  1181. * kmalloc_node allows us to add the slab to the right
  1182. * kmem_list3 and not this cpu's kmem_list3
  1183. */
  1184. err = init_cache_nodelists_node(node);
  1185. if (err < 0)
  1186. goto bad;
  1187. /*
  1188. * Now we can go ahead with allocating the shared arrays and
  1189. * array caches
  1190. */
  1191. list_for_each_entry(cachep, &slab_caches, list) {
  1192. struct array_cache *nc;
  1193. struct array_cache *shared = NULL;
  1194. struct array_cache **alien = NULL;
  1195. nc = alloc_arraycache(node, cachep->limit,
  1196. cachep->batchcount, GFP_KERNEL);
  1197. if (!nc)
  1198. goto bad;
  1199. if (cachep->shared) {
  1200. shared = alloc_arraycache(node,
  1201. cachep->shared * cachep->batchcount,
  1202. 0xbaadf00d, GFP_KERNEL);
  1203. if (!shared) {
  1204. kfree(nc);
  1205. goto bad;
  1206. }
  1207. }
  1208. if (use_alien_caches) {
  1209. alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
  1210. if (!alien) {
  1211. kfree(shared);
  1212. kfree(nc);
  1213. goto bad;
  1214. }
  1215. }
  1216. cachep->array[cpu] = nc;
  1217. l3 = cachep->nodelists[node];
  1218. BUG_ON(!l3);
  1219. spin_lock_irq(&l3->list_lock);
  1220. if (!l3->shared) {
  1221. /*
  1222. * We are serialised from CPU_DEAD or
  1223. * CPU_UP_CANCELLED by the cpucontrol lock
  1224. */
  1225. l3->shared = shared;
  1226. shared = NULL;
  1227. }
  1228. #ifdef CONFIG_NUMA
  1229. if (!l3->alien) {
  1230. l3->alien = alien;
  1231. alien = NULL;
  1232. }
  1233. #endif
  1234. spin_unlock_irq(&l3->list_lock);
  1235. kfree(shared);
  1236. free_alien_cache(alien);
  1237. if (cachep->flags & SLAB_DEBUG_OBJECTS)
  1238. slab_set_debugobj_lock_classes_node(cachep, node);
  1239. }
  1240. init_node_lock_keys(node);
  1241. return 0;
  1242. bad:
  1243. cpuup_canceled(cpu);
  1244. return -ENOMEM;
  1245. }
  1246. static int __cpuinit cpuup_callback(struct notifier_block *nfb,
  1247. unsigned long action, void *hcpu)
  1248. {
  1249. long cpu = (long)hcpu;
  1250. int err = 0;
  1251. switch (action) {
  1252. case CPU_UP_PREPARE:
  1253. case CPU_UP_PREPARE_FROZEN:
  1254. mutex_lock(&slab_mutex);
  1255. err = cpuup_prepare(cpu);
  1256. mutex_unlock(&slab_mutex);
  1257. break;
  1258. case CPU_ONLINE:
  1259. case CPU_ONLINE_FROZEN:
  1260. start_cpu_timer(cpu);
  1261. break;
  1262. #ifdef CONFIG_HOTPLUG_CPU
  1263. case CPU_DOWN_PREPARE:
  1264. case CPU_DOWN_PREPARE_FROZEN:
  1265. /*
  1266. * Shutdown cache reaper. Note that the slab_mutex is
  1267. * held so that if cache_reap() is invoked it cannot do
  1268. * anything expensive but will only modify reap_work
  1269. * and reschedule the timer.
  1270. */
  1271. cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
  1272. /* Now the cache_reaper is guaranteed to be not running. */
  1273. per_cpu(slab_reap_work, cpu).work.func = NULL;
  1274. break;
  1275. case CPU_DOWN_FAILED:
  1276. case CPU_DOWN_FAILED_FROZEN:
  1277. start_cpu_timer(cpu);
  1278. break;
  1279. case CPU_DEAD:
  1280. case CPU_DEAD_FROZEN:
  1281. /*
  1282. * Even if all the cpus of a node are down, we don't free the
  1283. * kmem_list3 of any cache. This to avoid a race between
  1284. * cpu_down, and a kmalloc allocation from another cpu for
  1285. * memory from the node of the cpu going down. The list3
  1286. * structure is usually allocated from kmem_cache_create() and
  1287. * gets destroyed at kmem_cache_destroy().
  1288. */
  1289. /* fall through */
  1290. #endif
  1291. case CPU_UP_CANCELED:
  1292. case CPU_UP_CANCELED_FROZEN:
  1293. mutex_lock(&slab_mutex);
  1294. cpuup_canceled(cpu);
  1295. mutex_unlock(&slab_mutex);
  1296. break;
  1297. }
  1298. return notifier_from_errno(err);
  1299. }
  1300. static struct notifier_block __cpuinitdata cpucache_notifier = {
  1301. &cpuup_callback, NULL, 0
  1302. };
  1303. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  1304. /*
  1305. * Drains freelist for a node on each slab cache, used for memory hot-remove.
  1306. * Returns -EBUSY if all objects cannot be drained so that the node is not
  1307. * removed.
  1308. *
  1309. * Must hold slab_mutex.
  1310. */
  1311. static int __meminit drain_cache_nodelists_node(int node)
  1312. {
  1313. struct kmem_cache *cachep;
  1314. int ret = 0;
  1315. list_for_each_entry(cachep, &slab_caches, list) {
  1316. struct kmem_list3 *l3;
  1317. l3 = cachep->nodelists[node];
  1318. if (!l3)
  1319. continue;
  1320. drain_freelist(cachep, l3, l3->free_objects);
  1321. if (!list_empty(&l3->slabs_full) ||
  1322. !list_empty(&l3->slabs_partial)) {
  1323. ret = -EBUSY;
  1324. break;
  1325. }
  1326. }
  1327. return ret;
  1328. }
  1329. static int __meminit slab_memory_callback(struct notifier_block *self,
  1330. unsigned long action, void *arg)
  1331. {
  1332. struct memory_notify *mnb = arg;
  1333. int ret = 0;
  1334. int nid;
  1335. nid = mnb->status_change_nid;
  1336. if (nid < 0)
  1337. goto out;
  1338. switch (action) {
  1339. case MEM_GOING_ONLINE:
  1340. mutex_lock(&slab_mutex);
  1341. ret = init_cache_nodelists_node(nid);
  1342. mutex_unlock(&slab_mutex);
  1343. break;
  1344. case MEM_GOING_OFFLINE:
  1345. mutex_lock(&slab_mutex);
  1346. ret = drain_cache_nodelists_node(nid);
  1347. mutex_unlock(&slab_mutex);
  1348. break;
  1349. case MEM_ONLINE:
  1350. case MEM_OFFLINE:
  1351. case MEM_CANCEL_ONLINE:
  1352. case MEM_CANCEL_OFFLINE:
  1353. break;
  1354. }
  1355. out:
  1356. return notifier_from_errno(ret);
  1357. }
  1358. #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
  1359. /*
  1360. * swap the static kmem_list3 with kmalloced memory
  1361. */
  1362. static void __init init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
  1363. int nodeid)
  1364. {
  1365. struct kmem_list3 *ptr;
  1366. ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_NOWAIT, nodeid);
  1367. BUG_ON(!ptr);
  1368. memcpy(ptr, list, sizeof(struct kmem_list3));
  1369. /*
  1370. * Do not assume that spinlocks can be initialized via memcpy:
  1371. */
  1372. spin_lock_init(&ptr->list_lock);
  1373. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  1374. cachep->nodelists[nodeid] = ptr;
  1375. }
  1376. /*
  1377. * For setting up all the kmem_list3s for cache whose buffer_size is same as
  1378. * size of kmem_list3.
  1379. */
  1380. static void __init set_up_list3s(struct kmem_cache *cachep, int index)
  1381. {
  1382. int node;
  1383. for_each_online_node(node) {
  1384. cachep->nodelists[node] = &initkmem_list3[index + node];
  1385. cachep->nodelists[node]->next_reap = jiffies +
  1386. REAPTIMEOUT_LIST3 +
  1387. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1388. }
  1389. }
  1390. /*
  1391. * Initialisation. Called after the page allocator have been initialised and
  1392. * before smp_init().
  1393. */
  1394. void __init kmem_cache_init(void)
  1395. {
  1396. size_t left_over;
  1397. struct cache_sizes *sizes;
  1398. struct cache_names *names;
  1399. int i;
  1400. int order;
  1401. int node;
  1402. if (num_possible_nodes() == 1)
  1403. use_alien_caches = 0;
  1404. for (i = 0; i < NUM_INIT_LISTS; i++) {
  1405. kmem_list3_init(&initkmem_list3[i]);
  1406. if (i < MAX_NUMNODES)
  1407. cache_cache.nodelists[i] = NULL;
  1408. }
  1409. set_up_list3s(&cache_cache, CACHE_CACHE);
  1410. /*
  1411. * Fragmentation resistance on low memory - only use bigger
  1412. * page orders on machines with more than 32MB of memory if
  1413. * not overridden on the command line.
  1414. */
  1415. if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
  1416. slab_max_order = SLAB_MAX_ORDER_HI;
  1417. /* Bootstrap is tricky, because several objects are allocated
  1418. * from caches that do not exist yet:
  1419. * 1) initialize the cache_cache cache: it contains the struct
  1420. * kmem_cache structures of all caches, except cache_cache itself:
  1421. * cache_cache is statically allocated.
  1422. * Initially an __init data area is used for the head array and the
  1423. * kmem_list3 structures, it's replaced with a kmalloc allocated
  1424. * array at the end of the bootstrap.
  1425. * 2) Create the first kmalloc cache.
  1426. * The struct kmem_cache for the new cache is allocated normally.
  1427. * An __init data area is used for the head array.
  1428. * 3) Create the remaining kmalloc caches, with minimally sized
  1429. * head arrays.
  1430. * 4) Replace the __init data head arrays for cache_cache and the first
  1431. * kmalloc cache with kmalloc allocated arrays.
  1432. * 5) Replace the __init data for kmem_list3 for cache_cache and
  1433. * the other cache's with kmalloc allocated memory.
  1434. * 6) Resize the head arrays of the kmalloc caches to their final sizes.
  1435. */
  1436. node = numa_mem_id();
  1437. /* 1) create the cache_cache */
  1438. INIT_LIST_HEAD(&slab_caches);
  1439. list_add(&cache_cache.list, &slab_caches);
  1440. cache_cache.colour_off = cache_line_size();
  1441. cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
  1442. cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE + node];
  1443. /*
  1444. * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
  1445. */
  1446. cache_cache.size = offsetof(struct kmem_cache, array[nr_cpu_ids]) +
  1447. nr_node_ids * sizeof(struct kmem_list3 *);
  1448. cache_cache.object_size = cache_cache.size;
  1449. cache_cache.size = ALIGN(cache_cache.size,
  1450. cache_line_size());
  1451. cache_cache.reciprocal_buffer_size =
  1452. reciprocal_value(cache_cache.size);
  1453. for (order = 0; order < MAX_ORDER; order++) {
  1454. cache_estimate(order, cache_cache.size,
  1455. cache_line_size(), 0, &left_over, &cache_cache.num);
  1456. if (cache_cache.num)
  1457. break;
  1458. }
  1459. BUG_ON(!cache_cache.num);
  1460. cache_cache.gfporder = order;
  1461. cache_cache.colour = left_over / cache_cache.colour_off;
  1462. cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
  1463. sizeof(struct slab), cache_line_size());
  1464. /* 2+3) create the kmalloc caches */
  1465. sizes = malloc_sizes;
  1466. names = cache_names;
  1467. /*
  1468. * Initialize the caches that provide memory for the array cache and the
  1469. * kmem_list3 structures first. Without this, further allocations will
  1470. * bug.
  1471. */
  1472. sizes[INDEX_AC].cs_cachep = __kmem_cache_create(names[INDEX_AC].name,
  1473. sizes[INDEX_AC].cs_size,
  1474. ARCH_KMALLOC_MINALIGN,
  1475. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1476. NULL);
  1477. if (INDEX_AC != INDEX_L3) {
  1478. sizes[INDEX_L3].cs_cachep =
  1479. __kmem_cache_create(names[INDEX_L3].name,
  1480. sizes[INDEX_L3].cs_size,
  1481. ARCH_KMALLOC_MINALIGN,
  1482. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1483. NULL);
  1484. }
  1485. slab_early_init = 0;
  1486. while (sizes->cs_size != ULONG_MAX) {
  1487. /*
  1488. * For performance, all the general caches are L1 aligned.
  1489. * This should be particularly beneficial on SMP boxes, as it
  1490. * eliminates "false sharing".
  1491. * Note for systems short on memory removing the alignment will
  1492. * allow tighter packing of the smaller caches.
  1493. */
  1494. if (!sizes->cs_cachep) {
  1495. sizes->cs_cachep = __kmem_cache_create(names->name,
  1496. sizes->cs_size,
  1497. ARCH_KMALLOC_MINALIGN,
  1498. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1499. NULL);
  1500. }
  1501. #ifdef CONFIG_ZONE_DMA
  1502. sizes->cs_dmacachep = __kmem_cache_create(
  1503. names->name_dma,
  1504. sizes->cs_size,
  1505. ARCH_KMALLOC_MINALIGN,
  1506. ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
  1507. SLAB_PANIC,
  1508. NULL);
  1509. #endif
  1510. sizes++;
  1511. names++;
  1512. }
  1513. /* 4) Replace the bootstrap head arrays */
  1514. {
  1515. struct array_cache *ptr;
  1516. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1517. BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
  1518. memcpy(ptr, cpu_cache_get(&cache_cache),
  1519. sizeof(struct arraycache_init));
  1520. /*
  1521. * Do not assume that spinlocks can be initialized via memcpy:
  1522. */
  1523. spin_lock_init(&ptr->lock);
  1524. cache_cache.array[smp_processor_id()] = ptr;
  1525. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1526. BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
  1527. != &initarray_generic.cache);
  1528. memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
  1529. sizeof(struct arraycache_init));
  1530. /*
  1531. * Do not assume that spinlocks can be initialized via memcpy:
  1532. */
  1533. spin_lock_init(&ptr->lock);
  1534. malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
  1535. ptr;
  1536. }
  1537. /* 5) Replace the bootstrap kmem_list3's */
  1538. {
  1539. int nid;
  1540. for_each_online_node(nid) {
  1541. init_list(&cache_cache, &initkmem_list3[CACHE_CACHE + nid], nid);
  1542. init_list(malloc_sizes[INDEX_AC].cs_cachep,
  1543. &initkmem_list3[SIZE_AC + nid], nid);
  1544. if (INDEX_AC != INDEX_L3) {
  1545. init_list(malloc_sizes[INDEX_L3].cs_cachep,
  1546. &initkmem_list3[SIZE_L3 + nid], nid);
  1547. }
  1548. }
  1549. }
  1550. slab_state = UP;
  1551. }
  1552. void __init kmem_cache_init_late(void)
  1553. {
  1554. struct kmem_cache *cachep;
  1555. slab_state = UP;
  1556. /* 6) resize the head arrays to their final sizes */
  1557. mutex_lock(&slab_mutex);
  1558. list_for_each_entry(cachep, &slab_caches, list)
  1559. if (enable_cpucache(cachep, GFP_NOWAIT))
  1560. BUG();
  1561. mutex_unlock(&slab_mutex);
  1562. /* Annotate slab for lockdep -- annotate the malloc caches */
  1563. init_lock_keys();
  1564. /* Done! */
  1565. slab_state = FULL;
  1566. /*
  1567. * Register a cpu startup notifier callback that initializes
  1568. * cpu_cache_get for all new cpus
  1569. */
  1570. register_cpu_notifier(&cpucache_notifier);
  1571. #ifdef CONFIG_NUMA
  1572. /*
  1573. * Register a memory hotplug callback that initializes and frees
  1574. * nodelists.
  1575. */
  1576. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  1577. #endif
  1578. /*
  1579. * The reap timers are started later, with a module init call: That part
  1580. * of the kernel is not yet operational.
  1581. */
  1582. }
  1583. static int __init cpucache_init(void)
  1584. {
  1585. int cpu;
  1586. /*
  1587. * Register the timers that return unneeded pages to the page allocator
  1588. */
  1589. for_each_online_cpu(cpu)
  1590. start_cpu_timer(cpu);
  1591. /* Done! */
  1592. slab_state = FULL;
  1593. return 0;
  1594. }
  1595. __initcall(cpucache_init);
  1596. static noinline void
  1597. slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
  1598. {
  1599. struct kmem_list3 *l3;
  1600. struct slab *slabp;
  1601. unsigned long flags;
  1602. int node;
  1603. printk(KERN_WARNING
  1604. "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1605. nodeid, gfpflags);
  1606. printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
  1607. cachep->name, cachep->size, cachep->gfporder);
  1608. for_each_online_node(node) {
  1609. unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
  1610. unsigned long active_slabs = 0, num_slabs = 0;
  1611. l3 = cachep->nodelists[node];
  1612. if (!l3)
  1613. continue;
  1614. spin_lock_irqsave(&l3->list_lock, flags);
  1615. list_for_each_entry(slabp, &l3->slabs_full, list) {
  1616. active_objs += cachep->num;
  1617. active_slabs++;
  1618. }
  1619. list_for_each_entry(slabp, &l3->slabs_partial, list) {
  1620. active_objs += slabp->inuse;
  1621. active_slabs++;
  1622. }
  1623. list_for_each_entry(slabp, &l3->slabs_free, list)
  1624. num_slabs++;
  1625. free_objects += l3->free_objects;
  1626. spin_unlock_irqrestore(&l3->list_lock, flags);
  1627. num_slabs += active_slabs;
  1628. num_objs = num_slabs * cachep->num;
  1629. printk(KERN_WARNING
  1630. " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
  1631. node, active_slabs, num_slabs, active_objs, num_objs,
  1632. free_objects);
  1633. }
  1634. }
  1635. /*
  1636. * Interface to system's page allocator. No need to hold the cache-lock.
  1637. *
  1638. * If we requested dmaable memory, we will get it. Even if we
  1639. * did not request dmaable memory, we might get it, but that
  1640. * would be relatively rare and ignorable.
  1641. */
  1642. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  1643. {
  1644. struct page *page;
  1645. int nr_pages;
  1646. int i;
  1647. #ifndef CONFIG_MMU
  1648. /*
  1649. * Nommu uses slab's for process anonymous memory allocations, and thus
  1650. * requires __GFP_COMP to properly refcount higher order allocations
  1651. */
  1652. flags |= __GFP_COMP;
  1653. #endif
  1654. flags |= cachep->allocflags;
  1655. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1656. flags |= __GFP_RECLAIMABLE;
  1657. page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
  1658. if (!page) {
  1659. if (!(flags & __GFP_NOWARN) && printk_ratelimit())
  1660. slab_out_of_memory(cachep, flags, nodeid);
  1661. return NULL;
  1662. }
  1663. /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
  1664. if (unlikely(page->pfmemalloc))
  1665. pfmemalloc_active = true;
  1666. nr_pages = (1 << cachep->gfporder);
  1667. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1668. add_zone_page_state(page_zone(page),
  1669. NR_SLAB_RECLAIMABLE, nr_pages);
  1670. else
  1671. add_zone_page_state(page_zone(page),
  1672. NR_SLAB_UNRECLAIMABLE, nr_pages);
  1673. for (i = 0; i < nr_pages; i++) {
  1674. __SetPageSlab(page + i);
  1675. if (page->pfmemalloc)
  1676. SetPageSlabPfmemalloc(page + i);
  1677. }
  1678. if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
  1679. kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
  1680. if (cachep->ctor)
  1681. kmemcheck_mark_uninitialized_pages(page, nr_pages);
  1682. else
  1683. kmemcheck_mark_unallocated_pages(page, nr_pages);
  1684. }
  1685. return page_address(page);
  1686. }
  1687. /*
  1688. * Interface to system's page release.
  1689. */
  1690. static void kmem_freepages(struct kmem_cache *cachep, void *addr)
  1691. {
  1692. unsigned long i = (1 << cachep->gfporder);
  1693. struct page *page = virt_to_page(addr);
  1694. const unsigned long nr_freed = i;
  1695. kmemcheck_free_shadow(page, cachep->gfporder);
  1696. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1697. sub_zone_page_state(page_zone(page),
  1698. NR_SLAB_RECLAIMABLE, nr_freed);
  1699. else
  1700. sub_zone_page_state(page_zone(page),
  1701. NR_SLAB_UNRECLAIMABLE, nr_freed);
  1702. while (i--) {
  1703. BUG_ON(!PageSlab(page));
  1704. __ClearPageSlabPfmemalloc(page);
  1705. __ClearPageSlab(page);
  1706. page++;
  1707. }
  1708. if (current->reclaim_state)
  1709. current->reclaim_state->reclaimed_slab += nr_freed;
  1710. free_pages((unsigned long)addr, cachep->gfporder);
  1711. }
  1712. static void kmem_rcu_free(struct rcu_head *head)
  1713. {
  1714. struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
  1715. struct kmem_cache *cachep = slab_rcu->cachep;
  1716. kmem_freepages(cachep, slab_rcu->addr);
  1717. if (OFF_SLAB(cachep))
  1718. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1719. }
  1720. #if DEBUG
  1721. #ifdef CONFIG_DEBUG_PAGEALLOC
  1722. static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
  1723. unsigned long caller)
  1724. {
  1725. int size = cachep->object_size;
  1726. addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
  1727. if (size < 5 * sizeof(unsigned long))
  1728. return;
  1729. *addr++ = 0x12345678;
  1730. *addr++ = caller;
  1731. *addr++ = smp_processor_id();
  1732. size -= 3 * sizeof(unsigned long);
  1733. {
  1734. unsigned long *sptr = &caller;
  1735. unsigned long svalue;
  1736. while (!kstack_end(sptr)) {
  1737. svalue = *sptr++;
  1738. if (kernel_text_address(svalue)) {
  1739. *addr++ = svalue;
  1740. size -= sizeof(unsigned long);
  1741. if (size <= sizeof(unsigned long))
  1742. break;
  1743. }
  1744. }
  1745. }
  1746. *addr++ = 0x87654321;
  1747. }
  1748. #endif
  1749. static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
  1750. {
  1751. int size = cachep->object_size;
  1752. addr = &((char *)addr)[obj_offset(cachep)];
  1753. memset(addr, val, size);
  1754. *(unsigned char *)(addr + size - 1) = POISON_END;
  1755. }
  1756. static void dump_line(char *data, int offset, int limit)
  1757. {
  1758. int i;
  1759. unsigned char error = 0;
  1760. int bad_count = 0;
  1761. printk(KERN_ERR "%03x: ", offset);
  1762. for (i = 0; i < limit; i++) {
  1763. if (data[offset + i] != POISON_FREE) {
  1764. error = data[offset + i];
  1765. bad_count++;
  1766. }
  1767. }
  1768. print_hex_dump(KERN_CONT, "", 0, 16, 1,
  1769. &data[offset], limit, 1);
  1770. if (bad_count == 1) {
  1771. error ^= POISON_FREE;
  1772. if (!(error & (error - 1))) {
  1773. printk(KERN_ERR "Single bit error detected. Probably "
  1774. "bad RAM.\n");
  1775. #ifdef CONFIG_X86
  1776. printk(KERN_ERR "Run memtest86+ or a similar memory "
  1777. "test tool.\n");
  1778. #else
  1779. printk(KERN_ERR "Run a memory test tool.\n");
  1780. #endif
  1781. }
  1782. }
  1783. }
  1784. #endif
  1785. #if DEBUG
  1786. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1787. {
  1788. int i, size;
  1789. char *realobj;
  1790. if (cachep->flags & SLAB_RED_ZONE) {
  1791. printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
  1792. *dbg_redzone1(cachep, objp),
  1793. *dbg_redzone2(cachep, objp));
  1794. }
  1795. if (cachep->flags & SLAB_STORE_USER) {
  1796. printk(KERN_ERR "Last user: [<%p>]",
  1797. *dbg_userword(cachep, objp));
  1798. print_symbol("(%s)",
  1799. (unsigned long)*dbg_userword(cachep, objp));
  1800. printk("\n");
  1801. }
  1802. realobj = (char *)objp + obj_offset(cachep);
  1803. size = cachep->object_size;
  1804. for (i = 0; i < size && lines; i += 16, lines--) {
  1805. int limit;
  1806. limit = 16;
  1807. if (i + limit > size)
  1808. limit = size - i;
  1809. dump_line(realobj, i, limit);
  1810. }
  1811. }
  1812. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1813. {
  1814. char *realobj;
  1815. int size, i;
  1816. int lines = 0;
  1817. realobj = (char *)objp + obj_offset(cachep);
  1818. size = cachep->object_size;
  1819. for (i = 0; i < size; i++) {
  1820. char exp = POISON_FREE;
  1821. if (i == size - 1)
  1822. exp = POISON_END;
  1823. if (realobj[i] != exp) {
  1824. int limit;
  1825. /* Mismatch ! */
  1826. /* Print header */
  1827. if (lines == 0) {
  1828. printk(KERN_ERR
  1829. "Slab corruption (%s): %s start=%p, len=%d\n",
  1830. print_tainted(), cachep->name, realobj, size);
  1831. print_objinfo(cachep, objp, 0);
  1832. }
  1833. /* Hexdump the affected line */
  1834. i = (i / 16) * 16;
  1835. limit = 16;
  1836. if (i + limit > size)
  1837. limit = size - i;
  1838. dump_line(realobj, i, limit);
  1839. i += 16;
  1840. lines++;
  1841. /* Limit to 5 lines */
  1842. if (lines > 5)
  1843. break;
  1844. }
  1845. }
  1846. if (lines != 0) {
  1847. /* Print some data about the neighboring objects, if they
  1848. * exist:
  1849. */
  1850. struct slab *slabp = virt_to_slab(objp);
  1851. unsigned int objnr;
  1852. objnr = obj_to_index(cachep, slabp, objp);
  1853. if (objnr) {
  1854. objp = index_to_obj(cachep, slabp, objnr - 1);
  1855. realobj = (char *)objp + obj_offset(cachep);
  1856. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1857. realobj, size);
  1858. print_objinfo(cachep, objp, 2);
  1859. }
  1860. if (objnr + 1 < cachep->num) {
  1861. objp = index_to_obj(cachep, slabp, objnr + 1);
  1862. realobj = (char *)objp + obj_offset(cachep);
  1863. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1864. realobj, size);
  1865. print_objinfo(cachep, objp, 2);
  1866. }
  1867. }
  1868. }
  1869. #endif
  1870. #if DEBUG
  1871. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1872. {
  1873. int i;
  1874. for (i = 0; i < cachep->num; i++) {
  1875. void *objp = index_to_obj(cachep, slabp, i);
  1876. if (cachep->flags & SLAB_POISON) {
  1877. #ifdef CONFIG_DEBUG_PAGEALLOC
  1878. if (cachep->size % PAGE_SIZE == 0 &&
  1879. OFF_SLAB(cachep))
  1880. kernel_map_pages(virt_to_page(objp),
  1881. cachep->size / PAGE_SIZE, 1);
  1882. else
  1883. check_poison_obj(cachep, objp);
  1884. #else
  1885. check_poison_obj(cachep, objp);
  1886. #endif
  1887. }
  1888. if (cachep->flags & SLAB_RED_ZONE) {
  1889. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1890. slab_error(cachep, "start of a freed object "
  1891. "was overwritten");
  1892. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1893. slab_error(cachep, "end of a freed object "
  1894. "was overwritten");
  1895. }
  1896. }
  1897. }
  1898. #else
  1899. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1900. {
  1901. }
  1902. #endif
  1903. /**
  1904. * slab_destroy - destroy and release all objects in a slab
  1905. * @cachep: cache pointer being destroyed
  1906. * @slabp: slab pointer being destroyed
  1907. *
  1908. * Destroy all the objs in a slab, and release the mem back to the system.
  1909. * Before calling the slab must have been unlinked from the cache. The
  1910. * cache-lock is not held/needed.
  1911. */
  1912. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
  1913. {
  1914. void *addr = slabp->s_mem - slabp->colouroff;
  1915. slab_destroy_debugcheck(cachep, slabp);
  1916. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1917. struct slab_rcu *slab_rcu;
  1918. slab_rcu = (struct slab_rcu *)slabp;
  1919. slab_rcu->cachep = cachep;
  1920. slab_rcu->addr = addr;
  1921. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1922. } else {
  1923. kmem_freepages(cachep, addr);
  1924. if (OFF_SLAB(cachep))
  1925. kmem_cache_free(cachep->slabp_cache, slabp);
  1926. }
  1927. }
  1928. static void __kmem_cache_destroy(struct kmem_cache *cachep)
  1929. {
  1930. int i;
  1931. struct kmem_list3 *l3;
  1932. for_each_online_cpu(i)
  1933. kfree(cachep->array[i]);
  1934. /* NUMA: free the list3 structures */
  1935. for_each_online_node(i) {
  1936. l3 = cachep->nodelists[i];
  1937. if (l3) {
  1938. kfree(l3->shared);
  1939. free_alien_cache(l3->alien);
  1940. kfree(l3);
  1941. }
  1942. }
  1943. kmem_cache_free(&cache_cache, cachep);
  1944. }
  1945. /**
  1946. * calculate_slab_order - calculate size (page order) of slabs
  1947. * @cachep: pointer to the cache that is being created
  1948. * @size: size of objects to be created in this cache.
  1949. * @align: required alignment for the objects.
  1950. * @flags: slab allocation flags
  1951. *
  1952. * Also calculates the number of objects per slab.
  1953. *
  1954. * This could be made much more intelligent. For now, try to avoid using
  1955. * high order pages for slabs. When the gfp() functions are more friendly
  1956. * towards high-order requests, this should be changed.
  1957. */
  1958. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1959. size_t size, size_t align, unsigned long flags)
  1960. {
  1961. unsigned long offslab_limit;
  1962. size_t left_over = 0;
  1963. int gfporder;
  1964. for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
  1965. unsigned int num;
  1966. size_t remainder;
  1967. cache_estimate(gfporder, size, align, flags, &remainder, &num);
  1968. if (!num)
  1969. continue;
  1970. if (flags & CFLGS_OFF_SLAB) {
  1971. /*
  1972. * Max number of objs-per-slab for caches which
  1973. * use off-slab slabs. Needed to avoid a possible
  1974. * looping condition in cache_grow().
  1975. */
  1976. offslab_limit = size - sizeof(struct slab);
  1977. offslab_limit /= sizeof(kmem_bufctl_t);
  1978. if (num > offslab_limit)
  1979. break;
  1980. }
  1981. /* Found something acceptable - save it away */
  1982. cachep->num = num;
  1983. cachep->gfporder = gfporder;
  1984. left_over = remainder;
  1985. /*
  1986. * A VFS-reclaimable slab tends to have most allocations
  1987. * as GFP_NOFS and we really don't want to have to be allocating
  1988. * higher-order pages when we are unable to shrink dcache.
  1989. */
  1990. if (flags & SLAB_RECLAIM_ACCOUNT)
  1991. break;
  1992. /*
  1993. * Large number of objects is good, but very large slabs are
  1994. * currently bad for the gfp()s.
  1995. */
  1996. if (gfporder >= slab_max_order)
  1997. break;
  1998. /*
  1999. * Acceptable internal fragmentation?
  2000. */
  2001. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  2002. break;
  2003. }
  2004. return left_over;
  2005. }
  2006. static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
  2007. {
  2008. if (slab_state >= FULL)
  2009. return enable_cpucache(cachep, gfp);
  2010. if (slab_state == DOWN) {
  2011. /*
  2012. * Note: the first kmem_cache_create must create the cache
  2013. * that's used by kmalloc(24), otherwise the creation of
  2014. * further caches will BUG().
  2015. */
  2016. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  2017. /*
  2018. * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
  2019. * the first cache, then we need to set up all its list3s,
  2020. * otherwise the creation of further caches will BUG().
  2021. */
  2022. set_up_list3s(cachep, SIZE_AC);
  2023. if (INDEX_AC == INDEX_L3)
  2024. slab_state = PARTIAL_L3;
  2025. else
  2026. slab_state = PARTIAL_ARRAYCACHE;
  2027. } else {
  2028. cachep->array[smp_processor_id()] =
  2029. kmalloc(sizeof(struct arraycache_init), gfp);
  2030. if (slab_state == PARTIAL_ARRAYCACHE) {
  2031. set_up_list3s(cachep, SIZE_L3);
  2032. slab_state = PARTIAL_L3;
  2033. } else {
  2034. int node;
  2035. for_each_online_node(node) {
  2036. cachep->nodelists[node] =
  2037. kmalloc_node(sizeof(struct kmem_list3),
  2038. gfp, node);
  2039. BUG_ON(!cachep->nodelists[node]);
  2040. kmem_list3_init(cachep->nodelists[node]);
  2041. }
  2042. }
  2043. }
  2044. cachep->nodelists[numa_mem_id()]->next_reap =
  2045. jiffies + REAPTIMEOUT_LIST3 +
  2046. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  2047. cpu_cache_get(cachep)->avail = 0;
  2048. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  2049. cpu_cache_get(cachep)->batchcount = 1;
  2050. cpu_cache_get(cachep)->touched = 0;
  2051. cachep->batchcount = 1;
  2052. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  2053. return 0;
  2054. }
  2055. /**
  2056. * __kmem_cache_create - Create a cache.
  2057. * @name: A string which is used in /proc/slabinfo to identify this cache.
  2058. * @size: The size of objects to be created in this cache.
  2059. * @align: The required alignment for the objects.
  2060. * @flags: SLAB flags
  2061. * @ctor: A constructor for the objects.
  2062. *
  2063. * Returns a ptr to the cache on success, NULL on failure.
  2064. * Cannot be called within a int, but can be interrupted.
  2065. * The @ctor is run when new pages are allocated by the cache.
  2066. *
  2067. * @name must be valid until the cache is destroyed. This implies that
  2068. * the module calling this has to destroy the cache before getting unloaded.
  2069. *
  2070. * The flags are
  2071. *
  2072. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  2073. * to catch references to uninitialised memory.
  2074. *
  2075. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  2076. * for buffer overruns.
  2077. *
  2078. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  2079. * cacheline. This can be beneficial if you're counting cycles as closely
  2080. * as davem.
  2081. */
  2082. struct kmem_cache *
  2083. __kmem_cache_create (const char *name, size_t size, size_t align,
  2084. unsigned long flags, void (*ctor)(void *))
  2085. {
  2086. size_t left_over, slab_size, ralign;
  2087. struct kmem_cache *cachep = NULL;
  2088. gfp_t gfp;
  2089. #if DEBUG
  2090. #if FORCED_DEBUG
  2091. /*
  2092. * Enable redzoning and last user accounting, except for caches with
  2093. * large objects, if the increased size would increase the object size
  2094. * above the next power of two: caches with object sizes just above a
  2095. * power of two have a significant amount of internal fragmentation.
  2096. */
  2097. if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
  2098. 2 * sizeof(unsigned long long)))
  2099. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  2100. if (!(flags & SLAB_DESTROY_BY_RCU))
  2101. flags |= SLAB_POISON;
  2102. #endif
  2103. if (flags & SLAB_DESTROY_BY_RCU)
  2104. BUG_ON(flags & SLAB_POISON);
  2105. #endif
  2106. /*
  2107. * Always checks flags, a caller might be expecting debug support which
  2108. * isn't available.
  2109. */
  2110. BUG_ON(flags & ~CREATE_MASK);
  2111. /*
  2112. * Check that size is in terms of words. This is needed to avoid
  2113. * unaligned accesses for some archs when redzoning is used, and makes
  2114. * sure any on-slab bufctl's are also correctly aligned.
  2115. */
  2116. if (size & (BYTES_PER_WORD - 1)) {
  2117. size += (BYTES_PER_WORD - 1);
  2118. size &= ~(BYTES_PER_WORD - 1);
  2119. }
  2120. /* calculate the final buffer alignment: */
  2121. /* 1) arch recommendation: can be overridden for debug */
  2122. if (flags & SLAB_HWCACHE_ALIGN) {
  2123. /*
  2124. * Default alignment: as specified by the arch code. Except if
  2125. * an object is really small, then squeeze multiple objects into
  2126. * one cacheline.
  2127. */
  2128. ralign = cache_line_size();
  2129. while (size <= ralign / 2)
  2130. ralign /= 2;
  2131. } else {
  2132. ralign = BYTES_PER_WORD;
  2133. }
  2134. /*
  2135. * Redzoning and user store require word alignment or possibly larger.
  2136. * Note this will be overridden by architecture or caller mandated
  2137. * alignment if either is greater than BYTES_PER_WORD.
  2138. */
  2139. if (flags & SLAB_STORE_USER)
  2140. ralign = BYTES_PER_WORD;
  2141. if (flags & SLAB_RED_ZONE) {
  2142. ralign = REDZONE_ALIGN;
  2143. /* If redzoning, ensure that the second redzone is suitably
  2144. * aligned, by adjusting the object size accordingly. */
  2145. size += REDZONE_ALIGN - 1;
  2146. size &= ~(REDZONE_ALIGN - 1);
  2147. }
  2148. /* 2) arch mandated alignment */
  2149. if (ralign < ARCH_SLAB_MINALIGN) {
  2150. ralign = ARCH_SLAB_MINALIGN;
  2151. }
  2152. /* 3) caller mandated alignment */
  2153. if (ralign < align) {
  2154. ralign = align;
  2155. }
  2156. /* disable debug if necessary */
  2157. if (ralign > __alignof__(unsigned long long))
  2158. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  2159. /*
  2160. * 4) Store it.
  2161. */
  2162. align = ralign;
  2163. if (slab_is_available())
  2164. gfp = GFP_KERNEL;
  2165. else
  2166. gfp = GFP_NOWAIT;
  2167. /* Get cache's description obj. */
  2168. cachep = kmem_cache_zalloc(&cache_cache, gfp);
  2169. if (!cachep)
  2170. return NULL;
  2171. cachep->nodelists = (struct kmem_list3 **)&cachep->array[nr_cpu_ids];
  2172. cachep->object_size = size;
  2173. cachep->align = align;
  2174. #if DEBUG
  2175. /*
  2176. * Both debugging options require word-alignment which is calculated
  2177. * into align above.
  2178. */
  2179. if (flags & SLAB_RED_ZONE) {
  2180. /* add space for red zone words */
  2181. cachep->obj_offset += sizeof(unsigned long long);
  2182. size += 2 * sizeof(unsigned long long);
  2183. }
  2184. if (flags & SLAB_STORE_USER) {
  2185. /* user store requires one word storage behind the end of
  2186. * the real object. But if the second red zone needs to be
  2187. * aligned to 64 bits, we must allow that much space.
  2188. */
  2189. if (flags & SLAB_RED_ZONE)
  2190. size += REDZONE_ALIGN;
  2191. else
  2192. size += BYTES_PER_WORD;
  2193. }
  2194. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  2195. if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
  2196. && cachep->object_size > cache_line_size() && ALIGN(size, align) < PAGE_SIZE) {
  2197. cachep->obj_offset += PAGE_SIZE - ALIGN(size, align);
  2198. size = PAGE_SIZE;
  2199. }
  2200. #endif
  2201. #endif
  2202. /*
  2203. * Determine if the slab management is 'on' or 'off' slab.
  2204. * (bootstrapping cannot cope with offslab caches so don't do
  2205. * it too early on. Always use on-slab management when
  2206. * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
  2207. */
  2208. if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
  2209. !(flags & SLAB_NOLEAKTRACE))
  2210. /*
  2211. * Size is large, assume best to place the slab management obj
  2212. * off-slab (should allow better packing of objs).
  2213. */
  2214. flags |= CFLGS_OFF_SLAB;
  2215. size = ALIGN(size, align);
  2216. left_over = calculate_slab_order(cachep, size, align, flags);
  2217. if (!cachep->num) {
  2218. printk(KERN_ERR
  2219. "kmem_cache_create: couldn't create cache %s.\n", name);
  2220. kmem_cache_free(&cache_cache, cachep);
  2221. return NULL;
  2222. }
  2223. slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
  2224. + sizeof(struct slab), align);
  2225. /*
  2226. * If the slab has been placed off-slab, and we have enough space then
  2227. * move it on-slab. This is at the expense of any extra colouring.
  2228. */
  2229. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  2230. flags &= ~CFLGS_OFF_SLAB;
  2231. left_over -= slab_size;
  2232. }
  2233. if (flags & CFLGS_OFF_SLAB) {
  2234. /* really off slab. No need for manual alignment */
  2235. slab_size =
  2236. cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
  2237. #ifdef CONFIG_PAGE_POISONING
  2238. /* If we're going to use the generic kernel_map_pages()
  2239. * poisoning, then it's going to smash the contents of
  2240. * the redzone and userword anyhow, so switch them off.
  2241. */
  2242. if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
  2243. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  2244. #endif
  2245. }
  2246. cachep->colour_off = cache_line_size();
  2247. /* Offset must be a multiple of the alignment. */
  2248. if (cachep->colour_off < align)
  2249. cachep->colour_off = align;
  2250. cachep->colour = left_over / cachep->colour_off;
  2251. cachep->slab_size = slab_size;
  2252. cachep->flags = flags;
  2253. cachep->allocflags = 0;
  2254. if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
  2255. cachep->allocflags |= GFP_DMA;
  2256. cachep->size = size;
  2257. cachep->reciprocal_buffer_size = reciprocal_value(size);
  2258. if (flags & CFLGS_OFF_SLAB) {
  2259. cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
  2260. /*
  2261. * This is a possibility for one of the malloc_sizes caches.
  2262. * But since we go off slab only for object size greater than
  2263. * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
  2264. * this should not happen at all.
  2265. * But leave a BUG_ON for some lucky dude.
  2266. */
  2267. BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
  2268. }
  2269. cachep->ctor = ctor;
  2270. cachep->name = name;
  2271. if (setup_cpu_cache(cachep, gfp)) {
  2272. __kmem_cache_destroy(cachep);
  2273. return NULL;
  2274. }
  2275. if (flags & SLAB_DEBUG_OBJECTS) {
  2276. /*
  2277. * Would deadlock through slab_destroy()->call_rcu()->
  2278. * debug_object_activate()->kmem_cache_alloc().
  2279. */
  2280. WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
  2281. slab_set_debugobj_lock_classes(cachep);
  2282. }
  2283. /* cache setup completed, link it into the list */
  2284. list_add(&cachep->list, &slab_caches);
  2285. return cachep;
  2286. }
  2287. #if DEBUG
  2288. static void check_irq_off(void)
  2289. {
  2290. BUG_ON(!irqs_disabled());
  2291. }
  2292. static void check_irq_on(void)
  2293. {
  2294. BUG_ON(irqs_disabled());
  2295. }
  2296. static void check_spinlock_acquired(struct kmem_cache *cachep)
  2297. {
  2298. #ifdef CONFIG_SMP
  2299. check_irq_off();
  2300. assert_spin_locked(&cachep->nodelists[numa_mem_id()]->list_lock);
  2301. #endif
  2302. }
  2303. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  2304. {
  2305. #ifdef CONFIG_SMP
  2306. check_irq_off();
  2307. assert_spin_locked(&cachep->nodelists[node]->list_lock);
  2308. #endif
  2309. }
  2310. #else
  2311. #define check_irq_off() do { } while(0)
  2312. #define check_irq_on() do { } while(0)
  2313. #define check_spinlock_acquired(x) do { } while(0)
  2314. #define check_spinlock_acquired_node(x, y) do { } while(0)
  2315. #endif
  2316. static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  2317. struct array_cache *ac,
  2318. int force, int node);
  2319. static void do_drain(void *arg)
  2320. {
  2321. struct kmem_cache *cachep = arg;
  2322. struct array_cache *ac;
  2323. int node = numa_mem_id();
  2324. check_irq_off();
  2325. ac = cpu_cache_get(cachep);
  2326. spin_lock(&cachep->nodelists[node]->list_lock);
  2327. free_block(cachep, ac->entry, ac->avail, node);
  2328. spin_unlock(&cachep->nodelists[node]->list_lock);
  2329. ac->avail = 0;
  2330. }
  2331. static void drain_cpu_caches(struct kmem_cache *cachep)
  2332. {
  2333. struct kmem_list3 *l3;
  2334. int node;
  2335. on_each_cpu(do_drain, cachep, 1);
  2336. check_irq_on();
  2337. for_each_online_node(node) {
  2338. l3 = cachep->nodelists[node];
  2339. if (l3 && l3->alien)
  2340. drain_alien_cache(cachep, l3->alien);
  2341. }
  2342. for_each_online_node(node) {
  2343. l3 = cachep->nodelists[node];
  2344. if (l3)
  2345. drain_array(cachep, l3, l3->shared, 1, node);
  2346. }
  2347. }
  2348. /*
  2349. * Remove slabs from the list of free slabs.
  2350. * Specify the number of slabs to drain in tofree.
  2351. *
  2352. * Returns the actual number of slabs released.
  2353. */
  2354. static int drain_freelist(struct kmem_cache *cache,
  2355. struct kmem_list3 *l3, int tofree)
  2356. {
  2357. struct list_head *p;
  2358. int nr_freed;
  2359. struct slab *slabp;
  2360. nr_freed = 0;
  2361. while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
  2362. spin_lock_irq(&l3->list_lock);
  2363. p = l3->slabs_free.prev;
  2364. if (p == &l3->slabs_free) {
  2365. spin_unlock_irq(&l3->list_lock);
  2366. goto out;
  2367. }
  2368. slabp = list_entry(p, struct slab, list);
  2369. #if DEBUG
  2370. BUG_ON(slabp->inuse);
  2371. #endif
  2372. list_del(&slabp->list);
  2373. /*
  2374. * Safe to drop the lock. The slab is no longer linked
  2375. * to the cache.
  2376. */
  2377. l3->free_objects -= cache->num;
  2378. spin_unlock_irq(&l3->list_lock);
  2379. slab_destroy(cache, slabp);
  2380. nr_freed++;
  2381. }
  2382. out:
  2383. return nr_freed;
  2384. }
  2385. /* Called with slab_mutex held to protect against cpu hotplug */
  2386. static int __cache_shrink(struct kmem_cache *cachep)
  2387. {
  2388. int ret = 0, i = 0;
  2389. struct kmem_list3 *l3;
  2390. drain_cpu_caches(cachep);
  2391. check_irq_on();
  2392. for_each_online_node(i) {
  2393. l3 = cachep->nodelists[i];
  2394. if (!l3)
  2395. continue;
  2396. drain_freelist(cachep, l3, l3->free_objects);
  2397. ret += !list_empty(&l3->slabs_full) ||
  2398. !list_empty(&l3->slabs_partial);
  2399. }
  2400. return (ret ? 1 : 0);
  2401. }
  2402. /**
  2403. * kmem_cache_shrink - Shrink a cache.
  2404. * @cachep: The cache to shrink.
  2405. *
  2406. * Releases as many slabs as possible for a cache.
  2407. * To help debugging, a zero exit status indicates all slabs were released.
  2408. */
  2409. int kmem_cache_shrink(struct kmem_cache *cachep)
  2410. {
  2411. int ret;
  2412. BUG_ON(!cachep || in_interrupt());
  2413. get_online_cpus();
  2414. mutex_lock(&slab_mutex);
  2415. ret = __cache_shrink(cachep);
  2416. mutex_unlock(&slab_mutex);
  2417. put_online_cpus();
  2418. return ret;
  2419. }
  2420. EXPORT_SYMBOL(kmem_cache_shrink);
  2421. /**
  2422. * kmem_cache_destroy - delete a cache
  2423. * @cachep: the cache to destroy
  2424. *
  2425. * Remove a &struct kmem_cache object from the slab cache.
  2426. *
  2427. * It is expected this function will be called by a module when it is
  2428. * unloaded. This will remove the cache completely, and avoid a duplicate
  2429. * cache being allocated each time a module is loaded and unloaded, if the
  2430. * module doesn't have persistent in-kernel storage across loads and unloads.
  2431. *
  2432. * The cache must be empty before calling this function.
  2433. *
  2434. * The caller must guarantee that no one will allocate memory from the cache
  2435. * during the kmem_cache_destroy().
  2436. */
  2437. void kmem_cache_destroy(struct kmem_cache *cachep)
  2438. {
  2439. BUG_ON(!cachep || in_interrupt());
  2440. /* Find the cache in the chain of caches. */
  2441. get_online_cpus();
  2442. mutex_lock(&slab_mutex);
  2443. /*
  2444. * the chain is never empty, cache_cache is never destroyed
  2445. */
  2446. list_del(&cachep->list);
  2447. if (__cache_shrink(cachep)) {
  2448. slab_error(cachep, "Can't free all objects");
  2449. list_add(&cachep->list, &slab_caches);
  2450. mutex_unlock(&slab_mutex);
  2451. put_online_cpus();
  2452. return;
  2453. }
  2454. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
  2455. rcu_barrier();
  2456. __kmem_cache_destroy(cachep);
  2457. mutex_unlock(&slab_mutex);
  2458. put_online_cpus();
  2459. }
  2460. EXPORT_SYMBOL(kmem_cache_destroy);
  2461. /*
  2462. * Get the memory for a slab management obj.
  2463. * For a slab cache when the slab descriptor is off-slab, slab descriptors
  2464. * always come from malloc_sizes caches. The slab descriptor cannot
  2465. * come from the same cache which is getting created because,
  2466. * when we are searching for an appropriate cache for these
  2467. * descriptors in kmem_cache_create, we search through the malloc_sizes array.
  2468. * If we are creating a malloc_sizes cache here it would not be visible to
  2469. * kmem_find_general_cachep till the initialization is complete.
  2470. * Hence we cannot have slabp_cache same as the original cache.
  2471. */
  2472. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
  2473. int colour_off, gfp_t local_flags,
  2474. int nodeid)
  2475. {
  2476. struct slab *slabp;
  2477. if (OFF_SLAB(cachep)) {
  2478. /* Slab management obj is off-slab. */
  2479. slabp = kmem_cache_alloc_node(cachep->slabp_cache,
  2480. local_flags, nodeid);
  2481. /*
  2482. * If the first object in the slab is leaked (it's allocated
  2483. * but no one has a reference to it), we want to make sure
  2484. * kmemleak does not treat the ->s_mem pointer as a reference
  2485. * to the object. Otherwise we will not report the leak.
  2486. */
  2487. kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
  2488. local_flags);
  2489. if (!slabp)
  2490. return NULL;
  2491. } else {
  2492. slabp = objp + colour_off;
  2493. colour_off += cachep->slab_size;
  2494. }
  2495. slabp->inuse = 0;
  2496. slabp->colouroff = colour_off;
  2497. slabp->s_mem = objp + colour_off;
  2498. slabp->nodeid = nodeid;
  2499. slabp->free = 0;
  2500. return slabp;
  2501. }
  2502. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  2503. {
  2504. return (kmem_bufctl_t *) (slabp + 1);
  2505. }
  2506. static void cache_init_objs(struct kmem_cache *cachep,
  2507. struct slab *slabp)
  2508. {
  2509. int i;
  2510. for (i = 0; i < cachep->num; i++) {
  2511. void *objp = index_to_obj(cachep, slabp, i);
  2512. #if DEBUG
  2513. /* need to poison the objs? */
  2514. if (cachep->flags & SLAB_POISON)
  2515. poison_obj(cachep, objp, POISON_FREE);
  2516. if (cachep->flags & SLAB_STORE_USER)
  2517. *dbg_userword(cachep, objp) = NULL;
  2518. if (cachep->flags & SLAB_RED_ZONE) {
  2519. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2520. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2521. }
  2522. /*
  2523. * Constructors are not allowed to allocate memory from the same
  2524. * cache which they are a constructor for. Otherwise, deadlock.
  2525. * They must also be threaded.
  2526. */
  2527. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  2528. cachep->ctor(objp + obj_offset(cachep));
  2529. if (cachep->flags & SLAB_RED_ZONE) {
  2530. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  2531. slab_error(cachep, "constructor overwrote the"
  2532. " end of an object");
  2533. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  2534. slab_error(cachep, "constructor overwrote the"
  2535. " start of an object");
  2536. }
  2537. if ((cachep->size % PAGE_SIZE) == 0 &&
  2538. OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  2539. kernel_map_pages(virt_to_page(objp),
  2540. cachep->size / PAGE_SIZE, 0);
  2541. #else
  2542. if (cachep->ctor)
  2543. cachep->ctor(objp);
  2544. #endif
  2545. slab_bufctl(slabp)[i] = i + 1;
  2546. }
  2547. slab_bufctl(slabp)[i - 1] = BUFCTL_END;
  2548. }
  2549. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  2550. {
  2551. if (CONFIG_ZONE_DMA_FLAG) {
  2552. if (flags & GFP_DMA)
  2553. BUG_ON(!(cachep->allocflags & GFP_DMA));
  2554. else
  2555. BUG_ON(cachep->allocflags & GFP_DMA);
  2556. }
  2557. }
  2558. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  2559. int nodeid)
  2560. {
  2561. void *objp = index_to_obj(cachep, slabp, slabp->free);
  2562. kmem_bufctl_t next;
  2563. slabp->inuse++;
  2564. next = slab_bufctl(slabp)[slabp->free];
  2565. #if DEBUG
  2566. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2567. WARN_ON(slabp->nodeid != nodeid);
  2568. #endif
  2569. slabp->free = next;
  2570. return objp;
  2571. }
  2572. static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  2573. void *objp, int nodeid)
  2574. {
  2575. unsigned int objnr = obj_to_index(cachep, slabp, objp);
  2576. #if DEBUG
  2577. /* Verify that the slab belongs to the intended node */
  2578. WARN_ON(slabp->nodeid != nodeid);
  2579. if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
  2580. printk(KERN_ERR "slab: double free detected in cache "
  2581. "'%s', objp %p\n", cachep->name, objp);
  2582. BUG();
  2583. }
  2584. #endif
  2585. slab_bufctl(slabp)[objnr] = slabp->free;
  2586. slabp->free = objnr;
  2587. slabp->inuse--;
  2588. }
  2589. /*
  2590. * Map pages beginning at addr to the given cache and slab. This is required
  2591. * for the slab allocator to be able to lookup the cache and slab of a
  2592. * virtual address for kfree, ksize, and slab debugging.
  2593. */
  2594. static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
  2595. void *addr)
  2596. {
  2597. int nr_pages;
  2598. struct page *page;
  2599. page = virt_to_page(addr);
  2600. nr_pages = 1;
  2601. if (likely(!PageCompound(page)))
  2602. nr_pages <<= cache->gfporder;
  2603. do {
  2604. page->slab_cache = cache;
  2605. page->slab_page = slab;
  2606. page++;
  2607. } while (--nr_pages);
  2608. }
  2609. /*
  2610. * Grow (by 1) the number of slabs within a cache. This is called by
  2611. * kmem_cache_alloc() when there are no active objs left in a cache.
  2612. */
  2613. static int cache_grow(struct kmem_cache *cachep,
  2614. gfp_t flags, int nodeid, void *objp)
  2615. {
  2616. struct slab *slabp;
  2617. size_t offset;
  2618. gfp_t local_flags;
  2619. struct kmem_list3 *l3;
  2620. /*
  2621. * Be lazy and only check for valid flags here, keeping it out of the
  2622. * critical path in kmem_cache_alloc().
  2623. */
  2624. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  2625. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2626. /* Take the l3 list lock to change the colour_next on this node */
  2627. check_irq_off();
  2628. l3 = cachep->nodelists[nodeid];
  2629. spin_lock(&l3->list_lock);
  2630. /* Get colour for the slab, and cal the next value. */
  2631. offset = l3->colour_next;
  2632. l3->colour_next++;
  2633. if (l3->colour_next >= cachep->colour)
  2634. l3->colour_next = 0;
  2635. spin_unlock(&l3->list_lock);
  2636. offset *= cachep->colour_off;
  2637. if (local_flags & __GFP_WAIT)
  2638. local_irq_enable();
  2639. /*
  2640. * The test for missing atomic flag is performed here, rather than
  2641. * the more obvious place, simply to reduce the critical path length
  2642. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  2643. * will eventually be caught here (where it matters).
  2644. */
  2645. kmem_flagcheck(cachep, flags);
  2646. /*
  2647. * Get mem for the objs. Attempt to allocate a physical page from
  2648. * 'nodeid'.
  2649. */
  2650. if (!objp)
  2651. objp = kmem_getpages(cachep, local_flags, nodeid);
  2652. if (!objp)
  2653. goto failed;
  2654. /* Get slab management. */
  2655. slabp = alloc_slabmgmt(cachep, objp, offset,
  2656. local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
  2657. if (!slabp)
  2658. goto opps1;
  2659. slab_map_pages(cachep, slabp, objp);
  2660. cache_init_objs(cachep, slabp);
  2661. if (local_flags & __GFP_WAIT)
  2662. local_irq_disable();
  2663. check_irq_off();
  2664. spin_lock(&l3->list_lock);
  2665. /* Make slab active. */
  2666. list_add_tail(&slabp->list, &(l3->slabs_free));
  2667. STATS_INC_GROWN(cachep);
  2668. l3->free_objects += cachep->num;
  2669. spin_unlock(&l3->list_lock);
  2670. return 1;
  2671. opps1:
  2672. kmem_freepages(cachep, objp);
  2673. failed:
  2674. if (local_flags & __GFP_WAIT)
  2675. local_irq_disable();
  2676. return 0;
  2677. }
  2678. #if DEBUG
  2679. /*
  2680. * Perform extra freeing checks:
  2681. * - detect bad pointers.
  2682. * - POISON/RED_ZONE checking
  2683. */
  2684. static void kfree_debugcheck(const void *objp)
  2685. {
  2686. if (!virt_addr_valid(objp)) {
  2687. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  2688. (unsigned long)objp);
  2689. BUG();
  2690. }
  2691. }
  2692. static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  2693. {
  2694. unsigned long long redzone1, redzone2;
  2695. redzone1 = *dbg_redzone1(cache, obj);
  2696. redzone2 = *dbg_redzone2(cache, obj);
  2697. /*
  2698. * Redzone is ok.
  2699. */
  2700. if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  2701. return;
  2702. if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  2703. slab_error(cache, "double free detected");
  2704. else
  2705. slab_error(cache, "memory outside object was overwritten");
  2706. printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
  2707. obj, redzone1, redzone2);
  2708. }
  2709. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2710. void *caller)
  2711. {
  2712. struct page *page;
  2713. unsigned int objnr;
  2714. struct slab *slabp;
  2715. BUG_ON(virt_to_cache(objp) != cachep);
  2716. objp -= obj_offset(cachep);
  2717. kfree_debugcheck(objp);
  2718. page = virt_to_head_page(objp);
  2719. slabp = page->slab_page;
  2720. if (cachep->flags & SLAB_RED_ZONE) {
  2721. verify_redzone_free(cachep, objp);
  2722. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2723. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2724. }
  2725. if (cachep->flags & SLAB_STORE_USER)
  2726. *dbg_userword(cachep, objp) = caller;
  2727. objnr = obj_to_index(cachep, slabp, objp);
  2728. BUG_ON(objnr >= cachep->num);
  2729. BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
  2730. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2731. slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
  2732. #endif
  2733. if (cachep->flags & SLAB_POISON) {
  2734. #ifdef CONFIG_DEBUG_PAGEALLOC
  2735. if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
  2736. store_stackinfo(cachep, objp, (unsigned long)caller);
  2737. kernel_map_pages(virt_to_page(objp),
  2738. cachep->size / PAGE_SIZE, 0);
  2739. } else {
  2740. poison_obj(cachep, objp, POISON_FREE);
  2741. }
  2742. #else
  2743. poison_obj(cachep, objp, POISON_FREE);
  2744. #endif
  2745. }
  2746. return objp;
  2747. }
  2748. static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
  2749. {
  2750. kmem_bufctl_t i;
  2751. int entries = 0;
  2752. /* Check slab's freelist to see if this obj is there. */
  2753. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2754. entries++;
  2755. if (entries > cachep->num || i >= cachep->num)
  2756. goto bad;
  2757. }
  2758. if (entries != cachep->num - slabp->inuse) {
  2759. bad:
  2760. printk(KERN_ERR "slab: Internal list corruption detected in "
  2761. "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
  2762. cachep->name, cachep->num, slabp, slabp->inuse,
  2763. print_tainted());
  2764. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
  2765. sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
  2766. 1);
  2767. BUG();
  2768. }
  2769. }
  2770. #else
  2771. #define kfree_debugcheck(x) do { } while(0)
  2772. #define cache_free_debugcheck(x,objp,z) (objp)
  2773. #define check_slabp(x,y) do { } while(0)
  2774. #endif
  2775. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
  2776. bool force_refill)
  2777. {
  2778. int batchcount;
  2779. struct kmem_list3 *l3;
  2780. struct array_cache *ac;
  2781. int node;
  2782. check_irq_off();
  2783. node = numa_mem_id();
  2784. if (unlikely(force_refill))
  2785. goto force_grow;
  2786. retry:
  2787. ac = cpu_cache_get(cachep);
  2788. batchcount = ac->batchcount;
  2789. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2790. /*
  2791. * If there was little recent activity on this cache, then
  2792. * perform only a partial refill. Otherwise we could generate
  2793. * refill bouncing.
  2794. */
  2795. batchcount = BATCHREFILL_LIMIT;
  2796. }
  2797. l3 = cachep->nodelists[node];
  2798. BUG_ON(ac->avail > 0 || !l3);
  2799. spin_lock(&l3->list_lock);
  2800. /* See if we can refill from the shared array */
  2801. if (l3->shared && transfer_objects(ac, l3->shared, batchcount)) {
  2802. l3->shared->touched = 1;
  2803. goto alloc_done;
  2804. }
  2805. while (batchcount > 0) {
  2806. struct list_head *entry;
  2807. struct slab *slabp;
  2808. /* Get slab alloc is to come from. */
  2809. entry = l3->slabs_partial.next;
  2810. if (entry == &l3->slabs_partial) {
  2811. l3->free_touched = 1;
  2812. entry = l3->slabs_free.next;
  2813. if (entry == &l3->slabs_free)
  2814. goto must_grow;
  2815. }
  2816. slabp = list_entry(entry, struct slab, list);
  2817. check_slabp(cachep, slabp);
  2818. check_spinlock_acquired(cachep);
  2819. /*
  2820. * The slab was either on partial or free list so
  2821. * there must be at least one object available for
  2822. * allocation.
  2823. */
  2824. BUG_ON(slabp->inuse >= cachep->num);
  2825. while (slabp->inuse < cachep->num && batchcount--) {
  2826. STATS_INC_ALLOCED(cachep);
  2827. STATS_INC_ACTIVE(cachep);
  2828. STATS_SET_HIGH(cachep);
  2829. ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
  2830. node));
  2831. }
  2832. check_slabp(cachep, slabp);
  2833. /* move slabp to correct slabp list: */
  2834. list_del(&slabp->list);
  2835. if (slabp->free == BUFCTL_END)
  2836. list_add(&slabp->list, &l3->slabs_full);
  2837. else
  2838. list_add(&slabp->list, &l3->slabs_partial);
  2839. }
  2840. must_grow:
  2841. l3->free_objects -= ac->avail;
  2842. alloc_done:
  2843. spin_unlock(&l3->list_lock);
  2844. if (unlikely(!ac->avail)) {
  2845. int x;
  2846. force_grow:
  2847. x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
  2848. /* cache_grow can reenable interrupts, then ac could change. */
  2849. ac = cpu_cache_get(cachep);
  2850. /* no objects in sight? abort */
  2851. if (!x && (ac->avail == 0 || force_refill))
  2852. return NULL;
  2853. if (!ac->avail) /* objects refilled by interrupt? */
  2854. goto retry;
  2855. }
  2856. ac->touched = 1;
  2857. return ac_get_obj(cachep, ac, flags, force_refill);
  2858. }
  2859. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2860. gfp_t flags)
  2861. {
  2862. might_sleep_if(flags & __GFP_WAIT);
  2863. #if DEBUG
  2864. kmem_flagcheck(cachep, flags);
  2865. #endif
  2866. }
  2867. #if DEBUG
  2868. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2869. gfp_t flags, void *objp, void *caller)
  2870. {
  2871. if (!objp)
  2872. return objp;
  2873. if (cachep->flags & SLAB_POISON) {
  2874. #ifdef CONFIG_DEBUG_PAGEALLOC
  2875. if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2876. kernel_map_pages(virt_to_page(objp),
  2877. cachep->size / PAGE_SIZE, 1);
  2878. else
  2879. check_poison_obj(cachep, objp);
  2880. #else
  2881. check_poison_obj(cachep, objp);
  2882. #endif
  2883. poison_obj(cachep, objp, POISON_INUSE);
  2884. }
  2885. if (cachep->flags & SLAB_STORE_USER)
  2886. *dbg_userword(cachep, objp) = caller;
  2887. if (cachep->flags & SLAB_RED_ZONE) {
  2888. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2889. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2890. slab_error(cachep, "double free, or memory outside"
  2891. " object was overwritten");
  2892. printk(KERN_ERR
  2893. "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
  2894. objp, *dbg_redzone1(cachep, objp),
  2895. *dbg_redzone2(cachep, objp));
  2896. }
  2897. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2898. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2899. }
  2900. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2901. {
  2902. struct slab *slabp;
  2903. unsigned objnr;
  2904. slabp = virt_to_head_page(objp)->slab_page;
  2905. objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
  2906. slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
  2907. }
  2908. #endif
  2909. objp += obj_offset(cachep);
  2910. if (cachep->ctor && cachep->flags & SLAB_POISON)
  2911. cachep->ctor(objp);
  2912. if (ARCH_SLAB_MINALIGN &&
  2913. ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
  2914. printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
  2915. objp, (int)ARCH_SLAB_MINALIGN);
  2916. }
  2917. return objp;
  2918. }
  2919. #else
  2920. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2921. #endif
  2922. static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
  2923. {
  2924. if (cachep == &cache_cache)
  2925. return false;
  2926. return should_failslab(cachep->object_size, flags, cachep->flags);
  2927. }
  2928. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2929. {
  2930. void *objp;
  2931. struct array_cache *ac;
  2932. bool force_refill = false;
  2933. check_irq_off();
  2934. ac = cpu_cache_get(cachep);
  2935. if (likely(ac->avail)) {
  2936. ac->touched = 1;
  2937. objp = ac_get_obj(cachep, ac, flags, false);
  2938. /*
  2939. * Allow for the possibility all avail objects are not allowed
  2940. * by the current flags
  2941. */
  2942. if (objp) {
  2943. STATS_INC_ALLOCHIT(cachep);
  2944. goto out;
  2945. }
  2946. force_refill = true;
  2947. }
  2948. STATS_INC_ALLOCMISS(cachep);
  2949. objp = cache_alloc_refill(cachep, flags, force_refill);
  2950. /*
  2951. * the 'ac' may be updated by cache_alloc_refill(),
  2952. * and kmemleak_erase() requires its correct value.
  2953. */
  2954. ac = cpu_cache_get(cachep);
  2955. out:
  2956. /*
  2957. * To avoid a false negative, if an object that is in one of the
  2958. * per-CPU caches is leaked, we need to make sure kmemleak doesn't
  2959. * treat the array pointers as a reference to the object.
  2960. */
  2961. if (objp)
  2962. kmemleak_erase(&ac->entry[ac->avail]);
  2963. return objp;
  2964. }
  2965. #ifdef CONFIG_NUMA
  2966. /*
  2967. * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
  2968. *
  2969. * If we are in_interrupt, then process context, including cpusets and
  2970. * mempolicy, may not apply and should not be used for allocation policy.
  2971. */
  2972. static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  2973. {
  2974. int nid_alloc, nid_here;
  2975. if (in_interrupt() || (flags & __GFP_THISNODE))
  2976. return NULL;
  2977. nid_alloc = nid_here = numa_mem_id();
  2978. if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
  2979. nid_alloc = cpuset_slab_spread_node();
  2980. else if (current->mempolicy)
  2981. nid_alloc = slab_node();
  2982. if (nid_alloc != nid_here)
  2983. return ____cache_alloc_node(cachep, flags, nid_alloc);
  2984. return NULL;
  2985. }
  2986. /*
  2987. * Fallback function if there was no memory available and no objects on a
  2988. * certain node and fall back is permitted. First we scan all the
  2989. * available nodelists for available objects. If that fails then we
  2990. * perform an allocation without specifying a node. This allows the page
  2991. * allocator to do its reclaim / fallback magic. We then insert the
  2992. * slab into the proper nodelist and then allocate from it.
  2993. */
  2994. static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
  2995. {
  2996. struct zonelist *zonelist;
  2997. gfp_t local_flags;
  2998. struct zoneref *z;
  2999. struct zone *zone;
  3000. enum zone_type high_zoneidx = gfp_zone(flags);
  3001. void *obj = NULL;
  3002. int nid;
  3003. unsigned int cpuset_mems_cookie;
  3004. if (flags & __GFP_THISNODE)
  3005. return NULL;
  3006. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  3007. retry_cpuset:
  3008. cpuset_mems_cookie = get_mems_allowed();
  3009. zonelist = node_zonelist(slab_node(), flags);
  3010. retry:
  3011. /*
  3012. * Look through allowed nodes for objects available
  3013. * from existing per node queues.
  3014. */
  3015. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  3016. nid = zone_to_nid(zone);
  3017. if (cpuset_zone_allowed_hardwall(zone, flags) &&
  3018. cache->nodelists[nid] &&
  3019. cache->nodelists[nid]->free_objects) {
  3020. obj = ____cache_alloc_node(cache,
  3021. flags | GFP_THISNODE, nid);
  3022. if (obj)
  3023. break;
  3024. }
  3025. }
  3026. if (!obj) {
  3027. /*
  3028. * This allocation will be performed within the constraints
  3029. * of the current cpuset / memory policy requirements.
  3030. * We may trigger various forms of reclaim on the allowed
  3031. * set and go into memory reserves if necessary.
  3032. */
  3033. if (local_flags & __GFP_WAIT)
  3034. local_irq_enable();
  3035. kmem_flagcheck(cache, flags);
  3036. obj = kmem_getpages(cache, local_flags, numa_mem_id());
  3037. if (local_flags & __GFP_WAIT)
  3038. local_irq_disable();
  3039. if (obj) {
  3040. /*
  3041. * Insert into the appropriate per node queues
  3042. */
  3043. nid = page_to_nid(virt_to_page(obj));
  3044. if (cache_grow(cache, flags, nid, obj)) {
  3045. obj = ____cache_alloc_node(cache,
  3046. flags | GFP_THISNODE, nid);
  3047. if (!obj)
  3048. /*
  3049. * Another processor may allocate the
  3050. * objects in the slab since we are
  3051. * not holding any locks.
  3052. */
  3053. goto retry;
  3054. } else {
  3055. /* cache_grow already freed obj */
  3056. obj = NULL;
  3057. }
  3058. }
  3059. }
  3060. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
  3061. goto retry_cpuset;
  3062. return obj;
  3063. }
  3064. /*
  3065. * A interface to enable slab creation on nodeid
  3066. */
  3067. static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  3068. int nodeid)
  3069. {
  3070. struct list_head *entry;
  3071. struct slab *slabp;
  3072. struct kmem_list3 *l3;
  3073. void *obj;
  3074. int x;
  3075. l3 = cachep->nodelists[nodeid];
  3076. BUG_ON(!l3);
  3077. retry:
  3078. check_irq_off();
  3079. spin_lock(&l3->list_lock);
  3080. entry = l3->slabs_partial.next;
  3081. if (entry == &l3->slabs_partial) {
  3082. l3->free_touched = 1;
  3083. entry = l3->slabs_free.next;
  3084. if (entry == &l3->slabs_free)
  3085. goto must_grow;
  3086. }
  3087. slabp = list_entry(entry, struct slab, list);
  3088. check_spinlock_acquired_node(cachep, nodeid);
  3089. check_slabp(cachep, slabp);
  3090. STATS_INC_NODEALLOCS(cachep);
  3091. STATS_INC_ACTIVE(cachep);
  3092. STATS_SET_HIGH(cachep);
  3093. BUG_ON(slabp->inuse == cachep->num);
  3094. obj = slab_get_obj(cachep, slabp, nodeid);
  3095. check_slabp(cachep, slabp);
  3096. l3->free_objects--;
  3097. /* move slabp to correct slabp list: */
  3098. list_del(&slabp->list);
  3099. if (slabp->free == BUFCTL_END)
  3100. list_add(&slabp->list, &l3->slabs_full);
  3101. else
  3102. list_add(&slabp->list, &l3->slabs_partial);
  3103. spin_unlock(&l3->list_lock);
  3104. goto done;
  3105. must_grow:
  3106. spin_unlock(&l3->list_lock);
  3107. x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
  3108. if (x)
  3109. goto retry;
  3110. return fallback_alloc(cachep, flags);
  3111. done:
  3112. return obj;
  3113. }
  3114. /**
  3115. * kmem_cache_alloc_node - Allocate an object on the specified node
  3116. * @cachep: The cache to allocate from.
  3117. * @flags: See kmalloc().
  3118. * @nodeid: node number of the target node.
  3119. * @caller: return address of caller, used for debug information
  3120. *
  3121. * Identical to kmem_cache_alloc but it will allocate memory on the given
  3122. * node, which can improve the performance for cpu bound structures.
  3123. *
  3124. * Fallback to other node is possible if __GFP_THISNODE is not set.
  3125. */
  3126. static __always_inline void *
  3127. __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
  3128. void *caller)
  3129. {
  3130. unsigned long save_flags;
  3131. void *ptr;
  3132. int slab_node = numa_mem_id();
  3133. flags &= gfp_allowed_mask;
  3134. lockdep_trace_alloc(flags);
  3135. if (slab_should_failslab(cachep, flags))
  3136. return NULL;
  3137. cache_alloc_debugcheck_before(cachep, flags);
  3138. local_irq_save(save_flags);
  3139. if (nodeid == NUMA_NO_NODE)
  3140. nodeid = slab_node;
  3141. if (unlikely(!cachep->nodelists[nodeid])) {
  3142. /* Node not bootstrapped yet */
  3143. ptr = fallback_alloc(cachep, flags);
  3144. goto out;
  3145. }
  3146. if (nodeid == slab_node) {
  3147. /*
  3148. * Use the locally cached objects if possible.
  3149. * However ____cache_alloc does not allow fallback
  3150. * to other nodes. It may fail while we still have
  3151. * objects on other nodes available.
  3152. */
  3153. ptr = ____cache_alloc(cachep, flags);
  3154. if (ptr)
  3155. goto out;
  3156. }
  3157. /* ___cache_alloc_node can fall back to other nodes */
  3158. ptr = ____cache_alloc_node(cachep, flags, nodeid);
  3159. out:
  3160. local_irq_restore(save_flags);
  3161. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
  3162. kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
  3163. flags);
  3164. if (likely(ptr))
  3165. kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
  3166. if (unlikely((flags & __GFP_ZERO) && ptr))
  3167. memset(ptr, 0, cachep->object_size);
  3168. return ptr;
  3169. }
  3170. static __always_inline void *
  3171. __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
  3172. {
  3173. void *objp;
  3174. if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
  3175. objp = alternate_node_alloc(cache, flags);
  3176. if (objp)
  3177. goto out;
  3178. }
  3179. objp = ____cache_alloc(cache, flags);
  3180. /*
  3181. * We may just have run out of memory on the local node.
  3182. * ____cache_alloc_node() knows how to locate memory on other nodes
  3183. */
  3184. if (!objp)
  3185. objp = ____cache_alloc_node(cache, flags, numa_mem_id());
  3186. out:
  3187. return objp;
  3188. }
  3189. #else
  3190. static __always_inline void *
  3191. __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  3192. {
  3193. return ____cache_alloc(cachep, flags);
  3194. }
  3195. #endif /* CONFIG_NUMA */
  3196. static __always_inline void *
  3197. __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
  3198. {
  3199. unsigned long save_flags;
  3200. void *objp;
  3201. flags &= gfp_allowed_mask;
  3202. lockdep_trace_alloc(flags);
  3203. if (slab_should_failslab(cachep, flags))
  3204. return NULL;
  3205. cache_alloc_debugcheck_before(cachep, flags);
  3206. local_irq_save(save_flags);
  3207. objp = __do_cache_alloc(cachep, flags);
  3208. local_irq_restore(save_flags);
  3209. objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
  3210. kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
  3211. flags);
  3212. prefetchw(objp);
  3213. if (likely(objp))
  3214. kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
  3215. if (unlikely((flags & __GFP_ZERO) && objp))
  3216. memset(objp, 0, cachep->object_size);
  3217. return objp;
  3218. }
  3219. /*
  3220. * Caller needs to acquire correct kmem_list's list_lock
  3221. */
  3222. static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
  3223. int node)
  3224. {
  3225. int i;
  3226. struct kmem_list3 *l3;
  3227. for (i = 0; i < nr_objects; i++) {
  3228. void *objp;
  3229. struct slab *slabp;
  3230. clear_obj_pfmemalloc(&objpp[i]);
  3231. objp = objpp[i];
  3232. slabp = virt_to_slab(objp);
  3233. l3 = cachep->nodelists[node];
  3234. list_del(&slabp->list);
  3235. check_spinlock_acquired_node(cachep, node);
  3236. check_slabp(cachep, slabp);
  3237. slab_put_obj(cachep, slabp, objp, node);
  3238. STATS_DEC_ACTIVE(cachep);
  3239. l3->free_objects++;
  3240. check_slabp(cachep, slabp);
  3241. /* fixup slab chains */
  3242. if (slabp->inuse == 0) {
  3243. if (l3->free_objects > l3->free_limit) {
  3244. l3->free_objects -= cachep->num;
  3245. /* No need to drop any previously held
  3246. * lock here, even if we have a off-slab slab
  3247. * descriptor it is guaranteed to come from
  3248. * a different cache, refer to comments before
  3249. * alloc_slabmgmt.
  3250. */
  3251. slab_destroy(cachep, slabp);
  3252. } else {
  3253. list_add(&slabp->list, &l3->slabs_free);
  3254. }
  3255. } else {
  3256. /* Unconditionally move a slab to the end of the
  3257. * partial list on free - maximum time for the
  3258. * other objects to be freed, too.
  3259. */
  3260. list_add_tail(&slabp->list, &l3->slabs_partial);
  3261. }
  3262. }
  3263. }
  3264. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  3265. {
  3266. int batchcount;
  3267. struct kmem_list3 *l3;
  3268. int node = numa_mem_id();
  3269. batchcount = ac->batchcount;
  3270. #if DEBUG
  3271. BUG_ON(!batchcount || batchcount > ac->avail);
  3272. #endif
  3273. check_irq_off();
  3274. l3 = cachep->nodelists[node];
  3275. spin_lock(&l3->list_lock);
  3276. if (l3->shared) {
  3277. struct array_cache *shared_array = l3->shared;
  3278. int max = shared_array->limit - shared_array->avail;
  3279. if (max) {
  3280. if (batchcount > max)
  3281. batchcount = max;
  3282. memcpy(&(shared_array->entry[shared_array->avail]),
  3283. ac->entry, sizeof(void *) * batchcount);
  3284. shared_array->avail += batchcount;
  3285. goto free_done;
  3286. }
  3287. }
  3288. free_block(cachep, ac->entry, batchcount, node);
  3289. free_done:
  3290. #if STATS
  3291. {
  3292. int i = 0;
  3293. struct list_head *p;
  3294. p = l3->slabs_free.next;
  3295. while (p != &(l3->slabs_free)) {
  3296. struct slab *slabp;
  3297. slabp = list_entry(p, struct slab, list);
  3298. BUG_ON(slabp->inuse);
  3299. i++;
  3300. p = p->next;
  3301. }
  3302. STATS_SET_FREEABLE(cachep, i);
  3303. }
  3304. #endif
  3305. spin_unlock(&l3->list_lock);
  3306. ac->avail -= batchcount;
  3307. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  3308. }
  3309. /*
  3310. * Release an obj back to its cache. If the obj has a constructed state, it must
  3311. * be in this state _before_ it is released. Called with disabled ints.
  3312. */
  3313. static inline void __cache_free(struct kmem_cache *cachep, void *objp,
  3314. void *caller)
  3315. {
  3316. struct array_cache *ac = cpu_cache_get(cachep);
  3317. check_irq_off();
  3318. kmemleak_free_recursive(objp, cachep->flags);
  3319. objp = cache_free_debugcheck(cachep, objp, caller);
  3320. kmemcheck_slab_free(cachep, objp, cachep->object_size);
  3321. /*
  3322. * Skip calling cache_free_alien() when the platform is not numa.
  3323. * This will avoid cache misses that happen while accessing slabp (which
  3324. * is per page memory reference) to get nodeid. Instead use a global
  3325. * variable to skip the call, which is mostly likely to be present in
  3326. * the cache.
  3327. */
  3328. if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
  3329. return;
  3330. if (likely(ac->avail < ac->limit)) {
  3331. STATS_INC_FREEHIT(cachep);
  3332. } else {
  3333. STATS_INC_FREEMISS(cachep);
  3334. cache_flusharray(cachep, ac);
  3335. }
  3336. ac_put_obj(cachep, ac, objp);
  3337. }
  3338. /**
  3339. * kmem_cache_alloc - Allocate an object
  3340. * @cachep: The cache to allocate from.
  3341. * @flags: See kmalloc().
  3342. *
  3343. * Allocate an object from this cache. The flags are only relevant
  3344. * if the cache has no available objects.
  3345. */
  3346. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  3347. {
  3348. void *ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
  3349. trace_kmem_cache_alloc(_RET_IP_, ret,
  3350. cachep->object_size, cachep->size, flags);
  3351. return ret;
  3352. }
  3353. EXPORT_SYMBOL(kmem_cache_alloc);
  3354. #ifdef CONFIG_TRACING
  3355. void *
  3356. kmem_cache_alloc_trace(size_t size, struct kmem_cache *cachep, gfp_t flags)
  3357. {
  3358. void *ret;
  3359. ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
  3360. trace_kmalloc(_RET_IP_, ret,
  3361. size, slab_buffer_size(cachep), flags);
  3362. return ret;
  3363. }
  3364. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  3365. #endif
  3366. #ifdef CONFIG_NUMA
  3367. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  3368. {
  3369. void *ret = __cache_alloc_node(cachep, flags, nodeid,
  3370. __builtin_return_address(0));
  3371. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  3372. cachep->object_size, cachep->size,
  3373. flags, nodeid);
  3374. return ret;
  3375. }
  3376. EXPORT_SYMBOL(kmem_cache_alloc_node);
  3377. #ifdef CONFIG_TRACING
  3378. void *kmem_cache_alloc_node_trace(size_t size,
  3379. struct kmem_cache *cachep,
  3380. gfp_t flags,
  3381. int nodeid)
  3382. {
  3383. void *ret;
  3384. ret = __cache_alloc_node(cachep, flags, nodeid,
  3385. __builtin_return_address(0));
  3386. trace_kmalloc_node(_RET_IP_, ret,
  3387. size, slab_buffer_size(cachep),
  3388. flags, nodeid);
  3389. return ret;
  3390. }
  3391. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  3392. #endif
  3393. static __always_inline void *
  3394. __do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
  3395. {
  3396. struct kmem_cache *cachep;
  3397. cachep = kmem_find_general_cachep(size, flags);
  3398. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3399. return cachep;
  3400. return kmem_cache_alloc_node_trace(size, cachep, flags, node);
  3401. }
  3402. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3403. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3404. {
  3405. return __do_kmalloc_node(size, flags, node,
  3406. __builtin_return_address(0));
  3407. }
  3408. EXPORT_SYMBOL(__kmalloc_node);
  3409. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  3410. int node, unsigned long caller)
  3411. {
  3412. return __do_kmalloc_node(size, flags, node, (void *)caller);
  3413. }
  3414. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  3415. #else
  3416. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3417. {
  3418. return __do_kmalloc_node(size, flags, node, NULL);
  3419. }
  3420. EXPORT_SYMBOL(__kmalloc_node);
  3421. #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
  3422. #endif /* CONFIG_NUMA */
  3423. /**
  3424. * __do_kmalloc - allocate memory
  3425. * @size: how many bytes of memory are required.
  3426. * @flags: the type of memory to allocate (see kmalloc).
  3427. * @caller: function caller for debug tracking of the caller
  3428. */
  3429. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  3430. void *caller)
  3431. {
  3432. struct kmem_cache *cachep;
  3433. void *ret;
  3434. /* If you want to save a few bytes .text space: replace
  3435. * __ with kmem_.
  3436. * Then kmalloc uses the uninlined functions instead of the inline
  3437. * functions.
  3438. */
  3439. cachep = __find_general_cachep(size, flags);
  3440. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3441. return cachep;
  3442. ret = __cache_alloc(cachep, flags, caller);
  3443. trace_kmalloc((unsigned long) caller, ret,
  3444. size, cachep->size, flags);
  3445. return ret;
  3446. }
  3447. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3448. void *__kmalloc(size_t size, gfp_t flags)
  3449. {
  3450. return __do_kmalloc(size, flags, __builtin_return_address(0));
  3451. }
  3452. EXPORT_SYMBOL(__kmalloc);
  3453. void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
  3454. {
  3455. return __do_kmalloc(size, flags, (void *)caller);
  3456. }
  3457. EXPORT_SYMBOL(__kmalloc_track_caller);
  3458. #else
  3459. void *__kmalloc(size_t size, gfp_t flags)
  3460. {
  3461. return __do_kmalloc(size, flags, NULL);
  3462. }
  3463. EXPORT_SYMBOL(__kmalloc);
  3464. #endif
  3465. /**
  3466. * kmem_cache_free - Deallocate an object
  3467. * @cachep: The cache the allocation was from.
  3468. * @objp: The previously allocated object.
  3469. *
  3470. * Free an object which was previously allocated from this
  3471. * cache.
  3472. */
  3473. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  3474. {
  3475. unsigned long flags;
  3476. local_irq_save(flags);
  3477. debug_check_no_locks_freed(objp, cachep->object_size);
  3478. if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
  3479. debug_check_no_obj_freed(objp, cachep->object_size);
  3480. __cache_free(cachep, objp, __builtin_return_address(0));
  3481. local_irq_restore(flags);
  3482. trace_kmem_cache_free(_RET_IP_, objp);
  3483. }
  3484. EXPORT_SYMBOL(kmem_cache_free);
  3485. /**
  3486. * kfree - free previously allocated memory
  3487. * @objp: pointer returned by kmalloc.
  3488. *
  3489. * If @objp is NULL, no operation is performed.
  3490. *
  3491. * Don't free memory not originally allocated by kmalloc()
  3492. * or you will run into trouble.
  3493. */
  3494. void kfree(const void *objp)
  3495. {
  3496. struct kmem_cache *c;
  3497. unsigned long flags;
  3498. trace_kfree(_RET_IP_, objp);
  3499. if (unlikely(ZERO_OR_NULL_PTR(objp)))
  3500. return;
  3501. local_irq_save(flags);
  3502. kfree_debugcheck(objp);
  3503. c = virt_to_cache(objp);
  3504. debug_check_no_locks_freed(objp, c->object_size);
  3505. debug_check_no_obj_freed(objp, c->object_size);
  3506. __cache_free(c, (void *)objp, __builtin_return_address(0));
  3507. local_irq_restore(flags);
  3508. }
  3509. EXPORT_SYMBOL(kfree);
  3510. unsigned int kmem_cache_size(struct kmem_cache *cachep)
  3511. {
  3512. return cachep->object_size;
  3513. }
  3514. EXPORT_SYMBOL(kmem_cache_size);
  3515. /*
  3516. * This initializes kmem_list3 or resizes various caches for all nodes.
  3517. */
  3518. static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
  3519. {
  3520. int node;
  3521. struct kmem_list3 *l3;
  3522. struct array_cache *new_shared;
  3523. struct array_cache **new_alien = NULL;
  3524. for_each_online_node(node) {
  3525. if (use_alien_caches) {
  3526. new_alien = alloc_alien_cache(node, cachep->limit, gfp);
  3527. if (!new_alien)
  3528. goto fail;
  3529. }
  3530. new_shared = NULL;
  3531. if (cachep->shared) {
  3532. new_shared = alloc_arraycache(node,
  3533. cachep->shared*cachep->batchcount,
  3534. 0xbaadf00d, gfp);
  3535. if (!new_shared) {
  3536. free_alien_cache(new_alien);
  3537. goto fail;
  3538. }
  3539. }
  3540. l3 = cachep->nodelists[node];
  3541. if (l3) {
  3542. struct array_cache *shared = l3->shared;
  3543. spin_lock_irq(&l3->list_lock);
  3544. if (shared)
  3545. free_block(cachep, shared->entry,
  3546. shared->avail, node);
  3547. l3->shared = new_shared;
  3548. if (!l3->alien) {
  3549. l3->alien = new_alien;
  3550. new_alien = NULL;
  3551. }
  3552. l3->free_limit = (1 + nr_cpus_node(node)) *
  3553. cachep->batchcount + cachep->num;
  3554. spin_unlock_irq(&l3->list_lock);
  3555. kfree(shared);
  3556. free_alien_cache(new_alien);
  3557. continue;
  3558. }
  3559. l3 = kmalloc_node(sizeof(struct kmem_list3), gfp, node);
  3560. if (!l3) {
  3561. free_alien_cache(new_alien);
  3562. kfree(new_shared);
  3563. goto fail;
  3564. }
  3565. kmem_list3_init(l3);
  3566. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  3567. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  3568. l3->shared = new_shared;
  3569. l3->alien = new_alien;
  3570. l3->free_limit = (1 + nr_cpus_node(node)) *
  3571. cachep->batchcount + cachep->num;
  3572. cachep->nodelists[node] = l3;
  3573. }
  3574. return 0;
  3575. fail:
  3576. if (!cachep->list.next) {
  3577. /* Cache is not active yet. Roll back what we did */
  3578. node--;
  3579. while (node >= 0) {
  3580. if (cachep->nodelists[node]) {
  3581. l3 = cachep->nodelists[node];
  3582. kfree(l3->shared);
  3583. free_alien_cache(l3->alien);
  3584. kfree(l3);
  3585. cachep->nodelists[node] = NULL;
  3586. }
  3587. node--;
  3588. }
  3589. }
  3590. return -ENOMEM;
  3591. }
  3592. struct ccupdate_struct {
  3593. struct kmem_cache *cachep;
  3594. struct array_cache *new[0];
  3595. };
  3596. static void do_ccupdate_local(void *info)
  3597. {
  3598. struct ccupdate_struct *new = info;
  3599. struct array_cache *old;
  3600. check_irq_off();
  3601. old = cpu_cache_get(new->cachep);
  3602. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  3603. new->new[smp_processor_id()] = old;
  3604. }
  3605. /* Always called with the slab_mutex held */
  3606. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3607. int batchcount, int shared, gfp_t gfp)
  3608. {
  3609. struct ccupdate_struct *new;
  3610. int i;
  3611. new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
  3612. gfp);
  3613. if (!new)
  3614. return -ENOMEM;
  3615. for_each_online_cpu(i) {
  3616. new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
  3617. batchcount, gfp);
  3618. if (!new->new[i]) {
  3619. for (i--; i >= 0; i--)
  3620. kfree(new->new[i]);
  3621. kfree(new);
  3622. return -ENOMEM;
  3623. }
  3624. }
  3625. new->cachep = cachep;
  3626. on_each_cpu(do_ccupdate_local, (void *)new, 1);
  3627. check_irq_on();
  3628. cachep->batchcount = batchcount;
  3629. cachep->limit = limit;
  3630. cachep->shared = shared;
  3631. for_each_online_cpu(i) {
  3632. struct array_cache *ccold = new->new[i];
  3633. if (!ccold)
  3634. continue;
  3635. spin_lock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
  3636. free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
  3637. spin_unlock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
  3638. kfree(ccold);
  3639. }
  3640. kfree(new);
  3641. return alloc_kmemlist(cachep, gfp);
  3642. }
  3643. /* Called with slab_mutex held always */
  3644. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
  3645. {
  3646. int err;
  3647. int limit, shared;
  3648. /*
  3649. * The head array serves three purposes:
  3650. * - create a LIFO ordering, i.e. return objects that are cache-warm
  3651. * - reduce the number of spinlock operations.
  3652. * - reduce the number of linked list operations on the slab and
  3653. * bufctl chains: array operations are cheaper.
  3654. * The numbers are guessed, we should auto-tune as described by
  3655. * Bonwick.
  3656. */
  3657. if (cachep->size > 131072)
  3658. limit = 1;
  3659. else if (cachep->size > PAGE_SIZE)
  3660. limit = 8;
  3661. else if (cachep->size > 1024)
  3662. limit = 24;
  3663. else if (cachep->size > 256)
  3664. limit = 54;
  3665. else
  3666. limit = 120;
  3667. /*
  3668. * CPU bound tasks (e.g. network routing) can exhibit cpu bound
  3669. * allocation behaviour: Most allocs on one cpu, most free operations
  3670. * on another cpu. For these cases, an efficient object passing between
  3671. * cpus is necessary. This is provided by a shared array. The array
  3672. * replaces Bonwick's magazine layer.
  3673. * On uniprocessor, it's functionally equivalent (but less efficient)
  3674. * to a larger limit. Thus disabled by default.
  3675. */
  3676. shared = 0;
  3677. if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
  3678. shared = 8;
  3679. #if DEBUG
  3680. /*
  3681. * With debugging enabled, large batchcount lead to excessively long
  3682. * periods with disabled local interrupts. Limit the batchcount
  3683. */
  3684. if (limit > 32)
  3685. limit = 32;
  3686. #endif
  3687. err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
  3688. if (err)
  3689. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  3690. cachep->name, -err);
  3691. return err;
  3692. }
  3693. /*
  3694. * Drain an array if it contains any elements taking the l3 lock only if
  3695. * necessary. Note that the l3 listlock also protects the array_cache
  3696. * if drain_array() is used on the shared array.
  3697. */
  3698. static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  3699. struct array_cache *ac, int force, int node)
  3700. {
  3701. int tofree;
  3702. if (!ac || !ac->avail)
  3703. return;
  3704. if (ac->touched && !force) {
  3705. ac->touched = 0;
  3706. } else {
  3707. spin_lock_irq(&l3->list_lock);
  3708. if (ac->avail) {
  3709. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  3710. if (tofree > ac->avail)
  3711. tofree = (ac->avail + 1) / 2;
  3712. free_block(cachep, ac->entry, tofree, node);
  3713. ac->avail -= tofree;
  3714. memmove(ac->entry, &(ac->entry[tofree]),
  3715. sizeof(void *) * ac->avail);
  3716. }
  3717. spin_unlock_irq(&l3->list_lock);
  3718. }
  3719. }
  3720. /**
  3721. * cache_reap - Reclaim memory from caches.
  3722. * @w: work descriptor
  3723. *
  3724. * Called from workqueue/eventd every few seconds.
  3725. * Purpose:
  3726. * - clear the per-cpu caches for this CPU.
  3727. * - return freeable pages to the main free memory pool.
  3728. *
  3729. * If we cannot acquire the cache chain mutex then just give up - we'll try
  3730. * again on the next iteration.
  3731. */
  3732. static void cache_reap(struct work_struct *w)
  3733. {
  3734. struct kmem_cache *searchp;
  3735. struct kmem_list3 *l3;
  3736. int node = numa_mem_id();
  3737. struct delayed_work *work = to_delayed_work(w);
  3738. if (!mutex_trylock(&slab_mutex))
  3739. /* Give up. Setup the next iteration. */
  3740. goto out;
  3741. list_for_each_entry(searchp, &slab_caches, list) {
  3742. check_irq_on();
  3743. /*
  3744. * We only take the l3 lock if absolutely necessary and we
  3745. * have established with reasonable certainty that
  3746. * we can do some work if the lock was obtained.
  3747. */
  3748. l3 = searchp->nodelists[node];
  3749. reap_alien(searchp, l3);
  3750. drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
  3751. /*
  3752. * These are racy checks but it does not matter
  3753. * if we skip one check or scan twice.
  3754. */
  3755. if (time_after(l3->next_reap, jiffies))
  3756. goto next;
  3757. l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
  3758. drain_array(searchp, l3, l3->shared, 0, node);
  3759. if (l3->free_touched)
  3760. l3->free_touched = 0;
  3761. else {
  3762. int freed;
  3763. freed = drain_freelist(searchp, l3, (l3->free_limit +
  3764. 5 * searchp->num - 1) / (5 * searchp->num));
  3765. STATS_ADD_REAPED(searchp, freed);
  3766. }
  3767. next:
  3768. cond_resched();
  3769. }
  3770. check_irq_on();
  3771. mutex_unlock(&slab_mutex);
  3772. next_reap_node();
  3773. out:
  3774. /* Set up the next iteration */
  3775. schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
  3776. }
  3777. #ifdef CONFIG_SLABINFO
  3778. static void print_slabinfo_header(struct seq_file *m)
  3779. {
  3780. /*
  3781. * Output format version, so at least we can change it
  3782. * without _too_ many complaints.
  3783. */
  3784. #if STATS
  3785. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  3786. #else
  3787. seq_puts(m, "slabinfo - version: 2.1\n");
  3788. #endif
  3789. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  3790. "<objperslab> <pagesperslab>");
  3791. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  3792. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  3793. #if STATS
  3794. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  3795. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  3796. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  3797. #endif
  3798. seq_putc(m, '\n');
  3799. }
  3800. static void *s_start(struct seq_file *m, loff_t *pos)
  3801. {
  3802. loff_t n = *pos;
  3803. mutex_lock(&slab_mutex);
  3804. if (!n)
  3805. print_slabinfo_header(m);
  3806. return seq_list_start(&slab_caches, *pos);
  3807. }
  3808. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  3809. {
  3810. return seq_list_next(p, &slab_caches, pos);
  3811. }
  3812. static void s_stop(struct seq_file *m, void *p)
  3813. {
  3814. mutex_unlock(&slab_mutex);
  3815. }
  3816. static int s_show(struct seq_file *m, void *p)
  3817. {
  3818. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
  3819. struct slab *slabp;
  3820. unsigned long active_objs;
  3821. unsigned long num_objs;
  3822. unsigned long active_slabs = 0;
  3823. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  3824. const char *name;
  3825. char *error = NULL;
  3826. int node;
  3827. struct kmem_list3 *l3;
  3828. active_objs = 0;
  3829. num_slabs = 0;
  3830. for_each_online_node(node) {
  3831. l3 = cachep->nodelists[node];
  3832. if (!l3)
  3833. continue;
  3834. check_irq_on();
  3835. spin_lock_irq(&l3->list_lock);
  3836. list_for_each_entry(slabp, &l3->slabs_full, list) {
  3837. if (slabp->inuse != cachep->num && !error)
  3838. error = "slabs_full accounting error";
  3839. active_objs += cachep->num;
  3840. active_slabs++;
  3841. }
  3842. list_for_each_entry(slabp, &l3->slabs_partial, list) {
  3843. if (slabp->inuse == cachep->num && !error)
  3844. error = "slabs_partial inuse accounting error";
  3845. if (!slabp->inuse && !error)
  3846. error = "slabs_partial/inuse accounting error";
  3847. active_objs += slabp->inuse;
  3848. active_slabs++;
  3849. }
  3850. list_for_each_entry(slabp, &l3->slabs_free, list) {
  3851. if (slabp->inuse && !error)
  3852. error = "slabs_free/inuse accounting error";
  3853. num_slabs++;
  3854. }
  3855. free_objects += l3->free_objects;
  3856. if (l3->shared)
  3857. shared_avail += l3->shared->avail;
  3858. spin_unlock_irq(&l3->list_lock);
  3859. }
  3860. num_slabs += active_slabs;
  3861. num_objs = num_slabs * cachep->num;
  3862. if (num_objs - active_objs != free_objects && !error)
  3863. error = "free_objects accounting error";
  3864. name = cachep->name;
  3865. if (error)
  3866. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  3867. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  3868. name, active_objs, num_objs, cachep->size,
  3869. cachep->num, (1 << cachep->gfporder));
  3870. seq_printf(m, " : tunables %4u %4u %4u",
  3871. cachep->limit, cachep->batchcount, cachep->shared);
  3872. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  3873. active_slabs, num_slabs, shared_avail);
  3874. #if STATS
  3875. { /* list3 stats */
  3876. unsigned long high = cachep->high_mark;
  3877. unsigned long allocs = cachep->num_allocations;
  3878. unsigned long grown = cachep->grown;
  3879. unsigned long reaped = cachep->reaped;
  3880. unsigned long errors = cachep->errors;
  3881. unsigned long max_freeable = cachep->max_freeable;
  3882. unsigned long node_allocs = cachep->node_allocs;
  3883. unsigned long node_frees = cachep->node_frees;
  3884. unsigned long overflows = cachep->node_overflow;
  3885. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
  3886. "%4lu %4lu %4lu %4lu %4lu",
  3887. allocs, high, grown,
  3888. reaped, errors, max_freeable, node_allocs,
  3889. node_frees, overflows);
  3890. }
  3891. /* cpu stats */
  3892. {
  3893. unsigned long allochit = atomic_read(&cachep->allochit);
  3894. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  3895. unsigned long freehit = atomic_read(&cachep->freehit);
  3896. unsigned long freemiss = atomic_read(&cachep->freemiss);
  3897. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  3898. allochit, allocmiss, freehit, freemiss);
  3899. }
  3900. #endif
  3901. seq_putc(m, '\n');
  3902. return 0;
  3903. }
  3904. /*
  3905. * slabinfo_op - iterator that generates /proc/slabinfo
  3906. *
  3907. * Output layout:
  3908. * cache-name
  3909. * num-active-objs
  3910. * total-objs
  3911. * object size
  3912. * num-active-slabs
  3913. * total-slabs
  3914. * num-pages-per-slab
  3915. * + further values on SMP and with statistics enabled
  3916. */
  3917. static const struct seq_operations slabinfo_op = {
  3918. .start = s_start,
  3919. .next = s_next,
  3920. .stop = s_stop,
  3921. .show = s_show,
  3922. };
  3923. #define MAX_SLABINFO_WRITE 128
  3924. /**
  3925. * slabinfo_write - Tuning for the slab allocator
  3926. * @file: unused
  3927. * @buffer: user buffer
  3928. * @count: data length
  3929. * @ppos: unused
  3930. */
  3931. static ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  3932. size_t count, loff_t *ppos)
  3933. {
  3934. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  3935. int limit, batchcount, shared, res;
  3936. struct kmem_cache *cachep;
  3937. if (count > MAX_SLABINFO_WRITE)
  3938. return -EINVAL;
  3939. if (copy_from_user(&kbuf, buffer, count))
  3940. return -EFAULT;
  3941. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3942. tmp = strchr(kbuf, ' ');
  3943. if (!tmp)
  3944. return -EINVAL;
  3945. *tmp = '\0';
  3946. tmp++;
  3947. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3948. return -EINVAL;
  3949. /* Find the cache in the chain of caches. */
  3950. mutex_lock(&slab_mutex);
  3951. res = -EINVAL;
  3952. list_for_each_entry(cachep, &slab_caches, list) {
  3953. if (!strcmp(cachep->name, kbuf)) {
  3954. if (limit < 1 || batchcount < 1 ||
  3955. batchcount > limit || shared < 0) {
  3956. res = 0;
  3957. } else {
  3958. res = do_tune_cpucache(cachep, limit,
  3959. batchcount, shared,
  3960. GFP_KERNEL);
  3961. }
  3962. break;
  3963. }
  3964. }
  3965. mutex_unlock(&slab_mutex);
  3966. if (res >= 0)
  3967. res = count;
  3968. return res;
  3969. }
  3970. static int slabinfo_open(struct inode *inode, struct file *file)
  3971. {
  3972. return seq_open(file, &slabinfo_op);
  3973. }
  3974. static const struct file_operations proc_slabinfo_operations = {
  3975. .open = slabinfo_open,
  3976. .read = seq_read,
  3977. .write = slabinfo_write,
  3978. .llseek = seq_lseek,
  3979. .release = seq_release,
  3980. };
  3981. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3982. static void *leaks_start(struct seq_file *m, loff_t *pos)
  3983. {
  3984. mutex_lock(&slab_mutex);
  3985. return seq_list_start(&slab_caches, *pos);
  3986. }
  3987. static inline int add_caller(unsigned long *n, unsigned long v)
  3988. {
  3989. unsigned long *p;
  3990. int l;
  3991. if (!v)
  3992. return 1;
  3993. l = n[1];
  3994. p = n + 2;
  3995. while (l) {
  3996. int i = l/2;
  3997. unsigned long *q = p + 2 * i;
  3998. if (*q == v) {
  3999. q[1]++;
  4000. return 1;
  4001. }
  4002. if (*q > v) {
  4003. l = i;
  4004. } else {
  4005. p = q + 2;
  4006. l -= i + 1;
  4007. }
  4008. }
  4009. if (++n[1] == n[0])
  4010. return 0;
  4011. memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  4012. p[0] = v;
  4013. p[1] = 1;
  4014. return 1;
  4015. }
  4016. static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
  4017. {
  4018. void *p;
  4019. int i;
  4020. if (n[0] == n[1])
  4021. return;
  4022. for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
  4023. if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
  4024. continue;
  4025. if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
  4026. return;
  4027. }
  4028. }
  4029. static void show_symbol(struct seq_file *m, unsigned long address)
  4030. {
  4031. #ifdef CONFIG_KALLSYMS
  4032. unsigned long offset, size;
  4033. char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
  4034. if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
  4035. seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
  4036. if (modname[0])
  4037. seq_printf(m, " [%s]", modname);
  4038. return;
  4039. }
  4040. #endif
  4041. seq_printf(m, "%p", (void *)address);
  4042. }
  4043. static int leaks_show(struct seq_file *m, void *p)
  4044. {
  4045. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
  4046. struct slab *slabp;
  4047. struct kmem_list3 *l3;
  4048. const char *name;
  4049. unsigned long *n = m->private;
  4050. int node;
  4051. int i;
  4052. if (!(cachep->flags & SLAB_STORE_USER))
  4053. return 0;
  4054. if (!(cachep->flags & SLAB_RED_ZONE))
  4055. return 0;
  4056. /* OK, we can do it */
  4057. n[1] = 0;
  4058. for_each_online_node(node) {
  4059. l3 = cachep->nodelists[node];
  4060. if (!l3)
  4061. continue;
  4062. check_irq_on();
  4063. spin_lock_irq(&l3->list_lock);
  4064. list_for_each_entry(slabp, &l3->slabs_full, list)
  4065. handle_slab(n, cachep, slabp);
  4066. list_for_each_entry(slabp, &l3->slabs_partial, list)
  4067. handle_slab(n, cachep, slabp);
  4068. spin_unlock_irq(&l3->list_lock);
  4069. }
  4070. name = cachep->name;
  4071. if (n[0] == n[1]) {
  4072. /* Increase the buffer size */
  4073. mutex_unlock(&slab_mutex);
  4074. m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
  4075. if (!m->private) {
  4076. /* Too bad, we are really out */
  4077. m->private = n;
  4078. mutex_lock(&slab_mutex);
  4079. return -ENOMEM;
  4080. }
  4081. *(unsigned long *)m->private = n[0] * 2;
  4082. kfree(n);
  4083. mutex_lock(&slab_mutex);
  4084. /* Now make sure this entry will be retried */
  4085. m->count = m->size;
  4086. return 0;
  4087. }
  4088. for (i = 0; i < n[1]; i++) {
  4089. seq_printf(m, "%s: %lu ", name, n[2*i+3]);
  4090. show_symbol(m, n[2*i+2]);
  4091. seq_putc(m, '\n');
  4092. }
  4093. return 0;
  4094. }
  4095. static const struct seq_operations slabstats_op = {
  4096. .start = leaks_start,
  4097. .next = s_next,
  4098. .stop = s_stop,
  4099. .show = leaks_show,
  4100. };
  4101. static int slabstats_open(struct inode *inode, struct file *file)
  4102. {
  4103. unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  4104. int ret = -ENOMEM;
  4105. if (n) {
  4106. ret = seq_open(file, &slabstats_op);
  4107. if (!ret) {
  4108. struct seq_file *m = file->private_data;
  4109. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  4110. m->private = n;
  4111. n = NULL;
  4112. }
  4113. kfree(n);
  4114. }
  4115. return ret;
  4116. }
  4117. static const struct file_operations proc_slabstats_operations = {
  4118. .open = slabstats_open,
  4119. .read = seq_read,
  4120. .llseek = seq_lseek,
  4121. .release = seq_release_private,
  4122. };
  4123. #endif
  4124. static int __init slab_proc_init(void)
  4125. {
  4126. proc_create("slabinfo",S_IWUSR|S_IRUSR,NULL,&proc_slabinfo_operations);
  4127. #ifdef CONFIG_DEBUG_SLAB_LEAK
  4128. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  4129. #endif
  4130. return 0;
  4131. }
  4132. module_init(slab_proc_init);
  4133. #endif
  4134. /**
  4135. * ksize - get the actual amount of memory allocated for a given object
  4136. * @objp: Pointer to the object
  4137. *
  4138. * kmalloc may internally round up allocations and return more memory
  4139. * than requested. ksize() can be used to determine the actual amount of
  4140. * memory allocated. The caller may use this additional memory, even though
  4141. * a smaller amount of memory was initially specified with the kmalloc call.
  4142. * The caller must guarantee that objp points to a valid object previously
  4143. * allocated with either kmalloc() or kmem_cache_alloc(). The object
  4144. * must not be freed during the duration of the call.
  4145. */
  4146. size_t ksize(const void *objp)
  4147. {
  4148. BUG_ON(!objp);
  4149. if (unlikely(objp == ZERO_SIZE_PTR))
  4150. return 0;
  4151. return virt_to_cache(objp)->object_size;
  4152. }
  4153. EXPORT_SYMBOL(ksize);