slab.c 108 KB

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