slub.c 126 KB

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