From patchwork Thu May 18 11:07:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13246484 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E7D5C77B7D for ; Thu, 18 May 2023 11:07:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF14E900007; Thu, 18 May 2023 07:07:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D0316900003; Thu, 18 May 2023 07:07:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC92B900007; Thu, 18 May 2023 07:07:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id AEBF9900003 for ; Thu, 18 May 2023 07:07:44 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 83B1BA0815 for ; Thu, 18 May 2023 11:07:44 +0000 (UTC) X-FDA: 80803100448.01.6C7A402 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf15.hostedemail.com (Postfix) with ESMTP id 9619BA000D for ; Thu, 18 May 2023 11:07:42 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684408062; a=rsa-sha256; cv=none; b=5pINLx3LUTbGshrjm4gGUQNSV7mMONbT90cPMDvpBWZMtk3irNXXmnPTrLNY0r4mp94xG8 FUtjWZsqvTQZmAmaWsHrTpijwPgjdcJGBH4pmVeCRRVd9boV1nDZjxCkfQdDUm65YbX2+a BF44Uf3uwhYtOFMl/MvnmRZQ9m2Ms3Q= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684408062; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iyeo2oFz3wRj1jfkhI8YHLr4XW9LbfJm+AYYq9yjljk=; b=4wHGKLDpSnQTZ0x4Ch/y2+KTYV2Pkz+OjtxGQODMOO5vQktr7LzbpmEG+zZHcf8lXcSpPt sCLnWjT4mrq6vlPDGIsJRuvpCTJY/nLqHPUhtW68JIGwFFMFs7wHNz5k5j+yRktrEsgNXP nymn6NQYrIojmlwow2LZhVkcTSgbnt8= 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 To: Andrew Morton , SeongJae Park , Christoph Hellwig Cc: Ryan Roberts , "Matthew Wilcox (Oracle)" , "Kirill A. Shutemov" , Lorenzo Stoakes , Uladzislau Rezki , Zi Yan , 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 X-Rspam-User: X-Stat-Signature: qoau5d48okqh8qycaqo5owb5894occt5 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9619BA000D X-HE-Tag: 1684408062-504052 X-HE-Meta: U2FsdGVkX1+snYr6qWWjNxDTDQowbZDXSrS07zesXR/QUHatTkCyPoxwIsjKcBZjxtuvNIXuHJprTCg2465igL0wqMWtm5PWq7snpi0Es8ahfABSa3mQJLI2vpSfv2kuLwZhZTBEE6TbQ7WhvPSvgE/hb90XZDlgKojUIP7Db7nEb/d86MNjnjE2sCEsuEYgAc+0PYdWILvBW66XY5HuHMmCUuQy7JJBhso488K5LvSn3KqPVZvolO/4lQS17RkKNJw+669BIVH2HSK6EBT7qgwXB75lEmpWKUshxaOyVrzIqlxcCpNPW3uavt1eOLTkxIRUUjMPqgrMMNbkriNeZDVO9SdVwpASkb9dNfxkKVH3p4apbdySP8uIWupIxXz34qnh7c2FJXS5+NVNkh9dudTQZbrC7LAU6yTIPDCkPK8atSFSnOmk3Za0Oszt4pqjha07W0RZppLEWJBRmgJN8cdsV/DnGn0vLYBpkx/gPrb4P3PjwlRlKNCaiabVlonluM+RELxMrmSeHkNsRvO/zkFv6yAxbq3zI0aFx8ToA2dnK2Q2bBXHbLBcv32ktAchyykyXBHV7choCJm7AvzFT1bBcFbnLrv7TS2iKl6jl2AYll0N5xpp3hHqQUeir0BKWr/hxx2Pf5Y7y9S5461bBO/bwxQEvB9HPD7ILzCrQvTYTD2uPcfDJBnaVByF485OcHRNhh5jq+9kqSzXh7Wo6QIe1SQC01APDnzXxk5TfM4CLrJqBErYZgO3QcakITSp21W9gWrTCZVzrMvg+WnbVDe6/2UCd2I4RVvmHm+8/msyQek/SP93crk2opLeWhduNK7yxjsdxQwRJAKS4K+oYjG/xdzYHIZU062gTV9iDMEtWg4z19zuwOtuYdkVO2z7NmZC+2rCWpsBvpAP0ww5lljiGskMqdTwxRBbocNUSNDVH624MJ3oWo1KwmeoA8GVephj4LwsPecCnqqH5uC 4Ydsf91N Poe9gL77Le+gvGkflwFaRC9yvLDQ0HjpVTbadcbuY2SKxgCea1GrJi2BRRFKEVHpnfZDFJR+1ExrKZm4wRslO8XeL+BZfhs3HI/fIaJr0ayk7ayWWlqYWdc8fBAnHxReRtWf9iDcR3xu1XLvPxBciIkvtVjhwU9lOlROcpquwjP0goMO3SFxhgpgwYg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Reviewed-by: Zi Yan Reviewed-by: SeongJae Park Reviewed-by: Mike Rapoport (IBM) --- 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;