hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
/*
 * SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2016 Philippe Gerum  <rpm@xenomai.org>.
 */
#include <linux/linkage.h>
#include <linux/preempt.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/irq_pipeline.h>
#include <linux/kconfig.h>
 
/*
 * A hybrid spinlock behaves in different ways depending on the
 * current interrupt stage on entry.
 *
 * Such spinlock always leaves hard IRQs disabled once locked. In
 * addition, it stalls the in-band stage when protecting a critical
 * section there, disabling preemption like regular spinlocks do as
 * well. This combination preserves the regular locking logic when
 * called from the in-band stage, while fully disabling preemption by
 * other interrupt stages.
 *
 * When taken from the pipeline entry context, a hybrid lock behaves
 * like a hard spinlock, assuming that hard IRQs are already disabled.
 *
 * The irq descriptor lock (struct irq_desc) is a typical example of
 * such lock, which properly serializes accesses regardless of the
 * calling context.
 */
void __hybrid_spin_lock(struct raw_spinlock *rlock)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
 
   if (running_inband())
       preempt_disable();
 
   __flags = hard_local_irq_save();
   hard_lock_acquire(rlock, 0, _RET_IP_);
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   lock->hwflags = __flags;
}
EXPORT_SYMBOL(__hybrid_spin_lock);
 
void __hybrid_spin_lock_nested(struct raw_spinlock *rlock, int subclass)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
 
   if (running_inband())
       preempt_disable();
 
   __flags = hard_local_irq_save();
   hard_lock_acquire_nested(rlock, subclass, _RET_IP_);
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   lock->hwflags = __flags;
}
EXPORT_SYMBOL(__hybrid_spin_lock_nested);
 
void __hybrid_spin_unlock(struct raw_spinlock *rlock)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
 
   /* Pick the flags before releasing the lock. */
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   __flags = lock->hwflags;
   hard_lock_release(rlock, _RET_IP_);
   hard_local_irq_restore(__flags);
 
   if (running_inband())
       preempt_enable();
}
EXPORT_SYMBOL(__hybrid_spin_unlock);
 
void __hybrid_spin_lock_irq(struct raw_spinlock *rlock)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
 
   __flags = hard_local_irq_save();
 
   if (running_inband()) {
       stall_inband();
       trace_hardirqs_off();
       preempt_disable();
   }
 
   hard_lock_acquire(rlock, 0, _RET_IP_);
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   lock->hwflags = __flags;
}
EXPORT_SYMBOL(__hybrid_spin_lock_irq);
 
void __hybrid_spin_unlock_irq(struct raw_spinlock *rlock)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
 
   /* Pick the flags before releasing the lock. */
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   __flags = lock->hwflags;
   hard_lock_release(rlock, _RET_IP_);
 
   if (running_inband()) {
       trace_hardirqs_on();
       unstall_inband_nocheck();
       hard_local_irq_restore(__flags);
       preempt_enable();
       return;
   }
 
   hard_local_irq_restore(__flags);
}
EXPORT_SYMBOL(__hybrid_spin_unlock_irq);
 
unsigned long __hybrid_spin_lock_irqsave(struct raw_spinlock *rlock)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags, flags;
 
   __flags = flags = hard_local_irq_save();
 
   if (running_inband()) {
       flags = test_and_stall_inband();
       trace_hardirqs_off();
       preempt_disable();
   }
 
   hard_lock_acquire(rlock, 0, _RET_IP_);
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   lock->hwflags = __flags;
 
   return flags;
}
EXPORT_SYMBOL(__hybrid_spin_lock_irqsave);
 
void __hybrid_spin_unlock_irqrestore(struct raw_spinlock *rlock,
                     unsigned long flags)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
 
   /* Pick the flags before releasing the lock. */
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   __flags = lock->hwflags;
   hard_lock_release(rlock, _RET_IP_);
 
   if (running_inband()) {
       if (!flags) {
           trace_hardirqs_on();
           unstall_inband_nocheck();
       }
       hard_local_irq_restore(__flags);
       preempt_enable();
       return;
   }
 
   hard_local_irq_restore(__flags);
}
EXPORT_SYMBOL(__hybrid_spin_unlock_irqrestore);
 
int __hybrid_spin_trylock(struct raw_spinlock *rlock)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
 
   if (running_inband())
       preempt_disable();
 
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   __flags = hard_local_irq_save();
 
   hard_spin_trylock_prepare(rlock);
   if (do_raw_spin_trylock(rlock)) {
       lock->hwflags = __flags;
       hard_trylock_acquire(rlock, 1, _RET_IP_);
       return 1;
   }
 
   hard_spin_trylock_fail(rlock);
   hard_local_irq_restore(__flags);
 
   if (running_inband())
       preempt_enable();
 
   return 0;
}
EXPORT_SYMBOL(__hybrid_spin_trylock);
 
int __hybrid_spin_trylock_irqsave(struct raw_spinlock *rlock,
                  unsigned long *flags)
{
   struct hybrid_spinlock *lock;
   unsigned long __flags;
   bool inband;
 
   inband = running_inband();
 
   __flags = *flags = hard_local_irq_save();
 
   lock = container_of(rlock, struct hybrid_spinlock, rlock);
   if (inband) {
       *flags = test_and_stall_inband();
       trace_hardirqs_off();
       preempt_disable();
   }
 
   hard_spin_trylock_prepare(rlock);
   if (do_raw_spin_trylock(rlock)) {
       hard_trylock_acquire(rlock, 1, _RET_IP_);
       lock->hwflags = __flags;
       return 1;
   }
 
   hard_spin_trylock_fail(rlock);
 
   if (inband && !*flags) {
       trace_hardirqs_on();
       unstall_inband_nocheck();
   }
 
   hard_local_irq_restore(__flags);
 
   if (inband)
       preempt_enable();
 
   return 0;
}
EXPORT_SYMBOL(__hybrid_spin_trylock_irqsave);