From: John Ogness <john.ogness@linutronix.de>
To: "John B. Wyatt IV" <jwyatt@redhat.com>,
Clark Williams <williams@redhat.com>,
John Kacur <jkacur@redhat.com>
Cc: "John B. Wyatt IV" <jwyatt@redhat.com>,
linux-rt-users@vger.kernel.org,
kernel-rts-sst <kernel-rts-sst@redhat.com>,
"John B. Wyatt IV" <sageofredondo@gmail.com>
Subject: Re: [PATCH] pi_stress: Add memory barrier to resolve crash
Date: Thu, 24 Oct 2024 23:26:17 +0206 [thread overview]
Message-ID: <84h691xkoe.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <20241024162620.349934-1-jwyatt@redhat.com>
Hi John,
On 2024-10-24, "John B. Wyatt IV" <jwyatt@redhat.com> wrote:
> + /**
> + * The pthread_barrier_wait should guarantee that only one
> + * thread at a time interacts with the variables below that
> + * if block.
The pthread_barrier_wait() does exactly the opposite. It guarantees that
multiple threads continue at the same time. Taking a look at some users
of @finish_barrier, we see this means that low_priority() will read
p->total while high_priority() is performing the non-atomic operation
p->total++.
> + *
> + * GCC -O2 rearranges the two increments above the wait
> + * function calls causing a race issue if you run this
> + * near full cores with one core (2 threads) free for
> + * housekeeping. This causes a crash at around 2 hour of
> + * running. You can prove this by commenting out the barrier
> + * and compiling with `-O0`. The crash does not show with
> + * -O0.
Turning off optimization is not a proof. Show us the assembly code.
> + *
> + * Add a memory barrier to force GCC to increment the variables
> + * below the pthread calls. This funcion depends on C11.
> + **/
What you are talking about are compiler barriers, which are for forcing
the compiler to obey instruction ordering. Memory barriers are for
guarenteed memory ordering for CPUs at _runtime_.
> + atomic_thread_fence(memory_order_seq_cst);
A single memory barrier makes no sense. Memory barriers must be paired
because you are ordering memory access between multiple CPUs.
> +
> /* update the group stats */
> p->total++;
If you want multiple tasks to be able to modify and read these
variables, then either use atomic operations or use locking. That is
what such mechanisms are for.
John Ogness
next prev parent reply other threads:[~2024-10-24 21:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-24 16:26 [PATCH] pi_stress: Add memory barrier to resolve crash John B. Wyatt IV
2024-10-24 20:12 ` Crystal Wood
2024-10-24 21:20 ` John Ogness [this message]
2024-10-29 22:15 ` Wander Lairson Costa
2024-10-30 22:03 ` John Kacur
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=84h691xkoe.fsf@jogness.linutronix.de \
--to=john.ogness@linutronix.de \
--cc=jkacur@redhat.com \
--cc=jwyatt@redhat.com \
--cc=kernel-rts-sst@redhat.com \
--cc=linux-rt-users@vger.kernel.org \
--cc=sageofredondo@gmail.com \
--cc=williams@redhat.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox