slab.c 115 KB

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