Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pM6C4-0003Vr-Gs for mharc-grub-devel@gnu.org; Sun, 29 Jan 2023 06:49:56 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pM6C3-0003VR-61 for grub-devel@gnu.org; Sun, 29 Jan 2023 06:49:55 -0500 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pM6C1-0002Dl-Hs for grub-devel@gnu.org; Sun, 29 Jan 2023 06:49:54 -0500 Received: by mail-pl1-x630.google.com with SMTP id jl3so8977103plb.8 for ; Sun, 29 Jan 2023 03:49:53 -0800 (PST) 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=XucNtRXwSY/qu/5D3bf4wmdfAjpC85NBqCMpm3zN/lg=; b=jpc7ycxNh9bylvL4WxJ6AZVcVo5NxTMgLHuztrA7Hk4kU57v47CChhhabsFgeFAFtS srKB7zcX9Igbg/T9LZo2jrCTU7EXRs0ByzFq6tL53xgqGDCuupAPcJSUv3nLWU1BsMbb 5RxzIULkr400a9pFa5s2pAPuGwCmrRepU+UNyXZP/p0svhzJT1s88j55I2+708UB3jUS eLl07JqbQqiaC6wF76La9irRK18fOOro6T0EtKbLHmxIw7f5bLp3J67ukbBssTi2e5HA F1JKF47HulCXiYH4wPq/HMJha5PG4YD3a4yOTcvYlZGspRN92TNPJ0L60xvk1+fF75g9 wMkA== 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=XucNtRXwSY/qu/5D3bf4wmdfAjpC85NBqCMpm3zN/lg=; b=ruTSoEgPF3n5hMxPRbBHynLCXdBzRZz4SM/TPlpwZKZqt8+FKAyoMXIJk1NPJK/DHl vltfUDmQXIMmbID17+OB+mtyJ8QAVSbqHNKgtV+6WrbjXlUX6MFSjtTCHzAHQrr16wyv t/yBcdTzxTTuNmNfr7ZN2JLQ+ujAkcFvaqIH515E3OZ8at9OC5Ra8PwpD+dxUu45Yon0 8zXwZGAF7EaNnV2JJ55R04tX5uxUpjf3fb3iWYz81kQ0QZZef1vp+bgmjxuc9E4fedXE GZDN4OB9rxYrXN8sYDFGyEqfLwIYgbOjVj370BYspZSqlsfLH7Tl5pwypQySZM041is+ bPdA== X-Gm-Message-State: AO0yUKUDpgvxO/PfFsST1G/o0iqT8w2d9AeLs11D015+yZNf2osa0APV q9DW1dF3bJ+yUjjN6cB/kNkX+mHDvg8= X-Google-Smtp-Source: AK7set9XogSUfjpVfB1abyAdOTX+D8X9nzsF7MexeOqJFabO6gJCLbeW6tDtgBCY7Pt3m54itOfzTQ== X-Received: by 2002:a05:6a20:8429:b0:bc:42f9:6b21 with SMTP id c41-20020a056a20842900b000bc42f96b21mr11360488pzd.15.1674992992254; Sun, 29 Jan 2023 03:49:52 -0800 (PST) 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 md24-20020a17090b23d800b0022c1fa729e6sm6019685pjb.7.2023.01.29.03.49.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Jan 2023 03:49:51 -0800 (PST) From: Zhang Boyang To: grub-devel@gnu.org Cc: Zhang Boyang Subject: [PATCH v5 3/3] mm: Avoid complex heap growth math in hot path Date: Sun, 29 Jan 2023 19:49:33 +0800 Message-Id: <20230129114933.192259-4-zhangboyang.id@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230129114933.192259-1-zhangboyang.id@gmail.com> References: <20230129114933.192259-1-zhangboyang.id@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=zhangboyang.id@gmail.com; helo=mail-pl1-x630.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: Sun, 29 Jan 2023 11:49:55 -0000 We do a lot of math about heap growth in hot path of grub_memalign(). However, the result is only used if out of memory is encountered, which is seldom. This patch moves these calculations away from hot path. These calculations is now only done if out of memory is encountered. This change can also help compiler to optimize interger overflow checks away. Signed-off-by: Zhang Boyang --- grub-core/kern/mm.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c index c3d96e2b0..94eb29108 100644 --- a/grub-core/kern/mm.c +++ b/grub-core/kern/mm.c @@ -467,20 +467,7 @@ grub_memalign (grub_size_t align, grub_size_t size) if (size > ~(grub_size_t) align) goto fail; - /* - * Pre-calculate the necessary size of heap growth (if applicable), - * with region management overhead taken into account. - */ - if (grub_add (size + align, GRUB_MM_MGMT_OVERHEAD, &grow)) - goto fail; - - /* Preallocate some extra space if heap growth is small. */ - grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA); - - /* Align up heap growth to make it friendly to CPU/MMU. */ - if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1)) - goto fail; - grow = ALIGN_UP (grow, GRUB_MM_HEAP_GROW_ALIGN); + grow = size + align; /* We currently assume at least a 32-bit grub_size_t, so limiting allocations to - 1MiB @@ -510,6 +497,25 @@ grub_memalign (grub_size_t align, grub_size_t size) /* Request additional pages, contiguous */ count++; + /* + * Calculate the necessary size of heap growth (if applicable), + * with region management overhead taken into account. + */ + if (grub_add (grow, GRUB_MM_MGMT_OVERHEAD, &grow)) + goto fail; + + /* Preallocate some extra space if heap growth is small. */ + grow = grub_max (grow, GRUB_MM_HEAP_GROW_EXTRA); + + /* Align up heap growth to make it friendly to CPU/MMU. */ + if (grow > ~(grub_size_t) (GRUB_MM_HEAP_GROW_ALIGN - 1)) + goto fail; + grow = ALIGN_UP (grow, GRUB_MM_HEAP_GROW_ALIGN); + + /* Do the same sanity check again. */ + if (grow > ~(grub_size_t) 0x100000) + goto fail; + if (grub_mm_add_region_fn != NULL && grub_mm_add_region_fn (grow, GRUB_MM_ADD_REGION_CONSECUTIVE) == GRUB_ERR_NONE) goto again; -- 2.30.2