1 // SPDX-License-Identifier: GPL-2.0
2 // bpf-lirc.c - handles bpf
4 // Copyright (C) 2018 Sean Young <sean@mess.org>
7 #include <linux/filter.h>
8 #include <linux/bpf_lirc.h>
9 #include "rc-core-priv.h"
11 #define lirc_rcu_dereference(p) \
12 rcu_dereference_protected(p, lockdep_is_held(&ir_raw_handler_lock))
15 * BPF interface for raw IR
17 const struct bpf_prog_ops lirc_mode2_prog_ops
= {
20 BPF_CALL_1(bpf_rc_repeat
, u32
*, sample
)
22 struct ir_raw_event_ctrl
*ctrl
;
24 ctrl
= container_of(sample
, struct ir_raw_event_ctrl
, bpf_sample
);
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
,
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
43 BPF_CALL_4(bpf_rc_keydown
, u32
*, sample
, u32
, protocol
, u64
, scancode
,
46 struct ir_raw_event_ctrl
*ctrl
;
48 ctrl
= container_of(sample
, struct ir_raw_event_ctrl
, bpf_sample
);
50 rc_keydown(ctrl
->dev
, protocol
, scancode
, toggle
!= 0);
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
,
65 BPF_CALL_3(bpf_rc_pointer_rel
, u32
*, sample
, s32
, rel_x
, s32
, rel_y
)
67 struct ir_raw_event_ctrl
*ctrl
;
69 ctrl
= container_of(sample
, struct ir_raw_event_ctrl
, bpf_sample
);
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
);
78 static const struct bpf_func_proto rc_pointer_rel_proto
= {
79 .func
= bpf_rc_pointer_rel
,
81 .ret_type
= RET_INTEGER
,
82 .arg1_type
= ARG_PTR_TO_CTX
,
83 .arg2_type
= ARG_ANYTHING
,
84 .arg3_type
= ARG_ANYTHING
,
87 static const struct bpf_func_proto
*
88 lirc_mode2_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
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_tail_call
:
112 return &bpf_tail_call_proto
;
113 case BPF_FUNC_get_prandom_u32
:
114 return &bpf_get_prandom_u32_proto
;
115 case BPF_FUNC_trace_printk
:
116 if (capable(CAP_SYS_ADMIN
))
117 return bpf_get_trace_printk_proto();
124 static bool lirc_mode2_is_valid_access(int off
, int size
,
125 enum bpf_access_type type
,
126 const struct bpf_prog
*prog
,
127 struct bpf_insn_access_aux
*info
)
129 /* We have one field of u32 */
130 return type
== BPF_READ
&& off
== 0 && size
== sizeof(u32
);
133 const struct bpf_verifier_ops lirc_mode2_verifier_ops
= {
134 .get_func_proto
= lirc_mode2_func_proto
,
135 .is_valid_access
= lirc_mode2_is_valid_access
138 #define BPF_MAX_PROGS 64
140 static int lirc_bpf_attach(struct rc_dev
*rcdev
, struct bpf_prog
*prog
)
142 struct bpf_prog_array
*old_array
;
143 struct bpf_prog_array
*new_array
;
144 struct ir_raw_event_ctrl
*raw
;
147 if (rcdev
->driver_type
!= RC_DRIVER_IR_RAW
)
150 ret
= mutex_lock_interruptible(&ir_raw_handler_lock
);
160 old_array
= lirc_rcu_dereference(raw
->progs
);
161 if (old_array
&& bpf_prog_array_length(old_array
) >= BPF_MAX_PROGS
) {
166 ret
= bpf_prog_array_copy(old_array
, NULL
, prog
, &new_array
);
170 rcu_assign_pointer(raw
->progs
, new_array
);
171 bpf_prog_array_free(old_array
);
174 mutex_unlock(&ir_raw_handler_lock
);
178 static int lirc_bpf_detach(struct rc_dev
*rcdev
, struct bpf_prog
*prog
)
180 struct bpf_prog_array
*old_array
;
181 struct bpf_prog_array
*new_array
;
182 struct ir_raw_event_ctrl
*raw
;
185 if (rcdev
->driver_type
!= RC_DRIVER_IR_RAW
)
188 ret
= mutex_lock_interruptible(&ir_raw_handler_lock
);
198 old_array
= lirc_rcu_dereference(raw
->progs
);
199 ret
= bpf_prog_array_copy(old_array
, prog
, NULL
, &new_array
);
201 * Do not use bpf_prog_array_delete_safe() as we would end up
202 * with a dummy entry in the array, and the we would free the
203 * dummy in lirc_bpf_free()
208 rcu_assign_pointer(raw
->progs
, new_array
);
209 bpf_prog_array_free(old_array
);
212 mutex_unlock(&ir_raw_handler_lock
);
216 void lirc_bpf_run(struct rc_dev
*rcdev
, u32 sample
)
218 struct ir_raw_event_ctrl
*raw
= rcdev
->raw
;
220 raw
->bpf_sample
= sample
;
223 BPF_PROG_RUN_ARRAY(raw
->progs
, &raw
->bpf_sample
, BPF_PROG_RUN
);
227 * This should be called once the rc thread has been stopped, so there can be
228 * no concurrent bpf execution.
230 * Should be called with the ir_raw_handler_lock held.
232 void lirc_bpf_free(struct rc_dev
*rcdev
)
234 struct bpf_prog_array_item
*item
;
235 struct bpf_prog_array
*array
;
237 array
= lirc_rcu_dereference(rcdev
->raw
->progs
);
241 for (item
= array
->items
; item
->prog
; item
++)
242 bpf_prog_put(item
->prog
);
244 bpf_prog_array_free(array
);
247 int lirc_prog_attach(const union bpf_attr
*attr
, struct bpf_prog
*prog
)
249 struct rc_dev
*rcdev
;
252 if (attr
->attach_flags
)
255 rcdev
= rc_dev_get_from_fd(attr
->target_fd
);
257 return PTR_ERR(rcdev
);
259 ret
= lirc_bpf_attach(rcdev
, prog
);
261 put_device(&rcdev
->dev
);
266 int lirc_prog_detach(const union bpf_attr
*attr
)
268 struct bpf_prog
*prog
;
269 struct rc_dev
*rcdev
;
272 if (attr
->attach_flags
)
275 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
,
276 BPF_PROG_TYPE_LIRC_MODE2
);
278 return PTR_ERR(prog
);
280 rcdev
= rc_dev_get_from_fd(attr
->target_fd
);
283 return PTR_ERR(rcdev
);
286 ret
= lirc_bpf_detach(rcdev
, prog
);
289 put_device(&rcdev
->dev
);
294 int lirc_prog_query(const union bpf_attr
*attr
, union bpf_attr __user
*uattr
)
296 __u32 __user
*prog_ids
= u64_to_user_ptr(attr
->query
.prog_ids
);
297 struct bpf_prog_array
*progs
;
298 struct rc_dev
*rcdev
;
302 if (attr
->query
.query_flags
)
305 rcdev
= rc_dev_get_from_fd(attr
->query
.target_fd
);
307 return PTR_ERR(rcdev
);
309 if (rcdev
->driver_type
!= RC_DRIVER_IR_RAW
) {
314 ret
= mutex_lock_interruptible(&ir_raw_handler_lock
);
318 progs
= lirc_rcu_dereference(rcdev
->raw
->progs
);
319 cnt
= progs
? bpf_prog_array_length(progs
) : 0;
321 if (copy_to_user(&uattr
->query
.prog_cnt
, &cnt
, sizeof(cnt
))) {
326 if (copy_to_user(&uattr
->query
.attach_flags
, &flags
, sizeof(flags
))) {
331 if (attr
->query
.prog_cnt
!= 0 && prog_ids
&& cnt
)
332 ret
= bpf_prog_array_copy_to_user(progs
, prog_ids
, cnt
);
335 mutex_unlock(&ir_raw_handler_lock
);
337 put_device(&rcdev
->dev
);