Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests: introduce additional eventfd test coverage

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

Link: https://lkml.kernel.org/r/20240527000200.5615-1-wen.yang@linux.dev
Signed-off-by: Wen Yang <wen.yang@linux.dev>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Andrei Vagin <avagin@google.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Dave Young <dyoung@redhat.com>
Cc: Tim Bird <tim.bird@sony.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Wen Yang and committed by
Andrew Morton
87beb669 51d82165

+131 -5
+131 -5
tools/testing/selftests/filesystems/eventfd/eventfd_test.c
··· 13 13 #include <sys/eventfd.h> 14 14 #include "../../kselftest_harness.h" 15 15 16 + #define EVENTFD_TEST_ITERATIONS 100000UL 17 + 16 18 struct error { 17 19 int code; 18 20 char msg[512]; ··· 42 40 return syscall(__NR_eventfd2, count, flags); 43 41 } 44 42 45 - TEST(eventfd01) 43 + TEST(eventfd_check_flag_rdwr) 46 44 { 47 45 int fd, flags; 48 46 ··· 56 54 close(fd); 57 55 } 58 56 59 - TEST(eventfd02) 57 + TEST(eventfd_check_flag_cloexec) 60 58 { 61 59 int fd, flags; 62 60 ··· 70 68 close(fd); 71 69 } 72 70 73 - TEST(eventfd03) 71 + TEST(eventfd_check_flag_nonblock) 74 72 { 75 73 int fd, flags; 76 74 ··· 85 83 close(fd); 86 84 } 87 85 88 - TEST(eventfd04) 86 + TEST(eventfd_chek_flag_cloexec_and_nonblock) 89 87 { 90 88 int fd, flags; 91 89 ··· 163 161 return 0; 164 162 } 165 163 166 - TEST(eventfd05) 164 + TEST(eventfd_check_flag_semaphore) 167 165 { 168 166 struct error err = {0}; 169 167 int fd, ret; ··· 181 179 ksft_print_msg("eventfd-semaphore check failed, msg: %s\n", 182 180 err.msg); 183 181 EXPECT_EQ(ret, 0); 182 + 183 + close(fd); 184 + } 185 + 186 + /* 187 + * A write(2) fails with the error EINVAL if the size of the supplied buffer 188 + * is less than 8 bytes, or if an attempt is made to write the value 189 + * 0xffffffffffffffff. 190 + */ 191 + TEST(eventfd_check_write) 192 + { 193 + uint64_t value = 1; 194 + ssize_t size; 195 + int fd; 196 + 197 + fd = sys_eventfd2(0, 0); 198 + ASSERT_GE(fd, 0); 199 + 200 + size = write(fd, &value, sizeof(int)); 201 + EXPECT_EQ(size, -1); 202 + EXPECT_EQ(errno, EINVAL); 203 + 204 + size = write(fd, &value, sizeof(value)); 205 + EXPECT_EQ(size, sizeof(value)); 206 + 207 + value = (uint64_t)-1; 208 + size = write(fd, &value, sizeof(value)); 209 + EXPECT_EQ(size, -1); 210 + EXPECT_EQ(errno, EINVAL); 211 + 212 + close(fd); 213 + } 214 + 215 + /* 216 + * A read(2) fails with the error EINVAL if the size of the supplied buffer is 217 + * less than 8 bytes. 218 + */ 219 + TEST(eventfd_check_read) 220 + { 221 + uint64_t value; 222 + ssize_t size; 223 + int fd; 224 + 225 + fd = sys_eventfd2(1, 0); 226 + ASSERT_GE(fd, 0); 227 + 228 + size = read(fd, &value, sizeof(int)); 229 + EXPECT_EQ(size, -1); 230 + EXPECT_EQ(errno, EINVAL); 231 + 232 + size = read(fd, &value, sizeof(value)); 233 + EXPECT_EQ(size, sizeof(value)); 234 + EXPECT_EQ(value, 1); 235 + 236 + close(fd); 237 + } 238 + 239 + 240 + /* 241 + * If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero 242 + * value, then a read(2) returns 8 bytes containing that value, and the 243 + * counter's value is reset to zero. 244 + * If the eventfd counter is zero at the time of the call to read(2), then the 245 + * call fails with the error EAGAIN if the file descriptor has been made nonblocking. 246 + */ 247 + TEST(eventfd_check_read_with_nonsemaphore) 248 + { 249 + uint64_t value; 250 + ssize_t size; 251 + int fd; 252 + int i; 253 + 254 + fd = sys_eventfd2(0, EFD_NONBLOCK); 255 + ASSERT_GE(fd, 0); 256 + 257 + value = 1; 258 + for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) { 259 + size = write(fd, &value, sizeof(value)); 260 + EXPECT_EQ(size, sizeof(value)); 261 + } 262 + 263 + size = read(fd, &value, sizeof(value)); 264 + EXPECT_EQ(size, sizeof(uint64_t)); 265 + EXPECT_EQ(value, EVENTFD_TEST_ITERATIONS); 266 + 267 + size = read(fd, &value, sizeof(value)); 268 + EXPECT_EQ(size, -1); 269 + EXPECT_EQ(errno, EAGAIN); 270 + 271 + close(fd); 272 + } 273 + 274 + /* 275 + * If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value, 276 + * then a read(2) returns 8 bytes containing the value 1, and the counter's 277 + * value is decremented by 1. 278 + * If the eventfd counter is zero at the time of the call to read(2), then the 279 + * call fails with the error EAGAIN if the file descriptor has been made nonblocking. 280 + */ 281 + TEST(eventfd_check_read_with_semaphore) 282 + { 283 + uint64_t value; 284 + ssize_t size; 285 + int fd; 286 + int i; 287 + 288 + fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK); 289 + ASSERT_GE(fd, 0); 290 + 291 + value = 1; 292 + for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) { 293 + size = write(fd, &value, sizeof(value)); 294 + EXPECT_EQ(size, sizeof(value)); 295 + } 296 + 297 + for (i = 0; i < EVENTFD_TEST_ITERATIONS; i++) { 298 + size = read(fd, &value, sizeof(value)); 299 + EXPECT_EQ(size, sizeof(value)); 300 + EXPECT_EQ(value, 1); 301 + } 302 + 303 + size = read(fd, &value, sizeof(value)); 304 + EXPECT_EQ(size, -1); 305 + EXPECT_EQ(errno, EAGAIN); 184 306 185 307 close(fd); 186 308 }