Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 1 | #include <linux/errno.h> |
| 2 | #include <linux/numa.h> |
| 3 | #include <linux/slab.h> |
| 4 | #include <linux/rculist.h> |
| 5 | #include <linux/threads.h> |
| 6 | #include <linux/preempt.h> |
| 7 | #include <linux/irqflags.h> |
| 8 | #include <linux/vmalloc.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/device-mapper.h> |
| 12 | |
| 13 | #include "dm.h" |
| 14 | #include "dm-stats.h" |
| 15 | |
| 16 | #define DM_MSG_PREFIX "stats" |
| 17 | |
| 18 | static int dm_stat_need_rcu_barrier; |
| 19 | |
| 20 | /* |
| 21 | * Using 64-bit values to avoid overflow (which is a |
| 22 | * problem that block/genhd.c's IO accounting has). |
| 23 | */ |
| 24 | struct dm_stat_percpu { |
| 25 | unsigned long long sectors[2]; |
| 26 | unsigned long long ios[2]; |
| 27 | unsigned long long merges[2]; |
| 28 | unsigned long long ticks[2]; |
| 29 | unsigned long long io_ticks[2]; |
| 30 | unsigned long long io_ticks_total; |
| 31 | unsigned long long time_in_queue; |
| 32 | }; |
| 33 | |
| 34 | struct dm_stat_shared { |
| 35 | atomic_t in_flight[2]; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 36 | unsigned long long stamp; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 37 | struct dm_stat_percpu tmp; |
| 38 | }; |
| 39 | |
| 40 | struct dm_stat { |
| 41 | struct list_head list_entry; |
| 42 | int id; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 43 | unsigned stat_flags; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 44 | size_t n_entries; |
| 45 | sector_t start; |
| 46 | sector_t end; |
| 47 | sector_t step; |
| 48 | const char *program_id; |
| 49 | const char *aux_data; |
| 50 | struct rcu_head rcu_head; |
| 51 | size_t shared_alloc_size; |
| 52 | size_t percpu_alloc_size; |
| 53 | struct dm_stat_percpu *stat_percpu[NR_CPUS]; |
| 54 | struct dm_stat_shared stat_shared[0]; |
| 55 | }; |
| 56 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 57 | #define STAT_PRECISE_TIMESTAMPS 1 |
| 58 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 59 | struct dm_stats_last_position { |
| 60 | sector_t last_sector; |
| 61 | unsigned last_rw; |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * A typo on the command line could possibly make the kernel run out of memory |
| 66 | * and crash. To prevent the crash we account all used memory. We fail if we |
| 67 | * exhaust 1/4 of all memory or 1/2 of vmalloc space. |
| 68 | */ |
| 69 | #define DM_STATS_MEMORY_FACTOR 4 |
| 70 | #define DM_STATS_VMALLOC_FACTOR 2 |
| 71 | |
| 72 | static DEFINE_SPINLOCK(shared_memory_lock); |
| 73 | |
| 74 | static unsigned long shared_memory_amount; |
| 75 | |
| 76 | static bool __check_shared_memory(size_t alloc_size) |
| 77 | { |
| 78 | size_t a; |
| 79 | |
| 80 | a = shared_memory_amount + alloc_size; |
| 81 | if (a < shared_memory_amount) |
| 82 | return false; |
| 83 | if (a >> PAGE_SHIFT > totalram_pages / DM_STATS_MEMORY_FACTOR) |
| 84 | return false; |
| 85 | #ifdef CONFIG_MMU |
| 86 | if (a > (VMALLOC_END - VMALLOC_START) / DM_STATS_VMALLOC_FACTOR) |
| 87 | return false; |
| 88 | #endif |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | static bool check_shared_memory(size_t alloc_size) |
| 93 | { |
| 94 | bool ret; |
| 95 | |
| 96 | spin_lock_irq(&shared_memory_lock); |
| 97 | |
| 98 | ret = __check_shared_memory(alloc_size); |
| 99 | |
| 100 | spin_unlock_irq(&shared_memory_lock); |
| 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | static bool claim_shared_memory(size_t alloc_size) |
| 106 | { |
| 107 | spin_lock_irq(&shared_memory_lock); |
| 108 | |
| 109 | if (!__check_shared_memory(alloc_size)) { |
| 110 | spin_unlock_irq(&shared_memory_lock); |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | shared_memory_amount += alloc_size; |
| 115 | |
| 116 | spin_unlock_irq(&shared_memory_lock); |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | static void free_shared_memory(size_t alloc_size) |
| 122 | { |
| 123 | unsigned long flags; |
| 124 | |
| 125 | spin_lock_irqsave(&shared_memory_lock, flags); |
| 126 | |
| 127 | if (WARN_ON_ONCE(shared_memory_amount < alloc_size)) { |
| 128 | spin_unlock_irqrestore(&shared_memory_lock, flags); |
| 129 | DMCRIT("Memory usage accounting bug."); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | shared_memory_amount -= alloc_size; |
| 134 | |
| 135 | spin_unlock_irqrestore(&shared_memory_lock, flags); |
| 136 | } |
| 137 | |
| 138 | static void *dm_kvzalloc(size_t alloc_size, int node) |
| 139 | { |
| 140 | void *p; |
| 141 | |
| 142 | if (!claim_shared_memory(alloc_size)) |
| 143 | return NULL; |
| 144 | |
| 145 | if (alloc_size <= KMALLOC_MAX_SIZE) { |
| 146 | p = kzalloc_node(alloc_size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN, node); |
| 147 | if (p) |
| 148 | return p; |
| 149 | } |
| 150 | p = vzalloc_node(alloc_size, node); |
| 151 | if (p) |
| 152 | return p; |
| 153 | |
| 154 | free_shared_memory(alloc_size); |
| 155 | |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | static void dm_kvfree(void *ptr, size_t alloc_size) |
| 160 | { |
| 161 | if (!ptr) |
| 162 | return; |
| 163 | |
| 164 | free_shared_memory(alloc_size); |
| 165 | |
Pekka Enberg | 0f24b79 | 2015-05-15 21:35:21 +0300 | [diff] [blame] | 166 | kvfree(ptr); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void dm_stat_free(struct rcu_head *head) |
| 170 | { |
| 171 | int cpu; |
| 172 | struct dm_stat *s = container_of(head, struct dm_stat, rcu_head); |
| 173 | |
| 174 | kfree(s->program_id); |
| 175 | kfree(s->aux_data); |
| 176 | for_each_possible_cpu(cpu) |
| 177 | dm_kvfree(s->stat_percpu[cpu], s->percpu_alloc_size); |
| 178 | dm_kvfree(s, s->shared_alloc_size); |
| 179 | } |
| 180 | |
| 181 | static int dm_stat_in_flight(struct dm_stat_shared *shared) |
| 182 | { |
| 183 | return atomic_read(&shared->in_flight[READ]) + |
| 184 | atomic_read(&shared->in_flight[WRITE]); |
| 185 | } |
| 186 | |
| 187 | void dm_stats_init(struct dm_stats *stats) |
| 188 | { |
| 189 | int cpu; |
| 190 | struct dm_stats_last_position *last; |
| 191 | |
| 192 | mutex_init(&stats->mutex); |
| 193 | INIT_LIST_HEAD(&stats->list); |
| 194 | stats->last = alloc_percpu(struct dm_stats_last_position); |
| 195 | for_each_possible_cpu(cpu) { |
| 196 | last = per_cpu_ptr(stats->last, cpu); |
| 197 | last->last_sector = (sector_t)ULLONG_MAX; |
| 198 | last->last_rw = UINT_MAX; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void dm_stats_cleanup(struct dm_stats *stats) |
| 203 | { |
| 204 | size_t ni; |
| 205 | struct dm_stat *s; |
| 206 | struct dm_stat_shared *shared; |
| 207 | |
| 208 | while (!list_empty(&stats->list)) { |
| 209 | s = container_of(stats->list.next, struct dm_stat, list_entry); |
| 210 | list_del(&s->list_entry); |
| 211 | for (ni = 0; ni < s->n_entries; ni++) { |
| 212 | shared = &s->stat_shared[ni]; |
| 213 | if (WARN_ON(dm_stat_in_flight(shared))) { |
| 214 | DMCRIT("leaked in-flight counter at index %lu " |
| 215 | "(start %llu, end %llu, step %llu): reads %d, writes %d", |
| 216 | (unsigned long)ni, |
| 217 | (unsigned long long)s->start, |
| 218 | (unsigned long long)s->end, |
| 219 | (unsigned long long)s->step, |
| 220 | atomic_read(&shared->in_flight[READ]), |
| 221 | atomic_read(&shared->in_flight[WRITE])); |
| 222 | } |
| 223 | } |
| 224 | dm_stat_free(&s->rcu_head); |
| 225 | } |
| 226 | free_percpu(stats->last); |
| 227 | } |
| 228 | |
| 229 | static int dm_stats_create(struct dm_stats *stats, sector_t start, sector_t end, |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 230 | sector_t step, unsigned stat_flags, |
| 231 | const char *program_id, const char *aux_data, |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 232 | void (*suspend_callback)(struct mapped_device *), |
| 233 | void (*resume_callback)(struct mapped_device *), |
| 234 | struct mapped_device *md) |
| 235 | { |
| 236 | struct list_head *l; |
| 237 | struct dm_stat *s, *tmp_s; |
| 238 | sector_t n_entries; |
| 239 | size_t ni; |
| 240 | size_t shared_alloc_size; |
| 241 | size_t percpu_alloc_size; |
| 242 | struct dm_stat_percpu *p; |
| 243 | int cpu; |
| 244 | int ret_id; |
| 245 | int r; |
| 246 | |
| 247 | if (end < start || !step) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | n_entries = end - start; |
| 251 | if (dm_sector_div64(n_entries, step)) |
| 252 | n_entries++; |
| 253 | |
| 254 | if (n_entries != (size_t)n_entries || !(size_t)(n_entries + 1)) |
| 255 | return -EOVERFLOW; |
| 256 | |
| 257 | shared_alloc_size = sizeof(struct dm_stat) + (size_t)n_entries * sizeof(struct dm_stat_shared); |
| 258 | if ((shared_alloc_size - sizeof(struct dm_stat)) / sizeof(struct dm_stat_shared) != n_entries) |
| 259 | return -EOVERFLOW; |
| 260 | |
| 261 | percpu_alloc_size = (size_t)n_entries * sizeof(struct dm_stat_percpu); |
| 262 | if (percpu_alloc_size / sizeof(struct dm_stat_percpu) != n_entries) |
| 263 | return -EOVERFLOW; |
| 264 | |
| 265 | if (!check_shared_memory(shared_alloc_size + num_possible_cpus() * percpu_alloc_size)) |
| 266 | return -ENOMEM; |
| 267 | |
| 268 | s = dm_kvzalloc(shared_alloc_size, NUMA_NO_NODE); |
| 269 | if (!s) |
| 270 | return -ENOMEM; |
| 271 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 272 | s->stat_flags = stat_flags; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 273 | s->n_entries = n_entries; |
| 274 | s->start = start; |
| 275 | s->end = end; |
| 276 | s->step = step; |
| 277 | s->shared_alloc_size = shared_alloc_size; |
| 278 | s->percpu_alloc_size = percpu_alloc_size; |
| 279 | |
| 280 | s->program_id = kstrdup(program_id, GFP_KERNEL); |
| 281 | if (!s->program_id) { |
| 282 | r = -ENOMEM; |
| 283 | goto out; |
| 284 | } |
| 285 | s->aux_data = kstrdup(aux_data, GFP_KERNEL); |
| 286 | if (!s->aux_data) { |
| 287 | r = -ENOMEM; |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | for (ni = 0; ni < n_entries; ni++) { |
| 292 | atomic_set(&s->stat_shared[ni].in_flight[READ], 0); |
| 293 | atomic_set(&s->stat_shared[ni].in_flight[WRITE], 0); |
| 294 | } |
| 295 | |
| 296 | for_each_possible_cpu(cpu) { |
| 297 | p = dm_kvzalloc(percpu_alloc_size, cpu_to_node(cpu)); |
| 298 | if (!p) { |
| 299 | r = -ENOMEM; |
| 300 | goto out; |
| 301 | } |
| 302 | s->stat_percpu[cpu] = p; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Suspend/resume to make sure there is no i/o in flight, |
| 307 | * so that newly created statistics will be exact. |
| 308 | * |
| 309 | * (note: we couldn't suspend earlier because we must not |
| 310 | * allocate memory while suspended) |
| 311 | */ |
| 312 | suspend_callback(md); |
| 313 | |
| 314 | mutex_lock(&stats->mutex); |
| 315 | s->id = 0; |
| 316 | list_for_each(l, &stats->list) { |
| 317 | tmp_s = container_of(l, struct dm_stat, list_entry); |
| 318 | if (WARN_ON(tmp_s->id < s->id)) { |
| 319 | r = -EINVAL; |
| 320 | goto out_unlock_resume; |
| 321 | } |
| 322 | if (tmp_s->id > s->id) |
| 323 | break; |
| 324 | if (unlikely(s->id == INT_MAX)) { |
| 325 | r = -ENFILE; |
| 326 | goto out_unlock_resume; |
| 327 | } |
| 328 | s->id++; |
| 329 | } |
| 330 | ret_id = s->id; |
| 331 | list_add_tail_rcu(&s->list_entry, l); |
| 332 | mutex_unlock(&stats->mutex); |
| 333 | |
| 334 | resume_callback(md); |
| 335 | |
| 336 | return ret_id; |
| 337 | |
| 338 | out_unlock_resume: |
| 339 | mutex_unlock(&stats->mutex); |
| 340 | resume_callback(md); |
| 341 | out: |
| 342 | dm_stat_free(&s->rcu_head); |
| 343 | return r; |
| 344 | } |
| 345 | |
| 346 | static struct dm_stat *__dm_stats_find(struct dm_stats *stats, int id) |
| 347 | { |
| 348 | struct dm_stat *s; |
| 349 | |
| 350 | list_for_each_entry(s, &stats->list, list_entry) { |
| 351 | if (s->id > id) |
| 352 | break; |
| 353 | if (s->id == id) |
| 354 | return s; |
| 355 | } |
| 356 | |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | static int dm_stats_delete(struct dm_stats *stats, int id) |
| 361 | { |
| 362 | struct dm_stat *s; |
| 363 | int cpu; |
| 364 | |
| 365 | mutex_lock(&stats->mutex); |
| 366 | |
| 367 | s = __dm_stats_find(stats, id); |
| 368 | if (!s) { |
| 369 | mutex_unlock(&stats->mutex); |
| 370 | return -ENOENT; |
| 371 | } |
| 372 | |
| 373 | list_del_rcu(&s->list_entry); |
| 374 | mutex_unlock(&stats->mutex); |
| 375 | |
| 376 | /* |
| 377 | * vfree can't be called from RCU callback |
| 378 | */ |
| 379 | for_each_possible_cpu(cpu) |
| 380 | if (is_vmalloc_addr(s->stat_percpu)) |
| 381 | goto do_sync_free; |
| 382 | if (is_vmalloc_addr(s)) { |
| 383 | do_sync_free: |
| 384 | synchronize_rcu_expedited(); |
| 385 | dm_stat_free(&s->rcu_head); |
| 386 | } else { |
| 387 | ACCESS_ONCE(dm_stat_need_rcu_barrier) = 1; |
| 388 | call_rcu(&s->rcu_head, dm_stat_free); |
| 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int dm_stats_list(struct dm_stats *stats, const char *program, |
| 394 | char *result, unsigned maxlen) |
| 395 | { |
| 396 | struct dm_stat *s; |
| 397 | sector_t len; |
| 398 | unsigned sz = 0; |
| 399 | |
| 400 | /* |
| 401 | * Output format: |
| 402 | * <region_id>: <start_sector>+<length> <step> <program_id> <aux_data> |
| 403 | */ |
| 404 | |
| 405 | mutex_lock(&stats->mutex); |
| 406 | list_for_each_entry(s, &stats->list, list_entry) { |
| 407 | if (!program || !strcmp(program, s->program_id)) { |
| 408 | len = s->end - s->start; |
| 409 | DMEMIT("%d: %llu+%llu %llu %s %s\n", s->id, |
| 410 | (unsigned long long)s->start, |
| 411 | (unsigned long long)len, |
| 412 | (unsigned long long)s->step, |
| 413 | s->program_id, |
| 414 | s->aux_data); |
| 415 | } |
| 416 | } |
| 417 | mutex_unlock(&stats->mutex); |
| 418 | |
| 419 | return 1; |
| 420 | } |
| 421 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 422 | static void dm_stat_round(struct dm_stat *s, struct dm_stat_shared *shared, |
| 423 | struct dm_stat_percpu *p) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 424 | { |
| 425 | /* |
| 426 | * This is racy, but so is part_round_stats_single. |
| 427 | */ |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 428 | unsigned long long now, difference; |
| 429 | unsigned in_flight_read, in_flight_write; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 430 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 431 | if (likely(!(s->stat_flags & STAT_PRECISE_TIMESTAMPS))) |
| 432 | now = jiffies; |
| 433 | else |
| 434 | now = ktime_to_ns(ktime_get()); |
| 435 | |
| 436 | difference = now - shared->stamp; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 437 | if (!difference) |
| 438 | return; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 439 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 440 | in_flight_read = (unsigned)atomic_read(&shared->in_flight[READ]); |
| 441 | in_flight_write = (unsigned)atomic_read(&shared->in_flight[WRITE]); |
| 442 | if (in_flight_read) |
| 443 | p->io_ticks[READ] += difference; |
| 444 | if (in_flight_write) |
| 445 | p->io_ticks[WRITE] += difference; |
| 446 | if (in_flight_read + in_flight_write) { |
| 447 | p->io_ticks_total += difference; |
| 448 | p->time_in_queue += (in_flight_read + in_flight_write) * difference; |
| 449 | } |
| 450 | shared->stamp = now; |
| 451 | } |
| 452 | |
| 453 | static void dm_stat_for_entry(struct dm_stat *s, size_t entry, |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 454 | unsigned long bi_rw, sector_t len, |
| 455 | struct dm_stats_aux *stats_aux, bool end, |
| 456 | unsigned long duration_jiffies) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 457 | { |
| 458 | unsigned long idx = bi_rw & REQ_WRITE; |
| 459 | struct dm_stat_shared *shared = &s->stat_shared[entry]; |
| 460 | struct dm_stat_percpu *p; |
| 461 | |
| 462 | /* |
Mikulas Patocka | bbf3f8c | 2013-09-13 17:42:24 -0400 | [diff] [blame] | 463 | * For strict correctness we should use local_irq_save/restore |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 464 | * instead of preempt_disable/enable. |
| 465 | * |
Mikulas Patocka | bbf3f8c | 2013-09-13 17:42:24 -0400 | [diff] [blame] | 466 | * preempt_disable/enable is racy if the driver finishes bios |
| 467 | * from non-interrupt context as well as from interrupt context |
| 468 | * or from more different interrupts. |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 469 | * |
Mikulas Patocka | bbf3f8c | 2013-09-13 17:42:24 -0400 | [diff] [blame] | 470 | * On 64-bit architectures the race only results in not counting some |
| 471 | * events, so it is acceptable. On 32-bit architectures the race could |
| 472 | * cause the counter going off by 2^32, so we need to do proper locking |
| 473 | * there. |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 474 | * |
| 475 | * part_stat_lock()/part_stat_unlock() have this race too. |
| 476 | */ |
Mikulas Patocka | bbf3f8c | 2013-09-13 17:42:24 -0400 | [diff] [blame] | 477 | #if BITS_PER_LONG == 32 |
| 478 | unsigned long flags; |
| 479 | local_irq_save(flags); |
| 480 | #else |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 481 | preempt_disable(); |
Mikulas Patocka | bbf3f8c | 2013-09-13 17:42:24 -0400 | [diff] [blame] | 482 | #endif |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 483 | p = &s->stat_percpu[smp_processor_id()][entry]; |
| 484 | |
| 485 | if (!end) { |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 486 | dm_stat_round(s, shared, p); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 487 | atomic_inc(&shared->in_flight[idx]); |
| 488 | } else { |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 489 | dm_stat_round(s, shared, p); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 490 | atomic_dec(&shared->in_flight[idx]); |
| 491 | p->sectors[idx] += len; |
| 492 | p->ios[idx] += 1; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 493 | p->merges[idx] += stats_aux->merged; |
| 494 | if (!(s->stat_flags & STAT_PRECISE_TIMESTAMPS)) |
| 495 | p->ticks[idx] += duration_jiffies; |
| 496 | else |
| 497 | p->ticks[idx] += stats_aux->duration_ns; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 498 | } |
| 499 | |
Mikulas Patocka | bbf3f8c | 2013-09-13 17:42:24 -0400 | [diff] [blame] | 500 | #if BITS_PER_LONG == 32 |
| 501 | local_irq_restore(flags); |
| 502 | #else |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 503 | preempt_enable(); |
Mikulas Patocka | bbf3f8c | 2013-09-13 17:42:24 -0400 | [diff] [blame] | 504 | #endif |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static void __dm_stat_bio(struct dm_stat *s, unsigned long bi_rw, |
| 508 | sector_t bi_sector, sector_t end_sector, |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 509 | bool end, unsigned long duration_jiffies, |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 510 | struct dm_stats_aux *stats_aux) |
| 511 | { |
| 512 | sector_t rel_sector, offset, todo, fragment_len; |
| 513 | size_t entry; |
| 514 | |
| 515 | if (end_sector <= s->start || bi_sector >= s->end) |
| 516 | return; |
| 517 | if (unlikely(bi_sector < s->start)) { |
| 518 | rel_sector = 0; |
| 519 | todo = end_sector - s->start; |
| 520 | } else { |
| 521 | rel_sector = bi_sector - s->start; |
| 522 | todo = end_sector - bi_sector; |
| 523 | } |
| 524 | if (unlikely(end_sector > s->end)) |
| 525 | todo -= (end_sector - s->end); |
| 526 | |
| 527 | offset = dm_sector_div64(rel_sector, s->step); |
| 528 | entry = rel_sector; |
| 529 | do { |
| 530 | if (WARN_ON_ONCE(entry >= s->n_entries)) { |
| 531 | DMCRIT("Invalid area access in region id %d", s->id); |
| 532 | return; |
| 533 | } |
| 534 | fragment_len = todo; |
| 535 | if (fragment_len > s->step - offset) |
| 536 | fragment_len = s->step - offset; |
| 537 | dm_stat_for_entry(s, entry, bi_rw, fragment_len, |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 538 | stats_aux, end, duration_jiffies); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 539 | todo -= fragment_len; |
| 540 | entry++; |
| 541 | offset = 0; |
| 542 | } while (unlikely(todo != 0)); |
| 543 | } |
| 544 | |
| 545 | void dm_stats_account_io(struct dm_stats *stats, unsigned long bi_rw, |
| 546 | sector_t bi_sector, unsigned bi_sectors, bool end, |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 547 | unsigned long duration_jiffies, |
| 548 | struct dm_stats_aux *stats_aux) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 549 | { |
| 550 | struct dm_stat *s; |
| 551 | sector_t end_sector; |
| 552 | struct dm_stats_last_position *last; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 553 | bool got_precise_time; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 554 | |
| 555 | if (unlikely(!bi_sectors)) |
| 556 | return; |
| 557 | |
| 558 | end_sector = bi_sector + bi_sectors; |
| 559 | |
| 560 | if (!end) { |
| 561 | /* |
| 562 | * A race condition can at worst result in the merged flag being |
| 563 | * misrepresented, so we don't have to disable preemption here. |
| 564 | */ |
Christoph Lameter | 1f125e7 | 2014-08-17 12:30:36 -0500 | [diff] [blame] | 565 | last = raw_cpu_ptr(stats->last); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 566 | stats_aux->merged = |
| 567 | (bi_sector == (ACCESS_ONCE(last->last_sector) && |
| 568 | ((bi_rw & (REQ_WRITE | REQ_DISCARD)) == |
| 569 | (ACCESS_ONCE(last->last_rw) & (REQ_WRITE | REQ_DISCARD))) |
| 570 | )); |
| 571 | ACCESS_ONCE(last->last_sector) = end_sector; |
| 572 | ACCESS_ONCE(last->last_rw) = bi_rw; |
| 573 | } |
| 574 | |
| 575 | rcu_read_lock(); |
| 576 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 577 | got_precise_time = false; |
| 578 | list_for_each_entry_rcu(s, &stats->list, list_entry) { |
| 579 | if (s->stat_flags & STAT_PRECISE_TIMESTAMPS && !got_precise_time) { |
| 580 | if (!end) |
| 581 | stats_aux->duration_ns = ktime_to_ns(ktime_get()); |
| 582 | else |
| 583 | stats_aux->duration_ns = ktime_to_ns(ktime_get()) - stats_aux->duration_ns; |
| 584 | got_precise_time = true; |
| 585 | } |
| 586 | __dm_stat_bio(s, bi_rw, bi_sector, end_sector, end, duration_jiffies, stats_aux); |
| 587 | } |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 588 | |
| 589 | rcu_read_unlock(); |
| 590 | } |
| 591 | |
| 592 | static void __dm_stat_init_temporary_percpu_totals(struct dm_stat_shared *shared, |
| 593 | struct dm_stat *s, size_t x) |
| 594 | { |
| 595 | int cpu; |
| 596 | struct dm_stat_percpu *p; |
| 597 | |
| 598 | local_irq_disable(); |
| 599 | p = &s->stat_percpu[smp_processor_id()][x]; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 600 | dm_stat_round(s, shared, p); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 601 | local_irq_enable(); |
| 602 | |
| 603 | memset(&shared->tmp, 0, sizeof(shared->tmp)); |
| 604 | for_each_possible_cpu(cpu) { |
| 605 | p = &s->stat_percpu[cpu][x]; |
| 606 | shared->tmp.sectors[READ] += ACCESS_ONCE(p->sectors[READ]); |
| 607 | shared->tmp.sectors[WRITE] += ACCESS_ONCE(p->sectors[WRITE]); |
| 608 | shared->tmp.ios[READ] += ACCESS_ONCE(p->ios[READ]); |
| 609 | shared->tmp.ios[WRITE] += ACCESS_ONCE(p->ios[WRITE]); |
| 610 | shared->tmp.merges[READ] += ACCESS_ONCE(p->merges[READ]); |
| 611 | shared->tmp.merges[WRITE] += ACCESS_ONCE(p->merges[WRITE]); |
| 612 | shared->tmp.ticks[READ] += ACCESS_ONCE(p->ticks[READ]); |
| 613 | shared->tmp.ticks[WRITE] += ACCESS_ONCE(p->ticks[WRITE]); |
| 614 | shared->tmp.io_ticks[READ] += ACCESS_ONCE(p->io_ticks[READ]); |
| 615 | shared->tmp.io_ticks[WRITE] += ACCESS_ONCE(p->io_ticks[WRITE]); |
| 616 | shared->tmp.io_ticks_total += ACCESS_ONCE(p->io_ticks_total); |
| 617 | shared->tmp.time_in_queue += ACCESS_ONCE(p->time_in_queue); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | static void __dm_stat_clear(struct dm_stat *s, size_t idx_start, size_t idx_end, |
| 622 | bool init_tmp_percpu_totals) |
| 623 | { |
| 624 | size_t x; |
| 625 | struct dm_stat_shared *shared; |
| 626 | struct dm_stat_percpu *p; |
| 627 | |
| 628 | for (x = idx_start; x < idx_end; x++) { |
| 629 | shared = &s->stat_shared[x]; |
| 630 | if (init_tmp_percpu_totals) |
| 631 | __dm_stat_init_temporary_percpu_totals(shared, s, x); |
| 632 | local_irq_disable(); |
| 633 | p = &s->stat_percpu[smp_processor_id()][x]; |
| 634 | p->sectors[READ] -= shared->tmp.sectors[READ]; |
| 635 | p->sectors[WRITE] -= shared->tmp.sectors[WRITE]; |
| 636 | p->ios[READ] -= shared->tmp.ios[READ]; |
| 637 | p->ios[WRITE] -= shared->tmp.ios[WRITE]; |
| 638 | p->merges[READ] -= shared->tmp.merges[READ]; |
| 639 | p->merges[WRITE] -= shared->tmp.merges[WRITE]; |
| 640 | p->ticks[READ] -= shared->tmp.ticks[READ]; |
| 641 | p->ticks[WRITE] -= shared->tmp.ticks[WRITE]; |
| 642 | p->io_ticks[READ] -= shared->tmp.io_ticks[READ]; |
| 643 | p->io_ticks[WRITE] -= shared->tmp.io_ticks[WRITE]; |
| 644 | p->io_ticks_total -= shared->tmp.io_ticks_total; |
| 645 | p->time_in_queue -= shared->tmp.time_in_queue; |
| 646 | local_irq_enable(); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | static int dm_stats_clear(struct dm_stats *stats, int id) |
| 651 | { |
| 652 | struct dm_stat *s; |
| 653 | |
| 654 | mutex_lock(&stats->mutex); |
| 655 | |
| 656 | s = __dm_stats_find(stats, id); |
| 657 | if (!s) { |
| 658 | mutex_unlock(&stats->mutex); |
| 659 | return -ENOENT; |
| 660 | } |
| 661 | |
| 662 | __dm_stat_clear(s, 0, s->n_entries, true); |
| 663 | |
| 664 | mutex_unlock(&stats->mutex); |
| 665 | |
| 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | /* |
| 670 | * This is like jiffies_to_msec, but works for 64-bit values. |
| 671 | */ |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 672 | static unsigned long long dm_jiffies_to_msec64(struct dm_stat *s, unsigned long long j) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 673 | { |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 674 | unsigned long long result; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 675 | unsigned mult; |
| 676 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 677 | if (s->stat_flags & STAT_PRECISE_TIMESTAMPS) |
| 678 | return j; |
| 679 | |
| 680 | result = 0; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 681 | if (j) |
| 682 | result = jiffies_to_msecs(j & 0x3fffff); |
| 683 | if (j >= 1 << 22) { |
| 684 | mult = jiffies_to_msecs(1 << 22); |
| 685 | result += (unsigned long long)mult * (unsigned long long)jiffies_to_msecs((j >> 22) & 0x3fffff); |
| 686 | } |
| 687 | if (j >= 1ULL << 44) |
| 688 | result += (unsigned long long)mult * (unsigned long long)mult * (unsigned long long)jiffies_to_msecs(j >> 44); |
| 689 | |
| 690 | return result; |
| 691 | } |
| 692 | |
| 693 | static int dm_stats_print(struct dm_stats *stats, int id, |
| 694 | size_t idx_start, size_t idx_len, |
| 695 | bool clear, char *result, unsigned maxlen) |
| 696 | { |
| 697 | unsigned sz = 0; |
| 698 | struct dm_stat *s; |
| 699 | size_t x; |
| 700 | sector_t start, end, step; |
| 701 | size_t idx_end; |
| 702 | struct dm_stat_shared *shared; |
| 703 | |
| 704 | /* |
| 705 | * Output format: |
| 706 | * <start_sector>+<length> counters |
| 707 | */ |
| 708 | |
| 709 | mutex_lock(&stats->mutex); |
| 710 | |
| 711 | s = __dm_stats_find(stats, id); |
| 712 | if (!s) { |
| 713 | mutex_unlock(&stats->mutex); |
| 714 | return -ENOENT; |
| 715 | } |
| 716 | |
| 717 | idx_end = idx_start + idx_len; |
| 718 | if (idx_end < idx_start || |
| 719 | idx_end > s->n_entries) |
| 720 | idx_end = s->n_entries; |
| 721 | |
| 722 | if (idx_start > idx_end) |
| 723 | idx_start = idx_end; |
| 724 | |
| 725 | step = s->step; |
| 726 | start = s->start + (step * idx_start); |
| 727 | |
| 728 | for (x = idx_start; x < idx_end; x++, start = end) { |
| 729 | shared = &s->stat_shared[x]; |
| 730 | end = start + step; |
| 731 | if (unlikely(end > s->end)) |
| 732 | end = s->end; |
| 733 | |
| 734 | __dm_stat_init_temporary_percpu_totals(shared, s, x); |
| 735 | |
| 736 | DMEMIT("%llu+%llu %llu %llu %llu %llu %llu %llu %llu %llu %d %llu %llu %llu %llu\n", |
| 737 | (unsigned long long)start, |
| 738 | (unsigned long long)step, |
| 739 | shared->tmp.ios[READ], |
| 740 | shared->tmp.merges[READ], |
| 741 | shared->tmp.sectors[READ], |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 742 | dm_jiffies_to_msec64(s, shared->tmp.ticks[READ]), |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 743 | shared->tmp.ios[WRITE], |
| 744 | shared->tmp.merges[WRITE], |
| 745 | shared->tmp.sectors[WRITE], |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 746 | dm_jiffies_to_msec64(s, shared->tmp.ticks[WRITE]), |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 747 | dm_stat_in_flight(shared), |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 748 | dm_jiffies_to_msec64(s, shared->tmp.io_ticks_total), |
| 749 | dm_jiffies_to_msec64(s, shared->tmp.time_in_queue), |
| 750 | dm_jiffies_to_msec64(s, shared->tmp.io_ticks[READ]), |
| 751 | dm_jiffies_to_msec64(s, shared->tmp.io_ticks[WRITE])); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 752 | |
| 753 | if (unlikely(sz + 1 >= maxlen)) |
| 754 | goto buffer_overflow; |
| 755 | } |
| 756 | |
| 757 | if (clear) |
| 758 | __dm_stat_clear(s, idx_start, idx_end, false); |
| 759 | |
| 760 | buffer_overflow: |
| 761 | mutex_unlock(&stats->mutex); |
| 762 | |
| 763 | return 1; |
| 764 | } |
| 765 | |
| 766 | static int dm_stats_set_aux(struct dm_stats *stats, int id, const char *aux_data) |
| 767 | { |
| 768 | struct dm_stat *s; |
| 769 | const char *new_aux_data; |
| 770 | |
| 771 | mutex_lock(&stats->mutex); |
| 772 | |
| 773 | s = __dm_stats_find(stats, id); |
| 774 | if (!s) { |
| 775 | mutex_unlock(&stats->mutex); |
| 776 | return -ENOENT; |
| 777 | } |
| 778 | |
| 779 | new_aux_data = kstrdup(aux_data, GFP_KERNEL); |
| 780 | if (!new_aux_data) { |
| 781 | mutex_unlock(&stats->mutex); |
| 782 | return -ENOMEM; |
| 783 | } |
| 784 | |
| 785 | kfree(s->aux_data); |
| 786 | s->aux_data = new_aux_data; |
| 787 | |
| 788 | mutex_unlock(&stats->mutex); |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static int message_stats_create(struct mapped_device *md, |
| 794 | unsigned argc, char **argv, |
| 795 | char *result, unsigned maxlen) |
| 796 | { |
| 797 | int id; |
| 798 | char dummy; |
| 799 | unsigned long long start, end, len, step; |
| 800 | unsigned divisor; |
| 801 | const char *program_id, *aux_data; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 802 | unsigned stat_flags = 0; |
| 803 | |
| 804 | struct dm_arg_set as, as_backup; |
| 805 | const char *a; |
| 806 | unsigned feature_args; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 807 | |
| 808 | /* |
| 809 | * Input format: |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 810 | * <range> <step> [<extra_parameters> <parameters>] [<program_id> [<aux_data>]] |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 811 | */ |
| 812 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 813 | if (argc < 3) |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 814 | return -EINVAL; |
| 815 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 816 | as.argc = argc; |
| 817 | as.argv = argv; |
| 818 | dm_consume_args(&as, 1); |
| 819 | |
| 820 | a = dm_shift_arg(&as); |
| 821 | if (!strcmp(a, "-")) { |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 822 | start = 0; |
| 823 | len = dm_get_size(md); |
| 824 | if (!len) |
| 825 | len = 1; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 826 | } else if (sscanf(a, "%llu+%llu%c", &start, &len, &dummy) != 2 || |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 827 | start != (sector_t)start || len != (sector_t)len) |
| 828 | return -EINVAL; |
| 829 | |
| 830 | end = start + len; |
| 831 | if (start >= end) |
| 832 | return -EINVAL; |
| 833 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 834 | a = dm_shift_arg(&as); |
| 835 | if (sscanf(a, "/%u%c", &divisor, &dummy) == 1) { |
Mikulas Patocka | dd4c1b7 | 2015-06-05 09:50:42 -0400 | [diff] [blame] | 836 | if (!divisor) |
| 837 | return -EINVAL; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 838 | step = end - start; |
| 839 | if (do_div(step, divisor)) |
| 840 | step++; |
| 841 | if (!step) |
| 842 | step = 1; |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 843 | } else if (sscanf(a, "%llu%c", &step, &dummy) != 1 || |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 844 | step != (sector_t)step || !step) |
| 845 | return -EINVAL; |
| 846 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 847 | as_backup = as; |
| 848 | a = dm_shift_arg(&as); |
| 849 | if (a && sscanf(a, "%u%c", &feature_args, &dummy) == 1) { |
| 850 | while (feature_args--) { |
| 851 | a = dm_shift_arg(&as); |
| 852 | if (!a) |
| 853 | return -EINVAL; |
| 854 | if (!strcasecmp(a, "precise_timestamps")) |
| 855 | stat_flags |= STAT_PRECISE_TIMESTAMPS; |
| 856 | else |
| 857 | return -EINVAL; |
| 858 | } |
| 859 | } else { |
| 860 | as = as_backup; |
| 861 | } |
| 862 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 863 | program_id = "-"; |
| 864 | aux_data = "-"; |
| 865 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 866 | a = dm_shift_arg(&as); |
| 867 | if (a) |
| 868 | program_id = a; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 869 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 870 | a = dm_shift_arg(&as); |
| 871 | if (a) |
| 872 | aux_data = a; |
| 873 | |
| 874 | if (as.argc) |
| 875 | return -EINVAL; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 876 | |
| 877 | /* |
| 878 | * If a buffer overflow happens after we created the region, |
| 879 | * it's too late (the userspace would retry with a larger |
| 880 | * buffer, but the region id that caused the overflow is already |
| 881 | * leaked). So we must detect buffer overflow in advance. |
| 882 | */ |
| 883 | snprintf(result, maxlen, "%d", INT_MAX); |
| 884 | if (dm_message_test_buffer_overflow(result, maxlen)) |
| 885 | return 1; |
| 886 | |
Mikulas Patocka | c96aec3 | 2015-06-09 17:21:39 -0400 | [diff] [blame^] | 887 | id = dm_stats_create(dm_get_stats(md), start, end, step, stat_flags, program_id, aux_data, |
Mike Snitzer | ffcc393 | 2014-10-28 18:34:52 -0400 | [diff] [blame] | 888 | dm_internal_suspend_fast, dm_internal_resume_fast, md); |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 889 | if (id < 0) |
| 890 | return id; |
| 891 | |
| 892 | snprintf(result, maxlen, "%d", id); |
| 893 | |
| 894 | return 1; |
| 895 | } |
| 896 | |
| 897 | static int message_stats_delete(struct mapped_device *md, |
| 898 | unsigned argc, char **argv) |
| 899 | { |
| 900 | int id; |
| 901 | char dummy; |
| 902 | |
| 903 | if (argc != 2) |
| 904 | return -EINVAL; |
| 905 | |
| 906 | if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0) |
| 907 | return -EINVAL; |
| 908 | |
| 909 | return dm_stats_delete(dm_get_stats(md), id); |
| 910 | } |
| 911 | |
| 912 | static int message_stats_clear(struct mapped_device *md, |
| 913 | unsigned argc, char **argv) |
| 914 | { |
| 915 | int id; |
| 916 | char dummy; |
| 917 | |
| 918 | if (argc != 2) |
| 919 | return -EINVAL; |
| 920 | |
| 921 | if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0) |
| 922 | return -EINVAL; |
| 923 | |
| 924 | return dm_stats_clear(dm_get_stats(md), id); |
| 925 | } |
| 926 | |
| 927 | static int message_stats_list(struct mapped_device *md, |
| 928 | unsigned argc, char **argv, |
| 929 | char *result, unsigned maxlen) |
| 930 | { |
| 931 | int r; |
| 932 | const char *program = NULL; |
| 933 | |
| 934 | if (argc < 1 || argc > 2) |
| 935 | return -EINVAL; |
| 936 | |
| 937 | if (argc > 1) { |
| 938 | program = kstrdup(argv[1], GFP_KERNEL); |
| 939 | if (!program) |
| 940 | return -ENOMEM; |
| 941 | } |
| 942 | |
| 943 | r = dm_stats_list(dm_get_stats(md), program, result, maxlen); |
| 944 | |
| 945 | kfree(program); |
| 946 | |
| 947 | return r; |
| 948 | } |
| 949 | |
| 950 | static int message_stats_print(struct mapped_device *md, |
| 951 | unsigned argc, char **argv, bool clear, |
| 952 | char *result, unsigned maxlen) |
| 953 | { |
| 954 | int id; |
| 955 | char dummy; |
| 956 | unsigned long idx_start = 0, idx_len = ULONG_MAX; |
| 957 | |
| 958 | if (argc != 2 && argc != 4) |
| 959 | return -EINVAL; |
| 960 | |
| 961 | if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0) |
| 962 | return -EINVAL; |
| 963 | |
| 964 | if (argc > 3) { |
| 965 | if (strcmp(argv[2], "-") && |
| 966 | sscanf(argv[2], "%lu%c", &idx_start, &dummy) != 1) |
| 967 | return -EINVAL; |
| 968 | if (strcmp(argv[3], "-") && |
| 969 | sscanf(argv[3], "%lu%c", &idx_len, &dummy) != 1) |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | |
| 973 | return dm_stats_print(dm_get_stats(md), id, idx_start, idx_len, clear, |
| 974 | result, maxlen); |
| 975 | } |
| 976 | |
| 977 | static int message_stats_set_aux(struct mapped_device *md, |
| 978 | unsigned argc, char **argv) |
| 979 | { |
| 980 | int id; |
| 981 | char dummy; |
| 982 | |
| 983 | if (argc != 3) |
| 984 | return -EINVAL; |
| 985 | |
| 986 | if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0) |
| 987 | return -EINVAL; |
| 988 | |
| 989 | return dm_stats_set_aux(dm_get_stats(md), id, argv[2]); |
| 990 | } |
| 991 | |
| 992 | int dm_stats_message(struct mapped_device *md, unsigned argc, char **argv, |
| 993 | char *result, unsigned maxlen) |
| 994 | { |
| 995 | int r; |
| 996 | |
| 997 | if (dm_request_based(md)) { |
| 998 | DMWARN("Statistics are only supported for bio-based devices"); |
| 999 | return -EOPNOTSUPP; |
| 1000 | } |
| 1001 | |
| 1002 | /* All messages here must start with '@' */ |
| 1003 | if (!strcasecmp(argv[0], "@stats_create")) |
| 1004 | r = message_stats_create(md, argc, argv, result, maxlen); |
| 1005 | else if (!strcasecmp(argv[0], "@stats_delete")) |
| 1006 | r = message_stats_delete(md, argc, argv); |
| 1007 | else if (!strcasecmp(argv[0], "@stats_clear")) |
| 1008 | r = message_stats_clear(md, argc, argv); |
| 1009 | else if (!strcasecmp(argv[0], "@stats_list")) |
| 1010 | r = message_stats_list(md, argc, argv, result, maxlen); |
| 1011 | else if (!strcasecmp(argv[0], "@stats_print")) |
| 1012 | r = message_stats_print(md, argc, argv, false, result, maxlen); |
| 1013 | else if (!strcasecmp(argv[0], "@stats_print_clear")) |
| 1014 | r = message_stats_print(md, argc, argv, true, result, maxlen); |
| 1015 | else if (!strcasecmp(argv[0], "@stats_set_aux")) |
| 1016 | r = message_stats_set_aux(md, argc, argv); |
| 1017 | else |
| 1018 | return 2; /* this wasn't a stats message */ |
| 1019 | |
| 1020 | if (r == -EINVAL) |
| 1021 | DMWARN("Invalid parameters for message %s", argv[0]); |
| 1022 | |
| 1023 | return r; |
| 1024 | } |
| 1025 | |
| 1026 | int __init dm_statistics_init(void) |
| 1027 | { |
Mikulas Patocka | 76f5bee | 2013-12-05 17:34:19 -0500 | [diff] [blame] | 1028 | shared_memory_amount = 0; |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 1029 | dm_stat_need_rcu_barrier = 0; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | void dm_statistics_exit(void) |
| 1034 | { |
| 1035 | if (dm_stat_need_rcu_barrier) |
| 1036 | rcu_barrier(); |
| 1037 | if (WARN_ON(shared_memory_amount)) |
| 1038 | DMCRIT("shared_memory_amount leaked: %lu", shared_memory_amount); |
| 1039 | } |
| 1040 | |
| 1041 | module_param_named(stats_current_allocated_bytes, shared_memory_amount, ulong, S_IRUGO); |
| 1042 | MODULE_PARM_DESC(stats_current_allocated_bytes, "Memory currently used by statistics"); |