slab.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503
  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 <= KMALLOC_SHIFT_HIGH; 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);
  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 inline int slabs_tofree(struct kmem_cache *cachep,
  1029. struct kmem_cache_node *n)
  1030. {
  1031. return (n->free_objects + cachep->num - 1) / cachep->num;
  1032. }
  1033. static void __cpuinit cpuup_canceled(long cpu)
  1034. {
  1035. struct kmem_cache *cachep;
  1036. struct kmem_cache_node *n = NULL;
  1037. int node = cpu_to_mem(cpu);
  1038. const struct cpumask *mask = cpumask_of_node(node);
  1039. list_for_each_entry(cachep, &slab_caches, list) {
  1040. struct array_cache *nc;
  1041. struct array_cache *shared;
  1042. struct array_cache **alien;
  1043. /* cpu is dead; no one can alloc from it. */
  1044. nc = cachep->array[cpu];
  1045. cachep->array[cpu] = NULL;
  1046. n = cachep->node[node];
  1047. if (!n)
  1048. goto free_array_cache;
  1049. spin_lock_irq(&n->list_lock);
  1050. /* Free limit for this kmem_cache_node */
  1051. n->free_limit -= cachep->batchcount;
  1052. if (nc)
  1053. free_block(cachep, nc->entry, nc->avail, node);
  1054. if (!cpumask_empty(mask)) {
  1055. spin_unlock_irq(&n->list_lock);
  1056. goto free_array_cache;
  1057. }
  1058. shared = n->shared;
  1059. if (shared) {
  1060. free_block(cachep, shared->entry,
  1061. shared->avail, node);
  1062. n->shared = NULL;
  1063. }
  1064. alien = n->alien;
  1065. n->alien = NULL;
  1066. spin_unlock_irq(&n->list_lock);
  1067. kfree(shared);
  1068. if (alien) {
  1069. drain_alien_cache(cachep, alien);
  1070. free_alien_cache(alien);
  1071. }
  1072. free_array_cache:
  1073. kfree(nc);
  1074. }
  1075. /*
  1076. * In the previous loop, all the objects were freed to
  1077. * the respective cache's slabs, now we can go ahead and
  1078. * shrink each nodelist to its limit.
  1079. */
  1080. list_for_each_entry(cachep, &slab_caches, list) {
  1081. n = cachep->node[node];
  1082. if (!n)
  1083. continue;
  1084. drain_freelist(cachep, n, slabs_tofree(cachep, n));
  1085. }
  1086. }
  1087. static int __cpuinit cpuup_prepare(long cpu)
  1088. {
  1089. struct kmem_cache *cachep;
  1090. struct kmem_cache_node *n = NULL;
  1091. int node = cpu_to_mem(cpu);
  1092. int err;
  1093. /*
  1094. * We need to do this right in the beginning since
  1095. * alloc_arraycache's are going to use this list.
  1096. * kmalloc_node allows us to add the slab to the right
  1097. * kmem_cache_node and not this cpu's kmem_cache_node
  1098. */
  1099. err = init_cache_node_node(node);
  1100. if (err < 0)
  1101. goto bad;
  1102. /*
  1103. * Now we can go ahead with allocating the shared arrays and
  1104. * array caches
  1105. */
  1106. list_for_each_entry(cachep, &slab_caches, list) {
  1107. struct array_cache *nc;
  1108. struct array_cache *shared = NULL;
  1109. struct array_cache **alien = NULL;
  1110. nc = alloc_arraycache(node, cachep->limit,
  1111. cachep->batchcount, GFP_KERNEL);
  1112. if (!nc)
  1113. goto bad;
  1114. if (cachep->shared) {
  1115. shared = alloc_arraycache(node,
  1116. cachep->shared * cachep->batchcount,
  1117. 0xbaadf00d, GFP_KERNEL);
  1118. if (!shared) {
  1119. kfree(nc);
  1120. goto bad;
  1121. }
  1122. }
  1123. if (use_alien_caches) {
  1124. alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
  1125. if (!alien) {
  1126. kfree(shared);
  1127. kfree(nc);
  1128. goto bad;
  1129. }
  1130. }
  1131. cachep->array[cpu] = nc;
  1132. n = cachep->node[node];
  1133. BUG_ON(!n);
  1134. spin_lock_irq(&n->list_lock);
  1135. if (!n->shared) {
  1136. /*
  1137. * We are serialised from CPU_DEAD or
  1138. * CPU_UP_CANCELLED by the cpucontrol lock
  1139. */
  1140. n->shared = shared;
  1141. shared = NULL;
  1142. }
  1143. #ifdef CONFIG_NUMA
  1144. if (!n->alien) {
  1145. n->alien = alien;
  1146. alien = NULL;
  1147. }
  1148. #endif
  1149. spin_unlock_irq(&n->list_lock);
  1150. kfree(shared);
  1151. free_alien_cache(alien);
  1152. if (cachep->flags & SLAB_DEBUG_OBJECTS)
  1153. slab_set_debugobj_lock_classes_node(cachep, node);
  1154. else if (!OFF_SLAB(cachep) &&
  1155. !(cachep->flags & SLAB_DESTROY_BY_RCU))
  1156. on_slab_lock_classes_node(cachep, node);
  1157. }
  1158. init_node_lock_keys(node);
  1159. return 0;
  1160. bad:
  1161. cpuup_canceled(cpu);
  1162. return -ENOMEM;
  1163. }
  1164. static int __cpuinit cpuup_callback(struct notifier_block *nfb,
  1165. unsigned long action, void *hcpu)
  1166. {
  1167. long cpu = (long)hcpu;
  1168. int err = 0;
  1169. switch (action) {
  1170. case CPU_UP_PREPARE:
  1171. case CPU_UP_PREPARE_FROZEN:
  1172. mutex_lock(&slab_mutex);
  1173. err = cpuup_prepare(cpu);
  1174. mutex_unlock(&slab_mutex);
  1175. break;
  1176. case CPU_ONLINE:
  1177. case CPU_ONLINE_FROZEN:
  1178. start_cpu_timer(cpu);
  1179. break;
  1180. #ifdef CONFIG_HOTPLUG_CPU
  1181. case CPU_DOWN_PREPARE:
  1182. case CPU_DOWN_PREPARE_FROZEN:
  1183. /*
  1184. * Shutdown cache reaper. Note that the slab_mutex is
  1185. * held so that if cache_reap() is invoked it cannot do
  1186. * anything expensive but will only modify reap_work
  1187. * and reschedule the timer.
  1188. */
  1189. cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
  1190. /* Now the cache_reaper is guaranteed to be not running. */
  1191. per_cpu(slab_reap_work, cpu).work.func = NULL;
  1192. break;
  1193. case CPU_DOWN_FAILED:
  1194. case CPU_DOWN_FAILED_FROZEN:
  1195. start_cpu_timer(cpu);
  1196. break;
  1197. case CPU_DEAD:
  1198. case CPU_DEAD_FROZEN:
  1199. /*
  1200. * Even if all the cpus of a node are down, we don't free the
  1201. * kmem_cache_node of any cache. This to avoid a race between
  1202. * cpu_down, and a kmalloc allocation from another cpu for
  1203. * memory from the node of the cpu going down. The node
  1204. * structure is usually allocated from kmem_cache_create() and
  1205. * gets destroyed at kmem_cache_destroy().
  1206. */
  1207. /* fall through */
  1208. #endif
  1209. case CPU_UP_CANCELED:
  1210. case CPU_UP_CANCELED_FROZEN:
  1211. mutex_lock(&slab_mutex);
  1212. cpuup_canceled(cpu);
  1213. mutex_unlock(&slab_mutex);
  1214. break;
  1215. }
  1216. return notifier_from_errno(err);
  1217. }
  1218. static struct notifier_block __cpuinitdata cpucache_notifier = {
  1219. &cpuup_callback, NULL, 0
  1220. };
  1221. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  1222. /*
  1223. * Drains freelist for a node on each slab cache, used for memory hot-remove.
  1224. * Returns -EBUSY if all objects cannot be drained so that the node is not
  1225. * removed.
  1226. *
  1227. * Must hold slab_mutex.
  1228. */
  1229. static int __meminit drain_cache_node_node(int node)
  1230. {
  1231. struct kmem_cache *cachep;
  1232. int ret = 0;
  1233. list_for_each_entry(cachep, &slab_caches, list) {
  1234. struct kmem_cache_node *n;
  1235. n = cachep->node[node];
  1236. if (!n)
  1237. continue;
  1238. drain_freelist(cachep, n, slabs_tofree(cachep, n));
  1239. if (!list_empty(&n->slabs_full) ||
  1240. !list_empty(&n->slabs_partial)) {
  1241. ret = -EBUSY;
  1242. break;
  1243. }
  1244. }
  1245. return ret;
  1246. }
  1247. static int __meminit slab_memory_callback(struct notifier_block *self,
  1248. unsigned long action, void *arg)
  1249. {
  1250. struct memory_notify *mnb = arg;
  1251. int ret = 0;
  1252. int nid;
  1253. nid = mnb->status_change_nid;
  1254. if (nid < 0)
  1255. goto out;
  1256. switch (action) {
  1257. case MEM_GOING_ONLINE:
  1258. mutex_lock(&slab_mutex);
  1259. ret = init_cache_node_node(nid);
  1260. mutex_unlock(&slab_mutex);
  1261. break;
  1262. case MEM_GOING_OFFLINE:
  1263. mutex_lock(&slab_mutex);
  1264. ret = drain_cache_node_node(nid);
  1265. mutex_unlock(&slab_mutex);
  1266. break;
  1267. case MEM_ONLINE:
  1268. case MEM_OFFLINE:
  1269. case MEM_CANCEL_ONLINE:
  1270. case MEM_CANCEL_OFFLINE:
  1271. break;
  1272. }
  1273. out:
  1274. return notifier_from_errno(ret);
  1275. }
  1276. #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
  1277. /*
  1278. * swap the static kmem_cache_node with kmalloced memory
  1279. */
  1280. static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
  1281. int nodeid)
  1282. {
  1283. struct kmem_cache_node *ptr;
  1284. ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
  1285. BUG_ON(!ptr);
  1286. memcpy(ptr, list, sizeof(struct kmem_cache_node));
  1287. /*
  1288. * Do not assume that spinlocks can be initialized via memcpy:
  1289. */
  1290. spin_lock_init(&ptr->list_lock);
  1291. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  1292. cachep->node[nodeid] = ptr;
  1293. }
  1294. /*
  1295. * For setting up all the kmem_cache_node for cache whose buffer_size is same as
  1296. * size of kmem_cache_node.
  1297. */
  1298. static void __init set_up_node(struct kmem_cache *cachep, int index)
  1299. {
  1300. int node;
  1301. for_each_online_node(node) {
  1302. cachep->node[node] = &init_kmem_cache_node[index + node];
  1303. cachep->node[node]->next_reap = jiffies +
  1304. REAPTIMEOUT_LIST3 +
  1305. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1306. }
  1307. }
  1308. /*
  1309. * The memory after the last cpu cache pointer is used for the
  1310. * the node pointer.
  1311. */
  1312. static void setup_node_pointer(struct kmem_cache *cachep)
  1313. {
  1314. cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
  1315. }
  1316. /*
  1317. * Initialisation. Called after the page allocator have been initialised and
  1318. * before smp_init().
  1319. */
  1320. void __init kmem_cache_init(void)
  1321. {
  1322. int i;
  1323. kmem_cache = &kmem_cache_boot;
  1324. setup_node_pointer(kmem_cache);
  1325. if (num_possible_nodes() == 1)
  1326. use_alien_caches = 0;
  1327. for (i = 0; i < NUM_INIT_LISTS; i++)
  1328. kmem_cache_node_init(&init_kmem_cache_node[i]);
  1329. set_up_node(kmem_cache, CACHE_CACHE);
  1330. /*
  1331. * Fragmentation resistance on low memory - only use bigger
  1332. * page orders on machines with more than 32MB of memory if
  1333. * not overridden on the command line.
  1334. */
  1335. if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
  1336. slab_max_order = SLAB_MAX_ORDER_HI;
  1337. /* Bootstrap is tricky, because several objects are allocated
  1338. * from caches that do not exist yet:
  1339. * 1) initialize the kmem_cache cache: it contains the struct
  1340. * kmem_cache structures of all caches, except kmem_cache itself:
  1341. * kmem_cache is statically allocated.
  1342. * Initially an __init data area is used for the head array and the
  1343. * kmem_cache_node structures, it's replaced with a kmalloc allocated
  1344. * array at the end of the bootstrap.
  1345. * 2) Create the first kmalloc cache.
  1346. * The struct kmem_cache for the new cache is allocated normally.
  1347. * An __init data area is used for the head array.
  1348. * 3) Create the remaining kmalloc caches, with minimally sized
  1349. * head arrays.
  1350. * 4) Replace the __init data head arrays for kmem_cache and the first
  1351. * kmalloc cache with kmalloc allocated arrays.
  1352. * 5) Replace the __init data for kmem_cache_node for kmem_cache and
  1353. * the other cache's with kmalloc allocated memory.
  1354. * 6) Resize the head arrays of the kmalloc caches to their final sizes.
  1355. */
  1356. /* 1) create the kmem_cache */
  1357. /*
  1358. * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
  1359. */
  1360. create_boot_cache(kmem_cache, "kmem_cache",
  1361. offsetof(struct kmem_cache, array[nr_cpu_ids]) +
  1362. nr_node_ids * sizeof(struct kmem_cache_node *),
  1363. SLAB_HWCACHE_ALIGN);
  1364. list_add(&kmem_cache->list, &slab_caches);
  1365. /* 2+3) create the kmalloc caches */
  1366. /*
  1367. * Initialize the caches that provide memory for the array cache and the
  1368. * kmem_cache_node structures first. Without this, further allocations will
  1369. * bug.
  1370. */
  1371. kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
  1372. kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
  1373. if (INDEX_AC != INDEX_NODE)
  1374. kmalloc_caches[INDEX_NODE] =
  1375. create_kmalloc_cache("kmalloc-node",
  1376. kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
  1377. slab_early_init = 0;
  1378. /* 4) Replace the bootstrap head arrays */
  1379. {
  1380. struct array_cache *ptr;
  1381. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1382. memcpy(ptr, cpu_cache_get(kmem_cache),
  1383. sizeof(struct arraycache_init));
  1384. /*
  1385. * Do not assume that spinlocks can be initialized via memcpy:
  1386. */
  1387. spin_lock_init(&ptr->lock);
  1388. kmem_cache->array[smp_processor_id()] = ptr;
  1389. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1390. BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
  1391. != &initarray_generic.cache);
  1392. memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
  1393. sizeof(struct arraycache_init));
  1394. /*
  1395. * Do not assume that spinlocks can be initialized via memcpy:
  1396. */
  1397. spin_lock_init(&ptr->lock);
  1398. kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
  1399. }
  1400. /* 5) Replace the bootstrap kmem_cache_node */
  1401. {
  1402. int nid;
  1403. for_each_online_node(nid) {
  1404. init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
  1405. init_list(kmalloc_caches[INDEX_AC],
  1406. &init_kmem_cache_node[SIZE_AC + nid], nid);
  1407. if (INDEX_AC != INDEX_NODE) {
  1408. init_list(kmalloc_caches[INDEX_NODE],
  1409. &init_kmem_cache_node[SIZE_NODE + nid], nid);
  1410. }
  1411. }
  1412. }
  1413. create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
  1414. }
  1415. void __init kmem_cache_init_late(void)
  1416. {
  1417. struct kmem_cache *cachep;
  1418. slab_state = UP;
  1419. /* 6) resize the head arrays to their final sizes */
  1420. mutex_lock(&slab_mutex);
  1421. list_for_each_entry(cachep, &slab_caches, list)
  1422. if (enable_cpucache(cachep, GFP_NOWAIT))
  1423. BUG();
  1424. mutex_unlock(&slab_mutex);
  1425. /* Annotate slab for lockdep -- annotate the malloc caches */
  1426. init_lock_keys();
  1427. /* Done! */
  1428. slab_state = FULL;
  1429. /*
  1430. * Register a cpu startup notifier callback that initializes
  1431. * cpu_cache_get for all new cpus
  1432. */
  1433. register_cpu_notifier(&cpucache_notifier);
  1434. #ifdef CONFIG_NUMA
  1435. /*
  1436. * Register a memory hotplug callback that initializes and frees
  1437. * node.
  1438. */
  1439. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  1440. #endif
  1441. /*
  1442. * The reap timers are started later, with a module init call: That part
  1443. * of the kernel is not yet operational.
  1444. */
  1445. }
  1446. static int __init cpucache_init(void)
  1447. {
  1448. int cpu;
  1449. /*
  1450. * Register the timers that return unneeded pages to the page allocator
  1451. */
  1452. for_each_online_cpu(cpu)
  1453. start_cpu_timer(cpu);
  1454. /* Done! */
  1455. slab_state = FULL;
  1456. return 0;
  1457. }
  1458. __initcall(cpucache_init);
  1459. static noinline void
  1460. slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
  1461. {
  1462. struct kmem_cache_node *n;
  1463. struct slab *slabp;
  1464. unsigned long flags;
  1465. int node;
  1466. printk(KERN_WARNING
  1467. "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1468. nodeid, gfpflags);
  1469. printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
  1470. cachep->name, cachep->size, cachep->gfporder);
  1471. for_each_online_node(node) {
  1472. unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
  1473. unsigned long active_slabs = 0, num_slabs = 0;
  1474. n = cachep->node[node];
  1475. if (!n)
  1476. continue;
  1477. spin_lock_irqsave(&n->list_lock, flags);
  1478. list_for_each_entry(slabp, &n->slabs_full, list) {
  1479. active_objs += cachep->num;
  1480. active_slabs++;
  1481. }
  1482. list_for_each_entry(slabp, &n->slabs_partial, list) {
  1483. active_objs += slabp->inuse;
  1484. active_slabs++;
  1485. }
  1486. list_for_each_entry(slabp, &n->slabs_free, list)
  1487. num_slabs++;
  1488. free_objects += n->free_objects;
  1489. spin_unlock_irqrestore(&n->list_lock, flags);
  1490. num_slabs += active_slabs;
  1491. num_objs = num_slabs * cachep->num;
  1492. printk(KERN_WARNING
  1493. " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
  1494. node, active_slabs, num_slabs, active_objs, num_objs,
  1495. free_objects);
  1496. }
  1497. }
  1498. /*
  1499. * Interface to system's page allocator. No need to hold the cache-lock.
  1500. *
  1501. * If we requested dmaable memory, we will get it. Even if we
  1502. * did not request dmaable memory, we might get it, but that
  1503. * would be relatively rare and ignorable.
  1504. */
  1505. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  1506. {
  1507. struct page *page;
  1508. int nr_pages;
  1509. int i;
  1510. #ifndef CONFIG_MMU
  1511. /*
  1512. * Nommu uses slab's for process anonymous memory allocations, and thus
  1513. * requires __GFP_COMP to properly refcount higher order allocations
  1514. */
  1515. flags |= __GFP_COMP;
  1516. #endif
  1517. flags |= cachep->allocflags;
  1518. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1519. flags |= __GFP_RECLAIMABLE;
  1520. page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
  1521. if (!page) {
  1522. if (!(flags & __GFP_NOWARN) && printk_ratelimit())
  1523. slab_out_of_memory(cachep, flags, nodeid);
  1524. return NULL;
  1525. }
  1526. /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
  1527. if (unlikely(page->pfmemalloc))
  1528. pfmemalloc_active = true;
  1529. nr_pages = (1 << cachep->gfporder);
  1530. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1531. add_zone_page_state(page_zone(page),
  1532. NR_SLAB_RECLAIMABLE, nr_pages);
  1533. else
  1534. add_zone_page_state(page_zone(page),
  1535. NR_SLAB_UNRECLAIMABLE, nr_pages);
  1536. for (i = 0; i < nr_pages; i++) {
  1537. __SetPageSlab(page + i);
  1538. if (page->pfmemalloc)
  1539. SetPageSlabPfmemalloc(page + i);
  1540. }
  1541. memcg_bind_pages(cachep, cachep->gfporder);
  1542. if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
  1543. kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
  1544. if (cachep->ctor)
  1545. kmemcheck_mark_uninitialized_pages(page, nr_pages);
  1546. else
  1547. kmemcheck_mark_unallocated_pages(page, nr_pages);
  1548. }
  1549. return page_address(page);
  1550. }
  1551. /*
  1552. * Interface to system's page release.
  1553. */
  1554. static void kmem_freepages(struct kmem_cache *cachep, void *addr)
  1555. {
  1556. unsigned long i = (1 << cachep->gfporder);
  1557. struct page *page = virt_to_page(addr);
  1558. const unsigned long nr_freed = i;
  1559. kmemcheck_free_shadow(page, cachep->gfporder);
  1560. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1561. sub_zone_page_state(page_zone(page),
  1562. NR_SLAB_RECLAIMABLE, nr_freed);
  1563. else
  1564. sub_zone_page_state(page_zone(page),
  1565. NR_SLAB_UNRECLAIMABLE, nr_freed);
  1566. while (i--) {
  1567. BUG_ON(!PageSlab(page));
  1568. __ClearPageSlabPfmemalloc(page);
  1569. __ClearPageSlab(page);
  1570. page++;
  1571. }
  1572. memcg_release_pages(cachep, cachep->gfporder);
  1573. if (current->reclaim_state)
  1574. current->reclaim_state->reclaimed_slab += nr_freed;
  1575. free_memcg_kmem_pages((unsigned long)addr, cachep->gfporder);
  1576. }
  1577. static void kmem_rcu_free(struct rcu_head *head)
  1578. {
  1579. struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
  1580. struct kmem_cache *cachep = slab_rcu->cachep;
  1581. kmem_freepages(cachep, slab_rcu->addr);
  1582. if (OFF_SLAB(cachep))
  1583. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1584. }
  1585. #if DEBUG
  1586. #ifdef CONFIG_DEBUG_PAGEALLOC
  1587. static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
  1588. unsigned long caller)
  1589. {
  1590. int size = cachep->object_size;
  1591. addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
  1592. if (size < 5 * sizeof(unsigned long))
  1593. return;
  1594. *addr++ = 0x12345678;
  1595. *addr++ = caller;
  1596. *addr++ = smp_processor_id();
  1597. size -= 3 * sizeof(unsigned long);
  1598. {
  1599. unsigned long *sptr = &caller;
  1600. unsigned long svalue;
  1601. while (!kstack_end(sptr)) {
  1602. svalue = *sptr++;
  1603. if (kernel_text_address(svalue)) {
  1604. *addr++ = svalue;
  1605. size -= sizeof(unsigned long);
  1606. if (size <= sizeof(unsigned long))
  1607. break;
  1608. }
  1609. }
  1610. }
  1611. *addr++ = 0x87654321;
  1612. }
  1613. #endif
  1614. static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
  1615. {
  1616. int size = cachep->object_size;
  1617. addr = &((char *)addr)[obj_offset(cachep)];
  1618. memset(addr, val, size);
  1619. *(unsigned char *)(addr + size - 1) = POISON_END;
  1620. }
  1621. static void dump_line(char *data, int offset, int limit)
  1622. {
  1623. int i;
  1624. unsigned char error = 0;
  1625. int bad_count = 0;
  1626. printk(KERN_ERR "%03x: ", offset);
  1627. for (i = 0; i < limit; i++) {
  1628. if (data[offset + i] != POISON_FREE) {
  1629. error = data[offset + i];
  1630. bad_count++;
  1631. }
  1632. }
  1633. print_hex_dump(KERN_CONT, "", 0, 16, 1,
  1634. &data[offset], limit, 1);
  1635. if (bad_count == 1) {
  1636. error ^= POISON_FREE;
  1637. if (!(error & (error - 1))) {
  1638. printk(KERN_ERR "Single bit error detected. Probably "
  1639. "bad RAM.\n");
  1640. #ifdef CONFIG_X86
  1641. printk(KERN_ERR "Run memtest86+ or a similar memory "
  1642. "test tool.\n");
  1643. #else
  1644. printk(KERN_ERR "Run a memory test tool.\n");
  1645. #endif
  1646. }
  1647. }
  1648. }
  1649. #endif
  1650. #if DEBUG
  1651. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1652. {
  1653. int i, size;
  1654. char *realobj;
  1655. if (cachep->flags & SLAB_RED_ZONE) {
  1656. printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
  1657. *dbg_redzone1(cachep, objp),
  1658. *dbg_redzone2(cachep, objp));
  1659. }
  1660. if (cachep->flags & SLAB_STORE_USER) {
  1661. printk(KERN_ERR "Last user: [<%p>]",
  1662. *dbg_userword(cachep, objp));
  1663. print_symbol("(%s)",
  1664. (unsigned long)*dbg_userword(cachep, objp));
  1665. printk("\n");
  1666. }
  1667. realobj = (char *)objp + obj_offset(cachep);
  1668. size = cachep->object_size;
  1669. for (i = 0; i < size && lines; i += 16, lines--) {
  1670. int limit;
  1671. limit = 16;
  1672. if (i + limit > size)
  1673. limit = size - i;
  1674. dump_line(realobj, i, limit);
  1675. }
  1676. }
  1677. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1678. {
  1679. char *realobj;
  1680. int size, i;
  1681. int lines = 0;
  1682. realobj = (char *)objp + obj_offset(cachep);
  1683. size = cachep->object_size;
  1684. for (i = 0; i < size; i++) {
  1685. char exp = POISON_FREE;
  1686. if (i == size - 1)
  1687. exp = POISON_END;
  1688. if (realobj[i] != exp) {
  1689. int limit;
  1690. /* Mismatch ! */
  1691. /* Print header */
  1692. if (lines == 0) {
  1693. printk(KERN_ERR
  1694. "Slab corruption (%s): %s start=%p, len=%d\n",
  1695. print_tainted(), cachep->name, realobj, size);
  1696. print_objinfo(cachep, objp, 0);
  1697. }
  1698. /* Hexdump the affected line */
  1699. i = (i / 16) * 16;
  1700. limit = 16;
  1701. if (i + limit > size)
  1702. limit = size - i;
  1703. dump_line(realobj, i, limit);
  1704. i += 16;
  1705. lines++;
  1706. /* Limit to 5 lines */
  1707. if (lines > 5)
  1708. break;
  1709. }
  1710. }
  1711. if (lines != 0) {
  1712. /* Print some data about the neighboring objects, if they
  1713. * exist:
  1714. */
  1715. struct slab *slabp = virt_to_slab(objp);
  1716. unsigned int objnr;
  1717. objnr = obj_to_index(cachep, slabp, objp);
  1718. if (objnr) {
  1719. objp = index_to_obj(cachep, slabp, objnr - 1);
  1720. realobj = (char *)objp + obj_offset(cachep);
  1721. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1722. realobj, size);
  1723. print_objinfo(cachep, objp, 2);
  1724. }
  1725. if (objnr + 1 < cachep->num) {
  1726. objp = index_to_obj(cachep, slabp, objnr + 1);
  1727. realobj = (char *)objp + obj_offset(cachep);
  1728. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1729. realobj, size);
  1730. print_objinfo(cachep, objp, 2);
  1731. }
  1732. }
  1733. }
  1734. #endif
  1735. #if DEBUG
  1736. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1737. {
  1738. int i;
  1739. for (i = 0; i < cachep->num; i++) {
  1740. void *objp = index_to_obj(cachep, slabp, i);
  1741. if (cachep->flags & SLAB_POISON) {
  1742. #ifdef CONFIG_DEBUG_PAGEALLOC
  1743. if (cachep->size % PAGE_SIZE == 0 &&
  1744. OFF_SLAB(cachep))
  1745. kernel_map_pages(virt_to_page(objp),
  1746. cachep->size / PAGE_SIZE, 1);
  1747. else
  1748. check_poison_obj(cachep, objp);
  1749. #else
  1750. check_poison_obj(cachep, objp);
  1751. #endif
  1752. }
  1753. if (cachep->flags & SLAB_RED_ZONE) {
  1754. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1755. slab_error(cachep, "start of a freed object "
  1756. "was overwritten");
  1757. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1758. slab_error(cachep, "end of a freed object "
  1759. "was overwritten");
  1760. }
  1761. }
  1762. }
  1763. #else
  1764. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1765. {
  1766. }
  1767. #endif
  1768. /**
  1769. * slab_destroy - destroy and release all objects in a slab
  1770. * @cachep: cache pointer being destroyed
  1771. * @slabp: slab pointer being destroyed
  1772. *
  1773. * Destroy all the objs in a slab, and release the mem back to the system.
  1774. * Before calling the slab must have been unlinked from the cache. The
  1775. * cache-lock is not held/needed.
  1776. */
  1777. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
  1778. {
  1779. void *addr = slabp->s_mem - slabp->colouroff;
  1780. slab_destroy_debugcheck(cachep, slabp);
  1781. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1782. struct slab_rcu *slab_rcu;
  1783. slab_rcu = (struct slab_rcu *)slabp;
  1784. slab_rcu->cachep = cachep;
  1785. slab_rcu->addr = addr;
  1786. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1787. } else {
  1788. kmem_freepages(cachep, addr);
  1789. if (OFF_SLAB(cachep))
  1790. kmem_cache_free(cachep->slabp_cache, slabp);
  1791. }
  1792. }
  1793. /**
  1794. * calculate_slab_order - calculate size (page order) of slabs
  1795. * @cachep: pointer to the cache that is being created
  1796. * @size: size of objects to be created in this cache.
  1797. * @align: required alignment for the objects.
  1798. * @flags: slab allocation flags
  1799. *
  1800. * Also calculates the number of objects per slab.
  1801. *
  1802. * This could be made much more intelligent. For now, try to avoid using
  1803. * high order pages for slabs. When the gfp() functions are more friendly
  1804. * towards high-order requests, this should be changed.
  1805. */
  1806. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1807. size_t size, size_t align, unsigned long flags)
  1808. {
  1809. unsigned long offslab_limit;
  1810. size_t left_over = 0;
  1811. int gfporder;
  1812. for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
  1813. unsigned int num;
  1814. size_t remainder;
  1815. cache_estimate(gfporder, size, align, flags, &remainder, &num);
  1816. if (!num)
  1817. continue;
  1818. if (flags & CFLGS_OFF_SLAB) {
  1819. /*
  1820. * Max number of objs-per-slab for caches which
  1821. * use off-slab slabs. Needed to avoid a possible
  1822. * looping condition in cache_grow().
  1823. */
  1824. offslab_limit = size - sizeof(struct slab);
  1825. offslab_limit /= sizeof(kmem_bufctl_t);
  1826. if (num > offslab_limit)
  1827. break;
  1828. }
  1829. /* Found something acceptable - save it away */
  1830. cachep->num = num;
  1831. cachep->gfporder = gfporder;
  1832. left_over = remainder;
  1833. /*
  1834. * A VFS-reclaimable slab tends to have most allocations
  1835. * as GFP_NOFS and we really don't want to have to be allocating
  1836. * higher-order pages when we are unable to shrink dcache.
  1837. */
  1838. if (flags & SLAB_RECLAIM_ACCOUNT)
  1839. break;
  1840. /*
  1841. * Large number of objects is good, but very large slabs are
  1842. * currently bad for the gfp()s.
  1843. */
  1844. if (gfporder >= slab_max_order)
  1845. break;
  1846. /*
  1847. * Acceptable internal fragmentation?
  1848. */
  1849. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  1850. break;
  1851. }
  1852. return left_over;
  1853. }
  1854. static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
  1855. {
  1856. if (slab_state >= FULL)
  1857. return enable_cpucache(cachep, gfp);
  1858. if (slab_state == DOWN) {
  1859. /*
  1860. * Note: Creation of first cache (kmem_cache).
  1861. * The setup_node is taken care
  1862. * of by the caller of __kmem_cache_create
  1863. */
  1864. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1865. slab_state = PARTIAL;
  1866. } else if (slab_state == PARTIAL) {
  1867. /*
  1868. * Note: the second kmem_cache_create must create the cache
  1869. * that's used by kmalloc(24), otherwise the creation of
  1870. * further caches will BUG().
  1871. */
  1872. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1873. /*
  1874. * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
  1875. * the second cache, then we need to set up all its node/,
  1876. * otherwise the creation of further caches will BUG().
  1877. */
  1878. set_up_node(cachep, SIZE_AC);
  1879. if (INDEX_AC == INDEX_NODE)
  1880. slab_state = PARTIAL_NODE;
  1881. else
  1882. slab_state = PARTIAL_ARRAYCACHE;
  1883. } else {
  1884. /* Remaining boot caches */
  1885. cachep->array[smp_processor_id()] =
  1886. kmalloc(sizeof(struct arraycache_init), gfp);
  1887. if (slab_state == PARTIAL_ARRAYCACHE) {
  1888. set_up_node(cachep, SIZE_NODE);
  1889. slab_state = PARTIAL_NODE;
  1890. } else {
  1891. int node;
  1892. for_each_online_node(node) {
  1893. cachep->node[node] =
  1894. kmalloc_node(sizeof(struct kmem_cache_node),
  1895. gfp, node);
  1896. BUG_ON(!cachep->node[node]);
  1897. kmem_cache_node_init(cachep->node[node]);
  1898. }
  1899. }
  1900. }
  1901. cachep->node[numa_mem_id()]->next_reap =
  1902. jiffies + REAPTIMEOUT_LIST3 +
  1903. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1904. cpu_cache_get(cachep)->avail = 0;
  1905. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1906. cpu_cache_get(cachep)->batchcount = 1;
  1907. cpu_cache_get(cachep)->touched = 0;
  1908. cachep->batchcount = 1;
  1909. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1910. return 0;
  1911. }
  1912. /**
  1913. * __kmem_cache_create - Create a cache.
  1914. * @cachep: cache management descriptor
  1915. * @flags: SLAB flags
  1916. *
  1917. * Returns a ptr to the cache on success, NULL on failure.
  1918. * Cannot be called within a int, but can be interrupted.
  1919. * The @ctor is run when new pages are allocated by the cache.
  1920. *
  1921. * The flags are
  1922. *
  1923. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  1924. * to catch references to uninitialised memory.
  1925. *
  1926. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  1927. * for buffer overruns.
  1928. *
  1929. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  1930. * cacheline. This can be beneficial if you're counting cycles as closely
  1931. * as davem.
  1932. */
  1933. int
  1934. __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
  1935. {
  1936. size_t left_over, slab_size, ralign;
  1937. gfp_t gfp;
  1938. int err;
  1939. size_t size = cachep->size;
  1940. #if DEBUG
  1941. #if FORCED_DEBUG
  1942. /*
  1943. * Enable redzoning and last user accounting, except for caches with
  1944. * large objects, if the increased size would increase the object size
  1945. * above the next power of two: caches with object sizes just above a
  1946. * power of two have a significant amount of internal fragmentation.
  1947. */
  1948. if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
  1949. 2 * sizeof(unsigned long long)))
  1950. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  1951. if (!(flags & SLAB_DESTROY_BY_RCU))
  1952. flags |= SLAB_POISON;
  1953. #endif
  1954. if (flags & SLAB_DESTROY_BY_RCU)
  1955. BUG_ON(flags & SLAB_POISON);
  1956. #endif
  1957. /*
  1958. * Check that size is in terms of words. This is needed to avoid
  1959. * unaligned accesses for some archs when redzoning is used, and makes
  1960. * sure any on-slab bufctl's are also correctly aligned.
  1961. */
  1962. if (size & (BYTES_PER_WORD - 1)) {
  1963. size += (BYTES_PER_WORD - 1);
  1964. size &= ~(BYTES_PER_WORD - 1);
  1965. }
  1966. /*
  1967. * Redzoning and user store require word alignment or possibly larger.
  1968. * Note this will be overridden by architecture or caller mandated
  1969. * alignment if either is greater than BYTES_PER_WORD.
  1970. */
  1971. if (flags & SLAB_STORE_USER)
  1972. ralign = BYTES_PER_WORD;
  1973. if (flags & SLAB_RED_ZONE) {
  1974. ralign = REDZONE_ALIGN;
  1975. /* If redzoning, ensure that the second redzone is suitably
  1976. * aligned, by adjusting the object size accordingly. */
  1977. size += REDZONE_ALIGN - 1;
  1978. size &= ~(REDZONE_ALIGN - 1);
  1979. }
  1980. /* 3) caller mandated alignment */
  1981. if (ralign < cachep->align) {
  1982. ralign = cachep->align;
  1983. }
  1984. /* disable debug if necessary */
  1985. if (ralign > __alignof__(unsigned long long))
  1986. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1987. /*
  1988. * 4) Store it.
  1989. */
  1990. cachep->align = ralign;
  1991. if (slab_is_available())
  1992. gfp = GFP_KERNEL;
  1993. else
  1994. gfp = GFP_NOWAIT;
  1995. setup_node_pointer(cachep);
  1996. #if DEBUG
  1997. /*
  1998. * Both debugging options require word-alignment which is calculated
  1999. * into align above.
  2000. */
  2001. if (flags & SLAB_RED_ZONE) {
  2002. /* add space for red zone words */
  2003. cachep->obj_offset += sizeof(unsigned long long);
  2004. size += 2 * sizeof(unsigned long long);
  2005. }
  2006. if (flags & SLAB_STORE_USER) {
  2007. /* user store requires one word storage behind the end of
  2008. * the real object. But if the second red zone needs to be
  2009. * aligned to 64 bits, we must allow that much space.
  2010. */
  2011. if (flags & SLAB_RED_ZONE)
  2012. size += REDZONE_ALIGN;
  2013. else
  2014. size += BYTES_PER_WORD;
  2015. }
  2016. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  2017. if (size >= kmalloc_size(INDEX_NODE + 1)
  2018. && cachep->object_size > cache_line_size()
  2019. && ALIGN(size, cachep->align) < PAGE_SIZE) {
  2020. cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
  2021. size = PAGE_SIZE;
  2022. }
  2023. #endif
  2024. #endif
  2025. /*
  2026. * Determine if the slab management is 'on' or 'off' slab.
  2027. * (bootstrapping cannot cope with offslab caches so don't do
  2028. * it too early on. Always use on-slab management when
  2029. * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
  2030. */
  2031. if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
  2032. !(flags & SLAB_NOLEAKTRACE))
  2033. /*
  2034. * Size is large, assume best to place the slab management obj
  2035. * off-slab (should allow better packing of objs).
  2036. */
  2037. flags |= CFLGS_OFF_SLAB;
  2038. size = ALIGN(size, cachep->align);
  2039. left_over = calculate_slab_order(cachep, size, cachep->align, flags);
  2040. if (!cachep->num)
  2041. return -E2BIG;
  2042. slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
  2043. + sizeof(struct slab), cachep->align);
  2044. /*
  2045. * If the slab has been placed off-slab, and we have enough space then
  2046. * move it on-slab. This is at the expense of any extra colouring.
  2047. */
  2048. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  2049. flags &= ~CFLGS_OFF_SLAB;
  2050. left_over -= slab_size;
  2051. }
  2052. if (flags & CFLGS_OFF_SLAB) {
  2053. /* really off slab. No need for manual alignment */
  2054. slab_size =
  2055. cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
  2056. #ifdef CONFIG_PAGE_POISONING
  2057. /* If we're going to use the generic kernel_map_pages()
  2058. * poisoning, then it's going to smash the contents of
  2059. * the redzone and userword anyhow, so switch them off.
  2060. */
  2061. if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
  2062. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  2063. #endif
  2064. }
  2065. cachep->colour_off = cache_line_size();
  2066. /* Offset must be a multiple of the alignment. */
  2067. if (cachep->colour_off < cachep->align)
  2068. cachep->colour_off = cachep->align;
  2069. cachep->colour = left_over / cachep->colour_off;
  2070. cachep->slab_size = slab_size;
  2071. cachep->flags = flags;
  2072. cachep->allocflags = 0;
  2073. if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
  2074. cachep->allocflags |= GFP_DMA;
  2075. cachep->size = size;
  2076. cachep->reciprocal_buffer_size = reciprocal_value(size);
  2077. if (flags & CFLGS_OFF_SLAB) {
  2078. cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
  2079. /*
  2080. * This is a possibility for one of the malloc_sizes caches.
  2081. * But since we go off slab only for object size greater than
  2082. * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
  2083. * this should not happen at all.
  2084. * But leave a BUG_ON for some lucky dude.
  2085. */
  2086. BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
  2087. }
  2088. err = setup_cpu_cache(cachep, gfp);
  2089. if (err) {
  2090. __kmem_cache_shutdown(cachep);
  2091. return err;
  2092. }
  2093. if (flags & SLAB_DEBUG_OBJECTS) {
  2094. /*
  2095. * Would deadlock through slab_destroy()->call_rcu()->
  2096. * debug_object_activate()->kmem_cache_alloc().
  2097. */
  2098. WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
  2099. slab_set_debugobj_lock_classes(cachep);
  2100. } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
  2101. on_slab_lock_classes(cachep);
  2102. return 0;
  2103. }
  2104. #if DEBUG
  2105. static void check_irq_off(void)
  2106. {
  2107. BUG_ON(!irqs_disabled());
  2108. }
  2109. static void check_irq_on(void)
  2110. {
  2111. BUG_ON(irqs_disabled());
  2112. }
  2113. static void check_spinlock_acquired(struct kmem_cache *cachep)
  2114. {
  2115. #ifdef CONFIG_SMP
  2116. check_irq_off();
  2117. assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
  2118. #endif
  2119. }
  2120. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  2121. {
  2122. #ifdef CONFIG_SMP
  2123. check_irq_off();
  2124. assert_spin_locked(&cachep->node[node]->list_lock);
  2125. #endif
  2126. }
  2127. #else
  2128. #define check_irq_off() do { } while(0)
  2129. #define check_irq_on() do { } while(0)
  2130. #define check_spinlock_acquired(x) do { } while(0)
  2131. #define check_spinlock_acquired_node(x, y) do { } while(0)
  2132. #endif
  2133. static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
  2134. struct array_cache *ac,
  2135. int force, int node);
  2136. static void do_drain(void *arg)
  2137. {
  2138. struct kmem_cache *cachep = arg;
  2139. struct array_cache *ac;
  2140. int node = numa_mem_id();
  2141. check_irq_off();
  2142. ac = cpu_cache_get(cachep);
  2143. spin_lock(&cachep->node[node]->list_lock);
  2144. free_block(cachep, ac->entry, ac->avail, node);
  2145. spin_unlock(&cachep->node[node]->list_lock);
  2146. ac->avail = 0;
  2147. }
  2148. static void drain_cpu_caches(struct kmem_cache *cachep)
  2149. {
  2150. struct kmem_cache_node *n;
  2151. int node;
  2152. on_each_cpu(do_drain, cachep, 1);
  2153. check_irq_on();
  2154. for_each_online_node(node) {
  2155. n = cachep->node[node];
  2156. if (n && n->alien)
  2157. drain_alien_cache(cachep, n->alien);
  2158. }
  2159. for_each_online_node(node) {
  2160. n = cachep->node[node];
  2161. if (n)
  2162. drain_array(cachep, n, n->shared, 1, node);
  2163. }
  2164. }
  2165. /*
  2166. * Remove slabs from the list of free slabs.
  2167. * Specify the number of slabs to drain in tofree.
  2168. *
  2169. * Returns the actual number of slabs released.
  2170. */
  2171. static int drain_freelist(struct kmem_cache *cache,
  2172. struct kmem_cache_node *n, int tofree)
  2173. {
  2174. struct list_head *p;
  2175. int nr_freed;
  2176. struct slab *slabp;
  2177. nr_freed = 0;
  2178. while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
  2179. spin_lock_irq(&n->list_lock);
  2180. p = n->slabs_free.prev;
  2181. if (p == &n->slabs_free) {
  2182. spin_unlock_irq(&n->list_lock);
  2183. goto out;
  2184. }
  2185. slabp = list_entry(p, struct slab, list);
  2186. #if DEBUG
  2187. BUG_ON(slabp->inuse);
  2188. #endif
  2189. list_del(&slabp->list);
  2190. /*
  2191. * Safe to drop the lock. The slab is no longer linked
  2192. * to the cache.
  2193. */
  2194. n->free_objects -= cache->num;
  2195. spin_unlock_irq(&n->list_lock);
  2196. slab_destroy(cache, slabp);
  2197. nr_freed++;
  2198. }
  2199. out:
  2200. return nr_freed;
  2201. }
  2202. /* Called with slab_mutex held to protect against cpu hotplug */
  2203. static int __cache_shrink(struct kmem_cache *cachep)
  2204. {
  2205. int ret = 0, i = 0;
  2206. struct kmem_cache_node *n;
  2207. drain_cpu_caches(cachep);
  2208. check_irq_on();
  2209. for_each_online_node(i) {
  2210. n = cachep->node[i];
  2211. if (!n)
  2212. continue;
  2213. drain_freelist(cachep, n, slabs_tofree(cachep, n));
  2214. ret += !list_empty(&n->slabs_full) ||
  2215. !list_empty(&n->slabs_partial);
  2216. }
  2217. return (ret ? 1 : 0);
  2218. }
  2219. /**
  2220. * kmem_cache_shrink - Shrink a cache.
  2221. * @cachep: The cache to shrink.
  2222. *
  2223. * Releases as many slabs as possible for a cache.
  2224. * To help debugging, a zero exit status indicates all slabs were released.
  2225. */
  2226. int kmem_cache_shrink(struct kmem_cache *cachep)
  2227. {
  2228. int ret;
  2229. BUG_ON(!cachep || in_interrupt());
  2230. get_online_cpus();
  2231. mutex_lock(&slab_mutex);
  2232. ret = __cache_shrink(cachep);
  2233. mutex_unlock(&slab_mutex);
  2234. put_online_cpus();
  2235. return ret;
  2236. }
  2237. EXPORT_SYMBOL(kmem_cache_shrink);
  2238. int __kmem_cache_shutdown(struct kmem_cache *cachep)
  2239. {
  2240. int i;
  2241. struct kmem_cache_node *n;
  2242. int rc = __cache_shrink(cachep);
  2243. if (rc)
  2244. return rc;
  2245. for_each_online_cpu(i)
  2246. kfree(cachep->array[i]);
  2247. /* NUMA: free the node structures */
  2248. for_each_online_node(i) {
  2249. n = cachep->node[i];
  2250. if (n) {
  2251. kfree(n->shared);
  2252. free_alien_cache(n->alien);
  2253. kfree(n);
  2254. }
  2255. }
  2256. return 0;
  2257. }
  2258. /*
  2259. * Get the memory for a slab management obj.
  2260. * For a slab cache when the slab descriptor is off-slab, slab descriptors
  2261. * always come from malloc_sizes caches. The slab descriptor cannot
  2262. * come from the same cache which is getting created because,
  2263. * when we are searching for an appropriate cache for these
  2264. * descriptors in kmem_cache_create, we search through the malloc_sizes array.
  2265. * If we are creating a malloc_sizes cache here it would not be visible to
  2266. * kmem_find_general_cachep till the initialization is complete.
  2267. * Hence we cannot have slabp_cache same as the original cache.
  2268. */
  2269. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
  2270. int colour_off, gfp_t local_flags,
  2271. int nodeid)
  2272. {
  2273. struct slab *slabp;
  2274. if (OFF_SLAB(cachep)) {
  2275. /* Slab management obj is off-slab. */
  2276. slabp = kmem_cache_alloc_node(cachep->slabp_cache,
  2277. local_flags, nodeid);
  2278. /*
  2279. * If the first object in the slab is leaked (it's allocated
  2280. * but no one has a reference to it), we want to make sure
  2281. * kmemleak does not treat the ->s_mem pointer as a reference
  2282. * to the object. Otherwise we will not report the leak.
  2283. */
  2284. kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
  2285. local_flags);
  2286. if (!slabp)
  2287. return NULL;
  2288. } else {
  2289. slabp = objp + colour_off;
  2290. colour_off += cachep->slab_size;
  2291. }
  2292. slabp->inuse = 0;
  2293. slabp->colouroff = colour_off;
  2294. slabp->s_mem = objp + colour_off;
  2295. slabp->nodeid = nodeid;
  2296. slabp->free = 0;
  2297. return slabp;
  2298. }
  2299. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  2300. {
  2301. return (kmem_bufctl_t *) (slabp + 1);
  2302. }
  2303. static void cache_init_objs(struct kmem_cache *cachep,
  2304. struct slab *slabp)
  2305. {
  2306. int i;
  2307. for (i = 0; i < cachep->num; i++) {
  2308. void *objp = index_to_obj(cachep, slabp, i);
  2309. #if DEBUG
  2310. /* need to poison the objs? */
  2311. if (cachep->flags & SLAB_POISON)
  2312. poison_obj(cachep, objp, POISON_FREE);
  2313. if (cachep->flags & SLAB_STORE_USER)
  2314. *dbg_userword(cachep, objp) = NULL;
  2315. if (cachep->flags & SLAB_RED_ZONE) {
  2316. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2317. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2318. }
  2319. /*
  2320. * Constructors are not allowed to allocate memory from the same
  2321. * cache which they are a constructor for. Otherwise, deadlock.
  2322. * They must also be threaded.
  2323. */
  2324. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  2325. cachep->ctor(objp + obj_offset(cachep));
  2326. if (cachep->flags & SLAB_RED_ZONE) {
  2327. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  2328. slab_error(cachep, "constructor overwrote the"
  2329. " end of an object");
  2330. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  2331. slab_error(cachep, "constructor overwrote the"
  2332. " start of an object");
  2333. }
  2334. if ((cachep->size % PAGE_SIZE) == 0 &&
  2335. OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  2336. kernel_map_pages(virt_to_page(objp),
  2337. cachep->size / PAGE_SIZE, 0);
  2338. #else
  2339. if (cachep->ctor)
  2340. cachep->ctor(objp);
  2341. #endif
  2342. slab_bufctl(slabp)[i] = i + 1;
  2343. }
  2344. slab_bufctl(slabp)[i - 1] = BUFCTL_END;
  2345. }
  2346. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  2347. {
  2348. if (CONFIG_ZONE_DMA_FLAG) {
  2349. if (flags & GFP_DMA)
  2350. BUG_ON(!(cachep->allocflags & GFP_DMA));
  2351. else
  2352. BUG_ON(cachep->allocflags & GFP_DMA);
  2353. }
  2354. }
  2355. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  2356. int nodeid)
  2357. {
  2358. void *objp = index_to_obj(cachep, slabp, slabp->free);
  2359. kmem_bufctl_t next;
  2360. slabp->inuse++;
  2361. next = slab_bufctl(slabp)[slabp->free];
  2362. #if DEBUG
  2363. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2364. WARN_ON(slabp->nodeid != nodeid);
  2365. #endif
  2366. slabp->free = next;
  2367. return objp;
  2368. }
  2369. static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  2370. void *objp, int nodeid)
  2371. {
  2372. unsigned int objnr = obj_to_index(cachep, slabp, objp);
  2373. #if DEBUG
  2374. /* Verify that the slab belongs to the intended node */
  2375. WARN_ON(slabp->nodeid != nodeid);
  2376. if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
  2377. printk(KERN_ERR "slab: double free detected in cache "
  2378. "'%s', objp %p\n", cachep->name, objp);
  2379. BUG();
  2380. }
  2381. #endif
  2382. slab_bufctl(slabp)[objnr] = slabp->free;
  2383. slabp->free = objnr;
  2384. slabp->inuse--;
  2385. }
  2386. /*
  2387. * Map pages beginning at addr to the given cache and slab. This is required
  2388. * for the slab allocator to be able to lookup the cache and slab of a
  2389. * virtual address for kfree, ksize, and slab debugging.
  2390. */
  2391. static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
  2392. void *addr)
  2393. {
  2394. int nr_pages;
  2395. struct page *page;
  2396. page = virt_to_page(addr);
  2397. nr_pages = 1;
  2398. if (likely(!PageCompound(page)))
  2399. nr_pages <<= cache->gfporder;
  2400. do {
  2401. page->slab_cache = cache;
  2402. page->slab_page = slab;
  2403. page++;
  2404. } while (--nr_pages);
  2405. }
  2406. /*
  2407. * Grow (by 1) the number of slabs within a cache. This is called by
  2408. * kmem_cache_alloc() when there are no active objs left in a cache.
  2409. */
  2410. static int cache_grow(struct kmem_cache *cachep,
  2411. gfp_t flags, int nodeid, void *objp)
  2412. {
  2413. struct slab *slabp;
  2414. size_t offset;
  2415. gfp_t local_flags;
  2416. struct kmem_cache_node *n;
  2417. /*
  2418. * Be lazy and only check for valid flags here, keeping it out of the
  2419. * critical path in kmem_cache_alloc().
  2420. */
  2421. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  2422. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2423. /* Take the node list lock to change the colour_next on this node */
  2424. check_irq_off();
  2425. n = cachep->node[nodeid];
  2426. spin_lock(&n->list_lock);
  2427. /* Get colour for the slab, and cal the next value. */
  2428. offset = n->colour_next;
  2429. n->colour_next++;
  2430. if (n->colour_next >= cachep->colour)
  2431. n->colour_next = 0;
  2432. spin_unlock(&n->list_lock);
  2433. offset *= cachep->colour_off;
  2434. if (local_flags & __GFP_WAIT)
  2435. local_irq_enable();
  2436. /*
  2437. * The test for missing atomic flag is performed here, rather than
  2438. * the more obvious place, simply to reduce the critical path length
  2439. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  2440. * will eventually be caught here (where it matters).
  2441. */
  2442. kmem_flagcheck(cachep, flags);
  2443. /*
  2444. * Get mem for the objs. Attempt to allocate a physical page from
  2445. * 'nodeid'.
  2446. */
  2447. if (!objp)
  2448. objp = kmem_getpages(cachep, local_flags, nodeid);
  2449. if (!objp)
  2450. goto failed;
  2451. /* Get slab management. */
  2452. slabp = alloc_slabmgmt(cachep, objp, offset,
  2453. local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
  2454. if (!slabp)
  2455. goto opps1;
  2456. slab_map_pages(cachep, slabp, objp);
  2457. cache_init_objs(cachep, slabp);
  2458. if (local_flags & __GFP_WAIT)
  2459. local_irq_disable();
  2460. check_irq_off();
  2461. spin_lock(&n->list_lock);
  2462. /* Make slab active. */
  2463. list_add_tail(&slabp->list, &(n->slabs_free));
  2464. STATS_INC_GROWN(cachep);
  2465. n->free_objects += cachep->num;
  2466. spin_unlock(&n->list_lock);
  2467. return 1;
  2468. opps1:
  2469. kmem_freepages(cachep, objp);
  2470. failed:
  2471. if (local_flags & __GFP_WAIT)
  2472. local_irq_disable();
  2473. return 0;
  2474. }
  2475. #if DEBUG
  2476. /*
  2477. * Perform extra freeing checks:
  2478. * - detect bad pointers.
  2479. * - POISON/RED_ZONE checking
  2480. */
  2481. static void kfree_debugcheck(const void *objp)
  2482. {
  2483. if (!virt_addr_valid(objp)) {
  2484. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  2485. (unsigned long)objp);
  2486. BUG();
  2487. }
  2488. }
  2489. static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  2490. {
  2491. unsigned long long redzone1, redzone2;
  2492. redzone1 = *dbg_redzone1(cache, obj);
  2493. redzone2 = *dbg_redzone2(cache, obj);
  2494. /*
  2495. * Redzone is ok.
  2496. */
  2497. if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  2498. return;
  2499. if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  2500. slab_error(cache, "double free detected");
  2501. else
  2502. slab_error(cache, "memory outside object was overwritten");
  2503. printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
  2504. obj, redzone1, redzone2);
  2505. }
  2506. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2507. unsigned long caller)
  2508. {
  2509. struct page *page;
  2510. unsigned int objnr;
  2511. struct slab *slabp;
  2512. BUG_ON(virt_to_cache(objp) != cachep);
  2513. objp -= obj_offset(cachep);
  2514. kfree_debugcheck(objp);
  2515. page = virt_to_head_page(objp);
  2516. slabp = page->slab_page;
  2517. if (cachep->flags & SLAB_RED_ZONE) {
  2518. verify_redzone_free(cachep, objp);
  2519. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2520. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2521. }
  2522. if (cachep->flags & SLAB_STORE_USER)
  2523. *dbg_userword(cachep, objp) = (void *)caller;
  2524. objnr = obj_to_index(cachep, slabp, objp);
  2525. BUG_ON(objnr >= cachep->num);
  2526. BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
  2527. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2528. slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
  2529. #endif
  2530. if (cachep->flags & SLAB_POISON) {
  2531. #ifdef CONFIG_DEBUG_PAGEALLOC
  2532. if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
  2533. store_stackinfo(cachep, objp, caller);
  2534. kernel_map_pages(virt_to_page(objp),
  2535. cachep->size / PAGE_SIZE, 0);
  2536. } else {
  2537. poison_obj(cachep, objp, POISON_FREE);
  2538. }
  2539. #else
  2540. poison_obj(cachep, objp, POISON_FREE);
  2541. #endif
  2542. }
  2543. return objp;
  2544. }
  2545. static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
  2546. {
  2547. kmem_bufctl_t i;
  2548. int entries = 0;
  2549. /* Check slab's freelist to see if this obj is there. */
  2550. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2551. entries++;
  2552. if (entries > cachep->num || i >= cachep->num)
  2553. goto bad;
  2554. }
  2555. if (entries != cachep->num - slabp->inuse) {
  2556. bad:
  2557. printk(KERN_ERR "slab: Internal list corruption detected in "
  2558. "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
  2559. cachep->name, cachep->num, slabp, slabp->inuse,
  2560. print_tainted());
  2561. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
  2562. sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
  2563. 1);
  2564. BUG();
  2565. }
  2566. }
  2567. #else
  2568. #define kfree_debugcheck(x) do { } while(0)
  2569. #define cache_free_debugcheck(x,objp,z) (objp)
  2570. #define check_slabp(x,y) do { } while(0)
  2571. #endif
  2572. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
  2573. bool force_refill)
  2574. {
  2575. int batchcount;
  2576. struct kmem_cache_node *n;
  2577. struct array_cache *ac;
  2578. int node;
  2579. check_irq_off();
  2580. node = numa_mem_id();
  2581. if (unlikely(force_refill))
  2582. goto force_grow;
  2583. retry:
  2584. ac = cpu_cache_get(cachep);
  2585. batchcount = ac->batchcount;
  2586. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2587. /*
  2588. * If there was little recent activity on this cache, then
  2589. * perform only a partial refill. Otherwise we could generate
  2590. * refill bouncing.
  2591. */
  2592. batchcount = BATCHREFILL_LIMIT;
  2593. }
  2594. n = cachep->node[node];
  2595. BUG_ON(ac->avail > 0 || !n);
  2596. spin_lock(&n->list_lock);
  2597. /* See if we can refill from the shared array */
  2598. if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
  2599. n->shared->touched = 1;
  2600. goto alloc_done;
  2601. }
  2602. while (batchcount > 0) {
  2603. struct list_head *entry;
  2604. struct slab *slabp;
  2605. /* Get slab alloc is to come from. */
  2606. entry = n->slabs_partial.next;
  2607. if (entry == &n->slabs_partial) {
  2608. n->free_touched = 1;
  2609. entry = n->slabs_free.next;
  2610. if (entry == &n->slabs_free)
  2611. goto must_grow;
  2612. }
  2613. slabp = list_entry(entry, struct slab, list);
  2614. check_slabp(cachep, slabp);
  2615. check_spinlock_acquired(cachep);
  2616. /*
  2617. * The slab was either on partial or free list so
  2618. * there must be at least one object available for
  2619. * allocation.
  2620. */
  2621. BUG_ON(slabp->inuse >= cachep->num);
  2622. while (slabp->inuse < cachep->num && batchcount--) {
  2623. STATS_INC_ALLOCED(cachep);
  2624. STATS_INC_ACTIVE(cachep);
  2625. STATS_SET_HIGH(cachep);
  2626. ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
  2627. node));
  2628. }
  2629. check_slabp(cachep, slabp);
  2630. /* move slabp to correct slabp list: */
  2631. list_del(&slabp->list);
  2632. if (slabp->free == BUFCTL_END)
  2633. list_add(&slabp->list, &n->slabs_full);
  2634. else
  2635. list_add(&slabp->list, &n->slabs_partial);
  2636. }
  2637. must_grow:
  2638. n->free_objects -= ac->avail;
  2639. alloc_done:
  2640. spin_unlock(&n->list_lock);
  2641. if (unlikely(!ac->avail)) {
  2642. int x;
  2643. force_grow:
  2644. x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
  2645. /* cache_grow can reenable interrupts, then ac could change. */
  2646. ac = cpu_cache_get(cachep);
  2647. node = numa_mem_id();
  2648. /* no objects in sight? abort */
  2649. if (!x && (ac->avail == 0 || force_refill))
  2650. return NULL;
  2651. if (!ac->avail) /* objects refilled by interrupt? */
  2652. goto retry;
  2653. }
  2654. ac->touched = 1;
  2655. return ac_get_obj(cachep, ac, flags, force_refill);
  2656. }
  2657. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2658. gfp_t flags)
  2659. {
  2660. might_sleep_if(flags & __GFP_WAIT);
  2661. #if DEBUG
  2662. kmem_flagcheck(cachep, flags);
  2663. #endif
  2664. }
  2665. #if DEBUG
  2666. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2667. gfp_t flags, void *objp, unsigned long caller)
  2668. {
  2669. if (!objp)
  2670. return objp;
  2671. if (cachep->flags & SLAB_POISON) {
  2672. #ifdef CONFIG_DEBUG_PAGEALLOC
  2673. if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2674. kernel_map_pages(virt_to_page(objp),
  2675. cachep->size / PAGE_SIZE, 1);
  2676. else
  2677. check_poison_obj(cachep, objp);
  2678. #else
  2679. check_poison_obj(cachep, objp);
  2680. #endif
  2681. poison_obj(cachep, objp, POISON_INUSE);
  2682. }
  2683. if (cachep->flags & SLAB_STORE_USER)
  2684. *dbg_userword(cachep, objp) = (void *)caller;
  2685. if (cachep->flags & SLAB_RED_ZONE) {
  2686. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2687. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2688. slab_error(cachep, "double free, or memory outside"
  2689. " object was overwritten");
  2690. printk(KERN_ERR
  2691. "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
  2692. objp, *dbg_redzone1(cachep, objp),
  2693. *dbg_redzone2(cachep, objp));
  2694. }
  2695. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2696. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2697. }
  2698. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2699. {
  2700. struct slab *slabp;
  2701. unsigned objnr;
  2702. slabp = virt_to_head_page(objp)->slab_page;
  2703. objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
  2704. slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
  2705. }
  2706. #endif
  2707. objp += obj_offset(cachep);
  2708. if (cachep->ctor && cachep->flags & SLAB_POISON)
  2709. cachep->ctor(objp);
  2710. if (ARCH_SLAB_MINALIGN &&
  2711. ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
  2712. printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
  2713. objp, (int)ARCH_SLAB_MINALIGN);
  2714. }
  2715. return objp;
  2716. }
  2717. #else
  2718. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2719. #endif
  2720. static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
  2721. {
  2722. if (cachep == kmem_cache)
  2723. return false;
  2724. return should_failslab(cachep->object_size, flags, cachep->flags);
  2725. }
  2726. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2727. {
  2728. void *objp;
  2729. struct array_cache *ac;
  2730. bool force_refill = false;
  2731. check_irq_off();
  2732. ac = cpu_cache_get(cachep);
  2733. if (likely(ac->avail)) {
  2734. ac->touched = 1;
  2735. objp = ac_get_obj(cachep, ac, flags, false);
  2736. /*
  2737. * Allow for the possibility all avail objects are not allowed
  2738. * by the current flags
  2739. */
  2740. if (objp) {
  2741. STATS_INC_ALLOCHIT(cachep);
  2742. goto out;
  2743. }
  2744. force_refill = true;
  2745. }
  2746. STATS_INC_ALLOCMISS(cachep);
  2747. objp = cache_alloc_refill(cachep, flags, force_refill);
  2748. /*
  2749. * the 'ac' may be updated by cache_alloc_refill(),
  2750. * and kmemleak_erase() requires its correct value.
  2751. */
  2752. ac = cpu_cache_get(cachep);
  2753. out:
  2754. /*
  2755. * To avoid a false negative, if an object that is in one of the
  2756. * per-CPU caches is leaked, we need to make sure kmemleak doesn't
  2757. * treat the array pointers as a reference to the object.
  2758. */
  2759. if (objp)
  2760. kmemleak_erase(&ac->entry[ac->avail]);
  2761. return objp;
  2762. }
  2763. #ifdef CONFIG_NUMA
  2764. /*
  2765. * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
  2766. *
  2767. * If we are in_interrupt, then process context, including cpusets and
  2768. * mempolicy, may not apply and should not be used for allocation policy.
  2769. */
  2770. static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  2771. {
  2772. int nid_alloc, nid_here;
  2773. if (in_interrupt() || (flags & __GFP_THISNODE))
  2774. return NULL;
  2775. nid_alloc = nid_here = numa_mem_id();
  2776. if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
  2777. nid_alloc = cpuset_slab_spread_node();
  2778. else if (current->mempolicy)
  2779. nid_alloc = slab_node();
  2780. if (nid_alloc != nid_here)
  2781. return ____cache_alloc_node(cachep, flags, nid_alloc);
  2782. return NULL;
  2783. }
  2784. /*
  2785. * Fallback function if there was no memory available and no objects on a
  2786. * certain node and fall back is permitted. First we scan all the
  2787. * available node for available objects. If that fails then we
  2788. * perform an allocation without specifying a node. This allows the page
  2789. * allocator to do its reclaim / fallback magic. We then insert the
  2790. * slab into the proper nodelist and then allocate from it.
  2791. */
  2792. static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
  2793. {
  2794. struct zonelist *zonelist;
  2795. gfp_t local_flags;
  2796. struct zoneref *z;
  2797. struct zone *zone;
  2798. enum zone_type high_zoneidx = gfp_zone(flags);
  2799. void *obj = NULL;
  2800. int nid;
  2801. unsigned int cpuset_mems_cookie;
  2802. if (flags & __GFP_THISNODE)
  2803. return NULL;
  2804. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2805. retry_cpuset:
  2806. cpuset_mems_cookie = get_mems_allowed();
  2807. zonelist = node_zonelist(slab_node(), flags);
  2808. retry:
  2809. /*
  2810. * Look through allowed nodes for objects available
  2811. * from existing per node queues.
  2812. */
  2813. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  2814. nid = zone_to_nid(zone);
  2815. if (cpuset_zone_allowed_hardwall(zone, flags) &&
  2816. cache->node[nid] &&
  2817. cache->node[nid]->free_objects) {
  2818. obj = ____cache_alloc_node(cache,
  2819. flags | GFP_THISNODE, nid);
  2820. if (obj)
  2821. break;
  2822. }
  2823. }
  2824. if (!obj) {
  2825. /*
  2826. * This allocation will be performed within the constraints
  2827. * of the current cpuset / memory policy requirements.
  2828. * We may trigger various forms of reclaim on the allowed
  2829. * set and go into memory reserves if necessary.
  2830. */
  2831. if (local_flags & __GFP_WAIT)
  2832. local_irq_enable();
  2833. kmem_flagcheck(cache, flags);
  2834. obj = kmem_getpages(cache, local_flags, numa_mem_id());
  2835. if (local_flags & __GFP_WAIT)
  2836. local_irq_disable();
  2837. if (obj) {
  2838. /*
  2839. * Insert into the appropriate per node queues
  2840. */
  2841. nid = page_to_nid(virt_to_page(obj));
  2842. if (cache_grow(cache, flags, nid, obj)) {
  2843. obj = ____cache_alloc_node(cache,
  2844. flags | GFP_THISNODE, nid);
  2845. if (!obj)
  2846. /*
  2847. * Another processor may allocate the
  2848. * objects in the slab since we are
  2849. * not holding any locks.
  2850. */
  2851. goto retry;
  2852. } else {
  2853. /* cache_grow already freed obj */
  2854. obj = NULL;
  2855. }
  2856. }
  2857. }
  2858. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
  2859. goto retry_cpuset;
  2860. return obj;
  2861. }
  2862. /*
  2863. * A interface to enable slab creation on nodeid
  2864. */
  2865. static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  2866. int nodeid)
  2867. {
  2868. struct list_head *entry;
  2869. struct slab *slabp;
  2870. struct kmem_cache_node *n;
  2871. void *obj;
  2872. int x;
  2873. VM_BUG_ON(nodeid > num_online_nodes());
  2874. n = cachep->node[nodeid];
  2875. BUG_ON(!n);
  2876. retry:
  2877. check_irq_off();
  2878. spin_lock(&n->list_lock);
  2879. entry = n->slabs_partial.next;
  2880. if (entry == &n->slabs_partial) {
  2881. n->free_touched = 1;
  2882. entry = n->slabs_free.next;
  2883. if (entry == &n->slabs_free)
  2884. goto must_grow;
  2885. }
  2886. slabp = list_entry(entry, struct slab, list);
  2887. check_spinlock_acquired_node(cachep, nodeid);
  2888. check_slabp(cachep, slabp);
  2889. STATS_INC_NODEALLOCS(cachep);
  2890. STATS_INC_ACTIVE(cachep);
  2891. STATS_SET_HIGH(cachep);
  2892. BUG_ON(slabp->inuse == cachep->num);
  2893. obj = slab_get_obj(cachep, slabp, nodeid);
  2894. check_slabp(cachep, slabp);
  2895. n->free_objects--;
  2896. /* move slabp to correct slabp list: */
  2897. list_del(&slabp->list);
  2898. if (slabp->free == BUFCTL_END)
  2899. list_add(&slabp->list, &n->slabs_full);
  2900. else
  2901. list_add(&slabp->list, &n->slabs_partial);
  2902. spin_unlock(&n->list_lock);
  2903. goto done;
  2904. must_grow:
  2905. spin_unlock(&n->list_lock);
  2906. x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
  2907. if (x)
  2908. goto retry;
  2909. return fallback_alloc(cachep, flags);
  2910. done:
  2911. return obj;
  2912. }
  2913. static __always_inline void *
  2914. slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
  2915. unsigned long caller)
  2916. {
  2917. unsigned long save_flags;
  2918. void *ptr;
  2919. int slab_node = numa_mem_id();
  2920. flags &= gfp_allowed_mask;
  2921. lockdep_trace_alloc(flags);
  2922. if (slab_should_failslab(cachep, flags))
  2923. return NULL;
  2924. cachep = memcg_kmem_get_cache(cachep, flags);
  2925. cache_alloc_debugcheck_before(cachep, flags);
  2926. local_irq_save(save_flags);
  2927. if (nodeid == NUMA_NO_NODE)
  2928. nodeid = slab_node;
  2929. if (unlikely(!cachep->node[nodeid])) {
  2930. /* Node not bootstrapped yet */
  2931. ptr = fallback_alloc(cachep, flags);
  2932. goto out;
  2933. }
  2934. if (nodeid == slab_node) {
  2935. /*
  2936. * Use the locally cached objects if possible.
  2937. * However ____cache_alloc does not allow fallback
  2938. * to other nodes. It may fail while we still have
  2939. * objects on other nodes available.
  2940. */
  2941. ptr = ____cache_alloc(cachep, flags);
  2942. if (ptr)
  2943. goto out;
  2944. }
  2945. /* ___cache_alloc_node can fall back to other nodes */
  2946. ptr = ____cache_alloc_node(cachep, flags, nodeid);
  2947. out:
  2948. local_irq_restore(save_flags);
  2949. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
  2950. kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
  2951. flags);
  2952. if (likely(ptr))
  2953. kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
  2954. if (unlikely((flags & __GFP_ZERO) && ptr))
  2955. memset(ptr, 0, cachep->object_size);
  2956. return ptr;
  2957. }
  2958. static __always_inline void *
  2959. __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
  2960. {
  2961. void *objp;
  2962. if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
  2963. objp = alternate_node_alloc(cache, flags);
  2964. if (objp)
  2965. goto out;
  2966. }
  2967. objp = ____cache_alloc(cache, flags);
  2968. /*
  2969. * We may just have run out of memory on the local node.
  2970. * ____cache_alloc_node() knows how to locate memory on other nodes
  2971. */
  2972. if (!objp)
  2973. objp = ____cache_alloc_node(cache, flags, numa_mem_id());
  2974. out:
  2975. return objp;
  2976. }
  2977. #else
  2978. static __always_inline void *
  2979. __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2980. {
  2981. return ____cache_alloc(cachep, flags);
  2982. }
  2983. #endif /* CONFIG_NUMA */
  2984. static __always_inline void *
  2985. slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
  2986. {
  2987. unsigned long save_flags;
  2988. void *objp;
  2989. flags &= gfp_allowed_mask;
  2990. lockdep_trace_alloc(flags);
  2991. if (slab_should_failslab(cachep, flags))
  2992. return NULL;
  2993. cachep = memcg_kmem_get_cache(cachep, flags);
  2994. cache_alloc_debugcheck_before(cachep, flags);
  2995. local_irq_save(save_flags);
  2996. objp = __do_cache_alloc(cachep, flags);
  2997. local_irq_restore(save_flags);
  2998. objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
  2999. kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
  3000. flags);
  3001. prefetchw(objp);
  3002. if (likely(objp))
  3003. kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
  3004. if (unlikely((flags & __GFP_ZERO) && objp))
  3005. memset(objp, 0, cachep->object_size);
  3006. return objp;
  3007. }
  3008. /*
  3009. * Caller needs to acquire correct kmem_list's list_lock
  3010. */
  3011. static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
  3012. int node)
  3013. {
  3014. int i;
  3015. struct kmem_cache_node *n;
  3016. for (i = 0; i < nr_objects; i++) {
  3017. void *objp;
  3018. struct slab *slabp;
  3019. clear_obj_pfmemalloc(&objpp[i]);
  3020. objp = objpp[i];
  3021. slabp = virt_to_slab(objp);
  3022. n = cachep->node[node];
  3023. list_del(&slabp->list);
  3024. check_spinlock_acquired_node(cachep, node);
  3025. check_slabp(cachep, slabp);
  3026. slab_put_obj(cachep, slabp, objp, node);
  3027. STATS_DEC_ACTIVE(cachep);
  3028. n->free_objects++;
  3029. check_slabp(cachep, slabp);
  3030. /* fixup slab chains */
  3031. if (slabp->inuse == 0) {
  3032. if (n->free_objects > n->free_limit) {
  3033. n->free_objects -= cachep->num;
  3034. /* No need to drop any previously held
  3035. * lock here, even if we have a off-slab slab
  3036. * descriptor it is guaranteed to come from
  3037. * a different cache, refer to comments before
  3038. * alloc_slabmgmt.
  3039. */
  3040. slab_destroy(cachep, slabp);
  3041. } else {
  3042. list_add(&slabp->list, &n->slabs_free);
  3043. }
  3044. } else {
  3045. /* Unconditionally move a slab to the end of the
  3046. * partial list on free - maximum time for the
  3047. * other objects to be freed, too.
  3048. */
  3049. list_add_tail(&slabp->list, &n->slabs_partial);
  3050. }
  3051. }
  3052. }
  3053. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  3054. {
  3055. int batchcount;
  3056. struct kmem_cache_node *n;
  3057. int node = numa_mem_id();
  3058. batchcount = ac->batchcount;
  3059. #if DEBUG
  3060. BUG_ON(!batchcount || batchcount > ac->avail);
  3061. #endif
  3062. check_irq_off();
  3063. n = cachep->node[node];
  3064. spin_lock(&n->list_lock);
  3065. if (n->shared) {
  3066. struct array_cache *shared_array = n->shared;
  3067. int max = shared_array->limit - shared_array->avail;
  3068. if (max) {
  3069. if (batchcount > max)
  3070. batchcount = max;
  3071. memcpy(&(shared_array->entry[shared_array->avail]),
  3072. ac->entry, sizeof(void *) * batchcount);
  3073. shared_array->avail += batchcount;
  3074. goto free_done;
  3075. }
  3076. }
  3077. free_block(cachep, ac->entry, batchcount, node);
  3078. free_done:
  3079. #if STATS
  3080. {
  3081. int i = 0;
  3082. struct list_head *p;
  3083. p = n->slabs_free.next;
  3084. while (p != &(n->slabs_free)) {
  3085. struct slab *slabp;
  3086. slabp = list_entry(p, struct slab, list);
  3087. BUG_ON(slabp->inuse);
  3088. i++;
  3089. p = p->next;
  3090. }
  3091. STATS_SET_FREEABLE(cachep, i);
  3092. }
  3093. #endif
  3094. spin_unlock(&n->list_lock);
  3095. ac->avail -= batchcount;
  3096. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  3097. }
  3098. /*
  3099. * Release an obj back to its cache. If the obj has a constructed state, it must
  3100. * be in this state _before_ it is released. Called with disabled ints.
  3101. */
  3102. static inline void __cache_free(struct kmem_cache *cachep, void *objp,
  3103. unsigned long caller)
  3104. {
  3105. struct array_cache *ac = cpu_cache_get(cachep);
  3106. check_irq_off();
  3107. kmemleak_free_recursive(objp, cachep->flags);
  3108. objp = cache_free_debugcheck(cachep, objp, caller);
  3109. kmemcheck_slab_free(cachep, objp, cachep->object_size);
  3110. /*
  3111. * Skip calling cache_free_alien() when the platform is not numa.
  3112. * This will avoid cache misses that happen while accessing slabp (which
  3113. * is per page memory reference) to get nodeid. Instead use a global
  3114. * variable to skip the call, which is mostly likely to be present in
  3115. * the cache.
  3116. */
  3117. if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
  3118. return;
  3119. if (likely(ac->avail < ac->limit)) {
  3120. STATS_INC_FREEHIT(cachep);
  3121. } else {
  3122. STATS_INC_FREEMISS(cachep);
  3123. cache_flusharray(cachep, ac);
  3124. }
  3125. ac_put_obj(cachep, ac, objp);
  3126. }
  3127. /**
  3128. * kmem_cache_alloc - Allocate an object
  3129. * @cachep: The cache to allocate from.
  3130. * @flags: See kmalloc().
  3131. *
  3132. * Allocate an object from this cache. The flags are only relevant
  3133. * if the cache has no available objects.
  3134. */
  3135. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  3136. {
  3137. void *ret = slab_alloc(cachep, flags, _RET_IP_);
  3138. trace_kmem_cache_alloc(_RET_IP_, ret,
  3139. cachep->object_size, cachep->size, flags);
  3140. return ret;
  3141. }
  3142. EXPORT_SYMBOL(kmem_cache_alloc);
  3143. #ifdef CONFIG_TRACING
  3144. void *
  3145. kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
  3146. {
  3147. void *ret;
  3148. ret = slab_alloc(cachep, flags, _RET_IP_);
  3149. trace_kmalloc(_RET_IP_, ret,
  3150. size, cachep->size, flags);
  3151. return ret;
  3152. }
  3153. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  3154. #endif
  3155. #ifdef CONFIG_NUMA
  3156. /**
  3157. * kmem_cache_alloc_node - Allocate an object on the specified node
  3158. * @cachep: The cache to allocate from.
  3159. * @flags: See kmalloc().
  3160. * @nodeid: node number of the target node.
  3161. *
  3162. * Identical to kmem_cache_alloc but it will allocate memory on the given
  3163. * node, which can improve the performance for cpu bound structures.
  3164. *
  3165. * Fallback to other node is possible if __GFP_THISNODE is not set.
  3166. */
  3167. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  3168. {
  3169. void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
  3170. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  3171. cachep->object_size, cachep->size,
  3172. flags, nodeid);
  3173. return ret;
  3174. }
  3175. EXPORT_SYMBOL(kmem_cache_alloc_node);
  3176. #ifdef CONFIG_TRACING
  3177. void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
  3178. gfp_t flags,
  3179. int nodeid,
  3180. size_t size)
  3181. {
  3182. void *ret;
  3183. ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
  3184. trace_kmalloc_node(_RET_IP_, ret,
  3185. size, cachep->size,
  3186. flags, nodeid);
  3187. return ret;
  3188. }
  3189. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  3190. #endif
  3191. static __always_inline void *
  3192. __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
  3193. {
  3194. struct kmem_cache *cachep;
  3195. cachep = kmalloc_slab(size, flags);
  3196. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3197. return cachep;
  3198. return kmem_cache_alloc_node_trace(cachep, flags, node, size);
  3199. }
  3200. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3201. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3202. {
  3203. return __do_kmalloc_node(size, flags, node, _RET_IP_);
  3204. }
  3205. EXPORT_SYMBOL(__kmalloc_node);
  3206. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  3207. int node, unsigned long caller)
  3208. {
  3209. return __do_kmalloc_node(size, flags, node, caller);
  3210. }
  3211. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  3212. #else
  3213. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3214. {
  3215. return __do_kmalloc_node(size, flags, node, 0);
  3216. }
  3217. EXPORT_SYMBOL(__kmalloc_node);
  3218. #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
  3219. #endif /* CONFIG_NUMA */
  3220. /**
  3221. * __do_kmalloc - allocate memory
  3222. * @size: how many bytes of memory are required.
  3223. * @flags: the type of memory to allocate (see kmalloc).
  3224. * @caller: function caller for debug tracking of the caller
  3225. */
  3226. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  3227. unsigned long caller)
  3228. {
  3229. struct kmem_cache *cachep;
  3230. void *ret;
  3231. /* If you want to save a few bytes .text space: replace
  3232. * __ with kmem_.
  3233. * Then kmalloc uses the uninlined functions instead of the inline
  3234. * functions.
  3235. */
  3236. cachep = kmalloc_slab(size, flags);
  3237. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3238. return cachep;
  3239. ret = slab_alloc(cachep, flags, caller);
  3240. trace_kmalloc(caller, ret,
  3241. size, cachep->size, flags);
  3242. return ret;
  3243. }
  3244. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3245. void *__kmalloc(size_t size, gfp_t flags)
  3246. {
  3247. return __do_kmalloc(size, flags, _RET_IP_);
  3248. }
  3249. EXPORT_SYMBOL(__kmalloc);
  3250. void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
  3251. {
  3252. return __do_kmalloc(size, flags, caller);
  3253. }
  3254. EXPORT_SYMBOL(__kmalloc_track_caller);
  3255. #else
  3256. void *__kmalloc(size_t size, gfp_t flags)
  3257. {
  3258. return __do_kmalloc(size, flags, 0);
  3259. }
  3260. EXPORT_SYMBOL(__kmalloc);
  3261. #endif
  3262. /**
  3263. * kmem_cache_free - Deallocate an object
  3264. * @cachep: The cache the allocation was from.
  3265. * @objp: The previously allocated object.
  3266. *
  3267. * Free an object which was previously allocated from this
  3268. * cache.
  3269. */
  3270. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  3271. {
  3272. unsigned long flags;
  3273. cachep = cache_from_obj(cachep, objp);
  3274. if (!cachep)
  3275. return;
  3276. local_irq_save(flags);
  3277. debug_check_no_locks_freed(objp, cachep->object_size);
  3278. if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
  3279. debug_check_no_obj_freed(objp, cachep->object_size);
  3280. __cache_free(cachep, objp, _RET_IP_);
  3281. local_irq_restore(flags);
  3282. trace_kmem_cache_free(_RET_IP_, objp);
  3283. }
  3284. EXPORT_SYMBOL(kmem_cache_free);
  3285. /**
  3286. * kfree - free previously allocated memory
  3287. * @objp: pointer returned by kmalloc.
  3288. *
  3289. * If @objp is NULL, no operation is performed.
  3290. *
  3291. * Don't free memory not originally allocated by kmalloc()
  3292. * or you will run into trouble.
  3293. */
  3294. void kfree(const void *objp)
  3295. {
  3296. struct kmem_cache *c;
  3297. unsigned long flags;
  3298. trace_kfree(_RET_IP_, objp);
  3299. if (unlikely(ZERO_OR_NULL_PTR(objp)))
  3300. return;
  3301. local_irq_save(flags);
  3302. kfree_debugcheck(objp);
  3303. c = virt_to_cache(objp);
  3304. debug_check_no_locks_freed(objp, c->object_size);
  3305. debug_check_no_obj_freed(objp, c->object_size);
  3306. __cache_free(c, (void *)objp, _RET_IP_);
  3307. local_irq_restore(flags);
  3308. }
  3309. EXPORT_SYMBOL(kfree);
  3310. /*
  3311. * This initializes kmem_cache_node or resizes various caches for all nodes.
  3312. */
  3313. static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
  3314. {
  3315. int node;
  3316. struct kmem_cache_node *n;
  3317. struct array_cache *new_shared;
  3318. struct array_cache **new_alien = NULL;
  3319. for_each_online_node(node) {
  3320. if (use_alien_caches) {
  3321. new_alien = alloc_alien_cache(node, cachep->limit, gfp);
  3322. if (!new_alien)
  3323. goto fail;
  3324. }
  3325. new_shared = NULL;
  3326. if (cachep->shared) {
  3327. new_shared = alloc_arraycache(node,
  3328. cachep->shared*cachep->batchcount,
  3329. 0xbaadf00d, gfp);
  3330. if (!new_shared) {
  3331. free_alien_cache(new_alien);
  3332. goto fail;
  3333. }
  3334. }
  3335. n = cachep->node[node];
  3336. if (n) {
  3337. struct array_cache *shared = n->shared;
  3338. spin_lock_irq(&n->list_lock);
  3339. if (shared)
  3340. free_block(cachep, shared->entry,
  3341. shared->avail, node);
  3342. n->shared = new_shared;
  3343. if (!n->alien) {
  3344. n->alien = new_alien;
  3345. new_alien = NULL;
  3346. }
  3347. n->free_limit = (1 + nr_cpus_node(node)) *
  3348. cachep->batchcount + cachep->num;
  3349. spin_unlock_irq(&n->list_lock);
  3350. kfree(shared);
  3351. free_alien_cache(new_alien);
  3352. continue;
  3353. }
  3354. n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
  3355. if (!n) {
  3356. free_alien_cache(new_alien);
  3357. kfree(new_shared);
  3358. goto fail;
  3359. }
  3360. kmem_cache_node_init(n);
  3361. n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  3362. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  3363. n->shared = new_shared;
  3364. n->alien = new_alien;
  3365. n->free_limit = (1 + nr_cpus_node(node)) *
  3366. cachep->batchcount + cachep->num;
  3367. cachep->node[node] = n;
  3368. }
  3369. return 0;
  3370. fail:
  3371. if (!cachep->list.next) {
  3372. /* Cache is not active yet. Roll back what we did */
  3373. node--;
  3374. while (node >= 0) {
  3375. if (cachep->node[node]) {
  3376. n = cachep->node[node];
  3377. kfree(n->shared);
  3378. free_alien_cache(n->alien);
  3379. kfree(n);
  3380. cachep->node[node] = NULL;
  3381. }
  3382. node--;
  3383. }
  3384. }
  3385. return -ENOMEM;
  3386. }
  3387. struct ccupdate_struct {
  3388. struct kmem_cache *cachep;
  3389. struct array_cache *new[0];
  3390. };
  3391. static void do_ccupdate_local(void *info)
  3392. {
  3393. struct ccupdate_struct *new = info;
  3394. struct array_cache *old;
  3395. check_irq_off();
  3396. old = cpu_cache_get(new->cachep);
  3397. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  3398. new->new[smp_processor_id()] = old;
  3399. }
  3400. /* Always called with the slab_mutex held */
  3401. static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3402. int batchcount, int shared, gfp_t gfp)
  3403. {
  3404. struct ccupdate_struct *new;
  3405. int i;
  3406. new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
  3407. gfp);
  3408. if (!new)
  3409. return -ENOMEM;
  3410. for_each_online_cpu(i) {
  3411. new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
  3412. batchcount, gfp);
  3413. if (!new->new[i]) {
  3414. for (i--; i >= 0; i--)
  3415. kfree(new->new[i]);
  3416. kfree(new);
  3417. return -ENOMEM;
  3418. }
  3419. }
  3420. new->cachep = cachep;
  3421. on_each_cpu(do_ccupdate_local, (void *)new, 1);
  3422. check_irq_on();
  3423. cachep->batchcount = batchcount;
  3424. cachep->limit = limit;
  3425. cachep->shared = shared;
  3426. for_each_online_cpu(i) {
  3427. struct array_cache *ccold = new->new[i];
  3428. if (!ccold)
  3429. continue;
  3430. spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
  3431. free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
  3432. spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
  3433. kfree(ccold);
  3434. }
  3435. kfree(new);
  3436. return alloc_kmemlist(cachep, gfp);
  3437. }
  3438. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3439. int batchcount, int shared, gfp_t gfp)
  3440. {
  3441. int ret;
  3442. struct kmem_cache *c = NULL;
  3443. int i = 0;
  3444. ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
  3445. if (slab_state < FULL)
  3446. return ret;
  3447. if ((ret < 0) || !is_root_cache(cachep))
  3448. return ret;
  3449. VM_BUG_ON(!mutex_is_locked(&slab_mutex));
  3450. for_each_memcg_cache_index(i) {
  3451. c = cache_from_memcg(cachep, i);
  3452. if (c)
  3453. /* return value determined by the parent cache only */
  3454. __do_tune_cpucache(c, limit, batchcount, shared, gfp);
  3455. }
  3456. return ret;
  3457. }
  3458. /* Called with slab_mutex held always */
  3459. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
  3460. {
  3461. int err;
  3462. int limit = 0;
  3463. int shared = 0;
  3464. int batchcount = 0;
  3465. if (!is_root_cache(cachep)) {
  3466. struct kmem_cache *root = memcg_root_cache(cachep);
  3467. limit = root->limit;
  3468. shared = root->shared;
  3469. batchcount = root->batchcount;
  3470. }
  3471. if (limit && shared && batchcount)
  3472. goto skip_setup;
  3473. /*
  3474. * The head array serves three purposes:
  3475. * - create a LIFO ordering, i.e. return objects that are cache-warm
  3476. * - reduce the number of spinlock operations.
  3477. * - reduce the number of linked list operations on the slab and
  3478. * bufctl chains: array operations are cheaper.
  3479. * The numbers are guessed, we should auto-tune as described by
  3480. * Bonwick.
  3481. */
  3482. if (cachep->size > 131072)
  3483. limit = 1;
  3484. else if (cachep->size > PAGE_SIZE)
  3485. limit = 8;
  3486. else if (cachep->size > 1024)
  3487. limit = 24;
  3488. else if (cachep->size > 256)
  3489. limit = 54;
  3490. else
  3491. limit = 120;
  3492. /*
  3493. * CPU bound tasks (e.g. network routing) can exhibit cpu bound
  3494. * allocation behaviour: Most allocs on one cpu, most free operations
  3495. * on another cpu. For these cases, an efficient object passing between
  3496. * cpus is necessary. This is provided by a shared array. The array
  3497. * replaces Bonwick's magazine layer.
  3498. * On uniprocessor, it's functionally equivalent (but less efficient)
  3499. * to a larger limit. Thus disabled by default.
  3500. */
  3501. shared = 0;
  3502. if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
  3503. shared = 8;
  3504. #if DEBUG
  3505. /*
  3506. * With debugging enabled, large batchcount lead to excessively long
  3507. * periods with disabled local interrupts. Limit the batchcount
  3508. */
  3509. if (limit > 32)
  3510. limit = 32;
  3511. #endif
  3512. batchcount = (limit + 1) / 2;
  3513. skip_setup:
  3514. err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
  3515. if (err)
  3516. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  3517. cachep->name, -err);
  3518. return err;
  3519. }
  3520. /*
  3521. * Drain an array if it contains any elements taking the node lock only if
  3522. * necessary. Note that the node listlock also protects the array_cache
  3523. * if drain_array() is used on the shared array.
  3524. */
  3525. static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
  3526. struct array_cache *ac, int force, int node)
  3527. {
  3528. int tofree;
  3529. if (!ac || !ac->avail)
  3530. return;
  3531. if (ac->touched && !force) {
  3532. ac->touched = 0;
  3533. } else {
  3534. spin_lock_irq(&n->list_lock);
  3535. if (ac->avail) {
  3536. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  3537. if (tofree > ac->avail)
  3538. tofree = (ac->avail + 1) / 2;
  3539. free_block(cachep, ac->entry, tofree, node);
  3540. ac->avail -= tofree;
  3541. memmove(ac->entry, &(ac->entry[tofree]),
  3542. sizeof(void *) * ac->avail);
  3543. }
  3544. spin_unlock_irq(&n->list_lock);
  3545. }
  3546. }
  3547. /**
  3548. * cache_reap - Reclaim memory from caches.
  3549. * @w: work descriptor
  3550. *
  3551. * Called from workqueue/eventd every few seconds.
  3552. * Purpose:
  3553. * - clear the per-cpu caches for this CPU.
  3554. * - return freeable pages to the main free memory pool.
  3555. *
  3556. * If we cannot acquire the cache chain mutex then just give up - we'll try
  3557. * again on the next iteration.
  3558. */
  3559. static void cache_reap(struct work_struct *w)
  3560. {
  3561. struct kmem_cache *searchp;
  3562. struct kmem_cache_node *n;
  3563. int node = numa_mem_id();
  3564. struct delayed_work *work = to_delayed_work(w);
  3565. if (!mutex_trylock(&slab_mutex))
  3566. /* Give up. Setup the next iteration. */
  3567. goto out;
  3568. list_for_each_entry(searchp, &slab_caches, list) {
  3569. check_irq_on();
  3570. /*
  3571. * We only take the node lock if absolutely necessary and we
  3572. * have established with reasonable certainty that
  3573. * we can do some work if the lock was obtained.
  3574. */
  3575. n = searchp->node[node];
  3576. reap_alien(searchp, n);
  3577. drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
  3578. /*
  3579. * These are racy checks but it does not matter
  3580. * if we skip one check or scan twice.
  3581. */
  3582. if (time_after(n->next_reap, jiffies))
  3583. goto next;
  3584. n->next_reap = jiffies + REAPTIMEOUT_LIST3;
  3585. drain_array(searchp, n, n->shared, 0, node);
  3586. if (n->free_touched)
  3587. n->free_touched = 0;
  3588. else {
  3589. int freed;
  3590. freed = drain_freelist(searchp, n, (n->free_limit +
  3591. 5 * searchp->num - 1) / (5 * searchp->num));
  3592. STATS_ADD_REAPED(searchp, freed);
  3593. }
  3594. next:
  3595. cond_resched();
  3596. }
  3597. check_irq_on();
  3598. mutex_unlock(&slab_mutex);
  3599. next_reap_node();
  3600. out:
  3601. /* Set up the next iteration */
  3602. schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
  3603. }
  3604. #ifdef CONFIG_SLABINFO
  3605. void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
  3606. {
  3607. struct slab *slabp;
  3608. unsigned long active_objs;
  3609. unsigned long num_objs;
  3610. unsigned long active_slabs = 0;
  3611. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  3612. const char *name;
  3613. char *error = NULL;
  3614. int node;
  3615. struct kmem_cache_node *n;
  3616. active_objs = 0;
  3617. num_slabs = 0;
  3618. for_each_online_node(node) {
  3619. n = cachep->node[node];
  3620. if (!n)
  3621. continue;
  3622. check_irq_on();
  3623. spin_lock_irq(&n->list_lock);
  3624. list_for_each_entry(slabp, &n->slabs_full, list) {
  3625. if (slabp->inuse != cachep->num && !error)
  3626. error = "slabs_full accounting error";
  3627. active_objs += cachep->num;
  3628. active_slabs++;
  3629. }
  3630. list_for_each_entry(slabp, &n->slabs_partial, list) {
  3631. if (slabp->inuse == cachep->num && !error)
  3632. error = "slabs_partial inuse accounting error";
  3633. if (!slabp->inuse && !error)
  3634. error = "slabs_partial/inuse accounting error";
  3635. active_objs += slabp->inuse;
  3636. active_slabs++;
  3637. }
  3638. list_for_each_entry(slabp, &n->slabs_free, list) {
  3639. if (slabp->inuse && !error)
  3640. error = "slabs_free/inuse accounting error";
  3641. num_slabs++;
  3642. }
  3643. free_objects += n->free_objects;
  3644. if (n->shared)
  3645. shared_avail += n->shared->avail;
  3646. spin_unlock_irq(&n->list_lock);
  3647. }
  3648. num_slabs += active_slabs;
  3649. num_objs = num_slabs * cachep->num;
  3650. if (num_objs - active_objs != free_objects && !error)
  3651. error = "free_objects accounting error";
  3652. name = cachep->name;
  3653. if (error)
  3654. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  3655. sinfo->active_objs = active_objs;
  3656. sinfo->num_objs = num_objs;
  3657. sinfo->active_slabs = active_slabs;
  3658. sinfo->num_slabs = num_slabs;
  3659. sinfo->shared_avail = shared_avail;
  3660. sinfo->limit = cachep->limit;
  3661. sinfo->batchcount = cachep->batchcount;
  3662. sinfo->shared = cachep->shared;
  3663. sinfo->objects_per_slab = cachep->num;
  3664. sinfo->cache_order = cachep->gfporder;
  3665. }
  3666. void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
  3667. {
  3668. #if STATS
  3669. { /* node stats */
  3670. unsigned long high = cachep->high_mark;
  3671. unsigned long allocs = cachep->num_allocations;
  3672. unsigned long grown = cachep->grown;
  3673. unsigned long reaped = cachep->reaped;
  3674. unsigned long errors = cachep->errors;
  3675. unsigned long max_freeable = cachep->max_freeable;
  3676. unsigned long node_allocs = cachep->node_allocs;
  3677. unsigned long node_frees = cachep->node_frees;
  3678. unsigned long overflows = cachep->node_overflow;
  3679. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
  3680. "%4lu %4lu %4lu %4lu %4lu",
  3681. allocs, high, grown,
  3682. reaped, errors, max_freeable, node_allocs,
  3683. node_frees, overflows);
  3684. }
  3685. /* cpu stats */
  3686. {
  3687. unsigned long allochit = atomic_read(&cachep->allochit);
  3688. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  3689. unsigned long freehit = atomic_read(&cachep->freehit);
  3690. unsigned long freemiss = atomic_read(&cachep->freemiss);
  3691. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  3692. allochit, allocmiss, freehit, freemiss);
  3693. }
  3694. #endif
  3695. }
  3696. #define MAX_SLABINFO_WRITE 128
  3697. /**
  3698. * slabinfo_write - Tuning for the slab allocator
  3699. * @file: unused
  3700. * @buffer: user buffer
  3701. * @count: data length
  3702. * @ppos: unused
  3703. */
  3704. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  3705. size_t count, loff_t *ppos)
  3706. {
  3707. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  3708. int limit, batchcount, shared, res;
  3709. struct kmem_cache *cachep;
  3710. if (count > MAX_SLABINFO_WRITE)
  3711. return -EINVAL;
  3712. if (copy_from_user(&kbuf, buffer, count))
  3713. return -EFAULT;
  3714. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3715. tmp = strchr(kbuf, ' ');
  3716. if (!tmp)
  3717. return -EINVAL;
  3718. *tmp = '\0';
  3719. tmp++;
  3720. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3721. return -EINVAL;
  3722. /* Find the cache in the chain of caches. */
  3723. mutex_lock(&slab_mutex);
  3724. res = -EINVAL;
  3725. list_for_each_entry(cachep, &slab_caches, list) {
  3726. if (!strcmp(cachep->name, kbuf)) {
  3727. if (limit < 1 || batchcount < 1 ||
  3728. batchcount > limit || shared < 0) {
  3729. res = 0;
  3730. } else {
  3731. res = do_tune_cpucache(cachep, limit,
  3732. batchcount, shared,
  3733. GFP_KERNEL);
  3734. }
  3735. break;
  3736. }
  3737. }
  3738. mutex_unlock(&slab_mutex);
  3739. if (res >= 0)
  3740. res = count;
  3741. return res;
  3742. }
  3743. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3744. static void *leaks_start(struct seq_file *m, loff_t *pos)
  3745. {
  3746. mutex_lock(&slab_mutex);
  3747. return seq_list_start(&slab_caches, *pos);
  3748. }
  3749. static inline int add_caller(unsigned long *n, unsigned long v)
  3750. {
  3751. unsigned long *p;
  3752. int l;
  3753. if (!v)
  3754. return 1;
  3755. l = n[1];
  3756. p = n + 2;
  3757. while (l) {
  3758. int i = l/2;
  3759. unsigned long *q = p + 2 * i;
  3760. if (*q == v) {
  3761. q[1]++;
  3762. return 1;
  3763. }
  3764. if (*q > v) {
  3765. l = i;
  3766. } else {
  3767. p = q + 2;
  3768. l -= i + 1;
  3769. }
  3770. }
  3771. if (++n[1] == n[0])
  3772. return 0;
  3773. memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  3774. p[0] = v;
  3775. p[1] = 1;
  3776. return 1;
  3777. }
  3778. static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
  3779. {
  3780. void *p;
  3781. int i;
  3782. if (n[0] == n[1])
  3783. return;
  3784. for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
  3785. if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
  3786. continue;
  3787. if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
  3788. return;
  3789. }
  3790. }
  3791. static void show_symbol(struct seq_file *m, unsigned long address)
  3792. {
  3793. #ifdef CONFIG_KALLSYMS
  3794. unsigned long offset, size;
  3795. char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
  3796. if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
  3797. seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
  3798. if (modname[0])
  3799. seq_printf(m, " [%s]", modname);
  3800. return;
  3801. }
  3802. #endif
  3803. seq_printf(m, "%p", (void *)address);
  3804. }
  3805. static int leaks_show(struct seq_file *m, void *p)
  3806. {
  3807. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
  3808. struct slab *slabp;
  3809. struct kmem_cache_node *n;
  3810. const char *name;
  3811. unsigned long *x = m->private;
  3812. int node;
  3813. int i;
  3814. if (!(cachep->flags & SLAB_STORE_USER))
  3815. return 0;
  3816. if (!(cachep->flags & SLAB_RED_ZONE))
  3817. return 0;
  3818. /* OK, we can do it */
  3819. x[1] = 0;
  3820. for_each_online_node(node) {
  3821. n = cachep->node[node];
  3822. if (!n)
  3823. continue;
  3824. check_irq_on();
  3825. spin_lock_irq(&n->list_lock);
  3826. list_for_each_entry(slabp, &n->slabs_full, list)
  3827. handle_slab(x, cachep, slabp);
  3828. list_for_each_entry(slabp, &n->slabs_partial, list)
  3829. handle_slab(x, cachep, slabp);
  3830. spin_unlock_irq(&n->list_lock);
  3831. }
  3832. name = cachep->name;
  3833. if (x[0] == x[1]) {
  3834. /* Increase the buffer size */
  3835. mutex_unlock(&slab_mutex);
  3836. m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
  3837. if (!m->private) {
  3838. /* Too bad, we are really out */
  3839. m->private = x;
  3840. mutex_lock(&slab_mutex);
  3841. return -ENOMEM;
  3842. }
  3843. *(unsigned long *)m->private = x[0] * 2;
  3844. kfree(x);
  3845. mutex_lock(&slab_mutex);
  3846. /* Now make sure this entry will be retried */
  3847. m->count = m->size;
  3848. return 0;
  3849. }
  3850. for (i = 0; i < x[1]; i++) {
  3851. seq_printf(m, "%s: %lu ", name, x[2*i+3]);
  3852. show_symbol(m, x[2*i+2]);
  3853. seq_putc(m, '\n');
  3854. }
  3855. return 0;
  3856. }
  3857. static const struct seq_operations slabstats_op = {
  3858. .start = leaks_start,
  3859. .next = s_next,
  3860. .stop = s_stop,
  3861. .show = leaks_show,
  3862. };
  3863. static int slabstats_open(struct inode *inode, struct file *file)
  3864. {
  3865. unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  3866. int ret = -ENOMEM;
  3867. if (n) {
  3868. ret = seq_open(file, &slabstats_op);
  3869. if (!ret) {
  3870. struct seq_file *m = file->private_data;
  3871. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  3872. m->private = n;
  3873. n = NULL;
  3874. }
  3875. kfree(n);
  3876. }
  3877. return ret;
  3878. }
  3879. static const struct file_operations proc_slabstats_operations = {
  3880. .open = slabstats_open,
  3881. .read = seq_read,
  3882. .llseek = seq_lseek,
  3883. .release = seq_release_private,
  3884. };
  3885. #endif
  3886. static int __init slab_proc_init(void)
  3887. {
  3888. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3889. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  3890. #endif
  3891. return 0;
  3892. }
  3893. module_init(slab_proc_init);
  3894. #endif
  3895. /**
  3896. * ksize - get the actual amount of memory allocated for a given object
  3897. * @objp: Pointer to the object
  3898. *
  3899. * kmalloc may internally round up allocations and return more memory
  3900. * than requested. ksize() can be used to determine the actual amount of
  3901. * memory allocated. The caller may use this additional memory, even though
  3902. * a smaller amount of memory was initially specified with the kmalloc call.
  3903. * The caller must guarantee that objp points to a valid object previously
  3904. * allocated with either kmalloc() or kmem_cache_alloc(). The object
  3905. * must not be freed during the duration of the call.
  3906. */
  3907. size_t ksize(const void *objp)
  3908. {
  3909. BUG_ON(!objp);
  3910. if (unlikely(objp == ZERO_SIZE_PTR))
  3911. return 0;
  3912. return virt_to_cache(objp)->object_size;
  3913. }
  3914. EXPORT_SYMBOL(ksize);