slab.c 108 KB

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