LWN: Comments on "An instruction-level BPF memory model" https://lwn.net/Articles/976071/ This is a special feed containing comments posted to the individual LWN article titled "An instruction-level BPF memory model". en-us Tue, 16 Sep 2025 06:00:40 +0000 Tue, 16 Sep 2025 06:00:40 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net An instruction-level BPF memory model https://lwn.net/Articles/977979/ https://lwn.net/Articles/977979/ PaulMcKenney Yes, I should have said "HOB", apologies for my confusion. <p>On the "exists" clause, no memory model that enforces single-object sequential consistency can have 0:r3=1 and 0:r5=0. I was therefore looking for some consequence of HOB beyond single-object sequential consistency, hence my "exists" clause matching the HOB defintion. <p>A simpler approach, which I should have thought of to begin with, is to disable HOB in the Aarch64 memory model, for example, like this: <blockquote><pre> let haz-ob = (* Exp-haz-ob | *) TLBI-ob | IC-ob </pre></blockquote> <p>(If you try this in a clone of the herd7tools repo, don't forget to &ldquo;make install&rdquo; so that your herd7 command knows about the change. And don't forget to do so again after undoing this act of vandalism!) <p>With this change, running herd7 on <code>catalogue/aarch64/tests/CoRR.litmus</code> says <code>Sometimes</code>, which supports your analysis of HOB, and invalidates mine. So, like DOB, HOB is not optional from a hardware viewpoint. <p>I agree that LKMM should continue to ignore DOB, at least until someone comes up with a good use case. But given that leaving out HOB causes the ARMv8 memory model to say <code>Sometimes</code> for <code>CoRR.litmus</code>, HOB is not optional. <p>Thank you for keeping me honest! Tue, 11 Jun 2024 17:08:02 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977602/ https://lwn.net/Articles/977602/ Alan.Stern <div class="FormattedComment"> Responding to your later points first...<br> <p> <span class="QuotedText">&gt; I am not so sure that I agree with your suggested change to the "exists" clause, given that DOB requires that R3 be coherence-before E2. What am I missing here?</span><br> <p> Here we are discussing a litmus test (on slide 69) for HOB, not DOB, so I'll assume your "DOB" above is a typo.<br> <p> Yes, HOB requires that R3 is coherence-before E2. Hence the "exists" clause should test for the value of R3 (i.e., 0:r5) being the original value of y, namely 0. The conclusion of HOB is that R1 must also be ordered before E2, in other words, that the value of R1 (i.e., 0:r3) must also be the original value of y, namely 0. (Or to put it another way, HOB says that while R1 might be executed later than R3, out of program order, it can't be executed so _much_ later that it observes the value written by E2.) Therefore to test for a violation of HOB, the "exists" clause has to ask whether 0:r3 is different from 0 and 0:r5 is equal to 0. That's what my suggested change does. (Notice that none of the executions listed on slide 70 violate HOB in this way.)<br> <p> <span class="QuotedText">&gt; I do not see how current hardware with current restrictions on speculation could leave out DOB without undue violence to the rest of its memory model, which might well be a failure of imagination on my part. So what are your thoughts on how reasonable hardware could leave out DOB?</span><br> <p> I agree with your point of view; DOB is a practical necessity. Another thing it is needed for, which the slides don't mention, is precise exceptions. Consider the Linux-kernel litmus test on slide 41; if the value of gp happened to be NULL so that the write to r1[i].value caused an addressing exception, the exception wouldn't be precise if the write to x (later in the program) had already been executed.<br> <p> Nevertheless, even though I expect DOB will be enforced by all hardware that runs the Linux kernel, there doesn't seem to be any good reason for including it in the memory model. As far as I know, nobody has suggested any use cases for it or shown that any existing code in the kernel relies on it.<br> <p> <span class="QuotedText">&gt; Slide 69's attempt does show a difference between ARMv8 and PPC using R,</span><br> <p> Indeed it does. And I would claim that this difference is _entirely_ due to R and has no connection with HOB at all. Despite what the slide says.<br> <p> <span class="QuotedText">&gt; I would of course be very happy for you to show me a better litmus test demonstrating the beyond-coherence-before effects of HOB!</span><br> <p> I don't know what effects you're talking about. As far as I can tell, the only effect of HOB is related to coherence ordering; it says that the value read by R1 has to be earlier in the coherence order than the value written by E2.<br> </div> Fri, 07 Jun 2024 18:58:06 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977582/ https://lwn.net/Articles/977582/ PaulMcKenney <div class="FormattedComment"> No argument whatsoever with the confusing part! ;-)<br> <p> Agreed, HOB has to be more than coherence-before. And the point of those slides was in fact to come up with a test for some ordering stronger than coherence-before. Slide 69's attempt does show a difference between ARMv8 and PPC using R, but you are quite right if you are claiming that I did not prove either: (1) That this difference was in fact a consequence of ARMv8 HOB, or (2) That there is not some better test for the effects of HOB beyond coherence-before. I would of course be very happy for you to show me a better litmus test demonstrating the beyond-coherence-before effects of HOB! I am with you in suspecting that other-multicopy atomicity has something to do with it, but I have not yet proved this to myself. (Nor have I yet asked the ARM folks because experience indicates that if I haven't beat my head pretty hard against the question, I won't understand their answer.)<br> <p> I am not so sure that I agree with your suggested change to the "exists" clause, given that DOB requires that R3 be coherence-before E2. What am I missing here?<br> <p> You are quite correct on the historical sequence of events leading to our deciding to leave DOB out of LKMM. I was instead giving a post-facto rationale for that decision. I do not see how current hardware with current restrictions on speculation could leave out DOB without undue violence to the rest of its memory model, which might well be a failure of imagination on my part. So what are your thoughts on how reasonable hardware could leave out DOB?<br> </div> Fri, 07 Jun 2024 15:40:46 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977470/ https://lwn.net/Articles/977470/ Alan.Stern <div class="FormattedComment"> The situation is very confusing; a more detailed explanation may help.<br> <p> For one thing, consider the description of Hazard Ordered Before (HOB) on slide 60 of your talk. The slide isn't entirely clear; does it require that R1 is coherence-before W2, or does it require that R1 executes before R3? The first meaning (which seems to be the one you're using) follows directly from the read-read coherence rule, so it is a consequence of per-variable sequential consistency. Since you undoubtedly want the BPF memory model to include this, there's no need to consider HOB. And to be crystal clear, PowerPC does not allow an earlier read to observe a later value than a later read of the same variable in the same thread.<br> <p> Adding to the confusion is the fact that, despite its title, the litmus test on slide 69 does not in fact test for HOB. That is, the condition in the "exists" clause isn't 0:r3=1 /\ 0:r5=0. The whole added business about y and the memory barriers is irrelevant for HOB. (Compare it to the Linux kernel code fragment on slide 62.) If you wanted to discuss the R pattern in slides 63-68, you shouldn't have gotten it all mixed up with HOB. (That could well be what confused Daroc.) It probably would have been better to avoid both topics and consider other-multicopy atomicity instead.<br> <p> Another thing is worth pointing out, relating to slide 57. Why doesn't the LKMM include Dependency Ordered Before? Not because it knows the full execution a priori -- that's equally true for the hardware memory models. The real reason goes back to the early days of development of the LKMM. Originally we had two different versions, a strong and a weak model. The strong model _did_ include Dependency Ordered Before. But we ended up dropping the strong model; the current LKMM is a descendant of our weak model.<br> </div> Thu, 06 Jun 2024 18:43:43 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977460/ https://lwn.net/Articles/977460/ PaulMcKenney <div class="FormattedComment"> Looks good to me!<br> </div> Thu, 06 Jun 2024 17:10:34 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977459/ https://lwn.net/Articles/977459/ daroc <p> Thank you for catching that; I had to read through Paul's slides again myself to figure out what was going on. </p> <p> I <em>think</em> that I have made a correct correction to the article. </p> Thu, 06 Jun 2024 17:06:08 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977447/ https://lwn.net/Articles/977447/ comex <div class="FormattedComment"> So Daroc indeed described this incorrectly in the article.<br> <p> Which is quite understandable! The property actually being tested is subtle enough that even with the benefit of reading the slides repeatedly and then reviewing the ARM architecture reference manual, I still don't really understand it :)<br> <p> </div> Thu, 06 Jun 2024 16:48:38 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977385/ https://lwn.net/Articles/977385/ PaulMcKenney <div class="FormattedComment"> That most certainly would be a novel claim. ;-)<br> <p> But the question is instead: "In ARMv8's hazard-ordered-before (HOB), does the fact that R2 cannot read a value older than that read by R1 impose additional ordering consequences?"<br> <p> The answer for PowerPC is "no". So to the extent that HOB does impose additional ordering consequences, the BPF memory model needs to avoid imposing similar ordering consequences. This is not a surprise, given that ARMv8 is other-multicopy atomic and PowerPC is non-multicopy atomic. So we should expect that there are constraints in the ARMv8 memory model that do not apply to the BPF memory model. This is all OK, because it is OK for hardware to provide stronger ordering than required, with x86 being the usual poster child for this extra strength.<br> <p> For more detail, please see slides 59-77 of the presentation at <a href="https://drive.google.com/file/d/1zLkpBVL1chDfyqZATOyKb_1QXnCdEmi5/view?usp=sharing.">https://drive.google.com/file/d/1zLkpBVL1chDfyqZATOyKb_1Q...</a><br> <p> Perhaps one of your questions is addressed by slide 64 and later. The initial scenario involves only one variable, but the added scaffolding required to test for additional ordering does add another variable. Does that help?<br> </div> Thu, 06 Jun 2024 13:53:07 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977335/ https://lwn.net/Articles/977335/ foom <div class="FormattedComment"> Just to be clear, you are saying the article's statement "operation R1 is allowed to read the value written by W1, even though R2 doesn't." is an accurate reflection of your intended statement?<br> <p> That, on powerpc CPUs, if the initial value is 0 and W1 stores a 1, you could have R1 read 1 (as written by W1), yet R2 (later in program order than R1, on the same thread) read the initial value of 0?<br> <p> I'm pretty sure that would be a novel claim, if so.<br> </div> Thu, 06 Jun 2024 12:25:14 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977326/ https://lwn.net/Articles/977326/ PaulMcKenney <div class="FormattedComment"> You got it right, the "hazard ordered before" example has only the single variable "x".<br> <p> Admittedly unconventional for a litmus test, but then again, this particular litmus tests probes an obscure corner of the ARMv8 memory model. ;-)<br> </div> Wed, 05 Jun 2024 23:45:34 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977232/ https://lwn.net/Articles/977232/ daroc <div class="FormattedComment"> It is, of course, entirely possible that I have made an error. Looking at my notes, I am quite sure that McKenney's second example did involve only one memory location, but it's possible that I missed some nuance of the specific kind of loads and stores used. There was a substantial portion of the talk dedicated to explaining how the reordering could actually be detected which went far enough over my head (and into the weeds) that I elided over it in the article.<br> </div> Wed, 05 Jun 2024 13:38:32 +0000 An atomic instruction is a full barrier to everything? https://lwn.net/Articles/977149/ https://lwn.net/Articles/977149/ farnz <p>Yes, and that's implicit in virtually all descriptions of memory ordering; what we normally talk about is "happens-before" ordering, where we say that A happens-before B if and only if we can deduce that A must have happened given that we can demonstrate that B has happened. <p>There's also sometimes the need to talk about whether a happens-before ordering is global (i.e. if thread X sees that A happens-before B, then thread Y must agree that A happens-before B) or local (if thread X sees that A happens-before B, thread Y could still see that B happens-before A, that A happens-before B, or that there's no ordering relationship between A and B). This is, I think what you were reaching towards; an atomic instruction does not always guarantee a global ordering, but on x86, it's defined as doing so (<tt>LOCK</tt> prefixes are defined this way). Wed, 05 Jun 2024 11:14:08 +0000 An atomic instruction is a full barrier to everything? https://lwn.net/Articles/977148/ https://lwn.net/Articles/977148/ epa <div class="FormattedComment"> Yes I see your point. So a more pedantic way to state the property would be that “if the effect of the atomic instruction is visible then the effects of all instructions before it are visible”. <br> </div> Wed, 05 Jun 2024 10:57:27 +0000 An atomic instruction is a full barrier to everything? https://lwn.net/Articles/977144/ https://lwn.net/Articles/977144/ farnz <p>There are CPUs that are ultra-strict like that (all x86 CPUs, I believe, meet this requirement); in your example, though, the detail you're missing is that if no other code uses r9 or address 123, there's no way to tell if the swap instruction was executed. <p>And it's a one-way condition; if the swap instruction was executed, then you know that all instructions before the swap instruction were executed. But if you do not know whether or not the swap instruction was executed, you do not know if the instructions before it are visible or not - and if you neither look at r9, nor inspect address 123, how do you know it executed? Wed, 05 Jun 2024 10:46:33 +0000 An atomic instruction is a full barrier to everything? https://lwn.net/Articles/977119/ https://lwn.net/Articles/977119/ epa <blockquote>All CPUs and tasks agree that the side effects of all instructions that come before an atomic instruction are visible before it, and all effects of subsequent instructions are not visible until it has been executed.</blockquote> Is that really true? I am sure there are CPUs that are ultra-strict like that. But I would have expected that only side effects of that instruction (and the earlier instructions which caused it to happen) are guaranteed visible. <p> If I do an atomic swap between register r9 and address 123, but no other code anywhere uses r9 or looks anywhere near that address, is it necessarily the case that my swap instruction makes visible the effect of all instructions before it? So I could do things with other registers and addresses, then do any swap instruction on any register to act as a barrier for everything done so far? Wed, 05 Jun 2024 05:59:19 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977112/ https://lwn.net/Articles/977112/ foom <div class="FormattedComment"> PPC definitely doesn't allow the reordering on a single memory location described here as example 2.<br> <p> I suspect the description may have been oversimplified or mis-transcribed from the talk, and likely originally involved mutations on multiple memory locations.<br> </div> Wed, 05 Jun 2024 04:37:25 +0000 An instruction-level BPF memory model https://lwn.net/Articles/977104/ https://lwn.net/Articles/977104/ comex <div class="FormattedComment"> Huh. Doesn’t the second example violate C++11’s read-read coherence requirement? [1] That is, assuming you translate the reads and writes described in the example into std::atomic loads and stores that use memory_ordering_relaxed. Yet the C++11 memory model was designed with the intent that relaxed loads and stores would compile into simple load and store instructions, without barriers, on the architectures common at the time – which includes POWER.<br> <p> So what gives? Am I misunderstanding the example, am I misunderstanding the C++ memory model, or is there actually a conflict here?<br> <p> [1] As described in: <a href="https://en.cppreference.com/w/cpp/atomic/memory_order">https://en.cppreference.com/w/cpp/atomic/memory_order</a><br> </div> Wed, 05 Jun 2024 01:01:55 +0000