LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Arnd Bergmann <arnd@arndb.de>,
	Ayush Sawal <ayush.sawal@chelsio.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Rohit Maheshwari <rohitm@chelsio.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Vinay Kumar Yadav <vinay.yadav@chelsio.com>,
	alsa-devel@alsa-project.org,
	bcm-kernel-feedback-list@broadcom.com,
	intel-gfx@lists.freedesktop.org,
	intel-gvt-dev@lists.freedesktop.org, linux-alpha@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org,
	linux-crypto@vger.kernel.org, linux-edac@vger.kernel.org,
	linux-fbdev@vger.kernel.org, linux-hyperv@vger.kernel.org,
	linux-iio@vger.kernel.org, linux-leds@vger.kernel.org,
	linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org,
	linux-pm@vger.kernel.org, linuxppc-dev@lists.ozlabs.org,
	linux-remoteproc@vger.kernel.org,
	linux-renesas-soc@vger.kernel.org, linux-s390@vger.kernel.org,
	linux-scsi@vger.kernel.org, linux-sh@vger.kernel.org,
	linux-staging@lists.linux.dev, linux-tegra@vger.kernel.org,
	linux-um@lists.infradead.org, linux-usb@vger.kernel.org,
	linux-xtensa@linux-xtensa.org, netdev@vger.kernel.org,
	openipmi-developer@lists.sourceforge.net, rcu@vger.kernel.org,
	sparclinux@vger.kernel.org, x86@kernel.org,
	xen-devel@lists.xenproject.org
Subject: [PATCH v0 42/42] notifier: Return an error when callback is already registered
Date: Mon,  8 Nov 2021 11:11:57 +0100	[thread overview]
Message-ID: <20211108101157.15189-43-bp@alien8.de> (raw)
In-Reply-To: <20211108101157.15189-1-bp@alien8.de>

From: Borislav Petkov <bp@suse.de>

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

  https://lore.kernel.org/amd-gfx/20210512013058.6827-1-mukul.joshi@amd.com/

Which is unnecessary.

Return -EEXIST to signal that case so that callers can act accordingly.
Enforce callers to check the return value, leading to loud screaming
during build:

  arch/x86/kernel/cpu/mce/core.c: In function ‘mce_register_decode_chain’:
  arch/x86/kernel/cpu/mce/core.c:167:2: error: ignoring return value of \
   ‘blocking_notifier_chain_register’, declared with attribute warn_unused_result [-Werror=unused-result]
    blocking_notifier_chain_register(&x86_mce_decoder_chain, nb);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Drop the WARN too, while at it.

Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Ayush Sawal <ayush.sawal@chelsio.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Rohit Maheshwari <rohitm@chelsio.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Vinay Kumar Yadav <vinay.yadav@chelsio.com>
Cc: alsa-devel@alsa-project.org
Cc: bcm-kernel-feedback-list@broadcom.com
Cc: intel-gfx@lists.freedesktop.org
Cc: intel-gvt-dev@lists.freedesktop.org
Cc: linux-alpha@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-clk@vger.kernel.org
Cc: linux-crypto@vger.kernel.org
Cc: linux-edac@vger.kernel.org
Cc: linux-fbdev@vger.kernel.org
Cc: linux-hyperv@vger.kernel.org
Cc: linux-iio@vger.kernel.org
Cc: linux-leds@vger.kernel.org
Cc: linux-mips@vger.kernel.org
Cc: linux-parisc@vger.kernel.org
Cc: linux-pm@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Cc: linux-remoteproc@vger.kernel.org
Cc: linux-renesas-soc@vger.kernel.org
Cc: linux-s390@vger.kernel.org
Cc: linux-scsi@vger.kernel.org
Cc: linux-sh@vger.kernel.org
Cc: linux-staging@lists.linux.dev
Cc: linux-tegra@vger.kernel.org
Cc: linux-um@lists.infradead.org
Cc: linux-usb@vger.kernel.org
Cc: linux-xtensa@linux-xtensa.org
Cc: netdev@vger.kernel.org
Cc: openipmi-developer@lists.sourceforge.net
Cc: rcu@vger.kernel.org
Cc: sparclinux@vger.kernel.org
Cc: x86@kernel.org
Cc: xen-devel@lists.xenproject.org
---
 include/linux/notifier.h |  8 ++++----
 kernel/notifier.c        | 36 +++++++++++++++++++-----------------
 2 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/include/linux/notifier.h b/include/linux/notifier.h
