Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp848537ybm; Tue, 21 May 2019 04:43:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqw+Dhf2a0G5SZ+Ea12oI1wUTUyshsykm8H8jiKR0De4k3c2bp/KU4r/Sslwx3RRP7eUWMkz X-Received: by 2002:aa7:930e:: with SMTP id 14mr29509105pfj.262.1558439009886; Tue, 21 May 2019 04:43:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558439009; cv=none; d=google.com; s=arc-20160816; b=mcTvEB3389hKwqn1JmqpIJO0aWbNTez08CqogNKMPWuj/ylZBnShFmmplgCSWWlJiV 083IhV9P++oLcrgC9p6x2maF+HblDwsqotxK5AzX9vyjznhPUaEPxSe/y9XZeOkN9fXa dIfKk4noQXta176hli/hwibrO5o9ZF2xmMpdGK8WXQeg7uM0ka3KTuEOtBw26sfE5g93 oAxAFhVptJoqaKmeE5HrQ4owGWkXKyqoJWwh82H/w0UJlrQT9+/EiDEqi42wXDlGHzu2 z8y479pa1Q+hze5NNUvbImUX24bgnoRberDzpfgsYesrKP8MZgiFGw15IqGTN2bKqVrU APOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Md/SEV623PH8+Ut8zUc7zYx2Wn6pyFc2gwGLva1wC8M=; b=lNZA93H8oEIjCJXyasgigxQp6DtIuEHhk6C0Kjx13iQt0CMjk/a2z1x/Eede9P1hoL aVAFOLzeprFHvGDX/nCr5fZd6meGkZ7z1qWDEZ/dAijJuFkq/dmUqJMxpgD7EQqYbdtA ephI/AF3PEkEryl3bBorfzi53sj3uLxS/0kWw7iIBnltIjjtV5ZPzZFPh5yKMPSaE9im h9POpx/bvgvKmPdsX6zbni5GLzNQxo+sKCvlHldcn3dPtsVgAzfGBtp//taJ5K6dLkJM Dq3Fryi7NWLzIHDNeT6idkLQP8Ecbd76vbxWue8Alj3Tw5UeXpZZGYILR0cssJwa7yZk E4fA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b=Kg5eAwKu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bf1si19744345plb.49.2019.05.21.04.43.14; Tue, 21 May 2019 04:43:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@brauner.io header.s=google header.b=Kg5eAwKu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728107AbfEULkw (ORCPT + 99 others); Tue, 21 May 2019 07:40:52 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:33688 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728062AbfEULkt (ORCPT ); Tue, 21 May 2019 07:40:49 -0400 Received: by mail-pl1-f194.google.com with SMTP id g21so263663plq.0 for ; Tue, 21 May 2019 04:40:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brauner.io; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Md/SEV623PH8+Ut8zUc7zYx2Wn6pyFc2gwGLva1wC8M=; b=Kg5eAwKuepKyHoZb0HIBpHsOjkWQvVsLqc8VN/0VGDOq5PzBKsxI6IQr7kDH3bEbgs zZWW728d/N9Bayktnz43WT4cIbAqKqGG4stcXL4mZNI8ES5QddufVPmSAuxeP8h85EI0 P4Jsl7YCYQfB4KNxPtE1r1FJJ943rbcT0S0la0XuVbmH5sbe4qTS2zoyharGmQ1S6cNn XqxluNBYBUVpO8xiVXLWd2HQmKkM+rxu86S2nPC7PchCjCY7Mlz2A9e2+qbpajUYcvB9 aOSugtRc7aWe9b52gavavVVbFhMs5KONMsqn/1YCylCHuC3IrXbWbyqvpjpzp1Aywh8X IFOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Md/SEV623PH8+Ut8zUc7zYx2Wn6pyFc2gwGLva1wC8M=; b=BflWHNncUVoKusvIrP5hAkbJzfnB0nZ7QDXachBzY8eps9mIuIlBc/zQsW/IqV4AVo eXyXVM7EIdrlXb/GoB7YlbPCSsd27uTx+2wkme3VkUP8+i5QEKuvhCclicvMbHOeSdWn h2/gXQbWBzAkGVyLFUAL9pIIEBMosz0QVyKR2MRKnV9jLJGbK/6POCEjyFqaY5zWRwwv YHsOauMr0dW38nTZHPCqatObwyDm7KU1MWHEnX51i2FRs/2zwRbCbvtjkiNxhDrNLTpW zg+3AAWBLPI1hAmsiKHxfTc8FuJTRPg4xgUm5EePWa0vc5DpucLaHUqJmVFGQtFGER8W uIzg== X-Gm-Message-State: APjAAAWx606R5dXa4xmcZ0MRrNGwd7cyUR9rd4nzj1+mfAoIlugy8UzV BPOnCGNettv2PKN3hsJ3qe7j0Q== X-Received: by 2002:a17:902:184:: with SMTP id b4mr55569251plb.2.1558438848409; Tue, 21 May 2019 04:40:48 -0700 (PDT) Received: from localhost.localdomain ([208.54.39.182]) by smtp.gmail.com with ESMTPSA id s9sm34103515pfa.31.2019.05.21.04.40.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 May 2019 04:40:47 -0700 (PDT) From: Christian Brauner To: viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org Cc: jannh@google.com, fweimer@redhat.com, oleg@redhat.com, tglx@linutronix.de, torvalds@linux-foundation.org, arnd@arndb.de, shuah@kernel.org, dhowells@redhat.com, tkjos@android.com, ldv@altlinux.org, miklos@szeredi.hu, linux-alpha@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-ia64@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-xtensa@linux-xtensa.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org, x86@kernel.org, Christian Brauner Subject: [PATCH 2/2] tests: add close_range() tests Date: Tue, 21 May 2019 13:34:48 +0200 Message-Id: <20190521113448.20654-2-christian@brauner.io> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190521113448.20654-1-christian@brauner.io> References: <20190521113448.20654-1-christian@brauner.io> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds basic tests for the new close_range() syscall. - test that no invalid flags can be passed - test that a range of file descriptors is correctly closed - test that a range of file descriptors is correctly closed if there there are already closed file descriptors in the range - test that max_fd is correctly capped to the current fdtable maximum Signed-off-by: Christian Brauner Cc: Arnd Bergmann Cc: Jann Horn Cc: David Howells Cc: Dmitry V. Levin Cc: Oleg Nesterov Cc: Florian Weimer Cc: linux-api@vger.kernel.org --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/core/.gitignore | 1 + tools/testing/selftests/core/Makefile | 6 + .../testing/selftests/core/close_range_test.c | 128 ++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/core/.gitignore create mode 100644 tools/testing/selftests/core/Makefile create mode 100644 tools/testing/selftests/core/close_range_test.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 9781ca79794a..06e57fabbff9 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -4,6 +4,7 @@ TARGETS += bpf TARGETS += breakpoints TARGETS += capabilities TARGETS += cgroup +TARGETS += core TARGETS += cpufreq TARGETS += cpu-hotplug TARGETS += drivers/dma-buf diff --git a/tools/testing/selftests/core/.gitignore b/tools/testing/selftests/core/.gitignore new file mode 100644 index 000000000000..6e6712ce5817 --- /dev/null +++ b/tools/testing/selftests/core/.gitignore @@ -0,0 +1 @@ +close_range_test diff --git a/tools/testing/selftests/core/Makefile b/tools/testing/selftests/core/Makefile new file mode 100644 index 000000000000..de3ae68aa345 --- /dev/null +++ b/tools/testing/selftests/core/Makefile @@ -0,0 +1,6 @@ +CFLAGS += -g -I../../../../usr/include/ -I../../../../include + +TEST_GEN_PROGS := close_range_test + +include ../lib.mk + diff --git a/tools/testing/selftests/core/close_range_test.c b/tools/testing/selftests/core/close_range_test.c new file mode 100644 index 000000000000..ab10cd205ab9 --- /dev/null +++ b/tools/testing/selftests/core/close_range_test.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +static inline int sys_close_range(unsigned int fd, unsigned int max_fd, + unsigned int flags) +{ + return syscall(__NR_close_range, fd, max_fd, flags); +} + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#endif + +int main(int argc, char **argv) +{ + const char *test_name = "close_range"; + int i, ret; + int open_fds[100]; + int fd_max, fd_mid, fd_min; + + ksft_set_plan(7); + + for (i = 0; i < ARRAY_SIZE(open_fds); i++) { + int fd; + + fd = open("/dev/null", O_RDONLY | O_CLOEXEC); + if (fd < 0) { + if (errno == ENOENT) + ksft_exit_skip( + "%s test: skipping test since /dev/null does not exist\n", + test_name); + + ksft_exit_fail_msg( + "%s test: %s - failed to open /dev/null\n", + strerror(errno), test_name); + } + + open_fds[i] = fd; + } + + fd_min = open_fds[0]; + fd_max = open_fds[99]; + + ret = sys_close_range(fd_min, fd_max, 1); + if (!ret) + ksft_exit_fail_msg( + "%s test: managed to pass invalid flag value\n", + test_name); + ksft_test_result_pass("do not allow invalid flag values for close_range()\n"); + + fd_mid = open_fds[50]; + ret = sys_close_range(fd_min, fd_mid, 0); + if (ret < 0) + ksft_exit_fail_msg( + "%s test: Failed to close range of file descriptors from 4 to 50\n", + test_name); + ksft_test_result_pass("close_range() from %d to %d\n", fd_min, fd_mid); + + for (i = 0; i <= 50; i++) { + ret = fcntl(open_fds[i], F_GETFL); + if (ret >= 0) + ksft_exit_fail_msg( + "%s test: Failed to close range of file descriptors from 4 to 50\n", + test_name); + } + ksft_test_result_pass("fcntl() verify closed range from %d to %d\n", fd_min, fd_mid); + + /* create a couple of gaps */ + close(57); + close(78); + close(81); + close(82); + close(84); + close(90); + + fd_mid = open_fds[51]; + /* Choose slightly lower limit and leave some fds for a later test */ + fd_max = open_fds[92]; + ret = sys_close_range(fd_mid, fd_max, 0); + if (ret < 0) + ksft_exit_fail_msg( + "%s test: Failed to close range of file descriptors from 51 to 100\n", + test_name); + ksft_test_result_pass("close_range() from %d to %d\n", fd_mid, fd_max); + + for (i = 51; i <= 92; i++) { + ret = fcntl(open_fds[i], F_GETFL); + if (ret >= 0) + ksft_exit_fail_msg( + "%s test: Failed to close range of file descriptors from 51 to 100\n", + test_name); + } + ksft_test_result_pass("fcntl() verify closed range from %d to %d\n", fd_mid, fd_max); + + fd_mid = open_fds[93]; + fd_max = open_fds[99]; + /* test that the kernel caps and still closes all fds */ + ret = sys_close_range(fd_mid, UINT_MAX, 0); + if (ret < 0) + ksft_exit_fail_msg( + "%s test: Failed to close range of file descriptors from 51 to 100\n", + test_name); + ksft_test_result_pass("close_range() from %d to %d\n", fd_mid, fd_max); + + for (i = 93; i < 100; i++) { + ret = fcntl(open_fds[i], F_GETFL); + if (ret >= 0) + ksft_exit_fail_msg( + "%s test: Failed to close range of file descriptors from 51 to 100\n", + test_name); + } + ksft_test_result_pass("fcntl() verify closed range from %d to %d\n", fd_mid, fd_max); + + return ksft_exit_pass(); +} -- 2.21.0