slab.c 101 KB

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