• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *buffer, size_t *lenp, loff_t *ppos);
55 static int appldata_interval_handler(struct ctl_table *ctl, int write,
56 				     void *buffer, size_t *lenp, loff_t *ppos);
57 
58 static struct ctl_table_header *appldata_sysctl_header;
59 static struct ctl_table appldata_table[] = {
60 	{
61 		.procname	= "timer",
62 		.mode		= S_IRUGO | S_IWUSR,
63 		.proc_handler	= appldata_timer_handler,
64 	},
65 	{
66 		.procname	= "interval",
67 		.mode		= S_IRUGO | S_IWUSR,
68 		.proc_handler	= appldata_interval_handler,
69 	},
70 	{ },
71 };
72 
73 static struct ctl_table appldata_dir_table[] = {
74 	{
75 		.procname	= appldata_proc_name,
76 		.maxlen		= 0,
77 		.mode		= S_IRUGO | S_IXUGO,
78 		.child		= appldata_table,
79 	},
80 	{ },
81 };
82 
83 /*
84  * Timer
85  */
86 static struct vtimer_list appldata_timer;
87 
88 static DEFINE_SPINLOCK(appldata_timer_lock);
89 static int appldata_interval = APPLDATA_CPU_INTERVAL;
90 static int appldata_timer_active;
91 static int appldata_timer_suspended = 0;
92 
93 /*
94  * Work queue
95  */
96 static struct workqueue_struct *appldata_wq;
97 static void appldata_work_fn(struct work_struct *work);
98 static DECLARE_WORK(appldata_work, appldata_work_fn);
99 
100 
101 /*
102  * Ops list
103  */
104 static DEFINE_MUTEX(appldata_ops_mutex);
105 static LIST_HEAD(appldata_ops_list);
106 
107 
108 /*************************** timer, work, DIAG *******************************/
109 /*
110  * appldata_timer_function()
111  *
112  * schedule work and reschedule timer
113  */
appldata_timer_function(unsigned long data)114 static void appldata_timer_function(unsigned long data)
115 {
116 	queue_work(appldata_wq, (struct work_struct *) data);
117 }
118 
119 /*
120  * appldata_work_fn()
121  *
122  * call data gathering function for each (active) module
123  */
appldata_work_fn(struct work_struct * work)124 static void appldata_work_fn(struct work_struct *work)
125 {
126 	struct list_head *lh;
127 	struct appldata_ops *ops;
128 
129 	mutex_lock(&appldata_ops_mutex);
130 	list_for_each(lh, &appldata_ops_list) {
131 		ops = list_entry(lh, struct appldata_ops, list);
132 		if (ops->active == 1) {
133 			ops->callback(ops->data);
134 		}
135 	}
136 	mutex_unlock(&appldata_ops_mutex);
137 }
138 
139 static 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 
147 /*
148  * appldata_diag()
149  *
150  * prepare parameter list, issue DIAG 0xDC
151  */
appldata_diag(char record_nr,u16 function,unsigned long buffer,u16 length,char * mod_lvl)152 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
153 			u16 length, char *mod_lvl)
154 {
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;
171 }
172 /************************ timer, work, DIAG <END> ****************************/
173 
174 
175 /****************************** /proc stuff **********************************/
176 
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  */
__appldata_vtimer_setup(int cmd)187 static void __appldata_vtimer_setup(int cmd)
188 {
189 	u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
190 
191 	switch (cmd) {
192 	case APPLDATA_ADD_TIMER:
193 		if (appldata_timer_active)
194 			break;
195 		appldata_timer.expires = timer_interval;
196 		add_virt_timer_periodic(&appldata_timer);
197 		appldata_timer_active = 1;
198 		break;
199 	case APPLDATA_DEL_TIMER:
200 		del_virt_timer(&appldata_timer);
201 		if (!appldata_timer_active)
202 			break;
203 		appldata_timer_active = 0;
204 		break;
205 	case APPLDATA_MOD_TIMER:
206 		if (!appldata_timer_active)
207 			break;
208 		mod_virt_timer_periodic(&appldata_timer, timer_interval);
209 	}
210 }
211 
212 /*
213  * appldata_timer_handler()
214  *
215  * Start/Stop timer, show status of timer (0 = not active, 1 = active)
216  */
217 static int
appldata_timer_handler(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)218 appldata_timer_handler(struct ctl_table *ctl, int write,
219 			   void *buffer, size_t *lenp, loff_t *ppos)
220 {
221 	int timer_active = appldata_timer_active;
222 	int rc;
223 	struct ctl_table ctl_entry = {
224 		.procname	= ctl->procname,
225 		.data		= &timer_active,
226 		.maxlen		= sizeof(int),
227 		.extra1		= SYSCTL_ZERO,
228 		.extra2		= SYSCTL_ONE,
229 	};
230 
231 	rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
232 	if (rc < 0 || !write)
233 		return rc;
234 
235 	spin_lock(&appldata_timer_lock);
236 	if (timer_active)
237 		__appldata_vtimer_setup(APPLDATA_ADD_TIMER);
238 	else
239 		__appldata_vtimer_setup(APPLDATA_DEL_TIMER);
240 	spin_unlock(&appldata_timer_lock);
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  */
250 static int
appldata_interval_handler(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)251 appldata_interval_handler(struct ctl_table *ctl, int write,
252 			   void *buffer, size_t *lenp, loff_t *ppos)
253 {
254 	int interval = appldata_interval;
255 	int rc;
256 	struct ctl_table ctl_entry = {
257 		.procname	= ctl->procname,
258 		.data		= &interval,
259 		.maxlen		= sizeof(int),
260 		.extra1		= SYSCTL_ONE,
261 	};
262 
263 	rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
264 	if (rc < 0 || !write)
265 		return rc;
266 
267 	spin_lock(&appldata_timer_lock);
268 	appldata_interval = interval;
269 	__appldata_vtimer_setup(APPLDATA_MOD_TIMER);
270 	spin_unlock(&appldata_timer_lock);
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  */
280 static int
appldata_generic_handler(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)281 appldata_generic_handler(struct ctl_table *ctl, int write,
282 			   void *buffer, size_t *lenp, loff_t *ppos)
283 {
284 	struct appldata_ops *ops = NULL, *tmp_ops;
285 	struct list_head *lh;
286 	int rc, found;
287 	int active;
288 	struct ctl_table ctl_entry = {
289 		.data		= &active,
290 		.maxlen		= sizeof(int),
291 		.extra1		= SYSCTL_ZERO,
292 		.extra2		= SYSCTL_ONE,
293 	};
294 
295 	found = 0;
296 	mutex_lock(&appldata_ops_mutex);
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) {
304 		mutex_unlock(&appldata_ops_mutex);
305 		return -ENODEV;
306 	}
307 	ops = ctl->data;
308 	if (!try_module_get(ops->owner)) {	// protect this function
309 		mutex_unlock(&appldata_ops_mutex);
310 		return -ENODEV;
311 	}
312 	mutex_unlock(&appldata_ops_mutex);
313 
314 	active = ops->active;
315 	rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
316 	if (rc < 0 || !write) {
317 		module_put(ops->owner);
318 		return rc;
319 	}
320 
321 	mutex_lock(&appldata_ops_mutex);
322 	if (active && (ops->active == 0)) {
323 		// protect work queue callback
324 		if (!try_module_get(ops->owner)) {
325 			mutex_unlock(&appldata_ops_mutex);
326 			module_put(ops->owner);
327 			return -ENODEV;
328 		}
329 		ops->callback(ops->data);	// init record
330 		rc = appldata_diag(ops->record_nr,
331 					APPLDATA_START_INTERVAL_REC,
332 					(unsigned long) ops->data, ops->size,
333 					ops->mod_lvl);
334 		if (rc != 0) {
335 			pr_err("Starting the data collection for %s "
336 			       "failed with rc=%d\n", ops->name, rc);
337 			module_put(ops->owner);
338 		} else
339 			ops->active = 1;
340 	} else if (!active && (ops->active == 1)) {
341 		ops->active = 0;
342 		rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
343 				(unsigned long) ops->data, ops->size,
344 				ops->mod_lvl);
345 		if (rc != 0)
346 			pr_err("Stopping the data collection for %s "
347 			       "failed with rc=%d\n", ops->name, rc);
348 		module_put(ops->owner);
349 	}
350 	mutex_unlock(&appldata_ops_mutex);
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  */
appldata_register_ops(struct appldata_ops * ops)364 int appldata_register_ops(struct appldata_ops *ops)
365 {
366 	if (ops->size > APPLDATA_MAX_REC_SIZE)
367 		return -EINVAL;
368 
369 	ops->ctl_table = kcalloc(4, sizeof(struct ctl_table), GFP_KERNEL);
370 	if (!ops->ctl_table)
371 		return -ENOMEM;
372 
373 	mutex_lock(&appldata_ops_mutex);
374 	list_add(&ops->list, &appldata_ops_list);
375 	mutex_unlock(&appldata_ops_mutex);
376 
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 
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 
387 	ops->sysctl_header = register_sysctl_table(ops->ctl_table);
388 	if (!ops->sysctl_header)
389 		goto out;
390 	return 0;
391 out:
392 	mutex_lock(&appldata_ops_mutex);
393 	list_del(&ops->list);
394 	mutex_unlock(&appldata_ops_mutex);
395 	kfree(ops->ctl_table);
396 	return -ENOMEM;
397 }
398 
399 /*
400  * appldata_unregister_ops()
401  *
402  * update ops list, unregister /proc entries, stop DIAG if necessary
403  */
appldata_unregister_ops(struct appldata_ops * ops)404 void appldata_unregister_ops(struct appldata_ops *ops)
405 {
406 	mutex_lock(&appldata_ops_mutex);
407 	list_del(&ops->list);
408 	mutex_unlock(&appldata_ops_mutex);
409 	unregister_sysctl_table(ops->sysctl_header);
410 	kfree(ops->ctl_table);
411 }
412 /********************** module-ops management <END> **************************/
413 
414 
415 /**************************** suspend / resume *******************************/
appldata_freeze(struct device * dev)416 static int appldata_freeze(struct device *dev)
417 {
418 	struct appldata_ops *ops;
419 	int rc;
420 	struct list_head *lh;
421 
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);
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 
appldata_restore(struct device * dev)445 static int appldata_restore(struct device *dev)
446 {
447 	struct appldata_ops *ops;
448 	int rc;
449 	struct list_head *lh;
450 
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);
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 
appldata_thaw(struct device * dev)477 static int appldata_thaw(struct device *dev)
478 {
479 	return appldata_restore(dev);
480 }
481 
482 static const struct dev_pm_ops appldata_pm_ops = {
483 	.freeze		= appldata_freeze,
484 	.thaw		= appldata_thaw,
485 	.restore	= appldata_restore,
486 };
487 
488 static struct platform_driver appldata_pdrv = {
489 	.driver = {
490 		.name	= "appldata",
491 		.pm	= &appldata_pm_ops,
492 	},
493 };
494 /************************* suspend / resume <END> ****************************/
495 
496 
497 /******************************* init / exit *********************************/
498 
499 /*
500  * appldata_init()
501  *
502  * init timer, register /proc entries
503  */
appldata_init(void)504 static int __init appldata_init(void)
505 {
506 	int rc;
507 
508 	init_virt_timer(&appldata_timer);
509 	appldata_timer.function = appldata_timer_function;
510 	appldata_timer.data = (unsigned long) &appldata_work;
511 
512 	rc = platform_driver_register(&appldata_pdrv);
513 	if (rc)
514 		return rc;
515 
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 	}
522 	appldata_wq = alloc_ordered_workqueue("appldata", 0);
523 	if (!appldata_wq) {
524 		rc = -ENOMEM;
525 		goto out_device;
526 	}
527 
528 	appldata_sysctl_header = register_sysctl_table(appldata_dir_table);
529 	return 0;
530 
531 out_device:
532 	platform_device_unregister(appldata_pdev);
533 out_driver:
534 	platform_driver_unregister(&appldata_pdrv);
535 	return rc;
536 }
537 
538 __initcall(appldata_init);
539 
540 /**************************** init / exit <END> ******************************/
541 
542 EXPORT_SYMBOL_GPL(appldata_register_ops);
543 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
544 EXPORT_SYMBOL_GPL(appldata_diag);
545 
546 #ifdef CONFIG_SWAP
547 EXPORT_SYMBOL_GPL(si_swapinfo);
548 #endif
549 EXPORT_SYMBOL_GPL(nr_threads);
550 EXPORT_SYMBOL_GPL(nr_running);
551 EXPORT_SYMBOL_GPL(nr_iowait);
552