slab.c 121 KB

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