slab.c 109 KB

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