slub.c 128 KB

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