Module Name:    src
Committed By:   martin
Date:           Mon Jul 31 14:59:25 UTC 2023

Modified Files:
        src/sys/arch/xen/include [netbsd-8]: xen.h
        src/sys/arch/xen/x86 [netbsd-8]: xen_intr.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1862):

        sys/arch/xen/x86/xen_intr.c: revision 1.31 (patch)
        sys/arch/xen/include/xen.h (apply patch)

xen_intr.c: Use kpreempt_disable/enable around access to curcpu().

curcpu() is not otherwise guaranteed to be stable at these points.

While here, nix nonsensical membars.  This need only be synchronized
with interrupts on the same CPU.

Proposed on port-xen:
https://mail-index.netbsd.org/port-xen/2022/07/13/msg010250.html


To generate a diff of this commit:
cvs rdiff -u -r1.37.10.1 -r1.37.10.2 src/sys/arch/xen/include/xen.h
cvs rdiff -u -r1.9 -r1.9.58.1 src/sys/arch/xen/x86/xen_intr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/include/xen.h
diff -u src/sys/arch/xen/include/xen.h:1.37.10.1 src/sys/arch/xen/include/xen.h:1.37.10.2
--- src/sys/arch/xen/include/xen.h:1.37.10.1	Sat Oct 13 17:16:12 2018
+++ src/sys/arch/xen/include/xen.h	Mon Jul 31 14:59:25 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: xen.h,v 1.37.10.1 2018/10/13 17:16:12 martin Exp $	*/
+/*	$NetBSD: xen.h,v 1.37.10.2 2023/07/31 14:59:25 martin Exp $	*/
 
 /*
  *
@@ -142,34 +142,46 @@ void xpq_flush_cache(void);
 
 #define __save_flags(x)							\
 do {									\
+	kpreempt_disable();						\
 	(x) = curcpu()->ci_vcpu->evtchn_upcall_mask;			\
+	kpreempt_enable();						\
 } while (0)
 
 #define __restore_flags(x)						\
 do {									\
-	volatile struct vcpu_info *_vci = curcpu()->ci_vcpu;		\
+	uint8_t _flags = (x);						\
+	struct cpu_info *_ci;						\
+									\
+	kpreempt_disable();						\
+	_ci = curcpu();							\
+	_ci->ci_vcpu->evtchn_upcall_mask = _flags;			\
 	__insn_barrier();						\
-	if ((_vci->evtchn_upcall_mask = (x)) == 0) {			\
-		x86_lfence();						\
-		if (__predict_false(_vci->evtchn_upcall_pending))	\
-			hypervisor_force_callback();			\
-	}								\
+	if (__predict_false(_ci->ci_vcpu->evtchn_upcall_pending &&	\
+		_flags == 0))						\
+		hypervisor_force_callback();				\
+	kpreempt_enable();						\
 } while (0)
 
 #define __cli()								\
 do {									\
+	kpreempt_disable();						\
 	curcpu()->ci_vcpu->evtchn_upcall_mask = 1;			\
-	x86_lfence();							\
+	kpreempt_enable();						\
+	__insn_barrier();						\
 } while (0)
 
 #define __sti()								\
 do {									\
-	volatile struct vcpu_info *_vci = curcpu()->ci_vcpu;		\
+	struct cpu_info *_ci;						\
+									\
+	__insn_barrier();						\
+	kpreempt_disable();						\
+	_ci = curcpu();							\
+	_ci->ci_vcpu->evtchn_upcall_mask = 0;				\
 	__insn_barrier();						\
-	_vci->evtchn_upcall_mask = 0;					\
-	x86_lfence(); /* unmask then check (avoid races) */		\
-	if (__predict_false(_vci->evtchn_upcall_pending))		\
+	if (__predict_false(_ci->ci_vcpu->evtchn_upcall_pending))	\
 		hypervisor_force_callback();				\
+	kpreempt_enable();						\
 } while (0)
 
 #define cli()			__cli()

Index: src/sys/arch/xen/x86/xen_intr.c
diff -u src/sys/arch/xen/x86/xen_intr.c:1.9 src/sys/arch/xen/x86/xen_intr.c:1.9.58.1
--- src/sys/arch/xen/x86/xen_intr.c:1.9	Fri Jan 16 20:16:47 2009
+++ src/sys/arch/xen/x86/xen_intr.c	Mon Jul 31 14:59:25 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: xen_intr.c,v 1.9 2009/01/16 20:16:47 jym Exp $	*/
+/*	$NetBSD: xen_intr.c,v 1.9.58.1 2023/07/31 14:59:25 martin Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xen_intr.c,v 1.9 2009/01/16 20:16:47 jym Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xen_intr.c,v 1.9.58.1 2023/07/31 14:59:25 martin Exp $");
 
 #include <sys/param.h>
 
@@ -97,18 +97,25 @@ x86_enable_intr(void)
 u_long
 x86_read_psl(void)
 {
+	u_long psl;
+
+	kpreempt_disable();
+	psl = curcpu()->ci_vcpu->evtchn_upcall_mask;
+	kpreempt_enable();
 
-	return (curcpu()->ci_vcpu->evtchn_upcall_mask);
+	return psl;
 }
 
 void
 x86_write_psl(u_long psl)
 {
-	struct cpu_info *ci = curcpu();
+	struct cpu_info *ci;
 
+	kpreempt_disable();
+	ci = curcpu();
 	ci->ci_vcpu->evtchn_upcall_mask = psl;
-	xen_rmb();
-	if (ci->ci_vcpu->evtchn_upcall_pending && psl == 0) {
+	__insn_barrier();
+	if (__predict_false(ci->ci_vcpu->evtchn_upcall_pending) && psl == 0)
 	    	hypervisor_force_callback();
-	}
+	kpreempt_enable();
 }

Reply via email to