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