slab.c 94 KB

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