Module Name:    src
Committed By:   christos
Date:           Sun Nov  1 20:58:38 UTC 2020

Modified Files:
        src/sys/rump/include/rump-sys: kern.h
        src/sys/rump/librump/rumpkern: intr.c scheduler.c sleepq.c
Added Files:
        src/sys/rump/include/sys: sleeptab.h

Log Message:
PR/55664: Ruslan Nikolaev: Fix:

1. A race condition (bug) in sys/rump/librump/rumpkern/intr.c since
   rumpuser_cv_signal() is called without holding a mutex
2. sleepq is implemented using a single (global) conditional
   variable; that should be done per each sleepq separately


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/rump/include/rump-sys/kern.h
cvs rdiff -u -r0 -r1.1 src/sys/rump/include/sys/sleeptab.h
cvs rdiff -u -r1.55 -r1.56 src/sys/rump/librump/rumpkern/intr.c
cvs rdiff -u -r1.51 -r1.52 src/sys/rump/librump/rumpkern/scheduler.c
cvs rdiff -u -r1.20 -r1.21 src/sys/rump/librump/rumpkern/sleepq.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/rump/include/rump-sys/kern.h
diff -u src/sys/rump/include/rump-sys/kern.h:1.4 src/sys/rump/include/rump-sys/kern.h:1.5
--- src/sys/rump/include/rump-sys/kern.h:1.4	Fri Aug 10 17:44:59 2018
+++ src/sys/rump/include/rump-sys/kern.h	Sun Nov  1 15:58:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.h,v 1.4 2018/08/10 21:44:59 pgoyette Exp $	*/
+/*	$NetBSD: kern.h,v 1.5 2020/11/01 20:58:38 christos Exp $	*/
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -174,6 +174,7 @@ void	rump_syscall_boot_establish(const s
 void	rump_schedlock_cv_wait(struct rumpuser_cv *);
 int	rump_schedlock_cv_timedwait(struct rumpuser_cv *,
 				    const struct timespec *);
+void	rump_schedlock_cv_signal(struct cpu_info *, struct rumpuser_cv *);
 
 void	rump_user_schedule(int, void *);
 void	rump_user_unschedule(int, int *, void *);

Index: src/sys/rump/librump/rumpkern/intr.c
diff -u src/sys/rump/librump/rumpkern/intr.c:1.55 src/sys/rump/librump/rumpkern/intr.c:1.56
--- src/sys/rump/librump/rumpkern/intr.c:1.55	Mon Dec 16 17:47:55 2019
+++ src/sys/rump/librump/rumpkern/intr.c	Sun Nov  1 15:58:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.55 2019/12/16 22:47:55 ad Exp $	*/
+/*	$NetBSD: intr.c,v 1.56 2020/11/01 20:58:38 christos Exp $	*/
 
 /*
  * Copyright (c) 2008-2010, 2015 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.55 2019/12/16 22:47:55 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.56 2020/11/01 20:58:38 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -464,7 +464,7 @@ rump_softint_run(struct cpu_info *ci)
 
 	for (i = 0; i < SOFTINT_COUNT; i++) {
 		if (!TAILQ_EMPTY(&si_lvl[i].si_pending))
-			rumpuser_cv_signal(si_lvl[i].si_cv);
+			rump_schedlock_cv_signal(ci, si_lvl[i].si_cv);
 	}
 }
 

Index: src/sys/rump/librump/rumpkern/scheduler.c
diff -u src/sys/rump/librump/rumpkern/scheduler.c:1.51 src/sys/rump/librump/rumpkern/scheduler.c:1.52
--- src/sys/rump/librump/rumpkern/scheduler.c:1.51	Sat Mar 14 14:08:39 2020
+++ src/sys/rump/librump/rumpkern/scheduler.c	Sun Nov  1 15:58:38 2020
@@ -1,4 +1,4 @@
-/*      $NetBSD: scheduler.c,v 1.51 2020/03/14 18:08:39 ad Exp $	*/
+/*      $NetBSD: scheduler.c,v 1.52 2020/11/01 20:58:38 christos Exp $	*/
 
 /*
  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.51 2020/03/14 18:08:39 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: scheduler.c,v 1.52 2020/11/01 20:58:38 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -179,6 +179,16 @@ rump_scheduler_init(int numcpu)
 	mutex_init(&unruntime_lock, MUTEX_DEFAULT, IPL_SCHED);
 }
 
+void
+rump_schedlock_cv_signal(struct cpu_info *ci, struct rumpuser_cv *cv)
+{
+	struct rumpcpu *rcpu = cpuinfo_to_rumpcpu(ci);
+
+	rumpuser_mutex_enter_nowrap(rcpu->rcpu_mtx);
+	rumpuser_cv_signal(cv);
+	rumpuser_mutex_exit(rcpu->rcpu_mtx);
+}
+
 /*
  * condvar ops using scheduler lock as the rumpuser interlock.
  */

Index: src/sys/rump/librump/rumpkern/sleepq.c
diff -u src/sys/rump/librump/rumpkern/sleepq.c:1.20 src/sys/rump/librump/rumpkern/sleepq.c:1.21
--- src/sys/rump/librump/rumpkern/sleepq.c:1.20	Sat Apr 25 11:42:15 2020
+++ src/sys/rump/librump/rumpkern/sleepq.c	Sun Nov  1 15:58:38 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: sleepq.c,v 1.20 2020/04/25 15:42:15 bouyer Exp $	*/
+/*	$NetBSD: sleepq.c,v 1.21 2020/11/01 20:58:38 christos Exp $	*/
 
 /*
  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sleepq.c,v 1.20 2020/04/25 15:42:15 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sleepq.c,v 1.21 2020/11/01 20:58:38 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/condvar.h>
@@ -40,25 +40,20 @@ __KERNEL_RCSID(0, "$NetBSD: sleepq.c,v 1
 #include <rump-sys/kern.h>
 
 syncobj_t sleep_syncobj;
-static kcondvar_t sq_cv;
 
-static int
-sqinit1(void)
+void
+sleepq_init(sleepq_t *sq)
 {
 
-	cv_init(&sq_cv, "sleepq");
-
-	return 0;
+	LIST_INIT(sq);
+	cv_init(&sq->sq_cv, "sleepq");
 }
 
 void
-sleepq_init(sleepq_t *sq)
+sleepq_destroy(sleepq_t *sq)
 {
-	static ONCE_DECL(sqctl);
-
-	RUN_ONCE(&sqctl, sqinit1);
 
-	LIST_INIT(sq);
+	cv_destroy(&sq->sq_cv);
 }
 
 void
@@ -83,7 +78,7 @@ sleepq_block(int timo, bool catch)
 
 	while (l->l_wchan) {
 		l->l_mutex = mp; /* keep sleepq lock until woken up */
-		error = cv_timedwait(&sq_cv, mp, timo);
+		error = cv_timedwait(&l->l_sleepq->sq_cv, mp, timo);
 		if (error == EWOULDBLOCK || error == EINTR) {
 			if (l->l_wchan) {
 				LIST_REMOVE(l, l_sleepchain);
@@ -118,7 +113,7 @@ sleepq_wake(sleepq_t *sq, wchan_t wchan,
 		}
 	}
 	if (found)
-		cv_broadcast(&sq_cv);
+		cv_broadcast(&sq->sq_cv);
 
 	mutex_spin_exit(mp);
 }
@@ -130,7 +125,7 @@ sleepq_unsleep(struct lwp *l, bool clean
 	l->l_wchan = NULL;
 	l->l_wmesg = NULL;
 	LIST_REMOVE(l, l_sleepchain);
-	cv_broadcast(&sq_cv);
+	cv_broadcast(&l->l_sleepq->sq_cv);
 
 	if (cleanup) {
 		mutex_spin_exit(l->l_mutex);

Added files:

Index: src/sys/rump/include/sys/sleeptab.h
diff -u /dev/null src/sys/rump/include/sys/sleeptab.h:1.1
--- /dev/null	Sun Nov  1 15:58:39 2020
+++ src/sys/rump/include/sys/sleeptab.h	Sun Nov  1 15:58:38 2020
@@ -0,0 +1,40 @@
+/*	$NetBSD: sleeptab.h,v 1.1 2020/11/01 20:58:38 christos Exp $	*/
+
+/*
+ * Copyright (c) 2020
+ *     The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef	_RUMP_SYS_SLEEPTAB_H_
+#define	_RUMP_SYS_SLEEPTAB_H_
+
+struct sleepq {
+	LIST_HEAD(, lwp);	/* anonymous struct */
+	kcondvar_t sq_cv;
+};
+
+void    sleepq_destroy(sleepq_t *); 
+
+#endif	/* _RUMP_SYS_SLEEPTAB_H_ */

Reply via email to