slub.c 115 KB

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