locking/percpu-rwsem: Add percpu_is_write_locked() and percpu_is_read_locked()
authorMarco Elver <elver@google.com>
Mon, 29 Aug 2022 12:47:15 +0000 (14:47 +0200)
committerPeter Zijlstra <peterz@infradead.org>
Tue, 30 Aug 2022 08:56:23 +0000 (10:56 +0200)
Implement simple accessors to probe percpu-rwsem's locked state:
percpu_is_write_locked(), percpu_is_read_locked().

Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
Acked-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20220829124719.675715-11-elver@google.com
include/linux/percpu-rwsem.h
kernel/locking/percpu-rwsem.c

index 5fda40f97fe916043b8217e8fd8b6a3ccdb0f26f..36b942b67b7d66ab031d28ad16de10164e8d6a0e 100644 (file)
@@ -121,9 +121,15 @@ static inline void percpu_up_read(struct percpu_rw_semaphore *sem)
        preempt_enable();
 }
 
+extern bool percpu_is_read_locked(struct percpu_rw_semaphore *);
 extern void percpu_down_write(struct percpu_rw_semaphore *);
 extern void percpu_up_write(struct percpu_rw_semaphore *);
 
+static inline bool percpu_is_write_locked(struct percpu_rw_semaphore *sem)
+{
+       return atomic_read(&sem->block);
+}
+
 extern int __percpu_init_rwsem(struct percpu_rw_semaphore *,
                                const char *, struct lock_class_key *);
 
index 5fe4c5495ba3c87c8505fe0dde278d0112579985..185bd1c906b0113f3921385bdd00d7950100023c 100644 (file)
@@ -192,6 +192,12 @@ EXPORT_SYMBOL_GPL(__percpu_down_read);
        __sum;                                                          \
 })
 
+bool percpu_is_read_locked(struct percpu_rw_semaphore *sem)
+{
+       return per_cpu_sum(*sem->read_count) != 0 && !atomic_read(&sem->block);
+}
+EXPORT_SYMBOL_GPL(percpu_is_read_locked);
+
 /*
  * Return true if the modular sum of the sem->read_count per-CPU variable is
  * zero.  If this sum is zero, then it is stable due to the fact that if any