dm-stats.c 29 KB

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