mem: refactor memzone reserve functions
[dpdk.git] / lib / librte_eal / common / eal_common_memzone.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <inttypes.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_log.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_eal.h>
47 #include <rte_eal_memconfig.h>
48 #include <rte_per_lcore.h>
49 #include <rte_errno.h>
50 #include <rte_string_fns.h>
51 #include <rte_common.h>
52
53 #include "eal_private.h"
54
55 /* internal copy of free memory segments */
56 static struct rte_memseg *free_memseg = NULL;
57
58 static inline const struct rte_memzone *
59 memzone_lookup_thread_unsafe(const char *name)
60 {
61         const struct rte_mem_config *mcfg;
62         unsigned i = 0;
63
64         /* get pointer to global configuration */
65         mcfg = rte_eal_get_configuration()->mem_config;
66
67         /*
68          * the algorithm is not optimal (linear), but there are few
69          * zones and this function should be called at init only
70          */
71         for (i = 0; i < RTE_MAX_MEMZONE && mcfg->memzone[i].addr != NULL; i++) {
72                 if (!strncmp(name, mcfg->memzone[i].name, RTE_MEMZONE_NAMESIZE))
73                         return &mcfg->memzone[i];
74         }
75
76         return NULL;
77 }
78
79 /*
80  * Helper function for memzone_reserve_aligned_thread_unsafe().
81  * Calculate address offset from the start of the segment.
82  * Align offset in that way that it satisfy istart alignmnet and
83  * buffer of the  requested length would not cross specified boundary.
84  */
85 static inline phys_addr_t
86 align_phys_boundary(const struct rte_memseg *ms, size_t len, size_t align,
87         size_t bound)
88 {
89         phys_addr_t addr_offset, bmask, end, start;
90         size_t step;
91
92         step = RTE_MAX(align, bound);
93         bmask = ~((phys_addr_t)bound - 1);
94
95         /* calculate offset to closest alignment */
96         start = RTE_ALIGN_CEIL(ms->phys_addr, align);
97         addr_offset = start - ms->phys_addr;
98
99         while (addr_offset + len < ms->len) {
100
101                 /* check, do we meet boundary condition */
102                 end = start + len - (len != 0);
103                 if ((start & bmask) == (end & bmask))
104                         break;
105
106                 /* calculate next offset */
107                 start = RTE_ALIGN_CEIL(start + 1, step);
108                 addr_offset = start - ms->phys_addr;
109         }
110
111         return addr_offset;
112 }
113
114 static const struct rte_memzone *
115 memzone_reserve_aligned_thread_unsafe(const char *name, size_t len,
116                 int socket_id, unsigned flags, unsigned align, unsigned bound)
117 {
118         struct rte_mem_config *mcfg;
119         unsigned i = 0;
120         int memseg_idx = -1;
121         uint64_t addr_offset, seg_offset = 0;
122         size_t requested_len;
123         size_t memseg_len = 0;
124         phys_addr_t memseg_physaddr;
125         void *memseg_addr;
126
127         /* get pointer to global configuration */
128         mcfg = rte_eal_get_configuration()->mem_config;
129
130         /* no more room in config */
131         if (mcfg->memzone_idx >= RTE_MAX_MEMZONE) {
132                 RTE_LOG(ERR, EAL, "%s(): No more room in config\n", __func__);
133                 rte_errno = ENOSPC;
134                 return NULL;
135         }
136
137         /* zone already exist */
138         if ((memzone_lookup_thread_unsafe(name)) != NULL) {
139                 RTE_LOG(DEBUG, EAL, "%s(): memzone <%s> already exists\n",
140                         __func__, name);
141                 rte_errno = EEXIST;
142                 return NULL;
143         }
144
145         /* if alignment is not a power of two */
146         if (align && !rte_is_power_of_2(align)) {
147                 RTE_LOG(ERR, EAL, "%s(): Invalid alignment: %u\n", __func__,
148                                 align);
149                 rte_errno = EINVAL;
150                 return NULL;
151         }
152
153         /* alignment less than cache size is not allowed */
154         if (align < RTE_CACHE_LINE_SIZE)
155                 align = RTE_CACHE_LINE_SIZE;
156
157
158         /* align length on cache boundary. Check for overflow before doing so */
159         if (len > SIZE_MAX - RTE_CACHE_LINE_MASK) {
160                 rte_errno = EINVAL; /* requested size too big */
161                 return NULL;
162         }
163
164         len += RTE_CACHE_LINE_MASK;
165         len &= ~((size_t) RTE_CACHE_LINE_MASK);
166
167         /* save minimal requested  length */
168         requested_len = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE,  len);
169
170         /* check that boundary condition is valid */
171         if (bound != 0 &&
172                         (requested_len > bound || !rte_is_power_of_2(bound))) {
173                 rte_errno = EINVAL;
174                 return NULL;
175         }
176
177         /* find the smallest segment matching requirements */
178         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
179                 /* last segment */
180                 if (free_memseg[i].addr == NULL)
181                         break;
182
183                 /* empty segment, skip it */
184                 if (free_memseg[i].len == 0)
185                         continue;
186
187                 /* bad socket ID */
188                 if (socket_id != SOCKET_ID_ANY &&
189                     free_memseg[i].socket_id != SOCKET_ID_ANY &&
190                     socket_id != free_memseg[i].socket_id)
191                         continue;
192
193                 /*
194                  * calculate offset to closest alignment that
195                  * meets boundary conditions.
196                  */
197                 addr_offset = align_phys_boundary(free_memseg + i,
198                         requested_len, align, bound);
199
200                 /* check len */
201                 if ((requested_len + addr_offset) > free_memseg[i].len)
202                         continue;
203
204                 /* check flags for hugepage sizes */
205                 if ((flags & RTE_MEMZONE_2MB) &&
206                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_1G)
207                         continue;
208                 if ((flags & RTE_MEMZONE_1GB) &&
209                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_2M)
210                         continue;
211                 if ((flags & RTE_MEMZONE_16MB) &&
212                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_16G)
213                         continue;
214                 if ((flags & RTE_MEMZONE_16GB) &&
215                                 free_memseg[i].hugepage_sz == RTE_PGSIZE_16M)
216                         continue;
217
218                 /* this segment is the best until now */
219                 if (memseg_idx == -1) {
220                         memseg_idx = i;
221                         memseg_len = free_memseg[i].len;
222                         seg_offset = addr_offset;
223                 }
224                 /* find the biggest contiguous zone */
225                 else if (len == 0) {
226                         if (free_memseg[i].len > memseg_len) {
227                                 memseg_idx = i;
228                                 memseg_len = free_memseg[i].len;
229                                 seg_offset = addr_offset;
230                         }
231                 }
232                 /*
233                  * find the smallest (we already checked that current
234                  * zone length is > len
235                  */
236                 else if (free_memseg[i].len + align < memseg_len ||
237                                 (free_memseg[i].len <= memseg_len + align &&
238                                 addr_offset < seg_offset)) {
239                         memseg_idx = i;
240                         memseg_len = free_memseg[i].len;
241                         seg_offset = addr_offset;
242                 }
243         }
244
245         /* no segment found */
246         if (memseg_idx == -1) {
247                 /*
248                  * If RTE_MEMZONE_SIZE_HINT_ONLY flag is specified,
249                  * try allocating again without the size parameter otherwise -fail.
250                  */
251                 if ((flags & RTE_MEMZONE_SIZE_HINT_ONLY)  &&
252                     ((flags & RTE_MEMZONE_1GB) || (flags & RTE_MEMZONE_2MB)
253                 || (flags & RTE_MEMZONE_16MB) || (flags & RTE_MEMZONE_16GB)))
254                         return memzone_reserve_aligned_thread_unsafe(name,
255                                 len, socket_id, 0, align, bound);
256
257                 rte_errno = ENOMEM;
258                 return NULL;
259         }
260
261         /* save aligned physical and virtual addresses */
262         memseg_physaddr = free_memseg[memseg_idx].phys_addr + seg_offset;
263         memseg_addr = RTE_PTR_ADD(free_memseg[memseg_idx].addr,
264                         (uintptr_t) seg_offset);
265
266         /* if we are looking for a biggest memzone */
267         if (len == 0) {
268                 if (bound == 0)
269                         requested_len = memseg_len - seg_offset;
270                 else
271                         requested_len = RTE_ALIGN_CEIL(memseg_physaddr + 1,
272                                 bound) - memseg_physaddr;
273         }
274
275         /* set length to correct value */
276         len = (size_t)seg_offset + requested_len;
277
278         /* update our internal state */
279         free_memseg[memseg_idx].len -= len;
280         free_memseg[memseg_idx].phys_addr += len;
281         free_memseg[memseg_idx].addr =
282                 (char *)free_memseg[memseg_idx].addr + len;
283
284         /* fill the zone in config */
285         struct rte_memzone *mz = &mcfg->memzone[mcfg->memzone_idx++];
286         snprintf(mz->name, sizeof(mz->name), "%s", name);
287         mz->phys_addr = memseg_physaddr;
288         mz->addr = memseg_addr;
289         mz->len = requested_len;
290         mz->hugepage_sz = free_memseg[memseg_idx].hugepage_sz;
291         mz->socket_id = free_memseg[memseg_idx].socket_id;
292         mz->flags = 0;
293         mz->memseg_id = memseg_idx;
294
295         return mz;
296 }
297
298 static const struct rte_memzone *
299 rte_memzone_reserve_thread_safe(const char *name, size_t len,
300                                 int socket_id, unsigned flags, unsigned align,
301                                 unsigned bound)
302 {
303         struct rte_mem_config *mcfg;
304         const struct rte_memzone *mz = NULL;
305
306         /* both sizes cannot be explicitly called for */
307         if (((flags & RTE_MEMZONE_1GB) && (flags & RTE_MEMZONE_2MB))
308                 || ((flags & RTE_MEMZONE_16MB) && (flags & RTE_MEMZONE_16GB))) {
309                 rte_errno = EINVAL;
310                 return NULL;
311         }
312
313         /* get pointer to global configuration */
314         mcfg = rte_eal_get_configuration()->mem_config;
315
316         rte_rwlock_write_lock(&mcfg->mlock);
317
318         mz = memzone_reserve_aligned_thread_unsafe(
319                 name, len, socket_id, flags, align, bound);
320
321         rte_rwlock_write_unlock(&mcfg->mlock);
322
323         return mz;
324 }
325
326 /*
327  * Return a pointer to a correctly filled memzone descriptor (with a
328  * specified alignment and boundary). If the allocation cannot be done,
329  * return NULL.
330  */
331 const struct rte_memzone *
332 rte_memzone_reserve_bounded(const char *name, size_t len, int socket_id,
333                             unsigned flags, unsigned align, unsigned bound)
334 {
335         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
336                                                align, bound);
337 }
338
339 /*
340  * Return a pointer to a correctly filled memzone descriptor (with a
341  * specified alignment). If the allocation cannot be done, return NULL.
342  */
343 const struct rte_memzone *
344 rte_memzone_reserve_aligned(const char *name, size_t len, int socket_id,
345                             unsigned flags, unsigned align)
346 {
347         return rte_memzone_reserve_thread_safe(name, len, socket_id, flags,
348                                                align, 0);
349 }
350
351 /*
352  * Return a pointer to a correctly filled memzone descriptor. If the
353  * allocation cannot be done, return NULL.
354  */
355 const struct rte_memzone *
356 rte_memzone_reserve(const char *name, size_t len, int socket_id,
357                     unsigned flags)
358 {
359         return rte_memzone_reserve_thread_safe(name, len, socket_id,
360                                                flags, RTE_CACHE_LINE_SIZE, 0);
361 }
362
363 /*
364  * Lookup for the memzone identified by the given name
365  */
366 const struct rte_memzone *
367 rte_memzone_lookup(const char *name)
368 {
369         struct rte_mem_config *mcfg;
370         const struct rte_memzone *memzone = NULL;
371
372         mcfg = rte_eal_get_configuration()->mem_config;
373
374         rte_rwlock_read_lock(&mcfg->mlock);
375
376         memzone = memzone_lookup_thread_unsafe(name);
377
378         rte_rwlock_read_unlock(&mcfg->mlock);
379
380         return memzone;
381 }
382
383 /* Dump all reserved memory zones on console */
384 void
385 rte_memzone_dump(FILE *f)
386 {
387         struct rte_mem_config *mcfg;
388         unsigned i = 0;
389
390         /* get pointer to global configuration */
391         mcfg = rte_eal_get_configuration()->mem_config;
392
393         rte_rwlock_read_lock(&mcfg->mlock);
394         /* dump all zones */
395         for (i=0; i<RTE_MAX_MEMZONE; i++) {
396                 if (mcfg->memzone[i].addr == NULL)
397                         break;
398                 fprintf(f, "Zone %u: name:<%s>, phys:0x%"PRIx64", len:0x%zx"
399                        ", virt:%p, socket_id:%"PRId32", flags:%"PRIx32"\n", i,
400                        mcfg->memzone[i].name,
401                        mcfg->memzone[i].phys_addr,
402                        mcfg->memzone[i].len,
403                        mcfg->memzone[i].addr,
404                        mcfg->memzone[i].socket_id,
405                        mcfg->memzone[i].flags);
406         }
407         rte_rwlock_read_unlock(&mcfg->mlock);
408 }
409
410 /*
411  * called by init: modify the free memseg list to have cache-aligned
412  * addresses and cache-aligned lengths
413  */
414 static int
415 memseg_sanitize(struct rte_memseg *memseg)
416 {
417         unsigned phys_align;
418         unsigned virt_align;
419         unsigned off;
420
421         phys_align = memseg->phys_addr & RTE_CACHE_LINE_MASK;
422         virt_align = (unsigned long)memseg->addr & RTE_CACHE_LINE_MASK;
423
424         /*
425          * sanity check: phys_addr and addr must have the same
426          * alignment
427          */
428         if (phys_align != virt_align)
429                 return -1;
430
431         /* memseg is really too small, don't bother with it */
432         if (memseg->len < (2 * RTE_CACHE_LINE_SIZE)) {
433                 memseg->len = 0;
434                 return 0;
435         }
436
437         /* align start address */
438         off = (RTE_CACHE_LINE_SIZE - phys_align) & RTE_CACHE_LINE_MASK;
439         memseg->phys_addr += off;
440         memseg->addr = (char *)memseg->addr + off;
441         memseg->len -= off;
442
443         /* align end address */
444         memseg->len &= ~((uint64_t)RTE_CACHE_LINE_MASK);
445
446         return 0;
447 }
448
449 /*
450  * Init the memzone subsystem
451  */
452 int
453 rte_eal_memzone_init(void)
454 {
455         struct rte_mem_config *mcfg;
456         const struct rte_memseg *memseg;
457         unsigned i = 0;
458
459         /* get pointer to global configuration */
460         mcfg = rte_eal_get_configuration()->mem_config;
461
462         /* mirror the runtime memsegs from config */
463         free_memseg = mcfg->free_memseg;
464
465         /* secondary processes don't need to initialise anything */
466         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
467                 return 0;
468
469         memseg = rte_eal_get_physmem_layout();
470         if (memseg == NULL) {
471                 RTE_LOG(ERR, EAL, "%s(): Cannot get physical layout\n", __func__);
472                 return -1;
473         }
474
475         rte_rwlock_write_lock(&mcfg->mlock);
476
477         /* fill in uninitialized free_memsegs */
478         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
479                 if (memseg[i].addr == NULL)
480                         break;
481                 if (free_memseg[i].addr != NULL)
482                         continue;
483                 memcpy(&free_memseg[i], &memseg[i], sizeof(struct rte_memseg));
484         }
485
486         /* make all zones cache-aligned */
487         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
488                 if (free_memseg[i].addr == NULL)
489                         break;
490                 if (memseg_sanitize(&free_memseg[i]) < 0) {
491                         RTE_LOG(ERR, EAL, "%s(): Sanity check failed\n", __func__);
492                         rte_rwlock_write_unlock(&mcfg->mlock);
493                         return -1;
494                 }
495         }
496
497         /* delete all zones */
498         mcfg->memzone_idx = 0;
499         memset(mcfg->memzone, 0, sizeof(mcfg->memzone));
500
501         rte_rwlock_write_unlock(&mcfg->mlock);
502
503         return 0;
504 }
505
506 /* Walk all reserved memory zones */
507 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *),
508                       void *arg)
509 {
510         struct rte_mem_config *mcfg;
511         unsigned i;
512
513         mcfg = rte_eal_get_configuration()->mem_config;
514
515         rte_rwlock_read_lock(&mcfg->mlock);
516         for (i=0; i<RTE_MAX_MEMZONE; i++) {
517                 if (mcfg->memzone[i].addr != NULL)
518                         (*func)(&mcfg->memzone[i], arg);
519         }
520         rte_rwlock_read_unlock(&mcfg->mlock);
521 }