All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>
To: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Memory barrier question.
Date: Fri, 29 Oct 2010 08:09:08 -0700	[thread overview]
Message-ID: <20101029150908.GC2367@linux.vnet.ibm.com> (raw)
In-Reply-To: <201010292223.IIF64065.tOLVFJFSQOMFOH@I-love.SAKURA.ne.jp>

On Fri, Oct 29, 2010 at 10:23:41PM +0900, Tetsuo Handa wrote:
> Hello.
> 
> I got a question regarding memory barrier.
> 
> sruct word {
> 	struct list_head list;
> 	char *buf;
> };
> 
> static LIST_HEAD(wordlist);
> static DEFINE_SPINLOCK(wordlist_lock);
> 
> --- On CPU 0 ---
> 
> struct word *hello = kzalloc(sizeof(*hello), GFP_KERNEL);
> hello->buf = kstrdup("hello", GFP_KERNEL);
> spin_lock(&wordlist_lock);
> list_add_rcu(&hello.list, &wordlist);
> spin_unlock(&wordlist_lock);
> 
> --- On CPU 1 ---
> 
> struct word *ptr;
> rcu_read_lock();
> list_for_each_entry_rcu(ptr, &wordlist, list) {
> 	char *str = ptr->buf;
> 	printk("%s\n", str);
> }
> rcu_read_unlock();
> 
> Use of rcu_assign_pointer() and rcu_dereference() guarantees that
> CPU 1 gets &hello->list by reading wordlist.next only after
> CPU 1 can get kstrdup()ed pointer by reading hello->buf.
> But what guarantees that CPU 1 gets "hello" by reading kstrdup()ed pointer?
> 
> Say, kstrdup("hello", GFP_KERNEL) stores
> 
>   'h' -> 0xC0000000
>   'e' -> 0xC0000001
>   'l' -> 0xC0000002
>   'l' -> 0xC0000003
>   'o' -> 0xC0000004
>   '\0' -> 0xC0000005
> 
> and hello->buf = kstrdup() stores
> 
>   0xC0000000 -> hello->buf
> 
> .
> 
> If ordered by smp_wmb() by CPU 0 and smp_rmb() by CPU 1,
> str = ptr->buf will load
> 
>   0xC0000000 -> str
> 
> and printk("%s\n", str) will load
> 
>   0xC0000000 -> 'h'
>   0xC0000001 -> 'e'
>   0xC0000002 -> 'l'
>   0xC0000003 -> 'l'
>   0xC0000004 -> 'o'
>   0xC0000005 -> '\0'
> 
> .
> 
> Since CPU 0 issued smp_wmb() (inside list_add_rcu()) but CPU 1 did not issue
> smp_rmb() (inside list_for_each_entry_rcu()), I think CPU 1 would see bogus
> values like
> 
>   0xC0000000 -> 'h'
>   0xC0000001 -> 'a'
>   0xC0000002 -> 'l'
>   0xC0000003 -> '1'
>   0xC0000004 -> 'o'
>   0xC0000005 -> 'w'
>   0xC0000006 -> 'e'
>   0xC0000007 -> 'e'
>   0xC0000008 -> 'n'
>   0xC0000009 -> '\0'

Timely string value, given this coming Sunday in USA.  ;-)

> .
> 
> It seems to me that people do not call smp_rmb() before reading memory
> which was dynamically allocated/initialized. What am I missing?

There is the compiler and the CPU.  The compiler is prohibited from
reordering the reads due to the ACCESS_ONCE().

For the CPU, let's take it by type of architecture...

First, let's get the UP-only architectures out of the way.  These would
always see their changes in order, so woiuld always see "hello".

Second, let's consider the TSO architectures, including x86, SPARC,
PA-RISC, and IBM Mainframe.  On these architectures, reads are not
reordered by the CPU, so if they see the new pointer, they will also
see the new characters -- hence "hello".

Next, let's consider weakly ordered systems that respect dependency
ordering (ARM, PowerPC, Itanium).  The load of the pointer would
always be ordered with respect to any dereference of the pointer,
so they would always see "hello".

This leave DEC Alpha.  In this architecture, smp_read_barrier_depends()
expands to smp_rmb(), which forces the ordering as required.  So
Alpha also sees "hello."

I believe that this covers all of the cases.

Am I missing anything?

							Thanx, Paul

  reply	other threads:[~2010-10-29 15:09 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-29 13:23 Memory barrier question Tetsuo Handa
2010-10-29 15:09 ` Paul E. McKenney [this message]
2010-10-30  5:48   ` Tetsuo Handa
2010-10-30  6:11     ` Eric Dumazet
2010-10-30 12:40       ` Tetsuo Handa
  -- strict thread matches above, loose matches on Subject: below --
2010-09-20 10:34 memory " George Spelvin
2010-09-15 14:36 Miklos Szeredi
2010-09-15 19:12 ` Rafael J. Wysocki
2010-09-16 11:55 ` David Howells
2010-09-16 13:42   ` Miklos Szeredi
2010-09-16 13:42     ` Miklos Szeredi
2010-09-16 13:42     ` Miklos Szeredi
2010-09-16 14:30     ` David Howells
2010-09-16 15:03       ` Paul E. McKenney
2010-09-16 16:06         ` Miklos Szeredi
2010-09-16 16:37           ` Paul E. McKenney
2010-09-16 16:56             ` Miklos Szeredi
2010-09-16 17:09               ` James Bottomley
2010-09-16 17:17                 ` Miklos Szeredi
2010-09-16 17:40                   ` James Bottomley
2010-09-17 21:49                   ` Benjamin Herrenschmidt
2010-09-17 23:12                     ` Paul E. McKenney
2010-09-19  2:47                       ` Benjamin Herrenschmidt
2010-09-19 15:26                         ` Paul E. McKenney
2010-09-19 20:15                           ` Miklos Szeredi
2010-09-19 21:59                             ` Paul E. McKenney
2010-09-20  0:58                               ` James Bottomley
2010-09-20  1:29                                 ` Paul E. McKenney
2010-09-20 16:01                                   ` Miklos Szeredi
2010-09-20 18:25                                     ` Paul E. McKenney
2010-09-20 18:25                                       ` Paul E. McKenney
2010-09-20 18:57                                       ` Paul E. McKenney
2010-09-20 20:26                                       ` Michael Cree
2010-09-20 20:40                                         ` Paul E. McKenney
2010-09-21 14:59                                           ` Paul E. McKenney
2010-09-22 18:41                                             ` Paul E. McKenney
2010-09-18  1:12                     ` Alan Cox
2010-09-16 16:50           ` Jamie Lokier
2010-09-16 16:18   ` Peter Zijlstra
2010-09-16 16:18     ` Peter Zijlstra
2010-09-16 17:59     ` David Howells

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=20101029150908.GC2367@linux.vnet.ibm.com \
    --to=paulmck@linux.vnet.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.