Message ID | 20230518110727.2106156-3-ryan.roberts@arm.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp415621vqo; Thu, 18 May 2023 04:26:53 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ50uLta1kJH9b/cFccFrjiHDthcUE82GzExixIotA9ubtv70AizW2TSwkJHpZLdJW6W2eKD X-Received: by 2002:a05:6a00:23c5:b0:645:b13e:e674 with SMTP id g5-20020a056a0023c500b00645b13ee674mr4220559pfc.26.1684409213639; Thu, 18 May 2023 04:26:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684409213; cv=none; d=google.com; s=arc-20160816; b=EX/oebWse7Q5IDnrIVr1NVUMEu52BdUUIpbFotu0cSnzgsDafwXsJN90tK4Rgz74y/ c4TQOEZ12xPACOeRnScG6kUsSPi/KcTDKrd+nDL8NQANNg03oOX4MNu0dMAsNIACQ366 luWe2gSZejQsDCUf+IhSdZcFPNi+nbQVD+hQuorLfiVnnXL9oHgV8Cx5kCE9Sq31idtL M2FtUg14ndZtt4zajIpyxt0vAviVkOKZo4RPAx8kYvDTefmihySv/qenViELv6yjbQvy IryyenEX5WAdsrJ9PJNHEIAraBmedF4X3jw8s4lJF9Pp+buRniYnM3cwhfeJIn56qorC 77Xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=iyeo2oFz3wRj1jfkhI8YHLr4XW9LbfJm+AYYq9yjljk=; b=madjducNHfZAFGypwP6FtbF0ySjheBN0bKI5LzmYezz3Q1Gb3NbE2/P9prGqfmcD48 TfDt2AJZ1AJojxT4pSMiOs3LUudbqOPpBKALyFjjd47NF1rVmUKPQUhehq2gHZJuYaVP GEypWNdYateSPF1qBExcmdKO6Q0xUxUDxZqtzp242rLNTZTAUHJTIUIMqozpiFVxKHc8 zSchTmAXyYHYsf92yI7csN5XDOPz+aONYTsnWoUt3Na0iqLsM9gx18+tQSjDVnTL+E10 2CtH0aq3szv6oStYg/isukLDilU/4dIiVJ7sKPMugfHUyCqp0IIwUIs5kAW81shJcqtg jpEA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 62-20020a620641000000b00643b4daa91esi1359992pfg.331.2023.05.18.04.26.41; Thu, 18 May 2023 04:26:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230453AbjERLHt (ORCPT <rfc822;pacteraone@gmail.com> + 99 others); Thu, 18 May 2023 07:07:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230397AbjERLHn (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 18 May 2023 07:07:43 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 15F3BB8 for <linux-kernel@vger.kernel.org>; Thu, 18 May 2023 04:07:42 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 9B791D75; Thu, 18 May 2023 04:08:26 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 427773F793; Thu, 18 May 2023 04:07:40 -0700 (PDT) From: Ryan Roberts <ryan.roberts@arm.com> To: Andrew Morton <akpm@linux-foundation.org>, SeongJae Park <sj@kernel.org>, Christoph Hellwig <hch@infradead.org> Cc: Ryan Roberts <ryan.roberts@arm.com>, "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: [PATCH v2 2/5] mm: damon must atomically clear young on ptes and pmds Date: Thu, 18 May 2023 12:07:24 +0100 Message-Id: <20230518110727.2106156-3-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230518110727.2106156-1-ryan.roberts@arm.com> References: <20230518110727.2106156-1-ryan.roberts@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766231075603471959?= X-GMAIL-MSGID: =?utf-8?q?1766231075603471959?= |
Series |
Encapsulate PTE contents from non-arch code
|
|
Commit Message
Ryan Roberts
May 18, 2023, 11:07 a.m. UTC
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> --- 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(-)
Comments
Hi Ryan, Nit. For consistency of DAMON commits subjects, I'd prefer something like "mm/damon/ops-common: atomically test and clear young on ptes and pmds". On Thu, 18 May 2023 12:07:24 +0100 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: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions") Nit. The commit is only for refactoring. The original code camde from commit 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> Other than above nits, Reviewed-by: SeongJae Park <sj@kernel.org> Thanks, SJ > --- > 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 > >
On Thu, May 18, 2023 at 5:07 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: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions") This should be a separate patch, since it's independent from what the series tries to do. And that patch should cc stable, since it fixes user data corruption. > Signed-off-by: Ryan Roberts <ryan.roberts@arm.com> > Reviewed-by: Zi Yan <ziy@nvidia.com> > --- > 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 */ Use ptep_clear_young_notify(). Similar below.
On 18/05/2023 18:13, SeongJae Park wrote: > Hi Ryan, > > > Nit. For consistency of DAMON commits subjects, I'd prefer something like > "mm/damon/ops-common: atomically test and clear young on ptes and pmds". > > On Thu, 18 May 2023 12:07:24 +0100 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: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions") > > Nit. The commit is only for refactoring. The original code camde from commit > 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> > > Other than above nits, > > Reviewed-by: SeongJae Park <sj@kernel.org> Thanks for the reviews! I'll fix the nits you raised for v3. > > > Thanks, > SJ >
On 19/05/2023 00:19, Yu Zhao wrote: > On Thu, May 18, 2023 at 5:07 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: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions") > > This should be a separate patch, since it's independent from what the > series tries to do. > > And that patch should cc stable, since it fixes user data corruption. Fair point. The first 3 patches are fixes for issues I found during the conversion. So if you're ok with it, I'll split into 2 series; the first 3 patches in the first, and the conversion to ptep_deref() (or ptep_get(), as per your comments in the other thread) in the second. I guess the whole first series should go to stable. Let me know if you disagree. > >> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com> >> Reviewed-by: Zi Yan <ziy@nvidia.com> >> --- >> 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 */ > > Use ptep_clear_young_notify(). Similar below. This looks sensible but I'd like clarification from SeongJae: Are you happy for me to do this refactoring as part of the patch? Thanks, Ryan
On Fri, 19 May 2023 10:02:48 +0100 Ryan Roberts <ryan.roberts@arm.com> wrote: > On 19/05/2023 00:19, Yu Zhao wrote: > > On Thu, May 18, 2023 at 5:07 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: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions") > > > > This should be a separate patch, since it's independent from what the > > series tries to do. > > > > And that patch should cc stable, since it fixes user data corruption. > > Fair point. The first 3 patches are fixes for issues I found during the > conversion. So if you're ok with it, I'll split into 2 series; the first 3 > patches in the first, and the conversion to ptep_deref() (or ptep_get(), as per > your comments in the other thread) in the second. I guess the whole first series > should go to stable. > > Let me know if you disagree. > > > > >> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com> > >> Reviewed-by: Zi Yan <ziy@nvidia.com> > >> --- > >> 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 */ > > > > Use ptep_clear_young_notify(). Similar below. > > This looks sensible but I'd like clarification from SeongJae: Are you happy for > me to do this refactoring as part of the patch? Yes, I would be happy for that :) Nevertheless, because that's a refactoring rather than a fix, I'd be happier if you could do that with a separate patch. Thanks, SJ > > Thanks, > Ryan >
On 19/05/2023 20:54, SeongJae Park wrote: > On Fri, 19 May 2023 10:02:48 +0100 Ryan Roberts <ryan.roberts@arm.com> wrote: > >> On 19/05/2023 00:19, Yu Zhao wrote: >>> On Thu, May 18, 2023 at 5:07 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: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions") >>> >>> This should be a separate patch, since it's independent from what the >>> series tries to do. >>> >>> And that patch should cc stable, since it fixes user data corruption. >> >> Fair point. The first 3 patches are fixes for issues I found during the >> conversion. So if you're ok with it, I'll split into 2 series; the first 3 >> patches in the first, and the conversion to ptep_deref() (or ptep_get(), as per >> your comments in the other thread) in the second. I guess the whole first series >> should go to stable. >> >> Let me know if you disagree. >> >>> >>>> Signed-off-by: Ryan Roberts <ryan.roberts@arm.com> >>>> Reviewed-by: Zi Yan <ziy@nvidia.com> >>>> --- >>>> 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 */ >>> >>> Use ptep_clear_young_notify(). Similar below. >> >> This looks sensible but I'd like clarification from SeongJae: Are you happy for >> me to do this refactoring as part of the patch? > > Yes, I would be happy for that :) Nevertheless, because that's a refactoring > rather than a fix, I'd be happier if you could do that with a separate patch. Great - I'll do it like this for v3. > > > Thanks, > SJ > >> >> Thanks, >> Ryan >>
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 > >
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;