slub.c 123 KB

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