Programming in C and shifting to Rust can be a hard challenge for an old-school
C programmer. Rust for Linux introduced a new programming paradigm to the Linux
Kernel and this means that C programmers like me need to shift our mindset.
I'll share my view on the matter after rewriting the VGEM DRM driver in Rust
during my Igalia Coding Experience in the summer: the view of a C programmer
and a beginner in Rust.
In this talk, I will discuss the challenges for a C programmer to write a Rust
kernel driver, addressing the use of the DRM bindings, developed by Asahi Lina,
the performance of the Rust driver, the benefits of the Rust features for
driver development, and the roadmap to convert a C DRM driver to a Rust DRM
driver.
(c) Linux Plumbers Conference 2023
15-15 Nov
Omni Richmond Hotel, Richmond, VA (US)
https://lpc.events/event/17/page/198-lpc-2023-overview
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Converting a DRM driver to Rust
1. Converting a DRM driver to Rust
Maíra Canal
Richmond, VA - LPC 2023
2. What is the VGEM driver?
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
VGEM (Virtual GEM provider) is a minimal non-hardware-backed GEM service.
It was written in C and introduced in 2015.
Fairly small driver (~400 lines): GEM service + 2 IOCTLs
Use case: no real GPU available in setups with QEMU and llvmpipe
3. Why are we rewriting VGEM?
Proof of Concept
It is a GPU-agnostic driver
It is a compact driver
Uses a lot of the DRM framework
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
4. What is rustgem?
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
Rustgem is a driver written in Rust with the exactly same functionality as VGEM
It was written using Asahi Lina's DRM bindings + RfL bindings
● Thanks RfL folks!
I wrote bindings for legacy platform device initialization and dma-resv
5. Managing unsafe code
● SAFETY review
● Q: How can we encourage SAFETY review inside the subsystems?
● Q: Can a beginner spot subtle safety issues?
Technical Hurdles
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
+ /// Returns the pointer to reservation object associated with this GEM object.
+ fn resv(&self) -> DmaResv {
+ // SAFETY: Every GEM object holds a reference to a reservation object
+ unsafe { DmaResv::from_raw(self.gem_obj().resv) }
+ }
6. How to write good safe abstractions?
● Rust For Linux: Writing Safe Abstractions & Drivers was a good resource for me
at that time
● Q: Maybe we could include more documentation about writing safe
abstractions?
Technical Hurdles
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
7. Problems with macro expansion
● Q: How can we make this easier?
Technical Hurdles
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
// include/uapi/drm/vgem_drm.h
#define DRM_IOCTL_VGEM_FENCE_ATTACH DRM_IOWR( DRM_COMMAND_BASE + DRM_VGEM_FENCE_ATTACH, struct drm_vgem_fence_attach)
#define DRM_IOCTL_VGEM_FENCE_SIGNAL DRM_IOW( DRM_COMMAND_BASE + DRM_VGEM_FENCE_SIGNAL, struct drm_vgem_fence_signal)
// include/uapi/drm/vgem_drm.h
/* Note: this is an enum so that it can be resolved by Rust bindgen. */
enum {
DRM_IOCTL_VGEM_FENCE_ATTACH = DRM_IOWR(DRM_COMMAND_BASE + DRM_VGEM_FENCE_ATTACH, struct drm_vgem_fence_attach),
DRM_IOCTL_VGEM_FENCE_SIGNAL = DRM_IOW(DRM_COMMAND_BASE + DRM_VGEM_FENCE_SIGNAL, struct drm_vgem_fence_signal),
};
8. We have the DRM bindings
We have the two upstreamable drivers (Asahi and rustgem)
Q: What could help us to upstream Rust for DRM?
Q: What is the next step?
Next step: Upstream
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
9. If we accept Rust in the DRM, it means that everyone is responsible for it
People writing bindings might need to touch C code
Rust has well-documented benefits that we might want as a community
If we want to see things moving forward, we need to compromise
Converting a DRM driver to Rust
Maíra Canal, LPC 2023
Next step: Upstream
10. Maybe we could include documentation about writing safe abstractions?
How can we encourage SAFETY review inside the subsystems?
How can we improve macro expansion?
What could help us to upstream Rust for DRM?
What is the next step to upstream Rust for DRM?
Discussion
Converting a DRM driver to Rust
Maíra Canal, LPC 2023