2022-10-08 09:06:52

by Li kunyu

[permalink] [raw]
Subject: [PATCH] lib: bpf: Optimized variable usage in the btf_parse_elf function

The following changes were made in the btf_parse_elf function:
1. The initialization assignments of err, fd, scn and elf variables are
removed, and they do not affect function security after analysis.
2. Remove unnecessary assignments to err variable (-error).

Signed-off-by: Li kunyu <[email protected]>
---
tools/lib/bpf/btf.c | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 2d14f1a52d7a..fa9d5fa03da4 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -910,10 +910,10 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
struct btf_ext **btf_ext)
{
Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
- int err = 0, fd = -1, idx = 0;
+ int err, fd, idx = 0;
struct btf *btf = NULL;
- Elf_Scn *scn = NULL;
- Elf *elf = NULL;
+ Elf_Scn *scn;
+ Elf *elf;
GElf_Ehdr ehdr;
size_t shstrndx;

@@ -924,9 +924,8 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,

fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
- err = -errno;
pr_warn("failed to open %s: %s\n", path, strerror(errno));
- return ERR_PTR(err);
+ return ERR_PTR(-errno);
}

err = -LIBBPF_ERRNO__FORMAT;
@@ -987,8 +986,6 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
}
}

- err = 0;
-
if (!btf_data) {
err = -ENOENT;
goto done;
--
2.18.2


2022-10-08 11:32:47

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] lib: bpf: Optimized variable usage in the btf_parse_elf function

Hi Li,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master linus/master v6.0 next-20221007]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Li-kunyu/lib-bpf-Optimized-variable-usage-in-the-btf_parse_elf-function/20221008-162305
base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: s390-defconfig
compiler: s390-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/74268712be9dbfe27fbf4d9b313f88e3f7e7cf0f
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Li-kunyu/lib-bpf-Optimized-variable-usage-in-the-btf_parse_elf-function/20221008-162305
git checkout 74268712be9dbfe27fbf4d9b313f88e3f7e7cf0f
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=s390 prepare

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

All errors (new ones prefixed by >>):

scripts/genksyms/parse.y: warning: 9 shift/reduce conflicts [-Wconflicts-sr]
scripts/genksyms/parse.y: warning: 5 reduce/reduce conflicts [-Wconflicts-rr]
scripts/genksyms/parse.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
btf.c: In function 'btf_parse_elf':
>> btf.c:954:23: error: 'scn' may be used uninitialized [-Werror=maybe-uninitialized]
954 | while ((scn = elf_nextscn(elf, scn)) != NULL) {
| ^~~~~~~~~~~~~~~~~~~~~
btf.c:915:18: note: 'scn' was declared here
915 | Elf_Scn *scn;
| ^~~
cc1: all warnings being treated as errors
make[5]: *** [tools/build/Makefile.build:96: tools/bpf/resolve_btfids/libbpf/staticobjs/btf.o] Error 1
make[5]: *** Waiting for unfinished jobs....
make[4]: *** [Makefile:157: tools/bpf/resolve_btfids/libbpf/staticobjs/libbpf-in.o] Error 2
make[3]: *** [Makefile:55: tools/bpf/resolve_btfids//libbpf/libbpf.a] Error 2
make[2]: *** [Makefile:76: bpf/resolve_btfids] Error 2
make[1]: *** [Makefile:1423: tools/bpf/resolve_btfids] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:231: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (3.01 kB)
config (90.04 kB)
Download all attachments

2022-10-08 12:59:42

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] lib: bpf: Optimized variable usage in the btf_parse_elf function

Hi Li,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]
[also build test ERROR on bpf/master linus/master v6.0 next-20221007]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/Li-kunyu/lib-bpf-Optimized-variable-usage-in-the-btf_parse_elf-function/20221008-162305
base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: x86_64-rhel-8.3-kselftests
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0
reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/74268712be9dbfe27fbf4d9b313f88e3f7e7cf0f
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Li-kunyu/lib-bpf-Optimized-variable-usage-in-the-btf_parse_elf-function/20221008-162305
git checkout 74268712be9dbfe27fbf4d9b313f88e3f7e7cf0f
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=x86_64 prepare

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>

Note: it may well be a FALSE warning. FWIW you are at least aware of it now.
http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings

All errors (new ones prefixed by >>):

