Skip to content

Commit ae7487d

Browse files
author
Benjamin Tissoires
committed
selftests/hid: ensure we can compile the tests on kernels pre-6.3
For the hid-bpf tests to compile, we need to have the definition of struct hid_bpf_ctx. This definition is an internal one from the kernel and it is supposed to be defined in the generated vmlinux.h. This vmlinux.h header is generated based on the currently running kernel or if the kernel was already compiled in the tree. If you just compile the selftests without compiling the kernel beforehand and you are running on a 6.2 kernel, you'll end up with a vmlinux.h without the hid_bpf_ctx definition. Use the clever trick from tools/testing/selftests/bpf/progs/bpf_iter.h to force the definition of that symbol in case we don't find it in the BTF and also add __attribute__((preserve_access_index)) to further support CO-RE functionality for these tests. Signed-off-by: Justin Stitt <[email protected]> Tested-by: Nick Desaulniers <[email protected]> # Build Tested-by: Justin Stitt <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent 19fbf67 commit ae7487d

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

tools/testing/selftests/hid/progs/hid.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2022 Red hat */
3-
#include "vmlinux.h"
4-
#include <bpf/bpf_helpers.h>
5-
#include <bpf/bpf_tracing.h>
63
#include "hid_bpf_helpers.h"
74

85
char _license[] SEC("license") = "GPL";

tools/testing/selftests/hid/progs/hid_bpf_helpers.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,83 @@
55
#ifndef __HID_BPF_HELPERS_H
66
#define __HID_BPF_HELPERS_H
77

8+
/* "undefine" structs and enums in vmlinux.h, because we "override" them below */
9+
#define hid_bpf_ctx hid_bpf_ctx___not_used
10+
#define hid_report_type hid_report_type___not_used
11+
#define hid_class_request hid_class_request___not_used
12+
#define hid_bpf_attach_flags hid_bpf_attach_flags___not_used
13+
#define HID_INPUT_REPORT HID_INPUT_REPORT___not_used
14+
#define HID_OUTPUT_REPORT HID_OUTPUT_REPORT___not_used
15+
#define HID_FEATURE_REPORT HID_FEATURE_REPORT___not_used
16+
#define HID_REPORT_TYPES HID_REPORT_TYPES___not_used
17+
#define HID_REQ_GET_REPORT HID_REQ_GET_REPORT___not_used
18+
#define HID_REQ_GET_IDLE HID_REQ_GET_IDLE___not_used
19+
#define HID_REQ_GET_PROTOCOL HID_REQ_GET_PROTOCOL___not_used
20+
#define HID_REQ_SET_REPORT HID_REQ_SET_REPORT___not_used
21+
#define HID_REQ_SET_IDLE HID_REQ_SET_IDLE___not_used
22+
#define HID_REQ_SET_PROTOCOL HID_REQ_SET_PROTOCOL___not_used
23+
#define HID_BPF_FLAG_NONE HID_BPF_FLAG_NONE___not_used
24+
#define HID_BPF_FLAG_INSERT_HEAD HID_BPF_FLAG_INSERT_HEAD___not_used
25+
#define HID_BPF_FLAG_MAX HID_BPF_FLAG_MAX___not_used
26+
27+
#include "vmlinux.h"
28+
29+
#undef hid_bpf_ctx
30+
#undef hid_report_type
31+
#undef hid_class_request
32+
#undef hid_bpf_attach_flags
33+
#undef HID_INPUT_REPORT
34+
#undef HID_OUTPUT_REPORT
35+
#undef HID_FEATURE_REPORT
36+
#undef HID_REPORT_TYPES
37+
#undef HID_REQ_GET_REPORT
38+
#undef HID_REQ_GET_IDLE
39+
#undef HID_REQ_GET_PROTOCOL
40+
#undef HID_REQ_SET_REPORT
41+
#undef HID_REQ_SET_IDLE
42+
#undef HID_REQ_SET_PROTOCOL
43+
#undef HID_BPF_FLAG_NONE
44+
#undef HID_BPF_FLAG_INSERT_HEAD
45+
#undef HID_BPF_FLAG_MAX
46+
47+
#include <bpf/bpf_helpers.h>
48+
#include <bpf/bpf_tracing.h>
49+
#include <linux/const.h>
50+
51+
enum hid_report_type {
52+
HID_INPUT_REPORT = 0,
53+
HID_OUTPUT_REPORT = 1,
54+
HID_FEATURE_REPORT = 2,
55+
56+
HID_REPORT_TYPES,
57+
};
58+
59+
struct hid_bpf_ctx {
60+
__u32 index;
61+
const struct hid_device *hid;
62+
__u32 allocated_size;
63+
enum hid_report_type report_type;
64+
union {
65+
__s32 retval;
66+
__s32 size;
67+
};
68+
} __attribute__((preserve_access_index));
69+
70+
enum hid_class_request {
71+
HID_REQ_GET_REPORT = 0x01,
72+
HID_REQ_GET_IDLE = 0x02,
73+
HID_REQ_GET_PROTOCOL = 0x03,
74+
HID_REQ_SET_REPORT = 0x09,
75+
HID_REQ_SET_IDLE = 0x0A,
76+
HID_REQ_SET_PROTOCOL = 0x0B,
77+
};
78+
79+
enum hid_bpf_attach_flags {
80+
HID_BPF_FLAG_NONE = 0,
81+
HID_BPF_FLAG_INSERT_HEAD = _BITUL(0),
82+
HID_BPF_FLAG_MAX,
83+
};
84+
885
/* following are kfuncs exported by HID for HID-BPF */
986
extern __u8 *hid_bpf_get_data(struct hid_bpf_ctx *ctx,
1087
unsigned int offset,

0 commit comments

Comments
 (0)