From: Chandra Seetharaman <sekharan@us.ibm.com>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Kernel development list <linux-kernel@vger.kernel.org>
Subject: Re: Notifier chains are unsafe
Date: Tue, 01 Nov 2005 12:20:34 -0800 [thread overview]
Message-ID: <1130876434.3586.378.camel@linuxchandra> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0511011010350.5081-100000@iolanthe.rowland.org>
On Tue, 2005-11-01 at 10:24 -0500, Alan Stern wrote:
> On Mon, 31 Oct 2005, Chandra Seetharaman wrote:
>
> > > #define notifier_block_enable(b) set_wmb((b)->enabled, 1)
> > > #define notifier_block_disable(b) set_wmb((b)->enabled, 0)
> >
> > I am not getting the complete picture. So, in unregister we would just
> > disable and never delete the notifier_block ? Or
> > notifier_block_enable/disable will be used by external entities
> > directly ?
>
> Register and unregister will continue to work as before, requiring a
> process context and the ability to sleep. notifier_block_enable/disable
> should be used when:
>
> a callout wants to disable itself as it is running, or
>
> someone running in an atomic context wants to enable or disable
> a callout.
>
> In the first case, unregister can't be used because it would hang. In the
> second case, register/unregister can't be used because they need to be
> able to sleep.
>
> In both cases the notifier block would have to be registered beforehand
> and unregistered later.
I understand. Thanks for the explanation. I like the option below better
(no new interface).
>
>
> > > It occurred to me that there _is_ a way to do unregister for atomic chains
> > > without blocking. Add to struct notifier_head
> > >
> > > atomic_t num_callers;
> > >
> > > Then in notifier_call_chain, do atomic_inc(&nh->num_callers) at the start
> > > and atomic_dec(&nh->num_callers) at the end. Finally, make unregister do
> > > this:
> > >
> > > int notifier_chain_unregister(struct notifier_head *nh,
> > > struct notifier_block *n)
> > > {
> > > if (nh->type == ATOMIC_NOTIFIER) {
> > > spin_lock(nh->lock);
> > > list_del(&n->node);
> > > smp_mb();
> > > while (atomic_read(&nh->num_callers) > 0)
> > > cpu_relax();
> > > spin_unlock(nh->lock);
> > > } else {
> > > ...
> > > }
> > > return 0;
> > > }
> >
> > But, how is the list protected in call_chain (will you be holding the
> > lock in call_chain() while incrementing the atomic variable).
>
> No; the list _won't_ be protected in call_chain. It will be possible to
> unregister a callout while the chain is in use. That's how the RCU
> approach works -- it uses no read locks, only write locks.
but, list_del poisons the next pointer which is not good for a reader
that is walking through the list, we have to use list_del_rcu instead.
Also, do you think we have to use _rcu versions of list traversal
functions in call_chain ?
>
> Deleting an entry while the list is in use is safe, because readers will
> encounter either the old or the new value of the .next pointer, and either
> one will be valid. The important thing is to make sure that no one will
> ever encounter the old pointer after unregister returns; that's what the
> "while" loop is for.
>
> Alan Stern
>
>
--
----------------------------------------------------------------------
Chandra Seetharaman | Be careful what you choose....
- sekharan@us.ibm.com | .......you may get it.
----------------------------------------------------------------------
next prev parent reply other threads:[~2005-11-01 20:20 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-10-24 20:48 Notifier chains are unsafe Alan Stern
2005-10-25 16:59 ` Joe Seigh
2005-10-25 23:30 ` Chandra Seetharaman
2005-10-26 18:46 ` Alan Stern
2005-10-26 19:05 ` Andreas Kleen
2005-10-26 20:40 ` Alan Stern
2005-10-26 21:44 ` Andi Kleen
2005-10-26 23:20 ` Chandra Seetharaman
2005-10-27 1:17 ` Joe Seigh
2005-10-28 1:36 ` Chandra Seetharaman
2005-10-27 14:13 ` Alan Stern
2005-10-26 22:40 ` Chandra Seetharaman
2005-10-27 15:28 ` Alan Stern
2005-10-27 20:43 ` Chandra Seetharaman
2005-10-27 21:21 ` Alan Stern
2005-10-27 23:02 ` Chandra Seetharaman
2005-10-28 0:48 ` Keith Owens
2005-10-28 1:34 ` Chandra Seetharaman
2005-10-28 14:23 ` Alan Stern
2005-10-28 22:15 ` Chandra Seetharaman
2005-10-29 14:51 ` Alan Stern
2005-10-31 22:22 ` Chandra Seetharaman
2005-11-01 15:24 ` Alan Stern
2005-11-01 20:20 ` Chandra Seetharaman [this message]
2005-11-01 21:20 ` Alan Stern
2005-11-02 9:50 ` Keith Owens
2005-11-02 16:03 ` Alan Stern
[not found] ` <mailman.1130460600.30060.linux-kernel2news@redhat.com>
2005-10-28 4:35 ` Pete Zaitcev
2005-10-25 23:43 ` Andi Kleen
2005-10-26 0:01 ` Chandra Seetharaman
2005-10-26 17:11 ` Andreas Kleen
2005-10-27 2:46 ` Herbert Xu
2005-10-29 12:25 ` Joe Seigh
2005-10-26 6:11 ` Keith Owens
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=1130876434.3586.378.camel@linuxchandra \
--to=sekharan@us.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox