From patchwork Sun Mar 19 15:10:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 66615 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB8783894C22 for ; Sun, 19 Mar 2023 15:18:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB8783894C22 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1679239125; bh=fbhKkyIUvwVhQIgWpcDaBhcFO5Q7BdDsWkFnV/fJdoo=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=FFuo1I9Pg6BaavICIqQNkP8uasoIfVu5L+sFDuFPCqRYt9AwMuYm2fY/c/TXGC4b1 WEa5nUZXHUU8f7wF+yTXAd+pfxihOv0gfuoWOkUv4xTbOFlwfWJODdf9KKoZPPhUVc V8tbDBlMke4pQq9UMpgf6rhXXojI6K05mTuvjYWc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id D5E13385828D for ; Sun, 19 Mar 2023 15:11:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D5E13385828D Received: by mail-lf1-x12f.google.com with SMTP id y15so12011515lfa.7 for ; Sun, 19 Mar 2023 08:11:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679238682; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fbhKkyIUvwVhQIgWpcDaBhcFO5Q7BdDsWkFnV/fJdoo=; b=IsY679g+Rj+goBtlM3jYxcGmjbqYfooZnx5NZELduKXKNbXlUMFnMHd9ce9KzpWK2g wkdM4HmpJ94uUtGnDccq8wduUW3wnqkBzDPdc0k5NBP0xCkX8phvNKVwozmImST+0tSt iwBKeBCBqrel/Dp3hjxtEI8yFLltzMKu17AmY+87o2DrJ8afilr8Qdz4rcdcYI+EcwV4 lRK8n2+MQ3KHrFah9TtmsRBs2f0lsMAjHieztEw8uLXQFkYjzQVrQpnhczPqInG6GCkl fmIgD0iGstrbRPoyS+BoIs7lREL+iYgejKE1kgNYNvjiASl1F74uyhVl8z4BtD8vos/k V+2w== X-Gm-Message-State: AO0yUKUKIxdrzs1VOYZfK/QDkQHUwLk7BRRe3Nd/RKzbL657P4vK+c6V TRXVQbZoHok3RFbYzMD8EC4i0ZOcwKXqEQ== X-Google-Smtp-Source: AK7set9zzl8NVU2HKow4JsDDq6PzJU+EvkfknmcCIkMonH/vBoqB0OS1yITr6j7/Z1Od8rrSAwYnLg== X-Received: by 2002:ac2:4282:0:b0:4a9:39f4:579a with SMTP id m2-20020ac24282000000b004a939f4579amr6134351lfh.66.1679238682218; Sun, 19 Mar 2023 08:11:22 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:577a:76f4:df43:5e66]) by smtp.gmail.com with ESMTPSA id m19-20020ac24253000000b004e90dee5469sm1274089lfl.157.2023.03.19.08.11.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Mar 2023 08:11:21 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Sergey Bugaev Subject: [RFC PATCH glibc 27/34] hurd: Don't leak __hurd_reply_port0 Date: Sun, 19 Mar 2023 18:10:10 +0300 Message-Id: <20230319151017.531737-28-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230319151017.531737-1-bugaevc@gmail.com> References: <20230319151017.531737-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, 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.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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Previously, once we set up TLS, we would implicitly switch from using __hurd_reply_port0 to reply_port inside the TCB, leaving the former unused. But we never deallocated it, so it got leaked. Instead, migrate the port into the new TCB's reply_port slot. This avoids both the port leak and an extra syscall to create a new reply port for the TCB. Signed-off-by: Sergey Bugaev --- sysdeps/mach/hurd/i386/tls.h | 5 +++++ sysdeps/mach/hurd/x86_64/tls.h | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sysdeps/mach/hurd/i386/tls.h b/sysdeps/mach/hurd/i386/tls.h index ee7b8004..0ac8917a 100644 --- a/sysdeps/mach/hurd/i386/tls.h +++ b/sysdeps/mach/hurd/i386/tls.h @@ -141,12 +141,15 @@ _hurd_tls_init (tcbhead_t *tcb) HURD_TLS_DESC_DECL (desc, tcb); thread_t self = __mach_thread_self (); bool success = true; + extern mach_port_t __hurd_reply_port0; /* This field is used by TLS accesses to get our "thread pointer" from the TLS point of view. */ tcb->tcb = tcb; /* We always at least start the sigthread anyway. */ tcb->multiple_threads = 1; + /* Take over the reply port we've been using. */ + tcb->reply_port = __hurd_reply_port0; /* Get the first available selector. */ int sel = -1; @@ -172,6 +175,8 @@ _hurd_tls_init (tcbhead_t *tcb) /* Now install the new selector. */ asm volatile ("mov %w0, %%gs" :: "q" (sel)); + /* This port is now owned by the TCB. */ + __hurd_reply_port0 = MACH_PORT_NULL; out: __mach_port_deallocate (__mach_task_self (), self); diff --git a/sysdeps/mach/hurd/x86_64/tls.h b/sysdeps/mach/hurd/x86_64/tls.h index da504d9c..2b7135f6 100644 --- a/sysdeps/mach/hurd/x86_64/tls.h +++ b/sysdeps/mach/hurd/x86_64/tls.h @@ -177,13 +177,20 @@ _hurd_tls_init (tcbhead_t *tcb) { error_t err; thread_t self = __mach_thread_self (); + extern mach_port_t __hurd_reply_port0; /* We always at least start the sigthread anyway. */ tcb->multiple_threads = 1; + /* Take over the reply port we've been using. */ + tcb->reply_port = __hurd_reply_port0; err = _hurd_tls_new (self, tcb); if (err == 0) - __libc_tls_initialized = 1; + { + __libc_tls_initialized = 1; + /* This port is now owned by the TCB. */ + __hurd_reply_port0 = MACH_PORT_NULL; + } __mach_port_deallocate (__mach_task_self (), self); return err == 0; }