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 must be completely optimized away if
  291. * a constant is passed to it. Mostly the same as
  292. * what is in linux/slab.h except it returns an
  293. * index.
  294. */
  295. static __always_inline int index_of(const size_t size)
  296. {
  297. if (__builtin_constant_p(size)) {
  298. int i = 0;
  299. #define CACHE(x) \
  300. if (size <=x) \
  301. return i; \
  302. else \
  303. i++;
  304. #include "linux/kmalloc_sizes.h"
  305. #undef CACHE
  306. {
  307. extern void __bad_size(void);
  308. __bad_size();
  309. }
  310. } else
  311. BUG();
  312. return 0;
  313. }
  314. #define INDEX_AC index_of(sizeof(struct arraycache_init))
  315. #define INDEX_L3 index_of(sizeof(struct kmem_list3))
  316. static inline void kmem_list3_init(struct kmem_list3 *parent)
  317. {
  318. INIT_LIST_HEAD(&parent->slabs_full);
  319. INIT_LIST_HEAD(&parent->slabs_partial);
  320. INIT_LIST_HEAD(&parent->slabs_free);
  321. parent->shared = NULL;
  322. parent->alien = NULL;
  323. spin_lock_init(&parent->list_lock);
  324. parent->free_objects = 0;
  325. parent->free_touched = 0;
  326. }
  327. #define MAKE_LIST(cachep, listp, slab, nodeid) \
  328. do { \
  329. INIT_LIST_HEAD(listp); \
  330. list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
  331. } while (0)
  332. #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
  333. do { \
  334. MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
  335. MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
  336. MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
  337. } while (0)
  338. /*
  339. * kmem_cache_t
  340. *
  341. * manages a cache.
  342. */
  343. struct kmem_cache_s {
  344. /* 1) per-cpu data, touched during every alloc/free */
  345. struct array_cache *array[NR_CPUS];
  346. unsigned int batchcount;
  347. unsigned int limit;
  348. unsigned int shared;
  349. unsigned int objsize;
  350. /* 2) touched by every alloc & free from the backend */
  351. struct kmem_list3 *nodelists[MAX_NUMNODES];
  352. unsigned int flags; /* constant flags */
  353. unsigned int num; /* # of objs per slab */
  354. spinlock_t spinlock;
  355. /* 3) cache_grow/shrink */
  356. /* order of pgs per slab (2^n) */
  357. unsigned int gfporder;
  358. /* force GFP flags, e.g. GFP_DMA */
  359. unsigned int gfpflags;
  360. size_t colour; /* cache colouring range */
  361. unsigned int colour_off; /* colour offset */
  362. unsigned int colour_next; /* cache colouring */
  363. kmem_cache_t *slabp_cache;
  364. unsigned int slab_size;
  365. unsigned int dflags; /* dynamic flags */
  366. /* constructor func */
  367. void (*ctor)(void *, kmem_cache_t *, unsigned long);
  368. /* de-constructor func */
  369. void (*dtor)(void *, kmem_cache_t *, unsigned long);
  370. /* 4) cache creation/removal */
  371. const char *name;
  372. struct list_head next;
  373. /* 5) statistics */
  374. #if STATS
  375. unsigned long num_active;
  376. unsigned long num_allocations;
  377. unsigned long high_mark;
  378. unsigned long grown;
  379. unsigned long reaped;
  380. unsigned long errors;
  381. unsigned long max_freeable;
  382. unsigned long node_allocs;
  383. unsigned long node_frees;
  384. atomic_t allochit;
  385. atomic_t allocmiss;
  386. atomic_t freehit;
  387. atomic_t freemiss;
  388. #endif
  389. #if DEBUG
  390. int dbghead;
  391. int reallen;
  392. #endif
  393. };
  394. #define CFLGS_OFF_SLAB (0x80000000UL)
  395. #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
  396. #define BATCHREFILL_LIMIT 16
  397. /* Optimization question: fewer reaps means less
  398. * probability for unnessary cpucache drain/refill cycles.
  399. *
  400. * OTHO the cpuarrays can contain lots of objects,
  401. * which could lock up otherwise freeable slabs.
  402. */
  403. #define REAPTIMEOUT_CPUC (2*HZ)
  404. #define REAPTIMEOUT_LIST3 (4*HZ)
  405. #if STATS
  406. #define STATS_INC_ACTIVE(x) ((x)->num_active++)
  407. #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
  408. #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
  409. #define STATS_INC_GROWN(x) ((x)->grown++)
  410. #define STATS_INC_REAPED(x) ((x)->reaped++)
  411. #define STATS_SET_HIGH(x) do { if ((x)->num_active > (x)->high_mark) \
  412. (x)->high_mark = (x)->num_active; \
  413. } while (0)
  414. #define STATS_INC_ERR(x) ((x)->errors++)
  415. #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
  416. #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
  417. #define STATS_SET_FREEABLE(x, i) \
  418. do { if ((x)->max_freeable < i) \
  419. (x)->max_freeable = i; \
  420. } while (0)
  421. #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
  422. #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
  423. #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
  424. #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
  425. #else
  426. #define STATS_INC_ACTIVE(x) do { } while (0)
  427. #define STATS_DEC_ACTIVE(x) do { } while (0)
  428. #define STATS_INC_ALLOCED(x) do { } while (0)
  429. #define STATS_INC_GROWN(x) do { } while (0)
  430. #define STATS_INC_REAPED(x) do { } while (0)
  431. #define STATS_SET_HIGH(x) do { } while (0)
  432. #define STATS_INC_ERR(x) do { } while (0)
  433. #define STATS_INC_NODEALLOCS(x) do { } while (0)
  434. #define STATS_INC_NODEFREES(x) do { } while (0)
  435. #define STATS_SET_FREEABLE(x, i) \
  436. do { } while (0)
  437. #define STATS_INC_ALLOCHIT(x) do { } while (0)
  438. #define STATS_INC_ALLOCMISS(x) do { } while (0)
  439. #define STATS_INC_FREEHIT(x) do { } while (0)
  440. #define STATS_INC_FREEMISS(x) do { } while (0)
  441. #endif
  442. #if DEBUG
  443. /* Magic nums for obj red zoning.
  444. * Placed in the first word before and the first word after an obj.
  445. */
  446. #define RED_INACTIVE 0x5A2CF071UL /* when obj is inactive */
  447. #define RED_ACTIVE 0x170FC2A5UL /* when obj is active */
  448. /* ...and for poisoning */
  449. #define POISON_INUSE 0x5a /* for use-uninitialised poisoning */
  450. #define POISON_FREE 0x6b /* for use-after-free poisoning */
  451. #define POISON_END 0xa5 /* end-byte of poisoning */
  452. /* memory layout of objects:
  453. * 0 : objp
  454. * 0 .. cachep->dbghead - BYTES_PER_WORD - 1: padding. This ensures that
  455. * the end of an object is aligned with the end of the real
  456. * allocation. Catches writes behind the end of the allocation.
  457. * cachep->dbghead - BYTES_PER_WORD .. cachep->dbghead - 1:
  458. * redzone word.
  459. * cachep->dbghead: The real object.
  460. * cachep->objsize - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
  461. * cachep->objsize - 1* BYTES_PER_WORD: last caller address [BYTES_PER_WORD long]
  462. */
  463. static int obj_dbghead(kmem_cache_t *cachep)
  464. {
  465. return cachep->dbghead;
  466. }
  467. static int obj_reallen(kmem_cache_t *cachep)
  468. {
  469. return cachep->reallen;
  470. }
  471. static unsigned long *dbg_redzone1(kmem_cache_t *cachep, void *objp)
  472. {
  473. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  474. return (unsigned long*) (objp+obj_dbghead(cachep)-BYTES_PER_WORD);
  475. }
  476. static unsigned long *dbg_redzone2(kmem_cache_t *cachep, void *objp)
  477. {
  478. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  479. if (cachep->flags & SLAB_STORE_USER)
  480. return (unsigned long*) (objp+cachep->objsize-2*BYTES_PER_WORD);
  481. return (unsigned long*) (objp+cachep->objsize-BYTES_PER_WORD);
  482. }
  483. static void **dbg_userword(kmem_cache_t *cachep, void *objp)
  484. {
  485. BUG_ON(!(cachep->flags & SLAB_STORE_USER));
  486. return (void**)(objp+cachep->objsize-BYTES_PER_WORD);
  487. }
  488. #else
  489. #define obj_dbghead(x) 0
  490. #define obj_reallen(cachep) (cachep->objsize)
  491. #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
  492. #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
  493. #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
  494. #endif
  495. /*
  496. * Maximum size of an obj (in 2^order pages)
  497. * and absolute limit for the gfp order.
  498. */
  499. #if defined(CONFIG_LARGE_ALLOCS)
  500. #define MAX_OBJ_ORDER 13 /* up to 32Mb */
  501. #define MAX_GFP_ORDER 13 /* up to 32Mb */
  502. #elif defined(CONFIG_MMU)
  503. #define MAX_OBJ_ORDER 5 /* 32 pages */
  504. #define MAX_GFP_ORDER 5 /* 32 pages */
  505. #else
  506. #define MAX_OBJ_ORDER 8 /* up to 1Mb */
  507. #define MAX_GFP_ORDER 8 /* up to 1Mb */
  508. #endif
  509. /*
  510. * Do not go above this order unless 0 objects fit into the slab.
  511. */
  512. #define BREAK_GFP_ORDER_HI 1
  513. #define BREAK_GFP_ORDER_LO 0
  514. static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
  515. /* Macros for storing/retrieving the cachep and or slab from the
  516. * global 'mem_map'. These are used to find the slab an obj belongs to.
  517. * With kfree(), these are used to find the cache which an obj belongs to.
  518. */
  519. #define SET_PAGE_CACHE(pg,x) ((pg)->lru.next = (struct list_head *)(x))
  520. #define GET_PAGE_CACHE(pg) ((kmem_cache_t *)(pg)->lru.next)
  521. #define SET_PAGE_SLAB(pg,x) ((pg)->lru.prev = (struct list_head *)(x))
  522. #define GET_PAGE_SLAB(pg) ((struct slab *)(pg)->lru.prev)
  523. /* These are the default caches for kmalloc. Custom caches can have other sizes. */
  524. struct cache_sizes malloc_sizes[] = {
  525. #define CACHE(x) { .cs_size = (x) },
  526. #include <linux/kmalloc_sizes.h>
  527. CACHE(ULONG_MAX)
  528. #undef CACHE
  529. };
  530. EXPORT_SYMBOL(malloc_sizes);
  531. /* Must match cache_sizes above. Out of line to keep cache footprint low. */
  532. struct cache_names {
  533. char *name;
  534. char *name_dma;
  535. };
  536. static struct cache_names __initdata cache_names[] = {
  537. #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
  538. #include <linux/kmalloc_sizes.h>
  539. { NULL, }
  540. #undef CACHE
  541. };
  542. static struct arraycache_init initarray_cache __initdata =
  543. { { 0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  544. static struct arraycache_init initarray_generic =
  545. { { 0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  546. /* internal cache of cache description objs */
  547. static kmem_cache_t cache_cache = {
  548. .batchcount = 1,
  549. .limit = BOOT_CPUCACHE_ENTRIES,
  550. .shared = 1,
  551. .objsize = sizeof(kmem_cache_t),
  552. .flags = SLAB_NO_REAP,
  553. .spinlock = SPIN_LOCK_UNLOCKED,
  554. .name = "kmem_cache",
  555. #if DEBUG
  556. .reallen = sizeof(kmem_cache_t),
  557. #endif
  558. };
  559. /* Guard access to the cache-chain. */
  560. static struct semaphore cache_chain_sem;
  561. static struct list_head cache_chain;
  562. /*
  563. * vm_enough_memory() looks at this to determine how many
  564. * slab-allocated pages are possibly freeable under pressure
  565. *
  566. * SLAB_RECLAIM_ACCOUNT turns this on per-slab
  567. */
  568. atomic_t slab_reclaim_pages;
  569. /*
  570. * chicken and egg problem: delay the per-cpu array allocation
  571. * until the general caches are up.
  572. */
  573. static enum {
  574. NONE,
  575. PARTIAL_AC,
  576. PARTIAL_L3,
  577. FULL
  578. } g_cpucache_up;
  579. static DEFINE_PER_CPU(struct work_struct, reap_work);
  580. static void free_block(kmem_cache_t* cachep, void** objpp, int len, int node);
  581. static void enable_cpucache (kmem_cache_t *cachep);
  582. static void cache_reap (void *unused);
  583. static int __node_shrink(kmem_cache_t *cachep, int node);
  584. static inline struct array_cache *ac_data(kmem_cache_t *cachep)
  585. {
  586. return cachep->array[smp_processor_id()];
  587. }
  588. static inline kmem_cache_t *__find_general_cachep(size_t size,
  589. unsigned int __nocast gfpflags)
  590. {
  591. struct cache_sizes *csizep = malloc_sizes;
  592. #if DEBUG
  593. /* This happens if someone tries to call
  594. * kmem_cache_create(), or __kmalloc(), before
  595. * the generic caches are initialized.
  596. */
  597. BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
  598. #endif
  599. while (size > csizep->cs_size)
  600. csizep++;
  601. /*
  602. * Really subtle: The last entry with cs->cs_size==ULONG_MAX
  603. * has cs_{dma,}cachep==NULL. Thus no special case
  604. * for large kmalloc calls required.
  605. */
  606. if (unlikely(gfpflags & GFP_DMA))
  607. return csizep->cs_dmacachep;
  608. return csizep->cs_cachep;
  609. }
  610. kmem_cache_t *kmem_find_general_cachep(size_t size,
  611. unsigned int __nocast gfpflags)
  612. {
  613. return __find_general_cachep(size, gfpflags);
  614. }
  615. EXPORT_SYMBOL(kmem_find_general_cachep);
  616. /* Cal the num objs, wastage, and bytes left over for a given slab size. */
  617. static void cache_estimate(unsigned long gfporder, size_t size, size_t align,
  618. int flags, size_t *left_over, unsigned int *num)
  619. {
  620. int i;
  621. size_t wastage = PAGE_SIZE<<gfporder;
  622. size_t extra = 0;
  623. size_t base = 0;
  624. if (!(flags & CFLGS_OFF_SLAB)) {
  625. base = sizeof(struct slab);
  626. extra = sizeof(kmem_bufctl_t);
  627. }
  628. i = 0;
  629. while (i*size + ALIGN(base+i*extra, align) <= wastage)
  630. i++;
  631. if (i > 0)
  632. i--;
  633. if (i > SLAB_LIMIT)
  634. i = SLAB_LIMIT;
  635. *num = i;
  636. wastage -= i*size;
  637. wastage -= ALIGN(base+i*extra, align);
  638. *left_over = wastage;
  639. }
  640. #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
  641. static void __slab_error(const char *function, kmem_cache_t *cachep, char *msg)
  642. {
  643. printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
  644. function, cachep->name, msg);
  645. dump_stack();
  646. }
  647. /*
  648. * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
  649. * via the workqueue/eventd.
  650. * Add the CPU number into the expiration time to minimize the possibility of
  651. * the CPUs getting into lockstep and contending for the global cache chain
  652. * lock.
  653. */
  654. static void __devinit start_cpu_timer(int cpu)
  655. {
  656. struct work_struct *reap_work = &per_cpu(reap_work, cpu);
  657. /*
  658. * When this gets called from do_initcalls via cpucache_init(),
  659. * init_workqueues() has already run, so keventd will be setup
  660. * at that time.
  661. */
  662. if (keventd_up() && reap_work->func == NULL) {
  663. INIT_WORK(reap_work, cache_reap, NULL);
  664. schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
  665. }
  666. }
  667. static struct array_cache *alloc_arraycache(int node, int entries,
  668. int batchcount)
  669. {
  670. int memsize = sizeof(void*)*entries+sizeof(struct array_cache);
  671. struct array_cache *nc = NULL;
  672. nc = kmalloc_node(memsize, GFP_KERNEL, node);
  673. if (nc) {
  674. nc->avail = 0;
  675. nc->limit = entries;
  676. nc->batchcount = batchcount;
  677. nc->touched = 0;
  678. spin_lock_init(&nc->lock);
  679. }
  680. return nc;
  681. }
  682. #ifdef CONFIG_NUMA
  683. static inline struct array_cache **alloc_alien_cache(int node, int limit)
  684. {
  685. struct array_cache **ac_ptr;
  686. int memsize = sizeof(void*)*MAX_NUMNODES;
  687. int i;
  688. if (limit > 1)
  689. limit = 12;
  690. ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
  691. if (ac_ptr) {
  692. for_each_node(i) {
  693. if (i == node || !node_online(i)) {
  694. ac_ptr[i] = NULL;
  695. continue;
  696. }
  697. ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
  698. if (!ac_ptr[i]) {
  699. for (i--; i <=0; i--)
  700. kfree(ac_ptr[i]);
  701. kfree(ac_ptr);
  702. return NULL;
  703. }
  704. }
  705. }
  706. return ac_ptr;
  707. }
  708. static inline void free_alien_cache(struct array_cache **ac_ptr)
  709. {
  710. int i;
  711. if (!ac_ptr)
  712. return;
  713. for_each_node(i)
  714. kfree(ac_ptr[i]);
  715. kfree(ac_ptr);
  716. }
  717. static inline void __drain_alien_cache(kmem_cache_t *cachep, struct array_cache *ac, int node)
  718. {
  719. struct kmem_list3 *rl3 = cachep->nodelists[node];
  720. if (ac->avail) {
  721. spin_lock(&rl3->list_lock);
  722. free_block(cachep, ac->entry, ac->avail, node);
  723. ac->avail = 0;
  724. spin_unlock(&rl3->list_lock);
  725. }
  726. }
  727. static void drain_alien_cache(kmem_cache_t *cachep, struct kmem_list3 *l3)
  728. {
  729. int i=0;
  730. struct array_cache *ac;
  731. unsigned long flags;
  732. for_each_online_node(i) {
  733. ac = l3->alien[i];
  734. if (ac) {
  735. spin_lock_irqsave(&ac->lock, flags);
  736. __drain_alien_cache(cachep, ac, i);
  737. spin_unlock_irqrestore(&ac->lock, flags);
  738. }
  739. }
  740. }
  741. #else
  742. #define alloc_alien_cache(node, limit) do { } while (0)
  743. #define free_alien_cache(ac_ptr) do { } while (0)
  744. #define drain_alien_cache(cachep, l3) do { } while (0)
  745. #endif
  746. static int __devinit cpuup_callback(struct notifier_block *nfb,
  747. unsigned long action, void *hcpu)
  748. {
  749. long cpu = (long)hcpu;
  750. kmem_cache_t* cachep;
  751. struct kmem_list3 *l3 = NULL;
  752. int node = cpu_to_node(cpu);
  753. int memsize = sizeof(struct kmem_list3);
  754. struct array_cache *nc = NULL;
  755. switch (action) {
  756. case CPU_UP_PREPARE:
  757. down(&cache_chain_sem);
  758. /* we need to do this right in the beginning since
  759. * alloc_arraycache's are going to use this list.
  760. * kmalloc_node allows us to add the slab to the right
  761. * kmem_list3 and not this cpu's kmem_list3
  762. */
  763. list_for_each_entry(cachep, &cache_chain, next) {
  764. /* setup the size64 kmemlist for cpu before we can
  765. * begin anything. Make sure some other cpu on this
  766. * node has not already allocated this
  767. */
  768. if (!cachep->nodelists[node]) {
  769. if (!(l3 = kmalloc_node(memsize,
  770. GFP_KERNEL, node)))
  771. goto bad;
  772. kmem_list3_init(l3);
  773. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  774. ((unsigned long)cachep)%REAPTIMEOUT_LIST3;
  775. cachep->nodelists[node] = l3;
  776. }
  777. spin_lock_irq(&cachep->nodelists[node]->list_lock);
  778. cachep->nodelists[node]->free_limit =
  779. (1 + nr_cpus_node(node)) *
  780. cachep->batchcount + cachep->num;
  781. spin_unlock_irq(&cachep->nodelists[node]->list_lock);
  782. }
  783. /* Now we can go ahead with allocating the shared array's
  784. & array cache's */
  785. list_for_each_entry(cachep, &cache_chain, next) {
  786. nc = alloc_arraycache(node, cachep->limit,
  787. cachep->batchcount);
  788. if (!nc)
  789. goto bad;
  790. cachep->array[cpu] = nc;
  791. l3 = cachep->nodelists[node];
  792. BUG_ON(!l3);
  793. if (!l3->shared) {
  794. if (!(nc = alloc_arraycache(node,
  795. cachep->shared*cachep->batchcount,
  796. 0xbaadf00d)))
  797. goto bad;
  798. /* we are serialised from CPU_DEAD or
  799. CPU_UP_CANCELLED by the cpucontrol lock */
  800. l3->shared = nc;
  801. }
  802. }
  803. up(&cache_chain_sem);
  804. break;
  805. case CPU_ONLINE:
  806. start_cpu_timer(cpu);
  807. break;
  808. #ifdef CONFIG_HOTPLUG_CPU
  809. case CPU_DEAD:
  810. /* fall thru */
  811. case CPU_UP_CANCELED:
  812. down(&cache_chain_sem);
  813. list_for_each_entry(cachep, &cache_chain, next) {
  814. struct array_cache *nc;
  815. cpumask_t mask;
  816. mask = node_to_cpumask(node);
  817. spin_lock_irq(&cachep->spinlock);
  818. /* cpu is dead; no one can alloc from it. */
  819. nc = cachep->array[cpu];
  820. cachep->array[cpu] = NULL;
  821. l3 = cachep->nodelists[node];
  822. if (!l3)
  823. goto unlock_cache;
  824. spin_lock(&l3->list_lock);
  825. /* Free limit for this kmem_list3 */
  826. l3->free_limit -= cachep->batchcount;
  827. if (nc)
  828. free_block(cachep, nc->entry, nc->avail, node);
  829. if (!cpus_empty(mask)) {
  830. spin_unlock(&l3->list_lock);
  831. goto unlock_cache;
  832. }
  833. if (l3->shared) {
  834. free_block(cachep, l3->shared->entry,
  835. l3->shared->avail, node);
  836. kfree(l3->shared);
  837. l3->shared = NULL;
  838. }
  839. if (l3->alien) {
  840. drain_alien_cache(cachep, l3);
  841. free_alien_cache(l3->alien);
  842. l3->alien = NULL;
  843. }
  844. /* free slabs belonging to this node */
  845. if (__node_shrink(cachep, node)) {
  846. cachep->nodelists[node] = NULL;
  847. spin_unlock(&l3->list_lock);
  848. kfree(l3);
  849. } else {
  850. spin_unlock(&l3->list_lock);
  851. }
  852. unlock_cache:
  853. spin_unlock_irq(&cachep->spinlock);
  854. kfree(nc);
  855. }
  856. up(&cache_chain_sem);
  857. break;
  858. #endif
  859. }
  860. return NOTIFY_OK;
  861. bad:
  862. up(&cache_chain_sem);
  863. return NOTIFY_BAD;
  864. }
  865. static struct notifier_block cpucache_notifier = { &cpuup_callback, NULL, 0 };
  866. /*
  867. * swap the static kmem_list3 with kmalloced memory
  868. */
  869. static void init_list(kmem_cache_t *cachep, struct kmem_list3 *list,
  870. int nodeid)
  871. {
  872. struct kmem_list3 *ptr;
  873. BUG_ON(cachep->nodelists[nodeid] != list);
  874. ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
  875. BUG_ON(!ptr);
  876. local_irq_disable();
  877. memcpy(ptr, list, sizeof(struct kmem_list3));
  878. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  879. cachep->nodelists[nodeid] = ptr;
  880. local_irq_enable();
  881. }
  882. /* Initialisation.
  883. * Called after the gfp() functions have been enabled, and before smp_init().
  884. */
  885. void __init kmem_cache_init(void)
  886. {
  887. size_t left_over;
  888. struct cache_sizes *sizes;
  889. struct cache_names *names;
  890. int i;
  891. for (i = 0; i < NUM_INIT_LISTS; i++) {
  892. kmem_list3_init(&initkmem_list3[i]);
  893. if (i < MAX_NUMNODES)
  894. cache_cache.nodelists[i] = NULL;
  895. }
  896. /*
  897. * Fragmentation resistance on low memory - only use bigger
  898. * page orders on machines with more than 32MB of memory.
  899. */
  900. if (num_physpages > (32 << 20) >> PAGE_SHIFT)
  901. slab_break_gfp_order = BREAK_GFP_ORDER_HI;
  902. /* Bootstrap is tricky, because several objects are allocated
  903. * from caches that do not exist yet:
  904. * 1) initialize the cache_cache cache: it contains the kmem_cache_t
  905. * structures of all caches, except cache_cache itself: cache_cache
  906. * is statically allocated.
  907. * Initially an __init data area is used for the head array and the
  908. * kmem_list3 structures, it's replaced with a kmalloc allocated
  909. * array at the end of the bootstrap.
  910. * 2) Create the first kmalloc cache.
  911. * The kmem_cache_t for the new cache is allocated normally.
  912. * An __init data area is used for the head array.
  913. * 3) Create the remaining kmalloc caches, with minimally sized
  914. * head arrays.
  915. * 4) Replace the __init data head arrays for cache_cache and the first
  916. * kmalloc cache with kmalloc allocated arrays.
  917. * 5) Replace the __init data for kmem_list3 for cache_cache and
  918. * the other cache's with kmalloc allocated memory.
  919. * 6) Resize the head arrays of the kmalloc caches to their final sizes.
  920. */
  921. /* 1) create the cache_cache */
  922. init_MUTEX(&cache_chain_sem);
  923. INIT_LIST_HEAD(&cache_chain);
  924. list_add(&cache_cache.next, &cache_chain);
  925. cache_cache.colour_off = cache_line_size();
  926. cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
  927. cache_cache.nodelists[numa_node_id()] = &initkmem_list3[CACHE_CACHE];
  928. cache_cache.objsize = ALIGN(cache_cache.objsize, cache_line_size());
  929. cache_estimate(0, cache_cache.objsize, cache_line_size(), 0,
  930. &left_over, &cache_cache.num);
  931. if (!cache_cache.num)
  932. BUG();
  933. cache_cache.colour = left_over/cache_cache.colour_off;
  934. cache_cache.colour_next = 0;
  935. cache_cache.slab_size = ALIGN(cache_cache.num*sizeof(kmem_bufctl_t) +
  936. sizeof(struct slab), cache_line_size());
  937. /* 2+3) create the kmalloc caches */
  938. sizes = malloc_sizes;
  939. names = cache_names;
  940. /* Initialize the caches that provide memory for the array cache
  941. * and the kmem_list3 structures first.
  942. * Without this, further allocations will bug
  943. */
  944. sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
  945. sizes[INDEX_AC].cs_size, ARCH_KMALLOC_MINALIGN,
  946. (ARCH_KMALLOC_FLAGS | SLAB_PANIC), NULL, NULL);
  947. if (INDEX_AC != INDEX_L3)
  948. sizes[INDEX_L3].cs_cachep =
  949. kmem_cache_create(names[INDEX_L3].name,
  950. sizes[INDEX_L3].cs_size, ARCH_KMALLOC_MINALIGN,
  951. (ARCH_KMALLOC_FLAGS | SLAB_PANIC), NULL, NULL);
  952. while (sizes->cs_size != ULONG_MAX) {
  953. /*
  954. * For performance, all the general caches are L1 aligned.
  955. * This should be particularly beneficial on SMP boxes, as it
  956. * eliminates "false sharing".
  957. * Note for systems short on memory removing the alignment will
  958. * allow tighter packing of the smaller caches.
  959. */
  960. if(!sizes->cs_cachep)
  961. sizes->cs_cachep = kmem_cache_create(names->name,
  962. sizes->cs_size, ARCH_KMALLOC_MINALIGN,
  963. (ARCH_KMALLOC_FLAGS | SLAB_PANIC), NULL, NULL);
  964. /* Inc off-slab bufctl limit until the ceiling is hit. */
  965. if (!(OFF_SLAB(sizes->cs_cachep))) {
  966. offslab_limit = sizes->cs_size-sizeof(struct slab);
  967. offslab_limit /= sizeof(kmem_bufctl_t);
  968. }
  969. sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
  970. sizes->cs_size, ARCH_KMALLOC_MINALIGN,
  971. (ARCH_KMALLOC_FLAGS | SLAB_CACHE_DMA | SLAB_PANIC),
  972. NULL, NULL);
  973. sizes++;
  974. names++;
  975. }
  976. /* 4) Replace the bootstrap head arrays */
  977. {
  978. void * ptr;
  979. ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  980. local_irq_disable();
  981. BUG_ON(ac_data(&cache_cache) != &initarray_cache.cache);
  982. memcpy(ptr, ac_data(&cache_cache),
  983. sizeof(struct arraycache_init));
  984. cache_cache.array[smp_processor_id()] = ptr;
  985. local_irq_enable();
  986. ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
  987. local_irq_disable();
  988. BUG_ON(ac_data(malloc_sizes[INDEX_AC].cs_cachep)
  989. != &initarray_generic.cache);
  990. memcpy(ptr, ac_data(malloc_sizes[INDEX_AC].cs_cachep),
  991. sizeof(struct arraycache_init));
  992. malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
  993. ptr;
  994. local_irq_enable();
  995. }
  996. /* 5) Replace the bootstrap kmem_list3's */
  997. {
  998. int node;
  999. /* Replace the static kmem_list3 structures for the boot cpu */
  1000. init_list(&cache_cache, &initkmem_list3[CACHE_CACHE],
  1001. numa_node_id());
  1002. for_each_online_node(node) {
  1003. init_list(malloc_sizes[INDEX_AC].cs_cachep,
  1004. &initkmem_list3[SIZE_AC+node], node);
  1005. if (INDEX_AC != INDEX_L3) {
  1006. init_list(malloc_sizes[INDEX_L3].cs_cachep,
  1007. &initkmem_list3[SIZE_L3+node],
  1008. node);
  1009. }
  1010. }
  1011. }
  1012. /* 6) resize the head arrays to their final sizes */
  1013. {
  1014. kmem_cache_t *cachep;
  1015. down(&cache_chain_sem);
  1016. list_for_each_entry(cachep, &cache_chain, next)
  1017. enable_cpucache(cachep);
  1018. up(&cache_chain_sem);
  1019. }
  1020. /* Done! */
  1021. g_cpucache_up = FULL;
  1022. /* Register a cpu startup notifier callback
  1023. * that initializes ac_data for all new cpus
  1024. */
  1025. register_cpu_notifier(&cpucache_notifier);
  1026. /* The reap timers are started later, with a module init call:
  1027. * That part of the kernel is not yet operational.
  1028. */
  1029. }
  1030. static int __init cpucache_init(void)
  1031. {
  1032. int cpu;
  1033. /*
  1034. * Register the timers that return unneeded
  1035. * pages to gfp.
  1036. */
  1037. for_each_online_cpu(cpu)
  1038. start_cpu_timer(cpu);
  1039. return 0;
  1040. }
  1041. __initcall(cpucache_init);
  1042. /*
  1043. * Interface to system's page allocator. No need to hold the cache-lock.
  1044. *
  1045. * If we requested dmaable memory, we will get it. Even if we
  1046. * did not request dmaable memory, we might get it, but that
  1047. * would be relatively rare and ignorable.
  1048. */
  1049. static void *kmem_getpages(kmem_cache_t *cachep, unsigned int __nocast flags, int nodeid)
  1050. {
  1051. struct page *page;
  1052. void *addr;
  1053. int i;
  1054. flags |= cachep->gfpflags;
  1055. if (likely(nodeid == -1)) {
  1056. page = alloc_pages(flags, cachep->gfporder);
  1057. } else {
  1058. page = alloc_pages_node(nodeid, flags, cachep->gfporder);
  1059. }
  1060. if (!page)
  1061. return NULL;
  1062. addr = page_address(page);
  1063. i = (1 << cachep->gfporder);
  1064. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1065. atomic_add(i, &slab_reclaim_pages);
  1066. add_page_state(nr_slab, i);
  1067. while (i--) {
  1068. SetPageSlab(page);
  1069. page++;
  1070. }
  1071. return addr;
  1072. }
  1073. /*
  1074. * Interface to system's page release.
  1075. */
  1076. static void kmem_freepages(kmem_cache_t *cachep, void *addr)
  1077. {
  1078. unsigned long i = (1<<cachep->gfporder);
  1079. struct page *page = virt_to_page(addr);
  1080. const unsigned long nr_freed = i;
  1081. while (i--) {
  1082. if (!TestClearPageSlab(page))
  1083. BUG();
  1084. page++;
  1085. }
  1086. sub_page_state(nr_slab, nr_freed);
  1087. if (current->reclaim_state)
  1088. current->reclaim_state->reclaimed_slab += nr_freed;
  1089. free_pages((unsigned long)addr, cachep->gfporder);
  1090. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1091. atomic_sub(1<<cachep->gfporder, &slab_reclaim_pages);
  1092. }
  1093. static void kmem_rcu_free(struct rcu_head *head)
  1094. {
  1095. struct slab_rcu *slab_rcu = (struct slab_rcu *) head;
  1096. kmem_cache_t *cachep = slab_rcu->cachep;
  1097. kmem_freepages(cachep, slab_rcu->addr);
  1098. if (OFF_SLAB(cachep))
  1099. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1100. }
  1101. #if DEBUG
  1102. #ifdef CONFIG_DEBUG_PAGEALLOC
  1103. static void store_stackinfo(kmem_cache_t *cachep, unsigned long *addr,
  1104. unsigned long caller)
  1105. {
  1106. int size = obj_reallen(cachep);
  1107. addr = (unsigned long *)&((char*)addr)[obj_dbghead(cachep)];
  1108. if (size < 5*sizeof(unsigned long))
  1109. return;
  1110. *addr++=0x12345678;
  1111. *addr++=caller;
  1112. *addr++=smp_processor_id();
  1113. size -= 3*sizeof(unsigned long);
  1114. {
  1115. unsigned long *sptr = &caller;
  1116. unsigned long svalue;
  1117. while (!kstack_end(sptr)) {
  1118. svalue = *sptr++;
  1119. if (kernel_text_address(svalue)) {
  1120. *addr++=svalue;
  1121. size -= sizeof(unsigned long);
  1122. if (size <= sizeof(unsigned long))
  1123. break;
  1124. }
  1125. }
  1126. }
  1127. *addr++=0x87654321;
  1128. }
  1129. #endif
  1130. static void poison_obj(kmem_cache_t *cachep, void *addr, unsigned char val)
  1131. {
  1132. int size = obj_reallen(cachep);
  1133. addr = &((char*)addr)[obj_dbghead(cachep)];
  1134. memset(addr, val, size);
  1135. *(unsigned char *)(addr+size-1) = POISON_END;
  1136. }
  1137. static void dump_line(char *data, int offset, int limit)
  1138. {
  1139. int i;
  1140. printk(KERN_ERR "%03x:", offset);
  1141. for (i=0;i<limit;i++) {
  1142. printk(" %02x", (unsigned char)data[offset+i]);
  1143. }
  1144. printk("\n");
  1145. }
  1146. #endif
  1147. #if DEBUG
  1148. static void print_objinfo(kmem_cache_t *cachep, void *objp, int lines)
  1149. {
  1150. int i, size;
  1151. char *realobj;
  1152. if (cachep->flags & SLAB_RED_ZONE) {
  1153. printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
  1154. *dbg_redzone1(cachep, objp),
  1155. *dbg_redzone2(cachep, objp));
  1156. }
  1157. if (cachep->flags & SLAB_STORE_USER) {
  1158. printk(KERN_ERR "Last user: [<%p>]",
  1159. *dbg_userword(cachep, objp));
  1160. print_symbol("(%s)",
  1161. (unsigned long)*dbg_userword(cachep, objp));
  1162. printk("\n");
  1163. }
  1164. realobj = (char*)objp+obj_dbghead(cachep);
  1165. size = obj_reallen(cachep);
  1166. for (i=0; i<size && lines;i+=16, lines--) {
  1167. int limit;
  1168. limit = 16;
  1169. if (i+limit > size)
  1170. limit = size-i;
  1171. dump_line(realobj, i, limit);
  1172. }
  1173. }
  1174. static void check_poison_obj(kmem_cache_t *cachep, void *objp)
  1175. {
  1176. char *realobj;
  1177. int size, i;
  1178. int lines = 0;
  1179. realobj = (char*)objp+obj_dbghead(cachep);
  1180. size = obj_reallen(cachep);
  1181. for (i=0;i<size;i++) {
  1182. char exp = POISON_FREE;
  1183. if (i == size-1)
  1184. exp = POISON_END;
  1185. if (realobj[i] != exp) {
  1186. int limit;
  1187. /* Mismatch ! */
  1188. /* Print header */
  1189. if (lines == 0) {
  1190. printk(KERN_ERR "Slab corruption: start=%p, len=%d\n",
  1191. realobj, size);
  1192. print_objinfo(cachep, objp, 0);
  1193. }
  1194. /* Hexdump the affected line */
  1195. i = (i/16)*16;
  1196. limit = 16;
  1197. if (i+limit > size)
  1198. limit = size-i;
  1199. dump_line(realobj, i, limit);
  1200. i += 16;
  1201. lines++;
  1202. /* Limit to 5 lines */
  1203. if (lines > 5)
  1204. break;
  1205. }
  1206. }
  1207. if (lines != 0) {
  1208. /* Print some data about the neighboring objects, if they
  1209. * exist:
  1210. */
  1211. struct slab *slabp = GET_PAGE_SLAB(virt_to_page(objp));
  1212. int objnr;
  1213. objnr = (objp-slabp->s_mem)/cachep->objsize;
  1214. if (objnr) {
  1215. objp = slabp->s_mem+(objnr-1)*cachep->objsize;
  1216. realobj = (char*)objp+obj_dbghead(cachep);
  1217. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1218. realobj, size);
  1219. print_objinfo(cachep, objp, 2);
  1220. }
  1221. if (objnr+1 < cachep->num) {
  1222. objp = slabp->s_mem+(objnr+1)*cachep->objsize;
  1223. realobj = (char*)objp+obj_dbghead(cachep);
  1224. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1225. realobj, size);
  1226. print_objinfo(cachep, objp, 2);
  1227. }
  1228. }
  1229. }
  1230. #endif
  1231. /* Destroy all the objs in a slab, and release the mem back to the system.
  1232. * Before calling the slab must have been unlinked from the cache.
  1233. * The cache-lock is not held/needed.
  1234. */
  1235. static void slab_destroy (kmem_cache_t *cachep, struct slab *slabp)
  1236. {
  1237. void *addr = slabp->s_mem - slabp->colouroff;
  1238. #if DEBUG
  1239. int i;
  1240. for (i = 0; i < cachep->num; i++) {
  1241. void *objp = slabp->s_mem + cachep->objsize * i;
  1242. if (cachep->flags & SLAB_POISON) {
  1243. #ifdef CONFIG_DEBUG_PAGEALLOC
  1244. if ((cachep->objsize%PAGE_SIZE)==0 && OFF_SLAB(cachep))
  1245. kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE,1);
  1246. else
  1247. check_poison_obj(cachep, objp);
  1248. #else
  1249. check_poison_obj(cachep, objp);
  1250. #endif
  1251. }
  1252. if (cachep->flags & SLAB_RED_ZONE) {
  1253. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1254. slab_error(cachep, "start of a freed object "
  1255. "was overwritten");
  1256. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1257. slab_error(cachep, "end of a freed object "
  1258. "was overwritten");
  1259. }
  1260. if (cachep->dtor && !(cachep->flags & SLAB_POISON))
  1261. (cachep->dtor)(objp+obj_dbghead(cachep), cachep, 0);
  1262. }
  1263. #else
  1264. if (cachep->dtor) {
  1265. int i;
  1266. for (i = 0; i < cachep->num; i++) {
  1267. void* objp = slabp->s_mem+cachep->objsize*i;
  1268. (cachep->dtor)(objp, cachep, 0);
  1269. }
  1270. }
  1271. #endif
  1272. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1273. struct slab_rcu *slab_rcu;
  1274. slab_rcu = (struct slab_rcu *) slabp;
  1275. slab_rcu->cachep = cachep;
  1276. slab_rcu->addr = addr;
  1277. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1278. } else {
  1279. kmem_freepages(cachep, addr);
  1280. if (OFF_SLAB(cachep))
  1281. kmem_cache_free(cachep->slabp_cache, slabp);
  1282. }
  1283. }
  1284. /* For setting up all the kmem_list3s for cache whose objsize is same
  1285. as size of kmem_list3. */
  1286. static inline void set_up_list3s(kmem_cache_t *cachep, int index)
  1287. {
  1288. int node;
  1289. for_each_online_node(node) {
  1290. cachep->nodelists[node] = &initkmem_list3[index+node];
  1291. cachep->nodelists[node]->next_reap = jiffies +
  1292. REAPTIMEOUT_LIST3 +
  1293. ((unsigned long)cachep)%REAPTIMEOUT_LIST3;
  1294. }
  1295. }
  1296. /**
  1297. * kmem_cache_create - Create a cache.
  1298. * @name: A string which is used in /proc/slabinfo to identify this cache.
  1299. * @size: The size of objects to be created in this cache.
  1300. * @align: The required alignment for the objects.
  1301. * @flags: SLAB flags
  1302. * @ctor: A constructor for the objects.
  1303. * @dtor: A destructor for the objects.
  1304. *
  1305. * Returns a ptr to the cache on success, NULL on failure.
  1306. * Cannot be called within a int, but can be interrupted.
  1307. * The @ctor is run when new pages are allocated by the cache
  1308. * and the @dtor is run before the pages are handed back.
  1309. *
  1310. * @name must be valid until the cache is destroyed. This implies that
  1311. * the module calling this has to destroy the cache before getting
  1312. * unloaded.
  1313. *
  1314. * The flags are
  1315. *
  1316. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  1317. * to catch references to uninitialised memory.
  1318. *
  1319. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  1320. * for buffer overruns.
  1321. *
  1322. * %SLAB_NO_REAP - Don't automatically reap this cache when we're under
  1323. * memory pressure.
  1324. *
  1325. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  1326. * cacheline. This can be beneficial if you're counting cycles as closely
  1327. * as davem.
  1328. */
  1329. kmem_cache_t *
  1330. kmem_cache_create (const char *name, size_t size, size_t align,
  1331. unsigned long flags, void (*ctor)(void*, kmem_cache_t *, unsigned long),
  1332. void (*dtor)(void*, kmem_cache_t *, unsigned long))
  1333. {
  1334. size_t left_over, slab_size, ralign;
  1335. kmem_cache_t *cachep = NULL;
  1336. /*
  1337. * Sanity checks... these are all serious usage bugs.
  1338. */
  1339. if ((!name) ||
  1340. in_interrupt() ||
  1341. (size < BYTES_PER_WORD) ||
  1342. (size > (1<<MAX_OBJ_ORDER)*PAGE_SIZE) ||
  1343. (dtor && !ctor)) {
  1344. printk(KERN_ERR "%s: Early error in slab %s\n",
  1345. __FUNCTION__, name);
  1346. BUG();
  1347. }
  1348. #if DEBUG
  1349. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  1350. if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
  1351. /* No constructor, but inital state check requested */
  1352. printk(KERN_ERR "%s: No con, but init state check "
  1353. "requested - %s\n", __FUNCTION__, name);
  1354. flags &= ~SLAB_DEBUG_INITIAL;
  1355. }
  1356. #if FORCED_DEBUG
  1357. /*
  1358. * Enable redzoning and last user accounting, except for caches with
  1359. * large objects, if the increased size would increase the object size
  1360. * above the next power of two: caches with object sizes just above a
  1361. * power of two have a significant amount of internal fragmentation.
  1362. */
  1363. if ((size < 4096 || fls(size-1) == fls(size-1+3*BYTES_PER_WORD)))
  1364. flags |= SLAB_RED_ZONE|SLAB_STORE_USER;
  1365. if (!(flags & SLAB_DESTROY_BY_RCU))
  1366. flags |= SLAB_POISON;
  1367. #endif
  1368. if (flags & SLAB_DESTROY_BY_RCU)
  1369. BUG_ON(flags & SLAB_POISON);
  1370. #endif
  1371. if (flags & SLAB_DESTROY_BY_RCU)
  1372. BUG_ON(dtor);
  1373. /*
  1374. * Always checks flags, a caller might be expecting debug
  1375. * support which isn't available.
  1376. */
  1377. if (flags & ~CREATE_MASK)
  1378. BUG();
  1379. /* Check that size is in terms of words. This is needed to avoid
  1380. * unaligned accesses for some archs when redzoning is used, and makes
  1381. * sure any on-slab bufctl's are also correctly aligned.
  1382. */
  1383. if (size & (BYTES_PER_WORD-1)) {
  1384. size += (BYTES_PER_WORD-1);
  1385. size &= ~(BYTES_PER_WORD-1);
  1386. }
  1387. /* calculate out the final buffer alignment: */
  1388. /* 1) arch recommendation: can be overridden for debug */
  1389. if (flags & SLAB_HWCACHE_ALIGN) {
  1390. /* Default alignment: as specified by the arch code.
  1391. * Except if an object is really small, then squeeze multiple
  1392. * objects into one cacheline.
  1393. */
  1394. ralign = cache_line_size();
  1395. while (size <= ralign/2)
  1396. ralign /= 2;
  1397. } else {
  1398. ralign = BYTES_PER_WORD;
  1399. }
  1400. /* 2) arch mandated alignment: disables debug if necessary */
  1401. if (ralign < ARCH_SLAB_MINALIGN) {
  1402. ralign = ARCH_SLAB_MINALIGN;
  1403. if (ralign > BYTES_PER_WORD)
  1404. flags &= ~(SLAB_RED_ZONE|SLAB_STORE_USER);
  1405. }
  1406. /* 3) caller mandated alignment: disables debug if necessary */
  1407. if (ralign < align) {
  1408. ralign = align;
  1409. if (ralign > BYTES_PER_WORD)
  1410. flags &= ~(SLAB_RED_ZONE|SLAB_STORE_USER);
  1411. }
  1412. /* 4) Store it. Note that the debug code below can reduce
  1413. * the alignment to BYTES_PER_WORD.
  1414. */
  1415. align = ralign;
  1416. /* Get cache's description obj. */
  1417. cachep = (kmem_cache_t *) kmem_cache_alloc(&cache_cache, SLAB_KERNEL);
  1418. if (!cachep)
  1419. goto opps;
  1420. memset(cachep, 0, sizeof(kmem_cache_t));
  1421. #if DEBUG
  1422. cachep->reallen = size;
  1423. if (flags & SLAB_RED_ZONE) {
  1424. /* redzoning only works with word aligned caches */
  1425. align = BYTES_PER_WORD;
  1426. /* add space for red zone words */
  1427. cachep->dbghead += BYTES_PER_WORD;
  1428. size += 2*BYTES_PER_WORD;
  1429. }
  1430. if (flags & SLAB_STORE_USER) {
  1431. /* user store requires word alignment and
  1432. * one word storage behind the end of the real
  1433. * object.
  1434. */
  1435. align = BYTES_PER_WORD;
  1436. size += BYTES_PER_WORD;
  1437. }
  1438. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  1439. if (size >= malloc_sizes[INDEX_L3+1].cs_size && cachep->reallen > cache_line_size() && size < PAGE_SIZE) {
  1440. cachep->dbghead += PAGE_SIZE - size;
  1441. size = PAGE_SIZE;
  1442. }
  1443. #endif
  1444. #endif
  1445. /* Determine if the slab management is 'on' or 'off' slab. */
  1446. if (size >= (PAGE_SIZE>>3))
  1447. /*
  1448. * Size is large, assume best to place the slab management obj
  1449. * off-slab (should allow better packing of objs).
  1450. */
  1451. flags |= CFLGS_OFF_SLAB;
  1452. size = ALIGN(size, align);
  1453. if ((flags & SLAB_RECLAIM_ACCOUNT) && size <= PAGE_SIZE) {
  1454. /*
  1455. * A VFS-reclaimable slab tends to have most allocations
  1456. * as GFP_NOFS and we really don't want to have to be allocating
  1457. * higher-order pages when we are unable to shrink dcache.
  1458. */
  1459. cachep->gfporder = 0;
  1460. cache_estimate(cachep->gfporder, size, align, flags,
  1461. &left_over, &cachep->num);
  1462. } else {
  1463. /*
  1464. * Calculate size (in pages) of slabs, and the num of objs per
  1465. * slab. This could be made much more intelligent. For now,
  1466. * try to avoid using high page-orders for slabs. When the
  1467. * gfp() funcs are more friendly towards high-order requests,
  1468. * this should be changed.
  1469. */
  1470. do {
  1471. unsigned int break_flag = 0;
  1472. cal_wastage:
  1473. cache_estimate(cachep->gfporder, size, align, flags,
  1474. &left_over, &cachep->num);
  1475. if (break_flag)
  1476. break;
  1477. if (cachep->gfporder >= MAX_GFP_ORDER)
  1478. break;
  1479. if (!cachep->num)
  1480. goto next;
  1481. if (flags & CFLGS_OFF_SLAB &&
  1482. cachep->num > offslab_limit) {
  1483. /* This num of objs will cause problems. */
  1484. cachep->gfporder--;
  1485. break_flag++;
  1486. goto cal_wastage;
  1487. }
  1488. /*
  1489. * Large num of objs is good, but v. large slabs are
  1490. * currently bad for the gfp()s.
  1491. */
  1492. if (cachep->gfporder >= slab_break_gfp_order)
  1493. break;
  1494. if ((left_over*8) <= (PAGE_SIZE<<cachep->gfporder))
  1495. break; /* Acceptable internal fragmentation. */
  1496. next:
  1497. cachep->gfporder++;
  1498. } while (1);
  1499. }
  1500. if (!cachep->num) {
  1501. printk("kmem_cache_create: couldn't create cache %s.\n", name);
  1502. kmem_cache_free(&cache_cache, cachep);
  1503. cachep = NULL;
  1504. goto opps;
  1505. }
  1506. slab_size = ALIGN(cachep->num*sizeof(kmem_bufctl_t)
  1507. + sizeof(struct slab), align);
  1508. /*
  1509. * If the slab has been placed off-slab, and we have enough space then
  1510. * move it on-slab. This is at the expense of any extra colouring.
  1511. */
  1512. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  1513. flags &= ~CFLGS_OFF_SLAB;
  1514. left_over -= slab_size;
  1515. }
  1516. if (flags & CFLGS_OFF_SLAB) {
  1517. /* really off slab. No need for manual alignment */
  1518. slab_size = cachep->num*sizeof(kmem_bufctl_t)+sizeof(struct slab);
  1519. }
  1520. cachep->colour_off = cache_line_size();
  1521. /* Offset must be a multiple of the alignment. */
  1522. if (cachep->colour_off < align)
  1523. cachep->colour_off = align;
  1524. cachep->colour = left_over/cachep->colour_off;
  1525. cachep->slab_size = slab_size;
  1526. cachep->flags = flags;
  1527. cachep->gfpflags = 0;
  1528. if (flags & SLAB_CACHE_DMA)
  1529. cachep->gfpflags |= GFP_DMA;
  1530. spin_lock_init(&cachep->spinlock);
  1531. cachep->objsize = size;
  1532. if (flags & CFLGS_OFF_SLAB)
  1533. cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
  1534. cachep->ctor = ctor;
  1535. cachep->dtor = dtor;
  1536. cachep->name = name;
  1537. /* Don't let CPUs to come and go */
  1538. lock_cpu_hotplug();
  1539. if (g_cpucache_up == FULL) {
  1540. enable_cpucache(cachep);
  1541. } else {
  1542. if (g_cpucache_up == NONE) {
  1543. /* Note: the first kmem_cache_create must create
  1544. * the cache that's used by kmalloc(24), otherwise
  1545. * the creation of further caches will BUG().
  1546. */
  1547. cachep->array[smp_processor_id()] =
  1548. &initarray_generic.cache;
  1549. /* If the cache that's used by
  1550. * kmalloc(sizeof(kmem_list3)) is the first cache,
  1551. * then we need to set up all its list3s, otherwise
  1552. * the creation of further caches will BUG().
  1553. */
  1554. set_up_list3s(cachep, SIZE_AC);
  1555. if (INDEX_AC == INDEX_L3)
  1556. g_cpucache_up = PARTIAL_L3;
  1557. else
  1558. g_cpucache_up = PARTIAL_AC;
  1559. } else {
  1560. cachep->array[smp_processor_id()] =
  1561. kmalloc(sizeof(struct arraycache_init),
  1562. GFP_KERNEL);
  1563. if (g_cpucache_up == PARTIAL_AC) {
  1564. set_up_list3s(cachep, SIZE_L3);
  1565. g_cpucache_up = PARTIAL_L3;
  1566. } else {
  1567. int node;
  1568. for_each_online_node(node) {
  1569. cachep->nodelists[node] =
  1570. kmalloc_node(sizeof(struct kmem_list3),
  1571. GFP_KERNEL, node);
  1572. BUG_ON(!cachep->nodelists[node]);
  1573. kmem_list3_init(cachep->nodelists[node]);
  1574. }
  1575. }
  1576. }
  1577. cachep->nodelists[numa_node_id()]->next_reap =
  1578. jiffies + REAPTIMEOUT_LIST3 +
  1579. ((unsigned long)cachep)%REAPTIMEOUT_LIST3;
  1580. BUG_ON(!ac_data(cachep));
  1581. ac_data(cachep)->avail = 0;
  1582. ac_data(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1583. ac_data(cachep)->batchcount = 1;
  1584. ac_data(cachep)->touched = 0;
  1585. cachep->batchcount = 1;
  1586. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1587. }
  1588. /* Need the semaphore to access the chain. */
  1589. down(&cache_chain_sem);
  1590. {
  1591. struct list_head *p;
  1592. mm_segment_t old_fs;
  1593. old_fs = get_fs();
  1594. set_fs(KERNEL_DS);
  1595. list_for_each(p, &cache_chain) {
  1596. kmem_cache_t *pc = list_entry(p, kmem_cache_t, next);
  1597. char tmp;
  1598. /* This happens when the module gets unloaded and doesn't
  1599. destroy its slab cache and noone else reuses the vmalloc
  1600. area of the module. Print a warning. */
  1601. if (__get_user(tmp,pc->name)) {
  1602. printk("SLAB: cache with size %d has lost its name\n",
  1603. pc->objsize);
  1604. continue;
  1605. }
  1606. if (!strcmp(pc->name,name)) {
  1607. printk("kmem_cache_create: duplicate cache %s\n",name);
  1608. up(&cache_chain_sem);
  1609. unlock_cpu_hotplug();
  1610. BUG();
  1611. }
  1612. }
  1613. set_fs(old_fs);
  1614. }
  1615. /* cache setup completed, link it into the list */
  1616. list_add(&cachep->next, &cache_chain);
  1617. up(&cache_chain_sem);
  1618. unlock_cpu_hotplug();
  1619. opps:
  1620. if (!cachep && (flags & SLAB_PANIC))
  1621. panic("kmem_cache_create(): failed to create slab `%s'\n",
  1622. name);
  1623. return cachep;
  1624. }
  1625. EXPORT_SYMBOL(kmem_cache_create);
  1626. #if DEBUG
  1627. static void check_irq_off(void)
  1628. {
  1629. BUG_ON(!irqs_disabled());
  1630. }
  1631. static void check_irq_on(void)
  1632. {
  1633. BUG_ON(irqs_disabled());
  1634. }
  1635. static void check_spinlock_acquired(kmem_cache_t *cachep)
  1636. {
  1637. #ifdef CONFIG_SMP
  1638. check_irq_off();
  1639. assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
  1640. #endif
  1641. }
  1642. static inline void check_spinlock_acquired_node(kmem_cache_t *cachep, int node)
  1643. {
  1644. #ifdef CONFIG_SMP
  1645. check_irq_off();
  1646. assert_spin_locked(&cachep->nodelists[node]->list_lock);
  1647. #endif
  1648. }
  1649. #else
  1650. #define check_irq_off() do { } while(0)
  1651. #define check_irq_on() do { } while(0)
  1652. #define check_spinlock_acquired(x) do { } while(0)
  1653. #define check_spinlock_acquired_node(x, y) do { } while(0)
  1654. #endif
  1655. /*
  1656. * Waits for all CPUs to execute func().
  1657. */
  1658. static void smp_call_function_all_cpus(void (*func) (void *arg), void *arg)
  1659. {
  1660. check_irq_on();
  1661. preempt_disable();
  1662. local_irq_disable();
  1663. func(arg);
  1664. local_irq_enable();
  1665. if (smp_call_function(func, arg, 1, 1))
  1666. BUG();
  1667. preempt_enable();
  1668. }
  1669. static void drain_array_locked(kmem_cache_t* cachep,
  1670. struct array_cache *ac, int force, int node);
  1671. static void do_drain(void *arg)
  1672. {
  1673. kmem_cache_t *cachep = (kmem_cache_t*)arg;
  1674. struct array_cache *ac;
  1675. int node = numa_node_id();
  1676. check_irq_off();
  1677. ac = ac_data(cachep);
  1678. spin_lock(&cachep->nodelists[node]->list_lock);
  1679. free_block(cachep, ac->entry, ac->avail, node);
  1680. spin_unlock(&cachep->nodelists[node]->list_lock);
  1681. ac->avail = 0;
  1682. }
  1683. static void drain_cpu_caches(kmem_cache_t *cachep)
  1684. {
  1685. struct kmem_list3 *l3;
  1686. int node;
  1687. smp_call_function_all_cpus(do_drain, cachep);
  1688. check_irq_on();
  1689. spin_lock_irq(&cachep->spinlock);
  1690. for_each_online_node(node) {
  1691. l3 = cachep->nodelists[node];
  1692. if (l3) {
  1693. spin_lock(&l3->list_lock);
  1694. drain_array_locked(cachep, l3->shared, 1, node);
  1695. spin_unlock(&l3->list_lock);
  1696. if (l3->alien)
  1697. drain_alien_cache(cachep, l3);
  1698. }
  1699. }
  1700. spin_unlock_irq(&cachep->spinlock);
  1701. }
  1702. static int __node_shrink(kmem_cache_t *cachep, int node)
  1703. {
  1704. struct slab *slabp;
  1705. struct kmem_list3 *l3 = cachep->nodelists[node];
  1706. int ret;
  1707. for (;;) {
  1708. struct list_head *p;
  1709. p = l3->slabs_free.prev;
  1710. if (p == &l3->slabs_free)
  1711. break;
  1712. slabp = list_entry(l3->slabs_free.prev, struct slab, list);
  1713. #if DEBUG
  1714. if (slabp->inuse)
  1715. BUG();
  1716. #endif
  1717. list_del(&slabp->list);
  1718. l3->free_objects -= cachep->num;
  1719. spin_unlock_irq(&l3->list_lock);
  1720. slab_destroy(cachep, slabp);
  1721. spin_lock_irq(&l3->list_lock);
  1722. }
  1723. ret = !list_empty(&l3->slabs_full) ||
  1724. !list_empty(&l3->slabs_partial);
  1725. return ret;
  1726. }
  1727. static int __cache_shrink(kmem_cache_t *cachep)
  1728. {
  1729. int ret = 0, i = 0;
  1730. struct kmem_list3 *l3;
  1731. drain_cpu_caches(cachep);
  1732. check_irq_on();
  1733. for_each_online_node(i) {
  1734. l3 = cachep->nodelists[i];
  1735. if (l3) {
  1736. spin_lock_irq(&l3->list_lock);
  1737. ret += __node_shrink(cachep, i);
  1738. spin_unlock_irq(&l3->list_lock);
  1739. }
  1740. }
  1741. return (ret ? 1 : 0);
  1742. }
  1743. /**
  1744. * kmem_cache_shrink - Shrink a cache.
  1745. * @cachep: The cache to shrink.
  1746. *
  1747. * Releases as many slabs as possible for a cache.
  1748. * To help debugging, a zero exit status indicates all slabs were released.
  1749. */
  1750. int kmem_cache_shrink(kmem_cache_t *cachep)
  1751. {
  1752. if (!cachep || in_interrupt())
  1753. BUG();
  1754. return __cache_shrink(cachep);
  1755. }
  1756. EXPORT_SYMBOL(kmem_cache_shrink);
  1757. /**
  1758. * kmem_cache_destroy - delete a cache
  1759. * @cachep: the cache to destroy
  1760. *
  1761. * Remove a kmem_cache_t object from the slab cache.
  1762. * Returns 0 on success.
  1763. *
  1764. * It is expected this function will be called by a module when it is
  1765. * unloaded. This will remove the cache completely, and avoid a duplicate
  1766. * cache being allocated each time a module is loaded and unloaded, if the
  1767. * module doesn't have persistent in-kernel storage across loads and unloads.
  1768. *
  1769. * The cache must be empty before calling this function.
  1770. *
  1771. * The caller must guarantee that noone will allocate memory from the cache
  1772. * during the kmem_cache_destroy().
  1773. */
  1774. int kmem_cache_destroy(kmem_cache_t * cachep)
  1775. {
  1776. int i;
  1777. struct kmem_list3 *l3;
  1778. if (!cachep || in_interrupt())
  1779. BUG();
  1780. /* Don't let CPUs to come and go */
  1781. lock_cpu_hotplug();
  1782. /* Find the cache in the chain of caches. */
  1783. down(&cache_chain_sem);
  1784. /*
  1785. * the chain is never empty, cache_cache is never destroyed
  1786. */
  1787. list_del(&cachep->next);
  1788. up(&cache_chain_sem);
  1789. if (__cache_shrink(cachep)) {
  1790. slab_error(cachep, "Can't free all objects");
  1791. down(&cache_chain_sem);
  1792. list_add(&cachep->next,&cache_chain);
  1793. up(&cache_chain_sem);
  1794. unlock_cpu_hotplug();
  1795. return 1;
  1796. }
  1797. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
  1798. synchronize_rcu();
  1799. for_each_online_cpu(i)
  1800. kfree(cachep->array[i]);
  1801. /* NUMA: free the list3 structures */
  1802. for_each_online_node(i) {
  1803. if ((l3 = cachep->nodelists[i])) {
  1804. kfree(l3->shared);
  1805. free_alien_cache(l3->alien);
  1806. kfree(l3);
  1807. }
  1808. }
  1809. kmem_cache_free(&cache_cache, cachep);
  1810. unlock_cpu_hotplug();
  1811. return 0;
  1812. }
  1813. EXPORT_SYMBOL(kmem_cache_destroy);
  1814. /* Get the memory for a slab management obj. */
  1815. static struct slab* alloc_slabmgmt(kmem_cache_t *cachep, void *objp,
  1816. int colour_off, unsigned int __nocast local_flags)
  1817. {
  1818. struct slab *slabp;
  1819. if (OFF_SLAB(cachep)) {
  1820. /* Slab management obj is off-slab. */
  1821. slabp = kmem_cache_alloc(cachep->slabp_cache, local_flags);
  1822. if (!slabp)
  1823. return NULL;
  1824. } else {
  1825. slabp = objp+colour_off;
  1826. colour_off += cachep->slab_size;
  1827. }
  1828. slabp->inuse = 0;
  1829. slabp->colouroff = colour_off;
  1830. slabp->s_mem = objp+colour_off;
  1831. return slabp;
  1832. }
  1833. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  1834. {
  1835. return (kmem_bufctl_t *)(slabp+1);
  1836. }
  1837. static void cache_init_objs(kmem_cache_t *cachep,
  1838. struct slab *slabp, unsigned long ctor_flags)
  1839. {
  1840. int i;
  1841. for (i = 0; i < cachep->num; i++) {
  1842. void *objp = slabp->s_mem+cachep->objsize*i;
  1843. #if DEBUG
  1844. /* need to poison the objs? */
  1845. if (cachep->flags & SLAB_POISON)
  1846. poison_obj(cachep, objp, POISON_FREE);
  1847. if (cachep->flags & SLAB_STORE_USER)
  1848. *dbg_userword(cachep, objp) = NULL;
  1849. if (cachep->flags & SLAB_RED_ZONE) {
  1850. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  1851. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  1852. }
  1853. /*
  1854. * Constructors are not allowed to allocate memory from
  1855. * the same cache which they are a constructor for.
  1856. * Otherwise, deadlock. They must also be threaded.
  1857. */
  1858. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  1859. cachep->ctor(objp+obj_dbghead(cachep), cachep, ctor_flags);
  1860. if (cachep->flags & SLAB_RED_ZONE) {
  1861. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1862. slab_error(cachep, "constructor overwrote the"
  1863. " end of an object");
  1864. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1865. slab_error(cachep, "constructor overwrote the"
  1866. " start of an object");
  1867. }
  1868. if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  1869. kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 0);
  1870. #else
  1871. if (cachep->ctor)
  1872. cachep->ctor(objp, cachep, ctor_flags);
  1873. #endif
  1874. slab_bufctl(slabp)[i] = i+1;
  1875. }
  1876. slab_bufctl(slabp)[i-1] = BUFCTL_END;
  1877. slabp->free = 0;
  1878. }
  1879. static void kmem_flagcheck(kmem_cache_t *cachep, unsigned int flags)
  1880. {
  1881. if (flags & SLAB_DMA) {
  1882. if (!(cachep->gfpflags & GFP_DMA))
  1883. BUG();
  1884. } else {
  1885. if (cachep->gfpflags & GFP_DMA)
  1886. BUG();
  1887. }
  1888. }
  1889. static void set_slab_attr(kmem_cache_t *cachep, struct slab *slabp, void *objp)
  1890. {
  1891. int i;
  1892. struct page *page;
  1893. /* Nasty!!!!!! I hope this is OK. */
  1894. i = 1 << cachep->gfporder;
  1895. page = virt_to_page(objp);
  1896. do {
  1897. SET_PAGE_CACHE(page, cachep);
  1898. SET_PAGE_SLAB(page, slabp);
  1899. page++;
  1900. } while (--i);
  1901. }
  1902. /*
  1903. * Grow (by 1) the number of slabs within a cache. This is called by
  1904. * kmem_cache_alloc() when there are no active objs left in a cache.
  1905. */
  1906. static int cache_grow(kmem_cache_t *cachep, unsigned int __nocast flags, int nodeid)
  1907. {
  1908. struct slab *slabp;
  1909. void *objp;
  1910. size_t offset;
  1911. unsigned int local_flags;
  1912. unsigned long ctor_flags;
  1913. struct kmem_list3 *l3;
  1914. /* Be lazy and only check for valid flags here,
  1915. * keeping it out of the critical path in kmem_cache_alloc().
  1916. */
  1917. if (flags & ~(SLAB_DMA|SLAB_LEVEL_MASK|SLAB_NO_GROW))
  1918. BUG();
  1919. if (flags & SLAB_NO_GROW)
  1920. return 0;
  1921. ctor_flags = SLAB_CTOR_CONSTRUCTOR;
  1922. local_flags = (flags & SLAB_LEVEL_MASK);
  1923. if (!(local_flags & __GFP_WAIT))
  1924. /*
  1925. * Not allowed to sleep. Need to tell a constructor about
  1926. * this - it might need to know...
  1927. */
  1928. ctor_flags |= SLAB_CTOR_ATOMIC;
  1929. /* About to mess with non-constant members - lock. */
  1930. check_irq_off();
  1931. spin_lock(&cachep->spinlock);
  1932. /* Get colour for the slab, and cal the next value. */
  1933. offset = cachep->colour_next;
  1934. cachep->colour_next++;
  1935. if (cachep->colour_next >= cachep->colour)
  1936. cachep->colour_next = 0;
  1937. offset *= cachep->colour_off;
  1938. spin_unlock(&cachep->spinlock);
  1939. check_irq_off();
  1940. if (local_flags & __GFP_WAIT)
  1941. local_irq_enable();
  1942. /*
  1943. * The test for missing atomic flag is performed here, rather than
  1944. * the more obvious place, simply to reduce the critical path length
  1945. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  1946. * will eventually be caught here (where it matters).
  1947. */
  1948. kmem_flagcheck(cachep, flags);
  1949. /* Get mem for the objs.
  1950. * Attempt to allocate a physical page from 'nodeid',
  1951. */
  1952. if (!(objp = kmem_getpages(cachep, flags, nodeid)))
  1953. goto failed;
  1954. /* Get slab management. */
  1955. if (!(slabp = alloc_slabmgmt(cachep, objp, offset, local_flags)))
  1956. goto opps1;
  1957. slabp->nodeid = nodeid;
  1958. set_slab_attr(cachep, slabp, objp);
  1959. cache_init_objs(cachep, slabp, ctor_flags);
  1960. if (local_flags & __GFP_WAIT)
  1961. local_irq_disable();
  1962. check_irq_off();
  1963. l3 = cachep->nodelists[nodeid];
  1964. spin_lock(&l3->list_lock);
  1965. /* Make slab active. */
  1966. list_add_tail(&slabp->list, &(l3->slabs_free));
  1967. STATS_INC_GROWN(cachep);
  1968. l3->free_objects += cachep->num;
  1969. spin_unlock(&l3->list_lock);
  1970. return 1;
  1971. opps1:
  1972. kmem_freepages(cachep, objp);
  1973. failed:
  1974. if (local_flags & __GFP_WAIT)
  1975. local_irq_disable();
  1976. return 0;
  1977. }
  1978. #if DEBUG
  1979. /*
  1980. * Perform extra freeing checks:
  1981. * - detect bad pointers.
  1982. * - POISON/RED_ZONE checking
  1983. * - destructor calls, for caches with POISON+dtor
  1984. */
  1985. static void kfree_debugcheck(const void *objp)
  1986. {
  1987. struct page *page;
  1988. if (!virt_addr_valid(objp)) {
  1989. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  1990. (unsigned long)objp);
  1991. BUG();
  1992. }
  1993. page = virt_to_page(objp);
  1994. if (!PageSlab(page)) {
  1995. printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n", (unsigned long)objp);
  1996. BUG();
  1997. }
  1998. }
  1999. static void *cache_free_debugcheck(kmem_cache_t *cachep, void *objp,
  2000. void *caller)
  2001. {
  2002. struct page *page;
  2003. unsigned int objnr;
  2004. struct slab *slabp;
  2005. objp -= obj_dbghead(cachep);
  2006. kfree_debugcheck(objp);
  2007. page = virt_to_page(objp);
  2008. if (GET_PAGE_CACHE(page) != cachep) {
  2009. printk(KERN_ERR "mismatch in kmem_cache_free: expected cache %p, got %p\n",
  2010. GET_PAGE_CACHE(page),cachep);
  2011. printk(KERN_ERR "%p is %s.\n", cachep, cachep->name);
  2012. printk(KERN_ERR "%p is %s.\n", GET_PAGE_CACHE(page), GET_PAGE_CACHE(page)->name);
  2013. WARN_ON(1);
  2014. }
  2015. slabp = GET_PAGE_SLAB(page);
  2016. if (cachep->flags & SLAB_RED_ZONE) {
  2017. if (*dbg_redzone1(cachep, objp) != RED_ACTIVE || *dbg_redzone2(cachep, objp) != RED_ACTIVE) {
  2018. slab_error(cachep, "double free, or memory outside"
  2019. " object was overwritten");
  2020. printk(KERN_ERR "%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
  2021. objp, *dbg_redzone1(cachep, objp), *dbg_redzone2(cachep, objp));
  2022. }
  2023. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2024. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2025. }
  2026. if (cachep->flags & SLAB_STORE_USER)
  2027. *dbg_userword(cachep, objp) = caller;
  2028. objnr = (objp-slabp->s_mem)/cachep->objsize;
  2029. BUG_ON(objnr >= cachep->num);
  2030. BUG_ON(objp != slabp->s_mem + objnr*cachep->objsize);
  2031. if (cachep->flags & SLAB_DEBUG_INITIAL) {
  2032. /* Need to call the slab's constructor so the
  2033. * caller can perform a verify of its state (debugging).
  2034. * Called without the cache-lock held.
  2035. */
  2036. cachep->ctor(objp+obj_dbghead(cachep),
  2037. cachep, SLAB_CTOR_CONSTRUCTOR|SLAB_CTOR_VERIFY);
  2038. }
  2039. if (cachep->flags & SLAB_POISON && cachep->dtor) {
  2040. /* we want to cache poison the object,
  2041. * call the destruction callback
  2042. */
  2043. cachep->dtor(objp+obj_dbghead(cachep), cachep, 0);
  2044. }
  2045. if (cachep->flags & SLAB_POISON) {
  2046. #ifdef CONFIG_DEBUG_PAGEALLOC
  2047. if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep)) {
  2048. store_stackinfo(cachep, objp, (unsigned long)caller);
  2049. kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 0);
  2050. } else {
  2051. poison_obj(cachep, objp, POISON_FREE);
  2052. }
  2053. #else
  2054. poison_obj(cachep, objp, POISON_FREE);
  2055. #endif
  2056. }
  2057. return objp;
  2058. }
  2059. static void check_slabp(kmem_cache_t *cachep, struct slab *slabp)
  2060. {
  2061. kmem_bufctl_t i;
  2062. int entries = 0;
  2063. /* Check slab's freelist to see if this obj is there. */
  2064. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2065. entries++;
  2066. if (entries > cachep->num || i >= cachep->num)
  2067. goto bad;
  2068. }
  2069. if (entries != cachep->num - slabp->inuse) {
  2070. bad:
  2071. printk(KERN_ERR "slab: Internal list corruption detected in cache '%s'(%d), slabp %p(%d). Hexdump:\n",
  2072. cachep->name, cachep->num, slabp, slabp->inuse);
  2073. for (i=0;i<sizeof(slabp)+cachep->num*sizeof(kmem_bufctl_t);i++) {
  2074. if ((i%16)==0)
  2075. printk("\n%03x:", i);
  2076. printk(" %02x", ((unsigned char*)slabp)[i]);
  2077. }
  2078. printk("\n");
  2079. BUG();
  2080. }
  2081. }
  2082. #else
  2083. #define kfree_debugcheck(x) do { } while(0)
  2084. #define cache_free_debugcheck(x,objp,z) (objp)
  2085. #define check_slabp(x,y) do { } while(0)
  2086. #endif
  2087. static void *cache_alloc_refill(kmem_cache_t *cachep, unsigned int __nocast flags)
  2088. {
  2089. int batchcount;
  2090. struct kmem_list3 *l3;
  2091. struct array_cache *ac;
  2092. check_irq_off();
  2093. ac = ac_data(cachep);
  2094. retry:
  2095. batchcount = ac->batchcount;
  2096. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2097. /* if there was little recent activity on this
  2098. * cache, then perform only a partial refill.
  2099. * Otherwise we could generate refill bouncing.
  2100. */
  2101. batchcount = BATCHREFILL_LIMIT;
  2102. }
  2103. l3 = cachep->nodelists[numa_node_id()];
  2104. BUG_ON(ac->avail > 0 || !l3);
  2105. spin_lock(&l3->list_lock);
  2106. if (l3->shared) {
  2107. struct array_cache *shared_array = l3->shared;
  2108. if (shared_array->avail) {
  2109. if (batchcount > shared_array->avail)
  2110. batchcount = shared_array->avail;
  2111. shared_array->avail -= batchcount;
  2112. ac->avail = batchcount;
  2113. memcpy(ac->entry,
  2114. &(shared_array->entry[shared_array->avail]),
  2115. sizeof(void*)*batchcount);
  2116. shared_array->touched = 1;
  2117. goto alloc_done;
  2118. }
  2119. }
  2120. while (batchcount > 0) {
  2121. struct list_head *entry;
  2122. struct slab *slabp;
  2123. /* Get slab alloc is to come from. */
  2124. entry = l3->slabs_partial.next;
  2125. if (entry == &l3->slabs_partial) {
  2126. l3->free_touched = 1;
  2127. entry = l3->slabs_free.next;
  2128. if (entry == &l3->slabs_free)
  2129. goto must_grow;
  2130. }
  2131. slabp = list_entry(entry, struct slab, list);
  2132. check_slabp(cachep, slabp);
  2133. check_spinlock_acquired(cachep);
  2134. while (slabp->inuse < cachep->num && batchcount--) {
  2135. kmem_bufctl_t next;
  2136. STATS_INC_ALLOCED(cachep);
  2137. STATS_INC_ACTIVE(cachep);
  2138. STATS_SET_HIGH(cachep);
  2139. /* get obj pointer */
  2140. ac->entry[ac->avail++] = slabp->s_mem +
  2141. slabp->free*cachep->objsize;
  2142. slabp->inuse++;
  2143. next = slab_bufctl(slabp)[slabp->free];
  2144. #if DEBUG
  2145. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2146. #endif
  2147. slabp->free = next;
  2148. }
  2149. check_slabp(cachep, slabp);
  2150. /* move slabp to correct slabp list: */
  2151. list_del(&slabp->list);
  2152. if (slabp->free == BUFCTL_END)
  2153. list_add(&slabp->list, &l3->slabs_full);
  2154. else
  2155. list_add(&slabp->list, &l3->slabs_partial);
  2156. }
  2157. must_grow:
  2158. l3->free_objects -= ac->avail;
  2159. alloc_done:
  2160. spin_unlock(&l3->list_lock);
  2161. if (unlikely(!ac->avail)) {
  2162. int x;
  2163. x = cache_grow(cachep, flags, numa_node_id());
  2164. // cache_grow can reenable interrupts, then ac could change.
  2165. ac = ac_data(cachep);
  2166. if (!x && ac->avail == 0) // no objects in sight? abort
  2167. return NULL;
  2168. if (!ac->avail) // objects refilled by interrupt?
  2169. goto retry;
  2170. }
  2171. ac->touched = 1;
  2172. return ac->entry[--ac->avail];
  2173. }
  2174. static inline void
  2175. cache_alloc_debugcheck_before(kmem_cache_t *cachep, unsigned int __nocast flags)
  2176. {
  2177. might_sleep_if(flags & __GFP_WAIT);
  2178. #if DEBUG
  2179. kmem_flagcheck(cachep, flags);
  2180. #endif
  2181. }
  2182. #if DEBUG
  2183. static void *
  2184. cache_alloc_debugcheck_after(kmem_cache_t *cachep,
  2185. unsigned int __nocast flags, void *objp, void *caller)
  2186. {
  2187. if (!objp)
  2188. return objp;
  2189. if (cachep->flags & SLAB_POISON) {
  2190. #ifdef CONFIG_DEBUG_PAGEALLOC
  2191. if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2192. kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 1);
  2193. else
  2194. check_poison_obj(cachep, objp);
  2195. #else
  2196. check_poison_obj(cachep, objp);
  2197. #endif
  2198. poison_obj(cachep, objp, POISON_INUSE);
  2199. }
  2200. if (cachep->flags & SLAB_STORE_USER)
  2201. *dbg_userword(cachep, objp) = caller;
  2202. if (cachep->flags & SLAB_RED_ZONE) {
  2203. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE || *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2204. slab_error(cachep, "double free, or memory outside"
  2205. " object was overwritten");
  2206. printk(KERN_ERR "%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
  2207. objp, *dbg_redzone1(cachep, objp), *dbg_redzone2(cachep, objp));
  2208. }
  2209. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2210. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2211. }
  2212. objp += obj_dbghead(cachep);
  2213. if (cachep->ctor && cachep->flags & SLAB_POISON) {
  2214. unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
  2215. if (!(flags & __GFP_WAIT))
  2216. ctor_flags |= SLAB_CTOR_ATOMIC;
  2217. cachep->ctor(objp, cachep, ctor_flags);
  2218. }
  2219. return objp;
  2220. }
  2221. #else
  2222. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2223. #endif
  2224. static inline void *__cache_alloc(kmem_cache_t *cachep, unsigned int __nocast flags)
  2225. {
  2226. unsigned long save_flags;
  2227. void* objp;
  2228. struct array_cache *ac;
  2229. cache_alloc_debugcheck_before(cachep, flags);
  2230. local_irq_save(save_flags);
  2231. ac = ac_data(cachep);
  2232. if (likely(ac->avail)) {
  2233. STATS_INC_ALLOCHIT(cachep);
  2234. ac->touched = 1;
  2235. objp = ac->entry[--ac->avail];
  2236. } else {
  2237. STATS_INC_ALLOCMISS(cachep);
  2238. objp = cache_alloc_refill(cachep, flags);
  2239. }
  2240. local_irq_restore(save_flags);
  2241. objp = cache_alloc_debugcheck_after(cachep, flags, objp,
  2242. __builtin_return_address(0));
  2243. prefetchw(objp);
  2244. return objp;
  2245. }
  2246. #ifdef CONFIG_NUMA
  2247. /*
  2248. * A interface to enable slab creation on nodeid
  2249. */
  2250. static void *__cache_alloc_node(kmem_cache_t *cachep, int flags, int nodeid)
  2251. {
  2252. struct list_head *entry;
  2253. struct slab *slabp;
  2254. struct kmem_list3 *l3;
  2255. void *obj;
  2256. kmem_bufctl_t next;
  2257. int x;
  2258. l3 = cachep->nodelists[nodeid];
  2259. BUG_ON(!l3);
  2260. retry:
  2261. spin_lock(&l3->list_lock);
  2262. entry = l3->slabs_partial.next;
  2263. if (entry == &l3->slabs_partial) {
  2264. l3->free_touched = 1;
  2265. entry = l3->slabs_free.next;
  2266. if (entry == &l3->slabs_free)
  2267. goto must_grow;
  2268. }
  2269. slabp = list_entry(entry, struct slab, list);
  2270. check_spinlock_acquired_node(cachep, nodeid);
  2271. check_slabp(cachep, slabp);
  2272. STATS_INC_NODEALLOCS(cachep);
  2273. STATS_INC_ACTIVE(cachep);
  2274. STATS_SET_HIGH(cachep);
  2275. BUG_ON(slabp->inuse == cachep->num);
  2276. /* get obj pointer */
  2277. obj = slabp->s_mem + slabp->free*cachep->objsize;
  2278. slabp->inuse++;
  2279. next = slab_bufctl(slabp)[slabp->free];
  2280. #if DEBUG
  2281. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2282. #endif
  2283. slabp->free = next;
  2284. check_slabp(cachep, slabp);
  2285. l3->free_objects--;
  2286. /* move slabp to correct slabp list: */
  2287. list_del(&slabp->list);
  2288. if (slabp->free == BUFCTL_END) {
  2289. list_add(&slabp->list, &l3->slabs_full);
  2290. } else {
  2291. list_add(&slabp->list, &l3->slabs_partial);
  2292. }
  2293. spin_unlock(&l3->list_lock);
  2294. goto done;
  2295. must_grow:
  2296. spin_unlock(&l3->list_lock);
  2297. x = cache_grow(cachep, flags, nodeid);
  2298. if (!x)
  2299. return NULL;
  2300. goto retry;
  2301. done:
  2302. return obj;
  2303. }
  2304. #endif
  2305. /*
  2306. * Caller needs to acquire correct kmem_list's list_lock
  2307. */
  2308. static void free_block(kmem_cache_t *cachep, void **objpp, int nr_objects, int node)
  2309. {
  2310. int i;
  2311. struct kmem_list3 *l3;
  2312. for (i = 0; i < nr_objects; i++) {
  2313. void *objp = objpp[i];
  2314. struct slab *slabp;
  2315. unsigned int objnr;
  2316. slabp = GET_PAGE_SLAB(virt_to_page(objp));
  2317. l3 = cachep->nodelists[node];
  2318. list_del(&slabp->list);
  2319. objnr = (objp - slabp->s_mem) / cachep->objsize;
  2320. check_spinlock_acquired_node(cachep, node);
  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. int node = numa_node_id();
  2357. batchcount = ac->batchcount;
  2358. #if DEBUG
  2359. BUG_ON(!batchcount || batchcount > ac->avail);
  2360. #endif
  2361. check_irq_off();
  2362. l3 = cachep->nodelists[node];
  2363. spin_lock(&l3->list_lock);
  2364. if (l3->shared) {
  2365. struct array_cache *shared_array = l3->shared;
  2366. int max = shared_array->limit-shared_array->avail;
  2367. if (max) {
  2368. if (batchcount > max)
  2369. batchcount = max;
  2370. memcpy(&(shared_array->entry[shared_array->avail]),
  2371. ac->entry,
  2372. sizeof(void*)*batchcount);
  2373. shared_array->avail += batchcount;
  2374. goto free_done;
  2375. }
  2376. }
  2377. free_block(cachep, ac->entry, batchcount, node);
  2378. free_done:
  2379. #if STATS
  2380. {
  2381. int i = 0;
  2382. struct list_head *p;
  2383. p = l3->slabs_free.next;
  2384. while (p != &(l3->slabs_free)) {
  2385. struct slab *slabp;
  2386. slabp = list_entry(p, struct slab, list);
  2387. BUG_ON(slabp->inuse);
  2388. i++;
  2389. p = p->next;
  2390. }
  2391. STATS_SET_FREEABLE(cachep, i);
  2392. }
  2393. #endif
  2394. spin_unlock(&l3->list_lock);
  2395. ac->avail -= batchcount;
  2396. memmove(ac->entry, &(ac->entry[batchcount]),
  2397. sizeof(void*)*ac->avail);
  2398. }
  2399. /*
  2400. * __cache_free
  2401. * Release an obj back to its cache. If the obj has a constructed
  2402. * state, it must be in this state _before_ it is released.
  2403. *
  2404. * Called with disabled ints.
  2405. */
  2406. static inline void __cache_free(kmem_cache_t *cachep, void *objp)
  2407. {
  2408. struct array_cache *ac = ac_data(cachep);
  2409. check_irq_off();
  2410. objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
  2411. /* Make sure we are not freeing a object from another
  2412. * node to the array cache on this cpu.
  2413. */
  2414. #ifdef CONFIG_NUMA
  2415. {
  2416. struct slab *slabp;
  2417. slabp = GET_PAGE_SLAB(virt_to_page(objp));
  2418. if (unlikely(slabp->nodeid != numa_node_id())) {
  2419. struct array_cache *alien = NULL;
  2420. int nodeid = slabp->nodeid;
  2421. struct kmem_list3 *l3 = cachep->nodelists[numa_node_id()];
  2422. STATS_INC_NODEFREES(cachep);
  2423. if (l3->alien && l3->alien[nodeid]) {
  2424. alien = l3->alien[nodeid];
  2425. spin_lock(&alien->lock);
  2426. if (unlikely(alien->avail == alien->limit))
  2427. __drain_alien_cache(cachep,
  2428. alien, nodeid);
  2429. alien->entry[alien->avail++] = objp;
  2430. spin_unlock(&alien->lock);
  2431. } else {
  2432. spin_lock(&(cachep->nodelists[nodeid])->
  2433. list_lock);
  2434. free_block(cachep, &objp, 1, nodeid);
  2435. spin_unlock(&(cachep->nodelists[nodeid])->
  2436. list_lock);
  2437. }
  2438. return;
  2439. }
  2440. }
  2441. #endif
  2442. if (likely(ac->avail < ac->limit)) {
  2443. STATS_INC_FREEHIT(cachep);
  2444. ac->entry[ac->avail++] = objp;
  2445. return;
  2446. } else {
  2447. STATS_INC_FREEMISS(cachep);
  2448. cache_flusharray(cachep, ac);
  2449. ac->entry[ac->avail++] = objp;
  2450. }
  2451. }
  2452. /**
  2453. * kmem_cache_alloc - Allocate an object
  2454. * @cachep: The cache to allocate from.
  2455. * @flags: See kmalloc().
  2456. *
  2457. * Allocate an object from this cache. The flags are only relevant
  2458. * if the cache has no available objects.
  2459. */
  2460. void *kmem_cache_alloc(kmem_cache_t *cachep, unsigned int __nocast flags)
  2461. {
  2462. return __cache_alloc(cachep, flags);
  2463. }
  2464. EXPORT_SYMBOL(kmem_cache_alloc);
  2465. /**
  2466. * kmem_ptr_validate - check if an untrusted pointer might
  2467. * be a slab entry.
  2468. * @cachep: the cache we're checking against
  2469. * @ptr: pointer to validate
  2470. *
  2471. * This verifies that the untrusted pointer looks sane:
  2472. * it is _not_ a guarantee that the pointer is actually
  2473. * part of the slab cache in question, but it at least
  2474. * validates that the pointer can be dereferenced and
  2475. * looks half-way sane.
  2476. *
  2477. * Currently only used for dentry validation.
  2478. */
  2479. int fastcall kmem_ptr_validate(kmem_cache_t *cachep, void *ptr)
  2480. {
  2481. unsigned long addr = (unsigned long) ptr;
  2482. unsigned long min_addr = PAGE_OFFSET;
  2483. unsigned long align_mask = BYTES_PER_WORD-1;
  2484. unsigned long size = cachep->objsize;
  2485. struct page *page;
  2486. if (unlikely(addr < min_addr))
  2487. goto out;
  2488. if (unlikely(addr > (unsigned long)high_memory - size))
  2489. goto out;
  2490. if (unlikely(addr & align_mask))
  2491. goto out;
  2492. if (unlikely(!kern_addr_valid(addr)))
  2493. goto out;
  2494. if (unlikely(!kern_addr_valid(addr + size - 1)))
  2495. goto out;
  2496. page = virt_to_page(ptr);
  2497. if (unlikely(!PageSlab(page)))
  2498. goto out;
  2499. if (unlikely(GET_PAGE_CACHE(page) != cachep))
  2500. goto out;
  2501. return 1;
  2502. out:
  2503. return 0;
  2504. }
  2505. #ifdef CONFIG_NUMA
  2506. /**
  2507. * kmem_cache_alloc_node - Allocate an object on the specified node
  2508. * @cachep: The cache to allocate from.
  2509. * @flags: See kmalloc().
  2510. * @nodeid: node number of the target node.
  2511. *
  2512. * Identical to kmem_cache_alloc, except that this function is slow
  2513. * and can sleep. And it will allocate memory on the given node, which
  2514. * can improve the performance for cpu bound structures.
  2515. * New and improved: it will now make sure that the object gets
  2516. * put on the correct node list so that there is no false sharing.
  2517. */
  2518. void *kmem_cache_alloc_node(kmem_cache_t *cachep, unsigned int __nocast flags, int nodeid)
  2519. {
  2520. unsigned long save_flags;
  2521. void *ptr;
  2522. if (nodeid == -1)
  2523. return __cache_alloc(cachep, flags);
  2524. if (unlikely(!cachep->nodelists[nodeid])) {
  2525. /* Fall back to __cache_alloc if we run into trouble */
  2526. printk(KERN_WARNING "slab: not allocating in inactive node %d for cache %s\n", nodeid, cachep->name);
  2527. return __cache_alloc(cachep,flags);
  2528. }
  2529. cache_alloc_debugcheck_before(cachep, flags);
  2530. local_irq_save(save_flags);
  2531. ptr = __cache_alloc_node(cachep, flags, nodeid);
  2532. local_irq_restore(save_flags);
  2533. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, __builtin_return_address(0));
  2534. return ptr;
  2535. }
  2536. EXPORT_SYMBOL(kmem_cache_alloc_node);
  2537. void *kmalloc_node(size_t size, unsigned int __nocast flags, int node)
  2538. {
  2539. kmem_cache_t *cachep;
  2540. cachep = kmem_find_general_cachep(size, flags);
  2541. if (unlikely(cachep == NULL))
  2542. return NULL;
  2543. return kmem_cache_alloc_node(cachep, flags, node);
  2544. }
  2545. EXPORT_SYMBOL(kmalloc_node);
  2546. #endif
  2547. /**
  2548. * kmalloc - allocate memory
  2549. * @size: how many bytes of memory are required.
  2550. * @flags: the type of memory to allocate.
  2551. *
  2552. * kmalloc is the normal method of allocating memory
  2553. * in the kernel.
  2554. *
  2555. * The @flags argument may be one of:
  2556. *
  2557. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  2558. *
  2559. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  2560. *
  2561. * %GFP_ATOMIC - Allocation will not sleep. Use inside interrupt handlers.
  2562. *
  2563. * Additionally, the %GFP_DMA flag may be set to indicate the memory
  2564. * must be suitable for DMA. This can mean different things on different
  2565. * platforms. For example, on i386, it means that the memory must come
  2566. * from the first 16MB.
  2567. */
  2568. void *__kmalloc(size_t size, unsigned int __nocast flags)
  2569. {
  2570. kmem_cache_t *cachep;
  2571. /* If you want to save a few bytes .text space: replace
  2572. * __ with kmem_.
  2573. * Then kmalloc uses the uninlined functions instead of the inline
  2574. * functions.
  2575. */
  2576. cachep = __find_general_cachep(size, flags);
  2577. BUG_ON(!cachep); /* Allocation size too large for kmalloc */
  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, node);
  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, cpu_to_node(i));
  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, node);
  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);