slab.c 95 KB

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