bpf-lirc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0
  2. // bpf-lirc.c - handles bpf
  3. //
  4. // Copyright (C) 2018 Sean Young <sean@mess.org>
  5. #include <linux/bpf.h>
  6. #include <linux/filter.h>
  7. #include <linux/bpf_lirc.h>
  8. #include "rc-core-priv.h"
  9. #define lirc_rcu_dereference(p) \
  10. rcu_dereference_protected(p, lockdep_is_held(&ir_raw_handler_lock))
  11. /*
  12. * BPF interface for raw IR
  13. */
  14. const struct bpf_prog_ops lirc_mode2_prog_ops = {
  15. };
  16. BPF_CALL_1(bpf_rc_repeat, u32*, sample)
  17. {
  18. struct ir_raw_event_ctrl *ctrl;
  19. ctrl = container_of(sample, struct ir_raw_event_ctrl, bpf_sample);
  20. rc_repeat(ctrl->dev);
  21. return 0;
  22. }
  23. static const struct bpf_func_proto rc_repeat_proto = {
  24. .func = bpf_rc_repeat,
  25. .gpl_only = true, /* rc_repeat is EXPORT_SYMBOL_GPL */
  26. .ret_type = RET_INTEGER,
  27. .arg1_type = ARG_PTR_TO_CTX,
  28. };
  29. BPF_CALL_4(bpf_rc_keydown, u32*, sample, u32, protocol, u64, scancode,
  30. u32, toggle)
  31. {
  32. struct ir_raw_event_ctrl *ctrl;
  33. ctrl = container_of(sample, struct ir_raw_event_ctrl, bpf_sample);
  34. rc_keydown(ctrl->dev, protocol, scancode, toggle != 0);
  35. return 0;
  36. }
  37. static const struct bpf_func_proto rc_keydown_proto = {
  38. .func = bpf_rc_keydown,
  39. .gpl_only = true, /* rc_keydown is EXPORT_SYMBOL_GPL */
  40. .ret_type = RET_INTEGER,
  41. .arg1_type = ARG_PTR_TO_CTX,
  42. .arg2_type = ARG_ANYTHING,
  43. .arg3_type = ARG_ANYTHING,
  44. .arg4_type = ARG_ANYTHING,
  45. };
  46. BPF_CALL_3(bpf_rc_pointer_rel, u32*, sample, s32, rel_x, s32, rel_y)
  47. {
  48. struct ir_raw_event_ctrl *ctrl;
  49. ctrl = container_of(sample, struct ir_raw_event_ctrl, bpf_sample);
  50. input_report_rel(ctrl->dev->input_dev, REL_X, rel_x);
  51. input_report_rel(ctrl->dev->input_dev, REL_Y, rel_y);
  52. input_sync(ctrl->dev->input_dev);
  53. return 0;
  54. }
  55. static const struct bpf_func_proto rc_pointer_rel_proto = {
  56. .func = bpf_rc_pointer_rel,
  57. .gpl_only = true,
  58. .ret_type = RET_INTEGER,
  59. .arg1_type = ARG_PTR_TO_CTX,
  60. .arg2_type = ARG_ANYTHING,
  61. .arg3_type = ARG_ANYTHING,
  62. };
  63. static const struct bpf_func_proto *
  64. lirc_mode2_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
  65. {
  66. switch (func_id) {
  67. case BPF_FUNC_rc_repeat:
  68. return &rc_repeat_proto;
  69. case BPF_FUNC_rc_keydown:
  70. return &rc_keydown_proto;
  71. case BPF_FUNC_rc_pointer_rel:
  72. return &rc_pointer_rel_proto;
  73. case BPF_FUNC_map_lookup_elem:
  74. return &bpf_map_lookup_elem_proto;
  75. case BPF_FUNC_map_update_elem:
  76. return &bpf_map_update_elem_proto;
  77. case BPF_FUNC_map_delete_elem:
  78. return &bpf_map_delete_elem_proto;
  79. case BPF_FUNC_map_push_elem:
  80. return &bpf_map_push_elem_proto;
  81. case BPF_FUNC_map_pop_elem:
  82. return &bpf_map_pop_elem_proto;
  83. case BPF_FUNC_map_peek_elem:
  84. return &bpf_map_peek_elem_proto;
  85. case BPF_FUNC_ktime_get_ns:
  86. return &bpf_ktime_get_ns_proto;
  87. case BPF_FUNC_ktime_get_boot_ns:
  88. return &bpf_ktime_get_boot_ns_proto;
  89. case BPF_FUNC_tail_call:
  90. return &bpf_tail_call_proto;
  91. case BPF_FUNC_get_prandom_u32:
  92. return &bpf_get_prandom_u32_proto;
  93. case BPF_FUNC_trace_printk:
  94. if (perfmon_capable())
  95. return bpf_get_trace_printk_proto();
  96. fallthrough;
  97. default:
  98. return NULL;
  99. }
  100. }
  101. static bool lirc_mode2_is_valid_access(int off, int size,
  102. enum bpf_access_type type,
  103. const struct bpf_prog *prog,
  104. struct bpf_insn_access_aux *info)
  105. {
  106. /* We have one field of u32 */
  107. return type == BPF_READ && off == 0 && size == sizeof(u32);
  108. }
  109. const struct bpf_verifier_ops lirc_mode2_verifier_ops = {
  110. .get_func_proto = lirc_mode2_func_proto,
  111. .is_valid_access = lirc_mode2_is_valid_access
  112. };
  113. #define BPF_MAX_PROGS 64
  114. static int lirc_bpf_attach(struct rc_dev *rcdev, struct bpf_prog *prog)
  115. {
  116. struct bpf_prog_array *old_array;
  117. struct bpf_prog_array *new_array;
  118. struct ir_raw_event_ctrl *raw;
  119. int ret;
  120. if (rcdev->driver_type != RC_DRIVER_IR_RAW)
  121. return -EINVAL;
  122. ret = mutex_lock_interruptible(&ir_raw_handler_lock);
  123. if (ret)
  124. return ret;
  125. raw = rcdev->raw;
  126. if (!raw) {
  127. ret = -ENODEV;
  128. goto unlock;
  129. }
  130. old_array = lirc_rcu_dereference(raw->progs);
  131. if (old_array && bpf_prog_array_length(old_array) >= BPF_MAX_PROGS) {
  132. ret = -E2BIG;
  133. goto unlock;
  134. }
  135. ret = bpf_prog_array_copy(old_array, NULL, prog, &new_array);
  136. if (ret < 0)
  137. goto unlock;
  138. rcu_assign_pointer(raw->progs, new_array);
  139. bpf_prog_array_free(old_array);
  140. unlock:
  141. mutex_unlock(&ir_raw_handler_lock);
  142. return ret;
  143. }
  144. static int lirc_bpf_detach(struct rc_dev *rcdev, struct bpf_prog *prog)
  145. {
  146. struct bpf_prog_array *old_array;
  147. struct bpf_prog_array *new_array;
  148. struct ir_raw_event_ctrl *raw;
  149. int ret;
  150. if (rcdev->driver_type != RC_DRIVER_IR_RAW)
  151. return -EINVAL;
  152. ret = mutex_lock_interruptible(&ir_raw_handler_lock);
  153. if (ret)
  154. return ret;
  155. raw = rcdev->raw;
  156. if (!raw) {
  157. ret = -ENODEV;
  158. goto unlock;
  159. }
  160. old_array = lirc_rcu_dereference(raw->progs);
  161. ret = bpf_prog_array_copy(old_array, prog, NULL, &new_array);
  162. /*
  163. * Do not use bpf_prog_array_delete_safe() as we would end up
  164. * with a dummy entry in the array, and the we would free the
  165. * dummy in lirc_bpf_free()
  166. */
  167. if (ret)
  168. goto unlock;
  169. rcu_assign_pointer(raw->progs, new_array);
  170. bpf_prog_array_free(old_array);
  171. bpf_prog_put(prog);
  172. unlock:
  173. mutex_unlock(&ir_raw_handler_lock);
  174. return ret;
  175. }
  176. void lirc_bpf_run(struct rc_dev *rcdev, u32 sample)
  177. {
  178. struct ir_raw_event_ctrl *raw = rcdev->raw;
  179. raw->bpf_sample = sample;
  180. if (raw->progs)
  181. BPF_PROG_RUN_ARRAY(raw->progs, &raw->bpf_sample, BPF_PROG_RUN);
  182. }
  183. /*
  184. * This should be called once the rc thread has been stopped, so there can be
  185. * no concurrent bpf execution.
  186. *
  187. * Should be called with the ir_raw_handler_lock held.
  188. */
  189. void lirc_bpf_free(struct rc_dev *rcdev)
  190. {
  191. struct bpf_prog_array_item *item;
  192. struct bpf_prog_array *array;
  193. array = lirc_rcu_dereference(rcdev->raw->progs);
  194. if (!array)
  195. return;
  196. for (item = array->items; item->prog; item++)
  197. bpf_prog_put(item->prog);
  198. bpf_prog_array_free(array);
  199. }
  200. int lirc_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  201. {
  202. struct rc_dev *rcdev;
  203. int ret;
  204. if (attr->attach_flags)
  205. return -EINVAL;
  206. rcdev = rc_dev_get_from_fd(attr->target_fd);
  207. if (IS_ERR(rcdev))
  208. return PTR_ERR(rcdev);
  209. ret = lirc_bpf_attach(rcdev, prog);
  210. put_device(&rcdev->dev);
  211. return ret;
  212. }
  213. int lirc_prog_detach(const union bpf_attr *attr)
  214. {
  215. struct bpf_prog *prog;
  216. struct rc_dev *rcdev;
  217. int ret;
  218. if (attr->attach_flags)
  219. return -EINVAL;
  220. prog = bpf_prog_get_type(attr->attach_bpf_fd,
  221. BPF_PROG_TYPE_LIRC_MODE2);
  222. if (IS_ERR(prog))
  223. return PTR_ERR(prog);
  224. rcdev = rc_dev_get_from_fd(attr->target_fd);
  225. if (IS_ERR(rcdev)) {
  226. bpf_prog_put(prog);
  227. return PTR_ERR(rcdev);
  228. }
  229. ret = lirc_bpf_detach(rcdev, prog);
  230. bpf_prog_put(prog);
  231. put_device(&rcdev->dev);
  232. return ret;
  233. }
  234. int lirc_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
  235. {
  236. __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
  237. struct bpf_prog_array *progs;
  238. struct rc_dev *rcdev;
  239. u32 cnt, flags = 0;
  240. int ret;
  241. if (attr->query.query_flags)
  242. return -EINVAL;
  243. rcdev = rc_dev_get_from_fd(attr->query.target_fd);
  244. if (IS_ERR(rcdev))
  245. return PTR_ERR(rcdev);
  246. if (rcdev->driver_type != RC_DRIVER_IR_RAW) {
  247. ret = -EINVAL;
  248. goto put;
  249. }
  250. ret = mutex_lock_interruptible(&ir_raw_handler_lock);
  251. if (ret)
  252. goto put;
  253. progs = lirc_rcu_dereference(rcdev->raw->progs);
  254. cnt = progs ? bpf_prog_array_length(progs) : 0;
  255. if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt))) {
  256. ret = -EFAULT;
  257. goto unlock;
  258. }
  259. if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags))) {
  260. ret = -EFAULT;
  261. goto unlock;
  262. }
  263. if (attr->query.prog_cnt != 0 && prog_ids && cnt)
  264. ret = bpf_prog_array_copy_to_user(progs, prog_ids,
  265. attr->query.prog_cnt);
  266. unlock:
  267. mutex_unlock(&ir_raw_handler_lock);
  268. put:
  269. put_device(&rcdev->dev);
  270. return ret;
  271. }