Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oXm9C-0002it-Fd for mharc-grub-devel@gnu.org; Mon, 12 Sep 2022 12:18:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57044) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oXm99-0002ib-2A for grub-devel@gnu.org; Mon, 12 Sep 2022 12:18:55 -0400 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]:38469) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oXm93-00036B-2i for grub-devel@gnu.org; Mon, 12 Sep 2022 12:18:54 -0400 Received: by mail-pj1-x1035.google.com with SMTP id x1-20020a17090ab00100b001fda21bbc90so12837901pjq.3 for ; Mon, 12 Sep 2022 09:18:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date; bh=jz1kZvrNw7d7mud0dtC0WBR1GsgO0utSS4C4EClPBS4=; b=IxxYhlqTIdzeS2st57GjylD0TB8l4Uo+YhSVIx2m6xWkVHphLcKcBKoWImn+XXHLUd 9pRzAbQEzMiONAduOCEgx6sWYxykn7p8dsnUSsOPcTnA4wpGqQXYSbitjX07eUvKXuht 01zeq4z0vUz9ZmRkEWgMWp2NoISPkMfJHSW1TEiMAS8Y5IKuyKLmBjyYcEq4XQqxwo29 /3J5J5rxlg0NM33S3DSIHFURKxoBL26vj9HvFCjLLueakJEx+OzzOG+pdC8IvSetxyLR wwsW5ZuGg+mR9Jaee4QsDm8v23L9U/ZGyQKK9EsN30ouSIPZfz0Zs4AV+05EORfcbvS9 JE9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=jz1kZvrNw7d7mud0dtC0WBR1GsgO0utSS4C4EClPBS4=; b=e3nnSG6HW3NYxlYdR7Mq6ouytDE+eKQ1UFxaa777UcCbzyckTXtC0XH8I4O7P4YzYD VFXrjHM7uy+2HsxHVGsb32e3OqCgYKQY5ImcbLAI6wTvDYkBsv0RvvNBGhEvc1hfa63g +sFQk9TwsRbZ8woVFx73gTP6ljU6wFPw0LNsGAA7qWFgA6eLo5hUtScO4lGjCox+jntk SdYjAFEoaOYzUk7FznAhDTePr/DtPvfwb1lsfWBozdcIx5uW8/ckt2xis4+TFhoXJ6kZ pmX2cef7XTN0FSN2xYo7AT/vvFdHFh6vKUcyRGuqkfdVPlYluxVlyAbzs1pa3rrY3tem eiLw== X-Gm-Message-State: ACgBeo1xM04C+FyQb/Fl9JyRM3kVaKrypYnDSpfnhFgROGCCRxiK2dcS RgkG8SKPq+hv+kz60H1Bl9I= X-Google-Smtp-Source: AA6agR7DIZsHIt98ZB95nlH+vBgMNNrF3r9A837CJKozdYAbUT2CjheZs26bIIli0T8RSX+3R9sYWg== X-Received: by 2002:a17:902:7c8f:b0:176:cdd8:7258 with SMTP id y15-20020a1709027c8f00b00176cdd87258mr26818807pll.49.1662999526621; Mon, 12 Sep 2022 09:18:46 -0700 (PDT) Received: from [0.0.0.0] (ec2-13-113-80-70.ap-northeast-1.compute.amazonaws.com. [13.113.80.70]) by smtp.gmail.com with ESMTPSA id n12-20020a17090ac68c00b002009c05b802sm5460554pjt.2.2022.09.12.09.18.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Sep 2022 09:18:46 -0700 (PDT) Message-ID: <109497d0-9076-e613-5d79-53696bcc18ea@gmail.com> Date: Tue, 13 Sep 2022 00:18:38 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [PATCH 1/1] mm: Better handling of adding new regions Content-Language: en-US To: Heinrich Schuchardt Cc: jim945@mail.ru, glin@suse.com, dkiper@net-space.pl, dja@axtens.net, ps@pks.im, droidbittin@gmail.com, langner.marcel@myiq.de, marcan@marcan.st, grub-devel@gnu.org References: <20220912141935.63344-1-zhangboyang.id@gmail.com> <20220912141935.63344-2-zhangboyang.id@gmail.com> From: Zhang Boyang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1035; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x1035.google.com X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 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, NICE_REPLY_A=-2.153, 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 16:18:55 -0000 Hi, On 2022/9/12 23:03, Heinrich Schuchardt wrote: > On 9/12/22 16:19, Zhang Boyang wrote: >> 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; > > rsize should be multiple of the EFI page size (4 KiB) as we call > AllocatPages(). > grub_mm_add_region_fn() can deal with page alignment, so rsize needn't to be page aligned. > For size << 1 MiB I see the point. > But does it make sense to allocate another 1 MiB if size >= 1 MiB? > There are costs for region management. I didn't investigated the details, but I think 1MiB is enough. The alignment is also another consideration, it also need extra padding. I think I should probably set "rsize" to "size + align + 0x100000". Patch V2 on the way... > Best regards > > Heinrich > >>     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; >>       } >