slub.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259
  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 = page->objects;
  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. struct kmem_cache_cpu *c)
  1325. {
  1326. void *freelist;
  1327. unsigned long counters;
  1328. struct page new;
  1329. /*
  1330. * Zap the freelist and set the frozen bit.
  1331. * The old freelist is the list of objects for the
  1332. * per cpu allocation list.
  1333. */
  1334. do {
  1335. freelist = page->freelist;
  1336. counters = page->counters;
  1337. new.counters = counters;
  1338. new.inuse = page->objects;
  1339. VM_BUG_ON(new.frozen);
  1340. new.frozen = 1;
  1341. } while (!__cmpxchg_double_slab(s, page,
  1342. freelist, counters,
  1343. NULL, new.counters,
  1344. "lock and freeze"));
  1345. remove_partial(n, page);
  1346. if (freelist) {
  1347. /* Populate the per cpu freelist */
  1348. c->freelist = freelist;
  1349. c->page = page;
  1350. c->node = page_to_nid(page);
  1351. return 1;
  1352. } else {
  1353. /*
  1354. * Slab page came from the wrong list. No object to allocate
  1355. * from. Put it onto the correct list and continue partial
  1356. * scan.
  1357. */
  1358. printk(KERN_ERR "SLUB: %s : Page without available objects on"
  1359. " partial list\n", s->name);
  1360. return 0;
  1361. }
  1362. }
  1363. /*
  1364. * Try to allocate a partial slab from a specific node.
  1365. */
  1366. static struct page *get_partial_node(struct kmem_cache *s,
  1367. struct kmem_cache_node *n, struct kmem_cache_cpu *c)
  1368. {
  1369. struct page *page;
  1370. /*
  1371. * Racy check. If we mistakenly see no partial slabs then we
  1372. * just allocate an empty slab. If we mistakenly try to get a
  1373. * partial slab and there is none available then get_partials()
  1374. * will return NULL.
  1375. */
  1376. if (!n || !n->nr_partial)
  1377. return NULL;
  1378. spin_lock(&n->list_lock);
  1379. list_for_each_entry(page, &n->partial, lru)
  1380. if (acquire_slab(s, n, page, c))
  1381. goto out;
  1382. page = NULL;
  1383. out:
  1384. spin_unlock(&n->list_lock);
  1385. return page;
  1386. }
  1387. /*
  1388. * Get a page from somewhere. Search in increasing NUMA distances.
  1389. */
  1390. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags,
  1391. struct kmem_cache_cpu *c)
  1392. {
  1393. #ifdef CONFIG_NUMA
  1394. struct zonelist *zonelist;
  1395. struct zoneref *z;
  1396. struct zone *zone;
  1397. enum zone_type high_zoneidx = gfp_zone(flags);
  1398. struct page *page;
  1399. /*
  1400. * The defrag ratio allows a configuration of the tradeoffs between
  1401. * inter node defragmentation and node local allocations. A lower
  1402. * defrag_ratio increases the tendency to do local allocations
  1403. * instead of attempting to obtain partial slabs from other nodes.
  1404. *
  1405. * If the defrag_ratio is set to 0 then kmalloc() always
  1406. * returns node local objects. If the ratio is higher then kmalloc()
  1407. * may return off node objects because partial slabs are obtained
  1408. * from other nodes and filled up.
  1409. *
  1410. * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
  1411. * defrag_ratio = 1000) then every (well almost) allocation will
  1412. * first attempt to defrag slab caches on other nodes. This means
  1413. * scanning over all nodes to look for partial slabs which may be
  1414. * expensive if we do it every time we are trying to find a slab
  1415. * with available objects.
  1416. */
  1417. if (!s->remote_node_defrag_ratio ||
  1418. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1419. return NULL;
  1420. get_mems_allowed();
  1421. zonelist = node_zonelist(slab_node(current->mempolicy), flags);
  1422. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1423. struct kmem_cache_node *n;
  1424. n = get_node(s, zone_to_nid(zone));
  1425. if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
  1426. n->nr_partial > s->min_partial) {
  1427. page = get_partial_node(s, n, c);
  1428. if (page) {
  1429. put_mems_allowed();
  1430. return page;
  1431. }
  1432. }
  1433. }
  1434. put_mems_allowed();
  1435. #endif
  1436. return NULL;
  1437. }
  1438. /*
  1439. * Get a partial page, lock it and return it.
  1440. */
  1441. static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node,
  1442. struct kmem_cache_cpu *c)
  1443. {
  1444. struct page *page;
  1445. int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
  1446. page = get_partial_node(s, get_node(s, searchnode), c);
  1447. if (page || node != NUMA_NO_NODE)
  1448. return page;
  1449. return get_any_partial(s, flags, c);
  1450. }
  1451. #ifdef CONFIG_PREEMPT
  1452. /*
  1453. * Calculate the next globally unique transaction for disambiguiation
  1454. * during cmpxchg. The transactions start with the cpu number and are then
  1455. * incremented by CONFIG_NR_CPUS.
  1456. */
  1457. #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
  1458. #else
  1459. /*
  1460. * No preemption supported therefore also no need to check for
  1461. * different cpus.
  1462. */
  1463. #define TID_STEP 1
  1464. #endif
  1465. static inline unsigned long next_tid(unsigned long tid)
  1466. {
  1467. return tid + TID_STEP;
  1468. }
  1469. static inline unsigned int tid_to_cpu(unsigned long tid)
  1470. {
  1471. return tid % TID_STEP;
  1472. }
  1473. static inline unsigned long tid_to_event(unsigned long tid)
  1474. {
  1475. return tid / TID_STEP;
  1476. }
  1477. static inline unsigned int init_tid(int cpu)
  1478. {
  1479. return cpu;
  1480. }
  1481. static inline void note_cmpxchg_failure(const char *n,
  1482. const struct kmem_cache *s, unsigned long tid)
  1483. {
  1484. #ifdef SLUB_DEBUG_CMPXCHG
  1485. unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
  1486. printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
  1487. #ifdef CONFIG_PREEMPT
  1488. if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
  1489. printk("due to cpu change %d -> %d\n",
  1490. tid_to_cpu(tid), tid_to_cpu(actual_tid));
  1491. else
  1492. #endif
  1493. if (tid_to_event(tid) != tid_to_event(actual_tid))
  1494. printk("due to cpu running other code. Event %ld->%ld\n",
  1495. tid_to_event(tid), tid_to_event(actual_tid));
  1496. else
  1497. printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
  1498. actual_tid, tid, next_tid(tid));
  1499. #endif
  1500. stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
  1501. }
  1502. void init_kmem_cache_cpus(struct kmem_cache *s)
  1503. {
  1504. int cpu;
  1505. for_each_possible_cpu(cpu)
  1506. per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
  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. page = c->page;
  1759. if (!page)
  1760. goto new_slab;
  1761. if (unlikely(!node_match(c, node))) {
  1762. stat(s, ALLOC_NODE_MISMATCH);
  1763. deactivate_slab(s, c);
  1764. goto new_slab;
  1765. }
  1766. stat(s, ALLOC_SLOWPATH);
  1767. do {
  1768. object = page->freelist;
  1769. counters = page->counters;
  1770. new.counters = counters;
  1771. VM_BUG_ON(!new.frozen);
  1772. /*
  1773. * If there is no object left then we use this loop to
  1774. * deactivate the slab which is simple since no objects
  1775. * are left in the slab and therefore we do not need to
  1776. * put the page back onto the partial list.
  1777. *
  1778. * If there are objects left then we retrieve them
  1779. * and use them to refill the per cpu queue.
  1780. */
  1781. new.inuse = page->objects;
  1782. new.frozen = object != NULL;
  1783. } while (!__cmpxchg_double_slab(s, page,
  1784. object, counters,
  1785. NULL, new.counters,
  1786. "__slab_alloc"));
  1787. if (unlikely(!object)) {
  1788. c->page = NULL;
  1789. stat(s, DEACTIVATE_BYPASS);
  1790. goto new_slab;
  1791. }
  1792. stat(s, ALLOC_REFILL);
  1793. load_freelist:
  1794. VM_BUG_ON(!page->frozen);
  1795. c->freelist = get_freepointer(s, object);
  1796. c->tid = next_tid(c->tid);
  1797. local_irq_restore(flags);
  1798. return object;
  1799. new_slab:
  1800. page = get_partial(s, gfpflags, node, c);
  1801. if (page) {
  1802. stat(s, ALLOC_FROM_PARTIAL);
  1803. object = c->freelist;
  1804. if (kmem_cache_debug(s))
  1805. goto debug;
  1806. goto load_freelist;
  1807. }
  1808. page = new_slab(s, gfpflags, node);
  1809. if (page) {
  1810. c = __this_cpu_ptr(s->cpu_slab);
  1811. if (c->page)
  1812. flush_slab(s, c);
  1813. /*
  1814. * No other reference to the page yet so we can
  1815. * muck around with it freely without cmpxchg
  1816. */
  1817. object = page->freelist;
  1818. page->freelist = NULL;
  1819. stat(s, ALLOC_SLAB);
  1820. c->node = page_to_nid(page);
  1821. c->page = page;
  1822. if (kmem_cache_debug(s))
  1823. goto debug;
  1824. goto load_freelist;
  1825. }
  1826. if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
  1827. slab_out_of_memory(s, gfpflags, node);
  1828. local_irq_restore(flags);
  1829. return NULL;
  1830. debug:
  1831. if (!object || !alloc_debug_processing(s, page, object, addr))
  1832. goto new_slab;
  1833. c->freelist = get_freepointer(s, object);
  1834. deactivate_slab(s, c);
  1835. c->node = NUMA_NO_NODE;
  1836. local_irq_restore(flags);
  1837. return object;
  1838. }
  1839. /*
  1840. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1841. * have the fastpath folded into their functions. So no function call
  1842. * overhead for requests that can be satisfied on the fastpath.
  1843. *
  1844. * The fastpath works by first checking if the lockless freelist can be used.
  1845. * If not then __slab_alloc is called for slow processing.
  1846. *
  1847. * Otherwise we can simply pick the next object from the lockless free list.
  1848. */
  1849. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1850. gfp_t gfpflags, int node, unsigned long addr)
  1851. {
  1852. void **object;
  1853. struct kmem_cache_cpu *c;
  1854. unsigned long tid;
  1855. if (slab_pre_alloc_hook(s, gfpflags))
  1856. return NULL;
  1857. redo:
  1858. /*
  1859. * Must read kmem_cache cpu data via this cpu ptr. Preemption is
  1860. * enabled. We may switch back and forth between cpus while
  1861. * reading from one cpu area. That does not matter as long
  1862. * as we end up on the original cpu again when doing the cmpxchg.
  1863. */
  1864. c = __this_cpu_ptr(s->cpu_slab);
  1865. /*
  1866. * The transaction ids are globally unique per cpu and per operation on
  1867. * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
  1868. * occurs on the right processor and that there was no operation on the
  1869. * linked list in between.
  1870. */
  1871. tid = c->tid;
  1872. barrier();
  1873. object = c->freelist;
  1874. if (unlikely(!object || !node_match(c, node)))
  1875. object = __slab_alloc(s, gfpflags, node, addr, c);
  1876. else {
  1877. /*
  1878. * The cmpxchg will only match if there was no additional
  1879. * operation and if we are on the right processor.
  1880. *
  1881. * The cmpxchg does the following atomically (without lock semantics!)
  1882. * 1. Relocate first pointer to the current per cpu area.
  1883. * 2. Verify that tid and freelist have not been changed
  1884. * 3. If they were not changed replace tid and freelist
  1885. *
  1886. * Since this is without lock semantics the protection is only against
  1887. * code executing on this cpu *not* from access by other cpus.
  1888. */
  1889. if (unlikely(!irqsafe_cpu_cmpxchg_double(
  1890. s->cpu_slab->freelist, s->cpu_slab->tid,
  1891. object, tid,
  1892. get_freepointer_safe(s, object), next_tid(tid)))) {
  1893. note_cmpxchg_failure("slab_alloc", s, tid);
  1894. goto redo;
  1895. }
  1896. stat(s, ALLOC_FASTPATH);
  1897. }
  1898. if (unlikely(gfpflags & __GFP_ZERO) && object)
  1899. memset(object, 0, s->objsize);
  1900. slab_post_alloc_hook(s, gfpflags, object);
  1901. return object;
  1902. }
  1903. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1904. {
  1905. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1906. trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
  1907. return ret;
  1908. }
  1909. EXPORT_SYMBOL(kmem_cache_alloc);
  1910. #ifdef CONFIG_TRACING
  1911. void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  1912. {
  1913. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1914. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
  1915. return ret;
  1916. }
  1917. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  1918. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  1919. {
  1920. void *ret = kmalloc_order(size, flags, order);
  1921. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  1922. return ret;
  1923. }
  1924. EXPORT_SYMBOL(kmalloc_order_trace);
  1925. #endif
  1926. #ifdef CONFIG_NUMA
  1927. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1928. {
  1929. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1930. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  1931. s->objsize, s->size, gfpflags, node);
  1932. return ret;
  1933. }
  1934. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1935. #ifdef CONFIG_TRACING
  1936. void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  1937. gfp_t gfpflags,
  1938. int node, size_t size)
  1939. {
  1940. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1941. trace_kmalloc_node(_RET_IP_, ret,
  1942. size, s->size, gfpflags, node);
  1943. return ret;
  1944. }
  1945. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  1946. #endif
  1947. #endif
  1948. /*
  1949. * Slow patch handling. This may still be called frequently since objects
  1950. * have a longer lifetime than the cpu slabs in most processing loads.
  1951. *
  1952. * So we still attempt to reduce cache line usage. Just take the slab
  1953. * lock and free the item. If there is no additional partial page
  1954. * handling required then we can return immediately.
  1955. */
  1956. static void __slab_free(struct kmem_cache *s, struct page *page,
  1957. void *x, unsigned long addr)
  1958. {
  1959. void *prior;
  1960. void **object = (void *)x;
  1961. int was_frozen;
  1962. int inuse;
  1963. struct page new;
  1964. unsigned long counters;
  1965. struct kmem_cache_node *n = NULL;
  1966. unsigned long uninitialized_var(flags);
  1967. stat(s, FREE_SLOWPATH);
  1968. if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
  1969. return;
  1970. do {
  1971. prior = page->freelist;
  1972. counters = page->counters;
  1973. set_freepointer(s, object, prior);
  1974. new.counters = counters;
  1975. was_frozen = new.frozen;
  1976. new.inuse--;
  1977. if ((!new.inuse || !prior) && !was_frozen && !n) {
  1978. n = get_node(s, page_to_nid(page));
  1979. /*
  1980. * Speculatively acquire the list_lock.
  1981. * If the cmpxchg does not succeed then we may
  1982. * drop the list_lock without any processing.
  1983. *
  1984. * Otherwise the list_lock will synchronize with
  1985. * other processors updating the list of slabs.
  1986. */
  1987. spin_lock_irqsave(&n->list_lock, flags);
  1988. }
  1989. inuse = new.inuse;
  1990. } while (!cmpxchg_double_slab(s, page,
  1991. prior, counters,
  1992. object, new.counters,
  1993. "__slab_free"));
  1994. if (likely(!n)) {
  1995. /*
  1996. * The list lock was not taken therefore no list
  1997. * activity can be necessary.
  1998. */
  1999. if (was_frozen)
  2000. stat(s, FREE_FROZEN);
  2001. return;
  2002. }
  2003. /*
  2004. * was_frozen may have been set after we acquired the list_lock in
  2005. * an earlier loop. So we need to check it here again.
  2006. */
  2007. if (was_frozen)
  2008. stat(s, FREE_FROZEN);
  2009. else {
  2010. if (unlikely(!inuse && n->nr_partial > s->min_partial))
  2011. goto slab_empty;
  2012. /*
  2013. * Objects left in the slab. If it was not on the partial list before
  2014. * then add it.
  2015. */
  2016. if (unlikely(!prior)) {
  2017. remove_full(s, page);
  2018. add_partial(n, page, 0);
  2019. stat(s, FREE_ADD_PARTIAL);
  2020. }
  2021. }
  2022. spin_unlock_irqrestore(&n->list_lock, flags);
  2023. return;
  2024. slab_empty:
  2025. if (prior) {
  2026. /*
  2027. * Slab on the partial list.
  2028. */
  2029. remove_partial(n, page);
  2030. stat(s, FREE_REMOVE_PARTIAL);
  2031. } else
  2032. /* Slab must be on the full list */
  2033. remove_full(s, page);
  2034. spin_unlock_irqrestore(&n->list_lock, flags);
  2035. stat(s, FREE_SLAB);
  2036. discard_slab(s, page);
  2037. }
  2038. /*
  2039. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  2040. * can perform fastpath freeing without additional function calls.
  2041. *
  2042. * The fastpath is only possible if we are freeing to the current cpu slab
  2043. * of this processor. This typically the case if we have just allocated
  2044. * the item before.
  2045. *
  2046. * If fastpath is not possible then fall back to __slab_free where we deal
  2047. * with all sorts of special processing.
  2048. */
  2049. static __always_inline void slab_free(struct kmem_cache *s,
  2050. struct page *page, void *x, unsigned long addr)
  2051. {
  2052. void **object = (void *)x;
  2053. struct kmem_cache_cpu *c;
  2054. unsigned long tid;
  2055. slab_free_hook(s, x);
  2056. redo:
  2057. /*
  2058. * Determine the currently cpus per cpu slab.
  2059. * The cpu may change afterward. However that does not matter since
  2060. * data is retrieved via this pointer. If we are on the same cpu
  2061. * during the cmpxchg then the free will succedd.
  2062. */
  2063. c = __this_cpu_ptr(s->cpu_slab);
  2064. tid = c->tid;
  2065. barrier();
  2066. if (likely(page == c->page)) {
  2067. set_freepointer(s, object, c->freelist);
  2068. if (unlikely(!irqsafe_cpu_cmpxchg_double(
  2069. s->cpu_slab->freelist, s->cpu_slab->tid,
  2070. c->freelist, tid,
  2071. object, next_tid(tid)))) {
  2072. note_cmpxchg_failure("slab_free", s, tid);
  2073. goto redo;
  2074. }
  2075. stat(s, FREE_FASTPATH);
  2076. } else
  2077. __slab_free(s, page, x, addr);
  2078. }
  2079. void kmem_cache_free(struct kmem_cache *s, void *x)
  2080. {
  2081. struct page *page;
  2082. page = virt_to_head_page(x);
  2083. slab_free(s, page, x, _RET_IP_);
  2084. trace_kmem_cache_free(_RET_IP_, x);
  2085. }
  2086. EXPORT_SYMBOL(kmem_cache_free);
  2087. /*
  2088. * Object placement in a slab is made very easy because we always start at
  2089. * offset 0. If we tune the size of the object to the alignment then we can
  2090. * get the required alignment by putting one properly sized object after
  2091. * another.
  2092. *
  2093. * Notice that the allocation order determines the sizes of the per cpu
  2094. * caches. Each processor has always one slab available for allocations.
  2095. * Increasing the allocation order reduces the number of times that slabs
  2096. * must be moved on and off the partial lists and is therefore a factor in
  2097. * locking overhead.
  2098. */
  2099. /*
  2100. * Mininum / Maximum order of slab pages. This influences locking overhead
  2101. * and slab fragmentation. A higher order reduces the number of partial slabs
  2102. * and increases the number of allocations possible without having to
  2103. * take the list_lock.
  2104. */
  2105. static int slub_min_order;
  2106. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  2107. static int slub_min_objects;
  2108. /*
  2109. * Merge control. If this is set then no merging of slab caches will occur.
  2110. * (Could be removed. This was introduced to pacify the merge skeptics.)
  2111. */
  2112. static int slub_nomerge;
  2113. /*
  2114. * Calculate the order of allocation given an slab object size.
  2115. *
  2116. * The order of allocation has significant impact on performance and other
  2117. * system components. Generally order 0 allocations should be preferred since
  2118. * order 0 does not cause fragmentation in the page allocator. Larger objects
  2119. * be problematic to put into order 0 slabs because there may be too much
  2120. * unused space left. We go to a higher order if more than 1/16th of the slab
  2121. * would be wasted.
  2122. *
  2123. * In order to reach satisfactory performance we must ensure that a minimum
  2124. * number of objects is in one slab. Otherwise we may generate too much
  2125. * activity on the partial lists which requires taking the list_lock. This is
  2126. * less a concern for large slabs though which are rarely used.
  2127. *
  2128. * slub_max_order specifies the order where we begin to stop considering the
  2129. * number of objects in a slab as critical. If we reach slub_max_order then
  2130. * we try to keep the page order as low as possible. So we accept more waste
  2131. * of space in favor of a small page order.
  2132. *
  2133. * Higher order allocations also allow the placement of more objects in a
  2134. * slab and thereby reduce object handling overhead. If the user has
  2135. * requested a higher mininum order then we start with that one instead of
  2136. * the smallest order which will fit the object.
  2137. */
  2138. static inline int slab_order(int size, int min_objects,
  2139. int max_order, int fract_leftover, int reserved)
  2140. {
  2141. int order;
  2142. int rem;
  2143. int min_order = slub_min_order;
  2144. if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
  2145. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  2146. for (order = max(min_order,
  2147. fls(min_objects * size - 1) - PAGE_SHIFT);
  2148. order <= max_order; order++) {
  2149. unsigned long slab_size = PAGE_SIZE << order;
  2150. if (slab_size < min_objects * size + reserved)
  2151. continue;
  2152. rem = (slab_size - reserved) % size;
  2153. if (rem <= slab_size / fract_leftover)
  2154. break;
  2155. }
  2156. return order;
  2157. }
  2158. static inline int calculate_order(int size, int reserved)
  2159. {
  2160. int order;
  2161. int min_objects;
  2162. int fraction;
  2163. int max_objects;
  2164. /*
  2165. * Attempt to find best configuration for a slab. This
  2166. * works by first attempting to generate a layout with
  2167. * the best configuration and backing off gradually.
  2168. *
  2169. * First we reduce the acceptable waste in a slab. Then
  2170. * we reduce the minimum objects required in a slab.
  2171. */
  2172. min_objects = slub_min_objects;
  2173. if (!min_objects)
  2174. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  2175. max_objects = order_objects(slub_max_order, size, reserved);
  2176. min_objects = min(min_objects, max_objects);
  2177. while (min_objects > 1) {
  2178. fraction = 16;
  2179. while (fraction >= 4) {
  2180. order = slab_order(size, min_objects,
  2181. slub_max_order, fraction, reserved);
  2182. if (order <= slub_max_order)
  2183. return order;
  2184. fraction /= 2;
  2185. }
  2186. min_objects--;
  2187. }
  2188. /*
  2189. * We were unable to place multiple objects in a slab. Now
  2190. * lets see if we can place a single object there.
  2191. */
  2192. order = slab_order(size, 1, slub_max_order, 1, reserved);
  2193. if (order <= slub_max_order)
  2194. return order;
  2195. /*
  2196. * Doh this slab cannot be placed using slub_max_order.
  2197. */
  2198. order = slab_order(size, 1, MAX_ORDER, 1, reserved);
  2199. if (order < MAX_ORDER)
  2200. return order;
  2201. return -ENOSYS;
  2202. }
  2203. /*
  2204. * Figure out what the alignment of the objects will be.
  2205. */
  2206. static unsigned long calculate_alignment(unsigned long flags,
  2207. unsigned long align, unsigned long size)
  2208. {
  2209. /*
  2210. * If the user wants hardware cache aligned objects then follow that
  2211. * suggestion if the object is sufficiently large.
  2212. *
  2213. * The hardware cache alignment cannot override the specified
  2214. * alignment though. If that is greater then use it.
  2215. */
  2216. if (flags & SLAB_HWCACHE_ALIGN) {
  2217. unsigned long ralign = cache_line_size();
  2218. while (size <= ralign / 2)
  2219. ralign /= 2;
  2220. align = max(align, ralign);
  2221. }
  2222. if (align < ARCH_SLAB_MINALIGN)
  2223. align = ARCH_SLAB_MINALIGN;
  2224. return ALIGN(align, sizeof(void *));
  2225. }
  2226. static void
  2227. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  2228. {
  2229. n->nr_partial = 0;
  2230. spin_lock_init(&n->list_lock);
  2231. INIT_LIST_HEAD(&n->partial);
  2232. #ifdef CONFIG_SLUB_DEBUG
  2233. atomic_long_set(&n->nr_slabs, 0);
  2234. atomic_long_set(&n->total_objects, 0);
  2235. INIT_LIST_HEAD(&n->full);
  2236. #endif
  2237. }
  2238. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
  2239. {
  2240. BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
  2241. SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
  2242. /*
  2243. * Must align to double word boundary for the double cmpxchg
  2244. * instructions to work; see __pcpu_double_call_return_bool().
  2245. */
  2246. s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
  2247. 2 * sizeof(void *));
  2248. if (!s->cpu_slab)
  2249. return 0;
  2250. init_kmem_cache_cpus(s);
  2251. return 1;
  2252. }
  2253. static struct kmem_cache *kmem_cache_node;
  2254. /*
  2255. * No kmalloc_node yet so do it by hand. We know that this is the first
  2256. * slab on the node for this slabcache. There are no concurrent accesses
  2257. * possible.
  2258. *
  2259. * Note that this function only works on the kmalloc_node_cache
  2260. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  2261. * memory on a fresh node that has no slab structures yet.
  2262. */
  2263. static void early_kmem_cache_node_alloc(int node)
  2264. {
  2265. struct page *page;
  2266. struct kmem_cache_node *n;
  2267. BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
  2268. page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
  2269. BUG_ON(!page);
  2270. if (page_to_nid(page) != node) {
  2271. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  2272. "node %d\n", node);
  2273. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  2274. "in order to be able to continue\n");
  2275. }
  2276. n = page->freelist;
  2277. BUG_ON(!n);
  2278. page->freelist = get_freepointer(kmem_cache_node, n);
  2279. page->inuse = 1;
  2280. page->frozen = 0;
  2281. kmem_cache_node->node[node] = n;
  2282. #ifdef CONFIG_SLUB_DEBUG
  2283. init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
  2284. init_tracking(kmem_cache_node, n);
  2285. #endif
  2286. init_kmem_cache_node(n, kmem_cache_node);
  2287. inc_slabs_node(kmem_cache_node, node, page->objects);
  2288. add_partial(n, page, 0);
  2289. }
  2290. static void free_kmem_cache_nodes(struct kmem_cache *s)
  2291. {
  2292. int node;
  2293. for_each_node_state(node, N_NORMAL_MEMORY) {
  2294. struct kmem_cache_node *n = s->node[node];
  2295. if (n)
  2296. kmem_cache_free(kmem_cache_node, n);
  2297. s->node[node] = NULL;
  2298. }
  2299. }
  2300. static int init_kmem_cache_nodes(struct kmem_cache *s)
  2301. {
  2302. int node;
  2303. for_each_node_state(node, N_NORMAL_MEMORY) {
  2304. struct kmem_cache_node *n;
  2305. if (slab_state == DOWN) {
  2306. early_kmem_cache_node_alloc(node);
  2307. continue;
  2308. }
  2309. n = kmem_cache_alloc_node(kmem_cache_node,
  2310. GFP_KERNEL, node);
  2311. if (!n) {
  2312. free_kmem_cache_nodes(s);
  2313. return 0;
  2314. }
  2315. s->node[node] = n;
  2316. init_kmem_cache_node(n, s);
  2317. }
  2318. return 1;
  2319. }
  2320. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  2321. {
  2322. if (min < MIN_PARTIAL)
  2323. min = MIN_PARTIAL;
  2324. else if (min > MAX_PARTIAL)
  2325. min = MAX_PARTIAL;
  2326. s->min_partial = min;
  2327. }
  2328. /*
  2329. * calculate_sizes() determines the order and the distribution of data within
  2330. * a slab object.
  2331. */
  2332. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  2333. {
  2334. unsigned long flags = s->flags;
  2335. unsigned long size = s->objsize;
  2336. unsigned long align = s->align;
  2337. int order;
  2338. /*
  2339. * Round up object size to the next word boundary. We can only
  2340. * place the free pointer at word boundaries and this determines
  2341. * the possible location of the free pointer.
  2342. */
  2343. size = ALIGN(size, sizeof(void *));
  2344. #ifdef CONFIG_SLUB_DEBUG
  2345. /*
  2346. * Determine if we can poison the object itself. If the user of
  2347. * the slab may touch the object after free or before allocation
  2348. * then we should never poison the object itself.
  2349. */
  2350. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  2351. !s->ctor)
  2352. s->flags |= __OBJECT_POISON;
  2353. else
  2354. s->flags &= ~__OBJECT_POISON;
  2355. /*
  2356. * If we are Redzoning then check if there is some space between the
  2357. * end of the object and the free pointer. If not then add an
  2358. * additional word to have some bytes to store Redzone information.
  2359. */
  2360. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  2361. size += sizeof(void *);
  2362. #endif
  2363. /*
  2364. * With that we have determined the number of bytes in actual use
  2365. * by the object. This is the potential offset to the free pointer.
  2366. */
  2367. s->inuse = size;
  2368. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  2369. s->ctor)) {
  2370. /*
  2371. * Relocate free pointer after the object if it is not
  2372. * permitted to overwrite the first word of the object on
  2373. * kmem_cache_free.
  2374. *
  2375. * This is the case if we do RCU, have a constructor or
  2376. * destructor or are poisoning the objects.
  2377. */
  2378. s->offset = size;
  2379. size += sizeof(void *);
  2380. }
  2381. #ifdef CONFIG_SLUB_DEBUG
  2382. if (flags & SLAB_STORE_USER)
  2383. /*
  2384. * Need to store information about allocs and frees after
  2385. * the object.
  2386. */
  2387. size += 2 * sizeof(struct track);
  2388. if (flags & SLAB_RED_ZONE)
  2389. /*
  2390. * Add some empty padding so that we can catch
  2391. * overwrites from earlier objects rather than let
  2392. * tracking information or the free pointer be
  2393. * corrupted if a user writes before the start
  2394. * of the object.
  2395. */
  2396. size += sizeof(void *);
  2397. #endif
  2398. /*
  2399. * Determine the alignment based on various parameters that the
  2400. * user specified and the dynamic determination of cache line size
  2401. * on bootup.
  2402. */
  2403. align = calculate_alignment(flags, align, s->objsize);
  2404. s->align = align;
  2405. /*
  2406. * SLUB stores one object immediately after another beginning from
  2407. * offset 0. In order to align the objects we have to simply size
  2408. * each object to conform to the alignment.
  2409. */
  2410. size = ALIGN(size, align);
  2411. s->size = size;
  2412. if (forced_order >= 0)
  2413. order = forced_order;
  2414. else
  2415. order = calculate_order(size, s->reserved);
  2416. if (order < 0)
  2417. return 0;
  2418. s->allocflags = 0;
  2419. if (order)
  2420. s->allocflags |= __GFP_COMP;
  2421. if (s->flags & SLAB_CACHE_DMA)
  2422. s->allocflags |= SLUB_DMA;
  2423. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  2424. s->allocflags |= __GFP_RECLAIMABLE;
  2425. /*
  2426. * Determine the number of objects per slab
  2427. */
  2428. s->oo = oo_make(order, size, s->reserved);
  2429. s->min = oo_make(get_order(size), size, s->reserved);
  2430. if (oo_objects(s->oo) > oo_objects(s->max))
  2431. s->max = s->oo;
  2432. return !!oo_objects(s->oo);
  2433. }
  2434. static int kmem_cache_open(struct kmem_cache *s,
  2435. const char *name, size_t size,
  2436. size_t align, unsigned long flags,
  2437. void (*ctor)(void *))
  2438. {
  2439. memset(s, 0, kmem_size);
  2440. s->name = name;
  2441. s->ctor = ctor;
  2442. s->objsize = size;
  2443. s->align = align;
  2444. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2445. s->reserved = 0;
  2446. if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
  2447. s->reserved = sizeof(struct rcu_head);
  2448. if (!calculate_sizes(s, -1))
  2449. goto error;
  2450. if (disable_higher_order_debug) {
  2451. /*
  2452. * Disable debugging flags that store metadata if the min slab
  2453. * order increased.
  2454. */
  2455. if (get_order(s->size) > get_order(s->objsize)) {
  2456. s->flags &= ~DEBUG_METADATA_FLAGS;
  2457. s->offset = 0;
  2458. if (!calculate_sizes(s, -1))
  2459. goto error;
  2460. }
  2461. }
  2462. #ifdef CONFIG_CMPXCHG_DOUBLE
  2463. if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
  2464. /* Enable fast mode */
  2465. s->flags |= __CMPXCHG_DOUBLE;
  2466. #endif
  2467. /*
  2468. * The larger the object size is, the more pages we want on the partial
  2469. * list to avoid pounding the page allocator excessively.
  2470. */
  2471. set_min_partial(s, ilog2(s->size));
  2472. s->refcount = 1;
  2473. #ifdef CONFIG_NUMA
  2474. s->remote_node_defrag_ratio = 1000;
  2475. #endif
  2476. if (!init_kmem_cache_nodes(s))
  2477. goto error;
  2478. if (alloc_kmem_cache_cpus(s))
  2479. return 1;
  2480. free_kmem_cache_nodes(s);
  2481. error:
  2482. if (flags & SLAB_PANIC)
  2483. panic("Cannot create slab %s size=%lu realsize=%u "
  2484. "order=%u offset=%u flags=%lx\n",
  2485. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2486. s->offset, flags);
  2487. return 0;
  2488. }
  2489. /*
  2490. * Determine the size of a slab object
  2491. */
  2492. unsigned int kmem_cache_size(struct kmem_cache *s)
  2493. {
  2494. return s->objsize;
  2495. }
  2496. EXPORT_SYMBOL(kmem_cache_size);
  2497. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2498. const char *text)
  2499. {
  2500. #ifdef CONFIG_SLUB_DEBUG
  2501. void *addr = page_address(page);
  2502. void *p;
  2503. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  2504. sizeof(long), GFP_ATOMIC);
  2505. if (!map)
  2506. return;
  2507. slab_err(s, page, "%s", text);
  2508. slab_lock(page);
  2509. get_map(s, page, map);
  2510. for_each_object(p, s, addr, page->objects) {
  2511. if (!test_bit(slab_index(p, s, addr), map)) {
  2512. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2513. p, p - addr);
  2514. print_tracking(s, p);
  2515. }
  2516. }
  2517. slab_unlock(page);
  2518. kfree(map);
  2519. #endif
  2520. }
  2521. /*
  2522. * Attempt to free all partial slabs on a node.
  2523. * This is called from kmem_cache_close(). We must be the last thread
  2524. * using the cache and therefore we do not need to lock anymore.
  2525. */
  2526. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2527. {
  2528. struct page *page, *h;
  2529. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2530. if (!page->inuse) {
  2531. remove_partial(n, page);
  2532. discard_slab(s, page);
  2533. } else {
  2534. list_slab_objects(s, page,
  2535. "Objects remaining on kmem_cache_close()");
  2536. }
  2537. }
  2538. }
  2539. /*
  2540. * Release all resources used by a slab cache.
  2541. */
  2542. static inline int kmem_cache_close(struct kmem_cache *s)
  2543. {
  2544. int node;
  2545. flush_all(s);
  2546. free_percpu(s->cpu_slab);
  2547. /* Attempt to free all objects */
  2548. for_each_node_state(node, N_NORMAL_MEMORY) {
  2549. struct kmem_cache_node *n = get_node(s, node);
  2550. free_partial(s, n);
  2551. if (n->nr_partial || slabs_node(s, node))
  2552. return 1;
  2553. }
  2554. free_kmem_cache_nodes(s);
  2555. return 0;
  2556. }
  2557. /*
  2558. * Close a cache and release the kmem_cache structure
  2559. * (must be used for caches created using kmem_cache_create)
  2560. */
  2561. void kmem_cache_destroy(struct kmem_cache *s)
  2562. {
  2563. down_write(&slub_lock);
  2564. s->refcount--;
  2565. if (!s->refcount) {
  2566. list_del(&s->list);
  2567. up_write(&slub_lock);
  2568. if (kmem_cache_close(s)) {
  2569. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2570. "still has objects.\n", s->name, __func__);
  2571. dump_stack();
  2572. }
  2573. if (s->flags & SLAB_DESTROY_BY_RCU)
  2574. rcu_barrier();
  2575. sysfs_slab_remove(s);
  2576. } else
  2577. up_write(&slub_lock);
  2578. }
  2579. EXPORT_SYMBOL(kmem_cache_destroy);
  2580. /********************************************************************
  2581. * Kmalloc subsystem
  2582. *******************************************************************/
  2583. struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
  2584. EXPORT_SYMBOL(kmalloc_caches);
  2585. static struct kmem_cache *kmem_cache;
  2586. #ifdef CONFIG_ZONE_DMA
  2587. static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
  2588. #endif
  2589. static int __init setup_slub_min_order(char *str)
  2590. {
  2591. get_option(&str, &slub_min_order);
  2592. return 1;
  2593. }
  2594. __setup("slub_min_order=", setup_slub_min_order);
  2595. static int __init setup_slub_max_order(char *str)
  2596. {
  2597. get_option(&str, &slub_max_order);
  2598. slub_max_order = min(slub_max_order, MAX_ORDER - 1);
  2599. return 1;
  2600. }
  2601. __setup("slub_max_order=", setup_slub_max_order);
  2602. static int __init setup_slub_min_objects(char *str)
  2603. {
  2604. get_option(&str, &slub_min_objects);
  2605. return 1;
  2606. }
  2607. __setup("slub_min_objects=", setup_slub_min_objects);
  2608. static int __init setup_slub_nomerge(char *str)
  2609. {
  2610. slub_nomerge = 1;
  2611. return 1;
  2612. }
  2613. __setup("slub_nomerge", setup_slub_nomerge);
  2614. static struct kmem_cache *__init create_kmalloc_cache(const char *name,
  2615. int size, unsigned int flags)
  2616. {
  2617. struct kmem_cache *s;
  2618. s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2619. /*
  2620. * This function is called with IRQs disabled during early-boot on
  2621. * single CPU so there's no need to take slub_lock here.
  2622. */
  2623. if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
  2624. flags, NULL))
  2625. goto panic;
  2626. list_add(&s->list, &slab_caches);
  2627. return s;
  2628. panic:
  2629. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2630. return NULL;
  2631. }
  2632. /*
  2633. * Conversion table for small slabs sizes / 8 to the index in the
  2634. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2635. * of two cache sizes there. The size of larger slabs can be determined using
  2636. * fls.
  2637. */
  2638. static s8 size_index[24] = {
  2639. 3, /* 8 */
  2640. 4, /* 16 */
  2641. 5, /* 24 */
  2642. 5, /* 32 */
  2643. 6, /* 40 */
  2644. 6, /* 48 */
  2645. 6, /* 56 */
  2646. 6, /* 64 */
  2647. 1, /* 72 */
  2648. 1, /* 80 */
  2649. 1, /* 88 */
  2650. 1, /* 96 */
  2651. 7, /* 104 */
  2652. 7, /* 112 */
  2653. 7, /* 120 */
  2654. 7, /* 128 */
  2655. 2, /* 136 */
  2656. 2, /* 144 */
  2657. 2, /* 152 */
  2658. 2, /* 160 */
  2659. 2, /* 168 */
  2660. 2, /* 176 */
  2661. 2, /* 184 */
  2662. 2 /* 192 */
  2663. };
  2664. static inline int size_index_elem(size_t bytes)
  2665. {
  2666. return (bytes - 1) / 8;
  2667. }
  2668. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2669. {
  2670. int index;
  2671. if (size <= 192) {
  2672. if (!size)
  2673. return ZERO_SIZE_PTR;
  2674. index = size_index[size_index_elem(size)];
  2675. } else
  2676. index = fls(size - 1);
  2677. #ifdef CONFIG_ZONE_DMA
  2678. if (unlikely((flags & SLUB_DMA)))
  2679. return kmalloc_dma_caches[index];
  2680. #endif
  2681. return kmalloc_caches[index];
  2682. }
  2683. void *__kmalloc(size_t size, gfp_t flags)
  2684. {
  2685. struct kmem_cache *s;
  2686. void *ret;
  2687. if (unlikely(size > SLUB_MAX_SIZE))
  2688. return kmalloc_large(size, flags);
  2689. s = get_slab(size, flags);
  2690. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2691. return s;
  2692. ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
  2693. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  2694. return ret;
  2695. }
  2696. EXPORT_SYMBOL(__kmalloc);
  2697. #ifdef CONFIG_NUMA
  2698. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2699. {
  2700. struct page *page;
  2701. void *ptr = NULL;
  2702. flags |= __GFP_COMP | __GFP_NOTRACK;
  2703. page = alloc_pages_node(node, flags, get_order(size));
  2704. if (page)
  2705. ptr = page_address(page);
  2706. kmemleak_alloc(ptr, size, 1, flags);
  2707. return ptr;
  2708. }
  2709. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2710. {
  2711. struct kmem_cache *s;
  2712. void *ret;
  2713. if (unlikely(size > SLUB_MAX_SIZE)) {
  2714. ret = kmalloc_large_node(size, flags, node);
  2715. trace_kmalloc_node(_RET_IP_, ret,
  2716. size, PAGE_SIZE << get_order(size),
  2717. flags, node);
  2718. return ret;
  2719. }
  2720. s = get_slab(size, flags);
  2721. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2722. return s;
  2723. ret = slab_alloc(s, flags, node, _RET_IP_);
  2724. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  2725. return ret;
  2726. }
  2727. EXPORT_SYMBOL(__kmalloc_node);
  2728. #endif
  2729. size_t ksize(const void *object)
  2730. {
  2731. struct page *page;
  2732. if (unlikely(object == ZERO_SIZE_PTR))
  2733. return 0;
  2734. page = virt_to_head_page(object);
  2735. if (unlikely(!PageSlab(page))) {
  2736. WARN_ON(!PageCompound(page));
  2737. return PAGE_SIZE << compound_order(page);
  2738. }
  2739. return slab_ksize(page->slab);
  2740. }
  2741. EXPORT_SYMBOL(ksize);
  2742. #ifdef CONFIG_SLUB_DEBUG
  2743. bool verify_mem_not_deleted(const void *x)
  2744. {
  2745. struct page *page;
  2746. void *object = (void *)x;
  2747. unsigned long flags;
  2748. bool rv;
  2749. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2750. return false;
  2751. local_irq_save(flags);
  2752. page = virt_to_head_page(x);
  2753. if (unlikely(!PageSlab(page))) {
  2754. /* maybe it was from stack? */
  2755. rv = true;
  2756. goto out_unlock;
  2757. }
  2758. slab_lock(page);
  2759. if (on_freelist(page->slab, page, object)) {
  2760. object_err(page->slab, page, object, "Object is on free-list");
  2761. rv = false;
  2762. } else {
  2763. rv = true;
  2764. }
  2765. slab_unlock(page);
  2766. out_unlock:
  2767. local_irq_restore(flags);
  2768. return rv;
  2769. }
  2770. EXPORT_SYMBOL(verify_mem_not_deleted);
  2771. #endif
  2772. void kfree(const void *x)
  2773. {
  2774. struct page *page;
  2775. void *object = (void *)x;
  2776. trace_kfree(_RET_IP_, x);
  2777. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2778. return;
  2779. page = virt_to_head_page(x);
  2780. if (unlikely(!PageSlab(page))) {
  2781. BUG_ON(!PageCompound(page));
  2782. kmemleak_free(x);
  2783. put_page(page);
  2784. return;
  2785. }
  2786. slab_free(page->slab, page, object, _RET_IP_);
  2787. }
  2788. EXPORT_SYMBOL(kfree);
  2789. /*
  2790. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2791. * the remaining slabs by the number of items in use. The slabs with the
  2792. * most items in use come first. New allocations will then fill those up
  2793. * and thus they can be removed from the partial lists.
  2794. *
  2795. * The slabs with the least items are placed last. This results in them
  2796. * being allocated from last increasing the chance that the last objects
  2797. * are freed in them.
  2798. */
  2799. int kmem_cache_shrink(struct kmem_cache *s)
  2800. {
  2801. int node;
  2802. int i;
  2803. struct kmem_cache_node *n;
  2804. struct page *page;
  2805. struct page *t;
  2806. int objects = oo_objects(s->max);
  2807. struct list_head *slabs_by_inuse =
  2808. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2809. unsigned long flags;
  2810. if (!slabs_by_inuse)
  2811. return -ENOMEM;
  2812. flush_all(s);
  2813. for_each_node_state(node, N_NORMAL_MEMORY) {
  2814. n = get_node(s, node);
  2815. if (!n->nr_partial)
  2816. continue;
  2817. for (i = 0; i < objects; i++)
  2818. INIT_LIST_HEAD(slabs_by_inuse + i);
  2819. spin_lock_irqsave(&n->list_lock, flags);
  2820. /*
  2821. * Build lists indexed by the items in use in each slab.
  2822. *
  2823. * Note that concurrent frees may occur while we hold the
  2824. * list_lock. page->inuse here is the upper limit.
  2825. */
  2826. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2827. list_move(&page->lru, slabs_by_inuse + page->inuse);
  2828. if (!page->inuse)
  2829. n->nr_partial--;
  2830. }
  2831. /*
  2832. * Rebuild the partial list with the slabs filled up most
  2833. * first and the least used slabs at the end.
  2834. */
  2835. for (i = objects - 1; i > 0; i--)
  2836. list_splice(slabs_by_inuse + i, n->partial.prev);
  2837. spin_unlock_irqrestore(&n->list_lock, flags);
  2838. /* Release empty slabs */
  2839. list_for_each_entry_safe(page, t, slabs_by_inuse, lru)
  2840. discard_slab(s, page);
  2841. }
  2842. kfree(slabs_by_inuse);
  2843. return 0;
  2844. }
  2845. EXPORT_SYMBOL(kmem_cache_shrink);
  2846. #if defined(CONFIG_MEMORY_HOTPLUG)
  2847. static int slab_mem_going_offline_callback(void *arg)
  2848. {
  2849. struct kmem_cache *s;
  2850. down_read(&slub_lock);
  2851. list_for_each_entry(s, &slab_caches, list)
  2852. kmem_cache_shrink(s);
  2853. up_read(&slub_lock);
  2854. return 0;
  2855. }
  2856. static void slab_mem_offline_callback(void *arg)
  2857. {
  2858. struct kmem_cache_node *n;
  2859. struct kmem_cache *s;
  2860. struct memory_notify *marg = arg;
  2861. int offline_node;
  2862. offline_node = marg->status_change_nid;
  2863. /*
  2864. * If the node still has available memory. we need kmem_cache_node
  2865. * for it yet.
  2866. */
  2867. if (offline_node < 0)
  2868. return;
  2869. down_read(&slub_lock);
  2870. list_for_each_entry(s, &slab_caches, list) {
  2871. n = get_node(s, offline_node);
  2872. if (n) {
  2873. /*
  2874. * if n->nr_slabs > 0, slabs still exist on the node
  2875. * that is going down. We were unable to free them,
  2876. * and offline_pages() function shouldn't call this
  2877. * callback. So, we must fail.
  2878. */
  2879. BUG_ON(slabs_node(s, offline_node));
  2880. s->node[offline_node] = NULL;
  2881. kmem_cache_free(kmem_cache_node, n);
  2882. }
  2883. }
  2884. up_read(&slub_lock);
  2885. }
  2886. static int slab_mem_going_online_callback(void *arg)
  2887. {
  2888. struct kmem_cache_node *n;
  2889. struct kmem_cache *s;
  2890. struct memory_notify *marg = arg;
  2891. int nid = marg->status_change_nid;
  2892. int ret = 0;
  2893. /*
  2894. * If the node's memory is already available, then kmem_cache_node is
  2895. * already created. Nothing to do.
  2896. */
  2897. if (nid < 0)
  2898. return 0;
  2899. /*
  2900. * We are bringing a node online. No memory is available yet. We must
  2901. * allocate a kmem_cache_node structure in order to bring the node
  2902. * online.
  2903. */
  2904. down_read(&slub_lock);
  2905. list_for_each_entry(s, &slab_caches, list) {
  2906. /*
  2907. * XXX: kmem_cache_alloc_node will fallback to other nodes
  2908. * since memory is not yet available from the node that
  2909. * is brought up.
  2910. */
  2911. n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
  2912. if (!n) {
  2913. ret = -ENOMEM;
  2914. goto out;
  2915. }
  2916. init_kmem_cache_node(n, s);
  2917. s->node[nid] = n;
  2918. }
  2919. out:
  2920. up_read(&slub_lock);
  2921. return ret;
  2922. }
  2923. static int slab_memory_callback(struct notifier_block *self,
  2924. unsigned long action, void *arg)
  2925. {
  2926. int ret = 0;
  2927. switch (action) {
  2928. case MEM_GOING_ONLINE:
  2929. ret = slab_mem_going_online_callback(arg);
  2930. break;
  2931. case MEM_GOING_OFFLINE:
  2932. ret = slab_mem_going_offline_callback(arg);
  2933. break;
  2934. case MEM_OFFLINE:
  2935. case MEM_CANCEL_ONLINE:
  2936. slab_mem_offline_callback(arg);
  2937. break;
  2938. case MEM_ONLINE:
  2939. case MEM_CANCEL_OFFLINE:
  2940. break;
  2941. }
  2942. if (ret)
  2943. ret = notifier_from_errno(ret);
  2944. else
  2945. ret = NOTIFY_OK;
  2946. return ret;
  2947. }
  2948. #endif /* CONFIG_MEMORY_HOTPLUG */
  2949. /********************************************************************
  2950. * Basic setup of slabs
  2951. *******************************************************************/
  2952. /*
  2953. * Used for early kmem_cache structures that were allocated using
  2954. * the page allocator
  2955. */
  2956. static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
  2957. {
  2958. int node;
  2959. list_add(&s->list, &slab_caches);
  2960. s->refcount = -1;
  2961. for_each_node_state(node, N_NORMAL_MEMORY) {
  2962. struct kmem_cache_node *n = get_node(s, node);
  2963. struct page *p;
  2964. if (n) {
  2965. list_for_each_entry(p, &n->partial, lru)
  2966. p->slab = s;
  2967. #ifdef CONFIG_SLUB_DEBUG
  2968. list_for_each_entry(p, &n->full, lru)
  2969. p->slab = s;
  2970. #endif
  2971. }
  2972. }
  2973. }
  2974. void __init kmem_cache_init(void)
  2975. {
  2976. int i;
  2977. int caches = 0;
  2978. struct kmem_cache *temp_kmem_cache;
  2979. int order;
  2980. struct kmem_cache *temp_kmem_cache_node;
  2981. unsigned long kmalloc_size;
  2982. kmem_size = offsetof(struct kmem_cache, node) +
  2983. nr_node_ids * sizeof(struct kmem_cache_node *);
  2984. /* Allocate two kmem_caches from the page allocator */
  2985. kmalloc_size = ALIGN(kmem_size, cache_line_size());
  2986. order = get_order(2 * kmalloc_size);
  2987. kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
  2988. /*
  2989. * Must first have the slab cache available for the allocations of the
  2990. * struct kmem_cache_node's. There is special bootstrap code in
  2991. * kmem_cache_open for slab_state == DOWN.
  2992. */
  2993. kmem_cache_node = (void *)kmem_cache + kmalloc_size;
  2994. kmem_cache_open(kmem_cache_node, "kmem_cache_node",
  2995. sizeof(struct kmem_cache_node),
  2996. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2997. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  2998. /* Able to allocate the per node structures */
  2999. slab_state = PARTIAL;
  3000. temp_kmem_cache = kmem_cache;
  3001. kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
  3002. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  3003. kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  3004. memcpy(kmem_cache, temp_kmem_cache, kmem_size);
  3005. /*
  3006. * Allocate kmem_cache_node properly from the kmem_cache slab.
  3007. * kmem_cache_node is separately allocated so no need to
  3008. * update any list pointers.
  3009. */
  3010. temp_kmem_cache_node = kmem_cache_node;
  3011. kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  3012. memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
  3013. kmem_cache_bootstrap_fixup(kmem_cache_node);
  3014. caches++;
  3015. kmem_cache_bootstrap_fixup(kmem_cache);
  3016. caches++;
  3017. /* Free temporary boot structure */
  3018. free_pages((unsigned long)temp_kmem_cache, order);
  3019. /* Now we can use the kmem_cache to allocate kmalloc slabs */
  3020. /*
  3021. * Patch up the size_index table if we have strange large alignment
  3022. * requirements for the kmalloc array. This is only the case for
  3023. * MIPS it seems. The standard arches will not generate any code here.
  3024. *
  3025. * Largest permitted alignment is 256 bytes due to the way we
  3026. * handle the index determination for the smaller caches.
  3027. *
  3028. * Make sure that nothing crazy happens if someone starts tinkering
  3029. * around with ARCH_KMALLOC_MINALIGN
  3030. */
  3031. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  3032. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  3033. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  3034. int elem = size_index_elem(i);
  3035. if (elem >= ARRAY_SIZE(size_index))
  3036. break;
  3037. size_index[elem] = KMALLOC_SHIFT_LOW;
  3038. }
  3039. if (KMALLOC_MIN_SIZE == 64) {
  3040. /*
  3041. * The 96 byte size cache is not used if the alignment
  3042. * is 64 byte.
  3043. */
  3044. for (i = 64 + 8; i <= 96; i += 8)
  3045. size_index[size_index_elem(i)] = 7;
  3046. } else if (KMALLOC_MIN_SIZE == 128) {
  3047. /*
  3048. * The 192 byte sized cache is not used if the alignment
  3049. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  3050. * instead.
  3051. */
  3052. for (i = 128 + 8; i <= 192; i += 8)
  3053. size_index[size_index_elem(i)] = 8;
  3054. }
  3055. /* Caches that are not of the two-to-the-power-of size */
  3056. if (KMALLOC_MIN_SIZE <= 32) {
  3057. kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
  3058. caches++;
  3059. }
  3060. if (KMALLOC_MIN_SIZE <= 64) {
  3061. kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
  3062. caches++;
  3063. }
  3064. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  3065. kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
  3066. caches++;
  3067. }
  3068. slab_state = UP;
  3069. /* Provide the correct kmalloc names now that the caches are up */
  3070. if (KMALLOC_MIN_SIZE <= 32) {
  3071. kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
  3072. BUG_ON(!kmalloc_caches[1]->name);
  3073. }
  3074. if (KMALLOC_MIN_SIZE <= 64) {
  3075. kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
  3076. BUG_ON(!kmalloc_caches[2]->name);
  3077. }
  3078. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  3079. char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
  3080. BUG_ON(!s);
  3081. kmalloc_caches[i]->name = s;
  3082. }
  3083. #ifdef CONFIG_SMP
  3084. register_cpu_notifier(&slab_notifier);
  3085. #endif
  3086. #ifdef CONFIG_ZONE_DMA
  3087. for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
  3088. struct kmem_cache *s = kmalloc_caches[i];
  3089. if (s && s->size) {
  3090. char *name = kasprintf(GFP_NOWAIT,
  3091. "dma-kmalloc-%d", s->objsize);
  3092. BUG_ON(!name);
  3093. kmalloc_dma_caches[i] = create_kmalloc_cache(name,
  3094. s->objsize, SLAB_CACHE_DMA);
  3095. }
  3096. }
  3097. #endif
  3098. printk(KERN_INFO
  3099. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  3100. " CPUs=%d, Nodes=%d\n",
  3101. caches, cache_line_size(),
  3102. slub_min_order, slub_max_order, slub_min_objects,
  3103. nr_cpu_ids, nr_node_ids);
  3104. }
  3105. void __init kmem_cache_init_late(void)
  3106. {
  3107. }
  3108. /*
  3109. * Find a mergeable slab cache
  3110. */
  3111. static int slab_unmergeable(struct kmem_cache *s)
  3112. {
  3113. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  3114. return 1;
  3115. if (s->ctor)
  3116. return 1;
  3117. /*
  3118. * We may have set a slab to be unmergeable during bootstrap.
  3119. */
  3120. if (s->refcount < 0)
  3121. return 1;
  3122. return 0;
  3123. }
  3124. static struct kmem_cache *find_mergeable(size_t size,
  3125. size_t align, unsigned long flags, const char *name,
  3126. void (*ctor)(void *))
  3127. {
  3128. struct kmem_cache *s;
  3129. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  3130. return NULL;
  3131. if (ctor)
  3132. return NULL;
  3133. size = ALIGN(size, sizeof(void *));
  3134. align = calculate_alignment(flags, align, size);
  3135. size = ALIGN(size, align);
  3136. flags = kmem_cache_flags(size, flags, name, NULL);
  3137. list_for_each_entry(s, &slab_caches, list) {
  3138. if (slab_unmergeable(s))
  3139. continue;
  3140. if (size > s->size)
  3141. continue;
  3142. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  3143. continue;
  3144. /*
  3145. * Check if alignment is compatible.
  3146. * Courtesy of Adrian Drzewiecki
  3147. */
  3148. if ((s->size & ~(align - 1)) != s->size)
  3149. continue;
  3150. if (s->size - size >= sizeof(void *))
  3151. continue;
  3152. return s;
  3153. }
  3154. return NULL;
  3155. }
  3156. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  3157. size_t align, unsigned long flags, void (*ctor)(void *))
  3158. {
  3159. struct kmem_cache *s;
  3160. char *n;
  3161. if (WARN_ON(!name))
  3162. return NULL;
  3163. down_write(&slub_lock);
  3164. s = find_mergeable(size, align, flags, name, ctor);
  3165. if (s) {
  3166. s->refcount++;
  3167. /*
  3168. * Adjust the object sizes so that we clear
  3169. * the complete object on kzalloc.
  3170. */
  3171. s->objsize = max(s->objsize, (int)size);
  3172. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  3173. if (sysfs_slab_alias(s, name)) {
  3174. s->refcount--;
  3175. goto err;
  3176. }
  3177. up_write(&slub_lock);
  3178. return s;
  3179. }
  3180. n = kstrdup(name, GFP_KERNEL);
  3181. if (!n)
  3182. goto err;
  3183. s = kmalloc(kmem_size, GFP_KERNEL);
  3184. if (s) {
  3185. if (kmem_cache_open(s, n,
  3186. size, align, flags, ctor)) {
  3187. list_add(&s->list, &slab_caches);
  3188. if (sysfs_slab_add(s)) {
  3189. list_del(&s->list);
  3190. kfree(n);
  3191. kfree(s);
  3192. goto err;
  3193. }
  3194. up_write(&slub_lock);
  3195. return s;
  3196. }
  3197. kfree(n);
  3198. kfree(s);
  3199. }
  3200. err:
  3201. up_write(&slub_lock);
  3202. if (flags & SLAB_PANIC)
  3203. panic("Cannot create slabcache %s\n", name);
  3204. else
  3205. s = NULL;
  3206. return s;
  3207. }
  3208. EXPORT_SYMBOL(kmem_cache_create);
  3209. #ifdef CONFIG_SMP
  3210. /*
  3211. * Use the cpu notifier to insure that the cpu slabs are flushed when
  3212. * necessary.
  3213. */
  3214. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  3215. unsigned long action, void *hcpu)
  3216. {
  3217. long cpu = (long)hcpu;
  3218. struct kmem_cache *s;
  3219. unsigned long flags;
  3220. switch (action) {
  3221. case CPU_UP_CANCELED:
  3222. case CPU_UP_CANCELED_FROZEN:
  3223. case CPU_DEAD:
  3224. case CPU_DEAD_FROZEN:
  3225. down_read(&slub_lock);
  3226. list_for_each_entry(s, &slab_caches, list) {
  3227. local_irq_save(flags);
  3228. __flush_cpu_slab(s, cpu);
  3229. local_irq_restore(flags);
  3230. }
  3231. up_read(&slub_lock);
  3232. break;
  3233. default:
  3234. break;
  3235. }
  3236. return NOTIFY_OK;
  3237. }
  3238. static struct notifier_block __cpuinitdata slab_notifier = {
  3239. .notifier_call = slab_cpuup_callback
  3240. };
  3241. #endif
  3242. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  3243. {
  3244. struct kmem_cache *s;
  3245. void *ret;
  3246. if (unlikely(size > SLUB_MAX_SIZE))
  3247. return kmalloc_large(size, gfpflags);
  3248. s = get_slab(size, gfpflags);
  3249. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3250. return s;
  3251. ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
  3252. /* Honor the call site pointer we received. */
  3253. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  3254. return ret;
  3255. }
  3256. #ifdef CONFIG_NUMA
  3257. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  3258. int node, unsigned long caller)
  3259. {
  3260. struct kmem_cache *s;
  3261. void *ret;
  3262. if (unlikely(size > SLUB_MAX_SIZE)) {
  3263. ret = kmalloc_large_node(size, gfpflags, node);
  3264. trace_kmalloc_node(caller, ret,
  3265. size, PAGE_SIZE << get_order(size),
  3266. gfpflags, node);
  3267. return ret;
  3268. }
  3269. s = get_slab(size, gfpflags);
  3270. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3271. return s;
  3272. ret = slab_alloc(s, gfpflags, node, caller);
  3273. /* Honor the call site pointer we received. */
  3274. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  3275. return ret;
  3276. }
  3277. #endif
  3278. #ifdef CONFIG_SYSFS
  3279. static int count_inuse(struct page *page)
  3280. {
  3281. return page->inuse;
  3282. }
  3283. static int count_total(struct page *page)
  3284. {
  3285. return page->objects;
  3286. }
  3287. #endif
  3288. #ifdef CONFIG_SLUB_DEBUG
  3289. static int validate_slab(struct kmem_cache *s, struct page *page,
  3290. unsigned long *map)
  3291. {
  3292. void *p;
  3293. void *addr = page_address(page);
  3294. if (!check_slab(s, page) ||
  3295. !on_freelist(s, page, NULL))
  3296. return 0;
  3297. /* Now we know that a valid freelist exists */
  3298. bitmap_zero(map, page->objects);
  3299. get_map(s, page, map);
  3300. for_each_object(p, s, addr, page->objects) {
  3301. if (test_bit(slab_index(p, s, addr), map))
  3302. if (!check_object(s, page, p, SLUB_RED_INACTIVE))
  3303. return 0;
  3304. }
  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_ACTIVE))
  3308. return 0;
  3309. return 1;
  3310. }
  3311. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  3312. unsigned long *map)
  3313. {
  3314. slab_lock(page);
  3315. validate_slab(s, page, map);
  3316. slab_unlock(page);
  3317. }
  3318. static int validate_slab_node(struct kmem_cache *s,
  3319. struct kmem_cache_node *n, unsigned long *map)
  3320. {
  3321. unsigned long count = 0;
  3322. struct page *page;
  3323. unsigned long flags;
  3324. spin_lock_irqsave(&n->list_lock, flags);
  3325. list_for_each_entry(page, &n->partial, lru) {
  3326. validate_slab_slab(s, page, map);
  3327. count++;
  3328. }
  3329. if (count != n->nr_partial)
  3330. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  3331. "counter=%ld\n", s->name, count, n->nr_partial);
  3332. if (!(s->flags & SLAB_STORE_USER))
  3333. goto out;
  3334. list_for_each_entry(page, &n->full, lru) {
  3335. validate_slab_slab(s, page, map);
  3336. count++;
  3337. }
  3338. if (count != atomic_long_read(&n->nr_slabs))
  3339. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  3340. "counter=%ld\n", s->name, count,
  3341. atomic_long_read(&n->nr_slabs));
  3342. out:
  3343. spin_unlock_irqrestore(&n->list_lock, flags);
  3344. return count;
  3345. }
  3346. static long validate_slab_cache(struct kmem_cache *s)
  3347. {
  3348. int node;
  3349. unsigned long count = 0;
  3350. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3351. sizeof(unsigned long), GFP_KERNEL);
  3352. if (!map)
  3353. return -ENOMEM;
  3354. flush_all(s);
  3355. for_each_node_state(node, N_NORMAL_MEMORY) {
  3356. struct kmem_cache_node *n = get_node(s, node);
  3357. count += validate_slab_node(s, n, map);
  3358. }
  3359. kfree(map);
  3360. return count;
  3361. }
  3362. /*
  3363. * Generate lists of code addresses where slabcache objects are allocated
  3364. * and freed.
  3365. */
  3366. struct location {
  3367. unsigned long count;
  3368. unsigned long addr;
  3369. long long sum_time;
  3370. long min_time;
  3371. long max_time;
  3372. long min_pid;
  3373. long max_pid;
  3374. DECLARE_BITMAP(cpus, NR_CPUS);
  3375. nodemask_t nodes;
  3376. };
  3377. struct loc_track {
  3378. unsigned long max;
  3379. unsigned long count;
  3380. struct location *loc;
  3381. };
  3382. static void free_loc_track(struct loc_track *t)
  3383. {
  3384. if (t->max)
  3385. free_pages((unsigned long)t->loc,
  3386. get_order(sizeof(struct location) * t->max));
  3387. }
  3388. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3389. {
  3390. struct location *l;
  3391. int order;
  3392. order = get_order(sizeof(struct location) * max);
  3393. l = (void *)__get_free_pages(flags, order);
  3394. if (!l)
  3395. return 0;
  3396. if (t->count) {
  3397. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3398. free_loc_track(t);
  3399. }
  3400. t->max = max;
  3401. t->loc = l;
  3402. return 1;
  3403. }
  3404. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3405. const struct track *track)
  3406. {
  3407. long start, end, pos;
  3408. struct location *l;
  3409. unsigned long caddr;
  3410. unsigned long age = jiffies - track->when;
  3411. start = -1;
  3412. end = t->count;
  3413. for ( ; ; ) {
  3414. pos = start + (end - start + 1) / 2;
  3415. /*
  3416. * There is nothing at "end". If we end up there
  3417. * we need to add something to before end.
  3418. */
  3419. if (pos == end)
  3420. break;
  3421. caddr = t->loc[pos].addr;
  3422. if (track->addr == caddr) {
  3423. l = &t->loc[pos];
  3424. l->count++;
  3425. if (track->when) {
  3426. l->sum_time += age;
  3427. if (age < l->min_time)
  3428. l->min_time = age;
  3429. if (age > l->max_time)
  3430. l->max_time = age;
  3431. if (track->pid < l->min_pid)
  3432. l->min_pid = track->pid;
  3433. if (track->pid > l->max_pid)
  3434. l->max_pid = track->pid;
  3435. cpumask_set_cpu(track->cpu,
  3436. to_cpumask(l->cpus));
  3437. }
  3438. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3439. return 1;
  3440. }
  3441. if (track->addr < caddr)
  3442. end = pos;
  3443. else
  3444. start = pos;
  3445. }
  3446. /*
  3447. * Not found. Insert new tracking element.
  3448. */
  3449. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3450. return 0;
  3451. l = t->loc + pos;
  3452. if (pos < t->count)
  3453. memmove(l + 1, l,
  3454. (t->count - pos) * sizeof(struct location));
  3455. t->count++;
  3456. l->count = 1;
  3457. l->addr = track->addr;
  3458. l->sum_time = age;
  3459. l->min_time = age;
  3460. l->max_time = age;
  3461. l->min_pid = track->pid;
  3462. l->max_pid = track->pid;
  3463. cpumask_clear(to_cpumask(l->cpus));
  3464. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3465. nodes_clear(l->nodes);
  3466. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3467. return 1;
  3468. }
  3469. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3470. struct page *page, enum track_item alloc,
  3471. unsigned long *map)
  3472. {
  3473. void *addr = page_address(page);
  3474. void *p;
  3475. bitmap_zero(map, page->objects);
  3476. get_map(s, page, map);
  3477. for_each_object(p, s, addr, page->objects)
  3478. if (!test_bit(slab_index(p, s, addr), map))
  3479. add_location(t, s, get_track(s, p, alloc));
  3480. }
  3481. static int list_locations(struct kmem_cache *s, char *buf,
  3482. enum track_item alloc)
  3483. {
  3484. int len = 0;
  3485. unsigned long i;
  3486. struct loc_track t = { 0, 0, NULL };
  3487. int node;
  3488. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3489. sizeof(unsigned long), GFP_KERNEL);
  3490. if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3491. GFP_TEMPORARY)) {
  3492. kfree(map);
  3493. return sprintf(buf, "Out of memory\n");
  3494. }
  3495. /* Push back cpu slabs */
  3496. flush_all(s);
  3497. for_each_node_state(node, N_NORMAL_MEMORY) {
  3498. struct kmem_cache_node *n = get_node(s, node);
  3499. unsigned long flags;
  3500. struct page *page;
  3501. if (!atomic_long_read(&n->nr_slabs))
  3502. continue;
  3503. spin_lock_irqsave(&n->list_lock, flags);
  3504. list_for_each_entry(page, &n->partial, lru)
  3505. process_slab(&t, s, page, alloc, map);
  3506. list_for_each_entry(page, &n->full, lru)
  3507. process_slab(&t, s, page, alloc, map);
  3508. spin_unlock_irqrestore(&n->list_lock, flags);
  3509. }
  3510. for (i = 0; i < t.count; i++) {
  3511. struct location *l = &t.loc[i];
  3512. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3513. break;
  3514. len += sprintf(buf + len, "%7ld ", l->count);
  3515. if (l->addr)
  3516. len += sprintf(buf + len, "%pS", (void *)l->addr);
  3517. else
  3518. len += sprintf(buf + len, "<not-available>");
  3519. if (l->sum_time != l->min_time) {
  3520. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3521. l->min_time,
  3522. (long)div_u64(l->sum_time, l->count),
  3523. l->max_time);
  3524. } else
  3525. len += sprintf(buf + len, " age=%ld",
  3526. l->min_time);
  3527. if (l->min_pid != l->max_pid)
  3528. len += sprintf(buf + len, " pid=%ld-%ld",
  3529. l->min_pid, l->max_pid);
  3530. else
  3531. len += sprintf(buf + len, " pid=%ld",
  3532. l->min_pid);
  3533. if (num_online_cpus() > 1 &&
  3534. !cpumask_empty(to_cpumask(l->cpus)) &&
  3535. len < PAGE_SIZE - 60) {
  3536. len += sprintf(buf + len, " cpus=");
  3537. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3538. to_cpumask(l->cpus));
  3539. }
  3540. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3541. len < PAGE_SIZE - 60) {
  3542. len += sprintf(buf + len, " nodes=");
  3543. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3544. l->nodes);
  3545. }
  3546. len += sprintf(buf + len, "\n");
  3547. }
  3548. free_loc_track(&t);
  3549. kfree(map);
  3550. if (!t.count)
  3551. len += sprintf(buf, "No data\n");
  3552. return len;
  3553. }
  3554. #endif
  3555. #ifdef SLUB_RESILIENCY_TEST
  3556. static void resiliency_test(void)
  3557. {
  3558. u8 *p;
  3559. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
  3560. printk(KERN_ERR "SLUB resiliency testing\n");
  3561. printk(KERN_ERR "-----------------------\n");
  3562. printk(KERN_ERR "A. Corruption after allocation\n");
  3563. p = kzalloc(16, GFP_KERNEL);
  3564. p[16] = 0x12;
  3565. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  3566. " 0x12->0x%p\n\n", p + 16);
  3567. validate_slab_cache(kmalloc_caches[4]);
  3568. /* Hmmm... The next two are dangerous */
  3569. p = kzalloc(32, GFP_KERNEL);
  3570. p[32 + sizeof(void *)] = 0x34;
  3571. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  3572. " 0x34 -> -0x%p\n", p);
  3573. printk(KERN_ERR
  3574. "If allocated object is overwritten then not detectable\n\n");
  3575. validate_slab_cache(kmalloc_caches[5]);
  3576. p = kzalloc(64, GFP_KERNEL);
  3577. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3578. *p = 0x56;
  3579. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3580. p);
  3581. printk(KERN_ERR
  3582. "If allocated object is overwritten then not detectable\n\n");
  3583. validate_slab_cache(kmalloc_caches[6]);
  3584. printk(KERN_ERR "\nB. Corruption after free\n");
  3585. p = kzalloc(128, GFP_KERNEL);
  3586. kfree(p);
  3587. *p = 0x78;
  3588. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3589. validate_slab_cache(kmalloc_caches[7]);
  3590. p = kzalloc(256, GFP_KERNEL);
  3591. kfree(p);
  3592. p[50] = 0x9a;
  3593. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  3594. p);
  3595. validate_slab_cache(kmalloc_caches[8]);
  3596. p = kzalloc(512, GFP_KERNEL);
  3597. kfree(p);
  3598. p[512] = 0xab;
  3599. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3600. validate_slab_cache(kmalloc_caches[9]);
  3601. }
  3602. #else
  3603. #ifdef CONFIG_SYSFS
  3604. static void resiliency_test(void) {};
  3605. #endif
  3606. #endif
  3607. #ifdef CONFIG_SYSFS
  3608. enum slab_stat_type {
  3609. SL_ALL, /* All slabs */
  3610. SL_PARTIAL, /* Only partially allocated slabs */
  3611. SL_CPU, /* Only slabs used for cpu caches */
  3612. SL_OBJECTS, /* Determine allocated objects not slabs */
  3613. SL_TOTAL /* Determine object capacity not slabs */
  3614. };
  3615. #define SO_ALL (1 << SL_ALL)
  3616. #define SO_PARTIAL (1 << SL_PARTIAL)
  3617. #define SO_CPU (1 << SL_CPU)
  3618. #define SO_OBJECTS (1 << SL_OBJECTS)
  3619. #define SO_TOTAL (1 << SL_TOTAL)
  3620. static ssize_t show_slab_objects(struct kmem_cache *s,
  3621. char *buf, unsigned long flags)
  3622. {
  3623. unsigned long total = 0;
  3624. int node;
  3625. int x;
  3626. unsigned long *nodes;
  3627. unsigned long *per_cpu;
  3628. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3629. if (!nodes)
  3630. return -ENOMEM;
  3631. per_cpu = nodes + nr_node_ids;
  3632. if (flags & SO_CPU) {
  3633. int cpu;
  3634. for_each_possible_cpu(cpu) {
  3635. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  3636. if (!c || c->node < 0)
  3637. continue;
  3638. if (c->page) {
  3639. if (flags & SO_TOTAL)
  3640. x = c->page->objects;
  3641. else if (flags & SO_OBJECTS)
  3642. x = c->page->inuse;
  3643. else
  3644. x = 1;
  3645. total += x;
  3646. nodes[c->node] += x;
  3647. }
  3648. per_cpu[c->node]++;
  3649. }
  3650. }
  3651. lock_memory_hotplug();
  3652. #ifdef CONFIG_SLUB_DEBUG
  3653. if (flags & SO_ALL) {
  3654. for_each_node_state(node, N_NORMAL_MEMORY) {
  3655. struct kmem_cache_node *n = get_node(s, node);
  3656. if (flags & SO_TOTAL)
  3657. x = atomic_long_read(&n->total_objects);
  3658. else if (flags & SO_OBJECTS)
  3659. x = atomic_long_read(&n->total_objects) -
  3660. count_partial(n, count_free);
  3661. else
  3662. x = atomic_long_read(&n->nr_slabs);
  3663. total += x;
  3664. nodes[node] += x;
  3665. }
  3666. } else
  3667. #endif
  3668. if (flags & SO_PARTIAL) {
  3669. for_each_node_state(node, N_NORMAL_MEMORY) {
  3670. struct kmem_cache_node *n = get_node(s, node);
  3671. if (flags & SO_TOTAL)
  3672. x = count_partial(n, count_total);
  3673. else if (flags & SO_OBJECTS)
  3674. x = count_partial(n, count_inuse);
  3675. else
  3676. x = n->nr_partial;
  3677. total += x;
  3678. nodes[node] += x;
  3679. }
  3680. }
  3681. x = sprintf(buf, "%lu", total);
  3682. #ifdef CONFIG_NUMA
  3683. for_each_node_state(node, N_NORMAL_MEMORY)
  3684. if (nodes[node])
  3685. x += sprintf(buf + x, " N%d=%lu",
  3686. node, nodes[node]);
  3687. #endif
  3688. unlock_memory_hotplug();
  3689. kfree(nodes);
  3690. return x + sprintf(buf + x, "\n");
  3691. }
  3692. #ifdef CONFIG_SLUB_DEBUG
  3693. static int any_slab_objects(struct kmem_cache *s)
  3694. {
  3695. int node;
  3696. for_each_online_node(node) {
  3697. struct kmem_cache_node *n = get_node(s, node);
  3698. if (!n)
  3699. continue;
  3700. if (atomic_long_read(&n->total_objects))
  3701. return 1;
  3702. }
  3703. return 0;
  3704. }
  3705. #endif
  3706. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3707. #define to_slab(n) container_of(n, struct kmem_cache, kobj)
  3708. struct slab_attribute {
  3709. struct attribute attr;
  3710. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3711. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3712. };
  3713. #define SLAB_ATTR_RO(_name) \
  3714. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  3715. #define SLAB_ATTR(_name) \
  3716. static struct slab_attribute _name##_attr = \
  3717. __ATTR(_name, 0644, _name##_show, _name##_store)
  3718. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3719. {
  3720. return sprintf(buf, "%d\n", s->size);
  3721. }
  3722. SLAB_ATTR_RO(slab_size);
  3723. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3724. {
  3725. return sprintf(buf, "%d\n", s->align);
  3726. }
  3727. SLAB_ATTR_RO(align);
  3728. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3729. {
  3730. return sprintf(buf, "%d\n", s->objsize);
  3731. }
  3732. SLAB_ATTR_RO(object_size);
  3733. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3734. {
  3735. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3736. }
  3737. SLAB_ATTR_RO(objs_per_slab);
  3738. static ssize_t order_store(struct kmem_cache *s,
  3739. const char *buf, size_t length)
  3740. {
  3741. unsigned long order;
  3742. int err;
  3743. err = strict_strtoul(buf, 10, &order);
  3744. if (err)
  3745. return err;
  3746. if (order > slub_max_order || order < slub_min_order)
  3747. return -EINVAL;
  3748. calculate_sizes(s, order);
  3749. return length;
  3750. }
  3751. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3752. {
  3753. return sprintf(buf, "%d\n", oo_order(s->oo));
  3754. }
  3755. SLAB_ATTR(order);
  3756. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  3757. {
  3758. return sprintf(buf, "%lu\n", s->min_partial);
  3759. }
  3760. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  3761. size_t length)
  3762. {
  3763. unsigned long min;
  3764. int err;
  3765. err = strict_strtoul(buf, 10, &min);
  3766. if (err)
  3767. return err;
  3768. set_min_partial(s, min);
  3769. return length;
  3770. }
  3771. SLAB_ATTR(min_partial);
  3772. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3773. {
  3774. if (!s->ctor)
  3775. return 0;
  3776. return sprintf(buf, "%pS\n", s->ctor);
  3777. }
  3778. SLAB_ATTR_RO(ctor);
  3779. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3780. {
  3781. return sprintf(buf, "%d\n", s->refcount - 1);
  3782. }
  3783. SLAB_ATTR_RO(aliases);
  3784. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3785. {
  3786. return show_slab_objects(s, buf, SO_PARTIAL);
  3787. }
  3788. SLAB_ATTR_RO(partial);
  3789. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3790. {
  3791. return show_slab_objects(s, buf, SO_CPU);
  3792. }
  3793. SLAB_ATTR_RO(cpu_slabs);
  3794. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3795. {
  3796. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3797. }
  3798. SLAB_ATTR_RO(objects);
  3799. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3800. {
  3801. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3802. }
  3803. SLAB_ATTR_RO(objects_partial);
  3804. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3805. {
  3806. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3807. }
  3808. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3809. const char *buf, size_t length)
  3810. {
  3811. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3812. if (buf[0] == '1')
  3813. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3814. return length;
  3815. }
  3816. SLAB_ATTR(reclaim_account);
  3817. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3818. {
  3819. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3820. }
  3821. SLAB_ATTR_RO(hwcache_align);
  3822. #ifdef CONFIG_ZONE_DMA
  3823. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3824. {
  3825. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3826. }
  3827. SLAB_ATTR_RO(cache_dma);
  3828. #endif
  3829. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3830. {
  3831. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3832. }
  3833. SLAB_ATTR_RO(destroy_by_rcu);
  3834. static ssize_t reserved_show(struct kmem_cache *s, char *buf)
  3835. {
  3836. return sprintf(buf, "%d\n", s->reserved);
  3837. }
  3838. SLAB_ATTR_RO(reserved);
  3839. #ifdef CONFIG_SLUB_DEBUG
  3840. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  3841. {
  3842. return show_slab_objects(s, buf, SO_ALL);
  3843. }
  3844. SLAB_ATTR_RO(slabs);
  3845. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  3846. {
  3847. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  3848. }
  3849. SLAB_ATTR_RO(total_objects);
  3850. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3851. {
  3852. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3853. }
  3854. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3855. const char *buf, size_t length)
  3856. {
  3857. s->flags &= ~SLAB_DEBUG_FREE;
  3858. if (buf[0] == '1') {
  3859. s->flags &= ~__CMPXCHG_DOUBLE;
  3860. s->flags |= SLAB_DEBUG_FREE;
  3861. }
  3862. return length;
  3863. }
  3864. SLAB_ATTR(sanity_checks);
  3865. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3866. {
  3867. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3868. }
  3869. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3870. size_t length)
  3871. {
  3872. s->flags &= ~SLAB_TRACE;
  3873. if (buf[0] == '1') {
  3874. s->flags &= ~__CMPXCHG_DOUBLE;
  3875. s->flags |= SLAB_TRACE;
  3876. }
  3877. return length;
  3878. }
  3879. SLAB_ATTR(trace);
  3880. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3881. {
  3882. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3883. }
  3884. static ssize_t red_zone_store(struct kmem_cache *s,
  3885. const char *buf, size_t length)
  3886. {
  3887. if (any_slab_objects(s))
  3888. return -EBUSY;
  3889. s->flags &= ~SLAB_RED_ZONE;
  3890. if (buf[0] == '1') {
  3891. s->flags &= ~__CMPXCHG_DOUBLE;
  3892. s->flags |= SLAB_RED_ZONE;
  3893. }
  3894. calculate_sizes(s, -1);
  3895. return length;
  3896. }
  3897. SLAB_ATTR(red_zone);
  3898. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3899. {
  3900. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3901. }
  3902. static ssize_t poison_store(struct kmem_cache *s,
  3903. const char *buf, size_t length)
  3904. {
  3905. if (any_slab_objects(s))
  3906. return -EBUSY;
  3907. s->flags &= ~SLAB_POISON;
  3908. if (buf[0] == '1') {
  3909. s->flags &= ~__CMPXCHG_DOUBLE;
  3910. s->flags |= SLAB_POISON;
  3911. }
  3912. calculate_sizes(s, -1);
  3913. return length;
  3914. }
  3915. SLAB_ATTR(poison);
  3916. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3917. {
  3918. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3919. }
  3920. static ssize_t store_user_store(struct kmem_cache *s,
  3921. const char *buf, size_t length)
  3922. {
  3923. if (any_slab_objects(s))
  3924. return -EBUSY;
  3925. s->flags &= ~SLAB_STORE_USER;
  3926. if (buf[0] == '1') {
  3927. s->flags &= ~__CMPXCHG_DOUBLE;
  3928. s->flags |= SLAB_STORE_USER;
  3929. }
  3930. calculate_sizes(s, -1);
  3931. return length;
  3932. }
  3933. SLAB_ATTR(store_user);
  3934. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3935. {
  3936. return 0;
  3937. }
  3938. static ssize_t validate_store(struct kmem_cache *s,
  3939. const char *buf, size_t length)
  3940. {
  3941. int ret = -EINVAL;
  3942. if (buf[0] == '1') {
  3943. ret = validate_slab_cache(s);
  3944. if (ret >= 0)
  3945. ret = length;
  3946. }
  3947. return ret;
  3948. }
  3949. SLAB_ATTR(validate);
  3950. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3951. {
  3952. if (!(s->flags & SLAB_STORE_USER))
  3953. return -ENOSYS;
  3954. return list_locations(s, buf, TRACK_ALLOC);
  3955. }
  3956. SLAB_ATTR_RO(alloc_calls);
  3957. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3958. {
  3959. if (!(s->flags & SLAB_STORE_USER))
  3960. return -ENOSYS;
  3961. return list_locations(s, buf, TRACK_FREE);
  3962. }
  3963. SLAB_ATTR_RO(free_calls);
  3964. #endif /* CONFIG_SLUB_DEBUG */
  3965. #ifdef CONFIG_FAILSLAB
  3966. static ssize_t failslab_show(struct kmem_cache *s, char *buf)
  3967. {
  3968. return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
  3969. }
  3970. static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
  3971. size_t length)
  3972. {
  3973. s->flags &= ~SLAB_FAILSLAB;
  3974. if (buf[0] == '1')
  3975. s->flags |= SLAB_FAILSLAB;
  3976. return length;
  3977. }
  3978. SLAB_ATTR(failslab);
  3979. #endif
  3980. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3981. {
  3982. return 0;
  3983. }
  3984. static ssize_t shrink_store(struct kmem_cache *s,
  3985. const char *buf, size_t length)
  3986. {
  3987. if (buf[0] == '1') {
  3988. int rc = kmem_cache_shrink(s);
  3989. if (rc)
  3990. return rc;
  3991. } else
  3992. return -EINVAL;
  3993. return length;
  3994. }
  3995. SLAB_ATTR(shrink);
  3996. #ifdef CONFIG_NUMA
  3997. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  3998. {
  3999. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  4000. }
  4001. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  4002. const char *buf, size_t length)
  4003. {
  4004. unsigned long ratio;
  4005. int err;
  4006. err = strict_strtoul(buf, 10, &ratio);
  4007. if (err)
  4008. return err;
  4009. if (ratio <= 100)
  4010. s->remote_node_defrag_ratio = ratio * 10;
  4011. return length;
  4012. }
  4013. SLAB_ATTR(remote_node_defrag_ratio);
  4014. #endif
  4015. #ifdef CONFIG_SLUB_STATS
  4016. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  4017. {
  4018. unsigned long sum = 0;
  4019. int cpu;
  4020. int len;
  4021. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  4022. if (!data)
  4023. return -ENOMEM;
  4024. for_each_online_cpu(cpu) {
  4025. unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
  4026. data[cpu] = x;
  4027. sum += x;
  4028. }
  4029. len = sprintf(buf, "%lu", sum);
  4030. #ifdef CONFIG_SMP
  4031. for_each_online_cpu(cpu) {
  4032. if (data[cpu] && len < PAGE_SIZE - 20)
  4033. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  4034. }
  4035. #endif
  4036. kfree(data);
  4037. return len + sprintf(buf + len, "\n");
  4038. }
  4039. static void clear_stat(struct kmem_cache *s, enum stat_item si)
  4040. {
  4041. int cpu;
  4042. for_each_online_cpu(cpu)
  4043. per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
  4044. }
  4045. #define STAT_ATTR(si, text) \
  4046. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  4047. { \
  4048. return show_stat(s, buf, si); \
  4049. } \
  4050. static ssize_t text##_store(struct kmem_cache *s, \
  4051. const char *buf, size_t length) \
  4052. { \
  4053. if (buf[0] != '0') \
  4054. return -EINVAL; \
  4055. clear_stat(s, si); \
  4056. return length; \
  4057. } \
  4058. SLAB_ATTR(text); \
  4059. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  4060. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  4061. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  4062. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  4063. STAT_ATTR(FREE_FROZEN, free_frozen);
  4064. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  4065. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  4066. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  4067. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  4068. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  4069. STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
  4070. STAT_ATTR(FREE_SLAB, free_slab);
  4071. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  4072. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  4073. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  4074. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  4075. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  4076. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  4077. STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
  4078. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  4079. STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
  4080. STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
  4081. #endif
  4082. static struct attribute *slab_attrs[] = {
  4083. &slab_size_attr.attr,
  4084. &object_size_attr.attr,
  4085. &objs_per_slab_attr.attr,
  4086. &order_attr.attr,
  4087. &min_partial_attr.attr,
  4088. &objects_attr.attr,
  4089. &objects_partial_attr.attr,
  4090. &partial_attr.attr,
  4091. &cpu_slabs_attr.attr,
  4092. &ctor_attr.attr,
  4093. &aliases_attr.attr,
  4094. &align_attr.attr,
  4095. &hwcache_align_attr.attr,
  4096. &reclaim_account_attr.attr,
  4097. &destroy_by_rcu_attr.attr,
  4098. &shrink_attr.attr,
  4099. &reserved_attr.attr,
  4100. #ifdef CONFIG_SLUB_DEBUG
  4101. &total_objects_attr.attr,
  4102. &slabs_attr.attr,
  4103. &sanity_checks_attr.attr,
  4104. &trace_attr.attr,
  4105. &red_zone_attr.attr,
  4106. &poison_attr.attr,
  4107. &store_user_attr.attr,
  4108. &validate_attr.attr,
  4109. &alloc_calls_attr.attr,
  4110. &free_calls_attr.attr,
  4111. #endif
  4112. #ifdef CONFIG_ZONE_DMA
  4113. &cache_dma_attr.attr,
  4114. #endif
  4115. #ifdef CONFIG_NUMA
  4116. &remote_node_defrag_ratio_attr.attr,
  4117. #endif
  4118. #ifdef CONFIG_SLUB_STATS
  4119. &alloc_fastpath_attr.attr,
  4120. &alloc_slowpath_attr.attr,
  4121. &free_fastpath_attr.attr,
  4122. &free_slowpath_attr.attr,
  4123. &free_frozen_attr.attr,
  4124. &free_add_partial_attr.attr,
  4125. &free_remove_partial_attr.attr,
  4126. &alloc_from_partial_attr.attr,
  4127. &alloc_slab_attr.attr,
  4128. &alloc_refill_attr.attr,
  4129. &alloc_node_mismatch_attr.attr,
  4130. &free_slab_attr.attr,
  4131. &cpuslab_flush_attr.attr,
  4132. &deactivate_full_attr.attr,
  4133. &deactivate_empty_attr.attr,
  4134. &deactivate_to_head_attr.attr,
  4135. &deactivate_to_tail_attr.attr,
  4136. &deactivate_remote_frees_attr.attr,
  4137. &deactivate_bypass_attr.attr,
  4138. &order_fallback_attr.attr,
  4139. &cmpxchg_double_fail_attr.attr,
  4140. &cmpxchg_double_cpu_fail_attr.attr,
  4141. #endif
  4142. #ifdef CONFIG_FAILSLAB
  4143. &failslab_attr.attr,
  4144. #endif
  4145. NULL
  4146. };
  4147. static struct attribute_group slab_attr_group = {
  4148. .attrs = slab_attrs,
  4149. };
  4150. static ssize_t slab_attr_show(struct kobject *kobj,
  4151. struct attribute *attr,
  4152. char *buf)
  4153. {
  4154. struct slab_attribute *attribute;
  4155. struct kmem_cache *s;
  4156. int err;
  4157. attribute = to_slab_attr(attr);
  4158. s = to_slab(kobj);
  4159. if (!attribute->show)
  4160. return -EIO;
  4161. err = attribute->show(s, buf);
  4162. return err;
  4163. }
  4164. static ssize_t slab_attr_store(struct kobject *kobj,
  4165. struct attribute *attr,
  4166. const char *buf, size_t len)
  4167. {
  4168. struct slab_attribute *attribute;
  4169. struct kmem_cache *s;
  4170. int err;
  4171. attribute = to_slab_attr(attr);
  4172. s = to_slab(kobj);
  4173. if (!attribute->store)
  4174. return -EIO;
  4175. err = attribute->store(s, buf, len);
  4176. return err;
  4177. }
  4178. static void kmem_cache_release(struct kobject *kobj)
  4179. {
  4180. struct kmem_cache *s = to_slab(kobj);
  4181. kfree(s->name);
  4182. kfree(s);
  4183. }
  4184. static const struct sysfs_ops slab_sysfs_ops = {
  4185. .show = slab_attr_show,
  4186. .store = slab_attr_store,
  4187. };
  4188. static struct kobj_type slab_ktype = {
  4189. .sysfs_ops = &slab_sysfs_ops,
  4190. .release = kmem_cache_release
  4191. };
  4192. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  4193. {
  4194. struct kobj_type *ktype = get_ktype(kobj);
  4195. if (ktype == &slab_ktype)
  4196. return 1;
  4197. return 0;
  4198. }
  4199. static const struct kset_uevent_ops slab_uevent_ops = {
  4200. .filter = uevent_filter,
  4201. };
  4202. static struct kset *slab_kset;
  4203. #define ID_STR_LENGTH 64
  4204. /* Create a unique string id for a slab cache:
  4205. *
  4206. * Format :[flags-]size
  4207. */
  4208. static char *create_unique_id(struct kmem_cache *s)
  4209. {
  4210. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  4211. char *p = name;
  4212. BUG_ON(!name);
  4213. *p++ = ':';
  4214. /*
  4215. * First flags affecting slabcache operations. We will only
  4216. * get here for aliasable slabs so we do not need to support
  4217. * too many flags. The flags here must cover all flags that
  4218. * are matched during merging to guarantee that the id is
  4219. * unique.
  4220. */
  4221. if (s->flags & SLAB_CACHE_DMA)
  4222. *p++ = 'd';
  4223. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  4224. *p++ = 'a';
  4225. if (s->flags & SLAB_DEBUG_FREE)
  4226. *p++ = 'F';
  4227. if (!(s->flags & SLAB_NOTRACK))
  4228. *p++ = 't';
  4229. if (p != name + 1)
  4230. *p++ = '-';
  4231. p += sprintf(p, "%07d", s->size);
  4232. BUG_ON(p > name + ID_STR_LENGTH - 1);
  4233. return name;
  4234. }
  4235. static int sysfs_slab_add(struct kmem_cache *s)
  4236. {
  4237. int err;
  4238. const char *name;
  4239. int unmergeable;
  4240. if (slab_state < SYSFS)
  4241. /* Defer until later */
  4242. return 0;
  4243. unmergeable = slab_unmergeable(s);
  4244. if (unmergeable) {
  4245. /*
  4246. * Slabcache can never be merged so we can use the name proper.
  4247. * This is typically the case for debug situations. In that
  4248. * case we can catch duplicate names easily.
  4249. */
  4250. sysfs_remove_link(&slab_kset->kobj, s->name);
  4251. name = s->name;
  4252. } else {
  4253. /*
  4254. * Create a unique name for the slab as a target
  4255. * for the symlinks.
  4256. */
  4257. name = create_unique_id(s);
  4258. }
  4259. s->kobj.kset = slab_kset;
  4260. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  4261. if (err) {
  4262. kobject_put(&s->kobj);
  4263. return err;
  4264. }
  4265. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  4266. if (err) {
  4267. kobject_del(&s->kobj);
  4268. kobject_put(&s->kobj);
  4269. return err;
  4270. }
  4271. kobject_uevent(&s->kobj, KOBJ_ADD);
  4272. if (!unmergeable) {
  4273. /* Setup first alias */
  4274. sysfs_slab_alias(s, s->name);
  4275. kfree(name);
  4276. }
  4277. return 0;
  4278. }
  4279. static void sysfs_slab_remove(struct kmem_cache *s)
  4280. {
  4281. if (slab_state < SYSFS)
  4282. /*
  4283. * Sysfs has not been setup yet so no need to remove the
  4284. * cache from sysfs.
  4285. */
  4286. return;
  4287. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  4288. kobject_del(&s->kobj);
  4289. kobject_put(&s->kobj);
  4290. }
  4291. /*
  4292. * Need to buffer aliases during bootup until sysfs becomes
  4293. * available lest we lose that information.
  4294. */
  4295. struct saved_alias {
  4296. struct kmem_cache *s;
  4297. const char *name;
  4298. struct saved_alias *next;
  4299. };
  4300. static struct saved_alias *alias_list;
  4301. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  4302. {
  4303. struct saved_alias *al;
  4304. if (slab_state == SYSFS) {
  4305. /*
  4306. * If we have a leftover link then remove it.
  4307. */
  4308. sysfs_remove_link(&slab_kset->kobj, name);
  4309. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  4310. }
  4311. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  4312. if (!al)
  4313. return -ENOMEM;
  4314. al->s = s;
  4315. al->name = name;
  4316. al->next = alias_list;
  4317. alias_list = al;
  4318. return 0;
  4319. }
  4320. static int __init slab_sysfs_init(void)
  4321. {
  4322. struct kmem_cache *s;
  4323. int err;
  4324. down_write(&slub_lock);
  4325. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  4326. if (!slab_kset) {
  4327. up_write(&slub_lock);
  4328. printk(KERN_ERR "Cannot register slab subsystem.\n");
  4329. return -ENOSYS;
  4330. }
  4331. slab_state = SYSFS;
  4332. list_for_each_entry(s, &slab_caches, list) {
  4333. err = sysfs_slab_add(s);
  4334. if (err)
  4335. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  4336. " to sysfs\n", s->name);
  4337. }
  4338. while (alias_list) {
  4339. struct saved_alias *al = alias_list;
  4340. alias_list = alias_list->next;
  4341. err = sysfs_slab_alias(al->s, al->name);
  4342. if (err)
  4343. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  4344. " %s to sysfs\n", s->name);
  4345. kfree(al);
  4346. }
  4347. up_write(&slub_lock);
  4348. resiliency_test();
  4349. return 0;
  4350. }
  4351. __initcall(slab_sysfs_init);
  4352. #endif /* CONFIG_SYSFS */
  4353. /*
  4354. * The /proc/slabinfo ABI
  4355. */
  4356. #ifdef CONFIG_SLABINFO
  4357. static void print_slabinfo_header(struct seq_file *m)
  4358. {
  4359. seq_puts(m, "slabinfo - version: 2.1\n");
  4360. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  4361. "<objperslab> <pagesperslab>");
  4362. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  4363. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  4364. seq_putc(m, '\n');
  4365. }
  4366. static void *s_start(struct seq_file *m, loff_t *pos)
  4367. {
  4368. loff_t n = *pos;
  4369. down_read(&slub_lock);
  4370. if (!n)
  4371. print_slabinfo_header(m);
  4372. return seq_list_start(&slab_caches, *pos);
  4373. }
  4374. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  4375. {
  4376. return seq_list_next(p, &slab_caches, pos);
  4377. }
  4378. static void s_stop(struct seq_file *m, void *p)
  4379. {
  4380. up_read(&slub_lock);
  4381. }
  4382. static int s_show(struct seq_file *m, void *p)
  4383. {
  4384. unsigned long nr_partials = 0;
  4385. unsigned long nr_slabs = 0;
  4386. unsigned long nr_inuse = 0;
  4387. unsigned long nr_objs = 0;
  4388. unsigned long nr_free = 0;
  4389. struct kmem_cache *s;
  4390. int node;
  4391. s = list_entry(p, struct kmem_cache, list);
  4392. for_each_online_node(node) {
  4393. struct kmem_cache_node *n = get_node(s, node);
  4394. if (!n)
  4395. continue;
  4396. nr_partials += n->nr_partial;
  4397. nr_slabs += atomic_long_read(&n->nr_slabs);
  4398. nr_objs += atomic_long_read(&n->total_objects);
  4399. nr_free += count_partial(n, count_free);
  4400. }
  4401. nr_inuse = nr_objs - nr_free;
  4402. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  4403. nr_objs, s->size, oo_objects(s->oo),
  4404. (1 << oo_order(s->oo)));
  4405. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  4406. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  4407. 0UL);
  4408. seq_putc(m, '\n');
  4409. return 0;
  4410. }
  4411. static const struct seq_operations slabinfo_op = {
  4412. .start = s_start,
  4413. .next = s_next,
  4414. .stop = s_stop,
  4415. .show = s_show,
  4416. };
  4417. static int slabinfo_open(struct inode *inode, struct file *file)
  4418. {
  4419. return seq_open(file, &slabinfo_op);
  4420. }
  4421. static const struct file_operations proc_slabinfo_operations = {
  4422. .open = slabinfo_open,
  4423. .read = seq_read,
  4424. .llseek = seq_lseek,
  4425. .release = seq_release,
  4426. };
  4427. static int __init slab_proc_init(void)
  4428. {
  4429. proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
  4430. return 0;
  4431. }
  4432. module_init(slab_proc_init);
  4433. #endif /* CONFIG_SLABINFO */