slub.c 86 KB

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