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
);
140 static 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" */
151 * prepare parameter list, issue DIAG 0xDC
153 int appldata_diag(char record_nr
, u16 function
, unsigned long buffer
,
154 u16 length
, char *mod_lvl
)
156 struct appldata_parameter_list
*parm_list
;
157 struct appldata_product_id
*id
;
160 parm_list
= kmalloc(sizeof(*parm_list
), GFP_KERNEL
);
161 id
= kmemdup(&appldata_id
, sizeof(appldata_id
), GFP_KERNEL
);
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
);
173 /************************ timer, work, DIAG <END> ****************************/
176 /****************************** /proc stuff **********************************/
178 #define APPLDATA_ADD_TIMER 0
179 #define APPLDATA_DEL_TIMER 1
180 #define APPLDATA_MOD_TIMER 2
183 * __appldata_vtimer_setup()
185 * Add, delete or modify virtual timers on all online cpus.
186 * The caller needs to get the appldata_timer_lock spinlock.
188 static void __appldata_vtimer_setup(int cmd
)
190 u64 timer_interval
= (u64
) appldata_interval
* 1000 * TOD_MICRO
;
193 case APPLDATA_ADD_TIMER
:
194 if (appldata_timer_active
)
196 appldata_timer
.expires
= timer_interval
;
197 add_virt_timer_periodic(&appldata_timer
);
198 appldata_timer_active
= 1;
200 case APPLDATA_DEL_TIMER
:
201 del_virt_timer(&appldata_timer
);
202 if (!appldata_timer_active
)
204 appldata_timer_active
= 0;
206 case APPLDATA_MOD_TIMER
:
207 if (!appldata_timer_active
)
209 mod_virt_timer_periodic(&appldata_timer
, timer_interval
);
214 * appldata_timer_handler()
216 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
219 appldata_timer_handler(struct ctl_table
*ctl
, int write
,
220 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
222 int timer_active
= appldata_timer_active
;
226 struct ctl_table ctl_entry
= {
227 .procname
= ctl
->procname
,
228 .data
= &timer_active
,
229 .maxlen
= sizeof(int),
234 rc
= proc_douintvec_minmax(&ctl_entry
, write
, buffer
, lenp
, ppos
);
235 if (rc
< 0 || !write
)
238 spin_lock(&appldata_timer_lock
);
240 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
242 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
243 spin_unlock(&appldata_timer_lock
);
248 * appldata_interval_handler()
250 * Set (CPU) timer interval for collection of data (in milliseconds), show
251 * current timer interval.
254 appldata_interval_handler(struct ctl_table
*ctl
, int write
,
255 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
257 int interval
= appldata_interval
;
260 struct ctl_table ctl_entry
= {
261 .procname
= ctl
->procname
,
263 .maxlen
= sizeof(int),
267 rc
= proc_dointvec_minmax(&ctl_entry
, write
, buffer
, lenp
, ppos
);
268 if (rc
< 0 || !write
)
271 spin_lock(&appldata_timer_lock
);
272 appldata_interval
= interval
;
273 __appldata_vtimer_setup(APPLDATA_MOD_TIMER
);
274 spin_unlock(&appldata_timer_lock
);
279 * appldata_generic_handler()
281 * Generic start/stop monitoring and DIAG, show status of
282 * monitoring (0 = not in process, 1 = in process)
285 appldata_generic_handler(struct ctl_table
*ctl
, int write
,
286 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
288 struct appldata_ops
*ops
= NULL
, *tmp_ops
;
289 struct list_head
*lh
;
294 struct ctl_table ctl_entry
= {
296 .maxlen
= sizeof(int),
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 active
= ops
->active
;
321 rc
= proc_douintvec_minmax(&ctl_entry
, write
, buffer
, lenp
, ppos
);
322 if (rc
< 0 || !write
) {
323 module_put(ops
->owner
);
327 mutex_lock(&appldata_ops_mutex
);
328 if (active
&& (ops
->active
== 0)) {
329 // protect work queue callback
330 if (!try_module_get(ops
->owner
)) {
331 mutex_unlock(&appldata_ops_mutex
);
332 module_put(ops
->owner
);
335 ops
->callback(ops
->data
); // init record
336 rc
= appldata_diag(ops
->record_nr
,
337 APPLDATA_START_INTERVAL_REC
,
338 (unsigned long) ops
->data
, ops
->size
,
341 pr_err("Starting the data collection for %s "
342 "failed with rc=%d\n", ops
->name
, rc
);
343 module_put(ops
->owner
);
346 } else if (!active
&& (ops
->active
== 1)) {
348 rc
= appldata_diag(ops
->record_nr
, APPLDATA_STOP_REC
,
349 (unsigned long) ops
->data
, ops
->size
,
352 pr_err("Stopping the data collection for %s "
353 "failed with rc=%d\n", ops
->name
, rc
);
354 module_put(ops
->owner
);
356 mutex_unlock(&appldata_ops_mutex
);
357 module_put(ops
->owner
);
361 /*************************** /proc stuff <END> *******************************/
364 /************************* module-ops management *****************************/
366 * appldata_register_ops()
368 * update ops list, register /proc/sys entries
370 int appldata_register_ops(struct appldata_ops
*ops
)
372 if (ops
->size
> APPLDATA_MAX_REC_SIZE
)
375 ops
->ctl_table
= kcalloc(4, sizeof(struct ctl_table
), GFP_KERNEL
);
379 mutex_lock(&appldata_ops_mutex
);
380 list_add(&ops
->list
, &appldata_ops_list
);
381 mutex_unlock(&appldata_ops_mutex
);
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];
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
;
393 ops
->sysctl_header
= register_sysctl_table(ops
->ctl_table
);
394 if (!ops
->sysctl_header
)
398 mutex_lock(&appldata_ops_mutex
);
399 list_del(&ops
->list
);
400 mutex_unlock(&appldata_ops_mutex
);
401 kfree(ops
->ctl_table
);
406 * appldata_unregister_ops()
408 * update ops list, unregister /proc entries, stop DIAG if necessary
410 void appldata_unregister_ops(struct appldata_ops
*ops
)
412 mutex_lock(&appldata_ops_mutex
);
413 list_del(&ops
->list
);
414 mutex_unlock(&appldata_ops_mutex
);
415 unregister_sysctl_table(ops
->sysctl_header
);
416 kfree(ops
->ctl_table
);
418 /********************** module-ops management <END> **************************/
421 /**************************** suspend / resume *******************************/
422 static int appldata_freeze(struct device
*dev
)
424 struct appldata_ops
*ops
;
426 struct list_head
*lh
;
428 spin_lock(&appldata_timer_lock
);
429 if (appldata_timer_active
) {
430 __appldata_vtimer_setup(APPLDATA_DEL_TIMER
);
431 appldata_timer_suspended
= 1;
433 spin_unlock(&appldata_timer_lock
);
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
,
443 pr_err("Stopping the data collection for %s "
444 "failed with rc=%d\n", ops
->name
, rc
);
447 mutex_unlock(&appldata_ops_mutex
);
451 static int appldata_restore(struct device
*dev
)
453 struct appldata_ops
*ops
;
455 struct list_head
*lh
;
457 spin_lock(&appldata_timer_lock
);
458 if (appldata_timer_suspended
) {
459 __appldata_vtimer_setup(APPLDATA_ADD_TIMER
);
460 appldata_timer_suspended
= 0;
462 spin_unlock(&appldata_timer_lock
);
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
,
474 pr_err("Starting the data collection for %s "
475 "failed with rc=%d\n", ops
->name
, rc
);
479 mutex_unlock(&appldata_ops_mutex
);
483 static int appldata_thaw(struct device
*dev
)
485 return appldata_restore(dev
);
488 static const struct dev_pm_ops appldata_pm_ops
= {
489 .freeze
= appldata_freeze
,
490 .thaw
= appldata_thaw
,
491 .restore
= appldata_restore
,
494 static struct platform_driver appldata_pdrv
= {
497 .pm
= &appldata_pm_ops
,
500 /************************* suspend / resume <END> ****************************/
503 /******************************* init / exit *********************************/
508 * init timer, register /proc entries
510 static int __init
appldata_init(void)
514 init_virt_timer(&appldata_timer
);
515 appldata_timer
.function
= appldata_timer_function
;
516 appldata_timer
.data
= (unsigned long) &appldata_work
;
518 rc
= platform_driver_register(&appldata_pdrv
);
522 appldata_pdev
= platform_device_register_simple("appldata", -1, NULL
,
524 if (IS_ERR(appldata_pdev
)) {
525 rc
= PTR_ERR(appldata_pdev
);
528 appldata_wq
= alloc_ordered_workqueue("appldata", 0);
534 appldata_sysctl_header
= register_sysctl_table(appldata_dir_table
);
538 platform_device_unregister(appldata_pdev
);
540 platform_driver_unregister(&appldata_pdrv
);
544 __initcall(appldata_init
);
546 /**************************** init / exit <END> ******************************/
548 EXPORT_SYMBOL_GPL(appldata_register_ops
);
549 EXPORT_SYMBOL_GPL(appldata_unregister_ops
);
550 EXPORT_SYMBOL_GPL(appldata_diag
);
553 EXPORT_SYMBOL_GPL(si_swapinfo
);
555 EXPORT_SYMBOL_GPL(nr_threads
);
556 EXPORT_SYMBOL_GPL(nr_running
);
557 EXPORT_SYMBOL_GPL(nr_iowait
);