FROMLIST: psi: rename psi fields in preparation for psi trigger addition
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / kernel / notifier.c
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 * 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 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
34 static 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
49 static 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 * notifier_call_chain - Informs the registered notifiers about an event.
64 * @nl: Pointer to head of the blocking notifier chain
65 * @val: Value passed unmodified to notifier function
66 * @v: Pointer passed unmodified to notifier function
67 * @nr_to_call: Number of notifier functions to be called. Don't care
68 * value of this parameter is -1.
69 * @nr_calls: Records the number of notifications sent. Don't care
70 * value of this field is NULL.
71 * @returns: notifier_call_chain returns the value returned by the
72 * last notifier function called.
73 */
74 static 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 dbg_snapshot_print_notifier_call((void **)nl,
94 (unsigned long)nb->notifier_call, DSS_FLAG_IN);
95 ret = nb->notifier_call(nb, val, v);
96 dbg_snapshot_print_notifier_call((void **)nl,
97 (unsigned long)nb->notifier_call, DSS_FLAG_OUT);
98
99 if (nr_calls)
100 (*nr_calls)++;
101
102 if (ret & NOTIFY_STOP_MASK)
103 break;
104 nb = next_nb;
105 nr_to_call--;
106 }
107 return ret;
108 }
109 NOKPROBE_SYMBOL(notifier_call_chain);
110
111 /*
112 * Atomic notifier chain routines. Registration and unregistration
113 * use a spinlock, and call_chain is synchronized by RCU (no locks).
114 */
115
116 /**
117 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
118 * @nh: Pointer to head of the atomic notifier chain
119 * @n: New entry in notifier chain
120 *
121 * Adds a notifier to an atomic notifier chain.
122 *
123 * Currently always returns zero.
124 */
125 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
126 struct notifier_block *n)
127 {
128 unsigned long flags;
129 int ret;
130
131 spin_lock_irqsave(&nh->lock, flags);
132 ret = notifier_chain_register(&nh->head, n);
133 spin_unlock_irqrestore(&nh->lock, flags);
134 return ret;
135 }
136 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
137
138 /**
139 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
140 * @nh: Pointer to head of the atomic notifier chain
141 * @n: Entry to remove from notifier chain
142 *
143 * Removes a notifier from an atomic notifier chain.
144 *
145 * Returns zero on success or %-ENOENT on failure.
146 */
147 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
148 struct notifier_block *n)
149 {
150 unsigned long flags;
151 int ret;
152
153 spin_lock_irqsave(&nh->lock, flags);
154 ret = notifier_chain_unregister(&nh->head, n);
155 spin_unlock_irqrestore(&nh->lock, flags);
156 synchronize_rcu();
157 return ret;
158 }
159 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
160
161 /**
162 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
163 * @nh: Pointer to head of the atomic notifier chain
164 * @val: Value passed unmodified to notifier function
165 * @v: Pointer passed unmodified to notifier function
166 * @nr_to_call: See the comment for notifier_call_chain.
167 * @nr_calls: See the comment for notifier_call_chain.
168 *
169 * Calls each function in a notifier chain in turn. The functions
170 * run in an atomic context, so they must not block.
171 * This routine uses RCU to synchronize with changes to the chain.
172 *
173 * If the return value of the notifier can be and'ed
174 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
175 * will return immediately, with the return value of
176 * the notifier function which halted execution.
177 * Otherwise the return value is the return value
178 * of the last notifier function called.
179 */
180 int __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
181 unsigned long val, void *v,
182 int nr_to_call, int *nr_calls)
183 {
184 int ret;
185
186 rcu_read_lock();
187 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
188 rcu_read_unlock();
189 return ret;
190 }
191 EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
192 NOKPROBE_SYMBOL(__atomic_notifier_call_chain);
193
194 int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
195 unsigned long val, void *v)
196 {
197 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
198 }
199 EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
200 NOKPROBE_SYMBOL(atomic_notifier_call_chain);
201
202 /*
203 * Blocking notifier chain routines. All access to the chain is
204 * synchronized by an rwsem.
205 */
206
207 /**
208 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
209 * @nh: Pointer to head of the blocking notifier chain
210 * @n: New entry in notifier chain
211 *
212 * Adds a notifier to a blocking notifier chain.
213 * Must be called in process context.
214 *
215 * Currently always returns zero.
216 */
217 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
218 struct notifier_block *n)
219 {
220 int ret;
221
222 /*
223 * This code gets used during boot-up, when task switching is
224 * not yet working and interrupts must remain disabled. At
225 * such times we must not call down_write().
226 */
227 if (unlikely(system_state == SYSTEM_BOOTING))
228 return notifier_chain_register(&nh->head, n);
229
230 down_write(&nh->rwsem);
231 ret = notifier_chain_register(&nh->head, n);
232 up_write(&nh->rwsem);
233 return ret;
234 }
235 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
236
237 /**
238 * blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain
239 * @nh: Pointer to head of the blocking notifier chain
240 * @n: New entry in notifier chain
241 *
242 * Adds a notifier to a blocking notifier chain, only if not already
243 * present in the chain.
244 * Must be called in process context.
245 *
246 * Currently always returns zero.
247 */
248 int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
249 struct notifier_block *n)
250 {
251 int ret;
252
253 down_write(&nh->rwsem);
254 ret = notifier_chain_cond_register(&nh->head, n);
255 up_write(&nh->rwsem);
256 return ret;
257 }
258 EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
259
260 /**
261 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
262 * @nh: Pointer to head of the blocking notifier chain
263 * @n: Entry to remove from notifier chain
264 *
265 * Removes a notifier from a blocking notifier chain.
266 * Must be called from process context.
267 *
268 * Returns zero on success or %-ENOENT on failure.
269 */
270 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
271 struct notifier_block *n)
272 {
273 int ret;
274
275 /*
276 * This code gets used during boot-up, when task switching is
277 * not yet working and interrupts must remain disabled. At
278 * such times we must not call down_write().
279 */
280 if (unlikely(system_state == SYSTEM_BOOTING))
281 return notifier_chain_unregister(&nh->head, n);
282
283 down_write(&nh->rwsem);
284 ret = notifier_chain_unregister(&nh->head, n);
285 up_write(&nh->rwsem);
286 return ret;
287 }
288 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
289
290 /**
291 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
292 * @nh: Pointer to head of the blocking notifier chain
293 * @val: Value passed unmodified to notifier function
294 * @v: Pointer passed unmodified to notifier function
295 * @nr_to_call: See comment for notifier_call_chain.
296 * @nr_calls: See comment for notifier_call_chain.
297 *
298 * Calls each function in a notifier chain in turn. The functions
299 * run in a process context, so they are allowed to block.
300 *
301 * If the return value of the notifier can be and'ed
302 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
303 * will return immediately, with the return value of
304 * the notifier function which halted execution.
305 * Otherwise the return value is the return value
306 * of the last notifier function called.
307 */
308 int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
309 unsigned long val, void *v,
310 int nr_to_call, int *nr_calls)
311 {
312 int ret = NOTIFY_DONE;
313
314 /*
315 * We check the head outside the lock, but if this access is
316 * racy then it does not matter what the result of the test
317 * is, we re-check the list after having taken the lock anyway:
318 */
319 if (rcu_access_pointer(nh->head)) {
320 down_read(&nh->rwsem);
321 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
322 nr_calls);
323 up_read(&nh->rwsem);
324 }
325 return ret;
326 }
327 EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
328
329 int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
330 unsigned long val, void *v)
331 {
332 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
333 }
334 EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
335
336 /*
337 * Raw notifier chain routines. There is no protection;
338 * the caller must provide it. Use at your own risk!
339 */
340
341 /**
342 * raw_notifier_chain_register - Add notifier to a raw notifier chain
343 * @nh: Pointer to head of the raw notifier chain
344 * @n: New entry in notifier chain
345 *
346 * Adds a notifier to a raw notifier chain.
347 * All locking must be provided by the caller.
348 *
349 * Currently always returns zero.
350 */
351 int raw_notifier_chain_register(struct raw_notifier_head *nh,
352 struct notifier_block *n)
353 {
354 return notifier_chain_register(&nh->head, n);
355 }
356 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
357
358 /**
359 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
360 * @nh: Pointer to head of the raw notifier chain
361 * @n: Entry to remove from notifier chain
362 *
363 * Removes a notifier from a raw notifier chain.
364 * All locking must be provided by the caller.
365 *
366 * Returns zero on success or %-ENOENT on failure.
367 */
368 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
369 struct notifier_block *n)
370 {
371 return notifier_chain_unregister(&nh->head, n);
372 }
373 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
374
375 /**
376 * __raw_notifier_call_chain - Call functions in a raw notifier chain
377 * @nh: Pointer to head of the raw notifier chain
378 * @val: Value passed unmodified to notifier function
379 * @v: Pointer passed unmodified to notifier function
380 * @nr_to_call: See comment for notifier_call_chain.
381 * @nr_calls: See comment for notifier_call_chain
382 *
383 * Calls each function in a notifier chain in turn. The functions
384 * run in an undefined context.
385 * All locking must be provided by the caller.
386 *
387 * If the return value of the notifier can be and'ed
388 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
389 * will return immediately, with the return value of
390 * the notifier function which halted execution.
391 * Otherwise the return value is the return value
392 * of the last notifier function called.
393 */
394 int __raw_notifier_call_chain(struct raw_notifier_head *nh,
395 unsigned long val, void *v,
396 int nr_to_call, int *nr_calls)
397 {
398 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
399 }
400 EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
401
402 int raw_notifier_call_chain(struct raw_notifier_head *nh,
403 unsigned long val, void *v)
404 {
405 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
406 }
407 EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
408
409 #ifdef CONFIG_SRCU
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 #endif /* CONFIG_SRCU */
537
538 static ATOMIC_NOTIFIER_HEAD(die_chain);
539
540 int notrace 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 RCU_LOCKDEP_WARN(!rcu_is_watching(),
552 "notify_die called but RCU thinks we're quiescent");
553 return atomic_notifier_call_chain(&die_chain, val, &args);
554 }
555 NOKPROBE_SYMBOL(notify_die);
556
557 int register_die_notifier(struct notifier_block *nb)
558 {
559 vmalloc_sync_all();
560 return atomic_notifier_chain_register(&die_chain, nb);
561 }
562 EXPORT_SYMBOL_GPL(register_die_notifier);
563
564 int unregister_die_notifier(struct notifier_block *nb)
565 {
566 return atomic_notifier_chain_unregister(&die_chain, nb);
567 }
568 EXPORT_SYMBOL_GPL(unregister_die_notifier);