slab.c 94 KB

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