From patchwork Mon Aug 30 18:52:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44819 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 3A0343858C3B for ; Mon, 30 Aug 2021 18:57:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A0343858C3B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630349840; bh=Yj4lqg5GyclohBdRl0aYtIIflIOhCW/+1gqSxHJ/Pm4=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=vHFI5ilWIQMYVo7mkSDEptoreLuRtVzqWvq4VxU2wlPLsN3NLFjDxgp5/jC912yqu G49RgcU3Gt5P/lajKhJkRTEGHjEQwrtObdVkA63ii21hB0bCS1uRuyJ3d5/Ds61PY9 PpQ5SlvCcWMSsulb83ZIj1h1SI0SsZnna7CfEWF8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by sourceware.org (Postfix) with ESMTPS id CB1F53858429 for ; Mon, 30 Aug 2021 18:52:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CB1F53858429 Received: by mail-qk1-x729.google.com with SMTP id c10so16756989qko.11 for ; Mon, 30 Aug 2021 11:52:29 -0700 (PDT) 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=Yj4lqg5GyclohBdRl0aYtIIflIOhCW/+1gqSxHJ/Pm4=; b=T4u/dTDwzgxXuykcJ4FutD1HftuFpS6jlmEh5BN3BIa8zcPHL9DVuvcICBTWhK5Snr augnnWOLE4M67mycaWbnX7xi/jrQTvWwmpgZbvIk+yIQ/nRL1tgpyRtkdDrE+9dTHlmd 5fHeKMd+a6Ehipg37uxUCVZVtYflkOfZXEGOTnM3QcLIpsVzhupCEeb/1wIKLnFVambd rbx4pPtiVQJOrMaNLu4hdmCCDHWrBVR/LXo+GO5BQgisygKBM6mWtplO4tvIohi7rGvB rgzs0/PdRCAXk+ZyiUuZW/E/OZvjRThRp4khcT/Q3ncN4xSsg7J1zZntIJvvR4zjkA1g 99dg== X-Gm-Message-State: AOAM531mOVPdYu8AM0HrPbhKoSwpxSBWAjfpmb8UV9n45KTbXGLtZVyO wpHmea7Y5c3GehDOlX+BiORiAIeAOBEmqQ== X-Google-Smtp-Source: ABdhPJxAGw4qztnQHk7r3wlohasqK0QORnnki6WVKm9Tmj6BXYpRjF255Lf4HmG0Nh3dedSl+Zgmbg== X-Received: by 2002:a37:65c3:: with SMTP id z186mr23409453qkb.313.1630349549269; Mon, 30 Aug 2021 11:52:29 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:1a68:7647:1f41:2147:1ed2]) by smtp.gmail.com with ESMTPSA id m187sm11752338qkd.131.2021.08.30.11.52.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Aug 2021 11:52:29 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v4 6/7] malloc: Move MORECORE fallback mmap to sysmalloc_mmap_fallback Date: Mon, 30 Aug 2021 15:52:14 -0300 Message-Id: <20210830185215.449572-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210830185215.449572-1-adhemerval.zanella@linaro.org> References: <20210830185215.449572-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Cc: Norbert Manthey , Guillaume Morin , Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" So it can be used on hugepage code as well. --- malloc/malloc.c | 85 ++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/malloc/malloc.c b/malloc/malloc.c index c91554edf9..3421a0b5da 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -2500,6 +2500,51 @@ sysmalloc_mmap (INTERNAL_SIZE_T nb, size_t pagesize, int extra_flags, mstate av) return chunk2mem (p); } +/* + Allocate memory using mmap() based on S and NB requested size, aligning to + PAGESIZE if required. The EXTRA_FLAGS is used on mmap() call. If the call + succeedes S is updated with the allocated size. This is used as a fallback + if MORECORE fails. + */ +static void * +sysmalloc_mmap_fallback (long int *s, INTERNAL_SIZE_T nb, + INTERNAL_SIZE_T old_size, size_t minsize, + size_t pagesize, int extra_flags, mstate av) +{ + long int size = *s; + + /* Cannot merge with old top, so add its size back in */ + if (contiguous (av)) + size = ALIGN_UP (size + old_size, pagesize); + + /* If we are relying on mmap as backup, then use larger units */ + if ((unsigned long) (size) < minsize) + size = minsize; + + /* Don't try if size wraps around 0 */ + if ((unsigned long) (size) <= (unsigned long) (nb)) + return MORECORE_FAILURE; + + char *mbrk = (char *) (MMAP (0, size, + mtag_mmap_flags | PROT_READ | PROT_WRITE, + extra_flags)); + if (mbrk == MAP_FAILED) + return MAP_FAILED; + +#ifdef MAP_HUGETLB + if (!(extra_flags & MAP_HUGETLB)) + madvise_thp (mbrk, size); +#endif + + /* Record that we no longer have a contiguous sbrk region. After the first + time mmap is used as backup, we do not ever rely on contiguous space + since this could incorrectly bridge regions. */ + set_noncontiguous (av); + + *s = size; + return mbrk; +} + static void * sysmalloc (INTERNAL_SIZE_T nb, mstate av) { @@ -2697,38 +2742,14 @@ sysmalloc (INTERNAL_SIZE_T nb, mstate av) segregated mmap region. */ - /* Cannot merge with old top, so add its size back in */ - if (contiguous (av)) - size = ALIGN_UP (size + old_size, pagesize); - - /* If we are relying on mmap as backup, then use larger units */ - if ((unsigned long) (size) < (unsigned long) (MMAP_AS_MORECORE_SIZE)) - size = MMAP_AS_MORECORE_SIZE; - - /* Don't try if size wraps around 0 */ - if ((unsigned long) (size) > (unsigned long) (nb)) - { - char *mbrk = (char *) (MMAP (0, size, - mtag_mmap_flags | PROT_READ | PROT_WRITE, - 0)); - - if (mbrk != MAP_FAILED) - { - madvise_thp (mbrk, size); - - /* We do not need, and cannot use, another sbrk call to find end */ - brk = mbrk; - snd_brk = brk + size; - - /* - Record that we no longer have a contiguous sbrk region. - After the first time mmap is used as backup, we do not - ever rely on contiguous space since this could incorrectly - bridge regions. - */ - set_noncontiguous (av); - } - } + char *mbrk = sysmalloc_mmap_fallback (&size, nb, old_size, pagesize, + MMAP_AS_MORECORE_SIZE, 0, av); + if (mbrk != MAP_FAILED) + { + /* We do not need, and cannot use, another sbrk call to find end */ + brk = mbrk; + snd_brk = brk + size; + } } if (brk != (char *) (MORECORE_FAILURE))