slub.c 81 KB

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