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