bus_for_each_dev(9)
π 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 forfnπ (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
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms