[prev in list] [next in list] [prev in thread] [next in thread] 

List:       linux-audio-dev
Subject:    Re: [LAD] Linux-audio-dev Digest, Vol 109, Issue 4
From:       Kjetil Matheussen <k.s.matheussen () gmail ! com>
Date:       2016-03-04 12:22:23
Message-ID: CAC6niEKfc0FdnXGRoqhEqXLZJShcYa6GpFdd4kiFpxpvngrsFg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Fri, Mar 4, 2016 at 1:00 PM, <
linux-audio-dev-request@lists.linuxaudio.org> wrote:

> Send Linux-audio-dev mailing list submissions to
>         linux-audio-dev@lists.linuxaudio.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://lists.linuxaudio.org/listinfo/linux-audio-dev
> or, via email, send a message with subject or body 'help' to
>         linux-audio-dev-request@lists.linuxaudio.org
>
> You can reach the person managing the list at
>         linux-audio-dev-owner@lists.linuxaudio.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Linux-audio-dev digest..."
>
>
> Today's Topics:
>
>    1. Re: Code reordering (Sebastian Gesemann)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 4 Mar 2016 10:16:02 +0100
> From: Sebastian Gesemann <s.gesemann@gmail.com>
> To: Jonathan Brickman <jeb@ponderworthy.com>
> Cc: Linux Audio Developers <linux-audio-dev@lists.linuxaudio.org>
> Subject: Re: [LAD] Code reordering
> Message-ID:
>         <
> CAGdQazeKzU1ZOyHrnZLoQnXgm2ca4yZ36+-+xT1MupZMfjTpPg@mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Wed, Mar 2, 2016 at 5:55 PM, Jonathan Brickman <jeb@ponderworthy.com>
> wrote:
> > On 3/1/2016 11:40 AM, Paul Davis wrote:
> >
> > > the JACK implementation relies on two things to work:
> > >
> > >    * pointer and integer operations are (weakly) atomic on all
> platforms
> > > that JACK runs on
> > >    * code reordering will either not happen or will be prevented by the
> > > compiler
> >
> > Does #2 mean that -O3 should always be avoided when compiling JACK
> clients?
>
> As I said, I consider JACK's ringbuffer implementation to be broken.
> According to the C11/C++11 memory model there is nothing in the code
> that prevents reordering the update to write_ptr and the update to
> *buf in jack_ringbuffer_write. The use of volatile only makes sure
> that read/write accesses to the volatile variables are not reordered
> or "optimized out" by caching. Specificaly, a volatile write is not a
> release barrier. It does not constrain reordering with respect to
> other memory locations (*buf). This makes the access to the buffer's
> content unordered and invokes undefined behaviour.
>
> Having said that, if you can be sure that the compiler does not
> reorder this (by checking the assembly code, for example) then you
> will be fine on an x86/x64 platform because this platform makes an
> extra guarantee: writes in one thread are never seen out of order from
> another thread's perspective.
>
>
You are right. There was even a discussion about how broken it was
in 2008, and it was fixed, at least in practice.
http://lists.linuxaudio.org/pipermail/linux-audio-user/2008-October/056000.html

Theoretically (and not unlikely also in practice), it seems to be still
broken.
This can also confirmed by compiling with -fsanitize=thread:

WARNING: ThreadSanitizer: data race (pid=24978)
Write of size 8 at 0x7d0c0000efd8 by thread T2:
#0 jack_ringbuffer_write jack/ringbuffer.c:247
(test-int-array-jack+0x000000401a30)
#1 writer_start /home/ksvalast/rbtest/test-int-array.c:85
(test-int-array-jack+0x000000400f4d)
#2 <null> <null> (libtsan.so.0+0x0000000235b9)

Previous read of size 8 at 0x7d0c0000efd8 by thread T1:
#0 jack_ringbuffer_read_space jack/ringbuffer.c:108
(test-int-array-jack+0x000000401346)
#1 reader_start /home/ksvalast/rbtest/test-int-array.c:48
(test-int-array-jack+0x000000400df2)
#2 <null> <null> (libtsan.so.0+0x0000000235b9)

