Source-Changes-HG archive

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

[src/trunk]: src/sys/external/bsd Move Linux SRCU to common.



details:   https://anonhg.NetBSD.org/src/rev/9874a653f26e
branches:  trunk
changeset: 1028010:9874a653f26e
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sun Dec 19 01:37:27 2021 +0000

description:
Move Linux SRCU to common.

diffstat:

 sys/external/bsd/common/conf/files.linux       |    3 +-
 sys/external/bsd/common/include/linux/srcu.h   |   59 ++++
 sys/external/bsd/common/linux/linux_srcu.c     |  307 +++++++++++++++++++++++++
 sys/external/bsd/drm2/include/linux/srcu.h     |   59 ----
 sys/external/bsd/drm2/linux/files.drmkms_linux |    3 +-
 sys/external/bsd/drm2/linux/linux_srcu.c       |  307 -------------------------
 6 files changed, 369 insertions(+), 369 deletions(-)

diffs (truncated from 780 to 300 lines):

diff -r f8adbc0159d0 -r 9874a653f26e sys/external/bsd/common/conf/files.linux
--- a/sys/external/bsd/common/conf/files.linux  Sun Dec 19 01:35:35 2021 +0000
+++ b/sys/external/bsd/common/conf/files.linux  Sun Dec 19 01:37:27 2021 +0000
@@ -1,9 +1,10 @@
-#       $NetBSD: files.linux,v 1.3 2021/12/19 01:33:17 riastradh Exp $
+#       $NetBSD: files.linux,v 1.4 2021/12/19 01:37:27 riastradh Exp $
 
 define linux
 
 makeoptions    linux   CPPFLAGS+="-I$S/external/bsd/common/include"
 
 file   external/bsd/common/linux/linux_rcu.c           linux
+file   external/bsd/common/linux/linux_srcu.c          linux
 file   external/bsd/common/linux/linux_tasklet.c       linux
 file   external/bsd/common/linux/linux_work.c          linux
