dm-stats.c 30 KB

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