Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/actions/vmtest/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: 'vmtest'
description: 'Build + run vmtest'
inputs:
arch:
description: 'what arch to test'
required: true
default: 'x86_64'
runs:
using: "composite"
steps:
# 1. Setup environment
- name: Setup build environment
uses: libbpf/ci/setup-build-env@master
# 2. Build
- name: Build kernel image
shell: bash
run: ${GITHUB_ACTION_PATH}/build.sh ${{ inputs.arch }}
- name: Build selftests
shell: bash
run: ${GITHUB_ACTION_PATH}/build_selftests.sh
env:
VMLINUX_BTF: ${{ github.workspace }}/vmlinux
# 3. Test
- name: Prepare rootfs
uses: libbpf/ci/prepare-rootfs@master
with:
project-name: 'libbpf'
arch: ${{ inputs.arch }}
kernel-root: '.'
- name: Run selftests
uses: libbpf/ci/run-qemu@master
with:
arch: ${{ inputs.arch}}
img: '/tmp/root.img'
vmlinuz: '${{ github.workspace }}/vmlinuz'
17 changes: 17 additions & 0 deletions .github/actions/vmtest/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -euo pipefail

ARCH="$1"

THISDIR="$(cd $(dirname $0) && pwd)"

source "${THISDIR}"/helpers.sh

travis_fold start build_kernel "Building kernel"

cp ${GITHUB_WORKSPACE}/travis-ci/vmtest/configs/config-latest.${ARCH} .config

make -j $((4*$(nproc))) olddefconfig all > /dev/null

travis_fold end build_kernel
42 changes: 42 additions & 0 deletions .github/actions/vmtest/build_selftests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

set -euo pipefail

THISDIR="$(cd $(dirname $0) && pwd)"

source "${THISDIR}"/helpers.sh

travis_fold start prepare_selftests "Building selftests"

LLVM_VER=15
LIBBPF_PATH="${REPO_ROOT}"

PREPARE_SELFTESTS_SCRIPT=${THISDIR}/prepare_selftests-${KERNEL}.sh
if [ -f "${PREPARE_SELFTESTS_SCRIPT}" ]; then
(cd "${REPO_ROOT}/${REPO_PATH}/tools/testing/selftests/bpf" && ${PREPARE_SELFTESTS_SCRIPT})
fi

if [[ "${KERNEL}" = 'LATEST' ]]; then
VMLINUX_H=
else
VMLINUX_H=${THISDIR}/vmlinux.h
fi

cd ${REPO_ROOT}/${REPO_PATH}
make \
CLANG=clang-${LLVM_VER} \
LLC=llc-${LLVM_VER} \
LLVM_STRIP=llvm-strip-${LLVM_VER} \
VMLINUX_BTF="${VMLINUX_BTF}" \
VMLINUX_H="${VMLINUX_H}" \
-C "${REPO_ROOT}/${REPO_PATH}/tools/testing/selftests/bpf" \
-j $((4*$(nproc))) > /dev/null
cd -
mkdir "${LIBBPF_PATH}"/selftests
cp -R "${REPO_ROOT}/${REPO_PATH}/tools/testing/selftests/bpf" \
"${LIBBPF_PATH}"/selftests
cd "${LIBBPF_PATH}"
rm selftests/bpf/.gitignore
git add selftests

travis_fold end prepare_selftests
44 changes: 44 additions & 0 deletions .github/actions/vmtest/helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# $1 - start or end
# $2 - fold identifier, no spaces
# $3 - fold section description
travis_fold() {
local YELLOW='\033[1;33m'
local NOCOLOR='\033[0m'
if [ -z ${GITHUB_WORKFLOW+x} ]; then
echo travis_fold:$1:$2
if [ ! -z "${3:-}" ]; then
echo -e "${YELLOW}$3${NOCOLOR}"
fi
echo
else
if [ $1 = "start" ]; then
line="::group::$2"
if [ ! -z "${3:-}" ]; then
line="$line - ${YELLOW}$3${NOCOLOR}"
fi
else
line="::endgroup::"
fi
echo -e "$line"
fi
}

__print() {
local TITLE=""
if [[ -n $2 ]]; then
TITLE=" title=$2"
fi
echo "::$1${TITLE}::$3"
}

# $1 - title
# $2 - message
print_error() {
__print error $1 $2
}

