slub.c 105 KB

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