Every kernel resource is a type with a Drop, so the
teardown order comes from the tree and not from the caller.
Each type owns the resources under it, so Drop runs
DESTROY_QUEUE, then the doorbell munmap, then munmap, UNMAP and FREE
for every Buffer, and closes the two fds last.
Drop is the method Rust runs when a
value goes out of scope. Every kernel resource is a type with a Drop,
so the types carry the teardown order. The caller does not have to
remember it.
The tree, outermost first:
Kfd holds the two fds and the gpu_id. Its Drop
closes both.
Buffer holds a va, a size, a
handle, flags and a view_mapped bit. Its Drop
does munmap, UNMAP and FREE.
Queue owns the ring, write_ptr, read_ptr, cwsr and
eop Buffers and the doorbell mapping. Its Drop sends DESTROY_QUEUE, munmaps the doorbell, and then lets
the Buffers drop in declaration order.
Event owns an event id and the event
page.
Kernel and Signal each hold one Buffer.
A Drop cannot fail. An error inside it prints to stderr
and the work goes on.
as_slice_mut borrows through the Buffer, so a slice
cannot outlive the memory behind it.
Every syscall returns Result<T, Error>. The Error carries
the call name and the errno, and prints as "call: message".
main prints it and exits 1.
Why this matters: a training run keeps a corpus of gigabytes
resident, and a GPU hang on this APU takes the desktop with
it. Ownership is the cheapest guard. It is the reason we chose Rust
over C.
Nesting gives the order for free. A Queue drops its own fields
first and then its Buffers, so DESTROY_QUEUE always runs before the
ring memory is freed.