vtime.c 11.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *    Virtual cpu timer based timer functions.
 *
4
 *    Copyright IBM Corp. 2004, 2012
L
Linus Torvalds 已提交
5 6 7
 *    Author(s): Jan Glauber <jan.glauber@de.ibm.com>
 */

8 9
#include <linux/kernel_stat.h>
#include <linux/export.h>
L
Linus Torvalds 已提交
10 11
#include <linux/kernel.h>
#include <linux/timex.h>
12 13
#include <linux/types.h>
#include <linux/time.h>
L
Linus Torvalds 已提交
14

15
#include <asm/cputime.h>
16
#include <asm/vtimer.h>
17
#include <asm/vtime.h>
M
Martin Schwidefsky 已提交
18 19
#include <asm/cpu_mf.h>
#include <asm/smp.h>
L
Linus Torvalds 已提交
20

H
Heiko Carstens 已提交
21 22
#include "entry.h"

23
static void virt_timer_expire(void);
L
Linus Torvalds 已提交
24

25 26 27 28 29
static LIST_HEAD(virt_timer_list);
static DEFINE_SPINLOCK(virt_timer_lock);
static atomic64_t virt_timer_current;
static atomic64_t virt_timer_elapsed;

30
DEFINE_PER_CPU(u64, mt_cycles[8]);
M
Martin Schwidefsky 已提交
31 32
static DEFINE_PER_CPU(u64, mt_scaling_mult) = { 1 };
static DEFINE_PER_CPU(u64, mt_scaling_div) = { 1 };
33
static DEFINE_PER_CPU(u64, mt_scaling_jiffies);
M
Martin Schwidefsky 已提交
34

35
static inline u64 get_vtimer(void)
36
{
37
	u64 timer;
38

39
	asm volatile("stpt %0" : "=m" (timer));
40 41 42
	return timer;
}

43
static inline void set_vtimer(u64 expires)
44
{
45
	u64 timer;
46

47 48 49 50
	asm volatile(
		"	stpt	%0\n"	/* Store current cpu timer value */
		"	spt	%1"	/* Set new value imm. afterwards */
		: "=m" (timer) : "m" (expires));
51 52 53 54
	S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
	S390_lowcore.last_update_timer = expires;
}

