Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1764967lqb; Sun, 26 May 2024 17:03:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWb/XF9qJoQgnUYBG0V5tgyjppHsK6MPYxqjPmt95qNGdaym6+QOE0rPk64Ozq0eHP8sx3VtKs3vxHAojhlp7tkqiU9zgocBvfp0qgLhA== X-Google-Smtp-Source: AGHT+IE1sw5VbeDCCL/5Ill6HYmaBBPxz6WHCVCYRHB2oMS0SExXp7lTrjjVmkM9AuYDik9+P3RR X-Received: by 2002:a17:907:940b:b0:a62:cf3b:77ad with SMTP id a640c23a62f3a-a62cf3b7972mr390447966b.49.1716768180564; Sun, 26 May 2024 17:03:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716768180; cv=pass; d=google.com; s=arc-20160816; b=Db58lgDcIsq3Q/hRvLzMEdDTJ6Azu4z+XFYpJyBeYyjMmcjnH6bXud0EV6RelUnjyf x0UCzLZdJ9z5y9u4P0xhCdFBvE1enOWi9TankhqkQGF5tBzUNqvTjXDylgUF6JtF5TBM jb2dhljXnCUWvyjeUGXChZwxqqFwPL0jpaLPEiKFlfE1orMoFmTqI6hmEauOcMWJVBx9 0bjshunX7bFygA2ibbUusJXN+YcmjlcCnglrQ5uFxaXYNT5I3RwlGJvuhC3DqmEfS3fA 7SqkZXV5CkA3JRVVtYER2vzcTYW/YF0W4Uo1vaThxYUlmN1gB99+xk2lCIPg5X7EVW/E lwQQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=VjOimlkAb10m4KMSg1DOqlHJhsmFxHSlxPO0CJcAMP4=; fh=OEyhGuBgSGMPUwtZfTk+AH9m8ocg+dek2Y94IwFnW0A=; b=I7gfdD3Z9wqE86qEKAKmr7nuZNh5wVx6lBWCDRPmWGG64UoUY9D+0KZ51cPmsd8uR3 TOHLjW2+hTdpxYzctevQ0L8a3bRtslUuA+oQad45W8Nd0kzxpcOf3vkXuECYQbLvfdG8 Mf0QjC5ws0MHgquITFZv6dM6BWOxIKoQcPsvfOLqx8X+z3pmAPGOqJ3NaGSjvwYD8w0o cJRpadkSpxJKukkYU1AREjg5mkkVhjfExnKhjVqJjBnKz5fydYZRNgd8RzddnMx+nX5Y A/6bTQ6wP1fnV4D+vTWRLrTbuXfy3XTxUBVHsNLTG4K/kWHrFk/9Cp+cS3bJHXIJA4im yIjg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oZFZCESK; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-189904-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189904-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a626cc37698si318931466b.365.2024.05.26.17.03.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 17:03:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189904-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=oZFZCESK; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-189904-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189904-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 23EC01F219D3 for ; Mon, 27 May 2024 00:03:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 971892F44; Mon, 27 May 2024 00:02:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="oZFZCESK" Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDF0C2F24 for ; Mon, 27 May 2024 00:02:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716768172; cv=none; b=TR8sDzqPwEtUrwA39KuBQMaW/I48NLoV0COJWqj4/mOnHEvN7AFfADV1Z8OIH8Joa391fnoVudH3amuG4XrsENAhGY8995O17GyUl4f8elEym0rXyVyCh1iqEMspSD9CgZHf/c91GzU8yHPsdSvMRHaqS/EaFgA5DbNIanV9O8A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716768172; c=relaxed/simple; bh=5Z/IQ1AG0LSghMA4nafETdJADGNE+D470yXbhvbV++8=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=QfuzmxUMyzB7OPK6SkCwtPEolk2dxrvJ9ThpYzdCLcIkQEFWxkZ+k0bSYcqtqutndHGCbmfdZwB+hYVjts0j/XzcQkBEukxzlGp1u/Ru2/a06JoW09bre12xdtYAC5wVBlj+tzK+T7FSzt7SHR+GTe4qt/l7krxIc7WN68tbcLQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=oZFZCESK; arc=none smtp.client-ip=95.215.58.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Envelope-To: skhan@linuxfoundation.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1716768167; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=VjOimlkAb10m4KMSg1DOqlHJhsmFxHSlxPO0CJcAMP4=; b=oZFZCESKyh4srLsDKsx5WPsLeRrP/XrmvJbpNuyLg7+fLFuBjxhdK+JDw0N6j29Dr+ZBCF QGxoL82JDx2HkkQdJifXgoczMxUfnqPhW9YNUFGfkJLNW4w1z4GR4xJ6hPpMeLGOmSUO1l KndM+M6bjwFFkAM0ZuJTKax0jVq5f20= X-Envelope-To: brauner@kernel.org X-Envelope-To: akpm@linux-foundation.org X-Envelope-To: wen.yang@linux.dev X-Envelope-To: shuah@kernel.org X-Envelope-To: avagin@google.com X-Envelope-To: mathieu.desnoyers@efficios.com X-Envelope-To: rostedt@goodmis.org X-Envelope-To: dyoung@redhat.com X-Envelope-To: tim.bird@sony.com X-Envelope-To: linux-kselftest@vger.kernel.org X-Envelope-To: linux-kernel@vger.kernel.org X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Wen Yang To: Shuah Khan , Christian Brauner , Andrew Morton Cc: Wen Yang , SShuah Khan , Andrei Vagin , Mathieu Desnoyers , Steven Rostedt , Dave Young , Tim Bird , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RESEDN PATCH v2] selftests: introduce additional eventfd test coverage Date: Mon, 27 May 2024 08:02:00 +0800 Message-Id: <20240527000200.5615-1-wen.yang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Add several new test cases which assert corner cases on the eventfd mechanism, for example, the supplied buffer is less than 8 bytes, attempting to write a value that is too large, etc. ./eventfd_test # Starting 9 tests from 1 test cases. # RUN global.eventfd_check_flag_rdwr ... # OK global.eventfd_check_flag_rdwr ok 1 global.eventfd_check_flag_rdwr # RUN global.eventfd_check_flag_cloexec ... # OK global.eventfd_check_flag_cloexec ok 2 global.eventfd_check_flag_cloexec # RUN global.eventfd_check_flag_nonblock ... # OK global.eventfd_check_flag_nonblock ok 3 global.eventfd_check_flag_nonblock # RUN global.eventfd_chek_flag_cloexec_and_nonblock ... # OK global.eventfd_chek_flag_cloexec_and_nonblock ok 4 global.eventfd_chek_flag_cloexec_and_nonblock # RUN global.eventfd_check_flag_semaphore ... # OK global.eventfd_check_flag_semaphore ok 5 global.eventfd_check_flag_semaphore # RUN global.eventfd_check_write ... # OK global.eventfd_check_write ok 6 global.eventfd_check_write # RUN global.eventfd_check_read ... # OK global.eventfd_check_read ok 7 global.eventfd_check_read # RUN global.eventfd_check_read_with_nonsemaphore ... # OK global.eventfd_check_read_with_nonsemaphore ok 8 global.eventfd_check_read_with_nonsemaphore # RUN global.eventfd_check_read_with_semaphore ... # OK global.eventfd_check_read_with_semaphore ok 9 global.eventfd_check_read_with_semaphore # PASSED: 9 / 9 tests passed. # Totals: pass:9 fail:0 xfail:0 xpass:0 skip:0 error:0 Signed-off-by: Wen Yang Cc: SShuah Khan Cc: Christian Brauner Cc: Andrei Vagin Cc: Mathieu Desnoyers Cc: Steven Rostedt Cc: Andrew Morton Cc: Dave Young Cc: Tim Bird Cc: linux-kselftest@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- v2: use strings which indicate what is being tested, that are useful to a human .../filesystems/eventfd/eventfd_test.c | 136 +++++++++++++++++- 1 file changed, 131 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/filesystems/eventfd/eventfd_test.c b/tools/testing/selftests/filesystems/eventfd/eventfd_test.c index f142a137526c..85acb4e3ef00 100644 --- a/tools/testing/selftests/filesystems/eventfd/eventfd_test.c +++ b/tools/testing/selftests/filesystems/eventfd/eventfd_test.c @@ -13,6 +13,8 @@ #include #include "../../kselftest_harness.h" +#define EVENTFD_TEST_ITERATIONS 100000UL + struct error { int code; char msg[512]; @@ -40,7 +42,7 @@ static inline int sys_eventfd2(unsigned int count, int flags) return syscall(__NR_eventfd2, count, flags); } -TEST(eventfd01) +TEST(eventfd_check_flag_rdwr) { int fd, flags; @@ -54,7 +56,7 @@ TEST(eventfd01) close(fd); } -TEST(eventfd02) +TEST(eventfd_check_flag_cloexec) { int fd, flags; @@ -68,7 +70,7 @@ TEST(eventfd02) close(fd); } -TEST(eventfd03) +TEST(eventfd_check_flag_nonblock) { int fd, flags; @@ -83,7 +85,7 @@ TEST(eventfd03) close(fd); } -TEST(eventfd04) +TEST(eventfd_chek_flag_cloexec_and_nonblock) { int fd, flags; @@ -161,7 +163,7 @@ static int verify_fdinfo(int fd, struct error *err, const char *prefix, return 0; } -TEST(eventfd05) +TEST(eventfd_check_flag_semaphore) { struct error err = {0}; int fd, ret; @@ -183,4 +185,128 @@ TEST(eventfd05) close(fd); } +/* + * A write(2) fails with the error EINVAL if the size of the supplied buffer + * is less than 8 bytes, or if an attempt is made to write the value + * 0xffffffffffffffff. + */ +TEST(eventfd_check_write) +{ + uint64_t value = 1; + ssize_t size; + int fd; + + fd = sys_eventfd2(0, 0); + ASSERT_GE(fd, 0); + + size = write(fd, &value, sizeof(int)); + EXPECT_EQ(size, -1); + EXPECT_EQ(errno, EINVAL); + + size = write(fd, &value, sizeof(value)); + EXPECT_EQ(size, sizeof(value)); + + value = (uint64_t)-1; + size = write(fd, &value, sizeof(value)); + EXPECT_EQ(size, -1); + EXPECT_EQ(errno, EINVAL); + + close(fd); +} + +/* + * A read(2) fails with the error EINVAL if the size of the supplied buffer is + * less than 8 bytes. + */ +TEST(eventfd_check_read) +{ + uint64_t value; + ssize_t size; + int fd; + + fd = sys_eventfd2(1, 0); + ASSERT_GE(fd, 0); + + size = read(fd, &value, sizeof(int)); + EXPECT_EQ(size, -1); + EXPECT_EQ(errno, EINVAL); + + size = read(fd, &value, sizeof(value)); + EXPECT_EQ(size, sizeof(value)); + EXPECT_EQ(value, 1); + + close(fd); +} + + +/* + * If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero + * value, then a read(2) returns 8 bytes containing that value, and the + * counter's value is reset to zero. + * If the eventfd counter is zero at the time of the call to read(2), then the + * call fails with the error EAGAIN if the file descriptor has been made nonblocking. + */ +TEST(eventfd_check_read_with_nonsemaphore) +{ + uint64_t value; + ssize_t size; + int fd; + int i; + + fd = sys_eventfd2(0, EFD_NONBLOCK); + ASSERT_GE(fd, 0); + + value = 1; + for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) { + size = write(fd, &value, sizeof(value)); + EXPECT_EQ(size, sizeof(value)); + } + + size = read(fd, &value, sizeof(value)); + EXPECT_EQ(size, sizeof(uint64_t)); + EXPECT_EQ(value, EVENTFD_TEST_ITERATIONS); + + size = read(fd, &value, sizeof(value)); + EXPECT_EQ(size, -1); + EXPECT_EQ(errno, EAGAIN); + + close(fd); +} + +/* + * If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value, + * then a read(2) returns 8 bytes containing the value 1, and the counter's + * value is decremented by 1. + * If the eventfd counter is zero at the time of the call to read(2), then the + * call fails with the error EAGAIN if the file descriptor has been made nonblocking. + */ +TEST(eventfd_check_read_with_semaphore) +{ + uint64_t value; + ssize_t size; + int fd; + int i; + + fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK); + ASSERT_GE(fd, 0); + + value = 1; + for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) { + size = write(fd, &value, sizeof(value)); + EXPECT_EQ(size, sizeof(value)); + } + + for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) { + size = read(fd, &value, sizeof(value)); + EXPECT_EQ(size, sizeof(value)); + EXPECT_EQ(value, 1); + } + + size = read(fd, &value, sizeof(value)); + EXPECT_EQ(size, -1); + EXPECT_EQ(errno, EAGAIN); + + close(fd); +} + TEST_HARNESS_MAIN -- 2.25.1