slab.c 116 KB

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