fix vma->anon_vma check for per-VMA locking; fix anon_vma memory ordering
Quoted 96 lines · Will Deacon
Quoted 79 lines · Jann Horn
Quoted 45 lines · Paul E. McKenney
Quoted 10 lines · Jann Horn
Hi!
Patch 1 here is a straightforward fix for a race in per-VMA locking code that can lead to use-after-free; I hope we can get this one into mainline and stable quickly.
Patch 2 is a fix for what I believe is a longstanding memory ordering issue in howvma->anon_vmais used across the MM subsystem; I expect that this one will have to go through a few iterations of review and potentially rewrites, because memory ordering is tricky.
(If someone else wants to take over patch 2, I would be very happy.) These patches don't really belong together all that much, I'm justQuoted 8 lines
sending them as a series because they'd otherwise conflict. I am CCing:
- Suren because patch 1 touches his code
- Matthew Wilcox because he is also currently working on per-VMA locking stuff
- all the maintainers/reviewers for the Kernel Memory Consistency Model so they can help figure out theREAD_ONCE()vssmp_load_acquire()thing
READ_ONCE()has weaker ordering properties thansmp_load_acquire(). For example, given a pointer gp:p = whichever(gp); a = 1; r1 = p->b; if ((uintptr_t)p & 0x1) WRITE_ONCE(b, 1); WRITE_ONCE(c, 1);Leaving aside the "&" needed by
smp_load_acquire(), if "whichever" is "READ_ONCE", then the load fromp->band theWRITE_ONCE()to "b" are ordered after the load from gp (the former due to an address dependency and the latter due to a (fragile) control dependency). The compiler is within its rights to reorder the store to "a" to precede the load from gp. The compiler is forbidden from reordering the store to "c" wtih the load from gp (because both are volatile accesses), but the CPU is completely within its rights to do this reordering.
But if "whichever" is "smp_load_acquire()", all four of the subsequent memory accesses are ordered after the load from gp. Similarly, forWRITE_ONCE()andsmp_store_release():p = READ_ONCE(gp); r1 = READ_ONCE(gi); r2 = READ_ONCE(gj); a = 1; WRITE_ONCE(b, 1); if (r1 & 0x1) whichever(p->q, r2);Quoted 11 lines
Again leaving aside the "&" needed by
smp_store_release(), if "whichever" isWRITE_ONCE(), then the load from gp, the load from gi, and the load from gj are all ordered before the store top->q(by address dependency, control dependency, and data dependency, respectively). The store to "a" can be reordered with the store top->qby the compiler. The store to "b" cannot be reordered with the store top->qby the compiler (again, both are volatile), but the CPU is free to reorder them, especially whenwhichever()is implemented as a conditional store.
But if "whichever" is "smp_store_release()", all five of the earlier memory accesses are ordered before the store top->q.
Does that help, or am I missing the point of your question?My main question is how permissible/ugly you think the following use of
READ_ONCE()would be, and whether you think it ought to be ansmp_load_acquire()instead.Assume that we are holding some kind of lock that ensures that the only possible concurrent update to "
vma->anon_vma" is that it changes from a NULL pointer to a non-NULL pointer (usingsmp_store_release()).if (
READ_ONCE(vma->anon_vma)!= NULL) {
// we now know thatvma->anon_vmacannot change anymore// access the same memory location again with a plain load struct
anon_vma*a =vma->anon_vma;// this needs to be address-dependency-ordered against one of // the loads from
vma->anon_vma
structanon_vma*root =a->root;
}Quoted 4 lines
Is this fine? If it is not fine just because the compiler might reorder the plain load of
vma->anon_vmabefore theREAD_ONCE()load, would it be fine after adding abarrier()directly after theREAD_ONCE()?I'm _very_ wary of mixing
READ_ONCE()and plain loads to the same variable, as I've run into cases where you have sequences such as:// Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently foo = *ptr; bar = READ_ONCE(*ptr); baz = *ptr;
Quoted 6 lines
and you can get foo == baz == 0 but bar == 1 because the compiler only ends up reading from memory twice.
That was the root cause behind
f069faba6887("arm64: mm: UseREAD_ONCEwhen dereferencing pointer to pte table"), which was very unpleasant to debug.
Will, Unless I am missing something fundamental, this case is different though.
This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) should work fine, no?
I am not seeing how the compiler can screw that up, so please do enlighten :).
Also RCU read dereference does a similar pattern (as Alan also pointed).
Cheers,
- Joel
Quoted 2 lines
Will
Quoted 32 lines · Joel Fernandes
Quoted 32 lines · Will Deacon
Quoted 12 lines · Jann Horn
if (
READ_ONCE(vma->anon_vma)!= NULL) {
// we now know thatvma->anon_vmacannot change anymore// access the same memory location again with a plain load struct
anon_vma*a =vma->anon_vma;// this needs to be address-dependency-ordered against one of // the loads from
vma->anon_vma
structanon_vma*root =a->root;
}Quoted 4 lines
Is this fine? If it is not fine just because the compiler might reorder the plain load of
vma->anon_vmabefore theREAD_ONCE()load, would it be fine after adding abarrier()directly after theREAD_ONCE()?I'm _very_ wary of mixing
READ_ONCE()and plain loads to the same variable, as I've run into cases where you have sequences such as:// Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently foo = *ptr; bar = READ_ONCE(*ptr); baz = *ptr;and you can get foo == baz == 0 but bar == 1 because the compiler only ends up reading from memory twice.
Quoted 8 lines
Quoted 3 lines
That was the root cause behind
f069faba6887("arm64: mm: UseREAD_ONCEwhen dereferencing pointer to pte table"), which was very unpleasant to debug.Will, Unless I am missing something fundamental, this case is different though.
This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) should work fine, no?
I am not seeing how the compiler can screw that up, so please do enlighten :).
I guess the thing I'm worried about is if there is some previous read of 'vma->anon_vma' which didn't use READ_ONCE() and the compiler kept the result around in a register. In that case, 'a' could be NULL, even if the READ_ONCE(vma->anon_vma) returned non-NULL.
The crux of the issue is that the compiler can break read-after-read ordering if you don't use READ_ONCE() consistently. Sadly, judging by the other part of the thread from Nadav, it's fiddly to fix this without wrecking the codegen.
Will
Quoted 43 lines · Will Deacon
Quoted 32 lines · Joel Fernandes
Quoted 32 lines · Will Deacon
Quoted 12 lines · Jann Horn
if (
READ_ONCE(vma->anon_vma)!= NULL) {
// we now know thatvma->anon_vmacannot change anymore// access the same memory location again with a plain load struct
anon_vma*a =vma->anon_vma;// this needs to be address-dependency-ordered against one of // the loads from
vma->anon_vma
structanon_vma*root =a->root;
}Quoted 4 lines
Is this fine? If it is not fine just because the compiler might reorder the plain load of
vma->anon_vmabefore theREAD_ONCE()load, would it be fine after adding abarrier()directly after theREAD_ONCE()?I'm _very_ wary of mixing
READ_ONCE()and plain loads to the same variable, as I've run into cases where you have sequences such as:// Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently foo = *ptr; bar = READ_ONCE(*ptr); baz = *ptr;and you can get foo == baz == 0 but bar == 1 because the compiler only ends up reading from memory twice.
Quoted 8 lines
Quoted 3 lines
That was the root cause behind
f069faba6887("arm64: mm: UseREAD_ONCEwhen dereferencing pointer to pte table"), which was very unpleasant to debug.Will, Unless I am missing something fundamental, this case is different though.
This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) should work fine, no?
I am not seeing how the compiler can screw that up, so please do enlighten :).
Quoted 4 lines
I guess the thing I'm worried about is if there is some previous read of '
vma->anon_vma' which didn't useREAD_ONCE()and the compiler kept the result around in a register. In that case, 'a' could be NULL, even if theREAD_ONCE(vma->anon_vma)returned non-NULL.
If I can be a bit brave enough to say -- that appears to be a compiler bug to me. It seems that the compiler in such an instance violates the "Sequential Consistency Per Variable" rule? I mean if it can't even keep SCPV true for a same memory-location load (plain or not) for a sequence of code, how can it expect the hardware to.
In other words, with that kind of caching, the value of the variable goes back in time which will be tough luck for even a fully ordered sequentially-consistent processor!!!
Quoted 4 lines
The crux of the issue is that the compiler can break read-after-read ordering if you don't use
READ_ONCE()consistently. Sadly, judging by the other part of the thread from Nadav, it's fiddly to fix this without wrecking the codegen.
Right. Thanks to you and others for sharing your informative perspective as always,
- Joel
Quoted 49 lines · Joel Fernandes
Quoted 43 lines · Will Deacon
Quoted 32 lines · Joel Fernandes
Quoted 32 lines · Will Deacon
Quoted 12 lines · Jann Horn
if (
READ_ONCE(vma->anon_vma)!= NULL) {
// we now know thatvma->anon_vmacannot change anymore// access the same memory location again with a plain load struct
anon_vma*a =vma->anon_vma;// this needs to be address-dependency-ordered against one of // the loads from
vma->anon_vma
structanon_vma*root =a->root;
}Quoted 4 lines
Is this fine? If it is not fine just because the compiler might reorder the plain load of
vma->anon_vmabefore theREAD_ONCE()load, would it be fine after adding abarrier()directly after theREAD_ONCE()?I'm _very_ wary of mixing
READ_ONCE()and plain loads to the same variable, as I've run into cases where you have sequences such as:// Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently foo = *ptr; bar = READ_ONCE(*ptr); baz = *ptr;and you can get foo == baz == 0 but bar == 1 because the compiler only ends up reading from memory twice.
Quoted 8 lines
Quoted 3 lines
That was the root cause behind
f069faba6887("arm64: mm: UseREAD_ONCEwhen dereferencing pointer to pte table"), which was very unpleasant to debug.Will, Unless I am missing something fundamental, this case is different though.
This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) should work fine, no?
I am not seeing how the compiler can screw that up, so please do enlighten :).Quoted 4 lines
I guess the thing I'm worried about is if there is some previous read of '
vma->anon_vma' which didn't useREAD_ONCE()and the compiler kept the result around in a register. In that case, 'a' could be NULL, even if theREAD_ONCE(vma->anon_vma)returned non-NULL.
Quoted 5 lines
If I can be a bit brave enough to say -- that appears to be a compiler bug to me. It seems that the compiler in such an instance violates the "Sequential Consistency Per Variable" rule? I mean if it can't even keep SCPV true for a same memory-location load (plain or not) for a sequence of code, how can it expect the hardware to.
It's not a compiler bug. In this example, some other thread performs a write that changes vma->anon_vma from NULL to non-NULL. This write races with the plain reads, and compilers are not required to obey the "Sequential Consistency Per Variable" rule (or indeed, any rule) when there is a data race.
Alan Stern
Quoted 57 lines · Alan Stern
Quoted 49 lines · Joel Fernandes
Quoted 43 lines · Will Deacon
Quoted 32 lines · Joel Fernandes
Quoted 32 lines · Will Deacon
Quoted 12 lines · Jann Horn
if (
READ_ONCE(vma->anon_vma)!= NULL) {
// we now know thatvma->anon_vmacannot change anymore// access the same memory location again with a plain load struct
anon_vma*a =vma->anon_vma;// this needs to be address-dependency-ordered against one of // the loads from
vma->anon_vma
structanon_vma*root =a->root;
}Quoted 4 lines
Is this fine? If it is not fine just because the compiler might reorder the plain load of
vma->anon_vmabefore theREAD_ONCE()load, would it be fine after adding abarrier()directly after theREAD_ONCE()?I'm _very_ wary of mixing
READ_ONCE()and plain loads to the same variable, as I've run into cases where you have sequences such as:// Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently foo = *ptr; bar = READ_ONCE(*ptr); baz = *ptr;and you can get foo == baz == 0 but bar == 1 because the compiler only ends up reading from memory twice.
Quoted 8 lines
Quoted 3 lines
That was the root cause behind
f069faba6887("arm64: mm: UseREAD_ONCEwhen dereferencing pointer to pte table"), which was very unpleasant to debug.Will, Unless I am missing something fundamental, this case is different though.
This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) should work fine, no?
I am not seeing how the compiler can screw that up, so please do enlighten :).Quoted 4 lines
I guess the thing I'm worried about is if there is some previous read of '
vma->anon_vma' which didn't useREAD_ONCE()and the compiler kept the result around in a register. In that case, 'a' could be NULL, even if theREAD_ONCE(vma->anon_vma)returned non-NULL.Quoted 5 lines
If I can be a bit brave enough to say -- that appears to be a compiler bug to me. It seems that the compiler in such an instance violates the "Sequential Consistency Per Variable" rule? I mean if it can't even keep SCPV true for a same memory-location load (plain or not) for a sequence of code, how can it expect the hardware to.
Quoted 5 lines
It's not a compiler bug. In this example, some other thread performs a write that changes
vma->anon_vmafrom NULL to non-NULL. This write races with the plain reads, and compilers are not required to obey the "Sequential Consistency Per Variable" rule (or indeed, any rule) when there is a data race.
So you're saying the following code behavior is OK?
/* Say anon_vma can only ever transition from NULL to non-NULL values */ a = vma->anon_vma; // Reads NULL
b = READ_ONCE(vma->anon_vma); // Reads non-NULL
c = vma->anon_vma; // Reads NULL!!!
if (b) {
c->some_attribute++; // Oopsie
}
thanks,
- Joel
(On the road now, so my replies will be slightly delayed for few hours)
Quoted 66 lines · Joel Fernandes
Quoted 57 lines · Alan Stern
Quoted 49 lines · Joel Fernandes
Quoted 43 lines · Will Deacon
Quoted 32 lines · Joel Fernandes
Quoted 32 lines · Will Deacon
Quoted 12 lines · Jann Horn
if (
READ_ONCE(vma->anon_vma)!= NULL) {
// we now know thatvma->anon_vmacannot change anymore// access the same memory location again with a plain load struct
anon_vma*a =vma->anon_vma;// this needs to be address-dependency-ordered against one of // the loads from
vma->anon_vma
structanon_vma*root =a->root;
}Quoted 4 lines
Is this fine? If it is not fine just because the compiler might reorder the plain load of
vma->anon_vmabefore theREAD_ONCE()load, would it be fine after adding abarrier()directly after theREAD_ONCE()?I'm _very_ wary of mixing
READ_ONCE()and plain loads to the same variable, as I've run into cases where you have sequences such as:// Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently foo = *ptr; bar = READ_ONCE(*ptr); baz = *ptr;and you can get foo == baz == 0 but bar == 1 because the compiler only ends up reading from memory twice.
Quoted 8 lines
Quoted 3 lines
That was the root cause behind
f069faba6887("arm64: mm: UseREAD_ONCEwhen dereferencing pointer to pte table"), which was very unpleasant to debug.Will, Unless I am missing something fundamental, this case is different though.
This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) should work fine, no?
I am not seeing how the compiler can screw that up, so please do enlighten :).Quoted 4 lines
I guess the thing I'm worried about is if there is some previous read of '
vma->anon_vma' which didn't useREAD_ONCE()and the compiler kept the result around in a register. In that case, 'a' could be NULL, even if theREAD_ONCE(vma->anon_vma)returned non-NULL.Quoted 5 lines
If I can be a bit brave enough to say -- that appears to be a compiler bug to me. It seems that the compiler in such an instance violates the "Sequential Consistency Per Variable" rule? I mean if it can't even keep SCPV true for a same memory-location load (plain or not) for a sequence of code, how can it expect the hardware to.
Quoted 5 lines
It's not a compiler bug. In this example, some other thread performs a write that changes
vma->anon_vmafrom NULL to non-NULL. This write races with the plain reads, and compilers are not required to obey the "Sequential Consistency Per Variable" rule (or indeed, any rule) when there is a data race.So you're saying the following code behavior is OK?
Quoted 7 lines
/* Say
anon_vmacan only ever transition from NULL to non-NULL values */ a =vma->anon_vma; // Reads NULL
b =READ_ONCE(vma->anon_vma); // Reads non-NULL
c =vma->anon_vma; // Reads NULL!!!
if (b) {
c->some_attribute++; // Oopsie
}
Is there some way to obtain (a && !b) that does not involve a data race, and they carte blanche for the compiler to do whatever it pleases? I am not seeing one.
What am I missing?
Thanx, Paul
Quoted 79 lines · Paul E. McKenney
Quoted 66 lines · Joel Fernandes
Quoted 58 lines · Alan Stern
Quoted 49 lines · Joel Fernandes
Quoted 43 lines · Will Deacon
Quoted 32 lines · Joel Fernandes
Quoted 32 lines · Will Deacon
Quoted 12 lines · Jann Horn
if (
READ_ONCE(vma->anon_vma)!= NULL) {
// we now know thatvma->anon_vmacannot change anymore// access the same memory location again with a plain load struct
anon_vma*a =vma->anon_vma;// this needs to be address-dependency-ordered against one of // the loads from
vma->anon_vma
structanon_vma*root =a->root;
}Quoted 4 lines
Is this fine? If it is not fine just because the compiler might reorder the plain load of
vma->anon_vmabefore theREAD_ONCE()load, would it be fine after adding abarrier()directly after theREAD_ONCE()?I'm _very_ wary of mixing
READ_ONCE()and plain loads to the same variable, as I've run into cases where you have sequences such as:// Assume *ptr is initially 0 and somebody else writes it to 1 // concurrently
foo = *ptr; bar = READ_ONCE(*ptr); baz = *ptr;and you can get foo == baz == 0 but bar == 1 because the compiler only ends up reading from memory twice.
Quoted 8 lines
Quoted 3 lines
That was the root cause behind
f069faba6887("arm64: mm: UseREAD_ONCEwhen dereferencing pointer to pte table"), which was very unpleasant to debug.Will, Unless I am missing something fundamental, this case is different though.
This case does not care about fewer reads. As long as the first read is volatile, the subsequent loads (even plain) should work fine, no?
I am not seeing how the compiler can screw that up, so please do enlighten :).Quoted 4 lines
I guess the thing I'm worried about is if there is some previous read of '
vma->anon_vma' which didn't useREAD_ONCE()and the compiler kept the result around in a register. In that case, 'a' could be NULL, even if theREAD_ONCE(vma->anon_vma)returned non-NULL.Quoted 5 lines
If I can be a bit brave enough to say -- that appears to be a compiler bug to me. It seems that the compiler in such an instance violates the "Sequential Consistency Per Variable" rule? I mean if it can't even keep SCPV true for a same memory-location load (plain or not) for a sequence of code, how can it expect the hardware to.
Quoted 5 lines
It's not a compiler bug. In this example, some other thread performs a write that changes
vma->anon_vmafrom NULL to non-NULL. This write races with the plain reads, and compilers are not required to obey the "Sequential Consistency Per Variable" rule (or indeed, any rule) when there is a data race.So you're saying the following code behavior is OK?
Quoted 7 lines
/* Say
anon_vmacan only ever transition from NULL to non-NULL values */ a =vma->anon_vma; // Reads NULL
b =READ_ONCE(vma->anon_vma); // Reads non-NULL
c =vma->anon_vma; // Reads NULL!!!
if (b) {
c->some_attribute++; // Oopsie
}
Quoted 5 lines
Is there some way to obtain (a && !b) that does not involve a data race, and they carte blanche for the compiler to do whatever it pleases? I am not seeing one.
What am I missing?
Probably nothing. I think I was living briefly in a fantasy world where I expected predictable compiler behavior on same-memory accesses amidst data traces. It is good to come back to reality.
thanks,
- Joel
Quoted 2 lines
Thanx, Paul