usb: gadget: f_mtp: Avoid race between mtp_read and mtp_function_disable
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / kernel / notifier.c
CommitLineData
fe9d4f57
AD
1#include <linux/kdebug.h>
2#include <linux/kprobes.h>
9984de1a 3#include <linux/export.h>
fe9d4f57
AD
4#include <linux/notifier.h>
5#include <linux/rcupdate.h>
6#include <linux/vmalloc.h>
c166f23c 7#include <linux/reboot.h>
1cac41cb
MB
8#include <linux/suspend.h>
9#include <linux/exynos-ss.h>
fe9d4f57
AD
10
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
6546bc42
ND
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
fe9d4f57
AD
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 */
b40a2cb6
MH
76static int notifier_call_chain(struct notifier_block **nl,
77 unsigned long val, void *v,
78 int nr_to_call, int *nr_calls)
fe9d4f57
AD
79{
80 int ret = NOTIFY_DONE;
81 struct notifier_block *nb, *next_nb;
82
d11c563d 83 nb = rcu_dereference_raw(*nl);
fe9d4f57
AD
84
85 while (nb && nr_to_call) {
d11c563d 86 next_nb = rcu_dereference_raw(nb->next);
1b2439db
AV
87
88#ifdef CONFIG_DEBUG_NOTIFIERS
ab7476cf 89 if (unlikely(!func_ptr_is_kernel_text(nb->notifier_call))) {
1b2439db
AV
90 WARN(1, "Invalid notifier called!");
91 nb = next_nb;
92 continue;
93 }
94#endif
1cac41cb
MB
95 if (val == PM_SUSPEND_PREPARE || val == PM_POST_SUSPEND)
96 exynos_ss_suspend(nb->notifier_call, NULL, ESS_FLAG_IN);
fe9d4f57 97 ret = nb->notifier_call(nb, val, v);
1cac41cb
MB
98 if (val == PM_SUSPEND_PREPARE || val == PM_POST_SUSPEND)
99 exynos_ss_suspend(nb->notifier_call, NULL, ESS_FLAG_OUT);
fe9d4f57
AD
100
101 if (nr_calls)
102 (*nr_calls)++;
103
104 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
105 break;
106 nb = next_nb;
107 nr_to_call--;
108 }
109 return ret;
110}
b40a2cb6 111NOKPROBE_SYMBOL(notifier_call_chain);
fe9d4f57
AD
112
113/*
114 * Atomic notifier chain routines. Registration and unregistration
115 * use a spinlock, and call_chain is synchronized by RCU (no locks).
116 */
117
118/**
119 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
120 * @nh: Pointer to head of the atomic notifier chain
121 * @n: New entry in notifier chain
122 *
123 * Adds a notifier to an atomic notifier chain.
124 *
125 * Currently always returns zero.
126 */
127int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
128 struct notifier_block *n)
129{
130 unsigned long flags;
131 int ret;
132
133 spin_lock_irqsave(&nh->lock, flags);
134 ret = notifier_chain_register(&nh->head, n);
135 spin_unlock_irqrestore(&nh->lock, flags);
136 return ret;
137}
138EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
139
140/**
141 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
142 * @nh: Pointer to head of the atomic notifier chain
143 * @n: Entry to remove from notifier chain
144 *
145 * Removes a notifier from an atomic notifier chain.
146 *
147 * Returns zero on success or %-ENOENT on failure.
148 */
149int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
150 struct notifier_block *n)
151{
152 unsigned long flags;
153 int ret;
154
155 spin_lock_irqsave(&nh->lock, flags);
156 ret = notifier_chain_unregister(&nh->head, n);
157 spin_unlock_irqrestore(&nh->lock, flags);
158 synchronize_rcu();
159 return ret;
160}
161EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
162
163/**
164 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
165 * @nh: Pointer to head of the atomic notifier chain
166 * @val: Value passed unmodified to notifier function
167 * @v: Pointer passed unmodified to notifier function
168 * @nr_to_call: See the comment for notifier_call_chain.
169 * @nr_calls: See the comment for notifier_call_chain.
170 *
171 * Calls each function in a notifier chain in turn. The functions
172 * run in an atomic context, so they must not block.
173 * This routine uses RCU to synchronize with changes to the chain.
174 *
175 * If the return value of the notifier can be and'ed
176 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
177 * will return immediately, with the return value of
178 * the notifier function which halted execution.
179 * Otherwise the return value is the return value
180 * of the last notifier function called.
181 */
b40a2cb6
MH
182int __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
183 unsigned long val, void *v,
184 int nr_to_call, int *nr_calls)
fe9d4f57
AD
185{
186 int ret;
187
188 rcu_read_lock();
189 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
190 rcu_read_unlock();
191 return ret;
192}
193EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
b40a2cb6 194NOKPROBE_SYMBOL(__atomic_notifier_call_chain);
fe9d4f57 195
b40a2cb6
MH
196int atomic_notifier_call_chain(struct atomic_notifier_head *nh,
197 unsigned long val, void *v)
fe9d4f57
AD
198{
199 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
200}
201EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
b40a2cb6 202NOKPROBE_SYMBOL(atomic_notifier_call_chain);
fe9d4f57
AD
203
204/*
205 * Blocking notifier chain routines. All access to the chain is
206 * synchronized by an rwsem.
207 */
208
209/**
210 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
211 * @nh: Pointer to head of the blocking notifier chain
212 * @n: New entry in notifier chain
213 *
214 * Adds a notifier to a blocking notifier chain.
215 * Must be called in process context.
216 *
217 * Currently always returns zero.
218 */
219int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
220 struct notifier_block *n)
221{
222 int ret;
223
224 /*
225 * This code gets used during boot-up, when task switching is
226 * not yet working and interrupts must remain disabled. At
227 * such times we must not call down_write().
228 */
229 if (unlikely(system_state == SYSTEM_BOOTING))
230 return notifier_chain_register(&nh->head, n);
231
232 down_write(&nh->rwsem);
233 ret = notifier_chain_register(&nh->head, n);
234 up_write(&nh->rwsem);
235 return ret;
236}
237EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
238
6546bc42
ND
239/**
240 * blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain
241 * @nh: Pointer to head of the blocking notifier chain
242 * @n: New entry in notifier chain
243 *
244 * Adds a notifier to a blocking notifier chain, only if not already
245 * present in the chain.
246 * Must be called in process context.
247 *
248 * Currently always returns zero.
249 */
250int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh,
251 struct notifier_block *n)
252{
253 int ret;
254
255 down_write(&nh->rwsem);
256 ret = notifier_chain_cond_register(&nh->head, n);
257 up_write(&nh->rwsem);
258 return ret;
259}
260EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register);
261
fe9d4f57
AD
262/**
263 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
264 * @nh: Pointer to head of the blocking notifier chain
265 * @n: Entry to remove from notifier chain
266 *
267 * Removes a notifier from a blocking notifier chain.
268 * Must be called from process context.
269 *
270 * Returns zero on success or %-ENOENT on failure.
271 */
272int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
273 struct notifier_block *n)
274{
275 int ret;
276
277 /*
278 * This code gets used during boot-up, when task switching is
279 * not yet working and interrupts must remain disabled. At
280 * such times we must not call down_write().
281 */
282 if (unlikely(system_state == SYSTEM_BOOTING))
283 return notifier_chain_unregister(&nh->head, n);
284
285 down_write(&nh->rwsem);
286 ret = notifier_chain_unregister(&nh->head, n);
287 up_write(&nh->rwsem);
288 return ret;
289}
290EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
291
292/**
293 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
294 * @nh: Pointer to head of the blocking notifier chain
295 * @val: Value passed unmodified to notifier function
296 * @v: Pointer passed unmodified to notifier function
297 * @nr_to_call: See comment for notifier_call_chain.
298 * @nr_calls: See comment for notifier_call_chain.
299 *
300 * Calls each function in a notifier chain in turn. The functions
301 * run in a process context, so they are allowed to block.
302 *
303 * If the return value of the notifier can be and'ed
304 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
305 * will return immediately, with the return value of
306 * the notifier function which halted execution.
307 * Otherwise the return value is the return value
308 * of the last notifier function called.
309 */
310int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
311 unsigned long val, void *v,
312 int nr_to_call, int *nr_calls)
313{
314 int ret = NOTIFY_DONE;
315
316 /*
317 * We check the head outside the lock, but if this access is
318 * racy then it does not matter what the result of the test
319 * is, we re-check the list after having taken the lock anyway:
320 */
8857563b 321 if (rcu_access_pointer(nh->head)) {
fe9d4f57
AD
322 down_read(&nh->rwsem);
323 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
324 nr_calls);
325 up_read(&nh->rwsem);
326 }
327 return ret;
328}
329EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
330
331int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
332 unsigned long val, void *v)
333{
334 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
335}
336EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
337
338/*
339 * Raw notifier chain routines. There is no protection;
340 * the caller must provide it. Use at your own risk!
341 */
342
343/**
344 * raw_notifier_chain_register - Add notifier to a raw notifier chain
345 * @nh: Pointer to head of the raw notifier chain
346 * @n: New entry in notifier chain
347 *
348 * Adds a notifier to a raw notifier chain.
349 * All locking must be provided by the caller.
350 *
351 * Currently always returns zero.
352 */
353int raw_notifier_chain_register(struct raw_notifier_head *nh,
354 struct notifier_block *n)
355{
356 return notifier_chain_register(&nh->head, n);
357}
358EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
359
360/**
361 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
362 * @nh: Pointer to head of the raw notifier chain
363 * @n: Entry to remove from notifier chain
364 *
365 * Removes a notifier from a raw notifier chain.
366 * All locking must be provided by the caller.
367 *
368 * Returns zero on success or %-ENOENT on failure.
369 */
370int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
371 struct notifier_block *n)
372{
373 return notifier_chain_unregister(&nh->head, n);
374}
375EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
376
377/**
378 * __raw_notifier_call_chain - Call functions in a raw notifier chain
379 * @nh: Pointer to head of the raw notifier chain
380 * @val: Value passed unmodified to notifier function
381 * @v: Pointer passed unmodified to notifier function
382 * @nr_to_call: See comment for notifier_call_chain.
383 * @nr_calls: See comment for notifier_call_chain
384 *
385 * Calls each function in a notifier chain in turn. The functions
386 * run in an undefined context.
387 * All locking must be provided by the caller.
388 *
389 * If the return value of the notifier can be and'ed
390 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
391 * will return immediately, with the return value of
392 * the notifier function which halted execution.
393 * Otherwise the return value is the return value
394 * of the last notifier function called.
395 */
396int __raw_notifier_call_chain(struct raw_notifier_head *nh,
397 unsigned long val, void *v,
398 int nr_to_call, int *nr_calls)
399{
400 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
401}
402EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
403
404int raw_notifier_call_chain(struct raw_notifier_head *nh,
405 unsigned long val, void *v)
406{
407 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
408}
409EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
410
83fe27ea 411#ifdef CONFIG_SRCU
fe9d4f57
AD
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
83fe27ea
PK
538#endif /* CONFIG_SRCU */
539
fe9d4f57
AD
540static ATOMIC_NOTIFIER_HEAD(die_chain);
541
b40a2cb6 542int notrace notify_die(enum die_val val, const char *str,
fe9d4f57
AD
543 struct pt_regs *regs, long err, int trap, int sig)
544{
545 struct die_args args = {
546 .regs = regs,
547 .str = str,
548 .err = err,
549 .trapnr = trap,
550 .signr = sig,
551
552 };
5778077d 553 RCU_LOCKDEP_WARN(!rcu_is_watching(),
e727c7d7 554 "notify_die called but RCU thinks we're quiescent");
fe9d4f57
AD
555 return atomic_notifier_call_chain(&die_chain, val, &args);
556}
b40a2cb6 557NOKPROBE_SYMBOL(notify_die);
fe9d4f57
AD
558
559int register_die_notifier(struct notifier_block *nb)
560{
561 vmalloc_sync_all();
562 return atomic_notifier_chain_register(&die_chain, nb);
563}
564EXPORT_SYMBOL_GPL(register_die_notifier);
565
566int unregister_die_notifier(struct notifier_block *nb)
567{
568 return atomic_notifier_chain_unregister(&die_chain, nb);
569}
570EXPORT_SYMBOL_GPL(unregister_die_notifier);