diff -r f8adbc0159d0 -r 9874a653f26e sys/external/bsd/common/include/linux/srcu.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/external/bsd/common/include/linux/srcu.h      Sun Dec 19 01:37:27 2021 +0000
@@ -0,0 +1,59 @@
+/*     $NetBSD: srcu.h,v 1.1 2021/12/19 01:37:27 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.
+ */
+
+#ifndef        _LINUX_SRCU_H_
+#define        _LINUX_SRCU_H_
+
+#include <sys/types.h>
+#include <sys/condvar.h>
+#include <sys/mutex.h>
+
+struct lwp;
+struct percpu;
+
+struct srcu {
+       struct percpu           *srcu_percpu;   /* struct srcu_cpu */
+       kmutex_t                srcu_lock;
+       kcondvar_t              srcu_cv;
+       struct lwp              *srcu_sync;
+       int64_t                 srcu_total;
+       volatile unsigned       srcu_gen;
+};
+
+void   srcu_init(struct srcu *, const char *);
+void   srcu_fini(struct srcu *);
+
+int    srcu_read_lock(struct srcu *);
+void   srcu_read_unlock(struct srcu *, int);
+
+void   synchronize_srcu(struct srcu *);
+
+#endif /* _LINUX_SRCU_H_ */
diff -r f8adbc0159d0 -r 9874a653f26e sys/external/bsd/common/linux/linux_srcu.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/external/bsd/common/linux/linux_srcu.c        Sun Dec 19 01:37:27 2021 +0000
@@ -0,0 +1,307 @@
+/*     $NetBSD: linux_srcu.c,v 1.1 2021/12/19 01:37:27 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_srcu.c,v 1.1 2021/12/19 01:37:27 riastradh Exp $");
+
+/*
+ * SRCU: Sleepable RCU
+ *
+ *     (This is not exactly SRCU as Linux implements it; it is my
+ *     approximation of the semantics I think we need.)
+ *
+ *     For each srcu context, representing a related set of read
+ *     sections, on each CPU we store two counts of numbers of
+ *     readers in two epochs: active readers and draining readers.
+ *
+ *     All new srcu read sections get counted in the active epoch.
+ *     When there's no synchronize_srcu in progress, the draining
+ *     epoch has zero readers.  When a thread calls synchronize_srcu,
+ *     which must be serialized by the caller, it it swaps the sense
+ *     of the epochs, issues an xcall to collect a global count of the
+ *     number of readers in the now-draining epoch, and waits for the
+ *     remainder to complete.
+ *
+ *     This is basically NetBSD localcount(9), but without the
+ *     restriction that the caller of localcount_drain must guarantee
+ *     no new readers -- srcu uses two counts per CPU instead of one
+ *     like localcount(9), and synchronize_srcu just waits for all
+ *     existing readers to drain while new oness count toward a new
+ *     epoch.
+ */
+
+#include <sys/types.h>
+#include <sys/condvar.h>
+#include <sys/mutex.h>
+#include <sys/percpu.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+#include <sys/xcall.h>
+
+#include <linux/srcu.h>
+
+struct srcu_cpu {
+       int64_t src_count[2];
+};
+
+/*
+ * srcu_init(srcu, name)
+ *
+ *     Initialize the srcu state with the specified name.  Caller must
+ *     call srcu_fini when done.
+ *
+ *     name should be no longer than 8 characters; longer will be
+ *     truncated.
+ *
+ *     May sleep.
+ */
+void
+srcu_init(struct srcu *srcu, const char *name)
+{
+
+       ASSERT_SLEEPABLE();
+
+       srcu->srcu_percpu = percpu_alloc(sizeof(struct srcu_cpu));
+       mutex_init(&srcu->srcu_lock, MUTEX_DEFAULT, IPL_VM);
+       cv_init(&srcu->srcu_cv, name);
+       srcu->srcu_sync = NULL;
+       srcu->srcu_total = 0;
+       srcu->srcu_gen = 0;
+}
+
+/*
+ * srcu_fini(srcu)
+ *
+ *     Finalize an srcu state, which must not be in use right now.  If
+ *     any srcu read sections might be active, caller must wait for
+ *     them to complete with synchronize_srcu.
+ *
+ *     May sleep.
+ */
+void
+srcu_fini(struct srcu *srcu)
+{
+
+       ASSERT_SLEEPABLE();
+
+       KASSERTMSG((srcu->srcu_sync == NULL),
+           "srcu_fini in lwp %p while synchronize_srcu running in lwp %p",
+           curlwp, srcu->srcu_sync);
+       cv_destroy(&srcu->srcu_cv);
+       mutex_destroy(&srcu->srcu_lock);
+       percpu_free(srcu->srcu_percpu, sizeof(struct srcu_cpu));
+}
+
+/*
+ * srcu_adjust(srcu, gen, delta)
+ *
+ *     Internal subroutine: Add delta to the local CPU's count of
+ *     readers in the generation gen.
+ *
+ *     Never sleeps.
+ */
+static void
+srcu_adjust(struct srcu *srcu, unsigned gen, int delta)
+{
+       struct srcu_cpu *cpu;
+       unsigned epoch = gen & 1; /* active epoch */
+
+       cpu = percpu_getref(srcu->srcu_percpu);
+       cpu->src_count[epoch] += delta;
+       percpu_putref(srcu->srcu_percpu);
+}
+
+/*
+ * srcu_read_lock(srcu)
+ *
+ *     Enter an srcu read section and return a ticket for it.  Any
+ *     subsequent synchronize_srcu will wait until this thread calls
+ *     srcu_read_unlock(srcu, ticket).
+ *
+ *     Never sleeps.
+ */
+int
+srcu_read_lock(struct srcu *srcu)
+{
+       unsigned gen;
+
+       /*
+        * Prevent xcall while we fetch the generation and adjust the
+        * count.
+        */
+       kpreempt_disable();
+       gen = srcu->srcu_gen;
+       /* Fetch the generation once before incrementing the count.  */
+       __insn_barrier();
+       srcu_adjust(srcu, gen, +1);
+       kpreempt_enable();
+
+       /*
+        * Increment the count in our generation before doing anything
+        * else on this CPU.
+        *
+        * No stronger, inter-CPU memory barrier is needed: if there is
+        * a concurrent synchronize_srcu, it will issue an xcall that
+        * functions as a stronger memory barrier.
+        */
+       __insn_barrier();
+
+       return gen;
+}
+
+/*
+ * srcu_read_unlock(srcu, ticket)
+ *
+ *     Exit an srcu read section started with srcu_read_lock returning
+ *     ticket.  If there is a pending synchronize_srcu and we might be
+ *     the last reader, notify it.
+ *
+ *     Never sleeps.
+ */
+void
+srcu_read_unlock(struct srcu *srcu, int ticket)
+{
+       unsigned gen = ticket;
+
+       /*
+        * Make sure all side effects have completed on this CPU before
+        * decrementing the count.
+        *
+        * No stronger, inter-CPU memory barrier is needed: if there is
+        * a concurrent synchronize_srcu, it will issue an xcall that
+        * functions as a stronger memory barrier.
+        */
+       __insn_barrier();
+
+       /*
+        * Prevent xcall while we determine whether we need to notify a
+        * sync and decrement the count in our generation.
+        */
+       kpreempt_disable();
+       if (__predict_true(gen == srcu->srcu_gen)) {
+               /*
+                * Fast path: just decrement the local count.  If a
+                * sync has begun and incremented gen after we observed
+                * it, it will issue an xcall that will run after this
+                * kpreempt_disable section to collect our local count.
+                */
+               srcu_adjust(srcu, gen, -1);
+       } else {
+               /*



Home | Main Index | Thread Index | Old Index