Re: [PATCH v1 05/18] mm: improve folio_likely_mapped_shared() using the mapcount of large folios

2024-04-16 Thread Lance Yang
Hey David,

Maybe I spotted a bug below.

[...]
 static inline bool folio_likely_mapped_shared(struct folio *folio)
 {
-   return page_mapcount(folio_page(folio, 0)) > 1;
+   int mapcount = folio_mapcount(folio);
+
+   /* Only partially-mappable folios require more care. */
+   if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio)))
+   return mapcount > 1;
+
+   /* A single mapping implies "mapped exclusively". */
+   if (mapcount <= 1)
+   return false;
+
+   /* If any page is mapped more than once we treat it "mapped shared". */
+   if (folio_entire_mapcount(folio) || mapcount > folio_nr_pages(folio))
+   return true;

bug: if a PMD-mapped THP is exclusively mapped, the folio_entire_mapcount()
function will return 1 (atomic_read(&folio->_entire_mapcount) + 1).

IIUC, when mapping a PMD entry for the entire THP, folio->_entire_mapcount
increments from -1 to 0.

Thanks,
Lance

+
+   /* Let's guess based on the first subpage. */
+   return atomic_read(&folio->_mapcount) > 0;
 }
[...]



Re: [PATCH v1 05/18] mm: improve folio_likely_mapped_shared() using the mapcount of large folios

2024-04-16 Thread David Hildenbrand

On 16.04.24 12:40, Lance Yang wrote:

Hey David,

Maybe I spotted a bug below.


Thanks for the review!



[...]
  static inline bool folio_likely_mapped_shared(struct folio *folio)
  {
-   return page_mapcount(folio_page(folio, 0)) > 1;
+   int mapcount = folio_mapcount(folio);
+
+   /* Only partially-mappable folios require more care. */
+   if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio)))
+   return mapcount > 1;
+
+   /* A single mapping implies "mapped exclusively". */
+   if (mapcount <= 1)
+   return false;
+
+   /* If any page is mapped more than once we treat it "mapped shared". */
+   if (folio_entire_mapcount(folio) || mapcount > folio_nr_pages(folio))
+   return true;

bug: if a PMD-mapped THP is exclusively mapped, the folio_entire_mapcount()
function will return 1 (atomic_read(&folio->_entire_mapcount) + 1).


If it's exclusively mapped, then folio_mapcount(folio)==1. In which case 
the previous statement:


if (mapcount <= 1)
return false;

Catches it.

IOW, once we reach this point we now that folio_mapcount(folio) > 1, and 
there must be something else besides the entire mapping ("more than once").



Or did I not address your concern?

--
Cheers,

David / dhildenb




Re: [PATCH v1 05/18] mm: improve folio_likely_mapped_shared() using the mapcount of large folios

2024-04-16 Thread Lance Yang
On Tue, Apr 16, 2024 at 6:47 PM David Hildenbrand  wrote:
>
> On 16.04.24 12:40, Lance Yang wrote:
> > Hey David,
> >
> > Maybe I spotted a bug below.
>
> Thanks for the review!
>
> >
> > [...]
> >   static inline bool folio_likely_mapped_shared(struct folio *folio)
> >   {
> > - return page_mapcount(folio_page(folio, 0)) > 1;
> > + int mapcount = folio_mapcount(folio);
> > +
> > + /* Only partially-mappable folios require more care. */
> > + if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio)))
> > + return mapcount > 1;
> > +
> > + /* A single mapping implies "mapped exclusively". */
> > + if (mapcount <= 1)
> > + return false;
> > +
> > + /* If any page is mapped more than once we treat it "mapped shared". 
> > */
> > + if (folio_entire_mapcount(folio) || mapcount > folio_nr_pages(folio))
> > + return true;
> >
> > bug: if a PMD-mapped THP is exclusively mapped, the folio_entire_mapcount()
> > function will return 1 (atomic_read(&folio->_entire_mapcount) + 1).
>
> If it's exclusively mapped, then folio_mapcount(folio)==1. In which case
> the previous statement:
>
> if (mapcount <= 1)
> return false;
>
> Catches it.

You're right!

>
> IOW, once we reach this point we now that folio_mapcount(folio) > 1, and
> there must be something else besides the entire mapping ("more than once").
>
>
> Or did I not address your concern?

Sorry, my mistake :(

Thanks,
Lance

>
> --
> Cheers,
>
> David / dhildenb
>



Re: [PATCH v1 05/18] mm: improve folio_likely_mapped_shared() using the mapcount of large folios

2024-04-16 Thread David Hildenbrand

On 16.04.24 12:52, Lance Yang wrote:

On Tue, Apr 16, 2024 at 6:47 PM David Hildenbrand  wrote:


On 16.04.24 12:40, Lance Yang wrote:

Hey David,

Maybe I spotted a bug below.


Thanks for the review!



[...]
   static inline bool folio_likely_mapped_shared(struct folio *folio)
   {
- return page_mapcount(folio_page(folio, 0)) > 1;
+ int mapcount = folio_mapcount(folio);
+
+ /* Only partially-mappable folios require more care. */
+ if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio)))
+ return mapcount > 1;
+
+ /* A single mapping implies "mapped exclusively". */
+ if (mapcount <= 1)
+ return false;
+
+ /* If any page is mapped more than once we treat it "mapped shared". */
+ if (folio_entire_mapcount(folio) || mapcount > folio_nr_pages(folio))
+ return true;

bug: if a PMD-mapped THP is exclusively mapped, the folio_entire_mapcount()
function will return 1 (atomic_read(&folio->_entire_mapcount) + 1).


If it's exclusively mapped, then folio_mapcount(folio)==1. In which case
the previous statement:

if (mapcount <= 1)
 return false;

Catches it.


You're right!



IOW, once we reach this point we now that folio_mapcount(folio) > 1, and
there must be something else besides the entire mapping ("more than once").


Or did I not address your concern?


Sorry, my mistake :(


No worries, thanks for the review and thinking this through!

--
Cheers,

David / dhildenb