slub.c 116 KB

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