Module Name:    src
Committed By:   riastradh
Date:           Mon Aug 27 13:31:37 UTC 2018

Modified Files:
        src/sys/external/bsd/drm2/include/linux: rcupdate.h types.h
        src/sys/external/bsd/drm2/linux: files.drmkms_linux linux_module.c
Added Files:
        src/sys/external/bsd/drm2/linux: linux_rcu.c

Log Message:
Implement some of the Linux RCU API.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/external/bsd/drm2/include/linux/rcupdate.h
cvs rdiff -u -r1.7 -r1.8 src/sys/external/bsd/drm2/include/linux/types.h
cvs rdiff -u -r1.11 -r1.12 src/sys/external/bsd/drm2/linux/files.drmkms_linux
cvs rdiff -u -r1.6 -r1.7 src/sys/external/bsd/drm2/linux/linux_module.c
cvs rdiff -u -r0 -r1.1 src/sys/external/bsd/drm2/linux/linux_rcu.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/external/bsd/drm2/include/linux/rcupdate.h
diff -u src/sys/external/bsd/drm2/include/linux/rcupdate.h:1.4 src/sys/external/bsd/drm2/include/linux/rcupdate.h:1.5
--- src/sys/external/bsd/drm2/include/linux/rcupdate.h:1.4	Mon Aug 27 07:34:23 2018
+++ src/sys/external/bsd/drm2/include/linux/rcupdate.h	Mon Aug 27 13:31:36 2018
@@ -1,7 +1,7 @@
-/*	$NetBSD: rcupdate.h,v 1.4 2018/08/27 07:34:23 riastradh Exp $	*/
+/*	$NetBSD: rcupdate.h,v 1.5 2018/08/27 13:31:36 riastradh Exp $	*/
 
 /*-
- * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
@@ -33,8 +33,8 @@
 #define _LINUX_RCUPDATE_H_
 
 #include <sys/atomic.h>
-#include <sys/null.h>
-#include <sys/xcall.h>
+#include <sys/cdefs.h>
+#include <sys/systm.h>
 
 #define	__rcu
 
@@ -44,15 +44,31 @@
 } while (0)
 
 
-#define	rcu_dereference(P)	rcu_dereference_protected((P), 1)
+#define	rcu_dereference(P) ({						      \
+	typeof(*(P)) *__rcu_dereference_tmp = (P);			      \
+	membar_datadep_consumer();					      \
+	__rcu_dereference_tmp;						      \
+})
 
 #define	rcu_dereference_protected(P, C) ({				      \
 	WARN_ON(!(C));							      \
-	typeof(*(P)) *__rcu_dereference_protected_tmp = (P);		      \
-	membar_datadep_consumer();					      \
-	__rcu_dereference_protected_tmp;				      \
+	(P);								      \
 })
 
+struct rcu_head {
+	void		(*rcuh_callback)(struct rcu_head *);
+	struct rcu_head	*rcuh_next;
+};
+
+#define	linux_call_rcu		call_rcu
+#define	linux_synchronize_rcu	synchronize_rcu
+
+int	linux_rcu_gc_init(void);
+void	linux_rcu_gc_fini(void);
+
+void	call_rcu(struct rcu_head *, void (*)(struct rcu_head *));
+void	synchronize_rcu(void);
+
 static inline void
 rcu_read_lock(void)
 {
@@ -69,15 +85,4 @@ rcu_read_unlock(void)
 	kpreempt_enable();
 }
 
-static inline void
-synchronize_rcu_xc(void *a, void *b)
-{
-}
-
-static inline void
-synchronize_rcu(void)
-{
-	xc_wait(xc_broadcast(0, &synchronize_rcu_xc, NULL, NULL));
-}
-
 #endif  /* _LINUX_RCUPDATE_H_ */

Index: src/sys/external/bsd/drm2/include/linux/types.h
diff -u src/sys/external/bsd/drm2/include/linux/types.h:1.7 src/sys/external/bsd/drm2/include/linux/types.h:1.8
--- src/sys/external/bsd/drm2/include/linux/types.h:1.7	Mon Aug 27 07:25:55 2018
+++ src/sys/external/bsd/drm2/include/linux/types.h	Mon Aug 27 13:31:36 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: types.h,v 1.7 2018/08/27 07:25:55 riastradh Exp $	*/
+/*	$NetBSD: types.h,v 1.8 2018/08/27 13:31:36 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -88,10 +88,6 @@ typedef unsigned long long cycles_t;
 /* XXX Not sure this is correct.  */
 typedef off_t loff_t;
 