55 56 57 58 59 60 61 62 63 64
static inline int virt_timer_forward(u64 elapsed)
{
	BUG_ON(!irqs_disabled());

	if (list_empty(&virt_timer_list))
		return 0;
	elapsed = atomic64_add_return(elapsed, &virt_timer_elapsed);
	return elapsed >= atomic64_read(&virt_timer_current);
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static void update_mt_scaling(void)
{
	u64 cycles_new[8], *cycles_old;
	u64 delta, fac, mult, div;
	int i;

	stcctm5(smp_cpu_mtid + 1, cycles_new);
	cycles_old = this_cpu_ptr(mt_cycles);
	fac = 1;
	mult = div = 0;
	for (i = 0; i <= smp_cpu_mtid; i++) {
		delta = cycles_new[i] - cycles_old[i];
		div += delta;
		mult *= i + 1;
		mult += delta * fac;
		fac *= i + 1;
	}
	div *= fac;
	if (div > 0) {
		/* Update scaling factor */
		__this_cpu_write(mt_scaling_mult, mult);
		__this_cpu_write(mt_scaling_div, div);
		memcpy(cycles_old, cycles_new,
		       sizeof(u64) * (smp_cpu_mtid + 1));
	}
	__this_cpu_write(mt_scaling_jiffies, jiffies_64);
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static inline u64 update_tsk_timer(unsigned long *tsk_vtime, u64 new)
{
	u64 delta;

	delta = new - *tsk_vtime;
	*tsk_vtime = new;
	return delta;
}


static inline u64 scale_vtime(u64 vtime)
{
	u64 mult = __this_cpu_read(mt_scaling_mult);
	u64 div = __this_cpu_read(mt_scaling_div);

	if (smp_cpu_mtid)
		return vtime * mult / div;
	return vtime;
}

static void account_system_index_scaled(struct task_struct *p,
					cputime_t cputime, cputime_t scaled,
					enum cpu_usage_stat index)
{
117
	p->stimescaled += cputime_to_nsecs(scaled);
118 119 120
	account_system_index_time(p, cputime, index);
}

L
Linus Torvalds 已提交
121 122 123 124
/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
125
static int do_account_vtime(struct task_struct *tsk)
L
Linus Torvalds 已提交
126
{
127
	u64 timer, clock, user, guest, system, hardirq, softirq, steal;
L
Linus Torvalds 已提交
128 129 130

	timer = S390_lowcore.last_update_timer;
	clock = S390_lowcore.last_update_clock;
131 132
	asm volatile(
		"	stpt	%0\n"	/* Store current cpu timer value */
133 134 135
#ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
		"	stckf	%1"	/* Store current tod clock value */
#else
136
		"	stck	%1"	/* Store current tod clock value */
137
#endif
138 139
		: "=m" (S390_lowcore.last_update_timer),
		  "=m" (S390_lowcore.last_update_clock));
140 141 142 143 144 145 146
	clock = S390_lowcore.last_update_clock - clock;
	timer -= S390_lowcore.last_update_timer;

	if (hardirq_count())
		S390_lowcore.hardirq_timer += timer;
	else
		S390_lowcore.system_timer += timer;
L
Linus Torvalds 已提交
147

148
	/* Update MT utilization calculation */
149
	if (smp_cpu_mtid &&
150 151
	    time_after64(jiffies_64, this_cpu_read(mt_scaling_jiffies)))
		update_mt_scaling();
M
Martin Schwidefsky 已提交
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	/* Calculate cputime delta */
	user = update_tsk_timer(&tsk->thread.user_timer,
				READ_ONCE(S390_lowcore.user_timer));
	guest = update_tsk_timer(&tsk->thread.guest_timer,
				 READ_ONCE(S390_lowcore.guest_timer));
	system = update_tsk_timer(&tsk->thread.system_timer,
				  READ_ONCE(S390_lowcore.system_timer));
	hardirq = update_tsk_timer(&tsk->thread.hardirq_timer,
				   READ_ONCE(S390_lowcore.hardirq_timer));
	softirq = update_tsk_timer(&tsk->thread.softirq_timer,
				   READ_ONCE(S390_lowcore.softirq_timer));
	S390_lowcore.steal_timer +=
		clock - user - guest - system - hardirq - softirq;

	/* Push account value */
	if (user) {
		account_user_time(tsk, user);
170
		tsk->utimescaled += cputime_to_nsecs(scale_vtime(user));
171
	}
M
Martin Schwidefsky 已提交
172

173 174
	if (guest) {
		account_guest_time(tsk, guest);
175
		tsk->utimescaled += cputime_to_nsecs(scale_vtime(guest));
M
Martin Schwidefsky 已提交
176
	}
177 178 179 180 181 182 183 184 185 186

	if (system)
		account_system_index_scaled(tsk, system, scale_vtime(system),
					    CPUTIME_SYSTEM);
	if (hardirq)
		account_system_index_scaled(tsk, hardirq, scale_vtime(hardirq),
					    CPUTIME_IRQ);
	if (softirq)
		account_system_index_scaled(tsk, softirq, scale_vtime(softirq),
					    CPUTIME_SOFTIRQ);
L
Linus Torvalds 已提交
187

188 189 190
	steal = S390_lowcore.steal_timer;
	if ((s64) steal > 0) {
		S390_lowcore.steal_timer = 0;
191
		account_steal_time(steal);
L
Linus Torvalds 已提交
192
	}
193

194
	return virt_timer_forward(user + guest + system + hardirq + softirq);
L
Linus Torvalds 已提交
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 204
	prev->thread.hardirq_timer = S390_lowcore.hardirq_timer;
	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 209
	S390_lowcore.hardirq_timer = current->thread.hardirq_timer;
	S390_lowcore.softirq_timer = current->thread.softirq_timer;
210
}
211

212 213 214 215 216
/*
 * In s390, accounting pending user time also implies
 * accounting system time in order to correctly compute
 * the stolen time accounting.
 */
217
void vtime_flush(struct task_struct *tsk)
218
{
219
	if (do_account_vtime(tsk))
220
		virt_timer_expire();
221 222
}

L
Linus Torvalds 已提交
223 224 225 226
/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
227
void vtime_account_irq_enter(struct task_struct *tsk)
L
Linus Torvalds 已提交
228
{
229
	u64 timer;
L
Linus Torvalds 已提交
230 231

	timer = S390_lowcore.last_update_timer;
232
	S390_lowcore.last_update_timer = get_vtimer();
233 234 235 236 237 238 239 240 241 242 243 244
	timer -= S390_lowcore.last_update_timer;

	if ((tsk->flags & PF_VCPU) && (irq_count() == 0))
		S390_lowcore.guest_timer += timer;
	else if (hardirq_count())
		S390_lowcore.hardirq_timer += timer;
	else if (in_serving_softirq())
		S390_lowcore.softirq_timer += timer;
	else
		S390_lowcore.system_timer += timer;

	virt_timer_forward(timer);
L
Linus Torvalds 已提交
245
}
246
EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
L
Linus Torvalds 已提交
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

L
Linus Torvalds 已提交
252 253 254 255 256 257
/*
 * Sorted add to a list. List is linear searched until first bigger
 * element is found.
 */
static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
{
258
	struct vtimer_list *tmp;
L
Linus Torvalds 已提交
259

260 261 262
	list_for_each_entry(tmp, head, entry) {
		if (tmp->expires > timer->expires) {
			list_add_tail(&timer->entry, &tmp->entry);
L
Linus Torvalds 已提交
263 264 265 266 267 268 269
			return;
		}
	}
	list_add_tail(&timer->entry, head);
}

/*
270
 * Handler for expired virtual CPU timer.
L
Linus Torvalds 已提交
271
 */
272
static void virt_timer_expire(void)
L
Linus Torvalds 已提交
273
{
274 275 276 277 278 279 280 281 282
	struct vtimer_list *timer, *tmp;
	unsigned long elapsed;
	LIST_HEAD(cb_list);

	/* walk timer list, fire all expired timers */
	spin_lock(&virt_timer_lock);
	elapsed = atomic64_read(&virt_timer_elapsed);
	list_for_each_entry_safe(timer, tmp, &virt_timer_list, entry) {
		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;
L
Linus Torvalds 已提交
287
	}
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	if (!list_empty(&virt_timer_list)) {
		timer = list_first_entry(&virt_timer_list,
					 struct vtimer_list, entry);
		atomic64_set(&virt_timer_current, timer->expires);
	}
	atomic64_sub(elapsed, &virt_timer_elapsed);
	spin_unlock(&virt_timer_lock);

	/* Do callbacks and recharge periodic timers */
	list_for_each_entry_safe(timer, tmp, &cb_list, entry) {
		list_del_init(&timer->entry);
		timer->function(timer->data);
		if (timer->interval) {
			/* Recharge interval timer */
			timer->expires = timer->interval +
				atomic64_read(&virt_timer_elapsed);
			spin_lock(&virt_timer_lock);
			list_add_sorted(timer, &virt_timer_list);
			spin_unlock(&virt_timer_lock);
		}
M
Martin Schwidefsky 已提交
308
	}
L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317 318 319
}

void init_virt_timer(struct vtimer_list *timer)
{
	timer->function = NULL;
	INIT_LIST_HEAD(&timer->entry);
}
EXPORT_SYMBOL(init_virt_timer);

static inline int vtimer_pending(struct vtimer_list *timer)
{
320
	return !list_empty(&timer->entry);
L
Linus Torvalds 已提交
321 322 323 324
}

static void internal_add_vtimer(struct vtimer_list *timer)
{
325 326 327 328 329
	if (list_empty(&virt_timer_list)) {
		/* First timer, just program it. */
		atomic64_set(&virt_timer_current, timer->expires);
		atomic64_set(&virt_timer_elapsed, 0);
		list_add(&timer->entry, &virt_timer_list);
330
	} else {
331 332 333 334
		/* Update timer against current base. */
		timer->expires += atomic64_read(&virt_timer_elapsed);
		if (likely((s64) timer->expires <
			   (s64) atomic64_read(&virt_timer_current)))
335
			/* The new timer expires before the current timer. */
336 337 338
			atomic64_set(&virt_timer_current, timer->expires);
		/* Insert new timer into the list. */
		list_add_sorted(timer, &virt_timer_list);
L
Linus Torvalds 已提交
339 340 341
	}
}

342
static void __add_vtimer(struct vtimer_list *timer, int periodic)
L
Linus Torvalds 已提交
343
{
344 345 346 347 348 349
	unsigned long flags;

	timer->interval = periodic ? timer->expires : 0;
	spin_lock_irqsave(&virt_timer_lock, flags);
	internal_add_vtimer(timer);
	spin_unlock_irqrestore(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
350 351 352 353 354
}

/*
 * add_virt_timer - add an oneshot virtual CPU timer
 */
355
void add_virt_timer(struct vtimer_list *timer)
L
Linus Torvalds 已提交
356
{
357
	__add_vtimer(timer, 0);
L
Linus Torvalds 已提交
358 359 360 361 362 363
}
EXPORT_SYMBOL(add_virt_timer);

/*
 * add_virt_timer_int - add an interval virtual CPU timer
 */
364
void add_virt_timer_periodic(struct vtimer_list *timer)
L
Linus Torvalds 已提交
365
{
366
	__add_vtimer(timer, 1);
L
Linus Torvalds 已提交
367 368 369
}
EXPORT_SYMBOL(add_virt_timer_periodic);

370
static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
L
Linus Torvalds 已提交
371 372
{
	unsigned long flags;
373
	int rc;
L
Linus Torvalds 已提交
374

375
	BUG_ON(!timer->function);
L
Linus Torvalds 已提交
376 377 378

	if (timer->expires == expires && vtimer_pending(timer))
		return 1;
379 380 381 382 383
	spin_lock_irqsave(&virt_timer_lock, flags);
	rc = vtimer_pending(timer);
	if (rc)
		list_del_init(&timer->entry);
	timer->interval = periodic ? expires : 0;
L
Linus Torvalds 已提交
384 385
	timer->expires = expires;
	internal_add_vtimer(timer);
386 387
	spin_unlock_irqrestore(&virt_timer_lock, flags);
	return rc;
L
Linus Torvalds 已提交
388
}
389 390 391 392

/*
 * returns whether it has modified a pending timer (1) or not (0)
 */
393
int mod_virt_timer(struct vtimer_list *timer, u64 expires)
394 395 396
{
	return __mod_vtimer(timer, expires, 0);
}
L
Linus Torvalds 已提交
397 398
EXPORT_SYMBOL(mod_virt_timer);

399 400 401
/*
 * returns whether it has modified a pending timer (1) or not (0)
 */
402
int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
403 404 405 406 407
{
	return __mod_vtimer(timer, expires, 1);
}
EXPORT_SYMBOL(mod_virt_timer_periodic);

L
Linus Torvalds 已提交
408
/*
409
 * Delete a virtual timer.
L
Linus Torvalds 已提交
410 411 412 413 414 415 416 417 418
 *
 * returns whether the deleted timer was pending (1) or not (0)
 */
int del_virt_timer(struct vtimer_list *timer)
{
	unsigned long flags;

	if (!vtimer_pending(timer))
		return 0;
419
	spin_lock_irqsave(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
420
	list_del_init(&timer->entry);
421
	spin_unlock_irqrestore(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
422 423 424 425 426 427 428
	return 1;
}
EXPORT_SYMBOL(del_virt_timer);

/*
 * Start the virtual CPU timer on the current CPU.
 */
429
void vtime_init(void)
L
Linus Torvalds 已提交
430
{
M
Martin Schwidefsky 已提交
431
	/* set initial cpu timer */
432
	set_vtimer(VTIMER_MAX_SLICE);
433 434 435 436 437 438 439
	/* Setup initial MT scaling values */
	if (smp_cpu_mtid) {
		__this_cpu_write(mt_scaling_jiffies, jiffies);
		__this_cpu_write(mt_scaling_mult, 1);
		__this_cpu_write(mt_scaling_div, 1);
		stcctm5(smp_cpu_mtid + 1, this_cpu_ptr(mt_cycles));
	}
L
Linus Torvalds 已提交
440
}