slab.c 108 KB

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