ipc: Update semtimedop() to use hrtimer
PATCH v3 1/1 ipc: Update semtimedop() to use hrtimer
semtimedop() should be converted to use hrtimer like it has been done for most of the system calls with timeouts. This system call already takes a struct timespec as an argument and can therefore provide finer granularity timed wait.
---v1->v2:
- Use timespec64_valid() to validate timeout and other changes as suggested by Thomas Gleixner
v2-v3 Added Reviewed by tag
--- ipc/sem.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
ipc/sem.c +11 -12
@@ -1995,7 +1995,9 @@ long __do_semtimedop(int semid, struct sembuf *sops,int max, locknum;bool undos = false, alter = false, dupsop = false;struct sem_queue queue; -unsigned long dup =0 , jiffies_left =0 ; +unsigned long dup =0 ; +ktime_t expires, *exp =NULL ; +bool timed_out = false;if (nsops <1 || semid <0 )return -EINVAL ;
@@ -2003,12 +2005,11 @@ long __do_semtimedop(int semid, struct sembuf *sops,return -E2BIG ;if (timeout) { -if (timeout->tv_sec <0 || timeout->tv_nsec <0 || - timeout->tv_nsec >=1000000000L ) { - error = -EINVAL ; - goto out; - } - jiffies_left =timespec64_to_jiffies (timeout); +if (!timespec64_valid (timeout)) +return -EINVAL ; + expires =ktime_add_safe (ktime_get (), +timespec64_to_ktime (*timeout)); + exp = &expires; }
@@ -2166,10 +2167,8 @@ long __do_semtimedop(int semid, struct sembuf *sops,sem_unlock (sma, locknum);rcu_read_unlock (); -if (timeout) - jiffies_left =schedule_timeout (jiffies_left); -else -schedule (); + timed_out = !schedule_hrtimeout_range (exp, + current->timer_slack_ns,HRTIMER_MODE_ABS );/* * fastpath: the semop has completed, either successfully or
@@ -2210,7 +2209,7 @@ long __do_semtimedop(int semid, struct sembuf *sops,/* * If an interrupt occurred we have to clean up the queue. */ -if (timeout && jiffies_left ==0 ) +if (timed_out) error = -EAGAIN ; }while (error == -EINTR && !signal_pending (current));/* spurious */
Signature · 1 line
2.7.4
Quoted 5 lines · Prakash Sangappa
semtimedop()should be converted to use hrtimer like it has been done for most of the system calls with timeouts. This system call already takes a struct timespec as an argument and can therefore provide finer granularity timed wait.
Quoted 8 lines
Signed-off-by Prakash Sangappa <[email protected]>Reviewed-by Thomas Gleixner <[email protected]>---
v1->v2:
- Usetimespec64_valid()to validate timeout and other changes as suggested by Thomas Gleixner
v2-v3 Added Reviewed by tag
---
The second '---' is not necesary, it's the first one that counts.
ipc/sem.c long __do_semtimedop(int semid, struct sembuf *sops, · line 2171
Quoted 10 lines
ipc/sem.c +2
-4@@ -2166,10 +2167,8 @@ long __do_semtimedop(int semid, struct sembuf *sops,sem_unlock (sma, locknum);rcu_read_unlock (); -if (timeout) - jiffies_left =schedule_timeout (jiffies_left); -else -schedule (); + timed_out = !schedule_hrtimeout_range (exp, + current->timer_slack_ns,HRTIMER_MODE_ABS );
I'm wondering if the slack parameter instead of passing the timer_slack_ns value immediately, we should do a rt_task() check and pass zero if so. And the opposite for the posix mqueue case: bit a little more lenient when !rt_task() and pass current->timer_slack_ns instead of zero. Of course mq is a lot more rt aware than sysv semaphores, but that doesn't mean both forms of ipc are not seen used with or without RT constraints.
Thanks,
Davidlohr
Quoted 10 lines · Davidlohr Bueso
ipc/sem.c long __do_semtimedop(int semid, struct sembuf *sops, · line 2171
Quoted 6 lines · Prakash Sangappa
ipc/sem.c +2
-4-if (timeout) - jiffies_left =schedule_timeout (jiffies_left); -else -schedule (); + timed_out = !schedule_hrtimeout_range (exp, + current->timer_slack_ns,HRTIMER_MODE_ABS );I'm wondering if the slack parameter instead of passing the
timer_slack_nsvalue immediately, we should do art_task()check and pass zero if so.
We should have a wrapper function which takes care of that instead of having checks all over the place.
Thanks,
tglx
Quoted 16 lines · Thomas Gleixner
Quoted 10 lines · Davidlohr Bueso
ipc/sem.c long __do_semtimedop(int semid, struct sembuf *sops, · line 2171
Quoted 6 lines · Prakash Sangappa
ipc/sem.c +2
-4-if (timeout) - jiffies_left =schedule_timeout (jiffies_left); -else -schedule (); + timed_out = !schedule_hrtimeout_range (exp, + current->timer_slack_ns,HRTIMER_MODE_ABS );I'm wondering if the slack parameter instead of passing the
timer_slack_nsvalue immediately, we should do art_task()check and pass zero if so.
Quoted 2 lines
We should have a wrapper function which takes care of that instead of having checks all over the place.
Ok it can be an inline function in sched.h which returns appropriate slack time. Use that in futex_wait() and sigtimedwait() also in addition to semtimedop() & mqueue codepath?
Should that be a separate patch?
Thanks,
-Prakash
Quoted 4 lines
Thanks,
tglx
Quoted 13 lines · Prakash Sangappa
Quoted 13 lines · Thomas Gleixner
Quoted 10 lines · Davidlohr Bueso
ipc/sem.c long __do_semtimedop(int semid, struct sembuf *sops, · line 2171
Quoted 6 lines · Prakash Sangappa
ipc/sem.c +2
-4-if (timeout) - jiffies_left =schedule_timeout (jiffies_left); -else -schedule (); + timed_out = !schedule_hrtimeout_range (exp, + current->timer_slack_ns,HRTIMER_MODE_ABS );I'm wondering if the slack parameter instead of passing the
timer_slack_nsvalue immediately, we should do art_task()check and pass zero if so.
Quoted 6 lines
Quoted 2 lines
We should have a wrapper function which takes care of that instead of having checks all over the place.
Ok it can be an inline function in
sched.hwhich returns appropriate slack time. Use that infutex_wait()andsigtimedwait()also in addition tosemtimedop()& mqueue codepath?
No. What I meant is a function which handles this internally, not an inline function which has to be invoked on various call sites.
Quoted 1 line
Should that be a separate patch?
Definitely. That's an orthogonal problem.
Thanks,
tglx
Quoted 2 lines · Thomas Gleixner
No. What I meant is a function which handles this internally, not an inline function which has to be invoked on various call sites.
Originally I was also thinking about it on a per-user basis, but after more thought I agree it's better if it is done by the hrtimer.
Something like so?
Thanks,
Davidlohr
---------------8<----------------------------------------------------- [PATCH] hrtimer: Ignore slack time for RT tasks
While in theory the timer can be triggered before expires+delta, for the cases of RT tasks they really have no business giving any lenience for extra slack time, so override any passed value by the user and always use zero.
--- kernel/time/hrtimer.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/time/hrtimer.c +0 -0
@@ -2297,6 +2297,13 @@ schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
return -EINTR; }
+ /*
+ * Override any slack passed by the user if under
+ * rt contraints.
+ */
+ if (rt_task(current))
+ delta = 0;
+
hrtimer_init_sleeper_on_stack(&t, clock_id, mode); hrtimer_set_expires_range_ns(&t.timer, *expires, delta); hrtimer_sleeper_start_expires(&t, mode);
@@ -2326,6 +2333,7 @@ schedule_hrtimeout_range_clock(ktime_t *expires, u64 delta,
* actual wakeup to a time that is both power and performance friendly.
* The kernel give the normal best effort behavior for "@expires+@delta",
* but may decide to fire the timer earlier, but no earlier than @expires.
+ * For scenarios under realtime constraints, @delta is always zero. *
* You can set the task state as follows -
*
--
2.36.0
Quoted 7 lines · Prakash Sangappa
semtimedop()should be converted to use hrtimer like it has been done for most of the system calls with timeouts. This system call already takes a struct timespec as an argument and can therefore provide finer granularity timed wait.Signed-off-by Prakash Sangappa <[email protected]>Reviewed-by Thomas Gleixner <[email protected]>