You need to sign in or sign up before continuing.
  1. 03 3月, 2014 1 次提交
  2. 16 1月, 2014 1 次提交
    • H
      futexes: Fix futex_hashsize initialization · 63b1a816
      Heiko Carstens 提交于
      "futexes: Increase hash table size for better performance"
      introduces a new alloc_large_system_hash() call.
      
      alloc_large_system_hash() however may allocate less memory than
      requested, e.g. limited by MAX_ORDER.
      
      Hence pass a pointer to alloc_large_system_hash() which will
      contain the hash shift when the function returns. Afterwards
      correctly set futex_hashsize.
      
      Fixes a crash on s390 where the requested allocation size was
      4MB but only 1MB was allocated.
      Signed-off-by: NHeiko Carstens <heiko.carstens@de.ibm.com>
      Cc: Darren Hart <dvhart@linux.intel.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
      Cc: Waiman Long <Waiman.Long@hp.com>
      Cc: Jason Low <jason.low2@hp.com>
      Cc: Davidlohr Bueso <davidlohr@hp.com>
      Link: http://lkml.kernel.org/r/20140116135450.GA4345@osirisSigned-off-by: NIngo Molnar <mingo@kernel.org>
      63b1a816
  3. 13 1月, 2014 5 次提交
    • P
      rtmutex: Turn the plist into an rb-tree · fb00aca4
      Peter Zijlstra 提交于
      Turn the pi-chains from plist to rb-tree, in the rt_mutex code,
      and provide a proper comparison function for -deadline and
      -priority tasks.
      
      This is done mainly because:
       - classical prio field of the plist is just an int, which might
         not be enough for representing a deadline;
       - manipulating such a list would become O(nr_deadline_tasks),
         which might be to much, as the number of -deadline task increases.
      
      Therefore, an rb-tree is used, and tasks are queued in it according
      to the following logic:
       - among two -priority (i.e., SCHED_BATCH/OTHER/RR/FIFO) tasks, the
         one with the higher (lower, actually!) prio wins;
       - among a -priority and a -deadline task, the latter always wins;
       - among two -deadline tasks, the one with the earliest deadline
         wins.
      
      Queueing and dequeueing functions are changed accordingly, for both
      the list of a task's pi-waiters and the list of tasks blocked on
      a pi-lock.
      Signed-off-by: NPeter Zijlstra <peterz@infradead.org>
      Signed-off-by: NDario Faggioli <raistlin@linux.it>
      Signed-off-by: NJuri Lelli <juri.lelli@gmail.com>
      Signed-off-again-by: NPeter Zijlstra <peterz@infradead.org>
      Link: http://lkml.kernel.org/r/1383831828-15501-10-git-send-email-juri.lelli@gmail.comSigned-off-by: NIngo Molnar <mingo@kernel.org>
      fb00aca4
    • D
      futexes: Avoid taking the hb->lock if there's nothing to wake up · b0c29f79
      Davidlohr Bueso 提交于
      In futex_wake() there is clearly no point in taking the hb->lock
      if we know beforehand that there are no tasks to be woken. While
      the hash bucket's plist head is a cheap way of knowing this, we
      cannot rely 100% on it as there is a racy window between the
      futex_wait call and when the task is actually added to the
      plist. To this end, we couple it with the spinlock check as
      tasks trying to enter the critical region are most likely
      potential waiters that will be added to the plist, thus
      preventing tasks sleeping forever if wakers don't acknowledge
      all possible waiters.
      
      Furthermore, the futex ordering guarantees are preserved,
      ensuring that waiters either observe the changed user space
      value before blocking or is woken by a concurrent waker. For
      wakers, this is done by relying on the barriers in
      get_futex_key_refs() -- for archs that do not have implicit mb
      in atomic_inc(), we explicitly add them through a new
      futex_get_mm function. For waiters we rely on the fact that
      spin_lock calls already update the head counter, so spinners
      are visible even if the lock hasn't been acquired yet.
      
      For more details please refer to the updated comments in the
      code and related discussion:
      
        https://lkml.org/lkml/2013/11/26/556
      
      Special thanks to tglx for careful review and feedback.
      Suggested-by: NLinus Torvalds <torvalds@linux-foundation.org>
      Reviewed-by: NDarren Hart <dvhart@linux.intel.com>
      Reviewed-by: NThomas Gleixner <tglx@linutronix.de>
      Reviewed-by: NPeter Zijlstra <peterz@infradead.org>
      Signed-off-by: NDavidlohr Bueso <davidlohr@hp.com>
      Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
      Cc: Mike Galbraith <efault@gmx.de>
      Cc: Jeff Mahoney <jeffm@suse.com>
      Cc: Scott Norton <scott.norton@hp.com>
      Cc: Tom Vaden <tom.vaden@hp.com>
      Cc: Aswin Chandramouleeswaran <aswin@hp.com>
      Cc: Waiman Long <Waiman.Long@hp.com>
      Cc: Jason Low <jason.low2@hp.com>
      Cc: Andrew Morton <akpm@linux-foundation.org>
      Link: http://lkml.kernel.org/r/1389569486-25487-5-git-send-email-davidlohr@hp.comSigned-off-by: NIngo Molnar <mingo@kernel.org>
      b0c29f79
    • T
      futexes: Document multiprocessor ordering guarantees · 99b60ce6
      Thomas Gleixner 提交于
      That's essential, if you want to hack on futexes.
      Reviewed-by: NDarren Hart <dvhart@linux.intel.com>
      Reviewed-by: NPeter Zijlstra <peterz@infradead.org>
      Reviewed-by: NPaul E. McKenney <paulmck@linux.vnet.ibm.com>
      Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: NDavidlohr Bueso <davidlohr@hp.com>
      Cc: Mike Galbraith <efault@gmx.de>
      Cc: Jeff Mahoney <jeffm@suse.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Randy Dunlap <rdunlap@infradead.org>
      Cc: Scott Norton <scott.norton@hp.com>
      Cc: Tom Vaden <tom.vaden@hp.com>
      Cc: Aswin Chandramouleeswaran <aswin@hp.com>
      Cc: Waiman Long <Waiman.Long@hp.com>
      Cc: Jason Low <jason.low2@hp.com>
      Cc: Andrew Morton <akpm@linux-foundation.org>
      Link: http://lkml.kernel.org/r/1389569486-25487-4-git-send-email-davidlohr@hp.comSigned-off-by: NIngo Molnar <mingo@kernel.org>
      99b60ce6
    • D
      futexes: Increase hash table size for better performance · a52b89eb
      Davidlohr Bueso 提交于
      Currently, the futex global hash table suffers from its fixed,
      smallish (for today's standards) size of 256 entries, as well as
      its lack of NUMA awareness. Large systems, using many futexes,
      can be prone to high amounts of collisions; where these futexes
      hash to the same bucket and lead to extra contention on the same
      hb->lock. Furthermore, cacheline bouncing is a reality when we
      have multiple hb->locks residing on the same cacheline and
      different futexes hash to adjacent buckets.
      
      This patch keeps the current static size of 16 entries for small
      systems, or otherwise, 256 * ncpus (or larger as we need to
      round the number to a power of 2). Note that this number of CPUs
      accounts for all CPUs that can ever be available in the system,
      taking into consideration things like hotpluging. While we do
      impose extra overhead at bootup by making the hash table larger,
      this is a one time thing, and does not shadow the benefits of
      this patch.
      
      Furthermore, as suggested by tglx, by cache aligning the hash
      buckets we can avoid access across cacheline boundaries and also
      avoid massive cache line bouncing if multiple cpus are hammering
      away at different hash buckets which happen to reside in the
      same cache line.
      
      Also, similar to other core kernel components (pid, dcache,
      tcp), by using alloc_large_system_hash() we benefit from its
      NUMA awareness and thus the table is distributed among the nodes
      instead of in a single one.
      
      For a custom microbenchmark that pounds on the uaddr hashing --
      making the wait path fail at futex_wait_setup() returning
      -EWOULDBLOCK for large amounts of futexes, we can see the
      following benefits on a 80-core, 8-socket 1Tb server:
      
       +---------+--------------------+------------------------+-----------------------+-------------------------------+
       | threads | baseline (ops/sec) | aligned-only (ops/sec) | large table (ops/sec) | large table+aligned (ops/sec) |
       +---------+--------------------+------------------------+-----------------------+-------------------------------+
       |     512 |              32426 | 50531  (+55.8%)        | 255274  (+687.2%)     | 292553  (+802.2%)             |
       |     256 |              65360 | 99588  (+52.3%)        | 443563  (+578.6%)     | 508088  (+677.3%)             |
       |     128 |             125635 | 200075 (+59.2%)        | 742613  (+491.1%)     | 835452  (+564.9%)             |
       |      80 |             193559 | 323425 (+67.1%)        | 1028147 (+431.1%)     | 1130304 (+483.9%)             |
       |      64 |             247667 | 443740 (+79.1%)        | 997300  (+302.6%)     | 1145494 (+362.5%)             |
       |      32 |             628412 | 721401 (+14.7%)        | 965996  (+53.7%)      | 1122115 (+78.5%)              |
       +---------+--------------------+------------------------+-----------------------+-------------------------------+
      Reviewed-by: NDarren Hart <dvhart@linux.intel.com>
      Reviewed-by: NPeter Zijlstra <peterz@infradead.org>
      Reviewed-by: NPaul E. McKenney <paulmck@linux.vnet.ibm.com>
      Reviewed-by: NWaiman Long <Waiman.Long@hp.com>
      Reviewed-and-tested-by: NJason Low <jason.low2@hp.com>
      Reviewed-by: NThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: NDavidlohr Bueso <davidlohr@hp.com>
      Cc: Mike Galbraith <efault@gmx.de>
      Cc: Jeff Mahoney <jeffm@suse.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Scott Norton <scott.norton@hp.com>
      Cc: Tom Vaden <tom.vaden@hp.com>
      Cc: Aswin Chandramouleeswaran <aswin@hp.com>
      Link: http://lkml.kernel.org/r/1389569486-25487-3-git-send-email-davidlohr@hp.comSigned-off-by: NIngo Molnar <mingo@kernel.org>
      a52b89eb
    • J
      futexes: Clean up various details · 0d00c7b2
      Jason Low 提交于
      - Remove unnecessary head variables.
      - Delete unused parameter in queue_unlock().
      Reviewed-by: NDarren Hart <dvhart@linux.intel.com>
      Reviewed-by: NPeter Zijlstra <peterz@infradead.org>
      Reviewed-by: NPaul E. McKenney <paulmck@linux.vnet.ibm.com>
      Reviewed-by: NThomas Gleixner <tglx@linutronix.de>
      Signed-off-by: NJason Low <jason.low2@hp.com>
      Signed-off-by: NDavidlohr Bueso <davidlohr@hp.com>
      Cc: Mike Galbraith <efault@gmx.de>
      Cc: Jeff Mahoney <jeffm@suse.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Scott Norton <scott.norton@hp.com>
      Cc: Tom Vaden <tom.vaden@hp.com>
      Cc: Aswin Chandramouleeswaran <aswin@hp.com>
      Cc: Waiman Long <Waiman.Long@hp.com>
      Cc: Andrew Morton <akpm@linux-foundation.org>
      Link: http://lkml.kernel.org/r/1389569486-25487-2-git-send-email-davidlohr@hp.comSigned-off-by: NIngo Molnar <mingo@kernel.org>
      0d00c7b2
  4. 13 12月, 2013 2 次提交
    • L
      futex: move user address verification up to common code · 5cdec2d8
      Linus Torvalds 提交于
      When debugging the read-only hugepage case, I was confused by the fact
      that get_futex_key() did an access_ok() only for the non-shared futex
      case, since the user address checking really isn't in any way specific
      to the private key handling.
      
      Now, it turns out that the shared key handling does effectively do the
      equivalent checks inside get_user_pages_fast() (it doesn't actually
      check the address range on x86, but does check the page protections for
      being a user page).  So it wasn't actually a bug, but the fact that we
      treat the address differently for private and shared futexes threw me
      for a loop.
      
      Just move the check up, so that it gets done for both cases.  Also, use
      the 'rw' parameter for the type, even if it doesn't actually matter any
      more (it's a historical artifact of the old racy i386 "page faults from
      kernel space don't check write protections").
      
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
      5cdec2d8
    • L
      futex: fix handling of read-only-mapped hugepages · f12d5bfc
      Linus Torvalds 提交于
      The hugepage code had the exact same bug that regular pages had in
      commit 7485d0d3 ("futexes: Remove rw parameter from
      get_futex_key()").
      
      The regular page case was fixed by commit 9ea71503 ("futex: Fix
      regression with read only mappings"), but the transparent hugepage case
      (added in a5b338f2: "thp: update futex compound knowledge") case
      remained broken.
      
      Found by Dave Jones and his trinity tool.
      Reported-and-tested-by: NDave Jones <davej@fedoraproject.org>
      Cc: stable@kernel.org # v2.6.38+
      Acked-by: NThomas Gleixner <tglx@linutronix.de>
      Cc: Mel Gorman <mgorman@suse.de>
      Cc: Darren Hart <dvhart@linux.intel.com>
      Cc: Andrea Arcangeli <aarcange@redhat.com>
      Cc: Oleg Nesterov <oleg@redhat.com>
      Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
      f12d5bfc
  5. 06 11月, 2013 1 次提交
  6. 26 6月, 2013 2 次提交
    • C
      futex: Use freezable blocking call · 88c8004f
      Colin Cross 提交于
      Avoid waking up every thread sleeping in a futex_wait call during
      suspend and resume by calling a freezable blocking call.  Previous
      patches modified the freezer to avoid sending wakeups to threads
      that are blocked in freezable blocking calls.
      
      This call was selected to be converted to a freezable call because
      it doesn't hold any locks or release any resources when interrupted
      that might be needed by another freezing task or a kernel driver
      during suspend, and is a common site where idle userspace tasks are
      blocked.
      Signed-off-by: NColin Cross <ccross@android.com>
      Cc: Rafael J. Wysocki <rjw@sisk.pl>
      Cc: arve@android.com
      Cc: Tejun Heo <tj@kernel.org>
      Cc: Oleg Nesterov <oleg@redhat.com>
      Cc: Darren Hart <dvhart@linux.intel.com>
      Cc: Randy Dunlap <rdunlap@infradead.org>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Link: http://lkml.kernel.org/r/1367458508-9133-8-git-send-email-ccross@android.comSigned-off-by: NThomas Gleixner <tglx@linutronix.de>
      88c8004f
    • Z
      futex: Take hugepages into account when generating futex_key · 13d60f4b
      Zhang Yi 提交于
      The futex_keys of process shared futexes are generated from the page
      offset, the mapping host and the mapping index of the futex user space
      address. This should result in an unique identifier for each futex.
      
      Though this is not true when futexes are located in different subpages
      of an hugepage. The reason is, that the mapping index for all those
      futexes evaluates to the index of the base page of the hugetlbfs
      mapping. So a futex at offset 0 of the hugepage mapping and another
      one at offset PAGE_SIZE of the same hugepage mapping have identical
      futex_keys. This happens because the futex code blindly uses
      page->index.
      
      Steps to reproduce the bug:
      
      1. Map a file from hugetlbfs. Initialize pthread_mutex1 at offset 0
         and pthread_mutex2 at offset PAGE_SIZE of the hugetlbfs
         mapping.
      
         The mutexes must be initialized as PTHREAD_PROCESS_SHARED because
         PTHREAD_PROCESS_PRIVATE mutexes are not affected by this issue as
         their keys solely depend on the user space address.
      
      2. Lock mutex1 and mutex2
      
      3. Create thread1 and in the thread function lock mutex1, which
         results in thread1 blocking on the locked mutex1.
      
      4. Create thread2 and in the thread function lock mutex2, which
         results in thread2 blocking on the locked mutex2.
      
      5. Unlock mutex2. Despite the fact that mutex2 got unlocked, thread2
         still blocks on mutex2 because the futex_key points to mutex1.
      
      To solve this issue we need to take the normal page index of the page
      which contains the futex into account, if the futex is in an hugetlbfs
      mapping. In other words, we calculate the normal page mapping index of
      the subpage in the hugetlbfs mapping.
      
      Mappings which are not based on hugetlbfs are not affected and still
      use page->index.
      
      Thanks to Mel Gorman who provided a patch for adding proper evaluation
      functions to the hugetlbfs code to avoid exposing hugetlbfs specific
      details to the futex code.
      
      [ tglx: Massaged changelog ]
      Signed-off-by: NZhang Yi <zhang.yi20@zte.com.cn>
      Reviewed-by: NJiang Biao <jiang.biao2@zte.com.cn>
      Tested-by: NMa Chenggong <ma.chenggong@zte.com.cn>
      Reviewed-by: N'Mel Gorman' <mgorman@suse.de>
      Acked-by: N'Darren Hart' <dvhart@linux.intel.com>
      Cc: 'Peter Zijlstra' <peterz@infradead.org>
      Cc: stable@vger.kernel.org
      Link: http://lkml.kernel.org/r/000101ce71a6%24a83c5880%24f8b50980%24@comSigned-off-by: NThomas Gleixner <tglx@linutronix.de>
      13d60f4b
  7. 12 5月, 2013 1 次提交
  8. 13 3月, 2013 1 次提交
  9. 28 2月, 2013 1 次提交
  10. 19 2月, 2013 1 次提交
    • T
      futex: Revert "futex: Mark get_robust_list as deprecated" · fe2b05f7
      Thomas Gleixner 提交于
      This reverts commit ec0c4274.
      
      get_robust_list() is in use and a removal would break existing user
      space. With the permission checks in place it's not longer a security
      hole. Remove the deprecation warnings.
      Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
      Cc: Cyrill Gorcunov <gorcunov@openvz.org>
      Cc: Richard Weinberger <richard@nod.at>
      Cc: akpm@linux-foundation.org
      Cc: paul.gortmaker@windriver.com
      Cc: davej@redhat.com
      Cc: keescook@chromium.org
      Cc: stable@vger.kernel.org
      Cc: ebiederm@xmission.com
      fe2b05f7
  11. 08 2月, 2013 1 次提交
  12. 27 11月, 2012 1 次提交
    • D
      futex: avoid wake_futex() for a PI futex_q · aa10990e
      Darren Hart 提交于
      Dave Jones reported a bug with futex_lock_pi() that his trinity test
      exposed.  Sometime between queue_me() and taking the q.lock_ptr, the
      lock_ptr became NULL, resulting in a crash.
      
      While futex_wake() is careful to not call wake_futex() on futex_q's with
      a pi_state or an rt_waiter (which are either waiting for a
      futex_unlock_pi() or a PI futex_requeue()), futex_wake_op() and
      futex_requeue() do not perform the same test.
      
      Update futex_wake_op() and futex_requeue() to test for q.pi_state and
      q.rt_waiter and abort with -EINVAL if detected.  To ensure any future
      breakage is caught, add a WARN() to wake_futex() if the same condition
      is true.
      
      This fix has seen 3 hours of testing with "trinity -c futex" on an
      x86_64 VM with 4 CPUS.
      
      [akpm@linux-foundation.org: tidy up the WARN()]
      Signed-off-by: NDarren Hart <dvhart@linux.intel.com>
      Reported-by: NDave Jones <davej@redat.com>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Ingo Molnar <mingo@elte.hu>
      Cc: John Kacur <jkacur@redhat.com>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
      aa10990e
  13. 01 11月, 2012 1 次提交
    • T
      futex: Handle futex_pi OWNER_DIED take over correctly · 59fa6245
      Thomas Gleixner 提交于
      Siddhesh analyzed a failure in the take over of pi futexes in case the
      owner died and provided a workaround.
      See: http://sourceware.org/bugzilla/show_bug.cgi?id=14076
      
      The detailed problem analysis shows:
      
      Futex F is initialized with PTHREAD_PRIO_INHERIT and
      PTHREAD_MUTEX_ROBUST_NP attributes.
      
      T1 lock_futex_pi(F);
      
      T2 lock_futex_pi(F);
         --> T2 blocks on the futex and creates pi_state which is associated
             to T1.
      
      T1 exits
         --> exit_robust_list() runs
             --> Futex F userspace value TID field is set to 0 and
                 FUTEX_OWNER_DIED bit is set.
      
      T3 lock_futex_pi(F);
         --> Succeeds due to the check for F's userspace TID field == 0
         --> Claims ownership of the futex and sets its own TID into the
             userspace TID field of futex F
         --> returns to user space
      
      T1 --> exit_pi_state_list()
             --> Transfers pi_state to waiter T2 and wakes T2 via
             	   rt_mutex_unlock(&pi_state->mutex)
      
      T2 --> acquires pi_state->mutex and gains real ownership of the
             pi_state
         --> Claims ownership of the futex and sets its own TID into the
             userspace TID field of futex F
         --> returns to user space
      
      T3 --> observes inconsistent state
      
      This problem is independent of UP/SMP, preemptible/non preemptible
      kernels, or process shared vs. private. The only difference is that
      certain configurations are more likely to expose it.
      
      So as Siddhesh correctly analyzed the following check in
      futex_lock_pi_atomic() is the culprit:
      
      	if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
      
      We check the userspace value for a TID value of 0 and take over the
      futex unconditionally if that's true.
      
      AFAICT this check is there as it is correct for a different corner
      case of futexes: the WAITERS bit became stale.
      
      Now the proposed change
      
      -	if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
      +       if (unlikely(ownerdied ||
      +                       !(curval & (FUTEX_TID_MASK | FUTEX_WAITERS)))) {
      
      solves the problem, but it's not obvious why and it wreckages the
      "stale WAITERS bit" case.
      
      What happens is, that due to the WAITERS bit being set (T2 is blocked
      on that futex) it enforces T3 to go through lookup_pi_state(), which
      in the above case returns an existing pi_state and therefor forces T3
      to legitimately fight with T2 over the ownership of the pi_state (via
      pi_state->mutex). Probelm solved!
      
      Though that does not work for the "WAITERS bit is stale" problem
      because if lookup_pi_state() does not find existing pi_state it
      returns -ERSCH (due to TID == 0) which causes futex_lock_pi() to
      return -ESRCH to user space because the OWNER_DIED bit is not set.
      
      Now there is a different solution to that problem. Do not look at the
      user space value at all and enforce a lookup of possibly available
      pi_state. If pi_state can be found, then the new incoming locker T3
      blocks on that pi_state and legitimately races with T2 to acquire the
      rt_mutex and the pi_state and therefor the proper ownership of the
      user space futex.
      
      lookup_pi_state() has the correct order of checks. It first tries to
      find a pi_state associated with the user space futex and only if that
      fails it checks for futex TID value = 0. If no pi_state is available
      nothing can create new state at that point because this happens with
      the hash bucket lock held.
      
      So the above scenario changes to:
      
      T1 lock_futex_pi(F);
      
      T2 lock_futex_pi(F);
         --> T2 blocks on the futex and creates pi_state which is associated
             to T1.
      
      T1 exits
         --> exit_robust_list() runs
             --> Futex F userspace value TID field is set to 0 and
                 FUTEX_OWNER_DIED bit is set.
      
      T3 lock_futex_pi(F);
         --> Finds pi_state and blocks on pi_state->rt_mutex
      
      T1 --> exit_pi_state_list()
             --> Transfers pi_state to waiter T2 and wakes it via
             	   rt_mutex_unlock(&pi_state->mutex)
      
      T2 --> acquires pi_state->mutex and gains ownership of the pi_state
         --> Claims ownership of the futex and sets its own TID into the
             userspace TID field of futex F
         --> returns to user space
      
      This covers all gazillion points on which T3 might come in between
      T1's exit_robust_list() clearing the TID field and T2 fixing it up. It
      also solves the "WAITERS bit stale" problem by forcing the take over.
      
      Another benefit of changing the code this way is that it makes it less
      dependent on untrusted user space values and therefor minimizes the
      possible wreckage which might be inflicted.
      
      As usual after staring for too long at the futex code my brain hurts
      so much that I really want to ditch that whole optimization of
      avoiding the syscall for the non contended case for PI futexes and rip
      out the maze of corner case handling code. Unfortunately we can't as
      user space relies on that existing behaviour, but at least thinking
      about it helps me to preserve my mental sanity. Maybe we should
      nevertheless :)
      Reported-and-tested-by: NSiddhesh Poyarekar <siddhesh.poyarekar@gmail.com>
      Link: http://lkml.kernel.org/r/alpine.LFD.2.02.1210232138540.2756@ionosAcked-by: NDarren Hart <dvhart@linux.intel.com>
      Cc: stable@vger.kernel.org
      Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
      59fa6245
  14. 24 7月, 2012 3 次提交
  15. 29 3月, 2012 2 次提交
  16. 15 2月, 2012 2 次提交
  17. 01 1月, 2012 1 次提交
    • H
      futex: Fix uninterruptible loop due to gate_area · e6780f72
      Hugh Dickins 提交于
      It was found (by Sasha) that if you use a futex located in the gate
      area we get stuck in an uninterruptible infinite loop, much like the
      ZERO_PAGE issue.
      
      While looking at this problem, PeterZ realized you'll get into similar
      trouble when hitting any install_special_pages() mapping.  And are there
      still drivers setting up their own special mmaps without page->mapping,
      and without special VM or pte flags to make get_user_pages fail?
      
      In most cases, if page->mapping is NULL, we do not need to retry at all:
      Linus points out that even /proc/sys/vm/drop_caches poses no problem,
      because it ends up using remove_mapping(), which takes care not to
      interfere when the page reference count is raised.
      
      But there is still one case which does need a retry: if memory pressure
      called shmem_writepage in between get_user_pages_fast dropping page
      table lock and our acquiring page lock, then the page gets switched from
      filecache to swapcache (and ->mapping set to NULL) whatever the refcount.
      Fault it back in to get the page->mapping needed for key->shared.inode.
      Reported-by: NSasha Levin <levinsasha928@gmail.com>
      Signed-off-by: NHugh Dickins <hughd@google.com>
      Cc: stable@vger.kernel.org
      Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
      e6780f72
  18. 31 10月, 2011 1 次提交
  19. 15 9月, 2011 2 次提交
  20. 27 7月, 2011 1 次提交
    • S
      futex: Fix regression with read only mappings · 9ea71503
      Shawn Bohrer 提交于
      commit 7485d0d3 (futexes: Remove rw
      parameter from get_futex_key()) in 2.6.33 fixed two problems:  First, It
      prevented a loop when encountering a ZERO_PAGE. Second, it fixed RW
      MAP_PRIVATE futex operations by forcing the COW to occur by
      unconditionally performing a write access get_user_pages_fast() to get
      the page.  The commit also introduced a user-mode regression in that it
      broke futex operations on read-only memory maps.  For example, this
      breaks workloads that have one or more reader processes doing a
      FUTEX_WAIT on a futex within a read only shared file mapping, and a
      writer processes that has a writable mapping issuing the FUTEX_WAKE.
      
      This fixes the regression for valid futex operations on RO mappings by
      trying a RO get_user_pages_fast() when the RW get_user_pages_fast()
      fails. This change makes it necessary to also check for invalid use
      cases, such as anonymous RO mappings (which can never change) and the
      ZERO_PAGE which the commit referenced above was written to address.
      
      This patch does restore the original behavior with RO MAP_PRIVATE
      mappings, which have inherent user-mode usage problems and don't really
      make sense.  With this patch performing a FUTEX_WAIT within a RO
      MAP_PRIVATE mapping will be successfully woken provided another process
      updates the region of the underlying mapped file.  However, the mmap()
      man page states that for a MAP_PRIVATE mapping:
      
        It is unspecified whether changes made to the file after
        the mmap() call are visible in the mapped region.
      
      So user-mode users attempting to use futex operations on RO MAP_PRIVATE
      mappings are depending on unspecified behavior.  Additionally a
      RO MAP_PRIVATE mapping could fail to wake up in the following case.
      
        Thread-A: call futex(FUTEX_WAIT, memory-region-A).
                  get_futex_key() return inode based key.
                  sleep on the key
        Thread-B: call mprotect(PROT_READ|PROT_WRITE, memory-region-A)
        Thread-B: write memory-region-A.
                  COW happen. This process's memory-region-A become related
                  to new COWed private (ie PageAnon=1) page.
        Thread-B: call futex(FUETX_WAKE, memory-region-A).
                  get_futex_key() return mm based key.
                  IOW, we fail to wake up Thread-A.
      
      Once again doing something like this is just silly and users who do
      something like this get what they deserve.
      
      While RO MAP_PRIVATE mappings are nonsensical, checking for a private
      mapping requires walking the vmas and was deemed too costly to avoid a
      userspace hang.
      
      This Patch is based on Peter Zijlstra's initial patch with modifications to
      only allow RO mappings for futex operations that need VERIFY_READ access.
      Reported-by: NDavid Oliver <david@rgmadvisors.com>
      Signed-off-by: NShawn Bohrer <sbohrer@rgmadvisors.com>
      Acked-by: NPeter Zijlstra <a.p.zijlstra@chello.nl>
      Signed-off-by: NDarren Hart <dvhart@linux.intel.com>
      Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
      Cc: peterz@infradead.org
      Cc: eric.dumazet@gmail.com
      Cc: zvonler@rgmadvisors.com
      Cc: hughd@google.com
      Link: http://lkml.kernel.org/r/1309450892-30676-1-git-send-email-sbohrer@rgmadvisors.com
      Cc: stable@kernel.org
      Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
      9ea71503
  21. 26 7月, 2011 1 次提交
    • B
      mm/futex: fix futex writes on archs with SW tracking of dirty & young · 2efaca92
      Benjamin Herrenschmidt 提交于
      I haven't reproduced it myself but the fail scenario is that on such
      machines (notably ARM and some embedded powerpc), if you manage to hit
      that futex path on a writable page whose dirty bit has gone from the PTE,
      you'll livelock inside the kernel from what I can tell.
      
      It will go in a loop of trying the atomic access, failing, trying gup to
      "fix it up", getting succcess from gup, go back to the atomic access,
      failing again because dirty wasn't fixed etc...
      
      So I think you essentially hang in the kernel.
      
      The scenario is probably rare'ish because affected architecture are
      embedded and tend to not swap much (if at all) so we probably rarely hit
      the case where dirty is missing or young is missing, but I think Shan has
      a piece of SW that can reliably reproduce it using a shared writable
      mapping & fork or something like that.
      
      On archs who use SW tracking of dirty & young, a page without dirty is
      effectively mapped read-only and a page without young unaccessible in the
      PTE.
      
      Additionally, some architectures might lazily flush the TLB when relaxing
      write protection (by doing only a local flush), and expect a fault to
      invalidate the stale entry if it's still present on another processor.
      
      The futex code assumes that if the "in_atomic()" access -EFAULT's, it can
      "fix it up" by causing get_user_pages() which would then be equivalent to
      taking the fault.
      
      However that isn't the case.  get_user_pages() will not call
      handle_mm_fault() in the case where the PTE seems to have the right
      permissions, regardless of the dirty and young state.  It will eventually
      update those bits ...  in the struct page, but not in the PTE.
      
      Additionally, it will not handle the lazy TLB flushing that can be
      required by some architectures in the fault case.
      
      Basically, gup is the wrong interface for the job.  The patch provides a
      more appropriate one which boils down to just calling handle_mm_fault()
      since what we are trying to do is simulate a real page fault.
      
      The futex code currently attempts to write to user memory within a
      pagefault disabled section, and if that fails, tries to fix it up using
      get_user_pages().
      
      This doesn't work on archs where the dirty and young bits are maintained
      by software, since they will gate access permission in the TLB, and will
      not be updated by gup().
      
      In addition, there's an expectation on some archs that a spurious write
      fault triggers a local TLB flush, and that is missing from the picture as
      well.
      
      I decided that adding those "features" to gup() would be too much for this
      already too complex function, and instead added a new simpler
      fixup_user_fault() which is essentially a wrapper around handle_mm_fault()
      which the futex code can call.
      
      [akpm@linux-foundation.org: coding-style fixes]
      [akpm@linux-foundation.org: fix some nits Darren saw, fiddle comment layout]
      Signed-off-by: NBenjamin Herrenschmidt <benh@kernel.crashing.org>
      Reported-by: NShan Hai <haishan.bai@gmail.com>
      Tested-by: NShan Hai <haishan.bai@gmail.com>
      Cc: David Laight <David.Laight@ACULAB.COM>
      Acked-by: NPeter Zijlstra <a.p.zijlstra@chello.nl>
      Cc: Darren Hart <darren.hart@intel.com>
      Cc: <stable@kernel.org>
      Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
      2efaca92
  22. 08 7月, 2011 1 次提交
  23. 15 4月, 2011 1 次提交
  24. 25 3月, 2011 1 次提交
  25. 24 3月, 2011 1 次提交
  26. 15 3月, 2011 1 次提交
    • T
      futex: Deobfuscate handle_futex_death() · 6e0aa9f8
      Thomas Gleixner 提交于
      handle_futex_death() uses futex_atomic_cmpxchg_inatomic() without
      disabling page faults. That's ok, but totally non obvious.
      
      We don't hold locks so we actually can and want to fault here, because
      the get_user() before futex_atomic_cmpxchg_inatomic() does not
      guarantee a R/W mapping.
      
      We could just add a big fat comment to explain this, but actually
      changing the code so that the functionality is entirely clear is
      better.
      
      Use the helper function which disables page faults around the
      futex_atomic_cmpxchg_inatomic() and handle a fault with a call to
      fault_in_user_writeable() as all other places in the futex code do as
      well.
      Pointed-out-by: NLinus Torvalds <torvalds@linux-foundation.org>
      Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
      Acked-by: NDarren Hart <darren@dvhart.com>
      Cc: Michel Lespinasse <walken@google.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Russell King <linux@arm.linux.org.uk>
      Cc: David Howells <dhowells@redhat.com>
      Cc: Tony Luck <tony.luck@intel.com>
      Cc: Michal Simek <monstr@monstr.eu>
      Cc: Ralf Baechle <ralf@linux-mips.org>
      Cc: "James E.J. Bottomley" <jejb@parisc-linux.org>
      Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
      Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
      Cc: Paul Mundt <lethal@linux-sh.org>
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: Chris Metcalf <cmetcalf@tilera.com>
      LKML-Reference: <alpine.LFD.2.00.1103141126590.2787@localhost6.localdomain6>
      Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
      6e0aa9f8
  27. 12 3月, 2011 2 次提交
  28. 11 3月, 2011 1 次提交
    • M
      futex: Sanitize cmpxchg_futex_value_locked API · 37a9d912
      Michel Lespinasse 提交于
      The cmpxchg_futex_value_locked API was funny in that it returned either
      the original, user-exposed futex value OR an error code such as -EFAULT.
      This was confusing at best, and could be a source of livelocks in places
      that retry the cmpxchg_futex_value_locked after trying to fix the issue
      by running fault_in_user_writeable().
          
      This change makes the cmpxchg_futex_value_locked API more similar to the
      get_futex_value_locked one, returning an error code and updating the
      original value through a reference argument.
      Signed-off-by: NMichel Lespinasse <walken@google.com>
      Acked-by: Chris Metcalf <cmetcalf@tilera.com>  [tile]
      Acked-by: Tony Luck <tony.luck@intel.com>  [ia64]
      Acked-by: NThomas Gleixner <tglx@linutronix.de>
      Tested-by: Michal Simek <monstr@monstr.eu>  [microblaze]
      Acked-by: David Howells <dhowells@redhat.com> [frv]
      Cc: Darren Hart <darren@dvhart.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Matt Turner <mattst88@gmail.com>
      Cc: Russell King <linux@arm.linux.org.uk>
      Cc: Ralf Baechle <ralf@linux-mips.org>
      Cc: "James E.J. Bottomley" <jejb@parisc-linux.org>
      Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
      Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
      Cc: Paul Mundt <lethal@linux-sh.org>
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      LKML-Reference: <20110311024851.GC26122@google.com>
      Signed-off-by: NThomas Gleixner <tglx@linutronix.de>
      37a9d912