On 2024-09-25 12:06, Jonas Oberhauser wrote:
Am 9/25/2024 um 8:35 AM schrieb Mathieu Desnoyers:
On 2024-09-25 07:57, Jonas Oberhauser wrote:
Hi Mathieu,
I haven't read your code in detail but it seems to me you have an ABA
bug: as I explained elsewhere, you could read the same pointer after
ABA but you don't synchronize with the newer store that gave you
node2, leaving you to speculatively read stale values through *ctx->hp.
(I am assuming here that ctx->hp is essentially an out parameter used
to let the caller know which node got protected).
The following change should fix it:
cmm_barrier();
- node2 = uatomic_load(node_p, CMM_RELAXED); /* Load A */
+ node2 = rcu_dereference(*node_p); /* Load A */
I don't think this fixes it, because IIRC rcu_dereference relies on the
address dependency (which we don't have here) to provide ordering.
I would recommend either:
- ctx->hp = node;
+ ctx->hp = node2;
which fixes the problem under the perhaps too weak assumption that the
compiler doesn't use its knowledge that node==node2 to just undo this
fix, or more strictly,
As stated in Documentation/RCU/rcu_dereference.rst from the Linux
kernel, comparing the result of rcu_dereference against another
non-NULL pointer is discouraged, as you rightly point out.
+ ctx->hp = READ_ONCE(node2);
which I believe makes sure that the value of node2 is used.
I am not entirely sure this extra READ_ONCE() would be sufficient
to prevent the compiler from making assumptions about the content
of node2 and thus use the result of the first load (node) instead.
It would also not suffice to prevent the CPU from speculatively
using the result of the first load to perform dependent loads AFAIU.
Alternatively you could always use an acquire load.
Unless someone comes up with a sound alternate approach,
I am tempted to go with an acquire load as the second load
within hpref_hp_get().
This way, the compiler would not attempt to use the
node value from the first load for dependent loads,
and the and CPU won't try to speculate dependent loads
either.
Thanks,
Mathieu
Best wishes,
jonas
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com