xref: /freebsd/sys/compat/linuxkpi/common/include/linux/rcupdate.h (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2016-2017 Mellanox Technologies, Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef	_LINUXKPI_LINUX_RCUPDATE_H_
27 #define	_LINUXKPI_LINUX_RCUPDATE_H_
28 
29 #include <linux/compiler.h>
30 #include <linux/types.h>
31 
32 #include <machine/atomic.h>
33 
34 #define	LINUX_KFREE_RCU_OFFSET_MAX	4096	/* exclusive */
35 
36 /* BSD specific defines */
37 #define	RCU_TYPE_REGULAR 0
38 #define	RCU_TYPE_SLEEPABLE 1
39 #define	RCU_TYPE_MAX 2
40 
41 #define	RCU_INITIALIZER(v)			\
42 	((__typeof(*(v)) *)(v))
43 
44 #define	RCU_INIT_POINTER(p, v) do {		\
45 	(p) = (v);				\
46 } while (0)
47 
48 #define	call_rcu(ptr, func) do {		\
49 	linux_call_rcu(RCU_TYPE_REGULAR, ptr, func);	\
50 } while (0)
51 
52 #define	rcu_barrier(void) do {			\
53 	linux_rcu_barrier(RCU_TYPE_REGULAR);	\
54 } while (0)
55 
56 #define	rcu_read_lock(void) do {		\
57 	linux_rcu_read_lock(RCU_TYPE_REGULAR);	\
58 } while (0)
59 
60 #define	rcu_read_unlock(void) do {		\
61 	linux_rcu_read_unlock(RCU_TYPE_REGULAR);\
62 } while (0)
63 
64 #define	synchronize_rcu(void) do {	\
65 	linux_synchronize_rcu(RCU_TYPE_REGULAR);	\
66 } while (0)
67 
68 #define	synchronize_rcu_expedited(void) do {	\
69 	linux_synchronize_rcu(RCU_TYPE_REGULAR);	\
70 } while (0)
71 
72 #define	kfree_rcu(ptr, rcu_head) do {				\
73 	CTASSERT(offsetof(__typeof(*(ptr)), rcu_head) <		\
74 	    LINUX_KFREE_RCU_OFFSET_MAX);			\
75 	call_rcu(&(ptr)->rcu_head, (rcu_callback_t)(uintptr_t)	\
76 	    offsetof(__typeof(*(ptr)), rcu_head));		\
77 } while (0)
78 
79 #define	rcu_access_pointer(p)			\
80 	((__typeof(*p) *)READ_ONCE(p))
81 
82 #define	rcu_dereference_protected(p, c)		\
83 	((__typeof(*p) *)READ_ONCE(p))
84 
85 #define	rcu_dereference(p)			\
86 	rcu_dereference_protected(p, 0)
87 
88 #define	rcu_dereference_check(p, c)		\
89 	rcu_dereference_protected(p, c)
90 
91 #define	rcu_dereference_raw(p)			\
92 	((__typeof(*p) *)READ_ONCE(p))
93 
94 #define	rcu_pointer_handoff(p) (p)
95 
96 #define	rcu_assign_pointer(p, v) do {				\
97 	atomic_store_rel_ptr((volatile uintptr_t *)&(p),	\
98 	    (uintptr_t)(v));					\
99 } while (0)
100 
101 #define	rcu_replace_pointer(rcu, ptr, c)			\
102 ({								\
103 	typeof(ptr) __tmp = rcu_dereference_protected(rcu, c);	\
104 	rcu_assign_pointer(rcu, ptr);				\
105 	__tmp;							\
106 })
107 
108 #define	rcu_swap_protected(rcu, ptr, c) do {			\
109 	typeof(ptr) p = rcu_dereference_protected(rcu, c);	\
110 	rcu_assign_pointer(rcu, ptr);				\
111 	(ptr) = p;						\
112 } while (0)
113 
114 /* prototypes */
115 
116 extern void linux_call_rcu(unsigned type, struct rcu_head *ptr, rcu_callback_t func);
117 extern void linux_rcu_barrier(unsigned type);
118 extern void linux_rcu_read_lock(unsigned type);
119 extern void linux_rcu_read_unlock(unsigned type);
120 extern void linux_synchronize_rcu(unsigned type);
121 
122 /* Empty implementation for !DEBUG */
123 #define	init_rcu_head(...)
124 #define	destroy_rcu_head(...)
125 #define	init_rcu_head_on_stack(...)
126 #define	destroy_rcu_head_on_stack(...)
127 
128 #endif					/* _LINUXKPI_LINUX_RCUPDATE_H_ */
129