linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC] hlist_add_tail_rcu disable sparse warning
@ 2016-11-23 20:48 Michael S. Tsirkin
  2016-11-26  0:52 ` David Miller
  2016-12-05 19:37 ` Michael S. Tsirkin
  0 siblings, 2 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2016-11-23 20:48 UTC (permalink / raw)
  To: David Miller, Paul E. McKenney, Arnd Bergmann; +Cc: linux-kernel

sparse is unhappy about this code in hlist_add_tail_rcu:

        struct hlist_node *i, *last = NULL;

        for (i = hlist_first_rcu(h); i; i = hlist_next_rcu(i))
                last = i;

This is because hlist_next_rcu and hlist_next_rcu return
__rcu pointers.

The following trivial patch disables the warning
without changing the behaviour in any way.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

---

I would appreciate review to confirm the function doesn't
do anything unsafe though.

In particular, should this use __hlist_for_each_rcu instead?
I note that __hlist_for_each_rcu does rcu_dereference
internally, which is missing here.

compile-tested only.

diff --git a/include/linux/rculist.h b/include/linux/rculist.h
index 8beb98d..33574db 100644
--- a/include/linux/rculist.h
+++ b/include/linux/rculist.h
@@ -511,7 +511,7 @@ static inline void hlist_add_tail_rcu(struct hlist_node *n,
 {
 	struct hlist_node *i, *last = NULL;
 
-	for (i = hlist_first_rcu(h); i; i = hlist_next_rcu(i))
+	for (i = h->first; i; i = i->next)
 		last = i;
 
 	if (last) {

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH RFC] hlist_add_tail_rcu disable sparse warning
  2016-11-23 20:48 [PATCH RFC] hlist_add_tail_rcu disable sparse warning Michael S. Tsirkin
@ 2016-11-26  0:52 ` David Miller
  2016-11-28 14:39   ` Paul E. McKenney
  2016-12-05  3:29   ` Michael S. Tsirkin
  2016-12-05 19:37 ` Michael S. Tsirkin
  1 sibling, 2 replies; 7+ messages in thread
From: David Miller @ 2016-11-26  0:52 UTC (permalink / raw)
  To: mst; +Cc: paulmck, arnd, linux-kernel

From: "Michael S. Tsirkin" <mst@redhat.com>
Date: Wed, 23 Nov 2016 22:48:19 +0200

> I would appreciate review to confirm the function doesn't
> do anything unsafe though.
> 
> In particular, should this use __hlist_for_each_rcu instead?
> I note that __hlist_for_each_rcu does rcu_dereference
> internally, which is missing here.

I personally think it should use __hlist_for_each_rcu, otherwise
nothing expresses the rcu-ness of the operation.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH RFC] hlist_add_tail_rcu disable sparse warning
  2016-11-26  0:52 ` David Miller
@ 2016-11-28 14:39   ` Paul E. McKenney
  2016-11-28 15:30     ` Michael S. Tsirkin
  2016-12-05  3:47     ` Michael S. Tsirkin
  2016-12-05  3:29   ` Michael S. Tsirkin
  1 sibling, 2 replies; 7+ messages in thread
From: Paul E. McKenney @ 2016-11-28 14:39 UTC (permalink / raw)
  To: David Miller; +Cc: mst, arnd, linux-kernel

On Fri, Nov 25, 2016 at 07:52:23PM -0500, David Miller wrote:
> From: "Michael S. Tsirkin" <mst@redhat.com>
> Date: Wed, 23 Nov 2016 22:48:19 +0200
> 
> > I would appreciate review to confirm the function doesn't
> > do anything unsafe though.
> > 
> > In particular, should this use __hlist_for_each_rcu instead?
> > I note that __hlist_for_each_rcu does rcu_dereference
> > internally, which is missing here.
> 
> I personally think it should use __hlist_for_each_rcu, otherwise
> nothing expresses the rcu-ness of the operation.

I like Dave's suggestion.  Michael, does that change work for you?

							Thanx, Paul

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH RFC] hlist_add_tail_rcu disable sparse warning
  2016-11-28 14:39   ` Paul E. McKenney
@ 2016-11-28 15:30     ` Michael S. Tsirkin
  2016-12-05  3:47     ` Michael S. Tsirkin
  1 sibling, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2016-11-28 15:30 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: David Miller, arnd, linux-kernel

On Mon, Nov 28, 2016 at 06:39:19AM -0800, Paul E. McKenney wrote:
> On Fri, Nov 25, 2016 at 07:52:23PM -0500, David Miller wrote:
> > From: "Michael S. Tsirkin" <mst@redhat.com>
> > Date: Wed, 23 Nov 2016 22:48:19 +0200
> > 
> > > I would appreciate review to confirm the function doesn't
> > > do anything unsafe though.
> > > 
> > > In particular, should this use __hlist_for_each_rcu instead?
> > > I note that __hlist_for_each_rcu does rcu_dereference
> > > internally, which is missing here.
> > 
> > I personally think it should use __hlist_for_each_rcu, otherwise
> > nothing expresses the rcu-ness of the operation.
> 
> I like Dave's suggestion.  Michael, does that change work for you?
> 
> 							Thanx, Paul

I think it does, I'll try and post the patch.

-- 
MST

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH RFC] hlist_add_tail_rcu disable sparse warning
  2016-11-26  0:52 ` David Miller
  2016-11-28 14:39   ` Paul E. McKenney
