From nobody Thu Feb 12 19:00:57 2026 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 9E5312D600 for ; Fri, 7 Jun 2024 21:14:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717794866; cv=none; b=a3Y8Jr7ygSLuhz76UpRTz47wRfnSi2MSG1bYLsTqTeQ2IG33t8E/dwwngUICEaLOeKv3LTMySuiXa6fDfTpalCVWrFGDXFs5EdjRPAOaEBqxYgBsKyRq2x1AQGAEsylSdS1i6nn9XzrziKHWoxS3XIUUJcN0+GuqJwC4IPf3qbE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717794866; c=relaxed/simple; bh=H42gb2DitcbJvZ0il0BlBxvvhBZnL9oKHsL4JaAXjtk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PFSN/Xl3V3bx+Bb6Php2cwryzqJsnBTjenBkFrdrMh1Egj4+EPWhbtCRZkRhNknZnrzO3fyqapwH/JZfkbfW87Aai8hwB+HTMlg1izEpuLMxhxnicwd48H23QQZdTVT5OrVSBFq9CkZh0sXdHYayPTbbKEwmOCerQLk79/6AI6w= 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=KjsVL9Jh; arc=none smtp.client-ip=209.85.210.182 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KjsVL9Jh" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-702508bf0a9so2147251b3a.0 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=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=3EhyAEXMHGWRHoN2nWQIucIAZ80NTOZFSo695eAloq0=; b=KjsVL9JhtFSXAIONZN3TySiV5+Fba/mknISo7SoOpgcOTib/GCz+0ZwC7aAcroI3D8 EcUBlSlFHCgpan5UKeHhvvt2Q4oqf4byNbmFa+HTb3blBvDKg8Ue3UDHFZhV9cGLbdI9 cVZFCRn69OCGhE+vLH2zs9JmsYN79JRZTJRcVTB/qGatz0tGRwBI80Jde93bbvFdmDXh OqnyWAcLhgM+YMhWmRBm0CGPFa4yv2kVAE2CbfSFiTaSUyZDwykcnJhpLeqTsWHJqrSM pVWEM5XR8Y8xoVe2sRYDgCQ7Xix05/ZZ8buLy4LrGNN+Z0UgsbWIRimhmz77WStLYIOY lqQg== 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=wTPCeywNlGhLv7kmHqM/d7BOREpz/k3/wW+CjrM7RKcI0dKPrk8Ca5wjQHOeavz7wg QyI19ZDz0R7iD1j3V8JMYlpGBJhIDrikEzJe7ZjAXJR4uWpSPWwbm2rucMO/ojOmZt+a PVwULlJf0QbKs5IgTD+PMn2mmXHgtCFuBWF/e0W741Huaniw+THUEHGVI88KoafEd5be Yz7IN/KYKJYYqxIESGBnrgLdqpSkwXC+E51ZmCmRqu4e801E05l/8fn59bmxhjiJtm+9 SrPaksdhMSAk/E1j9hddmir99OtYZ2oIYLYzIfGmIsg8cvmoew0VbvuCKVv9uMaWXQI+ YE7w== X-Forwarded-Encrypted: i=1; AJvYcCVrwzmENFeUF40EKWW7ZDflgHI20ROk3yL8Ynxv5VYhREnWrsMTwHgmSlFBRKqzriUqJpg03nceEh34eG5aM8cdln4UQFn1PFqtV9sD X-Gm-Message-State: AOJu0YyOmw6Db4CaLwvDNfaGBL6K8mVn2ZrqJZJakejm34QsUN1A214W kuIWqYfysiVjsVWqPBtN7H0RzBsqR+n8ri2u06bvKDBtjzxAEd0R 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Acked-by: David Hildenbrand --- 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, stru= ct page *page, return false; =20 /* ... 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, stru= ct page *page, return false; =20 /* ... 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; =20 /* 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; =20 /* 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); } =20 +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; =20 /* 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; =20 /* Do we need write faults for uffd-wp tracking? */ --=20 2.34.1 From nobody Thu Feb 12 19:00:57 2026 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 5291C155384 for ; Fri, 7 Jun 2024 21:14:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717794870; cv=none; b=CY7lKs6rVxoq6n/P8RcM6QD2kVbDRQFpKx0rSkDzq14I5A5lPjxc02CUUp3qFr+VOc3BNnTe6sVMDFC7WDjtFVyf/CLTFu5T6qmn6OxPYvuOtmFy/4s3Sso82ekVfXZhFzhx6puNu01UcJcaMsnccxLOHdbAZMQhx7txD5kn2IA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717794870; c=relaxed/simple; bh=JAVkFYMuGScehCz9ExhfnppPuqiO/nTupfV+sN9N3to=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RSpElVDlCi9hOJIn2T2AP0sRVgfaAG3uPJg6PQL38WZp+uVVNZ4pRgcnRYRrZES/54F6VxSfCOaMvUxZYAmYn5sfuj2IGUwQFgnSX+VvX+xBz5fNZhnPjdRYlCIAaaDNrwpZn1z+e8j5CZgvXJZUTgk2ihp55NI3GKA6whdzzHQ= 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=YwcFxsFT; arc=none smtp.client-ip=209.85.214.181 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YwcFxsFT" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1f6c7cded01so14782605ad.0 for ; Fri, 07 Jun 2024 14:14:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717794869; x=1718399669; 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=VWi1D+MWFV3PdLNdsuc+9ZlTB2gIrgKafnL+RRtVsTs=; b=YwcFxsFT3WYvoXlhpN/lqw4fEGZaB194CVl4W2Iu2LUXQLvU7uqRSLx0a7pqW3+1NB O50ZI91PqOXG+5v61FlxaYHR4FeVrrp2rOUJgoDjAA1ZnN9PJBVmconYAbr8whYes0kk 825EAE2xQx8qytzVato6Mi7OzMYhEWO7phCHS5nIJJ6Z5s7dnniaIRSrnQSCqHvruBFf KFGTpm4gQwW6hnmYLzvnENCsH71cIVrDQLmSQBgr642RXow+UisNwVkWw/KhLCQ3XMqC iVQ/N4aZZthUgB7W0NqB7SWAXNcdxw6uQ5wstqpqeTubKoZhJLRDVoBVAZZAg+W+yEPi ZZXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717794869; x=1718399669; 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=VWi1D+MWFV3PdLNdsuc+9ZlTB2gIrgKafnL+RRtVsTs=; b=Ls3TAnjZFSDFKmx8tEP+R5vryMLXNj5X0ngMyJKX0TlGvgzqGkK00bHNSQuQCKqSpk qkwx0aCRdf3qiLHh2qSJPyf13Pmn+KkrA+u5eJIUmNL9TTa017vs1EkITM0TcM5zLMFt TPIt0RCrcPnoRgTsC921zIhuik/HaCKUBNMCzowz6Kpif1vIpGgUy9LXIl1hMow/EYU4 saK0lmRRggaCq+QJ+kXIoiQgAywrHRbDR6Gnk587dMSVQjdJd+3OINaY4WNGW5LdKOiz yI8UyzRgKA8a8u/AFPgSwW71VOO91rhIanXUaSeQJZmE52p0wcfFvozEC0GvqgDGLUtM 8Oig== X-Forwarded-Encrypted: i=1; AJvYcCW6OTMKBMzbrxHNU7XMRDkvL6zGa/SNaEq+d5kdJ4hccMOyevfc7LwapEc8TXdLWVXjItjaMh6kU0b8hGTgtRxPDs/vFWREI0JoeOWM X-Gm-Message-State: AOJu0YxRyeIqrT8ghF4fPiv17yJOWxnYYKKpOSuhi+0LqupndsT/9Czs Oeih77FhDqryLMiX9y/MFOdenyOFhh7XKGAC0wYXc7Pmk5SQByYNBzV7SQ== X-Google-Smtp-Source: AGHT+IG6pwtK/hl5JOm7TirT0nRdsgcUjVPOoWYAZpxsEa+99qZaWFnUTQy5hXv8A03dkjib73+KUQ== X-Received: by 2002:a17:903:234b:b0:1e0:bae4:48f9 with SMTP id d9443c01a7336-1f6d02f3c76mr39672315ad.32.1717794868604; Fri, 07 Jun 2024 14:14:28 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 14:14:28 -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 2/2] mm: set pte writable while pte_soft_dirty() is true in do_swap_page() Date: Sat, 8 Jun 2024 09:13:58 +1200 Message-Id: <20240607211358.4660-3-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Barry Song This patch leverages the new pte_needs_soft_dirty_wp() helper to optimize a scenario where softdirty is enabled, but the softdirty flag has already been set in do_swap_page(). In this situation, we can use pte_mkwrite instead of applying write-protection since we don't depend on write faults. Suggested-by: David Hildenbrand Signed-off-by: Barry Song Acked-by: David Hildenbrand --- mm/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index db9130488231..a063e489446d 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) =3D=3D 1)) { if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) && - !vma_soft_dirty_enabled(vma)) { + !pte_needs_soft_dirty_wp(vma, pte)) { pte =3D pte_mkwrite(pte, vma); if (vmf->flags & FAULT_FLAG_WRITE) { pte =3D pte_mkdirty(pte); --=20 2.34.1