vtime.c 10.2 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 10 11
#include <linux/kernel_stat.h>
#include <linux/notifier.h>
#include <linux/kprobes.h>
#include <linux/export.h>
L
Linus Torvalds 已提交
12 13
#include <linux/kernel.h>
#include <linux/timex.h>
14 15
#include <linux/types.h>
#include <linux/time.h>
16
#include <linux/cpu.h>
17
#include <linux/smp.h>
L
Linus Torvalds 已提交
18

H
Heiko Carstens 已提交
19
#include <asm/irq_regs.h>
20
#include <asm/cputime.h>
21
#include <asm/vtimer.h>
22
#include <asm/irq.h>
M
Martin Schwidefsky 已提交
23
#include "entry.h"
L
Linus Torvalds 已提交
24

25
static void virt_timer_expire(void);
L
Linus Torvalds 已提交
26

27
DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
28

29 30 31 32 33 34
static LIST_HEAD(virt_timer_list);
static DEFINE_SPINLOCK(virt_timer_lock);
static atomic64_t virt_timer_current;
static atomic64_t virt_timer_elapsed;

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

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

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

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

54 55 56 57 58 59 60 61 62 63
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);
}

L
Linus Torvalds 已提交
64 65 66 67
/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
68
static int do_account_vtime(struct task_struct *tsk, int hardirq_offset)
L
Linus Torvalds 已提交
69
{
70
	struct thread_info *ti = task_thread_info(tsk);
71
	u64 timer, clock, user, system, steal;
L
Linus Torvalds 已提交
72 73 74

	timer = S390_lowcore.last_update_timer;
	clock = S390_lowcore.last_update_clock;
75 76 77 78 79
	asm volatile(
		"	stpt	%0\n"	/* Store current cpu timer value */
		"	stck	%1"	/* Store current tod clock value */
		: "=m" (S390_lowcore.last_update_timer),
		  "=m" (S390_lowcore.last_update_clock));
L
Linus Torvalds 已提交
80
	S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
81
	S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
L
Linus Torvalds 已提交
82

83 84 85 86
	user = S390_lowcore.user_timer - ti->user_timer;
	S390_lowcore.steal_timer -= user;
	ti->user_timer = S390_lowcore.user_timer;
	account_user_time(tsk, user, user);
L
Linus Torvalds 已提交
87

88 89 90
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
91
	account_system_time(tsk, hardirq_offset, system, system);
L
Linus Torvalds 已提交
92

93 94 95
	steal = S390_lowcore.steal_timer;
	if ((s64) steal > 0) {
		S390_lowcore.steal_timer = 0;
96
		account_steal_time(steal);
L
Linus Torvalds 已提交
97
	}
98 99

	return virt_timer_forward(user + system);
L
Linus Torvalds 已提交
100 101
}

102
void vtime_task_switch(struct task_struct *prev)
103
{
104 105 106 107 108 109
	struct thread_info *ti;

	do_account_vtime(prev, 0);
	ti = task_thread_info(prev);
	ti->user_timer = S390_lowcore.user_timer;
	ti->system_timer = S390_lowcore.system_timer;
110
	ti = task_thread_info(current);
111 112 113
	S390_lowcore.user_timer = ti->user_timer;
	S390_lowcore.system_timer = ti->system_timer;
}
114

115 116 117 118 119 120
/*
 * In s390, accounting pending user time also implies
 * accounting system time in order to correctly compute
 * the stolen time accounting.
 */
void vtime_account_user(struct task_struct *tsk)
121
{
122 123
	if (do_account_vtime(tsk, HARDIRQ_OFFSET))
		virt_timer_expire();
124 125
}

L
Linus Torvalds 已提交
126 127 128 129
/*
 * Update process times based on virtual cpu times stored by entry.S
 * to the lowcore fields user_timer, system_timer & steal_clock.
 */
