mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Michael Kerrisk (man-pages)" <mtk.manpages@gmail.com>
To: Darren Hart <dvhart@infradead.org>,
	Matthieu CASTET <matthieu.castet@parrot.com>
Cc: mtk.manpages@gmail.com, linux-kernel@vger.kernel.org,
	Darren Hart <dvhart@linux.intel.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Davidlohr Bueso <dave@stgolabs.net>,
	Thomas Gleixner <tglx@linutronix.de>,
	Eric Dumazet <dada1@cosmosbay.com>
Subject: Re: futex: Allow FUTEX_CLOCK_REALTIME with FUTEX_WAIT op
Date: Thu, 23 Jun 2016 12:53:30 +0200	[thread overview]
Message-ID: <9cc557d5-0546-42da-4230-b2f7299e1958@gmail.com> (raw)
In-Reply-To: <20160623044828.GA3379@f23x64.localdomain>

Hi Darren,

On 06/23/2016 06:48 AM, Darren Hart wrote:
> On Mon, Jun 20, 2016 at 04:26:52PM +0200, Matthieu CASTET wrote:
>> Hi,
>>
>> the commit 337f13046ff03717a9e99675284a817527440a49 is saying that it
>> change to syscall to an equivalent to FUTEX_WAIT_BITSET |
>> FUTEX_CLOCK_REALTIME with a bitset of FUTEX_BITSET_MATCH_ANY.
>>
>> It seems wrong to me, because in case of FUTEX_WAIT, in
>> "SYSCALL_DEFINE6(futex", we convert relative timeout to absolute
>> timeout [1].
>>
>> So FUTEX_CLOCK_REALTIME | FUTEX_WAIT is expecting a relative timeout
>> when FUTEX_WAIT_BITSET take an absolute timeout.
>>
>> To make it work you have to use something like the (untested) attached
>> patch.
>
> +Eric Dumazet
>
> Thanks for reporting Matthieu,
>
> FUTEX_WAIT traditionally used a relative timeout with CLOCK_MONOTONIC while
> FUTEX_WAIT_BITSET could use either ??? based on the FUTEX_CLOCK_ flag used. The
> man page is not particularly clear on this:
>
> http://man7.org/linux/man-pages/man2/futex.2.html
>
> "
> The FUTEX_WAIT_BITSET operation also interprets the timeout argument
> differently from FUTEX_WAIT.  See the discussion of FUTEX_CLOCK_REALTIME,
> above.
> "
>
> Matthew Kerrisk:
> I think this language could be removed now that we support the
> FUTEX_CLOCK_REALTIME flag for both futex ops.

Done.

> As for the intended behavior of the FUTEX_CLOCK_REALTIME flag:
>
>
> FUTEX_CLOCK_REALTIME (since Linux 2.6.28)
>     This option bit can be employed only with the FUTEX_WAIT_BITSET,
>     FUTEX_WAIT_REQUEUE_PI, and FUTEX_WAIT (since Linux 4.5) operations.
>
> (NOTE: FUTEX_WAIT was recently added after the patch in question here)
>
>     If this option is set, the kernel treats timeout as an absolute time based
>     on CLOCK_REALTIME.
>
>     If this option is not set, the kernel treats timeout as a relative time,
>     measured against the CLOCK_MONOTONIC clock.
>
>
> This supports your argument Matthieu. The assumption of a relative timeout for
> FUTEX_WAIT in SYSCALL_DEFINE6 needs to be updated to account for FUTEX_WAIT now
> honoring the FUTEX_CLOCK_REALTIME flag, which treats the timeout as absolute.
>
> However, I don't think the patch below is correct. The existing logic
> determines the type of timeout based on the futex_op when it should instead
> determine the type of timeout based on the FUTEX_CLOCK_REALTIME flag.
>
> My reading of the man page is that FUTEX_WAIT_BITSET abides by the timeout
> interpretation defined by the FUTEX_CLOCK_REALTIME attribute, so
> SYSCALL_DEFINE6 was misbehaving for FUTEX_WAIT|FUTEX_CLOCK_REALTIME (where the
> timeout should have been treated as absolute) as well as for
> FUTEX_WAIT_BITSET|FUTEX_CLOCK_MONOTONIC (where the timeout should have been
> treated as relative).
>
> Consider the following:
>
> diff --git a/kernel/futex.c b/kernel/futex.c
> index 33664f7..fa2af29 100644
> --- a/kernel/futex.c
> +++ b/kernel/futex.c
> @@ -3230,7 +3230,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
>  			return -EINVAL;
>
>  		t = timespec_to_ktime(ts);
> -		if (cmd == FUTEX_WAIT)
> +		if (!(cmd & FUTEX_CLOCK_REALTIME))
>  			t = ktime_add_safe(ktime_get(), t);
>  		tp = &t;
>  	}
>
> The concern for me is whether the code is incorrect, or if the man page is
> incorrect. Does existing userspace code expect the FUTEX_WAIT_BITSET op to
> always use an absolute timeout, regardless of the CLOCK used?

So, there clearly seem to be some things broken in the man page. See the
reply I sent to tglx.

Cheers,

Michael


>> [1]
>>         if (cmd == FUTEX_WAIT)
>>             t = ktime_add_safe(ktime_get(), t);
>
>> diff --git a/kernel/futex.c b/kernel/futex.c
>> index 33664f7..4bee915 100644
>> --- a/kernel/futex.c
>> +++ b/kernel/futex.c
>> @@ -3230,7 +3230,7 @@ SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
>>  			return -EINVAL;
>>
>>  		t = timespec_to_ktime(ts);
>> -		if (cmd == FUTEX_WAIT)
>> +		if (cmd == FUTEX_WAIT && !(op & FUTEX_CLOCK_REALTIME))
>>  			t = ktime_add_safe(ktime_get(), t);
>>  		tp = &t;
>>  	}
>
>


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/

      parent reply	other threads:[~2016-06-23 10:53 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-20 14:26 Matthieu CASTET
2016-06-23  4:48 ` Darren Hart
2016-06-23  7:18   ` Thomas Gleixner
2016-06-23 10:52     ` Michael Kerrisk (man-pages)
2016-06-23 13:40       ` Thomas Gleixner
2016-06-23 16:16         ` Darren Hart
2016-06-23 17:26           ` Thomas Gleixner
2016-06-23 18:28             ` Darren Hart
2016-06-23 18:41               ` Michael Kerrisk (man-pages)
2016-06-23 19:55                 ` Darren Hart
2016-06-23 20:31                   ` Darren Hart
     [not found]                     ` <d8bcb621-012e-f34c-4cf9-2d09aa23a43c@gmail.com>
2016-06-24  9:52                       ` Thomas Gleixner
2016-06-24 10:00                         ` Michael Kerrisk (man-pages)
2016-07-06 18:57                 ` Thomas Gleixner
2016-06-23 18:35           ` Michael Kerrisk (man-pages)
2016-06-23 19:53             ` Darren Hart
2016-06-24  8:13               ` Michael Kerrisk (man-pages)
2016-06-23 10:53   ` Michael Kerrisk (man-pages) [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=9cc557d5-0546-42da-4230-b2f7299e1958@gmail.com \
    --to=mtk.manpages@gmail.com \
    --cc=dada1@cosmosbay.com \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=dvhart@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthieu.castet@parrot.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®