slub.c 141 KB

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