1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
14 #include <rte_common.h>
16 #include <rte_errno.h>
17 #include <rte_spinlock.h>
18 #include <rte_tailq.h>
20 #include "eal_filesystem.h"
21 #include "eal_private.h"
23 #include "rte_fbarray.h"
25 #define MASK_SHIFT 6ULL
26 #define MASK_ALIGN (1ULL << MASK_SHIFT)
27 #define MASK_LEN_TO_IDX(x) ((x) >> MASK_SHIFT)
28 #define MASK_LEN_TO_MOD(x) ((x) - RTE_ALIGN_FLOOR(x, MASK_ALIGN))
29 #define MASK_GET_IDX(idx, mod) ((idx << MASK_SHIFT) + mod)
32 * We use this to keep track of created/attached memory areas to prevent user
33 * errors in API usage.
36 TAILQ_ENTRY(mem_area) next;
41 TAILQ_HEAD(mem_area_head, mem_area);
42 /* local per-process tailq */
43 static struct mem_area_head mem_area_tailq =
44 TAILQ_HEAD_INITIALIZER(mem_area_tailq);
45 static rte_spinlock_t mem_area_lock = RTE_SPINLOCK_INITIALIZER;
48 * This is a mask that is always stored at the end of array, to provide fast
49 * way of finding free/used spots without looping through each element.
58 calc_mask_size(unsigned int len)
60 /* mask must be multiple of MASK_ALIGN, even though length of array
61 * itself may not be aligned on that boundary.
63 len = RTE_ALIGN_CEIL(len, MASK_ALIGN);
64 return sizeof(struct used_mask) +
65 sizeof(uint64_t) * MASK_LEN_TO_IDX(len);
69 calc_data_size(size_t page_sz, unsigned int elt_sz, unsigned int len)
71 size_t data_sz = elt_sz * len;
72 size_t msk_sz = calc_mask_size(len);
73 return RTE_ALIGN_CEIL(data_sz + msk_sz, page_sz);
76 static struct used_mask *
77 get_used_mask(void *data, unsigned int elt_sz, unsigned int len)
79 return (struct used_mask *) RTE_PTR_ADD(data, elt_sz * len);
83 resize_and_map(int fd, void *addr, size_t len)
88 if (ftruncate(fd, len)) {
89 RTE_LOG(ERR, EAL, "Cannot truncate %s\n", path);
90 /* pass errno up the chain */
95 map_addr = mmap(addr, len, PROT_READ | PROT_WRITE,
96 MAP_SHARED | MAP_FIXED, fd, 0);
97 if (map_addr != addr) {
98 RTE_LOG(ERR, EAL, "mmap() failed: %s\n", strerror(errno));
99 /* pass errno up the chain */
107 overlap(const struct mem_area *ma, const void *start, size_t len)
109 const void *end = RTE_PTR_ADD(start, len);
110 const void *ma_start = ma->addr;
111 const void *ma_end = RTE_PTR_ADD(ma->addr, ma->len);
114 if (start >= ma_start && start < ma_end)
117 if (end >= ma_start && end < ma_end)
123 find_next_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
126 const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
128 unsigned int msk_idx, lookahead_idx, first, first_mod;
129 unsigned int last, last_mod;
130 uint64_t last_msk, ignore_msk;
133 * mask only has granularity of MASK_ALIGN, but start may not be aligned
134 * on that boundary, so construct a special mask to exclude anything we
135 * don't want to see to avoid confusing ctz.
137 first = MASK_LEN_TO_IDX(start);
138 first_mod = MASK_LEN_TO_MOD(start);
139 ignore_msk = ~((1ULL << first_mod) - 1);
141 /* array length may not be aligned, so calculate ignore mask for last
144 last = MASK_LEN_TO_IDX(arr->len);
145 last_mod = MASK_LEN_TO_MOD(arr->len);
146 last_msk = ~(-1ULL << last_mod);
148 for (msk_idx = first; msk_idx < msk->n_masks; msk_idx++) {
149 uint64_t cur_msk, lookahead_msk;
150 unsigned int run_start, clz, left;
153 * The process of getting n consecutive bits for arbitrary n is
154 * a bit involved, but here it is in a nutshell:
156 * 1. let n be the number of consecutive bits we're looking for
157 * 2. check if n can fit in one mask, and if so, do n-1
158 * rshift-ands to see if there is an appropriate run inside
160 * 2a. if we found a run, bail out early
161 * 2b. if we didn't find a run, proceed
162 * 3. invert the mask and count leading zeroes (that is, count
163 * how many consecutive set bits we had starting from the
164 * end of current mask) as k
165 * 3a. if k is 0, continue to next mask
166 * 3b. if k is not 0, we have a potential run
167 * 4. to satisfy our requirements, next mask must have n-k
168 * consecutive set bits right at the start, so we will do
169 * (n-k-1) rshift-ands and check if first bit is set.
171 * Step 4 will need to be repeated if (n-k) > MASK_ALIGN until
172 * we either run out of masks, lose the run, or find what we
175 cur_msk = msk->data[msk_idx];
178 /* if we're looking for free spaces, invert the mask */
182 /* combine current ignore mask with last index ignore mask */
184 ignore_msk |= last_msk;
186 /* if we have an ignore mask, ignore once */
188 cur_msk &= ignore_msk;
192 /* if n can fit in within a single mask, do a search */
193 if (n <= MASK_ALIGN) {
194 uint64_t tmp_msk = cur_msk;
196 for (s_idx = 0; s_idx < n - 1; s_idx++)
197 tmp_msk &= tmp_msk >> 1ULL;
198 /* we found what we were looking for */
200 run_start = __builtin_ctzll(tmp_msk);
201 return MASK_GET_IDX(msk_idx, run_start);
206 * we didn't find our run within the mask, or n > MASK_ALIGN,
207 * so we're going for plan B.
210 /* count leading zeroes on inverted mask */
212 clz = sizeof(cur_msk) * 8;
214 clz = __builtin_clzll(~cur_msk);
216 /* if there aren't any runs at the end either, just continue */
220 /* we have a partial run at the end, so try looking ahead */
221 run_start = MASK_ALIGN - clz;
224 for (lookahead_idx = msk_idx + 1; lookahead_idx < msk->n_masks;
226 unsigned int s_idx, need;
227 lookahead_msk = msk->data[lookahead_idx];
229 /* if we're looking for free space, invert the mask */
231 lookahead_msk = ~lookahead_msk;
233 /* figure out how many consecutive bits we need here */
234 need = RTE_MIN(left, MASK_ALIGN);
236 for (s_idx = 0; s_idx < need - 1; s_idx++)
237 lookahead_msk &= lookahead_msk >> 1ULL;
239 /* if first bit is not set, we've lost the run */
240 if ((lookahead_msk & 1) == 0) {
242 * we've scanned this far, so we know there are
243 * no runs in the space we've lookahead-scanned
244 * as well, so skip that on next iteration.
246 ignore_msk = ~((1ULL << need) - 1);
247 msk_idx = lookahead_idx;
253 /* check if we've found what we were looking for */
260 /* we didn't find anything, so continue */
264 return MASK_GET_IDX(msk_idx, run_start);
266 /* we didn't find anything */
267 rte_errno = used ? ENOENT : ENOSPC;
272 find_next(const struct rte_fbarray *arr, unsigned int start, bool used)
274 const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
276 unsigned int idx, first, first_mod;
277 unsigned int last, last_mod;
278 uint64_t last_msk, ignore_msk;
281 * mask only has granularity of MASK_ALIGN, but start may not be aligned
282 * on that boundary, so construct a special mask to exclude anything we
283 * don't want to see to avoid confusing ctz.
285 first = MASK_LEN_TO_IDX(start);
286 first_mod = MASK_LEN_TO_MOD(start);
287 ignore_msk = ~((1ULL << first_mod) - 1ULL);
289 /* array length may not be aligned, so calculate ignore mask for last
292 last = MASK_LEN_TO_IDX(arr->len);
293 last_mod = MASK_LEN_TO_MOD(arr->len);
294 last_msk = ~(-(1ULL) << last_mod);
296 for (idx = first; idx < msk->n_masks; idx++) {
297 uint64_t cur = msk->data[idx];
300 /* if we're looking for free entries, invert mask */
307 /* ignore everything before start on first iteration */
311 /* check if we have any entries */
316 * find first set bit - that will correspond to whatever it is
317 * that we're looking for.
319 found = __builtin_ctzll(cur);
320 return MASK_GET_IDX(idx, found);
322 /* we didn't find anything */
323 rte_errno = used ? ENOENT : ENOSPC;
328 find_contig(const struct rte_fbarray *arr, unsigned int start, bool used)
330 const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
332 unsigned int idx, first, first_mod;
333 unsigned int last, last_mod;
335 unsigned int need_len, result = 0;
337 /* array length may not be aligned, so calculate ignore mask for last
340 last = MASK_LEN_TO_IDX(arr->len);
341 last_mod = MASK_LEN_TO_MOD(arr->len);
342 last_msk = ~(-(1ULL) << last_mod);
344 first = MASK_LEN_TO_IDX(start);
345 first_mod = MASK_LEN_TO_MOD(start);
346 for (idx = first; idx < msk->n_masks; idx++, result += need_len) {
347 uint64_t cur = msk->data[idx];
348 unsigned int run_len;
350 need_len = MASK_ALIGN;
352 /* if we're looking for free entries, invert mask */
356 /* if this is last mask, ignore everything after last bit */
360 /* ignore everything before start on first iteration */
363 /* at the start, we don't need the full mask len */
364 need_len -= first_mod;
367 /* we will be looking for zeroes, so invert the mask */
370 /* if mask is zero, we have a complete run */
375 * see if current run ends before mask end.
377 run_len = __builtin_ctzll(cur);
379 /* add however many zeroes we've had in the last run and quit */
380 if (run_len < need_len) {
389 find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
392 const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
394 unsigned int msk_idx, lookbehind_idx, first, first_mod;
398 * mask only has granularity of MASK_ALIGN, but start may not be aligned
399 * on that boundary, so construct a special mask to exclude anything we
400 * don't want to see to avoid confusing ctz.
402 first = MASK_LEN_TO_IDX(start);
403 first_mod = MASK_LEN_TO_MOD(start);
404 /* we're going backwards, so mask must start from the top */
405 ignore_msk = first_mod == MASK_ALIGN - 1 ?
406 -1ULL : /* prevent overflow */
407 ~(-1ULL << (first_mod + 1));
409 /* go backwards, include zero */
412 uint64_t cur_msk, lookbehind_msk;
413 unsigned int run_start, run_end, ctz, left;
416 * The process of getting n consecutive bits from the top for
417 * arbitrary n is a bit involved, but here it is in a nutshell:
419 * 1. let n be the number of consecutive bits we're looking for
420 * 2. check if n can fit in one mask, and if so, do n-1
421 * lshift-ands to see if there is an appropriate run inside
423 * 2a. if we found a run, bail out early
424 * 2b. if we didn't find a run, proceed
425 * 3. invert the mask and count trailing zeroes (that is, count
426 * how many consecutive set bits we had starting from the
427 * start of current mask) as k
428 * 3a. if k is 0, continue to next mask
429 * 3b. if k is not 0, we have a potential run
430 * 4. to satisfy our requirements, next mask must have n-k
431 * consecutive set bits at the end, so we will do (n-k-1)
432 * lshift-ands and check if last bit is set.
434 * Step 4 will need to be repeated if (n-k) > MASK_ALIGN until
435 * we either run out of masks, lose the run, or find what we
438 cur_msk = msk->data[msk_idx];
441 /* if we're looking for free spaces, invert the mask */
445 /* if we have an ignore mask, ignore once */
447 cur_msk &= ignore_msk;
451 /* if n can fit in within a single mask, do a search */
452 if (n <= MASK_ALIGN) {
453 uint64_t tmp_msk = cur_msk;
455 for (s_idx = 0; s_idx < n - 1; s_idx++)
456 tmp_msk &= tmp_msk << 1ULL;
457 /* we found what we were looking for */
459 /* clz will give us offset from end of mask, and
460 * we only get the end of our run, not start,
461 * so adjust result to point to where start
464 run_start = MASK_ALIGN -
465 __builtin_clzll(tmp_msk) - n;
466 return MASK_GET_IDX(msk_idx, run_start);
471 * we didn't find our run within the mask, or n > MASK_ALIGN,
472 * so we're going for plan B.
475 /* count trailing zeroes on inverted mask */
477 ctz = sizeof(cur_msk) * 8;
479 ctz = __builtin_ctzll(~cur_msk);
481 /* if there aren't any runs at the start either, just
487 /* we have a partial run at the start, so try looking behind */
488 run_end = MASK_GET_IDX(msk_idx, ctz);
491 /* go backwards, include zero */
492 lookbehind_idx = msk_idx - 1;
494 /* we can't lookbehind as we've run out of masks, so stop */
499 const uint64_t last_bit = 1ULL << (MASK_ALIGN - 1);
500 unsigned int s_idx, need;
502 lookbehind_msk = msk->data[lookbehind_idx];
504 /* if we're looking for free space, invert the mask */
506 lookbehind_msk = ~lookbehind_msk;
508 /* figure out how many consecutive bits we need here */
509 need = RTE_MIN(left, MASK_ALIGN);
511 for (s_idx = 0; s_idx < need - 1; s_idx++)
512 lookbehind_msk &= lookbehind_msk << 1ULL;
514 /* if last bit is not set, we've lost the run */
515 if ((lookbehind_msk & last_bit) == 0) {
517 * we've scanned this far, so we know there are
518 * no runs in the space we've lookbehind-scanned
519 * as well, so skip that on next iteration.
521 ignore_msk = -1ULL << need;
522 msk_idx = lookbehind_idx;
528 /* check if we've found what we were looking for */
533 } while ((lookbehind_idx--) != 0); /* decrement after check to
537 /* we didn't find anything, so continue */
541 /* we've found what we were looking for, but we only know where
542 * the run ended, so calculate start position.
545 } while (msk_idx-- != 0); /* decrement after check to include zero */
546 /* we didn't find anything */
547 rte_errno = used ? ENOENT : ENOSPC;
552 find_prev(const struct rte_fbarray *arr, unsigned int start, bool used)
554 const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
556 unsigned int idx, first, first_mod;
560 * mask only has granularity of MASK_ALIGN, but start may not be aligned
561 * on that boundary, so construct a special mask to exclude anything we
562 * don't want to see to avoid confusing clz.
564 first = MASK_LEN_TO_IDX(start);
565 first_mod = MASK_LEN_TO_MOD(start);
566 /* we're going backwards, so mask must start from the top */
567 ignore_msk = first_mod == MASK_ALIGN - 1 ?
568 -1ULL : /* prevent overflow */
569 ~(-1ULL << (first_mod + 1));
571 /* go backwards, include zero */
574 uint64_t cur = msk->data[idx];
577 /* if we're looking for free entries, invert mask */
581 /* ignore everything before start on first iteration */
585 /* check if we have any entries */
590 * find last set bit - that will correspond to whatever it is
591 * that we're looking for. we're counting trailing zeroes, thus
592 * the value we get is counted from end of mask, so calculate
593 * position from start of mask.
595 found = MASK_ALIGN - __builtin_clzll(cur) - 1;
597 return MASK_GET_IDX(idx, found);
598 } while (idx-- != 0); /* decrement after check to include zero*/
600 /* we didn't find anything */
601 rte_errno = used ? ENOENT : ENOSPC;
606 find_rev_contig(const struct rte_fbarray *arr, unsigned int start, bool used)
608 const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
610 unsigned int idx, first, first_mod;
611 unsigned int need_len, result = 0;
613 first = MASK_LEN_TO_IDX(start);
614 first_mod = MASK_LEN_TO_MOD(start);
616 /* go backwards, include zero */
619 uint64_t cur = msk->data[idx];
620 unsigned int run_len;
622 need_len = MASK_ALIGN;
624 /* if we're looking for free entries, invert mask */
628 /* ignore everything after start on first iteration */
630 unsigned int end_len = MASK_ALIGN - first_mod - 1;
632 /* at the start, we don't need the full mask len */
636 /* we will be looking for zeroes, so invert the mask */
639 /* if mask is zero, we have a complete run */
644 * see where run ends, starting from the end.
646 run_len = __builtin_clzll(cur);
648 /* add however many zeroes we've had in the last run and quit */
649 if (run_len < need_len) {
655 } while (idx-- != 0); /* decrement after check to include zero */
660 set_used(struct rte_fbarray *arr, unsigned int idx, bool used)
662 struct used_mask *msk;
663 uint64_t msk_bit = 1ULL << MASK_LEN_TO_MOD(idx);
664 unsigned int msk_idx = MASK_LEN_TO_IDX(idx);
668 if (arr == NULL || idx >= arr->len) {
672 msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
675 /* prevent array from changing under us */
676 rte_rwlock_write_lock(&arr->rwlock);
678 already_used = (msk->data[msk_idx] & msk_bit) != 0;
680 /* nothing to be done */
681 if (used == already_used)
685 msk->data[msk_idx] |= msk_bit;
688 msk->data[msk_idx] &= ~msk_bit;
692 rte_rwlock_write_unlock(&arr->rwlock);
698 fully_validate(const char *name, unsigned int elt_sz, unsigned int len)
700 if (name == NULL || elt_sz == 0 || len == 0 || len > INT_MAX) {
705 if (strnlen(name, RTE_FBARRAY_NAME_LEN) == RTE_FBARRAY_NAME_LEN) {
706 rte_errno = ENAMETOOLONG;
713 rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
716 size_t page_sz, mmap_len;
718 struct used_mask *msk;
719 struct mem_area *ma = NULL;
728 if (fully_validate(name, elt_sz, len))
731 /* allocate mem area before doing anything */
732 ma = malloc(sizeof(*ma));
738 page_sz = sysconf(_SC_PAGESIZE);
739 if (page_sz == (size_t)-1) {
744 /* calculate our memory limits */
745 mmap_len = calc_data_size(page_sz, elt_sz, len);
747 data = eal_get_virtual_area(NULL, &mmap_len, page_sz, 0, 0);
753 rte_spinlock_lock(&mem_area_lock);
757 if (internal_config.no_shconf) {
758 /* remap virtual area as writable */
759 void *new_data = mmap(data, mmap_len, PROT_READ | PROT_WRITE,
760 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
761 if (new_data == MAP_FAILED) {
762 RTE_LOG(DEBUG, EAL, "%s(): couldn't remap anonymous memory: %s\n",
763 __func__, strerror(errno));
767 eal_get_fbarray_path(path, sizeof(path), name);
770 * Each fbarray is unique to process namespace, i.e. the
771 * filename depends on process prefix. Try to take out a lock
772 * and see if we succeed. If we don't, someone else is using it
775 fd = open(path, O_CREAT | O_RDWR, 0600);
777 RTE_LOG(DEBUG, EAL, "%s(): couldn't open %s: %s\n",
778 __func__, path, strerror(errno));
781 } else if (flock(fd, LOCK_EX | LOCK_NB)) {
782 RTE_LOG(DEBUG, EAL, "%s(): couldn't lock %s: %s\n",
783 __func__, path, strerror(errno));
788 /* take out a non-exclusive lock, so that other processes could
789 * still attach to it, but no other process could reinitialize
792 if (flock(fd, LOCK_SH | LOCK_NB)) {
797 if (resize_and_map(fd, data, mmap_len))
804 /* do not close fd - keep it until detach/destroy */
805 TAILQ_INSERT_TAIL(&mem_area_tailq, ma, next);
807 /* initialize the data */
808 memset(data, 0, mmap_len);
810 /* populate data structure */
811 strlcpy(arr->name, name, sizeof(arr->name));
814 arr->elt_sz = elt_sz;
817 msk = get_used_mask(data, elt_sz, len);
818 msk->n_masks = MASK_LEN_TO_IDX(RTE_ALIGN_CEIL(len, MASK_ALIGN));
820 rte_rwlock_init(&arr->rwlock);
822 rte_spinlock_unlock(&mem_area_lock);
827 munmap(data, mmap_len);
832 rte_spinlock_unlock(&mem_area_lock);
837 rte_fbarray_attach(struct rte_fbarray *arr)
839 struct mem_area *ma = NULL, *tmp = NULL;
840 size_t page_sz, mmap_len;
851 * we don't need to synchronize attach as two values we need (element
852 * size and array length) are constant for the duration of life of
853 * the array, so the parts we care about will not race.
856 if (fully_validate(arr->name, arr->elt_sz, arr->len))
859 ma = malloc(sizeof(*ma));
865 page_sz = sysconf(_SC_PAGESIZE);
866 if (page_sz == (size_t)-1) {
871 mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
873 /* check the tailq - maybe user has already mapped this address space */
874 rte_spinlock_lock(&mem_area_lock);
876 TAILQ_FOREACH(tmp, &mem_area_tailq, next) {
877 if (overlap(tmp, arr->data, mmap_len)) {
883 /* we know this memory area is unique, so proceed */
885 data = eal_get_virtual_area(arr->data, &mmap_len, page_sz, 0, 0);
889 eal_get_fbarray_path(path, sizeof(path), arr->name);
891 fd = open(path, O_RDWR);
897 /* lock the file, to let others know we're using it */
898 if (flock(fd, LOCK_SH | LOCK_NB)) {
903 if (resize_and_map(fd, data, mmap_len))
906 /* store our new memory area */
908 ma->fd = fd; /* keep fd until detach/destroy */
911 TAILQ_INSERT_TAIL(&mem_area_tailq, ma, next);
915 rte_spinlock_unlock(&mem_area_lock);
919 munmap(data, mmap_len);
923 rte_spinlock_unlock(&mem_area_lock);
928 rte_fbarray_detach(struct rte_fbarray *arr)
930 struct mem_area *tmp = NULL;
940 * we don't need to synchronize detach as two values we need (element
941 * size and total capacity) are constant for the duration of life of
942 * the array, so the parts we care about will not race. if the user is
943 * detaching while doing something else in the same process, we can't
944 * really do anything about it, things will blow up either way.
947 size_t page_sz = sysconf(_SC_PAGESIZE);
949 if (page_sz == (size_t)-1)
952 mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
954 /* does this area exist? */
955 rte_spinlock_lock(&mem_area_lock);
957 TAILQ_FOREACH(tmp, &mem_area_tailq, next) {
958 if (tmp->addr == arr->data && tmp->len == mmap_len)
967 munmap(arr->data, mmap_len);
969 /* area is unmapped, close fd and remove the tailq entry */
972 TAILQ_REMOVE(&mem_area_tailq, tmp, next);
977 rte_spinlock_unlock(&mem_area_lock);
982 rte_fbarray_destroy(struct rte_fbarray *arr)
984 struct mem_area *tmp = NULL;
995 * we don't need to synchronize detach as two values we need (element
996 * size and total capacity) are constant for the duration of life of
997 * the array, so the parts we care about will not race. if the user is
998 * detaching while doing something else in the same process, we can't
999 * really do anything about it, things will blow up either way.
1002 size_t page_sz = sysconf(_SC_PAGESIZE);
1004 if (page_sz == (size_t)-1)
1007 mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
1009 /* does this area exist? */
1010 rte_spinlock_lock(&mem_area_lock);
1012 TAILQ_FOREACH(tmp, &mem_area_tailq, next) {
1013 if (tmp->addr == arr->data && tmp->len == mmap_len)
1021 /* with no shconf, there were never any files to begin with */
1022 if (!internal_config.no_shconf) {
1024 * attempt to get an exclusive lock on the file, to ensure it
1025 * has been detached by all other processes
1028 if (flock(fd, LOCK_EX | LOCK_NB)) {
1029 RTE_LOG(DEBUG, EAL, "Cannot destroy fbarray - another process is using it\n");
1035 /* we're OK to destroy the file */
1036 eal_get_fbarray_path(path, sizeof(path), arr->name);
1038 RTE_LOG(DEBUG, EAL, "Cannot unlink fbarray: %s\n",
1042 * we're still holding an exclusive lock, so drop it to
1045 flock(fd, LOCK_SH | LOCK_NB);
1052 munmap(arr->data, mmap_len);
1054 /* area is unmapped, remove the tailq entry */
1055 TAILQ_REMOVE(&mem_area_tailq, tmp, next);
1059 /* reset the fbarray structure */
1060 memset(arr, 0, sizeof(*arr));
1062 rte_spinlock_unlock(&mem_area_lock);
1067 rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
1075 if (idx >= arr->len) {
1080 ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
1086 rte_fbarray_set_used(struct rte_fbarray *arr, unsigned int idx)
1088 return set_used(arr, idx, true);
1092 rte_fbarray_set_free(struct rte_fbarray *arr, unsigned int idx)
1094 return set_used(arr, idx, false);
1098 rte_fbarray_is_used(struct rte_fbarray *arr, unsigned int idx)
1100 struct used_mask *msk;
1105 if (arr == NULL || idx >= arr->len) {
1110 /* prevent array from changing under us */
1111 rte_rwlock_read_lock(&arr->rwlock);
1113 msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
1114 msk_idx = MASK_LEN_TO_IDX(idx);
1115 msk_bit = 1ULL << MASK_LEN_TO_MOD(idx);
1117 ret = (msk->data[msk_idx] & msk_bit) != 0;
1119 rte_rwlock_read_unlock(&arr->rwlock);
1125 fbarray_find(struct rte_fbarray *arr, unsigned int start, bool next, bool used)
1129 if (arr == NULL || start >= arr->len) {
1134 /* prevent array from changing under us */
1135 rte_rwlock_read_lock(&arr->rwlock);
1137 /* cheap checks to prevent doing useless work */
1139 if (arr->len == arr->count) {
1143 if (arr->count == 0) {
1148 if (arr->count == 0) {
1152 if (arr->len == arr->count) {
1158 ret = find_next(arr, start, used);
1160 ret = find_prev(arr, start, used);
1162 rte_rwlock_read_unlock(&arr->rwlock);
1167 rte_fbarray_find_next_free(struct rte_fbarray *arr, unsigned int start)
1169 return fbarray_find(arr, start, true, false);
1173 rte_fbarray_find_next_used(struct rte_fbarray *arr, unsigned int start)
1175 return fbarray_find(arr, start, true, true);
1179 rte_fbarray_find_prev_free(struct rte_fbarray *arr, unsigned int start)
1181 return fbarray_find(arr, start, false, false);
1185 rte_fbarray_find_prev_used(struct rte_fbarray *arr, unsigned int start)
1187 return fbarray_find(arr, start, false, true);
1191 fbarray_find_n(struct rte_fbarray *arr, unsigned int start, unsigned int n,
1192 bool next, bool used)
1196 if (arr == NULL || start >= arr->len || n > arr->len || n == 0) {
1200 if (next && (arr->len - start) < n) {
1201 rte_errno = used ? ENOENT : ENOSPC;
1204 if (!next && start < (n - 1)) {
1205 rte_errno = used ? ENOENT : ENOSPC;
1209 /* prevent array from changing under us */
1210 rte_rwlock_read_lock(&arr->rwlock);
1212 /* cheap checks to prevent doing useless work */
1214 if (arr->len == arr->count || arr->len - arr->count < n) {
1218 if (arr->count == 0) {
1219 ret = next ? start : start - n + 1;
1223 if (arr->count < n) {
1227 if (arr->count == arr->len) {
1228 ret = next ? start : start - n + 1;
1234 ret = find_next_n(arr, start, n, used);
1236 ret = find_prev_n(arr, start, n, used);
1238 rte_rwlock_read_unlock(&arr->rwlock);
1243 rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start,
1246 return fbarray_find_n(arr, start, n, true, false);
1250 rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start,
1253 return fbarray_find_n(arr, start, n, true, true);
1257 rte_fbarray_find_prev_n_free(struct rte_fbarray *arr, unsigned int start,
1260 return fbarray_find_n(arr, start, n, false, false);
1264 rte_fbarray_find_prev_n_used(struct rte_fbarray *arr, unsigned int start,
1267 return fbarray_find_n(arr, start, n, false, true);
1271 fbarray_find_contig(struct rte_fbarray *arr, unsigned int start, bool next,
1276 if (arr == NULL || start >= arr->len) {
1281 /* prevent array from changing under us */
1282 rte_rwlock_read_lock(&arr->rwlock);
1284 /* cheap checks to prevent doing useless work */
1286 if (arr->count == 0) {
1290 if (next && arr->count == arr->len) {
1291 ret = arr->len - start;
1294 if (!next && arr->count == arr->len) {
1299 if (arr->len == arr->count) {
1303 if (next && arr->count == 0) {
1304 ret = arr->len - start;
1307 if (!next && arr->count == 0) {
1314 ret = find_contig(arr, start, used);
1316 ret = find_rev_contig(arr, start, used);
1318 rte_rwlock_read_unlock(&arr->rwlock);
1323 fbarray_find_biggest(struct rte_fbarray *arr, unsigned int start, bool used,
1326 int cur_idx, next_idx, cur_len, biggest_idx, biggest_len;
1327 /* don't stack if conditions, use function pointers instead */
1328 int (*find_func)(struct rte_fbarray *, unsigned int);
1329 int (*find_contig_func)(struct rte_fbarray *, unsigned int);
1331 if (arr == NULL || start >= arr->len) {
1335 /* the other API calls already do their fair share of cheap checks, so
1336 * no need to do them here.
1339 /* the API's called are thread-safe, but something may still happen
1340 * inbetween the API calls, so lock the fbarray. all other API's are
1341 * read-locking the fbarray, so read lock here is OK.
1343 rte_rwlock_read_lock(&arr->rwlock);
1345 /* pick out appropriate functions */
1348 find_func = rte_fbarray_find_prev_used;
1349 find_contig_func = rte_fbarray_find_rev_contig_used;
1351 find_func = rte_fbarray_find_next_used;
1352 find_contig_func = rte_fbarray_find_contig_used;
1356 find_func = rte_fbarray_find_prev_free;
1357 find_contig_func = rte_fbarray_find_rev_contig_free;
1359 find_func = rte_fbarray_find_next_free;
1360 find_contig_func = rte_fbarray_find_contig_free;
1365 biggest_idx = -1; /* default is error */
1368 cur_idx = find_func(arr, cur_idx);
1370 /* block found, check its length */
1372 cur_len = find_contig_func(arr, cur_idx);
1373 /* decide where we go next */
1374 next_idx = rev ? cur_idx - cur_len : cur_idx + cur_len;
1375 /* move current index to start of chunk */
1376 cur_idx = rev ? next_idx + 1 : cur_idx;
1378 if (cur_len > biggest_len) {
1379 biggest_idx = cur_idx;
1380 biggest_len = cur_len;
1383 /* in reverse mode, next_idx may be -1 if chunk started
1384 * at array beginning. this means there's no more work
1390 /* nothing more to find, stop. however, a failed API
1391 * call has set rte_errno, which we want to ignore, as
1392 * reaching the end of fbarray is not an error.
1398 /* if we didn't find anything at all, set rte_errno */
1399 if (biggest_idx < 0)
1400 rte_errno = used ? ENOENT : ENOSPC;
1402 rte_rwlock_read_unlock(&arr->rwlock);
1407 rte_fbarray_find_biggest_free(struct rte_fbarray *arr, unsigned int start)
1409 return fbarray_find_biggest(arr, start, false, false);
1413 rte_fbarray_find_biggest_used(struct rte_fbarray *arr, unsigned int start)
1415 return fbarray_find_biggest(arr, start, true, false);
1419 rte_fbarray_find_rev_biggest_free(struct rte_fbarray *arr, unsigned int start)
1421 return fbarray_find_biggest(arr, start, false, true);
1425 rte_fbarray_find_rev_biggest_used(struct rte_fbarray *arr, unsigned int start)
1427 return fbarray_find_biggest(arr, start, true, true);
1432 rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start)
1434 return fbarray_find_contig(arr, start, true, false);
1438 rte_fbarray_find_contig_used(struct rte_fbarray *arr, unsigned int start)
1440 return fbarray_find_contig(arr, start, true, true);
1444 rte_fbarray_find_rev_contig_free(struct rte_fbarray *arr, unsigned int start)
1446 return fbarray_find_contig(arr, start, false, false);
1450 rte_fbarray_find_rev_contig_used(struct rte_fbarray *arr, unsigned int start)
1452 return fbarray_find_contig(arr, start, false, true);
1456 rte_fbarray_find_idx(const struct rte_fbarray *arr, const void *elt)
1462 * no need to synchronize as it doesn't matter if underlying data
1463 * changes - we're doing pointer arithmetic here.
1466 if (arr == NULL || elt == NULL) {
1470 end = RTE_PTR_ADD(arr->data, arr->elt_sz * arr->len);
1471 if (elt < arr->data || elt >= end) {
1476 ret = RTE_PTR_DIFF(elt, arr->data) / arr->elt_sz;
1482 rte_fbarray_dump_metadata(struct rte_fbarray *arr, FILE *f)
1484 struct used_mask *msk;
1487 if (arr == NULL || f == NULL) {
1492 if (fully_validate(arr->name, arr->elt_sz, arr->len)) {
1493 fprintf(f, "Invalid file-backed array\n");
1497 /* prevent array from changing under us */
1498 rte_rwlock_read_lock(&arr->rwlock);
1500 fprintf(f, "File-backed array: %s\n", arr->name);
1501 fprintf(f, "size: %i occupied: %i elt_sz: %i\n",
1502 arr->len, arr->count, arr->elt_sz);
1504 msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
1506 for (i = 0; i < msk->n_masks; i++)
1507 fprintf(f, "msk idx %i: 0x%016" PRIx64 "\n", i, msk->data[i]);
1509 rte_rwlock_read_unlock(&arr->rwlock);