slub.c 141 KB

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