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