Skip to content

Commit d9cb236

Browse files
Qais Yousefingomolnar
authored andcommitted
sched/rt: cpupri_find: Implement fallback mechanism for !fit case
When searching for the best lowest_mask with a fitness_fn passed, make sure we record the lowest_level that returns a valid lowest_mask so that we can use that as a fallback in case we fail to find a fitting CPU at all levels. The intention in the original patch was not to allow a down migration to unfitting CPU. But this missed the case where we are already running on unfitting one. With this change now RT tasks can still move between unfitting CPUs when they're already running on such CPU. And as Steve suggested; to adhere to the strict priority rules of RT, if a task is already running on a fitting CPU but due to priority it can't run on it, allow it to downmigrate to unfitting CPU so it can run. Reported-by: Pavan Kondeti <pkondeti@codeaurora.org> Signed-off-by: Qais Yousef <qais.yousef@arm.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Fixes: 804d402 ("sched/rt: Make RT capacity-aware") Link: https://lkml.kernel.org/r/20200302132721.8353-2-qais.yousef@arm.com Link: https://lore.kernel.org/lkml/20200203142712.a7yvlyo2y3le5cpn@e107158-lin/
1 parent 5ab297b commit d9cb236

1 file changed

Lines changed: 101 additions & 56 deletions

File tree

‎kernel/sched/cpupri.c‎

Lines changed: 101 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,59 @@ static int convert_prio(int prio)
4141
return cpupri;
4242
}
4343

44+
static inline int __cpupri_find(struct cpupri *cp, struct task_struct *p,
45+
struct cpumask *lowest_mask, int idx)
46+
{
47+
struct cpupri_vec *vec = &cp->pri_to_cpu[idx];
48+
int skip = 0;
49+
50+
if (!atomic_read(&(vec)->count))
51+
skip = 1;
52+
/*
53+
* When looking at the vector, we need to read the counter,
54+
* do a memory barrier, then read the mask.
55+
*
56+
* Note: This is still all racey, but we can deal with it.
57+
* Ideally, we only want to look at masks that are set.
58+
*
59+
* If a mask is not set, then the only thing wrong is that we
60+
* did a little more work than necessary.
61+
*
62+
* If we read a zero count but the mask is set, because of the
63+
* memory barriers, that can only happen when the highest prio
64+
* task for a run queue has left the run queue, in which case,
65+
* it will be followed by a pull. If the task we are processing
66+
* fails to find a proper place to go, that pull request will
67+
* pull this task if the run queue is running at a lower
68+
* priority.
69+
*/
70+
smp_rmb();
71+
72+
/* Need to do the rmb for every iteration */
73+
if (skip)
74+
return 0;
75+
76+
if (cpumask_any_and(p->cpus_ptr, vec->mask) >= nr_cpu_ids)
77+
return 0;
78+
79+
if (lowest_mask) {
80+
cpumask_and(lowest_mask, p->cpus_ptr, vec->mask);
81+
82+
/*
83+
* We have to ensure that we have at least one bit
84+
* still set in the array, since the map could have
85+
* been concurrently emptied between the first and
86+
* second reads of vec->mask. If we hit this
87+
* condition, simply act as though we never hit this
88+
* priority level and continue on.
89+
*/
90+
if (cpumask_empty(lowest_mask))
91+
return 0;
92+
}
93+
94+
return 1;
95+
}
96+
4497
/**
4598
* cpupri_find - find the best (lowest-pri) CPU in the system
4699
* @cp: The cpupri context
@@ -62,80 +115,72 @@ int cpupri_find(struct cpupri *cp, struct task_struct *p,
62115
struct cpumask *lowest_mask,
63116
bool (*fitness_fn)(struct task_struct *p, int cpu))
64117
{
65-
int idx = 0;
66118
int task_pri = convert_prio(p->prio);
119+
int best_unfit_idx = -1;
120+
int idx = 0, cpu;
67121

68122
BUG_ON(task_pri >= CPUPRI_NR_PRIORITIES);
69123

70124
for (idx = 0; idx < task_pri; idx++) {
71-
struct cpupri_vec *vec = &cp->pri_to_cpu[idx];
72-
int skip = 0;
73125

74-
if (!atomic_read(&(vec)->count))
75-
skip = 1;
76-
/*
77-
* When looking at the vector, we need to read the counter,
78-
* do a memory barrier, then read the mask.
79-
*
80-
* Note: This is still all racey, but we can deal with it.
81-
* Ideally, we only want to look at masks that are set.
82-
*
83-
* If a mask is not set, then the only thing wrong is that we
84-
* did a little more work than necessary.
85-
*
86-
* If we read a zero count but the mask is set, because of the
87-
* memory barriers, that can only happen when the highest prio
88-
* task for a run queue has left the run queue, in which case,
89-
* it will be followed by a pull. If the task we are processing
90-
* fails to find a proper place to go, that pull request will
91-
* pull this task if the run queue is running at a lower
92-
* priority.
93-
*/
94-
smp_rmb();
95-
96-
/* Need to do the rmb for every iteration */
97-
if (skip)
98-
continue;
99-
100-
if (cpumask_any_and(p->cpus_ptr, vec->mask) >= nr_cpu_ids)
126+
if (!__cpupri_find(cp, p, lowest_mask, idx))
101127
continue;
102128

