damon.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/4] Fixes for pte encapsulation bypasses
@ 2023-06-02  9:29 Ryan Roberts
  2023-06-02  9:29 ` [PATCH v3 1/4] mm: vmalloc must set pte via arch code Ryan Roberts
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Ryan Roberts @ 2023-06-02  9:29 UTC (permalink / raw)
  To: Andrew Morton, SeongJae Park, Christoph Hellwig
  Cc: Ryan Roberts, Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Yu Zhao, Mike Rapoport, linux-kernel, linux-mm, damon

This is the first half of v3 of a series to improve the encapsulation of pte
entries by disallowing non-arch code from directly dereferencing pte_t pointers.
Based on earlier feedback, I've split the series in 2; this first part contains
fixes for existing bugs that were discovered during the work. The second part,
which contains the new changes I'm adding, will be posted separately.

These fixes have had a fair amount of review now so I hope they can be
considered for addition to the appropriate mm- branch as is. And I've cc'ed
stable since I believe they should be candidates for backport.

See the v1 cover letter at [1] for rationale and explanation of overall
objective (requires both parts of the split series to achieve).

The series is split up as follows:

patch 1-2:  Fix bugs where code was _setting_ ptes directly, rather than using
            set_pte_at() and friends. Data corruption was theoretically
	    possible.
patch 3:    Minor refactoring requested in v2 review.
patch 4:    Fix highmem unmapping issue I spotted while doing the work.

Patches are based on v6.4-rc4 and a branch is available at [3].

Changes since v2 [2]:
   - patch 2: minor commit message rewording to fix review nits
   - patch 3: Refactored damon code to use {pte|pmd}p_clear_young_notify()
   - patch 1-4: applied Ack/Reviewed-by tags; thanks for those!

Changes since v1 [1]:
   - patch 1: Refactored pfn to use local variable
   - patch 1-2: Minor rewording of commit message: 'verify' -> 'check'
   - patch 1-3: applied Ack/Reviewed-by tags; thanks for those!

[1] https://lore.kernel.org/linux-mm/20230511132113.80196-1-ryan.roberts@arm.com/
[2] https://lore.kernel.org/linux-mm/20230518110727.2106156-1-ryan.roberts@arm.com/
[3] https://gitlab.arm.com/linux-arm/linux-rr/-/tree/bugs/pte_encapsulation_bypasses-lkml_v3

Thanks,
Ryan

Ryan Roberts (4):
  mm: vmalloc must set pte via arch code
  mm/damon/ops-common: atomically test and clear young on ptes and pmds
  mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify()
  mm: Fix failure to unmap pte on highmem systems

 mm/damon/ops-common.c | 30 ++++--------------------------
 mm/damon/ops-common.h |  4 ++--
 mm/damon/paddr.c      |  4 ++--
 mm/damon/vaddr.c      |  4 ++--
 mm/memory.c           |  6 ++----
 mm/vmalloc.c          | 10 ++++++++--
 6 files changed, 20 insertions(+), 38 deletions(-)

--
2.25.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v3 1/4] mm: vmalloc must set pte via arch code
  2023-06-02  9:29 [PATCH v3 0/4] Fixes for pte encapsulation bypasses Ryan Roberts
@ 2023-06-02  9:29 ` Ryan Roberts
  2023-06-02  9:29 ` [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds Ryan Roberts
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: Ryan Roberts @ 2023-06-02  9:29 UTC (permalink / raw)
  To: Andrew Morton, SeongJae Park, Christoph Hellwig
  Cc: Ryan Roberts, Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Yu Zhao, Mike Rapoport, linux-kernel, linux-mm, damon,
	Christoph Hellwig

It is bad practice to directly set pte entries within a pte table.
Instead all modifications must go through arch-provided helpers such as
set_pte_at() to give the arch code visibility and allow it to check
(and potentially modify) the operation.

Fixes: 3e9a9e256b1e ("mm: add a vmap_pfn function")
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Acked-by: Lorenzo Stoakes <lstoakes@gmail.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Uladzislau Rezki (Sony) <urezki@gmail.com>
Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>
---
 mm/vmalloc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 9683573f1225..48202ec5f79a 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -2899,10 +2899,16 @@ struct vmap_pfn_data {
 static int vmap_pfn_apply(pte_t *pte, unsigned long addr, void *private)
 {
 	struct vmap_pfn_data *data = private;
+	unsigned long pfn = data->pfns[data->idx];
+	pte_t ptent;
 
-	if (WARN_ON_ONCE(pfn_valid(data->pfns[data->idx])))
+	if (WARN_ON_ONCE(pfn_valid(pfn)))
 		return -EINVAL;
-	*pte = pte_mkspecial(pfn_pte(data->pfns[data->idx++], data->prot));
+
+	ptent = pte_mkspecial(pfn_pte(pfn, data->prot));
+	set_pte_at(&init_mm, addr, pte, ptent);
+
+	data->idx++;
 	return 0;
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds
  2023-06-02  9:29 [PATCH v3 0/4] Fixes for pte encapsulation bypasses Ryan Roberts
  2023-06-02  9:29 ` [PATCH v3 1/4] mm: vmalloc must set pte via arch code Ryan Roberts
