slab.c 114 KB

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