dm-stats.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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_array(*n_histogram_entries,
  818. sizeof(unsigned long long),
  819. GFP_KERNEL);
  820. if (!*histogram_boundaries)
  821. return -ENOMEM;
  822. n = 0;
  823. last = 0;
  824. while (1) {
  825. unsigned long long hi;
  826. int s;
  827. char ch;
  828. s = sscanf(h, "%llu%c", &hi, &ch);
  829. if (!s || (s == 2 && ch != ','))
  830. return -EINVAL;
  831. if (hi <= last)
  832. return -EINVAL;
  833. last = hi;
  834. (*histogram_boundaries)[n] = hi;
  835. if (s == 1)
  836. return 0;
  837. h = strchr(h, ',') + 1;
  838. n++;
  839. }
  840. }
  841. static int message_stats_create(struct mapped_device *md,
  842. unsigned int argc, char **argv,
  843. char *result, unsigned int maxlen)
  844. {
  845. int r;
  846. int id;
  847. char dummy;
  848. unsigned long long start, end, len, step;
  849. unsigned int divisor;
  850. const char *program_id, *aux_data;
  851. unsigned int stat_flags = 0;
  852. unsigned int n_histogram_entries = 0;
  853. unsigned long long *histogram_boundaries = NULL;
  854. struct dm_arg_set as, as_backup;
  855. const char *a;
  856. unsigned int feature_args;
  857. /*
  858. * Input format:
  859. * <range> <step> [<extra_parameters> <parameters>] [<program_id> [<aux_data>]]
  860. */
  861. if (argc < 3)
  862. goto ret_einval;
  863. as.argc = argc;
  864. as.argv = argv;
  865. dm_consume_args(&as, 1);
  866. a = dm_shift_arg(&as);
  867. if (!strcmp(a, "-")) {
  868. start = 0;
  869. len = dm_get_size(md);
  870. if (!len)
  871. len = 1;
  872. } else if (sscanf(a, "%llu+%llu%c", &start, &len, &dummy) != 2 ||
  873. start != (sector_t)start || len != (sector_t)len)
  874. goto ret_einval;
  875. end = start + len;
  876. if (start >= end)
  877. goto ret_einval;
  878. a = dm_shift_arg(&as);
  879. if (sscanf(a, "/%u%c", &divisor, &dummy) == 1) {
  880. if (!divisor)
  881. return -EINVAL;
  882. step = end - start;
  883. if (do_div(step, divisor))
  884. step++;
  885. if (!step)
  886. step = 1;
  887. } else if (sscanf(a, "%llu%c", &step, &dummy) != 1 ||
  888. step != (sector_t)step || !step)
  889. goto ret_einval;
  890. as_backup = as;
  891. a = dm_shift_arg(&as);
  892. if (a && sscanf(a, "%u%c", &feature_args, &dummy) == 1) {
  893. while (feature_args--) {
  894. a = dm_shift_arg(&as);
  895. if (!a)
  896. goto ret_einval;
  897. if (!strcasecmp(a, "precise_timestamps"))
  898. stat_flags |= STAT_PRECISE_TIMESTAMPS;
  899. else if (!strncasecmp(a, "histogram:", 10)) {
  900. if (n_histogram_entries)
  901. goto ret_einval;
  902. r = parse_histogram(a + 10, &n_histogram_entries, &histogram_boundaries);
  903. if (r)
  904. goto ret;
  905. } else
  906. goto ret_einval;
  907. }
  908. } else {
  909. as = as_backup;
  910. }
  911. program_id = "-";
  912. aux_data = "-";
  913. a = dm_shift_arg(&as);
  914. if (a)
  915. program_id = a;
  916. a = dm_shift_arg(&as);
  917. if (a)
  918. aux_data = a;
  919. if (as.argc)
  920. goto ret_einval;
  921. /*
  922. * If a buffer overflow happens after we created the region,
  923. * it's too late (the userspace would retry with a larger
  924. * buffer, but the region id that caused the overflow is already
  925. * leaked). So we must detect buffer overflow in advance.
  926. */
  927. snprintf(result, maxlen, "%d", INT_MAX);
  928. if (dm_message_test_buffer_overflow(result, maxlen)) {
  929. r = 1;
  930. goto ret;
  931. }
  932. id = dm_stats_create(dm_get_stats(md), start, end, step, stat_flags,
  933. n_histogram_entries, histogram_boundaries, program_id, aux_data,
  934. dm_internal_suspend_fast, dm_internal_resume_fast, md);
  935. if (id < 0) {
  936. r = id;
  937. goto ret;
  938. }
  939. snprintf(result, maxlen, "%d", id);
  940. r = 1;
  941. goto ret;
  942. ret_einval:
  943. r = -EINVAL;
  944. ret:
  945. kfree(histogram_boundaries);
  946. return r;
  947. }
  948. static int message_stats_delete(struct mapped_device *md,
  949. unsigned int argc, char **argv)
  950. {
  951. int id;
  952. char dummy;
  953. if (argc != 2)
  954. return -EINVAL;
  955. if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
  956. return -EINVAL;
  957. return dm_stats_delete(dm_get_stats(md), id);
  958. }
  959. static int message_stats_clear(struct mapped_device *md,
  960. unsigned int argc, char **argv)
  961. {
  962. int id;
  963. char dummy;
  964. if (argc != 2)
  965. return -EINVAL;
  966. if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
  967. return -EINVAL;
  968. return dm_stats_clear(dm_get_stats(md), id);
  969. }
  970. static int message_stats_list(struct mapped_device *md,
  971. unsigned int argc, char **argv,
  972. char *result, unsigned int maxlen)
  973. {
  974. int r;
  975. const char *program = NULL;
  976. if (argc < 1 || argc > 2)
  977. return -EINVAL;
  978. if (argc > 1) {
  979. program = kstrdup(argv[1], GFP_KERNEL);
  980. if (!program)
  981. return -ENOMEM;
  982. }
  983. r = dm_stats_list(dm_get_stats(md), program, result, maxlen);
  984. kfree(program);
  985. return r;
  986. }
  987. static int message_stats_print(struct mapped_device *md,
  988. unsigned int argc, char **argv, bool clear,
  989. char *result, unsigned int maxlen)
  990. {
  991. int id;
  992. char dummy;
  993. unsigned long idx_start = 0, idx_len = ULONG_MAX;
  994. if (argc != 2 && argc != 4)
  995. return -EINVAL;
  996. if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
  997. return -EINVAL;
  998. if (argc > 3) {
  999. if (strcmp(argv[2], "-") &&
  1000. sscanf(argv[2], "%lu%c", &idx_start, &dummy) != 1)
  1001. return -EINVAL;
  1002. if (strcmp(argv[3], "-") &&
  1003. sscanf(argv[3], "%lu%c", &idx_len, &dummy) != 1)
  1004. return -EINVAL;
  1005. }
  1006. return dm_stats_print(dm_get_stats(md), id, idx_start, idx_len, clear,
  1007. result, maxlen);
  1008. }
  1009. static int message_stats_set_aux(struct mapped_device *md,
  1010. unsigned int argc, char **argv)
  1011. {
  1012. int id;
  1013. char dummy;
  1014. if (argc != 3)
  1015. return -EINVAL;
  1016. if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
  1017. return -EINVAL;
  1018. return dm_stats_set_aux(dm_get_stats(md), id, argv[2]);
  1019. }
  1020. int dm_stats_message(struct mapped_device *md, unsigned int argc, char **argv,
  1021. char *result, unsigned int maxlen)
  1022. {
  1023. int r;
  1024. /* All messages here must start with '@' */
  1025. if (!strcasecmp(argv[0], "@stats_create"))
  1026. r = message_stats_create(md, argc, argv, result, maxlen);
  1027. else if (!strcasecmp(argv[0], "@stats_delete"))
  1028. r = message_stats_delete(md, argc, argv);
  1029. else if (!strcasecmp(argv[0], "@stats_clear"))
  1030. r = message_stats_clear(md, argc, argv);
  1031. else if (!strcasecmp(argv[0], "@stats_list"))
  1032. r = message_stats_list(md, argc, argv, result, maxlen);
  1033. else if (!strcasecmp(argv[0], "@stats_print"))
  1034. r = message_stats_print(md, argc, argv, false, result, maxlen);
  1035. else if (!strcasecmp(argv[0], "@stats_print_clear"))
  1036. r = message_stats_print(md, argc, argv, true, result, maxlen);
  1037. else if (!strcasecmp(argv[0], "@stats_set_aux"))
  1038. r = message_stats_set_aux(md, argc, argv);
  1039. else
  1040. return 2; /* this wasn't a stats message */
  1041. if (r == -EINVAL)
  1042. DMCRIT("Invalid parameters for message %s", argv[0]);
  1043. return r;
  1044. }
  1045. int __init dm_statistics_init(void)
  1046. {
  1047. shared_memory_amount = 0;
  1048. dm_stat_need_rcu_barrier = 0;
  1049. return 0;
  1050. }
  1051. void dm_statistics_exit(void)
  1052. {
  1053. if (dm_stat_need_rcu_barrier)
  1054. rcu_barrier();
  1055. if (WARN_ON(shared_memory_amount))
  1056. DMCRIT("shared_memory_amount leaked: %lu", shared_memory_amount);
  1057. }
  1058. module_param_named(stats_current_allocated_bytes, shared_memory_amount, ulong, 0444);
  1059. MODULE_PARM_DESC(stats_current_allocated_bytes, "Memory currently used by statistics");