slab.c 107 KB

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