<html><head><meta charset="UTF-8"><meta http-equiv="content-type" content="text/html; charset=us-ascii"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; display: inline !important; float: none;">On Wed May 10, 2023 at 1:31 PM AEST, Rohan McLure wrote:</span><br style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><blockquote type="cite" style="font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">Prior to this patch, data races are detectable by KCSAN of the following<br>forms:<br><br>[1] Asynchronous calls to mmiowb_set_pending() from an interrupt context<br>   or otherwise outside of a critical section<br>[2] Interrupted critical sections, where the interrupt will itself<br>   acquire a lock<br><br>In case [1], calling context does not need an mmiowb() call to be<br>issued, otherwise it would do so itself. Such calls to<br>mmiowb_set_pending() are either idempotent or no-ops.<br><br>In case [2], irrespective of when the interrupt occurs, the interrupt<br>will acquire and release its locks prior to its return, nesting_count<br>will continue balanced. In the worst case, the interrupted critical<br>section during a mmiowb_spin_unlock() call observes an mmiowb to be<br>pending and afterward is interrupted, leading to an extraneous call to<br>mmiowb(). This data race is clearly innocuous.<br><br>Mark all potentially asynchronous memory accesses with READ_ONCE or<br>WRITE_ONCE, including increments and decrements to nesting_count. This<br>has the effect of removing KCSAN warnings at consumer's callsites.<br><br>Signed-off-by: Rohan McLure <rmclure@linux.ibm.com><br>Reported-by: Michael Ellerman <mpe@ellerman.id.au><br>Reported-by: Gautam Menghani <gautam@linux.ibm.com><br>Tested-by: Gautam Menghani <gautam@linux.ibm.com><br>Acked-by: Arnd Bergmann <arnd@arndb.de><br>---<br>v2: Remove extraneous READ_ONCE in mmiowb_set_pending for nesting_count<br>---<br>include/asm-generic/mmiowb.h | 14 +++++++++-----<br>1 file changed, 9 insertions(+), 5 deletions(-)<br><br>diff --git a/include/asm-generic/mmiowb.h b/include/asm-generic/mmiowb.h<br>index 5698fca3bf56..6dea28c8835b 100644<br>--- a/include/asm-generic/mmiowb.h<br>+++ b/include/asm-generic/mmiowb.h<br>@@ -37,25 +37,29 @@ static inline void mmiowb_set_pending(void)<br><span class="Apple-tab-span" style="white-space: pre;">      </span>struct mmiowb_state *ms = __mmiowb_state();<br><br><span class="Apple-tab-span" style="white-space: pre;">   </span>if (likely(ms->nesting_count))<br>-<span class="Apple-tab-span" style="white-space: pre;">      </span><span class="Apple-tab-span" style="white-space: pre;">  </span>ms->mmiowb_pending = ms->nesting_count;<br>+<span class="Apple-tab-span" style="white-space: pre;">  </span><span class="Apple-tab-span" style="white-space: pre;">  </span>WRITE_ONCE(ms->mmiowb_pending, ms->nesting_count);<br>}<br><br>static inline void mmiowb_spin_lock(void)<br>{<br><span class="Apple-tab-span" style="white-space: pre;">     </span>struct mmiowb_state *ms = __mmiowb_state();<br>-<span class="Apple-tab-span" style="white-space: pre;">    </span>ms->nesting_count++;<br>+<br>+<span class="Apple-tab-span" style="white-space: pre;">     </span>/* Increment need not be atomic. Nestedness is balanced over interrupts. */<br>+<span class="Apple-tab-span" style="white-space: pre;">    </span>WRITE_ONCE(ms->nesting_count, READ_ONCE(ms->nesting_count) + 1);<br>}<br><br>static inline void mmiowb_spin_unlock(void)<br>{<br><span class="Apple-tab-span" style="white-space: pre;">     </span>struct mmiowb_state *ms = __mmiowb_state();<br>+<span class="Apple-tab-span" style="white-space: pre;">    </span>u16 pending = READ_ONCE(ms->mmiowb_pending);<br><br>-<span class="Apple-tab-span" style="white-space: pre;">      </span>if (unlikely(ms->mmiowb_pending)) {<br>-<span class="Apple-tab-span" style="white-space: pre;"> </span><span class="Apple-tab-span" style="white-space: pre;">  </span>ms->mmiowb_pending = 0;<br>+<span class="Apple-tab-span" style="white-space: pre;">     </span>WRITE_ONCE(ms->mmiowb_pending, 0);<br>+<span class="Apple-tab-span" style="white-space: pre;">  </span>if (unlikely(pending)) {<br><span class="Apple-tab-span" style="white-space: pre;">        </span><span class="Apple-tab-span" style="white-space: pre;">  </span>mmiowb();<br><span class="Apple-tab-span" style="white-space: pre;">       </span>}<br><br>-<span class="Apple-tab-span" style="white-space: pre;">    </span>ms->nesting_count--;<br>+<span class="Apple-tab-span" style="white-space: pre;">        </span>/* Decrement need not be atomic. Nestedness is balanced over interrupts. */<br>+<span class="Apple-tab-span" style="white-space: pre;">    </span>WRITE_ONCE(ms->nesting_count, READ_ONCE(ms->nesting_count) - 1);<br></blockquote><br style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; display: inline !important; float: none;">Still think the nesting_counts don't need WRITE_ONCE/READ_ONCE.</span><br style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; display: inline !important; float: none;">data_race() maybe but I don't know if it's even classed as a data</span><br style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; display: inline !important; float: none;">race. How does KCSAN handle/annotate preempt_count, for example?</span><br style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><br style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; display: inline !important; float: none;">Thanks,</span><br style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><span style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: IBMPlexMono; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; display: inline !important; float: none;">Nick</span></body></html>