fprobe.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fprobe - Simple ftrace probe wrapper for function entry.
  4. */
  5. #define pr_fmt(fmt) "fprobe: " fmt
  6. #include <linux/err.h>
  7. #include <linux/fprobe.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/kprobes.h>
  10. #include <linux/rethook.h>
  11. #include <linux/slab.h>
  12. #include <linux/sort.h>
  13. #include "trace.h"
  14. struct fprobe_rethook_node {
  15. struct rethook_node node;
  16. unsigned long entry_ip;
  17. unsigned long entry_parent_ip;
  18. char data[];
  19. };
  20. static inline void __fprobe_handler(unsigned long ip, unsigned long parent_ip,
  21. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  22. {
  23. struct fprobe_rethook_node *fpr;
  24. struct rethook_node *rh = NULL;
  25. struct fprobe *fp;
  26. void *entry_data = NULL;
  27. int ret = 0;
  28. fp = container_of(ops, struct fprobe, ops);
  29. if (fp->exit_handler) {
  30. rh = rethook_try_get(fp->rethook);
  31. if (!rh) {
  32. fp->nmissed++;
  33. return;
  34. }
  35. fpr = container_of(rh, struct fprobe_rethook_node, node);
  36. fpr->entry_ip = ip;
  37. fpr->entry_parent_ip = parent_ip;
  38. if (fp->entry_data_size)
  39. entry_data = fpr->data;
  40. }
  41. if (fp->entry_handler)
  42. ret = fp->entry_handler(fp, ip, parent_ip, ftrace_get_regs(fregs), entry_data);
  43. /* If entry_handler returns !0, nmissed is not counted. */
  44. if (rh) {
  45. if (ret)
  46. rethook_recycle(rh);
  47. else
  48. rethook_hook(rh, ftrace_get_regs(fregs), true);
  49. }
  50. }
  51. static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
  52. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  53. {
  54. struct fprobe *fp;
  55. int bit;
  56. fp = container_of(ops, struct fprobe, ops);
  57. if (fprobe_disabled(fp))
  58. return;
  59. /* recursion detection has to go before any traceable function and
  60. * all functions before this point should be marked as notrace
  61. */
  62. bit = ftrace_test_recursion_trylock(ip, parent_ip);
  63. if (bit < 0) {
  64. fp->nmissed++;
  65. return;
  66. }
  67. __fprobe_handler(ip, parent_ip, ops, fregs);
  68. ftrace_test_recursion_unlock(bit);
  69. }
  70. NOKPROBE_SYMBOL(fprobe_handler);
  71. static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
  72. struct ftrace_ops *ops, struct ftrace_regs *fregs)
  73. {
  74. struct fprobe *fp;
  75. int bit;
  76. fp = container_of(ops, struct fprobe, ops);
  77. if (fprobe_disabled(fp))
  78. return;
  79. /* recursion detection has to go before any traceable function and
  80. * all functions called before this point should be marked as notrace
  81. */
  82. bit = ftrace_test_recursion_trylock(ip, parent_ip);
  83. if (bit < 0) {
  84. fp->nmissed++;
  85. return;
  86. }
  87. /*
  88. * This user handler is shared with other kprobes and is not expected to be
  89. * called recursively. So if any other kprobe handler is running, this will
  90. * exit as kprobe does. See the section 'Share the callbacks with kprobes'
  91. * in Documentation/trace/fprobe.rst for more information.
  92. */
  93. if (unlikely(kprobe_running())) {
  94. fp->nmissed++;
  95. goto recursion_unlock;
  96. }
  97. kprobe_busy_begin();
  98. __fprobe_handler(ip, parent_ip, ops, fregs);
  99. kprobe_busy_end();
  100. recursion_unlock:
  101. ftrace_test_recursion_unlock(bit);
  102. }
  103. static void fprobe_exit_handler(struct rethook_node *rh, void *data,
  104. unsigned long ret_ip, struct pt_regs *regs)
  105. {
  106. struct fprobe *fp = (struct fprobe *)data;
  107. struct fprobe_rethook_node *fpr;
  108. int bit;
  109. if (!fp || fprobe_disabled(fp))
  110. return;
  111. fpr = container_of(rh, struct fprobe_rethook_node, node);
  112. /*
  113. * we need to assure no calls to traceable functions in-between the
  114. * end of fprobe_handler and the beginning of fprobe_exit_handler.
  115. */
  116. bit = ftrace_test_recursion_trylock(fpr->entry_ip, fpr->entry_parent_ip);
  117. if (bit < 0) {
  118. fp->nmissed++;
  119. return;
  120. }
  121. fp->exit_handler(fp, fpr->entry_ip, ret_ip, regs,
  122. fp->entry_data_size ? (void *)fpr->data : NULL);
  123. ftrace_test_recursion_unlock(bit);
  124. }
  125. NOKPROBE_SYMBOL(fprobe_exit_handler);
  126. static int symbols_cmp(const void *a, const void *b)
  127. {
  128. const char **str_a = (const char **) a;
  129. const char **str_b = (const char **) b;
  130. return strcmp(*str_a, *str_b);
  131. }
  132. /* Convert ftrace location address from symbols */
  133. static unsigned long *get_ftrace_locations(const char **syms, int num)
  134. {
  135. unsigned long *addrs;
  136. /* Convert symbols to symbol address */
  137. addrs = kcalloc(num, sizeof(*addrs), GFP_KERNEL);
  138. if (!addrs)
  139. return ERR_PTR(-ENOMEM);
  140. /* ftrace_lookup_symbols expects sorted symbols */
  141. sort(syms, num, sizeof(*syms), symbols_cmp, NULL);
  142. if (!ftrace_lookup_symbols(syms, num, addrs))
  143. return addrs;
  144. kfree(addrs);
  145. return ERR_PTR(-ENOENT);
  146. }
  147. static void fprobe_init(struct fprobe *fp)
  148. {
  149. fp->nmissed = 0;
  150. if (fprobe_shared_with_kprobes(fp))
  151. fp->ops.func = fprobe_kprobe_handler;
  152. else
  153. fp->ops.func = fprobe_handler;
  154. fp->ops.flags |= FTRACE_OPS_FL_SAVE_REGS;
  155. }
  156. static int fprobe_init_rethook(struct fprobe *fp, int num)
  157. {
  158. int size;
  159. if (!fp->exit_handler) {
  160. fp->rethook = NULL;
  161. return 0;
  162. }
  163. /* Initialize rethook if needed */
  164. if (fp->nr_maxactive)
  165. num = fp->nr_maxactive;
  166. else
  167. num *= num_possible_cpus() * 2;
  168. if (num <= 0)
  169. return -EINVAL;
  170. size = sizeof(struct fprobe_rethook_node) + fp->entry_data_size;
  171. /* Initialize rethook */
  172. fp->rethook = rethook_alloc((void *)fp, fprobe_exit_handler, size, num);
  173. if (IS_ERR(fp->rethook))
  174. return PTR_ERR(fp->rethook);
  175. return 0;
  176. }
  177. static void fprobe_fail_cleanup(struct fprobe *fp)
  178. {
  179. if (!IS_ERR_OR_NULL(fp->rethook)) {
  180. /* Don't need to cleanup rethook->handler because this is not used. */
  181. rethook_free(fp->rethook);
  182. fp->rethook = NULL;
  183. }
  184. ftrace_free_filter(&fp->ops);
  185. }
  186. /**
  187. * register_fprobe() - Register fprobe to ftrace by pattern.
  188. * @fp: A fprobe data structure to be registered.
  189. * @filter: A wildcard pattern of probed symbols.
  190. * @notfilter: A wildcard pattern of NOT probed symbols.
  191. *
  192. * Register @fp to ftrace for enabling the probe on the symbols matched to @filter.
  193. * If @notfilter is not NULL, the symbols matched the @notfilter are not probed.
  194. *
  195. * Return 0 if @fp is registered successfully, -errno if not.
  196. */
  197. int register_fprobe(struct fprobe *fp, const char *filter, const char *notfilter)
  198. {
  199. struct ftrace_hash *hash;
  200. unsigned char *str;
  201. int ret, len;
  202. if (!fp || !filter)
  203. return -EINVAL;
  204. fprobe_init(fp);
  205. len = strlen(filter);
  206. str = kstrdup(filter, GFP_KERNEL);
  207. ret = ftrace_set_filter(&fp->ops, str, len, 0);
  208. kfree(str);
  209. if (ret)
  210. return ret;
  211. if (notfilter) {
  212. len = strlen(notfilter);
  213. str = kstrdup(notfilter, GFP_KERNEL);
  214. ret = ftrace_set_notrace(&fp->ops, str, len, 0);
  215. kfree(str);
  216. if (ret)
  217. goto out;
  218. }
  219. /* TODO:
  220. * correctly calculate the total number of filtered symbols
  221. * from both filter and notfilter.
  222. */
  223. hash = rcu_access_pointer(fp->ops.local_hash.filter_hash);
  224. if (WARN_ON_ONCE(!hash))
  225. goto out;
  226. ret = fprobe_init_rethook(fp, (int)hash->count);
  227. if (!ret)
  228. ret = register_ftrace_function(&fp->ops);
  229. out:
  230. if (ret)
  231. fprobe_fail_cleanup(fp);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL_GPL(register_fprobe);
  235. /**
  236. * register_fprobe_ips() - Register fprobe to ftrace by address.
  237. * @fp: A fprobe data structure to be registered.
  238. * @addrs: An array of target ftrace location addresses.
  239. * @num: The number of entries of @addrs.
  240. *
  241. * Register @fp to ftrace for enabling the probe on the address given by @addrs.
  242. * The @addrs must be the addresses of ftrace location address, which may be
  243. * the symbol address + arch-dependent offset.
  244. * If you unsure what this mean, please use other registration functions.
  245. *
  246. * Return 0 if @fp is registered successfully, -errno if not.
  247. */
  248. int register_fprobe_ips(struct fprobe *fp, unsigned long *addrs, int num)
  249. {
  250. int ret;
  251. if (!fp || !addrs || num <= 0)
  252. return -EINVAL;
  253. fprobe_init(fp);
  254. ret = ftrace_set_filter_ips(&fp->ops, addrs, num, 0, 0);
  255. if (ret)
  256. return ret;
  257. ret = fprobe_init_rethook(fp, num);
  258. if (!ret)
  259. ret = register_ftrace_function(&fp->ops);
  260. if (ret)
  261. fprobe_fail_cleanup(fp);
  262. return ret;
  263. }
  264. EXPORT_SYMBOL_GPL(register_fprobe_ips);
  265. /**
  266. * register_fprobe_syms() - Register fprobe to ftrace by symbols.
  267. * @fp: A fprobe data structure to be registered.
  268. * @syms: An array of target symbols.
  269. * @num: The number of entries of @syms.
  270. *
  271. * Register @fp to the symbols given by @syms array. This will be useful if
  272. * you are sure the symbols exist in the kernel.
  273. *
  274. * Return 0 if @fp is registered successfully, -errno if not.
  275. */
  276. int register_fprobe_syms(struct fprobe *fp, const char **syms, int num)
  277. {
  278. unsigned long *addrs;
  279. int ret;
  280. if (!fp || !syms || num <= 0)
  281. return -EINVAL;
  282. addrs = get_ftrace_locations(syms, num);
  283. if (IS_ERR(addrs))
  284. return PTR_ERR(addrs);
  285. ret = register_fprobe_ips(fp, addrs, num);
  286. kfree(addrs);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL_GPL(register_fprobe_syms);
  290. bool fprobe_is_registered(struct fprobe *fp)
  291. {
  292. if (!fp || (fp->ops.saved_func != fprobe_handler &&
  293. fp->ops.saved_func != fprobe_kprobe_handler))
  294. return false;
  295. return true;
  296. }
  297. /**
  298. * unregister_fprobe() - Unregister fprobe from ftrace
  299. * @fp: A fprobe data structure to be unregistered.
  300. *
  301. * Unregister fprobe (and remove ftrace hooks from the function entries).
  302. *
  303. * Return 0 if @fp is unregistered successfully, -errno if not.
  304. */
  305. int unregister_fprobe(struct fprobe *fp)
  306. {
  307. int ret;
  308. if (!fprobe_is_registered(fp))
  309. return -EINVAL;
  310. if (!IS_ERR_OR_NULL(fp->rethook))
  311. rethook_stop(fp->rethook);
  312. ret = unregister_ftrace_function(&fp->ops);
  313. if (ret < 0)
  314. return ret;
  315. if (!IS_ERR_OR_NULL(fp->rethook))
  316. rethook_free(fp->rethook);
  317. ftrace_free_filter(&fp->ops);
  318. return ret;
  319. }
  320. EXPORT_SYMBOL_GPL(unregister_fprobe);