All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Sun Shaojie <sunshaojie@kylinos.cn>
Cc: boqun@kernel.org, linux-kernel@vger.kernel.org,
	longman@redhat.com, mingo@redhat.com, will@kernel.org
Subject: Re: [PATCH v2] locking/percpu-rwsem: Annotate intentional data race in readers_active_check()
Date: Tue, 23 Jun 2026 15:25:08 +0200	[thread overview]
Message-ID: <20260623132508.GA1181229@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20260623104132.505117-1-sunshaojie@kylinos.cn>

On Tue, Jun 23, 2026 at 06:41:32PM +0800, Sun Shaojie wrote:
> KCSAN reports a data race between readers_active_check() and a
> concurrently executing reader:
> 
>   BUG: KCSAN: data-race in readers_active_check / percpu_down_write
> 
>   race at unknown origin, with read to 0xffff9f3eb5bf5f30 of 4 bytes
>   by task 1271 on cpu 14:
>    readers_active_check+0x...
>    percpu_down_write+0x152/0x1f0
> 
>   value changed: 0xfffffff9 -> 0xfffffff8
> 
> readers_active_check() calls per_cpu_sum(*sem->read_count), which
> iterates over all CPUs and reads each CPU's per-CPU read_count
> variable.  Concurrently, a reader on a remote CPU is modifying its own
> CPU's read_count via this_cpu_inc() / this_cpu_dec() as it enters and
> exits the critical section.  These are plain reads and writes to the
> same per-CPU storage, hence KCSAN flags a data race.
> 
> This race is benign.  readers_active_check() is called from the
> percpu_down_write() wait loop (rcuwait_wait_event) after sem->block is
> already set.  At this point:
> 
>   - New readers must immediately back out (they see block set, decrement
>     their counter, and wake the writer), so counters can only decrease.
> 
>   - If the sum catches a reader's increment before its decrement,
>     readers_active_check() sees a non-zero sum and returns false.  The
>     writer merely iterates the wait loop again -- a harmless retry.
> 
>   - A false zero (observing sum == 0 while a reader is still active)
>     cannot happen: per_cpu_sum() reads each CPU's counter, and each
>     per-CPU int read is atomic on all architectures, so an active
>     reader's counter is always seen as non-zero.
> 
> Annotate the read with data_race() to suppress the KCSAN warning and
> document the intentional nature of this unlocked access.

Thank you, much better indeed.

      reply	other threads:[~2026-06-23 13:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23  9:34 [PATCH] locking/percpu-rwsem: Annotate intentional data race in readers_active_check() Sun Shaojie
2026-06-23  9:44 ` Peter Zijlstra
2026-06-23 10:41   ` [PATCH v2] " Sun Shaojie
2026-06-23 13:25     ` Peter Zijlstra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260623132508.GA1181229@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=boqun@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=sunshaojie@kylinos.cn \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.