slub.c 129 KB

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