From patchwork Fri Jun 7 21:13:57 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: 13690659 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 C5308C27C55 for ; Fri, 7 Jun 2024 21:14:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BAF36B0096; Fri, 7 Jun 2024 17:14:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 56ACA6B00A4; Fri, 7 Jun 2024 17:14:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 40C4D6B00A5; Fri, 7 Jun 2024 17:14:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2157A6B0096 for ; Fri, 7 Jun 2024 17:14:27 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CC8381C2F4E for ; Fri, 7 Jun 2024 21:14:26 +0000 (UTC) X-FDA: 82205346132.14.8C04BC9 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf21.hostedemail.com (Postfix) with ESMTP id 0FC431C0003 for ; Fri, 7 Jun 2024 21:14:24 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cH6OrdfC; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717794865; 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=3EhyAEXMHGWRHoN2nWQIucIAZ80NTOZFSo695eAloq0=; b=GZv6W8/HE7lHl00Dg5T1OvspEYcvIeqSvXNKAr1B3lA1KcBGkxwkhPnwd7m5N0rkx2DT/l em6K8T8MuR3Nv6qVZ5DEX4/1rWESB9A/crmDByzjQt4Cm7f7SgLO3Knd0p64g3mty70ocX K8CKbvGBu5jjb9l3EfwLbX3zF25NCME= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717794865; a=rsa-sha256; cv=none; b=ZU3LA+fBDcoT2WLWO9j3Aef6sjM1Cecwctb8d0VfLPrmsqG7j2+B2S6wpLjQ9xi1h3ZBwZ dUKttx6onUsxE/3l3BtSaZe7UOjHJG0k+posT5skp7nSRn/zNbfyo4vIN1yz0hrgFVph5f DEK2XnA4arhMUigIYzfSmTmecLK32js= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cH6OrdfC; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=21cnbao@gmail.com Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7024cd9dd3dso2108804b3a.3 for ; Fri, 07 Jun 2024 14:14:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717794864; x=1718399664; 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=3EhyAEXMHGWRHoN2nWQIucIAZ80NTOZFSo695eAloq0=; b=cH6OrdfCdgVpDMuru8BZxRIhoJLgU3Lrf65M9LqhfCGhHwyCacOVob9iEU0oviagRc aVPxGuiLLMQaah7JoI1Be6OxRpDiXrjitDWmIozGwRuM51vBOZAf78Llq68fX0JcFa+Y 0vdJh4qV/C1b8zmQFGHBmCkbmeU5gZtZetb4CiZvbdL6M+tBuPgRr1RwVScxU74vdAXc 39P/9sFa+BjM8XR04DqLm6RtSU4QuSVotUmqro5j9zW5s8XmtQMchHuwhYUi+CNVu7iY 4ReCcTT9eN0QQo1quNy9wRusgqcfGsnCJnYKuqaHr3F6L2xolbwcTxXFKLY7uk9HNA/9 oelg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717794864; x=1718399664; 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=3EhyAEXMHGWRHoN2nWQIucIAZ80NTOZFSo695eAloq0=; b=hXPhU2v8kn3IRsK6sBj3zBapghYFwJhc8d3tNyIQshmOOpjd+R9KU1+PR4U/xPrel+ jx+/NlR2/iplmCCvyyDseRCUBulnho2lcXhF9t8h6RYvDxnsULTQfSZNX8kXVVgQgxgX gnfygXDX05URVdbTRDHFwunCStRbVRtvtqBcOGcOiP/C+cYjdYlb0mKqE+wynZMg+nZL /8+WF85fqWuKxD2bInFsrNmvYgSKCtNlqO+GoCYMy1odsActIYsX7qJUyXJV5l+0F903 +5HDFx8L75eR3TMym29LSncb9nRpY3S7qIXTe1vSznM5zpY3jzi5549oYh2xmraJiXv0 uXqg== X-Forwarded-Encrypted: i=1; AJvYcCW9AaTYNPJuD593krxdipAas4/45WJDZjAUbiN4kpjvGVyhlnL4EhCjybV/+xJQr7wgA2fQdcgXe5f7+NX4uNStKHE= X-Gm-Message-State: AOJu0YwTiqvw7aUZShFns2EROLeA/RNxnbKfsGMXNLnYjnJIzOGx1VXo ej6ZnT2MNnP4VcrI5u+xCVO0Nq5oiCGnYJ3nqtjaTnrELV1qDzz1 X-Google-Smtp-Source: AGHT+IGVoYiDVigicI/nhRgC8cicg0NVdYKQ8BhxWWU6cBhpa44pUV1eORXN83dUfhdGXGQ3pcR2lA== X-Received: by 2002:a05:6a20:918f:b0:1a9:dd82:a42a with SMTP id adf61e73a8af0-1b2f9a5f4f4mr4021460637.31.1717794863849; Fri, 07 Jun 2024 14:14:23 -0700 (PDT) Received: from barry-desktop.hub ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f6d859ea3csm17912255ad.178.2024.06.07.14.14.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 14:14:23 -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 v2 1/2] mm: introduce pmd|pte_needs_soft_dirty_wp helpers for softdirty write-protect Date: Sat, 8 Jun 2024 09:13:57 +1200 Message-Id: <20240607211358.4660-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240607211358.4660-1-21cnbao@gmail.com> References: <20240607211358.4660-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: zx81bkmxsedzd48qz99edk9ajghm1dgh X-Rspamd-Queue-Id: 0FC431C0003 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1717794864-477737 X-HE-Meta: U2FsdGVkX18fHPzpQnt4ikaKFAnQ7RZgrXghFCFU0ndDeVte8eBlNkGPuKLzWGu1dwmxSRkEwBhc4abBJfhgnPi94/pBhKly0vvqK9QlQr67N+EVgzLPexvfhdDdVRp5xNbRXP6na7fan/ecY3U4jLovhjgV5vGSDCgo+nmP2mgSwa30QVKRWOZSd7+a651Smr1UJBGDF9Ev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8FwWCP5LDQ4ejV8hI0Nu/Yu5aC0hrA14GYAfn4c2oDCUVG2kb/bnKIMhVsfbMxFkP1ynsTb8wcsixuZnqDsO2RII5YkNjpseRviIFSQ1cPSq0R68P3WPYbrr+ajbwkU72jVBJiut9OzXFC+Z1aROHGr3BVj9mog7Aevw8VSkaGM6d9F9SplPF4kaolbAvJ2li74rhPt5mMwRnUv6uKKsbDgQSPuquMiEXw9CubNflsjfzL0mWtdpIEHYxy7LG1b4FePq8zDVnUUIp4utO+D86uMb7lY0oaofhFqEjiyNqMlDy2njDatONd2jrA== 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_needs_soft_dirty_wp and pmd_needs_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 then utilized in gup, huge_memory, and mprotect. Suggested-by: David Hildenbrand Signed-off-by: Barry Song --- mm/gup.c | 4 ++-- mm/huge_memory.c | 2 +- mm/internal.h | 10 ++++++++++ mm/mprotect.c | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 83e279731d1b..f15756a3646a 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_needs_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_needs_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..b29daea9c317 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_needs_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..b7853fd57d9a 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_needs_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_needs_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/mprotect.c b/mm/mprotect.c index 888ef66468db..222ab434da54 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_needs_soft_dirty_wp(vma, pte)) return false; /* Do we need write faults for uffd-wp tracking? */