slub.c 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526
  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. * Remove slab from the partial list, freeze it and
  1267. * return the pointer to the freelist.
  1268. *
  1269. * Returns a list of objects or NULL if it fails.
  1270. *
  1271. * Must hold list_lock since we modify the partial list.
  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. freelist = page->freelist;
  1286. counters = page->counters;
  1287. new.counters = counters;
  1288. if (mode)
  1289. new.inuse = page->objects;
  1290. VM_BUG_ON(new.frozen);
  1291. new.frozen = 1;
  1292. if (!__cmpxchg_double_slab(s, page,
  1293. freelist, counters,
  1294. NULL, new.counters,
  1295. "acquire_slab"))
  1296. return NULL;
  1297. remove_partial(n, page);
  1298. WARN_ON(!freelist);
  1299. return freelist;
  1300. }
  1301. static int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain);
  1302. /*
  1303. * Try to allocate a partial slab from a specific node.
  1304. */
  1305. static void *get_partial_node(struct kmem_cache *s,
  1306. struct kmem_cache_node *n, struct kmem_cache_cpu *c)
  1307. {
  1308. struct page *page, *page2;
  1309. void *object = NULL;
  1310. /*
  1311. * Racy check. If we mistakenly see no partial slabs then we
  1312. * just allocate an empty slab. If we mistakenly try to get a
  1313. * partial slab and there is none available then get_partials()
  1314. * will return NULL.
  1315. */
  1316. if (!n || !n->nr_partial)
  1317. return NULL;
  1318. spin_lock(&n->list_lock);
  1319. list_for_each_entry_safe(page, page2, &n->partial, lru) {
  1320. void *t = acquire_slab(s, n, page, object == NULL);
  1321. int available;
  1322. if (!t)
  1323. break;
  1324. if (!object) {
  1325. c->page = page;
  1326. c->node = page_to_nid(page);
  1327. stat(s, ALLOC_FROM_PARTIAL);
  1328. object = t;
  1329. available = page->objects - page->inuse;
  1330. } else {
  1331. page->freelist = t;
  1332. available = put_cpu_partial(s, page, 0);
  1333. stat(s, CPU_PARTIAL_NODE);
  1334. }
  1335. if (kmem_cache_debug(s) || available > s->cpu_partial / 2)
  1336. break;
  1337. }
  1338. spin_unlock(&n->list_lock);
  1339. return object;
  1340. }
  1341. /*
  1342. * Get a page from somewhere. Search in increasing NUMA distances.
  1343. */
  1344. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags,
  1345. struct kmem_cache_cpu *c)
  1346. {
  1347. #ifdef CONFIG_NUMA
  1348. struct zonelist *zonelist;
  1349. struct zoneref *z;
  1350. struct zone *zone;
  1351. enum zone_type high_zoneidx = gfp_zone(flags);
  1352. void *object;
  1353. unsigned int cpuset_mems_cookie;
  1354. /*
  1355. * The defrag ratio allows a configuration of the tradeoffs between
  1356. * inter node defragmentation and node local allocations. A lower
  1357. * defrag_ratio increases the tendency to do local allocations
  1358. * instead of attempting to obtain partial slabs from other nodes.
  1359. *
  1360. * If the defrag_ratio is set to 0 then kmalloc() always
  1361. * returns node local objects. If the ratio is higher then kmalloc()
  1362. * may return off node objects because partial slabs are obtained
  1363. * from other nodes and filled up.
  1364. *
  1365. * If /sys/kernel/slab/xx/defrag_ratio is set to 100 (which makes
  1366. * defrag_ratio = 1000) then every (well almost) allocation will
  1367. * first attempt to defrag slab caches on other nodes. This means
  1368. * scanning over all nodes to look for partial slabs which may be
  1369. * expensive if we do it every time we are trying to find a slab
  1370. * with available objects.
  1371. */
  1372. if (!s->remote_node_defrag_ratio ||
  1373. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1374. return NULL;
  1375. do {
  1376. cpuset_mems_cookie = get_mems_allowed();
  1377. zonelist = node_zonelist(slab_node(current->mempolicy), flags);
  1378. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  1379. struct kmem_cache_node *n;
  1380. n = get_node(s, zone_to_nid(zone));
  1381. if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
  1382. n->nr_partial > s->min_partial) {
  1383. object = get_partial_node(s, n, c);
  1384. if (object) {
  1385. /*
  1386. * Return the object even if
  1387. * put_mems_allowed indicated that
  1388. * the cpuset mems_allowed was
  1389. * updated in parallel. It's a
  1390. * harmless race between the alloc
  1391. * and the cpuset update.
  1392. */
  1393. put_mems_allowed(cpuset_mems_cookie);
  1394. return object;
  1395. }
  1396. }
  1397. }
  1398. } while (!put_mems_allowed(cpuset_mems_cookie));
  1399. #endif
  1400. return NULL;
  1401. }
  1402. /*
  1403. * Get a partial page, lock it and return it.
  1404. */
  1405. static void *get_partial(struct kmem_cache *s, gfp_t flags, int node,
  1406. struct kmem_cache_cpu *c)
  1407. {
  1408. void *object;
  1409. int searchnode = (node == NUMA_NO_NODE) ? numa_node_id() : node;
  1410. object = get_partial_node(s, get_node(s, searchnode), c);
  1411. if (object || node != NUMA_NO_NODE)
  1412. return object;
  1413. return get_any_partial(s, flags, c);
  1414. }
  1415. #ifdef CONFIG_PREEMPT
  1416. /*
  1417. * Calculate the next globally unique transaction for disambiguiation
  1418. * during cmpxchg. The transactions start with the cpu number and are then
  1419. * incremented by CONFIG_NR_CPUS.
  1420. */
  1421. #define TID_STEP roundup_pow_of_two(CONFIG_NR_CPUS)
  1422. #else
  1423. /*
  1424. * No preemption supported therefore also no need to check for
  1425. * different cpus.
  1426. */
  1427. #define TID_STEP 1
  1428. #endif
  1429. static inline unsigned long next_tid(unsigned long tid)
  1430. {
  1431. return tid + TID_STEP;
  1432. }
  1433. static inline unsigned int tid_to_cpu(unsigned long tid)
  1434. {
  1435. return tid % TID_STEP;
  1436. }
  1437. static inline unsigned long tid_to_event(unsigned long tid)
  1438. {
  1439. return tid / TID_STEP;
  1440. }
  1441. static inline unsigned int init_tid(int cpu)
  1442. {
  1443. return cpu;
  1444. }
  1445. static inline void note_cmpxchg_failure(const char *n,
  1446. const struct kmem_cache *s, unsigned long tid)
  1447. {
  1448. #ifdef SLUB_DEBUG_CMPXCHG
  1449. unsigned long actual_tid = __this_cpu_read(s->cpu_slab->tid);
  1450. printk(KERN_INFO "%s %s: cmpxchg redo ", n, s->name);
  1451. #ifdef CONFIG_PREEMPT
  1452. if (tid_to_cpu(tid) != tid_to_cpu(actual_tid))
  1453. printk("due to cpu change %d -> %d\n",
  1454. tid_to_cpu(tid), tid_to_cpu(actual_tid));
  1455. else
  1456. #endif
  1457. if (tid_to_event(tid) != tid_to_event(actual_tid))
  1458. printk("due to cpu running other code. Event %ld->%ld\n",
  1459. tid_to_event(tid), tid_to_event(actual_tid));
  1460. else
  1461. printk("for unknown reason: actual=%lx was=%lx target=%lx\n",
  1462. actual_tid, tid, next_tid(tid));
  1463. #endif
  1464. stat(s, CMPXCHG_DOUBLE_CPU_FAIL);
  1465. }
  1466. void init_kmem_cache_cpus(struct kmem_cache *s)
  1467. {
  1468. int cpu;
  1469. for_each_possible_cpu(cpu)
  1470. per_cpu_ptr(s->cpu_slab, cpu)->tid = init_tid(cpu);
  1471. }
  1472. /*
  1473. * Remove the cpu slab
  1474. */
  1475. static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1476. {
  1477. enum slab_modes { M_NONE, M_PARTIAL, M_FULL, M_FREE };
  1478. struct page *page = c->page;
  1479. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1480. int lock = 0;
  1481. enum slab_modes l = M_NONE, m = M_NONE;
  1482. void *freelist;
  1483. void *nextfree;
  1484. int tail = DEACTIVATE_TO_HEAD;
  1485. struct page new;
  1486. struct page old;
  1487. if (page->freelist) {
  1488. stat(s, DEACTIVATE_REMOTE_FREES);
  1489. tail = DEACTIVATE_TO_TAIL;
  1490. }
  1491. c->tid = next_tid(c->tid);
  1492. c->page = NULL;
  1493. freelist = c->freelist;
  1494. c->freelist = NULL;
  1495. /*
  1496. * Stage one: Free all available per cpu objects back
  1497. * to the page freelist while it is still frozen. Leave the
  1498. * last one.
  1499. *
  1500. * There is no need to take the list->lock because the page
  1501. * is still frozen.
  1502. */
  1503. while (freelist && (nextfree = get_freepointer(s, freelist))) {
  1504. void *prior;
  1505. unsigned long counters;
  1506. do {
  1507. prior = page->freelist;
  1508. counters = page->counters;
  1509. set_freepointer(s, freelist, prior);
  1510. new.counters = counters;
  1511. new.inuse--;
  1512. VM_BUG_ON(!new.frozen);
  1513. } while (!__cmpxchg_double_slab(s, page,
  1514. prior, counters,
  1515. freelist, new.counters,
  1516. "drain percpu freelist"));
  1517. freelist = nextfree;
  1518. }
  1519. /*
  1520. * Stage two: Ensure that the page is unfrozen while the
  1521. * list presence reflects the actual number of objects
  1522. * during unfreeze.
  1523. *
  1524. * We setup the list membership and then perform a cmpxchg
  1525. * with the count. If there is a mismatch then the page
  1526. * is not unfrozen but the page is on the wrong list.
  1527. *
  1528. * Then we restart the process which may have to remove
  1529. * the page from the list that we just put it on again
  1530. * because the number of objects in the slab may have
  1531. * changed.
  1532. */
  1533. redo:
  1534. old.freelist = page->freelist;
  1535. old.counters = page->counters;
  1536. VM_BUG_ON(!old.frozen);
  1537. /* Determine target state of the slab */
  1538. new.counters = old.counters;
  1539. if (freelist) {
  1540. new.inuse--;
  1541. set_freepointer(s, freelist, old.freelist);
  1542. new.freelist = freelist;
  1543. } else
  1544. new.freelist = old.freelist;
  1545. new.frozen = 0;
  1546. if (!new.inuse && n->nr_partial > s->min_partial)
  1547. m = M_FREE;
  1548. else if (new.freelist) {
  1549. m = M_PARTIAL;
  1550. if (!lock) {
  1551. lock = 1;
  1552. /*
  1553. * Taking the spinlock removes the possiblity
  1554. * that acquire_slab() will see a slab page that
  1555. * is frozen
  1556. */
  1557. spin_lock(&n->list_lock);
  1558. }
  1559. } else {
  1560. m = M_FULL;
  1561. if (kmem_cache_debug(s) && !lock) {
  1562. lock = 1;
  1563. /*
  1564. * This also ensures that the scanning of full
  1565. * slabs from diagnostic functions will not see
  1566. * any frozen slabs.
  1567. */
  1568. spin_lock(&n->list_lock);
  1569. }
  1570. }
  1571. if (l != m) {
  1572. if (l == M_PARTIAL)
  1573. remove_partial(n, page);
  1574. else if (l == M_FULL)
  1575. remove_full(s, page);
  1576. if (m == M_PARTIAL) {
  1577. add_partial(n, page, tail);
  1578. stat(s, tail);
  1579. } else if (m == M_FULL) {
  1580. stat(s, DEACTIVATE_FULL);
  1581. add_full(s, n, page);
  1582. }
  1583. }
  1584. l = m;
  1585. if (!__cmpxchg_double_slab(s, page,
  1586. old.freelist, old.counters,
  1587. new.freelist, new.counters,
  1588. "unfreezing slab"))
  1589. goto redo;
  1590. if (lock)
  1591. spin_unlock(&n->list_lock);
  1592. if (m == M_FREE) {
  1593. stat(s, DEACTIVATE_EMPTY);
  1594. discard_slab(s, page);
  1595. stat(s, FREE_SLAB);
  1596. }
  1597. }
  1598. /* Unfreeze all the cpu partial slabs */
  1599. static void unfreeze_partials(struct kmem_cache *s)
  1600. {
  1601. struct kmem_cache_node *n = NULL;
  1602. struct kmem_cache_cpu *c = this_cpu_ptr(s->cpu_slab);
  1603. struct page *page, *discard_page = NULL;
  1604. while ((page = c->partial)) {
  1605. enum slab_modes { M_PARTIAL, M_FREE };
  1606. enum slab_modes l, m;
  1607. struct page new;
  1608. struct page old;
  1609. c->partial = page->next;
  1610. l = M_FREE;
  1611. do {
  1612. old.freelist = page->freelist;
  1613. old.counters = page->counters;
  1614. VM_BUG_ON(!old.frozen);
  1615. new.counters = old.counters;
  1616. new.freelist = old.freelist;
  1617. new.frozen = 0;
  1618. if (!new.inuse && (!n || n->nr_partial > s->min_partial))
  1619. m = M_FREE;
  1620. else {
  1621. struct kmem_cache_node *n2 = get_node(s,
  1622. page_to_nid(page));
  1623. m = M_PARTIAL;
  1624. if (n != n2) {
  1625. if (n)
  1626. spin_unlock(&n->list_lock);
  1627. n = n2;
  1628. spin_lock(&n->list_lock);
  1629. }
  1630. }
  1631. if (l != m) {
  1632. if (l == M_PARTIAL) {
  1633. remove_partial(n, page);
  1634. stat(s, FREE_REMOVE_PARTIAL);
  1635. } else {
  1636. add_partial(n, page,
  1637. DEACTIVATE_TO_TAIL);
  1638. stat(s, FREE_ADD_PARTIAL);
  1639. }
  1640. l = m;
  1641. }
  1642. } while (!cmpxchg_double_slab(s, page,
  1643. old.freelist, old.counters,
  1644. new.freelist, new.counters,
  1645. "unfreezing slab"));
  1646. if (m == M_FREE) {
  1647. page->next = discard_page;
  1648. discard_page = page;
  1649. }
  1650. }
  1651. if (n)
  1652. spin_unlock(&n->list_lock);
  1653. while (discard_page) {
  1654. page = discard_page;
  1655. discard_page = discard_page->next;
  1656. stat(s, DEACTIVATE_EMPTY);
  1657. discard_slab(s, page);
  1658. stat(s, FREE_SLAB);
  1659. }
  1660. }
  1661. /*
  1662. * Put a page that was just frozen (in __slab_free) into a partial page
  1663. * slot if available. This is done without interrupts disabled and without
  1664. * preemption disabled. The cmpxchg is racy and may put the partial page
  1665. * onto a random cpus partial slot.
  1666. *
  1667. * If we did not find a slot then simply move all the partials to the
  1668. * per node partial list.
  1669. */
  1670. int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
  1671. {
  1672. struct page *oldpage;
  1673. int pages;
  1674. int pobjects;
  1675. do {
  1676. pages = 0;
  1677. pobjects = 0;
  1678. oldpage = this_cpu_read(s->cpu_slab->partial);
  1679. if (oldpage) {
  1680. pobjects = oldpage->pobjects;
  1681. pages = oldpage->pages;
  1682. if (drain && pobjects > s->cpu_partial) {
  1683. unsigned long flags;
  1684. /*
  1685. * partial array is full. Move the existing
  1686. * set to the per node partial list.
  1687. */
  1688. local_irq_save(flags);
  1689. unfreeze_partials(s);
  1690. local_irq_restore(flags);
  1691. pobjects = 0;
  1692. pages = 0;
  1693. stat(s, CPU_PARTIAL_DRAIN);
  1694. }
  1695. }
  1696. pages++;
  1697. pobjects += page->objects - page->inuse;
  1698. page->pages = pages;
  1699. page->pobjects = pobjects;
  1700. page->next = oldpage;
  1701. } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page) != oldpage);
  1702. return pobjects;
  1703. }
  1704. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1705. {
  1706. stat(s, CPUSLAB_FLUSH);
  1707. deactivate_slab(s, c);
  1708. }
  1709. /*
  1710. * Flush cpu slab.
  1711. *
  1712. * Called from IPI handler with interrupts disabled.
  1713. */
  1714. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1715. {
  1716. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  1717. if (likely(c)) {
  1718. if (c->page)
  1719. flush_slab(s, c);
  1720. unfreeze_partials(s);
  1721. }
  1722. }
  1723. static void flush_cpu_slab(void *d)
  1724. {
  1725. struct kmem_cache *s = d;
  1726. __flush_cpu_slab(s, smp_processor_id());
  1727. }
  1728. static bool has_cpu_slab(int cpu, void *info)
  1729. {
  1730. struct kmem_cache *s = info;
  1731. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  1732. return c->page || c->partial;
  1733. }
  1734. static void flush_all(struct kmem_cache *s)
  1735. {
  1736. on_each_cpu_cond(has_cpu_slab, flush_cpu_slab, s, 1, GFP_ATOMIC);
  1737. }
  1738. /*
  1739. * Check if the objects in a per cpu structure fit numa
  1740. * locality expectations.
  1741. */
  1742. static inline int node_match(struct kmem_cache_cpu *c, int node)
  1743. {
  1744. #ifdef CONFIG_NUMA
  1745. if (node != NUMA_NO_NODE && c->node != node)
  1746. return 0;
  1747. #endif
  1748. return 1;
  1749. }
  1750. static int count_free(struct page *page)
  1751. {
  1752. return page->objects - page->inuse;
  1753. }
  1754. static unsigned long count_partial(struct kmem_cache_node *n,
  1755. int (*get_count)(struct page *))
  1756. {
  1757. unsigned long flags;
  1758. unsigned long x = 0;
  1759. struct page *page;
  1760. spin_lock_irqsave(&n->list_lock, flags);
  1761. list_for_each_entry(page, &n->partial, lru)
  1762. x += get_count(page);
  1763. spin_unlock_irqrestore(&n->list_lock, flags);
  1764. return x;
  1765. }
  1766. static inline unsigned long node_nr_objs(struct kmem_cache_node *n)
  1767. {
  1768. #ifdef CONFIG_SLUB_DEBUG
  1769. return atomic_long_read(&n->total_objects);
  1770. #else
  1771. return 0;
  1772. #endif
  1773. }
  1774. static noinline void
  1775. slab_out_of_memory(struct kmem_cache *s, gfp_t gfpflags, int nid)
  1776. {
  1777. int node;
  1778. printk(KERN_WARNING
  1779. "SLUB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1780. nid, gfpflags);
  1781. printk(KERN_WARNING " cache: %s, object size: %d, buffer size: %d, "
  1782. "default order: %d, min order: %d\n", s->name, s->objsize,
  1783. s->size, oo_order(s->oo), oo_order(s->min));
  1784. if (oo_order(s->min) > get_order(s->objsize))
  1785. printk(KERN_WARNING " %s debugging increased min order, use "
  1786. "slub_debug=O to disable.\n", s->name);
  1787. for_each_online_node(node) {
  1788. struct kmem_cache_node *n = get_node(s, node);
  1789. unsigned long nr_slabs;
  1790. unsigned long nr_objs;
  1791. unsigned long nr_free;
  1792. if (!n)
  1793. continue;
  1794. nr_free = count_partial(n, count_free);
  1795. nr_slabs = node_nr_slabs(n);
  1796. nr_objs = node_nr_objs(n);
  1797. printk(KERN_WARNING
  1798. " node %d: slabs: %ld, objs: %ld, free: %ld\n",
  1799. node, nr_slabs, nr_objs, nr_free);
  1800. }
  1801. }
  1802. static inline void *new_slab_objects(struct kmem_cache *s, gfp_t flags,
  1803. int node, struct kmem_cache_cpu **pc)
  1804. {
  1805. void *freelist;
  1806. struct kmem_cache_cpu *c;
  1807. struct page *page = new_slab(s, flags, node);
  1808. if (page) {
  1809. c = __this_cpu_ptr(s->cpu_slab);
  1810. if (c->page)
  1811. flush_slab(s, c);
  1812. /*
  1813. * No other reference to the page yet so we can
  1814. * muck around with it freely without cmpxchg
  1815. */
  1816. freelist = page->freelist;
  1817. page->freelist = NULL;
  1818. stat(s, ALLOC_SLAB);
  1819. c->node = page_to_nid(page);
  1820. c->page = page;
  1821. *pc = c;
  1822. } else
  1823. freelist = NULL;
  1824. return freelist;
  1825. }
  1826. /*
  1827. * Check the page->freelist of a page and either transfer the freelist to the per cpu freelist
  1828. * or deactivate the page.
  1829. *
  1830. * The page is still frozen if the return value is not NULL.
  1831. *
  1832. * If this function returns NULL then the page has been unfrozen.
  1833. */
  1834. static inline void *get_freelist(struct kmem_cache *s, struct page *page)
  1835. {
  1836. struct page new;
  1837. unsigned long counters;
  1838. void *freelist;
  1839. do {
  1840. freelist = page->freelist;
  1841. counters = page->counters;
  1842. new.counters = counters;
  1843. VM_BUG_ON(!new.frozen);
  1844. new.inuse = page->objects;
  1845. new.frozen = freelist != NULL;
  1846. } while (!cmpxchg_double_slab(s, page,
  1847. freelist, counters,
  1848. NULL, new.counters,
  1849. "get_freelist"));
  1850. return freelist;
  1851. }
  1852. /*
  1853. * Slow path. The lockless freelist is empty or we need to perform
  1854. * debugging duties.
  1855. *
  1856. * Processing is still very fast if new objects have been freed to the
  1857. * regular freelist. In that case we simply take over the regular freelist
  1858. * as the lockless freelist and zap the regular freelist.
  1859. *
  1860. * If that is not working then we fall back to the partial lists. We take the
  1861. * first element of the freelist as the object to allocate now and move the
  1862. * rest of the freelist to the lockless freelist.
  1863. *
  1864. * And if we were unable to get a new slab from the partial slab lists then
  1865. * we need to allocate a new slab. This is the slowest path since it involves
  1866. * a call to the page allocator and the setup of a new slab.
  1867. */
  1868. static void *__slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
  1869. unsigned long addr, struct kmem_cache_cpu *c)
  1870. {
  1871. void *freelist;
  1872. unsigned long flags;
  1873. local_irq_save(flags);
  1874. #ifdef CONFIG_PREEMPT
  1875. /*
  1876. * We may have been preempted and rescheduled on a different
  1877. * cpu before disabling interrupts. Need to reload cpu area
  1878. * pointer.
  1879. */
  1880. c = this_cpu_ptr(s->cpu_slab);
  1881. #endif
  1882. if (!c->page)
  1883. goto new_slab;
  1884. redo:
  1885. if (unlikely(!node_match(c, node))) {
  1886. stat(s, ALLOC_NODE_MISMATCH);
  1887. deactivate_slab(s, c);
  1888. goto new_slab;
  1889. }
  1890. /* must check again c->freelist in case of cpu migration or IRQ */
  1891. freelist = c->freelist;
  1892. if (freelist)
  1893. goto load_freelist;
  1894. stat(s, ALLOC_SLOWPATH);
  1895. freelist = get_freelist(s, c->page);
  1896. if (!freelist) {
  1897. c->page = NULL;
  1898. stat(s, DEACTIVATE_BYPASS);
  1899. goto new_slab;
  1900. }
  1901. stat(s, ALLOC_REFILL);
  1902. load_freelist:
  1903. /*
  1904. * freelist is pointing to the list of objects to be used.
  1905. * page is pointing to the page from which the objects are obtained.
  1906. * That page must be frozen for per cpu allocations to work.
  1907. */
  1908. VM_BUG_ON(!c->page->frozen);
  1909. c->freelist = get_freepointer(s, freelist);
  1910. c->tid = next_tid(c->tid);
  1911. local_irq_restore(flags);
  1912. return freelist;
  1913. new_slab:
  1914. if (c->partial) {
  1915. c->page = c->partial;
  1916. c->partial = c->page->next;
  1917. c->node = page_to_nid(c->page);
  1918. stat(s, CPU_PARTIAL_ALLOC);
  1919. c->freelist = NULL;
  1920. goto redo;
  1921. }
  1922. /* Then do expensive stuff like retrieving pages from the partial lists */
  1923. freelist = get_partial(s, gfpflags, node, c);
  1924. if (unlikely(!freelist)) {
  1925. freelist = new_slab_objects(s, gfpflags, node, &c);
  1926. if (unlikely(!freelist)) {
  1927. if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
  1928. slab_out_of_memory(s, gfpflags, node);
  1929. local_irq_restore(flags);
  1930. return NULL;
  1931. }
  1932. }
  1933. if (likely(!kmem_cache_debug(s)))
  1934. goto load_freelist;
  1935. /* Only entered in the debug case */
  1936. if (!alloc_debug_processing(s, c->page, freelist, addr))
  1937. goto new_slab; /* Slab failed checks. Next slab needed */
  1938. c->freelist = get_freepointer(s, freelist);
  1939. deactivate_slab(s, c);
  1940. c->node = NUMA_NO_NODE;
  1941. local_irq_restore(flags);
  1942. return freelist;
  1943. }
  1944. /*
  1945. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1946. * have the fastpath folded into their functions. So no function call
  1947. * overhead for requests that can be satisfied on the fastpath.
  1948. *
  1949. * The fastpath works by first checking if the lockless freelist can be used.
  1950. * If not then __slab_alloc is called for slow processing.
  1951. *
  1952. * Otherwise we can simply pick the next object from the lockless free list.
  1953. */
  1954. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1955. gfp_t gfpflags, int node, unsigned long addr)
  1956. {
  1957. void **object;
  1958. struct kmem_cache_cpu *c;
  1959. unsigned long tid;
  1960. if (slab_pre_alloc_hook(s, gfpflags))
  1961. return NULL;
  1962. redo:
  1963. /*
  1964. * Must read kmem_cache cpu data via this cpu ptr. Preemption is
  1965. * enabled. We may switch back and forth between cpus while
  1966. * reading from one cpu area. That does not matter as long
  1967. * as we end up on the original cpu again when doing the cmpxchg.
  1968. */
  1969. c = __this_cpu_ptr(s->cpu_slab);
  1970. /*
  1971. * The transaction ids are globally unique per cpu and per operation on
  1972. * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
  1973. * occurs on the right processor and that there was no operation on the
  1974. * linked list in between.
  1975. */
  1976. tid = c->tid;
  1977. barrier();
  1978. object = c->freelist;
  1979. if (unlikely(!object || !node_match(c, node)))
  1980. object = __slab_alloc(s, gfpflags, node, addr, c);
  1981. else {
  1982. void *next_object = get_freepointer_safe(s, object);
  1983. /*
  1984. * The cmpxchg will only match if there was no additional
  1985. * operation and if we are on the right processor.
  1986. *
  1987. * The cmpxchg does the following atomically (without lock semantics!)
  1988. * 1. Relocate first pointer to the current per cpu area.
  1989. * 2. Verify that tid and freelist have not been changed
  1990. * 3. If they were not changed replace tid and freelist
  1991. *
  1992. * Since this is without lock semantics the protection is only against
  1993. * code executing on this cpu *not* from access by other cpus.
  1994. */
  1995. if (unlikely(!this_cpu_cmpxchg_double(
  1996. s->cpu_slab->freelist, s->cpu_slab->tid,
  1997. object, tid,
  1998. next_object, next_tid(tid)))) {
  1999. note_cmpxchg_failure("slab_alloc", s, tid);
  2000. goto redo;
  2001. }
  2002. prefetch_freepointer(s, next_object);
  2003. stat(s, ALLOC_FASTPATH);
  2004. }
  2005. if (unlikely(gfpflags & __GFP_ZERO) && object)
  2006. memset(object, 0, s->objsize);
  2007. slab_post_alloc_hook(s, gfpflags, object);
  2008. return object;
  2009. }
  2010. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  2011. {
  2012. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  2013. trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
  2014. return ret;
  2015. }
  2016. EXPORT_SYMBOL(kmem_cache_alloc);
  2017. #ifdef CONFIG_TRACING
  2018. void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  2019. {
  2020. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  2021. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
  2022. return ret;
  2023. }
  2024. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  2025. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  2026. {
  2027. void *ret = kmalloc_order(size, flags, order);
  2028. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  2029. return ret;
  2030. }
  2031. EXPORT_SYMBOL(kmalloc_order_trace);
  2032. #endif
  2033. #ifdef CONFIG_NUMA
  2034. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  2035. {
  2036. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  2037. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  2038. s->objsize, s->size, gfpflags, node);
  2039. return ret;
  2040. }
  2041. EXPORT_SYMBOL(kmem_cache_alloc_node);
  2042. #ifdef CONFIG_TRACING
  2043. void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  2044. gfp_t gfpflags,
  2045. int node, size_t size)
  2046. {
  2047. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  2048. trace_kmalloc_node(_RET_IP_, ret,
  2049. size, s->size, gfpflags, node);
  2050. return ret;
  2051. }
  2052. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  2053. #endif
  2054. #endif
  2055. /*
  2056. * Slow patch handling. This may still be called frequently since objects
  2057. * have a longer lifetime than the cpu slabs in most processing loads.
  2058. *
  2059. * So we still attempt to reduce cache line usage. Just take the slab
  2060. * lock and free the item. If there is no additional partial page
  2061. * handling required then we can return immediately.
  2062. */
  2063. static void __slab_free(struct kmem_cache *s, struct page *page,
  2064. void *x, unsigned long addr)
  2065. {
  2066. void *prior;
  2067. void **object = (void *)x;
  2068. int was_frozen;
  2069. int inuse;
  2070. struct page new;
  2071. unsigned long counters;
  2072. struct kmem_cache_node *n = NULL;
  2073. unsigned long uninitialized_var(flags);
  2074. stat(s, FREE_SLOWPATH);
  2075. if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
  2076. return;
  2077. do {
  2078. prior = page->freelist;
  2079. counters = page->counters;
  2080. set_freepointer(s, object, prior);
  2081. new.counters = counters;
  2082. was_frozen = new.frozen;
  2083. new.inuse--;
  2084. if ((!new.inuse || !prior) && !was_frozen && !n) {
  2085. if (!kmem_cache_debug(s) && !prior)
  2086. /*
  2087. * Slab was on no list before and will be partially empty
  2088. * We can defer the list move and instead freeze it.
  2089. */
  2090. new.frozen = 1;
  2091. else { /* Needs to be taken off a list */
  2092. n = get_node(s, page_to_nid(page));
  2093. /*
  2094. * Speculatively acquire the list_lock.
  2095. * If the cmpxchg does not succeed then we may
  2096. * drop the list_lock without any processing.
  2097. *
  2098. * Otherwise the list_lock will synchronize with
  2099. * other processors updating the list of slabs.
  2100. */
  2101. spin_lock_irqsave(&n->list_lock, flags);
  2102. }
  2103. }
  2104. inuse = new.inuse;
  2105. } while (!cmpxchg_double_slab(s, page,
  2106. prior, counters,
  2107. object, new.counters,
  2108. "__slab_free"));
  2109. if (likely(!n)) {
  2110. /*
  2111. * If we just froze the page then put it onto the
  2112. * per cpu partial list.
  2113. */
  2114. if (new.frozen && !was_frozen) {
  2115. put_cpu_partial(s, page, 1);
  2116. stat(s, CPU_PARTIAL_FREE);
  2117. }
  2118. /*
  2119. * The list lock was not taken therefore no list
  2120. * activity can be necessary.
  2121. */
  2122. if (was_frozen)
  2123. stat(s, FREE_FROZEN);
  2124. return;
  2125. }
  2126. /*
  2127. * was_frozen may have been set after we acquired the list_lock in
  2128. * an earlier loop. So we need to check it here again.
  2129. */
  2130. if (was_frozen)
  2131. stat(s, FREE_FROZEN);
  2132. else {
  2133. if (unlikely(!inuse && n->nr_partial > s->min_partial))
  2134. goto slab_empty;
  2135. /*
  2136. * Objects left in the slab. If it was not on the partial list before
  2137. * then add it.
  2138. */
  2139. if (unlikely(!prior)) {
  2140. remove_full(s, page);
  2141. add_partial(n, page, DEACTIVATE_TO_TAIL);
  2142. stat(s, FREE_ADD_PARTIAL);
  2143. }
  2144. }
  2145. spin_unlock_irqrestore(&n->list_lock, flags);
  2146. return;
  2147. slab_empty:
  2148. if (prior) {
  2149. /*
  2150. * Slab on the partial list.
  2151. */
  2152. remove_partial(n, page);
  2153. stat(s, FREE_REMOVE_PARTIAL);
  2154. } else
  2155. /* Slab must be on the full list */
  2156. remove_full(s, page);
  2157. spin_unlock_irqrestore(&n->list_lock, flags);
  2158. stat(s, FREE_SLAB);
  2159. discard_slab(s, page);
  2160. }
  2161. /*
  2162. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  2163. * can perform fastpath freeing without additional function calls.
  2164. *
  2165. * The fastpath is only possible if we are freeing to the current cpu slab
  2166. * of this processor. This typically the case if we have just allocated
  2167. * the item before.
  2168. *
  2169. * If fastpath is not possible then fall back to __slab_free where we deal
  2170. * with all sorts of special processing.
  2171. */
  2172. static __always_inline void slab_free(struct kmem_cache *s,
  2173. struct page *page, void *x, unsigned long addr)
  2174. {
  2175. void **object = (void *)x;
  2176. struct kmem_cache_cpu *c;
  2177. unsigned long tid;
  2178. slab_free_hook(s, x);
  2179. redo:
  2180. /*
  2181. * Determine the currently cpus per cpu slab.
  2182. * The cpu may change afterward. However that does not matter since
  2183. * data is retrieved via this pointer. If we are on the same cpu
  2184. * during the cmpxchg then the free will succedd.
  2185. */
  2186. c = __this_cpu_ptr(s->cpu_slab);
  2187. tid = c->tid;
  2188. barrier();
  2189. if (likely(page == c->page)) {
  2190. set_freepointer(s, object, c->freelist);
  2191. if (unlikely(!this_cpu_cmpxchg_double(
  2192. s->cpu_slab->freelist, s->cpu_slab->tid,
  2193. c->freelist, tid,
  2194. object, next_tid(tid)))) {
  2195. note_cmpxchg_failure("slab_free", s, tid);
  2196. goto redo;
  2197. }
  2198. stat(s, FREE_FASTPATH);
  2199. } else
  2200. __slab_free(s, page, x, addr);
  2201. }
  2202. void kmem_cache_free(struct kmem_cache *s, void *x)
  2203. {
  2204. struct page *page;
  2205. page = virt_to_head_page(x);
  2206. slab_free(s, page, x, _RET_IP_);
  2207. trace_kmem_cache_free(_RET_IP_, x);
  2208. }
  2209. EXPORT_SYMBOL(kmem_cache_free);
  2210. /*
  2211. * Object placement in a slab is made very easy because we always start at
  2212. * offset 0. If we tune the size of the object to the alignment then we can
  2213. * get the required alignment by putting one properly sized object after
  2214. * another.
  2215. *
  2216. * Notice that the allocation order determines the sizes of the per cpu
  2217. * caches. Each processor has always one slab available for allocations.
  2218. * Increasing the allocation order reduces the number of times that slabs
  2219. * must be moved on and off the partial lists and is therefore a factor in
  2220. * locking overhead.
  2221. */
  2222. /*
  2223. * Mininum / Maximum order of slab pages. This influences locking overhead
  2224. * and slab fragmentation. A higher order reduces the number of partial slabs
  2225. * and increases the number of allocations possible without having to
  2226. * take the list_lock.
  2227. */
  2228. static int slub_min_order;
  2229. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  2230. static int slub_min_objects;
  2231. /*
  2232. * Merge control. If this is set then no merging of slab caches will occur.
  2233. * (Could be removed. This was introduced to pacify the merge skeptics.)
  2234. */
  2235. static int slub_nomerge;
  2236. /*
  2237. * Calculate the order of allocation given an slab object size.
  2238. *
  2239. * The order of allocation has significant impact on performance and other
  2240. * system components. Generally order 0 allocations should be preferred since
  2241. * order 0 does not cause fragmentation in the page allocator. Larger objects
  2242. * be problematic to put into order 0 slabs because there may be too much
  2243. * unused space left. We go to a higher order if more than 1/16th of the slab
  2244. * would be wasted.
  2245. *
  2246. * In order to reach satisfactory performance we must ensure that a minimum
  2247. * number of objects is in one slab. Otherwise we may generate too much
  2248. * activity on the partial lists which requires taking the list_lock. This is
  2249. * less a concern for large slabs though which are rarely used.
  2250. *
  2251. * slub_max_order specifies the order where we begin to stop considering the
  2252. * number of objects in a slab as critical. If we reach slub_max_order then
  2253. * we try to keep the page order as low as possible. So we accept more waste
  2254. * of space in favor of a small page order.
  2255. *
  2256. * Higher order allocations also allow the placement of more objects in a
  2257. * slab and thereby reduce object handling overhead. If the user has
  2258. * requested a higher mininum order then we start with that one instead of
  2259. * the smallest order which will fit the object.
  2260. */
  2261. static inline int slab_order(int size, int min_objects,
  2262. int max_order, int fract_leftover, int reserved)
  2263. {
  2264. int order;
  2265. int rem;
  2266. int min_order = slub_min_order;
  2267. if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
  2268. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  2269. for (order = max(min_order,
  2270. fls(min_objects * size - 1) - PAGE_SHIFT);
  2271. order <= max_order; order++) {
  2272. unsigned long slab_size = PAGE_SIZE << order;
  2273. if (slab_size < min_objects * size + reserved)
  2274. continue;
  2275. rem = (slab_size - reserved) % size;
  2276. if (rem <= slab_size / fract_leftover)
  2277. break;
  2278. }
  2279. return order;
  2280. }
  2281. static inline int calculate_order(int size, int reserved)
  2282. {
  2283. int order;
  2284. int min_objects;
  2285. int fraction;
  2286. int max_objects;
  2287. /*
  2288. * Attempt to find best configuration for a slab. This
  2289. * works by first attempting to generate a layout with
  2290. * the best configuration and backing off gradually.
  2291. *
  2292. * First we reduce the acceptable waste in a slab. Then
  2293. * we reduce the minimum objects required in a slab.
  2294. */
  2295. min_objects = slub_min_objects;
  2296. if (!min_objects)
  2297. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  2298. max_objects = order_objects(slub_max_order, size, reserved);
  2299. min_objects = min(min_objects, max_objects);
  2300. while (min_objects > 1) {
  2301. fraction = 16;
  2302. while (fraction >= 4) {
  2303. order = slab_order(size, min_objects,
  2304. slub_max_order, fraction, reserved);
  2305. if (order <= slub_max_order)
  2306. return order;
  2307. fraction /= 2;
  2308. }
  2309. min_objects--;
  2310. }
  2311. /*
  2312. * We were unable to place multiple objects in a slab. Now
  2313. * lets see if we can place a single object there.
  2314. */
  2315. order = slab_order(size, 1, slub_max_order, 1, reserved);
  2316. if (order <= slub_max_order)
  2317. return order;
  2318. /*
  2319. * Doh this slab cannot be placed using slub_max_order.
  2320. */
  2321. order = slab_order(size, 1, MAX_ORDER, 1, reserved);
  2322. if (order < MAX_ORDER)
  2323. return order;
  2324. return -ENOSYS;
  2325. }
  2326. /*
  2327. * Figure out what the alignment of the objects will be.
  2328. */
  2329. static unsigned long calculate_alignment(unsigned long flags,
  2330. unsigned long align, unsigned long size)
  2331. {
  2332. /*
  2333. * If the user wants hardware cache aligned objects then follow that
  2334. * suggestion if the object is sufficiently large.
  2335. *
  2336. * The hardware cache alignment cannot override the specified
  2337. * alignment though. If that is greater then use it.
  2338. */
  2339. if (flags & SLAB_HWCACHE_ALIGN) {
  2340. unsigned long ralign = cache_line_size();
  2341. while (size <= ralign / 2)
  2342. ralign /= 2;
  2343. align = max(align, ralign);
  2344. }
  2345. if (align < ARCH_SLAB_MINALIGN)
  2346. align = ARCH_SLAB_MINALIGN;
  2347. return ALIGN(align, sizeof(void *));
  2348. }
  2349. static void
  2350. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  2351. {
  2352. n->nr_partial = 0;
  2353. spin_lock_init(&n->list_lock);
  2354. INIT_LIST_HEAD(&n->partial);
  2355. #ifdef CONFIG_SLUB_DEBUG
  2356. atomic_long_set(&n->nr_slabs, 0);
  2357. atomic_long_set(&n->total_objects, 0);
  2358. INIT_LIST_HEAD(&n->full);
  2359. #endif
  2360. }
  2361. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
  2362. {
  2363. BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
  2364. SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
  2365. /*
  2366. * Must align to double word boundary for the double cmpxchg
  2367. * instructions to work; see __pcpu_double_call_return_bool().
  2368. */
  2369. s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
  2370. 2 * sizeof(void *));
  2371. if (!s->cpu_slab)
  2372. return 0;
  2373. init_kmem_cache_cpus(s);
  2374. return 1;
  2375. }
  2376. static struct kmem_cache *kmem_cache_node;
  2377. /*
  2378. * No kmalloc_node yet so do it by hand. We know that this is the first
  2379. * slab on the node for this slabcache. There are no concurrent accesses
  2380. * possible.
  2381. *
  2382. * Note that this function only works on the kmalloc_node_cache
  2383. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  2384. * memory on a fresh node that has no slab structures yet.
  2385. */
  2386. static void early_kmem_cache_node_alloc(int node)
  2387. {
  2388. struct page *page;
  2389. struct kmem_cache_node *n;
  2390. BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
  2391. page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
  2392. BUG_ON(!page);
  2393. if (page_to_nid(page) != node) {
  2394. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  2395. "node %d\n", node);
  2396. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  2397. "in order to be able to continue\n");
  2398. }
  2399. n = page->freelist;
  2400. BUG_ON(!n);
  2401. page->freelist = get_freepointer(kmem_cache_node, n);
  2402. page->inuse = 1;
  2403. page->frozen = 0;
  2404. kmem_cache_node->node[node] = n;
  2405. #ifdef CONFIG_SLUB_DEBUG
  2406. init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
  2407. init_tracking(kmem_cache_node, n);
  2408. #endif
  2409. init_kmem_cache_node(n, kmem_cache_node);
  2410. inc_slabs_node(kmem_cache_node, node, page->objects);
  2411. add_partial(n, page, DEACTIVATE_TO_HEAD);
  2412. }
  2413. static void free_kmem_cache_nodes(struct kmem_cache *s)
  2414. {
  2415. int node;
  2416. for_each_node_state(node, N_NORMAL_MEMORY) {
  2417. struct kmem_cache_node *n = s->node[node];
  2418. if (n)
  2419. kmem_cache_free(kmem_cache_node, n);
  2420. s->node[node] = NULL;
  2421. }
  2422. }
  2423. static int init_kmem_cache_nodes(struct kmem_cache *s)
  2424. {
  2425. int node;
  2426. for_each_node_state(node, N_NORMAL_MEMORY) {
  2427. struct kmem_cache_node *n;
  2428. if (slab_state == DOWN) {
  2429. early_kmem_cache_node_alloc(node);
  2430. continue;
  2431. }
  2432. n = kmem_cache_alloc_node(kmem_cache_node,
  2433. GFP_KERNEL, node);
  2434. if (!n) {
  2435. free_kmem_cache_nodes(s);
  2436. return 0;
  2437. }
  2438. s->node[node] = n;
  2439. init_kmem_cache_node(n, s);
  2440. }
  2441. return 1;
  2442. }
  2443. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  2444. {
  2445. if (min < MIN_PARTIAL)
  2446. min = MIN_PARTIAL;
  2447. else if (min > MAX_PARTIAL)
  2448. min = MAX_PARTIAL;
  2449. s->min_partial = min;
  2450. }
  2451. /*
  2452. * calculate_sizes() determines the order and the distribution of data within
  2453. * a slab object.
  2454. */
  2455. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  2456. {
  2457. unsigned long flags = s->flags;
  2458. unsigned long size = s->objsize;
  2459. unsigned long align = s->align;
  2460. int order;
  2461. /*
  2462. * Round up object size to the next word boundary. We can only
  2463. * place the free pointer at word boundaries and this determines
  2464. * the possible location of the free pointer.
  2465. */
  2466. size = ALIGN(size, sizeof(void *));
  2467. #ifdef CONFIG_SLUB_DEBUG
  2468. /*
  2469. * Determine if we can poison the object itself. If the user of
  2470. * the slab may touch the object after free or before allocation
  2471. * then we should never poison the object itself.
  2472. */
  2473. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  2474. !s->ctor)
  2475. s->flags |= __OBJECT_POISON;
  2476. else
  2477. s->flags &= ~__OBJECT_POISON;
  2478. /*
  2479. * If we are Redzoning then check if there is some space between the
  2480. * end of the object and the free pointer. If not then add an
  2481. * additional word to have some bytes to store Redzone information.
  2482. */
  2483. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  2484. size += sizeof(void *);
  2485. #endif
  2486. /*
  2487. * With that we have determined the number of bytes in actual use
  2488. * by the object. This is the potential offset to the free pointer.
  2489. */
  2490. s->inuse = size;
  2491. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  2492. s->ctor)) {
  2493. /*
  2494. * Relocate free pointer after the object if it is not
  2495. * permitted to overwrite the first word of the object on
  2496. * kmem_cache_free.
  2497. *
  2498. * This is the case if we do RCU, have a constructor or
  2499. * destructor or are poisoning the objects.
  2500. */
  2501. s->offset = size;
  2502. size += sizeof(void *);
  2503. }
  2504. #ifdef CONFIG_SLUB_DEBUG
  2505. if (flags & SLAB_STORE_USER)
  2506. /*
  2507. * Need to store information about allocs and frees after
  2508. * the object.
  2509. */
  2510. size += 2 * sizeof(struct track);
  2511. if (flags & SLAB_RED_ZONE)
  2512. /*
  2513. * Add some empty padding so that we can catch
  2514. * overwrites from earlier objects rather than let
  2515. * tracking information or the free pointer be
  2516. * corrupted if a user writes before the start
  2517. * of the object.
  2518. */
  2519. size += sizeof(void *);
  2520. #endif
  2521. /*
  2522. * Determine the alignment based on various parameters that the
  2523. * user specified and the dynamic determination of cache line size
  2524. * on bootup.
  2525. */
  2526. align = calculate_alignment(flags, align, s->objsize);
  2527. s->align = align;
  2528. /*
  2529. * SLUB stores one object immediately after another beginning from
  2530. * offset 0. In order to align the objects we have to simply size
  2531. * each object to conform to the alignment.
  2532. */
  2533. size = ALIGN(size, align);
  2534. s->size = size;
  2535. if (forced_order >= 0)
  2536. order = forced_order;
  2537. else
  2538. order = calculate_order(size, s->reserved);
  2539. if (order < 0)
  2540. return 0;
  2541. s->allocflags = 0;
  2542. if (order)
  2543. s->allocflags |= __GFP_COMP;
  2544. if (s->flags & SLAB_CACHE_DMA)
  2545. s->allocflags |= SLUB_DMA;
  2546. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  2547. s->allocflags |= __GFP_RECLAIMABLE;
  2548. /*
  2549. * Determine the number of objects per slab
  2550. */
  2551. s->oo = oo_make(order, size, s->reserved);
  2552. s->min = oo_make(get_order(size), size, s->reserved);
  2553. if (oo_objects(s->oo) > oo_objects(s->max))
  2554. s->max = s->oo;
  2555. return !!oo_objects(s->oo);
  2556. }
  2557. static int kmem_cache_open(struct kmem_cache *s,
  2558. const char *name, size_t size,
  2559. size_t align, unsigned long flags,
  2560. void (*ctor)(void *))
  2561. {
  2562. memset(s, 0, kmem_size);
  2563. s->name = name;
  2564. s->ctor = ctor;
  2565. s->objsize = size;
  2566. s->align = align;
  2567. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2568. s->reserved = 0;
  2569. if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
  2570. s->reserved = sizeof(struct rcu_head);
  2571. if (!calculate_sizes(s, -1))
  2572. goto error;
  2573. if (disable_higher_order_debug) {
  2574. /*
  2575. * Disable debugging flags that store metadata if the min slab
  2576. * order increased.
  2577. */
  2578. if (get_order(s->size) > get_order(s->objsize)) {
  2579. s->flags &= ~DEBUG_METADATA_FLAGS;
  2580. s->offset = 0;
  2581. if (!calculate_sizes(s, -1))
  2582. goto error;
  2583. }
  2584. }
  2585. #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && \
  2586. defined(CONFIG_HAVE_ALIGNED_STRUCT_PAGE)
  2587. if (system_has_cmpxchg_double() && (s->flags & SLAB_DEBUG_FLAGS) == 0)
  2588. /* Enable fast mode */
  2589. s->flags |= __CMPXCHG_DOUBLE;
  2590. #endif
  2591. /*
  2592. * The larger the object size is, the more pages we want on the partial
  2593. * list to avoid pounding the page allocator excessively.
  2594. */
  2595. set_min_partial(s, ilog2(s->size) / 2);
  2596. /*
  2597. * cpu_partial determined the maximum number of objects kept in the
  2598. * per cpu partial lists of a processor.
  2599. *
  2600. * Per cpu partial lists mainly contain slabs that just have one
  2601. * object freed. If they are used for allocation then they can be
  2602. * filled up again with minimal effort. The slab will never hit the
  2603. * per node partial lists and therefore no locking will be required.
  2604. *
  2605. * This setting also determines
  2606. *
  2607. * A) The number of objects from per cpu partial slabs dumped to the
  2608. * per node list when we reach the limit.
  2609. * B) The number of objects in cpu partial slabs to extract from the
  2610. * per node list when we run out of per cpu objects. We only fetch 50%
  2611. * to keep some capacity around for frees.
  2612. */
  2613. if (kmem_cache_debug(s))
  2614. s->cpu_partial = 0;
  2615. else if (s->size >= PAGE_SIZE)
  2616. s->cpu_partial = 2;
  2617. else if (s->size >= 1024)
  2618. s->cpu_partial = 6;
  2619. else if (s->size >= 256)
  2620. s->cpu_partial = 13;
  2621. else
  2622. s->cpu_partial = 30;
  2623. s->refcount = 1;
  2624. #ifdef CONFIG_NUMA
  2625. s->remote_node_defrag_ratio = 1000;
  2626. #endif
  2627. if (!init_kmem_cache_nodes(s))
  2628. goto error;
  2629. if (alloc_kmem_cache_cpus(s))
  2630. return 1;
  2631. free_kmem_cache_nodes(s);
  2632. error:
  2633. if (flags & SLAB_PANIC)
  2634. panic("Cannot create slab %s size=%lu realsize=%u "
  2635. "order=%u offset=%u flags=%lx\n",
  2636. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2637. s->offset, flags);
  2638. return 0;
  2639. }
  2640. /*
  2641. * Determine the size of a slab object
  2642. */
  2643. unsigned int kmem_cache_size(struct kmem_cache *s)
  2644. {
  2645. return s->objsize;
  2646. }
  2647. EXPORT_SYMBOL(kmem_cache_size);
  2648. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2649. const char *text)
  2650. {
  2651. #ifdef CONFIG_SLUB_DEBUG
  2652. void *addr = page_address(page);
  2653. void *p;
  2654. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  2655. sizeof(long), GFP_ATOMIC);
  2656. if (!map)
  2657. return;
  2658. slab_err(s, page, "%s", text);
  2659. slab_lock(page);
  2660. get_map(s, page, map);
  2661. for_each_object(p, s, addr, page->objects) {
  2662. if (!test_bit(slab_index(p, s, addr), map)) {
  2663. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2664. p, p - addr);
  2665. print_tracking(s, p);
  2666. }
  2667. }
  2668. slab_unlock(page);
  2669. kfree(map);
  2670. #endif
  2671. }
  2672. /*
  2673. * Attempt to free all partial slabs on a node.
  2674. * This is called from kmem_cache_close(). We must be the last thread
  2675. * using the cache and therefore we do not need to lock anymore.
  2676. */
  2677. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2678. {
  2679. struct page *page, *h;
  2680. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2681. if (!page->inuse) {
  2682. remove_partial(n, page);
  2683. discard_slab(s, page);
  2684. } else {
  2685. list_slab_objects(s, page,
  2686. "Objects remaining on kmem_cache_close()");
  2687. }
  2688. }
  2689. }
  2690. /*
  2691. * Release all resources used by a slab cache.
  2692. */
  2693. static inline int kmem_cache_close(struct kmem_cache *s)
  2694. {
  2695. int node;
  2696. flush_all(s);
  2697. free_percpu(s->cpu_slab);
  2698. /* Attempt to free all objects */
  2699. for_each_node_state(node, N_NORMAL_MEMORY) {
  2700. struct kmem_cache_node *n = get_node(s, node);
  2701. free_partial(s, n);
  2702. if (n->nr_partial || slabs_node(s, node))
  2703. return 1;
  2704. }
  2705. free_kmem_cache_nodes(s);
  2706. return 0;
  2707. }
  2708. /*
  2709. * Close a cache and release the kmem_cache structure
  2710. * (must be used for caches created using kmem_cache_create)
  2711. */
  2712. void kmem_cache_destroy(struct kmem_cache *s)
  2713. {
  2714. down_write(&slub_lock);
  2715. s->refcount--;
  2716. if (!s->refcount) {
  2717. list_del(&s->list);
  2718. up_write(&slub_lock);
  2719. if (kmem_cache_close(s)) {
  2720. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2721. "still has objects.\n", s->name, __func__);
  2722. dump_stack();
  2723. }
  2724. if (s->flags & SLAB_DESTROY_BY_RCU)
  2725. rcu_barrier();
  2726. sysfs_slab_remove(s);
  2727. } else
  2728. up_write(&slub_lock);
  2729. }
  2730. EXPORT_SYMBOL(kmem_cache_destroy);
  2731. /********************************************************************
  2732. * Kmalloc subsystem
  2733. *******************************************************************/
  2734. struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
  2735. EXPORT_SYMBOL(kmalloc_caches);
  2736. static struct kmem_cache *kmem_cache;
  2737. #ifdef CONFIG_ZONE_DMA
  2738. static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
  2739. #endif
  2740. static int __init setup_slub_min_order(char *str)
  2741. {
  2742. get_option(&str, &slub_min_order);
  2743. return 1;
  2744. }
  2745. __setup("slub_min_order=", setup_slub_min_order);
  2746. static int __init setup_slub_max_order(char *str)
  2747. {
  2748. get_option(&str, &slub_max_order);
  2749. slub_max_order = min(slub_max_order, MAX_ORDER - 1);
  2750. return 1;
  2751. }
  2752. __setup("slub_max_order=", setup_slub_max_order);
  2753. static int __init setup_slub_min_objects(char *str)
  2754. {
  2755. get_option(&str, &slub_min_objects);
  2756. return 1;
  2757. }
  2758. __setup("slub_min_objects=", setup_slub_min_objects);
  2759. static int __init setup_slub_nomerge(char *str)
  2760. {
  2761. slub_nomerge = 1;
  2762. return 1;
  2763. }
  2764. __setup("slub_nomerge", setup_slub_nomerge);
  2765. static struct kmem_cache *__init create_kmalloc_cache(const char *name,
  2766. int size, unsigned int flags)
  2767. {
  2768. struct kmem_cache *s;
  2769. s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2770. /*
  2771. * This function is called with IRQs disabled during early-boot on
  2772. * single CPU so there's no need to take slub_lock here.
  2773. */
  2774. if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
  2775. flags, NULL))
  2776. goto panic;
  2777. list_add(&s->list, &slab_caches);
  2778. return s;
  2779. panic:
  2780. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2781. return NULL;
  2782. }
  2783. /*
  2784. * Conversion table for small slabs sizes / 8 to the index in the
  2785. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2786. * of two cache sizes there. The size of larger slabs can be determined using
  2787. * fls.
  2788. */
  2789. static s8 size_index[24] = {
  2790. 3, /* 8 */
  2791. 4, /* 16 */
  2792. 5, /* 24 */
  2793. 5, /* 32 */
  2794. 6, /* 40 */
  2795. 6, /* 48 */
  2796. 6, /* 56 */
  2797. 6, /* 64 */
  2798. 1, /* 72 */
  2799. 1, /* 80 */
  2800. 1, /* 88 */
  2801. 1, /* 96 */
  2802. 7, /* 104 */
  2803. 7, /* 112 */
  2804. 7, /* 120 */
  2805. 7, /* 128 */
  2806. 2, /* 136 */
  2807. 2, /* 144 */
  2808. 2, /* 152 */
  2809. 2, /* 160 */
  2810. 2, /* 168 */
  2811. 2, /* 176 */
  2812. 2, /* 184 */
  2813. 2 /* 192 */
  2814. };
  2815. static inline int size_index_elem(size_t bytes)
  2816. {
  2817. return (bytes - 1) / 8;
  2818. }
  2819. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2820. {
  2821. int index;
  2822. if (size <= 192) {
  2823. if (!size)
  2824. return ZERO_SIZE_PTR;
  2825. index = size_index[size_index_elem(size)];
  2826. } else
  2827. index = fls(size - 1);
  2828. #ifdef CONFIG_ZONE_DMA
  2829. if (unlikely((flags & SLUB_DMA)))
  2830. return kmalloc_dma_caches[index];
  2831. #endif
  2832. return kmalloc_caches[index];
  2833. }
  2834. void *__kmalloc(size_t size, gfp_t flags)
  2835. {
  2836. struct kmem_cache *s;
  2837. void *ret;
  2838. if (unlikely(size > SLUB_MAX_SIZE))
  2839. return kmalloc_large(size, flags);
  2840. s = get_slab(size, flags);
  2841. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2842. return s;
  2843. ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
  2844. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  2845. return ret;
  2846. }
  2847. EXPORT_SYMBOL(__kmalloc);
  2848. #ifdef CONFIG_NUMA
  2849. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2850. {
  2851. struct page *page;
  2852. void *ptr = NULL;
  2853. flags |= __GFP_COMP | __GFP_NOTRACK;
  2854. page = alloc_pages_node(node, flags, get_order(size));
  2855. if (page)
  2856. ptr = page_address(page);
  2857. kmemleak_alloc(ptr, size, 1, flags);
  2858. return ptr;
  2859. }
  2860. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2861. {
  2862. struct kmem_cache *s;
  2863. void *ret;
  2864. if (unlikely(size > SLUB_MAX_SIZE)) {
  2865. ret = kmalloc_large_node(size, flags, node);
  2866. trace_kmalloc_node(_RET_IP_, ret,
  2867. size, PAGE_SIZE << get_order(size),
  2868. flags, node);
  2869. return ret;
  2870. }
  2871. s = get_slab(size, flags);
  2872. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2873. return s;
  2874. ret = slab_alloc(s, flags, node, _RET_IP_);
  2875. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  2876. return ret;
  2877. }
  2878. EXPORT_SYMBOL(__kmalloc_node);
  2879. #endif
  2880. size_t ksize(const void *object)
  2881. {
  2882. struct page *page;
  2883. if (unlikely(object == ZERO_SIZE_PTR))
  2884. return 0;
  2885. page = virt_to_head_page(object);
  2886. if (unlikely(!PageSlab(page))) {
  2887. WARN_ON(!PageCompound(page));
  2888. return PAGE_SIZE << compound_order(page);
  2889. }
  2890. return slab_ksize(page->slab);
  2891. }
  2892. EXPORT_SYMBOL(ksize);
  2893. #ifdef CONFIG_SLUB_DEBUG
  2894. bool verify_mem_not_deleted(const void *x)
  2895. {
  2896. struct page *page;
  2897. void *object = (void *)x;
  2898. unsigned long flags;
  2899. bool rv;
  2900. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2901. return false;
  2902. local_irq_save(flags);
  2903. page = virt_to_head_page(x);
  2904. if (unlikely(!PageSlab(page))) {
  2905. /* maybe it was from stack? */
  2906. rv = true;
  2907. goto out_unlock;
  2908. }
  2909. slab_lock(page);
  2910. if (on_freelist(page->slab, page, object)) {
  2911. object_err(page->slab, page, object, "Object is on free-list");
  2912. rv = false;
  2913. } else {
  2914. rv = true;
  2915. }
  2916. slab_unlock(page);
  2917. out_unlock:
  2918. local_irq_restore(flags);
  2919. return rv;
  2920. }
  2921. EXPORT_SYMBOL(verify_mem_not_deleted);
  2922. #endif
  2923. void kfree(const void *x)
  2924. {
  2925. struct page *page;
  2926. void *object = (void *)x;
  2927. trace_kfree(_RET_IP_, x);
  2928. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2929. return;
  2930. page = virt_to_head_page(x);
  2931. if (unlikely(!PageSlab(page))) {
  2932. BUG_ON(!PageCompound(page));
  2933. kmemleak_free(x);
  2934. put_page(page);
  2935. return;
  2936. }
  2937. slab_free(page->slab, page, object, _RET_IP_);
  2938. }
  2939. EXPORT_SYMBOL(kfree);
  2940. /*
  2941. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2942. * the remaining slabs by the number of items in use. The slabs with the
  2943. * most items in use come first. New allocations will then fill those up
  2944. * and thus they can be removed from the partial lists.
  2945. *
  2946. * The slabs with the least items are placed last. This results in them
  2947. * being allocated from last increasing the chance that the last objects
  2948. * are freed in them.
  2949. */
  2950. int kmem_cache_shrink(struct kmem_cache *s)
  2951. {
  2952. int node;
  2953. int i;
  2954. struct kmem_cache_node *n;
  2955. struct page *page;
  2956. struct page *t;
  2957. int objects = oo_objects(s->max);
  2958. struct list_head *slabs_by_inuse =
  2959. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2960. unsigned long flags;
  2961. if (!slabs_by_inuse)
  2962. return -ENOMEM;
  2963. flush_all(s);
  2964. for_each_node_state(node, N_NORMAL_MEMORY) {
  2965. n = get_node(s, node);
  2966. if (!n->nr_partial)
  2967. continue;
  2968. for (i = 0; i < objects; i++)
  2969. INIT_LIST_HEAD(slabs_by_inuse + i);
  2970. spin_lock_irqsave(&n->list_lock, flags);
  2971. /*
  2972. * Build lists indexed by the items in use in each slab.
  2973. *
  2974. * Note that concurrent frees may occur while we hold the
  2975. * list_lock. page->inuse here is the upper limit.
  2976. */
  2977. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2978. list_move(&page->lru, slabs_by_inuse + page->inuse);
  2979. if (!page->inuse)
  2980. n->nr_partial--;
  2981. }
  2982. /*
  2983. * Rebuild the partial list with the slabs filled up most
  2984. * first and the least used slabs at the end.
  2985. */
  2986. for (i = objects - 1; i > 0; i--)
  2987. list_splice(slabs_by_inuse + i, n->partial.prev);
  2988. spin_unlock_irqrestore(&n->list_lock, flags);
  2989. /* Release empty slabs */
  2990. list_for_each_entry_safe(page, t, slabs_by_inuse, lru)
  2991. discard_slab(s, page);
  2992. }
  2993. kfree(slabs_by_inuse);
  2994. return 0;
  2995. }
  2996. EXPORT_SYMBOL(kmem_cache_shrink);
  2997. #if defined(CONFIG_MEMORY_HOTPLUG)
  2998. static int slab_mem_going_offline_callback(void *arg)
  2999. {
  3000. struct kmem_cache *s;
  3001. down_read(&slub_lock);
  3002. list_for_each_entry(s, &slab_caches, list)
  3003. kmem_cache_shrink(s);
  3004. up_read(&slub_lock);
  3005. return 0;
  3006. }
  3007. static void slab_mem_offline_callback(void *arg)
  3008. {
  3009. struct kmem_cache_node *n;
  3010. struct kmem_cache *s;
  3011. struct memory_notify *marg = arg;
  3012. int offline_node;
  3013. offline_node = marg->status_change_nid;
  3014. /*
  3015. * If the node still has available memory. we need kmem_cache_node
  3016. * for it yet.
  3017. */
  3018. if (offline_node < 0)
  3019. return;
  3020. down_read(&slub_lock);
  3021. list_for_each_entry(s, &slab_caches, list) {
  3022. n = get_node(s, offline_node);
  3023. if (n) {
  3024. /*
  3025. * if n->nr_slabs > 0, slabs still exist on the node
  3026. * that is going down. We were unable to free them,
  3027. * and offline_pages() function shouldn't call this
  3028. * callback. So, we must fail.
  3029. */
  3030. BUG_ON(slabs_node(s, offline_node));
  3031. s->node[offline_node] = NULL;
  3032. kmem_cache_free(kmem_cache_node, n);
  3033. }
  3034. }
  3035. up_read(&slub_lock);
  3036. }
  3037. static int slab_mem_going_online_callback(void *arg)
  3038. {
  3039. struct kmem_cache_node *n;
  3040. struct kmem_cache *s;
  3041. struct memory_notify *marg = arg;
  3042. int nid = marg->status_change_nid;
  3043. int ret = 0;
  3044. /*
  3045. * If the node's memory is already available, then kmem_cache_node is
  3046. * already created. Nothing to do.
  3047. */
  3048. if (nid < 0)
  3049. return 0;
  3050. /*
  3051. * We are bringing a node online. No memory is available yet. We must
  3052. * allocate a kmem_cache_node structure in order to bring the node
  3053. * online.
  3054. */
  3055. down_read(&slub_lock);
  3056. list_for_each_entry(s, &slab_caches, list) {
  3057. /*
  3058. * XXX: kmem_cache_alloc_node will fallback to other nodes
  3059. * since memory is not yet available from the node that
  3060. * is brought up.
  3061. */
  3062. n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
  3063. if (!n) {
  3064. ret = -ENOMEM;
  3065. goto out;
  3066. }
  3067. init_kmem_cache_node(n, s);
  3068. s->node[nid] = n;
  3069. }
  3070. out:
  3071. up_read(&slub_lock);
  3072. return ret;
  3073. }
  3074. static int slab_memory_callback(struct notifier_block *self,
  3075. unsigned long action, void *arg)
  3076. {
  3077. int ret = 0;
  3078. switch (action) {
  3079. case MEM_GOING_ONLINE:
  3080. ret = slab_mem_going_online_callback(arg);
  3081. break;
  3082. case MEM_GOING_OFFLINE:
  3083. ret = slab_mem_going_offline_callback(arg);
  3084. break;
  3085. case MEM_OFFLINE:
  3086. case MEM_CANCEL_ONLINE:
  3087. slab_mem_offline_callback(arg);
  3088. break;
  3089. case MEM_ONLINE:
  3090. case MEM_CANCEL_OFFLINE:
  3091. break;
  3092. }
  3093. if (ret)
  3094. ret = notifier_from_errno(ret);
  3095. else
  3096. ret = NOTIFY_OK;
  3097. return ret;
  3098. }
  3099. #endif /* CONFIG_MEMORY_HOTPLUG */
  3100. /********************************************************************
  3101. * Basic setup of slabs
  3102. *******************************************************************/
  3103. /*
  3104. * Used for early kmem_cache structures that were allocated using
  3105. * the page allocator
  3106. */
  3107. static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
  3108. {
  3109. int node;
  3110. list_add(&s->list, &slab_caches);
  3111. s->refcount = -1;
  3112. for_each_node_state(node, N_NORMAL_MEMORY) {
  3113. struct kmem_cache_node *n = get_node(s, node);
  3114. struct page *p;
  3115. if (n) {
  3116. list_for_each_entry(p, &n->partial, lru)
  3117. p->slab = s;
  3118. #ifdef CONFIG_SLUB_DEBUG
  3119. list_for_each_entry(p, &n->full, lru)
  3120. p->slab = s;
  3121. #endif
  3122. }
  3123. }
  3124. }
  3125. void __init kmem_cache_init(void)
  3126. {
  3127. int i;
  3128. int caches = 0;
  3129. struct kmem_cache *temp_kmem_cache;
  3130. int order;
  3131. struct kmem_cache *temp_kmem_cache_node;
  3132. unsigned long kmalloc_size;
  3133. if (debug_guardpage_minorder())
  3134. slub_max_order = 0;
  3135. kmem_size = offsetof(struct kmem_cache, node) +
  3136. nr_node_ids * sizeof(struct kmem_cache_node *);
  3137. /* Allocate two kmem_caches from the page allocator */
  3138. kmalloc_size = ALIGN(kmem_size, cache_line_size());
  3139. order = get_order(2 * kmalloc_size);
  3140. kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
  3141. /*
  3142. * Must first have the slab cache available for the allocations of the
  3143. * struct kmem_cache_node's. There is special bootstrap code in
  3144. * kmem_cache_open for slab_state == DOWN.
  3145. */
  3146. kmem_cache_node = (void *)kmem_cache + kmalloc_size;
  3147. kmem_cache_open(kmem_cache_node, "kmem_cache_node",
  3148. sizeof(struct kmem_cache_node),
  3149. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  3150. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  3151. /* Able to allocate the per node structures */
  3152. slab_state = PARTIAL;
  3153. temp_kmem_cache = kmem_cache;
  3154. kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
  3155. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  3156. kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  3157. memcpy(kmem_cache, temp_kmem_cache, kmem_size);
  3158. /*
  3159. * Allocate kmem_cache_node properly from the kmem_cache slab.
  3160. * kmem_cache_node is separately allocated so no need to
  3161. * update any list pointers.
  3162. */
  3163. temp_kmem_cache_node = kmem_cache_node;
  3164. kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  3165. memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
  3166. kmem_cache_bootstrap_fixup(kmem_cache_node);
  3167. caches++;
  3168. kmem_cache_bootstrap_fixup(kmem_cache);
  3169. caches++;
  3170. /* Free temporary boot structure */
  3171. free_pages((unsigned long)temp_kmem_cache, order);
  3172. /* Now we can use the kmem_cache to allocate kmalloc slabs */
  3173. /*
  3174. * Patch up the size_index table if we have strange large alignment
  3175. * requirements for the kmalloc array. This is only the case for
  3176. * MIPS it seems. The standard arches will not generate any code here.
  3177. *
  3178. * Largest permitted alignment is 256 bytes due to the way we
  3179. * handle the index determination for the smaller caches.
  3180. *
  3181. * Make sure that nothing crazy happens if someone starts tinkering
  3182. * around with ARCH_KMALLOC_MINALIGN
  3183. */
  3184. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  3185. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  3186. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  3187. int elem = size_index_elem(i);
  3188. if (elem >= ARRAY_SIZE(size_index))
  3189. break;
  3190. size_index[elem] = KMALLOC_SHIFT_LOW;
  3191. }
  3192. if (KMALLOC_MIN_SIZE == 64) {
  3193. /*
  3194. * The 96 byte size cache is not used if the alignment
  3195. * is 64 byte.
  3196. */
  3197. for (i = 64 + 8; i <= 96; i += 8)
  3198. size_index[size_index_elem(i)] = 7;
  3199. } else if (KMALLOC_MIN_SIZE == 128) {
  3200. /*
  3201. * The 192 byte sized cache is not used if the alignment
  3202. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  3203. * instead.
  3204. */
  3205. for (i = 128 + 8; i <= 192; i += 8)
  3206. size_index[size_index_elem(i)] = 8;
  3207. }
  3208. /* Caches that are not of the two-to-the-power-of size */
  3209. if (KMALLOC_MIN_SIZE <= 32) {
  3210. kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
  3211. caches++;
  3212. }
  3213. if (KMALLOC_MIN_SIZE <= 64) {
  3214. kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
  3215. caches++;
  3216. }
  3217. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  3218. kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
  3219. caches++;
  3220. }
  3221. slab_state = UP;
  3222. /* Provide the correct kmalloc names now that the caches are up */
  3223. if (KMALLOC_MIN_SIZE <= 32) {
  3224. kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
  3225. BUG_ON(!kmalloc_caches[1]->name);
  3226. }
  3227. if (KMALLOC_MIN_SIZE <= 64) {
  3228. kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
  3229. BUG_ON(!kmalloc_caches[2]->name);
  3230. }
  3231. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  3232. char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
  3233. BUG_ON(!s);
  3234. kmalloc_caches[i]->name = s;
  3235. }
  3236. #ifdef CONFIG_SMP
  3237. register_cpu_notifier(&slab_notifier);
  3238. #endif
  3239. #ifdef CONFIG_ZONE_DMA
  3240. for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
  3241. struct kmem_cache *s = kmalloc_caches[i];
  3242. if (s && s->size) {
  3243. char *name = kasprintf(GFP_NOWAIT,
  3244. "dma-kmalloc-%d", s->objsize);
  3245. BUG_ON(!name);
  3246. kmalloc_dma_caches[i] = create_kmalloc_cache(name,
  3247. s->objsize, SLAB_CACHE_DMA);
  3248. }
  3249. }
  3250. #endif
  3251. printk(KERN_INFO
  3252. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  3253. " CPUs=%d, Nodes=%d\n",
  3254. caches, cache_line_size(),
  3255. slub_min_order, slub_max_order, slub_min_objects,
  3256. nr_cpu_ids, nr_node_ids);
  3257. }
  3258. void __init kmem_cache_init_late(void)
  3259. {
  3260. }
  3261. /*
  3262. * Find a mergeable slab cache
  3263. */
  3264. static int slab_unmergeable(struct kmem_cache *s)
  3265. {
  3266. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  3267. return 1;
  3268. if (s->ctor)
  3269. return 1;
  3270. /*
  3271. * We may have set a slab to be unmergeable during bootstrap.
  3272. */
  3273. if (s->refcount < 0)
  3274. return 1;
  3275. return 0;
  3276. }
  3277. static struct kmem_cache *find_mergeable(size_t size,
  3278. size_t align, unsigned long flags, const char *name,
  3279. void (*ctor)(void *))
  3280. {
  3281. struct kmem_cache *s;
  3282. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  3283. return NULL;
  3284. if (ctor)
  3285. return NULL;
  3286. size = ALIGN(size, sizeof(void *));
  3287. align = calculate_alignment(flags, align, size);
  3288. size = ALIGN(size, align);
  3289. flags = kmem_cache_flags(size, flags, name, NULL);
  3290. list_for_each_entry(s, &slab_caches, list) {
  3291. if (slab_unmergeable(s))
  3292. continue;
  3293. if (size > s->size)
  3294. continue;
  3295. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  3296. continue;
  3297. /*
  3298. * Check if alignment is compatible.
  3299. * Courtesy of Adrian Drzewiecki
  3300. */
  3301. if ((s->size & ~(align - 1)) != s->size)
  3302. continue;
  3303. if (s->size - size >= sizeof(void *))
  3304. continue;
  3305. return s;
  3306. }
  3307. return NULL;
  3308. }
  3309. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  3310. size_t align, unsigned long flags, void (*ctor)(void *))
  3311. {
  3312. struct kmem_cache *s;
  3313. char *n;
  3314. if (WARN_ON(!name))
  3315. return NULL;
  3316. down_write(&slub_lock);
  3317. s = find_mergeable(size, align, flags, name, ctor);
  3318. if (s) {
  3319. s->refcount++;
  3320. /*
  3321. * Adjust the object sizes so that we clear
  3322. * the complete object on kzalloc.
  3323. */
  3324. s->objsize = max(s->objsize, (int)size);
  3325. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  3326. if (sysfs_slab_alias(s, name)) {
  3327. s->refcount--;
  3328. goto err;
  3329. }
  3330. up_write(&slub_lock);
  3331. return s;
  3332. }
  3333. n = kstrdup(name, GFP_KERNEL);
  3334. if (!n)
  3335. goto err;
  3336. s = kmalloc(kmem_size, GFP_KERNEL);
  3337. if (s) {
  3338. if (kmem_cache_open(s, n,
  3339. size, align, flags, ctor)) {
  3340. list_add(&s->list, &slab_caches);
  3341. up_write(&slub_lock);
  3342. if (sysfs_slab_add(s)) {
  3343. down_write(&slub_lock);
  3344. list_del(&s->list);
  3345. kfree(n);
  3346. kfree(s);
  3347. goto err;
  3348. }
  3349. return s;
  3350. }
  3351. kfree(n);
  3352. kfree(s);
  3353. }
  3354. err:
  3355. up_write(&slub_lock);
  3356. if (flags & SLAB_PANIC)
  3357. panic("Cannot create slabcache %s\n", name);
  3358. else
  3359. s = NULL;
  3360. return s;
  3361. }
  3362. EXPORT_SYMBOL(kmem_cache_create);
  3363. #ifdef CONFIG_SMP
  3364. /*
  3365. * Use the cpu notifier to insure that the cpu slabs are flushed when
  3366. * necessary.
  3367. */
  3368. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  3369. unsigned long action, void *hcpu)
  3370. {
  3371. long cpu = (long)hcpu;
  3372. struct kmem_cache *s;
  3373. unsigned long flags;
  3374. switch (action) {
  3375. case CPU_UP_CANCELED:
  3376. case CPU_UP_CANCELED_FROZEN:
  3377. case CPU_DEAD:
  3378. case CPU_DEAD_FROZEN:
  3379. down_read(&slub_lock);
  3380. list_for_each_entry(s, &slab_caches, list) {
  3381. local_irq_save(flags);
  3382. __flush_cpu_slab(s, cpu);
  3383. local_irq_restore(flags);
  3384. }
  3385. up_read(&slub_lock);
  3386. break;
  3387. default:
  3388. break;
  3389. }
  3390. return NOTIFY_OK;
  3391. }
  3392. static struct notifier_block __cpuinitdata slab_notifier = {
  3393. .notifier_call = slab_cpuup_callback
  3394. };
  3395. #endif
  3396. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  3397. {
  3398. struct kmem_cache *s;
  3399. void *ret;
  3400. if (unlikely(size > SLUB_MAX_SIZE))
  3401. return kmalloc_large(size, gfpflags);
  3402. s = get_slab(size, gfpflags);
  3403. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3404. return s;
  3405. ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
  3406. /* Honor the call site pointer we received. */
  3407. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  3408. return ret;
  3409. }
  3410. #ifdef CONFIG_NUMA
  3411. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  3412. int node, unsigned long caller)
  3413. {
  3414. struct kmem_cache *s;
  3415. void *ret;
  3416. if (unlikely(size > SLUB_MAX_SIZE)) {
  3417. ret = kmalloc_large_node(size, gfpflags, node);
  3418. trace_kmalloc_node(caller, ret,
  3419. size, PAGE_SIZE << get_order(size),
  3420. gfpflags, node);
  3421. return ret;
  3422. }
  3423. s = get_slab(size, gfpflags);
  3424. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3425. return s;
  3426. ret = slab_alloc(s, gfpflags, node, caller);
  3427. /* Honor the call site pointer we received. */
  3428. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  3429. return ret;
  3430. }
  3431. #endif
  3432. #ifdef CONFIG_SYSFS
  3433. static int count_inuse(struct page *page)
  3434. {
  3435. return page->inuse;
  3436. }
  3437. static int count_total(struct page *page)
  3438. {
  3439. return page->objects;
  3440. }
  3441. #endif
  3442. #ifdef CONFIG_SLUB_DEBUG
  3443. static int validate_slab(struct kmem_cache *s, struct page *page,
  3444. unsigned long *map)
  3445. {
  3446. void *p;
  3447. void *addr = page_address(page);
  3448. if (!check_slab(s, page) ||
  3449. !on_freelist(s, page, NULL))
  3450. return 0;
  3451. /* Now we know that a valid freelist exists */
  3452. bitmap_zero(map, page->objects);
  3453. get_map(s, page, map);
  3454. for_each_object(p, s, addr, page->objects) {
  3455. if (test_bit(slab_index(p, s, addr), map))
  3456. if (!check_object(s, page, p, SLUB_RED_INACTIVE))
  3457. return 0;
  3458. }
  3459. for_each_object(p, s, addr, page->objects)
  3460. if (!test_bit(slab_index(p, s, addr), map))
  3461. if (!check_object(s, page, p, SLUB_RED_ACTIVE))
  3462. return 0;
  3463. return 1;
  3464. }
  3465. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  3466. unsigned long *map)
  3467. {
  3468. slab_lock(page);
  3469. validate_slab(s, page, map);
  3470. slab_unlock(page);
  3471. }
  3472. static int validate_slab_node(struct kmem_cache *s,
  3473. struct kmem_cache_node *n, unsigned long *map)
  3474. {
  3475. unsigned long count = 0;
  3476. struct page *page;
  3477. unsigned long flags;
  3478. spin_lock_irqsave(&n->list_lock, flags);
  3479. list_for_each_entry(page, &n->partial, lru) {
  3480. validate_slab_slab(s, page, map);
  3481. count++;
  3482. }
  3483. if (count != n->nr_partial)
  3484. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  3485. "counter=%ld\n", s->name, count, n->nr_partial);
  3486. if (!(s->flags & SLAB_STORE_USER))
  3487. goto out;
  3488. list_for_each_entry(page, &n->full, lru) {
  3489. validate_slab_slab(s, page, map);
  3490. count++;
  3491. }
  3492. if (count != atomic_long_read(&n->nr_slabs))
  3493. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  3494. "counter=%ld\n", s->name, count,
  3495. atomic_long_read(&n->nr_slabs));
  3496. out:
  3497. spin_unlock_irqrestore(&n->list_lock, flags);
  3498. return count;
  3499. }
  3500. static long validate_slab_cache(struct kmem_cache *s)
  3501. {
  3502. int node;
  3503. unsigned long count = 0;
  3504. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3505. sizeof(unsigned long), GFP_KERNEL);
  3506. if (!map)
  3507. return -ENOMEM;
  3508. flush_all(s);
  3509. for_each_node_state(node, N_NORMAL_MEMORY) {
  3510. struct kmem_cache_node *n = get_node(s, node);
  3511. count += validate_slab_node(s, n, map);
  3512. }
  3513. kfree(map);
  3514. return count;
  3515. }
  3516. /*
  3517. * Generate lists of code addresses where slabcache objects are allocated
  3518. * and freed.
  3519. */
  3520. struct location {
  3521. unsigned long count;
  3522. unsigned long addr;
  3523. long long sum_time;
  3524. long min_time;
  3525. long max_time;
  3526. long min_pid;
  3527. long max_pid;
  3528. DECLARE_BITMAP(cpus, NR_CPUS);
  3529. nodemask_t nodes;
  3530. };
  3531. struct loc_track {
  3532. unsigned long max;
  3533. unsigned long count;
  3534. struct location *loc;
  3535. };
  3536. static void free_loc_track(struct loc_track *t)
  3537. {
  3538. if (t->max)
  3539. free_pages((unsigned long)t->loc,
  3540. get_order(sizeof(struct location) * t->max));
  3541. }
  3542. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3543. {
  3544. struct location *l;
  3545. int order;
  3546. order = get_order(sizeof(struct location) * max);
  3547. l = (void *)__get_free_pages(flags, order);
  3548. if (!l)
  3549. return 0;
  3550. if (t->count) {
  3551. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3552. free_loc_track(t);
  3553. }
  3554. t->max = max;
  3555. t->loc = l;
  3556. return 1;
  3557. }
  3558. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3559. const struct track *track)
  3560. {
  3561. long start, end, pos;
  3562. struct location *l;
  3563. unsigned long caddr;
  3564. unsigned long age = jiffies - track->when;
  3565. start = -1;
  3566. end = t->count;
  3567. for ( ; ; ) {
  3568. pos = start + (end - start + 1) / 2;
  3569. /*
  3570. * There is nothing at "end". If we end up there
  3571. * we need to add something to before end.
  3572. */
  3573. if (pos == end)
  3574. break;
  3575. caddr = t->loc[pos].addr;
  3576. if (track->addr == caddr) {
  3577. l = &t->loc[pos];
  3578. l->count++;
  3579. if (track->when) {
  3580. l->sum_time += age;
  3581. if (age < l->min_time)
  3582. l->min_time = age;
  3583. if (age > l->max_time)
  3584. l->max_time = age;
  3585. if (track->pid < l->min_pid)
  3586. l->min_pid = track->pid;
  3587. if (track->pid > l->max_pid)
  3588. l->max_pid = track->pid;
  3589. cpumask_set_cpu(track->cpu,
  3590. to_cpumask(l->cpus));
  3591. }
  3592. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3593. return 1;
  3594. }
  3595. if (track->addr < caddr)
  3596. end = pos;
  3597. else
  3598. start = pos;
  3599. }
  3600. /*
  3601. * Not found. Insert new tracking element.
  3602. */
  3603. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3604. return 0;
  3605. l = t->loc + pos;
  3606. if (pos < t->count)
  3607. memmove(l + 1, l,
  3608. (t->count - pos) * sizeof(struct location));
  3609. t->count++;
  3610. l->count = 1;
  3611. l->addr = track->addr;
  3612. l->sum_time = age;
  3613. l->min_time = age;
  3614. l->max_time = age;
  3615. l->min_pid = track->pid;
  3616. l->max_pid = track->pid;
  3617. cpumask_clear(to_cpumask(l->cpus));
  3618. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3619. nodes_clear(l->nodes);
  3620. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3621. return 1;
  3622. }
  3623. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3624. struct page *page, enum track_item alloc,
  3625. unsigned long *map)
  3626. {
  3627. void *addr = page_address(page);
  3628. void *p;
  3629. bitmap_zero(map, page->objects);
  3630. get_map(s, page, map);
  3631. for_each_object(p, s, addr, page->objects)
  3632. if (!test_bit(slab_index(p, s, addr), map))
  3633. add_location(t, s, get_track(s, p, alloc));
  3634. }
  3635. static int list_locations(struct kmem_cache *s, char *buf,
  3636. enum track_item alloc)
  3637. {
  3638. int len = 0;
  3639. unsigned long i;
  3640. struct loc_track t = { 0, 0, NULL };
  3641. int node;
  3642. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3643. sizeof(unsigned long), GFP_KERNEL);
  3644. if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3645. GFP_TEMPORARY)) {
  3646. kfree(map);
  3647. return sprintf(buf, "Out of memory\n");
  3648. }
  3649. /* Push back cpu slabs */
  3650. flush_all(s);
  3651. for_each_node_state(node, N_NORMAL_MEMORY) {
  3652. struct kmem_cache_node *n = get_node(s, node);
  3653. unsigned long flags;
  3654. struct page *page;
  3655. if (!atomic_long_read(&n->nr_slabs))
  3656. continue;
  3657. spin_lock_irqsave(&n->list_lock, flags);
  3658. list_for_each_entry(page, &n->partial, lru)
  3659. process_slab(&t, s, page, alloc, map);
  3660. list_for_each_entry(page, &n->full, lru)
  3661. process_slab(&t, s, page, alloc, map);
  3662. spin_unlock_irqrestore(&n->list_lock, flags);
  3663. }
  3664. for (i = 0; i < t.count; i++) {
  3665. struct location *l = &t.loc[i];
  3666. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3667. break;
  3668. len += sprintf(buf + len, "%7ld ", l->count);
  3669. if (l->addr)
  3670. len += sprintf(buf + len, "%pS", (void *)l->addr);
  3671. else
  3672. len += sprintf(buf + len, "<not-available>");
  3673. if (l->sum_time != l->min_time) {
  3674. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3675. l->min_time,
  3676. (long)div_u64(l->sum_time, l->count),
  3677. l->max_time);
  3678. } else
  3679. len += sprintf(buf + len, " age=%ld",
  3680. l->min_time);
  3681. if (l->min_pid != l->max_pid)
  3682. len += sprintf(buf + len, " pid=%ld-%ld",
  3683. l->min_pid, l->max_pid);
  3684. else
  3685. len += sprintf(buf + len, " pid=%ld",
  3686. l->min_pid);
  3687. if (num_online_cpus() > 1 &&
  3688. !cpumask_empty(to_cpumask(l->cpus)) &&
  3689. len < PAGE_SIZE - 60) {
  3690. len += sprintf(buf + len, " cpus=");
  3691. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3692. to_cpumask(l->cpus));
  3693. }
  3694. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3695. len < PAGE_SIZE - 60) {
  3696. len += sprintf(buf + len, " nodes=");
  3697. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3698. l->nodes);
  3699. }
  3700. len += sprintf(buf + len, "\n");
  3701. }
  3702. free_loc_track(&t);
  3703. kfree(map);
  3704. if (!t.count)
  3705. len += sprintf(buf, "No data\n");
  3706. return len;
  3707. }
  3708. #endif
  3709. #ifdef SLUB_RESILIENCY_TEST
  3710. static void resiliency_test(void)
  3711. {
  3712. u8 *p;
  3713. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
  3714. printk(KERN_ERR "SLUB resiliency testing\n");
  3715. printk(KERN_ERR "-----------------------\n");
  3716. printk(KERN_ERR "A. Corruption after allocation\n");
  3717. p = kzalloc(16, GFP_KERNEL);
  3718. p[16] = 0x12;
  3719. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  3720. " 0x12->0x%p\n\n", p + 16);
  3721. validate_slab_cache(kmalloc_caches[4]);
  3722. /* Hmmm... The next two are dangerous */
  3723. p = kzalloc(32, GFP_KERNEL);
  3724. p[32 + sizeof(void *)] = 0x34;
  3725. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  3726. " 0x34 -> -0x%p\n", p);
  3727. printk(KERN_ERR
  3728. "If allocated object is overwritten then not detectable\n\n");
  3729. validate_slab_cache(kmalloc_caches[5]);
  3730. p = kzalloc(64, GFP_KERNEL);
  3731. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3732. *p = 0x56;
  3733. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3734. p);
  3735. printk(KERN_ERR
  3736. "If allocated object is overwritten then not detectable\n\n");
  3737. validate_slab_cache(kmalloc_caches[6]);
  3738. printk(KERN_ERR "\nB. Corruption after free\n");
  3739. p = kzalloc(128, GFP_KERNEL);
  3740. kfree(p);
  3741. *p = 0x78;
  3742. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3743. validate_slab_cache(kmalloc_caches[7]);
  3744. p = kzalloc(256, GFP_KERNEL);
  3745. kfree(p);
  3746. p[50] = 0x9a;
  3747. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  3748. p);
  3749. validate_slab_cache(kmalloc_caches[8]);
  3750. p = kzalloc(512, GFP_KERNEL);
  3751. kfree(p);
  3752. p[512] = 0xab;
  3753. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3754. validate_slab_cache(kmalloc_caches[9]);
  3755. }
  3756. #else
  3757. #ifdef CONFIG_SYSFS
  3758. static void resiliency_test(void) {};
  3759. #endif
  3760. #endif
  3761. #ifdef CONFIG_SYSFS
  3762. enum slab_stat_type {
  3763. SL_ALL, /* All slabs */
  3764. SL_PARTIAL, /* Only partially allocated slabs */
  3765. SL_CPU, /* Only slabs used for cpu caches */
  3766. SL_OBJECTS, /* Determine allocated objects not slabs */
  3767. SL_TOTAL /* Determine object capacity not slabs */
  3768. };
  3769. #define SO_ALL (1 << SL_ALL)
  3770. #define SO_PARTIAL (1 << SL_PARTIAL)
  3771. #define SO_CPU (1 << SL_CPU)
  3772. #define SO_OBJECTS (1 << SL_OBJECTS)
  3773. #define SO_TOTAL (1 << SL_TOTAL)
  3774. static ssize_t show_slab_objects(struct kmem_cache *s,
  3775. char *buf, unsigned long flags)
  3776. {
  3777. unsigned long total = 0;
  3778. int node;
  3779. int x;
  3780. unsigned long *nodes;
  3781. unsigned long *per_cpu;
  3782. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3783. if (!nodes)
  3784. return -ENOMEM;
  3785. per_cpu = nodes + nr_node_ids;
  3786. if (flags & SO_CPU) {
  3787. int cpu;
  3788. for_each_possible_cpu(cpu) {
  3789. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  3790. int node = ACCESS_ONCE(c->node);
  3791. struct page *page;
  3792. if (node < 0)
  3793. continue;
  3794. page = ACCESS_ONCE(c->page);
  3795. if (page) {
  3796. if (flags & SO_TOTAL)
  3797. x = page->objects;
  3798. else if (flags & SO_OBJECTS)
  3799. x = page->inuse;
  3800. else
  3801. x = 1;
  3802. total += x;
  3803. nodes[node] += x;
  3804. }
  3805. page = c->partial;
  3806. if (page) {
  3807. x = page->pobjects;
  3808. total += x;
  3809. nodes[node] += x;
  3810. }
  3811. per_cpu[node]++;
  3812. }
  3813. }
  3814. lock_memory_hotplug();
  3815. #ifdef CONFIG_SLUB_DEBUG
  3816. if (flags & SO_ALL) {
  3817. for_each_node_state(node, N_NORMAL_MEMORY) {
  3818. struct kmem_cache_node *n = get_node(s, node);
  3819. if (flags & SO_TOTAL)
  3820. x = atomic_long_read(&n->total_objects);
  3821. else if (flags & SO_OBJECTS)
  3822. x = atomic_long_read(&n->total_objects) -
  3823. count_partial(n, count_free);
  3824. else
  3825. x = atomic_long_read(&n->nr_slabs);
  3826. total += x;
  3827. nodes[node] += x;
  3828. }
  3829. } else
  3830. #endif
  3831. if (flags & SO_PARTIAL) {
  3832. for_each_node_state(node, N_NORMAL_MEMORY) {
  3833. struct kmem_cache_node *n = get_node(s, node);
  3834. if (flags & SO_TOTAL)
  3835. x = count_partial(n, count_total);
  3836. else if (flags & SO_OBJECTS)
  3837. x = count_partial(n, count_inuse);
  3838. else
  3839. x = n->nr_partial;
  3840. total += x;
  3841. nodes[node] += x;
  3842. }
  3843. }
  3844. x = sprintf(buf, "%lu", total);
  3845. #ifdef CONFIG_NUMA
  3846. for_each_node_state(node, N_NORMAL_MEMORY)
  3847. if (nodes[node])
  3848. x += sprintf(buf + x, " N%d=%lu",
  3849. node, nodes[node]);
  3850. #endif
  3851. unlock_memory_hotplug();
  3852. kfree(nodes);
  3853. return x + sprintf(buf + x, "\n");
  3854. }
  3855. #ifdef CONFIG_SLUB_DEBUG
  3856. static int any_slab_objects(struct kmem_cache *s)
  3857. {
  3858. int node;
  3859. for_each_online_node(node) {
  3860. struct kmem_cache_node *n = get_node(s, node);
  3861. if (!n)
  3862. continue;
  3863. if (atomic_long_read(&n->total_objects))
  3864. return 1;
  3865. }
  3866. return 0;
  3867. }
  3868. #endif
  3869. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3870. #define to_slab(n) container_of(n, struct kmem_cache, kobj)
  3871. struct slab_attribute {
  3872. struct attribute attr;
  3873. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3874. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3875. };
  3876. #define SLAB_ATTR_RO(_name) \
  3877. static struct slab_attribute _name##_attr = \
  3878. __ATTR(_name, 0400, _name##_show, NULL)
  3879. #define SLAB_ATTR(_name) \
  3880. static struct slab_attribute _name##_attr = \
  3881. __ATTR(_name, 0600, _name##_show, _name##_store)
  3882. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3883. {
  3884. return sprintf(buf, "%d\n", s->size);
  3885. }
  3886. SLAB_ATTR_RO(slab_size);
  3887. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3888. {
  3889. return sprintf(buf, "%d\n", s->align);
  3890. }
  3891. SLAB_ATTR_RO(align);
  3892. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3893. {
  3894. return sprintf(buf, "%d\n", s->objsize);
  3895. }
  3896. SLAB_ATTR_RO(object_size);
  3897. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3898. {
  3899. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3900. }
  3901. SLAB_ATTR_RO(objs_per_slab);
  3902. static ssize_t order_store(struct kmem_cache *s,
  3903. const char *buf, size_t length)
  3904. {
  3905. unsigned long order;
  3906. int err;
  3907. err = strict_strtoul(buf, 10, &order);
  3908. if (err)
  3909. return err;
  3910. if (order > slub_max_order || order < slub_min_order)
  3911. return -EINVAL;
  3912. calculate_sizes(s, order);
  3913. return length;
  3914. }
  3915. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3916. {
  3917. return sprintf(buf, "%d\n", oo_order(s->oo));
  3918. }
  3919. SLAB_ATTR(order);
  3920. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  3921. {
  3922. return sprintf(buf, "%lu\n", s->min_partial);
  3923. }
  3924. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  3925. size_t length)
  3926. {
  3927. unsigned long min;
  3928. int err;
  3929. err = strict_strtoul(buf, 10, &min);
  3930. if (err)
  3931. return err;
  3932. set_min_partial(s, min);
  3933. return length;
  3934. }
  3935. SLAB_ATTR(min_partial);
  3936. static ssize_t cpu_partial_show(struct kmem_cache *s, char *buf)
  3937. {
  3938. return sprintf(buf, "%u\n", s->cpu_partial);
  3939. }
  3940. static ssize_t cpu_partial_store(struct kmem_cache *s, const char *buf,
  3941. size_t length)
  3942. {
  3943. unsigned long objects;
  3944. int err;
  3945. err = strict_strtoul(buf, 10, &objects);
  3946. if (err)
  3947. return err;
  3948. if (objects && kmem_cache_debug(s))
  3949. return -EINVAL;
  3950. s->cpu_partial = objects;
  3951. flush_all(s);
  3952. return length;
  3953. }
  3954. SLAB_ATTR(cpu_partial);
  3955. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3956. {
  3957. if (!s->ctor)
  3958. return 0;
  3959. return sprintf(buf, "%pS\n", s->ctor);
  3960. }
  3961. SLAB_ATTR_RO(ctor);
  3962. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3963. {
  3964. return sprintf(buf, "%d\n", s->refcount - 1);
  3965. }
  3966. SLAB_ATTR_RO(aliases);
  3967. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3968. {
  3969. return show_slab_objects(s, buf, SO_PARTIAL);
  3970. }
  3971. SLAB_ATTR_RO(partial);
  3972. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3973. {
  3974. return show_slab_objects(s, buf, SO_CPU);
  3975. }
  3976. SLAB_ATTR_RO(cpu_slabs);
  3977. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3978. {
  3979. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3980. }
  3981. SLAB_ATTR_RO(objects);
  3982. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3983. {
  3984. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3985. }
  3986. SLAB_ATTR_RO(objects_partial);
  3987. static ssize_t slabs_cpu_partial_show(struct kmem_cache *s, char *buf)
  3988. {
  3989. int objects = 0;
  3990. int pages = 0;
  3991. int cpu;
  3992. int len;
  3993. for_each_online_cpu(cpu) {
  3994. struct page *page = per_cpu_ptr(s->cpu_slab, cpu)->partial;
  3995. if (page) {
  3996. pages += page->pages;
  3997. objects += page->pobjects;
  3998. }
  3999. }
  4000. len = sprintf(buf, "%d(%d)", objects, pages);
  4001. #ifdef CONFIG_SMP
  4002. for_each_online_cpu(cpu) {
  4003. struct page *page = per_cpu_ptr(s->cpu_slab, cpu) ->partial;
  4004. if (page && len < PAGE_SIZE - 20)
  4005. len += sprintf(buf + len, " C%d=%d(%d)", cpu,
  4006. page->pobjects, page->pages);
  4007. }
  4008. #endif
  4009. return len + sprintf(buf + len, "\n");
  4010. }
  4011. SLAB_ATTR_RO(slabs_cpu_partial);
  4012. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  4013. {
  4014. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  4015. }
  4016. static ssize_t reclaim_account_store(struct kmem_cache *s,
  4017. const char *buf, size_t length)
  4018. {
  4019. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  4020. if (buf[0] == '1')
  4021. s->flags |= SLAB_RECLAIM_ACCOUNT;
  4022. return length;
  4023. }
  4024. SLAB_ATTR(reclaim_account);
  4025. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  4026. {
  4027. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  4028. }
  4029. SLAB_ATTR_RO(hwcache_align);
  4030. #ifdef CONFIG_ZONE_DMA
  4031. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  4032. {
  4033. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  4034. }
  4035. SLAB_ATTR_RO(cache_dma);
  4036. #endif
  4037. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  4038. {
  4039. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  4040. }
  4041. SLAB_ATTR_RO(destroy_by_rcu);
  4042. static ssize_t reserved_show(struct kmem_cache *s, char *buf)
  4043. {
  4044. return sprintf(buf, "%d\n", s->reserved);
  4045. }
  4046. SLAB_ATTR_RO(reserved);
  4047. #ifdef CONFIG_SLUB_DEBUG
  4048. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  4049. {
  4050. return show_slab_objects(s, buf, SO_ALL);
  4051. }
  4052. SLAB_ATTR_RO(slabs);
  4053. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  4054. {
  4055. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  4056. }
  4057. SLAB_ATTR_RO(total_objects);
  4058. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  4059. {
  4060. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  4061. }
  4062. static ssize_t sanity_checks_store(struct kmem_cache *s,
  4063. const char *buf, size_t length)
  4064. {
  4065. s->flags &= ~SLAB_DEBUG_FREE;
  4066. if (buf[0] == '1') {
  4067. s->flags &= ~__CMPXCHG_DOUBLE;
  4068. s->flags |= SLAB_DEBUG_FREE;
  4069. }
  4070. return length;
  4071. }
  4072. SLAB_ATTR(sanity_checks);
  4073. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  4074. {
  4075. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  4076. }
  4077. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  4078. size_t length)
  4079. {
  4080. s->flags &= ~SLAB_TRACE;
  4081. if (buf[0] == '1') {
  4082. s->flags &= ~__CMPXCHG_DOUBLE;
  4083. s->flags |= SLAB_TRACE;
  4084. }
  4085. return length;
  4086. }
  4087. SLAB_ATTR(trace);
  4088. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  4089. {
  4090. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  4091. }
  4092. static ssize_t red_zone_store(struct kmem_cache *s,
  4093. const char *buf, size_t length)
  4094. {
  4095. if (any_slab_objects(s))
  4096. return -EBUSY;
  4097. s->flags &= ~SLAB_RED_ZONE;
  4098. if (buf[0] == '1') {
  4099. s->flags &= ~__CMPXCHG_DOUBLE;
  4100. s->flags |= SLAB_RED_ZONE;
  4101. }
  4102. calculate_sizes(s, -1);
  4103. return length;
  4104. }
  4105. SLAB_ATTR(red_zone);
  4106. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  4107. {
  4108. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  4109. }
  4110. static ssize_t poison_store(struct kmem_cache *s,
  4111. const char *buf, size_t length)
  4112. {
  4113. if (any_slab_objects(s))
  4114. return -EBUSY;
  4115. s->flags &= ~SLAB_POISON;
  4116. if (buf[0] == '1') {
  4117. s->flags &= ~__CMPXCHG_DOUBLE;
  4118. s->flags |= SLAB_POISON;
  4119. }
  4120. calculate_sizes(s, -1);
  4121. return length;
  4122. }
  4123. SLAB_ATTR(poison);
  4124. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  4125. {
  4126. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  4127. }
  4128. static ssize_t store_user_store(struct kmem_cache *s,
  4129. const char *buf, size_t length)
  4130. {
  4131. if (any_slab_objects(s))
  4132. return -EBUSY;
  4133. s->flags &= ~SLAB_STORE_USER;
  4134. if (buf[0] == '1') {
  4135. s->flags &= ~__CMPXCHG_DOUBLE;
  4136. s->flags |= SLAB_STORE_USER;
  4137. }
  4138. calculate_sizes(s, -1);
  4139. return length;
  4140. }
  4141. SLAB_ATTR(store_user);
  4142. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  4143. {
  4144. return 0;
  4145. }
  4146. static ssize_t validate_store(struct kmem_cache *s,
  4147. const char *buf, size_t length)
  4148. {
  4149. int ret = -EINVAL;
  4150. if (buf[0] == '1') {
  4151. ret = validate_slab_cache(s);
  4152. if (ret >= 0)
  4153. ret = length;
  4154. }
  4155. return ret;
  4156. }
  4157. SLAB_ATTR(validate);
  4158. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  4159. {
  4160. if (!(s->flags & SLAB_STORE_USER))
  4161. return -ENOSYS;
  4162. return list_locations(s, buf, TRACK_ALLOC);
  4163. }
  4164. SLAB_ATTR_RO(alloc_calls);
  4165. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  4166. {
  4167. if (!(s->flags & SLAB_STORE_USER))
  4168. return -ENOSYS;
  4169. return list_locations(s, buf, TRACK_FREE);
  4170. }
  4171. SLAB_ATTR_RO(free_calls);
  4172. #endif /* CONFIG_SLUB_DEBUG */
  4173. #ifdef CONFIG_FAILSLAB
  4174. static ssize_t failslab_show(struct kmem_cache *s, char *buf)
  4175. {
  4176. return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
  4177. }
  4178. static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
  4179. size_t length)
  4180. {
  4181. s->flags &= ~SLAB_FAILSLAB;
  4182. if (buf[0] == '1')
  4183. s->flags |= SLAB_FAILSLAB;
  4184. return length;
  4185. }
  4186. SLAB_ATTR(failslab);
  4187. #endif
  4188. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  4189. {
  4190. return 0;
  4191. }
  4192. static ssize_t shrink_store(struct kmem_cache *s,
  4193. const char *buf, size_t length)
  4194. {
  4195. if (buf[0] == '1') {
  4196. int rc = kmem_cache_shrink(s);
  4197. if (rc)
  4198. return rc;
  4199. } else
  4200. return -EINVAL;
  4201. return length;
  4202. }
  4203. SLAB_ATTR(shrink);
  4204. #ifdef CONFIG_NUMA
  4205. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  4206. {
  4207. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  4208. }
  4209. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  4210. const char *buf, size_t length)
  4211. {
  4212. unsigned long ratio;
  4213. int err;
  4214. err = strict_strtoul(buf, 10, &ratio);
  4215. if (err)
  4216. return err;
  4217. if (ratio <= 100)
  4218. s->remote_node_defrag_ratio = ratio * 10;
  4219. return length;
  4220. }
  4221. SLAB_ATTR(remote_node_defrag_ratio);
  4222. #endif
  4223. #ifdef CONFIG_SLUB_STATS
  4224. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  4225. {
  4226. unsigned long sum = 0;
  4227. int cpu;
  4228. int len;
  4229. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  4230. if (!data)
  4231. return -ENOMEM;
  4232. for_each_online_cpu(cpu) {
  4233. unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
  4234. data[cpu] = x;
  4235. sum += x;
  4236. }
  4237. len = sprintf(buf, "%lu", sum);
  4238. #ifdef CONFIG_SMP
  4239. for_each_online_cpu(cpu) {
  4240. if (data[cpu] && len < PAGE_SIZE - 20)
  4241. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  4242. }
  4243. #endif
  4244. kfree(data);
  4245. return len + sprintf(buf + len, "\n");
  4246. }
  4247. static void clear_stat(struct kmem_cache *s, enum stat_item si)
  4248. {
  4249. int cpu;
  4250. for_each_online_cpu(cpu)
  4251. per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
  4252. }
  4253. #define STAT_ATTR(si, text) \
  4254. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  4255. { \
  4256. return show_stat(s, buf, si); \
  4257. } \
  4258. static ssize_t text##_store(struct kmem_cache *s, \
  4259. const char *buf, size_t length) \
  4260. { \
  4261. if (buf[0] != '0') \
  4262. return -EINVAL; \
  4263. clear_stat(s, si); \
  4264. return length; \
  4265. } \
  4266. SLAB_ATTR(text); \
  4267. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  4268. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  4269. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  4270. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  4271. STAT_ATTR(FREE_FROZEN, free_frozen);
  4272. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  4273. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  4274. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  4275. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  4276. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  4277. STAT_ATTR(ALLOC_NODE_MISMATCH, alloc_node_mismatch);
  4278. STAT_ATTR(FREE_SLAB, free_slab);
  4279. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  4280. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  4281. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  4282. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  4283. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  4284. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  4285. STAT_ATTR(DEACTIVATE_BYPASS, deactivate_bypass);
  4286. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  4287. STAT_ATTR(CMPXCHG_DOUBLE_CPU_FAIL, cmpxchg_double_cpu_fail);
  4288. STAT_ATTR(CMPXCHG_DOUBLE_FAIL, cmpxchg_double_fail);
  4289. STAT_ATTR(CPU_PARTIAL_ALLOC, cpu_partial_alloc);
  4290. STAT_ATTR(CPU_PARTIAL_FREE, cpu_partial_free);
  4291. STAT_ATTR(CPU_PARTIAL_NODE, cpu_partial_node);
  4292. STAT_ATTR(CPU_PARTIAL_DRAIN, cpu_partial_drain);
  4293. #endif
  4294. static struct attribute *slab_attrs[] = {
  4295. &slab_size_attr.attr,
  4296. &object_size_attr.attr,
  4297. &objs_per_slab_attr.attr,
  4298. &order_attr.attr,
  4299. &min_partial_attr.attr,
  4300. &cpu_partial_attr.attr,
  4301. &objects_attr.attr,
  4302. &objects_partial_attr.attr,
  4303. &partial_attr.attr,
  4304. &cpu_slabs_attr.attr,
  4305. &ctor_attr.attr,
  4306. &aliases_attr.attr,
  4307. &align_attr.attr,
  4308. &hwcache_align_attr.attr,
  4309. &reclaim_account_attr.attr,
  4310. &destroy_by_rcu_attr.attr,
  4311. &shrink_attr.attr,
  4312. &reserved_attr.attr,
  4313. &slabs_cpu_partial_attr.attr,
  4314. #ifdef CONFIG_SLUB_DEBUG
  4315. &total_objects_attr.attr,
  4316. &slabs_attr.attr,
  4317. &sanity_checks_attr.attr,
  4318. &trace_attr.attr,
  4319. &red_zone_attr.attr,
  4320. &poison_attr.attr,
  4321. &store_user_attr.attr,
  4322. &validate_attr.attr,
  4323. &alloc_calls_attr.attr,
  4324. &free_calls_attr.attr,
  4325. #endif
  4326. #ifdef CONFIG_ZONE_DMA
  4327. &cache_dma_attr.attr,
  4328. #endif
  4329. #ifdef CONFIG_NUMA
  4330. &remote_node_defrag_ratio_attr.attr,
  4331. #endif
  4332. #ifdef CONFIG_SLUB_STATS
  4333. &alloc_fastpath_attr.attr,
  4334. &alloc_slowpath_attr.attr,
  4335. &free_fastpath_attr.attr,
  4336. &free_slowpath_attr.attr,
  4337. &free_frozen_attr.attr,
  4338. &free_add_partial_attr.attr,
  4339. &free_remove_partial_attr.attr,
  4340. &alloc_from_partial_attr.attr,
  4341. &alloc_slab_attr.attr,
  4342. &alloc_refill_attr.attr,
  4343. &alloc_node_mismatch_attr.attr,
  4344. &free_slab_attr.attr,
  4345. &cpuslab_flush_attr.attr,
  4346. &deactivate_full_attr.attr,
  4347. &deactivate_empty_attr.attr,
  4348. &deactivate_to_head_attr.attr,
  4349. &deactivate_to_tail_attr.attr,
  4350. &deactivate_remote_frees_attr.attr,
  4351. &deactivate_bypass_attr.attr,
  4352. &order_fallback_attr.attr,
  4353. &cmpxchg_double_fail_attr.attr,
  4354. &cmpxchg_double_cpu_fail_attr.attr,
  4355. &cpu_partial_alloc_attr.attr,
  4356. &cpu_partial_free_attr.attr,
  4357. &cpu_partial_node_attr.attr,
  4358. &cpu_partial_drain_attr.attr,
  4359. #endif
  4360. #ifdef CONFIG_FAILSLAB
  4361. &failslab_attr.attr,
  4362. #endif
  4363. NULL
  4364. };
  4365. static struct attribute_group slab_attr_group = {
  4366. .attrs = slab_attrs,
  4367. };
  4368. static ssize_t slab_attr_show(struct kobject *kobj,
  4369. struct attribute *attr,
  4370. char *buf)
  4371. {
  4372. struct slab_attribute *attribute;
  4373. struct kmem_cache *s;
  4374. int err;
  4375. attribute = to_slab_attr(attr);
  4376. s = to_slab(kobj);
  4377. if (!attribute->show)
  4378. return -EIO;
  4379. err = attribute->show(s, buf);
  4380. return err;
  4381. }
  4382. static ssize_t slab_attr_store(struct kobject *kobj,
  4383. struct attribute *attr,
  4384. const char *buf, size_t len)
  4385. {
  4386. struct slab_attribute *attribute;
  4387. struct kmem_cache *s;
  4388. int err;
  4389. attribute = to_slab_attr(attr);
  4390. s = to_slab(kobj);
  4391. if (!attribute->store)
  4392. return -EIO;
  4393. err = attribute->store(s, buf, len);
  4394. return err;
  4395. }
  4396. static void kmem_cache_release(struct kobject *kobj)
  4397. {
  4398. struct kmem_cache *s = to_slab(kobj);
  4399. kfree(s->name);
  4400. kfree(s);
  4401. }
  4402. static const struct sysfs_ops slab_sysfs_ops = {
  4403. .show = slab_attr_show,
  4404. .store = slab_attr_store,
  4405. };
  4406. static struct kobj_type slab_ktype = {
  4407. .sysfs_ops = &slab_sysfs_ops,
  4408. .release = kmem_cache_release
  4409. };
  4410. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  4411. {
  4412. struct kobj_type *ktype = get_ktype(kobj);
  4413. if (ktype == &slab_ktype)
  4414. return 1;
  4415. return 0;
  4416. }
  4417. static const struct kset_uevent_ops slab_uevent_ops = {
  4418. .filter = uevent_filter,
  4419. };
  4420. static struct kset *slab_kset;
  4421. #define ID_STR_LENGTH 64
  4422. /* Create a unique string id for a slab cache:
  4423. *
  4424. * Format :[flags-]size
  4425. */
  4426. static char *create_unique_id(struct kmem_cache *s)
  4427. {
  4428. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  4429. char *p = name;
  4430. BUG_ON(!name);
  4431. *p++ = ':';
  4432. /*
  4433. * First flags affecting slabcache operations. We will only
  4434. * get here for aliasable slabs so we do not need to support
  4435. * too many flags. The flags here must cover all flags that
  4436. * are matched during merging to guarantee that the id is
  4437. * unique.
  4438. */
  4439. if (s->flags & SLAB_CACHE_DMA)
  4440. *p++ = 'd';
  4441. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  4442. *p++ = 'a';
  4443. if (s->flags & SLAB_DEBUG_FREE)
  4444. *p++ = 'F';
  4445. if (!(s->flags & SLAB_NOTRACK))
  4446. *p++ = 't';
  4447. if (p != name + 1)
  4448. *p++ = '-';
  4449. p += sprintf(p, "%07d", s->size);
  4450. BUG_ON(p > name + ID_STR_LENGTH - 1);
  4451. return name;
  4452. }
  4453. static int sysfs_slab_add(struct kmem_cache *s)
  4454. {
  4455. int err;
  4456. const char *name;
  4457. int unmergeable;
  4458. if (slab_state < SYSFS)
  4459. /* Defer until later */
  4460. return 0;
  4461. unmergeable = slab_unmergeable(s);
  4462. if (unmergeable) {
  4463. /*
  4464. * Slabcache can never be merged so we can use the name proper.
  4465. * This is typically the case for debug situations. In that
  4466. * case we can catch duplicate names easily.
  4467. */
  4468. sysfs_remove_link(&slab_kset->kobj, s->name);
  4469. name = s->name;
  4470. } else {
  4471. /*
  4472. * Create a unique name for the slab as a target
  4473. * for the symlinks.
  4474. */
  4475. name = create_unique_id(s);
  4476. }
  4477. s->kobj.kset = slab_kset;
  4478. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  4479. if (err) {
  4480. kobject_put(&s->kobj);
  4481. return err;
  4482. }
  4483. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  4484. if (err) {
  4485. kobject_del(&s->kobj);
  4486. kobject_put(&s->kobj);
  4487. return err;
  4488. }
  4489. kobject_uevent(&s->kobj, KOBJ_ADD);
  4490. if (!unmergeable) {
  4491. /* Setup first alias */
  4492. sysfs_slab_alias(s, s->name);
  4493. kfree(name);
  4494. }
  4495. return 0;
  4496. }
  4497. static void sysfs_slab_remove(struct kmem_cache *s)
  4498. {
  4499. if (slab_state < SYSFS)
  4500. /*
  4501. * Sysfs has not been setup yet so no need to remove the
  4502. * cache from sysfs.
  4503. */
  4504. return;
  4505. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  4506. kobject_del(&s->kobj);
  4507. kobject_put(&s->kobj);
  4508. }
  4509. /*
  4510. * Need to buffer aliases during bootup until sysfs becomes
  4511. * available lest we lose that information.
  4512. */
  4513. struct saved_alias {
  4514. struct kmem_cache *s;
  4515. const char *name;
  4516. struct saved_alias *next;
  4517. };
  4518. static struct saved_alias *alias_list;
  4519. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  4520. {
  4521. struct saved_alias *al;
  4522. if (slab_state == SYSFS) {
  4523. /*
  4524. * If we have a leftover link then remove it.
  4525. */
  4526. sysfs_remove_link(&slab_kset->kobj, name);
  4527. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  4528. }
  4529. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  4530. if (!al)
  4531. return -ENOMEM;
  4532. al->s = s;
  4533. al->name = name;
  4534. al->next = alias_list;
  4535. alias_list = al;
  4536. return 0;
  4537. }
  4538. static int __init slab_sysfs_init(void)
  4539. {
  4540. struct kmem_cache *s;
  4541. int err;
  4542. down_write(&slub_lock);
  4543. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  4544. if (!slab_kset) {
  4545. up_write(&slub_lock);
  4546. printk(KERN_ERR "Cannot register slab subsystem.\n");
  4547. return -ENOSYS;
  4548. }
  4549. slab_state = SYSFS;
  4550. list_for_each_entry(s, &slab_caches, list) {
  4551. err = sysfs_slab_add(s);
  4552. if (err)
  4553. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  4554. " to sysfs\n", s->name);
  4555. }
  4556. while (alias_list) {
  4557. struct saved_alias *al = alias_list;
  4558. alias_list = alias_list->next;
  4559. err = sysfs_slab_alias(al->s, al->name);
  4560. if (err)
  4561. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  4562. " %s to sysfs\n", s->name);
  4563. kfree(al);
  4564. }
  4565. up_write(&slub_lock);
  4566. resiliency_test();
  4567. return 0;
  4568. }
  4569. __initcall(slab_sysfs_init);
  4570. #endif /* CONFIG_SYSFS */
  4571. /*
  4572. * The /proc/slabinfo ABI
  4573. */
  4574. #ifdef CONFIG_SLABINFO
  4575. static void print_slabinfo_header(struct seq_file *m)
  4576. {
  4577. seq_puts(m, "slabinfo - version: 2.1\n");
  4578. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  4579. "<objperslab> <pagesperslab>");
  4580. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  4581. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  4582. seq_putc(m, '\n');
  4583. }
  4584. static void *s_start(struct seq_file *m, loff_t *pos)
  4585. {
  4586. loff_t n = *pos;
  4587. down_read(&slub_lock);
  4588. if (!n)
  4589. print_slabinfo_header(m);
  4590. return seq_list_start(&slab_caches, *pos);
  4591. }
  4592. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  4593. {
  4594. return seq_list_next(p, &slab_caches, pos);
  4595. }
  4596. static void s_stop(struct seq_file *m, void *p)
  4597. {
  4598. up_read(&slub_lock);
  4599. }
  4600. static int s_show(struct seq_file *m, void *p)
  4601. {
  4602. unsigned long nr_partials = 0;
  4603. unsigned long nr_slabs = 0;
  4604. unsigned long nr_inuse = 0;
  4605. unsigned long nr_objs = 0;
  4606. unsigned long nr_free = 0;
  4607. struct kmem_cache *s;
  4608. int node;
  4609. s = list_entry(p, struct kmem_cache, list);
  4610. for_each_online_node(node) {
  4611. struct kmem_cache_node *n = get_node(s, node);
  4612. if (!n)
  4613. continue;
  4614. nr_partials += n->nr_partial;
  4615. nr_slabs += atomic_long_read(&n->nr_slabs);
  4616. nr_objs += atomic_long_read(&n->total_objects);
  4617. nr_free += count_partial(n, count_free);
  4618. }
  4619. nr_inuse = nr_objs - nr_free;
  4620. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  4621. nr_objs, s->size, oo_objects(s->oo),
  4622. (1 << oo_order(s->oo)));
  4623. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  4624. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  4625. 0UL);
  4626. seq_putc(m, '\n');
  4627. return 0;
  4628. }
  4629. static const struct seq_operations slabinfo_op = {
  4630. .start = s_start,
  4631. .next = s_next,
  4632. .stop = s_stop,
  4633. .show = s_show,
  4634. };
  4635. static int slabinfo_open(struct inode *inode, struct file *file)
  4636. {
  4637. return seq_open(file, &slabinfo_op);
  4638. }
  4639. static const struct file_operations proc_slabinfo_operations = {
  4640. .open = slabinfo_open,
  4641. .read = seq_read,
  4642. .llseek = seq_lseek,
  4643. .release = seq_release,
  4644. };
  4645. static int __init slab_proc_init(void)
  4646. {
  4647. proc_create("slabinfo", S_IRUSR, NULL, &proc_slabinfo_operations);
  4648. return 0;
  4649. }
  4650. module_init(slab_proc_init);
  4651. #endif /* CONFIG_SLABINFO */