slab.c 108 KB

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