Skip to contentSkip to conversation

Increasing usage of direct pointer assignments from memcpy() calls with SmPL?

RFC Markus Elfring 3 participants 17 messages Oct 29, 2025 – Oct 31, 2025 Kernel janitors

lore
Share & tools
lore
#0 lore raw

Hello,

I got into the mood to try another simple source code transformation out which can be achieved also by the means of the semantic patch language.

@replacement@
expression object, size, source, target;
@@
target =
- object;
memcpy(
- target
+ object

        , source,
        size);

Test result (according to the software combination “Coccinelle 1.3.0”):
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --timeout 23 -j4 --chunksize 1 --no-loops -dir . …/Projekte/Coccinelle/janitor/use_memcpy_assignment.cocci > …/Projekte/Bau/Linux/scripts/Coccinelle/use_memcpy_assignment-no_loops-20251029.diff 2> …/Projekte/Bau/Linux/scripts/Coccinelle/use_memcpy_assignment-no_loops-errors-20251029.txt

real    5m35,579s
user    20m20,037s
sys     0m14,467s

It can be determined then from the generated diff file that mentioned implementation details can be transformed in 304 source files at the moment. Thus I became curious if it would be supported to adjust any places there according to (Linux) coding style preferences.

Regards,
Markus

#1 lore raw
Quoted 24 lines · Markus Elfring

Hello,

I got into the mood to try another simple source code transformation out which can be achieved also by the means of the semantic patch language.

@replacement@
expression object, size, source, target;
@@
target =

(quoted diff) +1 -2
-         object;
 memcpy(
-       target
+       object
        , source,
        size);

Test result (according to the software combination “Coccinelle 1.3.0”):
Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --timeout 23 -j4 --chunksize 1 --no-loops -dir . …/Projekte/Coccinelle/janitor/use_memcpy_assignment.cocci > …/Projekte/Bau/Linux/scripts/Coccinelle/use_memcpy_assignment-no_loops-20251029.diff 2> …/Projekte/Bau/Linux/scripts/Coccinelle/use_memcpy_assignment-no_loops-errors-20251029.txt

real    5m35,579s
user    20m20,037s
sys     0m14,467s
Quoted 4 lines

It can be determined then from the generated diff file that mentioned implementation details can be transformed in 304 source files at the moment. Thus I became curious if it would be supported to adjust any places there according to (Linux) coding style preferences.

If you have a concern, you have to say what it is. It doesn't seem it is about the running time, so why do you include that information?

I should not have to repeat your experiment to figure out what you are asking about.

julia

#2 lore raw
Quoted 6 lines
Quoted 4 lines

It can be determined then from the generated diff file that mentioned implementation details can be transformed in 304 source files at the moment. Thus I became curious if it would be supported to adjust any places there according to (Linux) coding style preferences.

If you have a concern, you have to say what it is.

I expressed something for further development considerations. The source code analysis result can be interpreted in some directions.

Quoted 2 lines

It doesn't seem it is
about the running time,

Not directly in this case.

Quoted 1 line

so why do you include that information?

Further software users can compare such a measurement with other observations.

Quoted 2 lines

I should not have to repeat your experiment to figure out what you are asking about.

You can recognise recurring development challenges, can't you?

* Change resistance

* Varying coding style preferences

* Code improvement possibilities

* Development resources

* Pretty-printing issues

Another test result representation for your convenience:
https://elixir.bootlin.com/linux/v6.18-rc3/source/arch/arm64/kvm/arm.c#L2554-L2726

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment.cocci arch/arm64/kvm/arm.c …
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)

                        goto out_err;
                }
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =
+               memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }
real    0m0,606s
user    0m0,576s
sys     0m0,030s

Another SmPL script example might become helpful.

@replacement2@
expression object, size, source, target;
@@
target =
- object; memcpy(target, source, size)
+ memcpy(object, source, size)
;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment2.cocci arch/arm64/kvm/arm.c …
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)

                        goto out_err;
                }
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start),
+                                 nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }
real    0m0,626s
user    0m0,588s
sys     0m0,037s

Regards,
Markus

#3 lore raw
Quoted 44 lines · Markus Elfring
Quoted 6 lines
Quoted 4 lines

