From patchwork Fri Jul 12 22:31:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1960039 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=ku2uA/Zt; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WLRHh2Yqfz20Md for ; Sat, 13 Jul 2024 08:35:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 57BC23831246 for ; Fri, 12 Jul 2024 22:35:10 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by sourceware.org (Postfix) with ESMTPS id 14C203858433 for ; Fri, 12 Jul 2024 22:33:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 14C203858433 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 14C203858433 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b30 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720823692; cv=none; b=ExTgt5wDw1MmwEUs0amdWUzJOyoa8SUc6/S2OINpJ79SPHjm2TrSAXbdeCElQVRLTTuhX/OfMi/8dYrYpMbAIi85b0HqAE1MFk/TSCN2GEv0f1553fk21Xp+izNZu3+pw3/iiNPLUH5lQ8/NrqN3iVmtGpkJCVL6GcFCzmj+Ka0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720823692; c=relaxed/simple; bh=a+B+beY2jsv4sEHhzC2ZeXWS0I+vtR0KndbJFxKkD24=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=GMsfCKhZ0S/SMYbGG991TwvmuTdYDnft+5w41R/9P+9BWG7MbbtlMrzzrbYDBU8Uc6z03VXtF34ZfWvuqbjJPgR9dGmxhs5FHN5nMW0DhW5ycIGga3YpPpdjkS9MM2kunGCv/1jH0Hqhm+y5P5y/cvMYzsVFrKrwnTB9kpp8BZ8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb30.google.com with SMTP id 3f1490d57ef6-e035dc23a21so2590383276.0 for ; Fri, 12 Jul 2024 15:33:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720823630; x=1721428430; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=U9l60l5LwnZcPy9JmoGouqwIdd/USxm0xL1No2eFIKA=; b=ku2uA/ZtGHO9zUzRahF5z2mMzTMFDcFGZLtv5gURODSI/tFvzUr/UMbQOWSdWuBYbZ Bqq0aaGOQl0URSx9swk/w1ASa3VJaQdfdGS/iyYrBT4wMBczcKUZkpZyEPfj1DhBcLAt IP/YRzjWMSoL7E9up0U4KVv6mc6KIaekVJ2IezHFz0cEEUt1QK9NA5xpgGjakYo/nS6E BIW3dCMy04vGZkohuOmQMlh4tJxdGKHXuAvZl34k+81IIomvfrlUC9hxPjaZNcsKS4+g zYm1nU7T3DOp7qI2UUMOgsYXo7DQUmSJgbZovB+USGrv965F6gETXNxyL8t9JcHl6ZiA +BXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720823630; x=1721428430; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U9l60l5LwnZcPy9JmoGouqwIdd/USxm0xL1No2eFIKA=; b=jWP2JjGLMudc7gYSelgYZ5Zc/eljXA+q+3NcasqqFviUF5fW4ajQWzoihTCJERIBRE 6gQGLFHDX1z93wuS6B5oZu6JoySo+xIsCMoGQutP4zI5a3GI6Dmubo7/RH3/fUGkMEsd 6SWQFmWOldEtejUIefd/taSR1RdEVbcXhcFcXklq6zuqBtUbKjs+LKx00D/4sU9omBsv T7WoZ0/gz6+SUnFafxaoHiG4LCUqKs3x8Lp7kocRZSLIiYO+qQy5mYQCSoEMjKOVkIlH aMGHkiOCZSk79Q/8EspGBBdyGmUcm8ZKaM9UMvUROvxjtxmLjG4uz42zwI1YiF/dhF0X WTKw== X-Gm-Message-State: AOJu0YyvEDC/9h2KuLTozqydL7GfIfirqbN+h9uBnVbb+R/8KMk9ONhn 50wegCVEDjHqUlZ2cP4rdrHY1uiGW2Luj4tmlp7wXFEsKm/oWNUwK2NCLkAkInjq3w== X-Google-Smtp-Source: AGHT+IGnrIz6w3t0EAT2rXstG+xmieltWwoE72D0KJFIaTF1pqDUSDKElE84m0g4cplyQwmvtIvZeA== X-Received: by 2002:a05:6902:1b0f:b0:e05:9f9c:5516 with SMTP id 3f1490d57ef6-e059f9c575fmr1154684276.19.1720823630237; Fri, 12 Jul 2024 15:33:50 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.160.179]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e041a9a1695sm1481322276.62.2024.07.12.15.33.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jul 2024 15:33:50 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id AE575C03D5 for ; Fri, 12 Jul 2024 15:31:47 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH 3/3] Add mremap tests Date: Fri, 12 Jul 2024 15:31:47 -0700 Message-ID: <20240712223147.1809816-4-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240712223147.1809816-1-hjl.tools@gmail.com> References: <20240712223147.1809816-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3021.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Add tests for MREMAP_MAYMOVE, MREMAP_FIXED and MREMAP_DONTUNMAP. On Linux, enable MREMAP_FIXED test for Linux kernel 2.3.31 and above. Enable MREMAP_DONTUNMAP test for Linux kernel 5.7 and above. Signed-off-by: H.J. Lu --- misc/Makefile | 1 + misc/tst-mremap.c | 73 ++++++++++++++++++++++++++++ sysdeps/generic/tst-mremap.h | 24 +++++++++ sysdeps/unix/sysv/linux/tst-mremap.h | 67 +++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 misc/tst-mremap.c create mode 100644 sysdeps/generic/tst-mremap.h create mode 100644 sysdeps/unix/sysv/linux/tst-mremap.h diff --git a/misc/Makefile b/misc/Makefile index 5d17c562fe..96474a6886 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -257,6 +257,7 @@ tests := \ tst-mntent-blank-passno \ tst-mntent-escape \ tst-mntent2 \ + tst-mremap \ tst-preadvwritev \ tst-preadvwritev2 \ tst-preadvwritev64 \ diff --git a/misc/tst-mremap.c b/misc/tst-mremap.c new file mode 100644 index 0000000000..03e01852cb --- /dev/null +++ b/misc/tst-mremap.c @@ -0,0 +1,73 @@ +/* Basic mremap tests. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include + +static void +test_mremap_fixed (void) +{ + size_t old_size = getpagesize (); + size_t new_size = old_size + old_size; + char *old_addr = xmmap (NULL, old_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + old_addr[0] = 1; + old_addr[old_size - 1] = 2; + + char *fixed_addr = xmmap (NULL, new_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + fixed_addr[0] = 1; + fixed_addr[new_size - 1] = 2; + + /* Test MREMAP_FIXED. */ + char *new_addr = mremap (old_addr, old_size, new_size, + MREMAP_FIXED | MREMAP_MAYMOVE, + fixed_addr); + TEST_VERIFY_EXIT (new_addr == fixed_addr); + new_addr[0] = 1; + new_addr[new_size - 1] = 2; + xmunmap (new_addr, new_size); +} + +#include + +static int +do_test (void) +{ + size_t old_size = getpagesize (); + char *old_addr = xmmap (NULL, old_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + old_addr[0] = 1; + old_addr[old_size - 1] = 2; + + /* Test MREMAP_MAYMOVE. */ + size_t new_size = old_size + old_size; + char *new_addr = mremap (old_addr, old_size, new_size, MREMAP_MAYMOVE); + TEST_VERIFY_EXIT (new_addr != MAP_FAILED); + new_addr[0] = 1; + new_addr[new_size - 1] = 2; + xmunmap (new_addr, new_size); + + return do_additonal_test (); +} + +#include diff --git a/sysdeps/generic/tst-mremap.h b/sysdeps/generic/tst-mremap.h new file mode 100644 index 0000000000..dbc760ca28 --- /dev/null +++ b/sysdeps/generic/tst-mremap.h @@ -0,0 +1,24 @@ +/* Additional mremap check. Generic version. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +static int +do_additonal_test (void) +{ + test_mremap_fixed (); + return 0; +} diff --git a/sysdeps/unix/sysv/linux/tst-mremap.h b/sysdeps/unix/sysv/linux/tst-mremap.h new file mode 100644 index 0000000000..1c1777e23d --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-mremap.h @@ -0,0 +1,67 @@ +/* Additional mremap check. Linux version. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +static void +test_mremap_dontunmap (void) +{ + size_t old_size = getpagesize (); + size_t new_size = old_size; + char *old_addr = xmmap (NULL, old_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + old_addr[0] = 1; + old_addr[old_size - 1] = 2; + + /* Create an available 64-page mmap region. */ + size_t fixed_size = old_size * 64; + char *fixed_addr = xmmap (NULL, fixed_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1); + xmunmap (fixed_addr, fixed_size); + + /* Test MREMAP_DONTUNMAP. It should return FIXED_ADDR created above. */ + char *new_addr = mremap (old_addr, old_size, new_size, + MREMAP_DONTUNMAP | MREMAP_MAYMOVE, + fixed_addr); + TEST_VERIFY_EXIT (fixed_addr == new_addr); + old_addr[0] = 3; + old_addr[old_size - 1] = 4; + new_addr[0] = 1; + new_addr[new_size - 1] = 2; + xmunmap (new_addr, new_size); + xmunmap (old_addr, old_size); +} + +static int +do_additonal_test (void) +{ + unsigned int kernel = get_linux_kernel_version (); + TEST_VERIFY_EXIT (kernel != 0); + + /* MREMAP_FIXED was added to Linux kernel 2.3.31. */ + if (kernel < make_linux_kernel_version (2, 3, 31)) + return EXIT_UNSUPPORTED; + test_mremap_fixed (); + + /* MREMAP_DONTUNMAP was added to Linux kernel 5.7. */ + if (kernel < make_linux_kernel_version (5, 7, 0)) + return EXIT_UNSUPPORTED; + test_mremap_dontunmap (); + + return 0; +}