1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
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>
16 #include "eal_private.h"
17 #include "eal_internal_cfg.h"
18 #include "eal_memalloc.h"
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;
27 struct mem_alloc_validator_entry {
28 TAILQ_ENTRY(mem_alloc_validator_entry) next;
29 char name[RTE_MEM_ALLOC_VALIDATOR_NAME_LEN];
30 rte_mem_alloc_validator_t clb;
35 /** Double linked list of actions. */
36 TAILQ_HEAD(mem_event_callback_entry_list, mem_event_callback_entry);
37 TAILQ_HEAD(mem_alloc_validator_entry_list, mem_alloc_validator_entry);
39 static struct mem_event_callback_entry_list mem_event_callback_list =
40 TAILQ_HEAD_INITIALIZER(mem_event_callback_list);
41 static rte_rwlock_t mem_event_rwlock = RTE_RWLOCK_INITIALIZER;
43 static struct mem_alloc_validator_entry_list mem_alloc_validator_list =
44 TAILQ_HEAD_INITIALIZER(mem_alloc_validator_list);
45 static rte_rwlock_t mem_alloc_validator_rwlock = RTE_RWLOCK_INITIALIZER;
47 static struct mem_event_callback_entry *
48 find_mem_event_callback(const char *name, void *arg)
50 struct mem_event_callback_entry *r;
52 TAILQ_FOREACH(r, &mem_event_callback_list, next) {
53 if (!strcmp(r->name, name) && r->arg == arg)
59 static struct mem_alloc_validator_entry *
60 find_mem_alloc_validator(const char *name, int socket_id)
62 struct mem_alloc_validator_entry *r;
64 TAILQ_FOREACH(r, &mem_alloc_validator_list, next) {
65 if (!strcmp(r->name, name) && r->socket_id == socket_id)
72 eal_memalloc_is_contig(const struct rte_memseg_list *msl, void *start,
75 void *end, *aligned_start, *aligned_end;
76 size_t pgsz = (size_t)msl->page_sz;
77 const struct rte_memseg *ms;
79 /* for IOVA_VA, it's always contiguous */
80 if (rte_eal_iova_mode() == RTE_IOVA_VA)
83 /* for legacy memory, it's always contiguous */
84 if (internal_config.legacy_mem)
87 end = RTE_PTR_ADD(start, len);
89 /* for nohuge, we check pagemap, otherwise check memseg */
90 if (!rte_eal_has_hugepages()) {
91 rte_iova_t cur, expected;
93 aligned_start = RTE_PTR_ALIGN_FLOOR(start, pgsz);
94 aligned_end = RTE_PTR_ALIGN_CEIL(end, pgsz);
96 /* if start and end are on the same page, bail out early */
97 if (RTE_PTR_DIFF(aligned_end, aligned_start) == pgsz)
100 /* skip first iteration */
101 cur = rte_mem_virt2iova(aligned_start);
102 expected = cur + pgsz;
103 aligned_start = RTE_PTR_ADD(aligned_start, pgsz);
105 while (aligned_start < aligned_end) {
106 cur = rte_mem_virt2iova(aligned_start);
109 aligned_start = RTE_PTR_ADD(aligned_start, pgsz);
113 int start_seg, end_seg, cur_seg;
114 rte_iova_t cur, expected;
116 aligned_start = RTE_PTR_ALIGN_FLOOR(start, pgsz);
117 aligned_end = RTE_PTR_ALIGN_CEIL(end, pgsz);
119 start_seg = RTE_PTR_DIFF(aligned_start, msl->base_va) /
121 end_seg = RTE_PTR_DIFF(aligned_end, msl->base_va) /
124 /* if start and end are on the same page, bail out early */
125 if (RTE_PTR_DIFF(aligned_end, aligned_start) == pgsz)
128 /* skip first iteration */
129 ms = rte_fbarray_get(&msl->memseg_arr, start_seg);
131 expected = cur + pgsz;
133 /* if we can't access IOVA addresses, assume non-contiguous */
134 if (cur == RTE_BAD_IOVA)
137 for (cur_seg = start_seg + 1; cur_seg < end_seg;
138 cur_seg++, expected += pgsz) {
139 ms = rte_fbarray_get(&msl->memseg_arr, cur_seg);
141 if (ms->iova != expected)
149 eal_memalloc_mem_event_callback_register(const char *name,
150 rte_mem_event_callback_t clb, void *arg)
152 struct mem_event_callback_entry *entry;
154 if (name == NULL || clb == NULL) {
158 len = strnlen(name, RTE_MEM_EVENT_CALLBACK_NAME_LEN);
162 } else if (len == RTE_MEM_EVENT_CALLBACK_NAME_LEN) {
163 rte_errno = ENAMETOOLONG;
166 rte_rwlock_write_lock(&mem_event_rwlock);
168 entry = find_mem_event_callback(name, arg);
175 entry = malloc(sizeof(*entry));
182 /* callback successfully created and is valid, add it to the list */
185 strlcpy(entry->name, name, RTE_MEM_EVENT_CALLBACK_NAME_LEN);
186 TAILQ_INSERT_TAIL(&mem_event_callback_list, entry, next);
190 RTE_LOG(DEBUG, EAL, "Mem event callback '%s:%p' registered\n",
194 rte_rwlock_write_unlock(&mem_event_rwlock);
199 eal_memalloc_mem_event_callback_unregister(const char *name, void *arg)
201 struct mem_event_callback_entry *entry;
208 len = strnlen(name, RTE_MEM_EVENT_CALLBACK_NAME_LEN);
212 } else if (len == RTE_MEM_EVENT_CALLBACK_NAME_LEN) {
213 rte_errno = ENAMETOOLONG;
216 rte_rwlock_write_lock(&mem_event_rwlock);
218 entry = find_mem_event_callback(name, arg);
224 TAILQ_REMOVE(&mem_event_callback_list, entry, next);
229 RTE_LOG(DEBUG, EAL, "Mem event callback '%s:%p' unregistered\n",
233 rte_rwlock_write_unlock(&mem_event_rwlock);
238 eal_memalloc_mem_event_notify(enum rte_mem_event event, const void *start,
241 struct mem_event_callback_entry *entry;
243 rte_rwlock_read_lock(&mem_event_rwlock);
245 TAILQ_FOREACH(entry, &mem_event_callback_list, next) {
246 RTE_LOG(DEBUG, EAL, "Calling mem event callback '%s:%p'\n",
247 entry->name, entry->arg);
248 entry->clb(event, start, len, entry->arg);
251 rte_rwlock_read_unlock(&mem_event_rwlock);
255 eal_memalloc_mem_alloc_validator_register(const char *name,
256 rte_mem_alloc_validator_t clb, int socket_id, size_t limit)
258 struct mem_alloc_validator_entry *entry;
260 if (name == NULL || clb == NULL || socket_id < 0) {
264 len = strnlen(name, RTE_MEM_ALLOC_VALIDATOR_NAME_LEN);
268 } else if (len == RTE_MEM_ALLOC_VALIDATOR_NAME_LEN) {
269 rte_errno = ENAMETOOLONG;
272 rte_rwlock_write_lock(&mem_alloc_validator_rwlock);
274 entry = find_mem_alloc_validator(name, socket_id);
281 entry = malloc(sizeof(*entry));
288 /* callback successfully created and is valid, add it to the list */
290 entry->socket_id = socket_id;
291 entry->limit = limit;
292 strlcpy(entry->name, name, RTE_MEM_ALLOC_VALIDATOR_NAME_LEN);
293 TAILQ_INSERT_TAIL(&mem_alloc_validator_list, entry, next);
297 RTE_LOG(DEBUG, EAL, "Mem alloc validator '%s' on socket %i with limit %zu registered\n",
298 name, socket_id, limit);
301 rte_rwlock_write_unlock(&mem_alloc_validator_rwlock);
306 eal_memalloc_mem_alloc_validator_unregister(const char *name, int socket_id)
308 struct mem_alloc_validator_entry *entry;
311 if (name == NULL || socket_id < 0) {
315 len = strnlen(name, RTE_MEM_ALLOC_VALIDATOR_NAME_LEN);
319 } else if (len == RTE_MEM_ALLOC_VALIDATOR_NAME_LEN) {
320 rte_errno = ENAMETOOLONG;
323 rte_rwlock_write_lock(&mem_alloc_validator_rwlock);
325 entry = find_mem_alloc_validator(name, socket_id);
331 TAILQ_REMOVE(&mem_alloc_validator_list, entry, next);
336 RTE_LOG(DEBUG, EAL, "Mem alloc validator '%s' on socket %i unregistered\n",
340 rte_rwlock_write_unlock(&mem_alloc_validator_rwlock);
345 eal_memalloc_mem_alloc_validate(int socket_id, size_t new_len)
347 struct mem_alloc_validator_entry *entry;
350 rte_rwlock_read_lock(&mem_alloc_validator_rwlock);
352 TAILQ_FOREACH(entry, &mem_alloc_validator_list, next) {
353 if (entry->socket_id != socket_id || entry->limit > new_len)
355 RTE_LOG(DEBUG, EAL, "Calling mem alloc validator '%s' on socket %i\n",
356 entry->name, entry->socket_id);
357 if (entry->clb(socket_id, entry->limit, new_len) < 0)
361 rte_rwlock_read_unlock(&mem_alloc_validator_rwlock);