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 __kprobes 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}
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
121 struct notifier_block *n)
122{
123 unsigned long flags;
124 int ret;
125
126 spin_lock_irqsave(&nh->lock, flags);
127 ret = notifier_chain_register(&nh->head, n);
128 spin_unlock_irqrestore(&nh->lock, flags);
129 return ret;
130}
131EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
132
133
134
135
136
137
138
139
140
141
142int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
143 struct notifier_block *n)
144{
145 unsigned long flags;
146 int ret;
147
148 spin_lock_irqsave(&nh->lock, flags);
149 ret = notifier_chain_unregister(&nh->head, n);
150 spin_unlock_irqrestore(&nh->lock, flags);
151 synchronize_rcu();
152 return ret;
153}
154EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
176 unsigned long val, void *v,
177 int nr_to_call, int *nr_calls)
178{
179 int ret;
180
181 rcu_read_lock();
182 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
183 rcu_read_unlock();
184 return ret;
185}
186EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
187
188int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
189 unsigned long val, void *v)
190{
191 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
192}
193EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
211 struct notifier_block *n)
212{
213 int ret;
214
215
216
217
218
219
220 if (unlikely(system_state == SYSTEM_BOOTING))
221 return notifier_chain_register(&nh->head, n);
222
223 down_write(&nh->rwsem);
224 ret = notifier_chain_register(&nh->head, n);
225 up_write(&nh->rwsem);
226 return ret;
227}
228EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
229
230
231
232
233
234
235
236
237
238
239
240
241int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
242 struct notifier_block *n)
243{
244 int ret;
245
246 down_write(&nh->rwsem);
247 ret = notifier_chain_cond_register(&nh->head, n);
248 up_write(&nh->rwsem);
249 return ret;
250}
251EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
252
253
254
255
256
257
258
259
260
261
262
263int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
264 struct notifier_block *n)
265{
266 int ret;
267
268
269
270
271
272
273 if (unlikely(system_state == SYSTEM_BOOTING))
274 return notifier_chain_unregister(&nh->head, n);
275
276 down_write(&nh->rwsem);
277 ret = notifier_chain_unregister(&nh->head, n);
278 up_write(&nh->rwsem);
279 return ret;
280}
281EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
302 unsigned long val, void *v,
303 int nr_to_call, int *nr_calls)
304{
305 int ret = NOTIFY_DONE;
306
307
308
309
310
311
312 if (rcu_dereference_raw(nh->head)) {
313 down_read(&nh->rwsem);
314 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
315 nr_calls);
316 up_read(&nh->rwsem);
317 }
318 return ret;
319}
320EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
321
322int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
323 unsigned long val, void *v)
324{
325 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
326}
327EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344int raw_notifier_chain_register(struct raw_notifier_head *nh,
345 struct notifier_block *n)
346{
347 return notifier_chain_register(&nh->head, n);
348}
349EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
350
351
352
353
354
355
356
357
358
359
360
361int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
362 struct notifier_block *n)
363{
364 return notifier_chain_unregister(&nh->head, n);
365}
366EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387int __raw_notifier_call_chain(struct raw_notifier_head *nh,
388 unsigned long val, void *v,
389 int nr_to_call, int *nr_calls)
390{
391 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
392}
393EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
394
395int raw_notifier_call_chain(struct raw_notifier_head *nh,
396 unsigned long val, void *v)
397{
398 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
399}
400EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
418 struct notifier_block *n)
419{
420 int ret;
421
422
423
424
425
426
427 if (unlikely(system_state == SYSTEM_BOOTING))
428 return notifier_chain_register(&nh->head, n);
429
430 mutex_lock(&nh->mutex);
431 ret = notifier_chain_register(&nh->head, n);
432 mutex_unlock(&nh->mutex);
433 return ret;
434}
435EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
436
437
438
439
440
441
442
443
444
445
446
447int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
448 struct notifier_block *n)
449{
450 int ret;
451
452
453
454
455
456
457 if (unlikely(system_state == SYSTEM_BOOTING))
458 return notifier_chain_unregister(&nh->head, n);
459
460 mutex_lock(&nh->mutex);
461 ret = notifier_chain_unregister(&nh->head, n);
462 mutex_unlock(&nh->mutex);
463 synchronize_srcu(&nh->srcu);
464 return ret;
465}
466EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
487 unsigned long val, void *v,
488 int nr_to_call, int *nr_calls)
489{
490 int ret;
491 int idx;
492
493 idx = srcu_read_lock(&nh->srcu);
494 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
495 srcu_read_unlock(&nh->srcu, idx);
496 return ret;
497}
498EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
499
500int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
501 unsigned long val, void *v)
502{
503 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
504}
505EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
506
507
508
509
510
511
512
513
514
515
516
517
518
519void srcu_init_notifier_head(struct srcu_notifier_head *nh)
520{
521 mutex_init(&nh->mutex);
522 if (init_srcu_struct(&nh->srcu) < 0)
523 BUG();
524 nh->head = NULL;
525}
526EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
527
528static ATOMIC_NOTIFIER_HEAD(die_chain);
529
530int notrace __kprobes notify_die(enum die_val val, const char *str,
531 struct pt_regs *regs, long err, int trap, int sig)
532{
533 struct die_args args = {
534 .regs = regs,
535 .str = str,
536 .err = err,
537 .trapnr = trap,
538 .signr = sig,
539
540 };
541 return atomic_notifier_call_chain(&die_chain, val, &args);
542}
543
544int register_die_notifier(struct notifier_block *nb)
545{
546 vmalloc_sync_all();
547 return atomic_notifier_chain_register(&die_chain, nb);
548}
549EXPORT_SYMBOL_GPL(register_die_notifier);
550
551int unregister_die_notifier(struct notifier_block *nb)
552{
553 return atomic_notifier_chain_unregister(&die_chain, nb);
554}
555EXPORT_SYMBOL_GPL(unregister_die_notifier);
556