mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] commands: sleep: add support for running without pollers
@ 2025-07-01  8:52 Ahmad Fatoum
  2025-07-01  8:52 ` [PATCH 2/3] poller: add option to detect delayed poller activation Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2025-07-01  8:52 UTC (permalink / raw)
  To: barebox; +Cc: bst, Ahmad Fatoum

This is useful to test how different parts of barebox behave when
pollers are delayed too much.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/sleep.c | 42 +++++++++++++++++++++++++++++++++++-------
 common/console.c | 13 +++++++++----
 include/stdio.h  |  6 ++++++
 3 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/commands/sleep.c b/commands/sleep.c
index 641dbb230ad5..bb9baf079404 100644
--- a/commands/sleep.c
+++ b/commands/sleep.c
@@ -7,15 +7,28 @@
 #include <command.h>
 #include <complete.h>
 #include <clock.h>
+#include <getopt.h>
 
 static int do_sleep(int argc, char *argv[])
 {
 	uint64_t start, delay;
+	bool blocking = false;
+	int opt;
 
-	if (argc != 2)
+	while((opt = getopt(argc, argv, "b")) > 0) {
+		switch(opt) {
+		case 'b':
+			blocking = true;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	if (argc - optind != 1)
 		return COMMAND_ERROR_USAGE;
 
-	delay = simple_strtoul(argv[1], NULL, 10);
+	delay = simple_strtoul(argv[optind], NULL, 10);
 
 	if (!strcmp(argv[0], "msleep"))
 		delay *= MSECOND;
@@ -23,9 +36,17 @@ static int do_sleep(int argc, char *argv[])
 		delay *= SECOND;
 
 	start = get_time_ns();
-	while (!is_timeout(start, delay)) {
-		if (ctrlc())
-			return 1;
+
+	if (blocking) {
+		while (!is_timeout_non_interruptible(start, delay)) {
+			if (ctrlc_non_interruptible())
+				return 1;
+		}
+	} else {
+		while (!is_timeout(start, delay)) {
+			if (ctrlc())
+				return 1;
+		}
 	}
 
 	return 0;
@@ -33,11 +54,18 @@ static int do_sleep(int argc, char *argv[])
 
 static const char * const sleep_aliases[] = { "msleep", NULL};
 
+BAREBOX_CMD_HELP_START(sleep)
+BAREBOX_CMD_HELP_TEXT("delay execution for n seconds or n mseconds if invoked as msleep")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-b",  "For development: busy loop while inhibiting pollers from running")
+BAREBOX_CMD_HELP_END
+
 BAREBOX_CMD_START(sleep)
 	.cmd		= do_sleep,
 	.aliases	= sleep_aliases,
-	BAREBOX_CMD_DESC("delay execution for n seconds or n mseconds if invoked as msleep")
-	BAREBOX_CMD_OPTS("SECONDS")
+	BAREBOX_CMD_DESC("delay execution for specified time")
+	BAREBOX_CMD_OPTS("[-b] SECONDS")
 	BAREBOX_CMD_GROUP(CMD_GRP_SCRIPT)
 	BAREBOX_CMD_COMPLETE(command_var_complete)
 BAREBOX_CMD_END
diff --git a/common/console.c b/common/console.c
index 424290b6cd10..24f1500a6558 100644
--- a/common/console.c
+++ b/common/console.c
@@ -648,13 +648,10 @@ void ctrlc_handled(void)
 	ctrlc_abort = 0;
 }
 
-/* test if ctrl-c was pressed */
-int ctrlc(void)
+int ctrlc_non_interruptible(void)
 {
 	int ret = 0;
 
-	resched();
-
 	if (!ctrlc_allowed)
 		return 0;
 
@@ -673,6 +670,14 @@ int ctrlc(void)
 
 	return ret;
 }
+EXPORT_SYMBOL(ctrlc_non_interruptible);
+
+/* test if ctrl-c was pressed */
+int ctrlc(void)
+{
+	resched();
+	return ctrlc_non_interruptible();
+}
 EXPORT_SYMBOL(ctrlc);
 
 static int console_ctrlc_init(void)
diff --git a/include/stdio.h b/include/stdio.h
index dcaed71dae05..cec303461ba3 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -59,6 +59,7 @@ void console_flush(void);
 int vprintf(const char *fmt, va_list args);
 
 int ctrlc(void);
+int ctrlc_non_interruptible(void);
 void ctrlc_handled(void);
 #else
 static inline int tstc(void)
@@ -91,6 +92,11 @@ static inline int ctrlc (void)
 	return 0;
 }
 
+int ctrlc_non_interruptible(void)
+{
+	return 0;
+}
+
 static inline void ctrlc_handled(void)
 {
 }
-- 
2.39.5




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] poller: add option to detect delayed poller activation
  2025-07-01  8:52 [PATCH 1/3] commands: sleep: add support for running without pollers Ahmad Fatoum
@ 2025-07-01  8:52 ` Ahmad Fatoum
  2025-07-01  8:52 ` [PATCH 3/3] menu: schedule pollers/workqueues while waiting for menu item selection Ahmad Fatoum
  2025-07-02  7:00 ` [PATCH 1/3] commands: sleep: add support for running without pollers Sascha Hauer
  2 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2025-07-01  8:52 UTC (permalink / raw)
  To: barebox; +Cc: bst, Ahmad Fatoum

