notifier.c 17 KB

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