slub.c 115 KB

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