From nobody Sun Sep 17 10:37:24 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RpPWT0tY2z4tTsw; Sun, 17 Sep 2023 10:37:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RpPWS75fnz4W2p; Sun, 17 Sep 2023 10:37:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694947045; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=QcqmqT9krmAWYfP8Ta+QrD2nk9nB3FrAoi1N7KUluis=; b=BuqRiub8gbjI5BB0Y2vlcFkUetbcSL8+HZ3JY6HcwNIDoi+usntp4CQwqoP2mtBES+pBKr 6BROHDLDgEWPqOD0LYyRBEc0NG98fO/Bg3syDEwG1elWI1PW6Jg1TTUX1jsTYY/nu9Vr0+ VW4JbHXtojw3zd34D2Jry+oxrl0Pk0VdszaPIW4Rtr7oPX90zoO8KVPDDbRv2HfsOqOTpB HqXrm75N1zqaOuoV/a/iBU2vPdr8QONYJKNYkmVn3sB2MU5jUU0rpd0woAXAT5miaxLdnt R4kEqUAwl7UXWSA27fR7qfPxonc+zYIwHmuKjp4hvSYl8Et+k5anzpQHJlTOJw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694947045; a=rsa-sha256; cv=none; b=b+Bqxi3WMjv45Wrl0ZyS6soYdhhdCqNAavvsC5q1el233dLlWKuhfNhaxsAzBpG6WxQ5vi VS33rjv/MRdzfMpyIXZF5bb7IKlUU76WIfKmX6x6mJ8hkvzxbLz9+ZQQf5a628Aqp2Nyrm uc6kckKsXIwdACKFo3LRmcJKDRXFiwy1Hbi33wJx/ojMAD7Z99SStpQUK0VAiOkLIPJxp6 IYrsz3Gycpj1qhqPALvwNHlJRabGHqv43rT14svWzJUeeag3YoJrrb/VXVRAj4t/iSaMfz LX5evwAmlfNUtCZ6taOg+T1XcZQQHhPB7FKCZk5fYyUst3jPoI3XNZdBZWpDuw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694947045; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=QcqmqT9krmAWYfP8Ta+QrD2nk9nB3FrAoi1N7KUluis=; b=JbhItoV6RIuq4Gm1GOZZRzh1uDGRXi7MTUtWlNnJ5PTEBD5pgmK2BNvQevhXX3tAL6QjRT A2/EkxGfXl4h6ro9T0ua+zWTXWFnjWORe7jPlPenet0YZR7hYLQVUyCgBSFSIBfGYVzTER FYRBEbMbqNoD3ExVOFii3e9cL7QxS67FC2/WWiUfZjNT3xolOmXnnpbXQXFsea+oP/J3sB adihLtej4As5hSsNdd4T8CyhcN/VpraJLNTjnFV5YFsKWlLNisjhneSbHeA34hDbsEzMsl Kt5PPCpOtbuVOva55iRPiTfBojijtZNNI9qrji/E31tjFjtJ9FNdlYclG79utw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4RpPWS67QMzln7; Sun, 17 Sep 2023 10:37:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38HAbOAq023831; Sun, 17 Sep 2023 10:37:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38HAbOcA023828; Sun, 17 Sep 2023 10:37:24 GMT (envelope-from git) Date: Sun, 17 Sep 2023 10:37:24 GMT Message-Id: <202309171037.38HAbOcA023828@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 8f26ed01bd74 - main - mlock tests: Add a regression test List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8f26ed01bd74aab21309ac04ae1d1368a6346c90 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=8f26ed01bd74aab21309ac04ae1d1368a6346c90 commit 8f26ed01bd74aab21309ac04ae1d1368a6346c90 Author: Mark Johnston AuthorDate: 2023-09-15 16:28:42 +0000 Commit: Mark Johnston CommitDate: 2023-09-17 10:27:31 +0000 mlock tests: Add a regression test The test tries to trigger creation of a superpage mapping in an mlock()ed region. MFC after: 2 weeks --- tests/sys/vm/mlock_test.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/tests/sys/vm/mlock_test.c b/tests/sys/vm/mlock_test.c index f0c76fe86b56..8c8ce4a2e8de 100644 --- a/tests/sys/vm/mlock_test.c +++ b/tests/sys/vm/mlock_test.c @@ -28,11 +28,15 @@ #include #include #include +#include #include #include +#include #include +#include #include +#include #include #include @@ -161,12 +165,122 @@ ATF_TC_BODY(mlock__truncate_and_unlock, tc) ATF_REQUIRE(munlock(addr, len) == 0); } +/* + * Exercise a corner case involving an interaction between mlock() and superpage + * creation: a truncation of the object backing a mapping results in the + * truncated region being unmapped by the pmap, but does not affect the logical + * mapping. In particular, the truncated region remains mlock()ed. If the + * mapping is later extended, a page fault in the formerly truncated region can + * result in superpage creation via a call to pmap_enter(psind = 1). + */ +ATF_TC(mlock__superpage_fault); +ATF_TC_HEAD(mlock__superpage_fault, tc) +{ + atf_tc_set_md_var(tc, "require.user", "root"); +} +ATF_TC_BODY(mlock__superpage_fault, tc) +{ + struct rlimit rlim; + void *addr1, *addr2; + size_t len, pagesizes[MAXPAGESIZES]; + int count, error, shmfd; + char vec; + + count = getpagesizes(pagesizes, MAXPAGESIZES); + ATF_REQUIRE_MSG(count >= 1, + "failed to get page sizes: %s", strerror(errno)); + if (count == 1) + atf_tc_skip("system does not support multiple page sizes"); + len = pagesizes[1]; + + error = getrlimit(RLIMIT_MEMLOCK, &rlim); + ATF_REQUIRE_MSG(error == 0, "getrlimit: %s", strerror(errno)); + rlim.rlim_cur += len; + rlim.rlim_max += len; + error = setrlimit(RLIMIT_MEMLOCK, &rlim); + ATF_REQUIRE_MSG(error == 0, "setrlimit: %s", strerror(errno)); + + shmfd = shm_open(SHM_ANON, O_RDWR | O_CREAT, 0600); + ATF_REQUIRE_MSG(shmfd >= 0, "shm_open: %s", strerror(errno)); + error = ftruncate(shmfd, len); + ATF_REQUIRE_MSG(error == 0, "ftruncate: %s", strerror(errno)); + + addr1 = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ALIGNED_SUPER, shmfd, 0); + ATF_REQUIRE_MSG(addr1 != MAP_FAILED, "mmap: %s", strerror(errno)); + ATF_REQUIRE_MSG(((uintptr_t)addr1 & (len - 1)) == 0, + "addr %p is misaligned", addr1); + addr2 = mmap(NULL, len, PROT_READ, + MAP_SHARED | MAP_ALIGNED_SUPER, shmfd, 0); + ATF_REQUIRE_MSG(addr2 != MAP_FAILED, "mmap: %s", strerror(errno)); + ATF_REQUIRE_MSG(((uintptr_t)addr2 & (len - 1)) == 0, + "addr %p is misaligned", addr2); + + memset(addr1, 0x42, len); + error = mincore(addr1, pagesizes[0], &vec); + ATF_REQUIRE_MSG(error == 0, "mincore: %s", strerror(errno)); + if ((vec & MINCORE_SUPER) == 0) + atf_tc_skip("initial superpage promotion failed"); + + error = mlock(addr2, len); + ATF_REQUIRE_MSG(error == 0, "mlock: %s", strerror(errno)); + error = mincore(addr2, pagesizes[0], &vec); + ATF_REQUIRE_MSG(error == 0, "mincore: %s", strerror(errno)); + ATF_REQUIRE((vec & MINCORE_SUPER) != 0); + + /* + * Free a page back to the superpage reservation, demoting both + * mappings. + */ + error = ftruncate(shmfd, len - pagesizes[0]); + ATF_REQUIRE_MSG(error == 0, "ftruncate: %s", strerror(errno)); + + /* + * Extend the mapping back to its original size. + */ + error = ftruncate(shmfd, len); + ATF_REQUIRE_MSG(error == 0, "ftruncate: %s", strerror(errno)); + + /* + * Trigger re-promotion. + */ + error = mincore(addr1, pagesizes[0], &vec); + ATF_REQUIRE_MSG(error == 0, "mincore: %s", strerror(errno)); + ATF_REQUIRE((vec & MINCORE_SUPER) == 0); + memset((char *)addr1 + len - pagesizes[0], 0x43, pagesizes[0]); + error = mincore(addr1, pagesizes[0], &vec); + ATF_REQUIRE_MSG(error == 0, "mincore: %s", strerror(errno)); + ATF_REQUIRE((vec & MINCORE_SUPER) != 0); + + /* + * Trigger a read fault, which should install a superpage mapping + * without promotion. + */ + error = mincore(addr2, pagesizes[0], &vec); + ATF_REQUIRE_MSG(error == 0, "mincore: %s", strerror(errno)); + ATF_REQUIRE((vec & MINCORE_SUPER) == 0); + (void)atomic_load( + (_Atomic int *)(void *)((char *)addr2 + len - pagesizes[0])); + error = mincore(addr2, pagesizes[0], &vec); + ATF_REQUIRE_MSG(error == 0, "mincore: %s", strerror(errno)); + ATF_REQUIRE((vec & MINCORE_SUPER) != 0); + + /* + * Trigger demotion of the wired mapping. + */ + error = munlock(addr2, pagesizes[0]); + ATF_REQUIRE_MSG(error == 0, "munlock: %s", strerror(errno)); + + ATF_REQUIRE(close(shmfd) == 0); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, mlock__copy_on_write_anon); ATF_TP_ADD_TC(tp, mlock__copy_on_write_vnode); ATF_TP_ADD_TC(tp, mlock__truncate_and_resize); ATF_TP_ADD_TC(tp, mlock__truncate_and_unlock); + ATF_TP_ADD_TC(tp, mlock__superpage_fault); return (atf_no_error()); }