xref: /openbmc/linux/kernel/notifier.c (revision 1b2439dbb703ae8d95a9ce7ece6b7800b80f41f0)
1 #include <linux/kdebug.h>
2 #include <linux/kprobes.h>
3 #include <linux/module.h>
4 #include <linux/notifier.h>
5 #include <linux/rcupdate.h>
6 #include <linux/vmalloc.h>
7 #include <linux/reboot.h>
8 
9 /*
10  *	Notifier list for kernel code which wants to be called
11  *	at shutdown. This is used to stop any idling DMA operations
12  *	and the like.
13  */
14 BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
15 
16 /*
17  *	Notifier chain core routines.  The exported routines below
18  *	are layered on top of these, with appropriate locking added.
19  */
20 
21 static int notifier_chain_register(struct notifier_block **nl,
22 		struct notifier_block *n)
23 {
24 	if (!kernel_text_address((unsigned long)n->notifier_call)) {
25 		WARN(1, "Invalid notifier registered!");
26 		return 0;
27 	}
28 	while ((*nl) != NULL) {
29 		if (n->priority > (*nl)->priority)
30 			break;
31 		nl = &((*nl)->next);
32 	}
33 	n->next = *nl;
34 	rcu_assign_pointer(*nl, n);
35 	return 0;
36 }
37 
38 static int notifier_chain_cond_register(struct notifier_block **nl,
39 		struct notifier_block *n)
40 {
41 	if (!kernel_text_address((unsigned long)n->notifier_call)) {
42 		WARN(1, "Invalid notifier registered!");
43 		return 0;
44 	}
45 	while ((*nl) != NULL) {
46 		if ((*nl) == n)
47 			return 0;
48 		if (n->priority > (*nl)->priority)
49 			break;
50 		nl = &((*nl)->next);
51 	}
52 	n->next = *nl;
53 	rcu_assign_pointer(*nl, n);
54 	return 0;
55 }
56 
57 static int notifier_chain_unregister(struct notifier_block **nl,
58 		struct notifier_block *n)
59 {
60 	while ((*nl) != NULL) {
61 		if ((*nl) == n) {
62 			rcu_assign_pointer(*nl, n->next);
63 			return 0;
64 		}
65 		nl = &((*nl)->next);
66 	}
67 	return -ENOENT;
68 }
69 
70 /**
71  * notifier_call_chain - Informs the registered notifiers about an event.
72  *	@nl:		Pointer to head of the blocking notifier chain
73  *	@val:		Value passed unmodified to notifier function
74  *	@v:		Pointer passed unmodified to notifier function
75  *	@nr_to_call:	Number of notifier functions to be called. Don't care
76  *			value of this parameter is -1.
77  *	@nr_calls:	Records the number of notifications sent. Don't care
78  *			value of this field is NULL.
79  *	@returns:	notifier_call_chain returns the value returned by the
80  *			last notifier function called.
81  */
82 static int __kprobes notifier_call_chain(struct notifier_block **nl,
83 					unsigned long val, void *v,
84 					int nr_to_call,	int *nr_calls)
85 {
86 	int ret = NOTIFY_DONE;
87 	struct notifier_block *nb, *next_nb;
88 
89 	nb = rcu_dereference(*nl);
90 
91 	while (nb && nr_to_call) {
92 		next_nb = rcu_dereference(nb->next);
93 
94 #ifdef CONFIG_DEBUG_NOTIFIERS
95 		if (!kernel_text_address((unsigned long)nb->notifier_call)) {
96 			WARN(1, "Invalid notifier called!");
97 			nb = next_nb;
98 			continue;
99 		}
100 #endif
101 		ret = nb->notifier_call(nb, val, v);
102 
103 		if (nr_calls)
104 			(*nr_calls)++;
105 
106 		if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
107 			break;
108 		nb = next_nb;
109 		nr_to_call--;
110 	}
111 	return ret;
112 }
113 
114 /*
115  *	Atomic notifier chain routines.  Registration and unregistration
116  *	use a spinlock, and call_chain is synchronized by RCU (no locks).
117  */
118 
119 /**
120  *	atomic_notifier_chain_register - Add notifier to an atomic notifier chain
121  *	@nh: Pointer to head of the atomic notifier chain
122  *	@n: New entry in notifier chain
123  *
124  *	Adds a notifier to an atomic notifier chain.
125  *
126  *	Currently always returns zero.
127  */
128 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
129 		struct notifier_block *n)
130 {
131 	unsigned long flags;
132 	int ret;
133 
134 	spin_lock_irqsave(&nh->lock, flags);
135 	ret = notifier_chain_register(&nh->head, n);
136 	spin_unlock_irqrestore(&nh->lock, flags);
137 	return ret;
138 }
139 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
140 
141 /**
142  *	atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
143  *	@nh: Pointer to head of the atomic notifier chain
144  *	@n: Entry to remove from notifier chain
145  *
146  *	Removes a notifier from an atomic notifier chain.
147  *
148  *	Returns zero on success or %-ENOENT on failure.
149  */
150 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
151 		struct notifier_block *n)
152 {
153 	unsigned long flags;
154 	int ret;
155 
156 	spin_lock_irqsave(&nh->lock, flags);
157 	ret = notifier_chain_unregister(&nh->head, n);
158 	spin_unlock_irqrestore(&nh->lock, flags);
159 	synchronize_rcu();
160 	return ret;
161 }
162 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
163 
164 /**
165  *	__atomic_notifier_call_chain - Call functions in an atomic notifier chain
166  *	@nh: Pointer to head of the atomic notifier chain
167  *	@val: Value passed unmodified to notifier function
168  *	@v: Pointer passed unmodified to notifier function
169  *	@nr_to_call: See the comment for notifier_call_chain.
170  *	@nr_calls: See the comment for notifier_call_chain.
171  *
172  *	Calls each function in a notifier chain in turn.  The functions
173  *	run in an atomic context, so they must not block.
174  *	This routine uses RCU to synchronize with changes to the chain.
175  *
176  *	If the return value of the notifier can be and'ed
177  *	with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
178  *	will return immediately, with the return value of
179  *	the notifier function which halted execution.
180  *	Otherwise the return value is the return value
181  *	of the last notifier function called.
182  */
183 int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
184 					unsigned long val, void *v,
185 					int nr_to_call, int *nr_calls)
186 {
187 	int ret;
188 
189 	rcu_read_lock();
190 	ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
191 	rcu_read_unlock();
192 	return ret;
193 }
194 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
195 
196 int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
197 		unsigned long val, void *v)
198 {
199 	return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
200 }
201 EXPORT_SYMBOL_GPL(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_dereference(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 /*
411  *	SRCU notifier chain routines.    Registration and unregistration
412  *	use a mutex, and call_chain is synchronized by SRCU (no locks).
413  */
414 
415 /**
416  *	srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
417  *	@nh: Pointer to head of the SRCU notifier chain
418  *	@n: New entry in notifier chain
419  *
420  *	Adds a notifier to an SRCU notifier chain.
421  *	Must be called in process context.
422  *
423  *	Currently always returns zero.
424  */
425 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
426 		struct notifier_block *n)
427 {
428 	int ret;
429 
430 	/*
431 	 * This code gets used during boot-up, when task switching is
432 	 * not yet working and interrupts must remain disabled.  At
433 	 * such times we must not call mutex_lock().
434 	 */
435 	if (unlikely(system_state == SYSTEM_BOOTING))
436 		return notifier_chain_register(&nh->head, n);
437 
438 	mutex_lock(&nh->mutex);
439 	ret = notifier_chain_register(&nh->head, n);
440 	mutex_unlock(&nh->mutex);
441 	return ret;
442 }
443 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
444 
445 /**
446  *	srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
447  *	@nh: Pointer to head of the SRCU notifier chain
448  *	@n: Entry to remove from notifier chain
449  *
450  *	Removes a notifier from an SRCU notifier chain.
451  *	Must be called from process context.
452  *
453  *	Returns zero on success or %-ENOENT on failure.
454  */
455 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
456 		struct notifier_block *n)
457 {
458 	int ret;
459 
460 	/*
461 	 * This code gets used during boot-up, when task switching is
462 	 * not yet working and interrupts must remain disabled.  At
463 	 * such times we must not call mutex_lock().
464 	 */
465 	if (unlikely(system_state == SYSTEM_BOOTING))
466 		return notifier_chain_unregister(&nh->head, n);
467 
468 	mutex_lock(&nh->mutex);
469 	ret = notifier_chain_unregister(&nh->head, n);
470 	mutex_unlock(&nh->mutex);
471 	synchronize_srcu(&nh->srcu);
472 	return ret;
473 }
474 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
475 
476 /**
477  *	__srcu_notifier_call_chain - Call functions in an SRCU notifier chain
478  *	@nh: Pointer to head of the SRCU notifier chain
479  *	@val: Value passed unmodified to notifier function
480  *	@v: Pointer passed unmodified to notifier function
481  *	@nr_to_call: See comment for notifier_call_chain.
482  *	@nr_calls: See comment for notifier_call_chain
483  *
484  *	Calls each function in a notifier chain in turn.  The functions
485  *	run in a process context, so they are allowed to block.
486  *
487  *	If the return value of the notifier can be and'ed
488  *	with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
489  *	will return immediately, with the return value of
490  *	the notifier function which halted execution.
491  *	Otherwise the return value is the return value
492  *	of the last notifier function called.
493  */
494 int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
495 			       unsigned long val, void *v,
496 			       int nr_to_call, int *nr_calls)
497 {
498 	int ret;
499 	int idx;
500 
501 	idx = srcu_read_lock(&nh->srcu);
502 	ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
503 	srcu_read_unlock(&nh->srcu, idx);
504 	return ret;
505 }
506 EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
507 
508 int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
509 		unsigned long val, void *v)
510 {
511 	return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
512 }
513 EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
514 
515 /**
516  *	srcu_init_notifier_head - Initialize an SRCU notifier head
517  *	@nh: Pointer to head of the srcu notifier chain
518  *
519  *	Unlike other sorts of notifier heads, SRCU notifier heads require
520  *	dynamic initialization.  Be sure to call this routine before
521  *	calling any of the other SRCU notifier routines for this head.
522  *
523  *	If an SRCU notifier head is deallocated, it must first be cleaned
524  *	up by calling srcu_cleanup_notifier_head().  Otherwise the head's
525  *	per-cpu data (used by the SRCU mechanism) will leak.
526  */
527 void srcu_init_notifier_head(struct srcu_notifier_head *nh)
528 {
529 	mutex_init(&nh->mutex);
530 	if (init_srcu_struct(&nh->srcu) < 0)
531 		BUG();
532 	nh->head = NULL;
533 }
534 EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
535 
536 /**
537  *	register_reboot_notifier - Register function to be called at reboot time
538  *	@nb: Info about notifier function to be called
539  *
540  *	Registers a function with the list of functions
541  *	to be called at reboot time.
542  *
543  *	Currently always returns zero, as blocking_notifier_chain_register()
544  *	always returns zero.
545  */
546 int register_reboot_notifier(struct notifier_block *nb)
547 {
548 	return blocking_notifier_chain_register(&reboot_notifier_list, nb);
549 }
550 EXPORT_SYMBOL(register_reboot_notifier);
551 
552 /**
553  *	unregister_reboot_notifier - Unregister previously registered reboot notifier
554  *	@nb: Hook to be unregistered
555  *
556  *	Unregisters a previously registered reboot
557  *	notifier function.
558  *
559  *	Returns zero on success, or %-ENOENT on failure.
560  */
561 int unregister_reboot_notifier(struct notifier_block *nb)
562 {
563 	return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
564 }
565 EXPORT_SYMBOL(unregister_reboot_notifier);
566 
567 static ATOMIC_NOTIFIER_HEAD(die_chain);
568 
569 int notify_die(enum die_val val, const char *str,
570 	       struct pt_regs *regs, long err, int trap, int sig)
571 {
572 	struct die_args args = {
573 		.regs	= regs,
574 		.str	= str,
575 		.err	= err,
576 		.trapnr	= trap,
577 		.signr	= sig,
578 
579 	};
580 	return atomic_notifier_call_chain(&die_chain, val, &args);
581 }
582 
583 int register_die_notifier(struct notifier_block *nb)
584 {
585 	vmalloc_sync_all();
586 	return atomic_notifier_chain_register(&die_chain, nb);
587 }
588 EXPORT_SYMBOL_GPL(register_die_notifier);
589 
590 int unregister_die_notifier(struct notifier_block *nb)
591 {
592 	return atomic_notifier_chain_unregister(&die_chain, nb);
593 }
594 EXPORT_SYMBOL_GPL(unregister_die_notifier);
595