bus_for_each_dev(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… bus_for_each_dev(9) β€” like, the device iterator bestie

okay so this is a page straight out of the Kernel API Manual (bus_for_each_dev(9), yes that's the whole vibe) and it's literally about walking a bus's device list and doing stuff to every single one. no cap, it's a for-each loop but make it kernel ✨


🌸 SYNOPSIS 🌸

int bus_for_each_dev (const struct bus_type *bus , struct device *start
, void *data , device_iter_t fn );

that's the whole signature, character for character, don't come for me 🫢


πŸŽ€ ARGUMENTS πŸŽ€

  • bus β€” the bus type. like, which bus we're strolling through πŸ’–
  • start β€” the device you wanna start iterating from. your starting point, your main character device πŸ‘‘
  • data β€” the data handed off to the callback. lil care package for fn 🎁 (well, the page calls it data, and data it is)
  • fn β€” the function that gets called for each device. one call per device, no skips πŸ¦‹

✨ DESCRIPTION ✨

so here's the tea: this thing iterates over bus's list of devices, and it calls fn for each one, passing it data. that's it, that's the loop πŸ’…

and if start is not NULL? we use that device to begin iterating from β€” like picking up the convo mid-scroll πŸ™ˆ

every single time, we check the return of fn. and if it returns anything other than 0, we break out and return that value. periodt. non-zero means we're done, bestie, we're leaving πŸƒβ€β™€οΈπŸ’¨


πŸ’€ NOTE πŸ’€

listen, this part is important and also a lil scary: the device that returns a non-zero value is not retained in any way, and its refcount is not incremented. like, at all. zero. nada 😭

so if the caller needs to retain this data, it should do so β€” and increment the reference count in the supplied callback. that's on you, not on us 🫢 don't forget, fr fr.


πŸ”₯ SEE ALSO πŸ”₯

kernel file ./drivers/base/bus.c plus the whole friend group:

bus_to_subsys(9), bus_find_device(9), bus_for_each_drv(9), bus_add_device(9), bus_probe_device(9), bus_remove_device(9), bus_add_driver(9), bus_remove_driver(9), bus_rescan_devices(9), device_reprobe(9), bus_register(9), bus_unregister(9), subsys_dev_iter_init(9), subsys_dev_iter_next(9), subsys_dev_iter_exit(9), subsys_system_register(9), subsys_virtual_register(9), driver_find(9), bus_get_dev_root(9)

all iconic, all worth a read, iykyk 🌈⭐


and that's the page!! brought to you by the fine folks over at ./drivers/base, dated October 2026, page bus_for_each_dev(9) β€” mwah, go iterate something πŸ’œπŸ§šβ€β™€οΈ

πŸ“¦ where this page comes from