Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp254062lqp; Fri, 12 Apr 2024 17:23:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX6MzR2YYc7ahAunFvaxSuo841OyF3tHfh8SFT5z2FX9CKrF3A8XW6LQUm+CAhjvmibY/dO0W68qp+abEsWHDga4z4J8ilS4Sbh52gPlg== X-Google-Smtp-Source: AGHT+IHivEFhersESeBzX+VIsD26O+A8mfqFN4QqNwOBD5EDSFZHRdvCCh3xL4FIJvBcCNaVHm4j X-Received: by 2002:a17:906:1c1b:b0:a52:1635:9d16 with SMTP id k27-20020a1709061c1b00b00a5216359d16mr4582319ejg.38.1712967799697; Fri, 12 Apr 2024 17:23:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712967799; cv=pass; d=google.com; s=arc-20160816; b=oIyocEhLQDjpb2AAVlgSpkZ1rZ7ZUXtotHdx6IJlbeicD6W56rhrcA/AKkLhIUnVNx TwducfzWKE9OYGKaa3cWNOXxwXXUPI7zzPx/leSmjG++KgQi59WNcMT78YYQkRCGrmjZ VJ7IjuMih6209P/DZWaBuhDqUnIx/I6bpNnOXZl4LytrQer7xGkg+fFI2hE0NXgFrpjL dwxrssZXF7gPS0wQt8JRkU2VOpu+EWL04VXtlPK4wuHaVZYGtbcj8dwu6BIXUafTHkj6 MHKBfBhsnEDWyrILS0mPkMJJ1yCK3GsWbr1IFQMJMHXOJZCngJ108vtAU95qz75hl+Fo 8Hvg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=1OvPRCdhoiay+bQidKgk1FXNIyrlSTLCMUCQGI9Z8wE=; fh=PSzX6BWkSn1zGrwEBoy6noPElhCgaGAkIayT8vPPlJw=; b=d/hDzfBNmOQQuN3RMzJZpHOLnEkeBHIe77DtfY+dXQ8Kosq3vDG23K9G+4MCctACaZ qOx5H9WtLdv5ch598tTtJPsOg0hBFb2aYbBY4RsZk/K97JiaxaPAic357W+ubIeGIE7j u1W/gPE7WoCtTguTN3WFobByzGepwDHiEcXE5Kym9blMET3B+g2QJJy8A1gIY+ZCK7EP dtEQXWGwmZDwFnOAKJFSoxQJxANGmQk69mqnkpcsZzlrsW5kqFXt5v/hag9nxVvKdMRr NrRdB0WQ28Kww5FubsF5DtaZ0DNG4KXxFizOwP/A3DSQmYTDzTYOJw22IpHqMdz1n5v0 x8fw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=eA163uBU; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-143441-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143441-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id p25-20020a170906229900b00a51a6c09d37si2147756eja.557.2024.04.12.17.23.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 17:23:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-143441-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=eA163uBU; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-143441-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-143441-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 45F9F1F229F1 for ; Sat, 13 Apr 2024 00:23:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E51594C97; Sat, 13 Apr 2024 00:23:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eA163uBU" Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96F334A24 for ; Sat, 13 Apr 2024 00:22:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712967781; cv=none; b=jrdjbT4d0ol1sw9kQn7fl3CJbLj/Jld3yIxXIecC/Dx2F18NEcXWuPfz5WGXYQe0OxWv0hIk21x3wCh1g0mqrhenJs7W7rxPkjmUmnjrEaD4q9H/lQbBnlwAVVfXNu3BO/eVY7MExgw5eotBkc5ZAL/XslZ3/tZ5wo6qtKnbCoM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712967781; c=relaxed/simple; bh=KF/2qsAtMNyFsi0x5yQQgTmdnX4ZegKINAVOh0xmpXs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AFynXGrrvfcgADQCIB1RSAv9IL0VZW7bu4rklx+fclPhpIGMFx8aWzQs9orosmBzXfMi1ufMuh+ucEJxsSAyue5Dec8rH7Nz3vz5n+WUqFLlq151AJSCQfNahYIXxXsfGQOmRjAzJtPenyJ86qy30CMOI+ZkZMEEo7Q84yQWO3o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eA163uBU; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1e3ff14f249so11126195ad.1 for ; Fri, 12 Apr 2024 17:22:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712967779; x=1713572579; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1OvPRCdhoiay+bQidKgk1FXNIyrlSTLCMUCQGI9Z8wE=; b=eA163uBU9qFga5qEnpwMCjq6dKE+aTEg8KemKiU0mpATuAq0c9L5VvM1CyGLjf4AR1 +WLRXAJ0cH1HW0fRJkuBW7BW7/GyeoZBR69B4G81kFrhrLljwkvfgap1YdP6cFuibHUq 2He6/iB/qRAg9zxWnlGvZo1VZ5a1L7ulO6aRn0uwfFBJl3rZ29tWJgcABuxDBMPtEdmk So1B7ZxfUPHJPsDjrvD+GpGBSS3R9i550WxDuGZ4r/J+89107ku9o9+w6Ce44zuGbK5r 2Y3nfsHxqveaxkWsE//inAd37RfwZ/+CRqcpsP02mRUnTgOjAqxdsai3qK1IN02OmYLE aUdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712967779; x=1713572579; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1OvPRCdhoiay+bQidKgk1FXNIyrlSTLCMUCQGI9Z8wE=; b=TMpoPiNqUctgInZikZ55r8meGAp2a6sOeJCFyF9fyRVMhx2/aHnWFpfqGZFHIZmEwu WFI9der9qmsfZPgIov++AN8w4kaNUszpBcDn8uMU5+/n4uKP0MJBpFVRQ97F5KdYbVfk Z1ekPuCZ5lGmBvaQuR0WdzslRYH5ogUR1CIiEqyzFx4tfqvK8RddCifmPIWewVS/oVeo J2nQrwEwIMu/s5cRmciFsLEbfJbg3foB8mqfSsh58kdmKfY1xM8ltfdnZOKnFr0EyaKw Af28u0HQ1yV5r0v4TIFlwRuPV/jqPXXzyGaac4omQOKUj1MEHEDglKPN5adLqu0LOvVY GuAA== X-Forwarded-Encrypted: i=1; AJvYcCX/SmS3dUn0yjJ9ncXmXirYDFyUWi5hOKuFGuJJ4iqTt6IItj7+qo1xJgHGidABIaF0SLywJBM35mpOHz6OOwn2FBkRTlyszEpHsNNp X-Gm-Message-State: AOJu0YzQ93vQ4a9HD9saJH/TDitSb9Q1xKnxmnJ55oMnEXM3WRcYNAde pzlrnUJhx/cpsAqaKBYgQsejYjiCVsXqQ/6QUq6ZZ9sxVybA2cwU X-Received: by 2002:a17:903:2443:b0:1e5:62:7a81 with SMTP id l3-20020a170903244300b001e500627a81mr5783849pls.22.1712967778837; Fri, 12 Apr 2024 17:22:58 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.217]) by smtp.gmail.com with ESMTPSA id a17-20020a170902ee9100b001e2a4ac7bf9sm3569618pld.111.2024.04.12.17.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 17:22:58 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: zokeefe@google.com, ryan.roberts@arm.com, 21cnbao@gmail.com, shy828301@gmail.com, david@redhat.com, mhocko@suse.com, fengwei.yin@intel.com, xiehuan09@gmail.com, wangkefeng.wang@huawei.com, songmuchun@bytedance.com, peterx@redhat.com, minchan@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Lance Yang Subject: [PATCH v6 2/2] mm/arm64: override clear_young_dirty_ptes() batch helper Date: Sat, 13 Apr 2024 08:22:19 +0800 Message-Id: <20240413002219.71246-3-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240413002219.71246-1-ioworker0@gmail.com> References: <20240413002219.71246-1-ioworker0@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The per-pte get_and_clear/modify/set approach would result in unfolding/refolding for contpte mappings on arm64. So we need to override clear_young_dirty_ptes() for arm64 to avoid it. Suggested-by: David Hildenbrand Suggested-by: Barry Song <21cnbao@gmail.com> Signed-off-by: Ryan Roberts Signed-off-by: Lance Yang --- arch/arm64/include/asm/pgtable.h | 37 ++++++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 28 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 9fd8613b2db2..f951774dd2d6 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1223,6 +1223,28 @@ static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address, __ptep_set_wrprotect(mm, address, ptep); } +static inline void __clear_young_dirty_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags) +{ + pte_t pte; + + for (;;) { + pte = __ptep_get(ptep); + + if (flags | CYDP_CLEAR_YOUNG) + pte = pte_mkold(pte); + if (flags | CYDP_CLEAR_DIRTY) + pte = pte_mkclean(pte); + + __set_pte(ptep, pte); + if (--nr == 0) + break; + ptep++; + addr += PAGE_SIZE; + } +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_PMDP_SET_WRPROTECT static inline void pmdp_set_wrprotect(struct mm_struct *mm, @@ -1379,6 +1401,9 @@ extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty); +extern void contpte_clear_young_dirty_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags); static __always_inline void contpte_try_fold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) @@ -1603,6 +1628,17 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty); } +#define clear_young_dirty_ptes clear_young_dirty_ptes +static inline void clear_young_dirty_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags) +{ + if (likely(nr == 1 && !pte_cont(__ptep_get(ptep)))) + __clear_young_dirty_ptes(mm, addr, ptep, nr, flags); + else + contpte_clear_young_dirty_ptes(mm, addr, ptep, nr, flags); +} + #else /* CONFIG_ARM64_CONTPTE */ #define ptep_get __ptep_get @@ -1622,6 +1658,7 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, #define wrprotect_ptes __wrprotect_ptes #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS #define ptep_set_access_flags __ptep_set_access_flags +#define clear_young_dirty_ptes __clear_young_dirty_ptes #endif /* CONFIG_ARM64_CONTPTE */ diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 1b64b4c3f8bf..bf3b089d9641 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -361,6 +361,34 @@ void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, } EXPORT_SYMBOL_GPL(contpte_wrprotect_ptes); +void contpte_clear_young_dirty_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, cydp_t flags) +{ + /* + * We can safely clear access/dirty without needing to unfold from + * the architectures perspective, even when contpte is set. If the + * range starts or ends midway through a contpte block, we can just + * expand to include the full contpte block. While this is not + * exactly what the core-mm asked for, it tracks access/dirty per + * folio, not per page. And since we only create a contpte block + * when it is covered by a single folio, we can get away with + * clearing access/dirty for the whole block. + */ + unsigned int start = addr; + unsigned int end = start + nr; + + if (pte_cont(__ptep_get(ptep + nr - 1))) + end = ALIGN(end, CONT_PTE_SIZE); + + if (pte_cont(__ptep_get(ptep))) { + start = ALIGN_DOWN(start, CONT_PTE_SIZE); + ptep = contpte_align_down(ptep); + } + + __clear_young_dirty_ptes(mm, start, ptep, end - start, flags); +} +EXPORT_SYMBOL_GPL(contpte_clear_young_dirty_ptes); + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty) -- 2.33.1