From patchwork Wed Jul 17 07:59:02 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: 1961463 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=brNif2pq; 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 4WP7d16ShNz1ySl for ; Wed, 17 Jul 2024 17:59:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 742A93839170 for ; Wed, 17 Jul 2024 07:59:31 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x1134.google.com (mail-yw1-x1134.google.com [IPv6:2607:f8b0:4864:20::1134]) by sourceware.org (Postfix) with ESMTPS id 8FE1B385F025 for ; Wed, 17 Jul 2024 07:59:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8FE1B385F025 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 8FE1B385F025 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721203152; cv=none; b=GNW2ufLkoxwQAwPfjcbZbMix1wnV0vEZejTVMqjpvs3HCmpP5CsHzZ1Q936qgN9H5XJQgqCQ8e9Q7Agc9KTUjTGdD6GKSyFmqwviQhC2+StfuJOm/Es6H6sobGEDvP+fQwNJGIKYZiG2Ry06uedPD84tFHjzvcq3ZpoNLpNgCM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721203152; c=relaxed/simple; bh=FGsZyrC++GHHxKHnA/EZcn4P7e8859PQJyzH+zw55tw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LxAf/WJnsQvjVJH5OWLbgDZR6vu3yuU6yVG/EjPRg3bJTYzJgEalaI1095S/drMaDAFoYTEZYZ2RE+p02hibnMyDCRT5hZVJunO8jv+uWGZjBxt/Dq9mn9nKh5SHjVTvjpdjY1Pbq3X9B3xmUmQMLyVrMo6lj9zmHP8VPCIVHDM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-65f880c56b1so39879177b3.3 for ; Wed, 17 Jul 2024 00:59:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721203150; x=1721807950; 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=myFexGVoy2AKAL4T5oYuZanqraOKmWr1sfloBVgJ7Z0=; b=brNif2pqOBYMV9hhgGgvqXSLf2hzRo/LSmYCuNvAiBWaolU2Pfum38ciobqyWtTcOz 6hGGRZZ7pDRt+TZltHp12k0JT1YIsSkrJ7FIIMAD6/pHzErP9ZClVUUj0drn8NYJfiqp PWYdX1GpuW4Jhft5+zY+eoqwcN+Urn1HhJ+TKpBuWxTIDeAEzC+UVu6nASgJiYwch7b3 aNbpO4u19EKTxzihRmgirwIBbjhBb6UJSyXLkKcNS30vF+4H1NFh9UMMGPk4SRrV55pA vx4cUzsV/EmdaMqrNlmRcm6eCyY8NcPIfoUBWxGPk6gwt1k79bdTD5PEsncFGUQKgTNv h+2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721203150; x=1721807950; 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=myFexGVoy2AKAL4T5oYuZanqraOKmWr1sfloBVgJ7Z0=; b=lzUE7Gmk4Qq5zgHbQKfgQvoigM2gC8/OnAneappq3SZdZHe3D+ISQDIKAgqSwmT+yA n6MhWlb0dJxmixf4UTMGoJQbhIL1Rtu/pogksjyy7EMYXwRfVYxJ/B+XT6eyRHiAlJJZ xFllhkSW+dbwO3EYFkY/wiPHRY0/AV7S6i6IryO7pWaACfTN+B8q7t4CPE/FHYQOVRLw hKmc8iDIYPyMT3jlzZdig0DmBQHCdx1kEesF9Zgke86gEMc2U5i8T2Khm0XSd/88lolC MkFPIh9px9GHQHHz44Rli3tK/Fn973vHdd6veVR8A8rWRJDcjBabC5rq2aovENSfdhno igfQ== X-Gm-Message-State: AOJu0YzpHTuVgrDB8m02WWmcjWC75XfhbBF+SQFPUvJOndfPrmilh8iJ XkQAxngqCwtxU0otzp2P8uQ8AsEraBcdx0/gGFvs9nTmuNQtTac+p8OjjMVsk0mGqg== X-Google-Smtp-Source: AGHT+IGDSABR9mPKhjbY8vol9LyNmcLLdko/nSYcLFVbKRuS6JSEA1yyM99mhEEvPSSfb4HpdC6KMw== X-Received: by 2002:a0d:da07:0:b0:64b:630f:9f85 with SMTP id 00721157ae682-664fe45113fmr10046547b3.12.1721203149787; Wed, 17 Jul 2024 00:59:09 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.30.168]) by smtp.gmail.com with ESMTPSA id 00721157ae682-65fc4451eebsm14508657b3.117.2024.07.17.00.59.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jul 2024 00:59:09 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 159DCC01AA for ; Wed, 17 Jul 2024 00:59:05 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH v3 1/3] linux: Update the mremap C implementation [BZ #31968] Date: Wed, 17 Jul 2024 00:59:02 -0700 Message-ID: <20240717075904.2365688-2-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240717075904.2365688-1-hjl.tools@gmail.com> References: <20240717075904.2365688-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3019.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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 Update the mremap C implementation to support the optional argument for MREMAP_DONTUNMAP added in Linux 5.7 since it may not always be correct to implement a variadic function as a non-variadic function on all Linux targets. Return MAP_FAILED and set errno to EINVAL for unknown flag bits. This fixes BZ #31968. Note: A test must be added when a new flag bit is introduced. Signed-off-by: H.J. Lu Reviewed-by: Adhemerval Zanella --- sysdeps/unix/sysv/linux/mremap.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sysdeps/unix/sysv/linux/mremap.c b/sysdeps/unix/sysv/linux/mremap.c index 4f770799c4..1ada5c1f40 100644 --- a/sysdeps/unix/sysv/linux/mremap.c +++ b/sysdeps/unix/sysv/linux/mremap.c @@ -20,6 +20,12 @@ #include #include #include +#include + +#define MREMAP_KNOWN_BITS \ + (MREMAP_MAYMOVE \ + | MREMAP_FIXED \ + | MREMAP_DONTUNMAP) void * __mremap (void *addr, size_t old_len, size_t new_len, int flags, ...) @@ -27,7 +33,13 @@ __mremap (void *addr, size_t old_len, size_t new_len, int flags, ...) va_list va; void *new_addr = NULL; - if (flags & MREMAP_FIXED) + if (flags & ~(MREMAP_KNOWN_BITS)) + { + __set_errno (EINVAL); + return MAP_FAILED; + } + + if (flags & (MREMAP_FIXED | MREMAP_DONTUNMAP)) { va_start (va, flags); new_addr = va_arg (va, void *); From patchwork Wed Jul 17 07:59:03 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: 1961464 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=PogGBQg0; 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 4WP7d54Tjqz1ySl for ; Wed, 17 Jul 2024 17:59:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D3D663858D39 for ; Wed, 17 Jul 2024 07:59:35 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by sourceware.org (Postfix) with ESMTPS id 5BC8B3858D39 for ; Wed, 17 Jul 2024 07:59:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5BC8B3858D39 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 5BC8B3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721203152; cv=none; b=tDjDXJe028xTgbDeyiZ0AqMQUIHInj71AYCgI0ORIp/VM+Nf4IJwOL8YELpYAHwVMFSSy1sg1Ca1Y8nQjrXIx/b70jwTbctsnYw1qfDTUtCnTqtg0kGaBAMODXLSpR2Wuft6rov8unlfxis3+pvaXKk4Ty/MBMQuN5qvPVVxa5A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721203152; c=relaxed/simple; bh=089WUDnNwGUENRAwEwtYwh7cyUzzIuQlOMEiZT2Ir1I=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fjgIKcfZ+u7Eii6AZWsY6KFshdPN02BPX5TQkF5JY+Tx5YbWlylMFOl7bRXt3lm1TqNBpc963yffz7xvujAk+p6U2dVlbAI3fkjIu1A7InPJKewZC4eVtF7aDMB0t6cm8wcP9fe8Sgtq18YW8CzAcgHtYeF4qUeXdlDUygr5LOk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb33.google.com with SMTP id 3f1490d57ef6-e03db345b0cso6024577276.1 for ; Wed, 17 Jul 2024 00:59:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721203149; x=1721807949; 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=MYSTc1vRVmD9HF5t/D7rT07o8wpHENQRFWQ1vT5Le3I=; b=PogGBQg0jkZQn03+tigU8UjIaUmkc7Be1BedEGPCkVai1F3nJJ6kUCaXwZR5wHsl46 L78OmSSGIPijKSdrQ0RJlNHmxzMy0Hu6YS/QwzlbsTFkeiKDrgjIGe9XFAP3Pbwx6WY+ PEjQhSTbIGKH/jikFE/3KXmFygZYdXLb/T7tDKLFGzmhfhFxcZ1jDdD5cvSyr93govwI dVI+eP21oo2GPRFzdyIKWyIvmP5SYYIsztc4cCkAMGcjQ84yWK3UYG9nTDHwB4gQR4H/ J6DGhg1+uU0iAldzDlWEAqdTCJMzqpSU2oRXAfYE467ic4cTrSZYXZ+db0USHKfk7CVy AUJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721203149; x=1721807949; 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=MYSTc1vRVmD9HF5t/D7rT07o8wpHENQRFWQ1vT5Le3I=; b=AlM4EBiGCRHHU/z9sSq73xffD64uB4s0JQXh/QMVME7weXVsWhpBA6/TTk3l6zsJcV VTmxPJIranvRhbytun3Nx+DcJzTpX9IauyjCMutWUJgKHNlCO3OJ0WLaP6QJIGHgYsKZ nyWsrCGc2UDZR50VYRVvkY9lAC2Glh2ghb9Vrca22DzT4FdE0d4QXj9v44468ytxgcGO 6KViIj8LkgFJTJlIcuOeSlhsoU4FU8Sta0zSwNa33TxcPVj5Y6AL64CfmIJRVH+L58FA WdDnZRUCRc4j0SuO3XU4s+FSY1pKJqircKxuwlc04ydYtLW4NMO95YobBwzZDmL/xiGv jncg== X-Gm-Message-State: AOJu0YzBG/DIWJeE7JN085woXymJssZuunE3XSJZO4stL8ejGax52Q+m 6NU4GU4NBz8sjtQvTXi6dUdxmjXSZU2TiNCsbq0icYRVeCqO4MLFKRO2ig7ufKsZTA== X-Google-Smtp-Source: AGHT+IFaYrpX6YSH6abS7hY1Y7WOot+f/z9LCVXTBld3/uWhLyvdGXGlpe2BQc7AxO2Ds+6j8uZ1Hg== X-Received: by 2002:a05:6902:2002:b0:e03:a4ba:856f with SMTP id 3f1490d57ef6-e05ed79d4eemr1115232276.53.1721203149337; Wed, 17 Jul 2024 00:59:09 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.30.168]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e05a471faf5sm1578269276.55.2024.07.17.00.59.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jul 2024 00:59:09 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 201B6C02F4 for ; Wed, 17 Jul 2024 00:59:05 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH v3 2/3] mremap: Update manual entry Date: Wed, 17 Jul 2024 00:59:03 -0700 Message-ID: <20240717075904.2365688-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240717075904.2365688-1-hjl.tools@gmail.com> References: <20240717075904.2365688-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3019.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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 Update mremap manual entry: 1. Change mremap to variadic. 2. Document MREMAP_FIXED and MREMAP_DONTUNMAP. Signed-off-by: H.J. Lu Reviewed-by: Adhemerval Zanella --- manual/llio.texi | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/manual/llio.texi b/manual/llio.texi index 6f0a48609b..c6f65089ee 100644 --- a/manual/llio.texi +++ b/manual/llio.texi @@ -1892,7 +1892,7 @@ There is no existing mapping in at least part of the given region. @end deftypefun -@deftypefun {void *} mremap (void *@var{address}, size_t @var{length}, size_t @var{new_length}, int @var{flag}) +@deftypefun {void *} mremap (void *@var{address}, size_t @var{length}, size_t @var{new_length}, int @var{flag}, ... /* void *@var{new_address} */) @standards{GNU, sys/mman.h} @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} @@ -1901,12 +1901,39 @@ area. @var{address} and @var{length} must cover a region entirely mapped in the same @code{mmap} statement. A new mapping with the same characteristics will be returned with the length @var{new_length}. -One option is possible, @code{MREMAP_MAYMOVE}. If it is given in -@var{flags}, the system may remove the existing mapping and create a new -one of the desired length in another location. +Possible flags are -The address of the resulting mapping is returned, or @math{-1}. Possible -error codes include: +@table @code + +@item MREMAP_MAYMOVE +If it is given in @var{flags}, the system may remove the existing mapping +and create a new one of the desired length in another location. + +@item MREMAP_FIXED +If it is given in @var{flags}, @code{mremap} accepts a fifth argument, +@code{void *new_address}, which specifies a page-aligned address to +which the mapping must be moved. Any previous mapping at the address +range specified by @var{new_address} and @var{new_size} is unmapped. + +@code{MREMAP_FIXED} must be used together with @code{MREMAP_MAYMOVE}. + +@item MREMAP_DONTUNMAP +If it is given in @var{flags}, @code{mremap} accepts a fifth argument, +@code{void *new_address}, which specifies a page-aligned address. Any +previous mapping at the address range specified by @var{new_address} and +@var{new_size} is unmapped. If @var{new_address} is @code{NULL}, the +kernel chooses the page-aligned address at which to create the mapping. +Otherwise, the kernel takes it as a hint about where to place the mapping. +The mapping at the address range specified by @var{old_address} and +@var{old_size} isn't unmapped. + +@code{MREMAP_DONTUNMAP} must be used together with @code{MREMAP_MAYMOVE}. +@var{old_size} must be the same as @var{new_size}. + +@end table + +The address of the resulting mapping is returned, or @code{MAP_FAILED}. +Possible error codes include: @table @code @@ -1915,7 +1942,7 @@ There is no existing mapping in at least part of the original region, or the region covers two or more distinct mappings. @item EINVAL -The address given is misaligned or inappropriate. +Any arguments are inappropriate, including unknown @var{flags} values. @item EAGAIN The region has pages locked, and if extended it would exceed the From patchwork Wed Jul 17 07:59:04 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: 1961465 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=PqIPKqob; 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 4WP7f25L3Wz1xqc for ; Wed, 17 Jul 2024 18:00:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0A139385DDE8 for ; Wed, 17 Jul 2024 08:00:25 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x1134.google.com (mail-yw1-x1134.google.com [IPv6:2607:f8b0:4864:20::1134]) by sourceware.org (Postfix) with ESMTPS id 12FCB3861000 for ; Wed, 17 Jul 2024 07:59:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 12FCB3861000 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 12FCB3861000 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721203155; cv=none; b=UvdPgDK4W/Ckvm9RuDdFM3Z39CgJ+2bjrkJTbmzhJ7HcACeLPDrJO92lXYUw+6tXQt9mw6Zk/ENR1cQdX6t+UUeV90srdB3a226Q1FiqINeZiPdX4ooDu/HDZfSwNlbPFJmYLrwEsefihSslqQxEmOJ4yhUi7q3OdCDjBS1VRGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721203155; c=relaxed/simple; bh=ZTnjx/dq/wb+0f1G3F97JisWoMPqLhvkZVXT3aKdy1Q=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QlRbzf5t/XzdySCN6b/QjhQn1rgnTisy5yD4tA4ciUZNb1UgkKPD28agW4h9rRBaIeVy/i/+uRv2/wdTWnx3vxVbNwvehoBHqS5eEo/TFHFLgy0oR8nQhwQxpMIWdlNXEk8EhJ5vQ1mg2K/Cjrzb7usCpM40XZUC9KVv4GP9cB8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-65f771485f2so39116637b3.0 for ; Wed, 17 Jul 2024 00:59:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721203150; x=1721807950; 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=E4lE6YPiFsuWnZPTX+n/UxR2QhjzBcJuKRgmUSjcoSs=; b=PqIPKqobnZfjBkSSxNCbj6i9wZkKi2x6EvlAtl4ogBQwoXEzo2ell9tNvQNnHVH4Tb iF6q/YABA5GtnbGCNK3wYiwCIJAvzyJQHn5TwTmzTF3rtP72AgTt7g/gARr77E9mtuQN fsOgzC6XjG1A/RI5CfZkODoyGNx/ghCdharVbQTdCGlgOSnUgQWiiQIy0UEJOy69DWyB 4wKb+Ipc3776sPOqcMEfnXzt7l86Os17kg7IMgM9pon/oO86gkakCn0jyDuFcveBja6F GkBrwLnCKHipnDSM6Gtc2DH+ytRMgtRyysxReYkA3xH7s3dVZTnMEErZ08A0eo1Jig05 CISA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721203150; x=1721807950; 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=E4lE6YPiFsuWnZPTX+n/UxR2QhjzBcJuKRgmUSjcoSs=; b=uNDFxaV8fUwiLEKIldBNRVw9h2v4aoEDji0Uv9rL6Yc53bZawwjD1hh0EiayixZJ5Y gkZtQqXTzk8rTz64HySDgIbQUBg6lrmCvaQk/+9mVZm1B84uFBVVprQQZQsNfSG2u4YU Paj1rbMGjUqZtyxSTDsGeCiImqHTnGFTPO1uX4a0oWN/7uniVn/sch6EXtyCrxNsqHDU 2AN1sDQS6C2kcAiz79XWGieUEU5RZX4GrdzDnBsimA8JfTf8Zk9HXkY4dfs0gmq/T2aS fhIRBPMO/EeqDMgBy5gNXU5jRo7YhWIwT+KS7bkobiI+/OiPt1ooDWUVaY+7URtlyBG5 iC0w== X-Gm-Message-State: AOJu0YyB6I/Qt8TcakYpEkxvzgKN7OSwTlgNAve8K/qmuxr5l0dxxrqY aXWtwWzAm6TyaTU5Ss/RuQ11NeZKDbf4GGjbez4e4x8XOmUiXUeU//z7c9+6zxVrRw== X-Google-Smtp-Source: AGHT+IHphnhk0SCBmvWxQKz4h5GsfsxZHklsfztsf5g8/vfiaCOvweV6QPvwVaOzgzVQlMWsQGukiw== X-Received: by 2002:a81:6e8b:0:b0:64b:4a9f:540d with SMTP id 00721157ae682-664ffcc457emr11426537b3.31.1721203150030; Wed, 17 Jul 2024 00:59:10 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.30.168]) by smtp.gmail.com with ESMTPSA id 00721157ae682-65fc445204csm14552417b3.127.2024.07.17.00.59.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jul 2024 00:59:09 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 2AB45C044B for ; Wed, 17 Jul 2024 00:59:05 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH v3 3/3] Add mremap tests Date: Wed, 17 Jul 2024 00:59:04 -0700 Message-ID: <20240717075904.2365688-4-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240717075904.2365688-1-hjl.tools@gmail.com> References: <20240717075904.2365688-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3019.7 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_BARRACUDACENTRAL, 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 and MREMAP_FIXED. On Linux, also test MREMAP_DONTUNMAP. Since MREMAP_FIXED was added to Linux kernel 2.3.31 and MREMAP_DONTUNMAP was added to Linux kernel 5.7, mark test failure if MREMAP_FIXED and MREMAP_DONTUNMAP tests fail on supported Linux kernel. Signed-off-by: H.J. Lu --- misc/Makefile | 2 + misc/tst-mremap1.c | 46 ++++++++++++++ misc/tst-mremap2.c | 54 ++++++++++++++++ sysdeps/generic/mremap-failure.h | 26 ++++++++ sysdeps/unix/sysv/linux/Makefile | 9 +++ .../unix/sysv/linux/linux-kernel-version.c | 62 ++++++++++++++++++ sysdeps/unix/sysv/linux/linux-support.h | 38 +++++++++++ sysdeps/unix/sysv/linux/mremap-failure.h | 47 ++++++++++++++ sysdeps/unix/sysv/linux/tst-linux-mremap1.c | 63 +++++++++++++++++++ 9 files changed, 347 insertions(+) create mode 100644 misc/tst-mremap1.c create mode 100644 misc/tst-mremap2.c create mode 100644 sysdeps/generic/mremap-failure.h create mode 100644 sysdeps/unix/sysv/linux/linux-kernel-version.c create mode 100644 sysdeps/unix/sysv/linux/linux-support.h create mode 100644 sysdeps/unix/sysv/linux/mremap-failure.h create mode 100644 sysdeps/unix/sysv/linux/tst-linux-mremap1.c diff --git a/misc/Makefile b/misc/Makefile index 5d17c562fe..7b7f8351bf 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -257,6 +257,8 @@ tests := \ tst-mntent-blank-passno \ tst-mntent-escape \ tst-mntent2 \ + tst-mremap1 \ + tst-mremap2 \ tst-preadvwritev \ tst-preadvwritev2 \ tst-preadvwritev64 \ diff --git a/misc/tst-mremap1.c b/misc/tst-mremap1.c new file mode 100644 index 0000000000..0469991a6c --- /dev/null +++ b/misc/tst-mremap1.c @@ -0,0 +1,46 @@ +/* Test mremap with MREMAP_MAYMOVE. + 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 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 0; +} + +#include diff --git a/misc/tst-mremap2.c b/misc/tst-mremap2.c new file mode 100644 index 0000000000..58591d43c3 --- /dev/null +++ b/misc/tst-mremap2.c @@ -0,0 +1,54 @@ +/* Test mremap with MREMAP_FIXED. + 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 int +do_test (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); + if (new_addr == MAP_FAILED) + return mremap_failure_exit (errno, MREMAP_FIXED); + new_addr[0] = 1; + new_addr[new_size - 1] = 2; + xmunmap (new_addr, new_size); + + return 0; +} + +#include diff --git a/sysdeps/generic/mremap-failure.h b/sysdeps/generic/mremap-failure.h new file mode 100644 index 0000000000..139a6b1fa2 --- /dev/null +++ b/sysdeps/generic/mremap-failure.h @@ -0,0 +1,26 @@ +/* mremap failure handling. 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 + . */ + +/* Return exit value on mremap failure with errno ERR when FLAGS is + passed to mremap. */ + +static int +mremap_failure_exit (int err, int flags) +{ + return EXIT_FAILURE; +} diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 097b5a26fc..c464f85bb7 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -206,6 +206,7 @@ tests += \ tst-getauxval \ tst-gettid \ tst-gettid-kill \ + tst-linux-mremap1 \ tst-memfd_create \ tst-misalign-clone \ tst-mlock2 \ @@ -661,3 +662,11 @@ tests-internal += \ tst-rseq-nptl \ # tests-internal endif + +ifeq ($(subdir),support) +linux-support-routines += \ + linux-kernel-version \ +# linux-support-routines +libsupport-routines += $(linux-support-routines) +libsupport-static-only-routines += $(linux-support-routines) +endif diff --git a/sysdeps/unix/sysv/linux/linux-kernel-version.c b/sysdeps/unix/sysv/linux/linux-kernel-version.c new file mode 100644 index 0000000000..2968c528b6 --- /dev/null +++ b/sysdeps/unix/sysv/linux/linux-kernel-version.c @@ -0,0 +1,62 @@ +/* Initialize CPU feature data. AArch64 version. + This file is part of the GNU C Library. + Copyright (C) 2017-2024 Free Software Foundation, Inc. + + 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 + +unsigned int +make_linux_kernel_version (unsigned int version, unsigned int patch, + unsigned int sub) +{ + TEST_VERIFY_EXIT (version <= 0xff); + TEST_VERIFY_EXIT (patch <= 0xff); + TEST_VERIFY_EXIT (sub <= 0xffff); + /* Return an unsigned int with VVPPSSSS. */ + return (version << 24) | (patch << 16) | sub; +} + +unsigned int +get_linux_kernel_version (void) +{ + struct utsname buf; + const char *p = &buf.release[0]; + unsigned int version; + unsigned int patch; + unsigned int sub; + + TEST_VERIFY_EXIT (uname (&buf) == 0); + + /* Get kernel version. */ + for (version = 0; *p >= '0' && *p <= '9'; p++) + version = version * 10 + *p - '0'; + TEST_VERIFY_EXIT (*p == '.'); + p++; + + /* Get kernel patch level. */ + for (patch = 0; *p >= '0' && *p <= '9'; p++) + patch = patch * 10 + *p - '0'; + TEST_VERIFY_EXIT (*p == '.'); + p++; + + /* Get kernel sub level. */ + for (sub = 0; *p >= '0' && *p <= '9'; p++) + sub = sub * 10 + *p - '0'; + + return make_linux_kernel_version (version, patch, sub); +} diff --git a/sysdeps/unix/sysv/linux/linux-support.h b/sysdeps/unix/sysv/linux/linux-support.h new file mode 100644 index 0000000000..a7c03040b1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/linux-support.h @@ -0,0 +1,38 @@ +/* Linux-specific support functions. + 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 + . */ + +#ifndef LINUX_SUPPORT_H +#define LINUX_SUPPORT_H + +#include + +__BEGIN_DECLS + +/* Return an unsigned int from version, patch level and sub level of + Linux kernel. */ +extern unsigned int make_linux_kernel_version (unsigned int __version, + unsigned int __patch, + unsigned int __sub); + +/* Similar to make_linux_kernel_version, but version, patch level and + sub level are retrieved from the uname system call. */ +extern unsigned int get_linux_kernel_version (void); + +__END_DECLS + +#endif /* LINUX_SUPPORT_H */ diff --git a/sysdeps/unix/sysv/linux/mremap-failure.h b/sysdeps/unix/sysv/linux/mremap-failure.h new file mode 100644 index 0000000000..bf6ffb819c --- /dev/null +++ b/sysdeps/unix/sysv/linux/mremap-failure.h @@ -0,0 +1,47 @@ +/* mremap failure handling. 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 +#include + +/* Return exit value on mremap failure with errno ERR when FLAGS is + passed to mremap. */ + +static int +mremap_failure_exit (int err, int flags) +{ + if (err != EINVAL) + return EXIT_FAILURE; + + unsigned int kernel = get_linux_kernel_version (); + TEST_VERIFY_EXIT (kernel != 0); + + /* Since MREMAP_FIXED was added to Linux kernel 2.3.31, return + EXIT_FAILURE if the kernel is 2.3.31 or newer. */ + if (flags == MREMAP_FIXED + && kernel >= make_linux_kernel_version (2, 3, 31)) + return EXIT_FAILURE; + + /* Since MREMAP_DONTUNMAP was added to Linux kernel 5.7, return + EXIT_FAILURE if the kernel is 5.7 or newer. */ + if (flags == MREMAP_DONTUNMAP + && kernel >= make_linux_kernel_version (5, 7, 0)) + return EXIT_FAILURE; + + return EXIT_UNSUPPORTED; +} diff --git a/sysdeps/unix/sysv/linux/tst-linux-mremap1.c b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c new file mode 100644 index 0000000000..599f6a6009 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-linux-mremap1.c @@ -0,0 +1,63 @@ +/* Test mremap with MREMAP_DONTUNMAP. + 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 +#include + +static int +do_test (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); + + /* Add 3 * pagesize. */ + fixed_size += 3 * old_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); + if (new_addr == MAP_FAILED) + return mremap_failure_exit (errno, MREMAP_DONTUNMAP); + 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); + + return 0; +} + +#include