dm-stats.c 29 KB

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