-struct rcu_head {
-
-};
-
 #define DECLARE_BITMAP(NAME, BITS)					      \
 	unsigned long NAME[((BITS) + ((NBBY*sizeof(unsigned long)) - 1)) /    \
 		(NBBY*sizeof(unsigned long))]

Index: src/sys/external/bsd/drm2/linux/files.drmkms_linux
diff -u src/sys/external/bsd/drm2/linux/files.drmkms_linux:1.11 src/sys/external/bsd/drm2/linux/files.drmkms_linux:1.12
--- src/sys/external/bsd/drm2/linux/files.drmkms_linux:1.11	Mon Aug 27 07:57:56 2018
+++ src/sys/external/bsd/drm2/linux/files.drmkms_linux	Mon Aug 27 13:31:37 2018
@@ -1,4 +1,4 @@
-#       $NetBSD: files.drmkms_linux,v 1.11 2018/08/27 07:57:56 riastradh Exp $
+#       $NetBSD: files.drmkms_linux,v 1.12 2018/08/27 13:31:37 riastradh Exp $
 
 define	drmkms_linux: i2cexec, i2c_bitbang
 
@@ -11,5 +11,6 @@ file	external/bsd/drm2/linux/linux_idr.c
 file	external/bsd/drm2/linux/linux_kmap.c		drmkms_linux
 file	external/bsd/drm2/linux/linux_list_sort.c	drmkms_linux
 file	external/bsd/drm2/linux/linux_module.c		drmkms_linux
+file	external/bsd/drm2/linux/linux_rcu.c		drmkms_linux
 file	external/bsd/drm2/linux/linux_writecomb.c	drmkms_linux
 file	external/bsd/drm2/linux/linux_ww_mutex.c	drmkms_linux

