Module Name: src Committed By: thorpej Date: Sun Oct 10 17:47:39 UTC 2021
Modified Files: src/distrib/sets/lists/debug: mi src/distrib/sets/lists/tests: mi src/tests/kernel/kqueue: Makefile Added Files: src/tests/kernel/kqueue: t_scan.c Log Message: Add a test case for the race condition in PR kern/50094, modeled after the Go run-time scenario described in the PR. To generate a diff of this commit: cvs rdiff -u -r1.363 -r1.364 src/distrib/sets/lists/debug/mi cvs rdiff -u -r1.1134 -r1.1135 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.5 -r1.6 src/tests/kernel/kqueue/Makefile cvs rdiff -u -r0 -r1.1 src/tests/kernel/kqueue/t_scan.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/debug/mi diff -u src/distrib/sets/lists/debug/mi:1.363 src/distrib/sets/lists/debug/mi:1.364 --- src/distrib/sets/lists/debug/mi:1.363 Sun Oct 10 17:43:15 2021 +++ src/distrib/sets/lists/debug/mi Sun Oct 10 17:47:38 2021 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.363 2021/10/10 17:43:15 thorpej Exp $ +# $NetBSD: mi,v 1.364 2021/10/10 17:47:38 thorpej Exp $ ./etc/mtree/set.debug comp-sys-root ./usr/lib comp-sys-usr compatdir ./usr/lib/i18n/libBIG5_g.a comp-c-debuglib debuglib,compatfile @@ -1765,6 +1765,7 @@ ./usr/libdata/debug/usr/tests/kernel/kqueue/t_proc2.debug tests-kernel-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/kernel/kqueue/t_proc3.debug tests-kernel-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/kernel/kqueue/t_proc4.debug tests-kernel-tests debug,atf,compattestfile +./usr/libdata/debug/usr/tests/kernel/kqueue/t_scan.debug tests-kernel-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/kernel/kqueue/t_sig.debug tests-kernel-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/kernel/kqueue/t_vnode.debug tests-kernel-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/kernel/kqueue/write/t_fifo.debug tests-kernel-tests debug,atf,compattestfile Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.1134 src/distrib/sets/lists/tests/mi:1.1135 --- src/distrib/sets/lists/tests/mi:1.1134 Sun Oct 10 17:43:15 2021 +++ src/distrib/sets/lists/tests/mi Sun Oct 10 17:47:38 2021 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1134 2021/10/10 17:43:15 thorpej Exp $ +# $NetBSD: mi,v 1.1135 2021/10/10 17:47:38 thorpej Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -2185,6 +2185,7 @@ ./usr/tests/kernel/kqueue/t_proc2 tests-kernel-tests compattestfile,atf ./usr/tests/kernel/kqueue/t_proc3 tests-kernel-tests compattestfile,atf ./usr/tests/kernel/kqueue/t_proc4 tests-kernel-tests compattestfile,atf +./usr/tests/kernel/kqueue/t_scan tests-kernel-tests compattestfile,atf ./usr/tests/kernel/kqueue/t_sig tests-kernel-tests compattestfile,atf ./usr/tests/kernel/kqueue/t_vnode tests-kernel-tests compattestfile,atf ./usr/tests/kernel/kqueue/write tests-kernel-tests compattestfile,atf Index: src/tests/kernel/kqueue/Makefile diff -u src/tests/kernel/kqueue/Makefile:1.5 src/tests/kernel/kqueue/Makefile:1.6 --- src/tests/kernel/kqueue/Makefile:1.5 Fri Apr 29 07:12:34 2016 +++ src/tests/kernel/kqueue/Makefile Sun Oct 10 17:47:39 2021 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.5 2016/04/29 07:12:34 ryoon Exp $ +# $NetBSD: Makefile,v 1.6 2021/10/10 17:47:39 thorpej Exp $ WARNS?=6 NOMAN= # defined @@ -14,7 +14,11 @@ TESTS_C= t_ioctl TESTS_C+= t_proc1 TESTS_C+= t_proc2 TESTS_C+= t_proc3 +TESTS_C+= t_proc4 +TESTS_C+= t_scan TESTS_C+= t_sig TESTS_C+= t_vnode +LDADD.t_scan+= -lpthread + .include <bsd.test.mk> Added files: Index: src/tests/kernel/kqueue/t_scan.c diff -u /dev/null src/tests/kernel/kqueue/t_scan.c:1.1 --- /dev/null Sun Oct 10 17:47:39 2021 +++ src/tests/kernel/kqueue/t_scan.c Sun Oct 10 17:47:39 2021 @@ -0,0 +1,117 @@ +/* $NetBSD: t_scan.c,v 1.1 2021/10/10 17:47:39 thorpej Exp $ */ + +/*- + * Copyright (c) 2021 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_scan.c,v 1.1 2021/10/10 17:47:39 thorpej Exp $"); + +#include <sys/event.h> +#include <sys/time.h> +#include <sys/types.h> + +#include <err.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <atf-c.h> + +/* + * Each kevent thread will make this many kevent() calls, and if it + * achieves this mark, we assume the race condition has not occurred + * the delcare the test passes. + */ +#define NKEVENT_CALLS 10000 + +static int kq; + +static void * +kevent_thread(void *arg) +{ + struct timespec ts = { 0, 0 }; + struct kevent event; + int rv, i; + + for (i = 0; i < NKEVENT_CALLS; i++) { + rv = kevent(kq, NULL, 0, &event, 1, &ts); + if (rv < 0) { + i = rv; + break; + } + if (rv != 1) { + break; + } + } + + return (void *)(intptr_t)i; +} + +ATF_TC(scan1); +ATF_TC_HEAD(scan1, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Exercises multi-threaded kevent() calls (PR kern/50094)"); +} + +ATF_TC_BODY(scan1, tc) +{ + pthread_t t1, t2; + void *v1, *v2; + intptr_t r1, r2; + struct kevent event; + int p[2]; + + ATF_REQUIRE(pipe(p) == 0); + ATF_REQUIRE((kq = kqueue()) >= 0); + + EV_SET(&event, p[0], EVFILT_READ, EV_ADD, 0, 0, 0); + ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0); + ATF_REQUIRE(write(p[1], p, 1) == 1); + + ATF_REQUIRE(pthread_create(&t1, NULL, kevent_thread, NULL) == 0); + ATF_REQUIRE(pthread_create(&t2, NULL, kevent_thread, NULL) == 0); + + ATF_REQUIRE(pthread_join(t1, &v1) == 0); + r1 = (intptr_t)v1; + + ATF_REQUIRE(pthread_join(t2, &v2) == 0); + r2 = (intptr_t)v2; + + ATF_REQUIRE(r1 >= 0); + ATF_REQUIRE(r2 >= 0); + + ATF_REQUIRE(r1 == NKEVENT_CALLS); + ATF_REQUIRE(r2 == NKEVENT_CALLS); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, scan1); + + return atf_no_error(); +}