From patchwork Sun Jun 25 23:17:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 1799588 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=TjdNxb09; 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 4Qq6NG66WHz20bH for ; Mon, 26 Jun 2023 09:18:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 877C53857717 for ; Sun, 25 Jun 2023 23:18:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 877C53857717 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1687735099; bh=yzdaXeFvSjrJgCPFh/GrqdtHSmYnDwOtLKYNVwbUp6w=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TjdNxb09qwAdAjc4ruraNhYVzBVLZSpbSHxVnLSBvcjk97xBohyYlwmTGrOD5fBjI EUudY9c9jSiOW4pIsaxGMS38Mqq1rNV3ua3CrBIHoqb/UYz8OV5W9+k+FUY2nqFkjj 0RAjIUPINoV6rehZJuCdk+cjRMHCL2A6jH91G9FM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by sourceware.org (Postfix) with ESMTPS id CBA933858D20 for ; Sun, 25 Jun 2023 23:18:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBA933858D20 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2b699a2fe86so13200111fa.3 for ; Sun, 25 Jun 2023 16:18:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687735081; x=1690327081; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yzdaXeFvSjrJgCPFh/GrqdtHSmYnDwOtLKYNVwbUp6w=; b=EErNkk23RZthgNTrT+i6BBXpBKmoNge5SsCYgwIcZeKgqUNm3huBRNmf2nz+Y3gJys mJ2zNPNAFukHKnSSieLOtgtJY/DDeNaXEx6slVo644l/QH6T9//DrtOYZPaayXUmyq7K u1VeZj5qJQkI0v3aqC8AuMjEqcIp62FPmDWK+qtUSIhN+seuClLED5Jmcu9VcGglu/VP D4JTkpk6w/53x0V4gIWBQjDuhRi3xCTqjeJ0W8AvuxxVnsRPg6F9MOLAGm8cIuZNYMsc xyRxuCEUwKhCwGRmh+1h0YAHljZjWScdNPIldsNK4ERLjajnrNdE4U4zNZ88wlXaCiaV BYdg== X-Gm-Message-State: AC+VfDyMnlQ9ssx6r7K3mX5y3zXmMyH5x/aWUxWRZKIP94LAuQMUAugG jqY1RHMgMVggPkjEPNXnF5R4zUtfpI8= X-Google-Smtp-Source: ACHHUZ7EBrNdTpLot4o3j4PeNwOKIgC9fDHhus0jerlYwItDGkArX55IVpnvMiNUUkjYV88DvWfhyQ== X-Received: by 2002:a19:6755:0:b0:4f8:58f4:b96e with SMTP id e21-20020a196755000000b004f858f4b96emr16291229lfj.37.1687735080881; Sun, 25 Jun 2023 16:18:00 -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.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 16:18:00 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Subject: [PATCH 1/5] htl: Let Mach place thread stacks Date: Mon, 26 Jun 2023 02:17:47 +0300 Message-ID: <20230625231751.404120-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.41.0 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" Instead of trying to allocate a thread stack at a specific address, looping over the address space, just set the ANYWHERE flag in vm_allocate (). The previous behavior: - defeats ASLR (for Mach versions that support ASLR), - is particularly slow if the lower 4 GB of the address space are mapped inaccessible, as we're planning to do on 64-bit Hurd, - is just silly. Signed-off-by: Sergey Bugaev --- sysdeps/mach/htl/pt-stack-alloc.c | 35 ++++++------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/sysdeps/mach/htl/pt-stack-alloc.c b/sysdeps/mach/htl/pt-stack-alloc.c index 429ac2d9..97e6b445 100644 --- a/sysdeps/mach/htl/pt-stack-alloc.c +++ b/sysdeps/mach/htl/pt-stack-alloc.c @@ -19,14 +19,9 @@ #include #include -#include #include -/* The next address to use for stack allocation. */ -static vm_address_t next_stack_base = VM_MIN_ADDRESS; - - /* Allocate a new stack of size STACKSIZE. If successful, store the address of the newly allocated stack in *STACKADDR and return 0. Otherwise return an error code (EINVAL for an invalid stack size, @@ -35,30 +30,12 @@ static vm_address_t next_stack_base = VM_MIN_ADDRESS; int __pthread_stack_alloc (void **stackaddr, size_t stacksize) { - vm_offset_t base; - int i = 0; - -get_stack: - i++; - for (base = next_stack_base; - base < VM_MAX_ADDRESS - && __vm_allocate (__mach_task_self (), &base, - stacksize, FALSE) != KERN_SUCCESS; base += stacksize) - ; - - if (base >= VM_MAX_ADDRESS) - { - if (i == 1) - { - next_stack_base = VM_MIN_ADDRESS; - goto get_stack; - } - else - return EAGAIN; - } + error_t err; - next_stack_base = base + stacksize; + err = __vm_allocate (__mach_task_self (), (vm_offset_t *) stackaddr, + stacksize, TRUE); - (*stackaddr) = (void *) base; - return 0; + if (err == KERN_NO_SPACE) + err = EAGAIN; + return err; }