slab.c 118 KB

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