Redesign TST_RETRY_FUNC()
PATCH v2 1/3 Redesign TST_RETRY_FUNC()
The TST_RETRY_FUNC() macro requires a single return value that'll be considered success. This cannot be used with system calls that e.g. return a new file descriptor because the success value is somewhat unpredictable.
Redesign TST_RETRY_FUNC() to accept arbitrary macro/function ECHCK as the second parameter for validating the FUNC return value.
- The loop will end succesfully if ECHCK(ret) evaluates to non-zero.
- The loop will fall through on timeout instead of calling tst_brk().
- errno will be cleared before every FUNC call.
- Add standard check macros for the most common call conventions:
- TST_RETVAL_EQ0(x) - x == 0
- TST_RETVAL_NOTNULL(x) - x != 0 or x != NULL
- TST_RETVAL_GE0(x) - x >= 0
---
Changes since v1: Everything in this patch. Redesign TST_RETRY_FUNC() instead of adding a copy with slightly different functionality.
include/tst_common.h | 45 +++++++++++++++------ testcases/kernel/syscalls/tgkill/tgkill03.c | 8 +++- 2 files changed, 39 insertions(+), 14 deletions(-)
include/tst_common.h +32 -13
@@ -26,35 +26,54 @@/** * TST_RETRY_FUNC() - Repeatedly retry a function with an increasing delay. * @FUNC - The function which will be retried -* @ERET - The value returned from @FUNC on success +* @ECHCK - Function/macro for validating @FUNC return value * -* This macro will call @FUNC in a loop with a delay between retries. If @FUNC -* returns @ERET then the loop exits. The delay between retries starts at one -* micro second and is then doubled each iteration until it exceeds one second -* (the total time sleeping will be approximately one second as well). When the -* delay exceeds one second tst_brk() is called. +* This macro will call @FUNC in a loop with a delay between retries. +* If ECHCK(ret) evaluates to non-zero, the loop ends. The delay between +* retries starts at one microsecond and is then doubled each iteration until +* it exceeds one second (the total time sleeping will be approximately one +* second as well). When the delay exceeds one second, the loop will end. +* The TST_RETRY_FUNC() macro returns the last value returned by @FUNC. */ -#define TST_RETRY_FUNC (FUNC ,ERET ) \ - TST_RETRY_FN_EXP_BACKOFF(FUNC, ERET, 1) +#define TST_RETRY_FUNC (FUNC ,ECHCK ) \ + TST_RETRY_FN_EXP_BACKOFF(FUNC, ECHCK, 1) -#define TST_RETRY_FN_EXP_BACKOFF (FUNC ,ERET ,MAX_DELAY ) \ +#define TST_RETRY_FN_EXP_BACKOFF (FUNC ,ECHCK ,MAX_DELAY ) \ ({ unsigned int tst_delay_, tst_max_delay_; \ + typeof(FUNC) tst_ret_; \ tst_delay_ = 1; \ tst_max_delay_ = tst_multiply_timeout(MAX_DELAY * 1000000); \ for (;;) { \ - typeof(FUNC) tst_ret_ = FUNC; \ - if (tst_ret_ == ERET) \ + errno = 0; \ + tst_ret_ = FUNC; \ + if (ECHCK(tst_ret_)) \ break; \ if (tst_delay_ < tst_max_delay_) { \ usleep(tst_delay_); \ tst_delay_ *= 2; \ } else { \ - tst_brk(TBROK, #FUNC" timed out"); \ + break; \ } \ } \ - ERET; \ + tst_ret_; \ }) +/* +* Return value validation macros for TST_RETRY_FUNC(): +* TST_RETVAL_EQ0() - Check that value is equal to zero +*/ +#define TST_RETVAL_EQ0 (x) (!(x)) + +/* +* TST_RETVAL_NOTNULL() - Check that value is not equal to zero/NULL +*/ +#define TST_RETVAL_NOTNULL (x) (!!(x)) + +/* +* TST_RETVAL_GE0() - Check that value is greater than or equal to zero +*/ +#define TST_RETVAL_GE0 (x) ((x) >= 0) +#define TST_BRK_SUPPORTS_ONLY_TCONF_TBROK (condition) \ do { ((void)sizeof(char[1 - 2 * !!(condition)])); } while (0)
testcases/kernel/syscalls/tgkill/tgkill03.c +7 -1
@@ -14,6 +14,8 @@#include "tst_test.h" #include "tgkill.h" +#define CHECK_ENOENT (x) ((x) == -1 && errno == ENOENT) +static pthread_t child_thread;static pid_t parent_tgid;
@@ -44,6 +46,7 @@ static void setup(void)sigset_t sigusr1;pthread_t defunct_thread;char defunct_tid_path[PATH_MAX ]; +int ret;sigemptyset (&sigusr1);sigaddset (&sigusr1,SIGUSR1 );
@@ -59,7 +62,10 @@ static void setup(void)SAFE_PTHREAD_CREATE (&defunct_thread,NULL , defunct_thread_func,NULL );SAFE_PTHREAD_JOIN (defunct_thread,NULL );sprintf (defunct_tid_path,"/proc/%d/task/%d" ,getpid (), defunct_tid); -TST_RETRY_FN_EXP_BACKOFF (access (defunct_tid_path,R_OK ),-1 ,15 ); + ret =TST_RETRY_FN_EXP_BACKOFF (access (defunct_tid_path,R_OK ), +CHECK_ENOENT ,15 ); +if (!CHECK_ENOENT (ret)) +tst_brk (TBROK ,"Timeout, %s still exists" , defunct_tid_path); }static void cleanup (void )
Signature · 1 line
2.25.0
PATCH v2 1/3 Redesign TST_RETRY_FUNC()
Quoted 14 lines · Martin Doucha
The
TST_RETRY_FUNC()macro requires a single return value that'll be considered success. This cannot be used with system calls that e.g. return a new file descriptor because the success value is somewhat unpredictable.Redesign
TST_RETRY_FUNC()to accept arbitrary macro/function ECHCK as the second parameter for validating the FUNC return value.
- The loop will end succesfully if ECHCK(ret) evaluates to non-zero.
- The loop will fall through on timeout instead of callingtst_brk().
- errno will be cleared before every FUNC call.
- Add standard check macros for the most common call conventions:
-TST_RETVAL_EQ0(x)- x == 0
-TST_RETVAL_NOTNULL(x)- x != 0 or x != NULL
-TST_RETVAL_GE0(x)- x >= 0
Quoted 2 lines
Signed-off-by Martin Doucha <[email protected]>---
Sorry, I forgot to add this to the commit message:
Please add it before pushing.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v2 2/3 Split off executable code from bpf/bpf_common.h
In reply to Martin Doucha---
Changes since v1: This patch was split off from the v1 BPF fix. Code cleanup to prevent future bugs and make the common code more readable.
testcases/kernel/syscalls/bpf/Makefile | 3 ++ testcases/kernel/syscalls/bpf/bpf_common.c | 45 ++++++++++++++++++++++ testcases/kernel/syscalls/bpf/bpf_common.h | 39 ++----------------- 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 testcases/kernel/syscalls/bpf/bpf_common.c
testcases/kernel/syscalls/bpf/Makefile +3 -0
@@ -5,6 +5,9 @@ top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk +FILTER_OUT_MAKE_TARGETS := bpf_common CFLAGS += -D_GNU_SOURCE include $(top_srcdir)/include/mk/generic_leaf_target.mk + +$(MAKE_TARGETS): %: %.o bpf_common.o
testcases/kernel/syscalls/bpf/bpf_common.c new file +45 -0
45 lines not on this page · raw message
testcases/kernel/syscalls/bpf/bpf_common.h +3 -36
@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ /* -* Copyright (c) 2019 Linux Test Project +* Copyright (c) 2019-2020 Linux Test Project */ #ifndef LTP_BPF_COMMON_H
@@ -8,40 +8,7 @@#define BPF_MEMLOCK_ADD (256*1024) -void rlimit_bump_memlock (void ) -{ -struct rlimit memlock_r; - -SAFE_GETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - memlock_r.rlim_cur +=BPF_MEMLOCK_ADD ; -tst_res (TINFO ,"Raising RLIMIT_MEMLOCK to %ld" , - (long )memlock_r.rlim_cur); - -if (memlock_r.rlim_cur <= memlock_r.rlim_max) { -SAFE_SETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - }else if ((geteuid () ==0 )) { - memlock_r.rlim_max +=BPF_MEMLOCK_ADD ; -SAFE_SETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - }else { -tst_res (TINFO ,"Can't raise RLIMIT_MEMLOCK, test may fail " -"due to lack of max locked memory" ); - } -} - -int bpf_map_create (union bpf_attr *attr) -{ -TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (TST_ERR ==EPERM ) { -tst_res (TCONF ,"Hint: check also /proc/sys/kernel/unprivileged_bpf_disabled" ); -tst_brk (TCONF |TTERRNO , -"bpf() requires CAP_SYS_ADMIN on this system" ); - }else { -tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); - } - } - -return TST_RET ; -} +void rlimit_bump_memlock (void ); +int bpf_map_create (union bpf_attr *attr);#endif
Signature · 1 line
2.25.0
PATCH v2 3/3 Fix BPF test program loading issues
In reply to Martin DouchaBPF programs require locked memory which will be released asynchronously. If a BPF program gets loaded too many times too quickly, memory allocation may fail due to race condition with asynchronous cleanup.
Wrap the bpf() test calls in a retry loop to fix the issue.
---
Changes since v1:
- bpf_common.h split was moved to a separate patch
- use redesigned TST_RETRY_FUNC() instead of TST_SPIN_TEST()
- simplify bpf() return value validation
- minor function name refactoring in the common code
testcases/kernel/syscalls/bpf/bpf_common.c | 60 +++++++++++-- testcases/kernel/syscalls/bpf/bpf_common.h | 3 + testcases/kernel/syscalls/bpf/bpf_prog01.c | 97 +++++++--------------- testcases/kernel/syscalls/bpf/bpf_prog02.c | 28 +------ testcases/kernel/syscalls/bpf/bpf_prog03.c | 38 ++++----- 5 files changed, 108 insertions(+), 118 deletions(-)
testcases/kernel/syscalls/bpf/bpf_common.c +55 -5
@@ -30,16 +30,66 @@ void rlimit_bump_memlock(void)int bpf_map_create (union bpf_attr *attr) { -TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { +int ret; + + ret =TST_RETRY_FUNC (bpf (BPF_MAP_CREATE , attr,sizeof (*attr)), +TST_RETVAL_GE0 ); + +if (ret ==-1 ) {if (TST_ERR ==EPERM ) {tst_res (TCONF ,"Hint: check also /proc/sys/kernel/unprivileged_bpf_disabled" ); -tst_brk (TCONF |TTERRNO , +tst_brk (TCONF |TERRNO ,"bpf() requires CAP_SYS_ADMIN on this system" ); }else { -tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); +tst_brk (TBROK |TERRNO ,"Failed to create array map" ); } } -return TST_RET ; +return ret; +} + +void bpf_init_prog_attr (union bpf_attr *attr,const struct bpf_insn *prog, +size_t prog_size,char *log_buf,size_t log_size) +{ +static struct bpf_insn *buf; +static size_t buf_size; +size_t prog_len = prog_size /sizeof (*prog); + +/* all guarded buffers will be free()d automatically by LTP library */ +if (!buf || prog_size > buf_size) { + buf =tst_alloc (prog_size); + buf_size = prog_size; + } + +memcpy (buf, prog, prog_size); +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (buf); + attr->insn_cnt = prog_len; + attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log_buf); + attr->log_size = log_size; + attr->log_level =1 ; +} + +int bpf_load_prog (union bpf_attr *attr,const char *log) +{ +int ret; + + ret =TST_RETRY_FUNC (bpf (BPF_PROG_LOAD , attr,sizeof (*attr)), +TST_RETVAL_GE0 ); + +if (ret >=0 ) { +tst_res (TPASS ,"Loaded program" ); +return ret; + } + +if (ret !=-1 ) +tst_brk (TBROK ,"Invalid bpf() return value: %d" , ret); + +if (log[0 ] !=0 ) +tst_brk (TBROK |TERRNO ,"Failed verification: %s" , log); + +tst_brk (TBROK |TERRNO ,"Failed to load program" ); +return ret; }
testcases/kernel/syscalls/bpf/bpf_common.h +3 -0
@@ -10,5 +10,8 @@void rlimit_bump_memlock (void );int bpf_map_create (union bpf_attr *attr); +void bpf_init_prog_attr (union bpf_attr *attr,const struct bpf_insn *prog, +size_t prog_size,char *log_buf,size_t log_size); +int bpf_load_prog (union bpf_attr *attr,const char *log);#endif
testcases/kernel/syscalls/bpf/bpf_prog01.c +31 -66
126 lines not on this page · raw message
testcases/kernel/syscalls/bpf/bpf_prog02.c +2 -26
@@ -37,7 +37,6 @@ static union bpf_attr *attr;static int load_prog (int fd) { -static struct bpf_insn *prog;struct bpf_insn insn[] = {BPF_MOV64_IMM (BPF_REG_6 ,1 ),/* 0: r6 = 1 */
@@ -67,31 +66,8 @@ static int load_prog(int fd) BPF_EXIT_INSN(), /* 26: return r0 */ }; -if (!prog) - prog =tst_alloc (sizeof (insn)); -memcpy (prog, insn,sizeof (insn)); - -memset (attr,0 ,sizeof (*attr)); - attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; - attr->insns =ptr_to_u64 (prog); - attr->insn_cnt =ARRAY_SIZE (insn); - attr->license =ptr_to_u64 ("GPL" ); - attr->log_buf =ptr_to_u64 (log); - attr->log_size =BUFSIZ ; - attr->log_level =1 ; - -TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (log[0 ] !=0 ) { -tst_res (TINFO ,"Verification log:" ); -fputs (log, stderr); -tst_brk (TBROK |TTERRNO ,"Failed verification" ); - }else { -tst_brk (TBROK |TTERRNO ,"Failed to load program" ); - } - } - -return TST_RET ; +bpf_init_prog_attr (attr, insn,sizeof (insn), log,BUFSIZ ); +return bpf_load_prog (attr, log); }static void setup (void )
testcases/kernel/syscalls/bpf/bpf_prog03.c +17 -21
@@ -32,6 +32,8 @@#define LOG_SIZE (1024 * 1024) +#define CHECK_BPF_RET (x) ((x) >= 0 || ((x) == -1 && errno != EPERM)) +static const char MSG [] ="Ahoj!" ;static char *msg;
@@ -42,7 +44,8 @@ static union bpf_attr *attr;static int load_prog (int fd) { -static struct bpf_insn *prog; +int ret; +struct bpf_insn insn[] = {BPF_LD_MAP_FD (BPF_REG_1 , fd),
@@ -85,31 +88,24 @@ static int load_prog(int fd) BPF_EXIT_INSN() }; -if (!prog) - prog =tst_alloc (sizeof (insn)); -memcpy (prog, insn,sizeof (insn)); +bpf_init_prog_attr (attr, insn,sizeof (insn), log,LOG_SIZE ); + ret =TST_RETRY_FUNC (bpf (BPF_PROG_LOAD , attr,sizeof (*attr)), +CHECK_BPF_RET ); -memset (attr,0 ,sizeof (*attr)); - attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; - attr->insns =ptr_to_u64 (prog); - attr->insn_cnt =ARRAY_SIZE (insn); - attr->license =ptr_to_u64 ("GPL" ); - attr->log_buf =ptr_to_u64 (log); - attr->log_size =LOG_SIZE ; - attr->log_level =1 ; - -TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (log[0 ] !=0 ) -tst_res (TPASS |TTERRNO ,"Failed verification" ); -else -tst_brk (TBROK |TTERRNO ,"Failed to load program" ); - }else { +if (ret <-1 ) +tst_brk (TBROK ,"Invalid bpf() return value %d" , ret); + +if (ret >=0 ) {tst_res (TINFO ,"Verification log:" );fputs (log, stderr); +return ret; } -return TST_RET ; +if (log[0 ] ==0 ) +tst_brk (TBROK |TERRNO ,"Failed to load program" ); + +tst_res (TPASS |TERRNO ,"Failed verification" ); +return ret; }static void setup (void )
Signature · 1 line
2.25.0
PATCH v2 1/3 Redesign TST_RETRY_FUNC()
In reply to Martin DouchaHi Martin,
Quoted 5 lines · Martin Doucha
The
TST_RETRY_FUNC()macro requires a single return value that'll be considered
success. This cannot be used with system calls that e.g. return a new file descriptor because the success value is somewhat unpredictable.
Quoted 10 lines
Redesign
TST_RETRY_FUNC()to accept arbitrary macro/function ECHCK as the second
parameter for validating the FUNC return value.
- The loop will end succesfully if ECHCK(ret) evaluates to non-zero.
- The loop will fall through on timeout instead of callingtst_brk().
- errno will be cleared before every FUNC call.
- Add standard check macros for the most common call conventions:
-TST_RETVAL_EQ0(x)- x == 0
-TST_RETVAL_NOTNULL(x)- x != 0 or x != NULL
-TST_RETVAL_GE0(x)- x >= 0
Nice to see this enhancement! Few comments as below:
1. We need to update the doc/test-writing-guidelines.txt too.
2. Maybe better to let the shell version is consistent with this new?
3. I remember there were discussions to support enabling infinite loop in TST_RETRY_FUNC, but not sure if it is possible to add in this patch, or we can do that after your patch merged.
http://lists.linux.it/pipermail/ltp/2019-October/013896.html
...
testcases/kernel/syscalls/tgkill/tgkill03.c static void setup(void) · line 66
Quoted 6 lines
sprintf(defunct_tid_path, "/proc/%d/task/%d", getpid(),
defunct_tid);testcases/kernel/syscalls/tgkill/tgkill03.c +2
-1-TST_RETRY_FN_EXP_BACKOFF (access (defunct_tid_path,R_OK ),-1 ,15 ); + ret =TST_RETRY_FN_EXP_BACKOFF (access (defunct_tid_path,R_OK ), +CHECK_ENOENT ,15 );
The test total timeout is set to 20 seconds, here reserve 15 seconds is too much for the macro looping because doing exponential backoff in 15secs(1us+2us+4us+..) actually larger than the 20secs. So I suggest raising the tst_test.timeout at the same time or set a smaller value to MAX_DELAY.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200208/6c9f4ea9/attachment.htm>
PATCH v2 1/3 Redesign TST_RETRY_FUNC()
Quoted 1 line · Li Wang
1. We need to update the doc/test-writing-guidelines.txt?too.
Right. I'll resubmit in a moment.
Quoted 1 line
2. Maybe better to let the shell version is consistent with this new?
That doesn't make much sense. Shell programs and functions have much simpler call conventions than C functions. If you really need to test a more complex result than a single return value in shell, writing a wrapper function is much easier than writing a validator function.
In C, it's the other way around. Writing a wrapper function would often be a ton of work compared to writing a simple retval validator macro.
Quoted 4 lines
3. I remember there were discussions to support enabling infinite loop in?TST_RETRY_FUNC, but not sure if it is possible to add in this patch, or we can do that after your patch merged.
http://lists.linux.it/pipermail/ltp/2019-October/013896.html
I'll leave that to someone else. Though I'd say that timeout of (1ULL<<40) should be infinite enough for everybody. All we need to do is change tst_delay_ and tst_max_delay_ type to unsigned long long.
Quoted 9 lines
...
? ? ? ? sprintf(defunct_tid_path, "/proc/%d/task/%d", getpid(),
defunct_tid);
-? ? ? ?TST_RETRY_FN_EXP_BACKOFF(access(defunct_tid_path, R_OK), -1,
15);
+? ? ? ?ret = TST_RETRY_FN_EXP_BACKOFF(access(defunct_tid_path, R_OK),
+? ? ? ? ? ? ? ?CHECK_ENOENT, 15);Quoted 5 lines
The test total timeout is set to 20 seconds, here reserve 15 seconds is too much for the macro looping because doing exponential backoff in 15secs(1us+2us+4us+..) actually larger than the 20secs. So I suggest raising the tst_test.timeout?at the same time or set a smaller value to
MAX_DELAY.
Actually, this entire retry loop will never take longer than 17 seconds. The last single delay will be at most 8.4 seconds (2^23 microseconds) long and the total combined delay before that will also take 8.4 seconds. The next delay would be 16.8 seconds which is too much so the loop will end. The main test function takes only a few milliseconds so there's no problem even in the worst case scenario.
I can change the delay to 9 seconds if you want. It'll make no difference in practice but the code will be less confusing to humans.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v2 1/3 Redesign TST_RETRY_FUNC()
Hi Martin,
Quoted 4 lines · Li Wang
Quoted 1 line
1. We need to update the doc/test-writing-guidelines.txt?too.
Right. I'll resubmit in a moment.
Thanks!
Quoted 9 lines
Quoted 1 line
2. Maybe better to let the shell version is consistent with this new?
That doesn't make much sense. Shell programs and functions have much simpler call conventions than C functions. If you really need to test a more complex result than a single return value in shell, writing a wrapper function is much easier than writing a validator function.
In C, it's the other way around. Writing a wrapper function would often be a ton of work compared to writing a simple retval validator macro.
+1.
Kind regards,
Petr
PATCH v2 1/3 Redesign TST_RETRY_FUNC()
In reply to Martin DouchaQuoted 12 lines · Martin Doucha
Quoted 1 line · Li Wang
1. We need to update the
doc/test-writing-guidelines.txttoo.Right. I'll resubmit in a moment.
Quoted 1 line
2. Maybe better to let the shell version is consistent with this new?
That doesn't make much sense. Shell programs and functions have much simpler call conventions than C functions. If you really need to test a more complex result than a single return value in shell, writing a wrapper function is much easier than writing a validator function.
According to the default convention of LTP, we maintain two versions of LTP APIs(C and Shell), we always keep them consistent for creating unified tests.
But here I'm OK to reserve a difference for the TST_RETRY_FUNC macro because it looks a bit complicated to achieve the same one in shell. If anyone has different thought please let me know :).
Quoted 12 lines
In C, it's the other way around. Writing a wrapper function would often be a ton of work compared to writing a simple retval validator macro.
Quoted 4 lines
3. I remember there were discussions to support enabling infinite loop in
TST_RETRY_FUNC, but not sure if it is possible to add in this patch, or we can do that after your patch merged.
http://lists.linux.it/pipermail/ltp/2019-October/013896.htmlI'll leave that to someone else. Though I'd say that timeout of (1ULL<<40) should be infinite enough for everybody. All we need to do is change
tst_delay_andtst_max_delay_type to unsigned long long.
Right. It seems no special need to achieve the infinite loop so far.
Quoted 37 lines
Quoted 7 lines
... sprintf(defunct_tid_path, "/proc/%d/task/%d", getpid(), defunct_tid); - TST_RETRY_FN_EXP_BACKOFF(access(defunct_tid_path, R_OK), -1, 15); + ret = TST_RETRY_FN_EXP_BACKOFF(access(defunct_tid_path,
R_OK),Quoted 8 lines
+
CHECK_ENOENT, 15);The test total timeout is set to 20 seconds, here reserve 15 seconds is too much for the macro looping because doing exponential backoff in 15secs(1us+2us+4us+..) actually larger than the 20secs. So I suggest raising the tst_test.timeout at the same time or set a smaller value to
MAX_DELAY.Actually, this entire retry loop will never take longer than 17 seconds. The last single delay will be at most 8.4 seconds (2^23 microseconds) long and the total combined delay before that will also take 8.4 seconds. The next delay would be 16.8 seconds which is too much so the loop will end. The main test function takes only a few milliseconds so there's no problem even in the worst case scenario.
I can change the delay to 9 seconds if you want. It'll make no difference in practice but the code will be less confusing to humans.
--
Martin Doucha [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200218/7eec2269/attachment-0001.htm>
PATCH v3 1/3 Redesign TST_RETRY_FUNC()
In reply to Li WangThe TST_RETRY_FUNC() macro requires a single return value that'll be considered success. This cannot be used with system calls that e.g. return a new file descriptor because the success value is somewhat unpredictable.
Redesign TST_RETRY_FUNC() to accept arbitrary macro/function ECHCK as the second parameter for validating the FUNC return value.
- The loop will end succesfully if ECHCK(ret) evaluates to non-zero.
- The loop will fall through on timeout instead of calling tst_brk().
- errno will be cleared before every FUNC call.
- Add standard check macros for the most common call conventions:
- TST_RETVAL_EQ0(x) - x == 0
- TST_RETVAL_NOTNULL(x) - x != 0 or x != NULL
- TST_RETVAL_GE0(x) - x >= 0
---
Changes since v1: Everything in this patch. Redesign TST_RETRY_FUNC() instead of adding a copy with slightly different functionality.
Changes since v2: Added documentation for new TST_RETRY_FUNC() behavior
doc/test-writing-guidelines.txt | 61 ++++++++++++++++----- include/tst_common.h | 45 ++++++++++----- testcases/kernel/syscalls/tgkill/tgkill03.c | 8 ++- 3 files changed, 86 insertions(+), 28 deletions(-)
doc/test-writing-guidelines.txt +47 -14
@@ -2317,30 +2317,63 @@ that can sleep for defined amount of seconds, milliseconds or microseconds. tst_sleep 100ms ------------------------------------------------------------------------------- -Retry a function in limited time -++++++++++++++++++++++++++++++++ - -Sometimes LTP test needs retrying a function for many times to get success. -This achievement makes that possible via keeping it retrying if the return -value of the function is NOT as we expected. After exceeding a limited time, -test will break from the retries immediately. The time limit is multiplied -with LTP_TIMEOUT_MUL. +Retry a function call multiple times +++++++++++++++++++++++++++++++++++++ + +Sometimes your LTP test needs to retry a function call multiple times because +the system is not ready to process it successfully on the first try. The LTP +library has some useful tools to handle call retrying automatically. +'TST_RETRY_FUNC()' will keep retrying for up to 1 second. If you want a custom +time limit, use 'TST_RETRY_FN_EXP_BACKOFF()'. Both methods return the value +returned by the last 'FUNC' call. + +The delay between retries starts at 1 microsecond and doubles after each call. +The retry loop ends when the function call succeeds or the next delay exceeds +the specified time (1 second for 'TST_RETRY_FUNC()'). The maximum delay is +multiplied by TST_TIMEOUT_MUL. The total cumulative delay may be up to twice +as long as the adjusted maximum delay. + +The C version of 'TST_RETRY_FUNC()' is a macro which takes two arguments: + +* 'FUNC' is the complete function call with arguments which should be retried + multiple times. +* 'SUCCESS_CHECK' is a macro or function which will validate 'FUNC' return + value. 'FUNC' call was successful if 'SUCCESS_CHECK(ret)' evaluates to + non-zero. + +Both retry methods clear 'errno' before every 'FUNC' call so your +'SUCCESS_CHECK' can look for specific error codes as well. The LTP library +also includes predefined 'SUCCESS_CHECK' macros for the most common call +conventions: + +* 'TST_RETVAL_EQ0()' - The call was successful if 'FUNC' returned 0 or NULL +* 'TST_RETVAL_NOTNULL()' - The call was successful if 'FUNC' returned any + value other than 0 or NULL. +* 'TST_RETVAL_GE0()' - The call was successful if 'FUNC' returned value >= 0. [source,c] ------------------------------------------------------------------------------- -# retry function in 1 second -TST_RETRY_FUNC(FUNC, EXPECTED_RET) +/* Keep trying for 1 second */ +TST_RETRY_FUNC(FUNC, SUCCESS_CHECK) -# retry function in N second -TST_RETRY_FN_EXP_BACKOFF(FUNC, EXPECTED_RET, N) +/* Keep trying for up to 2*N seconds */ +TST_RETRY_FN_EXP_BACKOFF(FUNC, SUCCESS_CHECK, N) ------------------------------------------------------------------------------- +The shell version of 'TST_RETRY_FUNC()' is simpler and takes slightly +different arguments: + +* 'FUNC' is a string containing the complete function or program call with + arguments. +* 'EXPECTED_RET' is a single expected return value. 'FUNC' call was successful + if the return value is equal to EXPECTED_RET. + [source,sh] ------------------------------------------------------------------------------- -# retry function in 1 second +# Keep trying for 1 second TST_RETRY_FUNC "FUNC arg1 arg2 ..." "EXPECTED_RET" -# retry function in N second +# Keep trying for up to 2*N seconds TST_RETRY_FN_EXP_BACKOFF "FUNC arg1 arg2 ..." "EXPECTED_RET" "N" -------------------------------------------------------------------------------
include/tst_common.h +32 -13
67 lines not on this page · raw message
testcases/kernel/syscalls/tgkill/tgkill03.c +7 -1
@@ -14,6 +14,8 @@#include "tst_test.h" #include "tgkill.h" +#define CHECK_ENOENT (x) ((x) == -1 && errno == ENOENT) +static pthread_t child_thread;static pid_t parent_tgid;
@@ -44,6 +46,7 @@ static void setup(void)sigset_t sigusr1;pthread_t defunct_thread;char defunct_tid_path[PATH_MAX ]; +int ret;sigemptyset (&sigusr1);sigaddset (&sigusr1,SIGUSR1 );
@@ -59,7 +62,10 @@ static void setup(void)SAFE_PTHREAD_CREATE (&defunct_thread,NULL , defunct_thread_func,NULL );SAFE_PTHREAD_JOIN (defunct_thread,NULL );sprintf (defunct_tid_path,"/proc/%d/task/%d" ,getpid (), defunct_tid); -TST_RETRY_FN_EXP_BACKOFF (access (defunct_tid_path,R_OK ),-1 ,15 ); + ret =TST_RETRY_FN_EXP_BACKOFF (access (defunct_tid_path,R_OK ), +CHECK_ENOENT ,15 ); +if (!CHECK_ENOENT (ret)) +tst_brk (TBROK ,"Timeout, %s still exists" , defunct_tid_path); }static void cleanup (void )
Signature · 1 line
2.25.0
PATCH v3 2/3 Split off executable code from bpf/bpf_common.h
---
Changes since v1: This patch was split off from the v1 BPF fix. Code cleanup to prevent future bugs and make the common code more readable.
Changes since v2: None.
testcases/kernel/syscalls/bpf/Makefile | 3 ++ testcases/kernel/syscalls/bpf/bpf_common.c | 45 ++++++++++++++++++++++ testcases/kernel/syscalls/bpf/bpf_common.h | 39 ++----------------- 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 testcases/kernel/syscalls/bpf/bpf_common.c
testcases/kernel/syscalls/bpf/Makefile +3 -0
@@ -5,6 +5,9 @@ top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk +FILTER_OUT_MAKE_TARGETS := bpf_common CFLAGS += -D_GNU_SOURCE include $(top_srcdir)/include/mk/generic_leaf_target.mk + +$(MAKE_TARGETS): %: %.o bpf_common.o
testcases/kernel/syscalls/bpf/bpf_common.c new file +45 -0
45 lines not on this page · raw message
testcases/kernel/syscalls/bpf/bpf_common.h +3 -36
@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ /* -* Copyright (c) 2019 Linux Test Project +* Copyright (c) 2019-2020 Linux Test Project */ #ifndef LTP_BPF_COMMON_H
@@ -8,40 +8,7 @@#define BPF_MEMLOCK_ADD (256*1024) -void rlimit_bump_memlock (void ) -{ -struct rlimit memlock_r; - -SAFE_GETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - memlock_r.rlim_cur +=BPF_MEMLOCK_ADD ; -tst_res (TINFO ,"Raising RLIMIT_MEMLOCK to %ld" , - (long )memlock_r.rlim_cur); - -if (memlock_r.rlim_cur <= memlock_r.rlim_max) { -SAFE_SETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - }else if ((geteuid () ==0 )) { - memlock_r.rlim_max +=BPF_MEMLOCK_ADD ; -SAFE_SETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - }else { -tst_res (TINFO ,"Can't raise RLIMIT_MEMLOCK, test may fail " -"due to lack of max locked memory" ); - } -} - -int bpf_map_create (union bpf_attr *attr) -{ -TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (TST_ERR ==EPERM ) { -tst_res (TCONF ,"Hint: check also /proc/sys/kernel/unprivileged_bpf_disabled" ); -tst_brk (TCONF |TTERRNO , -"bpf() requires CAP_SYS_ADMIN on this system" ); - }else { -tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); - } - } - -return TST_RET ; -} +void rlimit_bump_memlock (void ); +int bpf_map_create (union bpf_attr *attr);#endif
Signature · 1 line
2.25.0
PATCH v3 2/3 Split off executable code from bpf/bpf_common.h
Quoted 22 lines · Martin Doucha
Signed-off-by Martin Doucha <[email protected]>---
Changes since v1: This patch was split off from the v1 BPF fix. Code cleanup
to prevent future bugs and make the common code more readable.Changes since v2: None.
testcases/kernel/syscalls/bpf/Makefile | 3 ++ testcases/kernel/syscalls/bpf/bpf_common.c | 45 ++++++++++++++++++++++ testcases/kernel/syscalls/bpf/bpf_common.h | 39 ++----------------- 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 testcases/kernel/syscalls/bpf/bpf_common.ctestcases/kernel/syscalls/bpf/Makefile +0
-0
testcases/kernel/syscalls/bpf/Makefile top_srcdir ?= ../../../.. · line 13
Quoted 9 lines
testcases/kernel/syscalls/bpf/Makefile +3
-0@@ -5,6 +5,9 @@ top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk +FILTER_OUT_MAKE_TARGETS := bpf_common CFLAGS += -D_GNU_SOURCE include $(top_srcdir)/include/mk/generic_leaf_target.mk + +$(MAKE_TARGETS): %: %.o bpf_common.o
May I ask why we need to reserve these *.o binary files in the compiling?
# ls bpf_common.c bpf_common.o bpf_map01.c bpf_prog01 bpf_prog01.o bpf_prog02.c bpf_prog03 bpf_prog03.o bpf_common.h bpf_map01 bpf_map01.o bpf_prog01.c bpf_prog02 bpf_prog02.o bpf_prog03.c Makefile
Otherwise, the patchset looks good.
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200218/f61366ed/attachment.htm>
PATCH v3 2/3 Split off executable code from bpf/bpf_common.h
Quoted 18 lines · Li Wang
diff --git a/testcases/kernel/syscalls/bpf/Makefile
b/testcases/kernel/syscalls/bpf/Makefile
index 990c8c83c..2241bce9b 100644
--- a/testcases/kernel/syscalls/bpf/Makefile
+++ b/testcases/kernel/syscalls/bpf/Makefile
@@ -5,6 +5,9 @@ top_srcdir? ? ? ? ? ? ? ?= ../../../..
?include $(top_srcdir)/include/mk/testcases.mk <http://testcases.mk>
+FILTER_OUT_MAKE_TARGETS? ? ? ? := bpf_common
?CFLAGS? ? ? ? ? ? ? ? ?+= -D_GNU_SOURCE
?include $(top_srcdir)/include/mk/generic_leaf_target.mk
<http://generic_leaf_target.mk>
+
+$(MAKE_TARGETS): %: %.o bpf_common.oQuoted 1 line
May I ask why we need to reserve these *.o binary files in the compiling?
Sorry, I don't understand the question.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v3 2/3 Split off executable code from bpf/bpf_common.h
Quoted 19 lines · Martin Doucha
Quoted 18 lines · Li Wang
diff --git a/testcases/kernel/syscalls/bpf/Makefile b/testcases/kernel/syscalls/bpf/Makefile index 990c8c83c..2241bce9b 100644 --- a/testcases/kernel/syscalls/bpf/Makefile +++ b/testcases/kernel/syscalls/bpf/Makefile @@ -5,6 +5,9 @@ top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk <http://testcases.mk> +FILTER_OUT_MAKE_TARGETS := bpf_common CFLAGS += -D_GNU_SOURCE include $(top_srcdir)/include/mk/generic_leaf_target.mk <http://generic_leaf_target.mk> + +$(MAKE_TARGETS): %: %.o bpf_common.o
Quoted 4 lines
Quoted 1 line
May I ask why we need to reserve these *.o binary files in the compiling?
Sorry, I don't understand the question.
Sorry for the unclear question. I mean can we modify the Makefile as:
testcases/kernel/syscalls/bpf/Makefile +1 -1
@@ -10,4 +10,4 @@ CFLAGS += -D_GNU_SOURCE include $(top_srcdir)/include/mk/generic_leaf_target.mk -$(MAKE_TARGETS): %: %.o bpf_common.o +$(MAKE_TARGETS): %: bpf_common.o
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20200218/e77a3b69/attachment.htm>
PATCH v3 2/3 Split off executable code from bpf/bpf_common.h
Hi,
testcases/kernel/syscalls/bpf/Makefile CFLAGS += -D_GNU_SOURCE · line 13
Quoted 10 lines
Sorry for the unclear question. I mean can we modify the
Makefileas:testcases/kernel/syscalls/bpf/Makefile +1
-1@@ -10,4 +10,4 @@ CFLAGS += -D_GNU_SOURCE include $(top_srcdir)/include/mk/generic_leaf_target.mk -$(MAKE_TARGETS): %: %.o bpf_common.o +$(MAKE_TARGETS): %: bpf_common.o
+1 that removes object files.
Kind regards,
Petr
PATCH v3 2/3 Split off executable code from bpf/bpf_common.h
In reply to Li WangQuoted 10 lines · Li Wang
Sorry for the unclear question. I mean can we modify the
Makefileas:testcases/kernel/syscalls/bpf/Makefile +0
-0@@ -10,4 +10,4 @@ CFLAGS ? ? ? ? ? ? ? ? ? ? ? ?+= -D_GNU_SOURCE?
?include $(top_srcdir)/include/mk/generic_leaf_target.mk ?
-$(MAKE_TARGETS): %: %.o bpf_common.o +$(MAKE_TARGETS): %: bpf_common.o
Ah, OK. I'll fix that.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH v3 2/3 Split off executable code from bpf/bpf_common.h
In reply to Li WangHi,
Quoted 11 lines
May I ask why we need to reserve these *.o binary files in the compiling?
# ls bpf_common.c bpf_common.o bpf_map01.c bpf_prog01 bpf_prog01.o bpf_prog02.c bpf_prog03 bpf_prog03.o bpf_common.h bpf_map01 bpf_map01.o bpf_prog01.c bpf_prog02 bpf_prog02.o bpf_prog03.c MakefileOtherwise, the patchset looks good.
Acked-by Li Wang <[email protected]>
(+1 for Li change to Makefile).
Kind regards,
Petr
PATCH v3 3/3 Fix BPF test program loading issues
In reply to Martin DouchaBPF programs require locked memory which will be released asynchronously. If a BPF program gets loaded too many times too quickly, memory allocation may fail due to race condition with asynchronous cleanup.
Wrap the bpf() test calls in a retry loop to fix the issue.
---
Changes since v1:
- bpf_common.h split was moved to a separate patch
- use redesigned TST_RETRY_FUNC() instead of TST_SPIN_TEST()
- simplify bpf() return value validation
- minor function name refactoring in the common code
Changes since v2: None.
testcases/kernel/syscalls/bpf/bpf_common.c | 60 +++++++++++-- testcases/kernel/syscalls/bpf/bpf_common.h | 3 + testcases/kernel/syscalls/bpf/bpf_prog01.c | 97 +++++++--------------- testcases/kernel/syscalls/bpf/bpf_prog02.c | 28 +------ testcases/kernel/syscalls/bpf/bpf_prog03.c | 38 ++++----- 5 files changed, 108 insertions(+), 118 deletions(-)
testcases/kernel/syscalls/bpf/bpf_common.c +55 -5
@@ -30,16 +30,66 @@ void rlimit_bump_memlock(void)int bpf_map_create (union bpf_attr *attr) { -TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { +int ret; + + ret =TST_RETRY_FUNC (bpf (BPF_MAP_CREATE , attr,sizeof (*attr)), +TST_RETVAL_GE0 ); + +if (ret ==-1 ) {if (TST_ERR ==EPERM ) {tst_res (TCONF ,"Hint: check also /proc/sys/kernel/unprivileged_bpf_disabled" ); -tst_brk (TCONF |TTERRNO , +tst_brk (TCONF |TERRNO ,"bpf() requires CAP_SYS_ADMIN on this system" ); }else { -tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); +tst_brk (TBROK |TERRNO ,"Failed to create array map" ); } } -return TST_RET ; +return ret; +} + +void bpf_init_prog_attr (union bpf_attr *attr,const struct bpf_insn *prog, +size_t prog_size,char *log_buf,size_t log_size) +{ +static struct bpf_insn *buf; +static size_t buf_size; +size_t prog_len = prog_size /sizeof (*prog); + +/* all guarded buffers will be free()d automatically by LTP library */ +if (!buf || prog_size > buf_size) { + buf =tst_alloc (prog_size); + buf_size = prog_size; + } + +memcpy (buf, prog, prog_size); +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (buf); + attr->insn_cnt = prog_len; + attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log_buf); + attr->log_size = log_size; + attr->log_level =1 ; +} + +int bpf_load_prog (union bpf_attr *attr,const char *log) +{ +int ret; + + ret =TST_RETRY_FUNC (bpf (BPF_PROG_LOAD , attr,sizeof (*attr)), +TST_RETVAL_GE0 ); + +if (ret >=0 ) { +tst_res (TPASS ,"Loaded program" ); +return ret; + } + +if (ret !=-1 ) +tst_brk (TBROK ,"Invalid bpf() return value: %d" , ret); + +if (log[0 ] !=0 ) +tst_brk (TBROK |TERRNO ,"Failed verification: %s" , log); + +tst_brk (TBROK |TERRNO ,"Failed to load program" ); +return ret; }
testcases/kernel/syscalls/bpf/bpf_common.h +3 -0
@@ -10,5 +10,8 @@void rlimit_bump_memlock (void );int bpf_map_create (union bpf_attr *attr); +void bpf_init_prog_attr (union bpf_attr *attr,const struct bpf_insn *prog, +size_t prog_size,char *log_buf,size_t log_size); +int bpf_load_prog (union bpf_attr *attr,const char *log);#endif
testcases/kernel/syscalls/bpf/bpf_prog01.c +31 -66
126 lines not on this page · raw message
testcases/kernel/syscalls/bpf/bpf_prog02.c +2 -26
@@ -37,7 +37,6 @@ static union bpf_attr *attr;static int load_prog (int fd) { -static struct bpf_insn *prog;struct bpf_insn insn[] = {BPF_MOV64_IMM (BPF_REG_6 ,1 ),/* 0: r6 = 1 */
@@ -67,31 +66,8 @@ static int load_prog(int fd) BPF_EXIT_INSN(), /* 26: return r0 */ }; -if (!prog) - prog =tst_alloc (sizeof (insn)); -memcpy (prog, insn,sizeof (insn)); - -memset (attr,0 ,sizeof (*attr)); - attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; - attr->insns =ptr_to_u64 (prog); - attr->insn_cnt =ARRAY_SIZE (insn); - attr->license =ptr_to_u64 ("GPL" ); - attr->log_buf =ptr_to_u64 (log); - attr->log_size =BUFSIZ ; - attr->log_level =1 ; - -TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (log[0 ] !=0 ) { -tst_res (TINFO ,"Verification log:" ); -fputs (log, stderr); -tst_brk (TBROK |TTERRNO ,"Failed verification" ); - }else { -tst_brk (TBROK |TTERRNO ,"Failed to load program" ); - } - } - -return TST_RET ; +bpf_init_prog_attr (attr, insn,sizeof (insn), log,BUFSIZ ); +return bpf_load_prog (attr, log); }static void setup (void )
testcases/kernel/syscalls/bpf/bpf_prog03.c +17 -21
@@ -32,6 +32,8 @@#define LOG_SIZE (1024 * 1024) +#define CHECK_BPF_RET (x) ((x) >= 0 || ((x) == -1 && errno != EPERM)) +static const char MSG [] ="Ahoj!" ;static char *msg;
@@ -42,7 +44,8 @@ static union bpf_attr *attr;static int load_prog (int fd) { -static struct bpf_insn *prog; +int ret; +struct bpf_insn insn[] = {BPF_LD_MAP_FD (BPF_REG_1 , fd),
@@ -85,31 +88,24 @@ static int load_prog(int fd) BPF_EXIT_INSN() }; -if (!prog) - prog =tst_alloc (sizeof (insn)); -memcpy (prog, insn,sizeof (insn)); +bpf_init_prog_attr (attr, insn,sizeof (insn), log,LOG_SIZE ); + ret =TST_RETRY_FUNC (bpf (BPF_PROG_LOAD , attr,sizeof (*attr)), +CHECK_BPF_RET ); -memset (attr,0 ,sizeof (*attr)); - attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; - attr->insns =ptr_to_u64 (prog); - attr->insn_cnt =ARRAY_SIZE (insn); - attr->license =ptr_to_u64 ("GPL" ); - attr->log_buf =ptr_to_u64 (log); - attr->log_size =LOG_SIZE ; - attr->log_level =1 ; - -TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (log[0 ] !=0 ) -tst_res (TPASS |TTERRNO ,"Failed verification" ); -else -tst_brk (TBROK |TTERRNO ,"Failed to load program" ); - }else { +if (ret <-1 ) +tst_brk (TBROK ,"Invalid bpf() return value %d" , ret); + +if (ret >=0 ) {tst_res (TINFO ,"Verification log:" );fputs (log, stderr); +return ret; } -return TST_RET ; +if (log[0 ] ==0 ) +tst_brk (TBROK |TERRNO ,"Failed to load program" ); + +tst_res (TPASS |TERRNO ,"Failed verification" ); +return ret; }static void setup (void )
Signature · 1 line
2.25.0
PATCH v3 3/3 Fix BPF test program loading issues
PATCH v3 3/3 Fix BPF test program loading issues
Hi,
Fixes issues on ppc64le (previously failing).
Martin, thanks for your fix and whole cleanup.
Kind regards,
Petr
PATCH v3 1/3 Redesign TST_RETRY_FUNC()
In reply to Martin DouchaKind regards,
Petr
PATCH v4 2/3 Split off executable code from bpf/bpf_common.h
In reply to Martin Doucha---
Changes since v1: This patch was split off from the v1 BPF fix. Code cleanup to prevent future bugs and make the common code more readable.
Changes since v2: None.
Changes since v3: Don't generate separate .o files for test programs.
testcases/kernel/syscalls/bpf/Makefile | 3 ++ testcases/kernel/syscalls/bpf/bpf_common.c | 45 ++++++++++++++++++++++ testcases/kernel/syscalls/bpf/bpf_common.h | 39 ++----------------- 3 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 testcases/kernel/syscalls/bpf/bpf_common.c
testcases/kernel/syscalls/bpf/Makefile +3 -0
@@ -5,6 +5,9 @@ top_srcdir ?= ../../../.. include $(top_srcdir)/include/mk/testcases.mk +FILTER_OUT_MAKE_TARGETS := bpf_common CFLAGS += -D_GNU_SOURCE include $(top_srcdir)/include/mk/generic_leaf_target.mk + +$(MAKE_TARGETS): %: bpf_common.o
testcases/kernel/syscalls/bpf/bpf_common.c new file +45 -0
45 lines not on this page · raw message
testcases/kernel/syscalls/bpf/bpf_common.h +3 -36
@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-or-later */ /* -* Copyright (c) 2019 Linux Test Project +* Copyright (c) 2019-2020 Linux Test Project */ #ifndef LTP_BPF_COMMON_H
@@ -8,40 +8,7 @@#define BPF_MEMLOCK_ADD (256*1024) -void rlimit_bump_memlock (void ) -{ -struct rlimit memlock_r; - -SAFE_GETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - memlock_r.rlim_cur +=BPF_MEMLOCK_ADD ; -tst_res (TINFO ,"Raising RLIMIT_MEMLOCK to %ld" , - (long )memlock_r.rlim_cur); - -if (memlock_r.rlim_cur <= memlock_r.rlim_max) { -SAFE_SETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - }else if ((geteuid () ==0 )) { - memlock_r.rlim_max +=BPF_MEMLOCK_ADD ; -SAFE_SETRLIMIT (RLIMIT_MEMLOCK , &memlock_r); - }else { -tst_res (TINFO ,"Can't raise RLIMIT_MEMLOCK, test may fail " -"due to lack of max locked memory" ); - } -} - -int bpf_map_create (union bpf_attr *attr) -{ -TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (TST_ERR ==EPERM ) { -tst_res (TCONF ,"Hint: check also /proc/sys/kernel/unprivileged_bpf_disabled" ); -tst_brk (TCONF |TTERRNO , -"bpf() requires CAP_SYS_ADMIN on this system" ); - }else { -tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); - } - } - -return TST_RET ; -} +void rlimit_bump_memlock (void ); +int bpf_map_create (union bpf_attr *attr);#endif
Signature · 1 line
2.25.0
PATCH v4 3/3 Fix BPF test program loading issues
BPF programs require locked memory which will be released asynchronously. If a BPF program gets loaded too many times too quickly, memory allocation may fail due to race condition with asynchronous cleanup.
Wrap the bpf() test calls in a retry loop to fix the issue.
---
Changes since v1:
- bpf_common.h split was moved to a separate patch
- use redesigned TST_RETRY_FUNC() instead of TST_SPIN_TEST()
- simplify bpf() return value validation
- minor function name refactoring in the common code
Changes since v2: None.
testcases/kernel/syscalls/bpf/bpf_common.c | 60 +++++++++++-- testcases/kernel/syscalls/bpf/bpf_common.h | 3 + testcases/kernel/syscalls/bpf/bpf_prog01.c | 97 +++++++--------------- testcases/kernel/syscalls/bpf/bpf_prog02.c | 28 +------ testcases/kernel/syscalls/bpf/bpf_prog03.c | 38 ++++----- 5 files changed, 108 insertions(+), 118 deletions(-)
testcases/kernel/syscalls/bpf/bpf_common.c +55 -5
@@ -30,16 +30,66 @@ void rlimit_bump_memlock(void)int bpf_map_create (union bpf_attr *attr) { -TEST (bpf (BPF_MAP_CREATE , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { +int ret; + + ret =TST_RETRY_FUNC (bpf (BPF_MAP_CREATE , attr,sizeof (*attr)), +TST_RETVAL_GE0 ); + +if (ret ==-1 ) {if (TST_ERR ==EPERM ) {tst_res (TCONF ,"Hint: check also /proc/sys/kernel/unprivileged_bpf_disabled" ); -tst_brk (TCONF |TTERRNO , +tst_brk (TCONF |TERRNO ,"bpf() requires CAP_SYS_ADMIN on this system" ); }else { -tst_brk (TBROK |TTERRNO ,"Failed to create array map" ); +tst_brk (TBROK |TERRNO ,"Failed to create array map" ); } } -return TST_RET ; +return ret; +} + +void bpf_init_prog_attr (union bpf_attr *attr,const struct bpf_insn *prog, +size_t prog_size,char *log_buf,size_t log_size) +{ +static struct bpf_insn *buf; +static size_t buf_size; +size_t prog_len = prog_size /sizeof (*prog); + +/* all guarded buffers will be free()d automatically by LTP library */ +if (!buf || prog_size > buf_size) { + buf =tst_alloc (prog_size); + buf_size = prog_size; + } + +memcpy (buf, prog, prog_size); +memset (attr,0 ,sizeof (*attr)); + attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; + attr->insns =ptr_to_u64 (buf); + attr->insn_cnt = prog_len; + attr->license =ptr_to_u64 ("GPL" ); + attr->log_buf =ptr_to_u64 (log_buf); + attr->log_size = log_size; + attr->log_level =1 ; +} + +int bpf_load_prog (union bpf_attr *attr,const char *log) +{ +int ret; + + ret =TST_RETRY_FUNC (bpf (BPF_PROG_LOAD , attr,sizeof (*attr)), +TST_RETVAL_GE0 ); + +if (ret >=0 ) { +tst_res (TPASS ,"Loaded program" ); +return ret; + } + +if (ret !=-1 ) +tst_brk (TBROK ,"Invalid bpf() return value: %d" , ret); + +if (log[0 ] !=0 ) +tst_brk (TBROK |TERRNO ,"Failed verification: %s" , log); + +tst_brk (TBROK |TERRNO ,"Failed to load program" ); +return ret; }
testcases/kernel/syscalls/bpf/bpf_common.h +3 -0
@@ -10,5 +10,8 @@void rlimit_bump_memlock (void );int bpf_map_create (union bpf_attr *attr); +void bpf_init_prog_attr (union bpf_attr *attr,const struct bpf_insn *prog, +size_t prog_size,char *log_buf,size_t log_size); +int bpf_load_prog (union bpf_attr *attr,const char *log);#endif
testcases/kernel/syscalls/bpf/bpf_prog01.c +31 -66
126 lines not on this page · raw message
testcases/kernel/syscalls/bpf/bpf_prog02.c +2 -26
@@ -37,7 +37,6 @@ static union bpf_attr *attr;static int load_prog (int fd) { -static struct bpf_insn *prog;struct bpf_insn insn[] = {BPF_MOV64_IMM (BPF_REG_6 ,1 ),/* 0: r6 = 1 */
@@ -67,31 +66,8 @@ static int load_prog(int fd) BPF_EXIT_INSN(), /* 26: return r0 */ }; -if (!prog) - prog =tst_alloc (sizeof (insn)); -memcpy (prog, insn,sizeof (insn)); - -memset (attr,0 ,sizeof (*attr)); - attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; - attr->insns =ptr_to_u64 (prog); - attr->insn_cnt =ARRAY_SIZE (insn); - attr->license =ptr_to_u64 ("GPL" ); - attr->log_buf =ptr_to_u64 (log); - attr->log_size =BUFSIZ ; - attr->log_level =1 ; - -TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (log[0 ] !=0 ) { -tst_res (TINFO ,"Verification log:" ); -fputs (log, stderr); -tst_brk (TBROK |TTERRNO ,"Failed verification" ); - }else { -tst_brk (TBROK |TTERRNO ,"Failed to load program" ); - } - } - -return TST_RET ; +bpf_init_prog_attr (attr, insn,sizeof (insn), log,BUFSIZ ); +return bpf_load_prog (attr, log); }static void setup (void )
testcases/kernel/syscalls/bpf/bpf_prog03.c +17 -21
@@ -32,6 +32,8 @@#define LOG_SIZE (1024 * 1024) +#define CHECK_BPF_RET (x) ((x) >= 0 || ((x) == -1 && errno != EPERM)) +static const char MSG [] ="Ahoj!" ;static char *msg;
@@ -42,7 +44,8 @@ static union bpf_attr *attr;static int load_prog (int fd) { -static struct bpf_insn *prog; +int ret; +struct bpf_insn insn[] = {BPF_LD_MAP_FD (BPF_REG_1 , fd),
@@ -85,31 +88,24 @@ static int load_prog(int fd) BPF_EXIT_INSN() }; -if (!prog) - prog =tst_alloc (sizeof (insn)); -memcpy (prog, insn,sizeof (insn)); +bpf_init_prog_attr (attr, insn,sizeof (insn), log,LOG_SIZE ); + ret =TST_RETRY_FUNC (bpf (BPF_PROG_LOAD , attr,sizeof (*attr)), +CHECK_BPF_RET ); -memset (attr,0 ,sizeof (*attr)); - attr->prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; - attr->insns =ptr_to_u64 (prog); - attr->insn_cnt =ARRAY_SIZE (insn); - attr->license =ptr_to_u64 ("GPL" ); - attr->log_buf =ptr_to_u64 (log); - attr->log_size =LOG_SIZE ; - attr->log_level =1 ; - -TEST (bpf (BPF_PROG_LOAD , attr,sizeof (*attr))); -if (TST_RET ==-1 ) { -if (log[0 ] !=0 ) -tst_res (TPASS |TTERRNO ,"Failed verification" ); -else -tst_brk (TBROK |TTERRNO ,"Failed to load program" ); - }else { +if (ret <-1 ) +tst_brk (TBROK ,"Invalid bpf() return value %d" , ret); + +if (ret >=0 ) {tst_res (TINFO ,"Verification log:" );fputs (log, stderr); +return ret; } -return TST_RET ; +if (log[0 ] ==0 ) +tst_brk (TBROK |TERRNO ,"Failed to load program" ); + +tst_res (TPASS |TERRNO ,"Failed verification" ); +return ret; }static void setup (void )
Signature · 1 line
2.25.0
PATCH v3 1/3 Redesign TST_RETRY_FUNC()
In reply to Martin DouchaHi!
I've adjusted the documentation in this patch slightly and pushed the patchset, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]