notifier.c 17 KB

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