slub.c 115 KB

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