dm-stats.c 30 KB

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