From: Mike Rapoport <rppt@kernel.org>
To: Ryan Roberts <ryan.roberts@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
SeongJae Park <sj@kernel.org>,
Christoph Hellwig <hch@infradead.org>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
Lorenzo Stoakes <lstoakes@gmail.com>,
Uladzislau Rezki <urezki@gmail.com>, Zi Yan <ziy@nvidia.com>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
damon@lists.linux.dev
Subject: Re: [PATCH v2 2/5] mm: damon must atomically clear young on ptes and pmds
Date: Wed, 24 May 2023 21:49:17 +0300 [thread overview]
Message-ID: <20230524184917.GP4967@kernel.org> (raw)
In-Reply-To: <20230518110727.2106156-3-ryan.roberts@arm.com>
On Thu, May 18, 2023 at 12:07:24PM +0100, Ryan Roberts 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: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions")
> 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/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
>
>
--
Sincerely yours,
Mike.
next prev parent reply other threads:[~2023-05-24 18:49 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-18 11:07 [PATCH v2 0/5] Encapsulate PTE contents from non-arch code Ryan Roberts
2023-05-18 11:07 ` [PATCH v2 1/5] mm: vmalloc must set pte via arch code Ryan Roberts
2023-05-19 12:09 ` Uladzislau Rezki
2023-05-24 18:47 ` Mike Rapoport
2023-05-18 11:07 ` [PATCH v2 2/5] mm: damon must atomically clear young on ptes and pmds Ryan Roberts
2023-05-18 17:13 ` SeongJae Park
2023-05-19 8:53 ` Ryan Roberts
2023-05-18 23:19 ` Yu Zhao
2023-05-19 9:02 ` Ryan Roberts
2023-05-19 19:54 ` SeongJae Park
2023-05-22 8:53 ` Ryan Roberts
2023-05-24 18:49 ` Mike Rapoport [this message]
2023-05-18 11:07 ` [PATCH v2 3/5] mm: Fix failure to unmap pte on highmem systems Ryan Roberts
2023-05-24 18:59 ` Mike Rapoport
2023-05-18 11:07 ` [PATCH v2 4/5] mm: Add new ptep_deref() helper to fully encapsulate pte_t Ryan Roberts
2023-05-18 19:28 ` Yu Zhao
2023-05-19 9:12 ` Ryan Roberts
2023-05-25 9:08 ` Ryan Roberts
2023-05-26 2:02 ` Yu Zhao
2023-05-24 19:06 ` Mike Rapoport
2023-05-24 19:11 ` Ryan Roberts
2023-05-18 11:07 ` [PATCH v2 5/5] mm: ptep_deref() conversion Ryan Roberts
2023-05-18 12:08 ` Jason Gunthorpe
2023-05-18 17:22 ` SeongJae Park
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=20230524184917.GP4967@kernel.org \
--to=rppt@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=damon@lists.linux.dev \
--cc=hch@infradead.org \
--cc=kirill.shutemov@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lstoakes@gmail.com \
--cc=ryan.roberts@arm.com \
--cc=sj@kernel.org \
--cc=urezki@gmail.com \
--cc=willy@infradead.org \
--cc=ziy@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.