notifier.c 16 KB

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