From patchwork Tue Aug 29 13:34:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 111215 Delivered-To: patch@linaro.org Received: by 10.140.95.112 with SMTP id h103csp1344711qge; Tue, 29 Aug 2017 06:34:56 -0700 (PDT) X-Received: by 10.99.174.65 with SMTP id e1mr321888pgp.149.1504013696002; Tue, 29 Aug 2017 06:34:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1504013695; cv=none; d=google.com; s=arc-20160816; b=FDBcx3x07Rk6yuSNjTjjG2RHjHdouQDzoxNTcGNx99OzW2Y7qbaGDDqCs74wr5lrE+ SoY2W6GPBuZgbOuLUsIakAcPTq+Du2TSm+EOrfwidnGbYoYuZ+xTxKGJc5rlaarutq3z j82Oj6cLo48bq3fSiJ9Z9w8xinYrLKTnVs7pGB6AlIoSyW/pT+lAG5LRF/sAvGUlCBZN bVLusNZ2nsxZK6/MK/9EWIqs5xWwiw7w1pLcUyyQxY89nV+jC4CKl4Z9Aeb2sfLWPpbU t3/sqREzu4uCikERrAFuT2oHInoHUbLGtWXdsdlq88hEu4LKVm8djlUf11ucSJndsrxh xbXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:delivered-to:sender:list-help :list-post:list-archive:list-subscribe:list-unsubscribe:list-id :precedence:mailing-list:dkim-signature:domainkey-signature :arc-authentication-results; bh=XPPKTbnvFfGb8ldan6nZRmClDOejqrAaYTcAz0GgrsU=; b=WXSzIf+RPwa9xeusIpHCKS2Pzp9nkhtwuMbRB0l875Un6lhvNShLzN9fM048lROP/q 4cOQ/dtJ6KkgnOlcEqJdIEXwQ0LSUWPcl9GQW2sffObLS9ervEiNFDQD6a/0WFCXBr1u iFInDBq2MOsTzg4IziFWdsicTbIfMClaHEwxUuaVK81dNkxTeFHKY3RPL8WvNkizAbPI tlIUZi0k80TIOJpTF2sWitONV3SLCfkHpGV0zcP6lO1oahujzXdUqkNNqOrJ8J0FnLhL nJjj5v3xKfLn8mjr0lOgQi0bQA1D+4swNMnhqq3KaJRjqjdSla1QxFtbKc8/sOx+vXT+ Uj9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=dVtPNJHX; spf=pass (google.com: domain of libc-alpha-return-83814-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83814-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id f26si2344557pge.520.2017.08.29.06.34.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Aug 2017 06:34:55 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-83814-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=dVtPNJHX; spf=pass (google.com: domain of libc-alpha-return-83814-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83814-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; q=dns; s= default; b=NchAh5vU/9LEnsxBx/6EnxbzBhpwApVAMW2SIq5pxc/35mDuqnN6X Xo6UCZ3IITwfsDAB5CpD7VZzp5T0E+XucdDlMPj4xaXLCqNtyja3/hgqmbWBiahx fnfydH5mSRZaZ/CzSxqZIfGY7tcJXWEuDZDWnUXF/93QtV0spetSLU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id; s=default; bh=+gE4elJrAW8R6QABex++RugfytA=; b=dVtPNJHXABIfQ/EPeG8AacBik6CR 5eKyRrhE6XRu+qdzlD5dYfBbPBBn957DC3QWNbR+nRKDV+Gp8lIdu+jO6MXerCoA StL31O+IrOTfVeLfaHw8+8Na0oUiGelPbi+FDed6F7RcyXMY9/QCLWJY/Za/JMMm QNdE9l46ayct6Ho= Received: (qmail 126892 invoked by alias); 29 Aug 2017 13:34:43 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 126776 invoked by uid 89); 29 Aug 2017 13:34:41 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=advertise, H*m:1367, H*MI:1367 X-HELO: mail-qk0-f171.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=XPPKTbnvFfGb8ldan6nZRmClDOejqrAaYTcAz0GgrsU=; b=f7jDlHq+FZf36AmoJOu0m6kVjwfe4ewOgwHF5MTdKVmWSGPdJmykBsURjDOUQNmFIZ LS69XiuzwnyqOgPlh/BrfdBByDcCAJiz2rY7V/sJNbAvOxjnfkUa2O9aaaqQFF0IxpRz EBcARJkjyflv/PW8BaRj1L4hCfsDNv5UNBOggiZHZ18bqX5fyu2Y6Eh3FqL6WbxE0rK0 guwT33CJfUQgXCGLD9M8kR/aWCj9eKSe9v0w8DiL0zX+9Gz+xphkwu0vuf122SZy5JA9 fQEr8Nil6kIHQ6kJyLToKb5AvBRPM2rz4ubq9pZzTpwC3VCD9CES8Fn3lfXPWzMp+tzj vnRg== X-Gm-Message-State: AHYfb5gHfo4mdIWRXGm/wtmK4OYzOaxAoVHDm5OoDM+0KBAmKUsNswKQ bDFR3aTBGzMXf6CibAPXGA== X-Google-Smtp-Source: ADKCNb4htzydEOvcBrT5nTkIBpERl6XTCIc3X94XcyHpNqqctnHe6RyjR01T3AatAHMvFc0i9P+CJg== X-Received: by 10.55.121.130 with SMTP id u124mr5867860qkc.13.1504013669285; Tue, 29 Aug 2017 06:34:29 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Sergei Trofimovich Subject: [PATCH] ia64: Fix thread stack allocation permission set (BZ #21672) Date: Tue, 29 Aug 2017 10:34:22 -0300 Message-Id: <1504013662-1367-1-git-send-email-adhemerval.zanella@linaro.org> This patch fixes ia64 failures on thread exit by madvise the required area taking in consideration its disjoing stacks (NEED_SEPARATE_REGISTER_STACK). Also the snippet that setup the madvise call to advertise kernel the area won't be used anymore in near future is reallocated in allocatestack.c (for consistency to put all stack management function in one place). Checked on x86_64-linux-gnu and i686-linux-gnu for sanity (since it is not expected code changes for architecture that do not define NEED_SEPARATE_REGISTER_STACK) and also got a report that it fixes ia64-linux-gnu failures from Sergei Trofimovich . [BZ #21672] * nptl/allocatestack.c [_STACK_GROWS_DOWN] (setup_stack_prot): Set to use !NEED_SEPARATE_REGISTER_STACK as well. (advise_stack_range): New function. * nptl/pthread_create.c (START_THREAD_DEFN): Move logic to mark stack non required to advise_stack_range at allocatestack.c --- ChangeLog | 9 +++++++++ nptl/allocatestack.c | 29 ++++++++++++++++++++++++++++- nptl/pthread_create.c | 27 ++------------------------- 3 files changed, 39 insertions(+), 26 deletions(-) -- 2.7.4 diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index 6d1bcaa..8766deb 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -356,7 +356,7 @@ setup_stack_prot (char *mem, size_t size, char *guard, size_t guardsize, const int prot) { char *guardend = guard + guardsize; -#if _STACK_GROWS_DOWN +#if _STACK_GROWS_DOWN && !defined(NEED_SEPARATE_REGISTER_STACK) /* As defined at guard_position, for architectures with downward stack the guard page is always at start of the allocated area. */ if (__mprotect (guardend, size - guardsize, prot) != 0) @@ -372,6 +372,33 @@ setup_stack_prot (char *mem, size_t size, char *guard, size_t guardsize, return 0; } +/* Mark the memory of the stack as usable to the kernel. It frees everything + except for the space used for the TCB itself. */ +static inline void +__always_inline +advise_stack_range (void *mem, size_t size, uintptr_t pd, size_t guardsize) +{ + uintptr_t sp = (uintptr_t) CURRENT_STACK_FRAME; + size_t pagesize_m1 = __getpagesize () - 1; +#if _STACK_GROWS_DOWN && !defined(NEED_SEPARATE_REGISTER_STACK) + size_t freesize = (sp - (uintptr_t) mem) & ~pagesize_m1; + assert (freesize < size); + if (freesize > PTHREAD_STACK_MIN) + __madvise (mem, freesize - PTHREAD_STACK_MIN, MADV_DONTNEED); +#else + /* Page aligned start of memory to free (higher than or equal + to current sp plus the minimum stack size). */ + uintptr_t freeblock = (sp + PTHREAD_STACK_MIN + pagesize_m1) & ~pagesize_m1; + uintptr_t free_end = (pd - guardsize) & ~pagesize_m1; + if (free_end > freeblock) + { + size_t freesize = free_end - freeblock; + assert (freesize < size); + __madvise ((void*) freeblock, freesize, MADV_DONTNEED); + } +#endif +} + /* Returns a usable stack for a new thread either by allocating a new stack or reusing a cached stack of sufficient size. ATTR must be non-NULL and point to a valid pthread_attr. diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 2f8ada3..83b88bf 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -551,31 +551,8 @@ START_THREAD_DEFN } #endif - /* Mark the memory of the stack as usable to the kernel. We free - everything except for the space used for the TCB itself. */ - size_t pagesize_m1 = __getpagesize () - 1; -#ifdef _STACK_GROWS_DOWN - char *sp = CURRENT_STACK_FRAME; - size_t freesize = (sp - (char *) pd->stackblock) & ~pagesize_m1; - assert (freesize < pd->stackblock_size); - if (freesize > PTHREAD_STACK_MIN) - __madvise (pd->stackblock, freesize - PTHREAD_STACK_MIN, MADV_DONTNEED); -#else - /* Page aligned start of memory to free (higher than or equal - to current sp plus the minimum stack size). */ - void *freeblock = (void*)((size_t)(CURRENT_STACK_FRAME - + PTHREAD_STACK_MIN - + pagesize_m1) - & ~pagesize_m1); - char *free_end = (char *) (((uintptr_t) pd - pd->guardsize) & ~pagesize_m1); - /* Is there any space to free? */ - if (free_end > (char *)freeblock) - { - size_t freesize = (size_t)(free_end - (char *)freeblock); - assert (freesize < pd->stackblock_size); - __madvise (freeblock, freesize, MADV_DONTNEED); - } -#endif + advise_stack_range (pd->stackblock, pd->stackblock_size, (uintptr_t) pd, + pd->guardsize); /* If the thread is detached free the TCB. */ if (IS_DETACHED (pd))