slub.c 86 KB

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