xref: /freebsd/sys/compat/linuxkpi/common/include/linux/rcupdate.h (revision 643ac419fafba89f5adda0e0ea75b538727453fb)
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  * $FreeBSD$
27  */
28 #ifndef	_LINUXKPI_LINUX_RCUPDATE_H_
29 #define	_LINUXKPI_LINUX_RCUPDATE_H_
30 
31 #include <linux/compiler.h>
32 #include <linux/types.h>
33 
34 #include <machine/atomic.h>
35 
36 #define	LINUX_KFREE_RCU_OFFSET_MAX	4096	/* exclusive */
37 
38 /* BSD specific defines */
39 #define	RCU_TYPE_REGULAR 0
40 #define	RCU_TYPE_SLEEPABLE 1
41 #define	RCU_TYPE_MAX 2
42 
43 #define	RCU_INITIALIZER(v)			\
44 	((__typeof(*(v)) *)(v))
45 
46 #define	RCU_INIT_POINTER(p, v) do {		\
47 	(p) = (v);				\
48 } while (0)
49 
50 #define	call_rcu(ptr, func) do {		\
51 	linux_call_rcu(RCU_TYPE_REGULAR, ptr, func);	\
52 } while (0)
53 
54 #define	rcu_barrier(void) do {			\
55 	linux_rcu_barrier(RCU_TYPE_REGULAR);	\
56 } while (0)
57 
58 #define	rcu_read_lock(void) do {		\
59 	linux_rcu_read_lock(RCU_TYPE_REGULAR);	\
60 } while (0)
61 
62 #define	rcu_read_unlock(void) do {		\
63 	linux_rcu_read_unlock(RCU_TYPE_REGULAR);\
64 } while (0)
65 
66 #define	synchronize_rcu(void) do {	\
67 	linux_synchronize_rcu(RCU_TYPE_REGULAR);	\
68 } while (0)
69 
70 #define	synchronize_rcu_expedited(void) do {	\
71 	linux_synchronize_rcu(RCU_TYPE_REGULAR);	\
72 } while (0)
73 
74 #define	kfree_rcu(ptr, rcu_head) do {				\
75 	CTASSERT(offsetof(__typeof(*(ptr)), rcu_head) <		\
76 	    LINUX_KFREE_RCU_OFFSET_MAX);			\
77 	call_rcu(&(ptr)->rcu_head, (rcu_callback_t)(uintptr_t)	\
78 	    offsetof(__typeof(*(ptr)), rcu_head));		\
79 } while (0)
80 
81 #define	rcu_access_pointer(p)			\
82 	((__typeof(*p) *)READ_ONCE(p))
83 
84 #define	rcu_dereference_protected(p, c)		\
85 	((__typeof(*p) *)READ_ONCE(p))
86 
87 #define	rcu_dereference(p)			\
88 	rcu_dereference_protected(p, 0)
89 
90 #define	rcu_dereference_check(p, c)		\
91 	rcu_dereference_protected(p, c)
92 
93 #define	rcu_dereference_raw(p)			\
94 	((__typeof(*p) *)READ_ONCE(p))
95 
96 #define	rcu_pointer_handoff(p) (p)
97 
98 #define	rcu_assign_pointer(p, v) do {				\
99 	atomic_store_rel_ptr((volatile uintptr_t *)&(p),	\
100 	    (uintptr_t)(v));					\
101 } while (0)
102 
103 #define	rcu_replace_pointer(rcu, ptr, c)			\
104 ({								\
105 	typeof(ptr) __tmp = rcu_dereference_protected(rcu, c);	\
106 	rcu_assign_pointer(rcu, ptr);				\
107 	__tmp;							\
108 })
109 
110 #define	rcu_swap_protected(rcu, ptr, c) do {			\
111 	typeof(ptr) p = rcu_dereference_protected(rcu, c);	\
112 	rcu_assign_pointer(rcu, ptr);				\
113 	(ptr) = p;						\
114 } while (0)
115 
116 /* prototypes */
117 
118 extern void linux_call_rcu(unsigned type, struct rcu_head *ptr, rcu_callback_t func);
119 extern void linux_rcu_barrier(unsigned type);
120 extern void linux_rcu_read_lock(unsigned type);
121 extern void linux_rcu_read_unlock(unsigned type);
122 extern void linux_synchronize_rcu(unsigned type);
123 
124 /* Empty implementation for !DEBUG */
125 #define	init_rcu_head(...)
126 #define	destroy_rcu_head(...)
127 #define	init_rcu_head_on_stack(...)
128 #define	destroy_rcu_head_on_stack(...)
129 
130 #endif					/* _LINUXKPI_LINUX_RCUPDATE_H_ */
131