Index | Thread | Search

From:
Vitaliy Makkoveev <mvs@openbsd.org>
Subject:
mp-safe acpiread_filtops
To:
tech@openbsd.org
Date:
Sat, 19 Jul 2025 19:57:23 +0300

Download raw body.

Thread
  • Vitaliy Makkoveev:

    mp-safe acpiread_filtops

acpi_filtread() touches no shared data, so we need to protect only
kevent/knote stuff. Use `sc_note_mtx' for that purpose. IPL_BIO was
used to previous IPL level.

Index: sys/dev/acpi/acpi.c
===================================================================
RCS file: /cvs/src/sys/dev/acpi/acpi.c,v
retrieving revision 1.451
diff -u -p -r1.451 acpi.c
--- sys/dev/acpi/acpi.c	17 Jun 2025 13:01:11 -0000	1.451
+++ sys/dev/acpi/acpi.c	19 Jul 2025 16:52:35 -0000
@@ -1048,6 +1048,9 @@ acpi_attach_common(struct acpi_softc *sc
 	SIMPLEQ_INIT(&sc->sc_pwrresdevs);
 #endif /* NACPIPWRRES > 0 */
 
+	mtx_init(&sc->sc_note_mtx, IPL_BIO);
+	klist_init_mutex(&sc->sc_note, &sc->sc_note_mtx);
+
 	if (acpi_loadtables(sc, rsdp)) {
 		printf(": can't load tables\n");
 		acpi_unmap(&handle);
@@ -3478,7 +3481,7 @@ acpi_record_event(struct acpi_softc *sc,
 		return (1);
 
 	acpi_evindex++;
-	knote_locked(&sc->sc_note, APM_EVENT_COMPOSE(type, acpi_evindex));
+	knote(&sc->sc_note, APM_EVENT_COMPOSE(type, acpi_evindex));
 	return (0);
 }
 
Index: sys/dev/acpi/acpi_apm.c
===================================================================
RCS file: /cvs/src/sys/dev/acpi/acpi_apm.c,v
retrieving revision 1.4
diff -u -p -r1.4 acpi_apm.c
--- sys/dev/acpi/acpi_apm.c	30 Oct 2024 06:16:27 -0000	1.4
+++ sys/dev/acpi/acpi_apm.c	19 Jul 2025 16:52:35 -0000
@@ -163,19 +163,22 @@ acpiioctl(dev_t dev, u_long cmd, caddr_t
 
 void	acpi_filtdetach(struct knote *);
 int	acpi_filtread(struct knote *, long);
+int	acpi_filtmodify(struct kevent *, struct knote *);
+int	acpi_filtprocess(struct knote *, struct kevent *);
 
 const struct filterops acpiread_filtops = {
-	.f_flags	= FILTEROP_ISFD,
+	.f_flags	= FILTEROP_ISFD | FILTEROP_MPSAFE,
 	.f_attach	= NULL,
 	.f_detach	= acpi_filtdetach,
 	.f_event	= acpi_filtread,
+	.f_modify	= acpi_filtmodify,
+	.f_process	= acpi_filtprocess,
 };
 
 int
 acpikqfilter(dev_t dev, struct knote *kn)
 {
 	struct acpi_softc *sc = acpi_softc;
-	int s;
 
 	if (sc == NULL)
 		return (ENXIO);
@@ -190,9 +193,7 @@ acpikqfilter(dev_t dev, struct knote *kn
 
 	kn->kn_hook = sc;
 
-	s = splbio();
-	klist_insert_locked(&sc->sc_note, kn);
-	splx(s);
+	klist_insert(&sc->sc_note, kn);
 
 	return (0);
 }
@@ -201,20 +202,47 @@ void
 acpi_filtdetach(struct knote *kn)
 {
 	struct acpi_softc *sc = kn->kn_hook;
-	int s;
 
-	s = splbio();
-	klist_remove_locked(&sc->sc_note, kn);
-	splx(s);
+	klist_remove(&sc->sc_note, kn);
 }
 
 int
 acpi_filtread(struct knote *kn, long hint)
 {
+	struct acpi_softc *sc = kn->kn_hook;
+
+	MUTEX_ASSERT_LOCKED(&sc->sc_note_mtx);
+
 	/* XXX weird kqueue_scan() semantics */
 	if (hint && !kn->kn_data)
 		kn->kn_data = hint;
 	return (1);
+}
+
+int
+acpi_filtmodify(struct kevent *kev, struct knote *kn)
+{
+	struct acpi_softc *sc = kn->kn_hook;
+	int rv;
+
+	mtx_enter(&sc->sc_note_mtx);
+	rv = knote_modify(kev, kn);
+	mtx_leave(&sc->sc_note_mtx);
+
+	return (rv);
+}
+
+int
+acpi_filtprocess(struct knote *kn, struct kevent *kev)
+{
+	struct acpi_softc *sc = kn->kn_hook;
+	int rv;
+
+	mtx_enter(&sc->sc_note_mtx);
+	rv = knote_process(kn, kev);
+	mtx_leave(&sc->sc_note_mtx);
+
+	return (rv);
 }
 
 #ifdef SUSPEND
Index: sys/dev/acpi/acpivar.h
===================================================================
RCS file: /cvs/src/sys/dev/acpi/acpivar.h,v
retrieving revision 1.137
diff -u -p -r1.137 acpivar.h
--- sys/dev/acpi/acpivar.h	7 Jul 2025 00:55:15 -0000	1.137
+++ sys/dev/acpi/acpivar.h	19 Jul 2025 16:52:35 -0000
@@ -235,6 +235,7 @@ struct acpi_softc {
 	 */
 	struct acpi_facs	*sc_facs;	/* Shared with firmware! */
 
+	struct mutex		sc_note_mtx;
 	struct klist		sc_note;
 	struct acpi_reg_map	sc_pmregs[ACPIREG_MAXREG];
 	bus_space_handle_t	sc_ioh_pm1a_evt;