slub.c 105 KB

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