slab.c 98 KB

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