slab.c 110 KB

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