Open-Source Wikis

/

Linux

/

Subsystems

/

Samples

torvalds/linux

Samples

Purpose

samples/ ships small, working examples of how to use various kernel features. They are the place to start when you want to see what an API looks like in practice without searching through driver code.

Most samples build as kernel modules; a few are user-space programs that exercise kernel features.

Notable subdirectories

samples/
├── bpf/             # Classic BPF example collection: xdp_*, sockex*, tracex*, sample_map*, …
├── ftrace/          # Function-trace examples
├── kfifo/           # kfifo (lock-free fifo) samples
├── kobject/         # /sys integration via kobject
├── kprobes/, uprobes/  # Probe-attach examples
├── livepatch/       # Sample livepatch modules
├── mei/, vfio-mdev/, vfio-pci/  # Device pass-through samples
├── pktgen/          # Packet generator scripts
├── rpmsg/           # Remote-processor messaging
├── rust/            # Rust sample modules (CONFIG_RUST=y to build)
├── seccomp/         # seccomp-bpf examples
├── trace_events/, trace_printk/ # Trace event examples
├── v4l/             # V4L2 (video for linux) example tools
├── watchdog/        # Watchdog driver sample
├── damon/           # DAMON sample modules
├── cgroup/          # cgroup-bpf sample
├── auxdisplay/, hidraw/, fanotify/, hw_breakpoint/, landlock/, pidfd/, qmi/, mount_attr/, fprobe/, futex/, page_pool/, tdx-guest/, accel/, bridge/, livepatch/, …
└── Kconfig, Makefile

(About 47 subdirectories total.)

Examples worth reading

  • samples/rust/ — small Rust kernel modules. Best place to see the module! macro, KBox/KVec, and current Rust idioms in action.
  • samples/bpf/ — classic eBPF samples. Note that the modern equivalents tend to live in tools/testing/selftests/bpf/ using libbpf; these samples are simpler and older.
  • samples/kprobes/ — minimal kprobe attach.
  • samples/livepatch/ — see how a livepatch is structured.
  • samples/seccomp/ — example BPF programs for seccomp-bpf.
  • samples/landlock/ — Landlock sandboxing in 30 lines.

Building

Each subdir is opt-in via CONFIG_SAMPLE_*. Enable in make menuconfig under "Sample kernel code" or:

make M=samples/<subdir>

Integration points

Samples are by definition leaf consumers; they don't export anything other consumers depend on.

Entry points for modification

  • New API showcase: add samples/<feature>/, register a Kconfig option, write a small module. Aim for ≤ 200 lines.
  • Keep samples small. Drivers belong under drivers/.
  • Tools — selftests and bpftool.
  • Rust supportsamples/rust/ is the canonical Rust starter set.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Samples – Linux wiki | Factory