Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp832913pxb; Fri, 22 Apr 2022 12:09:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+vbPSlp4J1+ljHbZntDyX5x5HngWcspcBrgtieEsPYlIOkDCFPHz5PwE5NhAtwvKH1m9k X-Received: by 2002:a17:902:e841:b0:158:f1b3:18c with SMTP id t1-20020a170902e84100b00158f1b3018cmr5881483plg.172.1650654550348; Fri, 22 Apr 2022 12:09:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650654550; cv=none; d=google.com; s=arc-20160816; b=IrqyRqq5EYIE0UtN9H3Y7FhOB+Ut1SgEgFNenU7R9X/GAUlFgDc881LpVwH9ONehIA 4/fQ+2kzW1Uif+8p5IEqIevukLo5Csv3kaFSBMRUmMvjakSiy6Yi8RIbg0PX2KqXC6Kz cSxpDtMMfTnEi9xhnDDWyRcme0TGVyVfclQ2oeqKmcOn+Vd6Z6XBWZ5hrI7qwAoM9Klk N541/8yihky3UI+Snx+PWCs73xIff+Ij0XuTzQNw7cenggm74FFiIkXo2NpWw9jopU85 eObMLs/DupuonL9gMl7J3bQrZ4earlr2xyoyPe+Vc7vUX6rXFwIDdo3Eh730+HOmB13F ftBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Hi5GUSbAAds08X4ZJP7ddUqEMtipRWnoAKsCpMXwBrE=; b=vEV9jotPbQqNUfHx6eRULKMmCD8iCb6RmqaOyBk4J4Qt7XFxTyefPiLxKDcN7JQ51d lK7Sj/VOb2JXyQa8oD74IExplKwhv3qa/umsbbnmf6Gz59K/lA/I0Z4s5Q/BBn0BdZnt fQduP1bg84Gjgt4rc9GizQR9nrSNfUoexQ5yd0mrlZFzygELfjk6Ps0+xCT/lw54t4Ef GFgETLW9MDglj66qSuoQ+1oGe/Puuuxk9poFetNKIasgcubWQ9egBHQ48xV9Pc4ST38o +qRtmxJ/4Q1tM1jKXmw6Arx0GCIX0yCIPd5a3lzi2ntYdj/oFyMeEbpUqcE6r0pzCXeq 2CmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q+mprm14; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d7-20020a170903230700b0015904e23e37si9420653plh.59.2022.04.22.12.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 12:09:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q+mprm14; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EC62F1A1D81; Fri, 22 Apr 2022 11:27:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1388941AbiDUOLI (ORCPT + 99 others); Thu, 21 Apr 2022 10:11:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1388953AbiDUOK5 (ORCPT ); Thu, 21 Apr 2022 10:10:57 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 144DA3969C for ; Thu, 21 Apr 2022 07:08:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1650550084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Hi5GUSbAAds08X4ZJP7ddUqEMtipRWnoAKsCpMXwBrE=; b=Q+mprm14ss93fwo6insKjQS+zPtv9dv8L9I30mfBnLagrce7gbSnDbRfQf83og3dVQx9b0 xv7i7jfhCwSkjY5c/fk1krMGnijaEegdhUMNa5IqArwgFxfLZic4lnPc/Bj+6EbILlDil2 +Acku09i2mZtdg0y5sm7OjivEy6aK2w= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-296-lT9cCk2hPNGRU7F2P6C5hQ-1; Thu, 21 Apr 2022 10:08:00 -0400 X-MC-Unique: lT9cCk2hPNGRU7F2P6C5hQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EF23480346F; Thu, 21 Apr 2022 14:07:59 +0000 (UTC) Received: from plouf.redhat.com (unknown [10.39.194.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 93CAB40EC002; Thu, 21 Apr 2022 14:07:57 +0000 (UTC) From: Benjamin Tissoires To: Greg KH , Jiri Kosina , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh Cc: Tero Kristo , linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, bpf@vger.kernel.org, Benjamin Tissoires Subject: [RFC bpf-next v4 6/7] samples/bpf: add new hid_mouse example Date: Thu, 21 Apr 2022 16:07:39 +0200 Message-Id: <20220421140740.459558-7-benjamin.tissoires@redhat.com> In-Reply-To: <20220421140740.459558-1-benjamin.tissoires@redhat.com> References: <20220421140740.459558-1-benjamin.tissoires@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Everything should be available in the selftest part of the tree, but providing an example without uhid and hidraw will be more easy to follow for users. Signed-off-by: Benjamin Tissoires --- changes in v4: - dropped the not-yet-implemented rdesc_fixup - use the new API changes in v3: - use the new hid_get_data API - add a comment for the report descriptor fixup to explain what is done changes in v2: - split the series by bpf/libbpf/hid/selftests and samples --- samples/bpf/.gitignore | 1 + samples/bpf/Makefile | 23 +++++++ samples/bpf/hid_mouse.bpf.c | 59 ++++++++++++++++ samples/bpf/hid_mouse.c | 131 ++++++++++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 samples/bpf/hid_mouse.bpf.c create mode 100644 samples/bpf/hid_mouse.c diff --git a/samples/bpf/.gitignore b/samples/bpf/.gitignore index 0e7bfdbff80a..65440bd618b2 100644 --- a/samples/bpf/.gitignore +++ b/samples/bpf/.gitignore @@ -2,6 +2,7 @@ cpustat fds_example hbm +hid_mouse ibumad lathist lwt_len_hist diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 8fff5ad3444b..dffca4b04a94 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -60,6 +60,8 @@ tprogs-y += xdp_redirect_map tprogs-y += xdp_redirect tprogs-y += xdp_monitor +tprogs-y += hid_mouse + # Libbpf dependencies LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf LIBBPF_OUTPUT = $(abspath $(BPF_SAMPLES_PATH))/libbpf @@ -125,6 +127,8 @@ xdp_redirect-objs := xdp_redirect_user.o $(XDP_SAMPLE) xdp_monitor-objs := xdp_monitor_user.o $(XDP_SAMPLE) xdp_router_ipv4-objs := xdp_router_ipv4_user.o $(XDP_SAMPLE) +hid_mouse-objs := hid_mouse.o + # Tell kbuild to always build the programs always-y := $(tprogs-y) always-y += sockex1_kern.o @@ -349,6 +353,8 @@ $(obj)/hbm_out_kern.o: $(src)/hbm.h $(src)/hbm_kern.h $(obj)/hbm.o: $(src)/hbm.h $(obj)/hbm_edt_kern.o: $(src)/hbm.h $(src)/hbm_kern.h +$(obj)/hid_mouse.o: $(obj)/hid_mouse.skel.h + # Override includes for xdp_sample_user.o because $(srctree)/usr/include in # TPROGS_CFLAGS causes conflicts XDP_SAMPLE_CFLAGS += -Wall -O2 \ @@ -434,6 +440,23 @@ $(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL) @echo " BPF GEN-SKEL" $(@:.skel.h=) $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=)) > $@ +# Generate BPF skeletons for non XDP progs +OTHER_BPF_SKELS := hid_mouse.skel.h + +hid_mouse.skel.h-deps := hid_mouse.bpf.o + +OTHER_BPF_SRCS_LINKED := $(patsubst %.skel.h,%.bpf.c, $(OTHER_BPF_SKELS)) +OTHER_BPF_OBJS_LINKED := $(patsubst %.bpf.c,$(obj)/%.bpf.o, $(OTHER_BPF_SRCS_LINKED)) +OTHER_BPF_SKELS_LINKED := $(addprefix $(obj)/,$(OTHER_BPF_SKELS)) + +$(OTHER_BPF_SKELS_LINKED): $(OTHER_BPF_OBJS_LINKED) $(BPFTOOL) + @echo " BPF GEN-OBJ " $(@:.skel.h=) + $(Q)$(BPFTOOL) gen object $(@:.skel.h=.lbpf.o) $(addprefix $(obj)/,$($(@F)-deps)) + @echo " BPF GEN-SKEL" $(@:.skel.h=) + $(Q)$(BPFTOOL) gen skeleton $(@:.skel.h=.lbpf.o) name $(notdir $(@:.skel.h=_lskel)) > $@ +# $(call msg,GEN-SKEL,$@) +# $(Q)$(BPFTOOL) gen skeleton $< > $@ + # asm/sysreg.h - inline assembly used by it is incompatible with llvm. # But, there is no easy way to fix it, so just exclude it since it is # useless for BPF samples. diff --git a/samples/bpf/hid_mouse.bpf.c b/samples/bpf/hid_mouse.bpf.c new file mode 100644 index 000000000000..45cc26b70f5f --- /dev/null +++ b/samples/bpf/hid_mouse.bpf.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Note: for the following code to compile, we need HID to be included + * in vmlinuz (CONFIG_HID=y). + * If HID is compiled as a separate module, we need to use the vmlinux.h + * which contains the various hid symbols, it can be generated through: + * + * $> ./tools/bpf/bpftool/bpftool btf dump \ + * file /sys/kernel/btf/hid format c > samples/bpf/vmlinux.h + * + * Once the code is compiled, the fact that HID is a separate module + * or not is not an issue, the same binary will run similarily. + */ +#include "vmlinux.h" +#include +#include + +extern __u8 *hid_bpf_kfunc_get_data(struct hid_bpf_ctx *ctx, + unsigned int offset, + const size_t __sz) __ksym; +extern void hid_bpf_kfunc_data_release(__u8 *data) __ksym; +extern int hid_bpf_kfunc_hw_request(struct hid_bpf_ctx *ctx) __ksym; + +#define BUS_USB 3 + +SEC("fentry/hid_bpf_device_event") +int BPF_KPROBE(hid_event, struct hid_bpf_ctx *hctx, __s64 type) +{ + __u8 *rw_data = hid_bpf_kfunc_get_data(hctx, 0, 5); + + if (!rw_data) + return 0; + + if (hctx->bus == BUS_USB) { + /* note: the following call prevents the program to be loaded: + * hid_bpf_device_event() is not sleepable when this function is. + */ + hid_bpf_kfunc_hw_request(hctx); + + bpf_printk("data: %02x %02x %02x", rw_data[0], rw_data[1], rw_data[4]); + } + + hid_bpf_kfunc_data_release(rw_data); + + return 0; +} + +SEC("fmod_ret/hid_bpf_device_event") +int BPF_PROG(hid_mod_event, struct hid_bpf_ctx *hctx, __s64 type) +{ + /* prevent any USB event to go through the HID stack */ + if (hctx->bus == BUS_USB) + return -1; + + return 0; +} + +char _license[] SEC("license") = "GPL"; diff --git a/samples/bpf/hid_mouse.c b/samples/bpf/hid_mouse.c new file mode 100644 index 000000000000..aace0b959d3b --- /dev/null +++ b/samples/bpf/hid_mouse.c @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2022 Benjamin Tissoires + */ + +/* not sure why but this doesn't get preoperly imported */ +#define __must_check + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "bpf_util.h" +#include +#include + +#include "hid_mouse.skel.h" + +static char *sysfs_path; +static int sysfs_fd; +static int prog_count; + +static bool running = true; + +struct prog { + int fd; + struct bpf_link *link; + enum bpf_attach_type type; +}; + +static struct prog progs[10]; + +static void int_exit(int sig) +{ + running = false; + exit(0); +} + +static void usage(const char *prog) +{ + fprintf(stderr, + "%s: %s /sys/bus/hid/devices/0BUS:0VID:0PID:00ID/uevent\n\n", + __func__, prog); +} + +int main(int argc, char **argv) +{ + struct hid_mouse_lskel *skel; + int prog_fd, err; + const char *optstr = ""; + int opt; + char filename[256]; + + LIBBPF_OPTS(bpf_test_run_opts, topts, + .repeat = 1, + ); + + while ((opt = getopt(argc, argv, optstr)) != -1) { + switch (opt) { + default: + usage(basename(argv[0])); + return 1; + } + } + + if (optind == argc) { + usage(basename(argv[0])); + return 1; + } + + sysfs_path = argv[optind]; + if (!sysfs_path) { + perror("sysfs"); + return 1; + } + + skel = hid_mouse_lskel__open_and_load(); + if (!skel) { + fprintf(stderr, "%s %s:%d", __func__, __FILE__, __LINE__); + return -1; + } + + err = hid_mouse_lskel__attach(skel); + if (err) + goto cleanup; + + //prog_fd = bpf_program__fd(skel->progs.hid_event); + //err = bpf_prog_test_run_opts(prog_fd, &topts); + +// sysfs_fd = open(sysfs_path, O_RDONLY); +// +//// bpf_object__for_each_program(prog, obj) { +//// progs[prog_count].fd = bpf_program__fd(prog); +//// progs[prog_count].type = bpf_program__get_expected_attach_type(prog); +//// progs[prog_count].link = bpf_program__attach(prog); +//// if (libbpf_get_error(progs[prog_count].link)) { +//// fprintf(stderr, "bpf_prog_attach: err=%m\n"); +//// progs[prog_count].fd = 0; +//// progs[prog_count].link = NULL; +//// goto cleanup; +//// } +//// prog_count++; +//// } +// + signal(SIGINT, int_exit); + signal(SIGTERM, int_exit); +// +//// err = bpf_obj_get_info_by_fd(progs[0].fd, &info, &info_len); +//// if (err) { +//// printf("can't get prog info - %s\n", strerror(errno)); +//// goto cleanup; +//// } +// + while (running) + ; + + cleanup: + hid_mouse_lskel__destroy(skel); + + return err; +} -- 2.35.1