From patchwork Thu May 11 13:21:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13237923 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 D2340C77B7F for ; Thu, 11 May 2023 13:21:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 661D66B007E; Thu, 11 May 2023 09:21:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 63B6E6B0080; Thu, 11 May 2023 09:21:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 501FC6B0081; Thu, 11 May 2023 09:21:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4038C6B007E for ; Thu, 11 May 2023 09:21:30 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B174740540 for ; Thu, 11 May 2023 13:21:29 +0000 (UTC) X-FDA: 80778035898.20.F2A7854 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf05.hostedemail.com (Postfix) with ESMTP id 6FA91100024 for ; Thu, 11 May 2023 13:21:26 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; spf=pass (imf05.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=1683811286; 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=/l9fYJydCynmWpkjQY10Gzcv8GVW73Wd7YwWqJyDcrI=; b=oCJaK+jeacL6YhKszZ7d8ErKZAZncTQ2ojkjQN88e2OBrDmA390u93D2RaxwXEUlh3K0cx 1E/C5k4AdybI2jS5FNbAhXPgZhwVjj1cUm3obEsUF0Me5Aas9A1G4okNg4/YQMRnzlzKTz ZHdmFZAhMAqpIuShnkA9eo32k3HdSQY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683811286; a=rsa-sha256; cv=none; b=Cuss1uLztQ7DvVdojAHVNPAOijlKQaYVkHV7f1UljlU/kbhnTjE7Q9YuGBaCl9+R3FPgkS YrTWsMyu8Urn7JO6fcfX/NjuuOi9+e2JbXSnXBsqL5iPvkmh4uQ+oVBiEh0oeQ0WPTir3S w/tI+L5ZjIqmL0PVCUKOp9mX8gNUu1I= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; spf=pass (imf05.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 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 127C21682; Thu, 11 May 2023 06:22:10 -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 6BFCC3F5A1; Thu, 11 May 2023 06:21:24 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , "Matthew Wilcox (Oracle)" , "Kirill A. Shutemov" , SeongJae Park Cc: Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org, damon@lists.linux.dev Subject: [RESEND PATCH v1 2/5] mm: damon must atomically clear young on ptes and pmds Date: Thu, 11 May 2023 14:21:10 +0100 Message-Id: <20230511132113.80196-3-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230511132113.80196-1-ryan.roberts@arm.com> References: <20230511132113.80196-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Stat-Signature: 5qkzog736xpdc7i74rzy81tbg7d1i7di X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 6FA91100024 X-HE-Tag: 1683811286-219681 X-HE-Meta: U2FsdGVkX18zGJKyKJC6FVp7Hi3pHh4keVtciOJiVzh2G54NvrUKgsrfh8L/mWgaTjVrhXE9NJGk0TBbxhA7xfWrUdeMssoum0/YF/rYGGI3QkBolui7+j99WSN5ktjVeRzYDF5KSB013SynXiVZzCl8lYLGU+KEoXOzF1dDrc+Qo5PfioBcVp5d8Zc0x1SpuJgtRLE++Z+8MVAcM6mFcfMHiVQavfvs0pAE1Q8fJHPxQaT2GkBs3Wl2Z64DH3WLPcin+ZXMeRSYcYZnwGzmdmhHnkJNjyyukdLSy1w2QmIvmaI9y0uPcllh01QLlFN4ooBfPTDBZc57iqkBitp3TvlyTQq+6YXznAUM4uH8an3XWxq9E1RA4fyZRZRJeMV+V/kwrPLfzFRnMxz/4ntpZVRiMobl+tGbOiHo4pWQoSH+W/+UB5LbMGpGvG2OGPPu5VtePBy7nqrjo4+7LD18HoE52RP7R4fqcYK5U3jlbFkwZJC7kiiCYCiL0zhtdgU57qMzNA/za8tEknvPMFAIndzsnlFiZ4Fd7I/m/WV7ysuBK8jxI3y4qEIp1UwU7yPEzzIAmIl6nYlKofy0mQLcX0XVN0h9S5LQhioX9mPdg8N4bS7VWdhIsUrk6Xsos2oXVMQJPRwWZTRPS9m9Fr1xi0MPQ/eimiza+baURcjuUl9ZUR/fUsdiI3YppRcCWCKw4HKgxinqNQlrL1H8f+/M2lTSW79WLPgnIBpcC7d1XCWtsyi8umR97rVkDiytpUg0o+OGkaaZ5q/GErkrNJ4Lt9n4xTDtYicJdX+VC8KlG/OiCcc3q6EydYPbOfLR3SWtL7AVwhTisFNWarGyoyXR9PVBkbkMKOYbfJQQA2bHfpmbcSqMxAlLOUfIDtaD97epvjMe6zUbU4B8ZN4Jv4qZrfIMMP9/gSRd/vWjZkiImi0/SaPOj9w+KsarTdgHswPfsY5aOB2r5NJYBfn9nhy 65HFhbgj stIUGaV5Kz21aeImqp5VI/kVMH9ALl7yTYvhyoBdPzXAHZvknSBopVxnbdNF4zvF2oSnerMIqhbYOnBRiOZlGASoDXpnbRTjMQrrLklIzSMdpCe6vPKwMr4XT3ADH7H2xy39JR0yjWWRVMnDGLds9WdOq6g== 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 validate (and potentially modify) the operation. Fixes: 46c3a0accdc4 ("mm/damon/vaddr: separate commonly usable functions") Signed-off-by: Ryan Roberts --- 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(-) -- 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;