License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 21:07:57 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
perf bpf: Add prologue for BPF programs for fetching arguments
This patch generates a prologue for a BPF program which fetches arguments for
it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx
pointer. r2 is used to indicate if dereferencing was done successfully.
This patch uses r6 to hold ctx (struct pt_regs) and r7 to hold stack pointer
for result. Result of each arguments first store on stack:
low address
BPF_REG_FP - 24 ARG3
BPF_REG_FP - 16 ARG2
BPF_REG_FP - 8 ARG1
BPF_REG_FP
high address
Then loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register
r7 <- fp
r7 <- r7 - stack_offset // pointer to result slot
/* load r3 with the offset in pt_regs of 'reg' */
(r7) <- r3 // make slot valid
r3 <- r3 + off1 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7 // result put onto stack
call probe_read // read unsafe pointer
jnei r0, 0, err // error checking
r3 <- (r7) // read result
r3 <- r3 + off2 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7
call probe_read
jnei r0, 0, err
...
/* load r2, r3, r4 from stack */
goto success
err:
r2 <- 1
/* load r3, r4, r5 with 0 */
goto usercode
success:
r2 <- 0
usercode:
r1 <- r6 // restore ctx
// original user code
If all of arguments reside in register (dereferencing is not
required), gen_prologue_fastpath() will be used to create
fast prologue:
r3 <- (r1 + offset of reg1)
r4 <- (r1 + offset of reg2)
r5 <- (r1 + offset of reg3)
r2 <- 0
P.S.
eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stack
Committer note:
At least testing if it builds and loads:
# cat test_probe_arg.c
struct pt_regs;
__attribute__((section("lock_page=__lock_page page->flags"), used))
int func(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
char _license[] __attribute__((section("license"), used)) = "GPL";
int _version __attribute__((section("version"), used)) = 0x40300;
# perf record -e ./test_probe_arg.c usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.016 MB perf.data ]
# perf evlist
perf_bpf_probe:lock_page
#
Signed-off-by: He Kuang <hekuang@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1447675815-166222-11-git-send-email-wangnan0@huawei.com
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-16 19:10:12 +07:00
|
|
|
/*
|
|
|
|
* bpf-prologue.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 He Kuang <hekuang@huawei.com>
|
|
|
|
* Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
|
|
|
|
* Copyright (C) 2015 Huawei Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <bpf/libbpf.h>
|
|
|
|
#include "debug.h"
|
|
|
|
#include "bpf-loader.h"
|
|
|
|
#include "bpf-prologue.h"
|
|
|
|
#include "probe-finder.h"
|
2017-04-18 20:46:11 +07:00
|
|
|
#include <errno.h>
|
2019-08-30 02:18:59 +07:00
|
|
|
#include <stdlib.h>
|
perf bpf: Add prologue for BPF programs for fetching arguments
This patch generates a prologue for a BPF program which fetches arguments for
it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx
pointer. r2 is used to indicate if dereferencing was done successfully.
This patch uses r6 to hold ctx (struct pt_regs) and r7 to hold stack pointer
for result. Result of each arguments first store on stack:
low address
BPF_REG_FP - 24 ARG3
BPF_REG_FP - 16 ARG2
BPF_REG_FP - 8 ARG1
BPF_REG_FP
high address
Then loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register
r7 <- fp
r7 <- r7 - stack_offset // pointer to result slot
/* load r3 with the offset in pt_regs of 'reg' */
(r7) <- r3 // make slot valid
r3 <- r3 + off1 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7 // result put onto stack
call probe_read // read unsafe pointer
jnei r0, 0, err // error checking
r3 <- (r7) // read result
r3 <- r3 + off2 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7
call probe_read
jnei r0, 0, err
...
/* load r2, r3, r4 from stack */
goto success
err:
r2 <- 1
/* load r3, r4, r5 with 0 */
goto usercode
success:
r2 <- 0
usercode:
r1 <- r6 // restore ctx
// original user code
If all of arguments reside in register (dereferencing is not
required), gen_prologue_fastpath() will be used to create
fast prologue:
r3 <- (r1 + offset of reg1)
r4 <- (r1 + offset of reg2)
r5 <- (r1 + offset of reg3)
r2 <- 0
P.S.
eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stack
Committer note:
At least testing if it builds and loads:
# cat test_probe_arg.c
struct pt_regs;
__attribute__((section("lock_page=__lock_page page->flags"), used))
int func(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
char _license[] __attribute__((section("license"), used)) = "GPL";
int _version __attribute__((section("version"), used)) = 0x40300;
# perf record -e ./test_probe_arg.c usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.016 MB perf.data ]
# perf evlist
perf_bpf_probe:lock_page
#
Signed-off-by: He Kuang <hekuang@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1447675815-166222-11-git-send-email-wangnan0@huawei.com
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-16 19:10:12 +07:00
|
|
|
#include <dwarf-regs.h>
|
|
|
|
#include <linux/filter.h>
|
|
|
|
|
|
|
|
#define BPF_REG_SIZE 8
|
|
|
|
|
|
|
|
#define JMP_TO_ERROR_CODE -1
|
|
|
|
#define JMP_TO_SUCCESS_CODE -2
|
|
|
|
#define JMP_TO_USER_CODE -3
|
|
|
|
|
|
|
|
struct bpf_insn_pos {
|
|
|
|
struct bpf_insn *begin;
|
|
|
|
struct bpf_insn *end;
|
|
|
|
struct bpf_insn *pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
pos_get_cnt(struct bpf_insn_pos *pos)
|
|
|
|
{
|
|
|
|
return pos->pos - pos->begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
append_insn(struct bpf_insn new_insn, struct bpf_insn_pos *pos)
|
|
|
|
{
|
|
|
|
if (!pos->pos)
|
|
|
|
return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
|
|
|
|
|
|
|
|
if (pos->pos + 1 >= pos->end) {
|
|
|
|
pr_err("bpf prologue: prologue too long\n");
|
|
|
|
pos->pos = NULL;
|
|
|
|
return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(pos->pos)++ = new_insn;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_pos(struct bpf_insn_pos *pos)
|
|
|
|
{
|
|
|
|
if (!pos->pos || pos->pos >= pos->end)
|
|
|
|
return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-15 16:21:59 +07:00
|
|
|
/*
|
|
|
|
* Convert type string (u8/u16/u32/u64/s8/s16/s32/s64 ..., see
|
2018-05-09 01:14:57 +07:00
|
|
|
* Documentation/trace/kprobetrace.rst) to size field of BPF_LDX_MEM
|
2017-08-15 16:21:59 +07:00
|
|
|
* instruction (BPF_{B,H,W,DW}).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
argtype_to_ldx_size(const char *type)
|
|
|
|
{
|
|
|
|
int arg_size = type ? atoi(&type[1]) : 64;
|
|
|
|
|
|
|
|
switch (arg_size) {
|
|
|
|
case 8:
|
|
|
|
return BPF_B;
|
|
|
|
case 16:
|
|
|
|
return BPF_H;
|
|
|
|
case 32:
|
|
|
|
return BPF_W;
|
|
|
|
case 64:
|
|
|
|
default:
|
|
|
|
return BPF_DW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
insn_sz_to_str(int insn_sz)
|
|
|
|
{
|
|
|
|
switch (insn_sz) {
|
|
|
|
case BPF_B:
|
|
|
|
return "BPF_B";
|
|
|
|
case BPF_H:
|
|
|
|
return "BPF_H";
|
|
|
|
case BPF_W:
|
|
|
|
return "BPF_W";
|
|
|
|
case BPF_DW:
|
|
|
|
return "BPF_DW";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
perf bpf: Add prologue for BPF programs for fetching arguments
This patch generates a prologue for a BPF program which fetches arguments for
it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx
pointer. r2 is used to indicate if dereferencing was done successfully.
This patch uses r6 to hold ctx (struct pt_regs) and r7 to hold stack pointer
for result. Result of each arguments first store on stack:
low address
BPF_REG_FP - 24 ARG3
BPF_REG_FP - 16 ARG2
BPF_REG_FP - 8 ARG1
BPF_REG_FP
high address
Then loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register
r7 <- fp
r7 <- r7 - stack_offset // pointer to result slot
/* load r3 with the offset in pt_regs of 'reg' */
(r7) <- r3 // make slot valid
r3 <- r3 + off1 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7 // result put onto stack
call probe_read // read unsafe pointer
jnei r0, 0, err // error checking
r3 <- (r7) // read result
r3 <- r3 + off2 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7
call probe_read
jnei r0, 0, err
...
/* load r2, r3, r4 from stack */
goto success
err:
r2 <- 1
/* load r3, r4, r5 with 0 */
goto usercode
success:
r2 <- 0
usercode:
r1 <- r6 // restore ctx
// original user code
If all of arguments reside in register (dereferencing is not
required), gen_prologue_fastpath() will be used to create
fast prologue:
r3 <- (r1 + offset of reg1)
r4 <- (r1 + offset of reg2)
r5 <- (r1 + offset of reg3)
r2 <- 0
P.S.
eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stack
Committer note:
At least testing if it builds and loads:
# cat test_probe_arg.c
struct pt_regs;
__attribute__((section("lock_page=__lock_page page->flags"), used))
int func(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
char _license[] __attribute__((section("license"), used)) = "GPL";
int _version __attribute__((section("version"), used)) = 0x40300;
# perf record -e ./test_probe_arg.c usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.016 MB perf.data ]
# perf evlist
perf_bpf_probe:lock_page
#
Signed-off-by: He Kuang <hekuang@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1447675815-166222-11-git-send-email-wangnan0@huawei.com
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-16 19:10:12 +07:00
|
|
|
/* Give it a shorter name */
|
|
|
|
#define ins(i, p) append_insn((i), (p))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give a register name (in 'reg'), generate instruction to
|
|
|
|
* load register into an eBPF register rd:
|
|
|
|
* 'ldd target_reg, offset(ctx_reg)', where:
|
|
|
|
* ctx_reg is pre initialized to pointer of 'struct pt_regs'.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gen_ldx_reg_from_ctx(struct bpf_insn_pos *pos, int ctx_reg,
|
|
|
|
const char *reg, int target_reg)
|
|
|
|
{
|
|
|
|
int offset = regs_query_register_offset(reg);
|
|
|
|
|
|
|
|
if (offset < 0) {
|
|
|
|
pr_err("bpf: prologue: failed to get register %s\n",
|
|
|
|
reg);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
ins(BPF_LDX_MEM(BPF_DW, target_reg, ctx_reg, offset), pos);
|
|
|
|
|
|
|
|
return check_pos(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a BPF_FUNC_probe_read function call.
|
|
|
|
*
|
|
|
|
* src_base_addr_reg is a register holding base address,
|
|
|
|
* dst_addr_reg is a register holding dest address (on stack),
|
|
|
|
* result is:
|
|
|
|
*
|
|
|
|
* *[dst_addr_reg] = *([src_base_addr_reg] + offset)
|
|
|
|
*
|
|
|
|
* Arguments of BPF_FUNC_probe_read:
|
|
|
|
* ARG1: ptr to stack (dest)
|
|
|
|
* ARG2: size (8)
|
|
|
|
* ARG3: unsafe ptr (src)
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gen_read_mem(struct bpf_insn_pos *pos,
|
|
|
|
int src_base_addr_reg,
|
|
|
|
int dst_addr_reg,
|
|
|
|
long offset)
|
|
|
|
{
|
|
|
|
/* mov arg3, src_base_addr_reg */
|
|
|
|
if (src_base_addr_reg != BPF_REG_ARG3)
|
|
|
|
ins(BPF_MOV64_REG(BPF_REG_ARG3, src_base_addr_reg), pos);
|
|
|
|
/* add arg3, #offset */
|
|
|
|
if (offset)
|
|
|
|
ins(BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG3, offset), pos);
|
|
|
|
|
|
|
|
/* mov arg2, #reg_size */
|
|
|
|
ins(BPF_ALU64_IMM(BPF_MOV, BPF_REG_ARG2, BPF_REG_SIZE), pos);
|
|
|
|
|
|
|
|
/* mov arg1, dst_addr_reg */
|
|
|
|
if (dst_addr_reg != BPF_REG_ARG1)
|
|
|
|
ins(BPF_MOV64_REG(BPF_REG_ARG1, dst_addr_reg), pos);
|
|
|
|
|
|
|
|
/* Call probe_read */
|
|
|
|
ins(BPF_EMIT_CALL(BPF_FUNC_probe_read), pos);
|
|
|
|
/*
|
|
|
|
* Error processing: if read fail, goto error code,
|
|
|
|
* will be relocated. Target should be the start of
|
|
|
|
* error processing code.
|
|
|
|
*/
|
|
|
|
ins(BPF_JMP_IMM(BPF_JNE, BPF_REG_0, 0, JMP_TO_ERROR_CODE),
|
|
|
|
pos);
|
|
|
|
|
|
|
|
return check_pos(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Each arg should be bare register. Fetch and save them into argument
|
|
|
|
* registers (r3 - r5).
|
|
|
|
*
|
|
|
|
* BPF_REG_1 should have been initialized with pointer to
|
|
|
|
* 'struct pt_regs'.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gen_prologue_fastpath(struct bpf_insn_pos *pos,
|
|
|
|
struct probe_trace_arg *args, int nargs)
|
|
|
|
{
|
|
|
|
int i, err = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nargs; i++) {
|
|
|
|
err = gen_ldx_reg_from_ctx(pos, BPF_REG_1, args[i].value,
|
|
|
|
BPF_PROLOGUE_START_ARG_REG + i);
|
|
|
|
if (err)
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return check_pos(pos);
|
|
|
|
errout:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Slow path:
|
|
|
|
* At least one argument has the form of 'offset($rx)'.
|
|
|
|
*
|
|
|
|
* Following code first stores them into stack, then loads all of then
|
|
|
|
* to r2 - r5.
|
|
|
|
* Before final loading, the final result should be:
|
|
|
|
*
|
|
|
|
* low address
|
|
|
|
* BPF_REG_FP - 24 ARG3
|
|
|
|
* BPF_REG_FP - 16 ARG2
|
|
|
|
* BPF_REG_FP - 8 ARG1
|
|
|
|
* BPF_REG_FP
|
|
|
|
* high address
|
|
|
|
*
|
|
|
|
* For each argument (described as: offn(...off2(off1(reg)))),
|
|
|
|
* generates following code:
|
|
|
|
*
|
|
|
|
* r7 <- fp
|
|
|
|
* r7 <- r7 - stack_offset // Ideal code should initialize r7 using
|
|
|
|
* // fp before generating args. However,
|
|
|
|
* // eBPF won't regard r7 as stack pointer
|
|
|
|
* // if it is generated by minus 8 from
|
|
|
|
* // another stack pointer except fp.
|
|
|
|
* // This is why we have to set r7
|
|
|
|
* // to fp for each variable.
|
|
|
|
* r3 <- value of 'reg'-> generated using gen_ldx_reg_from_ctx()
|
|
|
|
* (r7) <- r3 // skip following instructions for bare reg
|
|
|
|
* r3 <- r3 + off1 . // skip if off1 == 0
|
|
|
|
* r2 <- 8 \
|
|
|
|
* r1 <- r7 |-> generated by gen_read_mem()
|
|
|
|
* call probe_read /
|
|
|
|
* jnei r0, 0, err ./
|
|
|
|
* r3 <- (r7)
|
|
|
|
* r3 <- r3 + off2 . // skip if off2 == 0
|
|
|
|
* r2 <- 8 \ // r2 may be broken by probe_read, so set again
|
|
|
|
* r1 <- r7 |-> generated by gen_read_mem()
|
|
|
|
* call probe_read /
|
|
|
|
* jnei r0, 0, err ./
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gen_prologue_slowpath(struct bpf_insn_pos *pos,
|
|
|
|
struct probe_trace_arg *args, int nargs)
|
|
|
|
{
|
|
|
|
int err, i;
|
|
|
|
|
|
|
|
for (i = 0; i < nargs; i++) {
|
|
|
|
struct probe_trace_arg *arg = &args[i];
|
|
|
|
const char *reg = arg->value;
|
|
|
|
struct probe_trace_arg_ref *ref = NULL;
|
|
|
|
int stack_offset = (i + 1) * -8;
|
|
|
|
|
|
|
|
pr_debug("prologue: fetch arg %d, base reg is %s\n",
|
|
|
|
i, reg);
|
|
|
|
|
|
|
|
/* value of base register is stored into ARG3 */
|
|
|
|
err = gen_ldx_reg_from_ctx(pos, BPF_REG_CTX, reg,
|
|
|
|
BPF_REG_ARG3);
|
|
|
|
if (err) {
|
|
|
|
pr_err("prologue: failed to get offset of register %s\n",
|
|
|
|
reg);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make r7 the stack pointer. */
|
|
|
|
ins(BPF_MOV64_REG(BPF_REG_7, BPF_REG_FP), pos);
|
|
|
|
/* r7 += -8 */
|
|
|
|
ins(BPF_ALU64_IMM(BPF_ADD, BPF_REG_7, stack_offset), pos);
|
|
|
|
/*
|
|
|
|
* Store r3 (base register) onto stack
|
|
|
|
* Ensure fp[offset] is set.
|
|
|
|
* fp is the only valid base register when storing
|
|
|
|
* into stack. We are not allowed to use r7 as base
|
|
|
|
* register here.
|
|
|
|
*/
|
|
|
|
ins(BPF_STX_MEM(BPF_DW, BPF_REG_FP, BPF_REG_ARG3,
|
|
|
|
stack_offset), pos);
|
|
|
|
|
|
|
|
ref = arg->ref;
|
|
|
|
while (ref) {
|
|
|
|
pr_debug("prologue: arg %d: offset %ld\n",
|
|
|
|
i, ref->offset);
|
|
|
|
err = gen_read_mem(pos, BPF_REG_3, BPF_REG_7,
|
|
|
|
ref->offset);
|
|
|
|
if (err) {
|
|
|
|
pr_err("prologue: failed to generate probe_read function call\n");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = ref->next;
|
|
|
|
/*
|
|
|
|
* Load previous result into ARG3. Use
|
|
|
|
* BPF_REG_FP instead of r7 because verifier
|
|
|
|
* allows FP based addressing only.
|
|
|
|
*/
|
|
|
|
if (ref)
|
|
|
|
ins(BPF_LDX_MEM(BPF_DW, BPF_REG_ARG3,
|
|
|
|
BPF_REG_FP, stack_offset), pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Final pass: read to registers */
|
2017-08-15 16:21:59 +07:00
|
|
|
for (i = 0; i < nargs; i++) {
|
|
|
|
int insn_sz = (args[i].ref) ? argtype_to_ldx_size(args[i].type) : BPF_DW;
|
|
|
|
|
|
|
|
pr_debug("prologue: load arg %d, insn_sz is %s\n",
|
|
|
|
i, insn_sz_to_str(insn_sz));
|
|
|
|
ins(BPF_LDX_MEM(insn_sz, BPF_PROLOGUE_START_ARG_REG + i,
|
perf bpf: Add prologue for BPF programs for fetching arguments
This patch generates a prologue for a BPF program which fetches arguments for
it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx
pointer. r2 is used to indicate if dereferencing was done successfully.
This patch uses r6 to hold ctx (struct pt_regs) and r7 to hold stack pointer
for result. Result of each arguments first store on stack:
low address
BPF_REG_FP - 24 ARG3
BPF_REG_FP - 16 ARG2
BPF_REG_FP - 8 ARG1
BPF_REG_FP
high address
Then loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register
r7 <- fp
r7 <- r7 - stack_offset // pointer to result slot
/* load r3 with the offset in pt_regs of 'reg' */
(r7) <- r3 // make slot valid
r3 <- r3 + off1 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7 // result put onto stack
call probe_read // read unsafe pointer
jnei r0, 0, err // error checking
r3 <- (r7) // read result
r3 <- r3 + off2 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7
call probe_read
jnei r0, 0, err
...
/* load r2, r3, r4 from stack */
goto success
err:
r2 <- 1
/* load r3, r4, r5 with 0 */
goto usercode
success:
r2 <- 0
usercode:
r1 <- r6 // restore ctx
// original user code
If all of arguments reside in register (dereferencing is not
required), gen_prologue_fastpath() will be used to create
fast prologue:
r3 <- (r1 + offset of reg1)
r4 <- (r1 + offset of reg2)
r5 <- (r1 + offset of reg3)
r2 <- 0
P.S.
eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stack
Committer note:
At least testing if it builds and loads:
# cat test_probe_arg.c
struct pt_regs;
__attribute__((section("lock_page=__lock_page page->flags"), used))
int func(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
char _license[] __attribute__((section("license"), used)) = "GPL";
int _version __attribute__((section("version"), used)) = 0x40300;
# perf record -e ./test_probe_arg.c usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.016 MB perf.data ]
# perf evlist
perf_bpf_probe:lock_page
#
Signed-off-by: He Kuang <hekuang@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1447675815-166222-11-git-send-email-wangnan0@huawei.com
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-16 19:10:12 +07:00
|
|
|
BPF_REG_FP, -BPF_REG_SIZE * (i + 1)), pos);
|
2017-08-15 16:21:59 +07:00
|
|
|
}
|
perf bpf: Add prologue for BPF programs for fetching arguments
This patch generates a prologue for a BPF program which fetches arguments for
it. With this patch, the program can have arguments as follow:
SEC("lock_page=__lock_page page->flags")
int lock_page(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
This patch passes at most 3 arguments from r3, r4 and r5. r1 is still the ctx
pointer. r2 is used to indicate if dereferencing was done successfully.
This patch uses r6 to hold ctx (struct pt_regs) and r7 to hold stack pointer
for result. Result of each arguments first store on stack:
low address
BPF_REG_FP - 24 ARG3
BPF_REG_FP - 16 ARG2
BPF_REG_FP - 8 ARG1
BPF_REG_FP
high address
Then loaded into r3, r4 and r5.
The output prologue for offn(...off2(off1(reg)))) should be:
r6 <- r1 // save ctx into a callee saved register
r7 <- fp
r7 <- r7 - stack_offset // pointer to result slot
/* load r3 with the offset in pt_regs of 'reg' */
(r7) <- r3 // make slot valid
r3 <- r3 + off1 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7 // result put onto stack
call probe_read // read unsafe pointer
jnei r0, 0, err // error checking
r3 <- (r7) // read result
r3 <- r3 + off2 // prepare to read unsafe pointer
r2 <- 8
r1 <- r7
call probe_read
jnei r0, 0, err
...
/* load r2, r3, r4 from stack */
goto success
err:
r2 <- 1
/* load r3, r4, r5 with 0 */
goto usercode
success:
r2 <- 0
usercode:
r1 <- r6 // restore ctx
// original user code
If all of arguments reside in register (dereferencing is not
required), gen_prologue_fastpath() will be used to create
fast prologue:
r3 <- (r1 + offset of reg1)
r4 <- (r1 + offset of reg2)
r5 <- (r1 + offset of reg3)
r2 <- 0
P.S.
eBPF calling convention is defined as:
* r0 - return value from in-kernel function, and exit value
for eBPF program
* r1 - r5 - arguments from eBPF program to in-kernel function
* r6 - r9 - callee saved registers that in-kernel function will
preserve
* r10 - read-only frame pointer to access stack
Committer note:
At least testing if it builds and loads:
# cat test_probe_arg.c
struct pt_regs;
__attribute__((section("lock_page=__lock_page page->flags"), used))
int func(struct pt_regs *ctx, int err, unsigned long flags)
{
return 1;
}
char _license[] __attribute__((section("license"), used)) = "GPL";
int _version __attribute__((section("version"), used)) = 0x40300;
# perf record -e ./test_probe_arg.c usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.016 MB perf.data ]
# perf evlist
perf_bpf_probe:lock_page
#
Signed-off-by: He Kuang <hekuang@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1447675815-166222-11-git-send-email-wangnan0@huawei.com
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-16 19:10:12 +07:00
|
|
|
|
|
|
|
ins(BPF_JMP_IMM(BPF_JA, BPF_REG_0, 0, JMP_TO_SUCCESS_CODE), pos);
|
|
|
|
|
|
|
|
return check_pos(pos);
|
|
|
|
errout:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
prologue_relocate(struct bpf_insn_pos *pos, struct bpf_insn *error_code,
|
|
|
|
struct bpf_insn *success_code, struct bpf_insn *user_code)
|
|
|
|
{
|
|
|
|
struct bpf_insn *insn;
|
|
|
|
|
|
|
|
if (check_pos(pos))
|
|
|
|
return -BPF_LOADER_ERRNO__PROLOGUE2BIG;
|
|
|
|
|
|
|
|
for (insn = pos->begin; insn < pos->pos; insn++) {
|
|
|
|
struct bpf_insn *target;
|
|
|
|
u8 class = BPF_CLASS(insn->code);
|
|
|
|
u8 opcode;
|
|
|
|
|
|
|
|
if (class != BPF_JMP)
|
|
|
|
continue;
|
|
|
|
opcode = BPF_OP(insn->code);
|
|
|
|
if (opcode == BPF_CALL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (insn->off) {
|
|
|
|
case JMP_TO_ERROR_CODE:
|
|
|
|
target = error_code;
|
|
|
|
break;
|
|
|
|
case JMP_TO_SUCCESS_CODE:
|
|
|
|
target = success_code;
|
|
|
|
break;
|
|
|
|
case JMP_TO_USER_CODE:
|
|
|
|
target = user_code;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pr_err("bpf prologue: internal error: relocation failed\n");
|
|
|
|
return -BPF_LOADER_ERRNO__PROLOGUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
insn->off = target - (insn + 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bpf__gen_prologue(struct probe_trace_arg *args, int nargs,
|
|
|
|
struct bpf_insn *new_prog, size_t *new_cnt,
|
|
|
|
size_t cnt_space)
|
|
|
|
{
|
|
|
|
struct bpf_insn *success_code = NULL;
|
|
|
|
struct bpf_insn *error_code = NULL;
|
|
|
|
struct bpf_insn *user_code = NULL;
|
|
|
|
struct bpf_insn_pos pos;
|
|
|
|
bool fastpath = true;
|
|
|
|
int err = 0, i;
|
|
|
|
|
|
|
|
if (!new_prog || !new_cnt)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (cnt_space > BPF_MAXINSNS)
|
|
|
|
cnt_space = BPF_MAXINSNS;
|
|
|
|
|
|
|
|
pos.begin = new_prog;
|
|
|
|
pos.end = new_prog + cnt_space;
|
|
|
|
pos.pos = new_prog;
|
|
|
|
|
|
|
|
if (!nargs) {
|
|
|
|
ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 0),
|
|
|
|
&pos);
|
|
|
|
|
|
|
|
if (check_pos(&pos))
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
*new_cnt = pos_get_cnt(&pos);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nargs > BPF_PROLOGUE_MAX_ARGS) {
|
|
|
|
pr_warning("bpf: prologue: %d arguments are dropped\n",
|
|
|
|
nargs - BPF_PROLOGUE_MAX_ARGS);
|
|
|
|
nargs = BPF_PROLOGUE_MAX_ARGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First pass: validation */
|
|
|
|
for (i = 0; i < nargs; i++) {
|
|
|
|
struct probe_trace_arg_ref *ref = args[i].ref;
|
|
|
|
|
|
|
|
if (args[i].value[0] == '@') {
|
|
|
|
/* TODO: fetch global variable */
|
|
|
|
pr_err("bpf: prologue: global %s%+ld not support\n",
|
|
|
|
args[i].value, ref ? ref->offset : 0);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ref) {
|
|
|
|
/* fastpath is true if all args has ref == NULL */
|
|
|
|
fastpath = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Instruction encodes immediate value using
|
|
|
|
* s32, ref->offset is long. On systems which
|
|
|
|
* can't fill long in s32, refuse to process if
|
|
|
|
* ref->offset too large (or small).
|
|
|
|
*/
|
|
|
|
#ifdef __LP64__
|
|
|
|
#define OFFSET_MAX ((1LL << 31) - 1)
|
|
|
|
#define OFFSET_MIN ((1LL << 31) * -1)
|
|
|
|
if (ref->offset > OFFSET_MAX ||
|
|
|
|
ref->offset < OFFSET_MIN) {
|
|
|
|
pr_err("bpf: prologue: offset out of bound: %ld\n",
|
|
|
|
ref->offset);
|
|
|
|
return -BPF_LOADER_ERRNO__PROLOGUEOOB;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ref = ref->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pr_debug("prologue: pass validation\n");
|
|
|
|
|
|
|
|
if (fastpath) {
|
|
|
|
/* If all variables are registers... */
|
|
|
|
pr_debug("prologue: fast path\n");
|
|
|
|
err = gen_prologue_fastpath(&pos, args, nargs);
|
|
|
|
if (err)
|
|
|
|
goto errout;
|
|
|
|
} else {
|
|
|
|
pr_debug("prologue: slow path\n");
|
|
|
|
|
|
|
|
/* Initialization: move ctx to a callee saved register. */
|
|
|
|
ins(BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1), &pos);
|
|
|
|
|
|
|
|
err = gen_prologue_slowpath(&pos, args, nargs);
|
|
|
|
if (err)
|
|
|
|
goto errout;
|
|
|
|
/*
|
|
|
|
* start of ERROR_CODE (only slow pass needs error code)
|
|
|
|
* mov r2 <- 1 // r2 is error number
|
|
|
|
* mov r3 <- 0 // r3, r4... should be touched or
|
|
|
|
* // verifier would complain
|
|
|
|
* mov r4 <- 0
|
|
|
|
* ...
|
|
|
|
* goto usercode
|
|
|
|
*/
|
|
|
|
error_code = pos.pos;
|
|
|
|
ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 1),
|
|
|
|
&pos);
|
|
|
|
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
ins(BPF_ALU64_IMM(BPF_MOV,
|
|
|
|
BPF_PROLOGUE_START_ARG_REG + i,
|
|
|
|
0),
|
|
|
|
&pos);
|
|
|
|
ins(BPF_JMP_IMM(BPF_JA, BPF_REG_0, 0, JMP_TO_USER_CODE),
|
|
|
|
&pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start of SUCCESS_CODE:
|
|
|
|
* mov r2 <- 0
|
|
|
|
* goto usercode // skip
|
|
|
|
*/
|
|
|
|
success_code = pos.pos;
|
|
|
|
ins(BPF_ALU64_IMM(BPF_MOV, BPF_PROLOGUE_FETCH_RESULT_REG, 0), &pos);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* start of USER_CODE:
|
|
|
|
* Restore ctx to r1
|
|
|
|
*/
|
|
|
|
user_code = pos.pos;
|
|
|
|
if (!fastpath) {
|
|
|
|
/*
|
|
|
|
* Only slow path needs restoring of ctx. In fast path,
|
|
|
|
* register are loaded directly from r1.
|
|
|
|
*/
|
|
|
|
ins(BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX), &pos);
|
|
|
|
err = prologue_relocate(&pos, error_code, success_code,
|
|
|
|
user_code);
|
|
|
|
if (err)
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = check_pos(&pos);
|
|
|
|
if (err)
|
|
|
|
goto errout;
|
|
|
|
|
|
|
|
*new_cnt = pos_get_cnt(&pos);
|
|
|
|
return 0;
|
|
|
|
errout:
|
|
|
|
return err;
|
|
|
|
}
|