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/uaccess.h>
31 #include <asm/appldata.h>
32 #include <asm/vtimer.h>
38 #define APPLDATA_CPU_INTERVAL 10000 /* default (CPU) time for
42 #define TOD_MICRO 0x01000 /* nr. of TOD clock units
46 * /proc entries (sysctl)
48 static const char appldata_proc_name[APPLDATA_PROC_NAME_LENGTH] = "appldata";
49 static int appldata_timer_handler(struct ctl_table *ctl, int write,
50 void *buffer, size_t *lenp, loff_t *ppos);
51 static int appldata_interval_handler(struct ctl_table *ctl, int write,
52 void *buffer, size_t *lenp, loff_t *ppos);
54 static struct ctl_table_header *appldata_sysctl_header;
55 static struct ctl_table appldata_table[] = {
58 .mode = S_IRUGO | S_IWUSR,
59 .proc_handler = appldata_timer_handler,
62 .procname = "interval",
63 .mode = S_IRUGO | S_IWUSR,
64 .proc_handler = appldata_interval_handler,
71 static struct vtimer_list appldata_timer;
73 static DEFINE_SPINLOCK(appldata_timer_lock);
74 static int appldata_interval = APPLDATA_CPU_INTERVAL;
75 static int appldata_timer_active;
80 static struct workqueue_struct *appldata_wq;
81 static void appldata_work_fn(struct work_struct *work);
82 static DECLARE_WORK(appldata_work, appldata_work_fn);
88 static DEFINE_MUTEX(appldata_ops_mutex);
89 static LIST_HEAD(appldata_ops_list);
92 /*************************** timer, work, DIAG *******************************/
94 * appldata_timer_function()
96 * schedule work and reschedule timer
98 static void appldata_timer_function(unsigned long data)
100 queue_work(appldata_wq, (struct work_struct *) data);
106 * call data gathering function for each (active) module
108 static void appldata_work_fn(struct work_struct *work)
110 struct list_head *lh;
111 struct appldata_ops *ops;
113 mutex_lock(&appldata_ops_mutex);
114 list_for_each(lh, &appldata_ops_list) {
115 ops = list_entry(lh, struct appldata_ops, list);
116 if (ops->active == 1) {
117 ops->callback(ops->data);
120 mutex_unlock(&appldata_ops_mutex);
123 static struct appldata_product_id appldata_id = {
124 .prod_nr = {0xD3, 0xC9, 0xD5, 0xE4,
125 0xE7, 0xD2, 0xD9}, /* "LINUXKR" */
126 .prod_fn = 0xD5D3, /* "NL" */
127 .version_nr = 0xF2F6, /* "26" */
128 .release_nr = 0xF0F1, /* "01" */
134 * prepare parameter list, issue DIAG 0xDC
136 int appldata_diag(char record_nr, u16 function, unsigned long buffer,
137 u16 length, char *mod_lvl)
139 struct appldata_parameter_list *parm_list;
140 struct appldata_product_id *id;
143 parm_list = kmalloc(sizeof(*parm_list), GFP_KERNEL);
144 id = kmemdup(&appldata_id, sizeof(appldata_id), GFP_KERNEL);
146 if (parm_list && id) {
147 id->record_nr = record_nr;
148 id->mod_lvl = (mod_lvl[0]) << 8 | mod_lvl[1];
149 rc = appldata_asm(parm_list, id, function,
150 (void *) buffer, length);
156 /************************ timer, work, DIAG <END> ****************************/
159 /****************************** /proc stuff **********************************/
161 #define APPLDATA_ADD_TIMER 0
162 #define APPLDATA_DEL_TIMER 1
163 #define APPLDATA_MOD_TIMER 2
166 * __appldata_vtimer_setup()
168 * Add, delete or modify virtual timers on all online cpus.
169 * The caller needs to get the appldata_timer_lock spinlock.
171 static void __appldata_vtimer_setup(int cmd)
173 u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
176 case APPLDATA_ADD_TIMER:
177 if (appldata_timer_active)
179 appldata_timer.expires = timer_interval;
180 add_virt_timer_periodic(&appldata_timer);
181 appldata_timer_active = 1;
183 case APPLDATA_DEL_TIMER:
184 del_virt_timer(&appldata_timer);
185 if (!appldata_timer_active)
187 appldata_timer_active = 0;
189 case APPLDATA_MOD_TIMER:
190 if (!appldata_timer_active)
192 mod_virt_timer_periodic(&appldata_timer, timer_interval);
197 * appldata_timer_handler()
199 * Start/Stop timer, show status of timer (0 = not active, 1 = active)
202 appldata_timer_handler(struct ctl_table *ctl, int write,
203 void *buffer, size_t *lenp, loff_t *ppos)
205 int timer_active = appldata_timer_active;
207 struct ctl_table ctl_entry = {
208 .procname = ctl->procname,
209 .data = &timer_active,
210 .maxlen = sizeof(int),
211 .extra1 = SYSCTL_ZERO,
212 .extra2 = SYSCTL_ONE,
215 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
216 if (rc < 0 || !write)
219 spin_lock(&appldata_timer_lock);
221 __appldata_vtimer_setup(APPLDATA_ADD_TIMER);
223 __appldata_vtimer_setup(APPLDATA_DEL_TIMER);
224 spin_unlock(&appldata_timer_lock);
229 * appldata_interval_handler()
231 * Set (CPU) timer interval for collection of data (in milliseconds), show
232 * current timer interval.
235 appldata_interval_handler(struct ctl_table *ctl, int write,
236 void *buffer, size_t *lenp, loff_t *ppos)
238 int interval = appldata_interval;
240 struct ctl_table ctl_entry = {
241 .procname = ctl->procname,
243 .maxlen = sizeof(int),
244 .extra1 = SYSCTL_ONE,
247 rc = proc_dointvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
248 if (rc < 0 || !write)
251 spin_lock(&appldata_timer_lock);
252 appldata_interval = interval;
253 __appldata_vtimer_setup(APPLDATA_MOD_TIMER);
254 spin_unlock(&appldata_timer_lock);
259 * appldata_generic_handler()
261 * Generic start/stop monitoring and DIAG, show status of
262 * monitoring (0 = not in process, 1 = in process)
265 appldata_generic_handler(struct ctl_table *ctl, int write,
266 void *buffer, size_t *lenp, loff_t *ppos)
268 struct appldata_ops *ops = NULL, *tmp_ops;
269 struct list_head *lh;
272 struct ctl_table ctl_entry = {
274 .maxlen = sizeof(int),
275 .extra1 = SYSCTL_ZERO,
276 .extra2 = SYSCTL_ONE,
280 mutex_lock(&appldata_ops_mutex);
281 list_for_each(lh, &appldata_ops_list) {
282 tmp_ops = list_entry(lh, struct appldata_ops, list);
283 if (&tmp_ops->ctl_table[0] == ctl) {
288 mutex_unlock(&appldata_ops_mutex);
292 if (!try_module_get(ops->owner)) { // protect this function
293 mutex_unlock(&appldata_ops_mutex);
296 mutex_unlock(&appldata_ops_mutex);
298 active = ops->active;
299 rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
300 if (rc < 0 || !write) {
301 module_put(ops->owner);
305 mutex_lock(&appldata_ops_mutex);
306 if (active && (ops->active == 0)) {
307 // protect work queue callback
308 if (!try_module_get(ops->owner)) {
309 mutex_unlock(&appldata_ops_mutex);
310 module_put(ops->owner);
313 ops->callback(ops->data); // init record
314 rc = appldata_diag(ops->record_nr,
315 APPLDATA_START_INTERVAL_REC,
316 (unsigned long) ops->data, ops->size,
319 pr_err("Starting the data collection for %s "
320 "failed with rc=%d\n", ops->name, rc);
321 module_put(ops->owner);
324 } else if (!active && (ops->active == 1)) {
326 rc = appldata_diag(ops->record_nr, APPLDATA_STOP_REC,
327 (unsigned long) ops->data, ops->size,
330 pr_err("Stopping the data collection for %s "
331 "failed with rc=%d\n", ops->name, rc);
332 module_put(ops->owner);
334 mutex_unlock(&appldata_ops_mutex);
335 module_put(ops->owner);
339 /*************************** /proc stuff <END> *******************************/
342 /************************* module-ops management *****************************/
344 * appldata_register_ops()
346 * update ops list, register /proc/sys entries
348 int appldata_register_ops(struct appldata_ops *ops)
350 if (ops->size > APPLDATA_MAX_REC_SIZE)
353 ops->ctl_table = kcalloc(1, sizeof(struct ctl_table), GFP_KERNEL);
357 mutex_lock(&appldata_ops_mutex);
358 list_add(&ops->list, &appldata_ops_list);
359 mutex_unlock(&appldata_ops_mutex);
361 ops->ctl_table[0].procname = ops->name;
362 ops->ctl_table[0].mode = S_IRUGO | S_IWUSR;
363 ops->ctl_table[0].proc_handler = appldata_generic_handler;
364 ops->ctl_table[0].data = ops;
366 ops->sysctl_header = register_sysctl_sz(appldata_proc_name, ops->ctl_table, 1);
367 if (!ops->sysctl_header)
371 mutex_lock(&appldata_ops_mutex);
372 list_del(&ops->list);
373 mutex_unlock(&appldata_ops_mutex);
374 kfree(ops->ctl_table);
379 * appldata_unregister_ops()
381 * update ops list, unregister /proc entries, stop DIAG if necessary
383 void appldata_unregister_ops(struct appldata_ops *ops)
385 mutex_lock(&appldata_ops_mutex);
386 list_del(&ops->list);
387 mutex_unlock(&appldata_ops_mutex);
388 unregister_sysctl_table(ops->sysctl_header);
389 kfree(ops->ctl_table);
391 /********************** module-ops management <END> **************************/
394 /******************************* init / exit *********************************/
399 * init timer, register /proc entries
401 static int __init appldata_init(void)
403 init_virt_timer(&appldata_timer);
404 appldata_timer.function = appldata_timer_function;
405 appldata_timer.data = (unsigned long) &appldata_work;
406 appldata_wq = alloc_ordered_workqueue("appldata", 0);
409 appldata_sysctl_header = register_sysctl(appldata_proc_name, appldata_table);
413 __initcall(appldata_init);
415 /**************************** init / exit <END> ******************************/
417 EXPORT_SYMBOL_GPL(appldata_register_ops);
418 EXPORT_SYMBOL_GPL(appldata_unregister_ops);
419 EXPORT_SYMBOL_GPL(appldata_diag);
422 EXPORT_SYMBOL_GPL(si_swapinfo);
424 EXPORT_SYMBOL_GPL(nr_threads);
425 EXPORT_SYMBOL_GPL(nr_running);
426 EXPORT_SYMBOL_GPL(nr_iowait);