Skip to content

Commit a760268

Browse files
Al ViroKAGA-KOKO
authored andcommitted
hrtimer: Move copyout of remaining time to do_nanosleep()
The hrtimer nanosleep() implementation can be simplified by moving the copy out of the remaining time to do_nanosleep() which is shared between the real nanosleep function and the restart function. The pointer to the timespec64 which is updated is already stored in the restart block at the call site, so the seperate handling of nanosleep and restart function can be avoided. [ tglx: Added changelog ] Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: John Stultz <john.stultz@linaro.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20170607084241.28657-4-viro@ZenIV.linux.org.uk
1 parent 192a82f commit a760268

1 file changed

Lines changed: 20 additions & 42 deletions

File tree

‎kernel/time/hrtimer.c‎

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
14411441

14421442
static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
14431443
{
1444+
struct timespec __user *rmtp;
14441445
hrtimer_init_sleeper(t, current);
14451446

14461447
do {
@@ -1457,57 +1458,41 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
14571458

14581459
__set_current_state(TASK_RUNNING);
14591460

1460-
return t->task == NULL;
1461-
}
1462-
1463-
static int update_rmtp(struct hrtimer *timer, struct timespec __user *rmtp)
1464-
{
1465-
struct timespec rmt;
1466-
ktime_t rem;
1467-
1468-
rem = hrtimer_expires_remaining(timer);
1469-
if (rem <= 0)
1461+
if (!t->task)
14701462
return 0;
1471-
rmt = ktime_to_timespec(rem);
14721463

1473-
if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1474-
return -EFAULT;
1475-
1476-
return 1;
1464+
rmtp = current->restart_block.nanosleep.rmtp;
1465+
if (rmtp) {
1466+
struct timespec rmt;
1467+
ktime_t rem = hrtimer_expires_remaining(&t->timer);
1468+
if (rem <= 0)
1469+
return 0;
1470+
rmt = ktime_to_timespec(rem);
1471+
1472+
if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1473+
return -EFAULT;
1474+
}
1475+
return -ERESTART_RESTARTBLOCK;
14771476
}
14781477

14791478
long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
14801479
{
14811480
struct hrtimer_sleeper t;
1482-
struct timespec __user *rmtp;
1483-
int ret = 0;
1481+
int ret;
14841482

14851483
hrtimer_init_on_stack(&t.timer, restart->nanosleep.clockid,
14861484
HRTIMER_MODE_ABS);
14871485
hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
14881486

1489-
if (do_nanosleep(&t, HRTIMER_MODE_ABS))
1490-
goto out;
1491-
1492-
rmtp = restart->nanosleep.rmtp;
1493-
if (rmtp) {
1494-
ret = update_rmtp(&t.timer, rmtp);
1495-
if (ret <= 0)
1496-
goto out;
1497-
}
1498-
1499-
/* The other values in restart are already filled in */
1500-
ret = -ERESTART_RESTARTBLOCK;
1501-
out:
1487+
ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
15021488
destroy_hrtimer_on_stack(&t.timer);
15031489
return ret;
15041490
}
15051491

15061492
long hrtimer_nanosleep(struct timespec64 *rqtp,
15071493
const enum hrtimer_mode mode, const clockid_t clockid)
15081494
{
1509-
struct restart_block *restart = &current->restart_block;
1510-
struct timespec __user *rmtp;
1495+
struct restart_block *restart;
15111496
struct hrtimer_sleeper t;
15121497
int ret = 0;
15131498
u64 slack;
@@ -1518,7 +1503,8 @@ long hrtimer_nanosleep(struct timespec64 *rqtp,
15181503

15191504
hrtimer_init_on_stack(&t.timer, clockid, mode);
15201505
hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
1521-
if (do_nanosleep(&t, mode))
1506+
ret = do_nanosleep(&t, mode);
1507+
if (ret != -ERESTART_RESTARTBLOCK)
15221508
goto out;
15231509

15241510
/* Absolute timers do not update the rmtp value and restart: */
@@ -1527,18 +1513,10 @@ long hrtimer_nanosleep(struct timespec64 *rqtp,
15271513
goto out;
15281514
}
15291515

1530-
rmtp = restart->nanosleep.rmtp;
1531-
if (rmtp) {
1532-
ret = update_rmtp(&t.timer, rmtp);
1533-
if (ret <= 0)
1534-
goto out;
1535-
}
1536-
1516+
restart = &current->restart_block;
15371517
restart->fn = hrtimer_nanosleep_restart;
15381518
restart->nanosleep.clockid = t.timer.base->clockid;
15391519
restart->nanosleep.expires = hrtimer_get_expires_tv64(&t.timer);
1540-
1541-
ret = -ERESTART_RESTARTBLOCK;
15421520
out:
15431521
destroy_hrtimer_on_stack(&t.timer);
15441522
return ret;

0 commit comments

Comments
 (0)