slub.c 81 KB

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