We already have checks that see if pollers run too long, but none to see
if poller run too late, because scheduling was disabled for a too long
period of time. Add an option that does this to aid with debugging.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/poller.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/common/poller.c b/common/poller.c
index 83f7205e5d57..18bd9c80f4b3 100644
--- a/common/poller.c
+++ b/common/poller.c
@@ -10,6 +10,7 @@
 #include <malloc.h>
 #include <module.h>
 #include <param.h>
+#include <linux/ktime.h>
 #include <poller.h>
 #include <clock.h>
 #include <linux/ktime.h>
@@ -188,19 +189,46 @@ static void poller_info(void)
 	}
 }
 
+static struct poller_async blocking_poller;
+
+static void blocking_poller_cb(void *priv)
+{
+	ktime_t diff = ktime_get() - blocking_poller.end;
+
+	if (diff < 0) {
+		pr_err("Poller watchdog activated %lluns too early!\n", diff);
+	} else if (diff > SECOND) {
+		pr_warn("Poller watchdog activated %llums too late!\n",
+			ktime_to_ms(diff));
+	}
+
+	poller_call_async(&blocking_poller, SECOND, blocking_poller_cb, NULL);
+}
+
+static void poller_blocking_time(void)
+{
+	if (blocking_poller.active)
+		return;
+
+	poller_async_register(&blocking_poller, "poller-monitor");
+	printf("Poller registered to monitor blocking time\n");
+	poller_call_async(&blocking_poller, SECOND, blocking_poller_cb, NULL);
+}
+
 BAREBOX_CMD_HELP_START(poller)
 BAREBOX_CMD_HELP_TEXT("print info about registered pollers")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT ("-i", "Print information about registered pollers")
 BAREBOX_CMD_HELP_OPT ("-t", "measure how many pollers we run in 1s")
+BAREBOX_CMD_HELP_OPT ("-b", "warn if poller was blocked from running for more than 1 second")
 BAREBOX_CMD_HELP_END
 
 static int do_poller(int argc, char *argv[])
 {
 	int opt;
 
-	while ((opt = getopt(argc, argv, "it")) > 0) {
+	while ((opt = getopt(argc, argv, "itb")) > 0) {
 		switch (opt) {
 		case 'i':
 			poller_info();
@@ -208,6 +236,9 @@ static int do_poller(int argc, char *argv[])
 		case 't':
 			poller_time();
 			return 0;
+		case 'b':
+			poller_blocking_time();
+			return 0;
 		}
 	}
 
-- 
2.39.5




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] menu: schedule pollers/workqueues while waiting for menu item selection
  2025-07-01  8:52 [PATCH 1/3] commands: sleep: add support for running without pollers Ahmad Fatoum
  2025-07-01  8:52 ` [PATCH 2/3] poller: add option to detect delayed poller activation Ahmad Fatoum
@ 2025-07-01  8:52 ` Ahmad Fatoum
  2025-07-01  9:24   ` Bastian Krause
  2025-07-02  7:00 ` [PATCH 1/3] commands: sleep: add support for running without pollers Sascha Hauer
  2 siblings, 1 reply; 5+ messages in thread
From: Ahmad Fatoum @ 2025-07-01  8:52 UTC (permalink / raw)
  To: barebox; +Cc: bst, Ahmad Fatoum

Pollers are already run during the menu, because getchar() will poll the
console driver every 100us and 100us happens to be the time at which
is_timeout starts scheduling. Workqueus can only be run outside command
context however, so running menutree (e.g., because shell was exited)
will not process any fastboot packets in the workqueue.

Fix this by releasing the slice for the duration of readkey. We do a
similar thing in the readline command as well.
Unlike the readline command, menutree can execute arbitrary scripts as
a result of the user input, so we restrict the slice only to the console
and rescheduling parts.

Reported-by: Bastian Krause <bst@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/menu.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/common/menu.c b/common/menu.c
index 128043c225df..c985f2987751 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -9,6 +9,7 @@
 #include <init.h>
 #include <menu.h>
 #include <malloc.h>
+#include <slice.h>
 #include <xfuncs.h>
 #include <errno.h>
 #include <readkey.h>
@@ -290,10 +291,14 @@ int menu_show(struct menu *m)
 		struct menu_entry *old_selected = m->selected;
 		int repaint = 0;
 
-		if (m->auto_select >= 0)
+		if (m->auto_select >= 0) {
 			ch = BB_KEY_RETURN;
-		else
+		} else {
+			/* Ensure workqueues can run during menu, e.g. for fastboot */
+			command_slice_release();
 			ch = read_key();
+			command_slice_acquire();
+		}
 
 		m->auto_select = -1;
 
-- 
2.39.5




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 3/3] menu: schedule pollers/workqueues while waiting for menu item selection
  2025-07-01  8:52 ` [PATCH 3/3] menu: schedule pollers/workqueues while waiting for menu item selection Ahmad Fatoum
@ 2025-07-01  9:24   ` Bastian Krause
  0 siblings, 0 replies; 5+ messages in thread
