slab.c 111 KB

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