Merge git://git.kernel.org/pub/scm/linux/kernel/git/steve/gfs2-2.6-nmw
[linux-2.6] / arch / s390 / appldata / appldata_base.c
CommitLineData
1da177e4
LT
1/*
2 * arch/s390/appldata/appldata_base.c
3 *
4 * Base infrastructure for Linux-z/VM Monitor Stream, Stage 1.
5 * Exports appldata_register_ops() and appldata_unregister_ops() for the
6 * data gathering modules.
7 *
d3ae942d 8 * Copyright IBM Corp. 2003, 2008
1da177e4 9 *
5b5dd21a 10 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
1da177e4
LT
11 */
12
e7534b0e
GS
13#define KMSG_COMPONENT "appldata"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
1da177e4
LT
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/slab.h>
19#include <linux/errno.h>
1da177e4
LT
20#include <linux/interrupt.h>
21#include <linux/proc_fs.h>
2dcea57a 22#include <linux/mm.h>
1da177e4
LT
23#include <linux/swap.h>
24#include <linux/pagemap.h>
25#include <linux/sysctl.h>
1da177e4
LT
26#include <linux/notifier.h>
27#include <linux/cpu.h>
f26d583e 28#include <linux/workqueue.h>
1f38d613
GS
29#include <asm/appldata.h>
30#include <asm/timer.h>
31#include <asm/uaccess.h>
32#include <asm/io.h>
33#include <asm/smp.h>
1da177e4
LT
34
35#include "appldata.h"
36
37
1da177e4
LT
38#define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
39 sampling interval in
40 milliseconds */
41
42#define TOD_MICRO 0x01000 /* nr. of TOD clock units
43 for 1 microsecond */
1da177e4
LT
44/*
45 * /proc entries (sysctl)
46 */
47static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
48static int appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
49 void __user *buffer, size_t *lenp, loff_t *ppos);
50static int appldata_interval_handler(ctl_table *ctl, int write,
51 struct file *filp,
52 void __user *buffer,
53 size_t *lenp, loff_t *ppos);
54
55static struct ctl_table_header *appldata_sysctl_header;
56static struct ctl_table appldata_table[] = {
57 {
1da177e4
LT
58 .procname = "timer",
59 .mode = S_IRUGO | S_IWUSR,
60 .proc_handler = &appldata_timer_handler,
61 },
62 {
1da177e4
LT
63 .procname = "interval",
64 .mode = S_IRUGO | S_IWUSR,
65 .proc_handler = &appldata_interval_handler,
66 },
37e3a6ac 67 { },
1da177e4
LT
68};
69
70static struct ctl_table appldata_dir_table[] = {
71 {
1da177e4
LT
72 .procname = appldata_proc_name,
73 .maxlen = 0,
74 .mode = S_IRUGO | S_IXUGO,
75 .child = appldata_table,
76 },
37e3a6ac 77 { },
1da177e4
LT
78};
79
80/*
81 * Timer
82 */
2b67fc46 83static DEFINE_PER_CPU(struct vtimer_list, appldata_timer);
1da177e4
LT
84static atomic_t appldata_expire_count = ATOMIC_INIT(0);
85
86static DEFINE_SPINLOCK(appldata_timer_lock);
87static int appldata_interval = APPLDATA_CPU_INTERVAL;
88static int appldata_timer_active;
89
90/*
f26d583e 91 * Work queue
1da177e4 92 */
f26d583e 93static struct workqueue_struct *appldata_wq;
6d5aefb8
DH
94static void appldata_work_fn(struct work_struct *work);
95static DECLARE_WORK(appldata_work, appldata_work_fn);
f26d583e 96
1da177e4
LT
97
98/*
99 * Ops list
100 */
b1ad171e 101static DEFINE_MUTEX(appldata_ops_mutex);
1da177e4
LT
102static LIST_HEAD(appldata_ops_list);
103
104
f26d583e 105/*************************** timer, work, DIAG *******************************/
1da177e4
LT
106/*
107 * appldata_timer_function()
108 *
f26d583e 109 * schedule work and reschedule timer
1da177e4 110 */
9d0a57cb 111static void appldata_timer_function(unsigned long data)
1da177e4 112{
1da177e4
LT
113 if (atomic_dec_and_test(&appldata_expire_count)) {
114 atomic_set(&appldata_expire_count, num_online_cpus());
f26d583e 115 queue_work(appldata_wq, (struct work_struct *) data);
1da177e4
LT
116 }
117}
118
119/*
f26d583e 120 * appldata_work_fn()
1da177e4
LT
121 *
122 * call data gathering function for each (active) module
123 */
6d5aefb8 124static void appldata_work_fn(struct work_struct *work)
1da177e4
LT
125{
126 struct list_head *lh;
127 struct appldata_ops *ops;
128 int i;
129
1da177e4 130 i = 0;
1760537b 131 get_online_cpus();
b1ad171e 132 mutex_lock(&appldata_ops_mutex);
1da177e4
LT
133 list_for_each(lh, &appldata_ops_list) {
134 ops = list_entry(lh, struct appldata_ops, list);
1da177e4
LT
135 if (ops->active == 1) {
136 ops->callback(ops->data);
137 }
138 }
b1ad171e 139 mutex_unlock(&appldata_ops_mutex);
1760537b 140 put_online_cpus();
1da177e4
LT
141}
142
143/*
144 * appldata_diag()
145 *
146 * prepare parameter list, issue DIAG 0xDC
147 */
5b5dd21a
GS
148int appldata_diag(char record_nr, u16 function, unsigned long buffer,
149 u16 length, char *mod_lvl)
1da177e4 150{
1f38d613 151 struct appldata_product_id id = {
1da177e4 152 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
1f38d613
GS
153 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
154 .prod_fn = 0xD5D3, /* "NL" */
1f38d613
GS
155 .version_nr = 0xF2F6, /* "26" */
156 .release_nr = 0xF0F1, /* "01" */
1da177e4
LT
157 };
158
925afbd6
GS
159 id.record_nr = record_nr;
160 id.mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
1f38d613 161 return appldata_asm(&id, function, (void *) buffer, length);
1da177e4 162}
f26d583e 163/************************ timer, work, DIAG <END> ****************************/
1da177e4
LT
164
165
166/****************************** /proc stuff **********************************/
167
168/*
169 * appldata_mod_vtimer_wrap()
170 *
3bb447fc 171 * wrapper function for mod_virt_timer(), because smp_call_function_single()
1da177e4
LT
172 * accepts only one parameter.
173 */
174static void __appldata_mod_vtimer_wrap(void *p) {
175 struct {
176 struct vtimer_list *timer;
177 u64 expires;
178 } *args = p;
43ae8a1b 179 mod_virt_timer_periodic(args->timer, args->expires);
1da177e4
LT
180}
181
182#define APPLDATA_ADD_TIMER 0
183#define APPLDATA_DEL_TIMER 1
184#define APPLDATA_MOD_TIMER 2
185
186/*
187 * __appldata_vtimer_setup()
188 *
189 * Add, delete or modify virtual timers on all online cpus.
190 * The caller needs to get the appldata_timer_lock spinlock.
191 */
192static void
193__appldata_vtimer_setup(int cmd)
194{
195 u64 per_cpu_interval;
196 int i;
197
198 switch (cmd) {
199 case APPLDATA_ADD_TIMER:
200 if (appldata_timer_active)
201 break;
202 per_cpu_interval = (u64) (appldata_interval*1000 /
203 num_online_cpus()) * TOD_MICRO;
204 for_each_online_cpu(i) {
205 per_cpu(appldata_timer, i).expires = per_cpu_interval;
3bb447fc
HC
206 smp_call_function_single(i, add_virt_timer_periodic,
207 &per_cpu(appldata_timer, i),
8691e5a8 208 1);
1da177e4
LT
209 }
210 appldata_timer_active = 1;
1da177e4
LT
211 break;
212 case APPLDATA_DEL_TIMER:
213 for_each_online_cpu(i)
214 del_virt_timer(&per_cpu(appldata_timer, i));
215 if (!appldata_timer_active)
216 break;
217 appldata_timer_active = 0;
218 atomic_set(&appldata_expire_count, num_online_cpus());
1da177e4
LT
219 break;
220 case APPLDATA_MOD_TIMER:
221 per_cpu_interval = (u64) (appldata_interval*1000 /
222 num_online_cpus()) * TOD_MICRO;
223 if (!appldata_timer_active)
224 break;
225 for_each_online_cpu(i) {
226 struct {
227 struct vtimer_list *timer;
228 u64 expires;
229 } args;
230 args.timer = &per_cpu(appldata_timer, i);
231 args.expires = per_cpu_interval;
3bb447fc 232 smp_call_function_single(i, __appldata_mod_vtimer_wrap,
8691e5a8 233 &args, 1);
1da177e4
LT
234 }
235 }
236}
237
238/*
239 * appldata_timer_handler()
240 *
241 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
242 */
243static int
244appldata_timer_handler(ctl_table *ctl, int write, struct file *filp,
245 void __user *buffer, size_t *lenp, loff_t *ppos)
246{
247 int len;
248 char buf[2];
249
250 if (!*lenp || *ppos) {
251 *lenp = 0;
252 return 0;
253 }
254 if (!write) {
255 len = sprintf(buf, appldata_timer_active ? "1\n" : "0\n");
256 if (len > *lenp)
257 len = *lenp;
258 if (copy_to_user(buffer, buf, len))
259 return -EFAULT;
260 goto out;
261 }
262 len = *lenp;
263 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
264 return -EFAULT;
1760537b 265 get_online_cpus();
1da177e4
LT
266 spin_lock(&appldata_timer_lock);
267 if (buf[0] == '1')
268 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
269 else if (buf[0] == '0')
270 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
271 spin_unlock(&appldata_timer_lock);
1760537b 272 put_online_cpus();
1da177e4
LT
273out:
274 *lenp = len;
275 *ppos += len;
276 return 0;
277}
278
279/*
280 * appldata_interval_handler()
281 *
282 * Set (CPU) timer interval for collection of data (in milliseconds), show
283 * current timer interval.
284 */
285static int
286appldata_interval_handler(ctl_table *ctl, int write, struct file *filp,
287 void __user *buffer, size_t *lenp, loff_t *ppos)
288{
289 int len, interval;
290 char buf[16];
291
292 if (!*lenp || *ppos) {
293 *lenp = 0;
294 return 0;
295 }
296 if (!write) {
297 len = sprintf(buf, "%i\n", appldata_interval);
298 if (len > *lenp)
299 len = *lenp;
300 if (copy_to_user(buffer, buf, len))
301 return -EFAULT;
302 goto out;
303 }
304 len = *lenp;
305 if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len)) {
306 return -EFAULT;
307 }
95425f19 308 interval = 0;
1da177e4 309 sscanf(buf, "%i", &interval);
d3ae942d 310 if (interval <= 0)
1da177e4 311 return -EINVAL;
1da177e4 312
1760537b 313 get_online_cpus();
1da177e4
LT
314 spin_lock(&appldata_timer_lock);
315 appldata_interval = interval;
316 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
317 spin_unlock(&appldata_timer_lock);
1760537b 318 put_online_cpus();
1da177e4
LT
319out:
320 *lenp = len;
321 *ppos += len;
322 return 0;
323}
324
325/*
326 * appldata_generic_handler()
327 *
328 * Generic start/stop monitoring and DIAG, show status of
329 * monitoring (0 = not in process, 1 = in process)
330 */
331static int
332appldata_generic_handler(ctl_table *ctl, int write, struct file *filp,
333 void __user *buffer, size_t *lenp, loff_t *ppos)
334{
335 struct appldata_ops *ops = NULL, *tmp_ops;
336 int rc, len, found;
337 char buf[2];
338 struct list_head *lh;
339
340 found = 0;
b1ad171e 341 mutex_lock(&appldata_ops_mutex);
1da177e4
LT
342 list_for_each(lh, &appldata_ops_list) {
343 tmp_ops = list_entry(lh, struct appldata_ops, list);
344 if (&tmp_ops->ctl_table[2] == ctl) {
345 found = 1;
346 }
347 }
348 if (!found) {
b1ad171e 349 mutex_unlock(&appldata_ops_mutex);
1da177e4
LT
350 return -ENODEV;
351 }
352 ops = ctl->data;
353 if (!try_module_get(ops->owner)) { // protect this function
b1ad171e 354 mutex_unlock(&appldata_ops_mutex);
1da177e4
LT
355 return -ENODEV;
356 }
b1ad171e 357 mutex_unlock(&appldata_ops_mutex);
1da177e4
LT
358
359 if (!*lenp || *ppos) {
360 *lenp = 0;
361 module_put(ops->owner);
362 return 0;
363 }
364 if (!write) {
365 len = sprintf(buf, ops->active ? "1\n" : "0\n");
366 if (len > *lenp)
367 len = *lenp;
368 if (copy_to_user(buffer, buf, len)) {
369 module_put(ops->owner);
370 return -EFAULT;
371 }
372 goto out;
373 }
374 len = *lenp;
375 if (copy_from_user(buf, buffer,
376 len > sizeof(buf) ? sizeof(buf) : len)) {
377 module_put(ops->owner);
378 return -EFAULT;
379 }
380
b1ad171e 381 mutex_lock(&appldata_ops_mutex);
1da177e4 382 if ((buf[0] == '1') && (ops->active == 0)) {
f26d583e
GS
383 // protect work queue callback
384 if (!try_module_get(ops->owner)) {
b1ad171e 385 mutex_unlock(&appldata_ops_mutex);
1da177e4
LT
386 module_put(ops->owner);
387 return -ENODEV;
388 }
1da177e4
LT
389 ops->callback(ops->data); // init record
390 rc = appldata_diag(ops->record_nr,
391 APPLDATA_START_INTERVAL_REC,
5b5dd21a
GS
392 (unsigned long) ops->data, ops->size,
393 ops->mod_lvl);
1da177e4 394 if (rc != 0) {
e7534b0e
GS
395 pr_err("Starting the data collection for %s "
396 "failed with rc=%d\n", ops->name, rc);
1da177e4 397 module_put(ops->owner);
d3ae942d 398 } else
5b5dd21a 399 ops->active = 1;
1da177e4
LT
400 } else if ((buf[0] == '0') && (ops->active == 1)) {
401 ops->active = 0;
402 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
5b5dd21a
GS
403 (unsigned long) ops->data, ops->size,
404 ops->mod_lvl);
d3ae942d 405 if (rc != 0)
e7534b0e
GS
406 pr_err("Stopping the data collection for %s "
407 "failed with rc=%d\n", ops->name, rc);
1da177e4
LT
408 module_put(ops->owner);
409 }
b1ad171e 410 mutex_unlock(&appldata_ops_mutex);
1da177e4
LT
411out:
412 *lenp = len;
413 *ppos += len;
414 module_put(ops->owner);
415 return 0;
416}
417
418/*************************** /proc stuff <END> *******************************/
419
420
421/************************* module-ops management *****************************/
422/*
423 * appldata_register_ops()
424 *
425 * update ops list, register /proc/sys entries
426 */
427int appldata_register_ops(struct appldata_ops *ops)
428{
13f8b7c5 429 if (ops->size > APPLDATA_MAX_REC_SIZE)
37e3a6ac 430 return -EINVAL;
1da177e4 431
37e3a6ac
HC
432 ops->ctl_table = kzalloc(4 * sizeof(struct ctl_table), GFP_KERNEL);
433 if (!ops->ctl_table)
1da177e4 434 return -ENOMEM;
1da177e4 435
b1ad171e 436 mutex_lock(&appldata_ops_mutex);
1da177e4 437 list_add(&ops->list, &appldata_ops_list);
b1ad171e 438 mutex_unlock(&appldata_ops_mutex);
1da177e4 439
1da177e4
LT
440 ops->ctl_table[0].procname = appldata_proc_name;
441 ops->ctl_table[0].maxlen = 0;
442 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
443 ops->ctl_table[0].child = &ops->ctl_table[2];
444
1da177e4
LT
445 ops->ctl_table[2].procname = ops->name;
446 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
447 ops->ctl_table[2].proc_handler = appldata_generic_handler;
448 ops->ctl_table[2].data = ops;
449
0b4d4147 450 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
37e3a6ac
HC
451 if (!ops->sysctl_header)
452 goto out;
1da177e4 453 return 0;
37e3a6ac 454out:
b1ad171e 455 mutex_lock(&appldata_ops_mutex);
37e3a6ac 456 list_del(&ops->list);
b1ad171e 457 mutex_unlock(&appldata_ops_mutex);
37e3a6ac
HC
458 kfree(ops->ctl_table);
459 return -ENOMEM;
1da177e4
LT
460}
461
462/*
463 * appldata_unregister_ops()
464 *
465 * update ops list, unregister /proc entries, stop DIAG if necessary
466 */
467void appldata_unregister_ops(struct appldata_ops *ops)
468{
b1ad171e 469 mutex_lock(&appldata_ops_mutex);
1da177e4 470 list_del(&ops->list);
b1ad171e 471 mutex_unlock(&appldata_ops_mutex);
330d57fb 472 unregister_sysctl_table(ops->sysctl_header);
37e3a6ac 473 kfree(ops->ctl_table);
1da177e4
LT
474}
475/********************** module-ops management <END> **************************/
476
477
478/******************************* init / exit *********************************/
479
84b36a8e 480static void __cpuinit appldata_online_cpu(int cpu)
1da177e4
LT
481{
482 init_virt_timer(&per_cpu(appldata_timer, cpu));
483 per_cpu(appldata_timer, cpu).function = appldata_timer_function;
484 per_cpu(appldata_timer, cpu).data = (unsigned long)
f26d583e 485 &appldata_work;
1da177e4
LT
486 atomic_inc(&appldata_expire_count);
487 spin_lock(&appldata_timer_lock);
488 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
489 spin_unlock(&appldata_timer_lock);
490}
491
076fc808 492static void __cpuinit appldata_offline_cpu(int cpu)
1da177e4
LT
493{
494 del_virt_timer(&per_cpu(appldata_timer, cpu));
495 if (atomic_dec_and_test(&appldata_expire_count)) {
496 atomic_set(&appldata_expire_count, num_online_cpus());
f26d583e 497 queue_work(appldata_wq, &appldata_work);
1da177e4
LT
498 }
499 spin_lock(&appldata_timer_lock);
500 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
501 spin_unlock(&appldata_timer_lock);
502}
503
11b8bf01
SS
504static int __cpuinit appldata_cpu_notify(struct notifier_block *self,
505 unsigned long action,
506 void *hcpu)
1da177e4
LT
507{
508 switch (action) {
509 case CPU_ONLINE:
8bb78442 510 case CPU_ONLINE_FROZEN:
1da177e4
LT
511 appldata_online_cpu((long) hcpu);
512 break;
1da177e4 513 case CPU_DEAD:
8bb78442 514 case CPU_DEAD_FROZEN:
1da177e4
LT
515 appldata_offline_cpu((long) hcpu);
516 break;
1da177e4
LT
517 default:
518 break;
519 }
520 return NOTIFY_OK;
521}
522
84b36a8e 523static struct notifier_block __cpuinitdata appldata_nb = {
1da177e4
LT
524 .notifier_call = appldata_cpu_notify,
525};
526
527/*
528 * appldata_init()
529 *
f26d583e 530 * init timer, register /proc entries
1da177e4
LT
531 */
532static int __init appldata_init(void)
533{
534 int i;
535
f26d583e 536 appldata_wq = create_singlethread_workqueue("appldata");
d3ae942d 537 if (!appldata_wq)
f26d583e 538 return -ENOMEM;
f26d583e 539
1760537b 540 get_online_cpus();
1da177e4
LT
541 for_each_online_cpu(i)
542 appldata_online_cpu(i);
1760537b 543 put_online_cpus();
1da177e4
LT
544
545 /* Register cpu hotplug notifier */
be6b5a35 546 register_hotcpu_notifier(&appldata_nb);
1da177e4 547
0b4d4147 548 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
1da177e4
LT
549 return 0;
550}
551
076fc808 552__initcall(appldata_init);
1da177e4 553
1da177e4
LT
554/**************************** init / exit <END> ******************************/
555
1da177e4
LT
556EXPORT_SYMBOL_GPL(appldata_register_ops);
557EXPORT_SYMBOL_GPL(appldata_unregister_ops);
5b5dd21a 558EXPORT_SYMBOL_GPL(appldata_diag);
1da177e4 559
0c3252d5 560#ifdef CONFIG_SWAP
1da177e4 561EXPORT_SYMBOL_GPL(si_swapinfo);
0c3252d5 562#endif
1da177e4 563EXPORT_SYMBOL_GPL(nr_threads);
1da177e4
LT
564EXPORT_SYMBOL_GPL(nr_running);
565EXPORT_SYMBOL_GPL(nr_iowait);