Re: [RFC PATCH v2 2/4] Documentation: RCU: Refer to ptr_eq()
From: Paul E. McKenney
Date: Sun Oct 06 2024 - 16:39:39 EST
On Sun, Oct 06, 2024 at 07:52:49PM +0000, David Laight wrote:
> From: Mathieu Desnoyers
> > Sent: 04 October 2024 19:28
> >
> > Refer to ptr_eq() in the rcu_dereference() documentation.
> >
> > ptr_eq() is a mechanism that preserves address dependencies when
> > comparing pointers, and should be favored when comparing a pointer
> > obtained from rcu_dereference() against another pointer.
>
> Why does this ever really matter for rcu?
>
> The check just ensure that any speculative load uses
> a specific one of the pointers when they are different.
> This can only matter if you care about the side effects
> of the speculative load.
It can matter when there are static variables used during OOM. The code
must check the pointer against the addresses of the static variables
to work out how to free them, which can enable the compiler to do
specialization optimizations that destroy the address dependencies.
Which is OK if those static variables are compile-time initialized or
some such. But not if they get new values each time they go into the
list, as this can result in the reader seeing pre-initialization garbage.
An admittedly rare but very real use case.
Thanx, Paul
> But rcu is all about (things like) lockless list following.
> So you need to wait until it is impossible for another
> execution context to have a reference to some memory
> before actually completely invalidating it (ie kfree()).
>
> And that 50 line comment is pointless.
>
> David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>