[PATCH bpf-next v2 23/28] HID: bpf: compute only the required buffer size for the device

Benjamin Tissoires
2022-03-04T17:35:40Z

There is no point in using 16 kB of memory if the device needs less
for all of its reports (uwhich is usually the case).

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

---

new in v2
---
drivers/hid/hid-bpf.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/hid/hid-bpf.c b/drivers/hid/hid-bpf.c
index b8c0060f3180..d56fbad990ed 100644
--- a/drivers/hid/hid-bpf.c
+++ b/drivers/hid/hid-bpf.c
@@ -61,11 +61,24 @@ static int hid_reconnect(struct hid_device *hdev)
static int hid_bpf_link_attach(struct hid_device *hdev, enum bpf_hid_attach_type type)
{
int err = 0;
+ unsigned int i, j, max_report_len = 0;
+
+ /* compute the maximum report length for this device */
+ for (i = 0; i < HID_REPORT_TYPES; i++) {
+ struct hid_report_enum *report_enum = hdev->report_enum + i;
+
+ for (j = 0; j < HID_MAX_IDS; j++) {
+ struct hid_report *report = report_enum->report_id_hash[j];
+
+ if (report)
+ max_report_len = max(max_report_len, hid_report_len(report));
+ }
+ }

switch (type) {
case BPF_HID_ATTACH_DEVICE_EVENT:
if (!hdev->bpf.ctx) {
- hdev->bpf.ctx = bpf_hid_allocate_ctx(hdev, HID_BPF_MAX_BUFFER_SIZE);
+ hdev->bpf.ctx = bpf_hid_allocate_ctx(hdev, max_report_len);
if (IS_ERR(hdev->bpf.ctx)) {
err = PTR_ERR(hdev->bpf.ctx);
hdev->bpf.ctx = NULL;
--
2.35.1