slab_def.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. struct kmem_cache *freelist_cache;
  28. unsigned int freelist_size;
  29. /* constructor func */
  30. void (*ctor)(void *obj);
  31. /* 4) cache creation/removal */
  32. const char *name;
  33. struct list_head list;
  34. int refcount;
  35. int object_size;
  36. int align;
  37. /* 5) statistics */
  38. #ifdef CONFIG_DEBUG_SLAB
  39. unsigned long num_active;
  40. unsigned long num_allocations;
  41. unsigned long high_mark;
  42. unsigned long grown;
  43. unsigned long reaped;
  44. unsigned long errors;
  45. unsigned long max_freeable;
  46. unsigned long node_allocs;
  47. unsigned long node_frees;
  48. unsigned long node_overflow;
  49. atomic_t allochit;
  50. atomic_t allocmiss;
  51. atomic_t freehit;
  52. atomic_t freemiss;
  53. /*
  54. * If debugging is enabled, then the allocator can add additional
  55. * fields and/or padding to every object. 'size' contains the total
  56. * object size including these internal fields, while 'obj_offset'
  57. * and 'object_size' contain the offset to the user object and its
  58. * size.
  59. */
  60. int obj_offset;
  61. #endif /* CONFIG_DEBUG_SLAB */
  62. #ifdef CONFIG_KASAN
  63. struct kasan_cache kasan_info;
  64. #endif
  65. #ifdef CONFIG_SLAB_FREELIST_RANDOM
  66. unsigned int *random_seq;
  67. #endif
  68. unsigned int useroffset; /* Usercopy region offset */
  69. unsigned int usersize; /* Usercopy region size */
  70. struct kmem_cache_node *node[MAX_NUMNODES];
  71. };
  72. static inline void *nearest_obj(struct kmem_cache *cache, struct page *page,
  73. void *x)
  74. {
  75. void *object = x - (x - page->s_mem) % cache->size;
  76. void *last_object = page->s_mem + (cache->num - 1) * cache->size;
  77. if (unlikely(object > last_object))
  78. return last_object;
  79. else
  80. return object;
  81. }
  82. /*
  83. * We want to avoid an expensive divide : (offset / cache->size)
  84. * Using the fact that size is a constant for a particular cache,
  85. * we can replace (offset / cache->size) by
  86. * reciprocal_divide(offset, cache->reciprocal_buffer_size)
  87. */
  88. static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  89. const struct page *page, void *obj)
  90. {
  91. u32 offset = (obj - page->s_mem);
  92. return reciprocal_divide(offset, cache->reciprocal_buffer_size);
  93. }
  94. static inline int objs_per_slab_page(const struct kmem_cache *cache,
  95. const struct page *page)
  96. {
  97. if (is_kfence_address(page_address(page)))
  98. return 1;
  99. return cache->num;
  100. }
  101. #endif /* _LINUX_SLAB_DEF_H */