public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: li guang <lig.fnst@cn.fujitsu.com>
Cc: viro@zeniv.linux.org.uk, edumazet@google.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] task_work: make FIFO task_work list
Date: Fri, 15 Mar 2013 15:34:12 +0100	[thread overview]
Message-ID: <20130315143412.GA21365@redhat.com> (raw)
In-Reply-To: <1363306591.21129.117.camel@liguang.fnst.cn.fujitsu.com>

On 03/15, li guang wrote:
>
> 在 2013-03-14四的 15:40 +0100,Oleg Nesterov写道:
> > > --- a/kernel/task_work.c
> > > +++ b/kernel/task_work.c
> > > @@ -13,11 +13,12 @@ task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
> > >  		head = ACCESS_ONCE(task->task_works);
> > >  		if (unlikely(head == &work_exited))
> > >  			return -ESRCH;
> > > -		work->next = head;
> > > -	} while (cmpxchg(&task->task_works, head, work) != head);
> > > +		head = head->next;
> > > +	} while (cmpxchg(&head, NULL, work) == head);
> >
> > I simply can't understand how this can work... The patch assumes
> > that head->next == NULL after head = head->next, why? And then
> > compares the result with head and succeeds if not equal.
> >
>
> then ->next filed was not initialized, so I think it will
> be 0'ed by compiler, is it unreliable?.

work->next is not necessarily initialized, but this is not the main
problem...

> > Could you please explain how it was supposed to work? If nothing
> > else, Suppose we have task->task_works -> W1 -> W2 -> W3. How this
> > code can add W4 after W3?
> >
>
> 1. head = task_works

head == &W1

> 2. head = head->next

head == &W2

> 3. if head == NULL
> 	/* it's next node of list tail (w3->next) */
> 	head = work

No,

>    else
>   	goto 1

And? You restart from ->task_works again.

> > Anyway, whatever I missed this is racy.
> >
> > 	head = head->next;
> >
> > nothing protects "head" after this. Say, it can be task_work_cancel'ed
> > and freed. So,
> >
> > 	 cmpxchg(&head, ...)
> >
> > can modify the freed and reused memory.
> >
> > Oleg.
>
> Thanks Oleg,
> Hmm, at first, I think even it was changed, it can't happened to be
> NULL, but ... maybe it need more deliberation.

My point was, even if it is not NULL nothing protects this element. It can
be freed/reused before you do cmpxchg(&head).

> The motivation it make the list FIFO at task_work_add, so you don't
> need to reverse it at task_work_run,

I understand, but this is not easy and unlikely possible without the
locking.

> and it's a time-saver if the list

Yes, but compared to the next loop which does do/while again _and_
calls the work->func() "Reverse the list" doesn't add too much.

Oleg.


      reply	other threads:[~2013-03-15 14:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-14  7:57 [PATCH 1/2] task_work: make FIFO task_work list liguang
2013-03-14  7:57 ` [PATCH 2/2] task_work: check callback if it's NULL liguang
2013-03-14 14:43   ` Oleg Nesterov
2013-03-15  0:20     ` li guang
2013-03-15  1:01       ` Li Zefan
2013-03-15  1:26         ` li guang
2013-03-15  1:43           ` Li Zefan
2013-03-15  2:29             ` li guang
2013-03-14 14:40 ` [PATCH 1/2] task_work: make FIFO task_work list Oleg Nesterov
2013-03-15  0:16   ` li guang
2013-03-15 14:34     ` Oleg Nesterov [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=20130315143412.GA21365@redhat.com \
    --to=oleg@redhat.com \
    --cc=edumazet@google.com \
    --cc=lig.fnst@cn.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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