slab.c 95 KB

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