ChanServ changed the topic of #rust-embedded to: Welcome to the Rust Embedded IRC channel! Bridged to #rust-embedded:matrix.org and logged at https://libera.irclog.whitequark.org/rust-embedded, code of conduct at https://www.rust-lang.org/conduct.html
rardiol has quit [Quit: https://quassel-irc.org - Chat comfortably. Anywhere.]
starblue has quit [Ping timeout: 276 seconds]
starblue has joined #rust-embedded
IlPalazzo-ojiisa has quit [Remote host closed the connection]
bjc has quit [Ping timeout: 255 seconds]
<cr1901> Can someone explain to me why the Acquire fence isn't sufficient here (and this line is required)? https://github.com/stm32-rs/stm32f1xx-hal/blob/59777ecad18ff7ed3fca82ed5ee4860c94a0031b/src/dma.rs#L390 I don't think I understand the comment
lehmrob has joined #rust-embedded
<re_irc> <@burrbull:matrix.org> I can't answer the question. I only know that this was insperred by https://docs.rust-embedded.org/embedonomicon/dma.html
rardiol has joined #rust-embedded
a2800276_ has quit [Remote host closed the connection]
bjc has joined #rust-embedded
starblue has quit [Ping timeout: 265 seconds]
starblue has joined #rust-embedded
<re_irc> <@mciantyre:matrix.org> cr1901: Just guessing: "compiler_fence(Acquire)" prevents instruction movement up to the preceding read. If a read isn't happening within (or at the end of) "stop()", then the compiler could move the subsequent buffer and payload reads (https://github.com/stm32-rs/stm32f1xx-hal/blob/59777ecad18ff7ed3fca82ed5ee4860c94a0031b/src/dma.rs#L403-L404) before / interleaved with the "stop()" instructions.
emerent_ has joined #rust-embedded
emerent is now known as Guest1666
Guest1666 has quit [Killed (tungsten.libera.chat (Nickname regained by services))]
emerent_ is now known as emerent
Foxyloxy_ has quit [Remote host closed the connection]
Foxyloxy has joined #rust-embedded
dc740 has joined #rust-embedded
IlPalazzo-ojiisa has joined #rust-embedded
<cr1901> AIUI, fence(Acquire) should prevent insns after the fence from being place before the fence. Is that incorrect?
<cr1901> So the ptr::read_volatile(&0) seems redundant
<re_irc> <@jamesmunns:beeper.com> the volatile read of a temporary definitely seems super out of place to me, and would be very sad if that was necessary for things to work correctly.
<re_irc> <@ryan-summers:matrix.org> If that were the case, it would indicate to me that the fence is incorrectly selected
<re_irc> <@jamesmunns:beeper.com> Yeah, the one after might want to be "Release"? Like you typically acquire before to make sure you "see" all changes, and you release after to make sure others "see" all changes.
<re_irc> <@jamesmunns:beeper.com> I still haven't finished reading Mara's book tho 😅
<cr1901> Well I feel a lot better just that other ppl think it's odd
<cr1901> (for want of a better term)
<re_irc> <@ryan-summers:matrix.org> To be frank, most code feels odd to me nowadays, and I immediately become suspicious when someone points me at any of it
<re_irc> <@dirbaio:matrix.org> yeah that code is not right
<cr1901> most code feels odd to me nowadays <-- most code was a mistake :'D
<re_irc> <@dirbaio:matrix.org> you need an AcqRel fence between writing the buffer and starting DMA
<re_irc> and another AcqRel fence between stopping the DMA and reading the buffer
<cr1901> Idk enough about DMA to fix it
<cr1901> About safe DMA in Rust*
<re_irc> <@jamesmunns:beeper.com> : this matches my understanding as well
<re_irc> <@dirbaio:matrix.org> that code is missing the 1st
<re_irc> <@dirbaio:matrix.org> and the 2nd does an Acquire + dummy read instead... not sure what the point is because other memory accesses can be reordered with the dummy read
<cr1901> the 2nd should be a Release fence?
<re_irc> <@jamesmunns:beeper.com> put an acqrel fence before the pac interaction in start, and put an acqrel fence after the interaction in stop
<re_irc> <@dirbaio:matrix.org> also when you stop DMA you have to spin waiting for EN=0, I can't find that there :D
<re_irc> <@jamesmunns:beeper.com> Yeah, I wonder if their volatile and fence (which issues a dmb!) is just long enough for en=0 to take
<re_irc> <@jamesmunns:beeper.com> oh nvm
<re_irc> <@jamesmunns:beeper.com> it's compiler fence not fence, no instruction emitted.
<re_irc> <@dirbaio:matrix.org> dunno, maybe not because it's only if you forcefully stop it, not needed if it stops naturally
<cr1901> >not sure what the point is because other memory accesses can be reordered with the dummy read <-- yea, but the Acquire fence is right after the dummy read. So only memory access before the Acquire can be reordered with--
* cr1901 sees the problem
<cr1901> yea the dummy read stands out as not supposed to be there, but Idk what the fix should look like
<re_irc> <@jamesmunns:beeper.com> > put an acqrel fence before the pac interaction in start, and put an acqrel fence after the interaction in stop
<re_irc> IMO.
<re_irc> <@dirbaio:matrix.org> idk
<re_irc> <@dirbaio:matrix.org> just use embassy-stm23, it has no DMA bugs :)
<re_irc> <@dirbaio:matrix.org> * embassy-stm32,
<re_irc> <@dirbaio:matrix.org> 🙈
<re_irc> <@mciantyre:matrix.org> cr1901: I'm going off the docs here (https://doc.rust-lang.org/core/sync/atomic/fn.compiler_fence.html).
<re_irc> > with Acquire, subsequent reads and writes cannot be moved ahead of preceding reads.
<re_irc> The key is "preceding reads" at the end of that statement. The fence is based on a read instruction, not just any instruction.
<re_irc> If there's no preceding read, the fence doesn't have a post. That's why I'm guessing there's a dummy "ptr::read_volatile".
<re_irc> <@jamesmunns:beeper.com> Honestly in my understanding the only thing you need to ensure is not reordered is the read on https://github.com/stm32-rs/stm32f1xx-hal/blob/59777ecad18ff7ed3fca82ed5ee4860c94a0031b/src/dma.rs#L364-L369 relative to "stop" on line 348.
<cr1901> mciantyre: Yea, it makes sense under that definition
<re_irc> <@jamesmunns:beeper.com> I don't think the fence on 346 actually _does_ anything. volatile ops are not reordered relative to each other, and 344 and 348 are basically both volatile ops.
jr-oss has quit [Ping timeout: 264 seconds]
<cr1901> I thought the definition of Acquire was " subsequent reads and writes cannot be moved ahead preceding memory accessed period"
<re_irc> <@jamesmunns:beeper.com> acquire is only "writes with Release or stronger ordering cannot be reordered across an acquire fence"
<re_irc> <@jamesmunns:beeper.com> and volatile ops have no synchronization interaction with atomic operations, afaik.
<re_irc> <@jamesmunns:beeper.com> the embedonomicon example shows this: https://docs.rust-embedded.org/embedonomicon/dma.html#compiler-misoptimizations
<re_irc> <@jamesmunns:beeper.com> "release before start, acquire after stop"
jr-oss has joined #rust-embedded
<re_irc> <@jamesmunns:beeper.com> https://doc.rust-lang.org/std/sync/atomic/fn.compiler_fence.html has some good docs too
<cr1901> >Intuitively, an acquire access ensures that every access after it stays after it.
<re_irc> <@jamesmunns:beeper.com> Yeah, we want the reads of buffer and payload to stay AFTER stop.
<cr1901> Sure, but my interpretation is that subsequent reads/writes won't be moved ahead of preceding reads AND writes
<cr1901> and docs say that "subsequent reads/writes won't be moved ahead of preceding reads"
<re_irc> <@jamesmunns:beeper.com> compiler fence docs at least say "with Acquire, subsequent reads and writes cannot be moved ahead of preceding reads."
<cr1901> But it says nothing about writes
<cr1901> >and volatile ops have no synchronization interaction with atomic operations, afaik. <-- I would think the fence would prevent things from being reordered before the volatile op
<cr1901> The volatile in the code I linked appears to be there as a "do not optimize away" measure
<re_irc> <@jamesmunns:beeper.com> Gotcha, I understand the point you are making better now
<re_irc> <@jamesmunns:beeper.com> honestly it feels like just using seqcst would be better here: "with SeqCst, no re-ordering of reads and writes across this point is allowed."
<re_irc> <@jamesmunns:beeper.com> (which is different than AcqRel)
<re_irc> <@jamesmunns:beeper.com> but I dunno. I should finish reading Mara's book before pretending to know what I am talking about, despite trying to understand this all for 5 years :p
<cr1901> Okay I think I get it: "However operations that occur before an acquire are free to be reordered to occur after it." <-- Actually, rereading this, this doesn't really contradict the fence docs.
<cr1901> This would be the same thing as saying "preceding writes can be moved after the fence" (as long as reads before the fence don't depend on these writes)
Darius has quit [Ping timeout: 252 seconds]
<cr1901> Anyway, atomics hard, brain bandwidth used for the day
Darius has joined #rust-embedded
<re_irc> <@dngrs:matrix.org> I think it would be good to have concrete examples for each scenario
<re_irc> <@dngrs:matrix.org> but I guess Mara's book covers that
<re_irc> <@jamesmunns:beeper.com> Yeah, her book is also available online, so they could be referenced directly in the embedded book
<re_irc> <@jamesmunns:beeper.com> https://marabos.nl/atomics/
<re_irc> <@jamesmunns:beeper.com> https://marabos.nl/atomics/memory-ordering.html is probably the one :D
<re_irc> <@peter9477:matrix.org> : If that's correct, it's just about the first and only description of any of this stuff that I can comprehend and make use of.
<re_irc> <@jamesmunns:beeper.com> Read Mara's book, it's 100% guaranteed to be more accurate than anything I said.
<re_irc> <@peter9477:matrix.org> Yep, nice looking resource. Hadn't seen a reference to it before.
<re_irc> <@jamesmunns:beeper.com> (sorry, that came off grumpy, atomics are one of those "every time I think I have it I am proven wrong", and I very much trust Mara's knowledge over mine :D)
<re_irc> <@peter9477:matrix.org> Very timely too, as I just threw a random mixed of Orderings into my Pdm changes as placeholders, knowing half of them are probably very wrong...
rardiol has quit [Quit: https://quassel-irc.org - Chat comfortably. Anywhere.]
rardiol has joined #rust-embedded
dc740 has quit [Remote host closed the connection]
dc740 has joined #rust-embedded