130
void vtime_account_irq_enter(struct task_struct *tsk)
L
Linus Torvalds 已提交
131
{
132
	struct thread_info *ti = task_thread_info(tsk);
133
	u64 timer, system;
L
Linus Torvalds 已提交
134

135 136
	WARN_ON_ONCE(!irqs_disabled());

L
Linus Torvalds 已提交
137
	timer = S390_lowcore.last_update_timer;
138
	S390_lowcore.last_update_timer = get_vtimer();
L
Linus Torvalds 已提交
139 140
	S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;

141 142 143
	system = S390_lowcore.system_timer - ti->system_timer;
	S390_lowcore.steal_timer -= system;
	ti->system_timer = S390_lowcore.system_timer;
144
	account_system_time(tsk, 0, system, system);
145 146

	virt_timer_forward(system);
L
Linus Torvalds 已提交
147
}
148
EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
L
Linus Torvalds 已提交
149

150
void vtime_account_system(struct task_struct *tsk)
151
__attribute__((alias("vtime_account_irq_enter")));
152
EXPORT_SYMBOL_GPL(vtime_account_system);
153

M
Martin Schwidefsky 已提交
154
void __kprobes vtime_stop_cpu(void)
L
Linus Torvalds 已提交
155
{
156
	struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
M
Martin Schwidefsky 已提交
157 158
	unsigned long long idle_time;
	unsigned long psw_mask;
159

M
Martin Schwidefsky 已提交
160
	trace_hardirqs_on();
161

M
Martin Schwidefsky 已提交
162 163 164 165 166 167
	/* Wait for external, I/O or machine check interrupt. */
	psw_mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_DAT |
		PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
	idle->nohz_delay = 0;

	/* Call the assembler magic in entry.S */
168
	psw_idle(idle, psw_mask);
M
Martin Schwidefsky 已提交
169 170

	/* Account time spent with enabled wait psw loaded as idle time. */
171 172
	idle->sequence++;
	smp_wmb();
173 174
	idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
	idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
175 176
	idle->idle_time += idle_time;
	idle->idle_count++;
M
Martin Schwidefsky 已提交
177
	account_idle_time(idle_time);
178 179
	smp_wmb();
	idle->sequence++;
L
Linus Torvalds 已提交
180 181
}

182 183
cputime64_t s390_get_idle_time(int cpu)
{
M
Martin Schwidefsky 已提交
184 185
	struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
	unsigned long long now, idle_enter, idle_exit;
186
	unsigned int sequence;
187

M
Martin Schwidefsky 已提交
188
	do {
189
		now = get_tod_clock();
M
Martin Schwidefsky 已提交
190
		sequence = ACCESS_ONCE(idle->sequence);
191 192
		idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
		idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
M
Martin Schwidefsky 已提交
193
	} while ((sequence & 1) || (idle->sequence != sequence));
194
	return idle_enter ? ((idle_exit ?: now) - idle_enter) : 0;
195 196
}

L
Linus Torvalds 已提交
197 198 199 200 201 202
/*
 * 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)
{
203
	struct vtimer_list *tmp;
L
Linus Torvalds 已提交
204

205 206 207
	list_for_each_entry(tmp, head, entry) {
		if (tmp->expires > timer->expires) {
			list_add_tail(&timer->entry, &tmp->entry);
L
Linus Torvalds 已提交
208 209 210 211 212 213 214
			return;
		}
	}
	list_add_tail(&timer->entry, head);
}

/*
215
 * Handler for expired virtual CPU timer.
L
Linus Torvalds 已提交
216
 */
217
static void virt_timer_expire(void)
L
Linus Torvalds 已提交
218
{
219 220 221 222 223 224 225 226 227
	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)
228
			/* move expired timer to the callback queue */
229
			list_move_tail(&timer->entry, &cb_list);
230
		else
231
			timer->expires -= elapsed;
L
Linus Torvalds 已提交
232
	}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	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 已提交
253
	}
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264
}

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)
{
265
	return !list_empty(&timer->entry);
L
Linus Torvalds 已提交
266 267 268 269
}

static void internal_add_vtimer(struct vtimer_list *timer)
{
270 271 272 273 274
	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);
275
	} else {
276 277 278 279
		/* Update timer against current base. */
		timer->expires += atomic64_read(&virt_timer_elapsed);
		if (likely((s64) timer->expires <
			   (s64) atomic64_read(&virt_timer_current)))
280
			/* The new timer expires before the current timer. */
281 282 283
			atomic64_set(&virt_timer_current, timer->expires);
		/* Insert new timer into the list. */
		list_add_sorted(timer, &virt_timer_list);
L
Linus Torvalds 已提交
284 285 286
	}
}

