slub.c 126 KB

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