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