slab.c 100 KB

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