slab.c 94 KB

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