Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 27 May 2026 14:59:22 +0000
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 187b46e1c4d7 - main - cpu_fork: Remove redundant assignments to td_pcb and td_frame
Message-ID:  <6a1706ca.23e13.4f26e07f@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=187b46e1c4d761efe2e92f662d68c0f49cd04b44

commit 187b46e1c4d761efe2e92f662d68c0f49cd04b44
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2026-03-26 17:39:52 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2026-05-27 13:43:43 +0000

    cpu_fork: Remove redundant assignments to td_pcb and td_frame
    
    cpu_thread_alloc() already sets these fields anytime td_kstack changes.
    
    Reviewed by:    kib, andrew (arm changes)
    Sponsored by:   AFRL, DARPA
    Pull Request:   https://ron-dev.freebsd.org/FreeBSD/src/pulls/23
---
 sys/amd64/amd64/vm_machdep.c     |  6 +-----
 sys/arm/arm/vm_machdep.c         |  8 ++------
 sys/arm64/arm64/vm_machdep.c     |  9 ++-------
 sys/i386/i386/vm_machdep.c       | 12 +++++-------
 sys/powerpc/powerpc/vm_machdep.c |  8 ++------
 sys/riscv/riscv/vm_machdep.c     |  2 --
 6 files changed, 12 insertions(+), 33 deletions(-)

diff --git a/sys/amd64/amd64/vm_machdep.c b/sys/amd64/amd64/vm_machdep.c
index 2e180003e93d..e37f122dffa3 100644
--- a/sys/amd64/amd64/vm_machdep.c
+++ b/sys/amd64/amd64/vm_machdep.c
@@ -167,8 +167,6 @@ copy_thread(struct thread *td1, struct thread *td2)
 		clear_pcb_flags(pcb2, PCB_TLSBASE);
 	}
 
-	td2->td_frame = (struct trapframe *)td2->td_md.md_stack_base - 1;
-
 	/*
 	 * Set registers for trampoline to user mode.  Leave space for the
 	 * return address on stack.  These are the kernel mode register values.
@@ -240,9 +238,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 		return;
 	}
 
-	/* Point the stack and pcb to the actual location */
-	set_top_of_stack_td(td2);
-	td2->td_pcb = pcb2 = get_pcb_td(td2);
+	pcb2 = td2->td_pcb;
 
 	copy_thread(td1, td2);
 
diff --git a/sys/arm/arm/vm_machdep.c b/sys/arm/arm/vm_machdep.c
index bee1c705fbbd..b1197255c281 100644
--- a/sys/arm/arm/vm_machdep.c
+++ b/sys/arm/arm/vm_machdep.c
@@ -97,9 +97,6 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	if ((flags & RFPROC) == 0)
 		return;
 
-	/* Point the pcb to the top of the stack */
-	pcb2 = (struct pcb *)
-	    (td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1;
 #ifdef VFP
 	/* Store actual state of VFP */
 	if (curthread == td1) {
@@ -107,7 +104,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 			vfp_save_state(td1, td1->td_pcb);
 	}
 #endif
-	td2->td_pcb = pcb2;
+	pcb2 = td2->td_pcb;
 
 	/* Clone td1's pcb */
 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
@@ -116,8 +113,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	mdp2 = &p2->p_md;
 	bcopy(&td1->td_proc->p_md, mdp2, sizeof(*mdp2));
 
-	/* Point the frame to the stack in front of pcb and copy td1's frame */
-	td2->td_frame = (struct trapframe *)pcb2 - 1;
+	/* Copy td1's frame */
 	*td2->td_frame = *td1->td_frame;
 
 	/*
diff --git a/sys/arm64/arm64/vm_machdep.c b/sys/arm64/arm64/vm_machdep.c
index 4cb87ca9856e..01542848ab56 100644
--- a/sys/arm64/arm64/vm_machdep.c
+++ b/sys/arm64/arm64/vm_machdep.c
@@ -89,10 +89,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 #endif
 	}
 
-	pcb2 = (struct pcb *)(td2->td_kstack +
-	    td2->td_kstack_pages * PAGE_SIZE) - 1;
-
-	td2->td_pcb = pcb2;
+	pcb2 = td2->td_pcb;
 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
 
 	/* Clear the debug register state. */
@@ -100,14 +97,12 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 
 	ptrauth_fork(td2, td1);
 
-	tf = STACKALIGN((struct trapframe *)pcb2 - 1);
+	tf = td2->td_frame;
 	bcopy(td1->td_frame, tf, sizeof(*tf));
 	tf->tf_x[0] = 0;
 	tf->tf_x[1] = 0;
 	tf->tf_spsr = td1->td_frame->tf_spsr & (PSR_M_32 | PSR_DAIF);
 
-	td2->td_frame = tf;
-
 	/* Set the return value registers for fork() */
 	td2->td_pcb->pcb_x[PCB_X19] = (uintptr_t)fork_return;
 	td2->td_pcb->pcb_x[PCB_X20] = (uintptr_t)td2;
diff --git a/sys/i386/i386/vm_machdep.c b/sys/i386/i386/vm_machdep.c
index 672ec9360c35..003f00070bff 100644
--- a/sys/i386/i386/vm_machdep.c
+++ b/sys/i386/i386/vm_machdep.c
@@ -232,9 +232,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 		return;
 	}
 
