slub.c 126 KB

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