mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <ahmad@a3f.at>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <ahmad@a3f.at>
Subject: [PATCH v2] fixup! common: introduce bthreads, co-operative barebox threads
Date: Mon, 22 Mar 2021 07:26:20 +0100	[thread overview]
Message-ID: <20210322062620.3706270-1-ahmad@a3f.at> (raw)

Specifying __aligned for a struct member only ensures a relative
alignment to the start of the struct. Correct the alignment
by making the stack a separate memalign allocation.

This fixes an issue where printf("%llu" printed bogus values when
run from a bthread, because gcc va_arg on RISC-V requires 16-bit
stack alignment.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 common/bthread.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/common/bthread.c b/common/bthread.c
index 80b486c99af7..c811797130eb 100644
--- a/common/bthread.c
+++ b/common/bthread.c
@@ -33,10 +33,9 @@ static struct bthread {
 #ifdef HAVE_FIBER_SANITIZER
 	void *fake_stack_save;
 #endif
-	u16 awake :1;
-	u16 should_stop :1;
-	u16 has_stopped :1;
-	u8 stack_space[] __aligned(16);
+	u8 awake :1;
+	u8 should_stop :1;
+	u8 has_stopped :1;
 } main_thread = {
 	.list = LIST_HEAD_INIT(main_thread.list),
 	.name = "main",
@@ -72,6 +71,7 @@ bool bthread_is_main(struct bthread *bthread)
 
 void bthread_free(struct bthread *bthread)
 {
+	free(bthread->stack);
 	free(bthread->name);
 	free(bthread);
 }
@@ -84,17 +84,18 @@ const char *bthread_name(struct bthread *bthread)
 struct bthread *bthread_create(int (*threadfn)(void *), void *data,
 			       const char *namefmt, ...)
 {
-	struct bthread *bthread = NULL;
+	struct bthread *bthread;
 	va_list ap;
 	int len;
 
-	bthread = malloc(struct_size(bthread, stack_space, CONFIG_STACK_SIZE));
+	bthread = calloc(1, sizeof(*bthread));
 	if (!bthread)
 		goto err;
 
-	memset(bthread, 0, sizeof(*bthread));
+	bthread->stack = memalign(16, CONFIG_STACK_SIZE);
+	if (!bthread->stack)
+		goto err;
 
-	bthread->stack = bthread->stack_space;
 	bthread->stack_size = CONFIG_STACK_SIZE;
 	bthread->threadfn = threadfn;
 	bthread->data = data;
@@ -112,7 +113,7 @@ struct bthread *bthread_create(int (*threadfn)(void *), void *data,
 
 	return bthread;
 err:
-	free(bthread);
+	bthread_free(bthread);
 	return NULL;
 }
 
-- 
2.30.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


                 reply	other threads:[~2021-03-22  6:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210322062620.3706270-1-ahmad@a3f.at \
    --to=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox