1 // SPDX-License-Identifier: GPL-2.0
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.
7 * Copyright IBM Corp. 2003, 2009
9 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
12 #define KMSG_COMPONENT "appldata"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/module.h>
16 #include <linux/sched/stat.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/proc_fs.h>
23 #include <linux/swap.h>
24 #include <linux/pagemap.h>
25 #include <linux/sysctl.h>
26 #include <linux/notifier.h>
27 #include <linux/cpu.h>
28 #include <linux/workqueue.h>
29 #include <linux/suspend.h>
30 #include <linux/platform_device.h>
31 #include <asm/appldata.h>
32 #include <asm/vtimer.h>
33 #include <linux/uaccess.h>
40 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
44 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
47 static struct platform_device *appldata_pdev;
50 * /proc entries (sysctl)
52 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
53 static int appldata_timer_handler(struct ctl_table *ctl, int write,
54 void *buffer, size_t *lenp, loff_t *ppos);
55 static int appldata_interval_handler(struct ctl_table *ctl, int write,
56 void *buffer, size_t *lenp, loff_t *ppos);
58 static struct ctl_table_header *appldata_sysctl_header;
59 static struct ctl_table appldata_table[] = {
62 .mode = S_IRUGO | S_IWUSR,
63 .proc_handler = appldata_timer_handler,
66 .procname = "interval",
67 .mode = S_IRUGO | S_IWUSR,
68 .proc_handler = appldata_interval_handler,
73 static struct ctl_table appldata_dir_table[] = {
75 .procname = appldata_proc_name,
77 .mode = S_IRUGO | S_IXUGO,
78 .child = appldata_table,
86 static struct vtimer_list appldata_timer;
88 static DEFINE_SPINLOCK(appldata_timer_lock);
89 static int appldata_interval = APPLDATA_CPU_INTERVAL;
90 static int appldata_timer_active;
91 static int appldata_timer_suspended = 0;
96 static struct workqueue_struct *appldata_wq;
97 static void appldata_work_fn(struct work_struct *work);
98 static DECLARE_WORK(appldata_work, appldata_work_fn);
104 static DEFINE_MUTEX(appldata_ops_mutex);
105 static LIST_HEAD(appldata_ops_list);
108 /*************************** timer, work, DIAG *******************************/
110 * appldata_timer_function()
112 * schedule work and reschedule timer
114 static void appldata_timer_function(unsigned long data)
116 queue_work(appldata_wq, (struct work_struct *) data);
122 * call data gathering function for each (active) module
124 static void appldata_work_fn(struct work_struct *work)
126 struct list_head *lh;
127 struct appldata_ops *ops;
129 mutex_lock(&appldata_ops_mutex);
130 list_for_each(lh, &appldata_ops_list) {
131 ops = list_entry(lh, struct appldata_ops, list);
132 if (ops->active == 1) {
133 ops->callback(ops->data);
136 mutex_unlock(&appldata_ops_mutex);
139 static struct appldata_product_id appldata_id = {
140 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
141 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
142 .prod_fn = 0xD5D3, /* "NL" */
143 .version_nr = 0xF2F6, /* "26" */
144 .release_nr = 0xF0F1, /* "01" */
150 * prepare parameter list, issue DIAG 0xDC
152 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
153 u16 length, char *mod_lvl)
155 struct appldata_parameter_list *parm_list;
156 struct appldata_product_id *id;
159 parm_list = kmalloc(sizeof(*parm_list), GFP_KERNEL);
160 id = kmemdup(&appldata_id, sizeof(appldata_id), GFP_KERNEL);
162 if (parm_list && id) {
163 id->record_nr = record_nr;
164 id->mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
165 rc = appldata_asm(parm_list, id, function,
166 (void *) buffer, length);
172 /************************ timer, work, DIAG <END> ****************************/
175 /****************************** /proc stuff **********************************/
177 #define APPLDATA_ADD_TIMER 0
178 #define APPLDATA_DEL_TIMER 1
179 #define APPLDATA_MOD_TIMER 2
182 * __appldata_vtimer_setup()
184 * Add, delete or modify virtual timers on all online cpus.
185 * The caller needs to get the appldata_timer_lock spinlock.
187 static void __appldata_vtimer_setup(int cmd)
189 u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
192 case APPLDATA_ADD_TIMER:
193 if (appldata_timer_active)
195 appldata_timer.expires = timer_interval;
196 add_virt_timer_periodic(&appldata_timer);
197 appldata_timer_active = 1;
199 case APPLDATA_DEL_TIMER:
200 del_virt_timer(&appldata_timer);
201 if (!appldata_timer_active)
203 appldata_timer_active = 0;
205 case APPLDATA_MOD_TIMER:
206 if (!appldata_timer_active)
208 mod_virt_timer_periodic(&appldata_timer, timer_interval);
213 * appldata_timer_handler()
215 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
218 appldata_timer_handler(struct ctl_table *ctl, int write,
219 void *buffer, size_t *lenp, loff_t *ppos)
221 int timer_active = appldata_timer_active;
223 struct ctl_table ctl_entry = {
224 .procname = ctl->procname,
225 .data = &timer_active,
226 .maxlen = sizeof(int),
227 .extra1 = SYSCTL_ZERO,
228 .extra2 = SYSCTL_ONE,
231 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
232 if (rc < 0 || !write)
235 spin_lock(&appldata_timer_lock);
237 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
239 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
240 spin_unlock(&appldata_timer_lock);
245 * appldata_interval_handler()
247 * Set (CPU) timer interval for collection of data (in milliseconds), show
248 * current timer interval.
251 appldata_interval_handler(struct ctl_table *ctl, int write,
252 void *buffer, size_t *lenp, loff_t *ppos)
254 int interval = appldata_interval;
256 struct ctl_table ctl_entry = {
257 .procname = ctl->procname,
259 .maxlen = sizeof(int),
260 .extra1 = SYSCTL_ONE,
263 rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
264 if (rc < 0 || !write)
267 spin_lock(&appldata_timer_lock);
268 appldata_interval = interval;
269 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
270 spin_unlock(&appldata_timer_lock);
275 * appldata_generic_handler()
277 * Generic start/stop monitoring and DIAG, show status of
278 * monitoring (0 = not in process, 1 = in process)
281 appldata_generic_handler(struct ctl_table *ctl, int write,
282 void *buffer, size_t *lenp, loff_t *ppos)
284 struct appldata_ops *ops = NULL, *tmp_ops;
285 struct list_head *lh;
288 struct ctl_table ctl_entry = {
290 .maxlen = sizeof(int),
291 .extra1 = SYSCTL_ZERO,
292 .extra2 = SYSCTL_ONE,
296 mutex_lock(&appldata_ops_mutex);
297 list_for_each(lh, &appldata_ops_list) {
298 tmp_ops = list_entry(lh, struct appldata_ops, list);
299 if (&tmp_ops->ctl_table[2] == ctl) {
304 mutex_unlock(&appldata_ops_mutex);
308 if (!try_module_get(ops->owner)) { // protect this function
309 mutex_unlock(&appldata_ops_mutex);
312 mutex_unlock(&appldata_ops_mutex);
314 active = ops->active;
315 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
316 if (rc < 0 || !write) {
317 module_put(ops->owner);
321 mutex_lock(&appldata_ops_mutex);
322 if (active && (ops->active == 0)) {
323 // protect work queue callback
324 if (!try_module_get(ops->owner)) {
325 mutex_unlock(&appldata_ops_mutex);
326 module_put(ops->owner);
329 ops->callback(ops->data); // init record
330 rc = appldata_diag(ops->record_nr,
331 APPLDATA_START_INTERVAL_REC,
332 (unsigned long) ops->data, ops->size,
335 pr_err("Starting the data collection for %s "
336 "failed with rc=%d\n", ops->name, rc);
337 module_put(ops->owner);
340 } else if (!active && (ops->active == 1)) {
342 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
343 (unsigned long) ops->data, ops->size,
346 pr_err("Stopping the data collection for %s "
347 "failed with rc=%d\n", ops->name, rc);
348 module_put(ops->owner);
350 mutex_unlock(&appldata_ops_mutex);
351 module_put(ops->owner);
355 /*************************** /proc stuff <END> *******************************/
358 /************************* module-ops management *****************************/
360 * appldata_register_ops()
362 * update ops list, register /proc/sys entries
364 int appldata_register_ops(struct appldata_ops *ops)
366 if (ops->size > APPLDATA_MAX_REC_SIZE)
369 ops->ctl_table = kcalloc(4, sizeof(struct ctl_table), GFP_KERNEL);
373 mutex_lock(&appldata_ops_mutex);
374 list_add(&ops->list, &appldata_ops_list);
375 mutex_unlock(&appldata_ops_mutex);
377 ops->ctl_table[0].procname = appldata_proc_name;
378 ops->ctl_table[0].maxlen = 0;
379 ops->ctl_table[0].mode = S_IRUGO | S_IXUGO;
380 ops->ctl_table[0].child = &ops->ctl_table[2];
382 ops->ctl_table[2].procname = ops->name;
383 ops->ctl_table[2].mode = S_IRUGO | S_IWUSR;
384 ops->ctl_table[2].proc_handler = appldata_generic_handler;
385 ops->ctl_table[2].data = ops;
387 ops->sysctl_header = register_sysctl_table(ops->ctl_table);
388 if (!ops->sysctl_header)
392 mutex_lock(&appldata_ops_mutex);
393 list_del(&ops->list);
394 mutex_unlock(&appldata_ops_mutex);
395 kfree(ops->ctl_table);
400 * appldata_unregister_ops()
402 * update ops list, unregister /proc entries, stop DIAG if necessary
404 void appldata_unregister_ops(struct appldata_ops *ops)
406 mutex_lock(&appldata_ops_mutex);
407 list_del(&ops->list);
408 mutex_unlock(&appldata_ops_mutex);
409 unregister_sysctl_table(ops->sysctl_header);
410 kfree(ops->ctl_table);
412 /********************** module-ops management <END> **************************/
415 /**************************** suspend / resume *******************************/
416 static int appldata_freeze(struct device *dev)
418 struct appldata_ops *ops;
420 struct list_head *lh;
422 spin_lock(&appldata_timer_lock);
423 if (appldata_timer_active) {
424 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
425 appldata_timer_suspended = 1;
427 spin_unlock(&appldata_timer_lock);
429 mutex_lock(&appldata_ops_mutex);
430 list_for_each(lh, &appldata_ops_list) {
431 ops = list_entry(lh, struct appldata_ops, list);
432 if (ops->active == 1) {
433 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
434 (unsigned long) ops->data, ops->size,
437 pr_err("Stopping the data collection for %s "
438 "failed with rc=%d\n", ops->name, rc);
441 mutex_unlock(&appldata_ops_mutex);
445 static int appldata_restore(struct device *dev)
447 struct appldata_ops *ops;
449 struct list_head *lh;
451 spin_lock(&appldata_timer_lock);
452 if (appldata_timer_suspended) {
453 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
454 appldata_timer_suspended = 0;
456 spin_unlock(&appldata_timer_lock);
458 mutex_lock(&appldata_ops_mutex);
459 list_for_each(lh, &appldata_ops_list) {
460 ops = list_entry(lh, struct appldata_ops, list);
461 if (ops->active == 1) {
462 ops->callback(ops->data); // init record
463 rc = appldata_diag(ops->record_nr,
464 APPLDATA_START_INTERVAL_REC,
465 (unsigned long) ops->data, ops->size,
468 pr_err("Starting the data collection for %s "
469 "failed with rc=%d\n", ops->name, rc);
473 mutex_unlock(&appldata_ops_mutex);
477 static int appldata_thaw(struct device *dev)
479 return appldata_restore(dev);
482 static const struct dev_pm_ops appldata_pm_ops = {
483 .freeze = appldata_freeze,
484 .thaw = appldata_thaw,
485 .restore = appldata_restore,
488 static struct platform_driver appldata_pdrv = {
491 .pm = &appldata_pm_ops,
494 /************************* suspend / resume <END> ****************************/
497 /******************************* init / exit *********************************/
502 * init timer, register /proc entries
504 static int __init appldata_init(void)
508 init_virt_timer(&appldata_timer);
509 appldata_timer.function = appldata_timer_function;
510 appldata_timer.data = (unsigned long) &appldata_work;
512 rc = platform_driver_register(&appldata_pdrv);
516 appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
518 if (IS_ERR(appldata_pdev)) {
519 rc = PTR_ERR(appldata_pdev);
522 appldata_wq = alloc_ordered_workqueue("appldata", 0);
528 appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
532 platform_device_unregister(appldata_pdev);
534 platform_driver_unregister(&appldata_pdrv);
538 __initcall(appldata_init);
540 /**************************** init / exit <END> ******************************/
542 EXPORT_SYMBOL_GPL(appldata_register_ops);
543 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
544 EXPORT_SYMBOL_GPL(appldata_diag);
547 EXPORT_SYMBOL_GPL(si_swapinfo);
549 EXPORT_SYMBOL_GPL(nr_threads);
550 EXPORT_SYMBOL_GPL(nr_running);
551 EXPORT_SYMBOL_GPL(nr_iowait);