kobject, kset, and Platform Match Order

Understand why the Linux kernel decouples hardware topology from driver code through bus_type, trace struct kobject and struct kset into sysfs, and prove the exact Linux 6.12 platform_match() cascade including exclusive driver_override and terminal id_table semantics.

15 minfree previewLinux 6.12C11U-Boot 2025.01→ module 1 lab
What you'll learn
  • Trace ordinary platform matching and its override exception.
  • Reject a name fallback after a present ID table misses.
  • Distinguish registered device identity from successful driver binding.

Why the Linux kernel Separates Hardware Topology from Driver Code

In bare-metal Cortex-A7 firmware (GL-E01/GL-E02) or early monolithic modules (GL-E04), it was tempting to hard-code #define CTRL_PHYS 0x0209C000 and #define CTRL_IRQ 41 directly inside module_init(). What breaks the instant your company ships a second board revision with two identical controllers at 0x0209C000 and 0x020A0000, or reuses the same IP block on another SoC? Hard-coded drivers duplicate source files per board, poke unpopulated silicon on boards that omit the peripheral, and leave /sys completely blind to power ordering and device relationships!

The Linux unified device model (drivers/base/) solves this permanently by splitting hardware support into three orthogonal objects connected across a struct bus_type (platform_bus_type for memory-mapped SoC peripherals that cannot self-enumerate like PCIe or USB):

  1. What hardware exists (struct platform_device, embedding struct device): Instantiated from Device Tree (of_platform_default_populate_init) or board code, carrying physical register ranges, interrupt specifiers, firmware compatible strings, and an optional driver_override.
  2. How software operates that IP (struct platform_driver, embedding struct device_driver): Pure hardware-agnostic code supplying .probe, .remove, .of_match_table, and .id_table—zero board addresses!
  3. Object identity & sysfs lifetime (struct kobject & struct kset): Every struct device embeds a struct kobject, which gives the kernel a uniform C base class providing a name, parent pointer (forming /sys/devices/... directory trees), atomic reference counting (struct kref via kobject_get() / kobject_put(), invoking ktype->release() only when the last reference drops to 0), and membership in a struct kset (devices_kset, governing KOBJ_ADD / KOBJ_BIND netlink uevent policy independently of parent hierarchy). Drivers embed struct device rather than allocating naked kobject instances for hardware.
fig_m01_l01_a.svg — Unified device model anatomy — embedded struct kobject…

Figure 1.1a. Unified device model anatomy — embedded struct drives /sys/devices identity and kref lifetime, governs uevents, and ./driver appears only after probe() returns 0.

mental model: A hospital surgical scheduler (platform_bus_type.match) pairs a booked operating suite (struct platform_device) with a credentialed surgical team (struct platform_driver): it checks an explicit administrative override (driver_override) first, exact board-certified procedure codes (Device Tree compatible, then ACPI) next, and a generic department name last—but if the team's published procedure table (id_table) exists and lacks the procedure, the scheduler stops immediately rather than falling back to matching surnames!

Where it stops working: Unlike human scheduling, kobject lifetime is governed by atomic kref reference counts whose release callback frees memory only at zero, and matching merely authorizes probe() without proving hardware readiness.

definition (kobject, kset & Linux 6.12 platform_match() Cascade): A kobject (kernel object) is the reference-counted identity and sysfs directory anchor embedded inside struct device, while a kset groups kobject instances under shared uevent notification policy (parent != kset). Inside drivers/base/platform.c, platform_match(dev, drv) evaluates a strict five-branch cascade: (1) if pdev->driver_override is non-NULL, return !strcmp(pdev->driver_override, drv->name) exclusively; (2) of_driver_match_device() (Device Tree); (3) acpi_driver_match_device() (ACPI); (4) if pdrv->id_table is non-NULL, return platform_match_id(pdrv->id_table, pdev) != NULL terminally (a miss returns 0 without testing names!); (5) otherwise compare strcmp(pdev->name, drv->name) == 0.

Step through the four-stage progression from device_add() registration to platform_match() and really_probe() binding below:

ast_m01_l01.svg — A present id_table terminates platform_match() even…step 1/4
  1. Step 1. Step 1: Register struct device and its embedded kobject under /sys/devices and emit KOBJ_ADD via devices_kset.
  2. Step 2. Step 2: Check exclusive driver_override first; otherwise evaluate Device Tree OF compatible before ACPI.
  3. Step 3. Step 3: If pdrv->id_table is non-NULL, return its match result terminally; fall back to drv->name ONLY if id_table is NULL.
  4. Step 4. Step 4: Only when probe() returns 0 does really_probe() publish the ./driver symlink confirming an active bind.

Figure 1.1b. A present id_table terminates platform_match() even when zero entries match, and matching only authorizes probe() rather than proving driver binding.