Location is heap block of size 48 at 0x7d0c0000efd0 allocated by main
thread:
#0 malloc <null> (libtsan.so.0+0x000000025993)
#1 jack_ringbuffer_create jack/ringbuffer.c:41
(test-int-array-jack+0x0000004010ab)
#2 main /home/ksvalast/rbtest/test-int-array.c:102
(test-int-array-jack+0x00000040100c)

Thread T2 (tid=24981, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
#1 main /home/ksvalast/rbtest/test-int-array.c:105
(test-int-array-jack+0x000000401056)

Thread T1 (tid=24980, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
#1 main /home/ksvalast/rbtest/test-int-array.c:104
(test-int-array-jack+0x00000040103b)

SUMMARY: ThreadSanitizer: data race jack/ringbuffer.c:247
jack_ringbuffer_write
==================
==================
WARNING: ThreadSanitizer: data race (pid=24978)
Read of size 8 at 0x7d500000fe00 by thread T1:
#0 memcpy <null> (libtsan.so.0+0x00000002666a)
#1 jack_ringbuffer_read jack/ringbuffer.c:166
(test-int-array-jack+0x0000004015fc)
#2 reader_start /home/ksvalast/rbtest/test-int-array.c:50
(test-int-array-jack+0x000000400e22)
#3 <null> <null> (libtsan.so.0+0x0000000235b9)

Previous write of size 8 at 0x7d500000fe00 by thread T2:
#0 memcpy <null> (libtsan.so.0+0x00000002666a)
#1 jack_ringbuffer_write jack/ringbuffer.c:246
(test-int-array-jack+0x0000004019e5)
#2 writer_start /home/ksvalast/rbtest/test-int-array.c:85
(test-int-array-jack+0x000000400f4d)
#3 <null> <null> (libtsan.so.0+0x0000000235b9)

Location is heap block of size 512 at 0x7d500000fe00 allocated by main
thread:
#0 malloc <null> (libtsan.so.0+0x000000025993)
#1 jack_ringbuffer_create jack/ringbuffer.c:52
(test-int-array-jack+0x0000004011c8)
#2 main /home/ksvalast/rbtest/test-int-array.c:102
(test-int-array-jack+0x00000040100c)

Thread T1 (tid=24980, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
#1 main /home/ksvalast/rbtest/test-int-array.c:104
(test-int-array-jack+0x00000040103b)

Thread T2 (tid=24981, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
#1 main /home/ksvalast/rbtest/test-int-array.c:105
(test-int-array-jack+0x000000401056)

SUMMARY: ThreadSanitizer: data race ??:0 __interceptor_memcpy
==================
==================
WARNING: ThreadSanitizer: data race (pid=24978)
Write of size 8 at 0x7d0c0000efe0 by thread T1:
#0 jack_ringbuffer_read jack/ringbuffer.c:167
(test-int-array-jack+0x000000401647)
#1 reader_start /home/ksvalast/rbtest/test-int-array.c:50
(test-int-array-jack+0x000000400e22)
#2 <null> <null> (libtsan.so.0+0x0000000235b9)

Previous read of size 8 at 0x7d0c0000efe0 by thread T2:
#0 jack_ringbuffer_write_space jack/ringbuffer.c:128
(test-int-array-jack+0x00000040141a)
#1 writer_start /home/ksvalast/rbtest/test-int-array.c:83
(test-int-array-jack+0x000000400f1d)
#2 <null> <null> (libtsan.so.0+0x0000000235b9)

Location is heap block of size 48 at 0x7d0c0000efd0 allocated by main
thread:
#0 malloc <null> (libtsan.so.0+0x000000025993)
#1 jack_ringbuffer_create jack/ringbuffer.c:41
(test-int-array-jack+0x0000004010ab)
#2 main /home/ksvalast/rbtest/test-int-array.c:102
(test-int-array-jack+0x00000040100c)

Thread T1 (tid=24980, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
#1 main /home/ksvalast/rbtest/test-int-array.c:104
(test-int-array-jack+0x00000040103b)

Thread T2 (tid=24981, running) created by main thread at:
#0 pthread_create <null> (libtsan.so.0+0x000000027a67)
#1 main /home/ksvalast/rbtest/test-int-array.c:105
(test-int-array-jack+0x000000401056)

SUMMARY: ThreadSanitizer: data race jack/ringbuffer.c:167
jack_ringbuffer_read
==================

[Attachment #5 (text/html)]

<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar \
4, 2016 at 1:00 PM,  <span dir="ltr">&lt;<a \
href="mailto:linux-audio-dev-request@lists.linuxaudio.org" \
target="_blank">linux-audio-dev-request@lists.linuxaudio.org</a>&gt;</span> \
wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Send \
                Linux-audio-dev mailing list submissions to<br>
            <a href="mailto:linux-audio-dev@lists.linuxaudio.org">linux-audio-dev@lists.linuxaudio.org</a><br>
 <br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
            <a href="http://lists.linuxaudio.org/listinfo/linux-audio-dev" \
rel="noreferrer" target="_blank">http://lists.linuxaudio.org/listinfo/linux-audio-dev</a><br>
 or, via email, send a message with subject or body &#39;help&#39; to<br>
            <a href="mailto:linux-audio-dev-request@lists.linuxaudio.org">linux-audio-dev-request@lists.linuxaudio.org</a><br>
 <br>
You can reach the person managing the list at<br>
            <a href="mailto:linux-audio-dev-owner@lists.linuxaudio.org">linux-audio-dev-owner@lists.linuxaudio.org</a><br>
 <br>
When replying, please edit your Subject line so it is more specific<br>
than &quot;Re: Contents of Linux-audio-dev digest...&quot;<br>
<br>
<br>
Today&#39;s Topics:<br>
<br>
     1. Re: Code reordering (Sebastian Gesemann)<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Fri, 4 Mar 2016 10:16:02 +0100<br>
From: Sebastian Gesemann &lt;<a \
                href="mailto:s.gesemann@gmail.com">s.gesemann@gmail.com</a>&gt;<br>
To: Jonathan Brickman &lt;<a \
                href="mailto:jeb@ponderworthy.com">jeb@ponderworthy.com</a>&gt;<br>
Cc: Linux Audio Developers &lt;<a \
href="mailto:linux-audio-dev@lists.linuxaudio.org">linux-audio-dev@lists.linuxaudio.org</a>&gt;<br>
                
Subject: Re: [LAD] Code reordering<br>
Message-ID:<br>
            &lt;<a href="mailto:CAGdQazeKzU1ZOyHrnZLoQnXgm2ca4yZ36%2B-%2BxT1MupZMfjTpP \
g@mail.gmail.com">CAGdQazeKzU1ZOyHrnZLoQnXgm2ca4yZ36+-+xT1MupZMfjTpPg@mail.gmail.com</a>&gt;<br>
                
Content-Type: text/plain; charset=UTF-8<br>
<br>
On Wed, Mar 2, 2016 at 5:55 PM, Jonathan Brickman &lt;<a \
href="mailto:jeb@ponderworthy.com">jeb@ponderworthy.com</a>&gt; wrote:<br> &gt; On \
3/1/2016 11:40 AM, Paul Davis wrote:<br> &gt;<br>
&gt; &gt; the JACK implementation relies on two things to work:<br>
&gt; &gt;<br>
&gt; &gt;      * pointer and integer operations are (weakly) atomic on all \
platforms<br> &gt; &gt; that JACK runs on<br>
&gt; &gt;      * code reordering will either not happen or will be prevented by \
the<br> &gt; &gt; compiler<br>
&gt;<br>
&gt; Does #2 mean that -O3 should always be avoided when compiling JACK clients?<br>
<br>
As I said, I consider JACK&#39;s ringbuffer implementation to be broken.<br>
According to the C11/C++11 memory model there is nothing in the code<br>
that prevents reordering the update to write_ptr and the update to<br>
*buf in jack_ringbuffer_write. The use of volatile only makes sure<br>
that read/write accesses to the volatile variables are not reordered<br>
or &quot;optimized out&quot; by caching. Specificaly, a volatile write is not a<br>
release barrier. It does not constrain reordering with respect to<br>
other memory locations (*buf). This makes the access to the buffer&#39;s<br>
content unordered and invokes undefined behaviour.<br>
<br>
Having said that, if you can be sure that the compiler does not<br>
reorder this (by checking the assembly code, for example) then you<br>
will be fine on an x86/x64 platform because this platform makes an<br>
extra guarantee: writes in one thread are never seen out of order from<br>
another thread&#39;s perspective.<br>
<br></blockquote><div><br></div><div>You are right. There was even a discussion about \
how broken it was</div><div>in 2008, and it was fixed, at least in \
practice.</div><div><a \
href="http://lists.linuxaudio.org/pipermail/linux-audio-user/2008-October/056000.html" \
>http://lists.linuxaudio.org/pipermail/linux-audio-user/2008-October/056000.html</a><br></div><div><br></div><div>Theoretically \
> (and not unlikely also in practice), it seems to be still broken.</div><div>This \
> can also confirmed by compiling with \
> -fsanitize=thread:</div><div><br></div><div><span \
> style="font-family:monospace"><span style="color:rgb(0,0,0)">WARNING: \
> ThreadSanitizer: data race (pid=24978)
</span><br>Write of size 8 at 0x7d0c0000efd8 by thread T2:
<br>#0 jack_ringbuffer_write jack/ringbuffer.c:247 \
(test-int-array-jack+0x000000401a30) <br>#1 writer_start \
/home/ksvalast/rbtest/test-int-array.c:85 (test-int-array-jack+0x000000400f4d) <br>#2 \
&lt;null&gt; &lt;null&gt; (libtsan.so.0+0x0000000235b9) <br>
<br>Previous read of size 8 at 0x7d0c0000efd8 by thread T1:
<br>#0 jack_ringbuffer_read_space jack/ringbuffer.c:108 \
(test-int-array-jack+0x000000401346) <br>#1 reader_start \
/home/ksvalast/rbtest/test-int-array.c:48 (test-int-array-jack+0x000000400df2) <br>#2 \
&lt;null&gt; &lt;null&gt; (libtsan.so.0+0x0000000235b9) <br>
<br>Location is heap block of size 48 at 0x7d0c0000efd0 allocated by main thread:
<br>#0 malloc &lt;null&gt; (libtsan.so.0+0x000000025993)
<br>#1 jack_ringbuffer_create jack/ringbuffer.c:41 \
(test-int-array-jack+0x0000004010ab) <br>#2 main \
/home/ksvalast/rbtest/test-int-array.c:102 (test-int-array-jack+0x00000040100c) <br>
<br>Thread T2 (tid=24981, running) created by main thread at:
<br>#0 pthread_create &lt;null&gt; (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:105 \
(test-int-array-jack+0x000000401056) <br>
<br>Thread T1 (tid=24980, running) created by main thread at:
<br>#0 pthread_create &lt;null&gt; (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:104 \
(test-int-array-jack+0x00000040103b) <br>
<br>SUMMARY: ThreadSanitizer: data race jack/ringbuffer.c:247 jack_ringbuffer_write
<br>==================
<br>==================
<br>WARNING: ThreadSanitizer: data race (pid=24978)
<br>Read of size 8 at 0x7d500000fe00 by thread T1:
<br>#0 memcpy &lt;null&gt; (libtsan.so.0+0x00000002666a)
<br>#1 jack_ringbuffer_read jack/ringbuffer.c:166 \
(test-int-array-jack+0x0000004015fc) <br>#2 reader_start \
/home/ksvalast/rbtest/test-int-array.c:50 (test-int-array-jack+0x000000400e22) <br>#3 \
&lt;null&gt; &lt;null&gt; (libtsan.so.0+0x0000000235b9) <br>
<br>Previous write of size 8 at 0x7d500000fe00 by thread T2:
<br>#0 memcpy &lt;null&gt; (libtsan.so.0+0x00000002666a)
<br>#1 jack_ringbuffer_write jack/ringbuffer.c:246 \
(test-int-array-jack+0x0000004019e5) <br>#2 writer_start \
/home/ksvalast/rbtest/test-int-array.c:85 (test-int-array-jack+0x000000400f4d) <br>#3 \
&lt;null&gt; &lt;null&gt; (libtsan.so.0+0x0000000235b9) <br>
<br>Location is heap block of size 512 at 0x7d500000fe00 allocated by main thread:
<br>#0 malloc &lt;null&gt; (libtsan.so.0+0x000000025993)
<br>#1 jack_ringbuffer_create jack/ringbuffer.c:52 \
(test-int-array-jack+0x0000004011c8) <br>#2 main \
/home/ksvalast/rbtest/test-int-array.c:102 (test-int-array-jack+0x00000040100c) <br>
<br>Thread T1 (tid=24980, running) created by main thread at:
<br>#0 pthread_create &lt;null&gt; (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:104 \
(test-int-array-jack+0x00000040103b) <br>
<br>Thread T2 (tid=24981, running) created by main thread at:
<br>#0 pthread_create &lt;null&gt; (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:105 \
(test-int-array-jack+0x000000401056)<br> <br></span></div><div><span \
style="font-family:monospace"><span style="color:rgb(0,0,0)">SUMMARY: \
ThreadSanitizer: data race ??:0 __interceptor_memcpy </span><br>==================
<br>==================
<br>WARNING: ThreadSanitizer: data race (pid=24978)
<br>Write of size 8 at 0x7d0c0000efe0 by thread T1:
<br>#0 jack_ringbuffer_read jack/ringbuffer.c:167 \
(test-int-array-jack+0x000000401647) <br>#1 reader_start \
/home/ksvalast/rbtest/test-int-array.c:50 (test-int-array-jack+0x000000400e22) <br>#2 \
&lt;null&gt; &lt;null&gt; (libtsan.so.0+0x0000000235b9) <br>
<br>Previous read of size 8 at 0x7d0c0000efe0 by thread T2:
<br>#0 jack_ringbuffer_write_space jack/ringbuffer.c:128 \
(test-int-array-jack+0x00000040141a) <br>#1 writer_start \
/home/ksvalast/rbtest/test-int-array.c:83 (test-int-array-jack+0x000000400f1d) <br>#2 \
&lt;null&gt; &lt;null&gt; (libtsan.so.0+0x0000000235b9) <br>
<br>Location is heap block of size 48 at 0x7d0c0000efd0 allocated by main thread:
<br>#0 malloc &lt;null&gt; (libtsan.so.0+0x000000025993)
<br>#1 jack_ringbuffer_create jack/ringbuffer.c:41 \
(test-int-array-jack+0x0000004010ab) <br>#2 main \
/home/ksvalast/rbtest/test-int-array.c:102 (test-int-array-jack+0x00000040100c) <br>
<br>Thread T1 (tid=24980, running) created by main thread at:
<br>#0 pthread_create &lt;null&gt; (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:104 \
(test-int-array-jack+0x00000040103b) <br>
<br>Thread T2 (tid=24981, running) created by main thread at:
<br>#0 pthread_create &lt;null&gt; (libtsan.so.0+0x000000027a67)
<br>#1 main /home/ksvalast/rbtest/test-int-array.c:105 \
(test-int-array-jack+0x000000401056) <br>
<br>SUMMARY: ThreadSanitizer: data race jack/ringbuffer.c:167 jack_ringbuffer_read
<br>==================<br>
<br></span></div></div></div></div>



_______________________________________________
Linux-audio-dev mailing list
Linux-audio-dev@lists.linuxaudio.org
http://lists.linuxaudio.org/listinfo/linux-audio-dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic