Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sched/rt: Clean up usage of rt_task()

rt_task() checks if a task has RT priority. But depends on your
dictionary, this could mean it belongs to RT class, or is a 'realtime'
task, which includes RT and DL classes.

Since this has caused some confusion already on discussion [1], it
seemed a clean up is due.

I define the usage of rt_task() to be tasks that belong to RT class.
Make sure that it returns true only for RT class and audit the users and
replace the ones required the old behavior with the new realtime_task()
which returns true for RT and DL classes. Introduce similar
realtime_prio() to create similar distinction to rt_prio() and update
the users that required the old behavior to use the new function.

Move MAX_DL_PRIO to prio.h so it can be used in the new definitions.

Document the functions to make it more obvious what is the difference
between them. PI-boosted tasks is a factor that must be taken into
account when choosing which function to use.

Rename task_is_realtime() to realtime_task_policy() as the old name is
confusing against the new realtime_task().

No functional changes were intended.

[1] https://lore.kernel.org/lkml/20240506100509.GL40213@noisy.programming.kicks-ass.net/

Signed-off-by: Qais Yousef <qyousef@layalina.io>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Phil Auld <pauld@redhat.com>
Reviewed-by: "Steven Rostedt (Google)" <rostedt@goodmis.org>
Reviewed-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Link: https://lore.kernel.org/r/20240610192018.1567075-2-qyousef@layalina.io

authored by

Qais Yousef and committed by
Peter Zijlstra
130fd056 4ae0c2b9

