slab.c 119 KB

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