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 __user
*buffer
, size_t *lenp
, loff_t
*ppos
);
55 static int appldata_interval_handler(struct ctl_table
*ctl
, int write
,
57 size_t *lenp
, loff_t
*ppos
);
59 static struct ctl_table_header
*appldata_sysctl_header
;
60 static struct ctl_table appldata_table
[] = {
63 .mode
= S_IRUGO
| S_IWUSR
,
64 .proc_handler
= appldata_timer_handler
,
67 .procname
= "interval",
68 .mode
= S_IRUGO
| S_IWUSR
,
69 .proc_handler
= appldata_interval_handler
,
74 static struct ctl_table appldata_dir_table
[] = {
76 .procname
= appldata_proc_name
,
78 .mode
= S_IRUGO
| S_IXUGO
,
79 .child
= appldata_table
,
87 static struct vtimer_list appldata_timer
;
89 static DEFINE_SPINLOCK(appldata_timer_lock
);
90 static int appldata_interval
= APPLDATA_CPU_INTERVAL
;
91 static int appldata_timer_active
;
92 static int appldata_timer_suspended
= 0;
97 static struct workqueue_struct
*appldata_wq
;
98 static void appldata_work_fn(struct work_struct
*work
);
99 static DECLARE_WORK(appldata_work
, appldata_work_fn
);
105 static DEFINE_MUTEX(appldata_ops_mutex
);
106 static LIST_HEAD(appldata_ops_list
);
109 /*************************** timer, work, DIAG *******************************/
111 * appldata_timer_function()
113 * schedule work and reschedule timer
115 static void appldata_timer_function(unsigned long data
)
117 queue_work(appldata_wq
, (struct work_struct
*) data
);
123 * call data gathering function for each (active) module
125 static void appldata_work_fn(struct work_struct
*work
)
127 struct list_head
*lh
;
128 struct appldata_ops
*ops
;
130 mutex_lock(&appldata_ops_mutex
);
131 list_for_each(lh
, &appldata_ops_list
) {
132 ops
= list_entry(lh
, struct appldata_ops
, list
);
133 if (ops
->active
== 1) {
134 ops
->callback(ops
->data
);
137 mutex_unlock(&appldata_ops_mutex
);
143 * prepare parameter list, issue DIAG 0xDC
145 int appldata_diag(char record_nr
, u16 function
, unsigned long buffer
,
146 u16 length
, char *mod_lvl
)
148 struct appldata_product_id id
= {
149 .prod_nr
= {0xD3, 0xC9, 0xD5, 0xE4,
150 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
151 .prod_fn
= 0xD5D3, /* "NL" */
152 .version_nr
= 0xF2F6, /* "26" */
153 .release_nr
= 0xF0F1, /* "01" */
156 id
.record_nr
= record_nr
;
157 id
.mod_lvl
= (mod_lvl
[0]) << 8 | mod_lvl
[1];
158 return appldata_asm(&id
, function
, (void *) buffer
, length
);
160 /************************ timer, work, DIAG <END> ****************************/
163 /****************************** /proc stuff **********************************/
165 #define APPLDATA_ADD_TIMER 0
166 #define APPLDATA_DEL_TIMER 1
167 #define APPLDATA_MOD_TIMER 2
170 * __appldata_vtimer_setup()
172 * Add, delete or modify virtual timers on all online cpus.
173 * The caller needs to get the appldata_timer_lock spinlock.
175 static void __appldata_vtimer_setup(int cmd
)
177 u64 timer_interval
= (u64
) appldata_interval
* 1000 * TOD_MICRO
;
180 case APPLDATA_ADD_TIMER
:
181 if (appldata_timer_active
)
183 appldata_timer
.expires
= timer_interval
;
184 add_virt_timer_periodic(&appldata_timer
);
185 appldata_timer_active
= 1;
187 case APPLDATA_DEL_TIMER
:
188 del_virt_timer(&appldata_timer
);
189 if (!appldata_timer_active
)
191 appldata_timer_active
= 0;
193 case APPLDATA_MOD_TIMER
:
194 if (!appldata_timer_active
)
196 mod_virt_timer_periodic(&appldata_timer
, timer_interval
);
201 * appldata_timer_handler()
203 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
206 appldata_timer_handler(struct ctl_table
*ctl
, int write
,
207 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
212 if (!*lenp
|| *ppos
) {
217 strncpy(buf
, appldata_timer_active
? "1\n" : "0\n",
219 len
= strnlen(buf
, ARRAY_SIZE(buf
));
222 if (copy_to_user(buffer
, buf
, len
))
227 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
))
229 spin_lock(&appldata_timer_lock
);
231 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
232 else if (buf
[0] == '0')
233 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
234 spin_unlock(&appldata_timer_lock
);
242 * appldata_interval_handler()
244 * Set (CPU) timer interval for collection of data (in milliseconds), show
245 * current timer interval.
248 appldata_interval_handler(struct ctl_table
*ctl
, int write
,
249 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
255 if (!*lenp
|| *ppos
) {
260 len
= sprintf(buf
, "%i\n", appldata_interval
);
263 if (copy_to_user(buffer
, buf
, len
))
268 if (copy_from_user(buf
, buffer
, len
> sizeof(buf
) ? sizeof(buf
) : len
))
271 sscanf(buf
, "%i", &interval
);
275 spin_lock(&appldata_timer_lock
);
276 appldata_interval
= interval
;
277 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
278 spin_unlock(&appldata_timer_lock
);
286 * appldata_generic_handler()
288 * Generic start/stop monitoring and DIAG, show status of
289 * monitoring (0 = not in process, 1 = in process)
292 appldata_generic_handler(struct ctl_table
*ctl
, int write
,
293 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
295 struct appldata_ops
*ops
= NULL
, *tmp_ops
;
299 struct list_head
*lh
;
302 mutex_lock(&appldata_ops_mutex
);
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
) {
310 mutex_unlock(&appldata_ops_mutex
);
314 if (!try_module_get(ops
->owner
)) { // protect this function
315 mutex_unlock(&appldata_ops_mutex
);
318 mutex_unlock(&appldata_ops_mutex
);
320 if (!*lenp
|| *ppos
) {
322 module_put(ops
->owner
);
326 strncpy(buf
, ops
->active
? "1\n" : "0\n", ARRAY_SIZE(buf
));
327 len
= strnlen(buf
, ARRAY_SIZE(buf
));
330 if (copy_to_user(buffer
, buf
, len
)) {
331 module_put(ops
->owner
);
337 if (copy_from_user(buf
, buffer
,
338 len
> sizeof(buf
) ? sizeof(buf
) : len
)) {
339 module_put(ops
->owner
);
343 mutex_lock(&appldata_ops_mutex
);
344 if ((buf
[0] == '1') && (ops
->active
== 0)) {
345 // protect work queue callback
346 if (!try_module_get(ops
->owner
)) {
347 mutex_unlock(&appldata_ops_mutex
);
348 module_put(ops
->owner
);
351 ops
->callback(ops
->data
); // init record
352 rc
= appldata_diag(ops
->record_nr
,
353 APPLDATA_START_INTERVAL_REC
,
354 (unsigned long) ops
->data
, ops
->size
,
357 pr_err("Starting the data collection for %s "
358 "failed with rc=%d\n", ops
->name
, rc
);
359 module_put(ops
->owner
);
362 } else if ((buf
[0] == '0') && (ops
->active
== 1)) {
364 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
365 (unsigned long) ops
->data
, ops
->size
,
368 pr_err("Stopping the data collection for %s "
369 "failed with rc=%d\n", ops
->name
, rc
);
370 module_put(ops
->owner
);
372 mutex_unlock(&appldata_ops_mutex
);
376 module_put(ops
->owner
);
380 /*************************** /proc stuff <END> *******************************/
383 /************************* module-ops management *****************************/
385 * appldata_register_ops()
387 * update ops list, register /proc/sys entries
389 int appldata_register_ops(struct appldata_ops
*ops
)
391 if (ops
->size
> APPLDATA_MAX_REC_SIZE
)
394 ops
->ctl_table
= kzalloc(4 * sizeof(struct ctl_table
), GFP_KERNEL
);
398 mutex_lock(&appldata_ops_mutex
);
399 list_add(&ops
->list
, &appldata_ops_list
);
400 mutex_unlock(&appldata_ops_mutex
);
402 ops
->ctl_table
[0].procname
= appldata_proc_name
;
403 ops
->ctl_table
[0].maxlen
= 0;
404 ops
->ctl_table
[0].mode
= S_IRUGO
| S_IXUGO
;
405 ops
->ctl_table
[0].child
= &ops
->ctl_table
[2];
407 ops
->ctl_table
[2].procname
= ops
->name
;
408 ops
->ctl_table
[2].mode
= S_IRUGO
| S_IWUSR
;
409 ops
->ctl_table
[2].proc_handler
= appldata_generic_handler
;
410 ops
->ctl_table
[2].data
= ops
;
412 ops
->sysctl_header
= register_sysctl_table(ops
->ctl_table
);
413 if (!ops
->sysctl_header
)
417 mutex_lock(&appldata_ops_mutex
);
418 list_del(&ops
->list
);
419 mutex_unlock(&appldata_ops_mutex
);
420 kfree(ops
->ctl_table
);
425 * appldata_unregister_ops()
427 * update ops list, unregister /proc entries, stop DIAG if necessary
429 void appldata_unregister_ops(struct appldata_ops
*ops
)
431 mutex_lock(&appldata_ops_mutex
);
432 list_del(&ops
->list
);
433 mutex_unlock(&appldata_ops_mutex
);
434 unregister_sysctl_table(ops
->sysctl_header
);
435 kfree(ops
->ctl_table
);
437 /********************** module-ops management <END> **************************/
440 /**************************** suspend / resume *******************************/
441 static int appldata_freeze(struct device
*dev
)
443 struct appldata_ops
*ops
;
445 struct list_head
*lh
;
447 spin_lock(&appldata_timer_lock
);
448 if (appldata_timer_active
) {
449 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
450 appldata_timer_suspended
= 1;
452 spin_unlock(&appldata_timer_lock
);
454 mutex_lock(&appldata_ops_mutex
);
455 list_for_each(lh
, &appldata_ops_list
) {
456 ops
= list_entry(lh
, struct appldata_ops
, list
);
457 if (ops
->active
== 1) {
458 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
459 (unsigned long) ops
->data
, ops
->size
,
462 pr_err("Stopping the data collection for %s "
463 "failed with rc=%d\n", ops
->name
, rc
);
466 mutex_unlock(&appldata_ops_mutex
);
470 static int appldata_restore(struct device
*dev
)
472 struct appldata_ops
*ops
;
474 struct list_head
*lh
;
476 spin_lock(&appldata_timer_lock
);
477 if (appldata_timer_suspended
) {
478 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
479 appldata_timer_suspended
= 0;
481 spin_unlock(&appldata_timer_lock
);
483 mutex_lock(&appldata_ops_mutex
);
484 list_for_each(lh
, &appldata_ops_list
) {
485 ops
= list_entry(lh
, struct appldata_ops
, list
);
486 if (ops
->active
== 1) {
487 ops
->callback(ops
->data
); // init record
488 rc
= appldata_diag(ops
->record_nr
,
489 APPLDATA_START_INTERVAL_REC
,
490 (unsigned long) ops
->data
, ops
->size
,
493 pr_err("Starting the data collection for %s "
494 "failed with rc=%d\n", ops
->name
, rc
);
498 mutex_unlock(&appldata_ops_mutex
);
502 static int appldata_thaw(struct device
*dev
)
504 return appldata_restore(dev
);
507 static const struct dev_pm_ops appldata_pm_ops
= {
508 .freeze
= appldata_freeze
,
509 .thaw
= appldata_thaw
,
510 .restore
= appldata_restore
,
513 static struct platform_driver appldata_pdrv
= {
516 .pm
= &appldata_pm_ops
,
519 /************************* suspend / resume <END> ****************************/
522 /******************************* init / exit *********************************/
527 * init timer, register /proc entries
529 static int __init
appldata_init(void)
533 init_virt_timer(&appldata_timer
);
534 appldata_timer
.function
= appldata_timer_function
;
535 appldata_timer
.data
= (unsigned long) &appldata_work
;
537 rc
= platform_driver_register(&appldata_pdrv
);
541 appldata_pdev
= platform_device_register_simple("appldata", -1, NULL
,
543 if (IS_ERR(appldata_pdev
)) {
544 rc
= PTR_ERR(appldata_pdev
);
547 appldata_wq
= alloc_ordered_workqueue("appldata", 0);
553 appldata_sysctl_header
= register_sysctl_table(appldata_dir_table
);
557 platform_device_unregister(appldata_pdev
);
559 platform_driver_unregister(&appldata_pdrv
);
563 __initcall(appldata_init
);
565 /**************************** init / exit <END> ******************************/
567 EXPORT_SYMBOL_GPL(appldata_register_ops
);
568 EXPORT_SYMBOL_GPL(appldata_unregister_ops
);
569 EXPORT_SYMBOL_GPL(appldata_diag
);
572 EXPORT_SYMBOL_GPL(si_swapinfo
);
574 EXPORT_SYMBOL_GPL(nr_threads
);
575 EXPORT_SYMBOL_GPL(nr_running
);
576 EXPORT_SYMBOL_GPL(nr_iowait
);