of/irq: Document of_irq_init() device node refcount contract
PATCH 1/1 of/irq: Document of_irq_init() device node refcount contract
When a driver initialization function is successful, of_irq_init() keeps a refcount on the device node it has passed, which makes it safe for the driver to store a pointer to the device node and use it after the initialization function returns.
This behavior is currently not documented anywhere, and only assumed. Add a paragraph to the comment block preceding of_irq_init() to document the behavior and turn it into a contract.
It is worth noting that of_irq_init() did leak device node refcounts in the past, and this was addressed in multiple commits; most recently in commit 708124d9e6e7 ("of/irq: Fix device node refcount leakages in of_irq_init()"), which fixed the refcount leaks for the initialization failure path only. That is an indirect confirmation that keeping the refcount on the successful path is intentional.
--- drivers/of/irq.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/of/irq.c +9 -0
@@ -647,6 +647,15 @@ struct of_intc_desc {* * This function scans the device tree for matching interrupt controller nodes, * and calls their initialization functions in order with parents first. +* +* The initialization functions are called while holding a refcount on the +* device node corresponding to the device that is being initialized (passed +* as the first parameter). If an initialization function is successful, the +* device node refcount is *not* dropped (ever); this is intentional and +* guarantees that the pointer passed to the initialization function is valid +* not only while the function runs, but also for the rest of the kernel +* lifetime (i.e. it is safe for a driver to store the device node pointer +* and use it *after* the initialization function returns). */ void __init of_irq_init (const struct of_device_id *matches) {
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] of/irq: false API contract documentation encourages use-after-free --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] of/irq: false API contract documentation encourages use-after-free --
commit 553b68f6aa4fe4b4ead9f98bb63dfba3cbd9c9a9
Author: Radu Rendec <[email protected]>
of/irq: Document of_irq_init() device node refcount contract
This commit adds documentation to of_irq_init() claiming that the device node refcount is intentionally never dropped upon successful initialization.
Quoted 10 lines
drivers/of/irq.c +2
-0@@ -647,6 +647,15 @@ struct of_intc_desc {* * This function scans the device tree for matching interrupt controller nodes, * and calls their initialization functions in order with parents first. +* +* The initialization functions are called while holding a refcount on the
drivers/of/irq.c struct of_intc_desc { · line 661
Quoted 10 lines
drivers/of/irq.c +7
-0+* device node corresponding to the device that is being initialized (passed +* as the first parameter). If an initialization function is successful, the +* device node refcount is *not* dropped (ever); this is intentional and +* guarantees that the pointer passed to the initialization function is valid +* not only while the function runs, but also for the rest of the kernel +* lifetime (i.e. it is safe for a driver to store the device node pointer +* and use it *after* the initialization function returns). */ void __init of_irq_init (const struct of_device_id *matches) {
[Severity: Medium]
Does this documentation accurately reflect the code's behavior?
Looking at the end of of_irq_init() in drivers/of/irq.c, there is a cleanup loop that explicitly drops the device node refcounts for successfully initialized controllers remaining in the parent list:
list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
list_del(&desc->list);
of_node_put(desc->interrupt_parent);
of_node_put(desc->dev);
kfree(desc);
}Any successfully initialized leaf interrupt controller, and any sibling controllers remaining in intc_parent_list when intc_desc_list becomes empty, will have their references dropped by this final drain loop.
The documentation appears to mistake a pre-existing memory leak (where parent controllers popped during the while loop leak their references) for an intentional, universal API contract, while ignoring this final drain loop that cleans up the remaining nodes.
If driver authors rely on this newly documented contract, they might assume they do not need to take their own reference on the device node. Could this lead to a use-after-free when the final loop drops the reference?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 47 lines · On Fri, 2026-10-09 at 01:19 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] of/irq: false API contract documentation encourages use-after-free --commit
553b68f6aa4fe4b4ead9f98bb63dfba3cbd9c9a9
Author: Radu Rendec <[email protected]>of/irq: Document
of_irq_init()device node refcount contractThis commit adds documentation to
of_irq_init()claiming that the device node refcount is intentionally never dropped upon successful initialization.Quoted 10 lines
drivers/of/irq.c +2
-0@@ -647,6 +647,15 @@ struct of_intc_desc { * * This function scans the device tree for matching interrupt controller nodes, * and calls their initialization functions in order with parents first. + * + * The initialization functions are called while holding a refcount on thedrivers/of/irq.c struct of_intc_desc { · line 661
Quoted 10 lines
drivers/of/irq.c +7
-0+* device node corresponding to the device that is being initialized (passed +* as the first parameter). If an initialization function is successful, the +* device node refcount is *not* dropped (ever); this is intentional and +* guarantees that the pointer passed to the initialization function is valid +* not only while the function runs, but also for the rest of the kernel +* lifetime (i.e. it is safe for a driver to store the device node pointer +* and use it *after* the initialization function returns). */ void __init of_irq_init(const struct of_device_id *matches) {[Severity: Medium]
Does this documentation accurately reflect the code's behavior?Looking at the end of
of_irq_init()indrivers/of/irq.c, there is a cleanup loop that explicitly drops the device node refcounts for successfully initialized controllers remaining in the parent list:list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) { list_del(&desc->list); of_node_put(desc->interrupt_parent); of_node_put(desc->dev); kfree(desc); }
Quoted 3 lines
Any successfully initialized leaf interrupt controller, and any sibling controllers remaining in
intc_parent_listwhenintc_desc_listbecomes empty, will have their references dropped by this final drain loop.
This is correct. At the time when I sent the patch, commit 30724547b221 ("of/irq: Fix remaining refcount leaks in of_irq_init()") hadn't been merged yet (or it had very recently and I hadn't picked it up yet).
Sorry for the noise!
By the way, I manually re-added the email addresses I had used on the initial email. It looks like sashiko replies to a fixed list of addresses.
Quoted 8 lines
The documentation appears to mistake a pre-existing memory leak (where parent controllers popped during the while loop leak their references) for an intentional, universal API contract, while ignoring this final drain loop that cleans up the remaining nodes.
If driver authors rely on this newly documented contract, they might assume they do not need to take their own reference on the device node. Could this lead to a use-after-free when the final loop drops the reference?