Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp569587pxb; Wed, 18 Aug 2021 08:49:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsPvrsKII9IJAbyHwnQGGWbg0GCXzXOGFhTqwFcCrshkdTDmrhe4CsPkrU4HEOXwG4e1Vw X-Received: by 2002:a17:906:168d:: with SMTP id s13mr10617251ejd.13.1629301796900; Wed, 18 Aug 2021 08:49:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629301796; cv=none; d=google.com; s=arc-20160816; b=WrHzl7SHYQTRa/ddlKxP1oqwYFtAOEFxbpRsWOrfrq+aUFpnhgQb7yDaM3tpBXXkdz wp7OKaqaBau2hKgXcgfzWsQIWEIV8eubkFKbRYcHZQOuPWdAlsvovIxd1xl/xd6Ksqxa nJoIwJxDqTDXnPu7rNBL/MqD5USNiG5CtxMoe9DiOwgDU7UuRN1nwthU+dhv1Ei3ly9f Fmx29Xsp8U8gxnv24sj8h73AVXlSUPSEm7ESPIhjsuBLxxjwUPEnJz0+x7k2EIcVR3xm 855rLZApsLFst/A91dZL3y8m9xA7mqP2r6DLNnvfOs0elEMOHf34mxxIbaJzZDFfcavA ezQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=QQ1pbYxxSXDHS5PZboZJzEI8JskJZI6zkGDFpVYhurc=; b=rXL/zS97OQWV4ZD1FGjLUK4moUJf1ubiivlyYnsFStcB5tcInz4YsvkPVRdfMTFvkC velBSO1jUKYkul9RfKr8On97AhGQJEBRRQMPC1PByA09lp7sXxtld3uq+6KknqxogxXE ChB0+7LMUJpVjAuHkUiD87o3FzJv+WDUlpJZvU9S/S5Eb2fG1mOwq6K+yHBGaxJGRSLC j5CBLW1FzAM+o5ux9NVX5RqM3S3Xvzfh6TJB5+SurIV0+NpIynchcf7DDOOLgwBSkPmW m/c7RBCu8g7NbINs0yRyOU6HRD7HxHuPwuygDt5I3goh7cPdgOQsX93iNJMQBWpFZCsk PfTw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n26si108399edr.521.2021.08.18.08.49.33; Wed, 18 Aug 2021 08:49:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240072AbhHRPsT (ORCPT + 99 others); Wed, 18 Aug 2021 11:48:19 -0400 Received: from mail-lj1-f172.google.com ([209.85.208.172]:43558 "EHLO mail-lj1-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240003AbhHRPsR (ORCPT ); Wed, 18 Aug 2021 11:48:17 -0400 Received: by mail-lj1-f172.google.com with SMTP id x7so5874447ljn.10; Wed, 18 Aug 2021 08:47:41 -0700 (PDT) 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=QQ1pbYxxSXDHS5PZboZJzEI8JskJZI6zkGDFpVYhurc=; b=JSqHCq8AMLxOfxBHNLPeraQn1EjrXLNsDFYiCYOEC2KgstobLzZb1XeHLDRUc0sQOC gXxY3p72G7E5HmX0pkZYDwzAfyg6YKpP/H49gJxLQupwpCijzEyFfc8+kIcdWosxjr5Z Y6quXaw6wKfUx3PUHlBKmcRulR3DHl8ua9sYFzOryJm6zZsgH19vLTQJ0iX8yqk8GiRy mEuCCYrCaKzHXM7Az1d0b3naan2Zi5lKm38rmi98dVquYK+/DJl2SdmNA7Irbt5kasFf vYBashZzfvV+5sJ0B6voU2kLvLWNWWcRmDfQWEz3nCq8su87XN+yZ5esV1dRIJYqj+iM ie0g== X-Gm-Message-State: AOAM533XiBQZnDwobdCXMuwWOAUh34z9bJ06tiaNtthZ5gK1CUb0RRCq 2d/qqfWOvQzHwjjHAKLGIfWCvrmw6tTBQBPV X-Received: by 2002:a2e:5307:: with SMTP id h7mr8788219ljb.154.1629301660712; Wed, 18 Aug 2021 08:47:40 -0700 (PDT) Received: from localhost.localdomain (broadband-188-32-236-56.ip.moscow.rt.ru. [188.32.236.56]) by smtp.googlemail.com with ESMTPSA id j4sm23808ljh.120.2021.08.18.08.47.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Aug 2021 08:47:40 -0700 (PDT) From: Denis Efremov To: linux-kselftest@vger.kernel.org Cc: Denis Efremov , linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, Jens Axboe , Jiri Kosina , Willy Tarreau Subject: [RFC PATCH 3/5] selftests: floppy: add basic tests for opening an empty device Date: Wed, 18 Aug 2021 18:46:44 +0300 Message-Id: <20210818154646.925351-4-efremov@linux.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210818154646.925351-1-efremov@linux.com> References: <20210818154646.925351-1-efremov@linux.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add basic tests for O_NDELAY and O_ACCMODE open of an empty floppy device. "empty" test works under assumption that there is no disk in "/dev/fd0". It's possible to run it on a real hardware. "run_empty.sh" automates the testing process by creating a minimal initrd image with "init" and "test" binaries and running the kernel in QEMU. The tests cover these regressions: - commit f2791e7eadf4 ("Revert "floppy: refactor open() flags handling"") - commit ff06db1efb2a ("floppy: fix open(O_ACCMODE) for ioctl-only open") Signed-off-by: Denis Efremov --- MAINTAINERS | 1 + tools/testing/selftests/floppy/.gitignore | 5 ++ tools/testing/selftests/floppy/Makefile | 10 ++++ tools/testing/selftests/floppy/config | 1 + tools/testing/selftests/floppy/empty.c | 58 +++++++++++++++++++++ tools/testing/selftests/floppy/init.c | 43 +++++++++++++++ tools/testing/selftests/floppy/lib.sh | 57 ++++++++++++++++++++ tools/testing/selftests/floppy/run_empty.sh | 16 ++++++ 8 files changed, 191 insertions(+) create mode 100644 tools/testing/selftests/floppy/.gitignore create mode 100644 tools/testing/selftests/floppy/Makefile create mode 100644 tools/testing/selftests/floppy/config create mode 100644 tools/testing/selftests/floppy/empty.c create mode 100644 tools/testing/selftests/floppy/init.c create mode 100644 tools/testing/selftests/floppy/lib.sh create mode 100755 tools/testing/selftests/floppy/run_empty.sh diff --git a/MAINTAINERS b/MAINTAINERS index fd25e4ecf0b9..f2a08b793b54 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -7227,6 +7227,7 @@ M: Denis Efremov L: linux-block@vger.kernel.org S: Odd Fixes F: drivers/block/floppy.c +F: tools/testing/selftests/floppy/ FLYSKY FSIA6B RC RECEIVER M: Markus Koch diff --git a/tools/testing/selftests/floppy/.gitignore b/tools/testing/selftests/floppy/.gitignore new file mode 100644 index 000000000000..a9fdf1dbaa97 --- /dev/null +++ b/tools/testing/selftests/floppy/.gitignore @@ -0,0 +1,5 @@ +init +cpio_list +initrd + +empty diff --git a/tools/testing/selftests/floppy/Makefile b/tools/testing/selftests/floppy/Makefile new file mode 100644 index 000000000000..83e18cd63210 --- /dev/null +++ b/tools/testing/selftests/floppy/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +CFLAGS := -static -I../../../../usr/include + +TEST_PROGS := run_empty.sh +TEST_GEN_FILES := init empty +TEST_FILES := lib.sh + +include ../lib.mk + +EXTRA_CLEAN := initrd cpio_list diff --git a/tools/testing/selftests/floppy/config b/tools/testing/selftests/floppy/config new file mode 100644 index 000000000000..d0c2cd8edfb2 --- /dev/null +++ b/tools/testing/selftests/floppy/config @@ -0,0 +1 @@ +CONFIG_BLK_DEV_FD=y diff --git a/tools/testing/selftests/floppy/empty.c b/tools/testing/selftests/floppy/empty.c new file mode 100644 index 000000000000..d7e2e6a74132 --- /dev/null +++ b/tools/testing/selftests/floppy/empty.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include "../kselftest_harness.h" + +FIXTURE(floppy) { + const char *dev; + int fd; +}; + +FIXTURE_VARIANT(floppy) { + int flags; +}; + +/* + * See ff06db1efb2a ("floppy: fix open(O_ACCMODE) for ioctl-only open") + * fdutils use O_ACCMODE for probing and ioctl-only open + */ +FIXTURE_VARIANT_ADD(floppy, ACCMODE) { + .flags = O_ACCMODE, +}; + +FIXTURE_VARIANT_ADD(floppy, NACCMODE) { + .flags = O_ACCMODE|O_NDELAY, +}; + +FIXTURE_VARIANT_ADD(floppy, NRD) { + .flags = O_RDONLY|O_NDELAY, +}; + +FIXTURE_VARIANT_ADD(floppy, NWR) { + .flags = O_WRONLY|O_NDELAY, +}; + +FIXTURE_VARIANT_ADD(floppy, NRDWR) { + .flags = O_RDWR|O_NDELAY, +}; + +FIXTURE_SETUP(floppy) +{ + self->dev = "/dev/fd0"; + if (access(self->dev, F_OK)) + ksft_exit_skip("No floppy device found\n"); +}; + +FIXTURE_TEARDOWN(floppy) +{ + ASSERT_EQ(close(self->fd), 0); +} + +TEST_F(floppy, open) +{ + self->fd = open(self->dev, variant->flags); + ASSERT_GT(self->fd, 0); +} + +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/floppy/init.c b/tools/testing/selftests/floppy/init.c new file mode 100644 index 000000000000..d4df4fcfd21f --- /dev/null +++ b/tools/testing/selftests/floppy/init.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include + +__attribute__((noreturn)) static void poweroff(void) +{ + fflush(stdout); + fflush(stderr); + reboot(RB_POWER_OFF); + sleep(10); + fprintf(stderr, "\nFailed to power off\n"); + exit(1); +} + +static void panic(const char *what) +{ + fprintf(stderr, "\nPANIC %s: %s\n", what, strerror(errno)); + poweroff(); +} + +int main(int argc, char *argv[]) +{ + pid_t pid; + + pid = fork(); + if (pid == -1) + panic("fork"); + else if (pid == 0) { + execl("/test", "test", NULL); + panic("exec"); + } + if (waitpid(pid, NULL, 0) < 0) + panic("waitpid"); + + poweroff(); + return 1; +} diff --git a/tools/testing/selftests/floppy/lib.sh b/tools/testing/selftests/floppy/lib.sh new file mode 100644 index 000000000000..22b711ec32ee --- /dev/null +++ b/tools/testing/selftests/floppy/lib.sh @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: GPL-2.0 + +TOP_DIR="$(realpath "$(dirname "$0")"/../../../../)" + +QEMU=qemu-system-x86_64 +KERNEL="$TOP_DIR"/arch/x86/boot/bzImage +INITRD=initrd + +gen_cpio_list() { + echo "file /init init 755 0 0" > cpio_list + echo "file /test $1 755 0 0" >> cpio_list + echo "dir /dev 755 0 0" >> cpio_list + echo "nod /dev/console 644 0 0 c 5 1" >> cpio_list + echo "nod /dev/fd0 666 0 0 b 2 0" >> cpio_list + echo "dir /mnt 755 0 0" >> cpio_list +} + +gen_initrd() { + "$TOP_DIR"/usr/gen_initramfs.sh -o initrd ./cpio_list +} + +get_selftest_log() { + perl -ne '$begin = $_ =~ /^TAP version/ unless $begin; + if ($begin && !$end) { + $_ =~ s/^\s+|\s+$|\[.+//g; + print($_ . "\n") if $_; + } + $end = $_ =~ /^# Totals:/ unless $end;' +} + +run_qemu() { + $QEMU -enable-kvm -nodefaults -nographic \ + -kernel "$KERNEL" -initrd "$INITRD" \ + -append "console=ttyS0 earlyprintk=serial" \ + -serial stdio -monitor none -no-reboot "$@" +} + +run_qemu_debug() { + run_qemu "$@" +} + +run_qemu_nodebug() { + run_qemu "$@" | get_selftest_log +} + +detect_debug() { + if [ "x$1" = "x" ]; then + run=run_qemu_nodebug + else + run=run_qemu_debug + fi +} + +run_qemu_empty() { + detect_debug "$1" + $run -drive index=0,if=floppy +} diff --git a/tools/testing/selftests/floppy/run_empty.sh b/tools/testing/selftests/floppy/run_empty.sh new file mode 100755 index 000000000000..f018107dc6a5 --- /dev/null +++ b/tools/testing/selftests/floppy/run_empty.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +set -e + +source "$(dirname $0)"/lib.sh + +while getopts d flag; do + case "${flag}" in + d) debug=1;; + esac +done + +gen_cpio_list empty +gen_initrd empty +run_qemu_empty $debug -- 2.31.1