bpf-lirc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 (bpf_token_capable(prog->aux->token, CAP_PERFMON))
  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, 0, &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, 0, &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. rcu_read_lock();
  182. bpf_prog_run_array(rcu_dereference(raw->progs),
  183. &raw->bpf_sample, bpf_prog_run);
  184. rcu_read_unlock();
  185. }
  186. }
  187. /*
  188. * This should be called once the rc thread has been stopped, so there can be
  189. * no concurrent bpf execution.
  190. *
  191. * Should be called with the ir_raw_handler_lock held.
  192. */
  193. void lirc_bpf_free(struct rc_dev *rcdev)
  194. {
  195. struct bpf_prog_array_item *item;
  196. struct bpf_prog_array *array;
  197. array = lirc_rcu_dereference(rcdev->raw->progs);
  198. if (!array)
  199. return;
  200. for (item = array->items; item->prog; item++)
  201. bpf_prog_put(item->prog);
  202. bpf_prog_array_free(array);
  203. }
  204. int lirc_prog_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  205. {
  206. struct rc_dev *rcdev;
  207. int ret;
  208. if (attr->attach_flags)
  209. return -EINVAL;
  210. rcdev = rc_dev_get_from_fd(attr->target_fd, true);
  211. if (IS_ERR(rcdev))
  212. return PTR_ERR(rcdev);
  213. ret = lirc_bpf_attach(rcdev, prog);
  214. put_device(&rcdev->dev);
  215. return ret;
  216. }
  217. int lirc_prog_detach(const union bpf_attr *attr)
  218. {
  219. struct bpf_prog *prog;
  220. struct rc_dev *rcdev;
  221. int ret;
  222. if (attr->attach_flags)
  223. return -EINVAL;
  224. prog = bpf_prog_get_type(attr->attach_bpf_fd,
  225. BPF_PROG_TYPE_LIRC_MODE2);
  226. if (IS_ERR(prog))
  227. return PTR_ERR(prog);
  228. rcdev = rc_dev_get_from_fd(attr->target_fd, true);
  229. if (IS_ERR(rcdev)) {
  230. bpf_prog_put(prog);
  231. return PTR_ERR(rcdev);
  232. }
  233. ret = lirc_bpf_detach(rcdev, prog);
  234. bpf_prog_put(prog);
  235. put_device(&rcdev->dev);
  236. return ret;
  237. }
  238. int lirc_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
  239. {
  240. __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
  241. struct bpf_prog_array *progs;
  242. struct rc_dev *rcdev;
  243. u32 cnt, flags = 0;
  244. int ret;
  245. if (attr->query.query_flags)
  246. return -EINVAL;
  247. rcdev = rc_dev_get_from_fd(attr->query.target_fd, false);
  248. if (IS_ERR(rcdev))
  249. return PTR_ERR(rcdev);
  250. if (rcdev->driver_type != RC_DRIVER_IR_RAW) {
  251. ret = -EINVAL;
  252. goto put;
  253. }
  254. ret = mutex_lock_interruptible(&ir_raw_handler_lock);
  255. if (ret)
  256. goto put;
  257. progs = lirc_rcu_dereference(rcdev->raw->progs);
  258. cnt = progs ? bpf_prog_array_length(progs) : 0;
  259. if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt))) {
  260. ret = -EFAULT;
  261. goto unlock;
  262. }
  263. if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags))) {
  264. ret = -EFAULT;
  265. goto unlock;
  266. }
  267. if (attr->query.prog_cnt != 0 && prog_ids && cnt)
  268. ret = bpf_prog_array_copy_to_user(progs, prog_ids,
  269. attr->query.prog_cnt);
  270. unlock:
  271. mutex_unlock(&ir_raw_handler_lock);
  272. put:
  273. put_device(&rcdev->dev);
  274. return ret;
  275. }