Linux kernel modules in Go

gokm builds Linux kernel modules from plain Go code, using TinyGo. Write filesystems, character devices, /proc files, network interfaces and block devices against Go packages: gokm supplies all the kernel-side code.

The Go gopher in a Tux costume
Experimentalgokm is experimental. Don’t use it in production: the modules it builds run inside the kernel, where a bug in the generated code or the Go runtime can crash the machine or corrupt data, and the API may change without notice.

Real Go

Goroutines, channels, select, sync, timers, fmt, maps, defer/recover and floating point all work inside the kernel.

Parallel

Kernel callbacks run Go on whatever CPU they arrive on, concurrently. Goroutines are kernel threads.

Device frameworks

Filesystems with mmap and xattrs, character devices with ioctl and poll, /proc files, network interfaces and block devices.

Any kernel API

gokm/linux generates bindings to any kernel function, struct or constant from the running kernel’s BTF.

Garbage collected

A growable, garbage-collected heap with a background collector. Kernel memory never holds Go pointers.

Built for the kernel

Retpolines, IBT, soft-float, the kernel code model and stack-depth analysis: modules are built with kbuild, like any other.

Hello, world#

package main

import "gokm/kernel"

func main() { // runs at insmod
	kernel.Print("hello from Go!")
	kernel.OnExit(func() { kernel.Print("goodbye from Go!") }) // runs at rmmod
}
gokm init hello          # creates hello/go.mod and hello/main.go
gokm build hello         # produces hello/hello.ko
sudo insmod hello/hello.ko && sudo rmmod hello && sudo dmesg | tail -2
# hello: hello from Go!
# hello: goodbye from Go!

The tutorial walks through it step by step.

A sibling project#

gopgext uses the same approach to build PostgreSQL extensions from Go: a custom TinyGo target and runtime layer for the host, a C runtime compiled by the host’s own build system (kbuild here, PGXS there), and generated glue.