History

notifier: Return an error when a callback has already been registered

Return -EEXIST when a notifier callback has already been registered on a notifier chain. This should avoid any homegrown registration tracking at the callsite like https://lore.kernel.org/amd-gfx/20210512013058.6827-1-mukul.joshi@amd.com for example. This version is an alternative of https://lore.kernel.org/r/20211108101157.15189-1-bp@alien8.de which needed to touch every caller not checking the registration routine's return value.

Borislav Petkov committed 4 years ago

Commit 5abb0651 parent fc74e0a1 file · +8 −7Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/notifier.c+8−7
@@ -20,12 +20,13 @@ BLOCKING_NOTIFIER_HEAD(reboot_notifier_list); · 19 unmodified lines
2020 */
2121
2222 static int notifier_chain_register(struct notifier_block **nl,
23− struct notifier_block *n)
23+ struct notifier_block *n)
2424 {
2525 while ((*nl) != NULL) {
2626 if (unlikely((*nl) == n)) {
27− WARN(1, "double register detected");
28− return 0;
27+ WARN(1, "notifier callback %ps already registered",
28+ n->notifier_call);
29+ return -EEXIST;
2930 }
3031 if (n->priority > (*nl)->priority)
3132 break;
@@ -134,7 +135,7 @@ static int notifier_call_chain_robust(struct notifier_block **nl, · 102 unmodified lines
134135 *
135136 * Adds a notifier to an atomic notifier chain.
136137 *
137− * Currently always returns zero.
138+ * Returns 0 on success, %-EEXIST on error.
138139 */
139140 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
140141 struct notifier_block *n)
@@ -216,7 +217,7 @@ NOKPROBE_SYMBOL(atomic_notifier_call_chain); · 75 unmodified lines
216217 * Adds a notifier to a blocking notifier chain.
217218 * Must be called in process context.
218219 *
219− * Currently always returns zero.
220+ * Returns 0 on success, %-EEXIST on error.
220221 */
221222 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
222223 struct notifier_block *n)
@@ -335,7 +336,7 @@ EXPORT_SYMBOL_GPL(blocking_notifier_call_chain); · 112 unmodified lines
335336 * Adds a notifier to a raw notifier chain.
336337 * All locking must be provided by the caller.
337338 *
338− * Currently always returns zero.
339+ * Returns 0 on success, %-EEXIST on error.
339340 */
340341 int raw_notifier_chain_register(struct raw_notifier_head *nh,
341342 struct notifier_block *n)
@@ -406,7 +407,7 @@ EXPORT_SYMBOL_GPL(raw_notifier_call_chain); · 64 unmodified lines
406407 * Adds a notifier to an SRCU notifier chain.
407408 * Must be called in process context.
408409 *
409− * Currently always returns zero.
410+ * Returns 0 on success, %-EEXIST on error.
410411 */
411412 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
412413 struct notifier_block *n)