slub.c 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787
  1. /*
  2. * SLUB: A slab allocator that limits cache line use instead of queuing
  3. * objects in per cpu and per node lists.
  4. *
  5. * The allocator synchronizes using per slab locks and only
  6. * uses a centralized lock to manage a pool of partial slabs.
  7. *
  8. * (C) 2007 SGI, Christoph Lameter
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/swap.h> /* struct reclaim_state */
  12. #include <linux/module.h>
  13. #include <linux/bit_spinlock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/bitops.h>
  16. #include <linux/slab.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/kmemtrace.h>
  20. #include <linux/kmemcheck.h>
  21. #include <linux/cpu.h>
  22. #include <linux/cpuset.h>
  23. #include <linux/mempolicy.h>
  24. #include <linux/ctype.h>
  25. #include <linux/debugobjects.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/memory.h>
  28. #include <linux/math64.h>
  29. #include <linux/fault-inject.h>
  30. /*
  31. * Lock order:
  32. * 1. slab_lock(page)
  33. * 2. slab->list_lock
  34. *
  35. * The slab_lock protects operations on the object of a particular
  36. * slab and its metadata in the page struct. If the slab lock
  37. * has been taken then no allocations nor frees can be performed
  38. * on the objects in the slab nor can the slab be added or removed
  39. * from the partial or full lists since this would mean modifying
  40. * the page_struct of the slab.
  41. *
  42. * The list_lock protects the partial and full list on each node and
  43. * the partial slab counter. If taken then no new slabs may be added or
  44. * removed from the lists nor make the number of partial slabs be modified.
  45. * (Note that the total number of slabs is an atomic value that may be
  46. * modified without taking the list lock).
  47. *
  48. * The list_lock is a centralized lock and thus we avoid taking it as
  49. * much as possible. As long as SLUB does not have to handle partial
  50. * slabs, operations can continue without any centralized lock. F.e.
  51. * allocating a long series of objects that fill up slabs does not require
  52. * the list lock.
  53. *
  54. * The lock order is sometimes inverted when we are trying to get a slab
  55. * off a list. We take the list_lock and then look for a page on the list
  56. * to use. While we do that objects in the slabs may be freed. We can
  57. * only operate on the slab if we have also taken the slab_lock. So we use
  58. * a slab_trylock() on the slab. If trylock was successful then no frees
  59. * can occur anymore and we can use the slab for allocations etc. If the
  60. * slab_trylock() does not succeed then frees are in progress in the slab and
  61. * we must stay away from it for a while since we may cause a bouncing
  62. * cacheline if we try to acquire the lock. So go onto the next slab.
  63. * If all pages are busy then we may allocate a new slab instead of reusing
  64. * a partial slab. A new slab has noone operating on it and thus there is
  65. * no danger of cacheline contention.
  66. *
  67. * Interrupts are disabled during allocation and deallocation in order to
  68. * make the slab allocator safe to use in the context of an irq. In addition
  69. * interrupts are disabled to ensure that the processor does not change
  70. * while handling per_cpu slabs, due to kernel preemption.
  71. *
  72. * SLUB assigns one slab for allocation to each processor.
  73. * Allocations only occur from these slabs called cpu slabs.
  74. *
  75. * Slabs with free elements are kept on a partial list and during regular
  76. * operations no list for full slabs is used. If an object in a full slab is
  77. * freed then the slab will show up again on the partial lists.
  78. * We track full slabs for debugging purposes though because otherwise we
  79. * cannot scan all objects.
  80. *
  81. * Slabs are freed when they become empty. Teardown and setup is
  82. * minimal so we rely on the page allocators per cpu caches for
  83. * fast frees and allocs.
  84. *
  85. * Overloading of page flags that are otherwise used for LRU management.
  86. *
  87. * PageActive The slab is frozen and exempt from list processing.
  88. * This means that the slab is dedicated to a purpose
  89. * such as satisfying allocations for a specific
  90. * processor. Objects may be freed in the slab while
  91. * it is frozen but slab_free will then skip the usual
  92. * list operations. It is up to the processor holding
  93. * the slab to integrate the slab into the slab lists
  94. * when the slab is no longer needed.
  95. *
  96. * One use of this flag is to mark slabs that are
  97. * used for allocations. Then such a slab becomes a cpu
  98. * slab. The cpu slab may be equipped with an additional
  99. * freelist that allows lockless access to
  100. * free objects in addition to the regular freelist
  101. * that requires the slab lock.
  102. *
  103. * PageError Slab requires special handling due to debug
  104. * options set. This moves slab handling out of
  105. * the fast path and disables lockless freelists.
  106. */
  107. #ifdef CONFIG_SLUB_DEBUG
  108. #define SLABDEBUG 1
  109. #else
  110. #define SLABDEBUG 0
  111. #endif
  112. /*
  113. * Issues still to be resolved:
  114. *
  115. * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
  116. *
  117. * - Variable sizing of the per node arrays
  118. */
  119. /* Enable to test recovery from slab corruption on boot */
  120. #undef SLUB_RESILIENCY_TEST
  121. /*
  122. * Mininum number of partial slabs. These will be left on the partial
  123. * lists even if they are empty. kmem_cache_shrink may reclaim them.
  124. */
  125. #define MIN_PARTIAL 5
  126. /*
  127. * Maximum number of desirable partial slabs.
  128. * The existence of more partial slabs makes kmem_cache_shrink
  129. * sort the partial list by the number of objects in the.
  130. */
  131. #define MAX_PARTIAL 10
  132. #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
  133. SLAB_POISON | SLAB_STORE_USER)
  134. /*
  135. * Debugging flags that require metadata to be stored in the slab. These get
  136. * disabled when slub_debug=O is used and a cache's min order increases with
  137. * metadata.
  138. */
  139. #define DEBUG_METADATA_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
  140. /*
  141. * Set of flags that will prevent slab merging
  142. */
  143. #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  144. SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE)
  145. #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
  146. SLAB_CACHE_DMA | SLAB_NOTRACK)
  147. #ifndef ARCH_KMALLOC_MINALIGN
  148. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  149. #endif
  150. #ifndef ARCH_SLAB_MINALIGN
  151. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  152. #endif
  153. #define OO_SHIFT 16
  154. #define OO_MASK ((1 << OO_SHIFT) - 1)
  155. #define MAX_OBJS_PER_PAGE 65535 /* since page.objects is u16 */
  156. /* Internal SLUB flags */
  157. #define __OBJECT_POISON 0x80000000 /* Poison object */
  158. #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
  159. static int kmem_size = sizeof(struct kmem_cache);
  160. #ifdef CONFIG_SMP
  161. static struct notifier_block slab_notifier;
  162. #endif
  163. static enum {
  164. DOWN, /* No slab functionality available */
  165. PARTIAL, /* kmem_cache_open() works but kmalloc does not */
  166. UP, /* Everything works but does not show up in sysfs */
  167. SYSFS /* Sysfs up */
  168. } slab_state = DOWN;
  169. /* A list of all slab caches on the system */
  170. static DECLARE_RWSEM(slub_lock);
  171. static LIST_HEAD(slab_caches);
  172. /*
  173. * Tracking user of a slab.
  174. */
  175. struct track {
  176. unsigned long addr; /* Called from address */
  177. int cpu; /* Was running on cpu */
  178. int pid; /* Pid context */
  179. unsigned long when; /* When did the operation occur */
  180. };
  181. enum track_item { TRACK_ALLOC, TRACK_FREE };
  182. #ifdef CONFIG_SLUB_DEBUG
  183. static int sysfs_slab_add(struct kmem_cache *);
  184. static int sysfs_slab_alias(struct kmem_cache *, const char *);
  185. static void sysfs_slab_remove(struct kmem_cache *);
  186. #else
  187. static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
  188. static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
  189. { return 0; }
  190. static inline void sysfs_slab_remove(struct kmem_cache *s)
  191. {
  192. kfree(s);
  193. }
  194. #endif
  195. static inline void stat(struct kmem_cache_cpu *c, enum stat_item si)
  196. {
  197. #ifdef CONFIG_SLUB_STATS
  198. c->stat[si]++;
  199. #endif
  200. }
  201. /********************************************************************
  202. * Core slab cache functions
  203. *******************************************************************/
  204. int slab_is_available(void)
  205. {
  206. return slab_state >= UP;
  207. }
  208. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  209. {
  210. #ifdef CONFIG_NUMA
  211. return s->node[node];
  212. #else
  213. return &s->local_node;
  214. #endif
  215. }
  216. static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
  217. {
  218. #ifdef CONFIG_SMP
  219. return s->cpu_slab[cpu];
  220. #else
  221. return &s->cpu_slab;
  222. #endif
  223. }
  224. /* Verify that a pointer has an address that is valid within a slab page */
  225. static inline int check_valid_pointer(struct kmem_cache *s,
  226. struct page *page, const void *object)
  227. {
  228. void *base;
  229. if (!object)
  230. return 1;
  231. base = page_address(page);
  232. if (object < base || object >= base + page->objects * s->size ||
  233. (object - base) % s->size) {
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. /*
  239. * Slow version of get and set free pointer.
  240. *
  241. * This version requires touching the cache lines of kmem_cache which
  242. * we avoid to do in the fast alloc free paths. There we obtain the offset
  243. * from the page struct.
  244. */
  245. static inline void *get_freepointer(struct kmem_cache *s, void *object)
  246. {
  247. return *(void **)(object + s->offset);
  248. }
  249. static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
  250. {
  251. *(void **)(object + s->offset) = fp;
  252. }
  253. /* Loop over all objects in a slab */
  254. #define for_each_object(__p, __s, __addr, __objects) \
  255. for (__p = (__addr); __p < (__addr) + (__objects) * (__s)->size;\
  256. __p += (__s)->size)
  257. /* Scan freelist */
  258. #define for_each_free_object(__p, __s, __free) \
  259. for (__p = (__free); __p; __p = get_freepointer((__s), __p))
  260. /* Determine object index from a given position */
  261. static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
  262. {
  263. return (p - addr) / s->size;
  264. }
  265. static inline struct kmem_cache_order_objects oo_make(int order,
  266. unsigned long size)
  267. {
  268. struct kmem_cache_order_objects x = {
  269. (order << OO_SHIFT) + (PAGE_SIZE << order) / size
  270. };
  271. return x;
  272. }
  273. static inline int oo_order(struct kmem_cache_order_objects x)
  274. {
  275. return x.x >> OO_SHIFT;
  276. }
  277. static inline int oo_objects(struct kmem_cache_order_objects x)
  278. {
  279. return x.x & OO_MASK;
  280. }
  281. #ifdef CONFIG_SLUB_DEBUG
  282. /*
  283. * Debug settings:
  284. */
  285. #ifdef CONFIG_SLUB_DEBUG_ON
  286. static int slub_debug = DEBUG_DEFAULT_FLAGS;
  287. #else
  288. static int slub_debug;
  289. #endif
  290. static char *slub_debug_slabs;
  291. static int disable_higher_order_debug;
  292. /*
  293. * Object debugging
  294. */
  295. static void print_section(char *text, u8 *addr, unsigned int length)
  296. {
  297. int i, offset;
  298. int newline = 1;
  299. char ascii[17];
  300. ascii[16] = 0;
  301. for (i = 0; i < length; i++) {
  302. if (newline) {
  303. printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
  304. newline = 0;
  305. }
  306. printk(KERN_CONT " %02x", addr[i]);
  307. offset = i % 16;
  308. ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
  309. if (offset == 15) {
  310. printk(KERN_CONT " %s\n", ascii);
  311. newline = 1;
  312. }
  313. }
  314. if (!newline) {
  315. i %= 16;
  316. while (i < 16) {
  317. printk(KERN_CONT " ");
  318. ascii[i] = ' ';
  319. i++;
  320. }
  321. printk(KERN_CONT " %s\n", ascii);
  322. }
  323. }
  324. static struct track *get_track(struct kmem_cache *s, void *object,
  325. enum track_item alloc)
  326. {
  327. struct track *p;
  328. if (s->offset)
  329. p = object + s->offset + sizeof(void *);
  330. else
  331. p = object + s->inuse;
  332. return p + alloc;
  333. }
  334. static void set_track(struct kmem_cache *s, void *object,
  335. enum track_item alloc, unsigned long addr)
  336. {
  337. struct track *p = get_track(s, object, alloc);
  338. if (addr) {
  339. p->addr = addr;
  340. p->cpu = smp_processor_id();
  341. p->pid = current->pid;
  342. p->when = jiffies;
  343. } else
  344. memset(p, 0, sizeof(struct track));
  345. }
  346. static void init_tracking(struct kmem_cache *s, void *object)
  347. {
  348. if (!(s->flags & SLAB_STORE_USER))
  349. return;
  350. set_track(s, object, TRACK_FREE, 0UL);
  351. set_track(s, object, TRACK_ALLOC, 0UL);
  352. }
  353. static void print_track(const char *s, struct track *t)
  354. {
  355. if (!t->addr)
  356. return;
  357. printk(KERN_ERR "INFO: %s in %pS age=%lu cpu=%u pid=%d\n",
  358. s, (void *)t->addr, jiffies - t->when, t->cpu, t->pid);
  359. }
  360. static void print_tracking(struct kmem_cache *s, void *object)
  361. {
  362. if (!(s->flags & SLAB_STORE_USER))
  363. return;
  364. print_track("Allocated", get_track(s, object, TRACK_ALLOC));
  365. print_track("Freed", get_track(s, object, TRACK_FREE));
  366. }
  367. static void print_page_info(struct page *page)
  368. {
  369. printk(KERN_ERR "INFO: Slab 0x%p objects=%u used=%u fp=0x%p flags=0x%04lx\n",
  370. page, page->objects, page->inuse, page->freelist, page->flags);
  371. }
  372. static void slab_bug(struct kmem_cache *s, char *fmt, ...)
  373. {
  374. va_list args;
  375. char buf[100];
  376. va_start(args, fmt);
  377. vsnprintf(buf, sizeof(buf), fmt, args);
  378. va_end(args);
  379. printk(KERN_ERR "========================================"
  380. "=====================================\n");
  381. printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
  382. printk(KERN_ERR "----------------------------------------"
  383. "-------------------------------------\n\n");
  384. }
  385. static void slab_fix(struct kmem_cache *s, char *fmt, ...)
  386. {
  387. va_list args;
  388. char buf[100];
  389. va_start(args, fmt);
  390. vsnprintf(buf, sizeof(buf), fmt, args);
  391. va_end(args);
  392. printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
  393. }
  394. static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
  395. {
  396. unsigned int off; /* Offset of last byte */
  397. u8 *addr = page_address(page);
  398. print_tracking(s, p);
  399. print_page_info(page);
  400. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
  401. p, p - addr, get_freepointer(s, p));
  402. if (p > addr + 16)
  403. print_section("Bytes b4", p - 16, 16);
  404. print_section("Object", p, min_t(unsigned long, s->objsize, PAGE_SIZE));
  405. if (s->flags & SLAB_RED_ZONE)
  406. print_section("Redzone", p + s->objsize,
  407. s->inuse - s->objsize);
  408. if (s->offset)
  409. off = s->offset + sizeof(void *);
  410. else
  411. off = s->inuse;
  412. if (s->flags & SLAB_STORE_USER)
  413. off += 2 * sizeof(struct track);
  414. if (off != s->size)
  415. /* Beginning of the filler is the free pointer */
  416. print_section("Padding", p + off, s->size - off);
  417. dump_stack();
  418. }
  419. static void object_err(struct kmem_cache *s, struct page *page,
  420. u8 *object, char *reason)
  421. {
  422. slab_bug(s, "%s", reason);
  423. print_trailer(s, page, object);
  424. }
  425. static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
  426. {
  427. va_list args;
  428. char buf[100];
  429. va_start(args, fmt);
  430. vsnprintf(buf, sizeof(buf), fmt, args);
  431. va_end(args);
  432. slab_bug(s, "%s", buf);
  433. print_page_info(page);
  434. dump_stack();
  435. }
  436. static void init_object(struct kmem_cache *s, void *object, int active)
  437. {
  438. u8 *p = object;
  439. if (s->flags & __OBJECT_POISON) {
  440. memset(p, POISON_FREE, s->objsize - 1);
  441. p[s->objsize - 1] = POISON_END;
  442. }
  443. if (s->flags & SLAB_RED_ZONE)
  444. memset(p + s->objsize,
  445. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
  446. s->inuse - s->objsize);
  447. }
  448. static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
  449. {
  450. while (bytes) {
  451. if (*start != (u8)value)
  452. return start;
  453. start++;
  454. bytes--;
  455. }
  456. return NULL;
  457. }
  458. static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
  459. void *from, void *to)
  460. {
  461. slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
  462. memset(from, data, to - from);
  463. }
  464. static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
  465. u8 *object, char *what,
  466. u8 *start, unsigned int value, unsigned int bytes)
  467. {
  468. u8 *fault;
  469. u8 *end;
  470. fault = check_bytes(start, value, bytes);
  471. if (!fault)
  472. return 1;
  473. end = start + bytes;
  474. while (end > fault && end[-1] == value)
  475. end--;
  476. slab_bug(s, "%s overwritten", what);
  477. printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
  478. fault, end - 1, fault[0], value);
  479. print_trailer(s, page, object);
  480. restore_bytes(s, what, value, fault, end);
  481. return 0;
  482. }
  483. /*
  484. * Object layout:
  485. *
  486. * object address
  487. * Bytes of the object to be managed.
  488. * If the freepointer may overlay the object then the free
  489. * pointer is the first word of the object.
  490. *
  491. * Poisoning uses 0x6b (POISON_FREE) and the last byte is
  492. * 0xa5 (POISON_END)
  493. *
  494. * object + s->objsize
  495. * Padding to reach word boundary. This is also used for Redzoning.
  496. * Padding is extended by another word if Redzoning is enabled and
  497. * objsize == inuse.
  498. *
  499. * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
  500. * 0xcc (RED_ACTIVE) for objects in use.
  501. *
  502. * object + s->inuse
  503. * Meta data starts here.
  504. *
  505. * A. Free pointer (if we cannot overwrite object on free)
  506. * B. Tracking data for SLAB_STORE_USER
  507. * C. Padding to reach required alignment boundary or at mininum
  508. * one word if debugging is on to be able to detect writes
  509. * before the word boundary.
  510. *
  511. * Padding is done using 0x5a (POISON_INUSE)
  512. *
  513. * object + s->size
  514. * Nothing is used beyond s->size.
  515. *
  516. * If slabcaches are merged then the objsize and inuse boundaries are mostly
  517. * ignored. And therefore no slab options that rely on these boundaries
  518. * may be used with merged slabcaches.
  519. */
  520. static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
  521. {
  522. unsigned long off = s->inuse; /* The end of info */
  523. if (s->offset)
  524. /* Freepointer is placed after the object. */
  525. off += sizeof(void *);
  526. if (s->flags & SLAB_STORE_USER)
  527. /* We also have user information there */
  528. off += 2 * sizeof(struct track);
  529. if (s->size == off)
  530. return 1;
  531. return check_bytes_and_report(s, page, p, "Object padding",
  532. p + off, POISON_INUSE, s->size - off);
  533. }
  534. /* Check the pad bytes at the end of a slab page */
  535. static int slab_pad_check(struct kmem_cache *s, struct page *page)
  536. {
  537. u8 *start;
  538. u8 *fault;
  539. u8 *end;
  540. int length;
  541. int remainder;
  542. if (!(s->flags & SLAB_POISON))
  543. return 1;
  544. start = page_address(page);
  545. length = (PAGE_SIZE << compound_order(page));
  546. end = start + length;
  547. remainder = length % s->size;
  548. if (!remainder)
  549. return 1;
  550. fault = check_bytes(end - remainder, POISON_INUSE, remainder);
  551. if (!fault)
  552. return 1;
  553. while (end > fault && end[-1] == POISON_INUSE)
  554. end--;
  555. slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
  556. print_section("Padding", end - remainder, remainder);
  557. restore_bytes(s, "slab padding", POISON_INUSE, end - remainder, end);
  558. return 0;
  559. }
  560. static int check_object(struct kmem_cache *s, struct page *page,
  561. void *object, int active)
  562. {
  563. u8 *p = object;
  564. u8 *endobject = object + s->objsize;
  565. if (s->flags & SLAB_RED_ZONE) {
  566. unsigned int red =
  567. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
  568. if (!check_bytes_and_report(s, page, object, "Redzone",
  569. endobject, red, s->inuse - s->objsize))
  570. return 0;
  571. } else {
  572. if ((s->flags & SLAB_POISON) && s->objsize < s->inuse) {
  573. check_bytes_and_report(s, page, p, "Alignment padding",
  574. endobject, POISON_INUSE, s->inuse - s->objsize);
  575. }
  576. }
  577. if (s->flags & SLAB_POISON) {
  578. if (!active && (s->flags & __OBJECT_POISON) &&
  579. (!check_bytes_and_report(s, page, p, "Poison", p,
  580. POISON_FREE, s->objsize - 1) ||
  581. !check_bytes_and_report(s, page, p, "Poison",
  582. p + s->objsize - 1, POISON_END, 1)))
  583. return 0;
  584. /*
  585. * check_pad_bytes cleans up on its own.
  586. */
  587. check_pad_bytes(s, page, p);
  588. }
  589. if (!s->offset && active)
  590. /*
  591. * Object and freepointer overlap. Cannot check
  592. * freepointer while object is allocated.
  593. */
  594. return 1;
  595. /* Check free pointer validity */
  596. if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
  597. object_err(s, page, p, "Freepointer corrupt");
  598. /*
  599. * No choice but to zap it and thus lose the remainder
  600. * of the free objects in this slab. May cause
  601. * another error because the object count is now wrong.
  602. */
  603. set_freepointer(s, p, NULL);
  604. return 0;
  605. }
  606. return 1;
  607. }
  608. static int check_slab(struct kmem_cache *s, struct page *page)
  609. {
  610. int maxobj;
  611. VM_BUG_ON(!irqs_disabled());
  612. if (!PageSlab(page)) {
  613. slab_err(s, page, "Not a valid slab page");
  614. return 0;
  615. }
  616. maxobj = (PAGE_SIZE << compound_order(page)) / s->size;
  617. if (page->objects > maxobj) {
  618. slab_err(s, page, "objects %u > max %u",
  619. s->name, page->objects, maxobj);
  620. return 0;
  621. }
  622. if (page->inuse > page->objects) {
  623. slab_err(s, page, "inuse %u > max %u",
  624. s->name, page->inuse, page->objects);
  625. return 0;
  626. }
  627. /* Slab_pad_check fixes things up after itself */
  628. slab_pad_check(s, page);
  629. return 1;
  630. }
  631. /*
  632. * Determine if a certain object on a page is on the freelist. Must hold the
  633. * slab lock to guarantee that the chains are in a consistent state.
  634. */
  635. static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
  636. {
  637. int nr = 0;
  638. void *fp = page->freelist;
  639. void *object = NULL;
  640. unsigned long max_objects;
  641. while (fp && nr <= page->objects) {
  642. if (fp == search)
  643. return 1;
  644. if (!check_valid_pointer(s, page, fp)) {
  645. if (object) {
  646. object_err(s, page, object,
  647. "Freechain corrupt");
  648. set_freepointer(s, object, NULL);
  649. break;
  650. } else {
  651. slab_err(s, page, "Freepointer corrupt");
  652. page->freelist = NULL;
  653. page->inuse = page->objects;
  654. slab_fix(s, "Freelist cleared");
  655. return 0;
  656. }
  657. break;
  658. }
  659. object = fp;
  660. fp = get_freepointer(s, object);
  661. nr++;
  662. }
  663. max_objects = (PAGE_SIZE << compound_order(page)) / s->size;
  664. if (max_objects > MAX_OBJS_PER_PAGE)
  665. max_objects = MAX_OBJS_PER_PAGE;
  666. if (page->objects != max_objects) {
  667. slab_err(s, page, "Wrong number of objects. Found %d but "
  668. "should be %d", page->objects, max_objects);
  669. page->objects = max_objects;
  670. slab_fix(s, "Number of objects adjusted.");
  671. }
  672. if (page->inuse != page->objects - nr) {
  673. slab_err(s, page, "Wrong object count. Counter is %d but "
  674. "counted were %d", page->inuse, page->objects - nr);
  675. page->inuse = page->objects - nr;
  676. slab_fix(s, "Object count adjusted.");
  677. }
  678. return search == NULL;
  679. }
  680. static void trace(struct kmem_cache *s, struct page *page, void *object,
  681. int alloc)
  682. {
  683. if (s->flags & SLAB_TRACE) {
  684. printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
  685. s->name,
  686. alloc ? "alloc" : "free",
  687. object, page->inuse,
  688. page->freelist);
  689. if (!alloc)
  690. print_section("Object", (void *)object, s->objsize);
  691. dump_stack();
  692. }
  693. }
  694. /*
  695. * Tracking of fully allocated slabs for debugging purposes.
  696. */
  697. static void add_full(struct kmem_cache_node *n, struct page *page)
  698. {
  699. spin_lock(&n->list_lock);
  700. list_add(&page->lru, &n->full);
  701. spin_unlock(&n->list_lock);
  702. }
  703. static void remove_full(struct kmem_cache *s, struct page *page)
  704. {
  705. struct kmem_cache_node *n;
  706. if (!(s->flags & SLAB_STORE_USER))
  707. return;
  708. n = get_node(s, page_to_nid(page));
  709. spin_lock(&n->list_lock);
  710. list_del(&page->lru);
  711. spin_unlock(&n->list_lock);
  712. }
  713. /* Tracking of the number of slabs for debugging purposes */
  714. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  715. {
  716. struct kmem_cache_node *n = get_node(s, node);
  717. return atomic_long_read(&n->nr_slabs);
  718. }
  719. static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
  720. {
  721. return atomic_long_read(&n->nr_slabs);
  722. }
  723. static inline void inc_slabs_node(struct kmem_cache *s, int node, int objects)
  724. {
  725. struct kmem_cache_node *n = get_node(s, node);
  726. /*
  727. * May be called early in order to allocate a slab for the
  728. * kmem_cache_node structure. Solve the chicken-egg
  729. * dilemma by deferring the increment of the count during
  730. * bootstrap (see early_kmem_cache_node_alloc).
  731. */
  732. if (!NUMA_BUILD || n) {
  733. atomic_long_inc(&n->nr_slabs);
  734. atomic_long_add(objects, &n->total_objects);
  735. }
  736. }
  737. static inline void dec_slabs_node(struct kmem_cache *s, int node, int objects)
  738. {
  739. struct kmem_cache_node *n = get_node(s, node);
  740. atomic_long_dec(&n->nr_slabs);
  741. atomic_long_sub(objects, &n->total_objects);
  742. }
  743. /* Object debug checks for alloc/free paths */
  744. static void setup_object_debug(struct kmem_cache *s, struct page *page,
  745. void *object)
  746. {
  747. if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
  748. return;
  749. init_object(s, object, 0);
  750. init_tracking(s, object);
  751. }
  752. static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
  753. void *object, unsigned long addr)
  754. {
  755. if (!check_slab(s, page))
  756. goto bad;
  757. if (!on_freelist(s, page, object)) {
  758. object_err(s, page, object, "Object already allocated");
  759. goto bad;
  760. }
  761. if (!check_valid_pointer(s, page, object)) {
  762. object_err(s, page, object, "Freelist Pointer check fails");
  763. goto bad;
  764. }
  765. if (!check_object(s, page, object, 0))
  766. goto bad;
  767. /* Success perform special debug activities for allocs */
  768. if (s->flags & SLAB_STORE_USER)
  769. set_track(s, object, TRACK_ALLOC, addr);
  770. trace(s, page, object, 1);
  771. init_object(s, object, 1);
  772. return 1;
  773. bad:
  774. if (PageSlab(page)) {
  775. /*
  776. * If this is a slab page then lets do the best we can
  777. * to avoid issues in the future. Marking all objects
  778. * as used avoids touching the remaining objects.
  779. */
  780. slab_fix(s, "Marking all objects used");
  781. page->inuse = page->objects;
  782. page->freelist = NULL;
  783. }
  784. return 0;
  785. }
  786. static int free_debug_processing(struct kmem_cache *s, struct page *page,
  787. void *object, unsigned long addr)
  788. {
  789. if (!check_slab(s, page))
  790. goto fail;
  791. if (!check_valid_pointer(s, page, object)) {
  792. slab_err(s, page, "Invalid object pointer 0x%p", object);
  793. goto fail;
  794. }
  795. if (on_freelist(s, page, object)) {
  796. object_err(s, page, object, "Object already free");
  797. goto fail;
  798. }
  799. if (!check_object(s, page, object, 1))
  800. return 0;
  801. if (unlikely(s != page->slab)) {
  802. if (!PageSlab(page)) {
  803. slab_err(s, page, "Attempt to free object(0x%p) "
  804. "outside of slab", object);
  805. } else if (!page->slab) {
  806. printk(KERN_ERR
  807. "SLUB <none>: no slab for object 0x%p.\n",
  808. object);
  809. dump_stack();
  810. } else
  811. object_err(s, page, object,
  812. "page slab pointer corrupt.");
  813. goto fail;
  814. }
  815. /* Special debug activities for freeing objects */
  816. if (!PageSlubFrozen(page) && !page->freelist)
  817. remove_full(s, page);
  818. if (s->flags & SLAB_STORE_USER)
  819. set_track(s, object, TRACK_FREE, addr);
  820. trace(s, page, object, 0);
  821. init_object(s, object, 0);
  822. return 1;
  823. fail:
  824. slab_fix(s, "Object at 0x%p not freed", object);
  825. return 0;
  826. }
  827. static int __init setup_slub_debug(char *str)
  828. {
  829. slub_debug = DEBUG_DEFAULT_FLAGS;
  830. if (*str++ != '=' || !*str)
  831. /*
  832. * No options specified. Switch on full debugging.
  833. */
  834. goto out;
  835. if (*str == ',')
  836. /*
  837. * No options but restriction on slabs. This means full
  838. * debugging for slabs matching a pattern.
  839. */
  840. goto check_slabs;
  841. if (tolower(*str) == 'o') {
  842. /*
  843. * Avoid enabling debugging on caches if its minimum order
  844. * would increase as a result.
  845. */
  846. disable_higher_order_debug = 1;
  847. goto out;
  848. }
  849. slub_debug = 0;
  850. if (*str == '-')
  851. /*
  852. * Switch off all debugging measures.
  853. */
  854. goto out;
  855. /*
  856. * Determine which debug features should be switched on
  857. */
  858. for (; *str && *str != ','; str++) {
  859. switch (tolower(*str)) {
  860. case 'f':
  861. slub_debug |= SLAB_DEBUG_FREE;
  862. break;
  863. case 'z':
  864. slub_debug |= SLAB_RED_ZONE;
  865. break;
  866. case 'p':
  867. slub_debug |= SLAB_POISON;
  868. break;
  869. case 'u':
  870. slub_debug |= SLAB_STORE_USER;
  871. break;
  872. case 't':
  873. slub_debug |= SLAB_TRACE;
  874. break;
  875. default:
  876. printk(KERN_ERR "slub_debug option '%c' "
  877. "unknown. skipped\n", *str);
  878. }
  879. }
  880. check_slabs:
  881. if (*str == ',')
  882. slub_debug_slabs = str + 1;
  883. out:
  884. return 1;
  885. }
  886. __setup("slub_debug", setup_slub_debug);
  887. static unsigned long kmem_cache_flags(unsigned long objsize,
  888. unsigned long flags, const char *name,
  889. void (*ctor)(void *))
  890. {
  891. /*
  892. * Enable debugging if selected on the kernel commandline.
  893. */
  894. if (slub_debug && (!slub_debug_slabs ||
  895. !strncmp(slub_debug_slabs, name, strlen(slub_debug_slabs))))
  896. flags |= slub_debug;
  897. return flags;
  898. }
  899. #else
  900. static inline void setup_object_debug(struct kmem_cache *s,
  901. struct page *page, void *object) {}
  902. static inline int alloc_debug_processing(struct kmem_cache *s,
  903. struct page *page, void *object, unsigned long addr) { return 0; }
  904. static inline int free_debug_processing(struct kmem_cache *s,
  905. struct page *page, void *object, unsigned long addr) { return 0; }
  906. static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
  907. { return 1; }
  908. static inline int check_object(struct kmem_cache *s, struct page *page,
  909. void *object, int active) { return 1; }
  910. static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
  911. static inline unsigned long kmem_cache_flags(unsigned long objsize,
  912. unsigned long flags, const char *name,
  913. void (*ctor)(void *))
  914. {
  915. return flags;
  916. }
  917. #define slub_debug 0
  918. static inline unsigned long slabs_node(struct kmem_cache *s, int node)
  919. { return 0; }
  920. static inline unsigned long node_nr_slabs(struct kmem_cache_node *n)
  921. { return 0; }
  922. static inline void inc_slabs_node(struct kmem_cache *s, int node,
  923. int objects) {}
  924. static inline void dec_slabs_node(struct kmem_cache *s, int node,
  925. int objects) {}
  926. #endif
  927. /*
  928. * Slab allocation and freeing
  929. */
  930. static inline struct page *alloc_slab_page(gfp_t flags, int node,
  931. struct kmem_cache_order_objects oo)
  932. {
  933. int order = oo_order(oo);
  934. flags |= __GFP_NOTRACK;
  935. if (node == -1)
  936. return alloc_pages(flags, order);
  937. else
  938. return alloc_pages_node(node, flags, order);
  939. }
  940. static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  941. {
  942. struct page *page;
  943. struct kmem_cache_order_objects oo = s->oo;
  944. gfp_t alloc_gfp;
  945. flags |= s->allocflags;
  946. /*
  947. * Let the initial higher-order allocation fail under memory pressure
  948. * so we fall-back to the minimum order allocation.
  949. */
  950. alloc_gfp = (flags | __GFP_NOWARN | __GFP_NORETRY) & ~__GFP_NOFAIL;
  951. page = alloc_slab_page(alloc_gfp, node, oo);
  952. if (unlikely(!page)) {
  953. oo = s->min;
  954. /*
  955. * Allocation may have failed due to fragmentation.
  956. * Try a lower order alloc if possible
  957. */
  958. page = alloc_slab_page(flags, node, oo);
  959. if (!page)
  960. return NULL;
  961. stat(get_cpu_slab(s, raw_smp_processor_id()), ORDER_FALLBACK);
  962. }
  963. if (kmemcheck_enabled
  964. && !(s->flags & (SLAB_NOTRACK | DEBUG_DEFAULT_FLAGS))) {
  965. int pages = 1 << oo_order(oo);
  966. kmemcheck_alloc_shadow(page, oo_order(oo), flags, node);
  967. /*
  968. * Objects from caches that have a constructor don't get
  969. * cleared when they're allocated, so we need to do it here.
  970. */
  971. if (s->ctor)
  972. kmemcheck_mark_uninitialized_pages(page, pages);
  973. else
  974. kmemcheck_mark_unallocated_pages(page, pages);
  975. }
  976. page->objects = oo_objects(oo);
  977. mod_zone_page_state(page_zone(page),
  978. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  979. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  980. 1 << oo_order(oo));
  981. return page;
  982. }
  983. static void setup_object(struct kmem_cache *s, struct page *page,
  984. void *object)
  985. {
  986. setup_object_debug(s, page, object);
  987. if (unlikely(s->ctor))
  988. s->ctor(object);
  989. }
  990. static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  991. {
  992. struct page *page;
  993. void *start;
  994. void *last;
  995. void *p;
  996. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  997. page = allocate_slab(s,
  998. flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  999. if (!page)
  1000. goto out;
  1001. inc_slabs_node(s, page_to_nid(page), page->objects);
  1002. page->slab = s;
  1003. page->flags |= 1 << PG_slab;
  1004. if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
  1005. SLAB_STORE_USER | SLAB_TRACE))
  1006. __SetPageSlubDebug(page);
  1007. start = page_address(page);
  1008. if (unlikely(s->flags & SLAB_POISON))
  1009. memset(start, POISON_INUSE, PAGE_SIZE << compound_order(page));
  1010. last = start;
  1011. for_each_object(p, s, start, page->objects) {
  1012. setup_object(s, page, last);
  1013. set_freepointer(s, last, p);
  1014. last = p;
  1015. }
  1016. setup_object(s, page, last);
  1017. set_freepointer(s, last, NULL);
  1018. page->freelist = start;
  1019. page->inuse = 0;
  1020. out:
  1021. return page;
  1022. }
  1023. static void __free_slab(struct kmem_cache *s, struct page *page)
  1024. {
  1025. int order = compound_order(page);
  1026. int pages = 1 << order;
  1027. if (unlikely(SLABDEBUG && PageSlubDebug(page))) {
  1028. void *p;
  1029. slab_pad_check(s, page);
  1030. for_each_object(p, s, page_address(page),
  1031. page->objects)
  1032. check_object(s, page, p, 0);
  1033. __ClearPageSlubDebug(page);
  1034. }
  1035. kmemcheck_free_shadow(page, compound_order(page));
  1036. mod_zone_page_state(page_zone(page),
  1037. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  1038. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  1039. -pages);
  1040. __ClearPageSlab(page);
  1041. reset_page_mapcount(page);
  1042. if (current->reclaim_state)
  1043. current->reclaim_state->reclaimed_slab += pages;
  1044. __free_pages(page, order);
  1045. }
  1046. static void rcu_free_slab(struct rcu_head *h)
  1047. {
  1048. struct page *page;
  1049. page = container_of((struct list_head *)h, struct page, lru);
  1050. __free_slab(page->slab, page);
  1051. }
  1052. static void free_slab(struct kmem_cache *s, struct page *page)
  1053. {
  1054. if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
  1055. /*
  1056. * RCU free overloads the RCU head over the LRU
  1057. */
  1058. struct rcu_head *head = (void *)&page->lru;
  1059. call_rcu(head, rcu_free_slab);
  1060. } else
  1061. __free_slab(s, page);
  1062. }
  1063. static void discard_slab(struct kmem_cache *s, struct page *page)
  1064. {
  1065. dec_slabs_node(s, page_to_nid(page), page->objects);
  1066. free_slab(s, page);
  1067. }
  1068. /*
  1069. * Per slab locking using the pagelock
  1070. */
  1071. static __always_inline void slab_lock(struct page *page)
  1072. {
  1073. bit_spin_lock(PG_locked, &page->flags);
  1074. }
  1075. static __always_inline void slab_unlock(struct page *page)
  1076. {
  1077. __bit_spin_unlock(PG_locked, &page->flags);
  1078. }
  1079. static __always_inline int slab_trylock(struct page *page)
  1080. {
  1081. int rc = 1;
  1082. rc = bit_spin_trylock(PG_locked, &page->flags);
  1083. return rc;
  1084. }
  1085. /*
  1086. * Management of partially allocated slabs
  1087. */
  1088. static void add_partial(struct kmem_cache_node *n,
  1089. struct page *page, int tail)
  1090. {
  1091. spin_lock(&n->list_lock);
  1092. n->nr_partial++;
  1093. if (tail)
  1094. list_add_tail(&page->lru, &n->partial);
  1095. else
  1096. list_add(&page->lru, &n->partial);
  1097. spin_unlock(&n->list_lock);
  1098. }
  1099. static void remove_partial(struct kmem_cache *s, struct page *page)
  1100. {
  1101. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1102. spin_lock(&n->list_lock);
  1103. list_del(&page->lru);
  1104. n->nr_partial--;
  1105. spin_unlock(&n->list_lock);
  1106. }
  1107. /*
  1108. * Lock slab and remove from the partial list.
  1109. *
  1110. * Must hold list_lock.
  1111. */
  1112. static inline int lock_and_freeze_slab(struct kmem_cache_node *n,
  1113. struct page *page)
  1114. {
  1115. if (slab_trylock(page)) {
  1116. list_del(&page->lru);
  1117. n->nr_partial--;
  1118. __SetPageSlubFrozen(page);
  1119. return 1;
  1120. }
  1121. return 0;
  1122. }
  1123. /*
  1124. * Try to allocate a partial slab from a specific node.
  1125. */
  1126. static struct page *get_partial_node(struct kmem_cache_node *n)
  1127. {
  1128. struct page *page;
  1129. /*
  1130. * Racy check. If we mistakenly see no partial slabs then we
  1131. * just allocate an empty slab. If we mistakenly try to get a
  1132. * partial slab and there is none available then get_partials()
  1133. * will return NULL.
  1134. */
  1135. if (!n || !n->nr_partial)
  1136. return NULL;
  1137. spin_lock(&n->list_lock);
  1138. list_for_each_entry(page, &n->partial, lru)
  1139. if (lock_and_freeze_slab(n, page))
  1140. goto out;
  1141. page = NULL;
  1142. out:
  1143. spin_unlock(&n->list_lock);
  1144. return page;
  1145. }
  1146. /*
  1147. * Get a page from somewhere. Search in increasing NUMA distances.
  1148. */
  1149. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
  1150. {
  1151. #ifdef CONFIG_NUMA
  1152. struct zonelist *zonelist;
  1153. struct zoneref *z;
  1154. struct zone *zone;
  1155. enum zone_type high_zoneidx = gfp_zone(flags);
  1156. struct page *page;
  1157. /*
  1158. * The defrag ratio allows a configuration of the tradeoffs between
  1159. * inter node defragmentation and node local allocations. A lower
  1160. * defrag_ratio increases the tendency to do local allocations
  1161. * instead of attempting to obtain partial slabs from other nodes.
  1162. *
  1163. * If the defrag_ratio is set to 0 then kmalloc() always
  1164. * returns node local objects. If the ratio is higher then kmalloc()
  1165. * may return off node objects because partial slabs are obtained
  1166. * from other nodes and filled up.
  1167. *
  1168. * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
  1169. * defrag_ratio = 1000) then every (well almost) allocation will
  1170. * first attempt to defrag slab caches on other nodes. This means
  1171. * scanning over all nodes to look for partial slabs which may be
  1172. * expensive if we do it every time we are trying to find a slab
  1173. * with available objects.
  1174. */
  1175. if (!s->remote_node_defrag_ratio ||
  1176. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1177. return NULL;
  1178. zonelist = node_zonelist(slab_node(current->mempolicy), flags);
  1179. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1180. struct kmem_cache_node *n;
  1181. n = get_node(s, zone_to_nid(zone));
  1182. if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
  1183. n->nr_partial > s->min_partial) {
  1184. page = get_partial_node(n);
  1185. if (page)
  1186. return page;
  1187. }
  1188. }
  1189. #endif
  1190. return NULL;
  1191. }
  1192. /*
  1193. * Get a partial page, lock it and return it.
  1194. */
  1195. static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
  1196. {
  1197. struct page *page;
  1198. int searchnode = (node == -1) ? numa_node_id() : node;
  1199. page = get_partial_node(get_node(s, searchnode));
  1200. if (page || (flags & __GFP_THISNODE))
  1201. return page;
  1202. return get_any_partial(s, flags);
  1203. }
  1204. /*
  1205. * Move a page back to the lists.
  1206. *
  1207. * Must be called with the slab lock held.
  1208. *
  1209. * On exit the slab lock will have been dropped.
  1210. */
  1211. static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
  1212. {
  1213. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1214. struct kmem_cache_cpu *c = get_cpu_slab(s, smp_processor_id());
  1215. __ClearPageSlubFrozen(page);
  1216. if (page->inuse) {
  1217. if (page->freelist) {
  1218. add_partial(n, page, tail);
  1219. stat(c, tail ? DEACTIVATE_TO_TAIL : DEACTIVATE_TO_HEAD);
  1220. } else {
  1221. stat(c, DEACTIVATE_FULL);
  1222. if (SLABDEBUG && PageSlubDebug(page) &&
  1223. (s->flags & SLAB_STORE_USER))
  1224. add_full(n, page);
  1225. }
  1226. slab_unlock(page);
  1227. } else {
  1228. stat(c, DEACTIVATE_EMPTY);
  1229. if (n->nr_partial < s->min_partial) {
  1230. /*
  1231. * Adding an empty slab to the partial slabs in order
  1232. * to avoid page allocator overhead. This slab needs
  1233. * to come after the other slabs with objects in
  1234. * so that the others get filled first. That way the
  1235. * size of the partial list stays small.
  1236. *
  1237. * kmem_cache_shrink can reclaim any empty slabs from
  1238. * the partial list.
  1239. */
  1240. add_partial(n, page, 1);
  1241. slab_unlock(page);
  1242. } else {
  1243. slab_unlock(page);
  1244. stat(get_cpu_slab(s, raw_smp_processor_id()), FREE_SLAB);
  1245. discard_slab(s, page);
  1246. }
  1247. }
  1248. }
  1249. /*
  1250. * Remove the cpu slab
  1251. */
  1252. static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1253. {
  1254. struct page *page = c->page;
  1255. int tail = 1;
  1256. if (page->freelist)
  1257. stat(c, DEACTIVATE_REMOTE_FREES);
  1258. /*
  1259. * Merge cpu freelist into slab freelist. Typically we get here
  1260. * because both freelists are empty. So this is unlikely
  1261. * to occur.
  1262. */
  1263. while (unlikely(c->freelist)) {
  1264. void **object;
  1265. tail = 0; /* Hot objects. Put the slab first */
  1266. /* Retrieve object from cpu_freelist */
  1267. object = c->freelist;
  1268. c->freelist = c->freelist[c->offset];
  1269. /* And put onto the regular freelist */
  1270. object[c->offset] = page->freelist;
  1271. page->freelist = object;
  1272. page->inuse--;
  1273. }
  1274. c->page = NULL;
  1275. unfreeze_slab(s, page, tail);
  1276. }
  1277. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1278. {
  1279. stat(c, CPUSLAB_FLUSH);
  1280. slab_lock(c->page);
  1281. deactivate_slab(s, c);
  1282. }
  1283. /*
  1284. * Flush cpu slab.
  1285. *
  1286. * Called from IPI handler with interrupts disabled.
  1287. */
  1288. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1289. {
  1290. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1291. if (likely(c && c->page))
  1292. flush_slab(s, c);
  1293. }
  1294. static void flush_cpu_slab(void *d)
  1295. {
  1296. struct kmem_cache *s = d;
  1297. __flush_cpu_slab(s, smp_processor_id());
  1298. }
  1299. static void flush_all(struct kmem_cache *s)
  1300. {
  1301. on_each_cpu(flush_cpu_slab, s, 1);
  1302. }
  1303. /*
  1304. * Check if the objects in a per cpu structure fit numa
  1305. * locality expectations.
  1306. */
  1307. static inline int node_match(struct kmem_cache_cpu *c, int node)
  1308. {
  1309. #ifdef CONFIG_NUMA
  1310. if (node != -1 && c->node != node)
  1311. return 0;
  1312. #endif
  1313. return 1;
  1314. }
  1315. static int count_free(struct page *page)
  1316. {
  1317. return page->objects - page->inuse;
  1318. }
  1319. static unsigned long count_partial(struct kmem_cache_node *n,
  1320. int (*get_count)(struct page *))
  1321. {
  1322. unsigned long flags;
  1323. unsigned long x = 0;
  1324. struct page *page;
  1325. spin_lock_irqsave(&n->list_lock, flags);
  1326. list_for_each_entry(page, &n->partial, lru)
  1327. x += get_count(page);
  1328. spin_unlock_irqrestore(&n->list_lock, flags);
  1329. return x;
  1330. }
  1331. static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
  1332. {
  1333. #ifdef CONFIG_SLUB_DEBUG
  1334. return atomic_long_read(&n->total_objects);
  1335. #else
  1336. return 0;
  1337. #endif
  1338. }
  1339. static noinline void
  1340. slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
  1341. {
  1342. int node;
  1343. printk(KERN_WARNING
  1344. "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1345. nid, gfpflags);
  1346. printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
  1347. "default order: %d, min order: %d\n", s->name, s->objsize,
  1348. s->size, oo_order(s->oo), oo_order(s->min));
  1349. if (oo_order(s->min) > get_order(s->objsize))
  1350. printk(KERN_WARNING " %s debugging increased min order, use "
  1351. "slub_debug=O to disable.\n", s->name);
  1352. for_each_online_node(node) {
  1353. struct kmem_cache_node *n = get_node(s, node);
  1354. unsigned long nr_slabs;
  1355. unsigned long nr_objs;
  1356. unsigned long nr_free;
  1357. if (!n)
  1358. continue;
  1359. nr_free = count_partial(n, count_free);
  1360. nr_slabs = node_nr_slabs(n);
  1361. nr_objs = node_nr_objs(n);
  1362. printk(KERN_WARNING
  1363. " node %d: slabs: %ld, objs: %ld, free: %ld\n",
  1364. node, nr_slabs, nr_objs, nr_free);
  1365. }
  1366. }
  1367. /*
  1368. * Slow path. The lockless freelist is empty or we need to perform
  1369. * debugging duties.
  1370. *
  1371. * Interrupts are disabled.
  1372. *
  1373. * Processing is still very fast if new objects have been freed to the
  1374. * regular freelist. In that case we simply take over the regular freelist
  1375. * as the lockless freelist and zap the regular freelist.
  1376. *
  1377. * If that is not working then we fall back to the partial lists. We take the
  1378. * first element of the freelist as the object to allocate now and move the
  1379. * rest of the freelist to the lockless freelist.
  1380. *
  1381. * And if we were unable to get a new slab from the partial slab lists then
  1382. * we need to allocate a new slab. This is the slowest path since it involves
  1383. * a call to the page allocator and the setup of a new slab.
  1384. */
  1385. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  1386. unsigned long addr, struct kmem_cache_cpu *c)
  1387. {
  1388. void **object;
  1389. struct page *new;
  1390. /* We handle __GFP_ZERO in the caller */
  1391. gfpflags &= ~__GFP_ZERO;
  1392. if (!c->page)
  1393. goto new_slab;
  1394. slab_lock(c->page);
  1395. if (unlikely(!node_match(c, node)))
  1396. goto another_slab;
  1397. stat(c, ALLOC_REFILL);
  1398. load_freelist:
  1399. object = c->page->freelist;
  1400. if (unlikely(!object))
  1401. goto another_slab;
  1402. if (unlikely(SLABDEBUG && PageSlubDebug(c->page)))
  1403. goto debug;
  1404. c->freelist = object[c->offset];
  1405. c->page->inuse = c->page->objects;
  1406. c->page->freelist = NULL;
  1407. c->node = page_to_nid(c->page);
  1408. unlock_out:
  1409. slab_unlock(c->page);
  1410. stat(c, ALLOC_SLOWPATH);
  1411. return object;
  1412. another_slab:
  1413. deactivate_slab(s, c);
  1414. new_slab:
  1415. new = get_partial(s, gfpflags, node);
  1416. if (new) {
  1417. c->page = new;
  1418. stat(c, ALLOC_FROM_PARTIAL);
  1419. goto load_freelist;
  1420. }
  1421. if (gfpflags & __GFP_WAIT)
  1422. local_irq_enable();
  1423. new = new_slab(s, gfpflags, node);
  1424. if (gfpflags & __GFP_WAIT)
  1425. local_irq_disable();
  1426. if (new) {
  1427. c = get_cpu_slab(s, smp_processor_id());
  1428. stat(c, ALLOC_SLAB);
  1429. if (c->page)
  1430. flush_slab(s, c);
  1431. slab_lock(new);
  1432. __SetPageSlubFrozen(new);
  1433. c->page = new;
  1434. goto load_freelist;
  1435. }
  1436. if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
  1437. slab_out_of_memory(s, gfpflags, node);
  1438. return NULL;
  1439. debug:
  1440. if (!alloc_debug_processing(s, c->page, object, addr))
  1441. goto another_slab;
  1442. c->page->inuse++;
  1443. c->page->freelist = object[c->offset];
  1444. c->node = -1;
  1445. goto unlock_out;
  1446. }
  1447. /*
  1448. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1449. * have the fastpath folded into their functions. So no function call
  1450. * overhead for requests that can be satisfied on the fastpath.
  1451. *
  1452. * The fastpath works by first checking if the lockless freelist can be used.
  1453. * If not then __slab_alloc is called for slow processing.
  1454. *
  1455. * Otherwise we can simply pick the next object from the lockless free list.
  1456. */
  1457. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1458. gfp_t gfpflags, int node, unsigned long addr)
  1459. {
  1460. void **object;
  1461. struct kmem_cache_cpu *c;
  1462. unsigned long flags;
  1463. unsigned int objsize;
  1464. gfpflags &= gfp_allowed_mask;
  1465. lockdep_trace_alloc(gfpflags);
  1466. might_sleep_if(gfpflags & __GFP_WAIT);
  1467. if (should_failslab(s->objsize, gfpflags))
  1468. return NULL;
  1469. local_irq_save(flags);
  1470. c = get_cpu_slab(s, smp_processor_id());
  1471. objsize = c->objsize;
  1472. if (unlikely(!c->freelist || !node_match(c, node)))
  1473. object = __slab_alloc(s, gfpflags, node, addr, c);
  1474. else {
  1475. object = c->freelist;
  1476. c->freelist = object[c->offset];
  1477. stat(c, ALLOC_FASTPATH);
  1478. }
  1479. local_irq_restore(flags);
  1480. if (unlikely((gfpflags & __GFP_ZERO) && object))
  1481. memset(object, 0, objsize);
  1482. kmemcheck_slab_alloc(s, gfpflags, object, c->objsize);
  1483. kmemleak_alloc_recursive(object, objsize, 1, s->flags, gfpflags);
  1484. return object;
  1485. }
  1486. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1487. {
  1488. void *ret = slab_alloc(s, gfpflags, -1, _RET_IP_);
  1489. trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
  1490. return ret;
  1491. }
  1492. EXPORT_SYMBOL(kmem_cache_alloc);
  1493. #ifdef CONFIG_KMEMTRACE
  1494. void *kmem_cache_alloc_notrace(struct kmem_cache *s, gfp_t gfpflags)
  1495. {
  1496. return slab_alloc(s, gfpflags, -1, _RET_IP_);
  1497. }
  1498. EXPORT_SYMBOL(kmem_cache_alloc_notrace);
  1499. #endif
  1500. #ifdef CONFIG_NUMA
  1501. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1502. {
  1503. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1504. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  1505. s->objsize, s->size, gfpflags, node);
  1506. return ret;
  1507. }
  1508. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1509. #endif
  1510. #ifdef CONFIG_KMEMTRACE
  1511. void *kmem_cache_alloc_node_notrace(struct kmem_cache *s,
  1512. gfp_t gfpflags,
  1513. int node)
  1514. {
  1515. return slab_alloc(s, gfpflags, node, _RET_IP_);
  1516. }
  1517. EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
  1518. #endif
  1519. /*
  1520. * Slow patch handling. This may still be called frequently since objects
  1521. * have a longer lifetime than the cpu slabs in most processing loads.
  1522. *
  1523. * So we still attempt to reduce cache line usage. Just take the slab
  1524. * lock and free the item. If there is no additional partial page
  1525. * handling required then we can return immediately.
  1526. */
  1527. static void __slab_free(struct kmem_cache *s, struct page *page,
  1528. void *x, unsigned long addr, unsigned int offset)
  1529. {
  1530. void *prior;
  1531. void **object = (void *)x;
  1532. struct kmem_cache_cpu *c;
  1533. c = get_cpu_slab(s, raw_smp_processor_id());
  1534. stat(c, FREE_SLOWPATH);
  1535. slab_lock(page);
  1536. if (unlikely(SLABDEBUG && PageSlubDebug(page)))
  1537. goto debug;
  1538. checks_ok:
  1539. prior = object[offset] = page->freelist;
  1540. page->freelist = object;
  1541. page->inuse--;
  1542. if (unlikely(PageSlubFrozen(page))) {
  1543. stat(c, FREE_FROZEN);
  1544. goto out_unlock;
  1545. }
  1546. if (unlikely(!page->inuse))
  1547. goto slab_empty;
  1548. /*
  1549. * Objects left in the slab. If it was not on the partial list before
  1550. * then add it.
  1551. */
  1552. if (unlikely(!prior)) {
  1553. add_partial(get_node(s, page_to_nid(page)), page, 1);
  1554. stat(c, FREE_ADD_PARTIAL);
  1555. }
  1556. out_unlock:
  1557. slab_unlock(page);
  1558. return;
  1559. slab_empty:
  1560. if (prior) {
  1561. /*
  1562. * Slab still on the partial list.
  1563. */
  1564. remove_partial(s, page);
  1565. stat(c, FREE_REMOVE_PARTIAL);
  1566. }
  1567. slab_unlock(page);
  1568. stat(c, FREE_SLAB);
  1569. discard_slab(s, page);
  1570. return;
  1571. debug:
  1572. if (!free_debug_processing(s, page, x, addr))
  1573. goto out_unlock;
  1574. goto checks_ok;
  1575. }
  1576. /*
  1577. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  1578. * can perform fastpath freeing without additional function calls.
  1579. *
  1580. * The fastpath is only possible if we are freeing to the current cpu slab
  1581. * of this processor. This typically the case if we have just allocated
  1582. * the item before.
  1583. *
  1584. * If fastpath is not possible then fall back to __slab_free where we deal
  1585. * with all sorts of special processing.
  1586. */
  1587. static __always_inline void slab_free(struct kmem_cache *s,
  1588. struct page *page, void *x, unsigned long addr)
  1589. {
  1590. void **object = (void *)x;
  1591. struct kmem_cache_cpu *c;
  1592. unsigned long flags;
  1593. kmemleak_free_recursive(x, s->flags);
  1594. local_irq_save(flags);
  1595. c = get_cpu_slab(s, smp_processor_id());
  1596. kmemcheck_slab_free(s, object, c->objsize);
  1597. debug_check_no_locks_freed(object, c->objsize);
  1598. if (!(s->flags & SLAB_DEBUG_OBJECTS))
  1599. debug_check_no_obj_freed(object, c->objsize);
  1600. if (likely(page == c->page && c->node >= 0)) {
  1601. object[c->offset] = c->freelist;
  1602. c->freelist = object;
  1603. stat(c, FREE_FASTPATH);
  1604. } else
  1605. __slab_free(s, page, x, addr, c->offset);
  1606. local_irq_restore(flags);
  1607. }
  1608. void kmem_cache_free(struct kmem_cache *s, void *x)
  1609. {
  1610. struct page *page;
  1611. page = virt_to_head_page(x);
  1612. slab_free(s, page, x, _RET_IP_);
  1613. trace_kmem_cache_free(_RET_IP_, x);
  1614. }
  1615. EXPORT_SYMBOL(kmem_cache_free);
  1616. /* Figure out on which slab page the object resides */
  1617. static struct page *get_object_page(const void *x)
  1618. {
  1619. struct page *page = virt_to_head_page(x);
  1620. if (!PageSlab(page))
  1621. return NULL;
  1622. return page;
  1623. }
  1624. /*
  1625. * Object placement in a slab is made very easy because we always start at
  1626. * offset 0. If we tune the size of the object to the alignment then we can
  1627. * get the required alignment by putting one properly sized object after
  1628. * another.
  1629. *
  1630. * Notice that the allocation order determines the sizes of the per cpu
  1631. * caches. Each processor has always one slab available for allocations.
  1632. * Increasing the allocation order reduces the number of times that slabs
  1633. * must be moved on and off the partial lists and is therefore a factor in
  1634. * locking overhead.
  1635. */
  1636. /*
  1637. * Mininum / Maximum order of slab pages. This influences locking overhead
  1638. * and slab fragmentation. A higher order reduces the number of partial slabs
  1639. * and increases the number of allocations possible without having to
  1640. * take the list_lock.
  1641. */
  1642. static int slub_min_order;
  1643. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  1644. static int slub_min_objects;
  1645. /*
  1646. * Merge control. If this is set then no merging of slab caches will occur.
  1647. * (Could be removed. This was introduced to pacify the merge skeptics.)
  1648. */
  1649. static int slub_nomerge;
  1650. /*
  1651. * Calculate the order of allocation given an slab object size.
  1652. *
  1653. * The order of allocation has significant impact on performance and other
  1654. * system components. Generally order 0 allocations should be preferred since
  1655. * order 0 does not cause fragmentation in the page allocator. Larger objects
  1656. * be problematic to put into order 0 slabs because there may be too much
  1657. * unused space left. We go to a higher order if more than 1/16th of the slab
  1658. * would be wasted.
  1659. *
  1660. * In order to reach satisfactory performance we must ensure that a minimum
  1661. * number of objects is in one slab. Otherwise we may generate too much
  1662. * activity on the partial lists which requires taking the list_lock. This is
  1663. * less a concern for large slabs though which are rarely used.
  1664. *
  1665. * slub_max_order specifies the order where we begin to stop considering the
  1666. * number of objects in a slab as critical. If we reach slub_max_order then
  1667. * we try to keep the page order as low as possible. So we accept more waste
  1668. * of space in favor of a small page order.
  1669. *
  1670. * Higher order allocations also allow the placement of more objects in a
  1671. * slab and thereby reduce object handling overhead. If the user has
  1672. * requested a higher mininum order then we start with that one instead of
  1673. * the smallest order which will fit the object.
  1674. */
  1675. static inline int slab_order(int size, int min_objects,
  1676. int max_order, int fract_leftover)
  1677. {
  1678. int order;
  1679. int rem;
  1680. int min_order = slub_min_order;
  1681. if ((PAGE_SIZE << min_order) / size > MAX_OBJS_PER_PAGE)
  1682. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  1683. for (order = max(min_order,
  1684. fls(min_objects * size - 1) - PAGE_SHIFT);
  1685. order <= max_order; order++) {
  1686. unsigned long slab_size = PAGE_SIZE << order;
  1687. if (slab_size < min_objects * size)
  1688. continue;
  1689. rem = slab_size % size;
  1690. if (rem <= slab_size / fract_leftover)
  1691. break;
  1692. }
  1693. return order;
  1694. }
  1695. static inline int calculate_order(int size)
  1696. {
  1697. int order;
  1698. int min_objects;
  1699. int fraction;
  1700. int max_objects;
  1701. /*
  1702. * Attempt to find best configuration for a slab. This
  1703. * works by first attempting to generate a layout with
  1704. * the best configuration and backing off gradually.
  1705. *
  1706. * First we reduce the acceptable waste in a slab. Then
  1707. * we reduce the minimum objects required in a slab.
  1708. */
  1709. min_objects = slub_min_objects;
  1710. if (!min_objects)
  1711. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  1712. max_objects = (PAGE_SIZE << slub_max_order)/size;
  1713. min_objects = min(min_objects, max_objects);
  1714. while (min_objects > 1) {
  1715. fraction = 16;
  1716. while (fraction >= 4) {
  1717. order = slab_order(size, min_objects,
  1718. slub_max_order, fraction);
  1719. if (order <= slub_max_order)
  1720. return order;
  1721. fraction /= 2;
  1722. }
  1723. min_objects--;
  1724. }
  1725. /*
  1726. * We were unable to place multiple objects in a slab. Now
  1727. * lets see if we can place a single object there.
  1728. */
  1729. order = slab_order(size, 1, slub_max_order, 1);
  1730. if (order <= slub_max_order)
  1731. return order;
  1732. /*
  1733. * Doh this slab cannot be placed using slub_max_order.
  1734. */
  1735. order = slab_order(size, 1, MAX_ORDER, 1);
  1736. if (order < MAX_ORDER)
  1737. return order;
  1738. return -ENOSYS;
  1739. }
  1740. /*
  1741. * Figure out what the alignment of the objects will be.
  1742. */
  1743. static unsigned long calculate_alignment(unsigned long flags,
  1744. unsigned long align, unsigned long size)
  1745. {
  1746. /*
  1747. * If the user wants hardware cache aligned objects then follow that
  1748. * suggestion if the object is sufficiently large.
  1749. *
  1750. * The hardware cache alignment cannot override the specified
  1751. * alignment though. If that is greater then use it.
  1752. */
  1753. if (flags & SLAB_HWCACHE_ALIGN) {
  1754. unsigned long ralign = cache_line_size();
  1755. while (size <= ralign / 2)
  1756. ralign /= 2;
  1757. align = max(align, ralign);
  1758. }
  1759. if (align < ARCH_SLAB_MINALIGN)
  1760. align = ARCH_SLAB_MINALIGN;
  1761. return ALIGN(align, sizeof(void *));
  1762. }
  1763. static void init_kmem_cache_cpu(struct kmem_cache *s,
  1764. struct kmem_cache_cpu *c)
  1765. {
  1766. c->page = NULL;
  1767. c->freelist = NULL;
  1768. c->node = 0;
  1769. c->offset = s->offset / sizeof(void *);
  1770. c->objsize = s->objsize;
  1771. #ifdef CONFIG_SLUB_STATS
  1772. memset(c->stat, 0, NR_SLUB_STAT_ITEMS * sizeof(unsigned));
  1773. #endif
  1774. }
  1775. static void
  1776. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  1777. {
  1778. n->nr_partial = 0;
  1779. spin_lock_init(&n->list_lock);
  1780. INIT_LIST_HEAD(&n->partial);
  1781. #ifdef CONFIG_SLUB_DEBUG
  1782. atomic_long_set(&n->nr_slabs, 0);
  1783. atomic_long_set(&n->total_objects, 0);
  1784. INIT_LIST_HEAD(&n->full);
  1785. #endif
  1786. }
  1787. #ifdef CONFIG_SMP
  1788. /*
  1789. * Per cpu array for per cpu structures.
  1790. *
  1791. * The per cpu array places all kmem_cache_cpu structures from one processor
  1792. * close together meaning that it becomes possible that multiple per cpu
  1793. * structures are contained in one cacheline. This may be particularly
  1794. * beneficial for the kmalloc caches.
  1795. *
  1796. * A desktop system typically has around 60-80 slabs. With 100 here we are
  1797. * likely able to get per cpu structures for all caches from the array defined
  1798. * here. We must be able to cover all kmalloc caches during bootstrap.
  1799. *
  1800. * If the per cpu array is exhausted then fall back to kmalloc
  1801. * of individual cachelines. No sharing is possible then.
  1802. */
  1803. #define NR_KMEM_CACHE_CPU 100
  1804. static DEFINE_PER_CPU(struct kmem_cache_cpu [NR_KMEM_CACHE_CPU],
  1805. kmem_cache_cpu);
  1806. static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
  1807. static DECLARE_BITMAP(kmem_cach_cpu_free_init_once, CONFIG_NR_CPUS);
  1808. static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
  1809. int cpu, gfp_t flags)
  1810. {
  1811. struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
  1812. if (c)
  1813. per_cpu(kmem_cache_cpu_free, cpu) =
  1814. (void *)c->freelist;
  1815. else {
  1816. /* Table overflow: So allocate ourselves */
  1817. c = kmalloc_node(
  1818. ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
  1819. flags, cpu_to_node(cpu));
  1820. if (!c)
  1821. return NULL;
  1822. }
  1823. init_kmem_cache_cpu(s, c);
  1824. return c;
  1825. }
  1826. static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
  1827. {
  1828. if (c < per_cpu(kmem_cache_cpu, cpu) ||
  1829. c >= per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
  1830. kfree(c);
  1831. return;
  1832. }
  1833. c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
  1834. per_cpu(kmem_cache_cpu_free, cpu) = c;
  1835. }
  1836. static void free_kmem_cache_cpus(struct kmem_cache *s)
  1837. {
  1838. int cpu;
  1839. for_each_online_cpu(cpu) {
  1840. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1841. if (c) {
  1842. s->cpu_slab[cpu] = NULL;
  1843. free_kmem_cache_cpu(c, cpu);
  1844. }
  1845. }
  1846. }
  1847. static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1848. {
  1849. int cpu;
  1850. for_each_online_cpu(cpu) {
  1851. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1852. if (c)
  1853. continue;
  1854. c = alloc_kmem_cache_cpu(s, cpu, flags);
  1855. if (!c) {
  1856. free_kmem_cache_cpus(s);
  1857. return 0;
  1858. }
  1859. s->cpu_slab[cpu] = c;
  1860. }
  1861. return 1;
  1862. }
  1863. /*
  1864. * Initialize the per cpu array.
  1865. */
  1866. static void init_alloc_cpu_cpu(int cpu)
  1867. {
  1868. int i;
  1869. if (cpumask_test_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once)))
  1870. return;
  1871. for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
  1872. free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
  1873. cpumask_set_cpu(cpu, to_cpumask(kmem_cach_cpu_free_init_once));
  1874. }
  1875. static void __init init_alloc_cpu(void)
  1876. {
  1877. int cpu;
  1878. for_each_online_cpu(cpu)
  1879. init_alloc_cpu_cpu(cpu);
  1880. }
  1881. #else
  1882. static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
  1883. static inline void init_alloc_cpu(void) {}
  1884. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1885. {
  1886. init_kmem_cache_cpu(s, &s->cpu_slab);
  1887. return 1;
  1888. }
  1889. #endif
  1890. #ifdef CONFIG_NUMA
  1891. /*
  1892. * No kmalloc_node yet so do it by hand. We know that this is the first
  1893. * slab on the node for this slabcache. There are no concurrent accesses
  1894. * possible.
  1895. *
  1896. * Note that this function only works on the kmalloc_node_cache
  1897. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  1898. * memory on a fresh node that has no slab structures yet.
  1899. */
  1900. static void early_kmem_cache_node_alloc(gfp_t gfpflags, int node)
  1901. {
  1902. struct page *page;
  1903. struct kmem_cache_node *n;
  1904. unsigned long flags;
  1905. BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
  1906. page = new_slab(kmalloc_caches, gfpflags, node);
  1907. BUG_ON(!page);
  1908. if (page_to_nid(page) != node) {
  1909. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  1910. "node %d\n", node);
  1911. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  1912. "in order to be able to continue\n");
  1913. }
  1914. n = page->freelist;
  1915. BUG_ON(!n);
  1916. page->freelist = get_freepointer(kmalloc_caches, n);
  1917. page->inuse++;
  1918. kmalloc_caches->node[node] = n;
  1919. #ifdef CONFIG_SLUB_DEBUG
  1920. init_object(kmalloc_caches, n, 1);
  1921. init_tracking(kmalloc_caches, n);
  1922. #endif
  1923. init_kmem_cache_node(n, kmalloc_caches);
  1924. inc_slabs_node(kmalloc_caches, node, page->objects);
  1925. /*
  1926. * lockdep requires consistent irq usage for each lock
  1927. * so even though there cannot be a race this early in
  1928. * the boot sequence, we still disable irqs.
  1929. */
  1930. local_irq_save(flags);
  1931. add_partial(n, page, 0);
  1932. local_irq_restore(flags);
  1933. }
  1934. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1935. {
  1936. int node;
  1937. for_each_node_state(node, N_NORMAL_MEMORY) {
  1938. struct kmem_cache_node *n = s->node[node];
  1939. if (n && n != &s->local_node)
  1940. kmem_cache_free(kmalloc_caches, n);
  1941. s->node[node] = NULL;
  1942. }
  1943. }
  1944. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1945. {
  1946. int node;
  1947. int local_node;
  1948. if (slab_state >= UP)
  1949. local_node = page_to_nid(virt_to_page(s));
  1950. else
  1951. local_node = 0;
  1952. for_each_node_state(node, N_NORMAL_MEMORY) {
  1953. struct kmem_cache_node *n;
  1954. if (local_node == node)
  1955. n = &s->local_node;
  1956. else {
  1957. if (slab_state == DOWN) {
  1958. early_kmem_cache_node_alloc(gfpflags, node);
  1959. continue;
  1960. }
  1961. n = kmem_cache_alloc_node(kmalloc_caches,
  1962. gfpflags, node);
  1963. if (!n) {
  1964. free_kmem_cache_nodes(s);
  1965. return 0;
  1966. }
  1967. }
  1968. s->node[node] = n;
  1969. init_kmem_cache_node(n, s);
  1970. }
  1971. return 1;
  1972. }
  1973. #else
  1974. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1975. {
  1976. }
  1977. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1978. {
  1979. init_kmem_cache_node(&s->local_node, s);
  1980. return 1;
  1981. }
  1982. #endif
  1983. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  1984. {
  1985. if (min < MIN_PARTIAL)
  1986. min = MIN_PARTIAL;
  1987. else if (min > MAX_PARTIAL)
  1988. min = MAX_PARTIAL;
  1989. s->min_partial = min;
  1990. }
  1991. /*
  1992. * calculate_sizes() determines the order and the distribution of data within
  1993. * a slab object.
  1994. */
  1995. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  1996. {
  1997. unsigned long flags = s->flags;
  1998. unsigned long size = s->objsize;
  1999. unsigned long align = s->align;
  2000. int order;
  2001. /*
  2002. * Round up object size to the next word boundary. We can only
  2003. * place the free pointer at word boundaries and this determines
  2004. * the possible location of the free pointer.
  2005. */
  2006. size = ALIGN(size, sizeof(void *));
  2007. #ifdef CONFIG_SLUB_DEBUG
  2008. /*
  2009. * Determine if we can poison the object itself. If the user of
  2010. * the slab may touch the object after free or before allocation
  2011. * then we should never poison the object itself.
  2012. */
  2013. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  2014. !s->ctor)
  2015. s->flags |= __OBJECT_POISON;
  2016. else
  2017. s->flags &= ~__OBJECT_POISON;
  2018. /*
  2019. * If we are Redzoning then check if there is some space between the
  2020. * end of the object and the free pointer. If not then add an
  2021. * additional word to have some bytes to store Redzone information.
  2022. */
  2023. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  2024. size += sizeof(void *);
  2025. #endif
  2026. /*
  2027. * With that we have determined the number of bytes in actual use
  2028. * by the object. This is the potential offset to the free pointer.
  2029. */
  2030. s->inuse = size;
  2031. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  2032. s->ctor)) {
  2033. /*
  2034. * Relocate free pointer after the object if it is not
  2035. * permitted to overwrite the first word of the object on
  2036. * kmem_cache_free.
  2037. *
  2038. * This is the case if we do RCU, have a constructor or
  2039. * destructor or are poisoning the objects.
  2040. */
  2041. s->offset = size;
  2042. size += sizeof(void *);
  2043. }
  2044. #ifdef CONFIG_SLUB_DEBUG
  2045. if (flags & SLAB_STORE_USER)
  2046. /*
  2047. * Need to store information about allocs and frees after
  2048. * the object.
  2049. */
  2050. size += 2 * sizeof(struct track);
  2051. if (flags & SLAB_RED_ZONE)
  2052. /*
  2053. * Add some empty padding so that we can catch
  2054. * overwrites from earlier objects rather than let
  2055. * tracking information or the free pointer be
  2056. * corrupted if a user writes before the start
  2057. * of the object.
  2058. */
  2059. size += sizeof(void *);
  2060. #endif
  2061. /*
  2062. * Determine the alignment based on various parameters that the
  2063. * user specified and the dynamic determination of cache line size
  2064. * on bootup.
  2065. */
  2066. align = calculate_alignment(flags, align, s->objsize);
  2067. s->align = align;
  2068. /*
  2069. * SLUB stores one object immediately after another beginning from
  2070. * offset 0. In order to align the objects we have to simply size
  2071. * each object to conform to the alignment.
  2072. */
  2073. size = ALIGN(size, align);
  2074. s->size = size;
  2075. if (forced_order >= 0)
  2076. order = forced_order;
  2077. else
  2078. order = calculate_order(size);
  2079. if (order < 0)
  2080. return 0;
  2081. s->allocflags = 0;
  2082. if (order)
  2083. s->allocflags |= __GFP_COMP;
  2084. if (s->flags & SLAB_CACHE_DMA)
  2085. s->allocflags |= SLUB_DMA;
  2086. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  2087. s->allocflags |= __GFP_RECLAIMABLE;
  2088. /*
  2089. * Determine the number of objects per slab
  2090. */
  2091. s->oo = oo_make(order, size);
  2092. s->min = oo_make(get_order(size), size);
  2093. if (oo_objects(s->oo) > oo_objects(s->max))
  2094. s->max = s->oo;
  2095. return !!oo_objects(s->oo);
  2096. }
  2097. static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
  2098. const char *name, size_t size,
  2099. size_t align, unsigned long flags,
  2100. void (*ctor)(void *))
  2101. {
  2102. memset(s, 0, kmem_size);
  2103. s->name = name;
  2104. s->ctor = ctor;
  2105. s->objsize = size;
  2106. s->align = align;
  2107. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2108. if (!calculate_sizes(s, -1))
  2109. goto error;
  2110. if (disable_higher_order_debug) {
  2111. /*
  2112. * Disable debugging flags that store metadata if the min slab
  2113. * order increased.
  2114. */
  2115. if (get_order(s->size) > get_order(s->objsize)) {
  2116. s->flags &= ~DEBUG_METADATA_FLAGS;
  2117. s->offset = 0;
  2118. if (!calculate_sizes(s, -1))
  2119. goto error;
  2120. }
  2121. }
  2122. /*
  2123. * The larger the object size is, the more pages we want on the partial
  2124. * list to avoid pounding the page allocator excessively.
  2125. */
  2126. set_min_partial(s, ilog2(s->size));
  2127. s->refcount = 1;
  2128. #ifdef CONFIG_NUMA
  2129. s->remote_node_defrag_ratio = 1000;
  2130. #endif
  2131. if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
  2132. goto error;
  2133. if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
  2134. return 1;
  2135. free_kmem_cache_nodes(s);
  2136. error:
  2137. if (flags & SLAB_PANIC)
  2138. panic("Cannot create slab %s size=%lu realsize=%u "
  2139. "order=%u offset=%u flags=%lx\n",
  2140. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2141. s->offset, flags);
  2142. return 0;
  2143. }
  2144. /*
  2145. * Check if a given pointer is valid
  2146. */
  2147. int kmem_ptr_validate(struct kmem_cache *s, const void *object)
  2148. {
  2149. struct page *page;
  2150. page = get_object_page(object);
  2151. if (!page || s != page->slab)
  2152. /* No slab or wrong slab */
  2153. return 0;
  2154. if (!check_valid_pointer(s, page, object))
  2155. return 0;
  2156. /*
  2157. * We could also check if the object is on the slabs freelist.
  2158. * But this would be too expensive and it seems that the main
  2159. * purpose of kmem_ptr_valid() is to check if the object belongs
  2160. * to a certain slab.
  2161. */
  2162. return 1;
  2163. }
  2164. EXPORT_SYMBOL(kmem_ptr_validate);
  2165. /*
  2166. * Determine the size of a slab object
  2167. */
  2168. unsigned int kmem_cache_size(struct kmem_cache *s)
  2169. {
  2170. return s->objsize;
  2171. }
  2172. EXPORT_SYMBOL(kmem_cache_size);
  2173. const char *kmem_cache_name(struct kmem_cache *s)
  2174. {
  2175. return s->name;
  2176. }
  2177. EXPORT_SYMBOL(kmem_cache_name);
  2178. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2179. const char *text)
  2180. {
  2181. #ifdef CONFIG_SLUB_DEBUG
  2182. void *addr = page_address(page);
  2183. void *p;
  2184. DECLARE_BITMAP(map, page->objects);
  2185. bitmap_zero(map, page->objects);
  2186. slab_err(s, page, "%s", text);
  2187. slab_lock(page);
  2188. for_each_free_object(p, s, page->freelist)
  2189. set_bit(slab_index(p, s, addr), map);
  2190. for_each_object(p, s, addr, page->objects) {
  2191. if (!test_bit(slab_index(p, s, addr), map)) {
  2192. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2193. p, p - addr);
  2194. print_tracking(s, p);
  2195. }
  2196. }
  2197. slab_unlock(page);
  2198. #endif
  2199. }
  2200. /*
  2201. * Attempt to free all partial slabs on a node.
  2202. */
  2203. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2204. {
  2205. unsigned long flags;
  2206. struct page *page, *h;
  2207. spin_lock_irqsave(&n->list_lock, flags);
  2208. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2209. if (!page->inuse) {
  2210. list_del(&page->lru);
  2211. discard_slab(s, page);
  2212. n->nr_partial--;
  2213. } else {
  2214. list_slab_objects(s, page,
  2215. "Objects remaining on kmem_cache_close()");
  2216. }
  2217. }
  2218. spin_unlock_irqrestore(&n->list_lock, flags);
  2219. }
  2220. /*
  2221. * Release all resources used by a slab cache.
  2222. */
  2223. static inline int kmem_cache_close(struct kmem_cache *s)
  2224. {
  2225. int node;
  2226. flush_all(s);
  2227. /* Attempt to free all objects */
  2228. free_kmem_cache_cpus(s);
  2229. for_each_node_state(node, N_NORMAL_MEMORY) {
  2230. struct kmem_cache_node *n = get_node(s, node);
  2231. free_partial(s, n);
  2232. if (n->nr_partial || slabs_node(s, node))
  2233. return 1;
  2234. }
  2235. free_kmem_cache_nodes(s);
  2236. return 0;
  2237. }
  2238. /*
  2239. * Close a cache and release the kmem_cache structure
  2240. * (must be used for caches created using kmem_cache_create)
  2241. */
  2242. void kmem_cache_destroy(struct kmem_cache *s)
  2243. {
  2244. down_write(&slub_lock);
  2245. s->refcount--;
  2246. if (!s->refcount) {
  2247. list_del(&s->list);
  2248. up_write(&slub_lock);
  2249. if (kmem_cache_close(s)) {
  2250. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2251. "still has objects.\n", s->name, __func__);
  2252. dump_stack();
  2253. }
  2254. if (s->flags & SLAB_DESTROY_BY_RCU)
  2255. rcu_barrier();
  2256. sysfs_slab_remove(s);
  2257. } else
  2258. up_write(&slub_lock);
  2259. }
  2260. EXPORT_SYMBOL(kmem_cache_destroy);
  2261. /********************************************************************
  2262. * Kmalloc subsystem
  2263. *******************************************************************/
  2264. struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT] __cacheline_aligned;
  2265. EXPORT_SYMBOL(kmalloc_caches);
  2266. static int __init setup_slub_min_order(char *str)
  2267. {
  2268. get_option(&str, &slub_min_order);
  2269. return 1;
  2270. }
  2271. __setup("slub_min_order=", setup_slub_min_order);
  2272. static int __init setup_slub_max_order(char *str)
  2273. {
  2274. get_option(&str, &slub_max_order);
  2275. slub_max_order = min(slub_max_order, MAX_ORDER - 1);
  2276. return 1;
  2277. }
  2278. __setup("slub_max_order=", setup_slub_max_order);
  2279. static int __init setup_slub_min_objects(char *str)
  2280. {
  2281. get_option(&str, &slub_min_objects);
  2282. return 1;
  2283. }
  2284. __setup("slub_min_objects=", setup_slub_min_objects);
  2285. static int __init setup_slub_nomerge(char *str)
  2286. {
  2287. slub_nomerge = 1;
  2288. return 1;
  2289. }
  2290. __setup("slub_nomerge", setup_slub_nomerge);
  2291. static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
  2292. const char *name, int size, gfp_t gfp_flags)
  2293. {
  2294. unsigned int flags = 0;
  2295. if (gfp_flags & SLUB_DMA)
  2296. flags = SLAB_CACHE_DMA;
  2297. /*
  2298. * This function is called with IRQs disabled during early-boot on
  2299. * single CPU so there's no need to take slub_lock here.
  2300. */
  2301. if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
  2302. flags, NULL))
  2303. goto panic;
  2304. list_add(&s->list, &slab_caches);
  2305. if (sysfs_slab_add(s))
  2306. goto panic;
  2307. return s;
  2308. panic:
  2309. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2310. }
  2311. #ifdef CONFIG_ZONE_DMA
  2312. static struct kmem_cache *kmalloc_caches_dma[SLUB_PAGE_SHIFT];
  2313. static void sysfs_add_func(struct work_struct *w)
  2314. {
  2315. struct kmem_cache *s;
  2316. down_write(&slub_lock);
  2317. list_for_each_entry(s, &slab_caches, list) {
  2318. if (s->flags & __SYSFS_ADD_DEFERRED) {
  2319. s->flags &= ~__SYSFS_ADD_DEFERRED;
  2320. sysfs_slab_add(s);
  2321. }
  2322. }
  2323. up_write(&slub_lock);
  2324. }
  2325. static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
  2326. static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
  2327. {
  2328. struct kmem_cache *s;
  2329. char *text;
  2330. size_t realsize;
  2331. unsigned long slabflags;
  2332. s = kmalloc_caches_dma[index];
  2333. if (s)
  2334. return s;
  2335. /* Dynamically create dma cache */
  2336. if (flags & __GFP_WAIT)
  2337. down_write(&slub_lock);
  2338. else {
  2339. if (!down_write_trylock(&slub_lock))
  2340. goto out;
  2341. }
  2342. if (kmalloc_caches_dma[index])
  2343. goto unlock_out;
  2344. realsize = kmalloc_caches[index].objsize;
  2345. text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
  2346. (unsigned int)realsize);
  2347. s = kmalloc(kmem_size, flags & ~SLUB_DMA);
  2348. /*
  2349. * Must defer sysfs creation to a workqueue because we don't know
  2350. * what context we are called from. Before sysfs comes up, we don't
  2351. * need to do anything because our sysfs initcall will start by
  2352. * adding all existing slabs to sysfs.
  2353. */
  2354. slabflags = SLAB_CACHE_DMA|SLAB_NOTRACK;
  2355. if (slab_state >= SYSFS)
  2356. slabflags |= __SYSFS_ADD_DEFERRED;
  2357. if (!s || !text || !kmem_cache_open(s, flags, text,
  2358. realsize, ARCH_KMALLOC_MINALIGN, slabflags, NULL)) {
  2359. kfree(s);
  2360. kfree(text);
  2361. goto unlock_out;
  2362. }
  2363. list_add(&s->list, &slab_caches);
  2364. kmalloc_caches_dma[index] = s;
  2365. if (slab_state >= SYSFS)
  2366. schedule_work(&sysfs_add_work);
  2367. unlock_out:
  2368. up_write(&slub_lock);
  2369. out:
  2370. return kmalloc_caches_dma[index];
  2371. }
  2372. #endif
  2373. /*
  2374. * Conversion table for small slabs sizes / 8 to the index in the
  2375. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2376. * of two cache sizes there. The size of larger slabs can be determined using
  2377. * fls.
  2378. */
  2379. static s8 size_index[24] = {
  2380. 3, /* 8 */
  2381. 4, /* 16 */
  2382. 5, /* 24 */
  2383. 5, /* 32 */
  2384. 6, /* 40 */
  2385. 6, /* 48 */
  2386. 6, /* 56 */
  2387. 6, /* 64 */
  2388. 1, /* 72 */
  2389. 1, /* 80 */
  2390. 1, /* 88 */
  2391. 1, /* 96 */
  2392. 7, /* 104 */
  2393. 7, /* 112 */
  2394. 7, /* 120 */
  2395. 7, /* 128 */
  2396. 2, /* 136 */
  2397. 2, /* 144 */
  2398. 2, /* 152 */
  2399. 2, /* 160 */
  2400. 2, /* 168 */
  2401. 2, /* 176 */
  2402. 2, /* 184 */
  2403. 2 /* 192 */
  2404. };
  2405. static inline int size_index_elem(size_t bytes)
  2406. {
  2407. return (bytes - 1) / 8;
  2408. }
  2409. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2410. {
  2411. int index;
  2412. if (size <= 192) {
  2413. if (!size)
  2414. return ZERO_SIZE_PTR;
  2415. index = size_index[size_index_elem(size)];
  2416. } else
  2417. index = fls(size - 1);
  2418. #ifdef CONFIG_ZONE_DMA
  2419. if (unlikely((flags & SLUB_DMA)))
  2420. return dma_kmalloc_cache(index, flags);
  2421. #endif
  2422. return &kmalloc_caches[index];
  2423. }
  2424. void *__kmalloc(size_t size, gfp_t flags)
  2425. {
  2426. struct kmem_cache *s;
  2427. void *ret;
  2428. if (unlikely(size > SLUB_MAX_SIZE))
  2429. return kmalloc_large(size, flags);
  2430. s = get_slab(size, flags);
  2431. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2432. return s;
  2433. ret = slab_alloc(s, flags, -1, _RET_IP_);
  2434. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  2435. return ret;
  2436. }
  2437. EXPORT_SYMBOL(__kmalloc);
  2438. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2439. {
  2440. struct page *page;
  2441. void *ptr = NULL;
  2442. flags |= __GFP_COMP | __GFP_NOTRACK;
  2443. page = alloc_pages_node(node, flags, get_order(size));
  2444. if (page)
  2445. ptr = page_address(page);
  2446. kmemleak_alloc(ptr, size, 1, flags);
  2447. return ptr;
  2448. }
  2449. #ifdef CONFIG_NUMA
  2450. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2451. {
  2452. struct kmem_cache *s;
  2453. void *ret;
  2454. if (unlikely(size > SLUB_MAX_SIZE)) {
  2455. ret = kmalloc_large_node(size, flags, node);
  2456. trace_kmalloc_node(_RET_IP_, ret,
  2457. size, PAGE_SIZE << get_order(size),
  2458. flags, node);
  2459. return ret;
  2460. }
  2461. s = get_slab(size, flags);
  2462. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2463. return s;
  2464. ret = slab_alloc(s, flags, node, _RET_IP_);
  2465. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  2466. return ret;
  2467. }
  2468. EXPORT_SYMBOL(__kmalloc_node);
  2469. #endif
  2470. size_t ksize(const void *object)
  2471. {
  2472. struct page *page;
  2473. struct kmem_cache *s;
  2474. if (unlikely(object == ZERO_SIZE_PTR))
  2475. return 0;
  2476. page = virt_to_head_page(object);
  2477. if (unlikely(!PageSlab(page))) {
  2478. WARN_ON(!PageCompound(page));
  2479. return PAGE_SIZE << compound_order(page);
  2480. }
  2481. s = page->slab;
  2482. #ifdef CONFIG_SLUB_DEBUG
  2483. /*
  2484. * Debugging requires use of the padding between object
  2485. * and whatever may come after it.
  2486. */
  2487. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  2488. return s->objsize;
  2489. #endif
  2490. /*
  2491. * If we have the need to store the freelist pointer
  2492. * back there or track user information then we can
  2493. * only use the space before that information.
  2494. */
  2495. if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
  2496. return s->inuse;
  2497. /*
  2498. * Else we can use all the padding etc for the allocation
  2499. */
  2500. return s->size;
  2501. }
  2502. EXPORT_SYMBOL(ksize);
  2503. void kfree(const void *x)
  2504. {
  2505. struct page *page;
  2506. void *object = (void *)x;
  2507. trace_kfree(_RET_IP_, x);
  2508. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2509. return;
  2510. page = virt_to_head_page(x);
  2511. if (unlikely(!PageSlab(page))) {
  2512. BUG_ON(!PageCompound(page));
  2513. kmemleak_free(x);
  2514. put_page(page);
  2515. return;
  2516. }
  2517. slab_free(page->slab, page, object, _RET_IP_);
  2518. }
  2519. EXPORT_SYMBOL(kfree);
  2520. /*
  2521. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2522. * the remaining slabs by the number of items in use. The slabs with the
  2523. * most items in use come first. New allocations will then fill those up
  2524. * and thus they can be removed from the partial lists.
  2525. *
  2526. * The slabs with the least items are placed last. This results in them
  2527. * being allocated from last increasing the chance that the last objects
  2528. * are freed in them.
  2529. */
  2530. int kmem_cache_shrink(struct kmem_cache *s)
  2531. {
  2532. int node;
  2533. int i;
  2534. struct kmem_cache_node *n;
  2535. struct page *page;
  2536. struct page *t;
  2537. int objects = oo_objects(s->max);
  2538. struct list_head *slabs_by_inuse =
  2539. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2540. unsigned long flags;
  2541. if (!slabs_by_inuse)
  2542. return -ENOMEM;
  2543. flush_all(s);
  2544. for_each_node_state(node, N_NORMAL_MEMORY) {
  2545. n = get_node(s, node);
  2546. if (!n->nr_partial)
  2547. continue;
  2548. for (i = 0; i < objects; i++)
  2549. INIT_LIST_HEAD(slabs_by_inuse + i);
  2550. spin_lock_irqsave(&n->list_lock, flags);
  2551. /*
  2552. * Build lists indexed by the items in use in each slab.
  2553. *
  2554. * Note that concurrent frees may occur while we hold the
  2555. * list_lock. page->inuse here is the upper limit.
  2556. */
  2557. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2558. if (!page->inuse && slab_trylock(page)) {
  2559. /*
  2560. * Must hold slab lock here because slab_free
  2561. * may have freed the last object and be
  2562. * waiting to release the slab.
  2563. */
  2564. list_del(&page->lru);
  2565. n->nr_partial--;
  2566. slab_unlock(page);
  2567. discard_slab(s, page);
  2568. } else {
  2569. list_move(&page->lru,
  2570. slabs_by_inuse + page->inuse);
  2571. }
  2572. }
  2573. /*
  2574. * Rebuild the partial list with the slabs filled up most
  2575. * first and the least used slabs at the end.
  2576. */
  2577. for (i = objects - 1; i >= 0; i--)
  2578. list_splice(slabs_by_inuse + i, n->partial.prev);
  2579. spin_unlock_irqrestore(&n->list_lock, flags);
  2580. }
  2581. kfree(slabs_by_inuse);
  2582. return 0;
  2583. }
  2584. EXPORT_SYMBOL(kmem_cache_shrink);
  2585. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  2586. static int slab_mem_going_offline_callback(void *arg)
  2587. {
  2588. struct kmem_cache *s;
  2589. down_read(&slub_lock);
  2590. list_for_each_entry(s, &slab_caches, list)
  2591. kmem_cache_shrink(s);
  2592. up_read(&slub_lock);
  2593. return 0;
  2594. }
  2595. static void slab_mem_offline_callback(void *arg)
  2596. {
  2597. struct kmem_cache_node *n;
  2598. struct kmem_cache *s;
  2599. struct memory_notify *marg = arg;
  2600. int offline_node;
  2601. offline_node = marg->status_change_nid;
  2602. /*
  2603. * If the node still has available memory. we need kmem_cache_node
  2604. * for it yet.
  2605. */
  2606. if (offline_node < 0)
  2607. return;
  2608. down_read(&slub_lock);
  2609. list_for_each_entry(s, &slab_caches, list) {
  2610. n = get_node(s, offline_node);
  2611. if (n) {
  2612. /*
  2613. * if n->nr_slabs > 0, slabs still exist on the node
  2614. * that is going down. We were unable to free them,
  2615. * and offline_pages() function shoudn't call this
  2616. * callback. So, we must fail.
  2617. */
  2618. BUG_ON(slabs_node(s, offline_node));
  2619. s->node[offline_node] = NULL;
  2620. kmem_cache_free(kmalloc_caches, n);
  2621. }
  2622. }
  2623. up_read(&slub_lock);
  2624. }
  2625. static int slab_mem_going_online_callback(void *arg)
  2626. {
  2627. struct kmem_cache_node *n;
  2628. struct kmem_cache *s;
  2629. struct memory_notify *marg = arg;
  2630. int nid = marg->status_change_nid;
  2631. int ret = 0;
  2632. /*
  2633. * If the node's memory is already available, then kmem_cache_node is
  2634. * already created. Nothing to do.
  2635. */
  2636. if (nid < 0)
  2637. return 0;
  2638. /*
  2639. * We are bringing a node online. No memory is available yet. We must
  2640. * allocate a kmem_cache_node structure in order to bring the node
  2641. * online.
  2642. */
  2643. down_read(&slub_lock);
  2644. list_for_each_entry(s, &slab_caches, list) {
  2645. /*
  2646. * XXX: kmem_cache_alloc_node will fallback to other nodes
  2647. * since memory is not yet available from the node that
  2648. * is brought up.
  2649. */
  2650. n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
  2651. if (!n) {
  2652. ret = -ENOMEM;
  2653. goto out;
  2654. }
  2655. init_kmem_cache_node(n, s);
  2656. s->node[nid] = n;
  2657. }
  2658. out:
  2659. up_read(&slub_lock);
  2660. return ret;
  2661. }
  2662. static int slab_memory_callback(struct notifier_block *self,
  2663. unsigned long action, void *arg)
  2664. {
  2665. int ret = 0;
  2666. switch (action) {
  2667. case MEM_GOING_ONLINE:
  2668. ret = slab_mem_going_online_callback(arg);
  2669. break;
  2670. case MEM_GOING_OFFLINE:
  2671. ret = slab_mem_going_offline_callback(arg);
  2672. break;
  2673. case MEM_OFFLINE:
  2674. case MEM_CANCEL_ONLINE:
  2675. slab_mem_offline_callback(arg);
  2676. break;
  2677. case MEM_ONLINE:
  2678. case MEM_CANCEL_OFFLINE:
  2679. break;
  2680. }
  2681. if (ret)
  2682. ret = notifier_from_errno(ret);
  2683. else
  2684. ret = NOTIFY_OK;
  2685. return ret;
  2686. }
  2687. #endif /* CONFIG_MEMORY_HOTPLUG */
  2688. /********************************************************************
  2689. * Basic setup of slabs
  2690. *******************************************************************/
  2691. void __init kmem_cache_init(void)
  2692. {
  2693. int i;
  2694. int caches = 0;
  2695. init_alloc_cpu();
  2696. #ifdef CONFIG_NUMA
  2697. /*
  2698. * Must first have the slab cache available for the allocations of the
  2699. * struct kmem_cache_node's. There is special bootstrap code in
  2700. * kmem_cache_open for slab_state == DOWN.
  2701. */
  2702. create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
  2703. sizeof(struct kmem_cache_node), GFP_NOWAIT);
  2704. kmalloc_caches[0].refcount = -1;
  2705. caches++;
  2706. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  2707. #endif
  2708. /* Able to allocate the per node structures */
  2709. slab_state = PARTIAL;
  2710. /* Caches that are not of the two-to-the-power-of size */
  2711. if (KMALLOC_MIN_SIZE <= 32) {
  2712. create_kmalloc_cache(&kmalloc_caches[1],
  2713. "kmalloc-96", 96, GFP_NOWAIT);
  2714. caches++;
  2715. }
  2716. if (KMALLOC_MIN_SIZE <= 64) {
  2717. create_kmalloc_cache(&kmalloc_caches[2],
  2718. "kmalloc-192", 192, GFP_NOWAIT);
  2719. caches++;
  2720. }
  2721. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  2722. create_kmalloc_cache(&kmalloc_caches[i],
  2723. "kmalloc", 1 << i, GFP_NOWAIT);
  2724. caches++;
  2725. }
  2726. /*
  2727. * Patch up the size_index table if we have strange large alignment
  2728. * requirements for the kmalloc array. This is only the case for
  2729. * MIPS it seems. The standard arches will not generate any code here.
  2730. *
  2731. * Largest permitted alignment is 256 bytes due to the way we
  2732. * handle the index determination for the smaller caches.
  2733. *
  2734. * Make sure that nothing crazy happens if someone starts tinkering
  2735. * around with ARCH_KMALLOC_MINALIGN
  2736. */
  2737. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  2738. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  2739. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  2740. int elem = size_index_elem(i);
  2741. if (elem >= ARRAY_SIZE(size_index))
  2742. break;
  2743. size_index[elem] = KMALLOC_SHIFT_LOW;
  2744. }
  2745. if (KMALLOC_MIN_SIZE == 64) {
  2746. /*
  2747. * The 96 byte size cache is not used if the alignment
  2748. * is 64 byte.
  2749. */
  2750. for (i = 64 + 8; i <= 96; i += 8)
  2751. size_index[size_index_elem(i)] = 7;
  2752. } else if (KMALLOC_MIN_SIZE == 128) {
  2753. /*
  2754. * The 192 byte sized cache is not used if the alignment
  2755. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  2756. * instead.
  2757. */
  2758. for (i = 128 + 8; i <= 192; i += 8)
  2759. size_index[size_index_elem(i)] = 8;
  2760. }
  2761. slab_state = UP;
  2762. /* Provide the correct kmalloc names now that the caches are up */
  2763. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++)
  2764. kmalloc_caches[i]. name =
  2765. kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
  2766. #ifdef CONFIG_SMP
  2767. register_cpu_notifier(&slab_notifier);
  2768. kmem_size = offsetof(struct kmem_cache, cpu_slab) +
  2769. nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
  2770. #else
  2771. kmem_size = sizeof(struct kmem_cache);
  2772. #endif
  2773. printk(KERN_INFO
  2774. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2775. " CPUs=%d, Nodes=%d\n",
  2776. caches, cache_line_size(),
  2777. slub_min_order, slub_max_order, slub_min_objects,
  2778. nr_cpu_ids, nr_node_ids);
  2779. }
  2780. void __init kmem_cache_init_late(void)
  2781. {
  2782. }
  2783. /*
  2784. * Find a mergeable slab cache
  2785. */
  2786. static int slab_unmergeable(struct kmem_cache *s)
  2787. {
  2788. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2789. return 1;
  2790. if (s->ctor)
  2791. return 1;
  2792. /*
  2793. * We may have set a slab to be unmergeable during bootstrap.
  2794. */
  2795. if (s->refcount < 0)
  2796. return 1;
  2797. return 0;
  2798. }
  2799. static struct kmem_cache *find_mergeable(size_t size,
  2800. size_t align, unsigned long flags, const char *name,
  2801. void (*ctor)(void *))
  2802. {
  2803. struct kmem_cache *s;
  2804. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  2805. return NULL;
  2806. if (ctor)
  2807. return NULL;
  2808. size = ALIGN(size, sizeof(void *));
  2809. align = calculate_alignment(flags, align, size);
  2810. size = ALIGN(size, align);
  2811. flags = kmem_cache_flags(size, flags, name, NULL);
  2812. list_for_each_entry(s, &slab_caches, list) {
  2813. if (slab_unmergeable(s))
  2814. continue;
  2815. if (size > s->size)
  2816. continue;
  2817. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  2818. continue;
  2819. /*
  2820. * Check if alignment is compatible.
  2821. * Courtesy of Adrian Drzewiecki
  2822. */
  2823. if ((s->size & ~(align - 1)) != s->size)
  2824. continue;
  2825. if (s->size - size >= sizeof(void *))
  2826. continue;
  2827. return s;
  2828. }
  2829. return NULL;
  2830. }
  2831. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  2832. size_t align, unsigned long flags, void (*ctor)(void *))
  2833. {
  2834. struct kmem_cache *s;
  2835. down_write(&slub_lock);
  2836. s = find_mergeable(size, align, flags, name, ctor);
  2837. if (s) {
  2838. int cpu;
  2839. s->refcount++;
  2840. /*
  2841. * Adjust the object sizes so that we clear
  2842. * the complete object on kzalloc.
  2843. */
  2844. s->objsize = max(s->objsize, (int)size);
  2845. /*
  2846. * And then we need to update the object size in the
  2847. * per cpu structures
  2848. */
  2849. for_each_online_cpu(cpu)
  2850. get_cpu_slab(s, cpu)->objsize = s->objsize;
  2851. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  2852. up_write(&slub_lock);
  2853. if (sysfs_slab_alias(s, name)) {
  2854. down_write(&slub_lock);
  2855. s->refcount--;
  2856. up_write(&slub_lock);
  2857. goto err;
  2858. }
  2859. return s;
  2860. }
  2861. s = kmalloc(kmem_size, GFP_KERNEL);
  2862. if (s) {
  2863. if (kmem_cache_open(s, GFP_KERNEL, name,
  2864. size, align, flags, ctor)) {
  2865. list_add(&s->list, &slab_caches);
  2866. up_write(&slub_lock);
  2867. if (sysfs_slab_add(s)) {
  2868. down_write(&slub_lock);
  2869. list_del(&s->list);
  2870. up_write(&slub_lock);
  2871. kfree(s);
  2872. goto err;
  2873. }
  2874. return s;
  2875. }
  2876. kfree(s);
  2877. }
  2878. up_write(&slub_lock);
  2879. err:
  2880. if (flags & SLAB_PANIC)
  2881. panic("Cannot create slabcache %s\n", name);
  2882. else
  2883. s = NULL;
  2884. return s;
  2885. }
  2886. EXPORT_SYMBOL(kmem_cache_create);
  2887. #ifdef CONFIG_SMP
  2888. /*
  2889. * Use the cpu notifier to insure that the cpu slabs are flushed when
  2890. * necessary.
  2891. */
  2892. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  2893. unsigned long action, void *hcpu)
  2894. {
  2895. long cpu = (long)hcpu;
  2896. struct kmem_cache *s;
  2897. unsigned long flags;
  2898. switch (action) {
  2899. case CPU_UP_PREPARE:
  2900. case CPU_UP_PREPARE_FROZEN:
  2901. init_alloc_cpu_cpu(cpu);
  2902. down_read(&slub_lock);
  2903. list_for_each_entry(s, &slab_caches, list)
  2904. s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
  2905. GFP_KERNEL);
  2906. up_read(&slub_lock);
  2907. break;
  2908. case CPU_UP_CANCELED:
  2909. case CPU_UP_CANCELED_FROZEN:
  2910. case CPU_DEAD:
  2911. case CPU_DEAD_FROZEN:
  2912. down_read(&slub_lock);
  2913. list_for_each_entry(s, &slab_caches, list) {
  2914. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  2915. local_irq_save(flags);
  2916. __flush_cpu_slab(s, cpu);
  2917. local_irq_restore(flags);
  2918. free_kmem_cache_cpu(c, cpu);
  2919. s->cpu_slab[cpu] = NULL;
  2920. }
  2921. up_read(&slub_lock);
  2922. break;
  2923. default:
  2924. break;
  2925. }
  2926. return NOTIFY_OK;
  2927. }
  2928. static struct notifier_block __cpuinitdata slab_notifier = {
  2929. .notifier_call = slab_cpuup_callback
  2930. };
  2931. #endif
  2932. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  2933. {
  2934. struct kmem_cache *s;
  2935. void *ret;
  2936. if (unlikely(size > SLUB_MAX_SIZE))
  2937. return kmalloc_large(size, gfpflags);
  2938. s = get_slab(size, gfpflags);
  2939. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2940. return s;
  2941. ret = slab_alloc(s, gfpflags, -1, caller);
  2942. /* Honor the call site pointer we recieved. */
  2943. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  2944. return ret;
  2945. }
  2946. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  2947. int node, unsigned long caller)
  2948. {
  2949. struct kmem_cache *s;
  2950. void *ret;
  2951. if (unlikely(size > SLUB_MAX_SIZE))
  2952. return kmalloc_large_node(size, gfpflags, node);
  2953. s = get_slab(size, gfpflags);
  2954. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2955. return s;
  2956. ret = slab_alloc(s, gfpflags, node, caller);
  2957. /* Honor the call site pointer we recieved. */
  2958. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  2959. return ret;
  2960. }
  2961. #ifdef CONFIG_SLUB_DEBUG
  2962. static int count_inuse(struct page *page)
  2963. {
  2964. return page->inuse;
  2965. }
  2966. static int count_total(struct page *page)
  2967. {
  2968. return page->objects;
  2969. }
  2970. static int validate_slab(struct kmem_cache *s, struct page *page,
  2971. unsigned long *map)
  2972. {
  2973. void *p;
  2974. void *addr = page_address(page);
  2975. if (!check_slab(s, page) ||
  2976. !on_freelist(s, page, NULL))
  2977. return 0;
  2978. /* Now we know that a valid freelist exists */
  2979. bitmap_zero(map, page->objects);
  2980. for_each_free_object(p, s, page->freelist) {
  2981. set_bit(slab_index(p, s, addr), map);
  2982. if (!check_object(s, page, p, 0))
  2983. return 0;
  2984. }
  2985. for_each_object(p, s, addr, page->objects)
  2986. if (!test_bit(slab_index(p, s, addr), map))
  2987. if (!check_object(s, page, p, 1))
  2988. return 0;
  2989. return 1;
  2990. }
  2991. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  2992. unsigned long *map)
  2993. {
  2994. if (slab_trylock(page)) {
  2995. validate_slab(s, page, map);
  2996. slab_unlock(page);
  2997. } else
  2998. printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
  2999. s->name, page);
  3000. if (s->flags & DEBUG_DEFAULT_FLAGS) {
  3001. if (!PageSlubDebug(page))
  3002. printk(KERN_ERR "SLUB %s: SlubDebug not set "
  3003. "on slab 0x%p\n", s->name, page);
  3004. } else {
  3005. if (PageSlubDebug(page))
  3006. printk(KERN_ERR "SLUB %s: SlubDebug set on "
  3007. "slab 0x%p\n", s->name, page);
  3008. }
  3009. }
  3010. static int validate_slab_node(struct kmem_cache *s,
  3011. struct kmem_cache_node *n, unsigned long *map)
  3012. {
  3013. unsigned long count = 0;
  3014. struct page *page;
  3015. unsigned long flags;
  3016. spin_lock_irqsave(&n->list_lock, flags);
  3017. list_for_each_entry(page, &n->partial, lru) {
  3018. validate_slab_slab(s, page, map);
  3019. count++;
  3020. }
  3021. if (count != n->nr_partial)
  3022. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  3023. "counter=%ld\n", s->name, count, n->nr_partial);
  3024. if (!(s->flags & SLAB_STORE_USER))
  3025. goto out;
  3026. list_for_each_entry(page, &n->full, lru) {
  3027. validate_slab_slab(s, page, map);
  3028. count++;
  3029. }
  3030. if (count != atomic_long_read(&n->nr_slabs))
  3031. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  3032. "counter=%ld\n", s->name, count,
  3033. atomic_long_read(&n->nr_slabs));
  3034. out:
  3035. spin_unlock_irqrestore(&n->list_lock, flags);
  3036. return count;
  3037. }
  3038. static long validate_slab_cache(struct kmem_cache *s)
  3039. {
  3040. int node;
  3041. unsigned long count = 0;
  3042. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3043. sizeof(unsigned long), GFP_KERNEL);
  3044. if (!map)
  3045. return -ENOMEM;
  3046. flush_all(s);
  3047. for_each_node_state(node, N_NORMAL_MEMORY) {
  3048. struct kmem_cache_node *n = get_node(s, node);
  3049. count += validate_slab_node(s, n, map);
  3050. }
  3051. kfree(map);
  3052. return count;
  3053. }
  3054. #ifdef SLUB_RESILIENCY_TEST
  3055. static void resiliency_test(void)
  3056. {
  3057. u8 *p;
  3058. printk(KERN_ERR "SLUB resiliency testing\n");
  3059. printk(KERN_ERR "-----------------------\n");
  3060. printk(KERN_ERR "A. Corruption after allocation\n");
  3061. p = kzalloc(16, GFP_KERNEL);
  3062. p[16] = 0x12;
  3063. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  3064. " 0x12->0x%p\n\n", p + 16);
  3065. validate_slab_cache(kmalloc_caches + 4);
  3066. /* Hmmm... The next two are dangerous */
  3067. p = kzalloc(32, GFP_KERNEL);
  3068. p[32 + sizeof(void *)] = 0x34;
  3069. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  3070. " 0x34 -> -0x%p\n", p);
  3071. printk(KERN_ERR
  3072. "If allocated object is overwritten then not detectable\n\n");
  3073. validate_slab_cache(kmalloc_caches + 5);
  3074. p = kzalloc(64, GFP_KERNEL);
  3075. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3076. *p = 0x56;
  3077. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3078. p);
  3079. printk(KERN_ERR
  3080. "If allocated object is overwritten then not detectable\n\n");
  3081. validate_slab_cache(kmalloc_caches + 6);
  3082. printk(KERN_ERR "\nB. Corruption after free\n");
  3083. p = kzalloc(128, GFP_KERNEL);
  3084. kfree(p);
  3085. *p = 0x78;
  3086. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3087. validate_slab_cache(kmalloc_caches + 7);
  3088. p = kzalloc(256, GFP_KERNEL);
  3089. kfree(p);
  3090. p[50] = 0x9a;
  3091. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  3092. p);
  3093. validate_slab_cache(kmalloc_caches + 8);
  3094. p = kzalloc(512, GFP_KERNEL);
  3095. kfree(p);
  3096. p[512] = 0xab;
  3097. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3098. validate_slab_cache(kmalloc_caches + 9);
  3099. }
  3100. #else
  3101. static void resiliency_test(void) {};
  3102. #endif
  3103. /*
  3104. * Generate lists of code addresses where slabcache objects are allocated
  3105. * and freed.
  3106. */
  3107. struct location {
  3108. unsigned long count;
  3109. unsigned long addr;
  3110. long long sum_time;
  3111. long min_time;
  3112. long max_time;
  3113. long min_pid;
  3114. long max_pid;
  3115. DECLARE_BITMAP(cpus, NR_CPUS);
  3116. nodemask_t nodes;
  3117. };
  3118. struct loc_track {
  3119. unsigned long max;
  3120. unsigned long count;
  3121. struct location *loc;
  3122. };
  3123. static void free_loc_track(struct loc_track *t)
  3124. {
  3125. if (t->max)
  3126. free_pages((unsigned long)t->loc,
  3127. get_order(sizeof(struct location) * t->max));
  3128. }
  3129. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3130. {
  3131. struct location *l;
  3132. int order;
  3133. order = get_order(sizeof(struct location) * max);
  3134. l = (void *)__get_free_pages(flags, order);
  3135. if (!l)
  3136. return 0;
  3137. if (t->count) {
  3138. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3139. free_loc_track(t);
  3140. }
  3141. t->max = max;
  3142. t->loc = l;
  3143. return 1;
  3144. }
  3145. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3146. const struct track *track)
  3147. {
  3148. long start, end, pos;
  3149. struct location *l;
  3150. unsigned long caddr;
  3151. unsigned long age = jiffies - track->when;
  3152. start = -1;
  3153. end = t->count;
  3154. for ( ; ; ) {
  3155. pos = start + (end - start + 1) / 2;
  3156. /*
  3157. * There is nothing at "end". If we end up there
  3158. * we need to add something to before end.
  3159. */
  3160. if (pos == end)
  3161. break;
  3162. caddr = t->loc[pos].addr;
  3163. if (track->addr == caddr) {
  3164. l = &t->loc[pos];
  3165. l->count++;
  3166. if (track->when) {
  3167. l->sum_time += age;
  3168. if (age < l->min_time)
  3169. l->min_time = age;
  3170. if (age > l->max_time)
  3171. l->max_time = age;
  3172. if (track->pid < l->min_pid)
  3173. l->min_pid = track->pid;
  3174. if (track->pid > l->max_pid)
  3175. l->max_pid = track->pid;
  3176. cpumask_set_cpu(track->cpu,
  3177. to_cpumask(l->cpus));
  3178. }
  3179. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3180. return 1;
  3181. }
  3182. if (track->addr < caddr)
  3183. end = pos;
  3184. else
  3185. start = pos;
  3186. }
  3187. /*
  3188. * Not found. Insert new tracking element.
  3189. */
  3190. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3191. return 0;
  3192. l = t->loc + pos;
  3193. if (pos < t->count)
  3194. memmove(l + 1, l,
  3195. (t->count - pos) * sizeof(struct location));
  3196. t->count++;
  3197. l->count = 1;
  3198. l->addr = track->addr;
  3199. l->sum_time = age;
  3200. l->min_time = age;
  3201. l->max_time = age;
  3202. l->min_pid = track->pid;
  3203. l->max_pid = track->pid;
  3204. cpumask_clear(to_cpumask(l->cpus));
  3205. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3206. nodes_clear(l->nodes);
  3207. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3208. return 1;
  3209. }
  3210. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3211. struct page *page, enum track_item alloc)
  3212. {
  3213. void *addr = page_address(page);
  3214. DECLARE_BITMAP(map, page->objects);
  3215. void *p;
  3216. bitmap_zero(map, page->objects);
  3217. for_each_free_object(p, s, page->freelist)
  3218. set_bit(slab_index(p, s, addr), map);
  3219. for_each_object(p, s, addr, page->objects)
  3220. if (!test_bit(slab_index(p, s, addr), map))
  3221. add_location(t, s, get_track(s, p, alloc));
  3222. }
  3223. static int list_locations(struct kmem_cache *s, char *buf,
  3224. enum track_item alloc)
  3225. {
  3226. int len = 0;
  3227. unsigned long i;
  3228. struct loc_track t = { 0, 0, NULL };
  3229. int node;
  3230. if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3231. GFP_TEMPORARY))
  3232. return sprintf(buf, "Out of memory\n");
  3233. /* Push back cpu slabs */
  3234. flush_all(s);
  3235. for_each_node_state(node, N_NORMAL_MEMORY) {
  3236. struct kmem_cache_node *n = get_node(s, node);
  3237. unsigned long flags;
  3238. struct page *page;
  3239. if (!atomic_long_read(&n->nr_slabs))
  3240. continue;
  3241. spin_lock_irqsave(&n->list_lock, flags);
  3242. list_for_each_entry(page, &n->partial, lru)
  3243. process_slab(&t, s, page, alloc);
  3244. list_for_each_entry(page, &n->full, lru)
  3245. process_slab(&t, s, page, alloc);
  3246. spin_unlock_irqrestore(&n->list_lock, flags);
  3247. }
  3248. for (i = 0; i < t.count; i++) {
  3249. struct location *l = &t.loc[i];
  3250. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3251. break;
  3252. len += sprintf(buf + len, "%7ld ", l->count);
  3253. if (l->addr)
  3254. len += sprint_symbol(buf + len, (unsigned long)l->addr);
  3255. else
  3256. len += sprintf(buf + len, "<not-available>");
  3257. if (l->sum_time != l->min_time) {
  3258. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3259. l->min_time,
  3260. (long)div_u64(l->sum_time, l->count),
  3261. l->max_time);
  3262. } else
  3263. len += sprintf(buf + len, " age=%ld",
  3264. l->min_time);
  3265. if (l->min_pid != l->max_pid)
  3266. len += sprintf(buf + len, " pid=%ld-%ld",
  3267. l->min_pid, l->max_pid);
  3268. else
  3269. len += sprintf(buf + len, " pid=%ld",
  3270. l->min_pid);
  3271. if (num_online_cpus() > 1 &&
  3272. !cpumask_empty(to_cpumask(l->cpus)) &&
  3273. len < PAGE_SIZE - 60) {
  3274. len += sprintf(buf + len, " cpus=");
  3275. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3276. to_cpumask(l->cpus));
  3277. }
  3278. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3279. len < PAGE_SIZE - 60) {
  3280. len += sprintf(buf + len, " nodes=");
  3281. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3282. l->nodes);
  3283. }
  3284. len += sprintf(buf + len, "\n");
  3285. }
  3286. free_loc_track(&t);
  3287. if (!t.count)
  3288. len += sprintf(buf, "No data\n");
  3289. return len;
  3290. }
  3291. enum slab_stat_type {
  3292. SL_ALL, /* All slabs */
  3293. SL_PARTIAL, /* Only partially allocated slabs */
  3294. SL_CPU, /* Only slabs used for cpu caches */
  3295. SL_OBJECTS, /* Determine allocated objects not slabs */
  3296. SL_TOTAL /* Determine object capacity not slabs */
  3297. };
  3298. #define SO_ALL (1 << SL_ALL)
  3299. #define SO_PARTIAL (1 << SL_PARTIAL)
  3300. #define SO_CPU (1 << SL_CPU)
  3301. #define SO_OBJECTS (1 << SL_OBJECTS)
  3302. #define SO_TOTAL (1 << SL_TOTAL)
  3303. static ssize_t show_slab_objects(struct kmem_cache *s,
  3304. char *buf, unsigned long flags)
  3305. {
  3306. unsigned long total = 0;
  3307. int node;
  3308. int x;
  3309. unsigned long *nodes;
  3310. unsigned long *per_cpu;
  3311. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3312. if (!nodes)
  3313. return -ENOMEM;
  3314. per_cpu = nodes + nr_node_ids;
  3315. if (flags & SO_CPU) {
  3316. int cpu;
  3317. for_each_possible_cpu(cpu) {
  3318. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  3319. if (!c || c->node < 0)
  3320. continue;
  3321. if (c->page) {
  3322. if (flags & SO_TOTAL)
  3323. x = c->page->objects;
  3324. else if (flags & SO_OBJECTS)
  3325. x = c->page->inuse;
  3326. else
  3327. x = 1;
  3328. total += x;
  3329. nodes[c->node] += x;
  3330. }
  3331. per_cpu[c->node]++;
  3332. }
  3333. }
  3334. if (flags & SO_ALL) {
  3335. for_each_node_state(node, N_NORMAL_MEMORY) {
  3336. struct kmem_cache_node *n = get_node(s, node);
  3337. if (flags & SO_TOTAL)
  3338. x = atomic_long_read(&n->total_objects);
  3339. else if (flags & SO_OBJECTS)
  3340. x = atomic_long_read(&n->total_objects) -
  3341. count_partial(n, count_free);
  3342. else
  3343. x = atomic_long_read(&n->nr_slabs);
  3344. total += x;
  3345. nodes[node] += x;
  3346. }
  3347. } else if (flags & SO_PARTIAL) {
  3348. for_each_node_state(node, N_NORMAL_MEMORY) {
  3349. struct kmem_cache_node *n = get_node(s, node);
  3350. if (flags & SO_TOTAL)
  3351. x = count_partial(n, count_total);
  3352. else if (flags & SO_OBJECTS)
  3353. x = count_partial(n, count_inuse);
  3354. else
  3355. x = n->nr_partial;
  3356. total += x;
  3357. nodes[node] += x;
  3358. }
  3359. }
  3360. x = sprintf(buf, "%lu", total);
  3361. #ifdef CONFIG_NUMA
  3362. for_each_node_state(node, N_NORMAL_MEMORY)
  3363. if (nodes[node])
  3364. x += sprintf(buf + x, " N%d=%lu",
  3365. node, nodes[node]);
  3366. #endif
  3367. kfree(nodes);
  3368. return x + sprintf(buf + x, "\n");
  3369. }
  3370. static int any_slab_objects(struct kmem_cache *s)
  3371. {
  3372. int node;
  3373. for_each_online_node(node) {
  3374. struct kmem_cache_node *n = get_node(s, node);
  3375. if (!n)
  3376. continue;
  3377. if (atomic_long_read(&n->total_objects))
  3378. return 1;
  3379. }
  3380. return 0;
  3381. }
  3382. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3383. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  3384. struct slab_attribute {
  3385. struct attribute attr;
  3386. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3387. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3388. };
  3389. #define SLAB_ATTR_RO(_name) \
  3390. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  3391. #define SLAB_ATTR(_name) \
  3392. static struct slab_attribute _name##_attr = \
  3393. __ATTR(_name, 0644, _name##_show, _name##_store)
  3394. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3395. {
  3396. return sprintf(buf, "%d\n", s->size);
  3397. }
  3398. SLAB_ATTR_RO(slab_size);
  3399. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3400. {
  3401. return sprintf(buf, "%d\n", s->align);
  3402. }
  3403. SLAB_ATTR_RO(align);
  3404. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3405. {
  3406. return sprintf(buf, "%d\n", s->objsize);
  3407. }
  3408. SLAB_ATTR_RO(object_size);
  3409. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3410. {
  3411. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3412. }
  3413. SLAB_ATTR_RO(objs_per_slab);
  3414. static ssize_t order_store(struct kmem_cache *s,
  3415. const char *buf, size_t length)
  3416. {
  3417. unsigned long order;
  3418. int err;
  3419. err = strict_strtoul(buf, 10, &order);
  3420. if (err)
  3421. return err;
  3422. if (order > slub_max_order || order < slub_min_order)
  3423. return -EINVAL;
  3424. calculate_sizes(s, order);
  3425. return length;
  3426. }
  3427. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3428. {
  3429. return sprintf(buf, "%d\n", oo_order(s->oo));
  3430. }
  3431. SLAB_ATTR(order);
  3432. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  3433. {
  3434. return sprintf(buf, "%lu\n", s->min_partial);
  3435. }
  3436. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  3437. size_t length)
  3438. {
  3439. unsigned long min;
  3440. int err;
  3441. err = strict_strtoul(buf, 10, &min);
  3442. if (err)
  3443. return err;
  3444. set_min_partial(s, min);
  3445. return length;
  3446. }
  3447. SLAB_ATTR(min_partial);
  3448. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3449. {
  3450. if (s->ctor) {
  3451. int n = sprint_symbol(buf, (unsigned long)s->ctor);
  3452. return n + sprintf(buf + n, "\n");
  3453. }
  3454. return 0;
  3455. }
  3456. SLAB_ATTR_RO(ctor);
  3457. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3458. {
  3459. return sprintf(buf, "%d\n", s->refcount - 1);
  3460. }
  3461. SLAB_ATTR_RO(aliases);
  3462. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  3463. {
  3464. return show_slab_objects(s, buf, SO_ALL);
  3465. }
  3466. SLAB_ATTR_RO(slabs);
  3467. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3468. {
  3469. return show_slab_objects(s, buf, SO_PARTIAL);
  3470. }
  3471. SLAB_ATTR_RO(partial);
  3472. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3473. {
  3474. return show_slab_objects(s, buf, SO_CPU);
  3475. }
  3476. SLAB_ATTR_RO(cpu_slabs);
  3477. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3478. {
  3479. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3480. }
  3481. SLAB_ATTR_RO(objects);
  3482. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3483. {
  3484. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3485. }
  3486. SLAB_ATTR_RO(objects_partial);
  3487. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  3488. {
  3489. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  3490. }
  3491. SLAB_ATTR_RO(total_objects);
  3492. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3493. {
  3494. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3495. }
  3496. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3497. const char *buf, size_t length)
  3498. {
  3499. s->flags &= ~SLAB_DEBUG_FREE;
  3500. if (buf[0] == '1')
  3501. s->flags |= SLAB_DEBUG_FREE;
  3502. return length;
  3503. }
  3504. SLAB_ATTR(sanity_checks);
  3505. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3506. {
  3507. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3508. }
  3509. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3510. size_t length)
  3511. {
  3512. s->flags &= ~SLAB_TRACE;
  3513. if (buf[0] == '1')
  3514. s->flags |= SLAB_TRACE;
  3515. return length;
  3516. }
  3517. SLAB_ATTR(trace);
  3518. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3519. {
  3520. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3521. }
  3522. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3523. const char *buf, size_t length)
  3524. {
  3525. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3526. if (buf[0] == '1')
  3527. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3528. return length;
  3529. }
  3530. SLAB_ATTR(reclaim_account);
  3531. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3532. {
  3533. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3534. }
  3535. SLAB_ATTR_RO(hwcache_align);
  3536. #ifdef CONFIG_ZONE_DMA
  3537. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3538. {
  3539. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3540. }
  3541. SLAB_ATTR_RO(cache_dma);
  3542. #endif
  3543. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3544. {
  3545. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3546. }
  3547. SLAB_ATTR_RO(destroy_by_rcu);
  3548. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3549. {
  3550. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3551. }
  3552. static ssize_t red_zone_store(struct kmem_cache *s,
  3553. const char *buf, size_t length)
  3554. {
  3555. if (any_slab_objects(s))
  3556. return -EBUSY;
  3557. s->flags &= ~SLAB_RED_ZONE;
  3558. if (buf[0] == '1')
  3559. s->flags |= SLAB_RED_ZONE;
  3560. calculate_sizes(s, -1);
  3561. return length;
  3562. }
  3563. SLAB_ATTR(red_zone);
  3564. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3565. {
  3566. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3567. }
  3568. static ssize_t poison_store(struct kmem_cache *s,
  3569. const char *buf, size_t length)
  3570. {
  3571. if (any_slab_objects(s))
  3572. return -EBUSY;
  3573. s->flags &= ~SLAB_POISON;
  3574. if (buf[0] == '1')
  3575. s->flags |= SLAB_POISON;
  3576. calculate_sizes(s, -1);
  3577. return length;
  3578. }
  3579. SLAB_ATTR(poison);
  3580. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3581. {
  3582. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3583. }
  3584. static ssize_t store_user_store(struct kmem_cache *s,
  3585. const char *buf, size_t length)
  3586. {
  3587. if (any_slab_objects(s))
  3588. return -EBUSY;
  3589. s->flags &= ~SLAB_STORE_USER;
  3590. if (buf[0] == '1')
  3591. s->flags |= SLAB_STORE_USER;
  3592. calculate_sizes(s, -1);
  3593. return length;
  3594. }
  3595. SLAB_ATTR(store_user);
  3596. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3597. {
  3598. return 0;
  3599. }
  3600. static ssize_t validate_store(struct kmem_cache *s,
  3601. const char *buf, size_t length)
  3602. {
  3603. int ret = -EINVAL;
  3604. if (buf[0] == '1') {
  3605. ret = validate_slab_cache(s);
  3606. if (ret >= 0)
  3607. ret = length;
  3608. }
  3609. return ret;
  3610. }
  3611. SLAB_ATTR(validate);
  3612. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3613. {
  3614. return 0;
  3615. }
  3616. static ssize_t shrink_store(struct kmem_cache *s,
  3617. const char *buf, size_t length)
  3618. {
  3619. if (buf[0] == '1') {
  3620. int rc = kmem_cache_shrink(s);
  3621. if (rc)
  3622. return rc;
  3623. } else
  3624. return -EINVAL;
  3625. return length;
  3626. }
  3627. SLAB_ATTR(shrink);
  3628. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3629. {
  3630. if (!(s->flags & SLAB_STORE_USER))
  3631. return -ENOSYS;
  3632. return list_locations(s, buf, TRACK_ALLOC);
  3633. }
  3634. SLAB_ATTR_RO(alloc_calls);
  3635. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3636. {
  3637. if (!(s->flags & SLAB_STORE_USER))
  3638. return -ENOSYS;
  3639. return list_locations(s, buf, TRACK_FREE);
  3640. }
  3641. SLAB_ATTR_RO(free_calls);
  3642. #ifdef CONFIG_NUMA
  3643. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  3644. {
  3645. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  3646. }
  3647. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  3648. const char *buf, size_t length)
  3649. {
  3650. unsigned long ratio;
  3651. int err;
  3652. err = strict_strtoul(buf, 10, &ratio);
  3653. if (err)
  3654. return err;
  3655. if (ratio <= 100)
  3656. s->remote_node_defrag_ratio = ratio * 10;
  3657. return length;
  3658. }
  3659. SLAB_ATTR(remote_node_defrag_ratio);
  3660. #endif
  3661. #ifdef CONFIG_SLUB_STATS
  3662. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  3663. {
  3664. unsigned long sum = 0;
  3665. int cpu;
  3666. int len;
  3667. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  3668. if (!data)
  3669. return -ENOMEM;
  3670. for_each_online_cpu(cpu) {
  3671. unsigned x = get_cpu_slab(s, cpu)->stat[si];
  3672. data[cpu] = x;
  3673. sum += x;
  3674. }
  3675. len = sprintf(buf, "%lu", sum);
  3676. #ifdef CONFIG_SMP
  3677. for_each_online_cpu(cpu) {
  3678. if (data[cpu] && len < PAGE_SIZE - 20)
  3679. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  3680. }
  3681. #endif
  3682. kfree(data);
  3683. return len + sprintf(buf + len, "\n");
  3684. }
  3685. #define STAT_ATTR(si, text) \
  3686. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  3687. { \
  3688. return show_stat(s, buf, si); \
  3689. } \
  3690. SLAB_ATTR_RO(text); \
  3691. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  3692. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  3693. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  3694. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  3695. STAT_ATTR(FREE_FROZEN, free_frozen);
  3696. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  3697. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  3698. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  3699. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  3700. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  3701. STAT_ATTR(FREE_SLAB, free_slab);
  3702. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  3703. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  3704. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  3705. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  3706. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  3707. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  3708. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  3709. #endif
  3710. static struct attribute *slab_attrs[] = {
  3711. &slab_size_attr.attr,
  3712. &object_size_attr.attr,
  3713. &objs_per_slab_attr.attr,
  3714. &order_attr.attr,
  3715. &min_partial_attr.attr,
  3716. &objects_attr.attr,
  3717. &objects_partial_attr.attr,
  3718. &total_objects_attr.attr,
  3719. &slabs_attr.attr,
  3720. &partial_attr.attr,
  3721. &cpu_slabs_attr.attr,
  3722. &ctor_attr.attr,
  3723. &aliases_attr.attr,
  3724. &align_attr.attr,
  3725. &sanity_checks_attr.attr,
  3726. &trace_attr.attr,
  3727. &hwcache_align_attr.attr,
  3728. &reclaim_account_attr.attr,
  3729. &destroy_by_rcu_attr.attr,
  3730. &red_zone_attr.attr,
  3731. &poison_attr.attr,
  3732. &store_user_attr.attr,
  3733. &validate_attr.attr,
  3734. &shrink_attr.attr,
  3735. &alloc_calls_attr.attr,
  3736. &free_calls_attr.attr,
  3737. #ifdef CONFIG_ZONE_DMA
  3738. &cache_dma_attr.attr,
  3739. #endif
  3740. #ifdef CONFIG_NUMA
  3741. &remote_node_defrag_ratio_attr.attr,
  3742. #endif
  3743. #ifdef CONFIG_SLUB_STATS
  3744. &alloc_fastpath_attr.attr,
  3745. &alloc_slowpath_attr.attr,
  3746. &free_fastpath_attr.attr,
  3747. &free_slowpath_attr.attr,
  3748. &free_frozen_attr.attr,
  3749. &free_add_partial_attr.attr,
  3750. &free_remove_partial_attr.attr,
  3751. &alloc_from_partial_attr.attr,
  3752. &alloc_slab_attr.attr,
  3753. &alloc_refill_attr.attr,
  3754. &free_slab_attr.attr,
  3755. &cpuslab_flush_attr.attr,
  3756. &deactivate_full_attr.attr,
  3757. &deactivate_empty_attr.attr,
  3758. &deactivate_to_head_attr.attr,
  3759. &deactivate_to_tail_attr.attr,
  3760. &deactivate_remote_frees_attr.attr,
  3761. &order_fallback_attr.attr,
  3762. #endif
  3763. NULL
  3764. };
  3765. static struct attribute_group slab_attr_group = {
  3766. .attrs = slab_attrs,
  3767. };
  3768. static ssize_t slab_attr_show(struct kobject *kobj,
  3769. struct attribute *attr,
  3770. char *buf)
  3771. {
  3772. struct slab_attribute *attribute;
  3773. struct kmem_cache *s;
  3774. int err;
  3775. attribute = to_slab_attr(attr);
  3776. s = to_slab(kobj);
  3777. if (!attribute->show)
  3778. return -EIO;
  3779. err = attribute->show(s, buf);
  3780. return err;
  3781. }
  3782. static ssize_t slab_attr_store(struct kobject *kobj,
  3783. struct attribute *attr,
  3784. const char *buf, size_t len)
  3785. {
  3786. struct slab_attribute *attribute;
  3787. struct kmem_cache *s;
  3788. int err;
  3789. attribute = to_slab_attr(attr);
  3790. s = to_slab(kobj);
  3791. if (!attribute->store)
  3792. return -EIO;
  3793. err = attribute->store(s, buf, len);
  3794. return err;
  3795. }
  3796. static void kmem_cache_release(struct kobject *kobj)
  3797. {
  3798. struct kmem_cache *s = to_slab(kobj);
  3799. kfree(s);
  3800. }
  3801. static struct sysfs_ops slab_sysfs_ops = {
  3802. .show = slab_attr_show,
  3803. .store = slab_attr_store,
  3804. };
  3805. static struct kobj_type slab_ktype = {
  3806. .sysfs_ops = &slab_sysfs_ops,
  3807. .release = kmem_cache_release
  3808. };
  3809. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  3810. {
  3811. struct kobj_type *ktype = get_ktype(kobj);
  3812. if (ktype == &slab_ktype)
  3813. return 1;
  3814. return 0;
  3815. }
  3816. static struct kset_uevent_ops slab_uevent_ops = {
  3817. .filter = uevent_filter,
  3818. };
  3819. static struct kset *slab_kset;
  3820. #define ID_STR_LENGTH 64
  3821. /* Create a unique string id for a slab cache:
  3822. *
  3823. * Format :[flags-]size
  3824. */
  3825. static char *create_unique_id(struct kmem_cache *s)
  3826. {
  3827. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  3828. char *p = name;
  3829. BUG_ON(!name);
  3830. *p++ = ':';
  3831. /*
  3832. * First flags affecting slabcache operations. We will only
  3833. * get here for aliasable slabs so we do not need to support
  3834. * too many flags. The flags here must cover all flags that
  3835. * are matched during merging to guarantee that the id is
  3836. * unique.
  3837. */
  3838. if (s->flags & SLAB_CACHE_DMA)
  3839. *p++ = 'd';
  3840. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3841. *p++ = 'a';
  3842. if (s->flags & SLAB_DEBUG_FREE)
  3843. *p++ = 'F';
  3844. if (!(s->flags & SLAB_NOTRACK))
  3845. *p++ = 't';
  3846. if (p != name + 1)
  3847. *p++ = '-';
  3848. p += sprintf(p, "%07d", s->size);
  3849. BUG_ON(p > name + ID_STR_LENGTH - 1);
  3850. return name;
  3851. }
  3852. static int sysfs_slab_add(struct kmem_cache *s)
  3853. {
  3854. int err;
  3855. const char *name;
  3856. int unmergeable;
  3857. if (slab_state < SYSFS)
  3858. /* Defer until later */
  3859. return 0;
  3860. unmergeable = slab_unmergeable(s);
  3861. if (unmergeable) {
  3862. /*
  3863. * Slabcache can never be merged so we can use the name proper.
  3864. * This is typically the case for debug situations. In that
  3865. * case we can catch duplicate names easily.
  3866. */
  3867. sysfs_remove_link(&slab_kset->kobj, s->name);
  3868. name = s->name;
  3869. } else {
  3870. /*
  3871. * Create a unique name for the slab as a target
  3872. * for the symlinks.
  3873. */
  3874. name = create_unique_id(s);
  3875. }
  3876. s->kobj.kset = slab_kset;
  3877. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  3878. if (err) {
  3879. kobject_put(&s->kobj);
  3880. return err;
  3881. }
  3882. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  3883. if (err) {
  3884. kobject_del(&s->kobj);
  3885. kobject_put(&s->kobj);
  3886. return err;
  3887. }
  3888. kobject_uevent(&s->kobj, KOBJ_ADD);
  3889. if (!unmergeable) {
  3890. /* Setup first alias */
  3891. sysfs_slab_alias(s, s->name);
  3892. kfree(name);
  3893. }
  3894. return 0;
  3895. }
  3896. static void sysfs_slab_remove(struct kmem_cache *s)
  3897. {
  3898. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  3899. kobject_del(&s->kobj);
  3900. kobject_put(&s->kobj);
  3901. }
  3902. /*
  3903. * Need to buffer aliases during bootup until sysfs becomes
  3904. * available lest we lose that information.
  3905. */
  3906. struct saved_alias {
  3907. struct kmem_cache *s;
  3908. const char *name;
  3909. struct saved_alias *next;
  3910. };
  3911. static struct saved_alias *alias_list;
  3912. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  3913. {
  3914. struct saved_alias *al;
  3915. if (slab_state == SYSFS) {
  3916. /*
  3917. * If we have a leftover link then remove it.
  3918. */
  3919. sysfs_remove_link(&slab_kset->kobj, name);
  3920. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  3921. }
  3922. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  3923. if (!al)
  3924. return -ENOMEM;
  3925. al->s = s;
  3926. al->name = name;
  3927. al->next = alias_list;
  3928. alias_list = al;
  3929. return 0;
  3930. }
  3931. static int __init slab_sysfs_init(void)
  3932. {
  3933. struct kmem_cache *s;
  3934. int err;
  3935. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  3936. if (!slab_kset) {
  3937. printk(KERN_ERR "Cannot register slab subsystem.\n");
  3938. return -ENOSYS;
  3939. }
  3940. slab_state = SYSFS;
  3941. list_for_each_entry(s, &slab_caches, list) {
  3942. err = sysfs_slab_add(s);
  3943. if (err)
  3944. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  3945. " to sysfs\n", s->name);
  3946. }
  3947. while (alias_list) {
  3948. struct saved_alias *al = alias_list;
  3949. alias_list = alias_list->next;
  3950. err = sysfs_slab_alias(al->s, al->name);
  3951. if (err)
  3952. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  3953. " %s to sysfs\n", s->name);
  3954. kfree(al);
  3955. }
  3956. resiliency_test();
  3957. return 0;
  3958. }
  3959. __initcall(slab_sysfs_init);
  3960. #endif
  3961. /*
  3962. * The /proc/slabinfo ABI
  3963. */
  3964. #ifdef CONFIG_SLABINFO
  3965. static void print_slabinfo_header(struct seq_file *m)
  3966. {
  3967. seq_puts(m, "slabinfo - version: 2.1\n");
  3968. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  3969. "<objperslab> <pagesperslab>");
  3970. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  3971. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  3972. seq_putc(m, '\n');
  3973. }
  3974. static void *s_start(struct seq_file *m, loff_t *pos)
  3975. {
  3976. loff_t n = *pos;
  3977. down_read(&slub_lock);
  3978. if (!n)
  3979. print_slabinfo_header(m);
  3980. return seq_list_start(&slab_caches, *pos);
  3981. }
  3982. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  3983. {
  3984. return seq_list_next(p, &slab_caches, pos);
  3985. }
  3986. static void s_stop(struct seq_file *m, void *p)
  3987. {
  3988. up_read(&slub_lock);
  3989. }
  3990. static int s_show(struct seq_file *m, void *p)
  3991. {
  3992. unsigned long nr_partials = 0;
  3993. unsigned long nr_slabs = 0;
  3994. unsigned long nr_inuse = 0;
  3995. unsigned long nr_objs = 0;
  3996. unsigned long nr_free = 0;
  3997. struct kmem_cache *s;
  3998. int node;
  3999. s = list_entry(p, struct kmem_cache, list);
  4000. for_each_online_node(node) {
  4001. struct kmem_cache_node *n = get_node(s, node);
  4002. if (!n)
  4003. continue;
  4004. nr_partials += n->nr_partial;
  4005. nr_slabs += atomic_long_read(&n->nr_slabs);
  4006. nr_objs += atomic_long_read(&n->total_objects);
  4007. nr_free += count_partial(n, count_free);
  4008. }
  4009. nr_inuse = nr_objs - nr_free;
  4010. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  4011. nr_objs, s->size, oo_objects(s->oo),
  4012. (1 << oo_order(s->oo)));
  4013. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  4014. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  4015. 0UL);
  4016. seq_putc(m, '\n');
  4017. return 0;
  4018. }
  4019. static const struct seq_operations slabinfo_op = {
  4020. .start = s_start,
  4021. .next = s_next,
  4022. .stop = s_stop,
  4023. .show = s_show,
  4024. };
  4025. static int slabinfo_open(struct inode *inode, struct file *file)
  4026. {
  4027. return seq_open(file, &slabinfo_op);
  4028. }
  4029. static const struct file_operations proc_slabinfo_operations = {
  4030. .open = slabinfo_open,
  4031. .read = seq_read,
  4032. .llseek = seq_lseek,
  4033. .release = seq_release,
  4034. };
  4035. static int __init slab_proc_init(void)
  4036. {
  4037. proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
  4038. return 0;
  4039. }
  4040. module_init(slab_proc_init);
  4041. #endif /* CONFIG_SLABINFO */