Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3172987imu; Fri, 23 Nov 2018 23:17:34 -0800 (PST) X-Google-Smtp-Source: AJdET5fZKL7sof+1OETVekzn4wW9Lk7mvbJfD48ds9GgfbKkErYWKj6OjWtx/RsUN8fQrvyCy4CX X-Received: by 2002:a62:3687:: with SMTP id d129-v6mr19275838pfa.56.1543043854361; Fri, 23 Nov 2018 23:17:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543043854; cv=none; d=google.com; s=arc-20160816; b=OzErN6vkWg2MuunYBEcxPyP9u6LvAsu3kpXL9Os7l4b5YoN8CEpQQve8XUSJ3s+zEu MnwFLBoz0TS4teB1hr6dqsCa/7pJDwgxWMaIqtsPsfMepwoz+3gBv8FQFCmKp2Zu0p8v e5Ndmyqz9ogkPq2NQ4TKyud16yP2Vlv1n8h8GqFWN8JZ0M/bZdo10+6Am+7+aDRSbC1m 09QURi9SUWZ2OhBmpaLtNva5K1EEC3iXl/VLWiGuJJHCzsrFOTAN5No426/Z595hgi5/ cs0EhA5lrZzo6rKYPYaycN5v92S00kJzzAVR/PzvDET9uaSfr/kFJ271Dbc7akY6YwT5 BiGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:thread-index:thread-topic :content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:dkim-signature:dkim-filter; bh=PIumVTXYQXvbWQfPyOMf00cNIJ33l9bzdm07kjzbotg=; b=IUqsxNsHL9HGU9AznuFIgeE/3biMpIuUq2UZ4t8/9hmuhj6ptxFB19AAZqo177YQ+9 ORO2fYKSe7QLrFZr2ZcfmamCACQhMC/CQOQ9kkuFXXOUuLe83CeM1yvc6keXzF6+g9u6 aM8qOqANqWz9IXK8NZmf6J7B2+hMgqeKq347kxil0/55Nb/e1gsPUeQqT17UByWDxzhw 4KGMO4RWHWi6RwaQXaA37SgUM/lMLNLg5by9DNQ9H0X56LV4Xo2Zr/L98D3ImGJ7ToL4 O9/rxoQ805Q6CKFd5jv1oNVZkdnu9tAWazY77pjtAss3ITJlAJlvQEWikKmN0CFz7jUq z/Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=G81ulrNf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s80si17377731pfa.130.2018.11.23.23.17.20; Fri, 23 Nov 2018 23:17:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=G81ulrNf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438314AbeKWGRP (ORCPT + 99 others); Fri, 23 Nov 2018 01:17:15 -0500 Received: from mail.efficios.com ([167.114.142.138]:34940 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731412AbeKWGRP (ORCPT ); Fri, 23 Nov 2018 01:17:15 -0500 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 426A17B11E; Thu, 22 Nov 2018 14:36:26 -0500 (EST) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id PrYYPdd2Jijd; Thu, 22 Nov 2018 14:36:25 -0500 (EST) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 92DE37B119; Thu, 22 Nov 2018 14:36:25 -0500 (EST) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 92DE37B119 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1542915385; bh=PIumVTXYQXvbWQfPyOMf00cNIJ33l9bzdm07kjzbotg=; h=Date:From:To:Message-ID:MIME-Version; b=G81ulrNfbZBBg0cjnuzlGV1kQcgig7fZG+5heOii0BD2CMhhjnGOeu49YFJ2aV/+Y zcWmY7cJP8SkRhhi+WWZy4t+nXJnvcfmLbCI76T6W1wR6zrhYfvJWb67yfXM90gNrl 4sGZ7a8HyFwDKn9WNZC7DN/4ORv02ZIz6BfJHoKTqBimGPsQaIskix+lOkZsezfFsJ SQejHvoUIm5pbgiXspPyrxS/SIQ1uM60cV4Mm061ipW8W3Xbb19eIyBaeTq/63yLCY 7jQ+fSDGohTVo38gN3KR4FeMkyHDPfjP1HwgCmwG6lDNEXOpzsaHBCwkuhuttjPFbb O8dgBubqQ0hNw== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id NsKJOeenbhex; Thu, 22 Nov 2018 14:36:25 -0500 (EST) Received: from mail02.efficios.com (mail02.efficios.com [167.114.142.138]) by mail.efficios.com (Postfix) with ESMTP id 700F47B10D; Thu, 22 Nov 2018 14:36:25 -0500 (EST) Date: Thu, 22 Nov 2018 14:36:25 -0500 (EST) From: Mathieu Desnoyers To: Rich Felker Cc: Szabolcs Nagy , Florian Weimer , nd , carlos , Joseph Myers , libc-alpha , Thomas Gleixner , Ben Maurer , Peter Zijlstra , "Paul E. McKenney" , Boqun Feng , Will Deacon , Dave Watson , Paul Turner , linux-kernel , linux-api Message-ID: <1705791029.10494.1542915385130.JavaMail.zimbra@efficios.com> In-Reply-To: <20181122190143.GI23599@brightrain.aerifal.cx> References: <20181121183936.8176-1-mathieu.desnoyers@efficios.com> <87a7m1ywni.fsf@oldenburg.str.redhat.com> <20181122151710.GF23599@brightrain.aerifal.cx> <875zwpyw81.fsf@oldenburg.str.redhat.com> <1306224240.10055.1542900799576.JavaMail.zimbra@efficios.com> <7032ab45-8314-b85d-5b4d-f83b41dab5b6@arm.com> <1602745030.10426.1542911744479.JavaMail.zimbra@efficios.com> <20181122190143.GI23599@brightrain.aerifal.cx> Subject: Re: [RFC PATCH v4 1/5] glibc: Perform rseq(2) registration at nptl init and thread creation MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [167.114.142.138] X-Mailer: Zimbra 8.8.10_GA_3047 (ZimbraWebClient - FF52 (Linux)/8.8.10_GA_3041) Thread-Topic: glibc: Perform rseq(2) registration at nptl init and thread creation Thread-Index: QmI0bEoeb/5TqaWLKcni1VnrANiv0A== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ----- On Nov 22, 2018, at 2:01 PM, Rich Felker dalias@libc.org wrote: > On Thu, Nov 22, 2018 at 01:35:44PM -0500, Mathieu Desnoyers wrote: >> ----- On Nov 22, 2018, at 11:24 AM, Szabolcs Nagy Szabolcs.Nagy@arm.com wrote: >> >> > On 22/11/18 15:33, Mathieu Desnoyers wrote: >> >> ----- On Nov 22, 2018, at 10:21 AM, Florian Weimer fweimer@redhat.com wrote: >> >>> Right, but in case of user-supplied stacks, we actually free TLS memory >> >>> at this point, so signals need to be blocked because the TCB is >> >>> (partially) gone after that. >> >> >> >> Unfortuntately, disabling signals is not enough. >> >> >> >> With rseq registered, the kernel accesses the rseq TLS area when returning to >> >> user-space after _preemption_ of user-space, which can be triggered at any >> >> point by an interrupt or a fault, even if signals are blocked. >> >> >> >> So if there are cases where the TLS memory is freed while the thread is still >> >> running, we _need_ to explicitly unregister rseq beforehand. >> > >> > i think the man page should point this out. >> >> Yes, I should add this to the proposed rseq(2) man page. >> >> > >> > the memory of a registered rseq object must not be freed >> > before thread exit. (either unregister it or free later) >> > >> > and ideally also point out that c language thread storage >> > duration does not provide this guarantee: it may be freed >> > by the implementation before thread exit (which is currently >> > not observable, but with the rseq syscall it is). >> >> How about the following wording ? >> >> Memory of a registered rseq object must not be freed before the >> thread exits. Reclaim of rseq object's memory must only be >> done after either an explicit rseq unregistration is performed >> or after the thread exit. Keep in mind that the implementation >> of the Thread-Local Storage (C language __thread) lifetime does >> not guarantee existence of the TLS area up until the thread exits. > > This is all really ugly for application/library code to have to deal > with. Maybe if the man page is considered as documenting the syscall > only, and not something you can use, it's okay, This is indeed for the rseq(2) manpage targeting the man-pages project, which documents system calls. > but "until the thread > exits" is not well-defined in the sense you want it here. It's more > like "until the kernel task for the thread exits", and the whole point > is that there is some interval in time between the abstract thread > exit and the kernel task exit that is not observable without rseq but > is observable if the rseq is wrongly left installed. It's important to clear a possible misunderstanding here: from the point where the thread issues the "exit" system call, the kernel won't touch the registered rseq TLS area anymore. So the point where the thread exits is actually well defined, even from a user-space perspective. The problematic scenario arises when glibc frees the TLS memory before invoking exit() when the thread terminates. In this kind of scenario, we need to explicitly invoke rseq unregister before TLS memory reclaim. Thanks, Mathieu -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com