Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9AF8BC433EF for ; Thu, 2 Dec 2021 13:36:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358267AbhLBNja (ORCPT ); Thu, 2 Dec 2021 08:39:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346862AbhLBNj0 (ORCPT ); Thu, 2 Dec 2021 08:39:26 -0500 Received: from mail.skyhub.de (mail.skyhub.de [IPv6:2a01:4f8:190:11c2::b:1457]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 512D5C06174A for ; Thu, 2 Dec 2021 05:36:03 -0800 (PST) Received: from zn.tnic (dslb-088-067-202-008.088.067.pools.vodafone-ip.de [88.67.202.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 9E8331EC0513; Thu, 2 Dec 2021 14:36:01 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1638452161; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=d7fPlpCgB7ns6gUEmSc73zBVYQELTFDKRgVdipaNLjg=; b=bub6wn2766sWZhIma411bgJXGycOb/O89Az6VUMWH8ThPf+HClV69r16MsJc6DAOirSbTx xZczR8TqA5oIAJz5/1qGiK94wblXPUDCPFxyGVlPfH7hmr79uOQI9pre8P9iq7/nikOzqG Mos0tQzxTLzBusLmyzVwb54E6xuapn8= From: Borislav Petkov To: X86 ML Cc: Valentin Schneider , "Rafael J. Wysocki" , Sebastian Andrzej Siewior , Geert Uytterhoeven , Alan Stern , Steven Rostedt , LKML Subject: [PATCH] notifier: Return an error when a callback has already been registered Date: Thu, 2 Dec 2021 14:36:01 +0100 Message-Id: <20211202133601.23527-1-bp@alien8.de> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Borislav Petkov Add another indirection to the notifiers callback registration path which does only the error checking and propagates the proper error value to the callers instead of returning only 0. 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. Signed-off-by: Borislav Petkov --- kernel/notifier.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/kernel/notifier.c b/kernel/notifier.c index b8251dc0bc0f..0820a156ce83 100644 --- a/kernel/notifier.c +++ b/kernel/notifier.c @@ -19,14 +19,12 @@ BLOCKING_NOTIFIER_HEAD(reboot_notifier_list); * are layered on top of these, with appropriate locking added. */ -static int notifier_chain_register(struct notifier_block **nl, - struct notifier_block *n) +static int __notifier_chain_register(struct notifier_block **nl, + 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); @@ -36,6 +34,18 @@ static int notifier_chain_register(struct notifier_block **nl, return 0; } +static int notifier_chain_register(struct notifier_block **nl, + struct notifier_block *n) +{ + int ret = __notifier_chain_register(nl, n); + + if (ret == -EEXIST) + WARN(1, "notifier callback %ps already registered", + n->notifier_call); + + return ret; +} + static int notifier_chain_unregister(struct notifier_block **nl, struct notifier_block *n) { @@ -134,7 +144,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, %-EEXIST on error. */ int atomic_notifier_chain_register(struct atomic_notifier_head *nh, struct notifier_block *n) @@ -216,7 +226,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, %-EEXIST on error. */ int blocking_notifier_chain_register(struct blocking_notifier_head *nh, struct notifier_block *n) @@ -335,7 +345,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, %-EEXIST on error. */ int raw_notifier_chain_register(struct raw_notifier_head *nh, struct notifier_block *n) @@ -406,7 +416,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, %-EEXIST on error. */ int srcu_notifier_chain_register(struct srcu_notifier_head *nh, struct notifier_block *n) -- 2.29.2