Blame view

kernel/linux-rt-4.4.41/include/linux/locallock.h 6.85 KB
5113f6f70   김현기   kernel add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  #ifndef _LINUX_LOCALLOCK_H
  #define _LINUX_LOCALLOCK_H
  
  #include <linux/percpu.h>
  #include <linux/spinlock.h>
  
  #ifdef CONFIG_PREEMPT_RT_BASE
  
  #ifdef CONFIG_DEBUG_SPINLOCK
  # define LL_WARN(cond)	WARN_ON(cond)
  #else
  # define LL_WARN(cond)	do { } while (0)
  #endif
  
  /*
   * per cpu lock based substitute for local_irq_*()
   */
  struct local_irq_lock {
  	spinlock_t		lock;
  	struct task_struct	*owner;
  	int			nestcnt;
  	unsigned long		flags;
  };
  
  #define DEFINE_LOCAL_IRQ_LOCK(lvar)					\
  	DEFINE_PER_CPU(struct local_irq_lock, lvar) = {			\
  		.lock = __SPIN_LOCK_UNLOCKED((lvar).lock) }
  
  #define DECLARE_LOCAL_IRQ_LOCK(lvar)					\
  	DECLARE_PER_CPU(struct local_irq_lock, lvar)
  
  #define local_irq_lock_init(lvar)					\
  	do {								\
  		int __cpu;						\
  		for_each_possible_cpu(__cpu)				\
  			spin_lock_init(&per_cpu(lvar, __cpu).lock);	\
  	} while (0)
  
  /*
   * spin_lock|trylock|unlock_local flavour that does not migrate disable
   * used for __local_lock|trylock|unlock where get_local_var/put_local_var
   * already takes care of the migrate_disable/enable
   * for CONFIG_PREEMPT_BASE map to the normal spin_* calls.
   */
  #ifdef CONFIG_PREEMPT_RT_FULL
  # define spin_lock_local(lock)			rt_spin_lock__no_mg(lock)
  # define spin_trylock_local(lock)		rt_spin_trylock__no_mg(lock)
  # define spin_unlock_local(lock)		rt_spin_unlock__no_mg(lock)
  #else
  # define spin_lock_local(lock)			spin_lock(lock)
  # define spin_trylock_local(lock)		spin_trylock(lock)
  # define spin_unlock_local(lock)		spin_unlock(lock)
  #endif
  
  static inline void __local_lock(struct local_irq_lock *lv)
  {
  	if (lv->owner != current) {
  		spin_lock_local(&lv->lock);
  		LL_WARN(lv->owner);
  		LL_WARN(lv->nestcnt);
  		lv->owner = current;
  	}
  	lv->nestcnt++;
  }
  
  #define local_lock(lvar)					\
  	do { __local_lock(&get_local_var(lvar)); } while (0)
  
  #define local_lock_on(lvar, cpu)				\
  	do { __local_lock(&per_cpu(lvar, cpu)); } while (0)
  
  static inline int __local_trylock(struct local_irq_lock *lv)
  {
  	if (lv->owner != current && spin_trylock_local(&lv->lock)) {
  		LL_WARN(lv->owner);
  		LL_WARN(lv->nestcnt);
  		lv->owner = current;
  		lv->nestcnt = 1;
  		return 1;
  	}
  	return 0;
  }
  
  #define local_trylock(lvar)						\
  	({								\
  		int __locked;						\
  		__locked = __local_trylock(&get_local_var(lvar));	\
  		if (!__locked)						\
  			put_local_var(lvar);				\
  		__locked;						\
  	})
  
  static inline void __local_unlock(struct local_irq_lock *lv)
  {
  	LL_WARN(lv->nestcnt == 0);
  	LL_WARN(lv->owner != current);
  	if (--lv->nestcnt)
  		return;
  
  	lv->owner = NULL;
  	spin_unlock_local(&lv->lock);
  }
  
  #define local_unlock(lvar)					\
  	do {							\
  		__local_unlock(this_cpu_ptr(&lvar));		\
  		put_local_var(lvar);				\
  	} while (0)
  
  #define local_unlock_on(lvar, cpu)                       \
  	do { __local_unlock(&per_cpu(lvar, cpu)); } while (0)
  
  static inline void __local_lock_irq(struct local_irq_lock *lv)
  {
  	spin_lock_irqsave(&lv->lock, lv->flags);
  	LL_WARN(lv->owner);
  	LL_WARN(lv->nestcnt);
  	lv->owner = current;
  	lv->nestcnt = 1;
  }
  
  #define local_lock_irq(lvar)						\
  	do { __local_lock_irq(&get_local_var(lvar)); } while (0)
  
  #define local_lock_irq_on(lvar, cpu)					\
  	do { __local_lock_irq(&per_cpu(lvar, cpu)); } while (0)
  
  static inline void __local_unlock_irq(struct local_irq_lock *lv)
  {
  	LL_WARN(!lv->nestcnt);
  	LL_WARN(lv->owner != current);
  	lv->owner = NULL;
  	lv->nestcnt = 0;
  	spin_unlock_irq(&lv->lock);
  }
  
  #define local_unlock_irq(lvar)						\
  	do {								\
  		__local_unlock_irq(this_cpu_ptr(&lvar));		\
  		put_local_var(lvar);					\
  	} while (0)
  
  #define local_unlock_irq_on(lvar, cpu)					\
  	do {								\
  		__local_unlock_irq(&per_cpu(lvar, cpu));		\
  	} while (0)
  
  static inline int __local_lock_irqsave(struct local_irq_lock *lv)
  {
  	if (lv->owner != current) {
  		__local_lock_irq(lv);
  		return 0;
  	} else {
  		lv->nestcnt++;
  		return 1;
  	}
  }
  
  #define local_lock_irqsave(lvar, _flags)				\
  	do {								\
  		if (__local_lock_irqsave(&get_local_var(lvar)))		\
  			put_local_var(lvar);				\
  		_flags = __this_cpu_read(lvar.flags);			\
  	} while (0)
  
  #define local_lock_irqsave_on(lvar, _flags, cpu)			\
  	do {								\
  		__local_lock_irqsave(&per_cpu(lvar, cpu));		\
  		_flags = per_cpu(lvar, cpu).flags;			\
  	} while (0)
  
  static inline int __local_unlock_irqrestore(struct local_irq_lock *lv,
  					    unsigned long flags)
  {
  	LL_WARN(!lv->nestcnt);
  	LL_WARN(lv->owner != current);
  	if (--lv->nestcnt)
  		return 0;
  
  	lv->owner = NULL;
  	spin_unlock_irqrestore(&lv->lock, lv->flags);
  	return 1;
  }
  
  #define local_unlock_irqrestore(lvar, flags)				\
  	do {								\
  		if (__local_unlock_irqrestore(this_cpu_ptr(&lvar), flags)) \
  			put_local_var(lvar);				\
  	} while (0)
  
  #define local_unlock_irqrestore_on(lvar, flags, cpu)			\
  	do {								\
  		__local_unlock_irqrestore(&per_cpu(lvar, cpu), flags);	\
  	} while (0)
  
  #define local_spin_trylock_irq(lvar, lock)				\
  	({								\
  		int __locked;						\
  		local_lock_irq(lvar);					\
  		__locked = spin_trylock(lock);				\
  		if (!__locked)						\
  			local_unlock_irq(lvar);				\
  		__locked;						\
  	})
  
  #define local_spin_lock_irq(lvar, lock)					\
  	do {								\
  		local_lock_irq(lvar);					\
  		spin_lock(lock);					\
  	} while (0)
  
  #define local_spin_unlock_irq(lvar, lock)				\
  	do {								\
  		spin_unlock(lock);					\
  		local_unlock_irq(lvar);					\
  	} while (0)
  
  #define local_spin_lock_irqsave(lvar, lock, flags)			\
  	do {								\
  		local_lock_irqsave(lvar, flags);			\
  		spin_lock(lock);					\
  	} while (0)
  
  #define local_spin_unlock_irqrestore(lvar, lock, flags)			\
  	do {								\
  		spin_unlock(lock);					\
  		local_unlock_irqrestore(lvar, flags);			\
  	} while (0)
  
  #define get_locked_var(lvar, var)					\
  	(*({								\
  		local_lock(lvar);					\
  		this_cpu_ptr(&var);					\
  	}))
  
  #define put_locked_var(lvar, var)	local_unlock(lvar);
  
  #define local_lock_cpu(lvar)						\
  	({								\
  		local_lock(lvar);					\
  		smp_processor_id();					\
  	})
  
  #define local_unlock_cpu(lvar)			local_unlock(lvar)
  
  #else /* PREEMPT_RT_BASE */
  
  #define DEFINE_LOCAL_IRQ_LOCK(lvar)		__typeof__(const int) lvar
  #define DECLARE_LOCAL_IRQ_LOCK(lvar)		extern __typeof__(const int) lvar
  
  static inline void local_irq_lock_init(int lvar) { }
  
  #define local_lock(lvar)			preempt_disable()
  #define local_unlock(lvar)			preempt_enable()
  #define local_lock_irq(lvar)			local_irq_disable()
  #define local_unlock_irq(lvar)			local_irq_enable()
  #define local_lock_irqsave(lvar, flags)		local_irq_save(flags)
  #define local_unlock_irqrestore(lvar, flags)	local_irq_restore(flags)
  
  #define local_spin_trylock_irq(lvar, lock)	spin_trylock_irq(lock)
  #define local_spin_lock_irq(lvar, lock)		spin_lock_irq(lock)
  #define local_spin_unlock_irq(lvar, lock)	spin_unlock_irq(lock)
  #define local_spin_lock_irqsave(lvar, lock, flags)	\
  	spin_lock_irqsave(lock, flags)
  #define local_spin_unlock_irqrestore(lvar, lock, flags)	\
  	spin_unlock_irqrestore(lock, flags)
  
  #define get_locked_var(lvar, var)		get_cpu_var(var)
  #define put_locked_var(lvar, var)		put_cpu_var(var)
  
  #define local_lock_cpu(lvar)			get_cpu()
  #define local_unlock_cpu(lvar)			put_cpu()
  
  #endif
  
  #endif