kvm_trace.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. rec.u.cycle.cycle_u64 = get_cycles();
  63. for (i = 0; i < rec.extra_u32; i++)
  64. rec.u.cycle.extra_u32[i] = va_arg(*args, u32);
  65. } else {
  66. for (i = 0; i < rec.extra_u32; i++)
  67. rec.u.nocycle.extra_u32[i] = va_arg(*args, u32);
  68. }
  69. size = calc_rec_size(rec.cycle_in, rec.extra_u32 * sizeof(u32));
  70. relay_write(kt->rchan, &rec, size);
  71. }
  72. static struct kvm_trace_probe kvm_trace_probes[] = {
  73. { "kvm_trace_entryexit", "%u %p %u %u %u %u %u %u", 1, kvm_add_trace },
  74. { "kvm_trace_handler", "%u %p %u %u %u %u %u %u", 0, kvm_add_trace },
  75. };
  76. static int lost_records_get(void *data, u64 *val)
  77. {
  78. struct kvm_trace *kt = data;
  79. *val = atomic_read(&kt->lost_records);
  80. return 0;
  81. }
  82. DEFINE_SIMPLE_ATTRIBUTE(kvm_trace_lost_ops, lost_records_get, NULL, "%llu\n");
  83. /*
  84. * The relay channel is used in "no-overwrite" mode, it keeps trace of how
  85. * many times we encountered a full subbuffer, to tell user space app the
  86. * lost records there were.
  87. */
  88. static int kvm_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
  89. void *prev_subbuf, size_t prev_padding)
  90. {
  91. struct kvm_trace *kt;
  92. if (!relay_buf_full(buf)) {
  93. if (!prev_subbuf) {
  94. /*
  95. * executed only once when the channel is opened
  96. * save metadata as first record
  97. */
  98. subbuf_start_reserve(buf, sizeof(u32));
  99. *(u32 *)subbuf = 0x12345678;
  100. }
  101. return 1;
  102. }
  103. kt = buf->chan->private_data;
  104. atomic_inc(&kt->lost_records);
  105. return 0;
  106. }
  107. static struct dentry *kvm_create_buf_file_callack(const char *filename,
  108. struct dentry *parent,
  109. int mode,
  110. struct rchan_buf *buf,
  111. int *is_global)
  112. {
  113. return debugfs_create_file(filename, mode, parent, buf,
  114. &relay_file_operations);
  115. }
  116. static int kvm_remove_buf_file_callback(struct dentry *dentry)
  117. {
  118. debugfs_remove(dentry);
  119. return 0;
  120. }
  121. static struct rchan_callbacks kvm_relay_callbacks = {
  122. .subbuf_start = kvm_subbuf_start_callback,
  123. .create_buf_file = kvm_create_buf_file_callack,
  124. .remove_buf_file = kvm_remove_buf_file_callback,
  125. };
  126. static int do_kvm_trace_enable(struct kvm_user_trace_setup *kuts)
  127. {
  128. struct kvm_trace *kt;
  129. int i, r = -ENOMEM;
  130. if (!kuts->buf_size || !kuts->buf_nr)
  131. return -EINVAL;
  132. kt = kzalloc(sizeof(*kt), GFP_KERNEL);
  133. if (!kt)
  134. goto err;
  135. r = -EIO;
  136. atomic_set(&kt->lost_records, 0);
  137. kt->lost_file = debugfs_create_file("lost_records", 0444, kvm_debugfs_dir,
  138. kt, &kvm_trace_lost_ops);
  139. if (!kt->lost_file)
  140. goto err;
  141. kt->rchan = relay_open("trace", kvm_debugfs_dir, kuts->buf_size,
  142. kuts->buf_nr, &kvm_relay_callbacks, kt);
  143. if (!kt->rchan)
  144. goto err;
  145. kvm_trace = kt;
  146. for (i = 0; i < ARRAY_SIZE(kvm_trace_probes); i++) {
  147. struct kvm_trace_probe *p = &kvm_trace_probes[i];
  148. r = marker_probe_register(p->name, p->format, p->probe_func, p);
  149. if (r)
  150. printk(KERN_INFO "Unable to register probe %s\n",
  151. p->name);
  152. }
  153. kvm_trace->trace_state = KVM_TRACE_STATE_RUNNING;
  154. return 0;
  155. err:
  156. if (kt) {
  157. if (kt->lost_file)
  158. debugfs_remove(kt->lost_file);
  159. if (kt->rchan)
  160. relay_close(kt->rchan);
  161. kfree(kt);
  162. }
  163. return r;
  164. }
  165. static int kvm_trace_enable(char __user *arg)
  166. {
  167. struct kvm_user_trace_setup kuts;
  168. int ret;
  169. ret = copy_from_user(&kuts, arg, sizeof(kuts));
  170. if (ret)
  171. return -EFAULT;
  172. ret = do_kvm_trace_enable(&kuts);
  173. if (ret)
  174. return ret;
  175. return 0;
  176. }
  177. static int kvm_trace_pause(void)
  178. {
  179. struct kvm_trace *kt = kvm_trace;
  180. int r = -EINVAL;
  181. if (kt == NULL)
  182. return r;
  183. if (kt->trace_state == KVM_TRACE_STATE_RUNNING) {
  184. kt->trace_state = KVM_TRACE_STATE_PAUSE;
  185. relay_flush(kt->rchan);
  186. r = 0;
  187. }
  188. return r;
  189. }
  190. void kvm_trace_cleanup(void)
  191. {
  192. struct kvm_trace *kt = kvm_trace;
  193. int i;
  194. if (kt == NULL)
  195. return;
  196. if (kt->trace_state == KVM_TRACE_STATE_RUNNING ||
  197. kt->trace_state == KVM_TRACE_STATE_PAUSE) {
  198. kt->trace_state = KVM_TRACE_STATE_CLEARUP;
  199. for (i = 0; i < ARRAY_SIZE(kvm_trace_probes); i++) {
  200. struct kvm_trace_probe *p = &kvm_trace_probes[i];
  201. marker_probe_unregister(p->name, p->probe_func, p);
  202. }
  203. relay_close(kt->rchan);
  204. debugfs_remove(kt->lost_file);
  205. kfree(kt);
  206. }
  207. }
  208. int kvm_trace_ioctl(unsigned int ioctl, unsigned long arg)
  209. {
  210. void __user *argp = (void __user *)arg;
  211. long r = -EINVAL;
  212. if (!capable(CAP_SYS_ADMIN))
  213. return -EPERM;
  214. switch (ioctl) {
  215. case KVM_TRACE_ENABLE:
  216. r = kvm_trace_enable(argp);
  217. break;
  218. case KVM_TRACE_PAUSE:
  219. r = kvm_trace_pause();
  220. break;
  221. case KVM_TRACE_DISABLE:
  222. r = 0;
  223. kvm_trace_cleanup();
  224. break;
  225. }
  226. return r;
  227. }