@ 2023-06-02  9:29 ` Ryan Roberts
  2023-06-02 16:35   ` Yu Zhao
  2023-06-02  9:29 ` [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify() Ryan Roberts
  2023-06-02  9:29 ` [PATCH v3 4/4] mm: Fix failure to unmap pte on highmem systems Ryan Roberts
  3 siblings, 1 reply; 13+ messages in thread
From: Ryan Roberts @ 2023-06-02  9:29 UTC (permalink / raw)
  To: Andrew Morton, SeongJae Park, Christoph Hellwig
  Cc: Ryan Roberts, Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Yu Zhao, Mike Rapoport, linux-kernel, linux-mm, damon

It is racy to non-atomically read a pte, then clear the young bit, then
write it back as this could discard dirty information. Further, it is
bad practice to directly set a pte entry within a table. Instead
clearing young must go through the arch-provided helper,
ptep_test_and_clear_young() to ensure it is modified atomically and to
give the arch code visibility and allow it to check (and potentially
modify) the operation.

Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces").
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Reviewed-by: SeongJae Park <sj@kernel.org>
Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>
---
 mm/damon/ops-common.c | 16 ++++++----------
 mm/damon/ops-common.h |  4 ++--
 mm/damon/paddr.c      |  4 ++--
 mm/damon/vaddr.c      |  4 ++--
 4 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/mm/damon/ops-common.c b/mm/damon/ops-common.c
index cc63cf953636..acc264b97903 100644
--- a/mm/damon/ops-common.c
+++ b/mm/damon/ops-common.c
@@ -37,7 +37,7 @@ struct folio *damon_get_folio(unsigned long pfn)
 	return folio;
 }
 
-void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, unsigned long addr)
+void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr)
 {
 	bool referenced = false;
 	struct folio *folio = damon_get_folio(pte_pfn(*pte));
@@ -45,13 +45,11 @@ void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, unsigned long addr)
 	if (!folio)
 		return;
 
-	if (pte_young(*pte)) {
+	if (ptep_test_and_clear_young(vma, addr, pte))
 		referenced = true;
-		*pte = pte_mkold(*pte);
-	}
 
 #ifdef CONFIG_MMU_NOTIFIER
-	if (mmu_notifier_clear_young(mm, addr, addr + PAGE_SIZE))
+	if (mmu_notifier_clear_young(vma->vm_mm, addr, addr + PAGE_SIZE))
 		referenced = true;
 #endif /* CONFIG_MMU_NOTIFIER */
 
@@ -62,7 +60,7 @@ void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, unsigned long addr)
 	folio_put(folio);
 }
 
