slab.c 116 KB

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