Skip to content

Commit 4570522

Browse files
Pull up following revision(s) (requested by riastradh in ticket #60):
sys/arch/sparc/sparc/locore.s: revision 1.287 share/man/man9/Makefile: revision 1.475 sys/arch/mips/mips/cpu_subr.c: revision 1.65 sys/arch/riscv/riscv/cpu_subr.c: revision 1.6 sys/arch/mips/mips/cpu_subr.c: revision 1.66 sys/arch/amd64/amd64/cpufunc.S: revision 1.70 common/lib/libc/arch/i386/atomic/atomic.S: revision 1.38 common/lib/libc/arch/sparc/atomic/membar_ops.S: revision 1.9 sys/arch/hppa/hppa/support.S: revision 1.9 sys/arch/alpha/alpha/locore.s: revision 1.145 share/man/man9/paravirt_membar_sync.9: revision 1.1 sys/arch/sparc64/sparc64/locore.s: revision 1.436 distrib/sets/lists/comp/mi: revision 1.2499 sys/arch/i386/i386/cpufunc.S: revision 1.54 common/lib/libc/arch/sparc64/atomic/membar_ops.S: revision 1.10 sys/sys/paravirt_membar.h: revision 1.1 sys/arch/arm/arm/cpu_subr.c: revision 1.6 sys/arch/virt68k/virt68k/locore.s: revision 1.17 common/lib/libc/arch/x86_64/atomic/atomic.S: revision 1.32 paravirt_membar_sync(9): New memory barrier. For use in paravirtualized drivers which require store-before-load ordering -- irrespective of whether the kernel is built for a single processor, or whether the (virtual) machine is booted with a single processor. This is even required on architectures that don't even have a store-before-load ordering barrier, like m68k; adding, e.g., a virtio bus is _as if_ the architecture has been extended with relaxed memory ordering when talking with that new bus. Such architectures need some way to request the hypervisor enforce that ordering -- on m68k, that's done by issuing a CASL instruction, which qemu maps to an atomic r/m/w with sequential consistency ordering in the host. PR kern/59618: occasional virtio block device lock ups/hangs mips: Fix asm arch options in new paravirt_membar_sync. Need to explicitly enable mips2 (MIPS-II) instructions in order to use sync. Fixes: /tmp/ccxgOmXc.s: Assembler messages: /tmp/ccxgOmXc.s:3576: Error: opcode not supported on this processor: mips1 (mips1) `sync' --- cpu_subr.o --- *** Failed target: cpu_subr.o PR kern/59618: occasional virtio block device lock ups/hangs
1 parent 4a7d33e commit 4570522

File tree

18 files changed

+423
-37
lines changed

18 files changed

+423
-37
lines changed

common/lib/libc/arch/i386/atomic/atomic.S

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: atomic.S,v 1.37 2024/07/16 22:44:38 riastradh Exp $ */
1+
/* $NetBSD: atomic.S,v 1.37.2.1 2025/10/19 10:29:20 martin Exp $ */
22

33
/*-
44
* Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -212,7 +212,8 @@ ENTRY(_membar_sync)
212212
* https://shipilev.net/blog/2014/on-the-fence-with-dependencies/
213213
* https://www.agner.org/optimize/instruction_tables.pdf
214214
*
215-
* Sync with xen_mb in sys/arch/i386/i386/cpufunc.S.
215+
* Sync with paravirt_membar_sync in
216+
* sys/arch/i386/i386/cpufunc.S.
216217
*/
217218
LOCK
218219
addl $0, -4(%esp)

common/lib/libc/arch/sparc/atomic/membar_ops.S

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: membar_ops.S,v 1.8 2022/04/09 23:32:52 riastradh Exp $ */
1+
/* $NetBSD: membar_ops.S,v 1.8.10.1 2025/10/19 10:29:20 martin Exp $ */
22

33
/*-
44
* Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -42,11 +42,16 @@
4242
* store-before-load but nothing else. Hence, only membar_sync (and
4343
* its deprecated alias membar_enter) must issue anything --
4444
* specifically, an LDSTUB, which (along with SWAP) is the only
45-
* instruction that implies a sequential consistency barrier.
45+
* instruction that implies a sequential consistency barrier. (SWAP is
46+
* not available in sparcv7, and maybe not all sparcv8, so we use
47+
* LDSTUB here.)
4648
*
4749
* If we ran with Partial Store Order (PSO), we would also need to
4850
* issue STBAR for membar_release (load/store-before-store) and
4951
* membar_producer (store-before-store).
52+
*
53+
* Sync membar_sync with paravirt_membar_sync in
54+
* sys/arch/sparc/sparc/locore.s.
5055
*/
5156

5257
ENTRY(_membar_acquire)

common/lib/libc/arch/sparc64/atomic/membar_ops.S

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: membar_ops.S,v 1.9 2022/04/09 23:32:52 riastradh Exp $ */
1+
/* $NetBSD: membar_ops.S,v 1.9.10.1 2025/10/19 10:29:21 martin Exp $ */
22

33
/*-
44
* Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -72,6 +72,9 @@ ENTRY(_membar_sync)
7272
* https://www.oracle.com/technetwork/server-storage/sun-sparc-enterprise/documentation/sparc-2i-usersmanual-2516677.pdf#page=518
7373
*
7474
* So let's avoid doing that.
75+
*
76+
* Sync with paravirt_membar_sync in
77+
* sys/arch/sparc64/sparc64/locore.s.
7578
*/
7679
membar #StoreLoad
7780
retl

common/lib/libc/arch/x86_64/atomic/atomic.S

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: atomic.S,v 1.31 2024/07/16 22:45:10 riastradh Exp $ */
1+
/* $NetBSD: atomic.S,v 1.31.2.1 2025/10/19 10:29:22 martin Exp $ */
22

33
/*-
44
* Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -287,7 +287,8 @@ ENTRY(_membar_sync)
287287
* https://shipilev.net/blog/2014/on-the-fence-with-dependencies/
288288
* https://www.agner.org/optimize/instruction_tables.pdf
289289
*
290-
* Sync with xen_mb in sys/arch/amd64/amd64/cpufunc.S.
290+
* Sync with paravirt_membar_sync in
291+
* sys/arch/amd64/amd64/cpufunc.S.
291292
*/
292293
LOCK
293294
addq $0, -8(%rsp)

distrib/sets/lists/comp/mi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $NetBSD: mi,v 1.2497.2.2 2025/10/11 10:46:48 martin Exp $
1+
# $NetBSD: mi,v 1.2497.2.3 2025/10/19 10:29:20 martin Exp $
22
#
33
# Note: don't delete entries from here - mark them as "obsolete" instead.
44
./etc/mtree/set.comp comp-sys-root
@@ -12893,6 +12893,7 @@
1289312893
./usr/share/man/cat9/optstr_get.0 comp-sys-catman .cat
1289412894
./usr/share/man/cat9/p_find.0 comp-obsolete obsolete
1289512895
./usr/share/man/cat9/panic.0 comp-sys-catman .cat
12896+
./usr/share/man/cat9/paravirt_membar_sync.0 comp-sys-catman .cat
1289612897
./usr/share/man/cat9/pathbuf.0 comp-sys-catman .cat
1289712898
./usr/share/man/cat9/pci.0 comp-sys-catman .cat
1289812899
./usr/share/man/cat9/pci_conf_hook.0 comp-sys-catman .cat
@@ -21757,6 +21758,7 @@
2175721758
./usr/share/man/html9/optstr_get.html comp-sys-htmlman html
2175821759
./usr/share/man/html9/p_find.html comp-obsolete obsolete
2175921760
./usr/share/man/html9/panic.html comp-sys-htmlman html
21761+
./usr/share/man/html9/paravirt_membar_sync.html comp-sys-htmlman html
2176021762
./usr/share/man/html9/pathbuf.html comp-sys-htmlman html
2176121763
./usr/share/man/html9/pci.html comp-sys-htmlman html
2176221764
./usr/share/man/html9/pci_conf_hook.html comp-sys-htmlman html
@@ -30778,6 +30780,7 @@
3077830780
./usr/share/man/man9/optstr_get.9 comp-sys-man .man
3077930781
./usr/share/man/man9/p_find.9 comp-obsolete obsolete
3078030782
./usr/share/man/man9/panic.9 comp-sys-man .man
30783+
./usr/share/man/man9/paravirt_membar_sync.9 comp-sys-man .man
3078130784
./usr/share/man/man9/pathbuf.9 comp-sys-man .man
3078230785
./usr/share/man/man9/pci.9 comp-sys-man .man
3078330786
./usr/share/man/man9/pci_conf_hook.9 comp-sys-man .man

share/man/man9/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $NetBSD: Makefile,v 1.474 2025/06/21 17:52:52 gutteridge Exp $
1+
# $NetBSD: Makefile,v 1.474.2.1 2025/10/19 10:29:19 martin Exp $
22

33
# Makefile for section 9 (kernel function and variable) manual pages.
44

@@ -41,7 +41,12 @@ MAN= accept_filter.9 accf_data.9 accf_http.9 acl.9 \
4141
microseq.9 microtime.9 microuptime.9 mi_switch.9 module.9 \
4242
mstohz.9 mutex.9 m_tag.9 namecache.9 \
4343
namei.9 nullop.9 opencrypto.9 optstr.9 \
44-
panic.9 pathbuf.9 pci.9 pci_configure_bus.9 pci_intr.9 \
44+
panic.9 \
45+
paravirt_membar_sync.9 \
46+
pathbuf.9 \
47+
pci.9 \
48+
pci_configure_bus.9 \
49+
pci_intr.9 \
4550
pci_msi.9 pckbport.9 pcmcia.9 pcq.9 pcu.9 \
4651
percpu.9 pfil.9 physio.9 pktqueue.9 pmap.9 pmatch.9 pmf.9 pool.9 \
4752
pool_cache.9 portfeatures.9 powerhook_establish.9 ppsratecheck.9 \
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
.\" $NetBSD: paravirt_membar_sync.9,v 1.1.2.2 2025/10/19 10:29:19 martin Exp $
2+
.\"
3+
.\" Copyright (c) 2025 The NetBSD Foundation
4+
.\" All rights reserved.
5+
.\"
6+
.\" Redistribution and use in source and binary forms, with or without
7+
.\" modification, are permitted provided that the following conditions
8+
.\" are met:
9+
.\" 1. Redistributions of source code must retain the above copyright
10+
.\" notice, this list of conditions and the following disclaimer.
11+
.\" 2. Redistributions in binary form must reproduce the above copyright
12+
.\" notice, this list of conditions and the following disclaimer in the
13+
.\" documentation and/or other materials provided with the distribution.
14+
.\"
15+
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16+
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17+
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18+
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19+
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21+
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22+
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23+
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24+
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25+
.\" POSSIBILITY OF SUCH DAMAGE.
26+
.\"
27+
.Dd August 31, 2025
28+
.Dt PARAVIRT_MEMBAR_SYNC 9
29+
.Os
30+
.Sh NAME
31+
.Nm paravirt_membar_sync
32+
.Nd memory barrier for paravirtualized device drivers
33+
.Sh SYNOPSIS
34+
.In sys/paravirt_membar.h
35+
.Ft void
36+
.Fn paravirt_membar_sync "void"
37+
.Sh DESCRIPTION
38+
The
39+
.Nm
40+
function issues a store-before-load barrier for coordination with a
41+
paravirtualized device.
42+
.Pp
43+
This function has the same ordering semantics as
44+
.Xr membar_sync 3 ,
45+
but
46+
.Xr membar_sync 3
47+
can only coordinate with other CPUs that
48+
.Nx
49+
is running on.
50+
In a virtual machine,
51+
.Nx
52+
may be running on a single
53+
.Em virtual
54+
CPU, and patch
55+
.Xr membar_sync 3
56+
to be a no-op, while the host side of a paravirtualized device may be
57+
running on a different
58+
.Em physical
59+
CPU requiring a barrier that
60+
.Xr membar_sync 3
61+
does not issue.
62+
.Sh EXAMPLES
63+
Submit a request to the host device, and notify the host to process
64+
it\(embut elide the notification, which is expensive, if the host is
65+
already reading requests anyway:
66+
.Bd -literal
67+
/*
68+
* Write the request into the ring buffer.
69+
*/
70+
memcpy(cputodev_ring->buffer[sc->sc_cputodev_idx], request,
71+
sizeof(*request));
72+
73+
/*
74+
* Publish the request to the host device side.
75+
*/
76+
cputodev_ring->header->producer_tail = ++sc->sc_cputodev_idx;
77+
78+
/*
79+
* Ensure we have published it _before_ we check whether the
80+
* host needs notification.
81+
*/
82+
paravirt_membar_sync();
83+
84+
/*
85+
* Notify the host, if needed. Notifying the host is usually
86+
* expensive (trap to hypervisor), so we try to avoid it if not
87+
* needed.
88+
*/
89+
if (cputodev_ring->header->needs_notification)
90+
notify_host();
91+
.Ed
92+
.Pp
93+
Enable interrupts from the host and check whether any were pending
94+
while interrupts were disabled:
95+
.Bd -literal
96+
/*
97+
* Tell the host device to deliver interrupts after this
98+
* point.
99+
*/
100+
restart:
101+
devtocpu_ring->header->needs_notification = true;
102+
103+
/*
104+
* Ensure we have requested interrupts _before_ we check
105+
* whether we missed any notifications.
106+
*/
107+
paravirt_membar_sync();
108+
109+
/*
110+
* Check whether there were any pending notifications while
111+
* interrupts were blocked. If not, stop here.
112+
*/
113+
idx = devtocpu_ring->header->producer_idx;
114+
if (sc->sc_devtocpu_idx == idx)
115+
return;
116+
117+
/*
118+
* Process the notifications.
119+
*/
120+
devtocpu_ring->header->needs_notification = false;
121+
while (sc->sc_devtocpu_idx != idx) {
122+
struct buffer *buf =
123+
devtocpu_ring->buffer[sc->sc_devtocpu_idx];
124+
process_notification(buf);
125+
sc->sc_devtocpu_idx++;
126+
sc->sc_devtocpu_idx %= ringlen;
127+
}
128+
goto restart;
129+
.Ed
130+
.Pp
131+
.Sy "N.B.:"
132+
Other ordering or bouncing may be required with
133+
.Xr bus_dmamap_sync 9 ;
134+
this is independent of
135+
.Nm ,
136+
which is needed
137+
.Em in addition to
138+
.Xr bus_dmamap_sync 9
139+
to guarantee store-before-load ordering when there is no intervening
140+
I/O doorbell trigger for a DMA operation, nor interrupt delivery for a
141+
DMA completion.
142+
.Sh SEE ALSO
143+
.Xr membar_ops 3 ,
144+
.Xr bus_dma 9 ,
145+
.Xr bus_space 9
146+
.Sh HISTORY
147+
These atomic operations first appeared in
148+
.Nx 12.0 .

sys/arch/alpha/alpha/locore.s

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: locore.s,v 1.144 2025/06/24 11:18:16 andvar Exp $ */
1+
/* $NetBSD: locore.s,v 1.144.2.1 2025/10/19 10:29:20 martin Exp $ */
22

33
/*-
44
* Copyright (c) 1999, 2000, 2019 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
6767

6868
#include <machine/asm.h>
6969

70-
__KERNEL_RCSID(0, "$NetBSD: locore.s,v 1.144 2025/06/24 11:18:16 andvar Exp $");
70+
__KERNEL_RCSID(0, "$NetBSD: locore.s,v 1.144.2.1 2025/10/19 10:29:20 martin Exp $");
7171

7272
#include "assym.h"
7373

@@ -1524,3 +1524,19 @@ LEAF(alpha_write_fpcr, 1); f30save = 0; fpcrtmp = 8; framesz = 16
15241524
lda sp, framesz(sp)
15251525
RET
15261526
END(alpha_write_fpcr)
1527+
1528+
LEAF(paravirt_membar_sync, 0)
1529+
/*
1530+
* Store-before-load ordering with respect to matching logic
1531+
* on the hypervisor side.
1532+
*
1533+
* This is the same as membar_sync, but without hotpatching
1534+
* away the MB instruction on uniprocessor boots -- because
1535+
* under virtualization, we still have to coordinate with a
1536+
* `device' backed by a hypervisor that is potentially on
1537+
* another physical CPU even if we observe only one virtual CPU
1538+
* as the guest.
1539+
*/
1540+
mb
1541+
RET
1542+
END(paravirt_membar_sync)

sys/arch/amd64/amd64/cpufunc.S

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: cpufunc.S,v 1.69 2025/05/23 02:48:19 riastradh Exp $ */
1+
/* $NetBSD: cpufunc.S,v 1.69.2.1 2025/10/19 10:29:19 martin Exp $ */
22

33
/*
44
* Copyright (c) 1998, 2007, 2008, 2020, 2023 The NetBSD Foundation, Inc.
@@ -61,17 +61,17 @@ ENTRY(x86_mfence)
6161
ret
6262
END(x86_mfence)
6363

64-
#ifdef XEN
65-
ENTRY(xen_mb)
64+
ENTRY(paravirt_membar_sync)
6665
/*
6766
* Store-before-load ordering with respect to matching logic
6867
* on the hypervisor side.
6968
*
7069
* This is the same as membar_sync, but without hotpatching
7170
* away the LOCK prefix on uniprocessor boots -- because under
72-
* Xen, we still have to coordinate with a `device' backed by a
73-
* hypervisor that is potentially on another physical CPU even
74-
* if we observe only one virtual CPU as the guest.
71+
* virtualization, we still have to coordinate with a `device'
72+
* backed by a hypervisor that is potentially on another
73+
* physical CPU even if we observe only one virtual CPU as the
74+
* guest.
7575
*
7676
* See common/lib/libc/arch/x86_64/atomic/atomic.S for
7777
* rationale and keep this in sync with the implementation
@@ -80,7 +80,10 @@ ENTRY(xen_mb)
8080
lock
8181
addq $0,-8(%rsp)
8282
ret
83-
END(xen_mb)
83+
END(paravirt_membar_sync)
84+
85+
#ifdef XEN
86+
STRONG_ALIAS(xen_mb,paravirt_membar_sync)
8487
#endif /* XEN */
8588

8689
#ifdef KDTRACE_HOOKS

0 commit comments

Comments
 (0)