slub.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673
  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 inline int slab_order(int size, int min_objects,
  1372. int max_order, int fract_leftover)
  1373. {
  1374. int order;
  1375. int rem;
  1376. for (order = max(slub_min_order,
  1377. fls(min_objects * size - 1) - PAGE_SHIFT);
  1378. order <= max_order; order++) {
  1379. unsigned long slab_size = PAGE_SIZE << order;
  1380. if (slab_size < min_objects * size)
  1381. continue;
  1382. rem = slab_size % size;
  1383. if (rem <= slab_size / fract_leftover)
  1384. break;
  1385. }
  1386. return order;
  1387. }
  1388. static inline int calculate_order(int size)
  1389. {
  1390. int order;
  1391. int min_objects;
  1392. int fraction;
  1393. /*
  1394. * Attempt to find best configuration for a slab. This
  1395. * works by first attempting to generate a layout with
  1396. * the best configuration and backing off gradually.
  1397. *
  1398. * First we reduce the acceptable waste in a slab. Then
  1399. * we reduce the minimum objects required in a slab.
  1400. */
  1401. min_objects = slub_min_objects;
  1402. while (min_objects > 1) {
  1403. fraction = 8;
  1404. while (fraction >= 4) {
  1405. order = slab_order(size, min_objects,
  1406. slub_max_order, fraction);
  1407. if (order <= slub_max_order)
  1408. return order;
  1409. fraction /= 2;
  1410. }
  1411. min_objects /= 2;
  1412. }
  1413. /*
  1414. * We were unable to place multiple objects in a slab. Now
  1415. * lets see if we can place a single object there.
  1416. */
  1417. order = slab_order(size, 1, slub_max_order, 1);
  1418. if (order <= slub_max_order)
  1419. return order;
  1420. /*
  1421. * Doh this slab cannot be placed using slub_max_order.
  1422. */
  1423. order = slab_order(size, 1, MAX_ORDER, 1);
  1424. if (order <= MAX_ORDER)
  1425. return order;
  1426. return -ENOSYS;
  1427. }
  1428. /*
  1429. * Figure out what the alignment of the objects will be.
  1430. */
  1431. static unsigned long calculate_alignment(unsigned long flags,
  1432. unsigned long align, unsigned long size)
  1433. {
  1434. /*
  1435. * If the user wants hardware cache aligned objects then
  1436. * follow that suggestion if the object is sufficiently
  1437. * large.
  1438. *
  1439. * The hardware cache alignment cannot override the
  1440. * specified alignment though. If that is greater
  1441. * then use it.
  1442. */
  1443. if ((flags & SLAB_HWCACHE_ALIGN) &&
  1444. size > cache_line_size() / 2)
  1445. return max_t(unsigned long, align, cache_line_size());
  1446. if (align < ARCH_SLAB_MINALIGN)
  1447. return ARCH_SLAB_MINALIGN;
  1448. return ALIGN(align, sizeof(void *));
  1449. }
  1450. static void init_kmem_cache_node(struct kmem_cache_node *n)
  1451. {
  1452. n->nr_partial = 0;
  1453. atomic_long_set(&n->nr_slabs, 0);
  1454. spin_lock_init(&n->list_lock);
  1455. INIT_LIST_HEAD(&n->partial);
  1456. INIT_LIST_HEAD(&n->full);
  1457. }
  1458. #ifdef CONFIG_NUMA
  1459. /*
  1460. * No kmalloc_node yet so do it by hand. We know that this is the first
  1461. * slab on the node for this slabcache. There are no concurrent accesses
  1462. * possible.
  1463. *
  1464. * Note that this function only works on the kmalloc_node_cache
  1465. * when allocating for the kmalloc_node_cache.
  1466. */
  1467. static struct kmem_cache_node * __init early_kmem_cache_node_alloc(gfp_t gfpflags,
  1468. int node)
  1469. {
  1470. struct page *page;
  1471. struct kmem_cache_node *n;
  1472. BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
  1473. page = new_slab(kmalloc_caches, gfpflags | GFP_THISNODE, node);
  1474. /* new_slab() disables interupts */
  1475. local_irq_enable();
  1476. BUG_ON(!page);
  1477. n = page->freelist;
  1478. BUG_ON(!n);
  1479. page->freelist = get_freepointer(kmalloc_caches, n);
  1480. page->inuse++;
  1481. kmalloc_caches->node[node] = n;
  1482. init_object(kmalloc_caches, n, 1);
  1483. init_kmem_cache_node(n);
  1484. atomic_long_inc(&n->nr_slabs);
  1485. add_partial(n, page);
  1486. return n;
  1487. }
  1488. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1489. {
  1490. int node;
  1491. for_each_online_node(node) {
  1492. struct kmem_cache_node *n = s->node[node];
  1493. if (n && n != &s->local_node)
  1494. kmem_cache_free(kmalloc_caches, n);
  1495. s->node[node] = NULL;
  1496. }
  1497. }
  1498. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1499. {
  1500. int node;
  1501. int local_node;
  1502. if (slab_state >= UP)
  1503. local_node = page_to_nid(virt_to_page(s));
  1504. else
  1505. local_node = 0;
  1506. for_each_online_node(node) {
  1507. struct kmem_cache_node *n;
  1508. if (local_node == node)
  1509. n = &s->local_node;
  1510. else {
  1511. if (slab_state == DOWN) {
  1512. n = early_kmem_cache_node_alloc(gfpflags,
  1513. node);
  1514. continue;
  1515. }
  1516. n = kmem_cache_alloc_node(kmalloc_caches,
  1517. gfpflags, node);
  1518. if (!n) {
  1519. free_kmem_cache_nodes(s);
  1520. return 0;
  1521. }
  1522. }
  1523. s->node[node] = n;
  1524. init_kmem_cache_node(n);
  1525. }
  1526. return 1;
  1527. }
  1528. #else
  1529. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1530. {
  1531. }
  1532. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1533. {
  1534. init_kmem_cache_node(&s->local_node);
  1535. return 1;
  1536. }
  1537. #endif
  1538. /*
  1539. * calculate_sizes() determines the order and the distribution of data within
  1540. * a slab object.
  1541. */
  1542. static int calculate_sizes(struct kmem_cache *s)
  1543. {
  1544. unsigned long flags = s->flags;
  1545. unsigned long size = s->objsize;
  1546. unsigned long align = s->align;
  1547. /*
  1548. * Determine if we can poison the object itself. If the user of
  1549. * the slab may touch the object after free or before allocation
  1550. * then we should never poison the object itself.
  1551. */
  1552. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  1553. !s->ctor && !s->dtor)
  1554. s->flags |= __OBJECT_POISON;
  1555. else
  1556. s->flags &= ~__OBJECT_POISON;
  1557. /*
  1558. * Round up object size to the next word boundary. We can only
  1559. * place the free pointer at word boundaries and this determines
  1560. * the possible location of the free pointer.
  1561. */
  1562. size = ALIGN(size, sizeof(void *));
  1563. #ifdef CONFIG_SLUB_DEBUG
  1564. /*
  1565. * If we are Redzoning then check if there is some space between the
  1566. * end of the object and the free pointer. If not then add an
  1567. * additional word to have some bytes to store Redzone information.
  1568. */
  1569. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  1570. size += sizeof(void *);
  1571. #endif
  1572. /*
  1573. * With that we have determined the number of bytes in actual use
  1574. * by the object. This is the potential offset to the free pointer.
  1575. */
  1576. s->inuse = size;
  1577. #ifdef CONFIG_SLUB_DEBUG
  1578. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  1579. s->ctor || s->dtor)) {
  1580. /*
  1581. * Relocate free pointer after the object if it is not
  1582. * permitted to overwrite the first word of the object on
  1583. * kmem_cache_free.
  1584. *
  1585. * This is the case if we do RCU, have a constructor or
  1586. * destructor or are poisoning the objects.
  1587. */
  1588. s->offset = size;
  1589. size += sizeof(void *);
  1590. }
  1591. if (flags & SLAB_STORE_USER)
  1592. /*
  1593. * Need to store information about allocs and frees after
  1594. * the object.
  1595. */
  1596. size += 2 * sizeof(struct track);
  1597. if (flags & SLAB_RED_ZONE)
  1598. /*
  1599. * Add some empty padding so that we can catch
  1600. * overwrites from earlier objects rather than let
  1601. * tracking information or the free pointer be
  1602. * corrupted if an user writes before the start
  1603. * of the object.
  1604. */
  1605. size += sizeof(void *);
  1606. #endif
  1607. /*
  1608. * Determine the alignment based on various parameters that the
  1609. * user specified and the dynamic determination of cache line size
  1610. * on bootup.
  1611. */
  1612. align = calculate_alignment(flags, align, s->objsize);
  1613. /*
  1614. * SLUB stores one object immediately after another beginning from
  1615. * offset 0. In order to align the objects we have to simply size
  1616. * each object to conform to the alignment.
  1617. */
  1618. size = ALIGN(size, align);
  1619. s->size = size;
  1620. s->order = calculate_order(size);
  1621. if (s->order < 0)
  1622. return 0;
  1623. /*
  1624. * Determine the number of objects per slab
  1625. */
  1626. s->objects = (PAGE_SIZE << s->order) / size;
  1627. /*
  1628. * Verify that the number of objects is within permitted limits.
  1629. * The page->inuse field is only 16 bit wide! So we cannot have
  1630. * more than 64k objects per slab.
  1631. */
  1632. if (!s->objects || s->objects > 65535)
  1633. return 0;
  1634. return 1;
  1635. }
  1636. static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
  1637. const char *name, size_t size,
  1638. size_t align, unsigned long flags,
  1639. void (*ctor)(void *, struct kmem_cache *, unsigned long),
  1640. void (*dtor)(void *, struct kmem_cache *, unsigned long))
  1641. {
  1642. memset(s, 0, kmem_size);
  1643. s->name = name;
  1644. s->ctor = ctor;
  1645. s->dtor = dtor;
  1646. s->objsize = size;
  1647. s->flags = flags;
  1648. s->align = align;
  1649. kmem_cache_open_debug_check(s);
  1650. if (!calculate_sizes(s))
  1651. goto error;
  1652. s->refcount = 1;
  1653. #ifdef CONFIG_NUMA
  1654. s->defrag_ratio = 100;
  1655. #endif
  1656. if (init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
  1657. return 1;
  1658. error:
  1659. if (flags & SLAB_PANIC)
  1660. panic("Cannot create slab %s size=%lu realsize=%u "
  1661. "order=%u offset=%u flags=%lx\n",
  1662. s->name, (unsigned long)size, s->size, s->order,
  1663. s->offset, flags);
  1664. return 0;
  1665. }
  1666. EXPORT_SYMBOL(kmem_cache_open);
  1667. /*
  1668. * Check if a given pointer is valid
  1669. */
  1670. int kmem_ptr_validate(struct kmem_cache *s, const void *object)
  1671. {
  1672. struct page * page;
  1673. page = get_object_page(object);
  1674. if (!page || s != page->slab)
  1675. /* No slab or wrong slab */
  1676. return 0;
  1677. if (!check_valid_pointer(s, page, object))
  1678. return 0;
  1679. /*
  1680. * We could also check if the object is on the slabs freelist.
  1681. * But this would be too expensive and it seems that the main
  1682. * purpose of kmem_ptr_valid is to check if the object belongs
  1683. * to a certain slab.
  1684. */
  1685. return 1;
  1686. }
  1687. EXPORT_SYMBOL(kmem_ptr_validate);
  1688. /*
  1689. * Determine the size of a slab object
  1690. */
  1691. unsigned int kmem_cache_size(struct kmem_cache *s)
  1692. {
  1693. return s->objsize;
  1694. }
  1695. EXPORT_SYMBOL(kmem_cache_size);
  1696. const char *kmem_cache_name(struct kmem_cache *s)
  1697. {
  1698. return s->name;
  1699. }
  1700. EXPORT_SYMBOL(kmem_cache_name);
  1701. /*
  1702. * Attempt to free all slabs on a node. Return the number of slabs we
  1703. * were unable to free.
  1704. */
  1705. static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
  1706. struct list_head *list)
  1707. {
  1708. int slabs_inuse = 0;
  1709. unsigned long flags;
  1710. struct page *page, *h;
  1711. spin_lock_irqsave(&n->list_lock, flags);
  1712. list_for_each_entry_safe(page, h, list, lru)
  1713. if (!page->inuse) {
  1714. list_del(&page->lru);
  1715. discard_slab(s, page);
  1716. } else
  1717. slabs_inuse++;
  1718. spin_unlock_irqrestore(&n->list_lock, flags);
  1719. return slabs_inuse;
  1720. }
  1721. /*
  1722. * Release all resources used by a slab cache.
  1723. */
  1724. static int kmem_cache_close(struct kmem_cache *s)
  1725. {
  1726. int node;
  1727. flush_all(s);
  1728. /* Attempt to free all objects */
  1729. for_each_online_node(node) {
  1730. struct kmem_cache_node *n = get_node(s, node);
  1731. n->nr_partial -= free_list(s, n, &n->partial);
  1732. if (atomic_long_read(&n->nr_slabs))
  1733. return 1;
  1734. }
  1735. free_kmem_cache_nodes(s);
  1736. return 0;
  1737. }
  1738. /*
  1739. * Close a cache and release the kmem_cache structure
  1740. * (must be used for caches created using kmem_cache_create)
  1741. */
  1742. void kmem_cache_destroy(struct kmem_cache *s)
  1743. {
  1744. down_write(&slub_lock);
  1745. s->refcount--;
  1746. if (!s->refcount) {
  1747. list_del(&s->list);
  1748. if (kmem_cache_close(s))
  1749. WARN_ON(1);
  1750. sysfs_slab_remove(s);
  1751. kfree(s);
  1752. }
  1753. up_write(&slub_lock);
  1754. }
  1755. EXPORT_SYMBOL(kmem_cache_destroy);
  1756. /********************************************************************
  1757. * Kmalloc subsystem
  1758. *******************************************************************/
  1759. struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1] __cacheline_aligned;
  1760. EXPORT_SYMBOL(kmalloc_caches);
  1761. #ifdef CONFIG_ZONE_DMA
  1762. static struct kmem_cache *kmalloc_caches_dma[KMALLOC_SHIFT_HIGH + 1];
  1763. #endif
  1764. static int __init setup_slub_min_order(char *str)
  1765. {
  1766. get_option (&str, &slub_min_order);
  1767. return 1;
  1768. }
  1769. __setup("slub_min_order=", setup_slub_min_order);
  1770. static int __init setup_slub_max_order(char *str)
  1771. {
  1772. get_option (&str, &slub_max_order);
  1773. return 1;
  1774. }
  1775. __setup("slub_max_order=", setup_slub_max_order);
  1776. static int __init setup_slub_min_objects(char *str)
  1777. {
  1778. get_option (&str, &slub_min_objects);
  1779. return 1;
  1780. }
  1781. __setup("slub_min_objects=", setup_slub_min_objects);
  1782. static int __init setup_slub_nomerge(char *str)
  1783. {
  1784. slub_nomerge = 1;
  1785. return 1;
  1786. }
  1787. __setup("slub_nomerge", setup_slub_nomerge);
  1788. static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
  1789. const char *name, int size, gfp_t gfp_flags)
  1790. {
  1791. unsigned int flags = 0;
  1792. if (gfp_flags & SLUB_DMA)
  1793. flags = SLAB_CACHE_DMA;
  1794. down_write(&slub_lock);
  1795. if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
  1796. flags, NULL, NULL))
  1797. goto panic;
  1798. list_add(&s->list, &slab_caches);
  1799. up_write(&slub_lock);
  1800. if (sysfs_slab_add(s))
  1801. goto panic;
  1802. return s;
  1803. panic:
  1804. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  1805. }
  1806. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  1807. {
  1808. int index = kmalloc_index(size);
  1809. if (!index)
  1810. return NULL;
  1811. /* Allocation too large? */
  1812. BUG_ON(index < 0);
  1813. #ifdef CONFIG_ZONE_DMA
  1814. if ((flags & SLUB_DMA)) {
  1815. struct kmem_cache *s;
  1816. struct kmem_cache *x;
  1817. char *text;
  1818. size_t realsize;
  1819. s = kmalloc_caches_dma[index];
  1820. if (s)
  1821. return s;
  1822. /* Dynamically create dma cache */
  1823. x = kmalloc(kmem_size, flags & ~SLUB_DMA);
  1824. if (!x)
  1825. panic("Unable to allocate memory for dma cache\n");
  1826. if (index <= KMALLOC_SHIFT_HIGH)
  1827. realsize = 1 << index;
  1828. else {
  1829. if (index == 1)
  1830. realsize = 96;
  1831. else
  1832. realsize = 192;
  1833. }
  1834. text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d",
  1835. (unsigned int)realsize);
  1836. s = create_kmalloc_cache(x, text, realsize, flags);
  1837. kmalloc_caches_dma[index] = s;
  1838. return s;
  1839. }
  1840. #endif
  1841. return &kmalloc_caches[index];
  1842. }
  1843. void *__kmalloc(size_t size, gfp_t flags)
  1844. {
  1845. struct kmem_cache *s = get_slab(size, flags);
  1846. if (s)
  1847. return slab_alloc(s, flags, -1, __builtin_return_address(0));
  1848. return NULL;
  1849. }
  1850. EXPORT_SYMBOL(__kmalloc);
  1851. #ifdef CONFIG_NUMA
  1852. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  1853. {
  1854. struct kmem_cache *s = get_slab(size, flags);
  1855. if (s)
  1856. return slab_alloc(s, flags, node, __builtin_return_address(0));
  1857. return NULL;
  1858. }
  1859. EXPORT_SYMBOL(__kmalloc_node);
  1860. #endif
  1861. size_t ksize(const void *object)
  1862. {
  1863. struct page *page = get_object_page(object);
  1864. struct kmem_cache *s;
  1865. BUG_ON(!page);
  1866. s = page->slab;
  1867. BUG_ON(!s);
  1868. /*
  1869. * Debugging requires use of the padding between object
  1870. * and whatever may come after it.
  1871. */
  1872. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  1873. return s->objsize;
  1874. /*
  1875. * If we have the need to store the freelist pointer
  1876. * back there or track user information then we can
  1877. * only use the space before that information.
  1878. */
  1879. if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
  1880. return s->inuse;
  1881. /*
  1882. * Else we can use all the padding etc for the allocation
  1883. */
  1884. return s->size;
  1885. }
  1886. EXPORT_SYMBOL(ksize);
  1887. void kfree(const void *x)
  1888. {
  1889. struct kmem_cache *s;
  1890. struct page *page;
  1891. if (!x)
  1892. return;
  1893. page = virt_to_head_page(x);
  1894. s = page->slab;
  1895. slab_free(s, page, (void *)x, __builtin_return_address(0));
  1896. }
  1897. EXPORT_SYMBOL(kfree);
  1898. /*
  1899. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  1900. * the remaining slabs by the number of items in use. The slabs with the
  1901. * most items in use come first. New allocations will then fill those up
  1902. * and thus they can be removed from the partial lists.
  1903. *
  1904. * The slabs with the least items are placed last. This results in them
  1905. * being allocated from last increasing the chance that the last objects
  1906. * are freed in them.
  1907. */
  1908. int kmem_cache_shrink(struct kmem_cache *s)
  1909. {
  1910. int node;
  1911. int i;
  1912. struct kmem_cache_node *n;
  1913. struct page *page;
  1914. struct page *t;
  1915. struct list_head *slabs_by_inuse =
  1916. kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
  1917. unsigned long flags;
  1918. if (!slabs_by_inuse)
  1919. return -ENOMEM;
  1920. flush_all(s);
  1921. for_each_online_node(node) {
  1922. n = get_node(s, node);
  1923. if (!n->nr_partial)
  1924. continue;
  1925. for (i = 0; i < s->objects; i++)
  1926. INIT_LIST_HEAD(slabs_by_inuse + i);
  1927. spin_lock_irqsave(&n->list_lock, flags);
  1928. /*
  1929. * Build lists indexed by the items in use in each slab.
  1930. *
  1931. * Note that concurrent frees may occur while we hold the
  1932. * list_lock. page->inuse here is the upper limit.
  1933. */
  1934. list_for_each_entry_safe(page, t, &n->partial, lru) {
  1935. if (!page->inuse && slab_trylock(page)) {
  1936. /*
  1937. * Must hold slab lock here because slab_free
  1938. * may have freed the last object and be
  1939. * waiting to release the slab.
  1940. */
  1941. list_del(&page->lru);
  1942. n->nr_partial--;
  1943. slab_unlock(page);
  1944. discard_slab(s, page);
  1945. } else {
  1946. if (n->nr_partial > MAX_PARTIAL)
  1947. list_move(&page->lru,
  1948. slabs_by_inuse + page->inuse);
  1949. }
  1950. }
  1951. if (n->nr_partial <= MAX_PARTIAL)
  1952. goto out;
  1953. /*
  1954. * Rebuild the partial list with the slabs filled up most
  1955. * first and the least used slabs at the end.
  1956. */
  1957. for (i = s->objects - 1; i >= 0; i--)
  1958. list_splice(slabs_by_inuse + i, n->partial.prev);
  1959. out:
  1960. spin_unlock_irqrestore(&n->list_lock, flags);
  1961. }
  1962. kfree(slabs_by_inuse);
  1963. return 0;
  1964. }
  1965. EXPORT_SYMBOL(kmem_cache_shrink);
  1966. /**
  1967. * krealloc - reallocate memory. The contents will remain unchanged.
  1968. * @p: object to reallocate memory for.
  1969. * @new_size: how many bytes of memory are required.
  1970. * @flags: the type of memory to allocate.
  1971. *
  1972. * The contents of the object pointed to are preserved up to the
  1973. * lesser of the new and old sizes. If @p is %NULL, krealloc()
  1974. * behaves exactly like kmalloc(). If @size is 0 and @p is not a
  1975. * %NULL pointer, the object pointed to is freed.
  1976. */
  1977. void *krealloc(const void *p, size_t new_size, gfp_t flags)
  1978. {
  1979. void *ret;
  1980. size_t ks;
  1981. if (unlikely(!p))
  1982. return kmalloc(new_size, flags);
  1983. if (unlikely(!new_size)) {
  1984. kfree(p);
  1985. return NULL;
  1986. }
  1987. ks = ksize(p);
  1988. if (ks >= new_size)
  1989. return (void *)p;
  1990. ret = kmalloc(new_size, flags);
  1991. if (ret) {
  1992. memcpy(ret, p, min(new_size, ks));
  1993. kfree(p);
  1994. }
  1995. return ret;
  1996. }
  1997. EXPORT_SYMBOL(krealloc);
  1998. /********************************************************************
  1999. * Basic setup of slabs
  2000. *******************************************************************/
  2001. void __init kmem_cache_init(void)
  2002. {
  2003. int i;
  2004. #ifdef CONFIG_NUMA
  2005. /*
  2006. * Must first have the slab cache available for the allocations of the
  2007. * struct kmem_cache_node's. There is special bootstrap code in
  2008. * kmem_cache_open for slab_state == DOWN.
  2009. */
  2010. create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
  2011. sizeof(struct kmem_cache_node), GFP_KERNEL);
  2012. #endif
  2013. /* Able to allocate the per node structures */
  2014. slab_state = PARTIAL;
  2015. /* Caches that are not of the two-to-the-power-of size */
  2016. create_kmalloc_cache(&kmalloc_caches[1],
  2017. "kmalloc-96", 96, GFP_KERNEL);
  2018. create_kmalloc_cache(&kmalloc_caches[2],
  2019. "kmalloc-192", 192, GFP_KERNEL);
  2020. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
  2021. create_kmalloc_cache(&kmalloc_caches[i],
  2022. "kmalloc", 1 << i, GFP_KERNEL);
  2023. slab_state = UP;
  2024. /* Provide the correct kmalloc names now that the caches are up */
  2025. for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
  2026. kmalloc_caches[i]. name =
  2027. kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
  2028. #ifdef CONFIG_SMP
  2029. register_cpu_notifier(&slab_notifier);
  2030. #endif
  2031. if (nr_cpu_ids) /* Remove when nr_cpu_ids is fixed upstream ! */
  2032. kmem_size = offsetof(struct kmem_cache, cpu_slab)
  2033. + nr_cpu_ids * sizeof(struct page *);
  2034. printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2035. " Processors=%d, Nodes=%d\n",
  2036. KMALLOC_SHIFT_HIGH, cache_line_size(),
  2037. slub_min_order, slub_max_order, slub_min_objects,
  2038. nr_cpu_ids, nr_node_ids);
  2039. }
  2040. /*
  2041. * Find a mergeable slab cache
  2042. */
  2043. static int slab_unmergeable(struct kmem_cache *s)
  2044. {
  2045. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2046. return 1;
  2047. if (s->ctor || s->dtor)
  2048. return 1;
  2049. return 0;
  2050. }
  2051. static struct kmem_cache *find_mergeable(size_t size,
  2052. size_t align, unsigned long flags,
  2053. void (*ctor)(void *, struct kmem_cache *, unsigned long),
  2054. void (*dtor)(void *, struct kmem_cache *, unsigned long))
  2055. {
  2056. struct list_head *h;
  2057. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  2058. return NULL;
  2059. if (ctor || dtor)
  2060. return NULL;
  2061. size = ALIGN(size, sizeof(void *));
  2062. align = calculate_alignment(flags, align, size);
  2063. size = ALIGN(size, align);
  2064. list_for_each(h, &slab_caches) {
  2065. struct kmem_cache *s =
  2066. container_of(h, struct kmem_cache, list);
  2067. if (slab_unmergeable(s))
  2068. continue;
  2069. if (size > s->size)
  2070. continue;
  2071. if (((flags | slub_debug) & SLUB_MERGE_SAME) !=
  2072. (s->flags & SLUB_MERGE_SAME))
  2073. continue;
  2074. /*
  2075. * Check if alignment is compatible.
  2076. * Courtesy of Adrian Drzewiecki
  2077. */
  2078. if ((s->size & ~(align -1)) != s->size)
  2079. continue;
  2080. if (s->size - size >= sizeof(void *))
  2081. continue;
  2082. return s;
  2083. }
  2084. return NULL;
  2085. }
  2086. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  2087. size_t align, unsigned long flags,
  2088. void (*ctor)(void *, struct kmem_cache *, unsigned long),
  2089. void (*dtor)(void *, struct kmem_cache *, unsigned long))
  2090. {
  2091. struct kmem_cache *s;
  2092. down_write(&slub_lock);
  2093. s = find_mergeable(size, align, flags, dtor, ctor);
  2094. if (s) {
  2095. s->refcount++;
  2096. /*
  2097. * Adjust the object sizes so that we clear
  2098. * the complete object on kzalloc.
  2099. */
  2100. s->objsize = max(s->objsize, (int)size);
  2101. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  2102. if (sysfs_slab_alias(s, name))
  2103. goto err;
  2104. } else {
  2105. s = kmalloc(kmem_size, GFP_KERNEL);
  2106. if (s && kmem_cache_open(s, GFP_KERNEL, name,
  2107. size, align, flags, ctor, dtor)) {
  2108. if (sysfs_slab_add(s)) {
  2109. kfree(s);
  2110. goto err;
  2111. }
  2112. list_add(&s->list, &slab_caches);
  2113. } else
  2114. kfree(s);
  2115. }
  2116. up_write(&slub_lock);
  2117. return s;
  2118. err:
  2119. up_write(&slub_lock);
  2120. if (flags & SLAB_PANIC)
  2121. panic("Cannot create slabcache %s\n", name);
  2122. else
  2123. s = NULL;
  2124. return s;
  2125. }
  2126. EXPORT_SYMBOL(kmem_cache_create);
  2127. void *kmem_cache_zalloc(struct kmem_cache *s, gfp_t flags)
  2128. {
  2129. void *x;
  2130. x = slab_alloc(s, flags, -1, __builtin_return_address(0));
  2131. if (x)
  2132. memset(x, 0, s->objsize);
  2133. return x;
  2134. }
  2135. EXPORT_SYMBOL(kmem_cache_zalloc);
  2136. #ifdef CONFIG_SMP
  2137. static void for_all_slabs(void (*func)(struct kmem_cache *, int), int cpu)
  2138. {
  2139. struct list_head *h;
  2140. down_read(&slub_lock);
  2141. list_for_each(h, &slab_caches) {
  2142. struct kmem_cache *s =
  2143. container_of(h, struct kmem_cache, list);
  2144. func(s, cpu);
  2145. }
  2146. up_read(&slub_lock);
  2147. }
  2148. /*
  2149. * Use the cpu notifier to insure that the cpu slabs are flushed when
  2150. * necessary.
  2151. */
  2152. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  2153. unsigned long action, void *hcpu)
  2154. {
  2155. long cpu = (long)hcpu;
  2156. switch (action) {
  2157. case CPU_UP_CANCELED:
  2158. case CPU_UP_CANCELED_FROZEN:
  2159. case CPU_DEAD:
  2160. case CPU_DEAD_FROZEN:
  2161. for_all_slabs(__flush_cpu_slab, cpu);
  2162. break;
  2163. default:
  2164. break;
  2165. }
  2166. return NOTIFY_OK;
  2167. }
  2168. static struct notifier_block __cpuinitdata slab_notifier =
  2169. { &slab_cpuup_callback, NULL, 0 };
  2170. #endif
  2171. #ifdef CONFIG_NUMA
  2172. /*****************************************************************
  2173. * Generic reaper used to support the page allocator
  2174. * (the cpu slabs are reaped by a per slab workqueue).
  2175. *
  2176. * Maybe move this to the page allocator?
  2177. ****************************************************************/
  2178. static DEFINE_PER_CPU(unsigned long, reap_node);
  2179. static void init_reap_node(int cpu)
  2180. {
  2181. int node;
  2182. node = next_node(cpu_to_node(cpu), node_online_map);
  2183. if (node == MAX_NUMNODES)
  2184. node = first_node(node_online_map);
  2185. __get_cpu_var(reap_node) = node;
  2186. }
  2187. static void next_reap_node(void)
  2188. {
  2189. int node = __get_cpu_var(reap_node);
  2190. /*
  2191. * Also drain per cpu pages on remote zones
  2192. */
  2193. if (node != numa_node_id())
  2194. drain_node_pages(node);
  2195. node = next_node(node, node_online_map);
  2196. if (unlikely(node >= MAX_NUMNODES))
  2197. node = first_node(node_online_map);
  2198. __get_cpu_var(reap_node) = node;
  2199. }
  2200. #else
  2201. #define init_reap_node(cpu) do { } while (0)
  2202. #define next_reap_node(void) do { } while (0)
  2203. #endif
  2204. #define REAPTIMEOUT_CPUC (2*HZ)
  2205. #ifdef CONFIG_SMP
  2206. static DEFINE_PER_CPU(struct delayed_work, reap_work);
  2207. static void cache_reap(struct work_struct *unused)
  2208. {
  2209. next_reap_node();
  2210. refresh_cpu_vm_stats(smp_processor_id());
  2211. schedule_delayed_work(&__get_cpu_var(reap_work),
  2212. REAPTIMEOUT_CPUC);
  2213. }
  2214. static void __devinit start_cpu_timer(int cpu)
  2215. {
  2216. struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
  2217. /*
  2218. * When this gets called from do_initcalls via cpucache_init(),
  2219. * init_workqueues() has already run, so keventd will be setup
  2220. * at that time.
  2221. */
  2222. if (keventd_up() && reap_work->work.func == NULL) {
  2223. init_reap_node(cpu);
  2224. INIT_DELAYED_WORK(reap_work, cache_reap);
  2225. schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
  2226. }
  2227. }
  2228. static int __init cpucache_init(void)
  2229. {
  2230. int cpu;
  2231. /*
  2232. * Register the timers that drain pcp pages and update vm statistics
  2233. */
  2234. for_each_online_cpu(cpu)
  2235. start_cpu_timer(cpu);
  2236. return 0;
  2237. }
  2238. __initcall(cpucache_init);
  2239. #endif
  2240. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
  2241. {
  2242. struct kmem_cache *s = get_slab(size, gfpflags);
  2243. if (!s)
  2244. return NULL;
  2245. return slab_alloc(s, gfpflags, -1, caller);
  2246. }
  2247. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  2248. int node, void *caller)
  2249. {
  2250. struct kmem_cache *s = get_slab(size, gfpflags);
  2251. if (!s)
  2252. return NULL;
  2253. return slab_alloc(s, gfpflags, node, caller);
  2254. }
  2255. #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
  2256. static int validate_slab(struct kmem_cache *s, struct page *page)
  2257. {
  2258. void *p;
  2259. void *addr = page_address(page);
  2260. DECLARE_BITMAP(map, s->objects);
  2261. if (!check_slab(s, page) ||
  2262. !on_freelist(s, page, NULL))
  2263. return 0;
  2264. /* Now we know that a valid freelist exists */
  2265. bitmap_zero(map, s->objects);
  2266. for_each_free_object(p, s, page->freelist) {
  2267. set_bit(slab_index(p, s, addr), map);
  2268. if (!check_object(s, page, p, 0))
  2269. return 0;
  2270. }
  2271. for_each_object(p, s, addr)
  2272. if (!test_bit(slab_index(p, s, addr), map))
  2273. if (!check_object(s, page, p, 1))
  2274. return 0;
  2275. return 1;
  2276. }
  2277. static void validate_slab_slab(struct kmem_cache *s, struct page *page)
  2278. {
  2279. if (slab_trylock(page)) {
  2280. validate_slab(s, page);
  2281. slab_unlock(page);
  2282. } else
  2283. printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
  2284. s->name, page);
  2285. if (s->flags & DEBUG_DEFAULT_FLAGS) {
  2286. if (!SlabDebug(page))
  2287. printk(KERN_ERR "SLUB %s: SlabDebug not set "
  2288. "on slab 0x%p\n", s->name, page);
  2289. } else {
  2290. if (SlabDebug(page))
  2291. printk(KERN_ERR "SLUB %s: SlabDebug set on "
  2292. "slab 0x%p\n", s->name, page);
  2293. }
  2294. }
  2295. static int validate_slab_node(struct kmem_cache *s, struct kmem_cache_node *n)
  2296. {
  2297. unsigned long count = 0;
  2298. struct page *page;
  2299. unsigned long flags;
  2300. spin_lock_irqsave(&n->list_lock, flags);
  2301. list_for_each_entry(page, &n->partial, lru) {
  2302. validate_slab_slab(s, page);
  2303. count++;
  2304. }
  2305. if (count != n->nr_partial)
  2306. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  2307. "counter=%ld\n", s->name, count, n->nr_partial);
  2308. if (!(s->flags & SLAB_STORE_USER))
  2309. goto out;
  2310. list_for_each_entry(page, &n->full, lru) {
  2311. validate_slab_slab(s, page);
  2312. count++;
  2313. }
  2314. if (count != atomic_long_read(&n->nr_slabs))
  2315. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  2316. "counter=%ld\n", s->name, count,
  2317. atomic_long_read(&n->nr_slabs));
  2318. out:
  2319. spin_unlock_irqrestore(&n->list_lock, flags);
  2320. return count;
  2321. }
  2322. static unsigned long validate_slab_cache(struct kmem_cache *s)
  2323. {
  2324. int node;
  2325. unsigned long count = 0;
  2326. flush_all(s);
  2327. for_each_online_node(node) {
  2328. struct kmem_cache_node *n = get_node(s, node);
  2329. count += validate_slab_node(s, n);
  2330. }
  2331. return count;
  2332. }
  2333. #ifdef SLUB_RESILIENCY_TEST
  2334. static void resiliency_test(void)
  2335. {
  2336. u8 *p;
  2337. printk(KERN_ERR "SLUB resiliency testing\n");
  2338. printk(KERN_ERR "-----------------------\n");
  2339. printk(KERN_ERR "A. Corruption after allocation\n");
  2340. p = kzalloc(16, GFP_KERNEL);
  2341. p[16] = 0x12;
  2342. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  2343. " 0x12->0x%p\n\n", p + 16);
  2344. validate_slab_cache(kmalloc_caches + 4);
  2345. /* Hmmm... The next two are dangerous */
  2346. p = kzalloc(32, GFP_KERNEL);
  2347. p[32 + sizeof(void *)] = 0x34;
  2348. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  2349. " 0x34 -> -0x%p\n", p);
  2350. printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
  2351. validate_slab_cache(kmalloc_caches + 5);
  2352. p = kzalloc(64, GFP_KERNEL);
  2353. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  2354. *p = 0x56;
  2355. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  2356. p);
  2357. printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
  2358. validate_slab_cache(kmalloc_caches + 6);
  2359. printk(KERN_ERR "\nB. Corruption after free\n");
  2360. p = kzalloc(128, GFP_KERNEL);
  2361. kfree(p);
  2362. *p = 0x78;
  2363. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  2364. validate_slab_cache(kmalloc_caches + 7);
  2365. p = kzalloc(256, GFP_KERNEL);
  2366. kfree(p);
  2367. p[50] = 0x9a;
  2368. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
  2369. validate_slab_cache(kmalloc_caches + 8);
  2370. p = kzalloc(512, GFP_KERNEL);
  2371. kfree(p);
  2372. p[512] = 0xab;
  2373. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  2374. validate_slab_cache(kmalloc_caches + 9);
  2375. }
  2376. #else
  2377. static void resiliency_test(void) {};
  2378. #endif
  2379. /*
  2380. * Generate lists of code addresses where slabcache objects are allocated
  2381. * and freed.
  2382. */
  2383. struct location {
  2384. unsigned long count;
  2385. void *addr;
  2386. long long sum_time;
  2387. long min_time;
  2388. long max_time;
  2389. long min_pid;
  2390. long max_pid;
  2391. cpumask_t cpus;
  2392. nodemask_t nodes;
  2393. };
  2394. struct loc_track {
  2395. unsigned long max;
  2396. unsigned long count;
  2397. struct location *loc;
  2398. };
  2399. static void free_loc_track(struct loc_track *t)
  2400. {
  2401. if (t->max)
  2402. free_pages((unsigned long)t->loc,
  2403. get_order(sizeof(struct location) * t->max));
  2404. }
  2405. static int alloc_loc_track(struct loc_track *t, unsigned long max)
  2406. {
  2407. struct location *l;
  2408. int order;
  2409. if (!max)
  2410. max = PAGE_SIZE / sizeof(struct location);
  2411. order = get_order(sizeof(struct location) * max);
  2412. l = (void *)__get_free_pages(GFP_KERNEL, order);
  2413. if (!l)
  2414. return 0;
  2415. if (t->count) {
  2416. memcpy(l, t->loc, sizeof(struct location) * t->count);
  2417. free_loc_track(t);
  2418. }
  2419. t->max = max;
  2420. t->loc = l;
  2421. return 1;
  2422. }
  2423. static int add_location(struct loc_track *t, struct kmem_cache *s,
  2424. const struct track *track)
  2425. {
  2426. long start, end, pos;
  2427. struct location *l;
  2428. void *caddr;
  2429. unsigned long age = jiffies - track->when;
  2430. start = -1;
  2431. end = t->count;
  2432. for ( ; ; ) {
  2433. pos = start + (end - start + 1) / 2;
  2434. /*
  2435. * There is nothing at "end". If we end up there
  2436. * we need to add something to before end.
  2437. */
  2438. if (pos == end)
  2439. break;
  2440. caddr = t->loc[pos].addr;
  2441. if (track->addr == caddr) {
  2442. l = &t->loc[pos];
  2443. l->count++;
  2444. if (track->when) {
  2445. l->sum_time += age;
  2446. if (age < l->min_time)
  2447. l->min_time = age;
  2448. if (age > l->max_time)
  2449. l->max_time = age;
  2450. if (track->pid < l->min_pid)
  2451. l->min_pid = track->pid;
  2452. if (track->pid > l->max_pid)
  2453. l->max_pid = track->pid;
  2454. cpu_set(track->cpu, l->cpus);
  2455. }
  2456. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  2457. return 1;
  2458. }
  2459. if (track->addr < caddr)
  2460. end = pos;
  2461. else
  2462. start = pos;
  2463. }
  2464. /*
  2465. * Not found. Insert new tracking element.
  2466. */
  2467. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max))
  2468. return 0;
  2469. l = t->loc + pos;
  2470. if (pos < t->count)
  2471. memmove(l + 1, l,
  2472. (t->count - pos) * sizeof(struct location));
  2473. t->count++;
  2474. l->count = 1;
  2475. l->addr = track->addr;
  2476. l->sum_time = age;
  2477. l->min_time = age;
  2478. l->max_time = age;
  2479. l->min_pid = track->pid;
  2480. l->max_pid = track->pid;
  2481. cpus_clear(l->cpus);
  2482. cpu_set(track->cpu, l->cpus);
  2483. nodes_clear(l->nodes);
  2484. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  2485. return 1;
  2486. }
  2487. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  2488. struct page *page, enum track_item alloc)
  2489. {
  2490. void *addr = page_address(page);
  2491. DECLARE_BITMAP(map, s->objects);
  2492. void *p;
  2493. bitmap_zero(map, s->objects);
  2494. for_each_free_object(p, s, page->freelist)
  2495. set_bit(slab_index(p, s, addr), map);
  2496. for_each_object(p, s, addr)
  2497. if (!test_bit(slab_index(p, s, addr), map))
  2498. add_location(t, s, get_track(s, p, alloc));
  2499. }
  2500. static int list_locations(struct kmem_cache *s, char *buf,
  2501. enum track_item alloc)
  2502. {
  2503. int n = 0;
  2504. unsigned long i;
  2505. struct loc_track t;
  2506. int node;
  2507. t.count = 0;
  2508. t.max = 0;
  2509. /* Push back cpu slabs */
  2510. flush_all(s);
  2511. for_each_online_node(node) {
  2512. struct kmem_cache_node *n = get_node(s, node);
  2513. unsigned long flags;
  2514. struct page *page;
  2515. if (!atomic_read(&n->nr_slabs))
  2516. continue;
  2517. spin_lock_irqsave(&n->list_lock, flags);
  2518. list_for_each_entry(page, &n->partial, lru)
  2519. process_slab(&t, s, page, alloc);
  2520. list_for_each_entry(page, &n->full, lru)
  2521. process_slab(&t, s, page, alloc);
  2522. spin_unlock_irqrestore(&n->list_lock, flags);
  2523. }
  2524. for (i = 0; i < t.count; i++) {
  2525. struct location *l = &t.loc[i];
  2526. if (n > PAGE_SIZE - 100)
  2527. break;
  2528. n += sprintf(buf + n, "%7ld ", l->count);
  2529. if (l->addr)
  2530. n += sprint_symbol(buf + n, (unsigned long)l->addr);
  2531. else
  2532. n += sprintf(buf + n, "<not-available>");
  2533. if (l->sum_time != l->min_time) {
  2534. unsigned long remainder;
  2535. n += sprintf(buf + n, " age=%ld/%ld/%ld",
  2536. l->min_time,
  2537. div_long_long_rem(l->sum_time, l->count, &remainder),
  2538. l->max_time);
  2539. } else
  2540. n += sprintf(buf + n, " age=%ld",
  2541. l->min_time);
  2542. if (l->min_pid != l->max_pid)
  2543. n += sprintf(buf + n, " pid=%ld-%ld",
  2544. l->min_pid, l->max_pid);
  2545. else
  2546. n += sprintf(buf + n, " pid=%ld",
  2547. l->min_pid);
  2548. if (num_online_cpus() > 1 && !cpus_empty(l->cpus)) {
  2549. n += sprintf(buf + n, " cpus=");
  2550. n += cpulist_scnprintf(buf + n, PAGE_SIZE - n - 50,
  2551. l->cpus);
  2552. }
  2553. if (num_online_nodes() > 1 && !nodes_empty(l->nodes)) {
  2554. n += sprintf(buf + n, " nodes=");
  2555. n += nodelist_scnprintf(buf + n, PAGE_SIZE - n - 50,
  2556. l->nodes);
  2557. }
  2558. n += sprintf(buf + n, "\n");
  2559. }
  2560. free_loc_track(&t);
  2561. if (!t.count)
  2562. n += sprintf(buf, "No data\n");
  2563. return n;
  2564. }
  2565. static unsigned long count_partial(struct kmem_cache_node *n)
  2566. {
  2567. unsigned long flags;
  2568. unsigned long x = 0;
  2569. struct page *page;
  2570. spin_lock_irqsave(&n->list_lock, flags);
  2571. list_for_each_entry(page, &n->partial, lru)
  2572. x += page->inuse;
  2573. spin_unlock_irqrestore(&n->list_lock, flags);
  2574. return x;
  2575. }
  2576. enum slab_stat_type {
  2577. SL_FULL,
  2578. SL_PARTIAL,
  2579. SL_CPU,
  2580. SL_OBJECTS
  2581. };
  2582. #define SO_FULL (1 << SL_FULL)
  2583. #define SO_PARTIAL (1 << SL_PARTIAL)
  2584. #define SO_CPU (1 << SL_CPU)
  2585. #define SO_OBJECTS (1 << SL_OBJECTS)
  2586. static unsigned long slab_objects(struct kmem_cache *s,
  2587. char *buf, unsigned long flags)
  2588. {
  2589. unsigned long total = 0;
  2590. int cpu;
  2591. int node;
  2592. int x;
  2593. unsigned long *nodes;
  2594. unsigned long *per_cpu;
  2595. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  2596. per_cpu = nodes + nr_node_ids;
  2597. for_each_possible_cpu(cpu) {
  2598. struct page *page = s->cpu_slab[cpu];
  2599. int node;
  2600. if (page) {
  2601. node = page_to_nid(page);
  2602. if (flags & SO_CPU) {
  2603. int x = 0;
  2604. if (flags & SO_OBJECTS)
  2605. x = page->inuse;
  2606. else
  2607. x = 1;
  2608. total += x;
  2609. nodes[node] += x;
  2610. }
  2611. per_cpu[node]++;
  2612. }
  2613. }
  2614. for_each_online_node(node) {
  2615. struct kmem_cache_node *n = get_node(s, node);
  2616. if (flags & SO_PARTIAL) {
  2617. if (flags & SO_OBJECTS)
  2618. x = count_partial(n);
  2619. else
  2620. x = n->nr_partial;
  2621. total += x;
  2622. nodes[node] += x;
  2623. }
  2624. if (flags & SO_FULL) {
  2625. int full_slabs = atomic_read(&n->nr_slabs)
  2626. - per_cpu[node]
  2627. - n->nr_partial;
  2628. if (flags & SO_OBJECTS)
  2629. x = full_slabs * s->objects;
  2630. else
  2631. x = full_slabs;
  2632. total += x;
  2633. nodes[node] += x;
  2634. }
  2635. }
  2636. x = sprintf(buf, "%lu", total);
  2637. #ifdef CONFIG_NUMA
  2638. for_each_online_node(node)
  2639. if (nodes[node])
  2640. x += sprintf(buf + x, " N%d=%lu",
  2641. node, nodes[node]);
  2642. #endif
  2643. kfree(nodes);
  2644. return x + sprintf(buf + x, "\n");
  2645. }
  2646. static int any_slab_objects(struct kmem_cache *s)
  2647. {
  2648. int node;
  2649. int cpu;
  2650. for_each_possible_cpu(cpu)
  2651. if (s->cpu_slab[cpu])
  2652. return 1;
  2653. for_each_node(node) {
  2654. struct kmem_cache_node *n = get_node(s, node);
  2655. if (n->nr_partial || atomic_read(&n->nr_slabs))
  2656. return 1;
  2657. }
  2658. return 0;
  2659. }
  2660. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  2661. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  2662. struct slab_attribute {
  2663. struct attribute attr;
  2664. ssize_t (*show)(struct kmem_cache *s, char *buf);
  2665. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  2666. };
  2667. #define SLAB_ATTR_RO(_name) \
  2668. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  2669. #define SLAB_ATTR(_name) \
  2670. static struct slab_attribute _name##_attr = \
  2671. __ATTR(_name, 0644, _name##_show, _name##_store)
  2672. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  2673. {
  2674. return sprintf(buf, "%d\n", s->size);
  2675. }
  2676. SLAB_ATTR_RO(slab_size);
  2677. static ssize_t align_show(struct kmem_cache *s, char *buf)
  2678. {
  2679. return sprintf(buf, "%d\n", s->align);
  2680. }
  2681. SLAB_ATTR_RO(align);
  2682. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  2683. {
  2684. return sprintf(buf, "%d\n", s->objsize);
  2685. }
  2686. SLAB_ATTR_RO(object_size);
  2687. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  2688. {
  2689. return sprintf(buf, "%d\n", s->objects);
  2690. }
  2691. SLAB_ATTR_RO(objs_per_slab);
  2692. static ssize_t order_show(struct kmem_cache *s, char *buf)
  2693. {
  2694. return sprintf(buf, "%d\n", s->order);
  2695. }
  2696. SLAB_ATTR_RO(order);
  2697. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  2698. {
  2699. if (s->ctor) {
  2700. int n = sprint_symbol(buf, (unsigned long)s->ctor);
  2701. return n + sprintf(buf + n, "\n");
  2702. }
  2703. return 0;
  2704. }
  2705. SLAB_ATTR_RO(ctor);
  2706. static ssize_t dtor_show(struct kmem_cache *s, char *buf)
  2707. {
  2708. if (s->dtor) {
  2709. int n = sprint_symbol(buf, (unsigned long)s->dtor);
  2710. return n + sprintf(buf + n, "\n");
  2711. }
  2712. return 0;
  2713. }
  2714. SLAB_ATTR_RO(dtor);
  2715. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  2716. {
  2717. return sprintf(buf, "%d\n", s->refcount - 1);
  2718. }
  2719. SLAB_ATTR_RO(aliases);
  2720. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  2721. {
  2722. return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
  2723. }
  2724. SLAB_ATTR_RO(slabs);
  2725. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  2726. {
  2727. return slab_objects(s, buf, SO_PARTIAL);
  2728. }
  2729. SLAB_ATTR_RO(partial);
  2730. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  2731. {
  2732. return slab_objects(s, buf, SO_CPU);
  2733. }
  2734. SLAB_ATTR_RO(cpu_slabs);
  2735. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  2736. {
  2737. return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
  2738. }
  2739. SLAB_ATTR_RO(objects);
  2740. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  2741. {
  2742. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  2743. }
  2744. static ssize_t sanity_checks_store(struct kmem_cache *s,
  2745. const char *buf, size_t length)
  2746. {
  2747. s->flags &= ~SLAB_DEBUG_FREE;
  2748. if (buf[0] == '1')
  2749. s->flags |= SLAB_DEBUG_FREE;
  2750. return length;
  2751. }
  2752. SLAB_ATTR(sanity_checks);
  2753. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  2754. {
  2755. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  2756. }
  2757. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  2758. size_t length)
  2759. {
  2760. s->flags &= ~SLAB_TRACE;
  2761. if (buf[0] == '1')
  2762. s->flags |= SLAB_TRACE;
  2763. return length;
  2764. }
  2765. SLAB_ATTR(trace);
  2766. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  2767. {
  2768. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  2769. }
  2770. static ssize_t reclaim_account_store(struct kmem_cache *s,
  2771. const char *buf, size_t length)
  2772. {
  2773. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  2774. if (buf[0] == '1')
  2775. s->flags |= SLAB_RECLAIM_ACCOUNT;
  2776. return length;
  2777. }
  2778. SLAB_ATTR(reclaim_account);
  2779. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  2780. {
  2781. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  2782. }
  2783. SLAB_ATTR_RO(hwcache_align);
  2784. #ifdef CONFIG_ZONE_DMA
  2785. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  2786. {
  2787. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  2788. }
  2789. SLAB_ATTR_RO(cache_dma);
  2790. #endif
  2791. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  2792. {
  2793. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  2794. }
  2795. SLAB_ATTR_RO(destroy_by_rcu);
  2796. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  2797. {
  2798. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  2799. }
  2800. static ssize_t red_zone_store(struct kmem_cache *s,
  2801. const char *buf, size_t length)
  2802. {
  2803. if (any_slab_objects(s))
  2804. return -EBUSY;
  2805. s->flags &= ~SLAB_RED_ZONE;
  2806. if (buf[0] == '1')
  2807. s->flags |= SLAB_RED_ZONE;
  2808. calculate_sizes(s);
  2809. return length;
  2810. }
  2811. SLAB_ATTR(red_zone);
  2812. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  2813. {
  2814. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  2815. }
  2816. static ssize_t poison_store(struct kmem_cache *s,
  2817. const char *buf, size_t length)
  2818. {
  2819. if (any_slab_objects(s))
  2820. return -EBUSY;
  2821. s->flags &= ~SLAB_POISON;
  2822. if (buf[0] == '1')
  2823. s->flags |= SLAB_POISON;
  2824. calculate_sizes(s);
  2825. return length;
  2826. }
  2827. SLAB_ATTR(poison);
  2828. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  2829. {
  2830. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  2831. }
  2832. static ssize_t store_user_store(struct kmem_cache *s,
  2833. const char *buf, size_t length)
  2834. {
  2835. if (any_slab_objects(s))
  2836. return -EBUSY;
  2837. s->flags &= ~SLAB_STORE_USER;
  2838. if (buf[0] == '1')
  2839. s->flags |= SLAB_STORE_USER;
  2840. calculate_sizes(s);
  2841. return length;
  2842. }
  2843. SLAB_ATTR(store_user);
  2844. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  2845. {
  2846. return 0;
  2847. }
  2848. static ssize_t validate_store(struct kmem_cache *s,
  2849. const char *buf, size_t length)
  2850. {
  2851. if (buf[0] == '1')
  2852. validate_slab_cache(s);
  2853. else
  2854. return -EINVAL;
  2855. return length;
  2856. }
  2857. SLAB_ATTR(validate);
  2858. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  2859. {
  2860. return 0;
  2861. }
  2862. static ssize_t shrink_store(struct kmem_cache *s,
  2863. const char *buf, size_t length)
  2864. {
  2865. if (buf[0] == '1') {
  2866. int rc = kmem_cache_shrink(s);
  2867. if (rc)
  2868. return rc;
  2869. } else
  2870. return -EINVAL;
  2871. return length;
  2872. }
  2873. SLAB_ATTR(shrink);
  2874. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  2875. {
  2876. if (!(s->flags & SLAB_STORE_USER))
  2877. return -ENOSYS;
  2878. return list_locations(s, buf, TRACK_ALLOC);
  2879. }
  2880. SLAB_ATTR_RO(alloc_calls);
  2881. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  2882. {
  2883. if (!(s->flags & SLAB_STORE_USER))
  2884. return -ENOSYS;
  2885. return list_locations(s, buf, TRACK_FREE);
  2886. }
  2887. SLAB_ATTR_RO(free_calls);
  2888. #ifdef CONFIG_NUMA
  2889. static ssize_t defrag_ratio_show(struct kmem_cache *s, char *buf)
  2890. {
  2891. return sprintf(buf, "%d\n", s->defrag_ratio / 10);
  2892. }
  2893. static ssize_t defrag_ratio_store(struct kmem_cache *s,
  2894. const char *buf, size_t length)
  2895. {
  2896. int n = simple_strtoul(buf, NULL, 10);
  2897. if (n < 100)
  2898. s->defrag_ratio = n * 10;
  2899. return length;
  2900. }
  2901. SLAB_ATTR(defrag_ratio);
  2902. #endif
  2903. static struct attribute * slab_attrs[] = {
  2904. &slab_size_attr.attr,
  2905. &object_size_attr.attr,
  2906. &objs_per_slab_attr.attr,
  2907. &order_attr.attr,
  2908. &objects_attr.attr,
  2909. &slabs_attr.attr,
  2910. &partial_attr.attr,
  2911. &cpu_slabs_attr.attr,
  2912. &ctor_attr.attr,
  2913. &dtor_attr.attr,
  2914. &aliases_attr.attr,
  2915. &align_attr.attr,
  2916. &sanity_checks_attr.attr,
  2917. &trace_attr.attr,
  2918. &hwcache_align_attr.attr,
  2919. &reclaim_account_attr.attr,
  2920. &destroy_by_rcu_attr.attr,
  2921. &red_zone_attr.attr,
  2922. &poison_attr.attr,
  2923. &store_user_attr.attr,
  2924. &validate_attr.attr,
  2925. &shrink_attr.attr,
  2926. &alloc_calls_attr.attr,
  2927. &free_calls_attr.attr,
  2928. #ifdef CONFIG_ZONE_DMA
  2929. &cache_dma_attr.attr,
  2930. #endif
  2931. #ifdef CONFIG_NUMA
  2932. &defrag_ratio_attr.attr,
  2933. #endif
  2934. NULL
  2935. };
  2936. static struct attribute_group slab_attr_group = {
  2937. .attrs = slab_attrs,
  2938. };
  2939. static ssize_t slab_attr_show(struct kobject *kobj,
  2940. struct attribute *attr,
  2941. char *buf)
  2942. {
  2943. struct slab_attribute *attribute;
  2944. struct kmem_cache *s;
  2945. int err;
  2946. attribute = to_slab_attr(attr);
  2947. s = to_slab(kobj);
  2948. if (!attribute->show)
  2949. return -EIO;
  2950. err = attribute->show(s, buf);
  2951. return err;
  2952. }
  2953. static ssize_t slab_attr_store(struct kobject *kobj,
  2954. struct attribute *attr,
  2955. const char *buf, size_t len)
  2956. {
  2957. struct slab_attribute *attribute;
  2958. struct kmem_cache *s;
  2959. int err;
  2960. attribute = to_slab_attr(attr);
  2961. s = to_slab(kobj);
  2962. if (!attribute->store)
  2963. return -EIO;
  2964. err = attribute->store(s, buf, len);
  2965. return err;
  2966. }
  2967. static struct sysfs_ops slab_sysfs_ops = {
  2968. .show = slab_attr_show,
  2969. .store = slab_attr_store,
  2970. };
  2971. static struct kobj_type slab_ktype = {
  2972. .sysfs_ops = &slab_sysfs_ops,
  2973. };
  2974. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  2975. {
  2976. struct kobj_type *ktype = get_ktype(kobj);
  2977. if (ktype == &slab_ktype)
  2978. return 1;
  2979. return 0;
  2980. }
  2981. static struct kset_uevent_ops slab_uevent_ops = {
  2982. .filter = uevent_filter,
  2983. };
  2984. decl_subsys(slab, &slab_ktype, &slab_uevent_ops);
  2985. #define ID_STR_LENGTH 64
  2986. /* Create a unique string id for a slab cache:
  2987. * format
  2988. * :[flags-]size:[memory address of kmemcache]
  2989. */
  2990. static char *create_unique_id(struct kmem_cache *s)
  2991. {
  2992. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  2993. char *p = name;
  2994. BUG_ON(!name);
  2995. *p++ = ':';
  2996. /*
  2997. * First flags affecting slabcache operations. We will only
  2998. * get here for aliasable slabs so we do not need to support
  2999. * too many flags. The flags here must cover all flags that
  3000. * are matched during merging to guarantee that the id is
  3001. * unique.
  3002. */
  3003. if (s->flags & SLAB_CACHE_DMA)
  3004. *p++ = 'd';
  3005. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3006. *p++ = 'a';
  3007. if (s->flags & SLAB_DEBUG_FREE)
  3008. *p++ = 'F';
  3009. if (p != name + 1)
  3010. *p++ = '-';
  3011. p += sprintf(p, "%07d", s->size);
  3012. BUG_ON(p > name + ID_STR_LENGTH - 1);
  3013. return name;
  3014. }
  3015. static int sysfs_slab_add(struct kmem_cache *s)
  3016. {
  3017. int err;
  3018. const char *name;
  3019. int unmergeable;
  3020. if (slab_state < SYSFS)
  3021. /* Defer until later */
  3022. return 0;
  3023. unmergeable = slab_unmergeable(s);
  3024. if (unmergeable) {
  3025. /*
  3026. * Slabcache can never be merged so we can use the name proper.
  3027. * This is typically the case for debug situations. In that
  3028. * case we can catch duplicate names easily.
  3029. */
  3030. sysfs_remove_link(&slab_subsys.kobj, s->name);
  3031. name = s->name;
  3032. } else {
  3033. /*
  3034. * Create a unique name for the slab as a target
  3035. * for the symlinks.
  3036. */
  3037. name = create_unique_id(s);
  3038. }
  3039. kobj_set_kset_s(s, slab_subsys);
  3040. kobject_set_name(&s->kobj, name);
  3041. kobject_init(&s->kobj);
  3042. err = kobject_add(&s->kobj);
  3043. if (err)
  3044. return err;
  3045. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  3046. if (err)
  3047. return err;
  3048. kobject_uevent(&s->kobj, KOBJ_ADD);
  3049. if (!unmergeable) {
  3050. /* Setup first alias */
  3051. sysfs_slab_alias(s, s->name);
  3052. kfree(name);
  3053. }
  3054. return 0;
  3055. }
  3056. static void sysfs_slab_remove(struct kmem_cache *s)
  3057. {
  3058. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  3059. kobject_del(&s->kobj);
  3060. }
  3061. /*
  3062. * Need to buffer aliases during bootup until sysfs becomes
  3063. * available lest we loose that information.
  3064. */
  3065. struct saved_alias {
  3066. struct kmem_cache *s;
  3067. const char *name;
  3068. struct saved_alias *next;
  3069. };
  3070. struct saved_alias *alias_list;
  3071. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  3072. {
  3073. struct saved_alias *al;
  3074. if (slab_state == SYSFS) {
  3075. /*
  3076. * If we have a leftover link then remove it.
  3077. */
  3078. sysfs_remove_link(&slab_subsys.kobj, name);
  3079. return sysfs_create_link(&slab_subsys.kobj,
  3080. &s->kobj, name);
  3081. }
  3082. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  3083. if (!al)
  3084. return -ENOMEM;
  3085. al->s = s;
  3086. al->name = name;
  3087. al->next = alias_list;
  3088. alias_list = al;
  3089. return 0;
  3090. }
  3091. static int __init slab_sysfs_init(void)
  3092. {
  3093. struct list_head *h;
  3094. int err;
  3095. err = subsystem_register(&slab_subsys);
  3096. if (err) {
  3097. printk(KERN_ERR "Cannot register slab subsystem.\n");
  3098. return -ENOSYS;
  3099. }
  3100. slab_state = SYSFS;
  3101. list_for_each(h, &slab_caches) {
  3102. struct kmem_cache *s =
  3103. container_of(h, struct kmem_cache, list);
  3104. err = sysfs_slab_add(s);
  3105. BUG_ON(err);
  3106. }
  3107. while (alias_list) {
  3108. struct saved_alias *al = alias_list;
  3109. alias_list = alias_list->next;
  3110. err = sysfs_slab_alias(al->s, al->name);
  3111. BUG_ON(err);
  3112. kfree(al);
  3113. }
  3114. resiliency_test();
  3115. return 0;
  3116. }
  3117. __initcall(slab_sysfs_init);
  3118. #endif