-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy patheal_memory.c
1945 lines (1689 loc) · 51.6 KB
/
eal_memory.c
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2014 Intel Corporation.
* Copyright(c) 2013 6WIND S.A.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/resource.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <setjmp.h>
#ifdef F_ADD_SEALS /* if file sealing is supported, so is memfd */
#define MEMFD_SUPPORTED
#endif
#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
#include <numa.h>
#include <numaif.h>
#endif
#include <rte_errno.h>
#include <rte_log.h>
#include <rte_memory.h>
#include <rte_eal.h>
#include <rte_lcore.h>
#include <rte_common.h>
#include "eal_private.h"
#include "eal_memalloc.h"
#include "eal_memcfg.h"
#include "eal_internal_cfg.h"
#include "eal_filesystem.h"
#include "eal_hugepages.h"
#include "eal_options.h"
#define PFN_MASK_SIZE 8
/**
* @file
* Huge page mapping under linux
*
* To reserve a big contiguous amount of memory, we use the hugepage
* feature of linux. For that, we need to have hugetlbfs mounted. This
* code will create many files in this directory (one per page) and
* map them in virtual memory. For each page, we will retrieve its
* physical address and remap it in order to have a virtual contiguous
* zone as well as a physical contiguous zone.
*/
static int phys_addrs_available = -1;
#define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
uint64_t eal_get_baseaddr(void)
{
/*
* Linux kernel uses a really high address as starting address for
* serving mmaps calls. If there exists addressing limitations and IOVA
* mode is VA, this starting address is likely too high for those
* devices. However, it is possible to use a lower address in the
* process virtual address space as with 64 bits there is a lot of
* available space.
*
* Current known limitations are 39 or 40 bits. Setting the starting
* address at 4GB implies there are 508GB or 1020GB for mapping the
* available hugepages. This is likely enough for most systems, although
* a device with addressing limitations should call
* rte_mem_check_dma_mask for ensuring all memory is within supported
* range.
*/
#if defined(RTE_ARCH_LOONGARCH)
return 0x7000000000ULL;
#else
return 0x100000000ULL;
#endif
}
/*
* Get physical address of any mapped virtual address in the current process.
*/
phys_addr_t
rte_mem_virt2phy(const void *virtaddr)
{
int fd, retval;
uint64_t page, physaddr;
unsigned long virt_pfn;
int page_size;
off_t offset;
if (phys_addrs_available == 0)
return RTE_BAD_IOVA;
/* standard page size */
page_size = getpagesize();
fd = open("/proc/self/pagemap", O_RDONLY);
if (fd < 0) {
RTE_LOG(INFO, EAL, "%s(): cannot open /proc/self/pagemap: %s\n",
__func__, strerror(errno));
return RTE_BAD_IOVA;
}
virt_pfn = (unsigned long)virtaddr / page_size;
offset = sizeof(uint64_t) * virt_pfn;
if (lseek(fd, offset, SEEK_SET) == (off_t) -1) {
RTE_LOG(INFO, EAL, "%s(): seek error in /proc/self/pagemap: %s\n",
__func__, strerror(errno));
close(fd);
return RTE_BAD_IOVA;
}
retval = read(fd, &page, PFN_MASK_SIZE);
close(fd);
if (retval < 0) {
RTE_LOG(INFO, EAL, "%s(): cannot read /proc/self/pagemap: %s\n",
__func__, strerror(errno));
return RTE_BAD_IOVA;
} else if (retval != PFN_MASK_SIZE) {
RTE_LOG(INFO, EAL, "%s(): read %d bytes from /proc/self/pagemap "
"but expected %d:\n",
__func__, retval, PFN_MASK_SIZE);
return RTE_BAD_IOVA;
}
/*
* the pfn (page frame number) are bits 0-54 (see
* pagemap.txt in linux Documentation)
*/
if ((page & 0x7fffffffffffffULL) == 0)
return RTE_BAD_IOVA;
physaddr = ((page & 0x7fffffffffffffULL) * page_size)
+ ((unsigned long)virtaddr % page_size);
return physaddr;
}
rte_iova_t
rte_mem_virt2iova(const void *virtaddr)
{
if (rte_eal_iova_mode() == RTE_IOVA_VA)
return (uintptr_t)virtaddr;
return rte_mem_virt2phy(virtaddr);
}
/*
* For each hugepage in hugepg_tbl, fill the physaddr value. We find
* it by browsing the /proc/self/pagemap special file.
*
* NOTE: Unless a kernel component (such as VFIO_NOIOMMU) locks the memory
* in place, the kernel may move the memory at any time (kcompactd, autonuma,
* etc etc) so one should only rely on this address being a constant in the
* general case.
*/
static int
find_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
{
unsigned int i;
phys_addr_t addr;
for (i = 0; i < hpi->num_pages[0]; i++) {
addr = rte_mem_virt2phy(hugepg_tbl[i].orig_va);
if (addr == RTE_BAD_PHYS_ADDR)
return -1;
hugepg_tbl[i].physaddr = addr;
}
return 0;
}
/*
* For each hugepage in hugepg_tbl, fill the physaddr value sequentially.
*/
static int
set_physaddrs(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
{
unsigned int i;
static phys_addr_t addr;
for (i = 0; i < hpi->num_pages[0]; i++) {
hugepg_tbl[i].physaddr = addr;
addr += hugepg_tbl[i].size;
}
return 0;
}
/*
* Check whether address-space layout randomization is enabled in
* the kernel. This is important for multi-process as it can prevent
* two processes mapping data to the same virtual address
* Returns:
* 0 - address space randomization disabled
* 1/2 - address space randomization enabled
* negative error code on error
*/
static int
aslr_enabled(void)
{
char c;
int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
if (fd < 0)
return -errno;
retval = read(fd, &c, 1);
close(fd);
if (retval < 0)
return -errno;
if (retval == 0)
return -EIO;
switch (c) {
case '0' : return 0;
case '1' : return 1;
case '2' : return 2;
default: return -EINVAL;
}
}
static sigjmp_buf huge_jmpenv;
static void huge_sigbus_handler(int signo __rte_unused)
{
siglongjmp(huge_jmpenv, 1);
}
/* Put setjmp into a wrap method to avoid compiling error. Any non-volatile,
* non-static local variable in the stack frame calling sigsetjmp might be
* clobbered by a call to longjmp.
*/
static int huge_wrap_sigsetjmp(void)
{
return sigsetjmp(huge_jmpenv, 1);
}
#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
/* Callback for numa library. */
void numa_error(char *where)
{
RTE_LOG(ERR, EAL, "%s failed: %s\n", where, strerror(errno));
}
#endif
/*
* Mmap all hugepages of hugepage table: it first open a file in
* hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
* virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
* in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
* map contiguous physical blocks in contiguous virtual blocks.
*/
static unsigned
map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi,
uint64_t *essential_memory __rte_unused)
{
int fd;
unsigned i;
void *virtaddr;
#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
int node_id = -1;
int essential_prev = 0;
int oldpolicy;
struct bitmask *oldmask = NULL;
bool have_numa = true;
unsigned long maxnode = 0;
const struct internal_config *internal_conf =
eal_get_internal_configuration();
/* Check if kernel supports NUMA. */
if (numa_available() != 0) {
RTE_LOG(DEBUG, EAL, "NUMA is not supported.\n");
have_numa = false;
}
if (have_numa) {
RTE_LOG(DEBUG, EAL, "Trying to obtain current memory policy.\n");
oldmask = numa_allocate_nodemask();
if (get_mempolicy(&oldpolicy, oldmask->maskp,
oldmask->size + 1, 0, 0) < 0) {
RTE_LOG(ERR, EAL,
"Failed to get current mempolicy: %s. "
"Assuming MPOL_DEFAULT.\n", strerror(errno));
oldpolicy = MPOL_DEFAULT;
}
for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
if (internal_conf->socket_mem[i])
maxnode = i + 1;
}
#endif
for (i = 0; i < hpi->num_pages[0]; i++) {
struct hugepage_file *hf = &hugepg_tbl[i];
uint64_t hugepage_sz = hpi->hugepage_sz;
#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
if (maxnode) {
unsigned int j;
for (j = 0; j < maxnode; j++)
if (essential_memory[j])
break;
if (j == maxnode) {
node_id = (node_id + 1) % maxnode;
while (!internal_conf->socket_mem[node_id]) {
node_id++;
node_id %= maxnode;
}
essential_prev = 0;
} else {
node_id = j;
essential_prev = essential_memory[j];
if (essential_memory[j] < hugepage_sz)
essential_memory[j] = 0;
else
essential_memory[j] -= hugepage_sz;
}
RTE_LOG(DEBUG, EAL,
"Setting policy MPOL_PREFERRED for socket %d\n",
node_id);
numa_set_preferred(node_id);
}
#endif
hf->file_id = i;
hf->size = hugepage_sz;
eal_get_hugefile_path(hf->filepath, sizeof(hf->filepath),
hpi->hugedir, hf->file_id);
hf->filepath[sizeof(hf->filepath) - 1] = '\0';
/* try to create hugepage file */
fd = open(hf->filepath, O_CREAT | O_RDWR, 0600);
if (fd < 0) {
RTE_LOG(DEBUG, EAL, "%s(): open failed: %s\n", __func__,
strerror(errno));
goto out;
}
/* map the segment, and populate page tables,
* the kernel fills this segment with zeros. we don't care where
* this gets mapped - we already have contiguous memory areas
* ready for us to map into.
*/
virtaddr = mmap(NULL, hugepage_sz, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, fd, 0);
if (virtaddr == MAP_FAILED) {
RTE_LOG(DEBUG, EAL, "%s(): mmap failed: %s\n", __func__,
strerror(errno));
close(fd);
goto out;
}
hf->orig_va = virtaddr;
/* In linux, hugetlb limitations, like cgroup, are
* enforced at fault time instead of mmap(), even
* with the option of MAP_POPULATE. Kernel will send
* a SIGBUS signal. To avoid to be killed, save stack
* environment here, if SIGBUS happens, we can jump
* back here.
*/
if (huge_wrap_sigsetjmp()) {
RTE_LOG(DEBUG, EAL, "SIGBUS: Cannot mmap more "
"hugepages of size %u MB\n",
(unsigned int)(hugepage_sz / 0x100000));
munmap(virtaddr, hugepage_sz);
close(fd);
unlink(hugepg_tbl[i].filepath);
#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
if (maxnode)
essential_memory[node_id] =
essential_prev;
#endif
goto out;
}
*(int *)virtaddr = 0;
/* set shared lock on the file. */
if (flock(fd, LOCK_SH) < 0) {
RTE_LOG(DEBUG, EAL, "%s(): Locking file failed:%s \n",
__func__, strerror(errno));
close(fd);
goto out;
}
close(fd);
}
out:
#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
if (maxnode) {
RTE_LOG(DEBUG, EAL,
"Restoring previous memory policy: %d\n", oldpolicy);
if (oldpolicy == MPOL_DEFAULT) {
numa_set_localalloc();
} else if (set_mempolicy(oldpolicy, oldmask->maskp,
oldmask->size + 1) < 0) {
RTE_LOG(ERR, EAL, "Failed to restore mempolicy: %s\n",
strerror(errno));
numa_set_localalloc();
}
}
if (oldmask != NULL)
numa_free_cpumask(oldmask);
#endif
return i;
}
/*
* Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
* page.
*/
static int
find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi)
{
int socket_id;
char *end, *nodestr;
unsigned i, hp_count = 0;
uint64_t virt_addr;
char buf[BUFSIZ];
char hugedir_str[PATH_MAX];
FILE *f;
f = fopen("/proc/self/numa_maps", "r");
if (f == NULL) {
RTE_LOG(NOTICE, EAL, "NUMA support not available"
" consider that all memory is in socket_id 0\n");
return 0;
}
snprintf(hugedir_str, sizeof(hugedir_str),
"%s/%s", hpi->hugedir, eal_get_hugefile_prefix());
/* parse numa map */
while (fgets(buf, sizeof(buf), f) != NULL) {
/* ignore non huge page */
if (strstr(buf, " huge ") == NULL &&
strstr(buf, hugedir_str) == NULL)
continue;
/* get zone addr */
virt_addr = strtoull(buf, &end, 16);
if (virt_addr == 0 || end == buf) {
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
goto error;
}
/* get node id (socket id) */
nodestr = strstr(buf, " N");
if (nodestr == NULL) {
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
goto error;
}
nodestr += 2;
end = strstr(nodestr, "=");
if (end == NULL) {
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
goto error;
}
end[0] = '\0';
end = NULL;
socket_id = strtoul(nodestr, &end, 0);
if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
goto error;
}
/* if we find this page in our mappings, set socket_id */
for (i = 0; i < hpi->num_pages[0]; i++) {
void *va = (void *)(unsigned long)virt_addr;
if (hugepg_tbl[i].orig_va == va) {
hugepg_tbl[i].socket_id = socket_id;
hp_count++;
#ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES
RTE_LOG(DEBUG, EAL,
"Hugepage %s is on socket %d\n",
hugepg_tbl[i].filepath, socket_id);
#endif
}
}
}
if (hp_count < hpi->num_pages[0])
goto error;
fclose(f);
return 0;
error:
fclose(f);
return -1;
}
static int
cmp_physaddr(const void *a, const void *b)
{
#ifndef RTE_ARCH_PPC_64
const struct hugepage_file *p1 = a;
const struct hugepage_file *p2 = b;
#else
/* PowerPC needs memory sorted in reverse order from x86 */
const struct hugepage_file *p1 = b;
const struct hugepage_file *p2 = a;
#endif
if (p1->physaddr < p2->physaddr)
return -1;
else if (p1->physaddr > p2->physaddr)
return 1;
else
return 0;
}
/*
* Uses mmap to create a shared memory area for storage of data
* Used in this file to store the hugepage file map on disk
*/
static void *
create_shared_memory(const char *filename, const size_t mem_size)
{
void *retval;
int fd;
const struct internal_config *internal_conf =
eal_get_internal_configuration();
/* if no shared files mode is used, create anonymous memory instead */
if (internal_conf->no_shconf) {
retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (retval == MAP_FAILED)
return NULL;
return retval;
}
fd = open(filename, O_CREAT | O_RDWR, 0600);
if (fd < 0)
return NULL;
if (ftruncate(fd, mem_size) < 0) {
close(fd);
return NULL;
}
retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (retval == MAP_FAILED)
return NULL;
return retval;
}
/*
* this copies *active* hugepages from one hugepage table to another.
* destination is typically the shared memory.
*/
static int
copy_hugepages_to_shared_mem(struct hugepage_file * dst, int dest_size,
const struct hugepage_file * src, int src_size)
{
int src_pos, dst_pos = 0;
for (src_pos = 0; src_pos < src_size; src_pos++) {
if (src[src_pos].orig_va != NULL) {
/* error on overflow attempt */
if (dst_pos == dest_size)
return -1;
memcpy(&dst[dst_pos], &src[src_pos], sizeof(struct hugepage_file));
dst_pos++;
}
}
return 0;
}
static int
unlink_hugepage_files(struct hugepage_file *hugepg_tbl,
unsigned num_hp_info)
{
unsigned socket, size;
int page, nrpages = 0;
const struct internal_config *internal_conf =
eal_get_internal_configuration();
/* get total number of hugepages */
for (size = 0; size < num_hp_info; size++)
for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
nrpages +=
internal_conf->hugepage_info[size].num_pages[socket];
for (page = 0; page < nrpages; page++) {
struct hugepage_file *hp = &hugepg_tbl[page];
if (hp->orig_va != NULL && unlink(hp->filepath)) {
RTE_LOG(WARNING, EAL, "%s(): Removing %s failed: %s\n",
__func__, hp->filepath, strerror(errno));
}
}
return 0;
}
/*
* unmaps hugepages that are not going to be used. since we originally allocate
* ALL hugepages (not just those we need), additional unmapping needs to be done.
*/
static int
unmap_unneeded_hugepages(struct hugepage_file *hugepg_tbl,
struct hugepage_info *hpi,
unsigned num_hp_info)
{
unsigned socket, size;
int page, nrpages = 0;
const struct internal_config *internal_conf =
eal_get_internal_configuration();
/* get total number of hugepages */
for (size = 0; size < num_hp_info; size++)
for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++)
nrpages += internal_conf->hugepage_info[size].num_pages[socket];
for (size = 0; size < num_hp_info; size++) {
for (socket = 0; socket < RTE_MAX_NUMA_NODES; socket++) {
unsigned pages_found = 0;
/* traverse until we have unmapped all the unused pages */
for (page = 0; page < nrpages; page++) {
struct hugepage_file *hp = &hugepg_tbl[page];
/* find a page that matches the criteria */
if ((hp->size == hpi[size].hugepage_sz) &&
(hp->socket_id == (int) socket)) {
/* if we skipped enough pages, unmap the rest */
if (pages_found == hpi[size].num_pages[socket]) {
uint64_t unmap_len;
unmap_len = hp->size;
/* get start addr and len of the remaining segment */
munmap(hp->orig_va,
(size_t)unmap_len);
hp->orig_va = NULL;
if (unlink(hp->filepath) == -1) {
RTE_LOG(ERR, EAL, "%s(): Removing %s failed: %s\n",
__func__, hp->filepath, strerror(errno));
return -1;
}
} else {
/* lock the page and skip */
pages_found++;
}
} /* match page */
} /* foreach page */
} /* foreach socket */
} /* foreach pagesize */
return 0;
}
static int
remap_segment(struct hugepage_file *hugepages, int seg_start, int seg_end)
{
struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
struct rte_memseg_list *msl;
struct rte_fbarray *arr;
int cur_page, seg_len;
unsigned int msl_idx;
int ms_idx;
uint64_t page_sz;
size_t memseg_len;
int socket_id;
#ifndef RTE_ARCH_64
const struct internal_config *internal_conf =
eal_get_internal_configuration();
#endif
page_sz = hugepages[seg_start].size;
socket_id = hugepages[seg_start].socket_id;
seg_len = seg_end - seg_start;
RTE_LOG(DEBUG, EAL, "Attempting to map %" PRIu64 "M on socket %i\n",
(seg_len * page_sz) >> 20ULL, socket_id);
/* find free space in memseg lists */
for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS; msl_idx++) {
bool empty;
msl = &mcfg->memsegs[msl_idx];
arr = &msl->memseg_arr;
if (msl->page_sz != page_sz)
continue;
if (msl->socket_id != socket_id)
continue;
/* leave space for a hole if array is not empty */
empty = arr->count == 0;
ms_idx = rte_fbarray_find_next_n_free(arr, 0,
seg_len + (empty ? 0 : 1));
/* memseg list is full? */
if (ms_idx < 0)
continue;
/* leave some space between memsegs, they are not IOVA
* contiguous, so they shouldn't be VA contiguous either.
*/
if (!empty)
ms_idx++;
break;
}
if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
RTE_LOG(ERR, EAL, "Could not find space for memseg. Please increase %s and/or %s in configuration.\n",
RTE_STR(RTE_MAX_MEMSEG_PER_TYPE),
RTE_STR(RTE_MAX_MEM_MB_PER_TYPE));
return -1;
}
#ifdef RTE_ARCH_PPC_64
/* for PPC64 we go through the list backwards */
for (cur_page = seg_end - 1; cur_page >= seg_start;
cur_page--, ms_idx++) {
#else
for (cur_page = seg_start; cur_page < seg_end; cur_page++, ms_idx++) {
#endif
struct hugepage_file *hfile = &hugepages[cur_page];
struct rte_memseg *ms = rte_fbarray_get(arr, ms_idx);
void *addr;
int fd;
fd = open(hfile->filepath, O_RDWR);
if (fd < 0) {
RTE_LOG(ERR, EAL, "Could not open '%s': %s\n",
hfile->filepath, strerror(errno));
return -1;
}
/* set shared lock on the file. */
if (flock(fd, LOCK_SH) < 0) {
RTE_LOG(DEBUG, EAL, "Could not lock '%s': %s\n",
hfile->filepath, strerror(errno));
close(fd);
return -1;
}
memseg_len = (size_t)page_sz;
addr = RTE_PTR_ADD(msl->base_va, ms_idx * memseg_len);
/* we know this address is already mmapped by memseg list, so
* using MAP_FIXED here is safe
*/
addr = mmap(addr, page_sz, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd, 0);
if (addr == MAP_FAILED) {
RTE_LOG(ERR, EAL, "Couldn't remap '%s': %s\n",
hfile->filepath, strerror(errno));
close(fd);
return -1;
}
/* we have a new address, so unmap previous one */
#ifndef RTE_ARCH_64
/* in 32-bit legacy mode, we have already unmapped the page */
if (!internal_conf->legacy_mem)
munmap(hfile->orig_va, page_sz);
#else
munmap(hfile->orig_va, page_sz);
#endif
hfile->orig_va = NULL;
hfile->final_va = addr;
/* rewrite physical addresses in IOVA as VA mode */
if (rte_eal_iova_mode() == RTE_IOVA_VA)
hfile->physaddr = (uintptr_t)addr;
/* set up memseg data */
ms->addr = addr;
ms->hugepage_sz = page_sz;
ms->len = memseg_len;
ms->iova = hfile->physaddr;
ms->socket_id = hfile->socket_id;
ms->nchannel = rte_memory_get_nchannel();
ms->nrank = rte_memory_get_nrank();
rte_fbarray_set_used(arr, ms_idx);
/* store segment fd internally */
if (eal_memalloc_set_seg_fd(msl_idx, ms_idx, fd) < 0)
RTE_LOG(ERR, EAL, "Could not store segment fd: %s\n",
rte_strerror(rte_errno));
}
RTE_LOG(DEBUG, EAL, "Allocated %" PRIu64 "M on socket %i\n",
(seg_len * page_sz) >> 20, socket_id);
return 0;
}
static uint64_t
get_mem_amount(uint64_t page_sz, uint64_t max_mem)
{
uint64_t area_sz, max_pages;
/* limit to RTE_MAX_MEMSEG_PER_LIST pages or RTE_MAX_MEM_MB_PER_LIST */
max_pages = RTE_MAX_MEMSEG_PER_LIST;
max_mem = RTE_MIN((uint64_t)RTE_MAX_MEM_MB_PER_LIST << 20, max_mem);
area_sz = RTE_MIN(page_sz * max_pages, max_mem);
/* make sure the list isn't smaller than the page size */
area_sz = RTE_MAX(area_sz, page_sz);
return RTE_ALIGN(area_sz, page_sz);
}
static int
memseg_list_free(struct rte_memseg_list *msl)
{
if (rte_fbarray_destroy(&msl->memseg_arr)) {
RTE_LOG(ERR, EAL, "Cannot destroy memseg list\n");
return -1;
}
memset(msl, 0, sizeof(*msl));
return 0;
}
/*
* Our VA space is not preallocated yet, so preallocate it here. We need to know
* how many segments there are in order to map all pages into one address space,
* and leave appropriate holes between segments so that rte_malloc does not
* concatenate them into one big segment.
*
* we also need to unmap original pages to free up address space.
*/
static int __rte_unused
prealloc_segments(struct hugepage_file *hugepages, int n_pages)
{
struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
int cur_page, seg_start_page, end_seg, new_memseg;
unsigned int hpi_idx, socket, i;
int n_contig_segs, n_segs;
int msl_idx;
const struct internal_config *internal_conf =
eal_get_internal_configuration();
/* before we preallocate segments, we need to free up our VA space.
* we're not removing files, and we already have information about
* PA-contiguousness, so it is safe to unmap everything.
*/
for (cur_page = 0; cur_page < n_pages; cur_page++) {
struct hugepage_file *hpi = &hugepages[cur_page];
munmap(hpi->orig_va, hpi->size);
hpi->orig_va = NULL;
}
/* we cannot know how many page sizes and sockets we have discovered, so
* loop over all of them
*/
for (hpi_idx = 0; hpi_idx < internal_conf->num_hugepage_sizes;
hpi_idx++) {
uint64_t page_sz =
internal_conf->hugepage_info[hpi_idx].hugepage_sz;
for (i = 0; i < rte_socket_count(); i++) {
struct rte_memseg_list *msl;
socket = rte_socket_id_by_idx(i);
n_contig_segs = 0;
n_segs = 0;
seg_start_page = -1;
for (cur_page = 0; cur_page < n_pages; cur_page++) {
struct hugepage_file *prev, *cur;
int prev_seg_start_page = -1;
cur = &hugepages[cur_page];
prev = cur_page == 0 ? NULL :
&hugepages[cur_page - 1];
new_memseg = 0;
end_seg = 0;
if (cur->size == 0)
end_seg = 1;
else if (cur->socket_id != (int) socket)
end_seg = 1;
else if (cur->size != page_sz)
end_seg = 1;
else if (cur_page == 0)
new_memseg = 1;
#ifdef RTE_ARCH_PPC_64
/* On PPC64 architecture, the mmap always start
* from higher address to lower address. Here,
* physical addresses are in descending order.
*/
else if ((prev->physaddr - cur->physaddr) !=
cur->size)
new_memseg = 1;
#else
else if ((cur->physaddr - prev->physaddr) !=
cur->size)
new_memseg = 1;
#endif
if (new_memseg) {
/* if we're already inside a segment,
* new segment means end of current one
*/
if (seg_start_page != -1) {
end_seg = 1;
prev_seg_start_page =
seg_start_page;
}
seg_start_page = cur_page;
}
if (end_seg) {
if (prev_seg_start_page != -1) {
/* we've found a new segment */
n_contig_segs++;
n_segs += cur_page -
prev_seg_start_page;
} else if (seg_start_page != -1) {
/* we didn't find new segment,
* but did end current one
*/
n_contig_segs++;
n_segs += cur_page -
seg_start_page;
seg_start_page = -1;
continue;
} else {
/* we're skipping this page */
continue;
}
}
/* segment continues */
}
/* check if we missed last segment */
if (seg_start_page != -1) {
n_contig_segs++;
n_segs += cur_page - seg_start_page;
}
/* if no segments were found, do not preallocate */
if (n_segs == 0)
continue;
/* we now have total number of pages that we will
* allocate for this segment list. add separator pages
* to the total count, and preallocate VA space.
*/
n_segs += n_contig_segs - 1;
/* now, preallocate VA space for these segments */
/* first, find suitable memseg list for this */
for (msl_idx = 0; msl_idx < RTE_MAX_MEMSEG_LISTS;
msl_idx++) {
msl = &mcfg->memsegs[msl_idx];
if (msl->base_va != NULL)
continue;
break;
}
if (msl_idx == RTE_MAX_MEMSEG_LISTS) {
RTE_LOG(ERR, EAL, "Not enough space in memseg lists, please increase %s\n",
RTE_STR(RTE_MAX_MEMSEG_LISTS));
return -1;
}
/* now, allocate fbarray itself */
if (eal_memseg_list_init(msl, page_sz, n_segs,
socket, msl_idx, true) < 0)
return -1;
/* finally, allocate VA space */
if (eal_memseg_list_alloc(msl, 0) < 0) {
RTE_LOG(ERR, EAL, "Cannot preallocate 0x%"PRIx64"kB hugepages\n",
page_sz >> 10);
return -1;
}
}
}
return 0;
}
/*
* We cannot reallocate memseg lists on the fly because PPC64 stores pages
* backwards, therefore we have to process the entire memseg first before
* remapping it into memseg list VA space.
*/
static int
remap_needed_hugepages(struct hugepage_file *hugepages, int n_pages)
{
int cur_page, seg_start_page, new_memseg, ret;
seg_start_page = 0;
for (cur_page = 0; cur_page < n_pages; cur_page++) {
struct hugepage_file *prev, *cur;
new_memseg = 0;