It can be determined then from the generated diff file that mentioned implementation details can be transformed in 304 source files at the moment. Thus I became curious if it would be supported to adjust any places there according to (Linux) coding style preferences.

If you have a concern, you have to say what it is.

I expressed something for further development considerations. The source code analysis result can be interpreted in some directions.

Quoted 2 lines

It doesn't seem it is
about the running time,

Not directly in this case.

Quoted 1 line

so why do you include that information?

Further software users can compare such a measurement with other observations.

Quoted 2 lines

I should not have to repeat your experiment to figure out what you are asking about.

You can recognise recurring development challenges, can't you?

* Change resistance

* Varying coding style preferences

* Code improvement possibilities

* Development resources

* Pretty-printing issues

Another test result representation for your convenience:
https://elixir.bootlin.com/linux/v6.18-rc3/source/arch/arm64/kvm/arm.c#L2554-L2726

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment.cocci arch/arm64/kvm/arm.c …

Review static int __init init_hyp_mode(void) · line 2605

Quoted 8 lines
(quoted diff) +2 -2
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =
+               memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;

This is all I wanted. The rest is complete noise.

I think you would get a more satisfactory result by just removing all of the code and adding it back. Then Coccinelle would take charge of laying out the whole thing.

thanks,
julia

Quoted 42 lines
(quoted diff) +0 -0
        }
 
real    0m0,606s
user    0m0,576s
sys     0m0,030s

Another SmPL script example might become helpful.

@replacement2@
expression object, size, source, target;
@@
target =

(quoted diff) +1 -1
-         object; memcpy(target, source, size)
+         memcpy(object, source, size)
 ;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment2.cocci arch/arm64/kvm/arm.c …

(quoted diff) +2 -2
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start),
+                                 nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }
 
real    0m0,626s
user    0m0,588s
sys     0m0,037s

Regards,
Markus

#4 lore raw

…> I think you would get a more satisfactory result by just removing all of

Quoted 2 lines

the code and adding it back. Then Coccinelle would take charge of laying out the whole thing.

…

See also once more:

Quoted 11 lines
Quoted 11 lines

Another SmPL script example might become helpful.

@replacement2@
expression object, size, source, target;
@@
target =

(quoted diff) +1 -1
-         object; memcpy(target, source, size)
+         memcpy(object, source, size)
 ;
Quoted 1 line
Quoted 1 line

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment2.cocci arch/arm64/kvm/arm.c

…>> real 0m0,626s

Quoted 2 lines
Quoted 2 lines

user 0m0,588s
sys 0m0,037s

Regards,
Markus

#5 lore raw
Quoted 16 lines · Markus Elfring

…> I think you would get a more satisfactory result by just removing all of

Quoted 2 lines

the code and adding it back. Then Coccinelle would take charge of laying out the whole thing.

…

See also once more:

Quoted 9 lines
Quoted 9 lines

Another SmPL script example might become helpful.

@replacement2@
expression object, size, source, target;
@@
target =

(quoted diff) +1 -1
-         object; memcpy(target, source, size)
+         memcpy(object, source, size)
 ;

I said to remove everything. target = also.

julia

Quoted 11 lines
Quoted 3 lines
Quoted 3 lines

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment2.cocci arch/arm64/kvm/arm.c

…>> real 0m0,626s

Quoted 2 lines
Quoted 2 lines

user 0m0,588s
sys 0m0,037s

Regards,
Markus

#6 lore raw
In reply to Julia Lawall
Quoted 2 lines

If you have a concern, you have to say what it is. It doesn't seem it is about the running time, so why do you include that information?

How will remaining open issues be resolved?

Quoted 1 line

I should not have to repeat your experiment

It would be nice if further software users would occasionally reproduce presented data processing possibilities.

Quoted 2 lines

to figure out what you are
asking about.

I “accidentally” tried also the following SmPL script variants out.

A)
@replacement3@
expression object, size, source, target;
@@
target =
-object; memcpy(target, source, size)
+object; memcpy(object, source, size)
;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment3.cocci arch/arm64/kvm/arm.c …
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)

                        goto out_err;
                }
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start),
+                                 nvhe_percpu_size());page_address(page);
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }
real    0m0,578s
user    0m0,524s
sys     0m0,047s