287
static void __add_vtimer(struct vtimer_list *timer, int periodic)
L
Linus Torvalds 已提交
288
{
289 290 291 292 293 294
	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 已提交
295 296 297 298 299
}

/*
 * add_virt_timer - add an oneshot virtual CPU timer
 */
300
void add_virt_timer(struct vtimer_list *timer)
L
Linus Torvalds 已提交
301
{
302
	__add_vtimer(timer, 0);
L
Linus Torvalds 已提交
303 304 305 306 307 308
}
EXPORT_SYMBOL(add_virt_timer);

/*
 * add_virt_timer_int - add an interval virtual CPU timer
 */
309
void add_virt_timer_periodic(struct vtimer_list *timer)
L
Linus Torvalds 已提交
310
{
311
	__add_vtimer(timer, 1);
L
Linus Torvalds 已提交
312 313 314
}
EXPORT_SYMBOL(add_virt_timer_periodic);

315
static int __mod_vtimer(struct vtimer_list *timer, u64 expires, int periodic)
L
Linus Torvalds 已提交
316 317
{
	unsigned long flags;
318
	int rc;
L
Linus Torvalds 已提交
319

320
	BUG_ON(!timer->function);
L
Linus Torvalds 已提交
321 322 323

	if (timer->expires == expires && vtimer_pending(timer))
		return 1;
324 325 326 327 328
	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 已提交
329 330
	timer->expires = expires;
	internal_add_vtimer(timer);
331 332
	spin_unlock_irqrestore(&virt_timer_lock, flags);
	return rc;
L
Linus Torvalds 已提交
333
}
334 335 336 337

/*
 * returns whether it has modified a pending timer (1) or not (0)
 */
338
int mod_virt_timer(struct vtimer_list *timer, u64 expires)
339 340 341
{
	return __mod_vtimer(timer, expires, 0);
}
L
Linus Torvalds 已提交
342 343
EXPORT_SYMBOL(mod_virt_timer);

344 345 346
/*
 * returns whether it has modified a pending timer (1) or not (0)
 */
347
int mod_virt_timer_periodic(struct vtimer_list *timer, u64 expires)
348 349 350 351 352
{
	return __mod_vtimer(timer, expires, 1);
}
EXPORT_SYMBOL(mod_virt_timer_periodic);

L
Linus Torvalds 已提交
353
/*
354
 * Delete a virtual timer.
L
Linus Torvalds 已提交
355 356 357 358 359 360 361 362 363
 *
 * 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;
364
	spin_lock_irqsave(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
365
	list_del_init(&timer->entry);
366
	spin_unlock_irqrestore(&virt_timer_lock, flags);
L
Linus Torvalds 已提交
367 368 369 370 371 372 373
	return 1;
}
EXPORT_SYMBOL(del_virt_timer);

/*
 * Start the virtual CPU timer on the current CPU.
 */
374
void init_cpu_vtimer(void)
L
Linus Torvalds 已提交
375
{
M
Martin Schwidefsky 已提交
376
	/* set initial cpu timer */
377
	set_vtimer(VTIMER_MAX_SLICE);
L
Linus Torvalds 已提交
378 379
}

380 381
static int s390_nohz_notify(struct notifier_block *self, unsigned long action,
			    void *hcpu)
382 383 384 385 386
{
	struct s390_idle_data *idle;
	long cpu = (long) hcpu;

	idle = &per_cpu(s390_idle, cpu);
387
	switch (action & ~CPU_TASKS_FROZEN) {
388 389 390 391 392 393 394 395
	case CPU_DYING:
		idle->nohz_delay = 0;
	default:
		break;
	}
	return NOTIFY_OK;
}

L
Linus Torvalds 已提交
396 397
void __init vtime_init(void)
{
M
Martin Schwidefsky 已提交
398
	/* Enable cpu timer interrupts on the boot cpu. */
L
Linus Torvalds 已提交
399
	init_cpu_vtimer();
400
	cpu_notifier(s390_nohz_notify, 0);
L
Linus Torvalds 已提交
401
}