Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oXkIL-0006K2-7U for mharc-grub-devel@gnu.org; Mon, 12 Sep 2022 10:20:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50218) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oXkIG-0006HX-1O for grub-devel@gnu.org; Mon, 12 Sep 2022 10:20:13 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]:40700) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oXkID-0008Hr-QE for grub-devel@gnu.org; Mon, 12 Sep 2022 10:20:11 -0400 Received: by mail-pl1-x633.google.com with SMTP id b21so8745463plz.7 for ; Mon, 12 Sep 2022 07:20:08 -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; bh=cVjpn61le3LO/pEjxA8R7dNQPZJZTAfKDCMhmqNZy3A=; b=oDZqBj8ngmYuUjpwg+ZrCMrwUJHFfEel444NBGJgxhsXwIRQ82u7KJdatza8PY+XYR OEX6r7FMvVW37q7UIndCwzSGGfVI9xjP+Jtp6TJB/YKqKXx8gn5p4rUv+jOaAmqlVxny MI/eJXOt6WaKDSKZXiPy7oGqWIuAZe51sY3Dzvj2YwgnzxTsIbnAhqr4l57nUuJHtZ1P caY3xckKUwcdIewqD+zj8Q+H2ySc+x73oVDFZWjW6BVhqt/9vNtOK2l8Sh8u/xnRimYU AwI6R66mT970BPp9oM7XfJP8s43/iVEUG+y4i0V2YsYAgSm1+WQ86DHQ+5x2MBM39yMz 42Xw== 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; bh=cVjpn61le3LO/pEjxA8R7dNQPZJZTAfKDCMhmqNZy3A=; b=tq+cgTO9PicjTjQOMLSX+0TdMIYjTfuDM5uqcItJTVT0vL7LeXxBFDAvYqPyakHmYh 7uTYb2jQ0tfcs5oM3TKrzQbK6W1YmJDqjPwvEFhVjIDoQQ+iWix/m6zDrdMdjDmfQRXS sl19ZXCX8v7tGH1Qku3V7sN37PP9ozvWU/EIcitQgtFej0mGm1jjd9Gh6hqw1Ev7Idzs QQDqlVOWZCLDaw8JegWOCglsdvpxeGAzd2mzij1Pldu4w6O6r33YwFOcoCvT2LYZu3Nd cHofWfMFIXL7Vi+DGFtLgwr/QGTlduxq34/v/lvuwKVurtgD4Op2hBJ5bi3ZxmlnEH5C 7UhQ== X-Gm-Message-State: ACgBeo1TSTJNLxpRooSGbUbXcVjNLOY7TkUaQI3BUu4BY7JX/P8qUron Rpd/aRuSUv5rp0xnr8eHutqEYHxC52akpw== X-Google-Smtp-Source: AA6agR6dwBOwNc2eUAd7xrvWw5Ntr/mXXU9XbxqQOzNPbwYcYA5h3EGPsmJcovRl3D2Hfg1pGUvbhw== X-Received: by 2002:a17:90b:2250:b0:202:adf7:dd6d with SMTP id hk16-20020a17090b225000b00202adf7dd6dmr11315809pjb.208.1662992406479; Mon, 12 Sep 2022 07:20:06 -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 u1-20020a17090341c100b00176ba091cd3sm2595406ple.196.2022.09.12.07.20.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Sep 2022 07:20:06 -0700 (PDT) From: Zhang Boyang To: grub-devel@gnu.org Cc: jim945@mail.ru, glin@suse.com, dkiper@net-space.pl, dja@axtens.net, ps@pks.im, droidbittin@gmail.com, heinrich.schuchardt@canonical.com, langner.marcel@myiq.de, marcan@marcan.st, Zhang Boyang Subject: [PATCH 1/1] mm: Better handling of adding new regions Date: Mon, 12 Sep 2022 22:19:35 +0800 Message-Id: <20220912141935.63344-2-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220912141935.63344-1-zhangboyang.id@gmail.com> References: <20220912141935.63344-1-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::633; envelope-from=zhangboyang.id@gmail.com; helo=mail-pl1-x633.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, T_SCC_BODY_TEXT_LINE=-0.01 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: Mon, 12 Sep 2022 14:20:16 -0000 The code of dynamically adding new regions has two problems. First, it always invalidate disk caches, which decreases performance severely. Second, it request exactly "size" bytes for new region, ignoring region overheads. This patch makes adding new regions more priority than disk cache invalidating. This patch also use "size + 1MB" as the size of new region. This value can address the region overheads, and it can also improve performance of small allocations when default heap is full. Fixes: 887f98f0db43 (mm: Allow dynamically requesting additional memory regions) Signed-off-by: Zhang Boyang --- grub-core/kern/mm.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index 75f6eacbe..e998454f8 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -410,6 +410,7 @@ 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 rsize; int count = 0; if (!grub_mm_base) @@ -423,6 +424,7 @@ grub_memalign (grub_size_t align, grub_size_t size) in name of sanity is beneficial. */ if ((size + align) > ~(grub_size_t) 0x100000) goto fail; + rsize = size + 0x100000; align = (align >> GRUB_MM_ALIGN_LOG2); if (align == 0) @@ -443,22 +445,16 @@ grub_memalign (grub_size_t align, grub_size_t size) switch (count) { case 0: - /* Invalidate disk caches. */ - grub_disk_cache_invalidate_all (); - count++; - goto again; - - case 1: /* 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 (rsize, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE) goto again; /* fallthrough */ - case 2: + case 1: /* Request additional pages, anything at all */ count++; @@ -468,12 +464,18 @@ 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 (rsize, GRUB_MM_ADD_REGION_NONE); goto again; } /* fallthrough */ + case 2: + /* Invalidate disk caches. */ + grub_disk_cache_invalidate_all (); + count++; + goto again; + default: break; } -- 2.30.2