From: Alan Huang <mmpgouride@gmail.com>
To: paulmck@kernel.org
Cc: Joel Fernandes <joel@joelfernandes.org>,
corbet@lwn.net, rcu@vger.kernel.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH v2] docs/RCU: Bring smp_wmb() back
Date: Wed, 21 Jun 2023 09:24:30 +0800 [thread overview]
Message-ID: <BC255E5C-5F0A-42DF-B16A-54731ED7426C@gmail.com> (raw)
In-Reply-To: <002d991c-4064-4219-a4a1-ef98bba88a65@paulmck-laptop>
> 2023年6月21日 06:33,Paul E. McKenney <paulmck@kernel.org> 写道:
>
> On Sat, Jun 17, 2023 at 02:53:46PM +0000, Alan Huang wrote:
>> There are two memory ordering required in the insertion algorithm,
>> we need to make sure obj->key is updated before obj->obj_node.next
>> and obj->refcnt, atomic_set_release is not enough to provide the
>> required memory barrier.
>>
>> Signed-off-by: Alan Huang <mmpgouride@gmail.com>
>> ---
>> Documentation/RCU/rculist_nulls.rst | 9 +++++++--
>> 1 file changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/Documentation/RCU/rculist_nulls.rst b/Documentation/RCU/rculist_nulls.rst
>> index e06ed40bb6..77244adbdf 100644
>> --- a/Documentation/RCU/rculist_nulls.rst
>> +++ b/Documentation/RCU/rculist_nulls.rst
>> @@ -98,7 +98,7 @@ Quoting Corey Minyard::
>> ----------------------
>>
>> We need to make sure a reader cannot read the new 'obj->obj_node.next' value
>> -and previous value of 'obj->key'. Otherwise, an item could be deleted
>> +and previous value of 'obj->key' at the same time. Otherwise, an item could be deleted
>> from a chain, and inserted into another chain. If new chain was empty
>> before the move, 'next' pointer is NULL, and lockless reader can not
>> detect the fact that it missed following items in original chain.
>> @@ -112,7 +112,12 @@ detect the fact that it missed following items in original chain.
>> obj = kmem_cache_alloc(...);
>> lock_chain(); // typically a spin_lock()
>> obj->key = key;
>> - atomic_set_release(&obj->refcnt, 1); // key before refcnt
>> + /*
>> + * we need to make sure obj->key is updated before obj->obj_node.next
>> + * and obj->refcnt
>> + */
>> + smp_wmb();
>> + atomic_set(&obj->refcnt, 1);
>
> You lost me on this one.
>
> From what I can see, in the original the atomic_set_release() provided
> ordering between the ->key assignment and the ->refcnt assignment,
> and then the rcu_assign_pointer() within hlist_add_head_rcu() provided
> ordering between both the ->key and the ->refcnt assignments on the one
> hand and the list insertion on the other.
>
> What am I missing here?
The objects are allocated with SLAB_TYPESAFE_BY_RCU, and there is
n->next = first within hlist_add_head_rcu() before rcu_assign_pointer(), which
modifies obj->obj_node.next. There may be readers holding the reference of obj in
lockless_lookup, and when updater modifies ->next, readers can see the change immediately
because of SLAB_TYPESAFE_BY_RCU.
So, the line n->next = first is running concurrently with the line pos->next in lockless_lookup.
As you can see, after pos->next in lockless_lookup is the smp_rmb(),
we need to make sure obj->key is updated before obj->obj_node.next (which is n->next = first in hlist_add_head_rcu).
So, the smp_wmb() is synchronized with the smp_rmb() in lockless_lookup() and the load acquire in try_get_ref().
>
> Thanx, Paul
>
>> hlist_add_head_rcu(&obj->obj_node, list);
>> unlock_chain(); // typically a spin_unlock()
>>
>> --
>> 2.34.1
>>
prev parent reply other threads:[~2023-06-21 1:25 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-17 14:53 [PATCH v2] docs/RCU: Bring smp_wmb() back Alan Huang
2023-06-17 17:23 ` Matthew Wilcox
2023-06-17 17:55 ` Alan Huang
2023-06-17 18:07 ` Matthew Wilcox
2023-06-17 18:21 ` Alan Huang
2023-06-20 22:33 ` Paul E. McKenney
2023-06-21 1:24 ` Alan Huang [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=BC255E5C-5F0A-42DF-B16A-54731ED7426C@gmail.com \
--to=mmpgouride@gmail.com \
--cc=corbet@lwn.net \
--cc=joel@joelfernandes.org \
--cc=linux-doc@vger.kernel.org \
--cc=paulmck@kernel.org \
--cc=rcu@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