Proving All Seven Match Control-Flow Edges in ISO C11

Study the Linux kernel 6.12 logic in target_excerpt.c (const struct device_driver *drv signature), then inspect verify.c. Notice how a single happy-path Device Tree test on hardware can hide broken fallback logic! In verify.c, match() returns distinct branch tags (1 OF, 2 ACPI, 3 ID-table hit, 4 name fallback, 5 allowed override, 0 unbound) and exercises all seven discriminating edges: OF beating simultaneous ACPI/ID/name (1), ACPI beating ID/name (2), id_table hit (3), present id_table miss returning 0 even though name == true, absent id_table allowing name match (4), denied driver_override blocking an otherwise-matching OF device (0), and allowed driver_override succeeding with all other tables false (5).

learner@ksa: ~/gl-e05/m01-l01
learner@ksa:~/gl-e05/m01-l01$ bat -H 5 -H 6 -H 11 -H 12 target_excerpt.c File: target_excerpt.c    static int match_excerpt(struct device *dev,                             const struct device_driver *drv)    {        struct platform_device *pdev = to_platform_device(dev);      struct platform_driver *pdrv = to_platform_driver(drv);        if (pdev->driver_override)            return !strcmp(pdev->driver_override, drv->name);        if (of_driver_match_device(dev, drv))            return 1;        if (acpi_driver_match_device(dev, drv))          return 1;        if (pdrv->id_table)            return platform_match_id(pdrv->id_table, pdev) != NULL;        return !strcmp(pdev->name, drv->name);    }
  1. driver_override is evaluated before OF/ACPI and accepts ONLY the named driver.
  2. When pdrv->id_table is non-NULL, platform_match returns the table lookup directly without testing pdev->name.
learner@ksa: ~/gl-e05/m01-l01
learner@ksa:~/gl-e05/m01-l01$ bat -H 7 -H 10 -H 17 -H 18 -H 19 verify.c File: verify.c    #include <assert.h>    #include <stdbool.h>    #include <stdio.h>    static int match(bool override, bool allowed, bool of, bool acpi,                     bool has_ids, bool id, bool name)    {      if (override) return allowed ? 5 : 0;        if (of) return 1;        if (acpi) return 2;      if (has_ids) return id ? 3 : 0;        return name ? 4 : 0;    }    int main(void)    {        assert(match(false, false, true, true, true, true, true) == 1);        assert(match(false, false, false, true, true, true, true) == 2);      assert(match(false, false, false, false, true, true, true) == 3);        assert(match(false, false, false, false, true, false, true) == 0);        assert(match(false, false, false, false, false, false, true) == 4);        assert(match(true, false, true, true, true, true, true) == 0);        assert(match(true, true, false, false, false, false, false) == 5);        puts("match_cases=7 id_table_miss=unbound override=exclusive");        return 0;    }
  1. Exclusive override branch: returns 5 if allowed, else 0 even when OF/ACPI/ID/name are all true.
  2. Terminal id_table gate: has_ids == true returns 3 on hit or 0 on miss, never falling through to line 11.
  3. Line 17 proves has_ids=true, id=false, name=true evaluates to 0 (unbound), contrasted with has_ids=false on line 18 returning 4.

Compiling and Fault-Injecting the Match Verifier

Compile verify.c under strict ISO C11 flags (-std=c11 -Wall -Wextra -Werror) and verify all seven assertions pass. To see the terminal id_table bug caught red-handed, temporarily mutate if (has_ids) return id ? 3 : 0; into if (has_ids && id) return 3; and re-run: assertion 4 (match(false, false, false, false, true, false, true) == 0) aborts immediately because control wrongly leaks into the name fallback! Remember what this host binary proves and what it does not: strace below records user-space ELF loading and write(1, ...), whereas verifying actual DTB compatible strings and /sys/bus/platform/devices/.../driver symlinks belongs on the booted ARMv7-A target.

learner@ksa: ~/gl-e05/m01-l01
learner@ksa:~/gl-e05/m01-l01gcc -std=c11 -Wall -Wextra -Werror verify.c -o verifylearner@ksa:~/gl-e05/m01-l01./verifymatch_cases=7 id_table_miss=unbound override=exclusivelearner@ksa:~/gl-e05/m01-l01echo $?0learner@ksa:~/gl-e05/m01-l01
strace — learner@ksa: ~/gl-e05/m01-l01
learner@ksa:~/gl-e05/m01-l01strace -e trace=openat,read,write,close ./verifyopenat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3close(3)                                = 0openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\243\2\0\0\0\0\0"..., 832) = 832close(3)                                = 0write(1, "match_cases=7 id_table_miss=unbo"..., 55) = 55+++ exited with 0 +++

Separating Registration, Matching, Binding, and kref Release

