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
[0];
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
->last
= alloc_percpu(struct dm_stats_last_position
);
199 for_each_possible_cpu(cpu
) {
200 last
= per_cpu_ptr(stats
->last
, cpu
);
201 last
->last_sector
= (sector_t
)ULLONG_MAX
;
202 last
->last_rw
= UINT_MAX
;
206 void dm_stats_cleanup(struct dm_stats
*stats
)
210 struct dm_stat_shared
*shared
;
212 while (!list_empty(&stats
->list
)) {
213 s
= container_of(stats
->list
.next
, struct dm_stat
, list_entry
);
214 list_del(&s
->list_entry
);
215 for (ni
= 0; ni
< s
->n_entries
; ni
++) {
216 shared
= &s
->stat_shared
[ni
];
217 if (WARN_ON(dm_stat_in_flight(shared
))) {
218 DMCRIT("leaked in-flight counter at index %lu "
219 "(start %llu, end %llu, step %llu): reads %d, writes %d",
221 (unsigned long long)s
->start
,
222 (unsigned long long)s
->end
,
223 (unsigned long long)s
->step
,
224 atomic_read(&shared
->in_flight
[READ
]),
225 atomic_read(&shared
->in_flight
[WRITE
]));
228 dm_stat_free(&s
->rcu_head
);
230 free_percpu(stats
->last
);
231 mutex_destroy(&stats
->mutex
);
234 static int dm_stats_create(struct dm_stats
*stats
, sector_t start
, sector_t end
,
235 sector_t step
, unsigned stat_flags
,
236 unsigned n_histogram_entries
,
237 unsigned long long *histogram_boundaries
,
238 const char *program_id
, const char *aux_data
,
239 void (*suspend_callback
)(struct mapped_device
*),
240 void (*resume_callback
)(struct mapped_device
*),
241 struct mapped_device
*md
)
244 struct dm_stat
*s
, *tmp_s
;
247 size_t shared_alloc_size
;
248 size_t percpu_alloc_size
;
249 size_t histogram_alloc_size
;
250 struct dm_stat_percpu
*p
;
255 if (end
< start
|| !step
)
258 n_entries
= end
- start
;
259 if (dm_sector_div64(n_entries
, step
))
262 if (n_entries
!= (size_t)n_entries
|| !(size_t)(n_entries
+ 1))
265 shared_alloc_size
= sizeof(struct dm_stat
) + (size_t)n_entries
* sizeof(struct dm_stat_shared
);
266 if ((shared_alloc_size
- sizeof(struct dm_stat
)) / sizeof(struct dm_stat_shared
) != n_entries
)
269 percpu_alloc_size
= (size_t)n_entries
* sizeof(struct dm_stat_percpu
);
270 if (percpu_alloc_size
/ sizeof(struct dm_stat_percpu
) != n_entries
)
273 histogram_alloc_size
= (n_histogram_entries
+ 1) * (size_t)n_entries
* sizeof(unsigned long long);
274 if (histogram_alloc_size
/ (n_histogram_entries
+ 1) != (size_t)n_entries
* sizeof(unsigned long long))
277 if (!check_shared_memory(shared_alloc_size
+ histogram_alloc_size
+
278 num_possible_cpus() * (percpu_alloc_size
+ histogram_alloc_size
)))
281 s
= dm_kvzalloc(shared_alloc_size
, NUMA_NO_NODE
);
285 s
->stat_flags
= stat_flags
;
286 s
->n_entries
= n_entries
;
290 s
->shared_alloc_size
= shared_alloc_size
;
291 s
->percpu_alloc_size
= percpu_alloc_size
;
292 s
->histogram_alloc_size
= histogram_alloc_size
;
294 s
->n_histogram_entries
= n_histogram_entries
;
295 s
->histogram_boundaries
= kmemdup(histogram_boundaries
,
296 s
->n_histogram_entries
* sizeof(unsigned long long), GFP_KERNEL
);
297 if (!s
->histogram_boundaries
) {
302 s
->program_id
= kstrdup(program_id
, GFP_KERNEL
);
303 if (!s
->program_id
) {
307 s
->aux_data
= kstrdup(aux_data
, GFP_KERNEL
);
313 for (ni
= 0; ni
< n_entries
; ni
++) {
314 atomic_set(&s
->stat_shared
[ni
].in_flight
[READ
], 0);
315 atomic_set(&s
->stat_shared
[ni
].in_flight
[WRITE
], 0);
318 if (s
->n_histogram_entries
) {
319 unsigned long long *hi
;
320 hi
= dm_kvzalloc(s
->histogram_alloc_size
, NUMA_NO_NODE
);
325 for (ni
= 0; ni
< n_entries
; ni
++) {
326 s
->stat_shared
[ni
].tmp
.histogram
= hi
;
327 hi
+= s
->n_histogram_entries
+ 1;
331 for_each_possible_cpu(cpu
) {
332 p
= dm_kvzalloc(percpu_alloc_size
, cpu_to_node(cpu
));
337 s
->stat_percpu
[cpu
] = p
;
338 if (s
->n_histogram_entries
) {
339 unsigned long long *hi
;
340 hi
= dm_kvzalloc(s
->histogram_alloc_size
, cpu_to_node(cpu
));
345 for (ni
= 0; ni
< n_entries
; ni
++) {
346 p
[ni
].histogram
= hi
;
347 hi
+= s
->n_histogram_entries
+ 1;
353 * Suspend/resume to make sure there is no i/o in flight,
354 * so that newly created statistics will be exact.
356 * (note: we couldn't suspend earlier because we must not
357 * allocate memory while suspended)
359 suspend_callback(md
);
361 mutex_lock(&stats
->mutex
);
363 list_for_each(l
, &stats
->list
) {
364 tmp_s
= container_of(l
, struct dm_stat
, list_entry
);
365 if (WARN_ON(tmp_s
->id
< s
->id
)) {
367 goto out_unlock_resume
;
369 if (tmp_s
->id
> s
->id
)
371 if (unlikely(s
->id
== INT_MAX
)) {
373 goto out_unlock_resume
;
378 list_add_tail_rcu(&s
->list_entry
, l
);
379 mutex_unlock(&stats
->mutex
);
386 mutex_unlock(&stats
->mutex
);
389 dm_stat_free(&s
->rcu_head
);
393 static struct dm_stat
*__dm_stats_find(struct dm_stats
*stats
, int id
)
397 list_for_each_entry(s
, &stats
->list
, list_entry
) {
407 static int dm_stats_delete(struct dm_stats
*stats
, int id
)
412 mutex_lock(&stats
->mutex
);
414 s
= __dm_stats_find(stats
, id
);
416 mutex_unlock(&stats
->mutex
);
420 list_del_rcu(&s
->list_entry
);
421 mutex_unlock(&stats
->mutex
);
424 * vfree can't be called from RCU callback
426 for_each_possible_cpu(cpu
)
427 if (is_vmalloc_addr(s
->stat_percpu
) ||
428 is_vmalloc_addr(s
->stat_percpu
[cpu
][0].histogram
))
430 if (is_vmalloc_addr(s
) ||
431 is_vmalloc_addr(s
->stat_shared
[0].tmp
.histogram
)) {
433 synchronize_rcu_expedited();
434 dm_stat_free(&s
->rcu_head
);
436 WRITE_ONCE(dm_stat_need_rcu_barrier
, 1);
437 call_rcu(&s
->rcu_head
, dm_stat_free
);
442 static int dm_stats_list(struct dm_stats
*stats
, const char *program
,
443 char *result
, unsigned maxlen
)
451 * <region_id>: <start_sector>+<length> <step> <program_id> <aux_data>
454 mutex_lock(&stats
->mutex
);
455 list_for_each_entry(s
, &stats
->list
, list_entry
) {
456 if (!program
|| !strcmp(program
, s
->program_id
)) {
457 len
= s
->end
- s
->start
;
458 DMEMIT("%d: %llu+%llu %llu %s %s", s
->id
,
459 (unsigned long long)s
->start
,
460 (unsigned long long)len
,
461 (unsigned long long)s
->step
,
464 if (s
->stat_flags
& STAT_PRECISE_TIMESTAMPS
)
465 DMEMIT(" precise_timestamps");
466 if (s
->n_histogram_entries
) {
468 DMEMIT(" histogram:");
469 for (i
= 0; i
< s
->n_histogram_entries
; i
++) {
472 DMEMIT("%llu", s
->histogram_boundaries
[i
]);
478 mutex_unlock(&stats
->mutex
);
483 static void dm_stat_round(struct dm_stat
*s
, struct dm_stat_shared
*shared
,
484 struct dm_stat_percpu
*p
)
487 * This is racy, but so is part_round_stats_single.
489 unsigned long long now
, difference
;
490 unsigned in_flight_read
, in_flight_write
;
492 if (likely(!(s
->stat_flags
& STAT_PRECISE_TIMESTAMPS
)))
495 now
= ktime_to_ns(ktime_get());
497 difference
= now
- shared
->stamp
;
501 in_flight_read
= (unsigned)atomic_read(&shared
->in_flight
[READ
]);
502 in_flight_write
= (unsigned)atomic_read(&shared
->in_flight
[WRITE
]);
504 p
->io_ticks
[READ
] += difference
;
506 p
->io_ticks
[WRITE
] += difference
;
507 if (in_flight_read
+ in_flight_write
) {
508 p
->io_ticks_total
+= difference
;
509 p
->time_in_queue
+= (in_flight_read
+ in_flight_write
) * difference
;
514 static void dm_stat_for_entry(struct dm_stat
*s
, size_t entry
,
515 int idx
, sector_t len
,
516 struct dm_stats_aux
*stats_aux
, bool end
,
517 unsigned long duration_jiffies
)
519 struct dm_stat_shared
*shared
= &s
->stat_shared
[entry
];
520 struct dm_stat_percpu
*p
;
523 * For strict correctness we should use local_irq_save/restore
524 * instead of preempt_disable/enable.
526 * preempt_disable/enable is racy if the driver finishes bios
527 * from non-interrupt context as well as from interrupt context
528 * or from more different interrupts.
530 * On 64-bit architectures the race only results in not counting some
531 * events, so it is acceptable. On 32-bit architectures the race could
532 * cause the counter going off by 2^32, so we need to do proper locking
535 * part_stat_lock()/part_stat_unlock() have this race too.
537 #if BITS_PER_LONG == 32
539 local_irq_save(flags
);
543 p
= &s
->stat_percpu
[smp_processor_id()][entry
];
546 dm_stat_round(s
, shared
, p
);
547 atomic_inc(&shared
->in_flight
[idx
]);
549 unsigned long long duration
;
550 dm_stat_round(s
, shared
, p
);
551 atomic_dec(&shared
->in_flight
[idx
]);
552 p
->sectors
[idx
] += len
;
554 p
->merges
[idx
] += stats_aux
->merged
;
555 if (!(s
->stat_flags
& STAT_PRECISE_TIMESTAMPS
)) {
556 p
->ticks
[idx
] += duration_jiffies
;
557 duration
= jiffies_to_msecs(duration_jiffies
);
559 p
->ticks
[idx
] += stats_aux
->duration_ns
;
560 duration
= stats_aux
->duration_ns
;
562 if (s
->n_histogram_entries
) {
563 unsigned lo
= 0, hi
= s
->n_histogram_entries
+ 1;
564 while (lo
+ 1 < hi
) {
565 unsigned mid
= (lo
+ hi
) / 2;
566 if (s
->histogram_boundaries
[mid
- 1] > duration
) {
577 #if BITS_PER_LONG == 32
578 local_irq_restore(flags
);
584 static void __dm_stat_bio(struct dm_stat
*s
, int bi_rw
,
585 sector_t bi_sector
, sector_t end_sector
,
586 bool end
, unsigned long duration_jiffies
,
587 struct dm_stats_aux
*stats_aux
)
589 sector_t rel_sector
, offset
, todo
, fragment_len
;
592 if (end_sector
<= s
->start
|| bi_sector
>= s
->end
)
594 if (unlikely(bi_sector
< s
->start
)) {
596 todo
= end_sector
- s
->start
;
598 rel_sector
= bi_sector
- s
->start
;
599 todo
= end_sector
- bi_sector
;
601 if (unlikely(end_sector
> s
->end
))
602 todo
-= (end_sector
- s
->end
);
604 offset
= dm_sector_div64(rel_sector
, s
->step
);
607 if (WARN_ON_ONCE(entry
>= s
->n_entries
)) {
608 DMCRIT("Invalid area access in region id %d", s
->id
);
612 if (fragment_len
> s
->step
- offset
)
613 fragment_len
= s
->step
- offset
;
614 dm_stat_for_entry(s
, entry
, bi_rw
, fragment_len
,
615 stats_aux
, end
, duration_jiffies
);
616 todo
-= fragment_len
;
619 } while (unlikely(todo
!= 0));
622 void dm_stats_account_io(struct dm_stats
*stats
, unsigned long bi_rw
,
623 sector_t bi_sector
, unsigned bi_sectors
, bool end
,
624 unsigned long duration_jiffies
,
625 struct dm_stats_aux
*stats_aux
)
629 struct dm_stats_last_position
*last
;
630 bool got_precise_time
;
632 if (unlikely(!bi_sectors
))
635 end_sector
= bi_sector
+ bi_sectors
;
639 * A race condition can at worst result in the merged flag being
640 * misrepresented, so we don't have to disable preemption here.
642 last
= raw_cpu_ptr(stats
->last
);
644 (bi_sector
== (READ_ONCE(last
->last_sector
) &&
646 (READ_ONCE(last
->last_rw
) == WRITE
))
648 WRITE_ONCE(last
->last_sector
, end_sector
);
649 WRITE_ONCE(last
->last_rw
, bi_rw
);
654 got_precise_time
= false;
655 list_for_each_entry_rcu(s
, &stats
->list
, list_entry
) {
656 if (s
->stat_flags
& STAT_PRECISE_TIMESTAMPS
&& !got_precise_time
) {
658 stats_aux
->duration_ns
= ktime_to_ns(ktime_get());
660 stats_aux
->duration_ns
= ktime_to_ns(ktime_get()) - stats_aux
->duration_ns
;
661 got_precise_time
= true;
663 __dm_stat_bio(s
, bi_rw
, bi_sector
, end_sector
, end
, duration_jiffies
, stats_aux
);
669 static void __dm_stat_init_temporary_percpu_totals(struct dm_stat_shared
*shared
,
670 struct dm_stat
*s
, size_t x
)
673 struct dm_stat_percpu
*p
;
676 p
= &s
->stat_percpu
[smp_processor_id()][x
];
677 dm_stat_round(s
, shared
, p
);
680 shared
->tmp
.sectors
[READ
] = 0;
681 shared
->tmp
.sectors
[WRITE
] = 0;
682 shared
->tmp
.ios
[READ
] = 0;
683 shared
->tmp
.ios
[WRITE
] = 0;
684 shared
->tmp
.merges
[READ
] = 0;
685 shared
->tmp
.merges
[WRITE
] = 0;
686 shared
->tmp
.ticks
[READ
] = 0;
687 shared
->tmp
.ticks
[WRITE
] = 0;
688 shared
->tmp
.io_ticks
[READ
] = 0;
689 shared
->tmp
.io_ticks
[WRITE
] = 0;
690 shared
->tmp
.io_ticks_total
= 0;
691 shared
->tmp
.time_in_queue
= 0;
693 if (s
->n_histogram_entries
)
694 memset(shared
->tmp
.histogram
, 0, (s
->n_histogram_entries
+ 1) * sizeof(unsigned long long));
696 for_each_possible_cpu(cpu
) {
697 p
= &s
->stat_percpu
[cpu
][x
];
698 shared
->tmp
.sectors
[READ
] += READ_ONCE(p
->sectors
[READ
]);
699 shared
->tmp
.sectors
[WRITE
] += READ_ONCE(p
->sectors
[WRITE
]);
700 shared
->tmp
.ios
[READ
] += READ_ONCE(p
->ios
[READ
]);
701 shared
->tmp
.ios
[WRITE
] += READ_ONCE(p
->ios
[WRITE
]);
702 shared
->tmp
.merges
[READ
] += READ_ONCE(p
->merges
[READ
]);
703 shared
->tmp
.merges
[WRITE
] += READ_ONCE(p
->merges
[WRITE
]);
704 shared
->tmp
.ticks
[READ
] += READ_ONCE(p
->ticks
[READ
]);
705 shared
->tmp
.ticks
[WRITE
] += READ_ONCE(p
->ticks
[WRITE
]);
706 shared
->tmp
.io_ticks
[READ
] += READ_ONCE(p
->io_ticks
[READ
]);
707 shared
->tmp
.io_ticks
[WRITE
] += READ_ONCE(p
->io_ticks
[WRITE
]);
708 shared
->tmp
.io_ticks_total
+= READ_ONCE(p
->io_ticks_total
);
709 shared
->tmp
.time_in_queue
+= READ_ONCE(p
->time_in_queue
);
710 if (s
->n_histogram_entries
) {
712 for (i
= 0; i
< s
->n_histogram_entries
+ 1; i
++)
713 shared
->tmp
.histogram
[i
] += READ_ONCE(p
->histogram
[i
]);
718 static void __dm_stat_clear(struct dm_stat
*s
, size_t idx_start
, size_t idx_end
,
719 bool init_tmp_percpu_totals
)
722 struct dm_stat_shared
*shared
;
723 struct dm_stat_percpu
*p
;
725 for (x
= idx_start
; x
< idx_end
; x
++) {
726 shared
= &s
->stat_shared
[x
];
727 if (init_tmp_percpu_totals
)
728 __dm_stat_init_temporary_percpu_totals(shared
, s
, x
);
730 p
= &s
->stat_percpu
[smp_processor_id()][x
];
731 p
->sectors
[READ
] -= shared
->tmp
.sectors
[READ
];
732 p
->sectors
[WRITE
] -= shared
->tmp
.sectors
[WRITE
];
733 p
->ios
[READ
] -= shared
->tmp
.ios
[READ
];
734 p
->ios
[WRITE
] -= shared
->tmp
.ios
[WRITE
];
735 p
->merges
[READ
] -= shared
->tmp
.merges
[READ
];
736 p
->merges
[WRITE
] -= shared
->tmp
.merges
[WRITE
];
737 p
->ticks
[READ
] -= shared
->tmp
.ticks
[READ
];
738 p
->ticks
[WRITE
] -= shared
->tmp
.ticks
[WRITE
];
739 p
->io_ticks
[READ
] -= shared
->tmp
.io_ticks
[READ
];
740 p
->io_ticks
[WRITE
] -= shared
->tmp
.io_ticks
[WRITE
];
741 p
->io_ticks_total
-= shared
->tmp
.io_ticks_total
;
742 p
->time_in_queue
-= shared
->tmp
.time_in_queue
;
744 if (s
->n_histogram_entries
) {
746 for (i
= 0; i
< s
->n_histogram_entries
+ 1; i
++) {
748 p
= &s
->stat_percpu
[smp_processor_id()][x
];
749 p
->histogram
[i
] -= shared
->tmp
.histogram
[i
];
756 static int dm_stats_clear(struct dm_stats
*stats
, int id
)
760 mutex_lock(&stats
->mutex
);
762 s
= __dm_stats_find(stats
, id
);
764 mutex_unlock(&stats
->mutex
);
768 __dm_stat_clear(s
, 0, s
->n_entries
, true);
770 mutex_unlock(&stats
->mutex
);
776 * This is like jiffies_to_msec, but works for 64-bit values.
778 static unsigned long long dm_jiffies_to_msec64(struct dm_stat
*s
, unsigned long long j
)
780 unsigned long long result
;
783 if (s
->stat_flags
& STAT_PRECISE_TIMESTAMPS
)
788 result
= jiffies_to_msecs(j
& 0x3fffff);
790 mult
= jiffies_to_msecs(1 << 22);
791 result
+= (unsigned long long)mult
* (unsigned long long)jiffies_to_msecs((j
>> 22) & 0x3fffff);
794 result
+= (unsigned long long)mult
* (unsigned long long)mult
* (unsigned long long)jiffies_to_msecs(j
>> 44);
799 static int dm_stats_print(struct dm_stats
*stats
, int id
,
800 size_t idx_start
, size_t idx_len
,
801 bool clear
, char *result
, unsigned maxlen
)
806 sector_t start
, end
, step
;
808 struct dm_stat_shared
*shared
;
812 * <start_sector>+<length> counters
815 mutex_lock(&stats
->mutex
);
817 s
= __dm_stats_find(stats
, id
);
819 mutex_unlock(&stats
->mutex
);
823 idx_end
= idx_start
+ idx_len
;
824 if (idx_end
< idx_start
||
825 idx_end
> s
->n_entries
)
826 idx_end
= s
->n_entries
;
828 if (idx_start
> idx_end
)
832 start
= s
->start
+ (step
* idx_start
);
834 for (x
= idx_start
; x
< idx_end
; x
++, start
= end
) {
835 shared
= &s
->stat_shared
[x
];
837 if (unlikely(end
> s
->end
))
840 __dm_stat_init_temporary_percpu_totals(shared
, s
, x
);
842 DMEMIT("%llu+%llu %llu %llu %llu %llu %llu %llu %llu %llu %d %llu %llu %llu %llu",
843 (unsigned long long)start
,
844 (unsigned long long)step
,
845 shared
->tmp
.ios
[READ
],
846 shared
->tmp
.merges
[READ
],
847 shared
->tmp
.sectors
[READ
],
848 dm_jiffies_to_msec64(s
, shared
->tmp
.ticks
[READ
]),
849 shared
->tmp
.ios
[WRITE
],
850 shared
->tmp
.merges
[WRITE
],
851 shared
->tmp
.sectors
[WRITE
],
852 dm_jiffies_to_msec64(s
, shared
->tmp
.ticks
[WRITE
]),
853 dm_stat_in_flight(shared
),
854 dm_jiffies_to_msec64(s
, shared
->tmp
.io_ticks_total
),
855 dm_jiffies_to_msec64(s
, shared
->tmp
.time_in_queue
),
856 dm_jiffies_to_msec64(s
, shared
->tmp
.io_ticks
[READ
]),
857 dm_jiffies_to_msec64(s
, shared
->tmp
.io_ticks
[WRITE
]));
858 if (s
->n_histogram_entries
) {
860 for (i
= 0; i
< s
->n_histogram_entries
+ 1; i
++) {
861 DMEMIT("%s%llu", !i
? " " : ":", shared
->tmp
.histogram
[i
]);
866 if (unlikely(sz
+ 1 >= maxlen
))
867 goto buffer_overflow
;
871 __dm_stat_clear(s
, idx_start
, idx_end
, false);
874 mutex_unlock(&stats
->mutex
);
879 static int dm_stats_set_aux(struct dm_stats
*stats
, int id
, const char *aux_data
)
882 const char *new_aux_data
;
884 mutex_lock(&stats
->mutex
);
886 s
= __dm_stats_find(stats
, id
);
888 mutex_unlock(&stats
->mutex
);
892 new_aux_data
= kstrdup(aux_data
, GFP_KERNEL
);
894 mutex_unlock(&stats
->mutex
);
899 s
->aux_data
= new_aux_data
;
901 mutex_unlock(&stats
->mutex
);
906 static int parse_histogram(const char *h
, unsigned *n_histogram_entries
,
907 unsigned long long **histogram_boundaries
)
911 unsigned long long last
;
913 *n_histogram_entries
= 1;
916 (*n_histogram_entries
)++;
918 *histogram_boundaries
= kmalloc_array(*n_histogram_entries
,
919 sizeof(unsigned long long),
921 if (!*histogram_boundaries
)
927 unsigned long long hi
;
930 s
= sscanf(h
, "%llu%c", &hi
, &ch
);
931 if (!s
|| (s
== 2 && ch
!= ','))
936 (*histogram_boundaries
)[n
] = hi
;
939 h
= strchr(h
, ',') + 1;
944 static int message_stats_create(struct mapped_device
*md
,
945 unsigned argc
, char **argv
,
946 char *result
, unsigned maxlen
)
951 unsigned long long start
, end
, len
, step
;
953 const char *program_id
, *aux_data
;
954 unsigned stat_flags
= 0;
956 unsigned n_histogram_entries
= 0;
957 unsigned long long *histogram_boundaries
= NULL
;
959 struct dm_arg_set as
, as_backup
;
961 unsigned feature_args
;
965 * <range> <step> [<extra_parameters> <parameters>] [<program_id> [<aux_data>]]
973 dm_consume_args(&as
, 1);
975 a
= dm_shift_arg(&as
);
976 if (!strcmp(a
, "-")) {
978 len
= dm_get_size(md
);
981 } else if (sscanf(a
, "%llu+%llu%c", &start
, &len
, &dummy
) != 2 ||
982 start
!= (sector_t
)start
|| len
!= (sector_t
)len
)
989 a
= dm_shift_arg(&as
);
990 if (sscanf(a
, "/%u%c", &divisor
, &dummy
) == 1) {
994 if (do_div(step
, divisor
))
998 } else if (sscanf(a
, "%llu%c", &step
, &dummy
) != 1 ||
999 step
!= (sector_t
)step
|| !step
)
1003 a
= dm_shift_arg(&as
);
1004 if (a
&& sscanf(a
, "%u%c", &feature_args
, &dummy
) == 1) {
1005 while (feature_args
--) {
1006 a
= dm_shift_arg(&as
);
1009 if (!strcasecmp(a
, "precise_timestamps"))
1010 stat_flags
|= STAT_PRECISE_TIMESTAMPS
;
1011 else if (!strncasecmp(a
, "histogram:", 10)) {
1012 if (n_histogram_entries
)
1014 if ((r
= parse_histogram(a
+ 10, &n_histogram_entries
, &histogram_boundaries
)))
1026 a
= dm_shift_arg(&as
);
1030 a
= dm_shift_arg(&as
);
1038 * If a buffer overflow happens after we created the region,
1039 * it's too late (the userspace would retry with a larger
1040 * buffer, but the region id that caused the overflow is already
1041 * leaked). So we must detect buffer overflow in advance.
1043 snprintf(result
, maxlen
, "%d", INT_MAX
);
1044 if (dm_message_test_buffer_overflow(result
, maxlen
)) {
1049 id
= dm_stats_create(dm_get_stats(md
), start
, end
, step
, stat_flags
,
1050 n_histogram_entries
, histogram_boundaries
, program_id
, aux_data
,
1051 dm_internal_suspend_fast
, dm_internal_resume_fast
, md
);
1057 snprintf(result
, maxlen
, "%d", id
);
1065 kfree(histogram_boundaries
);
1069 static int message_stats_delete(struct mapped_device
*md
,
1070 unsigned argc
, char **argv
)
1078 if (sscanf(argv
[1], "%d%c", &id
, &dummy
) != 1 || id
< 0)
1081 return dm_stats_delete(dm_get_stats(md
), id
);
1084 static int message_stats_clear(struct mapped_device
*md
,
1085 unsigned argc
, char **argv
)
1093 if (sscanf(argv
[1], "%d%c", &id
, &dummy
) != 1 || id
< 0)
1096 return dm_stats_clear(dm_get_stats(md
), id
);
1099 static int message_stats_list(struct mapped_device
*md
,
1100 unsigned argc
, char **argv
,
1101 char *result
, unsigned maxlen
)
1104 const char *program
= NULL
;
1106 if (argc
< 1 || argc
> 2)
1110 program
= kstrdup(argv
[1], GFP_KERNEL
);
1115 r
= dm_stats_list(dm_get_stats(md
), program
, result
, maxlen
);
1122 static int message_stats_print(struct mapped_device
*md
,
1123 unsigned argc
, char **argv
, bool clear
,
1124 char *result
, unsigned maxlen
)
1128 unsigned long idx_start
= 0, idx_len
= ULONG_MAX
;
1130 if (argc
!= 2 && argc
!= 4)
1133 if (sscanf(argv
[1], "%d%c", &id
, &dummy
) != 1 || id
< 0)
1137 if (strcmp(argv
[2], "-") &&
1138 sscanf(argv
[2], "%lu%c", &idx_start
, &dummy
) != 1)
1140 if (strcmp(argv
[3], "-") &&
1141 sscanf(argv
[3], "%lu%c", &idx_len
, &dummy
) != 1)
1145 return dm_stats_print(dm_get_stats(md
), id
, idx_start
, idx_len
, clear
,
1149 static int message_stats_set_aux(struct mapped_device
*md
,
1150 unsigned argc
, char **argv
)
1158 if (sscanf(argv
[1], "%d%c", &id
, &dummy
) != 1 || id
< 0)
1161 return dm_stats_set_aux(dm_get_stats(md
), id
, argv
[2]);
1164 int dm_stats_message(struct mapped_device
*md
, unsigned argc
, char **argv
,
1165 char *result
, unsigned maxlen
)
1169 /* All messages here must start with '@' */
1170 if (!strcasecmp(argv
[0], "@stats_create"))
1171 r
= message_stats_create(md
, argc
, argv
, result
, maxlen
);
1172 else if (!strcasecmp(argv
[0], "@stats_delete"))
1173 r
= message_stats_delete(md
, argc
, argv
);
1174 else if (!strcasecmp(argv
[0], "@stats_clear"))
1175 r
= message_stats_clear(md
, argc
, argv
);
1176 else if (!strcasecmp(argv
[0], "@stats_list"))
1177 r
= message_stats_list(md
, argc
, argv
, result
, maxlen
);
1178 else if (!strcasecmp(argv
[0], "@stats_print"))
1179 r
= message_stats_print(md
, argc
, argv
, false, result
, maxlen
);
1180 else if (!strcasecmp(argv
[0], "@stats_print_clear"))
1181 r
= message_stats_print(md
, argc
, argv
, true, result
, maxlen
);
1182 else if (!strcasecmp(argv
[0], "@stats_set_aux"))
1183 r
= message_stats_set_aux(md
, argc
, argv
);
1185 return 2; /* this wasn't a stats message */
1188 DMWARN("Invalid parameters for message %s", argv
[0]);
1193 int __init
dm_statistics_init(void)
1195 shared_memory_amount
= 0;
1196 dm_stat_need_rcu_barrier
= 0;
1200 void dm_statistics_exit(void)
1202 if (dm_stat_need_rcu_barrier
)
1204 if (WARN_ON(shared_memory_amount
))
1205 DMCRIT("shared_memory_amount leaked: %lu", shared_memory_amount
);
1208 module_param_named(stats_current_allocated_bytes
, shared_memory_amount
, ulong
, S_IRUGO
);
1209 MODULE_PARM_DESC(stats_current_allocated_bytes
, "Memory currently used by statistics");