Re: [PATCH V2 09/11] rcu: Implement PCPU_RCU_PREEMPT_COUNT framework

From: Lai Jiangshan
Date: Tue Apr 23 2024 - 23:03:10 EST


On Wed, Apr 24, 2024 at 2:19 AM Joel Fernandes <joel@xxxxxxxxxxxxxxxxx> wrote:
>
> On Sun, Apr 7, 2024 at 5:04 AM Lai Jiangshan <jiangshanlai@xxxxxxxxx> wrote:
> >
> > From: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx>
> >
> > When the arch code provides HAVE_PCPU_RCU_PREEMPT_COUNT and the
> > corresponding functions, rcu core uses the functions to implement
> > rcu_preempt_depth(), special bits, switching and so on.
> >
> > Cc: "Paul E. McKenney" <paulmck@xxxxxxxxxx>
> > Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> > Cc: Frederic Weisbecker <frederic@xxxxxxxxxx>
> > Signed-off-by: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx>
> > ---
> > include/linux/rcupdate.h | 33 +++++++++++++++++++++++++++++++++
> > kernel/rcu/Kconfig | 8 ++++++++
> > kernel/rcu/rcu.h | 4 ++++
> > kernel/rcu/tree_plugin.h | 8 ++++++++
> > 4 files changed, 53 insertions(+)
> >
> > diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> > index 328667ae8086..e3e5ce44c7dc 100644
> > --- a/include/linux/rcupdate.h
> > +++ b/include/linux/rcupdate.h
> > @@ -70,6 +70,8 @@ static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned
> >
> > void rcu_read_unlock_special(void);
> >
> > +#ifndef CONFIG_PCPU_RCU_PREEMPT_COUNT
> > +
> > void __rcu_read_lock(void);
> > void __rcu_read_unlock(void);
> >
> > @@ -81,6 +83,37 @@ void __rcu_read_unlock(void);
> > */
> > #define rcu_preempt_depth() READ_ONCE(current->rcu_read_lock_nesting)
> > #define rcu_preempt_depth_set(val) WRITE_ONCE(current->rcu_read_lock_nesting, (val))
> > +#define pcpu_rcu_preempt_special_set() do { } while (0)
> > +#define pcpu_rcu_preempt_special_clear() do { } while (0)
> > +
> > +#else /* #ifndef CONFIG_PCPU_RCU_PREEMPT_COUNT */
> > +
> > +#include <asm/rcu_preempt.h>
> > +
> > +static __always_inline void __rcu_read_lock(void)
> > +{
> > + pcpu_rcu_preempt_count_add(1);
> > + barrier();
> > +}
> > +
> > +static __always_inline void __rcu_read_unlock(void)
> > +{
> > + barrier();
> > + if (unlikely(pcpu_rcu_preempt_count_dec_and_test()))
> > + pcpu_rcu_read_unlock_special();
> > +}
>
> Previous code had comments about the barrier(); , can you add back
> those comments?
>
> Also there was a compiler barrier in the body of the if() as well?
>

The two "if"s in the referenced __rcu_read_unlock() are condensed into
a single "if" ("if (unlikely(pcpu_rcu_preempt_count_dec_and_test()))"),
so there is no extra barrier() needed in the body of the "if" which
is analogue to the body of the second "if" of the referenced
__rcu_read_unlock().

The special bit and the rcu_depth_count are condensed, so the code
mostly follows the way how preempt_enable() works.

Thanks
Lai

> For reference:
>
> void __rcu_read_unlock(void)
> {
> struct task_struct *t = current;
>
> barrier(); // critical section before exit code.
> if (rcu_preempt_read_exit() == 0) {
> barrier(); // critical-section exit before .s check.
> if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
> rcu_read_unlock_special(t);
> }