From patchwork Thu Apr 18 13:44:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Yang X-Patchwork-Id: 13634762 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 395D0C04FF8 for ; Thu, 18 Apr 2024 13:45:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B23E6B0095; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 91DF86B0098; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78A736B0096; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 55B4B6B0093 for ; Thu, 18 Apr 2024 09:44:59 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 37B90A1467 for ; Thu, 18 Apr 2024 13:44:50 +0000 (UTC) X-FDA: 82022773140.19.357D97A Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf14.hostedemail.com (Postfix) with ESMTP id 5CBEF100002 for ; Thu, 18 Apr 2024 13:44:48 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="C0cl4/BE"; spf=pass (imf14.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713447888; 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=lkdNcUi/cFtD91tOAv56m6RXgS21TIueDzxwluwn/Lw=; b=szp0bSPkdEdnT71rQdrEkRwnc5jBA9ndxz9zouSke3TXxgdkM/Van08MpCBOXCE9jiENTc Rr73ZnUjIwEgiA5T2GYftLZbMNS1lwgON5kciX7qL6mInonnbFfVuiBY7fOzHoT1AzQmQd uP9RU7sd3gchcVOxKJU/RpqVBkJxq9U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1713447888; a=rsa-sha256; cv=none; b=1exoEAX9wUjM1fvlF+PqRUMac/+Vmn75hjKzwPeyafkh2EQvMnG+Ve2XAzDV6cBvA0Ssyg xSRBD0f/6/GfyXH+qZd7XFP/ydaMSzvrz8VZgBAZVGsEN9ub5AA1OKy1bzw123EPp/BVjI qKK+vaEvEJ19kkzY67lEIXFYvfUBXQ0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="C0cl4/BE"; spf=pass (imf14.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1e504f58230so7813475ad.2 for ; Thu, 18 Apr 2024 06:44:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713447887; x=1714052687; darn=kvack.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=lkdNcUi/cFtD91tOAv56m6RXgS21TIueDzxwluwn/Lw=; b=C0cl4/BEB/5BjZ8Gfi4xnD1UT1UiMLRnntc7+JaI+xV7vsHwPtwKjeDEy0iiy3Loht Q5krdErkStYqwg4ZNZo0BCfshzfeZUQgHpkhjlavVPejcuTcoJFc7/mbpFRNmX/II35W +Eez1cYcZFECNY8PQYxElzlPlpYEw2izA66XHwKWkszQE+Rj8nOMRN5Id/V9C+Ghevor Oo3006hNsituKaTtN6M7y6AraqKkkwqh2WiBXmbwP9M2iSPCz0xZXtJfomF4Y22PBJbV kWFYL9In6SHF9XHvCX4LCU0uXznC/ZNaJkAczw55xMDZnM3rV1zjdrSSADkwVIVgoKV5 SAhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713447887; x=1714052687; 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=lkdNcUi/cFtD91tOAv56m6RXgS21TIueDzxwluwn/Lw=; b=LUh31hOfgiv0/bYY7aNawhliS18F+acKDi9XipceUKkrXAnNcev1LZ+Ii51y7S3QA8 +yR0BeTczZOYliyRPz5Z/Mcn3y03fO0tjr3I9GZz+nsY117HEZhw8U+Pn6NgOcWFSlKt qv41u6ilQpn8pNloiCejPhciRBtEiupze9b/a9pWttG+fxhSoPjT8gpMFLHy8BD5B4cc kCizCbVOq5BaLeYmxLD1caYD0gMuKAM+AHT9EqwCn2q70SvvTpe2n6vGJJ5WjjUe2WFY 0FTDRbxp+gMBhfBQLr14CUTRpOEIBwwRvdECircFBrUmPDIQxgmSr+pABeiXmeegLcsH XWAA== X-Forwarded-Encrypted: i=1; AJvYcCW+fpF5Qvo2LMgmpouNC80k9uSBbGVVWNAoG6HULYUcqWEdBwwMA5SmWOiIUARrJohWl5DIvX1RCwQ7BuLy6RykiEA= X-Gm-Message-State: AOJu0YwXVaQF+vhbx+cF1RDjq91OXKEZKTRKY/unDaGRPKn2Ih2wug90 b1iBVTrJNcK+BvJhzr2vRY8zIKjLo2sPjwq5xY5NKpjcBumD0imu X-Google-Smtp-Source: AGHT+IG2GVYbLpKp4yUNNUcOXfACY+2bHpA67aILzi27vexy/CeFyGpA5xeZ5gYV+u3w513tsrVcFA== X-Received: by 2002:a17:902:c94e:b0:1e7:b775:64bd with SMTP id i14-20020a170902c94e00b001e7b77564bdmr3953276pla.53.1713447887193; Thu, 18 Apr 2024 06:44:47 -0700 (PDT) Received: from LancedeMBP.lan ([112.10.225.217]) by smtp.gmail.com with ESMTPSA id d8-20020a170902b70800b001e4fdcf67desm1504837pls.299.2024.04.18.06.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 06:44:46 -0700 (PDT) From: Lance Yang To: akpm@linux-foundation.org Cc: ryan.roberts@arm.com, david@redhat.com, 21cnbao@gmail.com, mhocko@suse.com, fengwei.yin@intel.com, zokeefe@google.com, shy828301@gmail.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 v10 1/4] mm/madvise: introduce clear_young_dirty_ptes() batch helper Date: Thu, 18 Apr 2024 21:44:32 +0800 Message-Id: <20240418134435.6092-2-ioworker0@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20240418134435.6092-1-ioworker0@gmail.com> References: <20240418134435.6092-1-ioworker0@gmail.com> MIME-Version: 1.0 X-Stat-Signature: hrjrimmzguqi1njy7fjirj59wj6g6dgp X-Rspamd-Queue-Id: 5CBEF100002 X-Rspamd-Server: rspam06 X-Rspam-User: X-HE-Tag: 1713447888-828863 X-HE-Meta: U2FsdGVkX19fPN7vH/F0xOcZIAvuhocMi7I2F/TEz0DH1bnDUZS9hhs3EKTmq5f9T9cXkBzORf6p6vZMqVMajz8OcJZ4fuDvjt1pA2Be/LCe2ZZUBmDmbWfAXSPdgo9X3Isp7Vjn/rEVzedX2PDl3raI9XLpkLUTwZQZl+Fs6aoxRsGwkje6RKNcEoH+LJG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hpBrkM9TgzCEzHv4bsI0kFKxpX1lCHE0O4QetRkr45Ey8I6DmOtGnNu9KSHWz7Cez7+yG//OMxoTUJLxzGpfXdR5WMIA74U2/a5r+wEwoDiMxo3DWjz6IifWICd8EV+d1enLTHCn5aPFN3GDFZ47PV1TeVQ4SZL147xM1yVwkZx+qUppnHd7L1NqOQvwGZhL6dpeL+RqgXq1gdzMWn5x5oFIPEB5NFSK8dyJVe0F0/XKXeulHAAYEP5HkVKo0gE/ZwrvURH5ogMcz9yPkzKCls+td 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: List-Subscribe: List-Unsubscribe: This commit introduces clear_young_dirty_ptes() to replace mkold_ptes(). By doing so, we can use the same function for both use cases (madvise_pageout and madvise_free), and it also provides the flexibility to only clear the dirty flag in the future if needed. Suggested-by: Ryan Roberts Acked-by: David Hildenbrand Reviewed-by: Ryan Roberts Signed-off-by: Lance Yang --- include/linux/mm_types.h | 9 +++++ include/linux/pgtable.h | 74 ++++++++++++++++++++++++---------------- mm/madvise.c | 3 +- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index db0adf5721cc..24323c7d0bd4 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -1368,6 +1368,15 @@ enum fault_flag { typedef unsigned int __bitwise zap_flags_t; +/* Flags for clear_young_dirty_ptes(). */ +typedef int __bitwise cydp_t; + +/* Clear the access bit */ +#define CYDP_CLEAR_YOUNG ((__force cydp_t)BIT(0)) + +/* Clear the dirty bit */ +#define CYDP_CLEAR_DIRTY ((__force cydp_t)BIT(1)) + /* * FOLL_PIN and FOLL_LONGTERM may be used in various combinations with each * other. Here is what they mean, and how to use them: diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index e2f45e22a6d1..18019f037bae 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -361,36 +361,6 @@ static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, } #endif -#ifndef mkold_ptes -/** - * mkold_ptes - Mark PTEs that map consecutive pages of the same folio as old. - * @vma: VMA the pages are mapped into. - * @addr: Address the first page is mapped at. - * @ptep: Page table pointer for the first entry. - * @nr: Number of entries to mark old. - * - * May be overridden by the architecture; otherwise, implemented as a simple - * loop over ptep_test_and_clear_young(). - * - * Note that PTE bits in the PTE range besides the PFN can differ. For example, - * some PTEs might be write-protected. - * - * Context: The caller holds the page table lock. The PTEs map consecutive - * pages that belong to the same folio. The PTEs are all in the same PMD. - */ -static inline void mkold_ptes(struct vm_area_struct *vma, unsigned long addr, - pte_t *ptep, unsigned int nr) -{ - for (;;) { - ptep_test_and_clear_young(vma, addr, ptep); - if (--nr == 0) - break; - ptep++; - addr += PAGE_SIZE; - } -} -#endif - #ifndef __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG) static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma, @@ -489,6 +459,50 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, } #endif +#ifndef clear_young_dirty_ptes +/** + * clear_young_dirty_ptes - Mark PTEs that map consecutive pages of the + * same folio as old/clean. + * @mm: Address space the pages are mapped into. + * @addr: Address the first page is mapped at. + * @ptep: Page table pointer for the first entry. + * @nr: Number of entries to mark old/clean. + * @flags: Flags to modify the PTE batch semantics. + * + * May be overridden by the architecture; otherwise, implemented by + * get_and_clear/modify/set for each pte in the range. + * + * Note that PTE bits in the PTE range besides the PFN can differ. For example, + * some PTEs might be write-protected. + * + * Context: The caller holds the page table lock. The PTEs map consecutive + * pages that belong to the same folio. The PTEs are all in the same PMD. + */ +static inline void clear_young_dirty_ptes(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + unsigned int nr, cydp_t flags) +{ + pte_t pte; + + for (;;) { + if (flags == CYDP_CLEAR_YOUNG) + ptep_test_and_clear_young(vma, addr, ptep); + else { + pte = ptep_get_and_clear(vma->vm_mm, addr, ptep); + if (flags & CYDP_CLEAR_YOUNG) + pte = pte_mkold(pte); + if (flags & CYDP_CLEAR_DIRTY) + pte = pte_mkclean(pte); + set_pte_at(vma->vm_mm, addr, ptep, pte); + } + if (--nr == 0) + break; + ptep++; + addr += PAGE_SIZE; + } +} +#endif + static inline void ptep_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { diff --git a/mm/madvise.c b/mm/madvise.c index 4b869b682fd5..f5e3699e7b54 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -507,7 +507,8 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, continue; if (!pageout && pte_young(ptent)) { - mkold_ptes(vma, addr, pte, nr); + clear_young_dirty_ptes(vma, addr, pte, nr, + CYDP_CLEAR_YOUNG); tlb_remove_tlb_entries(tlb, pte, nr, addr); }