slub.c 102 KB

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