dm-stats.c 29 KB

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