From patchwork Fri Sep 18 20:11:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Balazs Kezes X-Patchwork-Id: 519572 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 290EF1401B5 for ; Sat, 19 Sep 2015 06:10:49 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=ZJ+HCgmm; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:cc:subject:message-id:references :mime-version:content-type:in-reply-to; q=dns; s=default; b=MfZm eXFp7FXH1Sh7CimbKaqs1spaLPSw98gjYBHzPJFDsL1cG/AIGMh/kH+w3vbSToLp /mY9iSfhcYcbnS/5ZXeyn6pzdYZbE6VFAtRomIKV7dJFXr4Irp/L6O2P/R35n06x 9wUpXe/BpHoA5CAY4kitsdVGWyIZLXBkbVyixLY= 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:date:from:to:cc:subject:message-id:references :mime-version:content-type:in-reply-to; s=default; bh=O353sUyP7t nNPAH+zBz/xnF6nbw=; b=ZJ+HCgmmWWvhyNQXL0yL/T7sr+gRjmfA3HPqerk3Ms VLHUSqBCm4o8ELU57MWcEWgt2SNJbxKDO9gqVEg8oaXJS/SXt5PSlWtmJ/KvWNw2 2FpWCkuPIENugewqtGaFLntgETQDp0nHNPwr26tMHA9sWiozkiEuNtobcjjKnMnP s= Received: (qmail 51814 invoked by alias); 18 Sep 2015 20:10: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 51801 invoked by uid 89); 18 Sep 2015 20:10:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-wi0-f169.google.com X-Received: by 10.180.23.193 with SMTP id o1mr56242wif.71.1442607039258; Fri, 18 Sep 2015 13:10:39 -0700 (PDT) Date: Fri, 18 Sep 2015 21:11:01 +0100 From: Balazs Kezes To: Rich Felker Cc: libc-alpha@sourceware.org Subject: Re: pthread wastes memory with mlockall(MCL_FUTURE) Message-ID: <20150918201101.GD27881@eper> References: <20150918102734.GA27881@eper> <20150918143824.GB17773@brightrain.aerifal.cx> <20150918163842.GB27881@eper> <20150918170853.GC17773@brightrain.aerifal.cx> <20150918192952.GC27881@eper> <20150918194521.GD17773@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20150918194521.GD17773@brightrain.aerifal.cx> User-Agent: Mutt/1.5.21 (2010-09-15) On 2015-09-18 15:45 -0400, Rich Felker wrote: > On Fri, Sep 18, 2015 at 08:29:52PM +0100, Balazs Kezes wrote: > > So here's what I think pthreads should do: First mmap with PROT_NONE > > and only then should mprotect read/write the stack pages. > > > > Does that sound reasonable? > > Yes. So here's the simplest patch I could come up with: I've verified in my pthreads example that pthreads doesn't waste memory with this patch applied. That's not really a nice fix though but this allocatestack() function looks too scary to me. :( diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index 753da61..c6065dc 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -501,12 +501,21 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp, size += pagesize_m1 + 1; #endif - mem = mmap (NULL, size, prot, + /* Map with PROT_NONE first and only then mprotect the pages to avoid + the kernel unnecessary reserving the pages in the case of + mlockall(MCL_FUTURE). */ + mem = mmap (NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0); if (__glibc_unlikely (mem == MAP_FAILED)) return errno; + if (__glibc_unlikely (mprotect (mem, size, prot) != 0)) + { + munmap(mem, size); + return errno; + } + /* SIZE is guaranteed to be greater than zero. So we can never get a null pointer back from mmap. */ assert (mem != NULL);