@ 2016-12-05  3:29   ` Michael S. Tsirkin
  1 sibling, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2016-12-05  3:29 UTC (permalink / raw)
  To: David Miller; +Cc: paulmck, arnd, linux-kernel

On Fri, Nov 25, 2016 at 07:52:23PM -0500, David Miller wrote:
> From: "Michael S. Tsirkin" <mst@redhat.com>
> Date: Wed, 23 Nov 2016 22:48:19 +0200
> 
> > I would appreciate review to confirm the function doesn't
> > do anything unsafe though.
> > 
> > In particular, should this use __hlist_for_each_rcu instead?
> > I note that __hlist_for_each_rcu does rcu_dereference
> > internally, which is missing here.
> 
> I personally think it should use __hlist_for_each_rcu, otherwise
> nothing expresses the rcu-ness of the operation.

What does "rcu-ness" mean in this context?

The question is not just about making the code pretty.

This operation is called outside any rcu critical section.
If you are going to call __hlist_for_each_rcu which
calls rcu_dereference, you should do it inside
a critical section.

Other operations such as hlist_add_behind_rcu manipulate
lists manually, maybe this one should, too?
Paul?

-- 
MST

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH RFC] hlist_add_tail_rcu disable sparse warning
  2016-11-28 14:39   ` Paul E. McKenney
  2016-11-28 15:30     ` Michael S. Tsirkin
@ 2016-12-05  3:47     ` Michael S. Tsirkin
  1 sibling, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2016-12-05  3:47 UTC (permalink / raw)
  To: Paul E. McKenney; +Cc: David Miller, arnd, linux-kernel

On Mon, Nov 28, 2016 at 06:39:19AM -0800, Paul E. McKenney wrote:
> On Fri, Nov 25, 2016 at 07:52:23PM -0500, David Miller wrote:
> > From: "Michael S. Tsirkin" <mst@redhat.com>
> > Date: Wed, 23 Nov 2016 22:48:19 +0200
> > 
> > > I would appreciate review to confirm the function doesn't
> > > do anything unsafe though.
> > > 
> > > In particular, should this use __hlist_for_each_rcu instead?
> > > I note that __hlist_for_each_rcu does rcu_dereference
> > > internally, which is missing here.
> > 
> > I personally think it should use __hlist_for_each_rcu, otherwise
> > nothing expresses the rcu-ness of the operation.
> 
> I like Dave's suggestion.  Michael, does that change work for you?
> 
> 							Thanx, Paul

I think we should change __hlist_for_each_rcu to skip rcu_dereference
though then, since it would be called outside any rcu read size critical
sections here.


-- 
MST

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH RFC] hlist_add_tail_rcu disable sparse warning
  2016-11-23 20:48 [PATCH RFC] hlist_add_tail_rcu disable sparse warning Michael S. Tsirkin
  2016-11-26  0:52 ` David Miller
@ 2016-12-05 19:37 ` Michael S. Tsirkin
  1 sibling, 0 replies; 7+ messages in thread
From: Michael S. Tsirkin @ 2016-12-05 19:37 UTC (permalink / raw)
  To: David Miller, Paul E. McKenney, Arnd Bergmann; +Cc: linux-kernel

On Wed, Nov 23, 2016 at 10:48:19PM +0200, Michael S. Tsirkin wrote:
> sparse is unhappy about this code in hlist_add_tail_rcu:
> 
>         struct hlist_node *i, *last = NULL;
> 
>         for (i = hlist_first_rcu(h); i; i = hlist_next_rcu(i))
>                 last = i;
> 
> This is because hlist_next_rcu and hlist_next_rcu return
> __rcu pointers.
> 
> The following trivial patch disables the warning
> without changing the behaviour in any way.
> 
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

So after reviewing this, there's no rcu-ness involved
except when we assign the next pointer on the last item.
This is because as the following comment says
 * The caller must take whatever precautions are necessary
 * (such as holding appropriate locks) to avoid racing
 * with another list-mutation primitive, such as hlist_add_head_rcu()
 * or hlist_del_rcu(), running on this same list.

I conclude this patch is actually the right thing to do, by comparison,
__hlist_for_each_rcu suggested by Dave Miller would be confusing since
it's designed to run in the rcu read side critical section.

I'll repost as non-RFC unless I hear otherwise.


> ---
> 
> I would appreciate review to confirm the function doesn't
> do anything unsafe though.
> 
> In particular, should this use __hlist_for_each_rcu instead?
> I note that __hlist_for_each_rcu does rcu_dereference
> internally, which is missing here.
> 
> compile-tested only.
> 
> diff --git a/include/linux/rculist.h b/include/linux/rculist.h
> index 8beb98d..33574db 100644
> --- a/include/linux/rculist.h
> +++ b/include/linux/rculist.h
> @@ -511,7 +511,7 @@ static inline void hlist_add_tail_rcu(struct hlist_node *n,
>  {
>  	struct hlist_node *i, *last = NULL;
>  
> -	for (i = hlist_first_rcu(h); i; i = hlist_next_rcu(i))
> +	for (i = h->first; i; i = i->next)
>  		last = i;
>  
>  	if (last) {

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-12-05 19:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-23 20:48 [PATCH RFC] hlist_add_tail_rcu disable sparse warning Michael S. Tsirkin
2016-11-26  0:52 ` David Miller
2016-11-28 14:39   ` Paul E. McKenney
2016-11-28 15:30     ` Michael S. Tsirkin
2016-12-05  3:47     ` Michael S. Tsirkin
2016-12-05  3:29   ` Michael S. Tsirkin
2016-12-05 19:37 ` Michael S. Tsirkin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).