memory-alloc.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #ifndef VDO_MEMORY_ALLOC_H
  6. #define VDO_MEMORY_ALLOC_H
  7. #include <linux/cache.h>
  8. #include <linux/io.h> /* for PAGE_SIZE */
  9. #include "permassert.h"
  10. #include "thread-registry.h"
  11. /* Custom memory allocation function that tracks memory usage */
  12. int __must_check vdo_allocate_memory(size_t size, size_t align, const char *what, void *ptr);
  13. /*
  14. * Allocate storage based on element counts, sizes, and alignment.
  15. *
  16. * This is a generalized form of our allocation use case: It allocates an array of objects,
  17. * optionally preceded by one object of another type (i.e., a struct with trailing variable-length
  18. * array), with the alignment indicated.
  19. *
  20. * Why is this inline? The sizes and alignment will always be constant, when invoked through the
  21. * macros below, and often the count will be a compile-time constant 1 or the number of extra bytes
  22. * will be a compile-time constant 0. So at least some of the arithmetic can usually be optimized
  23. * away, and the run-time selection between allocation functions always can. In many cases, it'll
  24. * boil down to just a function call with a constant size.
  25. *
  26. * @count: The number of objects to allocate
  27. * @size: The size of an object
  28. * @extra: The number of additional bytes to allocate
  29. * @align: The required alignment
  30. * @what: What is being allocated (for error logging)
  31. * @ptr: A pointer to hold the allocated memory
  32. *
  33. * Return: VDO_SUCCESS or an error code
  34. */
  35. static inline int __vdo_do_allocation(size_t count, size_t size, size_t extra,
  36. size_t align, const char *what, void *ptr)
  37. {
  38. size_t total_size = count * size + extra;
  39. /* Overflow check: */
  40. if ((size > 0) && (count > ((SIZE_MAX - extra) / size))) {
  41. /*
  42. * This is kind of a hack: We rely on the fact that SIZE_MAX would cover the entire
  43. * address space (minus one byte) and thus the system can never allocate that much
  44. * and the call will always fail. So we can report an overflow as "out of memory"
  45. * by asking for "merely" SIZE_MAX bytes.
  46. */
  47. total_size = SIZE_MAX;
  48. }
  49. return vdo_allocate_memory(total_size, align, what, ptr);
  50. }
  51. /*
  52. * Allocate one or more elements of the indicated type, logging an error if the allocation fails.
  53. * The memory will be zeroed.
  54. *
  55. * @COUNT: The number of objects to allocate
  56. * @TYPE: The type of objects to allocate. This type determines the alignment of the allocation.
  57. * @WHAT: What is being allocated (for error logging)
  58. * @PTR: A pointer to hold the allocated memory
  59. *
  60. * Return: VDO_SUCCESS or an error code
  61. */
  62. #define vdo_allocate(COUNT, TYPE, WHAT, PTR) \
  63. __vdo_do_allocation(COUNT, sizeof(TYPE), 0, __alignof__(TYPE), WHAT, PTR)
  64. /*
  65. * Allocate one object of an indicated type, followed by one or more elements of a second type,
  66. * logging an error if the allocation fails. The memory will be zeroed.
  67. *
  68. * @TYPE1: The type of the primary object to allocate. This type determines the alignment of the
  69. * allocated memory.
  70. * @COUNT: The number of objects to allocate
  71. * @TYPE2: The type of array objects to allocate
  72. * @WHAT: What is being allocated (for error logging)
  73. * @PTR: A pointer to hold the allocated memory
  74. *
  75. * Return: VDO_SUCCESS or an error code
  76. */
  77. #define vdo_allocate_extended(TYPE1, COUNT, TYPE2, WHAT, PTR) \
  78. __extension__({ \
  79. int _result; \
  80. TYPE1 **_ptr = (PTR); \
  81. BUILD_BUG_ON(__alignof__(TYPE1) < __alignof__(TYPE2)); \
  82. _result = __vdo_do_allocation(COUNT, \
  83. sizeof(TYPE2), \
  84. sizeof(TYPE1), \
  85. __alignof__(TYPE1), \
  86. WHAT, \
  87. _ptr); \
  88. _result; \
  89. })
  90. /*
  91. * Allocate memory starting on a cache line boundary, logging an error if the allocation fails. The
  92. * memory will be zeroed.
  93. *
  94. * @size: The number of bytes to allocate
  95. * @what: What is being allocated (for error logging)
  96. * @ptr: A pointer to hold the allocated memory
  97. *
  98. * Return: VDO_SUCCESS or an error code
  99. */
  100. static inline int __must_check vdo_allocate_cache_aligned(size_t size, const char *what, void *ptr)
  101. {
  102. return vdo_allocate_memory(size, L1_CACHE_BYTES, what, ptr);
  103. }
  104. /*
  105. * Allocate one element of the indicated type immediately, failing if the required memory is not
  106. * immediately available.
  107. *
  108. * @size: The number of bytes to allocate
  109. * @what: What is being allocated (for error logging)
  110. *
  111. * Return: pointer to the memory, or NULL if the memory is not available.
  112. */
  113. void *__must_check vdo_allocate_memory_nowait(size_t size, const char *what);
  114. int __must_check vdo_reallocate_memory(void *ptr, size_t old_size, size_t size,
  115. const char *what, void *new_ptr);
  116. int __must_check vdo_duplicate_string(const char *string, const char *what,
  117. char **new_string);
  118. /* Free memory allocated with vdo_allocate(). */
  119. void vdo_free(void *ptr);
  120. static inline void *__vdo_forget(void **ptr_ptr)
  121. {
  122. void *ptr = *ptr_ptr;
  123. *ptr_ptr = NULL;
  124. return ptr;
  125. }
  126. /*
  127. * Null out a pointer and return a copy to it. This macro should be used when passing a pointer to
  128. * a function for which it is not safe to access the pointer once the function returns.
  129. */
  130. #define vdo_forget(ptr) __vdo_forget((void **) &(ptr))
  131. void vdo_memory_init(void);
  132. void vdo_memory_exit(void);
  133. void vdo_register_allocating_thread(struct registered_thread *new_thread,
  134. const bool *flag_ptr);
  135. void vdo_unregister_allocating_thread(void);
  136. void vdo_get_memory_stats(u64 *bytes_used, u64 *peak_bytes_used);
  137. void vdo_report_memory_usage(void);
  138. #endif /* VDO_MEMORY_ALLOC_H */