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