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