kvm_trace.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * kvm trace
  3. *
  4. * It is designed to allow debugging traces of kvm to be generated
  5. * on UP / SMP machines. Each trace entry can be timestamped so that
  6. * it's possible to reconstruct a chronological record of trace events.
  7. * The implementation refers to blktrace kernel support.
  8. *
  9. * Copyright (c) 2008 Intel Corporation
  10. * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
  11. *
  12. * Authors: Feng(Eric) Liu, eric.e.liu@intel.com
  13. *
  14. * Date: Feb 2008
  15. */
  16. #include <linux/module.h>
  17. #include <linux/relay.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/kvm_host.h>
  20. #define KVM_TRACE_STATE_RUNNING (1 << 0)
  21. #define KVM_TRACE_STATE_PAUSE (1 << 1)
  22. #define KVM_TRACE_STATE_CLEARUP (1 << 2)
  23. struct kvm_trace {
  24. int trace_state;
  25. struct rchan *rchan;
  26. struct dentry *lost_file;
  27. atomic_t lost_records;
  28. };
  29. static struct kvm_trace *kvm_trace;
  30. struct kvm_trace_probe {
  31. const char *name;
  32. const char *format;
  33. u32 cycle_in;
  34. marker_probe_func *probe_func;
  35. };
  36. static inline int calc_rec_size(int cycle, int extra)
  37. {
  38. int rec_size = KVM_TRC_HEAD_SIZE;
  39. rec_size += extra;
  40. return cycle ? rec_size += KVM_TRC_CYCLE_SIZE : rec_size;
  41. }
  42. static void kvm_add_trace(void *probe_private, void *call_data,
  43. const char *format, va_list *args)
  44. {
  45. struct kvm_trace_probe *p = probe_private;
  46. struct kvm_trace *kt = kvm_trace;
  47. struct kvm_trace_rec rec;
  48. struct kvm_vcpu *vcpu;
  49. int i, extra, size;
  50. if (unlikely(kt->trace_state != KVM_TRACE_STATE_RUNNING))
  51. return;
  52. rec.event = va_arg(*args, u32);
  53. vcpu = va_arg(*args, struct kvm_vcpu *);
  54. rec.pid = current->tgid;
  55. rec.vcpu_id = vcpu->vcpu_id;
  56. extra = va_arg(*args, u32);
  57. WARN_ON(!(extra <= KVM_TRC_EXTRA_MAX));
  58. extra = min_t(u32, extra, KVM_TRC_EXTRA_MAX);
  59. rec.extra_u32 = extra;
  60. rec.cycle_in = p->cycle_in;
  61. if (rec.cycle_in) {
  62. u64 cycle = 0;
  63. cycle = get_cycles();
  64. rec.u.cycle.cycle_lo = (u32)cycle;
  65. rec.u.cycle.cycle_hi = (u32)(cycle >> 32);
  66. for (i = 0; i < rec.extra_u32; i++)
  67. rec.u.cycle.extra_u32[i] = va_arg(*args, u32);
  68. } else {
  69. for (i = 0; i < rec.extra_u32; i++)
  70. rec.u.nocycle.extra_u32[i] = va_arg(*args, u32);
  71. }
  72. size = calc_rec_size(rec.cycle_in, rec.extra_u32 * sizeof(u32));
  73. relay_write(kt->rchan, &rec, size);
  74. }
  75. static struct kvm_trace_probe kvm_trace_probes[] = {
  76. { "kvm_trace_entryexit", "%u %p %u %u %u %u %u %u", 1, kvm_add_trace },
  77. { "kvm_trace_handler", "%u %p %u %u %u %u %u %u", 0, kvm_add_trace },
  78. };
  79. static int lost_records_get(void *data, u64 *val)
  80. {
  81. struct kvm_trace *kt = data;
  82. *val = atomic_read(&kt->lost_records);
  83. return 0;
  84. }
  85. DEFINE_SIMPLE_ATTRIBUTE(kvm_trace_lost_ops, lost_records_get, NULL, "%llu\n");
  86. /*
  87. * The relay channel is used in "no-overwrite" mode, it keeps trace of how
  88. * many times we encountered a full subbuffer, to tell user space app the
  89. * lost records there were.
  90. */
  91. static int kvm_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
  92. void *prev_subbuf, size_t prev_padding)
  93. {
  94. struct kvm_trace *kt;
  95. if (!relay_buf_full(buf))
  96. return 1;
  97. kt = buf->chan->private_data;
  98. atomic_inc(&kt->lost_records);
  99. return 0;
  100. }
  101. static struct dentry *kvm_create_buf_file_callack(const char *filename,
  102. struct dentry *parent,
  103. int mode,
  104. struct rchan_buf *buf,
  105. int *is_global)
  106. {
  107. return debugfs_create_file(filename, mode, parent, buf,
  108. &relay_file_operations);
  109. }
  110. static int kvm_remove_buf_file_callback(struct dentry *dentry)
  111. {
  112. debugfs_remove(dentry);
  113. return 0;
  114. }
  115. static struct rchan_callbacks kvm_relay_callbacks = {
  116. .subbuf_start = kvm_subbuf_start_callback,
  117. .create_buf_file = kvm_create_buf_file_callack,
  118. .remove_buf_file = kvm_remove_buf_file_callback,
  119. };
  120. static int do_kvm_trace_enable(struct kvm_user_trace_setup *kuts)
  121. {
  122. struct kvm_trace *kt;
  123. int i, r = -ENOMEM;
  124. if (!kuts->buf_size || !kuts->buf_nr)
  125. return -EINVAL;
  126. kt = kzalloc(sizeof(*kt), GFP_KERNEL);
  127. if (!kt)
  128. goto err;
  129. r = -EIO;
  130. atomic_set(&kt->lost_records, 0);
  131. kt->lost_file = debugfs_create_file("lost_records", 0444, kvm_debugfs_dir,
  132. kt, &kvm_trace_lost_ops);
  133. if (!kt->lost_file)
  134. goto err;
  135. kt->rchan = relay_open("trace", kvm_debugfs_dir, kuts->buf_size,
  136. kuts->buf_nr, &kvm_relay_callbacks, kt);
  137. if (!kt->rchan)
  138. goto err;
  139. kvm_trace = kt;
  140. for (i = 0; i < ARRAY_SIZE(kvm_trace_probes); i++) {
  141. struct kvm_trace_probe *p = &kvm_trace_probes[i];
  142. r = marker_probe_register(p->name, p->format, p->probe_func, p);
  143. if (r)
  144. printk(KERN_INFO "Unable to register probe %s\n",
  145. p->name);
  146. }
  147. kvm_trace->trace_state = KVM_TRACE_STATE_RUNNING;
  148. return 0;
  149. err:
  150. if (kt) {
  151. if (kt->lost_file)
  152. debugfs_remove(kt->lost_file);
  153. if (kt->rchan)
  154. relay_close(kt->rchan);
  155. kfree(kt);
  156. }
  157. return r;
  158. }
  159. static int kvm_trace_enable(char __user *arg)
  160. {
  161. struct kvm_user_trace_setup kuts;
  162. int ret;
  163. ret = copy_from_user(&kuts, arg, sizeof(kuts));
  164. if (ret)
  165. return -EFAULT;
  166. ret = do_kvm_trace_enable(&kuts);
  167. if (ret)
  168. return ret;
  169. return 0;
  170. }
  171. static int kvm_trace_pause(void)
  172. {
  173. struct kvm_trace *kt = kvm_trace;
  174. int r = -EINVAL;
  175. if (kt == NULL)
  176. return r;
  177. if (kt->trace_state == KVM_TRACE_STATE_RUNNING) {
  178. kt->trace_state = KVM_TRACE_STATE_PAUSE;
  179. relay_flush(kt->rchan);
  180. r = 0;
  181. }
  182. return r;
  183. }
  184. void kvm_trace_cleanup(void)
  185. {
  186. struct kvm_trace *kt = kvm_trace;
  187. int i;
  188. if (kt == NULL)
  189. return;
  190. if (kt->trace_state == KVM_TRACE_STATE_RUNNING ||
  191. kt->trace_state == KVM_TRACE_STATE_PAUSE) {
  192. kt->trace_state = KVM_TRACE_STATE_CLEARUP;
  193. for (i = 0; i < ARRAY_SIZE(kvm_trace_probes); i++) {
  194. struct kvm_trace_probe *p = &kvm_trace_probes[i];
  195. marker_probe_unregister(p->name, p->probe_func, p);
  196. }
  197. relay_close(kt->rchan);
  198. debugfs_remove(kt->lost_file);
  199. kfree(kt);
  200. }
  201. }
  202. int kvm_trace_ioctl(unsigned int ioctl, unsigned long arg)
  203. {
  204. void __user *argp = (void __user *)arg;
  205. long r = -EINVAL;
  206. if (!capable(CAP_SYS_ADMIN))
  207. return -EPERM;
  208. switch (ioctl) {
  209. case KVM_TRACE_ENABLE:
  210. r = kvm_trace_enable(argp);
  211. break;
  212. case KVM_TRACE_PAUSE:
  213. r = kvm_trace_pause();
  214. break;
  215. case KVM_TRACE_DISABLE:
  216. r = 0;
  217. kvm_trace_cleanup();
  218. break;
  219. }
  220. return r;
  221. }