slab.c 94 KB

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