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