devres: Freeing the drs after all release() are called
PATCH 1/1 devres: Freeing the drs after all release() are called
In release_nodes(), it will call dr->node.release() and kfree dr one by one.
But sometimes the previous dr maybe be used by next .release(), such as:
[50314.855534] [<c12b172f>] ? synchronize_irq+0x3f/0xb0 [50314.861193] [<c12b18e9>] __free_irq+0x149/0x200 [50314.866367] [<c12b19e3>] free_irq+0x43/0xa0 [50314.871152] [<c12b4864>] devm_irq_release+0x14/0x20 [50314.876713] [<c169e806>] release_nodes+0x136/0x1b0 [50314.882178] [<c169ee79>] devres_release_all+0x39/0x60 [50314.887935] [<c169b411>] __device_release_driver+0x71/0xd0
the free_irq() will sync the last irq handling, which maybe use freed dr, then it will cause memory corruption.
Here split the dr kfreeing actions after all dr->node.release().
--- drivers/base/devres.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/base/devres.c +3 -1
@@ -490,9 +490,11 @@ static int release_nodes(struct device *dev, struct list_head *first,list_for_each_entry_safe_reverse (dr, tmp, &todo, node.entry) {devres_log (dev, &dr->node,"REL" ); dr->node.release (dev, dr->data); -kfree (dr); } +list_for_each_entry_safe_reverse (dr, tmp, &todo, node.entry) +kfree (dr); +return cnt; }
Signature · 1 line
1.7.9.5
Greg KH
maintainerQuoted 14 lines · Chuansheng Liu
In
release_nodes(), it will calldr->node.release()and kfree dr one by one.But sometimes the previous dr maybe be used by next .release(), such as:
[50314.855534] [<c12b172f>] ? synchronize_irq+0x3f/0xb0 [50314.861193] [<c12b18e9>] __free_irq+0x149/0x200 [50314.866367] [<c12b19e3>] free_irq+0x43/0xa0 [50314.871152] [<c12b4864>] devm_irq_release+0x14/0x20 [50314.876713] [<c169e806>] release_nodes+0x136/0x1b0 [50314.882178] [<c169ee79>] devres_release_all+0x39/0x60 [50314.887935] [<c169b411>] __device_release_driver+0x71/0xd0
Quoted 4 lines
the
free_irq()will sync the last irq handling, which maybe use freed dr, then it will cause memory corruption.Here split the dr kfreeing actions after all
dr->node.release().
But you aren't really solving any problems here, how is dr being used after the release function is called? Who is doing it in such a way that this change would really fix anything?
thanks,
greg k-h
Hello Greg,
Quoted 22 lines
-----Original Message-----
From: Greg KH [mailto:[email protected]]
Sent: Wednesday, November 06, 2013 4:59 PM
To: Liu, Chuansheng
Cc: [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] devres: Freeing the drs after allrelease()are calledQuoted 14 lines · Chuansheng Liu
In
release_nodes(), it will calldr->node.release()and kfree dr one by one.But sometimes the previous dr maybe be used by next .release(), such as:
[50314.855534] [<c12b172f>] ? synchronize_irq+0x3f/0xb0 [50314.861193] [<c12b18e9>] __free_irq+0x149/0x200 [50314.866367] [<c12b19e3>] free_irq+0x43/0xa0 [50314.871152] [<c12b4864>] devm_irq_release+0x14/0x20 [50314.876713] [<c169e806>] release_nodes+0x136/0x1b0 [50314.882178] [<c169ee79>] devres_release_all+0x39/0x60 [50314.887935] [<c169b411>] __device_release_driver+0x71/0xd0
Quoted 9 lines
Quoted 4 lines
the
free_irq()will sync the last irq handling, which maybe use freed dr, then it will cause memory corruption.Here split the dr kfreeing actions after all
dr->node.release().But you aren't really solving any problems here, how is dr being used after the release function is called? Who is doing it in such a way that this change would really fix anything?
The driver code is as below:_INIT() {
A = devm_kzalloc();
B= devm_request_threaded_irq(isr_handler);
C = devm_kzalloc();
}
When driver _EXIT, the devres_release_all () will be called.
The C will be kfreed before B, but when freeing irq B, the pending isr_handler() possibly will access the memory B which has been freed.
Then the memory corruption occurred.
This patch can solve this scenario.
Thanks.
Hello, Liu.
Quoted 8 lines · Chuansheng
The driver code is as below:
_INIT(){A =
devm_kzalloc();
B=devm_request_threaded_irq(isr_handler);
C =devm_kzalloc();
}
Quoted 6 lines
When driver
_EXIT, thedevres_release_all() will be called.
The C will be kfreed before B, but when freeing irq B, the pendingisr_handler()possibly will access the memory B which has been freed.
Then the memory corruption occurred.This patch can solve this scenario.
Isn't the bug there IRQ being requested before all its resources are allocated? The proposed change just masks the underlying issue or incorrectly ordered operations.
Thanks.
Signature · 1 line
tejun
Quoted 23 lines
-----Original Message-----
From: Tejun Heo [mailto:[email protected]] On Behalf Of [email protected]
Sent: Thursday, November 07, 2013 8:30 AM
To: Liu, Chuansheng
Cc: Greg KH; [email protected]; [email protected]
Subject: Re: [PATCH] devres: Freeing the drs after allrelease()are calledHello, Liu.
Quoted 10 lines · Chuansheng
The driver code is as below:
_INIT(){A =
devm_kzalloc();
B=devm_request_threaded_irq(isr_handler);
C =devm_kzalloc();
}When driver
_EXIT, thedevres_release_all() will be called.
The C will be kfreed before B, but when freeing irq B, the pendingisr_handler()possibly
Quoted 8 lines
Quoted 4 lines
will access the memory B which has been freed.
Then the memory corruption occurred.This patch can solve this scenario.
Isn't the bug there IRQ being requested before all its resources are allocated? The proposed change just masks the underlying issue or incorrectly ordered operations.
Yes, I knew I can put the code always like below:
A = devm_kzalloc();
C = devm_kzalloc();
...
B= devm_request_threaded_irq(isr_handler);
But, the above is just one simple coding prototype, if there are many calling:
E -- > F -- > D -- >... then to devm_kzalloc().
To be honest, it will make code too hard to always adapt the rule? And I trying to find out every potential devm_kzalloc() before irq requesting.
Quoted 37 lines · Chuansheng
Quoted 23 lines
-----Original Message-----
From: Tejun Heo [mailto:[email protected]] On Behalf Of [email protected]
Sent: Thursday, November 07, 2013 8:30 AM
To: Liu, Chuansheng
Cc: Greg KH; [email protected]; [email protected]
Subject: Re: [PATCH] devres: Freeing the drs after allrelease()are calledHello, Liu.
Quoted 10 lines · Chuansheng
The driver code is as below:
_INIT(){A =
devm_kzalloc();
B=devm_request_threaded_irq(isr_handler);
C =devm_kzalloc();
}When driver
_EXIT, thedevres_release_all() will be called.
The C will be kfreed before B, but when freeing irq B, the pendingisr_handler()possibly
Quoted 8 lines
Quoted 4 lines
will access the memory B which has been freed.
Then the memory corruption occurred.This patch can solve this scenario.
Isn't the bug there IRQ being requested before all its resources are allocated? The proposed change just masks the underlying issue or incorrectly ordered operations.
Yes, I knew I can put the code always like below:
A =devm_kzalloc();
C =devm_kzalloc();
...
B=devm_request_threaded_irq(isr_handler);
Quoted 5 lines
But, the above is just one simple coding prototype, if there are many calling:
E -- > F -- > D -- >... then todevm_kzalloc().To be honest, it will make code too hard to always adapt the rule? And I trying to find out every potential
devm_kzalloc()before irq requesting.
Such code is already broken anyway and has to be fixed - if your IRQ handler accesses data allocated by devm_kzalloc that you call only _after_ you called devm_request_irq() then interrupt arriving at unfortunate moment will crash your driver at the probing stage.
Thanks.
Signature · 1 line
Dmitry
Quoted 51 lines
-----Original Message-----
From: Dmitry Torokhov [mailto:[email protected]]
Sent: Thursday, November 07, 2013 8:47 AM
To: Liu, Chuansheng
Cc: [email protected]; Greg KH; [email protected]
Subject: Re: [PATCH] devres: Freeing the drs after allrelease()are calledQuoted 15 lines · Chuansheng
Quoted 15 lines
-----Original Message-----
From: Tejun Heo [mailto:[email protected]] On Behalf Of [email protected]
Sent: Thursday, November 07, 2013 8:30 AM
To: Liu, Chuansheng
Cc: Greg KH; [email protected]; [email protected]
Subject: Re: [PATCH] devres: Freeing the drs after allrelease()are calledHello, Liu.
Quoted 3 lines · Chuansheng
The driver code is as below:
_INIT(){Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
A =
devm_kzalloc();
B=devm_request_threaded_irq(isr_handler);
C =devm_kzalloc();
}When driver
_EXIT, thedevres_release_all() will be called. The C will be kfreed before B, but when freeing irq B, the pending
isr_handler()Quoted 18 lines
Quoted 9 lines
possibly
Quoted 4 lines
will access the memory B which has been freed.
Then the memory corruption occurred.This patch can solve this scenario.
Isn't the bug there IRQ being requested before all its resources are allocated? The proposed change just masks the underlying issue or incorrectly ordered operations.
Yes, I knew I can put the code always like below:
A =devm_kzalloc();
C =devm_kzalloc();
...
B=devm_request_threaded_irq(isr_handler);But, the above is just one simple coding prototype, if there are many calling:
E -- > F -- > D -- >... then todevm_kzalloc().
Quoted 7 lines
Quoted 2 lines
To be honest, it will make code too hard to always adapt the rule? And I trying to find out every potential
devm_kzalloc()before irq requesting.Such code is already broken anyway and has to be fixed - if your IRQ handler accesses data allocated by
devm_kzallocthat you call only _after_ you calleddevm_request_irq()then interrupt arriving at unfortunate moment will crash your driver at the probing stage.
Not exactly, request_irq() does not means the device irq is enabled indeed.
After using devres_release_all() to free the device resources, it hiddenly force all drivers probe() to put the request_irq() at the end of probe() function. Moreover, you can not call devres_alloc* again after probe().... Otherwise, there are potential memory corruption.
And except for irq related, maybe there are other things.
So I just feel this rule is too restricted?
Hello,
Quoted 11 lines · Chuansheng
Yes, I knew I can put the code always like below:
A =devm_kzalloc();
C =devm_kzalloc();
...
B=devm_request_threaded_irq(isr_handler);But, the above is just one simple coding prototype, if there are many calling:
E -- > F -- > D -- >... then todevm_kzalloc().To be honest, it will make code too hard to always adapt the rule? And I trying to find out every potential
devm_kzalloc()before irq requesting.
It isn't a good idea to paper over existing bugs from upper layer. You realize that the above code sequence is already buggy during init unless there's something explicitly blocking generation of irqs until init is complete, right? The right thing to do would be either reordering the operations or wrapping the operation which unblocks irq at the end of init with devres so that irq gets blocked before the rest of release proceeds.
What we must *NOT* do is working around existing bugs in a half-assed way from midlayer. What if some of the devres resources are more complex and actually need to be released by the devres release callback instead of being freed along with @dr? What if devres blocks are in use and releases are done in multiple steps and irq release belongs to a later group?
Signature · 1 line
tejun
Hello,
Quoted 30 lines
-----Original Message-----
From: Tejun Heo [mailto:[email protected]] On Behalf Of [email protected]
Sent: Thursday, November 07, 2013 8:52 AM
To: Liu, Chuansheng
Cc: Greg KH; [email protected]; [email protected]
Subject: Re: [PATCH] devres: Freeing the drs after allrelease()are calledHello,
Quoted 11 lines · Chuansheng
Yes, I knew I can put the code always like below:
A =devm_kzalloc();
C =devm_kzalloc();
...
B=devm_request_threaded_irq(isr_handler);But, the above is just one simple coding prototype, if there are many calling:
E -- > F -- > D -- >... then todevm_kzalloc().To be honest, it will make code too hard to always adapt the rule? And I trying to find out every potential
devm_kzalloc()before irq requesting.It isn't a good idea to paper over existing bugs from upper layer. You realize that the above code sequence is already buggy during init unless there's something explicitly blocking generation of irqs until init is complete, right? The right thing to do would be either reordering the operations or wrapping the operation which unblocks irq at the end of init with devres so that irq gets blocked before the rest of release proceeds.
Quoted 2 lines
What we must *NOT* do is working around existing bugs in a half-assed way from midlayer.
Yes, doing the right order initialization is always right thing. But normally when we hit the panic during shutdown/reboot like below:
PAGE FAULT XXX 0x12345678
It is really difficult to debug.
So at least, could we have method to expose these hidden issues?
Thanks.
I am reviewing other codes' usage of devm_request_threaded_irq() also.
Greg KH
maintainerQuoted 35 lines · Chuansheng
Hello,
Quoted 30 lines
-----Original Message-----
From: Tejun Heo [mailto:[email protected]] On Behalf Of [email protected]
Sent: Thursday, November 07, 2013 8:52 AM
To: Liu, Chuansheng
Cc: Greg KH; [email protected]; [email protected]
Subject: Re: [PATCH] devres: Freeing the drs after allrelease()are calledHello,
Quoted 11 lines · Chuansheng
Yes, I knew I can put the code always like below:
A =devm_kzalloc();
C =devm_kzalloc();
...
B=devm_request_threaded_irq(isr_handler);But, the above is just one simple coding prototype, if there are many calling:
E -- > F -- > D -- >... then todevm_kzalloc().To be honest, it will make code too hard to always adapt the rule? And I trying to find out every potential
devm_kzalloc()before irq requesting.It isn't a good idea to paper over existing bugs from upper layer. You realize that the above code sequence is already buggy during init unless there's something explicitly blocking generation of irqs until init is complete, right? The right thing to do would be either reordering the operations or wrapping the operation which unblocks irq at the end of init with devres so that irq gets blocked before the rest of release proceeds.
Quoted 2 lines
What we must *NOT* do is working around existing bugs in a half-assed way from midlayer.
Quoted 6 lines
Yes, doing the right order initialization is always right thing. But normally when we hit the panic during shutdown/reboot like below:
PAGE FAULT XXX0x12345678It is really difficult to debug.
So at least, could we have method to expose these hidden issues?
Have you enabled timer debugging? I think there's an irq debugging option as well.
We aren't going to paper over driver bugs by changing the kernel core, sorry. Consider this patch dropped.
greg k-h