slab.c 111 KB

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