linux/kernel/posix-cpu-timers.c
<<
>>
Prefs
   1/*
   2 * Implement CPU time clocks for the POSIX clock interface.
   3 */
   4
   5#include <linux/sched.h>
   6#include <linux/posix-timers.h>
   7#include <asm/uaccess.h>
   8#include <linux/errno.h>
   9
  10static int check_clock(const clockid_t which_clock)
  11{
  12        int error = 0;
  13        struct task_struct *p;
  14        const pid_t pid = CPUCLOCK_PID(which_clock);
  15
  16        if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
  17                return -EINVAL;
  18
  19        if (pid == 0)
  20                return 0;
  21
  22        read_lock(&tasklist_lock);
  23        p = find_task_by_pid(pid);
  24        if (!p || (CPUCLOCK_PERTHREAD(which_clock) ?
  25                   p->tgid != current->tgid : p->tgid != pid)) {
  26                error = -EINVAL;
  27        }
  28        read_unlock(&tasklist_lock);
  29
  30        return error;
  31}
  32
  33static inline union cpu_time_count
  34timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
  35{
  36        union cpu_time_count ret;
  37        ret.sched = 0;          /* high half always zero when .cpu used */
  38        if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  39                ret.sched = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
  40        } else {
  41                ret.cpu = timespec_to_cputime(tp);
  42        }
  43        return ret;
  44}
  45
  46static void sample_to_timespec(const clockid_t which_clock,
  47                               union cpu_time_count cpu,
  48                               struct timespec *tp)
  49{
  50        if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  51                tp->tv_sec = div_long_long_rem(cpu.sched,
  52                                               NSEC_PER_SEC, &tp->tv_nsec);
  53        } else {
  54                cputime_to_timespec(cpu.cpu, tp);
  55        }
  56}
  57
  58static inline int cpu_time_before(const clockid_t which_clock,
  59                                  union cpu_time_count now,
  60                                  union cpu_time_count then)
  61{
  62        if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  63                return now.sched < then.sched;
  64        }  else {
  65                return cputime_lt(now.cpu, then.cpu);
  66        }
  67}
  68static inline void cpu_time_add(const clockid_t which_clock,
  69                                union cpu_time_count *acc,
  70                                union cpu_time_count val)
  71{
  72        if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  73                acc->sched += val.sched;
  74        }  else {
  75                acc->cpu = cputime_add(acc->cpu, val.cpu);
  76        }
  77}
  78static inline union cpu_time_count cpu_time_sub(const clockid_t which_clock,
  79                                                union cpu_time_count a,
  80                                                union cpu_time_count b)
  81{
  82        if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
  83                a.sched -= b.sched;
  84        }  else {
  85                a.cpu = cputime_sub(a.cpu, b.cpu);
  86        }
  87        return a;
  88}
  89
  90/*
  91 * Divide and limit the result to res >= 1
  92 *
  93 * This is necessary to prevent signal delivery starvation, when the result of
  94 * the division would be rounded down to 0.
  95 */
  96static inline cputime_t cputime_div_non_zero(cputime_t time, unsigned long div)
  97{
  98        cputime_t res = cputime_div(time, div);
  99
 100        return max_t(cputime_t, res, 1);
 101}
 102
 103/*
 104 * Update expiry time from increment, and increase overrun count,
 105 * given the current clock sample.
 106 */
 107static void bump_cpu_timer(struct k_itimer *timer,
 108                                  union cpu_time_count now)
 109{
 110        int i;
 111
 112        if (timer->it.cpu.incr.sched == 0)
 113                return;
 114
 115        if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
 116                unsigned long long delta, incr;
 117
 118                if (now.sched < timer->it.cpu.expires.sched)
 119                        return;
 120                incr = timer->it.cpu.incr.sched;
 121                delta = now.sched + incr - timer->it.cpu.expires.sched;
 122                /* Don't use (incr*2 < delta), incr*2 might overflow. */
 123                for (i = 0; incr < delta - incr; i++)
 124                        incr = incr << 1;
 125                for (; i >= 0; incr >>= 1, i--) {
 126                        if (delta < incr)
 127                                continue;
 128                        timer->it.cpu.expires.sched += incr;
 129                        timer->it_overrun += 1 << i;
 130                        delta -= incr;
 131                }
 132        } else {
 133                cputime_t delta, incr;
 134
 135                if (cputime_lt(now.cpu, timer->it.cpu.expires.cpu))
 136                        return;
 137                incr = timer->it.cpu.incr.cpu;
 138                delta = cputime_sub(cputime_add(now.cpu, incr),
 139                                    timer->it.cpu.expires.cpu);
 140                /* Don't use (incr*2 < delta), incr*2 might overflow. */
 141                for (i = 0; cputime_lt(incr, cputime_sub(delta, incr)); i++)
 142                             incr = cputime_add(incr, incr);
 143                for (; i >= 0; incr = cputime_halve(incr), i--) {
 144                        if (cputime_lt(delta, incr))
 145                                continue;
 146                        timer->it.cpu.expires.cpu =
 147                                cputime_add(timer->it.cpu.expires.cpu, incr);
 148                        timer->it_overrun += 1 << i;
 149                        delta = cputime_sub(delta, incr);
 150                }
 151        }
 152}
 153
 154static inline cputime_t prof_ticks(struct task_struct *p)
 155{
 156        return cputime_add(p->utime, p->stime);
 157}
 158static inline cputime_t virt_ticks(struct task_struct *p)
 159{
 160        return p->utime;
 161}
 162static inline unsigned long long sched_ns(struct task_struct *p)
 163{
 164        return (p == current) ? current_sched_time(p) : p->sched_time;
 165}
 166
 167int posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
 168{
 169        int error = check_clock(which_clock);
 170        if (!error) {
 171                tp->tv_sec = 0;
 172                tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
 173                if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
 174                        /*
 175                         * If sched_clock is using a cycle counter, we
 176                         * don't have any idea of its true resolution
 177                         * exported, but it is much more than 1s/HZ.
 178                         */
 179                        tp->tv_nsec = 1;
 180                }
 181        }
 182        return error;
 183}
 184
 185int posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
 186{
 187        /*
 188         * You can never reset a CPU clock, but we check for other errors
 189         * in the call before failing with EPERM.
 190         */
 191        int error = check_clock(which_clock);
 192        if (error == 0) {
 193                error = -EPERM;
 194        }
 195        return error;
 196}
 197
 198
 199/*
 200 * Sample a per-thread clock for the given task.
 201 */
 202static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
 203                            union cpu_time_count *cpu)
 204{
 205        switch (CPUCLOCK_WHICH(which_clock)) {
 206        default:
 207                return -EINVAL;
 208        case CPUCLOCK_PROF:
 209                cpu->cpu = prof_ticks(p);
 210                break;
 211        case CPUCLOCK_VIRT:
 212                cpu->cpu = virt_ticks(p);
 213                break;
 214        case CPUCLOCK_SCHED:
 215                cpu->sched = sched_ns(p);
 216                break;
 217        }
 218        return 0;
 219}
 220
 221/*
 222 * Sample a process (thread group) clock for the given group_leader task.
 223 * Must be called with tasklist_lock held for reading.
 224 * Must be called with tasklist_lock held for reading, and p->sighand->siglock.
 225 */
 226static int cpu_clock_sample_group_locked(unsigned int clock_idx,
 227                                         struct task_struct *p,
 228                                         union cpu_time_count *cpu)
 229{
 230        struct task_struct *t = p;
 231        switch (clock_idx) {
 232        default:
 233                return -EINVAL;
 234        case CPUCLOCK_PROF:
 235                cpu->cpu = cputime_add(p->signal->utime, p->signal->stime);
 236                do {
 237                        cpu->cpu = cputime_add(cpu->cpu, prof_ticks(t));
 238                        t = next_thread(t);
 239                } while (t != p);
 240                break;
 241        case CPUCLOCK_VIRT:
 242                cpu->cpu = p->signal->utime;
 243                do {
 244                        cpu->cpu = cputime_add(cpu->cpu, virt_ticks(t));
 245                        t = next_thread(t);
 246                } while (t != p);
 247                break;
 248        case CPUCLOCK_SCHED:
 249                cpu->sched = p->signal->sched_time;
 250                /* Add in each other live thread.  */
 251                while ((t = next_thread(t)) != p) {
 252                        cpu->sched += t->sched_time;
 253                }
 254                cpu->sched += sched_ns(p);
 255                break;
 256        }
 257        return 0;
 258}
 259
 260/*
 261 * Sample a process (thread group) clock for the given group_leader task.
 262 * Must be called with tasklist_lock held for reading.
 263 */
 264static int cpu_clock_sample_group(const clockid_t which_clock,
 265                                  struct task_struct *p,
 266                                  union cpu_time_count *cpu)
 267{
 268        int ret;
 269        unsigned long flags;
 270        spin_lock_irqsave(&p->sighand->siglock, flags);
 271        ret = cpu_clock_sample_group_locked(CPUCLOCK_WHICH(which_clock), p,
 272                                            cpu);
 273        spin_unlock_irqrestore(&p->sighand->siglock, flags);
 274        return ret;
 275}
 276
 277
 278int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
 279{
 280        const pid_t pid = CPUCLOCK_PID(which_clock);
 281        int error = -EINVAL;
 282        union cpu_time_count rtn;
 283
 284        if (pid == 0) {
 285                /*
 286                 * Special case constant value for our own clocks.
 287                 * We don't have to do any lookup to find ourselves.
 288                 */
 289                if (CPUCLOCK_PERTHREAD(which_clock)) {
 290                        /*
 291                         * Sampling just ourselves we can do with no locking.
 292                         */
 293                        error = cpu_clock_sample(which_clock,
 294                                                 current, &rtn);
 295                } else {
 296                        read_lock(&tasklist_lock);
 297                        error = cpu_clock_sample_group(which_clock,
 298                                                       current, &rtn);
 299                        read_unlock(&tasklist_lock);
 300                }
 301        } else {
 302                /*
 303                 * Find the given PID, and validate that the caller
 304                 * should be able to see it.
 305                 */
 306                struct task_struct *p;
 307                read_lock(&tasklist_lock);
 308                p = find_task_by_pid(pid);
 309                if (p) {
 310                        if (CPUCLOCK_PERTHREAD(which_clock)) {
 311                                if (p->tgid == current->tgid) {
 312                                        error = cpu_clock_sample(which_clock,
 313                                                                 p, &rtn);
 314                                }
 315                        } else if (p->tgid == pid && p->signal) {
 316                                error = cpu_clock_sample_group(which_clock,
 317                                                               p, &rtn);
 318                        }
 319                }
 320                read_unlock(&tasklist_lock);
 321        }
 322
 323        if (error)
 324                return error;
 325        sample_to_timespec(which_clock, rtn, tp);
 326        return 0;
 327}
 328
 329
 330/*
 331 * Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
 332 * This is called from sys_timer_create with the new timer already locked.
 333 */
 334int posix_cpu_timer_create(struct k_itimer *new_timer)
 335{
 336        int ret = 0;
 337        const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
 338        struct task_struct *p;
 339
 340        if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
 341                return -EINVAL;
 342
 343        INIT_LIST_HEAD(&new_timer->it.cpu.entry);
 344        new_timer->it.cpu.incr.sched = 0;
 345        new_timer->it.cpu.expires.sched = 0;
 346
 347        read_lock(&tasklist_lock);
 348        if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
 349                if (pid == 0) {
 350                        p = current;
 351                } else {
 352                        p = find_task_by_pid(pid);
 353                        if (p && p->tgid != current->tgid)
 354                                p = NULL;
 355                }
 356        } else {
 357                if (pid == 0) {
 358                        p = current->group_leader;
 359                } else {
 360                        p = find_task_by_pid(pid);
 361                        if (p && p->tgid != pid)
 362                                p = NULL;
 363                }
 364        }
 365        new_timer->it.cpu.task = p;
 366        if (p) {
 367                get_task_struct(p);
 368        } else {
 369                ret = -EINVAL;
 370        }
 371        read_unlock(&tasklist_lock);
 372
 373        return ret;
 374}
 375
 376/*
 377 * Clean up a CPU-clock timer that is about to be destroyed.
 378 * This is called from timer deletion with the timer already locked.
 379 * If we return TIMER_RETRY, it's necessary to release the timer's lock
 380 * and try again.  (This happens when the timer is in the middle of firing.)
 381 */
 382int posix_cpu_timer_del(struct k_itimer *timer)
 383{
 384        struct task_struct *p = timer->it.cpu.task;
 385        int ret = 0;
 386
 387        if (likely(p != NULL)) {
 388                read_lock(&tasklist_lock);
 389                if (unlikely(p->signal == NULL)) {
 390                        /*
 391                         * We raced with the reaping of the task.
 392                         * The deletion should have cleared us off the list.
 393                         */
 394                        BUG_ON(!list_empty(&timer->it.cpu.entry));
 395                } else {
 396                        spin_lock(&p->sighand->siglock);
 397                        if (timer->it.cpu.firing)
 398                                ret = TIMER_RETRY;
 399                        else
 400                                list_del(&timer->it.cpu.entry);
 401                        spin_unlock(&p->sighand->siglock);
 402                }
 403                read_unlock(&tasklist_lock);
 404
 405                if (!ret)
 406                        put_task_struct(p);
 407        }
 408
 409        return ret;
 410}
 411
 412/*
 413 * Clean out CPU timers still ticking when a thread exited.  The task
 414 * pointer is cleared, and the expiry time is replaced with the residual
 415 * time for later timer_gettime calls to return.
 416 * This must be called with the siglock held.
 417 */
 418static void cleanup_timers(struct list_head *head,
 419                           cputime_t utime, cputime_t stime,
 420                           unsigned long long sched_time)
 421{
 422        struct cpu_timer_list *timer, *next;
 423        cputime_t ptime = cputime_add(utime, stime);
 424
 425        list_for_each_entry_safe(timer, next, head, entry) {
 426                list_del_init(&timer->entry);
 427                if (cputime_lt(timer->expires.cpu, ptime)) {
 428                        timer->expires.cpu = cputime_zero;
 429                } else {
 430                        timer->expires.cpu = cputime_sub(timer->expires.cpu,
 431                                                         ptime);
 432                }
 433        }
 434
 435        ++head;
 436        list_for_each_entry_safe(timer, next, head, entry) {
 437                list_del_init(&timer->entry);
 438                if (cputime_lt(timer->expires.cpu, utime)) {
 439                        timer->expires.cpu = cputime_zero;
 440                } else {
 441                        timer->expires.cpu = cputime_sub(timer->expires.cpu,
 442                                                         utime);
 443                }
 444        }
 445
 446        ++head;
 447        list_for_each_entry_safe(timer, next, head, entry) {
 448                list_del_init(&timer->entry);
 449                if (timer->expires.sched < sched_time) {
 450                        timer->expires.sched = 0;
 451                } else {
 452                        timer->expires.sched -= sched_time;
 453                }
 454        }
 455}
 456
 457/*
 458 * These are both called with the siglock held, when the current thread
 459 * is being reaped.  When the final (leader) thread in the group is reaped,
 460 * posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
 461 */
 462void posix_cpu_timers_exit(struct task_struct *tsk)
 463{
 464        cleanup_timers(tsk->cpu_timers,
 465                       tsk->utime, tsk->stime, tsk->sched_time);
 466
 467}
 468void posix_cpu_timers_exit_group(struct task_struct *tsk)
 469{
 470        cleanup_timers(tsk->signal->cpu_timers,
 471                       cputime_add(tsk->utime, tsk->signal->utime),
 472                       cputime_add(tsk->stime, tsk->signal->stime),
 473                       tsk->sched_time + tsk->signal->sched_time);
 474}
 475
 476
 477/*
 478 * Set the expiry times of all the threads in the process so one of them
 479 * will go off before the process cumulative expiry total is reached.
 480 */
 481static void process_timer_rebalance(struct task_struct *p,
 482                                    unsigned int clock_idx,
 483                                    union cpu_time_count expires,
 484                                    union cpu_time_count val)
 485{
 486        cputime_t ticks, left;
 487        unsigned long long ns, nsleft;
 488        struct task_struct *t = p;
 489        unsigned int nthreads = atomic_read(&p->signal->live);
 490
 491        if (!nthreads)
 492                return;
 493
 494        switch (clock_idx) {
 495        default:
 496                BUG();
 497                break;
 498        case CPUCLOCK_PROF:
 499                left = cputime_div_non_zero(cputime_sub(expires.cpu, val.cpu),
 500                                       nthreads);
 501                do {
 502                        if (likely(!(t->flags & PF_EXITING))) {
 503                                ticks = cputime_add(prof_ticks(t), left);
 504                                if (cputime_eq(t->it_prof_expires,
 505                                               cputime_zero) ||
 506                                    cputime_gt(t->it_prof_expires, ticks)) {
 507                                        t->it_prof_expires = ticks;
 508                                }
 509                        }
 510                        t = next_thread(t);
 511                } while (t != p);
 512                break;
 513        case CPUCLOCK_VIRT:
 514                left = cputime_div_non_zero(cputime_sub(expires.cpu, val.cpu),
 515                                       nthreads);
 516                do {
 517                        if (likely(!(t->flags & PF_EXITING))) {
 518                                ticks = cputime_add(virt_ticks(t), left);
 519                                if (cputime_eq(t->it_virt_expires,
 520                                               cputime_zero) ||
 521                                    cputime_gt(t->it_virt_expires, ticks)) {
 522                                        t->it_virt_expires = ticks;
 523                                }
 524                        }
 525                        t = next_thread(t);
 526                } while (t != p);
 527                break;
 528        case CPUCLOCK_SCHED:
 529                nsleft = expires.sched - val.sched;
 530                do_div(nsleft, nthreads);
 531                nsleft = max_t(unsigned long long, nsleft, 1);
 532                do {
 533                        if (likely(!(t->flags & PF_EXITING))) {
 534                                ns = t->sched_time + nsleft;
 535                                if (t->it_sched_expires == 0 ||
 536                                    t->it_sched_expires > ns) {
 537                                        t->it_sched_expires = ns;
 538                                }
 539                        }
 540                        t = next_thread(t);
 541                } while (t != p);
 542                break;
 543        }
 544}
 545
 546static void clear_dead_task(struct k_itimer *timer, union cpu_time_count now)
 547{
 548        /*
 549         * That's all for this thread or process.
 550         * We leave our residual in expires to be reported.
 551         */
 552        put_task_struct(timer->it.cpu.task);
 553        timer->it.cpu.task = NULL;
 554        timer->it.cpu.expires = cpu_time_sub(timer->it_clock,
 555                                             timer->it.cpu.expires,
 556                                             now);
 557}
 558
 559/*
 560 * Insert the timer on the appropriate list before any timers that
 561 * expire later.  This must be called with the tasklist_lock held
 562 * for reading, and interrupts disabled.
 563 */
 564static void arm_timer(struct k_itimer *timer, union cpu_time_count now)
 565{
 566        struct task_struct *p = timer->it.cpu.task;
 567        struct list_head *head, *listpos;
 568        struct cpu_timer_list *const nt = &timer->it.cpu;
 569        struct cpu_timer_list *next;
 570        unsigned long i;
 571
 572        head = (CPUCLOCK_PERTHREAD(timer->it_clock) ?
 573                p->cpu_timers : p->signal->cpu_timers);
 574        head += CPUCLOCK_WHICH(timer->it_clock);
 575
 576        BUG_ON(!irqs_disabled());
 577        spin_lock(&p->sighand->siglock);
 578
 579        listpos = head;
 580        if (CPUCLOCK_WHICH(timer->it_clock) == CPUCLOCK_SCHED) {
 581                list_for_each_entry(next, head, entry) {
 582                        if (next->expires.sched > nt->expires.sched)
 583                                break;
 584                        listpos = &next->entry;
 585                }
 586        } else {
 587                list_for_each_entry(next, head, entry) {
 588                        if (cputime_gt(next->expires.cpu, nt->expires.cpu))
 589                                break;
 590                        listpos = &next->entry;
 591                }
 592        }
 593        list_add(&nt->entry, listpos);
 594
 595        if (listpos == head) {
 596                /*
 597                 * We are the new earliest-expiring timer.
 598                 * If we are a thread timer, there can always
 599                 * be a process timer telling us to stop earlier.
 600                 */
 601
 602                if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
 603                        switch (CPUCLOCK_WHICH(timer->it_clock)) {
 604                        default:
 605                                BUG();
 606                        case CPUCLOCK_PROF:
 607                                if (cputime_eq(p->it_prof_expires,
 608                                               cputime_zero) ||
 609                                    cputime_gt(p->it_prof_expires,
 610                                               nt->expires.cpu))
 611                                        p->it_prof_expires = nt->expires.cpu;
 612                                break;
 613                        case CPUCLOCK_VIRT:
 614                                if (cputime_eq(p->it_virt_expires,
 615                                               cputime_zero) ||
 616                                    cputime_gt(p->it_virt_expires,
 617                                               nt->expires.cpu))
 618                                        p->it_virt_expires = nt->expires.cpu;
 619                                break;
 620                        case CPUCLOCK_SCHED:
 621                                if (p->it_sched_expires == 0 ||
 622                                    p->it_sched_expires > nt->expires.sched)
 623                                        p->it_sched_expires = nt->expires.sched;
 624                                break;
 625                        }
 626                } else {
 627                        /*
 628                         * For a process timer, we must balance
 629                         * all the live threads' expirations.
 630                         */
 631                        switch (CPUCLOCK_WHICH(timer->it_clock)) {
 632                        default:
 633                                BUG();
 634                        case CPUCLOCK_VIRT:
 635                                if (!cputime_eq(p->signal->it_virt_expires,
 636                                                cputime_zero) &&
 637                                    cputime_lt(p->signal->it_virt_expires,
 638                                               timer->it.cpu.expires.cpu))
 639                                        break;
 640                                goto rebalance;
 641                        case CPUCLOCK_PROF:
 642                                if (!cputime_eq(p->signal->it_prof_expires,
 643                                                cputime_zero) &&
 644                                    cputime_lt(p->signal->it_prof_expires,
 645                                               timer->it.cpu.expires.cpu))
 646                                        break;
 647                                i = p->signal->rlim[RLIMIT_CPU].rlim_cur;
 648                                if (i != RLIM_INFINITY &&
 649                                    i <= cputime_to_secs(timer->it.cpu.expires.cpu))
 650                                        break;
 651                                goto rebalance;
 652                        case CPUCLOCK_SCHED:
 653                        rebalance:
 654                                process_timer_rebalance(
 655                                        timer->it.cpu.task,
 656                                        CPUCLOCK_WHICH(timer->it_clock),
 657                                        timer->it.cpu.expires, now);
 658                                break;
 659                        }
 660                }
 661        }
 662
 663        spin_unlock(&p->sighand->siglock);
 664}
 665
 666/*
 667 * The timer is locked, fire it and arrange for its reload.
 668 */
 669static void cpu_timer_fire(struct k_itimer *timer)
 670{
 671        if (unlikely(timer->sigq == NULL)) {
 672                /*
 673                 * This a special case for clock_nanosleep,
 674                 * not a normal timer from sys_timer_create.
 675                 */
 676                wake_up_process(timer->it_process);
 677                timer->it.cpu.expires.sched = 0;
 678        } else if (timer->it.cpu.incr.sched == 0) {
 679                /*
 680                 * One-shot timer.  Clear it as soon as it's fired.
 681                 */
 682                posix_timer_event(timer, 0);
 683                timer->it.cpu.expires.sched = 0;
 684        } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
 685                /*
 686                 * The signal did not get queued because the signal
 687                 * was ignored, so we won't get any callback to
 688                 * reload the timer.  But we need to keep it
 689                 * ticking in case the signal is deliverable next time.
 690                 */
 691                posix_cpu_timer_schedule(timer);
 692        }
 693}
 694
 695/*
 696 * Guts of sys_timer_settime for CPU timers.
 697 * This is called with the timer locked and interrupts disabled.
 698 * If we return TIMER_RETRY, it's necessary to release the timer's lock
 699 * and try again.  (This happens when the timer is in the middle of firing.)
 700 */
 701int posix_cpu_timer_set(struct k_itimer *timer, int flags,
 702                        struct itimerspec *new, struct itimerspec *old)
 703{
 704        struct task_struct *p = timer->it.cpu.task;
 705        union cpu_time_count old_expires, new_expires, val;
 706        int ret;
 707
 708        if (unlikely(p == NULL)) {
 709                /*
 710                 * Timer refers to a dead task's clock.
 711                 */
 712                return -ESRCH;
 713        }
 714
 715        new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
 716
 717        read_lock(&tasklist_lock);
 718        /*
 719         * We need the tasklist_lock to protect against reaping that
 720         * clears p->signal.  If p has just been reaped, we can no
 721         * longer get any information about it at all.
 722         */
 723        if (unlikely(p->signal == NULL)) {
 724                read_unlock(&tasklist_lock);
 725                put_task_struct(p);
 726                timer->it.cpu.task = NULL;
 727                return -ESRCH;
 728        }
 729
 730        /*
 731         * Disarm any old timer after extracting its expiry time.
 732         */
 733        BUG_ON(!irqs_disabled());
 734
 735        ret = 0;
 736        spin_lock(&p->sighand->siglock);
 737        old_expires = timer->it.cpu.expires;
 738        if (unlikely(timer->it.cpu.firing)) {
 739                timer->it.cpu.firing = -1;
 740                ret = TIMER_RETRY;
 741        } else
 742                list_del_init(&timer->it.cpu.entry);
 743        spin_unlock(&p->sighand->siglock);
 744
 745        /*
 746         * We need to sample the current value to convert the new
 747         * value from to relative and absolute, and to convert the
 748         * old value from absolute to relative.  To set a process
 749         * timer, we need a sample to balance the thread expiry
 750         * times (in arm_timer).  With an absolute time, we must
 751         * check if it's already passed.  In short, we need a sample.
 752         */
 753        if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
 754                cpu_clock_sample(timer->it_clock, p, &val);
 755        } else {
 756                cpu_clock_sample_group(timer->it_clock, p, &val);
 757        }
 758
 759        if (old) {
 760                if (old_expires.sched == 0) {
 761                        old->it_value.tv_sec = 0;
 762                        old->it_value.tv_nsec = 0;
 763                } else {
 764                        /*
 765                         * Update the timer in case it has
 766                         * overrun already.  If it has,
 767                         * we'll report it as having overrun
 768                         * and with the next reloaded timer
 769                         * already ticking, though we are
 770                         * swallowing that pending
 771                         * notification here to install the
 772                         * new setting.
 773                         */
 774                        bump_cpu_timer(timer, val);
 775                        if (cpu_time_before(timer->it_clock, val,
 776                                            timer->it.cpu.expires)) {
 777                                old_expires = cpu_time_sub(
 778                                        timer->it_clock,
 779                                        timer->it.cpu.expires, val);
 780                                sample_to_timespec(timer->it_clock,
 781                                                   old_expires,
 782                                                   &old->it_value);
 783                        } else {
 784                                old->it_value.tv_nsec = 1;
 785                                old->it_value.tv_sec = 0;
 786                        }
 787                }
 788        }
 789
 790        if (unlikely(ret)) {
 791                /*
 792                 * We are colliding with the timer actually firing.
 793                 * Punt after filling in the timer's old value, and
 794                 * disable this firing since we are already reporting
 795                 * it as an overrun (thanks to bump_cpu_timer above).
 796                 */
 797                read_unlock(&tasklist_lock);
 798                goto out;
 799        }
 800
 801        if (new_expires.sched != 0 && !(flags & TIMER_ABSTIME)) {
 802                cpu_time_add(timer->it_clock, &new_expires, val);
 803        }
 804
 805        /*
 806         * Install the new expiry time (or zero).
 807         * For a timer with no notification action, we don't actually
 808         * arm the timer (we'll just fake it for timer_gettime).
 809         */
 810        timer->it.cpu.expires = new_expires;
 811        if (new_expires.sched != 0 &&
 812            (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
 813            cpu_time_before(timer->it_clock, val, new_expires)) {
 814                arm_timer(timer, val);
 815        }
 816
 817        read_unlock(&tasklist_lock);
 818
 819        /*
 820         * Install the new reload setting, and
 821         * set up the signal and overrun bookkeeping.
 822         */
 823        timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
 824                                                &new->it_interval);
 825
 826        /*
 827         * This acts as a modification timestamp for the timer,
 828         * so any automatic reload attempt will punt on seeing
 829         * that we have reset the timer manually.
 830         */
 831        timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
 832                ~REQUEUE_PENDING;
 833        timer->it_overrun_last = 0;
 834        timer->it_overrun = -1;
 835
 836        if (new_expires.sched != 0 &&
 837            (timer->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE &&
 838            !cpu_time_before(timer->it_clock, val, new_expires)) {
 839                /*
 840                 * The designated time already passed, so we notify
 841                 * immediately, even if the thread never runs to
 842                 * accumulate more time on this clock.
 843                 */
 844                cpu_timer_fire(timer);
 845        }
 846
 847        ret = 0;
 848 out:
 849        if (old) {
 850                sample_to_timespec(timer->it_clock,
 851                                   timer->it.cpu.incr, &old->it_interval);
 852        }
 853        return ret;
 854}
 855
 856void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
 857{
 858        union cpu_time_count now;
 859        struct task_struct *p = timer->it.cpu.task;
 860        int clear_dead;
 861
 862        /*
 863         * Easy part: convert the reload time.
 864         */
 865        sample_to_timespec(timer->it_clock,
 866                           timer->it.cpu.incr, &itp->it_interval);
 867
 868        if (timer->it.cpu.expires.sched == 0) { /* Timer not armed at all.  */
 869                itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
 870                return;
 871        }
 872
 873        if (unlikely(p == NULL)) {
 874                /*
 875                 * This task already died and the timer will never fire.
 876                 * In this case, expires is actually the dead value.
 877                 */
 878        dead:
 879                sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
 880                                   &itp->it_value);
 881                return;
 882        }
 883
 884        /*
 885         * Sample the clock to take the difference with the expiry time.
 886         */
 887        if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
 888                cpu_clock_sample(timer->it_clock, p, &now);
 889                clear_dead = p->exit_state;
 890        } else {
 891                read_lock(&tasklist_lock);
 892                if (unlikely(p->signal == NULL)) {
 893                        /*
 894                         * The process has been reaped.
 895                         * We can't even collect a sample any more.
 896                         * Call the timer disarmed, nothing else to do.
 897                         */
 898                        put_task_struct(p);
 899                        timer->it.cpu.task = NULL;
 900                        timer->it.cpu.expires.sched = 0;
 901                        read_unlock(&tasklist_lock);
 902                        goto dead;
 903                } else {
 904                        cpu_clock_sample_group(timer->it_clock, p, &now);
 905                        clear_dead = (unlikely(p->exit_state) &&
 906                                      thread_group_empty(p));
 907                }
 908                read_unlock(&tasklist_lock);
 909        }
 910
 911        if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
 912                if (timer->it.cpu.incr.sched == 0 &&
 913                    cpu_time_before(timer->it_clock,
 914                                    timer->it.cpu.expires, now)) {
 915                        /*
 916                         * Do-nothing timer expired and has no reload,
 917                         * so it's as if it was never set.
 918                         */
 919                        timer->it.cpu.expires.sched = 0;
 920                        itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
 921                        return;
 922                }
 923                /*
 924                 * Account for any expirations and reloads that should
 925                 * have happened.
 926                 */
 927                bump_cpu_timer(timer, now);
 928        }
 929
 930        if (unlikely(clear_dead)) {
 931                /*
 932                 * We've noticed that the thread is dead, but
 933                 * not yet reaped.  Take this opportunity to
 934                 * drop our task ref.
 935                 */
 936                clear_dead_task(timer, now);
 937                goto dead;
 938        }
 939
 940        if (cpu_time_before(timer->it_clock, now, timer->it.cpu.expires)) {
 941                sample_to_timespec(timer->it_clock,
 942                                   cpu_time_sub(timer->it_clock,
 943                                                timer->it.cpu.expires, now),
 944                                   &itp->it_value);
 945        } else {
 946                /*
 947                 * The timer should have expired already, but the firing
 948                 * hasn't taken place yet.  Say it's just about to expire.
 949                 */
 950                itp->it_value.tv_nsec = 1;
 951                itp->it_value.tv_sec = 0;
 952        }
 953}
 954
 955/*
 956 * Check for any per-thread CPU timers that have fired and move them off
 957 * the tsk->cpu_timers[N] list onto the firing list.  Here we update the
 958 * tsk->it_*_expires values to reflect the remaining thread CPU timers.
 959 */
 960static void check_thread_timers(struct task_struct *tsk,
 961                                struct list_head *firing)
 962{
 963        int maxfire;
 964        struct list_head *timers = tsk->cpu_timers;
 965
 966        maxfire = 20;
 967        tsk->it_prof_expires = cputime_zero;
 968        while (!list_empty(timers)) {
 969                struct cpu_timer_list *t = list_entry(timers->next,
 970                                                      struct cpu_timer_list,
 971                                                      entry);
 972                if (!--maxfire || cputime_lt(prof_ticks(tsk), t->expires.cpu)) {
 973                        tsk->it_prof_expires = t->expires.cpu;
 974                        break;
 975                }
 976                t->firing = 1;
 977                list_move_tail(&t->entry, firing);
 978        }
 979
 980        ++timers;
 981        maxfire = 20;
 982        tsk->it_virt_expires = cputime_zero;
 983        while (!list_empty(timers)) {
 984                struct cpu_timer_list *t = list_entry(timers->next,
 985                                                      struct cpu_timer_list,
 986                                                      entry);
 987                if (!--maxfire || cputime_lt(virt_ticks(tsk), t->expires.cpu)) {
 988                        tsk->it_virt_expires = t->expires.cpu;
 989                        break;
 990                }
 991                t->firing = 1;
 992                list_move_tail(&t->entry, firing);
 993        }
 994
 995        ++timers;
 996        maxfire = 20;
 997        tsk->it_sched_expires = 0;
 998        while (!list_empty(timers)) {
 999                struct cpu_timer_list *t = list_entry(timers->next,
1000                                                      struct cpu_timer_list,
1001                                                      entry);
1002                if (!--maxfire || tsk->sched_time < t->expires.sched) {
1003                        tsk->it_sched_expires = t->expires.sched;
1004                        break;
1005                }
1006                t->firing = 1;
1007                list_move_tail(&t->entry, firing);
1008        }
1009}
1010
1011/*
1012 * Check for any per-thread CPU timers that have fired and move them
1013 * off the tsk->*_timers list onto the firing list.  Per-thread timers
1014 * have already been taken off.
1015 */
1016static void check_process_timers(struct task_struct *tsk,
1017                                 struct list_head *firing)
1018{
1019        int maxfire;
1020        struct signal_struct *const sig = tsk->signal;
1021        cputime_t utime, stime, ptime, virt_expires, prof_expires;
1022        unsigned long long sched_time, sched_expires;
1023        struct task_struct *t;
1024        struct list_head *timers = sig->cpu_timers;
1025
1026        /*
1027         * Don't sample the current process CPU clocks if there are no timers.
1028         */
1029        if (list_empty(&timers[CPUCLOCK_PROF]) &&
1030            cputime_eq(sig->it_prof_expires, cputime_zero) &&
1031            sig->rlim[RLIMIT_CPU].rlim_cur == RLIM_INFINITY &&
1032            list_empty(&timers[CPUCLOCK_VIRT]) &&
1033            cputime_eq(sig->it_virt_expires, cputime_zero) &&
1034            list_empty(&timers[CPUCLOCK_SCHED]))
1035                return;
1036
1037        /*
1038         * Collect the current process totals.
1039         */
1040        utime = sig->utime;
1041        stime = sig->stime;
1042        sched_time = sig->sched_time;
1043        t = tsk;
1044        do {
1045                utime = cputime_add(utime, t->utime);
1046                stime = cputime_add(stime, t->stime);
1047                sched_time += t->sched_time;
1048                t = next_thread(t);
1049        } while (t != tsk);
1050        ptime = cputime_add(utime, stime);
1051
1052        maxfire = 20;
1053        prof_expires = cputime_zero;
1054        while (!list_empty(timers)) {
1055                struct cpu_timer_list *t = list_entry(timers->next,
1056                                                      struct cpu_timer_list,
1057                                                      entry);
1058                if (!--maxfire || cputime_lt(ptime, t->expires.cpu)) {
1059                        prof_expires = t->expires.cpu;
1060                        break;
1061                }
1062                t->firing = 1;
1063                list_move_tail(&t->entry, firing);
1064        }
1065
1066        ++timers;
1067        maxfire = 20;
1068        virt_expires = cputime_zero;
1069        while (!list_empty(timers)) {
1070                struct cpu_timer_list *t = list_entry(timers->next,
1071                                                      struct cpu_timer_list,
1072                                                      entry);
1073                if (!--maxfire || cputime_lt(utime, t->expires.cpu)) {
1074                        virt_expires = t->expires.cpu;
1075                        break;
1076                }
1077                t->firing = 1;
1078                list_move_tail(&t->entry, firing);
1079        }
1080
1081        ++timers;
1082        maxfire = 20;
1083        sched_expires = 0;
1084        while (!list_empty(timers)) {
1085                struct cpu_timer_list *t = list_entry(timers->next,
1086                                                      struct cpu_timer_list,
1087                                                      entry);
1088                if (!--maxfire || sched_time < t->expires.sched) {
1089                        sched_expires = t->expires.sched;
1090                        break;
1091                }
1092                t->firing = 1;
1093                list_move_tail(&t->entry, firing);
1094        }
1095
1096        /*
1097         * Check for the special case process timers.
1098         */
1099        if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1100                if (cputime_ge(ptime, sig->it_prof_expires)) {
1101                        /* ITIMER_PROF fires and reloads.  */
1102                        sig->it_prof_expires = sig->it_prof_incr;
1103                        if (!cputime_eq(sig->it_prof_expires, cputime_zero)) {
1104                                sig->it_prof_expires = cputime_add(
1105                                        sig->it_prof_expires, ptime);
1106                        }
1107                        __group_send_sig_info(SIGPROF, SEND_SIG_PRIV, tsk);
1108                }
1109                if (!cputime_eq(sig->it_prof_expires, cputime_zero) &&
1110                    (cputime_eq(prof_expires, cputime_zero) ||
1111                     cputime_lt(sig->it_prof_expires, prof_expires))) {
1112                        prof_expires = sig->it_prof_expires;
1113                }
1114        }
1115        if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1116                if (cputime_ge(utime, sig->it_virt_expires)) {
1117                        /* ITIMER_VIRTUAL fires and reloads.  */
1118                        sig->it_virt_expires = sig->it_virt_incr;
1119                        if (!cputime_eq(sig->it_virt_expires, cputime_zero)) {
1120                                sig->it_virt_expires = cputime_add(
1121                                        sig->it_virt_expires, utime);
1122                        }
1123                        __group_send_sig_info(SIGVTALRM, SEND_SIG_PRIV, tsk);
1124                }
1125                if (!cputime_eq(sig->it_virt_expires, cputime_zero) &&
1126                    (cputime_eq(virt_expires, cputime_zero) ||
1127                     cputime_lt(sig->it_virt_expires, virt_expires))) {
1128                        virt_expires = sig->it_virt_expires;
1129                }
1130        }
1131        if (sig->rlim[RLIMIT_CPU].rlim_cur != RLIM_INFINITY) {
1132                unsigned long psecs = cputime_to_secs(ptime);
1133                cputime_t x;
1134                if (psecs >= sig->rlim[RLIMIT_CPU].rlim_max) {
1135                        /*
1136                         * At the hard limit, we just die.
1137                         * No need to calculate anything else now.
1138                         */
1139                        __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
1140                        return;
1141                }
1142                if (psecs >= sig->rlim[RLIMIT_CPU].rlim_cur) {
1143                        /*
1144                         * At the soft limit, send a SIGXCPU every second.
1145                         */
1146                        __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
1147                        if (sig->rlim[RLIMIT_CPU].rlim_cur
1148                            < sig->rlim[RLIMIT_CPU].rlim_max) {
1149                                sig->rlim[RLIMIT_CPU].rlim_cur++;
1150                        }
1151                }
1152                x = secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
1153                if (cputime_eq(prof_expires, cputime_zero) ||
1154                    cputime_lt(x, prof_expires)) {
1155                        prof_expires = x;
1156                }
1157        }
1158
1159        if (!cputime_eq(prof_expires, cputime_zero) ||
1160            !cputime_eq(virt_expires, cputime_zero) ||
1161            sched_expires != 0) {
1162                /*
1163                 * Rebalance the threads' expiry times for the remaining
1164                 * process CPU timers.
1165                 */
1166
1167                cputime_t prof_left, virt_left, ticks;
1168                unsigned long long sched_left, sched;
1169                const unsigned int nthreads = atomic_read(&sig->live);
1170
1171                if (!nthreads)
1172                        return;
1173
1174                prof_left = cputime_sub(prof_expires, utime);
1175                prof_left = cputime_sub(prof_left, stime);
1176                prof_left = cputime_div_non_zero(prof_left, nthreads);
1177                virt_left = cputime_sub(virt_expires, utime);
1178                virt_left = cputime_div_non_zero(virt_left, nthreads);
1179                if (sched_expires) {
1180                        sched_left = sched_expires - sched_time;
1181                        do_div(sched_left, nthreads);
1182                        sched_left = max_t(unsigned long long, sched_left, 1);
1183                } else {
1184                        sched_left = 0;
1185                }
1186                t = tsk;
1187                do {
1188                        if (unlikely(t->flags & PF_EXITING))
1189                                continue;
1190
1191                        ticks = cputime_add(cputime_add(t->utime, t->stime),
1192                                            prof_left);
1193                        if (!cputime_eq(prof_expires, cputime_zero) &&
1194                            (cputime_eq(t->it_prof_expires, cputime_zero) ||
1195                             cputime_gt(t->it_prof_expires, ticks))) {
1196                                t->it_prof_expires = ticks;
1197                        }
1198
1199                        ticks = cputime_add(t->utime, virt_left);
1200                        if (!cputime_eq(virt_expires, cputime_zero) &&
1201                            (cputime_eq(t->it_virt_expires, cputime_zero) ||
1202                             cputime_gt(t->it_virt_expires, ticks))) {
1203                                t->it_virt_expires = ticks;
1204                        }
1205
1206                        sched = t->sched_time + sched_left;
1207                        if (sched_expires && (t->it_sched_expires == 0 ||
1208                                              t->it_sched_expires > sched)) {
1209                                t->it_sched_expires = sched;
1210                        }
1211                } while ((t = next_thread(t)) != tsk);
1212        }
1213}
1214
1215/*
1216 * This is called from the signal code (via do_schedule_next_timer)
1217 * when the last timer signal was delivered and we have to reload the timer.
1218 */
1219void posix_cpu_timer_schedule(struct k_itimer *timer)
1220{
1221        struct task_struct *p = timer->it.cpu.task;
1222        union cpu_time_count now;
1223
1224        if (unlikely(p == NULL))
1225                /*
1226                 * The task was cleaned up already, no future firings.
1227                 */
1228                goto out;
1229
1230        /*
1231         * Fetch the current sample and update the timer's expiry time.
1232         */
1233        if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
1234                cpu_clock_sample(timer->it_clock, p, &now);
1235                bump_cpu_timer(timer, now);
1236                if (unlikely(p->exit_state)) {
1237                        clear_dead_task(timer, now);
1238                        goto out;
1239                }
1240                read_lock(&tasklist_lock); /* arm_timer needs it.  */
1241        } else {
1242                read_lock(&tasklist_lock);
1243                if (unlikely(p->signal == NULL)) {
1244                        /*
1245                         * The process has been reaped.
1246                         * We can't even collect a sample any more.
1247                         */
1248                        put_task_struct(p);
1249                        timer->it.cpu.task = p = NULL;
1250                        timer->it.cpu.expires.sched = 0;
1251                        goto out_unlock;
1252                } else if (unlikely(p->exit_state) && thread_group_empty(p)) {
1253                        /*
1254                         * We've noticed that the thread is dead, but
1255                         * not yet reaped.  Take this opportunity to
1256                         * drop our task ref.
1257                         */
1258                        clear_dead_task(timer, now);
1259                        goto out_unlock;
1260                }
1261                cpu_clock_sample_group(timer->it_clock, p, &now);
1262                bump_cpu_timer(timer, now);
1263                /* Leave the tasklist_lock locked for the call below.  */
1264        }
1265
1266        /*
1267         * Now re-arm for the new expiry time.
1268         */
1269        arm_timer(timer, now);
1270
1271out_unlock:
1272        read_unlock(&tasklist_lock);
1273
1274out:
1275        timer->it_overrun_last = timer->it_overrun;
1276        timer->it_overrun = -1;
1277        ++timer->it_requeue_pending;
1278}
1279
1280/*
1281 * This is called from the timer interrupt handler.  The irq handler has
1282 * already updated our counts.  We need to check if any timers fire now.
1283 * Interrupts are disabled.
1284 */
1285void run_posix_cpu_timers(struct task_struct *tsk)
1286{
1287        LIST_HEAD(firing);
1288        struct k_itimer *timer, *next;
1289
1290        BUG_ON(!irqs_disabled());
1291
1292#define UNEXPIRED(clock) \
1293                (cputime_eq(tsk->it_##clock##_expires, cputime_zero) || \
1294                 cputime_lt(clock##_ticks(tsk), tsk->it_##clock##_expires))
1295
1296        if (UNEXPIRED(prof) && UNEXPIRED(virt) &&
1297            (tsk->it_sched_expires == 0 ||
1298             tsk->sched_time < tsk->it_sched_expires))
1299                return;
1300
1301#undef  UNEXPIRED
1302
1303        /*
1304         * Double-check with locks held.
1305         */
1306        read_lock(&tasklist_lock);
1307        if (likely(tsk->signal != NULL)) {
1308                spin_lock(&tsk->sighand->siglock);
1309
1310                /*
1311                 * Here we take off tsk->cpu_timers[N] and tsk->signal->cpu_timers[N]
1312                 * all the timers that are firing, and put them on the firing list.
1313                 */
1314                check_thread_timers(tsk, &firing);
1315                check_process_timers(tsk, &firing);
1316
1317                /*
1318                 * We must release these locks before taking any timer's lock.
1319                 * There is a potential race with timer deletion here, as the
1320                 * siglock now protects our private firing list.  We have set
1321                 * the firing flag in each timer, so that a deletion attempt
1322                 * that gets the timer lock before we do will give it up and
1323                 * spin until we've taken care of that timer below.
1324                 */
1325                spin_unlock(&tsk->sighand->siglock);
1326        }
1327        read_unlock(&tasklist_lock);
1328
1329        /*
1330         * Now that all the timers on our list have the firing flag,
1331         * noone will touch their list entries but us.  We'll take
1332         * each timer's lock before clearing its firing flag, so no
1333         * timer call will interfere.
1334         */
1335        list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
1336                int firing;
1337                spin_lock(&timer->it_lock);
1338                list_del_init(&timer->it.cpu.entry);
1339                firing = timer->it.cpu.firing;
1340                timer->it.cpu.firing = 0;
1341                /*
1342                 * The firing flag is -1 if we collided with a reset
1343                 * of the timer, which already reported this
1344                 * almost-firing as an overrun.  So don't generate an event.
1345                 */
1346                if (likely(firing >= 0)) {
1347                        cpu_timer_fire(timer);
1348                }
1349                spin_unlock(&timer->it_lock);
1350        }
1351}
1352
1353/*
1354 * Set one of the process-wide special case CPU timers.
1355 * The tasklist_lock and tsk->sighand->siglock must be held by the caller.
1356 * The oldval argument is null for the RLIMIT_CPU timer, where *newval is
1357 * absolute; non-null for ITIMER_*, where *newval is relative and we update
1358 * it to be absolute, *oldval is absolute and we update it to be relative.
1359 */
1360void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
1361                           cputime_t *newval, cputime_t *oldval)
1362{
1363        union cpu_time_count now;
1364        struct list_head *head;
1365
1366        BUG_ON(clock_idx == CPUCLOCK_SCHED);
1367        cpu_clock_sample_group_locked(clock_idx, tsk, &now);
1368
1369        if (oldval) {
1370                if (!cputime_eq(*oldval, cputime_zero)) {
1371                        if (cputime_le(*oldval, now.cpu)) {
1372                                /* Just about to fire. */
1373                                *oldval = jiffies_to_cputime(1);
1374                        } else {
1375                                *oldval = cputime_sub(*oldval, now.cpu);
1376                        }
1377                }
1378
1379                if (cputime_eq(*newval, cputime_zero))
1380                        return;
1381                *newval = cputime_add(*newval, now.cpu);
1382
1383                /*
1384                 * If the RLIMIT_CPU timer will expire before the
1385                 * ITIMER_PROF timer, we have nothing else to do.
1386                 */
1387                if (tsk->signal->rlim[RLIMIT_CPU].rlim_cur
1388                    < cputime_to_secs(*newval))
1389                        return;
1390        }
1391
1392        /*
1393         * Check whether there are any process timers already set to fire
1394         * before this one.  If so, we don't have anything more to do.
1395         */
1396        head = &tsk->signal->cpu_timers[clock_idx];
1397        if (list_empty(head) ||
1398            cputime_ge(list_entry(head->next,
1399                                  struct cpu_timer_list, entry)->expires.cpu,
1400                       *newval)) {
1401                /*
1402                 * Rejigger each thread's expiry time so that one will
1403                 * notice before we hit the process-cumulative expiry time.
1404                 */
1405                union cpu_time_count expires = { .sched = 0 };
1406                expires.cpu = *newval;
1407                process_timer_rebalance(tsk, clock_idx, expires, now);
1408        }
1409}
1410
1411static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
1412                            struct timespec *rqtp, struct itimerspec *it)
1413{
1414        struct k_itimer timer;
1415        int error;
1416
1417        /*
1418         * Set up a temporary timer and then wait for it to go off.
1419         */
1420        memset(&timer, 0, sizeof timer);
1421        spin_lock_init(&timer.it_lock);
1422        timer.it_clock = which_clock;
1423        timer.it_overrun = -1;
1424        error = posix_cpu_timer_create(&timer);
1425        timer.it_process = current;
1426        if (!error) {
1427                static struct itimerspec zero_it;
1428
1429                memset(it, 0, sizeof *it);
1430                it->it_value = *rqtp;
1431
1432                spin_lock_irq(&timer.it_lock);
1433                error = posix_cpu_timer_set(&timer, flags, it, NULL);
1434                if (error) {
1435                        spin_unlock_irq(&timer.it_lock);
1436                        return error;
1437                }
1438
1439                while (!signal_pending(current)) {
1440                        if (timer.it.cpu.expires.sched == 0) {
1441                                /*
1442                                 * Our timer fired and was reset.
1443                                 */
1444                                spin_unlock_irq(&timer.it_lock);
1445                                return 0;
1446                        }
1447
1448                        /*
1449                         * Block until cpu_timer_fire (or a signal) wakes us.
1450                         */
1451                        __set_current_state(TASK_INTERRUPTIBLE);
1452                        spin_unlock_irq(&timer.it_lock);
1453                        schedule();
1454                        spin_lock_irq(&timer.it_lock);
1455                }
1456
1457                /*
1458                 * We were interrupted by a signal.
1459                 */
1460                sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
1461                posix_cpu_timer_set(&timer, 0, &zero_it, it);
1462                spin_unlock_irq(&timer.it_lock);
1463
1464                if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
1465                        /*
1466                         * It actually did fire already.
1467                         */
1468                        return 0;
1469                }
1470
1471                error = -ERESTART_RESTARTBLOCK;
1472        }
1473
1474        return error;
1475}
1476
1477int posix_cpu_nsleep(const clockid_t which_clock, int flags,
1478                     struct timespec *rqtp, struct timespec __user *rmtp)
1479{
1480        struct restart_block *restart_block =
1481            &current_thread_info()->restart_block;
1482        struct itimerspec it;
1483        int error;
1484
1485        /*
1486         * Diagnose required errors first.
1487         */
1488        if (CPUCLOCK_PERTHREAD(which_clock) &&
1489            (CPUCLOCK_PID(which_clock) == 0 ||
1490             CPUCLOCK_PID(which_clock) == current->pid))
1491                return -EINVAL;
1492
1493        error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
1494
1495        if (error == -ERESTART_RESTARTBLOCK) {
1496
1497                if (flags & TIMER_ABSTIME)
1498                        return -ERESTARTNOHAND;
1499                /*
1500                 * Report back to the user the time still remaining.
1501                 */
1502                if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1503                        return -EFAULT;
1504
1505                restart_block->fn = posix_cpu_nsleep_restart;
1506                restart_block->arg0 = which_clock;
1507                restart_block->arg1 = (unsigned long) rmtp;
1508                restart_block->arg2 = rqtp->tv_sec;
1509                restart_block->arg3 = rqtp->tv_nsec;
1510        }
1511        return error;
1512}
1513
1514long posix_cpu_nsleep_restart(struct restart_block *restart_block)
1515{
1516        clockid_t which_clock = restart_block->arg0;
1517        struct timespec __user *rmtp;
1518        struct timespec t;
1519        struct itimerspec it;
1520        int error;
1521
1522        rmtp = (struct timespec __user *) restart_block->arg1;
1523        t.tv_sec = restart_block->arg2;
1524        t.tv_nsec = restart_block->arg3;
1525
1526        restart_block->fn = do_no_restart_syscall;
1527        error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
1528
1529        if (error == -ERESTART_RESTARTBLOCK) {
1530                /*
1531                 * Report back to the user the time still remaining.
1532                 */
1533                if (rmtp != NULL && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
1534                        return -EFAULT;
1535
1536                restart_block->fn = posix_cpu_nsleep_restart;
1537                restart_block->arg0 = which_clock;
1538                restart_block->arg1 = (unsigned long) rmtp;
1539                restart_block->arg2 = t.tv_sec;
1540                restart_block->arg3 = t.tv_nsec;
1541        }
1542        return error;
1543
1544}
1545
1546
1547#define PROCESS_CLOCK   MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
1548#define THREAD_CLOCK    MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
1549
1550static int process_cpu_clock_getres(const clockid_t which_clock,
1551                                    struct timespec *tp)
1552{
1553        return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
1554}
1555static int process_cpu_clock_get(const clockid_t which_clock,
1556                                 struct timespec *tp)
1557{
1558        return posix_cpu_clock_get(PROCESS_CLOCK, tp);
1559}
1560static int process_cpu_timer_create(struct k_itimer *timer)
1561{
1562        timer->it_clock = PROCESS_CLOCK;
1563        return posix_cpu_timer_create(timer);
1564}
1565static int process_cpu_nsleep(const clockid_t which_clock, int flags,
1566                              struct timespec *rqtp,
1567                              struct timespec __user *rmtp)
1568{
1569        return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
1570}
1571static long process_cpu_nsleep_restart(struct restart_block *restart_block)
1572{
1573        return -EINVAL;
1574}
1575static int thread_cpu_clock_getres(const clockid_t which_clock,
1576                                   struct timespec *tp)
1577{
1578        return posix_cpu_clock_getres(THREAD_CLOCK, tp);
1579}
1580static int thread_cpu_clock_get(const clockid_t which_clock,
1581                                struct timespec *tp)
1582{
1583        return posix_cpu_clock_get(THREAD_CLOCK, tp);
1584}
1585static int thread_cpu_timer_create(struct k_itimer *timer)
1586{
1587        timer->it_clock = THREAD_CLOCK;
1588        return posix_cpu_timer_create(timer);
1589}
1590static int thread_cpu_nsleep(const clockid_t which_clock, int flags,
1591                              struct timespec *rqtp, struct timespec __user *rmtp)
1592{
1593        return -EINVAL;
1594}
1595static long thread_cpu_nsleep_restart(struct restart_block *restart_block)
1596{
1597        return -EINVAL;
1598}
1599
1600static __init int init_posix_cpu_timers(void)
1601{
1602        struct k_clock process = {
1603                .clock_getres = process_cpu_clock_getres,
1604                .clock_get = process_cpu_clock_get,
1605                .clock_set = do_posix_clock_nosettime,
1606                .timer_create = process_cpu_timer_create,
1607                .nsleep = process_cpu_nsleep,
1608                .nsleep_restart = process_cpu_nsleep_restart,
1609        };
1610        struct k_clock thread = {
1611                .clock_getres = thread_cpu_clock_getres,
1612                .clock_get = thread_cpu_clock_get,
1613                .clock_set = do_posix_clock_nosettime,
1614                .timer_create = thread_cpu_timer_create,
1615                .nsleep = thread_cpu_nsleep,
1616                .nsleep_restart = thread_cpu_nsleep_restart,
1617        };
1618
1619        register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
1620        register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
1621
1622        return 0;
1623}
1624__initcall(init_posix_cpu_timers);
1625