eal: use memseg walk instead of iteration
[dpdk.git] / lib / librte_eal / common / malloc_heap.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <stdint.h>
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <sys/queue.h>
11
12 #include <rte_memory.h>
13 #include <rte_eal.h>
14 #include <rte_eal_memconfig.h>
15 #include <rte_launch.h>
16 #include <rte_per_lcore.h>
17 #include <rte_lcore.h>
18 #include <rte_common.h>
19 #include <rte_string_fns.h>
20 #include <rte_spinlock.h>
21 #include <rte_memcpy.h>
22 #include <rte_atomic.h>
23
24 #include "malloc_elem.h"
25 #include "malloc_heap.h"
26
27 static unsigned
28 check_hugepage_sz(unsigned flags, uint64_t hugepage_sz)
29 {
30         unsigned check_flag = 0;
31
32         if (!(flags & ~RTE_MEMZONE_SIZE_HINT_ONLY))
33                 return 1;
34
35         switch (hugepage_sz) {
36         case RTE_PGSIZE_256K:
37                 check_flag = RTE_MEMZONE_256KB;
38                 break;
39         case RTE_PGSIZE_2M:
40                 check_flag = RTE_MEMZONE_2MB;
41                 break;
42         case RTE_PGSIZE_16M:
43                 check_flag = RTE_MEMZONE_16MB;
44                 break;
45         case RTE_PGSIZE_256M:
46                 check_flag = RTE_MEMZONE_256MB;
47                 break;
48         case RTE_PGSIZE_512M:
49                 check_flag = RTE_MEMZONE_512MB;
50                 break;
51         case RTE_PGSIZE_1G:
52                 check_flag = RTE_MEMZONE_1GB;
53                 break;
54         case RTE_PGSIZE_4G:
55                 check_flag = RTE_MEMZONE_4GB;
56                 break;
57         case RTE_PGSIZE_16G:
58                 check_flag = RTE_MEMZONE_16GB;
59         }
60
61         return check_flag & flags;
62 }
63
64 /*
65  * Expand the heap with a memseg.
66  * This reserves the zone and sets a dummy malloc_elem header at the end
67  * to prevent overflow. The rest of the zone is added to free list as a single
68  * large free block
69  */
70 static int
71 malloc_heap_add_memseg(const struct rte_memseg *ms, void *arg __rte_unused)
72 {
73         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
74         struct malloc_elem *start_elem;
75         struct rte_memseg *found_ms;
76         struct malloc_heap *heap;
77         size_t elem_size;
78         int ms_idx;
79
80         heap = &mcfg->malloc_heaps[ms->socket_id];
81
82         /* ms is const, so find it */
83         ms_idx = ms - mcfg->memseg;
84         found_ms = &mcfg->memseg[ms_idx];
85
86         start_elem = (struct malloc_elem *)found_ms->addr;
87         elem_size = ms->len - MALLOC_ELEM_OVERHEAD;
88
89         malloc_elem_init(start_elem, heap, found_ms, elem_size);
90         malloc_elem_insert(start_elem);
91         malloc_elem_free_list_insert(start_elem);
92
93         heap->total_size += elem_size;
94
95         return 0;
96 }
97
98 /*
99  * Iterates through the freelist for a heap to find a free element
100  * which can store data of the required size and with the requested alignment.
101  * If size is 0, find the biggest available elem.
102  * Returns null on failure, or pointer to element on success.
103  */
104 static struct malloc_elem *
105 find_suitable_element(struct malloc_heap *heap, size_t size,
106                 unsigned int flags, size_t align, size_t bound, bool contig)
107 {
108         size_t idx;
109         struct malloc_elem *elem, *alt_elem = NULL;
110
111         for (idx = malloc_elem_free_list_index(size);
112                         idx < RTE_HEAP_NUM_FREELISTS; idx++) {
113                 for (elem = LIST_FIRST(&heap->free_head[idx]);
114                                 !!elem; elem = LIST_NEXT(elem, free_list)) {
115                         if (malloc_elem_can_hold(elem, size, align, bound,
116                                         contig)) {
117                                 if (check_hugepage_sz(flags, elem->ms->hugepage_sz))
118                                         return elem;
119                                 if (alt_elem == NULL)
120                                         alt_elem = elem;
121                         }
122                 }
123         }
124
125         if ((alt_elem != NULL) && (flags & RTE_MEMZONE_SIZE_HINT_ONLY))
126                 return alt_elem;
127
128         return NULL;
129 }
130
131 /*
132  * Main function to allocate a block of memory from the heap.
133  * It locks the free list, scans it, and adds a new memseg if the
134  * scan fails. Once the new memseg is added, it re-scans and should return
135  * the new element after releasing the lock.
136  */
137 void *
138 malloc_heap_alloc(struct malloc_heap *heap,
139                 const char *type __attribute__((unused)), size_t size, unsigned flags,
140                 size_t align, size_t bound, bool contig)
141 {
142         struct malloc_elem *elem;
143
144         size = RTE_CACHE_LINE_ROUNDUP(size);
145         align = RTE_CACHE_LINE_ROUNDUP(align);
146
147         rte_spinlock_lock(&heap->lock);
148
149         elem = find_suitable_element(heap, size, flags, align, bound, contig);
150         if (elem != NULL) {
151                 elem = malloc_elem_alloc(elem, size, align, bound, contig);
152                 /* increase heap's count of allocated elements */
153                 heap->alloc_count++;
154         }
155         rte_spinlock_unlock(&heap->lock);
156
157         return elem == NULL ? NULL : (void *)(&elem[1]);
158 }
159
160 int
161 malloc_heap_free(struct malloc_elem *elem)
162 {
163         struct malloc_heap *heap;
164         struct malloc_elem *ret;
165
166         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
167                 return -1;
168
169         /* elem may be merged with previous element, so keep heap address */
170         heap = elem->heap;
171
172         rte_spinlock_lock(&(heap->lock));
173
174         ret = malloc_elem_free(elem);
175
176         rte_spinlock_unlock(&(heap->lock));
177
178         return ret != NULL ? 0 : -1;
179 }
180
181 int
182 malloc_heap_resize(struct malloc_elem *elem, size_t size)
183 {
184         int ret;
185
186         if (!malloc_elem_cookies_ok(elem) || elem->state != ELEM_BUSY)
187                 return -1;
188
189         rte_spinlock_lock(&(elem->heap->lock));
190
191         ret = malloc_elem_resize(elem, size);
192
193         rte_spinlock_unlock(&(elem->heap->lock));
194
195         return ret;
196 }
197
198 /*
199  * Function to retrieve data for heap on given socket
200  */
201 int
202 malloc_heap_get_stats(struct malloc_heap *heap,
203                 struct rte_malloc_socket_stats *socket_stats)
204 {
205         size_t idx;
206         struct malloc_elem *elem;
207
208         rte_spinlock_lock(&heap->lock);
209
210         /* Initialise variables for heap */
211         socket_stats->free_count = 0;
212         socket_stats->heap_freesz_bytes = 0;
213         socket_stats->greatest_free_size = 0;
214
215         /* Iterate through free list */
216         for (idx = 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
217                 for (elem = LIST_FIRST(&heap->free_head[idx]);
218                         !!elem; elem = LIST_NEXT(elem, free_list))
219                 {
220                         socket_stats->free_count++;
221                         socket_stats->heap_freesz_bytes += elem->size;
222                         if (elem->size > socket_stats->greatest_free_size)
223                                 socket_stats->greatest_free_size = elem->size;
224                 }
225         }
226         /* Get stats on overall heap and allocated memory on this heap */
227         socket_stats->heap_totalsz_bytes = heap->total_size;
228         socket_stats->heap_allocsz_bytes = (socket_stats->heap_totalsz_bytes -
229                         socket_stats->heap_freesz_bytes);
230         socket_stats->alloc_count = heap->alloc_count;
231
232         rte_spinlock_unlock(&heap->lock);
233         return 0;
234 }
235
236 /*
237  * Function to retrieve data for heap on given socket
238  */
239 void
240 malloc_heap_dump(struct malloc_heap *heap, FILE *f)
241 {
242         struct malloc_elem *elem;
243
244         rte_spinlock_lock(&heap->lock);
245
246         fprintf(f, "Heap size: 0x%zx\n", heap->total_size);
247         fprintf(f, "Heap alloc count: %u\n", heap->alloc_count);
248
249         elem = heap->first;
250         while (elem) {
251                 malloc_elem_dump(elem, f);
252                 elem = elem->next;
253         }
254
255         rte_spinlock_unlock(&heap->lock);
256 }
257
258 int
259 rte_eal_malloc_heap_init(void)
260 {
261         struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
262
263         if (mcfg == NULL)
264                 return -1;
265
266         rte_memseg_walk(malloc_heap_add_memseg, NULL);
267
268         return 0;
269 }