dm-stats.c 29 KB

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