mem: use strlcpy instead of snprintf
[dpdk.git] / lib / librte_eal / common / eal_common_memalloc.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Intel Corporation
3  */
4
5 #include <string.h>
6
7 #include <rte_errno.h>
8 #include <rte_lcore.h>
9 #include <rte_fbarray.h>
10 #include <rte_memzone.h>
11 #include <rte_memory.h>
12 #include <rte_eal_memconfig.h>
13 #include <rte_string_fns.h>
14 #include <rte_rwlock.h>
15
16 #include "eal_private.h"
17 #include "eal_internal_cfg.h"
18 #include "eal_memalloc.h"
19
20 struct mem_event_callback_entry {
21         TAILQ_ENTRY(mem_event_callback_entry) next;
22         char name[RTE_MEM_EVENT_CALLBACK_NAME_LEN];
23         rte_mem_event_callback_t clb;
24 };
25
26 struct mem_alloc_validator_entry {
27         TAILQ_ENTRY(mem_alloc_validator_entry) next;
28         char name[RTE_MEM_ALLOC_VALIDATOR_NAME_LEN];
29         rte_mem_alloc_validator_t clb;
30         int socket_id;
31         size_t limit;
32 };
33
34 /** Double linked list of actions. */
35 TAILQ_HEAD(mem_event_callback_entry_list, mem_event_callback_entry);
36 TAILQ_HEAD(mem_alloc_validator_entry_list, mem_alloc_validator_entry);
37
38 static struct mem_event_callback_entry_list mem_event_callback_list =
39         TAILQ_HEAD_INITIALIZER(mem_event_callback_list);
40 static rte_rwlock_t mem_event_rwlock = RTE_RWLOCK_INITIALIZER;
41
42 static struct mem_alloc_validator_entry_list mem_alloc_validator_list =
43         TAILQ_HEAD_INITIALIZER(mem_alloc_validator_list);
44 static rte_rwlock_t mem_alloc_validator_rwlock = RTE_RWLOCK_INITIALIZER;
45
46 static struct mem_event_callback_entry *
47 find_mem_event_callback(const char *name)
48 {
49         struct mem_event_callback_entry *r;
50
51         TAILQ_FOREACH(r, &mem_event_callback_list, next) {
52                 if (!strcmp(r->name, name))
53                         break;
54         }
55         return r;
56 }
57
58 static struct mem_alloc_validator_entry *
59 find_mem_alloc_validator(const char *name, int socket_id)
60 {
61         struct mem_alloc_validator_entry *r;
62
63         TAILQ_FOREACH(r, &mem_alloc_validator_list, next) {
64                 if (!strcmp(r->name, name) && r->socket_id == socket_id)
65                         break;
66         }
67         return r;
68 }
69
70 bool
71 eal_memalloc_is_contig(const struct rte_memseg_list *msl, void *start,
72                 size_t len)
73 {
74         void *end, *aligned_start, *aligned_end;
75         size_t pgsz = (size_t)msl->page_sz;
76         const struct rte_memseg *ms;
77
78         /* for IOVA_VA, it's always contiguous */
79         if (rte_eal_iova_mode() == RTE_IOVA_VA)
80                 return true;
81
82         /* for legacy memory, it's always contiguous */
83         if (internal_config.legacy_mem)
84                 return true;
85
86         end = RTE_PTR_ADD(start, len);
87
88         /* for nohuge, we check pagemap, otherwise check memseg */
89         if (!rte_eal_has_hugepages()) {
90                 rte_iova_t cur, expected;
91
92                 aligned_start = RTE_PTR_ALIGN_FLOOR(start, pgsz);
93                 aligned_end = RTE_PTR_ALIGN_CEIL(end, pgsz);
94
95                 /* if start and end are on the same page, bail out early */
96                 if (RTE_PTR_DIFF(aligned_end, aligned_start) == pgsz)
97                         return true;
98
99                 /* skip first iteration */
100                 cur = rte_mem_virt2iova(aligned_start);
101                 expected = cur + pgsz;
102                 aligned_start = RTE_PTR_ADD(aligned_start, pgsz);
103
104                 while (aligned_start < aligned_end) {
105                         cur = rte_mem_virt2iova(aligned_start);
106                         if (cur != expected)
107                                 return false;
108                         aligned_start = RTE_PTR_ADD(aligned_start, pgsz);
109                         expected += pgsz;
110                 }
111         } else {
112                 int start_seg, end_seg, cur_seg;
113                 rte_iova_t cur, expected;
114
115                 aligned_start = RTE_PTR_ALIGN_FLOOR(start, pgsz);
116                 aligned_end = RTE_PTR_ALIGN_CEIL(end, pgsz);
117
118                 start_seg = RTE_PTR_DIFF(aligned_start, msl->base_va) /
119                                 pgsz;
120                 end_seg = RTE_PTR_DIFF(aligned_end, msl->base_va) /
121                                 pgsz;
122
123                 /* if start and end are on the same page, bail out early */
124                 if (RTE_PTR_DIFF(aligned_end, aligned_start) == pgsz)
125                         return true;
126
127                 /* skip first iteration */
128                 ms = rte_fbarray_get(&msl->memseg_arr, start_seg);
129                 cur = ms->iova;
130                 expected = cur + pgsz;
131
132                 /* if we can't access IOVA addresses, assume non-contiguous */
133                 if (cur == RTE_BAD_IOVA)
134                         return false;
135
136                 for (cur_seg = start_seg + 1; cur_seg < end_seg;
137                                 cur_seg++, expected += pgsz) {
138                         ms = rte_fbarray_get(&msl->memseg_arr, cur_seg);
139
140                         if (ms->iova != expected)
141                                 return false;
142                 }
143         }
144         return true;
145 }
146
147 int
148 eal_memalloc_mem_event_callback_register(const char *name,
149                 rte_mem_event_callback_t clb)
150 {
151         struct mem_event_callback_entry *entry;
152         int ret, len;
153         if (name == NULL || clb == NULL) {
154                 rte_errno = EINVAL;
155                 return -1;
156         }
157         len = strnlen(name, RTE_MEM_EVENT_CALLBACK_NAME_LEN);
158         if (len == 0) {
159                 rte_errno = EINVAL;
160                 return -1;
161         } else if (len == RTE_MEM_EVENT_CALLBACK_NAME_LEN) {
162                 rte_errno = ENAMETOOLONG;
163                 return -1;
164         }
165         rte_rwlock_write_lock(&mem_event_rwlock);
166
167         entry = find_mem_event_callback(name);
168         if (entry != NULL) {
169                 rte_errno = EEXIST;
170                 ret = -1;
171                 goto unlock;
172         }
173
174         entry = malloc(sizeof(*entry));
175         if (entry == NULL) {
176                 rte_errno = ENOMEM;
177                 ret = -1;
178                 goto unlock;
179         }
180
181         /* callback successfully created and is valid, add it to the list */
182         entry->clb = clb;
183         strlcpy(entry->name, name, RTE_MEM_EVENT_CALLBACK_NAME_LEN);
184         TAILQ_INSERT_TAIL(&mem_event_callback_list, entry, next);
185
186         ret = 0;
187
188         RTE_LOG(DEBUG, EAL, "Mem event callback '%s' registered\n", name);
189
190 unlock:
191         rte_rwlock_write_unlock(&mem_event_rwlock);
192         return ret;
193 }
194
195 int
196 eal_memalloc_mem_event_callback_unregister(const char *name)
197 {
198         struct mem_event_callback_entry *entry;
199         int ret, len;
200
201         if (name == NULL) {
202                 rte_errno = EINVAL;
203                 return -1;
204         }
205         len = strnlen(name, RTE_MEM_EVENT_CALLBACK_NAME_LEN);
206         if (len == 0) {
207                 rte_errno = EINVAL;
208                 return -1;
209         } else if (len == RTE_MEM_EVENT_CALLBACK_NAME_LEN) {
210                 rte_errno = ENAMETOOLONG;
211                 return -1;
212         }
213         rte_rwlock_write_lock(&mem_event_rwlock);
214
215         entry = find_mem_event_callback(name);
216         if (entry == NULL) {
217                 rte_errno = ENOENT;
218                 ret = -1;
219                 goto unlock;
220         }
221         TAILQ_REMOVE(&mem_event_callback_list, entry, next);
222         free(entry);
223
224         ret = 0;
225
226         RTE_LOG(DEBUG, EAL, "Mem event callback '%s' unregistered\n", name);
227
228 unlock:
229         rte_rwlock_write_unlock(&mem_event_rwlock);
230         return ret;
231 }
232
233 void
234 eal_memalloc_mem_event_notify(enum rte_mem_event event, const void *start,
235                 size_t len)
236 {
237         struct mem_event_callback_entry *entry;
238
239         rte_rwlock_read_lock(&mem_event_rwlock);
240
241         TAILQ_FOREACH(entry, &mem_event_callback_list, next) {
242                 RTE_LOG(DEBUG, EAL, "Calling mem event callback %s",
243                         entry->name);
244                 entry->clb(event, start, len);
245         }
246
247         rte_rwlock_read_unlock(&mem_event_rwlock);
248 }
249
250 int
251 eal_memalloc_mem_alloc_validator_register(const char *name,
252                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
253 {
254         struct mem_alloc_validator_entry *entry;
255         int ret, len;
256         if (name == NULL || clb == NULL || socket_id < 0) {
257                 rte_errno = EINVAL;
258                 return -1;
259         }
260         len = strnlen(name, RTE_MEM_ALLOC_VALIDATOR_NAME_LEN);
261         if (len == 0) {
262                 rte_errno = EINVAL;
263                 return -1;
264         } else if (len == RTE_MEM_ALLOC_VALIDATOR_NAME_LEN) {
265                 rte_errno = ENAMETOOLONG;
266                 return -1;
267         }
268         rte_rwlock_write_lock(&mem_alloc_validator_rwlock);
269
270         entry = find_mem_alloc_validator(name, socket_id);
271         if (entry != NULL) {
272                 rte_errno = EEXIST;
273                 ret = -1;
274                 goto unlock;
275         }
276
277         entry = malloc(sizeof(*entry));
278         if (entry == NULL) {
279                 rte_errno = ENOMEM;
280                 ret = -1;
281                 goto unlock;
282         }
283
284         /* callback successfully created and is valid, add it to the list */
285         entry->clb = clb;
286         entry->socket_id = socket_id;
287         entry->limit = limit;
288         strlcpy(entry->name, name, RTE_MEM_ALLOC_VALIDATOR_NAME_LEN);
289         TAILQ_INSERT_TAIL(&mem_alloc_validator_list, entry, next);
290
291         ret = 0;
292
293         RTE_LOG(DEBUG, EAL, "Mem alloc validator '%s' on socket %i with limit %zu registered\n",
294                 name, socket_id, limit);
295
296 unlock:
297         rte_rwlock_write_unlock(&mem_alloc_validator_rwlock);
298         return ret;
299 }
300
301 int
302 eal_memalloc_mem_alloc_validator_unregister(const char *name, int socket_id)
303 {
304         struct mem_alloc_validator_entry *entry;
305         int ret, len;
306
307         if (name == NULL || socket_id < 0) {
308                 rte_errno = EINVAL;
309                 return -1;
310         }
311         len = strnlen(name, RTE_MEM_ALLOC_VALIDATOR_NAME_LEN);
312         if (len == 0) {
313                 rte_errno = EINVAL;
314                 return -1;
315         } else if (len == RTE_MEM_ALLOC_VALIDATOR_NAME_LEN) {
316                 rte_errno = ENAMETOOLONG;
317                 return -1;
318         }
319         rte_rwlock_write_lock(&mem_alloc_validator_rwlock);
320
321         entry = find_mem_alloc_validator(name, socket_id);
322         if (entry == NULL) {
323                 rte_errno = ENOENT;
324                 ret = -1;
325                 goto unlock;
326         }
327         TAILQ_REMOVE(&mem_alloc_validator_list, entry, next);
328         free(entry);
329
330         ret = 0;
331
332         RTE_LOG(DEBUG, EAL, "Mem alloc validator '%s' on socket %i unregistered\n",
333                 name, socket_id);
334
335 unlock:
336         rte_rwlock_write_unlock(&mem_alloc_validator_rwlock);
337         return ret;
338 }
339
340 int
341 eal_memalloc_mem_alloc_validate(int socket_id, size_t new_len)
342 {
343         struct mem_alloc_validator_entry *entry;
344         int ret = 0;
345
346         rte_rwlock_read_lock(&mem_alloc_validator_rwlock);
347
348         TAILQ_FOREACH(entry, &mem_alloc_validator_list, next) {
349                 if (entry->socket_id != socket_id || entry->limit > new_len)
350                         continue;
351                 RTE_LOG(DEBUG, EAL, "Calling mem alloc validator '%s' on socket %i\n",
352                         entry->name, entry->socket_id);
353                 if (entry->clb(socket_id, entry->limit, new_len) < 0)
354                         ret = -1;
355         }
356
357         rte_rwlock_read_unlock(&mem_alloc_validator_rwlock);
358
359         return ret;
360 }