Skip to content

futex: Cure robust/PI futex exit races

Thomas Gleixner lkml oe-kbuild-all stable 1 version Applied

Status from this message: The following commit has been merged into the locking/core branch of tip:

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest12 patches 18 repliesApplied

Patches in v1 Applied

#Subject and filesSizeRepliesApplied
1/12futex: Prevent robust futex exit racekernel/futex.c+51 -70 replies
2/12futex: Move futex exit handling into futex codeinclude/linux/compat.h include/linux/futex.h kernel/fork.c +1 more+46 -413 replies
3/12futex: Replace PF_EXITPIDONE with a stateinclude/linux/futex.h include/linux/sched.h kernel/exit.c +1 more+49 -290 replies
4/12exit/exec: Seperate mm_release()fs/exec.c include/linux/sched/mm.h kernel/exit.c +1 more+17 -50 replies
5/12futex: Split futex_mm_release() for exit/execinclude/linux/futex.h kernel/fork.c kernel/futex.c+12 -60 replies
6/12futex: Set task::futex state to DEAD right after handling futex exitkernel/exit.c kernel/futex.c+1 -10 replies
7/12futex: Mark the begin of futex exit explicitlyinclude/linux/futex.h kernel/exit.c kernel/futex.c+40 -410 replies
8/12futex: Sanitize exit state handlingkernel/futex.c+10 -71 reply
9/12futex: Provide state handling for exec() as wellkernel/futex.c+34 -42 replies
10/12futex: Add mutex around futex exitinclude/linux/futex.h include/linux/sched.h kernel/futex.c+18 -00 replies
11/12futex: Provide distinct return value when owner is exitingkernel/futex.c+9 -70 replies
12/12futex: Prevent exit livelockkernel/futex.c+91 -150 replies

Reviews trailers per patch

None of the 12 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchIngo MolnarPeter Zijlstra (Intel)
SeriesR-b A-b
1/12
2/12
3/12
4/12
5/12
6/12
7/12
8/12
9/12
10/12
11/12
12/12

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [patch 00/12] futex: Cure robust/PI futex exit races

This series addresses a couple of robust/PI futex exit races:

1) The unlock races debugged and fixed by Yi and Yang

These races are really subtle and I'm still puzzled how to trigger them reliably enough to decode them.

The basic issue is that:

A) An unlocking task can be killed between clearing the user space futex value and calling futex(FUTEX_WAKE).

B) A woken up waiter can be killed before it can acquire the futex after returning to user space.

In both cases the futex value is 0 and due to that the robust list exit code refuses to wake up waiters as the futex is not owned by the exiting task. As a consequence all other waiters might be blocked forever.

2) Oleg provided a test case which causes an infinite loop in the futex_lock_pi() code.

The problem there is that an exiting task might be preempted by a waiter in a state which makes the waiter busy wait for the exiting task to complete the robust/PI exit cleanup code.

That's obviously impossible when the waiter has higher priority than the exiting task and both are pinned on the same CPU resulting in a live lock.

#1 is a straight forward and simple fix

The solution Yi and Yang provided looks solid and in the worst case causes a spurious wakeup of a waiter which is nothing to worry about as all waiter code has to be prepared for that anyway.

#2 is more complex

In the current implementation there is no way to block until the exiting task has finished the cleanup.

To fix this there is quite some code reshuffling required which at the same time is a valuable cleanup.

The final solution is to guard the futex exit handling with a per task mutex and make the waiter block on that mutex until the exiting task has the cleanup completed.

Details why a simpler solution is not feasible can be found here:

https://lore.kernel.org/r/[email protected]

Ignore my confusion of fork vs. vfork at the beginning of the thread. Futexes do that to human brains. :)

The following series addresses both issues.

Patch 1 is a slightly polished version of the original Yi and Yang submission. It is included for completeness sake and because it creates conflicts with the larger surgery which fixes issue #2.

Aside of that a few eyeballs more on that subtlety are definitely not a bad thing especially as this has a user space component in it.

The rest of the series addresses issue #2 which is more or less a kernel only problem, but extra eyeballs are appreciated.

I'm certainly not proud about the solution for #2 but it's the best I could come up with without violating the user/kernel state consistency constraints.

Rusty Russell was definitely right when he said that futexes are cursed, but as Peter Zijlstra pointed out he should have named them SNAFUtex right away.

The series is also available from git:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git WIP.locking/futex

Thanks,

tglx

8<-------------

 fs/exec.c                |    2 
 include/linux/compat.h   |    2 
 include/linux/futex.h    |   38 +++--
 include/linux/sched.h    |    3 
 include/linux/sched/mm.h |    6 
 kernel/exit.c            |   30 ----
 kernel/fork.c            |   40 ++---
 kernel/futex.c           |  324 ++++++++++++++++++++++++++++++++++++++++-------
 8 files changed, 330 insertions(+), 115 deletions(-)