[PATCH v4 4/8] common: bthread: schedule only in command context

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Jun 22 02:08:28 PDT 2021


On 22.06.21 10:26, Ahmad Fatoum wrote:
> Originally, I envisioned bthreads as replacement for pollers and
> workqueues. But even without preemption, having functions you call
> possibly accessing structures you are iterating over can corrupt
> memory. This problem exists with pollers as well, but because of their
> limited scope, it's harder to shoot your foot with them, as you
> don't keep implicit state between poller activations unlike bthreads,
> which maintain their stack across context switches.
> 
> Limit bthread scope instead to be a replacement for workqueues. This
> still allows us to port some classes of state-machine-in-kthread kernel
> code, while avoding the aforementioned pitfalls.
> 
> Cc: Jan Luebbe <j.luebbe at pengutronix.de>
> Cc: Sascha Hauer <a.hauer at pengutronix.de>

Mailer daemon just told me the first character of Sascha is not an a.

Please fixup on apply (or I do with v5)

> Cc: Lucas Stach <l.stach at pengutronix.de>
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> ---
>  commands/bthread.c | 5 +++++
>  common/sched.c     | 6 +++---
>  include/slice.h    | 2 --
>  3 files changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/commands/bthread.c b/commands/bthread.c
> index 964a1044c58b..0c9c221884e8 100644
> --- a/commands/bthread.c
> +++ b/commands/bthread.c
> @@ -10,12 +10,15 @@
>  #include <command.h>
>  #include <getopt.h>
>  #include <clock.h>
> +#include <slice.h>
>  
>  static int bthread_time(void)
>  {
>  	uint64_t start = get_time_ns();
>  	int i = 0;
>  
> +	slice_release(&command_slice);
> +
>  	/*
>  	 * How many background tasks can we have in one second?
>  	 *
> @@ -25,6 +28,8 @@ static int bthread_time(void)
>  	while (!is_timeout(start, SECOND))
>  		i++;
>  
> +	slice_acquire(&command_slice);
> +
>  	return i;
>  }
>  
> diff --git a/common/sched.c b/common/sched.c
> index dcf6522ac058..02582b2c5e4d 100644
> --- a/common/sched.c
> +++ b/common/sched.c
> @@ -15,12 +15,12 @@ void resched(void)
>  
>  	command_slice_acquire();
>  
> -	if (run_workqueues)
> +	if (run_workqueues) {
>  		wq_do_all_works();
> +		bthread_reschedule();
> +	}
>  
>  	poller_call();
>  
>  	command_slice_release();
> -
> -	bthread_reschedule();
>  }
> diff --git a/include/slice.h b/include/slice.h
> index 67d47b9082e6..6c4688e308fa 100644
> --- a/include/slice.h
> +++ b/include/slice.h
> @@ -38,8 +38,6 @@ void command_slice_release(void);
>  
>  #define assert_command_context() do { \
>  	WARN_ONCE(poller_active(), "%s called in poller\n", __func__); \
> -	WARN_ONCE(IS_ENABLED(CONFIG_BTHREAD) && !bthread_is_main(current), \
> -		  "%s called in secondary bthread\n", __func__); \
>  } while (0)
>  
>  #endif
> 

-- 
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 |



More information about the barebox mailing list