Subject: Re: [PATCH bpf-next v7 23/24] HID: bpf: add Surface Dial example

From: Greg KH <[email-masked]>
Date: Thu, 28 Jul 2022 17:12:38 +0200

On Thu, Jul 21, 2022 at 05:36:24PM +0200, Benjamin Tissoires wrote:
> Add a more complete HID-BPF example.
> 
> Signed-off-by: Benjamin Tissoires <[email-masked]>
> 
> ---
> 
> changes in v7:
> - remove unnecessary __must_check definition
> 
> new in v6
> 
> fix surface dial
> ---
>  samples/bpf/.gitignore             |   1 +
>  samples/bpf/Makefile               |   6 +-
>  samples/bpf/hid_surface_dial.bpf.c | 161 ++++++++++++++++++++++
>  samples/bpf/hid_surface_dial.c     | 212 +++++++++++++++++++++++++++++
>  4 files changed, 379 insertions(+), 1 deletion(-)
>  create mode 100644 samples/bpf/hid_surface_dial.bpf.c
>  create mode 100644 samples/bpf/hid_surface_dial.c
> 
> diff --git a/samples/bpf/.gitignore b/samples/bpf/.gitignore
> index 65440bd618b2..6a1079d3d064 100644
> --- a/samples/bpf/.gitignore
> +++ b/samples/bpf/.gitignore
> @@ -3,6 +3,7 @@ cpustat
>  fds_example
>  hbm
>  hid_mouse
> +hid_surface_dial
>  ibumad
>  lathist
>  lwt_len_hist
> diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
> index a965bbfaca47..5f5aa7b32565 100644
> --- a/samples/bpf/Makefile
> +++ b/samples/bpf/Makefile
> @@ -58,6 +58,7 @@ tprogs-y += xdp_redirect
>  tprogs-y += xdp_monitor
>  
>  tprogs-y += hid_mouse
> +tprogs-y += hid_surface_dial
>  
>  # Libbpf dependencies
>  LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
> @@ -122,6 +123,7 @@ 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
> +hid_surface_dial-objs := hid_surface_dial.o
>  
>  # Tell kbuild to always build the programs
>  always-y := $(tprogs-y)
> @@ -343,6 +345,7 @@ $(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
> +$(obj)/hid_surface_dial.o: $(obj)/hid_surface_dial.skel.h
>  
>  # Override includes for xdp_sample_user.o because $(srctree)/usr/include in
>  # TPROGS_CFLAGS causes conflicts
> @@ -429,9 +432,10 @@ $(BPF_SKELS_LINKED): $(BPF_OBJS_LINKED) $(BPFTOOL)
>  	$(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
> +OTHER_BPF_SKELS := hid_mouse.skel.h hid_surface_dial.skel.h
>  
>  hid_mouse.skel.h-deps := hid_mouse.bpf.o
> +hid_surface_dial.skel.h-deps := hid_surface_dial.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))
> diff --git a/samples/bpf/hid_surface_dial.bpf.c b/samples/bpf/hid_surface_dial.bpf.c
> new file mode 100644
> index 000000000000..16c821d3decf
> --- /dev/null
> +++ b/samples/bpf/hid_surface_dial.bpf.c
> @@ -0,0 +1,161 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/* Copyright (c) 2022 Benjamin Tissoires
> + */

No hints as to what this, and the other program are doing here in a
comment?

> +
> +	while (running)
> +		;

That's burning up a CPU, why not sleep/yield/something to allow the cpu
to not just pound on this variable and allow other things to happen?

thanks,

greg k-h