slub.c 139 KB

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