B)
@replacement4@
expression object, size, source, target;
@@
-target = object; memcpy(target, source, size)
+target = object; memcpy(object, source, size)
;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment4.cocci arch/arm64/kvm/arm.c …
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)

                        goto out_err;
                }
+               memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }
real    0m0,565s
user    0m0,533s
sys     0m0,032s

Would you like to reconsider implementation details accordingly?

Regards,
Markus

#7 lore raw
Quoted 57 lines · Markus Elfring
Quoted 2 lines

If you have a concern, you have to say what it is. It doesn't seem it is about the running time, so why do you include that information?

How will remaining open issues be resolved?

Quoted 1 line

I should not have to repeat your experiment

It would be nice if further software users would occasionally reproduce presented data processing possibilities.

Quoted 2 lines

to figure out what you are
asking about.

I “accidentally” tried also the following SmPL script variants out.

A)
@replacement3@
expression object, size, source, target;
@@
target =

(quoted diff) +1 -1
-object; memcpy(target, source, size)
+object; memcpy(object, source, size)
 ;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment3.cocci arch/arm64/kvm/arm.c …

(quoted diff) +2 -2
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start),
+                                 nvhe_percpu_size());page_address(page);
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }
 
real    0m0,578s
user    0m0,524s
sys     0m0,047s

B)
@replacement4@
expression object, size, source, target;
@@

(quoted diff) +1 -1
-target = object; memcpy(target, source, size)
+target = object; memcpy(object, source, size)
 ;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment4.cocci arch/arm64/kvm/arm.c …

Review static int __init init_hyp_mode(void) · line 2606

Quoted 8 lines
(quoted diff) +1 -1
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
+               memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }

Not sure what is the point of all this. Try:

- target = object; memcpy(target, source, size);
+ target = memcpy(target, source, size);

Then it will add newlines in the argument of memcpy if needed to stay in a reasonable number of columns.

julia

Quoted 13 lines

real    0m0,565s
user    0m0,533s
sys     0m0,032s

Would you like to reconsider implementation details accordingly?

Regards,
Markus

#8 lore raw
Quoted 17 lines
Quoted 17 lines

I “accidentally” tried also the following SmPL script variants out.

A)
@replacement3@
expression object, size, source, target;
@@
target =

(quoted diff) +1 -1
-object; memcpy(target, source, size)
+object; memcpy(object, source, size)
 ;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment3.cocci arch/arm64/kvm/arm.c …

(quoted diff) +0 -0
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
Quoted 6 lines

Review static int __init init_hyp_mode(void) · line 2606

Quoted 6 lines
(quoted diff) +2 -2
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start),
+                                 nvhe_percpu_size());page_address(page);
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }

…>> B)

Quoted 13 lines
Quoted 13 lines

@replacement4@
expression object, size, source, target;
@@

(quoted diff) +1 -1
-target = object; memcpy(target, source, size)
+target = object; memcpy(object, source, size)
 ;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment4.cocci arch/arm64/kvm/arm.c …

(quoted diff) +0 -0
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
Quoted 7 lines

Review static int __init init_hyp_mode(void) · line 2606

Quoted 5 lines
(quoted diff) +1 -1
+               memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }

Not sure what is the point of all this. Try:

…

Would you like to acknowledge that undesirable difference displays were generated anyhow for these test cases?

Quoted 1 line
Quoted 1 line

Would you like to reconsider implementation details accordingly?

Are we looking for corresponding software improvements?

Regards,
Markus

#9 lore raw
Quoted 44 lines · Markus Elfring
Quoted 17 lines
Quoted 17 lines

I “accidentally” tried also the following SmPL script variants out.

A)
@replacement3@
expression object, size, source, target;
@@
target =

(quoted diff) +1 -1
-object; memcpy(target, source, size)
+object; memcpy(object, source, size)
 ;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment3.cocci arch/arm64/kvm/arm.c …

(quoted diff) +0 -0
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
Quoted 6 lines

