From patchwork Thu Jun 6 03:40:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13687792 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 4C2FEC25B75 for ; Thu, 6 Jun 2024 03:40:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D39AB6B009B; Wed, 5 Jun 2024 23:40:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEA1F6B00A3; Wed, 5 Jun 2024 23:40:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB1536B00A4; Wed, 5 Jun 2024 23:40:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9E5686B009B for ; Wed, 5 Jun 2024 23:40:38 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 587AC1A0AD7 for ; Thu, 6 Jun 2024 03:40:38 +0000 (UTC) X-FDA: 82199061756.05.5716EB1 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf07.hostedemail.com (Postfix) with ESMTP id 955044000B for ; Thu, 6 Jun 2024 03:40:36 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=m1llcncS; spf=pass (imf07.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=21cnbao@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=1717645236; 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:references:dkim-signature; bh=m0xWv0/mIfZjELLkhKXh+CJWnTqcZ0qlcrVWS0o/mss=; b=5+0z0m3nx0fWmIRXLPUy06988G5L+GlGooBMkm+d5uS8jpRSVkXu0cvVfAmPRIHdExNl/d jQlrWqKPCk4KrKzinZ8e6Sxz2u5ZZ4cfer9nRFMgv2uEAlUIQ1OEjYv/R1NlAkyPTuN2nP 3oUsB+SWC0/+K3sGC8lTc3yQ89bSgtQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717645236; a=rsa-sha256; cv=none; b=aA/OjAo9Oe4XCr4/zSujQUvO5nh/X9n7KHf8lW74jAezllqYROqcgM/zGHw0Lq7G/qdltY JaKenvVMfekNm7tLMN3vkOIPrpRwjGTiVE7b6ttFM/MPASf2r5chqdvR5/pbt1b2MRiXr3 +jgP8Ha525xc7cXmrfvbizAPfUI1KXg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=m1llcncS; spf=pass (imf07.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1f6559668e1so4418265ad.3 for ; Wed, 05 Jun 2024 20:40:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717645235; x=1718250035; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=m0xWv0/mIfZjELLkhKXh+CJWnTqcZ0qlcrVWS0o/mss=; b=m1llcncSYuhGMUC74q8JA/t5+095dfqqArK657Bmgjn+uTI5tCsBjbaYcCxfxYdyeY 3XUcYXhp87BBr5sJGsqrK+tqFbK9xnimQN4+HvPo7jKPSf830Gx7xrWHHB9SmmLJ5ZOa FkeBDpADN143ksls+sPRcl0c9kacPrOIYRCT1Imn3t7w4j4lOeOF1KW3ORj4zGQKUCmS zgXqBYZrAqmwigLAx5jszFOF2vm0cJmxWDapCxi3WBoQUT0cJW1aMaTDXlm2gPOij09N Cw1I7AnknQF/CuW33hoimM472bKYF+d+7mYYVVpyuP+eXBK8QjHv6WLgZ8YwvUGNrDPb CZow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717645235; x=1718250035; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=m0xWv0/mIfZjELLkhKXh+CJWnTqcZ0qlcrVWS0o/mss=; b=GULZrz4IfzcUiZoWlMD3J6OVXbQupx3avVCRnPe+OyCwOwjx8WZcm/eerZDLe3/AmT RE7IyuQLJl0pJbuOnlptHFLupSpcN3TbXLQyq2+AtvHhZh5lDCvyATsE150+3RZMjT8e LpOK1kFnitY/Ey9rc24kbm7UfMEpFa6xOEM91hvk8/o1eI31QC/jTp00jliNBfZfZy4E 3GRRWC/f+Eu10N/RbPA4/Ypjj5iLWKE31OFd5BXLgqwdm2qMg5U721L2BKR23fdgeKv6 gxDshjJdOOoP3Uiofu/pVbir4NjJq1VmRka10h6OunDAZY1s12t3GO1XzNHqN+p6DS3g iEcA== X-Forwarded-Encrypted: i=1; AJvYcCWlNy5khmLeWfnjoVoCWhEBcLYpyXyKLO3SJNZirSUpr6KbbrDMNW1wpQZsYYxuLIhozCTyGKLAk1NjQC9PTu2MllU= X-Gm-Message-State: AOJu0YwSND2rmWs10tTaGAyoW0CrO67Zp5wSd0TPU9QMLexH8ldvKCC9 4MrOqiNeg5DCIopubcVQ+mkettEqluPsU1rnZljHDUh276vLrdyb X-Google-Smtp-Source: AGHT+IFUSRZyXANCiLPmbIEh7GVsB2DiB97BgrZ+OGnoxcZ2TyJBSstC44wcPExOyKYNeOsgSJTxpw== X-Received: by 2002:a17:902:e88f:b0:1f6:1778:7f1b with SMTP id d9443c01a7336-1f6a5901d12mr51704285ad.1.1717645235264; Wed, 05 Jun 2024 20:40:35 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6bd7ccf8bsm3148205ad.181.2024.06.05.20.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 20:40:34 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, david@redhat.com, linux-mm@kvack.org Cc: chrisl@kernel.org, kasong@tencent.com, linux-kernel@vger.kernel.org, minchan@kernel.org, ryan.roberts@arm.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org Subject: [PATCH] mm: introduce pmd|pte_need_soft_dirty_wp helpers for softdirty write-protect Date: Thu, 6 Jun 2024 15:40:16 +1200 Message-Id: <20240606034016.82559-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 1bqwdxo6gbmj1kp14ejyqcyr4mmc374t X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 955044000B X-HE-Tag: 1717645236-374660 X-HE-Meta: U2FsdGVkX1/PljesIeHonqI9HPKJhr+O4xH95HJ/qdjGDiHb/nH5OsKfwJeifPpVPZNjvRrfb9NxDkKCotqFe04CQ12tg4hiWjAaHJgRmjPlnQWoeK8y5gUn50MzQrvWwUwrjWLtjCmJc52MlJJ4SiY5FIeYRXuPc+K/Ey46WONofEhXOyQ9VRRfq5Pq7gDJTEEQXw819ehZYQiJ31mEaxP0/HvckONH9l3yKBoNbn0Bc0HyqYqIInHGkzbFPVUXjAUSWvEwsMqwgukA4qa7HRWWd6UgwbRkLlCIBZllm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xbSVRUbgCoAjnjlMwDBVTCJHydc7kUDGuH37/nIPU+5FGfJMs/kvhErxLAiV7xzzVQG/5DvenrACmaW4qf6l48WY2cAOngiZknN+4MQKVmbqiuUijFOtKWUn1aKBFC0eKzzB47odgsfuPyAfp1McIRQpdJ4eqhq4WR0IWgM+PC8q5ebZB25Gx69UX1t44ySbS6w81mzYR2oYzPSjSUnzaqH7vWixYWB31DvdDqNxUbksy8iYTOKdGDFubohLfpGSjTWFokk5iw== 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: From: Barry Song This patch introduces the pte_need_soft_dirty_wp and pmd_need_soft_dirty_wp helpers to determine if write protection is required for softdirty tracking. This can enhance code readability and improve its overall appearance. These new helpers are utilized in gup, huge_memory, and protect, and are particularly applied in do_swap_page() to optimize a softdirty scenario where mkwrite can still be performed. Suggested-by: David Hildenbrand Signed-off-by: Barry Song --- -v1: this is suggested by David here: https://lore.kernel.org/linux-mm/baf84b51-7e8a-4da8-9662-3f5cf14ad6f6@redhat.com/ thanks! mm/gup.c | 4 ++-- mm/huge_memory.c | 2 +- mm/internal.h | 10 ++++++++++ mm/memory.c | 2 +- mm/mprotect.c | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 83e279731d1b..756d5416df9c 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -820,7 +820,7 @@ static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page, return false; /* ... and a write-fault isn't required for other reasons. */ - if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd)) + if (pmd_need_soft_dirty_wp(vma, pmd)) return false; return !userfaultfd_huge_pmd_wp(vma, pmd); } @@ -941,7 +941,7 @@ static inline bool can_follow_write_pte(pte_t pte, struct page *page, return false; /* ... and a write-fault isn't required for other reasons. */ - if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte)) + if (pte_need_soft_dirty_wp(vma, pte)) return false; return !userfaultfd_pte_wp(vma, pte); } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 3fbcd77f5957..8fbb62f6e491 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1625,7 +1625,7 @@ static inline bool can_change_pmd_writable(struct vm_area_struct *vma, return false; /* Do we need write faults for softdirty tracking? */ - if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd)) + if (pmd_need_soft_dirty_wp(vma, pmd)) return false; /* Do we need write faults for uffd-wp tracking? */ diff --git a/mm/internal.h b/mm/internal.h index 12e95fdf61e9..51551626da68 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -1348,6 +1348,16 @@ static inline bool vma_soft_dirty_enabled(struct vm_area_struct *vma) return !(vma->vm_flags & VM_SOFTDIRTY); } +static inline bool pmd_need_soft_dirty_wp(struct vm_area_struct *vma, pmd_t pmd) +{ + return vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd); +} + +static inline bool pte_need_soft_dirty_wp(struct vm_area_struct *vma, pte_t pte) +{ + return vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte); +} + static inline void vma_iter_config(struct vma_iterator *vmi, unsigned long index, unsigned long last) { diff --git a/mm/memory.c b/mm/memory.c index db9130488231..6307c43796aa 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4322,7 +4322,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) if (!folio_test_ksm(folio) && (exclusive || folio_ref_count(folio) == 1)) { if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) && - !vma_soft_dirty_enabled(vma)) { + !pte_need_soft_dirty_wp(vma, pte)) { pte = pte_mkwrite(pte, vma); if (vmf->flags & FAULT_FLAG_WRITE) { pte = pte_mkdirty(pte); diff --git a/mm/mprotect.c b/mm/mprotect.c index 888ef66468db..5aea9ad11ae1 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -53,7 +53,7 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr, return false; /* Do we need write faults for softdirty tracking? */ - if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte)) + if (pte_need_soft_dirty_wp(vma, pte)) return false; /* Do we need write faults for uffd-wp tracking? */