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