1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/errno.h>
3 #include <linux/numa.h>
4 #include <linux/slab.h>
5 #include <linux/rculist.h>
6 #include <linux/threads.h>
7 #include <linux/preempt.h>
8 #include <linux/irqflags.h>
9 #include <linux/vmalloc.h>
11 #include <linux/module.h>
12 #include <linux/device-mapper.h>
17 #define DM_MSG_PREFIX "stats"
19 static int dm_stat_need_rcu_barrier;
22 * Using 64-bit values to avoid overflow (which is a
23 * problem that block/genhd.c's IO accounting has).
25 struct dm_stat_percpu {
26 unsigned long long sectors[2];
27 unsigned long long ios[2];
28 unsigned long long merges[2];
29 unsigned long long ticks[2];
30 unsigned long long io_ticks[2];
31 unsigned long long io_ticks_total;
32 unsigned long long time_in_queue;
33 unsigned long long *histogram;
36 struct dm_stat_shared {
37 atomic_t in_flight[2];
38 unsigned long long stamp;
39 struct dm_stat_percpu tmp;
43 struct list_head list_entry;
50 unsigned n_histogram_entries;
51 unsigned long long *histogram_boundaries;
52 const char *program_id;
54 struct rcu_head rcu_head;
55 size_t shared_alloc_size;
56 size_t percpu_alloc_size;
57 size_t histogram_alloc_size;
58 struct dm_stat_percpu *stat_percpu[NR_CPUS];
59 struct dm_stat_shared stat_shared[];
62 #define STAT_PRECISE_TIMESTAMPS 1
64 struct dm_stats_last_position {
70 * A typo on the command line could possibly make the kernel run out of memory
71 * and crash. To prevent the crash we account all used memory. We fail if we
72 * exhaust 1/4 of all memory or 1/2 of vmalloc space.
74 #define DM_STATS_MEMORY_FACTOR 4
75 #define DM_STATS_VMALLOC_FACTOR 2
77 static DEFINE_SPINLOCK(shared_memory_lock);
79 static unsigned long shared_memory_amount;
81 static bool __check_shared_memory(size_t alloc_size)
85 a = shared_memory_amount + alloc_size;
86 if (a < shared_memory_amount)
88 if (a >> PAGE_SHIFT > totalram_pages() / DM_STATS_MEMORY_FACTOR)
91 if (a > (VMALLOC_END - VMALLOC_START) / DM_STATS_VMALLOC_FACTOR)
97 static bool check_shared_memory(size_t alloc_size)
101 spin_lock_irq(&shared_memory_lock);
103 ret = __check_shared_memory(alloc_size);
105 spin_unlock_irq(&shared_memory_lock);
110 static bool claim_shared_memory(size_t alloc_size)
112 spin_lock_irq(&shared_memory_lock);
114 if (!__check_shared_memory(alloc_size)) {
115 spin_unlock_irq(&shared_memory_lock);
119 shared_memory_amount += alloc_size;
121 spin_unlock_irq(&shared_memory_lock);
126 static void free_shared_memory(size_t alloc_size)
130 spin_lock_irqsave(&shared_memory_lock, flags);
132 if (WARN_ON_ONCE(shared_memory_amount < alloc_size)) {
133 spin_unlock_irqrestore(&shared_memory_lock, flags);
134 DMCRIT("Memory usage accounting bug.");
138 shared_memory_amount -= alloc_size;
140 spin_unlock_irqrestore(&shared_memory_lock, flags);
143 static void *dm_kvzalloc(size_t alloc_size, int node)
147 if (!claim_shared_memory(alloc_size))
150 p = kvzalloc_node(alloc_size, GFP_KERNEL | __GFP_NOMEMALLOC, node);
154 free_shared_memory(alloc_size);
159 static void dm_kvfree(void *ptr, size_t alloc_size)
164 free_shared_memory(alloc_size);
169 static void dm_stat_free(struct rcu_head *head)
172 struct dm_stat *s = container_of(head, struct dm_stat, rcu_head);
174 kfree(s->histogram_boundaries);
175 kfree(s->program_id);
177 for_each_possible_cpu(cpu) {
178 dm_kvfree(s->stat_percpu[cpu][0].histogram, s->histogram_alloc_size);
179 dm_kvfree(s->stat_percpu[cpu], s->percpu_alloc_size);
181 dm_kvfree(s->stat_shared[0].tmp.histogram, s->histogram_alloc_size);
182 dm_kvfree(s, s->shared_alloc_size);
185 static int dm_stat_in_flight(struct dm_stat_shared *shared)
187 return atomic_read(&shared->in_flight[READ]) +
188 atomic_read(&shared->in_flight[WRITE]);
191 void dm_stats_init(struct dm_stats *stats)
194 struct dm_stats_last_position *last;
196 mutex_init(&stats->mutex);
197 INIT_LIST_HEAD(&stats->list);
198 stats->precise_timestamps = false;
199 stats->last = alloc_percpu(struct dm_stats_last_position);
200 for_each_possible_cpu(cpu) {
201 last = per_cpu_ptr(stats->last, cpu);
202 last->last_sector = (sector_t)ULLONG_MAX;
203 last->last_rw = UINT_MAX;
207 void dm_stats_cleanup(struct dm_stats *stats)
211 struct dm_stat_shared *shared;
213 while (!list_empty(&stats->list)) {
214 s = container_of(stats->list.next, struct dm_stat, list_entry);
215 list_del(&s->list_entry);
216 for (ni = 0; ni < s->n_entries; ni++) {
217 shared = &s->stat_shared[ni];
218 if (WARN_ON(dm_stat_in_flight(shared))) {
219 DMCRIT("leaked in-flight counter at index %lu "
220 "(start %llu, end %llu, step %llu): reads %d, writes %d",
222 (unsigned long long)s->start,
223 (unsigned long long)s->end,
224 (unsigned long long)s->step,
225 atomic_read(&shared->in_flight[READ]),
226 atomic_read(&shared->in_flight[WRITE]));
230 dm_stat_free(&s->rcu_head);
232 free_percpu(stats->last);
233 mutex_destroy(&stats->mutex);
236 static void dm_stats_recalc_precise_timestamps(struct dm_stats *stats)
239 struct dm_stat *tmp_s;
240 bool precise_timestamps = false;
242 list_for_each(l, &stats->list) {
243 tmp_s = container_of(l, struct dm_stat, list_entry);
244 if (tmp_s->stat_flags & STAT_PRECISE_TIMESTAMPS) {
245 precise_timestamps = true;
249 stats->precise_timestamps = precise_timestamps;
252 static int dm_stats_create(struct dm_stats *stats, sector_t start, sector_t end,
253 sector_t step, unsigned stat_flags,
254 unsigned n_histogram_entries,
255 unsigned long long *histogram_boundaries,
256 const char *program_id, const char *aux_data,
257 void (*suspend_callback)(struct mapped_device *),
258 void (*resume_callback)(struct mapped_device *),
259 struct mapped_device *md)
262 struct dm_stat *s, *tmp_s;
265 size_t shared_alloc_size;
266 size_t percpu_alloc_size;
267 size_t histogram_alloc_size;
268 struct dm_stat_percpu *p;
273 if (end < start || !step)
276 n_entries = end - start;
277 if (dm_sector_div64(n_entries, step))
280 if (n_entries != (size_t)n_entries || !(size_t)(n_entries + 1))
283 shared_alloc_size = struct_size(s, stat_shared, n_entries);
284 if ((shared_alloc_size - sizeof(struct dm_stat)) / sizeof(struct dm_stat_shared) != n_entries)
287 percpu_alloc_size = (size_t)n_entries * sizeof(struct dm_stat_percpu);
288 if (percpu_alloc_size / sizeof(struct dm_stat_percpu) != n_entries)
291 histogram_alloc_size = (n_histogram_entries + 1) * (size_t)n_entries * sizeof(unsigned long long);
292 if (histogram_alloc_size / (n_histogram_entries + 1) != (size_t)n_entries * sizeof(unsigned long long))
295 if (!check_shared_memory(shared_alloc_size + histogram_alloc_size +
296 num_possible_cpus() * (percpu_alloc_size + histogram_alloc_size)))
299 s = dm_kvzalloc(shared_alloc_size, NUMA_NO_NODE);
303 s->stat_flags = stat_flags;
304 s->n_entries = n_entries;
308 s->shared_alloc_size = shared_alloc_size;
309 s->percpu_alloc_size = percpu_alloc_size;
310 s->histogram_alloc_size = histogram_alloc_size;
312 s->n_histogram_entries = n_histogram_entries;
313 s->histogram_boundaries = kmemdup(histogram_boundaries,
314 s->n_histogram_entries * sizeof(unsigned long long), GFP_KERNEL);
315 if (!s->histogram_boundaries) {
320 s->program_id = kstrdup(program_id, GFP_KERNEL);
321 if (!s->program_id) {
325 s->aux_data = kstrdup(aux_data, GFP_KERNEL);
331 for (ni = 0; ni < n_entries; ni++) {
332 atomic_set(&s->stat_shared[ni].in_flight[READ], 0);
333 atomic_set(&s->stat_shared[ni].in_flight[WRITE], 0);
337 if (s->n_histogram_entries) {
338 unsigned long long *hi;
339 hi = dm_kvzalloc(s->histogram_alloc_size, NUMA_NO_NODE);
344 for (ni = 0; ni < n_entries; ni++) {
345 s->stat_shared[ni].tmp.histogram = hi;
346 hi += s->n_histogram_entries + 1;
351 for_each_possible_cpu(cpu) {
352 p = dm_kvzalloc(percpu_alloc_size, cpu_to_node(cpu));
357 s->stat_percpu[cpu] = p;
358 if (s->n_histogram_entries) {
359 unsigned long long *hi;
360 hi = dm_kvzalloc(s->histogram_alloc_size, cpu_to_node(cpu));
365 for (ni = 0; ni < n_entries; ni++) {
366 p[ni].histogram = hi;
367 hi += s->n_histogram_entries + 1;
374 * Suspend/resume to make sure there is no i/o in flight,
375 * so that newly created statistics will be exact.
377 * (note: we couldn't suspend earlier because we must not
378 * allocate memory while suspended)
380 suspend_callback(md);
382 mutex_lock(&stats->mutex);
384 list_for_each(l, &stats->list) {
385 tmp_s = container_of(l, struct dm_stat, list_entry);
386 if (WARN_ON(tmp_s->id < s->id)) {
388 goto out_unlock_resume;
390 if (tmp_s->id > s->id)
392 if (unlikely(s->id == INT_MAX)) {
394 goto out_unlock_resume;
399 list_add_tail_rcu(&s->list_entry, l);
401 dm_stats_recalc_precise_timestamps(stats);
403 if (!static_key_enabled(&stats_enabled.key))
404 static_branch_enable(&stats_enabled);
406 mutex_unlock(&stats->mutex);
413 mutex_unlock(&stats->mutex);
416 dm_stat_free(&s->rcu_head);
420 static struct dm_stat *__dm_stats_find(struct dm_stats *stats, int id)
424 list_for_each_entry(s, &stats->list, list_entry) {
434 static int dm_stats_delete(struct dm_stats *stats, int id)
439 mutex_lock(&stats->mutex);
441 s = __dm_stats_find(stats, id);
443 mutex_unlock(&stats->mutex);
447 list_del_rcu(&s->list_entry);
449 dm_stats_recalc_precise_timestamps(stats);
451 mutex_unlock(&stats->mutex);
454 * vfree can't be called from RCU callback
456 for_each_possible_cpu(cpu)
457 if (is_vmalloc_addr(s->stat_percpu) ||
458 is_vmalloc_addr(s->stat_percpu[cpu][0].histogram))
460 if (is_vmalloc_addr(s) ||
461 is_vmalloc_addr(s->stat_shared[0].tmp.histogram)) {
463 synchronize_rcu_expedited();
464 dm_stat_free(&s->rcu_head);
466 WRITE_ONCE(dm_stat_need_rcu_barrier, 1);
467 call_rcu(&s->rcu_head, dm_stat_free);
472 static int dm_stats_list(struct dm_stats *stats, const char *program,
473 char *result, unsigned maxlen)
481 * <region_id>: <start_sector>+<length> <step> <program_id> <aux_data>
484 mutex_lock(&stats->mutex);
485 list_for_each_entry(s, &stats->list, list_entry) {
486 if (!program || !strcmp(program, s->program_id)) {
487 len = s->end - s->start;
488 DMEMIT("%d: %llu+%llu %llu %s %s", s->id,
489 (unsigned long long)s->start,
490 (unsigned long long)len,
491 (unsigned long long)s->step,
494 if (s->stat_flags & STAT_PRECISE_TIMESTAMPS)
495 DMEMIT(" precise_timestamps");
496 if (s->n_histogram_entries) {
498 DMEMIT(" histogram:");
499 for (i = 0; i < s->n_histogram_entries; i++) {
502 DMEMIT("%llu", s->histogram_boundaries[i]);
509 mutex_unlock(&stats->mutex);
514 static void dm_stat_round(struct dm_stat *s, struct dm_stat_shared *shared,
515 struct dm_stat_percpu *p)
518 * This is racy, but so is part_round_stats_single.
520 unsigned long long now, difference;
521 unsigned in_flight_read, in_flight_write;
523 if (likely(!(s->stat_flags & STAT_PRECISE_TIMESTAMPS)))
526 now = ktime_to_ns(ktime_get());
528 difference = now - shared->stamp;
532 in_flight_read = (unsigned)atomic_read(&shared->in_flight[READ]);
533 in_flight_write = (unsigned)atomic_read(&shared->in_flight[WRITE]);
535 p->io_ticks[READ] += difference;
537 p->io_ticks[WRITE] += difference;
538 if (in_flight_read + in_flight_write) {
539 p->io_ticks_total += difference;
540 p->time_in_queue += (in_flight_read + in_flight_write) * difference;
545 static void dm_stat_for_entry(struct dm_stat *s, size_t entry,
546 int idx, sector_t len,
547 struct dm_stats_aux *stats_aux, bool end,
548 unsigned long duration_jiffies)
550 struct dm_stat_shared *shared = &s->stat_shared[entry];
551 struct dm_stat_percpu *p;
554 * For strict correctness we should use local_irq_save/restore
555 * instead of preempt_disable/enable.
557 * preempt_disable/enable is racy if the driver finishes bios
558 * from non-interrupt context as well as from interrupt context
559 * or from more different interrupts.
561 * On 64-bit architectures the race only results in not counting some
562 * events, so it is acceptable. On 32-bit architectures the race could
563 * cause the counter going off by 2^32, so we need to do proper locking
566 * part_stat_lock()/part_stat_unlock() have this race too.
568 #if BITS_PER_LONG == 32
570 local_irq_save(flags);
574 p = &s->stat_percpu[smp_processor_id()][entry];
577 dm_stat_round(s, shared, p);
578 atomic_inc(&shared->in_flight[idx]);
580 unsigned long long duration;
581 dm_stat_round(s, shared, p);
582 atomic_dec(&shared->in_flight[idx]);
583 p->sectors[idx] += len;
585 p->merges[idx] += stats_aux->merged;
586 if (!(s->stat_flags & STAT_PRECISE_TIMESTAMPS)) {
587 p->ticks[idx] += duration_jiffies;
588 duration = jiffies_to_msecs(duration_jiffies);
590 p->ticks[idx] += stats_aux->duration_ns;
591 duration = stats_aux->duration_ns;
593 if (s->n_histogram_entries) {
594 unsigned lo = 0, hi = s->n_histogram_entries + 1;
595 while (lo + 1 < hi) {
596 unsigned mid = (lo + hi) / 2;
597 if (s->histogram_boundaries[mid - 1] > duration) {
608 #if BITS_PER_LONG == 32
609 local_irq_restore(flags);
615 static void __dm_stat_bio(struct dm_stat *s, int bi_rw,
616 sector_t bi_sector, sector_t end_sector,
617 bool end, unsigned long duration_jiffies,
618 struct dm_stats_aux *stats_aux)
620 sector_t rel_sector, offset, todo, fragment_len;
623 if (end_sector <= s->start || bi_sector >= s->end)
625 if (unlikely(bi_sector < s->start)) {
627 todo = end_sector - s->start;
629 rel_sector = bi_sector - s->start;
630 todo = end_sector - bi_sector;
632 if (unlikely(end_sector > s->end))
633 todo -= (end_sector - s->end);
635 offset = dm_sector_div64(rel_sector, s->step);
638 if (WARN_ON_ONCE(entry >= s->n_entries)) {
639 DMCRIT("Invalid area access in region id %d", s->id);
643 if (fragment_len > s->step - offset)
644 fragment_len = s->step - offset;
645 dm_stat_for_entry(s, entry, bi_rw, fragment_len,
646 stats_aux, end, duration_jiffies);
647 todo -= fragment_len;
650 } while (unlikely(todo != 0));
653 void dm_stats_account_io(struct dm_stats *stats, unsigned long bi_rw,
654 sector_t bi_sector, unsigned bi_sectors, bool end,
655 unsigned long start_time,
656 struct dm_stats_aux *stats_aux)
660 struct dm_stats_last_position *last;
661 bool got_precise_time;
662 unsigned long duration_jiffies = 0;
664 if (unlikely(!bi_sectors))
667 end_sector = bi_sector + bi_sectors;
671 * A race condition can at worst result in the merged flag being
672 * misrepresented, so we don't have to disable preemption here.
674 last = raw_cpu_ptr(stats->last);
676 (bi_sector == (READ_ONCE(last->last_sector) &&
678 (READ_ONCE(last->last_rw) == WRITE))
680 WRITE_ONCE(last->last_sector, end_sector);
681 WRITE_ONCE(last->last_rw, bi_rw);
683 duration_jiffies = jiffies - start_time;
687 got_precise_time = false;
688 list_for_each_entry_rcu(s, &stats->list, list_entry) {
689 if (s->stat_flags & STAT_PRECISE_TIMESTAMPS && !got_precise_time) {
690 /* start (!end) duration_ns is set by DM core's alloc_io() */
692 stats_aux->duration_ns = ktime_to_ns(ktime_get()) - stats_aux->duration_ns;
693 got_precise_time = true;
695 __dm_stat_bio(s, bi_rw, bi_sector, end_sector, end, duration_jiffies, stats_aux);
701 static void __dm_stat_init_temporary_percpu_totals(struct dm_stat_shared *shared,
702 struct dm_stat *s, size_t x)
705 struct dm_stat_percpu *p;
708 p = &s->stat_percpu[smp_processor_id()][x];
709 dm_stat_round(s, shared, p);
712 shared->tmp.sectors[READ] = 0;
713 shared->tmp.sectors[WRITE] = 0;
714 shared->tmp.ios[READ] = 0;
715 shared->tmp.ios[WRITE] = 0;
716 shared->tmp.merges[READ] = 0;
717 shared->tmp.merges[WRITE] = 0;
718 shared->tmp.ticks[READ] = 0;
719 shared->tmp.ticks[WRITE] = 0;
720 shared->tmp.io_ticks[READ] = 0;
721 shared->tmp.io_ticks[WRITE] = 0;
722 shared->tmp.io_ticks_total = 0;
723 shared->tmp.time_in_queue = 0;
725 if (s->n_histogram_entries)
726 memset(shared->tmp.histogram, 0, (s->n_histogram_entries + 1) * sizeof(unsigned long long));
728 for_each_possible_cpu(cpu) {
729 p = &s->stat_percpu[cpu][x];
730 shared->tmp.sectors[READ] += READ_ONCE(p->sectors[READ]);
731 shared->tmp.sectors[WRITE] += READ_ONCE(p->sectors[WRITE]);
732 shared->tmp.ios[READ] += READ_ONCE(p->ios[READ]);
733 shared->tmp.ios[WRITE] += READ_ONCE(p->ios[WRITE]);
734 shared->tmp.merges[READ] += READ_ONCE(p->merges[READ]);
735 shared->tmp.merges[WRITE] += READ_ONCE(p->merges[WRITE]);
736 shared->tmp.ticks[READ] += READ_ONCE(p->ticks[READ]);
737 shared->tmp.ticks[WRITE] += READ_ONCE(p->ticks[WRITE]);
738 shared->tmp.io_ticks[READ] += READ_ONCE(p->io_ticks[READ]);
739 shared->tmp.io_ticks[WRITE] += READ_ONCE(p->io_ticks[WRITE]);
740 shared->tmp.io_ticks_total += READ_ONCE(p->io_ticks_total);
741 shared->tmp.time_in_queue += READ_ONCE(p->time_in_queue);
742 if (s->n_histogram_entries) {
744 for (i = 0; i < s->n_histogram_entries + 1; i++)
745 shared->tmp.histogram[i] += READ_ONCE(p->histogram[i]);
750 static void __dm_stat_clear(struct dm_stat *s, size_t idx_start, size_t idx_end,
751 bool init_tmp_percpu_totals)
754 struct dm_stat_shared *shared;
755 struct dm_stat_percpu *p;
757 for (x = idx_start; x < idx_end; x++) {
758 shared = &s->stat_shared[x];
759 if (init_tmp_percpu_totals)
760 __dm_stat_init_temporary_percpu_totals(shared, s, x);
762 p = &s->stat_percpu[smp_processor_id()][x];
763 p->sectors[READ] -= shared->tmp.sectors[READ];
764 p->sectors[WRITE] -= shared->tmp.sectors[WRITE];
765 p->ios[READ] -= shared->tmp.ios[READ];
766 p->ios[WRITE] -= shared->tmp.ios[WRITE];
767 p->merges[READ] -= shared->tmp.merges[READ];
768 p->merges[WRITE] -= shared->tmp.merges[WRITE];
769 p->ticks[READ] -= shared->tmp.ticks[READ];
770 p->ticks[WRITE] -= shared->tmp.ticks[WRITE];
771 p->io_ticks[READ] -= shared->tmp.io_ticks[READ];
772 p->io_ticks[WRITE] -= shared->tmp.io_ticks[WRITE];
773 p->io_ticks_total -= shared->tmp.io_ticks_total;
774 p->time_in_queue -= shared->tmp.time_in_queue;
776 if (s->n_histogram_entries) {
778 for (i = 0; i < s->n_histogram_entries + 1; i++) {
780 p = &s->stat_percpu[smp_processor_id()][x];
781 p->histogram[i] -= shared->tmp.histogram[i];
789 static int dm_stats_clear(struct dm_stats *stats, int id)
793 mutex_lock(&stats->mutex);
795 s = __dm_stats_find(stats, id);
797 mutex_unlock(&stats->mutex);
801 __dm_stat_clear(s, 0, s->n_entries, true);
803 mutex_unlock(&stats->mutex);
809 * This is like jiffies_to_msec, but works for 64-bit values.
811 static unsigned long long dm_jiffies_to_msec64(struct dm_stat *s, unsigned long long j)
813 unsigned long long result;
816 if (s->stat_flags & STAT_PRECISE_TIMESTAMPS)
821 result = jiffies_to_msecs(j & 0x3fffff);
823 mult = jiffies_to_msecs(1 << 22);
824 result += (unsigned long long)mult * (unsigned long long)jiffies_to_msecs((j >> 22) & 0x3fffff);
827 result += (unsigned long long)mult * (unsigned long long)mult * (unsigned long long)jiffies_to_msecs(j >> 44);
832 static int dm_stats_print(struct dm_stats *stats, int id,
833 size_t idx_start, size_t idx_len,
834 bool clear, char *result, unsigned maxlen)
839 sector_t start, end, step;
841 struct dm_stat_shared *shared;
845 * <start_sector>+<length> counters
848 mutex_lock(&stats->mutex);
850 s = __dm_stats_find(stats, id);
852 mutex_unlock(&stats->mutex);
856 idx_end = idx_start + idx_len;
857 if (idx_end < idx_start ||
858 idx_end > s->n_entries)
859 idx_end = s->n_entries;
861 if (idx_start > idx_end)
865 start = s->start + (step * idx_start);
867 for (x = idx_start; x < idx_end; x++, start = end) {
868 shared = &s->stat_shared[x];
870 if (unlikely(end > s->end))
873 __dm_stat_init_temporary_percpu_totals(shared, s, x);
875 DMEMIT("%llu+%llu %llu %llu %llu %llu %llu %llu %llu %llu %d %llu %llu %llu %llu",
876 (unsigned long long)start,
877 (unsigned long long)step,
878 shared->tmp.ios[READ],
879 shared->tmp.merges[READ],
880 shared->tmp.sectors[READ],
881 dm_jiffies_to_msec64(s, shared->tmp.ticks[READ]),
882 shared->tmp.ios[WRITE],
883 shared->tmp.merges[WRITE],
884 shared->tmp.sectors[WRITE],
885 dm_jiffies_to_msec64(s, shared->tmp.ticks[WRITE]),
886 dm_stat_in_flight(shared),
887 dm_jiffies_to_msec64(s, shared->tmp.io_ticks_total),
888 dm_jiffies_to_msec64(s, shared->tmp.time_in_queue),
889 dm_jiffies_to_msec64(s, shared->tmp.io_ticks[READ]),
890 dm_jiffies_to_msec64(s, shared->tmp.io_ticks[WRITE]));
891 if (s->n_histogram_entries) {
893 for (i = 0; i < s->n_histogram_entries + 1; i++) {
894 DMEMIT("%s%llu", !i ? " " : ":", shared->tmp.histogram[i]);
899 if (unlikely(sz + 1 >= maxlen))
900 goto buffer_overflow;
906 __dm_stat_clear(s, idx_start, idx_end, false);
909 mutex_unlock(&stats->mutex);
914 static int dm_stats_set_aux(struct dm_stats *stats, int id, const char *aux_data)
917 const char *new_aux_data;
919 mutex_lock(&stats->mutex);
921 s = __dm_stats_find(stats, id);
923 mutex_unlock(&stats->mutex);
927 new_aux_data = kstrdup(aux_data, GFP_KERNEL);
929 mutex_unlock(&stats->mutex);
934 s->aux_data = new_aux_data;
936 mutex_unlock(&stats->mutex);
941 static int parse_histogram(const char *h, unsigned *n_histogram_entries,
942 unsigned long long **histogram_boundaries)
946 unsigned long long last;
948 *n_histogram_entries = 1;
951 (*n_histogram_entries)++;
953 *histogram_boundaries = kmalloc_array(*n_histogram_entries,
954 sizeof(unsigned long long),
956 if (!*histogram_boundaries)
962 unsigned long long hi;
965 s = sscanf(h, "%llu%c", &hi, &ch);
966 if (!s || (s == 2 && ch != ','))
971 (*histogram_boundaries)[n] = hi;
974 h = strchr(h, ',') + 1;
979 static int message_stats_create(struct mapped_device *md,
980 unsigned argc, char **argv,
981 char *result, unsigned maxlen)
986 unsigned long long start, end, len, step;
988 const char *program_id, *aux_data;
989 unsigned stat_flags = 0;
991 unsigned n_histogram_entries = 0;
992 unsigned long long *histogram_boundaries = NULL;
994 struct dm_arg_set as, as_backup;
996 unsigned feature_args;
1000 * <range> <step> [<extra_parameters> <parameters>] [<program_id> [<aux_data>]]
1008 dm_consume_args(&as, 1);
1010 a = dm_shift_arg(&as);
1011 if (!strcmp(a, "-")) {
1013 len = dm_get_size(md);
1016 } else if (sscanf(a, "%llu+%llu%c", &start, &len, &dummy) != 2 ||
1017 start != (sector_t)start || len != (sector_t)len)
1024 a = dm_shift_arg(&as);
1025 if (sscanf(a, "/%u%c", &divisor, &dummy) == 1) {
1029 if (do_div(step, divisor))
1033 } else if (sscanf(a, "%llu%c", &step, &dummy) != 1 ||
1034 step != (sector_t)step || !step)
1038 a = dm_shift_arg(&as);
1039 if (a && sscanf(a, "%u%c", &feature_args, &dummy) == 1) {
1040 while (feature_args--) {
1041 a = dm_shift_arg(&as);
1044 if (!strcasecmp(a, "precise_timestamps"))
1045 stat_flags |= STAT_PRECISE_TIMESTAMPS;
1046 else if (!strncasecmp(a, "histogram:", 10)) {
1047 if (n_histogram_entries)
1049 if ((r = parse_histogram(a + 10, &n_histogram_entries, &histogram_boundaries)))
1061 a = dm_shift_arg(&as);
1065 a = dm_shift_arg(&as);
1073 * If a buffer overflow happens after we created the region,
1074 * it's too late (the userspace would retry with a larger
1075 * buffer, but the region id that caused the overflow is already
1076 * leaked). So we must detect buffer overflow in advance.
1078 snprintf(result, maxlen, "%d", INT_MAX);
1079 if (dm_message_test_buffer_overflow(result, maxlen)) {
1084 id = dm_stats_create(dm_get_stats(md), start, end, step, stat_flags,
1085 n_histogram_entries, histogram_boundaries, program_id, aux_data,
1086 dm_internal_suspend_fast, dm_internal_resume_fast, md);
1092 snprintf(result, maxlen, "%d", id);
1100 kfree(histogram_boundaries);
1104 static int message_stats_delete(struct mapped_device *md,
1105 unsigned argc, char **argv)
1113 if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
1116 return dm_stats_delete(dm_get_stats(md), id);
1119 static int message_stats_clear(struct mapped_device *md,
1120 unsigned argc, char **argv)
1128 if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
1131 return dm_stats_clear(dm_get_stats(md), id);
1134 static int message_stats_list(struct mapped_device *md,
1135 unsigned argc, char **argv,
1136 char *result, unsigned maxlen)
1139 const char *program = NULL;
1141 if (argc < 1 || argc > 2)
1145 program = kstrdup(argv[1], GFP_KERNEL);
1150 r = dm_stats_list(dm_get_stats(md), program, result, maxlen);
1157 static int message_stats_print(struct mapped_device *md,
1158 unsigned argc, char **argv, bool clear,
1159 char *result, unsigned maxlen)
1163 unsigned long idx_start = 0, idx_len = ULONG_MAX;
1165 if (argc != 2 && argc != 4)
1168 if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
1172 if (strcmp(argv[2], "-") &&
1173 sscanf(argv[2], "%lu%c", &idx_start, &dummy) != 1)
1175 if (strcmp(argv[3], "-") &&
1176 sscanf(argv[3], "%lu%c", &idx_len, &dummy) != 1)
1180 return dm_stats_print(dm_get_stats(md), id, idx_start, idx_len, clear,
1184 static int message_stats_set_aux(struct mapped_device *md,
1185 unsigned argc, char **argv)
1193 if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
1196 return dm_stats_set_aux(dm_get_stats(md), id, argv[2]);
1199 int dm_stats_message(struct mapped_device *md, unsigned argc, char **argv,
1200 char *result, unsigned maxlen)
1204 /* All messages here must start with '@' */
1205 if (!strcasecmp(argv[0], "@stats_create"))
1206 r = message_stats_create(md, argc, argv, result, maxlen);
1207 else if (!strcasecmp(argv[0], "@stats_delete"))
1208 r = message_stats_delete(md, argc, argv);
1209 else if (!strcasecmp(argv[0], "@stats_clear"))
1210 r = message_stats_clear(md, argc, argv);
1211 else if (!strcasecmp(argv[0], "@stats_list"))
1212 r = message_stats_list(md, argc, argv, result, maxlen);
1213 else if (!strcasecmp(argv[0], "@stats_print"))
1214 r = message_stats_print(md, argc, argv, false, result, maxlen);
1215 else if (!strcasecmp(argv[0], "@stats_print_clear"))
1216 r = message_stats_print(md, argc, argv, true, result, maxlen);
1217 else if (!strcasecmp(argv[0], "@stats_set_aux"))
1218 r = message_stats_set_aux(md, argc, argv);
1220 return 2; /* this wasn't a stats message */
1223 DMWARN("Invalid parameters for message %s", argv[0]);
1228 int __init dm_statistics_init(void)
1230 shared_memory_amount = 0;
1231 dm_stat_need_rcu_barrier = 0;
1235 void dm_statistics_exit(void)
1237 if (dm_stat_need_rcu_barrier)
1239 if (WARN_ON(shared_memory_amount))
1240 DMCRIT("shared_memory_amount leaked: %lu", shared_memory_amount);
1243 module_param_named(stats_current_allocated_bytes, shared_memory_amount, ulong, S_IRUGO);
1244 MODULE_PARM_DESC(stats_current_allocated_bytes, "Memory currently used by statistics");