-	/* Point the pcb to the top of the stack */
-	pcb2 = get_pcb_td(td2);
-	td2->td_pcb = pcb2;
+	pcb2 = td2->td_pcb;
 
 	copy_thread(td1, td2);
 
@@ -248,11 +246,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	/*
 	 * Copy the trap frame for the return to user mode as if from a
 	 * syscall.  This copies most of the user mode register values.
-	 * The -VM86_STACK_SPACE (-16) is so we can expand the trapframe
-	 * if we go to vm86.
 	 */
-	td2->td_frame = (struct trapframe *)((caddr_t)td2->td_pcb -
-	    VM86_STACK_SPACE) - 1;
 	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
 
 	/* Set child return values. */
@@ -381,6 +375,10 @@ cpu_thread_alloc(struct thread *td)
 	struct pcb *pcb;
 	struct xstate_hdr *xhdr;
 
+	/*
+	 * The -VM86_STACK_SPACE (-16) is so we can expand the trapframe
+	 * if we go to vm86.
+	 */
 	td->td_pcb = pcb = get_pcb_td(td);
 	td->td_frame = (struct trapframe *)((caddr_t)pcb -
 	    VM86_STACK_SPACE) - 1;
diff --git a/sys/powerpc/powerpc/vm_machdep.c b/sys/powerpc/powerpc/vm_machdep.c
index 00fdc301a7e7..1dc28739ad7c 100644
--- a/sys/powerpc/powerpc/vm_machdep.c
+++ b/sys/powerpc/powerpc/vm_machdep.c
@@ -123,9 +123,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	if (td1 == curthread)
 		cpu_update_pcb(td1);
 
-	pcb = (struct pcb *)__align_down(td2->td_kstack +
-	    td2->td_kstack_pages * PAGE_SIZE - sizeof(struct pcb), 0x40);
-	td2->td_pcb = pcb;
+	pcb = td2->td_pcb;
 
 	/* Copy the pcb */
 	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
@@ -135,7 +133,7 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	 * Copy the trap frame for the return to user mode as if from a
 	 * syscall.  This copies most of the user mode register values.
 	 */
-	tf = (struct trapframe *)pcb - 1;
+	tf = td2->td_frame;
 	bcopy(td1->td_frame, tf, sizeof(*tf));
 
 	/* Set up trap frame. */
@@ -143,8 +141,6 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	tf->fixreg[FIRSTARG + 1] = 0;
 	tf->cr &= ~0x10000000;
 
-	td2->td_frame = tf;
-
 	cf = (struct callframe *)tf - 1;
 	memset(cf, 0, sizeof(struct callframe));
 	#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
diff --git a/sys/riscv/riscv/vm_machdep.c b/sys/riscv/riscv/vm_machdep.c
index 206110157233..e718d522e572 100644
--- a/sys/riscv/riscv/vm_machdep.c
+++ b/sys/riscv/riscv/vm_machdep.c
@@ -100,8 +100,6 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 		critical_exit();
 	}
 
-	cpu_set_pcb_frame(td2);
-
 	pcb2 = td2->td_pcb;
 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
 


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6a1706ca.23e13.4f26e07f>