1 /*        $NetBSD: rcupdate.h,v 1.8 2021/12/19 11:49:11 riastradh Exp $         */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_RCUPDATE_H_
33 #define _LINUX_RCUPDATE_H_
34 
35 #include <sys/atomic.h>
36 #include <sys/cdefs.h>
37 #include <sys/systm.h>
38 
39 #include <linux/bug.h>
40 
41 #define   __rcu
42 
43 #define   RCU_INIT_POINTER(P, V)        ((P) = (V))
44 
45 #define   rcu_assign_pointer(P,V)       atomic_store_release(&(P), (V))
46 #define   rcu_dereference(P)  atomic_load_consume(&(P))
47 #define   rcu_access_pointer(P)         atomic_load_relaxed(&(P))
48 
49 #define   rcu_dereference_raw rcu_dereference
50 
51 #define   rcu_dereference_protected(P, C) ({                                          \
52           WARN_ON(!(C));                                                                        \
53           (P);                                                                                  \
54 })
55 
56 #define   rcu_replace_pointer(PTRP, NEWPTR, C) ({                                     \
57           __typeof__(PTRP) __rrp_ptrp = (PTRP);                                       \
58           __typeof__(PTRP) __rrp_oldptr =                                                       \
59                     rcu_dereference_protected(__rrp_ptrp, C);                         \
60           rcu_assign_pointer(__rrp_ptrp, NEWPTR);                                     \
61           __rrp_oldptr;                                                                         \
62 })
63 
64 
65 /* kill_dependency */
66 #define   rcu_pointer_handoff(P)        (P)
67 
68 struct rcu_head {
69           union {
70                     void                (*callback)(struct rcu_head *);
71                     void                *obj;
72           }                   rcuh_u;
73           struct rcu_head     *rcuh_next;
74 };
75 
76 #define   _kfree_rcu                    linux__kfree_rcu
77 #define   call_rcu            linux_call_rcu
78 #define   cond_synchronize_rcu          linux_cond_synchronize_rcu
79 #define   destroy_rcu_head    linux_destroy_rcu_head
80 #define   init_rcu_head                 linux_init_rcu_head
81 #define   get_state_synchronize_rcu linux_get_state_synchronize_rcu
82 #define   rcu_barrier                   linux_rcu_barrier
83 #define   synchronize_rcu               linux_synchronize_rcu
84 #define   synchronize_rcu_expedited linux_synchronize_rcu_expedited
85 
86 int       linux_rcu_gc_init(void);
87 void      linux_rcu_gc_fini(void);
88 
89 void      init_rcu_head(struct rcu_head *);
90 void      destroy_rcu_head(struct rcu_head *);
91 
92 void      call_rcu(struct rcu_head *, void (*)(struct rcu_head *));
93 void      rcu_barrier(void);
94 void      synchronize_rcu(void);
95 void      synchronize_rcu_expedited(void);
96 
97 unsigned long get_state_synchronize_rcu(void);
98 void      cond_synchronize_rcu(unsigned long);
99 
100 void      _kfree_rcu(struct rcu_head *, void *);
101 
102 static inline void
rcu_read_lock(void)103 rcu_read_lock(void)
104 {
105 
106           kpreempt_disable(); /* implies __insn_barrier */
107 }
108 
109 static inline void
rcu_read_unlock(void)110 rcu_read_unlock(void)
111 {
112 
113           kpreempt_enable();  /* implies __insn_barrier */
114 }
115 
116 #define   kfree_rcu(P, F)                                                                       \
117           _kfree_rcu(&(P)->F, (P))
118 
119 #endif  /* _LINUX_RCUPDATE_H_ */
120