Repository navigation
Tutorial porting a Linux API
Part of the system development guide.
This walks through one real piece of work from start to finish: making
Linux's current and struct task_struct work on NetBSD (done for the
v7.3 DRM import). The same steps apply to any API the DRM code needs.
Placeholders: $SRC is the source tree, $TOOLS the build.sh tool
directory, $OBJ the object directory, $LINUX a Linux git checkout.
Until DRM builds through the kernel build, the compile probe
(tools/probe in sys/external/bsd/drm2, local only) syntax-checks
every DRM source with the kernel's flags and ranks the first error of
each file:
cd $SRC/sys/external/bsd/drm2
awk '/^file/ {print $2}' drm/files.drmkms > /tmp/core.txt
tools/probe -l /tmp/core.txt -o /tmp/probe $TOOLS $OBJ/drm-probe-kernelSeveral files stopped on current->comm, current->group_leader,
task_tgid(): the task API. Before designing anything, measure what
the code really uses — fields and functions, and how often:
cat /tmp/core.txt | xargs grep -ho 'current->[a-z_]*\|task_[a-z_]*(\|pid_task(\|get_pid\|put_pid' |
sort | uniq -c | sort -rnThen read each use in context (grep -n the same patterns). Here the
uses fell into three groups: identity and lifetime (group_leader,
struct pid kept per open file under RCU, pid_task()), logging
(comm, task_pid_nr()), and exit detection (PF_EXITING,
exit_code). That is the requirement.
Three sources, in this order:
-
Linux documentation and kernel-doc comments. For GPL files, read only the comments, through the comment filter (kept outside the tree):
git -C $LINUX show v7.3.7:include/linux/sched.h | gpl-comments -d -s | less
-dskips comments inside function bodies,-sadds the declaration line each comment documents. -
MIT-licensed callers in
dist/, which show how the API is used. -
FreeBSD's LinuxKPI (BSD-2, may be read and ported), here
linux_current.c: one task per thread, made on first use.
Never read GPL function bodies, and never translate GPL code.
This is where most of the thinking goes. Questions and answers for this API (details in threads):
-
Where can per-thread data live? LWP specificdata
(specificdata(9)). Reading it (
lwp_getspecific) takes no lock; setting it may sleep. So a task can only be created where sleeping is allowed; everything else must cope (a placeholder task). -
What represents the process? Proc specificdata holds a group
leader task and the
struct pid. Its destructor runs inexit1(); readkern_exit.cto learn when (after files are closed, after the exit signal is recorded) — that decides what close-at-exit code can see. - How do lookups under RCU stay safe? Free tasks and pids after an RCU grace period; refcount them.
-
What else used the old definition?
grepthe compat layer: the kthread code had a privatestruct task_struct, which became state hung off the real task.
Read the NetBSD sources you depend on and write down what you learned in the source map as you go.
-
Headers in
sys/external/bsd/drm2/include/linux/(sched.h,pid.h): Linux's field and function names; functions renamed tolinux_*with#defines so they cannot clash with NetBSD symbols. -
The implementation in
sys/external/bsd/drm2/linux/linux_sched.c, a BSD licence header, and a comment at the top explaining the design and its limits. -
Register it:
linux/files.drmkms_linux(kernel builds),sys/modules/drmkms_linux/Makefile(the module), init/fini inlinux_module.c. -
Build the module to see warnings with the real flags:
cd $SRC/sys/modules/drmkms_linux && $TOOLS/bin/nbmake-amd64 -j16 dependall
Write kernel-side tests in tests/dev/drm/liblinux/ (one function per
case, returning NULL or a message; listed in drm_linux_tests.h) and
a small ATF program calling them (tests/dev/drm/t_linux_task). Test
the semantics that matter, not just that it compiles: here, that a pid
outlives its process (create a process in the rump kernel, take a pid
reference, let the process exit, check pid_task() answers NULL).
Run them, fastest loop first:
- Rump kernel on the build host — seconds; also under AddressSanitizer. See testing.
- The dev VM — the real NetBSD userland, seconds per cycle once installed. See testing.
- The full system test run — the final check before pushing.
Expect the first failures to be about the test environment rather than
your code: in this port, rump's kpreempt_disabled() always returning
true and its cached curlwp both made correct code fail. Read the
rump pitfalls first. Concurrency tests
must be run hundreds of times.
Run the probe again on the files from step 1: the task-API errors should be gone and the files should stop later (or compile). Record the numbers in the commit message.
-
Stage files by name and check for build products and local details before committing:
git diff --cached --name-only | grep -E '\.(o|d|a|so)$|\.depend|\.gdbinit' git diff --cached | grep '^+' | grep -E '/home/|192\.168'
-
The message explains what changed and why, the design's limits, bugs found on the way (and whether they are on
trunk, to report upstream), the tests, and the probe numbers. -
Update the guide in the same session: the design notes in the compat layer, concepts in Part III, facts in the source map.
drm_writeback.h is GPL-only upstream, and the NetBSD replacement in
drm2/include/drm/ hid the 7.3 version (see
shadowing headers). The steps:
list the structure members and functions MIT callers use (amdgpu's
display code, drm_atomic*.c); read the GPL header's kernel-doc
through the filter for member types and meanings; take argument orders
from MIT call sites; write the declarations; implement what a GPL-free
kernel can honestly do (here: creating a writeback connector fails
with -ENODEV, and the job functions assert they are unreachable —
see the stub policy); probe the
callers again.
aiBSD system development guide
Tutorial
II — Building
III — The kernel
- Kernel basics
- Booting and kernel modules
- Threads, processes, tasks
- Devices and drivers
- Files, descriptors, device nodes
- PCI
- Virtual memory
- Memory and DMA
- Page tables, TLBs, memory types
- Firmware
- State and events
- The console
- Power management
IV — Linux compat layer
V — DRM
VI — Testing
VII — Upstreams
Appendices