slub.c 81 KB

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