• Andrew Morton's avatar
    [PATCH] Add FUTEX_CMP_REQUEUE futex op · 9b91d73b
    Andrew Morton authored
    From: Jakub Jelinek <jakub@redhat.com>
    
    FUTEX_REQUEUE operation has been added to the kernel mainly to improve
    pthread_cond_broadcast which previously used FUTEX_WAKE INT_MAX op.
    pthread_cond_broadcast releases internal condvar mutex before FUTEX_REQUEUE
    operation, as otherwise the woken up thread most likely immediately sleeps
    again on the internal condvar mutex until the broadcasting thread releases it.
    
    Unfortunately this is racy and causes e.g.
    http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/nptl/tst-cond16.c?rev=1.1&content-type=text/x-cvsweb-markup&cvsroot=glibc
    to hang on SMP.
    
    http://listman.redhat.com/archives/phil-list/2004-May/msg00023.html contains
    analysis how the hang happens, the problem is if any thread does
    pthread_cond_*wait in between releasing of the internal condvar mutex and
    FUTEX_REQUEUE operation, a wrong thread might be awaken (and immediately go to
    sleep again because it doesn't satisfy conditions for returning from
    pthread_cond_*wait) while the right thread requeued on the associated mutex
    and there would be nobody to wake that thread up.
    
    The patch below extends FUTEX_REQUEUE operation with something FUTEX_WAIT
    already uses:
    
    FUTEX_CMP_REQUEUE is passed an additional argument which is the expected value
    of *futex.  Kernel then while holding the futex locks checks if *futex !=
    expected and returns -EAGAIN in that case, while if it is equal, continues
    with a normal FUTEX_REQUEUE operation.  If the syscall returns -EAGAIN, NPTL
    can fall back to FUTEX_WAKE INT_MAX operation which doesn't have this problem,
    but is less efficient, while in the likely case that nobody hit the (small)
    window the efficient FUTEX_REQUEUE operation is used.
    Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
    9b91d73b
compat.c 13.5 KB