slub.c 116 KB

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