linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: Lance Yang <ioworker0@gmail.com>
Cc: akpm@linux-foundation.org, ryan.roberts@arm.com,
	21cnbao@gmail.com, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] mm/memory: Fix boundary check for next PFN in folio_pte_batch()
Date: Tue, 27 Feb 2024 09:33:39 +0100	[thread overview]
Message-ID: <2aa8b0f0-4fd4-469a-ba72-82fe01d37f15@redhat.com> (raw)
In-Reply-To: <CAK1f24nr=giiYW2CVtbpuUxNshb9ksLhtY_v-SK1_AVvQPUEdQ@mail.gmail.com>

On 27.02.24 09:23, Lance Yang wrote:
> Hey David,
> 
> Thanks for taking time to review!
> 
> On Tue, Feb 27, 2024 at 3:30 PM David Hildenbrand <david@redhat.com> wrote:
>>
>> On 27.02.24 08:04, Lance Yang wrote:
>>> Previously, in folio_pte_batch(), only the upper boundary of the
>>> folio was checked using '>=' for comparison. This led to
>>> incorrect behavior when the next PFN exceeded the lower boundary
>>> of the folio, especially in corner cases where the next PFN might
>>> fall into a different folio.
>>
>> Which commit does this fix?
>>
>> The introducing commit (f8d937761d65c87e9987b88ea7beb7bddc333a0e) is
>> already in mm-stable, so we would need a Fixes: tag. Unless, Ryan's
>> changes introduced a problem.
>>
>> BUT
>>
>> I don't see what is broken. :)
>>
>> Can you please give an example/reproducer?
> 
> For example1:
> 
> PTE0 is present for large folio1.
> PTE1 is present for large folio1.
> PTE2 is present for large folio1.
> PTE3 is present for large folio1.
> 
> folio_nr_pages(folio1) is 4.
> folio_nr_pages(folio2) is 4.
> 
> pte = *start_ptep = PTE0;
> max_nr = folio_nr_pages(folio2);
> 
> If folio_pfn(folio1) < folio_pfn(folio2),
> the return value of folio_pte_batch(folio2, start_ptep, pte, max_nr)
> will be 4(Actually it should be 0).
> 
> For example2:
> 
> PTE0 is present for large folio2.
> PTE1 is present for large folio1.
> PTE2 is present for large folio1.
> PTE3 is present for large folio1.
> 
> folio_nr_pages(folio1) is 4.
> folio_nr_pages(folio2) is 4.
> 
> pte = *start_ptep = PTE0;
> max_nr = folio_nr_pages(folio1);
> 

In both cases, start_ptep does not map the folio.

It's a BUG in your caller unless I am missing something important.


> If max_nr=4, the return value of folio_pte_batch(folio1, start_ptep,
> pte, max_nr)
> will be 1(Actually it should be 0).
> 
> folio_pte_batch() will soon be exported, and IMO, these corner cases may need
> to be handled.

No, you should fix your caller.

The function cannot possibly do something reasonable if start_ptep does 
not map the folio.

nr = pte_batch_hint(start_ptep, pte);
...
ptep = start_ptep + nr; /* nr is >= 1 */
...
return min(ptep - start_ptep, max_nr); /* will return something > 0 */

Which would return > 0 for something that does not map that folio.


I was trying to avoid official kernel docs for this internal helper, 
maybe we have to improve it now.

-- 
Cheers,

David / dhildenb



  reply	other threads:[~2024-02-27  8:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-27  7:04 Lance Yang
2024-02-27  7:30 ` David Hildenbrand
2024-02-27  8:23   ` Lance Yang
2024-02-27  8:33     ` David Hildenbrand [this message]
2024-02-27  8:45       ` Lance Yang
2024-02-27  8:46         ` David Hildenbrand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2aa8b0f0-4fd4-469a-ba72-82fe01d37f15@redhat.com \
    --to=david@redhat.com \
    --cc=21cnbao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=ioworker0@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ryan.roberts@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox