slub.c 119 KB

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