slub.c 93 KB

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