slab_def.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SLAB_DEF_H
  3. #define _LINUX_SLAB_DEF_H
  4. #include <linux/kfence.h>
  5. #include <linux/reciprocal_div.h>
  6. /*
  7. * Definitions unique to the original Linux SLAB allocator.
  8. */
  9. struct kmem_cache {
  10. struct array_cache __percpu *cpu_cache;
  11. /* 1) Cache tunables. Protected by slab_mutex */
  12. unsigned int batchcount;
  13. unsigned int limit;
  14. unsigned int shared;
  15. unsigned int size;
  16. struct reciprocal_value reciprocal_buffer_size;
  17. /* 2) touched by every alloc & free from the backend */
  18. slab_flags_t flags; /* constant flags */
  19. unsigned int num; /* # of objs per slab */
  20. /* 3) cache_grow/shrink */
  21. /* order of pgs per slab (2^n) */
  22. unsigned int gfporder;
  23. /* force GFP flags, e.g. GFP_DMA */
  24. gfp_t allocflags;
  25. size_t colour; /* cache colouring range */
  26. unsigned int colour_off; /* colour offset */
  27. unsigned int freelist_size;
  28. /* constructor func */
  29. void (*ctor)(void *obj);
  30. /* 4) cache creation/removal */
  31. const char *name;
  32. struct list_head list;
  33. int refcount;
  34. int object_size;
  35. int align;
  36. /* 5) statistics */
  37. #ifdef CONFIG_DEBUG_SLAB
  38. unsigned long num_active;
  39. unsigned long num_allocations;
  40. unsigned long high_mark;
  41. unsigned long grown;
  42. unsigned long reaped;
  43. unsigned long errors;
  44. unsigned long max_freeable;
  45. unsigned long node_allocs;
  46. unsigned long node_frees;
  47. unsigned long node_overflow;
  48. atomic_t allochit;
  49. atomic_t allocmiss;
  50. atomic_t freehit;
  51. atomic_t freemiss;
  52. /*
  53. * If debugging is enabled, then the allocator can add additional
  54. * fields and/or padding to every object. 'size' contains the total
  55. * object size including these internal fields, while 'obj_offset'
  56. * and 'object_size' contain the offset to the user object and its
  57. * size.
  58. */
  59. int obj_offset;
  60. #endif /* CONFIG_DEBUG_SLAB */
  61. #ifdef CONFIG_KASAN
  62. struct kasan_cache kasan_info;
  63. #endif
  64. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  65. unsigned int *random_seq;
  66. #endif
  67. unsigned int useroffset; /* Usercopy region offset */
  68. unsigned int usersize; /* Usercopy region size */
  69. struct kmem_cache_node *node[MAX_NUMNODES];
  70. };
  71. static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *slab,
  72. void *x)
  73. {
  74. void *object = x - (x - slab->s_mem) % cache->size;
  75. void *last_object = slab->s_mem + (cache->num - 1) * cache->size;
  76. if (unlikely(object > last_object))
  77. return last_object;
  78. else
  79. return object;
  80. }
  81. /*
  82. * We want to avoid an expensive divide : (offset / cache->size)
  83. * Using the fact that size is a constant for a particular cache,
  84. * we can replace (offset / cache->size) by
  85. * reciprocal_divide(offset, cache->reciprocal_buffer_size)
  86. */
  87. static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  88. const struct slab *slab, void *obj)
  89. {
  90. u32 offset = (obj - slab->s_mem);
  91. return reciprocal_divide(offset, cache->reciprocal_buffer_size);
  92. }
  93. static inline int objs_per_slab(const struct kmem_cache *cache,
  94. const struct slab *slab)
  95. {
  96. if (is_kfence_address(slab_address(slab)))
  97. return 1;
  98. return cache->num;
  99. }
  100. #endif /* _LINUX_SLAB_DEF_H */