Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1ojhwz-00005v-0a for mharc-grub-devel@gnu.org; Sat, 15 Oct 2022 10:15:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51870) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ojhwx-0008Tt-8V for grub-devel@gnu.org; Sat, 15 Oct 2022 10:15:39 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:33406) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ojhwv-0002um-AT for grub-devel@gnu.org; Sat, 15 Oct 2022 10:15:38 -0400 Received: by mail-pj1-x1031.google.com with SMTP id n18-20020a17090ade9200b0020b0012097cso9878738pjv.0 for ; Sat, 15 Oct 2022 07:15:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZjtoQFrAXOLIK4z5UmpkDkNkLR9gtZ9ma2AZuycgvDM=; b=adctKB/dDtOy+DFfBohvSiOkj2Z2FwRRMtp6I2GKwCqAnOkQbDDd/1lOBZRIdtKQts mjN7QrhlNPfftoemuSlqucd3iFxNIdhaPlBqm3kh3o7pYnQdOQXF/a/SriCFkwDwCfCS BxI2JU7oHN0DI5JHxzdzS08/Xtvqqh0jn7L+3jWh0+X23aOkFlsqTvzJbrCGvWIMLLd9 Nn3LhXch2cunvYGDCN9dwtuNoESKG1sUuFsGJNsAMrNhYTJkFB3Eg9r/3/Dv43fBe8Mw btYuQLoh2xmIkLPPamo6yhnxOFJaBP+KRX2Yd/FqbakKpEitcepcX7YJPPLc5OFWZnDJ PIBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZjtoQFrAXOLIK4z5UmpkDkNkLR9gtZ9ma2AZuycgvDM=; b=W9MNqmfgKAMTOoD6oCrjzQBEHnTY4aIOYobsAWyCMD+kJJZ7w1lE3/pRNxayeJMJ2p qCmOMPulHKpcjsGOWhq+PJ59EU7sTLoJwLH5QMNGQ6y+4Tx9b79lEKlNDu1sMdo7BLwZ C9rRBhhDKmhfTl4v5BtfQnkCHCaG48afxOs0YrlIM0JhPZpSd6USCwF7hKKbCVELWfsN VdjHvNxPTQVCyVLOk1Bkk/cUkwC2CPph9fUYnSNkEClLHjBcyeXlfQfvX5jPlIovqinU H03vv49q3hbZe97L97fqG9kY87IO7H8eQwcQDOuE6BT6XUNiUn6czcQjqRsGqCgOnz3R i2fg== X-Gm-Message-State: ACrzQf1G39bfBpnZV38C8ZspmN+tEbppG96UA0wzkPsraBu5v8Ix82f/ AZtXFSiV3zY+zUuO4nOqjJN5hg0fUC0o9Q== X-Google-Smtp-Source: AMsMyM7yl6KCnBL+z2hCNK6Pc6q2rS2Xxzy+cVNqqA4Q3b3AocWeMR5QS8csyzaNZC6mW+W0S1+54Q== X-Received: by 2002:a17:902:f710:b0:178:a692:b1f7 with SMTP id h16-20020a170902f71000b00178a692b1f7mr3189905plo.112.1665843335884; Sat, 15 Oct 2022 07:15:35 -0700 (PDT) Received: from localhost.localdomain (ec2-13-113-80-70.ap-northeast-1.compute.amazonaws.com. [13.113.80.70]) by smtp.gmail.com with ESMTPSA id i15-20020a62870f000000b00562adeac326sm3580890pfe.102.2022.10.15.07.15.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Oct 2022 07:15:35 -0700 (PDT) From: Zhang Boyang To: grub-devel@gnu.org Cc: dkiper@net-space.pl, ps@pks.im, jim945@mail.ru, glin@suse.com, dja@axtens.net, droidbittin@gmail.com, heinrich.schuchardt@canonical.com, langner.marcel@myiq.de, marcan@marcan.st, Zhang Boyang Subject: [PATCH v4 2/2] mm: Better handling of adding new regions Date: Sat, 15 Oct 2022 22:15:12 +0800 Message-Id: <20221015141512.72193-3-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221015141512.72193-1-zhangboyang.id@gmail.com> References: <20221015141512.72193-1-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 15 Oct 2022 14:15:39 -0000 Previously, there are two problems of the handling of adding new regions. First, it doesn't take region management cost into account. Adding a memory area of `x` bytes will result in a heap region of less than `x` bytes avaliable. Thus, the new region might not adequate for current allocation request. Second, it doesn't pre-allocate a big region for small allocations, so it might be slow for many small allocations. This patch introduces GRUB_MM_MAX_COST to address the cost of region management. The value of this constant should make `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)` initialize a region of at least `size` free bytes available. The size of heap growth is now adjusted according this value before calling grub_mm_add_region_fn (). This patch also introduces GRUB_MM_HEAP_GROW to address the minimal heap growth granularity. When a small allocation encounters heap space exhaustion, the size of heap growth is now expended to this value, and a new region of bigger size will be added. Thus subsequent allocations can use the remaining space of that new region. Signed-off-by: Zhang Boyang --- grub-core/kern/mm.c | 18 +++++++++++++++--- include/grub/mm_private.h | 12 ++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index ae2279133..fe630f061 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -410,7 +410,9 @@ grub_memalign (grub_size_t align, grub_size_t size) { grub_mm_region_t r; grub_size_t n = ((size + GRUB_MM_ALIGN - 1) >> GRUB_MM_ALIGN_LOG2) + 1; + grub_size_t guarantee; int count = 0; + grub_size_t grow; if (!grub_mm_base) goto fail; @@ -418,10 +420,13 @@ grub_memalign (grub_size_t align, grub_size_t size) if (size > ~(grub_size_t) align) goto fail; + /* Allocation can always success if max continuous free chunk >= guarantee */ + guarantee = size + align; + /* We currently assume at least a 32-bit grub_size_t, so limiting allocations to - 1MiB in name of sanity is beneficial. */ - if ((size + align) > ~(grub_size_t) 0x100000) + if (guarantee > ~(grub_size_t) 0x100000) goto fail; align = (align >> GRUB_MM_ALIGN_LOG2); @@ -443,11 +448,18 @@ grub_memalign (grub_size_t align, grub_size_t size) switch (count) { case 0: + /* Calculate optimal size of heap growth. */ + grow = grub_max(guarantee, GRUB_MM_HEAP_GROW); + + /* Adjust heap growth to address the cost of region management. */ + if (grub_add(grow, GRUB_MM_MAX_COST, &grow)) + goto fail; + /* Request additional pages, contiguous */ count++; if (grub_mm_add_region_fn != NULL && - grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE) + grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE) goto again; /* fallthrough */ @@ -462,7 +474,7 @@ grub_memalign (grub_size_t align, grub_size_t size) * Try again even if this fails, in case it was able to partially * satisfy the request */ - grub_mm_add_region_fn (size, GRUB_MM_ADD_REGION_NONE); + grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_NONE); goto again; } diff --git a/include/grub/mm_private.h b/include/grub/mm_private.h index 96c2d816b..a01ba2507 100644 --- a/include/grub/mm_private.h +++ b/include/grub/mm_private.h @@ -95,6 +95,18 @@ typedef struct grub_mm_region } *grub_mm_region_t; +/* + * Set an upper bound of management cost of each region, + * with sizeof(struct grub_mm_region), sizeof(struct grub_mm_header) and + * any possible alignment or padding taken into account. + * The value should make `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)` + * initialize a region of at least `size` free bytes available. + */ +#define GRUB_MM_MAX_COST 0x1000 + +/* Minimal heap growth granularity, when existing heap space is exhausted. */ +#define GRUB_MM_HEAP_GROW 0x100000 + #ifndef GRUB_MACHINE_EMU extern grub_mm_region_t EXPORT_VAR (grub_mm_base); #endif -- 2.30.2