notifier.c 16 KB

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