103-
if (lowest_mask) {
104-
int cpu;
129+
if (!lowest_mask || !fitness_fn)
130+
return 1;
105131

106-
cpumask_and(lowest_mask, p->cpus_ptr, vec->mask);
132+
/* Ensure the capacity of the CPUs fit the task */
133+
for_each_cpu(cpu, lowest_mask) {
134+
if (!fitness_fn(p, cpu))
135+
cpumask_clear_cpu(cpu, lowest_mask);
136+
}
107137

138+
/*
139+
* If no CPU at the current priority can fit the task
140+
* continue looking
141+
*/
142+
if (cpumask_empty(lowest_mask)) {
108143
/*
109-
* We have to ensure that we have at least one bit
110-
* still set in the array, since the map could have
111-
* been concurrently emptied between the first and
112-
* second reads of vec->mask. If we hit this
113-
* condition, simply act as though we never hit this
114-
* priority level and continue on.
144+
* Store our fallback priority in case we
145+
* didn't find a fitting CPU
115146
*/
116-
if (cpumask_empty(lowest_mask))
117-
continue;
147+
if (best_unfit_idx == -1)
148+
best_unfit_idx = idx;
118149

119-
if (!fitness_fn)
120-
return 1;
121-
122-
/* Ensure the capacity of the CPUs fit the task */
123-
for_each_cpu(cpu, lowest_mask) {
124-
if (!fitness_fn(p, cpu))
125-
cpumask_clear_cpu(cpu, lowest_mask);
126-
}
127-
128-
/*
129-
* If no CPU at the current priority can fit the task
130-
* continue looking
131-
*/
132-
if (cpumask_empty(lowest_mask))
133-
continue;
150+
continue;
134151
}
135152

136153
return 1;
137154
}
138155

156+
/*
157+
* If we failed to find a fitting lowest_mask, make sure we fall back
158+
* to the last known unfitting lowest_mask.
159+
*
160+
* Note that the map of the recorded idx might have changed since then,
161+
* so we must ensure to do the full dance to make sure that level still
162+
* holds a valid lowest_mask.
163+
*
164+
* As per above, the map could have been concurrently emptied while we
165+
* were busy searching for a fitting lowest_mask at the other priority
166+
* levels.
167+
*
168+
* This rule favours honouring priority over fitting the task in the
169+
* correct CPU (Capacity Awareness being the only user now).
170+
* The idea is that if a higher priority task can run, then it should
171+
* run even if this ends up being on unfitting CPU.
172+
*
173+
* The cost of this trade-off is not entirely clear and will probably
174+
* be good for some workloads and bad for others.
175+
*
176+
* The main idea here is that if some CPUs were overcommitted, we try
177+
* to spread which is what the scheduler traditionally did. Sys admins
178+
* must do proper RT planning to avoid overloading the system if they
179+
* really care.
180+
*/
181+
if (best_unfit_idx != -1)
182+
return __cpupri_find(cp, p, lowest_mask, best_unfit_idx);
183+
139184
return 0;
140185
}
141186

0 commit comments

Comments
 (0)