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