slub.c 103 KB

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