slub.c 117 KB

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