From: Nick Piggin <nickpiggin@yahoo.com.au>
To: Akinobu Mita <akinobu.mita@gmail.com>
Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org
Subject: Re: [PATCH 1/5] lib: introduce call_once()
Date: Tue, 11 Mar 2008 23:41:37 +1100 [thread overview]
Message-ID: <200803112341.38005.nickpiggin@yahoo.com.au> (raw)
In-Reply-To: <20080310145704.GA6396@APFDCB5C>
On Tuesday 11 March 2008 01:57, Akinobu Mita wrote:
> +static inline int call_once(struct once_control *once_control,
> + int (*init_rouine)(void))
> +{
> + return likely(once_control->done) ? 0
> + : call_once_slow(once_control, init_rouine);
> +}
> +
> +#endif /* __LINUX_ONCE_H */
> Index: 2.6-rc/lib/once.c
> ===================================================================
> --- /dev/null
> +++ 2.6-rc/lib/once.c
> @@ -0,0 +1,18 @@
> +#include <linux/module.h>
> +#include <linux/once.h>
> +
> +int call_once_slow(struct once_control *once_control, int
> (*init_rouine)(void)) +{
> + int err = 0;
> +
> + mutex_lock(&once_control->lock);
> + if (!once_control->done) {
> + err = init_rouine();
> + if (!err)
> + once_control->done = 1;
> + }
> + mutex_unlock(&once_control->lock);
> +
> + return err;
> +}
> +EXPORT_SYMBOL_GPL(call_once_slow);
The store "once_control->done = 1" can become visible before
init_routine has finished. The code after calling call_once may
also speculatively load some memory before the load of
once_control->done completes, so you can likewise have a data
race that way too.
To fix this, you need smp_wmb after init_rouine(), and probably
smp_mb() in the fastpath after the check but before returning.
Basically any time you have this situation where you're touching
a shared variable without using locks, then you're vastly
increasing the complexity of the code, and so you must have a
good reason for it.
So acquiring the mutex unconditionally would be the best way to
go, unless you're calling this a lot in fastpaths (in which case
I would say you should probably rework your code)
Thanks,
Nick
prev parent reply other threads:[~2008-03-11 12:42 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-03-10 14:57 [PATCH 1/5] lib: introduce call_once() Akinobu Mita
2008-03-10 15:00 ` [PATCH 2/5] idr: use call_once() Akinobu Mita
2008-03-10 15:01 ` [PATCH 3/5] hugetlbfs: " Akinobu Mita
2008-03-10 15:03 ` [PATCH 4/5] shmem: " Akinobu Mita
2008-03-10 15:05 ` [PATCH 5/5] tiny-shmem: " Akinobu Mita
2008-03-10 22:15 ` [PATCH 4/5] shmem: " Hugh Dickins
2008-03-11 12:29 ` Akinobu Mita
2008-03-11 13:41 ` Hugh Dickins
2008-03-10 15:29 ` [PATCH 1/5] lib: introduce call_once() Joe Perches
2008-03-11 12:17 ` Akinobu Mita
2008-03-11 3:48 ` Andrew Morton
2008-03-11 4:10 ` Nick Piggin
2008-03-11 4:21 ` Andrew Morton
2008-03-11 12:27 ` Akinobu Mita
2008-03-11 17:35 ` Andrew Morton
2008-03-11 18:56 ` Joe Perches
2008-03-11 19:11 ` Andrew Morton
2008-03-15 4:01 ` Akinobu Mita
2008-03-11 12:41 ` Nick Piggin [this message]
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=200803112341.38005.nickpiggin@yahoo.com.au \
--to=nickpiggin@yahoo.com.au \
--cc=akinobu.mita@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/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