Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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