Skip to content

Commit 30cb6d5

Browse files
committed
Merge branch 'timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: hrtimers: Reorder clock bases hrtimers: Avoid touching inactive timer bases hrtimers: Make struct hrtimer_cpu_base layout less stupid timerfd: Manage cancelable timers in timerfd clockevents: Move C3 stop test outside lock alarmtimer: Drop device refcount after rtc_open() alarmtimer: Check return value of class_find_device() timerfd: Allow timers to be cancelled when clock was set hrtimers: Prepare for cancel on clock was set timers
2 parents 4867faa + 68fa61c commit 30cb6d5

10 files changed

Lines changed: 239 additions & 118 deletions

File tree

‎fs/timerfd.c‎

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,24 @@
2222
#include <linux/anon_inodes.h>
2323
#include <linux/timerfd.h>
2424
#include <linux/syscalls.h>
25+
#include <linux/rcupdate.h>
2526

2627
struct timerfd_ctx {
2728
struct hrtimer tmr;
2829
ktime_t tintv;
30+
ktime_t moffs;
2931
wait_queue_head_t wqh;
3032
u64 ticks;
3133
int expired;
3234
int clockid;
35+
struct rcu_head rcu;
36+
struct list_head clist;
37+
bool might_cancel;
3338
};
3439

40+
static LIST_HEAD(cancel_list);
41+
static DEFINE_SPINLOCK(cancel_lock);
42+
3543
/*
3644
* This gets called when the timer event triggers. We set the "expired"
3745
* flag, but we do not re-arm the timer (in case it's necessary,
@@ -51,6 +59,63 @@ static enum hrtimer_restart timerfd_tmrproc(struct hrtimer *htmr)
5159
return HRTIMER_NORESTART;
5260
}
5361

62+
/*
63+
* Called when the clock was set to cancel the timers in the cancel
64+
* list.
65+
*/
66+
void timerfd_clock_was_set(void)
67+
{
68+
ktime_t moffs = ktime_get_monotonic_offset();
69+
struct timerfd_ctx *ctx;
70+
unsigned long flags;
71+
72+
rcu_read_lock();
73+
list_for_each_entry_rcu(ctx, &cancel_list, clist) {
74+
if (!ctx->might_cancel)
75+
continue;
76+
spin_lock_irqsave(&ctx->wqh.lock, flags);
77+
if (ctx->moffs.tv64 != moffs.tv64) {
78+
ctx->moffs.tv64 = KTIME_MAX;
79+
wake_up_locked(&ctx->wqh);
80+
}
81+
spin_unlock_irqrestore(&ctx->wqh.lock, flags);
82+
}
83+
rcu_read_unlock();
84+
}
85+
86+
static void timerfd_remove_cancel(struct timerfd_ctx *ctx)
87+
{
88+
if (ctx->might_cancel) {
89+
ctx->might_cancel = false;
90+
spin_lock(&cancel_lock);
91+
list_del_rcu(&ctx->clist);
92+
spin_unlock(&cancel_lock);
93+
}
94+
}
95+
96+
static bool timerfd_canceled(struct timerfd_ctx *ctx)
97+
{
98+
if (!ctx->might_cancel || ctx->moffs.tv64 != KTIME_MAX)
99+
return false;
100+
ctx->moffs = ktime_get_monotonic_offset();
101+
return true;
102+
}
103+
104+
static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
105+
{
106+
if (ctx->clockid == CLOCK_REALTIME && (flags & TFD_TIMER_ABSTIME) &&
107+
(flags & TFD_TIMER_CANCEL_ON_SET)) {
108+
if (!ctx->might_cancel) {
109+
ctx->might_cancel = true;
110+
spin_lock(&cancel_lock);
111+
list_add_rcu(&ctx->clist, &cancel_list);
112+
spin_unlock(&cancel_lock);
113+
}
114+
} else if (ctx->might_cancel) {
115+
timerfd_remove_cancel(ctx);
116+
}
117+
}
118+
54119
static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
55120
{
56121
ktime_t remaining;
@@ -59,11 +124,12 @@ static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
59124
return remaining.tv64 < 0 ? ktime_set(0, 0): remaining;
60125
}
61126

62-
static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
63-
const struct itimerspec *ktmr)
127+
static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
128+
const struct itimerspec *ktmr)
64129
{
65130
enum hrtimer_mode htmode;
66131
ktime_t texp;
132+
int clockid = ctx->clockid;
67133

68134
htmode = (flags & TFD_TIMER_ABSTIME) ?
69135
HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
@@ -72,19 +138,24 @@ static void timerfd_setup(struct timerfd_ctx *ctx, int flags,
72138
ctx->expired = 0;
73139
ctx->ticks = 0;
74140
ctx->tintv = timespec_to_ktime(ktmr->it_interval);
75-
hrtimer_init(&ctx->tmr, ctx->clockid, htmode);
141+
hrtimer_init(&ctx->tmr, clockid, htmode);
76142
hrtimer_set_expires(&ctx->tmr, texp);
77143
ctx->tmr.function = timerfd_tmrproc;
78-
if (texp.tv64 != 0)
144+
if (texp.tv64 != 0) {
79145
hrtimer_start(&ctx->tmr, texp, htmode);
146+
if (timerfd_canceled(ctx))
147+
return -ECANCELED;
148+
}
149+
return 0;
80150
}
81151

82152
static int timerfd_release(struct inode *inode, struct file *file)
83153
{
84154
struct timerfd_ctx *ctx = file->private_data;
85155

156+
timerfd_remove_cancel(ctx);
86157
hrtimer_cancel(&ctx->tmr);
87-
kfree(ctx);
158+
kfree_rcu(ctx, rcu);
88159
return 0;
89160
}
90161

@@ -118,8 +189,21 @@ static ssize_t timerfd_read(struct file *file, char __user *buf, size_t count,
118189
res = -EAGAIN;
119190
else
120191
res = wait_event_interruptible_locked_irq(ctx->wqh, ctx->ticks);
192+
193+
/*
194+
* If clock has changed, we do not care about the
195+
* ticks and we do not rearm the timer. Userspace must
196+
* reevaluate anyway.
197+
*/
198+
if (timerfd_canceled(ctx)) {
199+
ctx->ticks = 0;
200+
ctx->expired = 0;
201+
res = -ECANCELED;
202+
}
203+
121204
if (ctx->ticks) {
122205
ticks = ctx->ticks;
206+
123207
if (ctx->expired && ctx->tintv.tv64) {
124208
/*
125209
* If tintv.tv64 != 0, this is a periodic timer that
@@ -183,6 +267,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
183267
init_waitqueue_head(&ctx->wqh);
184268
ctx->clockid = clockid;
185269
hrtimer_init(&ctx->tmr, clockid, HRTIMER_MODE_ABS);
270+
ctx->moffs = ktime_get_monotonic_offset();
186271

187272
ufd = anon_inode_getfd("[timerfd]", &timerfd_fops, ctx,
188273
O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
@@ -199,6 +284,7 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
199284
struct file *file;
200285
struct timerfd_ctx *ctx;
201286
struct itimerspec ktmr, kotmr;
287+
int ret;
202288

203289
if (copy_from_user(&ktmr, utmr, sizeof(ktmr)))
204290
return -EFAULT;
@@ -213,6 +299,8 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
213299
return PTR_ERR(file);
214300
ctx = file->private_data;
215301

302+
timerfd_setup_cancel(ctx, flags);
303+
216304
/*
217305
* We need to stop the existing timer before reprogramming
218306
* it to the new values.
@@ -240,14 +328,14 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
240328
/*
241329
* Re-program the timer to the new value ...
242330
*/
243-
timerfd_setup(ctx, flags, &ktmr);
331+
ret = timerfd_setup(ctx, flags, &ktmr);
244332

245333
spin_unlock_irq(&ctx->wqh.lock);
246334
fput(file);
247335
if (otmr && copy_to_user(otmr, &kotmr, sizeof(kotmr)))
248336
return -EFAULT;
249337

250-
return 0;
338+
return ret;
251339
}
252340

253341
SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)

‎include/linux/hrtimer.h‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,18 @@ struct hrtimer_sleeper {
143143
*/
144144
struct hrtimer_clock_base {
145145
struct hrtimer_cpu_base *cpu_base;
146-
clockid_t index;
146+
int index;
147+
clockid_t clockid;
147148
struct timerqueue_head active;
148149
ktime_t resolution;
149150
ktime_t (*get_time)(void);
150151
ktime_t softirq_time;
151-
#ifdef CONFIG_HIGH_RES_TIMERS
152152
ktime_t offset;
153-
#endif
154153
};
155154

156155
enum hrtimer_base_type {
157-
HRTIMER_BASE_REALTIME,
158156
HRTIMER_BASE_MONOTONIC,
157+
HRTIMER_BASE_REALTIME,
159158
HRTIMER_BASE_BOOTTIME,
160159
HRTIMER_MAX_CLOCK_BASES,
161160
};
@@ -164,7 +163,7 @@ enum hrtimer_base_type {
164163
* struct hrtimer_cpu_base - the per cpu clock bases
165164
* @lock: lock protecting the base and associated clock bases
166165
* and timers
167-
* @clock_base: array of clock bases for this cpu
166+
* @active_bases: Bitfield to mark bases with active timers
168167
* @expires_next: absolute time of the next event which was scheduled
169168
* via clock_set_next_event()
170169
* @hres_active: State of high resolution mode
@@ -173,10 +172,11 @@ enum hrtimer_base_type {
173172
* @nr_retries: Total number of hrtimer interrupt retries
174173
* @nr_hangs: Total number of hrtimer interrupt hangs
175174
* @max_hang_time: Maximum time spent in hrtimer_interrupt
175+
* @clock_base: array of clock bases for this cpu
176176
*/
177177
struct hrtimer_cpu_base {
178178
raw_spinlock_t lock;
179-
struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
179+
unsigned long active_bases;
180180
#ifdef CONFIG_HIGH_RES_TIMERS
181181
ktime_t expires_next;
182182
int hres_active;
@@ -186,6 +186,7 @@ struct hrtimer_cpu_base {
186186
unsigned long nr_hangs;
187187
ktime_t max_hang_time;
188188
#endif
189+
struct hrtimer_clock_base clock_base[HRTIMER_MAX_CLOCK_BASES];
189190
};
190191

191192
static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
@@ -256,8 +257,6 @@ static inline ktime_t hrtimer_expires_remaining(const struct hrtimer *timer)
256257
#ifdef CONFIG_HIGH_RES_TIMERS
257258
struct clock_event_device;
258259

259-
extern void clock_was_set(void);
260-
extern void hres_timers_resume(void);
261260
extern void hrtimer_interrupt(struct clock_event_device *dev);
262261

263262
/*
@@ -291,16 +290,8 @@ extern void hrtimer_peek_ahead_timers(void);
291290
# define MONOTONIC_RES_NSEC LOW_RES_NSEC
292291
# define KTIME_MONOTONIC_RES KTIME_LOW_RES
293292

294-
/*
295-
* clock_was_set() is a NOP for non- high-resolution systems. The
296-
* time-sorted order guarantees that a timer does not expire early and
297-
* is expired in the next softirq when the clock was advanced.
298-
*/
299-
static inline void clock_was_set(void) { }
300293
static inline void hrtimer_peek_ahead_timers(void) { }
301294

302-
static inline void hres_timers_resume(void) { }
303-
304295
/*
305296
* In non high resolution mode the time reference is taken from
306297
* the base softirq time variable.
@@ -316,10 +307,18 @@ static inline int hrtimer_is_hres_active(struct hrtimer *timer)
316307
}
317308
#endif
318309

310+
extern void clock_was_set(void);
311+
#ifdef CONFIG_TIMERFD
312+
extern void timerfd_clock_was_set(void);
313+
#else
314+
static inline void timerfd_clock_was_set(void) { }
315+
#endif
316+
extern void hrtimers_resume(void);
317+
319318
extern ktime_t ktime_get(void);
320319
extern ktime_t ktime_get_real(void);
321320
extern ktime_t ktime_get_boottime(void);
322-
321+
extern ktime_t ktime_get_monotonic_offset(void);
323322

324323
DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
325324

‎include/linux/thread_info.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct restart_block {
2929
} futex;
3030
/* For nanosleep */
3131
struct {
32-
clockid_t index;
32+
clockid_t clockid;
3333
struct timespec __user *rmtp;
3434
#ifdef CONFIG_COMPAT
3535
struct compat_timespec __user *compat_rmtp;

‎include/linux/timerfd.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
* shared O_* flags.
2020
*/
2121
#define TFD_TIMER_ABSTIME (1 << 0)
22+
#define TFD_TIMER_CANCEL_ON_SET (1 << 1)
2223
#define TFD_CLOEXEC O_CLOEXEC
2324
#define TFD_NONBLOCK O_NONBLOCK
2425

2526
#define TFD_SHARED_FCNTL_FLAGS (TFD_CLOEXEC | TFD_NONBLOCK)
2627
/* Flags for timerfd_create. */
2728
#define TFD_CREATE_FLAGS TFD_SHARED_FCNTL_FLAGS
2829
/* Flags for timerfd_settime. */
29-
#define TFD_SETTIME_FLAGS TFD_TIMER_ABSTIME
30+
#define TFD_SETTIME_FLAGS (TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)
3031

3132
#endif /* _LINUX_TIMERFD_H */

0 commit comments

Comments
 (0)