Blame view

kernel/sched/cpudeadline.c 5.07 KB
6bfd6d72f   Juri Lelli   sched/deadline: s...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   *  kernel/sched/cpudl.c
   *
   *  Global CPU deadline management
   *
   *  Author: Juri Lelli <j.lelli@sssup.it>
   *
   *  This program is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU General Public License
   *  as published by the Free Software Foundation; version 2
   *  of the License.
   */
  
  #include <linux/gfp.h>
  #include <linux/kernel.h>
944770ab5   Peter Zijlstra   sched/deadline: R...
16
  #include <linux/slab.h>
6bfd6d72f   Juri Lelli   sched/deadline: s...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  #include "cpudeadline.h"
  
  static inline int parent(int i)
  {
  	return (i - 1) >> 1;
  }
  
  static inline int left_child(int i)
  {
  	return (i << 1) + 1;
  }
  
  static inline int right_child(int i)
  {
  	return (i << 1) + 2;
  }
  
  static inline int dl_time_before(u64 a, u64 b)
  {
  	return (s64)(a - b) < 0;
  }
88f1ebbc2   Fengguang Wu   sched/deadline: F...
38
  static void cpudl_exchange(struct cpudl *cp, int a, int b)
6bfd6d72f   Juri Lelli   sched/deadline: s...
39
40
  {
  	int cpu_a = cp->elements[a].cpu, cpu_b = cp->elements[b].cpu;
944770ab5   Peter Zijlstra   sched/deadline: R...
41
42
43
44
  	swap(cp->elements[a].cpu, cp->elements[b].cpu);
  	swap(cp->elements[a].dl , cp->elements[b].dl );
  
  	swap(cp->elements[cpu_a].idx, cp->elements[cpu_b].idx);
6bfd6d72f   Juri Lelli   sched/deadline: s...
45
  }
88f1ebbc2   Fengguang Wu   sched/deadline: F...
46
  static void cpudl_heapify(struct cpudl *cp, int idx)
6bfd6d72f   Juri Lelli   sched/deadline: s...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  {
  	int l, r, largest;
  
  	/* adapted from lib/prio_heap.c */
  	while(1) {
  		l = left_child(idx);
  		r = right_child(idx);
  		largest = idx;
  
  		if ((l < cp->size) && dl_time_before(cp->elements[idx].dl,
  							cp->elements[l].dl))
  			largest = l;
  		if ((r < cp->size) && dl_time_before(cp->elements[largest].dl,
  							cp->elements[r].dl))
  			largest = r;
  		if (largest == idx)
  			break;
  
  		/* Push idx down the heap one level and bump one up */
  		cpudl_exchange(cp, largest, idx);
  		idx = largest;
  	}
  }
88f1ebbc2   Fengguang Wu   sched/deadline: F...
70
  static void cpudl_change_key(struct cpudl *cp, int idx, u64 new_dl)
6bfd6d72f   Juri Lelli   sched/deadline: s...
71
  {
eec751ed4   Juri Lelli   sched/deadline: S...
72
  	WARN_ON(idx == IDX_INVALID || !cpu_present(idx));
6bfd6d72f   Juri Lelli   sched/deadline: s...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  
  	if (dl_time_before(new_dl, cp->elements[idx].dl)) {
  		cp->elements[idx].dl = new_dl;
  		cpudl_heapify(cp, idx);
  	} else {
  		cp->elements[idx].dl = new_dl;
  		while (idx > 0 && dl_time_before(cp->elements[parent(idx)].dl,
  					cp->elements[idx].dl)) {
  			cpudl_exchange(cp, idx, parent(idx));
  			idx = parent(idx);
  		}
  	}
  }
  
  static inline int cpudl_maximum(struct cpudl *cp)
  {
  	return cp->elements[0].cpu;
  }
  
  /*
   * cpudl_find - find the best (later-dl) CPU in the system
   * @cp: the cpudl max-heap context
   * @p: the task
   * @later_mask: a mask to fill in with the selected CPUs (or NULL)
   *
   * Returns: int - best CPU (heap maximum if suitable)
   */
  int cpudl_find(struct cpudl *cp, struct task_struct *p,
  	       struct cpumask *later_mask)
  {
  	int best_cpu = -1;
  	const struct sched_dl_entity *dl_se = &p->dl;
91ec6778e   Juri Lelli   sched/deadline: F...
105
  	if (later_mask && cpumask_and(later_mask, later_mask, cp->free_cpus)) {
6bfd6d72f   Juri Lelli   sched/deadline: s...
106
107
108
109
110
111
112
113
114
115
  		best_cpu = cpumask_any(later_mask);
  		goto out;
  	} else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) &&
  			dl_time_before(dl_se->deadline, cp->elements[0].dl)) {
  		best_cpu = cpudl_maximum(cp);
  		if (later_mask)
  			cpumask_set_cpu(best_cpu, later_mask);
  	}
  
  out:
eec751ed4   Juri Lelli   sched/deadline: S...
116
  	WARN_ON(best_cpu != -1 && !cpu_present(best_cpu));
6bfd6d72f   Juri Lelli   sched/deadline: s...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  
  	return best_cpu;
  }
  
  /*
   * cpudl_set - update the cpudl max-heap
   * @cp: the cpudl max-heap context
   * @cpu: the target cpu
   * @dl: the new earliest deadline for this cpu
   *
   * Notes: assumes cpu_rq(cpu)->lock is locked
   *
   * Returns: (void)
   */
  void cpudl_set(struct cpudl *cp, int cpu, u64 dl, int is_valid)
  {
  	int old_idx, new_cpu;
  	unsigned long flags;
82b95800b   Boris Ostrovsky   sched/deadline: T...
135
  	WARN_ON(!cpu_present(cpu));
6bfd6d72f   Juri Lelli   sched/deadline: s...
136
137
  
  	raw_spin_lock_irqsave(&cp->lock, flags);
944770ab5   Peter Zijlstra   sched/deadline: R...
138
  	old_idx = cp->elements[cpu].idx;
6bfd6d72f   Juri Lelli   sched/deadline: s...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	if (!is_valid) {
  		/* remove item */
  		if (old_idx == IDX_INVALID) {
  			/*
  			 * Nothing to remove if old_idx was invalid.
  			 * This could happen if a rq_offline_dl is
  			 * called for a CPU without -dl tasks running.
  			 */
  			goto out;
  		}
  		new_cpu = cp->elements[cp->size - 1].cpu;
  		cp->elements[old_idx].dl = cp->elements[cp->size - 1].dl;
  		cp->elements[old_idx].cpu = new_cpu;
  		cp->size--;
944770ab5   Peter Zijlstra   sched/deadline: R...
153
154
  		cp->elements[new_cpu].idx = old_idx;
  		cp->elements[cpu].idx = IDX_INVALID;
6bfd6d72f   Juri Lelli   sched/deadline: s...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  		while (old_idx > 0 && dl_time_before(
  				cp->elements[parent(old_idx)].dl,
  				cp->elements[old_idx].dl)) {
  			cpudl_exchange(cp, old_idx, parent(old_idx));
  			old_idx = parent(old_idx);
  		}
  		cpumask_set_cpu(cpu, cp->free_cpus);
                  cpudl_heapify(cp, old_idx);
  
  		goto out;
  	}
  
  	if (old_idx == IDX_INVALID) {
  		cp->size++;
  		cp->elements[cp->size - 1].dl = 0;
  		cp->elements[cp->size - 1].cpu = cpu;
944770ab5   Peter Zijlstra   sched/deadline: R...
171
  		cp->elements[cpu].idx = cp->size - 1;
6bfd6d72f   Juri Lelli   sched/deadline: s...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  		cpudl_change_key(cp, cp->size - 1, dl);
  		cpumask_clear_cpu(cpu, cp->free_cpus);
  	} else {
  		cpudl_change_key(cp, old_idx, dl);
  	}
  
  out:
  	raw_spin_unlock_irqrestore(&cp->lock, flags);
  }
  
  /*
   * cpudl_init - initialize the cpudl structure
   * @cp: the cpudl max-heap context
   */
  int cpudl_init(struct cpudl *cp)
  {
  	int i;
  
  	memset(cp, 0, sizeof(*cp));
  	raw_spin_lock_init(&cp->lock);
  	cp->size = 0;
944770ab5   Peter Zijlstra   sched/deadline: R...
193
194
195
196
197
198
199
200
201
  
  	cp->elements = kcalloc(nr_cpu_ids,
  			       sizeof(struct cpudl_item),
  			       GFP_KERNEL);
  	if (!cp->elements)
  		return -ENOMEM;
  
  	if (!alloc_cpumask_var(&cp->free_cpus, GFP_KERNEL)) {
  		kfree(cp->elements);
6bfd6d72f   Juri Lelli   sched/deadline: s...
202
  		return -ENOMEM;
944770ab5   Peter Zijlstra   sched/deadline: R...
203
204
205
206
  	}
  
  	for_each_possible_cpu(i)
  		cp->elements[i].idx = IDX_INVALID;
6bfd6d72f   Juri Lelli   sched/deadline: s...
207
208
209
210
211
212
213
214
215
216
217
  	cpumask_setall(cp->free_cpus);
  
  	return 0;
  }
  
  /*
   * cpudl_cleanup - clean up the cpudl structure
   * @cp: the cpudl max-heap context
   */
  void cpudl_cleanup(struct cpudl *cp)
  {
6a7cd273d   Li Zefan   sched/deadline: F...
218
  	free_cpumask_var(cp->free_cpus);
944770ab5   Peter Zijlstra   sched/deadline: R...
219
  	kfree(cp->elements);
6bfd6d72f   Juri Lelli   sched/deadline: s...
220
  }