Keep three lifecycle transitions strictly separate during bring-up. First, device_add() registers the embedded kobject in /sys/devices/... and broadcasts KOBJ_ADD through devices_kset—so seeing a sysfs directory (or a /sys/class symlink pointing to it) never implies a driver bound! Second, platform_match() merely answers whether (dev, drv) is an authorized pair; it touches zero hardware registers. Third, really_probe() invokes drv->probe(pdev): if probe() fails or defers (-EPROBE_DEFER), the device stays registered in /sys/devices with no ./driver symlink so a later module load or supplier retry can bind cleanly. Finally, never call kfree() directly on an initialized struct device—kobject_put() decrements kref and runs the subsystem's .release() callback only when the final holder drops its reference.

Common Production Pitfalls

warning: Symptom: A board device pdev->name = "ksa-ctrl" and driver drv->name = "ksa-ctrl" are both loaded (ls /sys/bus/platform/devices and ls /sys/bus/platform/drivers show both), yet probe() never executes. Root Cause: pdrv->id_table points to a non-NULL platform_device_id array lacking "ksa-ctrl", and a non-NULL id_table terminates platform_match() without falling back to drv->name. Fix: Align the Device Tree compatible / of_match_table (or add the exact entry to id_table with MODULE_DEVICE_TABLE) rather than expecting a name fallback.

warning: Symptom: Updating a driver's of_match_table to match a Device Tree node's compatible string still fails to bind on a lab target after an earlier vfio or debug test script ran. Root Cause: /sys/bus/platform/devices/<dev>/driver_override still holds an active override string, which platform_match() checks before of_driver_match_device() and treats as exclusive. Fix: Inspect cat driver_override and clear an unintended override (echo > driver_override) only when restoring normal Device Tree matching.

Practice & Retrieval

Guided Practice: Tracing Conflicting Match Signals

Suppose a platform device carries driver_override = NULL, a Device Tree compatible = "ksa,ctrl-v1" matching drv->of_match_table, a non-NULL pdrv->id_table that misses, and pdev->name == drv->name. Trace match_excerpt() from top to bottom: (1) which branch returns, and why doesn't the missing id_table block probe() here (of_driver_match_device succeeds at Gate 2 before Gate 4 is reached!); (2) what changes if of_node is absent (NULL) while pdrv->id_table remains non-NULL and misses (Gate 4 returns 0, blocking Gate 5!).

Independent Practice: Sysfs Binding & Override Auditor

Copy verify.c to a scratch file binding_audit.c modeling a struct dev_state { bool registered; int match_branch; int probe_errno; } helper where is_visible_in_sysfs() depends solely on registered while has_driver_symlink() requires registered && match_branch > 0 && probe_errno == 0. Assert both (registered=true, match=1, probe_errno=-517) -> sysfs=true, bound=false and (override=true, allowed=false, of=true) -> match=0, bound=false under gcc -std=c11 -Wall -Wextra -Werror, and note which target commands (readlink -f /sys/bus/platform/devices/.../driver and cat modalias) verify binding on ARMv7-A.

quiz · 1 of 3

On a Linux 6.12 platform bus with pdev->driver_override == NULL, both of_driver_match_device() and strcmp(pdev->name, drv->name) == 0 would match. Which branch decides platform_match()?

quiz · 2 of 3

A platform_driver defines a non-NULL pdrv->id_table that contains no matching entry for pdev, OF/ACPI do not match, and pdev->name equals drv->name. What does platform_match() return?

quiz · 3 of 3

A device directory exists under /sys/devices/platform/soc/... (and is referenced by a symlink), yet drv->probe() returned -EINVAL. What is the resulting sysfs state?

Key takeaways

  • The Linux kernel unified device model separates hardware descriptions (struct platform_device), hardware-agnostic code (struct platform_driver), and reference-counted sysfs identity/uevent infrastructure (struct kobject and struct kset).
  • platform_match() checks pdev->driver_override exclusively before ordinary matching; in the ordinary path, OF (Device Tree) precedes ACPI, a present pdrv->id_table is terminal (blocking name fallback on a miss), and strcmp(pdev->name, drv->name) runs only when id_table == NULL.
  • Class and bus entries are symlinks to the canonical /sys/devices/... directory (readlink -f); seeing the device directory proves registration, whereas only ./driver proves probe() returned 0.
  • Later: Lesson m01-l02 enters platform_driver.probe() to reserve inclusive struct resource MMIO windows, decode ERR_PTR mappings, translate GIC interrupt indices, and design teardown for void (*remove)(pdev).
  • The OF branch returns before the name branch is considered.
  • The registered device can remain visible without successful binding.
  • Return the error before requesting or enabling that interrupt.
Progress is saved on this device.

search — fzf

0/0 ───────────────────────────────────────────

keyboard shortcuts — less

/
search
f
focus mode
[
previous
]
next
?
help
Esc
close

figure