Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / arch / s390 / appldata / appldata_base.c
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  */
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  */
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 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" */
146 };
147
148 /*
149  * appldata_diag()
150  *
151  * prepare parameter list, issue DIAG 0xDC
152  */
153 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
154                         u16 length, char *mod_lvl)
155 {
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;
172 }
173 /************************ timer, work, DIAG <END> ****************************/
174
175
176 /****************************** /proc stuff **********************************/
177
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  */
188 static void __appldata_vtimer_setup(int cmd)
189 {
190         u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
191
192         switch (cmd) {
193         case APPLDATA_ADD_TIMER:
194                 if (appldata_timer_active)
195                         break;
196                 appldata_timer.expires = timer_interval;
197                 add_virt_timer_periodic(&appldata_timer);
198                 appldata_timer_active = 1;
199                 break;
200         case APPLDATA_DEL_TIMER:
201                 del_virt_timer(&appldata_timer);
202                 if (!appldata_timer_active)
203                         break;
204                 appldata_timer_active = 0;
205                 break;
206         case APPLDATA_MOD_TIMER:
207                 if (!appldata_timer_active)
208                         break;
209                 mod_virt_timer_periodic(&appldata_timer, timer_interval);
210         }
211 }
212
213 /*
214  * appldata_timer_handler()
215  *
216  * Start/Stop timer, show status of timer (0 = not active, 1 = active)
217  */
218 static int
219 appldata_timer_handler(struct ctl_table *ctl, int write,
220                            void __user *buffer, size_t *lenp, loff_t *ppos)
221 {
222         int timer_active = appldata_timer_active;
223         int rc;
224         struct ctl_table ctl_entry = {
225                 .procname       = ctl->procname,
226                 .data           = &timer_active,
227                 .maxlen         = sizeof(int),
228                 .extra1         = SYSCTL_ZERO,
229                 .extra2         = SYSCTL_ONE,
230         };
231
232         rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
233         if (rc < 0 || !write)
234                 return rc;
235
236         spin_lock(&appldata_timer_lock);
237         if (timer_active)
238                 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
239         else
240                 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
241         spin_unlock(&appldata_timer_lock);
242         return 0;
243 }
244
245 /*
246  * appldata_interval_handler()
247  *
248  * Set (CPU) timer interval for collection of data (in milliseconds), show
249  * current timer interval.
250  */
251 static int
252 appldata_interval_handler(struct ctl_table *ctl, int write,
253                            void __user *buffer, size_t *lenp, loff_t *ppos)
254 {
255         int interval = appldata_interval;
256         int rc;
257         struct ctl_table ctl_entry = {
258                 .procname       = ctl->procname,
259                 .data           = &interval,
260                 .maxlen         = sizeof(int),
261                 .extra1         = SYSCTL_ONE,
262         };
263
264         rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
265         if (rc < 0 || !write)
266                 return rc;
267
268         spin_lock(&appldata_timer_lock);
269         appldata_interval = interval;
270         __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
271         spin_unlock(&appldata_timer_lock);
272         return 0;
273 }
274
275 /*
276  * appldata_generic_handler()
277  *
278  * Generic start/stop monitoring and DIAG, show status of
279  * monitoring (0 = not in process, 1 = in process)
280  */
281 static int
282 appldata_generic_handler(struct ctl_table *ctl, int write,
283                            void __user *buffer, size_t *lenp, loff_t *ppos)
284 {
285         struct appldata_ops *ops = NULL, *tmp_ops;
286         struct list_head *lh;
287         int rc, found;
288         int active;
289         struct ctl_table ctl_entry = {
290                 .data           = &active,
291                 .maxlen         = sizeof(int),
292                 .extra1         = SYSCTL_ZERO,
293                 .extra2         = SYSCTL_ONE,
294         };
295
296         found = 0;
297         mutex_lock(&appldata_ops_mutex);
298         list_for_each(lh, &appldata_ops_list) {
299                 tmp_ops = list_entry(lh, struct appldata_ops, list);
300                 if (&tmp_ops->ctl_table[2] == ctl) {
301                         found = 1;
302                 }
303         }
304         if (!found) {
305                 mutex_unlock(&appldata_ops_mutex);
306                 return -ENODEV;
307         }
308         ops = ctl->data;
309         if (!try_module_get(ops->owner)) {      // protect this function
310                 mutex_unlock(&appldata_ops_mutex);
311                 return -ENODEV;
312         }
313         mutex_unlock(&appldata_ops_mutex);
314
315         active = ops->active;
316         rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
317         if (rc < 0 || !write) {
318                 module_put(ops->owner);
319                 return rc;
320         }
321
322         mutex_lock(&appldata_ops_mutex);
323         if (active && (ops->active == 0)) {
324                 // protect work queue callback
325                 if (!try_module_get(ops->owner)) {
326                         mutex_unlock(&appldata_ops_mutex);
327                         module_put(ops->owner);
328                         return -ENODEV;
329                 }
330                 ops->callback(ops->data);       // init record
331                 rc = appldata_diag(ops->record_nr,
332                                         APPLDATA_START_INTERVAL_REC,
333                                         (unsigned long) ops->data, ops->size,
334                                         ops->mod_lvl);
335                 if (rc != 0) {
336                         pr_err("Starting the data collection for %s "
337                                "failed with rc=%d\n", ops->name, rc);
338                         module_put(ops->owner);
339                 } else
340                         ops->active = 1;
341         } else if (!active && (ops->active == 1)) {
342                 ops->active = 0;
343                 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
344                                 (unsigned long) ops->data, ops->size,
345                                 ops->mod_lvl);
346                 if (rc != 0)
347                         pr_err("Stopping the data collection for %s "
348                                "failed with rc=%d\n", ops->name, rc);
349                 module_put(ops->owner);
350         }
351         mutex_unlock(&appldata_ops_mutex);
352         module_put(ops->owner);
353         return 0;
354 }
355
356 /*************************** /proc stuff <END> *******************************/
357
358
359 /************************* module-ops management *****************************/
360 /*
361  * appldata_register_ops()
362  *
363  * update ops list, register /proc/sys entries
364  */
365 int appldata_register_ops(struct appldata_ops *ops)
366 {
367         if (ops->size > APPLDATA_MAX_REC_SIZE)
368                 return -EINVAL;
369
370         ops->ctl_table = kcalloc(4, sizeof(struct ctl_table), GFP_KERNEL);
371         if (!ops->ctl_table)
372                 return -ENOMEM;
373
374         mutex_lock(&appldata_ops_mutex);
375         list_add(&ops->list, &appldata_ops_list);
376         mutex_unlock(&appldata_ops_mutex);
377
378         ops->ctl_table[0].procname = appldata_proc_name;
379         ops->ctl_table[0].maxlen   = 0;
380         ops->ctl_table[0].mode     = S_IRUGO | S_IXUGO;
381         ops->ctl_table[0].child    = &ops->ctl_table[2];
382
383         ops->ctl_table[2].procname = ops->name;
384         ops->ctl_table[2].mode     = S_IRUGO | S_IWUSR;
385         ops->ctl_table[2].proc_handler = appldata_generic_handler;
386         ops->ctl_table[2].data = ops;
387
388         ops->sysctl_header = register_sysctl_table(ops->ctl_table);
389         if (!ops->sysctl_header)
390                 goto out;
391         return 0;
392 out:
393         mutex_lock(&appldata_ops_mutex);
394         list_del(&ops->list);
395         mutex_unlock(&appldata_ops_mutex);
396         kfree(ops->ctl_table);
397         return -ENOMEM;
398 }
399
400 /*
401  * appldata_unregister_ops()
402  *
403  * update ops list, unregister /proc entries, stop DIAG if necessary
404  */
405 void appldata_unregister_ops(struct appldata_ops *ops)
406 {
407         mutex_lock(&appldata_ops_mutex);
408         list_del(&ops->list);
409         mutex_unlock(&appldata_ops_mutex);
410         unregister_sysctl_table(ops->sysctl_header);
411         kfree(ops->ctl_table);
412 }
413 /********************** module-ops management <END> **************************/
414
415
416 /**************************** suspend / resume *******************************/
417 static int appldata_freeze(struct device *dev)
418 {
419         struct appldata_ops *ops;
420         int rc;
421         struct list_head *lh;
422
423         spin_lock(&appldata_timer_lock);
424         if (appldata_timer_active) {
425                 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
426                 appldata_timer_suspended = 1;
427         }
428         spin_unlock(&appldata_timer_lock);
429
430         mutex_lock(&appldata_ops_mutex);
431         list_for_each(lh, &appldata_ops_list) {
432                 ops = list_entry(lh, struct appldata_ops, list);
433                 if (ops->active == 1) {
434                         rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
435                                         (unsigned long) ops->data, ops->size,
436                                         ops->mod_lvl);
437                         if (rc != 0)
438                                 pr_err("Stopping the data collection for %s "
439                                        "failed with rc=%d\n", ops->name, rc);
440                 }
441         }
442         mutex_unlock(&appldata_ops_mutex);
443         return 0;
444 }
445
446 static int appldata_restore(struct device *dev)
447 {
448         struct appldata_ops *ops;
449         int rc;
450         struct list_head *lh;
451
452         spin_lock(&appldata_timer_lock);
453         if (appldata_timer_suspended) {
454                 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
455                 appldata_timer_suspended = 0;
456         }
457         spin_unlock(&appldata_timer_lock);
458
459         mutex_lock(&appldata_ops_mutex);
460         list_for_each(lh, &appldata_ops_list) {
461                 ops = list_entry(lh, struct appldata_ops, list);
462                 if (ops->active == 1) {
463                         ops->callback(ops->data);       // init record
464                         rc = appldata_diag(ops->record_nr,
465                                         APPLDATA_START_INTERVAL_REC,
466                                         (unsigned long) ops->data, ops->size,
467                                         ops->mod_lvl);
468                         if (rc != 0) {
469                                 pr_err("Starting the data collection for %s "
470                                        "failed with rc=%d\n", ops->name, rc);
471                         }
472                 }
473         }
474         mutex_unlock(&appldata_ops_mutex);
475         return 0;
476 }
477
478 static int appldata_thaw(struct device *dev)
479 {
480         return appldata_restore(dev);
481 }
482
483 static const struct dev_pm_ops appldata_pm_ops = {
484         .freeze         = appldata_freeze,
485         .thaw           = appldata_thaw,
486         .restore        = appldata_restore,
487 };
488
489 static struct platform_driver appldata_pdrv = {
490         .driver = {
491                 .name   = "appldata",
492                 .pm     = &appldata_pm_ops,
493         },
494 };
495 /************************* suspend / resume <END> ****************************/
496
497
498 /******************************* init / exit *********************************/
499
500 /*
501  * appldata_init()
502  *
503  * init timer, register /proc entries
504  */
505 static int __init appldata_init(void)
506 {
507         int rc;
508
509         init_virt_timer(&appldata_timer);
510         appldata_timer.function = appldata_timer_function;
511         appldata_timer.data = (unsigned long) &appldata_work;
512
513         rc = platform_driver_register(&appldata_pdrv);
514         if (rc)
515                 return rc;
516
517         appldata_pdev = platform_device_register_simple("appldata", -1, NULL,
518                                                         0);
519         if (IS_ERR(appldata_pdev)) {
520                 rc = PTR_ERR(appldata_pdev);
521                 goto out_driver;
522         }
523         appldata_wq = alloc_ordered_workqueue("appldata", 0);
524         if (!appldata_wq) {
525                 rc = -ENOMEM;
526                 goto out_device;
527         }
528
529         appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
530         return 0;
531
532 out_device:
533         platform_device_unregister(appldata_pdev);
534 out_driver:
535         platform_driver_unregister(&appldata_pdrv);
536         return rc;
537 }
538
539 __initcall(appldata_init);
540
541 /**************************** init / exit <END> ******************************/
542
543 EXPORT_SYMBOL_GPL(appldata_register_ops);
544 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
545 EXPORT_SYMBOL_GPL(appldata_diag);
546
547 #ifdef CONFIG_SWAP
548 EXPORT_SYMBOL_GPL(si_swapinfo);
549 #endif
550 EXPORT_SYMBOL_GPL(nr_threads);
551 EXPORT_SYMBOL_GPL(nr_running);
552 EXPORT_SYMBOL_GPL(nr_iowait);