+49 -21
+1 -1
fs/bcachefs/six.c
··· 335 335 */ 336 336 rcu_read_lock(); 337 337 struct task_struct *owner = READ_ONCE(lock->owner); 338 - bool ret = owner ? owner_on_cpu(owner) : !rt_task(current); 338 + bool ret = owner ? owner_on_cpu(owner) : !realtime_task(current); 339 339 rcu_read_unlock(); 340 340 341 341 return ret;
+1 -1
fs/select.c
··· 82 82 * Realtime tasks get a slack of 0 for obvious reasons. 83 83 */ 84 84 85 - if (rt_task(current)) 85 + if (realtime_task(current)) 86 86 return 0; 87 87 88 88 ktime_get_ts64(&now);
+1 -1
include/linux/ioprio.h
··· 40 40 { 41 41 if (task->policy == SCHED_IDLE) 42 42 return IOPRIO_CLASS_IDLE; 43 - else if (task_is_realtime(task)) 43 + else if (realtime_task_policy(task)) 44 44 return IOPRIO_CLASS_RT; 45 45 else 46 46 return IOPRIO_CLASS_BE;
+4 -2
include/linux/sched/deadline.h
··· 10 10 11 11 #include <linux/sched.h> 12 12 13 - #define MAX_DL_PRIO 0 14 - 15 13 static inline int dl_prio(int prio) 16 14 { 17 15 if (unlikely(prio < MAX_DL_PRIO)) ··· 17 19 return 0; 18 20 } 19 21 22 + /* 23 + * Returns true if a task has a priority that belongs to DL class. PI-boosted 24 + * tasks will return true. Use dl_policy() to ignore PI-boosted tasks. 25 + */ 20 26 static inline int dl_task(struct task_struct *p) 21 27 { 22 28 return dl_prio(p->prio);
+1
include/linux/sched/prio.h
··· 14 14 */ 15 15 16 16 #define MAX_RT_PRIO 100 17 + #define MAX_DL_PRIO 0 17 18 18 19 #define MAX_PRIO (MAX_RT_PRIO + NICE_WIDTH) 19 20 #define DEFAULT_PRIO (MAX_RT_PRIO + NICE_WIDTH / 2)
+26 -1
include/linux/sched/rt.h
··· 8 8 9 9 static inline int rt_prio(int prio) 10 10 { 11 + if (unlikely(prio < MAX_RT_PRIO && prio >= MAX_DL_PRIO)) 12 + return 1; 13 + return 0; 14 + } 15 + 16 + static inline int realtime_prio(int prio) 17 + { 11 18 if (unlikely(prio < MAX_RT_PRIO)) 12 19 return 1; 13 20 return 0; 14 21 } 15 22 23 + /* 24 + * Returns true if a task has a priority that belongs to RT class. PI-boosted 25 + * tasks will return true. Use rt_policy() to ignore PI-boosted tasks. 26 + */ 16 27 static inline int rt_task(struct task_struct *p) 17 28 { 18 29 return rt_prio(p->prio); 19 30 } 20 31 21 - static inline bool task_is_realtime(struct task_struct *tsk) 32 + /* 33 + * Returns true if a task has a priority that belongs to RT or DL classes. 34 + * PI-boosted tasks will return true. Use realtime_task_policy() to ignore 35 + * PI-boosted tasks. 36 + */ 37 + static inline int realtime_task(struct task_struct *p) 38 + { 39 + return realtime_prio(p->prio); 40 + } 41 + 42 + /* 43 + * Returns true if a task has a policy that belongs to RT or DL classes. 44 + * PI-boosted tasks will return false. 45 + */ 46 + static inline bool realtime_task_policy(struct task_struct *tsk) 22 47 { 23 48 int policy = tsk->policy; 24 49
+2 -2
kernel/locking/rtmutex.c
··· 347 347 { 348 348 int prio = task->prio; 349 349 350 - if (!rt_prio(prio)) 350 + if (!realtime_prio(prio)) 351 351 return DEFAULT_PRIO; 352 352 353 353 return prio; ··· 435 435 * Note that RT tasks are excluded from same priority (lateral) 436 436 * steals to prevent the introduction of an unbounded latency. 437 437 */ 438 - if (rt_prio(waiter->tree.prio) || dl_prio(waiter->tree.prio)) 438 + if (realtime_prio(waiter->tree.prio)) 439 439 return false; 440 440 441 441 return rt_waiter_node_equal(&waiter->tree, &top_waiter->tree);
+2 -2
kernel/locking/rwsem.c
··· 631 631 * if it is an RT task or wait in the wait queue 632 632 * for too long. 633 633 */ 634 - if (has_handoff || (!rt_task(waiter->task) && 634 + if (has_handoff || (!realtime_task(waiter->task) && 635 635 !time_after(jiffies, waiter->timeout))) 636 636 return false; 637 637 ··· 914 914 if (owner_state != OWNER_WRITER) { 915 915 if (need_resched()) 916 916 break; 917 - if (rt_task(current) && 917 + if (realtime_task(current) && 918 918 (prev_owner_state != OWNER_WRITER)) 919 919 break; 920 920 }
+1 -1
kernel/locking/ww_mutex.h
··· 237 237 int a_prio = a->task->prio; 238 238 int b_prio = b->task->prio; 239 239 240 - if (rt_prio(a_prio) || rt_prio(b_prio)) { 240 + if (realtime_prio(a_prio) || realtime_prio(b_prio)) { 241 241 242 242 if (a_prio > b_prio) 243 243 return true;
+2 -2
kernel/sched/core.c
··· 166 166 if (p->dl_server) 167 167 return -1; /* deadline */ 168 168 169 - if (rt_prio(p->prio)) /* includes deadline */ 169 + if (realtime_prio(p->prio)) /* includes deadline */ 170 170 return p->prio; /* [-1, 99] */ 171 171 172 172 if (p->sched_class == &idle_sched_class) ··· 8590 8590 schedstat_set(p->stats.sleep_start, 0); 8591 8591 schedstat_set(p->stats.block_start, 0); 8592 8592 8593 - if (!dl_task(p) && !rt_task(p)) { 8593 + if (!realtime_task(p)) { 8594 8594 /* 8595 8595 * Renice negative nice level userspace 8596 8596 * tasks back to 0:
+1 -1
kernel/sched/syscalls.c
··· 57 57 * keep the priority unchanged. Otherwise, update priority 58 58 * to the normal priority: 59 59 */ 60 - if (!rt_prio(p->prio)) 60 + if (!realtime_prio(p->prio)) 61 61 return p->normal_prio; 62 62 return p->prio; 63 63 }
+3 -3
kernel/time/hrtimer.c
··· 1975 1975 * expiry. 1976 1976 */ 1977 1977 if (IS_ENABLED(CONFIG_PREEMPT_RT)) { 1978 - if (task_is_realtime(current) && !(mode & HRTIMER_MODE_SOFT)) 1978 + if (realtime_task_policy(current) && !(mode & HRTIMER_MODE_SOFT)) 1979 1979 mode |= HRTIMER_MODE_HARD; 1980 1980 } 1981 1981 ··· 2075 2075 u64 slack; 2076 2076 2077 2077 slack = current->timer_slack_ns; 2078 - if (rt_task(current)) 2078 + if (realtime_task(current)) 2079 2079 slack = 0; 2080 2080 2081 2081 hrtimer_init_sleeper_on_stack(&t, clockid, mode); ··· 2280 2280 * Override any slack passed by the user if under 2281 2281 * rt contraints. 2282 2282 */ 2283 - if (rt_task(current)) 2283 + if (realtime_task(current)) 2284 2284 delta = 0; 2285 2285 2286 2286 hrtimer_init_sleeper_on_stack(&t, clock_id, mode);
+1 -1
kernel/trace/trace_sched_wakeup.c
··· 547 547 * - wakeup_dl handles tasks belonging to sched_dl class only. 548 548 */ 549 549 if (tracing_dl || (wakeup_dl && !dl_task(p)) || 550 - (wakeup_rt && !dl_task(p) && !rt_task(p)) || 550 + (wakeup_rt && !realtime_task(p)) || 551 551 (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio))) 552 552 return; 553 553
+2 -2
mm/page-writeback.c
··· 418 418 bg_thresh = (bg_ratio * available_memory) / PAGE_SIZE; 419 419 420 420 tsk = current; 421 - if (rt_task(tsk)) { 421 + if (realtime_task(tsk)) { 422 422 bg_thresh += bg_thresh / 4 + global_wb_domain.dirty_limit / 32; 423 423 thresh += thresh / 4 + global_wb_domain.dirty_limit / 32; 424 424 } ··· 477 477 else 478 478 dirty = vm_dirty_ratio * node_memory / 100; 479 479 480 - if (rt_task(tsk)) 480 + if (realtime_task(tsk)) 481 481 dirty += dirty / 4; 482 482 483 483 /*
+1 -1
mm/page_alloc.c
··· 4002 4002 */ 4003 4003 if (alloc_flags & ALLOC_MIN_RESERVE) 4004 4004 alloc_flags &= ~ALLOC_CPUSET; 4005 - } else if (unlikely(rt_task(current)) && in_task()) 4005 + } else if (unlikely(realtime_task(current)) && in_task()) 4006 4006 alloc_flags |= ALLOC_MIN_RESERVE; 4007 4007 4008 4008 alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, alloc_flags);