Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 
0003 #include <linux/sched/task.h>
0004 #include <linux/sched/signal.h>
0005 #include <linux/freezer.h>
0006 
0007 #include "futex.h"
0008 
0009 /*
0010  * READ this before attempting to hack on futexes!
0011  *
0012  * Basic futex operation and ordering guarantees
0013  * =============================================
0014  *
0015  * The waiter reads the futex value in user space and calls
0016  * futex_wait(). This function computes the hash bucket and acquires
0017  * the hash bucket lock. After that it reads the futex user space value
0018  * again and verifies that the data has not changed. If it has not changed
0019  * it enqueues itself into the hash bucket, releases the hash bucket lock
0020  * and schedules.
0021  *
0022  * The waker side modifies the user space value of the futex and calls
0023  * futex_wake(). This function computes the hash bucket and acquires the
0024  * hash bucket lock. Then it looks for waiters on that futex in the hash
0025  * bucket and wakes them.
0026  *
0027  * In futex wake up scenarios where no tasks are blocked on a futex, taking
0028  * the hb spinlock can be avoided and simply return. In order for this
0029  * optimization to work, ordering guarantees must exist so that the waiter
0030  * being added to the list is acknowledged when the list is concurrently being
0031  * checked by the waker, avoiding scenarios like the following:
0032  *
0033  * CPU 0                               CPU 1
0034  * val = *futex;
0035  * sys_futex(WAIT, futex, val);
0036  *   futex_wait(futex, val);
0037  *   uval = *futex;
0038  *                                     *futex = newval;
0039  *                                     sys_futex(WAKE, futex);
0040  *                                       futex_wake(futex);
0041  *                                       if (queue_empty())
0042  *                                         return;
0043  *   if (uval == val)
0044  *      lock(hash_bucket(futex));
0045  *      queue();
0046  *     unlock(hash_bucket(futex));
0047  *     schedule();
0048  *
0049  * This would cause the waiter on CPU 0 to wait forever because it
0050  * missed the transition of the user space value from val to newval
0051  * and the waker did not find the waiter in the hash bucket queue.
0052  *
0053  * The correct serialization ensures that a waiter either observes
0054  * the changed user space value before blocking or is woken by a
0055  * concurrent waker:
0056  *
0057  * CPU 0                                 CPU 1
0058  * val = *futex;
0059  * sys_futex(WAIT, futex, val);
0060  *   futex_wait(futex, val);
0061  *
0062  *   waiters++; (a)
0063  *   smp_mb(); (A) <-- paired with -.
0064  *                                  |
0065  *   lock(hash_bucket(futex));      |
0066  *                                  |
0067  *   uval = *futex;                 |
0068  *                                  |        *futex = newval;
0069  *                                  |        sys_futex(WAKE, futex);
0070  *                                  |          futex_wake(futex);
0071  *                                  |
0072  *                                  `--------> smp_mb(); (B)
0073  *   if (uval == val)
0074  *     queue();
0075  *     unlock(hash_bucket(futex));
0076  *     schedule();                         if (waiters)
0077  *                                           lock(hash_bucket(futex));
0078  *   else                                    wake_waiters(futex);
0079  *     waiters--; (b)                        unlock(hash_bucket(futex));
0080  *
0081  * Where (A) orders the waiters increment and the futex value read through
0082  * atomic operations (see futex_hb_waiters_inc) and where (B) orders the write
0083  * to futex and the waiters read (see futex_hb_waiters_pending()).
0084  *
0085  * This yields the following case (where X:=waiters, Y:=futex):
0086  *
0087  *  X = Y = 0
0088  *
0089  *  w[X]=1      w[Y]=1
0090  *  MB      MB
0091  *  r[Y]=y      r[X]=x
0092  *
0093  * Which guarantees that x==0 && y==0 is impossible; which translates back into
0094  * the guarantee that we cannot both miss the futex variable change and the
0095  * enqueue.
0096  *
0097  * Note that a new waiter is accounted for in (a) even when it is possible that
0098  * the wait call can return error, in which case we backtrack from it in (b).
0099  * Refer to the comment in futex_q_lock().
0100  *
0101  * Similarly, in order to account for waiters being requeued on another
0102  * address we always increment the waiters for the destination bucket before
0103  * acquiring the lock. It then decrements them again  after releasing it -
0104  * the code that actually moves the futex(es) between hash buckets (requeue_futex)
0105  * will do the additional required waiter count housekeeping. This is done for
0106  * double_lock_hb() and double_unlock_hb(), respectively.
0107  */
0108 
0109 /*
0110  * The hash bucket lock must be held when this is called.
0111  * Afterwards, the futex_q must not be accessed. Callers
0112  * must ensure to later call wake_up_q() for the actual
0113  * wakeups to occur.
0114  */
0115 void futex_wake_mark(struct wake_q_head *wake_q, struct futex_q *q)
0116 {
0117     struct task_struct *p = q->task;
0118 
0119     if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
0120         return;
0121 
0122     get_task_struct(p);
0123     __futex_unqueue(q);
0124     /*
0125      * The waiting task can free the futex_q as soon as q->lock_ptr = NULL
0126      * is written, without taking any locks. This is possible in the event
0127      * of a spurious wakeup, for example. A memory barrier is required here
0128      * to prevent the following store to lock_ptr from getting ahead of the
0129      * plist_del in __futex_unqueue().
0130      */
0131     smp_store_release(&q->lock_ptr, NULL);
0132 
0133     /*
0134      * Queue the task for later wakeup for after we've released
0135      * the hb->lock.
0136      */
0137     wake_q_add_safe(wake_q, p);
0138 }
0139 
0140 /*
0141  * Wake up waiters matching bitset queued on this futex (uaddr).
0142  */
0143 int futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
0144 {
0145     struct futex_hash_bucket *hb;
0146     struct futex_q *this, *next;
0147     union futex_key key = FUTEX_KEY_INIT;
0148     int ret;
0149     DEFINE_WAKE_Q(wake_q);
0150 
0151     if (!bitset)
0152         return -EINVAL;
0153 
0154     ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_READ);
0155     if (unlikely(ret != 0))
0156         return ret;
0157 
0158     hb = futex_hash(&key);
0159 
0160     /* Make sure we really have tasks to wakeup */
0161     if (!futex_hb_waiters_pending(hb))
0162         return ret;
0163 
0164     spin_lock(&hb->lock);
0165 
0166     plist_for_each_entry_safe(this, next, &hb->chain, list) {
0167         if (futex_match (&this->key, &key)) {
0168             if (this->pi_state || this->rt_waiter) {
0169                 ret = -EINVAL;
0170                 break;
0171             }
0172 
0173             /* Check if one of the bits is set in both bitsets */
0174             if (!(this->bitset & bitset))
0175                 continue;
0176 
0177             futex_wake_mark(&wake_q, this);
0178             if (++ret >= nr_wake)
0179                 break;
0180         }
0181     }
0182 
0183     spin_unlock(&hb->lock);
0184     wake_up_q(&wake_q);
0185     return ret;
0186 }
0187 
0188 static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr)
0189 {
0190     unsigned int op =     (encoded_op & 0x70000000) >> 28;
0191     unsigned int cmp =    (encoded_op & 0x0f000000) >> 24;
0192     int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11);
0193     int cmparg = sign_extend32(encoded_op & 0x00000fff, 11);
0194     int oldval, ret;
0195 
0196     if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) {
0197         if (oparg < 0 || oparg > 31) {
0198             char comm[sizeof(current->comm)];
0199             /*
0200              * kill this print and return -EINVAL when userspace
0201              * is sane again
0202              */
0203             pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n",
0204                     get_task_comm(comm, current), oparg);
0205             oparg &= 31;
0206         }
0207         oparg = 1 << oparg;
0208     }
0209 
0210     pagefault_disable();
0211     ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr);
0212     pagefault_enable();
0213     if (ret)
0214         return ret;
0215 
0216     switch (cmp) {
0217     case FUTEX_OP_CMP_EQ:
0218         return oldval == cmparg;
0219     case FUTEX_OP_CMP_NE:
0220         return oldval != cmparg;
0221     case FUTEX_OP_CMP_LT:
0222         return oldval < cmparg;
0223     case FUTEX_OP_CMP_GE:
0224         return oldval >= cmparg;
0225     case FUTEX_OP_CMP_LE:
0226         return oldval <= cmparg;
0227     case FUTEX_OP_CMP_GT:
0228         return oldval > cmparg;
0229     default:
0230         return -ENOSYS;
0231     }
0232 }
0233 
0234 /*
0235  * Wake up all waiters hashed on the physical page that is mapped
0236  * to this virtual address:
0237  */
0238 int futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
0239           int nr_wake, int nr_wake2, int op)
0240 {
0241     union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
0242     struct futex_hash_bucket *hb1, *hb2;
0243     struct futex_q *this, *next;
0244     int ret, op_ret;
0245     DEFINE_WAKE_Q(wake_q);
0246 
0247 retry:
0248     ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ);
0249     if (unlikely(ret != 0))
0250         return ret;
0251     ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE);
0252     if (unlikely(ret != 0))
0253         return ret;
0254 
0255     hb1 = futex_hash(&key1);
0256     hb2 = futex_hash(&key2);
0257 
0258 retry_private:
0259     double_lock_hb(hb1, hb2);
0260     op_ret = futex_atomic_op_inuser(op, uaddr2);
0261     if (unlikely(op_ret < 0)) {
0262         double_unlock_hb(hb1, hb2);
0263 
0264         if (!IS_ENABLED(CONFIG_MMU) ||
0265             unlikely(op_ret != -EFAULT && op_ret != -EAGAIN)) {
0266             /*
0267              * we don't get EFAULT from MMU faults if we don't have
0268              * an MMU, but we might get them from range checking
0269              */
0270             ret = op_ret;
0271             return ret;
0272         }
0273 
0274         if (op_ret == -EFAULT) {
0275             ret = fault_in_user_writeable(uaddr2);
0276             if (ret)
0277                 return ret;
0278         }
0279 
0280         cond_resched();
0281         if (!(flags & FLAGS_SHARED))
0282             goto retry_private;
0283         goto retry;
0284     }
0285 
0286     plist_for_each_entry_safe(this, next, &hb1->chain, list) {
0287         if (futex_match (&this->key, &key1)) {
0288             if (this->pi_state || this->rt_waiter) {
0289                 ret = -EINVAL;
0290                 goto out_unlock;
0291             }
0292             futex_wake_mark(&wake_q, this);
0293             if (++ret >= nr_wake)
0294                 break;
0295         }
0296     }
0297 
0298     if (op_ret > 0) {
0299         op_ret = 0;
0300         plist_for_each_entry_safe(this, next, &hb2->chain, list) {
0301             if (futex_match (&this->key, &key2)) {
0302                 if (this->pi_state || this->rt_waiter) {
0303                     ret = -EINVAL;
0304                     goto out_unlock;
0305                 }
0306                 futex_wake_mark(&wake_q, this);
0307                 if (++op_ret >= nr_wake2)
0308                     break;
0309             }
0310         }
0311         ret += op_ret;
0312     }
0313 
0314 out_unlock:
0315     double_unlock_hb(hb1, hb2);
0316     wake_up_q(&wake_q);
0317     return ret;
0318 }
0319 
0320 static long futex_wait_restart(struct restart_block *restart);
0321 
0322 /**
0323  * futex_wait_queue() - futex_queue() and wait for wakeup, timeout, or signal
0324  * @hb:     the futex hash bucket, must be locked by the caller
0325  * @q:      the futex_q to queue up on
0326  * @timeout:    the prepared hrtimer_sleeper, or null for no timeout
0327  */
0328 void futex_wait_queue(struct futex_hash_bucket *hb, struct futex_q *q,
0329                 struct hrtimer_sleeper *timeout)
0330 {
0331     /*
0332      * The task state is guaranteed to be set before another task can
0333      * wake it. set_current_state() is implemented using smp_store_mb() and
0334      * futex_queue() calls spin_unlock() upon completion, both serializing
0335      * access to the hash list and forcing another memory barrier.
0336      */
0337     set_current_state(TASK_INTERRUPTIBLE);
0338     futex_queue(q, hb);
0339 
0340     /* Arm the timer */
0341     if (timeout)
0342         hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS);
0343 
0344     /*
0345      * If we have been removed from the hash list, then another task
0346      * has tried to wake us, and we can skip the call to schedule().
0347      */
0348     if (likely(!plist_node_empty(&q->list))) {
0349         /*
0350          * If the timer has already expired, current will already be
0351          * flagged for rescheduling. Only call schedule if there
0352          * is no timeout, or if it has yet to expire.
0353          */
0354         if (!timeout || timeout->task)
0355             freezable_schedule();
0356     }
0357     __set_current_state(TASK_RUNNING);
0358 }
0359 
0360 /**
0361  * unqueue_multiple - Remove various futexes from their hash bucket
0362  * @v:     The list of futexes to unqueue
0363  * @count: Number of futexes in the list
0364  *
0365  * Helper to unqueue a list of futexes. This can't fail.
0366  *
0367  * Return:
0368  *  - >=0 - Index of the last futex that was awoken;
0369  *  - -1  - No futex was awoken
0370  */
0371 static int unqueue_multiple(struct futex_vector *v, int count)
0372 {
0373     int ret = -1, i;
0374 
0375     for (i = 0; i < count; i++) {
0376         if (!futex_unqueue(&v[i].q))
0377             ret = i;
0378     }
0379 
0380     return ret;
0381 }
0382 
0383 /**
0384  * futex_wait_multiple_setup - Prepare to wait and enqueue multiple futexes
0385  * @vs:     The futex list to wait on
0386  * @count:  The size of the list
0387  * @woken:  Index of the last woken futex, if any. Used to notify the
0388  *      caller that it can return this index to userspace (return parameter)
0389  *
0390  * Prepare multiple futexes in a single step and enqueue them. This may fail if
0391  * the futex list is invalid or if any futex was already awoken. On success the
0392  * task is ready to interruptible sleep.
0393  *
0394  * Return:
0395  *  -  1 - One of the futexes was woken by another thread
0396  *  -  0 - Success
0397  *  - <0 - -EFAULT, -EWOULDBLOCK or -EINVAL
0398  */
0399 static int futex_wait_multiple_setup(struct futex_vector *vs, int count, int *woken)
0400 {
0401     struct futex_hash_bucket *hb;
0402     bool retry = false;
0403     int ret, i;
0404     u32 uval;
0405 
0406     /*
0407      * Enqueuing multiple futexes is tricky, because we need to enqueue
0408      * each futex on the list before dealing with the next one to avoid
0409      * deadlocking on the hash bucket. But, before enqueuing, we need to
0410      * make sure that current->state is TASK_INTERRUPTIBLE, so we don't
0411      * lose any wake events, which cannot be done before the get_futex_key
0412      * of the next key, because it calls get_user_pages, which can sleep.
0413      * Thus, we fetch the list of futexes keys in two steps, by first
0414      * pinning all the memory keys in the futex key, and only then we read
0415      * each key and queue the corresponding futex.
0416      *
0417      * Private futexes doesn't need to recalculate hash in retry, so skip
0418      * get_futex_key() when retrying.
0419      */
0420 retry:
0421     for (i = 0; i < count; i++) {
0422         if ((vs[i].w.flags & FUTEX_PRIVATE_FLAG) && retry)
0423             continue;
0424 
0425         ret = get_futex_key(u64_to_user_ptr(vs[i].w.uaddr),
0426                     !(vs[i].w.flags & FUTEX_PRIVATE_FLAG),
0427                     &vs[i].q.key, FUTEX_READ);
0428 
0429         if (unlikely(ret))
0430             return ret;
0431     }
0432 
0433     set_current_state(TASK_INTERRUPTIBLE);
0434 
0435     for (i = 0; i < count; i++) {
0436         u32 __user *uaddr = (u32 __user *)(unsigned long)vs[i].w.uaddr;
0437         struct futex_q *q = &vs[i].q;
0438         u32 val = (u32)vs[i].w.val;
0439 
0440         hb = futex_q_lock(q);
0441         ret = futex_get_value_locked(&uval, uaddr);
0442 
0443         if (!ret && uval == val) {
0444             /*
0445              * The bucket lock can't be held while dealing with the
0446              * next futex. Queue each futex at this moment so hb can
0447              * be unlocked.
0448              */
0449             futex_queue(q, hb);
0450             continue;
0451         }
0452 
0453         futex_q_unlock(hb);
0454         __set_current_state(TASK_RUNNING);
0455 
0456         /*
0457          * Even if something went wrong, if we find out that a futex
0458          * was woken, we don't return error and return this index to
0459          * userspace
0460          */
0461         *woken = unqueue_multiple(vs, i);
0462         if (*woken >= 0)
0463             return 1;
0464 
0465         if (ret) {
0466             /*
0467              * If we need to handle a page fault, we need to do so
0468              * without any lock and any enqueued futex (otherwise
0469              * we could lose some wakeup). So we do it here, after
0470              * undoing all the work done so far. In success, we
0471              * retry all the work.
0472              */
0473             if (get_user(uval, uaddr))
0474                 return -EFAULT;
0475 
0476             retry = true;
0477             goto retry;
0478         }
0479 
0480         if (uval != val)
0481             return -EWOULDBLOCK;
0482     }
0483 
0484     return 0;
0485 }
0486 
0487 /**
0488  * futex_sleep_multiple - Check sleeping conditions and sleep
0489  * @vs:    List of futexes to wait for
0490  * @count: Length of vs
0491  * @to:    Timeout
0492  *
0493  * Sleep if and only if the timeout hasn't expired and no futex on the list has
0494  * been woken up.
0495  */
0496 static void futex_sleep_multiple(struct futex_vector *vs, unsigned int count,
0497                  struct hrtimer_sleeper *to)
0498 {
0499     if (to && !to->task)
0500         return;
0501 
0502     for (; count; count--, vs++) {
0503         if (!READ_ONCE(vs->q.lock_ptr))
0504             return;
0505     }
0506 
0507     freezable_schedule();
0508 }
0509 
0510 /**
0511  * futex_wait_multiple - Prepare to wait on and enqueue several futexes
0512  * @vs:     The list of futexes to wait on
0513  * @count:  The number of objects
0514  * @to:     Timeout before giving up and returning to userspace
0515  *
0516  * Entry point for the FUTEX_WAIT_MULTIPLE futex operation, this function
0517  * sleeps on a group of futexes and returns on the first futex that is
0518  * wake, or after the timeout has elapsed.
0519  *
0520  * Return:
0521  *  - >=0 - Hint to the futex that was awoken
0522  *  - <0  - On error
0523  */
0524 int futex_wait_multiple(struct futex_vector *vs, unsigned int count,
0525             struct hrtimer_sleeper *to)
0526 {
0527     int ret, hint = 0;
0528 
0529     if (to)
0530         hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS);
0531 
0532     while (1) {
0533         ret = futex_wait_multiple_setup(vs, count, &hint);
0534         if (ret) {
0535             if (ret > 0) {
0536                 /* A futex was woken during setup */
0537                 ret = hint;
0538             }
0539             return ret;
0540         }
0541 
0542         futex_sleep_multiple(vs, count, to);
0543 
0544         __set_current_state(TASK_RUNNING);
0545 
0546         ret = unqueue_multiple(vs, count);
0547         if (ret >= 0)
0548             return ret;
0549 
0550         if (to && !to->task)
0551             return -ETIMEDOUT;
0552         else if (signal_pending(current))
0553             return -ERESTARTSYS;
0554         /*
0555          * The final case is a spurious wakeup, for
0556          * which just retry.
0557          */
0558     }
0559 }
0560 
0561 /**
0562  * futex_wait_setup() - Prepare to wait on a futex
0563  * @uaddr:  the futex userspace address
0564  * @val:    the expected value
0565  * @flags:  futex flags (FLAGS_SHARED, etc.)
0566  * @q:      the associated futex_q
0567  * @hb:     storage for hash_bucket pointer to be returned to caller
0568  *
0569  * Setup the futex_q and locate the hash_bucket.  Get the futex value and
0570  * compare it with the expected value.  Handle atomic faults internally.
0571  * Return with the hb lock held on success, and unlocked on failure.
0572  *
0573  * Return:
0574  *  -  0 - uaddr contains val and hb has been locked;
0575  *  - <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
0576  */
0577 int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
0578              struct futex_q *q, struct futex_hash_bucket **hb)
0579 {
0580     u32 uval;
0581     int ret;
0582 
0583     /*
0584      * Access the page AFTER the hash-bucket is locked.
0585      * Order is important:
0586      *
0587      *   Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
0588      *   Userspace waker:  if (cond(var)) { var = new; futex_wake(&var); }
0589      *
0590      * The basic logical guarantee of a futex is that it blocks ONLY
0591      * if cond(var) is known to be true at the time of blocking, for
0592      * any cond.  If we locked the hash-bucket after testing *uaddr, that
0593      * would open a race condition where we could block indefinitely with
0594      * cond(var) false, which would violate the guarantee.
0595      *
0596      * On the other hand, we insert q and release the hash-bucket only
0597      * after testing *uaddr.  This guarantees that futex_wait() will NOT
0598      * absorb a wakeup if *uaddr does not match the desired values
0599      * while the syscall executes.
0600      */
0601 retry:
0602     ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, FUTEX_READ);
0603     if (unlikely(ret != 0))
0604         return ret;
0605 
0606 retry_private:
0607     *hb = futex_q_lock(q);
0608 
0609     ret = futex_get_value_locked(&uval, uaddr);
0610 
0611     if (ret) {
0612         futex_q_unlock(*hb);
0613 
0614         ret = get_user(uval, uaddr);
0615         if (ret)
0616             return ret;
0617 
0618         if (!(flags & FLAGS_SHARED))
0619             goto retry_private;
0620 
0621         goto retry;
0622     }
0623 
0624     if (uval != val) {
0625         futex_q_unlock(*hb);
0626         ret = -EWOULDBLOCK;
0627     }
0628 
0629     return ret;
0630 }
0631 
0632 int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val, ktime_t *abs_time, u32 bitset)
0633 {
0634     struct hrtimer_sleeper timeout, *to;
0635     struct restart_block *restart;
0636     struct futex_hash_bucket *hb;
0637     struct futex_q q = futex_q_init;
0638     int ret;
0639 
0640     if (!bitset)
0641         return -EINVAL;
0642     q.bitset = bitset;
0643 
0644     to = futex_setup_timer(abs_time, &timeout, flags,
0645                    current->timer_slack_ns);
0646 retry:
0647     /*
0648      * Prepare to wait on uaddr. On success, it holds hb->lock and q
0649      * is initialized.
0650      */
0651     ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
0652     if (ret)
0653         goto out;
0654 
0655     /* futex_queue and wait for wakeup, timeout, or a signal. */
0656     futex_wait_queue(hb, &q, to);
0657 
0658     /* If we were woken (and unqueued), we succeeded, whatever. */
0659     ret = 0;
0660     if (!futex_unqueue(&q))
0661         goto out;
0662     ret = -ETIMEDOUT;
0663     if (to && !to->task)
0664         goto out;
0665 
0666     /*
0667      * We expect signal_pending(current), but we might be the
0668      * victim of a spurious wakeup as well.
0669      */
0670     if (!signal_pending(current))
0671         goto retry;
0672 
0673     ret = -ERESTARTSYS;
0674     if (!abs_time)
0675         goto out;
0676 
0677     restart = &current->restart_block;
0678     restart->futex.uaddr = uaddr;
0679     restart->futex.val = val;
0680     restart->futex.time = *abs_time;
0681     restart->futex.bitset = bitset;
0682     restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
0683 
0684     ret = set_restart_fn(restart, futex_wait_restart);
0685 
0686 out:
0687     if (to) {
0688         hrtimer_cancel(&to->timer);
0689         destroy_hrtimer_on_stack(&to->timer);
0690     }
0691     return ret;
0692 }
0693 
0694 static long futex_wait_restart(struct restart_block *restart)
0695 {
0696     u32 __user *uaddr = restart->futex.uaddr;
0697     ktime_t t, *tp = NULL;
0698 
0699     if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
0700         t = restart->futex.time;
0701         tp = &t;
0702     }
0703     restart->fn = do_no_restart_syscall;
0704 
0705     return (long)futex_wait(uaddr, restart->futex.flags,
0706                 restart->futex.val, tp, restart->futex.bitset);
0707 }
0708