dm-stats.c 29 KB

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