slub.c 127 KB

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