slab.c 116 KB

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