From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0358D3858401 for ; Wed, 10 Aug 2022 06:22:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0358D3858401 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-593-X7QxIspHMQSp6RmxAlY9hg-1; Wed, 10 Aug 2022 02:22:44 -0400 X-MC-Unique: X7QxIspHMQSp6RmxAlY9hg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 009BD1C051A3 for ; Wed, 10 Aug 2022 06:22:44 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.75]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 749FB1415116 for ; Wed, 10 Aug 2022 06:22:43 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] malloc: Do not use MAP_NORESERVE to allocate heap segments Date: Wed, 10 Aug 2022 08:22:41 +0200 Message-ID: <87fsi4hb2m.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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-List-Received-Date: Wed, 10 Aug 2022 06:22:47 -0000 Address space for heap segments is reserved in a mmap call with MAP_ANONYMOUS | MAP_PRIVATE and protection flags PROT_NONE. This reservation does not count against the RSS limit of the process or system. Backing memory is allocated using mprotect in alloc_new_heap and grow_heap, and at this point, the allocator expects the kernel to provide memory (subject to memory overcommit). The SIGSEGV that might generate due to MAP_NORESERVE (according to the mmap manual page) does not seem to occur in practice, it's always SIGKILL from the OOM killer. Even if there is a way that SIGSEGV could be generated, it is confusing to applications that this only happens for secondary heaps, not for large mmap-based allocations, and not for the main arena. --- malloc/arena.c | 5 +---- malloc/malloc.c | 4 ---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/malloc/arena.c b/malloc/arena.c index defd25c8a6..074ecbc09f 100644 --- a/malloc/arena.c +++ b/malloc/arena.c @@ -559,16 +559,13 @@ new_heap (size_t size, size_t top_pad) #if HAVE_TUNABLES if (__glibc_unlikely (mp_.hp_pagesize != 0)) { - /* MAP_NORESERVE is not used for huge pages because some kernel may - not reserve the mmap region and a subsequent access may trigger - a SIGBUS if there is no free pages in the pool. */ heap_info *h = alloc_new_heap (size, top_pad, mp_.hp_pagesize, mp_.hp_flags); if (h != NULL) return h; } #endif - return alloc_new_heap (size, top_pad, GLRO (dl_pagesize), MAP_NORESERVE); + return alloc_new_heap (size, top_pad, GLRO (dl_pagesize), 0); } /* Grow a heap. size is automatically rounded up to a diff --git a/malloc/malloc.c b/malloc/malloc.c index 914052eb69..29fa71b3b2 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1110,10 +1110,6 @@ static mchunkptr mremap_chunk(mchunkptr p, size_t new_size); # define MAP_ANONYMOUS MAP_ANON #endif -#ifndef MAP_NORESERVE -# define MAP_NORESERVE 0 -#endif - #define MMAP(addr, size, prot, flags) \ __mmap((addr), (size), (prot), (flags)|MAP_ANONYMOUS|MAP_PRIVATE, -1, 0)