Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1orxmt-0002jd-FJ for mharc-grub-devel@gnu.org; Mon, 07 Nov 2022 03:47:23 -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 1orxmj-0002jF-UJ for grub-devel@gnu.org; Mon, 07 Nov 2022 03:47:22 -0500 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1orxmi-000580-0F for grub-devel@gnu.org; Mon, 07 Nov 2022 03:47:13 -0500 Received: by mail-pl1-x631.google.com with SMTP id j12so10443322plj.5 for ; Mon, 07 Nov 2022 00:47:11 -0800 (PST) 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:message-id:reply-to; bh=UWOBn0aRYY5RcqyfRziXkOmRqvCPWFKuznvygY+Zc9A=; b=TfOO5GH2Ui3f/MIqIluU8OLPa0OeRYNzyIzvDUv0f3x1oKvM9Sgn0/lwvtdrkoCFwx de4JnMEroavDxWjCL2bjbkgqp9tFB7Lj/QsepjsQT50BW7RoY2tFnIU4iSklECOO8Yx/ CnN/Ha8/+8an8e/sAdZQr3eO/3y+4CAtovqmJ4kQeRSZjrxZ+3sS90MyBiV7d2LBBjAd XlqZd+7hCp+VS4XZWsmUVKnOlN6vgEFsXRwPUvCU0cqz6epDkMzUWapvworBD3lARmwB i2PWJQlR5ZcpwdyxJZMmdphYMEH4EErujnQbzJ8oe8WhTGfCHZ8Gmxecv6kvB8AKMR4L BN/w== 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:message-id:reply-to; bh=UWOBn0aRYY5RcqyfRziXkOmRqvCPWFKuznvygY+Zc9A=; b=EiRqboPQX4xiBI5j4vIkfnom+Vq0g/emaU++DkbPu0ET0Mo7AAXGFfKzOfzQ7GqqRb nSj6CrDmpL0M1UfKh1BouEXtKJLcLKXw8UzYdW6CzL0IhgPszPMwQd0G2ZiGtn+TNnRO 5eJXyP4f2lUncKokRA5mx5bdxHQWzGCjoTM5BlHPm+/etv35a2AMv03S5X1725VY/Q1o n6NFVtv5LHplPsSMV7fbRV7MJk93MMrwj8okn1rrgZEvXo0Pcrr+4f9Lr3NrDfMgoCO9 MZhuMdK4skhg5wROj20dfvFeDELSr4m0vxbUTkCUWaBmusF0zzjjwIb0CE5lGdhMsTQ2 AJQQ== X-Gm-Message-State: ACrzQf3fCLKuN4lXFSL3crD5vku/MJMFM3d1nRJ6ly0AQ8N0xI3gDDJJ U91BPT4YVJmrz8lCbE5W1doJzZqsHgPOEg== X-Google-Smtp-Source: AMsMyM4vhj8u6W2QPgYEv/fsydvI/DxZS0LBp/K5yFQU/8qri9j8fibp07cP5sbmBxDvQVNhtAqd3w== X-Received: by 2002:a17:902:d1d1:b0:17f:8544:e0dd with SMTP id g17-20020a170902d1d100b0017f8544e0ddmr798980plb.34.1667810830434; Mon, 07 Nov 2022 00:47:10 -0800 (PST) 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 83-20020a621456000000b0056b6c7a17c6sm4058861pfu.12.2022.11.07.00.47.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Nov 2022 00:47:10 -0800 (PST) Message-ID: Date: Mon, 7 Nov 2022 16:47:04 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v4 2/2] mm: Better handling of adding new regions Content-Language: en-US To: grub-devel@gnu.org, Daniel Kiper Cc: 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 References: <20221015141512.72193-1-zhangboyang.id@gmail.com> <20221015141512.72193-3-zhangboyang.id@gmail.com> <20221020125010.7vyjn5qynl7tgol3@tomti.i.net-space.pl> From: Zhang Boyang In-Reply-To: <20221020125010.7vyjn5qynl7tgol3@tomti.i.net-space.pl> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=2607:f8b0:4864:20::631; envelope-from=zhangboyang.id@gmail.com; helo=mail-pl1-x631.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, NICE_REPLY_A=-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: Mon, 07 Nov 2022 08:47:22 -0000 Hi, On 2022/10/20 20:50, Daniel Kiper wrote: > On Sat, Oct 15, 2022 at 10:15:12PM +0800, Zhang Boyang wrote: >> Previously, there are two problems of the handling of adding new > > Please drop "Previously". It is not needed... > >> 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 should be separate patch... > >> 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. > > ... and this should be the second one, to be precise third patch in the series... > Will be fixed in next version. >> 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); > > Missing space before "(". > >> + >> + /* Adjust heap growth to address the cost of region management. */ >> + if (grub_add(grow, GRUB_MM_MAX_COST, &grow)) > > Ditto... > >> + 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. > > OK... > >> + * 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 > > ... but why do not you use sizeof() here then? There are complex alignments which are hard to predict (although it's indeed possible). These alignments are determined by not only "size" but also "addr". Calculating a tight upper bound value will be complex and error prone. So I think it's better to give a sufficiently large value, so complex calculations are avoided. > >> + >> +/* Minimal heap growth granularity, when existing heap space is exhausted. */ >> +#define GRUB_MM_HEAP_GROW 0x100000 > > It would be nice if you shortly explain in the comment before why you > come up with this value. > It was chosen almost arbitrarily. However, if this value is too small, the cost of small memory allocations will be expensive (more calls to grub_mm_add_region_fn(), which might be slow). If this value is too large, a lot of memory will be wasted and it might cause out-of-memory on machines with small amount of RAM. > Daniel Best Regards, Zhang Boyang