slab.c 111 KB

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