<div dir="ltr"><div>Hi,</div><div><br></div><div>This is our initial proposal for explicit fences everywhere and new memory management that doesn't use BO fences. It's a redesign of how Linux graphics drivers work, and it can coexist with what we have now.<br></div><div><br></div><div><br></div><div><b>1. Introduction</b><br></div><div>(skip this if you are already sold on explicit fences)</div><div><br></div><div></div><div>The current Linux graphics architecture was initially designed for GPUs with only one graphics queue where everything was executed in the submission order and per-BO fences were used for memory management and CPU-GPU synchronization, not GPU-GPU synchronization. Later, multiple queues were added on top, which required the introduction of implicit GPU-GPU synchronization between queues of different processes using per-BO fences. Recently, even parallel execution within one queue was enabled where a command buffer starts draws and compute shaders, but doesn't wait for them, enabling parallelism between back-to-back command buffers. Modesetting also uses per-BO fences for scheduling flips. Our GPU scheduler was created to enable all those use cases, and it's the only reason why the scheduler exists.<br></div><div><br></div><div>The GPU scheduler, implicit synchronization, BO-fence-based memory management, and the tracking of per-BO fences increase CPU overhead and latency, and reduce parallelism. There is a desire to replace all of them with something much simpler. Below is how we could do it.<br></div><div><br></div><div><br></div><div><b>2. Explicit synchronization for window systems and modesetting</b><br></div><div><br></div><div>The producer is an application and the consumer is a compositor or a modesetting driver.<br></div><div><br></div><div><b>2.1. The Present request</b><br></div><div><br></div><div>As part of the Present request, the producer will pass 2 fences (sync objects) to the consumer alongside the presented DMABUF BO:</div><div>- The submit fence: Initially unsignalled, it will be signalled when the producer has finished drawing into the presented buffer.<br></div><div>- The return fence: Initially unsignalled, it will be signalled when the consumer has finished using the presented buffer.</div><div><br></div><div>Deadlock mitigation to recover from segfaults:</div><div>- The kernel knows which process is obliged to signal which fence. This information is part of the Present request and supplied by userspace.<br></div><div>- If the producer crashes, the kernel signals the submit fence, so that the consumer can make forward progress.</div><div>- If the consumer crashes, the kernel signals the return fence, so that the producer can reclaim the buffer.</div><div>- A GPU hang signals all fences. Other deadlocks will be handled like GPU hangs.<br></div><div><br></div><div>Other window system requests can follow the same idea.<br></div><div><br></div><div><div>Merged fences where one fence object contains multiple fences will be supported. A merged fence is signalled only when its fences are signalled. The consumer will have the option to redefine the unsignalled return fence to a merged fence.<br></div></div><div><b><br></b></div><div><b>2.2. Modesetting</b><br></div><div><br></div><div>Since a modesetting driver can also be the consumer, the present ioctl will contain a submit fence and a return fence too. One small problem with this is that userspace can hang the modesetting
driver, but in theory, any later present ioctl can override the previous one, so the unsignalled presentation is never used.<br></div><div></div><div><br></div><div><br></div><div><b>3. New memory management</b></div><div><br></div><div>The per-BO fences will be removed and the kernel will not know which buffers are busy. This will reduce CPU overhead and latency. The kernel will not need per-BO fences with explicit synchronization, so we just need to remove their last user: buffer evictions. It also resolves the current OOM deadlock.<br></div><div><br></div><div><b>3.1. Evictions</b><br></div><div><br></div><div>If the kernel wants to move a buffer, it will have to wait for everything to go idle, halt all userspace command submissions, move the buffer, and resume everything. This is not expected to happen when memory is not exhausted. Other more efficient ways of synchronization are also possible (e.g. sync only one process), but are not discussed here.<br></div><div><br></div><div><b>3.2. Per-process VRAM usage quota</b><br></div><div><br></div><div>Each process can optionally and periodically query its VRAM usage quota and change domains of its buffers to obey that quota. For example, a process allocated 2 GB of buffers in VRAM, but the kernel decreased the quota to 1 GB. The process can change the domains of the least important buffers to GTT to get the best outcome for itself. If the process doesn't do it, the kernel will choose which buffers to evict at random. (thanks to Christian Koenig for this idea)<br></div><div><br></div><div><b>3.3. Buffer destruction without per-BO fences</b><br></div><div><br></div><div>When the buffer destroy ioctl is called, an optional fence list can be passed to the kernel to indicate when it's safe to deallocate the buffer. If the fence list is empty, the buffer will be deallocated immediately. Shared buffers will be handled by merging fence lists from all processes that destroy them. Mitigation of malicious behavior:</div><div>- If userspace destroys a busy buffer, it will get a GPU page fault.</div><div>- If userspace sends fences that never signal, the kernel will have a timeout period and then will proceed to deallocate the buffer anyway.</div><div><br></div><div><b>3.4. Other notes on MM</b><br></div><div><br></div><div><div>Overcommitment of GPU-accessible memory will cause an allocation failure or invoke the OOM killer. Evictions to GPU-inaccessible memory might not be supported.<br></div></div><div><br></div><div><div>Kernel drivers could move to this new memory management today. Only buffer residency and evictions would stop using per-BO fences.<br><b></b></div></div><div><br></div><div><br></div><div><b>4. Deprecating implicit synchronization<br></b></div><div></div><div><div><br></div><div>It can be phased out by introducing a new generation of hardware where the driver doesn't add support for it (like a driver fork would do), assuming userspace has all the changes for explicit synchronization. This could potentially create an isolated part of the kernel DRM where all drivers only support explicit synchronization.<br></div></div><div></div><div><br></div><div></div><div></div><div></div><div>Marek<br></div></div>