2021-04-22 20:31:24

by Thomas Gleixner

[permalink] [raw]
Subject: [patch 5/6] futex: Prepare futex_lock_pi() for runtime clock selection

futex_lock_pi() is the only futex operation which cannot select the clock
for timeouts (CLOCK_MONOTONIC/CLOCK_REALTIME). That's inconsistent and
there is no particular reason why this cannot be supported.

This was overlooked when CLOCK_REALTIME_FLAG was introduced and
unfortunately not reported when the inconsistency was discovered in glibc.

Prepare the function and enforce the CLOCK_REALTIME_FLAG on FUTEX_LOCK_PI
so that a new FUTEX_LOCK_PI2 can implement it correctly.

Reported-by: Kurt Kanzenbach <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
---
kernel/futex.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -2786,7 +2786,7 @@ static int futex_lock_pi(u32 __user *uad
if (refill_pi_state_cache())
return -ENOMEM;

- to = futex_setup_timer(time, &timeout, FLAGS_CLOCKRT, 0);
+ to = futex_setup_timer(time, &timeout, flags, 0);

retry:
ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, FUTEX_WRITE);
@@ -3711,7 +3711,7 @@ long do_futex(u32 __user *uaddr, int op,

if (op & FUTEX_CLOCK_REALTIME) {
flags |= FLAGS_CLOCKRT;
- if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
+ if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
return -ENOSYS;
}

@@ -3743,6 +3743,7 @@ long do_futex(u32 __user *uaddr, int op,
case FUTEX_WAKE_OP:
return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
case FUTEX_LOCK_PI:
+ flags |= FLAGS_CLOCKRT;
return futex_lock_pi(uaddr, flags, timeout, 0);
case FUTEX_UNLOCK_PI:
return futex_unlock_pi(uaddr, flags);


2021-04-23 09:46:32

by Lukasz Majewski

[permalink] [raw]
Subject: Re: [patch 5/6] futex: Prepare futex_lock_pi() for runtime clock selection

Hi Thomas,

> futex_lock_pi() is the only futex operation which cannot select the
> clock for timeouts (CLOCK_MONOTONIC/CLOCK_REALTIME). That's
> inconsistent and there is no particular reason why this cannot be
> supported.
>
> This was overlooked when CLOCK_REALTIME_FLAG was introduced and
> unfortunately not reported when the inconsistency was discovered in
> glibc.
>
> Prepare the function and enforce the CLOCK_REALTIME_FLAG on
> FUTEX_LOCK_PI so that a new FUTEX_LOCK_PI2 can implement it correctly.
>
> Reported-by: Kurt Kanzenbach <[email protected]>
> Signed-off-by: Thomas Gleixner <[email protected]>
> ---
> kernel/futex.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> --- a/kernel/futex.c
> +++ b/kernel/futex.c
> @@ -2786,7 +2786,7 @@ static int futex_lock_pi(u32 __user *uad
> if (refill_pi_state_cache())
> return -ENOMEM;
>
> - to = futex_setup_timer(time, &timeout, FLAGS_CLOCKRT, 0);
> + to = futex_setup_timer(time, &timeout, flags, 0);
>
> retry:
> ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key,
> FUTEX_WRITE); @@ -3711,7 +3711,7 @@ long do_futex(u32 __user *uaddr,
> int op,
> if (op & FUTEX_CLOCK_REALTIME) {
> flags |= FLAGS_CLOCKRT;
> - if (cmd != FUTEX_WAIT_BITSET && cmd !=
> FUTEX_WAIT_REQUEUE_PI)
> + if (cmd != FUTEX_WAIT_BITSET && cmd !=
> FUTEX_WAIT_REQUEUE_PI) return -ENOSYS;

What is the exact change for those two lines above? Looks like some
different tab/spaces...

> }
>
> @@ -3743,6 +3743,7 @@ long do_futex(u32 __user *uaddr, int op,
> case FUTEX_WAKE_OP:
> return futex_wake_op(uaddr, flags, uaddr2, val,
> val2, val3); case FUTEX_LOCK_PI:
> + flags |= FLAGS_CLOCKRT;
> return futex_lock_pi(uaddr, flags, timeout, 0);
> case FUTEX_UNLOCK_PI:
> return futex_unlock_pi(uaddr, flags);
>



Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: [email protected]


Attachments:
(No filename) (499.00 B)
OpenPGP digital signature

2021-04-23 10:10:25

by Thomas Gleixner

[permalink] [raw]
Subject: Re: [patch 5/6] futex: Prepare futex_lock_pi() for runtime clock selection

On Fri, Apr 23 2021 at 11:34, Lukasz Majewski wrote:
>> ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key,
>> FUTEX_WRITE); @@ -3711,7 +3711,7 @@ long do_futex(u32 __user *uaddr,
>> int op,
>> if (op & FUTEX_CLOCK_REALTIME) {
>> flags |= FLAGS_CLOCKRT;
>> - if (cmd != FUTEX_WAIT_BITSET && cmd !=
>> FUTEX_WAIT_REQUEUE_PI)
>> + if (cmd != FUTEX_WAIT_BITSET && cmd !=
>> FUTEX_WAIT_REQUEUE_PI) return -ENOSYS;
>
> What is the exact change for those two lines above? Looks like some
> different tab/spaces...

Oops. Yes. I surely stared at every hunk more than once...

Subject: [tip: locking/core] futex: Prepare futex_lock_pi() for runtime clock selection

The following commit has been merged into the locking/core branch of tip:

Commit-ID: e112c41341c03d9224a9fc522bdb3539bc849b56
Gitweb: https://git.kernel.org/tip/e112c41341c03d9224a9fc522bdb3539bc849b56
Author: Thomas Gleixner <[email protected]>
AuthorDate: Thu, 22 Apr 2021 21:44:22 +02:00
Committer: Peter Zijlstra <[email protected]>
CommitterDate: Tue, 22 Jun 2021 16:42:08 +02:00

futex: Prepare futex_lock_pi() for runtime clock selection

futex_lock_pi() is the only futex operation which cannot select the clock
for timeouts (CLOCK_MONOTONIC/CLOCK_REALTIME). That's inconsistent and
there is no particular reason why this cannot be supported.

This was overlooked when CLOCK_REALTIME_FLAG was introduced and
unfortunately not reported when the inconsistency was discovered in glibc.

Prepare the function and enforce the CLOCK_REALTIME_FLAG on FUTEX_LOCK_PI
so that a new FUTEX_LOCK_PI2 can implement it correctly.

Reported-by: Kurt Kanzenbach <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
---
kernel/futex.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index 08008c2..f820439 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -2783,7 +2783,7 @@ static int futex_lock_pi(u32 __user *uaddr, unsigned int flags,
if (refill_pi_state_cache())
return -ENOMEM;

- to = futex_setup_timer(time, &timeout, FLAGS_CLOCKRT, 0);
+ to = futex_setup_timer(time, &timeout, flags, 0);

retry:
ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, FUTEX_WRITE);
@@ -3739,6 +3739,7 @@ long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
case FUTEX_WAKE_OP:
return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
case FUTEX_LOCK_PI:
+ flags |= FLAGS_CLOCKRT;
return futex_lock_pi(uaddr, flags, timeout, 0);
case FUTEX_UNLOCK_PI:
return futex_unlock_pi(uaddr, flags);