blob: 361009e3e8ff668c7040cf0f439fa5f036bf5de4 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#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#ifdef CONFIG_NET
9#include <net/SI/print_sun.h>
10#endif
11/*
12 * Notifier list for kernel code which wants to be called
13 * at shutdown. This is used to stop any idling DMA operations
14 * and the like.
15 */
16BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
17
18/*
19 * Notifier chain core routines. The exported routines below
20 * are layered on top of these, with appropriate locking added.
21 */
22
23static int notifier_chain_register(struct notifier_block **nl,
24 struct notifier_block *n)
25{
26 while ((*nl) != NULL) {
27 if (n->priority > (*nl)->priority)
28 break;
29 nl = &((*nl)->next);
30 }
31 n->next = *nl;
32 rcu_assign_pointer(*nl, n);
33 return 0;
34}
35
36static int notifier_chain_cond_register(struct notifier_block **nl,
37 struct notifier_block *n)
38{
39 while ((*nl) != NULL) {
40 if ((*nl) == n)
41 return 0;
42 if (n->priority > (*nl)->priority)
43 break;
44 nl = &((*nl)->next);
45 }
46 n->next = *nl;
47 rcu_assign_pointer(*nl, n);
48 return 0;
49}
50
51static int notifier_chain_unregister(struct notifier_block **nl,
52 struct notifier_block *n)
53{
54 while ((*nl) != NULL) {
55 if ((*nl) == n) {
56 rcu_assign_pointer(*nl, n->next);
57 return 0;
58 }
59 nl = &((*nl)->next);
60 }
61 return -ENOENT;
62}
63
64/**
65 * notifier_call_chain - Informs the registered notifiers about an event.
66 * @nl: Pointer to head of the blocking notifier chain
67 * @val: Value passed unmodified to notifier function
68 * @v: Pointer passed unmodified to notifier function
69 * @nr_to_call: Number of notifier functions to be called. Don't care
70 * value of this parameter is -1.
71 * @nr_calls: Records the number of notifications sent. Don't care
72 * value of this field is NULL.
73 * @returns: notifier_call_chain returns the value returned by the
74 * last notifier function called.
75 */
76static int __kprobes notifier_call_chain(struct notifier_block **nl,
77 unsigned long val, void *v,
78 int nr_to_call, int *nr_calls)
79{
80 int ret = NOTIFY_DONE;
81 struct notifier_block *nb, *next_nb;
82#ifdef CONFIG_NET
83 char cpufreq[100];
84#endif
85
86 nb = rcu_dereference_raw(*nl);
87
88 while (nb && nr_to_call) {
89 next_nb = rcu_dereference_raw(nb->next);
90
91#ifdef CONFIG_DEBUG_NOTIFIERS
92 if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
93 WARN(1, "Invalid notifier called!");
94 nb = next_nb;
95 continue;
96 }
97#endif
98#ifdef CONFIG_NET
99 sprintf(cpufreq,"%pf",nb->notifier_call);
100 if(strncmp(cpufreq, "cpufreq",7))
101 net_run_track(PRT_NOTIFY_CALL,"net team add ,event = %d ;func =%pf",val,nb->notifier_call);
102#endif
103 ret = nb->notifier_call(nb, val, v);
104
105 if (nr_calls)
106 (*nr_calls)++;
107
108 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
109 break;
110 nb = next_nb;
111 nr_to_call--;
112 }
113 return ret;
114}
115
116/*
117 * Atomic notifier chain routines. Registration and unregistration
118 * use a spinlock, and call_chain is synchronized by RCU (no locks).
119 */
120
121/**
122 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
123 * @nh: Pointer to head of the atomic notifier chain
124 * @n: New entry in notifier chain
125 *
126 * Adds a notifier to an atomic notifier chain.
127 *
128 * Currently always returns zero.
129 */
130int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
131 struct notifier_block *n)
132{
133 unsigned long flags;
134 int ret;
135
136 spin_lock_irqsave(&nh->lock, flags);
137 ret = notifier_chain_register(&nh->head, n);
138 spin_unlock_irqrestore(&nh->lock, flags);
139 return ret;
140}
141EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
142
143/**
144 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
145 * @nh: Pointer to head of the atomic notifier chain
146 * @n: Entry to remove from notifier chain
147 *
148 * Removes a notifier from an atomic notifier chain.
149 *
150 * Returns zero on success or %-ENOENT on failure.
151 */
152int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
153 struct notifier_block *n)
154{
155 unsigned long flags;
156 int ret;
157
158 spin_lock_irqsave(&nh->lock, flags);
159 ret = notifier_chain_unregister(&nh->head, n);
160 spin_unlock_irqrestore(&nh->lock, flags);
161 synchronize_rcu();
162 return ret;
163}
164EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
165
166/**
167 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
168 * @nh: Pointer to head of the atomic notifier chain
169 * @val: Value passed unmodified to notifier function
170 * @v: Pointer passed unmodified to notifier function
171 * @nr_to_call: See the comment for notifier_call_chain.
172 * @nr_calls: See the comment for notifier_call_chain.
173 *
174 * Calls each function in a notifier chain in turn. The functions
175 * run in an atomic context, so they must not block.
176 * This routine uses RCU to synchronize with changes to the chain.
177 *
178 * If the return value of the notifier can be and'ed
179 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
180 * will return immediately, with the return value of
181 * the notifier function which halted execution.
182 * Otherwise the return value is the return value
183 * of the last notifier function called.
184 */
185int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
186 unsigned long val, void *v,
187 int nr_to_call, int *nr_calls)
188{
189 int ret;
190
191 rcu_read_lock();
192 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
193 rcu_read_unlock();
194 return ret;
195}
196EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
197
198int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
199 unsigned long val, void *v)
200{
201 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
202}
203EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
204
205/*
206 * Blocking notifier chain routines. All access to the chain is
207 * synchronized by an rwsem.
208 */
209
210/**
211 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
212 * @nh: Pointer to head of the blocking notifier chain
213 * @n: New entry in notifier chain
214 *
215 * Adds a notifier to a blocking notifier chain.
216 * Must be called in process context.
217 *
218 * Currently always returns zero.
219 */
220int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
221 struct notifier_block *n)
222{
223 int ret;
224
225 /*
226 * This code gets used during boot-up, when task switching is
227 * not yet working and interrupts must remain disabled. At
228 * such times we must not call down_write().
229 */
230 if (unlikely(system_state == SYSTEM_BOOTING))
231 return notifier_chain_register(&nh->head, n);
232
233 down_write(&nh->rwsem);
234 ret = notifier_chain_register(&nh->head, n);
235 up_write(&nh->rwsem);
236 return ret;
237}
238EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
239
240/**
241 * blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain
242 * @nh: Pointer to head of the blocking notifier chain
243 * @n: New entry in notifier chain
244 *
245 * Adds a notifier to a blocking notifier chain, only if not already
246 * present in the chain.
247 * Must be called in process context.
248 *
249 * Currently always returns zero.
250 */
251int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
252 struct notifier_block *n)
253{
254 int ret;
255
256 down_write(&nh->rwsem);
257 ret = notifier_chain_cond_register(&nh->head, n);
258 up_write(&nh->rwsem);
259 return ret;
260}
261EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
262
263/**
264 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
265 * @nh: Pointer to head of the blocking notifier chain
266 * @n: Entry to remove from notifier chain
267 *
268 * Removes a notifier from a blocking notifier chain.
269 * Must be called from process context.
270 *
271 * Returns zero on success or %-ENOENT on failure.
272 */
273int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
274 struct notifier_block *n)
275{
276 int ret;
277
278 /*
279 * This code gets used during boot-up, when task switching is
280 * not yet working and interrupts must remain disabled. At
281 * such times we must not call down_write().
282 */
283 if (unlikely(system_state == SYSTEM_BOOTING))
284 return notifier_chain_unregister(&nh->head, n);
285
286 down_write(&nh->rwsem);
287 ret = notifier_chain_unregister(&nh->head, n);
288 up_write(&nh->rwsem);
289 return ret;
290}
291EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
292
293/**
294 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
295 * @nh: Pointer to head of the blocking notifier chain
296 * @val: Value passed unmodified to notifier function
297 * @v: Pointer passed unmodified to notifier function
298 * @nr_to_call: See comment for notifier_call_chain.
299 * @nr_calls: See comment for notifier_call_chain.
300 *
301 * Calls each function in a notifier chain in turn. The functions
302 * run in a process context, so they are allowed to block.
303 *
304 * If the return value of the notifier can be and'ed
305 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
306 * will return immediately, with the return value of
307 * the notifier function which halted execution.
308 * Otherwise the return value is the return value
309 * of the last notifier function called.
310 */
311int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
312 unsigned long val, void *v,
313 int nr_to_call, int *nr_calls)
314{
315 int ret = NOTIFY_DONE;
316
317 /*
318 * We check the head outside the lock, but if this access is
319 * racy then it does not matter what the result of the test
320 * is, we re-check the list after having taken the lock anyway:
321 */
322 if (rcu_dereference_raw(nh->head)) {
323 down_read(&nh->rwsem);
324 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
325 nr_calls);
326 up_read(&nh->rwsem);
327 }
328 return ret;
329}
330EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
331
332int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
333 unsigned long val, void *v)
334{
335 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
336}
337EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
338
339/*
340 * Raw notifier chain routines. There is no protection;
341 * the caller must provide it. Use at your own risk!
342 */
343
344/**
345 * raw_notifier_chain_register - Add notifier to a raw notifier chain
346 * @nh: Pointer to head of the raw notifier chain
347 * @n: New entry in notifier chain
348 *
349 * Adds a notifier to a raw notifier chain.
350 * All locking must be provided by the caller.
351 *
352 * Currently always returns zero.
353 */
354int raw_notifier_chain_register(struct raw_notifier_head *nh,
355 struct notifier_block *n)
356{
357 return notifier_chain_register(&nh->head, n);
358}
359EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
360
361/**
362 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
363 * @nh: Pointer to head of the raw notifier chain
364 * @n: Entry to remove from notifier chain
365 *
366 * Removes a notifier from a raw notifier chain.
367 * All locking must be provided by the caller.
368 *
369 * Returns zero on success or %-ENOENT on failure.
370 */
371int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
372 struct notifier_block *n)
373{
374 return notifier_chain_unregister(&nh->head, n);
375}
376EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
377
378/**
379 * __raw_notifier_call_chain - Call functions in a raw notifier chain
380 * @nh: Pointer to head of the raw notifier chain
381 * @val: Value passed unmodified to notifier function
382 * @v: Pointer passed unmodified to notifier function
383 * @nr_to_call: See comment for notifier_call_chain.
384 * @nr_calls: See comment for notifier_call_chain
385 *
386 * Calls each function in a notifier chain in turn. The functions
387 * run in an undefined context.
388 * All locking must be provided by the caller.
389 *
390 * If the return value of the notifier can be and'ed
391 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
392 * will return immediately, with the return value of
393 * the notifier function which halted execution.
394 * Otherwise the return value is the return value
395 * of the last notifier function called.
396 */
397int __raw_notifier_call_chain(struct raw_notifier_head *nh,
398 unsigned long val, void *v,
399 int nr_to_call, int *nr_calls)
400{
401 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
402}
403EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
404
405int raw_notifier_call_chain(struct raw_notifier_head *nh,
406 unsigned long val, void *v)
407{
408 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
409}
410EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
411
412/*
413 * SRCU notifier chain routines. Registration and unregistration
414 * use a mutex, and call_chain is synchronized by SRCU (no locks).
415 */
416
417/**
418 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
419 * @nh: Pointer to head of the SRCU notifier chain
420 * @n: New entry in notifier chain
421 *
422 * Adds a notifier to an SRCU notifier chain.
423 * Must be called in process context.
424 *
425 * Currently always returns zero.
426 */
427int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
428 struct notifier_block *n)
429{
430 int ret;
431
432 /*
433 * This code gets used during boot-up, when task switching is
434 * not yet working and interrupts must remain disabled. At
435 * such times we must not call mutex_lock().
436 */
437 if (unlikely(system_state == SYSTEM_BOOTING))
438 return notifier_chain_register(&nh->head, n);
439
440 mutex_lock(&nh->mutex);
441 ret = notifier_chain_register(&nh->head, n);
442 mutex_unlock(&nh->mutex);
443 return ret;
444}
445EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
446
447/**
448 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
449 * @nh: Pointer to head of the SRCU notifier chain
450 * @n: Entry to remove from notifier chain
451 *
452 * Removes a notifier from an SRCU notifier chain.
453 * Must be called from process context.
454 *
455 * Returns zero on success or %-ENOENT on failure.
456 */
457int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
458 struct notifier_block *n)
459{
460 int ret;
461
462 /*
463 * This code gets used during boot-up, when task switching is
464 * not yet working and interrupts must remain disabled. At
465 * such times we must not call mutex_lock().
466 */
467 if (unlikely(system_state == SYSTEM_BOOTING))
468 return notifier_chain_unregister(&nh->head, n);
469
470 mutex_lock(&nh->mutex);
471 ret = notifier_chain_unregister(&nh->head, n);
472 mutex_unlock(&nh->mutex);
473 synchronize_srcu(&nh->srcu);
474 return ret;
475}
476EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
477
478/**
479 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
480 * @nh: Pointer to head of the SRCU notifier chain
481 * @val: Value passed unmodified to notifier function
482 * @v: Pointer passed unmodified to notifier function
483 * @nr_to_call: See comment for notifier_call_chain.
484 * @nr_calls: See comment for notifier_call_chain
485 *
486 * Calls each function in a notifier chain in turn. The functions
487 * run in a process context, so they are allowed to block.
488 *
489 * If the return value of the notifier can be and'ed
490 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
491 * will return immediately, with the return value of
492 * the notifier function which halted execution.
493 * Otherwise the return value is the return value
494 * of the last notifier function called.
495 */
496int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
497 unsigned long val, void *v,
498 int nr_to_call, int *nr_calls)
499{
500 int ret;
501 int idx;
502
503 idx = srcu_read_lock(&nh->srcu);
504 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
505 srcu_read_unlock(&nh->srcu, idx);
506 return ret;
507}
508EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
509
510int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
511 unsigned long val, void *v)
512{
513 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
514}
515EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
516
517/**
518 * srcu_init_notifier_head - Initialize an SRCU notifier head
519 * @nh: Pointer to head of the srcu notifier chain
520 *
521 * Unlike other sorts of notifier heads, SRCU notifier heads require
522 * dynamic initialization. Be sure to call this routine before
523 * calling any of the other SRCU notifier routines for this head.
524 *
525 * If an SRCU notifier head is deallocated, it must first be cleaned
526 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
527 * per-cpu data (used by the SRCU mechanism) will leak.
528 */
529void srcu_init_notifier_head(struct srcu_notifier_head *nh)
530{
531 mutex_init(&nh->mutex);
532 if (init_srcu_struct(&nh->srcu) < 0)
533 BUG();
534 nh->head = NULL;
535}
536EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
537
538static ATOMIC_NOTIFIER_HEAD(die_chain);
539
540int notrace __kprobes notify_die(enum die_val val, const char *str,
541 struct pt_regs *regs, long err, int trap, int sig)
542{
543 struct die_args args = {
544 .regs = regs,
545 .str = str,
546 .err = err,
547 .trapnr = trap,
548 .signr = sig,
549
550 };
551 return atomic_notifier_call_chain(&die_chain, val, &args);
552}
553
554int register_die_notifier(struct notifier_block *nb)
555{
556 vmalloc_sync_all();
557 return atomic_notifier_chain_register(&die_chain, nb);
558}
559EXPORT_SYMBOL_GPL(register_die_notifier);
560
561int unregister_die_notifier(struct notifier_block *nb)
562{
563 return atomic_notifier_chain_unregister(&die_chain, nb);
564}
565EXPORT_SYMBOL_GPL(unregister_die_notifier);