2019-10-24 13:50:55

by Ralph Campbell

[permalink] [raw]
Subject: [PATCH v3 2/3] mm/hmm: allow snapshot of the special zero page

If a device driver like nouveau tries to use hmm_range_fault() to access
the special shared zero page in system memory, hmm_range_fault() will
return -EFAULT and kill the process.
Allow hmm_range_fault() to return success (0) when the CPU pagetable
entry points to the special shared zero page.
page_to_pfn() and pfn_to_page() are defined on the zero page so just
handle it like any other page.

Signed-off-by: Ralph Campbell <[email protected]>
Cc: Christoph Hellwig <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Jason Gunthorpe <[email protected]>
---
mm/hmm.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/mm/hmm.c b/mm/hmm.c
index acf7a664b38c..8c96c9ddcae5 100644
--- a/mm/hmm.c
+++ b/mm/hmm.c
@@ -529,8 +529,14 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
if (unlikely(!hmm_vma_walk->pgmap))
return -EBUSY;
} else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
- *pfn = range->values[HMM_PFN_SPECIAL];
- return -EFAULT;
+ if (!is_zero_pfn(pte_pfn(pte))) {
+ *pfn = range->values[HMM_PFN_SPECIAL];
+ return -EFAULT;
+ }
+ /*
+ * Since each architecture defines a struct page for the zero
+ * page, just fall through and treat it like a normal page.
+ */
}

*pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
--
2.20.1


2019-10-24 14:21:56

by Jerome Glisse

[permalink] [raw]
Subject: Re: [PATCH v3 2/3] mm/hmm: allow snapshot of the special zero page

On Wed, Oct 23, 2019 at 12:55:14PM -0700, Ralph Campbell wrote:
> If a device driver like nouveau tries to use hmm_range_fault() to access
> the special shared zero page in system memory, hmm_range_fault() will
> return -EFAULT and kill the process.
> Allow hmm_range_fault() to return success (0) when the CPU pagetable
> entry points to the special shared zero page.
> page_to_pfn() and pfn_to_page() are defined on the zero page so just
> handle it like any other page.
>
> Signed-off-by: Ralph Campbell <[email protected]>
> Cc: Christoph Hellwig <[email protected]>

Reviewed-by: "J?r?me Glisse" <[email protected]>

> Cc: Jason Gunthorpe <[email protected]>
> ---
> mm/hmm.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/mm/hmm.c b/mm/hmm.c
> index acf7a664b38c..8c96c9ddcae5 100644
> --- a/mm/hmm.c
> +++ b/mm/hmm.c
> @@ -529,8 +529,14 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
> if (unlikely(!hmm_vma_walk->pgmap))
> return -EBUSY;
> } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
> - *pfn = range->values[HMM_PFN_SPECIAL];
> - return -EFAULT;
> + if (!is_zero_pfn(pte_pfn(pte))) {
> + *pfn = range->values[HMM_PFN_SPECIAL];
> + return -EFAULT;
> + }
> + /*
> + * Since each architecture defines a struct page for the zero
> + * page, just fall through and treat it like a normal page.
> + */
> }
>
> *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
> --
> 2.20.1
>

2019-10-25 04:47:05

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH v3 2/3] mm/hmm: allow snapshot of the special zero page

On 23.10.19 21:55, Ralph Campbell wrote:
> If a device driver like nouveau tries to use hmm_range_fault() to access
> the special shared zero page in system memory, hmm_range_fault() will
> return -EFAULT and kill the process.
> Allow hmm_range_fault() to return success (0) when the CPU pagetable
> entry points to the special shared zero page.
> page_to_pfn() and pfn_to_page() are defined on the zero page so just
> handle it like any other page.
>
> Signed-off-by: Ralph Campbell <[email protected]>
> Cc: Christoph Hellwig <[email protected]>
> Cc: "Jérôme Glisse" <[email protected]>
> Cc: Jason Gunthorpe <[email protected]>
> ---
> mm/hmm.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/mm/hmm.c b/mm/hmm.c
> index acf7a664b38c..8c96c9ddcae5 100644
> --- a/mm/hmm.c
> +++ b/mm/hmm.c
> @@ -529,8 +529,14 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr,
> if (unlikely(!hmm_vma_walk->pgmap))
> return -EBUSY;
> } else if (IS_ENABLED(CONFIG_ARCH_HAS_PTE_SPECIAL) && pte_special(pte)) {
> - *pfn = range->values[HMM_PFN_SPECIAL];
> - return -EFAULT;
> + if (!is_zero_pfn(pte_pfn(pte))) {
> + *pfn = range->values[HMM_PFN_SPECIAL];
> + return -EFAULT;
> + }
> + /*
> + * Since each architecture defines a struct page for the zero
> + * page, just fall through and treat it like a normal page.
> + */
> }
>
> *pfn = hmm_device_entry_from_pfn(range, pte_pfn(pte)) | cpu_flags;
>

Acked-by: David Hildenbrand <[email protected]>

--

Thanks,

David / dhildenb

2019-10-29 19:47:12

by Jason Gunthorpe

[permalink] [raw]
Subject: Re: [PATCH v3 2/3] mm/hmm: allow snapshot of the special zero page

On Wed, Oct 23, 2019 at 12:55:14PM -0700, Ralph Campbell wrote:
> If a device driver like nouveau tries to use hmm_range_fault() to access
> the special shared zero page in system memory, hmm_range_fault() will
> return -EFAULT and kill the process.
> Allow hmm_range_fault() to return success (0) when the CPU pagetable
> entry points to the special shared zero page.
> page_to_pfn() and pfn_to_page() are defined on the zero page so just
> handle it like any other page.
>
> Signed-off-by: Ralph Campbell <[email protected]>
> Cc: Christoph Hellwig <[email protected]>
> Cc: "Jérôme Glisse" <[email protected]>
> Cc: Jason Gunthorpe <[email protected]>
> ---
> mm/hmm.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)

Applied to hmm.git

Thanks,
Jason