Review static int __init init_hyp_mode(void) · line 2606

Quoted 6 lines
(quoted diff) +2 -2
-               page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
+               page_addr =memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start),
+                                 nvhe_percpu_size());page_address(page);
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }

…>> B)

Quoted 13 lines
Quoted 13 lines

@replacement4@
expression object, size, source, target;
@@

(quoted diff) +1 -1
-target = object; memcpy(target, source, size)
+target = object; memcpy(object, source, size)
 ;

Markus_Elfring@Sonne:…/Projekte/Linux/next-analyses> time /usr/bin/spatch --max-width 100 --no-loops …/Projekte/Coccinelle/janitor/use_memcpy_assignment4.cocci arch/arm64/kvm/arm.c …

(quoted diff) +0 -0
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
Quoted 7 lines

Review static int __init init_hyp_mode(void) · line 2606

Quoted 5 lines
(quoted diff) +1 -1
+               memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }

Not sure what is the point of all this. Try:

Quoted 4 lines

…

Would you like to acknowledge that undesirable difference displays were generated anyhow for these test cases?

Sure.

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Would you like to reconsider implementation details accordingly?

Are we looking for corresponding software improvements?

If time permits.

julia

#10 lore raw
In reply to Julia Lawall
Quoted 15 lines · Julia Lawall

Review static int __init init_hyp_mode(void) · line 2606

Quoted 9 lines · Markus Elfring
(quoted diff) +1 -1
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
                        goto out_err;
                }
 
+               memcpy(page_address(page), CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                page_addr = page_address(page);
-               memcpy(page_addr, CHOOSE_NVHE_SYM(__per_cpu_start), nvhe_percpu_size());
                kvm_nvhe_sym(kvm_arm_hyp_percpu_base)[cpu] = (unsigned long)page_addr;
        }

Not sure what is the point of all this. Try:

Quoted 2 lines

- target = object; memcpy(target, source, size);
+ target = memcpy(target, source, size);

No one will thank you for making these changes... :( Please don't do it.

regards,
dan carpenter

#11 lore raw
Quoted 1 line
Quoted 1 line

- target = object; memcpy(target, source, size);

+ target = memcpy(object, source, size);

Quoted 1 line

No one will thank you for making these changes... 🙁

Will any more contributors dare to try adjustments out at such source code places?

Quoted 1 line

Please don't do it.

How will change tolerance evolve further?

Regards,
Markus

#12 lore raw
In reply to Dan Carpenter
Quoted 1 line

No one will thank you for making these changes... :( Please don't do it.

I am curious if further developers would dare to try another source code search pattern out.

@display@
expression destination, size, source, target;
@@
*target = memcpy(destination, source, size);

I would like to point out that return values from memcpy() calls are assigned to variables in 16 source files of the software “Linux next-20251030”.

Under which circumstances can development interests grow to reduce “unused” return values also for this kind of function?
https://wiki.sei.cmu.edu/confluence/display/c/MSC13-C.+Detect+and+remove+unused+values

Regards,
Markus

#13 lore raw
In reply to Julia Lawall
Quoted 2 lines

If you have a concern, you have to say what it is. It doesn't seem it is about the running time, so why do you include that information?

How will the chances evolve to integrate another coccicheck script for the discussed source code transformation approach?

Regards,
Markus

#14 lore raw
Quoted 5 lines · Markus Elfring
Quoted 2 lines

If you have a concern, you have to say what it is. It doesn't seem it is about the running time, so why do you include that information?

How will the chances evolve to integrate another coccicheck script for the discussed source code transformation approach?

No chance, given Dan's negative feedback.

julia

#15 lore raw
Quoted 1 line

No chance, given Dan's negative feedback.

Will any additional arguments become more convincing for change possibilities?

Regards,
Markus

#16 lore raw
In reply to Julia Lawall
Quoted 1 line

If you have a concern, you have to say what it is.

The initial search pattern might be too simple so far.
Thus I became interested in further software extensions also according to the discussed source code transformation approach.
Can it be determined anyhow (by the means of the semantic patch language) that the variable from the assignment statement would not be used any more after the adjustment within the affected scope?

Regards,
Markus