]>
Commit | Line | Data |
---|---|---|
6bfd6d72 JL |
1 | /* |
2 | * kernel/sched/cpudl.c | |
3 | * | |
4 | * Global CPU deadline management | |
5 | * | |
6 | * Author: Juri Lelli <j.lelli@sssup.it> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; version 2 | |
11 | * of the License. | |
12 | */ | |
6bfd6d72 JL |
13 | #include <linux/gfp.h> |
14 | #include <linux/kernel.h> | |
944770ab | 15 | #include <linux/slab.h> |
6bfd6d72 JL |
16 | #include "cpudeadline.h" |
17 | ||
18 | static inline int parent(int i) | |
19 | { | |
20 | return (i - 1) >> 1; | |
21 | } | |
22 | ||
23 | static inline int left_child(int i) | |
24 | { | |
25 | return (i << 1) + 1; | |
26 | } | |
27 | ||
28 | static inline int right_child(int i) | |
29 | { | |
30 | return (i << 1) + 2; | |
31 | } | |
32 | ||
126b3b68 | 33 | static void cpudl_heapify_down(struct cpudl *cp, int idx) |
6bfd6d72 JL |
34 | { |
35 | int l, r, largest; | |
36 | ||
8e1bc301 TC |
37 | int orig_cpu = cp->elements[idx].cpu; |
38 | u64 orig_dl = cp->elements[idx].dl; | |
39 | ||
40 | if (left_child(idx) >= cp->size) | |
41 | return; | |
42 | ||
6bfd6d72 | 43 | /* adapted from lib/prio_heap.c */ |
c2e51382 | 44 | while (1) { |
8e1bc301 | 45 | u64 largest_dl; |
c2e51382 | 46 | |
6bfd6d72 JL |
47 | l = left_child(idx); |
48 | r = right_child(idx); | |
49 | largest = idx; | |
8e1bc301 | 50 | largest_dl = orig_dl; |
6bfd6d72 | 51 | |
8e1bc301 TC |
52 | if ((l < cp->size) && dl_time_before(orig_dl, |
53 | cp->elements[l].dl)) { | |
6bfd6d72 | 54 | largest = l; |
8e1bc301 TC |
55 | largest_dl = cp->elements[l].dl; |
56 | } | |
57 | if ((r < cp->size) && dl_time_before(largest_dl, | |
58 | cp->elements[r].dl)) | |
6bfd6d72 | 59 | largest = r; |
8e1bc301 | 60 | |
6bfd6d72 JL |
61 | if (largest == idx) |
62 | break; | |
63 | ||
8e1bc301 TC |
64 | /* pull largest child onto idx */ |
65 | cp->elements[idx].cpu = cp->elements[largest].cpu; | |
66 | cp->elements[idx].dl = cp->elements[largest].dl; | |
67 | cp->elements[cp->elements[idx].cpu].idx = idx; | |
6bfd6d72 JL |
68 | idx = largest; |
69 | } | |
8e1bc301 TC |
70 | /* actual push down of saved original values orig_* */ |
71 | cp->elements[idx].cpu = orig_cpu; | |
72 | cp->elements[idx].dl = orig_dl; | |
73 | cp->elements[cp->elements[idx].cpu].idx = idx; | |
6bfd6d72 JL |
74 | } |
75 | ||
126b3b68 | 76 | static void cpudl_heapify_up(struct cpudl *cp, int idx) |
6bfd6d72 | 77 | { |
8e1bc301 TC |
78 | int p; |
79 | ||
80 | int orig_cpu = cp->elements[idx].cpu; | |
81 | u64 orig_dl = cp->elements[idx].dl; | |
82 | ||
83 | if (idx == 0) | |
84 | return; | |
85 | ||
86 | do { | |
87 | p = parent(idx); | |
88 | if (dl_time_before(orig_dl, cp->elements[p].dl)) | |
89 | break; | |
90 | /* pull parent onto idx */ | |
91 | cp->elements[idx].cpu = cp->elements[p].cpu; | |
92 | cp->elements[idx].dl = cp->elements[p].dl; | |
93 | cp->elements[cp->elements[idx].cpu].idx = idx; | |
94 | idx = p; | |
95 | } while (idx != 0); | |
96 | /* actual push up of saved original values orig_* */ | |
97 | cp->elements[idx].cpu = orig_cpu; | |
98 | cp->elements[idx].dl = orig_dl; | |
99 | cp->elements[cp->elements[idx].cpu].idx = idx; | |
6bfd6d72 JL |
100 | } |
101 | ||
126b3b68 TC |
102 | static void cpudl_heapify(struct cpudl *cp, int idx) |
103 | { | |
104 | if (idx > 0 && dl_time_before(cp->elements[parent(idx)].dl, | |
105 | cp->elements[idx].dl)) | |
106 | cpudl_heapify_up(cp, idx); | |
107 | else | |
108 | cpudl_heapify_down(cp, idx); | |
109 | } | |
110 | ||
6bfd6d72 JL |
111 | static inline int cpudl_maximum(struct cpudl *cp) |
112 | { | |
113 | return cp->elements[0].cpu; | |
114 | } | |
115 | ||
116 | /* | |
117 | * cpudl_find - find the best (later-dl) CPU in the system | |
118 | * @cp: the cpudl max-heap context | |
119 | * @p: the task | |
120 | * @later_mask: a mask to fill in with the selected CPUs (or NULL) | |
121 | * | |
3261ed0b | 122 | * Returns: int - CPUs were found |
6bfd6d72 JL |
123 | */ |
124 | int cpudl_find(struct cpudl *cp, struct task_struct *p, | |
125 | struct cpumask *later_mask) | |
126 | { | |
6bfd6d72 JL |
127 | const struct sched_dl_entity *dl_se = &p->dl; |
128 | ||
16b26943 | 129 | if (later_mask && |
0c98d344 | 130 | cpumask_and(later_mask, cp->free_cpus, &p->cpus_allowed)) { |
3261ed0b BP |
131 | return 1; |
132 | } else { | |
133 | int best_cpu = cpudl_maximum(cp); | |
c2e51382 | 134 | |
3261ed0b | 135 | WARN_ON(best_cpu != -1 && !cpu_present(best_cpu)); |
6bfd6d72 | 136 | |
3261ed0b BP |
137 | if (cpumask_test_cpu(best_cpu, &p->cpus_allowed) && |
138 | dl_time_before(dl_se->deadline, cp->elements[0].dl)) { | |
139 | if (later_mask) | |
140 | cpumask_set_cpu(best_cpu, later_mask); | |
6bfd6d72 | 141 | |
3261ed0b BP |
142 | return 1; |
143 | } | |
144 | } | |
145 | return 0; | |
6bfd6d72 JL |
146 | } |
147 | ||
148 | /* | |
97fb7a0a | 149 | * cpudl_clear - remove a CPU from the cpudl max-heap |
6bfd6d72 | 150 | * @cp: the cpudl max-heap context |
97fb7a0a | 151 | * @cpu: the target CPU |
6bfd6d72 JL |
152 | * |
153 | * Notes: assumes cpu_rq(cpu)->lock is locked | |
154 | * | |
155 | * Returns: (void) | |
156 | */ | |
d8206bb3 | 157 | void cpudl_clear(struct cpudl *cp, int cpu) |
6bfd6d72 JL |
158 | { |
159 | int old_idx, new_cpu; | |
160 | unsigned long flags; | |
161 | ||
82b95800 | 162 | WARN_ON(!cpu_present(cpu)); |
6bfd6d72 JL |
163 | |
164 | raw_spin_lock_irqsave(&cp->lock, flags); | |
d8206bb3 | 165 | |
944770ab | 166 | old_idx = cp->elements[cpu].idx; |
d8206bb3 TC |
167 | if (old_idx == IDX_INVALID) { |
168 | /* | |
169 | * Nothing to remove if old_idx was invalid. | |
170 | * This could happen if a rq_offline_dl is | |
171 | * called for a CPU without -dl tasks running. | |
172 | */ | |
173 | } else { | |
6bfd6d72 JL |
174 | new_cpu = cp->elements[cp->size - 1].cpu; |
175 | cp->elements[old_idx].dl = cp->elements[cp->size - 1].dl; | |
176 | cp->elements[old_idx].cpu = new_cpu; | |
177 | cp->size--; | |
944770ab PZ |
178 | cp->elements[new_cpu].idx = old_idx; |
179 | cp->elements[cpu].idx = IDX_INVALID; | |
126b3b68 | 180 | cpudl_heapify(cp, old_idx); |
6bfd6d72 | 181 | |
d8206bb3 | 182 | cpumask_set_cpu(cpu, cp->free_cpus); |
6bfd6d72 | 183 | } |
d8206bb3 TC |
184 | raw_spin_unlock_irqrestore(&cp->lock, flags); |
185 | } | |
186 | ||
187 | /* | |
188 | * cpudl_set - update the cpudl max-heap | |
189 | * @cp: the cpudl max-heap context | |
97fb7a0a IM |
190 | * @cpu: the target CPU |
191 | * @dl: the new earliest deadline for this CPU | |
d8206bb3 TC |
192 | * |
193 | * Notes: assumes cpu_rq(cpu)->lock is locked | |
194 | * | |
195 | * Returns: (void) | |
196 | */ | |
197 | void cpudl_set(struct cpudl *cp, int cpu, u64 dl) | |
198 | { | |
199 | int old_idx; | |
200 | unsigned long flags; | |
201 | ||
202 | WARN_ON(!cpu_present(cpu)); | |
6bfd6d72 | 203 | |
d8206bb3 TC |
204 | raw_spin_lock_irqsave(&cp->lock, flags); |
205 | ||
206 | old_idx = cp->elements[cpu].idx; | |
6bfd6d72 | 207 | if (old_idx == IDX_INVALID) { |
126b3b68 | 208 | int new_idx = cp->size++; |
c2e51382 | 209 | |
126b3b68 TC |
210 | cp->elements[new_idx].dl = dl; |
211 | cp->elements[new_idx].cpu = cpu; | |
212 | cp->elements[cpu].idx = new_idx; | |
213 | cpudl_heapify_up(cp, new_idx); | |
6bfd6d72 JL |
214 | cpumask_clear_cpu(cpu, cp->free_cpus); |
215 | } else { | |
126b3b68 TC |
216 | cp->elements[old_idx].dl = dl; |
217 | cpudl_heapify(cp, old_idx); | |
6bfd6d72 JL |
218 | } |
219 | ||
6bfd6d72 JL |
220 | raw_spin_unlock_irqrestore(&cp->lock, flags); |
221 | } | |
222 | ||
16b26943 XP |
223 | /* |
224 | * cpudl_set_freecpu - Set the cpudl.free_cpus | |
225 | * @cp: the cpudl max-heap context | |
97fb7a0a | 226 | * @cpu: rd attached CPU |
16b26943 XP |
227 | */ |
228 | void cpudl_set_freecpu(struct cpudl *cp, int cpu) | |
229 | { | |
230 | cpumask_set_cpu(cpu, cp->free_cpus); | |
231 | } | |
232 | ||
233 | /* | |
234 | * cpudl_clear_freecpu - Clear the cpudl.free_cpus | |
235 | * @cp: the cpudl max-heap context | |
97fb7a0a | 236 | * @cpu: rd attached CPU |
16b26943 XP |
237 | */ |
238 | void cpudl_clear_freecpu(struct cpudl *cp, int cpu) | |
239 | { | |
240 | cpumask_clear_cpu(cpu, cp->free_cpus); | |
241 | } | |
242 | ||
6bfd6d72 JL |
243 | /* |
244 | * cpudl_init - initialize the cpudl structure | |
245 | * @cp: the cpudl max-heap context | |
246 | */ | |
247 | int cpudl_init(struct cpudl *cp) | |
248 | { | |
249 | int i; | |
250 | ||
6bfd6d72 JL |
251 | raw_spin_lock_init(&cp->lock); |
252 | cp->size = 0; | |
944770ab PZ |
253 | |
254 | cp->elements = kcalloc(nr_cpu_ids, | |
255 | sizeof(struct cpudl_item), | |
256 | GFP_KERNEL); | |
257 | if (!cp->elements) | |
258 | return -ENOMEM; | |
259 | ||
16b26943 | 260 | if (!zalloc_cpumask_var(&cp->free_cpus, GFP_KERNEL)) { |
944770ab | 261 | kfree(cp->elements); |
6bfd6d72 | 262 | return -ENOMEM; |
944770ab PZ |
263 | } |
264 | ||
265 | for_each_possible_cpu(i) | |
266 | cp->elements[i].idx = IDX_INVALID; | |
267 | ||
6bfd6d72 JL |
268 | return 0; |
269 | } | |
270 | ||
271 | /* | |
272 | * cpudl_cleanup - clean up the cpudl structure | |
273 | * @cp: the cpudl max-heap context | |
274 | */ | |
275 | void cpudl_cleanup(struct cpudl *cp) | |
276 | { | |
6a7cd273 | 277 | free_cpumask_var(cp->free_cpus); |
944770ab | 278 | kfree(cp->elements); |
6bfd6d72 | 279 | } |