Skip to contentSkip to conversation

notifier: Return non-null when callback already registered

RFC PATCH In review Borislav Petkov 2 participants 3 messages May 20, 2021 – May 21, 2021

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 notifier: Return non-null when callback already registered

From: Borislav Petkov <[email protected]>

The notifier registration routine doesn't return a proper error value when a callback has already been registered, leading people to track whether that regisration has happened at the call site:

https://lore.kernel.org/amd-gfx/[email protected]/

Which is unnecessary.

Return a non-null to signal that case so that callers can act accordingly.

---
 kernel/notifier.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/notifier.c +5 -5
index 1b019cbca594..ff7a3198c5fc 100644
@@ -25,7 +25,7 @@ static int notifier_chain_register(struct notifier_block **nl,
 	while ((*nl) != NULL) {
 		if (unlikely((*nl) == n)) {
 			WARN(1, "double register detected");
-			return 0;
+			return 1;
 		}
 		if (n->priority > (*nl)->priority)
 			break;
@@ -134,7 +134,7 @@ static int notifier_call_chain_robust(struct notifier_block **nl,
  *
  *	Adds a notifier to an atomic notifier chain.
  *
- *	Currently always returns zero.
+ *	Returns 0 on success, !0 on error.
  */
 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
 		struct notifier_block *n)
@@ -235,7 +235,7 @@ NOKPROBE_SYMBOL(atomic_notifier_call_chain);
  *	Adds a notifier to a blocking notifier chain.
  *	Must be called in process context.
  *
- *	Currently always returns zero.
+ *	Returns 0 on success, !0 on error.
  */
 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
 		struct notifier_block *n)
@@ -354,7 +354,7 @@ EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
  *	Adds a notifier to a raw notifier chain.
  *	All locking must be provided by the caller.
  *
- *	Currently always returns zero.
+ *	Returns 0 on success, !0 on error.
  */
 int raw_notifier_chain_register(struct raw_notifier_head *nh,
 		struct notifier_block *n)
@@ -425,7 +425,7 @@ EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
  *	Adds a notifier to an SRCU notifier chain.
  *	Must be called in process context.
  *
- *	Currently always returns zero.
+ *	Returns 0 on success, !0 on error.
  */
 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
 		struct notifier_block *n)
Signature · 1 line
2.29.2
#1 lore raw
Quoted 23 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

The notifier registration routine doesn't return a proper error value when a callback has already been registered, leading people to track whether that regisration has happened at the call site:

https://lore.kernel.org/amd-gfx/[email protected]/

Which is unnecessary.

Return a non-null to signal that case so that callers can act accordingly.

---
 kernel/notifier.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/notifier.c +0 -0
index 1b019cbca594..ff7a3198c5fc 100644

kernel/notifier.c static int notifier_chain_register(struct notifier_block **nl, · line 27

Quoted 3 lines
kernel/notifier.c +0 -0
@@ -25,7 +25,7 @@ static int notifier_chain_register(struct notifier_block **nl,
 	while ((*nl) != NULL) {
 		if (unlikely((*nl) == n)) {
 			WARN(1, "double register detected");

That should give a big clue^

kernel/notifier.c static int notifier_chain_register(struct notifier_block **nl, · line 28

Quoted 2 lines
kernel/notifier.c +1 -1
-			return 0;
+			return 1;

How about -EBUSY here?

#2 lore raw
Quoted 6 lines · Peter Zijlstra

kernel/notifier.c static int notifier_chain_register(struct notifier_block **nl, · line 27

Quoted 4 lines
kernel/notifier.c +0 -0
@@ -25,7 +25,7 @@ static int notifier_chain_register(struct notifier_block **nl,
 	while ((*nl) != NULL) {
 		if (unlikely((*nl) == n)) {
 			WARN(1, "double register detected");

That should give a big clue^

Yah, the big cluebat... Was wondering whether to remove it, actually. But then I'd need to audit all users whether they handle retval properly. Sounds like a kernelnewbies project to me...

Quoted 4 lines

kernel/notifier.c static int notifier_chain_register(struct notifier_block **nl, · line 28

Quoted 2 lines
kernel/notifier.c +1 -1
-			return 0;
+			return 1;

How about -EBUSY here?

Yah, anthing but 0.

But if we're gonna return -E things, -EEXIST looks like an even better fit to me...

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette