| Issue |
238205
|
| Summary |
[libc][GPU] free() has no release ordering: memory from malloc() contains another thread's data on MI300A
|
| Labels |
|
| Assignees |
|
| Reporter |
sbryngelson
|
On MI300A, memory returned by the GPU libc `malloc` can be overwritten by its previous owner. `Slab::deallocate` clears the chunk's bit with a relaxed `fetch_and`, and `Slab::allocate` sets it with a relaxed `fetch_or`. Nothing writes back the freeing thread's stores before a thread on another XCC can take the chunk.
It showed up in a Fortran kernel containing `loc = [i, j, k]`: flang turns the array constructor into a device `malloc`/`free`, and `loc` received other threads' values. The reproducer below is the same thing in C.
Tested on `main` (`fc16f151`; libc built with AFAR 24.3.0's clang and linked with `-Xoffload-linker libc.a`) and on AFAR 24.3.0.
## Reproducer
```c
// amdclang -fopenmp --offload-arch=gfx942 -O2 t.c && HSA_XNACK=1 ./a.out 20000000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct { int loc[3]; int ib; } rec_t;
int main(int argc, char **argv) {
long n = atol(argv[1]);
rec_t *r = calloc(n, sizeof(rec_t));
#pragma omp target enter data map(alloc : r[0 : n])
#pragma omp target teams distribute parallel for
for (long i = 0; i < n; i++) {
int *t = malloc(3 * sizeof(int));
t[0] = i; t[1] = i >> 8; t[2] = i ^ 1234;
memcpy(r[i].loc, t, sizeof t[0] * 3);
free(t);
r[i].ib = 1;
}
#pragma omp target update from(r[0 : n])
long bad = 0;
for (long i = 0; i < n; i++)
bad += r[i].loc[0] != (int)i || r[i].loc[1] != (int)(i >> 8) || r[i].loc[2] != (int)(i ^ 1234) || r[i].ib != 1;
printf("bad %ld of %ld\n", bad, n);
}
```
Each thread writes only its own record, so `bad` should be 0.
## Results
MI300A, `HSA_XNACK=1`, new process per launch, 10 launches each.
| libc | launches with `bad > 0` |
|---|---|
| AFAR 24.3.0 as shipped | 10 of 10 (665–1858 records) |
| `main`, unpatched | 10 of 10 (815–1965 records) |
| `main` with a release fence before the `fetch_and` | 0 of 10 |
| `main` with the patch below | 0 of 10 |
The Fortran program: 5 of 5 launches damaged unpatched, 0 of 5 patched.
It only happens when threads on more than one XCC allocate. With the allocating threads confined to one XCC there were no damaged records in 280M allocations; with three or more XCCs nearly every launch was damaged. Of single instructions put before `free`, `buffer_wbl2 sc1` (L2 write-back) cures it and `s_waitcnt vmcnt(0)` alone does not. HIP's device `malloc` (the ROCm allocator) does not show it.
## Patch
Commit e9350dc removed the release fence before the clear and the acquire fence after the claim. The release fence still in `GuardPtr::unlock` runs after the clear, so it does not order the caller's stores before the chunk can be reused.
```diff
@@ -313,7 +313,7 @@
before = cpp::AtomicRef(get_bitfield()[slot])
- .fetch_or(bitmask, cpp::MemoryOrder::RELAXED);
+ .fetch_or(bitmask, cpp::MemoryOrder::ACQUIRE);
@@ -343,7 +343,7 @@
cpp::AtomicRef(get_bitfield()[slot])
- .fetch_and(~(1u << bit), cpp::MemoryOrder::RELAXED);
+ .fetch_and(~(1u << bit), cpp::MemoryOrder::RELEASE);
```
`RELEASE` on the clear alone also passed every test, and fences work as well as ordered atomics. A kernel-time micro-benchmark showed no slowdown.
## Limits
Only gfx942 was tested. There is no hardware trace, so the cause is inferred from the tests above. The libc test suite was not run, and the libc was built with AFAR's compiler rather than an upstream toolchain.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs