Skip to content

Commit ea3b25e

Browse files
committed
Merge branch 'timers-compat' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
Pull timer-related user access updates from Al Viro: "Continuation of timers-related stuff (there had been more, but my parts of that series are already merged via timers/core). This is more of y2038 work by Deepa Dinamani, partially disrupted by the unification of native and compat timers-related syscalls" * 'timers-compat' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: posix_clocks: Use get_itimerspec64() and put_itimerspec64() timerfd: Use get_itimerspec64() and put_itimerspec64() nanosleep: Use get_timespec64() and put_timespec64() posix-timers: Use get_timespec64() and put_timespec64() posix-stubs: Conditionally include COMPAT_SYS_NI defines time: introduce {get,put}_itimerspec64 time: add get_timespec64 and put_timespec64
2 parents 89fbf53 + 725816e commit ea3b25e

12 files changed

Lines changed: 287 additions & 171 deletions

File tree

‎fs/timerfd.c‎

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static ktime_t timerfd_get_remaining(struct timerfd_ctx *ctx)
169169
}
170170

171171
static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
172-
const struct itimerspec *ktmr)
172+
const struct itimerspec64 *ktmr)
173173
{
174174
enum hrtimer_mode htmode;
175175
ktime_t texp;
@@ -178,10 +178,10 @@ static int timerfd_setup(struct timerfd_ctx *ctx, int flags,
178178
htmode = (flags & TFD_TIMER_ABSTIME) ?
179179
HRTIMER_MODE_ABS: HRTIMER_MODE_REL;
180180

181-
texp = timespec_to_ktime(ktmr->it_value);
181+
texp = timespec64_to_ktime(ktmr->it_value);
182182
ctx->expired = 0;
183183
ctx->ticks = 0;
184-
ctx->tintv = timespec_to_ktime(ktmr->it_interval);
184+
ctx->tintv = timespec64_to_ktime(ktmr->it_interval);
185185

186186
if (isalarm(ctx)) {
187187
alarm_init(&ctx->t.alarm,
@@ -432,16 +432,15 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
432432
}
433433

434434
static int do_timerfd_settime(int ufd, int flags,
435-
const struct itimerspec *new,
436-
struct itimerspec *old)
435+
const struct itimerspec64 *new,
436+
struct itimerspec64 *old)
437437
{
438438
struct fd f;
439439
struct timerfd_ctx *ctx;
440440
int ret;
441441

442442
if ((flags & ~TFD_SETTIME_FLAGS) ||
443-
!timespec_valid(&new->it_value) ||
444-
!timespec_valid(&new->it_interval))
443+
!itimerspec64_valid(new))
445444
return -EINVAL;
446445

447446
ret = timerfd_fget(ufd, &f);
@@ -487,8 +486,8 @@ static int do_timerfd_settime(int ufd, int flags,
487486
hrtimer_forward_now(&ctx->t.tmr, ctx->tintv);
488487
}
489488

490-
old->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
491-
old->it_interval = ktime_to_timespec(ctx->tintv);
489+
old->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
490+
old->it_interval = ktime_to_timespec64(ctx->tintv);
492491

493492
/*
494493
* Re-program the timer to the new value ...
@@ -500,7 +499,7 @@ static int do_timerfd_settime(int ufd, int flags,
500499
return ret;
501500
}
502501

503-
static int do_timerfd_gettime(int ufd, struct itimerspec *t)
502+
static int do_timerfd_gettime(int ufd, struct itimerspec64 *t)
504503
{
505504
struct fd f;
506505
struct timerfd_ctx *ctx;
@@ -525,8 +524,8 @@ static int do_timerfd_gettime(int ufd, struct itimerspec *t)
525524
hrtimer_restart(&ctx->t.tmr);
526525
}
527526
}
528-
t->it_value = ktime_to_timespec(timerfd_get_remaining(ctx));
529-
t->it_interval = ktime_to_timespec(ctx->tintv);
527+
t->it_value = ktime_to_timespec64(timerfd_get_remaining(ctx));
528+
t->it_interval = ktime_to_timespec64(ctx->tintv);
530529
spin_unlock_irq(&ctx->wqh.lock);
531530
fdput(f);
532531
return 0;
@@ -536,54 +535,54 @@ SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
536535
const struct itimerspec __user *, utmr,
537536
struct itimerspec __user *, otmr)
538537
{
539-
struct itimerspec new, old;
538+
struct itimerspec64 new, old;
540539
int ret;
541540

542-
if (copy_from_user(&new, utmr, sizeof(new)))
541+
if (get_itimerspec64(&new, utmr))
543542
return -EFAULT;
544543
ret = do_timerfd_settime(ufd, flags, &new, &old);
545544
if (ret)
546545
return ret;
547-
if (otmr && copy_to_user(otmr, &old, sizeof(old)))
546+
if (otmr && put_itimerspec64(&old, otmr))
548547
return -EFAULT;
549548

550549
return ret;
551550
}
552551

553552
SYSCALL_DEFINE2(timerfd_gettime, int, ufd, struct itimerspec __user *, otmr)
554553
{
555-
struct itimerspec kotmr;
554+
struct itimerspec64 kotmr;
556555
int ret = do_timerfd_gettime(ufd, &kotmr);
557556
if (ret)
558557
return ret;
559-
return copy_to_user(otmr, &kotmr, sizeof(kotmr)) ? -EFAULT: 0;
558+
return put_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
560559
}
561560

562561
#ifdef CONFIG_COMPAT
563562
COMPAT_SYSCALL_DEFINE4(timerfd_settime, int, ufd, int, flags,
564563
const struct compat_itimerspec __user *, utmr,
565564
struct compat_itimerspec __user *, otmr)
566565
{
567-
struct itimerspec new, old;
566+
struct itimerspec64 new, old;
568567
int ret;
569568

570-
if (get_compat_itimerspec(&new, utmr))
569+
if (get_compat_itimerspec64(&new, utmr))
571570
return -EFAULT;
572571
ret = do_timerfd_settime(ufd, flags, &new, &old);
573572
if (ret)
574573
return ret;
575-
if (otmr && put_compat_itimerspec(otmr, &old))
574+
if (otmr && put_compat_itimerspec64(&old, otmr))
576575
return -EFAULT;
577576
return ret;
578577
}
579578

580579
COMPAT_SYSCALL_DEFINE2(timerfd_gettime, int, ufd,
581580
struct compat_itimerspec __user *, otmr)
582581
{
583-
struct itimerspec kotmr;
582+
struct itimerspec64 kotmr;
584583
int ret = do_timerfd_gettime(ufd, &kotmr);
585584
if (ret)
586585
return ret;
587-
return put_compat_itimerspec(otmr, &kotmr) ? -EFAULT: 0;
586+
return put_compat_itimerspec64(&kotmr, otmr) ? -EFAULT : 0;
588587
}
589588
#endif

‎include/linux/compat.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ extern int compat_get_timespec(struct timespec *, const void __user *);
164164
extern int compat_put_timespec(const struct timespec *, void __user *);
165165
extern int compat_get_timeval(struct timeval *, const void __user *);
166166
extern int compat_put_timeval(const struct timeval *, void __user *);
167+
extern int compat_get_timespec64(struct timespec64 *, const void __user *);
168+
extern int compat_put_timespec64(const struct timespec64 *, void __user *);
169+
extern int get_compat_itimerspec64(struct itimerspec64 *its,
170+
const struct compat_itimerspec __user *uits);
171+
extern int put_compat_itimerspec64(const struct itimerspec64 *its,
172+
struct compat_itimerspec __user *uits);
167173

168174
/*
169175
* This function convert a timespec if necessary and returns a *user

‎include/linux/hrtimer.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ static inline u64 hrtimer_forward_now(struct hrtimer *timer,
453453

454454
/* Precise sleep: */
455455

456-
extern int nanosleep_copyout(struct restart_block *, struct timespec *);
456+
extern int nanosleep_copyout(struct restart_block *, struct timespec64 *);
457457
extern long hrtimer_nanosleep(const struct timespec64 *rqtp,
458458
const enum hrtimer_mode mode,
459459
const clockid_t clockid);

‎include/linux/posix-timers.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,4 @@ void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
113113
void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
114114

115115
void posixtimer_rearm(struct siginfo *info);
116-
117116
#endif

‎include/linux/time.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88

99
extern struct timezone sys_tz;
1010

11+
int get_timespec64(struct timespec64 *ts,
12+
const struct timespec __user *uts);
13+
int put_timespec64(const struct timespec64 *ts,
14+
struct timespec __user *uts);
15+
int get_itimerspec64(struct itimerspec64 *it,
16+
const struct itimerspec __user *uit);
17+
int put_itimerspec64(const struct itimerspec64 *it,
18+
struct itimerspec __user *uit);
19+
1120
#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
1221

1322
static inline int timespec_equal(const struct timespec *a,
@@ -270,4 +279,13 @@ static __always_inline void timespec_add_ns(struct timespec *a, u64 ns)
270279
a->tv_nsec = ns;
271280
}
272281

282+
static inline bool itimerspec64_valid(const struct itimerspec64 *its)
283+
{
284+
if (!timespec64_valid(&(its->it_interval)) ||
285+
!timespec64_valid(&(its->it_value)))
286+
return false;
287+
288+
return true;
289+
}
290+
273291
#endif

‎kernel/compat.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,50 @@ static int __compat_put_timespec(const struct timespec *ts, struct compat_timesp
120120
__put_user(ts->tv_nsec, &cts->tv_nsec)) ? -EFAULT : 0;
121121
}
122122

123+
static int __compat_get_timespec64(struct timespec64 *ts64,
124+
const struct compat_timespec __user *cts)
125+
{
126+
struct compat_timespec ts;
127+
int ret;
128+
129+
ret = copy_from_user(&ts, cts, sizeof(ts));
130+
if (ret)
131+
return -EFAULT;
132+
133+
ts64->tv_sec = ts.tv_sec;
134+
ts64->tv_nsec = ts.tv_nsec;
135+
136+
return 0;
137+
}
138+
139+
static int __compat_put_timespec64(const struct timespec64 *ts64,
140+
struct compat_timespec __user *cts)
141+
{
142+
struct compat_timespec ts = {
143+
.tv_sec = ts64->tv_sec,
144+
.tv_nsec = ts64->tv_nsec
145+
};
146+
return copy_to_user(cts, &ts, sizeof(ts)) ? -EFAULT : 0;
147+
}
148+
149+
int compat_get_timespec64(struct timespec64 *ts, const void __user *uts)
150+
{
151+
if (COMPAT_USE_64BIT_TIME)
152+
return copy_from_user(ts, uts, sizeof(*ts)) ? -EFAULT : 0;
153+
else
154+
return __compat_get_timespec64(ts, uts);
155+
}
156+
EXPORT_SYMBOL_GPL(compat_get_timespec64);
157+
158+
int compat_put_timespec64(const struct timespec64 *ts, void __user *uts)
159+
{
160+
if (COMPAT_USE_64BIT_TIME)
161+
return copy_to_user(uts, ts, sizeof(*ts)) ? -EFAULT : 0;
162+
else
163+
return __compat_put_timespec64(ts, uts);
164+
}
165+
EXPORT_SYMBOL_GPL(compat_put_timespec64);
166+
123167
int compat_get_timeval(struct timeval *tv, const void __user *utv)
124168
{
125169
if (COMPAT_USE_64BIT_TIME)
@@ -476,6 +520,27 @@ int put_compat_itimerspec(struct compat_itimerspec __user *dst,
476520
return 0;
477521
}
478522

523+
int get_compat_itimerspec64(struct itimerspec64 *its,
524+
const struct compat_itimerspec __user *uits)
525+
{
526+
527+
if (__compat_get_timespec64(&its->it_interval, &uits->it_interval) ||
528+
__compat_get_timespec64(&its->it_value, &uits->it_value))
529+
return -EFAULT;
530+
return 0;
531+
}
532+
EXPORT_SYMBOL_GPL(get_compat_itimerspec64);
533+
534+
int put_compat_itimerspec64(const struct itimerspec64 *its,
535+
struct compat_itimerspec __user *uits)
536+
{
537+
if (__compat_put_timespec64(&its->it_interval, &uits->it_interval) ||
538+
__compat_put_timespec64(&its->it_value, &uits->it_value))
539+
return -EFAULT;
540+
return 0;
541+
}
542+
EXPORT_SYMBOL_GPL(put_compat_itimerspec64);
543+
479544
/*
480545
* We currently only need the following fields from the sigevent
481546
* structure: sigev_value, sigev_signo, sig_notify and (sometimes

‎kernel/time/alarmtimer.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,14 +712,14 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
712712
alarmtimer_freezerset(absexp, type);
713713
restart = &current->restart_block;
714714
if (restart->nanosleep.type != TT_NONE) {
715-
struct timespec rmt;
715+
struct timespec64 rmt;
716716
ktime_t rem;
717717

718718
rem = ktime_sub(absexp, alarm_bases[type].gettime());
719719

720720
if (rem <= 0)
721721
return 0;
722-
rmt = ktime_to_timespec(rem);
722+
rmt = ktime_to_timespec64(rem);
723723

724724
return nanosleep_copyout(restart, &rmt);
725725
}

‎kernel/time/hrtimer.c‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,17 +1440,17 @@ void hrtimer_init_sleeper(struct hrtimer_sleeper *sl, struct task_struct *task)
14401440
}
14411441
EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
14421442

1443-
int nanosleep_copyout(struct restart_block *restart, struct timespec *ts)
1443+
int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
14441444
{
14451445
switch(restart->nanosleep.type) {
14461446
#ifdef CONFIG_COMPAT
14471447
case TT_COMPAT:
1448-
if (compat_put_timespec(ts, restart->nanosleep.compat_rmtp))
1448+
if (compat_put_timespec64(ts, restart->nanosleep.compat_rmtp))
14491449
return -EFAULT;
14501450
break;
14511451
#endif
14521452
case TT_NATIVE:
1453-
if (copy_to_user(restart->nanosleep.rmtp, ts, sizeof(struct timespec)))
1453+
if (put_timespec64(ts, restart->nanosleep.rmtp))
14541454
return -EFAULT;
14551455
break;
14561456
default:
@@ -1485,11 +1485,11 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
14851485
restart = &current->restart_block;
14861486
if (restart->nanosleep.type != TT_NONE) {
14871487
ktime_t rem = hrtimer_expires_remaining(&t->timer);
1488-
struct timespec rmt;
1488+
struct timespec64 rmt;
14891489

14901490
if (rem <= 0)
14911491
return 0;
1492-
rmt = ktime_to_timespec(rem);
1492+
rmt = ktime_to_timespec64(rem);
14931493

14941494
return nanosleep_copyout(restart, &rmt);
14951495
}
@@ -1546,39 +1546,35 @@ long hrtimer_nanosleep(const struct timespec64 *rqtp,
15461546
SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
15471547
struct timespec __user *, rmtp)
15481548
{
1549-
struct timespec64 tu64;
1550-
struct timespec tu;
1549+
struct timespec64 tu;
15511550

1552-
if (copy_from_user(&tu, rqtp, sizeof(tu)))
1551+
if (get_timespec64(&tu, rqtp))
15531552
return -EFAULT;
15541553

1555-
tu64 = timespec_to_timespec64(tu);
1556-
if (!timespec64_valid(&tu64))
1554+
if (!timespec64_valid(&tu))
15571555
return -EINVAL;
15581556

15591557
current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
15601558
current->restart_block.nanosleep.rmtp = rmtp;
1561-
return hrtimer_nanosleep(&tu64, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1559+
return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
15621560
}
15631561

15641562
#ifdef CONFIG_COMPAT
15651563

15661564
COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
15671565
struct compat_timespec __user *, rmtp)
15681566
{
1569-
struct timespec64 tu64;
1570-
struct timespec tu;
1567+
struct timespec64 tu;
15711568

1572-
if (compat_get_timespec(&tu, rqtp))
1569+
if (compat_get_timespec64(&tu, rqtp))
15731570
return -EFAULT;
15741571

1575-
tu64 = timespec_to_timespec64(tu);
1576-
if (!timespec64_valid(&tu64))
1572+
if (!timespec64_valid(&tu))
15771573
return -EINVAL;
15781574

15791575
current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
15801576
current->restart_block.nanosleep.compat_rmtp = rmtp;
1581-
return hrtimer_nanosleep(&tu64, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1577+
return hrtimer_nanosleep(&tu, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
15821578
}
15831579
#endif
15841580

‎kernel/time/posix-cpu-timers.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,12 +1318,8 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
13181318
*/
13191319
restart = &current->restart_block;
13201320
restart->nanosleep.expires = expires;
1321-
if (restart->nanosleep.type != TT_NONE) {
1322-
struct timespec ts;
1323-
1324-
ts = timespec64_to_timespec(it.it_value);
1325-
error = nanosleep_copyout(restart, &ts);
1326-
}
1321+
if (restart->nanosleep.type != TT_NONE)
1322+
error = nanosleep_copyout(restart, &it.it_value);
13271323
}
13281324

13291325
return error;

0 commit comments

Comments
 (0)