slab.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876
  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 intializations 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 'cache_chain_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/config.h>
  89. #include <linux/slab.h>
  90. #include <linux/mm.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/seq_file.h>
  97. #include <linux/notifier.h>
  98. #include <linux/kallsyms.h>
  99. #include <linux/cpu.h>
  100. #include <linux/sysctl.h>
  101. #include <linux/module.h>
  102. #include <linux/rcupdate.h>
  103. #include <linux/string.h>
  104. #include <linux/nodemask.h>
  105. #include <linux/mempolicy.h>
  106. #include <linux/mutex.h>
  107. #include <asm/uaccess.h>
  108. #include <asm/cacheflush.h>
  109. #include <asm/tlbflush.h>
  110. #include <asm/page.h>
  111. /*
  112. * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
  113. * SLAB_RED_ZONE & SLAB_POISON.
  114. * 0 for faster, smaller code (especially in the critical paths).
  115. *
  116. * STATS - 1 to collect stats for /proc/slabinfo.
  117. * 0 for faster, smaller code (especially in the critical paths).
  118. *
  119. * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
  120. */
  121. #ifdef CONFIG_DEBUG_SLAB
  122. #define DEBUG 1
  123. #define STATS 1
  124. #define FORCED_DEBUG 1
  125. #else
  126. #define DEBUG 0
  127. #define STATS 0
  128. #define FORCED_DEBUG 0
  129. #endif
  130. /* Shouldn't this be in a header file somewhere? */
  131. #define BYTES_PER_WORD sizeof(void *)
  132. #ifndef cache_line_size
  133. #define cache_line_size() L1_CACHE_BYTES
  134. #endif
  135. #ifndef ARCH_KMALLOC_MINALIGN
  136. /*
  137. * Enforce a minimum alignment for the kmalloc caches.
  138. * Usually, the kmalloc caches are cache_line_size() aligned, except when
  139. * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
  140. * Some archs want to perform DMA into kmalloc caches and need a guaranteed
  141. * alignment larger than BYTES_PER_WORD. ARCH_KMALLOC_MINALIGN allows that.
  142. * Note that this flag disables some debug features.
  143. */
  144. #define ARCH_KMALLOC_MINALIGN 0
  145. #endif
  146. #ifndef ARCH_SLAB_MINALIGN
  147. /*
  148. * Enforce a minimum alignment for all caches.
  149. * Intended for archs that get misalignment faults even for BYTES_PER_WORD
  150. * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
  151. * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
  152. * some debug features.
  153. */
  154. #define ARCH_SLAB_MINALIGN 0
  155. #endif
  156. #ifndef ARCH_KMALLOC_FLAGS
  157. #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
  158. #endif
  159. /* Legal flag mask for kmem_cache_create(). */
  160. #if DEBUG
  161. # define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
  162. SLAB_POISON | SLAB_HWCACHE_ALIGN | \
  163. SLAB_CACHE_DMA | \
  164. SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \
  165. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  166. SLAB_DESTROY_BY_RCU)
  167. #else
  168. # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
  169. SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
  170. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  171. SLAB_DESTROY_BY_RCU)
  172. #endif
  173. /*
  174. * kmem_bufctl_t:
  175. *
  176. * Bufctl's are used for linking objs within a slab
  177. * linked offsets.
  178. *
  179. * This implementation relies on "struct page" for locating the cache &
  180. * slab an object belongs to.
  181. * This allows the bufctl structure to be small (one int), but limits
  182. * the number of objects a slab (not a cache) can contain when off-slab
  183. * bufctls are used. The limit is the size of the largest general cache
  184. * that does not use off-slab slabs.
  185. * For 32bit archs with 4 kB pages, is this 56.
  186. * This is not serious, as it is only for large objects, when it is unwise
  187. * to have too many per slab.
  188. * Note: This limit can be raised by introducing a general cache whose size
  189. * is less than 512 (PAGE_SIZE<<3), but greater than 256.
  190. */
  191. typedef unsigned int kmem_bufctl_t;
  192. #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
  193. #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
  194. #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-2)
  195. /* Max number of objs-per-slab for caches which use off-slab slabs.
  196. * Needed to avoid a possible looping condition in cache_grow().
  197. */
  198. static unsigned long offslab_limit;
  199. /*
  200. * struct slab
  201. *
  202. * Manages the objs in a slab. Placed either at the beginning of mem allocated
  203. * for a slab, or allocated from an general cache.
  204. * Slabs are chained into three list: fully used, partial, fully free slabs.
  205. */
  206. struct slab {
  207. struct list_head list;
  208. unsigned long colouroff;
  209. void *s_mem; /* including colour offset */
  210. unsigned int inuse; /* num of objs active in slab */
  211. kmem_bufctl_t free;
  212. unsigned short nodeid;
  213. };
  214. /*
  215. * struct slab_rcu
  216. *
  217. * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
  218. * arrange for kmem_freepages to be called via RCU. This is useful if
  219. * we need to approach a kernel structure obliquely, from its address
  220. * obtained without the usual locking. We can lock the structure to
  221. * stabilize it and check it's still at the given address, only if we
  222. * can be sure that the memory has not been meanwhile reused for some
  223. * other kind of object (which our subsystem's lock might corrupt).
  224. *
  225. * rcu_read_lock before reading the address, then rcu_read_unlock after
  226. * taking the spinlock within the structure expected at that address.
  227. *
  228. * We assume struct slab_rcu can overlay struct slab when destroying.
  229. */
  230. struct slab_rcu {
  231. struct rcu_head head;
  232. struct kmem_cache *cachep;
  233. void *addr;
  234. };
  235. /*
  236. * struct array_cache
  237. *
  238. * Purpose:
  239. * - LIFO ordering, to hand out cache-warm objects from _alloc
  240. * - reduce the number of linked list operations
  241. * - reduce spinlock operations
  242. *
  243. * The limit is stored in the per-cpu structure to reduce the data cache
  244. * footprint.
  245. *
  246. */
  247. struct array_cache {
  248. unsigned int avail;
  249. unsigned int limit;
  250. unsigned int batchcount;
  251. unsigned int touched;
  252. spinlock_t lock;
  253. void *entry[0]; /*
  254. * Must have this definition in here for the proper
  255. * alignment of array_cache. Also simplifies accessing
  256. * the entries.
  257. * [0] is for gcc 2.95. It should really be [].
  258. */
  259. };
  260. /*
  261. * bootstrap: The caches do not work without cpuarrays anymore, but the
  262. * cpuarrays are allocated from the generic caches...
  263. */
  264. #define BOOT_CPUCACHE_ENTRIES 1
  265. struct arraycache_init {
  266. struct array_cache cache;
  267. void *entries[BOOT_CPUCACHE_ENTRIES];
  268. };
  269. /*
  270. * The slab lists for all objects.
  271. */
  272. struct kmem_list3 {
  273. struct list_head slabs_partial; /* partial list first, better asm code */
  274. struct list_head slabs_full;
  275. struct list_head slabs_free;
  276. unsigned long free_objects;
  277. unsigned long next_reap;
  278. int free_touched;
  279. unsigned int free_limit;
  280. unsigned int colour_next; /* Per-node cache coloring */
  281. spinlock_t list_lock;
  282. struct array_cache *shared; /* shared per node */
  283. struct array_cache **alien; /* on other nodes */
  284. };
  285. /*
  286. * Need this for bootstrapping a per node allocator.
  287. */
  288. #define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
  289. struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
  290. #define CACHE_CACHE 0
  291. #define SIZE_AC 1
  292. #define SIZE_L3 (1 + MAX_NUMNODES)
  293. /*
  294. * This function must be completely optimized away if a constant is passed to
  295. * it. Mostly the same as what is in linux/slab.h except it returns an index.
  296. */
  297. static __always_inline int index_of(const size_t size)
  298. {
  299. extern void __bad_size(void);
  300. if (__builtin_constant_p(size)) {
  301. int i = 0;
  302. #define CACHE(x) \
  303. if (size <=x) \
  304. return i; \
  305. else \
  306. i++;
  307. #include "linux/kmalloc_sizes.h"
  308. #undef CACHE
  309. __bad_size();
  310. } else
  311. __bad_size();
  312. return 0;
  313. }
  314. #define INDEX_AC index_of(sizeof(struct arraycache_init))
  315. #define INDEX_L3 index_of(sizeof(struct kmem_list3))
  316. static void kmem_list3_init(struct kmem_list3 *parent)
  317. {
  318. INIT_LIST_HEAD(&parent->slabs_full);
  319. INIT_LIST_HEAD(&parent->slabs_partial);
  320. INIT_LIST_HEAD(&parent->slabs_free);
  321. parent->shared = NULL;
  322. parent->alien = NULL;
  323. parent->colour_next = 0;
  324. spin_lock_init(&parent->list_lock);
  325. parent->free_objects = 0;
  326. parent->free_touched = 0;
  327. }
  328. #define MAKE_LIST(cachep, listp, slab, nodeid) \
  329. do { \
  330. INIT_LIST_HEAD(listp); \
  331. list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
  332. } while (0)
  333. #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
  334. do { \
  335. MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
  336. MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
  337. MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
  338. } while (0)
  339. /*
  340. * struct kmem_cache
  341. *
  342. * manages a cache.
  343. */
  344. struct kmem_cache {
  345. /* 1) per-cpu data, touched during every alloc/free */
  346. struct array_cache *array[NR_CPUS];
  347. /* 2) Cache tunables. Protected by cache_chain_mutex */
  348. unsigned int batchcount;
  349. unsigned int limit;
  350. unsigned int shared;
  351. unsigned int buffer_size;
  352. /* 3) touched by every alloc & free from the backend */
  353. struct kmem_list3 *nodelists[MAX_NUMNODES];
  354. unsigned int flags; /* constant flags */
  355. unsigned int num; /* # of objs per slab */
  356. /* 4) cache_grow/shrink */
  357. /* order of pgs per slab (2^n) */
  358. unsigned int gfporder;
  359. /* force GFP flags, e.g. GFP_DMA */
  360. gfp_t gfpflags;
  361. size_t colour; /* cache colouring range */
  362. unsigned int colour_off; /* colour offset */
  363. struct kmem_cache *slabp_cache;
  364. unsigned int slab_size;
  365. unsigned int dflags; /* dynamic flags */
  366. /* constructor func */
  367. void (*ctor) (void *, struct kmem_cache *, unsigned long);
  368. /* de-constructor func */
  369. void (*dtor) (void *, struct kmem_cache *, unsigned long);
  370. /* 5) cache creation/removal */
  371. const char *name;
  372. struct list_head next;
  373. /* 6) statistics */
  374. #if STATS
  375. unsigned long num_active;
  376. unsigned long num_allocations;
  377. unsigned long high_mark;
  378. unsigned long grown;
  379. unsigned long reaped;
  380. unsigned long errors;
  381. unsigned long max_freeable;
  382. unsigned long node_allocs;
  383. unsigned long node_frees;
  384. atomic_t allochit;
  385. atomic_t allocmiss;
  386. atomic_t freehit;
  387. atomic_t freemiss;
  388. #endif
  389. #if DEBUG
  390. /*
  391. * If debugging is enabled, then the allocator can add additional
  392. * fields and/or padding to every object. buffer_size contains the total
  393. * object size including these internal fields, the following two
  394. * variables contain the offset to the user object and its size.
  395. */
  396. int obj_offset;
  397. int obj_size;
  398. #endif
  399. };
  400. #define CFLGS_OFF_SLAB (0x80000000UL)
  401. #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
  402. #define BATCHREFILL_LIMIT 16
  403. /*
  404. * Optimization question: fewer reaps means less probability for unnessary
  405. * cpucache drain/refill cycles.
  406. *
  407. * OTOH the cpuarrays can contain lots of objects,
  408. * which could lock up otherwise freeable slabs.
  409. */
  410. #define REAPTIMEOUT_CPUC (2*HZ)
  411. #define REAPTIMEOUT_LIST3 (4*HZ)
  412. #if STATS
  413. #define STATS_INC_ACTIVE(x) ((x)->num_active++)
  414. #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
  415. #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
  416. #define STATS_INC_GROWN(x) ((x)->grown++)
  417. #define STATS_INC_REAPED(x) ((x)->reaped++)
  418. #define STATS_SET_HIGH(x) \
  419. do { \
  420. if ((x)->num_active > (x)->high_mark) \
  421. (x)->high_mark = (x)->num_active; \
  422. } while (0)
  423. #define STATS_INC_ERR(x) ((x)->errors++)
  424. #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
  425. #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
  426. #define STATS_SET_FREEABLE(x, i) \
  427. do { \
  428. if ((x)->max_freeable < i) \
  429. (x)->max_freeable = i; \
  430. } while (0)
  431. #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
  432. #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
  433. #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
  434. #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
  435. #else
  436. #define STATS_INC_ACTIVE(x) do { } while (0)
  437. #define STATS_DEC_ACTIVE(x) do { } while (0)
  438. #define STATS_INC_ALLOCED(x) do { } while (0)
  439. #define STATS_INC_GROWN(x) do { } while (0)
  440. #define STATS_INC_REAPED(x) do { } while (0)
  441. #define STATS_SET_HIGH(x) do { } while (0)
  442. #define STATS_INC_ERR(x) do { } while (0)
  443. #define STATS_INC_NODEALLOCS(x) do { } while (0)
  444. #define STATS_INC_NODEFREES(x) do { } while (0)
  445. #define STATS_SET_FREEABLE(x, i) do { } while (0)
  446. #define STATS_INC_ALLOCHIT(x) do { } while (0)
  447. #define STATS_INC_ALLOCMISS(x) do { } while (0)
  448. #define STATS_INC_FREEHIT(x) do { } while (0)
  449. #define STATS_INC_FREEMISS(x) do { } while (0)
  450. #endif
  451. #if DEBUG
  452. /*
  453. * Magic nums for obj red zoning.
  454. * Placed in the first word before and the first word after an obj.
  455. */
  456. #define RED_INACTIVE 0x5A2CF071UL /* when obj is inactive */
  457. #define RED_ACTIVE 0x170FC2A5UL /* when obj is active */
  458. /* ...and for poisoning */
  459. #define POISON_INUSE 0x5a /* for use-uninitialised poisoning */
  460. #define POISON_FREE 0x6b /* for use-after-free poisoning */
  461. #define POISON_END 0xa5 /* end-byte of poisoning */
  462. /*
  463. * memory layout of objects:
  464. * 0 : objp
  465. * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
  466. * the end of an object is aligned with the end of the real
  467. * allocation. Catches writes behind the end of the allocation.
  468. * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
  469. * redzone word.
  470. * cachep->obj_offset: The real object.
  471. * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
  472. * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
  473. * [BYTES_PER_WORD long]
  474. */
  475. static int obj_offset(struct kmem_cache *cachep)
  476. {
  477. return cachep->obj_offset;
  478. }
  479. static int obj_size(struct kmem_cache *cachep)
  480. {
  481. return cachep->obj_size;
  482. }
  483. static unsigned long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
  484. {
  485. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  486. return (unsigned long*) (objp+obj_offset(cachep)-BYTES_PER_WORD);
  487. }
  488. static unsigned long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
  489. {
  490. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  491. if (cachep->flags & SLAB_STORE_USER)
  492. return (unsigned long *)(objp + cachep->buffer_size -
  493. 2 * BYTES_PER_WORD);
  494. return (unsigned long *)(objp + cachep->buffer_size - BYTES_PER_WORD);
  495. }
  496. static void **dbg_userword(struct kmem_cache *cachep, void *objp)
  497. {
  498. BUG_ON(!(cachep->flags & SLAB_STORE_USER));
  499. return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
  500. }
  501. #else
  502. #define obj_offset(x) 0
  503. #define obj_size(cachep) (cachep->buffer_size)
  504. #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
  505. #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
  506. #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
  507. #endif
  508. /*
  509. * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
  510. * order.
  511. */
  512. #if defined(CONFIG_LARGE_ALLOCS)
  513. #define MAX_OBJ_ORDER 13 /* up to 32Mb */
  514. #define MAX_GFP_ORDER 13 /* up to 32Mb */
  515. #elif defined(CONFIG_MMU)
  516. #define MAX_OBJ_ORDER 5 /* 32 pages */
  517. #define MAX_GFP_ORDER 5 /* 32 pages */
  518. #else
  519. #define MAX_OBJ_ORDER 8 /* up to 1Mb */
  520. #define MAX_GFP_ORDER 8 /* up to 1Mb */
  521. #endif
  522. /*
  523. * Do not go above this order unless 0 objects fit into the slab.
  524. */
  525. #define BREAK_GFP_ORDER_HI 1
  526. #define BREAK_GFP_ORDER_LO 0
  527. static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
  528. /*
  529. * Functions for storing/retrieving the cachep and or slab from the page
  530. * allocator. These are used to find the slab an obj belongs to. With kfree(),
  531. * these are used to find the cache which an obj belongs to.
  532. */
  533. static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
  534. {
  535. page->lru.next = (struct list_head *)cache;
  536. }
  537. static inline struct kmem_cache *page_get_cache(struct page *page)
  538. {
  539. if (unlikely(PageCompound(page)))
  540. page = (struct page *)page_private(page);
  541. return (struct kmem_cache *)page->lru.next;
  542. }
  543. static inline void page_set_slab(struct page *page, struct slab *slab)
  544. {
  545. page->lru.prev = (struct list_head *)slab;
  546. }
  547. static inline struct slab *page_get_slab(struct page *page)
  548. {
  549. if (unlikely(PageCompound(page)))
  550. page = (struct page *)page_private(page);
  551. return (struct slab *)page->lru.prev;
  552. }
  553. static inline struct kmem_cache *virt_to_cache(const void *obj)
  554. {
  555. struct page *page = virt_to_page(obj);
  556. return page_get_cache(page);
  557. }
  558. static inline struct slab *virt_to_slab(const void *obj)
  559. {
  560. struct page *page = virt_to_page(obj);
  561. return page_get_slab(page);
  562. }
  563. static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
  564. unsigned int idx)
  565. {
  566. return slab->s_mem + cache->buffer_size * idx;
  567. }
  568. static inline unsigned int obj_to_index(struct kmem_cache *cache,
  569. struct slab *slab, void *obj)
  570. {
  571. return (unsigned)(obj - slab->s_mem) / cache->buffer_size;
  572. }
  573. /*
  574. * These are the default caches for kmalloc. Custom caches can have other sizes.
  575. */
  576. struct cache_sizes malloc_sizes[] = {
  577. #define CACHE(x) { .cs_size = (x) },
  578. #include <linux/kmalloc_sizes.h>
  579. CACHE(ULONG_MAX)
  580. #undef CACHE
  581. };
  582. EXPORT_SYMBOL(malloc_sizes);
  583. /* Must match cache_sizes above. Out of line to keep cache footprint low. */
  584. struct cache_names {
  585. char *name;
  586. char *name_dma;
  587. };
  588. static struct cache_names __initdata cache_names[] = {
  589. #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
  590. #include <linux/kmalloc_sizes.h>
  591. {NULL,}
  592. #undef CACHE
  593. };
  594. static struct arraycache_init initarray_cache __initdata =
  595. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  596. static struct arraycache_init initarray_generic =
  597. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  598. /* internal cache of cache description objs */
  599. static struct kmem_cache cache_cache = {
  600. .batchcount = 1,
  601. .limit = BOOT_CPUCACHE_ENTRIES,
  602. .shared = 1,
  603. .buffer_size = sizeof(struct kmem_cache),
  604. .name = "kmem_cache",
  605. #if DEBUG
  606. .obj_size = sizeof(struct kmem_cache),
  607. #endif
  608. };
  609. /* Guard access to the cache-chain. */
  610. static DEFINE_MUTEX(cache_chain_mutex);
  611. static struct list_head cache_chain;
  612. /*
  613. * vm_enough_memory() looks at this to determine how many slab-allocated pages
  614. * are possibly freeable under pressure
  615. *
  616. * SLAB_RECLAIM_ACCOUNT turns this on per-slab
  617. */
  618. atomic_t slab_reclaim_pages;
  619. /*
  620. * chicken and egg problem: delay the per-cpu array allocation
  621. * until the general caches are up.
  622. */
  623. static enum {
  624. NONE,
  625. PARTIAL_AC,
  626. PARTIAL_L3,
  627. FULL
  628. } g_cpucache_up;
  629. static DEFINE_PER_CPU(struct work_struct, reap_work);
  630. static void free_block(struct kmem_cache *cachep, void **objpp, int len,
  631. int node);
  632. static void enable_cpucache(struct kmem_cache *cachep);
  633. static void cache_reap(void *unused);
  634. static int __node_shrink(struct kmem_cache *cachep, int node);
  635. static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
  636. {
  637. return cachep->array[smp_processor_id()];
  638. }
  639. static inline struct kmem_cache *__find_general_cachep(size_t size,
  640. gfp_t gfpflags)
  641. {
  642. struct cache_sizes *csizep = malloc_sizes;
  643. #if DEBUG
  644. /* This happens if someone tries to call
  645. * kmem_cache_create(), or __kmalloc(), before
  646. * the generic caches are initialized.
  647. */
  648. BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
  649. #endif
  650. while (size > csizep->cs_size)
  651. csizep++;
  652. /*
  653. * Really subtle: The last entry with cs->cs_size==ULONG_MAX
  654. * has cs_{dma,}cachep==NULL. Thus no special case
  655. * for large kmalloc calls required.
  656. */
  657. if (unlikely(gfpflags & GFP_DMA))
  658. return csizep->cs_dmacachep;
  659. return csizep->cs_cachep;
  660. }
  661. struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
  662. {
  663. return __find_general_cachep(size, gfpflags);
  664. }
  665. EXPORT_SYMBOL(kmem_find_general_cachep);
  666. static size_t slab_mgmt_size(size_t nr_objs, size_t align)
  667. {
  668. return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
  669. }
  670. /*
  671. * Calculate the number of objects and left-over bytes for a given buffer size.
  672. */
  673. static void cache_estimate(unsigned long gfporder, size_t buffer_size,
  674. size_t align, int flags, size_t *left_over,
  675. unsigned int *num)
  676. {
  677. int nr_objs;
  678. size_t mgmt_size;
  679. size_t slab_size = PAGE_SIZE << gfporder;
  680. /*
  681. * The slab management structure can be either off the slab or
  682. * on it. For the latter case, the memory allocated for a
  683. * slab is used for:
  684. *
  685. * - The struct slab
  686. * - One kmem_bufctl_t for each object
  687. * - Padding to respect alignment of @align
  688. * - @buffer_size bytes for each object
  689. *
  690. * If the slab management structure is off the slab, then the
  691. * alignment will already be calculated into the size. Because
  692. * the slabs are all pages aligned, the objects will be at the
  693. * correct alignment when allocated.
  694. */
  695. if (flags & CFLGS_OFF_SLAB) {
  696. mgmt_size = 0;
  697. nr_objs = slab_size / buffer_size;
  698. if (nr_objs > SLAB_LIMIT)
  699. nr_objs = SLAB_LIMIT;
  700. } else {
  701. /*
  702. * Ignore padding for the initial guess. The padding
  703. * is at most @align-1 bytes, and @buffer_size is at
  704. * least @align. In the worst case, this result will
  705. * be one greater than the number of objects that fit
  706. * into the memory allocation when taking the padding
  707. * into account.
  708. */
  709. nr_objs = (slab_size - sizeof(struct slab)) /
  710. (buffer_size + sizeof(kmem_bufctl_t));
  711. /*
  712. * This calculated number will be either the right
  713. * amount, or one greater than what we want.
  714. */
  715. if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
  716. > slab_size)
  717. nr_objs--;
  718. if (nr_objs > SLAB_LIMIT)
  719. nr_objs = SLAB_LIMIT;
  720. mgmt_size = slab_mgmt_size(nr_objs, align);
  721. }
  722. *num = nr_objs;
  723. *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
  724. }
  725. #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
  726. static void __slab_error(const char *function, struct kmem_cache *cachep,
  727. char *msg)
  728. {
  729. printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
  730. function, cachep->name, msg);
  731. dump_stack();
  732. }
  733. #ifdef CONFIG_NUMA
  734. /*
  735. * Special reaping functions for NUMA systems called from cache_reap().
  736. * These take care of doing round robin flushing of alien caches (containing
  737. * objects freed on different nodes from which they were allocated) and the
  738. * flushing of remote pcps by calling drain_node_pages.
  739. */
  740. static DEFINE_PER_CPU(unsigned long, reap_node);
  741. static void init_reap_node(int cpu)
  742. {
  743. int node;
  744. node = next_node(cpu_to_node(cpu), node_online_map);
  745. if (node == MAX_NUMNODES)
  746. node = 0;
  747. __get_cpu_var(reap_node) = node;
  748. }
  749. static void next_reap_node(void)
  750. {
  751. int node = __get_cpu_var(reap_node);
  752. /*
  753. * Also drain per cpu pages on remote zones
  754. */
  755. if (node != numa_node_id())
  756. drain_node_pages(node);
  757. node = next_node(node, node_online_map);
  758. if (unlikely(node >= MAX_NUMNODES))
  759. node = first_node(node_online_map);
  760. __get_cpu_var(reap_node) = node;
  761. }
  762. #else
  763. #define init_reap_node(cpu) do { } while (0)
  764. #define next_reap_node(void) do { } while (0)
  765. #endif
  766. /*
  767. * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
  768. * via the workqueue/eventd.
  769. * Add the CPU number into the expiration time to minimize the possibility of
  770. * the CPUs getting into lockstep and contending for the global cache chain
  771. * lock.
  772. */
  773. static void __devinit start_cpu_timer(int cpu)
  774. {
  775. struct work_struct *reap_work = &per_cpu(reap_work, cpu);
  776. /*
  777. * When this gets called from do_initcalls via cpucache_init(),
  778. * init_workqueues() has already run, so keventd will be setup
  779. * at that time.
  780. */
  781. if (keventd_up() && reap_work->func == NULL) {
  782. init_reap_node(cpu);
  783. INIT_WORK(reap_work, cache_reap, NULL);
  784. schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
  785. }
  786. }
  787. static struct array_cache *alloc_arraycache(int node, int entries,
  788. int batchcount)
  789. {
  790. int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
  791. struct array_cache *nc = NULL;
  792. nc = kmalloc_node(memsize, GFP_KERNEL, node);
  793. if (nc) {
  794. nc->avail = 0;
  795. nc->limit = entries;
  796. nc->batchcount = batchcount;
  797. nc->touched = 0;
  798. spin_lock_init(&nc->lock);
  799. }
  800. return nc;
  801. }
  802. #ifdef CONFIG_NUMA
  803. static void *__cache_alloc_node(struct kmem_cache *, gfp_t, int);
  804. static struct array_cache **alloc_alien_cache(int node, int limit)
  805. {
  806. struct array_cache **ac_ptr;
  807. int memsize = sizeof(void *) * MAX_NUMNODES;
  808. int i;
  809. if (limit > 1)
  810. limit = 12;
  811. ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
  812. if (ac_ptr) {
  813. for_each_node(i) {
  814. if (i == node || !node_online(i)) {
  815. ac_ptr[i] = NULL;
  816. continue;
  817. }
  818. ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
  819. if (!ac_ptr[i]) {
  820. for (i--; i <= 0; i--)
  821. kfree(ac_ptr[i]);
  822. kfree(ac_ptr);
  823. return NULL;
  824. }
  825. }
  826. }
  827. return ac_ptr;
  828. }
  829. static void free_alien_cache(struct array_cache **ac_ptr)
  830. {
  831. int i;
  832. if (!ac_ptr)
  833. return;
  834. for_each_node(i)
  835. kfree(ac_ptr[i]);
  836. kfree(ac_ptr);
  837. }
  838. static void __drain_alien_cache(struct kmem_cache *cachep,
  839. struct array_cache *ac, int node)
  840. {
  841. struct kmem_list3 *rl3 = cachep->nodelists[node];
  842. if (ac->avail) {
  843. spin_lock(&rl3->list_lock);
  844. free_block(cachep, ac->entry, ac->avail, node);
  845. ac->avail = 0;
  846. spin_unlock(&rl3->list_lock);
  847. }
  848. }
  849. /*
  850. * Called from cache_reap() to regularly drain alien caches round robin.
  851. */
  852. static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
  853. {
  854. int node = __get_cpu_var(reap_node);
  855. if (l3->alien) {
  856. struct array_cache *ac = l3->alien[node];
  857. if (ac && ac->avail) {
  858. spin_lock_irq(&ac->lock);
  859. __drain_alien_cache(cachep, ac, node);
  860. spin_unlock_irq(&ac->lock);
  861. }
  862. }
  863. }
  864. static void drain_alien_cache(struct kmem_cache *cachep,
  865. struct array_cache **alien)
  866. {
  867. int i = 0;
  868. struct array_cache *ac;
  869. unsigned long flags;
  870. for_each_online_node(i) {
  871. ac = alien[i];
  872. if (ac) {
  873. spin_lock_irqsave(&ac->lock, flags);
  874. __drain_alien_cache(cachep, ac, i);
  875. spin_unlock_irqrestore(&ac->lock, flags);
  876. }
  877. }
  878. }
  879. #else
  880. #define drain_alien_cache(cachep, alien) do { } while (0)
  881. #define reap_alien(cachep, l3) do { } while (0)
  882. static inline struct array_cache **alloc_alien_cache(int node, int limit)
  883. {
  884. return (struct array_cache **) 0x01020304ul;
  885. }
  886. static inline void free_alien_cache(struct array_cache **ac_ptr)
  887. {
  888. }
  889. #endif
  890. static int __devinit cpuup_callback(struct notifier_block *nfb,
  891. unsigned long action, void *hcpu)
  892. {
  893. long cpu = (long)hcpu;
  894. struct kmem_cache *cachep;
  895. struct kmem_list3 *l3 = NULL;
  896. int node = cpu_to_node(cpu);
  897. int memsize = sizeof(struct kmem_list3);
  898. switch (action) {
  899. case CPU_UP_PREPARE:
  900. mutex_lock(&cache_chain_mutex);
  901. /*
  902. * We need to do this right in the beginning since
  903. * alloc_arraycache's are going to use this list.
  904. * kmalloc_node allows us to add the slab to the right
  905. * kmem_list3 and not this cpu's kmem_list3
  906. */
  907. list_for_each_entry(cachep, &cache_chain, next) {
  908. /*
  909. * Set up the size64 kmemlist for cpu before we can
  910. * begin anything. Make sure some other cpu on this
  911. * node has not already allocated this
  912. */
  913. if (!cachep->nodelists[node]) {
  914. l3 = kmalloc_node(memsize, GFP_KERNEL, node);
  915. if (!l3)
  916. goto bad;
  917. kmem_list3_init(l3);
  918. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  919. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  920. /*
  921. * The l3s don't come and go as CPUs come and
  922. * go. cache_chain_mutex is sufficient
  923. * protection here.
  924. */
  925. cachep->nodelists[node] = l3;
  926. }
  927. spin_lock_irq(&cachep->nodelists[node]->list_lock);
  928. cachep->nodelists[node]->free_limit =
  929. (1 + nr_cpus_node(node)) *
  930. cachep->batchcount + cachep->num;
  931. spin_unlock_irq(&cachep->nodelists[node]->list_lock);
  932. }
  933. /*
  934. * Now we can go ahead with allocating the shared arrays and
  935. * array caches
  936. */
  937. list_for_each_entry(cachep, &cache_chain, next) {
  938. struct array_cache *nc;
  939. struct array_cache *shared;
  940. struct array_cache **alien;
  941. nc = alloc_arraycache(node, cachep->limit,
  942. cachep->batchcount);
  943. if (!nc)
  944. goto bad;
  945. shared = alloc_arraycache(node,
  946. cachep->shared * cachep->batchcount,
  947. 0xbaadf00d);
  948. if (!shared)
  949. goto bad;
  950. alien = alloc_alien_cache(node, cachep->limit);
  951. if (!alien)
  952. goto bad;
  953. cachep->array[cpu] = nc;
  954. l3 = cachep->nodelists[node];
  955. BUG_ON(!l3);
  956. spin_lock_irq(&l3->list_lock);
  957. if (!l3->shared) {
  958. /*
  959. * We are serialised from CPU_DEAD or
  960. * CPU_UP_CANCELLED by the cpucontrol lock
  961. */
  962. l3->shared = shared;
  963. shared = NULL;
  964. }
  965. #ifdef CONFIG_NUMA
  966. if (!l3->alien) {
  967. l3->alien = alien;
  968. alien = NULL;
  969. }
  970. #endif
  971. spin_unlock_irq(&l3->list_lock);
  972. kfree(shared);
  973. free_alien_cache(alien);
  974. }
  975. mutex_unlock(&cache_chain_mutex);
  976. break;
  977. case CPU_ONLINE:
  978. start_cpu_timer(cpu);
  979. break;
  980. #ifdef CONFIG_HOTPLUG_CPU
  981. case CPU_DEAD:
  982. /*
  983. * Even if all the cpus of a node are down, we don't free the
  984. * kmem_list3 of any cache. This to avoid a race between
  985. * cpu_down, and a kmalloc allocation from another cpu for
  986. * memory from the node of the cpu going down. The list3
  987. * structure is usually allocated from kmem_cache_create() and
  988. * gets destroyed at kmem_cache_destroy().
  989. */
  990. /* fall thru */
  991. case CPU_UP_CANCELED:
  992. mutex_lock(&cache_chain_mutex);
  993. list_for_each_entry(cachep, &cache_chain, next) {
  994. struct array_cache *nc;
  995. struct array_cache *shared;
  996. struct array_cache **alien;
  997. cpumask_t mask;
  998. mask = node_to_cpumask(node);
  999. /* cpu is dead; no one can alloc from it. */
  1000. nc = cachep->array[cpu];
  1001. cachep->array[cpu] = NULL;
  1002. l3 = cachep->nodelists[node];
  1003. if (!l3)
  1004. goto free_array_cache;
  1005. spin_lock_irq(&l3->list_lock);
  1006. /* Free limit for this kmem_list3 */
  1007. l3->free_limit -= cachep->batchcount;
  1008. if (nc)
  1009. free_block(cachep, nc->entry, nc->avail, node);
  1010. if (!cpus_empty(mask)) {
  1011. spin_unlock_irq(&l3->list_lock);
  1012. goto free_array_cache;
  1013. }
  1014. shared = l3->shared;
  1015. if (shared) {
  1016. free_block(cachep, l3->shared->entry,
  1017. l3->shared->avail, node);
  1018. l3->shared = NULL;
  1019. }
  1020. alien = l3->alien;
  1021. l3->alien = NULL;
  1022. spin_unlock_irq(&l3->list_lock);
  1023. kfree(shared);
  1024. if (alien) {
  1025. drain_alien_cache(cachep, alien);
  1026. free_alien_cache(alien);
  1027. }
  1028. free_array_cache:
  1029. kfree(nc);
  1030. }
  1031. /*
  1032. * In the previous loop, all the objects were freed to
  1033. * the respective cache's slabs, now we can go ahead and
  1034. * shrink each nodelist to its limit.
  1035. */
  1036. list_for_each_entry(cachep, &cache_chain, next) {
  1037. l3 = cachep->nodelists[node];
  1038. if (!l3)
  1039. continue;
  1040. spin_lock_irq(&l3->list_lock);
  1041. /* free slabs belonging to this node */
  1042. __node_shrink(cachep, node);
  1043. spin_unlock_irq(&l3->list_lock);
  1044. }
  1045. mutex_unlock(&cache_chain_mutex);
  1046. break;
  1047. #endif
  1048. }
  1049. return NOTIFY_OK;
  1050. bad:
  1051. mutex_unlock(&cache_chain_mutex);
  1052. return NOTIFY_BAD;
  1053. }
  1054. static struct notifier_block cpucache_notifier = { &cpuup_callback, NULL, 0 };
  1055. /*
  1056. * swap the static kmem_list3 with kmalloced memory
  1057. */
  1058. static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
  1059. int nodeid)
  1060. {
  1061. struct kmem_list3 *ptr;
  1062. BUG_ON(cachep->nodelists[nodeid] != list);
  1063. ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
  1064. BUG_ON(!ptr);
  1065. local_irq_disable();
  1066. memcpy(ptr, list, sizeof(struct kmem_list3));
  1067. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  1068. cachep->nodelists[nodeid] = ptr;
  1069. local_irq_enable();
  1070. }
  1071. /*
  1072. * Initialisation. Called after the page allocator have been initialised and
  1073. * before smp_init().
  1074. */
  1075. void __init kmem_cache_init(void)
  1076. {
  1077. size_t left_over;
  1078. struct cache_sizes *sizes;
  1079. struct cache_names *names;
  1080. int i;
  1081. int order;
  1082. for (i = 0; i < NUM_INIT_LISTS; i++) {
  1083. kmem_list3_init(&initkmem_list3[i]);
  1084. if (i < MAX_NUMNODES)
  1085. cache_cache.nodelists[i] = NULL;
  1086. }
  1087. /*
  1088. * Fragmentation resistance on low memory - only use bigger
  1089. * page orders on machines with more than 32MB of memory.
  1090. */
  1091. if (num_physpages > (32 << 20) >> PAGE_SHIFT)
  1092. slab_break_gfp_order = BREAK_GFP_ORDER_HI;
  1093. /* Bootstrap is tricky, because several objects are allocated
  1094. * from caches that do not exist yet:
  1095. * 1) initialize the cache_cache cache: it contains the struct
  1096. * kmem_cache structures of all caches, except cache_cache itself:
  1097. * cache_cache is statically allocated.
  1098. * Initially an __init data area is used for the head array and the
  1099. * kmem_list3 structures, it's replaced with a kmalloc allocated
  1100. * array at the end of the bootstrap.
  1101. * 2) Create the first kmalloc cache.
  1102. * The struct kmem_cache for the new cache is allocated normally.
  1103. * An __init data area is used for the head array.
  1104. * 3) Create the remaining kmalloc caches, with minimally sized
  1105. * head arrays.
  1106. * 4) Replace the __init data head arrays for cache_cache and the first
  1107. * kmalloc cache with kmalloc allocated arrays.
  1108. * 5) Replace the __init data for kmem_list3 for cache_cache and
  1109. * the other cache's with kmalloc allocated memory.
  1110. * 6) Resize the head arrays of the kmalloc caches to their final sizes.
  1111. */
  1112. /* 1) create the cache_cache */
  1113. INIT_LIST_HEAD(&cache_chain);
  1114. list_add(&cache_cache.next, &cache_chain);
  1115. cache_cache.colour_off = cache_line_size();
  1116. cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
  1117. cache_cache.nodelists[numa_node_id()] = &initkmem_list3[CACHE_CACHE];
  1118. cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
  1119. cache_line_size());
  1120. for (order = 0; order < MAX_ORDER; order++) {
  1121. cache_estimate(order, cache_cache.buffer_size,
  1122. cache_line_size(), 0, &left_over, &cache_cache.num);
  1123. if (cache_cache.num)
  1124. break;
  1125. }
  1126. if (!cache_cache.num)
  1127. BUG();
  1128. cache_cache.gfporder = order;
  1129. cache_cache.colour = left_over / cache_cache.colour_off;
  1130. cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
  1131. sizeof(struct slab), cache_line_size());
  1132. /* 2+3) create the kmalloc caches */
  1133. sizes = malloc_sizes;
  1134. names = cache_names;
  1135. /*
  1136. * Initialize the caches that provide memory for the array cache and the
  1137. * kmem_list3 structures first. Without this, further allocations will
  1138. * bug.
  1139. */
  1140. sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
  1141. sizes[INDEX_AC].cs_size,
  1142. ARCH_KMALLOC_MINALIGN,
  1143. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1144. NULL, NULL);
  1145. if (INDEX_AC != INDEX_L3) {
  1146. sizes[INDEX_L3].cs_cachep =
  1147. kmem_cache_create(names[INDEX_L3].name,
  1148. sizes[INDEX_L3].cs_size,
  1149. ARCH_KMALLOC_MINALIGN,
  1150. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1151. NULL, NULL);
  1152. }
  1153. while (sizes->cs_size != ULONG_MAX) {
  1154. /*
  1155. * For performance, all the general caches are L1 aligned.
  1156. * This should be particularly beneficial on SMP boxes, as it
  1157. * eliminates "false sharing".
  1158. * Note for systems short on memory removing the alignment will
  1159. * allow tighter packing of the smaller caches.
  1160. */
  1161. if (!sizes->cs_cachep) {
  1162. sizes->cs_cachep = kmem_cache_create(names->name,
  1163. sizes->cs_size,
  1164. ARCH_KMALLOC_MINALIGN,
  1165. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1166. NULL, NULL);
  1167. }
  1168. /* Inc off-slab bufctl limit until the ceiling is hit. */
  1169. if (!(OFF_SLAB(sizes->cs_cachep))) {
  1170. offslab_limit = sizes->cs_size - sizeof(struct slab);
  1171. offslab_limit /= sizeof(kmem_bufctl_t);
  1172. }
  1173. sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
  1174. sizes->cs_size,
  1175. ARCH_KMALLOC_MINALIGN,
  1176. ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
  1177. SLAB_PANIC,
  1178. NULL, NULL);
  1179. sizes++;
  1180. names++;
  1181. }
  1182. /* 4) Replace the bootstrap head arrays */
  1183. {
  1184. void *ptr;
  1185. ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  1186. local_irq_disable();
  1187. BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
  1188. memcpy(ptr, cpu_cache_get(&cache_cache),
  1189. sizeof(struct arraycache_init));
  1190. cache_cache.array[smp_processor_id()] = ptr;
  1191. local_irq_enable();
  1192. ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  1193. local_irq_disable();
  1194. BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
  1195. != &initarray_generic.cache);
  1196. memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
  1197. sizeof(struct arraycache_init));
  1198. malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
  1199. ptr;
  1200. local_irq_enable();
  1201. }
  1202. /* 5) Replace the bootstrap kmem_list3's */
  1203. {
  1204. int node;
  1205. /* Replace the static kmem_list3 structures for the boot cpu */
  1206. init_list(&cache_cache, &initkmem_list3[CACHE_CACHE],
  1207. numa_node_id());
  1208. for_each_online_node(node) {
  1209. init_list(malloc_sizes[INDEX_AC].cs_cachep,
  1210. &initkmem_list3[SIZE_AC + node], node);
  1211. if (INDEX_AC != INDEX_L3) {
  1212. init_list(malloc_sizes[INDEX_L3].cs_cachep,
  1213. &initkmem_list3[SIZE_L3 + node],
  1214. node);
  1215. }
  1216. }
  1217. }
  1218. /* 6) resize the head arrays to their final sizes */
  1219. {
  1220. struct kmem_cache *cachep;
  1221. mutex_lock(&cache_chain_mutex);
  1222. list_for_each_entry(cachep, &cache_chain, next)
  1223. enable_cpucache(cachep);
  1224. mutex_unlock(&cache_chain_mutex);
  1225. }
  1226. /* Done! */
  1227. g_cpucache_up = FULL;
  1228. /*
  1229. * Register a cpu startup notifier callback that initializes
  1230. * cpu_cache_get for all new cpus
  1231. */
  1232. register_cpu_notifier(&cpucache_notifier);
  1233. /*
  1234. * The reap timers are started later, with a module init call: That part
  1235. * of the kernel is not yet operational.
  1236. */
  1237. }
  1238. static int __init cpucache_init(void)
  1239. {
  1240. int cpu;
  1241. /*
  1242. * Register the timers that return unneeded pages to the page allocator
  1243. */
  1244. for_each_online_cpu(cpu)
  1245. start_cpu_timer(cpu);
  1246. return 0;
  1247. }
  1248. __initcall(cpucache_init);
  1249. /*
  1250. * Interface to system's page allocator. No need to hold the cache-lock.
  1251. *
  1252. * If we requested dmaable memory, we will get it. Even if we
  1253. * did not request dmaable memory, we might get it, but that
  1254. * would be relatively rare and ignorable.
  1255. */
  1256. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  1257. {
  1258. struct page *page;
  1259. void *addr;
  1260. int i;
  1261. flags |= cachep->gfpflags;
  1262. page = alloc_pages_node(nodeid, flags, cachep->gfporder);
  1263. if (!page)
  1264. return NULL;
  1265. addr = page_address(page);
  1266. i = (1 << cachep->gfporder);
  1267. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1268. atomic_add(i, &slab_reclaim_pages);
  1269. add_page_state(nr_slab, i);
  1270. while (i--) {
  1271. __SetPageSlab(page);
  1272. page++;
  1273. }
  1274. return addr;
  1275. }
  1276. /*
  1277. * Interface to system's page release.
  1278. */
  1279. static void kmem_freepages(struct kmem_cache *cachep, void *addr)
  1280. {
  1281. unsigned long i = (1 << cachep->gfporder);
  1282. struct page *page = virt_to_page(addr);
  1283. const unsigned long nr_freed = i;
  1284. while (i--) {
  1285. BUG_ON(!PageSlab(page));
  1286. __ClearPageSlab(page);
  1287. page++;
  1288. }
  1289. sub_page_state(nr_slab, nr_freed);
  1290. if (current->reclaim_state)
  1291. current->reclaim_state->reclaimed_slab += nr_freed;
  1292. free_pages((unsigned long)addr, cachep->gfporder);
  1293. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1294. atomic_sub(1 << cachep->gfporder, &slab_reclaim_pages);
  1295. }
  1296. static void kmem_rcu_free(struct rcu_head *head)
  1297. {
  1298. struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
  1299. struct kmem_cache *cachep = slab_rcu->cachep;
  1300. kmem_freepages(cachep, slab_rcu->addr);
  1301. if (OFF_SLAB(cachep))
  1302. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1303. }
  1304. #if DEBUG
  1305. #ifdef CONFIG_DEBUG_PAGEALLOC
  1306. static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
  1307. unsigned long caller)
  1308. {
  1309. int size = obj_size(cachep);
  1310. addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
  1311. if (size < 5 * sizeof(unsigned long))
  1312. return;
  1313. *addr++ = 0x12345678;
  1314. *addr++ = caller;
  1315. *addr++ = smp_processor_id();
  1316. size -= 3 * sizeof(unsigned long);
  1317. {
  1318. unsigned long *sptr = &caller;
  1319. unsigned long svalue;
  1320. while (!kstack_end(sptr)) {
  1321. svalue = *sptr++;
  1322. if (kernel_text_address(svalue)) {
  1323. *addr++ = svalue;
  1324. size -= sizeof(unsigned long);
  1325. if (size <= sizeof(unsigned long))
  1326. break;
  1327. }
  1328. }
  1329. }
  1330. *addr++ = 0x87654321;
  1331. }
  1332. #endif
  1333. static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
  1334. {
  1335. int size = obj_size(cachep);
  1336. addr = &((char *)addr)[obj_offset(cachep)];
  1337. memset(addr, val, size);
  1338. *(unsigned char *)(addr + size - 1) = POISON_END;
  1339. }
  1340. static void dump_line(char *data, int offset, int limit)
  1341. {
  1342. int i;
  1343. printk(KERN_ERR "%03x:", offset);
  1344. for (i = 0; i < limit; i++)
  1345. printk(" %02x", (unsigned char)data[offset + i]);
  1346. printk("\n");
  1347. }
  1348. #endif
  1349. #if DEBUG
  1350. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1351. {
  1352. int i, size;
  1353. char *realobj;
  1354. if (cachep->flags & SLAB_RED_ZONE) {
  1355. printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
  1356. *dbg_redzone1(cachep, objp),
  1357. *dbg_redzone2(cachep, objp));
  1358. }
  1359. if (cachep->flags & SLAB_STORE_USER) {
  1360. printk(KERN_ERR "Last user: [<%p>]",
  1361. *dbg_userword(cachep, objp));
  1362. print_symbol("(%s)",
  1363. (unsigned long)*dbg_userword(cachep, objp));
  1364. printk("\n");
  1365. }
  1366. realobj = (char *)objp + obj_offset(cachep);
  1367. size = obj_size(cachep);
  1368. for (i = 0; i < size && lines; i += 16, lines--) {
  1369. int limit;
  1370. limit = 16;
  1371. if (i + limit > size)
  1372. limit = size - i;
  1373. dump_line(realobj, i, limit);
  1374. }
  1375. }
  1376. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1377. {
  1378. char *realobj;
  1379. int size, i;
  1380. int lines = 0;
  1381. realobj = (char *)objp + obj_offset(cachep);
  1382. size = obj_size(cachep);
  1383. for (i = 0; i < size; i++) {
  1384. char exp = POISON_FREE;
  1385. if (i == size - 1)
  1386. exp = POISON_END;
  1387. if (realobj[i] != exp) {
  1388. int limit;
  1389. /* Mismatch ! */
  1390. /* Print header */
  1391. if (lines == 0) {
  1392. printk(KERN_ERR
  1393. "Slab corruption: start=%p, len=%d\n",
  1394. realobj, size);
  1395. print_objinfo(cachep, objp, 0);
  1396. }
  1397. /* Hexdump the affected line */
  1398. i = (i / 16) * 16;
  1399. limit = 16;
  1400. if (i + limit > size)
  1401. limit = size - i;
  1402. dump_line(realobj, i, limit);
  1403. i += 16;
  1404. lines++;
  1405. /* Limit to 5 lines */
  1406. if (lines > 5)
  1407. break;
  1408. }
  1409. }
  1410. if (lines != 0) {
  1411. /* Print some data about the neighboring objects, if they
  1412. * exist:
  1413. */
  1414. struct slab *slabp = virt_to_slab(objp);
  1415. unsigned int objnr;
  1416. objnr = obj_to_index(cachep, slabp, objp);
  1417. if (objnr) {
  1418. objp = index_to_obj(cachep, slabp, objnr - 1);
  1419. realobj = (char *)objp + obj_offset(cachep);
  1420. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1421. realobj, size);
  1422. print_objinfo(cachep, objp, 2);
  1423. }
  1424. if (objnr + 1 < cachep->num) {
  1425. objp = index_to_obj(cachep, slabp, objnr + 1);
  1426. realobj = (char *)objp + obj_offset(cachep);
  1427. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1428. realobj, size);
  1429. print_objinfo(cachep, objp, 2);
  1430. }
  1431. }
  1432. }
  1433. #endif
  1434. #if DEBUG
  1435. /**
  1436. * slab_destroy_objs - destroy a slab and its objects
  1437. * @cachep: cache pointer being destroyed
  1438. * @slabp: slab pointer being destroyed
  1439. *
  1440. * Call the registered destructor for each object in a slab that is being
  1441. * destroyed.
  1442. */
  1443. static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
  1444. {
  1445. int i;
  1446. for (i = 0; i < cachep->num; i++) {
  1447. void *objp = index_to_obj(cachep, slabp, i);
  1448. if (cachep->flags & SLAB_POISON) {
  1449. #ifdef CONFIG_DEBUG_PAGEALLOC
  1450. if (cachep->buffer_size % PAGE_SIZE == 0 &&
  1451. OFF_SLAB(cachep))
  1452. kernel_map_pages(virt_to_page(objp),
  1453. cachep->buffer_size / PAGE_SIZE, 1);
  1454. else
  1455. check_poison_obj(cachep, objp);
  1456. #else
  1457. check_poison_obj(cachep, objp);
  1458. #endif
  1459. }
  1460. if (cachep->flags & SLAB_RED_ZONE) {
  1461. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1462. slab_error(cachep, "start of a freed object "
  1463. "was overwritten");
  1464. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1465. slab_error(cachep, "end of a freed object "
  1466. "was overwritten");
  1467. }
  1468. if (cachep->dtor && !(cachep->flags & SLAB_POISON))
  1469. (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
  1470. }
  1471. }
  1472. #else
  1473. static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
  1474. {
  1475. if (cachep->dtor) {
  1476. int i;
  1477. for (i = 0; i < cachep->num; i++) {
  1478. void *objp = index_to_obj(cachep, slabp, i);
  1479. (cachep->dtor) (objp, cachep, 0);
  1480. }
  1481. }
  1482. }
  1483. #endif
  1484. /**
  1485. * slab_destroy - destroy and release all objects in a slab
  1486. * @cachep: cache pointer being destroyed
  1487. * @slabp: slab pointer being destroyed
  1488. *
  1489. * Destroy all the objs in a slab, and release the mem back to the system.
  1490. * Before calling the slab must have been unlinked from the cache. The
  1491. * cache-lock is not held/needed.
  1492. */
  1493. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
  1494. {
  1495. void *addr = slabp->s_mem - slabp->colouroff;
  1496. slab_destroy_objs(cachep, slabp);
  1497. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1498. struct slab_rcu *slab_rcu;
  1499. slab_rcu = (struct slab_rcu *)slabp;
  1500. slab_rcu->cachep = cachep;
  1501. slab_rcu->addr = addr;
  1502. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1503. } else {
  1504. kmem_freepages(cachep, addr);
  1505. if (OFF_SLAB(cachep))
  1506. kmem_cache_free(cachep->slabp_cache, slabp);
  1507. }
  1508. }
  1509. /*
  1510. * For setting up all the kmem_list3s for cache whose buffer_size is same as
  1511. * size of kmem_list3.
  1512. */
  1513. static void set_up_list3s(struct kmem_cache *cachep, int index)
  1514. {
  1515. int node;
  1516. for_each_online_node(node) {
  1517. cachep->nodelists[node] = &initkmem_list3[index + node];
  1518. cachep->nodelists[node]->next_reap = jiffies +
  1519. REAPTIMEOUT_LIST3 +
  1520. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1521. }
  1522. }
  1523. /**
  1524. * calculate_slab_order - calculate size (page order) of slabs
  1525. * @cachep: pointer to the cache that is being created
  1526. * @size: size of objects to be created in this cache.
  1527. * @align: required alignment for the objects.
  1528. * @flags: slab allocation flags
  1529. *
  1530. * Also calculates the number of objects per slab.
  1531. *
  1532. * This could be made much more intelligent. For now, try to avoid using
  1533. * high order pages for slabs. When the gfp() functions are more friendly
  1534. * towards high-order requests, this should be changed.
  1535. */
  1536. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1537. size_t size, size_t align, unsigned long flags)
  1538. {
  1539. size_t left_over = 0;
  1540. int gfporder;
  1541. for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
  1542. unsigned int num;
  1543. size_t remainder;
  1544. cache_estimate(gfporder, size, align, flags, &remainder, &num);
  1545. if (!num)
  1546. continue;
  1547. /* More than offslab_limit objects will cause problems */
  1548. if ((flags & CFLGS_OFF_SLAB) && num > offslab_limit)
  1549. break;
  1550. /* Found something acceptable - save it away */
  1551. cachep->num = num;
  1552. cachep->gfporder = gfporder;
  1553. left_over = remainder;
  1554. /*
  1555. * A VFS-reclaimable slab tends to have most allocations
  1556. * as GFP_NOFS and we really don't want to have to be allocating
  1557. * higher-order pages when we are unable to shrink dcache.
  1558. */
  1559. if (flags & SLAB_RECLAIM_ACCOUNT)
  1560. break;
  1561. /*
  1562. * Large number of objects is good, but very large slabs are
  1563. * currently bad for the gfp()s.
  1564. */
  1565. if (gfporder >= slab_break_gfp_order)
  1566. break;
  1567. /*
  1568. * Acceptable internal fragmentation?
  1569. */
  1570. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  1571. break;
  1572. }
  1573. return left_over;
  1574. }
  1575. static void setup_cpu_cache(struct kmem_cache *cachep)
  1576. {
  1577. if (g_cpucache_up == FULL) {
  1578. enable_cpucache(cachep);
  1579. return;
  1580. }
  1581. if (g_cpucache_up == NONE) {
  1582. /*
  1583. * Note: the first kmem_cache_create must create the cache
  1584. * that's used by kmalloc(24), otherwise the creation of
  1585. * further caches will BUG().
  1586. */
  1587. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1588. /*
  1589. * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
  1590. * the first cache, then we need to set up all its list3s,
  1591. * otherwise the creation of further caches will BUG().
  1592. */
  1593. set_up_list3s(cachep, SIZE_AC);
  1594. if (INDEX_AC == INDEX_L3)
  1595. g_cpucache_up = PARTIAL_L3;
  1596. else
  1597. g_cpucache_up = PARTIAL_AC;
  1598. } else {
  1599. cachep->array[smp_processor_id()] =
  1600. kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  1601. if (g_cpucache_up == PARTIAL_AC) {
  1602. set_up_list3s(cachep, SIZE_L3);
  1603. g_cpucache_up = PARTIAL_L3;
  1604. } else {
  1605. int node;
  1606. for_each_online_node(node) {
  1607. cachep->nodelists[node] =
  1608. kmalloc_node(sizeof(struct kmem_list3),
  1609. GFP_KERNEL, node);
  1610. BUG_ON(!cachep->nodelists[node]);
  1611. kmem_list3_init(cachep->nodelists[node]);
  1612. }
  1613. }
  1614. }
  1615. cachep->nodelists[numa_node_id()]->next_reap =
  1616. jiffies + REAPTIMEOUT_LIST3 +
  1617. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1618. cpu_cache_get(cachep)->avail = 0;
  1619. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1620. cpu_cache_get(cachep)->batchcount = 1;
  1621. cpu_cache_get(cachep)->touched = 0;
  1622. cachep->batchcount = 1;
  1623. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1624. }
  1625. /**
  1626. * kmem_cache_create - Create a cache.
  1627. * @name: A string which is used in /proc/slabinfo to identify this cache.
  1628. * @size: The size of objects to be created in this cache.
  1629. * @align: The required alignment for the objects.
  1630. * @flags: SLAB flags
  1631. * @ctor: A constructor for the objects.
  1632. * @dtor: A destructor for the objects.
  1633. *
  1634. * Returns a ptr to the cache on success, NULL on failure.
  1635. * Cannot be called within a int, but can be interrupted.
  1636. * The @ctor is run when new pages are allocated by the cache
  1637. * and the @dtor is run before the pages are handed back.
  1638. *
  1639. * @name must be valid until the cache is destroyed. This implies that
  1640. * the module calling this has to destroy the cache before getting unloaded.
  1641. *
  1642. * The flags are
  1643. *
  1644. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  1645. * to catch references to uninitialised memory.
  1646. *
  1647. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  1648. * for buffer overruns.
  1649. *
  1650. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  1651. * cacheline. This can be beneficial if you're counting cycles as closely
  1652. * as davem.
  1653. */
  1654. struct kmem_cache *
  1655. kmem_cache_create (const char *name, size_t size, size_t align,
  1656. unsigned long flags,
  1657. void (*ctor)(void*, struct kmem_cache *, unsigned long),
  1658. void (*dtor)(void*, struct kmem_cache *, unsigned long))
  1659. {
  1660. size_t left_over, slab_size, ralign;
  1661. struct kmem_cache *cachep = NULL;
  1662. struct list_head *p;
  1663. /*
  1664. * Sanity checks... these are all serious usage bugs.
  1665. */
  1666. if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
  1667. (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
  1668. printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
  1669. name);
  1670. BUG();
  1671. }
  1672. /*
  1673. * Prevent CPUs from coming and going.
  1674. * lock_cpu_hotplug() nests outside cache_chain_mutex
  1675. */
  1676. lock_cpu_hotplug();
  1677. mutex_lock(&cache_chain_mutex);
  1678. list_for_each(p, &cache_chain) {
  1679. struct kmem_cache *pc = list_entry(p, struct kmem_cache, next);
  1680. mm_segment_t old_fs = get_fs();
  1681. char tmp;
  1682. int res;
  1683. /*
  1684. * This happens when the module gets unloaded and doesn't
  1685. * destroy its slab cache and no-one else reuses the vmalloc
  1686. * area of the module. Print a warning.
  1687. */
  1688. set_fs(KERNEL_DS);
  1689. res = __get_user(tmp, pc->name);
  1690. set_fs(old_fs);
  1691. if (res) {
  1692. printk("SLAB: cache with size %d has lost its name\n",
  1693. pc->buffer_size);
  1694. continue;
  1695. }
  1696. if (!strcmp(pc->name, name)) {
  1697. printk("kmem_cache_create: duplicate cache %s\n", name);
  1698. dump_stack();
  1699. goto oops;
  1700. }
  1701. }
  1702. #if DEBUG
  1703. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  1704. if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
  1705. /* No constructor, but inital state check requested */
  1706. printk(KERN_ERR "%s: No con, but init state check "
  1707. "requested - %s\n", __FUNCTION__, name);
  1708. flags &= ~SLAB_DEBUG_INITIAL;
  1709. }
  1710. #if FORCED_DEBUG
  1711. /*
  1712. * Enable redzoning and last user accounting, except for caches with
  1713. * large objects, if the increased size would increase the object size
  1714. * above the next power of two: caches with object sizes just above a
  1715. * power of two have a significant amount of internal fragmentation.
  1716. */
  1717. if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
  1718. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  1719. if (!(flags & SLAB_DESTROY_BY_RCU))
  1720. flags |= SLAB_POISON;
  1721. #endif
  1722. if (flags & SLAB_DESTROY_BY_RCU)
  1723. BUG_ON(flags & SLAB_POISON);
  1724. #endif
  1725. if (flags & SLAB_DESTROY_BY_RCU)
  1726. BUG_ON(dtor);
  1727. /*
  1728. * Always checks flags, a caller might be expecting debug support which
  1729. * isn't available.
  1730. */
  1731. if (flags & ~CREATE_MASK)
  1732. BUG();
  1733. /*
  1734. * Check that size is in terms of words. This is needed to avoid
  1735. * unaligned accesses for some archs when redzoning is used, and makes
  1736. * sure any on-slab bufctl's are also correctly aligned.
  1737. */
  1738. if (size & (BYTES_PER_WORD - 1)) {
  1739. size += (BYTES_PER_WORD - 1);
  1740. size &= ~(BYTES_PER_WORD - 1);
  1741. }
  1742. /* calculate the final buffer alignment: */
  1743. /* 1) arch recommendation: can be overridden for debug */
  1744. if (flags & SLAB_HWCACHE_ALIGN) {
  1745. /*
  1746. * Default alignment: as specified by the arch code. Except if
  1747. * an object is really small, then squeeze multiple objects into
  1748. * one cacheline.
  1749. */
  1750. ralign = cache_line_size();
  1751. while (size <= ralign / 2)
  1752. ralign /= 2;
  1753. } else {
  1754. ralign = BYTES_PER_WORD;
  1755. }
  1756. /* 2) arch mandated alignment: disables debug if necessary */
  1757. if (ralign < ARCH_SLAB_MINALIGN) {
  1758. ralign = ARCH_SLAB_MINALIGN;
  1759. if (ralign > BYTES_PER_WORD)
  1760. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1761. }
  1762. /* 3) caller mandated alignment: disables debug if necessary */
  1763. if (ralign < align) {
  1764. ralign = align;
  1765. if (ralign > BYTES_PER_WORD)
  1766. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  1767. }
  1768. /*
  1769. * 4) Store it. Note that the debug code below can reduce
  1770. * the alignment to BYTES_PER_WORD.
  1771. */
  1772. align = ralign;
  1773. /* Get cache's description obj. */
  1774. cachep = kmem_cache_alloc(&cache_cache, SLAB_KERNEL);
  1775. if (!cachep)
  1776. goto oops;
  1777. memset(cachep, 0, sizeof(struct kmem_cache));
  1778. #if DEBUG
  1779. cachep->obj_size = size;
  1780. if (flags & SLAB_RED_ZONE) {
  1781. /* redzoning only works with word aligned caches */
  1782. align = BYTES_PER_WORD;
  1783. /* add space for red zone words */
  1784. cachep->obj_offset += BYTES_PER_WORD;
  1785. size += 2 * BYTES_PER_WORD;
  1786. }
  1787. if (flags & SLAB_STORE_USER) {
  1788. /* user store requires word alignment and
  1789. * one word storage behind the end of the real
  1790. * object.
  1791. */
  1792. align = BYTES_PER_WORD;
  1793. size += BYTES_PER_WORD;
  1794. }
  1795. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  1796. if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
  1797. && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
  1798. cachep->obj_offset += PAGE_SIZE - size;
  1799. size = PAGE_SIZE;
  1800. }
  1801. #endif
  1802. #endif
  1803. /* Determine if the slab management is 'on' or 'off' slab. */
  1804. if (size >= (PAGE_SIZE >> 3))
  1805. /*
  1806. * Size is large, assume best to place the slab management obj
  1807. * off-slab (should allow better packing of objs).
  1808. */
  1809. flags |= CFLGS_OFF_SLAB;
  1810. size = ALIGN(size, align);
  1811. left_over = calculate_slab_order(cachep, size, align, flags);
  1812. if (!cachep->num) {
  1813. printk("kmem_cache_create: couldn't create cache %s.\n", name);
  1814. kmem_cache_free(&cache_cache, cachep);
  1815. cachep = NULL;
  1816. goto oops;
  1817. }
  1818. slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
  1819. + sizeof(struct slab), align);
  1820. /*
  1821. * If the slab has been placed off-slab, and we have enough space then
  1822. * move it on-slab. This is at the expense of any extra colouring.
  1823. */
  1824. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  1825. flags &= ~CFLGS_OFF_SLAB;
  1826. left_over -= slab_size;
  1827. }
  1828. if (flags & CFLGS_OFF_SLAB) {
  1829. /* really off slab. No need for manual alignment */
  1830. slab_size =
  1831. cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
  1832. }
  1833. cachep->colour_off = cache_line_size();
  1834. /* Offset must be a multiple of the alignment. */
  1835. if (cachep->colour_off < align)
  1836. cachep->colour_off = align;
  1837. cachep->colour = left_over / cachep->colour_off;
  1838. cachep->slab_size = slab_size;
  1839. cachep->flags = flags;
  1840. cachep->gfpflags = 0;
  1841. if (flags & SLAB_CACHE_DMA)
  1842. cachep->gfpflags |= GFP_DMA;
  1843. cachep->buffer_size = size;
  1844. if (flags & CFLGS_OFF_SLAB)
  1845. cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
  1846. cachep->ctor = ctor;
  1847. cachep->dtor = dtor;
  1848. cachep->name = name;
  1849. setup_cpu_cache(cachep);
  1850. /* cache setup completed, link it into the list */
  1851. list_add(&cachep->next, &cache_chain);
  1852. oops:
  1853. if (!cachep && (flags & SLAB_PANIC))
  1854. panic("kmem_cache_create(): failed to create slab `%s'\n",
  1855. name);
  1856. mutex_unlock(&cache_chain_mutex);
  1857. unlock_cpu_hotplug();
  1858. return cachep;
  1859. }
  1860. EXPORT_SYMBOL(kmem_cache_create);
  1861. #if DEBUG
  1862. static void check_irq_off(void)
  1863. {
  1864. BUG_ON(!irqs_disabled());
  1865. }
  1866. static void check_irq_on(void)
  1867. {
  1868. BUG_ON(irqs_disabled());
  1869. }
  1870. static void check_spinlock_acquired(struct kmem_cache *cachep)
  1871. {
  1872. #ifdef CONFIG_SMP
  1873. check_irq_off();
  1874. assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
  1875. #endif
  1876. }
  1877. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  1878. {
  1879. #ifdef CONFIG_SMP
  1880. check_irq_off();
  1881. assert_spin_locked(&cachep->nodelists[node]->list_lock);
  1882. #endif
  1883. }
  1884. #else
  1885. #define check_irq_off() do { } while(0)
  1886. #define check_irq_on() do { } while(0)
  1887. #define check_spinlock_acquired(x) do { } while(0)
  1888. #define check_spinlock_acquired_node(x, y) do { } while(0)
  1889. #endif
  1890. static void drain_array_locked(struct kmem_cache *cachep,
  1891. struct array_cache *ac, int force, int node);
  1892. static void do_drain(void *arg)
  1893. {
  1894. struct kmem_cache *cachep = arg;
  1895. struct array_cache *ac;
  1896. int node = numa_node_id();
  1897. check_irq_off();
  1898. ac = cpu_cache_get(cachep);
  1899. spin_lock(&cachep->nodelists[node]->list_lock);
  1900. free_block(cachep, ac->entry, ac->avail, node);
  1901. spin_unlock(&cachep->nodelists[node]->list_lock);
  1902. ac->avail = 0;
  1903. }
  1904. static void drain_cpu_caches(struct kmem_cache *cachep)
  1905. {
  1906. struct kmem_list3 *l3;
  1907. int node;
  1908. on_each_cpu(do_drain, cachep, 1, 1);
  1909. check_irq_on();
  1910. for_each_online_node(node) {
  1911. l3 = cachep->nodelists[node];
  1912. if (l3) {
  1913. spin_lock_irq(&l3->list_lock);
  1914. drain_array_locked(cachep, l3->shared, 1, node);
  1915. spin_unlock_irq(&l3->list_lock);
  1916. if (l3->alien)
  1917. drain_alien_cache(cachep, l3->alien);
  1918. }
  1919. }
  1920. }
  1921. static int __node_shrink(struct kmem_cache *cachep, int node)
  1922. {
  1923. struct slab *slabp;
  1924. struct kmem_list3 *l3 = cachep->nodelists[node];
  1925. int ret;
  1926. for (;;) {
  1927. struct list_head *p;
  1928. p = l3->slabs_free.prev;
  1929. if (p == &l3->slabs_free)
  1930. break;
  1931. slabp = list_entry(l3->slabs_free.prev, struct slab, list);
  1932. #if DEBUG
  1933. if (slabp->inuse)
  1934. BUG();
  1935. #endif
  1936. list_del(&slabp->list);
  1937. l3->free_objects -= cachep->num;
  1938. spin_unlock_irq(&l3->list_lock);
  1939. slab_destroy(cachep, slabp);
  1940. spin_lock_irq(&l3->list_lock);
  1941. }
  1942. ret = !list_empty(&l3->slabs_full) || !list_empty(&l3->slabs_partial);
  1943. return ret;
  1944. }
  1945. static int __cache_shrink(struct kmem_cache *cachep)
  1946. {
  1947. int ret = 0, i = 0;
  1948. struct kmem_list3 *l3;
  1949. drain_cpu_caches(cachep);
  1950. check_irq_on();
  1951. for_each_online_node(i) {
  1952. l3 = cachep->nodelists[i];
  1953. if (l3) {
  1954. spin_lock_irq(&l3->list_lock);
  1955. ret += __node_shrink(cachep, i);
  1956. spin_unlock_irq(&l3->list_lock);
  1957. }
  1958. }
  1959. return (ret ? 1 : 0);
  1960. }
  1961. /**
  1962. * kmem_cache_shrink - Shrink a cache.
  1963. * @cachep: The cache to shrink.
  1964. *
  1965. * Releases as many slabs as possible for a cache.
  1966. * To help debugging, a zero exit status indicates all slabs were released.
  1967. */
  1968. int kmem_cache_shrink(struct kmem_cache *cachep)
  1969. {
  1970. if (!cachep || in_interrupt())
  1971. BUG();
  1972. return __cache_shrink(cachep);
  1973. }
  1974. EXPORT_SYMBOL(kmem_cache_shrink);
  1975. /**
  1976. * kmem_cache_destroy - delete a cache
  1977. * @cachep: the cache to destroy
  1978. *
  1979. * Remove a struct kmem_cache object from the slab cache.
  1980. * Returns 0 on success.
  1981. *
  1982. * It is expected this function will be called by a module when it is
  1983. * unloaded. This will remove the cache completely, and avoid a duplicate
  1984. * cache being allocated each time a module is loaded and unloaded, if the
  1985. * module doesn't have persistent in-kernel storage across loads and unloads.
  1986. *
  1987. * The cache must be empty before calling this function.
  1988. *
  1989. * The caller must guarantee that noone will allocate memory from the cache
  1990. * during the kmem_cache_destroy().
  1991. */
  1992. int kmem_cache_destroy(struct kmem_cache *cachep)
  1993. {
  1994. int i;
  1995. struct kmem_list3 *l3;
  1996. if (!cachep || in_interrupt())
  1997. BUG();
  1998. /* Don't let CPUs to come and go */
  1999. lock_cpu_hotplug();
  2000. /* Find the cache in the chain of caches. */
  2001. mutex_lock(&cache_chain_mutex);
  2002. /*
  2003. * the chain is never empty, cache_cache is never destroyed
  2004. */
  2005. list_del(&cachep->next);
  2006. mutex_unlock(&cache_chain_mutex);
  2007. if (__cache_shrink(cachep)) {
  2008. slab_error(cachep, "Can't free all objects");
  2009. mutex_lock(&cache_chain_mutex);
  2010. list_add(&cachep->next, &cache_chain);
  2011. mutex_unlock(&cache_chain_mutex);
  2012. unlock_cpu_hotplug();
  2013. return 1;
  2014. }
  2015. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
  2016. synchronize_rcu();
  2017. for_each_online_cpu(i)
  2018. kfree(cachep->array[i]);
  2019. /* NUMA: free the list3 structures */
  2020. for_each_online_node(i) {
  2021. l3 = cachep->nodelists[i];
  2022. if (l3) {
  2023. kfree(l3->shared);
  2024. free_alien_cache(l3->alien);
  2025. kfree(l3);
  2026. }
  2027. }
  2028. kmem_cache_free(&cache_cache, cachep);
  2029. unlock_cpu_hotplug();
  2030. return 0;
  2031. }
  2032. EXPORT_SYMBOL(kmem_cache_destroy);
  2033. /* Get the memory for a slab management obj. */
  2034. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
  2035. int colour_off, gfp_t local_flags)
  2036. {
  2037. struct slab *slabp;
  2038. if (OFF_SLAB(cachep)) {
  2039. /* Slab management obj is off-slab. */
  2040. slabp = kmem_cache_alloc(cachep->slabp_cache, local_flags);
  2041. if (!slabp)
  2042. return NULL;
  2043. } else {
  2044. slabp = objp + colour_off;
  2045. colour_off += cachep->slab_size;
  2046. }
  2047. slabp->inuse = 0;
  2048. slabp->colouroff = colour_off;
  2049. slabp->s_mem = objp + colour_off;
  2050. return slabp;
  2051. }
  2052. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  2053. {
  2054. return (kmem_bufctl_t *) (slabp + 1);
  2055. }
  2056. static void cache_init_objs(struct kmem_cache *cachep,
  2057. struct slab *slabp, unsigned long ctor_flags)
  2058. {
  2059. int i;
  2060. for (i = 0; i < cachep->num; i++) {
  2061. void *objp = index_to_obj(cachep, slabp, i);
  2062. #if DEBUG
  2063. /* need to poison the objs? */
  2064. if (cachep->flags & SLAB_POISON)
  2065. poison_obj(cachep, objp, POISON_FREE);
  2066. if (cachep->flags & SLAB_STORE_USER)
  2067. *dbg_userword(cachep, objp) = NULL;
  2068. if (cachep->flags & SLAB_RED_ZONE) {
  2069. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2070. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2071. }
  2072. /*
  2073. * Constructors are not allowed to allocate memory from the same
  2074. * cache which they are a constructor for. Otherwise, deadlock.
  2075. * They must also be threaded.
  2076. */
  2077. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  2078. cachep->ctor(objp + obj_offset(cachep), cachep,
  2079. ctor_flags);
  2080. if (cachep->flags & SLAB_RED_ZONE) {
  2081. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  2082. slab_error(cachep, "constructor overwrote the"
  2083. " end of an object");
  2084. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  2085. slab_error(cachep, "constructor overwrote the"
  2086. " start of an object");
  2087. }
  2088. if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
  2089. OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  2090. kernel_map_pages(virt_to_page(objp),
  2091. cachep->buffer_size / PAGE_SIZE, 0);
  2092. #else
  2093. if (cachep->ctor)
  2094. cachep->ctor(objp, cachep, ctor_flags);
  2095. #endif
  2096. slab_bufctl(slabp)[i] = i + 1;
  2097. }
  2098. slab_bufctl(slabp)[i - 1] = BUFCTL_END;
  2099. slabp->free = 0;
  2100. }
  2101. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  2102. {
  2103. if (flags & SLAB_DMA)
  2104. BUG_ON(!(cachep->gfpflags & GFP_DMA));
  2105. else
  2106. BUG_ON(cachep->gfpflags & GFP_DMA);
  2107. }
  2108. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  2109. int nodeid)
  2110. {
  2111. void *objp = index_to_obj(cachep, slabp, slabp->free);
  2112. kmem_bufctl_t next;
  2113. slabp->inuse++;
  2114. next = slab_bufctl(slabp)[slabp->free];
  2115. #if DEBUG
  2116. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2117. WARN_ON(slabp->nodeid != nodeid);
  2118. #endif
  2119. slabp->free = next;
  2120. return objp;
  2121. }
  2122. static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  2123. void *objp, int nodeid)
  2124. {
  2125. unsigned int objnr = obj_to_index(cachep, slabp, objp);
  2126. #if DEBUG
  2127. /* Verify that the slab belongs to the intended node */
  2128. WARN_ON(slabp->nodeid != nodeid);
  2129. if (slab_bufctl(slabp)[objnr] != BUFCTL_FREE) {
  2130. printk(KERN_ERR "slab: double free detected in cache "
  2131. "'%s', objp %p\n", cachep->name, objp);
  2132. BUG();
  2133. }
  2134. #endif
  2135. slab_bufctl(slabp)[objnr] = slabp->free;
  2136. slabp->free = objnr;
  2137. slabp->inuse--;
  2138. }
  2139. static void set_slab_attr(struct kmem_cache *cachep, struct slab *slabp,
  2140. void *objp)
  2141. {
  2142. int i;
  2143. struct page *page;
  2144. /* Nasty!!!!!! I hope this is OK. */
  2145. page = virt_to_page(objp);
  2146. i = 1;
  2147. if (likely(!PageCompound(page)))
  2148. i <<= cachep->gfporder;
  2149. do {
  2150. page_set_cache(page, cachep);
  2151. page_set_slab(page, slabp);
  2152. page++;
  2153. } while (--i);
  2154. }
  2155. /*
  2156. * Grow (by 1) the number of slabs within a cache. This is called by
  2157. * kmem_cache_alloc() when there are no active objs left in a cache.
  2158. */
  2159. static int cache_grow(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  2160. {
  2161. struct slab *slabp;
  2162. void *objp;
  2163. size_t offset;
  2164. gfp_t local_flags;
  2165. unsigned long ctor_flags;
  2166. struct kmem_list3 *l3;
  2167. /*
  2168. * Be lazy and only check for valid flags here, keeping it out of the
  2169. * critical path in kmem_cache_alloc().
  2170. */
  2171. if (flags & ~(SLAB_DMA | SLAB_LEVEL_MASK | SLAB_NO_GROW))
  2172. BUG();
  2173. if (flags & SLAB_NO_GROW)
  2174. return 0;
  2175. ctor_flags = SLAB_CTOR_CONSTRUCTOR;
  2176. local_flags = (flags & SLAB_LEVEL_MASK);
  2177. if (!(local_flags & __GFP_WAIT))
  2178. /*
  2179. * Not allowed to sleep. Need to tell a constructor about
  2180. * this - it might need to know...
  2181. */
  2182. ctor_flags |= SLAB_CTOR_ATOMIC;
  2183. /* Take the l3 list lock to change the colour_next on this node */
  2184. check_irq_off();
  2185. l3 = cachep->nodelists[nodeid];
  2186. spin_lock(&l3->list_lock);
  2187. /* Get colour for the slab, and cal the next value. */
  2188. offset = l3->colour_next;
  2189. l3->colour_next++;
  2190. if (l3->colour_next >= cachep->colour)
  2191. l3->colour_next = 0;
  2192. spin_unlock(&l3->list_lock);
  2193. offset *= cachep->colour_off;
  2194. if (local_flags & __GFP_WAIT)
  2195. local_irq_enable();
  2196. /*
  2197. * The test for missing atomic flag is performed here, rather than
  2198. * the more obvious place, simply to reduce the critical path length
  2199. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  2200. * will eventually be caught here (where it matters).
  2201. */
  2202. kmem_flagcheck(cachep, flags);
  2203. /*
  2204. * Get mem for the objs. Attempt to allocate a physical page from
  2205. * 'nodeid'.
  2206. */
  2207. objp = kmem_getpages(cachep, flags, nodeid);
  2208. if (!objp)
  2209. goto failed;
  2210. /* Get slab management. */
  2211. slabp = alloc_slabmgmt(cachep, objp, offset, local_flags);
  2212. if (!slabp)
  2213. goto opps1;
  2214. slabp->nodeid = nodeid;
  2215. set_slab_attr(cachep, slabp, objp);
  2216. cache_init_objs(cachep, slabp, ctor_flags);
  2217. if (local_flags & __GFP_WAIT)
  2218. local_irq_disable();
  2219. check_irq_off();
  2220. spin_lock(&l3->list_lock);
  2221. /* Make slab active. */
  2222. list_add_tail(&slabp->list, &(l3->slabs_free));
  2223. STATS_INC_GROWN(cachep);
  2224. l3->free_objects += cachep->num;
  2225. spin_unlock(&l3->list_lock);
  2226. return 1;
  2227. opps1:
  2228. kmem_freepages(cachep, objp);
  2229. failed:
  2230. if (local_flags & __GFP_WAIT)
  2231. local_irq_disable();
  2232. return 0;
  2233. }
  2234. #if DEBUG
  2235. /*
  2236. * Perform extra freeing checks:
  2237. * - detect bad pointers.
  2238. * - POISON/RED_ZONE checking
  2239. * - destructor calls, for caches with POISON+dtor
  2240. */
  2241. static void kfree_debugcheck(const void *objp)
  2242. {
  2243. struct page *page;
  2244. if (!virt_addr_valid(objp)) {
  2245. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  2246. (unsigned long)objp);
  2247. BUG();
  2248. }
  2249. page = virt_to_page(objp);
  2250. if (!PageSlab(page)) {
  2251. printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n",
  2252. (unsigned long)objp);
  2253. BUG();
  2254. }
  2255. }
  2256. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2257. void *caller)
  2258. {
  2259. struct page *page;
  2260. unsigned int objnr;
  2261. struct slab *slabp;
  2262. objp -= obj_offset(cachep);
  2263. kfree_debugcheck(objp);
  2264. page = virt_to_page(objp);
  2265. if (page_get_cache(page) != cachep) {
  2266. printk(KERN_ERR "mismatch in kmem_cache_free: expected "
  2267. "cache %p, got %p\n",
  2268. page_get_cache(page), cachep);
  2269. printk(KERN_ERR "%p is %s.\n", cachep, cachep->name);
  2270. printk(KERN_ERR "%p is %s.\n", page_get_cache(page),
  2271. page_get_cache(page)->name);
  2272. WARN_ON(1);
  2273. }
  2274. slabp = page_get_slab(page);
  2275. if (cachep->flags & SLAB_RED_ZONE) {
  2276. if (*dbg_redzone1(cachep, objp) != RED_ACTIVE ||
  2277. *dbg_redzone2(cachep, objp) != RED_ACTIVE) {
  2278. slab_error(cachep, "double free, or memory outside"
  2279. " object was overwritten");
  2280. printk(KERN_ERR "%p: redzone 1:0x%lx, "
  2281. "redzone 2:0x%lx.\n",
  2282. objp, *dbg_redzone1(cachep, objp),
  2283. *dbg_redzone2(cachep, objp));
  2284. }
  2285. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2286. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2287. }
  2288. if (cachep->flags & SLAB_STORE_USER)
  2289. *dbg_userword(cachep, objp) = caller;
  2290. objnr = obj_to_index(cachep, slabp, objp);
  2291. BUG_ON(objnr >= cachep->num);
  2292. BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
  2293. if (cachep->flags & SLAB_DEBUG_INITIAL) {
  2294. /*
  2295. * Need to call the slab's constructor so the caller can
  2296. * perform a verify of its state (debugging). Called without
  2297. * the cache-lock held.
  2298. */
  2299. cachep->ctor(objp + obj_offset(cachep),
  2300. cachep, SLAB_CTOR_CONSTRUCTOR | SLAB_CTOR_VERIFY);
  2301. }
  2302. if (cachep->flags & SLAB_POISON && cachep->dtor) {
  2303. /* we want to cache poison the object,
  2304. * call the destruction callback
  2305. */
  2306. cachep->dtor(objp + obj_offset(cachep), cachep, 0);
  2307. }
  2308. if (cachep->flags & SLAB_POISON) {
  2309. #ifdef CONFIG_DEBUG_PAGEALLOC
  2310. if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
  2311. store_stackinfo(cachep, objp, (unsigned long)caller);
  2312. kernel_map_pages(virt_to_page(objp),
  2313. cachep->buffer_size / PAGE_SIZE, 0);
  2314. } else {
  2315. poison_obj(cachep, objp, POISON_FREE);
  2316. }
  2317. #else
  2318. poison_obj(cachep, objp, POISON_FREE);
  2319. #endif
  2320. }
  2321. return objp;
  2322. }
  2323. static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
  2324. {
  2325. kmem_bufctl_t i;
  2326. int entries = 0;
  2327. /* Check slab's freelist to see if this obj is there. */
  2328. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2329. entries++;
  2330. if (entries > cachep->num || i >= cachep->num)
  2331. goto bad;
  2332. }
  2333. if (entries != cachep->num - slabp->inuse) {
  2334. bad:
  2335. printk(KERN_ERR "slab: Internal list corruption detected in "
  2336. "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
  2337. cachep->name, cachep->num, slabp, slabp->inuse);
  2338. for (i = 0;
  2339. i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
  2340. i++) {
  2341. if (i % 16 == 0)
  2342. printk("\n%03x:", i);
  2343. printk(" %02x", ((unsigned char *)slabp)[i]);
  2344. }
  2345. printk("\n");
  2346. BUG();
  2347. }
  2348. }
  2349. #else
  2350. #define kfree_debugcheck(x) do { } while(0)
  2351. #define cache_free_debugcheck(x,objp,z) (objp)
  2352. #define check_slabp(x,y) do { } while(0)
  2353. #endif
  2354. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
  2355. {
  2356. int batchcount;
  2357. struct kmem_list3 *l3;
  2358. struct array_cache *ac;
  2359. check_irq_off();
  2360. ac = cpu_cache_get(cachep);
  2361. retry:
  2362. batchcount = ac->batchcount;
  2363. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2364. /*
  2365. * If there was little recent activity on this cache, then
  2366. * perform only a partial refill. Otherwise we could generate
  2367. * refill bouncing.
  2368. */
  2369. batchcount = BATCHREFILL_LIMIT;
  2370. }
  2371. l3 = cachep->nodelists[numa_node_id()];
  2372. BUG_ON(ac->avail > 0 || !l3);
  2373. spin_lock(&l3->list_lock);
  2374. if (l3->shared) {
  2375. struct array_cache *shared_array = l3->shared;
  2376. if (shared_array->avail) {
  2377. if (batchcount > shared_array->avail)
  2378. batchcount = shared_array->avail;
  2379. shared_array->avail -= batchcount;
  2380. ac->avail = batchcount;
  2381. memcpy(ac->entry,
  2382. &(shared_array->entry[shared_array->avail]),
  2383. sizeof(void *) * batchcount);
  2384. shared_array->touched = 1;
  2385. goto alloc_done;
  2386. }
  2387. }
  2388. while (batchcount > 0) {
  2389. struct list_head *entry;
  2390. struct slab *slabp;
  2391. /* Get slab alloc is to come from. */
  2392. entry = l3->slabs_partial.next;
  2393. if (entry == &l3->slabs_partial) {
  2394. l3->free_touched = 1;
  2395. entry = l3->slabs_free.next;
  2396. if (entry == &l3->slabs_free)
  2397. goto must_grow;
  2398. }
  2399. slabp = list_entry(entry, struct slab, list);
  2400. check_slabp(cachep, slabp);
  2401. check_spinlock_acquired(cachep);
  2402. while (slabp->inuse < cachep->num && batchcount--) {
  2403. STATS_INC_ALLOCED(cachep);
  2404. STATS_INC_ACTIVE(cachep);
  2405. STATS_SET_HIGH(cachep);
  2406. ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
  2407. numa_node_id());
  2408. }
  2409. check_slabp(cachep, slabp);
  2410. /* move slabp to correct slabp list: */
  2411. list_del(&slabp->list);
  2412. if (slabp->free == BUFCTL_END)
  2413. list_add(&slabp->list, &l3->slabs_full);
  2414. else
  2415. list_add(&slabp->list, &l3->slabs_partial);
  2416. }
  2417. must_grow:
  2418. l3->free_objects -= ac->avail;
  2419. alloc_done:
  2420. spin_unlock(&l3->list_lock);
  2421. if (unlikely(!ac->avail)) {
  2422. int x;
  2423. x = cache_grow(cachep, flags, numa_node_id());
  2424. /* cache_grow can reenable interrupts, then ac could change. */
  2425. ac = cpu_cache_get(cachep);
  2426. if (!x && ac->avail == 0) /* no objects in sight? abort */
  2427. return NULL;
  2428. if (!ac->avail) /* objects refilled by interrupt? */
  2429. goto retry;
  2430. }
  2431. ac->touched = 1;
  2432. return ac->entry[--ac->avail];
  2433. }
  2434. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2435. gfp_t flags)
  2436. {
  2437. might_sleep_if(flags & __GFP_WAIT);
  2438. #if DEBUG
  2439. kmem_flagcheck(cachep, flags);
  2440. #endif
  2441. }
  2442. #if DEBUG
  2443. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2444. gfp_t flags, void *objp, void *caller)
  2445. {
  2446. if (!objp)
  2447. return objp;
  2448. if (cachep->flags & SLAB_POISON) {
  2449. #ifdef CONFIG_DEBUG_PAGEALLOC
  2450. if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2451. kernel_map_pages(virt_to_page(objp),
  2452. cachep->buffer_size / PAGE_SIZE, 1);
  2453. else
  2454. check_poison_obj(cachep, objp);
  2455. #else
  2456. check_poison_obj(cachep, objp);
  2457. #endif
  2458. poison_obj(cachep, objp, POISON_INUSE);
  2459. }
  2460. if (cachep->flags & SLAB_STORE_USER)
  2461. *dbg_userword(cachep, objp) = caller;
  2462. if (cachep->flags & SLAB_RED_ZONE) {
  2463. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2464. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2465. slab_error(cachep, "double free, or memory outside"
  2466. " object was overwritten");
  2467. printk(KERN_ERR
  2468. "%p: redzone 1:0x%lx, redzone 2:0x%lx\n",
  2469. objp, *dbg_redzone1(cachep, objp),
  2470. *dbg_redzone2(cachep, objp));
  2471. }
  2472. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2473. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2474. }
  2475. objp += obj_offset(cachep);
  2476. if (cachep->ctor && cachep->flags & SLAB_POISON) {
  2477. unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
  2478. if (!(flags & __GFP_WAIT))
  2479. ctor_flags |= SLAB_CTOR_ATOMIC;
  2480. cachep->ctor(objp, cachep, ctor_flags);
  2481. }
  2482. return objp;
  2483. }
  2484. #else
  2485. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2486. #endif
  2487. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2488. {
  2489. void *objp;
  2490. struct array_cache *ac;
  2491. #ifdef CONFIG_NUMA
  2492. if (unlikely(current->mempolicy && !in_interrupt())) {
  2493. int nid = slab_node(current->mempolicy);
  2494. if (nid != numa_node_id())
  2495. return __cache_alloc_node(cachep, flags, nid);
  2496. }
  2497. #endif
  2498. check_irq_off();
  2499. ac = cpu_cache_get(cachep);
  2500. if (likely(ac->avail)) {
  2501. STATS_INC_ALLOCHIT(cachep);
  2502. ac->touched = 1;
  2503. objp = ac->entry[--ac->avail];
  2504. } else {
  2505. STATS_INC_ALLOCMISS(cachep);
  2506. objp = cache_alloc_refill(cachep, flags);
  2507. }
  2508. return objp;
  2509. }
  2510. static __always_inline void *__cache_alloc(struct kmem_cache *cachep,
  2511. gfp_t flags, void *caller)
  2512. {
  2513. unsigned long save_flags;
  2514. void *objp;
  2515. cache_alloc_debugcheck_before(cachep, flags);
  2516. local_irq_save(save_flags);
  2517. objp = ____cache_alloc(cachep, flags);
  2518. local_irq_restore(save_flags);
  2519. objp = cache_alloc_debugcheck_after(cachep, flags, objp,
  2520. caller);
  2521. prefetchw(objp);
  2522. return objp;
  2523. }
  2524. #ifdef CONFIG_NUMA
  2525. /*
  2526. * A interface to enable slab creation on nodeid
  2527. */
  2528. static void *__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  2529. int nodeid)
  2530. {
  2531. struct list_head *entry;
  2532. struct slab *slabp;
  2533. struct kmem_list3 *l3;
  2534. void *obj;
  2535. int x;
  2536. l3 = cachep->nodelists[nodeid];
  2537. BUG_ON(!l3);
  2538. retry:
  2539. check_irq_off();
  2540. spin_lock(&l3->list_lock);
  2541. entry = l3->slabs_partial.next;
  2542. if (entry == &l3->slabs_partial) {
  2543. l3->free_touched = 1;
  2544. entry = l3->slabs_free.next;
  2545. if (entry == &l3->slabs_free)
  2546. goto must_grow;
  2547. }
  2548. slabp = list_entry(entry, struct slab, list);
  2549. check_spinlock_acquired_node(cachep, nodeid);
  2550. check_slabp(cachep, slabp);
  2551. STATS_INC_NODEALLOCS(cachep);
  2552. STATS_INC_ACTIVE(cachep);
  2553. STATS_SET_HIGH(cachep);
  2554. BUG_ON(slabp->inuse == cachep->num);
  2555. obj = slab_get_obj(cachep, slabp, nodeid);
  2556. check_slabp(cachep, slabp);
  2557. l3->free_objects--;
  2558. /* move slabp to correct slabp list: */
  2559. list_del(&slabp->list);
  2560. if (slabp->free == BUFCTL_END)
  2561. list_add(&slabp->list, &l3->slabs_full);
  2562. else
  2563. list_add(&slabp->list, &l3->slabs_partial);
  2564. spin_unlock(&l3->list_lock);
  2565. goto done;
  2566. must_grow:
  2567. spin_unlock(&l3->list_lock);
  2568. x = cache_grow(cachep, flags, nodeid);
  2569. if (!x)
  2570. return NULL;
  2571. goto retry;
  2572. done:
  2573. return obj;
  2574. }
  2575. #endif
  2576. /*
  2577. * Caller needs to acquire correct kmem_list's list_lock
  2578. */
  2579. static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
  2580. int node)
  2581. {
  2582. int i;
  2583. struct kmem_list3 *l3;
  2584. for (i = 0; i < nr_objects; i++) {
  2585. void *objp = objpp[i];
  2586. struct slab *slabp;
  2587. slabp = virt_to_slab(objp);
  2588. l3 = cachep->nodelists[node];
  2589. list_del(&slabp->list);
  2590. check_spinlock_acquired_node(cachep, node);
  2591. check_slabp(cachep, slabp);
  2592. slab_put_obj(cachep, slabp, objp, node);
  2593. STATS_DEC_ACTIVE(cachep);
  2594. l3->free_objects++;
  2595. check_slabp(cachep, slabp);
  2596. /* fixup slab chains */
  2597. if (slabp->inuse == 0) {
  2598. if (l3->free_objects > l3->free_limit) {
  2599. l3->free_objects -= cachep->num;
  2600. slab_destroy(cachep, slabp);
  2601. } else {
  2602. list_add(&slabp->list, &l3->slabs_free);
  2603. }
  2604. } else {
  2605. /* Unconditionally move a slab to the end of the
  2606. * partial list on free - maximum time for the
  2607. * other objects to be freed, too.
  2608. */
  2609. list_add_tail(&slabp->list, &l3->slabs_partial);
  2610. }
  2611. }
  2612. }
  2613. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  2614. {
  2615. int batchcount;
  2616. struct kmem_list3 *l3;
  2617. int node = numa_node_id();
  2618. batchcount = ac->batchcount;
  2619. #if DEBUG
  2620. BUG_ON(!batchcount || batchcount > ac->avail);
  2621. #endif
  2622. check_irq_off();
  2623. l3 = cachep->nodelists[node];
  2624. spin_lock(&l3->list_lock);
  2625. if (l3->shared) {
  2626. struct array_cache *shared_array = l3->shared;
  2627. int max = shared_array->limit - shared_array->avail;
  2628. if (max) {
  2629. if (batchcount > max)
  2630. batchcount = max;
  2631. memcpy(&(shared_array->entry[shared_array->avail]),
  2632. ac->entry, sizeof(void *) * batchcount);
  2633. shared_array->avail += batchcount;
  2634. goto free_done;
  2635. }
  2636. }
  2637. free_block(cachep, ac->entry, batchcount, node);
  2638. free_done:
  2639. #if STATS
  2640. {
  2641. int i = 0;
  2642. struct list_head *p;
  2643. p = l3->slabs_free.next;
  2644. while (p != &(l3->slabs_free)) {
  2645. struct slab *slabp;
  2646. slabp = list_entry(p, struct slab, list);
  2647. BUG_ON(slabp->inuse);
  2648. i++;
  2649. p = p->next;
  2650. }
  2651. STATS_SET_FREEABLE(cachep, i);
  2652. }
  2653. #endif
  2654. spin_unlock(&l3->list_lock);
  2655. ac->avail -= batchcount;
  2656. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  2657. }
  2658. /*
  2659. * Release an obj back to its cache. If the obj has a constructed state, it must
  2660. * be in this state _before_ it is released. Called with disabled ints.
  2661. */
  2662. static inline void __cache_free(struct kmem_cache *cachep, void *objp)
  2663. {
  2664. struct array_cache *ac = cpu_cache_get(cachep);
  2665. check_irq_off();
  2666. objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
  2667. /* Make sure we are not freeing a object from another
  2668. * node to the array cache on this cpu.
  2669. */
  2670. #ifdef CONFIG_NUMA
  2671. {
  2672. struct slab *slabp;
  2673. slabp = virt_to_slab(objp);
  2674. if (unlikely(slabp->nodeid != numa_node_id())) {
  2675. struct array_cache *alien = NULL;
  2676. int nodeid = slabp->nodeid;
  2677. struct kmem_list3 *l3;
  2678. l3 = cachep->nodelists[numa_node_id()];
  2679. STATS_INC_NODEFREES(cachep);
  2680. if (l3->alien && l3->alien[nodeid]) {
  2681. alien = l3->alien[nodeid];
  2682. spin_lock(&alien->lock);
  2683. if (unlikely(alien->avail == alien->limit))
  2684. __drain_alien_cache(cachep,
  2685. alien, nodeid);
  2686. alien->entry[alien->avail++] = objp;
  2687. spin_unlock(&alien->lock);
  2688. } else {
  2689. spin_lock(&(cachep->nodelists[nodeid])->
  2690. list_lock);
  2691. free_block(cachep, &objp, 1, nodeid);
  2692. spin_unlock(&(cachep->nodelists[nodeid])->
  2693. list_lock);
  2694. }
  2695. return;
  2696. }
  2697. }
  2698. #endif
  2699. if (likely(ac->avail < ac->limit)) {
  2700. STATS_INC_FREEHIT(cachep);
  2701. ac->entry[ac->avail++] = objp;
  2702. return;
  2703. } else {
  2704. STATS_INC_FREEMISS(cachep);
  2705. cache_flusharray(cachep, ac);
  2706. ac->entry[ac->avail++] = objp;
  2707. }
  2708. }
  2709. /**
  2710. * kmem_cache_alloc - Allocate an object
  2711. * @cachep: The cache to allocate from.
  2712. * @flags: See kmalloc().
  2713. *
  2714. * Allocate an object from this cache. The flags are only relevant
  2715. * if the cache has no available objects.
  2716. */
  2717. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2718. {
  2719. return __cache_alloc(cachep, flags, __builtin_return_address(0));
  2720. }
  2721. EXPORT_SYMBOL(kmem_cache_alloc);
  2722. /**
  2723. * kmem_ptr_validate - check if an untrusted pointer might
  2724. * be a slab entry.
  2725. * @cachep: the cache we're checking against
  2726. * @ptr: pointer to validate
  2727. *
  2728. * This verifies that the untrusted pointer looks sane:
  2729. * it is _not_ a guarantee that the pointer is actually
  2730. * part of the slab cache in question, but it at least
  2731. * validates that the pointer can be dereferenced and
  2732. * looks half-way sane.
  2733. *
  2734. * Currently only used for dentry validation.
  2735. */
  2736. int fastcall kmem_ptr_validate(struct kmem_cache *cachep, void *ptr)
  2737. {
  2738. unsigned long addr = (unsigned long)ptr;
  2739. unsigned long min_addr = PAGE_OFFSET;
  2740. unsigned long align_mask = BYTES_PER_WORD - 1;
  2741. unsigned long size = cachep->buffer_size;
  2742. struct page *page;
  2743. if (unlikely(addr < min_addr))
  2744. goto out;
  2745. if (unlikely(addr > (unsigned long)high_memory - size))
  2746. goto out;
  2747. if (unlikely(addr & align_mask))
  2748. goto out;
  2749. if (unlikely(!kern_addr_valid(addr)))
  2750. goto out;
  2751. if (unlikely(!kern_addr_valid(addr + size - 1)))
  2752. goto out;
  2753. page = virt_to_page(ptr);
  2754. if (unlikely(!PageSlab(page)))
  2755. goto out;
  2756. if (unlikely(page_get_cache(page) != cachep))
  2757. goto out;
  2758. return 1;
  2759. out:
  2760. return 0;
  2761. }
  2762. #ifdef CONFIG_NUMA
  2763. /**
  2764. * kmem_cache_alloc_node - Allocate an object on the specified node
  2765. * @cachep: The cache to allocate from.
  2766. * @flags: See kmalloc().
  2767. * @nodeid: node number of the target node.
  2768. *
  2769. * Identical to kmem_cache_alloc, except that this function is slow
  2770. * and can sleep. And it will allocate memory on the given node, which
  2771. * can improve the performance for cpu bound structures.
  2772. * New and improved: it will now make sure that the object gets
  2773. * put on the correct node list so that there is no false sharing.
  2774. */
  2775. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  2776. {
  2777. unsigned long save_flags;
  2778. void *ptr;
  2779. cache_alloc_debugcheck_before(cachep, flags);
  2780. local_irq_save(save_flags);
  2781. if (nodeid == -1 || nodeid == numa_node_id() ||
  2782. !cachep->nodelists[nodeid])
  2783. ptr = ____cache_alloc(cachep, flags);
  2784. else
  2785. ptr = __cache_alloc_node(cachep, flags, nodeid);
  2786. local_irq_restore(save_flags);
  2787. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr,
  2788. __builtin_return_address(0));
  2789. return ptr;
  2790. }
  2791. EXPORT_SYMBOL(kmem_cache_alloc_node);
  2792. void *kmalloc_node(size_t size, gfp_t flags, int node)
  2793. {
  2794. struct kmem_cache *cachep;
  2795. cachep = kmem_find_general_cachep(size, flags);
  2796. if (unlikely(cachep == NULL))
  2797. return NULL;
  2798. return kmem_cache_alloc_node(cachep, flags, node);
  2799. }
  2800. EXPORT_SYMBOL(kmalloc_node);
  2801. #endif
  2802. /**
  2803. * kmalloc - allocate memory
  2804. * @size: how many bytes of memory are required.
  2805. * @flags: the type of memory to allocate.
  2806. * @caller: function caller for debug tracking of the caller
  2807. *
  2808. * kmalloc is the normal method of allocating memory
  2809. * in the kernel.
  2810. *
  2811. * The @flags argument may be one of:
  2812. *
  2813. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  2814. *
  2815. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  2816. *
  2817. * %GFP_ATOMIC - Allocation will not sleep. Use inside interrupt handlers.
  2818. *
  2819. * Additionally, the %GFP_DMA flag may be set to indicate the memory
  2820. * must be suitable for DMA. This can mean different things on different
  2821. * platforms. For example, on i386, it means that the memory must come
  2822. * from the first 16MB.
  2823. */
  2824. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  2825. void *caller)
  2826. {
  2827. struct kmem_cache *cachep;
  2828. /* If you want to save a few bytes .text space: replace
  2829. * __ with kmem_.
  2830. * Then kmalloc uses the uninlined functions instead of the inline
  2831. * functions.
  2832. */
  2833. cachep = __find_general_cachep(size, flags);
  2834. if (unlikely(cachep == NULL))
  2835. return NULL;
  2836. return __cache_alloc(cachep, flags, caller);
  2837. }
  2838. #ifndef CONFIG_DEBUG_SLAB
  2839. void *__kmalloc(size_t size, gfp_t flags)
  2840. {
  2841. return __do_kmalloc(size, flags, NULL);
  2842. }
  2843. EXPORT_SYMBOL(__kmalloc);
  2844. #else
  2845. void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
  2846. {
  2847. return __do_kmalloc(size, flags, caller);
  2848. }
  2849. EXPORT_SYMBOL(__kmalloc_track_caller);
  2850. #endif
  2851. #ifdef CONFIG_SMP
  2852. /**
  2853. * __alloc_percpu - allocate one copy of the object for every present
  2854. * cpu in the system, zeroing them.
  2855. * Objects should be dereferenced using the per_cpu_ptr macro only.
  2856. *
  2857. * @size: how many bytes of memory are required.
  2858. */
  2859. void *__alloc_percpu(size_t size)
  2860. {
  2861. int i;
  2862. struct percpu_data *pdata = kmalloc(sizeof(*pdata), GFP_KERNEL);
  2863. if (!pdata)
  2864. return NULL;
  2865. /*
  2866. * Cannot use for_each_online_cpu since a cpu may come online
  2867. * and we have no way of figuring out how to fix the array
  2868. * that we have allocated then....
  2869. */
  2870. for_each_cpu(i) {
  2871. int node = cpu_to_node(i);
  2872. if (node_online(node))
  2873. pdata->ptrs[i] = kmalloc_node(size, GFP_KERNEL, node);
  2874. else
  2875. pdata->ptrs[i] = kmalloc(size, GFP_KERNEL);
  2876. if (!pdata->ptrs[i])
  2877. goto unwind_oom;
  2878. memset(pdata->ptrs[i], 0, size);
  2879. }
  2880. /* Catch derefs w/o wrappers */
  2881. return (void *)(~(unsigned long)pdata);
  2882. unwind_oom:
  2883. while (--i >= 0) {
  2884. if (!cpu_possible(i))
  2885. continue;
  2886. kfree(pdata->ptrs[i]);
  2887. }
  2888. kfree(pdata);
  2889. return NULL;
  2890. }
  2891. EXPORT_SYMBOL(__alloc_percpu);
  2892. #endif
  2893. /**
  2894. * kmem_cache_free - Deallocate an object
  2895. * @cachep: The cache the allocation was from.
  2896. * @objp: The previously allocated object.
  2897. *
  2898. * Free an object which was previously allocated from this
  2899. * cache.
  2900. */
  2901. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  2902. {
  2903. unsigned long flags;
  2904. local_irq_save(flags);
  2905. __cache_free(cachep, objp);
  2906. local_irq_restore(flags);
  2907. }
  2908. EXPORT_SYMBOL(kmem_cache_free);
  2909. /**
  2910. * kfree - free previously allocated memory
  2911. * @objp: pointer returned by kmalloc.
  2912. *
  2913. * If @objp is NULL, no operation is performed.
  2914. *
  2915. * Don't free memory not originally allocated by kmalloc()
  2916. * or you will run into trouble.
  2917. */
  2918. void kfree(const void *objp)
  2919. {
  2920. struct kmem_cache *c;
  2921. unsigned long flags;
  2922. if (unlikely(!objp))
  2923. return;
  2924. local_irq_save(flags);
  2925. kfree_debugcheck(objp);
  2926. c = virt_to_cache(objp);
  2927. mutex_debug_check_no_locks_freed(objp, obj_size(c));
  2928. __cache_free(c, (void *)objp);
  2929. local_irq_restore(flags);
  2930. }
  2931. EXPORT_SYMBOL(kfree);
  2932. #ifdef CONFIG_SMP
  2933. /**
  2934. * free_percpu - free previously allocated percpu memory
  2935. * @objp: pointer returned by alloc_percpu.
  2936. *
  2937. * Don't free memory not originally allocated by alloc_percpu()
  2938. * The complemented objp is to check for that.
  2939. */
  2940. void free_percpu(const void *objp)
  2941. {
  2942. int i;
  2943. struct percpu_data *p = (struct percpu_data *)(~(unsigned long)objp);
  2944. /*
  2945. * We allocate for all cpus so we cannot use for online cpu here.
  2946. */
  2947. for_each_cpu(i)
  2948. kfree(p->ptrs[i]);
  2949. kfree(p);
  2950. }
  2951. EXPORT_SYMBOL(free_percpu);
  2952. #endif
  2953. unsigned int kmem_cache_size(struct kmem_cache *cachep)
  2954. {
  2955. return obj_size(cachep);
  2956. }
  2957. EXPORT_SYMBOL(kmem_cache_size);
  2958. const char *kmem_cache_name(struct kmem_cache *cachep)
  2959. {
  2960. return cachep->name;
  2961. }
  2962. EXPORT_SYMBOL_GPL(kmem_cache_name);
  2963. /*
  2964. * This initializes kmem_list3 for all nodes.
  2965. */
  2966. static int alloc_kmemlist(struct kmem_cache *cachep)
  2967. {
  2968. int node;
  2969. struct kmem_list3 *l3;
  2970. int err = 0;
  2971. for_each_online_node(node) {
  2972. struct array_cache *nc = NULL, *new;
  2973. struct array_cache **new_alien = NULL;
  2974. #ifdef CONFIG_NUMA
  2975. new_alien = alloc_alien_cache(node, cachep->limit);
  2976. if (!new_alien)
  2977. goto fail;
  2978. #endif
  2979. new = alloc_arraycache(node, cachep->shared*cachep->batchcount,
  2980. 0xbaadf00d);
  2981. if (!new)
  2982. goto fail;
  2983. l3 = cachep->nodelists[node];
  2984. if (l3) {
  2985. spin_lock_irq(&l3->list_lock);
  2986. nc = cachep->nodelists[node]->shared;
  2987. if (nc)
  2988. free_block(cachep, nc->entry, nc->avail, node);
  2989. l3->shared = new;
  2990. if (!cachep->nodelists[node]->alien) {
  2991. l3->alien = new_alien;
  2992. new_alien = NULL;
  2993. }
  2994. l3->free_limit = (1 + nr_cpus_node(node)) *
  2995. cachep->batchcount + cachep->num;
  2996. spin_unlock_irq(&l3->list_lock);
  2997. kfree(nc);
  2998. free_alien_cache(new_alien);
  2999. continue;
  3000. }
  3001. l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
  3002. if (!l3)
  3003. goto fail;
  3004. kmem_list3_init(l3);
  3005. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  3006. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  3007. l3->shared = new;
  3008. l3->alien = new_alien;
  3009. l3->free_limit = (1 + nr_cpus_node(node)) *
  3010. cachep->batchcount + cachep->num;
  3011. cachep->nodelists[node] = l3;
  3012. }
  3013. return err;
  3014. fail:
  3015. err = -ENOMEM;
  3016. return err;
  3017. }
  3018. struct ccupdate_struct {
  3019. struct kmem_cache *cachep;
  3020. struct array_cache *new[NR_CPUS];
  3021. };
  3022. static void do_ccupdate_local(void *info)
  3023. {
  3024. struct ccupdate_struct *new = info;
  3025. struct array_cache *old;
  3026. check_irq_off();
  3027. old = cpu_cache_get(new->cachep);
  3028. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  3029. new->new[smp_processor_id()] = old;
  3030. }
  3031. /* Always called with the cache_chain_mutex held */
  3032. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3033. int batchcount, int shared)
  3034. {
  3035. struct ccupdate_struct new;
  3036. int i, err;
  3037. memset(&new.new, 0, sizeof(new.new));
  3038. for_each_online_cpu(i) {
  3039. new.new[i] = alloc_arraycache(cpu_to_node(i), limit,
  3040. batchcount);
  3041. if (!new.new[i]) {
  3042. for (i--; i >= 0; i--)
  3043. kfree(new.new[i]);
  3044. return -ENOMEM;
  3045. }
  3046. }
  3047. new.cachep = cachep;
  3048. on_each_cpu(do_ccupdate_local, (void *)&new, 1, 1);
  3049. check_irq_on();
  3050. cachep->batchcount = batchcount;
  3051. cachep->limit = limit;
  3052. cachep->shared = shared;
  3053. for_each_online_cpu(i) {
  3054. struct array_cache *ccold = new.new[i];
  3055. if (!ccold)
  3056. continue;
  3057. spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
  3058. free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
  3059. spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
  3060. kfree(ccold);
  3061. }
  3062. err = alloc_kmemlist(cachep);
  3063. if (err) {
  3064. printk(KERN_ERR "alloc_kmemlist failed for %s, error %d.\n",
  3065. cachep->name, -err);
  3066. BUG();
  3067. }
  3068. return 0;
  3069. }
  3070. /* Called with cache_chain_mutex held always */
  3071. static void enable_cpucache(struct kmem_cache *cachep)
  3072. {
  3073. int err;
  3074. int limit, shared;
  3075. /*
  3076. * The head array serves three purposes:
  3077. * - create a LIFO ordering, i.e. return objects that are cache-warm
  3078. * - reduce the number of spinlock operations.
  3079. * - reduce the number of linked list operations on the slab and
  3080. * bufctl chains: array operations are cheaper.
  3081. * The numbers are guessed, we should auto-tune as described by
  3082. * Bonwick.
  3083. */
  3084. if (cachep->buffer_size > 131072)
  3085. limit = 1;
  3086. else if (cachep->buffer_size > PAGE_SIZE)
  3087. limit = 8;
  3088. else if (cachep->buffer_size > 1024)
  3089. limit = 24;
  3090. else if (cachep->buffer_size > 256)
  3091. limit = 54;
  3092. else
  3093. limit = 120;
  3094. /*
  3095. * CPU bound tasks (e.g. network routing) can exhibit cpu bound
  3096. * allocation behaviour: Most allocs on one cpu, most free operations
  3097. * on another cpu. For these cases, an efficient object passing between
  3098. * cpus is necessary. This is provided by a shared array. The array
  3099. * replaces Bonwick's magazine layer.
  3100. * On uniprocessor, it's functionally equivalent (but less efficient)
  3101. * to a larger limit. Thus disabled by default.
  3102. */
  3103. shared = 0;
  3104. #ifdef CONFIG_SMP
  3105. if (cachep->buffer_size <= PAGE_SIZE)
  3106. shared = 8;
  3107. #endif
  3108. #if DEBUG
  3109. /*
  3110. * With debugging enabled, large batchcount lead to excessively long
  3111. * periods with disabled local interrupts. Limit the batchcount
  3112. */
  3113. if (limit > 32)
  3114. limit = 32;
  3115. #endif
  3116. err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
  3117. if (err)
  3118. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  3119. cachep->name, -err);
  3120. }
  3121. static void drain_array_locked(struct kmem_cache *cachep,
  3122. struct array_cache *ac, int force, int node)
  3123. {
  3124. int tofree;
  3125. check_spinlock_acquired_node(cachep, node);
  3126. if (ac->touched && !force) {
  3127. ac->touched = 0;
  3128. } else if (ac->avail) {
  3129. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  3130. if (tofree > ac->avail)
  3131. tofree = (ac->avail + 1) / 2;
  3132. free_block(cachep, ac->entry, tofree, node);
  3133. ac->avail -= tofree;
  3134. memmove(ac->entry, &(ac->entry[tofree]),
  3135. sizeof(void *) * ac->avail);
  3136. }
  3137. }
  3138. /**
  3139. * cache_reap - Reclaim memory from caches.
  3140. * @unused: unused parameter
  3141. *
  3142. * Called from workqueue/eventd every few seconds.
  3143. * Purpose:
  3144. * - clear the per-cpu caches for this CPU.
  3145. * - return freeable pages to the main free memory pool.
  3146. *
  3147. * If we cannot acquire the cache chain mutex then just give up - we'll try
  3148. * again on the next iteration.
  3149. */
  3150. static void cache_reap(void *unused)
  3151. {
  3152. struct list_head *walk;
  3153. struct kmem_list3 *l3;
  3154. if (!mutex_trylock(&cache_chain_mutex)) {
  3155. /* Give up. Setup the next iteration. */
  3156. schedule_delayed_work(&__get_cpu_var(reap_work),
  3157. REAPTIMEOUT_CPUC);
  3158. return;
  3159. }
  3160. list_for_each(walk, &cache_chain) {
  3161. struct kmem_cache *searchp;
  3162. struct list_head *p;
  3163. int tofree;
  3164. struct slab *slabp;
  3165. searchp = list_entry(walk, struct kmem_cache, next);
  3166. check_irq_on();
  3167. l3 = searchp->nodelists[numa_node_id()];
  3168. reap_alien(searchp, l3);
  3169. spin_lock_irq(&l3->list_lock);
  3170. drain_array_locked(searchp, cpu_cache_get(searchp), 0,
  3171. numa_node_id());
  3172. if (time_after(l3->next_reap, jiffies))
  3173. goto next_unlock;
  3174. l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
  3175. if (l3->shared)
  3176. drain_array_locked(searchp, l3->shared, 0,
  3177. numa_node_id());
  3178. if (l3->free_touched) {
  3179. l3->free_touched = 0;
  3180. goto next_unlock;
  3181. }
  3182. tofree = (l3->free_limit + 5 * searchp->num - 1) /
  3183. (5 * searchp->num);
  3184. do {
  3185. p = l3->slabs_free.next;
  3186. if (p == &(l3->slabs_free))
  3187. break;
  3188. slabp = list_entry(p, struct slab, list);
  3189. BUG_ON(slabp->inuse);
  3190. list_del(&slabp->list);
  3191. STATS_INC_REAPED(searchp);
  3192. /*
  3193. * Safe to drop the lock. The slab is no longer linked
  3194. * to the cache. searchp cannot disappear, we hold
  3195. * cache_chain_lock
  3196. */
  3197. l3->free_objects -= searchp->num;
  3198. spin_unlock_irq(&l3->list_lock);
  3199. slab_destroy(searchp, slabp);
  3200. spin_lock_irq(&l3->list_lock);
  3201. } while (--tofree > 0);
  3202. next_unlock:
  3203. spin_unlock_irq(&l3->list_lock);
  3204. cond_resched();
  3205. }
  3206. check_irq_on();
  3207. mutex_unlock(&cache_chain_mutex);
  3208. next_reap_node();
  3209. /* Set up the next iteration */
  3210. schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC);
  3211. }
  3212. #ifdef CONFIG_PROC_FS
  3213. static void print_slabinfo_header(struct seq_file *m)
  3214. {
  3215. /*
  3216. * Output format version, so at least we can change it
  3217. * without _too_ many complaints.
  3218. */
  3219. #if STATS
  3220. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  3221. #else
  3222. seq_puts(m, "slabinfo - version: 2.1\n");
  3223. #endif
  3224. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  3225. "<objperslab> <pagesperslab>");
  3226. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  3227. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  3228. #if STATS
  3229. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  3230. "<error> <maxfreeable> <nodeallocs> <remotefrees>");
  3231. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  3232. #endif
  3233. seq_putc(m, '\n');
  3234. }
  3235. static void *s_start(struct seq_file *m, loff_t *pos)
  3236. {
  3237. loff_t n = *pos;
  3238. struct list_head *p;
  3239. mutex_lock(&cache_chain_mutex);
  3240. if (!n)
  3241. print_slabinfo_header(m);
  3242. p = cache_chain.next;
  3243. while (n--) {
  3244. p = p->next;
  3245. if (p == &cache_chain)
  3246. return NULL;
  3247. }
  3248. return list_entry(p, struct kmem_cache, next);
  3249. }
  3250. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  3251. {
  3252. struct kmem_cache *cachep = p;
  3253. ++*pos;
  3254. return cachep->next.next == &cache_chain ?
  3255. NULL : list_entry(cachep->next.next, struct kmem_cache, next);
  3256. }
  3257. static void s_stop(struct seq_file *m, void *p)
  3258. {
  3259. mutex_unlock(&cache_chain_mutex);
  3260. }
  3261. static int s_show(struct seq_file *m, void *p)
  3262. {
  3263. struct kmem_cache *cachep = p;
  3264. struct list_head *q;
  3265. struct slab *slabp;
  3266. unsigned long active_objs;
  3267. unsigned long num_objs;
  3268. unsigned long active_slabs = 0;
  3269. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  3270. const char *name;
  3271. char *error = NULL;
  3272. int node;
  3273. struct kmem_list3 *l3;
  3274. active_objs = 0;
  3275. num_slabs = 0;
  3276. for_each_online_node(node) {
  3277. l3 = cachep->nodelists[node];
  3278. if (!l3)
  3279. continue;
  3280. check_irq_on();
  3281. spin_lock_irq(&l3->list_lock);
  3282. list_for_each(q, &l3->slabs_full) {
  3283. slabp = list_entry(q, struct slab, list);
  3284. if (slabp->inuse != cachep->num && !error)
  3285. error = "slabs_full accounting error";
  3286. active_objs += cachep->num;
  3287. active_slabs++;
  3288. }
  3289. list_for_each(q, &l3->slabs_partial) {
  3290. slabp = list_entry(q, struct slab, list);
  3291. if (slabp->inuse == cachep->num && !error)
  3292. error = "slabs_partial inuse accounting error";
  3293. if (!slabp->inuse && !error)
  3294. error = "slabs_partial/inuse accounting error";
  3295. active_objs += slabp->inuse;
  3296. active_slabs++;
  3297. }
  3298. list_for_each(q, &l3->slabs_free) {
  3299. slabp = list_entry(q, struct slab, list);
  3300. if (slabp->inuse && !error)
  3301. error = "slabs_free/inuse accounting error";
  3302. num_slabs++;
  3303. }
  3304. free_objects += l3->free_objects;
  3305. if (l3->shared)
  3306. shared_avail += l3->shared->avail;
  3307. spin_unlock_irq(&l3->list_lock);
  3308. }
  3309. num_slabs += active_slabs;
  3310. num_objs = num_slabs * cachep->num;
  3311. if (num_objs - active_objs != free_objects && !error)
  3312. error = "free_objects accounting error";
  3313. name = cachep->name;
  3314. if (error)
  3315. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  3316. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  3317. name, active_objs, num_objs, cachep->buffer_size,
  3318. cachep->num, (1 << cachep->gfporder));
  3319. seq_printf(m, " : tunables %4u %4u %4u",
  3320. cachep->limit, cachep->batchcount, cachep->shared);
  3321. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  3322. active_slabs, num_slabs, shared_avail);
  3323. #if STATS
  3324. { /* list3 stats */
  3325. unsigned long high = cachep->high_mark;
  3326. unsigned long allocs = cachep->num_allocations;
  3327. unsigned long grown = cachep->grown;
  3328. unsigned long reaped = cachep->reaped;
  3329. unsigned long errors = cachep->errors;
  3330. unsigned long max_freeable = cachep->max_freeable;
  3331. unsigned long node_allocs = cachep->node_allocs;
  3332. unsigned long node_frees = cachep->node_frees;
  3333. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
  3334. %4lu %4lu %4lu %4lu", allocs, high, grown,
  3335. reaped, errors, max_freeable, node_allocs,
  3336. node_frees);
  3337. }
  3338. /* cpu stats */
  3339. {
  3340. unsigned long allochit = atomic_read(&cachep->allochit);
  3341. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  3342. unsigned long freehit = atomic_read(&cachep->freehit);
  3343. unsigned long freemiss = atomic_read(&cachep->freemiss);
  3344. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  3345. allochit, allocmiss, freehit, freemiss);
  3346. }
  3347. #endif
  3348. seq_putc(m, '\n');
  3349. return 0;
  3350. }
  3351. /*
  3352. * slabinfo_op - iterator that generates /proc/slabinfo
  3353. *
  3354. * Output layout:
  3355. * cache-name
  3356. * num-active-objs
  3357. * total-objs
  3358. * object size
  3359. * num-active-slabs
  3360. * total-slabs
  3361. * num-pages-per-slab
  3362. * + further values on SMP and with statistics enabled
  3363. */
  3364. struct seq_operations slabinfo_op = {
  3365. .start = s_start,
  3366. .next = s_next,
  3367. .stop = s_stop,
  3368. .show = s_show,
  3369. };
  3370. #define MAX_SLABINFO_WRITE 128
  3371. /**
  3372. * slabinfo_write - Tuning for the slab allocator
  3373. * @file: unused
  3374. * @buffer: user buffer
  3375. * @count: data length
  3376. * @ppos: unused
  3377. */
  3378. ssize_t slabinfo_write(struct file *file, const char __user * buffer,
  3379. size_t count, loff_t *ppos)
  3380. {
  3381. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  3382. int limit, batchcount, shared, res;
  3383. struct list_head *p;
  3384. if (count > MAX_SLABINFO_WRITE)
  3385. return -EINVAL;
  3386. if (copy_from_user(&kbuf, buffer, count))
  3387. return -EFAULT;
  3388. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3389. tmp = strchr(kbuf, ' ');
  3390. if (!tmp)
  3391. return -EINVAL;
  3392. *tmp = '\0';
  3393. tmp++;
  3394. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3395. return -EINVAL;
  3396. /* Find the cache in the chain of caches. */
  3397. mutex_lock(&cache_chain_mutex);
  3398. res = -EINVAL;
  3399. list_for_each(p, &cache_chain) {
  3400. struct kmem_cache *cachep;
  3401. cachep = list_entry(p, struct kmem_cache, next);
  3402. if (!strcmp(cachep->name, kbuf)) {
  3403. if (limit < 1 || batchcount < 1 ||
  3404. batchcount > limit || shared < 0) {
  3405. res = 0;
  3406. } else {
  3407. res = do_tune_cpucache(cachep, limit,
  3408. batchcount, shared);
  3409. }
  3410. break;
  3411. }
  3412. }
  3413. mutex_unlock(&cache_chain_mutex);
  3414. if (res >= 0)
  3415. res = count;
  3416. return res;
  3417. }
  3418. #endif
  3419. /**
  3420. * ksize - get the actual amount of memory allocated for a given object
  3421. * @objp: Pointer to the object
  3422. *
  3423. * kmalloc may internally round up allocations and return more memory
  3424. * than requested. ksize() can be used to determine the actual amount of
  3425. * memory allocated. The caller may use this additional memory, even though
  3426. * a smaller amount of memory was initially specified with the kmalloc call.
  3427. * The caller must guarantee that objp points to a valid object previously
  3428. * allocated with either kmalloc() or kmem_cache_alloc(). The object
  3429. * must not be freed during the duration of the call.
  3430. */
  3431. unsigned int ksize(const void *objp)
  3432. {
  3433. if (unlikely(objp == NULL))
  3434. return 0;
  3435. return obj_size(virt_to_cache(objp));
  3436. }