[PATCH v2] sysctl: Reduce dput(child) calls in proc_sys_fill_cache()

Markus Elfring posted 1 patch 1 month ago
fs/proc/proc_sysctl.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH v2] sysctl: Reduce dput(child) calls in proc_sys_fill_cache()
Posted by Markus Elfring 1 month ago
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 23 Oct 2024 16:54:59 +0200

Replace two dput(child) calls with one that occurs immediately before
the IS_ERR evaluation. This transformation can be performed because
dput() gets called regardless of the value returned by IS_ERR(res).

This issue was transformed by using a script for the
semantic patch language like the following.
<SmPL>
@extended_adjustment@
expression e, f != { mutex_unlock }, x, y;
@@
+f(e);
 if (...)
 {
 <+... when != \( e = x \| y(..., &e, ...) \)
-   f(e);
 ...+>
 }
-f(e);
</SmPL>

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---

V2:
* This update suggestion was rebased on source files of the software
  “Linux next-20241023”.

* The change description was adjusted according to the wording preferences
  by Joel Granados.

* An SmPL script example was appended.


 fs/proc/proc_sysctl.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 9b9dfc450cb3..b277a1ca392e 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -698,11 +698,11 @@ static bool proc_sys_fill_cache(struct file *file,
 			res = d_splice_alias(inode, child);
 			d_lookup_done(child);
 			if (unlikely(res)) {
-				if (IS_ERR(res)) {
-					dput(child);
-					return false;
-				}
 				dput(child);
+
+				if (IS_ERR(res))
+					return false;
+
 				child = res;
 			}
 		}
--
2.47.0
Re: [PATCH v2] sysctl: Reduce dput(child) calls in proc_sys_fill_cache()
Posted by Joel Granados 3 weeks, 4 days ago
On Wed, Oct 23, 2024 at 05:27:11PM +0200, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 23 Oct 2024 16:54:59 +0200

Small comment: Remember to send out a new patch-set email in order to
make it easier for tools like b4.

> 
> Replace two dput(child) calls with one that occurs immediately before
> the IS_ERR evaluation. This transformation can be performed because
> dput() gets called regardless of the value returned by IS_ERR(res).
> 
> This issue was transformed by using a script for the
> semantic patch language like the following.
> <SmPL>
> @extended_adjustment@
> expression e, f != { mutex_unlock }, x, y;
> @@
> +f(e);
>  if (...)
>  {
>  <+... when != \( e = x \| y(..., &e, ...) \)
> -   f(e);
>  ...+>
>  }
> -f(e);
> </SmPL>
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
> 
> V2:
> * This update suggestion was rebased on source files of the software
>   “Linux next-20241023”.
> 
> * The change description was adjusted according to the wording preferences
>   by Joel Granados.
> 
> * An SmPL script example was appended.
> 
> 
>  fs/proc/proc_sysctl.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> index 9b9dfc450cb3..b277a1ca392e 100644
> --- a/fs/proc/proc_sysctl.c
> +++ b/fs/proc/proc_sysctl.c
> @@ -698,11 +698,11 @@ static bool proc_sys_fill_cache(struct file *file,
>  			res = d_splice_alias(inode, child);
>  			d_lookup_done(child);
>  			if (unlikely(res)) {
> -				if (IS_ERR(res)) {
> -					dput(child);
> -					return false;
> -				}
>  				dput(child);
> +
> +				if (IS_ERR(res))
> +					return false;
> +
>  				child = res;
>  			}
>  		}
> --
> 2.47.0
> 

This looks good to me. I'll push it to sysctl-next.
Thx for the patch

Reviewed-by: Joel Granados <joel.granados@kernel.org>

Best

-- 

Joel Granados
Re: [PATCH v2] sysctl: Reduce dput(child) calls in proc_sys_fill_cache()
Posted by Joel Granados 4 weeks ago
On Wed, Oct 23, 2024 at 05:27:11PM +0200, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 23 Oct 2024 16:54:59 +0200
> 
> Replace two dput(child) calls with one that occurs immediately before
> the IS_ERR evaluation. This transformation can be performed because
> dput() gets called regardless of the value returned by IS_ERR(res).
> 
> This issue was transformed by using a script for the
> semantic patch language like the following.
> <SmPL>
Aren't you missing a "virtual patch" here?

Is there another way to run it besides this command:
make coccicheck MODE=patch SPFLAGS="--in-place --include-headers --smpl-spacing --jobs=16" COCCI=SCRIPT

Best
> @extended_adjustment@
> expression e, f != { mutex_unlock }, x, y;
> @@
> +f(e);
>  if (...)
>  {
>  <+... when != \( e = x \| y(..., &e, ...) \)
> -   f(e);
>  ...+>
>  }
> -f(e);
> </SmPL>
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
> 
> V2:
> * This update suggestion was rebased on source files of the software
>   “Linux next-20241023”.
> 
> * The change description was adjusted according to the wording preferences
>   by Joel Granados.
> 
> * An SmPL script example was appended.
> 
> 
>  fs/proc/proc_sysctl.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
> index 9b9dfc450cb3..b277a1ca392e 100644
> --- a/fs/proc/proc_sysctl.c
> +++ b/fs/proc/proc_sysctl.c
> @@ -698,11 +698,11 @@ static bool proc_sys_fill_cache(struct file *file,
>  			res = d_splice_alias(inode, child);
>  			d_lookup_done(child);
>  			if (unlikely(res)) {
> -				if (IS_ERR(res)) {
> -					dput(child);
> -					return false;
> -				}
>  				dput(child);
> +
> +				if (IS_ERR(res))
> +					return false;
> +
>  				child = res;
>  			}
>  		}
> --
> 2.47.0
> 

-- 

Joel Granados
Re: [v2] sysctl: Reduce dput(child) calls in proc_sys_fill_cache()
Posted by Markus Elfring 4 weeks ago
…
>> This issue was transformed by using a script for the
>> semantic patch language like the following.
>> <SmPL>
> Aren't you missing a "virtual patch" here?

Yes.

I presented an SmPL script example which would not be directly used
for the tool “coccicheck”.


> Is there another way to run it besides this command:

Another command example:
time spatch -timeout 23 -j4 --chunksize 1 --no-loops -dir . …/Projekte/Coccinelle/janitor/avoid_duplicate_function_call2.cocci > …/Projekte/Bau/Linux/scripts/Coccinelle/tuning1/next/20240913/avoid_duplicate_function_call2-no_loops.diff 2> …/Projekte/Bau/Linux/scripts/Coccinelle/tuning1/next/20240913/avoid_duplicate_function_call2-no_loops-errors.txt


Regards,
Markus