btf.c: In function 'btf_parse_elf':
>> btf.c:954:23: error: 'scn' may be used uninitialized in this function [-Werror=maybe-uninitialized]
954 | while ((scn = elf_nextscn(elf, scn)) != NULL) {
| ^~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[5]: *** [tools/build/Makefile.build:96: tools/bpf/resolve_btfids/libbpf/staticobjs/btf.o] Error 1
make[5]: *** Waiting for unfinished jobs....
make[4]: *** [Makefile:157: tools/bpf/resolve_btfids/libbpf/staticobjs/libbpf-in.o] Error 2
make[3]: *** [Makefile:55: tools/bpf/resolve_btfids//libbpf/libbpf.a] Error 2
make[2]: *** [Makefile:76: bpf/resolve_btfids] Error 2
make[1]: *** [Makefile:1422: tools/bpf/resolve_btfids] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:231: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.

--
0-DAY CI Kernel Test Service
https://01.org/lkp


Attachments:
(No filename) (2.47 kB)
config (171.06 kB)
Download all attachments

2022-10-09 07:13:01

by Li kunyu

[permalink] [raw]
Subject: [PATCH] lib: bpf: Optimized variable usage in the btf_parse_elf function

The following changes were made in the btf_parse_elf function:
1. The initialization assignments of err, fd, scn and elf variables are
removed, and they do not affect function security after analysis.
2. Remove unnecessary assignments to err variable (-error).

Signed-off-by: Li kunyu <[email protected]>
---
tools/lib/bpf/btf.c | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 2d14f1a52d7a..fa9d5fa03da4 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -910,10 +910,10 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
struct btf_ext **btf_ext)
{
Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
- int err = 0, fd = -1, idx = 0;
+ int err, fd, idx = 0;
struct btf *btf = NULL;
- Elf_Scn *scn = NULL;
- Elf *elf = NULL;
+ Elf_Scn *scn;
+ Elf *elf;
GElf_Ehdr ehdr;
size_t shstrndx;

@@ -924,9 +924,8 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,

fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
- err = -errno;
pr_warn("failed to open %s: %s\n", path, strerror(errno));
- return ERR_PTR(err);
+ return ERR_PTR(-errno);
}

err = -LIBBPF_ERRNO__FORMAT;
@@ -987,8 +986,6 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
}
}

- err = 0;
-
if (!btf_data) {
err = -ENOENT;
goto done;
--
2.18.2

2022-10-09 07:27:20

by Li kunyu

[permalink] [raw]
Subject:


Hello, I use the main line code (https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git)The compilation can be successful according to the compilation instructions you provided (at present, it is unclear why the bpf repository compiles warnings and compilation fails). Later, I will deploy a new repository and try to compile again.

2022-10-09 08:12:16

by Li kunyu

[permalink] [raw]
Subject:


Hello, I use the main line code( https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git )The compilation can be successful according to the compilation instructions you provided (at present, it is unclear why the bpf repository compiles warnings and compilation fails). Later, I will deploy a new repository and try to compile again.


After writing the reply message, press 'e' to edit the message, and the result is sent to the patch file.....

2022-10-09 12:56:11

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH] lib: bpf: Optimized variable usage in the btf_parse_elf function

On Sun, Oct 09, 2022 at 03:08:28PM +0800, Li kunyu wrote:
> The following changes were made in the btf_parse_elf function:
> 1. The initialization assignments of err, fd, scn and elf variables are
> removed, and they do not affect function security after analysis.
> 2. Remove unnecessary assignments to err variable (-error).
>
> Signed-off-by: Li kunyu <[email protected]>
> ---
> tools/lib/bpf/btf.c | 11 ++++-------
> 1 file changed, 4 insertions(+), 7 deletions(-)
>
> diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
> index 2d14f1a52d7a..fa9d5fa03da4 100644
> --- a/tools/lib/bpf/btf.c
> +++ b/tools/lib/bpf/btf.c
> @@ -910,10 +910,10 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
> struct btf_ext **btf_ext)
> {
> Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
> - int err = 0, fd = -1, idx = 0;
> + int err, fd, idx = 0;
> struct btf *btf = NULL;
> - Elf_Scn *scn = NULL;
> - Elf *elf = NULL;
> + Elf_Scn *scn;
> + Elf *elf;
> GElf_Ehdr ehdr;
> size_t shstrndx;
>
> @@ -924,9 +924,8 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
>
> fd = open(path, O_RDONLY | O_CLOEXEC);
> if (fd < 0) {
> - err = -errno;
> pr_warn("failed to open %s: %s\n", path, strerror(errno));
> - return ERR_PTR(err);
> + return ERR_PTR(-errno);

hum, pr_warn could potentionally change errno

jirka

> }
>
> err = -LIBBPF_ERRNO__FORMAT;
> @@ -987,8 +986,6 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
> }
> }
>
> - err = 0;
> -
> if (!btf_data) {
> err = -ENOENT;
> goto done;
> --
> 2.18.2
>