slab.c 118 KB

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