index 87069b8459af..45cc5a8d0fd8 100644
--- a/include/linux/notifier.h
+++ b/include/linux/notifier.h
@@ -141,13 +141,13 @@ extern void srcu_init_notifier_head(struct srcu_notifier_head *nh);
 
 #ifdef __KERNEL__
 
-extern int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
+extern int __must_check atomic_notifier_chain_register(struct atomic_notifier_head *nh,
 		struct notifier_block *nb);
-extern int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
+extern int __must_check blocking_notifier_chain_register(struct blocking_notifier_head *nh,
 		struct notifier_block *nb);
-extern int raw_notifier_chain_register(struct raw_notifier_head *nh,
+extern int __must_check raw_notifier_chain_register(struct raw_notifier_head *nh,
 		struct notifier_block *nb);
-extern int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
+extern int __must_check srcu_notifier_chain_register(struct srcu_notifier_head *nh,
 		struct notifier_block *nb);
 
 extern int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
diff --git a/kernel/notifier.c b/kernel/notifier.c
index b8251dc0bc0f..451ef3f73ad2 100644
--- a/kernel/notifier.c
+++ b/kernel/notifier.c
@@ -20,13 +20,11 @@ BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
  */
 
 static int notifier_chain_register(struct notifier_block **nl,
-		struct notifier_block *n)
+				   struct notifier_block *n)
 {
 	while ((*nl) != NULL) {
-		if (unlikely((*nl) == n)) {
-			WARN(1, "double register detected");
-			return 0;
-		}
+		if (unlikely((*nl) == n))
+			return -EEXIST;
 		if (n->priority > (*nl)->priority)
 			break;
 		nl = &((*nl)->next);
@@ -134,10 +132,11 @@ 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, %-EEXIST on error.
  */
-int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
-		struct notifier_block *n)
+int __must_check
+atomic_notifier_chain_register(struct atomic_notifier_head *nh,
+			       struct notifier_block *n)
 {
 	unsigned long flags;
 	int ret;
@@ -216,10 +215,11 @@ 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, %-EEXIST on error.
  */
-int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
-		struct notifier_block *n)
+int __must_check
+blocking_notifier_chain_register(struct blocking_notifier_head *nh,
+				 struct notifier_block *n)
 {
 	int ret;
 
@@ -335,10 +335,11 @@ 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, %-EEXIST on error.
  */
-int raw_notifier_chain_register(struct raw_notifier_head *nh,
-		struct notifier_block *n)
+int __must_check
+raw_notifier_chain_register(struct raw_notifier_head *nh,
+			    struct notifier_block *n)
 {
 	return notifier_chain_register(&nh->head, n);
 }
@@ -406,10 +407,11 @@ 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, %-EEXIST on error.
  */
-int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
-		struct notifier_block *n)
+int __must_check
+srcu_notifier_chain_register(struct srcu_notifier_head *nh,
+			     struct notifier_block *n)
 {
 	int ret;
 
-- 
2.29.2


  parent reply	other threads:[~2021-11-08 10:16 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-08 10:11 [PATCH v0 00/42] notifiers: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 01/42] x86: Check notifier registration return value Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 02/42] xen/x86: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 03/42] impi: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 04/42] clk: renesas: " Borislav Petkov
