slab.c 112 KB

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