Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp5984955rwp; Mon, 17 Jul 2023 12:47:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlF8f0aJrcCJuFEvZXU46mzLlJDV3PmkwiXvzEni52uJPqJsXnkepRnZm2Lev6NWNoLgkZYO X-Received: by 2002:a17:906:ecf5:b0:993:dd0d:10a5 with SMTP id qt21-20020a170906ecf500b00993dd0d10a5mr11605040ejb.22.1689623227752; Mon, 17 Jul 2023 12:47:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689623227; cv=none; d=google.com; s=arc-20160816; b=eg7YmvPbEkD2SuRqpMtuci7gW3IWPdhPXZmWFQbkSI8ToJsWlkWUDxNLqt0ZGhRcgR p5eNdkLes0acU7RRWSqXiMtG1bO7PY6qtJ8xV+zNQ4n5oeysb8ENlpQhNrY/3s08bsmB 6xMkfaiEZ/3Rox7plW69wmYENIzddO8ZhqivG39PQCWyVgAYhrdqBc48N/PlZRq+HbsE +naJ5IVEh3/32CnKRoAc6eV0qoKQCD2IhfMAn7Za/aA33f6ecRURpaLNPa54mq3uSUwm rQrvXXc2AKg0gc8KpQJ9CCxd8/SJE6Z+s/bPR8rYldK2YfUI8l7/U/Xvo315mlIRWwwY Lwtg== 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 :dkim-signature; bh=Xv118nvDdsWwnQIi5JMMBX7BpxATFP1qjTLSaokOlnY=; fh=Kqehjqvk7+B/CEnFvgNcqm2A3EidkYTERPX/LUBlH2w=; b=ZRLEBd4mwQmAwsFS1ThY+yPfOjWhTESO0JMzly8JVHlml1xYwl+7JBBp17R5RbMJgQ MIkPpxw/Vq45PuXkkdc9EMWSRtrLM3QfcyEyeSSjr1NnlhtIevt9Yaw7lztetciZgxA1 cYnIsSdlqMbiZV/4dYFbF3zD15K6sRkoPX1dnL7aXIFc+ik7wscpGUNk9fHCC+THUarR NDMiBYVZbwvlIgK7A5Y2j+yY1mFacV43qW+orrFGohV6tAlmXw2UNT+nFvsK/CMbQJBN hbrgTOoXcKmG+4xSn+rjUp4BtLijkwaBN7d0BTx5bp8xgb4Mu0fD1CsbEUpXsS1HC3p3 R15w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tZBQ0hW1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qp15-20020a170907206f00b009936880c9afsi88302ejb.377.2023.07.17.12.46.43; Mon, 17 Jul 2023 12:47:07 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=tZBQ0hW1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230195AbjGQTaZ (ORCPT + 99 others); Mon, 17 Jul 2023 15:30:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230522AbjGQTaV (ORCPT ); Mon, 17 Jul 2023 15:30:21 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5EC18E1; Mon, 17 Jul 2023 12:30:20 -0700 (PDT) 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 EE9E86122B; 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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: X-Mailing-List: linux-kernel@vger.kernel.org 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; -- 2.25.1