From mboxrd@z Thu Jan 1 00:00:00 1970 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 A8283C433FE for ; Fri, 10 Dec 2021 18:20:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245213AbhLJSXn (ORCPT ); Fri, 10 Dec 2021 13:23:43 -0500 Received: from mail-oo1-f43.google.com ([209.85.161.43]:44644 "EHLO mail-oo1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234227AbhLJSXj (ORCPT ); Fri, 10 Dec 2021 13:23:39 -0500 Received: by mail-oo1-f43.google.com with SMTP id t9-20020a4a8589000000b002c5c4d19723so2583009ooh.11; Fri, 10 Dec 2021 10:20:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=otNvdkTkoV7wYQ8jKu5rR7dHo6vQCE5BmfrHPdMYTOI=; b=ikdHVOMVbhQOfHWeQxWJE8ZSIQAvJnnCR1/sJvqQpXXyxB/UPTHe4qe2pNZOTL0072 /u5zBjqY6cUaHJAy7RidUEd5FTfbFt0ilFstzWvh1b4YVX4UEdaantBmcv7a6R6JIA6W jSv4hhUBkiYpfL/fODkn0JwaUoxNfci2MLBWGTwiBOW4z1+eWaAo69RegvnVbgmq9ELC WfQll7AqjifJMXihmJWbdbVhoR8jBjbCKKojYaDOfFE6GT9tCv9dFvludS0bcAfjVC5b dcUASeGFpWPI7oNDAv6obx2uIqZaXUGJQtL6ycKlFTCecTeXUd+JvHmKsgX7EglYWBDO LtGg== X-Gm-Message-State: AOAM532lhh+A2EyQBrzedcp4ZNfW/aZFBONPRbIvA6+B1v/uchAVFdkG L96pxGSplJn6MNpfTO6I2ghfccSG4IOXuKUIfY0= X-Google-Smtp-Source: ABdhPJwWhlgSTT1XO9DdsJeGtp3+qj/KLI9CFrQ1iXrm/9qKavB6UGr0uIjkNl6uj3XKEKq2VM+aZTVW+dl+zG+2Q+M= X-Received: by 2002:a4a:1d82:: with SMTP id 124mr9074671oog.91.1639160403307; Fri, 10 Dec 2021 10:20:03 -0800 (PST) MIME-Version: 1.0 References: <20211126180101.27818-1-digetx@gmail.com> <20211126180101.27818-4-digetx@gmail.com> In-Reply-To: <20211126180101.27818-4-digetx@gmail.com> From: "Rafael J. Wysocki" Date: Fri, 10 Dec 2021 19:19:52 +0100 Message-ID: Subject: Re: [PATCH v4 03/25] notifier: Add atomic/blocking_notifier_has_unique_priority() To: Dmitry Osipenko Cc: Thierry Reding , Jonathan Hunter , Russell King , Catalin Marinas , Will Deacon , Guo Ren , Geert Uytterhoeven , Greg Ungerer , Joshua Thompson , Thomas Bogendoerfer , Sebastian Reichel , Linus Walleij , Philipp Zabel , Greentime Hu , Vincent Chen , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Benjamin Herrenschmidt , Paul Mackerras , Paul Walmsley , Palmer Dabbelt , Albert Ou , Yoshinori Sato , Rich Felker , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "the arch/x86 maintainers" , "H. Peter Anvin" , Boris Ostrovsky , Juergen Gross , Stefano Stabellini , "Rafael J. Wysocki" , Len Brown , Santosh Shilimkar , Krzysztof Kozlowski , Liam Girdwood , Mark Brown , Pavel Machek , Lee Jones , Andrew Morton , Guenter Roeck , Daniel Lezcano , Andy Shevchenko , Ulf Hansson , alankao@andestech.com, "K . C . Kuen-Chern Lin" , Linux ARM , Linux Kernel Mailing List , linux-csky@vger.kernel.org, linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev , linux-riscv@lists.infradead.org, Linux-sh list , xen-devel@lists.xenproject.org, ACPI Devel Maling List , Linux PM , linux-tegra Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-sh@vger.kernel.org On Fri, Nov 26, 2021 at 7:02 PM Dmitry Osipenko wrote: > > Add atomic/blocking_notifier_has_unique_priority() helpers which return > true if given handler has unique priority. > > Signed-off-by: Dmitry Osipenko > --- > include/linux/notifier.h | 5 +++ > kernel/notifier.c | 69 ++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 74 insertions(+) > > diff --git a/include/linux/notifier.h b/include/linux/notifier.h > index 924c9d7c8e73..2c4036f225e1 100644 > --- a/include/linux/notifier.h > +++ b/include/linux/notifier.h > @@ -175,6 +175,11 @@ int raw_notifier_call_chain_robust(struct raw_notifier_head *nh, > > bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh); > > +bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh, > + struct notifier_block *nb); > +bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh, > + struct notifier_block *nb); > + > #define NOTIFY_DONE 0x0000 /* Don't care */ > #define NOTIFY_OK 0x0001 /* Suits me */ > #define NOTIFY_STOP_MASK 0x8000 /* Don't call further */ > diff --git a/kernel/notifier.c b/kernel/notifier.c > index b20cb7b9b1f0..7a325b742104 100644 > --- a/kernel/notifier.c > +++ b/kernel/notifier.c > @@ -122,6 +122,19 @@ static int notifier_call_chain_robust(struct notifier_block **nl, > return ret; > } > > +static int notifier_has_unique_priority(struct notifier_block **nl, > + struct notifier_block *n) > +{ > + while (*nl && (*nl)->priority >= n->priority) { > + if ((*nl)->priority == n->priority && *nl != n) > + return false; > + > + nl = &((*nl)->next); > + } > + > + return true; > +} > + > /* > * Atomic notifier chain routines. Registration and unregistration > * use a spinlock, and call_chain is synchronized by RCU (no locks). > @@ -203,6 +216,30 @@ int atomic_notifier_call_chain(struct atomic_notifier_head *nh, > EXPORT_SYMBOL_GPL(atomic_notifier_call_chain); > NOKPROBE_SYMBOL(atomic_notifier_call_chain); > > +/** > + * atomic_notifier_has_unique_priority - Checks whether notifier's priority is unique > + * @nh: Pointer to head of the atomic notifier chain > + * @n: Entry in notifier chain to check > + * > + * Checks whether there is another notifier in the chain with the same priority. > + * Must be called in process context. > + * > + * Returns true if priority is unique, false otherwise. > + */ > +bool atomic_notifier_has_unique_priority(struct atomic_notifier_head *nh, > + struct notifier_block *n) > +{ > + unsigned long flags; > + bool ret; > + > + spin_lock_irqsave(&nh->lock, flags); > + ret = notifier_has_unique_priority(&nh->head, n); > + spin_unlock_irqrestore(&nh->lock, flags); This only works if the caller can prevent new entries from being added to the list at this point or if the caller knows that they cannot be added for some reason, but the kerneldoc doesn't mention this limitation. > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(atomic_notifier_has_unique_priority); > + > /* > * Blocking notifier chain routines. All access to the chain is > * synchronized by an rwsem. > @@ -336,6 +373,38 @@ bool blocking_notifier_call_chain_is_empty(struct blocking_notifier_head *nh) > } > EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_is_empty); > > +/** > + * blocking_notifier_has_unique_priority - Checks whether notifier's priority is unique > + * @nh: Pointer to head of the blocking notifier chain > + * @n: Entry in notifier chain to check > + * > + * Checks whether there is another notifier in the chain with the same priority. > + * Must be called in process context. > + * > + * Returns true if priority is unique, false otherwise. > + */ > +bool blocking_notifier_has_unique_priority(struct blocking_notifier_head *nh, > + struct notifier_block *n) > +{ > + bool ret; > + > + /* > + * This code gets used during boot-up, when task switching is > + * not yet working and interrupts must remain disabled. At such > + * times we must not call down_read(). > + */ > + if (system_state != SYSTEM_BOOTING) No, please don't do this, it makes the whole thing error-prone. > + down_read(&nh->rwsem); > + > + ret = notifier_has_unique_priority(&nh->head, n); > + > + if (system_state != SYSTEM_BOOTING) > + up_read(&nh->rwsem); And still what if a new entry with a non-unique priority is added to the chain at this point? > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(blocking_notifier_has_unique_priority); > + > /* > * Raw notifier chain routines. There is no protection; > * the caller must provide it. Use at your own risk! > -- > 2.33.1 >