slub.c 101 KB

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