From: Bastian Krause @ 2025-07-01  9:24 UTC (permalink / raw)
  To: Ahmad Fatoum, barebox

On 7/1/25 10:52 AM, Ahmad Fatoum wrote:
> Pollers are already run during the menu, because getchar() will poll the
> console driver every 100us and 100us happens to be the time at which
> is_timeout starts scheduling. Workqueus can only be run outside command
> context however, so running menutree (e.g., because shell was exited)
> will not process any fastboot packets in the workqueue.
> 
> Fix this by releasing the slice for the duration of readkey. We do a
> similar thing in the readline command as well.
> Unlike the readline command, menutree can execute arbitrary scripts as
> a result of the user input, so we restrict the slice only to the console
> and rescheduling parts.
> 
> Reported-by: Bastian Krause <bst@pengutronix.de>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

Tested-by: Bastian Krause <bst@pengutronix.de>

Thanks!

Regards,
Bastian

> ---
>   common/menu.c | 9 +++++++--
>   1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/common/menu.c b/common/menu.c
> index 128043c225df..c985f2987751 100644
> --- a/common/menu.c
> +++ b/common/menu.c
> @@ -9,6 +9,7 @@
>   #include <init.h>
>   #include <menu.h>
>   #include <malloc.h>
> +#include <slice.h>
>   #include <xfuncs.h>
>   #include <errno.h>
>   #include <readkey.h>
> @@ -290,10 +291,14 @@ int menu_show(struct menu *m)
>   		struct menu_entry *old_selected = m->selected;
>   		int repaint = 0;
>   
> -		if (m->auto_select >= 0)
> +		if (m->auto_select >= 0) {
>   			ch = BB_KEY_RETURN;
> -		else
> +		} else {
> +			/* Ensure workqueues can run during menu, e.g. for fastboot */
> +			command_slice_release();
>   			ch = read_key();
> +			command_slice_acquire();
> +		}
>   
>   		m->auto_select = -1;
>   


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] commands: sleep: add support for running without pollers
  2025-07-01  8:52 [PATCH 1/3] commands: sleep: add support for running without pollers Ahmad Fatoum
  2025-07-01  8:52 ` [PATCH 2/3] poller: add option to detect delayed poller activation Ahmad Fatoum
  2025-07-01  8:52 ` [PATCH 3/3] menu: schedule pollers/workqueues while waiting for menu item selection Ahmad Fatoum
@ 2025-07-02  7:00 ` Sascha Hauer
  2 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2025-07-02  7:00 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum; +Cc: bst


On Tue, 01 Jul 2025 10:52:49 +0200, Ahmad Fatoum wrote:
> This is useful to test how different parts of barebox behave when
> pollers are delayed too much.
> 
> 

Applied, thanks!

[1/3] commands: sleep: add support for running without pollers
      https://git.pengutronix.de/cgit/barebox/commit/?id=1e0f8a89f126 (link may not be stable)
[2/3] poller: add option to detect delayed poller activation
      https://git.pengutronix.de/cgit/barebox/commit/?id=0423ebf2f7a5 (link may not be stable)
[3/3] menu: schedule pollers/workqueues while waiting for menu item selection
      https://git.pengutronix.de/cgit/barebox/commit/?id=a2c5e429594d (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-07-02  7:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-01  8:52 [PATCH 1/3] commands: sleep: add support for running without pollers Ahmad Fatoum
2025-07-01  8:52 ` [PATCH 2/3] poller: add option to detect delayed poller activation Ahmad Fatoum
2025-07-01  8:52 ` [PATCH 3/3] menu: schedule pollers/workqueues while waiting for menu item selection Ahmad Fatoum
2025-07-01  9:24   ` Bastian Krause
2025-07-02  7:00 ` [PATCH 1/3] commands: sleep: add support for running without pollers Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox