slub.c 84 KB

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