slub.c 123 KB

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