Re: [PATCH 16/19] context_tracking: Convert state to atomic_t

From: nicolas saenz julienne
Date: Wed Mar 09 2022 - 12:18:24 EST


On Wed, 2022-03-02 at 16:48 +0100, Frederic Weisbecker wrote:
> Context tracking's state and dynticks counter are going to be merged
> in a single field so that both updates can happen atomically and at the
> same time. Prepare for that with converting the state into an atomic_t.
>
> Signed-off-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
> Cc: Paul E. McKenney <paulmck@xxxxxxxxxx>
> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> Cc: Neeraj Upadhyay <quic_neeraju@xxxxxxxxxxx>
> Cc: Uladzislau Rezki <uladzislau.rezki@xxxxxxxx>
> Cc: Joel Fernandes <joel@xxxxxxxxxxxxxxxxx>
> Cc: Boqun Feng <boqun.feng@xxxxxxxxx>
> Cc: Nicolas Saenz Julienne <nsaenz@xxxxxxxxxx>
> Cc: Marcelo Tosatti <mtosatti@xxxxxxxxxx>
> Cc: Xiongfeng Wang <wangxiongfeng2@xxxxxxxxxx>
> Cc: Yu Liao<liaoyu15@xxxxxxxxxx>
> Cc: Phil Auld <pauld@xxxxxxxxxx>
> Cc: Paul Gortmaker<paul.gortmaker@xxxxxxxxxxxxx>
> Cc: Alex Belits <abelits@xxxxxxxxxxx>
> ---
> static __always_inline bool context_tracking_in_user(void)
> {
> - return __this_cpu_read(context_tracking.state) == CONTEXT_USER;
> + return __ct_state() == CONTEXT_USER;
> }

I was wondering whether it'd make more sense to use ct_state() for extra safety
vs preemption, but it turns out the function isn't being used at all.

I figure it'd be better to remove it altogether and leave ct_state() as the
goto function for this sort of checks.

> #else
> static inline bool context_tracking_in_user(void) { return false; }
> diff --git a/kernel/context_tracking.c b/kernel/context_tracking.c
> index de247e758767..69db43548768 100644
> --- a/kernel/context_tracking.c
> +++ b/kernel/context_tracking.c
> @@ -337,6 +337,7 @@ static __always_inline void context_tracking_recursion_exit(void)
> */
> void noinstr __ct_user_enter(enum ctx_state state)
> {
> + struct context_tracking *ct = this_cpu_ptr(&context_tracking);

I wonder if there is any value to having __ct_state() take 'struct
context_tracking *ct' as an argument to avoid a redundant this_cpu_ptr()...

> lockdep_assert_irqs_disabled();
>
> /* Kernel threads aren't supposed to go to userspace */
> @@ -345,8 +346,8 @@ void noinstr __ct_user_enter(enum ctx_state state)
> if (!context_tracking_recursion_enter())
> return;
>
> - if ( __this_cpu_read(context_tracking.state) != state) {
> - if (__this_cpu_read(context_tracking.active)) {
> + if (__ct_state() != state) {

...here (and in __ct_user_exit()).

Regards,
Nicolas