Skip to content

Commit c0edd7c

Browse files
deepa-hubAl Viro
authored andcommitted
nanosleep: Use get_timespec64() and put_timespec64()
Usage of these apis and their compat versions makes the syscalls: clock_nanosleep and nanosleep and their compat implementations simpler. This is a preparatory patch to isolate data conversions to struct timespec64 at userspace boundaries. This helps contain the changes needed to transition to new y2038 safe types. Signed-off-by: Deepa Dinamani <deepa.kernel@gmail.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 5c49941 commit c0edd7c

5 files changed

Lines changed: 26 additions & 38 deletions

File tree

‎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);

‎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
@@ -1314,12 +1314,8 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
13141314
*/
13151315
restart = &current->restart_block;
13161316
restart->nanosleep.expires = expires;
1317-
if (restart->nanosleep.type != TT_NONE) {
1318-
struct timespec ts;
1319-
1320-
ts = timespec64_to_timespec(it.it_value);
1321-
error = nanosleep_copyout(restart, &ts);
1322-
}
1317+
if (restart->nanosleep.type != TT_NONE)
1318+
error = nanosleep_copyout(restart, &it.it_value);
13231319
}
13241320

13251321
return error;

‎kernel/time/posix-timers.c‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,26 +1213,24 @@ SYSCALL_DEFINE4(clock_nanosleep, const clockid_t, which_clock, int, flags,
12131213
struct timespec __user *, rmtp)
12141214
{
12151215
const struct k_clock *kc = clockid_to_kclock(which_clock);
1216-
struct timespec64 t64;
1217-
struct timespec t;
1216+
struct timespec64 t;
12181217

12191218
if (!kc)
12201219
return -EINVAL;
12211220
if (!kc->nsleep)
12221221
return -ENANOSLEEP_NOTSUP;
12231222

1224-
if (copy_from_user(&t, rqtp, sizeof (struct timespec)))
1223+
if (get_timespec64(&t, rqtp))
12251224
return -EFAULT;
12261225

1227-
t64 = timespec_to_timespec64(t);
1228-
if (!timespec64_valid(&t64))
1226+
if (!timespec64_valid(&t))
12291227
return -EINVAL;
12301228
if (flags & TIMER_ABSTIME)
12311229
rmtp = NULL;
12321230
current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
12331231
current->restart_block.nanosleep.rmtp = rmtp;
12341232

1235-
return kc->nsleep(which_clock, flags, &t64);
1233+
return kc->nsleep(which_clock, flags, &t);
12361234
}
12371235

12381236
#ifdef CONFIG_COMPAT
@@ -1241,26 +1239,24 @@ COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
12411239
struct compat_timespec __user *, rmtp)
12421240
{
12431241
const struct k_clock *kc = clockid_to_kclock(which_clock);
1244-
struct timespec64 t64;
1245-
struct timespec t;
1242+
struct timespec64 t;
12461243

12471244
if (!kc)
12481245
return -EINVAL;
12491246
if (!kc->nsleep)
12501247
return -ENANOSLEEP_NOTSUP;
12511248

1252-
if (compat_get_timespec(&t, rqtp))
1249+
if (compat_get_timespec64(&t, rqtp))
12531250
return -EFAULT;
12541251

1255-
t64 = timespec_to_timespec64(t);
1256-
if (!timespec64_valid(&t64))
1252+
if (!timespec64_valid(&t))
12571253
return -EINVAL;
12581254
if (flags & TIMER_ABSTIME)
12591255
rmtp = NULL;
12601256
current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
12611257
current->restart_block.nanosleep.compat_rmtp = rmtp;
12621258

1263-
return kc->nsleep(which_clock, flags, &t64);
1259+
return kc->nsleep(which_clock, flags, &t);
12641260
}
12651261
#endif
12661262

0 commit comments

Comments
 (0)