xref: /linux/arch/s390/kernel/vtime.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  *    Virtual cpu timer based timer functions.
3  *
4  *    Copyright IBM Corp. 2004, 2012
5  *    Author(s): Jan Glauber <jan.glauber@de.ibm.com>
6  */
7 
8 #include <linux/kernel_stat.h>
9 #include <linux/sched/cputime.h>
10 #include <linux/export.h>
11 #include <linux/kernel.h>
12 #include <linux/timex.h>
13 #include <linux/types.h>
14 #include <linux/time.h>
15 
16 #include <asm/vtimer.h>
17 #include <asm/vtime.h>
18 #include <asm/cpu_mf.h>
19 #include <asm/smp.h>
20 
21 #include "entry.h"
22 
23 static void virt_timer_expire(void);
24 
25 static LIST_HEAD(virt_timer_list);
26 static DEFINE_SPINLOCK(virt_timer_lock);
27 static atomic64_t virt_timer_current;
28 static atomic64_t virt_timer_elapsed;
29 
30 DEFINE_PER_CPU(u64, mt_cycles[8]);
31 static DEFINE_PER_CPU(u64, mt_scaling_mult) = { 1 };
32 static DEFINE_PER_CPU(u64, mt_scaling_div) = { 1 };
33 static DEFINE_PER_CPU(u64, mt_scaling_jiffies);
34 
35 static inline u64 get_vtimer(void)
36 {
37 	u64 timer;
38 
39 	asm volatile("stpt %0" : "=m" (timer));
40 	return timer;
41 }
42 
43 static inline void set_vtimer(u64 expires)
44 {
45 	u64 timer;
46 
47 	asm volatile(
48 		"	stpt	%0\n"	/* Store current cpu timer value */
49 		"	spt	%1"	/* Set new value imm. afterwards */
50 		: "=m" (timer) : "m" (expires));
51 	S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
52 	S390_lowcore.last_update_timer = expires;
53 }
54 
55 static inline int virt_timer_forward(u64 elapsed)
56 {
57 	BUG_ON(!irqs_disabled());
58 
59 	if (list_empty(&virt_timer_list))
60 		return 0;
61 	elapsed = atomic64_add_return(elapsed, &virt_timer_elapsed);
62 	return elapsed >= atomic64_read(&virt_timer_current);
63 }
64 
65 static void update_mt_scaling(void)
66 {
67 	u64 cycles_new[8], *cycles_old;
68 	u64 delta, fac, mult, div;
69 	int i;
70 
71 	stcctm5(smp_cpu_mtid + 1, cycles_new);
72 	cycles_old = this_cpu_ptr(mt_cycles);
73 	fac = 1;
74 	mult = div = 0;
75 	for (i = 0; i <= smp_cpu_mtid; i++) {
76 		delta = cycles_new[i] - cycles_old[i];
77 		div += delta;
78 		mult *= i + 1;
79 		mult += delta * fac;
80 		fac *= i + 1;
81 	}
82 	div *= fac;
83 	if (div > 0) {
84 		/* Update scaling factor */
85 		__this_cpu_write(mt_scaling_mult, mult);
86 		__this_cpu_write(mt_scaling_div, div);
87 		memcpy(cycles_old, cycles_new,
88 		       sizeof(u64) * (smp_cpu_mtid + 1));
89 	}
90 	__this_cpu_write(mt_scaling_jiffies, jiffies_64);
91 }
92 
93 static inline u64 update_tsk_timer(unsigned long *tsk_vtime, u64 new)
94 {
95 	u64 delta;
96 
97 	delta = new - *tsk_vtime;
98 	*tsk_vtime = new;
99 	return delta;
100 }
101 
102 
103 static inline u64 scale_vtime(u64 vtime)
104 {
105 	u64 mult = __this_cpu_read(mt_scaling_mult);
106 	u64 div = __this_cpu_read(mt_scaling_div);
107 
108 	if (smp_cpu_mtid)
109 		return vtime * mult / div;
110 	return vtime;
111 }
112 
113 static void account_system_index_scaled(struct task_struct *p,
114 					u64 cputime, u64 scaled,
115 					enum cpu_usage_stat index)
116 {
117 	p->stimescaled += cputime_to_nsecs(scaled);
118 	account_system_index_time(p, cputime_to_nsecs(cputime), index);
119 }
120 
121 /*
122  * Update process times based on virtual cpu times stored by entry.S
123  * to the lowcore fields user_timer, system_timer & steal_clock.
124  */
125 static int do_account_vtime(struct task_struct *tsk)
126 {
127 	u64 timer, clock, user, guest, system, hardirq, softirq, steal;
128 
129 	timer = S390_lowcore.last_update_timer;
130 	clock = S390_lowcore.last_update_clock;
131 	asm volatile(
132 		"	stpt	%0\n"	/* Store current cpu timer value */
133 #ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
134 		"	stckf	%1"	/* Store current tod clock value */
135 #else
136 		"	stck	%1"	/* Store current tod clock value */
137 #endif
138 		: "=m" (S390_lowcore.last_update_timer),
139 		  "=m" (S390_lowcore.last_update_clock));
140 	clock = S390_lowcore.last_update_clock - clock;
141 	timer -= S390_lowcore.last_update_timer;
142 
143 	if (hardirq_count())
144 		S390_lowcore.hardirq_timer += timer;
145 	else
146 		S390_lowcore.system_timer += timer;
147 
148 	/* Update MT utilization calculation */
149 	if (smp_cpu_mtid &&
150 	    time_after64(jiffies_64, this_cpu_read(mt_scaling_jiffies)))
151 		update_mt_scaling();
152 
153 	/* Calculate cputime delta */
154 	user = update_tsk_timer(&tsk->thread.user_timer,
155 				READ_ONCE(S390_lowcore.user_timer));
156 	guest = update_tsk_timer(&tsk->thread.guest_timer,
157 				 READ_ONCE(S390_lowcore.guest_timer));
158 	system = update_tsk_timer(&tsk->thread.system_timer,
159 				  READ_ONCE(S390_lowcore.system_timer));
160 	hardirq = update_tsk_timer(&tsk->thread.hardirq_timer,
161 				   READ_ONCE(S390_lowcore.hardirq_timer));
162 	softirq = update_tsk_timer(&tsk->thread.softirq_timer,
163 				   READ_ONCE(S390_lowcore.softirq_timer));
164 	S390_lowcore.steal_timer +=
165 		clock - user - guest - system - hardirq - softirq;
166 
167 	/* Push account value */
168 	if (user) {
169 		account_user_time(tsk, cputime_to_nsecs(user));
170 		tsk->utimescaled += cputime_to_nsecs(scale_vtime(user));
171 	}
172 
173 	if (guest) {
174 		account_guest_time(tsk, cputime_to_nsecs(guest));
175 		tsk->utimescaled += cputime_to_nsecs(scale_vtime(guest));
176 	}
177 
178 	if (system)
179 		account_system_index_scaled(tsk, system, scale_vtime(system),
180 					    CPUTIME_SYSTEM);
181 	if (hardirq)
182 		account_system_index_scaled(tsk, hardirq, scale_vtime(hardirq),
183 					    CPUTIME_IRQ);
184 	if (softirq)
185 		account_system_index_scaled(tsk, softirq, scale_vtime(softirq),
186 					    CPUTIME_SOFTIRQ);
187 
188 	steal = S390_lowcore.steal_timer;
189 	if ((s64) steal > 0) {
190 		S390_lowcore.steal_timer = 0;
191 		account_steal_time(cputime_to_nsecs(steal));
192 	}
193 
194 	return virt_timer_forward(user + guest + system + hardirq + softirq);
195 }
196 
197 void vtime_task_switch(struct task_struct *prev)
198 {
199 	do_account_vtime(prev);
200 	prev->thread.user_timer = S390_lowcore.user_timer;
201 	prev->thread.guest_timer = S390_lowcore.guest_timer;
202 	prev->thread.system_timer = S390_lowcore.system_timer;
203 	prev->thread.hardirq_timer = S390_lowcore.hardirq_timer;
204 	prev->thread.softirq_timer = S390_lowcore.softirq_timer;
205 	S390_lowcore.user_timer = current->thread.user_timer;
206 	S390_lowcore.guest_timer = current->thread.guest_timer;
207 	S390_lowcore.system_timer = current->thread.system_timer;
208 	S390_lowcore.hardirq_timer = current->thread.hardirq_timer;
209 	S390_lowcore.softirq_timer = current->thread.softirq_timer;
210 }
211 
212 /*
213  * In s390, accounting pending user time also implies
214  * accounting system time in order to correctly compute
215  * the stolen time accounting.
216  */
217 void vtime_flush(struct task_struct *tsk)
218 {
219 	if (do_account_vtime(tsk))
220 		virt_timer_expire();
221 }
222 
223 /*
224  * Update process times based on virtual cpu times stored by entry.S
225  * to the lowcore fields user_timer, system_timer & steal_clock.
226  */
227 void vtime_account_irq_enter(struct task_struct *tsk)
228 {
229 	u64 timer;
230 
231 	timer = S390_lowcore.last_update_timer;
232 	S390_lowcore.last_update_timer = get_vtimer();
233 	timer -= S390_lowcore.last_update_timer;
234 
235 	if ((tsk->flags & PF_VCPU) && (irq_count() == 0))
236 		S390_lowcore.guest_timer += timer;
237 	else if (hardirq_count())
238 		S390_lowcore.hardirq_timer += timer;
239 	else if (in_serving_softirq())
240 		S390_lowcore.softirq_timer += timer;
241 	else
242 		S390_lowcore.system_timer += timer;
243 
244 	virt_timer_forward(timer);
245 }
246 EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
247 
248 void vtime_account_system(struct task_struct *tsk)
249 __attribute__((alias("vtime_account_irq_enter")));
250 EXPORT_SYMBOL_GPL(vtime_account_system);
251 
252 /*
253  * Sorted add to a list. List is linear searched until first bigger
254  * element is found.
255  */
256 static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
257 {
258 	struct vtimer_list *tmp;
259 
260 	list_for_each_entry(tmp, head, entry) {
261 		if (tmp->expires > timer->expires) {
262 			list_add_tail(&timer->entry, &tmp->entry);
263 			return;
264 		}
265 	}
266 	list_add_tail(&timer->entry, head);
267 }
268 
269 /*
270  * Handler for expired virtual CPU timer.
271  */
272 static void virt_timer_expire(void)
273 {
274 	struct vtimer_list *timer, *tmp;
275 	unsigned long elapsed;
276 	LIST_HEAD(cb_list);
277 
278 	/* walk timer list, fire all expired timers */
279 	spin_lock(&virt_timer_lock);
280 	elapsed = atomic64_read(&virt_timer_elapsed);
281 	list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
282 		if (timer->expires < elapsed)
283 			/* move expired timer to the callback queue */
284 			list_move_tail(&timer->entry, &cb_list);
285 		else
286 			timer->expires -= elapsed;
287 	}
288 	if (!list_empty(&virt_timer_list)) {
289 		timer = list_first_entry(&virt_timer_list,
290 					 struct vtimer_list, entry);
291 		atomic64_set(&virt_timer_current, timer->expires);
292 	}
293 	atomic64_sub(elapsed, &virt_timer_elapsed);
294 	spin_unlock(&virt_timer_lock);
295 
296 	/* Do callbacks and recharge periodic timers */
297 	list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
298 		list_del_init(&timer->entry);
299 		timer->function(timer->data);
300 		if (timer->interval) {
301 			/* Recharge interval timer */
302 			timer->expires = timer->interval +
303 				atomic64_read(&virt_timer_elapsed);
304 			spin_lock(&virt_timer_lock);
305 			list_add_sorted(timer, &virt_timer_list);
306 			spin_unlock(&virt_timer_lock);
307 		}
308 	}
309 }
310 
311 void init_virt_timer(struct vtimer_list *timer)
312 {
313 	timer->function = NULL;
314 	INIT_LIST_HEAD(&timer->entry);
315 }
316 EXPORT_SYMBOL(init_virt_timer);
317 
318 static inline int vtimer_pending(struct vtimer_list *timer)
319 {
320 	return !list_empty(&timer->entry);
321 }
322 
323 static void internal_add_vtimer(struct vtimer_list *timer)
324 {
325 	if (list_empty(&virt_timer_list)) {
326 		/* First timer, just program it. */
327 		atomic64_set(&virt_timer_current, timer->expires);
328 		atomic64_set(&virt_timer_elapsed, 0);
329 		list_add(&timer->entry, &virt_timer_list);
330 	} else {
331 		/* Update timer against current base. */
332 		timer->expires += atomic64_read(&virt_timer_elapsed);
333 		if (likely((s64) timer->expires <
334 			   (s64) atomic64_read(&virt_timer_current)))
335 			/* The new timer expires before the current timer. */
336 			atomic64_set(&virt_timer_current, timer->expires);
337 		/* Insert new timer into the list. */
338 		list_add_sorted(timer, &virt_timer_list);
339 	}
340 }
341 
342 static void __add_vtimer(struct vtimer_list *timer, int periodic)
343 {
344 	unsigned long flags;
345 
346 	timer->interval = periodic ? timer->expires : 0;
347 	spin_lock_irqsave(&virt_timer_lock, flags);
348 	internal_add_vtimer(timer);
349 	spin_unlock_irqrestore(&virt_timer_lock, flags);
350 }
351 
352 /*
353  * add_virt_timer - add a oneshot virtual CPU timer
354  */
355 void add_virt_timer(struct vtimer_list *timer)
356 {
357 	__add_vtimer(timer, 0);
358 }
359 EXPORT_SYMBOL(add_virt_timer);
360 
361 /*
362  * add_virt_timer_int - add an interval virtual CPU timer
363  */
364 void add_virt_timer_periodic(struct vtimer_list *timer)
365 {
366 	__add_vtimer(timer, 1);
367 }
368 EXPORT_SYMBOL(add_virt_timer_periodic);
369 
370 static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
371 {
372 	unsigned long flags;
373 	int rc;
374 
375 	BUG_ON(!timer->function);
376 
377 	if (timer->expires == expires && vtimer_pending(timer))
378 		return 1;
379 	spin_lock_irqsave(&virt_timer_lock, flags);
380 	rc = vtimer_pending(timer);
381 	if (rc)
382 		list_del_init(&timer->entry);
383 	timer->interval = periodic ? expires : 0;
384 	timer->expires = expires;
385 	internal_add_vtimer(timer);
386 	spin_unlock_irqrestore(&virt_timer_lock, flags);
387 	return rc;
388 }
389 
390 /*
391  * returns whether it has modified a pending timer (1) or not (0)
392  */
393 int mod_virt_timer(struct vtimer_list *timer, u64 expires)
394 {
395 	return __mod_vtimer(timer, expires, 0);
396 }
397 EXPORT_SYMBOL(mod_virt_timer);
398 
399 /*
400  * returns whether it has modified a pending timer (1) or not (0)
401  */
402 int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
403 {
404 	return __mod_vtimer(timer, expires, 1);
405 }
406 EXPORT_SYMBOL(mod_virt_timer_periodic);
407 
408 /*
409  * Delete a virtual timer.
410  *
411  * returns whether the deleted timer was pending (1) or not (0)
412  */
413 int del_virt_timer(struct vtimer_list *timer)
414 {
415 	unsigned long flags;
416 
417 	if (!vtimer_pending(timer))
418 		return 0;
419 	spin_lock_irqsave(&virt_timer_lock, flags);
420 	list_del_init(&timer->entry);
421 	spin_unlock_irqrestore(&virt_timer_lock, flags);
422 	return 1;
423 }
424 EXPORT_SYMBOL(del_virt_timer);
425 
426 /*
427  * Start the virtual CPU timer on the current CPU.
428  */
429 void vtime_init(void)
430 {
431 	/* set initial cpu timer */
432 	set_vtimer(VTIMER_MAX_SLICE);
433 	/* Setup initial MT scaling values */
434 	if (smp_cpu_mtid) {
435 		__this_cpu_write(mt_scaling_jiffies, jiffies);
436 		__this_cpu_write(mt_scaling_mult, 1);
437 		__this_cpu_write(mt_scaling_div, 1);
438 		stcctm5(smp_cpu_mtid + 1, this_cpu_ptr(mt_cycles));
439 	}
440 }
441