# $1 - title
# $2 - message
print_notice() {
__print notice $1 $2
}
50 changes: 50 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: bpf-ci

on:
pull_request:

concurrency:
group: ci-test-${{ github.head_ref }}
cancel-in-progress: true

jobs:
VM_Test:
runs-on: ${{ matrix.runs_on }}
name: Kernel ${{ matrix.kernel }} on ${{ matrix.runs_on }} + selftests
timeout-minutes: 100
strategy:
fail-fast: false
matrix:
include:
- kernel: 'LATEST'
runs_on: ubuntu-latest
arch: 'x86_64'
- kernel: 'LATEST'
runs_on: z15
arch: 's390x'
env:
AUTHOR_EMAIL: "$(git log -1 --pretty=\"%aE\")"
KERNEL: LATEST
REPO_ROOT: ${{ github.workspace }}
REPO_PATH: ""
steps:
- uses: actions/checkout@v2
- if: ${{ github.repository != 'kernel-patches/bpf' && github.repository != 'kernel-patches/bpf-rc' }}
name: Download bpf-next tree
uses: libbpf/ci/get-linux-source@master
with:
dest: '.kernel'
- if: ${{ github.repository != 'kernel-patches/bpf' && github.repository != 'kernel-patches/bpf-rc' }}
name: Move linux source in place
shell: bash
run: |
rm -rf .kernel/.git
cp -rf .kernel/. .
rm -rf .kernel
- uses: libbpf/ci/patch-kernel@master
with:
patches-root: '${{ github.workspace }}/travis-ci/diffs'
repo-root: '${{ github.workspace }}'
- uses: ./.github/actions/vmtest
with:
arch: ${{ matrix.arch }}
18 changes: 0 additions & 18 deletions README
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
Linux kernel
============

There are several guides for kernel developers and users. These guides can
be rendered in a number of formats, like HTML and PDF. Please read
Documentation/admin-guide/README.rst first.

In order to build the documentation, use ``make htmldocs`` or
``make pdfdocs``. The formatted documentation can also be read online at:

https://www.kernel.org/doc/html/latest/

There are various text files in the Documentation/ subdirectory,
several of them using the Restructured Text markup notation.

Please read the Documentation/process/changes.rst file, as it contains the
requirements for building and running the kernel, and information about
the problems which may result by upgrading your kernel.
115 changes: 115 additions & 0 deletions drivers/hid/hid-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <linux/hiddev.h>
#include <linux/hid-debug.h>
#include <linux/hidraw.h>
#include <linux/btf.h>
#include <linux/btf_ids.h>
#include <linux/hid_bpf.h>

#include "hid-ids.h"

Expand Down Expand Up @@ -2008,6 +2011,99 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
}
EXPORT_SYMBOL_GPL(hid_report_raw_event);

struct hid_bpf_ctx_kern {
struct hid_device *hdev;
struct hid_bpf_ctx ctx;
u8 *data;
size_t size;
};

__weak int hid_bpf_device_event(struct hid_bpf_ctx *ctx, s64 type)
{
return 0;
}
ALLOW_ERROR_INJECTION(hid_bpf_device_event, NS_ERRNO);

noinline __u8 *
hid_bpf_kfunc_get_data(struct hid_bpf_ctx *ctx, unsigned int offset, const size_t __sz)
{
struct hid_bpf_ctx_kern *ctx_kern;

if (!ctx)
return NULL;

ctx_kern = container_of(ctx, struct hid_bpf_ctx_kern, ctx);

return ctx_kern->data;
}

noinline void
hid_bpf_kfunc_data_release(__u8 *data)
{
}

noinline int
hid_bpf_kfunc_hw_request(struct hid_bpf_ctx *ctx)
{
if (!ctx)
return -EINVAL;

pr_err("%s test ctx->bus: %04x %s:%d", __func__, ctx->bus, __FILE__, __LINE__);

return 0;
}

/*
* The following set contains all functions we agree BPF programs
* can use.
*/
BTF_SET_START(hid_bpf_kfunc_ids)
BTF_ID(func, hid_bpf_kfunc_get_data)
BTF_ID(func, hid_bpf_kfunc_data_release)
BTF_ID(func, hid_bpf_kfunc_hw_request)
BTF_SET_END(hid_bpf_kfunc_ids)

