slub.c 142 KB

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