linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: Coccinelle: Checking of_node_put() calls with SmPL
       [not found] <201907111435459627761@zte.com.cn>
@ 2019-07-11  6:46 ` Julia Lawall
  2019-07-11  9:33   ` Markus Elfring
  2019-07-11  9:04 ` Markus Elfring
  2019-07-11 14:41 ` Tyrel Datwyler
  2 siblings, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2019-07-11  6:46 UTC (permalink / raw)
  To: wen.yang99
  Cc: Markus.Elfring, rjw, daniel.lezcano, linux-pm, kernel-janitors,
	benh, cheng.shengyu, galak, mpe, paulus, oss, xue.zhihong,
	wang.yi59, linux-kernel, linuxppc-dev

[-- Attachment #1: Type: text/plain, Size: 1808 bytes --]



On Thu, 11 Jul 2019, wen.yang99@zte.com.cn wrote:

> > > we developed a coccinelle script to detect such problems.
> >
> > Would you find the implementation of the function “dt_init_idle_driver”
> > suspicious according to discussed source code search patterns?
> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/cpuidle/dt_idle_states.c?id=e9a83bd2322035ed9d7dcf35753d3f984d76c6a5#n208
> > https://elixir.bootlin.com/linux/v5.2/source/drivers/cpuidle/dt_idle_states.c#L208
> >
> >
> > > This script is still being improved.
> >
> > Will corresponding software development challenges become more interesting?
>
> Hello Markus,
> This is the simplified code pattern for it:
>
> 172         for (i = 0; ; i++) {
> 173                 state_node = of_parse_phandle(...);     ---> Obtain here
> ...
> 177                 match_id = of_match_node(matches, state_node);
> 178                 if (!match_id) {
> 179                         err = -ENODEV;
> 180                         break;                         --->  Jump out of the loop without releasing it
> 181                 }
> 182
> 183                 if (!of_device_is_available(state_node)) {
> 184                         of_node_put(state_node);
> 185                         continue;                    --->  Release the object references within a loop
> 186                 }
> ...
> 208                 of_node_put(state_node);  -->  Release the object references within a loop
> 209         }
> 210
> 211         of_node_put(state_node);       -->    There may be double free here.
>
> This code pattern is very interesting and the coccinelle software should also recognize this pattern.

In my experience, when you start looking at these of_node_put things, all
sorts of strange things appear...

julia

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Coccinelle: Checking of_node_put() calls with SmPL
       [not found] <201907111435459627761@zte.com.cn>
  2019-07-11  6:46 ` Coccinelle: Checking of_node_put() calls with SmPL Julia Lawall
@ 2019-07-11  9:04 ` Markus Elfring
  2019-07-11 14:41 ` Tyrel Datwyler
  2 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2019-07-11  9:04 UTC (permalink / raw)
  To: Wen Yang, linux-pm, kernel-janitors
  Cc: Julia Lawall, Rafael J. Wysocki, Daniel Lezcano,
	Benjamin Herrenschmidt, Cheng Shengyu, Kumar Gala,
	Michael Ellerman, Paul Mackerras, Scott Wood, Xue Zhihong,
	Yi Wang, linux-kernel, linuxppc-dev

> 180                         break;                         --->  Jump out of the loop without releasing it

The device node reference is released behind this for loop.


> 183                 if (!of_device_is_available(state_node)) {
> 184                         of_node_put(state_node);

This function call was added by the commit “cpuidle: dt: Add missing 'of_node_put()'”
on 2017-06-12.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/cpuidle/dt_idle_states.c?id=b2cdd8e1b54849477a32d820acc2e87828a38f3d


> 185                         continue;                    --->  Release the object references within a loop

I became curious on the applicability of an other coding style
(for a software refactoring) at this place.
How do you think about to achieve the same effect by using a goto statement
instead of two statements in such an if branch?


> 208                 of_node_put(state_node);  -->  Release the object references within a loop
> 209         }
> 210
> 211         of_node_put(state_node);       -->    There may be double free here.

This information points a recurring challenge out for safe source code analysis.
How would you like to exclude the detection of false positives finally?


> This code pattern is very interesting

Thanks that you think also in this direction.


> and the coccinelle software should also recognize this pattern.

There are some open issues to consider for available analysis tools.
How will corresponding details be clarified then?

Regards,
Markus

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Coccinelle: Checking of_node_put() calls with SmPL
  2019-07-11  6:46 ` Coccinelle: Checking of_node_put() calls with SmPL Julia Lawall
@ 2019-07-11  9:33   ` Markus Elfring
  0 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2019-07-11  9:33 UTC (permalink / raw)
  To: Julia Lawall, Wen Yang, kernel-janitors
  Cc: Rafael J. Wysocki, Daniel Lezcano, Benjamin Herrenschmidt,
	Cheng Shengyu, Kumar Gala, Michael Ellerman, Paul Mackerras,
	Scott Wood, Xue Zhihong, Yi Wang, linux-kernel, linuxppc-dev,
	linux-pm

> In my experience, when you start looking at these of_node_put things,
> all sorts of strange things appear...

How much will this situation influence the achievement of further improvements
also for your software?

Regards,
Markus

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Coccinelle: Checking of_node_put() calls with SmPL
       [not found] <201907111435459627761@zte.com.cn>
  2019-07-11  6:46 ` Coccinelle: Checking of_node_put() calls with SmPL Julia Lawall
  2019-07-11  9:04 ` Markus Elfring
@ 2019-07-11 14:41 ` Tyrel Datwyler
  2 siblings, 0 replies; 5+ messages in thread
From: Tyrel Datwyler @ 2019-07-11 14:41 UTC (permalink / raw)
  To: wen.yang99, Markus.Elfring, julia.lawall
  Cc: wang.yi59, linux-pm, rjw, daniel.lezcano, kernel-janitors,
	linux-kernel, oss, paulus, xue.zhihong, linuxppc-dev,
	cheng.shengyu

On 07/10/2019 11:35 PM, wen.yang99@zte.com.cn wrote:
>>> we developed a coccinelle script to detect such problems.
>>
>> Would you find the implementation of the function “dt_init_idle_driver”
>> suspicious according to discussed source code search patterns?
>> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/cpuidle/dt_idle_states.c?id=e9a83bd2322035ed9d7dcf35753d3f984d76c6a5#n208
>> https://elixir.bootlin.com/linux/v5.2/source/drivers/cpuidle/dt_idle_states.c#L208
>>
>>
>>> This script is still being improved.
>>
>> Will corresponding software development challenges become more interesting?
> 
> Hello Markus,
> This is the simplified code pattern for it:
> 
> 172         for (i = 0; ; i++) {

This loop can only be exited on a break.

> 173                 state_node = of_parse_phandle(...);     ---> Obtain here
> ...
> 177                 match_id = of_match_node(matches, state_node);
> 178                 if (!match_id) {
> 179                         err = -ENODEV;                              
> 180                         break;                         --->  Jump out of the loop without releasing it
> 181                 }
> 182 
> 183                 if (!of_device_is_available(state_node)) {
> 184                         of_node_put(state_node);
> 185                         continue;                    --->  Release the object references within a loop
> 186                 }
> ...
> 208                 of_node_put(state_node);  -->  Release the object references within a loop

This is required at the end of every loop or continue to free the reference.
Only a break will exit the loop where we hit the below of_node_put().

> 209         }
> 210 
> 211         of_node_put(state_node);       -->    There may be double free here.

None of the break conditions call of_node_put(), so it needs to be called here.

-Tyrel

> 
> This code pattern is very interesting and the coccinelle software should also recognize this pattern.
> 
> Regards,
> Wen
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Coccinelle: Checking of_node_put() calls with SmPL
       [not found] <201907101533443009168@zte.com.cn>
@ 2019-07-10 15:15 ` Markus Elfring
  0 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2019-07-10 15:15 UTC (permalink / raw)
  To: Wen Yang, Rafael J. Wysocki, Daniel Lezcano, linux-pm, kernel-janitors
  Cc: Benjamin Herrenschmidt, Cheng Shengyu, Kumar Gala,
	Michael Ellerman, Paul Mackerras, Scott Wood, Xue Zhihong,
	Yi Wang, linux-kernel, linuxppc-dev

> we developed a coccinelle script to detect such problems.

Would you find the implementation of the function “dt_init_idle_driver”
suspicious according to discussed source code search patterns?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/cpuidle/dt_idle_states.c?id=e9a83bd2322035ed9d7dcf35753d3f984d76c6a5#n208
https://elixir.bootlin.com/linux/v5.2/source/drivers/cpuidle/dt_idle_states.c#L208


> This script is still being improved.

Will corresponding software development challenges become more interesting?

Regards,
Markus

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-07-11 14:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <201907111435459627761@zte.com.cn>
2019-07-11  6:46 ` Coccinelle: Checking of_node_put() calls with SmPL Julia Lawall
2019-07-11  9:33   ` Markus Elfring
2019-07-11  9:04 ` Markus Elfring
2019-07-11 14:41 ` Tyrel Datwyler
     [not found] <201907101533443009168@zte.com.cn>
2019-07-10 15:15 ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).