slub.c 143 KB

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