Re: [PATCH 2/5] efi/x86: Implement support for unaccepted memory

Dave Hansen
2021-08-12T21:43:28Z

On 8/12/21 2:14 PM, Kirill A. Shutemov wrote:
> On Tue, Aug 10, 2021 at 10:50:33AM -0700, Dave Hansen wrote:
>>> + if ((start & PMD_MASK) == (end & PMD_MASK)) {
>>> + npages = (end - start) / PAGE_SIZE;
>>> + __accept_memory(start, start + npages * PAGE_SIZE);
>>> + return;
>>> + }
>>
>> Hmm, is it possible to have this case hit, but neither of the two below
>> cases? This seems to be looking for a case where the range is somehow
>> entirely contained in one PMD_SIZE area, but where it doesn't consume a
>> whole area.
>>
>> Wouldn't that mean that 'start' or 'end' must be unaligned?
>
> The problem is that if both of them unaligned round_up() and round_down()
> in the cases below would step outside the requested range.

Ahh, got it.

You might want to add some comments like:

/* Immediately accept whole thing if within a PMD_SIZE block: */

/* Immediately accept a <PMD_SIZE piece at the start: */

/* Immediately accept a <PMD_SIZE piece at the end: */

/* Mark full PMD_SIZE areas so they can be accepted later */

To the three if statements and the bitmap_set().

After looking at this, I do think you probably did this the simplest way
possible. It just needs a little help.

>>> + if (start & ~PMD_MASK) {
>>> + npages = (round_up(start, PMD_SIZE) - start) / PAGE_SIZE;
>>> + __accept_memory(start, start + npages * PAGE_SIZE);
>>> + start = round_up(start, PMD_SIZE);
>>> + }
>>> +
>>> + if (end & ~PMD_MASK) {
>>> + npages = (end - round_down(end, PMD_SIZE)) / PAGE_SIZE;
>>> + end = round_down(end, PMD_SIZE);
>>> + __accept_memory(end, end + npages * PAGE_SIZE);
>>> + }
>>> + npages = (end - start) / PMD_SIZE;
>>> + bitmap_set((unsigned long *)params->unaccepted_memory,
>>> + start / PMD_SIZE, npages);
>>> +}

One note as I'm looking at this again: 'npages' can be 0. Imagine if
you had an 8k region that started with the last 4k page of a 2M area and
ended on the first 4k page of the next 2M area, like 0x1ff000->0x201000.

I think it's harmless and bitmap_set() seems to handle it correctly.
But, it's probably worth a comment because it's not obvious.