From patchwork Sun Jun 25 23:17:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 1799590 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=dHCheofN; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq6NV6n4pz20b7 for ; Mon, 26 Jun 2023 09:18:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E624E385772A for ; Sun, 25 Jun 2023 23:18:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E624E385772A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687735112; bh=ihugqOu6QpcBxCGXfh6PRaq5n1Q1I0Bl0P/lHPY0K48=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=dHCheofNcnPPfCijnr6yFq9jSQbxlN2XCdNRgeofb/1s34vLylui8/mAKEw95qT5K pZw3esVJGTknA0bWL+X8xgwyRefVc3WBjCP2qD+rdn5SxXKcfkaZ2Pp1fwqMIJcyMJ Bgb+rUpMsA7fPu3pcijlU2wYDIsy7Adph/xnaN+M= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 3833C3858D39 for ; Sun, 25 Jun 2023 23:18:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3833C3858D39 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-4f973035d60so3034921e87.3 for ; Sun, 25 Jun 2023 16:18:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687735084; x=1690327084; 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=ihugqOu6QpcBxCGXfh6PRaq5n1Q1I0Bl0P/lHPY0K48=; b=L8TJVBEK4fx97oIoOJazV0n6uf1W2elkIr8jamIvVQsGtpC8O7YrS5lWa+l+3vnOmY Mvmy57akGmUWOuwpZfb+/ChBLhmyvOxo8P5FOEv4dfWgb7kvtFuh8EYyEwNOJ+uKMahC uvY7JyfUhJL2W8hsDJP7ZvbYDLSNHnKCxhNe5Yqdo/UfxveRjJs/+CLh110lzUNo6Pe7 YKM/v/91hNEyGY007CUF3nmkxh49/7m0VHdY2f4O/Wfas25ygEqRFpKQH9S2bp5B/pNJ wfecPInLfYBcObecclOqZb/kfcHYH+bKsk9m6AAYWhIe5C3+t4xQo7IIBYfn4Qi0bZ7T v7DA== X-Gm-Message-State: AC+VfDwNIAB6Xh5OZhzBE+TF8Kf6/WurIIrn+8OGsHcmUNhXIvzVp5F4 TEjl2uoao2XyloYBHbl8+gRVRNfbAYc= X-Google-Smtp-Source: ACHHUZ5RYI/2rTAIFqg4vPA/vz1XQhecR1s7hNa1IRMgAGt11YzX+HRxqv7SUQX3KBMAa2Cqnc6Wrg== X-Received: by 2002:a05:6512:b94:b0:4f9:72a5:2b76 with SMTP id b20-20020a0565120b9400b004f972a52b76mr4127841lfv.65.1687735084303; Sun, 25 Jun 2023 16:18:04 -0700 (PDT) Received: from localhost.localdomain ([2a02:2168:b344:a600:4435:f106:1598:d2b0]) by smtp.gmail.com with ESMTPSA id eq6-20020a056512488600b004f86d546458sm851547lfb.6.2023.06.25.16.18.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 16:18:03 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 5/5] hurd: Implement MAP_EXCL Date: Mon, 26 Jun 2023 02:17:51 +0300 Message-ID: <20230625231751.404120-5-bugaevc@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230625231751.404120-1-bugaevc@gmail.com> References: <20230625231751.404120-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" MAP_FIXED is defined to silently replace any existing mappings at the address range being mapped over. This, however, is a dangerous, and only rarely desired behavior. Various Unix systems provide replacements or additions to MAP_FIXED: * SerenityOS and Linux provide MAP_FIXED_NOREPLACE. If the address space already contains a mapping in the requested range, Linux returns EEXIST. SerenityOS returns ENOMEM, however that is a bug, as the MAP_FIXED_NOREPLACE implementation is intended to be compatible with Linux. * FreeBSD provides the MAP_EXCL flag that has to be used in combination with MAP_FIXED. It returns EINVAL if the requested range already contains existing mappings. This is directly analogous to the O_EXCL flag in the open () call. * DragonFly BSD, NetBSD, and OpenBSD provide MAP_TRYFIXED, but with different semantics. DragonFly BSD returns ENOMEM if the requested range already contains existing mappings. NetBSD does not return an error, but instead creates the mapping at a different address if the requested range contains mappings. OpenBSD behaves the same, but also notes that this is the default behavior even without MAP_TRYFIXED (which is the case on the Hurd too). Since the Hurd leans closer to the BSD side, add MAP_EXCL as the primary API to request the behavior of not replacing existing mappings. Declare MAP_FIXED_NOREPLACE and MAP_TRYFIXED as aliases of (MAP_FIXED|MAP_EXCL), so any existing software that checks for either of those macros will pick them up automatically. For compatibility with Linux, return EEXIST if a mapping already exists. Finally, a fun bit of horrifying trivia: until very recently, there has been a function named try_mmap_fixed () in the Wine project. On Darwin, it used mach_vm_map () to map the (anonymous) pages without replacing any existing mappings. On Solaris and NetBSD, it instead paused the other threads in the process using vfork (), then used mincore () to probe the pages in the desired address range for being already mapped -- an error return from mincore () indicates that the page is not mapped at all. Finally, if no conflicting mappings were found, still in the vforked child process it performed the mapping with MAP_FIXED, and then returned from the child back into the parent, resuming the other threads. This relied on: * being able to do calls other than execve and _exit from the vforked child, which is undefined behavior according to POSIX; * the parent and the child sharing the address space, and changes made in the child being visible in the parent; * vfork suspending all the threads of the calling process, not just the calling thread. All of this is undefined according to POSIX, but was apparently true on Solaris, which is the system this function was originally implemented for. But on NetBSD, where this shim was later ported to, the last bullet point does not hold: vfork only suspends the calling thread; while the other threads continue to run. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/bits/mman_ext.h | 7 ++++++- sysdeps/mach/hurd/mmap.c | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/sysdeps/mach/hurd/bits/mman_ext.h b/sysdeps/mach/hurd/bits/mman_ext.h index bbb94743..9658cdd6 100644 --- a/sysdeps/mach/hurd/bits/mman_ext.h +++ b/sysdeps/mach/hurd/bits/mman_ext.h @@ -22,5 +22,10 @@ #ifdef __USE_GNU # define SHM_ANON ((const char *) 1) -# define MAP_32BIT 0x1000 + +# define MAP_32BIT 0x1000 /* Map in the lower 2 GB. */ +# define MAP_EXCL 0x4000 /* With MAP_FIXED, don't replace existing mappings. */ + +# define MAP_TRYFIXED (MAP_FIXED | MAP_EXCL) /* BSD name. */ +# define MAP_FIXED_NOREPLACE (MAP_FIXED | MAP_EXCL) /* Linux name. */ #endif /* __USE_GNU */ diff --git a/sysdeps/mach/hurd/mmap.c b/sysdeps/mach/hurd/mmap.c index 33672cf6..20264a77 100644 --- a/sysdeps/mach/hurd/mmap.c +++ b/sysdeps/mach/hurd/mmap.c @@ -46,6 +46,9 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) if ((mapaddr & (__vm_page_size - 1)) || (offset & (__vm_page_size - 1))) return (void *) (long int) __hurd_fail (EINVAL); + if ((flags & MAP_EXCL) && ! (flags & MAP_FIXED)) + return (void *) (long int) __hurd_fail (EINVAL); + vmprot = VM_PROT_NONE; if (prot & PROT_READ) vmprot |= VM_PROT_READ; @@ -156,15 +159,20 @@ __mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset) { if (err == KERN_NO_SPACE) { - /* XXX this is not atomic as it is in unix! */ - /* The region is already allocated; deallocate it first. */ - err = __vm_deallocate (__mach_task_self (), mapaddr, len); - if (! err) - err = __vm_map (__mach_task_self (), - &mapaddr, (vm_size_t) len, mask, - 0, memobj, (vm_offset_t) offset, - copy, vmprot, max_vmprot, - copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE); + if (flags & MAP_EXCL) + err = EEXIST; + else + { + /* The region is already allocated; deallocate it first. */ + /* XXX this is not atomic as it is in unix! */ + err = __vm_deallocate (__mach_task_self (), mapaddr, len); + if (! err) + err = __vm_map (__mach_task_self (), + &mapaddr, (vm_size_t) len, mask, + 0, memobj, (vm_offset_t) offset, + copy, vmprot, max_vmprot, + copy ? VM_INHERIT_COPY : VM_INHERIT_SHARE); + } } } else