slub.c 139 KB

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