slub.c 102 KB

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