trace_uprobe.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * uprobes-based tracing events
  4. *
  5. * Copyright (C) IBM Corporation, 2010-2012
  6. * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
  7. */
  8. #define pr_fmt(fmt) "trace_uprobe: " fmt
  9. #include <linux/bpf-cgroup.h>
  10. #include <linux/security.h>
  11. #include <linux/ctype.h>
  12. #include <linux/module.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/uprobes.h>
  15. #include <linux/namei.h>
  16. #include <linux/string.h>
  17. #include <linux/rculist.h>
  18. #include <linux/filter.h>
  19. #include <linux/percpu.h>
  20. #include "trace_dynevent.h"
  21. #include "trace_probe.h"
  22. #include "trace_probe_tmpl.h"
  23. #define UPROBE_EVENT_SYSTEM "uprobes"
  24. struct uprobe_trace_entry_head {
  25. struct trace_entry ent;
  26. unsigned long vaddr[];
  27. };
  28. #define SIZEOF_TRACE_ENTRY(is_return) \
  29. (sizeof(struct uprobe_trace_entry_head) + \
  30. sizeof(unsigned long) * (is_return ? 2 : 1))
  31. #define DATAOF_TRACE_ENTRY(entry, is_return) \
  32. ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
  33. static int trace_uprobe_create(const char *raw_command);
  34. static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);
  35. static int trace_uprobe_release(struct dyn_event *ev);
  36. static bool trace_uprobe_is_busy(struct dyn_event *ev);
  37. static bool trace_uprobe_match(const char *system, const char *event,
  38. int argc, const char **argv, struct dyn_event *ev);
  39. static struct dyn_event_operations trace_uprobe_ops = {
  40. .create = trace_uprobe_create,
  41. .show = trace_uprobe_show,
  42. .is_busy = trace_uprobe_is_busy,
  43. .free = trace_uprobe_release,
  44. .match = trace_uprobe_match,
  45. };
  46. /*
  47. * uprobe event core functions
  48. */
  49. struct trace_uprobe {
  50. struct dyn_event devent;
  51. struct uprobe_consumer consumer;
  52. struct path path;
  53. char *filename;
  54. struct uprobe *uprobe;
  55. unsigned long offset;
  56. unsigned long ref_ctr_offset;
  57. unsigned long __percpu *nhits;
  58. struct trace_probe tp;
  59. };
  60. static bool is_trace_uprobe(struct dyn_event *ev)
  61. {
  62. return ev->ops == &trace_uprobe_ops;
  63. }
  64. static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev)
  65. {
  66. return container_of(ev, struct trace_uprobe, devent);
  67. }
  68. /**
  69. * for_each_trace_uprobe - iterate over the trace_uprobe list
  70. * @pos: the struct trace_uprobe * for each entry
  71. * @dpos: the struct dyn_event * to use as a loop cursor
  72. */
  73. #define for_each_trace_uprobe(pos, dpos) \
  74. for_each_dyn_event(dpos) \
  75. if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos)))
  76. static int register_uprobe_event(struct trace_uprobe *tu);
  77. static int unregister_uprobe_event(struct trace_uprobe *tu);
  78. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
  79. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  80. unsigned long func, struct pt_regs *regs);
  81. #ifdef CONFIG_STACK_GROWSUP
  82. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  83. {
  84. return addr - (n * sizeof(long));
  85. }
  86. #else
  87. static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
  88. {
  89. return addr + (n * sizeof(long));
  90. }
  91. #endif
  92. static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
  93. {
  94. unsigned long ret;
  95. unsigned long addr = user_stack_pointer(regs);
  96. addr = adjust_stack_addr(addr, n);
  97. if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
  98. return 0;
  99. return ret;
  100. }
  101. /*
  102. * Uprobes-specific fetch functions
  103. */
  104. static nokprobe_inline int
  105. probe_mem_read(void *dest, void *src, size_t size)
  106. {
  107. void __user *vaddr = (void __force __user *)src;
  108. return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
  109. }
  110. static nokprobe_inline int
  111. probe_mem_read_user(void *dest, void *src, size_t size)
  112. {
  113. return probe_mem_read(dest, src, size);
  114. }
  115. /*
  116. * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
  117. * length and relative data location.
  118. */
  119. static nokprobe_inline int
  120. fetch_store_string(unsigned long addr, void *dest, void *base)
  121. {
  122. long ret;
  123. u32 loc = *(u32 *)dest;
  124. int maxlen = get_loc_len(loc);
  125. u8 *dst = get_loc_data(dest, base);
  126. void __user *src = (void __force __user *) addr;
  127. if (unlikely(!maxlen))
  128. return -ENOMEM;
  129. if (addr == FETCH_TOKEN_COMM)
  130. ret = strscpy(dst, current->comm, maxlen);
  131. else
  132. ret = strncpy_from_user(dst, src, maxlen);
  133. if (ret >= 0) {
  134. if (ret == maxlen)
  135. dst[ret - 1] = '\0';
  136. else
  137. /*
  138. * Include the terminating null byte. In this case it
  139. * was copied by strncpy_from_user but not accounted
  140. * for in ret.
  141. */
  142. ret++;
  143. *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
  144. } else
  145. *(u32 *)dest = make_data_loc(0, (void *)dst - base);
  146. return ret;
  147. }
  148. static nokprobe_inline int
  149. fetch_store_string_user(unsigned long addr, void *dest, void *base)
  150. {
  151. return fetch_store_string(addr, dest, base);
  152. }
  153. /* Return the length of string -- including null terminal byte */
  154. static nokprobe_inline int
  155. fetch_store_strlen(unsigned long addr)
  156. {
  157. int len;
  158. void __user *vaddr = (void __force __user *) addr;
  159. if (addr == FETCH_TOKEN_COMM)
  160. len = strlen(current->comm) + 1;
  161. else
  162. len = strnlen_user(vaddr, MAX_STRING_SIZE);
  163. return (len > MAX_STRING_SIZE) ? 0 : len;
  164. }
  165. static nokprobe_inline int
  166. fetch_store_strlen_user(unsigned long addr)
  167. {
  168. return fetch_store_strlen(addr);
  169. }
  170. static unsigned long translate_user_vaddr(unsigned long file_offset)
  171. {
  172. unsigned long base_addr;
  173. struct uprobe_dispatch_data *udd;
  174. udd = (void *) current->utask->vaddr;
  175. base_addr = udd->bp_addr - udd->tu->offset;
  176. return base_addr + file_offset;
  177. }
  178. /* Note that we don't verify it, since the code does not come from user space */
  179. static int
  180. process_fetch_insn(struct fetch_insn *code, void *rec, void *edata,
  181. void *dest, void *base)
  182. {
  183. struct pt_regs *regs = rec;
  184. unsigned long val;
  185. int ret;
  186. /* 1st stage: get value from context */
  187. switch (code->op) {
  188. case FETCH_OP_REG:
  189. val = regs_get_register(regs, code->param);
  190. break;
  191. case FETCH_OP_STACK:
  192. val = get_user_stack_nth(regs, code->param);
  193. break;
  194. case FETCH_OP_STACKP:
  195. val = user_stack_pointer(regs);
  196. break;
  197. case FETCH_OP_RETVAL:
  198. val = regs_return_value(regs);
  199. break;
  200. case FETCH_OP_COMM:
  201. val = FETCH_TOKEN_COMM;
  202. break;
  203. case FETCH_OP_FOFFS:
  204. val = translate_user_vaddr(code->immediate);
  205. break;
  206. default:
  207. ret = process_common_fetch_insn(code, &val);
  208. if (ret < 0)
  209. return ret;
  210. }
  211. code++;
  212. return process_fetch_insn_bottom(code, val, dest, base);
  213. }
  214. NOKPROBE_SYMBOL(process_fetch_insn)
  215. static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
  216. {
  217. rwlock_init(&filter->rwlock);
  218. filter->nr_systemwide = 0;
  219. INIT_LIST_HEAD(&filter->perf_events);
  220. }
  221. static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
  222. {
  223. return !filter->nr_systemwide && list_empty(&filter->perf_events);
  224. }
  225. static inline bool is_ret_probe(struct trace_uprobe *tu)
  226. {
  227. return tu->consumer.ret_handler != NULL;
  228. }
  229. static bool trace_uprobe_is_busy(struct dyn_event *ev)
  230. {
  231. struct trace_uprobe *tu = to_trace_uprobe(ev);
  232. return trace_probe_is_enabled(&tu->tp);
  233. }
  234. static bool trace_uprobe_match_command_head(struct trace_uprobe *tu,
  235. int argc, const char **argv)
  236. {
  237. char buf[MAX_ARGSTR_LEN + 1];
  238. int len;
  239. if (!argc)
  240. return true;
  241. len = strlen(tu->filename);
  242. if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':')
  243. return false;
  244. if (tu->ref_ctr_offset == 0)
  245. snprintf(buf, sizeof(buf), "0x%0*lx",
  246. (int)(sizeof(void *) * 2), tu->offset);
  247. else
  248. snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)",
  249. (int)(sizeof(void *) * 2), tu->offset,
  250. tu->ref_ctr_offset);
  251. if (strcmp(buf, &argv[0][len + 1]))
  252. return false;
  253. argc--; argv++;
  254. return trace_probe_match_command_args(&tu->tp, argc, argv);
  255. }
  256. static bool trace_uprobe_match(const char *system, const char *event,
  257. int argc, const char **argv, struct dyn_event *ev)
  258. {
  259. struct trace_uprobe *tu = to_trace_uprobe(ev);
  260. return (event[0] == '\0' ||
  261. strcmp(trace_probe_name(&tu->tp), event) == 0) &&
  262. (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) &&
  263. trace_uprobe_match_command_head(tu, argc, argv);
  264. }
  265. static nokprobe_inline struct trace_uprobe *
  266. trace_uprobe_primary_from_call(struct trace_event_call *call)
  267. {
  268. struct trace_probe *tp;
  269. tp = trace_probe_primary_from_call(call);
  270. if (WARN_ON_ONCE(!tp))
  271. return NULL;
  272. return container_of(tp, struct trace_uprobe, tp);
  273. }
  274. /*
  275. * Allocate new trace_uprobe and initialize it (including uprobes).
  276. */
  277. static struct trace_uprobe *
  278. alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
  279. {
  280. struct trace_uprobe *tu;
  281. int ret;
  282. tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL);
  283. if (!tu)
  284. return ERR_PTR(-ENOMEM);
  285. tu->nhits = alloc_percpu(unsigned long);
  286. if (!tu->nhits) {
  287. ret = -ENOMEM;
  288. goto error;
  289. }
  290. ret = trace_probe_init(&tu->tp, event, group, true, nargs);
  291. if (ret < 0)
  292. goto error;
  293. dyn_event_init(&tu->devent, &trace_uprobe_ops);
  294. tu->consumer.handler = uprobe_dispatcher;
  295. if (is_ret)
  296. tu->consumer.ret_handler = uretprobe_dispatcher;
  297. init_trace_uprobe_filter(tu->tp.event->filter);
  298. return tu;
  299. error:
  300. free_percpu(tu->nhits);
  301. kfree(tu);
  302. return ERR_PTR(ret);
  303. }
  304. static void free_trace_uprobe(struct trace_uprobe *tu)
  305. {
  306. if (!tu)
  307. return;
  308. path_put(&tu->path);
  309. trace_probe_cleanup(&tu->tp);
  310. kfree(tu->filename);
  311. free_percpu(tu->nhits);
  312. kfree(tu);
  313. }
  314. static struct trace_uprobe *find_probe_event(const char *event, const char *group)
  315. {
  316. struct dyn_event *pos;
  317. struct trace_uprobe *tu;
  318. for_each_trace_uprobe(tu, pos)
  319. if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&
  320. strcmp(trace_probe_group_name(&tu->tp), group) == 0)
  321. return tu;
  322. return NULL;
  323. }
  324. /* Unregister a trace_uprobe and probe_event */
  325. static int unregister_trace_uprobe(struct trace_uprobe *tu)
  326. {
  327. int ret;
  328. if (trace_probe_has_sibling(&tu->tp))
  329. goto unreg;
  330. /* If there's a reference to the dynamic event */
  331. if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp)))
  332. return -EBUSY;
  333. ret = unregister_uprobe_event(tu);
  334. if (ret)
  335. return ret;
  336. unreg:
  337. dyn_event_remove(&tu->devent);
  338. trace_probe_unlink(&tu->tp);
  339. free_trace_uprobe(tu);
  340. return 0;
  341. }
  342. static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig,
  343. struct trace_uprobe *comp)
  344. {
  345. struct trace_probe_event *tpe = orig->tp.event;
  346. struct inode *comp_inode = d_real_inode(comp->path.dentry);
  347. int i;
  348. list_for_each_entry(orig, &tpe->probes, tp.list) {
  349. if (comp_inode != d_real_inode(orig->path.dentry) ||
  350. comp->offset != orig->offset)
  351. continue;
  352. /*
  353. * trace_probe_compare_arg_type() ensured that nr_args and
  354. * each argument name and type are same. Let's compare comm.
  355. */
  356. for (i = 0; i < orig->tp.nr_args; i++) {
  357. if (strcmp(orig->tp.args[i].comm,
  358. comp->tp.args[i].comm))
  359. break;
  360. }
  361. if (i == orig->tp.nr_args)
  362. return true;
  363. }
  364. return false;
  365. }
  366. static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to)
  367. {
  368. int ret;
  369. ret = trace_probe_compare_arg_type(&tu->tp, &to->tp);
  370. if (ret) {
  371. /* Note that argument starts index = 2 */
  372. trace_probe_log_set_index(ret + 1);
  373. trace_probe_log_err(0, DIFF_ARG_TYPE);
  374. return -EEXIST;
  375. }
  376. if (trace_uprobe_has_same_uprobe(to, tu)) {
  377. trace_probe_log_set_index(0);
  378. trace_probe_log_err(0, SAME_PROBE);
  379. return -EEXIST;
  380. }
  381. /* Append to existing event */
  382. ret = trace_probe_append(&tu->tp, &to->tp);
  383. if (!ret)
  384. dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
  385. return ret;
  386. }
  387. /*
  388. * Uprobe with multiple reference counter is not allowed. i.e.
  389. * If inode and offset matches, reference counter offset *must*
  390. * match as well. Though, there is one exception: If user is
  391. * replacing old trace_uprobe with new one(same group/event),
  392. * then we allow same uprobe with new reference counter as far
  393. * as the new one does not conflict with any other existing
  394. * ones.
  395. */
  396. static int validate_ref_ctr_offset(struct trace_uprobe *new)
  397. {
  398. struct dyn_event *pos;
  399. struct trace_uprobe *tmp;
  400. struct inode *new_inode = d_real_inode(new->path.dentry);
  401. for_each_trace_uprobe(tmp, pos) {
  402. if (new_inode == d_real_inode(tmp->path.dentry) &&
  403. new->offset == tmp->offset &&
  404. new->ref_ctr_offset != tmp->ref_ctr_offset) {
  405. pr_warn("Reference counter offset mismatch.");
  406. return -EINVAL;
  407. }
  408. }
  409. return 0;
  410. }
  411. /* Register a trace_uprobe and probe_event */
  412. static int register_trace_uprobe(struct trace_uprobe *tu)
  413. {
  414. struct trace_uprobe *old_tu;
  415. int ret;
  416. mutex_lock(&event_mutex);
  417. ret = validate_ref_ctr_offset(tu);
  418. if (ret)
  419. goto end;
  420. /* register as an event */
  421. old_tu = find_probe_event(trace_probe_name(&tu->tp),
  422. trace_probe_group_name(&tu->tp));
  423. if (old_tu) {
  424. if (is_ret_probe(tu) != is_ret_probe(old_tu)) {
  425. trace_probe_log_set_index(0);
  426. trace_probe_log_err(0, DIFF_PROBE_TYPE);
  427. ret = -EEXIST;
  428. } else {
  429. ret = append_trace_uprobe(tu, old_tu);
  430. }
  431. goto end;
  432. }
  433. ret = register_uprobe_event(tu);
  434. if (ret) {
  435. if (ret == -EEXIST) {
  436. trace_probe_log_set_index(0);
  437. trace_probe_log_err(0, EVENT_EXIST);
  438. } else
  439. pr_warn("Failed to register probe event(%d)\n", ret);
  440. goto end;
  441. }
  442. dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
  443. end:
  444. mutex_unlock(&event_mutex);
  445. return ret;
  446. }
  447. /*
  448. * Argument syntax:
  449. * - Add uprobe: p|r[:[GRP/][EVENT]] PATH:OFFSET[%return][(REF)] [FETCHARGS]
  450. */
  451. static int __trace_uprobe_create(int argc, const char **argv)
  452. {
  453. struct trace_uprobe *tu;
  454. const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
  455. char *arg, *filename, *rctr, *rctr_end, *tmp;
  456. char buf[MAX_EVENT_NAME_LEN];
  457. char gbuf[MAX_EVENT_NAME_LEN];
  458. enum probe_print_type ptype;
  459. struct path path;
  460. unsigned long offset, ref_ctr_offset;
  461. bool is_return = false;
  462. int i, ret;
  463. ref_ctr_offset = 0;
  464. switch (argv[0][0]) {
  465. case 'r':
  466. is_return = true;
  467. break;
  468. case 'p':
  469. break;
  470. default:
  471. return -ECANCELED;
  472. }
  473. if (argc < 2)
  474. return -ECANCELED;
  475. if (argc - 2 > MAX_TRACE_ARGS)
  476. return -E2BIG;
  477. if (argv[0][1] == ':')
  478. event = &argv[0][2];
  479. if (!strchr(argv[1], '/'))
  480. return -ECANCELED;
  481. filename = kstrdup(argv[1], GFP_KERNEL);
  482. if (!filename)
  483. return -ENOMEM;
  484. /* Find the last occurrence, in case the path contains ':' too. */
  485. arg = strrchr(filename, ':');
  486. if (!arg || !isdigit(arg[1])) {
  487. kfree(filename);
  488. return -ECANCELED;
  489. }
  490. trace_probe_log_init("trace_uprobe", argc, argv);
  491. trace_probe_log_set_index(1); /* filename is the 2nd argument */
  492. *arg++ = '\0';
  493. ret = kern_path(filename, LOOKUP_FOLLOW, &path);
  494. if (ret) {
  495. trace_probe_log_err(0, FILE_NOT_FOUND);
  496. kfree(filename);
  497. trace_probe_log_clear();
  498. return ret;
  499. }
  500. if (!d_is_reg(path.dentry)) {
  501. trace_probe_log_err(0, NO_REGULAR_FILE);
  502. ret = -EINVAL;
  503. goto fail_address_parse;
  504. }
  505. /* Parse reference counter offset if specified. */
  506. rctr = strchr(arg, '(');
  507. if (rctr) {
  508. rctr_end = strchr(rctr, ')');
  509. if (!rctr_end) {
  510. ret = -EINVAL;
  511. rctr_end = rctr + strlen(rctr);
  512. trace_probe_log_err(rctr_end - filename,
  513. REFCNT_OPEN_BRACE);
  514. goto fail_address_parse;
  515. } else if (rctr_end[1] != '\0') {
  516. ret = -EINVAL;
  517. trace_probe_log_err(rctr_end + 1 - filename,
  518. BAD_REFCNT_SUFFIX);
  519. goto fail_address_parse;
  520. }
  521. *rctr++ = '\0';
  522. *rctr_end = '\0';
  523. ret = kstrtoul(rctr, 0, &ref_ctr_offset);
  524. if (ret) {
  525. trace_probe_log_err(rctr - filename, BAD_REFCNT);
  526. goto fail_address_parse;
  527. }
  528. }
  529. /* Check if there is %return suffix */
  530. tmp = strchr(arg, '%');
  531. if (tmp) {
  532. if (!strcmp(tmp, "%return")) {
  533. *tmp = '\0';
  534. is_return = true;
  535. } else {
  536. trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX);
  537. ret = -EINVAL;
  538. goto fail_address_parse;
  539. }
  540. }
  541. /* Parse uprobe offset. */
  542. ret = kstrtoul(arg, 0, &offset);
  543. if (ret) {
  544. trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
  545. goto fail_address_parse;
  546. }
  547. /* setup a probe */
  548. trace_probe_log_set_index(0);
  549. if (event) {
  550. ret = traceprobe_parse_event_name(&event, &group, gbuf,
  551. event - argv[0]);
  552. if (ret)
  553. goto fail_address_parse;
  554. }
  555. if (!event) {
  556. char *tail;
  557. char *ptr;
  558. tail = kstrdup(kbasename(filename), GFP_KERNEL);
  559. if (!tail) {
  560. ret = -ENOMEM;
  561. goto fail_address_parse;
  562. }
  563. ptr = strpbrk(tail, ".-_");
  564. if (ptr)
  565. *ptr = '\0';
  566. snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
  567. event = buf;
  568. kfree(tail);
  569. }
  570. argc -= 2;
  571. argv += 2;
  572. tu = alloc_trace_uprobe(group, event, argc, is_return);
  573. if (IS_ERR(tu)) {
  574. ret = PTR_ERR(tu);
  575. /* This must return -ENOMEM otherwise there is a bug */
  576. WARN_ON_ONCE(ret != -ENOMEM);
  577. goto fail_address_parse;
  578. }
  579. tu->offset = offset;
  580. tu->ref_ctr_offset = ref_ctr_offset;
  581. tu->path = path;
  582. tu->filename = filename;
  583. /* parse arguments */
  584. for (i = 0; i < argc; i++) {
  585. struct traceprobe_parse_context ctx = {
  586. .flags = (is_return ? TPARG_FL_RETURN : 0) | TPARG_FL_USER,
  587. };
  588. trace_probe_log_set_index(i + 2);
  589. ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], &ctx);
  590. traceprobe_finish_parse(&ctx);
  591. if (ret)
  592. goto error;
  593. }
  594. ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
  595. ret = traceprobe_set_print_fmt(&tu->tp, ptype);
  596. if (ret < 0)
  597. goto error;
  598. ret = register_trace_uprobe(tu);
  599. if (!ret)
  600. goto out;
  601. error:
  602. free_trace_uprobe(tu);
  603. out:
  604. trace_probe_log_clear();
  605. return ret;
  606. fail_address_parse:
  607. trace_probe_log_clear();
  608. path_put(&path);
  609. kfree(filename);
  610. return ret;
  611. }
  612. int trace_uprobe_create(const char *raw_command)
  613. {
  614. return trace_probe_create(raw_command, __trace_uprobe_create);
  615. }
  616. static int create_or_delete_trace_uprobe(const char *raw_command)
  617. {
  618. int ret;
  619. if (raw_command[0] == '-')
  620. return dyn_event_release(raw_command, &trace_uprobe_ops);
  621. ret = trace_uprobe_create(raw_command);
  622. return ret == -ECANCELED ? -EINVAL : ret;
  623. }
  624. static int trace_uprobe_release(struct dyn_event *ev)
  625. {
  626. struct trace_uprobe *tu = to_trace_uprobe(ev);
  627. return unregister_trace_uprobe(tu);
  628. }
  629. /* Probes listing interfaces */
  630. static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
  631. {
  632. struct trace_uprobe *tu = to_trace_uprobe(ev);
  633. char c = is_ret_probe(tu) ? 'r' : 'p';
  634. int i;
  635. seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp),
  636. trace_probe_name(&tu->tp), tu->filename,
  637. (int)(sizeof(void *) * 2), tu->offset);
  638. if (tu->ref_ctr_offset)
  639. seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
  640. for (i = 0; i < tu->tp.nr_args; i++)
  641. seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
  642. seq_putc(m, '\n');
  643. return 0;
  644. }
  645. static int probes_seq_show(struct seq_file *m, void *v)
  646. {
  647. struct dyn_event *ev = v;
  648. if (!is_trace_uprobe(ev))
  649. return 0;
  650. return trace_uprobe_show(m, ev);
  651. }
  652. static const struct seq_operations probes_seq_op = {
  653. .start = dyn_event_seq_start,
  654. .next = dyn_event_seq_next,
  655. .stop = dyn_event_seq_stop,
  656. .show = probes_seq_show
  657. };
  658. static int probes_open(struct inode *inode, struct file *file)
  659. {
  660. int ret;
  661. ret = security_locked_down(LOCKDOWN_TRACEFS);
  662. if (ret)
  663. return ret;
  664. if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
  665. ret = dyn_events_release_all(&trace_uprobe_ops);
  666. if (ret)
  667. return ret;
  668. }
  669. return seq_open(file, &probes_seq_op);
  670. }
  671. static ssize_t probes_write(struct file *file, const char __user *buffer,
  672. size_t count, loff_t *ppos)
  673. {
  674. return trace_parse_run_command(file, buffer, count, ppos,
  675. create_or_delete_trace_uprobe);
  676. }
  677. static const struct file_operations uprobe_events_ops = {
  678. .owner = THIS_MODULE,
  679. .open = probes_open,
  680. .read = seq_read,
  681. .llseek = seq_lseek,
  682. .release = seq_release,
  683. .write = probes_write,
  684. };
  685. /* Probes profiling interfaces */
  686. static int probes_profile_seq_show(struct seq_file *m, void *v)
  687. {
  688. struct dyn_event *ev = v;
  689. struct trace_uprobe *tu;
  690. unsigned long nhits;
  691. int cpu;
  692. if (!is_trace_uprobe(ev))
  693. return 0;
  694. tu = to_trace_uprobe(ev);
  695. nhits = 0;
  696. for_each_possible_cpu(cpu) {
  697. nhits += per_cpu(*tu->nhits, cpu);
  698. }
  699. seq_printf(m, " %s %-44s %15lu\n", tu->filename,
  700. trace_probe_name(&tu->tp), nhits);
  701. return 0;
  702. }
  703. static const struct seq_operations profile_seq_op = {
  704. .start = dyn_event_seq_start,
  705. .next = dyn_event_seq_next,
  706. .stop = dyn_event_seq_stop,
  707. .show = probes_profile_seq_show
  708. };
  709. static int profile_open(struct inode *inode, struct file *file)
  710. {
  711. int ret;
  712. ret = security_locked_down(LOCKDOWN_TRACEFS);
  713. if (ret)
  714. return ret;
  715. return seq_open(file, &profile_seq_op);
  716. }
  717. static const struct file_operations uprobe_profile_ops = {
  718. .owner = THIS_MODULE,
  719. .open = profile_open,
  720. .read = seq_read,
  721. .llseek = seq_lseek,
  722. .release = seq_release,
  723. };
  724. struct uprobe_cpu_buffer {
  725. struct mutex mutex;
  726. void *buf;
  727. int dsize;
  728. };
  729. static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
  730. static int uprobe_buffer_refcnt;
  731. #define MAX_UCB_BUFFER_SIZE PAGE_SIZE
  732. static int uprobe_buffer_init(void)
  733. {
  734. int cpu, err_cpu;
  735. uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
  736. if (uprobe_cpu_buffer == NULL)
  737. return -ENOMEM;
  738. for_each_possible_cpu(cpu) {
  739. struct page *p = alloc_pages_node(cpu_to_node(cpu),
  740. GFP_KERNEL, 0);
  741. if (p == NULL) {
  742. err_cpu = cpu;
  743. goto err;
  744. }
  745. per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
  746. mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
  747. }
  748. return 0;
  749. err:
  750. for_each_possible_cpu(cpu) {
  751. if (cpu == err_cpu)
  752. break;
  753. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
  754. }
  755. free_percpu(uprobe_cpu_buffer);
  756. return -ENOMEM;
  757. }
  758. static int uprobe_buffer_enable(void)
  759. {
  760. int ret = 0;
  761. BUG_ON(!mutex_is_locked(&event_mutex));
  762. if (uprobe_buffer_refcnt++ == 0) {
  763. ret = uprobe_buffer_init();
  764. if (ret < 0)
  765. uprobe_buffer_refcnt--;
  766. }
  767. return ret;
  768. }
  769. static void uprobe_buffer_disable(void)
  770. {
  771. int cpu;
  772. BUG_ON(!mutex_is_locked(&event_mutex));
  773. if (--uprobe_buffer_refcnt == 0) {
  774. for_each_possible_cpu(cpu)
  775. free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
  776. cpu)->buf);
  777. free_percpu(uprobe_cpu_buffer);
  778. uprobe_cpu_buffer = NULL;
  779. }
  780. }
  781. static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
  782. {
  783. struct uprobe_cpu_buffer *ucb;
  784. int cpu;
  785. cpu = raw_smp_processor_id();
  786. ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
  787. /*
  788. * Use per-cpu buffers for fastest access, but we might migrate
  789. * so the mutex makes sure we have sole access to it.
  790. */
  791. mutex_lock(&ucb->mutex);
  792. return ucb;
  793. }
  794. static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
  795. {
  796. if (!ucb)
  797. return;
  798. mutex_unlock(&ucb->mutex);
  799. }
  800. static struct uprobe_cpu_buffer *prepare_uprobe_buffer(struct trace_uprobe *tu,
  801. struct pt_regs *regs,
  802. struct uprobe_cpu_buffer **ucbp)
  803. {
  804. struct uprobe_cpu_buffer *ucb;
  805. int dsize, esize;
  806. if (*ucbp)
  807. return *ucbp;
  808. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  809. dsize = __get_data_size(&tu->tp, regs, NULL);
  810. ucb = uprobe_buffer_get();
  811. ucb->dsize = tu->tp.size + dsize;
  812. if (WARN_ON_ONCE(ucb->dsize > MAX_UCB_BUFFER_SIZE)) {
  813. ucb->dsize = MAX_UCB_BUFFER_SIZE;
  814. dsize = MAX_UCB_BUFFER_SIZE - tu->tp.size;
  815. }
  816. store_trace_args(ucb->buf, &tu->tp, regs, NULL, esize, dsize);
  817. *ucbp = ucb;
  818. return ucb;
  819. }
  820. static void __uprobe_trace_func(struct trace_uprobe *tu,
  821. unsigned long func, struct pt_regs *regs,
  822. struct uprobe_cpu_buffer *ucb,
  823. struct trace_event_file *trace_file)
  824. {
  825. struct uprobe_trace_entry_head *entry;
  826. struct trace_event_buffer fbuffer;
  827. void *data;
  828. int size, esize;
  829. struct trace_event_call *call = trace_probe_event_call(&tu->tp);
  830. WARN_ON(call != trace_file->event_call);
  831. if (trace_trigger_soft_disabled(trace_file))
  832. return;
  833. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  834. size = esize + ucb->dsize;
  835. entry = trace_event_buffer_reserve(&fbuffer, trace_file, size);
  836. if (!entry)
  837. return;
  838. if (is_ret_probe(tu)) {
  839. entry->vaddr[0] = func;
  840. entry->vaddr[1] = instruction_pointer(regs);
  841. data = DATAOF_TRACE_ENTRY(entry, true);
  842. } else {
  843. entry->vaddr[0] = instruction_pointer(regs);
  844. data = DATAOF_TRACE_ENTRY(entry, false);
  845. }
  846. memcpy(data, ucb->buf, ucb->dsize);
  847. trace_event_buffer_commit(&fbuffer);
  848. }
  849. /* uprobe handler */
  850. static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
  851. struct uprobe_cpu_buffer **ucbp)
  852. {
  853. struct event_file_link *link;
  854. struct uprobe_cpu_buffer *ucb;
  855. if (is_ret_probe(tu))
  856. return 0;
  857. ucb = prepare_uprobe_buffer(tu, regs, ucbp);
  858. rcu_read_lock();
  859. trace_probe_for_each_link_rcu(link, &tu->tp)
  860. __uprobe_trace_func(tu, 0, regs, ucb, link->file);
  861. rcu_read_unlock();
  862. return 0;
  863. }
  864. static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
  865. struct pt_regs *regs,
  866. struct uprobe_cpu_buffer **ucbp)
  867. {
  868. struct event_file_link *link;
  869. struct uprobe_cpu_buffer *ucb;
  870. ucb = prepare_uprobe_buffer(tu, regs, ucbp);
  871. rcu_read_lock();
  872. trace_probe_for_each_link_rcu(link, &tu->tp)
  873. __uprobe_trace_func(tu, func, regs, ucb, link->file);
  874. rcu_read_unlock();
  875. }
  876. /* Event entry printers */
  877. static enum print_line_t
  878. print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
  879. {
  880. struct uprobe_trace_entry_head *entry;
  881. struct trace_seq *s = &iter->seq;
  882. struct trace_uprobe *tu;
  883. u8 *data;
  884. entry = (struct uprobe_trace_entry_head *)iter->ent;
  885. tu = trace_uprobe_primary_from_call(
  886. container_of(event, struct trace_event_call, event));
  887. if (unlikely(!tu))
  888. goto out;
  889. if (is_ret_probe(tu)) {
  890. trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
  891. trace_probe_name(&tu->tp),
  892. entry->vaddr[1], entry->vaddr[0]);
  893. data = DATAOF_TRACE_ENTRY(entry, true);
  894. } else {
  895. trace_seq_printf(s, "%s: (0x%lx)",
  896. trace_probe_name(&tu->tp),
  897. entry->vaddr[0]);
  898. data = DATAOF_TRACE_ENTRY(entry, false);
  899. }
  900. if (trace_probe_print_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
  901. goto out;
  902. trace_seq_putc(s, '\n');
  903. out:
  904. return trace_handle_return(s);
  905. }
  906. typedef bool (*filter_func_t)(struct uprobe_consumer *self, struct mm_struct *mm);
  907. static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter)
  908. {
  909. struct inode *inode = d_real_inode(tu->path.dentry);
  910. struct uprobe *uprobe;
  911. tu->consumer.filter = filter;
  912. uprobe = uprobe_register(inode, tu->offset, tu->ref_ctr_offset, &tu->consumer);
  913. if (IS_ERR(uprobe))
  914. return PTR_ERR(uprobe);
  915. tu->uprobe = uprobe;
  916. return 0;
  917. }
  918. static void __probe_event_disable(struct trace_probe *tp)
  919. {
  920. struct trace_uprobe *tu;
  921. bool sync = false;
  922. tu = container_of(tp, struct trace_uprobe, tp);
  923. WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
  924. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  925. if (!tu->uprobe)
  926. continue;
  927. uprobe_unregister_nosync(tu->uprobe, &tu->consumer);
  928. sync = true;
  929. tu->uprobe = NULL;
  930. }
  931. if (sync)
  932. uprobe_unregister_sync();
  933. }
  934. static int probe_event_enable(struct trace_event_call *call,
  935. struct trace_event_file *file, filter_func_t filter)
  936. {
  937. struct trace_probe *tp;
  938. struct trace_uprobe *tu;
  939. bool enabled;
  940. int ret;
  941. tp = trace_probe_primary_from_call(call);
  942. if (WARN_ON_ONCE(!tp))
  943. return -ENODEV;
  944. enabled = trace_probe_is_enabled(tp);
  945. /* This may also change "enabled" state */
  946. if (file) {
  947. if (trace_probe_test_flag(tp, TP_FLAG_PROFILE))
  948. return -EINTR;
  949. ret = trace_probe_add_file(tp, file);
  950. if (ret < 0)
  951. return ret;
  952. } else {
  953. if (trace_probe_test_flag(tp, TP_FLAG_TRACE))
  954. return -EINTR;
  955. trace_probe_set_flag(tp, TP_FLAG_PROFILE);
  956. }
  957. tu = container_of(tp, struct trace_uprobe, tp);
  958. WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
  959. if (enabled)
  960. return 0;
  961. ret = uprobe_buffer_enable();
  962. if (ret)
  963. goto err_flags;
  964. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  965. ret = trace_uprobe_enable(tu, filter);
  966. if (ret) {
  967. __probe_event_disable(tp);
  968. goto err_buffer;
  969. }
  970. }
  971. return 0;
  972. err_buffer:
  973. uprobe_buffer_disable();
  974. err_flags:
  975. if (file)
  976. trace_probe_remove_file(tp, file);
  977. else
  978. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  979. return ret;
  980. }
  981. static void probe_event_disable(struct trace_event_call *call,
  982. struct trace_event_file *file)
  983. {
  984. struct trace_probe *tp;
  985. tp = trace_probe_primary_from_call(call);
  986. if (WARN_ON_ONCE(!tp))
  987. return;
  988. if (!trace_probe_is_enabled(tp))
  989. return;
  990. if (file) {
  991. if (trace_probe_remove_file(tp, file) < 0)
  992. return;
  993. if (trace_probe_is_enabled(tp))
  994. return;
  995. } else
  996. trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
  997. __probe_event_disable(tp);
  998. uprobe_buffer_disable();
  999. }
  1000. static int uprobe_event_define_fields(struct trace_event_call *event_call)
  1001. {
  1002. int ret, size;
  1003. struct uprobe_trace_entry_head field;
  1004. struct trace_uprobe *tu;
  1005. tu = trace_uprobe_primary_from_call(event_call);
  1006. if (unlikely(!tu))
  1007. return -ENODEV;
  1008. if (is_ret_probe(tu)) {
  1009. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
  1010. DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
  1011. size = SIZEOF_TRACE_ENTRY(true);
  1012. } else {
  1013. DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
  1014. size = SIZEOF_TRACE_ENTRY(false);
  1015. }
  1016. return traceprobe_define_arg_fields(event_call, size, &tu->tp);
  1017. }
  1018. #ifdef CONFIG_PERF_EVENTS
  1019. static bool
  1020. __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
  1021. {
  1022. struct perf_event *event;
  1023. list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
  1024. if (event->hw.target->mm == mm)
  1025. return true;
  1026. }
  1027. return false;
  1028. }
  1029. static inline bool
  1030. trace_uprobe_filter_event(struct trace_uprobe_filter *filter,
  1031. struct perf_event *event)
  1032. {
  1033. return __uprobe_perf_filter(filter, event->hw.target->mm);
  1034. }
  1035. static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter,
  1036. struct perf_event *event)
  1037. {
  1038. bool done;
  1039. write_lock(&filter->rwlock);
  1040. if (event->hw.target) {
  1041. list_del(&event->hw.tp_list);
  1042. done = filter->nr_systemwide ||
  1043. (event->hw.target->flags & PF_EXITING) ||
  1044. trace_uprobe_filter_event(filter, event);
  1045. } else {
  1046. filter->nr_systemwide--;
  1047. done = filter->nr_systemwide;
  1048. }
  1049. write_unlock(&filter->rwlock);
  1050. return done;
  1051. }
  1052. /* This returns true if the filter always covers target mm */
  1053. static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter,
  1054. struct perf_event *event)
  1055. {
  1056. bool done;
  1057. write_lock(&filter->rwlock);
  1058. if (event->hw.target) {
  1059. /*
  1060. * event->parent != NULL means copy_process(), we can avoid
  1061. * uprobe_apply(). current->mm must be probed and we can rely
  1062. * on dup_mmap() which preserves the already installed bp's.
  1063. *
  1064. * attr.enable_on_exec means that exec/mmap will install the
  1065. * breakpoints we need.
  1066. */
  1067. done = filter->nr_systemwide ||
  1068. event->parent || event->attr.enable_on_exec ||
  1069. trace_uprobe_filter_event(filter, event);
  1070. list_add(&event->hw.tp_list, &filter->perf_events);
  1071. } else {
  1072. done = filter->nr_systemwide;
  1073. filter->nr_systemwide++;
  1074. }
  1075. write_unlock(&filter->rwlock);
  1076. return done;
  1077. }
  1078. static int uprobe_perf_close(struct trace_event_call *call,
  1079. struct perf_event *event)
  1080. {
  1081. struct trace_probe *tp;
  1082. struct trace_uprobe *tu;
  1083. int ret = 0;
  1084. tp = trace_probe_primary_from_call(call);
  1085. if (WARN_ON_ONCE(!tp))
  1086. return -ENODEV;
  1087. tu = container_of(tp, struct trace_uprobe, tp);
  1088. if (trace_uprobe_filter_remove(tu->tp.event->filter, event))
  1089. return 0;
  1090. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  1091. ret = uprobe_apply(tu->uprobe, &tu->consumer, false);
  1092. if (ret)
  1093. break;
  1094. }
  1095. return ret;
  1096. }
  1097. static int uprobe_perf_open(struct trace_event_call *call,
  1098. struct perf_event *event)
  1099. {
  1100. struct trace_probe *tp;
  1101. struct trace_uprobe *tu;
  1102. int err = 0;
  1103. tp = trace_probe_primary_from_call(call);
  1104. if (WARN_ON_ONCE(!tp))
  1105. return -ENODEV;
  1106. tu = container_of(tp, struct trace_uprobe, tp);
  1107. if (trace_uprobe_filter_add(tu->tp.event->filter, event))
  1108. return 0;
  1109. list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
  1110. err = uprobe_apply(tu->uprobe, &tu->consumer, true);
  1111. if (err) {
  1112. uprobe_perf_close(call, event);
  1113. break;
  1114. }
  1115. }
  1116. return err;
  1117. }
  1118. static bool uprobe_perf_filter(struct uprobe_consumer *uc, struct mm_struct *mm)
  1119. {
  1120. struct trace_uprobe_filter *filter;
  1121. struct trace_uprobe *tu;
  1122. int ret;
  1123. tu = container_of(uc, struct trace_uprobe, consumer);
  1124. filter = tu->tp.event->filter;
  1125. /*
  1126. * speculative short-circuiting check to avoid unnecessarily taking
  1127. * filter->rwlock below, if the uprobe has system-wide consumer
  1128. */
  1129. if (READ_ONCE(filter->nr_systemwide))
  1130. return true;
  1131. read_lock(&filter->rwlock);
  1132. ret = __uprobe_perf_filter(filter, mm);
  1133. read_unlock(&filter->rwlock);
  1134. return ret;
  1135. }
  1136. static void __uprobe_perf_func(struct trace_uprobe *tu,
  1137. unsigned long func, struct pt_regs *regs,
  1138. struct uprobe_cpu_buffer **ucbp)
  1139. {
  1140. struct trace_event_call *call = trace_probe_event_call(&tu->tp);
  1141. struct uprobe_trace_entry_head *entry;
  1142. struct uprobe_cpu_buffer *ucb;
  1143. struct hlist_head *head;
  1144. void *data;
  1145. int size, esize;
  1146. int rctx;
  1147. #ifdef CONFIG_BPF_EVENTS
  1148. if (bpf_prog_array_valid(call)) {
  1149. const struct bpf_prog_array *array;
  1150. u32 ret;
  1151. rcu_read_lock_trace();
  1152. array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held());
  1153. ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run);
  1154. rcu_read_unlock_trace();
  1155. if (!ret)
  1156. return;
  1157. }
  1158. #endif /* CONFIG_BPF_EVENTS */
  1159. esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
  1160. ucb = prepare_uprobe_buffer(tu, regs, ucbp);
  1161. size = esize + ucb->dsize;
  1162. size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
  1163. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
  1164. return;
  1165. preempt_disable();
  1166. head = this_cpu_ptr(call->perf_events);
  1167. if (hlist_empty(head))
  1168. goto out;
  1169. entry = perf_trace_buf_alloc(size, NULL, &rctx);
  1170. if (!entry)
  1171. goto out;
  1172. if (is_ret_probe(tu)) {
  1173. entry->vaddr[0] = func;
  1174. entry->vaddr[1] = instruction_pointer(regs);
  1175. data = DATAOF_TRACE_ENTRY(entry, true);
  1176. } else {
  1177. entry->vaddr[0] = instruction_pointer(regs);
  1178. data = DATAOF_TRACE_ENTRY(entry, false);
  1179. }
  1180. memcpy(data, ucb->buf, ucb->dsize);
  1181. if (size - esize > ucb->dsize)
  1182. memset(data + ucb->dsize, 0, size - esize - ucb->dsize);
  1183. perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
  1184. head, NULL);
  1185. out:
  1186. preempt_enable();
  1187. }
  1188. /* uprobe profile handler */
  1189. static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
  1190. struct uprobe_cpu_buffer **ucbp)
  1191. {
  1192. if (!uprobe_perf_filter(&tu->consumer, current->mm))
  1193. return UPROBE_HANDLER_REMOVE;
  1194. if (!is_ret_probe(tu))
  1195. __uprobe_perf_func(tu, 0, regs, ucbp);
  1196. return 0;
  1197. }
  1198. static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
  1199. struct pt_regs *regs,
  1200. struct uprobe_cpu_buffer **ucbp)
  1201. {
  1202. __uprobe_perf_func(tu, func, regs, ucbp);
  1203. }
  1204. int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
  1205. const char **filename, u64 *probe_offset,
  1206. u64 *probe_addr, bool perf_type_tracepoint)
  1207. {
  1208. const char *pevent = trace_event_name(event->tp_event);
  1209. const char *group = event->tp_event->class->system;
  1210. struct trace_uprobe *tu;
  1211. if (perf_type_tracepoint)
  1212. tu = find_probe_event(pevent, group);
  1213. else
  1214. tu = trace_uprobe_primary_from_call(event->tp_event);
  1215. if (!tu)
  1216. return -EINVAL;
  1217. *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
  1218. : BPF_FD_TYPE_UPROBE;
  1219. *filename = tu->filename;
  1220. *probe_offset = tu->offset;
  1221. *probe_addr = 0;
  1222. return 0;
  1223. }
  1224. #endif /* CONFIG_PERF_EVENTS */
  1225. static int
  1226. trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
  1227. void *data)
  1228. {
  1229. struct trace_event_file *file = data;
  1230. switch (type) {
  1231. case TRACE_REG_REGISTER:
  1232. return probe_event_enable(event, file, NULL);
  1233. case TRACE_REG_UNREGISTER:
  1234. probe_event_disable(event, file);
  1235. return 0;
  1236. #ifdef CONFIG_PERF_EVENTS
  1237. case TRACE_REG_PERF_REGISTER:
  1238. return probe_event_enable(event, NULL, uprobe_perf_filter);
  1239. case TRACE_REG_PERF_UNREGISTER:
  1240. probe_event_disable(event, NULL);
  1241. return 0;
  1242. case TRACE_REG_PERF_OPEN:
  1243. return uprobe_perf_open(event, data);
  1244. case TRACE_REG_PERF_CLOSE:
  1245. return uprobe_perf_close(event, data);
  1246. #endif
  1247. default:
  1248. return 0;
  1249. }
  1250. }
  1251. static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
  1252. {
  1253. struct trace_uprobe *tu;
  1254. struct uprobe_dispatch_data udd;
  1255. struct uprobe_cpu_buffer *ucb = NULL;
  1256. int ret = 0;
  1257. tu = container_of(con, struct trace_uprobe, consumer);
  1258. this_cpu_inc(*tu->nhits);
  1259. udd.tu = tu;
  1260. udd.bp_addr = instruction_pointer(regs);
  1261. current->utask->vaddr = (unsigned long) &udd;
  1262. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1263. return 0;
  1264. if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
  1265. ret |= uprobe_trace_func(tu, regs, &ucb);
  1266. #ifdef CONFIG_PERF_EVENTS
  1267. if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
  1268. ret |= uprobe_perf_func(tu, regs, &ucb);
  1269. #endif
  1270. uprobe_buffer_put(ucb);
  1271. return ret;
  1272. }
  1273. static int uretprobe_dispatcher(struct uprobe_consumer *con,
  1274. unsigned long func, struct pt_regs *regs)
  1275. {
  1276. struct trace_uprobe *tu;
  1277. struct uprobe_dispatch_data udd;
  1278. struct uprobe_cpu_buffer *ucb = NULL;
  1279. tu = container_of(con, struct trace_uprobe, consumer);
  1280. udd.tu = tu;
  1281. udd.bp_addr = func;
  1282. current->utask->vaddr = (unsigned long) &udd;
  1283. if (WARN_ON_ONCE(!uprobe_cpu_buffer))
  1284. return 0;
  1285. if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
  1286. uretprobe_trace_func(tu, func, regs, &ucb);
  1287. #ifdef CONFIG_PERF_EVENTS
  1288. if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
  1289. uretprobe_perf_func(tu, func, regs, &ucb);
  1290. #endif
  1291. uprobe_buffer_put(ucb);
  1292. return 0;
  1293. }
  1294. static struct trace_event_functions uprobe_funcs = {
  1295. .trace = print_uprobe_event
  1296. };
  1297. static struct trace_event_fields uprobe_fields_array[] = {
  1298. { .type = TRACE_FUNCTION_TYPE,
  1299. .define_fields = uprobe_event_define_fields },
  1300. {}
  1301. };
  1302. static inline void init_trace_event_call(struct trace_uprobe *tu)
  1303. {
  1304. struct trace_event_call *call = trace_probe_event_call(&tu->tp);
  1305. call->event.funcs = &uprobe_funcs;
  1306. call->class->fields_array = uprobe_fields_array;
  1307. call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY;
  1308. call->class->reg = trace_uprobe_register;
  1309. }
  1310. static int register_uprobe_event(struct trace_uprobe *tu)
  1311. {
  1312. init_trace_event_call(tu);
  1313. return trace_probe_register_event_call(&tu->tp);
  1314. }
  1315. static int unregister_uprobe_event(struct trace_uprobe *tu)
  1316. {
  1317. return trace_probe_unregister_event_call(&tu->tp);
  1318. }
  1319. #ifdef CONFIG_PERF_EVENTS
  1320. struct trace_event_call *
  1321. create_local_trace_uprobe(char *name, unsigned long offs,
  1322. unsigned long ref_ctr_offset, bool is_return)
  1323. {
  1324. enum probe_print_type ptype;
  1325. struct trace_uprobe *tu;
  1326. struct path path;
  1327. int ret;
  1328. ret = kern_path(name, LOOKUP_FOLLOW, &path);
  1329. if (ret)
  1330. return ERR_PTR(ret);
  1331. if (!d_is_reg(path.dentry)) {
  1332. path_put(&path);
  1333. return ERR_PTR(-EINVAL);
  1334. }
  1335. /*
  1336. * local trace_kprobes are not added to dyn_event, so they are never
  1337. * searched in find_trace_kprobe(). Therefore, there is no concern of
  1338. * duplicated name "DUMMY_EVENT" here.
  1339. */
  1340. tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
  1341. is_return);
  1342. if (IS_ERR(tu)) {
  1343. pr_info("Failed to allocate trace_uprobe.(%d)\n",
  1344. (int)PTR_ERR(tu));
  1345. path_put(&path);
  1346. return ERR_CAST(tu);
  1347. }
  1348. tu->offset = offs;
  1349. tu->path = path;
  1350. tu->ref_ctr_offset = ref_ctr_offset;
  1351. tu->filename = kstrdup(name, GFP_KERNEL);
  1352. if (!tu->filename) {
  1353. ret = -ENOMEM;
  1354. goto error;
  1355. }
  1356. init_trace_event_call(tu);
  1357. ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
  1358. if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) {
  1359. ret = -ENOMEM;
  1360. goto error;
  1361. }
  1362. return trace_probe_event_call(&tu->tp);
  1363. error:
  1364. free_trace_uprobe(tu);
  1365. return ERR_PTR(ret);
  1366. }
  1367. void destroy_local_trace_uprobe(struct trace_event_call *event_call)
  1368. {
  1369. struct trace_uprobe *tu;
  1370. tu = trace_uprobe_primary_from_call(event_call);
  1371. free_trace_uprobe(tu);
  1372. }
  1373. #endif /* CONFIG_PERF_EVENTS */
  1374. /* Make a trace interface for controlling probe points */
  1375. static __init int init_uprobe_trace(void)
  1376. {
  1377. int ret;
  1378. ret = dyn_event_register(&trace_uprobe_ops);
  1379. if (ret)
  1380. return ret;
  1381. ret = tracing_init_dentry();
  1382. if (ret)
  1383. return 0;
  1384. trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL,
  1385. NULL, &uprobe_events_ops);
  1386. /* Profile interface */
  1387. trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL,
  1388. NULL, &uprobe_profile_ops);
  1389. return 0;
  1390. }
  1391. fs_initcall(init_uprobe_trace);