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