All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kent Overstreet <kent.overstreet@linux.dev>
To: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	linux-fsdevel@vger.kernel.org
Cc: Kent Overstreet <kent.overstreet@linux.dev>,
	tglx@linutronix.de, x86@kernel.org, tj@kernel.org,
	peterz@infradead.org, mathieu.desnoyers@efficios.com,
	paulmck@kernel.org, keescook@chromium.org,
	dave.hansen@linux.intel.com, mingo@redhat.com, will@kernel.org,
	longman@redhat.com, boqun.feng@gmail.com, brauner@kernel.org
Subject: [PATCH 21/50] locking/seqlock: Split out seqlock_types.h
Date: Fri, 15 Dec 2023 22:26:20 -0500	[thread overview]
Message-ID: <20231216032651.3553101-11-kent.overstreet@linux.dev> (raw)
In-Reply-To: <20231216032651.3553101-1-kent.overstreet@linux.dev>

Trimming down sched.h dependencies: we don't want to include more than
the base types.

Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: Waiman Long <longman@redhat.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
---
 include/linux/sched.h         |  2 +-
 include/linux/seqlock.h       | 79 +----------------------------
 include/linux/seqlock_types.h | 93 +++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+), 78 deletions(-)
 create mode 100644 include/linux/seqlock_types.h

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 6d803d0904d9..436f7ce1450a 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -33,7 +33,7 @@
 #include <linux/task_io_accounting.h>
 #include <linux/posix-timers_types.h>
 #include <linux/rseq.h>
-#include <linux/seqlock.h>
+#include <linux/seqlock_types.h>
 #include <linux/kcsan.h>
 #include <linux/rv.h>
 #include <linux/livepatch_sched.h>
diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
index e92f9d5577ba..d90d8ee29d81 100644
--- a/include/linux/seqlock.h
+++ b/include/linux/seqlock.h
@@ -18,6 +18,7 @@
 #include <linux/lockdep.h>
 #include <linux/mutex.h>
 #include <linux/preempt.h>
+#include <linux/seqlock_types.h>
 #include <linux/spinlock.h>
 
 #include <asm/processor.h>
@@ -37,37 +38,6 @@
  */
 #define KCSAN_SEQLOCK_REGION_MAX 1000
 
-/*
- * Sequence counters (seqcount_t)
- *
- * This is the raw counting mechanism, without any writer protection.
- *
- * Write side critical sections must be serialized and non-preemptible.
- *
- * If readers can be invoked from hardirq or softirq contexts,
- * interrupts or bottom halves must also be respectively disabled before
- * entering the write section.
- *
- * This mechanism can't be used if the protected data contains pointers,
- * as the writer can invalidate a pointer that a reader is following.
- *
- * If the write serialization mechanism is one of the common kernel
- * locking primitives, use a sequence counter with associated lock
- * (seqcount_LOCKNAME_t) instead.
- *
- * If it's desired to automatically handle the sequence counter writer
- * serialization and non-preemptibility requirements, use a sequential
- * lock (seqlock_t) instead.
- *
- * See Documentation/locking/seqlock.rst
- */
-typedef struct seqcount {
-	unsigned sequence;
-#ifdef CONFIG_DEBUG_LOCK_ALLOC
-	struct lockdep_map dep_map;
-#endif
-} seqcount_t;
-
 static inline void __seqcount_init(seqcount_t *s, const char *name,
 					  struct lock_class_key *key)
 {
@@ -131,28 +101,6 @@ static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
  * See Documentation/locking/seqlock.rst
  */
 
-/*
- * For PREEMPT_RT, seqcount_LOCKNAME_t write side critical sections cannot
- * disable preemption. It can lead to higher latencies, and the write side
- * sections will not be able to acquire locks which become sleeping locks
- * (e.g. spinlock_t).
- *
- * To remain preemptible while avoiding a possible livelock caused by the
- * reader preempting the writer, use a different technique: let the reader
- * detect if a seqcount_LOCKNAME_t writer is in progress. If that is the
- * case, acquire then release the associated LOCKNAME writer serialization
- * lock. This will allow any possibly-preempted writer to make progress
- * until the end of its writer serialization lock critical section.
- *
- * This lock-unlock technique must be implemented for all of PREEMPT_RT
- * sleeping locks.  See Documentation/locking/locktypes.rst
- */
-#if defined(CONFIG_LOCKDEP) || defined(CONFIG_PREEMPT_RT)
-#define __SEQ_LOCK(expr)	expr
-#else
-#define __SEQ_LOCK(expr)
-#endif
-
 /*
  * typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
  * @seqcount:	The real sequence counter
@@ -194,11 +142,6 @@ static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
  * @lockbase:		prefix for associated lock/unlock
  */
 #define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase)	\
-typedef struct seqcount_##lockname {					\
-	seqcount_t		seqcount;				\
-	__SEQ_LOCK(locktype	*lock);					\
-} seqcount_##lockname##_t;						\
-									\
 static __always_inline seqcount_t *					\
 __seqprop_##lockname##_ptr(seqcount_##lockname##_t *s)			\
 {									\
@@ -284,6 +227,7 @@ SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t,  false,    raw_spin)
 SEQCOUNT_LOCKNAME(spinlock,     spinlock_t,      __SEQ_RT, spin)
 SEQCOUNT_LOCKNAME(rwlock,       rwlock_t,        __SEQ_RT, read)
 SEQCOUNT_LOCKNAME(mutex,        struct mutex,    true,     mutex)
