* [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout()
@ 2018-03-16 14:21 Sebastian Andrzej Siewior
2018-03-16 14:21 ` [RT PATCH 2/2] Revert "cpu_chill: Add a UNINTERRUPTIBLE hrtimer_nanosleep" Sebastian Andrzej Siewior
2018-03-16 15:00 ` [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout() Thomas Gleixner
0 siblings, 2 replies; 4+ messages in thread
From: Sebastian Andrzej Siewior @ 2018-03-16 14:21 UTC (permalink / raw)
To: linux-rt-users; +Cc: linux-kernel, tglx, Steven Rostedt
cpu_chill() uses __hrtimer_nanosleep() with a hack to get
TASK_UNINTERRUPTIBLE state. It seems to work but it does not.
The problem is that we may have a signal pending and receive a sporadic
wake up _before_ the timer expires. At that point the code will look at
->restart_block and use it based on its value from the previous syscall
resulting in a possible copy-to-user.
Instead all this trouble, we could use schedule_hrtimeout() which is not
an user-space interface and does not have this side effects.
Cc: stable-rt@vger.kernel.org
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
kernel/time/hrtimer.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 6c77643eaf02..c0efc22ba635 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1873,14 +1873,13 @@ COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
*/
void cpu_chill(void)
{
- struct timespec64 tu = {
- .tv_nsec = NSEC_PER_MSEC,
- };
+ ktime_t chill_time;
unsigned int freeze_flag = current->flags & PF_NOFREEZE;
+ chill_time = ktime_set(0, NSEC_PER_MSEC);
+ set_current_state(TASK_UNINTERRUPTIBLE);
current->flags |= PF_NOFREEZE;
- __hrtimer_nanosleep(&tu, HRTIMER_MODE_REL_HARD, CLOCK_MONOTONIC,
- TASK_UNINTERRUPTIBLE);
+ schedule_hrtimeout(&chill_time, HRTIMER_MODE_REL_HARD);
if (!freeze_flag)
current->flags &= ~PF_NOFREEZE;
}
--
2.16.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [RT PATCH 2/2] Revert "cpu_chill: Add a UNINTERRUPTIBLE hrtimer_nanosleep"
2018-03-16 14:21 [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout() Sebastian Andrzej Siewior
@ 2018-03-16 14:21 ` Sebastian Andrzej Siewior
2018-03-16 15:00 ` [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout() Thomas Gleixner
1 sibling, 0 replies; 4+ messages in thread
From: Sebastian Andrzej Siewior @ 2018-03-16 14:21 UTC (permalink / raw)
To: linux-rt-users; +Cc: linux-kernel, tglx, Steven Rostedt
This reverts commit "cpu_chill: Add a UNINTERRUPTIBLE
hrtimer_nanosleep". Since cpu_chill() is now using schedule_hrtimeout()
we can remove that change since we have no users for it.
Cc: stable-rt@vger.kernel.org
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
kernel/time/hrtimer.c | 21 ++++++---------------
1 file changed, 6 insertions(+), 15 deletions(-)
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index c0efc22ba635..ce2c2d04cbaa 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1742,13 +1742,12 @@ int nanosleep_copyout(struct restart_block *restart, struct timespec64 *ts)
return -ERESTART_RESTARTBLOCK;
}
-static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode,
- unsigned long state)
+static int __sched do_nanosleep(struct hrtimer_sleeper *t, enum hrtimer_mode mode)
{
struct restart_block *restart;
do {
- set_current_state(state);
+ set_current_state(TASK_INTERRUPTIBLE);
hrtimer_start_expires(&t->timer, mode);
if (likely(t->task))
@@ -1786,15 +1785,13 @@ static long __sched hrtimer_nanosleep_restart(struct restart_block *restart)
hrtimer_init_sleeper_on_stack(&t, restart->nanosleep.clockid,
HRTIMER_MODE_ABS, current);
hrtimer_set_expires_tv64(&t.timer, restart->nanosleep.expires);
- /* cpu_chill() does not care about restart state. */
- ret = do_nanosleep(&t, HRTIMER_MODE_ABS, TASK_INTERRUPTIBLE);
+ ret = do_nanosleep(&t, HRTIMER_MODE_ABS);
destroy_hrtimer_on_stack(&t.timer);
return ret;
}
-static long __hrtimer_nanosleep(const struct timespec64 *rqtp,
- const enum hrtimer_mode mode, const clockid_t clockid,
- unsigned long state)
+long hrtimer_nanosleep(const struct timespec64 *rqtp,
+ const enum hrtimer_mode mode, const clockid_t clockid)
{
struct restart_block *restart;
struct hrtimer_sleeper t;
@@ -1807,7 +1804,7 @@ static long __hrtimer_nanosleep(const struct timespec64 *rqtp,
hrtimer_init_sleeper_on_stack(&t, clockid, mode, current);
hrtimer_set_expires_range_ns(&t.timer, timespec64_to_ktime(*rqtp), slack);
- ret = do_nanosleep(&t, mode, state);
+ ret = do_nanosleep(&t, mode);
if (ret != -ERESTART_RESTARTBLOCK)
goto out;
@@ -1826,12 +1823,6 @@ static long __hrtimer_nanosleep(const struct timespec64 *rqtp,
return ret;
}
-long hrtimer_nanosleep(const struct timespec64 *rqtp,
- const enum hrtimer_mode mode, const clockid_t clockid)
-{
- return __hrtimer_nanosleep(rqtp, mode, clockid, TASK_INTERRUPTIBLE);
-}
-
SYSCALL_DEFINE2(nanosleep, struct timespec __user *, rqtp,
struct timespec __user *, rmtp)
{
--
2.16.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout()
2018-03-16 14:21 [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout() Sebastian Andrzej Siewior
2018-03-16 14:21 ` [RT PATCH 2/2] Revert "cpu_chill: Add a UNINTERRUPTIBLE hrtimer_nanosleep" Sebastian Andrzej Siewior
@ 2018-03-16 15:00 ` Thomas Gleixner
2018-03-16 16:23 ` [RT PATCH 1/2 v2] " Sebastian Andrzej Siewior
1 sibling, 1 reply; 4+ messages in thread
From: Thomas Gleixner @ 2018-03-16 15:00 UTC (permalink / raw)
To: Sebastian Andrzej Siewior; +Cc: linux-rt-users, linux-kernel, Steven Rostedt
On Fri, 16 Mar 2018, Sebastian Andrzej Siewior wrote:
> cpu_chill() uses __hrtimer_nanosleep() with a hack to get
> TASK_UNINTERRUPTIBLE state. It seems to work but it does not.
> The problem is that we may have a signal pending and receive a sporadic
> wake up _before_ the timer expires. At that point the code will look at
_We_ have nothing pending. Please describe it in a factual way and not like
you are running the code yourself.
> ->restart_block and use it based on its value from the previous syscall
> resulting in a possible copy-to-user.
What you want to describe is:
If a task calls cpu_chill() and gets woken up by a regular or spurious
wakeup and has a signal pending, then it exits the sleep loop in
do_nanosleep() and sets up the restart block. If restart->nanosleep.type is
not TI_NONE then this results in accessing a stale user pointer from a
previously interrupted syscall and a copy to user based on the stale
pointer or a BUG() when 'type' is not supported in nanosleep_copyout().
> instead all this trouble, we could use schedule_hrtimeout() which is not
Sentences start with an uppercase letter.
> instead all this trouble, we could use schedule_hrtimeout() which is not
s/we could// ....
> an user-space interface and does not have this side effects.
hmm?
> Cc: stable-rt@vger.kernel.org
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> ---
> kernel/time/hrtimer.c | 9 ++++-----
> 1 file changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
> index 6c77643eaf02..c0efc22ba635 100644
> --- a/kernel/time/hrtimer.c
> +++ b/kernel/time/hrtimer.c
> @@ -1873,14 +1873,13 @@ COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
> */
> void cpu_chill(void)
> {
> - struct timespec64 tu = {
> - .tv_nsec = NSEC_PER_MSEC,
> - };
> + ktime_t chill_time;
> unsigned int freeze_flag = current->flags & PF_NOFREEZE;
>
> + chill_time = ktime_set(0, NSEC_PER_MSEC);
> + set_current_state(TASK_UNINTERRUPTIBLE);
> current->flags |= PF_NOFREEZE;
> - __hrtimer_nanosleep(&tu, HRTIMER_MODE_REL_HARD, CLOCK_MONOTONIC,
> - TASK_UNINTERRUPTIBLE);
> + schedule_hrtimeout(&chill_time, HRTIMER_MODE_REL_HARD);
> if (!freeze_flag)
> current->flags &= ~PF_NOFREEZE;
> }
> --
> 2.16.2
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RT PATCH 1/2 v2] kernel/cpu_chill: use schedule_hrtimeout()
2018-03-16 15:00 ` [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout() Thomas Gleixner
@ 2018-03-16 16:23 ` Sebastian Andrzej Siewior
0 siblings, 0 replies; 4+ messages in thread
From: Sebastian Andrzej Siewior @ 2018-03-16 16:23 UTC (permalink / raw)
To: Thomas Gleixner; +Cc: linux-rt-users, linux-kernel, Steven Rostedt
If a task calls cpu_chill() and gets woken up by a regular or spurious
wakeup and has a signal pending, then it exits the sleep loop in
do_nanosleep() and sets up the restart block. If restart->nanosleep.type is
not TI_NONE then this results in accessing a stale user pointer from a
previously interrupted syscall and a copy to user based on the stale
pointer or a BUG() when 'type' is not supported in nanosleep_copyout().
Instead all this trouble, use schedule_hrtimeout().
Cc: stable-rt@vger.kernel.org
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
kernel/time/hrtimer.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 6c77643eaf02..c0efc22ba635 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -1873,14 +1873,13 @@ COMPAT_SYSCALL_DEFINE2(nanosleep, struct compat_timespec __user *, rqtp,
*/
void cpu_chill(void)
{
- struct timespec64 tu = {
- .tv_nsec = NSEC_PER_MSEC,
- };
+ ktime_t chill_time;
unsigned int freeze_flag = current->flags & PF_NOFREEZE;
+ chill_time = ktime_set(0, NSEC_PER_MSEC);
+ set_current_state(TASK_UNINTERRUPTIBLE);
current->flags |= PF_NOFREEZE;
- __hrtimer_nanosleep(&tu, HRTIMER_MODE_REL_HARD, CLOCK_MONOTONIC,
- TASK_UNINTERRUPTIBLE);
+ schedule_hrtimeout(&chill_time, HRTIMER_MODE_REL_HARD);
if (!freeze_flag)
current->flags &= ~PF_NOFREEZE;
}
--
2.16.2
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-03-16 16:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-16 14:21 [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout() Sebastian Andrzej Siewior
2018-03-16 14:21 ` [RT PATCH 2/2] Revert "cpu_chill: Add a UNINTERRUPTIBLE hrtimer_nanosleep" Sebastian Andrzej Siewior
2018-03-16 15:00 ` [RT PATCH 1/2] kernel/cpu_chill: use schedule_hrtimeout() Thomas Gleixner
2018-03-16 16:23 ` [RT PATCH 1/2 v2] " Sebastian Andrzej Siewior
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.