Index: src/sys/external/bsd/drm2/linux/linux_module.c
diff -u src/sys/external/bsd/drm2/linux/linux_module.c:1.6 src/sys/external/bsd/drm2/linux/linux_module.c:1.7
--- src/sys/external/bsd/drm2/linux/linux_module.c:1.6	Mon Apr 13 22:24:34 2015
+++ src/sys/external/bsd/drm2/linux/linux_module.c	Mon Aug 27 13:31:37 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_module.c,v 1.6 2015/04/13 22:24:34 pgoyette Exp $	*/
+/*	$NetBSD: linux_module.c,v 1.7 2018/08/27 13:31:37 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: linux_module.c,v 1.6 2015/04/13 22:24:34 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_module.c,v 1.7 2018/08/27 13:31:37 riastradh Exp $");
 
 #include <sys/module.h>
 #ifndef _MODULE
@@ -41,6 +41,7 @@ __KERNEL_RCSID(0, "$NetBSD: linux_module
 #include <linux/idr.h>
 #include <linux/io.h>
 #include <linux/mutex.h>
+#include <linux/rcupdate.h>
 #include <linux/reservation.h>
 #include <linux/workqueue.h>
 
@@ -65,24 +66,31 @@ linux_init(void)
 		goto fail1;
 	}
 
+	error = linux_rcu_gc_init();
+	if (error) {
+		printf("linux: unable to initialize rcu gc: %d\n", error);
+		goto fail2;
+	}
+
 	error = linux_workqueue_init();
 	if (error) {
 		printf("linux: unable to initialize workqueues: %d\n", error);
-		goto fail2;
+		goto fail3;
 	}
 
 	error = linux_writecomb_init();
 	if (error) {
 		printf("linux: unable to initialize write-combining: %d\n",
 		    error);
-		goto fail3;
+		goto fail4;
 	}
 
 	return 0;
 
-fail4: __unused
+fail5: __unused
 	linux_writecomb_fini();
-fail3:	linux_workqueue_fini();
+fail4:	linux_workqueue_fini();
+fail3:	linux_rcu_gc_fini();
 fail2:	linux_kmap_fini();
 fail1:	linux_idr_module_fini();
 fail0:	return error;
@@ -107,6 +115,7 @@ linux_fini(void)
 
 	linux_writecomb_fini();
 	linux_workqueue_fini();
+	linux_rcu_gc_fini();
 	linux_kmap_fini();
 	linux_idr_module_fini();
 }

Added files:

Index: src/sys/external/bsd/drm2/linux/linux_rcu.c
diff -u /dev/null src/sys/external/bsd/drm2/linux/linux_rcu.c:1.1
--- /dev/null	Mon Aug 27 13:31:37 2018
+++ src/sys/external/bsd/drm2/linux/linux_rcu.c	Mon Aug 27 13:31:37 2018
@@ -0,0 +1,149 @@
+/*	$NetBSD: linux_rcu.c,v 1.1 2018/08/27 13:31:37 riastradh Exp $	*/
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Taylor R. Campbell.
+ *
+ * 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: linux_rcu.c,v 1.1 2018/08/27 13:31:37 riastradh Exp $");
+
+#include <sys/types.h>
+#include <sys/condvar.h>
+#include <sys/kthread.h>
+#include <sys/mutex.h>
+#include <sys/xcall.h>
+
+#include <linux/rcupdate.h>
+
+struct {
+	kmutex_t	lock;
+	kcondvar_t	cv;
+	struct rcu_head	*first;
+	struct lwp	*lwp;
+	bool		dying;
+} gc __cacheline_aligned;
+
+static void
+rcu_xc(void *a, void *b)
+{
+}
+
+void
+synchronize_rcu(void)
+{
+
+	xc_wait(xc_broadcast(0, &rcu_xc, NULL, NULL));
+}
+
+void
+call_rcu(struct rcu_head *head, void (*callback)(struct rcu_head *))
+{
+
+	head->rcuh_callback = callback;
+
+	mutex_enter(&gc.lock);
+	head->rcuh_next = gc.first;
+	gc.first = head;
+	cv_signal(&gc.cv);
+	mutex_exit(&gc.lock);
+}
+
+static void
+gc_thread(void *cookie)
+{
+	struct rcu_head *head, *next;
+
+	mutex_enter(&gc.lock);
+	for (;;) {
+		/* Wait for a task or death notice.  */
+		while ((head = gc.first) == NULL && !gc.dying)
+			cv_wait(&gc.cv, &gc.lock);
+
+		/* If we got a list of callbacks, run them.  */
+		if (head != NULL) {
+			gc.first = NULL;	/* mine */
+			mutex_exit(&gc.lock);
+
+			/* Wait for activity on all CPUs.  */
+			synchronize_rcu();
+
+			/* It is now safe to call the callbacks.  */
+			for (; head != NULL; head = next) {
+				next = head->rcuh_next;
+				(*head->rcuh_callback)(head);
+			}
+
+			mutex_enter(&gc.lock);
+		}
+
+		/* If we're asked to close shop, do so.  */
+		if (gc.dying)
+			break;
+	}
+	mutex_exit(&gc.lock);
+}
+
+int
+linux_rcu_gc_init(void)
+{
+	int error;
+
+	mutex_init(&gc.lock, MUTEX_DEFAULT, IPL_VM);
+	cv_init(&gc.cv, "lnxrcugc");
+	gc.first = NULL;
+	gc.dying = false;
+
+	error = kthread_create(PRI_NONE,
+	    KTHREAD_MPSAFE|KTHREAD_TS|KTHREAD_MUSTJOIN, NULL, &gc_thread, NULL,
+	    &gc.lwp, "lnxrcugc");
+	if (error)
+		goto fail;
+
+	/* Success!  */
+	return 0;
+
+fail:	cv_destroy(&gc.cv);
+	mutex_destroy(&gc.lock);
+	return error;
+}
+
+void
+linux_rcu_gc_fini(void)
+{
+
+	mutex_enter(&gc.lock);
+	gc.dying = true;
+	cv_signal(&gc.cv);
+	mutex_exit(&gc.lock);
+
+	kthread_join(gc.lwp);
+	gc.lwp = NULL;
+	KASSERT(gc.first == NULL);
+	cv_destroy(&gc.cv);
+	mutex_destroy(&gc.lock);
+}

Reply via email to