LCOV - code coverage report
Current view: top level - fs - timerfd.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-djwa @ Mon Jul 31 20:08:17 PDT 2023 Lines: 158 264 59.8 %
Date: 2023-07-31 20:08:17 Functions: 17 25 68.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  *  fs/timerfd.c
       4             :  *
       5             :  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
       6             :  *
       7             :  *
       8             :  *  Thanks to Thomas Gleixner for code reviews and useful comments.
       9             :  *
      10             :  */
      11             : 
      12             : #include <linux/alarmtimer.h>
      13             : #include <linux/file.h>
      14             : #include <linux/poll.h>
      15             : #include <linux/init.h>
      16             : #include <linux/fs.h>
      17             : #include <linux/sched.h>
      18             : #include <linux/kernel.h>
      19             : #include <linux/slab.h>
      20             : #include <linux/list.h>
      21             : #include <linux/spinlock.h>
      22             : #include <linux/time.h>
      23             : #include <linux/hrtimer.h>
      24             : #include <linux/anon_inodes.h>
      25             : #include <linux/timerfd.h>
      26             : #include <linux/syscalls.h>
      27             : #include <linux/compat.h>
      28             : #include <linux/rcupdate.h>
      29             : #include <linux/time_namespace.h>
      30             : 
      31             : struct timerfd_ctx {
      32             :         union {
      33             :                 struct hrtimer tmr;
      34             :                 struct alarm alarm;
      35             :         } t;
      36             :         ktime_t tintv;
      37             :         ktime_t moffs;
      38             :         wait_queue_head_t wqh;
      39             :         u64 ticks;
      40             :         int clockid;
      41             :         short unsigned expired;
      42             :         short unsigned settime_flags;   /* to show in fdinfo */
      43             :         struct rcu_head rcu;
      44             :         struct list_head clist;
      45             :         spinlock_t cancel_lock;
      46             :         bool might_cancel;
      47             : };
      48             : 
      49             : static LIST_HEAD(cancel_list);
      50             : static DEFINE_SPINLOCK(cancel_lock);
      51             : 
      52             : static inline bool isalarm(struct timerfd_ctx *ctx)
      53             : {
      54     5322933 :         return ctx->clockid == CLOCK_REALTIME_ALARM ||
      55             :                 ctx->clockid == CLOCK_BOOTTIME_ALARM;
      56             : }
      57             : 
      58             : /*
      59             :  * This gets called when the timer event triggers. We set the "expired"
      60             :  * flag, but we do not re-arm the timer (in case it's necessary,
      61             :  * tintv != 0) until the timer is accessed.
      62             :  */
      63      291354 : static void timerfd_triggered(struct timerfd_ctx *ctx)
      64             : {
      65      291354 :         unsigned long flags;
      66             : 
      67      291354 :         spin_lock_irqsave(&ctx->wqh.lock, flags);
      68      291327 :         ctx->expired = 1;
      69      291327 :         ctx->ticks++;
      70      291327 :         wake_up_locked_poll(&ctx->wqh, EPOLLIN);
      71      291339 :         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
      72      291343 : }
      73             : 
      74      291302 : static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
      75             : {
      76      291302 :         struct timerfd_ctx *ctx = container_of(htmr, struct timerfd_ctx,
      77             :                                                t.tmr);
      78      291302 :         timerfd_triggered(ctx);
      79      291344 :         return HRTIMER_NORESTART;
      80             : }
      81             : 
      82           0 : static enum alarmtimer_restart timerfd_alarmproc(struct alarm *alarm,
      83             :         ktime_t now)
      84             : {
      85           0 :         struct timerfd_ctx *ctx = container_of(alarm, struct timerfd_ctx,
      86             :                                                t.alarm);
      87           0 :         timerfd_triggered(ctx);
      88           0 :         return ALARMTIMER_NORESTART;
      89             : }
      90             : 
      91             : /*
      92             :  * Called when the clock was set to cancel the timers in the cancel
      93             :  * list. This will wake up processes waiting on these timers. The
      94             :  * wake-up requires ctx->ticks to be non zero, therefore we increment
      95             :  * it before calling wake_up_locked().
      96             :  */
      97           0 : void timerfd_clock_was_set(void)
      98             : {
      99           0 :         ktime_t moffs = ktime_mono_to_real(0);
     100           0 :         struct timerfd_ctx *ctx;
     101           0 :         unsigned long flags;
     102             : 
     103           0 :         rcu_read_lock();
     104           0 :         list_for_each_entry_rcu(ctx, &cancel_list, clist) {
     105           0 :                 if (!ctx->might_cancel)
     106           0 :                         continue;
     107           0 :                 spin_lock_irqsave(&ctx->wqh.lock, flags);
     108           0 :                 if (ctx->moffs != moffs) {
     109           0 :                         ctx->moffs = KTIME_MAX;
     110           0 :                         ctx->ticks++;
     111           0 :                         wake_up_locked_poll(&ctx->wqh, EPOLLIN);
     112             :                 }
     113           0 :                 spin_unlock_irqrestore(&ctx->wqh.lock, flags);
     114             :         }
     115           0 :         rcu_read_unlock();
     116           0 : }
     117             : 
     118           0 : static void timerfd_resume_work(struct work_struct *work)
     119             : {
     120           0 :         timerfd_clock_was_set();
     121           0 : }
     122             : 
     123             : static DECLARE_WORK(timerfd_work, timerfd_resume_work);
     124             : 
     125             : /*
     126             :  * Invoked from timekeeping_resume(). Defer the actual update to work so
     127             :  * timerfd_clock_was_set() runs in task context.
     128             :  */
     129           0 : void timerfd_resume(void)
     130             : {
     131           0 :         schedule_work(&timerfd_work);
     132           0 : }
     133             : 
     134     1178401 : static void __timerfd_remove_cancel(struct timerfd_ctx *ctx)
     135             : {
     136     1178401 :         if (ctx->might_cancel) {
     137         613 :                 ctx->might_cancel = false;
     138         613 :                 spin_lock(&cancel_lock);
     139         613 :                 list_del_rcu(&ctx->clist);
     140         613 :                 spin_unlock(&cancel_lock);
     141             :         }
     142     1178401 : }
     143             : 
     144       88275 : static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
     145             : {
     146       88275 :         spin_lock(&ctx->cancel_lock);
     147       88275 :         __timerfd_remove_cancel(ctx);
     148       88275 :         spin_unlock(&ctx->cancel_lock);
     149       88275 : }
     150             : 
     151     1151964 : static bool timerfd_canceled(struct timerfd_ctx *ctx)
     152             : {
     153     1151964 :         if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
     154             :                 return false;
     155           0 :         ctx->moffs = ktime_mono_to_real(0);
     156           0 :         return true;
     157             : }
     158             : 
     159     1090606 : static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
     160             : {
     161     1090606 :         spin_lock(&ctx->cancel_lock);
     162     1090676 :         if ((ctx->clockid == CLOCK_REALTIME ||
     163             :              ctx->clockid == CLOCK_REALTIME_ALARM) &&
     164        1197 :             (flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
     165         606 :                 if (!ctx->might_cancel) {
     166         606 :                         ctx->might_cancel = true;
     167         606 :                         spin_lock(&cancel_lock);
     168         606 :                         list_add_rcu(&ctx->clist, &cancel_list);
     169         606 :                         spin_unlock(&cancel_lock);
     170             :                 }
     171             :         } else {
     172     1090070 :                 __timerfd_remove_cancel(ctx);
     173             :         }
     174     1090827 :         spin_unlock(&ctx->cancel_lock);
     175     1090776 : }
     176             : 
     177     1090765 : static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
     178             : {
     179     1090765 :         ktime_t remaining;
     180             : 
     181     1090765 :         if (isalarm(ctx))
     182           0 :                 remaining = alarm_expires_remaining(&ctx->t.alarm);
     183             :         else
     184     1090765 :                 remaining = hrtimer_expires_remaining_adjusted(&ctx->t.tmr);
     185             : 
     186     1090755 :         return remaining < 0 ? 0: remaining;
     187             : }
     188             : 
     189     1090641 : static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
     190             :                          const struct itimerspec64 *ktmr)
     191             : {
     192     1090641 :         enum hrtimer_mode htmode;
     193     1090641 :         ktime_t texp;
     194     1090641 :         int clockid = ctx->clockid;
     195             : 
     196     1090641 :         htmode = (flags & TFD_TIMER_ABSTIME) ?
     197             :                 HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
     198             : 
     199     1090641 :         texp = timespec64_to_ktime(ktmr->it_value);
     200     1090641 :         ctx->expired = 0;
     201     1090641 :         ctx->ticks = 0;
     202     1090641 :         ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
     203             : 
     204     1090641 :         if (isalarm(ctx)) {
     205           0 :                 alarm_init(&ctx->t.alarm,
     206             :                            ctx->clockid == CLOCK_REALTIME_ALARM ?
     207             :                            ALARM_REALTIME : ALARM_BOOTTIME,
     208             :                            timerfd_alarmproc);
     209             :         } else {
     210     1090641 :                 hrtimer_init(&ctx->t.tmr, clockid, htmode);
     211     1090738 :                 hrtimer_set_expires(&ctx->t.tmr, texp);
     212     1090738 :                 ctx->t.tmr.function = timerfd_tmrproc;
     213             :         }
     214             : 
     215     1090738 :         if (texp != 0) {
     216      871843 :                 if (flags & TFD_TIMER_ABSTIME)
     217      868626 :                         texp = timens_ktime_to_host(clockid, texp);
     218      871843 :                 if (isalarm(ctx)) {
     219           0 :                         if (flags & TFD_TIMER_ABSTIME)
     220           0 :                                 alarm_start(&ctx->t.alarm, texp);
     221             :                         else
     222           0 :                                 alarm_start_relative(&ctx->t.alarm, texp);
     223             :                 } else {
     224      871843 :                         hrtimer_start(&ctx->t.tmr, texp, htmode);
     225             :                 }
     226             : 
     227      871951 :                 if (timerfd_canceled(ctx))
     228             :                         return -ECANCELED;
     229             :         }
     230             : 
     231     1090847 :         ctx->settime_flags = flags & TFD_SETTIME_FLAGS;
     232     1090847 :         return 0;
     233             : }
     234             : 
     235       88275 : static int timerfd_release(struct inode *inode, struct file *file)
     236             : {
     237       88275 :         struct timerfd_ctx *ctx = file->private_data;
     238             : 
     239       88275 :         timerfd_remove_cancel(ctx);
     240             : 
     241       88275 :         if (isalarm(ctx))
     242           0 :                 alarm_cancel(&ctx->t.alarm);
     243             :         else
     244       88275 :                 hrtimer_cancel(&ctx->t.tmr);
     245       88275 :         kfree_rcu(ctx, rcu);
     246       88275 :         return 0;
     247             : }
     248             : 
     249      625020 : static __poll_t timerfd_poll(struct file *file, poll_table *wait)
     250             : {
     251      625020 :         struct timerfd_ctx *ctx = file->private_data;
     252      625020 :         __poll_t events = 0;
     253      625020 :         unsigned long flags;
     254             : 
     255      625020 :         poll_wait(file, &ctx->wqh, wait);
     256             : 
     257      625022 :         spin_lock_irqsave(&ctx->wqh.lock, flags);
     258      624990 :         if (ctx->ticks)
     259      280002 :                 events |= EPOLLIN;
     260      624990 :         spin_unlock_irqrestore(&ctx->wqh.lock, flags);
     261             : 
     262      624958 :         return events;
     263             : }
     264             : 
     265      280032 : static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
     266             :                             loff_t *ppos)
     267             : {
     268      280032 :         struct timerfd_ctx *ctx = file->private_data;
     269      280032 :         ssize_t res;
     270      280032 :         u64 ticks = 0;
     271             : 
     272      280032 :         if (count < sizeof(ticks))
     273             :                 return -EINVAL;
     274      280032 :         spin_lock_irq(&ctx->wqh.lock);
     275      279905 :         if (file->f_flags & O_NONBLOCK)
     276             :                 res = -EAGAIN;
     277             :         else
     278           0 :                 res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
     279             : 
     280             :         /*
     281             :          * If clock has changed, we do not care about the
     282             :          * ticks and we do not rearm the timer. Userspace must
     283             :          * reevaluate anyway.
     284             :          */
     285      279905 :         if (timerfd_canceled(ctx)) {
     286           0 :                 ctx->ticks = 0;
     287           0 :                 ctx->expired = 0;
     288           0 :                 res = -ECANCELED;
     289             :         }
     290             : 
     291      279968 :         if (ctx->ticks) {
     292      279962 :                 ticks = ctx->ticks;
     293             : 
     294      279962 :                 if (ctx->expired && ctx->tintv) {
     295             :                         /*
     296             :                          * If tintv != 0, this is a periodic timer that
     297             :                          * needs to be re-armed. We avoid doing it in the timer
     298             :                          * callback to avoid DoS attacks specifying a very
     299             :                          * short timer period.
     300             :                          */
     301           0 :                         if (isalarm(ctx)) {
     302           0 :                                 ticks += alarm_forward_now(
     303           0 :                                         &ctx->t.alarm, ctx->tintv) - 1;
     304           0 :                                 alarm_restart(&ctx->t.alarm);
     305             :                         } else {
     306           0 :                                 ticks += hrtimer_forward_now(&ctx->t.tmr,
     307           0 :                                                              ctx->tintv) - 1;
     308           0 :                                 hrtimer_restart(&ctx->t.tmr);
     309             :                         }
     310             :                 }
     311      279962 :                 ctx->expired = 0;
     312      279962 :                 ctx->ticks = 0;
     313             :         }
     314      279968 :         spin_unlock_irq(&ctx->wqh.lock);
     315      279975 :         if (ticks)
     316      280029 :                 res = put_user(ticks, (u64 __user *) buf) ? -EFAULT: sizeof(ticks);
     317             :         return res;
     318             : }
     319             : 
     320             : #ifdef CONFIG_PROC_FS
     321           0 : static void timerfd_show(struct seq_file *m, struct file *file)
     322             : {
     323           0 :         struct timerfd_ctx *ctx = file->private_data;
     324           0 :         struct timespec64 value, interval;
     325             : 
     326           0 :         spin_lock_irq(&ctx->wqh.lock);
     327           0 :         value = ktime_to_timespec64(timerfd_get_remaining(ctx));
     328           0 :         interval = ktime_to_timespec64(ctx->tintv);
     329           0 :         spin_unlock_irq(&ctx->wqh.lock);
     330             : 
     331           0 :         seq_printf(m,
     332             :                    "clockid: %d\n"
     333             :                    "ticks: %llu\n"
     334             :                    "settime flags: 0%o\n"
     335             :                    "it_value: (%llu, %llu)\n"
     336             :                    "it_interval: (%llu, %llu)\n",
     337             :                    ctx->clockid,
     338           0 :                    (unsigned long long)ctx->ticks,
     339           0 :                    ctx->settime_flags,
     340           0 :                    (unsigned long long)value.tv_sec,
     341           0 :                    (unsigned long long)value.tv_nsec,
     342           0 :                    (unsigned long long)interval.tv_sec,
     343           0 :                    (unsigned long long)interval.tv_nsec);
     344           0 : }
     345             : #else
     346             : #define timerfd_show NULL
     347             : #endif
     348             : 
     349             : #ifdef CONFIG_CHECKPOINT_RESTORE
     350             : static long timerfd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
     351             : {
     352             :         struct timerfd_ctx *ctx = file->private_data;
     353             :         int ret = 0;
     354             : 
     355             :         switch (cmd) {
     356             :         case TFD_IOC_SET_TICKS: {
     357             :                 u64 ticks;
     358             : 
     359             :                 if (copy_from_user(&ticks, (u64 __user *)arg, sizeof(ticks)))
     360             :                         return -EFAULT;
     361             :                 if (!ticks)
     362             :                         return -EINVAL;
     363             : 
     364             :                 spin_lock_irq(&ctx->wqh.lock);
     365             :                 if (!timerfd_canceled(ctx)) {
     366             :                         ctx->ticks = ticks;
     367             :                         wake_up_locked_poll(&ctx->wqh, EPOLLIN);
     368             :                 } else
     369             :                         ret = -ECANCELED;
     370             :                 spin_unlock_irq(&ctx->wqh.lock);
     371             :                 break;
     372             :         }
     373             :         default:
     374             :                 ret = -ENOTTY;
     375             :                 break;
     376             :         }
     377             : 
     378             :         return ret;
     379             : }
     380             : #else
     381             : #define timerfd_ioctl NULL
     382             : #endif
     383             : 
     384             : static const struct file_operations timerfd_fops = {
     385             :         .release        = timerfd_release,
     386             :         .poll           = timerfd_poll,
     387             :         .read           = timerfd_read,
     388             :         .llseek         = noop_llseek,
     389             :         .show_fdinfo    = timerfd_show,
     390             :         .unlocked_ioctl = timerfd_ioctl,
     391             : };
     392             : 
     393     1090831 : static int timerfd_fget(int fd, struct fd *p)
     394             : {
     395     1090831 :         struct fd f = fdget(fd);
     396     1090673 :         if (!f.file)
     397             :                 return -EBADF;
     398     1090673 :         if (f.file->f_op != &timerfd_fops) {
     399           0 :                 fdput(f);
     400           0 :                 return -EINVAL;
     401             :         }
     402     1090673 :         *p = f;
     403     1090673 :         return 0;
     404             : }
     405             : 
     406      176536 : SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
     407             : {
     408       88268 :         int ufd;
     409       88268 :         struct timerfd_ctx *ctx;
     410             : 
     411             :         /* Check the TFD_* constants for consistency.  */
     412       88268 :         BUILD_BUG_ON(TFD_CLOEXEC != O_CLOEXEC);
     413       88268 :         BUILD_BUG_ON(TFD_NONBLOCK != O_NONBLOCK);
     414             : 
     415       88268 :         if ((flags & ~TFD_CREATE_FLAGS) ||
     416       88268 :             (clockid != CLOCK_MONOTONIC &&
     417       88268 :              clockid != CLOCK_REALTIME &&
     418       88268 :              clockid != CLOCK_REALTIME_ALARM &&
     419       13691 :              clockid != CLOCK_BOOTTIME &&
     420       13691 :              clockid != CLOCK_BOOTTIME_ALARM))
     421             :                 return -EINVAL;
     422             : 
     423       88268 :         if ((clockid == CLOCK_REALTIME_ALARM ||
     424           0 :              clockid == CLOCK_BOOTTIME_ALARM) &&
     425           0 :             !capable(CAP_WAKE_ALARM))
     426             :                 return -EPERM;
     427             : 
     428       88268 :         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
     429       88268 :         if (!ctx)
     430             :                 return -ENOMEM;
     431             : 
     432       88268 :         init_waitqueue_head(&ctx->wqh);
     433       88268 :         spin_lock_init(&ctx->cancel_lock);
     434       88268 :         ctx->clockid = clockid;
     435             : 
     436       88268 :         if (isalarm(ctx))
     437           0 :                 alarm_init(&ctx->t.alarm,
     438             :                            ctx->clockid == CLOCK_REALTIME_ALARM ?
     439             :                            ALARM_REALTIME : ALARM_BOOTTIME,
     440             :                            timerfd_alarmproc);
     441             :         else
     442       88268 :                 hrtimer_init(&ctx->t.tmr, clockid, HRTIMER_MODE_ABS);
     443             : 
     444       88268 :         ctx->moffs = ktime_mono_to_real(0);
     445             : 
     446       88268 :         ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
     447       88268 :                                O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
     448       88268 :         if (ufd < 0)
     449           0 :                 kfree(ctx);
     450             : 
     451       88268 :         return ufd;
     452             : }
     453             : 
     454     1090625 : static int do_timerfd_settime(int ufd, int flags, 
     455             :                 const struct itimerspec64 *new,
     456             :                 struct itimerspec64 *old)
     457             : {
     458     1090625 :         struct fd f;
     459     1090625 :         struct timerfd_ctx *ctx;
     460     1090625 :         int ret;
     461             : 
     462     1090625 :         if ((flags & ~TFD_SETTIME_FLAGS) ||
     463     1090625 :                  !itimerspec64_valid(new))
     464             :                 return -EINVAL;
     465             : 
     466     1090625 :         ret = timerfd_fget(ufd, &f);
     467     1090642 :         if (ret)
     468             :                 return ret;
     469     1090642 :         ctx = f.file->private_data;
     470             : 
     471     1090642 :         if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) {
     472           0 :                 fdput(f);
     473           0 :                 return -EPERM;
     474             :         }
     475             : 
     476     1090642 :         timerfd_setup_cancel(ctx, flags);
     477             : 
     478             :         /*
     479             :          * We need to stop the existing timer before reprogramming
     480             :          * it to the new values.
     481             :          */
     482     1090800 :         for (;;) {
     483     1090800 :                 spin_lock_irq(&ctx->wqh.lock);
     484             : 
     485     1090767 :                 if (isalarm(ctx)) {
     486           0 :                         if (alarm_try_to_cancel(&ctx->t.alarm) >= 0)
     487             :                                 break;
     488             :                 } else {
     489     1090767 :                         if (hrtimer_try_to_cancel(&ctx->t.tmr) >= 0)
     490             :                                 break;
     491             :                 }
     492           1 :                 spin_unlock_irq(&ctx->wqh.lock);
     493             : 
     494           0 :                 if (isalarm(ctx))
     495           0 :                         hrtimer_cancel_wait_running(&ctx->t.alarm.timer);
     496             :                 else
     497           0 :                         hrtimer_cancel_wait_running(&ctx->t.tmr);
     498             :         }
     499             : 
     500             :         /*
     501             :          * If the timer is expired and it's periodic, we need to advance it
     502             :          * because the caller may want to know the previous expiration time.
     503             :          * We do not update "ticks" and "expired" since the timer will be
     504             :          * re-programmed again in the following timerfd_setup() call.
     505             :          */
     506     1090837 :         if (ctx->expired && ctx->tintv) {
     507           0 :                 if (isalarm(ctx))
     508           0 :                         alarm_forward_now(&ctx->t.alarm, ctx->tintv);
     509             :                 else
     510           0 :                         hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
     511             :         }
     512             : 
     513     1090837 :         old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
     514     1090801 :         old->it_interval = ktime_to_timespec64(ctx->tintv);
     515             : 
     516             :         /*
     517             :          * Re-program the timer to the new value ...
     518             :          */
     519     1090798 :         ret = timerfd_setup(ctx, flags, new);
     520             : 
     521     1090827 :         spin_unlock_irq(&ctx->wqh.lock);
     522     1090786 :         fdput(f);
     523     1090786 :         return ret;
     524             : }
     525             : 
     526           0 : static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
     527             : {
     528           0 :         struct fd f;
     529           0 :         struct timerfd_ctx *ctx;
     530           0 :         int ret = timerfd_fget(ufd, &f);
     531           0 :         if (ret)
     532             :                 return ret;
     533           0 :         ctx = f.file->private_data;
     534             : 
     535           0 :         spin_lock_irq(&ctx->wqh.lock);
     536           0 :         if (ctx->expired && ctx->tintv) {
     537           0 :                 ctx->expired = 0;
     538             : 
     539           0 :                 if (isalarm(ctx)) {
     540           0 :                         ctx->ticks +=
     541           0 :                                 alarm_forward_now(
     542           0 :                                         &ctx->t.alarm, ctx->tintv) - 1;
     543           0 :                         alarm_restart(&ctx->t.alarm);
     544             :                 } else {
     545           0 :                         ctx->ticks +=
     546           0 :                                 hrtimer_forward_now(&ctx->t.tmr, ctx->tintv)
     547           0 :                                 - 1;
     548           0 :                         hrtimer_restart(&ctx->t.tmr);
     549             :                 }
     550             :         }
     551           0 :         t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
     552           0 :         t->it_interval = ktime_to_timespec64(ctx->tintv);
     553           0 :         spin_unlock_irq(&ctx->wqh.lock);
     554           0 :         fdput(f);
     555           0 :         return 0;
     556             : }
     557             : 
     558     2181503 : SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
     559             :                 const struct __kernel_itimerspec __user *, utmr,
     560             :                 struct __kernel_itimerspec __user *, otmr)
     561             : {
     562     1090746 :         struct itimerspec64 new, old;
     563     1090746 :         int ret;
     564             : 
     565     1090746 :         if (get_itimerspec64(&new, utmr))
     566             :                 return -EFAULT;
     567     1090800 :         ret = do_timerfd_settime(ufd, flags, &new, &old);
     568     1090842 :         if (ret)
     569           0 :                 return ret;
     570     1090842 :         if (otmr && put_itimerspec64(&old, otmr))
     571           0 :                 return -EFAULT;
     572             : 
     573             :         return ret;
     574             : }
     575             : 
     576           0 : SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct __kernel_itimerspec __user *, otmr)
     577             : {
     578           0 :         struct itimerspec64 kotmr;
     579           0 :         int ret = do_timerfd_gettime(ufd, &kotmr);
     580           0 :         if (ret)
     581           0 :                 return ret;
     582           0 :         return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
     583             : }
     584             : 
     585             : #ifdef CONFIG_COMPAT_32BIT_TIME
     586             : SYSCALL_DEFINE4(timerfd_settime32, int, ufd, int, flags,
     587             :                 const struct old_itimerspec32 __user *, utmr,
     588             :                 struct old_itimerspec32 __user *, otmr)
     589             : {
     590             :         struct itimerspec64 new, old;
     591             :         int ret;
     592             : 
     593             :         if (get_old_itimerspec32(&new, utmr))
     594             :                 return -EFAULT;
     595             :         ret = do_timerfd_settime(ufd, flags, &new, &old);
     596             :         if (ret)
     597             :                 return ret;
     598             :         if (otmr && put_old_itimerspec32(&old, otmr))
     599             :                 return -EFAULT;
     600             :         return ret;
     601             : }
     602             : 
     603             : SYSCALL_DEFINE2(timerfd_gettime32, int, ufd,
     604             :                 struct old_itimerspec32 __user *, otmr)
     605             : {
     606             :         struct itimerspec64 kotmr;
     607             :         int ret = do_timerfd_gettime(ufd, &kotmr);
     608             :         if (ret)
     609             :                 return ret;
     610             :         return put_old_itimerspec32(&kotmr, otmr) ? -EFAULT : 0;
     611             : }
     612             : #endif

Generated by: LCOV version 1.14