slab.c 117 KB

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