Skip to content

Commit edbeda4

Browse files
Al ViroKAGA-KOKO
authored andcommitted
time/posix-timers: Move the compat copyouts to the nanosleep implementations
Turn restart_block.nanosleep.{rmtp,compat_rmtp} into a tagged union (kind = 1 -> native, kind = 2 -> compat, kind = 0 -> nothing) and make the places doing actual copyout handle compat as well as native (that will become a helper in the next commit). Result: compat wrappers, messing with reassignments, etc. are gone. [ tglx: Folded in a variant of Peter Zijlstras enum patch ] 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-6-viro@ZenIV.linux.org.uk
1 parent 99e6c0e commit edbeda4

8 files changed

Lines changed: 142 additions & 171 deletions

File tree

‎include/linux/posix-timers.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ void posix_cpu_timers_exit_group(struct task_struct *task);
110110
void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
111111
u64 *newval, u64 *oldval);
112112

113-
long clock_nanosleep_restart(struct restart_block *restart_block);
114-
115113
void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
116114

117115
void posixtimer_rearm(struct siginfo *info);

‎include/linux/restart_block.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ struct timespec;
1111
struct compat_timespec;
1212
struct pollfd;
1313

14+
enum timespec_type {
15+
TT_NONE = 0,
16+
TT_NATIVE = 1,
17+
#ifdef CONFIG_COMPAT
18+
TT_COMPAT = 2,
19+
#endif
20+
};
21+
1422
/*
1523
* System call restart block.
1624
*/
@@ -29,10 +37,13 @@ struct restart_block {
2937
/* For nanosleep */
3038
struct {
3139
clockid_t clockid;
32-
struct timespec __user *rmtp;
40+
enum timespec_type type;
41+
union {
42+
struct timespec __user *rmtp;
3343
#ifdef CONFIG_COMPAT
34-
struct compat_timespec __user *compat_rmtp;
44+
struct compat_timespec __user *compat_rmtp;
3545
#endif
46+
};
3647
u64 expires;
3748
} nanosleep;
3849
/* For poll */

‎kernel/compat.c‎

Lines changed: 0 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -213,82 +213,6 @@ int compat_convert_timespec(struct timespec __user **kts,
213213
return 0;
214214
}
215215

216-
static long compat_nanosleep_restart(struct restart_block *restart)
217-
{
218-
struct compat_timespec __user *rmtp;
219-
struct timespec rmt;
220-
mm_segment_t oldfs;
221-
long ret;
222-
223-
restart->nanosleep.rmtp = (struct timespec __user *) &rmt;
224-
oldfs = get_fs();
225-
set_fs(KERNEL_DS);
226-
ret = hrtimer_nanosleep_restart(restart);
227-
set_fs(oldfs);
228-
229-
if (ret == -ERESTART_RESTARTBLOCK) {
230-
rmtp = restart->nanosleep.compat_rmtp;
231-
232-
if (rmtp && compat_put_timespec(&rmt, rmtp))
233-
return -EFAULT;
234-
}
235-
236-
return ret;
237-
}
238-
239-
COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
240-
struct compat_timespec __user *, rmtp)
241-
{
242-
struct timespec tu, rmt;
243-
struct timespec64 tu64;
244-
mm_segment_t oldfs;
245-
long ret;
246-
247-
if (compat_get_timespec(&tu, rqtp))
248-
return -EFAULT;
249-
250-
tu64 = timespec_to_timespec64(tu);
251-
if (!timespec64_valid(&tu64))
252-
return -EINVAL;
253-
254-
oldfs = get_fs();
255-
set_fs(KERNEL_DS);
256-
current->restart_block.nanosleep.rmtp =
257-
rmtp ? (struct timespec __user *)&rmt : NULL;
258-
ret = hrtimer_nanosleep(&tu64, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
259-
set_fs(oldfs);
260-
261-
/*
262-
* hrtimer_nanosleep() can only return 0 or
263-
* -ERESTART_RESTARTBLOCK here because:
264-
*
265-
* - we call it with HRTIMER_MODE_REL and therefor exclude the
266-
* -ERESTARTNOHAND return path.
267-
*
268-
* - we supply the rmtp argument from the task stack (due to
269-
* the necessary compat conversion. So the update cannot
270-
* fail, which excludes the -EFAULT return path as well. If
271-
* it fails nevertheless we have a bigger problem and wont
272-
* reach this place anymore.
273-
*
274-
* - if the return value is 0, we do not have to update rmtp
275-
* because there is no remaining time.
276-
*
277-
* We check for -ERESTART_RESTARTBLOCK nevertheless if the
278-
* core implementation decides to return random nonsense.
279-
*/
280-
if (ret == -ERESTART_RESTARTBLOCK) {
281-
struct restart_block *restart = &current->restart_block;
282-
283-
restart->fn = compat_nanosleep_restart;
284-
restart->nanosleep.compat_rmtp = rmtp;
285-
286-
if (rmtp && compat_put_timespec(&rmt, rmtp))
287-
return -EFAULT;
288-
}
289-
return ret;
290-
}
291-
292216
static inline long get_compat_itimerval(struct itimerval *o,
293217
struct compat_itimerval __user *i)
294218
{
@@ -821,61 +745,6 @@ COMPAT_SYSCALL_DEFINE2(clock_getres, clockid_t, which_clock,
821745
return err;
822746
}
823747

824-
static long compat_clock_nanosleep_restart(struct restart_block *restart)
825-
{
826-
long err;
827-
mm_segment_t oldfs;
828-
struct timespec tu;
829-
struct compat_timespec __user *rmtp = restart->nanosleep.compat_rmtp;
830-
831-
restart->nanosleep.rmtp = (struct timespec __user *) &tu;
832-
oldfs = get_fs();
833-
set_fs(KERNEL_DS);
834-
err = clock_nanosleep_restart(restart);
835-
set_fs(oldfs);
836-
837-
if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
838-
compat_put_timespec(&tu, rmtp))
839-
return -EFAULT;
840-
841-
if (err == -ERESTART_RESTARTBLOCK) {
842-
restart->fn = compat_clock_nanosleep_restart;
843-
restart->nanosleep.compat_rmtp = rmtp;
844-
}
845-
return err;
846-
}
847-
848-
COMPAT_SYSCALL_DEFINE4(clock_nanosleep, clockid_t, which_clock, int, flags,
849-
struct compat_timespec __user *, rqtp,
850-
struct compat_timespec __user *, rmtp)
851-
{
852-
long err;
853-
mm_segment_t oldfs;
854-
struct timespec in, out;
855-
struct restart_block *restart;
856-
857-
if (compat_get_timespec(&in, rqtp))
858-
return -EFAULT;
859-
860-
oldfs = get_fs();
861-
set_fs(KERNEL_DS);
862-
err = sys_clock_nanosleep(which_clock, flags,
863-
(struct timespec __user *) &in,
864-
(struct timespec __user *) &out);
865-
set_fs(oldfs);
866-
867-
if ((err == -ERESTART_RESTARTBLOCK) && rmtp &&
868-
compat_put_timespec(&out, rmtp))
869-
return -EFAULT;
870-
871-
if (err == -ERESTART_RESTARTBLOCK) {
872-
restart = &current->restart_block;
873-
restart->fn = compat_clock_nanosleep_restart;
874-
restart->nanosleep.compat_rmtp = rmtp;
875-
}
876-
return err;
877-
}
878-
879748
/*
880749
* We currently only need the following fields from the sigevent
881750
* structure: sigev_value, sigev_signo, sig_notify and (sometimes

‎kernel/time/alarmtimer.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <linux/posix-timers.h>
2828
#include <linux/workqueue.h>
2929
#include <linux/freezer.h>
30+
#include <linux/compat.h>
3031

3132
#include "posix-timers.h"
3233

@@ -691,7 +692,7 @@ static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
691692
static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
692693
enum alarmtimer_type type)
693694
{
694-
struct timespec __user *rmtp;
695+
struct restart_block *restart;
695696
alarm->data = (void *)current;
696697
do {
697698
set_current_state(TASK_INTERRUPTIBLE);
@@ -709,8 +710,8 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
709710

710711
if (freezing(current))
711712
alarmtimer_freezerset(absexp, type);
712-
rmtp = current->restart_block.nanosleep.rmtp;
713-
if (rmtp) {
713+
restart = &current->restart_block;
714+
if (restart->nanosleep.type != TT_NONE) {
714715
struct timespec rmt;
715716
ktime_t rem;
716717

@@ -720,7 +721,14 @@ static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp,
720721
return 0;
721722
rmt = ktime_to_timespec(rem);
722723

723-
if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
724+
#ifdef CONFIG_COMPAT
725+
if (restart->nanosleep.type == TT_COMPAT) {
726+
if (compat_put_timespec(&rmt,
727+
restart->nanosleep.compat_rmtp))
728+
return -EFAULT;
729+
} else
730+
#endif
731+
if (copy_to_user(restart->nanosleep.rmtp, &rmt, sizeof(rmt)))
724732
return -EFAULT;
725733
}
726734
return -ERESTART_RESTARTBLOCK;

‎kernel/time/hrtimer.c‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <linux/sched/debug.h>
5252
#include <linux/timer.h>
5353
#include <linux/freezer.h>
54+
#include <linux/compat.h>
5455

5556
#include <linux/uaccess.h>
5657

@@ -1441,7 +1442,8 @@ EXPORT_SYMBOL_GPL(hrtimer_init_sleeper);
14411442

14421443
static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
14431444
{
1444-
struct timespec __user *rmtp;
1445+
struct restart_block *restart;
1446+
14451447
hrtimer_init_sleeper(t, current);
14461448

14471449
do {
@@ -1461,15 +1463,23 @@ static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mod
14611463
if (!t->task)
14621464
return 0;
14631465

1464-
rmtp = current->restart_block.nanosleep.rmtp;
1465-
if (rmtp) {
1466-
struct timespec rmt;
1466+
restart = &current->restart_block;
1467+
if (restart->nanosleep.type != TT_NONE) {
14671468
ktime_t rem = hrtimer_expires_remaining(&t->timer);
1469+
struct timespec rmt;
1470+
14681471
if (rem <= 0)
14691472
return 0;
14701473
rmt = ktime_to_timespec(rem);
14711474

1472-
if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
1475+
#ifdef CONFIG_COMPAT
1476+
if (restart->nanosleep.type == TT_COMPAT) {
1477+
if (compat_put_timespec(&rmt,
1478+
restart->nanosleep.compat_rmtp))
1479+
return -EFAULT;
1480+
} else
1481+
#endif
1482+
if (copy_to_user(restart->nanosleep.rmtp, &rmt, sizeof(rmt)))
14731483
return -EFAULT;
14741484
}
14751485
return -ERESTART_RESTARTBLOCK;
@@ -1535,10 +1545,32 @@ SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
15351545
if (!timespec64_valid(&tu64))
15361546
return -EINVAL;
15371547

1548+
current->restart_block.nanosleep.type = rmtp ? TT_NATIVE : TT_NONE;
15381549
current->restart_block.nanosleep.rmtp = rmtp;
15391550
return hrtimer_nanosleep(&tu64, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
15401551
}
15411552

1553+
#ifdef CONFIG_COMPAT
1554+
1555+
COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
1556+
struct compat_timespec __user *, rmtp)
1557+
{
1558+
struct timespec64 tu64;
1559+
struct timespec tu;
1560+
1561+
if (compat_get_timespec(&tu, rqtp))
1562+
return -EFAULT;
1563+
1564+
tu64 = timespec_to_timespec64(tu);
1565+
if (!timespec64_valid(&tu64))
1566+
return -EINVAL;
1567+
1568+
current->restart_block.nanosleep.type = rmtp ? TT_COMPAT : TT_NONE;
1569+
current->restart_block.nanosleep.compat_rmtp = rmtp;
1570+
return hrtimer_nanosleep(&tu64, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1571+
}
1572+
#endif
1573+
15421574
/*
15431575
* Functions related to boot-time initialization:
15441576
*/

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <trace/events/timer.h>
1313
#include <linux/tick.h>
1414
#include <linux/workqueue.h>
15+
#include <linux/compat.h>
1516

1617
#include "posix-timers.h"
1718

@@ -1243,10 +1244,9 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
12431244
timer.it_process = current;
12441245
if (!error) {
12451246
static struct itimerspec64 zero_it;
1246-
struct restart_block *restart = &current->restart_block;
1247-
struct timespec __user *rmtp;
1247+
struct restart_block *restart;
12481248

1249-
memset(&it, 0, sizeof it);
1249+
memset(&it, 0, sizeof(it));
12501250
it.it_value = *rqtp;
12511251

12521252
spin_lock_irq(&timer.it_lock);
@@ -1311,12 +1311,20 @@ static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
13111311
/*
13121312
* Report back to the user the time still remaining.
13131313
*/
1314-
rmtp = restart->nanosleep.rmtp;
1315-
if (rmtp) {
1314+
restart = &current->restart_block;
1315+
if (restart->nanosleep.type != TT_NONE) {
13161316
struct timespec ts;
13171317

13181318
ts = timespec64_to_timespec(it.it_value);
1319-
if (copy_to_user(rmtp, &ts, sizeof(*rmtp)))
1319+
#ifdef CONFIG_COMPAT
1320+
if (restart->nanosleep.type == TT_COMPAT) {
1321+
if (compat_put_timespec(&ts,
1322+
restart->nanosleep.compat_rmtp))
1323+
return -EFAULT;
1324+
} else
1325+
#endif
1326+
if (copy_to_user(restart->nanosleep.rmtp, &ts,
1327+
sizeof(ts)))
13201328
return -EFAULT;
13211329
}
13221330
restart->nanosleep.expires = timespec64_to_ns(rqtp);

0 commit comments

Comments
 (0)