dm-stats.c 29 KB

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