2022-09-01 23:07:01

by Joel Fernandes

[permalink] [raw]
Subject: [PATCH v5 03/18] rcu/tree: Use READ_ONCE() for lockless read of rnp->qsmask

The rnp->qsmask is locklessly accessed from rcutree_dying_cpu(). This
may help avoid load/store tearing due to concurrent access, KCSAN
issues, and preserve sanity of people reading the mask in tracing.

Signed-off-by: Joel Fernandes (Google) <[email protected]>
---
kernel/rcu/tree.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
index 0ca21ac0f064..5ec97e3f7468 100644
--- a/kernel/rcu/tree.c
+++ b/kernel/rcu/tree.c
@@ -2106,7 +2106,7 @@ int rcutree_dying_cpu(unsigned int cpu)
if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
return 0;

- blkd = !!(rnp->qsmask & rdp->grpmask);
+ blkd = !!(READ_ONCE(rnp->qsmask) & rdp->grpmask);
trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
blkd ? TPS("cpuofl-bgp") : TPS("cpuofl"));
return 0;
--
2.37.2.789.g6183377224-goog


2022-09-06 22:43:18

by Joel Fernandes

[permalink] [raw]
Subject: Re: [PATCH v5 03/18] rcu/tree: Use READ_ONCE() for lockless read of rnp->qsmask



On 9/6/2022 6:26 PM, Boqun Feng wrote:
> On Thu, Sep 01, 2022 at 10:17:05PM +0000, Joel Fernandes (Google) wrote:
>> The rnp->qsmask is locklessly accessed from rcutree_dying_cpu(). This
>> may help avoid load/store tearing due to concurrent access, KCSAN
>
> Nit: you can avoid only load tearing with READ_ONCE().
>

I modified it as you suggested for the next revision, thanks.

2022-09-06 22:49:08

by Boqun Feng

[permalink] [raw]
Subject: Re: [PATCH v5 03/18] rcu/tree: Use READ_ONCE() for lockless read of rnp->qsmask

On Thu, Sep 01, 2022 at 10:17:05PM +0000, Joel Fernandes (Google) wrote:
> The rnp->qsmask is locklessly accessed from rcutree_dying_cpu(). This
> may help avoid load/store tearing due to concurrent access, KCSAN

Nit: you can avoid only load tearing with READ_ONCE().

Regards,
Boqun

> issues, and preserve sanity of people reading the mask in tracing.
>
> Signed-off-by: Joel Fernandes (Google) <[email protected]>
> ---
> kernel/rcu/tree.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
> index 0ca21ac0f064..5ec97e3f7468 100644
> --- a/kernel/rcu/tree.c
> +++ b/kernel/rcu/tree.c
> @@ -2106,7 +2106,7 @@ int rcutree_dying_cpu(unsigned int cpu)
> if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
> return 0;
>
> - blkd = !!(rnp->qsmask & rdp->grpmask);
> + blkd = !!(READ_ONCE(rnp->qsmask) & rdp->grpmask);
> trace_rcu_grace_period(rcu_state.name, READ_ONCE(rnp->gp_seq),
> blkd ? TPS("cpuofl-bgp") : TPS("cpuofl"));
> return 0;
> --
> 2.37.2.789.g6183377224-goog
>