Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'posix-timers-2024-07-29' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic/linux-dynticks into timers/core

Pull updates for posix timers and related signal code from Frederic Weisbecker:

* Prepare posix timers selftests for upcoming changes:

- Check signal behaviour sanity against SIG_IGN

- Check signal behaviour sanity against timer
reprogramm/deletion

- Check SIGEV_NONE pending expiry read

- Check interval timer read on a pending SIGNAL

- Check correct overrun count after signal block/unblock

* Various consolidations:

- timer get/set

- signal queue

* Fixes:
- Correctly read SIGEV_NONE timers

- Forward expiry while reading expired interval timers
with pending signal

- Don't arm SIGEV_NONE timers

* Various cleanups all over the place

+610 -289
+3 -3
fs/proc/base.c
··· 2456 2456 if (!tp->sighand) 2457 2457 return ERR_PTR(-ESRCH); 2458 2458 2459 - return seq_list_start(&tp->task->signal->posix_timers, *pos); 2459 + return seq_hlist_start(&tp->task->signal->posix_timers, *pos); 2460 2460 } 2461 2461 2462 2462 static void *timers_next(struct seq_file *m, void *v, loff_t *pos) 2463 2463 { 2464 2464 struct timers_private *tp = m->private; 2465 - return seq_list_next(v, &tp->task->signal->posix_timers, pos); 2465 + return seq_hlist_next(v, &tp->task->signal->posix_timers, pos); 2466 2466 } 2467 2467 2468 2468 static void timers_stop(struct seq_file *m, void *v) ··· 2491 2491 [SIGEV_THREAD] = "thread", 2492 2492 }; 2493 2493 2494 - timer = list_entry((struct list_head *)v, struct k_itimer, list); 2494 + timer = hlist_entry((struct hlist_node *)v, struct k_itimer, list); 2495 2495 notify = timer->it_sigev_notify; 2496 2496 2497 2497 seq_printf(m, "ID: %d\n", timer->it_id);
+2 -2
fs/signalfd.c
··· 159 159 DECLARE_WAITQUEUE(wait, current); 160 160 161 161 spin_lock_irq(&current->sighand->siglock); 162 - ret = dequeue_signal(current, &ctx->sigmask, info, &type); 162 + ret = dequeue_signal(&ctx->sigmask, info, &type); 163 163 switch (ret) { 164 164 case 0: 165 165 if (!nonblock) ··· 174 174 add_wait_queue(&current->sighand->signalfd_wqh, &wait); 175 175 for (;;) { 176 176 set_current_state(TASK_INTERRUPTIBLE); 177 - ret = dequeue_signal(current, &ctx->sigmask, info, &type); 177 + ret = dequeue_signal(&ctx->sigmask, info, &type); 178 178 if (ret != 0) 179 179 break; 180 180 if (signal_pending(current)) {
+1 -1
include/linux/posix-timers.h
··· 158 158 * @rcu: RCU head for freeing the timer. 159 159 */ 160 160 struct k_itimer { 161 - struct list_head list; 161 + struct hlist_node list; 162 162 struct hlist_node t_hash; 163 163 spinlock_t it_lock; 164 164 const struct k_clock *kclock;
+3 -4
include/linux/sched/signal.h
··· 137 137 138 138 /* POSIX.1b Interval Timers */ 139 139 unsigned int next_posix_timer_id; 140 - struct list_head posix_timers; 140 + struct hlist_head posix_timers; 141 141 142 142 /* ITIMER_REAL timer for the process */ 143 143 struct hrtimer real_timer; ··· 276 276 extern void flush_signals(struct task_struct *); 277 277 extern void ignore_signals(struct task_struct *); 278 278 extern void flush_signal_handlers(struct task_struct *, int force_default); 279 - extern int dequeue_signal(struct task_struct *task, sigset_t *mask, 280 - kernel_siginfo_t *info, enum pid_type *type); 279 + extern int dequeue_signal(sigset_t *mask, kernel_siginfo_t *info, enum pid_type *type); 281 280 282 281 static inline int kernel_dequeue_signal(void) 283 282 { ··· 286 287 int ret; 287 288 288 289 spin_lock_irq(&task->sighand->siglock); 289 - ret = dequeue_signal(task, &task->blocked, &__info, &__type); 290 + ret = dequeue_signal(&task->blocked, &__info, &__type); 290 291 spin_unlock_irq(&task->sighand->siglock); 291 292 292 293 return ret;
+1 -1
init/init_task.c
··· 29 29 .cred_guard_mutex = __MUTEX_INITIALIZER(init_signals.cred_guard_mutex), 30 30 .exec_update_lock = __RWSEM_INITIALIZER(init_signals.exec_update_lock), 31 31 #ifdef CONFIG_POSIX_TIMERS 32 - .posix_timers = LIST_HEAD_INIT(init_signals.posix_timers), 32 + .posix_timers = HLIST_HEAD_INIT, 33 33 .cputimer = { 34 34 .cputime_atomic = INIT_CPUTIME_ATOMIC, 35 35 },
+1 -1
kernel/fork.c
··· 1861 1861 prev_cputime_init(&sig->prev_cputime); 1862 1862 1863 1863 #ifdef CONFIG_POSIX_TIMERS 1864 - INIT_LIST_HEAD(&sig->posix_timers); 1864 + INIT_HLIST_HEAD(&sig->posix_timers); 1865 1865 hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1866 1866 sig->real_timer.function = it_real_fn; 1867 1867 #endif
+17 -17
kernel/signal.c
··· 618 618 } 619 619 620 620 /* 621 - * Dequeue a signal and return the element to the caller, which is 622 - * expected to free it. 623 - * 624 - * All callers have to hold the siglock. 621 + * Try to dequeue a signal. If a deliverable signal is found fill in the 622 + * caller provided siginfo and return the signal number. Otherwise return 623 + * 0. 625 624 */ 626 - int dequeue_signal(struct task_struct *tsk, sigset_t *mask, 627 - kernel_siginfo_t *info, enum pid_type *type) 625 + int dequeue_signal(sigset_t *mask, kernel_siginfo_t *info, enum pid_type *type) 628 626 { 627 + struct task_struct *tsk = current; 629 628 bool resched_timer = false; 630 629 int signr; 631 630 632 - /* We only dequeue private signals from ourselves, we don't let 633 - * signalfd steal them 634 - */ 631 + lockdep_assert_held(&tsk->sighand->siglock); 632 + 635 633 *type = PIDTYPE_PID; 636 634 signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer); 637 635 if (!signr) { ··· 1938 1940 1939 1941 void sigqueue_free(struct sigqueue *q) 1940 1942 { 1941 - unsigned long flags; 1942 1943 spinlock_t *lock = &current->sighand->siglock; 1944 + unsigned long flags; 1943 1945 1944 - BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); 1946 + if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) 1947 + return; 1945 1948 /* 1946 1949 * We must hold ->siglock while testing q->list 1947 1950 * to serialize with collect_signal() or with ··· 1970 1971 unsigned long flags; 1971 1972 int ret, result; 1972 1973 1973 - BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); 1974 + if (WARN_ON_ONCE(!(q->flags & SIGQUEUE_PREALLOC))) 1975 + return 0; 1976 + if (WARN_ON_ONCE(q->info.si_code != SI_TIMER)) 1977 + return 0; 1974 1978 1975 1979 ret = -1; 1976 1980 rcu_read_lock(); ··· 2008 2006 * If an SI_TIMER entry is already queue just increment 2009 2007 * the overrun count. 2010 2008 */ 2011 - BUG_ON(q->info.si_code != SI_TIMER); 2012 2009 q->info.si_overrun++; 2013 2010 result = TRACE_SIGNAL_ALREADY_PENDING; 2014 2011 goto out; ··· 2794 2793 type = PIDTYPE_PID; 2795 2794 signr = dequeue_synchronous_signal(&ksig->info); 2796 2795 if (!signr) 2797 - signr = dequeue_signal(current, &current->blocked, 2798 - &ksig->info, &type); 2796 + signr = dequeue_signal(&current->blocked, &ksig->info, &type); 2799 2797 2800 2798 if (!signr) 2801 2799 break; /* will return 0 */ ··· 3648 3648 signotset(&mask); 3649 3649 3650 3650 spin_lock_irq(&tsk->sighand->siglock); 3651 - sig = dequeue_signal(tsk, &mask, info, &type); 3651 + sig = dequeue_signal(&mask, info, &type); 3652 3652 if (!sig && timeout) { 3653 3653 /* 3654 3654 * None ready, temporarily unblock those we're interested ··· 3667 3667 spin_lock_irq(&tsk->sighand->siglock); 3668 3668 __set_task_blocked(tsk, &tsk->real_blocked); 3669 3669 sigemptyset(&tsk->real_blocked); 3670 - sig = dequeue_signal(tsk, &mask, info, &type); 3670 + sig = dequeue_signal(&mask, info, &type); 3671 3671 } 3672 3672 spin_unlock_irq(&tsk->sighand->siglock); 3673 3673
+1 -6
kernel/time/alarmtimer.c
··· 574 574 it.alarm.alarmtimer); 575 575 enum alarmtimer_restart result = ALARMTIMER_NORESTART; 576 576 unsigned long flags; 577 - int si_private = 0; 578 577 579 578 spin_lock_irqsave(&ptr->it_lock, flags); 580 579 581 - ptr->it_active = 0; 582 - if (ptr->it_interval) 583 - si_private = ++ptr->it_requeue_pending; 584 - 585 - if (posix_timer_event(ptr, si_private) && ptr->it_interval) { 580 + if (posix_timer_queue_signal(ptr) && ptr->it_interval) { 586 581 /* 587 582 * Handle ignored signals and rearm the timer. This will go 588 583 * away once we handle ignored signals proper. Ensure that
+89 -126
kernel/time/posix-cpu-timers.c
··· 453 453 struct cpu_timer *ctmr = &timer->it.cpu; 454 454 struct posix_cputimer_base *base; 455 455 456 + timer->it_active = 0; 456 457 if (!cpu_timer_dequeue(ctmr)) 457 458 return; 458 459 ··· 560 559 struct cpu_timer *ctmr = &timer->it.cpu; 561 560 u64 newexp = cpu_timer_getexpires(ctmr); 562 561 562 + timer->it_active = 1; 563 563 if (!cpu_timer_enqueue(&base->tqhead, ctmr)) 564 564 return; 565 565 ··· 586 584 { 587 585 struct cpu_timer *ctmr = &timer->it.cpu; 588 586 589 - if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { 590 - /* 591 - * User don't want any signal. 592 - */ 593 - cpu_timer_setexpires(ctmr, 0); 594 - } else if (unlikely(timer->sigq == NULL)) { 587 + timer->it_active = 0; 588 + if (unlikely(timer->sigq == NULL)) { 595 589 /* 596 590 * This a special case for clock_nanosleep, 597 591 * not a normal timer from sys_timer_create. ··· 598 600 /* 599 601 * One-shot timer. Clear it as soon as it's fired. 600 602 */ 601 - posix_timer_event(timer, 0); 603 + posix_timer_queue_signal(timer); 602 604 cpu_timer_setexpires(ctmr, 0); 603 - } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { 605 + } else if (posix_timer_queue_signal(timer)) { 604 606 /* 605 607 * The signal did not get queued because the signal 606 608 * was ignored, so we won't get any callback to ··· 612 614 } 613 615 } 614 616 617 + static void __posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp, u64 now); 618 + 615 619 /* 616 620 * Guts of sys_timer_settime for CPU timers. 617 621 * This is called with the timer locked and interrupts disabled. ··· 623 623 static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, 624 624 struct itimerspec64 *new, struct itimerspec64 *old) 625 625 { 626 + bool sigev_none = timer->it_sigev_notify == SIGEV_NONE; 626 627 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); 627 - u64 old_expires, new_expires, old_incr, val; 628 628 struct cpu_timer *ctmr = &timer->it.cpu; 629 + u64 old_expires, new_expires, now; 629 630 struct sighand_struct *sighand; 630 631 struct task_struct *p; 631 632 unsigned long flags; ··· 663 662 return -ESRCH; 664 663 } 665 664 666 - /* 667 - * Disarm any old timer after extracting its expiry time. 668 - */ 669 - old_incr = timer->it_interval; 665 + /* Retrieve the current expiry time before disarming the timer */ 670 666 old_expires = cpu_timer_getexpires(ctmr); 671 667 672 668 if (unlikely(timer->it.cpu.firing)) { ··· 671 673 ret = TIMER_RETRY; 672 674 } else { 673 675 cpu_timer_dequeue(ctmr); 676 + timer->it_active = 0; 674 677 } 675 678 676 679 /* 677 - * We need to sample the current value to convert the new 678 - * value from to relative and absolute, and to convert the 679 - * old value from absolute to relative. To set a process 680 - * timer, we need a sample to balance the thread expiry 681 - * times (in arm_timer). With an absolute time, we must 682 - * check if it's already passed. In short, we need a sample. 680 + * Sample the current clock for saving the previous setting 681 + * and for rearming the timer. 683 682 */ 684 683 if (CPUCLOCK_PERTHREAD(timer->it_clock)) 685 - val = cpu_clock_sample(clkid, p); 684 + now = cpu_clock_sample(clkid, p); 686 685 else 687 - val = cpu_clock_sample_group(clkid, p, true); 686 + now = cpu_clock_sample_group(clkid, p, !sigev_none); 688 687 688 + /* Retrieve the previous expiry value if requested. */ 689 689 if (old) { 690 - if (old_expires == 0) { 691 - old->it_value.tv_sec = 0; 692 - old->it_value.tv_nsec = 0; 693 - } else { 694 - /* 695 - * Update the timer in case it has overrun already. 696 - * If it has, we'll report it as having overrun and 697 - * with the next reloaded timer already ticking, 698 - * though we are swallowing that pending 699 - * notification here to install the new setting. 700 - */ 701 - u64 exp = bump_cpu_timer(timer, val); 702 - 703 - if (val < exp) { 704 - old_expires = exp - val; 705 - old->it_value = ns_to_timespec64(old_expires); 706 - } else { 707 - old->it_value.tv_nsec = 1; 708 - old->it_value.tv_sec = 0; 709 - } 710 - } 690 + old->it_value = (struct timespec64){ }; 691 + if (old_expires) 692 + __posix_cpu_timer_get(timer, old, now); 711 693 } 712 694 695 + /* Retry if the timer expiry is running concurrently */ 713 696 if (unlikely(ret)) { 714 - /* 715 - * We are colliding with the timer actually firing. 716 - * Punt after filling in the timer's old value, and 717 - * disable this firing since we are already reporting 718 - * it as an overrun (thanks to bump_cpu_timer above). 719 - */ 720 697 unlock_task_sighand(p, &flags); 721 698 goto out; 722 699 } 723 700 724 - if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) { 725 - new_expires += val; 726 - } 701 + /* Convert relative expiry time to absolute */ 702 + if (new_expires && !(timer_flags & TIMER_ABSTIME)) 703 + new_expires += now; 704 + 705 + /* Set the new expiry time (might be 0) */ 706 + cpu_timer_setexpires(ctmr, new_expires); 727 707 728 708 /* 729 - * Install the new expiry time (or zero). 730 - * For a timer with no notification action, we don't actually 731 - * arm the timer (we'll just fake it for timer_gettime). 709 + * Arm the timer if it is not disabled, the new expiry value has 710 + * not yet expired and the timer requires signal delivery. 711 + * SIGEV_NONE timers are never armed. In case the timer is not 712 + * armed, enforce the reevaluation of the timer base so that the 713 + * process wide cputime counter can be disabled eventually. 732 714 */ 733 - cpu_timer_setexpires(ctmr, new_expires); 734 - if (new_expires != 0 && val < new_expires) { 735 - arm_timer(timer, p); 715 + if (likely(!sigev_none)) { 716 + if (new_expires && now < new_expires) 717 + arm_timer(timer, p); 718 + else 719 + trigger_base_recalc_expires(timer, p); 736 720 } 737 721 738 722 unlock_task_sighand(p, &flags); 739 - /* 740 - * Install the new reload setting, and 741 - * set up the signal and overrun bookkeeping. 742 - */ 743 - timer->it_interval = timespec64_to_ktime(new->it_interval); 723 + 724 + posix_timer_set_common(timer, new); 744 725 745 726 /* 746 - * This acts as a modification timestamp for the timer, 747 - * so any automatic reload attempt will punt on seeing 748 - * that we have reset the timer manually. 727 + * If the new expiry time was already in the past the timer was not 728 + * queued. Fire it immediately even if the thread never runs to 729 + * accumulate more time on this clock. 749 730 */ 750 - timer->it_requeue_pending = (timer->it_requeue_pending + 2) & 751 - ~REQUEUE_PENDING; 752 - timer->it_overrun_last = 0; 753 - timer->it_overrun = -1; 754 - 755 - if (val >= new_expires) { 756 - if (new_expires != 0) { 757 - /* 758 - * The designated time already passed, so we notify 759 - * immediately, even if the thread never runs to 760 - * accumulate more time on this clock. 761 - */ 762 - cpu_timer_fire(timer); 763 - } 764 - 765 - /* 766 - * Make sure we don't keep around the process wide cputime 767 - * counter or the tick dependency if they are not necessary. 768 - */ 769 - sighand = lock_task_sighand(p, &flags); 770 - if (!sighand) 771 - goto out; 772 - 773 - if (!cpu_timer_queued(ctmr)) 774 - trigger_base_recalc_expires(timer, p); 775 - 776 - unlock_task_sighand(p, &flags); 777 - } 778 - out: 731 + if (!sigev_none && new_expires && now >= new_expires) 732 + cpu_timer_fire(timer); 733 + out: 779 734 rcu_read_unlock(); 780 - if (old) 781 - old->it_interval = ns_to_timespec64(old_incr); 782 - 783 735 return ret; 736 + } 737 + 738 + static void __posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp, u64 now) 739 + { 740 + bool sigev_none = timer->it_sigev_notify == SIGEV_NONE; 741 + u64 expires, iv = timer->it_interval; 742 + 743 + /* 744 + * Make sure that interval timers are moved forward for the 745 + * following cases: 746 + * - SIGEV_NONE timers which are never armed 747 + * - Timers which expired, but the signal has not yet been 748 + * delivered 749 + */ 750 + if (iv && ((timer->it_requeue_pending & REQUEUE_PENDING) || sigev_none)) 751 + expires = bump_cpu_timer(timer, now); 752 + else 753 + expires = cpu_timer_getexpires(&timer->it.cpu); 754 + 755 + /* 756 + * Expired interval timers cannot have a remaining time <= 0. 757 + * The kernel has to move them forward so that the next 758 + * timer expiry is > @now. 759 + */ 760 + if (now < expires) { 761 + itp->it_value = ns_to_timespec64(expires - now); 762 + } else { 763 + /* 764 + * A single shot SIGEV_NONE timer must return 0, when it is 765 + * expired! Timers which have a real signal delivery mode 766 + * must return a remaining time greater than 0 because the 767 + * signal has not yet been delivered. 768 + */ 769 + if (!sigev_none) 770 + itp->it_value.tv_nsec = 1; 771 + } 784 772 } 785 773 786 774 static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp) 787 775 { 788 776 clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); 789 - struct cpu_timer *ctmr = &timer->it.cpu; 790 - u64 now, expires = cpu_timer_getexpires(ctmr); 791 777 struct task_struct *p; 778 + u64 now; 792 779 793 780 rcu_read_lock(); 794 781 p = cpu_timer_task_rcu(timer); 795 - if (!p) 796 - goto out; 782 + if (p && cpu_timer_getexpires(&timer->it.cpu)) { 783 + itp->it_interval = ktime_to_timespec64(timer->it_interval); 797 784 798 - /* 799 - * Easy part: convert the reload time. 800 - */ 801 - itp->it_interval = ktime_to_timespec64(timer->it_interval); 785 + if (CPUCLOCK_PERTHREAD(timer->it_clock)) 786 + now = cpu_clock_sample(clkid, p); 787 + else 788 + now = cpu_clock_sample_group(clkid, p, false); 802 789 803 - if (!expires) 804 - goto out; 805 - 806 - /* 807 - * Sample the clock to take the difference with the expiry time. 808 - */ 809 - if (CPUCLOCK_PERTHREAD(timer->it_clock)) 810 - now = cpu_clock_sample(clkid, p); 811 - else 812 - now = cpu_clock_sample_group(clkid, p, false); 813 - 814 - if (now < expires) { 815 - itp->it_value = ns_to_timespec64(expires - now); 816 - } else { 817 - /* 818 - * The timer should have expired already, but the firing 819 - * hasn't taken place yet. Say it's just about to expire. 820 - */ 821 - itp->it_value.tv_nsec = 1; 822 - itp->it_value.tv_sec = 0; 790 + __posix_cpu_timer_get(timer, itp, now); 823 791 } 824 - out: 825 792 rcu_read_unlock(); 826 793 } 827 794
+42 -27
kernel/time/posix-timers.c
··· 277 277 unlock_timer(timr, flags); 278 278 } 279 279 280 - int posix_timer_event(struct k_itimer *timr, int si_private) 280 + int posix_timer_queue_signal(struct k_itimer *timr) 281 281 { 282 + int ret, si_private = 0; 282 283 enum pid_type type; 283 - int ret; 284 + 285 + lockdep_assert_held(&timr->it_lock); 286 + 287 + timr->it_active = 0; 288 + if (timr->it_interval) 289 + si_private = ++timr->it_requeue_pending; 290 + 284 291 /* 285 292 * FIXME: if ->sigq is queued we can race with 286 293 * dequeue_signal()->posixtimer_rearm(). ··· 316 309 */ 317 310 static enum hrtimer_restart posix_timer_fn(struct hrtimer *timer) 318 311 { 312 + struct k_itimer *timr = container_of(timer, struct k_itimer, it.real.timer); 319 313 enum hrtimer_restart ret = HRTIMER_NORESTART; 320 - struct k_itimer *timr; 321 314 unsigned long flags; 322 - int si_private = 0; 323 315 324 - timr = container_of(timer, struct k_itimer, it.real.timer); 325 316 spin_lock_irqsave(&timr->it_lock, flags); 326 317 327 - timr->it_active = 0; 328 - if (timr->it_interval != 0) 329 - si_private = ++timr->it_requeue_pending; 330 - 331 - if (posix_timer_event(timr, si_private)) { 318 + if (posix_timer_queue_signal(timr)) { 332 319 /* 333 320 * The signal was not queued due to SIG_IGN. As a 334 321 * consequence the timer is not going to be rearmed from ··· 516 515 spin_lock_irq(&current->sighand->siglock); 517 516 /* This makes the timer valid in the hash table */ 518 517 WRITE_ONCE(new_timer->it_signal, current->signal); 519 - list_add(&new_timer->list, &current->signal->posix_timers); 518 + hlist_add_head(&new_timer->list, &current->signal->posix_timers); 520 519 spin_unlock_irq(&current->sighand->siglock); 521 520 /* 522 521 * After unlocking sighand::siglock @new_timer is subject to ··· 857 856 return lock_timer(timer_id, flags); 858 857 } 859 858 859 + /* 860 + * Set up the new interval and reset the signal delivery data 861 + */ 862 + void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *new_setting) 863 + { 864 + if (new_setting->it_value.tv_sec || new_setting->it_value.tv_nsec) 865 + timer->it_interval = timespec64_to_ktime(new_setting->it_interval); 866 + else 867 + timer->it_interval = 0; 868 + 869 + /* Prevent reloading in case there is a signal pending */ 870 + timer->it_requeue_pending = (timer->it_requeue_pending + 2) & ~REQUEUE_PENDING; 871 + /* Reset overrun accounting */ 872 + timer->it_overrun_last = 0; 873 + timer->it_overrun = -1LL; 874 + } 875 + 860 876 /* Set a POSIX.1b interval timer. */ 861 877 int common_timer_set(struct k_itimer *timr, int flags, 862 878 struct itimerspec64 *new_setting, ··· 896 878 return TIMER_RETRY; 897 879 898 880 timr->it_active = 0; 899 - timr->it_requeue_pending = (timr->it_requeue_pending + 2) & 900 - ~REQUEUE_PENDING; 901 - timr->it_overrun_last = 0; 881 + posix_timer_set_common(timr, new_setting); 902 882 903 - /* Switch off the timer when it_value is zero */ 883 + /* Keep timer disarmed when it_value is zero */ 904 884 if (!new_setting->it_value.tv_sec && !new_setting->it_value.tv_nsec) 905 885 return 0; 906 886 907 - timr->it_interval = timespec64_to_ktime(new_setting->it_interval); 908 887 expires = timespec64_to_ktime(new_setting->it_value); 909 888 if (flags & TIMER_ABSTIME) 910 889 expires = timens_ktime_to_host(timr->it_clock, expires); ··· 919 904 const struct k_clock *kc; 920 905 struct k_itimer *timr; 921 906 unsigned long flags; 922 - int error = 0; 907 + int error; 923 908 924 909 if (!timespec64_valid(&new_spec64->it_interval) || 925 910 !timespec64_valid(&new_spec64->it_value)) ··· 932 917 retry: 933 918 if (!timr) 934 919 return -EINVAL; 920 + 921 + if (old_spec64) 922 + old_spec64->it_interval = ktime_to_timespec64(timr->it_interval); 935 923 936 924 kc = timr->kclock; 937 925 if (WARN_ON_ONCE(!kc || !kc->timer_set)) ··· 1039 1021 } 1040 1022 1041 1023 spin_lock(&current->sighand->siglock); 1042 - list_del(&timer->list); 1024 + hlist_del(&timer->list); 1043 1025 spin_unlock(&current->sighand->siglock); 1044 1026 /* 1045 1027 * A concurrent lookup could check timer::it_signal lockless. It ··· 1089 1071 1090 1072 goto retry_delete; 1091 1073 } 1092 - list_del(&timer->list); 1074 + hlist_del(&timer->list); 1093 1075 1094 1076 /* 1095 1077 * Setting timer::it_signal to NULL is technically not required ··· 1110 1092 */ 1111 1093 void exit_itimers(struct task_struct *tsk) 1112 1094 { 1113 - struct list_head timers; 1114 - struct k_itimer *tmr; 1095 + struct hlist_head timers; 1115 1096 1116 - if (list_empty(&tsk->signal->posix_timers)) 1097 + if (hlist_empty(&tsk->signal->posix_timers)) 1117 1098 return; 1118 1099 1119 1100 /* Protect against concurrent read via /proc/$PID/timers */ 1120 1101 spin_lock_irq(&tsk->sighand->siglock); 1121 - list_replace_init(&tsk->signal->posix_timers, &timers); 1102 + hlist_move_list(&tsk->signal->posix_timers, &timers); 1122 1103 spin_unlock_irq(&tsk->sighand->siglock); 1123 1104 1124 1105 /* The timers are not longer accessible via tsk::signal */ 1125 - while (!list_empty(&timers)) { 1126 - tmr = list_first_entry(&timers, struct k_itimer, list); 1127 - itimer_delete(tmr); 1128 - } 1106 + while (!hlist_empty(&timers)) 1107 + itimer_delete(hlist_entry(timers.first, struct k_itimer, list)); 1129 1108 } 1130 1109 1131 1110 SYSCALL_DEFINE2(clock_settime, const clockid_t, which_clock,
+2 -1
kernel/time/posix-timers.h
··· 36 36 extern const struct k_clock clock_thread; 37 37 extern const struct k_clock alarm_clock; 38 38 39 - int posix_timer_event(struct k_itimer *timr, int si_private); 39 + int posix_timer_queue_signal(struct k_itimer *timr); 40 40 41 41 void common_timer_get(struct k_itimer *timr, struct itimerspec64 *cur_setting); 42 42 int common_timer_set(struct k_itimer *timr, int flags, 43 43 struct itimerspec64 *new_setting, 44 44 struct itimerspec64 *old_setting); 45 + void posix_timer_set_common(struct k_itimer *timer, struct itimerspec64 *new_setting); 45 46 int common_timer_del(struct k_itimer *timer);
+448 -100
tools/testing/selftests/timers/posix_timers.c
··· 6 6 * 7 7 * Kernel loop code stolen from Steven Rostedt <srostedt@redhat.com> 8 8 */ 9 - 9 + #define _GNU_SOURCE 10 10 #include <sys/time.h> 11 + #include <sys/types.h> 11 12 #include <stdio.h> 12 13 #include <signal.h> 14 + #include <stdint.h> 15 + #include <string.h> 13 16 #include <unistd.h> 14 17 #include <time.h> 15 18 #include <pthread.h> ··· 21 18 22 19 #define DELAY 2 23 20 #define USECS_PER_SEC 1000000 21 + #define NSECS_PER_SEC 1000000000 22 + 23 + static void __fatal_error(const char *test, const char *name, const char *what) 24 + { 25 + char buf[64]; 26 + 27 + strerror_r(errno, buf, sizeof(buf)); 28 + 29 + if (name && strlen(name)) 30 + ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, buf); 31 + else 32 + ksft_exit_fail_msg("%s %s %s\n", test, what, buf); 33 + } 34 + 35 + #define fatal_error(name, what) __fatal_error(__func__, name, what) 24 36 25 37 static volatile int done; 26 38 ··· 92 74 return 0; 93 75 } 94 76 95 - static int check_itimer(int which) 77 + static void check_itimer(int which, const char *name) 96 78 { 97 - const char *name; 98 - int err; 99 79 struct timeval start, end; 100 80 struct itimerval val = { 101 81 .it_value.tv_sec = DELAY, 102 82 }; 103 - 104 - if (which == ITIMER_VIRTUAL) 105 - name = "ITIMER_VIRTUAL"; 106 - else if (which == ITIMER_PROF) 107 - name = "ITIMER_PROF"; 108 - else if (which == ITIMER_REAL) 109 - name = "ITIMER_REAL"; 110 - else 111 - return -1; 112 83 113 84 done = 0; 114 85 ··· 108 101 else if (which == ITIMER_REAL) 109 102 signal(SIGALRM, sig_handler); 110 103 111 - err = gettimeofday(&start, NULL); 112 - if (err < 0) { 113 - ksft_perror("Can't call gettimeofday()"); 114 - return -1; 115 - } 104 + if (gettimeofday(&start, NULL) < 0) 105 + fatal_error(name, "gettimeofday()"); 116 106 117 - err = setitimer(which, &val, NULL); 118 - if (err < 0) { 119 - ksft_perror("Can't set timer"); 120 - return -1; 121 - } 107 + if (setitimer(which, &val, NULL) < 0) 108 + fatal_error(name, "setitimer()"); 122 109 123 110 if (which == ITIMER_VIRTUAL) 124 111 user_loop(); ··· 121 120 else if (which == ITIMER_REAL) 122 121 idle_loop(); 123 122 124 - err = gettimeofday(&end, NULL); 125 - if (err < 0) { 126 - ksft_perror("Can't call gettimeofday()"); 127 - return -1; 128 - } 123 + if (gettimeofday(&end, NULL) < 0) 124 + fatal_error(name, "gettimeofday()"); 129 125 130 126 ksft_test_result(check_diff(start, end) == 0, "%s\n", name); 131 - 132 - return 0; 133 127 } 134 128 135 - static int check_timer_create(int which) 129 + static void check_timer_create(int which, const char *name) 136 130 { 137 - const char *type; 138 - int err; 139 - timer_t id; 140 131 struct timeval start, end; 141 132 struct itimerspec val = { 142 133 .it_value.tv_sec = DELAY, 143 134 }; 144 - 145 - if (which == CLOCK_THREAD_CPUTIME_ID) { 146 - type = "thread"; 147 - } else if (which == CLOCK_PROCESS_CPUTIME_ID) { 148 - type = "process"; 149 - } else { 150 - ksft_print_msg("Unknown timer_create() type %d\n", which); 151 - return -1; 152 - } 135 + timer_t id; 153 136 154 137 done = 0; 155 - err = timer_create(which, NULL, &id); 156 - if (err < 0) { 157 - ksft_perror("Can't create timer"); 158 - return -1; 159 - } 160 - signal(SIGALRM, sig_handler); 161 138 162 - err = gettimeofday(&start, NULL); 163 - if (err < 0) { 164 - ksft_perror("Can't call gettimeofday()"); 165 - return -1; 166 - } 139 + if (timer_create(which, NULL, &id) < 0) 140 + fatal_error(name, "timer_create()"); 167 141 168 - err = timer_settime(id, 0, &val, NULL); 169 - if (err < 0) { 170 - ksft_perror("Can't set timer"); 171 - return -1; 172 - } 142 + if (signal(SIGALRM, sig_handler) == SIG_ERR) 143 + fatal_error(name, "signal()"); 144 + 145 + if (gettimeofday(&start, NULL) < 0) 146 + fatal_error(name, "gettimeofday()"); 147 + 148 + if (timer_settime(id, 0, &val, NULL) < 0) 149 + fatal_error(name, "timer_settime()"); 173 150 174 151 user_loop(); 175 152 176 - err = gettimeofday(&end, NULL); 177 - if (err < 0) { 178 - ksft_perror("Can't call gettimeofday()"); 179 - return -1; 180 - } 153 + if (gettimeofday(&end, NULL) < 0) 154 + fatal_error(name, "gettimeofday()"); 181 155 182 156 ksft_test_result(check_diff(start, end) == 0, 183 - "timer_create() per %s\n", type); 184 - 185 - return 0; 157 + "timer_create() per %s\n", name); 186 158 } 187 159 188 160 static pthread_t ctd_thread; ··· 183 209 184 210 ctd_count = 100; 185 211 if (timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id)) 186 - return "Can't create timer\n"; 212 + fatal_error(NULL, "timer_create()"); 187 213 if (timer_settime(id, 0, &val, NULL)) 188 - return "Can't set timer\n"; 189 - 214 + fatal_error(NULL, "timer_settime()"); 190 215 while (ctd_count > 0 && !ctd_failed) 191 216 ; 192 217 193 218 if (timer_delete(id)) 194 - return "Can't delete timer\n"; 219 + fatal_error(NULL, "timer_delete()"); 195 220 196 221 return NULL; 197 222 } ··· 198 225 /* 199 226 * Test that only the running thread receives the timer signal. 200 227 */ 201 - static int check_timer_distribution(void) 228 + static void check_timer_distribution(void) 202 229 { 203 - const char *errmsg; 230 + if (signal(SIGALRM, ctd_sighandler) == SIG_ERR) 231 + fatal_error(NULL, "signal()"); 204 232 205 - signal(SIGALRM, ctd_sighandler); 206 - 207 - errmsg = "Can't create thread\n"; 208 233 if (pthread_create(&ctd_thread, NULL, ctd_thread_func, NULL)) 209 - goto err; 234 + fatal_error(NULL, "pthread_create()"); 210 235 211 - errmsg = "Can't join thread\n"; 212 - if (pthread_join(ctd_thread, (void **)&errmsg) || errmsg) 213 - goto err; 236 + if (pthread_join(ctd_thread, NULL)) 237 + fatal_error(NULL, "pthread_join()"); 214 238 215 239 if (!ctd_failed) 216 240 ksft_test_result_pass("check signal distribution\n"); ··· 215 245 ksft_test_result_fail("check signal distribution\n"); 216 246 else 217 247 ksft_test_result_skip("check signal distribution (old kernel)\n"); 218 - return 0; 219 - err: 220 - ksft_print_msg("%s", errmsg); 221 - return -1; 248 + } 249 + 250 + struct tmrsig { 251 + int signals; 252 + int overruns; 253 + }; 254 + 255 + static void siginfo_handler(int sig, siginfo_t *si, void *uc) 256 + { 257 + struct tmrsig *tsig = si ? si->si_ptr : NULL; 258 + 259 + if (tsig) { 260 + tsig->signals++; 261 + tsig->overruns += si->si_overrun; 262 + } 263 + } 264 + 265 + static void *ignore_thread(void *arg) 266 + { 267 + unsigned int *tid = arg; 268 + sigset_t set; 269 + 270 + sigemptyset(&set); 271 + sigaddset(&set, SIGUSR1); 272 + if (sigprocmask(SIG_BLOCK, &set, NULL)) 273 + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); 274 + 275 + *tid = gettid(); 276 + sleep(100); 277 + 278 + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) 279 + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); 280 + return NULL; 281 + } 282 + 283 + static void check_sig_ign(int thread) 284 + { 285 + struct tmrsig tsig = { }; 286 + struct itimerspec its; 287 + unsigned int tid = 0; 288 + struct sigaction sa; 289 + struct sigevent sev; 290 + pthread_t pthread; 291 + timer_t timerid; 292 + sigset_t set; 293 + 294 + if (thread) { 295 + if (pthread_create(&pthread, NULL, ignore_thread, &tid)) 296 + fatal_error(NULL, "pthread_create()"); 297 + sleep(1); 298 + } 299 + 300 + sa.sa_flags = SA_SIGINFO; 301 + sa.sa_sigaction = siginfo_handler; 302 + sigemptyset(&sa.sa_mask); 303 + if (sigaction(SIGUSR1, &sa, NULL)) 304 + fatal_error(NULL, "sigaction()"); 305 + 306 + /* Block the signal */ 307 + sigemptyset(&set); 308 + sigaddset(&set, SIGUSR1); 309 + if (sigprocmask(SIG_BLOCK, &set, NULL)) 310 + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); 311 + 312 + memset(&sev, 0, sizeof(sev)); 313 + sev.sigev_notify = SIGEV_SIGNAL; 314 + sev.sigev_signo = SIGUSR1; 315 + sev.sigev_value.sival_ptr = &tsig; 316 + if (thread) { 317 + sev.sigev_notify = SIGEV_THREAD_ID; 318 + sev._sigev_un._tid = tid; 319 + } 320 + 321 + if (timer_create(CLOCK_MONOTONIC, &sev, &timerid)) 322 + fatal_error(NULL, "timer_create()"); 323 + 324 + /* Start the timer to expire in 100ms and 100ms intervals */ 325 + its.it_value.tv_sec = 0; 326 + its.it_value.tv_nsec = 100000000; 327 + its.it_interval.tv_sec = 0; 328 + its.it_interval.tv_nsec = 100000000; 329 + timer_settime(timerid, 0, &its, NULL); 330 + 331 + sleep(1); 332 + 333 + /* Set the signal to be ignored */ 334 + if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) 335 + fatal_error(NULL, "signal(SIG_IGN)"); 336 + 337 + sleep(1); 338 + 339 + if (thread) { 340 + /* Stop the thread first. No signal should be delivered to it */ 341 + if (pthread_cancel(pthread)) 342 + fatal_error(NULL, "pthread_cancel()"); 343 + if (pthread_join(pthread, NULL)) 344 + fatal_error(NULL, "pthread_join()"); 345 + } 346 + 347 + /* Restore the handler */ 348 + if (sigaction(SIGUSR1, &sa, NULL)) 349 + fatal_error(NULL, "sigaction()"); 350 + 351 + sleep(1); 352 + 353 + /* Unblock it, which should deliver the signal in the !thread case*/ 354 + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) 355 + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); 356 + 357 + if (timer_delete(timerid)) 358 + fatal_error(NULL, "timer_delete()"); 359 + 360 + if (!thread) { 361 + ksft_test_result(tsig.signals == 1 && tsig.overruns == 29, 362 + "check_sig_ign SIGEV_SIGNAL\n"); 363 + } else { 364 + ksft_test_result(tsig.signals == 0 && tsig.overruns == 0, 365 + "check_sig_ign SIGEV_THREAD_ID\n"); 366 + } 367 + } 368 + 369 + static void check_rearm(void) 370 + { 371 + struct tmrsig tsig = { }; 372 + struct itimerspec its; 373 + struct sigaction sa; 374 + struct sigevent sev; 375 + timer_t timerid; 376 + sigset_t set; 377 + 378 + sa.sa_flags = SA_SIGINFO; 379 + sa.sa_sigaction = siginfo_handler; 380 + sigemptyset(&sa.sa_mask); 381 + if (sigaction(SIGUSR1, &sa, NULL)) 382 + fatal_error(NULL, "sigaction()"); 383 + 384 + /* Block the signal */ 385 + sigemptyset(&set); 386 + sigaddset(&set, SIGUSR1); 387 + if (sigprocmask(SIG_BLOCK, &set, NULL)) 388 + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); 389 + 390 + memset(&sev, 0, sizeof(sev)); 391 + sev.sigev_notify = SIGEV_SIGNAL; 392 + sev.sigev_signo = SIGUSR1; 393 + sev.sigev_value.sival_ptr = &tsig; 394 + if (timer_create(CLOCK_MONOTONIC, &sev, &timerid)) 395 + fatal_error(NULL, "timer_create()"); 396 + 397 + /* Start the timer to expire in 100ms and 100ms intervals */ 398 + its.it_value.tv_sec = 0; 399 + its.it_value.tv_nsec = 100000000; 400 + its.it_interval.tv_sec = 0; 401 + its.it_interval.tv_nsec = 100000000; 402 + if (timer_settime(timerid, 0, &its, NULL)) 403 + fatal_error(NULL, "timer_settime()"); 404 + 405 + sleep(1); 406 + 407 + /* Reprogram the timer to single shot */ 408 + its.it_value.tv_sec = 10; 409 + its.it_value.tv_nsec = 0; 410 + its.it_interval.tv_sec = 0; 411 + its.it_interval.tv_nsec = 0; 412 + if (timer_settime(timerid, 0, &its, NULL)) 413 + fatal_error(NULL, "timer_settime()"); 414 + 415 + /* Unblock it, which should not deliver a signal */ 416 + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) 417 + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); 418 + 419 + if (timer_delete(timerid)) 420 + fatal_error(NULL, "timer_delete()"); 421 + 422 + ksft_test_result(!tsig.signals, "check_rearm\n"); 423 + } 424 + 425 + static void check_delete(void) 426 + { 427 + struct tmrsig tsig = { }; 428 + struct itimerspec its; 429 + struct sigaction sa; 430 + struct sigevent sev; 431 + timer_t timerid; 432 + sigset_t set; 433 + 434 + sa.sa_flags = SA_SIGINFO; 435 + sa.sa_sigaction = siginfo_handler; 436 + sigemptyset(&sa.sa_mask); 437 + if (sigaction(SIGUSR1, &sa, NULL)) 438 + fatal_error(NULL, "sigaction()"); 439 + 440 + /* Block the signal */ 441 + sigemptyset(&set); 442 + sigaddset(&set, SIGUSR1); 443 + if (sigprocmask(SIG_BLOCK, &set, NULL)) 444 + fatal_error(NULL, "sigprocmask(SIG_BLOCK)"); 445 + 446 + memset(&sev, 0, sizeof(sev)); 447 + sev.sigev_notify = SIGEV_SIGNAL; 448 + sev.sigev_signo = SIGUSR1; 449 + sev.sigev_value.sival_ptr = &tsig; 450 + if (timer_create(CLOCK_MONOTONIC, &sev, &timerid)) 451 + fatal_error(NULL, "timer_create()"); 452 + 453 + /* Start the timer to expire in 100ms and 100ms intervals */ 454 + its.it_value.tv_sec = 0; 455 + its.it_value.tv_nsec = 100000000; 456 + its.it_interval.tv_sec = 0; 457 + its.it_interval.tv_nsec = 100000000; 458 + if (timer_settime(timerid, 0, &its, NULL)) 459 + fatal_error(NULL, "timer_settime()"); 460 + 461 + sleep(1); 462 + 463 + if (timer_delete(timerid)) 464 + fatal_error(NULL, "timer_delete()"); 465 + 466 + /* Unblock it, which should not deliver a signal */ 467 + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) 468 + fatal_error(NULL, "sigprocmask(SIG_UNBLOCK)"); 469 + 470 + ksft_test_result(!tsig.signals, "check_delete\n"); 471 + } 472 + 473 + static inline int64_t calcdiff_ns(struct timespec t1, struct timespec t2) 474 + { 475 + int64_t diff; 476 + 477 + diff = NSECS_PER_SEC * (int64_t)((int) t1.tv_sec - (int) t2.tv_sec); 478 + diff += ((int) t1.tv_nsec - (int) t2.tv_nsec); 479 + return diff; 480 + } 481 + 482 + static void check_sigev_none(int which, const char *name) 483 + { 484 + struct timespec start, now; 485 + struct itimerspec its; 486 + struct sigevent sev; 487 + timer_t timerid; 488 + 489 + memset(&sev, 0, sizeof(sev)); 490 + sev.sigev_notify = SIGEV_NONE; 491 + 492 + if (timer_create(which, &sev, &timerid)) 493 + fatal_error(name, "timer_create()"); 494 + 495 + /* Start the timer to expire in 100ms and 100ms intervals */ 496 + its.it_value.tv_sec = 0; 497 + its.it_value.tv_nsec = 100000000; 498 + its.it_interval.tv_sec = 0; 499 + its.it_interval.tv_nsec = 100000000; 500 + timer_settime(timerid, 0, &its, NULL); 501 + 502 + if (clock_gettime(which, &start)) 503 + fatal_error(name, "clock_gettime()"); 504 + 505 + do { 506 + if (clock_gettime(which, &now)) 507 + fatal_error(name, "clock_gettime()"); 508 + } while (calcdiff_ns(now, start) < NSECS_PER_SEC); 509 + 510 + if (timer_gettime(timerid, &its)) 511 + fatal_error(name, "timer_gettime()"); 512 + 513 + if (timer_delete(timerid)) 514 + fatal_error(name, "timer_delete()"); 515 + 516 + ksft_test_result(its.it_value.tv_sec || its.it_value.tv_nsec, 517 + "check_sigev_none %s\n", name); 518 + } 519 + 520 + static void check_gettime(int which, const char *name) 521 + { 522 + struct itimerspec its, prev; 523 + struct timespec start, now; 524 + struct sigevent sev; 525 + timer_t timerid; 526 + int wraps = 0; 527 + sigset_t set; 528 + 529 + /* Block the signal */ 530 + sigemptyset(&set); 531 + sigaddset(&set, SIGUSR1); 532 + if (sigprocmask(SIG_BLOCK, &set, NULL)) 533 + fatal_error(name, "sigprocmask(SIG_BLOCK)"); 534 + 535 + memset(&sev, 0, sizeof(sev)); 536 + sev.sigev_notify = SIGEV_SIGNAL; 537 + sev.sigev_signo = SIGUSR1; 538 + 539 + if (timer_create(which, &sev, &timerid)) 540 + fatal_error(name, "timer_create()"); 541 + 542 + /* Start the timer to expire in 100ms and 100ms intervals */ 543 + its.it_value.tv_sec = 0; 544 + its.it_value.tv_nsec = 100000000; 545 + its.it_interval.tv_sec = 0; 546 + its.it_interval.tv_nsec = 100000000; 547 + if (timer_settime(timerid, 0, &its, NULL)) 548 + fatal_error(name, "timer_settime()"); 549 + 550 + if (timer_gettime(timerid, &prev)) 551 + fatal_error(name, "timer_gettime()"); 552 + 553 + if (clock_gettime(which, &start)) 554 + fatal_error(name, "clock_gettime()"); 555 + 556 + do { 557 + if (clock_gettime(which, &now)) 558 + fatal_error(name, "clock_gettime()"); 559 + if (timer_gettime(timerid, &its)) 560 + fatal_error(name, "timer_gettime()"); 561 + if (its.it_value.tv_nsec > prev.it_value.tv_nsec) 562 + wraps++; 563 + prev = its; 564 + 565 + } while (calcdiff_ns(now, start) < NSECS_PER_SEC); 566 + 567 + if (timer_delete(timerid)) 568 + fatal_error(name, "timer_delete()"); 569 + 570 + ksft_test_result(wraps > 1, "check_gettime %s\n", name); 571 + } 572 + 573 + static void check_overrun(int which, const char *name) 574 + { 575 + struct timespec start, now; 576 + struct tmrsig tsig = { }; 577 + struct itimerspec its; 578 + struct sigaction sa; 579 + struct sigevent sev; 580 + timer_t timerid; 581 + sigset_t set; 582 + 583 + sa.sa_flags = SA_SIGINFO; 584 + sa.sa_sigaction = siginfo_handler; 585 + sigemptyset(&sa.sa_mask); 586 + if (sigaction(SIGUSR1, &sa, NULL)) 587 + fatal_error(name, "sigaction()"); 588 + 589 + /* Block the signal */ 590 + sigemptyset(&set); 591 + sigaddset(&set, SIGUSR1); 592 + if (sigprocmask(SIG_BLOCK, &set, NULL)) 593 + fatal_error(name, "sigprocmask(SIG_BLOCK)"); 594 + 595 + memset(&sev, 0, sizeof(sev)); 596 + sev.sigev_notify = SIGEV_SIGNAL; 597 + sev.sigev_signo = SIGUSR1; 598 + sev.sigev_value.sival_ptr = &tsig; 599 + if (timer_create(which, &sev, &timerid)) 600 + fatal_error(name, "timer_create()"); 601 + 602 + /* Start the timer to expire in 100ms and 100ms intervals */ 603 + its.it_value.tv_sec = 0; 604 + its.it_value.tv_nsec = 100000000; 605 + its.it_interval.tv_sec = 0; 606 + its.it_interval.tv_nsec = 100000000; 607 + if (timer_settime(timerid, 0, &its, NULL)) 608 + fatal_error(name, "timer_settime()"); 609 + 610 + if (clock_gettime(which, &start)) 611 + fatal_error(name, "clock_gettime()"); 612 + 613 + do { 614 + if (clock_gettime(which, &now)) 615 + fatal_error(name, "clock_gettime()"); 616 + } while (calcdiff_ns(now, start) < NSECS_PER_SEC); 617 + 618 + /* Unblock it, which should deliver a signal */ 619 + if (sigprocmask(SIG_UNBLOCK, &set, NULL)) 620 + fatal_error(name, "sigprocmask(SIG_UNBLOCK)"); 621 + 622 + if (timer_delete(timerid)) 623 + fatal_error(name, "timer_delete()"); 624 + 625 + ksft_test_result(tsig.signals == 1 && tsig.overruns == 9, 626 + "check_overrun %s\n", name); 222 627 } 223 628 224 629 int main(int argc, char **argv) 225 630 { 226 631 ksft_print_header(); 227 - ksft_set_plan(6); 632 + ksft_set_plan(18); 228 633 229 634 ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n"); 230 635 ksft_print_msg("based timers if other threads run on the CPU...\n"); 231 636 232 - if (check_itimer(ITIMER_VIRTUAL) < 0) 233 - ksft_exit_fail(); 234 - 235 - if (check_itimer(ITIMER_PROF) < 0) 236 - ksft_exit_fail(); 237 - 238 - if (check_itimer(ITIMER_REAL) < 0) 239 - ksft_exit_fail(); 240 - 241 - if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0) 242 - ksft_exit_fail(); 637 + check_itimer(ITIMER_VIRTUAL, "ITIMER_VIRTUAL"); 638 + check_itimer(ITIMER_PROF, "ITIMER_PROF"); 639 + check_itimer(ITIMER_REAL, "ITIMER_REAL"); 640 + check_timer_create(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); 243 641 244 642 /* 245 643 * It's unfortunately hard to reliably test a timer expiration ··· 618 280 * to ensure true parallelism. So test only one thread until we 619 281 * find a better solution. 620 282 */ 621 - if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0) 622 - ksft_exit_fail(); 283 + check_timer_create(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); 284 + check_timer_distribution(); 623 285 624 - if (check_timer_distribution() < 0) 625 - ksft_exit_fail(); 286 + check_sig_ign(0); 287 + check_sig_ign(1); 288 + check_rearm(); 289 + check_delete(); 290 + check_sigev_none(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); 291 + check_sigev_none(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); 292 + check_gettime(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); 293 + check_gettime(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); 294 + check_gettime(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); 295 + check_overrun(CLOCK_MONOTONIC, "CLOCK_MONOTONIC"); 296 + check_overrun(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID"); 297 + check_overrun(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID"); 626 298 627 299 ksft_finished(); 628 300 }