From: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
To: Jerome Forissier <jerome.forissier@linaro.org>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>,
Sughosh Ganu <sughosh.ganu@linaro.org>,
Raymond Mao <raymond.mao@linaro.org>,
Patrick Rudolph <patrick.rudolph@9elements.com>,
Michal Simek <michal.simek@amd.com>,
u-boot@lists.denx.de
Subject: Re: [RFC PATCH 06/10] uthread: add cooperative multi-tasking interface
Date: Fri, 14 Feb 2025 15:47:03 +0100 [thread overview]
Message-ID: <57368c2f-3003-4264-a3e6-0c8705b401da@canonical.com> (raw)
In-Reply-To: <20250214140031.484344-7-jerome.forissier@linaro.org>
On 14.02.25 15:00, Jerome Forissier wrote:
> Add an new internal API called uthread (Kconfig symbol: UTHREAD) which
> provides cooperative multi-tasking. The goal is to be able to improve
> the performance of some parts of U-Boot by overlapping lengthy
> operations. Each uthread has its own stack allocated on the heap. The
> default stack size is defined by the UTHREAD_STACK_SIZE symbol and is
> used when uthread_create() is passed zero for the stack_sz argument.
>
> The implementation is based on context-switching via initjmp()/setjmp()/
> longjmp() and is inspired from barebox threads [1].
>
> [1] https://github.com/barebox/barebox/blob/master/common/bthread.c
>
> Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
> ---
> include/uthread.h | 31 ++++++++++++++
> lib/Kconfig | 19 ++++++++
> lib/Makefile | 2 +
> lib/uthread.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 159 insertions(+)
> create mode 100644 include/uthread.h
> create mode 100644 lib/uthread.c
>
> diff --git a/include/uthread.h b/include/uthread.h
> new file mode 100644
> index 00000000000..ea1389e82c6
> --- /dev/null
> +++ b/include/uthread.h
> @@ -0,0 +1,31 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + * Copyright 2025 Linaro Limited
> + */
> +
> +#include <linux/types.h>
> +
> +#ifndef _UTHREAD_H_
> +#define _UTHREAD_H_
> +
> +#ifdef CONFIG_UTHREAD
> +
> +int uthread_create(void (*fn)(void *), void *arg, size_t stack_sz);
> +void uthread_free_all(void);
> +/* Returns false when all threads are done */
> +bool uthread_schedule(void);
> +
> +#else
> +
> +static inline int uthread_create(void (*fn)(void *), void *arg, size_t stack_sz)
> +{
> + fn(arg);
> + return 0;
> +}
> +
> +static inline void uthread_free_all(void) { }
> +
> +static inline bool uthread_schedule(void) { return false; }
> +
> +#endif /* CONFIG_UTHREAD */
> +#endif /* _UTHREAD_H_ */
> diff --git a/lib/Kconfig b/lib/Kconfig
> index 1a683dea670..c3416bbd0be 100644
> --- a/lib/Kconfig
> +++ b/lib/Kconfig
> @@ -1255,6 +1255,25 @@ config PHANDLE_CHECK_SEQ
> enable this config option to distinguish them using
> phandles in fdtdec_get_alias_seq() function.
>
> +config UTHREAD
> + bool "Enable thread support"
> + depends on HAVE_INITJMP
> + help
> + Implement a simple form of cooperative multi-tasking based on
> + context-switching via initjmp(), setjmp() and longjmp(). The
> + uthread_ interface enables the main thread of execution to create
> + one or more secondary threads and schedule them until they all have
> + returned. At any point a thread may suspend its execution and
> + schedule another thread, which allows for the efficient multiplexing
> + of leghthy operations.
> +
> +config UTHREAD_STACK_SIZE
> + int "Default uthread stack size"
> + depends on UTHREAD
> + default 32168
> + help
> + The default stask size for uthreads. Each uthread has its own stack.
> +
> endmenu
>
> source "lib/fwu_updates/Kconfig"
> diff --git a/lib/Makefile b/lib/Makefile
> index a7bc2f3134a..3610694de7a 100644
> --- a/lib/Makefile
> +++ b/lib/Makefile
> @@ -164,6 +164,8 @@ obj-$(CONFIG_LIB_ELF) += elf.o
>
> obj-$(CONFIG_$(PHASE_)SEMIHOSTING) += semihosting.o
>
> +obj-$(CONFIG_UTHREAD) += uthread.o
> +
> #
> # Build a fast OID lookup registry from include/linux/oid_registry.h
> #
> diff --git a/lib/uthread.c b/lib/uthread.c
> new file mode 100644
> index 00000000000..bb132001fb6
> --- /dev/null
> +++ b/lib/uthread.c
> @@ -0,0 +1,107 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +//
> +// Copyright (C) 2021 Ahmad Fatoum, Pengutronix
> +// Copyright (C) 2025 Linaro Limited
> +//
> +// An implementation of cooperative multi-tasking inspired from barebox threads
> +// https://github.com/barebox/barebox/blob/master/common/bthread.c
> +
> +#include <compiler.h>
> +#include <asm/setjmp.h>
> +#include <linux/kernel.h>
> +#include <linux/list.h>
> +#include <malloc.h>
> +#include <stdint.h>
> +#include <uthread.h>
> +
> +static struct uthread {
> + void (*fn)(void *);
> + void *arg;
> + jmp_buf ctx;
> + void *stack;
> + bool done;
> + struct list_head list;
> +} main_thread = {
> + .list = LIST_HEAD_INIT(main_thread.list),
> +};
> +
> +static struct uthread *current = &main_thread;
> +
> +static void __noreturn uthread_trampoline(void)
> +{
> + current->fn(current->arg);
> + current->done = true;
> + current = &main_thread;
> + longjmp(current->ctx, 1);
> + /* Not reached */
> + while (true)
> + ;
> +}
> +
> +static void uthread_free(struct uthread *uthread)
> +{
> + if (!uthread)
> + return;
> + free(uthread->stack);
> + free(uthread);
> +}
> +
> +int uthread_create(void (*fn)(void *), void *arg, size_t stack_sz)
> +{
> + struct uthread *uthread;
> +
> + if (!stack_sz)
> + stack_sz = CONFIG_UTHREAD_STACK_SIZE;
> +
> + uthread = calloc(1, sizeof(*uthread));
> + if (!uthread)
> + return -1;
> +
> + uthread->stack = memalign(16, stack_sz);
> + if (!uthread->stack)
> + goto err;
> +
> + uthread->fn = fn;
> + uthread->arg = arg;
> +
> + list_add_tail(&uthread->list, ¤t->list);
> +
> + initjmp(uthread->ctx, uthread_trampoline, uthread->stack + stack_sz);
> +
> + return 0;
> +err:
> + uthread_free(uthread);
> + return -1;
> +}
> +
> +void uthread_free_all(void)
> +{
> + struct uthread *next;
> + struct uthread *tmp;
> +
> + list_for_each_entry_safe(next, tmp, ¤t->list, list) {
> + list_del(&next->list);
> + uthread_free(next);
> + }
> +}
> +
For every function, please, create a Sphinx style documentation.
For the whole uthread library, please, create a document in
/doc/develop/ explaining how to use the API functions.
Best regards
Heinrich
> +static void uthread_resume(struct uthread *uthread)
> +{
> + if (!setjmp(current->ctx)) {
> + current = uthread;
> + longjmp(uthread->ctx, 1);
> + }
> +}
> +
> +bool uthread_schedule(void)
> +{
> + struct uthread *next;
> +
> + list_for_each_entry(next, ¤t->list, list) {
> + if (!next->done) {
> + uthread_resume(next);
> + return true;
> + }
> + }
> + return false;
> +}
next prev parent reply other threads:[~2025-02-14 14:47 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-14 14:00 [RFC PATCH 00/10] Uthreads Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 01/10] arch: introduce symbol HAVE_INITJMP Jerome Forissier
2025-02-14 14:41 ` Heinrich Schuchardt
2025-02-14 15:03 ` Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 02/10] arm: add initjmp() Jerome Forissier
2025-02-14 14:35 ` Heinrich Schuchardt
2025-02-17 13:10 ` Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 03/10] riscv: " Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 04/10] sandbox: " Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 05/10] test: lib: add initjmp() test Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 06/10] uthread: add cooperative multi-tasking interface Jerome Forissier
2025-02-14 14:47 ` Heinrich Schuchardt [this message]
2025-02-14 18:25 ` Yao Zi
2025-02-17 9:48 ` Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 07/10] lib: time: hook uthread_schedule() into udelay() Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 08/10] dm: usb: move bus initialization into new static function usb_init_bus() Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 09/10] dm: usb: initialize and scan multiple buses simultaneously with uthread Jerome Forissier
2025-02-14 14:00 ` [RFC PATCH 10/10] test: lib: add uthread test Jerome Forissier
2025-02-14 18:41 ` Yao Zi
2025-02-17 9:49 ` Jerome Forissier
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=57368c2f-3003-4264-a3e6-0c8705b401da@canonical.com \
--to=heinrich.schuchardt@canonical.com \
--cc=ilias.apalodimas@linaro.org \
--cc=jerome.forissier@linaro.org \
--cc=michal.simek@amd.com \
--cc=patrick.rudolph@9elements.com \
--cc=raymond.mao@linaro.org \
--cc=sjg@chromium.org \
--cc=sughosh.ganu@linaro.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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