1
2#include <linux/kdebug.h>
3#include <linux/kprobes.h>
4#include <linux/export.h>
5#include <linux/notifier.h>
6#include <linux/rcupdate.h>
7#include <linux/vmalloc.h>
8#include <linux/reboot.h>
9
10
11
12
13
14
15BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
16
17
18
19
20
21
22static int notifier_chain_register(struct notifier_block **nl,
23 struct notifier_block *n)
24{
25 while ((*nl) != NULL) {
26 if (unlikely((*nl) == n)) {
27 WARN(1, "double register detected");
28 return 0;
29 }
30 if (n->priority > (*nl)->priority)
31 break;
32 nl = &((*nl)->next);
33 }
34 n->next = *nl;
35 rcu_assign_pointer(*nl, n);
36 return 0;
37}
38
39static int notifier_chain_unregister(struct notifier_block **nl,
40 struct notifier_block *n)
41{
42 while ((*nl) != NULL) {
43 if ((*nl) == n) {
44 rcu_assign_pointer(*nl, n->next);
45 return 0;
46 }
47 nl = &((*nl)->next);
48 }
49 return -ENOENT;
50}
51
52
53
54
55
56
57
58
59
60
61
62
63
64static int notifier_call_chain(struct notifier_block **nl,
65 unsigned long val, void *v,
66 int nr_to_call, int *nr_calls)
67{
68 int ret = NOTIFY_DONE;
69 struct notifier_block *nb, *next_nb;
70
71 nb = rcu_dereference_raw(*nl);
72
73 while (nb && nr_to_call) {
74 next_nb = rcu_dereference_raw(nb->next);
75
76#ifdef CONFIG_DEBUG_NOTIFIERS
77 if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
78 WARN(1, "Invalid notifier called!");
79 nb = next_nb;
80 continue;
81 }
82#endif
83 ret = nb->notifier_call(nb, val, v);
84
85 if (nr_calls)
86 (*nr_calls)++;
87
88 if (ret & NOTIFY_STOP_MASK)
89 break;
90 nb = next_nb;
91 nr_to_call--;
92 }
93 return ret;
94}
95NOKPROBE_SYMBOL(notifier_call_chain);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112static int notifier_call_chain_robust(struct notifier_block **nl,
113 unsigned long val_up, unsigned long val_down,
114 void *v)
115{
116 int ret, nr = 0;
117
118 ret = notifier_call_chain(nl, val_up, v, -1, &nr);
119 if (ret & NOTIFY_STOP_MASK)
120 notifier_call_chain(nl, val_down, v, nr-1, NULL);
121
122 return ret;
123}
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
140 struct notifier_block *n)
141{
142 unsigned long flags;
143 int ret;
144
145 spin_lock_irqsave(&nh->lock, flags);
146 ret = notifier_chain_register(&nh->head, n);
147 spin_unlock_irqrestore(&nh->lock, flags);
148 return ret;
149}
150EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
151
152
153
154
155
156
157
158
159
160
161int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
162 struct notifier_block *n)
163{
164 unsigned long flags;
165 int ret;
166
167 spin_lock_irqsave(&nh->lock, flags);
168 ret = notifier_chain_unregister(&nh->head, n);
169 spin_unlock_irqrestore(&nh->lock, flags);
170 synchronize_rcu();
171 return ret;
172}
173EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
193 unsigned long val, void *v)
194{
195 int ret;
196
197 rcu_read_lock();
198 ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
199 rcu_read_unlock();
200
201 return ret;
202}
203EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
204NOKPROBE_SYMBOL(atomic_notifier_call_chain);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
222 struct notifier_block *n)
223{
224 int ret;
225
226
227
228
229
230
231 if (unlikely(system_state == SYSTEM_BOOTING))
232 return notifier_chain_register(&nh->head, n);
233
234 down_write(&nh->rwsem);
235 ret = notifier_chain_register(&nh->head, n);
236 up_write(&nh->rwsem);
237 return ret;
238}
239EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
240
241
242
243
244
245
246
247
248
249
250
251int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
252 struct notifier_block *n)
253{
254 int ret;
255
256
257
258
259
260
261 if (unlikely(system_state == SYSTEM_BOOTING))
262 return notifier_chain_unregister(&nh->head, n);
263
264 down_write(&nh->rwsem);
265 ret = notifier_chain_unregister(&nh->head, n);
266 up_write(&nh->rwsem);
267 return ret;
268}
269EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
270
271int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh,
272 unsigned long val_up, unsigned long val_down, void *v)
273{
274 int ret = NOTIFY_DONE;
275
276
277
278
279
280
281 if (rcu_access_pointer(nh->head)) {
282 down_read(&nh->rwsem);
283 ret = notifier_call_chain_robust(&nh->head, val_up, val_down, v);
284 up_read(&nh->rwsem);
285 }
286 return ret;
287}
288EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_robust);
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
307 unsigned long val, void *v)
308{
309 int ret = NOTIFY_DONE;
310
311
312
313
314
315
316 if (rcu_access_pointer(nh->head)) {
317 down_read(&nh->rwsem);
318 ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
319 up_read(&nh->rwsem);
320 }
321 return ret;
322}
323EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340int raw_notifier_chain_register(struct raw_notifier_head *nh,
341 struct notifier_block *n)
342{
343 return notifier_chain_register(&nh->head, n);
344}
345EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
346
347
348
349
350
351
352
353
354
355
356
357int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
358 struct notifier_block *n)
359{
360 return notifier_chain_unregister(&nh->head, n);
361}
362EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
363
364int raw_notifier_call_chain_robust(struct raw_notifier_head *nh,
365 unsigned long val_up, unsigned long val_down, void *v)
366{
367 return notifier_call_chain_robust(&nh->head, val_up, val_down, v);
368}
369EXPORT_SYMBOL_GPL(raw_notifier_call_chain_robust);
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388int raw_notifier_call_chain(struct raw_notifier_head *nh,
389 unsigned long val, void *v)
390{
391 return notifier_call_chain(&nh->head, val, v, -1, NULL);
392}
393EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
394
395#ifdef CONFIG_SRCU
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
412 struct notifier_block *n)
413{
414 int ret;
415
416
417
418
419
420
421 if (unlikely(system_state == SYSTEM_BOOTING))
422 return notifier_chain_register(&nh->head, n);
423
424 mutex_lock(&nh->mutex);
425 ret = notifier_chain_register(&nh->head, n);
426 mutex_unlock(&nh->mutex);
427 return ret;
428}
429EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
430
431
432
433
434
435
436
437
438
439
440
441int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
442 struct notifier_block *n)
443{
444 int ret;
445
446
447
448
449
450
451 if (unlikely(system_state == SYSTEM_BOOTING))
452 return notifier_chain_unregister(&nh->head, n);
453
454 mutex_lock(&nh->mutex);
455 ret = notifier_chain_unregister(&nh->head, n);
456 mutex_unlock(&nh->mutex);
457 synchronize_srcu(&nh->srcu);
458 return ret;
459}
460EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
479 unsigned long val, void *v)
480{
481 int ret;
482 int idx;
483
484 idx = srcu_read_lock(&nh->srcu);
485 ret = notifier_call_chain(&nh->head, val, v, -1, NULL);
486 srcu_read_unlock(&nh->srcu, idx);
487 return ret;
488}
489EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
490
491
492
493
494
495
496
497
498
499
500
501
502
503void srcu_init_notifier_head(struct srcu_notifier_head *nh)
504{
505 mutex_init(&nh->mutex);
506 if (init_srcu_struct(&nh->srcu) < 0)
507 BUG();
508 nh->head = NULL;
509}
510EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
511
512#endif
513
514static ATOMIC_NOTIFIER_HEAD(die_chain);
515
516int notrace notify_die(enum die_val val, const char *str,
517 struct pt_regs *regs, long err, int trap, int sig)
518{
519 struct die_args args = {
520 .regs = regs,
521 .str = str,
522 .err = err,
523 .trapnr = trap,
524 .signr = sig,
525
526 };
527 RCU_LOCKDEP_WARN(!rcu_is_watching(),
528 "notify_die called but RCU thinks we're quiescent");
529 return atomic_notifier_call_chain(&die_chain, val, &args);
530}
531NOKPROBE_SYMBOL(notify_die);
532
533int register_die_notifier(struct notifier_block *nb)
534{
535 return atomic_notifier_chain_register(&die_chain, nb);
536}
537EXPORT_SYMBOL_GPL(register_die_notifier);
538
539int unregister_die_notifier(struct notifier_block *nb)
540{
541 return atomic_notifier_chain_unregister(&die_chain, nb);
542}
543EXPORT_SYMBOL_GPL(unregister_die_notifier);
544