+#undef SEQCOUNT_LOCKNAME
 
 /*
  * SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
@@ -794,25 +738,6 @@ static inline void raw_write_seqcount_latch(seqcount_latch_t *s)
 	smp_wmb();      /* increment "sequence" before following stores */
 }
 
-/*
- * Sequential locks (seqlock_t)
- *
- * Sequence counters with an embedded spinlock for writer serialization
- * and non-preemptibility.
- *
- * For more info, see:
- *    - Comments on top of seqcount_t
- *    - Documentation/locking/seqlock.rst
- */
-typedef struct {
-	/*
-	 * Make sure that readers don't starve writers on PREEMPT_RT: use
-	 * seqcount_spinlock_t instead of seqcount_t. Check __SEQ_LOCK().
-	 */
-	seqcount_spinlock_t seqcount;
-	spinlock_t lock;
-} seqlock_t;
-
 #define __SEQLOCK_UNLOCKED(lockname)					\
 	{								\
 		.seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \
diff --git a/include/linux/seqlock_types.h b/include/linux/seqlock_types.h
new file mode 100644
index 000000000000..dfdf43e3fa3d
--- /dev/null
+++ b/include/linux/seqlock_types.h
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_SEQLOCK_TYPES_H
+#define __LINUX_SEQLOCK_TYPES_H
+
+#include <linux/lockdep_types.h>
+#include <linux/mutex_types.h>
+#include <linux/spinlock_types.h>
+
+/*
+ * Sequence counters (seqcount_t)
+ *
+ * This is the raw counting mechanism, without any writer protection.
+ *
+ * Write side critical sections must be serialized and non-preemptible.
+ *
+ * If readers can be invoked from hardirq or softirq contexts,
+ * interrupts or bottom halves must also be respectively disabled before
+ * entering the write section.
+ *
+ * This mechanism can't be used if the protected data contains pointers,
+ * as the writer can invalidate a pointer that a reader is following.
+ *
+ * If the write serialization mechanism is one of the common kernel
+ * locking primitives, use a sequence counter with associated lock
+ * (seqcount_LOCKNAME_t) instead.
+ *
+ * If it's desired to automatically handle the sequence counter writer
+ * serialization and non-preemptibility requirements, use a sequential
+ * lock (seqlock_t) instead.
+ *
+ * See Documentation/locking/seqlock.rst
+ */
+typedef struct seqcount {
+	unsigned sequence;
+#ifdef CONFIG_DEBUG_LOCK_ALLOC
+	struct lockdep_map dep_map;
+#endif
+} seqcount_t;
+
+/*
+ * For PREEMPT_RT, seqcount_LOCKNAME_t write side critical sections cannot
+ * disable preemption. It can lead to higher latencies, and the write side
+ * sections will not be able to acquire locks which become sleeping locks
+ * (e.g. spinlock_t).
+ *
+ * To remain preemptible while avoiding a possible livelock caused by the
+ * reader preempting the writer, use a different technique: let the reader
+ * detect if a seqcount_LOCKNAME_t writer is in progress. If that is the
+ * case, acquire then release the associated LOCKNAME writer serialization
+ * lock. This will allow any possibly-preempted writer to make progress
+ * until the end of its writer serialization lock critical section.
+ *
+ * This lock-unlock technique must be implemented for all of PREEMPT_RT
+ * sleeping locks.  See Documentation/locking/locktypes.rst
+ */
+#if defined(CONFIG_LOCKDEP) || defined(CONFIG_PREEMPT_RT)
+#define __SEQ_LOCK(expr)	expr
+#else
+#define __SEQ_LOCK(expr)
+#endif
+
+#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase)	\
+typedef struct seqcount_##lockname {					\
+	seqcount_t		seqcount;				\
+	__SEQ_LOCK(locktype	*lock);					\
+} seqcount_##lockname##_t;
+
+SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t,  false,    raw_spin)
+SEQCOUNT_LOCKNAME(spinlock,     spinlock_t,      __SEQ_RT, spin)
+SEQCOUNT_LOCKNAME(rwlock,       rwlock_t,        __SEQ_RT, read)
+SEQCOUNT_LOCKNAME(mutex,        struct mutex,    true,     mutex)
+#undef SEQCOUNT_LOCKNAME
+
+/*
+ * Sequential locks (seqlock_t)
+ *
+ * Sequence counters with an embedded spinlock for writer serialization
+ * and non-preemptibility.
+ *
+ * For more info, see:
+ *    - Comments on top of seqcount_t
+ *    - Documentation/locking/seqlock.rst
+ */
+typedef struct {
+	/*
+	 * Make sure that readers don't starve writers on PREEMPT_RT: use
+	 * seqcount_spinlock_t instead of seqcount_t. Check __SEQ_LOCK().
+	 */
+	seqcount_spinlock_t seqcount;
+	spinlock_t lock;
+} seqlock_t;
+
+#endif /* __LINUX_SEQLOCK_TYPES_H */
-- 
2.43.0


  parent reply	other threads:[~2023-12-16  3:27 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-16  2:47 [PATCH 00/50] big header dependency cleanup targeting sched.h Kent Overstreet
2023-12-16  2:47 ` [PATCH 01/50] drivers/gpu/drm/i915/i915_memcpy.c: fix missing includes Kent Overstreet
2024-03-08 13:46   ` Jani Nikula
2023-12-16  2:47 ` [PATCH 02/50] x86/kernel/fpu/bugs.c: fix missing include Kent Overstreet
2023-12-18 11:08   ` Sohil Mehta
2023-12-19  2:05     ` Kent Overstreet
2023-12-16  2:47 ` [PATCH 03/50] x86/lib/cache-smp.c: " Kent Overstreet
2023-12-18 10:48   ` Sohil Mehta
2023-12-19  2:06     ` Kent Overstreet
2023-12-19  4:04       ` Sohil Mehta
2023-12-16  2:47 ` [PATCH 04/50] x86/include/asm/debugreg.h: " Kent Overstreet
2023-12-16  2:47 ` [PATCH 05/50] x86/include/asm/paravirt_types.h: " Kent Overstreet
2023-12-16  2:47 ` [PATCH 06/50] task_stack.h: add " Kent Overstreet
2023-12-16  2:47 ` [PATCH 07/50] nsproxy.h: " Kent Overstreet
2023-12-16  2:47 ` [PATCH 08/50] kernel/fork.c: " Kent Overstreet
2023-12-16  2:47 ` [PATCH 09/50] kmsan: add missing types.h dependency Kent Overstreet
2023-12-16  2:47 ` [PATCH 10/50] time_namespace.h: fix missing include Kent Overstreet
2023-12-16  3:26 ` [PATCH 11/50] nodemask: Split out include/linux/nodemask_types.h Kent Overstreet
2023-12-16  3:26   ` [PATCH 12/50] prandom: Remove unused include Kent Overstreet
2023-12-16 18:52     ` Randy Dunlap
2023-12-16 22:19       ` Kent Overstreet
2023-12-16  3:26   ` [PATCH 13/50] timekeeping: Kill percpu.h dependency Kent Overstreet
2023-12-16  3:26   ` [PATCH 14/50] arm64: Fix circular header dependency Kent Overstreet
2023-12-16  3:26   ` [PATCH 15/50] kernel/numa.c: Move logging out of numa.h Kent Overstreet
2023-12-19 16:36     ` Nathan Chancellor
2023-12-19 21:02       ` Kent Overstreet
2023-12-19 22:52     ` Matthew Wilcox
2023-12-20  0:37       ` Kent Overstreet
2023-12-16  3:26   ` [PATCH 16/50] sched.h: Move (spin|rwlock)_needbreak() to spinlock.h Kent Overstreet
2024-01-15 20:31     ` Leonardo Bras
2023-12-16  3:26   ` [PATCH 17/50] ktime.h: move ktime_t to types.h Kent Overstreet
2023-12-16  3:26   ` [PATCH 18/50] hrtimers: Split out hrtimer_types.h Kent Overstreet
2023-12-16  3:26   ` [PATCH 19/50] locking/mutex: split out mutex_types.h Kent Overstreet
2023-12-18 16:53     ` Waiman Long
2023-12-18 18:12       ` Waiman Long
2023-12-19  1:46       ` Kent Overstreet
2023-12-19  3:04         ` Waiman Long
2023-12-19  3:37           ` Kent Overstreet
2023-12-19  3:39             ` Waiman Long
2023-12-16  3:26   ` [PATCH 20/50] posix-cpu-timers: Split out posix-timers_types.h Kent Overstreet
2023-12-16  3:26   ` Kent Overstreet [this message]
2023-12-18 17:02     ` [PATCH 21/50] locking/seqlock: Split out seqlock_types.h Waiman Long
2023-12-16  3:29 ` [PATCH 22/50] pid: Split out pid_types.h Kent Overstreet
2023-12-16  3:29   ` [PATCH 23/50] sched.h: move pid helpers to pid.h Kent Overstreet
2023-12-16  3:29   ` [PATCH 24/50] plist: Split out plist_types.h Kent Overstreet
2023-12-16  3:29   ` [PATCH 25/50] wait: Remove uapi header file from main header file Kent Overstreet
2023-12-18 12:39     ` Christian Brauner
2023-12-16  3:29   ` [PATCH 26/50] rslib: kill bogus dependency on list.h Kent Overstreet
2023-12-16 19:05     ` Randy Dunlap
2023-12-16 19:09       ` Kent Overstreet
2023-12-16 19:10       ` Randy Dunlap
2023-12-16  3:29   ` [PATCH 27/50] timerqueue: Split out timerqueue_types.h Kent Overstreet
2023-12-16  3:29   ` [PATCH 28/50] signal: Kill bogus dependency on list.h Kent Overstreet
2023-12-16  3:29   ` [PATCH 29/50] timers: Split out timer_types.h Kent Overstreet
2023-12-16  3:29   ` [PATCH 30/50] workqueue: Split out workqueue_types.h Kent Overstreet
2023-12-16  3:29   ` [PATCH 31/50] shm: Slim down dependencies Kent Overstreet
2023-12-16  3:29   ` [PATCH 32/50] ipc: Kill bogus dependency on spinlock.h Kent Overstreet
2023-12-18 11:04   ` [PATCH 22/50] pid: Split out pid_types.h Christian Brauner
2023-12-16  3:32 ` [PATCH 33/50] Split out irqflags_types.h Kent Overstreet
2023-12-16  3:32   ` [PATCH 34/50] mm_types_task.h: Trim dependencies Kent Overstreet
2023-12-16  3:32   ` [PATCH 35/50] cpumask: Split out cpumask_types.h Kent Overstreet
2023-12-16  3:32   ` [PATCH 36/50] syscall_user_dispatch.h: split out *_types.h Kent Overstreet
2023-12-16  3:32   ` [PATCH 37/50] x86/signal: kill dependency on time.h Kent Overstreet
2023-12-16  3:32   ` [PATCH 38/50] uapi/linux/resource.h: fix include Kent Overstreet
2023-12-16  3:32   ` [PATCH 39/50] refcount: Split out refcount_types.h Kent Overstreet
2023-12-16  3:32   ` [PATCH 40/50] seccomp: Split out seccomp_types.h Kent Overstreet
2023-12-16  3:32   ` [PATCH 41/50] uidgid: Split out uidgid_types.h Kent Overstreet
2023-12-18 11:01     ` Christian Brauner
2023-12-16  3:32   ` [PATCH 42/50] sem: Split out sem_types.h Kent Overstreet
2023-12-20 11:53     ` Geert Uytterhoeven
2023-12-20 21:39       ` Kent Overstreet
2024-01-02  8:47         ` Geert Uytterhoeven
2023-12-16  3:32   ` [PATCH 43/50] lockdep: move held_lock to lockdep_types.h Kent Overstreet
2023-12-18 17:05     ` Waiman Long
2023-12-16  3:35 ` [PATCH 44/50] restart_block: Trim includes Kent Overstreet
2023-12-16  3:35   ` [PATCH 45/50] rseq: Split out rseq.h from sched.h Kent Overstreet
2023-12-16  3:35   ` [PATCH 46/50] preempt.h: Kill dependency on list.h Kent Overstreet
2023-12-16  6:13     ` Matthew Wilcox
2023-12-16 19:21       ` Randy Dunlap
2023-12-16 22:35       ` Kent Overstreet
2023-12-17  0:04         ` Randy Dunlap
2023-12-17  0:18           ` Matthew Wilcox
2023-12-17  0:20             ` Kent Overstreet
2023-12-17  2:03               ` Randy Dunlap
2023-12-17  2:05                 ` Kent Overstreet
2023-12-17  0:18           ` Kent Overstreet
2023-12-17  0:26             ` Randy Dunlap
2023-12-16  3:35   ` [PATCH 47/50] thread_info, uaccess.h: Move HARDENED_USERCOPY to better location Kent Overstreet
2023-12-16  3:35   ` [PATCH 48/50] Kill unnecessary kernel.h include Kent Overstreet
2023-12-16  3:35   ` [PATCH 49/50] kill unnecessary thread_info.h include Kent Overstreet
2023-12-16  3:35   ` [PATCH 50/50] Kill sched.h dependency on rcupdate.h Kent Overstreet
2023-12-16 19:35     ` Paul E. McKenney
2023-12-16 22:20       ` Kent Overstreet
2023-12-20 11:59     ` Geert Uytterhoeven
2023-12-20 21:39       ` Kent Overstreet
2024-01-02 11:39         ` Geert Uytterhoeven

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231216032651.3553101-11-kent.overstreet@linux.dev \
    --to=kent.overstreet@linux.dev \
    --cc=boqun.feng@gmail.com \
    --cc=brauner@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=keescook@chromium.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=longman@redhat.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.