slub.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909
  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 no one 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. /* must check again c->freelist in case of cpu migration or IRQ */
  1553. object = c->freelist;
  1554. if (object)
  1555. goto update_freelist;
  1556. stat(s, ALLOC_REFILL);
  1557. load_freelist:
  1558. object = page->freelist;
  1559. if (unlikely(!object))
  1560. goto another_slab;
  1561. if (kmem_cache_debug(s))
  1562. goto debug;
  1563. update_freelist:
  1564. c->freelist = get_freepointer(s, object);
  1565. page->inuse = page->objects;
  1566. page->freelist = NULL;
  1567. slab_unlock(page);
  1568. c->tid = next_tid(c->tid);
  1569. local_irq_restore(flags);
  1570. stat(s, ALLOC_SLOWPATH);
  1571. return object;
  1572. another_slab:
  1573. deactivate_slab(s, c);
  1574. new_slab:
  1575. page = get_partial(s, gfpflags, node);
  1576. if (page) {
  1577. stat(s, ALLOC_FROM_PARTIAL);
  1578. c->node = page_to_nid(page);
  1579. c->page = page;
  1580. goto load_freelist;
  1581. }
  1582. gfpflags &= gfp_allowed_mask;
  1583. if (gfpflags & __GFP_WAIT)
  1584. local_irq_enable();
  1585. page = new_slab(s, gfpflags, node);
  1586. if (gfpflags & __GFP_WAIT)
  1587. local_irq_disable();
  1588. if (page) {
  1589. c = __this_cpu_ptr(s->cpu_slab);
  1590. stat(s, ALLOC_SLAB);
  1591. if (c->page)
  1592. flush_slab(s, c);
  1593. slab_lock(page);
  1594. __SetPageSlubFrozen(page);
  1595. c->node = page_to_nid(page);
  1596. c->page = page;
  1597. goto load_freelist;
  1598. }
  1599. if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
  1600. slab_out_of_memory(s, gfpflags, node);
  1601. local_irq_restore(flags);
  1602. return NULL;
  1603. debug:
  1604. if (!alloc_debug_processing(s, page, object, addr))
  1605. goto another_slab;
  1606. page->inuse++;
  1607. page->freelist = get_freepointer(s, object);
  1608. deactivate_slab(s, c);
  1609. c->page = NULL;
  1610. c->node = NUMA_NO_NODE;
  1611. local_irq_restore(flags);
  1612. return object;
  1613. }
  1614. /*
  1615. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1616. * have the fastpath folded into their functions. So no function call
  1617. * overhead for requests that can be satisfied on the fastpath.
  1618. *
  1619. * The fastpath works by first checking if the lockless freelist can be used.
  1620. * If not then __slab_alloc is called for slow processing.
  1621. *
  1622. * Otherwise we can simply pick the next object from the lockless free list.
  1623. */
  1624. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1625. gfp_t gfpflags, int node, unsigned long addr)
  1626. {
  1627. void **object;
  1628. struct kmem_cache_cpu *c;
  1629. unsigned long tid;
  1630. if (slab_pre_alloc_hook(s, gfpflags))
  1631. return NULL;
  1632. redo:
  1633. /*
  1634. * Must read kmem_cache cpu data via this cpu ptr. Preemption is
  1635. * enabled. We may switch back and forth between cpus while
  1636. * reading from one cpu area. That does not matter as long
  1637. * as we end up on the original cpu again when doing the cmpxchg.
  1638. */
  1639. c = __this_cpu_ptr(s->cpu_slab);
  1640. /*
  1641. * The transaction ids are globally unique per cpu and per operation on
  1642. * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
  1643. * occurs on the right processor and that there was no operation on the
  1644. * linked list in between.
  1645. */
  1646. tid = c->tid;
  1647. barrier();
  1648. object = c->freelist;
  1649. if (unlikely(!object || !node_match(c, node)))
  1650. object = __slab_alloc(s, gfpflags, node, addr, c);
  1651. else {
  1652. /*
  1653. * The cmpxchg will only match if there was no additional
  1654. * operation and if we are on the right processor.
  1655. *
  1656. * The cmpxchg does the following atomically (without lock semantics!)
  1657. * 1. Relocate first pointer to the current per cpu area.
  1658. * 2. Verify that tid and freelist have not been changed
  1659. * 3. If they were not changed replace tid and freelist
  1660. *
  1661. * Since this is without lock semantics the protection is only against
  1662. * code executing on this cpu *not* from access by other cpus.
  1663. */
  1664. if (unlikely(!irqsafe_cpu_cmpxchg_double(
  1665. s->cpu_slab->freelist, s->cpu_slab->tid,
  1666. object, tid,
  1667. get_freepointer_safe(s, object), next_tid(tid)))) {
  1668. note_cmpxchg_failure("slab_alloc", s, tid);
  1669. goto redo;
  1670. }
  1671. stat(s, ALLOC_FASTPATH);
  1672. }
  1673. if (unlikely(gfpflags & __GFP_ZERO) && object)
  1674. memset(object, 0, s->objsize);
  1675. slab_post_alloc_hook(s, gfpflags, object);
  1676. return object;
  1677. }
  1678. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1679. {
  1680. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1681. trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
  1682. return ret;
  1683. }
  1684. EXPORT_SYMBOL(kmem_cache_alloc);
  1685. #ifdef CONFIG_TRACING
  1686. void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  1687. {
  1688. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1689. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
  1690. return ret;
  1691. }
  1692. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  1693. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  1694. {
  1695. void *ret = kmalloc_order(size, flags, order);
  1696. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  1697. return ret;
  1698. }
  1699. EXPORT_SYMBOL(kmalloc_order_trace);
  1700. #endif
  1701. #ifdef CONFIG_NUMA
  1702. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1703. {
  1704. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1705. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  1706. s->objsize, s->size, gfpflags, node);
  1707. return ret;
  1708. }
  1709. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1710. #ifdef CONFIG_TRACING
  1711. void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  1712. gfp_t gfpflags,
  1713. int node, size_t size)
  1714. {
  1715. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1716. trace_kmalloc_node(_RET_IP_, ret,
  1717. size, s->size, gfpflags, node);
  1718. return ret;
  1719. }
  1720. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  1721. #endif
  1722. #endif
  1723. /*
  1724. * Slow patch handling. This may still be called frequently since objects
  1725. * have a longer lifetime than the cpu slabs in most processing loads.
  1726. *
  1727. * So we still attempt to reduce cache line usage. Just take the slab
  1728. * lock and free the item. If there is no additional partial page
  1729. * handling required then we can return immediately.
  1730. */
  1731. static void __slab_free(struct kmem_cache *s, struct page *page,
  1732. void *x, unsigned long addr)
  1733. {
  1734. void *prior;
  1735. void **object = (void *)x;
  1736. unsigned long flags;
  1737. local_irq_save(flags);
  1738. slab_lock(page);
  1739. stat(s, FREE_SLOWPATH);
  1740. if (kmem_cache_debug(s) && !free_debug_processing(s, page, x, addr))
  1741. goto out_unlock;
  1742. prior = page->freelist;
  1743. set_freepointer(s, object, prior);
  1744. page->freelist = object;
  1745. page->inuse--;
  1746. if (unlikely(PageSlubFrozen(page))) {
  1747. stat(s, FREE_FROZEN);
  1748. goto out_unlock;
  1749. }
  1750. if (unlikely(!page->inuse))
  1751. goto slab_empty;
  1752. /*
  1753. * Objects left in the slab. If it was not on the partial list before
  1754. * then add it.
  1755. */
  1756. if (unlikely(!prior)) {
  1757. add_partial(get_node(s, page_to_nid(page)), page, 1);
  1758. stat(s, FREE_ADD_PARTIAL);
  1759. }
  1760. out_unlock:
  1761. slab_unlock(page);
  1762. local_irq_restore(flags);
  1763. return;
  1764. slab_empty:
  1765. if (prior) {
  1766. /*
  1767. * Slab still on the partial list.
  1768. */
  1769. remove_partial(s, page);
  1770. stat(s, FREE_REMOVE_PARTIAL);
  1771. }
  1772. slab_unlock(page);
  1773. local_irq_restore(flags);
  1774. stat(s, FREE_SLAB);
  1775. discard_slab(s, page);
  1776. }
  1777. /*
  1778. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  1779. * can perform fastpath freeing without additional function calls.
  1780. *
  1781. * The fastpath is only possible if we are freeing to the current cpu slab
  1782. * of this processor. This typically the case if we have just allocated
  1783. * the item before.
  1784. *
  1785. * If fastpath is not possible then fall back to __slab_free where we deal
  1786. * with all sorts of special processing.
  1787. */
  1788. static __always_inline void slab_free(struct kmem_cache *s,
  1789. struct page *page, void *x, unsigned long addr)
  1790. {
  1791. void **object = (void *)x;
  1792. struct kmem_cache_cpu *c;
  1793. unsigned long tid;
  1794. slab_free_hook(s, x);
  1795. redo:
  1796. /*
  1797. * Determine the currently cpus per cpu slab.
  1798. * The cpu may change afterward. However that does not matter since
  1799. * data is retrieved via this pointer. If we are on the same cpu
  1800. * during the cmpxchg then the free will succedd.
  1801. */
  1802. c = __this_cpu_ptr(s->cpu_slab);
  1803. tid = c->tid;
  1804. barrier();
  1805. if (likely(page == c->page)) {
  1806. set_freepointer(s, object, c->freelist);
  1807. if (unlikely(!irqsafe_cpu_cmpxchg_double(
  1808. s->cpu_slab->freelist, s->cpu_slab->tid,
  1809. c->freelist, tid,
  1810. object, next_tid(tid)))) {
  1811. note_cmpxchg_failure("slab_free", s, tid);
  1812. goto redo;
  1813. }
  1814. stat(s, FREE_FASTPATH);
  1815. } else
  1816. __slab_free(s, page, x, addr);
  1817. }
  1818. void kmem_cache_free(struct kmem_cache *s, void *x)
  1819. {
  1820. struct page *page;
  1821. page = virt_to_head_page(x);
  1822. slab_free(s, page, x, _RET_IP_);
  1823. trace_kmem_cache_free(_RET_IP_, x);
  1824. }
  1825. EXPORT_SYMBOL(kmem_cache_free);
  1826. /*
  1827. * Object placement in a slab is made very easy because we always start at
  1828. * offset 0. If we tune the size of the object to the alignment then we can
  1829. * get the required alignment by putting one properly sized object after
  1830. * another.
  1831. *
  1832. * Notice that the allocation order determines the sizes of the per cpu
  1833. * caches. Each processor has always one slab available for allocations.
  1834. * Increasing the allocation order reduces the number of times that slabs
  1835. * must be moved on and off the partial lists and is therefore a factor in
  1836. * locking overhead.
  1837. */
  1838. /*
  1839. * Mininum / Maximum order of slab pages. This influences locking overhead
  1840. * and slab fragmentation. A higher order reduces the number of partial slabs
  1841. * and increases the number of allocations possible without having to
  1842. * take the list_lock.
  1843. */
  1844. static int slub_min_order;
  1845. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  1846. static int slub_min_objects;
  1847. /*
  1848. * Merge control. If this is set then no merging of slab caches will occur.
  1849. * (Could be removed. This was introduced to pacify the merge skeptics.)
  1850. */
  1851. static int slub_nomerge;
  1852. /*
  1853. * Calculate the order of allocation given an slab object size.
  1854. *
  1855. * The order of allocation has significant impact on performance and other
  1856. * system components. Generally order 0 allocations should be preferred since
  1857. * order 0 does not cause fragmentation in the page allocator. Larger objects
  1858. * be problematic to put into order 0 slabs because there may be too much
  1859. * unused space left. We go to a higher order if more than 1/16th of the slab
  1860. * would be wasted.
  1861. *
  1862. * In order to reach satisfactory performance we must ensure that a minimum
  1863. * number of objects is in one slab. Otherwise we may generate too much
  1864. * activity on the partial lists which requires taking the list_lock. This is
  1865. * less a concern for large slabs though which are rarely used.
  1866. *
  1867. * slub_max_order specifies the order where we begin to stop considering the
  1868. * number of objects in a slab as critical. If we reach slub_max_order then
  1869. * we try to keep the page order as low as possible. So we accept more waste
  1870. * of space in favor of a small page order.
  1871. *
  1872. * Higher order allocations also allow the placement of more objects in a
  1873. * slab and thereby reduce object handling overhead. If the user has
  1874. * requested a higher mininum order then we start with that one instead of
  1875. * the smallest order which will fit the object.
  1876. */
  1877. static inline int slab_order(int size, int min_objects,
  1878. int max_order, int fract_leftover, int reserved)
  1879. {
  1880. int order;
  1881. int rem;
  1882. int min_order = slub_min_order;
  1883. if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
  1884. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  1885. for (order = max(min_order,
  1886. fls(min_objects * size - 1) - PAGE_SHIFT);
  1887. order <= max_order; order++) {
  1888. unsigned long slab_size = PAGE_SIZE << order;
  1889. if (slab_size < min_objects * size + reserved)
  1890. continue;
  1891. rem = (slab_size - reserved) % size;
  1892. if (rem <= slab_size / fract_leftover)
  1893. break;
  1894. }
  1895. return order;
  1896. }
  1897. static inline int calculate_order(int size, int reserved)
  1898. {
  1899. int order;
  1900. int min_objects;
  1901. int fraction;
  1902. int max_objects;
  1903. /*
  1904. * Attempt to find best configuration for a slab. This
  1905. * works by first attempting to generate a layout with
  1906. * the best configuration and backing off gradually.
  1907. *
  1908. * First we reduce the acceptable waste in a slab. Then
  1909. * we reduce the minimum objects required in a slab.
  1910. */
  1911. min_objects = slub_min_objects;
  1912. if (!min_objects)
  1913. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  1914. max_objects = order_objects(slub_max_order, size, reserved);
  1915. min_objects = min(min_objects, max_objects);
  1916. while (min_objects > 1) {
  1917. fraction = 16;
  1918. while (fraction >= 4) {
  1919. order = slab_order(size, min_objects,
  1920. slub_max_order, fraction, reserved);
  1921. if (order <= slub_max_order)
  1922. return order;
  1923. fraction /= 2;
  1924. }
  1925. min_objects--;
  1926. }
  1927. /*
  1928. * We were unable to place multiple objects in a slab. Now
  1929. * lets see if we can place a single object there.
  1930. */
  1931. order = slab_order(size, 1, slub_max_order, 1, reserved);
  1932. if (order <= slub_max_order)
  1933. return order;
  1934. /*
  1935. * Doh this slab cannot be placed using slub_max_order.
  1936. */
  1937. order = slab_order(size, 1, MAX_ORDER, 1, reserved);
  1938. if (order < MAX_ORDER)
  1939. return order;
  1940. return -ENOSYS;
  1941. }
  1942. /*
  1943. * Figure out what the alignment of the objects will be.
  1944. */
  1945. static unsigned long calculate_alignment(unsigned long flags,
  1946. unsigned long align, unsigned long size)
  1947. {
  1948. /*
  1949. * If the user wants hardware cache aligned objects then follow that
  1950. * suggestion if the object is sufficiently large.
  1951. *
  1952. * The hardware cache alignment cannot override the specified
  1953. * alignment though. If that is greater then use it.
  1954. */
  1955. if (flags & SLAB_HWCACHE_ALIGN) {
  1956. unsigned long ralign = cache_line_size();
  1957. while (size <= ralign / 2)
  1958. ralign /= 2;
  1959. align = max(align, ralign);
  1960. }
  1961. if (align < ARCH_SLAB_MINALIGN)
  1962. align = ARCH_SLAB_MINALIGN;
  1963. return ALIGN(align, sizeof(void *));
  1964. }
  1965. static void
  1966. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  1967. {
  1968. n->nr_partial = 0;
  1969. spin_lock_init(&n->list_lock);
  1970. INIT_LIST_HEAD(&n->partial);
  1971. #ifdef CONFIG_SLUB_DEBUG
  1972. atomic_long_set(&n->nr_slabs, 0);
  1973. atomic_long_set(&n->total_objects, 0);
  1974. INIT_LIST_HEAD(&n->full);
  1975. #endif
  1976. }
  1977. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
  1978. {
  1979. BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
  1980. SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
  1981. /*
  1982. * Must align to double word boundary for the double cmpxchg
  1983. * instructions to work; see __pcpu_double_call_return_bool().
  1984. */
  1985. s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu),
  1986. 2 * sizeof(void *));
  1987. if (!s->cpu_slab)
  1988. return 0;
  1989. init_kmem_cache_cpus(s);
  1990. return 1;
  1991. }
  1992. static struct kmem_cache *kmem_cache_node;
  1993. /*
  1994. * No kmalloc_node yet so do it by hand. We know that this is the first
  1995. * slab on the node for this slabcache. There are no concurrent accesses
  1996. * possible.
  1997. *
  1998. * Note that this function only works on the kmalloc_node_cache
  1999. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  2000. * memory on a fresh node that has no slab structures yet.
  2001. */
  2002. static void early_kmem_cache_node_alloc(int node)
  2003. {
  2004. struct page *page;
  2005. struct kmem_cache_node *n;
  2006. unsigned long flags;
  2007. BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
  2008. page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
  2009. BUG_ON(!page);
  2010. if (page_to_nid(page) != node) {
  2011. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  2012. "node %d\n", node);
  2013. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  2014. "in order to be able to continue\n");
  2015. }
  2016. n = page->freelist;
  2017. BUG_ON(!n);
  2018. page->freelist = get_freepointer(kmem_cache_node, n);
  2019. page->inuse++;
  2020. kmem_cache_node->node[node] = n;
  2021. #ifdef CONFIG_SLUB_DEBUG
  2022. init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
  2023. init_tracking(kmem_cache_node, n);
  2024. #endif
  2025. init_kmem_cache_node(n, kmem_cache_node);
  2026. inc_slabs_node(kmem_cache_node, node, page->objects);
  2027. /*
  2028. * lockdep requires consistent irq usage for each lock
  2029. * so even though there cannot be a race this early in
  2030. * the boot sequence, we still disable irqs.
  2031. */
  2032. local_irq_save(flags);
  2033. add_partial(n, page, 0);
  2034. local_irq_restore(flags);
  2035. }
  2036. static void free_kmem_cache_nodes(struct kmem_cache *s)
  2037. {
  2038. int node;
  2039. for_each_node_state(node, N_NORMAL_MEMORY) {
  2040. struct kmem_cache_node *n = s->node[node];
  2041. if (n)
  2042. kmem_cache_free(kmem_cache_node, n);
  2043. s->node[node] = NULL;
  2044. }
  2045. }
  2046. static int init_kmem_cache_nodes(struct kmem_cache *s)
  2047. {
  2048. int node;
  2049. for_each_node_state(node, N_NORMAL_MEMORY) {
  2050. struct kmem_cache_node *n;
  2051. if (slab_state == DOWN) {
  2052. early_kmem_cache_node_alloc(node);
  2053. continue;
  2054. }
  2055. n = kmem_cache_alloc_node(kmem_cache_node,
  2056. GFP_KERNEL, node);
  2057. if (!n) {
  2058. free_kmem_cache_nodes(s);
  2059. return 0;
  2060. }
  2061. s->node[node] = n;
  2062. init_kmem_cache_node(n, s);
  2063. }
  2064. return 1;
  2065. }
  2066. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  2067. {
  2068. if (min < MIN_PARTIAL)
  2069. min = MIN_PARTIAL;
  2070. else if (min > MAX_PARTIAL)
  2071. min = MAX_PARTIAL;
  2072. s->min_partial = min;
  2073. }
  2074. /*
  2075. * calculate_sizes() determines the order and the distribution of data within
  2076. * a slab object.
  2077. */
  2078. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  2079. {
  2080. unsigned long flags = s->flags;
  2081. unsigned long size = s->objsize;
  2082. unsigned long align = s->align;
  2083. int order;
  2084. /*
  2085. * Round up object size to the next word boundary. We can only
  2086. * place the free pointer at word boundaries and this determines
  2087. * the possible location of the free pointer.
  2088. */
  2089. size = ALIGN(size, sizeof(void *));
  2090. #ifdef CONFIG_SLUB_DEBUG
  2091. /*
  2092. * Determine if we can poison the object itself. If the user of
  2093. * the slab may touch the object after free or before allocation
  2094. * then we should never poison the object itself.
  2095. */
  2096. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  2097. !s->ctor)
  2098. s->flags |= __OBJECT_POISON;
  2099. else
  2100. s->flags &= ~__OBJECT_POISON;
  2101. /*
  2102. * If we are Redzoning then check if there is some space between the
  2103. * end of the object and the free pointer. If not then add an
  2104. * additional word to have some bytes to store Redzone information.
  2105. */
  2106. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  2107. size += sizeof(void *);
  2108. #endif
  2109. /*
  2110. * With that we have determined the number of bytes in actual use
  2111. * by the object. This is the potential offset to the free pointer.
  2112. */
  2113. s->inuse = size;
  2114. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  2115. s->ctor)) {
  2116. /*
  2117. * Relocate free pointer after the object if it is not
  2118. * permitted to overwrite the first word of the object on
  2119. * kmem_cache_free.
  2120. *
  2121. * This is the case if we do RCU, have a constructor or
  2122. * destructor or are poisoning the objects.
  2123. */
  2124. s->offset = size;
  2125. size += sizeof(void *);
  2126. }
  2127. #ifdef CONFIG_SLUB_DEBUG
  2128. if (flags & SLAB_STORE_USER)
  2129. /*
  2130. * Need to store information about allocs and frees after
  2131. * the object.
  2132. */
  2133. size += 2 * sizeof(struct track);
  2134. if (flags & SLAB_RED_ZONE)
  2135. /*
  2136. * Add some empty padding so that we can catch
  2137. * overwrites from earlier objects rather than let
  2138. * tracking information or the free pointer be
  2139. * corrupted if a user writes before the start
  2140. * of the object.
  2141. */
  2142. size += sizeof(void *);
  2143. #endif
  2144. /*
  2145. * Determine the alignment based on various parameters that the
  2146. * user specified and the dynamic determination of cache line size
  2147. * on bootup.
  2148. */
  2149. align = calculate_alignment(flags, align, s->objsize);
  2150. s->align = align;
  2151. /*
  2152. * SLUB stores one object immediately after another beginning from
  2153. * offset 0. In order to align the objects we have to simply size
  2154. * each object to conform to the alignment.
  2155. */
  2156. size = ALIGN(size, align);
  2157. s->size = size;
  2158. if (forced_order >= 0)
  2159. order = forced_order;
  2160. else
  2161. order = calculate_order(size, s->reserved);
  2162. if (order < 0)
  2163. return 0;
  2164. s->allocflags = 0;
  2165. if (order)
  2166. s->allocflags |= __GFP_COMP;
  2167. if (s->flags & SLAB_CACHE_DMA)
  2168. s->allocflags |= SLUB_DMA;
  2169. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  2170. s->allocflags |= __GFP_RECLAIMABLE;
  2171. /*
  2172. * Determine the number of objects per slab
  2173. */
  2174. s->oo = oo_make(order, size, s->reserved);
  2175. s->min = oo_make(get_order(size), size, s->reserved);
  2176. if (oo_objects(s->oo) > oo_objects(s->max))
  2177. s->max = s->oo;
  2178. return !!oo_objects(s->oo);
  2179. }
  2180. static int kmem_cache_open(struct kmem_cache *s,
  2181. const char *name, size_t size,
  2182. size_t align, unsigned long flags,
  2183. void (*ctor)(void *))
  2184. {
  2185. memset(s, 0, kmem_size);
  2186. s->name = name;
  2187. s->ctor = ctor;
  2188. s->objsize = size;
  2189. s->align = align;
  2190. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2191. s->reserved = 0;
  2192. if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
  2193. s->reserved = sizeof(struct rcu_head);
  2194. if (!calculate_sizes(s, -1))
  2195. goto error;
  2196. if (disable_higher_order_debug) {
  2197. /*
  2198. * Disable debugging flags that store metadata if the min slab
  2199. * order increased.
  2200. */
  2201. if (get_order(s->size) > get_order(s->objsize)) {
  2202. s->flags &= ~DEBUG_METADATA_FLAGS;
  2203. s->offset = 0;
  2204. if (!calculate_sizes(s, -1))
  2205. goto error;
  2206. }
  2207. }
  2208. /*
  2209. * The larger the object size is, the more pages we want on the partial
  2210. * list to avoid pounding the page allocator excessively.
  2211. */
  2212. set_min_partial(s, ilog2(s->size));
  2213. s->refcount = 1;
  2214. #ifdef CONFIG_NUMA
  2215. s->remote_node_defrag_ratio = 1000;
  2216. #endif
  2217. if (!init_kmem_cache_nodes(s))
  2218. goto error;
  2219. if (alloc_kmem_cache_cpus(s))
  2220. return 1;
  2221. free_kmem_cache_nodes(s);
  2222. error:
  2223. if (flags & SLAB_PANIC)
  2224. panic("Cannot create slab %s size=%lu realsize=%u "
  2225. "order=%u offset=%u flags=%lx\n",
  2226. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2227. s->offset, flags);
  2228. return 0;
  2229. }
  2230. /*
  2231. * Determine the size of a slab object
  2232. */
  2233. unsigned int kmem_cache_size(struct kmem_cache *s)
  2234. {
  2235. return s->objsize;
  2236. }
  2237. EXPORT_SYMBOL(kmem_cache_size);
  2238. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2239. const char *text)
  2240. {
  2241. #ifdef CONFIG_SLUB_DEBUG
  2242. void *addr = page_address(page);
  2243. void *p;
  2244. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  2245. sizeof(long), GFP_ATOMIC);
  2246. if (!map)
  2247. return;
  2248. slab_err(s, page, "%s", text);
  2249. slab_lock(page);
  2250. get_map(s, page, map);
  2251. for_each_object(p, s, addr, page->objects) {
  2252. if (!test_bit(slab_index(p, s, addr), map)) {
  2253. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2254. p, p - addr);
  2255. print_tracking(s, p);
  2256. }
  2257. }
  2258. slab_unlock(page);
  2259. kfree(map);
  2260. #endif
  2261. }
  2262. /*
  2263. * Attempt to free all partial slabs on a node.
  2264. */
  2265. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2266. {
  2267. unsigned long flags;
  2268. struct page *page, *h;
  2269. spin_lock_irqsave(&n->list_lock, flags);
  2270. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2271. if (!page->inuse) {
  2272. __remove_partial(n, page);
  2273. discard_slab(s, page);
  2274. } else {
  2275. list_slab_objects(s, page,
  2276. "Objects remaining on kmem_cache_close()");
  2277. }
  2278. }
  2279. spin_unlock_irqrestore(&n->list_lock, flags);
  2280. }
  2281. /*
  2282. * Release all resources used by a slab cache.
  2283. */
  2284. static inline int kmem_cache_close(struct kmem_cache *s)
  2285. {
  2286. int node;
  2287. flush_all(s);
  2288. free_percpu(s->cpu_slab);
  2289. /* Attempt to free all objects */
  2290. for_each_node_state(node, N_NORMAL_MEMORY) {
  2291. struct kmem_cache_node *n = get_node(s, node);
  2292. free_partial(s, n);
  2293. if (n->nr_partial || slabs_node(s, node))
  2294. return 1;
  2295. }
  2296. free_kmem_cache_nodes(s);
  2297. return 0;
  2298. }
  2299. /*
  2300. * Close a cache and release the kmem_cache structure
  2301. * (must be used for caches created using kmem_cache_create)
  2302. */
  2303. void kmem_cache_destroy(struct kmem_cache *s)
  2304. {
  2305. down_write(&slub_lock);
  2306. s->refcount--;
  2307. if (!s->refcount) {
  2308. list_del(&s->list);
  2309. if (kmem_cache_close(s)) {
  2310. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2311. "still has objects.\n", s->name, __func__);
  2312. dump_stack();
  2313. }
  2314. if (s->flags & SLAB_DESTROY_BY_RCU)
  2315. rcu_barrier();
  2316. sysfs_slab_remove(s);
  2317. }
  2318. up_write(&slub_lock);
  2319. }
  2320. EXPORT_SYMBOL(kmem_cache_destroy);
  2321. /********************************************************************
  2322. * Kmalloc subsystem
  2323. *******************************************************************/
  2324. struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
  2325. EXPORT_SYMBOL(kmalloc_caches);
  2326. static struct kmem_cache *kmem_cache;
  2327. #ifdef CONFIG_ZONE_DMA
  2328. static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
  2329. #endif
  2330. static int __init setup_slub_min_order(char *str)
  2331. {
  2332. get_option(&str, &slub_min_order);
  2333. return 1;
  2334. }
  2335. __setup("slub_min_order=", setup_slub_min_order);
  2336. static int __init setup_slub_max_order(char *str)
  2337. {
  2338. get_option(&str, &slub_max_order);
  2339. slub_max_order = min(slub_max_order, MAX_ORDER - 1);
  2340. return 1;
  2341. }
  2342. __setup("slub_max_order=", setup_slub_max_order);
  2343. static int __init setup_slub_min_objects(char *str)
  2344. {
  2345. get_option(&str, &slub_min_objects);
  2346. return 1;
  2347. }
  2348. __setup("slub_min_objects=", setup_slub_min_objects);
  2349. static int __init setup_slub_nomerge(char *str)
  2350. {
  2351. slub_nomerge = 1;
  2352. return 1;
  2353. }
  2354. __setup("slub_nomerge", setup_slub_nomerge);
  2355. static struct kmem_cache *__init create_kmalloc_cache(const char *name,
  2356. int size, unsigned int flags)
  2357. {
  2358. struct kmem_cache *s;
  2359. s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2360. /*
  2361. * This function is called with IRQs disabled during early-boot on
  2362. * single CPU so there's no need to take slub_lock here.
  2363. */
  2364. if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
  2365. flags, NULL))
  2366. goto panic;
  2367. list_add(&s->list, &slab_caches);
  2368. return s;
  2369. panic:
  2370. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2371. return NULL;
  2372. }
  2373. /*
  2374. * Conversion table for small slabs sizes / 8 to the index in the
  2375. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2376. * of two cache sizes there. The size of larger slabs can be determined using
  2377. * fls.
  2378. */
  2379. static s8 size_index[24] = {
  2380. 3, /* 8 */
  2381. 4, /* 16 */
  2382. 5, /* 24 */
  2383. 5, /* 32 */
  2384. 6, /* 40 */
  2385. 6, /* 48 */
  2386. 6, /* 56 */
  2387. 6, /* 64 */
  2388. 1, /* 72 */
  2389. 1, /* 80 */
  2390. 1, /* 88 */
  2391. 1, /* 96 */
  2392. 7, /* 104 */
  2393. 7, /* 112 */
  2394. 7, /* 120 */
  2395. 7, /* 128 */
  2396. 2, /* 136 */
  2397. 2, /* 144 */
  2398. 2, /* 152 */
  2399. 2, /* 160 */
  2400. 2, /* 168 */
  2401. 2, /* 176 */
  2402. 2, /* 184 */
  2403. 2 /* 192 */
  2404. };
  2405. static inline int size_index_elem(size_t bytes)
  2406. {
  2407. return (bytes - 1) / 8;
  2408. }
  2409. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2410. {
  2411. int index;
  2412. if (size <= 192) {
  2413. if (!size)
  2414. return ZERO_SIZE_PTR;
  2415. index = size_index[size_index_elem(size)];
  2416. } else
  2417. index = fls(size - 1);
  2418. #ifdef CONFIG_ZONE_DMA
  2419. if (unlikely((flags & SLUB_DMA)))
  2420. return kmalloc_dma_caches[index];
  2421. #endif
  2422. return kmalloc_caches[index];
  2423. }
  2424. void *__kmalloc(size_t size, gfp_t flags)
  2425. {
  2426. struct kmem_cache *s;
  2427. void *ret;
  2428. if (unlikely(size > SLUB_MAX_SIZE))
  2429. return kmalloc_large(size, flags);
  2430. s = get_slab(size, flags);
  2431. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2432. return s;
  2433. ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
  2434. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  2435. return ret;
  2436. }
  2437. EXPORT_SYMBOL(__kmalloc);
  2438. #ifdef CONFIG_NUMA
  2439. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2440. {
  2441. struct page *page;
  2442. void *ptr = NULL;
  2443. flags |= __GFP_COMP | __GFP_NOTRACK;
  2444. page = alloc_pages_node(node, flags, get_order(size));
  2445. if (page)
  2446. ptr = page_address(page);
  2447. kmemleak_alloc(ptr, size, 1, flags);
  2448. return ptr;
  2449. }
  2450. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2451. {
  2452. struct kmem_cache *s;
  2453. void *ret;
  2454. if (unlikely(size > SLUB_MAX_SIZE)) {
  2455. ret = kmalloc_large_node(size, flags, node);
  2456. trace_kmalloc_node(_RET_IP_, ret,
  2457. size, PAGE_SIZE << get_order(size),
  2458. flags, node);
  2459. return ret;
  2460. }
  2461. s = get_slab(size, flags);
  2462. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2463. return s;
  2464. ret = slab_alloc(s, flags, node, _RET_IP_);
  2465. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  2466. return ret;
  2467. }
  2468. EXPORT_SYMBOL(__kmalloc_node);
  2469. #endif
  2470. size_t ksize(const void *object)
  2471. {
  2472. struct page *page;
  2473. if (unlikely(object == ZERO_SIZE_PTR))
  2474. return 0;
  2475. page = virt_to_head_page(object);
  2476. if (unlikely(!PageSlab(page))) {
  2477. WARN_ON(!PageCompound(page));
  2478. return PAGE_SIZE << compound_order(page);
  2479. }
  2480. return slab_ksize(page->slab);
  2481. }
  2482. EXPORT_SYMBOL(ksize);
  2483. void kfree(const void *x)
  2484. {
  2485. struct page *page;
  2486. void *object = (void *)x;
  2487. trace_kfree(_RET_IP_, x);
  2488. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2489. return;
  2490. page = virt_to_head_page(x);
  2491. if (unlikely(!PageSlab(page))) {
  2492. BUG_ON(!PageCompound(page));
  2493. kmemleak_free(x);
  2494. put_page(page);
  2495. return;
  2496. }
  2497. slab_free(page->slab, page, object, _RET_IP_);
  2498. }
  2499. EXPORT_SYMBOL(kfree);
  2500. /*
  2501. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2502. * the remaining slabs by the number of items in use. The slabs with the
  2503. * most items in use come first. New allocations will then fill those up
  2504. * and thus they can be removed from the partial lists.
  2505. *
  2506. * The slabs with the least items are placed last. This results in them
  2507. * being allocated from last increasing the chance that the last objects
  2508. * are freed in them.
  2509. */
  2510. int kmem_cache_shrink(struct kmem_cache *s)
  2511. {
  2512. int node;
  2513. int i;
  2514. struct kmem_cache_node *n;
  2515. struct page *page;
  2516. struct page *t;
  2517. int objects = oo_objects(s->max);
  2518. struct list_head *slabs_by_inuse =
  2519. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2520. unsigned long flags;
  2521. if (!slabs_by_inuse)
  2522. return -ENOMEM;
  2523. flush_all(s);
  2524. for_each_node_state(node, N_NORMAL_MEMORY) {
  2525. n = get_node(s, node);
  2526. if (!n->nr_partial)
  2527. continue;
  2528. for (i = 0; i < objects; i++)
  2529. INIT_LIST_HEAD(slabs_by_inuse + i);
  2530. spin_lock_irqsave(&n->list_lock, flags);
  2531. /*
  2532. * Build lists indexed by the items in use in each slab.
  2533. *
  2534. * Note that concurrent frees may occur while we hold the
  2535. * list_lock. page->inuse here is the upper limit.
  2536. */
  2537. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2538. if (!page->inuse && slab_trylock(page)) {
  2539. /*
  2540. * Must hold slab lock here because slab_free
  2541. * may have freed the last object and be
  2542. * waiting to release the slab.
  2543. */
  2544. __remove_partial(n, page);
  2545. slab_unlock(page);
  2546. discard_slab(s, page);
  2547. } else {
  2548. list_move(&page->lru,
  2549. slabs_by_inuse + page->inuse);
  2550. }
  2551. }
  2552. /*
  2553. * Rebuild the partial list with the slabs filled up most
  2554. * first and the least used slabs at the end.
  2555. */
  2556. for (i = objects - 1; i >= 0; i--)
  2557. list_splice(slabs_by_inuse + i, n->partial.prev);
  2558. spin_unlock_irqrestore(&n->list_lock, flags);
  2559. }
  2560. kfree(slabs_by_inuse);
  2561. return 0;
  2562. }
  2563. EXPORT_SYMBOL(kmem_cache_shrink);
  2564. #if defined(CONFIG_MEMORY_HOTPLUG)
  2565. static int slab_mem_going_offline_callback(void *arg)
  2566. {
  2567. struct kmem_cache *s;
  2568. down_read(&slub_lock);
  2569. list_for_each_entry(s, &slab_caches, list)
  2570. kmem_cache_shrink(s);
  2571. up_read(&slub_lock);
  2572. return 0;
  2573. }
  2574. static void slab_mem_offline_callback(void *arg)
  2575. {
  2576. struct kmem_cache_node *n;
  2577. struct kmem_cache *s;
  2578. struct memory_notify *marg = arg;
  2579. int offline_node;
  2580. offline_node = marg->status_change_nid;
  2581. /*
  2582. * If the node still has available memory. we need kmem_cache_node
  2583. * for it yet.
  2584. */
  2585. if (offline_node < 0)
  2586. return;
  2587. down_read(&slub_lock);
  2588. list_for_each_entry(s, &slab_caches, list) {
  2589. n = get_node(s, offline_node);
  2590. if (n) {
  2591. /*
  2592. * if n->nr_slabs > 0, slabs still exist on the node
  2593. * that is going down. We were unable to free them,
  2594. * and offline_pages() function shouldn't call this
  2595. * callback. So, we must fail.
  2596. */
  2597. BUG_ON(slabs_node(s, offline_node));
  2598. s->node[offline_node] = NULL;
  2599. kmem_cache_free(kmem_cache_node, n);
  2600. }
  2601. }
  2602. up_read(&slub_lock);
  2603. }
  2604. static int slab_mem_going_online_callback(void *arg)
  2605. {
  2606. struct kmem_cache_node *n;
  2607. struct kmem_cache *s;
  2608. struct memory_notify *marg = arg;
  2609. int nid = marg->status_change_nid;
  2610. int ret = 0;
  2611. /*
  2612. * If the node's memory is already available, then kmem_cache_node is
  2613. * already created. Nothing to do.
  2614. */
  2615. if (nid < 0)
  2616. return 0;
  2617. /*
  2618. * We are bringing a node online. No memory is available yet. We must
  2619. * allocate a kmem_cache_node structure in order to bring the node
  2620. * online.
  2621. */
  2622. down_read(&slub_lock);
  2623. list_for_each_entry(s, &slab_caches, list) {
  2624. /*
  2625. * XXX: kmem_cache_alloc_node will fallback to other nodes
  2626. * since memory is not yet available from the node that
  2627. * is brought up.
  2628. */
  2629. n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
  2630. if (!n) {
  2631. ret = -ENOMEM;
  2632. goto out;
  2633. }
  2634. init_kmem_cache_node(n, s);
  2635. s->node[nid] = n;
  2636. }
  2637. out:
  2638. up_read(&slub_lock);
  2639. return ret;
  2640. }
  2641. static int slab_memory_callback(struct notifier_block *self,
  2642. unsigned long action, void *arg)
  2643. {
  2644. int ret = 0;
  2645. switch (action) {
  2646. case MEM_GOING_ONLINE:
  2647. ret = slab_mem_going_online_callback(arg);
  2648. break;
  2649. case MEM_GOING_OFFLINE:
  2650. ret = slab_mem_going_offline_callback(arg);
  2651. break;
  2652. case MEM_OFFLINE:
  2653. case MEM_CANCEL_ONLINE:
  2654. slab_mem_offline_callback(arg);
  2655. break;
  2656. case MEM_ONLINE:
  2657. case MEM_CANCEL_OFFLINE:
  2658. break;
  2659. }
  2660. if (ret)
  2661. ret = notifier_from_errno(ret);
  2662. else
  2663. ret = NOTIFY_OK;
  2664. return ret;
  2665. }
  2666. #endif /* CONFIG_MEMORY_HOTPLUG */
  2667. /********************************************************************
  2668. * Basic setup of slabs
  2669. *******************************************************************/
  2670. /*
  2671. * Used for early kmem_cache structures that were allocated using
  2672. * the page allocator
  2673. */
  2674. static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
  2675. {
  2676. int node;
  2677. list_add(&s->list, &slab_caches);
  2678. s->refcount = -1;
  2679. for_each_node_state(node, N_NORMAL_MEMORY) {
  2680. struct kmem_cache_node *n = get_node(s, node);
  2681. struct page *p;
  2682. if (n) {
  2683. list_for_each_entry(p, &n->partial, lru)
  2684. p->slab = s;
  2685. #ifdef CONFIG_SLUB_DEBUG
  2686. list_for_each_entry(p, &n->full, lru)
  2687. p->slab = s;
  2688. #endif
  2689. }
  2690. }
  2691. }
  2692. void __init kmem_cache_init(void)
  2693. {
  2694. int i;
  2695. int caches = 0;
  2696. struct kmem_cache *temp_kmem_cache;
  2697. int order;
  2698. struct kmem_cache *temp_kmem_cache_node;
  2699. unsigned long kmalloc_size;
  2700. kmem_size = offsetof(struct kmem_cache, node) +
  2701. nr_node_ids * sizeof(struct kmem_cache_node *);
  2702. /* Allocate two kmem_caches from the page allocator */
  2703. kmalloc_size = ALIGN(kmem_size, cache_line_size());
  2704. order = get_order(2 * kmalloc_size);
  2705. kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
  2706. /*
  2707. * Must first have the slab cache available for the allocations of the
  2708. * struct kmem_cache_node's. There is special bootstrap code in
  2709. * kmem_cache_open for slab_state == DOWN.
  2710. */
  2711. kmem_cache_node = (void *)kmem_cache + kmalloc_size;
  2712. kmem_cache_open(kmem_cache_node, "kmem_cache_node",
  2713. sizeof(struct kmem_cache_node),
  2714. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2715. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  2716. /* Able to allocate the per node structures */
  2717. slab_state = PARTIAL;
  2718. temp_kmem_cache = kmem_cache;
  2719. kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
  2720. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2721. kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2722. memcpy(kmem_cache, temp_kmem_cache, kmem_size);
  2723. /*
  2724. * Allocate kmem_cache_node properly from the kmem_cache slab.
  2725. * kmem_cache_node is separately allocated so no need to
  2726. * update any list pointers.
  2727. */
  2728. temp_kmem_cache_node = kmem_cache_node;
  2729. kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2730. memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
  2731. kmem_cache_bootstrap_fixup(kmem_cache_node);
  2732. caches++;
  2733. kmem_cache_bootstrap_fixup(kmem_cache);
  2734. caches++;
  2735. /* Free temporary boot structure */
  2736. free_pages((unsigned long)temp_kmem_cache, order);
  2737. /* Now we can use the kmem_cache to allocate kmalloc slabs */
  2738. /*
  2739. * Patch up the size_index table if we have strange large alignment
  2740. * requirements for the kmalloc array. This is only the case for
  2741. * MIPS it seems. The standard arches will not generate any code here.
  2742. *
  2743. * Largest permitted alignment is 256 bytes due to the way we
  2744. * handle the index determination for the smaller caches.
  2745. *
  2746. * Make sure that nothing crazy happens if someone starts tinkering
  2747. * around with ARCH_KMALLOC_MINALIGN
  2748. */
  2749. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  2750. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  2751. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  2752. int elem = size_index_elem(i);
  2753. if (elem >= ARRAY_SIZE(size_index))
  2754. break;
  2755. size_index[elem] = KMALLOC_SHIFT_LOW;
  2756. }
  2757. if (KMALLOC_MIN_SIZE == 64) {
  2758. /*
  2759. * The 96 byte size cache is not used if the alignment
  2760. * is 64 byte.
  2761. */
  2762. for (i = 64 + 8; i <= 96; i += 8)
  2763. size_index[size_index_elem(i)] = 7;
  2764. } else if (KMALLOC_MIN_SIZE == 128) {
  2765. /*
  2766. * The 192 byte sized cache is not used if the alignment
  2767. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  2768. * instead.
  2769. */
  2770. for (i = 128 + 8; i <= 192; i += 8)
  2771. size_index[size_index_elem(i)] = 8;
  2772. }
  2773. /* Caches that are not of the two-to-the-power-of size */
  2774. if (KMALLOC_MIN_SIZE <= 32) {
  2775. kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
  2776. caches++;
  2777. }
  2778. if (KMALLOC_MIN_SIZE <= 64) {
  2779. kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
  2780. caches++;
  2781. }
  2782. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  2783. kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
  2784. caches++;
  2785. }
  2786. slab_state = UP;
  2787. /* Provide the correct kmalloc names now that the caches are up */
  2788. if (KMALLOC_MIN_SIZE <= 32) {
  2789. kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
  2790. BUG_ON(!kmalloc_caches[1]->name);
  2791. }
  2792. if (KMALLOC_MIN_SIZE <= 64) {
  2793. kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
  2794. BUG_ON(!kmalloc_caches[2]->name);
  2795. }
  2796. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  2797. char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
  2798. BUG_ON(!s);
  2799. kmalloc_caches[i]->name = s;
  2800. }
  2801. #ifdef CONFIG_SMP
  2802. register_cpu_notifier(&slab_notifier);
  2803. #endif
  2804. #ifdef CONFIG_ZONE_DMA
  2805. for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
  2806. struct kmem_cache *s = kmalloc_caches[i];
  2807. if (s && s->size) {
  2808. char *name = kasprintf(GFP_NOWAIT,
  2809. "dma-kmalloc-%d", s->objsize);
  2810. BUG_ON(!name);
  2811. kmalloc_dma_caches[i] = create_kmalloc_cache(name,
  2812. s->objsize, SLAB_CACHE_DMA);
  2813. }
  2814. }
  2815. #endif
  2816. printk(KERN_INFO
  2817. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2818. " CPUs=%d, Nodes=%d\n",
  2819. caches, cache_line_size(),
  2820. slub_min_order, slub_max_order, slub_min_objects,
  2821. nr_cpu_ids, nr_node_ids);
  2822. }
  2823. void __init kmem_cache_init_late(void)
  2824. {
  2825. }
  2826. /*
  2827. * Find a mergeable slab cache
  2828. */
  2829. static int slab_unmergeable(struct kmem_cache *s)
  2830. {
  2831. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2832. return 1;
  2833. if (s->ctor)
  2834. return 1;
  2835. /*
  2836. * We may have set a slab to be unmergeable during bootstrap.
  2837. */
  2838. if (s->refcount < 0)
  2839. return 1;
  2840. return 0;
  2841. }
  2842. static struct kmem_cache *find_mergeable(size_t size,
  2843. size_t align, unsigned long flags, const char *name,
  2844. void (*ctor)(void *))
  2845. {
  2846. struct kmem_cache *s;
  2847. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  2848. return NULL;
  2849. if (ctor)
  2850. return NULL;
  2851. size = ALIGN(size, sizeof(void *));
  2852. align = calculate_alignment(flags, align, size);
  2853. size = ALIGN(size, align);
  2854. flags = kmem_cache_flags(size, flags, name, NULL);
  2855. list_for_each_entry(s, &slab_caches, list) {
  2856. if (slab_unmergeable(s))
  2857. continue;
  2858. if (size > s->size)
  2859. continue;
  2860. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  2861. continue;
  2862. /*
  2863. * Check if alignment is compatible.
  2864. * Courtesy of Adrian Drzewiecki
  2865. */
  2866. if ((s->size & ~(align - 1)) != s->size)
  2867. continue;
  2868. if (s->size - size >= sizeof(void *))
  2869. continue;
  2870. return s;
  2871. }
  2872. return NULL;
  2873. }
  2874. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  2875. size_t align, unsigned long flags, void (*ctor)(void *))
  2876. {
  2877. struct kmem_cache *s;
  2878. char *n;
  2879. if (WARN_ON(!name))
  2880. return NULL;
  2881. down_write(&slub_lock);
  2882. s = find_mergeable(size, align, flags, name, ctor);
  2883. if (s) {
  2884. s->refcount++;
  2885. /*
  2886. * Adjust the object sizes so that we clear
  2887. * the complete object on kzalloc.
  2888. */
  2889. s->objsize = max(s->objsize, (int)size);
  2890. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  2891. if (sysfs_slab_alias(s, name)) {
  2892. s->refcount--;
  2893. goto err;
  2894. }
  2895. up_write(&slub_lock);
  2896. return s;
  2897. }
  2898. n = kstrdup(name, GFP_KERNEL);
  2899. if (!n)
  2900. goto err;
  2901. s = kmalloc(kmem_size, GFP_KERNEL);
  2902. if (s) {
  2903. if (kmem_cache_open(s, n,
  2904. size, align, flags, ctor)) {
  2905. list_add(&s->list, &slab_caches);
  2906. up_write(&slub_lock);
  2907. if (sysfs_slab_add(s)) {
  2908. down_write(&slub_lock);
  2909. list_del(&s->list);
  2910. kfree(n);
  2911. kfree(s);
  2912. goto err;
  2913. }
  2914. return s;
  2915. }
  2916. kfree(n);
  2917. kfree(s);
  2918. }
  2919. err:
  2920. up_write(&slub_lock);
  2921. if (flags & SLAB_PANIC)
  2922. panic("Cannot create slabcache %s\n", name);
  2923. else
  2924. s = NULL;
  2925. return s;
  2926. }
  2927. EXPORT_SYMBOL(kmem_cache_create);
  2928. #ifdef CONFIG_SMP
  2929. /*
  2930. * Use the cpu notifier to insure that the cpu slabs are flushed when
  2931. * necessary.
  2932. */
  2933. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  2934. unsigned long action, void *hcpu)
  2935. {
  2936. long cpu = (long)hcpu;
  2937. struct kmem_cache *s;
  2938. unsigned long flags;
  2939. switch (action) {
  2940. case CPU_UP_CANCELED:
  2941. case CPU_UP_CANCELED_FROZEN:
  2942. case CPU_DEAD:
  2943. case CPU_DEAD_FROZEN:
  2944. down_read(&slub_lock);
  2945. list_for_each_entry(s, &slab_caches, list) {
  2946. local_irq_save(flags);
  2947. __flush_cpu_slab(s, cpu);
  2948. local_irq_restore(flags);
  2949. }
  2950. up_read(&slub_lock);
  2951. break;
  2952. default:
  2953. break;
  2954. }
  2955. return NOTIFY_OK;
  2956. }
  2957. static struct notifier_block __cpuinitdata slab_notifier = {
  2958. .notifier_call = slab_cpuup_callback
  2959. };
  2960. #endif
  2961. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  2962. {
  2963. struct kmem_cache *s;
  2964. void *ret;
  2965. if (unlikely(size > SLUB_MAX_SIZE))
  2966. return kmalloc_large(size, gfpflags);
  2967. s = get_slab(size, gfpflags);
  2968. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2969. return s;
  2970. ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
  2971. /* Honor the call site pointer we received. */
  2972. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  2973. return ret;
  2974. }
  2975. #ifdef CONFIG_NUMA
  2976. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  2977. int node, unsigned long caller)
  2978. {
  2979. struct kmem_cache *s;
  2980. void *ret;
  2981. if (unlikely(size > SLUB_MAX_SIZE)) {
  2982. ret = kmalloc_large_node(size, gfpflags, node);
  2983. trace_kmalloc_node(caller, ret,
  2984. size, PAGE_SIZE << get_order(size),
  2985. gfpflags, node);
  2986. return ret;
  2987. }
  2988. s = get_slab(size, gfpflags);
  2989. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2990. return s;
  2991. ret = slab_alloc(s, gfpflags, node, caller);
  2992. /* Honor the call site pointer we received. */
  2993. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  2994. return ret;
  2995. }
  2996. #endif
  2997. #ifdef CONFIG_SYSFS
  2998. static int count_inuse(struct page *page)
  2999. {
  3000. return page->inuse;
  3001. }
  3002. static int count_total(struct page *page)
  3003. {
  3004. return page->objects;
  3005. }
  3006. #endif
  3007. #ifdef CONFIG_SLUB_DEBUG
  3008. static int validate_slab(struct kmem_cache *s, struct page *page,
  3009. unsigned long *map)
  3010. {
  3011. void *p;
  3012. void *addr = page_address(page);
  3013. if (!check_slab(s, page) ||
  3014. !on_freelist(s, page, NULL))
  3015. return 0;
  3016. /* Now we know that a valid freelist exists */
  3017. bitmap_zero(map, page->objects);
  3018. get_map(s, page, map);
  3019. for_each_object(p, s, addr, page->objects) {
  3020. if (test_bit(slab_index(p, s, addr), map))
  3021. if (!check_object(s, page, p, SLUB_RED_INACTIVE))
  3022. return 0;
  3023. }
  3024. for_each_object(p, s, addr, page->objects)
  3025. if (!test_bit(slab_index(p, s, addr), map))
  3026. if (!check_object(s, page, p, SLUB_RED_ACTIVE))
  3027. return 0;
  3028. return 1;
  3029. }
  3030. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  3031. unsigned long *map)
  3032. {
  3033. if (slab_trylock(page)) {
  3034. validate_slab(s, page, map);
  3035. slab_unlock(page);
  3036. } else
  3037. printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
  3038. s->name, page);
  3039. }
  3040. static int validate_slab_node(struct kmem_cache *s,
  3041. struct kmem_cache_node *n, unsigned long *map)
  3042. {
  3043. unsigned long count = 0;
  3044. struct page *page;
  3045. unsigned long flags;
  3046. spin_lock_irqsave(&n->list_lock, flags);
  3047. list_for_each_entry(page, &n->partial, lru) {
  3048. validate_slab_slab(s, page, map);
  3049. count++;
  3050. }
  3051. if (count != n->nr_partial)
  3052. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  3053. "counter=%ld\n", s->name, count, n->nr_partial);
  3054. if (!(s->flags & SLAB_STORE_USER))
  3055. goto out;
  3056. list_for_each_entry(page, &n->full, lru) {
  3057. validate_slab_slab(s, page, map);
  3058. count++;
  3059. }
  3060. if (count != atomic_long_read(&n->nr_slabs))
  3061. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  3062. "counter=%ld\n", s->name, count,
  3063. atomic_long_read(&n->nr_slabs));
  3064. out:
  3065. spin_unlock_irqrestore(&n->list_lock, flags);
  3066. return count;
  3067. }
  3068. static long validate_slab_cache(struct kmem_cache *s)
  3069. {
  3070. int node;
  3071. unsigned long count = 0;
  3072. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3073. sizeof(unsigned long), GFP_KERNEL);
  3074. if (!map)
  3075. return -ENOMEM;
  3076. flush_all(s);
  3077. for_each_node_state(node, N_NORMAL_MEMORY) {
  3078. struct kmem_cache_node *n = get_node(s, node);
  3079. count += validate_slab_node(s, n, map);
  3080. }
  3081. kfree(map);
  3082. return count;
  3083. }
  3084. /*
  3085. * Generate lists of code addresses where slabcache objects are allocated
  3086. * and freed.
  3087. */
  3088. struct location {
  3089. unsigned long count;
  3090. unsigned long addr;
  3091. long long sum_time;
  3092. long min_time;
  3093. long max_time;
  3094. long min_pid;
  3095. long max_pid;
  3096. DECLARE_BITMAP(cpus, NR_CPUS);
  3097. nodemask_t nodes;
  3098. };
  3099. struct loc_track {
  3100. unsigned long max;
  3101. unsigned long count;
  3102. struct location *loc;
  3103. };
  3104. static void free_loc_track(struct loc_track *t)
  3105. {
  3106. if (t->max)
  3107. free_pages((unsigned long)t->loc,
  3108. get_order(sizeof(struct location) * t->max));
  3109. }
  3110. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3111. {
  3112. struct location *l;
  3113. int order;
  3114. order = get_order(sizeof(struct location) * max);
  3115. l = (void *)__get_free_pages(flags, order);
  3116. if (!l)
  3117. return 0;
  3118. if (t->count) {
  3119. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3120. free_loc_track(t);
  3121. }
  3122. t->max = max;
  3123. t->loc = l;
  3124. return 1;
  3125. }
  3126. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3127. const struct track *track)
  3128. {
  3129. long start, end, pos;
  3130. struct location *l;
  3131. unsigned long caddr;
  3132. unsigned long age = jiffies - track->when;
  3133. start = -1;
  3134. end = t->count;
  3135. for ( ; ; ) {
  3136. pos = start + (end - start + 1) / 2;
  3137. /*
  3138. * There is nothing at "end". If we end up there
  3139. * we need to add something to before end.
  3140. */
  3141. if (pos == end)
  3142. break;
  3143. caddr = t->loc[pos].addr;
  3144. if (track->addr == caddr) {
  3145. l = &t->loc[pos];
  3146. l->count++;
  3147. if (track->when) {
  3148. l->sum_time += age;
  3149. if (age < l->min_time)
  3150. l->min_time = age;
  3151. if (age > l->max_time)
  3152. l->max_time = age;
  3153. if (track->pid < l->min_pid)
  3154. l->min_pid = track->pid;
  3155. if (track->pid > l->max_pid)
  3156. l->max_pid = track->pid;
  3157. cpumask_set_cpu(track->cpu,
  3158. to_cpumask(l->cpus));
  3159. }
  3160. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3161. return 1;
  3162. }
  3163. if (track->addr < caddr)
  3164. end = pos;
  3165. else
  3166. start = pos;
  3167. }
  3168. /*
  3169. * Not found. Insert new tracking element.
  3170. */
  3171. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3172. return 0;
  3173. l = t->loc + pos;
  3174. if (pos < t->count)
  3175. memmove(l + 1, l,
  3176. (t->count - pos) * sizeof(struct location));
  3177. t->count++;
  3178. l->count = 1;
  3179. l->addr = track->addr;
  3180. l->sum_time = age;
  3181. l->min_time = age;
  3182. l->max_time = age;
  3183. l->min_pid = track->pid;
  3184. l->max_pid = track->pid;
  3185. cpumask_clear(to_cpumask(l->cpus));
  3186. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3187. nodes_clear(l->nodes);
  3188. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3189. return 1;
  3190. }
  3191. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3192. struct page *page, enum track_item alloc,
  3193. unsigned long *map)
  3194. {
  3195. void *addr = page_address(page);
  3196. void *p;
  3197. bitmap_zero(map, page->objects);
  3198. get_map(s, page, map);
  3199. for_each_object(p, s, addr, page->objects)
  3200. if (!test_bit(slab_index(p, s, addr), map))
  3201. add_location(t, s, get_track(s, p, alloc));
  3202. }
  3203. static int list_locations(struct kmem_cache *s, char *buf,
  3204. enum track_item alloc)
  3205. {
  3206. int len = 0;
  3207. unsigned long i;
  3208. struct loc_track t = { 0, 0, NULL };
  3209. int node;
  3210. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3211. sizeof(unsigned long), GFP_KERNEL);
  3212. if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3213. GFP_TEMPORARY)) {
  3214. kfree(map);
  3215. return sprintf(buf, "Out of memory\n");
  3216. }
  3217. /* Push back cpu slabs */
  3218. flush_all(s);
  3219. for_each_node_state(node, N_NORMAL_MEMORY) {
  3220. struct kmem_cache_node *n = get_node(s, node);
  3221. unsigned long flags;
  3222. struct page *page;
  3223. if (!atomic_long_read(&n->nr_slabs))
  3224. continue;
  3225. spin_lock_irqsave(&n->list_lock, flags);
  3226. list_for_each_entry(page, &n->partial, lru)
  3227. process_slab(&t, s, page, alloc, map);
  3228. list_for_each_entry(page, &n->full, lru)
  3229. process_slab(&t, s, page, alloc, map);
  3230. spin_unlock_irqrestore(&n->list_lock, flags);
  3231. }
  3232. for (i = 0; i < t.count; i++) {
  3233. struct location *l = &t.loc[i];
  3234. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3235. break;
  3236. len += sprintf(buf + len, "%7ld ", l->count);
  3237. if (l->addr)
  3238. len += sprintf(buf + len, "%pS", (void *)l->addr);
  3239. else
  3240. len += sprintf(buf + len, "<not-available>");
  3241. if (l->sum_time != l->min_time) {
  3242. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3243. l->min_time,
  3244. (long)div_u64(l->sum_time, l->count),
  3245. l->max_time);
  3246. } else
  3247. len += sprintf(buf + len, " age=%ld",
  3248. l->min_time);
  3249. if (l->min_pid != l->max_pid)
  3250. len += sprintf(buf + len, " pid=%ld-%ld",
  3251. l->min_pid, l->max_pid);
  3252. else
  3253. len += sprintf(buf + len, " pid=%ld",
  3254. l->min_pid);
  3255. if (num_online_cpus() > 1 &&
  3256. !cpumask_empty(to_cpumask(l->cpus)) &&
  3257. len < PAGE_SIZE - 60) {
  3258. len += sprintf(buf + len, " cpus=");
  3259. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3260. to_cpumask(l->cpus));
  3261. }
  3262. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3263. len < PAGE_SIZE - 60) {
  3264. len += sprintf(buf + len, " nodes=");
  3265. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3266. l->nodes);
  3267. }
  3268. len += sprintf(buf + len, "\n");
  3269. }
  3270. free_loc_track(&t);
  3271. kfree(map);
  3272. if (!t.count)
  3273. len += sprintf(buf, "No data\n");
  3274. return len;
  3275. }
  3276. #endif
  3277. #ifdef SLUB_RESILIENCY_TEST
  3278. static void resiliency_test(void)
  3279. {
  3280. u8 *p;
  3281. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
  3282. printk(KERN_ERR "SLUB resiliency testing\n");
  3283. printk(KERN_ERR "-----------------------\n");
  3284. printk(KERN_ERR "A. Corruption after allocation\n");
  3285. p = kzalloc(16, GFP_KERNEL);
  3286. p[16] = 0x12;
  3287. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  3288. " 0x12->0x%p\n\n", p + 16);
  3289. validate_slab_cache(kmalloc_caches[4]);
  3290. /* Hmmm... The next two are dangerous */
  3291. p = kzalloc(32, GFP_KERNEL);
  3292. p[32 + sizeof(void *)] = 0x34;
  3293. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  3294. " 0x34 -> -0x%p\n", p);
  3295. printk(KERN_ERR
  3296. "If allocated object is overwritten then not detectable\n\n");
  3297. validate_slab_cache(kmalloc_caches[5]);
  3298. p = kzalloc(64, GFP_KERNEL);
  3299. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3300. *p = 0x56;
  3301. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3302. p);
  3303. printk(KERN_ERR
  3304. "If allocated object is overwritten then not detectable\n\n");
  3305. validate_slab_cache(kmalloc_caches[6]);
  3306. printk(KERN_ERR "\nB. Corruption after free\n");
  3307. p = kzalloc(128, GFP_KERNEL);
  3308. kfree(p);
  3309. *p = 0x78;
  3310. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3311. validate_slab_cache(kmalloc_caches[7]);
  3312. p = kzalloc(256, GFP_KERNEL);
  3313. kfree(p);
  3314. p[50] = 0x9a;
  3315. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  3316. p);
  3317. validate_slab_cache(kmalloc_caches[8]);
  3318. p = kzalloc(512, GFP_KERNEL);
  3319. kfree(p);
  3320. p[512] = 0xab;
  3321. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3322. validate_slab_cache(kmalloc_caches[9]);
  3323. }
  3324. #else
  3325. #ifdef CONFIG_SYSFS
  3326. static void resiliency_test(void) {};
  3327. #endif
  3328. #endif
  3329. #ifdef CONFIG_SYSFS
  3330. enum slab_stat_type {
  3331. SL_ALL, /* All slabs */
  3332. SL_PARTIAL, /* Only partially allocated slabs */
  3333. SL_CPU, /* Only slabs used for cpu caches */
  3334. SL_OBJECTS, /* Determine allocated objects not slabs */
  3335. SL_TOTAL /* Determine object capacity not slabs */
  3336. };
  3337. #define SO_ALL (1 << SL_ALL)
  3338. #define SO_PARTIAL (1 << SL_PARTIAL)
  3339. #define SO_CPU (1 << SL_CPU)
  3340. #define SO_OBJECTS (1 << SL_OBJECTS)
  3341. #define SO_TOTAL (1 << SL_TOTAL)
  3342. static ssize_t show_slab_objects(struct kmem_cache *s,
  3343. char *buf, unsigned long flags)
  3344. {
  3345. unsigned long total = 0;
  3346. int node;
  3347. int x;
  3348. unsigned long *nodes;
  3349. unsigned long *per_cpu;
  3350. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3351. if (!nodes)
  3352. return -ENOMEM;
  3353. per_cpu = nodes + nr_node_ids;
  3354. if (flags & SO_CPU) {
  3355. int cpu;
  3356. for_each_possible_cpu(cpu) {
  3357. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  3358. if (!c || c->node < 0)
  3359. continue;
  3360. if (c->page) {
  3361. if (flags & SO_TOTAL)
  3362. x = c->page->objects;
  3363. else if (flags & SO_OBJECTS)
  3364. x = c->page->inuse;
  3365. else
  3366. x = 1;
  3367. total += x;
  3368. nodes[c->node] += x;
  3369. }
  3370. per_cpu[c->node]++;
  3371. }
  3372. }
  3373. lock_memory_hotplug();
  3374. #ifdef CONFIG_SLUB_DEBUG
  3375. if (flags & SO_ALL) {
  3376. for_each_node_state(node, N_NORMAL_MEMORY) {
  3377. struct kmem_cache_node *n = get_node(s, node);
  3378. if (flags & SO_TOTAL)
  3379. x = atomic_long_read(&n->total_objects);
  3380. else if (flags & SO_OBJECTS)
  3381. x = atomic_long_read(&n->total_objects) -
  3382. count_partial(n, count_free);
  3383. else
  3384. x = atomic_long_read(&n->nr_slabs);
  3385. total += x;
  3386. nodes[node] += x;
  3387. }
  3388. } else
  3389. #endif
  3390. if (flags & SO_PARTIAL) {
  3391. for_each_node_state(node, N_NORMAL_MEMORY) {
  3392. struct kmem_cache_node *n = get_node(s, node);
  3393. if (flags & SO_TOTAL)
  3394. x = count_partial(n, count_total);
  3395. else if (flags & SO_OBJECTS)
  3396. x = count_partial(n, count_inuse);
  3397. else
  3398. x = n->nr_partial;
  3399. total += x;
  3400. nodes[node] += x;
  3401. }
  3402. }
  3403. x = sprintf(buf, "%lu", total);
  3404. #ifdef CONFIG_NUMA
  3405. for_each_node_state(node, N_NORMAL_MEMORY)
  3406. if (nodes[node])
  3407. x += sprintf(buf + x, " N%d=%lu",
  3408. node, nodes[node]);
  3409. #endif
  3410. unlock_memory_hotplug();
  3411. kfree(nodes);
  3412. return x + sprintf(buf + x, "\n");
  3413. }
  3414. #ifdef CONFIG_SLUB_DEBUG
  3415. static int any_slab_objects(struct kmem_cache *s)
  3416. {
  3417. int node;
  3418. for_each_online_node(node) {
  3419. struct kmem_cache_node *n = get_node(s, node);
  3420. if (!n)
  3421. continue;
  3422. if (atomic_long_read(&n->total_objects))
  3423. return 1;
  3424. }
  3425. return 0;
  3426. }
  3427. #endif
  3428. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3429. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  3430. struct slab_attribute {
  3431. struct attribute attr;
  3432. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3433. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3434. };
  3435. #define SLAB_ATTR_RO(_name) \
  3436. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  3437. #define SLAB_ATTR(_name) \
  3438. static struct slab_attribute _name##_attr = \
  3439. __ATTR(_name, 0644, _name##_show, _name##_store)
  3440. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3441. {
  3442. return sprintf(buf, "%d\n", s->size);
  3443. }
  3444. SLAB_ATTR_RO(slab_size);
  3445. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3446. {
  3447. return sprintf(buf, "%d\n", s->align);
  3448. }
  3449. SLAB_ATTR_RO(align);
  3450. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3451. {
  3452. return sprintf(buf, "%d\n", s->objsize);
  3453. }
  3454. SLAB_ATTR_RO(object_size);
  3455. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3456. {
  3457. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3458. }
  3459. SLAB_ATTR_RO(objs_per_slab);
  3460. static ssize_t order_store(struct kmem_cache *s,
  3461. const char *buf, size_t length)
  3462. {
  3463. unsigned long order;
  3464. int err;
  3465. err = strict_strtoul(buf, 10, &order);
  3466. if (err)
  3467. return err;
  3468. if (order > slub_max_order || order < slub_min_order)
  3469. return -EINVAL;
  3470. calculate_sizes(s, order);
  3471. return length;
  3472. }
  3473. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3474. {
  3475. return sprintf(buf, "%d\n", oo_order(s->oo));
  3476. }
  3477. SLAB_ATTR(order);
  3478. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  3479. {
  3480. return sprintf(buf, "%lu\n", s->min_partial);
  3481. }
  3482. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  3483. size_t length)
  3484. {
  3485. unsigned long min;
  3486. int err;
  3487. err = strict_strtoul(buf, 10, &min);
  3488. if (err)
  3489. return err;
  3490. set_min_partial(s, min);
  3491. return length;
  3492. }
  3493. SLAB_ATTR(min_partial);
  3494. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3495. {
  3496. if (!s->ctor)
  3497. return 0;
  3498. return sprintf(buf, "%pS\n", s->ctor);
  3499. }
  3500. SLAB_ATTR_RO(ctor);
  3501. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3502. {
  3503. return sprintf(buf, "%d\n", s->refcount - 1);
  3504. }
  3505. SLAB_ATTR_RO(aliases);
  3506. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3507. {
  3508. return show_slab_objects(s, buf, SO_PARTIAL);
  3509. }
  3510. SLAB_ATTR_RO(partial);
  3511. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3512. {
  3513. return show_slab_objects(s, buf, SO_CPU);
  3514. }
  3515. SLAB_ATTR_RO(cpu_slabs);
  3516. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3517. {
  3518. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3519. }
  3520. SLAB_ATTR_RO(objects);
  3521. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3522. {
  3523. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3524. }
  3525. SLAB_ATTR_RO(objects_partial);
  3526. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3527. {
  3528. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3529. }
  3530. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3531. const char *buf, size_t length)
  3532. {
  3533. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3534. if (buf[0] == '1')
  3535. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3536. return length;
  3537. }
  3538. SLAB_ATTR(reclaim_account);
  3539. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3540. {
  3541. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3542. }
  3543. SLAB_ATTR_RO(hwcache_align);
  3544. #ifdef CONFIG_ZONE_DMA
  3545. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3546. {
  3547. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3548. }
  3549. SLAB_ATTR_RO(cache_dma);
  3550. #endif
  3551. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3552. {
  3553. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3554. }
  3555. SLAB_ATTR_RO(destroy_by_rcu);
  3556. static ssize_t reserved_show(struct kmem_cache *s, char *buf)
  3557. {
  3558. return sprintf(buf, "%d\n", s->reserved);
  3559. }
  3560. SLAB_ATTR_RO(reserved);
  3561. #ifdef CONFIG_SLUB_DEBUG
  3562. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  3563. {
  3564. return show_slab_objects(s, buf, SO_ALL);
  3565. }
  3566. SLAB_ATTR_RO(slabs);
  3567. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  3568. {
  3569. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  3570. }
  3571. SLAB_ATTR_RO(total_objects);
  3572. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3573. {
  3574. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3575. }
  3576. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3577. const char *buf, size_t length)
  3578. {
  3579. s->flags &= ~SLAB_DEBUG_FREE;
  3580. if (buf[0] == '1')
  3581. s->flags |= SLAB_DEBUG_FREE;
  3582. return length;
  3583. }
  3584. SLAB_ATTR(sanity_checks);
  3585. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3586. {
  3587. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3588. }
  3589. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3590. size_t length)
  3591. {
  3592. s->flags &= ~SLAB_TRACE;
  3593. if (buf[0] == '1')
  3594. s->flags |= SLAB_TRACE;
  3595. return length;
  3596. }
  3597. SLAB_ATTR(trace);
  3598. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3599. {
  3600. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3601. }
  3602. static ssize_t red_zone_store(struct kmem_cache *s,
  3603. const char *buf, size_t length)
  3604. {
  3605. if (any_slab_objects(s))
  3606. return -EBUSY;
  3607. s->flags &= ~SLAB_RED_ZONE;
  3608. if (buf[0] == '1')
  3609. s->flags |= SLAB_RED_ZONE;
  3610. calculate_sizes(s, -1);
  3611. return length;
  3612. }
  3613. SLAB_ATTR(red_zone);
  3614. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3615. {
  3616. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3617. }
  3618. static ssize_t poison_store(struct kmem_cache *s,
  3619. const char *buf, size_t length)
  3620. {
  3621. if (any_slab_objects(s))
  3622. return -EBUSY;
  3623. s->flags &= ~SLAB_POISON;
  3624. if (buf[0] == '1')
  3625. s->flags |= SLAB_POISON;
  3626. calculate_sizes(s, -1);
  3627. return length;
  3628. }
  3629. SLAB_ATTR(poison);
  3630. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3631. {
  3632. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3633. }
  3634. static ssize_t store_user_store(struct kmem_cache *s,
  3635. const char *buf, size_t length)
  3636. {
  3637. if (any_slab_objects(s))
  3638. return -EBUSY;
  3639. s->flags &= ~SLAB_STORE_USER;
  3640. if (buf[0] == '1')
  3641. s->flags |= SLAB_STORE_USER;
  3642. calculate_sizes(s, -1);
  3643. return length;
  3644. }
  3645. SLAB_ATTR(store_user);
  3646. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3647. {
  3648. return 0;
  3649. }
  3650. static ssize_t validate_store(struct kmem_cache *s,
  3651. const char *buf, size_t length)
  3652. {
  3653. int ret = -EINVAL;
  3654. if (buf[0] == '1') {
  3655. ret = validate_slab_cache(s);
  3656. if (ret >= 0)
  3657. ret = length;
  3658. }
  3659. return ret;
  3660. }
  3661. SLAB_ATTR(validate);
  3662. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3663. {
  3664. if (!(s->flags & SLAB_STORE_USER))
  3665. return -ENOSYS;
  3666. return list_locations(s, buf, TRACK_ALLOC);
  3667. }
  3668. SLAB_ATTR_RO(alloc_calls);
  3669. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3670. {
  3671. if (!(s->flags & SLAB_STORE_USER))
  3672. return -ENOSYS;
  3673. return list_locations(s, buf, TRACK_FREE);
  3674. }
  3675. SLAB_ATTR_RO(free_calls);
  3676. #endif /* CONFIG_SLUB_DEBUG */
  3677. #ifdef CONFIG_FAILSLAB
  3678. static ssize_t failslab_show(struct kmem_cache *s, char *buf)
  3679. {
  3680. return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
  3681. }
  3682. static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
  3683. size_t length)
  3684. {
  3685. s->flags &= ~SLAB_FAILSLAB;
  3686. if (buf[0] == '1')
  3687. s->flags |= SLAB_FAILSLAB;
  3688. return length;
  3689. }
  3690. SLAB_ATTR(failslab);
  3691. #endif
  3692. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3693. {
  3694. return 0;
  3695. }
  3696. static ssize_t shrink_store(struct kmem_cache *s,
  3697. const char *buf, size_t length)
  3698. {
  3699. if (buf[0] == '1') {
  3700. int rc = kmem_cache_shrink(s);
  3701. if (rc)
  3702. return rc;
  3703. } else
  3704. return -EINVAL;
  3705. return length;
  3706. }
  3707. SLAB_ATTR(shrink);
  3708. #ifdef CONFIG_NUMA
  3709. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  3710. {
  3711. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  3712. }
  3713. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  3714. const char *buf, size_t length)
  3715. {
  3716. unsigned long ratio;
  3717. int err;
  3718. err = strict_strtoul(buf, 10, &ratio);
  3719. if (err)
  3720. return err;
  3721. if (ratio <= 100)
  3722. s->remote_node_defrag_ratio = ratio * 10;
  3723. return length;
  3724. }
  3725. SLAB_ATTR(remote_node_defrag_ratio);
  3726. #endif
  3727. #ifdef CONFIG_SLUB_STATS
  3728. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  3729. {
  3730. unsigned long sum = 0;
  3731. int cpu;
  3732. int len;
  3733. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  3734. if (!data)
  3735. return -ENOMEM;
  3736. for_each_online_cpu(cpu) {
  3737. unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
  3738. data[cpu] = x;
  3739. sum += x;
  3740. }
  3741. len = sprintf(buf, "%lu", sum);
  3742. #ifdef CONFIG_SMP
  3743. for_each_online_cpu(cpu) {
  3744. if (data[cpu] && len < PAGE_SIZE - 20)
  3745. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  3746. }
  3747. #endif
  3748. kfree(data);
  3749. return len + sprintf(buf + len, "\n");
  3750. }
  3751. static void clear_stat(struct kmem_cache *s, enum stat_item si)
  3752. {
  3753. int cpu;
  3754. for_each_online_cpu(cpu)
  3755. per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
  3756. }
  3757. #define STAT_ATTR(si, text) \
  3758. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  3759. { \
  3760. return show_stat(s, buf, si); \
  3761. } \
  3762. static ssize_t text##_store(struct kmem_cache *s, \
  3763. const char *buf, size_t length) \
  3764. { \
  3765. if (buf[0] != '0') \
  3766. return -EINVAL; \
  3767. clear_stat(s, si); \
  3768. return length; \
  3769. } \
  3770. SLAB_ATTR(text); \
  3771. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  3772. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  3773. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  3774. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  3775. STAT_ATTR(FREE_FROZEN, free_frozen);
  3776. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  3777. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  3778. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  3779. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  3780. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  3781. STAT_ATTR(FREE_SLAB, free_slab);
  3782. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  3783. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  3784. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  3785. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  3786. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  3787. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  3788. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  3789. #endif
  3790. static struct attribute *slab_attrs[] = {
  3791. &slab_size_attr.attr,
  3792. &object_size_attr.attr,
  3793. &objs_per_slab_attr.attr,
  3794. &order_attr.attr,
  3795. &min_partial_attr.attr,
  3796. &objects_attr.attr,
  3797. &objects_partial_attr.attr,
  3798. &partial_attr.attr,
  3799. &cpu_slabs_attr.attr,
  3800. &ctor_attr.attr,
  3801. &aliases_attr.attr,
  3802. &align_attr.attr,
  3803. &hwcache_align_attr.attr,
  3804. &reclaim_account_attr.attr,
  3805. &destroy_by_rcu_attr.attr,
  3806. &shrink_attr.attr,
  3807. &reserved_attr.attr,
  3808. #ifdef CONFIG_SLUB_DEBUG
  3809. &total_objects_attr.attr,
  3810. &slabs_attr.attr,
  3811. &sanity_checks_attr.attr,
  3812. &trace_attr.attr,
  3813. &red_zone_attr.attr,
  3814. &poison_attr.attr,
  3815. &store_user_attr.attr,
  3816. &validate_attr.attr,
  3817. &alloc_calls_attr.attr,
  3818. &free_calls_attr.attr,
  3819. #endif
  3820. #ifdef CONFIG_ZONE_DMA
  3821. &cache_dma_attr.attr,
  3822. #endif
  3823. #ifdef CONFIG_NUMA
  3824. &remote_node_defrag_ratio_attr.attr,
  3825. #endif
  3826. #ifdef CONFIG_SLUB_STATS
  3827. &alloc_fastpath_attr.attr,
  3828. &alloc_slowpath_attr.attr,
  3829. &free_fastpath_attr.attr,
  3830. &free_slowpath_attr.attr,
  3831. &free_frozen_attr.attr,
  3832. &free_add_partial_attr.attr,
  3833. &free_remove_partial_attr.attr,
  3834. &alloc_from_partial_attr.attr,
  3835. &alloc_slab_attr.attr,
  3836. &alloc_refill_attr.attr,
  3837. &free_slab_attr.attr,
  3838. &cpuslab_flush_attr.attr,
  3839. &deactivate_full_attr.attr,
  3840. &deactivate_empty_attr.attr,
  3841. &deactivate_to_head_attr.attr,
  3842. &deactivate_to_tail_attr.attr,
  3843. &deactivate_remote_frees_attr.attr,
  3844. &order_fallback_attr.attr,
  3845. #endif
  3846. #ifdef CONFIG_FAILSLAB
  3847. &failslab_attr.attr,
  3848. #endif
  3849. NULL
  3850. };
  3851. static struct attribute_group slab_attr_group = {
  3852. .attrs = slab_attrs,
  3853. };
  3854. static ssize_t slab_attr_show(struct kobject *kobj,
  3855. struct attribute *attr,
  3856. char *buf)
  3857. {
  3858. struct slab_attribute *attribute;
  3859. struct kmem_cache *s;
  3860. int err;
  3861. attribute = to_slab_attr(attr);
  3862. s = to_slab(kobj);
  3863. if (!attribute->show)
  3864. return -EIO;
  3865. err = attribute->show(s, buf);
  3866. return err;
  3867. }
  3868. static ssize_t slab_attr_store(struct kobject *kobj,
  3869. struct attribute *attr,
  3870. const char *buf, size_t len)
  3871. {
  3872. struct slab_attribute *attribute;
  3873. struct kmem_cache *s;
  3874. int err;
  3875. attribute = to_slab_attr(attr);
  3876. s = to_slab(kobj);
  3877. if (!attribute->store)
  3878. return -EIO;
  3879. err = attribute->store(s, buf, len);
  3880. return err;
  3881. }
  3882. static void kmem_cache_release(struct kobject *kobj)
  3883. {
  3884. struct kmem_cache *s = to_slab(kobj);
  3885. kfree(s->name);
  3886. kfree(s);
  3887. }
  3888. static const struct sysfs_ops slab_sysfs_ops = {
  3889. .show = slab_attr_show,
  3890. .store = slab_attr_store,
  3891. };
  3892. static struct kobj_type slab_ktype = {
  3893. .sysfs_ops = &slab_sysfs_ops,
  3894. .release = kmem_cache_release
  3895. };
  3896. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  3897. {
  3898. struct kobj_type *ktype = get_ktype(kobj);
  3899. if (ktype == &slab_ktype)
  3900. return 1;
  3901. return 0;
  3902. }
  3903. static const struct kset_uevent_ops slab_uevent_ops = {
  3904. .filter = uevent_filter,
  3905. };
  3906. static struct kset *slab_kset;
  3907. #define ID_STR_LENGTH 64
  3908. /* Create a unique string id for a slab cache:
  3909. *
  3910. * Format :[flags-]size
  3911. */
  3912. static char *create_unique_id(struct kmem_cache *s)
  3913. {
  3914. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  3915. char *p = name;
  3916. BUG_ON(!name);
  3917. *p++ = ':';
  3918. /*
  3919. * First flags affecting slabcache operations. We will only
  3920. * get here for aliasable slabs so we do not need to support
  3921. * too many flags. The flags here must cover all flags that
  3922. * are matched during merging to guarantee that the id is
  3923. * unique.
  3924. */
  3925. if (s->flags & SLAB_CACHE_DMA)
  3926. *p++ = 'd';
  3927. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3928. *p++ = 'a';
  3929. if (s->flags & SLAB_DEBUG_FREE)
  3930. *p++ = 'F';
  3931. if (!(s->flags & SLAB_NOTRACK))
  3932. *p++ = 't';
  3933. if (p != name + 1)
  3934. *p++ = '-';
  3935. p += sprintf(p, "%07d", s->size);
  3936. BUG_ON(p > name + ID_STR_LENGTH - 1);
  3937. return name;
  3938. }
  3939. static int sysfs_slab_add(struct kmem_cache *s)
  3940. {
  3941. int err;
  3942. const char *name;
  3943. int unmergeable;
  3944. if (slab_state < SYSFS)
  3945. /* Defer until later */
  3946. return 0;
  3947. unmergeable = slab_unmergeable(s);
  3948. if (unmergeable) {
  3949. /*
  3950. * Slabcache can never be merged so we can use the name proper.
  3951. * This is typically the case for debug situations. In that
  3952. * case we can catch duplicate names easily.
  3953. */
  3954. sysfs_remove_link(&slab_kset->kobj, s->name);
  3955. name = s->name;
  3956. } else {
  3957. /*
  3958. * Create a unique name for the slab as a target
  3959. * for the symlinks.
  3960. */
  3961. name = create_unique_id(s);
  3962. }
  3963. s->kobj.kset = slab_kset;
  3964. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  3965. if (err) {
  3966. kobject_put(&s->kobj);
  3967. return err;
  3968. }
  3969. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  3970. if (err) {
  3971. kobject_del(&s->kobj);
  3972. kobject_put(&s->kobj);
  3973. return err;
  3974. }
  3975. kobject_uevent(&s->kobj, KOBJ_ADD);
  3976. if (!unmergeable) {
  3977. /* Setup first alias */
  3978. sysfs_slab_alias(s, s->name);
  3979. kfree(name);
  3980. }
  3981. return 0;
  3982. }
  3983. static void sysfs_slab_remove(struct kmem_cache *s)
  3984. {
  3985. if (slab_state < SYSFS)
  3986. /*
  3987. * Sysfs has not been setup yet so no need to remove the
  3988. * cache from sysfs.
  3989. */
  3990. return;
  3991. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  3992. kobject_del(&s->kobj);
  3993. kobject_put(&s->kobj);
  3994. }
  3995. /*
  3996. * Need to buffer aliases during bootup until sysfs becomes
  3997. * available lest we lose that information.
  3998. */
  3999. struct saved_alias {
  4000. struct kmem_cache *s;
  4001. const char *name;
  4002. struct saved_alias *next;
  4003. };
  4004. static struct saved_alias *alias_list;
  4005. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  4006. {
  4007. struct saved_alias *al;
  4008. if (slab_state == SYSFS) {
  4009. /*
  4010. * If we have a leftover link then remove it.
  4011. */
  4012. sysfs_remove_link(&slab_kset->kobj, name);
  4013. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  4014. }
  4015. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  4016. if (!al)
  4017. return -ENOMEM;
  4018. al->s = s;
  4019. al->name = name;
  4020. al->next = alias_list;
  4021. alias_list = al;
  4022. return 0;
  4023. }
  4024. static int __init slab_sysfs_init(void)
  4025. {
  4026. struct kmem_cache *s;
  4027. int err;
  4028. down_write(&slub_lock);
  4029. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  4030. if (!slab_kset) {
  4031. up_write(&slub_lock);
  4032. printk(KERN_ERR "Cannot register slab subsystem.\n");
  4033. return -ENOSYS;
  4034. }
  4035. slab_state = SYSFS;
  4036. list_for_each_entry(s, &slab_caches, list) {
  4037. err = sysfs_slab_add(s);
  4038. if (err)
  4039. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  4040. " to sysfs\n", s->name);
  4041. }
  4042. while (alias_list) {
  4043. struct saved_alias *al = alias_list;
  4044. alias_list = alias_list->next;
  4045. err = sysfs_slab_alias(al->s, al->name);
  4046. if (err)
  4047. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  4048. " %s to sysfs\n", s->name);
  4049. kfree(al);
  4050. }
  4051. up_write(&slub_lock);
  4052. resiliency_test();
  4053. return 0;
  4054. }
  4055. __initcall(slab_sysfs_init);
  4056. #endif /* CONFIG_SYSFS */
  4057. /*
  4058. * The /proc/slabinfo ABI
  4059. */
  4060. #ifdef CONFIG_SLABINFO
  4061. static void print_slabinfo_header(struct seq_file *m)
  4062. {
  4063. seq_puts(m, "slabinfo - version: 2.1\n");
  4064. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  4065. "<objperslab> <pagesperslab>");
  4066. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  4067. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  4068. seq_putc(m, '\n');
  4069. }
  4070. static void *s_start(struct seq_file *m, loff_t *pos)
  4071. {
  4072. loff_t n = *pos;
  4073. down_read(&slub_lock);
  4074. if (!n)
  4075. print_slabinfo_header(m);
  4076. return seq_list_start(&slab_caches, *pos);
  4077. }
  4078. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  4079. {
  4080. return seq_list_next(p, &slab_caches, pos);
  4081. }
  4082. static void s_stop(struct seq_file *m, void *p)
  4083. {
  4084. up_read(&slub_lock);
  4085. }
  4086. static int s_show(struct seq_file *m, void *p)
  4087. {
  4088. unsigned long nr_partials = 0;
  4089. unsigned long nr_slabs = 0;
  4090. unsigned long nr_inuse = 0;
  4091. unsigned long nr_objs = 0;
  4092. unsigned long nr_free = 0;
  4093. struct kmem_cache *s;
  4094. int node;
  4095. s = list_entry(p, struct kmem_cache, list);
  4096. for_each_online_node(node) {
  4097. struct kmem_cache_node *n = get_node(s, node);
  4098. if (!n)
  4099. continue;
  4100. nr_partials += n->nr_partial;
  4101. nr_slabs += atomic_long_read(&n->nr_slabs);
  4102. nr_objs += atomic_long_read(&n->total_objects);
  4103. nr_free += count_partial(n, count_free);
  4104. }
  4105. nr_inuse = nr_objs - nr_free;
  4106. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  4107. nr_objs, s->size, oo_objects(s->oo),
  4108. (1 << oo_order(s->oo)));
  4109. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  4110. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  4111. 0UL);
  4112. seq_putc(m, '\n');
  4113. return 0;
  4114. }
  4115. static const struct seq_operations slabinfo_op = {
  4116. .start = s_start,
  4117. .next = s_next,
  4118. .stop = s_stop,
  4119. .show = s_show,
  4120. };
  4121. static int slabinfo_open(struct inode *inode, struct file *file)
  4122. {
  4123. return seq_open(file, &slabinfo_op);
  4124. }
  4125. static const struct file_operations proc_slabinfo_operations = {
  4126. .open = slabinfo_open,
  4127. .read = seq_read,
  4128. .llseek = seq_lseek,
  4129. .release = seq_release,
  4130. };
  4131. static int __init slab_proc_init(void)
  4132. {
  4133. proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
  4134. return 0;
  4135. }
  4136. module_init(slab_proc_init);
  4137. #endif /* CONFIG_SLABINFO */