/*
* The following set contains all functions to provide a kernel
* resource to the BPF program.
* We need to add a counterpart release function.
*/
BTF_SET_START(hid_bpf_kfunc_acquire_ids)
BTF_ID(func, hid_bpf_kfunc_get_data)
BTF_SET_END(hid_bpf_kfunc_acquire_ids)

/*
* The following set is the release counterpart of the previous
* function set.
*/
BTF_SET_START(hid_bpf_kfunc_release_ids)
BTF_ID(func, hid_bpf_kfunc_data_release)
BTF_SET_END(hid_bpf_kfunc_release_ids)

/*
* The following set tells which functions are sleepable.
*/
BTF_SET_START(hid_bpf_kfunc_sleepable_ids)
BTF_ID(func, hid_bpf_kfunc_hw_request)
BTF_SET_END(hid_bpf_kfunc_sleepable_ids)

static const struct btf_kfunc_id_set hid_bpf_kfunc_set = {
.owner = THIS_MODULE,
.check_set = &hid_bpf_kfunc_ids,
.acquire_set = &hid_bpf_kfunc_acquire_ids,
.release_set = &hid_bpf_kfunc_release_ids,
.ret_null_set = &hid_bpf_kfunc_acquire_ids, /* duplicated to force BPF programs to
* check the validity of the returned pointer
* in acquire function
*/
.sleepable_set = &hid_bpf_kfunc_sleepable_ids,
};

static int __init hid_bpf_init(void)
{
return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &hid_bpf_kfunc_set);
}

/**
* hid_input_report - report data from lower layer (usb, bt...)
*
Expand All @@ -2025,6 +2121,17 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i
struct hid_driver *hdrv;
struct hid_report *report;
int ret = 0;
struct hid_bpf_ctx_kern ctx_kern = {
.hdev = hid,
.ctx = {
.bus = hid->bus,
.group = hid->group,
.vendor = hid->vendor,
.product = hid->product,
},
.data = data,
.size = size,
};

if (!hid)
return -ENODEV;
Expand All @@ -2039,6 +2146,10 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i
report_enum = hid->report_enum + type;
hdrv = hid->driver;

ret = hid_bpf_device_event(&ctx_kern.ctx, type);
if (ret)
goto unlock;

if (!size) {
dbg_hid("empty report\n");
ret = -1;
Expand Down Expand Up @@ -2914,6 +3025,10 @@ static int __init hid_init(void)

hid_debug_init();

ret = hid_bpf_init();
if (ret)
pr_err("%s error in bpf_init: %d %s:%d", __func__, ret, __FILE__, __LINE__);

return 0;
err_bus:
bus_unregister(&hid_bus_type);
Expand Down
1 change: 1 addition & 0 deletions include/asm-generic/error-injection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum {
EI_ETYPE_ERRNO, /* Return -ERRNO if failure */
EI_ETYPE_ERRNO_NULL, /* Return -ERRNO or NULL if failure */
EI_ETYPE_TRUE, /* Return true if failure */
EI_ETYPE_NS_ERRNO, /* Return -ERRNO if failure and tag the function as non-sleepable */
};

struct error_injection_entry {
Expand Down
8 changes: 8 additions & 0 deletions include/linux/btf.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum btf_kfunc_type {
BTF_KFUNC_TYPE_ACQUIRE,
BTF_KFUNC_TYPE_RELEASE,
BTF_KFUNC_TYPE_RET_NULL,
BTF_KFUNC_TYPE_SLEEPABLE,
BTF_KFUNC_TYPE_MAX,
};

Expand All @@ -35,6 +36,7 @@ struct btf_kfunc_id_set {
struct btf_id_set *acquire_set;
struct btf_id_set *release_set;
struct btf_id_set *ret_null_set;
struct btf_id_set *sleepable_set;
};
struct btf_id_set *sets[BTF_KFUNC_TYPE_MAX];
};
Expand Down Expand Up @@ -332,6 +334,12 @@ static inline struct btf_param *btf_params(const struct btf_type *t)
return (struct btf_param *)(t + 1);
}

struct bpf_reg_state;

bool btf_is_kfunc_arg_mem_size(const struct btf *btf,
const struct btf_param *arg,
const struct bpf_reg_state *reg);

#ifdef CONFIG_BPF_SYSCALL
struct bpf_prog;

Expand Down
Loading