2021-11-08 14:12   ` Geert Uytterhoeven
2021-11-08 10:11 ` [PATCH v0 05/42] dca: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 06/42] firmware: " Borislav Petkov
2021-11-12 18:43   ` Cristian Marussi
2021-11-08 10:11 ` [PATCH v0 07/42] drm/i915: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 08/42] Drivers: hv: vmbus: " Borislav Petkov
2021-11-08 11:16   ` Wei Liu
2021-11-08 11:39     ` Borislav Petkov
2021-11-08 11:45       ` Wei Liu
2021-11-08 12:08         ` Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 09/42] iio: proximity: cros_ec: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 10/42] leds: trigger: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 11/42] misc: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 12/42] ethernet: chelsio: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 13/42] power: reset: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 14/42] remoteproc: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 15/42] scsi: target: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 16/42] USB: " Borislav Petkov
2021-11-08 14:05   ` Alan Stern
2021-11-08 14:09     ` Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 17/42] drivers: video: " Borislav Petkov
2021-11-08 11:16   ` Wei Liu
2021-11-08 10:11 ` [PATCH v0 18/42] drivers/xen: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 19/42] kernel/hung_task: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 20/42] rcu: " Borislav Petkov
2021-11-08 16:53   ` Paul E. McKenney
2021-11-08 10:11 ` [PATCH v0 21/42] tracing: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 22/42] net: fib_notifier: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 23/42] ASoC: soc-jack: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 24/42] staging: olpc_dcon: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 25/42] arch/um: " Borislav Petkov
2021-11-08 10:23   ` Johannes Berg
2021-11-08 10:48     ` Borislav Petkov
2021-11-08 10:56       ` Johannes Berg
2021-11-08 10:11 ` [PATCH v0 26/42] alpha: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 27/42] bus: brcmstb_gisb: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 28/42] soc: bcm: brcmstb: pm: pm-arm: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 29/42] arm64: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 30/42] soc/tegra: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 31/42] parisc: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 32/42] macintosh/adb: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 33/42] mips: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 34/42] powerpc: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 35/42] sh: " Borislav Petkov
2021-11-08 13:31   ` Geert Uytterhoeven
2021-11-08 13:49     ` Borislav Petkov
2021-11-08 14:03       ` Geert Uytterhoeven
2021-11-08 14:48   ` Sergey Shtylyov
2021-11-08 10:11 ` [PATCH v0 36/42] s390: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 37/42] sparc: " Borislav Petkov
2021-11-08 19:59   ` David Miller
2021-11-08 10:11 ` [PATCH v0 38/42] xtensa: " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 39/42] crypto: ccree - check " Borislav Petkov
2021-11-12 18:32   ` Cristian Marussi
2021-11-12 18:48     ` Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 40/42] EDAC/altera: Check " Borislav Petkov
2021-11-08 10:11 ` [PATCH v0 41/42] power: supply: ab8500: " Borislav Petkov
2021-11-08 10:11 ` Borislav Petkov [this message]
2021-11-08 14:07   ` [PATCH v0 42/42] notifier: Return an error when callback is already registered Geert Uytterhoeven
2021-11-08 14:21     ` Borislav Petkov
2021-11-08 15:25       ` Geert Uytterhoeven
2021-11-08 15:58         ` Borislav Petkov
2021-11-08 16:12           ` Geert Uytterhoeven
2021-11-08 16:21             ` Borislav Petkov
2021-11-08 20:59               ` Alan Stern
2021-11-08 21:18                 ` Borislav Petkov
2021-11-08 10:19 ` [PATCH v0 00/42] notifiers: " Borislav Petkov
2021-11-08 14:17   ` Alan Stern
2021-11-08 14:24     ` Borislav Petkov
2021-11-08 14:35       ` Borislav Petkov
2021-11-08 16:23         ` Steven Rostedt
2021-11-08 16:29           ` Borislav Petkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211108101157.15189-43-bp@alien8.de \
    --to=bp@alien8.de \
    --cc=alsa-devel@alsa-project.org \
    --cc=arnd@arndb.de \
    --cc=ayush.sawal@chelsio.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-gvt-dev@lists.freedesktop.org \
    --cc=linux-alpha@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-parisc@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=linux-remoteproc@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=linux-sh@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=linux-tegra@vger.kernel.org \
    --cc=linux-um@lists.infradead.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=linux-xtensa@linux-xtensa.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=netdev@vger.kernel.org \
    --cc=openipmi-developer@lists.sourceforge.net \
    --cc=rcu@vger.kernel.org \
    --cc=rohitm@chelsio.com \
    --cc=rostedt@goodmis.org \
    --cc=sparclinux@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=vinay.yadav@chelsio.com \
    --cc=x86@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    --subject='Re: [PATCH v0 42/42] notifier: Return an error when callback is already registered' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).