git: b53133a7789b - main - proc: load/store p_cowgen using atomic primitives

From: Mateusz Guzik <mjg_at_FreeBSD.org>
Date: Sun, 13 Feb 2022 13:07:15 UTC
The branch main has been updated by mjg:

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

commit b53133a7789b2cf17944a16248fbad4e75e6b432
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2022-02-12 19:33:34 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2022-02-13 13:07:08 +0000

    proc: load/store p_cowgen using atomic primitives
---
 sys/amd64/amd64/trap.c     | 2 +-
 sys/arm/arm/trap-v6.c      | 2 +-
 sys/i386/i386/trap.c       | 2 +-
 sys/kern/subr_syscall.c    | 2 +-
 sys/kern/subr_trap.c       | 2 +-
 sys/powerpc/powerpc/trap.c | 2 +-
 sys/sys/proc.h             | 5 +++--
 7 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/sys/amd64/amd64/trap.c b/sys/amd64/amd64/trap.c
index ff4bccebed5b..8d8bdd6fbb6e 100644
--- a/sys/amd64/amd64/trap.c
+++ b/sys/amd64/amd64/trap.c
@@ -309,7 +309,7 @@ trap(struct trapframe *frame)
 		td->td_pticks = 0;
 		td->td_frame = frame;
 		addr = frame->tf_rip;
-		if (td->td_cowgen != p->p_cowgen)
+		if (td->td_cowgen != atomic_load_int(&p->p_cowgen))
 			thread_cow_update(td);
 
 		switch (type) {
diff --git a/sys/arm/arm/trap-v6.c b/sys/arm/arm/trap-v6.c
index 7bd5780af338..13eca6efb9bf 100644
--- a/sys/arm/arm/trap-v6.c
+++ b/sys/arm/arm/trap-v6.c
@@ -418,7 +418,7 @@ abort_handler(struct trapframe *tf, int prefetch)
 	p = td->td_proc;
 	if (usermode) {
 		td->td_pticks = 0;
-		if (td->td_cowgen != p->p_cowgen)
+		if (td->td_cowgen != atomic_load_int(&p->p_cowgen))
 			thread_cow_update(td);
 	}
 
diff --git a/sys/i386/i386/trap.c b/sys/i386/i386/trap.c
index d770cf808f5f..9da5a177906c 100644
--- a/sys/i386/i386/trap.c
+++ b/sys/i386/i386/trap.c
@@ -301,7 +301,7 @@ trap(struct trapframe *frame)
 		td->td_pticks = 0;
 		td->td_frame = frame;
 		addr = frame->tf_eip;
-		if (td->td_cowgen != p->p_cowgen)
+		if (td->td_cowgen != atomic_load_int(&p->p_cowgen))
 			thread_cow_update(td);
 
 		switch (type) {
diff --git a/sys/kern/subr_syscall.c b/sys/kern/subr_syscall.c
index fa5dcb482f2c..33dd50d3d50a 100644
--- a/sys/kern/subr_syscall.c
+++ b/sys/kern/subr_syscall.c
@@ -68,7 +68,7 @@ syscallenter(struct thread *td)
 	sa = &td->td_sa;
 
 	td->td_pticks = 0;
-	if (__predict_false(td->td_cowgen != p->p_cowgen))
+	if (__predict_false(td->td_cowgen != atomic_load_int(&p->p_cowgen)))
 		thread_cow_update(td);
 	traced = (p->p_flag & P_TRACED) != 0;
 	if (__predict_false(traced || td->td_dbgflags & TDB_USERWR)) {
diff --git a/sys/kern/subr_trap.c b/sys/kern/subr_trap.c
index ec8f686a497d..52559c162249 100644
--- a/sys/kern/subr_trap.c
+++ b/sys/kern/subr_trap.c
@@ -247,7 +247,7 @@ ast(struct trapframe *framep)
 	thread_unlock(td);
 	VM_CNT_INC(v_trap);
 
-	if (td->td_cowgen != p->p_cowgen)
+	if (td->td_cowgen != atomic_load_int(&p->p_cowgen))
 		thread_cow_update(td);
 	if (td->td_pflags & TDP_OWEUPC && p->p_flag & P_PROFIL) {
 		addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
diff --git a/sys/powerpc/powerpc/trap.c b/sys/powerpc/powerpc/trap.c
index ab5189a0f418..3fb1badd7ba8 100644
--- a/sys/powerpc/powerpc/trap.c
+++ b/sys/powerpc/powerpc/trap.c
@@ -254,7 +254,7 @@ trap(struct trapframe *frame)
 		td->td_pticks = 0;
 		td->td_frame = frame;
 		addr = frame->srr0;
-		if (td->td_cowgen != p->p_cowgen)
+		if (td->td_cowgen != atomic_load_int(&p->p_cowgen))
 			thread_cow_update(td);
 
 		/* User Mode Traps */
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index 0e33192303f4..71105239e40e 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -1005,8 +1005,9 @@ extern pid_t pid_max;
 } while (0)
 
 #define	PROC_UPDATE_COW(p) do {						\
-	PROC_LOCK_ASSERT((p), MA_OWNED);				\
-	(p)->p_cowgen++;						\
+	struct proc *_p = (p);						\
+	PROC_LOCK_ASSERT((_p), MA_OWNED);				\
+	atomic_store_int(&_p->p_cowgen, _p->p_cowgen + 1);		\
 } while (0)
 
 #define	PROC_COW_CHANGECOUNT(td, p) ({					\