From patchwork Mon Jul 17 19:30:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13316517 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 AAFCDEB64DC for ; Mon, 17 Jul 2023 19:30:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 298C08D0001; Mon, 17 Jul 2023 15:30:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 249696B0074; Mon, 17 Jul 2023 15:30:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 110C18D0001; Mon, 17 Jul 2023 15:30:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F318B6B0072 for ; Mon, 17 Jul 2023 15:30:23 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BDAA1140532 for ; Mon, 17 Jul 2023 19:30:23 +0000 (UTC) X-FDA: 81022095126.15.D3BD19A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id E807E8001F for ; Mon, 17 Jul 2023 19:30:20 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tZBQ0hW1; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689622221; 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:dkim-signature; bh=Xv118nvDdsWwnQIi5JMMBX7BpxATFP1qjTLSaokOlnY=; b=0D0MqUSNdGSPx3C+wh1z/VvcEIjBJkZNUec5gpCkbr78ZbzK6WJteq4zZo/G43HHhqVrQa HjeCmUfLVjEmgccV76OnQne0rhAb/6k+YhP2FQfJLCgtYGJhs2xBLCHDA7J5lrNzSNl5IZ OvOMHhhPBQF5jW8Y1U9X0kfPPZoTZ7Q= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tZBQ0hW1; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf30.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689622221; a=rsa-sha256; cv=none; b=yyLVIFGD9ZNoSrU+nhHw13yos8GpJTAeyS7Ztw9AK7IVQyZwnOp7gmfnTEIMqdxfz5Agj/ 50hrAdgn1y2Vr/pnybcyY73gQVVDNR7esj/Y95WrUygIKPU+93pJ4CsdZUicVvVCXSxdDS NFWbom/xwb17Tu84wUyod7latU0viic= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E48506120E; Mon, 17 Jul 2023 19:30:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 282DCC433C8; Mon, 17 Jul 2023 19:30:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689622219; bh=B3lKcl6AG4VzLC8vKc3oOwsXsHm9qE0hLnyJCufjkwQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tZBQ0hW12YiHopMYlgU/iscjagmj++kFnsMTRjCXXss4RiQSZmC+t1AoqGLtwSH/Q ApRemcZP1v8V0m6UAxS1FT0C/s3o6IyBBx980E6uz1JTeeg30qeLdFNpeh9eiAS9UO ypGzf+fanyWIUa5996eXCNxKos8nrQJD3hXn+b51BlcSEZIg2QtnqOntZITca/GM3F rWSt60S2igAtbmHSzNBLFkG3/oW96LvlEHPby+FkCu4ayFcG44Y4eOBUCl49O4AGum zt7XLhr0V/n4IPtlkN1uJL/Pb9V6soEQ9Rdl6aqDNqN/1So3uIzt8vuoZTSC0w4ClN Q5VPS8kggiT0Q== From: SeongJae Park To: gregkh@linuxfoundation.org Cc: ryan.roberts@arm.com, akpm@linux-foundation.org, hch@lst.de, kirill.shutemov@linux.intel.com, lstoakes@gmail.com, rppt@kernel.org, sj@kernel.org, stable@vger.kernel.org, urezki@gmail.com, willy@infradead.org, yuzhao@google.com, ziy@nvidia.com, damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5.15.y] mm/damon/ops-common: atomically test and clear young on ptes and pmds Date: Mon, 17 Jul 2023 19:30:08 +0000 Message-Id: <20230717193008.122040-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <2023071613-reminder-relapse-b922@gregkh> References: <2023071613-reminder-relapse-b922@gregkh> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E807E8001F X-Stat-Signature: oed1dgemenm91ayw5qbzog1frdjkth6r X-HE-Tag: 1689622220-15529 X-HE-Meta: U2FsdGVkX18HmfCg89rNZIeFuX4r7VWTcBT4jeE9mhpO90EH2pdo9L1HqmNRMHcA1maeqW2oSB2/xEPIrhBW66oq6m2MGWzzWyWkfqfPcO2SjSQHV+5i0CouGA0oV4lmZJasYtdisw/OP+PATzG3t5YE3tKv6ArfcPcMNP2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qSqGJ5YTfQTKl1QBeRz9MlYhOoL7rUoQI4xVKHVOwS3pL+oconUJMOoIFpXmRDgsgtPuCxumevrWlqPRl5+nAaPXTLYhArkg2v5HmAPNPtumRFlCdiHDjYRx4ZvlN/1VYJgvQsRwFgcrhs3sDLQK5GgtrVlnC9NNDAdFuXHCUUaRPtP2SsUUg+LUkHLwFSbzw4BeNAFdEEm2deOyowIxZeYNA5hWye+VUChdEn8Tj3Cwvk2wWswHXFHJ4XXDUbZ9tMzAimxh4RwMragTIyY2pddbGS/c5T5RZSteEiqiurvlFQMCVf9rTTQYDjWtgeF2Id+uzjKjkzJ8pHmmDtblsSbAK1uiE8hnWrur8HfcE= 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: From: Ryan Roberts commit c11d34fa139e4b0fb4249a30f37b178353533fa1 upstream. 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. Link: https://lkml.kernel.org/r/20230602092949.545577-3-ryan.roberts@arm.com Fixes: 3f49584b262c ("mm/damon: implement primitives for the virtual memory address spaces"). Signed-off-by: Ryan Roberts Reviewed-by: Zi Yan Reviewed-by: SeongJae Park Reviewed-by: Mike Rapoport (IBM) Cc: Christoph Hellwig Cc: Kirill A. Shutemov Cc: Lorenzo Stoakes Cc: Matthew Wilcox (Oracle) Cc: Uladzislau Rezki (Sony) Cc: Yu Zhao Cc: Signed-off-by: Andrew Morton Signed-off-by: SeongJae Park --- This is a manual backport of the commit, which cannot cleanly cherry-picked on 5.15.y[1], on 5.15.y, specifically 5.15.120. [1] https://lore.kernel.org/stable/2023071613-reminder-relapse-b922@gregkh/ mm/damon/vaddr.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/mm/damon/vaddr.c b/mm/damon/vaddr.c index 1945196fd743..6ad96da15081 100644 --- a/mm/damon/vaddr.c +++ b/mm/damon/vaddr.c @@ -393,7 +393,7 @@ static struct page *damon_get_page(unsigned long pfn) return page; } -static void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, +static void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr) { bool referenced = false; @@ -402,13 +402,11 @@ static void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, if (!page) 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 */ @@ -419,7 +417,7 @@ static void damon_ptep_mkold(pte_t *pte, struct mm_struct *mm, put_page(page); } -static void damon_pmdp_mkold(pmd_t *pmd, struct mm_struct *mm, +static void damon_pmdp_mkold(pmd_t *pmd, struct vm_area_struct *vma, unsigned long addr) { #ifdef CONFIG_TRANSPARENT_HUGEPAGE @@ -429,13 +427,11 @@ static void damon_pmdp_mkold(pmd_t *pmd, struct mm_struct *mm, if (!page) 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, + if (mmu_notifier_clear_young(vma->vm_mm, addr, addr + ((1UL) << HPAGE_PMD_SHIFT))) referenced = true; #endif /* CONFIG_MMU_NOTIFIER */ @@ -462,7 +458,7 @@ static int damon_mkold_pmd_entry(pmd_t *pmd, unsigned long addr, } if (pmd_huge(*pmd)) { - damon_pmdp_mkold(pmd, walk->mm, addr); + damon_pmdp_mkold(pmd, walk->vma, addr); spin_unlock(ptl); return 0; } @@ -474,7 +470,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;