slab.c 96 KB

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