slub.c 133 KB

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