notifier.c 17 KB

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