Skip to contentSkip to conversation

sparc: time: Use pointer from memcpy() call for assignment in setup_sparc64_timer()

PATCH In review Markus Elfring 4 participants 6 messages Oct 31, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sparc: time: Use pointer from memcpy() call for assignment in setup_sparc64_timer()

From: Markus Elfring <[email protected]>
Date: Fri, 31 Oct 2025 08:36:13 +0100

A pointer was assigned to a variable. The same pointer was used for the destination parameter of a memcpy() call.
This function is documented in the way that the same value is returned. Thus convert two separate statements into a direct variable assignment for the return value from a memory copy action.

The source code was transformed by using the Coccinelle software.

---
 arch/sparc/kernel/time_64.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
arch/sparc/kernel/time_64.c +1 -3
index b32f27f929d1..e9c29574cd59 100644
@@ -760,9 +760,7 @@ void setup_sparc64_timer(void)
 			     : /* no outputs */
 			     : "r" (pstate));
 
-	sevt = this_cpu_ptr(&sparc64_events);
-
-	memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
+	sevt = memcpy(this_cpu_ptr(&sparc64_events), &sparc64_clockevent, sizeof(*sevt));
 	sevt->cpumask = cpumask_of(smp_processor_id());
 
 	clockevents_register_device(sevt);
Signature · 1 line
2.51.1
#1 lore raw

Hi Markus,

Quoted 9 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Fri, 31 Oct 2025 08:36:13 +0100

A pointer was assigned to a variable. The same pointer was used for the destination parameter of a memcpy() call.
This function is documented in the way that the same value is returned. Thus convert two separate statements into a direct variable assignment for the return value from a memory copy action.

Quoted 3 lines

The source code was transformed by using the Coccinelle software.

Thanks for your patch!

arch/sparc/kernel/time_64.c void setup_sparc64_timer(void) · line 763

Quoted 10 lines
arch/sparc/kernel/time_64.c +1 -3
@@ -760,9 +760,7 @@ void setup_sparc64_timer(void)
                             : /* no outputs */
                             : "r" (pstate));
 
-       sevt = this_cpu_ptr(&sparc64_events);
-
-       memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
+       sevt = memcpy(this_cpu_ptr(&sparc64_events), &sparc64_clockevent, sizeof(*sevt));

IMHO this makes the code harder to read:
- Only 0.15% of the memcpy() calls in the kernel use the memcpy() chaining feature,
- The line is now longer than the 80-character limit, which is still customary for this file.

arch/sparc/kernel/time_64.c void setup_sparc64_timer(void) · line 766

Quoted 3 lines
arch/sparc/kernel/time_64.c +0 -0
        sevt->cpumask = cpumask_of(smp_processor_id());
 
        clockevents_register_device(sevt);

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw

…>> +++ b/arch/sparc/kernel/time_64.c

Quoted 4 lines
Quoted 4 lines
arch/sparc/kernel/time_64.c +0 -0
@@ -760,9 +760,7 @@ void setup_sparc64_timer(void)
                             : /* no outputs */
                             : "r" (pstate));
 
Quoted 8 lines

arch/sparc/kernel/time_64.c void setup_sparc64_timer(void) · line 763

Quoted 4 lines
arch/sparc/kernel/time_64.c +1 -3
-       sevt = this_cpu_ptr(&sparc64_events);
-
-       memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
+       sevt = memcpy(this_cpu_ptr(&sparc64_events), &sparc64_clockevent, sizeof(*sevt));

IMHO this makes the code harder to read:
- Only 0.15% of the memcpy() calls in the kernel use the memcpy() chaining feature,

I obviously propose to refactor this implementation detail.

Quoted 2 lines

- The line is now longer than the 80-character limit, which is still customary for this file.

Would you like to get a subsequent patch variant?

Regards,
Markus

#3 lore raw
Quoted 14 lines · On Fri, 31 Oct 2025 09:46:25 +0100 Markus Elfring <[email protected]> wrote:

…>> +++ b/arch/sparc/kernel/time_64.c

Quoted 4 lines
Quoted 4 lines
arch/sparc/kernel/time_64.c +0 -0
@@ -760,9 +760,7 @@ void setup_sparc64_timer(void)
                             : /* no outputs */
                             : "r" (pstate));
 
Quoted 8 lines

arch/sparc/kernel/time_64.c void setup_sparc64_timer(void) · line 763

Quoted 4 lines
arch/sparc/kernel/time_64.c +1 -3
-       sevt = this_cpu_ptr(&sparc64_events);
-
-       memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
+       sevt = memcpy(this_cpu_ptr(&sparc64_events), &sparc64_clockevent, sizeof(*sevt));  

IMHO this makes the code harder to read:
- Only 0.15% of the memcpy() calls in the kernel use the memcpy() chaining feature,

Quoted 1 line

I obviously propose to refactor this implementation detail.

Reduce it to 0% and the kernel memcpy() can be made 'void'. :-) That will simplify the architecture specific implementations.

The same can be done for strcpy() and strcat() (etc). Where the 'useful' return value would be the address of the '\0'.

David

#4 lore raw
In reply to Markus Elfring

Hi Markus,

Quoted 13 lines · Markus Elfring

…>> +++ b/arch/sparc/kernel/time_64.c

Quoted 12 lines

arch/sparc/kernel/time_64.c void setup_sparc64_timer(void) · line 763

Quoted 8 lines
arch/sparc/kernel/time_64.c +1 -3
@@ -760,9 +760,7 @@ void setup_sparc64_timer(void)
                             : /* no outputs */
                             : "r" (pstate));
 
-       sevt = this_cpu_ptr(&sparc64_events);
-
-       memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
+       sevt = memcpy(this_cpu_ptr(&sparc64_events), &sparc64_clockevent, sizeof(*sevt));

IMHO this makes the code harder to read:
- Only 0.15% of the memcpy() calls in the kernel use the memcpy() chaining feature,

It is also less clear the passed size matches the destination pointer.

Quoted 1 line

I obviously propose to refactor this implementation detail.

Oh no...

<other-bad-ideas>

The above function could be shortened by writing

    (sevt = memcpy(this_cpu_ptr(&sparc64_events), &sparc64_clockevent,

sizeof(*sevt)))->cpumask = cpumask_of(smp_processor_id());

And after introducing a variant of clockevents_register_device() that takes the cpumask as a parameter:

clockevents_register_device_with_cpumask(memcpy(this_cpu_ptr(&sparc64_events),
&sparc64_clockevent, sizeof(*sevt)), cpumask_of(smp_processor_id()));

</other-bad-ideas>

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#5 lore raw
Quoted 5 lines · Geert Uytterhoeven

The above function could be shortened by writing

    (sevt = memcpy(this_cpu_ptr(&sparc64_events), &sparc64_clockevent,

sizeof(*sevt)))->cpumask = cpumask_of(smp_processor_id());

Heh.

regards,
dan carpenter