slab.c 114 KB

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