From patchwork Fri Feb 7 11:22:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Doucha X-Patchwork-Id: 1234876 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48DXwF53VHz9sRR for ; Fri, 7 Feb 2020 22:22:53 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id B62693C23F8 for ; Fri, 7 Feb 2020 12:22:50 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [217.194.8.6]) by picard.linux.it (Postfix) with ESMTP id 4F8CA3C238E for ; Fri, 7 Feb 2020 12:22:38 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id D6E5114149E8 for ; Fri, 7 Feb 2020 12:22:37 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 4F943B21F for ; Fri, 7 Feb 2020 11:22:37 +0000 (UTC) From: Martin Doucha To: ltp@lists.linux.it Date: Fri, 7 Feb 2020 12:22:35 +0100 Message-Id: <20200207112236.16462-2-mdoucha@suse.cz> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200207112236.16462-1-mdoucha@suse.cz> References: <20200207112236.16462-1-mdoucha@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-6.smtp.seeweb.it Subject: [LTP] [PATCH v2 2/3] Split off executable code from bpf/bpf_common.h X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: 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 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 +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 diff --git a/testcases/kernel/syscalls/bpf/bpf_common.c b/testcases/kernel/syscalls/bpf/bpf_common.c new file mode 100644 index 000000000..fce364af8 --- /dev/null +++ b/testcases/kernel/syscalls/bpf/bpf_common.c @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2019-2020 Linux Test Project + */ + +#define TST_NO_DEFAULT_MAIN +#include "tst_test.h" +#include "lapi/bpf.h" +#include "bpf_common.h" + +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; +} diff --git a/testcases/kernel/syscalls/bpf/bpf_common.h b/testcases/kernel/syscalls/bpf/bpf_common.h index f700bede2..e46a519eb 100644 --- a/testcases/kernel/syscalls/bpf/bpf_common.h +++ b/testcases/kernel/syscalls/bpf/bpf_common.h @@ -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