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