-void damon_pmdp_mkold(pmd_t *pmd, struct mm_struct *mm, unsigned long addr)
+void damon_pmdp_mkold(pmd_t *pmd, struct vm_area_struct *vma, unsigned long addr)
 {
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	bool referenced = false;
@@ -71,13 +69,11 @@ void damon_pmdp_mkold(pmd_t *pmd, struct mm_struct *mm, unsigned long addr)
 	if (!folio)
 		return;
 
-	if (pmd_young(*pmd)) {
+	if (pmdp_test_and_clear_young(vma, addr, pmd))
 		referenced = true;
-		*pmd = pmd_mkold(*pmd);
-	}
 
 #ifdef CONFIG_MMU_NOTIFIER
-	if (mmu_notifier_clear_young(mm, addr, addr + HPAGE_PMD_SIZE))
+	if (mmu_notifier_clear_young(vma->vm_mm, addr, addr + HPAGE_PMD_SIZE))
 		referenced = true;
 #endif /* CONFIG_MMU_NOTIFIER */
 
diff --git a/mm/damon/ops-common.h b/mm/damon/ops-common.h
index 14f4bc69f29b..18d837d11bce 100644
--- a/mm/damon/ops-common.h
+++ b/mm/damon/ops-common.h
@@ -9,8 +9,8 @@
 
 struct folio *damon_get_folio(unsigned long pfn);
 
-void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, unsigned long addr);
-void damon_pmdp_mkold(pmd_t *pmd, struct mm_struct *mm, unsigned long addr);
+void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr);
+void damon_pmdp_mkold(pmd_t *pmd, struct vm_area_struct *vma, unsigned long addr);
 
 int damon_cold_score(struct damon_ctx *c, struct damon_region *r,
 			struct damos *s);
diff --git a/mm/damon/paddr.c b/mm/damon/paddr.c
index 467b99166b43..5b3a3463d078 100644
--- a/mm/damon/paddr.c
+++ b/mm/damon/paddr.c
@@ -24,9 +24,9 @@ static bool __damon_pa_mkold(struct folio *folio, struct vm_area_struct *vma,
 	while (page_vma_mapped_walk(&pvmw)) {
 		addr = pvmw.address;
 		if (pvmw.pte)
-			damon_ptep_mkold(pvmw.pte, vma->vm_mm, addr);
+			damon_ptep_mkold(pvmw.pte, vma, addr);
 		else
-			damon_pmdp_mkold(pvmw.pmd, vma->vm_mm, addr);
+			damon_pmdp_mkold(pvmw.pmd, vma, addr);
 	}
 	return true;
 }
diff --git a/mm/damon/vaddr.c b/mm/damon/vaddr.c
index 1fec16d7263e..37994fb6120c 100644
--- a/mm/damon/vaddr.c
+++ b/mm/damon/vaddr.c
@@ -311,7 +311,7 @@ static int damon_mkold_pmd_entry(pmd_t *pmd, unsigned long addr,
 		}
 
 		if (pmd_trans_huge(*pmd)) {
-			damon_pmdp_mkold(pmd, walk->mm, addr);
+			damon_pmdp_mkold(pmd, walk->vma, addr);
 			spin_unlock(ptl);
 			return 0;
 		}
