slub.c 80 KB

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