aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJens Wiklander2020-12-28 16:07:47 -0600
committerJérôme Forissier2021-01-05 08:21:06 -0600
commit3f286c3b9c107cd0f765967b3d9c1cc3c563477d (patch)
tree51d7a720e37c23a1b1e5e1f45c2fcd70dab82172
parent17967299679c805b86fc045a4e0939ad2ad3a797 (diff)
downloadti-optee-os-3f286c3b9c107cd0f765967b3d9c1cc3c563477d.tar.gz
ti-optee-os-3f286c3b9c107cd0f765967b3d9c1cc3c563477d.tar.xz
ti-optee-os-3f286c3b9c107cd0f765967b3d9c1cc3c563477d.zip
Reintroduce memalign() and friends
memalign() and friends where removed with the commit 8cd8a6296974 ("Remove memalign()"). At the time memalign() was unused and a bit buggy. This new memalign() is believed to work correctly due to extensive testing. Recently memalign() has been needed by certain drivers so it makes sense to add it again. Acked-by: Etienne Carriere <etienne.carriere@linaro.org> Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
-rw-r--r--core/pta/tests/misc.c86
-rw-r--r--lib/libutils/isoc/bget_malloc.c73
-rw-r--r--lib/libutils/isoc/include/malloc.h11
3 files changed, 159 insertions, 11 deletions
diff --git a/core/pta/tests/misc.c b/core/pta/tests/misc.c
index a5065088a..cd9e5ee7b 100644
--- a/core/pta/tests/misc.c
+++ b/core/pta/tests/misc.c
@@ -312,7 +312,7 @@ static int self_test_malloc(void)
312 bool r; 312 bool r;
313 int ret = 0; 313 int ret = 0;
314 314
315 LOG("malloc tests (malloc, free, calloc, realloc):"); 315 LOG("malloc tests:");
316 LOG(" p1=%p p2=%p p3=%p p4=%p", 316 LOG(" p1=%p p2=%p p3=%p p4=%p",
317 (void *)p1, (void *)p2, (void *)p3, (void *)p4); 317 (void *)p1, (void *)p2, (void *)p3, (void *)p4);
318 /* test malloc */ 318 /* test malloc */
@@ -373,6 +373,47 @@ static int self_test_malloc(void)
373 p3 = NULL; 373 p3 = NULL;
374 p4 = NULL; 374 p4 = NULL;
375 375
376 /* test memalign */
377 p3 = memalign(0x1000, 1024);
378 LOG("- p3 = memalign(%d, 1024)", 0x1000);
379 p1 = malloc(1024);
380 LOG("- p1 = malloc(1024)");
381 p4 = memalign(0x100, 512);
382 LOG("- p4 = memalign(%d, 512)", 0x100);
383 LOG(" p1=%p p2=%p p3=%p p4=%p",
384 (void *)p1, (void *)p2, (void *)p3, (void *)p4);
385 r = (p1 && p3 && p4 &&
386 !((vaddr_t)p3 % 0x1000) && !((vaddr_t)p4 % 0x100));
387 if (!r)
388 ret = -1;
389 LOG(" => test %s", r ? "ok" : "FAILED");
390 LOG("");
391 LOG("- free p1, p3, p4");
392 free(p1);
393 free(p3);
394 free(p4);
395 p1 = NULL;
396 p3 = NULL;
397 p4 = NULL;
398
399 /* test memalign with invalid alignments */
400 p3 = memalign(100, 1024);
401 LOG("- p3 = memalign(%d, 1024)", 100);
402 p4 = memalign(0, 1024);
403 LOG("- p4 = memalign(%d, 1024)", 0);
404 LOG(" p1=%p p2=%p p3=%p p4=%p",
405 (void *)p1, (void *)p2, (void *)p3, (void *)p4);
406 r = (!p3 && !p4);
407 if (!r)
408 ret = -1;
409 LOG(" => test %s", r ? "ok" : "FAILED");
410 LOG("");
411 LOG("- free p3, p4");
412 free(p3);
413 free(p4);
414 p3 = NULL;
415 p4 = NULL;
416
376 /* test free(NULL) */ 417 /* test free(NULL) */
377 LOG("- free NULL"); 418 LOG("- free NULL");
378 free(NULL); 419 free(NULL);
@@ -391,7 +432,7 @@ static int self_test_nex_malloc(void)
391 bool r; 432 bool r;
392 int ret = 0; 433 int ret = 0;
393 434
394 LOG("nex_malloc tests (nex_malloc, nex_free, nex_calloc, nex_realloc):"); 435 LOG("nex_malloc tests:");
395 LOG(" p1=%p p2=%p p3=%p p4=%p", 436 LOG(" p1=%p p2=%p p3=%p p4=%p",
396 (void *)p1, (void *)p2, (void *)p3, (void *)p4); 437 (void *)p1, (void *)p2, (void *)p3, (void *)p4);
397 /* test malloc */ 438 /* test malloc */
@@ -452,6 +493,47 @@ static int self_test_nex_malloc(void)
452 p3 = NULL; 493 p3 = NULL;
453 p4 = NULL; 494 p4 = NULL;
454 495
496 /* test memalign */
497 p3 = nex_memalign(0x1000, 1024);
498 LOG("- p3 = nex_memalign(%d, 1024)", 0x1000);
499 p1 = nex_malloc(1024);
500 LOG("- p1 = nex_malloc(1024)");
501 p4 = nex_memalign(0x100, 512);
502 LOG("- p4 = nex_memalign(%d, 512)", 0x100);
503 LOG(" p1=%p p2=%p p3=%p p4=%p",
504 (void *)p1, (void *)p2, (void *)p3, (void *)p4);
505 r = (p1 && p3 && p4 &&
506 !((vaddr_t)p3 % 0x1000) && !((vaddr_t)p4 % 0x100));
507 if (!r)
508 ret = -1;
509 LOG(" => test %s", r ? "ok" : "FAILED");
510 LOG("");
511 LOG("- nex_free p1, p3, p4");
512 nex_free(p1);
513 nex_free(p3);
514 nex_free(p4);
515 p1 = NULL;
516 p3 = NULL;
517 p4 = NULL;
518
519 /* test memalign with invalid alignments */
520 p3 = nex_memalign(100, 1024);
521 LOG("- p3 = nex_memalign(%d, 1024)", 100);
522 p4 = nex_memalign(0, 1024);
523 LOG("- p4 = nex_memalign(%d, 1024)", 0);
524 LOG(" p1=%p p2=%p p3=%p p4=%p",
525 (void *)p1, (void *)p2, (void *)p3, (void *)p4);
526 r = (!p3 && !p4);
527 if (!r)
528 ret = -1;
529 LOG(" => test %s", r ? "ok" : "FAILED");
530 LOG("");
531 LOG("- nex_free p3, p4");
532 nex_free(p3);
533 nex_free(p4);
534 p3 = NULL;
535 p4 = NULL;
536
455 /* test free(NULL) */ 537 /* test free(NULL) */
456 LOG("- nex_free NULL"); 538 LOG("- nex_free NULL");
457 nex_free(NULL); 539 nex_free(NULL);
diff --git a/lib/libutils/isoc/bget_malloc.c b/lib/libutils/isoc/bget_malloc.c
index ced39ea2d..8aeffb7f2 100644
--- a/lib/libutils/isoc/bget_malloc.c
+++ b/lib/libutils/isoc/bget_malloc.c
@@ -346,18 +346,14 @@ static bool bpool_foreach(struct malloc_ctx *ctx,
346 for (bpool_foreach_iterator_init((ctx),(iterator)); \ 346 for (bpool_foreach_iterator_init((ctx),(iterator)); \
347 bpool_foreach((ctx),(iterator), (bp));) 347 bpool_foreach((ctx),(iterator), (bp));)
348 348
349static void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size, 349static void *raw_memalign(size_t hdr_size, size_t ftr_size, size_t alignment,
350 struct malloc_ctx *ctx) 350 size_t pl_size, struct malloc_ctx *ctx)
351{ 351{
352 void *ptr = NULL; 352 void *ptr = NULL;
353 bufsize s; 353 bufsize s;
354 354
355 /* 355 if (!alignment || !IS_POWER_OF_TWO(alignment))
356 * Make sure that malloc has correct alignment of returned buffers. 356 return NULL;
357 * The assumption is that uintptr_t will be as wide as the largest
358 * required alignment of any type.
359 */
360 COMPILE_TIME_ASSERT(SizeQuant >= sizeof(uintptr_t));
361 357
362 raw_malloc_validate_pools(ctx); 358 raw_malloc_validate_pools(ctx);
363 359
@@ -369,13 +365,23 @@ static void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size,
369 if (!s) 365 if (!s)
370 s++; 366 s++;
371 367
372 ptr = bget(0, hdr_size, s, &ctx->poolset); 368 ptr = bget(alignment, hdr_size, s, &ctx->poolset);
373out: 369out:
374 raw_malloc_return_hook(ptr, pl_size, ctx); 370 raw_malloc_return_hook(ptr, pl_size, ctx);
375 371
376 return ptr; 372 return ptr;
377} 373}
378 374
375static void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size,
376 struct malloc_ctx *ctx)
377{
378 /*
379 * Note that we're feeding SizeQ as alignment, this is the smallest
380 * alignment that bget() can use.
381 */
382 return raw_memalign(hdr_size, ftr_size, SizeQ, pl_size, ctx);
383}
384
379static void raw_free(void *ptr, struct malloc_ctx *ctx, bool wipe) 385static void raw_free(void *ptr, struct malloc_ctx *ctx, bool wipe)
380{ 386{
381 raw_malloc_validate_pools(ctx); 387 raw_malloc_validate_pools(ctx);
@@ -600,6 +606,23 @@ static void *gen_mdbg_realloc(struct malloc_ctx *ctx, const char *fname,
600#define realloc_unlocked(ctx, ptr, size) \ 606#define realloc_unlocked(ctx, ptr, size) \
601 gen_mdbg_realloc_unlocked(ctx, __FILE__, __LINE__, (ptr), (size)) 607 gen_mdbg_realloc_unlocked(ctx, __FILE__, __LINE__, (ptr), (size))
602 608
609static void *gen_mdbg_memalign(struct malloc_ctx *ctx, const char *fname,
610 int lineno, size_t alignment, size_t size)
611{
612 struct mdbg_hdr *hdr;
613 uint32_t exceptions = malloc_lock(ctx);
614
615 hdr = raw_memalign(sizeof(struct mdbg_hdr), mdbg_get_ftr_size(size),
616 alignment, size, ctx);
617 if (hdr) {
618 mdbg_update_hdr(hdr, fname, lineno, size);
619 hdr++;
620 }
621 malloc_unlock(ctx, exceptions);
622 return hdr;
623}
624
625
603static void *get_payload_start_size(void *raw_buf, size_t *size) 626static void *get_payload_start_size(void *raw_buf, size_t *size)
604{ 627{
605 struct mdbg_hdr *hdr = raw_buf; 628 struct mdbg_hdr *hdr = raw_buf;
@@ -651,6 +674,12 @@ void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size)
651 return gen_mdbg_realloc(&malloc_ctx, fname, lineno, ptr, size); 674 return gen_mdbg_realloc(&malloc_ctx, fname, lineno, ptr, size);
652} 675}
653 676
677void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
678 size_t size)
679{
680 return gen_mdbg_memalign(&malloc_ctx, fname, lineno, alignment, size);
681}
682
654void mdbg_check(int bufdump) 683void mdbg_check(int bufdump)
655{ 684{
656 gen_mdbg_check(&malloc_ctx, bufdump); 685 gen_mdbg_check(&malloc_ctx, bufdump);
@@ -701,6 +730,16 @@ void *realloc(void *ptr, size_t size)
701 return p; 730 return p;
702} 731}
703 732
733void *memalign(size_t alignment, size_t size)
734{
735 void *p;
736 uint32_t exceptions = malloc_lock(&malloc_ctx);
737
738 p = raw_memalign(0, 0, alignment, size, &malloc_ctx);
739 malloc_unlock(&malloc_ctx, exceptions);
740 return p;
741}
742
704static void *get_payload_start_size(void *ptr, size_t *size) 743static void *get_payload_start_size(void *ptr, size_t *size)
705{ 744{
706 *size = bget_buf_size(ptr); 745 *size = bget_buf_size(ptr);
@@ -873,6 +912,16 @@ void *nex_realloc(void *ptr, size_t size)
873 return p; 912 return p;
874} 913}
875 914
915void *nex_memalign(size_t alignment, size_t size)
916{
917 void *p;
918 uint32_t exceptions = malloc_lock(&nex_malloc_ctx);
919
920 p = raw_memalign(0, 0, alignment, size, &nex_malloc_ctx);
921 malloc_unlock(&nex_malloc_ctx, exceptions);
922 return p;
923}
924
876void nex_free(void *ptr) 925void nex_free(void *ptr)
877{ 926{
878 uint32_t exceptions = malloc_lock(&nex_malloc_ctx); 927 uint32_t exceptions = malloc_lock(&nex_malloc_ctx);
@@ -898,6 +947,12 @@ void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size)
898 return gen_mdbg_realloc(&nex_malloc_ctx, fname, lineno, ptr, size); 947 return gen_mdbg_realloc(&nex_malloc_ctx, fname, lineno, ptr, size);
899} 948}
900 949
950void *nex_mdbg_memalign(const char *fname, int lineno, size_t alignment,
951 size_t size)
952{
953 return gen_mdbg_memalign(&nex_malloc_ctx, fname, lineno, alignment, size);
954}
955
901void nex_mdbg_check(int bufdump) 956void nex_mdbg_check(int bufdump)
902{ 957{
903 gen_mdbg_check(&nex_malloc_ctx, bufdump); 958 gen_mdbg_check(&nex_malloc_ctx, bufdump);
diff --git a/lib/libutils/isoc/include/malloc.h b/lib/libutils/isoc/include/malloc.h
index 992d2282c..36547ff7c 100644
--- a/lib/libutils/isoc/include/malloc.h
+++ b/lib/libutils/isoc/include/malloc.h
@@ -15,6 +15,8 @@ void free(void *ptr);
15void *mdbg_malloc(const char *fname, int lineno, size_t size); 15void *mdbg_malloc(const char *fname, int lineno, size_t size);
16void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 16void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
17void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 17void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
18void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
19 size_t size);
18 20
19void mdbg_check(int bufdump); 21void mdbg_check(int bufdump);
20 22
@@ -23,12 +25,15 @@ void mdbg_check(int bufdump);
23 mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 25 mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
24#define realloc(ptr, size) \ 26#define realloc(ptr, size) \
25 mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 27 mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
28#define memalign(alignment, size) \
29 mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
26 30
27#else 31#else
28 32
29void *malloc(size_t size); 33void *malloc(size_t size);
30void *calloc(size_t nmemb, size_t size); 34void *calloc(size_t nmemb, size_t size);
31void *realloc(void *ptr, size_t size); 35void *realloc(void *ptr, size_t size);
36void *memalign(size_t alignment, size_t size);
32 37
33#define mdbg_check(x) do { } while (0) 38#define mdbg_check(x) do { } while (0)
34 39
@@ -86,6 +91,8 @@ void nex_free(void *ptr);
86void *nex_mdbg_malloc(const char *fname, int lineno, size_t size); 91void *nex_mdbg_malloc(const char *fname, int lineno, size_t size);
87void *nex_mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 92void *nex_mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
88void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 93void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
94void *nex_mdbg_memalign(const char *fname, int lineno, size_t alignment,
95 size_t size);
89 96
90void nex_mdbg_check(int bufdump); 97void nex_mdbg_check(int bufdump);
91 98
@@ -94,12 +101,15 @@ void nex_mdbg_check(int bufdump);
94 nex_mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 101 nex_mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
95#define nex_realloc(ptr, size) \ 102#define nex_realloc(ptr, size) \
96 nex_mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 103 nex_mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
104#define nex_memalign(alignment, size) \
105 nex_mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
97 106
98#else /* ENABLE_MDBG */ 107#else /* ENABLE_MDBG */
99 108
100void *nex_malloc(size_t size); 109void *nex_malloc(size_t size);
101void *nex_calloc(size_t nmemb, size_t size); 110void *nex_calloc(size_t nmemb, size_t size);
102void *nex_realloc(void *ptr, size_t size); 111void *nex_realloc(void *ptr, size_t size);
112void *nex_memalign(size_t alignment, size_t size);
103 113
104#define nex_mdbg_check(x) do { } while (0) 114#define nex_mdbg_check(x) do { } while (0)
105 115
@@ -124,6 +134,7 @@ void nex_malloc_reset_stats(void);
124#define nex_malloc(size) malloc(size) 134#define nex_malloc(size) malloc(size)
125#define nex_calloc(nmemb, size) calloc(nmemb, size) 135#define nex_calloc(nmemb, size) calloc(nmemb, size)
126#define nex_realloc(ptr, size) realloc(ptr, size) 136#define nex_realloc(ptr, size) realloc(ptr, size)
137#define nex_memalign(alignment, size) memalign(alignment, size)
127 138
128#endif /* CFG_VIRTUALIZATION */ 139#endif /* CFG_VIRTUALIZATION */
129 140