device_add(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– device_add(9) β€” the kernel API manual, but make it cute πŸŽ€

okay so this is device_add(9) from the Kernel API Manual, and bestie, it's giving "part two of a two-part saga" energy and I am obsessed. Let's get into it ✨


🌸 NAME 🌸

device_add β€” add device to device hierarchy. that's it, that's the vibe πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

int device_add (struct device *dev );

yes she returns an int, yes she takes a struct device *, no we are not scared 🫢


πŸŽ€ ARGUMENTS πŸŽ€

  • dev β€” device. literally just "device." the original said it in three letters and honestly? iconic minimalism πŸ’€

✨ DESCRIPTION ✨

so this is part 2 of device_register, though it may be called separately β€” and I say may because it's only valid _iff_ device_initialize has been called separately first. read that again, bestie, because the italics are doing the heavy lifting πŸ™ˆ

what it actually does: it adds dev to the kobject hierarchy via kobject_add, adds it to the global and sibling lists for the device, and then adds it to the other relevant subsystems of the driver model. like a glow-up in three phases, no cap πŸ”₯

now the serious part, and I need you to lock in:

Do not call this routine or device_register more than once for any device structure. The driver model core is not designed to work with devices that get unregistered and then spring back to life. (Among other things, it's very hard to guarantee that all references to the previous incarnation of dev have been dropped.)

so no resurrecting devices, bestie. that's not main character energy, that's undefined behaviour energy πŸ’€ instead: allocate and register a fresh new struct device. new device, new you, periodt 🌈


πŸ’€ NOTE πŸ’€

_Never_ directly free dev after calling this function β€” even if it returned an error! Always use put_device to give up your reference instead. this is the number one rule and I will not be negotiating πŸ™…β€β™€οΈ

rule of thumb, and I'm quoting the energy here:

  • if device_add succeeds, you should call device_del when you want to get rid of it πŸ‘‹
  • if device_add has not succeeded, use only put_device to drop the reference count 🫢

that's it. that's the whole flowchart. screenshot it, put it on your wall πŸŽ€


πŸ” SEE ALSO πŸ”

Kernel file ./drivers/base/core.c, plus the whole extended friend group:


and that's the page, bestie 🫢 this lil drop came out of ./drivers/base, dated October 2026, and it's filed under device_add(9) β€” go forth, add your devices exactly once, and slay πŸ’…βœ¨πŸ‘‘

πŸ“¦ where this page comes from