slub.c 76 KB

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