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