Back to home page

LXR

 
 

    


0001 #ifndef MM_SLAB_H
0002 #define MM_SLAB_H
0003 /*
0004  * Internal slab definitions
0005  */
0006 
0007 #ifdef CONFIG_SLOB
0008 /*
0009  * Common fields provided in kmem_cache by all slab allocators
0010  * This struct is either used directly by the allocator (SLOB)
0011  * or the allocator must include definitions for all fields
0012  * provided in kmem_cache_common in their definition of kmem_cache.
0013  *
0014  * Once we can do anonymous structs (C11 standard) we could put a
0015  * anonymous struct definition in these allocators so that the
0016  * separate allocations in the kmem_cache structure of SLAB and
0017  * SLUB is no longer needed.
0018  */
0019 struct kmem_cache {
0020     unsigned int object_size;/* The original size of the object */
0021     unsigned int size;  /* The aligned/padded/added on size  */
0022     unsigned int align; /* Alignment as calculated */
0023     unsigned long flags;    /* Active flags on the slab */
0024     const char *name;   /* Slab name for sysfs */
0025     int refcount;       /* Use counter */
0026     void (*ctor)(void *);   /* Called on object slot creation */
0027     struct list_head list;  /* List of all slab caches on the system */
0028 };
0029 
0030 #endif /* CONFIG_SLOB */
0031 
0032 #ifdef CONFIG_SLAB
0033 #include <linux/slab_def.h>
0034 #endif
0035 
0036 #ifdef CONFIG_SLUB
0037 #include <linux/slub_def.h>
0038 #endif
0039 
0040 #include <linux/memcontrol.h>
0041 #include <linux/fault-inject.h>
0042 #include <linux/kmemcheck.h>
0043 #include <linux/kasan.h>
0044 #include <linux/kmemleak.h>
0045 #include <linux/random.h>
0046 
0047 /*
0048  * State of the slab allocator.
0049  *
0050  * This is used to describe the states of the allocator during bootup.
0051  * Allocators use this to gradually bootstrap themselves. Most allocators
0052  * have the problem that the structures used for managing slab caches are
0053  * allocated from slab caches themselves.
0054  */
0055 enum slab_state {
0056     DOWN,           /* No slab functionality yet */
0057     PARTIAL,        /* SLUB: kmem_cache_node available */
0058     PARTIAL_NODE,       /* SLAB: kmalloc size for node struct available */
0059     UP,         /* Slab caches usable but not all extras yet */
0060     FULL            /* Everything is working */
0061 };
0062 
0063 extern enum slab_state slab_state;
0064 
0065 /* The slab cache mutex protects the management structures during changes */
0066 extern struct mutex slab_mutex;
0067 
0068 /* The list of all slab caches on the system */
0069 extern struct list_head slab_caches;
0070 
0071 /* The slab cache that manages slab cache information */
0072 extern struct kmem_cache *kmem_cache;
0073 
0074 unsigned long calculate_alignment(unsigned long flags,
0075         unsigned long align, unsigned long size);
0076 
0077 #ifndef CONFIG_SLOB
0078 /* Kmalloc array related functions */
0079 void setup_kmalloc_cache_index_table(void);
0080 void create_kmalloc_caches(unsigned long);
0081 
0082 /* Find the kmalloc slab corresponding for a certain size */
0083 struct kmem_cache *kmalloc_slab(size_t, gfp_t);
0084 #endif
0085 
0086 
0087 /* Functions provided by the slab allocators */
0088 extern int __kmem_cache_create(struct kmem_cache *, unsigned long flags);
0089 
0090 extern struct kmem_cache *create_kmalloc_cache(const char *name, size_t size,
0091             unsigned long flags);
0092 extern void create_boot_cache(struct kmem_cache *, const char *name,
0093             size_t size, unsigned long flags);
0094 
0095 int slab_unmergeable(struct kmem_cache *s);
0096 struct kmem_cache *find_mergeable(size_t size, size_t align,
0097         unsigned long flags, const char *name, void (*ctor)(void *));
0098 #ifndef CONFIG_SLOB
0099 struct kmem_cache *
0100 __kmem_cache_alias(const char *name, size_t size, size_t align,
0101            unsigned long flags, void (*ctor)(void *));
0102 
0103 unsigned long kmem_cache_flags(unsigned long object_size,
0104     unsigned long flags, const char *name,
0105     void (*ctor)(void *));
0106 #else
0107 static inline struct kmem_cache *
0108 __kmem_cache_alias(const char *name, size_t size, size_t align,
0109            unsigned long flags, void (*ctor)(void *))
0110 { return NULL; }
0111 
0112 static inline unsigned long kmem_cache_flags(unsigned long object_size,
0113     unsigned long flags, const char *name,
0114     void (*ctor)(void *))
0115 {
0116     return flags;
0117 }
0118 #endif
0119 
0120 
0121 /* Legal flag mask for kmem_cache_create(), for various configurations */
0122 #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | SLAB_PANIC | \
0123              SLAB_DESTROY_BY_RCU | SLAB_DEBUG_OBJECTS )
0124 
0125 #if defined(CONFIG_DEBUG_SLAB)
0126 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
0127 #elif defined(CONFIG_SLUB_DEBUG)
0128 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
0129               SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
0130 #else
0131 #define SLAB_DEBUG_FLAGS (0)
0132 #endif
0133 
0134 #if defined(CONFIG_SLAB)
0135 #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
0136               SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
0137               SLAB_NOTRACK | SLAB_ACCOUNT)
0138 #elif defined(CONFIG_SLUB)
0139 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
0140               SLAB_TEMPORARY | SLAB_NOTRACK | SLAB_ACCOUNT)
0141 #else
0142 #define SLAB_CACHE_FLAGS (0)
0143 #endif
0144 
0145 /* Common flags available with current configuration */
0146 #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
0147 
0148 /* Common flags permitted for kmem_cache_create */
0149 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
0150                   SLAB_RED_ZONE | \
0151                   SLAB_POISON | \
0152                   SLAB_STORE_USER | \
0153                   SLAB_TRACE | \
0154                   SLAB_CONSISTENCY_CHECKS | \
0155                   SLAB_MEM_SPREAD | \
0156                   SLAB_NOLEAKTRACE | \
0157                   SLAB_RECLAIM_ACCOUNT | \
0158                   SLAB_TEMPORARY | \
0159                   SLAB_NOTRACK | \
0160                   SLAB_ACCOUNT)
0161 
0162 int __kmem_cache_shutdown(struct kmem_cache *);
0163 void __kmem_cache_release(struct kmem_cache *);
0164 int __kmem_cache_shrink(struct kmem_cache *);
0165 void slab_kmem_cache_release(struct kmem_cache *);
0166 
0167 struct seq_file;
0168 struct file;
0169 
0170 struct slabinfo {
0171     unsigned long active_objs;
0172     unsigned long num_objs;
0173     unsigned long active_slabs;
0174     unsigned long num_slabs;
0175     unsigned long shared_avail;
0176     unsigned int limit;
0177     unsigned int batchcount;
0178     unsigned int shared;
0179     unsigned int objects_per_slab;
0180     unsigned int cache_order;
0181 };
0182 
0183 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
0184 void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
0185 ssize_t slabinfo_write(struct file *file, const char __user *buffer,
0186                size_t count, loff_t *ppos);
0187 
0188 /*
0189  * Generic implementation of bulk operations
0190  * These are useful for situations in which the allocator cannot
0191  * perform optimizations. In that case segments of the object listed
0192  * may be allocated or freed using these operations.
0193  */
0194 void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
0195 int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
0196 
0197 #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
0198 /*
0199  * Iterate over all memcg caches of the given root cache. The caller must hold
0200  * slab_mutex.
0201  */
0202 #define for_each_memcg_cache(iter, root) \
0203     list_for_each_entry(iter, &(root)->memcg_params.list, \
0204                 memcg_params.list)
0205 
0206 static inline bool is_root_cache(struct kmem_cache *s)
0207 {
0208     return s->memcg_params.is_root_cache;
0209 }
0210 
0211 static inline bool slab_equal_or_root(struct kmem_cache *s,
0212                       struct kmem_cache *p)
0213 {
0214     return p == s || p == s->memcg_params.root_cache;
0215 }
0216 
0217 /*
0218  * We use suffixes to the name in memcg because we can't have caches
0219  * created in the system with the same name. But when we print them
0220  * locally, better refer to them with the base name
0221  */
0222 static inline const char *cache_name(struct kmem_cache *s)
0223 {
0224     if (!is_root_cache(s))
0225         s = s->memcg_params.root_cache;
0226     return s->name;
0227 }
0228 
0229 /*
0230  * Note, we protect with RCU only the memcg_caches array, not per-memcg caches.
0231  * That said the caller must assure the memcg's cache won't go away by either
0232  * taking a css reference to the owner cgroup, or holding the slab_mutex.
0233  */
0234 static inline struct kmem_cache *
0235 cache_from_memcg_idx(struct kmem_cache *s, int idx)
0236 {
0237     struct kmem_cache *cachep;
0238     struct memcg_cache_array *arr;
0239 
0240     rcu_read_lock();
0241     arr = rcu_dereference(s->memcg_params.memcg_caches);
0242 
0243     /*
0244      * Make sure we will access the up-to-date value. The code updating
0245      * memcg_caches issues a write barrier to match this (see
0246      * memcg_create_kmem_cache()).
0247      */
0248     cachep = lockless_dereference(arr->entries[idx]);
0249     rcu_read_unlock();
0250 
0251     return cachep;
0252 }
0253 
0254 static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
0255 {
0256     if (is_root_cache(s))
0257         return s;
0258     return s->memcg_params.root_cache;
0259 }
0260 
0261 static __always_inline int memcg_charge_slab(struct page *page,
0262                          gfp_t gfp, int order,
0263                          struct kmem_cache *s)
0264 {
0265     int ret;
0266 
0267     if (!memcg_kmem_enabled())
0268         return 0;
0269     if (is_root_cache(s))
0270         return 0;
0271 
0272     ret = memcg_kmem_charge_memcg(page, gfp, order, s->memcg_params.memcg);
0273     if (ret)
0274         return ret;
0275 
0276     memcg_kmem_update_page_stat(page,
0277             (s->flags & SLAB_RECLAIM_ACCOUNT) ?
0278             MEMCG_SLAB_RECLAIMABLE : MEMCG_SLAB_UNRECLAIMABLE,
0279             1 << order);
0280     return 0;
0281 }
0282 
0283 static __always_inline void memcg_uncharge_slab(struct page *page, int order,
0284                         struct kmem_cache *s)
0285 {
0286     if (!memcg_kmem_enabled())
0287         return;
0288 
0289     memcg_kmem_update_page_stat(page,
0290             (s->flags & SLAB_RECLAIM_ACCOUNT) ?
0291             MEMCG_SLAB_RECLAIMABLE : MEMCG_SLAB_UNRECLAIMABLE,
0292             -(1 << order));
0293     memcg_kmem_uncharge(page, order);
0294 }
0295 
0296 extern void slab_init_memcg_params(struct kmem_cache *);
0297 
0298 #else /* CONFIG_MEMCG && !CONFIG_SLOB */
0299 
0300 #define for_each_memcg_cache(iter, root) \
0301     for ((void)(iter), (void)(root); 0; )
0302 
0303 static inline bool is_root_cache(struct kmem_cache *s)
0304 {
0305     return true;
0306 }
0307 
0308 static inline bool slab_equal_or_root(struct kmem_cache *s,
0309                       struct kmem_cache *p)
0310 {
0311     return true;
0312 }
0313 
0314 static inline const char *cache_name(struct kmem_cache *s)
0315 {
0316     return s->name;
0317 }
0318 
0319 static inline struct kmem_cache *
0320 cache_from_memcg_idx(struct kmem_cache *s, int idx)
0321 {
0322     return NULL;
0323 }
0324 
0325 static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
0326 {
0327     return s;
0328 }
0329 
0330 static inline int memcg_charge_slab(struct page *page, gfp_t gfp, int order,
0331                     struct kmem_cache *s)
0332 {
0333     return 0;
0334 }
0335 
0336 static inline void memcg_uncharge_slab(struct page *page, int order,
0337                        struct kmem_cache *s)
0338 {
0339 }
0340 
0341 static inline void slab_init_memcg_params(struct kmem_cache *s)
0342 {
0343 }
0344 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
0345 
0346 static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
0347 {
0348     struct kmem_cache *cachep;
0349     struct page *page;
0350 
0351     /*
0352      * When kmemcg is not being used, both assignments should return the
0353      * same value. but we don't want to pay the assignment price in that
0354      * case. If it is not compiled in, the compiler should be smart enough
0355      * to not do even the assignment. In that case, slab_equal_or_root
0356      * will also be a constant.
0357      */
0358     if (!memcg_kmem_enabled() &&
0359         !unlikely(s->flags & SLAB_CONSISTENCY_CHECKS))
0360         return s;
0361 
0362     page = virt_to_head_page(x);
0363     cachep = page->slab_cache;
0364     if (slab_equal_or_root(cachep, s))
0365         return cachep;
0366 
0367     pr_err("%s: Wrong slab cache. %s but object is from %s\n",
0368            __func__, s->name, cachep->name);
0369     WARN_ON_ONCE(1);
0370     return s;
0371 }
0372 
0373 static inline size_t slab_ksize(const struct kmem_cache *s)
0374 {
0375 #ifndef CONFIG_SLUB
0376     return s->object_size;
0377 
0378 #else /* CONFIG_SLUB */
0379 # ifdef CONFIG_SLUB_DEBUG
0380     /*
0381      * Debugging requires use of the padding between object
0382      * and whatever may come after it.
0383      */
0384     if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
0385         return s->object_size;
0386 # endif
0387     if (s->flags & SLAB_KASAN)
0388         return s->object_size;
0389     /*
0390      * If we have the need to store the freelist pointer
0391      * back there or track user information then we can
0392      * only use the space before that information.
0393      */
0394     if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
0395         return s->inuse;
0396     /*
0397      * Else we can use all the padding etc for the allocation
0398      */
0399     return s->size;
0400 #endif
0401 }
0402 
0403 static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
0404                              gfp_t flags)
0405 {
0406     flags &= gfp_allowed_mask;
0407     lockdep_trace_alloc(flags);
0408     might_sleep_if(gfpflags_allow_blocking(flags));
0409 
0410     if (should_failslab(s, flags))
0411         return NULL;
0412 
0413     if (memcg_kmem_enabled() &&
0414         ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))
0415         return memcg_kmem_get_cache(s);
0416 
0417     return s;
0418 }
0419 
0420 static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
0421                     size_t size, void **p)
0422 {
0423     size_t i;
0424 
0425     flags &= gfp_allowed_mask;
0426     for (i = 0; i < size; i++) {
0427         void *object = p[i];
0428 
0429         kmemcheck_slab_alloc(s, flags, object, slab_ksize(s));
0430         kmemleak_alloc_recursive(object, s->object_size, 1,
0431                      s->flags, flags);
0432         kasan_slab_alloc(s, object, flags);
0433     }
0434 
0435     if (memcg_kmem_enabled())
0436         memcg_kmem_put_cache(s);
0437 }
0438 
0439 #ifndef CONFIG_SLOB
0440 /*
0441  * The slab lists for all objects.
0442  */
0443 struct kmem_cache_node {
0444     spinlock_t list_lock;
0445 
0446 #ifdef CONFIG_SLAB
0447     struct list_head slabs_partial; /* partial list first, better asm code */
0448     struct list_head slabs_full;
0449     struct list_head slabs_free;
0450     unsigned long total_slabs;  /* length of all slab lists */
0451     unsigned long free_slabs;   /* length of free slab list only */
0452     unsigned long free_objects;
0453     unsigned int free_limit;
0454     unsigned int colour_next;   /* Per-node cache coloring */
0455     struct array_cache *shared; /* shared per node */
0456     struct alien_cache **alien; /* on other nodes */
0457     unsigned long next_reap;    /* updated without locking */
0458     int free_touched;       /* updated without locking */
0459 #endif
0460 
0461 #ifdef CONFIG_SLUB
0462     unsigned long nr_partial;
0463     struct list_head partial;
0464 #ifdef CONFIG_SLUB_DEBUG
0465     atomic_long_t nr_slabs;
0466     atomic_long_t total_objects;
0467     struct list_head full;
0468 #endif
0469 #endif
0470 
0471 };
0472 
0473 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
0474 {
0475     return s->node[node];
0476 }
0477 
0478 /*
0479  * Iterator over all nodes. The body will be executed for each node that has
0480  * a kmem_cache_node structure allocated (which is true for all online nodes)
0481  */
0482 #define for_each_kmem_cache_node(__s, __node, __n) \
0483     for (__node = 0; __node < nr_node_ids; __node++) \
0484          if ((__n = get_node(__s, __node)))
0485 
0486 #endif
0487 
0488 void *slab_start(struct seq_file *m, loff_t *pos);
0489 void *slab_next(struct seq_file *m, void *p, loff_t *pos);
0490 void slab_stop(struct seq_file *m, void *p);
0491 int memcg_slab_show(struct seq_file *m, void *p);
0492 
0493 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
0494 
0495 #ifdef CONFIG_SLAB_FREELIST_RANDOM
0496 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
0497             gfp_t gfp);
0498 void cache_random_seq_destroy(struct kmem_cache *cachep);
0499 #else
0500 static inline int cache_random_seq_create(struct kmem_cache *cachep,
0501                     unsigned int count, gfp_t gfp)
0502 {
0503     return 0;
0504 }
0505 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
0506 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
0507 
0508 #endif /* MM_SLAB_H */