Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p5m8X-000506-Q6 for mharc-grub-devel@gnu.org; Thu, 15 Dec 2022 06:10:50 -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 1p5m8W-0004zu-25 for grub-devel@gnu.org; Thu, 15 Dec 2022 06:10:48 -0500 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p5m8U-0006gR-2L for grub-devel@gnu.org; Thu, 15 Dec 2022 06:10:47 -0500 Received: by mail-pj1-x102a.google.com with SMTP id o12so10093767pjo.4 for ; Thu, 15 Dec 2022 03:10:45 -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=9Ui24uQNSsBk6GqbMneWsuFaxrbcmqtMGgmXpLihQOc=; b=oZibooAb02tf8WMV1ZGIHM5VphUT6YOurpDDJZ3SlaNO/6i0O8oM+6EKGoph3aUAR7 ylKax0RgsHb7L25OqqKB0vjADkfP5RyL2zhu9UeDaWwn8i+QWekQOVUy5OQDKGovGtP4 OdHSLFoBP2WEmouRpeowPO37PHdcwRlurBJYakXSb7M6H0HRyhb12gyNvipImb1qMPM0 Q5ePtmC27rxiwfwMyBdsCSXPc1UrSKRDiKYXCEwgwnX/seSpc/EtXYNItUaTcKC/CCxS 3T+GFrLlY8vu3c6BxObZ5L8AZc6FrKeXe63VstNWoSNa3a1GpswO3SmD8w+uxed1OJrU eMRg== 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=9Ui24uQNSsBk6GqbMneWsuFaxrbcmqtMGgmXpLihQOc=; b=rqmStXk14E2c2CoI0dpE7h7yygjW0iHWq9PnRW2NZLIzKCOFc1pNj4H0PBqdwcHWt1 lEMi2gZZikAkt6mPwYGiT46kblKJlpupkZqYLxO2eTO819aSK6Z925Gq/q2u0hnrfwBZ 4MAtgsxoXA5Ow4H47I97Scx1mH8ojvGZ4B4MYzlQTZmp+hj6Jc+NUPeHS9Yb5Nfp4NRi u1nnZQzTBn6s4lpfaiclzHqWaTsrJeCWHeS4oKWe1UacN0B9W4ylILZi05DaOJvtcEJj YR7ks8hBPdQyCC1rx1EtTvyYgG3/sCxBQoAsZE1FAroXmL/O9f6J/Ol430xYHWGJN9a5 PDCQ== X-Gm-Message-State: ANoB5pmO/+/P1sVB8or+slJIeMw8nlQRou27bs0Q0iFxl+MePb30jKCj 1SfHIs94Ayu0m8iysy8VAyCwd2rw8Z8= X-Google-Smtp-Source: AA0mqf5999iUiI+iHdwNwosu+7sM/o3BMp31omUml05bty29Iqfr3u8oU5tcfdAQCRZwGf5zU3euLA== X-Received: by 2002:a05:6a20:bf02:b0:af:1a39:553d with SMTP id gc2-20020a056a20bf0200b000af1a39553dmr6555436pzb.14.1671102643994; Thu, 15 Dec 2022 03:10:43 -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 r3-20020aa79ec3000000b00576cc60e5c9sm1478277pfq.87.2022.12.15.03.10.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Dec 2022 03:10:43 -0800 (PST) Message-ID: <78b71224-5956-e806-2638-4f66dca99d08@gmail.com> Date: Thu, 15 Dec 2022 19:10:41 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 Subject: Re: [PATCH 1/2] mm: Adjust new region size to take management cost into account Content-Language: en-US To: Daniel Kiper Cc: grub-devel@gnu.org References: <20221129141734.123708-1-zhangboyang.id@gmail.com> <20221129141734.123708-2-zhangboyang.id@gmail.com> <20221213134812.4g2guzy57edkx6ra@tomti.i.net-space.pl> From: Zhang Boyang In-Reply-To: <20221213134812.4g2guzy57edkx6ra@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::102a; envelope-from=zhangboyang.id@gmail.com; helo=mail-pj1-x102a.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: Thu, 15 Dec 2022 11:10:48 -0000 Hi, On 2022/12/13 21:48, Daniel Kiper wrote: > On Tue, Nov 29, 2022 at 10:17:33PM +0800, Zhang Boyang wrote: >> When grub_memalign() encounters out-of-memory, it will try >> grub_mm_add_region_fn() to request more memory from system firmware. >> However, the size passed to it doesn't take region management cost into > > s/cost/overhead/? If yes then please fix everything below too... > >> account. Adding a memory area of `size` bytes will result in a heap > > s/`/"/ Same below if needed... > >> region of less than `size` bytes truely avaliable. Thus, the new region >> might not adequate for current allocation request, confusing > > s/might not/may not be/ > >> out-of-memory handling code. >> >> This patch introduces GRUB_MM_MAX_COST to address the region management > > s/GRUB_MM_MAX_COST/GRUB_MM_MGMT_OVERHEAD/? > >> cost (e.g. metadata, alignment). The value of this new constant should >> make `grub_malloc (size)` always success after a successful call to > > s/`grub_malloc (size)`/grub_malloc(size)/ > >> `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`. > > s/`grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`/grub_mm_init_region(addr, size + GRUB_MM_MAX_COST)/ > Fixed in v2. >> The new region size is now set to `size + GRUB_MM_MAX_COST`, thus if >> grub_mm_add_region_fn() succeeded, current allocation request can always >> success. >> >> Signed-off-by: Zhang Boyang >> --- >> grub-core/kern/mm.c | 15 ++++++++++++--- >> include/grub/mm_private.h | 9 +++++++++ >> 2 files changed, 21 insertions(+), 3 deletions(-) >> >> diff --git a/grub-core/kern/mm.c b/grub-core/kern/mm.c >> index ae2279133..973cb6b15 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 bound; >> int count = 0; >> + grub_size_t grow; > > grub_size_t bound, 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; >> >> + /* If largest free chunk in region >= bound, allocation can always success */ > > I am not sure I understand. Could you expand this comment a bit? > Fixed. Please see v2 for new comment. >> + bound = size + align; > > I think you should use grub_add() here too... The overflow is already checked in "if (size > ~(grub_size_t) align) goto fail;". I can rewrote this check using grub_add() if necessary. > > And doing this you change the allocation behavior below. In original > algorithm we allocate only size bytes below. I think this should > not be changed. > In fact, there is no change to algorithm. I just want to reuse the expression "size + align" (because it passes overflow check), so I give it the name "bound". >> /* 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 (bound > ~(grub_size_t) 0x100000) > > s/bound/limit/? > I think "bound" is more suitable. Because that value is a upper bound (an overestimate) of how much new memory we need. >> goto fail; >> >> align = (align >> GRUB_MM_ALIGN_LOG2); >> @@ -443,11 +448,15 @@ grub_memalign (grub_size_t align, grub_size_t size) >> switch (count) >> { >> case 0: >> + /* Adjust heap growth to address the region management cost. */ >> + if (grub_add (bound, GRUB_MM_MAX_COST, &grow)) > > I think this should happen before limit check above. > Fixed in v2. >> + 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 +471,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..f212110e4 100644 >> --- a/include/grub/mm_private.h >> +++ b/include/grub/mm_private.h >> @@ -95,6 +95,15 @@ 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_malloc (size)` always success after a successful >> + * call to `grub_mm_init_region (addr, size + GRUB_MM_MAX_COST)`. >> + */ >> +#define GRUB_MM_MAX_COST 0x1000 >> + > > I would define this variable in the following way even if the result > is the same: > > #define GRUB_MM_MGMT_OVERHEAD ALIGN_UP (ALIGN_UP (sizeof (*grub_mm_region_t), GRUB_MM_ALIGN) + > 10 * ALIGN_UP (sizeof (*grub_mm_header), GRUB_MM_ALIGN), 0x1000) > > This way it will be more readable what is happening here, i.e. size of > header of memory region plus size of 10, this is quite arbitrary figure, > memory block headers aligned to page size (it would be nice to have > a constant here but it would require more work to make it happen for > all archs; so, let's use a number for this for time being). It would > be nice to have relevant comment before GRUB_MM_MGMT_OVERHEAD > definition too. > > And probably it can be defined in grub-core/kern/mm.c instead of > include/grub/mm_private.h. > Please see v2 for new implementation. > Daniel Best Regards, Zhang Boyang