Skip to contentSkip to conversation

Misc comments fixup in relocate_kernel_64.S

PATCH Superseded Kai Huang 2 participants 7 messages Aug 19, 2024 – Aug 20, 2024 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kai Huang

author
#0 lore raw

cover Misc comments fixup in relocate_kernel_64.S

The assembly code of relcocate_kernel() takes some time to follow for newbies like me. This series adds some comments to try to improve the readability hoping they could be helpful to others too. Also fix an error (IIUC) in one comment.

Kai Huang (2):
x86/kexec: Fix a comment of swap_pages() assembly x86/kexec: Add comments around swap_pages() assembly to improve
readability

 arch/x86/kernel/relocate_kernel_64.S | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

base-commit: b8c7cbc324dc17b9e42379b42603613580bec2d8

Signature · 1 line
2.45.2
author
#1 lore raw

PATCH 1/2 x86/kexec: Fix a comment of swap_pages() assembly

When relocate_kernel() gets called, %rdi holds 'indirection_page' and %rsi holds 'page_list'. And %rdi always holds 'indirection_page' when swap_pages() is called.

Therefore the comment of the first line code of swap_pages()

movq %rdi, %rcx /* Put the page_list in %rcx */

.. isn't correct because it actually moves the 'indirection_page' to the %rcx. Fix it.

---
 arch/x86/kernel/relocate_kernel_64.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/relocate_kernel_64.S +1 -1
index 042c9a0334e9..f7a3ca3dee53 100644
@@ -258,7 +258,7 @@ SYM_CODE_END(virtual_mapped)
 	/* Do the copies */
 SYM_CODE_START_LOCAL_NOALIGN(swap_pages)
 	UNWIND_HINT_END_OF_STACK
-	movq	%rdi, %rcx	/* Put the page_list in %rcx */
+	movq	%rdi, %rcx	/* Put the indirection_page in %rcx */
 	xorl	%edi, %edi
 	xorl	%esi, %esi
 	jmp	1f
Signature · 1 line
2.45.2
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] x86/kexec: Fix a comment of swap_pages() assembly

Quoted 4 lines · Kai Huang

When relocate_kernel() gets called, %rdi holds 'indirection_page' and %rsi holds 'page_list'. And %rdi always holds 'indirection_page' when swap_pages() is called.

Quoted 8 lines

Therefore the comment of the first line code of swap_pages()

movq %rdi, %rcx /* Put the page_list in %rcx */

.. isn't correct because it actually moves the 'indirection_page' to the %rcx. Fix it.

Looks like it got broken by 4bfaaef01a1b ("[PATCH] Avoid overwriting the current pgd (V4, x86_64)")

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author
#3 lore raw

Re: [PATCH 1/2] x86/kexec: Fix a comment of swap_pages() assembly

Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Kai Huang

When relocate_kernel() gets called, %rdi holds 'indirection_page' and %rsi holds 'page_list'. And %rdi always holds 'indirection_page' when swap_pages() is called.

Therefore the comment of the first line code of swap_pages()

movq %rdi, %rcx /* Put the page_list in %rcx */

.. isn't correct because it actually moves the 'indirection_page' to the %rcx. Fix it.

Quoted 7 lines
Quoted 1 line

Looks like it got broken by 4bfaaef01a1b ("[PATCH] Avoid overwriting the current pgd (V4, x86_64)")

Thanks for finding this. I spent some time yesterday trying to do so but it wasn't obvious to me. :-)

Yes that line was firstly introduced by commit 5234f5eb04abb ("[PATCH] kexec:
x86_64 kexec implementation") but by that time it was correct: %rdi indeed holds 'page_list'. The commit you mentioned above adds a new (first) argument to relocate_kernel() and %rdi was changed to hold 'indirection_page', but the comment was leftover.

But the two commits were introduced at 2004 and 2006, so I don't think it worth any Fixes tag?

#4 lore raw

Re: [PATCH 1/2] x86/kexec: Fix a comment of swap_pages() assembly

Quoted 2 lines · Kai

But the two commits were introduced at 2004 and 2006, so I don't think it worth any Fixes tag?

Nah. It is a comment after all.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author
#5 lore raw

PATCH 2/2 x86/kexec: Add comments around swap_pages() assembly to improve readability

In reply to Kai Huang

The current assembly around swap_pages() in the relocate_kernel() takes some time to follow because the use of registers can be easily lost when the line of assembly goes long. Add a couple of comments to clarify the code around swap_pages() to improve readability.

---
 arch/x86/kernel/relocate_kernel_64.S | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kernel/relocate_kernel_64.S +6 -2
index f7a3ca3dee53..e9e88c342f75 100644
@@ -170,6 +170,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped)
 	wbinvd
 .Lsme_off:
 
+	/* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */
 	movq	%rcx, %r11
 	call	swap_pages
 
@@ -289,18 +290,21 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages)
 	movq	%rcx,   %rsi  /* For ever source page do a copy */
 	andq	$0xfffffffffffff000, %rsi
 
-	movq	%rdi, %rdx
-	movq	%rsi, %rax
+	movq	%rdi, %rdx    /* Save destination page to %rdx */
+	movq	%rsi, %rax    /* Save source page to %rax */
 
+	/* copy source page to swap page */
 	movq	%r10, %rdi
 	movl	$512, %ecx
 	rep ; movsq
 
+	/* copy destination page to source page */
 	movq	%rax, %rdi
 	movq	%rdx, %rsi
 	movl	$512, %ecx
 	rep ; movsq
 
+	/* copy swap page to destination page */
 	movq	%rdx, %rdi
 	movq	%r10, %rsi
 	movl	$512, %ecx
Signature · 1 line
2.45.2
Acked-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] x86/kexec: Add comments around swap_pages() assembly to improve readability

Quoted 6 lines · Kai Huang

The current assembly around swap_pages() in the relocate_kernel() takes some time to follow because the use of registers can be easily lost when the line of assembly goes long. Add a couple of comments to clarify the code around swap_pages() to improve readability.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov