From patchwork Fri Feb 14 14:00:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 865102 Delivered-To: patch@linaro.org Received: by 2002:a05:6000:1568:b0:38f:210b:807b with SMTP id 8csp170239wrz; Fri, 14 Feb 2025 06:02:22 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWn8xlOjvQnKUXIKc/fOLTD9G/D0xSs3OTyUl+UeTs1R5vKHE8/k/oTRE3ZX4GfRbep/I8ASA==@linaro.org X-Google-Smtp-Source: AGHT+IF7Ny9qcdjSLukdP5WFhRrKUE0BebKvuQ43lDEJNvuS+VMxa/lNELQ/FmLLmvzev1PylC6x X-Received: by 2002:a5d:5849:0:b0:38a:8906:6b66 with SMTP id ffacd0b85a97d-38dea2e9632mr15754559f8f.38.1739541741295; Fri, 14 Feb 2025 06:02:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1739541741; cv=none; d=google.com; s=arc-20240605; b=bE9bo6F25J1bGYAnA3Dwk+W4xopy574St7ipOD03ZyQSuRLC2vq5M2DoNUWRIioHwq 2POfWbocWq16yZu772Wil7GKqysNH92BSJudbaq8HSiSselhkqHoZOtQP6YHI5oHzKML /zRlylUFAM4+2Lnxl+2WEOC8VRkhxlCbk4EF73hOPfWtVbdm3C9E+hlZXpKd758/MBdM Px5ghrL9TdWa4shGPouc19jhriv9fRx/7wYALKKJQXWakCsEpVMI6logOYvB+FKD0CCf 5YaWk2xIeLzLWt6ji1Z64gu7IPu8MqNsdQgKxT/SZJrPI65ap0Z1wcVGXzWJfNBjMSr5 c62Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=rQWcMVhiDsInzul+8CcxPqL1KlSPSKMbGE8a9Q4ktAU=; fh=TQ9l4ZhCz/Tf0/aj9oP7eJFW47zEyp6bK9wMMzlxmag=; b=WzXpo/izPED4wgNdwa4nSr2GPswU7JZ3Oe9E+B2bB90/AmBu7kqA3j7vN2XDVXxTLB bp7yVmOB5I2R4PSKSrf3YZyho9MFq2ssma/UMVNBi47d+mwNJa4giOvbQnzvyBgpPN9h PzQxolHqeb0ui0gwW3I5Bq+rS2w9mN4IOkze3zgHspLkmexR0E17N7hqSdidIbKGFyU5 okhX0El5DWzZemEsd+sOMPeCk/Qf7r2ney3sscQaggSvkJttK/jPWKjjVrdXCO/6k3DD ZFiQNVtcXbq5JT6hWLNVzgT+CrupuJsNz8PVjYzpqXj2xSJvZuwYfrx1ha9SzxSHdtWK 0nGw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hqWUFUKL; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id 5b1f17b1804b1-439618e5f62si44328015e9.165.2025.02.14.06.02.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Feb 2025 06:02:20 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=hqWUFUKL; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EA8BD80F57; Fri, 14 Feb 2025 15:01:10 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="hqWUFUKL"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6235980F3F; Fri, 14 Feb 2025 15:01:09 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E807F80E88 for ; Fri, 14 Feb 2025 15:01:06 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-4395dddb07dso22069715e9.2 for ; Fri, 14 Feb 2025 06:01:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1739541666; x=1740146466; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rQWcMVhiDsInzul+8CcxPqL1KlSPSKMbGE8a9Q4ktAU=; b=hqWUFUKLqatwjZyIy0Ycz1gSu6QyGok7oySUh2Oj218Y8YyBpIOwgv/4ACj7MKBsYj DPlzNJnVv3l6Q4/V3GE0IcuGplYpJjgOJ3XawNSrlI0ABBR+riOs6iKUJWd2i9RLsnJc EjIOoI7KZkwfOvB35Yf4JdI5Zs6AXSH3gXmyM9JAsPu1ownxlFf66TxBtDec5NZcXCeD 1wKxcIZ32+GxsZHW8eVQ7P5elAs8yKcwPHeYWUSuX6u37H5lqFsYr9NaY7+X8TB4Xkvr UREkdmmdCsamJrJQ2a2EeDSnD+nt5sM8K24DIRoxWnvNB59cSTBgBMoaAuvNE60j0nhX 27ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739541666; x=1740146466; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rQWcMVhiDsInzul+8CcxPqL1KlSPSKMbGE8a9Q4ktAU=; b=Az6IL8ULO6c76N00ZPo1YFYPAL37k3gxmCBd6uWg79AE9l7ujS7/KAvw9M9TUbnMt3 GTz+fTbjCuNad03ddi8WhGWwcxG1mSWDG77NSdU30iyUj/PFbf86jMe6Ube/P+80bih3 cYpBRHftAIl4YGNUrXIRqxunElVpSgZJ2a3wf6D/Soz0JBn/Ngoja0awUcZTxUYLBRUt 0mPYubN1IkgMKFcOSV/gF69cj8qtVGKRVGJBo6SRYyD18l8UQsELBqRyRqoO5j2Mjuuu knsol+kHsA+/uEWJlNmFeB/WgrjHOrzzHRzLCA6RR2lUuS2Pbk5S7rScP4a1G51PRkA5 h4QQ== X-Gm-Message-State: AOJu0Yy1wYIyIR+iItWsyyE1tpAV2E5Go4pE7Oa79d/AptFgS9c31JMY hy899AhVeInN93zELPjjPoYcFHDZsKEfQh2a/YaaMKBgdrgDe9FB0zsKY7gj4t+OpUMOXDKCQky nC83RTA== X-Gm-Gg: ASbGncsepO3E+683rLgYbPRYqDcfZVCC0V9jNuP5mmc6XVW3cJQQgLPiC/qFSk3xTwb yHs4c15jKyPjxihXQ1K54dXWwBzeQPNV2klIH8BjMo2+k1gyBpSWNzoCnyupXKLAchgLEdRoQbE pUUJP+cghUxlmrjqe7LHo2bRjCL9M8eJcJnuZk+fbF79GSArp0y8j40JjimkYb2LfK73JroM/aC kN5qlID6NXQRMK7/6GjKunq7n4PFtqR4BH7Nx8lRtOptN1u/t5QxywsoQHQncAVkHc1v945xpX3 BO/g+MC1su/OpzCzHucetjjE X-Received: by 2002:a05:600c:1e18:b0:439:6589:e1de with SMTP id 5b1f17b1804b1-4396589e306mr54809285e9.26.1739541664219; Fri, 14 Feb 2025 06:01:04 -0800 (PST) Received: from builder.. ([2a01:e0a:3cb:7bb0:65ac:d875:2c3e:607f]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38f258fc7e0sm4666391f8f.48.2025.02.14.06.01.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Feb 2025 06:01:03 -0800 (PST) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Simon Glass , Sughosh Ganu , Raymond Mao , Patrick Rudolph , Michal Simek Subject: [RFC PATCH 06/10] uthread: add cooperative multi-tasking interface Date: Fri, 14 Feb 2025 15:00:21 +0100 Message-ID: <20250214140031.484344-7-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250214140031.484344-1-jerome.forissier@linaro.org> References: <20250214140031.484344-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean 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 --- 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 + +#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 +#include +#include +#include +#include +#include +#include + +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); + } +} + +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; +}