slab.c 108 KB

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