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