Port-arm archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CURRENT broken on Raspberry Pi 2?



>thorpej%me.com@localhost (Jason Thorpe) writes:
>
>>Might I suggest a reference count?  RUN_ONCE() can initialize the mutex around the counter, and then the counter can keep track of the init's and fini's called, and only do the work on the 0->1 edge and the 1->0 edge
>.
>
>You also need to destroy the mutex somewhen.
>
>RUN_ONCE needs to learn a bit more about life cycles.

I made INIT_ONCE(9) and FINI_ONCE(9) with reference counter.
Is this OK like this?

cvs -q diff -aup sys/once.h kern/subr_once.c external/bsd/dwc2/dwc2.c external/bsd/common/linux/linux_work.c
Index: sys/once.h
===================================================================
RCS file: /src/cvs/cvsroot-netbsd/src/sys/sys/once.h,v
retrieving revision 1.6
diff -a -u -p -r1.6 once.h
--- sys/once.h	3 Mar 2018 19:21:59 -0000	1.6
+++ sys/once.h	6 Mar 2019 19:32:36 -0000
@@ -31,8 +31,9 @@
 #define	_SYS_ONCE_H_
 
 typedef struct {
-	unsigned o_status;
 	int o_error;
+	uint16_t o_refcnt;
+	uint16_t o_status;
 #define	ONCE_VIRGIN	0
 #define	ONCE_RUNNING	1
 #define	ONCE_DONE	2
@@ -40,14 +41,20 @@ typedef struct {
 
 void once_init(void);
 int _run_once(once_t *, int (*)(void));
+int _init_once(once_t *, int (*)(void));
+void _fini_once(once_t *, void (*)(void));
 
 #define	ONCE_DECL(o) \
-	once_t (o) __read_mostly = { \
+	once_t (o) = { \
 		.o_status = 0, \
+		.o_refcnt = 0, \
 	};
 
 #define	RUN_ONCE(o, fn) \
     (__predict_true((o)->o_status == ONCE_DONE) ? \
       ((o)->o_error) : _run_once((o), (fn)))
 
+#define	INIT_ONCE(o, fn)	_init_once((o), (fn))
+#define	FINI_ONCE(o, fn)	_fini_once((o), (fn))
+
 #endif /* _SYS_ONCE_H_ */
Index: kern/subr_once.c
===================================================================
RCS file: /src/cvs/cvsroot-netbsd/src/sys/kern/subr_once.c,v
retrieving revision 1.6
diff -a -u -p -r1.6 subr_once.c
--- kern/subr_once.c	15 Mar 2009 17:14:40 -0000	1.6
+++ kern/subr_once.c	6 Mar 2019 19:29:33 -0000
@@ -69,3 +69,48 @@ _run_once(once_t *o, int (*fn)(void))
 	KASSERT(o->o_status == ONCE_DONE);
 	return o->o_error;
 }
+
+int
+_init_once(once_t *o, int (*fn)(void))
+{
+	int error;
+
+	mutex_enter(&oncemtx);
+	while (o->o_status == ONCE_RUNNING)
+		cv_wait(&oncecv, &oncemtx);
+
+	if (o->o_refcnt++ == 0) {
+		o->o_status = ONCE_RUNNING;
+		mutex_exit(&oncemtx);
+		o->o_errori = fn();
+		mutex_enter(&oncemtx);
+		o->o_status = ONCE_DONE;
+	}
+
+	while (o->o_status == ONCE_RUNNING)
+		cv_wait(&oncecv, &oncemtx);
+	error = o->o_error;
+	mutex_exit(&oncemtx);
+
+	return error;
+}
+
+void
+_fini_once(once_t *o, void (*fn)(void))
+{
+	mutex_enter(&oncemtx);
+	while (o->o_status == ONCE_RUNNING)
+		cv_wait(&oncecv, &oncemtx);
+
+	if (--o->o_refcnt == 0) {
+		o->o_status = ONCE_RUNNING;
+		mutex_exit(&oncemtx);
+		fn();
+		mutex_enter(&oncemtx);
+		o->o_status = ONCE_VIRGIN;
+	}
+
+	while (o->o_status == ONCE_RUNNING)
+		cv_wait(&oncecv, &oncemtx);
+	mutex_exit(&oncemtx);
+}
Index: external/bsd/dwc2/dwc2.c
===================================================================
RCS file: /src/cvs/cvsroot-netbsd/src/sys/external/bsd/dwc2/dwc2.c,v
retrieving revision 1.58
diff -a -u -p -r1.58 dwc2.c
--- external/bsd/dwc2/dwc2.c	17 Feb 2019 04:17:52 -0000	1.58
+++ external/bsd/dwc2/dwc2.c	6 Mar 2019 10:55:11 -0000
@@ -1273,6 +1273,10 @@ dwc2_init(struct dwc2_softc *sc)
 {
 	int err = 0;
 
+	err = linux_workqueue_init();
+	if (err)
+		return err;
+
 	sc->sc_bus.ub_hcpriv = sc;
 	sc->sc_bus.ub_revision = USBREV_2_0;
 	sc->sc_bus.ub_methods = &dwc2_bus_methods;
Index: external/bsd/common/linux/linux_work.c
===================================================================
RCS file: /src/cvs/cvsroot-netbsd/src/sys/external/bsd/common/linux/linux_work.c,v
retrieving revision 1.43
diff -a -u -p -r1.43 linux_work.c
--- external/bsd/common/linux/linux_work.c	27 Aug 2018 15:25:43 -0000	1.43
+++ external/bsd/common/linux/linux_work.c	6 Mar 2019 19:30:40 -0000
@@ -41,6 +41,7 @@ __KERNEL_RCSID(0, "$NetBSD: linux_work.c
 #include <sys/kthread.h>
 #include <sys/lwp.h>
 #include <sys/mutex.h>
+#include <sys/once.h>
 #include <sys/queue.h>
 #include <sys/sdt.h>
 
@@ -130,8 +131,8 @@ atomic_cas_uintptr(volatile uintptr_t *p
  *	Initialize the Linux workqueue subsystem.  Return 0 on success,
  *	NetBSD error on failure.
  */
-int
-linux_workqueue_init(void)
+static int
+linux_workqueue_init0(void)
 {
 	int error;
 
@@ -173,8 +174,8 @@ fail0:	KASSERT(error);
  *
  *	Destroy the Linux workqueue subsystem.  Never fails.
  */
-void
-linux_workqueue_fini(void)
+static void
+linux_workqueue_fini0(void)
 {
 
 	destroy_workqueue(system_power_efficient_wq);
@@ -182,6 +183,20 @@ linux_workqueue_fini(void)
 	destroy_workqueue(system_wq);
 	lwp_specific_key_delete(workqueue_key);
 }
+
+static ONCE_DECL(linux_workqueue_init_once);
+
+int
+linux_workqueue_init(void)
+{
+	return INIT_ONCE(&linux_workqueue_init_once, &linux_workqueue_init0);
+}
+
+void
+linux_workqueue_fini(void)
+{
+	return FINI_ONCE(&linux_workqueue_init_once, &linux_workqueue_fini0);
+}
 
 /*
  * Workqueues


Home | Main Index | Thread Index | Old Index