slub.c 134 KB

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