@@ -323,7 +323,7 @@ static int damon_mkold_pmd_entry(pmd_t *pmd, unsigned long addr,
 	pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
 	if (!pte_present(*pte))
 		goto out;
-	damon_ptep_mkold(pte, walk->mm, addr);
+	damon_ptep_mkold(pte, walk->vma, addr);
 out:
 	pte_unmap_unlock(pte, ptl);
 	return 0;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify()
  2023-06-02  9:29 [PATCH v3 0/4] Fixes for pte encapsulation bypasses Ryan Roberts
  2023-06-02  9:29 ` [PATCH v3 1/4] mm: vmalloc must set pte via arch code Ryan Roberts
  2023-06-02  9:29 ` [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds Ryan Roberts
@ 2023-06-02  9:29 ` Ryan Roberts
  2023-06-02 16:28   ` Yu Zhao
  2023-06-02 21:54   ` SeongJae Park
  2023-06-02  9:29 ` [PATCH v3 4/4] mm: Fix failure to unmap pte on highmem systems Ryan Roberts
  3 siblings, 2 replies; 13+ messages in thread
From: Ryan Roberts @ 2023-06-02  9:29 UTC (permalink / raw)
  To: Andrew Morton, SeongJae Park, Christoph Hellwig
  Cc: Ryan Roberts, Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Yu Zhao, Mike Rapoport, linux-kernel, linux-mm, damon

With the fix in place to atomically test and clear young on ptes and
pmds, simplify the code to handle the clearing for both the primary mmu
and the mmu notifier with a single API call.

Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
---
 mm/damon/ops-common.c | 22 ++--------------------
 1 file changed, 2 insertions(+), 20 deletions(-)

diff --git a/mm/damon/ops-common.c b/mm/damon/ops-common.c
index acc264b97903..d4ab81229136 100644
--- a/mm/damon/ops-common.c
+++ b/mm/damon/ops-common.c
@@ -39,21 +39,12 @@ struct folio *damon_get_folio(unsigned long pfn)
 
 void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr)
 {
-	bool referenced = false;
 	struct folio *folio = damon_get_folio(pte_pfn(*pte));
 
 	if (!folio)
 		return;
 
-	if (ptep_test_and_clear_young(vma, addr, pte))
-		referenced = true;
-
-#ifdef CONFIG_MMU_NOTIFIER
-	if (mmu_notifier_clear_young(vma->vm_mm, addr, addr + PAGE_SIZE))
-		referenced = true;
-#endif /* CONFIG_MMU_NOTIFIER */
-
-	if (referenced)
+	if (ptep_clear_young_notify(vma, addr, pte))
 		folio_set_young(folio);
 
 	folio_set_idle(folio);
@@ -63,21 +54,12 @@ void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr
 void damon_pmdp_mkold(pmd_t *pmd, struct vm_area_struct *vma, unsigned long addr)
 {
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	bool referenced = false;
 	struct folio *folio = damon_get_folio(pmd_pfn(*pmd));
 
 	if (!folio)
 		return;
 
-	if (pmdp_test_and_clear_young(vma, addr, pmd))
-		referenced = true;
-
-#ifdef CONFIG_MMU_NOTIFIER
-	if (mmu_notifier_clear_young(vma->vm_mm, addr, addr + HPAGE_PMD_SIZE))
-		referenced = true;
-#endif /* CONFIG_MMU_NOTIFIER */
-
-	if (referenced)
+	if (pmdp_clear_young_notify(vma, addr, pmd))
 		folio_set_young(folio);
 
 	folio_set_idle(folio);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v3 4/4] mm: Fix failure to unmap pte on highmem systems
  2023-06-02  9:29 [PATCH v3 0/4] Fixes for pte encapsulation bypasses Ryan Roberts
                   ` (2 preceding siblings ...)
  2023-06-02  9:29 ` [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify() Ryan Roberts
@ 2023-06-02  9:29 ` Ryan Roberts
  3 siblings, 0 replies; 13+ messages in thread
From: Ryan Roberts @ 2023-06-02  9:29 UTC (permalink / raw)
  To: Andrew Morton, SeongJae Park, Christoph Hellwig
  Cc: Ryan Roberts, Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Yu Zhao, Mike Rapoport, linux-kernel, linux-mm, damon

The loser of a race to service a pte for a device private entry in the
swap path previously unlocked the ptl, but failed to unmap the pte. This
only affects highmem systems since unmapping a pte is a noop on
non-highmem systems.

Fixes: 16ce101db85d ("mm/memory.c: fix race when faulting a device private page")
Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>
---
 mm/memory.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index f69fbc251198..ed429e20a1bb 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -3728,10 +3728,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
 			vmf->page = pfn_swap_entry_to_page(entry);
 			vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
 					vmf->address, &vmf->ptl);
-			if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte))) {
-				spin_unlock(vmf->ptl);
-				goto out;
-			}
+			if (unlikely(!pte_same(*vmf->pte, vmf->orig_pte)))
+				goto unlock;
 
 			/*
 			 * Get a page reference while we know the page can't be
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify()
  2023-06-02  9:29 ` [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify() Ryan Roberts
@ 2023-06-02 16:28   ` Yu Zhao
  2023-06-02 21:54   ` SeongJae Park
  1 sibling, 0 replies; 13+ messages in thread
From: Yu Zhao @ 2023-06-02 16:28 UTC (permalink / raw)
  To: Ryan Roberts
  Cc: Andrew Morton, SeongJae Park, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Mike Rapoport, linux-kernel, linux-mm, damon

On Fri, Jun 2, 2023 at 3:30 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
>
> With the fix in place to atomically test and clear young on ptes and
> pmds, simplify the code to handle the clearing for both the primary mmu
> and the mmu notifier with a single API call.
>
> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>

Acked-by: Yu Zhao <yuzhao@google.com>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds
  2023-06-02  9:29 ` [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds Ryan Roberts
@ 2023-06-02 16:35   ` Yu Zhao
  2023-06-02 17:14     ` Ryan Roberts
  0 siblings, 1 reply; 13+ messages in thread
From: Yu Zhao @ 2023-06-02 16:35 UTC (permalink / raw)
  To: Ryan Roberts
  Cc: Andrew Morton, SeongJae Park, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Mike Rapoport, linux-kernel, linux-mm, damon

On Fri, Jun 2, 2023 at 3:30 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
>
> It is racy to non-atomically read a pte, then clear the young bit, then
> write it back as this could discard dirty information. Further, it is
> bad practice to directly set a pte entry within a table. Instead
> clearing young must go through the arch-provided helper,
> ptep_test_and_clear_young() to ensure it is modified atomically and to
> give the arch code visibility and allow it to check (and potentially
> modify) the operation.
>
> Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces").

Just to double check: was "Cc: stable@vger.kernel.org" overlooked or
deemed unnecessary?

> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
> Reviewed-by: Zi Yan <ziy@nvidia.com>
> Reviewed-by: SeongJae Park <sj@kernel.org>
> Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds
  2023-06-02 16:35   ` Yu Zhao
@ 2023-06-02 17:14     ` Ryan Roberts
  2023-06-02 17:35       ` Yu Zhao
  2023-06-02 19:15       ` SeongJae Park
  0 siblings, 2 replies; 13+ messages in thread
From: Ryan Roberts @ 2023-06-02 17:14 UTC (permalink / raw)
  To: Yu Zhao
  Cc: Andrew Morton, SeongJae Park, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Mike Rapoport, linux-kernel, linux-mm, damon

On 02/06/2023 17:35, Yu Zhao wrote:
> On Fri, Jun 2, 2023 at 3:30 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>
>> It is racy to non-atomically read a pte, then clear the young bit, then
>> write it back as this could discard dirty information. Further, it is
>> bad practice to directly set a pte entry within a table. Instead
>> clearing young must go through the arch-provided helper,
>> ptep_test_and_clear_young() to ensure it is modified atomically and to
>> give the arch code visibility and allow it to check (and potentially
>> modify) the operation.
>>
>> Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces").
> 
> Just to double check: was "Cc: stable@vger.kernel.org" overlooked or
> deemed unnecessary?

It was overlooked - incompetance strikes again! I was intending to cc the whole
series. What's the best way to fix this? Can I just add stable in cc on reply to
the cover letter or will I have to resend the lot?

> 
>> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
>> Reviewed-by: Zi Yan <ziy@nvidia.com>
>> Reviewed-by: SeongJae Park <sj@kernel.org>
>> Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds
  2023-06-02 17:14     ` Ryan Roberts
@ 2023-06-02 17:35       ` Yu Zhao
  2023-06-02 19:15       ` SeongJae Park
  1 sibling, 0 replies; 13+ messages in thread
From: Yu Zhao @ 2023-06-02 17:35 UTC (permalink / raw)
  To: Ryan Roberts
  Cc: Andrew Morton, SeongJae Park, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Mike Rapoport, linux-kernel, linux-mm, damon

On Fri, Jun 2, 2023 at 11:14 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
>
> On 02/06/2023 17:35, Yu Zhao wrote:
> > On Fri, Jun 2, 2023 at 3:30 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
> >>
> >> It is racy to non-atomically read a pte, then clear the young bit, then
> >> write it back as this could discard dirty information. Further, it is
> >> bad practice to directly set a pte entry within a table. Instead
> >> clearing young must go through the arch-provided helper,
> >> ptep_test_and_clear_young() to ensure it is modified atomically and to
> >> give the arch code visibility and allow it to check (and potentially
> >> modify) the operation.
> >>
> >> Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces").
> >
> > Just to double check: was "Cc: stable@vger.kernel.org" overlooked or
> > deemed unnecessary?
>
> It was overlooked - incompetance strikes again! I was intending to cc the whole
> series. What's the best way to fix this? Can I just add stable in cc on reply to
> the cover letter or will I have to resend the lot?

Resending the whole series would be more reliable for the process (and
easier for Andrew). You might want to include a few new
reviewed/acked-bys anyway (I just acked the next patch).

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds
  2023-06-02 17:14     ` Ryan Roberts
  2023-06-02 17:35       ` Yu Zhao
@ 2023-06-02 19:15       ` SeongJae Park
  2023-06-02 21:43         ` SeongJae Park
  1 sibling, 1 reply; 13+ messages in thread
From: SeongJae Park @ 2023-06-02 19:15 UTC (permalink / raw)
  To: Ryan Roberts
  Cc: Yu Zhao, Andrew Morton, SeongJae Park, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Mike Rapoport, linux-kernel, linux-mm, damon

Hi Ryan,

On Fri, 2 Jun 2023 18:14:25 +0100 Ryan Roberts <ryan.roberts@arm.com> wrote:

> On 02/06/2023 17:35, Yu Zhao wrote:
> > On Fri, Jun 2, 2023 at 3:30 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
> >>
> >> It is racy to non-atomically read a pte, then clear the young bit, then
> >> write it back as this could discard dirty information. Further, it is
> >> bad practice to directly set a pte entry within a table. Instead
> >> clearing young must go through the arch-provided helper,
> >> ptep_test_and_clear_young() to ensure it is modified atomically and to
> >> give the arch code visibility and allow it to check (and potentially
> >> modify) the operation.
> >>
> >> Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces").
> > 
> > Just to double check: was "Cc: stable@vger.kernel.org" overlooked or
> > deemed unnecessary?
> 
> It was overlooked - incompetance strikes again! I was intending to cc the
> whole series.

Not the whole patches in this series but only this patch is intended to be
merged in stable series, right?  If I'm not wrong, you could add
'Cc: <stable@vger.kernel.org>' tag here[1] when resending, to let stable kernel
maintainers easily understand exactly what patches should be merged in the
stable kernels.  So, you wouldn't need to touch coverletter or cc whole series
but only this one.

[1] https://www.kernel.org/doc/html/v4.10/process/stable-kernel-rules.html


Thanks,
SJ

> What's the best way to fix this? Can I just add stable in cc on reply to
> the cover letter or will I have to resend the lot?
> 
> > 
> >> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>
> >> Reviewed-by: Zi Yan <ziy@nvidia.com>
> >> Reviewed-by: SeongJae Park <sj@kernel.org>
> >> Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds
  2023-06-02 19:15       ` SeongJae Park
@ 2023-06-02 21:43         ` SeongJae Park
  2023-06-03 18:20           ` Ryan Roberts
  0 siblings, 1 reply; 13+ messages in thread
From: SeongJae Park @ 2023-06-02 21:43 UTC (permalink / raw)
  To: SeongJae Park
  Cc: Ryan Roberts, Yu Zhao, Andrew Morton, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Mike Rapoport, linux-kernel, linux-mm, damon

On Fri, 2 Jun 2023 19:15:01 +0000 SeongJae Park <sj@kernel.org> wrote:

> Hi Ryan,
> 
> On Fri, 2 Jun 2023 18:14:25 +0100 Ryan Roberts <ryan.roberts@arm.com> wrote:
> 
> > On 02/06/2023 17:35, Yu Zhao wrote:
> > > On Fri, Jun 2, 2023 at 3:30 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
> > >>
> > >> It is racy to non-atomically read a pte, then clear the young bit, then
> > >> write it back as this could discard dirty information. Further, it is
> > >> bad practice to directly set a pte entry within a table. Instead
> > >> clearing young must go through the arch-provided helper,
> > >> ptep_test_and_clear_young() to ensure it is modified atomically and to
> > >> give the arch code visibility and allow it to check (and potentially
> > >> modify) the operation.
> > >>
> > >> Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces").
> > > 
> > > Just to double check: was "Cc: stable@vger.kernel.org" overlooked or
> > > deemed unnecessary?
> > 
> > It was overlooked - incompetance strikes again! I was intending to cc the
> > whole series.
> 
> Not the whole patches in this series but only this patch is intended to be
> merged in stable series, right?  If I'm not wrong, you could add
> 'Cc: <stable@vger.kernel.org>' tag here[1] when resending, to let stable kernel
> maintainers easily understand exactly what patches should be merged in the
> stable kernels.  So, you wouldn't need to touch coverletter or cc whole series
> but only this one.
> 
> [1] https://www.kernel.org/doc/html/v4.10/process/stable-kernel-rules.html

And I just found Andrew added the tag while adding this to the -mm queue.
Thank you, Andrew!

[1] https://lore.kernel.org/mm-commits/20230602205509.9DFBDC433D2@smtp.kernel.org/


Thanks,
SJ

> 
> 
> Thanks,
> SJ
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify()
  2023-06-02  9:29 ` [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify() Ryan Roberts
  2023-06-02 16:28   ` Yu Zhao
@ 2023-06-02 21:54   ` SeongJae Park
  1 sibling, 0 replies; 13+ messages in thread
From: SeongJae Park @ 2023-06-02 21:54 UTC (permalink / raw)
  To: Ryan Roberts
  Cc: Andrew Morton, SeongJae Park, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Yu Zhao, Mike Rapoport, linux-kernel, linux-mm, damon

Hi Ryan,

On Fri, 2 Jun 2023 10:29:48 +0100 Ryan Roberts <ryan.roberts@arm.com> wrote:

> With the fix in place to atomically test and clear young on ptes and
> pmds, simplify the code to handle the clearing for both the primary mmu
> and the mmu notifier with a single API call.
> 
> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com>

Reviewed-by: SeongJae Park <sj@kernel.org>


Thanks,
SJ

> ---
>  mm/damon/ops-common.c | 22 ++--------------------
>  1 file changed, 2 insertions(+), 20 deletions(-)
> 
> diff --git a/mm/damon/ops-common.c b/mm/damon/ops-common.c
> index acc264b97903..d4ab81229136 100644
> --- a/mm/damon/ops-common.c
> +++ b/mm/damon/ops-common.c
> @@ -39,21 +39,12 @@ struct folio *damon_get_folio(unsigned long pfn)
>  
>  void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr)
>  {
> -	bool referenced = false;
>  	struct folio *folio = damon_get_folio(pte_pfn(*pte));
>  
>  	if (!folio)
>  		return;
>  
> -	if (ptep_test_and_clear_young(vma, addr, pte))
> -		referenced = true;
> -
> -#ifdef CONFIG_MMU_NOTIFIER
> -	if (mmu_notifier_clear_young(vma->vm_mm, addr, addr + PAGE_SIZE))
> -		referenced = true;
> -#endif /* CONFIG_MMU_NOTIFIER */
> -
> -	if (referenced)
> +	if (ptep_clear_young_notify(vma, addr, pte))
>  		folio_set_young(folio);
>  
>  	folio_set_idle(folio);
> @@ -63,21 +54,12 @@ void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr
>  void damon_pmdp_mkold(pmd_t *pmd, struct vm_area_struct *vma, unsigned long addr)
>  {
>  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
> -	bool referenced = false;
>  	struct folio *folio = damon_get_folio(pmd_pfn(*pmd));
>  
>  	if (!folio)
>  		return;
>  
> -	if (pmdp_test_and_clear_young(vma, addr, pmd))
> -		referenced = true;
> -
> -#ifdef CONFIG_MMU_NOTIFIER
> -	if (mmu_notifier_clear_young(vma->vm_mm, addr, addr + HPAGE_PMD_SIZE))
> -		referenced = true;
> -#endif /* CONFIG_MMU_NOTIFIER */
> -
> -	if (referenced)
> +	if (pmdp_clear_young_notify(vma, addr, pmd))
>  		folio_set_young(folio);
>  
>  	folio_set_idle(folio);
> -- 
> 2.25.1
> 
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds
  2023-06-02 21:43         ` SeongJae Park
@ 2023-06-03 18:20           ` Ryan Roberts
  0 siblings, 0 replies; 13+ messages in thread
From: Ryan Roberts @ 2023-06-03 18:20 UTC (permalink / raw)
  To: SeongJae Park
  Cc: Yu Zhao, Andrew Morton, Christoph Hellwig,
	Matthew Wilcox (Oracle),
	Kirill A. Shutemov, Lorenzo Stoakes, Uladzislau Rezki, Zi Yan,
	Mike Rapoport, linux-kernel, linux-mm, damon

On 02/06/2023 22:43, SeongJae Park wrote:
> On Fri, 2 Jun 2023 19:15:01 +0000 SeongJae Park <sj@kernel.org> wrote:
> 
>> Hi Ryan,
>>
>> On Fri, 2 Jun 2023 18:14:25 +0100 Ryan Roberts <ryan.roberts@arm.com> wrote:
>>
>>> On 02/06/2023 17:35, Yu Zhao wrote:
>>>> On Fri, Jun 2, 2023 at 3:30 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>>>>
>>>>> It is racy to non-atomically read a pte, then clear the young bit, then
>>>>> write it back as this could discard dirty information. Further, it is
>>>>> bad practice to directly set a pte entry within a table. Instead
>>>>> clearing young must go through the arch-provided helper,
>>>>> ptep_test_and_clear_young() to ensure it is modified atomically and to
>>>>> give the arch code visibility and allow it to check (and potentially
>>>>> modify) the operation.
>>>>>
>>>>> Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces").
>>>>
>>>> Just to double check: was "Cc: stable@vger.kernel.org" overlooked or
>>>> deemed unnecessary?
>>>
>>> It was overlooked - incompetance strikes again! I was intending to cc the
>>> whole series.
>>
>> Not the whole patches in this series but only this patch is intended to be
>> merged in stable series, right?  If I'm not wrong, you could add
>> 'Cc: <stable@vger.kernel.org>' tag here[1] when resending, to let stable kernel
>> maintainers easily understand exactly what patches should be merged in the
>> stable kernels.  So, you wouldn't need to touch coverletter or cc whole series
>> but only this one.
>>
>> [1] https://www.kernel.org/doc/html/v4.10/process/stable-kernel-rules.html
> 
> And I just found Andrew added the tag while adding this to the -mm queue.
> Thank you, Andrew!

Yes indeed - thanks for fixing that up for me, Andrew!

> 
> [1] https://lore.kernel.org/mm-commits/20230602205509.9DFBDC433D2@smtp.kernel.org/
> 
> 
> Thanks,
> SJ
> 
>>
>>
>> Thanks,
>> SJ
>>


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-06-03 18:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-02  9:29 [PATCH v3 0/4] Fixes for pte encapsulation bypasses Ryan Roberts
2023-06-02  9:29 ` [PATCH v3 1/4] mm: vmalloc must set pte via arch code Ryan Roberts
2023-06-02  9:29 ` [PATCH v3 2/4] mm/damon/ops-common: atomically test and clear young on ptes and pmds Ryan Roberts
2023-06-02 16:35   ` Yu Zhao
2023-06-02 17:14     ` Ryan Roberts
2023-06-02 17:35       ` Yu Zhao
2023-06-02 19:15       ` SeongJae Park
2023-06-02 21:43         ` SeongJae Park
2023-06-03 18:20           ` Ryan Roberts
2023-06-02  9:29 ` [PATCH v3 3/4] mm/damon/ops-common: Refactor to use {pte|pmd}p_clear_young_notify() Ryan Roberts
2023-06-02 16:28   ` Yu Zhao
2023-06-02 21:54   ` SeongJae Park
2023-06-02  9:29 ` [PATCH v3 4/4] mm: Fix failure to unmap pte on highmem systems Ryan Roberts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).