notifier.c 18 KB

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