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.
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):
- What hardware exists (
struct platform_device, embeddingstruct device): Instantiated fromDevice Tree(of_platform_default_populate_init) or board code, carrying physical register ranges, interrupt specifiers, firmwarecompatiblestrings, and an optionaldriver_override. - How software operates that IP (
struct platform_driver, embeddingstruct device_driver): Pure hardware-agnostic code supplying.probe,.remove,.of_match_table, and.id_table—zero board addresses! - Object identity & sysfs lifetime (
struct kobject&struct kset): Everystruct deviceembeds astruct kobject, which gives the kernel a uniform C base class providing a name,parentpointer (forming/sys/devices/...directory trees), atomic reference counting (struct krefviakobject_get()/kobject_put(), invokingktype->release()only when the last reference drops to0), and membership in astruct kset(devices_kset, governingKOBJ_ADD/KOBJ_BINDnetlinkueventpolicy independently ofparenthierarchy). Drivers embedstruct devicerather than allocating nakedkobjectinstances for hardware.
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:
- Step 1. Step 1: Register struct device and its embedded kobject under /sys/devices and emit KOBJ_ADD via devices_kset.
- Step 2. Step 2: Check exclusive driver_override first; otherwise evaluate Device Tree OF compatible before ACPI.
- 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.
- 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.
Tracing the Five-Stage platform_match Cascade and Sysfs Symlinks
Why do engineers stare at pdev->name = "ksa-sensor" and drv->name = "ksa-sensor" in code review yet see zero probe() calls at boot? Trace Figure 1.1b line by line! Many developers misremember platform matching as *"try OF, else try ACPI, else try id_table, else try name."* In the Linux kernel 6.12 (drivers/base/platform.c), stage 4 (if (pdrv->id_table) return platform_match_id(...) != NULL;) is an unconditional early return, not an if (match) return 1;! If a driver defines a non-NULL id_table (perhaps left over from "ksa-sensor-v2") and the entry misses, platform_match() returns 0 immediately—never reaching strcmp(pdev->name, drv->name)! Similarly, if a test script wrote an active string to /sys/bus/platform/devices/.../driver_override, stage 1 short-circuits before Device Tree compatible matching even runs.
Figure 1.1b. Exact Linux 6.12 platform_match() control flow — both driver_override (Gate 1) and a non-NULL id_table (Gate 4) are terminal decisions.
When diagnosing missing binds on our ARMv7-A Cortex-A7 target, always start from the canonical node under /sys/devices and resolve symlinks with readlink -f. Entries under /sys/class/... group interfaces functionally—they do not create second hardware instances—and a device directory remaining visible in /sys/devices/platform/... proves only registration, whereas the ./driver symlink proves successful binding (probe() == 0). Also remember on 32-bit Cortex-A7 (sizeof(void *) == 4) never to conflate CPU pointer width with physical resource or DMA widths (resource_size_t / dma_addr_t).
platform_match(9) GL-E05 Quick Reference platform_match(9) NAME platform_match - pair a platform_device with a candidate platform_driver CASCADE (Linux 6.12 drivers/base/platform.c) 1. pdev->driver_override != NULL -> !strcmp(driver_override, drv->name) 2. of_driver_match_device() -> 1 on Device Tree compatible hit 3. acpi_driver_match_device() -> 1 on ACPI firmware table hit 4. pdrv->id_table != NULL -> platform_match_id() != NULL (TERMINAL) 5. fallback (id_table == NULL) -> !strcmp(pdev->name, drv->name) SYSFS INVARIANT /sys/devices/... proves registration; ./driver symlink proves probe() == 0. Kernel Systems Academy Lesson 1.1 platform_match(9) GL-E05 quick reference · full manual page: man 9 platform_match | Platform Match Branch | Trigger Condition | Miss Behavior | Production Diagnostic Signal |
|---|---|---|---|
pdev->driver_override | Non-NULL override string set | Terminal 0 (blocks OF/ACPI/ID/name) | cat .../driver_override shows non-(null) |
of_driver_match_device | dev->of_node + drv->of_match_table | Falls through to ACPI -> id_table | Compare of_node/compatible vs modinfo |
acpi_driver_match_device | ACPI companion + acpi_match_table | Falls through to pdrv->id_table | cat .../modalias (acpi:... prefix) |
pdrv->id_table | pdrv->id_table != NULL | Terminal 0 (blocks drv->name!) | Equal pdev->name/drv->name never probes |
strcmp(pdev->name, drv->name) | Reached only if id_table == NULL | Returns 0 (candidate rejected) | Legacy board-file fallback only |
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$ 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); }- driver_override is evaluated before OF/ACPI and accepts ONLY the named driver.
- When pdrv->id_table is non-NULL, platform_match returns the table lookup directly without testing pdev->name.
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; }- Exclusive override branch: returns 5 if allowed, else 0 even when OF/ACPI/ID/name are all true.
- Terminal id_table gate: has_ids == true returns 3 on hit or 0 on miss, never falling through to line 11.
- 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-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-l01learner@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.
Key takeaways
- The
Linux kernelunified device model separates hardware descriptions (struct platform_device), hardware-agnostic code (struct platform_driver), and reference-countedsysfsidentity/uevent infrastructure (struct kobjectandstruct kset). platform_match()checkspdev->driver_overrideexclusively before ordinary matching; in the ordinary path,OF(Device Tree) precedesACPI, a presentpdrv->id_tableis terminal (blockingnamefallback on a miss), andstrcmp(pdev->name, drv->name)runs only whenid_table == NULL.- Class and bus entries are symlinks to the canonical
/sys/devices/...directory (readlink -f); seeing the device directory proves registration, whereas only./driverprovesprobe()returned0. - Later: Lesson
m01-l02entersplatform_driver.probe()to reserve inclusivestruct resourceMMIO windows, decodeERR_PTRmappings, translateGICinterrupt indices, and design teardown forvoid (*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.