slab.c 94 KB

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