diff mbox series

[RFC,05/39] mm/rmap: introduce and use hugetlb_try_share_anon_rmap()

Message ID 20231204142146.91437-6-david@redhat.com (mailing list archive)
State New
Headers show
Series mm/rmap: interface overhaul | expand

Commit Message

David Hildenbrand Dec. 4, 2023, 2:21 p.m. UTC
hugetlb rmap handling differs quite a lot from "ordinary" rmap code.
For example, hugetlb currently only supports entire mappings, and treats
any mapping as mapped using a single "logical PTE". Let's move it out
of the way so we can overhaul our "ordinary" rmap.
implementation/interface.

So let's introduce and use hugetlb_try_dup_anon_rmap() to make all
hugetlb handling use dedicated hugetlb_* rmap functions.

Note that try_to_unmap_one() does not need care. Easy to spot because
among all that nasty hugetlb special-casing in that function, we're not
using set_huge_pte_at() on the anon path -- well, and that code assumes
that we we would want to swapout.

Signed-off-by: David Hildenbrand <david@redhat.com>
---
 include/linux/rmap.h | 20 ++++++++++++++++++++
 mm/rmap.c            | 15 ++++++++++-----
 2 files changed, 30 insertions(+), 5 deletions(-)

Comments

Yin Fengwei Dec. 6, 2023, 1:23 a.m. UTC | #1
On 12/4/23 22:21, David Hildenbrand wrote:
> hugetlb rmap handling differs quite a lot from "ordinary" rmap code.
> For example, hugetlb currently only supports entire mappings, and treats
> any mapping as mapped using a single "logical PTE". Let's move it out
> of the way so we can overhaul our "ordinary" rmap.
> implementation/interface.
> 
> So let's introduce and use hugetlb_try_dup_anon_rmap() to make all
> hugetlb handling use dedicated hugetlb_* rmap functions.
> 
> Note that try_to_unmap_one() does not need care. Easy to spot because
> among all that nasty hugetlb special-casing in that function, we're not
> using set_huge_pte_at() on the anon path -- well, and that code assumes
> that we we would want to swapout.
> 
> Signed-off-by: David Hildenbrand <david@redhat.com>

Reviewed-by: Yin Fengwei <fengwei.yin@intel.com>

> ---
>  include/linux/rmap.h | 20 ++++++++++++++++++++
>  mm/rmap.c            | 15 ++++++++++-----
>  2 files changed, 30 insertions(+), 5 deletions(-)
> 
> diff --git a/include/linux/rmap.h b/include/linux/rmap.h
> index 8068c332e2ce5..3f38141b53b9d 100644
> --- a/include/linux/rmap.h
> +++ b/include/linux/rmap.h
> @@ -223,6 +223,26 @@ static inline int hugetlb_try_dup_anon_rmap(struct folio *folio,
>  	return 0;
>  }
>  
> +/* See page_try_share_anon_rmap() */
> +static inline int hugetlb_try_share_anon_rmap(struct folio *folio)
> +{
> +	VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
> +	VM_WARN_ON_FOLIO(!PageAnonExclusive(&folio->page), folio);
> +
> +	/* See page_try_share_anon_rmap() */
> +	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
> +		smp_mb();
> +
> +	if (unlikely(folio_maybe_dma_pinned(folio)))
> +		return -EBUSY;
> +	ClearPageAnonExclusive(&folio->page);
> +
> +	/* See page_try_share_anon_rmap() */
> +	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
> +		smp_mb__after_atomic();
> +	return 0;
> +}
> +
>  static inline void hugetlb_add_file_rmap(struct folio *folio)
>  {
>  	VM_WARN_ON_FOLIO(folio_test_anon(folio), folio);
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 5037581b79ec6..2f1af3958e687 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -2105,13 +2105,18 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
>  				       !anon_exclusive, subpage);
>  
>  			/* See page_try_share_anon_rmap(): clear PTE first. */
> -			if (anon_exclusive &&
> -			    page_try_share_anon_rmap(subpage)) {
> -				if (folio_test_hugetlb(folio))
> +			if (folio_test_hugetlb(folio)) {
> +				if (anon_exclusive &&
> +				    hugetlb_try_share_anon_rmap(folio)) {
>  					set_huge_pte_at(mm, address, pvmw.pte,
>  							pteval, hsz);
> -				else
> -					set_pte_at(mm, address, pvmw.pte, pteval);
> +					ret = false;
> +					page_vma_mapped_walk_done(&pvmw);
> +					break;
> +				}
> +			} else if (anon_exclusive &&
> +				   page_try_share_anon_rmap(page)) {
> +				set_pte_at(mm, address, pvmw.pte, pteval);
>  				ret = false;
>  				page_vma_mapped_walk_done(&pvmw);
>  				break;
diff mbox series

Patch

diff --git a/include/linux/rmap.h b/include/linux/rmap.h
index 8068c332e2ce5..3f38141b53b9d 100644
--- a/include/linux/rmap.h
+++ b/include/linux/rmap.h
@@ -223,6 +223,26 @@  static inline int hugetlb_try_dup_anon_rmap(struct folio *folio,
 	return 0;
 }
 
+/* See page_try_share_anon_rmap() */
+static inline int hugetlb_try_share_anon_rmap(struct folio *folio)
+{
+	VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
+	VM_WARN_ON_FOLIO(!PageAnonExclusive(&folio->page), folio);
+
+	/* See page_try_share_anon_rmap() */
+	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
+		smp_mb();
+
+	if (unlikely(folio_maybe_dma_pinned(folio)))
+		return -EBUSY;
+	ClearPageAnonExclusive(&folio->page);
+
+	/* See page_try_share_anon_rmap() */
+	if (IS_ENABLED(CONFIG_HAVE_FAST_GUP))
+		smp_mb__after_atomic();
+	return 0;
+}
+
 static inline void hugetlb_add_file_rmap(struct folio *folio)
 {
 	VM_WARN_ON_FOLIO(folio_test_anon(folio), folio);
diff --git a/mm/rmap.c b/mm/rmap.c
index 5037581b79ec6..2f1af3958e687 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2105,13 +2105,18 @@  static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 				       !anon_exclusive, subpage);
 
 			/* See page_try_share_anon_rmap(): clear PTE first. */
-			if (anon_exclusive &&
-			    page_try_share_anon_rmap(subpage)) {
-				if (folio_test_hugetlb(folio))
+			if (folio_test_hugetlb(folio)) {
+				if (anon_exclusive &&
+				    hugetlb_try_share_anon_rmap(folio)) {
 					set_huge_pte_at(mm, address, pvmw.pte,
 							pteval, hsz);
-				else
-					set_pte_at(mm, address, pvmw.pte, pteval);
+					ret = false;
+					page_vma_mapped_walk_done(&pvmw);
+					break;
+				}
+			} else if (anon_exclusive &&
+				   page_try_share_anon_rmap(page)) {
+				set_pte_at(mm, address, pvmw.pte, pteval);
 				ret = false;
 				page_vma_mapped_walk_done(&pvmw);
 				break;