Skip to contentSkip to conversation

KVM: arm64: Use pointer from memcpy() call for assignment in init_hyp_mode()

PATCH In review Markus Elfring 3 participants 3 messages Oct 30, 2025

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

PATCH 1/1 KVM: arm64: Use pointer from memcpy() call for assignment in init_hyp_mode()

From: Markus Elfring <[email protected]>
Date: Thu, 30 Oct 2025 18:01:41 +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/arm64/kvm/arm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/arm.c +2 -2
index 870953b4a8a7..feab88c31703 100644
@@ -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;
 	}
 
Signature · 1 line
2.51.1
reviewer
#1 lore raw
Quoted 21 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 30 Oct 2025 18:01:41 +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/arm64/kvm/arm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/arm.c +0 -0
index 870953b4a8a7..feab88c31703 100644

arch/arm64/kvm/arm.c static int __init init_hyp_mode(void) · line 2604

Quoted 7 lines
arch/arm64/kvm/arm.c +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());

This change makes the code harder to read, and harder to modify. It saves no space.

As Dan said [1]:

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

[1] https://lore.kernel.org/lkml/[email protected]/

Mark.

#2 lore raw
Quoted 25 lines · On Thu, 30 Oct 2025 17:49:51 +0000 Mark Rutland <[email protected]> wrote:
Quoted 24 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Thu, 30 Oct 2025 18:01:41 +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/arm64/kvm/arm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/arm.c +0 -0
index 870953b4a8a7..feab88c31703 100644
@@ -2600,8 +2600,8 @@ static int __init init_hyp_mode(void)
 			goto out_err;
 		}
 
Quoted 7 lines

arch/arm64/kvm/arm.c static int __init init_hyp_mode(void) · line 2604

Quoted 4 lines
arch/arm64/kvm/arm.c +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());  

This change makes the code harder to read, and harder to modify. It saves no space.

It might save a register spill - but really isn't worth the effort. memcpy() is really best treated as being 'void'.
Indeed most implementations would be better if it were 'void'. Although you could define:
#define memcpy(d, s, l) ({ auto _d = d; void_memcpy(_d, s, l); _d}) so that the compiler would optimise away the save that memcpy() typically has to do.

I even suspect that memcpy() is an old enough function that the return value is 'what the implementation happened to leave in r0'.

David

Quoted 10 lines

As Dan said [1]:

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

[1] https://lore.kernel.org/lkml/[email protected]/

Mark.