slub.c 138 KB

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