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