Linux Audio

Check our new training course

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