Skip to content

Tutorial porting a Linux API

ebenali edited this page Dec 1, 2026 · 1 revision

Tutorial: porting a Linux API to the compat layer

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.

1. Find the gap and rank it

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-kernel

Several 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 -rn

Then 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.

2. Learn the semantics, cleanly

Three sources, in this order:

  1. 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

    -d skips comments inside function bodies, -s adds the declaration line each comment documents.

  2. MIT-licensed callers in dist/, which show how the API is used.

  3. 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.

3. Map it onto NetBSD

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 in exit1(); read kern_exit.c to 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? grep the compat layer: the kthread code had a private struct 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.

4. Implement

  • Headers in sys/external/bsd/drm2/include/linux/ (sched.h, pid.h): Linux's field and function names; functions renamed to linux_* 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 in linux_module.c.

  • Build the module to see warnings with the real flags:

    cd $SRC/sys/modules/drmkms_linux && $TOOLS/bin/nbmake-amd64 -j16 dependall

5. Test

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:

  1. Rump kernel on the build host — seconds; also under AddressSanitizer. See testing.
  2. The dev VM — the real NetBSD userland, seconds per cycle once installed. See testing.
  3. 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.

6. Check the result against the gap

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.

7. Commit

  • 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.

A second, shorter example: a GPL-shadowed header

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.

Clone this wiki locally