notifier.c 16 KB

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