Question on (prop \ id) & int excluding non-trivial self-links
Hi all,
While experimenting with extending LKMM to model MMIO semantics, we noticed a subtlety in how hb uses prop in linux-kernel.cat:
let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ; [Marked] ; rfe? ; [Marked]
let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked] acyclic hb as happens-before
Because every step in prop is optional, (x, x) is in prop in the degenerate zero-step [Marked] case, and hb uses (prop \ id) & int to drop those trivial self-links.
However, \ id removes all self-links, including non-trivial multi-step paths in prop from an event back to itself. Semantically, such a path means an overwrite of A must propagate back to A's CPU before A executes, which should be forbidden as an hb cycle rather than excluded by \ id.
In the current model, this seems harmless because any candidate execution with a non-trivial prop self-link is already rejected elsewhere. In our MMIO experiment, some of those self-cycles were no longer caught elsewhere and leaked through because prop \ id stripped them.
Defining a non-empty variant of prop and using (nonempty-prop & int) in hb avoids dropping real cycles. Is this a semantic issue? Would patches for this kind of issue be welcome?
Note 1: Here's an example where A ->prop A:
C prop-self-loop
{}
P0(int *x, int *y)
{
int r0;
r0 = READ_ONCE(*y);
smp_store_release(x, 1); // A
}P1(int *x, int *y)
{
WRITE_ONCE(*x, 2);
smp_store_release(y, 1);
}exists (0:r0=1 /\ x=2)
In the candidate execution with 0:r0=1 and x=2, A reaches itself in prop via coe ; po-rel ; (rfe ; po-rel).
Note 2: Here's an example fix:
diff --git a/tools/memory-model/linux-kernel.cat b/tools/memory-model/linux-kernel.cat --- a/tools/memory-model/linux-kernel.cat +++ b/tools/memory-model/linux-kernel.cat @@ -99,14 +99,16 @@ let A-cumul(r) = (rfe ; [Marked])? ; r let rmw-sequence = (rf ; rmw)* let cumul-fence = [Marked] ; (A-cumul(strong-fence | po-rel) | wmb | po-unlock-lock-po) ; [Marked] ; rmw-sequence -let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ; - [Marked] ; rfe? ; [Marked] +let nonempty-prop = [Marked] ; ( + ((overwrite & ext) ; cumul-fence* ; [Marked] ; rfe?) | + (cumul-fence+ ; [Marked] ; rfe?) | rfe) ; [Marked] +let prop = [Marked] | nonempty-prop
(*
* Happens Before: Ordering from the passage of time.
* No fences needed here for prop because relation confined to one process. *)
-let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked] +let hb = [Marked] ; (ppo | rfe | (nonempty-prop & int)) ; [Marked]
acyclic hb as happens-before
Thanks,
Wayne
Alan Stern
maintainerQuoted 15 lines · Wayne Yan
Hi all,
While experimenting with extending LKMM to model MMIO semantics, we noticed a subtlety in how hb uses prop in linux-kernel.cat:
let prop = [Marked] ; (overwrite & ext)? ; cumul-fence* ; [Marked] ; rfe? ; [Marked]let hb = [Marked] ; (ppo | rfe | ((prop \ id) & int)) ; [Marked] acyclic hb as happens-before
Because every step in prop is optional, (x, x) is in prop in the degenerate zero-step [Marked] case, and hb uses (prop \ id) & int to drop those trivial self-links.
Quoted 5 lines
However, \ id removes all self-links, including non-trivial multi-step paths in prop from an event back to itself. Semantically, such a path means an overwrite of A must propagate back to A's CPU before A executes, which should be forbidden as an hb cycle rather than excluded by \ id.
True. But consider: How do you (or more accurately, how does the memory model) know that the overwrite reached A's CPU before A executed? The answer is that there is some load B, on A's CPU, which happens before A and reads a value that reaches that CPU after the overwrite does. Then prop would establish a link from A to B, as well as the link from A to itself. This link from A to B, combined with B ->hb A, is enough to rule out the execution. The memory model doesn't need to know that prop links A to itself.
There are other cases you can consider, such as when A and B access the same location. But then, even if A and B are the same access, the execution would already be ruled out by the coherence axiom.
Quoted 3 lines
In the current model, this seems harmless because any candidate execution with a non-trivial prop self-link is already rejected elsewhere.
More or less as I said.
Quoted 3 lines
In our MMIO experiment, some of those self-cycles were no
longer caught elsewhere and leaked through because prop \ id stripped them.
As Jonas asked, how does this happen?
Quoted 3 lines
Defining a non-empty variant of prop and using (nonempty-prop & int) in hb avoids dropping real cycles. Is this a semantic issue? Would patches for this kind of issue be welcome?
There doesn't seem to be any need for them, especially considering that the memory model is not meant to apply to MMIO. In fact, you would probably want a completely different memory model if you had to cover interactions between CPUs and I/O devices.
Quoted 11 lines
Note 1: Here's an example where A ->prop A:
C prop-self-loop
{}
P0(int *x, int *y){ int r0; r0 = READ_ONCE(*y);
Let B be this READ_ONCE() access.
Quoted 9 lines
smp_store_release(x, 1); // A }
P1(int *x, int *y){ WRITE_ONCE(*x, 2); smp_store_release(y, 1); }
Quoted 4 lines
exists (0:r0=1 /\ x=2)
In the candidate execution with 0:r0=1 and x=2, A reaches itself in prop via coe ; po-rel ; (rfe ; po-rel).
But also A ->prop B via (coe ; po-rel ; rfe). And since B is linked to A via po-rel, there already is a cycle in hb not relying on A ->prop A.
Alan Stern
Quoted 5 lines · Alan Stern
Quoted 3 lines
In our MMIO experiment, some of those self-cycles were no longer caught elsewhere and leaked through because prop \ id stripped them.
As Jonas asked, how does this happen?
Jonas is right: I put an ordering relation in cumul-fence and didn't put it in hb in an earlier draft of our model, thinking that since cumul-fence is a stronger guarantee, putting it there was enough.
Specifically, I was modeling Rule 2 for readX()/writeX() from Documentation/memory-barriers.txt:
2. A writeX() issued by a CPU thread holding a spinlock is ordered before a writeX() to the same peripheral from another CPU thread issued after a later acquisition of the same spinlock.
as:
let in-spinlock = ([LKW] ; po) \ ([LKW] ; po-loc ; [UL] ; po?) let mmio-lock = [Mmio-write] ; in-spinlock^-1 ; co ; in-spinlock ; [Mmio-write]
and put it inside cumul-fence after restricting acq-po/po-rel to normal RAM, per the rule that SMP acquire/release and spinlocks do not order relaxed MMIO accesses.
Once we added the proper MMIO hb edges, changing back to prop \ id changed nothing, so no upstream change to prop is needed.
Quoted 4 lines
There doesn't seem to be any need for them, especially considering that the memory model is not meant to apply to MMIO. In fact, you would probably want a completely different memory model if you had to cover interactions between CPUs and I/O devices.
On this point: our goal is to model the CPU-side ordering guarantees for MMIO primitives mentioned in memory-barriers.txt. Since driver code uses them alongside normal RAM accesses, spinlocks, and RCU, extending linux-kernel.cat seemed much more practical for our use case. Do you think it's wrong to put the CPU-side subset on top of the existing linux-kernel.cat?
Thanks,
Wayne
Alan Stern
maintainerQuoted 6 lines · Wayne Yan
Quoted 4 lines · Alan Stern
There doesn't seem to be any need for them, especially considering that the memory model is not meant to apply to MMIO. In fact, you would probably want a completely different memory model if you had to cover interactions between CPUs and I/O devices.
Quoted 6 lines
On this point: our goal is to model the CPU-side ordering guarantees for MMIO primitives mentioned in memory-barriers.txt. Since driver code uses them alongside normal RAM accesses, spinlocks, and RCU, extending linux-kernel.cat seemed much more practical for our use case. Do you think it's wrong to put the CPU-side subset on top of the existing linux-kernel.cat?
Nothing wrong at all, provided you put the extension in a separate file (like lock.cat is separate).
How does your memory model express the relation between two MMIO-mapped addresses of being on the same device?
Does the model include things like PCI's posted writes?
Alan Stern
Quoted 2 lines · Alan Stern
How does your memory model express the relation between two MMIO-mapped addresses of being on the same device?
For now we assume all MMIO variables in a litmus test belong to a single peripheral. Supporting multiple distinct peripherals in one test would probably require changes to the herd7 runtime that we haven't looked into yet.
Quoted 1 line
Does the model include things like PCI's posted writes?
We don't model a PCI bus explicitly; we only constrain what memory-barriers.txt and device-io.rst guarantee and nothing else, so writeX() ordering stays loose enough to allow posted-write effects unless flushed.
For example, in this test:
P0(int *reg, int *buf)
{
int r1;
writel(1, reg);
r1 = READ_ONCE(*buf);
}
P1(int *reg, int *buf)
{
int r2;
WRITE_ONCE(*buf, 1);
mb();
r2 = readl(reg);
}exists (0:r1=0 /\ 1:r2=0)
0:r1=0 /\ 1:r2=0 is allowed because writel() is only ordered after prior RAM writes, so P0 can execute READ_ONCE(*buf) while writel() is still posted in flight.
Instead, inserting a readl(reg); flush right after writel(1, reg); on P0 makes the outcome forbidden because writel() is ordered before readl() to the same peripheral and readl() completes before subsequent RAM reads, matching the readback flush pattern in device-io.rst.
Similarly, under a spinlock:
P0(int *reg, int *x, spinlock_t *l)
{
spin_lock(l);
writel(1, reg);
WRITE_ONCE(*x, 1);
spin_unlock(l);
}
P1(int *reg, int *x, spinlock_t *l)
{
int r0;
int r1;
spin_lock(l);
r0 = READ_ONCE(*x);
if (r0)
r1 = readl(reg);
spin_unlock(l);
}exists (1:r0=1 /\ 1:r1=0)
1:r0=1 /\ 1:r1=0 is still allowed because spinlocks only order writeX() against a later lock holder's writeX().
Thanks,
Wayne