Files

158 lines
5.1 KiB
C
Raw Permalink Normal View History

/* SPDX-License-Identifier: GPL-2.0+ */
2017-03-25 09:59:38 -07:00
/*
* Sleepable Read-Copy Update mechanism for mutual exclusion,
* tiny variant.
*
* Copyright (C) IBM Corporation, 2017
*
* Author: Paul McKenney <paulmck@linux.ibm.com>
2017-03-25 09:59:38 -07:00
*/
#ifndef _LINUX_SRCU_TINY_H
#define _LINUX_SRCU_TINY_H
2026-03-23 20:14:18 -04:00
#include <linux/irq_work_types.h>
2017-03-25 09:59:38 -07:00
#include <linux/swait.h>
struct srcu_struct {
2017-04-21 13:33:20 -07:00
short srcu_lock_nesting[2]; /* srcu_read_lock() nesting depth. */
u8 srcu_gp_running; /* GP workqueue running? */
u8 srcu_gp_waiting; /* GP waiting for readers? */
unsigned long srcu_idx; /* Current reader array element in bit 0x2. */
unsigned long srcu_idx_max; /* Furthest future srcu_idx request. */
2017-03-25 09:59:38 -07:00
struct swait_queue_head srcu_wq;
/* Last srcu_read_unlock() wakes GP. */
struct rcu_head *srcu_cb_head; /* Pending callbacks: Head. */
struct rcu_head **srcu_cb_tail; /* Pending callbacks: Tail. */
2017-03-25 09:59:38 -07:00
struct work_struct srcu_work; /* For driving grace periods. */
2026-03-23 20:14:18 -04:00
struct irq_work srcu_irq_work; /* Defer schedule_work() to irq work. */
2017-03-25 09:59:38 -07:00
#ifdef CONFIG_DEBUG_LOCK_ALLOC
struct lockdep_map dep_map;
#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
};
void srcu_drive_gp(struct work_struct *wp);
2026-03-23 20:14:18 -04:00
void srcu_tiny_irq_work(struct irq_work *irq_work);
2017-03-25 09:59:38 -07:00
2025-11-05 12:32:04 -08:00
#define __SRCU_STRUCT_INIT(name, __ignored, ___ignored, ____ignored) \
2017-03-25 09:59:38 -07:00
{ \
.srcu_wq = __SWAIT_QUEUE_HEAD_INITIALIZER(name.srcu_wq), \
.srcu_cb_tail = &name.srcu_cb_head, \
2017-03-25 09:59:38 -07:00
.srcu_work = __WORK_INITIALIZER(name.srcu_work, srcu_drive_gp), \
2026-03-23 20:14:18 -04:00
.srcu_irq_work = { .func = srcu_tiny_irq_work }, \
2017-03-25 09:59:38 -07:00
__SRCU_DEP_MAP_INIT(name) \
}
/*
* This odd _STATIC_ arrangement is needed for API compatibility with
* Tree SRCU, which needs some per-CPU data.
*/
#define DEFINE_SRCU(name) \
2025-11-05 12:32:04 -08:00
struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name)
2017-03-25 09:59:38 -07:00
#define DEFINE_STATIC_SRCU(name) \
2025-11-05 12:32:04 -08:00
static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name)
#define DEFINE_SRCU_FAST(name) DEFINE_SRCU(name)
#define DEFINE_STATIC_SRCU_FAST(name) \
static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name)
2025-11-05 12:32:14 -08:00
#define DEFINE_SRCU_FAST_UPDOWN(name) DEFINE_SRCU(name)
#define DEFINE_STATIC_SRCU_FAST_UPDOWN(name) \
static struct srcu_struct name = __SRCU_STRUCT_INIT(name, name, name, name)
2017-03-25 09:59:38 -07:00
// Dummy structure for srcu_notifier_head.
struct srcu_usage { };
#define __SRCU_USAGE_INIT(name) { }
2025-11-05 12:32:04 -08:00
#define __init_srcu_struct_fast __init_srcu_struct
2025-11-05 12:32:14 -08:00
#define __init_srcu_struct_fast_updown __init_srcu_struct
2025-11-05 12:32:04 -08:00
#ifndef CONFIG_DEBUG_LOCK_ALLOC
#define init_srcu_struct_fast init_srcu_struct
2025-11-05 12:32:14 -08:00
#define init_srcu_struct_fast_updown init_srcu_struct
2025-11-05 12:32:04 -08:00
#endif // #ifndef CONFIG_DEBUG_LOCK_ALLOC
void synchronize_srcu(struct srcu_struct *ssp);
2017-03-25 09:59:38 -07:00
2017-04-28 14:16:16 -07:00
/*
* Counts the new reader in the appropriate per-CPU element of the
* srcu_struct. Can be invoked from irq/bh handlers, but the matching
* __srcu_read_unlock() must be in the same handler instance. Returns an
* index that must be passed to the matching srcu_read_unlock().
*/
static inline int __srcu_read_lock(struct srcu_struct *ssp)
2025-12-19 16:40:04 +01:00
__acquires_shared(ssp)
2017-04-28 14:16:16 -07:00
{
int idx;
2024-12-12 20:06:53 -08:00
preempt_disable(); // Needed for PREEMPT_LAZY
idx = ((READ_ONCE(ssp->srcu_idx) + 1) & 0x2) >> 1;
2021-06-02 16:31:38 -07:00
WRITE_ONCE(ssp->srcu_lock_nesting[idx], READ_ONCE(ssp->srcu_lock_nesting[idx]) + 1);
preempt_enable();
2025-12-19 16:40:04 +01:00
__acquire_shared(ssp);
2017-04-28 14:16:16 -07:00
return idx;
}
2025-01-09 13:19:42 -08:00
struct srcu_ctr;
static inline bool __srcu_ptr_to_ctr(struct srcu_struct *ssp, struct srcu_ctr __percpu *scpp)
{
return (int)(intptr_t)(struct srcu_ctr __force __kernel *)scpp;
}
static inline struct srcu_ctr __percpu *__srcu_ctr_to_ptr(struct srcu_struct *ssp, int idx)
{
return (struct srcu_ctr __percpu *)(intptr_t)idx;
}
static inline struct srcu_ctr __percpu *__srcu_read_lock_fast(struct srcu_struct *ssp)
2025-12-19 16:40:04 +01:00
__acquires_shared(ssp)
2025-01-09 13:19:42 -08:00
{
return __srcu_ctr_to_ptr(ssp, __srcu_read_lock(ssp));
}
static inline void __srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
2025-12-19 16:40:04 +01:00
__releases_shared(ssp)
2025-01-09 13:19:42 -08:00
{
__srcu_read_unlock(ssp, __srcu_ptr_to_ctr(ssp, scp));
}
2025-11-05 12:32:14 -08:00
static inline struct srcu_ctr __percpu *__srcu_read_lock_fast_updown(struct srcu_struct *ssp)
2025-12-19 16:40:04 +01:00
__acquires_shared(ssp)
2025-11-05 12:32:14 -08:00
{
return __srcu_ctr_to_ptr(ssp, __srcu_read_lock(ssp));
}
static inline
void __srcu_read_unlock_fast_updown(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp)
2025-12-19 16:40:04 +01:00
__releases_shared(ssp)
2025-11-05 12:32:14 -08:00
{
__srcu_read_unlock(ssp, __srcu_ptr_to_ctr(ssp, scp));
}
static inline void synchronize_srcu_expedited(struct srcu_struct *ssp)
2017-03-25 09:59:38 -07:00
{
synchronize_srcu(ssp);
2017-03-25 09:59:38 -07:00
}
static inline void srcu_barrier(struct srcu_struct *ssp)
2017-03-25 09:59:38 -07:00
{
synchronize_srcu(ssp);
2017-03-25 09:59:38 -07:00
}
static inline void srcu_expedite_current(struct srcu_struct *ssp) { }
#define srcu_check_read_flavor(ssp, read_flavor) do { } while (0)
/* Defined here to avoid size increase for non-torture kernels. */
static inline void srcu_torture_stats_print(struct srcu_struct *ssp,
char *tt, char *tf)
{
int idx;
2021-06-02 16:31:38 -07:00
idx = ((data_race(READ_ONCE(ssp->srcu_idx)) + 1) & 0x2) >> 1;
pr_alert("%s%s Tiny SRCU per-CPU(idx=%d): (%hd,%hd) gp: %lu->%lu\n",
tt, tf, idx,
2021-06-02 16:31:38 -07:00
data_race(READ_ONCE(ssp->srcu_lock_nesting[!idx])),
data_race(READ_ONCE(ssp->srcu_lock_nesting[idx])),
data_race(READ_ONCE(ssp->srcu_idx)),
data_race(READ_ONCE(ssp->srcu_idx_max)));
}
2017-03-25 09:59:38 -07:00
#endif