slub.c 117 KB

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