notifier.c 18 KB

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